e1000_spi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. #include <common.h>
  2. #include "e1000.h"
  3. #include <linux/compiler.h>
  4. /*-----------------------------------------------------------------------
  5. * SPI transfer
  6. *
  7. * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
  8. * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
  9. *
  10. * The source of the outgoing bits is the "dout" parameter and the
  11. * destination of the input bits is the "din" parameter. Note that "dout"
  12. * and "din" can point to the same memory location, in which case the
  13. * input data overwrites the output data (since both are buffered by
  14. * temporary variables, this is OK).
  15. *
  16. * This may be interrupted with Ctrl-C if "intr" is true, otherwise it will
  17. * never return an error.
  18. */
  19. static int e1000_spi_xfer(struct e1000_hw *hw, unsigned int bitlen,
  20. const void *dout_mem, void *din_mem, bool intr)
  21. {
  22. const uint8_t *dout = dout_mem;
  23. uint8_t *din = din_mem;
  24. uint8_t mask = 0;
  25. uint32_t eecd;
  26. unsigned long i;
  27. /* Pre-read the control register */
  28. eecd = E1000_READ_REG(hw, EECD);
  29. /* Iterate over each bit */
  30. for (i = 0, mask = 0x80; i < bitlen; i++, mask = (mask >> 1)?:0x80) {
  31. /* Check for interrupt */
  32. if (intr && ctrlc())
  33. return -1;
  34. /* Determine the output bit */
  35. if (dout && dout[i >> 3] & mask)
  36. eecd |= E1000_EECD_DI;
  37. else
  38. eecd &= ~E1000_EECD_DI;
  39. /* Write the output bit and wait 50us */
  40. E1000_WRITE_REG(hw, EECD, eecd);
  41. E1000_WRITE_FLUSH(hw);
  42. udelay(50);
  43. /* Poke the clock (waits 50us) */
  44. e1000_raise_ee_clk(hw, &eecd);
  45. /* Now read the input bit */
  46. eecd = E1000_READ_REG(hw, EECD);
  47. if (din) {
  48. if (eecd & E1000_EECD_DO)
  49. din[i >> 3] |= mask;
  50. else
  51. din[i >> 3] &= ~mask;
  52. }
  53. /* Poke the clock again (waits 50us) */
  54. e1000_lower_ee_clk(hw, &eecd);
  55. }
  56. /* Now clear any remaining bits of the input */
  57. if (din && (i & 7))
  58. din[i >> 3] &= ~((mask << 1) - 1);
  59. return 0;
  60. }
  61. #ifdef CONFIG_E1000_SPI_GENERIC
  62. static inline struct e1000_hw *e1000_hw_from_spi(struct spi_slave *spi)
  63. {
  64. return container_of(spi, struct e1000_hw, spi);
  65. }
  66. /* Not sure why all of these are necessary */
  67. void spi_init_r(void) { /* Nothing to do */ }
  68. void spi_init_f(void) { /* Nothing to do */ }
  69. void spi_init(void) { /* Nothing to do */ }
  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->nic, "No attached SPI EEPROM found!\n");
  82. return NULL;
  83. }
  84. /* Argument sanity checks */
  85. if (cs != 0) {
  86. E1000_ERR(hw->nic, "No such SPI chip: %u\n", cs);
  87. return NULL;
  88. }
  89. if (mode != SPI_MODE_0) {
  90. E1000_ERR(hw->nic, "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->nic, "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. #if 0
  152. static int e1000_spi_eeprom_disable_wr(struct e1000_hw *hw, 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 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. #endif
  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->nic, "Requested zero-sized dump!\n");
  286. return 1;
  287. }
  288. if ((0x10000 < length) || (0x10000 - length < offset)) {
  289. E1000_ERR(hw->nic, "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->nic, "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->nic, "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->nic, "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->nic->name, offset, offset + length - 1);
  314. for (i = 0; i < length; i++) {
  315. if ((i & 0xF) == 0)
  316. printf("\n%s: %04hX: ", hw->nic->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->nic, "Requested zero-sized dump!\n");
  343. return 1;
  344. }
  345. if ((0x10000 < length) || (0x10000 - length < offset)) {
  346. E1000_ERR(hw->nic, "Can't dump past 0xFFFF!\n");
  347. return 1;
  348. }
  349. /* Acquire the EEPROM */
  350. if (e1000_acquire_eeprom(hw)) {
  351. E1000_ERR(hw->nic, "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->nic, "Interrupted!\n");
  357. e1000_release_eeprom(hw);
  358. return 1;
  359. }
  360. e1000_release_eeprom(hw);
  361. printf("%s: ===== EEPROM DUMP COMPLETE =====\n", hw->nic->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->nic, "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->nic, "Interrupted!\n");
  386. e1000_release_eeprom(hw);
  387. return 1;
  388. }
  389. e1000_release_eeprom(hw);
  390. printf("%s: ===== EEPROM PROGRAMMED =====\n", hw->nic->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->nic, "Unable to allocate EEPROM buffer!\n");
  412. return 1;
  413. }
  414. /* Acquire the EEPROM */
  415. if (e1000_acquire_eeprom(hw)) {
  416. E1000_ERR(hw->nic, "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->nic, "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->nic->name, checksum);
  434. e1000_release_eeprom(hw);
  435. return 0;
  436. }
  437. /* Hrm, verification failed, print an error */
  438. E1000_ERR(hw->nic, "EEPROM checksum is incorrect!\n");
  439. E1000_ERR(hw->nic, " ...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->nic->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->nic, "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->nic, "No attached SPI EEPROM found!\n");
  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 */