6 #include <linux/compiler.h>
8 /*-----------------------------------------------------------------------
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.
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).
20 * This may be interrupted with Ctrl-C if "intr" is true, otherwise it will
21 * never return an error.
23 static int e1000_spi_xfer(struct e1000_hw *hw, unsigned int bitlen,
24 const void *dout_mem, void *din_mem, bool intr)
26 const uint8_t *dout = dout_mem;
27 uint8_t *din = din_mem;
33 /* Pre-read the control register */
34 eecd = E1000_READ_REG(hw, EECD);
36 /* Iterate over each bit */
37 for (i = 0, mask = 0x80; i < bitlen; i++, mask = (mask >> 1)?:0x80) {
38 /* Check for interrupt */
42 /* Determine the output bit */
43 if (dout && dout[i >> 3] & mask)
44 eecd |= E1000_EECD_DI;
46 eecd &= ~E1000_EECD_DI;
48 /* Write the output bit and wait 50us */
49 E1000_WRITE_REG(hw, EECD, eecd);
50 E1000_WRITE_FLUSH(hw);
53 /* Poke the clock (waits 50us) */
54 e1000_raise_ee_clk(hw, &eecd);
56 /* Now read the input bit */
57 eecd = E1000_READ_REG(hw, EECD);
59 if (eecd & E1000_EECD_DO)
65 /* Poke the clock again (waits 50us) */
66 e1000_lower_ee_clk(hw, &eecd);
69 /* Now clear any remaining bits of the input */
71 din[i >> 3] &= ~((mask << 1) - 1);
76 #ifdef CONFIG_E1000_SPI_GENERIC
77 static inline struct e1000_hw *e1000_hw_from_spi(struct spi_slave *spi)
79 return container_of(spi, struct e1000_hw, spi);
82 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
83 unsigned int max_hz, unsigned int mode)
85 /* Find the right PCI device */
86 struct e1000_hw *hw = e1000_find_card(bus);
88 printf("ERROR: No such e1000 device: e1000#%u\n", bus);
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");
98 /* Argument sanity checks */
100 E1000_ERR(hw, "No such SPI chip: %u\n", cs);
103 if (mode != SPI_MODE_0) {
104 E1000_ERR(hw, "Only SPI MODE-0 is supported!\n");
108 /* TODO: Use max_hz somehow */
109 E1000_DBG(hw->nic, "EEPROM SPI access requested\n");
113 void spi_free_slave(struct spi_slave *spi)
115 __maybe_unused struct e1000_hw *hw = e1000_hw_from_spi(spi);
116 E1000_DBG(hw->nic, "EEPROM SPI access released\n");
119 int spi_claim_bus(struct spi_slave *spi)
121 struct e1000_hw *hw = e1000_hw_from_spi(spi);
123 if (e1000_acquire_eeprom(hw)) {
124 E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
131 void spi_release_bus(struct spi_slave *spi)
133 struct e1000_hw *hw = e1000_hw_from_spi(spi);
134 e1000_release_eeprom(hw);
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)
141 struct e1000_hw *hw = e1000_hw_from_spi(spi);
144 if (flags & SPI_XFER_BEGIN)
145 e1000_standby_eeprom(hw);
147 ret = e1000_spi_xfer(hw, bitlen, dout_mem, din_mem, true);
149 if (flags & SPI_XFER_END)
150 e1000_standby_eeprom(hw);
155 #endif /* not CONFIG_E1000_SPI_GENERIC */
157 #ifdef CONFIG_CMD_E1000
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
167 /* The EEPROM status bits */
168 #define SPI_EEPROM_STATUS_BUSY 0x01
169 #define SPI_EEPROM_STATUS_WREN 0x02
171 static int e1000_spi_eeprom_enable_wr(struct e1000_hw *hw, bool intr)
173 u8 op[] = { SPI_EEPROM_ENABLE_WR };
174 e1000_standby_eeprom(hw);
175 return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
179 * These have been tested to perform correctly, but they are not used by any
180 * of the EEPROM commands at this time.
182 static __maybe_unused int e1000_spi_eeprom_disable_wr(struct e1000_hw *hw,
185 u8 op[] = { SPI_EEPROM_DISABLE_WR };
186 e1000_standby_eeprom(hw);
187 return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
190 static __maybe_unused int e1000_spi_eeprom_write_status(struct e1000_hw *hw,
191 u8 status, bool intr)
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);
198 static int e1000_spi_eeprom_read_status(struct e1000_hw *hw, bool intr)
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))
207 static int e1000_spi_eeprom_write_page(struct e1000_hw *hw,
208 const void *data, u16 off, u16 len, bool intr)
211 SPI_EEPROM_WRITE_PAGE,
212 (off >> (hw->eeprom.address_bits - 8)) & 0xff, off & 0xff
215 e1000_standby_eeprom(hw);
217 if (e1000_spi_xfer(hw, 8 + hw->eeprom.address_bits, op, NULL, intr))
219 if (e1000_spi_xfer(hw, len << 3, data, NULL, intr))
225 static int e1000_spi_eeprom_read_page(struct e1000_hw *hw,
226 void *data, u16 off, u16 len, bool intr)
229 SPI_EEPROM_READ_PAGE,
230 (off >> (hw->eeprom.address_bits - 8)) & 0xff, off & 0xff
233 e1000_standby_eeprom(hw);
235 if (e1000_spi_xfer(hw, 8 + hw->eeprom.address_bits, op, NULL, intr))
237 if (e1000_spi_xfer(hw, len << 3, NULL, data, intr))
243 static int e1000_spi_eeprom_poll_ready(struct e1000_hw *hw, bool intr)
246 while ((status = e1000_spi_eeprom_read_status(hw, intr)) >= 0) {
247 if (!(status & SPI_EEPROM_STATUS_BUSY))
253 static int e1000_spi_eeprom_dump(struct e1000_hw *hw,
254 void *data, u16 off, unsigned int len, bool intr)
256 /* Interruptibly wait for the EEPROM to be ready */
257 if (e1000_spi_eeprom_poll_ready(hw, intr))
260 /* Dump each page in sequence */
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;
268 /* Now dump the page */
269 if (e1000_spi_eeprom_read_page(hw, data, off, pg_len, intr))
272 /* Otherwise go on to the next page */
282 static int e1000_spi_eeprom_program(struct e1000_hw *hw,
283 const void *data, u16 off, u16 len, bool intr)
285 /* Program each page in sequence */
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;
293 /* Interruptibly wait for the EEPROM to be ready */
294 if (e1000_spi_eeprom_poll_ready(hw, intr))
297 /* Enable write access */
298 if (e1000_spi_eeprom_enable_wr(hw, intr))
301 /* Now program the page */
302 if (e1000_spi_eeprom_write_page(hw, data, off, pg_len, intr))
305 /* Otherwise go on to the next page */
311 /* Wait for the last write to complete */
312 if (e1000_spi_eeprom_poll_ready(hw, intr))
319 static int do_e1000_spi_show(struct cmd_tbl *cmdtp, struct e1000_hw *hw,
320 int argc, char *const argv[])
322 unsigned int length = 0;
332 /* Parse the offset and length */
334 offset = simple_strtoul(argv[0], NULL, 0);
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;
340 /* Extra sanity checks */
342 E1000_ERR(hw, "Requested zero-sized dump!\n");
345 if ((0x10000 < length) || (0x10000 - length < offset)) {
346 E1000_ERR(hw, "Can't dump past 0xFFFF!\n");
350 /* Allocate a buffer to hold stuff */
351 buffer = malloc(length);
353 E1000_ERR(hw, "Out of Memory!\n");
357 /* Acquire the EEPROM and perform the dump */
358 if (e1000_acquire_eeprom(hw)) {
359 E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
363 err = e1000_spi_eeprom_dump(hw, buffer, offset, length, true);
364 e1000_release_eeprom(hw);
366 E1000_ERR(hw, "Interrupted!\n");
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++) {
376 printf("\n%s: %04hX: ", hw->name, offset + i);
377 else if ((i & 0xF) == 0x8)
379 printf(" %02hx", buffer[i]);
388 static int do_e1000_spi_dump(struct cmd_tbl *cmdtp, struct e1000_hw *hw,
389 int argc, char *const argv[])
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);
405 /* Extra sanity checks */
407 E1000_ERR(hw, "Requested zero-sized dump!\n");
410 if ((0x10000 < length) || (0x10000 - length < offset)) {
411 E1000_ERR(hw, "Can't dump past 0xFFFF!\n");
415 /* Acquire the EEPROM */
416 if (e1000_acquire_eeprom(hw)) {
417 E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
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);
428 e1000_release_eeprom(hw);
429 printf("%s: ===== EEPROM DUMP COMPLETE =====\n", hw->name);
433 static int do_e1000_spi_program(struct cmd_tbl *cmdtp, struct e1000_hw *hw,
434 int argc, char *const argv[])
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);
450 /* Acquire the EEPROM */
451 if (e1000_acquire_eeprom(hw)) {
452 E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
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);
463 e1000_release_eeprom(hw);
464 printf("%s: ===== EEPROM PROGRAMMED =====\n", hw->name);
468 static int do_e1000_spi_checksum(struct cmd_tbl *cmdtp, struct e1000_hw *hw,
469 int argc, char *const argv[])
471 uint16_t i, length, checksum = 0, checksum_reg;
477 else if ((argc == 1) && !strcmp(argv[0], "update"))
484 /* Allocate a temporary buffer */
485 length = sizeof(uint16_t) * (EEPROM_CHECKSUM_REG + 1);
486 buffer = malloc(length);
488 E1000_ERR(hw, "Unable to allocate EEPROM buffer!\n");
492 /* Acquire the EEPROM */
493 if (e1000_acquire_eeprom(hw)) {
494 E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
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);
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]);
512 if (checksum_reg == checksum) {
513 printf("%s: INFO: EEPROM checksum is correct! (0x%04hx)\n",
515 e1000_release_eeprom(hw);
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);
524 /* If they didn't ask us to update it, just return an error */
526 e1000_release_eeprom(hw);
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);
540 e1000_release_eeprom(hw);
544 int do_e1000_spi(struct cmd_tbl *cmdtp, struct e1000_hw *hw,
545 int argc, char *const argv[])
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",
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);
563 if (!strcmp(argv[0], "dump"))
564 return do_e1000_spi_dump(cmdtp, hw, argc - 1, argv + 1);
566 if (!strcmp(argv[0], "program"))
567 return do_e1000_spi_program(cmdtp, hw, argc - 1, argv + 1);
569 if (!strcmp(argv[0], "checksum"))
570 return do_e1000_spi_checksum(cmdtp, hw, argc - 1, argv + 1);
576 #endif /* not CONFIG_CMD_E1000 */