4 #include <linux/compiler.h>
6 /*-----------------------------------------------------------------------
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.
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).
18 * This may be interrupted with Ctrl-C if "intr" is true, otherwise it will
19 * never return an error.
21 static int e1000_spi_xfer(struct e1000_hw *hw, unsigned int bitlen,
22 const void *dout_mem, void *din_mem, bool intr)
24 const uint8_t *dout = dout_mem;
25 uint8_t *din = din_mem;
31 /* Pre-read the control register */
32 eecd = E1000_READ_REG(hw, EECD);
34 /* Iterate over each bit */
35 for (i = 0, mask = 0x80; i < bitlen; i++, mask = (mask >> 1)?:0x80) {
36 /* Check for interrupt */
40 /* Determine the output bit */
41 if (dout && dout[i >> 3] & mask)
42 eecd |= E1000_EECD_DI;
44 eecd &= ~E1000_EECD_DI;
46 /* Write the output bit and wait 50us */
47 E1000_WRITE_REG(hw, EECD, eecd);
48 E1000_WRITE_FLUSH(hw);
51 /* Poke the clock (waits 50us) */
52 e1000_raise_ee_clk(hw, &eecd);
54 /* Now read the input bit */
55 eecd = E1000_READ_REG(hw, EECD);
57 if (eecd & E1000_EECD_DO)
63 /* Poke the clock again (waits 50us) */
64 e1000_lower_ee_clk(hw, &eecd);
67 /* Now clear any remaining bits of the input */
69 din[i >> 3] &= ~((mask << 1) - 1);
74 #ifdef CONFIG_E1000_SPI_GENERIC
75 static inline struct e1000_hw *e1000_hw_from_spi(struct spi_slave *spi)
77 return container_of(spi, struct e1000_hw, spi);
80 /* Not sure why all of these are necessary */
81 void spi_init_r(void) { /* Nothing to do */ }
82 void spi_init_f(void) { /* Nothing to do */ }
83 void spi_init(void) { /* Nothing to do */ }
85 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
86 unsigned int max_hz, unsigned int mode)
88 /* Find the right PCI device */
89 struct e1000_hw *hw = e1000_find_card(bus);
91 printf("ERROR: No such e1000 device: e1000#%u\n", bus);
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");
101 /* Argument sanity checks */
103 E1000_ERR(hw->nic, "No such SPI chip: %u\n", cs);
106 if (mode != SPI_MODE_0) {
107 E1000_ERR(hw->nic, "Only SPI MODE-0 is supported!\n");
111 /* TODO: Use max_hz somehow */
112 E1000_DBG(hw->nic, "EEPROM SPI access requested\n");
116 void spi_free_slave(struct spi_slave *spi)
118 __maybe_unused struct e1000_hw *hw = e1000_hw_from_spi(spi);
119 E1000_DBG(hw->nic, "EEPROM SPI access released\n");
122 int spi_claim_bus(struct spi_slave *spi)
124 struct e1000_hw *hw = e1000_hw_from_spi(spi);
126 if (e1000_acquire_eeprom(hw)) {
127 E1000_ERR(hw->nic, "EEPROM SPI cannot be acquired!\n");
134 void spi_release_bus(struct spi_slave *spi)
136 struct e1000_hw *hw = e1000_hw_from_spi(spi);
137 e1000_release_eeprom(hw);
140 /* Skinny wrapper around e1000_spi_xfer */
141 int spi_xfer(struct spi_slave *spi, unsigned int bitlen,
142 const void *dout_mem, void *din_mem, unsigned long flags)
144 struct e1000_hw *hw = e1000_hw_from_spi(spi);
147 if (flags & SPI_XFER_BEGIN)
148 e1000_standby_eeprom(hw);
150 ret = e1000_spi_xfer(hw, bitlen, dout_mem, din_mem, true);
152 if (flags & SPI_XFER_END)
153 e1000_standby_eeprom(hw);
158 #endif /* not CONFIG_E1000_SPI_GENERIC */
160 #ifdef CONFIG_CMD_E1000
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
170 /* The EEPROM status bits */
171 #define SPI_EEPROM_STATUS_BUSY 0x01
172 #define SPI_EEPROM_STATUS_WREN 0x02
174 static int e1000_spi_eeprom_enable_wr(struct e1000_hw *hw, bool intr)
176 u8 op[] = { SPI_EEPROM_ENABLE_WR };
177 e1000_standby_eeprom(hw);
178 return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
182 * These have been tested to perform correctly, but they are not used by any
183 * of the EEPROM commands at this time.
186 static int e1000_spi_eeprom_disable_wr(struct e1000_hw *hw, bool intr)
188 u8 op[] = { SPI_EEPROM_DISABLE_WR };
189 e1000_standby_eeprom(hw);
190 return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
193 static int e1000_spi_eeprom_write_status(struct e1000_hw *hw,
194 u8 status, bool intr)
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);
202 static int e1000_spi_eeprom_read_status(struct e1000_hw *hw, bool intr)
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))
211 static int e1000_spi_eeprom_write_page(struct e1000_hw *hw,
212 const void *data, u16 off, u16 len, bool intr)
215 SPI_EEPROM_WRITE_PAGE,
216 (off >> (hw->eeprom.address_bits - 8)) & 0xff, off & 0xff
219 e1000_standby_eeprom(hw);
221 if (e1000_spi_xfer(hw, 8 + hw->eeprom.address_bits, op, NULL, intr))
223 if (e1000_spi_xfer(hw, len << 3, data, NULL, intr))
229 static int e1000_spi_eeprom_read_page(struct e1000_hw *hw,
230 void *data, u16 off, u16 len, bool intr)
233 SPI_EEPROM_READ_PAGE,
234 (off >> (hw->eeprom.address_bits - 8)) & 0xff, off & 0xff
237 e1000_standby_eeprom(hw);
239 if (e1000_spi_xfer(hw, 8 + hw->eeprom.address_bits, op, NULL, intr))
241 if (e1000_spi_xfer(hw, len << 3, NULL, data, intr))
247 static int e1000_spi_eeprom_poll_ready(struct e1000_hw *hw, bool intr)
250 while ((status = e1000_spi_eeprom_read_status(hw, intr)) >= 0) {
251 if (!(status & SPI_EEPROM_STATUS_BUSY))
257 static int e1000_spi_eeprom_dump(struct e1000_hw *hw,
258 void *data, u16 off, unsigned int len, bool intr)
260 /* Interruptibly wait for the EEPROM to be ready */
261 if (e1000_spi_eeprom_poll_ready(hw, intr))
264 /* Dump each page in sequence */
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;
272 /* Now dump the page */
273 if (e1000_spi_eeprom_read_page(hw, data, off, pg_len, intr))
276 /* Otherwise go on to the next page */
286 static int e1000_spi_eeprom_program(struct e1000_hw *hw,
287 const void *data, u16 off, u16 len, bool intr)
289 /* Program each page in sequence */
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;
297 /* Interruptibly wait for the EEPROM to be ready */
298 if (e1000_spi_eeprom_poll_ready(hw, intr))
301 /* Enable write access */
302 if (e1000_spi_eeprom_enable_wr(hw, intr))
305 /* Now program the page */
306 if (e1000_spi_eeprom_write_page(hw, data, off, pg_len, intr))
309 /* Otherwise go on to the next page */
315 /* Wait for the last write to complete */
316 if (e1000_spi_eeprom_poll_ready(hw, intr))
323 static int do_e1000_spi_show(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
324 int argc, char * const argv[])
326 unsigned int length = 0;
336 /* Parse the offset and length */
338 offset = simple_strtoul(argv[0], NULL, 0);
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;
344 /* Extra sanity checks */
346 E1000_ERR(hw->nic, "Requested zero-sized dump!\n");
349 if ((0x10000 < length) || (0x10000 - length < offset)) {
350 E1000_ERR(hw->nic, "Can't dump past 0xFFFF!\n");
354 /* Allocate a buffer to hold stuff */
355 buffer = malloc(length);
357 E1000_ERR(hw->nic, "Out of Memory!\n");
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");
367 err = e1000_spi_eeprom_dump(hw, buffer, offset, length, true);
368 e1000_release_eeprom(hw);
370 E1000_ERR(hw->nic, "Interrupted!\n");
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++) {
380 printf("\n%s: %04hX: ", hw->nic->name, offset + i);
381 else if ((i & 0xF) == 0x8)
383 printf(" %02hx", buffer[i]);
392 static int do_e1000_spi_dump(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
393 int argc, char * const argv[])
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);
409 /* Extra sanity checks */
411 E1000_ERR(hw->nic, "Requested zero-sized dump!\n");
414 if ((0x10000 < length) || (0x10000 - length < offset)) {
415 E1000_ERR(hw->nic, "Can't dump past 0xFFFF!\n");
419 /* Acquire the EEPROM */
420 if (e1000_acquire_eeprom(hw)) {
421 E1000_ERR(hw->nic, "EEPROM SPI cannot be acquired!\n");
425 /* Perform the programming operation */
426 if (e1000_spi_eeprom_dump(hw, dest, offset, length, true) < 0) {
427 E1000_ERR(hw->nic, "Interrupted!\n");
428 e1000_release_eeprom(hw);
432 e1000_release_eeprom(hw);
433 printf("%s: ===== EEPROM DUMP COMPLETE =====\n", hw->nic->name);
437 static int do_e1000_spi_program(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
438 int argc, char * const argv[])
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);
454 /* Acquire the EEPROM */
455 if (e1000_acquire_eeprom(hw)) {
456 E1000_ERR(hw->nic, "EEPROM SPI cannot be acquired!\n");
460 /* Perform the programming operation */
461 if (e1000_spi_eeprom_program(hw, source, offset, length, true) < 0) {
462 E1000_ERR(hw->nic, "Interrupted!\n");
463 e1000_release_eeprom(hw);
467 e1000_release_eeprom(hw);
468 printf("%s: ===== EEPROM PROGRAMMED =====\n", hw->nic->name);
472 static int do_e1000_spi_checksum(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
473 int argc, char * const argv[])
475 uint16_t i, length, checksum = 0, checksum_reg;
481 else if ((argc == 1) && !strcmp(argv[0], "update"))
488 /* Allocate a temporary buffer */
489 length = sizeof(uint16_t) * (EEPROM_CHECKSUM_REG + 1);
490 buffer = malloc(length);
492 E1000_ERR(hw->nic, "Unable to allocate EEPROM buffer!\n");
496 /* Acquire the EEPROM */
497 if (e1000_acquire_eeprom(hw)) {
498 E1000_ERR(hw->nic, "EEPROM SPI cannot be acquired!\n");
502 /* Read the EEPROM */
503 if (e1000_spi_eeprom_dump(hw, buffer, 0, length, true) < 0) {
504 E1000_ERR(hw->nic, "Interrupted!\n");
505 e1000_release_eeprom(hw);
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]);
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);
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);
528 /* If they didn't ask us to update it, just return an error */
530 e1000_release_eeprom(hw);
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),
538 sizeof(uint16_t), true)) {
539 E1000_ERR(hw->nic, "Interrupted!\n");
540 e1000_release_eeprom(hw);
544 e1000_release_eeprom(hw);
548 int do_e1000_spi(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
549 int argc, char * const argv[])
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");
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);
566 if (!strcmp(argv[0], "dump"))
567 return do_e1000_spi_dump(cmdtp, hw, argc - 1, argv + 1);
569 if (!strcmp(argv[0], "program"))
570 return do_e1000_spi_program(cmdtp, hw, argc - 1, argv + 1);
572 if (!strcmp(argv[0], "checksum"))
573 return do_e1000_spi_checksum(cmdtp, hw, argc - 1, argv + 1);
579 #endif /* not CONFIG_CMD_E1000 */