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, "No attached SPI EEPROM found!\n");
101 /* Argument sanity checks */
103 E1000_ERR(hw, "No such SPI chip: %u\n", cs);
106 if (mode != SPI_MODE_0) {
107 E1000_ERR(hw, "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, "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.
185 static __maybe_unused int e1000_spi_eeprom_disable_wr(struct e1000_hw *hw,
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 __maybe_unused 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);
201 static int e1000_spi_eeprom_read_status(struct e1000_hw *hw, bool intr)
203 u8 op[] = { SPI_EEPROM_READ_STATUS, 0 };
204 e1000_standby_eeprom(hw);
205 if (e1000_spi_xfer(hw, 8*sizeof(op), op, op, intr))
210 static int e1000_spi_eeprom_write_page(struct e1000_hw *hw,
211 const void *data, u16 off, u16 len, bool intr)
214 SPI_EEPROM_WRITE_PAGE,
215 (off >> (hw->eeprom.address_bits - 8)) & 0xff, off & 0xff
218 e1000_standby_eeprom(hw);
220 if (e1000_spi_xfer(hw, 8 + hw->eeprom.address_bits, op, NULL, intr))
222 if (e1000_spi_xfer(hw, len << 3, data, NULL, intr))
228 static int e1000_spi_eeprom_read_page(struct e1000_hw *hw,
229 void *data, u16 off, u16 len, bool intr)
232 SPI_EEPROM_READ_PAGE,
233 (off >> (hw->eeprom.address_bits - 8)) & 0xff, off & 0xff
236 e1000_standby_eeprom(hw);
238 if (e1000_spi_xfer(hw, 8 + hw->eeprom.address_bits, op, NULL, intr))
240 if (e1000_spi_xfer(hw, len << 3, NULL, data, intr))
246 static int e1000_spi_eeprom_poll_ready(struct e1000_hw *hw, bool intr)
249 while ((status = e1000_spi_eeprom_read_status(hw, intr)) >= 0) {
250 if (!(status & SPI_EEPROM_STATUS_BUSY))
256 static int e1000_spi_eeprom_dump(struct e1000_hw *hw,
257 void *data, u16 off, unsigned int len, bool intr)
259 /* Interruptibly wait for the EEPROM to be ready */
260 if (e1000_spi_eeprom_poll_ready(hw, intr))
263 /* Dump each page in sequence */
265 /* Calculate the data bytes on this page */
266 u16 pg_off = off & (hw->eeprom.page_size - 1);
267 u16 pg_len = hw->eeprom.page_size - pg_off;
271 /* Now dump the page */
272 if (e1000_spi_eeprom_read_page(hw, data, off, pg_len, intr))
275 /* Otherwise go on to the next page */
285 static int e1000_spi_eeprom_program(struct e1000_hw *hw,
286 const void *data, u16 off, u16 len, bool intr)
288 /* Program each page in sequence */
290 /* Calculate the data bytes on this page */
291 u16 pg_off = off & (hw->eeprom.page_size - 1);
292 u16 pg_len = hw->eeprom.page_size - pg_off;
296 /* Interruptibly wait for the EEPROM to be ready */
297 if (e1000_spi_eeprom_poll_ready(hw, intr))
300 /* Enable write access */
301 if (e1000_spi_eeprom_enable_wr(hw, intr))
304 /* Now program the page */
305 if (e1000_spi_eeprom_write_page(hw, data, off, pg_len, intr))
308 /* Otherwise go on to the next page */
314 /* Wait for the last write to complete */
315 if (e1000_spi_eeprom_poll_ready(hw, intr))
322 static int do_e1000_spi_show(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
323 int argc, char * const argv[])
325 unsigned int length = 0;
335 /* Parse the offset and length */
337 offset = simple_strtoul(argv[0], NULL, 0);
339 length = simple_strtoul(argv[1], NULL, 0);
340 else if (offset < (hw->eeprom.word_size << 1))
341 length = (hw->eeprom.word_size << 1) - offset;
343 /* Extra sanity checks */
345 E1000_ERR(hw, "Requested zero-sized dump!\n");
348 if ((0x10000 < length) || (0x10000 - length < offset)) {
349 E1000_ERR(hw, "Can't dump past 0xFFFF!\n");
353 /* Allocate a buffer to hold stuff */
354 buffer = malloc(length);
356 E1000_ERR(hw, "Out of Memory!\n");
360 /* Acquire the EEPROM and perform the dump */
361 if (e1000_acquire_eeprom(hw)) {
362 E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
366 err = e1000_spi_eeprom_dump(hw, buffer, offset, length, true);
367 e1000_release_eeprom(hw);
369 E1000_ERR(hw, "Interrupted!\n");
374 /* Now hexdump the result */
375 printf("%s: ===== Intel e1000 EEPROM (0x%04hX - 0x%04hX) =====",
376 hw->name, offset, offset + length - 1);
377 for (i = 0; i < length; i++) {
379 printf("\n%s: %04hX: ", hw->name, offset + i);
380 else if ((i & 0xF) == 0x8)
382 printf(" %02hx", buffer[i]);
391 static int do_e1000_spi_dump(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
392 int argc, char * const argv[])
403 /* Parse the arguments */
404 dest = (void *)simple_strtoul(argv[0], NULL, 16);
405 offset = simple_strtoul(argv[1], NULL, 0);
406 length = simple_strtoul(argv[2], NULL, 0);
408 /* Extra sanity checks */
410 E1000_ERR(hw, "Requested zero-sized dump!\n");
413 if ((0x10000 < length) || (0x10000 - length < offset)) {
414 E1000_ERR(hw, "Can't dump past 0xFFFF!\n");
418 /* Acquire the EEPROM */
419 if (e1000_acquire_eeprom(hw)) {
420 E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
424 /* Perform the programming operation */
425 if (e1000_spi_eeprom_dump(hw, dest, offset, length, true) < 0) {
426 E1000_ERR(hw, "Interrupted!\n");
427 e1000_release_eeprom(hw);
431 e1000_release_eeprom(hw);
432 printf("%s: ===== EEPROM DUMP COMPLETE =====\n", hw->name);
436 static int do_e1000_spi_program(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
437 int argc, char * const argv[])
448 /* Parse the arguments */
449 source = (const void *)simple_strtoul(argv[0], NULL, 16);
450 offset = simple_strtoul(argv[1], NULL, 0);
451 length = simple_strtoul(argv[2], NULL, 0);
453 /* Acquire the EEPROM */
454 if (e1000_acquire_eeprom(hw)) {
455 E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
459 /* Perform the programming operation */
460 if (e1000_spi_eeprom_program(hw, source, offset, length, true) < 0) {
461 E1000_ERR(hw, "Interrupted!\n");
462 e1000_release_eeprom(hw);
466 e1000_release_eeprom(hw);
467 printf("%s: ===== EEPROM PROGRAMMED =====\n", hw->name);
471 static int do_e1000_spi_checksum(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
472 int argc, char * const argv[])
474 uint16_t i, length, checksum = 0, checksum_reg;
480 else if ((argc == 1) && !strcmp(argv[0], "update"))
487 /* Allocate a temporary buffer */
488 length = sizeof(uint16_t) * (EEPROM_CHECKSUM_REG + 1);
489 buffer = malloc(length);
491 E1000_ERR(hw, "Unable to allocate EEPROM buffer!\n");
495 /* Acquire the EEPROM */
496 if (e1000_acquire_eeprom(hw)) {
497 E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
501 /* Read the EEPROM */
502 if (e1000_spi_eeprom_dump(hw, buffer, 0, length, true) < 0) {
503 E1000_ERR(hw, "Interrupted!\n");
504 e1000_release_eeprom(hw);
508 /* Compute the checksum and read the expected value */
509 for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
510 checksum += le16_to_cpu(buffer[i]);
511 checksum = ((uint16_t)EEPROM_SUM) - checksum;
512 checksum_reg = le16_to_cpu(buffer[i]);
515 if (checksum_reg == checksum) {
516 printf("%s: INFO: EEPROM checksum is correct! (0x%04hx)\n",
518 e1000_release_eeprom(hw);
522 /* Hrm, verification failed, print an error */
523 E1000_ERR(hw, "EEPROM checksum is incorrect!\n");
524 E1000_ERR(hw, " ...register was 0x%04hx, calculated 0x%04hx\n",
525 checksum_reg, checksum);
527 /* If they didn't ask us to update it, just return an error */
529 e1000_release_eeprom(hw);
533 /* Ok, correct it! */
534 printf("%s: Reprogramming the EEPROM checksum...\n", hw->name);
535 buffer[i] = cpu_to_le16(checksum);
536 if (e1000_spi_eeprom_program(hw, &buffer[i], i * sizeof(uint16_t),
537 sizeof(uint16_t), true)) {
538 E1000_ERR(hw, "Interrupted!\n");
539 e1000_release_eeprom(hw);
543 e1000_release_eeprom(hw);
547 int do_e1000_spi(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
548 int argc, char * const argv[])
555 /* Make sure it has an SPI chip */
556 if (hw->eeprom.type != e1000_eeprom_spi) {
557 E1000_ERR(hw, "No attached SPI EEPROM found (%d)!\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 */