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
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".
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 #include <linux/if_ether.h>
29 #include <linux/delay.h>
31 #include "e1000_mac.h"
32 #include "e1000_nvm.h"
35 * igb_raise_eec_clk - Raise EEPROM clock
36 * @hw: pointer to the HW structure
37 * @eecd: pointer to the EEPROM
39 * Enable/Raise the EEPROM clock bit.
41 static void igb_raise_eec_clk(struct e1000_hw
*hw
, u32
*eecd
)
43 *eecd
= *eecd
| E1000_EECD_SK
;
44 wr32(E1000_EECD
, *eecd
);
46 udelay(hw
->nvm
.delay_usec
);
50 * igb_lower_eec_clk - Lower EEPROM clock
51 * @hw: pointer to the HW structure
52 * @eecd: pointer to the EEPROM
54 * Clear/Lower the EEPROM clock bit.
56 static void igb_lower_eec_clk(struct e1000_hw
*hw
, u32
*eecd
)
58 *eecd
= *eecd
& ~E1000_EECD_SK
;
59 wr32(E1000_EECD
, *eecd
);
61 udelay(hw
->nvm
.delay_usec
);
65 * igb_shift_out_eec_bits - Shift data bits our to the EEPROM
66 * @hw: pointer to the HW structure
67 * @data: data to send to the EEPROM
68 * @count: number of bits to shift out
70 * We need to shift 'count' bits out to the EEPROM. So, the value in the
71 * "data" parameter will be shifted out to the EEPROM one bit at a time.
72 * In order to do this, "data" must be broken down into bits.
74 static void igb_shift_out_eec_bits(struct e1000_hw
*hw
, u16 data
, u16 count
)
76 struct e1000_nvm_info
*nvm
= &hw
->nvm
;
77 u32 eecd
= rd32(E1000_EECD
);
80 mask
= 0x01 << (count
- 1);
81 if (nvm
->type
== e1000_nvm_eeprom_microwire
)
82 eecd
&= ~E1000_EECD_DO
;
83 else if (nvm
->type
== e1000_nvm_eeprom_spi
)
84 eecd
|= E1000_EECD_DO
;
87 eecd
&= ~E1000_EECD_DI
;
90 eecd
|= E1000_EECD_DI
;
92 wr32(E1000_EECD
, eecd
);
95 udelay(nvm
->delay_usec
);
97 igb_raise_eec_clk(hw
, &eecd
);
98 igb_lower_eec_clk(hw
, &eecd
);
103 eecd
&= ~E1000_EECD_DI
;
104 wr32(E1000_EECD
, eecd
);
108 * igb_shift_in_eec_bits - Shift data bits in from the EEPROM
109 * @hw: pointer to the HW structure
110 * @count: number of bits to shift in
112 * In order to read a register from the EEPROM, we need to shift 'count' bits
113 * in from the EEPROM. Bits are "shifted in" by raising the clock input to
114 * the EEPROM (setting the SK bit), and then reading the value of the data out
115 * "DO" bit. During this "shifting in" process the data in "DI" bit should
118 static u16
igb_shift_in_eec_bits(struct e1000_hw
*hw
, u16 count
)
124 eecd
= rd32(E1000_EECD
);
126 eecd
&= ~(E1000_EECD_DO
| E1000_EECD_DI
);
129 for (i
= 0; i
< count
; i
++) {
131 igb_raise_eec_clk(hw
, &eecd
);
133 eecd
= rd32(E1000_EECD
);
135 eecd
&= ~E1000_EECD_DI
;
136 if (eecd
& E1000_EECD_DO
)
139 igb_lower_eec_clk(hw
, &eecd
);
146 * igb_poll_eerd_eewr_done - Poll for EEPROM read/write completion
147 * @hw: pointer to the HW structure
148 * @ee_reg: EEPROM flag for polling
150 * Polls the EEPROM status bit for either read or write completion based
151 * upon the value of 'ee_reg'.
153 static s32
igb_poll_eerd_eewr_done(struct e1000_hw
*hw
, int ee_reg
)
155 u32 attempts
= 100000;
157 s32 ret_val
= -E1000_ERR_NVM
;
159 for (i
= 0; i
< attempts
; i
++) {
160 if (ee_reg
== E1000_NVM_POLL_READ
)
161 reg
= rd32(E1000_EERD
);
163 reg
= rd32(E1000_EEWR
);
165 if (reg
& E1000_NVM_RW_REG_DONE
) {
177 * igb_acquire_nvm - Generic request for access to EEPROM
178 * @hw: pointer to the HW structure
180 * Set the EEPROM access request bit and wait for EEPROM access grant bit.
181 * Return successful if access grant bit set, else clear the request for
182 * EEPROM access and return -E1000_ERR_NVM (-1).
184 s32
igb_acquire_nvm(struct e1000_hw
*hw
)
186 u32 eecd
= rd32(E1000_EECD
);
187 s32 timeout
= E1000_NVM_GRANT_ATTEMPTS
;
191 wr32(E1000_EECD
, eecd
| E1000_EECD_REQ
);
192 eecd
= rd32(E1000_EECD
);
195 if (eecd
& E1000_EECD_GNT
)
198 eecd
= rd32(E1000_EECD
);
203 eecd
&= ~E1000_EECD_REQ
;
204 wr32(E1000_EECD
, eecd
);
205 hw_dbg("Could not acquire NVM grant\n");
206 ret_val
= -E1000_ERR_NVM
;
213 * igb_standby_nvm - Return EEPROM to standby state
214 * @hw: pointer to the HW structure
216 * Return the EEPROM to a standby state.
218 static void igb_standby_nvm(struct e1000_hw
*hw
)
220 struct e1000_nvm_info
*nvm
= &hw
->nvm
;
221 u32 eecd
= rd32(E1000_EECD
);
223 if (nvm
->type
== e1000_nvm_eeprom_microwire
) {
224 eecd
&= ~(E1000_EECD_CS
| E1000_EECD_SK
);
225 wr32(E1000_EECD
, eecd
);
227 udelay(nvm
->delay_usec
);
229 igb_raise_eec_clk(hw
, &eecd
);
232 eecd
|= E1000_EECD_CS
;
233 wr32(E1000_EECD
, eecd
);
235 udelay(nvm
->delay_usec
);
237 igb_lower_eec_clk(hw
, &eecd
);
238 } else if (nvm
->type
== e1000_nvm_eeprom_spi
) {
239 /* Toggle CS to flush commands */
240 eecd
|= E1000_EECD_CS
;
241 wr32(E1000_EECD
, eecd
);
243 udelay(nvm
->delay_usec
);
244 eecd
&= ~E1000_EECD_CS
;
245 wr32(E1000_EECD
, eecd
);
247 udelay(nvm
->delay_usec
);
252 * e1000_stop_nvm - Terminate EEPROM command
253 * @hw: pointer to the HW structure
255 * Terminates the current command by inverting the EEPROM's chip select pin.
257 static void e1000_stop_nvm(struct e1000_hw
*hw
)
261 eecd
= rd32(E1000_EECD
);
262 if (hw
->nvm
.type
== e1000_nvm_eeprom_spi
) {
264 eecd
|= E1000_EECD_CS
;
265 igb_lower_eec_clk(hw
, &eecd
);
266 } else if (hw
->nvm
.type
== e1000_nvm_eeprom_microwire
) {
267 /* CS on Microcwire is active-high */
268 eecd
&= ~(E1000_EECD_CS
| E1000_EECD_DI
);
269 wr32(E1000_EECD
, eecd
);
270 igb_raise_eec_clk(hw
, &eecd
);
271 igb_lower_eec_clk(hw
, &eecd
);
276 * igb_release_nvm - Release exclusive access to EEPROM
277 * @hw: pointer to the HW structure
279 * Stop any current commands to the EEPROM and clear the EEPROM request bit.
281 void igb_release_nvm(struct e1000_hw
*hw
)
287 eecd
= rd32(E1000_EECD
);
288 eecd
&= ~E1000_EECD_REQ
;
289 wr32(E1000_EECD
, eecd
);
293 * igb_ready_nvm_eeprom - Prepares EEPROM for read/write
294 * @hw: pointer to the HW structure
296 * Setups the EEPROM for reading and writing.
298 static s32
igb_ready_nvm_eeprom(struct e1000_hw
*hw
)
300 struct e1000_nvm_info
*nvm
= &hw
->nvm
;
301 u32 eecd
= rd32(E1000_EECD
);
307 if (nvm
->type
== e1000_nvm_eeprom_microwire
) {
308 /* Clear SK and DI */
309 eecd
&= ~(E1000_EECD_DI
| E1000_EECD_SK
);
310 wr32(E1000_EECD
, eecd
);
312 eecd
|= E1000_EECD_CS
;
313 wr32(E1000_EECD
, eecd
);
314 } else if (nvm
->type
== e1000_nvm_eeprom_spi
) {
315 /* Clear SK and CS */
316 eecd
&= ~(E1000_EECD_CS
| E1000_EECD_SK
);
317 wr32(E1000_EECD
, eecd
);
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.
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
))
340 hw_dbg("SPI NVM Status error\n");
341 ret_val
= -E1000_ERR_NVM
;
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
;
366 * A check for invalid values: offset too large, too many words,
367 * and not enough words.
369 if ((offset
>= nvm
->word_size
) || (words
> (nvm
->word_size
- offset
)) ||
371 hw_dbg("nvm parameter(s) out of bounds\n");
372 ret_val
= -E1000_ERR_NVM
;
376 for (i
= 0; i
< words
; i
++) {
377 eerd
= ((offset
+i
) << E1000_NVM_RW_ADDR_SHIFT
) +
378 E1000_NVM_RW_REG_START
;
380 wr32(E1000_EERD
, eerd
);
381 ret_val
= igb_poll_eerd_eewr_done(hw
, E1000_NVM_POLL_READ
);
385 data
[i
] = (rd32(E1000_EERD
) >>
386 E1000_NVM_RW_REG_DATA
);
394 * igb_write_nvm_spi - Write to EEPROM using SPI
395 * @hw: pointer to the HW structure
396 * @offset: offset within the EEPROM to be written to
397 * @words: number of words to write
398 * @data: 16 bit word(s) to be written to the EEPROM
400 * Writes data to EEPROM at offset using SPI interface.
402 * If e1000_update_nvm_checksum is not called after this function , the
403 * EEPROM will most likley contain an invalid checksum.
405 s32
igb_write_nvm_spi(struct e1000_hw
*hw
, u16 offset
, u16 words
, u16
*data
)
407 struct e1000_nvm_info
*nvm
= &hw
->nvm
;
412 * A check for invalid values: offset too large, too many words,
413 * and not enough words.
415 if ((offset
>= nvm
->word_size
) || (words
> (nvm
->word_size
- offset
)) ||
417 hw_dbg("nvm parameter(s) out of bounds\n");
418 ret_val
= -E1000_ERR_NVM
;
422 ret_val
= hw
->nvm
.ops
.acquire(hw
);
428 while (widx
< words
) {
429 u8 write_opcode
= NVM_WRITE_OPCODE_SPI
;
431 ret_val
= igb_ready_nvm_eeprom(hw
);
437 /* Send the WRITE ENABLE command (8 bit opcode) */
438 igb_shift_out_eec_bits(hw
, NVM_WREN_OPCODE_SPI
,
444 * Some SPI eeproms use the 8th address bit embedded in the
447 if ((nvm
->address_bits
== 8) && (offset
>= 128))
448 write_opcode
|= NVM_A8_OPCODE_SPI
;
450 /* Send the Write command (8-bit opcode + addr) */
451 igb_shift_out_eec_bits(hw
, write_opcode
, nvm
->opcode_bits
);
452 igb_shift_out_eec_bits(hw
, (u16
)((offset
+ widx
) * 2),
455 /* Loop to allow for up to whole page write of eeprom */
456 while (widx
< words
) {
457 u16 word_out
= data
[widx
];
458 word_out
= (word_out
>> 8) | (word_out
<< 8);
459 igb_shift_out_eec_bits(hw
, word_out
, 16);
462 if ((((offset
+ widx
) * 2) % nvm
->page_size
) == 0) {
471 hw
->nvm
.ops
.release(hw
);
478 * igb_read_part_num - Read device part number
479 * @hw: pointer to the HW structure
480 * @part_num: pointer to device part number
482 * Reads the product board assembly (PBA) number from the EEPROM and stores
483 * the value in part_num.
485 s32
igb_read_part_num(struct e1000_hw
*hw
, u32
*part_num
)
490 ret_val
= hw
->nvm
.ops
.read(hw
, NVM_PBA_OFFSET_0
, 1, &nvm_data
);
492 hw_dbg("NVM Read Error\n");
495 *part_num
= (u32
)(nvm_data
<< 16);
497 ret_val
= hw
->nvm
.ops
.read(hw
, NVM_PBA_OFFSET_1
, 1, &nvm_data
);
499 hw_dbg("NVM Read Error\n");
502 *part_num
|= nvm_data
;
509 * igb_read_mac_addr - Read device MAC address
510 * @hw: pointer to the HW structure
512 * Reads the device MAC address from the EEPROM and stores the value.
513 * Since devices with two ports use the same EEPROM, we increment the
514 * last bit in the MAC address for the second port.
516 s32
igb_read_mac_addr(struct e1000_hw
*hw
)
522 rar_high
= rd32(E1000_RAH(0));
523 rar_low
= rd32(E1000_RAL(0));
525 for (i
= 0; i
< E1000_RAL_MAC_ADDR_LEN
; i
++)
526 hw
->mac
.perm_addr
[i
] = (u8
)(rar_low
>> (i
*8));
528 for (i
= 0; i
< E1000_RAH_MAC_ADDR_LEN
; i
++)
529 hw
->mac
.perm_addr
[i
+4] = (u8
)(rar_high
>> (i
*8));
531 for (i
= 0; i
< ETH_ALEN
; i
++)
532 hw
->mac
.addr
[i
] = hw
->mac
.perm_addr
[i
];
538 * igb_validate_nvm_checksum - Validate EEPROM checksum
539 * @hw: pointer to the HW structure
541 * Calculates the EEPROM checksum by reading/adding each word of the EEPROM
542 * and then verifies that the sum of the EEPROM is equal to 0xBABA.
544 s32
igb_validate_nvm_checksum(struct e1000_hw
*hw
)
550 for (i
= 0; i
< (NVM_CHECKSUM_REG
+ 1); i
++) {
551 ret_val
= hw
->nvm
.ops
.read(hw
, i
, 1, &nvm_data
);
553 hw_dbg("NVM Read Error\n");
556 checksum
+= nvm_data
;
559 if (checksum
!= (u16
) NVM_SUM
) {
560 hw_dbg("NVM Checksum Invalid\n");
561 ret_val
= -E1000_ERR_NVM
;
570 * igb_update_nvm_checksum - Update EEPROM checksum
571 * @hw: pointer to the HW structure
573 * Updates the EEPROM checksum by reading/adding each word of the EEPROM
574 * up to the checksum. Then calculates the EEPROM checksum and writes the
575 * value to the EEPROM.
577 s32
igb_update_nvm_checksum(struct e1000_hw
*hw
)
583 for (i
= 0; i
< NVM_CHECKSUM_REG
; i
++) {
584 ret_val
= hw
->nvm
.ops
.read(hw
, i
, 1, &nvm_data
);
586 hw_dbg("NVM Read Error while updating checksum.\n");
589 checksum
+= nvm_data
;
591 checksum
= (u16
) NVM_SUM
- checksum
;
592 ret_val
= hw
->nvm
.ops
.write(hw
, NVM_CHECKSUM_REG
, 1, &checksum
);
594 hw_dbg("NVM Write Error while updating checksum.\n");