1 /*******************************************************************************
3 Intel PRO/10GbE 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
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".
25 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *******************************************************************************/
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 /* Local prototypes */
34 static u16 ixgb_shift_in_bits(struct ixgb_hw *hw);
36 static void ixgb_shift_out_bits(struct ixgb_hw *hw,
39 static void ixgb_standby_eeprom(struct ixgb_hw *hw);
41 static bool ixgb_wait_eeprom_command(struct ixgb_hw *hw);
43 static void ixgb_cleanup_eeprom(struct ixgb_hw *hw);
45 /******************************************************************************
46 * Raises the EEPROM's clock input.
48 * hw - Struct containing variables accessed by shared code
49 * eecd_reg - EECD's current value
50 *****************************************************************************/
52 ixgb_raise_clock(struct ixgb_hw *hw,
55 /* Raise the clock input to the EEPROM (by setting the SK bit), and then
56 * wait 50 microseconds.
58 *eecd_reg = *eecd_reg | IXGB_EECD_SK;
59 IXGB_WRITE_REG(hw, EECD, *eecd_reg);
63 /******************************************************************************
64 * Lowers the EEPROM's clock input.
66 * hw - Struct containing variables accessed by shared code
67 * eecd_reg - EECD's current value
68 *****************************************************************************/
70 ixgb_lower_clock(struct ixgb_hw *hw,
73 /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
74 * wait 50 microseconds.
76 *eecd_reg = *eecd_reg & ~IXGB_EECD_SK;
77 IXGB_WRITE_REG(hw, EECD, *eecd_reg);
81 /******************************************************************************
82 * Shift data bits out to the EEPROM.
84 * hw - Struct containing variables accessed by shared code
85 * data - data to send to the EEPROM
86 * count - number of bits to shift out
87 *****************************************************************************/
89 ixgb_shift_out_bits(struct ixgb_hw *hw,
96 /* We need to shift "count" bits out to the EEPROM. So, value in the
97 * "data" parameter will be shifted out to the EEPROM one bit at a time.
98 * In order to do this, "data" must be broken down into bits.
100 mask = 0x01 << (count - 1);
101 eecd_reg = IXGB_READ_REG(hw, EECD);
102 eecd_reg &= ~(IXGB_EECD_DO | IXGB_EECD_DI);
104 /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
105 * and then raising and then lowering the clock (the SK bit controls
106 * the clock input to the EEPROM). A "0" is shifted out to the EEPROM
107 * by setting "DI" to "0" and then raising and then lowering the clock.
109 eecd_reg &= ~IXGB_EECD_DI;
112 eecd_reg |= IXGB_EECD_DI;
114 IXGB_WRITE_REG(hw, EECD, eecd_reg);
118 ixgb_raise_clock(hw, &eecd_reg);
119 ixgb_lower_clock(hw, &eecd_reg);
125 /* We leave the "DI" bit set to "0" when we leave this routine. */
126 eecd_reg &= ~IXGB_EECD_DI;
127 IXGB_WRITE_REG(hw, EECD, eecd_reg);
130 /******************************************************************************
131 * Shift data bits in from the EEPROM
133 * hw - Struct containing variables accessed by shared code
134 *****************************************************************************/
136 ixgb_shift_in_bits(struct ixgb_hw *hw)
142 /* In order to read a register from the EEPROM, we need to shift 16 bits
143 * in from the EEPROM. Bits are "shifted in" by raising the clock input to
144 * the EEPROM (setting the SK bit), and then reading the value of the "DO"
145 * bit. During this "shifting in" process the "DI" bit should always be
149 eecd_reg = IXGB_READ_REG(hw, EECD);
151 eecd_reg &= ~(IXGB_EECD_DO | IXGB_EECD_DI);
154 for (i = 0; i < 16; i++) {
156 ixgb_raise_clock(hw, &eecd_reg);
158 eecd_reg = IXGB_READ_REG(hw, EECD);
160 eecd_reg &= ~(IXGB_EECD_DI);
161 if (eecd_reg & IXGB_EECD_DO)
164 ixgb_lower_clock(hw, &eecd_reg);
170 /******************************************************************************
171 * Prepares EEPROM for access
173 * hw - Struct containing variables accessed by shared code
175 * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
176 * function should be called before issuing a command to the EEPROM.
177 *****************************************************************************/
179 ixgb_setup_eeprom(struct ixgb_hw *hw)
183 eecd_reg = IXGB_READ_REG(hw, EECD);
185 /* Clear SK and DI */
186 eecd_reg &= ~(IXGB_EECD_SK | IXGB_EECD_DI);
187 IXGB_WRITE_REG(hw, EECD, eecd_reg);
190 eecd_reg |= IXGB_EECD_CS;
191 IXGB_WRITE_REG(hw, EECD, eecd_reg);
194 /******************************************************************************
195 * Returns EEPROM to a "standby" state
197 * hw - Struct containing variables accessed by shared code
198 *****************************************************************************/
200 ixgb_standby_eeprom(struct ixgb_hw *hw)
204 eecd_reg = IXGB_READ_REG(hw, EECD);
206 /* Deselect EEPROM */
207 eecd_reg &= ~(IXGB_EECD_CS | IXGB_EECD_SK);
208 IXGB_WRITE_REG(hw, EECD, eecd_reg);
212 eecd_reg |= IXGB_EECD_SK;
213 IXGB_WRITE_REG(hw, EECD, eecd_reg);
217 eecd_reg |= IXGB_EECD_CS;
218 IXGB_WRITE_REG(hw, EECD, eecd_reg);
222 eecd_reg &= ~IXGB_EECD_SK;
223 IXGB_WRITE_REG(hw, EECD, eecd_reg);
227 /******************************************************************************
228 * Raises then lowers the EEPROM's clock pin
230 * hw - Struct containing variables accessed by shared code
231 *****************************************************************************/
233 ixgb_clock_eeprom(struct ixgb_hw *hw)
237 eecd_reg = IXGB_READ_REG(hw, EECD);
239 /* Rising edge of clock */
240 eecd_reg |= IXGB_EECD_SK;
241 IXGB_WRITE_REG(hw, EECD, eecd_reg);
244 /* Falling edge of clock */
245 eecd_reg &= ~IXGB_EECD_SK;
246 IXGB_WRITE_REG(hw, EECD, eecd_reg);
250 /******************************************************************************
251 * Terminates a command by lowering the EEPROM's chip select pin
253 * hw - Struct containing variables accessed by shared code
254 *****************************************************************************/
256 ixgb_cleanup_eeprom(struct ixgb_hw *hw)
260 eecd_reg = IXGB_READ_REG(hw, EECD);
262 eecd_reg &= ~(IXGB_EECD_CS | IXGB_EECD_DI);
264 IXGB_WRITE_REG(hw, EECD, eecd_reg);
266 ixgb_clock_eeprom(hw);
269 /******************************************************************************
270 * Waits for the EEPROM to finish the current command.
272 * hw - Struct containing variables accessed by shared code
274 * The command is done when the EEPROM's data out pin goes high.
277 * true: EEPROM data pin is high before timeout.
278 * false: Time expired.
279 *****************************************************************************/
281 ixgb_wait_eeprom_command(struct ixgb_hw *hw)
286 /* Toggle the CS line. This in effect tells to EEPROM to actually execute
287 * the command in question.
289 ixgb_standby_eeprom(hw);
291 /* Now read DO repeatedly until is high (equal to '1'). The EEPROM will
292 * signal that the command has been completed by raising the DO signal.
293 * If DO does not go high in 10 milliseconds, then error out.
295 for (i = 0; i < 200; i++) {
296 eecd_reg = IXGB_READ_REG(hw, EECD);
298 if (eecd_reg & IXGB_EECD_DO)
307 /******************************************************************************
308 * Verifies that the EEPROM has a valid checksum
310 * hw - Struct containing variables accessed by shared code
312 * Reads the first 64 16 bit words of the EEPROM and sums the values read.
313 * If the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
317 * true: Checksum is valid
318 * false: Checksum is not valid.
319 *****************************************************************************/
321 ixgb_validate_eeprom_checksum(struct ixgb_hw *hw)
326 for (i = 0; i < (EEPROM_CHECKSUM_REG + 1); i++)
327 checksum += ixgb_read_eeprom(hw, i);
329 if (checksum == (u16) EEPROM_SUM)
335 /******************************************************************************
336 * Calculates the EEPROM checksum and writes it to the EEPROM
338 * hw - Struct containing variables accessed by shared code
340 * Sums the first 63 16 bit words of the EEPROM. Subtracts the sum from 0xBABA.
341 * Writes the difference to word offset 63 of the EEPROM.
342 *****************************************************************************/
344 ixgb_update_eeprom_checksum(struct ixgb_hw *hw)
349 for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
350 checksum += ixgb_read_eeprom(hw, i);
352 checksum = (u16) EEPROM_SUM - checksum;
354 ixgb_write_eeprom(hw, EEPROM_CHECKSUM_REG, checksum);
357 /******************************************************************************
358 * Writes a 16 bit word to a given offset in the EEPROM.
360 * hw - Struct containing variables accessed by shared code
361 * reg - offset within the EEPROM to be written to
362 * data - 16 bit word to be written to the EEPROM
364 * If ixgb_update_eeprom_checksum is not called after this function, the
365 * EEPROM will most likely contain an invalid checksum.
367 *****************************************************************************/
369 ixgb_write_eeprom(struct ixgb_hw *hw, u16 offset, u16 data)
371 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
373 /* Prepare the EEPROM for writing */
374 ixgb_setup_eeprom(hw);
376 /* Send the 9-bit EWEN (write enable) command to the EEPROM (5-bit opcode
377 * plus 4-bit dummy). This puts the EEPROM into write/erase mode.
379 ixgb_shift_out_bits(hw, EEPROM_EWEN_OPCODE, 5);
380 ixgb_shift_out_bits(hw, 0, 4);
382 /* Prepare the EEPROM */
383 ixgb_standby_eeprom(hw);
385 /* Send the Write command (3-bit opcode + 6-bit addr) */
386 ixgb_shift_out_bits(hw, EEPROM_WRITE_OPCODE, 3);
387 ixgb_shift_out_bits(hw, offset, 6);
390 ixgb_shift_out_bits(hw, data, 16);
392 ixgb_wait_eeprom_command(hw);
394 /* Recover from write */
395 ixgb_standby_eeprom(hw);
397 /* Send the 9-bit EWDS (write disable) command to the EEPROM (5-bit
398 * opcode plus 4-bit dummy). This takes the EEPROM out of write/erase
401 ixgb_shift_out_bits(hw, EEPROM_EWDS_OPCODE, 5);
402 ixgb_shift_out_bits(hw, 0, 4);
404 /* Done with writing */
405 ixgb_cleanup_eeprom(hw);
407 /* clear the init_ctrl_reg_1 to signify that the cache is invalidated */
408 ee_map->init_ctrl_reg_1 = cpu_to_le16(EEPROM_ICW1_SIGNATURE_CLEAR);
411 /******************************************************************************
412 * Reads a 16 bit word from the EEPROM.
414 * hw - Struct containing variables accessed by shared code
415 * offset - offset of 16 bit word in the EEPROM to read
418 * The 16-bit value read from the eeprom
419 *****************************************************************************/
421 ixgb_read_eeprom(struct ixgb_hw *hw,
426 /* Prepare the EEPROM for reading */
427 ixgb_setup_eeprom(hw);
429 /* Send the READ command (opcode + addr) */
430 ixgb_shift_out_bits(hw, EEPROM_READ_OPCODE, 3);
432 * We have a 64 word EEPROM, there are 6 address bits
434 ixgb_shift_out_bits(hw, offset, 6);
437 data = ixgb_shift_in_bits(hw);
439 /* End this read operation */
440 ixgb_standby_eeprom(hw);
445 /******************************************************************************
446 * Reads eeprom and stores data in shared structure.
447 * Validates eeprom checksum and eeprom signature.
449 * hw - Struct containing variables accessed by shared code
452 * true: if eeprom read is successful
454 *****************************************************************************/
456 ixgb_get_eeprom_data(struct ixgb_hw *hw)
460 struct ixgb_ee_map_type *ee_map;
464 ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
466 pr_debug("Reading eeprom data\n");
467 for (i = 0; i < IXGB_EEPROM_SIZE ; i++) {
469 ee_data = ixgb_read_eeprom(hw, i);
471 hw->eeprom[i] = cpu_to_le16(ee_data);
474 if (checksum != (u16) EEPROM_SUM) {
475 pr_debug("Checksum invalid\n");
476 /* clear the init_ctrl_reg_1 to signify that the cache is
478 ee_map->init_ctrl_reg_1 = cpu_to_le16(EEPROM_ICW1_SIGNATURE_CLEAR);
482 if ((ee_map->init_ctrl_reg_1 & cpu_to_le16(EEPROM_ICW1_SIGNATURE_MASK))
483 != cpu_to_le16(EEPROM_ICW1_SIGNATURE_VALID)) {
484 pr_debug("Signature invalid\n");
491 /******************************************************************************
492 * Local function to check if the eeprom signature is good
493 * If the eeprom signature is good, calls ixgb)get_eeprom_data.
495 * hw - Struct containing variables accessed by shared code
498 * true: eeprom signature was good and the eeprom read was successful
500 ******************************************************************************/
502 ixgb_check_and_get_eeprom_data (struct ixgb_hw* hw)
504 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
506 if ((ee_map->init_ctrl_reg_1 & cpu_to_le16(EEPROM_ICW1_SIGNATURE_MASK))
507 == cpu_to_le16(EEPROM_ICW1_SIGNATURE_VALID)) {
510 return ixgb_get_eeprom_data(hw);
514 /******************************************************************************
515 * return a word from the eeprom
517 * hw - Struct containing variables accessed by shared code
518 * index - Offset of eeprom word
521 * Word at indexed offset in eeprom, if valid, 0 otherwise.
522 ******************************************************************************/
524 ixgb_get_eeprom_word(struct ixgb_hw *hw, u16 index)
527 if ((index < IXGB_EEPROM_SIZE) &&
528 (ixgb_check_and_get_eeprom_data(hw) == true)) {
529 return hw->eeprom[index];
535 /******************************************************************************
536 * return the mac address from EEPROM
538 * hw - Struct containing variables accessed by shared code
539 * mac_addr - Ethernet Address if EEPROM contents are valid, 0 otherwise
542 ******************************************************************************/
544 ixgb_get_ee_mac_addr(struct ixgb_hw *hw,
548 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
552 if (ixgb_check_and_get_eeprom_data(hw) == true) {
553 for (i = 0; i < IXGB_ETH_LENGTH_OF_ADDRESS; i++) {
554 mac_addr[i] = ee_map->mac_addr[i];
556 pr_debug("eeprom mac address = %pM\n", mac_addr);
561 /******************************************************************************
562 * return the Printed Board Assembly number from EEPROM
564 * hw - Struct containing variables accessed by shared code
567 * PBA number if EEPROM contents are valid, 0 otherwise
568 ******************************************************************************/
570 ixgb_get_ee_pba_number(struct ixgb_hw *hw)
572 if (ixgb_check_and_get_eeprom_data(hw) == true)
573 return le16_to_cpu(hw->eeprom[EEPROM_PBA_1_2_REG])
574 | (le16_to_cpu(hw->eeprom[EEPROM_PBA_3_4_REG])<<16);
580 /******************************************************************************
581 * return the Device Id from EEPROM
583 * hw - Struct containing variables accessed by shared code
586 * Device Id if EEPROM contents are valid, 0 otherwise
587 ******************************************************************************/
589 ixgb_get_ee_device_id(struct ixgb_hw *hw)
591 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
593 if (ixgb_check_and_get_eeprom_data(hw) == true)
594 return le16_to_cpu(ee_map->device_id);