e1000_spi.c 14 KB

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