5 #include <linux/compiler.h>
7 /*-----------------------------------------------------------------------
10 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
11 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
13 * The source of the outgoing bits is the "dout" parameter and the
14 * destination of the input bits is the "din" parameter. Note that "dout"
15 * and "din" can point to the same memory location, in which case the
16 * input data overwrites the output data (since both are buffered by
17 * temporary variables, this is OK).
19 * This may be interrupted with Ctrl-C if "intr" is true, otherwise it will
20 * never return an error.
22 static int e1000_spi_xfer(struct e1000_hw *hw, unsigned int bitlen,
23 const void *dout_mem, void *din_mem, bool intr)
25 const uint8_t *dout = dout_mem;
26 uint8_t *din = din_mem;
32 /* Pre-read the control register */
33 eecd = E1000_READ_REG(hw, EECD);
35 /* Iterate over each bit */
36 for (i = 0, mask = 0x80; i < bitlen; i++, mask = (mask >> 1)?:0x80) {
37 /* Check for interrupt */
41 /* Determine the output bit */
42 if (dout && dout[i >> 3] & mask)
43 eecd |= E1000_EECD_DI;
45 eecd &= ~E1000_EECD_DI;
47 /* Write the output bit and wait 50us */
48 E1000_WRITE_REG(hw, EECD, eecd);
49 E1000_WRITE_FLUSH(hw);
52 /* Poke the clock (waits 50us) */
53 e1000_raise_ee_clk(hw, &eecd);
55 /* Now read the input bit */
56 eecd = E1000_READ_REG(hw, EECD);
58 if (eecd & E1000_EECD_DO)
64 /* Poke the clock again (waits 50us) */
65 e1000_lower_ee_clk(hw, &eecd);
68 /* Now clear any remaining bits of the input */
70 din[i >> 3] &= ~((mask << 1) - 1);
75 #ifdef CONFIG_E1000_SPI_GENERIC
76 static inline struct e1000_hw *e1000_hw_from_spi(struct spi_slave *spi)
78 return container_of(spi, struct e1000_hw, spi);
81 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
82 unsigned int max_hz, unsigned int mode)
84 /* Find the right PCI device */
85 struct e1000_hw *hw = e1000_find_card(bus);
87 printf("ERROR: No such e1000 device: e1000#%u\n", bus);
91 /* Make sure it has an SPI chip */
92 if (hw->eeprom.type != e1000_eeprom_spi) {
93 E1000_ERR(hw, "No attached SPI EEPROM found!\n");
97 /* Argument sanity checks */
99 E1000_ERR(hw, "No such SPI chip: %u\n", cs);
102 if (mode != SPI_MODE_0) {
103 E1000_ERR(hw, "Only SPI MODE-0 is supported!\n");
107 /* TODO: Use max_hz somehow */
108 E1000_DBG(hw->nic, "EEPROM SPI access requested\n");
112 void spi_free_slave(struct spi_slave *spi)
114 __maybe_unused struct e1000_hw *hw = e1000_hw_from_spi(spi);
115 E1000_DBG(hw->nic, "EEPROM SPI access released\n");
118 int spi_claim_bus(struct spi_slave *spi)
120 struct e1000_hw *hw = e1000_hw_from_spi(spi);
122 if (e1000_acquire_eeprom(hw)) {
123 E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
130 void spi_release_bus(struct spi_slave *spi)
132 struct e1000_hw *hw = e1000_hw_from_spi(spi);
133 e1000_release_eeprom(hw);
136 /* Skinny wrapper around e1000_spi_xfer */
137 int spi_xfer(struct spi_slave *spi, unsigned int bitlen,
138 const void *dout_mem, void *din_mem, unsigned long flags)
140 struct e1000_hw *hw = e1000_hw_from_spi(spi);
143 if (flags & SPI_XFER_BEGIN)
144 e1000_standby_eeprom(hw);
146 ret = e1000_spi_xfer(hw, bitlen, dout_mem, din_mem, true);
148 if (flags & SPI_XFER_END)
149 e1000_standby_eeprom(hw);
154 #endif /* not CONFIG_E1000_SPI_GENERIC */
156 #ifdef CONFIG_CMD_E1000
158 /* The EEPROM opcodes */
159 #define SPI_EEPROM_ENABLE_WR 0x06
160 #define SPI_EEPROM_DISABLE_WR 0x04
161 #define SPI_EEPROM_WRITE_STATUS 0x01
162 #define SPI_EEPROM_READ_STATUS 0x05
163 #define SPI_EEPROM_WRITE_PAGE 0x02
164 #define SPI_EEPROM_READ_PAGE 0x03
166 /* The EEPROM status bits */
167 #define SPI_EEPROM_STATUS_BUSY 0x01
168 #define SPI_EEPROM_STATUS_WREN 0x02
170 static int e1000_spi_eeprom_enable_wr(struct e1000_hw *hw, bool intr)
172 u8 op[] = { SPI_EEPROM_ENABLE_WR };
173 e1000_standby_eeprom(hw);
174 return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
178 * These have been tested to perform correctly, but they are not used by any
179 * of the EEPROM commands at this time.
181 static __maybe_unused int e1000_spi_eeprom_disable_wr(struct e1000_hw *hw,
184 u8 op[] = { SPI_EEPROM_DISABLE_WR };
185 e1000_standby_eeprom(hw);
186 return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
189 static __maybe_unused int e1000_spi_eeprom_write_status(struct e1000_hw *hw,
190 u8 status, bool intr)
192 u8 op[] = { SPI_EEPROM_WRITE_STATUS, status };
193 e1000_standby_eeprom(hw);
194 return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
197 static int e1000_spi_eeprom_read_status(struct e1000_hw *hw, bool intr)
199 u8 op[] = { SPI_EEPROM_READ_STATUS, 0 };
200 e1000_standby_eeprom(hw);
201 if (e1000_spi_xfer(hw, 8*sizeof(op), op, op, intr))
206 static int e1000_spi_eeprom_write_page(struct e1000_hw *hw,
207 const void *data, u16 off, u16 len, bool intr)
210 SPI_EEPROM_WRITE_PAGE,
211 (off >> (hw->eeprom.address_bits - 8)) & 0xff, off & 0xff
214 e1000_standby_eeprom(hw);
216 if (e1000_spi_xfer(hw, 8 + hw->eeprom.address_bits, op, NULL, intr))
218 if (e1000_spi_xfer(hw, len << 3, data, NULL, intr))
224 static int e1000_spi_eeprom_read_page(struct e1000_hw *hw,
225 void *data, u16 off, u16 len, bool intr)
228 SPI_EEPROM_READ_PAGE,
229 (off >> (hw->eeprom.address_bits - 8)) & 0xff, off & 0xff
232 e1000_standby_eeprom(hw);
234 if (e1000_spi_xfer(hw, 8 + hw->eeprom.address_bits, op, NULL, intr))
236 if (e1000_spi_xfer(hw, len << 3, NULL, data, intr))
242 static int e1000_spi_eeprom_poll_ready(struct e1000_hw *hw, bool intr)
245 while ((status = e1000_spi_eeprom_read_status(hw, intr)) >= 0) {
246 if (!(status & SPI_EEPROM_STATUS_BUSY))
252 static int e1000_spi_eeprom_dump(struct e1000_hw *hw,
253 void *data, u16 off, unsigned int len, bool intr)
255 /* Interruptibly wait for the EEPROM to be ready */
256 if (e1000_spi_eeprom_poll_ready(hw, intr))
259 /* Dump each page in sequence */
261 /* Calculate the data bytes on this page */
262 u16 pg_off = off & (hw->eeprom.page_size - 1);
263 u16 pg_len = hw->eeprom.page_size - pg_off;
267 /* Now dump the page */
268 if (e1000_spi_eeprom_read_page(hw, data, off, pg_len, intr))
271 /* Otherwise go on to the next page */
281 static int e1000_spi_eeprom_program(struct e1000_hw *hw,
282 const void *data, u16 off, u16 len, bool intr)
284 /* Program each page in sequence */
286 /* Calculate the data bytes on this page */
287 u16 pg_off = off & (hw->eeprom.page_size - 1);
288 u16 pg_len = hw->eeprom.page_size - pg_off;
292 /* Interruptibly wait for the EEPROM to be ready */
293 if (e1000_spi_eeprom_poll_ready(hw, intr))
296 /* Enable write access */
297 if (e1000_spi_eeprom_enable_wr(hw, intr))
300 /* Now program the page */
301 if (e1000_spi_eeprom_write_page(hw, data, off, pg_len, intr))
304 /* Otherwise go on to the next page */
310 /* Wait for the last write to complete */
311 if (e1000_spi_eeprom_poll_ready(hw, intr))
318 static int do_e1000_spi_show(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
319 int argc, char * const argv[])
321 unsigned int length = 0;
331 /* Parse the offset and length */
333 offset = simple_strtoul(argv[0], NULL, 0);
335 length = simple_strtoul(argv[1], NULL, 0);
336 else if (offset < (hw->eeprom.word_size << 1))
337 length = (hw->eeprom.word_size << 1) - offset;
339 /* Extra sanity checks */
341 E1000_ERR(hw, "Requested zero-sized dump!\n");
344 if ((0x10000 < length) || (0x10000 - length < offset)) {
345 E1000_ERR(hw, "Can't dump past 0xFFFF!\n");
349 /* Allocate a buffer to hold stuff */
350 buffer = malloc(length);
352 E1000_ERR(hw, "Out of Memory!\n");
356 /* Acquire the EEPROM and perform the dump */
357 if (e1000_acquire_eeprom(hw)) {
358 E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
362 err = e1000_spi_eeprom_dump(hw, buffer, offset, length, true);
363 e1000_release_eeprom(hw);
365 E1000_ERR(hw, "Interrupted!\n");
370 /* Now hexdump the result */
371 printf("%s: ===== Intel e1000 EEPROM (0x%04hX - 0x%04hX) =====",
372 hw->name, offset, offset + length - 1);
373 for (i = 0; i < length; i++) {
375 printf("\n%s: %04hX: ", hw->name, offset + i);
376 else if ((i & 0xF) == 0x8)
378 printf(" %02hx", buffer[i]);
387 static int do_e1000_spi_dump(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
388 int argc, char * const argv[])
399 /* Parse the arguments */
400 dest = (void *)simple_strtoul(argv[0], NULL, 16);
401 offset = simple_strtoul(argv[1], NULL, 0);
402 length = simple_strtoul(argv[2], NULL, 0);
404 /* Extra sanity checks */
406 E1000_ERR(hw, "Requested zero-sized dump!\n");
409 if ((0x10000 < length) || (0x10000 - length < offset)) {
410 E1000_ERR(hw, "Can't dump past 0xFFFF!\n");
414 /* Acquire the EEPROM */
415 if (e1000_acquire_eeprom(hw)) {
416 E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
420 /* Perform the programming operation */
421 if (e1000_spi_eeprom_dump(hw, dest, offset, length, true) < 0) {
422 E1000_ERR(hw, "Interrupted!\n");
423 e1000_release_eeprom(hw);
427 e1000_release_eeprom(hw);
428 printf("%s: ===== EEPROM DUMP COMPLETE =====\n", hw->name);
432 static int do_e1000_spi_program(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
433 int argc, char * const argv[])
444 /* Parse the arguments */
445 source = (const void *)simple_strtoul(argv[0], NULL, 16);
446 offset = simple_strtoul(argv[1], NULL, 0);
447 length = simple_strtoul(argv[2], NULL, 0);
449 /* Acquire the EEPROM */
450 if (e1000_acquire_eeprom(hw)) {
451 E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
455 /* Perform the programming operation */
456 if (e1000_spi_eeprom_program(hw, source, offset, length, true) < 0) {
457 E1000_ERR(hw, "Interrupted!\n");
458 e1000_release_eeprom(hw);
462 e1000_release_eeprom(hw);
463 printf("%s: ===== EEPROM PROGRAMMED =====\n", hw->name);
467 static int do_e1000_spi_checksum(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
468 int argc, char * const argv[])
470 uint16_t i, length, checksum = 0, checksum_reg;
476 else if ((argc == 1) && !strcmp(argv[0], "update"))
483 /* Allocate a temporary buffer */
484 length = sizeof(uint16_t) * (EEPROM_CHECKSUM_REG + 1);
485 buffer = malloc(length);
487 E1000_ERR(hw, "Unable to allocate EEPROM buffer!\n");
491 /* Acquire the EEPROM */
492 if (e1000_acquire_eeprom(hw)) {
493 E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
497 /* Read the EEPROM */
498 if (e1000_spi_eeprom_dump(hw, buffer, 0, length, true) < 0) {
499 E1000_ERR(hw, "Interrupted!\n");
500 e1000_release_eeprom(hw);
504 /* Compute the checksum and read the expected value */
505 for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
506 checksum += le16_to_cpu(buffer[i]);
507 checksum = ((uint16_t)EEPROM_SUM) - checksum;
508 checksum_reg = le16_to_cpu(buffer[i]);
511 if (checksum_reg == checksum) {
512 printf("%s: INFO: EEPROM checksum is correct! (0x%04hx)\n",
514 e1000_release_eeprom(hw);
518 /* Hrm, verification failed, print an error */
519 E1000_ERR(hw, "EEPROM checksum is incorrect!\n");
520 E1000_ERR(hw, " ...register was 0x%04hx, calculated 0x%04hx\n",
521 checksum_reg, checksum);
523 /* If they didn't ask us to update it, just return an error */
525 e1000_release_eeprom(hw);
529 /* Ok, correct it! */
530 printf("%s: Reprogramming the EEPROM checksum...\n", hw->name);
531 buffer[i] = cpu_to_le16(checksum);
532 if (e1000_spi_eeprom_program(hw, &buffer[i], i * sizeof(uint16_t),
533 sizeof(uint16_t), true)) {
534 E1000_ERR(hw, "Interrupted!\n");
535 e1000_release_eeprom(hw);
539 e1000_release_eeprom(hw);
543 int do_e1000_spi(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
544 int argc, char * const argv[])
551 /* Make sure it has an SPI chip */
552 if (hw->eeprom.type != e1000_eeprom_spi) {
553 E1000_ERR(hw, "No attached SPI EEPROM found (%d)!\n",
558 /* Check the eeprom sub-sub-command arguments */
559 if (!strcmp(argv[0], "show"))
560 return do_e1000_spi_show(cmdtp, hw, argc - 1, argv + 1);
562 if (!strcmp(argv[0], "dump"))
563 return do_e1000_spi_dump(cmdtp, hw, argc - 1, argv + 1);
565 if (!strcmp(argv[0], "program"))
566 return do_e1000_spi_program(cmdtp, hw, argc - 1, argv + 1);
568 if (!strcmp(argv[0], "checksum"))
569 return do_e1000_spi_checksum(cmdtp, hw, argc - 1, argv + 1);
575 #endif /* not CONFIG_CMD_E1000 */