1*1a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
22b133ad6SJeff Kirsher /*
32b133ad6SJeff Kirsher * Copyright(c) 2007 Atheros Corporation. All rights reserved.
42b133ad6SJeff Kirsher *
52b133ad6SJeff Kirsher * Derived from Intel e1000 driver
62b133ad6SJeff Kirsher * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
72b133ad6SJeff Kirsher */
82b133ad6SJeff Kirsher #include <linux/pci.h>
92b133ad6SJeff Kirsher #include <linux/delay.h>
102b133ad6SJeff Kirsher #include <linux/mii.h>
112b133ad6SJeff Kirsher #include <linux/crc32.h>
122b133ad6SJeff Kirsher
132b133ad6SJeff Kirsher #include "atl1e.h"
142b133ad6SJeff Kirsher
152b133ad6SJeff Kirsher /*
162b133ad6SJeff Kirsher * check_eeprom_exist
172b133ad6SJeff Kirsher * return 0 if eeprom exist
182b133ad6SJeff Kirsher */
atl1e_check_eeprom_exist(struct atl1e_hw * hw)192b133ad6SJeff Kirsher int atl1e_check_eeprom_exist(struct atl1e_hw *hw)
202b133ad6SJeff Kirsher {
212b133ad6SJeff Kirsher u32 value;
222b133ad6SJeff Kirsher
232b133ad6SJeff Kirsher value = AT_READ_REG(hw, REG_SPI_FLASH_CTRL);
242b133ad6SJeff Kirsher if (value & SPI_FLASH_CTRL_EN_VPD) {
252b133ad6SJeff Kirsher value &= ~SPI_FLASH_CTRL_EN_VPD;
262b133ad6SJeff Kirsher AT_WRITE_REG(hw, REG_SPI_FLASH_CTRL, value);
272b133ad6SJeff Kirsher }
282b133ad6SJeff Kirsher value = AT_READ_REGW(hw, REG_PCIE_CAP_LIST);
292b133ad6SJeff Kirsher return ((value & 0xFF00) == 0x6C00) ? 0 : 1;
302b133ad6SJeff Kirsher }
312b133ad6SJeff Kirsher
atl1e_hw_set_mac_addr(struct atl1e_hw * hw)322b133ad6SJeff Kirsher void atl1e_hw_set_mac_addr(struct atl1e_hw *hw)
332b133ad6SJeff Kirsher {
342b133ad6SJeff Kirsher u32 value;
352b133ad6SJeff Kirsher /*
362b133ad6SJeff Kirsher * 00-0B-6A-F6-00-DC
372b133ad6SJeff Kirsher * 0: 6AF600DC 1: 000B
382b133ad6SJeff Kirsher * low dword
392b133ad6SJeff Kirsher */
402b133ad6SJeff Kirsher value = (((u32)hw->mac_addr[2]) << 24) |
412b133ad6SJeff Kirsher (((u32)hw->mac_addr[3]) << 16) |
422b133ad6SJeff Kirsher (((u32)hw->mac_addr[4]) << 8) |
432b133ad6SJeff Kirsher (((u32)hw->mac_addr[5])) ;
442b133ad6SJeff Kirsher AT_WRITE_REG_ARRAY(hw, REG_MAC_STA_ADDR, 0, value);
452b133ad6SJeff Kirsher /* hight dword */
462b133ad6SJeff Kirsher value = (((u32)hw->mac_addr[0]) << 8) |
472b133ad6SJeff Kirsher (((u32)hw->mac_addr[1])) ;
482b133ad6SJeff Kirsher AT_WRITE_REG_ARRAY(hw, REG_MAC_STA_ADDR, 1, value);
492b133ad6SJeff Kirsher }
502b133ad6SJeff Kirsher
512b133ad6SJeff Kirsher /*
522b133ad6SJeff Kirsher * atl1e_get_permanent_address
532b133ad6SJeff Kirsher * return 0 if get valid mac address,
542b133ad6SJeff Kirsher */
atl1e_get_permanent_address(struct atl1e_hw * hw)552b133ad6SJeff Kirsher static int atl1e_get_permanent_address(struct atl1e_hw *hw)
562b133ad6SJeff Kirsher {
572b133ad6SJeff Kirsher u32 addr[2];
582b133ad6SJeff Kirsher u32 i;
592b133ad6SJeff Kirsher u32 twsi_ctrl_data;
602b133ad6SJeff Kirsher u8 eth_addr[ETH_ALEN];
612b133ad6SJeff Kirsher
622b133ad6SJeff Kirsher if (is_valid_ether_addr(hw->perm_mac_addr))
632b133ad6SJeff Kirsher return 0;
642b133ad6SJeff Kirsher
652b133ad6SJeff Kirsher /* init */
662b133ad6SJeff Kirsher addr[0] = addr[1] = 0;
672b133ad6SJeff Kirsher
682b133ad6SJeff Kirsher if (!atl1e_check_eeprom_exist(hw)) {
692b133ad6SJeff Kirsher /* eeprom exist */
702b133ad6SJeff Kirsher twsi_ctrl_data = AT_READ_REG(hw, REG_TWSI_CTRL);
712b133ad6SJeff Kirsher twsi_ctrl_data |= TWSI_CTRL_SW_LDSTART;
722b133ad6SJeff Kirsher AT_WRITE_REG(hw, REG_TWSI_CTRL, twsi_ctrl_data);
732b133ad6SJeff Kirsher for (i = 0; i < AT_TWSI_EEPROM_TIMEOUT; i++) {
742b133ad6SJeff Kirsher msleep(10);
752b133ad6SJeff Kirsher twsi_ctrl_data = AT_READ_REG(hw, REG_TWSI_CTRL);
762b133ad6SJeff Kirsher if ((twsi_ctrl_data & TWSI_CTRL_SW_LDSTART) == 0)
772b133ad6SJeff Kirsher break;
782b133ad6SJeff Kirsher }
792b133ad6SJeff Kirsher if (i >= AT_TWSI_EEPROM_TIMEOUT)
802b133ad6SJeff Kirsher return AT_ERR_TIMEOUT;
812b133ad6SJeff Kirsher }
822b133ad6SJeff Kirsher
832b133ad6SJeff Kirsher /* maybe MAC-address is from BIOS */
842b133ad6SJeff Kirsher addr[0] = AT_READ_REG(hw, REG_MAC_STA_ADDR);
852b133ad6SJeff Kirsher addr[1] = AT_READ_REG(hw, REG_MAC_STA_ADDR + 4);
862b133ad6SJeff Kirsher *(u32 *) ð_addr[2] = swab32(addr[0]);
872b133ad6SJeff Kirsher *(u16 *) ð_addr[0] = swab16(*(u16 *)&addr[1]);
882b133ad6SJeff Kirsher
892b133ad6SJeff Kirsher if (is_valid_ether_addr(eth_addr)) {
902b133ad6SJeff Kirsher memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
912b133ad6SJeff Kirsher return 0;
922b133ad6SJeff Kirsher }
932b133ad6SJeff Kirsher
942b133ad6SJeff Kirsher return AT_ERR_EEPROM;
952b133ad6SJeff Kirsher }
962b133ad6SJeff Kirsher
atl1e_write_eeprom(struct atl1e_hw * hw,u32 offset,u32 value)972b133ad6SJeff Kirsher bool atl1e_write_eeprom(struct atl1e_hw *hw, u32 offset, u32 value)
982b133ad6SJeff Kirsher {
992b133ad6SJeff Kirsher return true;
1002b133ad6SJeff Kirsher }
1012b133ad6SJeff Kirsher
atl1e_read_eeprom(struct atl1e_hw * hw,u32 offset,u32 * p_value)1022b133ad6SJeff Kirsher bool atl1e_read_eeprom(struct atl1e_hw *hw, u32 offset, u32 *p_value)
1032b133ad6SJeff Kirsher {
1042b133ad6SJeff Kirsher int i;
1052b133ad6SJeff Kirsher u32 control;
1062b133ad6SJeff Kirsher
1072b133ad6SJeff Kirsher if (offset & 3)
1082b133ad6SJeff Kirsher return false; /* address do not align */
1092b133ad6SJeff Kirsher
1102b133ad6SJeff Kirsher AT_WRITE_REG(hw, REG_VPD_DATA, 0);
1112b133ad6SJeff Kirsher control = (offset & VPD_CAP_VPD_ADDR_MASK) << VPD_CAP_VPD_ADDR_SHIFT;
1122b133ad6SJeff Kirsher AT_WRITE_REG(hw, REG_VPD_CAP, control);
1132b133ad6SJeff Kirsher
1142b133ad6SJeff Kirsher for (i = 0; i < 10; i++) {
1152b133ad6SJeff Kirsher msleep(2);
1162b133ad6SJeff Kirsher control = AT_READ_REG(hw, REG_VPD_CAP);
1172b133ad6SJeff Kirsher if (control & VPD_CAP_VPD_FLAG)
1182b133ad6SJeff Kirsher break;
1192b133ad6SJeff Kirsher }
1202b133ad6SJeff Kirsher if (control & VPD_CAP_VPD_FLAG) {
1212b133ad6SJeff Kirsher *p_value = AT_READ_REG(hw, REG_VPD_DATA);
1222b133ad6SJeff Kirsher return true;
1232b133ad6SJeff Kirsher }
1242b133ad6SJeff Kirsher return false; /* timeout */
1252b133ad6SJeff Kirsher }
1262b133ad6SJeff Kirsher
atl1e_force_ps(struct atl1e_hw * hw)1272b133ad6SJeff Kirsher void atl1e_force_ps(struct atl1e_hw *hw)
1282b133ad6SJeff Kirsher {
1292b133ad6SJeff Kirsher AT_WRITE_REGW(hw, REG_GPHY_CTRL,
1302b133ad6SJeff Kirsher GPHY_CTRL_PW_WOL_DIS | GPHY_CTRL_EXT_RESET);
1312b133ad6SJeff Kirsher }
1322b133ad6SJeff Kirsher
1332b133ad6SJeff Kirsher /*
1342b133ad6SJeff Kirsher * Reads the adapter's MAC address from the EEPROM
1352b133ad6SJeff Kirsher *
1362b133ad6SJeff Kirsher * hw - Struct containing variables accessed by shared code
1372b133ad6SJeff Kirsher */
atl1e_read_mac_addr(struct atl1e_hw * hw)1382b133ad6SJeff Kirsher int atl1e_read_mac_addr(struct atl1e_hw *hw)
1392b133ad6SJeff Kirsher {
1402b133ad6SJeff Kirsher int err = 0;
1412b133ad6SJeff Kirsher
1422b133ad6SJeff Kirsher err = atl1e_get_permanent_address(hw);
1432b133ad6SJeff Kirsher if (err)
1442b133ad6SJeff Kirsher return AT_ERR_EEPROM;
1452b133ad6SJeff Kirsher memcpy(hw->mac_addr, hw->perm_mac_addr, sizeof(hw->perm_mac_addr));
1462b133ad6SJeff Kirsher return 0;
1472b133ad6SJeff Kirsher }
1482b133ad6SJeff Kirsher
1492b133ad6SJeff Kirsher /*
1502b133ad6SJeff Kirsher * atl1e_hash_mc_addr
1512b133ad6SJeff Kirsher * purpose
1522b133ad6SJeff Kirsher * set hash value for a multicast address
1532b133ad6SJeff Kirsher */
atl1e_hash_mc_addr(struct atl1e_hw * hw,u8 * mc_addr)1542b133ad6SJeff Kirsher u32 atl1e_hash_mc_addr(struct atl1e_hw *hw, u8 *mc_addr)
1552b133ad6SJeff Kirsher {
1562b133ad6SJeff Kirsher u32 crc32;
1572b133ad6SJeff Kirsher u32 value = 0;
1582b133ad6SJeff Kirsher int i;
1592b133ad6SJeff Kirsher
1602b133ad6SJeff Kirsher crc32 = ether_crc_le(6, mc_addr);
1612b133ad6SJeff Kirsher for (i = 0; i < 32; i++)
1622b133ad6SJeff Kirsher value |= (((crc32 >> i) & 1) << (31 - i));
1632b133ad6SJeff Kirsher
1642b133ad6SJeff Kirsher return value;
1652b133ad6SJeff Kirsher }
1662b133ad6SJeff Kirsher
1672b133ad6SJeff Kirsher /*
1682b133ad6SJeff Kirsher * Sets the bit in the multicast table corresponding to the hash value.
1692b133ad6SJeff Kirsher * hw - Struct containing variables accessed by shared code
1702b133ad6SJeff Kirsher * hash_value - Multicast address hash value
1712b133ad6SJeff Kirsher */
atl1e_hash_set(struct atl1e_hw * hw,u32 hash_value)1722b133ad6SJeff Kirsher void atl1e_hash_set(struct atl1e_hw *hw, u32 hash_value)
1732b133ad6SJeff Kirsher {
1742b133ad6SJeff Kirsher u32 hash_bit, hash_reg;
1752b133ad6SJeff Kirsher u32 mta;
1762b133ad6SJeff Kirsher
1772b133ad6SJeff Kirsher /*
1782b133ad6SJeff Kirsher * The HASH Table is a register array of 2 32-bit registers.
1792b133ad6SJeff Kirsher * It is treated like an array of 64 bits. We want to set
1802b133ad6SJeff Kirsher * bit BitArray[hash_value]. So we figure out what register
1812b133ad6SJeff Kirsher * the bit is in, read it, OR in the new bit, then write
1822b133ad6SJeff Kirsher * back the new value. The register is determined by the
1832b133ad6SJeff Kirsher * upper 7 bits of the hash value and the bit within that
1842b133ad6SJeff Kirsher * register are determined by the lower 5 bits of the value.
1852b133ad6SJeff Kirsher */
1862b133ad6SJeff Kirsher hash_reg = (hash_value >> 31) & 0x1;
1872b133ad6SJeff Kirsher hash_bit = (hash_value >> 26) & 0x1F;
1882b133ad6SJeff Kirsher
1892b133ad6SJeff Kirsher mta = AT_READ_REG_ARRAY(hw, REG_RX_HASH_TABLE, hash_reg);
1902b133ad6SJeff Kirsher
1912b133ad6SJeff Kirsher mta |= (1 << hash_bit);
1922b133ad6SJeff Kirsher
1932b133ad6SJeff Kirsher AT_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, hash_reg, mta);
1942b133ad6SJeff Kirsher }
1952b133ad6SJeff Kirsher /*
1962b133ad6SJeff Kirsher * Reads the value from a PHY register
1972b133ad6SJeff Kirsher * hw - Struct containing variables accessed by shared code
1982b133ad6SJeff Kirsher * reg_addr - address of the PHY register to read
1992b133ad6SJeff Kirsher */
atl1e_read_phy_reg(struct atl1e_hw * hw,u16 reg_addr,u16 * phy_data)2002b133ad6SJeff Kirsher int atl1e_read_phy_reg(struct atl1e_hw *hw, u16 reg_addr, u16 *phy_data)
2012b133ad6SJeff Kirsher {
2022b133ad6SJeff Kirsher u32 val;
2032b133ad6SJeff Kirsher int i;
2042b133ad6SJeff Kirsher
2052b133ad6SJeff Kirsher val = ((u32)(reg_addr & MDIO_REG_ADDR_MASK)) << MDIO_REG_ADDR_SHIFT |
2062b133ad6SJeff Kirsher MDIO_START | MDIO_SUP_PREAMBLE | MDIO_RW |
2072b133ad6SJeff Kirsher MDIO_CLK_25_4 << MDIO_CLK_SEL_SHIFT;
2082b133ad6SJeff Kirsher
2092b133ad6SJeff Kirsher AT_WRITE_REG(hw, REG_MDIO_CTRL, val);
2102b133ad6SJeff Kirsher
2112b133ad6SJeff Kirsher wmb();
2122b133ad6SJeff Kirsher
2132b133ad6SJeff Kirsher for (i = 0; i < MDIO_WAIT_TIMES; i++) {
2142b133ad6SJeff Kirsher udelay(2);
2152b133ad6SJeff Kirsher val = AT_READ_REG(hw, REG_MDIO_CTRL);
2162b133ad6SJeff Kirsher if (!(val & (MDIO_START | MDIO_BUSY)))
2172b133ad6SJeff Kirsher break;
2182b133ad6SJeff Kirsher wmb();
2192b133ad6SJeff Kirsher }
2202b133ad6SJeff Kirsher if (!(val & (MDIO_START | MDIO_BUSY))) {
2212b133ad6SJeff Kirsher *phy_data = (u16)val;
2222b133ad6SJeff Kirsher return 0;
2232b133ad6SJeff Kirsher }
2242b133ad6SJeff Kirsher
2252b133ad6SJeff Kirsher return AT_ERR_PHY;
2262b133ad6SJeff Kirsher }
2272b133ad6SJeff Kirsher
2282b133ad6SJeff Kirsher /*
2292b133ad6SJeff Kirsher * Writes a value to a PHY register
2302b133ad6SJeff Kirsher * hw - Struct containing variables accessed by shared code
2312b133ad6SJeff Kirsher * reg_addr - address of the PHY register to write
2322b133ad6SJeff Kirsher * data - data to write to the PHY
2332b133ad6SJeff Kirsher */
atl1e_write_phy_reg(struct atl1e_hw * hw,u32 reg_addr,u16 phy_data)2342b133ad6SJeff Kirsher int atl1e_write_phy_reg(struct atl1e_hw *hw, u32 reg_addr, u16 phy_data)
2352b133ad6SJeff Kirsher {
2362b133ad6SJeff Kirsher int i;
2372b133ad6SJeff Kirsher u32 val;
2382b133ad6SJeff Kirsher
2392b133ad6SJeff Kirsher val = ((u32)(phy_data & MDIO_DATA_MASK)) << MDIO_DATA_SHIFT |
2402b133ad6SJeff Kirsher (reg_addr&MDIO_REG_ADDR_MASK) << MDIO_REG_ADDR_SHIFT |
2412b133ad6SJeff Kirsher MDIO_SUP_PREAMBLE |
2422b133ad6SJeff Kirsher MDIO_START |
2432b133ad6SJeff Kirsher MDIO_CLK_25_4 << MDIO_CLK_SEL_SHIFT;
2442b133ad6SJeff Kirsher
2452b133ad6SJeff Kirsher AT_WRITE_REG(hw, REG_MDIO_CTRL, val);
2462b133ad6SJeff Kirsher wmb();
2472b133ad6SJeff Kirsher
2482b133ad6SJeff Kirsher for (i = 0; i < MDIO_WAIT_TIMES; i++) {
2492b133ad6SJeff Kirsher udelay(2);
2502b133ad6SJeff Kirsher val = AT_READ_REG(hw, REG_MDIO_CTRL);
2512b133ad6SJeff Kirsher if (!(val & (MDIO_START | MDIO_BUSY)))
2522b133ad6SJeff Kirsher break;
2532b133ad6SJeff Kirsher wmb();
2542b133ad6SJeff Kirsher }
2552b133ad6SJeff Kirsher
2562b133ad6SJeff Kirsher if (!(val & (MDIO_START | MDIO_BUSY)))
2572b133ad6SJeff Kirsher return 0;
2582b133ad6SJeff Kirsher
2592b133ad6SJeff Kirsher return AT_ERR_PHY;
2602b133ad6SJeff Kirsher }
2612b133ad6SJeff Kirsher
2622b133ad6SJeff Kirsher /*
2632b133ad6SJeff Kirsher * atl1e_init_pcie - init PCIE module
2642b133ad6SJeff Kirsher */
atl1e_init_pcie(struct atl1e_hw * hw)2652b133ad6SJeff Kirsher static void atl1e_init_pcie(struct atl1e_hw *hw)
2662b133ad6SJeff Kirsher {
2672b133ad6SJeff Kirsher u32 value;
2682b133ad6SJeff Kirsher /* comment 2lines below to save more power when sususpend
2692b133ad6SJeff Kirsher value = LTSSM_TEST_MODE_DEF;
2702b133ad6SJeff Kirsher AT_WRITE_REG(hw, REG_LTSSM_TEST_MODE, value);
2712b133ad6SJeff Kirsher */
2722b133ad6SJeff Kirsher
2732b133ad6SJeff Kirsher /* pcie flow control mode change */
2742b133ad6SJeff Kirsher value = AT_READ_REG(hw, 0x1008);
2752b133ad6SJeff Kirsher value |= 0x8000;
2762b133ad6SJeff Kirsher AT_WRITE_REG(hw, 0x1008, value);
2772b133ad6SJeff Kirsher }
2782b133ad6SJeff Kirsher /*
2792b133ad6SJeff Kirsher * Configures PHY autoneg and flow control advertisement settings
2802b133ad6SJeff Kirsher *
2812b133ad6SJeff Kirsher * hw - Struct containing variables accessed by shared code
2822b133ad6SJeff Kirsher */
atl1e_phy_setup_autoneg_adv(struct atl1e_hw * hw)2832b133ad6SJeff Kirsher static int atl1e_phy_setup_autoneg_adv(struct atl1e_hw *hw)
2842b133ad6SJeff Kirsher {
2852b133ad6SJeff Kirsher s32 ret_val;
2862b133ad6SJeff Kirsher u16 mii_autoneg_adv_reg;
2872b133ad6SJeff Kirsher u16 mii_1000t_ctrl_reg;
2882b133ad6SJeff Kirsher
2892b133ad6SJeff Kirsher if (0 != hw->mii_autoneg_adv_reg)
2902b133ad6SJeff Kirsher return 0;
2912b133ad6SJeff Kirsher /* Read the MII Auto-Neg Advertisement Register (Address 4/9). */
2922b133ad6SJeff Kirsher mii_autoneg_adv_reg = MII_AR_DEFAULT_CAP_MASK;
2932b133ad6SJeff Kirsher mii_1000t_ctrl_reg = MII_AT001_CR_1000T_DEFAULT_CAP_MASK;
2942b133ad6SJeff Kirsher
2952b133ad6SJeff Kirsher /*
2962b133ad6SJeff Kirsher * Need to parse autoneg_advertised and set up
2972b133ad6SJeff Kirsher * the appropriate PHY registers. First we will parse for
2982b133ad6SJeff Kirsher * autoneg_advertised software override. Since we can advertise
2992b133ad6SJeff Kirsher * a plethora of combinations, we need to check each bit
3002b133ad6SJeff Kirsher * individually.
3012b133ad6SJeff Kirsher */
3022b133ad6SJeff Kirsher
3032b133ad6SJeff Kirsher /*
3042b133ad6SJeff Kirsher * First we clear all the 10/100 mb speed bits in the Auto-Neg
3052b133ad6SJeff Kirsher * Advertisement Register (Address 4) and the 1000 mb speed bits in
3062b133ad6SJeff Kirsher * the 1000Base-T control Register (Address 9).
3072b133ad6SJeff Kirsher */
3082b133ad6SJeff Kirsher mii_autoneg_adv_reg &= ~ADVERTISE_ALL;
3092b133ad6SJeff Kirsher mii_1000t_ctrl_reg &= ~MII_AT001_CR_1000T_SPEED_MASK;
3102b133ad6SJeff Kirsher
3112b133ad6SJeff Kirsher /*
3122b133ad6SJeff Kirsher * Need to parse MediaType and setup the
3132b133ad6SJeff Kirsher * appropriate PHY registers.
3142b133ad6SJeff Kirsher */
3152b133ad6SJeff Kirsher switch (hw->media_type) {
3162b133ad6SJeff Kirsher case MEDIA_TYPE_AUTO_SENSOR:
3172b133ad6SJeff Kirsher mii_autoneg_adv_reg |= ADVERTISE_ALL;
3182b133ad6SJeff Kirsher hw->autoneg_advertised = ADVERTISE_ALL;
3192b133ad6SJeff Kirsher if (hw->nic_type == athr_l1e) {
3202b133ad6SJeff Kirsher mii_1000t_ctrl_reg |= ADVERTISE_1000FULL;
3212b133ad6SJeff Kirsher hw->autoneg_advertised |= ADVERTISE_1000_FULL;
3222b133ad6SJeff Kirsher }
3232b133ad6SJeff Kirsher break;
3242b133ad6SJeff Kirsher
3252b133ad6SJeff Kirsher case MEDIA_TYPE_100M_FULL:
3262b133ad6SJeff Kirsher mii_autoneg_adv_reg |= ADVERTISE_100FULL;
3272b133ad6SJeff Kirsher hw->autoneg_advertised = ADVERTISE_100_FULL;
3282b133ad6SJeff Kirsher break;
3292b133ad6SJeff Kirsher
3302b133ad6SJeff Kirsher case MEDIA_TYPE_100M_HALF:
3312b133ad6SJeff Kirsher mii_autoneg_adv_reg |= ADVERTISE_100_HALF;
3322b133ad6SJeff Kirsher hw->autoneg_advertised = ADVERTISE_100_HALF;
3332b133ad6SJeff Kirsher break;
3342b133ad6SJeff Kirsher
3352b133ad6SJeff Kirsher case MEDIA_TYPE_10M_FULL:
3362b133ad6SJeff Kirsher mii_autoneg_adv_reg |= ADVERTISE_10_FULL;
3372b133ad6SJeff Kirsher hw->autoneg_advertised = ADVERTISE_10_FULL;
3382b133ad6SJeff Kirsher break;
3392b133ad6SJeff Kirsher
3402b133ad6SJeff Kirsher default:
3412b133ad6SJeff Kirsher mii_autoneg_adv_reg |= ADVERTISE_10_HALF;
3422b133ad6SJeff Kirsher hw->autoneg_advertised = ADVERTISE_10_HALF;
3432b133ad6SJeff Kirsher break;
3442b133ad6SJeff Kirsher }
3452b133ad6SJeff Kirsher
3462b133ad6SJeff Kirsher /* flow control fixed to enable all */
3472b133ad6SJeff Kirsher mii_autoneg_adv_reg |= (ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP);
3482b133ad6SJeff Kirsher
3492b133ad6SJeff Kirsher hw->mii_autoneg_adv_reg = mii_autoneg_adv_reg;
3502b133ad6SJeff Kirsher hw->mii_1000t_ctrl_reg = mii_1000t_ctrl_reg;
3512b133ad6SJeff Kirsher
3522b133ad6SJeff Kirsher ret_val = atl1e_write_phy_reg(hw, MII_ADVERTISE, mii_autoneg_adv_reg);
3532b133ad6SJeff Kirsher if (ret_val)
3542b133ad6SJeff Kirsher return ret_val;
3552b133ad6SJeff Kirsher
3562b133ad6SJeff Kirsher if (hw->nic_type == athr_l1e || hw->nic_type == athr_l2e_revA) {
3572b133ad6SJeff Kirsher ret_val = atl1e_write_phy_reg(hw, MII_CTRL1000,
3582b133ad6SJeff Kirsher mii_1000t_ctrl_reg);
3592b133ad6SJeff Kirsher if (ret_val)
3602b133ad6SJeff Kirsher return ret_val;
3612b133ad6SJeff Kirsher }
3622b133ad6SJeff Kirsher
3632b133ad6SJeff Kirsher return 0;
3642b133ad6SJeff Kirsher }
3652b133ad6SJeff Kirsher
3662b133ad6SJeff Kirsher
3672b133ad6SJeff Kirsher /*
3682b133ad6SJeff Kirsher * Resets the PHY and make all config validate
3692b133ad6SJeff Kirsher *
3702b133ad6SJeff Kirsher * hw - Struct containing variables accessed by shared code
3712b133ad6SJeff Kirsher *
3722b133ad6SJeff Kirsher * Sets bit 15 and 12 of the MII control regiser (for F001 bug)
3732b133ad6SJeff Kirsher */
atl1e_phy_commit(struct atl1e_hw * hw)3742b133ad6SJeff Kirsher int atl1e_phy_commit(struct atl1e_hw *hw)
3752b133ad6SJeff Kirsher {
3762b133ad6SJeff Kirsher struct atl1e_adapter *adapter = hw->adapter;
3772b133ad6SJeff Kirsher int ret_val;
3782b133ad6SJeff Kirsher u16 phy_data;
3792b133ad6SJeff Kirsher
3802b133ad6SJeff Kirsher phy_data = BMCR_RESET | BMCR_ANENABLE | BMCR_ANRESTART;
3812b133ad6SJeff Kirsher
3822b133ad6SJeff Kirsher ret_val = atl1e_write_phy_reg(hw, MII_BMCR, phy_data);
3832b133ad6SJeff Kirsher if (ret_val) {
3842b133ad6SJeff Kirsher u32 val;
3852b133ad6SJeff Kirsher int i;
3862b133ad6SJeff Kirsher /**************************************
3872b133ad6SJeff Kirsher * pcie serdes link may be down !
3882b133ad6SJeff Kirsher **************************************/
3892b133ad6SJeff Kirsher for (i = 0; i < 25; i++) {
3902b133ad6SJeff Kirsher msleep(1);
3912b133ad6SJeff Kirsher val = AT_READ_REG(hw, REG_MDIO_CTRL);
3922b133ad6SJeff Kirsher if (!(val & (MDIO_START | MDIO_BUSY)))
3932b133ad6SJeff Kirsher break;
3942b133ad6SJeff Kirsher }
3952b133ad6SJeff Kirsher
3962b133ad6SJeff Kirsher if (0 != (val & (MDIO_START | MDIO_BUSY))) {
3972b133ad6SJeff Kirsher netdev_err(adapter->netdev,
3982b133ad6SJeff Kirsher "pcie linkdown at least for 25ms\n");
3992b133ad6SJeff Kirsher return ret_val;
4002b133ad6SJeff Kirsher }
4012b133ad6SJeff Kirsher
4022b133ad6SJeff Kirsher netdev_err(adapter->netdev, "pcie linkup after %d ms\n", i);
4032b133ad6SJeff Kirsher }
4042b133ad6SJeff Kirsher return 0;
4052b133ad6SJeff Kirsher }
4062b133ad6SJeff Kirsher
atl1e_phy_init(struct atl1e_hw * hw)4072b133ad6SJeff Kirsher int atl1e_phy_init(struct atl1e_hw *hw)
4082b133ad6SJeff Kirsher {
4092b133ad6SJeff Kirsher struct atl1e_adapter *adapter = hw->adapter;
4102b133ad6SJeff Kirsher s32 ret_val;
4112b133ad6SJeff Kirsher u16 phy_val;
4122b133ad6SJeff Kirsher
4132b133ad6SJeff Kirsher if (hw->phy_configured) {
4142b133ad6SJeff Kirsher if (hw->re_autoneg) {
4152b133ad6SJeff Kirsher hw->re_autoneg = false;
4162b133ad6SJeff Kirsher return atl1e_restart_autoneg(hw);
4172b133ad6SJeff Kirsher }
4182b133ad6SJeff Kirsher return 0;
4192b133ad6SJeff Kirsher }
4202b133ad6SJeff Kirsher
4212b133ad6SJeff Kirsher /* RESET GPHY Core */
4222b133ad6SJeff Kirsher AT_WRITE_REGW(hw, REG_GPHY_CTRL, GPHY_CTRL_DEFAULT);
4232b133ad6SJeff Kirsher msleep(2);
4242b133ad6SJeff Kirsher AT_WRITE_REGW(hw, REG_GPHY_CTRL, GPHY_CTRL_DEFAULT |
4252b133ad6SJeff Kirsher GPHY_CTRL_EXT_RESET);
4262b133ad6SJeff Kirsher msleep(2);
4272b133ad6SJeff Kirsher
4282b133ad6SJeff Kirsher /* patches */
4292b133ad6SJeff Kirsher /* p1. eable hibernation mode */
4302b133ad6SJeff Kirsher ret_val = atl1e_write_phy_reg(hw, MII_DBG_ADDR, 0xB);
4312b133ad6SJeff Kirsher if (ret_val)
4322b133ad6SJeff Kirsher return ret_val;
4332b133ad6SJeff Kirsher ret_val = atl1e_write_phy_reg(hw, MII_DBG_DATA, 0xBC00);
4342b133ad6SJeff Kirsher if (ret_val)
4352b133ad6SJeff Kirsher return ret_val;
4362b133ad6SJeff Kirsher /* p2. set Class A/B for all modes */
4372b133ad6SJeff Kirsher ret_val = atl1e_write_phy_reg(hw, MII_DBG_ADDR, 0);
4382b133ad6SJeff Kirsher if (ret_val)
4392b133ad6SJeff Kirsher return ret_val;
4402b133ad6SJeff Kirsher phy_val = 0x02ef;
4412b133ad6SJeff Kirsher /* remove Class AB */
4422b133ad6SJeff Kirsher /* phy_val = hw->emi_ca ? 0x02ef : 0x02df; */
4432b133ad6SJeff Kirsher ret_val = atl1e_write_phy_reg(hw, MII_DBG_DATA, phy_val);
4442b133ad6SJeff Kirsher if (ret_val)
4452b133ad6SJeff Kirsher return ret_val;
4462b133ad6SJeff Kirsher /* p3. 10B ??? */
4472b133ad6SJeff Kirsher ret_val = atl1e_write_phy_reg(hw, MII_DBG_ADDR, 0x12);
4482b133ad6SJeff Kirsher if (ret_val)
4492b133ad6SJeff Kirsher return ret_val;
4502b133ad6SJeff Kirsher ret_val = atl1e_write_phy_reg(hw, MII_DBG_DATA, 0x4C04);
4512b133ad6SJeff Kirsher if (ret_val)
4522b133ad6SJeff Kirsher return ret_val;
4532b133ad6SJeff Kirsher /* p4. 1000T power */
4542b133ad6SJeff Kirsher ret_val = atl1e_write_phy_reg(hw, MII_DBG_ADDR, 0x4);
4552b133ad6SJeff Kirsher if (ret_val)
4562b133ad6SJeff Kirsher return ret_val;
4572b133ad6SJeff Kirsher ret_val = atl1e_write_phy_reg(hw, MII_DBG_DATA, 0x8BBB);
4582b133ad6SJeff Kirsher if (ret_val)
4592b133ad6SJeff Kirsher return ret_val;
4602b133ad6SJeff Kirsher
4612b133ad6SJeff Kirsher ret_val = atl1e_write_phy_reg(hw, MII_DBG_ADDR, 0x5);
4622b133ad6SJeff Kirsher if (ret_val)
4632b133ad6SJeff Kirsher return ret_val;
4642b133ad6SJeff Kirsher ret_val = atl1e_write_phy_reg(hw, MII_DBG_DATA, 0x2C46);
4652b133ad6SJeff Kirsher if (ret_val)
4662b133ad6SJeff Kirsher return ret_val;
4672b133ad6SJeff Kirsher
4682b133ad6SJeff Kirsher msleep(1);
4692b133ad6SJeff Kirsher
4702b133ad6SJeff Kirsher /*Enable PHY LinkChange Interrupt */
4712b133ad6SJeff Kirsher ret_val = atl1e_write_phy_reg(hw, MII_INT_CTRL, 0xC00);
4722b133ad6SJeff Kirsher if (ret_val) {
4732b133ad6SJeff Kirsher netdev_err(adapter->netdev,
4742b133ad6SJeff Kirsher "Error enable PHY linkChange Interrupt\n");
4752b133ad6SJeff Kirsher return ret_val;
4762b133ad6SJeff Kirsher }
4772b133ad6SJeff Kirsher /* setup AutoNeg parameters */
4782b133ad6SJeff Kirsher ret_val = atl1e_phy_setup_autoneg_adv(hw);
4792b133ad6SJeff Kirsher if (ret_val) {
4802b133ad6SJeff Kirsher netdev_err(adapter->netdev,
4812b133ad6SJeff Kirsher "Error Setting up Auto-Negotiation\n");
4822b133ad6SJeff Kirsher return ret_val;
4832b133ad6SJeff Kirsher }
4842b133ad6SJeff Kirsher /* SW.Reset & En-Auto-Neg to restart Auto-Neg*/
4852b133ad6SJeff Kirsher netdev_dbg(adapter->netdev, "Restarting Auto-Negotiation\n");
4862b133ad6SJeff Kirsher ret_val = atl1e_phy_commit(hw);
4872b133ad6SJeff Kirsher if (ret_val) {
4882b133ad6SJeff Kirsher netdev_err(adapter->netdev, "Error resetting the phy\n");
4892b133ad6SJeff Kirsher return ret_val;
4902b133ad6SJeff Kirsher }
4912b133ad6SJeff Kirsher
4922b133ad6SJeff Kirsher hw->phy_configured = true;
4932b133ad6SJeff Kirsher
4942b133ad6SJeff Kirsher return 0;
4952b133ad6SJeff Kirsher }
4962b133ad6SJeff Kirsher
4972b133ad6SJeff Kirsher /*
4982b133ad6SJeff Kirsher * Reset the transmit and receive units; mask and clear all interrupts.
4992b133ad6SJeff Kirsher * hw - Struct containing variables accessed by shared code
5002b133ad6SJeff Kirsher * return : 0 or idle status (if error)
5012b133ad6SJeff Kirsher */
atl1e_reset_hw(struct atl1e_hw * hw)5022b133ad6SJeff Kirsher int atl1e_reset_hw(struct atl1e_hw *hw)
5032b133ad6SJeff Kirsher {
5042b133ad6SJeff Kirsher struct atl1e_adapter *adapter = hw->adapter;
5052b133ad6SJeff Kirsher struct pci_dev *pdev = adapter->pdev;
5062b133ad6SJeff Kirsher
5072b133ad6SJeff Kirsher u32 idle_status_data = 0;
5082b133ad6SJeff Kirsher u16 pci_cfg_cmd_word = 0;
5092b133ad6SJeff Kirsher int timeout = 0;
5102b133ad6SJeff Kirsher
5112b133ad6SJeff Kirsher /* Workaround for PCI problem when BIOS sets MMRBC incorrectly. */
5122b133ad6SJeff Kirsher pci_read_config_word(pdev, PCI_REG_COMMAND, &pci_cfg_cmd_word);
5132b133ad6SJeff Kirsher if ((pci_cfg_cmd_word & (CMD_IO_SPACE |
5142b133ad6SJeff Kirsher CMD_MEMORY_SPACE | CMD_BUS_MASTER))
5152b133ad6SJeff Kirsher != (CMD_IO_SPACE | CMD_MEMORY_SPACE | CMD_BUS_MASTER)) {
5162b133ad6SJeff Kirsher pci_cfg_cmd_word |= (CMD_IO_SPACE |
5172b133ad6SJeff Kirsher CMD_MEMORY_SPACE | CMD_BUS_MASTER);
5182b133ad6SJeff Kirsher pci_write_config_word(pdev, PCI_REG_COMMAND, pci_cfg_cmd_word);
5192b133ad6SJeff Kirsher }
5202b133ad6SJeff Kirsher
5212b133ad6SJeff Kirsher /*
5222b133ad6SJeff Kirsher * Issue Soft Reset to the MAC. This will reset the chip's
5232b133ad6SJeff Kirsher * transmit, receive, DMA. It will not effect
5242b133ad6SJeff Kirsher * the current PCI configuration. The global reset bit is self-
5252b133ad6SJeff Kirsher * clearing, and should clear within a microsecond.
5262b133ad6SJeff Kirsher */
5272b133ad6SJeff Kirsher AT_WRITE_REG(hw, REG_MASTER_CTRL,
5282b133ad6SJeff Kirsher MASTER_CTRL_LED_MODE | MASTER_CTRL_SOFT_RST);
5292b133ad6SJeff Kirsher wmb();
5302b133ad6SJeff Kirsher msleep(1);
5312b133ad6SJeff Kirsher
5322b133ad6SJeff Kirsher /* Wait at least 10ms for All module to be Idle */
5332b133ad6SJeff Kirsher for (timeout = 0; timeout < AT_HW_MAX_IDLE_DELAY; timeout++) {
5342b133ad6SJeff Kirsher idle_status_data = AT_READ_REG(hw, REG_IDLE_STATUS);
5352b133ad6SJeff Kirsher if (idle_status_data == 0)
5362b133ad6SJeff Kirsher break;
5372b133ad6SJeff Kirsher msleep(1);
5382b133ad6SJeff Kirsher cpu_relax();
5392b133ad6SJeff Kirsher }
5402b133ad6SJeff Kirsher
5412b133ad6SJeff Kirsher if (timeout >= AT_HW_MAX_IDLE_DELAY) {
5422b133ad6SJeff Kirsher netdev_err(adapter->netdev,
5432b133ad6SJeff Kirsher "MAC state machine can't be idle since disabled for 10ms second\n");
5442b133ad6SJeff Kirsher return AT_ERR_TIMEOUT;
5452b133ad6SJeff Kirsher }
5462b133ad6SJeff Kirsher
5472b133ad6SJeff Kirsher return 0;
5482b133ad6SJeff Kirsher }
5492b133ad6SJeff Kirsher
5502b133ad6SJeff Kirsher
5512b133ad6SJeff Kirsher /*
5522b133ad6SJeff Kirsher * Performs basic configuration of the adapter.
5532b133ad6SJeff Kirsher *
5542b133ad6SJeff Kirsher * hw - Struct containing variables accessed by shared code
5552b133ad6SJeff Kirsher * Assumes that the controller has previously been reset and is in a
5562b133ad6SJeff Kirsher * post-reset uninitialized state. Initializes multicast table,
5572b133ad6SJeff Kirsher * and Calls routines to setup link
5582b133ad6SJeff Kirsher * Leaves the transmit and receive units disabled and uninitialized.
5592b133ad6SJeff Kirsher */
atl1e_init_hw(struct atl1e_hw * hw)5602b133ad6SJeff Kirsher int atl1e_init_hw(struct atl1e_hw *hw)
5612b133ad6SJeff Kirsher {
5622b133ad6SJeff Kirsher s32 ret_val = 0;
5632b133ad6SJeff Kirsher
5642b133ad6SJeff Kirsher atl1e_init_pcie(hw);
5652b133ad6SJeff Kirsher
5662b133ad6SJeff Kirsher /* Zero out the Multicast HASH table */
5672b133ad6SJeff Kirsher /* clear the old settings from the multicast hash table */
5682b133ad6SJeff Kirsher AT_WRITE_REG(hw, REG_RX_HASH_TABLE, 0);
5692b133ad6SJeff Kirsher AT_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, 1, 0);
5702b133ad6SJeff Kirsher
5712b133ad6SJeff Kirsher ret_val = atl1e_phy_init(hw);
5722b133ad6SJeff Kirsher
5732b133ad6SJeff Kirsher return ret_val;
5742b133ad6SJeff Kirsher }
5752b133ad6SJeff Kirsher
5762b133ad6SJeff Kirsher /*
5772b133ad6SJeff Kirsher * Detects the current speed and duplex settings of the hardware.
5782b133ad6SJeff Kirsher *
5792b133ad6SJeff Kirsher * hw - Struct containing variables accessed by shared code
5802b133ad6SJeff Kirsher * speed - Speed of the connection
5812b133ad6SJeff Kirsher * duplex - Duplex setting of the connection
5822b133ad6SJeff Kirsher */
atl1e_get_speed_and_duplex(struct atl1e_hw * hw,u16 * speed,u16 * duplex)5832b133ad6SJeff Kirsher int atl1e_get_speed_and_duplex(struct atl1e_hw *hw, u16 *speed, u16 *duplex)
5842b133ad6SJeff Kirsher {
5852b133ad6SJeff Kirsher int err;
5862b133ad6SJeff Kirsher u16 phy_data;
5872b133ad6SJeff Kirsher
5882b133ad6SJeff Kirsher /* Read PHY Specific Status Register (17) */
5892b133ad6SJeff Kirsher err = atl1e_read_phy_reg(hw, MII_AT001_PSSR, &phy_data);
5902b133ad6SJeff Kirsher if (err)
5912b133ad6SJeff Kirsher return err;
5922b133ad6SJeff Kirsher
5932b133ad6SJeff Kirsher if (!(phy_data & MII_AT001_PSSR_SPD_DPLX_RESOLVED))
5942b133ad6SJeff Kirsher return AT_ERR_PHY_RES;
5952b133ad6SJeff Kirsher
5962b133ad6SJeff Kirsher switch (phy_data & MII_AT001_PSSR_SPEED) {
5972b133ad6SJeff Kirsher case MII_AT001_PSSR_1000MBS:
5982b133ad6SJeff Kirsher *speed = SPEED_1000;
5992b133ad6SJeff Kirsher break;
6002b133ad6SJeff Kirsher case MII_AT001_PSSR_100MBS:
6012b133ad6SJeff Kirsher *speed = SPEED_100;
6022b133ad6SJeff Kirsher break;
6032b133ad6SJeff Kirsher case MII_AT001_PSSR_10MBS:
6042b133ad6SJeff Kirsher *speed = SPEED_10;
6052b133ad6SJeff Kirsher break;
6062b133ad6SJeff Kirsher default:
6072b133ad6SJeff Kirsher return AT_ERR_PHY_SPEED;
6082b133ad6SJeff Kirsher }
6092b133ad6SJeff Kirsher
6102b133ad6SJeff Kirsher if (phy_data & MII_AT001_PSSR_DPLX)
6112b133ad6SJeff Kirsher *duplex = FULL_DUPLEX;
6122b133ad6SJeff Kirsher else
6132b133ad6SJeff Kirsher *duplex = HALF_DUPLEX;
6142b133ad6SJeff Kirsher
6152b133ad6SJeff Kirsher return 0;
6162b133ad6SJeff Kirsher }
6172b133ad6SJeff Kirsher
atl1e_restart_autoneg(struct atl1e_hw * hw)6182b133ad6SJeff Kirsher int atl1e_restart_autoneg(struct atl1e_hw *hw)
6192b133ad6SJeff Kirsher {
6202b133ad6SJeff Kirsher int err = 0;
6212b133ad6SJeff Kirsher
6222b133ad6SJeff Kirsher err = atl1e_write_phy_reg(hw, MII_ADVERTISE, hw->mii_autoneg_adv_reg);
6232b133ad6SJeff Kirsher if (err)
6242b133ad6SJeff Kirsher return err;
6252b133ad6SJeff Kirsher
6262b133ad6SJeff Kirsher if (hw->nic_type == athr_l1e || hw->nic_type == athr_l2e_revA) {
6272b133ad6SJeff Kirsher err = atl1e_write_phy_reg(hw, MII_CTRL1000,
6282b133ad6SJeff Kirsher hw->mii_1000t_ctrl_reg);
6292b133ad6SJeff Kirsher if (err)
6302b133ad6SJeff Kirsher return err;
6312b133ad6SJeff Kirsher }
6322b133ad6SJeff Kirsher
6332b133ad6SJeff Kirsher err = atl1e_write_phy_reg(hw, MII_BMCR,
6342b133ad6SJeff Kirsher BMCR_RESET | BMCR_ANENABLE | BMCR_ANRESTART);
6352b133ad6SJeff Kirsher return err;
6362b133ad6SJeff Kirsher }
6372b133ad6SJeff Kirsher
638