1 /*******************************************************************************
4 Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2 of the License, or (at your option)
11 This program is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59
18 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 The full GNU General Public License is included in this distribution in the
24 Linux NICS <linux.nics@intel.com>
25 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *******************************************************************************/
31 /* Local prototypes */
32 static uint16_t ixgb_shift_in_bits(struct ixgb_hw *hw);
34 static void ixgb_shift_out_bits(struct ixgb_hw *hw,
35 uint16_t data, uint16_t count);
36 static void ixgb_standby_eeprom(struct ixgb_hw *hw);
38 static boolean_t ixgb_wait_eeprom_command(struct ixgb_hw *hw);
40 static void ixgb_cleanup_eeprom(struct ixgb_hw *hw);
42 /******************************************************************************
43 * Raises the EEPROM's clock input.
45 * hw - Struct containing variables accessed by shared code
46 * eecd_reg - EECD's current value
47 *****************************************************************************/
48 static void ixgb_raise_clock(struct ixgb_hw *hw, uint32_t * eecd_reg)
50 /* Raise the clock input to the EEPROM (by setting the SK bit), and then
51 * wait 50 microseconds.
53 *eecd_reg = *eecd_reg | IXGB_EECD_SK;
54 IXGB_WRITE_REG(hw, EECD, *eecd_reg);
59 /******************************************************************************
60 * Lowers the EEPROM's clock input.
62 * hw - Struct containing variables accessed by shared code
63 * eecd_reg - EECD's current value
64 *****************************************************************************/
65 static void ixgb_lower_clock(struct ixgb_hw *hw, uint32_t * eecd_reg)
67 /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
68 * wait 50 microseconds.
70 *eecd_reg = *eecd_reg & ~IXGB_EECD_SK;
71 IXGB_WRITE_REG(hw, EECD, *eecd_reg);
76 /******************************************************************************
77 * Shift data bits out to the EEPROM.
79 * hw - Struct containing variables accessed by shared code
80 * data - data to send to the EEPROM
81 * count - number of bits to shift out
82 *****************************************************************************/
84 ixgb_shift_out_bits(struct ixgb_hw *hw, uint16_t data, uint16_t count)
89 /* We need to shift "count" bits out to the EEPROM. So, value in the
90 * "data" parameter will be shifted out to the EEPROM one bit at a time.
91 * In order to do this, "data" must be broken down into bits.
93 mask = 0x01 << (count - 1);
94 eecd_reg = IXGB_READ_REG(hw, EECD);
95 eecd_reg &= ~(IXGB_EECD_DO | IXGB_EECD_DI);
97 /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
98 * and then raising and then lowering the clock (the SK bit controls
99 * the clock input to the EEPROM). A "0" is shifted out to the EEPROM
100 * by setting "DI" to "0" and then raising and then lowering the clock.
102 eecd_reg &= ~IXGB_EECD_DI;
105 eecd_reg |= IXGB_EECD_DI;
107 IXGB_WRITE_REG(hw, EECD, eecd_reg);
111 ixgb_raise_clock(hw, &eecd_reg);
112 ixgb_lower_clock(hw, &eecd_reg);
118 /* We leave the "DI" bit set to "0" when we leave this routine. */
119 eecd_reg &= ~IXGB_EECD_DI;
120 IXGB_WRITE_REG(hw, EECD, eecd_reg);
124 /******************************************************************************
125 * Shift data bits in from the EEPROM
127 * hw - Struct containing variables accessed by shared code
128 *****************************************************************************/
129 static uint16_t ixgb_shift_in_bits(struct ixgb_hw *hw)
135 /* In order to read a register from the EEPROM, we need to shift 16 bits
136 * in from the EEPROM. Bits are "shifted in" by raising the clock input to
137 * the EEPROM (setting the SK bit), and then reading the value of the "DO"
138 * bit. During this "shifting in" process the "DI" bit should always be
142 eecd_reg = IXGB_READ_REG(hw, EECD);
144 eecd_reg &= ~(IXGB_EECD_DO | IXGB_EECD_DI);
147 for (i = 0; i < 16; i++) {
149 ixgb_raise_clock(hw, &eecd_reg);
151 eecd_reg = IXGB_READ_REG(hw, EECD);
153 eecd_reg &= ~(IXGB_EECD_DI);
154 if (eecd_reg & IXGB_EECD_DO)
157 ixgb_lower_clock(hw, &eecd_reg);
163 /******************************************************************************
164 * Prepares EEPROM for access
166 * hw - Struct containing variables accessed by shared code
168 * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
169 * function should be called before issuing a command to the EEPROM.
170 *****************************************************************************/
171 static void ixgb_setup_eeprom(struct ixgb_hw *hw)
175 eecd_reg = IXGB_READ_REG(hw, EECD);
177 /* Clear SK and DI */
178 eecd_reg &= ~(IXGB_EECD_SK | IXGB_EECD_DI);
179 IXGB_WRITE_REG(hw, EECD, eecd_reg);
182 eecd_reg |= IXGB_EECD_CS;
183 IXGB_WRITE_REG(hw, EECD, eecd_reg);
187 /******************************************************************************
188 * Returns EEPROM to a "standby" state
190 * hw - Struct containing variables accessed by shared code
191 *****************************************************************************/
192 static void ixgb_standby_eeprom(struct ixgb_hw *hw)
196 eecd_reg = IXGB_READ_REG(hw, EECD);
199 eecd_reg &= ~(IXGB_EECD_CS | IXGB_EECD_SK);
200 IXGB_WRITE_REG(hw, EECD, eecd_reg);
204 eecd_reg |= IXGB_EECD_SK;
205 IXGB_WRITE_REG(hw, EECD, eecd_reg);
209 eecd_reg |= IXGB_EECD_CS;
210 IXGB_WRITE_REG(hw, EECD, eecd_reg);
214 eecd_reg &= ~IXGB_EECD_SK;
215 IXGB_WRITE_REG(hw, EECD, eecd_reg);
220 /******************************************************************************
221 * Raises then lowers the EEPROM's clock pin
223 * hw - Struct containing variables accessed by shared code
224 *****************************************************************************/
225 static void ixgb_clock_eeprom(struct ixgb_hw *hw)
229 eecd_reg = IXGB_READ_REG(hw, EECD);
231 /* Rising edge of clock */
232 eecd_reg |= IXGB_EECD_SK;
233 IXGB_WRITE_REG(hw, EECD, eecd_reg);
236 /* Falling edge of clock */
237 eecd_reg &= ~IXGB_EECD_SK;
238 IXGB_WRITE_REG(hw, EECD, eecd_reg);
243 /******************************************************************************
244 * Terminates a command by lowering the EEPROM's chip select pin
246 * hw - Struct containing variables accessed by shared code
247 *****************************************************************************/
248 static void ixgb_cleanup_eeprom(struct ixgb_hw *hw)
252 eecd_reg = IXGB_READ_REG(hw, EECD);
254 eecd_reg &= ~(IXGB_EECD_CS | IXGB_EECD_DI);
256 IXGB_WRITE_REG(hw, EECD, eecd_reg);
258 ixgb_clock_eeprom(hw);
262 /******************************************************************************
263 * Waits for the EEPROM to finish the current command.
265 * hw - Struct containing variables accessed by shared code
267 * The command is done when the EEPROM's data out pin goes high.
270 * TRUE: EEPROM data pin is high before timeout.
271 * FALSE: Time expired.
272 *****************************************************************************/
273 static boolean_t ixgb_wait_eeprom_command(struct ixgb_hw *hw)
278 /* Toggle the CS line. This in effect tells to EEPROM to actually execute
279 * the command in question.
281 ixgb_standby_eeprom(hw);
283 /* Now read DO repeatedly until is high (equal to '1'). The EEEPROM will
284 * signal that the command has been completed by raising the DO signal.
285 * If DO does not go high in 10 milliseconds, then error out.
287 for (i = 0; i < 200; i++) {
288 eecd_reg = IXGB_READ_REG(hw, EECD);
290 if (eecd_reg & IXGB_EECD_DO)
299 /******************************************************************************
300 * Verifies that the EEPROM has a valid checksum
302 * hw - Struct containing variables accessed by shared code
304 * Reads the first 64 16 bit words of the EEPROM and sums the values read.
305 * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
309 * TRUE: Checksum is valid
310 * FALSE: Checksum is not valid.
311 *****************************************************************************/
312 boolean_t ixgb_validate_eeprom_checksum(struct ixgb_hw * hw)
314 uint16_t checksum = 0;
317 for (i = 0; i < (EEPROM_CHECKSUM_REG + 1); i++)
318 checksum += ixgb_read_eeprom(hw, i);
320 if (checksum == (uint16_t) EEPROM_SUM)
326 /******************************************************************************
327 * Calculates the EEPROM checksum and writes it to the EEPROM
329 * hw - Struct containing variables accessed by shared code
331 * Sums the first 63 16 bit words of the EEPROM. Subtracts the sum from 0xBABA.
332 * Writes the difference to word offset 63 of the EEPROM.
333 *****************************************************************************/
334 void ixgb_update_eeprom_checksum(struct ixgb_hw *hw)
336 uint16_t checksum = 0;
339 for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
340 checksum += ixgb_read_eeprom(hw, i);
342 checksum = (uint16_t) EEPROM_SUM - checksum;
344 ixgb_write_eeprom(hw, EEPROM_CHECKSUM_REG, checksum);
348 /******************************************************************************
349 * Writes a 16 bit word to a given offset in the EEPROM.
351 * hw - Struct containing variables accessed by shared code
352 * reg - offset within the EEPROM to be written to
353 * data - 16 bit word to be writen to the EEPROM
355 * If ixgb_update_eeprom_checksum is not called after this function, the
356 * EEPROM will most likely contain an invalid checksum.
358 *****************************************************************************/
359 void ixgb_write_eeprom(struct ixgb_hw *hw, uint16_t offset, uint16_t data)
361 /* Prepare the EEPROM for writing */
362 ixgb_setup_eeprom(hw);
364 /* Send the 9-bit EWEN (write enable) command to the EEPROM (5-bit opcode
365 * plus 4-bit dummy). This puts the EEPROM into write/erase mode.
367 ixgb_shift_out_bits(hw, EEPROM_EWEN_OPCODE, 5);
368 ixgb_shift_out_bits(hw, 0, 4);
370 /* Prepare the EEPROM */
371 ixgb_standby_eeprom(hw);
373 /* Send the Write command (3-bit opcode + 6-bit addr) */
374 ixgb_shift_out_bits(hw, EEPROM_WRITE_OPCODE, 3);
375 ixgb_shift_out_bits(hw, offset, 6);
378 ixgb_shift_out_bits(hw, data, 16);
380 ixgb_wait_eeprom_command(hw);
382 /* Recover from write */
383 ixgb_standby_eeprom(hw);
385 /* Send the 9-bit EWDS (write disable) command to the EEPROM (5-bit
386 * opcode plus 4-bit dummy). This takes the EEPROM out of write/erase
389 ixgb_shift_out_bits(hw, EEPROM_EWDS_OPCODE, 5);
390 ixgb_shift_out_bits(hw, 0, 4);
392 /* Done with writing */
393 ixgb_cleanup_eeprom(hw);
398 /******************************************************************************
399 * Reads a 16 bit word from the EEPROM.
401 * hw - Struct containing variables accessed by shared code
402 * offset - offset of 16 bit word in the EEPROM to read
405 * The 16-bit value read from the eeprom
406 *****************************************************************************/
407 uint16_t ixgb_read_eeprom(struct ixgb_hw * hw, uint16_t offset)
411 /* Prepare the EEPROM for reading */
412 ixgb_setup_eeprom(hw);
414 /* Send the READ command (opcode + addr) */
415 ixgb_shift_out_bits(hw, EEPROM_READ_OPCODE, 3);
417 * We have a 64 word EEPROM, there are 6 address bits
419 ixgb_shift_out_bits(hw, offset, 6);
422 data = ixgb_shift_in_bits(hw);
424 /* End this read operation */
425 ixgb_standby_eeprom(hw);
430 /******************************************************************************
431 * Reads eeprom and stores data in shared structure.
432 * Validates eeprom checksum and eeprom signature.
434 * hw - Struct containing variables accessed by shared code
437 * TRUE: if eeprom read is successful
439 *****************************************************************************/
440 boolean_t ixgb_get_eeprom_data(struct ixgb_hw * hw)
443 uint16_t checksum = 0;
444 struct ixgb_ee_map_type *ee_map;
446 DEBUGFUNC("ixgb_get_eeprom_data");
448 ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
450 DEBUGOUT("ixgb_ee: Reading eeprom data\n");
451 for (i = 0; i < IXGB_EEPROM_SIZE; i++) {
453 ee_data = ixgb_read_eeprom(hw, i);
455 hw->eeprom[i] = le16_to_cpu(ee_data);
458 if (checksum != (uint16_t) EEPROM_SUM) {
459 DEBUGOUT("ixgb_ee: Checksum invalid.\n");
463 if ((ee_map->init_ctrl_reg_1 & le16_to_cpu(EEPROM_ICW1_SIGNATURE_MASK))
464 != le16_to_cpu(EEPROM_ICW1_SIGNATURE_VALID)) {
465 DEBUGOUT("ixgb_ee: Signature invalid.\n");
472 /******************************************************************************
473 * Local function to check if the eeprom signature is good
474 * If the eeprom signature is good, calls ixgb)get_eeprom_data.
476 * hw - Struct containing variables accessed by shared code
479 * TRUE: eeprom signature was good and the eeprom read was successful
481 ******************************************************************************/
482 static boolean_t ixgb_check_and_get_eeprom_data(struct ixgb_hw *hw)
484 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
486 if ((ee_map->init_ctrl_reg_1 & le16_to_cpu(EEPROM_ICW1_SIGNATURE_MASK))
487 == le16_to_cpu(EEPROM_ICW1_SIGNATURE_VALID)) {
490 return ixgb_get_eeprom_data(hw);
494 /******************************************************************************
495 * return a word from the eeprom
497 * hw - Struct containing variables accessed by shared code
498 * index - Offset of eeprom word
501 * Word at indexed offset in eeprom, if valid, 0 otherwise.
502 ******************************************************************************/
503 uint16_t ixgb_get_eeprom_word(struct ixgb_hw * hw, uint16_t index)
506 if ((index < IXGB_EEPROM_SIZE) &&
507 (ixgb_check_and_get_eeprom_data(hw) == TRUE)) {
508 return (hw->eeprom[index]);
514 /******************************************************************************
515 * return the mac address from EEPROM
517 * hw - Struct containing variables accessed by shared code
518 * mac_addr - Ethernet Address if EEPROM contents are valid, 0 otherwise
521 ******************************************************************************/
522 void ixgb_get_ee_mac_addr(struct ixgb_hw *hw, uint8_t * mac_addr)
525 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
527 DEBUGFUNC("ixgb_get_ee_mac_addr");
529 if (ixgb_check_and_get_eeprom_data(hw) == TRUE) {
530 for (i = 0; i < IXGB_ETH_LENGTH_OF_ADDRESS; i++) {
531 mac_addr[i] = ee_map->mac_addr[i];
532 DEBUGOUT2("mac(%d) = %.2X\n", i, mac_addr[i]);
537 /******************************************************************************
538 * return the compatibility flags from EEPROM
540 * hw - Struct containing variables accessed by shared code
543 * compatibility flags if EEPROM contents are valid, 0 otherwise
544 ******************************************************************************/
545 uint16_t ixgb_get_ee_compatibility(struct ixgb_hw *hw)
547 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
549 if (ixgb_check_and_get_eeprom_data(hw) == TRUE)
550 return (ee_map->compatibility);
555 /******************************************************************************
556 * return the Printed Board Assembly number from EEPROM
558 * hw - Struct containing variables accessed by shared code
561 * PBA number if EEPROM contents are valid, 0 otherwise
562 ******************************************************************************/
563 uint32_t ixgb_get_ee_pba_number(struct ixgb_hw * hw)
565 if (ixgb_check_and_get_eeprom_data(hw) == TRUE)
566 return (le16_to_cpu(hw->eeprom[EEPROM_PBA_1_2_REG])
567 | (le16_to_cpu(hw->eeprom[EEPROM_PBA_3_4_REG]) << 16));
572 /******************************************************************************
573 * return the Initialization Control Word 1 from EEPROM
575 * hw - Struct containing variables accessed by shared code
578 * Initialization Control Word 1 if EEPROM contents are valid, 0 otherwise
579 ******************************************************************************/
580 uint16_t ixgb_get_ee_init_ctrl_reg_1(struct ixgb_hw * hw)
582 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
584 if (ixgb_check_and_get_eeprom_data(hw) == TRUE)
585 return (ee_map->init_ctrl_reg_1);
590 /******************************************************************************
591 * return the Initialization Control Word 2 from EEPROM
593 * hw - Struct containing variables accessed by shared code
596 * Initialization Control Word 2 if EEPROM contents are valid, 0 otherwise
597 ******************************************************************************/
598 uint16_t ixgb_get_ee_init_ctrl_reg_2(struct ixgb_hw * hw)
600 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
602 if (ixgb_check_and_get_eeprom_data(hw) == TRUE)
603 return (ee_map->init_ctrl_reg_2);
608 /******************************************************************************
609 * return the Subsystem Id from EEPROM
611 * hw - Struct containing variables accessed by shared code
614 * Subsystem Id if EEPROM contents are valid, 0 otherwise
615 ******************************************************************************/
616 uint16_t ixgb_get_ee_subsystem_id(struct ixgb_hw * hw)
618 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
620 if (ixgb_check_and_get_eeprom_data(hw) == TRUE)
621 return (ee_map->subsystem_id);
626 /******************************************************************************
627 * return the Sub Vendor Id from EEPROM
629 * hw - Struct containing variables accessed by shared code
632 * Sub Vendor Id if EEPROM contents are valid, 0 otherwise
633 ******************************************************************************/
634 uint16_t ixgb_get_ee_subvendor_id(struct ixgb_hw * hw)
636 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
638 if (ixgb_check_and_get_eeprom_data(hw) == TRUE)
639 return (ee_map->subvendor_id);
644 /******************************************************************************
645 * return the Device Id from EEPROM
647 * hw - Struct containing variables accessed by shared code
650 * Device Id if EEPROM contents are valid, 0 otherwise
651 ******************************************************************************/
652 uint16_t ixgb_get_ee_device_id(struct ixgb_hw * hw)
654 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
656 if (ixgb_check_and_get_eeprom_data(hw) == TRUE)
657 return (ee_map->device_id);
662 /******************************************************************************
663 * return the Vendor Id from EEPROM
665 * hw - Struct containing variables accessed by shared code
668 * Device Id if EEPROM contents are valid, 0 otherwise
669 ******************************************************************************/
670 uint16_t ixgb_get_ee_vendor_id(struct ixgb_hw * hw)
672 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
674 if (ixgb_check_and_get_eeprom_data(hw) == TRUE)
675 return (ee_map->vendor_id);
680 /******************************************************************************
681 * return the Software Defined Pins Register from EEPROM
683 * hw - Struct containing variables accessed by shared code
686 * SDP Register if EEPROM contents are valid, 0 otherwise
687 ******************************************************************************/
688 uint16_t ixgb_get_ee_swdpins_reg(struct ixgb_hw * hw)
690 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
692 if (ixgb_check_and_get_eeprom_data(hw) == TRUE)
693 return (ee_map->swdpins_reg);
698 /******************************************************************************
699 * return the D3 Power Management Bits from EEPROM
701 * hw - Struct containing variables accessed by shared code
704 * D3 Power Management Bits if EEPROM contents are valid, 0 otherwise
705 ******************************************************************************/
706 uint8_t ixgb_get_ee_d3_power(struct ixgb_hw * hw)
708 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
710 if (ixgb_check_and_get_eeprom_data(hw) == TRUE)
711 return (ee_map->d3_power);
716 /******************************************************************************
717 * return the D0 Power Management Bits from EEPROM
719 * hw - Struct containing variables accessed by shared code
722 * D0 Power Management Bits if EEPROM contents are valid, 0 otherwise
723 ******************************************************************************/
724 uint8_t ixgb_get_ee_d0_power(struct ixgb_hw * hw)
726 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
728 if (ixgb_check_and_get_eeprom_data(hw) == TRUE)
729 return (ee_map->d0_power);