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 */ 250d3ad854SRussell King #include <linux/ctype.h> 268d8963c3SRussell King #include <linux/delay.h> 270d3ad854SRussell King #include <linux/hwmon.h> 28952b6b3bSAntoine Tenart #include <linux/marvell_phy.h> 290d3ad854SRussell King #include <linux/phy.h> 3036023da1SRussell King #include <linux/sfp.h> 3120b2af32SRussell King 32c47455f9SMaxime Chevallier #define MV_PHY_ALASKA_NBT_QUIRK_MASK 0xfffffffe 33c47455f9SMaxime Chevallier #define MV_PHY_ALASKA_NBT_QUIRK_REV (MARVELL_PHY_ID_88X3310 | 0xa) 34c47455f9SMaxime Chevallier 3520b2af32SRussell King enum { 36dd649b4fSRussell King MV_PMA_FW_VER0 = 0xc011, 37dd649b4fSRussell King MV_PMA_FW_VER1 = 0xc012, 383d3ced2eSRussell King MV_PMA_BOOT = 0xc050, 393d3ced2eSRussell King MV_PMA_BOOT_FATAL = BIT(0), 403d3ced2eSRussell King 4120b2af32SRussell King MV_PCS_BASE_T = 0x0000, 4220b2af32SRussell King MV_PCS_BASE_R = 0x1000, 4320b2af32SRussell King MV_PCS_1000BASEX = 0x2000, 4420b2af32SRussell King 458d8963c3SRussell King MV_PCS_CSCR1 = 0x8000, 46a585c03eSRussell King MV_PCS_CSCR1_ED_MASK = 0x0300, 47a585c03eSRussell King MV_PCS_CSCR1_ED_OFF = 0x0000, 48a585c03eSRussell King MV_PCS_CSCR1_ED_RX = 0x0200, 49a585c03eSRussell King MV_PCS_CSCR1_ED_NLP = 0x0300, 508d8963c3SRussell King MV_PCS_CSCR1_MDIX_MASK = 0x0060, 518d8963c3SRussell King MV_PCS_CSCR1_MDIX_MDI = 0x0000, 528d8963c3SRussell King MV_PCS_CSCR1_MDIX_MDIX = 0x0020, 538d8963c3SRussell King MV_PCS_CSCR1_MDIX_AUTO = 0x0060, 548d8963c3SRussell King 55c84786faSRussell King MV_PCS_CSSR1 = 0x8008, 56c84786faSRussell King MV_PCS_CSSR1_SPD1_MASK = 0xc000, 57c84786faSRussell King MV_PCS_CSSR1_SPD1_SPD2 = 0xc000, 58c84786faSRussell King MV_PCS_CSSR1_SPD1_1000 = 0x8000, 59c84786faSRussell King MV_PCS_CSSR1_SPD1_100 = 0x4000, 60c84786faSRussell King MV_PCS_CSSR1_SPD1_10 = 0x0000, 61c84786faSRussell King MV_PCS_CSSR1_DUPLEX_FULL= BIT(13), 62c84786faSRussell King MV_PCS_CSSR1_RESOLVED = BIT(11), 63c84786faSRussell King MV_PCS_CSSR1_MDIX = BIT(6), 64c84786faSRussell King MV_PCS_CSSR1_SPD2_MASK = 0x000c, 65c84786faSRussell King MV_PCS_CSSR1_SPD2_5000 = 0x0008, 66c84786faSRussell King MV_PCS_CSSR1_SPD2_2500 = 0x0004, 67c84786faSRussell King MV_PCS_CSSR1_SPD2_10000 = 0x0000, 68ea4efe25SRussell King 6920b2af32SRussell King /* These registers appear at 0x800X and 0xa00X - the 0xa00X control 7020b2af32SRussell King * registers appear to set themselves to the 0x800X when AN is 7120b2af32SRussell King * restarted, but status registers appear readable from either. 7220b2af32SRussell King */ 7320b2af32SRussell King MV_AN_CTRL1000 = 0x8000, /* 1000base-T control register */ 7420b2af32SRussell King MV_AN_STAT1000 = 0x8001, /* 1000base-T status register */ 750d3ad854SRussell King 760d3ad854SRussell King /* Vendor2 MMD registers */ 77af3e28cbSAntoine Tenart MV_V2_PORT_CTRL = 0xf001, 788f48c2acSRussell King MV_V2_PORT_CTRL_SWRST = BIT(15), 798f48c2acSRussell King MV_V2_PORT_CTRL_PWRDOWN = BIT(11), 800d3ad854SRussell King MV_V2_TEMP_CTRL = 0xf08a, 810d3ad854SRussell King MV_V2_TEMP_CTRL_MASK = 0xc000, 820d3ad854SRussell King MV_V2_TEMP_CTRL_SAMPLE = 0x0000, 830d3ad854SRussell King MV_V2_TEMP_CTRL_DISABLE = 0xc000, 840d3ad854SRussell King MV_V2_TEMP = 0xf08c, 850d3ad854SRussell King MV_V2_TEMP_UNKNOWN = 0x9600, /* unknown function */ 860d3ad854SRussell King }; 870d3ad854SRussell King 880d3ad854SRussell King struct mv3310_priv { 89dd649b4fSRussell King u32 firmware_ver; 90dd649b4fSRussell King 910d3ad854SRussell King struct device *hwmon_dev; 920d3ad854SRussell King char *hwmon_name; 9320b2af32SRussell King }; 9420b2af32SRussell King 950d3ad854SRussell King #ifdef CONFIG_HWMON 960d3ad854SRussell King static umode_t mv3310_hwmon_is_visible(const void *data, 970d3ad854SRussell King enum hwmon_sensor_types type, 980d3ad854SRussell King u32 attr, int channel) 990d3ad854SRussell King { 1000d3ad854SRussell King if (type == hwmon_chip && attr == hwmon_chip_update_interval) 1010d3ad854SRussell King return 0444; 1020d3ad854SRussell King if (type == hwmon_temp && attr == hwmon_temp_input) 1030d3ad854SRussell King return 0444; 1040d3ad854SRussell King return 0; 1050d3ad854SRussell King } 1060d3ad854SRussell King 1070d3ad854SRussell King static int mv3310_hwmon_read(struct device *dev, enum hwmon_sensor_types type, 1080d3ad854SRussell King u32 attr, int channel, long *value) 1090d3ad854SRussell King { 1100d3ad854SRussell King struct phy_device *phydev = dev_get_drvdata(dev); 1110d3ad854SRussell King int temp; 1120d3ad854SRussell King 1130d3ad854SRussell King if (type == hwmon_chip && attr == hwmon_chip_update_interval) { 1140d3ad854SRussell King *value = MSEC_PER_SEC; 1150d3ad854SRussell King return 0; 1160d3ad854SRussell King } 1170d3ad854SRussell King 1180d3ad854SRussell King if (type == hwmon_temp && attr == hwmon_temp_input) { 1190d3ad854SRussell King temp = phy_read_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP); 1200d3ad854SRussell King if (temp < 0) 1210d3ad854SRussell King return temp; 1220d3ad854SRussell King 1230d3ad854SRussell King *value = ((temp & 0xff) - 75) * 1000; 1240d3ad854SRussell King 1250d3ad854SRussell King return 0; 1260d3ad854SRussell King } 1270d3ad854SRussell King 1280d3ad854SRussell King return -EOPNOTSUPP; 1290d3ad854SRussell King } 1300d3ad854SRussell King 1310d3ad854SRussell King static const struct hwmon_ops mv3310_hwmon_ops = { 1320d3ad854SRussell King .is_visible = mv3310_hwmon_is_visible, 1330d3ad854SRussell King .read = mv3310_hwmon_read, 1340d3ad854SRussell King }; 1350d3ad854SRussell King 1360d3ad854SRussell King static u32 mv3310_hwmon_chip_config[] = { 1370d3ad854SRussell King HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL, 1380d3ad854SRussell King 0, 1390d3ad854SRussell King }; 1400d3ad854SRussell King 1410d3ad854SRussell King static const struct hwmon_channel_info mv3310_hwmon_chip = { 1420d3ad854SRussell King .type = hwmon_chip, 1430d3ad854SRussell King .config = mv3310_hwmon_chip_config, 1440d3ad854SRussell King }; 1450d3ad854SRussell King 1460d3ad854SRussell King static u32 mv3310_hwmon_temp_config[] = { 1470d3ad854SRussell King HWMON_T_INPUT, 1480d3ad854SRussell King 0, 1490d3ad854SRussell King }; 1500d3ad854SRussell King 1510d3ad854SRussell King static const struct hwmon_channel_info mv3310_hwmon_temp = { 1520d3ad854SRussell King .type = hwmon_temp, 1530d3ad854SRussell King .config = mv3310_hwmon_temp_config, 1540d3ad854SRussell King }; 1550d3ad854SRussell King 1560d3ad854SRussell King static const struct hwmon_channel_info *mv3310_hwmon_info[] = { 1570d3ad854SRussell King &mv3310_hwmon_chip, 1580d3ad854SRussell King &mv3310_hwmon_temp, 1590d3ad854SRussell King NULL, 1600d3ad854SRussell King }; 1610d3ad854SRussell King 1620d3ad854SRussell King static const struct hwmon_chip_info mv3310_hwmon_chip_info = { 1630d3ad854SRussell King .ops = &mv3310_hwmon_ops, 1640d3ad854SRussell King .info = mv3310_hwmon_info, 1650d3ad854SRussell King }; 1660d3ad854SRussell King 1670d3ad854SRussell King static int mv3310_hwmon_config(struct phy_device *phydev, bool enable) 1680d3ad854SRussell King { 1690d3ad854SRussell King u16 val; 1700d3ad854SRussell King int ret; 1710d3ad854SRussell King 1720d3ad854SRussell King ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP, 1730d3ad854SRussell King MV_V2_TEMP_UNKNOWN); 1740d3ad854SRussell King if (ret < 0) 1750d3ad854SRussell King return ret; 1760d3ad854SRussell King 1770d3ad854SRussell King val = enable ? MV_V2_TEMP_CTRL_SAMPLE : MV_V2_TEMP_CTRL_DISABLE; 1780d3ad854SRussell King 179b06d8e5aSHeiner Kallweit return phy_modify_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP_CTRL, 180b06d8e5aSHeiner Kallweit MV_V2_TEMP_CTRL_MASK, val); 1810d3ad854SRussell King } 1820d3ad854SRussell King 1830d3ad854SRussell King static void mv3310_hwmon_disable(void *data) 1840d3ad854SRussell King { 1850d3ad854SRussell King struct phy_device *phydev = data; 1860d3ad854SRussell King 1870d3ad854SRussell King mv3310_hwmon_config(phydev, false); 1880d3ad854SRussell King } 1890d3ad854SRussell King 1900d3ad854SRussell King static int mv3310_hwmon_probe(struct phy_device *phydev) 1910d3ad854SRussell King { 1920d3ad854SRussell King struct device *dev = &phydev->mdio.dev; 1930d3ad854SRussell King struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev); 1940d3ad854SRussell King int i, j, ret; 1950d3ad854SRussell King 1960d3ad854SRussell King priv->hwmon_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL); 1970d3ad854SRussell King if (!priv->hwmon_name) 1980d3ad854SRussell King return -ENODEV; 1990d3ad854SRussell King 2000d3ad854SRussell King for (i = j = 0; priv->hwmon_name[i]; i++) { 2010d3ad854SRussell King if (isalnum(priv->hwmon_name[i])) { 2020d3ad854SRussell King if (i != j) 2030d3ad854SRussell King priv->hwmon_name[j] = priv->hwmon_name[i]; 2040d3ad854SRussell King j++; 2050d3ad854SRussell King } 2060d3ad854SRussell King } 2070d3ad854SRussell King priv->hwmon_name[j] = '\0'; 2080d3ad854SRussell King 2090d3ad854SRussell King ret = mv3310_hwmon_config(phydev, true); 2100d3ad854SRussell King if (ret) 2110d3ad854SRussell King return ret; 2120d3ad854SRussell King 2130d3ad854SRussell King ret = devm_add_action_or_reset(dev, mv3310_hwmon_disable, phydev); 2140d3ad854SRussell King if (ret) 2150d3ad854SRussell King return ret; 2160d3ad854SRussell King 2170d3ad854SRussell King priv->hwmon_dev = devm_hwmon_device_register_with_info(dev, 2180d3ad854SRussell King priv->hwmon_name, phydev, 2190d3ad854SRussell King &mv3310_hwmon_chip_info, NULL); 2200d3ad854SRussell King 2210d3ad854SRussell King return PTR_ERR_OR_ZERO(priv->hwmon_dev); 2220d3ad854SRussell King } 2230d3ad854SRussell King #else 2240d3ad854SRussell King static inline int mv3310_hwmon_config(struct phy_device *phydev, bool enable) 2250d3ad854SRussell King { 2260d3ad854SRussell King return 0; 2270d3ad854SRussell King } 2280d3ad854SRussell King 2290d3ad854SRussell King static int mv3310_hwmon_probe(struct phy_device *phydev) 2300d3ad854SRussell King { 2310d3ad854SRussell King return 0; 2320d3ad854SRussell King } 2330d3ad854SRussell King #endif 2340d3ad854SRussell King 235c9cc1c81SRussell King static int mv3310_power_down(struct phy_device *phydev) 236c9cc1c81SRussell King { 237c9cc1c81SRussell King return phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL, 238c9cc1c81SRussell King MV_V2_PORT_CTRL_PWRDOWN); 239c9cc1c81SRussell King } 240c9cc1c81SRussell King 241c9cc1c81SRussell King static int mv3310_power_up(struct phy_device *phydev) 242c9cc1c81SRussell King { 2438f48c2acSRussell King struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev); 2448f48c2acSRussell King int ret; 2458f48c2acSRussell King 2468f48c2acSRussell King ret = phy_clear_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL, 247c9cc1c81SRussell King MV_V2_PORT_CTRL_PWRDOWN); 2488f48c2acSRussell King 249*829e7573SBaruch Siach if (phydev->drv->phy_id != MARVELL_PHY_ID_88X3310 || 250*829e7573SBaruch Siach priv->firmware_ver < 0x00030000) 2518f48c2acSRussell King return ret; 2528f48c2acSRussell King 2538f48c2acSRussell King return phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL, 2548f48c2acSRussell King MV_V2_PORT_CTRL_SWRST); 255c9cc1c81SRussell King } 256c9cc1c81SRussell King 2578d8963c3SRussell King static int mv3310_reset(struct phy_device *phydev, u32 unit) 2588d8963c3SRussell King { 2598964a217SDejin Zheng int val, err; 2608d8963c3SRussell King 2618d8963c3SRussell King err = phy_modify_mmd(phydev, MDIO_MMD_PCS, unit + MDIO_CTRL1, 2628d8963c3SRussell King MDIO_CTRL1_RESET, MDIO_CTRL1_RESET); 2638d8963c3SRussell King if (err < 0) 2648d8963c3SRussell King return err; 2658d8963c3SRussell King 2668964a217SDejin Zheng return phy_read_mmd_poll_timeout(phydev, MDIO_MMD_PCS, 2678964a217SDejin Zheng unit + MDIO_CTRL1, val, 2688964a217SDejin Zheng !(val & MDIO_CTRL1_RESET), 2698964a217SDejin Zheng 5000, 100000, true); 2708d8963c3SRussell King } 2718d8963c3SRussell King 272a585c03eSRussell King static int mv3310_get_edpd(struct phy_device *phydev, u16 *edpd) 273a585c03eSRussell King { 274a585c03eSRussell King int val; 275a585c03eSRussell King 276a585c03eSRussell King val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1); 277a585c03eSRussell King if (val < 0) 278a585c03eSRussell King return val; 279a585c03eSRussell King 280a585c03eSRussell King switch (val & MV_PCS_CSCR1_ED_MASK) { 281a585c03eSRussell King case MV_PCS_CSCR1_ED_NLP: 282a585c03eSRussell King *edpd = 1000; 283a585c03eSRussell King break; 284a585c03eSRussell King case MV_PCS_CSCR1_ED_RX: 285a585c03eSRussell King *edpd = ETHTOOL_PHY_EDPD_NO_TX; 286a585c03eSRussell King break; 287a585c03eSRussell King default: 288a585c03eSRussell King *edpd = ETHTOOL_PHY_EDPD_DISABLE; 289a585c03eSRussell King break; 290a585c03eSRussell King } 291a585c03eSRussell King return 0; 292a585c03eSRussell King } 293a585c03eSRussell King 294a585c03eSRussell King static int mv3310_set_edpd(struct phy_device *phydev, u16 edpd) 295a585c03eSRussell King { 296a585c03eSRussell King u16 val; 297a585c03eSRussell King int err; 298a585c03eSRussell King 299a585c03eSRussell King switch (edpd) { 300a585c03eSRussell King case 1000: 301a585c03eSRussell King case ETHTOOL_PHY_EDPD_DFLT_TX_MSECS: 302a585c03eSRussell King val = MV_PCS_CSCR1_ED_NLP; 303a585c03eSRussell King break; 304a585c03eSRussell King 305a585c03eSRussell King case ETHTOOL_PHY_EDPD_NO_TX: 306a585c03eSRussell King val = MV_PCS_CSCR1_ED_RX; 307a585c03eSRussell King break; 308a585c03eSRussell King 309a585c03eSRussell King case ETHTOOL_PHY_EDPD_DISABLE: 310a585c03eSRussell King val = MV_PCS_CSCR1_ED_OFF; 311a585c03eSRussell King break; 312a585c03eSRussell King 313a585c03eSRussell King default: 314a585c03eSRussell King return -EINVAL; 315a585c03eSRussell King } 316a585c03eSRussell King 317a585c03eSRussell King err = phy_modify_mmd_changed(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1, 318a585c03eSRussell King MV_PCS_CSCR1_ED_MASK, val); 319a585c03eSRussell King if (err > 0) 320a585c03eSRussell King err = mv3310_reset(phydev, MV_PCS_BASE_T); 321a585c03eSRussell King 322a585c03eSRussell King return err; 323a585c03eSRussell King } 324a585c03eSRussell King 32536023da1SRussell King static int mv3310_sfp_insert(void *upstream, const struct sfp_eeprom_id *id) 32636023da1SRussell King { 32736023da1SRussell King struct phy_device *phydev = upstream; 32836023da1SRussell King __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, }; 32936023da1SRussell King phy_interface_t iface; 33036023da1SRussell King 33136023da1SRussell King sfp_parse_support(phydev->sfp_bus, id, support); 332a4516c70SRussell King iface = sfp_select_interface(phydev->sfp_bus, support); 33336023da1SRussell King 334e0f909bcSRussell King if (iface != PHY_INTERFACE_MODE_10GBASER) { 33536023da1SRussell King dev_err(&phydev->mdio.dev, "incompatible SFP module inserted\n"); 33636023da1SRussell King return -EINVAL; 33736023da1SRussell King } 33836023da1SRussell King return 0; 33936023da1SRussell King } 34036023da1SRussell King 34136023da1SRussell King static const struct sfp_upstream_ops mv3310_sfp_ops = { 34236023da1SRussell King .attach = phy_sfp_attach, 34336023da1SRussell King .detach = phy_sfp_detach, 34436023da1SRussell King .module_insert = mv3310_sfp_insert, 34536023da1SRussell King }; 34636023da1SRussell King 34720b2af32SRussell King static int mv3310_probe(struct phy_device *phydev) 34820b2af32SRussell King { 3490d3ad854SRussell King struct mv3310_priv *priv; 35020b2af32SRussell King u32 mmd_mask = MDIO_DEVS_PMAPMD | MDIO_DEVS_AN; 3510d3ad854SRussell King int ret; 35220b2af32SRussell King 35320b2af32SRussell King if (!phydev->is_c45 || 35420b2af32SRussell King (phydev->c45_ids.devices_in_package & mmd_mask) != mmd_mask) 35520b2af32SRussell King return -ENODEV; 35620b2af32SRussell King 3573d3ced2eSRussell King ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_BOOT); 3583d3ced2eSRussell King if (ret < 0) 3593d3ced2eSRussell King return ret; 3603d3ced2eSRussell King 3613d3ced2eSRussell King if (ret & MV_PMA_BOOT_FATAL) { 3623d3ced2eSRussell King dev_warn(&phydev->mdio.dev, 3633d3ced2eSRussell King "PHY failed to boot firmware, status=%04x\n", ret); 3643d3ced2eSRussell King return -ENODEV; 3653d3ced2eSRussell King } 3663d3ced2eSRussell King 3670d3ad854SRussell King priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL); 3680d3ad854SRussell King if (!priv) 3690d3ad854SRussell King return -ENOMEM; 3700d3ad854SRussell King 3710d3ad854SRussell King dev_set_drvdata(&phydev->mdio.dev, priv); 3720d3ad854SRussell King 373dd649b4fSRussell King ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_FW_VER0); 374dd649b4fSRussell King if (ret < 0) 375dd649b4fSRussell King return ret; 376dd649b4fSRussell King 377dd649b4fSRussell King priv->firmware_ver = ret << 16; 378dd649b4fSRussell King 379dd649b4fSRussell King ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_FW_VER1); 380dd649b4fSRussell King if (ret < 0) 381dd649b4fSRussell King return ret; 382dd649b4fSRussell King 383dd649b4fSRussell King priv->firmware_ver |= ret; 384dd649b4fSRussell King 385dd649b4fSRussell King phydev_info(phydev, "Firmware version %u.%u.%u.%u\n", 386dd649b4fSRussell King priv->firmware_ver >> 24, (priv->firmware_ver >> 16) & 255, 387dd649b4fSRussell King (priv->firmware_ver >> 8) & 255, priv->firmware_ver & 255); 388dd649b4fSRussell King 389c9cc1c81SRussell King /* Powering down the port when not in use saves about 600mW */ 390c9cc1c81SRussell King ret = mv3310_power_down(phydev); 391c9cc1c81SRussell King if (ret) 392c9cc1c81SRussell King return ret; 393c9cc1c81SRussell King 3940d3ad854SRussell King ret = mv3310_hwmon_probe(phydev); 3950d3ad854SRussell King if (ret) 3960d3ad854SRussell King return ret; 3970d3ad854SRussell King 39836023da1SRussell King return phy_sfp_probe(phydev, &mv3310_sfp_ops); 39920b2af32SRussell King } 40020b2af32SRussell King 4010d3ad854SRussell King static int mv3310_suspend(struct phy_device *phydev) 4020d3ad854SRussell King { 403c9cc1c81SRussell King return mv3310_power_down(phydev); 4040d3ad854SRussell King } 4050d3ad854SRussell King 4060d3ad854SRussell King static int mv3310_resume(struct phy_device *phydev) 4070d3ad854SRussell King { 408af3e28cbSAntoine Tenart int ret; 409af3e28cbSAntoine Tenart 410c9cc1c81SRussell King ret = mv3310_power_up(phydev); 411af3e28cbSAntoine Tenart if (ret) 412af3e28cbSAntoine Tenart return ret; 413af3e28cbSAntoine Tenart 4140d3ad854SRussell King return mv3310_hwmon_config(phydev, true); 4150d3ad854SRussell King } 4160d3ad854SRussell King 417c47455f9SMaxime Chevallier /* Some PHYs in the Alaska family such as the 88X3310 and the 88E2010 418c47455f9SMaxime Chevallier * don't set bit 14 in PMA Extended Abilities (1.11), although they do 419c47455f9SMaxime Chevallier * support 2.5GBASET and 5GBASET. For these models, we can still read their 420c47455f9SMaxime Chevallier * 2.5G/5G extended abilities register (1.21). We detect these models based on 421c47455f9SMaxime Chevallier * the PMA device identifier, with a mask matching models known to have this 422c47455f9SMaxime Chevallier * issue 423c47455f9SMaxime Chevallier */ 424c47455f9SMaxime Chevallier static bool mv3310_has_pma_ngbaset_quirk(struct phy_device *phydev) 425c47455f9SMaxime Chevallier { 426c47455f9SMaxime Chevallier if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_PMAPMD)) 427c47455f9SMaxime Chevallier return false; 428c47455f9SMaxime Chevallier 429c47455f9SMaxime Chevallier /* Only some revisions of the 88X3310 family PMA seem to be impacted */ 430c47455f9SMaxime Chevallier return (phydev->c45_ids.device_ids[MDIO_MMD_PMAPMD] & 431c47455f9SMaxime Chevallier MV_PHY_ALASKA_NBT_QUIRK_MASK) == MV_PHY_ALASKA_NBT_QUIRK_REV; 432c47455f9SMaxime Chevallier } 433c47455f9SMaxime Chevallier 43420b2af32SRussell King static int mv3310_config_init(struct phy_device *phydev) 43520b2af32SRussell King { 436c9cc1c81SRussell King int err; 437c9cc1c81SRussell King 43820b2af32SRussell King /* Check that the PHY interface type is compatible */ 43920b2af32SRussell King if (phydev->interface != PHY_INTERFACE_MODE_SGMII && 440e555e5b1SMaxime Chevallier phydev->interface != PHY_INTERFACE_MODE_2500BASEX && 44120b2af32SRussell King phydev->interface != PHY_INTERFACE_MODE_XAUI && 44220b2af32SRussell King phydev->interface != PHY_INTERFACE_MODE_RXAUI && 443e0f909bcSRussell King phydev->interface != PHY_INTERFACE_MODE_10GBASER) 44420b2af32SRussell King return -ENODEV; 44520b2af32SRussell King 4468d8963c3SRussell King phydev->mdix_ctrl = ETH_TP_MDI_AUTO; 4478d8963c3SRussell King 448c9cc1c81SRussell King /* Power up so reset works */ 449c9cc1c81SRussell King err = mv3310_power_up(phydev); 450c9cc1c81SRussell King if (err) 451c9cc1c81SRussell King return err; 452c9cc1c81SRussell King 453a585c03eSRussell King /* Enable EDPD mode - saving 600mW */ 454a585c03eSRussell King return mv3310_set_edpd(phydev, ETHTOOL_PHY_EDPD_DFLT_TX_MSECS); 45574145424SMaxime Chevallier } 45674145424SMaxime Chevallier 45774145424SMaxime Chevallier static int mv3310_get_features(struct phy_device *phydev) 45874145424SMaxime Chevallier { 45974145424SMaxime Chevallier int ret, val; 46074145424SMaxime Chevallier 461ac3f5533SMaxime Chevallier ret = genphy_c45_pma_read_abilities(phydev); 462ac3f5533SMaxime Chevallier if (ret) 463ac3f5533SMaxime Chevallier return ret; 46420b2af32SRussell King 465c47455f9SMaxime Chevallier if (mv3310_has_pma_ngbaset_quirk(phydev)) { 466c47455f9SMaxime Chevallier val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, 467c47455f9SMaxime Chevallier MDIO_PMA_NG_EXTABLE); 468c47455f9SMaxime Chevallier if (val < 0) 469c47455f9SMaxime Chevallier return val; 470c47455f9SMaxime Chevallier 471c47455f9SMaxime Chevallier linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 472c47455f9SMaxime Chevallier phydev->supported, 473c47455f9SMaxime Chevallier val & MDIO_PMA_NG_EXTABLE_2_5GBT); 474c47455f9SMaxime Chevallier 475c47455f9SMaxime Chevallier linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, 476c47455f9SMaxime Chevallier phydev->supported, 477c47455f9SMaxime Chevallier val & MDIO_PMA_NG_EXTABLE_5GBT); 478c47455f9SMaxime Chevallier } 479c47455f9SMaxime Chevallier 48020b2af32SRussell King return 0; 48120b2af32SRussell King } 48220b2af32SRussell King 4838d8963c3SRussell King static int mv3310_config_mdix(struct phy_device *phydev) 4848d8963c3SRussell King { 4858d8963c3SRussell King u16 val; 4868d8963c3SRussell King int err; 4878d8963c3SRussell King 4888d8963c3SRussell King switch (phydev->mdix_ctrl) { 4898d8963c3SRussell King case ETH_TP_MDI_AUTO: 4908d8963c3SRussell King val = MV_PCS_CSCR1_MDIX_AUTO; 4918d8963c3SRussell King break; 4928d8963c3SRussell King case ETH_TP_MDI_X: 4938d8963c3SRussell King val = MV_PCS_CSCR1_MDIX_MDIX; 4948d8963c3SRussell King break; 4958d8963c3SRussell King case ETH_TP_MDI: 4968d8963c3SRussell King val = MV_PCS_CSCR1_MDIX_MDI; 4978d8963c3SRussell King break; 4988d8963c3SRussell King default: 4998d8963c3SRussell King return -EINVAL; 5008d8963c3SRussell King } 5018d8963c3SRussell King 5028d8963c3SRussell King err = phy_modify_mmd_changed(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1, 5038d8963c3SRussell King MV_PCS_CSCR1_MDIX_MASK, val); 5048d8963c3SRussell King if (err > 0) 5058d8963c3SRussell King err = mv3310_reset(phydev, MV_PCS_BASE_T); 5068d8963c3SRussell King 5078d8963c3SRussell King return err; 5088d8963c3SRussell King } 5098d8963c3SRussell King 51020b2af32SRussell King static int mv3310_config_aneg(struct phy_device *phydev) 51120b2af32SRussell King { 51220b2af32SRussell King bool changed = false; 5133c1bcc86SAndrew Lunn u16 reg; 51420b2af32SRussell King int ret; 51520b2af32SRussell King 5168d8963c3SRussell King ret = mv3310_config_mdix(phydev); 5178d8963c3SRussell King if (ret < 0) 5188d8963c3SRussell King return ret; 519ea4efe25SRussell King 52030de65c3SHeiner Kallweit if (phydev->autoneg == AUTONEG_DISABLE) 52130de65c3SHeiner Kallweit return genphy_c45_pma_setup_forced(phydev); 52220b2af32SRussell King 5233de97f3cSAndrew Lunn ret = genphy_c45_an_config_aneg(phydev); 52420b2af32SRussell King if (ret < 0) 52520b2af32SRussell King return ret; 52620b2af32SRussell King if (ret > 0) 52720b2af32SRussell King changed = true; 52820b2af32SRussell King 5293de97f3cSAndrew Lunn /* Clause 45 has no standardized support for 1000BaseT, therefore 5303de97f3cSAndrew Lunn * use vendor registers for this mode. 5313de97f3cSAndrew Lunn */ 5323c1bcc86SAndrew Lunn reg = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising); 533b06d8e5aSHeiner Kallweit ret = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MV_AN_CTRL1000, 5343c1bcc86SAndrew Lunn ADVERTISE_1000FULL | ADVERTISE_1000HALF, reg); 53520b2af32SRussell King if (ret < 0) 53620b2af32SRussell King return ret; 53720b2af32SRussell King if (ret > 0) 53820b2af32SRussell King changed = true; 53920b2af32SRussell King 5406b4cb6cbSHeiner Kallweit return genphy_c45_check_and_restart_aneg(phydev, changed); 54120b2af32SRussell King } 54220b2af32SRussell King 54320b2af32SRussell King static int mv3310_aneg_done(struct phy_device *phydev) 54420b2af32SRussell King { 54520b2af32SRussell King int val; 54620b2af32SRussell King 54720b2af32SRussell King val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1); 54820b2af32SRussell King if (val < 0) 54920b2af32SRussell King return val; 55020b2af32SRussell King 55120b2af32SRussell King if (val & MDIO_STAT1_LSTATUS) 55220b2af32SRussell King return 1; 55320b2af32SRussell King 55420b2af32SRussell King return genphy_c45_aneg_done(phydev); 55520b2af32SRussell King } 55620b2af32SRussell King 55736c4449aSRussell King static void mv3310_update_interface(struct phy_device *phydev) 55836c4449aSRussell King { 55936c4449aSRussell King if ((phydev->interface == PHY_INTERFACE_MODE_SGMII || 560e555e5b1SMaxime Chevallier phydev->interface == PHY_INTERFACE_MODE_2500BASEX || 561e0f909bcSRussell King phydev->interface == PHY_INTERFACE_MODE_10GBASER) && 562e0f909bcSRussell King phydev->link) { 56336c4449aSRussell King /* The PHY automatically switches its serdes interface (and 564e0f909bcSRussell King * active PHYXS instance) between Cisco SGMII, 10GBase-R and 565e555e5b1SMaxime Chevallier * 2500BaseX modes according to the speed. Florian suggests 566e555e5b1SMaxime Chevallier * setting phydev->interface to communicate this to the MAC. 567e555e5b1SMaxime Chevallier * Only do this if we are already in one of the above modes. 56836c4449aSRussell King */ 569e555e5b1SMaxime Chevallier switch (phydev->speed) { 570e555e5b1SMaxime Chevallier case SPEED_10000: 571e0f909bcSRussell King phydev->interface = PHY_INTERFACE_MODE_10GBASER; 572e555e5b1SMaxime Chevallier break; 573e555e5b1SMaxime Chevallier case SPEED_2500: 574e555e5b1SMaxime Chevallier phydev->interface = PHY_INTERFACE_MODE_2500BASEX; 575e555e5b1SMaxime Chevallier break; 576e555e5b1SMaxime Chevallier case SPEED_1000: 577e555e5b1SMaxime Chevallier case SPEED_100: 578e555e5b1SMaxime Chevallier case SPEED_10: 57936c4449aSRussell King phydev->interface = PHY_INTERFACE_MODE_SGMII; 580e555e5b1SMaxime Chevallier break; 581e555e5b1SMaxime Chevallier default: 582e555e5b1SMaxime Chevallier break; 583e555e5b1SMaxime Chevallier } 58436c4449aSRussell King } 58536c4449aSRussell King } 58636c4449aSRussell King 58720b2af32SRussell King /* 10GBASE-ER,LR,LRM,SR do not support autonegotiation. */ 588c84786faSRussell King static int mv3310_read_status_10gbaser(struct phy_device *phydev) 58920b2af32SRussell King { 59020b2af32SRussell King phydev->link = 1; 59120b2af32SRussell King phydev->speed = SPEED_10000; 59220b2af32SRussell King phydev->duplex = DUPLEX_FULL; 59320b2af32SRussell King 59420b2af32SRussell King return 0; 59520b2af32SRussell King } 59620b2af32SRussell King 597c84786faSRussell King static int mv3310_read_status_copper(struct phy_device *phydev) 59820b2af32SRussell King { 599c84786faSRussell King int cssr1, speed, val; 60020b2af32SRussell King 601998a8a83SHeiner Kallweit val = genphy_c45_read_link(phydev); 60220b2af32SRussell King if (val < 0) 60320b2af32SRussell King return val; 60420b2af32SRussell King 60520b2af32SRussell King val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1); 60620b2af32SRussell King if (val < 0) 60720b2af32SRussell King return val; 60820b2af32SRussell King 609c84786faSRussell King cssr1 = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_CSSR1); 610c84786faSRussell King if (cssr1 < 0) 611c84786faSRussell King return val; 612c84786faSRussell King 613c84786faSRussell King /* If the link settings are not resolved, mark the link down */ 614c84786faSRussell King if (!(cssr1 & MV_PCS_CSSR1_RESOLVED)) { 615c84786faSRussell King phydev->link = 0; 616c84786faSRussell King return 0; 617c84786faSRussell King } 618c84786faSRussell King 619c84786faSRussell King /* Read the copper link settings */ 620c84786faSRussell King speed = cssr1 & MV_PCS_CSSR1_SPD1_MASK; 621c84786faSRussell King if (speed == MV_PCS_CSSR1_SPD1_SPD2) 622c84786faSRussell King speed |= cssr1 & MV_PCS_CSSR1_SPD2_MASK; 623c84786faSRussell King 624c84786faSRussell King switch (speed) { 625c84786faSRussell King case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_10000: 626c84786faSRussell King phydev->speed = SPEED_10000; 627c84786faSRussell King break; 628c84786faSRussell King 629c84786faSRussell King case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_5000: 630c84786faSRussell King phydev->speed = SPEED_5000; 631c84786faSRussell King break; 632c84786faSRussell King 633c84786faSRussell King case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_2500: 634c84786faSRussell King phydev->speed = SPEED_2500; 635c84786faSRussell King break; 636c84786faSRussell King 637c84786faSRussell King case MV_PCS_CSSR1_SPD1_1000: 638c84786faSRussell King phydev->speed = SPEED_1000; 639c84786faSRussell King break; 640c84786faSRussell King 641c84786faSRussell King case MV_PCS_CSSR1_SPD1_100: 642c84786faSRussell King phydev->speed = SPEED_100; 643c84786faSRussell King break; 644c84786faSRussell King 645c84786faSRussell King case MV_PCS_CSSR1_SPD1_10: 646c84786faSRussell King phydev->speed = SPEED_10; 647c84786faSRussell King break; 648c84786faSRussell King } 649c84786faSRussell King 650c84786faSRussell King phydev->duplex = cssr1 & MV_PCS_CSSR1_DUPLEX_FULL ? 651c84786faSRussell King DUPLEX_FULL : DUPLEX_HALF; 652c84786faSRussell King phydev->mdix = cssr1 & MV_PCS_CSSR1_MDIX ? 653c84786faSRussell King ETH_TP_MDI_X : ETH_TP_MDI; 654c84786faSRussell King 65520b2af32SRussell King if (val & MDIO_AN_STAT1_COMPLETE) { 65620b2af32SRussell King val = genphy_c45_read_lpa(phydev); 65720b2af32SRussell King if (val < 0) 65820b2af32SRussell King return val; 65920b2af32SRussell King 660cc1122b0SColin Ian King /* Read the link partner's 1G advertisement */ 66120b2af32SRussell King val = phy_read_mmd(phydev, MDIO_MMD_AN, MV_AN_STAT1000); 66220b2af32SRussell King if (val < 0) 66320b2af32SRussell King return val; 66420b2af32SRussell King 66578a24df3SAndrew Lunn mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, val); 66620b2af32SRussell King 667c84786faSRussell King /* Update the pause status */ 668c84786faSRussell King phy_resolve_aneg_pause(phydev); 66920b2af32SRussell King } 67020b2af32SRussell King 671c84786faSRussell King return 0; 67220b2af32SRussell King } 67320b2af32SRussell King 674c84786faSRussell King static int mv3310_read_status(struct phy_device *phydev) 675c84786faSRussell King { 676c84786faSRussell King int err, val; 677ea4efe25SRussell King 678c84786faSRussell King phydev->speed = SPEED_UNKNOWN; 679c84786faSRussell King phydev->duplex = DUPLEX_UNKNOWN; 680c84786faSRussell King linkmode_zero(phydev->lp_advertising); 681c84786faSRussell King phydev->link = 0; 682c84786faSRussell King phydev->pause = 0; 683c84786faSRussell King phydev->asym_pause = 0; 684ea4efe25SRussell King phydev->mdix = ETH_TP_MDI_INVALID; 685ea4efe25SRussell King 686c84786faSRussell King val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1); 687c84786faSRussell King if (val < 0) 688c84786faSRussell King return val; 689c84786faSRussell King 690c84786faSRussell King if (val & MDIO_STAT1_LSTATUS) 691c84786faSRussell King err = mv3310_read_status_10gbaser(phydev); 692c84786faSRussell King else 693c84786faSRussell King err = mv3310_read_status_copper(phydev); 694c84786faSRussell King if (err < 0) 695c84786faSRussell King return err; 696c84786faSRussell King 697c84786faSRussell King if (phydev->link) 69836c4449aSRussell King mv3310_update_interface(phydev); 69920b2af32SRussell King 70020b2af32SRussell King return 0; 70120b2af32SRussell King } 70220b2af32SRussell King 703a585c03eSRussell King static int mv3310_get_tunable(struct phy_device *phydev, 704a585c03eSRussell King struct ethtool_tunable *tuna, void *data) 705a585c03eSRussell King { 706a585c03eSRussell King switch (tuna->id) { 707a585c03eSRussell King case ETHTOOL_PHY_EDPD: 708a585c03eSRussell King return mv3310_get_edpd(phydev, data); 709a585c03eSRussell King default: 710a585c03eSRussell King return -EOPNOTSUPP; 711a585c03eSRussell King } 712a585c03eSRussell King } 713a585c03eSRussell King 714a585c03eSRussell King static int mv3310_set_tunable(struct phy_device *phydev, 715a585c03eSRussell King struct ethtool_tunable *tuna, const void *data) 716a585c03eSRussell King { 717a585c03eSRussell King switch (tuna->id) { 718a585c03eSRussell King case ETHTOOL_PHY_EDPD: 719a585c03eSRussell King return mv3310_set_edpd(phydev, *(u16 *)data); 720a585c03eSRussell King default: 721a585c03eSRussell King return -EOPNOTSUPP; 722a585c03eSRussell King } 723a585c03eSRussell King } 724a585c03eSRussell King 72520b2af32SRussell King static struct phy_driver mv3310_drivers[] = { 72620b2af32SRussell King { 727631ba906SMaxime Chevallier .phy_id = MARVELL_PHY_ID_88X3310, 728952b6b3bSAntoine Tenart .phy_id_mask = MARVELL_PHY_ID_MASK, 72920b2af32SRussell King .name = "mv88x3310", 73074145424SMaxime Chevallier .get_features = mv3310_get_features, 7317be3ad84SHeiner Kallweit .soft_reset = genphy_no_soft_reset, 73220b2af32SRussell King .config_init = mv3310_config_init, 7330d3ad854SRussell King .probe = mv3310_probe, 7340d3ad854SRussell King .suspend = mv3310_suspend, 7350d3ad854SRussell King .resume = mv3310_resume, 73620b2af32SRussell King .config_aneg = mv3310_config_aneg, 73720b2af32SRussell King .aneg_done = mv3310_aneg_done, 73820b2af32SRussell King .read_status = mv3310_read_status, 739a585c03eSRussell King .get_tunable = mv3310_get_tunable, 740a585c03eSRussell King .set_tunable = mv3310_set_tunable, 74120b2af32SRussell King }, 74262d01535SMaxime Chevallier { 74362d01535SMaxime Chevallier .phy_id = MARVELL_PHY_ID_88E2110, 74462d01535SMaxime Chevallier .phy_id_mask = MARVELL_PHY_ID_MASK, 74562d01535SMaxime Chevallier .name = "mv88x2110", 74662d01535SMaxime Chevallier .probe = mv3310_probe, 747e02c4a9dSAntoine Tenart .suspend = mv3310_suspend, 748e02c4a9dSAntoine Tenart .resume = mv3310_resume, 7497be3ad84SHeiner Kallweit .soft_reset = genphy_no_soft_reset, 75062d01535SMaxime Chevallier .config_init = mv3310_config_init, 75162d01535SMaxime Chevallier .config_aneg = mv3310_config_aneg, 75262d01535SMaxime Chevallier .aneg_done = mv3310_aneg_done, 75362d01535SMaxime Chevallier .read_status = mv3310_read_status, 754a585c03eSRussell King .get_tunable = mv3310_get_tunable, 755a585c03eSRussell King .set_tunable = mv3310_set_tunable, 75662d01535SMaxime Chevallier }, 75720b2af32SRussell King }; 75820b2af32SRussell King 75920b2af32SRussell King module_phy_driver(mv3310_drivers); 76020b2af32SRussell King 76120b2af32SRussell King static struct mdio_device_id __maybe_unused mv3310_tbl[] = { 762631ba906SMaxime Chevallier { MARVELL_PHY_ID_88X3310, MARVELL_PHY_ID_MASK }, 76362d01535SMaxime Chevallier { MARVELL_PHY_ID_88E2110, MARVELL_PHY_ID_MASK }, 76420b2af32SRussell King { }, 76520b2af32SRussell King }; 76620b2af32SRussell King MODULE_DEVICE_TABLE(mdio, mv3310_tbl); 76720b2af32SRussell King MODULE_DESCRIPTION("Marvell Alaska X 10Gigabit Ethernet PHY driver (MV88X3310)"); 76820b2af32SRussell King MODULE_LICENSE("GPL"); 769