]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/net/e1000_spi.c
3 /*-----------------------------------------------------------------------
6 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
7 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
9 * The source of the outgoing bits is the "dout" parameter and the
10 * destination of the input bits is the "din" parameter. Note that "dout"
11 * and "din" can point to the same memory location, in which case the
12 * input data overwrites the output data (since both are buffered by
13 * temporary variables, this is OK).
15 * This may be interrupted with Ctrl-C if "intr" is true, otherwise it will
16 * never return an error.
18 static int e1000_spi_xfer(struct e1000_hw
*hw
, unsigned int bitlen
,
19 const void *dout_mem
, void *din_mem
, boolean_t intr
)
21 const uint8_t *dout
= dout_mem
;
22 uint8_t *din
= din_mem
;
28 /* Pre-read the control register */
29 eecd
= E1000_READ_REG(hw
, EECD
);
31 /* Iterate over each bit */
32 for (i
= 0, mask
= 0x80; i
< bitlen
; i
++, mask
= (mask
>> 1)?:0x80) {
33 /* Check for interrupt */
37 /* Determine the output bit */
38 if (dout
&& dout
[i
>> 3] & mask
)
39 eecd
|= E1000_EECD_DI
;
41 eecd
&= ~E1000_EECD_DI
;
43 /* Write the output bit and wait 50us */
44 E1000_WRITE_REG(hw
, EECD
, eecd
);
45 E1000_WRITE_FLUSH(hw
);
48 /* Poke the clock (waits 50us) */
49 e1000_raise_ee_clk(hw
, &eecd
);
51 /* Now read the input bit */
52 eecd
= E1000_READ_REG(hw
, EECD
);
54 if (eecd
& E1000_EECD_DO
)
60 /* Poke the clock again (waits 50us) */
61 e1000_lower_ee_clk(hw
, &eecd
);
64 /* Now clear any remaining bits of the input */
66 din
[i
>> 3] &= ~((mask
<< 1) - 1);
71 #ifdef CONFIG_E1000_SPI_GENERIC
72 static inline struct e1000_hw
*e1000_hw_from_spi(struct spi_slave
*spi
)
74 return container_of(spi
, struct e1000_hw
, spi
);
77 /* Not sure why all of these are necessary */
78 void spi_init_r(void) { /* Nothing to do */ }
79 void spi_init_f(void) { /* Nothing to do */ }
80 void spi_init(void) { /* Nothing to do */ }
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
->nic
, "No attached SPI EEPROM found!\n");
98 /* Argument sanity checks */
100 E1000_ERR(hw
->nic
, "No such SPI chip: %u\n", cs
);
103 if (mode
!= SPI_MODE_0
) {
104 E1000_ERR(hw
->nic
, "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 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
->nic
, "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
, boolean_t 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.
183 static int e1000_spi_eeprom_disable_wr(struct e1000_hw
*hw
, boolean_t intr
)
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 int e1000_spi_eeprom_write_status(struct e1000_hw
*hw
,
191 u8 status
, boolean_t 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
);
199 static int e1000_spi_eeprom_read_status(struct e1000_hw
*hw
, boolean_t intr
)
201 u8 op
[] = { SPI_EEPROM_READ_STATUS
, 0 };
202 e1000_standby_eeprom(hw
);
203 if (e1000_spi_xfer(hw
, 8*sizeof(op
), op
, op
, intr
))
208 static int e1000_spi_eeprom_write_page(struct e1000_hw
*hw
,
209 const void *data
, u16 off
, u16 len
, boolean_t intr
)
212 SPI_EEPROM_WRITE_PAGE
,
213 (off
>> (hw
->eeprom
.address_bits
- 8)) & 0xff, off
& 0xff
216 e1000_standby_eeprom(hw
);
218 if (e1000_spi_xfer(hw
, 8 + hw
->eeprom
.address_bits
, op
, NULL
, intr
))
220 if (e1000_spi_xfer(hw
, len
<< 3, data
, NULL
, intr
))
226 static int e1000_spi_eeprom_read_page(struct e1000_hw
*hw
,
227 void *data
, u16 off
, u16 len
, boolean_t intr
)
230 SPI_EEPROM_READ_PAGE
,
231 (off
>> (hw
->eeprom
.address_bits
- 8)) & 0xff, off
& 0xff
234 e1000_standby_eeprom(hw
);
236 if (e1000_spi_xfer(hw
, 8 + hw
->eeprom
.address_bits
, op
, NULL
, intr
))
238 if (e1000_spi_xfer(hw
, len
<< 3, NULL
, data
, intr
))
244 static int e1000_spi_eeprom_poll_ready(struct e1000_hw
*hw
, boolean_t intr
)
247 while ((status
= e1000_spi_eeprom_read_status(hw
, intr
)) >= 0) {
248 if (!(status
& SPI_EEPROM_STATUS_BUSY
))
254 static int e1000_spi_eeprom_dump(struct e1000_hw
*hw
,
255 void *data
, u16 off
, unsigned int len
, boolean_t intr
)
257 /* Interruptibly wait for the EEPROM to be ready */
258 if (e1000_spi_eeprom_poll_ready(hw
, intr
))
261 /* Dump each page in sequence */
263 /* Calculate the data bytes on this page */
264 u16 pg_off
= off
& (hw
->eeprom
.page_size
- 1);
265 u16 pg_len
= hw
->eeprom
.page_size
- pg_off
;
269 /* Now dump the page */
270 if (e1000_spi_eeprom_read_page(hw
, data
, off
, pg_len
, intr
))
273 /* Otherwise go on to the next page */
283 static int e1000_spi_eeprom_program(struct e1000_hw
*hw
,
284 const void *data
, u16 off
, u16 len
, boolean_t intr
)
286 /* Program each page in sequence */
288 /* Calculate the data bytes on this page */
289 u16 pg_off
= off
& (hw
->eeprom
.page_size
- 1);
290 u16 pg_len
= hw
->eeprom
.page_size
- pg_off
;
294 /* Interruptibly wait for the EEPROM to be ready */
295 if (e1000_spi_eeprom_poll_ready(hw
, intr
))
298 /* Enable write access */
299 if (e1000_spi_eeprom_enable_wr(hw
, intr
))
302 /* Now program the page */
303 if (e1000_spi_eeprom_write_page(hw
, data
, off
, pg_len
, intr
))
306 /* Otherwise go on to the next page */
312 /* Wait for the last write to complete */
313 if (e1000_spi_eeprom_poll_ready(hw
, intr
))
320 static int do_e1000_spi_show(cmd_tbl_t
*cmdtp
, struct e1000_hw
*hw
,
321 int argc
, char * const argv
[])
323 unsigned int length
= 0;
333 /* Parse the offset and length */
335 offset
= simple_strtoul(argv
[0], NULL
, 0);
337 length
= simple_strtoul(argv
[1], NULL
, 0);
338 else if (offset
< (hw
->eeprom
.word_size
<< 1))
339 length
= (hw
->eeprom
.word_size
<< 1) - offset
;
341 /* Extra sanity checks */
343 E1000_ERR(hw
->nic
, "Requested zero-sized dump!\n");
346 if ((0x10000 < length
) || (0x10000 - length
< offset
)) {
347 E1000_ERR(hw
->nic
, "Can't dump past 0xFFFF!\n");
351 /* Allocate a buffer to hold stuff */
352 buffer
= malloc(length
);
354 E1000_ERR(hw
->nic
, "Out of Memory!\n");
358 /* Acquire the EEPROM and perform the dump */
359 if (e1000_acquire_eeprom(hw
)) {
360 E1000_ERR(hw
->nic
, "EEPROM SPI cannot be acquired!\n");
364 err
= e1000_spi_eeprom_dump(hw
, buffer
, offset
, length
, TRUE
);
365 e1000_release_eeprom(hw
);
367 E1000_ERR(hw
->nic
, "Interrupted!\n");
372 /* Now hexdump the result */
373 printf("%s: ===== Intel e1000 EEPROM (0x%04hX - 0x%04hX) =====",
374 hw
->nic
->name
, offset
, offset
+ length
- 1);
375 for (i
= 0; i
< length
; i
++) {
377 printf("\n%s: %04hX: ", hw
->nic
->name
, offset
+ i
);
378 else if ((i
& 0xF) == 0x8)
380 printf(" %02hx", buffer
[i
]);
389 static int do_e1000_spi_dump(cmd_tbl_t
*cmdtp
, struct e1000_hw
*hw
,
390 int argc
, char * const argv
[])
401 /* Parse the arguments */
402 dest
= (void *)simple_strtoul(argv
[0], NULL
, 16);
403 offset
= simple_strtoul(argv
[1], NULL
, 0);
404 length
= simple_strtoul(argv
[2], NULL
, 0);
406 /* Extra sanity checks */
408 E1000_ERR(hw
->nic
, "Requested zero-sized dump!\n");
411 if ((0x10000 < length
) || (0x10000 - length
< offset
)) {
412 E1000_ERR(hw
->nic
, "Can't dump past 0xFFFF!\n");
416 /* Acquire the EEPROM */
417 if (e1000_acquire_eeprom(hw
)) {
418 E1000_ERR(hw
->nic
, "EEPROM SPI cannot be acquired!\n");
422 /* Perform the programming operation */
423 if (e1000_spi_eeprom_dump(hw
, dest
, offset
, length
, TRUE
) < 0) {
424 E1000_ERR(hw
->nic
, "Interrupted!\n");
425 e1000_release_eeprom(hw
);
429 e1000_release_eeprom(hw
);
430 printf("%s: ===== EEPROM DUMP COMPLETE =====\n", hw
->nic
->name
);
434 static int do_e1000_spi_program(cmd_tbl_t
*cmdtp
, struct e1000_hw
*hw
,
435 int argc
, char * const argv
[])
446 /* Parse the arguments */
447 source
= (const void *)simple_strtoul(argv
[0], NULL
, 16);
448 offset
= simple_strtoul(argv
[1], NULL
, 0);
449 length
= simple_strtoul(argv
[2], NULL
, 0);
451 /* Acquire the EEPROM */
452 if (e1000_acquire_eeprom(hw
)) {
453 E1000_ERR(hw
->nic
, "EEPROM SPI cannot be acquired!\n");
457 /* Perform the programming operation */
458 if (e1000_spi_eeprom_program(hw
, source
, offset
, length
, TRUE
) < 0) {
459 E1000_ERR(hw
->nic
, "Interrupted!\n");
460 e1000_release_eeprom(hw
);
464 e1000_release_eeprom(hw
);
465 printf("%s: ===== EEPROM PROGRAMMED =====\n", hw
->nic
->name
);
469 static int do_e1000_spi_checksum(cmd_tbl_t
*cmdtp
, struct e1000_hw
*hw
,
470 int argc
, char * const argv
[])
472 uint16_t i
, length
, checksum
, checksum_reg
;
478 else if ((argc
== 1) && !strcmp(argv
[0], "update"))
485 /* Allocate a temporary buffer */
486 length
= sizeof(uint16_t) * (EEPROM_CHECKSUM_REG
+ 1);
487 buffer
= malloc(length
);
489 E1000_ERR(hw
->nic
, "Unable to allocate EEPROM buffer!\n");
493 /* Acquire the EEPROM */
494 if (e1000_acquire_eeprom(hw
)) {
495 E1000_ERR(hw
->nic
, "EEPROM SPI cannot be acquired!\n");
499 /* Read the EEPROM */
500 if (e1000_spi_eeprom_dump(hw
, buffer
, 0, length
, TRUE
) < 0) {
501 E1000_ERR(hw
->nic
, "Interrupted!\n");
502 e1000_release_eeprom(hw
);
506 /* Compute the checksum and read the expected value */
507 for (i
= 0; i
< EEPROM_CHECKSUM_REG
; i
++)
508 checksum
+= le16_to_cpu(buffer
[i
]);
509 checksum
= ((uint16_t)EEPROM_SUM
) - checksum
;
510 checksum_reg
= le16_to_cpu(buffer
[i
]);
513 if (checksum_reg
== checksum
) {
514 printf("%s: INFO: EEPROM checksum is correct! (0x%04hx)\n",
515 hw
->nic
->name
, checksum
);
516 e1000_release_eeprom(hw
);
520 /* Hrm, verification failed, print an error */
521 E1000_ERR(hw
->nic
, "EEPROM checksum is incorrect!\n");
522 E1000_ERR(hw
->nic
, " ...register was 0x%04hx, calculated 0x%04hx\n",
523 checksum_reg
, checksum
);
525 /* If they didn't ask us to update it, just return an error */
527 e1000_release_eeprom(hw
);
531 /* Ok, correct it! */
532 printf("%s: Reprogramming the EEPROM checksum...\n", hw
->nic
->name
);
533 buffer
[i
] = cpu_to_le16(checksum
);
534 if (e1000_spi_eeprom_program(hw
, &buffer
[i
], i
* sizeof(uint16_t),
535 sizeof(uint16_t), TRUE
)) {
536 E1000_ERR(hw
->nic
, "Interrupted!\n");
537 e1000_release_eeprom(hw
);
541 e1000_release_eeprom(hw
);
545 int do_e1000_spi(cmd_tbl_t
*cmdtp
, struct e1000_hw
*hw
,
546 int argc
, char * const argv
[])
553 /* Make sure it has an SPI chip */
554 if (hw
->eeprom
.type
!= e1000_eeprom_spi
) {
555 E1000_ERR(hw
->nic
, "No attached SPI EEPROM found!\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 */