nvm.c 16 KB

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