2 * Marvell 10G 88x3310 PHY driver
4 * Based upon the ID registers, this PHY appears to be a mixture of IPs
5 * from two different companies.
7 * There appears to be several different data paths through the PHY which
8 * are automatically managed by the PHY. The following has been determined
9 * via observation and experimentation for a setup using single-lane Serdes:
11 * SGMII PHYXS -- BASE-T PCS -- 10G PMA -- AN -- Copper (for <= 1G)
12 * 10GBASE-KR PHYXS -- BASE-T PCS -- 10G PMA -- AN -- Copper (for 10G)
13 * 10GBASE-KR PHYXS -- BASE-R PCS -- Fiber
15 * With XAUI, observation shows:
17 * XAUI PHYXS -- <appropriate PCS as above>
19 * and no switching of the host interface mode occurs.
21 * If both the fiber and copper ports are connected, the first to gain
22 * link takes priority and the other port is completely locked out.
24 #include <linux/ctype.h>
25 #include <linux/hwmon.h>
26 #include <linux/marvell_phy.h>
27 #include <linux/phy.h>
31 MV_PMA_BOOT_FATAL = BIT(0),
33 MV_PCS_BASE_T = 0x0000,
34 MV_PCS_BASE_R = 0x1000,
35 MV_PCS_1000BASEX = 0x2000,
37 MV_PCS_PAIRSWAP = 0x8182,
38 MV_PCS_PAIRSWAP_MASK = 0x0003,
39 MV_PCS_PAIRSWAP_AB = 0x0002,
40 MV_PCS_PAIRSWAP_NONE = 0x0003,
42 /* These registers appear at 0x800X and 0xa00X - the 0xa00X control
43 * registers appear to set themselves to the 0x800X when AN is
44 * restarted, but status registers appear readable from either.
46 MV_AN_CTRL1000 = 0x8000, /* 1000base-T control register */
47 MV_AN_STAT1000 = 0x8001, /* 1000base-T status register */
49 /* Vendor2 MMD registers */
50 MV_V2_TEMP_CTRL = 0xf08a,
51 MV_V2_TEMP_CTRL_MASK = 0xc000,
52 MV_V2_TEMP_CTRL_SAMPLE = 0x0000,
53 MV_V2_TEMP_CTRL_DISABLE = 0xc000,
55 MV_V2_TEMP_UNKNOWN = 0x9600, /* unknown function */
59 struct device *hwmon_dev;
63 static int mv3310_modify(struct phy_device *phydev, int devad, u16 reg,
68 old = phy_read_mmd(phydev, devad, reg);
72 val = (old & ~mask) | (bits & mask);
76 ret = phy_write_mmd(phydev, devad, reg, val);
78 return ret < 0 ? ret : 1;
82 static umode_t mv3310_hwmon_is_visible(const void *data,
83 enum hwmon_sensor_types type,
84 u32 attr, int channel)
86 if (type == hwmon_chip && attr == hwmon_chip_update_interval)
88 if (type == hwmon_temp && attr == hwmon_temp_input)
93 static int mv3310_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
94 u32 attr, int channel, long *value)
96 struct phy_device *phydev = dev_get_drvdata(dev);
99 if (type == hwmon_chip && attr == hwmon_chip_update_interval) {
100 *value = MSEC_PER_SEC;
104 if (type == hwmon_temp && attr == hwmon_temp_input) {
105 temp = phy_read_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP);
109 *value = ((temp & 0xff) - 75) * 1000;
117 static const struct hwmon_ops mv3310_hwmon_ops = {
118 .is_visible = mv3310_hwmon_is_visible,
119 .read = mv3310_hwmon_read,
122 static u32 mv3310_hwmon_chip_config[] = {
123 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
127 static const struct hwmon_channel_info mv3310_hwmon_chip = {
129 .config = mv3310_hwmon_chip_config,
132 static u32 mv3310_hwmon_temp_config[] = {
137 static const struct hwmon_channel_info mv3310_hwmon_temp = {
139 .config = mv3310_hwmon_temp_config,
142 static const struct hwmon_channel_info *mv3310_hwmon_info[] = {
148 static const struct hwmon_chip_info mv3310_hwmon_chip_info = {
149 .ops = &mv3310_hwmon_ops,
150 .info = mv3310_hwmon_info,
153 static int mv3310_hwmon_config(struct phy_device *phydev, bool enable)
158 ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP,
163 val = enable ? MV_V2_TEMP_CTRL_SAMPLE : MV_V2_TEMP_CTRL_DISABLE;
164 ret = mv3310_modify(phydev, MDIO_MMD_VEND2, MV_V2_TEMP_CTRL,
165 MV_V2_TEMP_CTRL_MASK, val);
167 return ret < 0 ? ret : 0;
170 static void mv3310_hwmon_disable(void *data)
172 struct phy_device *phydev = data;
174 mv3310_hwmon_config(phydev, false);
177 static int mv3310_hwmon_probe(struct phy_device *phydev)
179 struct device *dev = &phydev->mdio.dev;
180 struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
183 priv->hwmon_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
184 if (!priv->hwmon_name)
187 for (i = j = 0; priv->hwmon_name[i]; i++) {
188 if (isalnum(priv->hwmon_name[i])) {
190 priv->hwmon_name[j] = priv->hwmon_name[i];
194 priv->hwmon_name[j] = '\0';
196 ret = mv3310_hwmon_config(phydev, true);
200 ret = devm_add_action_or_reset(dev, mv3310_hwmon_disable, phydev);
204 priv->hwmon_dev = devm_hwmon_device_register_with_info(dev,
205 priv->hwmon_name, phydev,
206 &mv3310_hwmon_chip_info, NULL);
208 return PTR_ERR_OR_ZERO(priv->hwmon_dev);
211 static inline int mv3310_hwmon_config(struct phy_device *phydev, bool enable)
216 static int mv3310_hwmon_probe(struct phy_device *phydev)
222 static int mv3310_probe(struct phy_device *phydev)
224 struct mv3310_priv *priv;
225 u32 mmd_mask = MDIO_DEVS_PMAPMD | MDIO_DEVS_AN;
228 if (!phydev->is_c45 ||
229 (phydev->c45_ids.devices_in_package & mmd_mask) != mmd_mask)
232 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_BOOT);
236 if (ret & MV_PMA_BOOT_FATAL) {
237 dev_warn(&phydev->mdio.dev,
238 "PHY failed to boot firmware, status=%04x\n", ret);
242 priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
246 dev_set_drvdata(&phydev->mdio.dev, priv);
248 ret = mv3310_hwmon_probe(phydev);
255 static int mv3310_suspend(struct phy_device *phydev)
260 static int mv3310_resume(struct phy_device *phydev)
262 return mv3310_hwmon_config(phydev, true);
265 static int mv3310_config_init(struct phy_device *phydev)
267 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported) = { 0, };
271 /* Check that the PHY interface type is compatible */
272 if (phydev->interface != PHY_INTERFACE_MODE_SGMII &&
273 phydev->interface != PHY_INTERFACE_MODE_XAUI &&
274 phydev->interface != PHY_INTERFACE_MODE_RXAUI &&
275 phydev->interface != PHY_INTERFACE_MODE_10GKR)
278 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT, supported);
279 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, supported);
281 if (phydev->c45_ids.devices_in_package & MDIO_DEVS_AN) {
282 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
286 if (val & MDIO_AN_STAT1_ABLE)
287 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, supported);
290 val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_STAT2);
294 /* Ethtool does not support the WAN mode bits */
295 if (val & (MDIO_PMA_STAT2_10GBSR | MDIO_PMA_STAT2_10GBLR |
296 MDIO_PMA_STAT2_10GBER | MDIO_PMA_STAT2_10GBLX4 |
297 MDIO_PMA_STAT2_10GBSW | MDIO_PMA_STAT2_10GBLW |
298 MDIO_PMA_STAT2_10GBEW))
299 __set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, supported);
300 if (val & MDIO_PMA_STAT2_10GBSR)
301 __set_bit(ETHTOOL_LINK_MODE_10000baseSR_Full_BIT, supported);
302 if (val & MDIO_PMA_STAT2_10GBLR)
303 __set_bit(ETHTOOL_LINK_MODE_10000baseLR_Full_BIT, supported);
304 if (val & MDIO_PMA_STAT2_10GBER)
305 __set_bit(ETHTOOL_LINK_MODE_10000baseER_Full_BIT, supported);
307 if (val & MDIO_PMA_STAT2_EXTABLE) {
308 val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_EXTABLE);
312 if (val & (MDIO_PMA_EXTABLE_10GBT | MDIO_PMA_EXTABLE_1000BT |
313 MDIO_PMA_EXTABLE_100BTX | MDIO_PMA_EXTABLE_10BT))
314 __set_bit(ETHTOOL_LINK_MODE_TP_BIT, supported);
315 if (val & MDIO_PMA_EXTABLE_10GBLRM)
316 __set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, supported);
317 if (val & (MDIO_PMA_EXTABLE_10GBKX4 | MDIO_PMA_EXTABLE_10GBKR |
318 MDIO_PMA_EXTABLE_1000BKX))
319 __set_bit(ETHTOOL_LINK_MODE_Backplane_BIT, supported);
320 if (val & MDIO_PMA_EXTABLE_10GBLRM)
321 __set_bit(ETHTOOL_LINK_MODE_10000baseLRM_Full_BIT,
323 if (val & MDIO_PMA_EXTABLE_10GBT)
324 __set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
326 if (val & MDIO_PMA_EXTABLE_10GBKX4)
327 __set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
329 if (val & MDIO_PMA_EXTABLE_10GBKR)
330 __set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
332 if (val & MDIO_PMA_EXTABLE_1000BT)
333 __set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
335 if (val & MDIO_PMA_EXTABLE_1000BKX)
336 __set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
338 if (val & MDIO_PMA_EXTABLE_100BTX) {
339 __set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
341 __set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
344 if (val & MDIO_PMA_EXTABLE_10BT) {
345 __set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
347 __set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
352 if (!ethtool_convert_link_mode_to_legacy_u32(&mask, supported))
353 dev_warn(&phydev->mdio.dev,
354 "PHY supports (%*pb) more modes than phylib supports, some modes not supported.\n",
355 __ETHTOOL_LINK_MODE_MASK_NBITS, supported);
357 phydev->supported &= mask;
358 phydev->advertising &= phydev->supported;
363 static int mv3310_config_aneg(struct phy_device *phydev)
365 bool changed = false;
369 /* We don't support manual MDI control */
370 phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
372 if (phydev->autoneg == AUTONEG_DISABLE) {
373 ret = genphy_c45_pma_setup_forced(phydev);
377 return genphy_c45_an_disable_aneg(phydev);
380 phydev->advertising &= phydev->supported;
381 advertising = phydev->advertising;
383 ret = mv3310_modify(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE,
384 ADVERTISE_ALL | ADVERTISE_100BASE4 |
385 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
386 ethtool_adv_to_mii_adv_t(advertising));
392 ret = mv3310_modify(phydev, MDIO_MMD_AN, MV_AN_CTRL1000,
393 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
394 ethtool_adv_to_mii_ctrl1000_t(advertising));
400 /* 10G control register */
401 ret = mv3310_modify(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL,
402 MDIO_AN_10GBT_CTRL_ADV10G,
403 advertising & ADVERTISED_10000baseT_Full ?
404 MDIO_AN_10GBT_CTRL_ADV10G : 0);
411 ret = genphy_c45_restart_aneg(phydev);
416 static int mv3310_aneg_done(struct phy_device *phydev)
420 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
424 if (val & MDIO_STAT1_LSTATUS)
427 return genphy_c45_aneg_done(phydev);
430 static void mv3310_update_interface(struct phy_device *phydev)
432 if ((phydev->interface == PHY_INTERFACE_MODE_SGMII ||
433 phydev->interface == PHY_INTERFACE_MODE_10GKR) && phydev->link) {
434 /* The PHY automatically switches its serdes interface (and
435 * active PHYXS instance) between Cisco SGMII and 10GBase-KR
436 * modes according to the speed. Florian suggests setting
437 * phydev->interface to communicate this to the MAC. Only do
438 * this if we are already in either SGMII or 10GBase-KR mode.
440 if (phydev->speed == SPEED_10000)
441 phydev->interface = PHY_INTERFACE_MODE_10GKR;
442 else if (phydev->speed >= SPEED_10 &&
443 phydev->speed < SPEED_10000)
444 phydev->interface = PHY_INTERFACE_MODE_SGMII;
448 /* 10GBASE-ER,LR,LRM,SR do not support autonegotiation. */
449 static int mv3310_read_10gbr_status(struct phy_device *phydev)
452 phydev->speed = SPEED_10000;
453 phydev->duplex = DUPLEX_FULL;
455 mv3310_update_interface(phydev);
460 static int mv3310_read_status(struct phy_device *phydev)
462 u32 mmd_mask = phydev->c45_ids.devices_in_package;
465 /* The vendor devads do not report link status. Avoid the PHYXS
466 * instance as there are three, and its status depends on the MAC
467 * being appropriately configured for the negotiated speed.
469 mmd_mask &= ~(BIT(MDIO_MMD_VEND1) | BIT(MDIO_MMD_VEND2) |
470 BIT(MDIO_MMD_PHYXS));
472 phydev->speed = SPEED_UNKNOWN;
473 phydev->duplex = DUPLEX_UNKNOWN;
474 phydev->lp_advertising = 0;
477 phydev->asym_pause = 0;
480 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
484 if (val & MDIO_STAT1_LSTATUS)
485 return mv3310_read_10gbr_status(phydev);
487 val = genphy_c45_read_link(phydev, mmd_mask);
491 phydev->link = val > 0 ? 1 : 0;
493 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
497 if (val & MDIO_AN_STAT1_COMPLETE) {
498 val = genphy_c45_read_lpa(phydev);
502 /* Read the link partner's 1G advertisement */
503 val = phy_read_mmd(phydev, MDIO_MMD_AN, MV_AN_STAT1000);
507 phydev->lp_advertising |= mii_stat1000_to_ethtool_lpa_t(val);
509 if (phydev->autoneg == AUTONEG_ENABLE)
510 phy_resolve_aneg_linkmode(phydev);
513 if (phydev->autoneg != AUTONEG_ENABLE) {
514 val = genphy_c45_read_pma(phydev);
519 if (phydev->speed == SPEED_10000) {
520 val = genphy_c45_read_mdix(phydev);
524 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_PAIRSWAP);
528 switch (val & MV_PCS_PAIRSWAP_MASK) {
529 case MV_PCS_PAIRSWAP_AB:
530 phydev->mdix = ETH_TP_MDI_X;
532 case MV_PCS_PAIRSWAP_NONE:
533 phydev->mdix = ETH_TP_MDI;
536 phydev->mdix = ETH_TP_MDI_INVALID;
541 mv3310_update_interface(phydev);
546 static struct phy_driver mv3310_drivers[] = {
548 .phy_id = 0x002b09aa,
549 .phy_id_mask = MARVELL_PHY_ID_MASK,
551 .features = SUPPORTED_10baseT_Full |
552 SUPPORTED_10baseT_Half |
553 SUPPORTED_100baseT_Full |
554 SUPPORTED_100baseT_Half |
555 SUPPORTED_1000baseT_Full |
559 SUPPORTED_10000baseT_Full |
561 .soft_reset = gen10g_no_soft_reset,
562 .config_init = mv3310_config_init,
563 .probe = mv3310_probe,
564 .suspend = mv3310_suspend,
565 .resume = mv3310_resume,
566 .config_aneg = mv3310_config_aneg,
567 .aneg_done = mv3310_aneg_done,
568 .read_status = mv3310_read_status,
572 module_phy_driver(mv3310_drivers);
574 static struct mdio_device_id __maybe_unused mv3310_tbl[] = {
575 { 0x002b09aa, MARVELL_PHY_ID_MASK },
578 MODULE_DEVICE_TABLE(mdio, mv3310_tbl);
579 MODULE_DESCRIPTION("Marvell Alaska X 10Gigabit Ethernet PHY driver (MV88X3310)");
580 MODULE_LICENSE("GPL");