e1000_spi.c 14 KB

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