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