e1000_nvm.c 20 KB

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