e1000_nvm.c 14 KB

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