uboot/drivers/net/e1000.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/**************************************************************************
   3Intel Pro 1000 for ppcboot/das-u-boot
   4Drivers are port from Intel's Linux driver e1000-4.3.15
   5and from Etherboot pro 1000 driver by mrakes at vivato dot net
   6tested on both gig copper and gig fiber boards
   7***************************************************************************/
   8/*******************************************************************************
   9
  10
  11  Copyright(c) 1999 - 2002 Intel Corporation. All rights reserved.
  12
  13
  14  Contact Information:
  15  Linux NICS <linux.nics@intel.com>
  16  Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  17
  18*******************************************************************************/
  19/*
  20 *  Copyright (C) Archway Digital Solutions.
  21 *
  22 *  written by Chrsitopher Li <cli at arcyway dot com> or <chrisl at gnuchina dot org>
  23 *  2/9/2002
  24 *
  25 *  Copyright (C) Linux Networx.
  26 *  Massive upgrade to work with the new intel gigabit NICs.
  27 *  <ebiederman at lnxi dot com>
  28 *
  29 *  Copyright 2011 Freescale Semiconductor, Inc.
  30 */
  31
  32#include <common.h>
  33#include <cpu_func.h>
  34#include <dm.h>
  35#include <errno.h>
  36#include <memalign.h>
  37#include <pci.h>
  38#include "e1000.h"
  39
  40#define TOUT_LOOP   100000
  41
  42#ifdef CONFIG_DM_ETH
  43#define virt_to_bus(devno, v)   dm_pci_virt_to_mem(devno, (void *) (v))
  44#define bus_to_phys(devno, a)   dm_pci_mem_to_phys(devno, a)
  45#else
  46#define virt_to_bus(devno, v)   pci_virt_to_mem(devno, (void *) (v))
  47#define bus_to_phys(devno, a)   pci_mem_to_phys(devno, a)
  48#endif
  49
  50#define E1000_DEFAULT_PCI_PBA   0x00000030
  51#define E1000_DEFAULT_PCIE_PBA  0x000a0026
  52
  53/* NIC specific static variables go here */
  54
  55/* Intel i210 needs the DMA descriptor rings aligned to 128b */
  56#define E1000_BUFFER_ALIGN      128
  57
  58/*
  59 * TODO(sjg@chromium.org): Even with driver model we share these buffers.
  60 * Concurrent receiving on multiple active Ethernet devices will not work.
  61 * Normally U-Boot does not support this anyway. To fix it in this driver,
  62 * move these buffers and the tx/rx pointers to struct e1000_hw.
  63 */
  64DEFINE_ALIGN_BUFFER(struct e1000_tx_desc, tx_base, 16, E1000_BUFFER_ALIGN);
  65DEFINE_ALIGN_BUFFER(struct e1000_rx_desc, rx_base, 16, E1000_BUFFER_ALIGN);
  66DEFINE_ALIGN_BUFFER(unsigned char, packet, 4096, E1000_BUFFER_ALIGN);
  67
  68static int tx_tail;
  69static int rx_tail, rx_last;
  70#ifdef CONFIG_DM_ETH
  71static int num_cards;   /* Number of E1000 devices seen so far */
  72#endif
  73
  74static struct pci_device_id e1000_supported[] = {
  75        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82542) },
  76        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_FIBER) },
  77        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_COPPER) },
  78        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_COPPER) },
  79        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_FIBER) },
  80        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_COPPER) },
  81        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_LOM) },
  82        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM) },
  83        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_COPPER) },
  84        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545GM_COPPER) },
  85        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_COPPER) },
  86        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_FIBER) },
  87        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_FIBER) },
  88        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_COPPER) },
  89        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM_LOM) },
  90        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541ER) },
  91        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541GI_LF) },
  92        /* E1000 PCIe card */
  93        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_COPPER) },
  94        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_FIBER) },
  95        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES) },
  96        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER) },
  97        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571PT_QUAD_COPPER) },
  98        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_FIBER) },
  99        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER_LOWPROFILE) },
 100        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_DUAL) },
 101        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_QUAD) },
 102        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_COPPER) },
 103        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_FIBER) },
 104        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_SERDES) },
 105        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI) },
 106        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E) },
 107        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E_IAMT) },
 108        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573L) },
 109        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82574L) },
 110        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_QUAD_COPPER_KSP3) },
 111        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_DPT) },
 112        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_DPT) },
 113        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_SPT) },
 114        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_SPT) },
 115        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED) },
 116        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED) },
 117        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER) },
 118        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_COPPER) },
 119        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS) },
 120        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES) },
 121        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS) },
 122        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_1000BASEKX) },
 123
 124        {}
 125};
 126
 127/* Function forward declarations */
 128static int e1000_setup_link(struct e1000_hw *hw);
 129static int e1000_setup_fiber_link(struct e1000_hw *hw);
 130static int e1000_setup_copper_link(struct e1000_hw *hw);
 131static int e1000_phy_setup_autoneg(struct e1000_hw *hw);
 132static void e1000_config_collision_dist(struct e1000_hw *hw);
 133static int e1000_config_mac_to_phy(struct e1000_hw *hw);
 134static int e1000_config_fc_after_link_up(struct e1000_hw *hw);
 135static int e1000_check_for_link(struct e1000_hw *hw);
 136static int e1000_wait_autoneg(struct e1000_hw *hw);
 137static int e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t * speed,
 138                                       uint16_t * duplex);
 139static int e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
 140                              uint16_t * phy_data);
 141static int e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
 142                               uint16_t phy_data);
 143static int32_t e1000_phy_hw_reset(struct e1000_hw *hw);
 144static int e1000_phy_reset(struct e1000_hw *hw);
 145static int e1000_detect_gig_phy(struct e1000_hw *hw);
 146static void e1000_set_media_type(struct e1000_hw *hw);
 147
 148static int32_t e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask);
 149static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask);
 150static int32_t e1000_check_phy_reset_block(struct e1000_hw *hw);
 151
 152#ifndef CONFIG_E1000_NO_NVM
 153static void e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw);
 154static int32_t e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw);
 155static int32_t e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
 156                uint16_t words,
 157                uint16_t *data);
 158/******************************************************************************
 159 * Raises the EEPROM's clock input.
 160 *
 161 * hw - Struct containing variables accessed by shared code
 162 * eecd - EECD's current value
 163 *****************************************************************************/
 164void e1000_raise_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
 165{
 166        /* Raise the clock input to the EEPROM (by setting the SK bit), and then
 167         * wait 50 microseconds.
 168         */
 169        *eecd = *eecd | E1000_EECD_SK;
 170        E1000_WRITE_REG(hw, EECD, *eecd);
 171        E1000_WRITE_FLUSH(hw);
 172        udelay(50);
 173}
 174
 175/******************************************************************************
 176 * Lowers the EEPROM's clock input.
 177 *
 178 * hw - Struct containing variables accessed by shared code
 179 * eecd - EECD's current value
 180 *****************************************************************************/
 181void e1000_lower_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
 182{
 183        /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
 184         * wait 50 microseconds.
 185         */
 186        *eecd = *eecd & ~E1000_EECD_SK;
 187        E1000_WRITE_REG(hw, EECD, *eecd);
 188        E1000_WRITE_FLUSH(hw);
 189        udelay(50);
 190}
 191
 192/******************************************************************************
 193 * Shift data bits out to the EEPROM.
 194 *
 195 * hw - Struct containing variables accessed by shared code
 196 * data - data to send to the EEPROM
 197 * count - number of bits to shift out
 198 *****************************************************************************/
 199static void
 200e1000_shift_out_ee_bits(struct e1000_hw *hw, uint16_t data, uint16_t count)
 201{
 202        uint32_t eecd;
 203        uint32_t mask;
 204
 205        /* We need to shift "count" bits out to the EEPROM. So, value in the
 206         * "data" parameter will be shifted out to the EEPROM one bit at a time.
 207         * In order to do this, "data" must be broken down into bits.
 208         */
 209        mask = 0x01 << (count - 1);
 210        eecd = E1000_READ_REG(hw, EECD);
 211        eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
 212        do {
 213                /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
 214                 * and then raising and then lowering the clock (the SK bit controls
 215                 * the clock input to the EEPROM).  A "0" is shifted out to the EEPROM
 216                 * by setting "DI" to "0" and then raising and then lowering the clock.
 217                 */
 218                eecd &= ~E1000_EECD_DI;
 219
 220                if (data & mask)
 221                        eecd |= E1000_EECD_DI;
 222
 223                E1000_WRITE_REG(hw, EECD, eecd);
 224                E1000_WRITE_FLUSH(hw);
 225
 226                udelay(50);
 227
 228                e1000_raise_ee_clk(hw, &eecd);
 229                e1000_lower_ee_clk(hw, &eecd);
 230
 231                mask = mask >> 1;
 232
 233        } while (mask);
 234
 235        /* We leave the "DI" bit set to "0" when we leave this routine. */
 236        eecd &= ~E1000_EECD_DI;
 237        E1000_WRITE_REG(hw, EECD, eecd);
 238}
 239
 240/******************************************************************************
 241 * Shift data bits in from the EEPROM
 242 *
 243 * hw - Struct containing variables accessed by shared code
 244 *****************************************************************************/
 245static uint16_t
 246e1000_shift_in_ee_bits(struct e1000_hw *hw, uint16_t count)
 247{
 248        uint32_t eecd;
 249        uint32_t i;
 250        uint16_t data;
 251
 252        /* In order to read a register from the EEPROM, we need to shift 'count'
 253         * bits in from the EEPROM. Bits are "shifted in" by raising the clock
 254         * input to the EEPROM (setting the SK bit), and then reading the
 255         * value of the "DO" bit.  During this "shifting in" process the
 256         * "DI" bit should always be clear.
 257         */
 258
 259        eecd = E1000_READ_REG(hw, EECD);
 260
 261        eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
 262        data = 0;
 263
 264        for (i = 0; i < count; i++) {
 265                data = data << 1;
 266                e1000_raise_ee_clk(hw, &eecd);
 267
 268                eecd = E1000_READ_REG(hw, EECD);
 269
 270                eecd &= ~(E1000_EECD_DI);
 271                if (eecd & E1000_EECD_DO)
 272                        data |= 1;
 273
 274                e1000_lower_ee_clk(hw, &eecd);
 275        }
 276
 277        return data;
 278}
 279
 280/******************************************************************************
 281 * Returns EEPROM to a "standby" state
 282 *
 283 * hw - Struct containing variables accessed by shared code
 284 *****************************************************************************/
 285void e1000_standby_eeprom(struct e1000_hw *hw)
 286{
 287        struct e1000_eeprom_info *eeprom = &hw->eeprom;
 288        uint32_t eecd;
 289
 290        eecd = E1000_READ_REG(hw, EECD);
 291
 292        if (eeprom->type == e1000_eeprom_microwire) {
 293                eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
 294                E1000_WRITE_REG(hw, EECD, eecd);
 295                E1000_WRITE_FLUSH(hw);
 296                udelay(eeprom->delay_usec);
 297
 298                /* Clock high */
 299                eecd |= E1000_EECD_SK;
 300                E1000_WRITE_REG(hw, EECD, eecd);
 301                E1000_WRITE_FLUSH(hw);
 302                udelay(eeprom->delay_usec);
 303
 304                /* Select EEPROM */
 305                eecd |= E1000_EECD_CS;
 306                E1000_WRITE_REG(hw, EECD, eecd);
 307                E1000_WRITE_FLUSH(hw);
 308                udelay(eeprom->delay_usec);
 309
 310                /* Clock low */
 311                eecd &= ~E1000_EECD_SK;
 312                E1000_WRITE_REG(hw, EECD, eecd);
 313                E1000_WRITE_FLUSH(hw);
 314                udelay(eeprom->delay_usec);
 315        } else if (eeprom->type == e1000_eeprom_spi) {
 316                /* Toggle CS to flush commands */
 317                eecd |= E1000_EECD_CS;
 318                E1000_WRITE_REG(hw, EECD, eecd);
 319                E1000_WRITE_FLUSH(hw);
 320                udelay(eeprom->delay_usec);
 321                eecd &= ~E1000_EECD_CS;
 322                E1000_WRITE_REG(hw, EECD, eecd);
 323                E1000_WRITE_FLUSH(hw);
 324                udelay(eeprom->delay_usec);
 325        }
 326}
 327
 328/***************************************************************************
 329* Description:     Determines if the onboard NVM is FLASH or EEPROM.
 330*
 331* hw - Struct containing variables accessed by shared code
 332****************************************************************************/
 333static bool e1000_is_onboard_nvm_eeprom(struct e1000_hw *hw)
 334{
 335        uint32_t eecd = 0;
 336
 337        DEBUGFUNC();
 338
 339        if (hw->mac_type == e1000_ich8lan)
 340                return false;
 341
 342        if (hw->mac_type == e1000_82573 || hw->mac_type == e1000_82574) {
 343                eecd = E1000_READ_REG(hw, EECD);
 344
 345                /* Isolate bits 15 & 16 */
 346                eecd = ((eecd >> 15) & 0x03);
 347
 348                /* If both bits are set, device is Flash type */
 349                if (eecd == 0x03)
 350                        return false;
 351        }
 352        return true;
 353}
 354
 355/******************************************************************************
 356 * Prepares EEPROM for access
 357 *
 358 * hw - Struct containing variables accessed by shared code
 359 *
 360 * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
 361 * function should be called before issuing a command to the EEPROM.
 362 *****************************************************************************/
 363int32_t e1000_acquire_eeprom(struct e1000_hw *hw)
 364{
 365        struct e1000_eeprom_info *eeprom = &hw->eeprom;
 366        uint32_t eecd, i = 0;
 367
 368        DEBUGFUNC();
 369
 370        if (e1000_swfw_sync_acquire(hw, E1000_SWFW_EEP_SM))
 371                return -E1000_ERR_SWFW_SYNC;
 372        eecd = E1000_READ_REG(hw, EECD);
 373
 374        if (hw->mac_type != e1000_82573 && hw->mac_type != e1000_82574) {
 375                /* Request EEPROM Access */
 376                if (hw->mac_type > e1000_82544) {
 377                        eecd |= E1000_EECD_REQ;
 378                        E1000_WRITE_REG(hw, EECD, eecd);
 379                        eecd = E1000_READ_REG(hw, EECD);
 380                        while ((!(eecd & E1000_EECD_GNT)) &&
 381                                (i < E1000_EEPROM_GRANT_ATTEMPTS)) {
 382                                i++;
 383                                udelay(5);
 384                                eecd = E1000_READ_REG(hw, EECD);
 385                        }
 386                        if (!(eecd & E1000_EECD_GNT)) {
 387                                eecd &= ~E1000_EECD_REQ;
 388                                E1000_WRITE_REG(hw, EECD, eecd);
 389                                DEBUGOUT("Could not acquire EEPROM grant\n");
 390                                return -E1000_ERR_EEPROM;
 391                        }
 392                }
 393        }
 394
 395        /* Setup EEPROM for Read/Write */
 396
 397        if (eeprom->type == e1000_eeprom_microwire) {
 398                /* Clear SK and DI */
 399                eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
 400                E1000_WRITE_REG(hw, EECD, eecd);
 401
 402                /* Set CS */
 403                eecd |= E1000_EECD_CS;
 404                E1000_WRITE_REG(hw, EECD, eecd);
 405        } else if (eeprom->type == e1000_eeprom_spi) {
 406                /* Clear SK and CS */
 407                eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
 408                E1000_WRITE_REG(hw, EECD, eecd);
 409                udelay(1);
 410        }
 411
 412        return E1000_SUCCESS;
 413}
 414
 415/******************************************************************************
 416 * Sets up eeprom variables in the hw struct.  Must be called after mac_type
 417 * is configured.  Additionally, if this is ICH8, the flash controller GbE
 418 * registers must be mapped, or this will crash.
 419 *
 420 * hw - Struct containing variables accessed by shared code
 421 *****************************************************************************/
 422static int32_t e1000_init_eeprom_params(struct e1000_hw *hw)
 423{
 424        struct e1000_eeprom_info *eeprom = &hw->eeprom;
 425        uint32_t eecd;
 426        int32_t ret_val = E1000_SUCCESS;
 427        uint16_t eeprom_size;
 428
 429        if (hw->mac_type == e1000_igb)
 430                eecd = E1000_READ_REG(hw, I210_EECD);
 431        else
 432                eecd = E1000_READ_REG(hw, EECD);
 433
 434        DEBUGFUNC();
 435
 436        switch (hw->mac_type) {
 437        case e1000_82542_rev2_0:
 438        case e1000_82542_rev2_1:
 439        case e1000_82543:
 440        case e1000_82544:
 441                eeprom->type = e1000_eeprom_microwire;
 442                eeprom->word_size = 64;
 443                eeprom->opcode_bits = 3;
 444                eeprom->address_bits = 6;
 445                eeprom->delay_usec = 50;
 446                eeprom->use_eerd = false;
 447                eeprom->use_eewr = false;
 448        break;
 449        case e1000_82540:
 450        case e1000_82545:
 451        case e1000_82545_rev_3:
 452        case e1000_82546:
 453        case e1000_82546_rev_3:
 454                eeprom->type = e1000_eeprom_microwire;
 455                eeprom->opcode_bits = 3;
 456                eeprom->delay_usec = 50;
 457                if (eecd & E1000_EECD_SIZE) {
 458                        eeprom->word_size = 256;
 459                        eeprom->address_bits = 8;
 460                } else {
 461                        eeprom->word_size = 64;
 462                        eeprom->address_bits = 6;
 463                }
 464                eeprom->use_eerd = false;
 465                eeprom->use_eewr = false;
 466                break;
 467        case e1000_82541:
 468        case e1000_82541_rev_2:
 469        case e1000_82547:
 470        case e1000_82547_rev_2:
 471                if (eecd & E1000_EECD_TYPE) {
 472                        eeprom->type = e1000_eeprom_spi;
 473                        eeprom->opcode_bits = 8;
 474                        eeprom->delay_usec = 1;
 475                        if (eecd & E1000_EECD_ADDR_BITS) {
 476                                eeprom->page_size = 32;
 477                                eeprom->address_bits = 16;
 478                        } else {
 479                                eeprom->page_size = 8;
 480                                eeprom->address_bits = 8;
 481                        }
 482                } else {
 483                        eeprom->type = e1000_eeprom_microwire;
 484                        eeprom->opcode_bits = 3;
 485                        eeprom->delay_usec = 50;
 486                        if (eecd & E1000_EECD_ADDR_BITS) {
 487                                eeprom->word_size = 256;
 488                                eeprom->address_bits = 8;
 489                        } else {
 490                                eeprom->word_size = 64;
 491                                eeprom->address_bits = 6;
 492                        }
 493                }
 494                eeprom->use_eerd = false;
 495                eeprom->use_eewr = false;
 496                break;
 497        case e1000_82571:
 498        case e1000_82572:
 499                eeprom->type = e1000_eeprom_spi;
 500                eeprom->opcode_bits = 8;
 501                eeprom->delay_usec = 1;
 502                if (eecd & E1000_EECD_ADDR_BITS) {
 503                        eeprom->page_size = 32;
 504                        eeprom->address_bits = 16;
 505                } else {
 506                        eeprom->page_size = 8;
 507                        eeprom->address_bits = 8;
 508                }
 509                eeprom->use_eerd = false;
 510                eeprom->use_eewr = false;
 511                break;
 512        case e1000_82573:
 513        case e1000_82574:
 514                eeprom->type = e1000_eeprom_spi;
 515                eeprom->opcode_bits = 8;
 516                eeprom->delay_usec = 1;
 517                if (eecd & E1000_EECD_ADDR_BITS) {
 518                        eeprom->page_size = 32;
 519                        eeprom->address_bits = 16;
 520                } else {
 521                        eeprom->page_size = 8;
 522                        eeprom->address_bits = 8;
 523                }
 524                if (e1000_is_onboard_nvm_eeprom(hw) == false) {
 525                        eeprom->use_eerd = true;
 526                        eeprom->use_eewr = true;
 527
 528                        eeprom->type = e1000_eeprom_flash;
 529                        eeprom->word_size = 2048;
 530
 531                /* Ensure that the Autonomous FLASH update bit is cleared due to
 532                 * Flash update issue on parts which use a FLASH for NVM. */
 533                        eecd &= ~E1000_EECD_AUPDEN;
 534                        E1000_WRITE_REG(hw, EECD, eecd);
 535                }
 536                break;
 537        case e1000_80003es2lan:
 538                eeprom->type = e1000_eeprom_spi;
 539                eeprom->opcode_bits = 8;
 540                eeprom->delay_usec = 1;
 541                if (eecd & E1000_EECD_ADDR_BITS) {
 542                        eeprom->page_size = 32;
 543                        eeprom->address_bits = 16;
 544                } else {
 545                        eeprom->page_size = 8;
 546                        eeprom->address_bits = 8;
 547                }
 548                eeprom->use_eerd = true;
 549                eeprom->use_eewr = false;
 550                break;
 551        case e1000_igb:
 552                /* i210 has 4k of iNVM mapped as EEPROM */
 553                eeprom->type = e1000_eeprom_invm;
 554                eeprom->opcode_bits = 8;
 555                eeprom->delay_usec = 1;
 556                eeprom->page_size = 32;
 557                eeprom->address_bits = 16;
 558                eeprom->use_eerd = true;
 559                eeprom->use_eewr = false;
 560                break;
 561        default:
 562                break;
 563        }
 564
 565        if (eeprom->type == e1000_eeprom_spi ||
 566            eeprom->type == e1000_eeprom_invm) {
 567                /* eeprom_size will be an enum [0..8] that maps
 568                 * to eeprom sizes 128B to
 569                 * 32KB (incremented by powers of 2).
 570                 */
 571                if (hw->mac_type <= e1000_82547_rev_2) {
 572                        /* Set to default value for initial eeprom read. */
 573                        eeprom->word_size = 64;
 574                        ret_val = e1000_read_eeprom(hw, EEPROM_CFG, 1,
 575                                        &eeprom_size);
 576                        if (ret_val)
 577                                return ret_val;
 578                        eeprom_size = (eeprom_size & EEPROM_SIZE_MASK)
 579                                >> EEPROM_SIZE_SHIFT;
 580                        /* 256B eeprom size was not supported in earlier
 581                         * hardware, so we bump eeprom_size up one to
 582                         * ensure that "1" (which maps to 256B) is never
 583                         * the result used in the shifting logic below. */
 584                        if (eeprom_size)
 585                                eeprom_size++;
 586                } else {
 587                        eeprom_size = (uint16_t)((eecd &
 588                                E1000_EECD_SIZE_EX_MASK) >>
 589                                E1000_EECD_SIZE_EX_SHIFT);
 590                }
 591
 592                eeprom->word_size = 1 << (eeprom_size + EEPROM_WORD_SIZE_SHIFT);
 593        }
 594        return ret_val;
 595}
 596
 597/******************************************************************************
 598 * Polls the status bit (bit 1) of the EERD to determine when the read is done.
 599 *
 600 * hw - Struct containing variables accessed by shared code
 601 *****************************************************************************/
 602static int32_t
 603e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd)
 604{
 605        uint32_t attempts = 100000;
 606        uint32_t i, reg = 0;
 607        int32_t done = E1000_ERR_EEPROM;
 608
 609        for (i = 0; i < attempts; i++) {
 610                if (eerd == E1000_EEPROM_POLL_READ) {
 611                        if (hw->mac_type == e1000_igb)
 612                                reg = E1000_READ_REG(hw, I210_EERD);
 613                        else
 614                                reg = E1000_READ_REG(hw, EERD);
 615                } else {
 616                        if (hw->mac_type == e1000_igb)
 617                                reg = E1000_READ_REG(hw, I210_EEWR);
 618                        else
 619                                reg = E1000_READ_REG(hw, EEWR);
 620                }
 621
 622                if (reg & E1000_EEPROM_RW_REG_DONE) {
 623                        done = E1000_SUCCESS;
 624                        break;
 625                }
 626                udelay(5);
 627        }
 628
 629        return done;
 630}
 631
 632/******************************************************************************
 633 * Reads a 16 bit word from the EEPROM using the EERD register.
 634 *
 635 * hw - Struct containing variables accessed by shared code
 636 * offset - offset of  word in the EEPROM to read
 637 * data - word read from the EEPROM
 638 * words - number of words to read
 639 *****************************************************************************/
 640static int32_t
 641e1000_read_eeprom_eerd(struct e1000_hw *hw,
 642                        uint16_t offset,
 643                        uint16_t words,
 644                        uint16_t *data)
 645{
 646        uint32_t i, eerd = 0;
 647        int32_t error = 0;
 648
 649        for (i = 0; i < words; i++) {
 650                eerd = ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) +
 651                        E1000_EEPROM_RW_REG_START;
 652
 653                if (hw->mac_type == e1000_igb)
 654                        E1000_WRITE_REG(hw, I210_EERD, eerd);
 655                else
 656                        E1000_WRITE_REG(hw, EERD, eerd);
 657
 658                error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_READ);
 659
 660                if (error)
 661                        break;
 662
 663                if (hw->mac_type == e1000_igb) {
 664                        data[i] = (E1000_READ_REG(hw, I210_EERD) >>
 665                                E1000_EEPROM_RW_REG_DATA);
 666                } else {
 667                        data[i] = (E1000_READ_REG(hw, EERD) >>
 668                                E1000_EEPROM_RW_REG_DATA);
 669                }
 670
 671        }
 672
 673        return error;
 674}
 675
 676void e1000_release_eeprom(struct e1000_hw *hw)
 677{
 678        uint32_t eecd;
 679
 680        DEBUGFUNC();
 681
 682        eecd = E1000_READ_REG(hw, EECD);
 683
 684        if (hw->eeprom.type == e1000_eeprom_spi) {
 685                eecd |= E1000_EECD_CS;  /* Pull CS high */
 686                eecd &= ~E1000_EECD_SK; /* Lower SCK */
 687
 688                E1000_WRITE_REG(hw, EECD, eecd);
 689
 690                udelay(hw->eeprom.delay_usec);
 691        } else if (hw->eeprom.type == e1000_eeprom_microwire) {
 692                /* cleanup eeprom */
 693
 694                /* CS on Microwire is active-high */
 695                eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
 696
 697                E1000_WRITE_REG(hw, EECD, eecd);
 698
 699                /* Rising edge of clock */
 700                eecd |= E1000_EECD_SK;
 701                E1000_WRITE_REG(hw, EECD, eecd);
 702                E1000_WRITE_FLUSH(hw);
 703                udelay(hw->eeprom.delay_usec);
 704
 705                /* Falling edge of clock */
 706                eecd &= ~E1000_EECD_SK;
 707                E1000_WRITE_REG(hw, EECD, eecd);
 708                E1000_WRITE_FLUSH(hw);
 709                udelay(hw->eeprom.delay_usec);
 710        }
 711
 712        /* Stop requesting EEPROM access */
 713        if (hw->mac_type > e1000_82544) {
 714                eecd &= ~E1000_EECD_REQ;
 715                E1000_WRITE_REG(hw, EECD, eecd);
 716        }
 717
 718        e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
 719}
 720
 721/******************************************************************************
 722 * Reads a 16 bit word from the EEPROM.
 723 *
 724 * hw - Struct containing variables accessed by shared code
 725 *****************************************************************************/
 726static int32_t
 727e1000_spi_eeprom_ready(struct e1000_hw *hw)
 728{
 729        uint16_t retry_count = 0;
 730        uint8_t spi_stat_reg;
 731
 732        DEBUGFUNC();
 733
 734        /* Read "Status Register" repeatedly until the LSB is cleared.  The
 735         * EEPROM will signal that the command has been completed by clearing
 736         * bit 0 of the internal status register.  If it's not cleared within
 737         * 5 milliseconds, then error out.
 738         */
 739        retry_count = 0;
 740        do {
 741                e1000_shift_out_ee_bits(hw, EEPROM_RDSR_OPCODE_SPI,
 742                        hw->eeprom.opcode_bits);
 743                spi_stat_reg = (uint8_t)e1000_shift_in_ee_bits(hw, 8);
 744                if (!(spi_stat_reg & EEPROM_STATUS_RDY_SPI))
 745                        break;
 746
 747                udelay(5);
 748                retry_count += 5;
 749
 750                e1000_standby_eeprom(hw);
 751        } while (retry_count < EEPROM_MAX_RETRY_SPI);
 752
 753        /* ATMEL SPI write time could vary from 0-20mSec on 3.3V devices (and
 754         * only 0-5mSec on 5V devices)
 755         */
 756        if (retry_count >= EEPROM_MAX_RETRY_SPI) {
 757                DEBUGOUT("SPI EEPROM Status error\n");
 758                return -E1000_ERR_EEPROM;
 759        }
 760
 761        return E1000_SUCCESS;
 762}
 763
 764/******************************************************************************
 765 * Reads a 16 bit word from the EEPROM.
 766 *
 767 * hw - Struct containing variables accessed by shared code
 768 * offset - offset of  word in the EEPROM to read
 769 * data - word read from the EEPROM
 770 *****************************************************************************/
 771static int32_t
 772e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
 773                uint16_t words, uint16_t *data)
 774{
 775        struct e1000_eeprom_info *eeprom = &hw->eeprom;
 776        uint32_t i = 0;
 777
 778        DEBUGFUNC();
 779
 780        /* If eeprom is not yet detected, do so now */
 781        if (eeprom->word_size == 0)
 782                e1000_init_eeprom_params(hw);
 783
 784        /* A check for invalid values:  offset too large, too many words,
 785         * and not enough words.
 786         */
 787        if ((offset >= eeprom->word_size) ||
 788                (words > eeprom->word_size - offset) ||
 789                (words == 0)) {
 790                DEBUGOUT("\"words\" parameter out of bounds."
 791                        "Words = %d, size = %d\n", offset, eeprom->word_size);
 792                return -E1000_ERR_EEPROM;
 793        }
 794
 795        /* EEPROM's that don't use EERD to read require us to bit-bang the SPI
 796         * directly. In this case, we need to acquire the EEPROM so that
 797         * FW or other port software does not interrupt.
 798         */
 799        if (e1000_is_onboard_nvm_eeprom(hw) == true &&
 800                hw->eeprom.use_eerd == false) {
 801
 802                /* Prepare the EEPROM for bit-bang reading */
 803                if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
 804                        return -E1000_ERR_EEPROM;
 805        }
 806
 807        /* Eerd register EEPROM access requires no eeprom aquire/release */
 808        if (eeprom->use_eerd == true)
 809                return e1000_read_eeprom_eerd(hw, offset, words, data);
 810
 811        /* Set up the SPI or Microwire EEPROM for bit-bang reading.  We have
 812         * acquired the EEPROM at this point, so any returns should relase it */
 813        if (eeprom->type == e1000_eeprom_spi) {
 814                uint16_t word_in;
 815                uint8_t read_opcode = EEPROM_READ_OPCODE_SPI;
 816
 817                if (e1000_spi_eeprom_ready(hw)) {
 818                        e1000_release_eeprom(hw);
 819                        return -E1000_ERR_EEPROM;
 820                }
 821
 822                e1000_standby_eeprom(hw);
 823
 824                /* Some SPI eeproms use the 8th address bit embedded in
 825                 * the opcode */
 826                if ((eeprom->address_bits == 8) && (offset >= 128))
 827                        read_opcode |= EEPROM_A8_OPCODE_SPI;
 828
 829                /* Send the READ command (opcode + addr)  */
 830                e1000_shift_out_ee_bits(hw, read_opcode, eeprom->opcode_bits);
 831                e1000_shift_out_ee_bits(hw, (uint16_t)(offset*2),
 832                                eeprom->address_bits);
 833
 834                /* Read the data.  The address of the eeprom internally
 835                 * increments with each byte (spi) being read, saving on the
 836                 * overhead of eeprom setup and tear-down.  The address
 837                 * counter will roll over if reading beyond the size of
 838                 * the eeprom, thus allowing the entire memory to be read
 839                 * starting from any offset. */
 840                for (i = 0; i < words; i++) {
 841                        word_in = e1000_shift_in_ee_bits(hw, 16);
 842                        data[i] = (word_in >> 8) | (word_in << 8);
 843                }
 844        } else if (eeprom->type == e1000_eeprom_microwire) {
 845                for (i = 0; i < words; i++) {
 846                        /* Send the READ command (opcode + addr)  */
 847                        e1000_shift_out_ee_bits(hw,
 848                                EEPROM_READ_OPCODE_MICROWIRE,
 849                                eeprom->opcode_bits);
 850                        e1000_shift_out_ee_bits(hw, (uint16_t)(offset + i),
 851                                eeprom->address_bits);
 852
 853                        /* Read the data.  For microwire, each word requires
 854                         * the overhead of eeprom setup and tear-down. */
 855                        data[i] = e1000_shift_in_ee_bits(hw, 16);
 856                        e1000_standby_eeprom(hw);
 857                }
 858        }
 859
 860        /* End this read operation */
 861        e1000_release_eeprom(hw);
 862
 863        return E1000_SUCCESS;
 864}
 865
 866#ifndef CONFIG_DM_ETH
 867/******************************************************************************
 868 *  e1000_write_eeprom_srwr - Write to Shadow Ram using EEWR
 869 *  @hw: pointer to the HW structure
 870 *  @offset: offset within the Shadow Ram to be written to
 871 *  @words: number of words to write
 872 *  @data: 16 bit word(s) to be written to the Shadow Ram
 873 *
 874 *  Writes data to Shadow Ram at offset using EEWR register.
 875 *
 876 *  If e1000_update_eeprom_checksum_i210 is not called after this function, the
 877 *  Shadow Ram will most likely contain an invalid checksum.
 878 *****************************************************************************/
 879static int32_t e1000_write_eeprom_srwr(struct e1000_hw *hw, uint16_t offset,
 880                                       uint16_t words, uint16_t *data)
 881{
 882        struct e1000_eeprom_info *eeprom = &hw->eeprom;
 883        uint32_t i, k, eewr = 0;
 884        uint32_t attempts = 100000;
 885        int32_t ret_val = 0;
 886
 887        /* A check for invalid values:  offset too large, too many words,
 888         * too many words for the offset, and not enough words.
 889         */
 890        if ((offset >= eeprom->word_size) ||
 891            (words > (eeprom->word_size - offset)) || (words == 0)) {
 892                DEBUGOUT("nvm parameter(s) out of bounds\n");
 893                ret_val = -E1000_ERR_EEPROM;
 894                goto out;
 895        }
 896
 897        for (i = 0; i < words; i++) {
 898                eewr = ((offset + i) << E1000_EEPROM_RW_ADDR_SHIFT)
 899                                | (data[i] << E1000_EEPROM_RW_REG_DATA) |
 900                                E1000_EEPROM_RW_REG_START;
 901
 902                E1000_WRITE_REG(hw, I210_EEWR, eewr);
 903
 904                for (k = 0; k < attempts; k++) {
 905                        if (E1000_EEPROM_RW_REG_DONE &
 906                            E1000_READ_REG(hw, I210_EEWR)) {
 907                                ret_val = 0;
 908                                break;
 909                        }
 910                        udelay(5);
 911                }
 912
 913                if (ret_val) {
 914                        DEBUGOUT("Shadow RAM write EEWR timed out\n");
 915                        break;
 916                }
 917        }
 918
 919out:
 920        return ret_val;
 921}
 922
 923/******************************************************************************
 924 *  e1000_pool_flash_update_done_i210 - Pool FLUDONE status.
 925 *  @hw: pointer to the HW structure
 926 *
 927 *****************************************************************************/
 928static int32_t e1000_pool_flash_update_done_i210(struct e1000_hw *hw)
 929{
 930        int32_t ret_val = -E1000_ERR_EEPROM;
 931        uint32_t i, reg;
 932
 933        for (i = 0; i < E1000_FLUDONE_ATTEMPTS; i++) {
 934                reg = E1000_READ_REG(hw, EECD);
 935                if (reg & E1000_EECD_FLUDONE_I210) {
 936                        ret_val = 0;
 937                        break;
 938                }
 939                udelay(5);
 940        }
 941
 942        return ret_val;
 943}
 944
 945/******************************************************************************
 946 *  e1000_update_flash_i210 - Commit EEPROM to the flash
 947 *  @hw: pointer to the HW structure
 948 *
 949 *****************************************************************************/
 950static int32_t e1000_update_flash_i210(struct e1000_hw *hw)
 951{
 952        int32_t ret_val = 0;
 953        uint32_t flup;
 954
 955        ret_val = e1000_pool_flash_update_done_i210(hw);
 956        if (ret_val == -E1000_ERR_EEPROM) {
 957                DEBUGOUT("Flash update time out\n");
 958                goto out;
 959        }
 960
 961        flup = E1000_READ_REG(hw, EECD) | E1000_EECD_FLUPD_I210;
 962        E1000_WRITE_REG(hw, EECD, flup);
 963
 964        ret_val = e1000_pool_flash_update_done_i210(hw);
 965        if (ret_val)
 966                DEBUGOUT("Flash update time out\n");
 967        else
 968                DEBUGOUT("Flash update complete\n");
 969
 970out:
 971        return ret_val;
 972}
 973
 974/******************************************************************************
 975 *  e1000_update_eeprom_checksum_i210 - Update EEPROM checksum
 976 *  @hw: pointer to the HW structure
 977 *
 978 *  Updates the EEPROM checksum by reading/adding each word of the EEPROM
 979 *  up to the checksum.  Then calculates the EEPROM checksum and writes the
 980 *  value to the EEPROM. Next commit EEPROM data onto the Flash.
 981 *****************************************************************************/
 982static int32_t e1000_update_eeprom_checksum_i210(struct e1000_hw *hw)
 983{
 984        int32_t ret_val = 0;
 985        uint16_t checksum = 0;
 986        uint16_t i, nvm_data;
 987
 988        /* Read the first word from the EEPROM. If this times out or fails, do
 989         * not continue or we could be in for a very long wait while every
 990         * EEPROM read fails
 991         */
 992        ret_val = e1000_read_eeprom_eerd(hw, 0, 1, &nvm_data);
 993        if (ret_val) {
 994                DEBUGOUT("EEPROM read failed\n");
 995                goto out;
 996        }
 997
 998        if (!(e1000_get_hw_eeprom_semaphore(hw))) {
 999                /* Do not use hw->nvm.ops.write, hw->nvm.ops.read
1000                 * because we do not want to take the synchronization
1001                 * semaphores twice here.
1002                 */
1003
1004                for (i = 0; i < EEPROM_CHECKSUM_REG; i++) {
1005                        ret_val = e1000_read_eeprom_eerd(hw, i, 1, &nvm_data);
1006                        if (ret_val) {
1007                                e1000_put_hw_eeprom_semaphore(hw);
1008                                DEBUGOUT("EEPROM Read Error while updating checksum.\n");
1009                                goto out;
1010                        }
1011                        checksum += nvm_data;
1012                }
1013                checksum = (uint16_t)EEPROM_SUM - checksum;
1014                ret_val = e1000_write_eeprom_srwr(hw, EEPROM_CHECKSUM_REG, 1,
1015                                                  &checksum);
1016                if (ret_val) {
1017                        e1000_put_hw_eeprom_semaphore(hw);
1018                        DEBUGOUT("EEPROM Write Error while updating checksum.\n");
1019                        goto out;
1020                }
1021
1022                e1000_put_hw_eeprom_semaphore(hw);
1023
1024                ret_val = e1000_update_flash_i210(hw);
1025        } else {
1026                ret_val = -E1000_ERR_SWFW_SYNC;
1027        }
1028
1029out:
1030        return ret_val;
1031}
1032#endif
1033
1034/******************************************************************************
1035 * Verifies that the EEPROM has a valid checksum
1036 *
1037 * hw - Struct containing variables accessed by shared code
1038 *
1039 * Reads the first 64 16 bit words of the EEPROM and sums the values read.
1040 * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
1041 * valid.
1042 *****************************************************************************/
1043static int e1000_validate_eeprom_checksum(struct e1000_hw *hw)
1044{
1045        uint16_t i, checksum, checksum_reg, *buf;
1046
1047        DEBUGFUNC();
1048
1049        /* Allocate a temporary buffer */
1050        buf = malloc(sizeof(buf[0]) * (EEPROM_CHECKSUM_REG + 1));
1051        if (!buf) {
1052                E1000_ERR(hw, "Unable to allocate EEPROM buffer!\n");
1053                return -E1000_ERR_EEPROM;
1054        }
1055
1056        /* Read the EEPROM */
1057        if (e1000_read_eeprom(hw, 0, EEPROM_CHECKSUM_REG + 1, buf) < 0) {
1058                E1000_ERR(hw, "Unable to read EEPROM!\n");
1059                return -E1000_ERR_EEPROM;
1060        }
1061
1062        /* Compute the checksum */
1063        checksum = 0;
1064        for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
1065                checksum += buf[i];
1066        checksum = ((uint16_t)EEPROM_SUM) - checksum;
1067        checksum_reg = buf[i];
1068
1069        /* Verify it! */
1070        if (checksum == checksum_reg)
1071                return 0;
1072
1073        /* Hrm, verification failed, print an error */
1074        E1000_ERR(hw, "EEPROM checksum is incorrect!\n");
1075        E1000_ERR(hw, "  ...register was 0x%04hx, calculated 0x%04hx\n",
1076                  checksum_reg, checksum);
1077
1078        return -E1000_ERR_EEPROM;
1079}
1080#endif /* CONFIG_E1000_NO_NVM */
1081
1082/*****************************************************************************
1083 * Set PHY to class A mode
1084 * Assumes the following operations will follow to enable the new class mode.
1085 *  1. Do a PHY soft reset
1086 *  2. Restart auto-negotiation or force link.
1087 *
1088 * hw - Struct containing variables accessed by shared code
1089 ****************************************************************************/
1090static int32_t
1091e1000_set_phy_mode(struct e1000_hw *hw)
1092{
1093#ifndef CONFIG_E1000_NO_NVM
1094        int32_t ret_val;
1095        uint16_t eeprom_data;
1096
1097        DEBUGFUNC();
1098
1099        if ((hw->mac_type == e1000_82545_rev_3) &&
1100                (hw->media_type == e1000_media_type_copper)) {
1101                ret_val = e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD,
1102                                1, &eeprom_data);
1103                if (ret_val)
1104                        return ret_val;
1105
1106                if ((eeprom_data != EEPROM_RESERVED_WORD) &&
1107                        (eeprom_data & EEPROM_PHY_CLASS_A)) {
1108                        ret_val = e1000_write_phy_reg(hw,
1109                                        M88E1000_PHY_PAGE_SELECT, 0x000B);
1110                        if (ret_val)
1111                                return ret_val;
1112                        ret_val = e1000_write_phy_reg(hw,
1113                                        M88E1000_PHY_GEN_CONTROL, 0x8104);
1114                        if (ret_val)
1115                                return ret_val;
1116
1117                        hw->phy_reset_disable = false;
1118                }
1119        }
1120#endif
1121        return E1000_SUCCESS;
1122}
1123
1124#ifndef CONFIG_E1000_NO_NVM
1125/***************************************************************************
1126 *
1127 * Obtaining software semaphore bit (SMBI) before resetting PHY.
1128 *
1129 * hw: Struct containing variables accessed by shared code
1130 *
1131 * returns: - E1000_ERR_RESET if fail to obtain semaphore.
1132 *            E1000_SUCCESS at any other case.
1133 *
1134 ***************************************************************************/
1135static int32_t
1136e1000_get_software_semaphore(struct e1000_hw *hw)
1137{
1138         int32_t timeout = hw->eeprom.word_size + 1;
1139         uint32_t swsm;
1140
1141        DEBUGFUNC();
1142
1143        if (hw->mac_type != e1000_80003es2lan && hw->mac_type != e1000_igb)
1144                return E1000_SUCCESS;
1145
1146        while (timeout) {
1147                swsm = E1000_READ_REG(hw, SWSM);
1148                /* If SMBI bit cleared, it is now set and we hold
1149                 * the semaphore */
1150                if (!(swsm & E1000_SWSM_SMBI))
1151                        break;
1152                mdelay(1);
1153                timeout--;
1154        }
1155
1156        if (!timeout) {
1157                DEBUGOUT("Driver can't access device - SMBI bit is set.\n");
1158                return -E1000_ERR_RESET;
1159        }
1160
1161        return E1000_SUCCESS;
1162}
1163#endif
1164
1165/***************************************************************************
1166 * This function clears HW semaphore bits.
1167 *
1168 * hw: Struct containing variables accessed by shared code
1169 *
1170 * returns: - None.
1171 *
1172 ***************************************************************************/
1173static void
1174e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw)
1175{
1176#ifndef CONFIG_E1000_NO_NVM
1177         uint32_t swsm;
1178
1179        DEBUGFUNC();
1180
1181        if (!hw->eeprom_semaphore_present)
1182                return;
1183
1184        swsm = E1000_READ_REG(hw, SWSM);
1185        if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
1186                /* Release both semaphores. */
1187                swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
1188        } else
1189                swsm &= ~(E1000_SWSM_SWESMBI);
1190        E1000_WRITE_REG(hw, SWSM, swsm);
1191#endif
1192}
1193
1194/***************************************************************************
1195 *
1196 * Using the combination of SMBI and SWESMBI semaphore bits when resetting
1197 * adapter or Eeprom access.
1198 *
1199 * hw: Struct containing variables accessed by shared code
1200 *
1201 * returns: - E1000_ERR_EEPROM if fail to access EEPROM.
1202 *            E1000_SUCCESS at any other case.
1203 *
1204 ***************************************************************************/
1205static int32_t
1206e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw)
1207{
1208#ifndef CONFIG_E1000_NO_NVM
1209        int32_t timeout;
1210        uint32_t swsm;
1211
1212        DEBUGFUNC();
1213
1214        if (!hw->eeprom_semaphore_present)
1215                return E1000_SUCCESS;
1216
1217        if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
1218                /* Get the SW semaphore. */
1219                if (e1000_get_software_semaphore(hw) != E1000_SUCCESS)
1220                        return -E1000_ERR_EEPROM;
1221        }
1222
1223        /* Get the FW semaphore. */
1224        timeout = hw->eeprom.word_size + 1;
1225        while (timeout) {
1226                swsm = E1000_READ_REG(hw, SWSM);
1227                swsm |= E1000_SWSM_SWESMBI;
1228                E1000_WRITE_REG(hw, SWSM, swsm);
1229                /* if we managed to set the bit we got the semaphore. */
1230                swsm = E1000_READ_REG(hw, SWSM);
1231                if (swsm & E1000_SWSM_SWESMBI)
1232                        break;
1233
1234                udelay(50);
1235                timeout--;
1236        }
1237
1238        if (!timeout) {
1239                /* Release semaphores */
1240                e1000_put_hw_eeprom_semaphore(hw);
1241                DEBUGOUT("Driver can't access the Eeprom - "
1242                                "SWESMBI bit is set.\n");
1243                return -E1000_ERR_EEPROM;
1244        }
1245#endif
1246        return E1000_SUCCESS;
1247}
1248
1249/* Take ownership of the PHY */
1250static int32_t
1251e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask)
1252{
1253        uint32_t swfw_sync = 0;
1254        uint32_t swmask = mask;
1255        uint32_t fwmask = mask << 16;
1256        int32_t timeout = 200;
1257
1258        DEBUGFUNC();
1259        while (timeout) {
1260                if (e1000_get_hw_eeprom_semaphore(hw))
1261                        return -E1000_ERR_SWFW_SYNC;
1262
1263                swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
1264                if (!(swfw_sync & (fwmask | swmask)))
1265                        break;
1266
1267                /* firmware currently using resource (fwmask) */
1268                /* or other software thread currently using resource (swmask) */
1269                e1000_put_hw_eeprom_semaphore(hw);
1270                mdelay(5);
1271                timeout--;
1272        }
1273
1274        if (!timeout) {
1275                DEBUGOUT("Driver can't access resource, SW_FW_SYNC timeout.\n");
1276                return -E1000_ERR_SWFW_SYNC;
1277        }
1278
1279        swfw_sync |= swmask;
1280        E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1281
1282        e1000_put_hw_eeprom_semaphore(hw);
1283        return E1000_SUCCESS;
1284}
1285
1286static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask)
1287{
1288        uint32_t swfw_sync = 0;
1289
1290        DEBUGFUNC();
1291        while (e1000_get_hw_eeprom_semaphore(hw))
1292                ; /* Empty */
1293
1294        swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
1295        swfw_sync &= ~mask;
1296        E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1297
1298        e1000_put_hw_eeprom_semaphore(hw);
1299}
1300
1301static bool e1000_is_second_port(struct e1000_hw *hw)
1302{
1303        switch (hw->mac_type) {
1304        case e1000_80003es2lan:
1305        case e1000_82546:
1306        case e1000_82571:
1307                if (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)
1308                        return true;
1309                /* Fallthrough */
1310        default:
1311                return false;
1312        }
1313}
1314
1315#ifndef CONFIG_E1000_NO_NVM
1316/******************************************************************************
1317 * Reads the adapter's MAC address from the EEPROM
1318 *
1319 * hw - Struct containing variables accessed by shared code
1320 * enetaddr - buffering where the MAC address will be stored
1321 *****************************************************************************/
1322static int e1000_read_mac_addr_from_eeprom(struct e1000_hw *hw,
1323                                           unsigned char enetaddr[6])
1324{
1325        uint16_t offset;
1326        uint16_t eeprom_data;
1327        int i;
1328
1329        for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
1330                offset = i >> 1;
1331                if (e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
1332                        DEBUGOUT("EEPROM Read Error\n");
1333                        return -E1000_ERR_EEPROM;
1334                }
1335                enetaddr[i] = eeprom_data & 0xff;
1336                enetaddr[i + 1] = (eeprom_data >> 8) & 0xff;
1337        }
1338
1339        return 0;
1340}
1341
1342/******************************************************************************
1343 * Reads the adapter's MAC address from the RAL/RAH registers
1344 *
1345 * hw - Struct containing variables accessed by shared code
1346 * enetaddr - buffering where the MAC address will be stored
1347 *****************************************************************************/
1348static int e1000_read_mac_addr_from_regs(struct e1000_hw *hw,
1349                                         unsigned char enetaddr[6])
1350{
1351        uint16_t offset, tmp;
1352        uint32_t reg_data = 0;
1353        int i;
1354
1355        if (hw->mac_type != e1000_igb)
1356                return -E1000_ERR_MAC_TYPE;
1357
1358        for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
1359                offset = i >> 1;
1360
1361                if (offset == 0)
1362                        reg_data = E1000_READ_REG_ARRAY(hw, RA, 0);
1363                else if (offset == 1)
1364                        reg_data >>= 16;
1365                else if (offset == 2)
1366                        reg_data = E1000_READ_REG_ARRAY(hw, RA, 1);
1367                tmp = reg_data & 0xffff;
1368
1369                enetaddr[i] = tmp & 0xff;
1370                enetaddr[i + 1] = (tmp >> 8) & 0xff;
1371        }
1372
1373        return 0;
1374}
1375
1376/******************************************************************************
1377 * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
1378 * second function of dual function devices
1379 *
1380 * hw - Struct containing variables accessed by shared code
1381 * enetaddr - buffering where the MAC address will be stored
1382 *****************************************************************************/
1383static int e1000_read_mac_addr(struct e1000_hw *hw, unsigned char enetaddr[6])
1384{
1385        int ret_val;
1386
1387        if (hw->mac_type == e1000_igb) {
1388                /* i210 preloads MAC address into RAL/RAH registers */
1389                ret_val = e1000_read_mac_addr_from_regs(hw, enetaddr);
1390        } else {
1391                ret_val = e1000_read_mac_addr_from_eeprom(hw, enetaddr);
1392        }
1393        if (ret_val)
1394                return ret_val;
1395
1396        /* Invert the last bit if this is the second device */
1397        if (e1000_is_second_port(hw))
1398                enetaddr[5] ^= 1;
1399
1400        return 0;
1401}
1402#endif
1403
1404/******************************************************************************
1405 * Initializes receive address filters.
1406 *
1407 * hw - Struct containing variables accessed by shared code
1408 *
1409 * Places the MAC address in receive address register 0 and clears the rest
1410 * of the receive addresss registers. Clears the multicast table. Assumes
1411 * the receiver is in reset when the routine is called.
1412 *****************************************************************************/
1413static void
1414e1000_init_rx_addrs(struct e1000_hw *hw, unsigned char enetaddr[6])
1415{
1416        uint32_t i;
1417        uint32_t addr_low;
1418        uint32_t addr_high;
1419
1420        DEBUGFUNC();
1421
1422        /* Setup the receive address. */
1423        DEBUGOUT("Programming MAC Address into RAR[0]\n");
1424        addr_low = (enetaddr[0] |
1425                    (enetaddr[1] << 8) |
1426                    (enetaddr[2] << 16) | (enetaddr[3] << 24));
1427
1428        addr_high = (enetaddr[4] | (enetaddr[5] << 8) | E1000_RAH_AV);
1429
1430        E1000_WRITE_REG_ARRAY(hw, RA, 0, addr_low);
1431        E1000_WRITE_REG_ARRAY(hw, RA, 1, addr_high);
1432
1433        /* Zero out the other 15 receive addresses. */
1434        DEBUGOUT("Clearing RAR[1-15]\n");
1435        for (i = 1; i < E1000_RAR_ENTRIES; i++) {
1436                E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
1437                E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
1438        }
1439}
1440
1441/******************************************************************************
1442 * Clears the VLAN filer table
1443 *
1444 * hw - Struct containing variables accessed by shared code
1445 *****************************************************************************/
1446static void
1447e1000_clear_vfta(struct e1000_hw *hw)
1448{
1449        uint32_t offset;
1450
1451        for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++)
1452                E1000_WRITE_REG_ARRAY(hw, VFTA, offset, 0);
1453}
1454
1455/******************************************************************************
1456 * Set the mac type member in the hw struct.
1457 *
1458 * hw - Struct containing variables accessed by shared code
1459 *****************************************************************************/
1460int32_t
1461e1000_set_mac_type(struct e1000_hw *hw)
1462{
1463        DEBUGFUNC();
1464
1465        switch (hw->device_id) {
1466        case E1000_DEV_ID_82542:
1467                switch (hw->revision_id) {
1468                case E1000_82542_2_0_REV_ID:
1469                        hw->mac_type = e1000_82542_rev2_0;
1470                        break;
1471                case E1000_82542_2_1_REV_ID:
1472                        hw->mac_type = e1000_82542_rev2_1;
1473                        break;
1474                default:
1475                        /* Invalid 82542 revision ID */
1476                        return -E1000_ERR_MAC_TYPE;
1477                }
1478                break;
1479        case E1000_DEV_ID_82543GC_FIBER:
1480        case E1000_DEV_ID_82543GC_COPPER:
1481                hw->mac_type = e1000_82543;
1482                break;
1483        case E1000_DEV_ID_82544EI_COPPER:
1484        case E1000_DEV_ID_82544EI_FIBER:
1485        case E1000_DEV_ID_82544GC_COPPER:
1486        case E1000_DEV_ID_82544GC_LOM:
1487                hw->mac_type = e1000_82544;
1488                break;
1489        case E1000_DEV_ID_82540EM:
1490        case E1000_DEV_ID_82540EM_LOM:
1491        case E1000_DEV_ID_82540EP:
1492        case E1000_DEV_ID_82540EP_LOM:
1493        case E1000_DEV_ID_82540EP_LP:
1494                hw->mac_type = e1000_82540;
1495                break;
1496        case E1000_DEV_ID_82545EM_COPPER:
1497        case E1000_DEV_ID_82545EM_FIBER:
1498                hw->mac_type = e1000_82545;
1499                break;
1500        case E1000_DEV_ID_82545GM_COPPER:
1501        case E1000_DEV_ID_82545GM_FIBER:
1502        case E1000_DEV_ID_82545GM_SERDES:
1503                hw->mac_type = e1000_82545_rev_3;
1504                break;
1505        case E1000_DEV_ID_82546EB_COPPER:
1506        case E1000_DEV_ID_82546EB_FIBER:
1507        case E1000_DEV_ID_82546EB_QUAD_COPPER:
1508                hw->mac_type = e1000_82546;
1509                break;
1510        case E1000_DEV_ID_82546GB_COPPER:
1511        case E1000_DEV_ID_82546GB_FIBER:
1512        case E1000_DEV_ID_82546GB_SERDES:
1513        case E1000_DEV_ID_82546GB_PCIE:
1514        case E1000_DEV_ID_82546GB_QUAD_COPPER:
1515        case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1516                hw->mac_type = e1000_82546_rev_3;
1517                break;
1518        case E1000_DEV_ID_82541EI:
1519        case E1000_DEV_ID_82541EI_MOBILE:
1520        case E1000_DEV_ID_82541ER_LOM:
1521                hw->mac_type = e1000_82541;
1522                break;
1523        case E1000_DEV_ID_82541ER:
1524        case E1000_DEV_ID_82541GI:
1525        case E1000_DEV_ID_82541GI_LF:
1526        case E1000_DEV_ID_82541GI_MOBILE:
1527                hw->mac_type = e1000_82541_rev_2;
1528                break;
1529        case E1000_DEV_ID_82547EI:
1530        case E1000_DEV_ID_82547EI_MOBILE:
1531                hw->mac_type = e1000_82547;
1532                break;
1533        case E1000_DEV_ID_82547GI:
1534                hw->mac_type = e1000_82547_rev_2;
1535                break;
1536        case E1000_DEV_ID_82571EB_COPPER:
1537        case E1000_DEV_ID_82571EB_FIBER:
1538        case E1000_DEV_ID_82571EB_SERDES:
1539        case E1000_DEV_ID_82571EB_SERDES_DUAL:
1540        case E1000_DEV_ID_82571EB_SERDES_QUAD:
1541        case E1000_DEV_ID_82571EB_QUAD_COPPER:
1542        case E1000_DEV_ID_82571PT_QUAD_COPPER:
1543        case E1000_DEV_ID_82571EB_QUAD_FIBER:
1544        case E1000_DEV_ID_82571EB_QUAD_COPPER_LOWPROFILE:
1545                hw->mac_type = e1000_82571;
1546                break;
1547        case E1000_DEV_ID_82572EI_COPPER:
1548        case E1000_DEV_ID_82572EI_FIBER:
1549        case E1000_DEV_ID_82572EI_SERDES:
1550        case E1000_DEV_ID_82572EI:
1551                hw->mac_type = e1000_82572;
1552                break;
1553        case E1000_DEV_ID_82573E:
1554        case E1000_DEV_ID_82573E_IAMT:
1555        case E1000_DEV_ID_82573L:
1556                hw->mac_type = e1000_82573;
1557                break;
1558        case E1000_DEV_ID_82574L:
1559                hw->mac_type = e1000_82574;
1560                break;
1561        case E1000_DEV_ID_80003ES2LAN_COPPER_SPT:
1562        case E1000_DEV_ID_80003ES2LAN_SERDES_SPT:
1563        case E1000_DEV_ID_80003ES2LAN_COPPER_DPT:
1564        case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
1565                hw->mac_type = e1000_80003es2lan;
1566                break;
1567        case E1000_DEV_ID_ICH8_IGP_M_AMT:
1568        case E1000_DEV_ID_ICH8_IGP_AMT:
1569        case E1000_DEV_ID_ICH8_IGP_C:
1570        case E1000_DEV_ID_ICH8_IFE:
1571        case E1000_DEV_ID_ICH8_IFE_GT:
1572        case E1000_DEV_ID_ICH8_IFE_G:
1573        case E1000_DEV_ID_ICH8_IGP_M:
1574                hw->mac_type = e1000_ich8lan;
1575                break;
1576        case PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED:
1577        case PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED:
1578        case PCI_DEVICE_ID_INTEL_I210_COPPER:
1579        case PCI_DEVICE_ID_INTEL_I211_COPPER:
1580        case PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS:
1581        case PCI_DEVICE_ID_INTEL_I210_SERDES:
1582        case PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS:
1583        case PCI_DEVICE_ID_INTEL_I210_1000BASEKX:
1584                hw->mac_type = e1000_igb;
1585                break;
1586        default:
1587                /* Should never have loaded on this device */
1588                return -E1000_ERR_MAC_TYPE;
1589        }
1590        return E1000_SUCCESS;
1591}
1592
1593/******************************************************************************
1594 * Reset the transmit and receive units; mask and clear all interrupts.
1595 *
1596 * hw - Struct containing variables accessed by shared code
1597 *****************************************************************************/
1598void
1599e1000_reset_hw(struct e1000_hw *hw)
1600{
1601        uint32_t ctrl;
1602        uint32_t ctrl_ext;
1603        uint32_t manc;
1604        uint32_t pba = 0;
1605        uint32_t reg;
1606
1607        DEBUGFUNC();
1608
1609        /* get the correct pba value for both PCI and PCIe*/
1610        if (hw->mac_type <  e1000_82571)
1611                pba = E1000_DEFAULT_PCI_PBA;
1612        else
1613                pba = E1000_DEFAULT_PCIE_PBA;
1614
1615        /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
1616        if (hw->mac_type == e1000_82542_rev2_0) {
1617                DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
1618#ifdef CONFIG_DM_ETH
1619                dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1620                                hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
1621#else
1622                pci_write_config_word(hw->pdev, PCI_COMMAND,
1623                                hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
1624#endif
1625        }
1626
1627        /* Clear interrupt mask to stop board from generating interrupts */
1628        DEBUGOUT("Masking off all interrupts\n");
1629        if (hw->mac_type == e1000_igb)
1630                E1000_WRITE_REG(hw, I210_IAM, 0);
1631        E1000_WRITE_REG(hw, IMC, 0xffffffff);
1632
1633        /* Disable the Transmit and Receive units.  Then delay to allow
1634         * any pending transactions to complete before we hit the MAC with
1635         * the global reset.
1636         */
1637        E1000_WRITE_REG(hw, RCTL, 0);
1638        E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
1639        E1000_WRITE_FLUSH(hw);
1640
1641        /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
1642        hw->tbi_compatibility_on = false;
1643
1644        /* Delay to allow any outstanding PCI transactions to complete before
1645         * resetting the device
1646         */
1647        mdelay(10);
1648
1649        /* Issue a global reset to the MAC.  This will reset the chip's
1650         * transmit, receive, DMA, and link units.  It will not effect
1651         * the current PCI configuration.  The global reset bit is self-
1652         * clearing, and should clear within a microsecond.
1653         */
1654        DEBUGOUT("Issuing a global reset to MAC\n");
1655        ctrl = E1000_READ_REG(hw, CTRL);
1656
1657        E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
1658
1659        /* Force a reload from the EEPROM if necessary */
1660        if (hw->mac_type == e1000_igb) {
1661                mdelay(20);
1662                reg = E1000_READ_REG(hw, STATUS);
1663                if (reg & E1000_STATUS_PF_RST_DONE)
1664                        DEBUGOUT("PF OK\n");
1665                reg = E1000_READ_REG(hw, I210_EECD);
1666                if (reg & E1000_EECD_AUTO_RD)
1667                        DEBUGOUT("EEC OK\n");
1668        } else if (hw->mac_type < e1000_82540) {
1669                /* Wait for reset to complete */
1670                udelay(10);
1671                ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1672                ctrl_ext |= E1000_CTRL_EXT_EE_RST;
1673                E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
1674                E1000_WRITE_FLUSH(hw);
1675                /* Wait for EEPROM reload */
1676                mdelay(2);
1677        } else {
1678                /* Wait for EEPROM reload (it happens automatically) */
1679                mdelay(4);
1680                /* Dissable HW ARPs on ASF enabled adapters */
1681                manc = E1000_READ_REG(hw, MANC);
1682                manc &= ~(E1000_MANC_ARP_EN);
1683                E1000_WRITE_REG(hw, MANC, manc);
1684        }
1685
1686        /* Clear interrupt mask to stop board from generating interrupts */
1687        DEBUGOUT("Masking off all interrupts\n");
1688        if (hw->mac_type == e1000_igb)
1689                E1000_WRITE_REG(hw, I210_IAM, 0);
1690        E1000_WRITE_REG(hw, IMC, 0xffffffff);
1691
1692        /* Clear any pending interrupt events. */
1693        E1000_READ_REG(hw, ICR);
1694
1695        /* If MWI was previously enabled, reenable it. */
1696        if (hw->mac_type == e1000_82542_rev2_0) {
1697#ifdef CONFIG_DM_ETH
1698                dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
1699#else
1700                pci_write_config_word(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
1701#endif
1702        }
1703        if (hw->mac_type != e1000_igb)
1704                E1000_WRITE_REG(hw, PBA, pba);
1705}
1706
1707/******************************************************************************
1708 *
1709 * Initialize a number of hardware-dependent bits
1710 *
1711 * hw: Struct containing variables accessed by shared code
1712 *
1713 * This function contains hardware limitation workarounds for PCI-E adapters
1714 *
1715 *****************************************************************************/
1716static void
1717e1000_initialize_hardware_bits(struct e1000_hw *hw)
1718{
1719        if ((hw->mac_type >= e1000_82571) &&
1720                        (!hw->initialize_hw_bits_disable)) {
1721                /* Settings common to all PCI-express silicon */
1722                uint32_t reg_ctrl, reg_ctrl_ext;
1723                uint32_t reg_tarc0, reg_tarc1;
1724                uint32_t reg_tctl;
1725                uint32_t reg_txdctl, reg_txdctl1;
1726
1727                /* link autonegotiation/sync workarounds */
1728                reg_tarc0 = E1000_READ_REG(hw, TARC0);
1729                reg_tarc0 &= ~((1 << 30)|(1 << 29)|(1 << 28)|(1 << 27));
1730
1731                /* Enable not-done TX descriptor counting */
1732                reg_txdctl = E1000_READ_REG(hw, TXDCTL);
1733                reg_txdctl |= E1000_TXDCTL_COUNT_DESC;
1734                E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
1735
1736                reg_txdctl1 = E1000_READ_REG(hw, TXDCTL1);
1737                reg_txdctl1 |= E1000_TXDCTL_COUNT_DESC;
1738                E1000_WRITE_REG(hw, TXDCTL1, reg_txdctl1);
1739
1740
1741                switch (hw->mac_type) {
1742                case e1000_igb:                 /* IGB is cool */
1743                        return;
1744                case e1000_82571:
1745                case e1000_82572:
1746                        /* Clear PHY TX compatible mode bits */
1747                        reg_tarc1 = E1000_READ_REG(hw, TARC1);
1748                        reg_tarc1 &= ~((1 << 30)|(1 << 29));
1749
1750                        /* link autonegotiation/sync workarounds */
1751                        reg_tarc0 |= ((1 << 26)|(1 << 25)|(1 << 24)|(1 << 23));
1752
1753                        /* TX ring control fixes */
1754                        reg_tarc1 |= ((1 << 26)|(1 << 25)|(1 << 24));
1755
1756                        /* Multiple read bit is reversed polarity */
1757                        reg_tctl = E1000_READ_REG(hw, TCTL);
1758                        if (reg_tctl & E1000_TCTL_MULR)
1759                                reg_tarc1 &= ~(1 << 28);
1760                        else
1761                                reg_tarc1 |= (1 << 28);
1762
1763                        E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1764                        break;
1765                case e1000_82573:
1766                case e1000_82574:
1767                        reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1768                        reg_ctrl_ext &= ~(1 << 23);
1769                        reg_ctrl_ext |= (1 << 22);
1770
1771                        /* TX byte count fix */
1772                        reg_ctrl = E1000_READ_REG(hw, CTRL);
1773                        reg_ctrl &= ~(1 << 29);
1774
1775                        E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1776                        E1000_WRITE_REG(hw, CTRL, reg_ctrl);
1777                        break;
1778                case e1000_80003es2lan:
1779        /* improve small packet performace for fiber/serdes */
1780                        if ((hw->media_type == e1000_media_type_fiber)
1781                        || (hw->media_type ==
1782                                e1000_media_type_internal_serdes)) {
1783                                reg_tarc0 &= ~(1 << 20);
1784                        }
1785
1786                /* Multiple read bit is reversed polarity */
1787                        reg_tctl = E1000_READ_REG(hw, TCTL);
1788                        reg_tarc1 = E1000_READ_REG(hw, TARC1);
1789                        if (reg_tctl & E1000_TCTL_MULR)
1790                                reg_tarc1 &= ~(1 << 28);
1791                        else
1792                                reg_tarc1 |= (1 << 28);
1793
1794                        E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1795                        break;
1796                case e1000_ich8lan:
1797                        /* Reduce concurrent DMA requests to 3 from 4 */
1798                        if ((hw->revision_id < 3) ||
1799                        ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1800                                (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))
1801                                reg_tarc0 |= ((1 << 29)|(1 << 28));
1802
1803                        reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1804                        reg_ctrl_ext |= (1 << 22);
1805                        E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1806
1807                        /* workaround TX hang with TSO=on */
1808                        reg_tarc0 |= ((1 << 27)|(1 << 26)|(1 << 24)|(1 << 23));
1809
1810                        /* Multiple read bit is reversed polarity */
1811                        reg_tctl = E1000_READ_REG(hw, TCTL);
1812                        reg_tarc1 = E1000_READ_REG(hw, TARC1);
1813                        if (reg_tctl & E1000_TCTL_MULR)
1814                                reg_tarc1 &= ~(1 << 28);
1815                        else
1816                                reg_tarc1 |= (1 << 28);
1817
1818                        /* workaround TX hang with TSO=on */
1819                        reg_tarc1 |= ((1 << 30)|(1 << 26)|(1 << 24));
1820
1821                        E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1822                        break;
1823                default:
1824                        break;
1825                }
1826
1827                E1000_WRITE_REG(hw, TARC0, reg_tarc0);
1828        }
1829}
1830
1831/******************************************************************************
1832 * Performs basic configuration of the adapter.
1833 *
1834 * hw - Struct containing variables accessed by shared code
1835 *
1836 * Assumes that the controller has previously been reset and is in a
1837 * post-reset uninitialized state. Initializes the receive address registers,
1838 * multicast table, and VLAN filter table. Calls routines to setup link
1839 * configuration and flow control settings. Clears all on-chip counters. Leaves
1840 * the transmit and receive units disabled and uninitialized.
1841 *****************************************************************************/
1842static int
1843e1000_init_hw(struct e1000_hw *hw, unsigned char enetaddr[6])
1844{
1845        uint32_t ctrl;
1846        uint32_t i;
1847        int32_t ret_val;
1848        uint16_t pcix_cmd_word;
1849        uint16_t pcix_stat_hi_word;
1850        uint16_t cmd_mmrbc;
1851        uint16_t stat_mmrbc;
1852        uint32_t mta_size;
1853        uint32_t reg_data;
1854        uint32_t ctrl_ext;
1855        DEBUGFUNC();
1856        /* force full DMA clock frequency for 10/100 on ICH8 A0-B0 */
1857        if ((hw->mac_type == e1000_ich8lan) &&
1858                ((hw->revision_id < 3) ||
1859                ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1860                (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))) {
1861                        reg_data = E1000_READ_REG(hw, STATUS);
1862                        reg_data &= ~0x80000000;
1863                        E1000_WRITE_REG(hw, STATUS, reg_data);
1864        }
1865        /* Do not need initialize Identification LED */
1866
1867        /* Set the media type and TBI compatibility */
1868        e1000_set_media_type(hw);
1869
1870        /* Must be called after e1000_set_media_type
1871         * because media_type is used */
1872        e1000_initialize_hardware_bits(hw);
1873
1874        /* Disabling VLAN filtering. */
1875        DEBUGOUT("Initializing the IEEE VLAN\n");
1876        /* VET hardcoded to standard value and VFTA removed in ICH8 LAN */
1877        if (hw->mac_type != e1000_ich8lan) {
1878                if (hw->mac_type < e1000_82545_rev_3)
1879                        E1000_WRITE_REG(hw, VET, 0);
1880                e1000_clear_vfta(hw);
1881        }
1882
1883        /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
1884        if (hw->mac_type == e1000_82542_rev2_0) {
1885                DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
1886#ifdef CONFIG_DM_ETH
1887                dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1888                                      hw->
1889                                      pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
1890#else
1891                pci_write_config_word(hw->pdev, PCI_COMMAND,
1892                                      hw->
1893                                      pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
1894#endif
1895                E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
1896                E1000_WRITE_FLUSH(hw);
1897                mdelay(5);
1898        }
1899
1900        /* Setup the receive address. This involves initializing all of the Receive
1901         * Address Registers (RARs 0 - 15).
1902         */
1903        e1000_init_rx_addrs(hw, enetaddr);
1904
1905        /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
1906        if (hw->mac_type == e1000_82542_rev2_0) {
1907                E1000_WRITE_REG(hw, RCTL, 0);
1908                E1000_WRITE_FLUSH(hw);
1909                mdelay(1);
1910#ifdef CONFIG_DM_ETH
1911                dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
1912#else
1913                pci_write_config_word(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
1914#endif
1915        }
1916
1917        /* Zero out the Multicast HASH table */
1918        DEBUGOUT("Zeroing the MTA\n");
1919        mta_size = E1000_MC_TBL_SIZE;
1920        if (hw->mac_type == e1000_ich8lan)
1921                mta_size = E1000_MC_TBL_SIZE_ICH8LAN;
1922        for (i = 0; i < mta_size; i++) {
1923                E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
1924                /* use write flush to prevent Memory Write Block (MWB) from
1925                 * occuring when accessing our register space */
1926                E1000_WRITE_FLUSH(hw);
1927        }
1928
1929        switch (hw->mac_type) {
1930        case e1000_82545_rev_3:
1931        case e1000_82546_rev_3:
1932        case e1000_igb:
1933                break;
1934        default:
1935        /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
1936        if (hw->bus_type == e1000_bus_type_pcix) {
1937#ifdef CONFIG_DM_ETH
1938                dm_pci_read_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1939                                     &pcix_cmd_word);
1940                dm_pci_read_config16(hw->pdev, PCIX_STATUS_REGISTER_HI,
1941                                     &pcix_stat_hi_word);
1942#else
1943                pci_read_config_word(hw->pdev, PCIX_COMMAND_REGISTER,
1944                                     &pcix_cmd_word);
1945                pci_read_config_word(hw->pdev, PCIX_STATUS_REGISTER_HI,
1946                                     &pcix_stat_hi_word);
1947#endif
1948                cmd_mmrbc =
1949                    (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
1950                    PCIX_COMMAND_MMRBC_SHIFT;
1951                stat_mmrbc =
1952                    (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
1953                    PCIX_STATUS_HI_MMRBC_SHIFT;
1954                if (stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
1955                        stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
1956                if (cmd_mmrbc > stat_mmrbc) {
1957                        pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
1958                        pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
1959#ifdef CONFIG_DM_ETH
1960                        dm_pci_write_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1961                                              pcix_cmd_word);
1962#else
1963                        pci_write_config_word(hw->pdev, PCIX_COMMAND_REGISTER,
1964                                              pcix_cmd_word);
1965#endif
1966                }
1967        }
1968                break;
1969        }
1970
1971        /* More time needed for PHY to initialize */
1972        if (hw->mac_type == e1000_ich8lan)
1973                mdelay(15);
1974        if (hw->mac_type == e1000_igb)
1975                mdelay(15);
1976
1977        /* Call a subroutine to configure the link and setup flow control. */
1978        ret_val = e1000_setup_link(hw);
1979
1980        /* Set the transmit descriptor write-back policy */
1981        if (hw->mac_type > e1000_82544) {
1982                ctrl = E1000_READ_REG(hw, TXDCTL);
1983                ctrl =
1984                    (ctrl & ~E1000_TXDCTL_WTHRESH) |
1985                    E1000_TXDCTL_FULL_TX_DESC_WB;
1986                E1000_WRITE_REG(hw, TXDCTL, ctrl);
1987        }
1988
1989        /* Set the receive descriptor write back policy */
1990        if (hw->mac_type >= e1000_82571) {
1991                ctrl = E1000_READ_REG(hw, RXDCTL);
1992                ctrl =
1993                    (ctrl & ~E1000_RXDCTL_WTHRESH) |
1994                    E1000_RXDCTL_FULL_RX_DESC_WB;
1995                E1000_WRITE_REG(hw, RXDCTL, ctrl);
1996        }
1997
1998        switch (hw->mac_type) {
1999        default:
2000                break;
2001        case e1000_80003es2lan:
2002                /* Enable retransmit on late collisions */
2003                reg_data = E1000_READ_REG(hw, TCTL);
2004                reg_data |= E1000_TCTL_RTLC;
2005                E1000_WRITE_REG(hw, TCTL, reg_data);
2006
2007                /* Configure Gigabit Carry Extend Padding */
2008                reg_data = E1000_READ_REG(hw, TCTL_EXT);
2009                reg_data &= ~E1000_TCTL_EXT_GCEX_MASK;
2010                reg_data |= DEFAULT_80003ES2LAN_TCTL_EXT_GCEX;
2011                E1000_WRITE_REG(hw, TCTL_EXT, reg_data);
2012
2013                /* Configure Transmit Inter-Packet Gap */
2014                reg_data = E1000_READ_REG(hw, TIPG);
2015                reg_data &= ~E1000_TIPG_IPGT_MASK;
2016                reg_data |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
2017                E1000_WRITE_REG(hw, TIPG, reg_data);
2018
2019                reg_data = E1000_READ_REG_ARRAY(hw, FFLT, 0x0001);
2020                reg_data &= ~0x00100000;
2021                E1000_WRITE_REG_ARRAY(hw, FFLT, 0x0001, reg_data);
2022                /* Fall through */
2023        case e1000_82571:
2024        case e1000_82572:
2025        case e1000_ich8lan:
2026                ctrl = E1000_READ_REG(hw, TXDCTL1);
2027                ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH)
2028                        | E1000_TXDCTL_FULL_TX_DESC_WB;
2029                E1000_WRITE_REG(hw, TXDCTL1, ctrl);
2030                break;
2031        case e1000_82573:
2032        case e1000_82574:
2033                reg_data = E1000_READ_REG(hw, GCR);
2034                reg_data |= E1000_GCR_L1_ACT_WITHOUT_L0S_RX;
2035                E1000_WRITE_REG(hw, GCR, reg_data);
2036        case e1000_igb:
2037                break;
2038        }
2039
2040        if (hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER ||
2041                hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3) {
2042                ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
2043                /* Relaxed ordering must be disabled to avoid a parity
2044                 * error crash in a PCI slot. */
2045                ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
2046                E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2047        }
2048
2049        return ret_val;
2050}
2051
2052/******************************************************************************
2053 * Configures flow control and link settings.
2054 *
2055 * hw - Struct containing variables accessed by shared code
2056 *
2057 * Determines which flow control settings to use. Calls the apropriate media-
2058 * specific link configuration function. Configures the flow control settings.
2059 * Assuming the adapter has a valid link partner, a valid link should be
2060 * established. Assumes the hardware has previously been reset and the
2061 * transmitter and receiver are not enabled.
2062 *****************************************************************************/
2063static int
2064e1000_setup_link(struct e1000_hw *hw)
2065{
2066        int32_t ret_val;
2067#ifndef CONFIG_E1000_NO_NVM
2068        uint32_t ctrl_ext;
2069        uint16_t eeprom_data;
2070#endif
2071
2072        DEBUGFUNC();
2073
2074        /* In the case of the phy reset being blocked, we already have a link.
2075         * We do not have to set it up again. */
2076        if (e1000_check_phy_reset_block(hw))
2077                return E1000_SUCCESS;
2078
2079#ifndef CONFIG_E1000_NO_NVM
2080        /* Read and store word 0x0F of the EEPROM. This word contains bits
2081         * that determine the hardware's default PAUSE (flow control) mode,
2082         * a bit that determines whether the HW defaults to enabling or
2083         * disabling auto-negotiation, and the direction of the
2084         * SW defined pins. If there is no SW over-ride of the flow
2085         * control setting, then the variable hw->fc will
2086         * be initialized based on a value in the EEPROM.
2087         */
2088        if (e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG, 1,
2089                                &eeprom_data) < 0) {
2090                DEBUGOUT("EEPROM Read Error\n");
2091                return -E1000_ERR_EEPROM;
2092        }
2093#endif
2094        if (hw->fc == e1000_fc_default) {
2095                switch (hw->mac_type) {
2096                case e1000_ich8lan:
2097                case e1000_82573:
2098                case e1000_82574:
2099                case e1000_igb:
2100                        hw->fc = e1000_fc_full;
2101                        break;
2102                default:
2103#ifndef CONFIG_E1000_NO_NVM
2104                        ret_val = e1000_read_eeprom(hw,
2105                                EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
2106                        if (ret_val) {
2107                                DEBUGOUT("EEPROM Read Error\n");
2108                                return -E1000_ERR_EEPROM;
2109                        }
2110                        if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
2111                                hw->fc = e1000_fc_none;
2112                        else if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
2113                                    EEPROM_WORD0F_ASM_DIR)
2114                                hw->fc = e1000_fc_tx_pause;
2115                        else
2116#endif
2117                                hw->fc = e1000_fc_full;
2118                        break;
2119                }
2120        }
2121
2122        /* We want to save off the original Flow Control configuration just
2123         * in case we get disconnected and then reconnected into a different
2124         * hub or switch with different Flow Control capabilities.
2125         */
2126        if (hw->mac_type == e1000_82542_rev2_0)
2127                hw->fc &= (~e1000_fc_tx_pause);
2128
2129        if ((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
2130                hw->fc &= (~e1000_fc_rx_pause);
2131
2132        hw->original_fc = hw->fc;
2133
2134        DEBUGOUT("After fix-ups FlowControl is now = %x\n", hw->fc);
2135
2136#ifndef CONFIG_E1000_NO_NVM
2137        /* Take the 4 bits from EEPROM word 0x0F that determine the initial
2138         * polarity value for the SW controlled pins, and setup the
2139         * Extended Device Control reg with that info.
2140         * This is needed because one of the SW controlled pins is used for
2141         * signal detection.  So this should be done before e1000_setup_pcs_link()
2142         * or e1000_phy_setup() is called.
2143         */
2144        if (hw->mac_type == e1000_82543) {
2145                ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
2146                            SWDPIO__EXT_SHIFT);
2147                E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2148        }
2149#endif
2150
2151        /* Call the necessary subroutine to configure the link. */
2152        ret_val = (hw->media_type == e1000_media_type_fiber) ?
2153            e1000_setup_fiber_link(hw) : e1000_setup_copper_link(hw);
2154        if (ret_val < 0) {
2155                return ret_val;
2156        }
2157
2158        /* Initialize the flow control address, type, and PAUSE timer
2159         * registers to their default values.  This is done even if flow
2160         * control is disabled, because it does not hurt anything to
2161         * initialize these registers.
2162         */
2163        DEBUGOUT("Initializing the Flow Control address, type"
2164                        "and timer regs\n");
2165
2166        /* FCAL/H and FCT are hardcoded to standard values in e1000_ich8lan. */
2167        if (hw->mac_type != e1000_ich8lan) {
2168                E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
2169                E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
2170                E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
2171        }
2172
2173        E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
2174
2175        /* Set the flow control receive threshold registers.  Normally,
2176         * these registers will be set to a default threshold that may be
2177         * adjusted later by the driver's runtime code.  However, if the
2178         * ability to transmit pause frames in not enabled, then these
2179         * registers will be set to 0.
2180         */
2181        if (!(hw->fc & e1000_fc_tx_pause)) {
2182                E1000_WRITE_REG(hw, FCRTL, 0);
2183                E1000_WRITE_REG(hw, FCRTH, 0);
2184        } else {
2185                /* We need to set up the Receive Threshold high and low water marks
2186                 * as well as (optionally) enabling the transmission of XON frames.
2187                 */
2188                if (hw->fc_send_xon) {
2189                        E1000_WRITE_REG(hw, FCRTL,
2190                                        (hw->fc_low_water | E1000_FCRTL_XONE));
2191                        E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2192                } else {
2193                        E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
2194                        E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2195                }
2196        }
2197        return ret_val;
2198}
2199
2200/******************************************************************************
2201 * Sets up link for a fiber based adapter
2202 *
2203 * hw - Struct containing variables accessed by shared code
2204 *
2205 * Manipulates Physical Coding Sublayer functions in order to configure
2206 * link. Assumes the hardware has been previously reset and the transmitter
2207 * and receiver are not enabled.
2208 *****************************************************************************/
2209static int
2210e1000_setup_fiber_link(struct e1000_hw *hw)
2211{
2212        uint32_t ctrl;
2213        uint32_t status;
2214        uint32_t txcw = 0;
2215        uint32_t i;
2216        uint32_t signal;
2217        int32_t ret_val;
2218
2219        DEBUGFUNC();
2220        /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
2221         * set when the optics detect a signal. On older adapters, it will be
2222         * cleared when there is a signal
2223         */
2224        ctrl = E1000_READ_REG(hw, CTRL);
2225        if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
2226                signal = E1000_CTRL_SWDPIN1;
2227        else
2228                signal = 0;
2229
2230        printf("signal for %s is %x (ctrl %08x)!!!!\n", hw->name, signal,
2231               ctrl);
2232        /* Take the link out of reset */
2233        ctrl &= ~(E1000_CTRL_LRST);
2234
2235        e1000_config_collision_dist(hw);
2236
2237        /* Check for a software override of the flow control settings, and setup
2238         * the device accordingly.  If auto-negotiation is enabled, then software
2239         * will have to set the "PAUSE" bits to the correct value in the Tranmsit
2240         * Config Word Register (TXCW) and re-start auto-negotiation.  However, if
2241         * auto-negotiation is disabled, then software will have to manually
2242         * configure the two flow control enable bits in the CTRL register.
2243         *
2244         * The possible values of the "fc" parameter are:
2245         *      0:  Flow control is completely disabled
2246         *      1:  Rx flow control is enabled (we can receive pause frames, but
2247         *          not send pause frames).
2248         *      2:  Tx flow control is enabled (we can send pause frames but we do
2249         *          not support receiving pause frames).
2250         *      3:  Both Rx and TX flow control (symmetric) are enabled.
2251         */
2252        switch (hw->fc) {
2253        case e1000_fc_none:
2254                /* Flow control is completely disabled by a software over-ride. */
2255                txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
2256                break;
2257        case e1000_fc_rx_pause:
2258                /* RX Flow control is enabled and TX Flow control is disabled by a
2259                 * software over-ride. Since there really isn't a way to advertise
2260                 * that we are capable of RX Pause ONLY, we will advertise that we
2261                 * support both symmetric and asymmetric RX PAUSE. Later, we will
2262                 *  disable the adapter's ability to send PAUSE frames.
2263                 */
2264                txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2265                break;
2266        case e1000_fc_tx_pause:
2267                /* TX Flow control is enabled, and RX Flow control is disabled, by a
2268                 * software over-ride.
2269                 */
2270                txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
2271                break;
2272        case e1000_fc_full:
2273                /* Flow control (both RX and TX) is enabled by a software over-ride. */
2274                txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2275                break;
2276        default:
2277                DEBUGOUT("Flow control param set incorrectly\n");
2278                return -E1000_ERR_CONFIG;
2279                break;
2280        }
2281
2282        /* Since auto-negotiation is enabled, take the link out of reset (the link
2283         * will be in reset, because we previously reset the chip). This will
2284         * restart auto-negotiation.  If auto-neogtiation is successful then the
2285         * link-up status bit will be set and the flow control enable bits (RFCE
2286         * and TFCE) will be set according to their negotiated value.
2287         */
2288        DEBUGOUT("Auto-negotiation enabled (%#x)\n", txcw);
2289
2290        E1000_WRITE_REG(hw, TXCW, txcw);
2291        E1000_WRITE_REG(hw, CTRL, ctrl);
2292        E1000_WRITE_FLUSH(hw);
2293
2294        hw->txcw = txcw;
2295        mdelay(1);
2296
2297        /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
2298         * indication in the Device Status Register.  Time-out if a link isn't
2299         * seen in 500 milliseconds seconds (Auto-negotiation should complete in
2300         * less than 500 milliseconds even if the other end is doing it in SW).
2301         */
2302        if ((E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
2303                DEBUGOUT("Looking for Link\n");
2304                for (i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
2305                        mdelay(10);
2306                        status = E1000_READ_REG(hw, STATUS);
2307                        if (status & E1000_STATUS_LU)
2308                                break;
2309                }
2310                if (i == (LINK_UP_TIMEOUT / 10)) {
2311                        /* AutoNeg failed to achieve a link, so we'll call
2312                         * e1000_check_for_link. This routine will force the link up if we
2313                         * detect a signal. This will allow us to communicate with
2314                         * non-autonegotiating link partners.
2315                         */
2316                        DEBUGOUT("Never got a valid link from auto-neg!!!\n");
2317                        hw->autoneg_failed = 1;
2318                        ret_val = e1000_check_for_link(hw);
2319                        if (ret_val < 0) {
2320                                DEBUGOUT("Error while checking for link\n");
2321                                return ret_val;
2322                        }
2323                        hw->autoneg_failed = 0;
2324                } else {
2325                        hw->autoneg_failed = 0;
2326                        DEBUGOUT("Valid Link Found\n");
2327                }
2328        } else {
2329                DEBUGOUT("No Signal Detected\n");
2330                return -E1000_ERR_NOLINK;
2331        }
2332        return 0;
2333}
2334
2335/******************************************************************************
2336* Make sure we have a valid PHY and change PHY mode before link setup.
2337*
2338* hw - Struct containing variables accessed by shared code
2339******************************************************************************/
2340static int32_t
2341e1000_copper_link_preconfig(struct e1000_hw *hw)
2342{
2343        uint32_t ctrl;
2344        int32_t ret_val;
2345        uint16_t phy_data;
2346
2347        DEBUGFUNC();
2348
2349        ctrl = E1000_READ_REG(hw, CTRL);
2350        /* With 82543, we need to force speed and duplex on the MAC equal to what
2351         * the PHY speed and duplex configuration is. In addition, we need to
2352         * perform a hardware reset on the PHY to take it out of reset.
2353         */
2354        if (hw->mac_type > e1000_82543) {
2355                ctrl |= E1000_CTRL_SLU;
2356                ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
2357                E1000_WRITE_REG(hw, CTRL, ctrl);
2358        } else {
2359                ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX
2360                                | E1000_CTRL_SLU);
2361                E1000_WRITE_REG(hw, CTRL, ctrl);
2362                ret_val = e1000_phy_hw_reset(hw);
2363                if (ret_val)
2364                        return ret_val;
2365        }
2366
2367        /* Make sure we have a valid PHY */
2368        ret_val = e1000_detect_gig_phy(hw);
2369        if (ret_val) {
2370                DEBUGOUT("Error, did not detect valid phy.\n");
2371                return ret_val;
2372        }
2373        DEBUGOUT("Phy ID = %x\n", hw->phy_id);
2374
2375        /* Set PHY to class A mode (if necessary) */
2376        ret_val = e1000_set_phy_mode(hw);
2377        if (ret_val)
2378                return ret_val;
2379        if ((hw->mac_type == e1000_82545_rev_3) ||
2380                (hw->mac_type == e1000_82546_rev_3)) {
2381                ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2382                                &phy_data);
2383                phy_data |= 0x00000008;
2384                ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2385                                phy_data);
2386        }
2387
2388        if (hw->mac_type <= e1000_82543 ||
2389                hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
2390                hw->mac_type == e1000_82541_rev_2
2391                || hw->mac_type == e1000_82547_rev_2)
2392                        hw->phy_reset_disable = false;
2393
2394        return E1000_SUCCESS;
2395}
2396
2397/*****************************************************************************
2398 *
2399 * This function sets the lplu state according to the active flag.  When
2400 * activating lplu this function also disables smart speed and vise versa.
2401 * lplu will not be activated unless the device autonegotiation advertisment
2402 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2403 * hw: Struct containing variables accessed by shared code
2404 * active - true to enable lplu false to disable lplu.
2405 *
2406 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2407 *            E1000_SUCCESS at any other case.
2408 *
2409 ****************************************************************************/
2410
2411static int32_t
2412e1000_set_d3_lplu_state(struct e1000_hw *hw, bool active)
2413{
2414        uint32_t phy_ctrl = 0;
2415        int32_t ret_val;
2416        uint16_t phy_data;
2417        DEBUGFUNC();
2418
2419        if (hw->phy_type != e1000_phy_igp && hw->phy_type != e1000_phy_igp_2
2420            && hw->phy_type != e1000_phy_igp_3)
2421                return E1000_SUCCESS;
2422
2423        /* During driver activity LPLU should not be used or it will attain link
2424         * from the lowest speeds starting from 10Mbps. The capability is used
2425         * for Dx transitions and states */
2426        if (hw->mac_type == e1000_82541_rev_2
2427                        || hw->mac_type == e1000_82547_rev_2) {
2428                ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO,
2429                                &phy_data);
2430                if (ret_val)
2431                        return ret_val;
2432        } else if (hw->mac_type == e1000_ich8lan) {
2433                /* MAC writes into PHY register based on the state transition
2434                 * and start auto-negotiation. SW driver can overwrite the
2435                 * settings in CSR PHY power control E1000_PHY_CTRL register. */
2436                phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
2437        } else {
2438                ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2439                                &phy_data);
2440                if (ret_val)
2441                        return ret_val;
2442        }
2443
2444        if (!active) {
2445                if (hw->mac_type == e1000_82541_rev_2 ||
2446                        hw->mac_type == e1000_82547_rev_2) {
2447                        phy_data &= ~IGP01E1000_GMII_FLEX_SPD;
2448                        ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
2449                                        phy_data);
2450                        if (ret_val)
2451                                return ret_val;
2452                } else {
2453                        if (hw->mac_type == e1000_ich8lan) {
2454                                phy_ctrl &= ~E1000_PHY_CTRL_NOND0A_LPLU;
2455                                E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2456                        } else {
2457                                phy_data &= ~IGP02E1000_PM_D3_LPLU;
2458                                ret_val = e1000_write_phy_reg(hw,
2459                                        IGP02E1000_PHY_POWER_MGMT, phy_data);
2460                                if (ret_val)
2461                                        return ret_val;
2462                        }
2463                }
2464
2465        /* LPLU and SmartSpeed are mutually exclusive.  LPLU is used during
2466         * Dx states where the power conservation is most important.  During
2467         * driver activity we should enable SmartSpeed, so performance is
2468         * maintained. */
2469                if (hw->smart_speed == e1000_smart_speed_on) {
2470                        ret_val = e1000_read_phy_reg(hw,
2471                                        IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2472                        if (ret_val)
2473                                return ret_val;
2474
2475                        phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2476                        ret_val = e1000_write_phy_reg(hw,
2477                                        IGP01E1000_PHY_PORT_CONFIG, phy_data);
2478                        if (ret_val)
2479                                return ret_val;
2480                } else if (hw->smart_speed == e1000_smart_speed_off) {
2481                        ret_val = e1000_read_phy_reg(hw,
2482                                        IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2483                        if (ret_val)
2484                                return ret_val;
2485
2486                        phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2487                        ret_val = e1000_write_phy_reg(hw,
2488                                        IGP01E1000_PHY_PORT_CONFIG, phy_data);
2489                        if (ret_val)
2490                                return ret_val;
2491                }
2492
2493        } else if ((hw->autoneg_advertised == AUTONEG_ADVERTISE_SPEED_DEFAULT)
2494                || (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_ALL) ||
2495                (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_100_ALL)) {
2496
2497                if (hw->mac_type == e1000_82541_rev_2 ||
2498                    hw->mac_type == e1000_82547_rev_2) {
2499                        phy_data |= IGP01E1000_GMII_FLEX_SPD;
2500                        ret_val = e1000_write_phy_reg(hw,
2501                                        IGP01E1000_GMII_FIFO, phy_data);
2502                        if (ret_val)
2503                                return ret_val;
2504                } else {
2505                        if (hw->mac_type == e1000_ich8lan) {
2506                                phy_ctrl |= E1000_PHY_CTRL_NOND0A_LPLU;
2507                                E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2508                        } else {
2509                                phy_data |= IGP02E1000_PM_D3_LPLU;
2510                                ret_val = e1000_write_phy_reg(hw,
2511                                        IGP02E1000_PHY_POWER_MGMT, phy_data);
2512                                if (ret_val)
2513                                        return ret_val;
2514                        }
2515                }
2516
2517                /* When LPLU is enabled we should disable SmartSpeed */
2518                ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2519                                &phy_data);
2520                if (ret_val)
2521                        return ret_val;
2522
2523                phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2524                ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2525                                phy_data);
2526                if (ret_val)
2527                        return ret_val;
2528        }
2529        return E1000_SUCCESS;
2530}
2531
2532/*****************************************************************************
2533 *
2534 * This function sets the lplu d0 state according to the active flag.  When
2535 * activating lplu this function also disables smart speed and vise versa.
2536 * lplu will not be activated unless the device autonegotiation advertisment
2537 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2538 * hw: Struct containing variables accessed by shared code
2539 * active - true to enable lplu false to disable lplu.
2540 *
2541 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2542 *            E1000_SUCCESS at any other case.
2543 *
2544 ****************************************************************************/
2545
2546static int32_t
2547e1000_set_d0_lplu_state(struct e1000_hw *hw, bool active)
2548{
2549        uint32_t phy_ctrl = 0;
2550        int32_t ret_val;
2551        uint16_t phy_data;
2552        DEBUGFUNC();
2553
2554        if (hw->mac_type <= e1000_82547_rev_2)
2555                return E1000_SUCCESS;
2556
2557        if (hw->mac_type == e1000_ich8lan) {
2558                phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
2559        } else if (hw->mac_type == e1000_igb) {
2560                phy_ctrl = E1000_READ_REG(hw, I210_PHY_CTRL);
2561        } else {
2562                ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2563                                &phy_data);
2564                if (ret_val)
2565                        return ret_val;
2566        }
2567
2568        if (!active) {
2569                if (hw->mac_type == e1000_ich8lan) {
2570                        phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2571                        E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2572                } else if (hw->mac_type == e1000_igb) {
2573                        phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2574                        E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
2575                } else {
2576                        phy_data &= ~IGP02E1000_PM_D0_LPLU;
2577                        ret_val = e1000_write_phy_reg(hw,
2578                                        IGP02E1000_PHY_POWER_MGMT, phy_data);
2579                        if (ret_val)
2580                                return ret_val;
2581                }
2582
2583                if (hw->mac_type == e1000_igb)
2584                        return E1000_SUCCESS;
2585
2586        /* LPLU and SmartSpeed are mutually exclusive.  LPLU is used during
2587         * Dx states where the power conservation is most important.  During
2588         * driver activity we should enable SmartSpeed, so performance is
2589         * maintained. */
2590                if (hw->smart_speed == e1000_smart_speed_on) {
2591                        ret_val = e1000_read_phy_reg(hw,
2592                                        IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2593                        if (ret_val)
2594                                return ret_val;
2595
2596                        phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2597                        ret_val = e1000_write_phy_reg(hw,
2598                                        IGP01E1000_PHY_PORT_CONFIG, phy_data);
2599                        if (ret_val)
2600                                return ret_val;
2601                } else if (hw->smart_speed == e1000_smart_speed_off) {
2602                        ret_val = e1000_read_phy_reg(hw,
2603                                        IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2604                        if (ret_val)
2605                                return ret_val;
2606
2607                        phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2608                        ret_val = e1000_write_phy_reg(hw,
2609                                        IGP01E1000_PHY_PORT_CONFIG, phy_data);
2610                        if (ret_val)
2611                                return ret_val;
2612                }
2613
2614
2615        } else {
2616
2617                if (hw->mac_type == e1000_ich8lan) {
2618                        phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2619                        E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2620                } else if (hw->mac_type == e1000_igb) {
2621                        phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2622                        E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
2623                } else {
2624                        phy_data |= IGP02E1000_PM_D0_LPLU;
2625                        ret_val = e1000_write_phy_reg(hw,
2626                                        IGP02E1000_PHY_POWER_MGMT, phy_data);
2627                        if (ret_val)
2628                                return ret_val;
2629                }
2630
2631                if (hw->mac_type == e1000_igb)
2632                        return E1000_SUCCESS;
2633
2634                /* When LPLU is enabled we should disable SmartSpeed */
2635                ret_val = e1000_read_phy_reg(hw,
2636                                IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2637                if (ret_val)
2638                        return ret_val;
2639
2640                phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2641                ret_val = e1000_write_phy_reg(hw,
2642                                IGP01E1000_PHY_PORT_CONFIG, phy_data);
2643                if (ret_val)
2644                        return ret_val;
2645
2646        }
2647        return E1000_SUCCESS;
2648}
2649
2650/********************************************************************
2651* Copper link setup for e1000_phy_igp series.
2652*
2653* hw - Struct containing variables accessed by shared code
2654*********************************************************************/
2655static int32_t
2656e1000_copper_link_igp_setup(struct e1000_hw *hw)
2657{
2658        uint32_t led_ctrl;
2659        int32_t ret_val;
2660        uint16_t phy_data;
2661
2662        DEBUGFUNC();
2663
2664        if (hw->phy_reset_disable)
2665                return E1000_SUCCESS;
2666
2667        ret_val = e1000_phy_reset(hw);
2668        if (ret_val) {
2669                DEBUGOUT("Error Resetting the PHY\n");
2670                return ret_val;
2671        }
2672
2673        /* Wait 15ms for MAC to configure PHY from eeprom settings */
2674        mdelay(15);
2675        if (hw->mac_type != e1000_ich8lan) {
2676                /* Configure activity LED after PHY reset */
2677                led_ctrl = E1000_READ_REG(hw, LEDCTL);
2678                led_ctrl &= IGP_ACTIVITY_LED_MASK;
2679                led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
2680                E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
2681        }
2682
2683        /* The NVM settings will configure LPLU in D3 for IGP2 and IGP3 PHYs */
2684        if (hw->phy_type == e1000_phy_igp) {
2685                /* disable lplu d3 during driver init */
2686                ret_val = e1000_set_d3_lplu_state(hw, false);
2687                if (ret_val) {
2688                        DEBUGOUT("Error Disabling LPLU D3\n");
2689                        return ret_val;
2690                }
2691        }
2692
2693        /* disable lplu d0 during driver init */
2694        ret_val = e1000_set_d0_lplu_state(hw, false);
2695        if (ret_val) {
2696                DEBUGOUT("Error Disabling LPLU D0\n");
2697                return ret_val;
2698        }
2699        /* Configure mdi-mdix settings */
2700        ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
2701        if (ret_val)
2702                return ret_val;
2703
2704        if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
2705                hw->dsp_config_state = e1000_dsp_config_disabled;
2706                /* Force MDI for earlier revs of the IGP PHY */
2707                phy_data &= ~(IGP01E1000_PSCR_AUTO_MDIX
2708                                | IGP01E1000_PSCR_FORCE_MDI_MDIX);
2709                hw->mdix = 1;
2710
2711        } else {
2712                hw->dsp_config_state = e1000_dsp_config_enabled;
2713                phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
2714
2715                switch (hw->mdix) {
2716                case 1:
2717                        phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
2718                        break;
2719                case 2:
2720                        phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
2721                        break;
2722                case 0:
2723                default:
2724                        phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
2725                        break;
2726                }
2727        }
2728        ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
2729        if (ret_val)
2730                return ret_val;
2731
2732        /* set auto-master slave resolution settings */
2733        if (hw->autoneg) {
2734                e1000_ms_type phy_ms_setting = hw->master_slave;
2735
2736                if (hw->ffe_config_state == e1000_ffe_config_active)
2737                        hw->ffe_config_state = e1000_ffe_config_enabled;
2738
2739                if (hw->dsp_config_state == e1000_dsp_config_activated)
2740                        hw->dsp_config_state = e1000_dsp_config_enabled;
2741
2742                /* when autonegotiation advertisment is only 1000Mbps then we
2743                  * should disable SmartSpeed and enable Auto MasterSlave
2744                  * resolution as hardware default. */
2745                if (hw->autoneg_advertised == ADVERTISE_1000_FULL) {
2746                        /* Disable SmartSpeed */
2747                        ret_val = e1000_read_phy_reg(hw,
2748                                        IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2749                        if (ret_val)
2750                                return ret_val;
2751                        phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2752                        ret_val = e1000_write_phy_reg(hw,
2753                                        IGP01E1000_PHY_PORT_CONFIG, phy_data);
2754                        if (ret_val)
2755                                return ret_val;
2756                        /* Set auto Master/Slave resolution process */
2757                        ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
2758                                        &phy_data);
2759                        if (ret_val)
2760                                return ret_val;
2761                        phy_data &= ~CR_1000T_MS_ENABLE;
2762                        ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
2763                                        phy_data);
2764                        if (ret_val)
2765                                return ret_val;
2766                }
2767
2768                ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
2769                if (ret_val)
2770                        return ret_val;
2771
2772                /* load defaults for future use */
2773                hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
2774                                ((phy_data & CR_1000T_MS_VALUE) ?
2775                                e1000_ms_force_master :
2776                                e1000_ms_force_slave) :
2777                                e1000_ms_auto;
2778
2779                switch (phy_ms_setting) {
2780                case e1000_ms_force_master:
2781                        phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
2782                        break;
2783                case e1000_ms_force_slave:
2784                        phy_data |= CR_1000T_MS_ENABLE;
2785                        phy_data &= ~(CR_1000T_MS_VALUE);
2786                        break;
2787                case e1000_ms_auto:
2788                        phy_data &= ~CR_1000T_MS_ENABLE;
2789                default:
2790                        break;
2791                }
2792                ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
2793                if (ret_val)
2794                        return ret_val;
2795        }
2796
2797        return E1000_SUCCESS;
2798}
2799
2800/*****************************************************************************
2801 * This function checks the mode of the firmware.
2802 *
2803 * returns  - true when the mode is IAMT or false.
2804 ****************************************************************************/
2805bool
2806e1000_check_mng_mode(struct e1000_hw *hw)
2807{
2808        uint32_t fwsm;
2809        DEBUGFUNC();
2810
2811        fwsm = E1000_READ_REG(hw, FWSM);
2812
2813        if (hw->mac_type == e1000_ich8lan) {
2814                if ((fwsm & E1000_FWSM_MODE_MASK) ==
2815                    (E1000_MNG_ICH_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
2816                        return true;
2817        } else if ((fwsm & E1000_FWSM_MODE_MASK) ==
2818                       (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
2819                        return true;
2820
2821        return false;
2822}
2823
2824static int32_t
2825e1000_write_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t data)
2826{
2827        uint16_t swfw = E1000_SWFW_PHY0_SM;
2828        uint32_t reg_val;
2829        DEBUGFUNC();
2830
2831        if (e1000_is_second_port(hw))
2832                swfw = E1000_SWFW_PHY1_SM;
2833
2834        if (e1000_swfw_sync_acquire(hw, swfw))
2835                return -E1000_ERR_SWFW_SYNC;
2836
2837        reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT)
2838                        & E1000_KUMCTRLSTA_OFFSET) | data;
2839        E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2840        udelay(2);
2841
2842        return E1000_SUCCESS;
2843}
2844
2845static int32_t
2846e1000_read_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t *data)
2847{
2848        uint16_t swfw = E1000_SWFW_PHY0_SM;
2849        uint32_t reg_val;
2850        DEBUGFUNC();
2851
2852        if (e1000_is_second_port(hw))
2853                swfw = E1000_SWFW_PHY1_SM;
2854
2855        if (e1000_swfw_sync_acquire(hw, swfw)) {
2856                debug("%s[%i]\n", __func__, __LINE__);
2857                return -E1000_ERR_SWFW_SYNC;
2858        }
2859
2860        /* Write register address */
2861        reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT) &
2862                        E1000_KUMCTRLSTA_OFFSET) | E1000_KUMCTRLSTA_REN;
2863        E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2864        udelay(2);
2865
2866        /* Read the data returned */
2867        reg_val = E1000_READ_REG(hw, KUMCTRLSTA);
2868        *data = (uint16_t)reg_val;
2869
2870        return E1000_SUCCESS;
2871}
2872
2873/********************************************************************
2874* Copper link setup for e1000_phy_gg82563 series.
2875*
2876* hw - Struct containing variables accessed by shared code
2877*********************************************************************/
2878static int32_t
2879e1000_copper_link_ggp_setup(struct e1000_hw *hw)
2880{
2881        int32_t ret_val;
2882        uint16_t phy_data;
2883        uint32_t reg_data;
2884
2885        DEBUGFUNC();
2886
2887        if (!hw->phy_reset_disable) {
2888                /* Enable CRS on TX for half-duplex operation. */
2889                ret_val = e1000_read_phy_reg(hw,
2890                                GG82563_PHY_MAC_SPEC_CTRL, &phy_data);
2891                if (ret_val)
2892                        return ret_val;
2893
2894                phy_data |= GG82563_MSCR_ASSERT_CRS_ON_TX;
2895                /* Use 25MHz for both link down and 1000BASE-T for Tx clock */
2896                phy_data |= GG82563_MSCR_TX_CLK_1000MBPS_25MHZ;
2897
2898                ret_val = e1000_write_phy_reg(hw,
2899                                GG82563_PHY_MAC_SPEC_CTRL, phy_data);
2900                if (ret_val)
2901                        return ret_val;
2902
2903                /* Options:
2904                 *   MDI/MDI-X = 0 (default)
2905                 *   0 - Auto for all speeds
2906                 *   1 - MDI mode
2907                 *   2 - MDI-X mode
2908                 *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
2909                 */
2910                ret_val = e1000_read_phy_reg(hw,
2911                                GG82563_PHY_SPEC_CTRL, &phy_data);
2912                if (ret_val)
2913                        return ret_val;
2914
2915                phy_data &= ~GG82563_PSCR_CROSSOVER_MODE_MASK;
2916
2917                switch (hw->mdix) {
2918                case 1:
2919                        phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDI;
2920                        break;
2921                case 2:
2922                        phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDIX;
2923                        break;
2924                case 0:
2925                default:
2926                        phy_data |= GG82563_PSCR_CROSSOVER_MODE_AUTO;
2927                        break;
2928                }
2929
2930                /* Options:
2931                 *   disable_polarity_correction = 0 (default)
2932                 *       Automatic Correction for Reversed Cable Polarity
2933                 *   0 - Disabled
2934                 *   1 - Enabled
2935                 */
2936                phy_data &= ~GG82563_PSCR_POLARITY_REVERSAL_DISABLE;
2937                ret_val = e1000_write_phy_reg(hw,
2938                                GG82563_PHY_SPEC_CTRL, phy_data);
2939
2940                if (ret_val)
2941                        return ret_val;
2942
2943                /* SW Reset the PHY so all changes take effect */
2944                ret_val = e1000_phy_reset(hw);
2945                if (ret_val) {
2946                        DEBUGOUT("Error Resetting the PHY\n");
2947                        return ret_val;
2948                }
2949        } /* phy_reset_disable */
2950
2951        if (hw->mac_type == e1000_80003es2lan) {
2952                /* Bypass RX and TX FIFO's */
2953                ret_val = e1000_write_kmrn_reg(hw,
2954                                E1000_KUMCTRLSTA_OFFSET_FIFO_CTRL,
2955                                E1000_KUMCTRLSTA_FIFO_CTRL_RX_BYPASS
2956                                | E1000_KUMCTRLSTA_FIFO_CTRL_TX_BYPASS);
2957                if (ret_val)
2958                        return ret_val;
2959
2960                ret_val = e1000_read_phy_reg(hw,
2961                                GG82563_PHY_SPEC_CTRL_2, &phy_data);
2962                if (ret_val)
2963                        return ret_val;
2964
2965                phy_data &= ~GG82563_PSCR2_REVERSE_AUTO_NEG;
2966                ret_val = e1000_write_phy_reg(hw,
2967                                GG82563_PHY_SPEC_CTRL_2, phy_data);
2968
2969                if (ret_val)
2970                        return ret_val;
2971
2972                reg_data = E1000_READ_REG(hw, CTRL_EXT);
2973                reg_data &= ~(E1000_CTRL_EXT_LINK_MODE_MASK);
2974                E1000_WRITE_REG(hw, CTRL_EXT, reg_data);
2975
2976                ret_val = e1000_read_phy_reg(hw,
2977                                GG82563_PHY_PWR_MGMT_CTRL, &phy_data);
2978                if (ret_val)
2979                        return ret_val;
2980
2981        /* Do not init these registers when the HW is in IAMT mode, since the
2982         * firmware will have already initialized them.  We only initialize
2983         * them if the HW is not in IAMT mode.
2984         */
2985                if (e1000_check_mng_mode(hw) == false) {
2986                        /* Enable Electrical Idle on the PHY */
2987                        phy_data |= GG82563_PMCR_ENABLE_ELECTRICAL_IDLE;
2988                        ret_val = e1000_write_phy_reg(hw,
2989                                        GG82563_PHY_PWR_MGMT_CTRL, phy_data);
2990                        if (ret_val)
2991                                return ret_val;
2992
2993                        ret_val = e1000_read_phy_reg(hw,
2994                                        GG82563_PHY_KMRN_MODE_CTRL, &phy_data);
2995                        if (ret_val)
2996                                return ret_val;
2997
2998                        phy_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
2999                        ret_val = e1000_write_phy_reg(hw,
3000                                        GG82563_PHY_KMRN_MODE_CTRL, phy_data);
3001
3002                        if (ret_val)
3003                                return ret_val;
3004                }
3005
3006                /* Workaround: Disable padding in Kumeran interface in the MAC
3007                 * and in the PHY to avoid CRC errors.
3008                 */
3009                ret_val = e1000_read_phy_reg(hw,
3010                                GG82563_PHY_INBAND_CTRL, &phy_data);
3011                if (ret_val)
3012                        return ret_val;
3013                phy_data |= GG82563_ICR_DIS_PADDING;
3014                ret_val = e1000_write_phy_reg(hw,
3015                                GG82563_PHY_INBAND_CTRL, phy_data);
3016                if (ret_val)
3017                        return ret_val;
3018        }
3019        return E1000_SUCCESS;
3020}
3021
3022/********************************************************************
3023* Copper link setup for e1000_phy_m88 series.
3024*
3025* hw - Struct containing variables accessed by shared code
3026*********************************************************************/
3027static int32_t
3028e1000_copper_link_mgp_setup(struct e1000_hw *hw)
3029{
3030        int32_t ret_val;
3031        uint16_t phy_data;
3032
3033        DEBUGFUNC();
3034
3035        if (hw->phy_reset_disable)
3036                return E1000_SUCCESS;
3037
3038        /* Enable CRS on TX. This must be set for half-duplex operation. */
3039        ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
3040        if (ret_val)
3041                return ret_val;
3042
3043        phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
3044
3045        /* Options:
3046         *   MDI/MDI-X = 0 (default)
3047         *   0 - Auto for all speeds
3048         *   1 - MDI mode
3049         *   2 - MDI-X mode
3050         *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
3051         */
3052        phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
3053
3054        switch (hw->mdix) {
3055        case 1:
3056                phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
3057                break;
3058        case 2:
3059                phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
3060                break;
3061        case 3:
3062                phy_data |= M88E1000_PSCR_AUTO_X_1000T;
3063                break;
3064        case 0:
3065        default:
3066                phy_data |= M88E1000_PSCR_AUTO_X_MODE;
3067                break;
3068        }
3069
3070        /* Options:
3071         *   disable_polarity_correction = 0 (default)
3072         *       Automatic Correction for Reversed Cable Polarity
3073         *   0 - Disabled
3074         *   1 - Enabled
3075         */
3076        phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
3077        ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
3078        if (ret_val)
3079                return ret_val;
3080
3081        if (hw->phy_revision < M88E1011_I_REV_4) {
3082                /* Force TX_CLK in the Extended PHY Specific Control Register
3083                 * to 25MHz clock.
3084                 */
3085                ret_val = e1000_read_phy_reg(hw,
3086                                M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
3087                if (ret_val)
3088                        return ret_val;
3089
3090                phy_data |= M88E1000_EPSCR_TX_CLK_25;
3091
3092                if ((hw->phy_revision == E1000_REVISION_2) &&
3093                        (hw->phy_id == M88E1111_I_PHY_ID)) {
3094                        /* Vidalia Phy, set the downshift counter to 5x */
3095                        phy_data &= ~(M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK);
3096                        phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
3097                        ret_val = e1000_write_phy_reg(hw,
3098                                        M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3099                        if (ret_val)
3100                                return ret_val;
3101                } else {
3102                        /* Configure Master and Slave downshift values */
3103                        phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK
3104                                        | M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
3105                        phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X
3106                                        | M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
3107                        ret_val = e1000_write_phy_reg(hw,
3108                                        M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3109                        if (ret_val)
3110                                return ret_val;
3111                }
3112        }
3113
3114        /* SW Reset the PHY so all changes take effect */
3115        ret_val = e1000_phy_reset(hw);
3116        if (ret_val) {
3117                DEBUGOUT("Error Resetting the PHY\n");
3118                return ret_val;
3119        }
3120
3121        return E1000_SUCCESS;
3122}
3123
3124/********************************************************************
3125* Setup auto-negotiation and flow control advertisements,
3126* and then perform auto-negotiation.
3127*
3128* hw - Struct containing variables accessed by shared code
3129*********************************************************************/
3130static int32_t
3131e1000_copper_link_autoneg(struct e1000_hw *hw)
3132{
3133        int32_t ret_val;
3134        uint16_t phy_data;
3135
3136        DEBUGFUNC();
3137
3138        /* Perform some bounds checking on the hw->autoneg_advertised
3139         * parameter.  If this variable is zero, then set it to the default.
3140         */
3141        hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
3142
3143        /* If autoneg_advertised is zero, we assume it was not defaulted
3144         * by the calling code so we set to advertise full capability.
3145         */
3146        if (hw->autoneg_advertised == 0)
3147                hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
3148
3149        /* IFE phy only supports 10/100 */
3150        if (hw->phy_type == e1000_phy_ife)
3151                hw->autoneg_advertised &= AUTONEG_ADVERTISE_10_100_ALL;
3152
3153        DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
3154        ret_val = e1000_phy_setup_autoneg(hw);
3155        if (ret_val) {
3156                DEBUGOUT("Error Setting up Auto-Negotiation\n");
3157                return ret_val;
3158        }
3159        DEBUGOUT("Restarting Auto-Neg\n");
3160
3161        /* Restart auto-negotiation by setting the Auto Neg Enable bit and
3162         * the Auto Neg Restart bit in the PHY control register.
3163         */
3164        ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
3165        if (ret_val)
3166                return ret_val;
3167
3168        phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
3169        ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
3170        if (ret_val)
3171                return ret_val;
3172
3173        /* Does the user want to wait for Auto-Neg to complete here, or
3174         * check at a later time (for example, callback routine).
3175         */
3176        /* If we do not wait for autonegtation to complete I
3177         * do not see a valid link status.
3178         * wait_autoneg_complete = 1 .
3179         */
3180        if (hw->wait_autoneg_complete) {
3181                ret_val = e1000_wait_autoneg(hw);
3182                if (ret_val) {
3183                        DEBUGOUT("Error while waiting for autoneg"
3184                                        "to complete\n");
3185                        return ret_val;
3186                }
3187        }
3188
3189        hw->get_link_status = true;
3190
3191        return E1000_SUCCESS;
3192}
3193
3194/******************************************************************************
3195* Config the MAC and the PHY after link is up.
3196*   1) Set up the MAC to the current PHY speed/duplex
3197*      if we are on 82543.  If we
3198*      are on newer silicon, we only need to configure
3199*      collision distance in the Transmit Control Register.
3200*   2) Set up flow control on the MAC to that established with
3201*      the link partner.
3202*   3) Config DSP to improve Gigabit link quality for some PHY revisions.
3203*
3204* hw - Struct containing variables accessed by shared code
3205******************************************************************************/
3206static int32_t
3207e1000_copper_link_postconfig(struct e1000_hw *hw)
3208{
3209        int32_t ret_val;
3210        DEBUGFUNC();
3211
3212        if (hw->mac_type >= e1000_82544) {
3213                e1000_config_collision_dist(hw);
3214        } else {
3215                ret_val = e1000_config_mac_to_phy(hw);
3216                if (ret_val) {
3217                        DEBUGOUT("Error configuring MAC to PHY settings\n");
3218                        return ret_val;
3219                }
3220        }
3221        ret_val = e1000_config_fc_after_link_up(hw);
3222        if (ret_val) {
3223                DEBUGOUT("Error Configuring Flow Control\n");
3224                return ret_val;
3225        }
3226        return E1000_SUCCESS;
3227}
3228
3229/******************************************************************************
3230* Detects which PHY is present and setup the speed and duplex
3231*
3232* hw - Struct containing variables accessed by shared code
3233******************************************************************************/
3234static int
3235e1000_setup_copper_link(struct e1000_hw *hw)
3236{
3237        int32_t ret_val;
3238        uint16_t i;
3239        uint16_t phy_data;
3240        uint16_t reg_data;
3241
3242        DEBUGFUNC();
3243
3244        switch (hw->mac_type) {
3245        case e1000_80003es2lan:
3246        case e1000_ich8lan:
3247                /* Set the mac to wait the maximum time between each
3248                 * iteration and increase the max iterations when
3249                 * polling the phy; this fixes erroneous timeouts at 10Mbps. */
3250                ret_val = e1000_write_kmrn_reg(hw,
3251                                GG82563_REG(0x34, 4), 0xFFFF);
3252                if (ret_val)
3253                        return ret_val;
3254                ret_val = e1000_read_kmrn_reg(hw,
3255                                GG82563_REG(0x34, 9), &reg_data);
3256                if (ret_val)
3257                        return ret_val;
3258                reg_data |= 0x3F;
3259                ret_val = e1000_write_kmrn_reg(hw,
3260                                GG82563_REG(0x34, 9), reg_data);
3261                if (ret_val)
3262                        return ret_val;
3263        default:
3264                break;
3265        }
3266
3267        /* Check if it is a valid PHY and set PHY mode if necessary. */
3268        ret_val = e1000_copper_link_preconfig(hw);
3269        if (ret_val)
3270                return ret_val;
3271        switch (hw->mac_type) {
3272        case e1000_80003es2lan:
3273                /* Kumeran registers are written-only */
3274                reg_data =
3275                E1000_KUMCTRLSTA_INB_CTRL_LINK_STATUS_TX_TIMEOUT_DEFAULT;
3276                reg_data |= E1000_KUMCTRLSTA_INB_CTRL_DIS_PADDING;
3277                ret_val = e1000_write_kmrn_reg(hw,
3278                                E1000_KUMCTRLSTA_OFFSET_INB_CTRL, reg_data);
3279                if (ret_val)
3280                        return ret_val;
3281                break;
3282        default:
3283                break;
3284        }
3285
3286        if (hw->phy_type == e1000_phy_igp ||
3287                hw->phy_type == e1000_phy_igp_3 ||
3288                hw->phy_type == e1000_phy_igp_2) {
3289                ret_val = e1000_copper_link_igp_setup(hw);
3290                if (ret_val)
3291                        return ret_val;
3292        } else if (hw->phy_type == e1000_phy_m88 ||
3293                hw->phy_type == e1000_phy_igb) {
3294                ret_val = e1000_copper_link_mgp_setup(hw);
3295                if (ret_val)
3296                        return ret_val;
3297        } else if (hw->phy_type == e1000_phy_gg82563) {
3298                ret_val = e1000_copper_link_ggp_setup(hw);
3299                if (ret_val)
3300                        return ret_val;
3301        }
3302
3303        /* always auto */
3304        /* Setup autoneg and flow control advertisement
3305          * and perform autonegotiation */
3306        ret_val = e1000_copper_link_autoneg(hw);
3307        if (ret_val)
3308                return ret_val;
3309
3310        /* Check link status. Wait up to 100 microseconds for link to become
3311         * valid.
3312         */
3313        for (i = 0; i < 10; i++) {
3314                ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3315                if (ret_val)
3316                        return ret_val;
3317                ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3318                if (ret_val)
3319                        return ret_val;
3320
3321                if (phy_data & MII_SR_LINK_STATUS) {
3322                        /* Config the MAC and PHY after link is up */
3323                        ret_val = e1000_copper_link_postconfig(hw);
3324                        if (ret_val)
3325                                return ret_val;
3326
3327                        DEBUGOUT("Valid link established!!!\n");
3328                        return E1000_SUCCESS;
3329                }
3330                udelay(10);
3331        }
3332
3333        DEBUGOUT("Unable to establish link!!!\n");
3334        return E1000_SUCCESS;
3335}
3336
3337/******************************************************************************
3338* Configures PHY autoneg and flow control advertisement settings
3339*
3340* hw - Struct containing variables accessed by shared code
3341******************************************************************************/
3342int32_t
3343e1000_phy_setup_autoneg(struct e1000_hw *hw)
3344{
3345        int32_t ret_val;
3346        uint16_t mii_autoneg_adv_reg;
3347        uint16_t mii_1000t_ctrl_reg;
3348
3349        DEBUGFUNC();
3350
3351        /* Read the MII Auto-Neg Advertisement Register (Address 4). */
3352        ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
3353        if (ret_val)
3354                return ret_val;
3355
3356        if (hw->phy_type != e1000_phy_ife) {
3357                /* Read the MII 1000Base-T Control Register (Address 9). */
3358                ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
3359                                &mii_1000t_ctrl_reg);
3360                if (ret_val)
3361                        return ret_val;
3362        } else
3363                mii_1000t_ctrl_reg = 0;
3364
3365        /* Need to parse both autoneg_advertised and fc and set up
3366         * the appropriate PHY registers.  First we will parse for
3367         * autoneg_advertised software override.  Since we can advertise
3368         * a plethora of combinations, we need to check each bit
3369         * individually.
3370         */
3371
3372        /* First we clear all the 10/100 mb speed bits in the Auto-Neg
3373         * Advertisement Register (Address 4) and the 1000 mb speed bits in
3374         * the  1000Base-T Control Register (Address 9).
3375         */
3376        mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
3377        mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
3378
3379        DEBUGOUT("autoneg_advertised %x\n", hw->autoneg_advertised);
3380
3381        /* Do we want to advertise 10 Mb Half Duplex? */
3382        if (hw->autoneg_advertised & ADVERTISE_10_HALF) {
3383                DEBUGOUT("Advertise 10mb Half duplex\n");
3384                mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
3385        }
3386
3387        /* Do we want to advertise 10 Mb Full Duplex? */
3388        if (hw->autoneg_advertised & ADVERTISE_10_FULL) {
3389                DEBUGOUT("Advertise 10mb Full duplex\n");
3390                mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
3391        }
3392
3393        /* Do we want to advertise 100 Mb Half Duplex? */
3394        if (hw->autoneg_advertised & ADVERTISE_100_HALF) {
3395                DEBUGOUT("Advertise 100mb Half duplex\n");
3396                mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
3397        }
3398
3399        /* Do we want to advertise 100 Mb Full Duplex? */
3400        if (hw->autoneg_advertised & ADVERTISE_100_FULL) {
3401                DEBUGOUT("Advertise 100mb Full duplex\n");
3402                mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
3403        }
3404
3405        /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
3406        if (hw->autoneg_advertised & ADVERTISE_1000_HALF) {
3407                DEBUGOUT
3408                    ("Advertise 1000mb Half duplex requested, request denied!\n");
3409        }
3410
3411        /* Do we want to advertise 1000 Mb Full Duplex? */
3412        if (hw->autoneg_advertised & ADVERTISE_1000_FULL) {
3413                DEBUGOUT("Advertise 1000mb Full duplex\n");
3414                mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
3415        }
3416
3417        /* Check for a software override of the flow control settings, and
3418         * setup the PHY advertisement registers accordingly.  If
3419         * auto-negotiation is enabled, then software will have to set the
3420         * "PAUSE" bits to the correct value in the Auto-Negotiation
3421         * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
3422         *
3423         * The possible values of the "fc" parameter are:
3424         *      0:  Flow control is completely disabled
3425         *      1:  Rx flow control is enabled (we can receive pause frames
3426         *          but not send pause frames).
3427         *      2:  Tx flow control is enabled (we can send pause frames
3428         *          but we do not support receiving pause frames).
3429         *      3:  Both Rx and TX flow control (symmetric) are enabled.
3430         *  other:  No software override.  The flow control configuration
3431         *          in the EEPROM is used.
3432         */
3433        switch (hw->fc) {
3434        case e1000_fc_none:     /* 0 */
3435                /* Flow control (RX & TX) is completely disabled by a
3436                 * software over-ride.
3437                 */
3438                mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3439                break;
3440        case e1000_fc_rx_pause: /* 1 */
3441                /* RX Flow control is enabled, and TX Flow control is
3442                 * disabled, by a software over-ride.
3443                 */
3444                /* Since there really isn't a way to advertise that we are
3445                 * capable of RX Pause ONLY, we will advertise that we
3446                 * support both symmetric and asymmetric RX PAUSE.  Later
3447                 * (in e1000_config_fc_after_link_up) we will disable the
3448                 *hw's ability to send PAUSE frames.
3449                 */
3450                mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3451                break;
3452        case e1000_fc_tx_pause: /* 2 */
3453                /* TX Flow control is enabled, and RX Flow control is
3454                 * disabled, by a software over-ride.
3455                 */
3456                mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
3457                mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
3458                break;
3459        case e1000_fc_full:     /* 3 */
3460                /* Flow control (both RX and TX) is enabled by a software
3461                 * over-ride.
3462                 */
3463                mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3464                break;
3465        default:
3466                DEBUGOUT("Flow control param set incorrectly\n");
3467                return -E1000_ERR_CONFIG;
3468        }
3469
3470        ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
3471        if (ret_val)
3472                return ret_val;
3473
3474        DEBUGOUT("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
3475
3476        if (hw->phy_type != e1000_phy_ife) {
3477                ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
3478                                mii_1000t_ctrl_reg);
3479                if (ret_val)
3480                        return ret_val;
3481        }
3482
3483        return E1000_SUCCESS;
3484}
3485
3486/******************************************************************************
3487* Sets the collision distance in the Transmit Control register
3488*
3489* hw - Struct containing variables accessed by shared code
3490*
3491* Link should have been established previously. Reads the speed and duplex
3492* information from the Device Status register.
3493******************************************************************************/
3494static void
3495e1000_config_collision_dist(struct e1000_hw *hw)
3496{
3497        uint32_t tctl, coll_dist;
3498
3499        DEBUGFUNC();
3500
3501        if (hw->mac_type < e1000_82543)
3502                coll_dist = E1000_COLLISION_DISTANCE_82542;
3503        else
3504                coll_dist = E1000_COLLISION_DISTANCE;
3505
3506        tctl = E1000_READ_REG(hw, TCTL);
3507
3508        tctl &= ~E1000_TCTL_COLD;
3509        tctl |= coll_dist << E1000_COLD_SHIFT;
3510
3511        E1000_WRITE_REG(hw, TCTL, tctl);
3512        E1000_WRITE_FLUSH(hw);
3513}
3514
3515/******************************************************************************
3516* Sets MAC speed and duplex settings to reflect the those in the PHY
3517*
3518* hw - Struct containing variables accessed by shared code
3519* mii_reg - data to write to the MII control register
3520*
3521* The contents of the PHY register containing the needed information need to
3522* be passed in.
3523******************************************************************************/
3524static int
3525e1000_config_mac_to_phy(struct e1000_hw *hw)
3526{
3527        uint32_t ctrl;
3528        uint16_t phy_data;
3529
3530        DEBUGFUNC();
3531
3532        /* Read the Device Control Register and set the bits to Force Speed
3533         * and Duplex.
3534         */
3535        ctrl = E1000_READ_REG(hw, CTRL);
3536        ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
3537        ctrl &= ~(E1000_CTRL_ILOS);
3538        ctrl |= (E1000_CTRL_SPD_SEL);
3539
3540        /* Set up duplex in the Device Control and Transmit Control
3541         * registers depending on negotiated values.
3542         */
3543        if (e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data) < 0) {
3544                DEBUGOUT("PHY Read Error\n");
3545                return -E1000_ERR_PHY;
3546        }
3547        if (phy_data & M88E1000_PSSR_DPLX)
3548                ctrl |= E1000_CTRL_FD;
3549        else
3550                ctrl &= ~E1000_CTRL_FD;
3551
3552        e1000_config_collision_dist(hw);
3553
3554        /* Set up speed in the Device Control register depending on
3555         * negotiated values.
3556         */
3557        if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
3558                ctrl |= E1000_CTRL_SPD_1000;
3559        else if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
3560                ctrl |= E1000_CTRL_SPD_100;
3561        /* Write the configured values back to the Device Control Reg. */
3562        E1000_WRITE_REG(hw, CTRL, ctrl);
3563        return 0;
3564}
3565
3566/******************************************************************************
3567 * Forces the MAC's flow control settings.
3568 *
3569 * hw - Struct containing variables accessed by shared code
3570 *
3571 * Sets the TFCE and RFCE bits in the device control register to reflect
3572 * the adapter settings. TFCE and RFCE need to be explicitly set by
3573 * software when a Copper PHY is used because autonegotiation is managed
3574 * by the PHY rather than the MAC. Software must also configure these
3575 * bits when link is forced on a fiber connection.
3576 *****************************************************************************/
3577static int
3578e1000_force_mac_fc(struct e1000_hw *hw)
3579{
3580        uint32_t ctrl;
3581
3582        DEBUGFUNC();
3583
3584        /* Get the current configuration of the Device Control Register */
3585        ctrl = E1000_READ_REG(hw, CTRL);
3586
3587        /* Because we didn't get link via the internal auto-negotiation
3588         * mechanism (we either forced link or we got link via PHY
3589         * auto-neg), we have to manually enable/disable transmit an
3590         * receive flow control.
3591         *
3592         * The "Case" statement below enables/disable flow control
3593         * according to the "hw->fc" parameter.
3594         *
3595         * The possible values of the "fc" parameter are:
3596         *      0:  Flow control is completely disabled
3597         *      1:  Rx flow control is enabled (we can receive pause
3598         *          frames but not send pause frames).
3599         *      2:  Tx flow control is enabled (we can send pause frames
3600         *          frames but we do not receive pause frames).
3601         *      3:  Both Rx and TX flow control (symmetric) is enabled.
3602         *  other:  No other values should be possible at this point.
3603         */
3604
3605        switch (hw->fc) {
3606        case e1000_fc_none:
3607                ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
3608                break;
3609        case e1000_fc_rx_pause:
3610                ctrl &= (~E1000_CTRL_TFCE);
3611                ctrl |= E1000_CTRL_RFCE;
3612                break;
3613        case e1000_fc_tx_pause:
3614                ctrl &= (~E1000_CTRL_RFCE);
3615                ctrl |= E1000_CTRL_TFCE;
3616                break;
3617        case e1000_fc_full:
3618                ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
3619                break;
3620        default:
3621                DEBUGOUT("Flow control param set incorrectly\n");
3622                return -E1000_ERR_CONFIG;
3623        }
3624
3625        /* Disable TX Flow Control for 82542 (rev 2.0) */
3626        if (hw->mac_type == e1000_82542_rev2_0)
3627                ctrl &= (~E1000_CTRL_TFCE);
3628
3629        E1000_WRITE_REG(hw, CTRL, ctrl);
3630        return 0;
3631}
3632
3633/******************************************************************************
3634 * Configures flow control settings after link is established
3635 *
3636 * hw - Struct containing variables accessed by shared code
3637 *
3638 * Should be called immediately after a valid link has been established.
3639 * Forces MAC flow control settings if link was forced. When in MII/GMII mode
3640 * and autonegotiation is enabled, the MAC flow control settings will be set
3641 * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
3642 * and RFCE bits will be automaticaly set to the negotiated flow control mode.
3643 *****************************************************************************/
3644static int32_t
3645e1000_config_fc_after_link_up(struct e1000_hw *hw)
3646{
3647        int32_t ret_val;
3648        uint16_t mii_status_reg;
3649        uint16_t mii_nway_adv_reg;
3650        uint16_t mii_nway_lp_ability_reg;
3651        uint16_t speed;
3652        uint16_t duplex;
3653
3654        DEBUGFUNC();
3655
3656        /* Check for the case where we have fiber media and auto-neg failed
3657         * so we had to force link.  In this case, we need to force the
3658         * configuration of the MAC to match the "fc" parameter.
3659         */
3660        if (((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed))
3661                || ((hw->media_type == e1000_media_type_internal_serdes)
3662                && (hw->autoneg_failed))
3663                || ((hw->media_type == e1000_media_type_copper)
3664                && (!hw->autoneg))) {
3665                ret_val = e1000_force_mac_fc(hw);
3666                if (ret_val < 0) {
3667                        DEBUGOUT("Error forcing flow control settings\n");
3668                        return ret_val;
3669                }
3670        }
3671
3672        /* Check for the case where we have copper media and auto-neg is
3673         * enabled.  In this case, we need to check and see if Auto-Neg
3674         * has completed, and if so, how the PHY and link partner has
3675         * flow control configured.
3676         */
3677        if (hw->media_type == e1000_media_type_copper) {
3678                /* Read the MII Status Register and check to see if AutoNeg
3679                 * has completed.  We read this twice because this reg has
3680                 * some "sticky" (latched) bits.
3681                 */
3682                if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
3683                        DEBUGOUT("PHY Read Error\n");
3684                        return -E1000_ERR_PHY;
3685                }
3686                if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
3687                        DEBUGOUT("PHY Read Error\n");
3688                        return -E1000_ERR_PHY;
3689                }
3690
3691                if (mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
3692                        /* The AutoNeg process has completed, so we now need to
3693                         * read both the Auto Negotiation Advertisement Register
3694                         * (Address 4) and the Auto_Negotiation Base Page Ability
3695                         * Register (Address 5) to determine how flow control was
3696                         * negotiated.
3697                         */
3698                        if (e1000_read_phy_reg
3699                            (hw, PHY_AUTONEG_ADV, &mii_nway_adv_reg) < 0) {
3700                                DEBUGOUT("PHY Read Error\n");
3701                                return -E1000_ERR_PHY;
3702                        }
3703                        if (e1000_read_phy_reg
3704                            (hw, PHY_LP_ABILITY,
3705                             &mii_nway_lp_ability_reg) < 0) {
3706                                DEBUGOUT("PHY Read Error\n");
3707                                return -E1000_ERR_PHY;
3708                        }
3709
3710                        /* Two bits in the Auto Negotiation Advertisement Register
3711                         * (Address 4) and two bits in the Auto Negotiation Base
3712                         * Page Ability Register (Address 5) determine flow control
3713                         * for both the PHY and the link partner.  The following
3714                         * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
3715                         * 1999, describes these PAUSE resolution bits and how flow
3716                         * control is determined based upon these settings.
3717                         * NOTE:  DC = Don't Care
3718                         *
3719                         *   LOCAL DEVICE  |   LINK PARTNER
3720                         * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
3721                         *-------|---------|-------|---------|--------------------
3722                         *   0   |    0    |  DC   |   DC    | e1000_fc_none
3723                         *   0   |    1    |   0   |   DC    | e1000_fc_none
3724                         *   0   |    1    |   1   |    0    | e1000_fc_none
3725                         *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
3726                         *   1   |    0    |   0   |   DC    | e1000_fc_none
3727                         *   1   |   DC    |   1   |   DC    | e1000_fc_full
3728                         *   1   |    1    |   0   |    0    | e1000_fc_none
3729                         *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
3730                         *
3731                         */
3732                        /* Are both PAUSE bits set to 1?  If so, this implies
3733                         * Symmetric Flow Control is enabled at both ends.  The
3734                         * ASM_DIR bits are irrelevant per the spec.
3735                         *
3736                         * For Symmetric Flow Control:
3737                         *
3738                         *   LOCAL DEVICE  |   LINK PARTNER
3739                         * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3740                         *-------|---------|-------|---------|--------------------
3741                         *   1   |   DC    |   1   |   DC    | e1000_fc_full
3742                         *
3743                         */
3744                        if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3745                            (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
3746                                /* Now we need to check if the user selected RX ONLY
3747                                 * of pause frames.  In this case, we had to advertise
3748                                 * FULL flow control because we could not advertise RX
3749                                 * ONLY. Hence, we must now check to see if we need to
3750                                 * turn OFF  the TRANSMISSION of PAUSE frames.
3751                                 */
3752                                if (hw->original_fc == e1000_fc_full) {
3753                                        hw->fc = e1000_fc_full;
3754                                        DEBUGOUT("Flow Control = FULL.\r\n");
3755                                } else {
3756                                        hw->fc = e1000_fc_rx_pause;
3757                                        DEBUGOUT
3758                                            ("Flow Control = RX PAUSE frames only.\r\n");
3759                                }
3760                        }
3761                        /* For receiving PAUSE frames ONLY.
3762                         *
3763                         *   LOCAL DEVICE  |   LINK PARTNER
3764                         * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3765                         *-------|---------|-------|---------|--------------------
3766                         *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
3767                         *
3768                         */
3769                        else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3770                                 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3771                                 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3772                                 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3773                        {
3774                                hw->fc = e1000_fc_tx_pause;
3775                                DEBUGOUT
3776                                    ("Flow Control = TX PAUSE frames only.\r\n");
3777                        }
3778                        /* For transmitting PAUSE frames ONLY.
3779                         *
3780                         *   LOCAL DEVICE  |   LINK PARTNER
3781                         * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3782                         *-------|---------|-------|---------|--------------------
3783                         *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
3784                         *
3785                         */
3786                        else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3787                                 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3788                                 !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3789                                 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3790                        {
3791                                hw->fc = e1000_fc_rx_pause;
3792                                DEBUGOUT
3793                                    ("Flow Control = RX PAUSE frames only.\r\n");
3794                        }
3795                        /* Per the IEEE spec, at this point flow control should be
3796                         * disabled.  However, we want to consider that we could
3797                         * be connected to a legacy switch that doesn't advertise
3798                         * desired flow control, but can be forced on the link
3799                         * partner.  So if we advertised no flow control, that is
3800                         * what we will resolve to.  If we advertised some kind of
3801                         * receive capability (Rx Pause Only or Full Flow Control)
3802                         * and the link partner advertised none, we will configure
3803                         * ourselves to enable Rx Flow Control only.  We can do
3804                         * this safely for two reasons:  If the link partner really
3805                         * didn't want flow control enabled, and we enable Rx, no
3806                         * harm done since we won't be receiving any PAUSE frames
3807                         * anyway.  If the intent on the link partner was to have
3808                         * flow control enabled, then by us enabling RX only, we
3809                         * can at least receive pause frames and process them.
3810                         * This is a good idea because in most cases, since we are
3811                         * predominantly a server NIC, more times than not we will
3812                         * be asked to delay transmission of packets than asking
3813                         * our link partner to pause transmission of frames.
3814                         */
3815                        else if (hw->original_fc == e1000_fc_none ||
3816                                 hw->original_fc == e1000_fc_tx_pause) {
3817                                hw->fc = e1000_fc_none;
3818                                DEBUGOUT("Flow Control = NONE.\r\n");
3819                        } else {
3820                                hw->fc = e1000_fc_rx_pause;
3821                                DEBUGOUT
3822                                    ("Flow Control = RX PAUSE frames only.\r\n");
3823                        }
3824
3825                        /* Now we need to do one last check...  If we auto-
3826                         * negotiated to HALF DUPLEX, flow control should not be
3827                         * enabled per IEEE 802.3 spec.
3828                         */
3829                        e1000_get_speed_and_duplex(hw, &speed, &duplex);
3830
3831                        if (duplex == HALF_DUPLEX)
3832                                hw->fc = e1000_fc_none;
3833
3834                        /* Now we call a subroutine to actually force the MAC
3835                         * controller to use the correct flow control settings.
3836                         */
3837                        ret_val = e1000_force_mac_fc(hw);
3838                        if (ret_val < 0) {
3839                                DEBUGOUT
3840                                    ("Error forcing flow control settings\n");
3841                                return ret_val;
3842                        }
3843                } else {
3844                        DEBUGOUT
3845                            ("Copper PHY and Auto Neg has not completed.\r\n");
3846                }
3847        }
3848        return E1000_SUCCESS;
3849}
3850
3851/******************************************************************************
3852 * Checks to see if the link status of the hardware has changed.
3853 *
3854 * hw - Struct containing variables accessed by shared code
3855 *
3856 * Called by any function that needs to check the link status of the adapter.
3857 *****************************************************************************/
3858static int
3859e1000_check_for_link(struct e1000_hw *hw)
3860{
3861        uint32_t rxcw;
3862        uint32_t ctrl;
3863        uint32_t status;
3864        uint32_t rctl;
3865        uint32_t signal;
3866        int32_t ret_val;
3867        uint16_t phy_data;
3868        uint16_t lp_capability;
3869
3870        DEBUGFUNC();
3871
3872        /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
3873         * set when the optics detect a signal. On older adapters, it will be
3874         * cleared when there is a signal
3875         */
3876        ctrl = E1000_READ_REG(hw, CTRL);
3877        if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
3878                signal = E1000_CTRL_SWDPIN1;
3879        else
3880                signal = 0;
3881
3882        status = E1000_READ_REG(hw, STATUS);
3883        rxcw = E1000_READ_REG(hw, RXCW);
3884        DEBUGOUT("ctrl: %#08x status %#08x rxcw %#08x\n", ctrl, status, rxcw);
3885
3886        /* If we have a copper PHY then we only want to go out to the PHY
3887         * registers to see if Auto-Neg has completed and/or if our link
3888         * status has changed.  The get_link_status flag will be set if we
3889         * receive a Link Status Change interrupt or we have Rx Sequence
3890         * Errors.
3891         */
3892        if ((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
3893                /* First we want to see if the MII Status Register reports
3894                 * link.  If so, then we want to get the current speed/duplex
3895                 * of the PHY.
3896                 * Read the register twice since the link bit is sticky.
3897                 */
3898                if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3899                        DEBUGOUT("PHY Read Error\n");
3900                        return -E1000_ERR_PHY;
3901                }
3902                if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3903                        DEBUGOUT("PHY Read Error\n");
3904                        return -E1000_ERR_PHY;
3905                }
3906
3907                if (phy_data & MII_SR_LINK_STATUS) {
3908                        hw->get_link_status = false;
3909                } else {
3910                        /* No link detected */
3911                        return -E1000_ERR_NOLINK;
3912                }
3913
3914                /* We have a M88E1000 PHY and Auto-Neg is enabled.  If we
3915                 * have Si on board that is 82544 or newer, Auto
3916                 * Speed Detection takes care of MAC speed/duplex
3917                 * configuration.  So we only need to configure Collision
3918                 * Distance in the MAC.  Otherwise, we need to force
3919                 * speed/duplex on the MAC to the current PHY speed/duplex
3920                 * settings.
3921                 */
3922                if (hw->mac_type >= e1000_82544)
3923                        e1000_config_collision_dist(hw);
3924                else {
3925                        ret_val = e1000_config_mac_to_phy(hw);
3926                        if (ret_val < 0) {
3927                                DEBUGOUT
3928                                    ("Error configuring MAC to PHY settings\n");
3929                                return ret_val;
3930                        }
3931                }
3932
3933                /* Configure Flow Control now that Auto-Neg has completed. First, we
3934                 * need to restore the desired flow control settings because we may
3935                 * have had to re-autoneg with a different link partner.
3936                 */
3937                ret_val = e1000_config_fc_after_link_up(hw);
3938                if (ret_val < 0) {
3939                        DEBUGOUT("Error configuring flow control\n");
3940                        return ret_val;
3941                }
3942
3943                /* At this point we know that we are on copper and we have
3944                 * auto-negotiated link.  These are conditions for checking the link
3945                 * parter capability register.  We use the link partner capability to
3946                 * determine if TBI Compatibility needs to be turned on or off.  If
3947                 * the link partner advertises any speed in addition to Gigabit, then
3948                 * we assume that they are GMII-based, and TBI compatibility is not
3949                 * needed. If no other speeds are advertised, we assume the link
3950                 * partner is TBI-based, and we turn on TBI Compatibility.
3951                 */
3952                if (hw->tbi_compatibility_en) {
3953                        if (e1000_read_phy_reg
3954                            (hw, PHY_LP_ABILITY, &lp_capability) < 0) {
3955                                DEBUGOUT("PHY Read Error\n");
3956                                return -E1000_ERR_PHY;
3957                        }
3958                        if (lp_capability & (NWAY_LPAR_10T_HD_CAPS |
3959                                             NWAY_LPAR_10T_FD_CAPS |
3960                                             NWAY_LPAR_100TX_HD_CAPS |
3961                                             NWAY_LPAR_100TX_FD_CAPS |
3962                                             NWAY_LPAR_100T4_CAPS)) {
3963                                /* If our link partner advertises anything in addition to
3964                                 * gigabit, we do not need to enable TBI compatibility.
3965                                 */
3966                                if (hw->tbi_compatibility_on) {
3967                                        /* If we previously were in the mode, turn it off. */
3968                                        rctl = E1000_READ_REG(hw, RCTL);
3969                                        rctl &= ~E1000_RCTL_SBP;
3970                                        E1000_WRITE_REG(hw, RCTL, rctl);
3971                                        hw->tbi_compatibility_on = false;
3972                                }
3973                        } else {
3974                                /* If TBI compatibility is was previously off, turn it on. For
3975                                 * compatibility with a TBI link partner, we will store bad
3976                                 * packets. Some frames have an additional byte on the end and
3977                                 * will look like CRC errors to to the hardware.
3978                                 */
3979                                if (!hw->tbi_compatibility_on) {
3980                                        hw->tbi_compatibility_on = true;
3981                                        rctl = E1000_READ_REG(hw, RCTL);
3982                                        rctl |= E1000_RCTL_SBP;
3983                                        E1000_WRITE_REG(hw, RCTL, rctl);
3984                                }
3985                        }
3986                }
3987        }
3988        /* If we don't have link (auto-negotiation failed or link partner cannot
3989         * auto-negotiate), the cable is plugged in (we have signal), and our
3990         * link partner is not trying to auto-negotiate with us (we are receiving
3991         * idles or data), we need to force link up. We also need to give
3992         * auto-negotiation time to complete, in case the cable was just plugged
3993         * in. The autoneg_failed flag does this.
3994         */
3995        else if ((hw->media_type == e1000_media_type_fiber) &&
3996                 (!(status & E1000_STATUS_LU)) &&
3997                 ((ctrl & E1000_CTRL_SWDPIN1) == signal) &&
3998                 (!(rxcw & E1000_RXCW_C))) {
3999                if (hw->autoneg_failed == 0) {
4000                        hw->autoneg_failed = 1;
4001                        return 0;
4002                }
4003                DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\r\n");
4004
4005                /* Disable auto-negotiation in the TXCW register */
4006                E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
4007
4008                /* Force link-up and also force full-duplex. */
4009                ctrl = E1000_READ_REG(hw, CTRL);
4010                ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
4011                E1000_WRITE_REG(hw, CTRL, ctrl);
4012
4013                /* Configure Flow Control after forcing link up. */
4014                ret_val = e1000_config_fc_after_link_up(hw);
4015                if (ret_val < 0) {
4016                        DEBUGOUT("Error configuring flow control\n");
4017                        return ret_val;
4018                }
4019        }
4020        /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
4021         * auto-negotiation in the TXCW register and disable forced link in the
4022         * Device Control register in an attempt to auto-negotiate with our link
4023         * partner.
4024         */
4025        else if ((hw->media_type == e1000_media_type_fiber) &&
4026                 (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
4027                DEBUGOUT
4028                    ("RXing /C/, enable AutoNeg and stop forcing link.\r\n");
4029                E1000_WRITE_REG(hw, TXCW, hw->txcw);
4030                E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
4031        }
4032        return 0;
4033}
4034
4035/******************************************************************************
4036* Configure the MAC-to-PHY interface for 10/100Mbps
4037*
4038* hw - Struct containing variables accessed by shared code
4039******************************************************************************/
4040static int32_t
4041e1000_configure_kmrn_for_10_100(struct e1000_hw *hw, uint16_t duplex)
4042{
4043        int32_t ret_val = E1000_SUCCESS;
4044        uint32_t tipg;
4045        uint16_t reg_data;
4046
4047        DEBUGFUNC();
4048
4049        reg_data = E1000_KUMCTRLSTA_HD_CTRL_10_100_DEFAULT;
4050        ret_val = e1000_write_kmrn_reg(hw,
4051                        E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4052        if (ret_val)
4053                return ret_val;
4054
4055        /* Configure Transmit Inter-Packet Gap */
4056        tipg = E1000_READ_REG(hw, TIPG);
4057        tipg &= ~E1000_TIPG_IPGT_MASK;
4058        tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_10_100;
4059        E1000_WRITE_REG(hw, TIPG, tipg);
4060
4061        ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4062
4063        if (ret_val)
4064                return ret_val;
4065
4066        if (duplex == HALF_DUPLEX)
4067                reg_data |= GG82563_KMCR_PASS_FALSE_CARRIER;
4068        else
4069                reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4070
4071        ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4072
4073        return ret_val;
4074}
4075
4076static int32_t
4077e1000_configure_kmrn_for_1000(struct e1000_hw *hw)
4078{
4079        int32_t ret_val = E1000_SUCCESS;
4080        uint16_t reg_data;
4081        uint32_t tipg;
4082
4083        DEBUGFUNC();
4084
4085        reg_data = E1000_KUMCTRLSTA_HD_CTRL_1000_DEFAULT;
4086        ret_val = e1000_write_kmrn_reg(hw,
4087                        E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4088        if (ret_val)
4089                return ret_val;
4090
4091        /* Configure Transmit Inter-Packet Gap */
4092        tipg = E1000_READ_REG(hw, TIPG);
4093        tipg &= ~E1000_TIPG_IPGT_MASK;
4094        tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
4095        E1000_WRITE_REG(hw, TIPG, tipg);
4096
4097        ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4098
4099        if (ret_val)
4100                return ret_val;
4101
4102        reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4103        ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4104
4105        return ret_val;
4106}
4107
4108/******************************************************************************
4109 * Detects the current speed and duplex settings of the hardware.
4110 *
4111 * hw - Struct containing variables accessed by shared code
4112 * speed - Speed of the connection
4113 * duplex - Duplex setting of the connection
4114 *****************************************************************************/
4115static int
4116e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t *speed,
4117                uint16_t *duplex)
4118{
4119        uint32_t status;
4120        int32_t ret_val;
4121        uint16_t phy_data;
4122
4123        DEBUGFUNC();
4124
4125        if (hw->mac_type >= e1000_82543) {
4126                status = E1000_READ_REG(hw, STATUS);
4127                if (status & E1000_STATUS_SPEED_1000) {
4128                        *speed = SPEED_1000;
4129                        DEBUGOUT("1000 Mbs, ");
4130                } else if (status & E1000_STATUS_SPEED_100) {
4131                        *speed = SPEED_100;
4132                        DEBUGOUT("100 Mbs, ");
4133                } else {
4134                        *speed = SPEED_10;
4135                        DEBUGOUT("10 Mbs, ");
4136                }
4137
4138                if (status & E1000_STATUS_FD) {
4139                        *duplex = FULL_DUPLEX;
4140                        DEBUGOUT("Full Duplex\r\n");
4141                } else {
4142                        *duplex = HALF_DUPLEX;
4143                        DEBUGOUT(" Half Duplex\r\n");
4144                }
4145        } else {
4146                DEBUGOUT("1000 Mbs, Full Duplex\r\n");
4147                *speed = SPEED_1000;
4148                *duplex = FULL_DUPLEX;
4149        }
4150
4151        /* IGP01 PHY may advertise full duplex operation after speed downgrade
4152         * even if it is operating at half duplex.  Here we set the duplex
4153         * settings to match the duplex in the link partner's capabilities.
4154         */
4155        if (hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
4156                ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
4157                if (ret_val)
4158                        return ret_val;
4159
4160                if (!(phy_data & NWAY_ER_LP_NWAY_CAPS))
4161                        *duplex = HALF_DUPLEX;
4162                else {
4163                        ret_val = e1000_read_phy_reg(hw,
4164                                        PHY_LP_ABILITY, &phy_data);
4165                        if (ret_val)
4166                                return ret_val;
4167                        if ((*speed == SPEED_100 &&
4168                                !(phy_data & NWAY_LPAR_100TX_FD_CAPS))
4169                                || (*speed == SPEED_10
4170                                && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
4171                                *duplex = HALF_DUPLEX;
4172                }
4173        }
4174
4175        if ((hw->mac_type == e1000_80003es2lan) &&
4176                (hw->media_type == e1000_media_type_copper)) {
4177                if (*speed == SPEED_1000)
4178                        ret_val = e1000_configure_kmrn_for_1000(hw);
4179                else
4180                        ret_val = e1000_configure_kmrn_for_10_100(hw, *duplex);
4181                if (ret_val)
4182                        return ret_val;
4183        }
4184        return E1000_SUCCESS;
4185}
4186
4187/******************************************************************************
4188* Blocks until autoneg completes or times out (~4.5 seconds)
4189*
4190* hw - Struct containing variables accessed by shared code
4191******************************************************************************/
4192static int
4193e1000_wait_autoneg(struct e1000_hw *hw)
4194{
4195        uint16_t i;
4196        uint16_t phy_data;
4197
4198        DEBUGFUNC();
4199        DEBUGOUT("Waiting for Auto-Neg to complete.\n");
4200
4201        /* We will wait for autoneg to complete or timeout to expire. */
4202        for (i = PHY_AUTO_NEG_TIME; i > 0; i--) {
4203                /* Read the MII Status Register and wait for Auto-Neg
4204                 * Complete bit to be set.
4205                 */
4206                if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4207                        DEBUGOUT("PHY Read Error\n");
4208                        return -E1000_ERR_PHY;
4209                }
4210                if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4211                        DEBUGOUT("PHY Read Error\n");
4212                        return -E1000_ERR_PHY;
4213                }
4214                if (phy_data & MII_SR_AUTONEG_COMPLETE) {
4215                        DEBUGOUT("Auto-Neg complete.\n");
4216                        return 0;
4217                }
4218                mdelay(100);
4219        }
4220        DEBUGOUT("Auto-Neg timedout.\n");
4221        return -E1000_ERR_TIMEOUT;
4222}
4223
4224/******************************************************************************
4225* Raises the Management Data Clock
4226*
4227* hw - Struct containing variables accessed by shared code
4228* ctrl - Device control register's current value
4229******************************************************************************/
4230static void
4231e1000_raise_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4232{
4233        /* Raise the clock input to the Management Data Clock (by setting the MDC
4234         * bit), and then delay 2 microseconds.
4235         */
4236        E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
4237        E1000_WRITE_FLUSH(hw);
4238        udelay(2);
4239}
4240
4241/******************************************************************************
4242* Lowers the Management Data Clock
4243*
4244* hw - Struct containing variables accessed by shared code
4245* ctrl - Device control register's current value
4246******************************************************************************/
4247static void
4248e1000_lower_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4249{
4250        /* Lower the clock input to the Management Data Clock (by clearing the MDC
4251         * bit), and then delay 2 microseconds.
4252         */
4253        E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
4254        E1000_WRITE_FLUSH(hw);
4255        udelay(2);
4256}
4257
4258/******************************************************************************
4259* Shifts data bits out to the PHY
4260*
4261* hw - Struct containing variables accessed by shared code
4262* data - Data to send out to the PHY
4263* count - Number of bits to shift out
4264*
4265* Bits are shifted out in MSB to LSB order.
4266******************************************************************************/
4267static void
4268e1000_shift_out_mdi_bits(struct e1000_hw *hw, uint32_t data, uint16_t count)
4269{
4270        uint32_t ctrl;
4271        uint32_t mask;
4272
4273        /* We need to shift "count" number of bits out to the PHY. So, the value
4274         * in the "data" parameter will be shifted out to the PHY one bit at a
4275         * time. In order to do this, "data" must be broken down into bits.
4276         */
4277        mask = 0x01;
4278        mask <<= (count - 1);
4279
4280        ctrl = E1000_READ_REG(hw, CTRL);
4281
4282        /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
4283        ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
4284
4285        while (mask) {
4286                /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
4287                 * then raising and lowering the Management Data Clock. A "0" is
4288                 * shifted out to the PHY by setting the MDIO bit to "0" and then
4289                 * raising and lowering the clock.
4290                 */
4291                if (data & mask)
4292                        ctrl |= E1000_CTRL_MDIO;
4293                else
4294                        ctrl &= ~E1000_CTRL_MDIO;
4295
4296                E1000_WRITE_REG(hw, CTRL, ctrl);
4297                E1000_WRITE_FLUSH(hw);
4298
4299                udelay(2);
4300
4301                e1000_raise_mdi_clk(hw, &ctrl);
4302                e1000_lower_mdi_clk(hw, &ctrl);
4303
4304                mask = mask >> 1;
4305        }
4306}
4307
4308/******************************************************************************
4309* Shifts data bits in from the PHY
4310*
4311* hw - Struct containing variables accessed by shared code
4312*
4313* Bits are shifted in in MSB to LSB order.
4314******************************************************************************/
4315static uint16_t
4316e1000_shift_in_mdi_bits(struct e1000_hw *hw)
4317{
4318        uint32_t ctrl;
4319        uint16_t data = 0;
4320        uint8_t i;
4321
4322        /* In order to read a register from the PHY, we need to shift in a total
4323         * of 18 bits from the PHY. The first two bit (turnaround) times are used
4324         * to avoid contention on the MDIO pin when a read operation is performed.
4325         * These two bits are ignored by us and thrown away. Bits are "shifted in"
4326         * by raising the input to the Management Data Clock (setting the MDC bit),
4327         * and then reading the value of the MDIO bit.
4328         */
4329        ctrl = E1000_READ_REG(hw, CTRL);
4330
4331        /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
4332        ctrl &= ~E1000_CTRL_MDIO_DIR;
4333        ctrl &= ~E1000_CTRL_MDIO;
4334
4335        E1000_WRITE_REG(hw, CTRL, ctrl);
4336        E1000_WRITE_FLUSH(hw);
4337
4338        /* Raise and Lower the clock before reading in the data. This accounts for
4339         * the turnaround bits. The first clock occurred when we clocked out the
4340         * last bit of the Register Address.
4341         */
4342        e1000_raise_mdi_clk(hw, &ctrl);
4343        e1000_lower_mdi_clk(hw, &ctrl);
4344
4345        for (data = 0, i = 0; i < 16; i++) {
4346                data = data << 1;
4347                e1000_raise_mdi_clk(hw, &ctrl);
4348                ctrl = E1000_READ_REG(hw, CTRL);
4349                /* Check to see if we shifted in a "1". */
4350                if (ctrl & E1000_CTRL_MDIO)
4351                        data |= 1;
4352                e1000_lower_mdi_clk(hw, &ctrl);
4353        }
4354
4355        e1000_raise_mdi_clk(hw, &ctrl);
4356        e1000_lower_mdi_clk(hw, &ctrl);
4357
4358        return data;
4359}
4360
4361/*****************************************************************************
4362* Reads the value from a PHY register
4363*
4364* hw - Struct containing variables accessed by shared code
4365* reg_addr - address of the PHY register to read
4366******************************************************************************/
4367static int
4368e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t * phy_data)
4369{
4370        uint32_t i;
4371        uint32_t mdic = 0;
4372        const uint32_t phy_addr = 1;
4373
4374        if (reg_addr > MAX_PHY_REG_ADDRESS) {
4375                DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4376                return -E1000_ERR_PARAM;
4377        }
4378
4379        if (hw->mac_type > e1000_82543) {
4380                /* Set up Op-code, Phy Address, and register address in the MDI
4381                 * Control register.  The MAC will take care of interfacing with the
4382                 * PHY to retrieve the desired data.
4383                 */
4384                mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
4385                        (phy_addr << E1000_MDIC_PHY_SHIFT) |
4386                        (E1000_MDIC_OP_READ));
4387
4388                E1000_WRITE_REG(hw, MDIC, mdic);
4389
4390                /* Poll the ready bit to see if the MDI read completed */
4391                for (i = 0; i < 64; i++) {
4392                        udelay(10);
4393                        mdic = E1000_READ_REG(hw, MDIC);
4394                        if (mdic & E1000_MDIC_READY)
4395                                break;
4396                }
4397                if (!(mdic & E1000_MDIC_READY)) {
4398                        DEBUGOUT("MDI Read did not complete\n");
4399                        return -E1000_ERR_PHY;
4400                }
4401                if (mdic & E1000_MDIC_ERROR) {
4402                        DEBUGOUT("MDI Error\n");
4403                        return -E1000_ERR_PHY;
4404                }
4405                *phy_data = (uint16_t) mdic;
4406        } else {
4407                /* We must first send a preamble through the MDIO pin to signal the
4408                 * beginning of an MII instruction.  This is done by sending 32
4409                 * consecutive "1" bits.
4410                 */
4411                e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4412
4413                /* Now combine the next few fields that are required for a read
4414                 * operation.  We use this method instead of calling the
4415                 * e1000_shift_out_mdi_bits routine five different times. The format of
4416                 * a MII read instruction consists of a shift out of 14 bits and is
4417                 * defined as follows:
4418                 *    <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
4419                 * followed by a shift in of 18 bits.  This first two bits shifted in
4420                 * are TurnAround bits used to avoid contention on the MDIO pin when a
4421                 * READ operation is performed.  These two bits are thrown away
4422                 * followed by a shift in of 16 bits which contains the desired data.
4423                 */
4424                mdic = ((reg_addr) | (phy_addr << 5) |
4425                        (PHY_OP_READ << 10) | (PHY_SOF << 12));
4426
4427                e1000_shift_out_mdi_bits(hw, mdic, 14);
4428
4429                /* Now that we've shifted out the read command to the MII, we need to
4430                 * "shift in" the 16-bit value (18 total bits) of the requested PHY
4431                 * register address.
4432                 */
4433                *phy_data = e1000_shift_in_mdi_bits(hw);
4434        }
4435        return 0;
4436}
4437
4438/******************************************************************************
4439* Writes a value to a PHY register
4440*
4441* hw - Struct containing variables accessed by shared code
4442* reg_addr - address of the PHY register to write
4443* data - data to write to the PHY
4444******************************************************************************/
4445static int
4446e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t phy_data)
4447{
4448        uint32_t i;
4449        uint32_t mdic = 0;
4450        const uint32_t phy_addr = 1;
4451
4452        if (reg_addr > MAX_PHY_REG_ADDRESS) {
4453                DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4454                return -E1000_ERR_PARAM;
4455        }
4456
4457        if (hw->mac_type > e1000_82543) {
4458                /* Set up Op-code, Phy Address, register address, and data intended
4459                 * for the PHY register in the MDI Control register.  The MAC will take
4460                 * care of interfacing with the PHY to send the desired data.
4461                 */
4462                mdic = (((uint32_t) phy_data) |
4463                        (reg_addr << E1000_MDIC_REG_SHIFT) |
4464                        (phy_addr << E1000_MDIC_PHY_SHIFT) |
4465                        (E1000_MDIC_OP_WRITE));
4466
4467                E1000_WRITE_REG(hw, MDIC, mdic);
4468
4469                /* Poll the ready bit to see if the MDI read completed */
4470                for (i = 0; i < 64; i++) {
4471                        udelay(10);
4472                        mdic = E1000_READ_REG(hw, MDIC);
4473                        if (mdic & E1000_MDIC_READY)
4474                                break;
4475                }
4476                if (!(mdic & E1000_MDIC_READY)) {
4477                        DEBUGOUT("MDI Write did not complete\n");
4478                        return -E1000_ERR_PHY;
4479                }
4480        } else {
4481                /* We'll need to use the SW defined pins to shift the write command
4482                 * out to the PHY. We first send a preamble to the PHY to signal the
4483                 * beginning of the MII instruction.  This is done by sending 32
4484                 * consecutive "1" bits.
4485                 */
4486                e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4487
4488                /* Now combine the remaining required fields that will indicate a
4489                 * write operation. We use this method instead of calling the
4490                 * e1000_shift_out_mdi_bits routine for each field in the command. The
4491                 * format of a MII write instruction is as follows:
4492                 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
4493                 */
4494                mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
4495                        (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
4496                mdic <<= 16;
4497                mdic |= (uint32_t) phy_data;
4498
4499                e1000_shift_out_mdi_bits(hw, mdic, 32);
4500        }
4501        return 0;
4502}
4503
4504/******************************************************************************
4505 * Checks if PHY reset is blocked due to SOL/IDER session, for example.
4506 * Returning E1000_BLK_PHY_RESET isn't necessarily an error.  But it's up to
4507 * the caller to figure out how to deal with it.
4508 *
4509 * hw - Struct containing variables accessed by shared code
4510 *
4511 * returns: - E1000_BLK_PHY_RESET
4512 *            E1000_SUCCESS
4513 *
4514 *****************************************************************************/
4515int32_t
4516e1000_check_phy_reset_block(struct e1000_hw *hw)
4517{
4518        uint32_t manc = 0;
4519        uint32_t fwsm = 0;
4520
4521        if (hw->mac_type == e1000_ich8lan) {
4522                fwsm = E1000_READ_REG(hw, FWSM);
4523                return (fwsm & E1000_FWSM_RSPCIPHY) ? E1000_SUCCESS
4524                                                : E1000_BLK_PHY_RESET;
4525        }
4526
4527        if (hw->mac_type > e1000_82547_rev_2)
4528                manc = E1000_READ_REG(hw, MANC);
4529        return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ?
4530                E1000_BLK_PHY_RESET : E1000_SUCCESS;
4531}
4532
4533/***************************************************************************
4534 * Checks if the PHY configuration is done
4535 *
4536 * hw: Struct containing variables accessed by shared code
4537 *
4538 * returns: - E1000_ERR_RESET if fail to reset MAC
4539 *            E1000_SUCCESS at any other case.
4540 *
4541 ***************************************************************************/
4542static int32_t
4543e1000_get_phy_cfg_done(struct e1000_hw *hw)
4544{
4545        int32_t timeout = PHY_CFG_TIMEOUT;
4546        uint32_t cfg_mask = E1000_EEPROM_CFG_DONE;
4547
4548        DEBUGFUNC();
4549
4550        switch (hw->mac_type) {
4551        default:
4552                mdelay(10);
4553                break;
4554
4555        case e1000_80003es2lan:
4556                /* Separate *_CFG_DONE_* bit for each port */
4557                if (e1000_is_second_port(hw))
4558                        cfg_mask = E1000_EEPROM_CFG_DONE_PORT_1;
4559                /* Fall Through */
4560
4561        case e1000_82571:
4562        case e1000_82572:
4563        case e1000_igb:
4564                while (timeout) {
4565                        if (hw->mac_type == e1000_igb) {
4566                                if (E1000_READ_REG(hw, I210_EEMNGCTL) & cfg_mask)
4567                                        break;
4568                        } else {
4569                                if (E1000_READ_REG(hw, EEMNGCTL) & cfg_mask)
4570                                        break;
4571                        }
4572                        mdelay(1);
4573                        timeout--;
4574                }
4575                if (!timeout) {
4576                        DEBUGOUT("MNG configuration cycle has not "
4577                                        "completed.\n");
4578                        return -E1000_ERR_RESET;
4579                }
4580                break;
4581        }
4582
4583        return E1000_SUCCESS;
4584}
4585
4586/******************************************************************************
4587* Returns the PHY to the power-on reset state
4588*
4589* hw - Struct containing variables accessed by shared code
4590******************************************************************************/
4591int32_t
4592e1000_phy_hw_reset(struct e1000_hw *hw)
4593{
4594        uint16_t swfw = E1000_SWFW_PHY0_SM;
4595        uint32_t ctrl, ctrl_ext;
4596        uint32_t led_ctrl;
4597        int32_t ret_val;
4598
4599        DEBUGFUNC();
4600
4601        /* In the case of the phy reset being blocked, it's not an error, we
4602         * simply return success without performing the reset. */
4603        ret_val = e1000_check_phy_reset_block(hw);
4604        if (ret_val)
4605                return E1000_SUCCESS;
4606
4607        DEBUGOUT("Resetting Phy...\n");
4608
4609        if (hw->mac_type > e1000_82543) {
4610                if (e1000_is_second_port(hw))
4611                        swfw = E1000_SWFW_PHY1_SM;
4612
4613                if (e1000_swfw_sync_acquire(hw, swfw)) {
4614                        DEBUGOUT("Unable to acquire swfw sync\n");
4615                        return -E1000_ERR_SWFW_SYNC;
4616                }
4617
4618                /* Read the device control register and assert the E1000_CTRL_PHY_RST
4619                 * bit. Then, take it out of reset.
4620                 */
4621                ctrl = E1000_READ_REG(hw, CTRL);
4622                E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
4623                E1000_WRITE_FLUSH(hw);
4624
4625                if (hw->mac_type < e1000_82571)
4626                        udelay(10);
4627                else
4628                        udelay(100);
4629
4630                E1000_WRITE_REG(hw, CTRL, ctrl);
4631                E1000_WRITE_FLUSH(hw);
4632
4633                if (hw->mac_type >= e1000_82571)
4634                        mdelay(10);
4635
4636        } else {
4637                /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
4638                 * bit to put the PHY into reset. Then, take it out of reset.
4639                 */
4640                ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
4641                ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
4642                ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
4643                E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4644                E1000_WRITE_FLUSH(hw);
4645                mdelay(10);
4646                ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
4647                E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4648                E1000_WRITE_FLUSH(hw);
4649        }
4650        udelay(150);
4651
4652        if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
4653                /* Configure activity LED after PHY reset */
4654                led_ctrl = E1000_READ_REG(hw, LEDCTL);
4655                led_ctrl &= IGP_ACTIVITY_LED_MASK;
4656                led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
4657                E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
4658        }
4659
4660        e1000_swfw_sync_release(hw, swfw);
4661
4662        /* Wait for FW to finish PHY configuration. */
4663        ret_val = e1000_get_phy_cfg_done(hw);
4664        if (ret_val != E1000_SUCCESS)
4665                return ret_val;
4666
4667        return ret_val;
4668}
4669
4670/******************************************************************************
4671 * IGP phy init script - initializes the GbE PHY
4672 *
4673 * hw - Struct containing variables accessed by shared code
4674 *****************************************************************************/
4675static void
4676e1000_phy_init_script(struct e1000_hw *hw)
4677{
4678        uint32_t ret_val;
4679        uint16_t phy_saved_data;
4680        DEBUGFUNC();
4681
4682        if (hw->phy_init_script) {
4683                mdelay(20);
4684
4685                /* Save off the current value of register 0x2F5B to be
4686                 * restored at the end of this routine. */
4687                ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
4688
4689                /* Disabled the PHY transmitter */
4690                e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
4691
4692                mdelay(20);
4693
4694                e1000_write_phy_reg(hw, 0x0000, 0x0140);
4695
4696                mdelay(5);
4697
4698                switch (hw->mac_type) {
4699                case e1000_82541:
4700                case e1000_82547:
4701                        e1000_write_phy_reg(hw, 0x1F95, 0x0001);
4702
4703                        e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
4704
4705                        e1000_write_phy_reg(hw, 0x1F79, 0x0018);
4706
4707                        e1000_write_phy_reg(hw, 0x1F30, 0x1600);
4708
4709                        e1000_write_phy_reg(hw, 0x1F31, 0x0014);
4710
4711                        e1000_write_phy_reg(hw, 0x1F32, 0x161C);
4712
4713                        e1000_write_phy_reg(hw, 0x1F94, 0x0003);
4714
4715                        e1000_write_phy_reg(hw, 0x1F96, 0x003F);
4716
4717                        e1000_write_phy_reg(hw, 0x2010, 0x0008);
4718                        break;
4719
4720                case e1000_82541_rev_2:
4721                case e1000_82547_rev_2:
4722                        e1000_write_phy_reg(hw, 0x1F73, 0x0099);
4723                        break;
4724                default:
4725                        break;
4726                }
4727
4728                e1000_write_phy_reg(hw, 0x0000, 0x3300);
4729
4730                mdelay(20);
4731
4732                /* Now enable the transmitter */
4733                if (!ret_val)
4734                        e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
4735
4736                if (hw->mac_type == e1000_82547) {
4737                        uint16_t fused, fine, coarse;
4738
4739                        /* Move to analog registers page */
4740                        e1000_read_phy_reg(hw,
4741                                IGP01E1000_ANALOG_SPARE_FUSE_STATUS, &fused);
4742
4743                        if (!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
4744                                e1000_read_phy_reg(hw,
4745                                        IGP01E1000_ANALOG_FUSE_STATUS, &fused);
4746
4747                                fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
4748                                coarse = fused
4749                                        & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
4750
4751                                if (coarse >
4752                                        IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
4753                                        coarse -=
4754                                        IGP01E1000_ANALOG_FUSE_COARSE_10;
4755                                        fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
4756                                } else if (coarse
4757                                        == IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
4758                                        fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
4759
4760                                fused = (fused
4761                                        & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
4762                                        (fine
4763                                        & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
4764                                        (coarse
4765                                        & IGP01E1000_ANALOG_FUSE_COARSE_MASK);
4766
4767                                e1000_write_phy_reg(hw,
4768                                        IGP01E1000_ANALOG_FUSE_CONTROL, fused);
4769                                e1000_write_phy_reg(hw,
4770                                        IGP01E1000_ANALOG_FUSE_BYPASS,
4771                                IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
4772                        }
4773                }
4774        }
4775}
4776
4777/******************************************************************************
4778* Resets the PHY
4779*
4780* hw - Struct containing variables accessed by shared code
4781*
4782* Sets bit 15 of the MII Control register
4783******************************************************************************/
4784int32_t
4785e1000_phy_reset(struct e1000_hw *hw)
4786{
4787        int32_t ret_val;
4788        uint16_t phy_data;
4789
4790        DEBUGFUNC();
4791
4792        /* In the case of the phy reset being blocked, it's not an error, we
4793         * simply return success without performing the reset. */
4794        ret_val = e1000_check_phy_reset_block(hw);
4795        if (ret_val)
4796                return E1000_SUCCESS;
4797
4798        switch (hw->phy_type) {
4799        case e1000_phy_igp:
4800        case e1000_phy_igp_2:
4801        case e1000_phy_igp_3:
4802        case e1000_phy_ife:
4803        case e1000_phy_igb:
4804                ret_val = e1000_phy_hw_reset(hw);
4805                if (ret_val)
4806                        return ret_val;
4807                break;
4808        default:
4809                ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
4810                if (ret_val)
4811                        return ret_val;
4812
4813                phy_data |= MII_CR_RESET;
4814                ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
4815                if (ret_val)
4816                        return ret_val;
4817
4818                udelay(1);
4819                break;
4820        }
4821
4822        if (hw->phy_type == e1000_phy_igp || hw->phy_type == e1000_phy_igp_2)
4823                e1000_phy_init_script(hw);
4824
4825        return E1000_SUCCESS;
4826}
4827
4828static int e1000_set_phy_type (struct e1000_hw *hw)
4829{
4830        DEBUGFUNC ();
4831
4832        if (hw->mac_type == e1000_undefined)
4833                return -E1000_ERR_PHY_TYPE;
4834
4835        switch (hw->phy_id) {
4836        case M88E1000_E_PHY_ID:
4837        case M88E1000_I_PHY_ID:
4838        case M88E1011_I_PHY_ID:
4839        case M88E1111_I_PHY_ID:
4840                hw->phy_type = e1000_phy_m88;
4841                break;
4842        case IGP01E1000_I_PHY_ID:
4843                if (hw->mac_type == e1000_82541 ||
4844                        hw->mac_type == e1000_82541_rev_2 ||
4845                        hw->mac_type == e1000_82547 ||
4846                        hw->mac_type == e1000_82547_rev_2) {
4847                        hw->phy_type = e1000_phy_igp;
4848                        break;
4849                }
4850        case IGP03E1000_E_PHY_ID:
4851                hw->phy_type = e1000_phy_igp_3;
4852                break;
4853        case IFE_E_PHY_ID:
4854        case IFE_PLUS_E_PHY_ID:
4855        case IFE_C_E_PHY_ID:
4856                hw->phy_type = e1000_phy_ife;
4857                break;
4858        case GG82563_E_PHY_ID:
4859                if (hw->mac_type == e1000_80003es2lan) {
4860                        hw->phy_type = e1000_phy_gg82563;
4861                        break;
4862                }
4863        case BME1000_E_PHY_ID:
4864                hw->phy_type = e1000_phy_bm;
4865                break;
4866        case I210_I_PHY_ID:
4867                hw->phy_type = e1000_phy_igb;
4868                break;
4869                /* Fall Through */
4870        default:
4871                /* Should never have loaded on this device */
4872                hw->phy_type = e1000_phy_undefined;
4873                return -E1000_ERR_PHY_TYPE;
4874        }
4875
4876        return E1000_SUCCESS;
4877}
4878
4879/******************************************************************************
4880* Probes the expected PHY address for known PHY IDs
4881*
4882* hw - Struct containing variables accessed by shared code
4883******************************************************************************/
4884static int32_t
4885e1000_detect_gig_phy(struct e1000_hw *hw)
4886{
4887        int32_t phy_init_status, ret_val;
4888        uint16_t phy_id_high, phy_id_low;
4889        bool match = false;
4890
4891        DEBUGFUNC();
4892
4893        /* The 82571 firmware may still be configuring the PHY.  In this
4894         * case, we cannot access the PHY until the configuration is done.  So
4895         * we explicitly set the PHY values. */
4896        if (hw->mac_type == e1000_82571 ||
4897                hw->mac_type == e1000_82572) {
4898                hw->phy_id = IGP01E1000_I_PHY_ID;
4899                hw->phy_type = e1000_phy_igp_2;
4900                return E1000_SUCCESS;
4901        }
4902
4903        /* ESB-2 PHY reads require e1000_phy_gg82563 to be set because of a
4904         * work- around that forces PHY page 0 to be set or the reads fail.
4905         * The rest of the code in this routine uses e1000_read_phy_reg to
4906         * read the PHY ID.  So for ESB-2 we need to have this set so our
4907         * reads won't fail.  If the attached PHY is not a e1000_phy_gg82563,
4908         * the routines below will figure this out as well. */
4909        if (hw->mac_type == e1000_80003es2lan)
4910                hw->phy_type = e1000_phy_gg82563;
4911
4912        /* Read the PHY ID Registers to identify which PHY is onboard. */
4913        ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
4914        if (ret_val)
4915                return ret_val;
4916
4917        hw->phy_id = (uint32_t) (phy_id_high << 16);
4918        udelay(20);
4919        ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
4920        if (ret_val)
4921                return ret_val;
4922
4923        hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
4924        hw->phy_revision = (uint32_t) phy_id_low & ~PHY_REVISION_MASK;
4925
4926        switch (hw->mac_type) {
4927        case e1000_82543:
4928                if (hw->phy_id == M88E1000_E_PHY_ID)
4929                        match = true;
4930                break;
4931        case e1000_82544:
4932                if (hw->phy_id == M88E1000_I_PHY_ID)
4933                        match = true;
4934                break;
4935        case e1000_82540:
4936        case e1000_82545:
4937        case e1000_82545_rev_3:
4938        case e1000_82546:
4939        case e1000_82546_rev_3:
4940                if (hw->phy_id == M88E1011_I_PHY_ID)
4941                        match = true;
4942                break;
4943        case e1000_82541:
4944        case e1000_82541_rev_2:
4945        case e1000_82547:
4946        case e1000_82547_rev_2:
4947                if(hw->phy_id == IGP01E1000_I_PHY_ID)
4948                        match = true;
4949
4950                break;
4951        case e1000_82573:
4952                if (hw->phy_id == M88E1111_I_PHY_ID)
4953                        match = true;
4954                break;
4955        case e1000_82574:
4956                if (hw->phy_id == BME1000_E_PHY_ID)
4957                        match = true;
4958                break;
4959        case e1000_80003es2lan:
4960                if (hw->phy_id == GG82563_E_PHY_ID)
4961                        match = true;
4962                break;
4963        case e1000_ich8lan:
4964                if (hw->phy_id == IGP03E1000_E_PHY_ID)
4965                        match = true;
4966                if (hw->phy_id == IFE_E_PHY_ID)
4967                        match = true;
4968                if (hw->phy_id == IFE_PLUS_E_PHY_ID)
4969                        match = true;
4970                if (hw->phy_id == IFE_C_E_PHY_ID)
4971                        match = true;
4972                break;
4973        case e1000_igb:
4974                if (hw->phy_id == I210_I_PHY_ID)
4975                        match = true;
4976                break;
4977        default:
4978                DEBUGOUT("Invalid MAC type %d\n", hw->mac_type);
4979                return -E1000_ERR_CONFIG;
4980        }
4981
4982        phy_init_status = e1000_set_phy_type(hw);
4983
4984        if ((match) && (phy_init_status == E1000_SUCCESS)) {
4985                DEBUGOUT("PHY ID 0x%X detected\n", hw->phy_id);
4986                return 0;
4987        }
4988        DEBUGOUT("Invalid PHY ID 0x%X\n", hw->phy_id);
4989        return -E1000_ERR_PHY;
4990}
4991
4992/*****************************************************************************
4993 * Set media type and TBI compatibility.
4994 *
4995 * hw - Struct containing variables accessed by shared code
4996 * **************************************************************************/
4997void
4998e1000_set_media_type(struct e1000_hw *hw)
4999{
5000        uint32_t status;
5001
5002        DEBUGFUNC();
5003
5004        if (hw->mac_type != e1000_82543) {
5005                /* tbi_compatibility is only valid on 82543 */
5006                hw->tbi_compatibility_en = false;
5007        }
5008
5009        switch (hw->device_id) {
5010        case E1000_DEV_ID_82545GM_SERDES:
5011        case E1000_DEV_ID_82546GB_SERDES:
5012        case E1000_DEV_ID_82571EB_SERDES:
5013        case E1000_DEV_ID_82571EB_SERDES_DUAL:
5014        case E1000_DEV_ID_82571EB_SERDES_QUAD:
5015        case E1000_DEV_ID_82572EI_SERDES:
5016        case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
5017                hw->media_type = e1000_media_type_internal_serdes;
5018                break;
5019        default:
5020                switch (hw->mac_type) {
5021                case e1000_82542_rev2_0:
5022                case e1000_82542_rev2_1:
5023                        hw->media_type = e1000_media_type_fiber;
5024                        break;
5025                case e1000_ich8lan:
5026                case e1000_82573:
5027                case e1000_82574:
5028                case e1000_igb:
5029                        /* The STATUS_TBIMODE bit is reserved or reused
5030                         * for the this device.
5031                         */
5032                        hw->media_type = e1000_media_type_copper;
5033                        break;
5034                default:
5035                        status = E1000_READ_REG(hw, STATUS);
5036                        if (status & E1000_STATUS_TBIMODE) {
5037                                hw->media_type = e1000_media_type_fiber;
5038                                /* tbi_compatibility not valid on fiber */
5039                                hw->tbi_compatibility_en = false;
5040                        } else {
5041                                hw->media_type = e1000_media_type_copper;
5042                        }
5043                        break;
5044                }
5045        }
5046}
5047
5048/**
5049 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
5050 *
5051 * e1000_sw_init initializes the Adapter private data structure.
5052 * Fields are initialized based on PCI device information and
5053 * OS network device settings (MTU size).
5054 **/
5055
5056static int
5057e1000_sw_init(struct e1000_hw *hw)
5058{
5059        int result;
5060
5061        /* PCI config space info */
5062#ifdef CONFIG_DM_ETH
5063        dm_pci_read_config16(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5064        dm_pci_read_config16(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5065        dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5066                             &hw->subsystem_vendor_id);
5067        dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5068
5069        dm_pci_read_config8(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5070        dm_pci_read_config16(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
5071#else
5072        pci_read_config_word(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5073        pci_read_config_word(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5074        pci_read_config_word(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5075                             &hw->subsystem_vendor_id);
5076        pci_read_config_word(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5077
5078        pci_read_config_byte(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5079        pci_read_config_word(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
5080#endif
5081
5082        /* identify the MAC */
5083        result = e1000_set_mac_type(hw);
5084        if (result) {
5085                E1000_ERR(hw, "Unknown MAC Type\n");
5086                return result;
5087        }
5088
5089        switch (hw->mac_type) {
5090        default:
5091                break;
5092        case e1000_82541:
5093        case e1000_82547:
5094        case e1000_82541_rev_2:
5095        case e1000_82547_rev_2:
5096                hw->phy_init_script = 1;
5097                break;
5098        }
5099
5100        /* flow control settings */
5101        hw->fc_high_water = E1000_FC_HIGH_THRESH;
5102        hw->fc_low_water = E1000_FC_LOW_THRESH;
5103        hw->fc_pause_time = E1000_FC_PAUSE_TIME;
5104        hw->fc_send_xon = 1;
5105
5106        /* Media type - copper or fiber */
5107        hw->tbi_compatibility_en = true;
5108        e1000_set_media_type(hw);
5109
5110        if (hw->mac_type >= e1000_82543) {
5111                uint32_t status = E1000_READ_REG(hw, STATUS);
5112
5113                if (status & E1000_STATUS_TBIMODE) {
5114                        DEBUGOUT("fiber interface\n");
5115                        hw->media_type = e1000_media_type_fiber;
5116                } else {
5117                        DEBUGOUT("copper interface\n");
5118                        hw->media_type = e1000_media_type_copper;
5119                }
5120        } else {
5121                hw->media_type = e1000_media_type_fiber;
5122        }
5123
5124        hw->wait_autoneg_complete = true;
5125        if (hw->mac_type < e1000_82543)
5126                hw->report_tx_early = 0;
5127        else
5128                hw->report_tx_early = 1;
5129
5130        return E1000_SUCCESS;
5131}
5132
5133void
5134fill_rx(struct e1000_hw *hw)
5135{
5136        struct e1000_rx_desc *rd;
5137        unsigned long flush_start, flush_end;
5138
5139        rx_last = rx_tail;
5140        rd = rx_base + rx_tail;
5141        rx_tail = (rx_tail + 1) % 8;
5142        memset(rd, 0, 16);
5143        rd->buffer_addr = cpu_to_le64((unsigned long)packet);
5144
5145        /*
5146         * Make sure there are no stale data in WB over this area, which
5147         * might get written into the memory while the e1000 also writes
5148         * into the same memory area.
5149         */
5150        invalidate_dcache_range((unsigned long)packet,
5151                                (unsigned long)packet + 4096);
5152        /* Dump the DMA descriptor into RAM. */
5153        flush_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
5154        flush_end = flush_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5155        flush_dcache_range(flush_start, flush_end);
5156
5157        E1000_WRITE_REG(hw, RDT, rx_tail);
5158}
5159
5160/**
5161 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
5162 * @adapter: board private structure
5163 *
5164 * Configure the Tx unit of the MAC after a reset.
5165 **/
5166
5167static void
5168e1000_configure_tx(struct e1000_hw *hw)
5169{
5170        unsigned long tctl;
5171        unsigned long tipg, tarc;
5172        uint32_t ipgr1, ipgr2;
5173
5174        E1000_WRITE_REG(hw, TDBAL, lower_32_bits((unsigned long)tx_base));
5175        E1000_WRITE_REG(hw, TDBAH, upper_32_bits((unsigned long)tx_base));
5176
5177        E1000_WRITE_REG(hw, TDLEN, 128);
5178
5179        /* Setup the HW Tx Head and Tail descriptor pointers */
5180        E1000_WRITE_REG(hw, TDH, 0);
5181        E1000_WRITE_REG(hw, TDT, 0);
5182        tx_tail = 0;
5183
5184        /* Set the default values for the Tx Inter Packet Gap timer */
5185        if (hw->mac_type <= e1000_82547_rev_2 &&
5186            (hw->media_type == e1000_media_type_fiber ||
5187             hw->media_type == e1000_media_type_internal_serdes))
5188                tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
5189        else
5190                tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
5191
5192        /* Set the default values for the Tx Inter Packet Gap timer */
5193        switch (hw->mac_type) {
5194        case e1000_82542_rev2_0:
5195        case e1000_82542_rev2_1:
5196                tipg = DEFAULT_82542_TIPG_IPGT;
5197                ipgr1 = DEFAULT_82542_TIPG_IPGR1;
5198                ipgr2 = DEFAULT_82542_TIPG_IPGR2;
5199                break;
5200        case e1000_80003es2lan:
5201                ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5202                ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
5203                break;
5204        default:
5205                ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5206                ipgr2 = DEFAULT_82543_TIPG_IPGR2;
5207                break;
5208        }
5209        tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
5210        tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
5211        E1000_WRITE_REG(hw, TIPG, tipg);
5212        /* Program the Transmit Control Register */
5213        tctl = E1000_READ_REG(hw, TCTL);
5214        tctl &= ~E1000_TCTL_CT;
5215        tctl |= E1000_TCTL_EN | E1000_TCTL_PSP |
5216            (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
5217
5218        if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
5219                tarc = E1000_READ_REG(hw, TARC0);
5220                /* set the speed mode bit, we'll clear it if we're not at
5221                 * gigabit link later */
5222                /* git bit can be set to 1*/
5223        } else if (hw->mac_type == e1000_80003es2lan) {
5224                tarc = E1000_READ_REG(hw, TARC0);
5225                tarc |= 1;
5226                E1000_WRITE_REG(hw, TARC0, tarc);
5227                tarc = E1000_READ_REG(hw, TARC1);
5228                tarc |= 1;
5229                E1000_WRITE_REG(hw, TARC1, tarc);
5230        }
5231
5232
5233        e1000_config_collision_dist(hw);
5234        /* Setup Transmit Descriptor Settings for eop descriptor */
5235        hw->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
5236
5237        /* Need to set up RS bit */
5238        if (hw->mac_type < e1000_82543)
5239                hw->txd_cmd |= E1000_TXD_CMD_RPS;
5240        else
5241                hw->txd_cmd |= E1000_TXD_CMD_RS;
5242
5243
5244        if (hw->mac_type == e1000_igb) {
5245                E1000_WRITE_REG(hw, TCTL_EXT, 0x42 << 10);
5246
5247                uint32_t reg_txdctl = E1000_READ_REG(hw, TXDCTL);
5248                reg_txdctl |= 1 << 25;
5249                E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
5250                mdelay(20);
5251        }
5252
5253
5254
5255        E1000_WRITE_REG(hw, TCTL, tctl);
5256
5257
5258}
5259
5260/**
5261 * e1000_setup_rctl - configure the receive control register
5262 * @adapter: Board private structure
5263 **/
5264static void
5265e1000_setup_rctl(struct e1000_hw *hw)
5266{
5267        uint32_t rctl;
5268
5269        rctl = E1000_READ_REG(hw, RCTL);
5270
5271        rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
5272
5273        rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO
5274                | E1000_RCTL_RDMTS_HALF;        /* |
5275                        (hw.mc_filter_type << E1000_RCTL_MO_SHIFT); */
5276
5277        if (hw->tbi_compatibility_on == 1)
5278                rctl |= E1000_RCTL_SBP;
5279        else
5280                rctl &= ~E1000_RCTL_SBP;
5281
5282        rctl &= ~(E1000_RCTL_SZ_4096);
5283                rctl |= E1000_RCTL_SZ_2048;
5284                rctl &= ~(E1000_RCTL_BSEX | E1000_RCTL_LPE);
5285        E1000_WRITE_REG(hw, RCTL, rctl);
5286}
5287
5288/**
5289 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
5290 * @adapter: board private structure
5291 *
5292 * Configure the Rx unit of the MAC after a reset.
5293 **/
5294static void
5295e1000_configure_rx(struct e1000_hw *hw)
5296{
5297        unsigned long rctl, ctrl_ext;
5298        rx_tail = 0;
5299
5300        /* make sure receives are disabled while setting up the descriptors */
5301        rctl = E1000_READ_REG(hw, RCTL);
5302        E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
5303        if (hw->mac_type >= e1000_82540) {
5304                /* Set the interrupt throttling rate.  Value is calculated
5305                 * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) */
5306#define MAX_INTS_PER_SEC        8000
5307#define DEFAULT_ITR             1000000000/(MAX_INTS_PER_SEC * 256)
5308                E1000_WRITE_REG(hw, ITR, DEFAULT_ITR);
5309        }
5310
5311        if (hw->mac_type >= e1000_82571) {
5312                ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
5313                /* Reset delay timers after every interrupt */
5314                ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
5315                E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
5316                E1000_WRITE_FLUSH(hw);
5317        }
5318        /* Setup the Base and Length of the Rx Descriptor Ring */
5319        E1000_WRITE_REG(hw, RDBAL, lower_32_bits((unsigned long)rx_base));
5320        E1000_WRITE_REG(hw, RDBAH, upper_32_bits((unsigned long)rx_base));
5321
5322        E1000_WRITE_REG(hw, RDLEN, 128);
5323
5324        /* Setup the HW Rx Head and Tail Descriptor Pointers */
5325        E1000_WRITE_REG(hw, RDH, 0);
5326        E1000_WRITE_REG(hw, RDT, 0);
5327        /* Enable Receives */
5328
5329        if (hw->mac_type == e1000_igb) {
5330
5331                uint32_t reg_rxdctl = E1000_READ_REG(hw, RXDCTL);
5332                reg_rxdctl |= 1 << 25;
5333                E1000_WRITE_REG(hw, RXDCTL, reg_rxdctl);
5334                mdelay(20);
5335        }
5336
5337        E1000_WRITE_REG(hw, RCTL, rctl);
5338
5339        fill_rx(hw);
5340}
5341
5342/**************************************************************************
5343POLL - Wait for a frame
5344***************************************************************************/
5345static int
5346_e1000_poll(struct e1000_hw *hw)
5347{
5348        struct e1000_rx_desc *rd;
5349        unsigned long inval_start, inval_end;
5350        uint32_t len;
5351
5352        /* return true if there's an ethernet packet ready to read */
5353        rd = rx_base + rx_last;
5354
5355        /* Re-load the descriptor from RAM. */
5356        inval_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
5357        inval_end = inval_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5358        invalidate_dcache_range(inval_start, inval_end);
5359
5360        if (!(rd->status & E1000_RXD_STAT_DD))
5361                return 0;
5362        /* DEBUGOUT("recv: packet len=%d\n", rd->length); */
5363        /* Packet received, make sure the data are re-loaded from RAM. */
5364        len = le16_to_cpu(rd->length);
5365        invalidate_dcache_range((unsigned long)packet,
5366                                (unsigned long)packet +
5367                                roundup(len, ARCH_DMA_MINALIGN));
5368        return len;
5369}
5370
5371static int _e1000_transmit(struct e1000_hw *hw, void *txpacket, int length)
5372{
5373        void *nv_packet = (void *)txpacket;
5374        struct e1000_tx_desc *txp;
5375        int i = 0;
5376        unsigned long flush_start, flush_end;
5377
5378        txp = tx_base + tx_tail;
5379        tx_tail = (tx_tail + 1) % 8;
5380
5381        txp->buffer_addr = cpu_to_le64(virt_to_bus(hw->pdev, nv_packet));
5382        txp->lower.data = cpu_to_le32(hw->txd_cmd | length);
5383        txp->upper.data = 0;
5384
5385        /* Dump the packet into RAM so e1000 can pick them. */
5386        flush_dcache_range((unsigned long)nv_packet,
5387                           (unsigned long)nv_packet +
5388                           roundup(length, ARCH_DMA_MINALIGN));
5389        /* Dump the descriptor into RAM as well. */
5390        flush_start = ((unsigned long)txp) & ~(ARCH_DMA_MINALIGN - 1);
5391        flush_end = flush_start + roundup(sizeof(*txp), ARCH_DMA_MINALIGN);
5392        flush_dcache_range(flush_start, flush_end);
5393
5394        E1000_WRITE_REG(hw, TDT, tx_tail);
5395
5396        E1000_WRITE_FLUSH(hw);
5397        while (1) {
5398                invalidate_dcache_range(flush_start, flush_end);
5399                if (le32_to_cpu(txp->upper.data) & E1000_TXD_STAT_DD)
5400                        break;
5401                if (i++ > TOUT_LOOP) {
5402                        DEBUGOUT("e1000: tx timeout\n");
5403                        return 0;
5404                }
5405                udelay(10);     /* give the nic a chance to write to the register */
5406        }
5407        return 1;
5408}
5409
5410static void
5411_e1000_disable(struct e1000_hw *hw)
5412{
5413        /* Turn off the ethernet interface */
5414        E1000_WRITE_REG(hw, RCTL, 0);
5415        E1000_WRITE_REG(hw, TCTL, 0);
5416
5417        /* Clear the transmit ring */
5418        E1000_WRITE_REG(hw, TDH, 0);
5419        E1000_WRITE_REG(hw, TDT, 0);
5420
5421        /* Clear the receive ring */
5422        E1000_WRITE_REG(hw, RDH, 0);
5423        E1000_WRITE_REG(hw, RDT, 0);
5424
5425        mdelay(10);
5426}
5427
5428/*reset function*/
5429static inline int
5430e1000_reset(struct e1000_hw *hw, unsigned char enetaddr[6])
5431{
5432        e1000_reset_hw(hw);
5433        if (hw->mac_type >= e1000_82544)
5434                E1000_WRITE_REG(hw, WUC, 0);
5435
5436        return e1000_init_hw(hw, enetaddr);
5437}
5438
5439static int
5440_e1000_init(struct e1000_hw *hw, unsigned char enetaddr[6])
5441{
5442        int ret_val = 0;
5443
5444        ret_val = e1000_reset(hw, enetaddr);
5445        if (ret_val < 0) {
5446                if ((ret_val == -E1000_ERR_NOLINK) ||
5447                    (ret_val == -E1000_ERR_TIMEOUT)) {
5448                        E1000_ERR(hw, "Valid Link not detected: %d\n", ret_val);
5449                } else {
5450                        E1000_ERR(hw, "Hardware Initialization Failed\n");
5451                }
5452                return ret_val;
5453        }
5454        e1000_configure_tx(hw);
5455        e1000_setup_rctl(hw);
5456        e1000_configure_rx(hw);
5457        return 0;
5458}
5459
5460/******************************************************************************
5461 * Gets the current PCI bus type of hardware
5462 *
5463 * hw - Struct containing variables accessed by shared code
5464 *****************************************************************************/
5465void e1000_get_bus_type(struct e1000_hw *hw)
5466{
5467        uint32_t status;
5468
5469        switch (hw->mac_type) {
5470        case e1000_82542_rev2_0:
5471        case e1000_82542_rev2_1:
5472                hw->bus_type = e1000_bus_type_pci;
5473                break;
5474        case e1000_82571:
5475        case e1000_82572:
5476        case e1000_82573:
5477        case e1000_82574:
5478        case e1000_80003es2lan:
5479        case e1000_ich8lan:
5480        case e1000_igb:
5481                hw->bus_type = e1000_bus_type_pci_express;
5482                break;
5483        default:
5484                status = E1000_READ_REG(hw, STATUS);
5485                hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
5486                                e1000_bus_type_pcix : e1000_bus_type_pci;
5487                break;
5488        }
5489}
5490
5491#ifndef CONFIG_DM_ETH
5492/* A list of all registered e1000 devices */
5493static LIST_HEAD(e1000_hw_list);
5494#endif
5495
5496#ifdef CONFIG_DM_ETH
5497static int e1000_init_one(struct e1000_hw *hw, int cardnum,
5498                          struct udevice *devno, unsigned char enetaddr[6])
5499#else
5500static int e1000_init_one(struct e1000_hw *hw, int cardnum, pci_dev_t devno,
5501                          unsigned char enetaddr[6])
5502#endif
5503{
5504        u32 val;
5505
5506        /* Assign the passed-in values */
5507#ifdef CONFIG_DM_ETH
5508        hw->pdev = devno;
5509#else
5510        hw->pdev = devno;
5511#endif
5512        hw->cardnum = cardnum;
5513
5514        /* Print a debug message with the IO base address */
5515#ifdef CONFIG_DM_ETH
5516        dm_pci_read_config32(devno, PCI_BASE_ADDRESS_0, &val);
5517#else
5518        pci_read_config_dword(devno, PCI_BASE_ADDRESS_0, &val);
5519#endif
5520        E1000_DBG(hw, "iobase 0x%08x\n", val & 0xfffffff0);
5521
5522        /* Try to enable I/O accesses and bus-mastering */
5523        val = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
5524#ifdef CONFIG_DM_ETH
5525        dm_pci_write_config32(devno, PCI_COMMAND, val);
5526#else
5527        pci_write_config_dword(devno, PCI_COMMAND, val);
5528#endif
5529
5530        /* Make sure it worked */
5531#ifdef CONFIG_DM_ETH
5532        dm_pci_read_config32(devno, PCI_COMMAND, &val);
5533#else
5534        pci_read_config_dword(devno, PCI_COMMAND, &val);
5535#endif
5536        if (!(val & PCI_COMMAND_MEMORY)) {
5537                E1000_ERR(hw, "Can't enable I/O memory\n");
5538                return -ENOSPC;
5539        }
5540        if (!(val & PCI_COMMAND_MASTER)) {
5541                E1000_ERR(hw, "Can't enable bus-mastering\n");
5542                return -EPERM;
5543        }
5544
5545        /* Are these variables needed? */
5546        hw->fc = e1000_fc_default;
5547        hw->original_fc = e1000_fc_default;
5548        hw->autoneg_failed = 0;
5549        hw->autoneg = 1;
5550        hw->get_link_status = true;
5551#ifndef CONFIG_E1000_NO_NVM
5552        hw->eeprom_semaphore_present = true;
5553#endif
5554#ifdef CONFIG_DM_ETH
5555        hw->hw_addr = dm_pci_map_bar(devno,     PCI_BASE_ADDRESS_0,
5556                                                PCI_REGION_MEM);
5557#else
5558        hw->hw_addr = pci_map_bar(devno,        PCI_BASE_ADDRESS_0,
5559                                                PCI_REGION_MEM);
5560#endif
5561        hw->mac_type = e1000_undefined;
5562
5563        /* MAC and Phy settings */
5564        if (e1000_sw_init(hw) < 0) {
5565                E1000_ERR(hw, "Software init failed\n");
5566                return -EIO;
5567        }
5568        if (e1000_check_phy_reset_block(hw))
5569                E1000_ERR(hw, "PHY Reset is blocked!\n");
5570
5571        /* Basic init was OK, reset the hardware and allow SPI access */
5572        e1000_reset_hw(hw);
5573
5574#ifndef CONFIG_E1000_NO_NVM
5575        /* Validate the EEPROM and get chipset information */
5576        if (e1000_init_eeprom_params(hw)) {
5577                E1000_ERR(hw, "EEPROM is invalid!\n");
5578                return -EINVAL;
5579        }
5580        if ((E1000_READ_REG(hw, I210_EECD) & E1000_EECD_FLUPD) &&
5581            e1000_validate_eeprom_checksum(hw))
5582                return -ENXIO;
5583        e1000_read_mac_addr(hw, enetaddr);
5584#endif
5585        e1000_get_bus_type(hw);
5586
5587#ifndef CONFIG_E1000_NO_NVM
5588        printf("e1000: %02x:%02x:%02x:%02x:%02x:%02x\n       ",
5589               enetaddr[0], enetaddr[1], enetaddr[2],
5590               enetaddr[3], enetaddr[4], enetaddr[5]);
5591#else
5592        memset(enetaddr, 0, 6);
5593        printf("e1000: no NVM\n");
5594#endif
5595
5596        return 0;
5597}
5598
5599/* Put the name of a device in a string */
5600static void e1000_name(char *str, int cardnum)
5601{
5602        sprintf(str, "e1000#%u", cardnum);
5603}
5604
5605#ifndef CONFIG_DM_ETH
5606/**************************************************************************
5607TRANSMIT - Transmit a frame
5608***************************************************************************/
5609static int e1000_transmit(struct eth_device *nic, void *txpacket, int length)
5610{
5611        struct e1000_hw *hw = nic->priv;
5612
5613        return _e1000_transmit(hw, txpacket, length);
5614}
5615
5616/**************************************************************************
5617DISABLE - Turn off ethernet interface
5618***************************************************************************/
5619static void
5620e1000_disable(struct eth_device *nic)
5621{
5622        struct e1000_hw *hw = nic->priv;
5623
5624        _e1000_disable(hw);
5625}
5626
5627/**************************************************************************
5628INIT - set up ethernet interface(s)
5629***************************************************************************/
5630static int
5631e1000_init(struct eth_device *nic, bd_t *bis)
5632{
5633        struct e1000_hw *hw = nic->priv;
5634
5635        return _e1000_init(hw, nic->enetaddr);
5636}
5637
5638static int
5639e1000_poll(struct eth_device *nic)
5640{
5641        struct e1000_hw *hw = nic->priv;
5642        int len;
5643
5644        len = _e1000_poll(hw);
5645        if (len) {
5646                net_process_received_packet((uchar *)packet, len);
5647                fill_rx(hw);
5648        }
5649
5650        return len ? 1 : 0;
5651}
5652
5653static int e1000_write_hwaddr(struct eth_device *dev)
5654{
5655#ifndef CONFIG_E1000_NO_NVM
5656        unsigned char *mac = dev->enetaddr;
5657        unsigned char current_mac[6];
5658        struct e1000_hw *hw = dev->priv;
5659        uint16_t data[3];
5660        int ret_val, i;
5661
5662        DEBUGOUT("%s: mac=%pM\n", __func__, mac);
5663
5664        memset(current_mac, 0, 6);
5665
5666        /* Read from EEPROM, not from registers, to make sure
5667         * the address is persistently configured
5668         */
5669        ret_val = e1000_read_mac_addr_from_eeprom(hw, current_mac);
5670        DEBUGOUT("%s: current mac=%pM\n", __func__, current_mac);
5671
5672        /* Only write to EEPROM if the given address is different or
5673         * reading the current address failed
5674         */
5675        if (!ret_val && memcmp(current_mac, mac, 6) == 0)
5676                return 0;
5677
5678        for (i = 0; i < 3; ++i)
5679                data[i] = mac[i * 2 + 1] << 8 | mac[i * 2];
5680
5681        ret_val = e1000_write_eeprom_srwr(hw, 0x0, 3, data);
5682
5683        if (!ret_val)
5684                ret_val = e1000_update_eeprom_checksum_i210(hw);
5685
5686        return ret_val;
5687#else
5688        return 0;
5689#endif
5690}
5691
5692/**************************************************************************
5693PROBE - Look for an adapter, this routine's visible to the outside
5694You should omit the last argument struct pci_device * for a non-PCI NIC
5695***************************************************************************/
5696int
5697e1000_initialize(bd_t * bis)
5698{
5699        unsigned int i;
5700        pci_dev_t devno;
5701        int ret;
5702
5703        DEBUGFUNC();
5704
5705        /* Find and probe all the matching PCI devices */
5706        for (i = 0; (devno = pci_find_devices(e1000_supported, i)) >= 0; i++) {
5707                /*
5708                 * These will never get freed due to errors, this allows us to
5709                 * perform SPI EEPROM programming from U-Boot, for example.
5710                 */
5711                struct eth_device *nic = malloc(sizeof(*nic));
5712                struct e1000_hw *hw = malloc(sizeof(*hw));
5713                if (!nic || !hw) {
5714                        printf("e1000#%u: Out of Memory!\n", i);
5715                        free(nic);
5716                        free(hw);
5717                        continue;
5718                }
5719
5720                /* Make sure all of the fields are initially zeroed */
5721                memset(nic, 0, sizeof(*nic));
5722                memset(hw, 0, sizeof(*hw));
5723                nic->priv = hw;
5724
5725                /* Generate a card name */
5726                e1000_name(nic->name, i);
5727                hw->name = nic->name;
5728
5729                ret = e1000_init_one(hw, i, devno, nic->enetaddr);
5730                if (ret)
5731                        continue;
5732                list_add_tail(&hw->list_node, &e1000_hw_list);
5733
5734                hw->nic = nic;
5735
5736                /* Set up the function pointers and register the device */
5737                nic->init = e1000_init;
5738                nic->recv = e1000_poll;
5739                nic->send = e1000_transmit;
5740                nic->halt = e1000_disable;
5741                nic->write_hwaddr = e1000_write_hwaddr;
5742                eth_register(nic);
5743        }
5744
5745        return i;
5746}
5747
5748struct e1000_hw *e1000_find_card(unsigned int cardnum)
5749{
5750        struct e1000_hw *hw;
5751
5752        list_for_each_entry(hw, &e1000_hw_list, list_node)
5753                if (hw->cardnum == cardnum)
5754                        return hw;
5755
5756        return NULL;
5757}
5758#endif /* !CONFIG_DM_ETH */
5759
5760#ifdef CONFIG_CMD_E1000
5761static int do_e1000(cmd_tbl_t *cmdtp, int flag,
5762                int argc, char * const argv[])
5763{
5764        unsigned char *mac = NULL;
5765#ifdef CONFIG_DM_ETH
5766        struct eth_pdata *plat;
5767        struct udevice *dev;
5768        char name[30];
5769        int ret;
5770#endif
5771#if !defined(CONFIG_DM_ETH) || defined(CONFIG_E1000_SPI)
5772        struct e1000_hw *hw;
5773#endif
5774        int cardnum;
5775
5776        if (argc < 3) {
5777                cmd_usage(cmdtp);
5778                return 1;
5779        }
5780
5781        /* Make sure we can find the requested e1000 card */
5782        cardnum = simple_strtoul(argv[1], NULL, 10);
5783#ifdef CONFIG_DM_ETH
5784        e1000_name(name, cardnum);
5785        ret = uclass_get_device_by_name(UCLASS_ETH, name, &dev);
5786        if (!ret) {
5787                plat = dev_get_platdata(dev);
5788                mac = plat->enetaddr;
5789        }
5790#else
5791        hw = e1000_find_card(cardnum);
5792        if (hw)
5793                mac = hw->nic->enetaddr;
5794#endif
5795        if (!mac) {
5796                printf("e1000: ERROR: No such device: e1000#%s\n", argv[1]);
5797                return 1;
5798        }
5799
5800        if (!strcmp(argv[2], "print-mac-address")) {
5801                printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
5802                        mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
5803                return 0;
5804        }
5805
5806#ifdef CONFIG_E1000_SPI
5807#ifdef CONFIG_DM_ETH
5808        hw = dev_get_priv(dev);
5809#endif
5810        /* Handle the "SPI" subcommand */
5811        if (!strcmp(argv[2], "spi"))
5812                return do_e1000_spi(cmdtp, hw, argc - 3, argv + 3);
5813#endif
5814
5815        cmd_usage(cmdtp);
5816        return 1;
5817}
5818
5819U_BOOT_CMD(
5820        e1000, 7, 0, do_e1000,
5821        "Intel e1000 controller management",
5822        /*  */"<card#> print-mac-address\n"
5823#ifdef CONFIG_E1000_SPI
5824        "e1000 <card#> spi show [<offset> [<length>]]\n"
5825        "e1000 <card#> spi dump <addr> <offset> <length>\n"
5826        "e1000 <card#> spi program <addr> <offset> <length>\n"
5827        "e1000 <card#> spi checksum [update]\n"
5828#endif
5829        "       - Manage the Intel E1000 PCI device"
5830);
5831#endif /* not CONFIG_CMD_E1000 */
5832
5833#ifdef CONFIG_DM_ETH
5834static int e1000_eth_start(struct udevice *dev)
5835{
5836        struct eth_pdata *plat = dev_get_platdata(dev);
5837        struct e1000_hw *hw = dev_get_priv(dev);
5838
5839        return _e1000_init(hw, plat->enetaddr);
5840}
5841
5842static void e1000_eth_stop(struct udevice *dev)
5843{
5844        struct e1000_hw *hw = dev_get_priv(dev);
5845
5846        _e1000_disable(hw);
5847}
5848
5849static int e1000_eth_send(struct udevice *dev, void *packet, int length)
5850{
5851        struct e1000_hw *hw = dev_get_priv(dev);
5852        int ret;
5853
5854        ret = _e1000_transmit(hw, packet, length);
5855
5856        return ret ? 0 : -ETIMEDOUT;
5857}
5858
5859static int e1000_eth_recv(struct udevice *dev, int flags, uchar **packetp)
5860{
5861        struct e1000_hw *hw = dev_get_priv(dev);
5862        int len;
5863
5864        len = _e1000_poll(hw);
5865        if (len)
5866                *packetp = packet;
5867
5868        return len ? len : -EAGAIN;
5869}
5870
5871static int e1000_free_pkt(struct udevice *dev, uchar *packet, int length)
5872{
5873        struct e1000_hw *hw = dev_get_priv(dev);
5874
5875        fill_rx(hw);
5876
5877        return 0;
5878}
5879
5880static int e1000_eth_probe(struct udevice *dev)
5881{
5882        struct eth_pdata *plat = dev_get_platdata(dev);
5883        struct e1000_hw *hw = dev_get_priv(dev);
5884        int ret;
5885
5886        hw->name = dev->name;
5887        ret = e1000_init_one(hw, trailing_strtol(dev->name),
5888                             dev, plat->enetaddr);
5889        if (ret < 0) {
5890                printf(pr_fmt("failed to initialize card: %d\n"), ret);
5891                return ret;
5892        }
5893
5894        return 0;
5895}
5896
5897static int e1000_eth_bind(struct udevice *dev)
5898{
5899        char name[20];
5900
5901        /*
5902         * A simple way to number the devices. When device tree is used this
5903         * is unnecessary, but when the device is just discovered on the PCI
5904         * bus we need a name. We could instead have the uclass figure out
5905         * which devices are different and number them.
5906         */
5907        e1000_name(name, num_cards++);
5908
5909        return device_set_name(dev, name);
5910}
5911
5912static const struct eth_ops e1000_eth_ops = {
5913        .start  = e1000_eth_start,
5914        .send   = e1000_eth_send,
5915        .recv   = e1000_eth_recv,
5916        .stop   = e1000_eth_stop,
5917        .free_pkt = e1000_free_pkt,
5918};
5919
5920static const struct udevice_id e1000_eth_ids[] = {
5921        { .compatible = "intel,e1000" },
5922        { }
5923};
5924
5925U_BOOT_DRIVER(eth_e1000) = {
5926        .name   = "eth_e1000",
5927        .id     = UCLASS_ETH,
5928        .of_match = e1000_eth_ids,
5929        .bind   = e1000_eth_bind,
5930        .probe  = e1000_eth_probe,
5931        .ops    = &e1000_eth_ops,
5932        .priv_auto_alloc_size = sizeof(struct e1000_hw),
5933        .platdata_auto_alloc_size = sizeof(struct eth_pdata),
5934};
5935
5936U_BOOT_PCI_DEVICE(eth_e1000, e1000_supported);
5937#endif
5938