e1000_spi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. #include "e1000.h"
  2. #include <linux/compiler.h>
  3. /*-----------------------------------------------------------------------
  4. * SPI transfer
  5. *
  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.
  8. *
  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).
  14. *
  15. * This may be interrupted with Ctrl-C if "intr" is true, otherwise it will
  16. * never return an error.
  17. */
  18. static int e1000_spi_xfer(struct e1000_hw *hw, unsigned int bitlen,
  19. const void *dout_mem, void *din_mem, bool intr)
  20. {
  21. const uint8_t *dout = dout_mem;
  22. uint8_t *din = din_mem;
  23. uint8_t mask = 0;
  24. uint32_t eecd;
  25. unsigned long i;
  26. /* Pre-read the control register */
  27. eecd = E1000_READ_REG(hw, EECD);
  28. /* Iterate over each bit */
  29. for (i = 0, mask = 0x80; i < bitlen; i++, mask = (mask >> 1)?:0x80) {
  30. /* Check for interrupt */
  31. if (intr && ctrlc())
  32. return -1;
  33. /* Determine the output bit */
  34. if (dout && dout[i >> 3] & mask)
  35. eecd |= E1000_EECD_DI;
  36. else
  37. eecd &= ~E1000_EECD_DI;
  38. /* Write the output bit and wait 50us */
  39. E1000_WRITE_REG(hw, EECD, eecd);
  40. E1000_WRITE_FLUSH(hw);
  41. udelay(50);
  42. /* Poke the clock (waits 50us) */
  43. e1000_raise_ee_clk(hw, &eecd);
  44. /* Now read the input bit */
  45. eecd = E1000_READ_REG(hw, EECD);
  46. if (din) {
  47. if (eecd & E1000_EECD_DO)
  48. din[i >> 3] |= mask;
  49. else
  50. din[i >> 3] &= ~mask;
  51. }
  52. /* Poke the clock again (waits 50us) */
  53. e1000_lower_ee_clk(hw, &eecd);
  54. }
  55. /* Now clear any remaining bits of the input */
  56. if (din && (i & 7))
  57. din[i >> 3] &= ~((mask << 1) - 1);
  58. return 0;
  59. }
  60. #ifdef CONFIG_E1000_SPI_GENERIC
  61. static inline struct e1000_hw *e1000_hw_from_spi(struct spi_slave *spi)
  62. {
  63. return container_of(spi, struct e1000_hw, spi);
  64. }
  65. /* Not sure why all of these are necessary */
  66. void spi_init_r(void) { /* Nothing to do */ }
  67. void spi_init_f(void) { /* Nothing to do */ }
  68. void spi_init(void) { /* Nothing to do */ }
  69. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  70. unsigned int max_hz, unsigned int mode)
  71. {
  72. /* Find the right PCI device */
  73. struct e1000_hw *hw = e1000_find_card(bus);
  74. if (!hw) {
  75. printf("ERROR: No such e1000 device: e1000#%u\n", bus);
  76. return NULL;
  77. }
  78. /* Make sure it has an SPI chip */
  79. if (hw->eeprom.type != e1000_eeprom_spi) {
  80. E1000_ERR(hw->nic, "No attached SPI EEPROM found!\n");
  81. return NULL;
  82. }
  83. /* Argument sanity checks */
  84. if (cs != 0) {
  85. E1000_ERR(hw->nic, "No such SPI chip: %u\n", cs);
  86. return NULL;
  87. }
  88. if (mode != SPI_MODE_0) {
  89. E1000_ERR(hw->nic, "Only SPI MODE-0 is supported!\n");
  90. return NULL;
  91. }
  92. /* TODO: Use max_hz somehow */
  93. E1000_DBG(hw->nic, "EEPROM SPI access requested\n");
  94. return &hw->spi;
  95. }
  96. void spi_free_slave(struct spi_slave *spi)
  97. {
  98. __maybe_unused struct e1000_hw *hw = e1000_hw_from_spi(spi);
  99. E1000_DBG(hw->nic, "EEPROM SPI access released\n");
  100. }
  101. int spi_claim_bus(struct spi_slave *spi)
  102. {
  103. struct e1000_hw *hw = e1000_hw_from_spi(spi);
  104. if (e1000_acquire_eeprom(hw)) {
  105. E1000_ERR(hw->nic, "EEPROM SPI cannot be acquired!\n");
  106. return -1;
  107. }
  108. return 0;
  109. }
  110. void spi_release_bus(struct spi_slave *spi)
  111. {
  112. struct e1000_hw *hw = e1000_hw_from_spi(spi);
  113. e1000_release_eeprom(hw);
  114. }
  115. /* Skinny wrapper around e1000_spi_xfer */
  116. int spi_xfer(struct spi_slave *spi, unsigned int bitlen,
  117. const void *dout_mem, void *din_mem, unsigned long flags)
  118. {
  119. struct e1000_hw *hw = e1000_hw_from_spi(spi);
  120. int ret;
  121. if (flags & SPI_XFER_BEGIN)
  122. e1000_standby_eeprom(hw);
  123. ret = e1000_spi_xfer(hw, bitlen, dout_mem, din_mem, true);
  124. if (flags & SPI_XFER_END)
  125. e1000_standby_eeprom(hw);
  126. return ret;
  127. }
  128. #endif /* not CONFIG_E1000_SPI_GENERIC */
  129. #ifdef CONFIG_CMD_E1000
  130. /* The EEPROM opcodes */
  131. #define SPI_EEPROM_ENABLE_WR 0x06
  132. #define SPI_EEPROM_DISABLE_WR 0x04
  133. #define SPI_EEPROM_WRITE_STATUS 0x01
  134. #define SPI_EEPROM_READ_STATUS 0x05
  135. #define SPI_EEPROM_WRITE_PAGE 0x02
  136. #define SPI_EEPROM_READ_PAGE 0x03
  137. /* The EEPROM status bits */
  138. #define SPI_EEPROM_STATUS_BUSY 0x01
  139. #define SPI_EEPROM_STATUS_WREN 0x02
  140. static int e1000_spi_eeprom_enable_wr(struct e1000_hw *hw, bool intr)
  141. {
  142. u8 op[] = { SPI_EEPROM_ENABLE_WR };
  143. e1000_standby_eeprom(hw);
  144. return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
  145. }
  146. /*
  147. * These have been tested to perform correctly, but they are not used by any
  148. * of the EEPROM commands at this time.
  149. */
  150. #if 0
  151. static int e1000_spi_eeprom_disable_wr(struct e1000_hw *hw, bool intr)
  152. {
  153. u8 op[] = { SPI_EEPROM_DISABLE_WR };
  154. e1000_standby_eeprom(hw);
  155. return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
  156. }
  157. static int e1000_spi_eeprom_write_status(struct e1000_hw *hw,
  158. u8 status, bool intr)
  159. {
  160. u8 op[] = { SPI_EEPROM_WRITE_STATUS, status };
  161. e1000_standby_eeprom(hw);
  162. return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
  163. }
  164. #endif
  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(cmd_tbl_t *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->nic, "Requested zero-sized dump!\n");
  285. return 1;
  286. }
  287. if ((0x10000 < length) || (0x10000 - length < offset)) {
  288. E1000_ERR(hw->nic, "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->nic, "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->nic, "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->nic, "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->nic->name, offset, offset + length - 1);
  313. for (i = 0; i < length; i++) {
  314. if ((i & 0xF) == 0)
  315. printf("\n%s: %04hX: ", hw->nic->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(cmd_tbl_t *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 *)simple_strtoul(argv[0], NULL, 16);
  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->nic, "Requested zero-sized dump!\n");
  342. return 1;
  343. }
  344. if ((0x10000 < length) || (0x10000 - length < offset)) {
  345. E1000_ERR(hw->nic, "Can't dump past 0xFFFF!\n");
  346. return 1;
  347. }
  348. /* Acquire the EEPROM */
  349. if (e1000_acquire_eeprom(hw)) {
  350. E1000_ERR(hw->nic, "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->nic, "Interrupted!\n");
  356. e1000_release_eeprom(hw);
  357. return 1;
  358. }
  359. e1000_release_eeprom(hw);
  360. printf("%s: ===== EEPROM DUMP COMPLETE =====\n", hw->nic->name);
  361. return 0;
  362. }
  363. static int do_e1000_spi_program(cmd_tbl_t *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 *)simple_strtoul(argv[0], NULL, 16);
  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->nic, "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->nic, "Interrupted!\n");
  385. e1000_release_eeprom(hw);
  386. return 1;
  387. }
  388. e1000_release_eeprom(hw);
  389. printf("%s: ===== EEPROM PROGRAMMED =====\n", hw->nic->name);
  390. return 0;
  391. }
  392. static int do_e1000_spi_checksum(cmd_tbl_t *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->nic, "Unable to allocate EEPROM buffer!\n");
  411. return 1;
  412. }
  413. /* Acquire the EEPROM */
  414. if (e1000_acquire_eeprom(hw)) {
  415. E1000_ERR(hw->nic, "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->nic, "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->nic->name, checksum);
  433. e1000_release_eeprom(hw);
  434. return 0;
  435. }
  436. /* Hrm, verification failed, print an error */
  437. E1000_ERR(hw->nic, "EEPROM checksum is incorrect!\n");
  438. E1000_ERR(hw->nic, " ...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->nic->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->nic, "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(cmd_tbl_t *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->nic, "No attached SPI EEPROM found!\n");
  467. return 1;
  468. }
  469. /* Check the eeprom sub-sub-command arguments */
  470. if (!strcmp(argv[0], "show"))
  471. return do_e1000_spi_show(cmdtp, hw, argc - 1, argv + 1);
  472. if (!strcmp(argv[0], "dump"))
  473. return do_e1000_spi_dump(cmdtp, hw, argc - 1, argv + 1);
  474. if (!strcmp(argv[0], "program"))
  475. return do_e1000_spi_program(cmdtp, hw, argc - 1, argv + 1);
  476. if (!strcmp(argv[0], "checksum"))
  477. return do_e1000_spi_checksum(cmdtp, hw, argc - 1, argv + 1);
  478. cmd_usage(cmdtp);
  479. return 1;
  480. }
  481. #endif /* not CONFIG_CMD_E1000 */