e1000_nvm.c 13 KB

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