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