nvm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /* Intel PRO/1000 Linux driver
  2. * Copyright(c) 1999 - 2015 Intel Corporation.
  3. *
  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. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * The full GNU General Public License is included in this distribution in
  14. * the file called "COPYING".
  15. *
  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 = BIT(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. e_dbg("NVM read error: %d\n", ret_val);
  285. break;
  286. }
  287. data[i] = (er32(EERD) >> E1000_NVM_RW_REG_DATA);
  288. }
  289. return ret_val;
  290. }
  291. /**
  292. * e1000e_write_nvm_spi - Write to EEPROM using SPI
  293. * @hw: pointer to the HW structure
  294. * @offset: offset within the EEPROM to be written to
  295. * @words: number of words to write
  296. * @data: 16 bit word(s) to be written to the EEPROM
  297. *
  298. * Writes data to EEPROM at offset using SPI interface.
  299. *
  300. * If e1000e_update_nvm_checksum is not called after this function , the
  301. * EEPROM will most likely contain an invalid checksum.
  302. **/
  303. s32 e1000e_write_nvm_spi(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
  304. {
  305. struct e1000_nvm_info *nvm = &hw->nvm;
  306. s32 ret_val = -E1000_ERR_NVM;
  307. u16 widx = 0;
  308. /* A check for invalid values: offset too large, too many words,
  309. * and not enough words.
  310. */
  311. if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
  312. (words == 0)) {
  313. e_dbg("nvm parameter(s) out of bounds\n");
  314. return -E1000_ERR_NVM;
  315. }
  316. while (widx < words) {
  317. u8 write_opcode = NVM_WRITE_OPCODE_SPI;
  318. ret_val = nvm->ops.acquire(hw);
  319. if (ret_val)
  320. return ret_val;
  321. ret_val = e1000_ready_nvm_eeprom(hw);
  322. if (ret_val) {
  323. nvm->ops.release(hw);
  324. return ret_val;
  325. }
  326. e1000_standby_nvm(hw);
  327. /* Send the WRITE ENABLE command (8 bit opcode) */
  328. e1000_shift_out_eec_bits(hw, NVM_WREN_OPCODE_SPI,
  329. nvm->opcode_bits);
  330. e1000_standby_nvm(hw);
  331. /* Some SPI eeproms use the 8th address bit embedded in the
  332. * opcode
  333. */
  334. if ((nvm->address_bits == 8) && (offset >= 128))
  335. write_opcode |= NVM_A8_OPCODE_SPI;
  336. /* Send the Write command (8-bit opcode + addr) */
  337. e1000_shift_out_eec_bits(hw, write_opcode, nvm->opcode_bits);
  338. e1000_shift_out_eec_bits(hw, (u16)((offset + widx) * 2),
  339. nvm->address_bits);
  340. /* Loop to allow for up to whole page write of eeprom */
  341. while (widx < words) {
  342. u16 word_out = data[widx];
  343. word_out = (word_out >> 8) | (word_out << 8);
  344. e1000_shift_out_eec_bits(hw, word_out, 16);
  345. widx++;
  346. if ((((offset + widx) * 2) % nvm->page_size) == 0) {
  347. e1000_standby_nvm(hw);
  348. break;
  349. }
  350. }
  351. usleep_range(10000, 20000);
  352. nvm->ops.release(hw);
  353. }
  354. return ret_val;
  355. }
  356. /**
  357. * e1000_read_pba_string_generic - Read device part number
  358. * @hw: pointer to the HW structure
  359. * @pba_num: pointer to device part number
  360. * @pba_num_size: size of part number buffer
  361. *
  362. * Reads the product board assembly (PBA) number from the EEPROM and stores
  363. * the value in pba_num.
  364. **/
  365. s32 e1000_read_pba_string_generic(struct e1000_hw *hw, u8 *pba_num,
  366. u32 pba_num_size)
  367. {
  368. s32 ret_val;
  369. u16 nvm_data;
  370. u16 pba_ptr;
  371. u16 offset;
  372. u16 length;
  373. if (pba_num == NULL) {
  374. e_dbg("PBA string buffer was null\n");
  375. return -E1000_ERR_INVALID_ARGUMENT;
  376. }
  377. ret_val = e1000_read_nvm(hw, NVM_PBA_OFFSET_0, 1, &nvm_data);
  378. if (ret_val) {
  379. e_dbg("NVM Read Error\n");
  380. return ret_val;
  381. }
  382. ret_val = e1000_read_nvm(hw, NVM_PBA_OFFSET_1, 1, &pba_ptr);
  383. if (ret_val) {
  384. e_dbg("NVM Read Error\n");
  385. return ret_val;
  386. }
  387. /* if nvm_data is not ptr guard the PBA must be in legacy format which
  388. * means pba_ptr is actually our second data word for the PBA number
  389. * and we can decode it into an ascii string
  390. */
  391. if (nvm_data != NVM_PBA_PTR_GUARD) {
  392. e_dbg("NVM PBA number is not stored as string\n");
  393. /* make sure callers buffer is big enough to store the PBA */
  394. if (pba_num_size < E1000_PBANUM_LENGTH) {
  395. e_dbg("PBA string buffer too small\n");
  396. return E1000_ERR_NO_SPACE;
  397. }
  398. /* extract hex string from data and pba_ptr */
  399. pba_num[0] = (nvm_data >> 12) & 0xF;
  400. pba_num[1] = (nvm_data >> 8) & 0xF;
  401. pba_num[2] = (nvm_data >> 4) & 0xF;
  402. pba_num[3] = nvm_data & 0xF;
  403. pba_num[4] = (pba_ptr >> 12) & 0xF;
  404. pba_num[5] = (pba_ptr >> 8) & 0xF;
  405. pba_num[6] = '-';
  406. pba_num[7] = 0;
  407. pba_num[8] = (pba_ptr >> 4) & 0xF;
  408. pba_num[9] = pba_ptr & 0xF;
  409. /* put a null character on the end of our string */
  410. pba_num[10] = '\0';
  411. /* switch all the data but the '-' to hex char */
  412. for (offset = 0; offset < 10; offset++) {
  413. if (pba_num[offset] < 0xA)
  414. pba_num[offset] += '0';
  415. else if (pba_num[offset] < 0x10)
  416. pba_num[offset] += 'A' - 0xA;
  417. }
  418. return 0;
  419. }
  420. ret_val = e1000_read_nvm(hw, pba_ptr, 1, &length);
  421. if (ret_val) {
  422. e_dbg("NVM Read Error\n");
  423. return ret_val;
  424. }
  425. if (length == 0xFFFF || length == 0) {
  426. e_dbg("NVM PBA number section invalid length\n");
  427. return -E1000_ERR_NVM_PBA_SECTION;
  428. }
  429. /* check if pba_num buffer is big enough */
  430. if (pba_num_size < (((u32)length * 2) - 1)) {
  431. e_dbg("PBA string buffer too small\n");
  432. return -E1000_ERR_NO_SPACE;
  433. }
  434. /* trim pba length from start of string */
  435. pba_ptr++;
  436. length--;
  437. for (offset = 0; offset < length; offset++) {
  438. ret_val = e1000_read_nvm(hw, pba_ptr + offset, 1, &nvm_data);
  439. if (ret_val) {
  440. e_dbg("NVM Read Error\n");
  441. return ret_val;
  442. }
  443. pba_num[offset * 2] = (u8)(nvm_data >> 8);
  444. pba_num[(offset * 2) + 1] = (u8)(nvm_data & 0xFF);
  445. }
  446. pba_num[offset * 2] = '\0';
  447. return 0;
  448. }
  449. /**
  450. * e1000_read_mac_addr_generic - Read device MAC address
  451. * @hw: pointer to the HW structure
  452. *
  453. * Reads the device MAC address from the EEPROM and stores the value.
  454. * Since devices with two ports use the same EEPROM, we increment the
  455. * last bit in the MAC address for the second port.
  456. **/
  457. s32 e1000_read_mac_addr_generic(struct e1000_hw *hw)
  458. {
  459. u32 rar_high;
  460. u32 rar_low;
  461. u16 i;
  462. rar_high = er32(RAH(0));
  463. rar_low = er32(RAL(0));
  464. for (i = 0; i < E1000_RAL_MAC_ADDR_LEN; i++)
  465. hw->mac.perm_addr[i] = (u8)(rar_low >> (i * 8));
  466. for (i = 0; i < E1000_RAH_MAC_ADDR_LEN; i++)
  467. hw->mac.perm_addr[i + 4] = (u8)(rar_high >> (i * 8));
  468. for (i = 0; i < ETH_ALEN; i++)
  469. hw->mac.addr[i] = hw->mac.perm_addr[i];
  470. return 0;
  471. }
  472. /**
  473. * e1000e_validate_nvm_checksum_generic - Validate EEPROM checksum
  474. * @hw: pointer to the HW structure
  475. *
  476. * Calculates the EEPROM checksum by reading/adding each word of the EEPROM
  477. * and then verifies that the sum of the EEPROM is equal to 0xBABA.
  478. **/
  479. s32 e1000e_validate_nvm_checksum_generic(struct e1000_hw *hw)
  480. {
  481. s32 ret_val;
  482. u16 checksum = 0;
  483. u16 i, nvm_data;
  484. for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
  485. ret_val = e1000_read_nvm(hw, i, 1, &nvm_data);
  486. if (ret_val) {
  487. e_dbg("NVM Read Error\n");
  488. return ret_val;
  489. }
  490. checksum += nvm_data;
  491. }
  492. if (checksum != (u16)NVM_SUM) {
  493. e_dbg("NVM Checksum Invalid\n");
  494. return -E1000_ERR_NVM;
  495. }
  496. return 0;
  497. }
  498. /**
  499. * e1000e_update_nvm_checksum_generic - Update EEPROM checksum
  500. * @hw: pointer to the HW structure
  501. *
  502. * Updates the EEPROM checksum by reading/adding each word of the EEPROM
  503. * up to the checksum. Then calculates the EEPROM checksum and writes the
  504. * value to the EEPROM.
  505. **/
  506. s32 e1000e_update_nvm_checksum_generic(struct e1000_hw *hw)
  507. {
  508. s32 ret_val;
  509. u16 checksum = 0;
  510. u16 i, nvm_data;
  511. for (i = 0; i < NVM_CHECKSUM_REG; i++) {
  512. ret_val = e1000_read_nvm(hw, i, 1, &nvm_data);
  513. if (ret_val) {
  514. e_dbg("NVM Read Error while updating checksum.\n");
  515. return ret_val;
  516. }
  517. checksum += nvm_data;
  518. }
  519. checksum = (u16)NVM_SUM - checksum;
  520. ret_val = e1000_write_nvm(hw, NVM_CHECKSUM_REG, 1, &checksum);
  521. if (ret_val)
  522. e_dbg("NVM Write Error while updating checksum.\n");
  523. return ret_val;
  524. }
  525. /**
  526. * e1000e_reload_nvm_generic - Reloads EEPROM
  527. * @hw: pointer to the HW structure
  528. *
  529. * Reloads the EEPROM by setting the "Reinitialize from EEPROM" bit in the
  530. * extended control register.
  531. **/
  532. void e1000e_reload_nvm_generic(struct e1000_hw *hw)
  533. {
  534. u32 ctrl_ext;
  535. usleep_range(10, 20);
  536. ctrl_ext = er32(CTRL_EXT);
  537. ctrl_ext |= E1000_CTRL_EXT_EE_RST;
  538. ew32(CTRL_EXT, ctrl_ext);
  539. e1e_flush();
  540. }