e1000_nvm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /* Intel(R) Gigabit Ethernet Linux driver
  2. * Copyright(c) 2007-2014 Intel Corporation.
  3. * This program is free software; you can redistribute it and/or modify it
  4. * under the terms and conditions of the GNU General Public License,
  5. * version 2, as published by the Free Software Foundation.
  6. *
  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. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program; if not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * The full GNU General Public License is included in this distribution in
  16. * the file called "COPYING".
  17. *
  18. * Contact Information:
  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 <linux/if_ether.h>
  23. #include <linux/delay.h>
  24. #include "e1000_mac.h"
  25. #include "e1000_nvm.h"
  26. /**
  27. * igb_raise_eec_clk - Raise EEPROM clock
  28. * @hw: pointer to the HW structure
  29. * @eecd: pointer to the EEPROM
  30. *
  31. * Enable/Raise the EEPROM clock bit.
  32. **/
  33. static void igb_raise_eec_clk(struct e1000_hw *hw, u32 *eecd)
  34. {
  35. *eecd = *eecd | E1000_EECD_SK;
  36. wr32(E1000_EECD, *eecd);
  37. wrfl();
  38. udelay(hw->nvm.delay_usec);
  39. }
  40. /**
  41. * igb_lower_eec_clk - Lower EEPROM clock
  42. * @hw: pointer to the HW structure
  43. * @eecd: pointer to the EEPROM
  44. *
  45. * Clear/Lower the EEPROM clock bit.
  46. **/
  47. static void igb_lower_eec_clk(struct e1000_hw *hw, u32 *eecd)
  48. {
  49. *eecd = *eecd & ~E1000_EECD_SK;
  50. wr32(E1000_EECD, *eecd);
  51. wrfl();
  52. udelay(hw->nvm.delay_usec);
  53. }
  54. /**
  55. * igb_shift_out_eec_bits - Shift data bits our to the EEPROM
  56. * @hw: pointer to the HW structure
  57. * @data: data to send to the EEPROM
  58. * @count: number of bits to shift out
  59. *
  60. * We need to shift 'count' bits out to the EEPROM. So, the value in the
  61. * "data" parameter will be shifted out to the EEPROM one bit at a time.
  62. * In order to do this, "data" must be broken down into bits.
  63. **/
  64. static void igb_shift_out_eec_bits(struct e1000_hw *hw, u16 data, u16 count)
  65. {
  66. struct e1000_nvm_info *nvm = &hw->nvm;
  67. u32 eecd = rd32(E1000_EECD);
  68. u32 mask;
  69. mask = 1u << (count - 1);
  70. 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_spi) {
  183. /* Toggle CS to flush commands */
  184. eecd |= E1000_EECD_CS;
  185. wr32(E1000_EECD, eecd);
  186. wrfl();
  187. udelay(nvm->delay_usec);
  188. eecd &= ~E1000_EECD_CS;
  189. wr32(E1000_EECD, eecd);
  190. wrfl();
  191. udelay(nvm->delay_usec);
  192. }
  193. }
  194. /**
  195. * e1000_stop_nvm - Terminate EEPROM command
  196. * @hw: pointer to the HW structure
  197. *
  198. * Terminates the current command by inverting the EEPROM's chip select pin.
  199. **/
  200. static void e1000_stop_nvm(struct e1000_hw *hw)
  201. {
  202. u32 eecd;
  203. eecd = rd32(E1000_EECD);
  204. if (hw->nvm.type == e1000_nvm_eeprom_spi) {
  205. /* Pull CS high */
  206. eecd |= E1000_EECD_CS;
  207. igb_lower_eec_clk(hw, &eecd);
  208. }
  209. }
  210. /**
  211. * igb_release_nvm - Release exclusive access to EEPROM
  212. * @hw: pointer to the HW structure
  213. *
  214. * Stop any current commands to the EEPROM and clear the EEPROM request bit.
  215. **/
  216. void igb_release_nvm(struct e1000_hw *hw)
  217. {
  218. u32 eecd;
  219. e1000_stop_nvm(hw);
  220. eecd = rd32(E1000_EECD);
  221. eecd &= ~E1000_EECD_REQ;
  222. wr32(E1000_EECD, eecd);
  223. }
  224. /**
  225. * igb_ready_nvm_eeprom - Prepares EEPROM for read/write
  226. * @hw: pointer to the HW structure
  227. *
  228. * Setups the EEPROM for reading and writing.
  229. **/
  230. static s32 igb_ready_nvm_eeprom(struct e1000_hw *hw)
  231. {
  232. struct e1000_nvm_info *nvm = &hw->nvm;
  233. u32 eecd = rd32(E1000_EECD);
  234. s32 ret_val = 0;
  235. u16 timeout = 0;
  236. u8 spi_stat_reg;
  237. if (nvm->type == e1000_nvm_eeprom_spi) {
  238. /* Clear SK and CS */
  239. eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
  240. wr32(E1000_EECD, eecd);
  241. wrfl();
  242. udelay(1);
  243. timeout = NVM_MAX_RETRY_SPI;
  244. /* Read "Status Register" repeatedly until the LSB is cleared.
  245. * The EEPROM will signal that the command has been completed
  246. * by clearing bit 0 of the internal status register. If it's
  247. * not cleared within 'timeout', then error out.
  248. */
  249. while (timeout) {
  250. igb_shift_out_eec_bits(hw, NVM_RDSR_OPCODE_SPI,
  251. hw->nvm.opcode_bits);
  252. spi_stat_reg = (u8)igb_shift_in_eec_bits(hw, 8);
  253. if (!(spi_stat_reg & NVM_STATUS_RDY_SPI))
  254. break;
  255. udelay(5);
  256. igb_standby_nvm(hw);
  257. timeout--;
  258. }
  259. if (!timeout) {
  260. hw_dbg("SPI NVM Status error\n");
  261. ret_val = -E1000_ERR_NVM;
  262. goto out;
  263. }
  264. }
  265. out:
  266. return ret_val;
  267. }
  268. /**
  269. * igb_read_nvm_spi - Read EEPROM's using SPI
  270. * @hw: pointer to the HW structure
  271. * @offset: offset of word in the EEPROM to read
  272. * @words: number of words to read
  273. * @data: word read from the EEPROM
  274. *
  275. * Reads a 16 bit word from the EEPROM.
  276. **/
  277. s32 igb_read_nvm_spi(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
  278. {
  279. struct e1000_nvm_info *nvm = &hw->nvm;
  280. u32 i = 0;
  281. s32 ret_val;
  282. u16 word_in;
  283. u8 read_opcode = NVM_READ_OPCODE_SPI;
  284. /* A check for invalid values: offset too large, too many words,
  285. * and not enough words.
  286. */
  287. if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
  288. (words == 0)) {
  289. hw_dbg("nvm parameter(s) out of bounds\n");
  290. ret_val = -E1000_ERR_NVM;
  291. goto out;
  292. }
  293. ret_val = nvm->ops.acquire(hw);
  294. if (ret_val)
  295. goto out;
  296. ret_val = igb_ready_nvm_eeprom(hw);
  297. if (ret_val)
  298. goto release;
  299. igb_standby_nvm(hw);
  300. if ((nvm->address_bits == 8) && (offset >= 128))
  301. read_opcode |= NVM_A8_OPCODE_SPI;
  302. /* Send the READ command (opcode + addr) */
  303. igb_shift_out_eec_bits(hw, read_opcode, nvm->opcode_bits);
  304. igb_shift_out_eec_bits(hw, (u16)(offset*2), nvm->address_bits);
  305. /* Read the data. SPI NVMs increment the address with each byte
  306. * read and will roll over if reading beyond the end. This allows
  307. * us to read the whole NVM from any offset
  308. */
  309. for (i = 0; i < words; i++) {
  310. word_in = igb_shift_in_eec_bits(hw, 16);
  311. data[i] = (word_in >> 8) | (word_in << 8);
  312. }
  313. release:
  314. nvm->ops.release(hw);
  315. out:
  316. return ret_val;
  317. }
  318. /**
  319. * igb_read_nvm_eerd - Reads EEPROM using EERD register
  320. * @hw: pointer to the HW structure
  321. * @offset: offset of word in the EEPROM to read
  322. * @words: number of words to read
  323. * @data: word read from the EEPROM
  324. *
  325. * Reads a 16 bit word from the EEPROM using the EERD register.
  326. **/
  327. s32 igb_read_nvm_eerd(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
  328. {
  329. struct e1000_nvm_info *nvm = &hw->nvm;
  330. u32 i, eerd = 0;
  331. s32 ret_val = 0;
  332. /* A check for invalid values: offset too large, too many words,
  333. * and not enough words.
  334. */
  335. if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
  336. (words == 0)) {
  337. hw_dbg("nvm parameter(s) out of bounds\n");
  338. ret_val = -E1000_ERR_NVM;
  339. goto out;
  340. }
  341. for (i = 0; i < words; i++) {
  342. eerd = ((offset+i) << E1000_NVM_RW_ADDR_SHIFT) +
  343. E1000_NVM_RW_REG_START;
  344. wr32(E1000_EERD, eerd);
  345. ret_val = igb_poll_eerd_eewr_done(hw, E1000_NVM_POLL_READ);
  346. if (ret_val)
  347. break;
  348. data[i] = (rd32(E1000_EERD) >>
  349. E1000_NVM_RW_REG_DATA);
  350. }
  351. out:
  352. return ret_val;
  353. }
  354. /**
  355. * igb_write_nvm_spi - Write to EEPROM using SPI
  356. * @hw: pointer to the HW structure
  357. * @offset: offset within the EEPROM to be written to
  358. * @words: number of words to write
  359. * @data: 16 bit word(s) to be written to the EEPROM
  360. *
  361. * Writes data to EEPROM at offset using SPI interface.
  362. *
  363. * If e1000_update_nvm_checksum is not called after this function , the
  364. * EEPROM will most likley contain an invalid checksum.
  365. **/
  366. s32 igb_write_nvm_spi(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
  367. {
  368. struct e1000_nvm_info *nvm = &hw->nvm;
  369. s32 ret_val = -E1000_ERR_NVM;
  370. u16 widx = 0;
  371. /* A check for invalid values: offset too large, too many words,
  372. * and not enough words.
  373. */
  374. if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
  375. (words == 0)) {
  376. hw_dbg("nvm parameter(s) out of bounds\n");
  377. return ret_val;
  378. }
  379. while (widx < words) {
  380. u8 write_opcode = NVM_WRITE_OPCODE_SPI;
  381. ret_val = nvm->ops.acquire(hw);
  382. if (ret_val)
  383. return ret_val;
  384. ret_val = igb_ready_nvm_eeprom(hw);
  385. if (ret_val) {
  386. nvm->ops.release(hw);
  387. return ret_val;
  388. }
  389. igb_standby_nvm(hw);
  390. /* Send the WRITE ENABLE command (8 bit opcode) */
  391. igb_shift_out_eec_bits(hw, NVM_WREN_OPCODE_SPI,
  392. nvm->opcode_bits);
  393. igb_standby_nvm(hw);
  394. /* Some SPI eeproms use the 8th address bit embedded in the
  395. * opcode
  396. */
  397. if ((nvm->address_bits == 8) && (offset >= 128))
  398. write_opcode |= NVM_A8_OPCODE_SPI;
  399. /* Send the Write command (8-bit opcode + addr) */
  400. igb_shift_out_eec_bits(hw, write_opcode, nvm->opcode_bits);
  401. igb_shift_out_eec_bits(hw, (u16)((offset + widx) * 2),
  402. nvm->address_bits);
  403. /* Loop to allow for up to whole page write of eeprom */
  404. while (widx < words) {
  405. u16 word_out = data[widx];
  406. word_out = (word_out >> 8) | (word_out << 8);
  407. igb_shift_out_eec_bits(hw, word_out, 16);
  408. widx++;
  409. if ((((offset + widx) * 2) % nvm->page_size) == 0) {
  410. igb_standby_nvm(hw);
  411. break;
  412. }
  413. }
  414. usleep_range(1000, 2000);
  415. nvm->ops.release(hw);
  416. }
  417. return ret_val;
  418. }
  419. /**
  420. * igb_read_part_string - Read device part number
  421. * @hw: pointer to the HW structure
  422. * @part_num: pointer to device part number
  423. * @part_num_size: size of part number buffer
  424. *
  425. * Reads the product board assembly (PBA) number from the EEPROM and stores
  426. * the value in part_num.
  427. **/
  428. s32 igb_read_part_string(struct e1000_hw *hw, u8 *part_num, u32 part_num_size)
  429. {
  430. s32 ret_val;
  431. u16 nvm_data;
  432. u16 pointer;
  433. u16 offset;
  434. u16 length;
  435. if (part_num == NULL) {
  436. hw_dbg("PBA string buffer was null\n");
  437. ret_val = E1000_ERR_INVALID_ARGUMENT;
  438. goto out;
  439. }
  440. ret_val = hw->nvm.ops.read(hw, NVM_PBA_OFFSET_0, 1, &nvm_data);
  441. if (ret_val) {
  442. hw_dbg("NVM Read Error\n");
  443. goto out;
  444. }
  445. ret_val = hw->nvm.ops.read(hw, NVM_PBA_OFFSET_1, 1, &pointer);
  446. if (ret_val) {
  447. hw_dbg("NVM Read Error\n");
  448. goto out;
  449. }
  450. /* if nvm_data is not ptr guard the PBA must be in legacy format which
  451. * means pointer is actually our second data word for the PBA number
  452. * and we can decode it into an ascii string
  453. */
  454. if (nvm_data != NVM_PBA_PTR_GUARD) {
  455. hw_dbg("NVM PBA number is not stored as string\n");
  456. /* we will need 11 characters to store the PBA */
  457. if (part_num_size < 11) {
  458. hw_dbg("PBA string buffer too small\n");
  459. return E1000_ERR_NO_SPACE;
  460. }
  461. /* extract hex string from data and pointer */
  462. part_num[0] = (nvm_data >> 12) & 0xF;
  463. part_num[1] = (nvm_data >> 8) & 0xF;
  464. part_num[2] = (nvm_data >> 4) & 0xF;
  465. part_num[3] = nvm_data & 0xF;
  466. part_num[4] = (pointer >> 12) & 0xF;
  467. part_num[5] = (pointer >> 8) & 0xF;
  468. part_num[6] = '-';
  469. part_num[7] = 0;
  470. part_num[8] = (pointer >> 4) & 0xF;
  471. part_num[9] = pointer & 0xF;
  472. /* put a null character on the end of our string */
  473. part_num[10] = '\0';
  474. /* switch all the data but the '-' to hex char */
  475. for (offset = 0; offset < 10; offset++) {
  476. if (part_num[offset] < 0xA)
  477. part_num[offset] += '0';
  478. else if (part_num[offset] < 0x10)
  479. part_num[offset] += 'A' - 0xA;
  480. }
  481. goto out;
  482. }
  483. ret_val = hw->nvm.ops.read(hw, pointer, 1, &length);
  484. if (ret_val) {
  485. hw_dbg("NVM Read Error\n");
  486. goto out;
  487. }
  488. if (length == 0xFFFF || length == 0) {
  489. hw_dbg("NVM PBA number section invalid length\n");
  490. ret_val = E1000_ERR_NVM_PBA_SECTION;
  491. goto out;
  492. }
  493. /* check if part_num buffer is big enough */
  494. if (part_num_size < (((u32)length * 2) - 1)) {
  495. hw_dbg("PBA string buffer too small\n");
  496. ret_val = E1000_ERR_NO_SPACE;
  497. goto out;
  498. }
  499. /* trim pba length from start of string */
  500. pointer++;
  501. length--;
  502. for (offset = 0; offset < length; offset++) {
  503. ret_val = hw->nvm.ops.read(hw, pointer + offset, 1, &nvm_data);
  504. if (ret_val) {
  505. hw_dbg("NVM Read Error\n");
  506. goto out;
  507. }
  508. part_num[offset * 2] = (u8)(nvm_data >> 8);
  509. part_num[(offset * 2) + 1] = (u8)(nvm_data & 0xFF);
  510. }
  511. part_num[offset * 2] = '\0';
  512. out:
  513. return ret_val;
  514. }
  515. /**
  516. * igb_read_mac_addr - Read device MAC address
  517. * @hw: pointer to the HW structure
  518. *
  519. * Reads the device MAC address from the EEPROM and stores the value.
  520. * Since devices with two ports use the same EEPROM, we increment the
  521. * last bit in the MAC address for the second port.
  522. **/
  523. s32 igb_read_mac_addr(struct e1000_hw *hw)
  524. {
  525. u32 rar_high;
  526. u32 rar_low;
  527. u16 i;
  528. rar_high = rd32(E1000_RAH(0));
  529. rar_low = rd32(E1000_RAL(0));
  530. for (i = 0; i < E1000_RAL_MAC_ADDR_LEN; i++)
  531. hw->mac.perm_addr[i] = (u8)(rar_low >> (i*8));
  532. for (i = 0; i < E1000_RAH_MAC_ADDR_LEN; i++)
  533. hw->mac.perm_addr[i+4] = (u8)(rar_high >> (i*8));
  534. for (i = 0; i < ETH_ALEN; i++)
  535. hw->mac.addr[i] = hw->mac.perm_addr[i];
  536. return 0;
  537. }
  538. /**
  539. * igb_validate_nvm_checksum - Validate EEPROM checksum
  540. * @hw: pointer to the HW structure
  541. *
  542. * Calculates the EEPROM checksum by reading/adding each word of the EEPROM
  543. * and then verifies that the sum of the EEPROM is equal to 0xBABA.
  544. **/
  545. s32 igb_validate_nvm_checksum(struct e1000_hw *hw)
  546. {
  547. s32 ret_val = 0;
  548. u16 checksum = 0;
  549. u16 i, nvm_data;
  550. for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
  551. ret_val = hw->nvm.ops.read(hw, i, 1, &nvm_data);
  552. if (ret_val) {
  553. hw_dbg("NVM Read Error\n");
  554. goto out;
  555. }
  556. checksum += nvm_data;
  557. }
  558. if (checksum != (u16) NVM_SUM) {
  559. hw_dbg("NVM Checksum Invalid\n");
  560. ret_val = -E1000_ERR_NVM;
  561. goto out;
  562. }
  563. out:
  564. return ret_val;
  565. }
  566. /**
  567. * igb_update_nvm_checksum - Update EEPROM checksum
  568. * @hw: pointer to the HW structure
  569. *
  570. * Updates the EEPROM checksum by reading/adding each word of the EEPROM
  571. * up to the checksum. Then calculates the EEPROM checksum and writes the
  572. * value to the EEPROM.
  573. **/
  574. s32 igb_update_nvm_checksum(struct e1000_hw *hw)
  575. {
  576. s32 ret_val;
  577. u16 checksum = 0;
  578. u16 i, nvm_data;
  579. for (i = 0; i < NVM_CHECKSUM_REG; i++) {
  580. ret_val = hw->nvm.ops.read(hw, i, 1, &nvm_data);
  581. if (ret_val) {
  582. hw_dbg("NVM Read Error while updating checksum.\n");
  583. goto out;
  584. }
  585. checksum += nvm_data;
  586. }
  587. checksum = (u16) NVM_SUM - checksum;
  588. ret_val = hw->nvm.ops.write(hw, NVM_CHECKSUM_REG, 1, &checksum);
  589. if (ret_val)
  590. hw_dbg("NVM Write Error while updating checksum.\n");
  591. out:
  592. return ret_val;
  593. }
  594. /**
  595. * igb_get_fw_version - Get firmware version information
  596. * @hw: pointer to the HW structure
  597. * @fw_vers: pointer to output structure
  598. *
  599. * unsupported MAC types will return all 0 version structure
  600. **/
  601. void igb_get_fw_version(struct e1000_hw *hw, struct e1000_fw_version *fw_vers)
  602. {
  603. u16 eeprom_verh, eeprom_verl, etrack_test, fw_version;
  604. u8 q, hval, rem, result;
  605. u16 comb_verh, comb_verl, comb_offset;
  606. memset(fw_vers, 0, sizeof(struct e1000_fw_version));
  607. /* basic eeprom version numbers and bits used vary by part and by tool
  608. * used to create the nvm images. Check which data format we have.
  609. */
  610. hw->nvm.ops.read(hw, NVM_ETRACK_HIWORD, 1, &etrack_test);
  611. switch (hw->mac.type) {
  612. case e1000_i211:
  613. igb_read_invm_version(hw, fw_vers);
  614. return;
  615. case e1000_82575:
  616. case e1000_82576:
  617. case e1000_82580:
  618. /* Use this format, unless EETRACK ID exists,
  619. * then use alternate format
  620. */
  621. if ((etrack_test & NVM_MAJOR_MASK) != NVM_ETRACK_VALID) {
  622. hw->nvm.ops.read(hw, NVM_VERSION, 1, &fw_version);
  623. fw_vers->eep_major = (fw_version & NVM_MAJOR_MASK)
  624. >> NVM_MAJOR_SHIFT;
  625. fw_vers->eep_minor = (fw_version & NVM_MINOR_MASK)
  626. >> NVM_MINOR_SHIFT;
  627. fw_vers->eep_build = (fw_version & NVM_IMAGE_ID_MASK);
  628. goto etrack_id;
  629. }
  630. break;
  631. case e1000_i210:
  632. if (!(igb_get_flash_presence_i210(hw))) {
  633. igb_read_invm_version(hw, fw_vers);
  634. return;
  635. }
  636. /* fall through */
  637. case e1000_i350:
  638. /* find combo image version */
  639. hw->nvm.ops.read(hw, NVM_COMB_VER_PTR, 1, &comb_offset);
  640. if ((comb_offset != 0x0) &&
  641. (comb_offset != NVM_VER_INVALID)) {
  642. hw->nvm.ops.read(hw, (NVM_COMB_VER_OFF + comb_offset
  643. + 1), 1, &comb_verh);
  644. hw->nvm.ops.read(hw, (NVM_COMB_VER_OFF + comb_offset),
  645. 1, &comb_verl);
  646. /* get Option Rom version if it exists and is valid */
  647. if ((comb_verh && comb_verl) &&
  648. ((comb_verh != NVM_VER_INVALID) &&
  649. (comb_verl != NVM_VER_INVALID))) {
  650. fw_vers->or_valid = true;
  651. fw_vers->or_major =
  652. comb_verl >> NVM_COMB_VER_SHFT;
  653. fw_vers->or_build =
  654. (comb_verl << NVM_COMB_VER_SHFT)
  655. | (comb_verh >> NVM_COMB_VER_SHFT);
  656. fw_vers->or_patch =
  657. comb_verh & NVM_COMB_VER_MASK;
  658. }
  659. }
  660. break;
  661. default:
  662. return;
  663. }
  664. hw->nvm.ops.read(hw, NVM_VERSION, 1, &fw_version);
  665. fw_vers->eep_major = (fw_version & NVM_MAJOR_MASK)
  666. >> NVM_MAJOR_SHIFT;
  667. /* check for old style version format in newer images*/
  668. if ((fw_version & NVM_NEW_DEC_MASK) == 0x0) {
  669. eeprom_verl = (fw_version & NVM_COMB_VER_MASK);
  670. } else {
  671. eeprom_verl = (fw_version & NVM_MINOR_MASK)
  672. >> NVM_MINOR_SHIFT;
  673. }
  674. /* Convert minor value to hex before assigning to output struct
  675. * Val to be converted will not be higher than 99, per tool output
  676. */
  677. q = eeprom_verl / NVM_HEX_CONV;
  678. hval = q * NVM_HEX_TENS;
  679. rem = eeprom_verl % NVM_HEX_CONV;
  680. result = hval + rem;
  681. fw_vers->eep_minor = result;
  682. etrack_id:
  683. if ((etrack_test & NVM_MAJOR_MASK) == NVM_ETRACK_VALID) {
  684. hw->nvm.ops.read(hw, NVM_ETRACK_WORD, 1, &eeprom_verl);
  685. hw->nvm.ops.read(hw, (NVM_ETRACK_WORD + 1), 1, &eeprom_verh);
  686. fw_vers->etrack_id = (eeprom_verh << NVM_ETRACK_SHIFT)
  687. | eeprom_verl;
  688. }
  689. }