]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/net/e1000_spi.c
rockchip: rk3036: Add clock driver
[people/ms/u-boot.git] / drivers / net / e1000_spi.c
CommitLineData
c752cd2a 1#include <common.h>
24b852a7 2#include <console.h>
ce5207e1 3#include "e1000.h"
deb7282f 4#include <linux/compiler.h>
ce5207e1
KM
5
6/*-----------------------------------------------------------------------
7 * SPI transfer
8 *
9 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
10 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
11 *
12 * The source of the outgoing bits is the "dout" parameter and the
13 * destination of the input bits is the "din" parameter. Note that "dout"
14 * and "din" can point to the same memory location, in which case the
15 * input data overwrites the output data (since both are buffered by
16 * temporary variables, this is OK).
17 *
18 * This may be interrupted with Ctrl-C if "intr" is true, otherwise it will
19 * never return an error.
20 */
21static int e1000_spi_xfer(struct e1000_hw *hw, unsigned int bitlen,
472d5460 22 const void *dout_mem, void *din_mem, bool intr)
ce5207e1
KM
23{
24 const uint8_t *dout = dout_mem;
25 uint8_t *din = din_mem;
26
27 uint8_t mask = 0;
28 uint32_t eecd;
29 unsigned long i;
30
31 /* Pre-read the control register */
32 eecd = E1000_READ_REG(hw, EECD);
33
34 /* Iterate over each bit */
35 for (i = 0, mask = 0x80; i < bitlen; i++, mask = (mask >> 1)?:0x80) {
36 /* Check for interrupt */
37 if (intr && ctrlc())
38 return -1;
39
40 /* Determine the output bit */
41 if (dout && dout[i >> 3] & mask)
42 eecd |= E1000_EECD_DI;
43 else
44 eecd &= ~E1000_EECD_DI;
45
46 /* Write the output bit and wait 50us */
47 E1000_WRITE_REG(hw, EECD, eecd);
48 E1000_WRITE_FLUSH(hw);
49 udelay(50);
50
51 /* Poke the clock (waits 50us) */
52 e1000_raise_ee_clk(hw, &eecd);
53
54 /* Now read the input bit */
55 eecd = E1000_READ_REG(hw, EECD);
56 if (din) {
57 if (eecd & E1000_EECD_DO)
58 din[i >> 3] |= mask;
59 else
60 din[i >> 3] &= ~mask;
61 }
62
63 /* Poke the clock again (waits 50us) */
64 e1000_lower_ee_clk(hw, &eecd);
65 }
66
67 /* Now clear any remaining bits of the input */
68 if (din && (i & 7))
69 din[i >> 3] &= ~((mask << 1) - 1);
70
71 return 0;
72}
73
74#ifdef CONFIG_E1000_SPI_GENERIC
75static inline struct e1000_hw *e1000_hw_from_spi(struct spi_slave *spi)
76{
77 return container_of(spi, struct e1000_hw, spi);
78}
79
80/* Not sure why all of these are necessary */
81void spi_init_r(void) { /* Nothing to do */ }
82void spi_init_f(void) { /* Nothing to do */ }
83void spi_init(void) { /* Nothing to do */ }
84
85struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
86 unsigned int max_hz, unsigned int mode)
87{
88 /* Find the right PCI device */
89 struct e1000_hw *hw = e1000_find_card(bus);
90 if (!hw) {
91 printf("ERROR: No such e1000 device: e1000#%u\n", bus);
92 return NULL;
93 }
94
95 /* Make sure it has an SPI chip */
96 if (hw->eeprom.type != e1000_eeprom_spi) {
97 E1000_ERR(hw->nic, "No attached SPI EEPROM found!\n");
98 return NULL;
99 }
100
101 /* Argument sanity checks */
102 if (cs != 0) {
103 E1000_ERR(hw->nic, "No such SPI chip: %u\n", cs);
104 return NULL;
105 }
106 if (mode != SPI_MODE_0) {
107 E1000_ERR(hw->nic, "Only SPI MODE-0 is supported!\n");
108 return NULL;
109 }
110
111 /* TODO: Use max_hz somehow */
112 E1000_DBG(hw->nic, "EEPROM SPI access requested\n");
113 return &hw->spi;
114}
115
116void spi_free_slave(struct spi_slave *spi)
117{
deb7282f 118 __maybe_unused struct e1000_hw *hw = e1000_hw_from_spi(spi);
ce5207e1
KM
119 E1000_DBG(hw->nic, "EEPROM SPI access released\n");
120}
121
122int spi_claim_bus(struct spi_slave *spi)
123{
124 struct e1000_hw *hw = e1000_hw_from_spi(spi);
125
126 if (e1000_acquire_eeprom(hw)) {
127 E1000_ERR(hw->nic, "EEPROM SPI cannot be acquired!\n");
128 return -1;
129 }
130
131 return 0;
132}
133
134void spi_release_bus(struct spi_slave *spi)
135{
136 struct e1000_hw *hw = e1000_hw_from_spi(spi);
137 e1000_release_eeprom(hw);
138}
139
140/* Skinny wrapper around e1000_spi_xfer */
141int spi_xfer(struct spi_slave *spi, unsigned int bitlen,
142 const void *dout_mem, void *din_mem, unsigned long flags)
143{
144 struct e1000_hw *hw = e1000_hw_from_spi(spi);
145 int ret;
146
147 if (flags & SPI_XFER_BEGIN)
148 e1000_standby_eeprom(hw);
149
472d5460 150 ret = e1000_spi_xfer(hw, bitlen, dout_mem, din_mem, true);
ce5207e1
KM
151
152 if (flags & SPI_XFER_END)
153 e1000_standby_eeprom(hw);
154
155 return ret;
156}
157
158#endif /* not CONFIG_E1000_SPI_GENERIC */
159
160#ifdef CONFIG_CMD_E1000
161
162/* The EEPROM opcodes */
163#define SPI_EEPROM_ENABLE_WR 0x06
164#define SPI_EEPROM_DISABLE_WR 0x04
165#define SPI_EEPROM_WRITE_STATUS 0x01
166#define SPI_EEPROM_READ_STATUS 0x05
167#define SPI_EEPROM_WRITE_PAGE 0x02
168#define SPI_EEPROM_READ_PAGE 0x03
169
170/* The EEPROM status bits */
171#define SPI_EEPROM_STATUS_BUSY 0x01
172#define SPI_EEPROM_STATUS_WREN 0x02
173
472d5460 174static int e1000_spi_eeprom_enable_wr(struct e1000_hw *hw, bool intr)
ce5207e1
KM
175{
176 u8 op[] = { SPI_EEPROM_ENABLE_WR };
177 e1000_standby_eeprom(hw);
178 return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
179}
180
181/*
182 * These have been tested to perform correctly, but they are not used by any
183 * of the EEPROM commands at this time.
184 */
185#if 0
472d5460 186static int e1000_spi_eeprom_disable_wr(struct e1000_hw *hw, bool intr)
ce5207e1
KM
187{
188 u8 op[] = { SPI_EEPROM_DISABLE_WR };
189 e1000_standby_eeprom(hw);
190 return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
191}
192
193static int e1000_spi_eeprom_write_status(struct e1000_hw *hw,
472d5460 194 u8 status, bool intr)
ce5207e1
KM
195{
196 u8 op[] = { SPI_EEPROM_WRITE_STATUS, status };
197 e1000_standby_eeprom(hw);
198 return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
199}
200#endif
201
472d5460 202static int e1000_spi_eeprom_read_status(struct e1000_hw *hw, bool intr)
ce5207e1
KM
203{
204 u8 op[] = { SPI_EEPROM_READ_STATUS, 0 };
205 e1000_standby_eeprom(hw);
206 if (e1000_spi_xfer(hw, 8*sizeof(op), op, op, intr))
207 return -1;
208 return op[1];
209}
210
211static int e1000_spi_eeprom_write_page(struct e1000_hw *hw,
472d5460 212 const void *data, u16 off, u16 len, bool intr)
ce5207e1
KM
213{
214 u8 op[] = {
215 SPI_EEPROM_WRITE_PAGE,
216 (off >> (hw->eeprom.address_bits - 8)) & 0xff, off & 0xff
217 };
218
219 e1000_standby_eeprom(hw);
220
221 if (e1000_spi_xfer(hw, 8 + hw->eeprom.address_bits, op, NULL, intr))
222 return -1;
223 if (e1000_spi_xfer(hw, len << 3, data, NULL, intr))
224 return -1;
225
226 return 0;
227}
228
229static int e1000_spi_eeprom_read_page(struct e1000_hw *hw,
472d5460 230 void *data, u16 off, u16 len, bool intr)
ce5207e1
KM
231{
232 u8 op[] = {
233 SPI_EEPROM_READ_PAGE,
234 (off >> (hw->eeprom.address_bits - 8)) & 0xff, off & 0xff
235 };
236
237 e1000_standby_eeprom(hw);
238
239 if (e1000_spi_xfer(hw, 8 + hw->eeprom.address_bits, op, NULL, intr))
240 return -1;
241 if (e1000_spi_xfer(hw, len << 3, NULL, data, intr))
242 return -1;
243
244 return 0;
245}
246
472d5460 247static int e1000_spi_eeprom_poll_ready(struct e1000_hw *hw, bool intr)
ce5207e1
KM
248{
249 int status;
250 while ((status = e1000_spi_eeprom_read_status(hw, intr)) >= 0) {
251 if (!(status & SPI_EEPROM_STATUS_BUSY))
252 return 0;
253 }
254 return -1;
255}
256
257static int e1000_spi_eeprom_dump(struct e1000_hw *hw,
472d5460 258 void *data, u16 off, unsigned int len, bool intr)
ce5207e1
KM
259{
260 /* Interruptibly wait for the EEPROM to be ready */
261 if (e1000_spi_eeprom_poll_ready(hw, intr))
262 return -1;
263
264 /* Dump each page in sequence */
265 while (len) {
266 /* Calculate the data bytes on this page */
267 u16 pg_off = off & (hw->eeprom.page_size - 1);
268 u16 pg_len = hw->eeprom.page_size - pg_off;
269 if (pg_len > len)
270 pg_len = len;
271
272 /* Now dump the page */
273 if (e1000_spi_eeprom_read_page(hw, data, off, pg_len, intr))
274 return -1;
275
276 /* Otherwise go on to the next page */
277 len -= pg_len;
278 off += pg_len;
279 data += pg_len;
280 }
281
282 /* We're done! */
283 return 0;
284}
285
286static int e1000_spi_eeprom_program(struct e1000_hw *hw,
472d5460 287 const void *data, u16 off, u16 len, bool intr)
ce5207e1
KM
288{
289 /* Program each page in sequence */
290 while (len) {
291 /* Calculate the data bytes on this page */
292 u16 pg_off = off & (hw->eeprom.page_size - 1);
293 u16 pg_len = hw->eeprom.page_size - pg_off;
294 if (pg_len > len)
295 pg_len = len;
296
297 /* Interruptibly wait for the EEPROM to be ready */
298 if (e1000_spi_eeprom_poll_ready(hw, intr))
299 return -1;
300
301 /* Enable write access */
302 if (e1000_spi_eeprom_enable_wr(hw, intr))
303 return -1;
304
305 /* Now program the page */
306 if (e1000_spi_eeprom_write_page(hw, data, off, pg_len, intr))
307 return -1;
308
309 /* Otherwise go on to the next page */
310 len -= pg_len;
311 off += pg_len;
312 data += pg_len;
313 }
314
315 /* Wait for the last write to complete */
316 if (e1000_spi_eeprom_poll_ready(hw, intr))
317 return -1;
318
319 /* We're done! */
320 return 0;
321}
322
323static int do_e1000_spi_show(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
324 int argc, char * const argv[])
325{
326 unsigned int length = 0;
327 u16 i, offset = 0;
328 u8 *buffer;
329 int err;
330
331 if (argc > 2) {
332 cmd_usage(cmdtp);
333 return 1;
334 }
335
336 /* Parse the offset and length */
337 if (argc >= 1)
338 offset = simple_strtoul(argv[0], NULL, 0);
339 if (argc == 2)
340 length = simple_strtoul(argv[1], NULL, 0);
341 else if (offset < (hw->eeprom.word_size << 1))
342 length = (hw->eeprom.word_size << 1) - offset;
343
344 /* Extra sanity checks */
345 if (!length) {
346 E1000_ERR(hw->nic, "Requested zero-sized dump!\n");
347 return 1;
348 }
349 if ((0x10000 < length) || (0x10000 - length < offset)) {
350 E1000_ERR(hw->nic, "Can't dump past 0xFFFF!\n");
351 return 1;
352 }
353
354 /* Allocate a buffer to hold stuff */
355 buffer = malloc(length);
356 if (!buffer) {
357 E1000_ERR(hw->nic, "Out of Memory!\n");
358 return 1;
359 }
360
361 /* Acquire the EEPROM and perform the dump */
362 if (e1000_acquire_eeprom(hw)) {
363 E1000_ERR(hw->nic, "EEPROM SPI cannot be acquired!\n");
364 free(buffer);
365 return 1;
366 }
472d5460 367 err = e1000_spi_eeprom_dump(hw, buffer, offset, length, true);
ce5207e1
KM
368 e1000_release_eeprom(hw);
369 if (err) {
370 E1000_ERR(hw->nic, "Interrupted!\n");
371 free(buffer);
372 return 1;
373 }
374
375 /* Now hexdump the result */
376 printf("%s: ===== Intel e1000 EEPROM (0x%04hX - 0x%04hX) =====",
377 hw->nic->name, offset, offset + length - 1);
378 for (i = 0; i < length; i++) {
379 if ((i & 0xF) == 0)
380 printf("\n%s: %04hX: ", hw->nic->name, offset + i);
381 else if ((i & 0xF) == 0x8)
382 printf(" ");
383 printf(" %02hx", buffer[i]);
384 }
385 printf("\n");
386
387 /* Success! */
388 free(buffer);
389 return 0;
390}
391
392static int do_e1000_spi_dump(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
393 int argc, char * const argv[])
394{
395 unsigned int length;
396 u16 offset;
397 void *dest;
398
399 if (argc != 3) {
400 cmd_usage(cmdtp);
401 return 1;
402 }
403
404 /* Parse the arguments */
405 dest = (void *)simple_strtoul(argv[0], NULL, 16);
406 offset = simple_strtoul(argv[1], NULL, 0);
407 length = simple_strtoul(argv[2], NULL, 0);
408
409 /* Extra sanity checks */
410 if (!length) {
411 E1000_ERR(hw->nic, "Requested zero-sized dump!\n");
412 return 1;
413 }
414 if ((0x10000 < length) || (0x10000 - length < offset)) {
415 E1000_ERR(hw->nic, "Can't dump past 0xFFFF!\n");
416 return 1;
417 }
418
419 /* Acquire the EEPROM */
420 if (e1000_acquire_eeprom(hw)) {
421 E1000_ERR(hw->nic, "EEPROM SPI cannot be acquired!\n");
422 return 1;
423 }
424
425 /* Perform the programming operation */
472d5460 426 if (e1000_spi_eeprom_dump(hw, dest, offset, length, true) < 0) {
ce5207e1
KM
427 E1000_ERR(hw->nic, "Interrupted!\n");
428 e1000_release_eeprom(hw);
429 return 1;
430 }
431
432 e1000_release_eeprom(hw);
433 printf("%s: ===== EEPROM DUMP COMPLETE =====\n", hw->nic->name);
434 return 0;
435}
436
437static int do_e1000_spi_program(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
438 int argc, char * const argv[])
439{
440 unsigned int length;
441 const void *source;
442 u16 offset;
443
444 if (argc != 3) {
445 cmd_usage(cmdtp);
446 return 1;
447 }
448
449 /* Parse the arguments */
450 source = (const void *)simple_strtoul(argv[0], NULL, 16);
451 offset = simple_strtoul(argv[1], NULL, 0);
452 length = simple_strtoul(argv[2], NULL, 0);
453
454 /* Acquire the EEPROM */
455 if (e1000_acquire_eeprom(hw)) {
456 E1000_ERR(hw->nic, "EEPROM SPI cannot be acquired!\n");
457 return 1;
458 }
459
460 /* Perform the programming operation */
472d5460 461 if (e1000_spi_eeprom_program(hw, source, offset, length, true) < 0) {
ce5207e1
KM
462 E1000_ERR(hw->nic, "Interrupted!\n");
463 e1000_release_eeprom(hw);
464 return 1;
465 }
466
467 e1000_release_eeprom(hw);
468 printf("%s: ===== EEPROM PROGRAMMED =====\n", hw->nic->name);
469 return 0;
470}
471
472static int do_e1000_spi_checksum(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
473 int argc, char * const argv[])
474{
deb7282f 475 uint16_t i, length, checksum = 0, checksum_reg;
ce5207e1 476 uint16_t *buffer;
472d5460 477 bool upd;
ce5207e1
KM
478
479 if (argc == 0)
480 upd = 0;
481 else if ((argc == 1) && !strcmp(argv[0], "update"))
482 upd = 1;
483 else {
484 cmd_usage(cmdtp);
485 return 1;
486 }
487
488 /* Allocate a temporary buffer */
489 length = sizeof(uint16_t) * (EEPROM_CHECKSUM_REG + 1);
490 buffer = malloc(length);
491 if (!buffer) {
492 E1000_ERR(hw->nic, "Unable to allocate EEPROM buffer!\n");
493 return 1;
494 }
495
496 /* Acquire the EEPROM */
497 if (e1000_acquire_eeprom(hw)) {
498 E1000_ERR(hw->nic, "EEPROM SPI cannot be acquired!\n");
499 return 1;
500 }
501
502 /* Read the EEPROM */
472d5460 503 if (e1000_spi_eeprom_dump(hw, buffer, 0, length, true) < 0) {
ce5207e1
KM
504 E1000_ERR(hw->nic, "Interrupted!\n");
505 e1000_release_eeprom(hw);
506 return 1;
507 }
508
509 /* Compute the checksum and read the expected value */
510 for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
511 checksum += le16_to_cpu(buffer[i]);
512 checksum = ((uint16_t)EEPROM_SUM) - checksum;
513 checksum_reg = le16_to_cpu(buffer[i]);
514
515 /* Verify it! */
516 if (checksum_reg == checksum) {
517 printf("%s: INFO: EEPROM checksum is correct! (0x%04hx)\n",
518 hw->nic->name, checksum);
519 e1000_release_eeprom(hw);
520 return 0;
521 }
522
523 /* Hrm, verification failed, print an error */
524 E1000_ERR(hw->nic, "EEPROM checksum is incorrect!\n");
525 E1000_ERR(hw->nic, " ...register was 0x%04hx, calculated 0x%04hx\n",
526 checksum_reg, checksum);
527
528 /* If they didn't ask us to update it, just return an error */
529 if (!upd) {
530 e1000_release_eeprom(hw);
531 return 1;
532 }
533
534 /* Ok, correct it! */
535 printf("%s: Reprogramming the EEPROM checksum...\n", hw->nic->name);
536 buffer[i] = cpu_to_le16(checksum);
537 if (e1000_spi_eeprom_program(hw, &buffer[i], i * sizeof(uint16_t),
472d5460 538 sizeof(uint16_t), true)) {
ce5207e1
KM
539 E1000_ERR(hw->nic, "Interrupted!\n");
540 e1000_release_eeprom(hw);
541 return 1;
542 }
543
544 e1000_release_eeprom(hw);
545 return 0;
546}
547
548int do_e1000_spi(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
549 int argc, char * const argv[])
550{
551 if (argc < 1) {
552 cmd_usage(cmdtp);
553 return 1;
554 }
555
556 /* Make sure it has an SPI chip */
557 if (hw->eeprom.type != e1000_eeprom_spi) {
558 E1000_ERR(hw->nic, "No attached SPI EEPROM found!\n");
559 return 1;
560 }
561
562 /* Check the eeprom sub-sub-command arguments */
563 if (!strcmp(argv[0], "show"))
564 return do_e1000_spi_show(cmdtp, hw, argc - 1, argv + 1);
565
566 if (!strcmp(argv[0], "dump"))
567 return do_e1000_spi_dump(cmdtp, hw, argc - 1, argv + 1);
568
569 if (!strcmp(argv[0], "program"))
570 return do_e1000_spi_program(cmdtp, hw, argc - 1, argv + 1);
571
572 if (!strcmp(argv[0], "checksum"))
573 return do_e1000_spi_checksum(cmdtp, hw, argc - 1, argv + 1);
574
575 cmd_usage(cmdtp);
576 return 1;
577}
578
579#endif /* not CONFIG_CMD_E1000 */