nvm.c 15 KB

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