e1000_spi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. #include <common.h>
  2. #include <console.h>
  3. #include "e1000.h"
  4. #include <linux/compiler.h>
  5. /*-----------------------------------------------------------------------
  6. * SPI transfer
  7. *
  8. * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
  9. * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
  10. *
  11. * The source of the outgoing bits is the "dout" parameter and the
  12. * destination of the input bits is the "din" parameter. Note that "dout"
  13. * and "din" can point to the same memory location, in which case the
  14. * input data overwrites the output data (since both are buffered by
  15. * temporary variables, this is OK).
  16. *
  17. * This may be interrupted with Ctrl-C if "intr" is true, otherwise it will
  18. * never return an error.
  19. */
  20. static int e1000_spi_xfer(struct e1000_hw *hw, unsigned int bitlen,
  21. const void *dout_mem, void *din_mem, bool intr)
  22. {
  23. const uint8_t *dout = dout_mem;
  24. uint8_t *din = din_mem;
  25. uint8_t mask = 0;
  26. uint32_t eecd;
  27. unsigned long i;
  28. /* Pre-read the control register */
  29. eecd = E1000_READ_REG(hw, EECD);
  30. /* Iterate over each bit */
  31. for (i = 0, mask = 0x80; i < bitlen; i++, mask = (mask >> 1)?:0x80) {
  32. /* Check for interrupt */
  33. if (intr && ctrlc())
  34. return -1;
  35. /* Determine the output bit */
  36. if (dout && dout[i >> 3] & mask)
  37. eecd |= E1000_EECD_DI;
  38. else
  39. eecd &= ~E1000_EECD_DI;
  40. /* Write the output bit and wait 50us */
  41. E1000_WRITE_REG(hw, EECD, eecd);
  42. E1000_WRITE_FLUSH(hw);
  43. udelay(50);
  44. /* Poke the clock (waits 50us) */
  45. e1000_raise_ee_clk(hw, &eecd);
  46. /* Now read the input bit */
  47. eecd = E1000_READ_REG(hw, EECD);
  48. if (din) {
  49. if (eecd & E1000_EECD_DO)
  50. din[i >> 3] |= mask;
  51. else
  52. din[i >> 3] &= ~mask;
  53. }
  54. /* Poke the clock again (waits 50us) */
  55. e1000_lower_ee_clk(hw, &eecd);
  56. }
  57. /* Now clear any remaining bits of the input */
  58. if (din && (i & 7))
  59. din[i >> 3] &= ~((mask << 1) - 1);
  60. return 0;
  61. }
  62. #ifdef CONFIG_E1000_SPI_GENERIC
  63. static inline struct e1000_hw *e1000_hw_from_spi(struct spi_slave *spi)
  64. {
  65. return container_of(spi, struct e1000_hw, spi);
  66. }
  67. /* Not sure why all of these are necessary */
  68. void spi_init_r(void) { /* Nothing to do */ }
  69. void spi_init_f(void) { /* Nothing to do */ }
  70. void spi_init(void) { /* Nothing to do */ }
  71. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  72. unsigned int max_hz, unsigned int mode)
  73. {
  74. /* Find the right PCI device */
  75. struct e1000_hw *hw = e1000_find_card(bus);
  76. if (!hw) {
  77. printf("ERROR: No such e1000 device: e1000#%u\n", bus);
  78. return NULL;
  79. }
  80. /* Make sure it has an SPI chip */
  81. if (hw->eeprom.type != e1000_eeprom_spi) {
  82. E1000_ERR(hw, "No attached SPI EEPROM found!\n");
  83. return NULL;
  84. }
  85. /* Argument sanity checks */
  86. if (cs != 0) {
  87. E1000_ERR(hw, "No such SPI chip: %u\n", cs);
  88. return NULL;
  89. }
  90. if (mode != SPI_MODE_0) {
  91. E1000_ERR(hw, "Only SPI MODE-0 is supported!\n");
  92. return NULL;
  93. }
  94. /* TODO: Use max_hz somehow */
  95. E1000_DBG(hw->nic, "EEPROM SPI access requested\n");
  96. return &hw->spi;
  97. }
  98. void spi_free_slave(struct spi_slave *spi)
  99. {
  100. __maybe_unused struct e1000_hw *hw = e1000_hw_from_spi(spi);
  101. E1000_DBG(hw->nic, "EEPROM SPI access released\n");
  102. }
  103. int spi_claim_bus(struct spi_slave *spi)
  104. {
  105. struct e1000_hw *hw = e1000_hw_from_spi(spi);
  106. if (e1000_acquire_eeprom(hw)) {
  107. E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
  108. return -1;
  109. }
  110. return 0;
  111. }
  112. void spi_release_bus(struct spi_slave *spi)
  113. {
  114. struct e1000_hw *hw = e1000_hw_from_spi(spi);
  115. e1000_release_eeprom(hw);
  116. }
  117. /* Skinny wrapper around e1000_spi_xfer */
  118. int spi_xfer(struct spi_slave *spi, unsigned int bitlen,
  119. const void *dout_mem, void *din_mem, unsigned long flags)
  120. {
  121. struct e1000_hw *hw = e1000_hw_from_spi(spi);
  122. int ret;
  123. if (flags & SPI_XFER_BEGIN)
  124. e1000_standby_eeprom(hw);
  125. ret = e1000_spi_xfer(hw, bitlen, dout_mem, din_mem, true);
  126. if (flags & SPI_XFER_END)
  127. e1000_standby_eeprom(hw);
  128. return ret;
  129. }
  130. #endif /* not CONFIG_E1000_SPI_GENERIC */
  131. #ifdef CONFIG_CMD_E1000
  132. /* The EEPROM opcodes */
  133. #define SPI_EEPROM_ENABLE_WR 0x06
  134. #define SPI_EEPROM_DISABLE_WR 0x04
  135. #define SPI_EEPROM_WRITE_STATUS 0x01
  136. #define SPI_EEPROM_READ_STATUS 0x05
  137. #define SPI_EEPROM_WRITE_PAGE 0x02
  138. #define SPI_EEPROM_READ_PAGE 0x03
  139. /* The EEPROM status bits */
  140. #define SPI_EEPROM_STATUS_BUSY 0x01
  141. #define SPI_EEPROM_STATUS_WREN 0x02
  142. static int e1000_spi_eeprom_enable_wr(struct e1000_hw *hw, bool intr)
  143. {
  144. u8 op[] = { SPI_EEPROM_ENABLE_WR };
  145. e1000_standby_eeprom(hw);
  146. return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
  147. }
  148. /*
  149. * These have been tested to perform correctly, but they are not used by any
  150. * of the EEPROM commands at this time.
  151. */
  152. static __maybe_unused int e1000_spi_eeprom_disable_wr(struct e1000_hw *hw,
  153. bool intr)
  154. {
  155. u8 op[] = { SPI_EEPROM_DISABLE_WR };
  156. e1000_standby_eeprom(hw);
  157. return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
  158. }
  159. static __maybe_unused int e1000_spi_eeprom_write_status(struct e1000_hw *hw,
  160. u8 status, bool intr)
  161. {
  162. u8 op[] = { SPI_EEPROM_WRITE_STATUS, status };
  163. e1000_standby_eeprom(hw);
  164. return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
  165. }
  166. static int e1000_spi_eeprom_read_status(struct e1000_hw *hw, bool intr)
  167. {
  168. u8 op[] = { SPI_EEPROM_READ_STATUS, 0 };
  169. e1000_standby_eeprom(hw);
  170. if (e1000_spi_xfer(hw, 8*sizeof(op), op, op, intr))
  171. return -1;
  172. return op[1];
  173. }
  174. static int e1000_spi_eeprom_write_page(struct e1000_hw *hw,
  175. const void *data, u16 off, u16 len, bool intr)
  176. {
  177. u8 op[] = {
  178. SPI_EEPROM_WRITE_PAGE,
  179. (off >> (hw->eeprom.address_bits - 8)) & 0xff, off & 0xff
  180. };
  181. e1000_standby_eeprom(hw);
  182. if (e1000_spi_xfer(hw, 8 + hw->eeprom.address_bits, op, NULL, intr))
  183. return -1;
  184. if (e1000_spi_xfer(hw, len << 3, data, NULL, intr))
  185. return -1;
  186. return 0;
  187. }
  188. static int e1000_spi_eeprom_read_page(struct e1000_hw *hw,
  189. void *data, u16 off, u16 len, bool intr)
  190. {
  191. u8 op[] = {
  192. SPI_EEPROM_READ_PAGE,
  193. (off >> (hw->eeprom.address_bits - 8)) & 0xff, off & 0xff
  194. };
  195. e1000_standby_eeprom(hw);
  196. if (e1000_spi_xfer(hw, 8 + hw->eeprom.address_bits, op, NULL, intr))
  197. return -1;
  198. if (e1000_spi_xfer(hw, len << 3, NULL, data, intr))
  199. return -1;
  200. return 0;
  201. }
  202. static int e1000_spi_eeprom_poll_ready(struct e1000_hw *hw, bool intr)
  203. {
  204. int status;
  205. while ((status = e1000_spi_eeprom_read_status(hw, intr)) >= 0) {
  206. if (!(status & SPI_EEPROM_STATUS_BUSY))
  207. return 0;
  208. }
  209. return -1;
  210. }
  211. static int e1000_spi_eeprom_dump(struct e1000_hw *hw,
  212. void *data, u16 off, unsigned int len, bool intr)
  213. {
  214. /* Interruptibly wait for the EEPROM to be ready */
  215. if (e1000_spi_eeprom_poll_ready(hw, intr))
  216. return -1;
  217. /* Dump each page in sequence */
  218. while (len) {
  219. /* Calculate the data bytes on this page */
  220. u16 pg_off = off & (hw->eeprom.page_size - 1);
  221. u16 pg_len = hw->eeprom.page_size - pg_off;
  222. if (pg_len > len)
  223. pg_len = len;
  224. /* Now dump the page */
  225. if (e1000_spi_eeprom_read_page(hw, data, off, pg_len, intr))
  226. return -1;
  227. /* Otherwise go on to the next page */
  228. len -= pg_len;
  229. off += pg_len;
  230. data += pg_len;
  231. }
  232. /* We're done! */
  233. return 0;
  234. }
  235. static int e1000_spi_eeprom_program(struct e1000_hw *hw,
  236. const void *data, u16 off, u16 len, bool intr)
  237. {
  238. /* Program each page in sequence */
  239. while (len) {
  240. /* Calculate the data bytes on this page */
  241. u16 pg_off = off & (hw->eeprom.page_size - 1);
  242. u16 pg_len = hw->eeprom.page_size - pg_off;
  243. if (pg_len > len)
  244. pg_len = len;
  245. /* Interruptibly wait for the EEPROM to be ready */
  246. if (e1000_spi_eeprom_poll_ready(hw, intr))
  247. return -1;
  248. /* Enable write access */
  249. if (e1000_spi_eeprom_enable_wr(hw, intr))
  250. return -1;
  251. /* Now program the page */
  252. if (e1000_spi_eeprom_write_page(hw, data, off, pg_len, intr))
  253. return -1;
  254. /* Otherwise go on to the next page */
  255. len -= pg_len;
  256. off += pg_len;
  257. data += pg_len;
  258. }
  259. /* Wait for the last write to complete */
  260. if (e1000_spi_eeprom_poll_ready(hw, intr))
  261. return -1;
  262. /* We're done! */
  263. return 0;
  264. }
  265. static int do_e1000_spi_show(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
  266. int argc, char * const argv[])
  267. {
  268. unsigned int length = 0;
  269. u16 i, offset = 0;
  270. u8 *buffer;
  271. int err;
  272. if (argc > 2) {
  273. cmd_usage(cmdtp);
  274. return 1;
  275. }
  276. /* Parse the offset and length */
  277. if (argc >= 1)
  278. offset = simple_strtoul(argv[0], NULL, 0);
  279. if (argc == 2)
  280. length = simple_strtoul(argv[1], NULL, 0);
  281. else if (offset < (hw->eeprom.word_size << 1))
  282. length = (hw->eeprom.word_size << 1) - offset;
  283. /* Extra sanity checks */
  284. if (!length) {
  285. E1000_ERR(hw, "Requested zero-sized dump!\n");
  286. return 1;
  287. }
  288. if ((0x10000 < length) || (0x10000 - length < offset)) {
  289. E1000_ERR(hw, "Can't dump past 0xFFFF!\n");
  290. return 1;
  291. }
  292. /* Allocate a buffer to hold stuff */
  293. buffer = malloc(length);
  294. if (!buffer) {
  295. E1000_ERR(hw, "Out of Memory!\n");
  296. return 1;
  297. }
  298. /* Acquire the EEPROM and perform the dump */
  299. if (e1000_acquire_eeprom(hw)) {
  300. E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
  301. free(buffer);
  302. return 1;
  303. }
  304. err = e1000_spi_eeprom_dump(hw, buffer, offset, length, true);
  305. e1000_release_eeprom(hw);
  306. if (err) {
  307. E1000_ERR(hw, "Interrupted!\n");
  308. free(buffer);
  309. return 1;
  310. }
  311. /* Now hexdump the result */
  312. printf("%s: ===== Intel e1000 EEPROM (0x%04hX - 0x%04hX) =====",
  313. hw->name, offset, offset + length - 1);
  314. for (i = 0; i < length; i++) {
  315. if ((i & 0xF) == 0)
  316. printf("\n%s: %04hX: ", hw->name, offset + i);
  317. else if ((i & 0xF) == 0x8)
  318. printf(" ");
  319. printf(" %02hx", buffer[i]);
  320. }
  321. printf("\n");
  322. /* Success! */
  323. free(buffer);
  324. return 0;
  325. }
  326. static int do_e1000_spi_dump(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
  327. int argc, char * const argv[])
  328. {
  329. unsigned int length;
  330. u16 offset;
  331. void *dest;
  332. if (argc != 3) {
  333. cmd_usage(cmdtp);
  334. return 1;
  335. }
  336. /* Parse the arguments */
  337. dest = (void *)simple_strtoul(argv[0], NULL, 16);
  338. offset = simple_strtoul(argv[1], NULL, 0);
  339. length = simple_strtoul(argv[2], NULL, 0);
  340. /* Extra sanity checks */
  341. if (!length) {
  342. E1000_ERR(hw, "Requested zero-sized dump!\n");
  343. return 1;
  344. }
  345. if ((0x10000 < length) || (0x10000 - length < offset)) {
  346. E1000_ERR(hw, "Can't dump past 0xFFFF!\n");
  347. return 1;
  348. }
  349. /* Acquire the EEPROM */
  350. if (e1000_acquire_eeprom(hw)) {
  351. E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
  352. return 1;
  353. }
  354. /* Perform the programming operation */
  355. if (e1000_spi_eeprom_dump(hw, dest, offset, length, true) < 0) {
  356. E1000_ERR(hw, "Interrupted!\n");
  357. e1000_release_eeprom(hw);
  358. return 1;
  359. }
  360. e1000_release_eeprom(hw);
  361. printf("%s: ===== EEPROM DUMP COMPLETE =====\n", hw->name);
  362. return 0;
  363. }
  364. static int do_e1000_spi_program(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
  365. int argc, char * const argv[])
  366. {
  367. unsigned int length;
  368. const void *source;
  369. u16 offset;
  370. if (argc != 3) {
  371. cmd_usage(cmdtp);
  372. return 1;
  373. }
  374. /* Parse the arguments */
  375. source = (const void *)simple_strtoul(argv[0], NULL, 16);
  376. offset = simple_strtoul(argv[1], NULL, 0);
  377. length = simple_strtoul(argv[2], NULL, 0);
  378. /* Acquire the EEPROM */
  379. if (e1000_acquire_eeprom(hw)) {
  380. E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
  381. return 1;
  382. }
  383. /* Perform the programming operation */
  384. if (e1000_spi_eeprom_program(hw, source, offset, length, true) < 0) {
  385. E1000_ERR(hw, "Interrupted!\n");
  386. e1000_release_eeprom(hw);
  387. return 1;
  388. }
  389. e1000_release_eeprom(hw);
  390. printf("%s: ===== EEPROM PROGRAMMED =====\n", hw->name);
  391. return 0;
  392. }
  393. static int do_e1000_spi_checksum(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
  394. int argc, char * const argv[])
  395. {
  396. uint16_t i, length, checksum = 0, checksum_reg;
  397. uint16_t *buffer;
  398. bool upd;
  399. if (argc == 0)
  400. upd = 0;
  401. else if ((argc == 1) && !strcmp(argv[0], "update"))
  402. upd = 1;
  403. else {
  404. cmd_usage(cmdtp);
  405. return 1;
  406. }
  407. /* Allocate a temporary buffer */
  408. length = sizeof(uint16_t) * (EEPROM_CHECKSUM_REG + 1);
  409. buffer = malloc(length);
  410. if (!buffer) {
  411. E1000_ERR(hw, "Unable to allocate EEPROM buffer!\n");
  412. return 1;
  413. }
  414. /* Acquire the EEPROM */
  415. if (e1000_acquire_eeprom(hw)) {
  416. E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n");
  417. return 1;
  418. }
  419. /* Read the EEPROM */
  420. if (e1000_spi_eeprom_dump(hw, buffer, 0, length, true) < 0) {
  421. E1000_ERR(hw, "Interrupted!\n");
  422. e1000_release_eeprom(hw);
  423. return 1;
  424. }
  425. /* Compute the checksum and read the expected value */
  426. for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
  427. checksum += le16_to_cpu(buffer[i]);
  428. checksum = ((uint16_t)EEPROM_SUM) - checksum;
  429. checksum_reg = le16_to_cpu(buffer[i]);
  430. /* Verify it! */
  431. if (checksum_reg == checksum) {
  432. printf("%s: INFO: EEPROM checksum is correct! (0x%04hx)\n",
  433. hw->name, checksum);
  434. e1000_release_eeprom(hw);
  435. return 0;
  436. }
  437. /* Hrm, verification failed, print an error */
  438. E1000_ERR(hw, "EEPROM checksum is incorrect!\n");
  439. E1000_ERR(hw, " ...register was 0x%04hx, calculated 0x%04hx\n",
  440. checksum_reg, checksum);
  441. /* If they didn't ask us to update it, just return an error */
  442. if (!upd) {
  443. e1000_release_eeprom(hw);
  444. return 1;
  445. }
  446. /* Ok, correct it! */
  447. printf("%s: Reprogramming the EEPROM checksum...\n", hw->name);
  448. buffer[i] = cpu_to_le16(checksum);
  449. if (e1000_spi_eeprom_program(hw, &buffer[i], i * sizeof(uint16_t),
  450. sizeof(uint16_t), true)) {
  451. E1000_ERR(hw, "Interrupted!\n");
  452. e1000_release_eeprom(hw);
  453. return 1;
  454. }
  455. e1000_release_eeprom(hw);
  456. return 0;
  457. }
  458. int do_e1000_spi(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
  459. int argc, char * const argv[])
  460. {
  461. if (argc < 1) {
  462. cmd_usage(cmdtp);
  463. return 1;
  464. }
  465. /* Make sure it has an SPI chip */
  466. if (hw->eeprom.type != e1000_eeprom_spi) {
  467. E1000_ERR(hw, "No attached SPI EEPROM found (%d)!\n",
  468. hw->eeprom.type);
  469. return 1;
  470. }
  471. /* Check the eeprom sub-sub-command arguments */
  472. if (!strcmp(argv[0], "show"))
  473. return do_e1000_spi_show(cmdtp, hw, argc - 1, argv + 1);
  474. if (!strcmp(argv[0], "dump"))
  475. return do_e1000_spi_dump(cmdtp, hw, argc - 1, argv + 1);
  476. if (!strcmp(argv[0], "program"))
  477. return do_e1000_spi_program(cmdtp, hw, argc - 1, argv + 1);
  478. if (!strcmp(argv[0], "checksum"))
  479. return do_e1000_spi_checksum(cmdtp, hw, argc - 1, argv + 1);
  480. cmd_usage(cmdtp);
  481. return 1;
  482. }
  483. #endif /* not CONFIG_CMD_E1000 */