blob: 89f635fd5dde7739e38a8d2f290bb4e9a0b4fad5 [file] [log] [blame]
Auke Kok9d5c8242008-01-24 02:22:38 -08001/*******************************************************************************
2
3 Intel(R) Gigabit Ethernet Linux driver
Carolyn Wyborny74cfb2e2014-02-25 17:58:57 -08004 Copyright(c) 2007-2014 Intel Corporation.
Auke Kok9d5c8242008-01-24 02:22:38 -08005
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
Carolyn Wyborny74cfb2e2014-02-25 17:58:57 -080016 this program; if not, see <http://www.gnu.org/licenses/>.
Auke Kok9d5c8242008-01-24 02:22:38 -080017
18 The full GNU General Public License is included in this distribution in
19 the file called "COPYING".
20
21 Contact Information:
22 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24
25*******************************************************************************/
26
27#include <linux/if_ether.h>
28#include <linux/delay.h>
29
30#include "e1000_mac.h"
31#include "e1000_nvm.h"
32
33/**
Jeff Kirsher733596b2008-06-27 10:59:59 -070034 * igb_raise_eec_clk - Raise EEPROM clock
Auke Kok9d5c8242008-01-24 02:22:38 -080035 * @hw: pointer to the HW structure
36 * @eecd: pointer to the EEPROM
37 *
38 * Enable/Raise the EEPROM clock bit.
39 **/
40static void igb_raise_eec_clk(struct e1000_hw *hw, u32 *eecd)
41{
42 *eecd = *eecd | E1000_EECD_SK;
43 wr32(E1000_EECD, *eecd);
44 wrfl();
45 udelay(hw->nvm.delay_usec);
46}
47
48/**
Jeff Kirsher733596b2008-06-27 10:59:59 -070049 * igb_lower_eec_clk - Lower EEPROM clock
Auke Kok9d5c8242008-01-24 02:22:38 -080050 * @hw: pointer to the HW structure
51 * @eecd: pointer to the EEPROM
52 *
53 * Clear/Lower the EEPROM clock bit.
54 **/
55static void igb_lower_eec_clk(struct e1000_hw *hw, u32 *eecd)
56{
57 *eecd = *eecd & ~E1000_EECD_SK;
58 wr32(E1000_EECD, *eecd);
59 wrfl();
60 udelay(hw->nvm.delay_usec);
61}
62
63/**
Jeff Kirsher733596b2008-06-27 10:59:59 -070064 * igb_shift_out_eec_bits - Shift data bits our to the EEPROM
Auke Kok9d5c8242008-01-24 02:22:38 -080065 * @hw: pointer to the HW structure
66 * @data: data to send to the EEPROM
67 * @count: number of bits to shift out
68 *
69 * We need to shift 'count' bits out to the EEPROM. So, the value in the
70 * "data" parameter will be shifted out to the EEPROM one bit at a time.
71 * In order to do this, "data" must be broken down into bits.
72 **/
73static void igb_shift_out_eec_bits(struct e1000_hw *hw, u16 data, u16 count)
74{
75 struct e1000_nvm_info *nvm = &hw->nvm;
76 u32 eecd = rd32(E1000_EECD);
77 u32 mask;
78
79 mask = 0x01 << (count - 1);
Alexander Duyck285b4162009-10-05 06:34:44 +000080 if (nvm->type == e1000_nvm_eeprom_spi)
Auke Kok9d5c8242008-01-24 02:22:38 -080081 eecd |= E1000_EECD_DO;
82
83 do {
84 eecd &= ~E1000_EECD_DI;
85
86 if (data & mask)
87 eecd |= E1000_EECD_DI;
88
89 wr32(E1000_EECD, eecd);
90 wrfl();
91
92 udelay(nvm->delay_usec);
93
94 igb_raise_eec_clk(hw, &eecd);
95 igb_lower_eec_clk(hw, &eecd);
96
97 mask >>= 1;
98 } while (mask);
99
100 eecd &= ~E1000_EECD_DI;
101 wr32(E1000_EECD, eecd);
102}
103
104/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700105 * igb_shift_in_eec_bits - Shift data bits in from the EEPROM
Auke Kok9d5c8242008-01-24 02:22:38 -0800106 * @hw: pointer to the HW structure
107 * @count: number of bits to shift in
108 *
109 * In order to read a register from the EEPROM, we need to shift 'count' bits
110 * in from the EEPROM. Bits are "shifted in" by raising the clock input to
111 * the EEPROM (setting the SK bit), and then reading the value of the data out
112 * "DO" bit. During this "shifting in" process the data in "DI" bit should
113 * always be clear.
114 **/
115static u16 igb_shift_in_eec_bits(struct e1000_hw *hw, u16 count)
116{
117 u32 eecd;
118 u32 i;
119 u16 data;
120
121 eecd = rd32(E1000_EECD);
122
123 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
124 data = 0;
125
126 for (i = 0; i < count; i++) {
127 data <<= 1;
128 igb_raise_eec_clk(hw, &eecd);
129
130 eecd = rd32(E1000_EECD);
131
132 eecd &= ~E1000_EECD_DI;
133 if (eecd & E1000_EECD_DO)
134 data |= 1;
135
136 igb_lower_eec_clk(hw, &eecd);
137 }
138
139 return data;
140}
141
142/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700143 * igb_poll_eerd_eewr_done - Poll for EEPROM read/write completion
Auke Kok9d5c8242008-01-24 02:22:38 -0800144 * @hw: pointer to the HW structure
145 * @ee_reg: EEPROM flag for polling
146 *
147 * Polls the EEPROM status bit for either read or write completion based
148 * upon the value of 'ee_reg'.
149 **/
150static s32 igb_poll_eerd_eewr_done(struct e1000_hw *hw, int ee_reg)
151{
152 u32 attempts = 100000;
153 u32 i, reg = 0;
154 s32 ret_val = -E1000_ERR_NVM;
155
156 for (i = 0; i < attempts; i++) {
157 if (ee_reg == E1000_NVM_POLL_READ)
158 reg = rd32(E1000_EERD);
159 else
160 reg = rd32(E1000_EEWR);
161
162 if (reg & E1000_NVM_RW_REG_DONE) {
163 ret_val = 0;
164 break;
165 }
166
167 udelay(5);
168 }
169
170 return ret_val;
171}
172
173/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700174 * igb_acquire_nvm - Generic request for access to EEPROM
Auke Kok9d5c8242008-01-24 02:22:38 -0800175 * @hw: pointer to the HW structure
176 *
177 * Set the EEPROM access request bit and wait for EEPROM access grant bit.
178 * Return successful if access grant bit set, else clear the request for
179 * EEPROM access and return -E1000_ERR_NVM (-1).
180 **/
181s32 igb_acquire_nvm(struct e1000_hw *hw)
182{
183 u32 eecd = rd32(E1000_EECD);
184 s32 timeout = E1000_NVM_GRANT_ATTEMPTS;
185 s32 ret_val = 0;
186
187
188 wr32(E1000_EECD, eecd | E1000_EECD_REQ);
189 eecd = rd32(E1000_EECD);
190
191 while (timeout) {
192 if (eecd & E1000_EECD_GNT)
193 break;
194 udelay(5);
195 eecd = rd32(E1000_EECD);
196 timeout--;
197 }
198
199 if (!timeout) {
200 eecd &= ~E1000_EECD_REQ;
201 wr32(E1000_EECD, eecd);
Auke Kok652fff32008-06-27 11:00:18 -0700202 hw_dbg("Could not acquire NVM grant\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800203 ret_val = -E1000_ERR_NVM;
204 }
205
206 return ret_val;
207}
208
209/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700210 * igb_standby_nvm - Return EEPROM to standby state
Auke Kok9d5c8242008-01-24 02:22:38 -0800211 * @hw: pointer to the HW structure
212 *
213 * Return the EEPROM to a standby state.
214 **/
215static void igb_standby_nvm(struct e1000_hw *hw)
216{
217 struct e1000_nvm_info *nvm = &hw->nvm;
218 u32 eecd = rd32(E1000_EECD);
219
Alexander Duyck285b4162009-10-05 06:34:44 +0000220 if (nvm->type == e1000_nvm_eeprom_spi) {
Auke Kok9d5c8242008-01-24 02:22:38 -0800221 /* Toggle CS to flush commands */
222 eecd |= E1000_EECD_CS;
223 wr32(E1000_EECD, eecd);
224 wrfl();
225 udelay(nvm->delay_usec);
226 eecd &= ~E1000_EECD_CS;
227 wr32(E1000_EECD, eecd);
228 wrfl();
229 udelay(nvm->delay_usec);
230 }
231}
232
233/**
234 * e1000_stop_nvm - Terminate EEPROM command
235 * @hw: pointer to the HW structure
236 *
237 * Terminates the current command by inverting the EEPROM's chip select pin.
238 **/
239static void e1000_stop_nvm(struct e1000_hw *hw)
240{
241 u32 eecd;
242
243 eecd = rd32(E1000_EECD);
244 if (hw->nvm.type == e1000_nvm_eeprom_spi) {
245 /* Pull CS high */
246 eecd |= E1000_EECD_CS;
247 igb_lower_eec_clk(hw, &eecd);
Auke Kok9d5c8242008-01-24 02:22:38 -0800248 }
249}
250
251/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700252 * igb_release_nvm - Release exclusive access to EEPROM
Auke Kok9d5c8242008-01-24 02:22:38 -0800253 * @hw: pointer to the HW structure
254 *
255 * Stop any current commands to the EEPROM and clear the EEPROM request bit.
256 **/
257void igb_release_nvm(struct e1000_hw *hw)
258{
259 u32 eecd;
260
261 e1000_stop_nvm(hw);
262
263 eecd = rd32(E1000_EECD);
264 eecd &= ~E1000_EECD_REQ;
265 wr32(E1000_EECD, eecd);
266}
267
268/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700269 * igb_ready_nvm_eeprom - Prepares EEPROM for read/write
Auke Kok9d5c8242008-01-24 02:22:38 -0800270 * @hw: pointer to the HW structure
271 *
272 * Setups the EEPROM for reading and writing.
273 **/
274static s32 igb_ready_nvm_eeprom(struct e1000_hw *hw)
275{
276 struct e1000_nvm_info *nvm = &hw->nvm;
277 u32 eecd = rd32(E1000_EECD);
278 s32 ret_val = 0;
279 u16 timeout = 0;
280 u8 spi_stat_reg;
281
282
Alexander Duyck285b4162009-10-05 06:34:44 +0000283 if (nvm->type == e1000_nvm_eeprom_spi) {
Auke Kok9d5c8242008-01-24 02:22:38 -0800284 /* Clear SK and CS */
285 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
286 wr32(E1000_EECD, eecd);
Jesse Brandeburg945a5152011-07-20 00:56:21 +0000287 wrfl();
Auke Kok9d5c8242008-01-24 02:22:38 -0800288 udelay(1);
289 timeout = NVM_MAX_RETRY_SPI;
290
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000291 /* Read "Status Register" repeatedly until the LSB is cleared.
Auke Kok9d5c8242008-01-24 02:22:38 -0800292 * The EEPROM will signal that the command has been completed
293 * by clearing bit 0 of the internal status register. If it's
294 * not cleared within 'timeout', then error out.
295 */
296 while (timeout) {
297 igb_shift_out_eec_bits(hw, NVM_RDSR_OPCODE_SPI,
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000298 hw->nvm.opcode_bits);
Auke Kok9d5c8242008-01-24 02:22:38 -0800299 spi_stat_reg = (u8)igb_shift_in_eec_bits(hw, 8);
300 if (!(spi_stat_reg & NVM_STATUS_RDY_SPI))
301 break;
302
303 udelay(5);
304 igb_standby_nvm(hw);
305 timeout--;
306 }
307
308 if (!timeout) {
Auke Kok652fff32008-06-27 11:00:18 -0700309 hw_dbg("SPI NVM Status error\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800310 ret_val = -E1000_ERR_NVM;
311 goto out;
312 }
313 }
314
315out:
316 return ret_val;
317}
318
319/**
Carolyn Wyborny4322e562011-03-11 20:43:18 -0800320 * igb_read_nvm_spi - Read EEPROM's using SPI
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.
327 **/
328s32 igb_read_nvm_spi(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
329{
330 struct e1000_nvm_info *nvm = &hw->nvm;
331 u32 i = 0;
332 s32 ret_val;
333 u16 word_in;
334 u8 read_opcode = NVM_READ_OPCODE_SPI;
335
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000336 /* A check for invalid values: offset too large, too many words,
Carolyn Wyborny4322e562011-03-11 20:43:18 -0800337 * and not enough words.
338 */
339 if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
340 (words == 0)) {
341 hw_dbg("nvm parameter(s) out of bounds\n");
342 ret_val = -E1000_ERR_NVM;
343 goto out;
344 }
345
346 ret_val = nvm->ops.acquire(hw);
347 if (ret_val)
348 goto out;
349
350 ret_val = igb_ready_nvm_eeprom(hw);
351 if (ret_val)
352 goto release;
353
354 igb_standby_nvm(hw);
355
356 if ((nvm->address_bits == 8) && (offset >= 128))
357 read_opcode |= NVM_A8_OPCODE_SPI;
358
359 /* Send the READ command (opcode + addr) */
360 igb_shift_out_eec_bits(hw, read_opcode, nvm->opcode_bits);
361 igb_shift_out_eec_bits(hw, (u16)(offset*2), nvm->address_bits);
362
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000363 /* Read the data. SPI NVMs increment the address with each byte
Carolyn Wyborny4322e562011-03-11 20:43:18 -0800364 * read and will roll over if reading beyond the end. This allows
365 * us to read the whole NVM from any offset
366 */
367 for (i = 0; i < words; i++) {
368 word_in = igb_shift_in_eec_bits(hw, 16);
369 data[i] = (word_in >> 8) | (word_in << 8);
370 }
371
372release:
373 nvm->ops.release(hw);
374
375out:
376 return ret_val;
377}
378
379/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700380 * igb_read_nvm_eerd - Reads EEPROM using EERD register
Auke Kok9d5c8242008-01-24 02:22:38 -0800381 * @hw: pointer to the HW structure
382 * @offset: offset of word in the EEPROM to read
383 * @words: number of words to read
384 * @data: word read from the EEPROM
385 *
386 * Reads a 16 bit word from the EEPROM using the EERD register.
387 **/
388s32 igb_read_nvm_eerd(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
389{
390 struct e1000_nvm_info *nvm = &hw->nvm;
391 u32 i, eerd = 0;
392 s32 ret_val = 0;
393
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000394 /* A check for invalid values: offset too large, too many words,
Auke Kok9d5c8242008-01-24 02:22:38 -0800395 * and not enough words.
396 */
397 if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
398 (words == 0)) {
Auke Kok652fff32008-06-27 11:00:18 -0700399 hw_dbg("nvm parameter(s) out of bounds\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800400 ret_val = -E1000_ERR_NVM;
401 goto out;
402 }
403
404 for (i = 0; i < words; i++) {
405 eerd = ((offset+i) << E1000_NVM_RW_ADDR_SHIFT) +
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000406 E1000_NVM_RW_REG_START;
Auke Kok9d5c8242008-01-24 02:22:38 -0800407
408 wr32(E1000_EERD, eerd);
409 ret_val = igb_poll_eerd_eewr_done(hw, E1000_NVM_POLL_READ);
410 if (ret_val)
411 break;
412
413 data[i] = (rd32(E1000_EERD) >>
Carolyn Wyborny4322e562011-03-11 20:43:18 -0800414 E1000_NVM_RW_REG_DATA);
Auke Kok9d5c8242008-01-24 02:22:38 -0800415 }
416
417out:
418 return ret_val;
419}
420
421/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700422 * igb_write_nvm_spi - Write to EEPROM using SPI
Auke Kok9d5c8242008-01-24 02:22:38 -0800423 * @hw: pointer to the HW structure
424 * @offset: offset within the EEPROM to be written to
425 * @words: number of words to write
426 * @data: 16 bit word(s) to be written to the EEPROM
427 *
428 * Writes data to EEPROM at offset using SPI interface.
429 *
430 * If e1000_update_nvm_checksum is not called after this function , the
431 * EEPROM will most likley contain an invalid checksum.
432 **/
433s32 igb_write_nvm_spi(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
434{
435 struct e1000_nvm_info *nvm = &hw->nvm;
Akeem G. Abodunrin23e0f142012-11-03 03:08:41 +0000436 s32 ret_val = -E1000_ERR_NVM;
Auke Kok9d5c8242008-01-24 02:22:38 -0800437 u16 widx = 0;
438
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000439 /* A check for invalid values: offset too large, too many words,
Auke Kok9d5c8242008-01-24 02:22:38 -0800440 * and not enough words.
441 */
442 if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
443 (words == 0)) {
Auke Kok652fff32008-06-27 11:00:18 -0700444 hw_dbg("nvm parameter(s) out of bounds\n");
Akeem G. Abodunrin23e0f142012-11-03 03:08:41 +0000445 return ret_val;
Auke Kok9d5c8242008-01-24 02:22:38 -0800446 }
447
Auke Kok9d5c8242008-01-24 02:22:38 -0800448 while (widx < words) {
449 u8 write_opcode = NVM_WRITE_OPCODE_SPI;
450
Akeem G. Abodunrin23e0f142012-11-03 03:08:41 +0000451 ret_val = nvm->ops.acquire(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -0800452 if (ret_val)
Akeem G. Abodunrin23e0f142012-11-03 03:08:41 +0000453 return ret_val;
454
455 ret_val = igb_ready_nvm_eeprom(hw);
456 if (ret_val) {
457 nvm->ops.release(hw);
458 return ret_val;
459 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800460
461 igb_standby_nvm(hw);
462
463 /* Send the WRITE ENABLE command (8 bit opcode) */
464 igb_shift_out_eec_bits(hw, NVM_WREN_OPCODE_SPI,
465 nvm->opcode_bits);
466
467 igb_standby_nvm(hw);
468
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000469 /* Some SPI eeproms use the 8th address bit embedded in the
Auke Kok9d5c8242008-01-24 02:22:38 -0800470 * opcode
471 */
472 if ((nvm->address_bits == 8) && (offset >= 128))
473 write_opcode |= NVM_A8_OPCODE_SPI;
474
475 /* Send the Write command (8-bit opcode + addr) */
476 igb_shift_out_eec_bits(hw, write_opcode, nvm->opcode_bits);
477 igb_shift_out_eec_bits(hw, (u16)((offset + widx) * 2),
478 nvm->address_bits);
479
480 /* Loop to allow for up to whole page write of eeprom */
481 while (widx < words) {
482 u16 word_out = data[widx];
Carolyn Wyborny9005df32014-04-11 01:45:34 +0000483
Auke Kok9d5c8242008-01-24 02:22:38 -0800484 word_out = (word_out >> 8) | (word_out << 8);
485 igb_shift_out_eec_bits(hw, word_out, 16);
486 widx++;
487
488 if ((((offset + widx) * 2) % nvm->page_size) == 0) {
489 igb_standby_nvm(hw);
490 break;
491 }
492 }
Akeem G. Abodunrin23e0f142012-11-03 03:08:41 +0000493 usleep_range(1000, 2000);
494 nvm->ops.release(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -0800495 }
496
Auke Kok9d5c8242008-01-24 02:22:38 -0800497 return ret_val;
498}
499
500/**
Carolyn Wyborny9835fd72010-11-22 17:17:21 +0000501 * igb_read_part_string - Read device part number
Auke Kok9d5c8242008-01-24 02:22:38 -0800502 * @hw: pointer to the HW structure
503 * @part_num: pointer to device part number
Carolyn Wyborny9835fd72010-11-22 17:17:21 +0000504 * @part_num_size: size of part number buffer
Auke Kok9d5c8242008-01-24 02:22:38 -0800505 *
506 * Reads the product board assembly (PBA) number from the EEPROM and stores
507 * the value in part_num.
508 **/
Carolyn Wyborny9835fd72010-11-22 17:17:21 +0000509s32 igb_read_part_string(struct e1000_hw *hw, u8 *part_num, u32 part_num_size)
Auke Kok9d5c8242008-01-24 02:22:38 -0800510{
Carolyn Wyborny9835fd72010-11-22 17:17:21 +0000511 s32 ret_val;
Auke Kok9d5c8242008-01-24 02:22:38 -0800512 u16 nvm_data;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +0000513 u16 pointer;
514 u16 offset;
515 u16 length;
516
517 if (part_num == NULL) {
518 hw_dbg("PBA string buffer was null\n");
519 ret_val = E1000_ERR_INVALID_ARGUMENT;
520 goto out;
521 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800522
Alexander Duyck312c75a2009-02-06 23:17:47 +0000523 ret_val = hw->nvm.ops.read(hw, NVM_PBA_OFFSET_0, 1, &nvm_data);
Auke Kok9d5c8242008-01-24 02:22:38 -0800524 if (ret_val) {
Auke Kok652fff32008-06-27 11:00:18 -0700525 hw_dbg("NVM Read Error\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800526 goto out;
527 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800528
Carolyn Wyborny9835fd72010-11-22 17:17:21 +0000529 ret_val = hw->nvm.ops.read(hw, NVM_PBA_OFFSET_1, 1, &pointer);
Auke Kok9d5c8242008-01-24 02:22:38 -0800530 if (ret_val) {
Auke Kok652fff32008-06-27 11:00:18 -0700531 hw_dbg("NVM Read Error\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800532 goto out;
533 }
Carolyn Wyborny9835fd72010-11-22 17:17:21 +0000534
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000535 /* if nvm_data is not ptr guard the PBA must be in legacy format which
Carolyn Wyborny9835fd72010-11-22 17:17:21 +0000536 * means pointer is actually our second data word for the PBA number
537 * and we can decode it into an ascii string
538 */
539 if (nvm_data != NVM_PBA_PTR_GUARD) {
540 hw_dbg("NVM PBA number is not stored as string\n");
541
542 /* we will need 11 characters to store the PBA */
543 if (part_num_size < 11) {
544 hw_dbg("PBA string buffer too small\n");
545 return E1000_ERR_NO_SPACE;
546 }
547
548 /* extract hex string from data and pointer */
549 part_num[0] = (nvm_data >> 12) & 0xF;
550 part_num[1] = (nvm_data >> 8) & 0xF;
551 part_num[2] = (nvm_data >> 4) & 0xF;
552 part_num[3] = nvm_data & 0xF;
553 part_num[4] = (pointer >> 12) & 0xF;
554 part_num[5] = (pointer >> 8) & 0xF;
555 part_num[6] = '-';
556 part_num[7] = 0;
557 part_num[8] = (pointer >> 4) & 0xF;
558 part_num[9] = pointer & 0xF;
559
560 /* put a null character on the end of our string */
561 part_num[10] = '\0';
562
563 /* switch all the data but the '-' to hex char */
564 for (offset = 0; offset < 10; offset++) {
565 if (part_num[offset] < 0xA)
566 part_num[offset] += '0';
567 else if (part_num[offset] < 0x10)
568 part_num[offset] += 'A' - 0xA;
569 }
570
571 goto out;
572 }
573
574 ret_val = hw->nvm.ops.read(hw, pointer, 1, &length);
575 if (ret_val) {
576 hw_dbg("NVM Read Error\n");
577 goto out;
578 }
579
580 if (length == 0xFFFF || length == 0) {
581 hw_dbg("NVM PBA number section invalid length\n");
582 ret_val = E1000_ERR_NVM_PBA_SECTION;
583 goto out;
584 }
585 /* check if part_num buffer is big enough */
586 if (part_num_size < (((u32)length * 2) - 1)) {
587 hw_dbg("PBA string buffer too small\n");
588 ret_val = E1000_ERR_NO_SPACE;
589 goto out;
590 }
591
592 /* trim pba length from start of string */
593 pointer++;
594 length--;
595
596 for (offset = 0; offset < length; offset++) {
597 ret_val = hw->nvm.ops.read(hw, pointer + offset, 1, &nvm_data);
598 if (ret_val) {
599 hw_dbg("NVM Read Error\n");
600 goto out;
601 }
602 part_num[offset * 2] = (u8)(nvm_data >> 8);
603 part_num[(offset * 2) + 1] = (u8)(nvm_data & 0xFF);
604 }
605 part_num[offset * 2] = '\0';
Auke Kok9d5c8242008-01-24 02:22:38 -0800606
607out:
608 return ret_val;
609}
610
611/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700612 * igb_read_mac_addr - Read device MAC address
Auke Kok9d5c8242008-01-24 02:22:38 -0800613 * @hw: pointer to the HW structure
614 *
615 * Reads the device MAC address from the EEPROM and stores the value.
616 * Since devices with two ports use the same EEPROM, we increment the
617 * last bit in the MAC address for the second port.
618 **/
619s32 igb_read_mac_addr(struct e1000_hw *hw)
620{
Alexander Duyck40a70b32009-02-06 23:17:06 +0000621 u32 rar_high;
622 u32 rar_low;
623 u16 i;
Auke Kok9d5c8242008-01-24 02:22:38 -0800624
Alexander Duyck40a70b32009-02-06 23:17:06 +0000625 rar_high = rd32(E1000_RAH(0));
626 rar_low = rd32(E1000_RAL(0));
Auke Kok9d5c8242008-01-24 02:22:38 -0800627
Alexander Duyck40a70b32009-02-06 23:17:06 +0000628 for (i = 0; i < E1000_RAL_MAC_ADDR_LEN; i++)
629 hw->mac.perm_addr[i] = (u8)(rar_low >> (i*8));
630
631 for (i = 0; i < E1000_RAH_MAC_ADDR_LEN; i++)
632 hw->mac.perm_addr[i+4] = (u8)(rar_high >> (i*8));
Auke Kok9d5c8242008-01-24 02:22:38 -0800633
634 for (i = 0; i < ETH_ALEN; i++)
635 hw->mac.addr[i] = hw->mac.perm_addr[i];
636
Alexander Duyck40a70b32009-02-06 23:17:06 +0000637 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -0800638}
639
640/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700641 * igb_validate_nvm_checksum - Validate EEPROM checksum
Auke Kok9d5c8242008-01-24 02:22:38 -0800642 * @hw: pointer to the HW structure
643 *
644 * Calculates the EEPROM checksum by reading/adding each word of the EEPROM
645 * and then verifies that the sum of the EEPROM is equal to 0xBABA.
646 **/
647s32 igb_validate_nvm_checksum(struct e1000_hw *hw)
648{
649 s32 ret_val = 0;
650 u16 checksum = 0;
651 u16 i, nvm_data;
652
653 for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
Alexander Duyck312c75a2009-02-06 23:17:47 +0000654 ret_val = hw->nvm.ops.read(hw, i, 1, &nvm_data);
Auke Kok9d5c8242008-01-24 02:22:38 -0800655 if (ret_val) {
Auke Kok652fff32008-06-27 11:00:18 -0700656 hw_dbg("NVM Read Error\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800657 goto out;
658 }
659 checksum += nvm_data;
660 }
661
662 if (checksum != (u16) NVM_SUM) {
Auke Kok652fff32008-06-27 11:00:18 -0700663 hw_dbg("NVM Checksum Invalid\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800664 ret_val = -E1000_ERR_NVM;
665 goto out;
666 }
667
668out:
669 return ret_val;
670}
671
672/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700673 * igb_update_nvm_checksum - Update EEPROM checksum
Auke Kok9d5c8242008-01-24 02:22:38 -0800674 * @hw: pointer to the HW structure
675 *
676 * Updates the EEPROM checksum by reading/adding each word of the EEPROM
677 * up to the checksum. Then calculates the EEPROM checksum and writes the
678 * value to the EEPROM.
679 **/
680s32 igb_update_nvm_checksum(struct e1000_hw *hw)
681{
682 s32 ret_val;
683 u16 checksum = 0;
684 u16 i, nvm_data;
685
686 for (i = 0; i < NVM_CHECKSUM_REG; i++) {
Alexander Duyck312c75a2009-02-06 23:17:47 +0000687 ret_val = hw->nvm.ops.read(hw, i, 1, &nvm_data);
Auke Kok9d5c8242008-01-24 02:22:38 -0800688 if (ret_val) {
Auke Kok652fff32008-06-27 11:00:18 -0700689 hw_dbg("NVM Read Error while updating checksum.\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800690 goto out;
691 }
692 checksum += nvm_data;
693 }
694 checksum = (u16) NVM_SUM - checksum;
Alexander Duyck312c75a2009-02-06 23:17:47 +0000695 ret_val = hw->nvm.ops.write(hw, NVM_CHECKSUM_REG, 1, &checksum);
Auke Kok9d5c8242008-01-24 02:22:38 -0800696 if (ret_val)
Auke Kok652fff32008-06-27 11:00:18 -0700697 hw_dbg("NVM Write Error while updating checksum.\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800698
699out:
700 return ret_val;
701}
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +0000702
703/**
704 * igb_get_fw_version - Get firmware version information
705 * @hw: pointer to the HW structure
706 * @fw_vers: pointer to output structure
707 *
708 * unsupported MAC types will return all 0 version structure
709 **/
710void igb_get_fw_version(struct e1000_hw *hw, struct e1000_fw_version *fw_vers)
711{
Carolyn Wyborny7dc98a62013-07-16 19:25:33 +0000712 u16 eeprom_verh, eeprom_verl, etrack_test, fw_version;
713 u8 q, hval, rem, result;
714 u16 comb_verh, comb_verl, comb_offset;
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +0000715
716 memset(fw_vers, 0, sizeof(struct e1000_fw_version));
717
Carolyn Wyborny7dc98a62013-07-16 19:25:33 +0000718 /* basic eeprom version numbers and bits used vary by part and by tool
719 * used to create the nvm images. Check which data format we have.
720 */
721 hw->nvm.ops.read(hw, NVM_ETRACK_HIWORD, 1, &etrack_test);
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +0000722 switch (hw->mac.type) {
723 case e1000_i211:
Carolyn Wyborny09e77282012-10-23 13:04:37 +0000724 igb_read_invm_version(hw, fw_vers);
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +0000725 return;
726 case e1000_82575:
727 case e1000_82576:
728 case e1000_82580:
Carolyn Wyborny7dc98a62013-07-16 19:25:33 +0000729 /* Use this format, unless EETRACK ID exists,
730 * then use alternate format
731 */
732 if ((etrack_test & NVM_MAJOR_MASK) != NVM_ETRACK_VALID) {
733 hw->nvm.ops.read(hw, NVM_VERSION, 1, &fw_version);
734 fw_vers->eep_major = (fw_version & NVM_MAJOR_MASK)
735 >> NVM_MAJOR_SHIFT;
736 fw_vers->eep_minor = (fw_version & NVM_MINOR_MASK)
737 >> NVM_MINOR_SHIFT;
738 fw_vers->eep_build = (fw_version & NVM_IMAGE_ID_MASK);
739 goto etrack_id;
740 }
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +0000741 break;
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +0000742 case e1000_i210:
Carolyn Wyborny7dc98a62013-07-16 19:25:33 +0000743 if (!(igb_get_flash_presence_i210(hw))) {
744 igb_read_invm_version(hw, fw_vers);
745 return;
746 }
747 /* fall through */
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +0000748 case e1000_i350:
749 /* find combo image version */
750 hw->nvm.ops.read(hw, NVM_COMB_VER_PTR, 1, &comb_offset);
Carolyn Wyborny7dc98a62013-07-16 19:25:33 +0000751 if ((comb_offset != 0x0) &&
752 (comb_offset != NVM_VER_INVALID)) {
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +0000753
754 hw->nvm.ops.read(hw, (NVM_COMB_VER_OFF + comb_offset
755 + 1), 1, &comb_verh);
756 hw->nvm.ops.read(hw, (NVM_COMB_VER_OFF + comb_offset),
757 1, &comb_verl);
758
759 /* get Option Rom version if it exists and is valid */
760 if ((comb_verh && comb_verl) &&
761 ((comb_verh != NVM_VER_INVALID) &&
762 (comb_verl != NVM_VER_INVALID))) {
763
764 fw_vers->or_valid = true;
765 fw_vers->or_major =
766 comb_verl >> NVM_COMB_VER_SHFT;
767 fw_vers->or_build =
Carolyn Wyborny7dc98a62013-07-16 19:25:33 +0000768 (comb_verl << NVM_COMB_VER_SHFT)
769 | (comb_verh >> NVM_COMB_VER_SHFT);
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +0000770 fw_vers->or_patch =
771 comb_verh & NVM_COMB_VER_MASK;
772 }
773 }
774 break;
775 default:
Carolyn Wyborny7dc98a62013-07-16 19:25:33 +0000776 return;
777 }
778 hw->nvm.ops.read(hw, NVM_VERSION, 1, &fw_version);
779 fw_vers->eep_major = (fw_version & NVM_MAJOR_MASK)
780 >> NVM_MAJOR_SHIFT;
781
782 /* check for old style version format in newer images*/
783 if ((fw_version & NVM_NEW_DEC_MASK) == 0x0) {
784 eeprom_verl = (fw_version & NVM_COMB_VER_MASK);
785 } else {
786 eeprom_verl = (fw_version & NVM_MINOR_MASK)
787 >> NVM_MINOR_SHIFT;
788 }
789 /* Convert minor value to hex before assigning to output struct
790 * Val to be converted will not be higher than 99, per tool output
791 */
792 q = eeprom_verl / NVM_HEX_CONV;
793 hval = q * NVM_HEX_TENS;
794 rem = eeprom_verl % NVM_HEX_CONV;
795 result = hval + rem;
796 fw_vers->eep_minor = result;
797
798etrack_id:
799 if ((etrack_test & NVM_MAJOR_MASK) == NVM_ETRACK_VALID) {
800 hw->nvm.ops.read(hw, NVM_ETRACK_WORD, 1, &eeprom_verl);
801 hw->nvm.ops.read(hw, (NVM_ETRACK_WORD + 1), 1, &eeprom_verh);
802 fw_vers->etrack_id = (eeprom_verh << NVM_ETRACK_SHIFT)
803 | eeprom_verl;
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +0000804 }
805 return;
806}