120b2af32SRussell King /* 220b2af32SRussell King * Marvell 10G 88x3310 PHY driver 320b2af32SRussell King * 420b2af32SRussell King * Based upon the ID registers, this PHY appears to be a mixture of IPs 520b2af32SRussell King * from two different companies. 620b2af32SRussell King * 720b2af32SRussell King * There appears to be several different data paths through the PHY which 820b2af32SRussell King * are automatically managed by the PHY. The following has been determined 920b2af32SRussell King * via observation and experimentation: 1020b2af32SRussell King * 1120b2af32SRussell King * SGMII PHYXS -- BASE-T PCS -- 10G PMA -- AN -- Copper (for <= 1G) 1220b2af32SRussell King * 10GBASE-KR PHYXS -- BASE-T PCS -- 10G PMA -- AN -- Copper (for 10G) 1320b2af32SRussell King * 10GBASE-KR PHYXS -- BASE-R PCS -- Fiber 1420b2af32SRussell King * 1520b2af32SRussell King * If both the fiber and copper ports are connected, the first to gain 1620b2af32SRussell King * link takes priority and the other port is completely locked out. 1720b2af32SRussell King */ 1820b2af32SRussell King #include <linux/phy.h> 19*952b6b3bSAntoine Tenart #include <linux/marvell_phy.h> 2020b2af32SRussell King 2120b2af32SRussell King enum { 2220b2af32SRussell King MV_PCS_BASE_T = 0x0000, 2320b2af32SRussell King MV_PCS_BASE_R = 0x1000, 2420b2af32SRussell King MV_PCS_1000BASEX = 0x2000, 2520b2af32SRussell King 2620b2af32SRussell King /* These registers appear at 0x800X and 0xa00X - the 0xa00X control 2720b2af32SRussell King * registers appear to set themselves to the 0x800X when AN is 2820b2af32SRussell King * restarted, but status registers appear readable from either. 2920b2af32SRussell King */ 3020b2af32SRussell King MV_AN_CTRL1000 = 0x8000, /* 1000base-T control register */ 3120b2af32SRussell King MV_AN_STAT1000 = 0x8001, /* 1000base-T status register */ 3220b2af32SRussell King 3320b2af32SRussell King /* This register appears to reflect the copper status */ 3420b2af32SRussell King MV_AN_RESULT = 0xa016, 3520b2af32SRussell King MV_AN_RESULT_SPD_10 = BIT(12), 3620b2af32SRussell King MV_AN_RESULT_SPD_100 = BIT(13), 3720b2af32SRussell King MV_AN_RESULT_SPD_1000 = BIT(14), 3820b2af32SRussell King MV_AN_RESULT_SPD_10000 = BIT(15), 3920b2af32SRussell King }; 4020b2af32SRussell King 4120b2af32SRussell King static int mv3310_modify(struct phy_device *phydev, int devad, u16 reg, 4220b2af32SRussell King u16 mask, u16 bits) 4320b2af32SRussell King { 4420b2af32SRussell King int old, val, ret; 4520b2af32SRussell King 4620b2af32SRussell King old = phy_read_mmd(phydev, devad, reg); 4720b2af32SRussell King if (old < 0) 4820b2af32SRussell King return old; 4920b2af32SRussell King 5020b2af32SRussell King val = (old & ~mask) | (bits & mask); 5120b2af32SRussell King if (val == old) 5220b2af32SRussell King return 0; 5320b2af32SRussell King 5420b2af32SRussell King ret = phy_write_mmd(phydev, devad, reg, val); 5520b2af32SRussell King 5620b2af32SRussell King return ret < 0 ? ret : 1; 5720b2af32SRussell King } 5820b2af32SRussell King 5920b2af32SRussell King static int mv3310_probe(struct phy_device *phydev) 6020b2af32SRussell King { 6120b2af32SRussell King u32 mmd_mask = MDIO_DEVS_PMAPMD | MDIO_DEVS_AN; 6220b2af32SRussell King 6320b2af32SRussell King if (!phydev->is_c45 || 6420b2af32SRussell King (phydev->c45_ids.devices_in_package & mmd_mask) != mmd_mask) 6520b2af32SRussell King return -ENODEV; 6620b2af32SRussell King 6720b2af32SRussell King return 0; 6820b2af32SRussell King } 6920b2af32SRussell King 7020b2af32SRussell King /* 7120b2af32SRussell King * Resetting the MV88X3310 causes it to become non-responsive. Avoid 7220b2af32SRussell King * setting the reset bit(s). 7320b2af32SRussell King */ 7420b2af32SRussell King static int mv3310_soft_reset(struct phy_device *phydev) 7520b2af32SRussell King { 7620b2af32SRussell King return 0; 7720b2af32SRussell King } 7820b2af32SRussell King 7920b2af32SRussell King static int mv3310_config_init(struct phy_device *phydev) 8020b2af32SRussell King { 8120b2af32SRussell King __ETHTOOL_DECLARE_LINK_MODE_MASK(supported) = { 0, }; 8220b2af32SRussell King u32 mask; 8320b2af32SRussell King int val; 8420b2af32SRussell King 8520b2af32SRussell King /* Check that the PHY interface type is compatible */ 8620b2af32SRussell King if (phydev->interface != PHY_INTERFACE_MODE_SGMII && 8720b2af32SRussell King phydev->interface != PHY_INTERFACE_MODE_XGMII && 8820b2af32SRussell King phydev->interface != PHY_INTERFACE_MODE_XAUI && 8920b2af32SRussell King phydev->interface != PHY_INTERFACE_MODE_RXAUI && 9020b2af32SRussell King phydev->interface != PHY_INTERFACE_MODE_10GKR) 9120b2af32SRussell King return -ENODEV; 9220b2af32SRussell King 9320b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_Pause_BIT, supported); 9420b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, supported); 9520b2af32SRussell King 9620b2af32SRussell King if (phydev->c45_ids.devices_in_package & MDIO_DEVS_AN) { 9720b2af32SRussell King val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1); 9820b2af32SRussell King if (val < 0) 9920b2af32SRussell King return val; 10020b2af32SRussell King 10120b2af32SRussell King if (val & MDIO_AN_STAT1_ABLE) 10220b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, supported); 10320b2af32SRussell King } 10420b2af32SRussell King 10520b2af32SRussell King val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_STAT2); 10620b2af32SRussell King if (val < 0) 10720b2af32SRussell King return val; 10820b2af32SRussell King 10920b2af32SRussell King /* Ethtool does not support the WAN mode bits */ 11020b2af32SRussell King if (val & (MDIO_PMA_STAT2_10GBSR | MDIO_PMA_STAT2_10GBLR | 11120b2af32SRussell King MDIO_PMA_STAT2_10GBER | MDIO_PMA_STAT2_10GBLX4 | 11220b2af32SRussell King MDIO_PMA_STAT2_10GBSW | MDIO_PMA_STAT2_10GBLW | 11320b2af32SRussell King MDIO_PMA_STAT2_10GBEW)) 11420b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, supported); 11520b2af32SRussell King if (val & MDIO_PMA_STAT2_10GBSR) 11620b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_10000baseSR_Full_BIT, supported); 11720b2af32SRussell King if (val & MDIO_PMA_STAT2_10GBLR) 11820b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_10000baseLR_Full_BIT, supported); 11920b2af32SRussell King if (val & MDIO_PMA_STAT2_10GBER) 12020b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_10000baseER_Full_BIT, supported); 12120b2af32SRussell King 12220b2af32SRussell King if (val & MDIO_PMA_STAT2_EXTABLE) { 12320b2af32SRussell King val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_EXTABLE); 12420b2af32SRussell King if (val < 0) 12520b2af32SRussell King return val; 12620b2af32SRussell King 12720b2af32SRussell King if (val & (MDIO_PMA_EXTABLE_10GBT | MDIO_PMA_EXTABLE_1000BT | 12820b2af32SRussell King MDIO_PMA_EXTABLE_100BTX | MDIO_PMA_EXTABLE_10BT)) 12920b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_TP_BIT, supported); 13020b2af32SRussell King if (val & MDIO_PMA_EXTABLE_10GBLRM) 13120b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, supported); 13220b2af32SRussell King if (val & (MDIO_PMA_EXTABLE_10GBKX4 | MDIO_PMA_EXTABLE_10GBKR | 13320b2af32SRussell King MDIO_PMA_EXTABLE_1000BKX)) 13420b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_Backplane_BIT, supported); 13520b2af32SRussell King if (val & MDIO_PMA_EXTABLE_10GBLRM) 13620b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_10000baseLRM_Full_BIT, 13720b2af32SRussell King supported); 13820b2af32SRussell King if (val & MDIO_PMA_EXTABLE_10GBT) 13920b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, 14020b2af32SRussell King supported); 14120b2af32SRussell King if (val & MDIO_PMA_EXTABLE_10GBKX4) 14220b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, 14320b2af32SRussell King supported); 14420b2af32SRussell King if (val & MDIO_PMA_EXTABLE_10GBKR) 14520b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, 14620b2af32SRussell King supported); 14720b2af32SRussell King if (val & MDIO_PMA_EXTABLE_1000BT) 14820b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 14920b2af32SRussell King supported); 15020b2af32SRussell King if (val & MDIO_PMA_EXTABLE_1000BKX) 15120b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, 15220b2af32SRussell King supported); 15320b2af32SRussell King if (val & MDIO_PMA_EXTABLE_100BTX) 15420b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, 15520b2af32SRussell King supported); 15620b2af32SRussell King if (val & MDIO_PMA_EXTABLE_10BT) 15720b2af32SRussell King __set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, 15820b2af32SRussell King supported); 15920b2af32SRussell King } 16020b2af32SRussell King 16120b2af32SRussell King if (!ethtool_convert_link_mode_to_legacy_u32(&mask, supported)) 16220b2af32SRussell King dev_warn(&phydev->mdio.dev, 16320b2af32SRussell King "PHY supports (%*pb) more modes than phylib supports, some modes not supported.\n", 16420b2af32SRussell King __ETHTOOL_LINK_MODE_MASK_NBITS, supported); 16520b2af32SRussell King 16620b2af32SRussell King phydev->supported &= mask; 16720b2af32SRussell King phydev->advertising &= phydev->supported; 16820b2af32SRussell King 16920b2af32SRussell King return 0; 17020b2af32SRussell King } 17120b2af32SRussell King 17220b2af32SRussell King static int mv3310_config_aneg(struct phy_device *phydev) 17320b2af32SRussell King { 17420b2af32SRussell King bool changed = false; 17520b2af32SRussell King u32 advertising; 17620b2af32SRussell King int ret; 17720b2af32SRussell King 17820b2af32SRussell King if (phydev->autoneg == AUTONEG_DISABLE) { 17920b2af32SRussell King ret = genphy_c45_pma_setup_forced(phydev); 18020b2af32SRussell King if (ret < 0) 18120b2af32SRussell King return ret; 18220b2af32SRussell King 18320b2af32SRussell King return genphy_c45_an_disable_aneg(phydev); 18420b2af32SRussell King } 18520b2af32SRussell King 18620b2af32SRussell King phydev->advertising &= phydev->supported; 18720b2af32SRussell King advertising = phydev->advertising; 18820b2af32SRussell King 18920b2af32SRussell King ret = mv3310_modify(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE, 19020b2af32SRussell King ADVERTISE_ALL | ADVERTISE_100BASE4 | 19120b2af32SRussell King ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM, 19220b2af32SRussell King ethtool_adv_to_mii_adv_t(advertising)); 19320b2af32SRussell King if (ret < 0) 19420b2af32SRussell King return ret; 19520b2af32SRussell King if (ret > 0) 19620b2af32SRussell King changed = true; 19720b2af32SRussell King 19820b2af32SRussell King ret = mv3310_modify(phydev, MDIO_MMD_AN, MV_AN_CTRL1000, 19920b2af32SRussell King ADVERTISE_1000FULL | ADVERTISE_1000HALF, 20020b2af32SRussell King ethtool_adv_to_mii_ctrl1000_t(advertising)); 20120b2af32SRussell King if (ret < 0) 20220b2af32SRussell King return ret; 20320b2af32SRussell King if (ret > 0) 20420b2af32SRussell King changed = true; 20520b2af32SRussell King 20620b2af32SRussell King /* 10G control register */ 20720b2af32SRussell King ret = mv3310_modify(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL, 20820b2af32SRussell King MDIO_AN_10GBT_CTRL_ADV10G, 20920b2af32SRussell King advertising & ADVERTISED_10000baseT_Full ? 21020b2af32SRussell King MDIO_AN_10GBT_CTRL_ADV10G : 0); 21120b2af32SRussell King if (ret < 0) 21220b2af32SRussell King return ret; 21320b2af32SRussell King if (ret > 0) 21420b2af32SRussell King changed = true; 21520b2af32SRussell King 21620b2af32SRussell King if (changed) 21720b2af32SRussell King ret = genphy_c45_restart_aneg(phydev); 21820b2af32SRussell King 21920b2af32SRussell King return ret; 22020b2af32SRussell King } 22120b2af32SRussell King 22220b2af32SRussell King static int mv3310_aneg_done(struct phy_device *phydev) 22320b2af32SRussell King { 22420b2af32SRussell King int val; 22520b2af32SRussell King 22620b2af32SRussell King val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1); 22720b2af32SRussell King if (val < 0) 22820b2af32SRussell King return val; 22920b2af32SRussell King 23020b2af32SRussell King if (val & MDIO_STAT1_LSTATUS) 23120b2af32SRussell King return 1; 23220b2af32SRussell King 23320b2af32SRussell King return genphy_c45_aneg_done(phydev); 23420b2af32SRussell King } 23520b2af32SRussell King 23620b2af32SRussell King /* 10GBASE-ER,LR,LRM,SR do not support autonegotiation. */ 23720b2af32SRussell King static int mv3310_read_10gbr_status(struct phy_device *phydev) 23820b2af32SRussell King { 23920b2af32SRussell King phydev->link = 1; 24020b2af32SRussell King phydev->speed = SPEED_10000; 24120b2af32SRussell King phydev->duplex = DUPLEX_FULL; 24220b2af32SRussell King 24320b2af32SRussell King if (phydev->interface == PHY_INTERFACE_MODE_SGMII) 24420b2af32SRussell King phydev->interface = PHY_INTERFACE_MODE_10GKR; 24520b2af32SRussell King 24620b2af32SRussell King return 0; 24720b2af32SRussell King } 24820b2af32SRussell King 24920b2af32SRussell King static int mv3310_read_status(struct phy_device *phydev) 25020b2af32SRussell King { 25120b2af32SRussell King u32 mmd_mask = phydev->c45_ids.devices_in_package; 25220b2af32SRussell King int val; 25320b2af32SRussell King 25420b2af32SRussell King /* The vendor devads do not report link status. Avoid the PHYXS 25520b2af32SRussell King * instance as there are three, and its status depends on the MAC 25620b2af32SRussell King * being appropriately configured for the negotiated speed. 25720b2af32SRussell King */ 25820b2af32SRussell King mmd_mask &= ~(BIT(MDIO_MMD_VEND1) | BIT(MDIO_MMD_VEND2) | 25920b2af32SRussell King BIT(MDIO_MMD_PHYXS)); 26020b2af32SRussell King 26120b2af32SRussell King phydev->speed = SPEED_UNKNOWN; 26220b2af32SRussell King phydev->duplex = DUPLEX_UNKNOWN; 26320b2af32SRussell King phydev->lp_advertising = 0; 26420b2af32SRussell King phydev->link = 0; 26520b2af32SRussell King phydev->pause = 0; 26620b2af32SRussell King phydev->asym_pause = 0; 26720b2af32SRussell King 26820b2af32SRussell King val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1); 26920b2af32SRussell King if (val < 0) 27020b2af32SRussell King return val; 27120b2af32SRussell King 27220b2af32SRussell King if (val & MDIO_STAT1_LSTATUS) 27320b2af32SRussell King return mv3310_read_10gbr_status(phydev); 27420b2af32SRussell King 27520b2af32SRussell King val = genphy_c45_read_link(phydev, mmd_mask); 27620b2af32SRussell King if (val < 0) 27720b2af32SRussell King return val; 27820b2af32SRussell King 27920b2af32SRussell King phydev->link = val > 0 ? 1 : 0; 28020b2af32SRussell King 28120b2af32SRussell King val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1); 28220b2af32SRussell King if (val < 0) 28320b2af32SRussell King return val; 28420b2af32SRussell King 28520b2af32SRussell King if (val & MDIO_AN_STAT1_COMPLETE) { 28620b2af32SRussell King val = genphy_c45_read_lpa(phydev); 28720b2af32SRussell King if (val < 0) 28820b2af32SRussell King return val; 28920b2af32SRussell King 29020b2af32SRussell King /* Read the link partner's 1G advertisment */ 29120b2af32SRussell King val = phy_read_mmd(phydev, MDIO_MMD_AN, MV_AN_STAT1000); 29220b2af32SRussell King if (val < 0) 29320b2af32SRussell King return val; 29420b2af32SRussell King 29520b2af32SRussell King phydev->lp_advertising |= mii_stat1000_to_ethtool_lpa_t(val); 29620b2af32SRussell King 29720b2af32SRussell King if (phydev->autoneg == AUTONEG_ENABLE) { 29820b2af32SRussell King val = phy_read_mmd(phydev, MDIO_MMD_AN, MV_AN_RESULT); 29920b2af32SRussell King if (val < 0) 30020b2af32SRussell King return val; 30120b2af32SRussell King 30220b2af32SRussell King if (val & MV_AN_RESULT_SPD_10000) 30320b2af32SRussell King phydev->speed = SPEED_10000; 30420b2af32SRussell King else if (val & MV_AN_RESULT_SPD_1000) 30520b2af32SRussell King phydev->speed = SPEED_1000; 30620b2af32SRussell King else if (val & MV_AN_RESULT_SPD_100) 30720b2af32SRussell King phydev->speed = SPEED_100; 30820b2af32SRussell King else if (val & MV_AN_RESULT_SPD_10) 30920b2af32SRussell King phydev->speed = SPEED_10; 31020b2af32SRussell King 31120b2af32SRussell King phydev->duplex = DUPLEX_FULL; 31220b2af32SRussell King } 31320b2af32SRussell King } 31420b2af32SRussell King 31520b2af32SRussell King if (phydev->autoneg != AUTONEG_ENABLE) { 31620b2af32SRussell King val = genphy_c45_read_pma(phydev); 31720b2af32SRussell King if (val < 0) 31820b2af32SRussell King return val; 31920b2af32SRussell King } 32020b2af32SRussell King 32120b2af32SRussell King if ((phydev->interface == PHY_INTERFACE_MODE_SGMII || 32220b2af32SRussell King phydev->interface == PHY_INTERFACE_MODE_10GKR) && phydev->link) { 32320b2af32SRussell King /* The PHY automatically switches its serdes interface (and 32420b2af32SRussell King * active PHYXS instance) between Cisco SGMII and 10GBase-KR 32520b2af32SRussell King * modes according to the speed. Florian suggests setting 32620b2af32SRussell King * phydev->interface to communicate this to the MAC. Only do 32720b2af32SRussell King * this if we are already in either SGMII or 10GBase-KR mode. 32820b2af32SRussell King */ 32920b2af32SRussell King if (phydev->speed == SPEED_10000) 33020b2af32SRussell King phydev->interface = PHY_INTERFACE_MODE_10GKR; 33120b2af32SRussell King else if (phydev->speed >= SPEED_10 && 33220b2af32SRussell King phydev->speed < SPEED_10000) 33320b2af32SRussell King phydev->interface = PHY_INTERFACE_MODE_SGMII; 33420b2af32SRussell King } 33520b2af32SRussell King 33620b2af32SRussell King return 0; 33720b2af32SRussell King } 33820b2af32SRussell King 33920b2af32SRussell King static struct phy_driver mv3310_drivers[] = { 34020b2af32SRussell King { 34120b2af32SRussell King .phy_id = 0x002b09aa, 342*952b6b3bSAntoine Tenart .phy_id_mask = MARVELL_PHY_ID_MASK, 34320b2af32SRussell King .name = "mv88x3310", 34420b2af32SRussell King .features = SUPPORTED_10baseT_Full | 34520b2af32SRussell King SUPPORTED_100baseT_Full | 34620b2af32SRussell King SUPPORTED_1000baseT_Full | 34720b2af32SRussell King SUPPORTED_Autoneg | 34820b2af32SRussell King SUPPORTED_TP | 34920b2af32SRussell King SUPPORTED_FIBRE | 35020b2af32SRussell King SUPPORTED_10000baseT_Full | 35120b2af32SRussell King SUPPORTED_Backplane, 35220b2af32SRussell King .probe = mv3310_probe, 35320b2af32SRussell King .soft_reset = mv3310_soft_reset, 35420b2af32SRussell King .config_init = mv3310_config_init, 35520b2af32SRussell King .config_aneg = mv3310_config_aneg, 35620b2af32SRussell King .aneg_done = mv3310_aneg_done, 35720b2af32SRussell King .read_status = mv3310_read_status, 35820b2af32SRussell King }, 35920b2af32SRussell King }; 36020b2af32SRussell King 36120b2af32SRussell King module_phy_driver(mv3310_drivers); 36220b2af32SRussell King 36320b2af32SRussell King static struct mdio_device_id __maybe_unused mv3310_tbl[] = { 364*952b6b3bSAntoine Tenart { 0x002b09aa, MARVELL_PHY_ID_MASK }, 36520b2af32SRussell King { }, 36620b2af32SRussell King }; 36720b2af32SRussell King MODULE_DEVICE_TABLE(mdio, mv3310_tbl); 36820b2af32SRussell King MODULE_DESCRIPTION("Marvell Alaska X 10Gigabit Ethernet PHY driver (MV88X3310)"); 36920b2af32SRussell King MODULE_LICENSE("GPL"); 370