[igb] Add igb driver
[gpxe.git] / src / drivers / net / igb / igb_nvm.c
blob1bad567cf14ffdc4ae6d5ce54c62dfd5326954e6
1 /*******************************************************************************
3 Intel(R) Gigabit Ethernet Linux driver
4 Copyright(c) 2007-2009 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 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 *******************************************************************************/
28 FILE_LICENCE ( GPL2_ONLY );
30 #include "igb.h"
32 static void igb_stop_nvm(struct e1000_hw *hw);
33 static void igb_reload_nvm_generic(struct e1000_hw *hw);
35 /**
36 * igb_init_nvm_ops_generic - Initialize NVM function pointers
37 * @hw: pointer to the HW structure
39 * Setups up the function pointers to no-op functions
40 **/
41 void igb_init_nvm_ops_generic(struct e1000_hw *hw)
43 struct e1000_nvm_info *nvm = &hw->nvm;
44 DEBUGFUNC("igb_init_nvm_ops_generic");
46 /* Initialize function pointers */
47 nvm->ops.reload = igb_reload_nvm_generic;
50 /**
51 * igb_raise_eec_clk - Raise EEPROM clock
52 * @hw: pointer to the HW structure
53 * @eecd: pointer to the EEPROM
55 * Enable/Raise the EEPROM clock bit.
56 **/
57 static void igb_raise_eec_clk(struct e1000_hw *hw, u32 *eecd)
59 *eecd = *eecd | E1000_EECD_SK;
60 E1000_WRITE_REG(hw, E1000_EECD, *eecd);
61 E1000_WRITE_FLUSH(hw);
62 usec_delay(hw->nvm.delay_usec);
65 /**
66 * igb_lower_eec_clk - Lower EEPROM clock
67 * @hw: pointer to the HW structure
68 * @eecd: pointer to the EEPROM
70 * Clear/Lower the EEPROM clock bit.
71 **/
72 static void igb_lower_eec_clk(struct e1000_hw *hw, u32 *eecd)
74 *eecd = *eecd & ~E1000_EECD_SK;
75 E1000_WRITE_REG(hw, E1000_EECD, *eecd);
76 E1000_WRITE_FLUSH(hw);
77 usec_delay(hw->nvm.delay_usec);
80 /**
81 * igb_shift_out_eec_bits - Shift data bits our to the EEPROM
82 * @hw: pointer to the HW structure
83 * @data: data to send to the EEPROM
84 * @count: number of bits to shift out
86 * We need to shift 'count' bits out to the EEPROM. So, the value in the
87 * "data" parameter will be shifted out to the EEPROM one bit at a time.
88 * In order to do this, "data" must be broken down into bits.
89 **/
90 static void igb_shift_out_eec_bits(struct e1000_hw *hw, u16 data, u16 count)
92 struct e1000_nvm_info *nvm = &hw->nvm;
93 u32 eecd = E1000_READ_REG(hw, E1000_EECD);
94 u32 mask;
96 DEBUGFUNC("igb_shift_out_eec_bits");
98 mask = 0x01 << (count - 1);
99 if (nvm->type == e1000_nvm_eeprom_spi)
100 eecd |= E1000_EECD_DO;
102 do {
103 eecd &= ~E1000_EECD_DI;
105 if (data & mask)
106 eecd |= E1000_EECD_DI;
108 E1000_WRITE_REG(hw, E1000_EECD, eecd);
109 E1000_WRITE_FLUSH(hw);
111 usec_delay(nvm->delay_usec);
113 igb_raise_eec_clk(hw, &eecd);
114 igb_lower_eec_clk(hw, &eecd);
116 mask >>= 1;
117 } while (mask);
119 eecd &= ~E1000_EECD_DI;
120 E1000_WRITE_REG(hw, E1000_EECD, eecd);
124 * igb_shift_in_eec_bits - Shift data bits in from the EEPROM
125 * @hw: pointer to the HW structure
126 * @count: number of bits to shift in
128 * In order to read a register from the EEPROM, we need to shift 'count' bits
129 * in from the EEPROM. Bits are "shifted in" by raising the clock input to
130 * the EEPROM (setting the SK bit), and then reading the value of the data out
131 * "DO" bit. During this "shifting in" process the data in "DI" bit should
132 * always be clear.
134 static u16 igb_shift_in_eec_bits(struct e1000_hw *hw, u16 count)
136 u32 eecd;
137 u32 i;
138 u16 data;
140 DEBUGFUNC("igb_shift_in_eec_bits");
142 eecd = E1000_READ_REG(hw, E1000_EECD);
144 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
145 data = 0;
147 for (i = 0; i < count; i++) {
148 data <<= 1;
149 igb_raise_eec_clk(hw, &eecd);
151 eecd = E1000_READ_REG(hw, E1000_EECD);
153 eecd &= ~E1000_EECD_DI;
154 if (eecd & E1000_EECD_DO)
155 data |= 1;
157 igb_lower_eec_clk(hw, &eecd);
160 return data;
164 * igb_poll_eerd_eewr_done - Poll for EEPROM read/write completion
165 * @hw: pointer to the HW structure
166 * @ee_reg: EEPROM flag for polling
168 * Polls the EEPROM status bit for either read or write completion based
169 * upon the value of 'ee_reg'.
171 s32 igb_poll_eerd_eewr_done(struct e1000_hw *hw, int ee_reg)
173 u32 attempts = 100000;
174 u32 i, reg = 0;
175 s32 ret_val = -E1000_ERR_NVM;
177 DEBUGFUNC("igb_poll_eerd_eewr_done");
179 for (i = 0; i < attempts; i++) {
180 if (ee_reg == E1000_NVM_POLL_READ)
181 reg = E1000_READ_REG(hw, E1000_EERD);
182 else
183 reg = E1000_READ_REG(hw, E1000_EEWR);
185 if (reg & E1000_NVM_RW_REG_DONE) {
186 ret_val = E1000_SUCCESS;
187 break;
190 usec_delay(5);
193 return ret_val;
197 * igb_acquire_nvm_generic - Generic request for access to EEPROM
198 * @hw: pointer to the HW structure
200 * Set the EEPROM access request bit and wait for EEPROM access grant bit.
201 * Return successful if access grant bit set, else clear the request for
202 * EEPROM access and return -E1000_ERR_NVM (-1).
204 s32 igb_acquire_nvm_generic(struct e1000_hw *hw)
206 u32 eecd = E1000_READ_REG(hw, E1000_EECD);
207 s32 timeout = E1000_NVM_GRANT_ATTEMPTS;
208 s32 ret_val = E1000_SUCCESS;
210 DEBUGFUNC("igb_acquire_nvm_generic");
212 E1000_WRITE_REG(hw, E1000_EECD, eecd | E1000_EECD_REQ);
213 eecd = E1000_READ_REG(hw, E1000_EECD);
215 while (timeout) {
216 if (eecd & E1000_EECD_GNT)
217 break;
218 usec_delay(5);
219 eecd = E1000_READ_REG(hw, E1000_EECD);
220 timeout--;
223 if (!timeout) {
224 eecd &= ~E1000_EECD_REQ;
225 E1000_WRITE_REG(hw, E1000_EECD, eecd);
226 DEBUGOUT("Could not acquire NVM grant\n");
227 ret_val = -E1000_ERR_NVM;
230 return ret_val;
234 * igb_standby_nvm - Return EEPROM to standby state
235 * @hw: pointer to the HW structure
237 * Return the EEPROM to a standby state.
239 static void igb_standby_nvm(struct e1000_hw *hw)
241 struct e1000_nvm_info *nvm = &hw->nvm;
242 u32 eecd = E1000_READ_REG(hw, E1000_EECD);
244 DEBUGFUNC("igb_standby_nvm");
246 if (nvm->type == e1000_nvm_eeprom_spi) {
247 /* Toggle CS to flush commands */
248 eecd |= E1000_EECD_CS;
249 E1000_WRITE_REG(hw, E1000_EECD, eecd);
250 E1000_WRITE_FLUSH(hw);
251 usec_delay(nvm->delay_usec);
252 eecd &= ~E1000_EECD_CS;
253 E1000_WRITE_REG(hw, E1000_EECD, eecd);
254 E1000_WRITE_FLUSH(hw);
255 usec_delay(nvm->delay_usec);
260 * igb_stop_nvm - Terminate EEPROM command
261 * @hw: pointer to the HW structure
263 * Terminates the current command by inverting the EEPROM's chip select pin.
265 static void igb_stop_nvm(struct e1000_hw *hw)
267 u32 eecd;
269 DEBUGFUNC("igb_stop_nvm");
271 eecd = E1000_READ_REG(hw, E1000_EECD);
272 if (hw->nvm.type == e1000_nvm_eeprom_spi) {
273 /* Pull CS high */
274 eecd |= E1000_EECD_CS;
275 igb_lower_eec_clk(hw, &eecd);
280 * igb_release_nvm_generic - Release exclusive access to EEPROM
281 * @hw: pointer to the HW structure
283 * Stop any current commands to the EEPROM and clear the EEPROM request bit.
285 void igb_release_nvm_generic(struct e1000_hw *hw)
287 u32 eecd;
289 DEBUGFUNC("igb_release_nvm_generic");
291 igb_stop_nvm(hw);
293 eecd = E1000_READ_REG(hw, E1000_EECD);
294 eecd &= ~E1000_EECD_REQ;
295 E1000_WRITE_REG(hw, E1000_EECD, eecd);
299 * igb_ready_nvm_eeprom - Prepares EEPROM for read/write
300 * @hw: pointer to the HW structure
302 * Setups the EEPROM for reading and writing.
304 static s32 igb_ready_nvm_eeprom(struct e1000_hw *hw)
306 struct e1000_nvm_info *nvm = &hw->nvm;
307 u32 eecd = E1000_READ_REG(hw, E1000_EECD);
308 s32 ret_val = E1000_SUCCESS;
309 u16 timeout = 0;
310 u8 spi_stat_reg;
312 DEBUGFUNC("igb_ready_nvm_eeprom");
314 if (nvm->type == e1000_nvm_eeprom_spi) {
315 /* Clear SK and CS */
316 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
317 E1000_WRITE_REG(hw, E1000_EECD, eecd);
318 usec_delay(1);
319 timeout = NVM_MAX_RETRY_SPI;
322 * Read "Status Register" repeatedly until the LSB is cleared.
323 * The EEPROM will signal that the command has been completed
324 * by clearing bit 0 of the internal status register. If it's
325 * not cleared within 'timeout', then error out.
327 while (timeout) {
328 igb_shift_out_eec_bits(hw, NVM_RDSR_OPCODE_SPI,
329 hw->nvm.opcode_bits);
330 spi_stat_reg = (u8)igb_shift_in_eec_bits(hw, 8);
331 if (!(spi_stat_reg & NVM_STATUS_RDY_SPI))
332 break;
334 usec_delay(5);
335 igb_standby_nvm(hw);
336 timeout--;
339 if (!timeout) {
340 DEBUGOUT("SPI NVM Status error\n");
341 ret_val = -E1000_ERR_NVM;
342 goto out;
346 out:
347 return ret_val;
351 * igb_read_nvm_eerd - Reads EEPROM using EERD register
352 * @hw: pointer to the HW structure
353 * @offset: offset of word in the EEPROM to read
354 * @words: number of words to read
355 * @data: word read from the EEPROM
357 * Reads a 16 bit word from the EEPROM using the EERD register.
359 s32 igb_read_nvm_eerd(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
361 struct e1000_nvm_info *nvm = &hw->nvm;
362 u32 i, eerd = 0;
363 s32 ret_val = E1000_SUCCESS;
365 DEBUGFUNC("igb_read_nvm_eerd");
368 * A check for invalid values: offset too large, too many words,
369 * too many words for the offset, and not enough words.
371 if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
372 (words == 0)) {
373 DEBUGOUT("nvm parameter(s) out of bounds\n");
374 ret_val = -E1000_ERR_NVM;
375 goto out;
378 for (i = 0; i < words; i++) {
379 eerd = ((offset+i) << E1000_NVM_RW_ADDR_SHIFT) +
380 E1000_NVM_RW_REG_START;
382 E1000_WRITE_REG(hw, E1000_EERD, eerd);
383 ret_val = igb_poll_eerd_eewr_done(hw, E1000_NVM_POLL_READ);
384 if (ret_val)
385 break;
387 data[i] = (E1000_READ_REG(hw, E1000_EERD) >>
388 E1000_NVM_RW_REG_DATA);
391 out:
392 return ret_val;
396 * igb_write_nvm_spi - Write to EEPROM using SPI
397 * @hw: pointer to the HW structure
398 * @offset: offset within the EEPROM to be written to
399 * @words: number of words to write
400 * @data: 16 bit word(s) to be written to the EEPROM
402 * Writes data to EEPROM at offset using SPI interface.
404 * If e1000_update_nvm_checksum is not called after this function , the
405 * EEPROM will most likely contain an invalid checksum.
407 s32 igb_write_nvm_spi(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
409 struct e1000_nvm_info *nvm = &hw->nvm;
410 s32 ret_val;
411 u16 widx = 0;
413 DEBUGFUNC("igb_write_nvm_spi");
416 * A check for invalid values: offset too large, too many words,
417 * and not enough words.
419 if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
420 (words == 0)) {
421 DEBUGOUT("nvm parameter(s) out of bounds\n");
422 ret_val = -E1000_ERR_NVM;
423 goto out;
426 ret_val = nvm->ops.acquire(hw);
427 if (ret_val)
428 goto out;
430 while (widx < words) {
431 u8 write_opcode = NVM_WRITE_OPCODE_SPI;
433 ret_val = igb_ready_nvm_eeprom(hw);
434 if (ret_val)
435 goto release;
437 igb_standby_nvm(hw);
439 /* Send the WRITE ENABLE command (8 bit opcode) */
440 igb_shift_out_eec_bits(hw, NVM_WREN_OPCODE_SPI,
441 nvm->opcode_bits);
443 igb_standby_nvm(hw);
446 * Some SPI eeproms use the 8th address bit embedded in the
447 * opcode
449 if ((nvm->address_bits == 8) && (offset >= 128))
450 write_opcode |= NVM_A8_OPCODE_SPI;
452 /* Send the Write command (8-bit opcode + addr) */
453 igb_shift_out_eec_bits(hw, write_opcode, nvm->opcode_bits);
454 igb_shift_out_eec_bits(hw, (u16)((offset + widx) * 2),
455 nvm->address_bits);
457 /* Loop to allow for up to whole page write of eeprom */
458 while (widx < words) {
459 u16 word_out = data[widx];
460 word_out = (word_out >> 8) | (word_out << 8);
461 igb_shift_out_eec_bits(hw, word_out, 16);
462 widx++;
464 if ((((offset + widx) * 2) % nvm->page_size) == 0) {
465 igb_standby_nvm(hw);
466 break;
471 msec_delay(10);
472 release:
473 nvm->ops.release(hw);
475 out:
476 return ret_val;
480 * igb_read_pba_num_generic - Read device part number
481 * @hw: pointer to the HW structure
482 * @pba_num: pointer to device part number
484 * Reads the product board assembly (PBA) number from the EEPROM and stores
485 * the value in pba_num.
487 s32 igb_read_pba_num_generic(struct e1000_hw *hw, u32 *pba_num)
489 s32 ret_val;
490 u16 nvm_data;
492 DEBUGFUNC("igb_read_pba_num_generic");
494 ret_val = hw->nvm.ops.read(hw, NVM_PBA_OFFSET_0, 1, &nvm_data);
495 if (ret_val) {
496 DEBUGOUT("NVM Read Error\n");
497 goto out;
499 *pba_num = (u32)(nvm_data << 16);
501 ret_val = hw->nvm.ops.read(hw, NVM_PBA_OFFSET_1, 1, &nvm_data);
502 if (ret_val) {
503 DEBUGOUT("NVM Read Error\n");
504 goto out;
506 *pba_num |= nvm_data;
508 out:
509 return ret_val;
513 * igb_read_mac_addr_generic - Read device MAC address
514 * @hw: pointer to the HW structure
516 * Reads the device MAC address from the EEPROM and stores the value.
517 * Since devices with two ports use the same EEPROM, we increment the
518 * last bit in the MAC address for the second port.
520 s32 igb_read_mac_addr_generic(struct e1000_hw *hw)
522 u32 rar_high;
523 u32 rar_low;
524 u16 i;
526 rar_high = E1000_READ_REG(hw, E1000_RAH(0));
527 rar_low = E1000_READ_REG(hw, E1000_RAL(0));
529 for (i = 0; i < E1000_RAL_MAC_ADDR_LEN; i++)
530 hw->mac.perm_addr[i] = (u8)(rar_low >> (i*8));
532 for (i = 0; i < E1000_RAH_MAC_ADDR_LEN; i++)
533 hw->mac.perm_addr[i+4] = (u8)(rar_high >> (i*8));
535 for (i = 0; i < ETH_ADDR_LEN; i++)
536 hw->mac.addr[i] = hw->mac.perm_addr[i];
538 return E1000_SUCCESS;
542 * igb_validate_nvm_checksum_generic - Validate EEPROM checksum
543 * @hw: pointer to the HW structure
545 * Calculates the EEPROM checksum by reading/adding each word of the EEPROM
546 * and then verifies that the sum of the EEPROM is equal to 0xBABA.
548 s32 igb_validate_nvm_checksum_generic(struct e1000_hw *hw)
550 s32 ret_val = E1000_SUCCESS;
551 u16 checksum = 0;
552 u16 i, nvm_data;
554 DEBUGFUNC("igb_validate_nvm_checksum_generic");
556 for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
557 ret_val = hw->nvm.ops.read(hw, i, 1, &nvm_data);
558 if (ret_val) {
559 DEBUGOUT("NVM Read Error\n");
560 goto out;
562 checksum += nvm_data;
565 if (checksum != (u16) NVM_SUM) {
566 DEBUGOUT("NVM Checksum Invalid\n");
567 ret_val = -E1000_ERR_NVM;
568 goto out;
571 out:
572 return ret_val;
576 * igb_update_nvm_checksum_generic - Update EEPROM checksum
577 * @hw: pointer to the HW structure
579 * Updates the EEPROM checksum by reading/adding each word of the EEPROM
580 * up to the checksum. Then calculates the EEPROM checksum and writes the
581 * value to the EEPROM.
583 s32 igb_update_nvm_checksum_generic(struct e1000_hw *hw)
585 s32 ret_val;
586 u16 checksum = 0;
587 u16 i, nvm_data;
589 DEBUGFUNC("igb_update_nvm_checksum");
591 for (i = 0; i < NVM_CHECKSUM_REG; i++) {
592 ret_val = hw->nvm.ops.read(hw, i, 1, &nvm_data);
593 if (ret_val) {
594 DEBUGOUT("NVM Read Error while updating checksum.\n");
595 goto out;
597 checksum += nvm_data;
599 checksum = (u16) NVM_SUM - checksum;
600 ret_val = hw->nvm.ops.write(hw, NVM_CHECKSUM_REG, 1, &checksum);
601 if (ret_val) {
602 DEBUGOUT("NVM Write Error while updating checksum.\n");
604 out:
605 return ret_val;
609 * igb_reload_nvm_generic - Reloads EEPROM
610 * @hw: pointer to the HW structure
612 * Reloads the EEPROM by setting the "Reinitialize from EEPROM" bit in the
613 * extended control register.
615 static void igb_reload_nvm_generic(struct e1000_hw *hw)
617 u32 ctrl_ext;
619 DEBUGFUNC("igb_reload_nvm_generic");
621 usec_delay(10);
622 ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
623 ctrl_ext |= E1000_CTRL_EXT_EE_RST;
624 E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
625 E1000_WRITE_FLUSH(hw);