e1000_nvm.c 19 KB

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