Skip to content
Snippets Groups Projects
e1000.c 155 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**************************************************************************
    
    Andre Schwarz's avatar
    Andre Schwarz committed
    Intel Pro 1000 for ppcboot/das-u-boot
    
    Drivers are port from Intel's Linux driver e1000-4.3.15
    and from Etherboot pro 1000 driver by mrakes at vivato dot net
    tested on both gig copper and gig fiber boards
    ***************************************************************************/
    /*******************************************************************************
    
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
      Copyright(c) 1999 - 2002 Intel Corporation. All rights reserved.
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
     * SPDX-License-Identifier:	GPL-2.0+
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
      Contact Information:
      Linux NICS <linux.nics@intel.com>
      Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
    
    *******************************************************************************/
    /*
     *  Copyright (C) Archway Digital Solutions.
     *
     *  written by Chrsitopher Li <cli at arcyway dot com> or <chrisl at gnuchina dot org>
     *  2/9/2002
     *
     *  Copyright (C) Linux Networx.
     *  Massive upgrade to work with the new intel gigabit NICs.
     *  <ebiederman at lnxi dot com>
    
     *
     *  Copyright 2011 Freescale Semiconductor, Inc.
    
     */
    
    #include "e1000.h"
    
    #define TOUT_LOOP   100000
    
    
    #define virt_to_bus(devno, v)	pci_virt_to_mem(devno, (void *) (v))
    
    #define bus_to_phys(devno, a)	pci_mem_to_phys(devno, a)
    
    
    #define E1000_DEFAULT_PCI_PBA	0x00000030
    #define E1000_DEFAULT_PCIE_PBA	0x000a0026
    
    
    /* NIC specific static variables go here */
    
    
    /* Intel i210 needs the DMA descriptor rings aligned to 128b */
    #define E1000_BUFFER_ALIGN	128
    
    DEFINE_ALIGN_BUFFER(struct e1000_tx_desc, tx_base, 16, E1000_BUFFER_ALIGN);
    DEFINE_ALIGN_BUFFER(struct e1000_rx_desc, rx_base, 16, E1000_BUFFER_ALIGN);
    DEFINE_ALIGN_BUFFER(unsigned char, packet, 4096, E1000_BUFFER_ALIGN);
    
    
    static int tx_tail;
    static int rx_tail, rx_last;
    
    
    static struct pci_device_id e1000_supported[] = {
    
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82542},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_FIBER},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_COPPER},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_COPPER},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_FIBER},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_COPPER},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_LOM},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_COPPER},
    
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545GM_COPPER},
    
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_COPPER},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_FIBER},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_FIBER},
    
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_COPPER},
    
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM_LOM},
    
    Andre Schwarz's avatar
    Andre Schwarz committed
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541ER},
    
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541GI_LF},
    
    	/* E1000 PCIe card */
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_COPPER},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_FIBER      },
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES     },
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571PT_QUAD_COPPER},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_FIBER},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER_LOWPROFILE},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_DUAL},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_QUAD},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_COPPER},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_FIBER},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_SERDES},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E_IAMT},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573L},
    
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82574L},
    
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_QUAD_COPPER_KSP3},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_DPT},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_DPT},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_SPT},
    	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_SPT},
    
    };
    
    /* Function forward declarations */
    static int e1000_setup_link(struct eth_device *nic);
    static int e1000_setup_fiber_link(struct eth_device *nic);
    static int e1000_setup_copper_link(struct eth_device *nic);
    static int e1000_phy_setup_autoneg(struct e1000_hw *hw);
    static void e1000_config_collision_dist(struct e1000_hw *hw);
    static int e1000_config_mac_to_phy(struct e1000_hw *hw);
    static int e1000_config_fc_after_link_up(struct e1000_hw *hw);
    static int e1000_check_for_link(struct eth_device *nic);
    static int e1000_wait_autoneg(struct e1000_hw *hw);
    
    static int e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t * speed,
    
    				       uint16_t * duplex);
    static int e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
    			      uint16_t * phy_data);
    static int e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
    			       uint16_t phy_data);
    
    static int32_t e1000_phy_hw_reset(struct e1000_hw *hw);
    
    static int e1000_phy_reset(struct e1000_hw *hw);
    static int e1000_detect_gig_phy(struct e1000_hw *hw);
    
    static void e1000_set_media_type(struct e1000_hw *hw);
    
    static int32_t e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask);
    static int32_t e1000_check_phy_reset_block(struct e1000_hw *hw);
    
    #ifndef CONFIG_E1000_NO_NVM
    static void e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw);
    
    static int32_t e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
    		uint16_t words,
    		uint16_t *data);
    
    /******************************************************************************
     * Raises the EEPROM's clock input.
     *
     * hw - Struct containing variables accessed by shared code
     * eecd - EECD's current value
     *****************************************************************************/
    
    void e1000_raise_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
    
    {
    	/* Raise the clock input to the EEPROM (by setting the SK bit), and then
    	 * wait 50 microseconds.
    	 */
    	*eecd = *eecd | E1000_EECD_SK;
    	E1000_WRITE_REG(hw, EECD, *eecd);
    	E1000_WRITE_FLUSH(hw);
    	udelay(50);
    }
    
    /******************************************************************************
     * Lowers the EEPROM's clock input.
     *
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
     * hw - Struct containing variables accessed by shared code
    
     * eecd - EECD's current value
     *****************************************************************************/
    
    void e1000_lower_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	/* Lower the clock input to the EEPROM (by clearing the SK bit), and then
    	 * wait 50 microseconds.
    
    	 */
    	*eecd = *eecd & ~E1000_EECD_SK;
    	E1000_WRITE_REG(hw, EECD, *eecd);
    	E1000_WRITE_FLUSH(hw);
    	udelay(50);
    }
    
    /******************************************************************************
     * Shift data bits out to the EEPROM.
     *
     * hw - Struct containing variables accessed by shared code
     * data - data to send to the EEPROM
     * count - number of bits to shift out
     *****************************************************************************/
    static void
    e1000_shift_out_ee_bits(struct e1000_hw *hw, uint16_t data, uint16_t count)
    {
    	uint32_t eecd;
    	uint32_t mask;
    
    	/* We need to shift "count" bits out to the EEPROM. So, value in the
    	 * "data" parameter will be shifted out to the EEPROM one bit at a time.
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	 * In order to do this, "data" must be broken down into bits.
    
    	 */
    	mask = 0x01 << (count - 1);
    	eecd = E1000_READ_REG(hw, EECD);
    	eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
    	do {
    		/* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
    		 * and then raising and then lowering the clock (the SK bit controls
    		 * the clock input to the EEPROM).  A "0" is shifted out to the EEPROM
    		 * by setting "DI" to "0" and then raising and then lowering the clock.
    		 */
    		eecd &= ~E1000_EECD_DI;
    
    		if (data & mask)
    			eecd |= E1000_EECD_DI;
    
    		E1000_WRITE_REG(hw, EECD, eecd);
    		E1000_WRITE_FLUSH(hw);
    
    		udelay(50);
    
    		e1000_raise_ee_clk(hw, &eecd);
    		e1000_lower_ee_clk(hw, &eecd);
    
    		mask = mask >> 1;
    
    	} while (mask);
    
    	/* We leave the "DI" bit set to "0" when we leave this routine. */
    	eecd &= ~E1000_EECD_DI;
    	E1000_WRITE_REG(hw, EECD, eecd);
    }
    
    /******************************************************************************
     * Shift data bits in from the EEPROM
     *
     * hw - Struct containing variables accessed by shared code
     *****************************************************************************/
    static uint16_t
    
    e1000_shift_in_ee_bits(struct e1000_hw *hw, uint16_t count)
    
    {
    	uint32_t eecd;
    	uint32_t i;
    	uint16_t data;
    
    
    	/* In order to read a register from the EEPROM, we need to shift 'count'
    	 * bits in from the EEPROM. Bits are "shifted in" by raising the clock
    	 * input to the EEPROM (setting the SK bit), and then reading the
    	 * value of the "DO" bit.  During this "shifting in" process the
    	 * "DI" bit should always be clear.
    
    	 */
    
    	eecd = E1000_READ_REG(hw, EECD);
    
    	eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
    	data = 0;
    
    
    	for (i = 0; i < count; i++) {
    
    		data = data << 1;
    		e1000_raise_ee_clk(hw, &eecd);
    
    		eecd = E1000_READ_REG(hw, EECD);
    
    		eecd &= ~(E1000_EECD_DI);
    		if (eecd & E1000_EECD_DO)
    			data |= 1;
    
    		e1000_lower_ee_clk(hw, &eecd);
    	}
    
    	return data;
    }
    
    /******************************************************************************
    
     * Returns EEPROM to a "standby" state
    
     *
     * hw - Struct containing variables accessed by shared code
     *****************************************************************************/
    
    void e1000_standby_eeprom(struct e1000_hw *hw)
    
    	struct e1000_eeprom_info *eeprom = &hw->eeprom;
    
    	uint32_t eecd;
    
    	eecd = E1000_READ_REG(hw, EECD);
    
    
    	if (eeprom->type == e1000_eeprom_microwire) {
    		eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
    		E1000_WRITE_REG(hw, EECD, eecd);
    		E1000_WRITE_FLUSH(hw);
    		udelay(eeprom->delay_usec);
    
    		/* Clock high */
    		eecd |= E1000_EECD_SK;
    		E1000_WRITE_REG(hw, EECD, eecd);
    		E1000_WRITE_FLUSH(hw);
    		udelay(eeprom->delay_usec);
    
    		/* Select EEPROM */
    		eecd |= E1000_EECD_CS;
    		E1000_WRITE_REG(hw, EECD, eecd);
    		E1000_WRITE_FLUSH(hw);
    		udelay(eeprom->delay_usec);
    
    		/* Clock low */
    		eecd &= ~E1000_EECD_SK;
    		E1000_WRITE_REG(hw, EECD, eecd);
    		E1000_WRITE_FLUSH(hw);
    		udelay(eeprom->delay_usec);
    	} else if (eeprom->type == e1000_eeprom_spi) {
    		/* Toggle CS to flush commands */
    		eecd |= E1000_EECD_CS;
    		E1000_WRITE_REG(hw, EECD, eecd);
    		E1000_WRITE_FLUSH(hw);
    		udelay(eeprom->delay_usec);
    		eecd &= ~E1000_EECD_CS;
    		E1000_WRITE_REG(hw, EECD, eecd);
    		E1000_WRITE_FLUSH(hw);
    		udelay(eeprom->delay_usec);
    	}
    }
    
    /***************************************************************************
    * Description:     Determines if the onboard NVM is FLASH or EEPROM.
    *
    * hw - Struct containing variables accessed by shared code
    ****************************************************************************/
    
    York Sun's avatar
    York Sun committed
    static bool e1000_is_onboard_nvm_eeprom(struct e1000_hw *hw)
    
    {
    	uint32_t eecd = 0;
    
    	DEBUGFUNC();
    
    	if (hw->mac_type == e1000_ich8lan)
    
    York Sun's avatar
    York Sun committed
    		return false;
    
    	if (hw->mac_type == e1000_82573 || hw->mac_type == e1000_82574) {
    
    		eecd = E1000_READ_REG(hw, EECD);
    
    		/* Isolate bits 15 & 16 */
    		eecd = ((eecd >> 15) & 0x03);
    
    		/* If both bits are set, device is Flash type */
    		if (eecd == 0x03)
    
    York Sun's avatar
    York Sun committed
    			return false;
    
    York Sun's avatar
    York Sun committed
    	return true;
    
    }
    
    /******************************************************************************
    
     * Prepares EEPROM for access
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
     *
    
     * hw - Struct containing variables accessed by shared code
    
     *
     * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
     * function should be called before issuing a command to the EEPROM.
    
     *****************************************************************************/
    
    int32_t e1000_acquire_eeprom(struct e1000_hw *hw)
    
    	struct e1000_eeprom_info *eeprom = &hw->eeprom;
    	uint32_t eecd, i = 0;
    
    	DEBUGFUNC();
    
    
    	if (e1000_swfw_sync_acquire(hw, E1000_SWFW_EEP_SM))
    		return -E1000_ERR_SWFW_SYNC;
    
    	eecd = E1000_READ_REG(hw, EECD);
    
    
    	if (hw->mac_type != e1000_82573 || hw->mac_type != e1000_82574) {
    
    		/* Request EEPROM Access */
    		if (hw->mac_type > e1000_82544) {
    			eecd |= E1000_EECD_REQ;
    			E1000_WRITE_REG(hw, EECD, eecd);
    			eecd = E1000_READ_REG(hw, EECD);
    			while ((!(eecd & E1000_EECD_GNT)) &&
    				(i < E1000_EEPROM_GRANT_ATTEMPTS)) {
    				i++;
    				udelay(5);
    				eecd = E1000_READ_REG(hw, EECD);
    			}
    			if (!(eecd & E1000_EECD_GNT)) {
    				eecd &= ~E1000_EECD_REQ;
    				E1000_WRITE_REG(hw, EECD, eecd);
    				DEBUGOUT("Could not acquire EEPROM grant\n");
    				return -E1000_ERR_EEPROM;
    			}
    		}
    	}
    
    	/* Setup EEPROM for Read/Write */
    
    	if (eeprom->type == e1000_eeprom_microwire) {
    		/* Clear SK and DI */
    		eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
    		E1000_WRITE_REG(hw, EECD, eecd);
    
    		/* Set CS */
    		eecd |= E1000_EECD_CS;
    		E1000_WRITE_REG(hw, EECD, eecd);
    	} else if (eeprom->type == e1000_eeprom_spi) {
    		/* Clear SK and CS */
    		eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
    		E1000_WRITE_REG(hw, EECD, eecd);
    		udelay(1);
    	}
    
    	return E1000_SUCCESS;
    
    }
    
    /******************************************************************************
    
     * Sets up eeprom variables in the hw struct.  Must be called after mac_type
     * is configured.  Additionally, if this is ICH8, the flash controller GbE
     * registers must be mapped, or this will crash.
    
     *
     * hw - Struct containing variables accessed by shared code
     *****************************************************************************/
    
    static int32_t e1000_init_eeprom_params(struct e1000_hw *hw)
    
    	struct e1000_eeprom_info *eeprom = &hw->eeprom;
    	uint32_t eecd = E1000_READ_REG(hw, EECD);
    	int32_t ret_val = E1000_SUCCESS;
    	uint16_t eeprom_size;
    
    	DEBUGFUNC();
    
    
    	switch (hw->mac_type) {
    	case e1000_82542_rev2_0:
    	case e1000_82542_rev2_1:
    	case e1000_82543:
    	case e1000_82544:
    		eeprom->type = e1000_eeprom_microwire;
    		eeprom->word_size = 64;
    		eeprom->opcode_bits = 3;
    		eeprom->address_bits = 6;
    		eeprom->delay_usec = 50;
    
    York Sun's avatar
    York Sun committed
    		eeprom->use_eerd = false;
    		eeprom->use_eewr = false;
    
    	break;
    	case e1000_82540:
    	case e1000_82545:
    	case e1000_82545_rev_3:
    	case e1000_82546:
    	case e1000_82546_rev_3:
    		eeprom->type = e1000_eeprom_microwire;
    		eeprom->opcode_bits = 3;
    		eeprom->delay_usec = 50;
    		if (eecd & E1000_EECD_SIZE) {
    			eeprom->word_size = 256;
    			eeprom->address_bits = 8;
    		} else {
    			eeprom->word_size = 64;
    			eeprom->address_bits = 6;
    		}
    
    York Sun's avatar
    York Sun committed
    		eeprom->use_eerd = false;
    		eeprom->use_eewr = false;
    
    		break;
    	case e1000_82541:
    	case e1000_82541_rev_2:
    	case e1000_82547:
    	case e1000_82547_rev_2:
    		if (eecd & E1000_EECD_TYPE) {
    			eeprom->type = e1000_eeprom_spi;
    			eeprom->opcode_bits = 8;
    			eeprom->delay_usec = 1;
    			if (eecd & E1000_EECD_ADDR_BITS) {
    				eeprom->page_size = 32;
    				eeprom->address_bits = 16;
    			} else {
    				eeprom->page_size = 8;
    				eeprom->address_bits = 8;
    			}
    		} else {
    			eeprom->type = e1000_eeprom_microwire;
    			eeprom->opcode_bits = 3;
    			eeprom->delay_usec = 50;
    			if (eecd & E1000_EECD_ADDR_BITS) {
    				eeprom->word_size = 256;
    				eeprom->address_bits = 8;
    			} else {
    				eeprom->word_size = 64;
    				eeprom->address_bits = 6;
    			}
    		}
    
    York Sun's avatar
    York Sun committed
    		eeprom->use_eerd = false;
    		eeprom->use_eewr = false;
    
    		break;
    	case e1000_82571:
    	case e1000_82572:
    		eeprom->type = e1000_eeprom_spi;
    		eeprom->opcode_bits = 8;
    		eeprom->delay_usec = 1;
    		if (eecd & E1000_EECD_ADDR_BITS) {
    			eeprom->page_size = 32;
    			eeprom->address_bits = 16;
    		} else {
    			eeprom->page_size = 8;
    			eeprom->address_bits = 8;
    		}
    
    York Sun's avatar
    York Sun committed
    		eeprom->use_eerd = false;
    		eeprom->use_eewr = false;
    
    		break;
    	case e1000_82573:
    
    	case e1000_82574:
    
    		eeprom->type = e1000_eeprom_spi;
    		eeprom->opcode_bits = 8;
    		eeprom->delay_usec = 1;
    		if (eecd & E1000_EECD_ADDR_BITS) {
    			eeprom->page_size = 32;
    			eeprom->address_bits = 16;
    		} else {
    			eeprom->page_size = 8;
    			eeprom->address_bits = 8;
    
    York Sun's avatar
    York Sun committed
    		eeprom->use_eerd = true;
    		eeprom->use_eewr = true;
    		if (e1000_is_onboard_nvm_eeprom(hw) == false) {
    
    			eeprom->type = e1000_eeprom_flash;
    			eeprom->word_size = 2048;
    
    		/* Ensure that the Autonomous FLASH update bit is cleared due to
    		 * Flash update issue on parts which use a FLASH for NVM. */
    			eecd &= ~E1000_EECD_AUPDEN;
    
    			E1000_WRITE_REG(hw, EECD, eecd);
    		}
    
    		break;
    	case e1000_80003es2lan:
    		eeprom->type = e1000_eeprom_spi;
    		eeprom->opcode_bits = 8;
    		eeprom->delay_usec = 1;
    		if (eecd & E1000_EECD_ADDR_BITS) {
    			eeprom->page_size = 32;
    			eeprom->address_bits = 16;
    		} else {
    			eeprom->page_size = 8;
    			eeprom->address_bits = 8;
    		}
    
    York Sun's avatar
    York Sun committed
    		eeprom->use_eerd = true;
    		eeprom->use_eewr = false;
    
    		break;
    
    	/* ich8lan does not support currently. if needed, please
    	 * add corresponding code and functions.
    	 */
    #if 0
    	case e1000_ich8lan:
    		{
    		int32_t  i = 0;
    
    		eeprom->type = e1000_eeprom_ich8;
    
    York Sun's avatar
    York Sun committed
    		eeprom->use_eerd = false;
    		eeprom->use_eewr = false;
    
    		eeprom->word_size = E1000_SHADOW_RAM_WORDS;
    		uint32_t flash_size = E1000_READ_ICH_FLASH_REG(hw,
    				ICH_FLASH_GFPREG);
    		/* Zero the shadow RAM structure. But don't load it from NVM
    		 * so as to save time for driver init */
    		if (hw->eeprom_shadow_ram != NULL) {
    			for (i = 0; i < E1000_SHADOW_RAM_WORDS; i++) {
    
    York Sun's avatar
    York Sun committed
    				hw->eeprom_shadow_ram[i].modified = false;
    
    				hw->eeprom_shadow_ram[i].eeprom_word = 0xFFFF;
    			}
    		}
    
    		hw->flash_base_addr = (flash_size & ICH_GFPREG_BASE_MASK) *
    				ICH_FLASH_SECTOR_SIZE;
    
    		hw->flash_bank_size = ((flash_size >> 16)
    				& ICH_GFPREG_BASE_MASK) + 1;
    		hw->flash_bank_size -= (flash_size & ICH_GFPREG_BASE_MASK);
    
    		hw->flash_bank_size *= ICH_FLASH_SECTOR_SIZE;
    
    		hw->flash_bank_size /= 2 * sizeof(uint16_t);
    		break;
    		}
    #endif
    	default:
    		break;
    
    	if (eeprom->type == e1000_eeprom_spi) {
    		/* eeprom_size will be an enum [0..8] that maps
    		 * to eeprom sizes 128B to
    		 * 32KB (incremented by powers of 2).
    		 */
    		if (hw->mac_type <= e1000_82547_rev_2) {
    			/* Set to default value for initial eeprom read. */
    			eeprom->word_size = 64;
    			ret_val = e1000_read_eeprom(hw, EEPROM_CFG, 1,
    					&eeprom_size);
    			if (ret_val)
    				return ret_val;
    			eeprom_size = (eeprom_size & EEPROM_SIZE_MASK)
    				>> EEPROM_SIZE_SHIFT;
    			/* 256B eeprom size was not supported in earlier
    			 * hardware, so we bump eeprom_size up one to
    			 * ensure that "1" (which maps to 256B) is never
    			 * the result used in the shifting logic below. */
    			if (eeprom_size)
    				eeprom_size++;
    		} else {
    			eeprom_size = (uint16_t)((eecd &
    				E1000_EECD_SIZE_EX_MASK) >>
    				E1000_EECD_SIZE_EX_SHIFT);
    		}
    
    		eeprom->word_size = 1 << (eeprom_size + EEPROM_WORD_SIZE_SHIFT);
    	}
    	return ret_val;
    
    /******************************************************************************
     * Polls the status bit (bit 1) of the EERD to determine when the read is done.
     *
     * hw - Struct containing variables accessed by shared code
     *****************************************************************************/
    static int32_t
    e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd)
    
    	uint32_t attempts = 100000;
    	uint32_t i, reg = 0;
    	int32_t done = E1000_ERR_EEPROM;
    
    	for (i = 0; i < attempts; i++) {
    		if (eerd == E1000_EEPROM_POLL_READ)
    			reg = E1000_READ_REG(hw, EERD);
    		else
    			reg = E1000_READ_REG(hw, EEWR);
    
    		if (reg & E1000_EEPROM_RW_REG_DONE) {
    			done = E1000_SUCCESS;
    			break;
    		}
    		udelay(5);
    	}
    
    	return done;
    
    /******************************************************************************
     * Reads a 16 bit word from the EEPROM using the EERD register.
     *
     * hw - Struct containing variables accessed by shared code
     * offset - offset of  word in the EEPROM to read
     * data - word read from the EEPROM
     * words - number of words to read
     *****************************************************************************/
    static int32_t
    e1000_read_eeprom_eerd(struct e1000_hw *hw,
    			uint16_t offset,
    			uint16_t words,
    			uint16_t *data)
    
    	uint32_t i, eerd = 0;
    	int32_t error = 0;
    
    	for (i = 0; i < words; i++) {
    		eerd = ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) +
    			E1000_EEPROM_RW_REG_START;
    
    		E1000_WRITE_REG(hw, EERD, eerd);
    		error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_READ);
    
    		if (error)
    			break;
    		data[i] = (E1000_READ_REG(hw, EERD) >>
    				E1000_EEPROM_RW_REG_DATA);
    
    
    	return error;
    
    void e1000_release_eeprom(struct e1000_hw *hw)
    
    	DEBUGFUNC();
    
    	eecd = E1000_READ_REG(hw, EECD);
    
    	if (hw->eeprom.type == e1000_eeprom_spi) {
    		eecd |= E1000_EECD_CS;  /* Pull CS high */
    		eecd &= ~E1000_EECD_SK; /* Lower SCK */
    
    
    		E1000_WRITE_REG(hw, EECD, eecd);
    
    
    		udelay(hw->eeprom.delay_usec);
    	} else if (hw->eeprom.type == e1000_eeprom_microwire) {
    		/* cleanup eeprom */
    
    		/* CS on Microwire is active-high */
    		eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
    
    		E1000_WRITE_REG(hw, EECD, eecd);
    
    		/* Rising edge of clock */
    		eecd |= E1000_EECD_SK;
    		E1000_WRITE_REG(hw, EECD, eecd);
    		E1000_WRITE_FLUSH(hw);
    		udelay(hw->eeprom.delay_usec);
    
    		/* Falling edge of clock */
    		eecd &= ~E1000_EECD_SK;
    		E1000_WRITE_REG(hw, EECD, eecd);
    		E1000_WRITE_FLUSH(hw);
    		udelay(hw->eeprom.delay_usec);
    
    	}
    
    	/* Stop requesting EEPROM access */
    	if (hw->mac_type > e1000_82544) {
    		eecd &= ~E1000_EECD_REQ;
    		E1000_WRITE_REG(hw, EECD, eecd);
    	}
    }
    /******************************************************************************
    
     * Reads a 16 bit word from the EEPROM.
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
     *
    
     * hw - Struct containing variables accessed by shared code
     *****************************************************************************/
    
    static int32_t
    e1000_spi_eeprom_ready(struct e1000_hw *hw)
    
    	uint16_t retry_count = 0;
    	uint8_t spi_stat_reg;
    
    	/* Read "Status Register" repeatedly until the LSB is cleared.  The
    	 * EEPROM will signal that the command has been completed by clearing
    	 * bit 0 of the internal status register.  If it's not cleared within
    	 * 5 milliseconds, then error out.
    	 */
    	retry_count = 0;
    	do {
    		e1000_shift_out_ee_bits(hw, EEPROM_RDSR_OPCODE_SPI,
    			hw->eeprom.opcode_bits);
    		spi_stat_reg = (uint8_t)e1000_shift_in_ee_bits(hw, 8);
    		if (!(spi_stat_reg & EEPROM_STATUS_RDY_SPI))
    			break;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    		udelay(5);
    		retry_count += 5;
    
    		e1000_standby_eeprom(hw);
    	} while (retry_count < EEPROM_MAX_RETRY_SPI);
    
    	/* ATMEL SPI write time could vary from 0-20mSec on 3.3V devices (and
    	 * only 0-5mSec on 5V devices)
    	 */
    	if (retry_count >= EEPROM_MAX_RETRY_SPI) {
    		DEBUGOUT("SPI EEPROM Status error\n");
    
    		return -E1000_ERR_EEPROM;
    	}
    
    
    	return E1000_SUCCESS;
    
    }
    
    /******************************************************************************
    
     * Reads a 16 bit word from the EEPROM.
    
     * hw - Struct containing variables accessed by shared code
     * offset - offset of  word in the EEPROM to read
     * data - word read from the EEPROM
    
     *****************************************************************************/
    
    static int32_t
    e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
    		uint16_t words, uint16_t *data)
    
    	struct e1000_eeprom_info *eeprom = &hw->eeprom;
    	uint32_t i = 0;
    
    	/* If eeprom is not yet detected, do so now */
    	if (eeprom->word_size == 0)
    		e1000_init_eeprom_params(hw);
    
    	/* A check for invalid values:  offset too large, too many words,
    	 * and not enough words.
    	 */
    	if ((offset >= eeprom->word_size) ||
    		(words > eeprom->word_size - offset) ||
    		(words == 0)) {
    		DEBUGOUT("\"words\" parameter out of bounds."
    			"Words = %d, size = %d\n", offset, eeprom->word_size);
    		return -E1000_ERR_EEPROM;
    	}
    
    	/* EEPROM's that don't use EERD to read require us to bit-bang the SPI
    	 * directly. In this case, we need to acquire the EEPROM so that
    	 * FW or other port software does not interrupt.
    	 */
    
    York Sun's avatar
    York Sun committed
    	if (e1000_is_onboard_nvm_eeprom(hw) == true &&
    		hw->eeprom.use_eerd == false) {
    
    
    		/* Prepare the EEPROM for bit-bang reading */
    		if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
    			return -E1000_ERR_EEPROM;
    	}
    
    	/* Eerd register EEPROM access requires no eeprom aquire/release */
    
    York Sun's avatar
    York Sun committed
    	if (eeprom->use_eerd == true)
    
    		return e1000_read_eeprom_eerd(hw, offset, words, data);
    
    	/* ich8lan does not support currently. if needed, please
    	 * add corresponding code and functions.
    	 */
    #if 0
    	/* ICH EEPROM access is done via the ICH flash controller */
    	if (eeprom->type == e1000_eeprom_ich8)
    		return e1000_read_eeprom_ich8(hw, offset, words, data);
    #endif
    	/* Set up the SPI or Microwire EEPROM for bit-bang reading.  We have
    	 * acquired the EEPROM at this point, so any returns should relase it */
    	if (eeprom->type == e1000_eeprom_spi) {
    		uint16_t word_in;
    		uint8_t read_opcode = EEPROM_READ_OPCODE_SPI;
    
    		if (e1000_spi_eeprom_ready(hw)) {
    			e1000_release_eeprom(hw);
    			return -E1000_ERR_EEPROM;
    		}
    
    		e1000_standby_eeprom(hw);
    
    		/* Some SPI eeproms use the 8th address bit embedded in
    		 * the opcode */
    		if ((eeprom->address_bits == 8) && (offset >= 128))
    			read_opcode |= EEPROM_A8_OPCODE_SPI;
    
    		/* Send the READ command (opcode + addr)  */
    		e1000_shift_out_ee_bits(hw, read_opcode, eeprom->opcode_bits);
    		e1000_shift_out_ee_bits(hw, (uint16_t)(offset*2),
    				eeprom->address_bits);
    
    		/* Read the data.  The address of the eeprom internally
    		 * increments with each byte (spi) being read, saving on the
    		 * overhead of eeprom setup and tear-down.  The address
    		 * counter will roll over if reading beyond the size of
    		 * the eeprom, thus allowing the entire memory to be read
    		 * starting from any offset. */
    		for (i = 0; i < words; i++) {
    			word_in = e1000_shift_in_ee_bits(hw, 16);
    			data[i] = (word_in >> 8) | (word_in << 8);
    		}
    	} else if (eeprom->type == e1000_eeprom_microwire) {
    		for (i = 0; i < words; i++) {
    			/* Send the READ command (opcode + addr)  */
    			e1000_shift_out_ee_bits(hw,
    				EEPROM_READ_OPCODE_MICROWIRE,
    				eeprom->opcode_bits);
    			e1000_shift_out_ee_bits(hw, (uint16_t)(offset + i),
    				eeprom->address_bits);
    
    			/* Read the data.  For microwire, each word requires
    			 * the overhead of eeprom setup and tear-down. */
    			data[i] = e1000_shift_in_ee_bits(hw, 16);
    			e1000_standby_eeprom(hw);
    		}
    	}
    
    	/* End this read operation */
    	e1000_release_eeprom(hw);
    
    	return E1000_SUCCESS;
    }
    
    /******************************************************************************
     * Verifies that the EEPROM has a valid checksum
     *
     * hw - Struct containing variables accessed by shared code
     *
     * Reads the first 64 16 bit words of the EEPROM and sums the values read.
     * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
     * valid.
     *****************************************************************************/
    
    static int e1000_validate_eeprom_checksum(struct e1000_hw *hw)
    
    	uint16_t i, checksum, checksum_reg, *buf;
    
    	/* Allocate a temporary buffer */
    	buf = malloc(sizeof(buf[0]) * (EEPROM_CHECKSUM_REG + 1));
    	if (!buf) {
    		E1000_ERR(hw->nic, "Unable to allocate EEPROM buffer!\n");
    		return -E1000_ERR_EEPROM;
    
    	/* Read the EEPROM */
    	if (e1000_read_eeprom(hw, 0, EEPROM_CHECKSUM_REG + 1, buf) < 0) {
    		E1000_ERR(hw->nic, "Unable to read EEPROM!\n");
    
    		return -E1000_ERR_EEPROM;
    	}
    
    	checksum = 0;
    
    	for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
    		checksum += buf[i];
    	checksum = ((uint16_t)EEPROM_SUM) - checksum;
    	checksum_reg = buf[i];
    
    	/* Verify it! */
    	if (checksum == checksum_reg)
    		return 0;
    
    	/* Hrm, verification failed, print an error */
    	E1000_ERR(hw->nic, "EEPROM checksum is incorrect!\n");
    	E1000_ERR(hw->nic, "  ...register was 0x%04hx, calculated 0x%04hx\n",
    			checksum_reg, checksum);
    
    	return -E1000_ERR_EEPROM;
    
    #endif /* CONFIG_E1000_NO_NVM */
    
    
    /*****************************************************************************
     * Set PHY to class A mode
     * Assumes the following operations will follow to enable the new class mode.
     *  1. Do a PHY soft reset
     *  2. Restart auto-negotiation or force link.
     *
     * hw - Struct containing variables accessed by shared code
     ****************************************************************************/
    static int32_t
    e1000_set_phy_mode(struct e1000_hw *hw)
    {
    
    #ifndef CONFIG_E1000_NO_NVM
    
    	int32_t ret_val;
    	uint16_t eeprom_data;
    
    	DEBUGFUNC();
    
    	if ((hw->mac_type == e1000_82545_rev_3) &&
    		(hw->media_type == e1000_media_type_copper)) {
    		ret_val = e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD,
    				1, &eeprom_data);
    		if (ret_val)
    			return ret_val;
    
    		if ((eeprom_data != EEPROM_RESERVED_WORD) &&
    			(eeprom_data & EEPROM_PHY_CLASS_A)) {
    			ret_val = e1000_write_phy_reg(hw,
    					M88E1000_PHY_PAGE_SELECT, 0x000B);
    			if (ret_val)
    				return ret_val;
    			ret_val = e1000_write_phy_reg(hw,
    					M88E1000_PHY_GEN_CONTROL, 0x8104);
    			if (ret_val)
    				return ret_val;
    
    
    York Sun's avatar
    York Sun committed
    			hw->phy_reset_disable = false;
    
    	return E1000_SUCCESS;
    }
    
    #ifndef CONFIG_E1000_NO_NVM
    
    /***************************************************************************
     *
     * Obtaining software semaphore bit (SMBI) before resetting PHY.
     *
     * hw: Struct containing variables accessed by shared code
     *
     * returns: - E1000_ERR_RESET if fail to obtain semaphore.
     *            E1000_SUCCESS at any other case.
     *
     ***************************************************************************/
    static int32_t
    e1000_get_software_semaphore(struct e1000_hw *hw)
    {
    	 int32_t timeout = hw->eeprom.word_size + 1;
    	 uint32_t swsm;
    
    	DEBUGFUNC();
    
    	if (hw->mac_type != e1000_80003es2lan)
    		return E1000_SUCCESS;
    
    	while (timeout) {
    		swsm = E1000_READ_REG(hw, SWSM);
    		/* If SMBI bit cleared, it is now set and we hold
    		 * the semaphore */
    		if (!(swsm & E1000_SWSM_SMBI))
    			break;
    		mdelay(1);
    		timeout--;
    	}
    
    	if (!timeout) {
    		DEBUGOUT("Driver can't access device - SMBI bit is set.\n");
    		return -E1000_ERR_RESET;
    	}
    
    	return E1000_SUCCESS;
    }
    
    
    /***************************************************************************
     * This function clears HW semaphore bits.
     *
     * hw: Struct containing variables accessed by shared code
     *
     * returns: - None.
     *
     ***************************************************************************/
    static void
    e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw)
    {
    
    #ifndef CONFIG_E1000_NO_NVM
    
    	 uint32_t swsm;
    
    	DEBUGFUNC();
    
    	if (!hw->eeprom_semaphore_present)
    		return;
    
    	swsm = E1000_READ_REG(hw, SWSM);
    	if (hw->mac_type == e1000_80003es2lan) {
    		/* Release both semaphores. */
    		swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
    	} else
    		swsm &= ~(E1000_SWSM_SWESMBI);
    	E1000_WRITE_REG(hw, SWSM, swsm);