nvm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 1999 - 2018 Intel Corporation. */
  3. #include "e1000.h"
  4. /**
  5. * e1000_raise_eec_clk - Raise EEPROM clock
  6. * @hw: pointer to the HW structure
  7. * @eecd: pointer to the EEPROM
  8. *
  9. * Enable/Raise the EEPROM clock bit.
  10. **/
  11. static void e1000_raise_eec_clk(struct e1000_hw *hw, u32 *eecd)
  12. {
  13. *eecd = *eecd | E1000_EECD_SK;
  14. ew32(EECD, *eecd);
  15. e1e_flush();
  16. udelay(hw->nvm.delay_usec);
  17. }
  18. /**
  19. * e1000_lower_eec_clk - Lower EEPROM clock
  20. * @hw: pointer to the HW structure
  21. * @eecd: pointer to the EEPROM
  22. *
  23. * Clear/Lower the EEPROM clock bit.
  24. **/
  25. static void e1000_lower_eec_clk(struct e1000_hw *hw, u32 *eecd)
  26. {
  27. *eecd = *eecd & ~E1000_EECD_SK;
  28. ew32(EECD, *eecd);
  29. e1e_flush();
  30. udelay(hw->nvm.delay_usec);
  31. }
  32. /**
  33. * e1000_shift_out_eec_bits - Shift data bits our to the EEPROM
  34. * @hw: pointer to the HW structure
  35. * @data: data to send to the EEPROM
  36. * @count: number of bits to shift out
  37. *
  38. * We need to shift 'count' bits out to the EEPROM. So, the value in the
  39. * "data" parameter will be shifted out to the EEPROM one bit at a time.
  40. * In order to do this, "data" must be broken down into bits.
  41. **/
  42. static void e1000_shift_out_eec_bits(struct e1000_hw *hw, u16 data, u16 count)
  43. {
  44. struct e1000_nvm_info *nvm = &hw->nvm;
  45. u32 eecd = er32(EECD);
  46. u32 mask;
  47. mask = BIT(count - 1);
  48. if (nvm->type == e1000_nvm_eeprom_spi)
  49. eecd |= E1000_EECD_DO;
  50. do {
  51. eecd &= ~E1000_EECD_DI;
  52. if (data & mask)
  53. eecd |= E1000_EECD_DI;
  54. ew32(EECD, eecd);
  55. e1e_flush();
  56. udelay(nvm->delay_usec);
  57. e1000_raise_eec_clk(hw, &eecd);
  58. e1000_lower_eec_clk(hw, &eecd);
  59. mask >>= 1;
  60. } while (mask);
  61. eecd &= ~E1000_EECD_DI;
  62. ew32(EECD, eecd);
  63. }
  64. /**
  65. * e1000_shift_in_eec_bits - Shift data bits in from the EEPROM
  66. * @hw: pointer to the HW structure
  67. * @count: number of bits to shift in
  68. *
  69. * In order to read a register from the EEPROM, we need to shift 'count' bits
  70. * in from the EEPROM. Bits are "shifted in" by raising the clock input to
  71. * the EEPROM (setting the SK bit), and then reading the value of the data out
  72. * "DO" bit. During this "shifting in" process the data in "DI" bit should
  73. * always be clear.
  74. **/
  75. static u16 e1000_shift_in_eec_bits(struct e1000_hw *hw, u16 count)
  76. {
  77. u32 eecd;
  78. u32 i;
  79. u16 data;
  80. eecd = er32(EECD);
  81. eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
  82. data = 0;
  83. for (i = 0; i < count; i++) {
  84. data <<= 1;
  85. e1000_raise_eec_clk(hw, &eecd);
  86. eecd = er32(EECD);
  87. eecd &= ~E1000_EECD_DI;
  88. if (eecd & E1000_EECD_DO)
  89. data |= 1;
  90. e1000_lower_eec_clk(hw, &eecd);
  91. }
  92. return data;
  93. }
  94. /**
  95. * e1000e_poll_eerd_eewr_done - Poll for EEPROM read/write completion
  96. * @hw: pointer to the HW structure
  97. * @ee_reg: EEPROM flag for polling
  98. *
  99. * Polls the EEPROM status bit for either read or write completion based
  100. * upon the value of 'ee_reg'.
  101. **/
  102. s32 e1000e_poll_eerd_eewr_done(struct e1000_hw *hw, int ee_reg)
  103. {
  104. u32 attempts = 100000;
  105. u32 i, reg = 0;
  106. for (i = 0; i < attempts; i++) {
  107. if (ee_reg == E1000_NVM_POLL_READ)
  108. reg = er32(EERD);
  109. else
  110. reg = er32(EEWR);
  111. if (reg & E1000_NVM_RW_REG_DONE)
  112. return 0;
  113. udelay(5);
  114. }
  115. return -E1000_ERR_NVM;
  116. }
  117. /**
  118. * e1000e_acquire_nvm - Generic request for access to EEPROM
  119. * @hw: pointer to the HW structure
  120. *
  121. * Set the EEPROM access request bit and wait for EEPROM access grant bit.
  122. * Return successful if access grant bit set, else clear the request for
  123. * EEPROM access and return -E1000_ERR_NVM (-1).
  124. **/
  125. s32 e1000e_acquire_nvm(struct e1000_hw *hw)
  126. {
  127. u32 eecd = er32(EECD);
  128. s32 timeout = E1000_NVM_GRANT_ATTEMPTS;
  129. ew32(EECD, eecd | E1000_EECD_REQ);
  130. eecd = er32(EECD);
  131. while (timeout) {
  132. if (eecd & E1000_EECD_GNT)
  133. break;
  134. udelay(5);
  135. eecd = er32(EECD);
  136. timeout--;
  137. }
  138. if (!timeout) {
  139. eecd &= ~E1000_EECD_REQ;
  140. ew32(EECD, eecd);
  141. e_dbg("Could not acquire NVM grant\n");
  142. return -E1000_ERR_NVM;
  143. }
  144. return 0;
  145. }
  146. /**
  147. * e1000_standby_nvm - Return EEPROM to standby state
  148. * @hw: pointer to the HW structure
  149. *
  150. * Return the EEPROM to a standby state.
  151. **/
  152. static void e1000_standby_nvm(struct e1000_hw *hw)
  153. {
  154. struct e1000_nvm_info *nvm = &hw->nvm;
  155. u32 eecd = er32(EECD);
  156. if (nvm->type == e1000_nvm_eeprom_spi) {
  157. /* Toggle CS to flush commands */
  158. eecd |= E1000_EECD_CS;
  159. ew32(EECD, eecd);
  160. e1e_flush();
  161. udelay(nvm->delay_usec);
  162. eecd &= ~E1000_EECD_CS;
  163. ew32(EECD, eecd);
  164. e1e_flush();
  165. udelay(nvm->delay_usec);
  166. }
  167. }
  168. /**
  169. * e1000_stop_nvm - Terminate EEPROM command
  170. * @hw: pointer to the HW structure
  171. *
  172. * Terminates the current command by inverting the EEPROM's chip select pin.
  173. **/
  174. static void e1000_stop_nvm(struct e1000_hw *hw)
  175. {
  176. u32 eecd;
  177. eecd = er32(EECD);
  178. if (hw->nvm.type == e1000_nvm_eeprom_spi) {
  179. /* Pull CS high */
  180. eecd |= E1000_EECD_CS;
  181. e1000_lower_eec_clk(hw, &eecd);
  182. }
  183. }
  184. /**
  185. * e1000e_release_nvm - Release exclusive access to EEPROM
  186. * @hw: pointer to the HW structure
  187. *
  188. * Stop any current commands to the EEPROM and clear the EEPROM request bit.
  189. **/
  190. void e1000e_release_nvm(struct e1000_hw *hw)
  191. {
  192. u32 eecd;
  193. e1000_stop_nvm(hw);
  194. eecd = er32(EECD);
  195. eecd &= ~E1000_EECD_REQ;
  196. ew32(EECD, eecd);
  197. }
  198. /**
  199. * e1000_ready_nvm_eeprom - Prepares EEPROM for read/write
  200. * @hw: pointer to the HW structure
  201. *
  202. * Setups the EEPROM for reading and writing.
  203. **/
  204. static s32 e1000_ready_nvm_eeprom(struct e1000_hw *hw)
  205. {
  206. struct e1000_nvm_info *nvm = &hw->nvm;
  207. u32 eecd = er32(EECD);
  208. u8 spi_stat_reg;
  209. if (nvm->type == e1000_nvm_eeprom_spi) {
  210. u16 timeout = NVM_MAX_RETRY_SPI;
  211. /* Clear SK and CS */
  212. eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
  213. ew32(EECD, eecd);
  214. e1e_flush();
  215. udelay(1);
  216. /* Read "Status Register" repeatedly until the LSB is cleared.
  217. * The EEPROM will signal that the command has been completed
  218. * by clearing bit 0 of the internal status register. If it's
  219. * not cleared within 'timeout', then error out.
  220. */
  221. while (timeout) {
  222. e1000_shift_out_eec_bits(hw, NVM_RDSR_OPCODE_SPI,
  223. hw->nvm.opcode_bits);
  224. spi_stat_reg = (u8)e1000_shift_in_eec_bits(hw, 8);
  225. if (!(spi_stat_reg & NVM_STATUS_RDY_SPI))
  226. break;
  227. udelay(5);
  228. e1000_standby_nvm(hw);
  229. timeout--;
  230. }
  231. if (!timeout) {
  232. e_dbg("SPI NVM Status error\n");
  233. return -E1000_ERR_NVM;
  234. }
  235. }
  236. return 0;
  237. }
  238. /**
  239. * e1000e_read_nvm_eerd - Reads EEPROM using EERD register
  240. * @hw: pointer to the HW structure
  241. * @offset: offset of word in the EEPROM to read
  242. * @words: number of words to read
  243. * @data: word read from the EEPROM
  244. *
  245. * Reads a 16 bit word from the EEPROM using the EERD register.
  246. **/
  247. s32 e1000e_read_nvm_eerd(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
  248. {
  249. struct e1000_nvm_info *nvm = &hw->nvm;
  250. u32 i, eerd = 0;
  251. s32 ret_val = 0;
  252. /* A check for invalid values: offset too large, too many words,
  253. * too many words for the offset, and not enough words.
  254. */
  255. if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
  256. (words == 0)) {
  257. e_dbg("nvm parameter(s) out of bounds\n");
  258. return -E1000_ERR_NVM;
  259. }
  260. for (i = 0; i < words; i++) {
  261. eerd = ((offset + i) << E1000_NVM_RW_ADDR_SHIFT) +
  262. E1000_NVM_RW_REG_START;
  263. ew32(EERD, eerd);
  264. ret_val = e1000e_poll_eerd_eewr_done(hw, E1000_NVM_POLL_READ);
  265. if (ret_val) {
  266. e_dbg("NVM read error: %d\n", ret_val);
  267. break;
  268. }
  269. data[i] = (er32(EERD) >> E1000_NVM_RW_REG_DATA);
  270. }
  271. return ret_val;
  272. }
  273. /**
  274. * e1000e_write_nvm_spi - Write to EEPROM using SPI
  275. * @hw: pointer to the HW structure
  276. * @offset: offset within the EEPROM to be written to
  277. * @words: number of words to write
  278. * @data: 16 bit word(s) to be written to the EEPROM
  279. *
  280. * Writes data to EEPROM at offset using SPI interface.
  281. *
  282. * If e1000e_update_nvm_checksum is not called after this function , the
  283. * EEPROM will most likely contain an invalid checksum.
  284. **/
  285. s32 e1000e_write_nvm_spi(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
  286. {
  287. struct e1000_nvm_info *nvm = &hw->nvm;
  288. s32 ret_val = -E1000_ERR_NVM;
  289. u16 widx = 0;
  290. /* A check for invalid values: offset too large, too many words,
  291. * and not enough words.
  292. */
  293. if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
  294. (words == 0)) {
  295. e_dbg("nvm parameter(s) out of bounds\n");
  296. return -E1000_ERR_NVM;
  297. }
  298. while (widx < words) {
  299. u8 write_opcode = NVM_WRITE_OPCODE_SPI;
  300. ret_val = nvm->ops.acquire(hw);
  301. if (ret_val)
  302. return ret_val;
  303. ret_val = e1000_ready_nvm_eeprom(hw);
  304. if (ret_val) {
  305. nvm->ops.release(hw);
  306. return ret_val;
  307. }
  308. e1000_standby_nvm(hw);
  309. /* Send the WRITE ENABLE command (8 bit opcode) */
  310. e1000_shift_out_eec_bits(hw, NVM_WREN_OPCODE_SPI,
  311. nvm->opcode_bits);
  312. e1000_standby_nvm(hw);
  313. /* Some SPI eeproms use the 8th address bit embedded in the
  314. * opcode
  315. */
  316. if ((nvm->address_bits == 8) && (offset >= 128))
  317. write_opcode |= NVM_A8_OPCODE_SPI;
  318. /* Send the Write command (8-bit opcode + addr) */
  319. e1000_shift_out_eec_bits(hw, write_opcode, nvm->opcode_bits);
  320. e1000_shift_out_eec_bits(hw, (u16)((offset + widx) * 2),
  321. nvm->address_bits);
  322. /* Loop to allow for up to whole page write of eeprom */
  323. while (widx < words) {
  324. u16 word_out = data[widx];
  325. word_out = (word_out >> 8) | (word_out << 8);
  326. e1000_shift_out_eec_bits(hw, word_out, 16);
  327. widx++;
  328. if ((((offset + widx) * 2) % nvm->page_size) == 0) {
  329. e1000_standby_nvm(hw);
  330. break;
  331. }
  332. }
  333. usleep_range(10000, 11000);
  334. nvm->ops.release(hw);
  335. }
  336. return ret_val;
  337. }
  338. /**
  339. * e1000_read_pba_string_generic - Read device part number
  340. * @hw: pointer to the HW structure
  341. * @pba_num: pointer to device part number
  342. * @pba_num_size: size of part number buffer
  343. *
  344. * Reads the product board assembly (PBA) number from the EEPROM and stores
  345. * the value in pba_num.
  346. **/
  347. s32 e1000_read_pba_string_generic(struct e1000_hw *hw, u8 *pba_num,
  348. u32 pba_num_size)
  349. {
  350. s32 ret_val;
  351. u16 nvm_data;
  352. u16 pba_ptr;
  353. u16 offset;
  354. u16 length;
  355. if (pba_num == NULL) {
  356. e_dbg("PBA string buffer was null\n");
  357. return -E1000_ERR_INVALID_ARGUMENT;
  358. }
  359. ret_val = e1000_read_nvm(hw, NVM_PBA_OFFSET_0, 1, &nvm_data);
  360. if (ret_val) {
  361. e_dbg("NVM Read Error\n");
  362. return ret_val;
  363. }
  364. ret_val = e1000_read_nvm(hw, NVM_PBA_OFFSET_1, 1, &pba_ptr);
  365. if (ret_val) {
  366. e_dbg("NVM Read Error\n");
  367. return ret_val;
  368. }
  369. /* if nvm_data is not ptr guard the PBA must be in legacy format which
  370. * means pba_ptr is actually our second data word for the PBA number
  371. * and we can decode it into an ascii string
  372. */
  373. if (nvm_data != NVM_PBA_PTR_GUARD) {
  374. e_dbg("NVM PBA number is not stored as string\n");
  375. /* make sure callers buffer is big enough to store the PBA */
  376. if (pba_num_size < E1000_PBANUM_LENGTH) {
  377. e_dbg("PBA string buffer too small\n");
  378. return E1000_ERR_NO_SPACE;
  379. }
  380. /* extract hex string from data and pba_ptr */
  381. pba_num[0] = (nvm_data >> 12) & 0xF;
  382. pba_num[1] = (nvm_data >> 8) & 0xF;
  383. pba_num[2] = (nvm_data >> 4) & 0xF;
  384. pba_num[3] = nvm_data & 0xF;
  385. pba_num[4] = (pba_ptr >> 12) & 0xF;
  386. pba_num[5] = (pba_ptr >> 8) & 0xF;
  387. pba_num[6] = '-';
  388. pba_num[7] = 0;
  389. pba_num[8] = (pba_ptr >> 4) & 0xF;
  390. pba_num[9] = pba_ptr & 0xF;
  391. /* put a null character on the end of our string */
  392. pba_num[10] = '\0';
  393. /* switch all the data but the '-' to hex char */
  394. for (offset = 0; offset < 10; offset++) {
  395. if (pba_num[offset] < 0xA)
  396. pba_num[offset] += '0';
  397. else if (pba_num[offset] < 0x10)
  398. pba_num[offset] += 'A' - 0xA;
  399. }
  400. return 0;
  401. }
  402. ret_val = e1000_read_nvm(hw, pba_ptr, 1, &length);
  403. if (ret_val) {
  404. e_dbg("NVM Read Error\n");
  405. return ret_val;
  406. }
  407. if (length == 0xFFFF || length == 0) {
  408. e_dbg("NVM PBA number section invalid length\n");
  409. return -E1000_ERR_NVM_PBA_SECTION;
  410. }
  411. /* check if pba_num buffer is big enough */
  412. if (pba_num_size < (((u32)length * 2) - 1)) {
  413. e_dbg("PBA string buffer too small\n");
  414. return -E1000_ERR_NO_SPACE;
  415. }
  416. /* trim pba length from start of string */
  417. pba_ptr++;
  418. length--;
  419. for (offset = 0; offset < length; offset++) {
  420. ret_val = e1000_read_nvm(hw, pba_ptr + offset, 1, &nvm_data);
  421. if (ret_val) {
  422. e_dbg("NVM Read Error\n");
  423. return ret_val;
  424. }
  425. pba_num[offset * 2] = (u8)(nvm_data >> 8);
  426. pba_num[(offset * 2) + 1] = (u8)(nvm_data & 0xFF);
  427. }
  428. pba_num[offset * 2] = '\0';
  429. return 0;
  430. }
  431. /**
  432. * e1000_read_mac_addr_generic - Read device MAC address
  433. * @hw: pointer to the HW structure
  434. *
  435. * Reads the device MAC address from the EEPROM and stores the value.
  436. * Since devices with two ports use the same EEPROM, we increment the
  437. * last bit in the MAC address for the second port.
  438. **/
  439. s32 e1000_read_mac_addr_generic(struct e1000_hw *hw)
  440. {
  441. u32 rar_high;
  442. u32 rar_low;
  443. u16 i;
  444. rar_high = er32(RAH(0));
  445. rar_low = er32(RAL(0));
  446. for (i = 0; i < E1000_RAL_MAC_ADDR_LEN; i++)
  447. hw->mac.perm_addr[i] = (u8)(rar_low >> (i * 8));
  448. for (i = 0; i < E1000_RAH_MAC_ADDR_LEN; i++)
  449. hw->mac.perm_addr[i + 4] = (u8)(rar_high >> (i * 8));
  450. for (i = 0; i < ETH_ALEN; i++)
  451. hw->mac.addr[i] = hw->mac.perm_addr[i];
  452. return 0;
  453. }
  454. /**
  455. * e1000e_validate_nvm_checksum_generic - Validate EEPROM checksum
  456. * @hw: pointer to the HW structure
  457. *
  458. * Calculates the EEPROM checksum by reading/adding each word of the EEPROM
  459. * and then verifies that the sum of the EEPROM is equal to 0xBABA.
  460. **/
  461. s32 e1000e_validate_nvm_checksum_generic(struct e1000_hw *hw)
  462. {
  463. s32 ret_val;
  464. u16 checksum = 0;
  465. u16 i, nvm_data;
  466. for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
  467. ret_val = e1000_read_nvm(hw, i, 1, &nvm_data);
  468. if (ret_val) {
  469. e_dbg("NVM Read Error\n");
  470. return ret_val;
  471. }
  472. checksum += nvm_data;
  473. }
  474. if (checksum != (u16)NVM_SUM) {
  475. e_dbg("NVM Checksum Invalid\n");
  476. return -E1000_ERR_NVM;
  477. }
  478. return 0;
  479. }
  480. /**
  481. * e1000e_update_nvm_checksum_generic - Update EEPROM checksum
  482. * @hw: pointer to the HW structure
  483. *
  484. * Updates the EEPROM checksum by reading/adding each word of the EEPROM
  485. * up to the checksum. Then calculates the EEPROM checksum and writes the
  486. * value to the EEPROM.
  487. **/
  488. s32 e1000e_update_nvm_checksum_generic(struct e1000_hw *hw)
  489. {
  490. s32 ret_val;
  491. u16 checksum = 0;
  492. u16 i, nvm_data;
  493. for (i = 0; i < NVM_CHECKSUM_REG; i++) {
  494. ret_val = e1000_read_nvm(hw, i, 1, &nvm_data);
  495. if (ret_val) {
  496. e_dbg("NVM Read Error while updating checksum.\n");
  497. return ret_val;
  498. }
  499. checksum += nvm_data;
  500. }
  501. checksum = (u16)NVM_SUM - checksum;
  502. ret_val = e1000_write_nvm(hw, NVM_CHECKSUM_REG, 1, &checksum);
  503. if (ret_val)
  504. e_dbg("NVM Write Error while updating checksum.\n");
  505. return ret_val;
  506. }
  507. /**
  508. * e1000e_reload_nvm_generic - Reloads EEPROM
  509. * @hw: pointer to the HW structure
  510. *
  511. * Reloads the EEPROM by setting the "Reinitialize from EEPROM" bit in the
  512. * extended control register.
  513. **/
  514. void e1000e_reload_nvm_generic(struct e1000_hw *hw)
  515. {
  516. u32 ctrl_ext;
  517. usleep_range(10, 20);
  518. ctrl_ext = er32(CTRL_EXT);
  519. ctrl_ext |= E1000_CTRL_EXT_EE_RST;
  520. ew32(CTRL_EXT, ctrl_ext);
  521. e1e_flush();
  522. }