1 // SPDX-License-Identifier: GPL-2.0+
3 * drivers/net/phy/at803x.c
5 * Driver for Qualcomm Atheros AR803x PHY
10 #include <linux/phy.h>
11 #include <linux/module.h>
12 #include <linux/string.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/ethtool_netlink.h>
16 #include <linux/bitfield.h>
17 #include <linux/regulator/of_regulator.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/regulator/consumer.h>
21 #include <linux/phylink.h>
22 #include <linux/sfp.h>
23 #include <dt-bindings/net/qca-ar803x.h>
27 #define AT803X_LED_CONTROL 0x18
29 #define AT803X_PHY_MMD3_WOL_CTRL 0x8012
30 #define AT803X_WOL_EN BIT(5)
32 #define AT803X_REG_CHIP_CONFIG 0x1f
33 #define AT803X_BT_BX_REG_SEL 0x8000
35 #define AT803X_MODE_CFG_MASK 0x0F
36 #define AT803X_MODE_CFG_BASET_RGMII 0x00
37 #define AT803X_MODE_CFG_BASET_SGMII 0x01
38 #define AT803X_MODE_CFG_BX1000_RGMII_50OHM 0x02
39 #define AT803X_MODE_CFG_BX1000_RGMII_75OHM 0x03
40 #define AT803X_MODE_CFG_BX1000_CONV_50OHM 0x04
41 #define AT803X_MODE_CFG_BX1000_CONV_75OHM 0x05
42 #define AT803X_MODE_CFG_FX100_RGMII_50OHM 0x06
43 #define AT803X_MODE_CFG_FX100_CONV_50OHM 0x07
44 #define AT803X_MODE_CFG_RGMII_AUTO_MDET 0x0B
45 #define AT803X_MODE_CFG_FX100_RGMII_75OHM 0x0E
46 #define AT803X_MODE_CFG_FX100_CONV_75OHM 0x0F
48 #define AT803X_PSSR 0x11 /*PHY-Specific Status Register*/
49 #define AT803X_PSSR_MR_AN_COMPLETE 0x0200
51 #define AT803X_DEBUG_REG_1F 0x1F
52 #define AT803X_DEBUG_PLL_ON BIT(2)
53 #define AT803X_DEBUG_RGMII_1V8 BIT(3)
55 /* AT803x supports either the XTAL input pad, an internal PLL or the
56 * DSP as clock reference for the clock output pad. The XTAL reference
57 * is only used for 25 MHz output, all other frequencies need the PLL.
58 * The DSP as a clock reference is used in synchronous ethernet
61 * By default the PLL is only enabled if there is a link. Otherwise
62 * the PHY will go into low power state and disabled the PLL. You can
63 * set the PLL_ON bit (see debug register 0x1f) to keep the PLL always
66 #define AT803X_MMD7_CLK25M 0x8016
67 #define AT803X_CLK_OUT_MASK GENMASK(4, 2)
68 #define AT803X_CLK_OUT_25MHZ_XTAL 0
69 #define AT803X_CLK_OUT_25MHZ_DSP 1
70 #define AT803X_CLK_OUT_50MHZ_PLL 2
71 #define AT803X_CLK_OUT_50MHZ_DSP 3
72 #define AT803X_CLK_OUT_62_5MHZ_PLL 4
73 #define AT803X_CLK_OUT_62_5MHZ_DSP 5
74 #define AT803X_CLK_OUT_125MHZ_PLL 6
75 #define AT803X_CLK_OUT_125MHZ_DSP 7
77 /* The AR8035 has another mask which is compatible with the AR8031/AR8033 mask
78 * but doesn't support choosing between XTAL/PLL and DSP.
80 #define AT8035_CLK_OUT_MASK GENMASK(4, 3)
82 #define AT803X_CLK_OUT_STRENGTH_MASK GENMASK(8, 7)
83 #define AT803X_CLK_OUT_STRENGTH_FULL 0
84 #define AT803X_CLK_OUT_STRENGTH_HALF 1
85 #define AT803X_CLK_OUT_STRENGTH_QUARTER 2
87 #define AT803X_MMD3_SMARTEEE_CTL1 0x805b
88 #define AT803X_MMD3_SMARTEEE_CTL2 0x805c
89 #define AT803X_MMD3_SMARTEEE_CTL3 0x805d
90 #define AT803X_MMD3_SMARTEEE_CTL3_LPI_EN BIT(8)
92 #define ATH9331_PHY_ID 0x004dd041
93 #define ATH8030_PHY_ID 0x004dd076
94 #define ATH8031_PHY_ID 0x004dd074
95 #define ATH8032_PHY_ID 0x004dd023
96 #define ATH8035_PHY_ID 0x004dd072
97 #define AT8030_PHY_ID_MASK 0xffffffef
99 #define QCA9561_PHY_ID 0x004dd042
101 #define AT803X_PAGE_FIBER 0
102 #define AT803X_PAGE_COPPER 1
104 /* don't turn off internal PLL */
105 #define AT803X_KEEP_PLL_ENABLED BIT(0)
106 #define AT803X_DISABLE_SMARTEEE BIT(1)
108 /* disable hibernation mode */
109 #define AT803X_DISABLE_HIBERNATION_MODE BIT(2)
111 MODULE_DESCRIPTION("Qualcomm Atheros AR803x PHY driver");
112 MODULE_AUTHOR("Matus Ujhelyi");
113 MODULE_LICENSE("GPL");
119 u8 smarteee_lpi_tw_1g;
120 u8 smarteee_lpi_tw_100m;
123 struct regulator_dev *vddio_rdev;
124 struct regulator_dev *vddh_rdev;
127 struct at803x_context {
136 static int at803x_write_page(struct phy_device *phydev, int page)
141 if (page == AT803X_PAGE_COPPER) {
142 set = AT803X_BT_BX_REG_SEL;
146 mask = AT803X_BT_BX_REG_SEL;
149 return __phy_modify(phydev, AT803X_REG_CHIP_CONFIG, mask, set);
152 static int at803x_read_page(struct phy_device *phydev)
154 int ccr = __phy_read(phydev, AT803X_REG_CHIP_CONFIG);
159 if (ccr & AT803X_BT_BX_REG_SEL)
160 return AT803X_PAGE_COPPER;
162 return AT803X_PAGE_FIBER;
165 static int at803x_enable_rx_delay(struct phy_device *phydev)
167 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 0,
168 AT803X_DEBUG_RX_CLK_DLY_EN);
171 static int at803x_enable_tx_delay(struct phy_device *phydev)
173 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE, 0,
174 AT803X_DEBUG_TX_CLK_DLY_EN);
177 static int at803x_disable_rx_delay(struct phy_device *phydev)
179 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL,
180 AT803X_DEBUG_RX_CLK_DLY_EN, 0);
183 static int at803x_disable_tx_delay(struct phy_device *phydev)
185 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE,
186 AT803X_DEBUG_TX_CLK_DLY_EN, 0);
189 /* save relevant PHY registers to private copy */
190 static void at803x_context_save(struct phy_device *phydev,
191 struct at803x_context *context)
193 context->bmcr = phy_read(phydev, MII_BMCR);
194 context->advertise = phy_read(phydev, MII_ADVERTISE);
195 context->control1000 = phy_read(phydev, MII_CTRL1000);
196 context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE);
197 context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED);
198 context->led_control = phy_read(phydev, AT803X_LED_CONTROL);
201 /* restore relevant PHY registers from private copy */
202 static void at803x_context_restore(struct phy_device *phydev,
203 const struct at803x_context *context)
205 phy_write(phydev, MII_BMCR, context->bmcr);
206 phy_write(phydev, MII_ADVERTISE, context->advertise);
207 phy_write(phydev, MII_CTRL1000, context->control1000);
208 phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable);
209 phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed);
210 phy_write(phydev, AT803X_LED_CONTROL, context->led_control);
213 static int at803x_suspend(struct phy_device *phydev)
218 value = phy_read(phydev, AT803X_INTR_ENABLE);
219 wol_enabled = value & AT803X_INTR_ENABLE_WOL;
222 value = BMCR_ISOLATE;
226 phy_modify(phydev, MII_BMCR, 0, value);
231 static int at803x_resume(struct phy_device *phydev)
233 return phy_modify(phydev, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE, 0);
236 static int at803x_parse_dt(struct phy_device *phydev)
238 struct device_node *node = phydev->mdio.dev.of_node;
239 struct at803x_priv *priv = phydev->priv;
240 u32 freq, strength, tw;
244 if (!IS_ENABLED(CONFIG_OF_MDIO))
247 if (of_property_read_bool(node, "qca,disable-smarteee"))
248 priv->flags |= AT803X_DISABLE_SMARTEEE;
250 if (of_property_read_bool(node, "qca,disable-hibernation-mode"))
251 priv->flags |= AT803X_DISABLE_HIBERNATION_MODE;
253 if (!of_property_read_u32(node, "qca,smarteee-tw-us-1g", &tw)) {
254 if (!tw || tw > 255) {
255 phydev_err(phydev, "invalid qca,smarteee-tw-us-1g\n");
258 priv->smarteee_lpi_tw_1g = tw;
261 if (!of_property_read_u32(node, "qca,smarteee-tw-us-100m", &tw)) {
262 if (!tw || tw > 255) {
263 phydev_err(phydev, "invalid qca,smarteee-tw-us-100m\n");
266 priv->smarteee_lpi_tw_100m = tw;
269 ret = of_property_read_u32(node, "qca,clk-out-frequency", &freq);
273 sel = AT803X_CLK_OUT_25MHZ_XTAL;
276 sel = AT803X_CLK_OUT_50MHZ_PLL;
279 sel = AT803X_CLK_OUT_62_5MHZ_PLL;
282 sel = AT803X_CLK_OUT_125MHZ_PLL;
285 phydev_err(phydev, "invalid qca,clk-out-frequency\n");
289 priv->clk_25m_reg |= FIELD_PREP(AT803X_CLK_OUT_MASK, sel);
290 priv->clk_25m_mask |= AT803X_CLK_OUT_MASK;
293 ret = of_property_read_u32(node, "qca,clk-out-strength", &strength);
295 priv->clk_25m_mask |= AT803X_CLK_OUT_STRENGTH_MASK;
297 case AR803X_STRENGTH_FULL:
298 priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_FULL;
300 case AR803X_STRENGTH_HALF:
301 priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_HALF;
303 case AR803X_STRENGTH_QUARTER:
304 priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_QUARTER;
307 phydev_err(phydev, "invalid qca,clk-out-strength\n");
315 static int at803x_probe(struct phy_device *phydev)
317 struct device *dev = &phydev->mdio.dev;
318 struct at803x_priv *priv;
321 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
327 ret = at803x_parse_dt(phydev);
334 static int at803x_get_features(struct phy_device *phydev)
336 struct at803x_priv *priv = phydev->priv;
339 err = genphy_read_abilities(phydev);
343 if (phydev->drv->phy_id != ATH8031_PHY_ID)
346 /* AR8031/AR8033 have different status registers
347 * for copper and fiber operation. However, the
348 * extended status register is the same for both
351 * As a result of that, ESTATUS_1000_XFULL is set
352 * to 1 even when operating in copper TP mode.
354 * Remove this mode from the supported link modes
355 * when not operating in 1000BaseX mode.
357 if (!priv->is_1000basex)
358 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
364 static int at803x_smarteee_config(struct phy_device *phydev)
366 struct at803x_priv *priv = phydev->priv;
367 u16 mask = 0, val = 0;
370 if (priv->flags & AT803X_DISABLE_SMARTEEE)
371 return phy_modify_mmd(phydev, MDIO_MMD_PCS,
372 AT803X_MMD3_SMARTEEE_CTL3,
373 AT803X_MMD3_SMARTEEE_CTL3_LPI_EN, 0);
375 if (priv->smarteee_lpi_tw_1g) {
377 val |= priv->smarteee_lpi_tw_1g << 8;
379 if (priv->smarteee_lpi_tw_100m) {
381 val |= priv->smarteee_lpi_tw_100m;
386 ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL1,
391 return phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL3,
392 AT803X_MMD3_SMARTEEE_CTL3_LPI_EN,
393 AT803X_MMD3_SMARTEEE_CTL3_LPI_EN);
396 static int at803x_clk_out_config(struct phy_device *phydev)
398 struct at803x_priv *priv = phydev->priv;
400 if (!priv->clk_25m_mask)
403 return phy_modify_mmd(phydev, MDIO_MMD_AN, AT803X_MMD7_CLK25M,
404 priv->clk_25m_mask, priv->clk_25m_reg);
407 static int at8031_pll_config(struct phy_device *phydev)
409 struct at803x_priv *priv = phydev->priv;
411 /* The default after hardware reset is PLL OFF. After a soft reset, the
412 * values are retained.
414 if (priv->flags & AT803X_KEEP_PLL_ENABLED)
415 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
416 0, AT803X_DEBUG_PLL_ON);
418 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
419 AT803X_DEBUG_PLL_ON, 0);
422 static int at803x_hibernation_mode_config(struct phy_device *phydev)
424 struct at803x_priv *priv = phydev->priv;
426 /* The default after hardware reset is hibernation mode enabled. After
427 * software reset, the value is retained.
429 if (!(priv->flags & AT803X_DISABLE_HIBERNATION_MODE) &&
430 !(phydev->dev_flags & PHY_F_RXC_ALWAYS_ON))
433 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_HIB_CTRL,
434 AT803X_DEBUG_HIB_CTRL_PS_HIB_EN, 0);
437 static int at803x_config_init(struct phy_device *phydev)
441 /* The RX and TX delay default is:
442 * after HW reset: RX delay enabled and TX delay disabled
443 * after SW reset: RX delay enabled, while TX delay retains the
444 * value before reset.
446 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
447 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
448 ret = at803x_enable_rx_delay(phydev);
450 ret = at803x_disable_rx_delay(phydev);
454 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
455 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
456 ret = at803x_enable_tx_delay(phydev);
458 ret = at803x_disable_tx_delay(phydev);
462 ret = at803x_smarteee_config(phydev);
466 ret = at803x_clk_out_config(phydev);
470 ret = at803x_hibernation_mode_config(phydev);
474 /* Ar803x extended next page bit is enabled by default. Cisco
475 * multigig switches read this bit and attempt to negotiate 10Gbps
476 * rates even if the next page bit is disabled. This is incorrect
477 * behaviour but we still need to accommodate it. XNP is only needed
478 * for 10Gbps support, so disable XNP.
480 return phy_modify(phydev, MII_ADVERTISE, MDIO_AN_CTRL1_XNP, 0);
483 static void at803x_link_change_notify(struct phy_device *phydev)
486 * Conduct a hardware reset for AT8030 every time a link loss is
487 * signalled. This is necessary to circumvent a hardware bug that
488 * occurs when the cable is unplugged while TX packets are pending
489 * in the FIFO. In such cases, the FIFO enters an error mode it
490 * cannot recover from by software.
492 if (phydev->state == PHY_NOLINK && phydev->mdio.reset_gpio) {
493 struct at803x_context context;
495 at803x_context_save(phydev, &context);
497 phy_device_reset(phydev, 1);
498 usleep_range(1000, 2000);
499 phy_device_reset(phydev, 0);
500 usleep_range(1000, 2000);
502 at803x_context_restore(phydev, &context);
504 phydev_dbg(phydev, "%s(): phy was reset\n", __func__);
508 static int at803x_config_aneg(struct phy_device *phydev)
510 struct at803x_priv *priv = phydev->priv;
513 ret = at803x_prepare_config_aneg(phydev);
517 if (priv->is_1000basex)
518 return genphy_c37_config_aneg(phydev);
520 return genphy_config_aneg(phydev);
523 static int at803x_cable_test_result_trans(u16 status)
525 switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
526 case AT803X_CDT_STATUS_STAT_NORMAL:
527 return ETHTOOL_A_CABLE_RESULT_CODE_OK;
528 case AT803X_CDT_STATUS_STAT_SHORT:
529 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
530 case AT803X_CDT_STATUS_STAT_OPEN:
531 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
532 case AT803X_CDT_STATUS_STAT_FAIL:
534 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
538 static bool at803x_cdt_test_failed(u16 status)
540 return FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status) ==
541 AT803X_CDT_STATUS_STAT_FAIL;
544 static bool at803x_cdt_fault_length_valid(u16 status)
546 switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
547 case AT803X_CDT_STATUS_STAT_OPEN:
548 case AT803X_CDT_STATUS_STAT_SHORT:
554 static int at803x_cable_test_one_pair(struct phy_device *phydev, int pair)
556 static const int ethtool_pair[] = {
557 ETHTOOL_A_CABLE_PAIR_A,
558 ETHTOOL_A_CABLE_PAIR_B,
559 ETHTOOL_A_CABLE_PAIR_C,
560 ETHTOOL_A_CABLE_PAIR_D,
564 val = FIELD_PREP(AT803X_CDT_MDI_PAIR_MASK, pair) |
565 AT803X_CDT_ENABLE_TEST;
566 ret = at803x_cdt_start(phydev, val);
570 ret = at803x_cdt_wait_for_completion(phydev, AT803X_CDT_ENABLE_TEST);
574 val = phy_read(phydev, AT803X_CDT_STATUS);
578 if (at803x_cdt_test_failed(val))
581 ethnl_cable_test_result(phydev, ethtool_pair[pair],
582 at803x_cable_test_result_trans(val));
584 if (at803x_cdt_fault_length_valid(val)) {
585 val = FIELD_GET(AT803X_CDT_STATUS_DELTA_TIME_MASK, val);
586 ethnl_cable_test_fault_length(phydev, ethtool_pair[pair],
587 at803x_cdt_fault_length(val));
593 static int at803x_cable_test_get_status(struct phy_device *phydev,
594 bool *finished, unsigned long pair_mask)
601 /* According to the datasheet the CDT can be performed when
602 * there is no link partner or when the link partner is
603 * auto-negotiating. Starting the test will restart the AN
604 * automatically. It seems that doing this repeatedly we will
605 * get a slot where our link partner won't disturb our
608 while (pair_mask && retries--) {
609 for_each_set_bit(pair, &pair_mask, 4) {
610 ret = at803x_cable_test_one_pair(phydev, pair);
614 clear_bit(pair, &pair_mask);
625 static void at803x_cable_test_autoneg(struct phy_device *phydev)
627 /* Enable auto-negotiation, but advertise no capabilities, no link
628 * will be established. A restart of the auto-negotiation is not
629 * required, because the cable test will automatically break the link.
631 phy_write(phydev, MII_BMCR, BMCR_ANENABLE);
632 phy_write(phydev, MII_ADVERTISE, ADVERTISE_CSMA);
635 static int at803x_cable_test_start(struct phy_device *phydev)
637 at803x_cable_test_autoneg(phydev);
638 /* we do all the (time consuming) work later */
642 static int at8031_rgmii_reg_set_voltage_sel(struct regulator_dev *rdev,
643 unsigned int selector)
645 struct phy_device *phydev = rdev_get_drvdata(rdev);
648 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
649 0, AT803X_DEBUG_RGMII_1V8);
651 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
652 AT803X_DEBUG_RGMII_1V8, 0);
655 static int at8031_rgmii_reg_get_voltage_sel(struct regulator_dev *rdev)
657 struct phy_device *phydev = rdev_get_drvdata(rdev);
660 val = at803x_debug_reg_read(phydev, AT803X_DEBUG_REG_1F);
664 return (val & AT803X_DEBUG_RGMII_1V8) ? 1 : 0;
667 static const struct regulator_ops vddio_regulator_ops = {
668 .list_voltage = regulator_list_voltage_table,
669 .set_voltage_sel = at8031_rgmii_reg_set_voltage_sel,
670 .get_voltage_sel = at8031_rgmii_reg_get_voltage_sel,
673 static const unsigned int vddio_voltage_table[] = {
678 static const struct regulator_desc vddio_desc = {
680 .of_match = of_match_ptr("vddio-regulator"),
681 .n_voltages = ARRAY_SIZE(vddio_voltage_table),
682 .volt_table = vddio_voltage_table,
683 .ops = &vddio_regulator_ops,
684 .type = REGULATOR_VOLTAGE,
685 .owner = THIS_MODULE,
688 static const struct regulator_ops vddh_regulator_ops = {
691 static const struct regulator_desc vddh_desc = {
693 .of_match = of_match_ptr("vddh-regulator"),
696 .ops = &vddh_regulator_ops,
697 .type = REGULATOR_VOLTAGE,
698 .owner = THIS_MODULE,
701 static int at8031_register_regulators(struct phy_device *phydev)
703 struct at803x_priv *priv = phydev->priv;
704 struct device *dev = &phydev->mdio.dev;
705 struct regulator_config config = { };
708 config.driver_data = phydev;
710 priv->vddio_rdev = devm_regulator_register(dev, &vddio_desc, &config);
711 if (IS_ERR(priv->vddio_rdev)) {
712 phydev_err(phydev, "failed to register VDDIO regulator\n");
713 return PTR_ERR(priv->vddio_rdev);
716 priv->vddh_rdev = devm_regulator_register(dev, &vddh_desc, &config);
717 if (IS_ERR(priv->vddh_rdev)) {
718 phydev_err(phydev, "failed to register VDDH regulator\n");
719 return PTR_ERR(priv->vddh_rdev);
725 static int at8031_sfp_insert(void *upstream, const struct sfp_eeprom_id *id)
727 struct phy_device *phydev = upstream;
728 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_support);
729 __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
730 DECLARE_PHY_INTERFACE_MASK(interfaces);
731 phy_interface_t iface;
733 linkmode_zero(phy_support);
734 phylink_set(phy_support, 1000baseX_Full);
735 phylink_set(phy_support, 1000baseT_Full);
736 phylink_set(phy_support, Autoneg);
737 phylink_set(phy_support, Pause);
738 phylink_set(phy_support, Asym_Pause);
740 linkmode_zero(sfp_support);
741 sfp_parse_support(phydev->sfp_bus, id, sfp_support, interfaces);
742 /* Some modules support 10G modes as well as others we support.
743 * Mask out non-supported modes so the correct interface is picked.
745 linkmode_and(sfp_support, phy_support, sfp_support);
747 if (linkmode_empty(sfp_support)) {
748 dev_err(&phydev->mdio.dev, "incompatible SFP module inserted\n");
752 iface = sfp_select_interface(phydev->sfp_bus, sfp_support);
754 /* Only 1000Base-X is supported by AR8031/8033 as the downstream SerDes
755 * interface for use with SFP modules.
756 * However, some copper modules detected as having a preferred SGMII
757 * interface do default to and function in 1000Base-X mode, so just
758 * print a warning and allow such modules, as they may have some chance
761 if (iface == PHY_INTERFACE_MODE_SGMII)
762 dev_warn(&phydev->mdio.dev, "module may not function if 1000Base-X not supported\n");
763 else if (iface != PHY_INTERFACE_MODE_1000BASEX)
769 static const struct sfp_upstream_ops at8031_sfp_ops = {
770 .attach = phy_sfp_attach,
771 .detach = phy_sfp_detach,
772 .module_insert = at8031_sfp_insert,
775 static int at8031_parse_dt(struct phy_device *phydev)
777 struct device_node *node = phydev->mdio.dev.of_node;
778 struct at803x_priv *priv = phydev->priv;
781 if (of_property_read_bool(node, "qca,keep-pll-enabled"))
782 priv->flags |= AT803X_KEEP_PLL_ENABLED;
784 ret = at8031_register_regulators(phydev);
788 ret = devm_regulator_get_enable_optional(&phydev->mdio.dev,
791 phydev_err(phydev, "failed to get VDDIO regulator\n");
795 /* Only AR8031/8033 support 1000Base-X for SFP modules */
796 return phy_sfp_probe(phydev, &at8031_sfp_ops);
799 static int at8031_probe(struct phy_device *phydev)
801 struct at803x_priv *priv;
806 ret = at803x_probe(phydev);
812 /* Only supported on AR8031/AR8033, the AR8030/AR8035 use strapping
815 ret = at8031_parse_dt(phydev);
819 ccr = phy_read(phydev, AT803X_REG_CHIP_CONFIG);
822 mode_cfg = ccr & AT803X_MODE_CFG_MASK;
825 case AT803X_MODE_CFG_BX1000_RGMII_50OHM:
826 case AT803X_MODE_CFG_BX1000_RGMII_75OHM:
827 priv->is_1000basex = true;
829 case AT803X_MODE_CFG_FX100_RGMII_50OHM:
830 case AT803X_MODE_CFG_FX100_RGMII_75OHM:
831 priv->is_fiber = true;
835 /* Disable WoL in 1588 register which is enabled
838 return phy_modify_mmd(phydev, MDIO_MMD_PCS,
839 AT803X_PHY_MMD3_WOL_CTRL,
843 static int at8031_config_init(struct phy_device *phydev)
845 struct at803x_priv *priv = phydev->priv;
848 /* Some bootloaders leave the fiber page selected.
849 * Switch to the appropriate page (fiber or copper), as otherwise we
850 * read the PHY capabilities from the wrong page.
852 phy_lock_mdio_bus(phydev);
853 ret = at803x_write_page(phydev,
854 priv->is_fiber ? AT803X_PAGE_FIBER :
856 phy_unlock_mdio_bus(phydev);
860 ret = at8031_pll_config(phydev);
864 return at803x_config_init(phydev);
867 static int at8031_set_wol(struct phy_device *phydev,
868 struct ethtool_wolinfo *wol)
872 /* First setup MAC address and enable WOL interrupt */
873 ret = at803x_set_wol(phydev, wol);
877 if (wol->wolopts & WAKE_MAGIC)
878 /* Enable WOL function for 1588 */
879 ret = phy_modify_mmd(phydev, MDIO_MMD_PCS,
880 AT803X_PHY_MMD3_WOL_CTRL,
883 /* Disable WoL function for 1588 */
884 ret = phy_modify_mmd(phydev, MDIO_MMD_PCS,
885 AT803X_PHY_MMD3_WOL_CTRL,
891 static int at8031_config_intr(struct phy_device *phydev)
893 struct at803x_priv *priv = phydev->priv;
896 if (phydev->interrupts == PHY_INTERRUPT_ENABLED &&
898 /* Clear any pending interrupts */
899 err = at803x_ack_interrupt(phydev);
903 value |= AT803X_INTR_ENABLE_LINK_FAIL_BX;
904 value |= AT803X_INTR_ENABLE_LINK_SUCCESS_BX;
906 err = phy_set_bits(phydev, AT803X_INTR_ENABLE, value);
911 return at803x_config_intr(phydev);
914 /* AR8031 and AR8033 share the same read status logic */
915 static int at8031_read_status(struct phy_device *phydev)
917 struct at803x_priv *priv = phydev->priv;
920 if (priv->is_1000basex)
921 return genphy_c37_read_status(phydev, &changed);
923 return at803x_read_status(phydev);
926 /* AR8031 and AR8035 share the same cable test get status reg */
927 static int at8031_cable_test_get_status(struct phy_device *phydev,
930 return at803x_cable_test_get_status(phydev, finished, 0xf);
933 /* AR8031 and AR8035 share the same cable test start logic */
934 static int at8031_cable_test_start(struct phy_device *phydev)
936 at803x_cable_test_autoneg(phydev);
937 phy_write(phydev, MII_CTRL1000, 0);
938 /* we do all the (time consuming) work later */
942 /* AR8032, AR9331 and QCA9561 share the same cable test get status reg */
943 static int at8032_cable_test_get_status(struct phy_device *phydev,
946 return at803x_cable_test_get_status(phydev, finished, 0x3);
949 static int at8035_parse_dt(struct phy_device *phydev)
951 struct at803x_priv *priv = phydev->priv;
953 /* Mask is set by the generic at803x_parse_dt
954 * if property is set. Assume property is set
955 * with the mask not zero.
957 if (priv->clk_25m_mask) {
958 /* Fixup for the AR8030/AR8035. This chip has another mask and
959 * doesn't support the DSP reference. Eg. the lowest bit of the
960 * mask. The upper two bits select the same frequencies. Mask
961 * the lowest bit here.
964 * There was no datasheet for the AR8030 available so this is
965 * just a guess. But the AR8035 is listed as pin compatible
966 * to the AR8030 so there might be a good chance it works on
969 priv->clk_25m_reg &= AT8035_CLK_OUT_MASK;
970 priv->clk_25m_mask &= AT8035_CLK_OUT_MASK;
976 /* AR8030 and AR8035 shared the same special mask for clk_25m */
977 static int at8035_probe(struct phy_device *phydev)
981 ret = at803x_probe(phydev);
985 return at8035_parse_dt(phydev);
988 static struct phy_driver at803x_driver[] = {
990 /* Qualcomm Atheros AR8035 */
991 PHY_ID_MATCH_EXACT(ATH8035_PHY_ID),
992 .name = "Qualcomm Atheros AR8035",
993 .flags = PHY_POLL_CABLE_TEST,
994 .probe = at8035_probe,
995 .config_aneg = at803x_config_aneg,
996 .config_init = at803x_config_init,
997 .soft_reset = genphy_soft_reset,
998 .set_wol = at803x_set_wol,
999 .get_wol = at803x_get_wol,
1000 .suspend = at803x_suspend,
1001 .resume = at803x_resume,
1002 /* PHY_GBIT_FEATURES */
1003 .read_status = at803x_read_status,
1004 .config_intr = at803x_config_intr,
1005 .handle_interrupt = at803x_handle_interrupt,
1006 .get_tunable = at803x_get_tunable,
1007 .set_tunable = at803x_set_tunable,
1008 .cable_test_start = at8031_cable_test_start,
1009 .cable_test_get_status = at8031_cable_test_get_status,
1011 /* Qualcomm Atheros AR8030 */
1012 .phy_id = ATH8030_PHY_ID,
1013 .name = "Qualcomm Atheros AR8030",
1014 .phy_id_mask = AT8030_PHY_ID_MASK,
1015 .probe = at8035_probe,
1016 .config_init = at803x_config_init,
1017 .link_change_notify = at803x_link_change_notify,
1018 .set_wol = at803x_set_wol,
1019 .get_wol = at803x_get_wol,
1020 .suspend = at803x_suspend,
1021 .resume = at803x_resume,
1022 /* PHY_BASIC_FEATURES */
1023 .config_intr = at803x_config_intr,
1024 .handle_interrupt = at803x_handle_interrupt,
1026 /* Qualcomm Atheros AR8031/AR8033 */
1027 PHY_ID_MATCH_EXACT(ATH8031_PHY_ID),
1028 .name = "Qualcomm Atheros AR8031/AR8033",
1029 .flags = PHY_POLL_CABLE_TEST,
1030 .probe = at8031_probe,
1031 .config_init = at8031_config_init,
1032 .config_aneg = at803x_config_aneg,
1033 .soft_reset = genphy_soft_reset,
1034 .set_wol = at8031_set_wol,
1035 .get_wol = at803x_get_wol,
1036 .suspend = at803x_suspend,
1037 .resume = at803x_resume,
1038 .read_page = at803x_read_page,
1039 .write_page = at803x_write_page,
1040 .get_features = at803x_get_features,
1041 .read_status = at8031_read_status,
1042 .config_intr = at8031_config_intr,
1043 .handle_interrupt = at803x_handle_interrupt,
1044 .get_tunable = at803x_get_tunable,
1045 .set_tunable = at803x_set_tunable,
1046 .cable_test_start = at8031_cable_test_start,
1047 .cable_test_get_status = at8031_cable_test_get_status,
1049 /* Qualcomm Atheros AR8032 */
1050 PHY_ID_MATCH_EXACT(ATH8032_PHY_ID),
1051 .name = "Qualcomm Atheros AR8032",
1052 .probe = at803x_probe,
1053 .flags = PHY_POLL_CABLE_TEST,
1054 .config_init = at803x_config_init,
1055 .link_change_notify = at803x_link_change_notify,
1056 .suspend = at803x_suspend,
1057 .resume = at803x_resume,
1058 /* PHY_BASIC_FEATURES */
1059 .config_intr = at803x_config_intr,
1060 .handle_interrupt = at803x_handle_interrupt,
1061 .cable_test_start = at803x_cable_test_start,
1062 .cable_test_get_status = at8032_cable_test_get_status,
1064 /* ATHEROS AR9331 */
1065 PHY_ID_MATCH_EXACT(ATH9331_PHY_ID),
1066 .name = "Qualcomm Atheros AR9331 built-in PHY",
1067 .probe = at803x_probe,
1068 .suspend = at803x_suspend,
1069 .resume = at803x_resume,
1070 .flags = PHY_POLL_CABLE_TEST,
1071 /* PHY_BASIC_FEATURES */
1072 .config_intr = at803x_config_intr,
1073 .handle_interrupt = at803x_handle_interrupt,
1074 .cable_test_start = at803x_cable_test_start,
1075 .cable_test_get_status = at8032_cable_test_get_status,
1076 .read_status = at803x_read_status,
1077 .soft_reset = genphy_soft_reset,
1078 .config_aneg = at803x_config_aneg,
1080 /* Qualcomm Atheros QCA9561 */
1081 PHY_ID_MATCH_EXACT(QCA9561_PHY_ID),
1082 .name = "Qualcomm Atheros QCA9561 built-in PHY",
1083 .probe = at803x_probe,
1084 .suspend = at803x_suspend,
1085 .resume = at803x_resume,
1086 .flags = PHY_POLL_CABLE_TEST,
1087 /* PHY_BASIC_FEATURES */
1088 .config_intr = at803x_config_intr,
1089 .handle_interrupt = at803x_handle_interrupt,
1090 .cable_test_start = at803x_cable_test_start,
1091 .cable_test_get_status = at8032_cable_test_get_status,
1092 .read_status = at803x_read_status,
1093 .soft_reset = genphy_soft_reset,
1094 .config_aneg = at803x_config_aneg,
1097 module_phy_driver(at803x_driver);
1099 static struct mdio_device_id __maybe_unused atheros_tbl[] = {
1100 { ATH8030_PHY_ID, AT8030_PHY_ID_MASK },
1101 { PHY_ID_MATCH_EXACT(ATH8031_PHY_ID) },
1102 { PHY_ID_MATCH_EXACT(ATH8032_PHY_ID) },
1103 { PHY_ID_MATCH_EXACT(ATH8035_PHY_ID) },
1104 { PHY_ID_MATCH_EXACT(ATH9331_PHY_ID) },
1105 { PHY_ID_MATCH_EXACT(QCA9561_PHY_ID) },
1109 MODULE_DEVICE_TABLE(mdio, atheros_tbl);