1a2443fd1SAndrew Lunn // SPDX-License-Identifier: GPL-2.0+ 220b2af32SRussell King /* 320b2af32SRussell King * Marvell 10G 88x3310 PHY driver 420b2af32SRussell King * 520b2af32SRussell King * Based upon the ID registers, this PHY appears to be a mixture of IPs 620b2af32SRussell King * from two different companies. 720b2af32SRussell King * 820b2af32SRussell King * There appears to be several different data paths through the PHY which 920b2af32SRussell King * are automatically managed by the PHY. The following has been determined 1005ca1b32SRussell King * via observation and experimentation for a setup using single-lane Serdes: 1120b2af32SRussell King * 1220b2af32SRussell King * SGMII PHYXS -- BASE-T PCS -- 10G PMA -- AN -- Copper (for <= 1G) 1320b2af32SRussell King * 10GBASE-KR PHYXS -- BASE-T PCS -- 10G PMA -- AN -- Copper (for 10G) 1420b2af32SRussell King * 10GBASE-KR PHYXS -- BASE-R PCS -- Fiber 1520b2af32SRussell King * 1605ca1b32SRussell King * With XAUI, observation shows: 1705ca1b32SRussell King * 1805ca1b32SRussell King * XAUI PHYXS -- <appropriate PCS as above> 1905ca1b32SRussell King * 2005ca1b32SRussell King * and no switching of the host interface mode occurs. 2105ca1b32SRussell King * 2220b2af32SRussell King * If both the fiber and copper ports are connected, the first to gain 2320b2af32SRussell King * link takes priority and the other port is completely locked out. 2420b2af32SRussell King */ 254075a6a0SRussell King #include <linux/bitfield.h> 260d3ad854SRussell King #include <linux/ctype.h> 278d8963c3SRussell King #include <linux/delay.h> 280d3ad854SRussell King #include <linux/hwmon.h> 29952b6b3bSAntoine Tenart #include <linux/marvell_phy.h> 300d3ad854SRussell King #include <linux/phy.h> 3136023da1SRussell King #include <linux/sfp.h> 3208041a9aSVoon Weifeng #include <linux/netdevice.h> 3320b2af32SRussell King 34c47455f9SMaxime Chevallier #define MV_PHY_ALASKA_NBT_QUIRK_MASK 0xfffffffe 35c47455f9SMaxime Chevallier #define MV_PHY_ALASKA_NBT_QUIRK_REV (MARVELL_PHY_ID_88X3310 | 0xa) 36c47455f9SMaxime Chevallier 374075a6a0SRussell King #define MV_VERSION(a,b,c,d) ((a) << 24 | (b) << 16 | (c) << 8 | (d)) 384075a6a0SRussell King 3920b2af32SRussell King enum { 40dd649b4fSRussell King MV_PMA_FW_VER0 = 0xc011, 41dd649b4fSRussell King MV_PMA_FW_VER1 = 0xc012, 429ab0fbd0SMarek Behún MV_PMA_21X0_PORT_CTRL = 0xc04a, 439ab0fbd0SMarek Behún MV_PMA_21X0_PORT_CTRL_SWRST = BIT(15), 449ab0fbd0SMarek Behún MV_PMA_21X0_PORT_CTRL_MACTYPE_MASK = 0x7, 459ab0fbd0SMarek Behún MV_PMA_21X0_PORT_CTRL_MACTYPE_USXGMII = 0x0, 469ab0fbd0SMarek Behún MV_PMA_2180_PORT_CTRL_MACTYPE_DXGMII = 0x1, 479ab0fbd0SMarek Behún MV_PMA_2180_PORT_CTRL_MACTYPE_QXGMII = 0x2, 489ab0fbd0SMarek Behún MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER = 0x4, 499ab0fbd0SMarek Behún MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER_NO_SGMII_AN = 0x5, 509ab0fbd0SMarek Behún MV_PMA_21X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH = 0x6, 513d3ced2eSRussell King MV_PMA_BOOT = 0xc050, 523d3ced2eSRussell King MV_PMA_BOOT_FATAL = BIT(0), 533d3ced2eSRussell King 5420b2af32SRussell King MV_PCS_BASE_T = 0x0000, 5520b2af32SRussell King MV_PCS_BASE_R = 0x1000, 5620b2af32SRussell King MV_PCS_1000BASEX = 0x2000, 5720b2af32SRussell King 588d8963c3SRussell King MV_PCS_CSCR1 = 0x8000, 59a585c03eSRussell King MV_PCS_CSCR1_ED_MASK = 0x0300, 60a585c03eSRussell King MV_PCS_CSCR1_ED_OFF = 0x0000, 61a585c03eSRussell King MV_PCS_CSCR1_ED_RX = 0x0200, 62a585c03eSRussell King MV_PCS_CSCR1_ED_NLP = 0x0300, 638d8963c3SRussell King MV_PCS_CSCR1_MDIX_MASK = 0x0060, 648d8963c3SRussell King MV_PCS_CSCR1_MDIX_MDI = 0x0000, 658d8963c3SRussell King MV_PCS_CSCR1_MDIX_MDIX = 0x0020, 668d8963c3SRussell King MV_PCS_CSCR1_MDIX_AUTO = 0x0060, 678d8963c3SRussell King 684075a6a0SRussell King MV_PCS_DSC1 = 0x8003, 694075a6a0SRussell King MV_PCS_DSC1_ENABLE = BIT(9), 704075a6a0SRussell King MV_PCS_DSC1_10GBT = 0x01c0, 714075a6a0SRussell King MV_PCS_DSC1_1GBR = 0x0038, 724075a6a0SRussell King MV_PCS_DSC1_100BTX = 0x0007, 734075a6a0SRussell King MV_PCS_DSC2 = 0x8004, 744075a6a0SRussell King MV_PCS_DSC2_2P5G = 0xf000, 754075a6a0SRussell King MV_PCS_DSC2_5G = 0x0f00, 764075a6a0SRussell King 77c84786faSRussell King MV_PCS_CSSR1 = 0x8008, 78c84786faSRussell King MV_PCS_CSSR1_SPD1_MASK = 0xc000, 79c84786faSRussell King MV_PCS_CSSR1_SPD1_SPD2 = 0xc000, 80c84786faSRussell King MV_PCS_CSSR1_SPD1_1000 = 0x8000, 81c84786faSRussell King MV_PCS_CSSR1_SPD1_100 = 0x4000, 82c84786faSRussell King MV_PCS_CSSR1_SPD1_10 = 0x0000, 83c84786faSRussell King MV_PCS_CSSR1_DUPLEX_FULL= BIT(13), 84c84786faSRussell King MV_PCS_CSSR1_RESOLVED = BIT(11), 85c84786faSRussell King MV_PCS_CSSR1_MDIX = BIT(6), 86c84786faSRussell King MV_PCS_CSSR1_SPD2_MASK = 0x000c, 87c84786faSRussell King MV_PCS_CSSR1_SPD2_5000 = 0x0008, 88c84786faSRussell King MV_PCS_CSSR1_SPD2_2500 = 0x0004, 89c84786faSRussell King MV_PCS_CSSR1_SPD2_10000 = 0x0000, 90ea4efe25SRussell King 91c3e302edSBaruch Siach /* Temperature read register (88E2110 only) */ 92c3e302edSBaruch Siach MV_PCS_TEMP = 0x8042, 93c3e302edSBaruch Siach 94a5de4be0SMarek Behún /* Number of ports on the device */ 95a5de4be0SMarek Behún MV_PCS_PORT_INFO = 0xd00d, 96a5de4be0SMarek Behún MV_PCS_PORT_INFO_NPORTS_MASK = 0x0380, 97a5de4be0SMarek Behún MV_PCS_PORT_INFO_NPORTS_SHIFT = 7, 98a5de4be0SMarek Behún 99*d6d29292SRussell King /* SerDes reinitialization 88E21X0 */ 100*d6d29292SRussell King MV_AN_21X0_SERDES_CTRL2 = 0x800f, 101*d6d29292SRussell King MV_AN_21X0_SERDES_CTRL2_AUTO_INIT_DIS = BIT(13), 102*d6d29292SRussell King MV_AN_21X0_SERDES_CTRL2_RUN_INIT = BIT(15), 103*d6d29292SRussell King 10420b2af32SRussell King /* These registers appear at 0x800X and 0xa00X - the 0xa00X control 10520b2af32SRussell King * registers appear to set themselves to the 0x800X when AN is 10620b2af32SRussell King * restarted, but status registers appear readable from either. 10720b2af32SRussell King */ 10820b2af32SRussell King MV_AN_CTRL1000 = 0x8000, /* 1000base-T control register */ 10920b2af32SRussell King MV_AN_STAT1000 = 0x8001, /* 1000base-T status register */ 1100d3ad854SRussell King 1110d3ad854SRussell King /* Vendor2 MMD registers */ 112af3e28cbSAntoine Tenart MV_V2_PORT_CTRL = 0xf001, 1138f48c2acSRussell King MV_V2_PORT_CTRL_PWRDOWN = BIT(11), 1149893f316SMarek Behún MV_V2_33X0_PORT_CTRL_SWRST = BIT(15), 1159893f316SMarek Behún MV_V2_33X0_PORT_CTRL_MACTYPE_MASK = 0x7, 116f8ee45fcSMarek Behún MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI = 0x0, 117f8ee45fcSMarek Behún MV_V2_3310_PORT_CTRL_MACTYPE_XAUI_RATE_MATCH = 0x1, 118f8ee45fcSMarek Behún MV_V2_3340_PORT_CTRL_MACTYPE_RXAUI_NO_SGMII_AN = 0x1, 119f8ee45fcSMarek Behún MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI_RATE_MATCH = 0x2, 120f8ee45fcSMarek Behún MV_V2_3310_PORT_CTRL_MACTYPE_XAUI = 0x3, 121f8ee45fcSMarek Behún MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER = 0x4, 122f8ee45fcSMarek Behún MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_NO_SGMII_AN = 0x5, 123f8ee45fcSMarek Behún MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH = 0x6, 124f8ee45fcSMarek Behún MV_V2_33X0_PORT_CTRL_MACTYPE_USXGMII = 0x7, 12508041a9aSVoon Weifeng MV_V2_PORT_INTR_STS = 0xf040, 12608041a9aSVoon Weifeng MV_V2_PORT_INTR_MASK = 0xf043, 12708041a9aSVoon Weifeng MV_V2_PORT_INTR_STS_WOL_EN = BIT(8), 12808041a9aSVoon Weifeng MV_V2_MAGIC_PKT_WORD0 = 0xf06b, 12908041a9aSVoon Weifeng MV_V2_MAGIC_PKT_WORD1 = 0xf06c, 13008041a9aSVoon Weifeng MV_V2_MAGIC_PKT_WORD2 = 0xf06d, 13108041a9aSVoon Weifeng /* Wake on LAN registers */ 13208041a9aSVoon Weifeng MV_V2_WOL_CTRL = 0xf06e, 13308041a9aSVoon Weifeng MV_V2_WOL_CTRL_CLEAR_STS = BIT(15), 13408041a9aSVoon Weifeng MV_V2_WOL_CTRL_MAGIC_PKT_EN = BIT(0), 135c3e302edSBaruch Siach /* Temperature control/read registers (88X3310 only) */ 1360d3ad854SRussell King MV_V2_TEMP_CTRL = 0xf08a, 1370d3ad854SRussell King MV_V2_TEMP_CTRL_MASK = 0xc000, 1380d3ad854SRussell King MV_V2_TEMP_CTRL_SAMPLE = 0x0000, 1390d3ad854SRussell King MV_V2_TEMP_CTRL_DISABLE = 0xc000, 1400d3ad854SRussell King MV_V2_TEMP = 0xf08c, 1410d3ad854SRussell King MV_V2_TEMP_UNKNOWN = 0x9600, /* unknown function */ 1420d3ad854SRussell King }; 1430d3ad854SRussell King 14497bbe3bdSMarek Behún struct mv3310_chip { 1454075a6a0SRussell King bool (*has_downshift)(struct phy_device *phydev); 146261a74c6SMarek Behún void (*init_supported_interfaces)(unsigned long *mask); 14797bbe3bdSMarek Behún int (*get_mactype)(struct phy_device *phydev); 148*d6d29292SRussell King int (*set_mactype)(struct phy_device *phydev, int mactype); 149*d6d29292SRussell King int (*select_mactype)(unsigned long *interfaces); 15097bbe3bdSMarek Behún int (*init_interface)(struct phy_device *phydev, int mactype); 151884d9a67SMarek Behún 152884d9a67SMarek Behún #ifdef CONFIG_HWMON 153884d9a67SMarek Behún int (*hwmon_read_temp_reg)(struct phy_device *phydev); 154884d9a67SMarek Behún #endif 15597bbe3bdSMarek Behún }; 15697bbe3bdSMarek Behún 1570d3ad854SRussell King struct mv3310_priv { 158261a74c6SMarek Behún DECLARE_BITMAP(supported_interfaces, PHY_INTERFACE_MODE_MAX); 159261a74c6SMarek Behún 160dd649b4fSRussell King u32 firmware_ver; 1614075a6a0SRussell King bool has_downshift; 162e1170333SBaruch Siach bool rate_match; 16397bbe3bdSMarek Behún phy_interface_t const_interface; 164dd649b4fSRussell King 1650d3ad854SRussell King struct device *hwmon_dev; 1660d3ad854SRussell King char *hwmon_name; 16720b2af32SRussell King }; 16820b2af32SRussell King 16997bbe3bdSMarek Behún static const struct mv3310_chip *to_mv3310_chip(struct phy_device *phydev) 17097bbe3bdSMarek Behún { 17197bbe3bdSMarek Behún return phydev->drv->driver_data; 17297bbe3bdSMarek Behún } 17397bbe3bdSMarek Behún 1740d3ad854SRussell King #ifdef CONFIG_HWMON 1750d3ad854SRussell King static umode_t mv3310_hwmon_is_visible(const void *data, 1760d3ad854SRussell King enum hwmon_sensor_types type, 1770d3ad854SRussell King u32 attr, int channel) 1780d3ad854SRussell King { 1790d3ad854SRussell King if (type == hwmon_chip && attr == hwmon_chip_update_interval) 1800d3ad854SRussell King return 0444; 1810d3ad854SRussell King if (type == hwmon_temp && attr == hwmon_temp_input) 1820d3ad854SRussell King return 0444; 1830d3ad854SRussell King return 0; 1840d3ad854SRussell King } 1850d3ad854SRussell King 186c3e302edSBaruch Siach static int mv3310_hwmon_read_temp_reg(struct phy_device *phydev) 187c3e302edSBaruch Siach { 188c3e302edSBaruch Siach return phy_read_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP); 189c3e302edSBaruch Siach } 190c3e302edSBaruch Siach 191c3e302edSBaruch Siach static int mv2110_hwmon_read_temp_reg(struct phy_device *phydev) 192c3e302edSBaruch Siach { 193c3e302edSBaruch Siach return phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_TEMP); 194c3e302edSBaruch Siach } 195c3e302edSBaruch Siach 1960d3ad854SRussell King static int mv3310_hwmon_read(struct device *dev, enum hwmon_sensor_types type, 1970d3ad854SRussell King u32 attr, int channel, long *value) 1980d3ad854SRussell King { 1990d3ad854SRussell King struct phy_device *phydev = dev_get_drvdata(dev); 200884d9a67SMarek Behún const struct mv3310_chip *chip = to_mv3310_chip(phydev); 2010d3ad854SRussell King int temp; 2020d3ad854SRussell King 2030d3ad854SRussell King if (type == hwmon_chip && attr == hwmon_chip_update_interval) { 2040d3ad854SRussell King *value = MSEC_PER_SEC; 2050d3ad854SRussell King return 0; 2060d3ad854SRussell King } 2070d3ad854SRussell King 2080d3ad854SRussell King if (type == hwmon_temp && attr == hwmon_temp_input) { 209884d9a67SMarek Behún temp = chip->hwmon_read_temp_reg(phydev); 2100d3ad854SRussell King if (temp < 0) 2110d3ad854SRussell King return temp; 2120d3ad854SRussell King 2130d3ad854SRussell King *value = ((temp & 0xff) - 75) * 1000; 2140d3ad854SRussell King 2150d3ad854SRussell King return 0; 2160d3ad854SRussell King } 2170d3ad854SRussell King 2180d3ad854SRussell King return -EOPNOTSUPP; 2190d3ad854SRussell King } 2200d3ad854SRussell King 2210d3ad854SRussell King static const struct hwmon_ops mv3310_hwmon_ops = { 2220d3ad854SRussell King .is_visible = mv3310_hwmon_is_visible, 2230d3ad854SRussell King .read = mv3310_hwmon_read, 2240d3ad854SRussell King }; 2250d3ad854SRussell King 2260d3ad854SRussell King static u32 mv3310_hwmon_chip_config[] = { 2270d3ad854SRussell King HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL, 2280d3ad854SRussell King 0, 2290d3ad854SRussell King }; 2300d3ad854SRussell King 2310d3ad854SRussell King static const struct hwmon_channel_info mv3310_hwmon_chip = { 2320d3ad854SRussell King .type = hwmon_chip, 2330d3ad854SRussell King .config = mv3310_hwmon_chip_config, 2340d3ad854SRussell King }; 2350d3ad854SRussell King 2360d3ad854SRussell King static u32 mv3310_hwmon_temp_config[] = { 2370d3ad854SRussell King HWMON_T_INPUT, 2380d3ad854SRussell King 0, 2390d3ad854SRussell King }; 2400d3ad854SRussell King 2410d3ad854SRussell King static const struct hwmon_channel_info mv3310_hwmon_temp = { 2420d3ad854SRussell King .type = hwmon_temp, 2430d3ad854SRussell King .config = mv3310_hwmon_temp_config, 2440d3ad854SRussell King }; 2450d3ad854SRussell King 2460d3ad854SRussell King static const struct hwmon_channel_info *mv3310_hwmon_info[] = { 2470d3ad854SRussell King &mv3310_hwmon_chip, 2480d3ad854SRussell King &mv3310_hwmon_temp, 2490d3ad854SRussell King NULL, 2500d3ad854SRussell King }; 2510d3ad854SRussell King 2520d3ad854SRussell King static const struct hwmon_chip_info mv3310_hwmon_chip_info = { 2530d3ad854SRussell King .ops = &mv3310_hwmon_ops, 2540d3ad854SRussell King .info = mv3310_hwmon_info, 2550d3ad854SRussell King }; 2560d3ad854SRussell King 2570d3ad854SRussell King static int mv3310_hwmon_config(struct phy_device *phydev, bool enable) 2580d3ad854SRussell King { 2590d3ad854SRussell King u16 val; 2600d3ad854SRussell King int ret; 2610d3ad854SRussell King 262c3e302edSBaruch Siach if (phydev->drv->phy_id != MARVELL_PHY_ID_88X3310) 263c3e302edSBaruch Siach return 0; 264c3e302edSBaruch Siach 2650d3ad854SRussell King ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP, 2660d3ad854SRussell King MV_V2_TEMP_UNKNOWN); 2670d3ad854SRussell King if (ret < 0) 2680d3ad854SRussell King return ret; 2690d3ad854SRussell King 2700d3ad854SRussell King val = enable ? MV_V2_TEMP_CTRL_SAMPLE : MV_V2_TEMP_CTRL_DISABLE; 2710d3ad854SRussell King 272b06d8e5aSHeiner Kallweit return phy_modify_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP_CTRL, 273b06d8e5aSHeiner Kallweit MV_V2_TEMP_CTRL_MASK, val); 2740d3ad854SRussell King } 2750d3ad854SRussell King 2760d3ad854SRussell King static int mv3310_hwmon_probe(struct phy_device *phydev) 2770d3ad854SRussell King { 2780d3ad854SRussell King struct device *dev = &phydev->mdio.dev; 2790d3ad854SRussell King struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev); 2800d3ad854SRussell King int i, j, ret; 2810d3ad854SRussell King 2820d3ad854SRussell King priv->hwmon_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL); 2830d3ad854SRussell King if (!priv->hwmon_name) 2840d3ad854SRussell King return -ENODEV; 2850d3ad854SRussell King 2860d3ad854SRussell King for (i = j = 0; priv->hwmon_name[i]; i++) { 2870d3ad854SRussell King if (isalnum(priv->hwmon_name[i])) { 2880d3ad854SRussell King if (i != j) 2890d3ad854SRussell King priv->hwmon_name[j] = priv->hwmon_name[i]; 2900d3ad854SRussell King j++; 2910d3ad854SRussell King } 2920d3ad854SRussell King } 2930d3ad854SRussell King priv->hwmon_name[j] = '\0'; 2940d3ad854SRussell King 2950d3ad854SRussell King ret = mv3310_hwmon_config(phydev, true); 2960d3ad854SRussell King if (ret) 2970d3ad854SRussell King return ret; 2980d3ad854SRussell King 2990d3ad854SRussell King priv->hwmon_dev = devm_hwmon_device_register_with_info(dev, 3000d3ad854SRussell King priv->hwmon_name, phydev, 3010d3ad854SRussell King &mv3310_hwmon_chip_info, NULL); 3020d3ad854SRussell King 3030d3ad854SRussell King return PTR_ERR_OR_ZERO(priv->hwmon_dev); 3040d3ad854SRussell King } 3050d3ad854SRussell King #else 3060d3ad854SRussell King static inline int mv3310_hwmon_config(struct phy_device *phydev, bool enable) 3070d3ad854SRussell King { 3080d3ad854SRussell King return 0; 3090d3ad854SRussell King } 3100d3ad854SRussell King 3110d3ad854SRussell King static int mv3310_hwmon_probe(struct phy_device *phydev) 3120d3ad854SRussell King { 3130d3ad854SRussell King return 0; 3140d3ad854SRussell King } 3150d3ad854SRussell King #endif 3160d3ad854SRussell King 317c9cc1c81SRussell King static int mv3310_power_down(struct phy_device *phydev) 318c9cc1c81SRussell King { 319c9cc1c81SRussell King return phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL, 320c9cc1c81SRussell King MV_V2_PORT_CTRL_PWRDOWN); 321c9cc1c81SRussell King } 322c9cc1c81SRussell King 323c9cc1c81SRussell King static int mv3310_power_up(struct phy_device *phydev) 324c9cc1c81SRussell King { 3258f48c2acSRussell King struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev); 3268f48c2acSRussell King int ret; 3278f48c2acSRussell King 3288f48c2acSRussell King ret = phy_clear_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL, 329c9cc1c81SRussell King MV_V2_PORT_CTRL_PWRDOWN); 3308f48c2acSRussell King 331829e7573SBaruch Siach if (phydev->drv->phy_id != MARVELL_PHY_ID_88X3310 || 332829e7573SBaruch Siach priv->firmware_ver < 0x00030000) 3338f48c2acSRussell King return ret; 3348f48c2acSRussell King 3358f48c2acSRussell King return phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL, 3369893f316SMarek Behún MV_V2_33X0_PORT_CTRL_SWRST); 337c9cc1c81SRussell King } 338c9cc1c81SRussell King 3398d8963c3SRussell King static int mv3310_reset(struct phy_device *phydev, u32 unit) 3408d8963c3SRussell King { 3418964a217SDejin Zheng int val, err; 3428d8963c3SRussell King 3438d8963c3SRussell King err = phy_modify_mmd(phydev, MDIO_MMD_PCS, unit + MDIO_CTRL1, 3448d8963c3SRussell King MDIO_CTRL1_RESET, MDIO_CTRL1_RESET); 3458d8963c3SRussell King if (err < 0) 3468d8963c3SRussell King return err; 3478d8963c3SRussell King 3488964a217SDejin Zheng return phy_read_mmd_poll_timeout(phydev, MDIO_MMD_PCS, 3498964a217SDejin Zheng unit + MDIO_CTRL1, val, 3508964a217SDejin Zheng !(val & MDIO_CTRL1_RESET), 3518964a217SDejin Zheng 5000, 100000, true); 3528d8963c3SRussell King } 3538d8963c3SRussell King 3544075a6a0SRussell King static int mv3310_get_downshift(struct phy_device *phydev, u8 *ds) 3554075a6a0SRussell King { 3564075a6a0SRussell King struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev); 3574075a6a0SRussell King int val; 3584075a6a0SRussell King 3594075a6a0SRussell King if (!priv->has_downshift) 3604075a6a0SRussell King return -EOPNOTSUPP; 3614075a6a0SRussell King 3624075a6a0SRussell King val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_DSC1); 3634075a6a0SRussell King if (val < 0) 3644075a6a0SRussell King return val; 3654075a6a0SRussell King 3664075a6a0SRussell King if (val & MV_PCS_DSC1_ENABLE) 3674075a6a0SRussell King /* assume that all fields are the same */ 3684075a6a0SRussell King *ds = 1 + FIELD_GET(MV_PCS_DSC1_10GBT, (u16)val); 3694075a6a0SRussell King else 3704075a6a0SRussell King *ds = DOWNSHIFT_DEV_DISABLE; 3714075a6a0SRussell King 3724075a6a0SRussell King return 0; 3734075a6a0SRussell King } 3744075a6a0SRussell King 3754075a6a0SRussell King static int mv3310_set_downshift(struct phy_device *phydev, u8 ds) 3764075a6a0SRussell King { 3774075a6a0SRussell King struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev); 3784075a6a0SRussell King u16 val; 3794075a6a0SRussell King int err; 3804075a6a0SRussell King 3814075a6a0SRussell King if (!priv->has_downshift) 3824075a6a0SRussell King return -EOPNOTSUPP; 3834075a6a0SRussell King 3844075a6a0SRussell King if (ds == DOWNSHIFT_DEV_DISABLE) 3854075a6a0SRussell King return phy_clear_bits_mmd(phydev, MDIO_MMD_PCS, MV_PCS_DSC1, 3864075a6a0SRussell King MV_PCS_DSC1_ENABLE); 3874075a6a0SRussell King 3884075a6a0SRussell King /* DOWNSHIFT_DEV_DEFAULT_COUNT is confusing. It looks like it should 3894075a6a0SRussell King * set the default settings for the PHY. However, it is used for 3904075a6a0SRussell King * "ethtool --set-phy-tunable ethN downshift on". The intention is 3914075a6a0SRussell King * to enable downshift at a default number of retries. The default 3924075a6a0SRussell King * settings for 88x3310 are for two retries with downshift disabled. 3934075a6a0SRussell King * So let's use two retries with downshift enabled. 3944075a6a0SRussell King */ 3954075a6a0SRussell King if (ds == DOWNSHIFT_DEV_DEFAULT_COUNT) 3964075a6a0SRussell King ds = 2; 3974075a6a0SRussell King 3984075a6a0SRussell King if (ds > 8) 3994075a6a0SRussell King return -E2BIG; 4004075a6a0SRussell King 4014075a6a0SRussell King ds -= 1; 4024075a6a0SRussell King val = FIELD_PREP(MV_PCS_DSC2_2P5G, ds); 4034075a6a0SRussell King val |= FIELD_PREP(MV_PCS_DSC2_5G, ds); 4044075a6a0SRussell King err = phy_modify_mmd(phydev, MDIO_MMD_PCS, MV_PCS_DSC2, 4054075a6a0SRussell King MV_PCS_DSC2_2P5G | MV_PCS_DSC2_5G, val); 4064075a6a0SRussell King if (err < 0) 4074075a6a0SRussell King return err; 4084075a6a0SRussell King 4094075a6a0SRussell King val = MV_PCS_DSC1_ENABLE; 4104075a6a0SRussell King val |= FIELD_PREP(MV_PCS_DSC1_10GBT, ds); 4114075a6a0SRussell King val |= FIELD_PREP(MV_PCS_DSC1_1GBR, ds); 4124075a6a0SRussell King val |= FIELD_PREP(MV_PCS_DSC1_100BTX, ds); 4134075a6a0SRussell King 4144075a6a0SRussell King return phy_modify_mmd(phydev, MDIO_MMD_PCS, MV_PCS_DSC1, 4154075a6a0SRussell King MV_PCS_DSC1_ENABLE | MV_PCS_DSC1_10GBT | 4164075a6a0SRussell King MV_PCS_DSC1_1GBR | MV_PCS_DSC1_100BTX, val); 4174075a6a0SRussell King } 4184075a6a0SRussell King 419a585c03eSRussell King static int mv3310_get_edpd(struct phy_device *phydev, u16 *edpd) 420a585c03eSRussell King { 421a585c03eSRussell King int val; 422a585c03eSRussell King 423a585c03eSRussell King val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1); 424a585c03eSRussell King if (val < 0) 425a585c03eSRussell King return val; 426a585c03eSRussell King 427a585c03eSRussell King switch (val & MV_PCS_CSCR1_ED_MASK) { 428a585c03eSRussell King case MV_PCS_CSCR1_ED_NLP: 429a585c03eSRussell King *edpd = 1000; 430a585c03eSRussell King break; 431a585c03eSRussell King case MV_PCS_CSCR1_ED_RX: 432a585c03eSRussell King *edpd = ETHTOOL_PHY_EDPD_NO_TX; 433a585c03eSRussell King break; 434a585c03eSRussell King default: 435a585c03eSRussell King *edpd = ETHTOOL_PHY_EDPD_DISABLE; 436a585c03eSRussell King break; 437a585c03eSRussell King } 438a585c03eSRussell King return 0; 439a585c03eSRussell King } 440a585c03eSRussell King 441a585c03eSRussell King static int mv3310_set_edpd(struct phy_device *phydev, u16 edpd) 442a585c03eSRussell King { 443a585c03eSRussell King u16 val; 444a585c03eSRussell King int err; 445a585c03eSRussell King 446a585c03eSRussell King switch (edpd) { 447a585c03eSRussell King case 1000: 448a585c03eSRussell King case ETHTOOL_PHY_EDPD_DFLT_TX_MSECS: 449a585c03eSRussell King val = MV_PCS_CSCR1_ED_NLP; 450a585c03eSRussell King break; 451a585c03eSRussell King 452a585c03eSRussell King case ETHTOOL_PHY_EDPD_NO_TX: 453a585c03eSRussell King val = MV_PCS_CSCR1_ED_RX; 454a585c03eSRussell King break; 455a585c03eSRussell King 456a585c03eSRussell King case ETHTOOL_PHY_EDPD_DISABLE: 457a585c03eSRussell King val = MV_PCS_CSCR1_ED_OFF; 458a585c03eSRussell King break; 459a585c03eSRussell King 460a585c03eSRussell King default: 461a585c03eSRussell King return -EINVAL; 462a585c03eSRussell King } 463a585c03eSRussell King 464a585c03eSRussell King err = phy_modify_mmd_changed(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1, 465a585c03eSRussell King MV_PCS_CSCR1_ED_MASK, val); 466a585c03eSRussell King if (err > 0) 467a585c03eSRussell King err = mv3310_reset(phydev, MV_PCS_BASE_T); 468a585c03eSRussell King 469a585c03eSRussell King return err; 470a585c03eSRussell King } 471a585c03eSRussell King 47236023da1SRussell King static int mv3310_sfp_insert(void *upstream, const struct sfp_eeprom_id *id) 47336023da1SRussell King { 47436023da1SRussell King struct phy_device *phydev = upstream; 47536023da1SRussell King __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, }; 476fd580c98SRussell King DECLARE_PHY_INTERFACE_MASK(interfaces); 47736023da1SRussell King phy_interface_t iface; 47836023da1SRussell King 479fd580c98SRussell King sfp_parse_support(phydev->sfp_bus, id, support, interfaces); 480a4516c70SRussell King iface = sfp_select_interface(phydev->sfp_bus, support); 48136023da1SRussell King 482e0f909bcSRussell King if (iface != PHY_INTERFACE_MODE_10GBASER) { 48336023da1SRussell King dev_err(&phydev->mdio.dev, "incompatible SFP module inserted\n"); 48436023da1SRussell King return -EINVAL; 48536023da1SRussell King } 48636023da1SRussell King return 0; 48736023da1SRussell King } 48836023da1SRussell King 48936023da1SRussell King static const struct sfp_upstream_ops mv3310_sfp_ops = { 49036023da1SRussell King .attach = phy_sfp_attach, 49136023da1SRussell King .detach = phy_sfp_detach, 49236023da1SRussell King .module_insert = mv3310_sfp_insert, 49336023da1SRussell King }; 49436023da1SRussell King 49520b2af32SRussell King static int mv3310_probe(struct phy_device *phydev) 49620b2af32SRussell King { 497261a74c6SMarek Behún const struct mv3310_chip *chip = to_mv3310_chip(phydev); 4980d3ad854SRussell King struct mv3310_priv *priv; 49920b2af32SRussell King u32 mmd_mask = MDIO_DEVS_PMAPMD | MDIO_DEVS_AN; 5000d3ad854SRussell King int ret; 50120b2af32SRussell King 50220b2af32SRussell King if (!phydev->is_c45 || 50320b2af32SRussell King (phydev->c45_ids.devices_in_package & mmd_mask) != mmd_mask) 50420b2af32SRussell King return -ENODEV; 50520b2af32SRussell King 5063d3ced2eSRussell King ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_BOOT); 5073d3ced2eSRussell King if (ret < 0) 5083d3ced2eSRussell King return ret; 5093d3ced2eSRussell King 5103d3ced2eSRussell King if (ret & MV_PMA_BOOT_FATAL) { 5113d3ced2eSRussell King dev_warn(&phydev->mdio.dev, 5123d3ced2eSRussell King "PHY failed to boot firmware, status=%04x\n", ret); 5133d3ced2eSRussell King return -ENODEV; 5143d3ced2eSRussell King } 5153d3ced2eSRussell King 5160d3ad854SRussell King priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL); 5170d3ad854SRussell King if (!priv) 5180d3ad854SRussell King return -ENOMEM; 5190d3ad854SRussell King 5200d3ad854SRussell King dev_set_drvdata(&phydev->mdio.dev, priv); 5210d3ad854SRussell King 522dd649b4fSRussell King ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_FW_VER0); 523dd649b4fSRussell King if (ret < 0) 524dd649b4fSRussell King return ret; 525dd649b4fSRussell King 526dd649b4fSRussell King priv->firmware_ver = ret << 16; 527dd649b4fSRussell King 528dd649b4fSRussell King ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_FW_VER1); 529dd649b4fSRussell King if (ret < 0) 530dd649b4fSRussell King return ret; 531dd649b4fSRussell King 532dd649b4fSRussell King priv->firmware_ver |= ret; 533dd649b4fSRussell King 534dd649b4fSRussell King phydev_info(phydev, "Firmware version %u.%u.%u.%u\n", 535dd649b4fSRussell King priv->firmware_ver >> 24, (priv->firmware_ver >> 16) & 255, 536dd649b4fSRussell King (priv->firmware_ver >> 8) & 255, priv->firmware_ver & 255); 537dd649b4fSRussell King 5384075a6a0SRussell King if (chip->has_downshift) 5394075a6a0SRussell King priv->has_downshift = chip->has_downshift(phydev); 5404075a6a0SRussell King 541c9cc1c81SRussell King /* Powering down the port when not in use saves about 600mW */ 542c9cc1c81SRussell King ret = mv3310_power_down(phydev); 543c9cc1c81SRussell King if (ret) 544c9cc1c81SRussell King return ret; 545c9cc1c81SRussell King 5460d3ad854SRussell King ret = mv3310_hwmon_probe(phydev); 5470d3ad854SRussell King if (ret) 5480d3ad854SRussell King return ret; 5490d3ad854SRussell King 550261a74c6SMarek Behún chip->init_supported_interfaces(priv->supported_interfaces); 551261a74c6SMarek Behún 55236023da1SRussell King return phy_sfp_probe(phydev, &mv3310_sfp_ops); 55320b2af32SRussell King } 55420b2af32SRussell King 5551b8ef142SMarek Behún static void mv3310_remove(struct phy_device *phydev) 5561b8ef142SMarek Behún { 5571b8ef142SMarek Behún mv3310_hwmon_config(phydev, false); 5581b8ef142SMarek Behún } 5591b8ef142SMarek Behún 5600d3ad854SRussell King static int mv3310_suspend(struct phy_device *phydev) 5610d3ad854SRussell King { 562c9cc1c81SRussell King return mv3310_power_down(phydev); 5630d3ad854SRussell King } 5640d3ad854SRussell King 5650d3ad854SRussell King static int mv3310_resume(struct phy_device *phydev) 5660d3ad854SRussell King { 567af3e28cbSAntoine Tenart int ret; 568af3e28cbSAntoine Tenart 569c9cc1c81SRussell King ret = mv3310_power_up(phydev); 570af3e28cbSAntoine Tenart if (ret) 571af3e28cbSAntoine Tenart return ret; 572af3e28cbSAntoine Tenart 5730d3ad854SRussell King return mv3310_hwmon_config(phydev, true); 5740d3ad854SRussell King } 5750d3ad854SRussell King 576c47455f9SMaxime Chevallier /* Some PHYs in the Alaska family such as the 88X3310 and the 88E2010 577c47455f9SMaxime Chevallier * don't set bit 14 in PMA Extended Abilities (1.11), although they do 578c47455f9SMaxime Chevallier * support 2.5GBASET and 5GBASET. For these models, we can still read their 579c47455f9SMaxime Chevallier * 2.5G/5G extended abilities register (1.21). We detect these models based on 580c47455f9SMaxime Chevallier * the PMA device identifier, with a mask matching models known to have this 581c47455f9SMaxime Chevallier * issue 582c47455f9SMaxime Chevallier */ 583c47455f9SMaxime Chevallier static bool mv3310_has_pma_ngbaset_quirk(struct phy_device *phydev) 584c47455f9SMaxime Chevallier { 585c47455f9SMaxime Chevallier if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_PMAPMD)) 586c47455f9SMaxime Chevallier return false; 587c47455f9SMaxime Chevallier 588c47455f9SMaxime Chevallier /* Only some revisions of the 88X3310 family PMA seem to be impacted */ 589c47455f9SMaxime Chevallier return (phydev->c45_ids.device_ids[MDIO_MMD_PMAPMD] & 590c47455f9SMaxime Chevallier MV_PHY_ALASKA_NBT_QUIRK_MASK) == MV_PHY_ALASKA_NBT_QUIRK_REV; 591c47455f9SMaxime Chevallier } 592c47455f9SMaxime Chevallier 59397bbe3bdSMarek Behún static int mv2110_get_mactype(struct phy_device *phydev) 59497bbe3bdSMarek Behún { 59597bbe3bdSMarek Behún int mactype; 59697bbe3bdSMarek Behún 59797bbe3bdSMarek Behún mactype = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_21X0_PORT_CTRL); 59897bbe3bdSMarek Behún if (mactype < 0) 59997bbe3bdSMarek Behún return mactype; 60097bbe3bdSMarek Behún 60197bbe3bdSMarek Behún return mactype & MV_PMA_21X0_PORT_CTRL_MACTYPE_MASK; 60297bbe3bdSMarek Behún } 60397bbe3bdSMarek Behún 604*d6d29292SRussell King static int mv2110_set_mactype(struct phy_device *phydev, int mactype) 605*d6d29292SRussell King { 606*d6d29292SRussell King int err, val; 607*d6d29292SRussell King 608*d6d29292SRussell King mactype &= MV_PMA_21X0_PORT_CTRL_MACTYPE_MASK; 609*d6d29292SRussell King err = phy_modify_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_21X0_PORT_CTRL, 610*d6d29292SRussell King MV_PMA_21X0_PORT_CTRL_SWRST | 611*d6d29292SRussell King MV_PMA_21X0_PORT_CTRL_MACTYPE_MASK, 612*d6d29292SRussell King MV_PMA_21X0_PORT_CTRL_SWRST | mactype); 613*d6d29292SRussell King if (err) 614*d6d29292SRussell King return err; 615*d6d29292SRussell King 616*d6d29292SRussell King err = phy_set_bits_mmd(phydev, MDIO_MMD_AN, MV_AN_21X0_SERDES_CTRL2, 617*d6d29292SRussell King MV_AN_21X0_SERDES_CTRL2_AUTO_INIT_DIS | 618*d6d29292SRussell King MV_AN_21X0_SERDES_CTRL2_RUN_INIT); 619*d6d29292SRussell King if (err) 620*d6d29292SRussell King return err; 621*d6d29292SRussell King 622*d6d29292SRussell King err = phy_read_mmd_poll_timeout(phydev, MDIO_MMD_AN, 623*d6d29292SRussell King MV_AN_21X0_SERDES_CTRL2, val, 624*d6d29292SRussell King !(val & 625*d6d29292SRussell King MV_AN_21X0_SERDES_CTRL2_RUN_INIT), 626*d6d29292SRussell King 5000, 100000, true); 627*d6d29292SRussell King if (err) 628*d6d29292SRussell King return err; 629*d6d29292SRussell King 630*d6d29292SRussell King return phy_clear_bits_mmd(phydev, MDIO_MMD_AN, MV_AN_21X0_SERDES_CTRL2, 631*d6d29292SRussell King MV_AN_21X0_SERDES_CTRL2_AUTO_INIT_DIS); 632*d6d29292SRussell King } 633*d6d29292SRussell King 634*d6d29292SRussell King static int mv2110_select_mactype(unsigned long *interfaces) 635*d6d29292SRussell King { 636*d6d29292SRussell King if (test_bit(PHY_INTERFACE_MODE_USXGMII, interfaces)) 637*d6d29292SRussell King return MV_PMA_21X0_PORT_CTRL_MACTYPE_USXGMII; 638*d6d29292SRussell King else if (test_bit(PHY_INTERFACE_MODE_SGMII, interfaces) && 639*d6d29292SRussell King !test_bit(PHY_INTERFACE_MODE_10GBASER, interfaces)) 640*d6d29292SRussell King return MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER; 641*d6d29292SRussell King else if (test_bit(PHY_INTERFACE_MODE_10GBASER, interfaces)) 642*d6d29292SRussell King return MV_PMA_21X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH; 643*d6d29292SRussell King else 644*d6d29292SRussell King return -1; 645*d6d29292SRussell King } 646*d6d29292SRussell King 64797bbe3bdSMarek Behún static int mv3310_get_mactype(struct phy_device *phydev) 64897bbe3bdSMarek Behún { 64997bbe3bdSMarek Behún int mactype; 65097bbe3bdSMarek Behún 65197bbe3bdSMarek Behún mactype = phy_read_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL); 65297bbe3bdSMarek Behún if (mactype < 0) 65397bbe3bdSMarek Behún return mactype; 65497bbe3bdSMarek Behún 65597bbe3bdSMarek Behún return mactype & MV_V2_33X0_PORT_CTRL_MACTYPE_MASK; 65697bbe3bdSMarek Behún } 65797bbe3bdSMarek Behún 658*d6d29292SRussell King static int mv3310_set_mactype(struct phy_device *phydev, int mactype) 659*d6d29292SRussell King { 660*d6d29292SRussell King int ret; 661*d6d29292SRussell King 662*d6d29292SRussell King mactype &= MV_V2_33X0_PORT_CTRL_MACTYPE_MASK; 663*d6d29292SRussell King ret = phy_modify_mmd_changed(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL, 664*d6d29292SRussell King MV_V2_33X0_PORT_CTRL_MACTYPE_MASK, 665*d6d29292SRussell King mactype); 666*d6d29292SRussell King if (ret <= 0) 667*d6d29292SRussell King return ret; 668*d6d29292SRussell King 669*d6d29292SRussell King return phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL, 670*d6d29292SRussell King MV_V2_33X0_PORT_CTRL_SWRST); 671*d6d29292SRussell King } 672*d6d29292SRussell King 673*d6d29292SRussell King static int mv3310_select_mactype(unsigned long *interfaces) 674*d6d29292SRussell King { 675*d6d29292SRussell King if (test_bit(PHY_INTERFACE_MODE_USXGMII, interfaces)) 676*d6d29292SRussell King return MV_V2_33X0_PORT_CTRL_MACTYPE_USXGMII; 677*d6d29292SRussell King else if (test_bit(PHY_INTERFACE_MODE_SGMII, interfaces) && 678*d6d29292SRussell King test_bit(PHY_INTERFACE_MODE_10GBASER, interfaces)) 679*d6d29292SRussell King return MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER; 680*d6d29292SRussell King else if (test_bit(PHY_INTERFACE_MODE_SGMII, interfaces) && 681*d6d29292SRussell King test_bit(PHY_INTERFACE_MODE_RXAUI, interfaces)) 682*d6d29292SRussell King return MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI; 683*d6d29292SRussell King else if (test_bit(PHY_INTERFACE_MODE_SGMII, interfaces) && 684*d6d29292SRussell King test_bit(PHY_INTERFACE_MODE_XAUI, interfaces)) 685*d6d29292SRussell King return MV_V2_3310_PORT_CTRL_MACTYPE_XAUI; 686*d6d29292SRussell King else if (test_bit(PHY_INTERFACE_MODE_10GBASER, interfaces)) 687*d6d29292SRussell King return MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH; 688*d6d29292SRussell King else if (test_bit(PHY_INTERFACE_MODE_RXAUI, interfaces)) 689*d6d29292SRussell King return MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI_RATE_MATCH; 690*d6d29292SRussell King else if (test_bit(PHY_INTERFACE_MODE_XAUI, interfaces)) 691*d6d29292SRussell King return MV_V2_3310_PORT_CTRL_MACTYPE_XAUI_RATE_MATCH; 692*d6d29292SRussell King else if (test_bit(PHY_INTERFACE_MODE_SGMII, interfaces)) 693*d6d29292SRussell King return MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER; 694*d6d29292SRussell King else 695*d6d29292SRussell King return -1; 696*d6d29292SRussell King } 697*d6d29292SRussell King 69897bbe3bdSMarek Behún static int mv2110_init_interface(struct phy_device *phydev, int mactype) 69920b2af32SRussell King { 700e1170333SBaruch Siach struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev); 70197bbe3bdSMarek Behún 70297bbe3bdSMarek Behún priv->rate_match = false; 70397bbe3bdSMarek Behún 704ccbf2891SMarek Behún if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH) 70597bbe3bdSMarek Behún priv->rate_match = true; 706ccbf2891SMarek Behún 707ccbf2891SMarek Behún if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_USXGMII) 708ccbf2891SMarek Behún priv->const_interface = PHY_INTERFACE_MODE_USXGMII; 709ccbf2891SMarek Behún else if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH) 71097bbe3bdSMarek Behún priv->const_interface = PHY_INTERFACE_MODE_10GBASER; 711ccbf2891SMarek Behún else if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER || 712ccbf2891SMarek Behún mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER_NO_SGMII_AN) 713ccbf2891SMarek Behún priv->const_interface = PHY_INTERFACE_MODE_NA; 714ccbf2891SMarek Behún else 715ccbf2891SMarek Behún return -EINVAL; 71697bbe3bdSMarek Behún 71797bbe3bdSMarek Behún return 0; 71897bbe3bdSMarek Behún } 71997bbe3bdSMarek Behún 72097bbe3bdSMarek Behún static int mv3310_init_interface(struct phy_device *phydev, int mactype) 72197bbe3bdSMarek Behún { 72297bbe3bdSMarek Behún struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev); 72397bbe3bdSMarek Behún 72497bbe3bdSMarek Behún priv->rate_match = false; 72597bbe3bdSMarek Behún 72697bbe3bdSMarek Behún if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH || 72797bbe3bdSMarek Behún mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI_RATE_MATCH || 72897bbe3bdSMarek Behún mactype == MV_V2_3310_PORT_CTRL_MACTYPE_XAUI_RATE_MATCH) 72997bbe3bdSMarek Behún priv->rate_match = true; 73097bbe3bdSMarek Behún 731ccbf2891SMarek Behún if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_USXGMII) 732ccbf2891SMarek Behún priv->const_interface = PHY_INTERFACE_MODE_USXGMII; 733ccbf2891SMarek Behún else if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH || 734ccbf2891SMarek Behún mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_NO_SGMII_AN || 735ccbf2891SMarek Behún mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER) 73697bbe3bdSMarek Behún priv->const_interface = PHY_INTERFACE_MODE_10GBASER; 737ccbf2891SMarek Behún else if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI_RATE_MATCH || 738ccbf2891SMarek Behún mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI) 73997bbe3bdSMarek Behún priv->const_interface = PHY_INTERFACE_MODE_RXAUI; 740ccbf2891SMarek Behún else if (mactype == MV_V2_3310_PORT_CTRL_MACTYPE_XAUI_RATE_MATCH || 741ccbf2891SMarek Behún mactype == MV_V2_3310_PORT_CTRL_MACTYPE_XAUI) 74297bbe3bdSMarek Behún priv->const_interface = PHY_INTERFACE_MODE_XAUI; 743ccbf2891SMarek Behún else 744ccbf2891SMarek Behún return -EINVAL; 74597bbe3bdSMarek Behún 74697bbe3bdSMarek Behún return 0; 74797bbe3bdSMarek Behún } 74897bbe3bdSMarek Behún 7499885d016SMarek Behún static int mv3340_init_interface(struct phy_device *phydev, int mactype) 7509885d016SMarek Behún { 7519885d016SMarek Behún struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev); 7529885d016SMarek Behún int err = 0; 7539885d016SMarek Behún 7549885d016SMarek Behún priv->rate_match = false; 7559885d016SMarek Behún 7569885d016SMarek Behún if (mactype == MV_V2_3340_PORT_CTRL_MACTYPE_RXAUI_NO_SGMII_AN) 7579885d016SMarek Behún priv->const_interface = PHY_INTERFACE_MODE_RXAUI; 7589885d016SMarek Behún else 7599885d016SMarek Behún err = mv3310_init_interface(phydev, mactype); 7609885d016SMarek Behún 7619885d016SMarek Behún return err; 7629885d016SMarek Behún } 7639885d016SMarek Behún 76497bbe3bdSMarek Behún static int mv3310_config_init(struct phy_device *phydev) 76597bbe3bdSMarek Behún { 766261a74c6SMarek Behún struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev); 76797bbe3bdSMarek Behún const struct mv3310_chip *chip = to_mv3310_chip(phydev); 76897bbe3bdSMarek Behún int err, mactype; 769c9cc1c81SRussell King 77020b2af32SRussell King /* Check that the PHY interface type is compatible */ 771261a74c6SMarek Behún if (!test_bit(phydev->interface, priv->supported_interfaces)) 77220b2af32SRussell King return -ENODEV; 77320b2af32SRussell King 7748d8963c3SRussell King phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 7758d8963c3SRussell King 776c9cc1c81SRussell King /* Power up so reset works */ 777c9cc1c81SRussell King err = mv3310_power_up(phydev); 778c9cc1c81SRussell King if (err) 779c9cc1c81SRussell King return err; 780c9cc1c81SRussell King 781*d6d29292SRussell King /* If host provided host supported interface modes, try to select the 782*d6d29292SRussell King * best one 783*d6d29292SRussell King */ 784*d6d29292SRussell King if (!phy_interface_empty(phydev->host_interfaces)) { 785*d6d29292SRussell King mactype = chip->select_mactype(phydev->host_interfaces); 786*d6d29292SRussell King if (mactype >= 0) { 787*d6d29292SRussell King phydev_info(phydev, "Changing MACTYPE to %i\n", 788*d6d29292SRussell King mactype); 789*d6d29292SRussell King err = chip->set_mactype(phydev, mactype); 790*d6d29292SRussell King if (err) 791*d6d29292SRussell King return err; 792*d6d29292SRussell King } 793*d6d29292SRussell King } 794*d6d29292SRussell King 79597bbe3bdSMarek Behún mactype = chip->get_mactype(phydev); 79697bbe3bdSMarek Behún if (mactype < 0) 79797bbe3bdSMarek Behún return mactype; 79897bbe3bdSMarek Behún 79997bbe3bdSMarek Behún err = chip->init_interface(phydev, mactype); 800ccbf2891SMarek Behún if (err) { 801ccbf2891SMarek Behún phydev_err(phydev, "MACTYPE configuration invalid\n"); 80297bbe3bdSMarek Behún return err; 803ccbf2891SMarek Behún } 804e1170333SBaruch Siach 805a585c03eSRussell King /* Enable EDPD mode - saving 600mW */ 8064075a6a0SRussell King err = mv3310_set_edpd(phydev, ETHTOOL_PHY_EDPD_DFLT_TX_MSECS); 8074075a6a0SRussell King if (err) 8084075a6a0SRussell King return err; 8094075a6a0SRussell King 8104075a6a0SRussell King /* Allow downshift */ 8114075a6a0SRussell King err = mv3310_set_downshift(phydev, DOWNSHIFT_DEV_DEFAULT_COUNT); 8124075a6a0SRussell King if (err && err != -EOPNOTSUPP) 8134075a6a0SRussell King return err; 8144075a6a0SRussell King 8154075a6a0SRussell King return 0; 81674145424SMaxime Chevallier } 81774145424SMaxime Chevallier 81874145424SMaxime Chevallier static int mv3310_get_features(struct phy_device *phydev) 81974145424SMaxime Chevallier { 82074145424SMaxime Chevallier int ret, val; 82174145424SMaxime Chevallier 822ac3f5533SMaxime Chevallier ret = genphy_c45_pma_read_abilities(phydev); 823ac3f5533SMaxime Chevallier if (ret) 824ac3f5533SMaxime Chevallier return ret; 82520b2af32SRussell King 826c47455f9SMaxime Chevallier if (mv3310_has_pma_ngbaset_quirk(phydev)) { 827c47455f9SMaxime Chevallier val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, 828c47455f9SMaxime Chevallier MDIO_PMA_NG_EXTABLE); 829c47455f9SMaxime Chevallier if (val < 0) 830c47455f9SMaxime Chevallier return val; 831c47455f9SMaxime Chevallier 832c47455f9SMaxime Chevallier linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 833c47455f9SMaxime Chevallier phydev->supported, 834c47455f9SMaxime Chevallier val & MDIO_PMA_NG_EXTABLE_2_5GBT); 835c47455f9SMaxime Chevallier 836c47455f9SMaxime Chevallier linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, 837c47455f9SMaxime Chevallier phydev->supported, 838c47455f9SMaxime Chevallier val & MDIO_PMA_NG_EXTABLE_5GBT); 839c47455f9SMaxime Chevallier } 840c47455f9SMaxime Chevallier 84120b2af32SRussell King return 0; 84220b2af32SRussell King } 84320b2af32SRussell King 8448d8963c3SRussell King static int mv3310_config_mdix(struct phy_device *phydev) 8458d8963c3SRussell King { 8468d8963c3SRussell King u16 val; 8478d8963c3SRussell King int err; 8488d8963c3SRussell King 8498d8963c3SRussell King switch (phydev->mdix_ctrl) { 8508d8963c3SRussell King case ETH_TP_MDI_AUTO: 8518d8963c3SRussell King val = MV_PCS_CSCR1_MDIX_AUTO; 8528d8963c3SRussell King break; 8538d8963c3SRussell King case ETH_TP_MDI_X: 8548d8963c3SRussell King val = MV_PCS_CSCR1_MDIX_MDIX; 8558d8963c3SRussell King break; 8568d8963c3SRussell King case ETH_TP_MDI: 8578d8963c3SRussell King val = MV_PCS_CSCR1_MDIX_MDI; 8588d8963c3SRussell King break; 8598d8963c3SRussell King default: 8608d8963c3SRussell King return -EINVAL; 8618d8963c3SRussell King } 8628d8963c3SRussell King 8638d8963c3SRussell King err = phy_modify_mmd_changed(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1, 8648d8963c3SRussell King MV_PCS_CSCR1_MDIX_MASK, val); 8658d8963c3SRussell King if (err > 0) 8668d8963c3SRussell King err = mv3310_reset(phydev, MV_PCS_BASE_T); 8678d8963c3SRussell King 8688d8963c3SRussell King return err; 8698d8963c3SRussell King } 8708d8963c3SRussell King 87120b2af32SRussell King static int mv3310_config_aneg(struct phy_device *phydev) 87220b2af32SRussell King { 87320b2af32SRussell King bool changed = false; 8743c1bcc86SAndrew Lunn u16 reg; 87520b2af32SRussell King int ret; 87620b2af32SRussell King 8778d8963c3SRussell King ret = mv3310_config_mdix(phydev); 8788d8963c3SRussell King if (ret < 0) 8798d8963c3SRussell King return ret; 880ea4efe25SRussell King 88130de65c3SHeiner Kallweit if (phydev->autoneg == AUTONEG_DISABLE) 88230de65c3SHeiner Kallweit return genphy_c45_pma_setup_forced(phydev); 88320b2af32SRussell King 8843de97f3cSAndrew Lunn ret = genphy_c45_an_config_aneg(phydev); 88520b2af32SRussell King if (ret < 0) 88620b2af32SRussell King return ret; 88720b2af32SRussell King if (ret > 0) 88820b2af32SRussell King changed = true; 88920b2af32SRussell King 8903de97f3cSAndrew Lunn /* Clause 45 has no standardized support for 1000BaseT, therefore 8913de97f3cSAndrew Lunn * use vendor registers for this mode. 8923de97f3cSAndrew Lunn */ 8933c1bcc86SAndrew Lunn reg = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising); 894b06d8e5aSHeiner Kallweit ret = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MV_AN_CTRL1000, 8953c1bcc86SAndrew Lunn ADVERTISE_1000FULL | ADVERTISE_1000HALF, reg); 89620b2af32SRussell King if (ret < 0) 89720b2af32SRussell King return ret; 89820b2af32SRussell King if (ret > 0) 89920b2af32SRussell King changed = true; 90020b2af32SRussell King 9016b4cb6cbSHeiner Kallweit return genphy_c45_check_and_restart_aneg(phydev, changed); 90220b2af32SRussell King } 90320b2af32SRussell King 90420b2af32SRussell King static int mv3310_aneg_done(struct phy_device *phydev) 90520b2af32SRussell King { 90620b2af32SRussell King int val; 90720b2af32SRussell King 90820b2af32SRussell King val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1); 90920b2af32SRussell King if (val < 0) 91020b2af32SRussell King return val; 91120b2af32SRussell King 91220b2af32SRussell King if (val & MDIO_STAT1_LSTATUS) 91320b2af32SRussell King return 1; 91420b2af32SRussell King 91520b2af32SRussell King return genphy_c45_aneg_done(phydev); 91620b2af32SRussell King } 91720b2af32SRussell King 91836c4449aSRussell King static void mv3310_update_interface(struct phy_device *phydev) 91936c4449aSRussell King { 920e1170333SBaruch Siach struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev); 921e1170333SBaruch Siach 922ccbf2891SMarek Behún if (!phydev->link) 923ccbf2891SMarek Behún return; 924ccbf2891SMarek Behún 92597bbe3bdSMarek Behún /* In all of the "* with Rate Matching" modes the PHY interface is fixed 92697bbe3bdSMarek Behún * at 10Gb. The PHY adapts the rate to actual wire speed with help of 927e1170333SBaruch Siach * internal 16KB buffer. 928ccbf2891SMarek Behún * 929ccbf2891SMarek Behún * In USXGMII mode the PHY interface mode is also fixed. 930e1170333SBaruch Siach */ 931ccbf2891SMarek Behún if (priv->rate_match || 932ccbf2891SMarek Behún priv->const_interface == PHY_INTERFACE_MODE_USXGMII) { 93397bbe3bdSMarek Behún phydev->interface = priv->const_interface; 934e1170333SBaruch Siach return; 935e1170333SBaruch Siach } 936e1170333SBaruch Siach 937ccbf2891SMarek Behún /* The PHY automatically switches its serdes interface (and active PHYXS 938ccbf2891SMarek Behún * instance) between Cisco SGMII, 2500BaseX, 5GBase-R and 10GBase-R / 939ccbf2891SMarek Behún * xaui / rxaui modes according to the speed. 940ccbf2891SMarek Behún * Florian suggests setting phydev->interface to communicate this to the 941ccbf2891SMarek Behún * MAC. Only do this if we are already in one of the above modes. 94236c4449aSRussell King */ 943e555e5b1SMaxime Chevallier switch (phydev->speed) { 944e555e5b1SMaxime Chevallier case SPEED_10000: 945ccbf2891SMarek Behún phydev->interface = priv->const_interface; 946e555e5b1SMaxime Chevallier break; 9470d375542SMarek Behún case SPEED_5000: 9480d375542SMarek Behún phydev->interface = PHY_INTERFACE_MODE_5GBASER; 9490d375542SMarek Behún break; 950e555e5b1SMaxime Chevallier case SPEED_2500: 951e555e5b1SMaxime Chevallier phydev->interface = PHY_INTERFACE_MODE_2500BASEX; 952e555e5b1SMaxime Chevallier break; 953e555e5b1SMaxime Chevallier case SPEED_1000: 954e555e5b1SMaxime Chevallier case SPEED_100: 955e555e5b1SMaxime Chevallier case SPEED_10: 95636c4449aSRussell King phydev->interface = PHY_INTERFACE_MODE_SGMII; 957e555e5b1SMaxime Chevallier break; 958e555e5b1SMaxime Chevallier default: 959e555e5b1SMaxime Chevallier break; 960e555e5b1SMaxime Chevallier } 96136c4449aSRussell King } 96236c4449aSRussell King 96320b2af32SRussell King /* 10GBASE-ER,LR,LRM,SR do not support autonegotiation. */ 964c84786faSRussell King static int mv3310_read_status_10gbaser(struct phy_device *phydev) 96520b2af32SRussell King { 96620b2af32SRussell King phydev->link = 1; 96720b2af32SRussell King phydev->speed = SPEED_10000; 96820b2af32SRussell King phydev->duplex = DUPLEX_FULL; 9694217a64eSMichael Walle phydev->port = PORT_FIBRE; 97020b2af32SRussell King 97120b2af32SRussell King return 0; 97220b2af32SRussell King } 97320b2af32SRussell King 974c84786faSRussell King static int mv3310_read_status_copper(struct phy_device *phydev) 97520b2af32SRussell King { 976c84786faSRussell King int cssr1, speed, val; 97720b2af32SRussell King 978998a8a83SHeiner Kallweit val = genphy_c45_read_link(phydev); 97920b2af32SRussell King if (val < 0) 98020b2af32SRussell King return val; 98120b2af32SRussell King 98220b2af32SRussell King val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1); 98320b2af32SRussell King if (val < 0) 98420b2af32SRussell King return val; 98520b2af32SRussell King 986c84786faSRussell King cssr1 = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_CSSR1); 987c84786faSRussell King if (cssr1 < 0) 9880ed9704bSBaruch Siach return cssr1; 989c84786faSRussell King 990c84786faSRussell King /* If the link settings are not resolved, mark the link down */ 991c84786faSRussell King if (!(cssr1 & MV_PCS_CSSR1_RESOLVED)) { 992c84786faSRussell King phydev->link = 0; 993c84786faSRussell King return 0; 994c84786faSRussell King } 995c84786faSRussell King 996c84786faSRussell King /* Read the copper link settings */ 997c84786faSRussell King speed = cssr1 & MV_PCS_CSSR1_SPD1_MASK; 998c84786faSRussell King if (speed == MV_PCS_CSSR1_SPD1_SPD2) 999c84786faSRussell King speed |= cssr1 & MV_PCS_CSSR1_SPD2_MASK; 1000c84786faSRussell King 1001c84786faSRussell King switch (speed) { 1002c84786faSRussell King case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_10000: 1003c84786faSRussell King phydev->speed = SPEED_10000; 1004c84786faSRussell King break; 1005c84786faSRussell King 1006c84786faSRussell King case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_5000: 1007c84786faSRussell King phydev->speed = SPEED_5000; 1008c84786faSRussell King break; 1009c84786faSRussell King 1010c84786faSRussell King case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_2500: 1011c84786faSRussell King phydev->speed = SPEED_2500; 1012c84786faSRussell King break; 1013c84786faSRussell King 1014c84786faSRussell King case MV_PCS_CSSR1_SPD1_1000: 1015c84786faSRussell King phydev->speed = SPEED_1000; 1016c84786faSRussell King break; 1017c84786faSRussell King 1018c84786faSRussell King case MV_PCS_CSSR1_SPD1_100: 1019c84786faSRussell King phydev->speed = SPEED_100; 1020c84786faSRussell King break; 1021c84786faSRussell King 1022c84786faSRussell King case MV_PCS_CSSR1_SPD1_10: 1023c84786faSRussell King phydev->speed = SPEED_10; 1024c84786faSRussell King break; 1025c84786faSRussell King } 1026c84786faSRussell King 1027c84786faSRussell King phydev->duplex = cssr1 & MV_PCS_CSSR1_DUPLEX_FULL ? 1028c84786faSRussell King DUPLEX_FULL : DUPLEX_HALF; 10294217a64eSMichael Walle phydev->port = PORT_TP; 1030c84786faSRussell King phydev->mdix = cssr1 & MV_PCS_CSSR1_MDIX ? 1031c84786faSRussell King ETH_TP_MDI_X : ETH_TP_MDI; 1032c84786faSRussell King 103320b2af32SRussell King if (val & MDIO_AN_STAT1_COMPLETE) { 103420b2af32SRussell King val = genphy_c45_read_lpa(phydev); 103520b2af32SRussell King if (val < 0) 103620b2af32SRussell King return val; 103720b2af32SRussell King 1038cc1122b0SColin Ian King /* Read the link partner's 1G advertisement */ 103920b2af32SRussell King val = phy_read_mmd(phydev, MDIO_MMD_AN, MV_AN_STAT1000); 104020b2af32SRussell King if (val < 0) 104120b2af32SRussell King return val; 104220b2af32SRussell King 104378a24df3SAndrew Lunn mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, val); 104420b2af32SRussell King 1045c84786faSRussell King /* Update the pause status */ 1046c84786faSRussell King phy_resolve_aneg_pause(phydev); 104720b2af32SRussell King } 104820b2af32SRussell King 1049c84786faSRussell King return 0; 105020b2af32SRussell King } 105120b2af32SRussell King 1052c84786faSRussell King static int mv3310_read_status(struct phy_device *phydev) 1053c84786faSRussell King { 1054c84786faSRussell King int err, val; 1055ea4efe25SRussell King 1056c84786faSRussell King phydev->speed = SPEED_UNKNOWN; 1057c84786faSRussell King phydev->duplex = DUPLEX_UNKNOWN; 1058c84786faSRussell King linkmode_zero(phydev->lp_advertising); 1059c84786faSRussell King phydev->link = 0; 1060c84786faSRussell King phydev->pause = 0; 1061c84786faSRussell King phydev->asym_pause = 0; 1062ea4efe25SRussell King phydev->mdix = ETH_TP_MDI_INVALID; 1063ea4efe25SRussell King 1064c84786faSRussell King val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1); 1065c84786faSRussell King if (val < 0) 1066c84786faSRussell King return val; 1067c84786faSRussell King 1068c84786faSRussell King if (val & MDIO_STAT1_LSTATUS) 1069c84786faSRussell King err = mv3310_read_status_10gbaser(phydev); 1070c84786faSRussell King else 1071c84786faSRussell King err = mv3310_read_status_copper(phydev); 1072c84786faSRussell King if (err < 0) 1073c84786faSRussell King return err; 1074c84786faSRussell King 1075c84786faSRussell King if (phydev->link) 107636c4449aSRussell King mv3310_update_interface(phydev); 107720b2af32SRussell King 107820b2af32SRussell King return 0; 107920b2af32SRussell King } 108020b2af32SRussell King 1081a585c03eSRussell King static int mv3310_get_tunable(struct phy_device *phydev, 1082a585c03eSRussell King struct ethtool_tunable *tuna, void *data) 1083a585c03eSRussell King { 1084a585c03eSRussell King switch (tuna->id) { 10854075a6a0SRussell King case ETHTOOL_PHY_DOWNSHIFT: 10864075a6a0SRussell King return mv3310_get_downshift(phydev, data); 1087a585c03eSRussell King case ETHTOOL_PHY_EDPD: 1088a585c03eSRussell King return mv3310_get_edpd(phydev, data); 1089a585c03eSRussell King default: 1090a585c03eSRussell King return -EOPNOTSUPP; 1091a585c03eSRussell King } 1092a585c03eSRussell King } 1093a585c03eSRussell King 1094a585c03eSRussell King static int mv3310_set_tunable(struct phy_device *phydev, 1095a585c03eSRussell King struct ethtool_tunable *tuna, const void *data) 1096a585c03eSRussell King { 1097a585c03eSRussell King switch (tuna->id) { 10984075a6a0SRussell King case ETHTOOL_PHY_DOWNSHIFT: 10994075a6a0SRussell King return mv3310_set_downshift(phydev, *(u8 *)data); 1100a585c03eSRussell King case ETHTOOL_PHY_EDPD: 1101a585c03eSRussell King return mv3310_set_edpd(phydev, *(u16 *)data); 1102a585c03eSRussell King default: 1103a585c03eSRussell King return -EOPNOTSUPP; 1104a585c03eSRussell King } 1105a585c03eSRussell King } 1106a585c03eSRussell King 11074075a6a0SRussell King static bool mv3310_has_downshift(struct phy_device *phydev) 11084075a6a0SRussell King { 11094075a6a0SRussell King struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev); 11104075a6a0SRussell King 11114075a6a0SRussell King /* Fails to downshift with firmware older than v0.3.5.0 */ 11124075a6a0SRussell King return priv->firmware_ver >= MV_VERSION(0,3,5,0); 11134075a6a0SRussell King } 11144075a6a0SRussell King 1115261a74c6SMarek Behún static void mv3310_init_supported_interfaces(unsigned long *mask) 1116261a74c6SMarek Behún { 1117261a74c6SMarek Behún __set_bit(PHY_INTERFACE_MODE_SGMII, mask); 1118261a74c6SMarek Behún __set_bit(PHY_INTERFACE_MODE_2500BASEX, mask); 1119261a74c6SMarek Behún __set_bit(PHY_INTERFACE_MODE_5GBASER, mask); 1120261a74c6SMarek Behún __set_bit(PHY_INTERFACE_MODE_XAUI, mask); 1121261a74c6SMarek Behún __set_bit(PHY_INTERFACE_MODE_RXAUI, mask); 1122261a74c6SMarek Behún __set_bit(PHY_INTERFACE_MODE_10GBASER, mask); 1123261a74c6SMarek Behún __set_bit(PHY_INTERFACE_MODE_USXGMII, mask); 1124261a74c6SMarek Behún } 1125261a74c6SMarek Behún 11269885d016SMarek Behún static void mv3340_init_supported_interfaces(unsigned long *mask) 11279885d016SMarek Behún { 11289885d016SMarek Behún __set_bit(PHY_INTERFACE_MODE_SGMII, mask); 11299885d016SMarek Behún __set_bit(PHY_INTERFACE_MODE_2500BASEX, mask); 11309885d016SMarek Behún __set_bit(PHY_INTERFACE_MODE_5GBASER, mask); 11319885d016SMarek Behún __set_bit(PHY_INTERFACE_MODE_RXAUI, mask); 11329885d016SMarek Behún __set_bit(PHY_INTERFACE_MODE_10GBASER, mask); 11339885d016SMarek Behún __set_bit(PHY_INTERFACE_MODE_USXGMII, mask); 11349885d016SMarek Behún } 11359885d016SMarek Behún 1136261a74c6SMarek Behún static void mv2110_init_supported_interfaces(unsigned long *mask) 1137261a74c6SMarek Behún { 1138261a74c6SMarek Behún __set_bit(PHY_INTERFACE_MODE_SGMII, mask); 1139261a74c6SMarek Behún __set_bit(PHY_INTERFACE_MODE_2500BASEX, mask); 1140261a74c6SMarek Behún __set_bit(PHY_INTERFACE_MODE_5GBASER, mask); 1141261a74c6SMarek Behún __set_bit(PHY_INTERFACE_MODE_10GBASER, mask); 1142261a74c6SMarek Behún __set_bit(PHY_INTERFACE_MODE_USXGMII, mask); 1143261a74c6SMarek Behún } 1144261a74c6SMarek Behún 11450fca947cSMarek Behún static void mv2111_init_supported_interfaces(unsigned long *mask) 11460fca947cSMarek Behún { 11470fca947cSMarek Behún __set_bit(PHY_INTERFACE_MODE_SGMII, mask); 11480fca947cSMarek Behún __set_bit(PHY_INTERFACE_MODE_2500BASEX, mask); 11490fca947cSMarek Behún __set_bit(PHY_INTERFACE_MODE_10GBASER, mask); 11500fca947cSMarek Behún __set_bit(PHY_INTERFACE_MODE_USXGMII, mask); 11510fca947cSMarek Behún } 11520fca947cSMarek Behún 115397bbe3bdSMarek Behún static const struct mv3310_chip mv3310_type = { 11544075a6a0SRussell King .has_downshift = mv3310_has_downshift, 1155261a74c6SMarek Behún .init_supported_interfaces = mv3310_init_supported_interfaces, 115697bbe3bdSMarek Behún .get_mactype = mv3310_get_mactype, 1157*d6d29292SRussell King .set_mactype = mv3310_set_mactype, 1158*d6d29292SRussell King .select_mactype = mv3310_select_mactype, 115997bbe3bdSMarek Behún .init_interface = mv3310_init_interface, 1160884d9a67SMarek Behún 1161884d9a67SMarek Behún #ifdef CONFIG_HWMON 1162884d9a67SMarek Behún .hwmon_read_temp_reg = mv3310_hwmon_read_temp_reg, 1163884d9a67SMarek Behún #endif 116497bbe3bdSMarek Behún }; 116597bbe3bdSMarek Behún 11669885d016SMarek Behún static const struct mv3310_chip mv3340_type = { 11674075a6a0SRussell King .has_downshift = mv3310_has_downshift, 11689885d016SMarek Behún .init_supported_interfaces = mv3340_init_supported_interfaces, 11699885d016SMarek Behún .get_mactype = mv3310_get_mactype, 1170*d6d29292SRussell King .set_mactype = mv3310_set_mactype, 1171*d6d29292SRussell King .select_mactype = mv3310_select_mactype, 11729885d016SMarek Behún .init_interface = mv3340_init_interface, 11739885d016SMarek Behún 11749885d016SMarek Behún #ifdef CONFIG_HWMON 11759885d016SMarek Behún .hwmon_read_temp_reg = mv3310_hwmon_read_temp_reg, 11769885d016SMarek Behún #endif 11779885d016SMarek Behún }; 11789885d016SMarek Behún 117997bbe3bdSMarek Behún static const struct mv3310_chip mv2110_type = { 1180261a74c6SMarek Behún .init_supported_interfaces = mv2110_init_supported_interfaces, 118197bbe3bdSMarek Behún .get_mactype = mv2110_get_mactype, 1182*d6d29292SRussell King .set_mactype = mv2110_set_mactype, 1183*d6d29292SRussell King .select_mactype = mv2110_select_mactype, 118497bbe3bdSMarek Behún .init_interface = mv2110_init_interface, 1185884d9a67SMarek Behún 1186884d9a67SMarek Behún #ifdef CONFIG_HWMON 1187884d9a67SMarek Behún .hwmon_read_temp_reg = mv2110_hwmon_read_temp_reg, 1188884d9a67SMarek Behún #endif 118997bbe3bdSMarek Behún }; 119097bbe3bdSMarek Behún 11910fca947cSMarek Behún static const struct mv3310_chip mv2111_type = { 11920fca947cSMarek Behún .init_supported_interfaces = mv2111_init_supported_interfaces, 11930fca947cSMarek Behún .get_mactype = mv2110_get_mactype, 1194*d6d29292SRussell King .set_mactype = mv2110_set_mactype, 1195*d6d29292SRussell King .select_mactype = mv2110_select_mactype, 11960fca947cSMarek Behún .init_interface = mv2110_init_interface, 11970fca947cSMarek Behún 11980fca947cSMarek Behún #ifdef CONFIG_HWMON 11990fca947cSMarek Behún .hwmon_read_temp_reg = mv2110_hwmon_read_temp_reg, 12000fca947cSMarek Behún #endif 12010fca947cSMarek Behún }; 12020fca947cSMarek Behún 1203a5de4be0SMarek Behún static int mv3310_get_number_of_ports(struct phy_device *phydev) 1204a5de4be0SMarek Behún { 1205a5de4be0SMarek Behún int ret; 1206a5de4be0SMarek Behún 1207a5de4be0SMarek Behún ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_PORT_INFO); 1208a5de4be0SMarek Behún if (ret < 0) 1209a5de4be0SMarek Behún return ret; 1210a5de4be0SMarek Behún 1211a5de4be0SMarek Behún ret &= MV_PCS_PORT_INFO_NPORTS_MASK; 1212a5de4be0SMarek Behún ret >>= MV_PCS_PORT_INFO_NPORTS_SHIFT; 1213a5de4be0SMarek Behún 1214a5de4be0SMarek Behún return ret + 1; 1215a5de4be0SMarek Behún } 1216a5de4be0SMarek Behún 1217a5de4be0SMarek Behún static int mv3310_match_phy_device(struct phy_device *phydev) 1218a5de4be0SMarek Behún { 12190d55649dSVladimir Oltean if ((phydev->c45_ids.device_ids[MDIO_MMD_PMAPMD] & 12200d55649dSVladimir Oltean MARVELL_PHY_ID_MASK) != MARVELL_PHY_ID_88X3310) 12210d55649dSVladimir Oltean return 0; 12220d55649dSVladimir Oltean 1223a5de4be0SMarek Behún return mv3310_get_number_of_ports(phydev) == 1; 1224a5de4be0SMarek Behún } 1225a5de4be0SMarek Behún 1226a5de4be0SMarek Behún static int mv3340_match_phy_device(struct phy_device *phydev) 1227a5de4be0SMarek Behún { 12280d55649dSVladimir Oltean if ((phydev->c45_ids.device_ids[MDIO_MMD_PMAPMD] & 12290d55649dSVladimir Oltean MARVELL_PHY_ID_MASK) != MARVELL_PHY_ID_88X3310) 12300d55649dSVladimir Oltean return 0; 12310d55649dSVladimir Oltean 1232a5de4be0SMarek Behún return mv3310_get_number_of_ports(phydev) == 4; 1233a5de4be0SMarek Behún } 1234a5de4be0SMarek Behún 12350fca947cSMarek Behún static int mv211x_match_phy_device(struct phy_device *phydev, bool has_5g) 12360fca947cSMarek Behún { 12370fca947cSMarek Behún int val; 12380fca947cSMarek Behún 12390fca947cSMarek Behún if ((phydev->c45_ids.device_ids[MDIO_MMD_PMAPMD] & 12400fca947cSMarek Behún MARVELL_PHY_ID_MASK) != MARVELL_PHY_ID_88E2110) 12410fca947cSMarek Behún return 0; 12420fca947cSMarek Behún 12430fca947cSMarek Behún val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_SPEED); 12440fca947cSMarek Behún if (val < 0) 12450fca947cSMarek Behún return val; 12460fca947cSMarek Behún 12470fca947cSMarek Behún return !!(val & MDIO_PCS_SPEED_5G) == has_5g; 12480fca947cSMarek Behún } 12490fca947cSMarek Behún 12500fca947cSMarek Behún static int mv2110_match_phy_device(struct phy_device *phydev) 12510fca947cSMarek Behún { 12520fca947cSMarek Behún return mv211x_match_phy_device(phydev, true); 12530fca947cSMarek Behún } 12540fca947cSMarek Behún 12550fca947cSMarek Behún static int mv2111_match_phy_device(struct phy_device *phydev) 12560fca947cSMarek Behún { 12570fca947cSMarek Behún return mv211x_match_phy_device(phydev, false); 12580fca947cSMarek Behún } 12590fca947cSMarek Behún 126008041a9aSVoon Weifeng static void mv3110_get_wol(struct phy_device *phydev, 126108041a9aSVoon Weifeng struct ethtool_wolinfo *wol) 126208041a9aSVoon Weifeng { 126308041a9aSVoon Weifeng int ret; 126408041a9aSVoon Weifeng 126508041a9aSVoon Weifeng wol->supported = WAKE_MAGIC; 126608041a9aSVoon Weifeng wol->wolopts = 0; 126708041a9aSVoon Weifeng 126808041a9aSVoon Weifeng ret = phy_read_mmd(phydev, MDIO_MMD_VEND2, MV_V2_WOL_CTRL); 126908041a9aSVoon Weifeng if (ret < 0) 127008041a9aSVoon Weifeng return; 127108041a9aSVoon Weifeng 127208041a9aSVoon Weifeng if (ret & MV_V2_WOL_CTRL_MAGIC_PKT_EN) 127308041a9aSVoon Weifeng wol->wolopts |= WAKE_MAGIC; 127408041a9aSVoon Weifeng } 127508041a9aSVoon Weifeng 127608041a9aSVoon Weifeng static int mv3110_set_wol(struct phy_device *phydev, 127708041a9aSVoon Weifeng struct ethtool_wolinfo *wol) 127808041a9aSVoon Weifeng { 127908041a9aSVoon Weifeng int ret; 128008041a9aSVoon Weifeng 128108041a9aSVoon Weifeng if (wol->wolopts & WAKE_MAGIC) { 128208041a9aSVoon Weifeng /* Enable the WOL interrupt */ 128308041a9aSVoon Weifeng ret = phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, 128408041a9aSVoon Weifeng MV_V2_PORT_INTR_MASK, 128508041a9aSVoon Weifeng MV_V2_PORT_INTR_STS_WOL_EN); 128608041a9aSVoon Weifeng if (ret < 0) 128708041a9aSVoon Weifeng return ret; 128808041a9aSVoon Weifeng 128908041a9aSVoon Weifeng /* Store the device address for the magic packet */ 129008041a9aSVoon Weifeng ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, 129108041a9aSVoon Weifeng MV_V2_MAGIC_PKT_WORD2, 129208041a9aSVoon Weifeng ((phydev->attached_dev->dev_addr[5] << 8) | 129308041a9aSVoon Weifeng phydev->attached_dev->dev_addr[4])); 129408041a9aSVoon Weifeng if (ret < 0) 129508041a9aSVoon Weifeng return ret; 129608041a9aSVoon Weifeng 129708041a9aSVoon Weifeng ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, 129808041a9aSVoon Weifeng MV_V2_MAGIC_PKT_WORD1, 129908041a9aSVoon Weifeng ((phydev->attached_dev->dev_addr[3] << 8) | 130008041a9aSVoon Weifeng phydev->attached_dev->dev_addr[2])); 130108041a9aSVoon Weifeng if (ret < 0) 130208041a9aSVoon Weifeng return ret; 130308041a9aSVoon Weifeng 130408041a9aSVoon Weifeng ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, 130508041a9aSVoon Weifeng MV_V2_MAGIC_PKT_WORD0, 130608041a9aSVoon Weifeng ((phydev->attached_dev->dev_addr[1] << 8) | 130708041a9aSVoon Weifeng phydev->attached_dev->dev_addr[0])); 130808041a9aSVoon Weifeng if (ret < 0) 130908041a9aSVoon Weifeng return ret; 131008041a9aSVoon Weifeng 131108041a9aSVoon Weifeng /* Clear WOL status and enable magic packet matching */ 131208041a9aSVoon Weifeng ret = phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, 131308041a9aSVoon Weifeng MV_V2_WOL_CTRL, 131408041a9aSVoon Weifeng MV_V2_WOL_CTRL_MAGIC_PKT_EN | 131508041a9aSVoon Weifeng MV_V2_WOL_CTRL_CLEAR_STS); 131608041a9aSVoon Weifeng if (ret < 0) 131708041a9aSVoon Weifeng return ret; 131808041a9aSVoon Weifeng } else { 131908041a9aSVoon Weifeng /* Disable magic packet matching & reset WOL status bit */ 132008041a9aSVoon Weifeng ret = phy_modify_mmd(phydev, MDIO_MMD_VEND2, 132108041a9aSVoon Weifeng MV_V2_WOL_CTRL, 132208041a9aSVoon Weifeng MV_V2_WOL_CTRL_MAGIC_PKT_EN, 132308041a9aSVoon Weifeng MV_V2_WOL_CTRL_CLEAR_STS); 132408041a9aSVoon Weifeng if (ret < 0) 132508041a9aSVoon Weifeng return ret; 132608041a9aSVoon Weifeng } 132708041a9aSVoon Weifeng 132808041a9aSVoon Weifeng /* Reset the clear WOL status bit as it does not self-clear */ 132908041a9aSVoon Weifeng return phy_clear_bits_mmd(phydev, MDIO_MMD_VEND2, 133008041a9aSVoon Weifeng MV_V2_WOL_CTRL, 133108041a9aSVoon Weifeng MV_V2_WOL_CTRL_CLEAR_STS); 133208041a9aSVoon Weifeng } 133308041a9aSVoon Weifeng 133420b2af32SRussell King static struct phy_driver mv3310_drivers[] = { 133520b2af32SRussell King { 1336631ba906SMaxime Chevallier .phy_id = MARVELL_PHY_ID_88X3310, 1337a5de4be0SMarek Behún .phy_id_mask = MARVELL_PHY_ID_MASK, 1338a5de4be0SMarek Behún .match_phy_device = mv3310_match_phy_device, 133920b2af32SRussell King .name = "mv88x3310", 134097bbe3bdSMarek Behún .driver_data = &mv3310_type, 134174145424SMaxime Chevallier .get_features = mv3310_get_features, 134220b2af32SRussell King .config_init = mv3310_config_init, 13430d3ad854SRussell King .probe = mv3310_probe, 13440d3ad854SRussell King .suspend = mv3310_suspend, 13450d3ad854SRussell King .resume = mv3310_resume, 134620b2af32SRussell King .config_aneg = mv3310_config_aneg, 134720b2af32SRussell King .aneg_done = mv3310_aneg_done, 134820b2af32SRussell King .read_status = mv3310_read_status, 1349a585c03eSRussell King .get_tunable = mv3310_get_tunable, 1350a585c03eSRussell King .set_tunable = mv3310_set_tunable, 13511b8ef142SMarek Behún .remove = mv3310_remove, 1352d137c70dSWong Vee Khee .set_loopback = genphy_c45_loopback, 135308041a9aSVoon Weifeng .get_wol = mv3110_get_wol, 135408041a9aSVoon Weifeng .set_wol = mv3110_set_wol, 135520b2af32SRussell King }, 135662d01535SMaxime Chevallier { 1357a5de4be0SMarek Behún .phy_id = MARVELL_PHY_ID_88X3310, 1358a5de4be0SMarek Behún .phy_id_mask = MARVELL_PHY_ID_MASK, 1359a5de4be0SMarek Behún .match_phy_device = mv3340_match_phy_device, 13609885d016SMarek Behún .name = "mv88x3340", 13619885d016SMarek Behún .driver_data = &mv3340_type, 13629885d016SMarek Behún .get_features = mv3310_get_features, 13639885d016SMarek Behún .config_init = mv3310_config_init, 13649885d016SMarek Behún .probe = mv3310_probe, 13659885d016SMarek Behún .suspend = mv3310_suspend, 13669885d016SMarek Behún .resume = mv3310_resume, 13679885d016SMarek Behún .config_aneg = mv3310_config_aneg, 13689885d016SMarek Behún .aneg_done = mv3310_aneg_done, 13699885d016SMarek Behún .read_status = mv3310_read_status, 13709885d016SMarek Behún .get_tunable = mv3310_get_tunable, 13719885d016SMarek Behún .set_tunable = mv3310_set_tunable, 13729885d016SMarek Behún .remove = mv3310_remove, 13739885d016SMarek Behún .set_loopback = genphy_c45_loopback, 13749885d016SMarek Behún }, 13759885d016SMarek Behún { 137662d01535SMaxime Chevallier .phy_id = MARVELL_PHY_ID_88E2110, 137762d01535SMaxime Chevallier .phy_id_mask = MARVELL_PHY_ID_MASK, 13780fca947cSMarek Behún .match_phy_device = mv2110_match_phy_device, 1379c89f27d4SMarek Behún .name = "mv88e2110", 138097bbe3bdSMarek Behún .driver_data = &mv2110_type, 138162d01535SMaxime Chevallier .probe = mv3310_probe, 1382e02c4a9dSAntoine Tenart .suspend = mv3310_suspend, 1383e02c4a9dSAntoine Tenart .resume = mv3310_resume, 138462d01535SMaxime Chevallier .config_init = mv3310_config_init, 138562d01535SMaxime Chevallier .config_aneg = mv3310_config_aneg, 138662d01535SMaxime Chevallier .aneg_done = mv3310_aneg_done, 138762d01535SMaxime Chevallier .read_status = mv3310_read_status, 1388a585c03eSRussell King .get_tunable = mv3310_get_tunable, 1389a585c03eSRussell King .set_tunable = mv3310_set_tunable, 13901b8ef142SMarek Behún .remove = mv3310_remove, 1391d137c70dSWong Vee Khee .set_loopback = genphy_c45_loopback, 139208041a9aSVoon Weifeng .get_wol = mv3110_get_wol, 139308041a9aSVoon Weifeng .set_wol = mv3110_set_wol, 139462d01535SMaxime Chevallier }, 13950fca947cSMarek Behún { 13960fca947cSMarek Behún .phy_id = MARVELL_PHY_ID_88E2110, 13970fca947cSMarek Behún .phy_id_mask = MARVELL_PHY_ID_MASK, 13980fca947cSMarek Behún .match_phy_device = mv2111_match_phy_device, 13990fca947cSMarek Behún .name = "mv88e2111", 14000fca947cSMarek Behún .driver_data = &mv2111_type, 14010fca947cSMarek Behún .probe = mv3310_probe, 14020fca947cSMarek Behún .suspend = mv3310_suspend, 14030fca947cSMarek Behún .resume = mv3310_resume, 14040fca947cSMarek Behún .config_init = mv3310_config_init, 14050fca947cSMarek Behún .config_aneg = mv3310_config_aneg, 14060fca947cSMarek Behún .aneg_done = mv3310_aneg_done, 14070fca947cSMarek Behún .read_status = mv3310_read_status, 14080fca947cSMarek Behún .get_tunable = mv3310_get_tunable, 14090fca947cSMarek Behún .set_tunable = mv3310_set_tunable, 14100fca947cSMarek Behún .remove = mv3310_remove, 14110fca947cSMarek Behún .set_loopback = genphy_c45_loopback, 14120fca947cSMarek Behún }, 141320b2af32SRussell King }; 141420b2af32SRussell King 141520b2af32SRussell King module_phy_driver(mv3310_drivers); 141620b2af32SRussell King 141720b2af32SRussell King static struct mdio_device_id __maybe_unused mv3310_tbl[] = { 1418a5de4be0SMarek Behún { MARVELL_PHY_ID_88X3310, MARVELL_PHY_ID_MASK }, 141962d01535SMaxime Chevallier { MARVELL_PHY_ID_88E2110, MARVELL_PHY_ID_MASK }, 142020b2af32SRussell King { }, 142120b2af32SRussell King }; 142220b2af32SRussell King MODULE_DEVICE_TABLE(mdio, mv3310_tbl); 1423c7dce05eSMarek Behún MODULE_DESCRIPTION("Marvell Alaska X/M multi-gigabit Ethernet PHY driver"); 142420b2af32SRussell King MODULE_LICENSE("GPL"); 1425