1 /*******************************************************************************
3 Copyright (c) 2001-2004, Intel Corporation
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 3. Neither the name of the Intel Corporation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
32 *******************************************************************************/
36 #include <dev/ixgb/ixgb_hw.h>
37 #include <dev/ixgb/ixgb_ee.h>
39 /* Local prototypes */
40 static uint16_t ixgb_shift_in_bits(struct ixgb_hw *hw);
42 static void ixgb_shift_out_bits(struct ixgb_hw *hw,
45 static void ixgb_standby_eeprom(struct ixgb_hw *hw);
47 static boolean_t ixgb_wait_eeprom_command(struct ixgb_hw *hw);
49 static void ixgb_cleanup_eeprom(struct ixgb_hw *hw);
51 /******************************************************************************
52 * Raises the EEPROM's clock input.
54 * hw - Struct containing variables accessed by shared code
55 * eecd_reg - EECD's current value
56 *****************************************************************************/
58 ixgb_raise_clock(struct ixgb_hw *hw,
61 /* Raise the clock input to the EEPROM (by setting the SK bit), and then
62 * wait 50 microseconds.
64 *eecd_reg = *eecd_reg | IXGB_EECD_SK;
65 IXGB_WRITE_REG(hw, EECD, *eecd_reg);
70 /******************************************************************************
71 * Lowers the EEPROM's clock input.
73 * hw - Struct containing variables accessed by shared code
74 * eecd_reg - EECD's current value
75 *****************************************************************************/
77 ixgb_lower_clock(struct ixgb_hw *hw,
80 /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
81 * wait 50 microseconds.
83 *eecd_reg = *eecd_reg & ~IXGB_EECD_SK;
84 IXGB_WRITE_REG(hw, EECD, *eecd_reg);
89 /******************************************************************************
90 * Shift data bits out to the EEPROM.
92 * hw - Struct containing variables accessed by shared code
93 * data - data to send to the EEPROM
94 * count - number of bits to shift out
95 *****************************************************************************/
97 ixgb_shift_out_bits(struct ixgb_hw *hw,
104 /* We need to shift "count" bits out to the EEPROM. So, value in the
105 * "data" parameter will be shifted out to the EEPROM one bit at a time.
106 * In order to do this, "data" must be broken down into bits.
108 mask = 0x01 << (count - 1);
109 eecd_reg = IXGB_READ_REG(hw, EECD);
110 eecd_reg &= ~(IXGB_EECD_DO | IXGB_EECD_DI);
112 /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
113 * and then raising and then lowering the clock (the SK bit controls
114 * the clock input to the EEPROM). A "0" is shifted out to the EEPROM
115 * by setting "DI" to "0" and then raising and then lowering the clock.
117 eecd_reg &= ~IXGB_EECD_DI;
120 eecd_reg |= IXGB_EECD_DI;
122 IXGB_WRITE_REG(hw, EECD, eecd_reg);
126 ixgb_raise_clock(hw, &eecd_reg);
127 ixgb_lower_clock(hw, &eecd_reg);
133 /* We leave the "DI" bit set to "0" when we leave this routine. */
134 eecd_reg &= ~IXGB_EECD_DI;
135 IXGB_WRITE_REG(hw, EECD, eecd_reg);
139 /******************************************************************************
140 * Shift data bits in from the EEPROM
142 * hw - Struct containing variables accessed by shared code
143 *****************************************************************************/
145 ixgb_shift_in_bits(struct ixgb_hw *hw)
151 /* In order to read a register from the EEPROM, we need to shift 16 bits
152 * in from the EEPROM. Bits are "shifted in" by raising the clock input to
153 * the EEPROM (setting the SK bit), and then reading the value of the "DO"
154 * bit. During this "shifting in" process the "DI" bit should always be
158 eecd_reg = IXGB_READ_REG(hw, EECD);
160 eecd_reg &= ~(IXGB_EECD_DO | IXGB_EECD_DI);
163 for(i = 0; i < 16; i++) {
165 ixgb_raise_clock(hw, &eecd_reg);
167 eecd_reg = IXGB_READ_REG(hw, EECD);
169 eecd_reg &= ~(IXGB_EECD_DI);
170 if(eecd_reg & IXGB_EECD_DO)
173 ixgb_lower_clock(hw, &eecd_reg);
179 /******************************************************************************
180 * Prepares EEPROM for access
182 * hw - Struct containing variables accessed by shared code
184 * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
185 * function should be called before issuing a command to the EEPROM.
186 *****************************************************************************/
188 ixgb_setup_eeprom(struct ixgb_hw *hw)
192 eecd_reg = IXGB_READ_REG(hw, EECD);
194 /* Clear SK and DI */
195 eecd_reg &= ~(IXGB_EECD_SK | IXGB_EECD_DI);
196 IXGB_WRITE_REG(hw, EECD, eecd_reg);
199 eecd_reg |= IXGB_EECD_CS;
200 IXGB_WRITE_REG(hw, EECD, eecd_reg);
204 /******************************************************************************
205 * Returns EEPROM to a "standby" state
207 * hw - Struct containing variables accessed by shared code
208 *****************************************************************************/
210 ixgb_standby_eeprom(struct ixgb_hw *hw)
214 eecd_reg = IXGB_READ_REG(hw, EECD);
217 eecd_reg &= ~(IXGB_EECD_CS | IXGB_EECD_SK);
218 IXGB_WRITE_REG(hw, EECD, eecd_reg);
222 eecd_reg |= IXGB_EECD_SK;
223 IXGB_WRITE_REG(hw, EECD, eecd_reg);
227 eecd_reg |= IXGB_EECD_CS;
228 IXGB_WRITE_REG(hw, EECD, eecd_reg);
232 eecd_reg &= ~IXGB_EECD_SK;
233 IXGB_WRITE_REG(hw, EECD, eecd_reg);
238 /******************************************************************************
239 * Raises then lowers the EEPROM's clock pin
241 * hw - Struct containing variables accessed by shared code
242 *****************************************************************************/
244 ixgb_clock_eeprom(struct ixgb_hw *hw)
248 eecd_reg = IXGB_READ_REG(hw, EECD);
250 /* Rising edge of clock */
251 eecd_reg |= IXGB_EECD_SK;
252 IXGB_WRITE_REG(hw, EECD, eecd_reg);
255 /* Falling edge of clock */
256 eecd_reg &= ~IXGB_EECD_SK;
257 IXGB_WRITE_REG(hw, EECD, eecd_reg);
262 /******************************************************************************
263 * Terminates a command by lowering the EEPROM's chip select pin
265 * hw - Struct containing variables accessed by shared code
266 *****************************************************************************/
268 ixgb_cleanup_eeprom(struct ixgb_hw *hw)
272 eecd_reg = IXGB_READ_REG(hw, EECD);
274 eecd_reg &= ~(IXGB_EECD_CS | IXGB_EECD_DI);
276 IXGB_WRITE_REG(hw, EECD, eecd_reg);
278 ixgb_clock_eeprom(hw);
282 /******************************************************************************
283 * Waits for the EEPROM to finish the current command.
285 * hw - Struct containing variables accessed by shared code
287 * The command is done when the EEPROM's data out pin goes high.
290 * TRUE: EEPROM data pin is high before timeout.
291 * FALSE: Time expired.
292 *****************************************************************************/
294 ixgb_wait_eeprom_command(struct ixgb_hw *hw)
300 /* Toggle the CS line. This in effect tells to EEPROM to actually execute
301 * the command in question.
303 ixgb_standby_eeprom(hw);
305 /* Now read DO repeatedly until is high (equal to '1'). The EEEPROM will
306 * signal that the command has been completed by raising the DO signal.
307 * If DO does not go high in 10 milliseconds, then error out.
309 for(i = 0; i < 200; i++) {
310 eecd_reg = IXGB_READ_REG(hw, EECD);
312 if(eecd_reg & IXGB_EECD_DO)
322 /******************************************************************************
323 * Verifies that the EEPROM has a valid checksum
325 * hw - Struct containing variables accessed by shared code
327 * Reads the first 64 16 bit words of the EEPROM and sums the values read.
328 * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
332 * TRUE: Checksum is valid
333 * FALSE: Checksum is not valid.
334 *****************************************************************************/
336 ixgb_validate_eeprom_checksum(struct ixgb_hw *hw)
338 uint16_t checksum = 0;
341 for(i = 0; i < (EEPROM_CHECKSUM_REG + 1); i++)
342 checksum += ixgb_read_eeprom(hw, i);
344 if(checksum == (uint16_t) EEPROM_SUM)
350 /******************************************************************************
351 * Calculates the EEPROM checksum and writes it to the EEPROM
353 * hw - Struct containing variables accessed by shared code
355 * Sums the first 63 16 bit words of the EEPROM. Subtracts the sum from 0xBABA.
356 * Writes the difference to word offset 63 of the EEPROM.
357 *****************************************************************************/
359 ixgb_update_eeprom_checksum(struct ixgb_hw *hw)
361 uint16_t checksum = 0;
364 for(i = 0; i < EEPROM_CHECKSUM_REG; i++)
365 checksum += ixgb_read_eeprom(hw, i);
367 checksum = (uint16_t) EEPROM_SUM - checksum;
369 ixgb_write_eeprom(hw, EEPROM_CHECKSUM_REG, checksum);
373 /******************************************************************************
374 * Writes a 16 bit word to a given offset in the EEPROM.
376 * hw - Struct containing variables accessed by shared code
377 * reg - offset within the EEPROM to be written to
378 * data - 16 bit word to be writen to the EEPROM
380 * If ixgb_update_eeprom_checksum is not called after this function, the
381 * EEPROM will most likely contain an invalid checksum.
383 *****************************************************************************/
385 ixgb_write_eeprom(struct ixgb_hw *hw,
389 /* Prepare the EEPROM for writing */
390 ixgb_setup_eeprom(hw);
392 /* Send the 9-bit EWEN (write enable) command to the EEPROM (5-bit opcode
393 * plus 4-bit dummy). This puts the EEPROM into write/erase mode.
395 ixgb_shift_out_bits(hw, EEPROM_EWEN_OPCODE, 5);
396 ixgb_shift_out_bits(hw, 0, 4);
398 /* Prepare the EEPROM */
399 ixgb_standby_eeprom(hw);
401 /* Send the Write command (3-bit opcode + 6-bit addr) */
402 ixgb_shift_out_bits(hw, EEPROM_WRITE_OPCODE, 3);
403 ixgb_shift_out_bits(hw, offset, 6);
406 ixgb_shift_out_bits(hw, data, 16);
408 ixgb_wait_eeprom_command(hw);
410 /* Recover from write */
411 ixgb_standby_eeprom(hw);
413 /* Send the 9-bit EWDS (write disable) command to the EEPROM (5-bit
414 * opcode plus 4-bit dummy). This takes the EEPROM out of write/erase
417 ixgb_shift_out_bits(hw, EEPROM_EWDS_OPCODE, 5);
418 ixgb_shift_out_bits(hw, 0, 4);
420 /* Done with writing */
421 ixgb_cleanup_eeprom(hw);
426 /******************************************************************************
427 * Reads a 16 bit word from the EEPROM.
429 * hw - Struct containing variables accessed by shared code
430 * offset - offset of 16 bit word in the EEPROM to read
433 * The 16-bit value read from the eeprom
434 *****************************************************************************/
436 ixgb_read_eeprom(struct ixgb_hw *hw,
441 /* Prepare the EEPROM for reading */
442 ixgb_setup_eeprom(hw);
444 /* Send the READ command (opcode + addr) */
445 ixgb_shift_out_bits(hw, EEPROM_READ_OPCODE, 3);
447 * We have a 64 word EEPROM, there are 6 address bits
449 ixgb_shift_out_bits(hw, offset, 6);
452 data = ixgb_shift_in_bits(hw);
454 /* End this read operation */
455 ixgb_standby_eeprom(hw);
460 /******************************************************************************
461 * Reads eeprom and stores data in shared structure.
462 * Validates eeprom checksum and eeprom signature.
464 * hw - Struct containing variables accessed by shared code
467 * TRUE: if eeprom read is successful
469 *****************************************************************************/
471 ixgb_get_eeprom_data(struct ixgb_hw *hw)
474 uint16_t checksum = 0;
475 struct ixgb_ee_map_type *ee_map;
477 DEBUGFUNC("ixgb_get_eeprom_data");
479 ee_map = (struct ixgb_ee_map_type *) hw->eeprom;
481 DEBUGOUT("ixgb_ee: Reading eeprom data\n");
482 for (i=0; i < IXGB_EEPROM_SIZE ; i++) {
484 ee_data = ixgb_read_eeprom(hw, i);
486 hw->eeprom[i] = le16_to_cpu (ee_data);
489 if (checksum != (uint16_t) EEPROM_SUM) {
490 DEBUGOUT("ixgb_ee: Checksum invalid.\n");
494 if ((ee_map->init_ctrl_reg_1 & le16_to_cpu(EEPROM_ICW1_SIGNATURE_MASK))
495 != le16_to_cpu(EEPROM_ICW1_SIGNATURE_VALID)) {
496 DEBUGOUT("ixgb_ee: Signature invalid.\n");
504 /******************************************************************************
505 * Local function to check if the eeprom signature is good
506 * If the eeprom signature is good, calls ixgb)get_eeprom_data.
508 * hw - Struct containing variables accessed by shared code
511 * TRUE: eeprom signature was good and the eeprom read was successful
513 ******************************************************************************/
515 ixgb_check_and_get_eeprom_data (struct ixgb_hw* hw)
517 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *) hw->eeprom;
520 if ((ee_map->init_ctrl_reg_1 & le16_to_cpu(EEPROM_ICW1_SIGNATURE_MASK))
521 == le16_to_cpu(EEPROM_ICW1_SIGNATURE_VALID)) {
524 return ixgb_get_eeprom_data(hw);
528 /******************************************************************************
529 * return a word from the eeprom
531 * hw - Struct containing variables accessed by shared code
532 * index - Offset of eeprom word
535 * Word at indexed offset in eeprom, if valid, 0 otherwise.
536 ******************************************************************************/
538 ixgb_get_eeprom_word(struct ixgb_hw *hw, uint16_t index)
541 if ((index < IXGB_EEPROM_SIZE) &&
542 (ixgb_check_and_get_eeprom_data (hw) == TRUE)) {
543 return(hw->eeprom[index]);
550 /******************************************************************************
551 * return the mac address from EEPROM
553 * hw - Struct containing variables accessed by shared code
554 * mac_addr - Ethernet Address if EEPROM contents are valid, 0 otherwise
557 ******************************************************************************/
559 ixgb_get_ee_mac_addr(struct ixgb_hw *hw,
563 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *) hw->eeprom;
565 DEBUGFUNC("ixgb_get_ee_mac_addr");
567 if (ixgb_check_and_get_eeprom_data (hw) == TRUE) {
568 for (i = 0; i < IXGB_ETH_LENGTH_OF_ADDRESS; i++) {
569 mac_addr[i] = ee_map->mac_addr[i];
570 DEBUGOUT2("mac(%d) = %.2X\n", i, mac_addr[i]);
575 /******************************************************************************
576 * return the compatibility flags from EEPROM
578 * hw - Struct containing variables accessed by shared code
581 * compatibility flags if EEPROM contents are valid, 0 otherwise
582 ******************************************************************************/
584 ixgb_get_ee_compatibility(struct ixgb_hw *hw)
586 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *) hw->eeprom;
588 if (ixgb_check_and_get_eeprom_data (hw) == TRUE)
589 return(ee_map->compatibility);
594 /******************************************************************************
595 * return the Printed Board Assembly number from EEPROM
597 * hw - Struct containing variables accessed by shared code
600 * PBA number if EEPROM contents are valid, 0 otherwise
601 ******************************************************************************/
603 ixgb_get_ee_pba_number(struct ixgb_hw *hw)
605 if (ixgb_check_and_get_eeprom_data (hw) == TRUE)
606 return ( le16_to_cpu(hw->eeprom[EEPROM_PBA_1_2_REG])
607 | (le16_to_cpu(hw->eeprom[EEPROM_PBA_3_4_REG])<<16));
612 /******************************************************************************
613 * return the Initialization Control Word 1 from EEPROM
615 * hw - Struct containing variables accessed by shared code
618 * Initialization Control Word 1 if EEPROM contents are valid, 0 otherwise
619 ******************************************************************************/
621 ixgb_get_ee_init_ctrl_reg_1(struct ixgb_hw *hw)
623 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *) hw->eeprom;
625 if (ixgb_check_and_get_eeprom_data (hw) == TRUE)
626 return(ee_map->init_ctrl_reg_1);
631 /******************************************************************************
632 * return the Initialization Control Word 2 from EEPROM
634 * hw - Struct containing variables accessed by shared code
637 * Initialization Control Word 2 if EEPROM contents are valid, 0 otherwise
638 ******************************************************************************/
640 ixgb_get_ee_init_ctrl_reg_2(struct ixgb_hw *hw)
642 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *) hw->eeprom;
644 if (ixgb_check_and_get_eeprom_data (hw) == TRUE)
645 return(ee_map->init_ctrl_reg_2);
650 /******************************************************************************
651 * return the Subsystem Id from EEPROM
653 * hw - Struct containing variables accessed by shared code
656 * Subsystem Id if EEPROM contents are valid, 0 otherwise
657 ******************************************************************************/
659 ixgb_get_ee_subsystem_id(struct ixgb_hw *hw)
661 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *) hw->eeprom;
663 if (ixgb_check_and_get_eeprom_data (hw) == TRUE)
664 return(ee_map->subsystem_id);
669 /******************************************************************************
670 * return the Sub Vendor Id from EEPROM
672 * hw - Struct containing variables accessed by shared code
675 * Sub Vendor Id if EEPROM contents are valid, 0 otherwise
676 ******************************************************************************/
678 ixgb_get_ee_subvendor_id(struct ixgb_hw *hw)
680 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *) hw->eeprom;
682 if (ixgb_check_and_get_eeprom_data (hw) == TRUE)
683 return(ee_map->subvendor_id);
688 /******************************************************************************
689 * return the Device Id from EEPROM
691 * hw - Struct containing variables accessed by shared code
694 * Device Id if EEPROM contents are valid, 0 otherwise
695 ******************************************************************************/
697 ixgb_get_ee_device_id(struct ixgb_hw *hw)
699 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *) hw->eeprom;
701 if (ixgb_check_and_get_eeprom_data (hw) == TRUE)
702 return(ee_map->device_id);
707 /******************************************************************************
708 * return the Vendor Id from EEPROM
710 * hw - Struct containing variables accessed by shared code
713 * Device Id if EEPROM contents are valid, 0 otherwise
714 ******************************************************************************/
716 ixgb_get_ee_vendor_id(struct ixgb_hw *hw)
718 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *) hw->eeprom;
720 if (ixgb_check_and_get_eeprom_data (hw) == TRUE)
721 return(ee_map->vendor_id);
726 /******************************************************************************
727 * return the Software Defined Pins Register from EEPROM
729 * hw - Struct containing variables accessed by shared code
732 * SDP Register if EEPROM contents are valid, 0 otherwise
733 ******************************************************************************/
735 ixgb_get_ee_swdpins_reg(struct ixgb_hw *hw)
737 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *) hw->eeprom;
739 if (ixgb_check_and_get_eeprom_data (hw) == TRUE)
740 return(ee_map->swdpins_reg);
745 /******************************************************************************
746 * return the D3 Power Management Bits from EEPROM
748 * hw - Struct containing variables accessed by shared code
751 * D3 Power Management Bits if EEPROM contents are valid, 0 otherwise
752 ******************************************************************************/
754 ixgb_get_ee_d3_power(struct ixgb_hw *hw)
756 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *) hw->eeprom;
758 if (ixgb_check_and_get_eeprom_data (hw) == TRUE)
759 return(ee_map->d3_power);
764 /******************************************************************************
765 * return the D0 Power Management Bits from EEPROM
767 * hw - Struct containing variables accessed by shared code
770 * D0 Power Management Bits if EEPROM contents are valid, 0 otherwise
771 ******************************************************************************/
773 ixgb_get_ee_d0_power(struct ixgb_hw *hw)
775 struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *) hw->eeprom;
777 if (ixgb_check_and_get_eeprom_data (hw) == TRUE)
778 return(ee_map->d0_power);