Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / workbench / devs / networks / e1000 / e1000_nvm.c
blob22ccc9a1fef6df84e15d3aeb873d42750bcda54c
1 /*******************************************************************************
3 Intel PRO/1000 Linux driver
4 Copyright(c) 1999 - 2008 Intel Corporation.
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.
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.
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
22 Contact Information:
23 Linux NICS <linux.nics@intel.com>
24 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *******************************************************************************/
29 #include "e1000_api.h"
30 #include "e1000_nvm.h"
32 /**
33 * e1000_init_nvm_ops_generic - Initialize NVM function pointers
34 * @hw: pointer to the HW structure
36 * Setups up the function pointers to no-op functions
37 **/
38 void e1000_init_nvm_ops_generic(struct e1000_hw *hw)
40 struct e1000_nvm_info *nvm = &hw->nvm;
41 DEBUGFUNC("e1000_init_nvm_ops_generic");
43 /* Initialize function pointers */
44 nvm->ops.init_params = e1000_null_ops_generic;
45 nvm->ops.acquire = e1000_null_ops_generic;
46 nvm->ops.read = e1000_null_read_nvm;
47 nvm->ops.release = e1000_null_nvm_generic;
48 nvm->ops.reload = e1000_reload_nvm_generic;
49 nvm->ops.update = e1000_null_ops_generic;
50 nvm->ops.valid_led_default = e1000_null_led_default;
51 nvm->ops.validate = e1000_null_ops_generic;
52 nvm->ops.write = e1000_null_write_nvm;
55 /**
56 * e1000_null_nvm_read - No-op function, return 0
57 * @hw: pointer to the HW structure
58 **/
59 s32 e1000_null_read_nvm(struct e1000_hw *hw, u16 a, u16 b, u16 *c)
61 DEBUGFUNC("e1000_null_read_nvm");
62 return E1000_SUCCESS;
65 /**
66 * e1000_null_nvm_generic - No-op function, return void
67 * @hw: pointer to the HW structure
68 **/
69 void e1000_null_nvm_generic(struct e1000_hw *hw)
71 DEBUGFUNC("e1000_null_nvm_generic");
72 return;
75 /**
76 * e1000_null_led_default - No-op function, return 0
77 * @hw: pointer to the HW structure
78 **/
79 s32 e1000_null_led_default(struct e1000_hw *hw, u16 *data)
81 DEBUGFUNC("e1000_null_led_default");
82 return E1000_SUCCESS;
85 /**
86 * e1000_null_write_nvm - No-op function, return 0
87 * @hw: pointer to the HW structure
88 **/
89 s32 e1000_null_write_nvm(struct e1000_hw *hw, u16 a, u16 b, u16 *c)
91 DEBUGFUNC("e1000_null_write_nvm");
92 return E1000_SUCCESS;
95 /**
96 * e1000_raise_eec_clk - Raise EEPROM clock
97 * @hw: pointer to the HW structure
98 * @eecd: pointer to the EEPROM
100 * Enable/Raise the EEPROM clock bit.
102 static void e1000_raise_eec_clk(struct e1000_hw *hw, u32 *eecd)
104 *eecd = *eecd | E1000_EECD_SK;
105 E1000_WRITE_REG(hw, E1000_EECD, *eecd);
106 E1000_WRITE_FLUSH(hw);
107 usec_delay(hw->nvm.delay_usec);
111 * e1000_lower_eec_clk - Lower EEPROM clock
112 * @hw: pointer to the HW structure
113 * @eecd: pointer to the EEPROM
115 * Clear/Lower the EEPROM clock bit.
117 static void e1000_lower_eec_clk(struct e1000_hw *hw, u32 *eecd)
119 *eecd = *eecd & ~E1000_EECD_SK;
120 E1000_WRITE_REG(hw, E1000_EECD, *eecd);
121 E1000_WRITE_FLUSH(hw);
122 usec_delay(hw->nvm.delay_usec);
126 * e1000_shift_out_eec_bits - Shift data bits our to the EEPROM
127 * @hw: pointer to the HW structure
128 * @data: data to send to the EEPROM
129 * @count: number of bits to shift out
131 * We need to shift 'count' bits out to the EEPROM. So, the value in the
132 * "data" parameter will be shifted out to the EEPROM one bit at a time.
133 * In order to do this, "data" must be broken down into bits.
135 static void e1000_shift_out_eec_bits(struct e1000_hw *hw, u16 data, u16 count)
137 struct e1000_nvm_info *nvm = &hw->nvm;
138 u32 eecd = E1000_READ_REG(hw, E1000_EECD);
139 u32 mask;
141 DEBUGFUNC("e1000_shift_out_eec_bits");
143 mask = 0x01 << (count - 1);
144 if (nvm->type == e1000_nvm_eeprom_microwire)
145 eecd &= ~E1000_EECD_DO;
146 else if (nvm->type == e1000_nvm_eeprom_spi)
147 eecd |= E1000_EECD_DO;
149 do {
150 eecd &= ~E1000_EECD_DI;
152 if (data & mask)
153 eecd |= E1000_EECD_DI;
155 E1000_WRITE_REG(hw, E1000_EECD, eecd);
156 E1000_WRITE_FLUSH(hw);
158 usec_delay(nvm->delay_usec);
160 e1000_raise_eec_clk(hw, &eecd);
161 e1000_lower_eec_clk(hw, &eecd);
163 mask >>= 1;
164 } while (mask);
166 eecd &= ~E1000_EECD_DI;
167 E1000_WRITE_REG(hw, E1000_EECD, eecd);
171 * e1000_shift_in_eec_bits - Shift data bits in from the EEPROM
172 * @hw: pointer to the HW structure
173 * @count: number of bits to shift in
175 * In order to read a register from the EEPROM, we need to shift 'count' bits
176 * in from the EEPROM. Bits are "shifted in" by raising the clock input to
177 * the EEPROM (setting the SK bit), and then reading the value of the data out
178 * "DO" bit. During this "shifting in" process the data in "DI" bit should
179 * always be clear.
181 static u16 e1000_shift_in_eec_bits(struct e1000_hw *hw, u16 count)
183 u32 eecd;
184 u32 i;
185 u16 data;
187 DEBUGFUNC("e1000_shift_in_eec_bits");
189 eecd = E1000_READ_REG(hw, E1000_EECD);
191 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
192 data = 0;
194 for (i = 0; i < count; i++) {
195 data <<= 1;
196 e1000_raise_eec_clk(hw, &eecd);
198 eecd = E1000_READ_REG(hw, E1000_EECD);
200 eecd &= ~E1000_EECD_DI;
201 if (eecd & E1000_EECD_DO)
202 data |= 1;
204 e1000_lower_eec_clk(hw, &eecd);
207 return data;
211 * e1000_poll_eerd_eewr_done - Poll for EEPROM read/write completion
212 * @hw: pointer to the HW structure
213 * @ee_reg: EEPROM flag for polling
215 * Polls the EEPROM status bit for either read or write completion based
216 * upon the value of 'ee_reg'.
218 s32 e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int ee_reg)
220 u32 attempts = 100000;
221 u32 i, reg = 0;
222 s32 ret_val = -E1000_ERR_NVM;
224 DEBUGFUNC("e1000_poll_eerd_eewr_done");
226 for (i = 0; i < attempts; i++) {
227 if (ee_reg == E1000_NVM_POLL_READ)
228 reg = E1000_READ_REG(hw, E1000_EERD);
229 else
230 reg = E1000_READ_REG(hw, E1000_EEWR);
232 if (reg & E1000_NVM_RW_REG_DONE) {
233 ret_val = E1000_SUCCESS;
234 break;
237 usec_delay(5);
240 return ret_val;
244 * e1000_acquire_nvm_generic - Generic request for access to EEPROM
245 * @hw: pointer to the HW structure
247 * Set the EEPROM access request bit and wait for EEPROM access grant bit.
248 * Return successful if access grant bit set, else clear the request for
249 * EEPROM access and return -E1000_ERR_NVM (-1).
251 s32 e1000_acquire_nvm_generic(struct e1000_hw *hw)
253 u32 eecd = E1000_READ_REG(hw, E1000_EECD);
254 s32 timeout = E1000_NVM_GRANT_ATTEMPTS;
255 s32 ret_val = E1000_SUCCESS;
257 DEBUGFUNC("e1000_acquire_nvm_generic");
259 E1000_WRITE_REG(hw, E1000_EECD, eecd | E1000_EECD_REQ);
260 eecd = E1000_READ_REG(hw, E1000_EECD);
262 while (timeout) {
263 if (eecd & E1000_EECD_GNT)
264 break;
265 usec_delay(5);
266 eecd = E1000_READ_REG(hw, E1000_EECD);
267 timeout--;
270 if (!timeout) {
271 eecd &= ~E1000_EECD_REQ;
272 E1000_WRITE_REG(hw, E1000_EECD, eecd);
273 DEBUGOUT("Could not acquire NVM grant\n");
274 ret_val = -E1000_ERR_NVM;
277 return ret_val;
281 * e1000_standby_nvm - Return EEPROM to standby state
282 * @hw: pointer to the HW structure
284 * Return the EEPROM to a standby state.
286 static void e1000_standby_nvm(struct e1000_hw *hw)
288 struct e1000_nvm_info *nvm = &hw->nvm;
289 u32 eecd = E1000_READ_REG(hw, E1000_EECD);
291 DEBUGFUNC("e1000_standby_nvm");
293 if (nvm->type == e1000_nvm_eeprom_microwire) {
294 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
295 E1000_WRITE_REG(hw, E1000_EECD, eecd);
296 E1000_WRITE_FLUSH(hw);
297 usec_delay(nvm->delay_usec);
299 e1000_raise_eec_clk(hw, &eecd);
301 /* Select EEPROM */
302 eecd |= E1000_EECD_CS;
303 E1000_WRITE_REG(hw, E1000_EECD, eecd);
304 E1000_WRITE_FLUSH(hw);
305 usec_delay(nvm->delay_usec);
307 e1000_lower_eec_clk(hw, &eecd);
308 } else if (nvm->type == e1000_nvm_eeprom_spi) {
309 /* Toggle CS to flush commands */
310 eecd |= E1000_EECD_CS;
311 E1000_WRITE_REG(hw, E1000_EECD, eecd);
312 E1000_WRITE_FLUSH(hw);
313 usec_delay(nvm->delay_usec);
314 eecd &= ~E1000_EECD_CS;
315 E1000_WRITE_REG(hw, E1000_EECD, eecd);
316 E1000_WRITE_FLUSH(hw);
317 usec_delay(nvm->delay_usec);
322 * e1000_stop_nvm - Terminate EEPROM command
323 * @hw: pointer to the HW structure
325 * Terminates the current command by inverting the EEPROM's chip select pin.
327 void e1000_stop_nvm(struct e1000_hw *hw)
329 u32 eecd;
331 DEBUGFUNC("e1000_stop_nvm");
333 eecd = E1000_READ_REG(hw, E1000_EECD);
334 if (hw->nvm.type == e1000_nvm_eeprom_spi) {
335 /* Pull CS high */
336 eecd |= E1000_EECD_CS;
337 e1000_lower_eec_clk(hw, &eecd);
338 } else if (hw->nvm.type == e1000_nvm_eeprom_microwire) {
339 /* CS on Microwire is active-high */
340 eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
341 E1000_WRITE_REG(hw, E1000_EECD, eecd);
342 e1000_raise_eec_clk(hw, &eecd);
343 e1000_lower_eec_clk(hw, &eecd);
348 * e1000_release_nvm_generic - Release exclusive access to EEPROM
349 * @hw: pointer to the HW structure
351 * Stop any current commands to the EEPROM and clear the EEPROM request bit.
353 void e1000_release_nvm_generic(struct e1000_hw *hw)
355 u32 eecd;
357 DEBUGFUNC("e1000_release_nvm_generic");
359 e1000_stop_nvm(hw);
361 eecd = E1000_READ_REG(hw, E1000_EECD);
362 eecd &= ~E1000_EECD_REQ;
363 E1000_WRITE_REG(hw, E1000_EECD, eecd);
367 * e1000_ready_nvm_eeprom - Prepares EEPROM for read/write
368 * @hw: pointer to the HW structure
370 * Setups the EEPROM for reading and writing.
372 static s32 e1000_ready_nvm_eeprom(struct e1000_hw *hw)
374 struct e1000_nvm_info *nvm = &hw->nvm;
375 u32 eecd = E1000_READ_REG(hw, E1000_EECD);
376 s32 ret_val = E1000_SUCCESS;
377 u16 timeout = 0;
378 u8 spi_stat_reg;
380 DEBUGFUNC("e1000_ready_nvm_eeprom");
382 if (nvm->type == e1000_nvm_eeprom_microwire) {
383 /* Clear SK and DI */
384 eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
385 E1000_WRITE_REG(hw, E1000_EECD, eecd);
386 /* Set CS */
387 eecd |= E1000_EECD_CS;
388 E1000_WRITE_REG(hw, E1000_EECD, eecd);
389 } else if (nvm->type == e1000_nvm_eeprom_spi) {
390 /* Clear SK and CS */
391 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
392 E1000_WRITE_REG(hw, E1000_EECD, eecd);
393 usec_delay(1);
394 timeout = NVM_MAX_RETRY_SPI;
397 * Read "Status Register" repeatedly until the LSB is cleared.
398 * The EEPROM will signal that the command has been completed
399 * by clearing bit 0 of the internal status register. If it's
400 * not cleared within 'timeout', then error out.
402 while (timeout) {
403 e1000_shift_out_eec_bits(hw, NVM_RDSR_OPCODE_SPI,
404 hw->nvm.opcode_bits);
405 spi_stat_reg = (u8)e1000_shift_in_eec_bits(hw, 8);
406 if (!(spi_stat_reg & NVM_STATUS_RDY_SPI))
407 break;
409 usec_delay(5);
410 e1000_standby_nvm(hw);
411 timeout--;
414 if (!timeout) {
415 DEBUGOUT("SPI NVM Status error\n");
416 ret_val = -E1000_ERR_NVM;
417 goto out;
421 out:
422 return ret_val;
426 * e1000_read_nvm_spi - Read EEPROM's using SPI
427 * @hw: pointer to the HW structure
428 * @offset: offset of word in the EEPROM to read
429 * @words: number of words to read
430 * @data: word read from the EEPROM
432 * Reads a 16 bit word from the EEPROM.
434 s32 e1000_read_nvm_spi(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
436 struct e1000_nvm_info *nvm = &hw->nvm;
437 u32 i = 0;
438 s32 ret_val;
439 u16 word_in;
440 u8 read_opcode = NVM_READ_OPCODE_SPI;
442 DEBUGFUNC("e1000_read_nvm_spi");
445 * A check for invalid values: offset too large, too many words,
446 * and not enough words.
448 if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
449 (words == 0)) {
450 DEBUGOUT("nvm parameter(s) out of bounds\n");
451 ret_val = -E1000_ERR_NVM;
452 goto out;
455 ret_val = nvm->ops.acquire(hw);
456 if (ret_val)
457 goto out;
459 ret_val = e1000_ready_nvm_eeprom(hw);
460 if (ret_val)
461 goto release;
463 e1000_standby_nvm(hw);
465 if ((nvm->address_bits == 8) && (offset >= 128))
466 read_opcode |= NVM_A8_OPCODE_SPI;
468 /* Send the READ command (opcode + addr) */
469 e1000_shift_out_eec_bits(hw, read_opcode, nvm->opcode_bits);
470 e1000_shift_out_eec_bits(hw, (u16)(offset*2), nvm->address_bits);
473 * Read the data. SPI NVMs increment the address with each byte
474 * read and will roll over if reading beyond the end. This allows
475 * us to read the whole NVM from any offset
477 for (i = 0; i < words; i++) {
478 word_in = e1000_shift_in_eec_bits(hw, 16);
479 data[i] = (word_in >> 8) | (word_in << 8);
482 release:
483 nvm->ops.release(hw);
485 out:
486 return ret_val;
490 * e1000_read_nvm_microwire - Reads EEPROM's using microwire
491 * @hw: pointer to the HW structure
492 * @offset: offset of word in the EEPROM to read
493 * @words: number of words to read
494 * @data: word read from the EEPROM
496 * Reads a 16 bit word from the EEPROM.
498 s32 e1000_read_nvm_microwire(struct e1000_hw *hw, u16 offset, u16 words,
499 u16 *data)
501 struct e1000_nvm_info *nvm = &hw->nvm;
502 u32 i = 0;
503 s32 ret_val;
504 u8 read_opcode = NVM_READ_OPCODE_MICROWIRE;
506 DEBUGFUNC("e1000_read_nvm_microwire");
509 * A check for invalid values: offset too large, too many words,
510 * and not enough words.
512 if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
513 (words == 0)) {
514 DEBUGOUT("nvm parameter(s) out of bounds\n");
515 ret_val = -E1000_ERR_NVM;
516 goto out;
519 ret_val = nvm->ops.acquire(hw);
520 if (ret_val)
521 goto out;
523 ret_val = e1000_ready_nvm_eeprom(hw);
524 if (ret_val)
525 goto release;
527 for (i = 0; i < words; i++) {
528 /* Send the READ command (opcode + addr) */
529 e1000_shift_out_eec_bits(hw, read_opcode, nvm->opcode_bits);
530 e1000_shift_out_eec_bits(hw, (u16)(offset + i),
531 nvm->address_bits);
534 * Read the data. For microwire, each word requires the
535 * overhead of setup and tear-down.
537 data[i] = e1000_shift_in_eec_bits(hw, 16);
538 e1000_standby_nvm(hw);
541 release:
542 nvm->ops.release(hw);
544 out:
545 return ret_val;
549 * e1000_read_nvm_eerd - Reads EEPROM using EERD register
550 * @hw: pointer to the HW structure
551 * @offset: offset of word in the EEPROM to read
552 * @words: number of words to read
553 * @data: word read from the EEPROM
555 * Reads a 16 bit word from the EEPROM using the EERD register.
557 s32 e1000_read_nvm_eerd(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
559 struct e1000_nvm_info *nvm = &hw->nvm;
560 u32 i, eerd = 0;
561 s32 ret_val = E1000_SUCCESS;
563 DEBUGFUNC("e1000_read_nvm_eerd");
566 * A check for invalid values: offset too large, too many words,
567 * too many words for the offset, and not enough words.
569 if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
570 (words == 0)) {
571 DEBUGOUT("nvm parameter(s) out of bounds\n");
572 ret_val = -E1000_ERR_NVM;
573 goto out;
576 for (i = 0; i < words; i++) {
577 eerd = ((offset+i) << E1000_NVM_RW_ADDR_SHIFT) +
578 E1000_NVM_RW_REG_START;
580 E1000_WRITE_REG(hw, E1000_EERD, eerd);
581 ret_val = e1000_poll_eerd_eewr_done(hw, E1000_NVM_POLL_READ);
582 if (ret_val)
583 break;
585 data[i] = (E1000_READ_REG(hw, E1000_EERD) >>
586 E1000_NVM_RW_REG_DATA);
589 out:
590 return ret_val;
594 * e1000_write_nvm_spi - Write to EEPROM using SPI
595 * @hw: pointer to the HW structure
596 * @offset: offset within the EEPROM to be written to
597 * @words: number of words to write
598 * @data: 16 bit word(s) to be written to the EEPROM
600 * Writes data to EEPROM at offset using SPI interface.
602 * If e1000_update_nvm_checksum is not called after this function , the
603 * EEPROM will most likely contain an invalid checksum.
605 s32 e1000_write_nvm_spi(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
607 struct e1000_nvm_info *nvm = &hw->nvm;
608 s32 ret_val;
609 u16 widx = 0;
611 DEBUGFUNC("e1000_write_nvm_spi");
614 * A check for invalid values: offset too large, too many words,
615 * and not enough words.
617 if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
618 (words == 0)) {
619 DEBUGOUT("nvm parameter(s) out of bounds\n");
620 ret_val = -E1000_ERR_NVM;
621 goto out;
624 ret_val = nvm->ops.acquire(hw);
625 if (ret_val)
626 goto out;
628 msec_delay(10);
630 while (widx < words) {
631 u8 write_opcode = NVM_WRITE_OPCODE_SPI;
633 ret_val = e1000_ready_nvm_eeprom(hw);
634 if (ret_val)
635 goto release;
637 e1000_standby_nvm(hw);
639 /* Send the WRITE ENABLE command (8 bit opcode) */
640 e1000_shift_out_eec_bits(hw, NVM_WREN_OPCODE_SPI,
641 nvm->opcode_bits);
643 e1000_standby_nvm(hw);
646 * Some SPI eeproms use the 8th address bit embedded in the
647 * opcode
649 if ((nvm->address_bits == 8) && (offset >= 128))
650 write_opcode |= NVM_A8_OPCODE_SPI;
652 /* Send the Write command (8-bit opcode + addr) */
653 e1000_shift_out_eec_bits(hw, write_opcode, nvm->opcode_bits);
654 e1000_shift_out_eec_bits(hw, (u16)((offset + widx) * 2),
655 nvm->address_bits);
657 /* Loop to allow for up to whole page write of eeprom */
658 while (widx < words) {
659 u16 word_out = data[widx];
660 word_out = (word_out >> 8) | (word_out << 8);
661 e1000_shift_out_eec_bits(hw, word_out, 16);
662 widx++;
664 if ((((offset + widx) * 2) % nvm->page_size) == 0) {
665 e1000_standby_nvm(hw);
666 break;
671 msec_delay(10);
672 release:
673 nvm->ops.release(hw);
675 out:
676 return ret_val;
680 * e1000_write_nvm_microwire - Writes EEPROM using microwire
681 * @hw: pointer to the HW structure
682 * @offset: offset within the EEPROM to be written to
683 * @words: number of words to write
684 * @data: 16 bit word(s) to be written to the EEPROM
686 * Writes data to EEPROM at offset using microwire interface.
688 * If e1000_update_nvm_checksum is not called after this function , the
689 * EEPROM will most likely contain an invalid checksum.
691 s32 e1000_write_nvm_microwire(struct e1000_hw *hw, u16 offset, u16 words,
692 u16 *data)
694 struct e1000_nvm_info *nvm = &hw->nvm;
695 s32 ret_val;
696 u32 eecd;
697 u16 words_written = 0;
698 u16 widx = 0;
700 DEBUGFUNC("e1000_write_nvm_microwire");
703 * A check for invalid values: offset too large, too many words,
704 * and not enough words.
706 if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
707 (words == 0)) {
708 DEBUGOUT("nvm parameter(s) out of bounds\n");
709 ret_val = -E1000_ERR_NVM;
710 goto out;
713 ret_val = nvm->ops.acquire(hw);
714 if (ret_val)
715 goto out;
717 ret_val = e1000_ready_nvm_eeprom(hw);
718 if (ret_val)
719 goto release;
721 e1000_shift_out_eec_bits(hw, NVM_EWEN_OPCODE_MICROWIRE,
722 (u16)(nvm->opcode_bits + 2));
724 e1000_shift_out_eec_bits(hw, 0, (u16)(nvm->address_bits - 2));
726 e1000_standby_nvm(hw);
728 while (words_written < words) {
729 e1000_shift_out_eec_bits(hw, NVM_WRITE_OPCODE_MICROWIRE,
730 nvm->opcode_bits);
732 e1000_shift_out_eec_bits(hw, (u16)(offset + words_written),
733 nvm->address_bits);
735 e1000_shift_out_eec_bits(hw, data[words_written], 16);
737 e1000_standby_nvm(hw);
739 for (widx = 0; widx < 200; widx++) {
740 eecd = E1000_READ_REG(hw, E1000_EECD);
741 if (eecd & E1000_EECD_DO)
742 break;
743 usec_delay(50);
746 if (widx == 200) {
747 DEBUGOUT("NVM Write did not complete\n");
748 ret_val = -E1000_ERR_NVM;
749 goto release;
752 e1000_standby_nvm(hw);
754 words_written++;
757 e1000_shift_out_eec_bits(hw, NVM_EWDS_OPCODE_MICROWIRE,
758 (u16)(nvm->opcode_bits + 2));
760 e1000_shift_out_eec_bits(hw, 0, (u16)(nvm->address_bits - 2));
762 release:
763 nvm->ops.release(hw);
765 out:
766 return ret_val;
770 * e1000_read_pba_num_generic - Read device part number
771 * @hw: pointer to the HW structure
772 * @pba_num: pointer to device part number
774 * Reads the product board assembly (PBA) number from the EEPROM and stores
775 * the value in pba_num.
777 s32 e1000_read_pba_num_generic(struct e1000_hw *hw, u32 *pba_num)
779 s32 ret_val;
780 u16 nvm_data;
782 DEBUGFUNC("e1000_read_pba_num_generic");
784 ret_val = hw->nvm.ops.read(hw, NVM_PBA_OFFSET_0, 1, &nvm_data);
785 if (ret_val) {
786 DEBUGOUT("NVM Read Error\n");
787 goto out;
789 *pba_num = (u32)(nvm_data << 16);
791 ret_val = hw->nvm.ops.read(hw, NVM_PBA_OFFSET_1, 1, &nvm_data);
792 if (ret_val) {
793 DEBUGOUT("NVM Read Error\n");
794 goto out;
796 *pba_num |= nvm_data;
798 out:
799 return ret_val;
803 * e1000_read_mac_addr_generic - Read device MAC address
804 * @hw: pointer to the HW structure
806 * Reads the device MAC address from the EEPROM and stores the value.
807 * Since devices with two ports use the same EEPROM, we increment the
808 * last bit in the MAC address for the second port.
810 s32 e1000_read_mac_addr_generic(struct e1000_hw *hw)
812 s32 ret_val = E1000_SUCCESS;
813 u16 offset, nvm_data, i;
815 DEBUGFUNC("e1000_read_mac_addr");
817 for (i = 0; i < ETH_ADDR_LEN; i += 2) {
818 offset = i >> 1;
819 ret_val = hw->nvm.ops.read(hw, offset, 1, &nvm_data);
820 if (ret_val) {
821 DEBUGOUT("NVM Read Error\n");
822 goto out;
824 hw->mac.perm_addr[i] = (u8)(nvm_data & 0xFF);
825 hw->mac.perm_addr[i+1] = (u8)(nvm_data >> 8);
828 /* Flip last bit of mac address if we're on second port */
829 if (hw->bus.func == E1000_FUNC_1)
830 hw->mac.perm_addr[5] ^= 1;
832 for (i = 0; i < ETH_ADDR_LEN; i++)
833 hw->mac.addr[i] = hw->mac.perm_addr[i];
835 out:
836 return ret_val;
840 * e1000_validate_nvm_checksum_generic - Validate EEPROM checksum
841 * @hw: pointer to the HW structure
843 * Calculates the EEPROM checksum by reading/adding each word of the EEPROM
844 * and then verifies that the sum of the EEPROM is equal to 0xBABA.
846 s32 e1000_validate_nvm_checksum_generic(struct e1000_hw *hw)
848 s32 ret_val = E1000_SUCCESS;
849 u16 checksum = 0;
850 u16 i, nvm_data;
852 DEBUGFUNC("e1000_validate_nvm_checksum_generic");
854 for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
855 ret_val = hw->nvm.ops.read(hw, i, 1, &nvm_data);
856 if (ret_val) {
857 DEBUGOUT("NVM Read Error\n");
858 goto out;
860 checksum += nvm_data;
863 if (checksum != (u16) NVM_SUM) {
864 DEBUGOUT("NVM Checksum Invalid\n");
865 ret_val = -E1000_ERR_NVM;
866 goto out;
869 out:
870 return ret_val;
874 * e1000_update_nvm_checksum_generic - Update EEPROM checksum
875 * @hw: pointer to the HW structure
877 * Updates the EEPROM checksum by reading/adding each word of the EEPROM
878 * up to the checksum. Then calculates the EEPROM checksum and writes the
879 * value to the EEPROM.
881 s32 e1000_update_nvm_checksum_generic(struct e1000_hw *hw)
883 s32 ret_val;
884 u16 checksum = 0;
885 u16 i, nvm_data;
887 DEBUGFUNC("e1000_update_nvm_checksum");
889 for (i = 0; i < NVM_CHECKSUM_REG; i++) {
890 ret_val = hw->nvm.ops.read(hw, i, 1, &nvm_data);
891 if (ret_val) {
892 DEBUGOUT("NVM Read Error while updating checksum.\n");
893 goto out;
895 checksum += nvm_data;
897 checksum = (u16) NVM_SUM - checksum;
898 ret_val = hw->nvm.ops.write(hw, NVM_CHECKSUM_REG, 1, &checksum);
899 if (ret_val) {
900 DEBUGOUT("NVM Write Error while updating checksum.\n");
903 out:
904 return ret_val;
908 * e1000_reload_nvm_generic - Reloads EEPROM
909 * @hw: pointer to the HW structure
911 * Reloads the EEPROM by setting the "Reinitialize from EEPROM" bit in the
912 * extended control register.
914 void e1000_reload_nvm_generic(struct e1000_hw *hw)
916 u32 ctrl_ext;
918 DEBUGFUNC("e1000_reload_nvm_generic");
920 usec_delay(10);
921 ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
922 ctrl_ext |= E1000_CTRL_EXT_EE_RST;
923 E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
924 E1000_WRITE_FLUSH(hw);