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/of_gpio.h>
17 #include <linux/bitfield.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/regulator/of_regulator.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/consumer.h>
22 #include <dt-bindings/net/qca-ar803x.h>
24 #define AT803X_SPECIFIC_FUNCTION_CONTROL 0x10
25 #define AT803X_SFC_ASSERT_CRS BIT(11)
26 #define AT803X_SFC_FORCE_LINK BIT(10)
27 #define AT803X_SFC_MDI_CROSSOVER_MODE_M GENMASK(6, 5)
28 #define AT803X_SFC_AUTOMATIC_CROSSOVER 0x3
29 #define AT803X_SFC_MANUAL_MDIX 0x1
30 #define AT803X_SFC_MANUAL_MDI 0x0
31 #define AT803X_SFC_SQE_TEST BIT(2)
32 #define AT803X_SFC_POLARITY_REVERSAL BIT(1)
33 #define AT803X_SFC_DISABLE_JABBER BIT(0)
35 #define AT803X_SPECIFIC_STATUS 0x11
36 #define AT803X_SS_SPEED_MASK (3 << 14)
37 #define AT803X_SS_SPEED_1000 (2 << 14)
38 #define AT803X_SS_SPEED_100 (1 << 14)
39 #define AT803X_SS_SPEED_10 (0 << 14)
40 #define AT803X_SS_DUPLEX BIT(13)
41 #define AT803X_SS_SPEED_DUPLEX_RESOLVED BIT(11)
42 #define AT803X_SS_MDIX BIT(6)
44 #define AT803X_INTR_ENABLE 0x12
45 #define AT803X_INTR_ENABLE_AUTONEG_ERR BIT(15)
46 #define AT803X_INTR_ENABLE_SPEED_CHANGED BIT(14)
47 #define AT803X_INTR_ENABLE_DUPLEX_CHANGED BIT(13)
48 #define AT803X_INTR_ENABLE_PAGE_RECEIVED BIT(12)
49 #define AT803X_INTR_ENABLE_LINK_FAIL BIT(11)
50 #define AT803X_INTR_ENABLE_LINK_SUCCESS BIT(10)
51 #define AT803X_INTR_ENABLE_WIRESPEED_DOWNGRADE BIT(5)
52 #define AT803X_INTR_ENABLE_POLARITY_CHANGED BIT(1)
53 #define AT803X_INTR_ENABLE_WOL BIT(0)
55 #define AT803X_INTR_STATUS 0x13
57 #define AT803X_SMART_SPEED 0x14
58 #define AT803X_SMART_SPEED_ENABLE BIT(5)
59 #define AT803X_SMART_SPEED_RETRY_LIMIT_MASK GENMASK(4, 2)
60 #define AT803X_SMART_SPEED_BYPASS_TIMER BIT(1)
61 #define AT803X_CDT 0x16
62 #define AT803X_CDT_MDI_PAIR_MASK GENMASK(9, 8)
63 #define AT803X_CDT_ENABLE_TEST BIT(0)
64 #define AT803X_CDT_STATUS 0x1c
65 #define AT803X_CDT_STATUS_STAT_NORMAL 0
66 #define AT803X_CDT_STATUS_STAT_SHORT 1
67 #define AT803X_CDT_STATUS_STAT_OPEN 2
68 #define AT803X_CDT_STATUS_STAT_FAIL 3
69 #define AT803X_CDT_STATUS_STAT_MASK GENMASK(9, 8)
70 #define AT803X_CDT_STATUS_DELTA_TIME_MASK GENMASK(7, 0)
71 #define AT803X_LED_CONTROL 0x18
73 #define AT803X_DEVICE_ADDR 0x03
74 #define AT803X_LOC_MAC_ADDR_0_15_OFFSET 0x804C
75 #define AT803X_LOC_MAC_ADDR_16_31_OFFSET 0x804B
76 #define AT803X_LOC_MAC_ADDR_32_47_OFFSET 0x804A
77 #define AT803X_REG_CHIP_CONFIG 0x1f
78 #define AT803X_BT_BX_REG_SEL 0x8000
80 #define AT803X_DEBUG_ADDR 0x1D
81 #define AT803X_DEBUG_DATA 0x1E
83 #define AT803X_MODE_CFG_MASK 0x0F
84 #define AT803X_MODE_CFG_SGMII 0x01
86 #define AT803X_PSSR 0x11 /*PHY-Specific Status Register*/
87 #define AT803X_PSSR_MR_AN_COMPLETE 0x0200
89 #define AT803X_DEBUG_REG_0 0x00
90 #define AT803X_DEBUG_RX_CLK_DLY_EN BIT(15)
92 #define AT803X_DEBUG_REG_5 0x05
93 #define AT803X_DEBUG_TX_CLK_DLY_EN BIT(8)
95 #define AT803X_DEBUG_REG_3C 0x3C
97 #define AT803X_DEBUG_REG_3D 0x3D
99 #define AT803X_DEBUG_REG_1F 0x1F
100 #define AT803X_DEBUG_PLL_ON BIT(2)
101 #define AT803X_DEBUG_RGMII_1V8 BIT(3)
103 #define MDIO_AZ_DEBUG 0x800D
105 /* AT803x supports either the XTAL input pad, an internal PLL or the
106 * DSP as clock reference for the clock output pad. The XTAL reference
107 * is only used for 25 MHz output, all other frequencies need the PLL.
108 * The DSP as a clock reference is used in synchronous ethernet
111 * By default the PLL is only enabled if there is a link. Otherwise
112 * the PHY will go into low power state and disabled the PLL. You can
113 * set the PLL_ON bit (see debug register 0x1f) to keep the PLL always
116 #define AT803X_MMD7_CLK25M 0x8016
117 #define AT803X_CLK_OUT_MASK GENMASK(4, 2)
118 #define AT803X_CLK_OUT_25MHZ_XTAL 0
119 #define AT803X_CLK_OUT_25MHZ_DSP 1
120 #define AT803X_CLK_OUT_50MHZ_PLL 2
121 #define AT803X_CLK_OUT_50MHZ_DSP 3
122 #define AT803X_CLK_OUT_62_5MHZ_PLL 4
123 #define AT803X_CLK_OUT_62_5MHZ_DSP 5
124 #define AT803X_CLK_OUT_125MHZ_PLL 6
125 #define AT803X_CLK_OUT_125MHZ_DSP 7
127 /* The AR8035 has another mask which is compatible with the AR8031/AR8033 mask
128 * but doesn't support choosing between XTAL/PLL and DSP.
130 #define AT8035_CLK_OUT_MASK GENMASK(4, 3)
132 #define AT803X_CLK_OUT_STRENGTH_MASK GENMASK(8, 7)
133 #define AT803X_CLK_OUT_STRENGTH_FULL 0
134 #define AT803X_CLK_OUT_STRENGTH_HALF 1
135 #define AT803X_CLK_OUT_STRENGTH_QUARTER 2
137 #define AT803X_DEFAULT_DOWNSHIFT 5
138 #define AT803X_MIN_DOWNSHIFT 2
139 #define AT803X_MAX_DOWNSHIFT 9
141 #define AT803X_MMD3_SMARTEEE_CTL1 0x805b
142 #define AT803X_MMD3_SMARTEEE_CTL2 0x805c
143 #define AT803X_MMD3_SMARTEEE_CTL3 0x805d
144 #define AT803X_MMD3_SMARTEEE_CTL3_LPI_EN BIT(8)
146 #define ATH9331_PHY_ID 0x004dd041
147 #define ATH8030_PHY_ID 0x004dd076
148 #define ATH8031_PHY_ID 0x004dd074
149 #define ATH8032_PHY_ID 0x004dd023
150 #define ATH8035_PHY_ID 0x004dd072
151 #define AT8030_PHY_ID_MASK 0xffffffef
153 #define QCA8327_PHY_ID 0x004dd034
154 #define QCA8337_PHY_ID 0x004dd036
155 #define QCA8K_PHY_ID_MASK 0xffffffff
157 #define QCA8K_DEVFLAGS_REVISION_MASK GENMASK(2, 0)
159 #define AT803X_PAGE_FIBER 0
160 #define AT803X_PAGE_COPPER 1
162 /* don't turn off internal PLL */
163 #define AT803X_KEEP_PLL_ENABLED BIT(0)
164 #define AT803X_DISABLE_SMARTEEE BIT(1)
166 MODULE_DESCRIPTION("Qualcomm Atheros AR803x PHY driver");
167 MODULE_AUTHOR("Matus Ujhelyi");
168 MODULE_LICENSE("GPL");
170 enum stat_access_type {
175 struct at803x_hw_stat {
179 enum stat_access_type access_type;
182 static struct at803x_hw_stat at803x_hw_stats[] = {
183 { "phy_idle_errors", 0xa, GENMASK(7, 0), PHY},
184 { "phy_receive_errors", 0x15, GENMASK(15, 0), PHY},
185 { "eee_wake_errors", 0x16, GENMASK(15, 0), MMD},
192 u8 smarteee_lpi_tw_1g;
193 u8 smarteee_lpi_tw_100m;
194 struct regulator_dev *vddio_rdev;
195 struct regulator_dev *vddh_rdev;
196 struct regulator *vddio;
197 u64 stats[ARRAY_SIZE(at803x_hw_stats)];
200 struct at803x_context {
209 static int at803x_debug_reg_write(struct phy_device *phydev, u16 reg, u16 data)
213 ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
217 return phy_write(phydev, AT803X_DEBUG_DATA, data);
220 static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg)
224 ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
228 return phy_read(phydev, AT803X_DEBUG_DATA);
231 static int at803x_debug_reg_mask(struct phy_device *phydev, u16 reg,
237 ret = at803x_debug_reg_read(phydev, reg);
245 return phy_write(phydev, AT803X_DEBUG_DATA, val);
248 static int at803x_write_page(struct phy_device *phydev, int page)
253 if (page == AT803X_PAGE_COPPER) {
254 set = AT803X_BT_BX_REG_SEL;
258 mask = AT803X_BT_BX_REG_SEL;
261 return __phy_modify(phydev, AT803X_REG_CHIP_CONFIG, mask, set);
264 static int at803x_read_page(struct phy_device *phydev)
266 int ccr = __phy_read(phydev, AT803X_REG_CHIP_CONFIG);
271 if (ccr & AT803X_BT_BX_REG_SEL)
272 return AT803X_PAGE_COPPER;
274 return AT803X_PAGE_FIBER;
277 static int at803x_enable_rx_delay(struct phy_device *phydev)
279 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_0, 0,
280 AT803X_DEBUG_RX_CLK_DLY_EN);
283 static int at803x_enable_tx_delay(struct phy_device *phydev)
285 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_5, 0,
286 AT803X_DEBUG_TX_CLK_DLY_EN);
289 static int at803x_disable_rx_delay(struct phy_device *phydev)
291 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_0,
292 AT803X_DEBUG_RX_CLK_DLY_EN, 0);
295 static int at803x_disable_tx_delay(struct phy_device *phydev)
297 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_5,
298 AT803X_DEBUG_TX_CLK_DLY_EN, 0);
301 /* save relevant PHY registers to private copy */
302 static void at803x_context_save(struct phy_device *phydev,
303 struct at803x_context *context)
305 context->bmcr = phy_read(phydev, MII_BMCR);
306 context->advertise = phy_read(phydev, MII_ADVERTISE);
307 context->control1000 = phy_read(phydev, MII_CTRL1000);
308 context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE);
309 context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED);
310 context->led_control = phy_read(phydev, AT803X_LED_CONTROL);
313 /* restore relevant PHY registers from private copy */
314 static void at803x_context_restore(struct phy_device *phydev,
315 const struct at803x_context *context)
317 phy_write(phydev, MII_BMCR, context->bmcr);
318 phy_write(phydev, MII_ADVERTISE, context->advertise);
319 phy_write(phydev, MII_CTRL1000, context->control1000);
320 phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable);
321 phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed);
322 phy_write(phydev, AT803X_LED_CONTROL, context->led_control);
325 static int at803x_set_wol(struct phy_device *phydev,
326 struct ethtool_wolinfo *wol)
328 struct net_device *ndev = phydev->attached_dev;
332 unsigned int i, offsets[] = {
333 AT803X_LOC_MAC_ADDR_32_47_OFFSET,
334 AT803X_LOC_MAC_ADDR_16_31_OFFSET,
335 AT803X_LOC_MAC_ADDR_0_15_OFFSET,
341 if (wol->wolopts & WAKE_MAGIC) {
342 mac = (const u8 *) ndev->dev_addr;
344 if (!is_valid_ether_addr(mac))
347 for (i = 0; i < 3; i++)
348 phy_write_mmd(phydev, AT803X_DEVICE_ADDR, offsets[i],
349 mac[(i * 2) + 1] | (mac[(i * 2)] << 8));
351 value = phy_read(phydev, AT803X_INTR_ENABLE);
352 value |= AT803X_INTR_ENABLE_WOL;
353 ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
356 value = phy_read(phydev, AT803X_INTR_STATUS);
358 value = phy_read(phydev, AT803X_INTR_ENABLE);
359 value &= (~AT803X_INTR_ENABLE_WOL);
360 ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
363 value = phy_read(phydev, AT803X_INTR_STATUS);
369 static void at803x_get_wol(struct phy_device *phydev,
370 struct ethtool_wolinfo *wol)
374 wol->supported = WAKE_MAGIC;
377 value = phy_read(phydev, AT803X_INTR_ENABLE);
378 if (value & AT803X_INTR_ENABLE_WOL)
379 wol->wolopts |= WAKE_MAGIC;
382 static int at803x_get_sset_count(struct phy_device *phydev)
384 return ARRAY_SIZE(at803x_hw_stats);
387 static void at803x_get_strings(struct phy_device *phydev, u8 *data)
391 for (i = 0; i < ARRAY_SIZE(at803x_hw_stats); i++) {
392 strscpy(data + i * ETH_GSTRING_LEN,
393 at803x_hw_stats[i].string, ETH_GSTRING_LEN);
397 static u64 at803x_get_stat(struct phy_device *phydev, int i)
399 struct at803x_hw_stat stat = at803x_hw_stats[i];
400 struct at803x_priv *priv = phydev->priv;
404 if (stat.access_type == MMD)
405 val = phy_read_mmd(phydev, MDIO_MMD_PCS, stat.reg);
407 val = phy_read(phydev, stat.reg);
412 val = val & stat.mask;
413 priv->stats[i] += val;
414 ret = priv->stats[i];
420 static void at803x_get_stats(struct phy_device *phydev,
421 struct ethtool_stats *stats, u64 *data)
425 for (i = 0; i < ARRAY_SIZE(at803x_hw_stats); i++)
426 data[i] = at803x_get_stat(phydev, i);
429 static int at803x_suspend(struct phy_device *phydev)
434 value = phy_read(phydev, AT803X_INTR_ENABLE);
435 wol_enabled = value & AT803X_INTR_ENABLE_WOL;
438 value = BMCR_ISOLATE;
442 phy_modify(phydev, MII_BMCR, 0, value);
447 static int at803x_resume(struct phy_device *phydev)
449 return phy_modify(phydev, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE, 0);
452 static int at803x_rgmii_reg_set_voltage_sel(struct regulator_dev *rdev,
453 unsigned int selector)
455 struct phy_device *phydev = rdev_get_drvdata(rdev);
458 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
459 0, AT803X_DEBUG_RGMII_1V8);
461 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
462 AT803X_DEBUG_RGMII_1V8, 0);
465 static int at803x_rgmii_reg_get_voltage_sel(struct regulator_dev *rdev)
467 struct phy_device *phydev = rdev_get_drvdata(rdev);
470 val = at803x_debug_reg_read(phydev, AT803X_DEBUG_REG_1F);
474 return (val & AT803X_DEBUG_RGMII_1V8) ? 1 : 0;
477 static const struct regulator_ops vddio_regulator_ops = {
478 .list_voltage = regulator_list_voltage_table,
479 .set_voltage_sel = at803x_rgmii_reg_set_voltage_sel,
480 .get_voltage_sel = at803x_rgmii_reg_get_voltage_sel,
483 static const unsigned int vddio_voltage_table[] = {
488 static const struct regulator_desc vddio_desc = {
490 .of_match = of_match_ptr("vddio-regulator"),
491 .n_voltages = ARRAY_SIZE(vddio_voltage_table),
492 .volt_table = vddio_voltage_table,
493 .ops = &vddio_regulator_ops,
494 .type = REGULATOR_VOLTAGE,
495 .owner = THIS_MODULE,
498 static const struct regulator_ops vddh_regulator_ops = {
501 static const struct regulator_desc vddh_desc = {
503 .of_match = of_match_ptr("vddh-regulator"),
506 .ops = &vddh_regulator_ops,
507 .type = REGULATOR_VOLTAGE,
508 .owner = THIS_MODULE,
511 static int at8031_register_regulators(struct phy_device *phydev)
513 struct at803x_priv *priv = phydev->priv;
514 struct device *dev = &phydev->mdio.dev;
515 struct regulator_config config = { };
518 config.driver_data = phydev;
520 priv->vddio_rdev = devm_regulator_register(dev, &vddio_desc, &config);
521 if (IS_ERR(priv->vddio_rdev)) {
522 phydev_err(phydev, "failed to register VDDIO regulator\n");
523 return PTR_ERR(priv->vddio_rdev);
526 priv->vddh_rdev = devm_regulator_register(dev, &vddh_desc, &config);
527 if (IS_ERR(priv->vddh_rdev)) {
528 phydev_err(phydev, "failed to register VDDH regulator\n");
529 return PTR_ERR(priv->vddh_rdev);
535 static bool at803x_match_phy_id(struct phy_device *phydev, u32 phy_id)
537 return (phydev->phy_id & phydev->drv->phy_id_mask)
538 == (phy_id & phydev->drv->phy_id_mask);
541 static int at803x_parse_dt(struct phy_device *phydev)
543 struct device_node *node = phydev->mdio.dev.of_node;
544 struct at803x_priv *priv = phydev->priv;
545 u32 freq, strength, tw;
549 if (!IS_ENABLED(CONFIG_OF_MDIO))
552 if (of_property_read_bool(node, "qca,disable-smarteee"))
553 priv->flags |= AT803X_DISABLE_SMARTEEE;
555 if (!of_property_read_u32(node, "qca,smarteee-tw-us-1g", &tw)) {
556 if (!tw || tw > 255) {
557 phydev_err(phydev, "invalid qca,smarteee-tw-us-1g\n");
560 priv->smarteee_lpi_tw_1g = tw;
563 if (!of_property_read_u32(node, "qca,smarteee-tw-us-100m", &tw)) {
564 if (!tw || tw > 255) {
565 phydev_err(phydev, "invalid qca,smarteee-tw-us-100m\n");
568 priv->smarteee_lpi_tw_100m = tw;
571 ret = of_property_read_u32(node, "qca,clk-out-frequency", &freq);
575 sel = AT803X_CLK_OUT_25MHZ_XTAL;
578 sel = AT803X_CLK_OUT_50MHZ_PLL;
581 sel = AT803X_CLK_OUT_62_5MHZ_PLL;
584 sel = AT803X_CLK_OUT_125MHZ_PLL;
587 phydev_err(phydev, "invalid qca,clk-out-frequency\n");
591 priv->clk_25m_reg |= FIELD_PREP(AT803X_CLK_OUT_MASK, sel);
592 priv->clk_25m_mask |= AT803X_CLK_OUT_MASK;
594 /* Fixup for the AR8030/AR8035. This chip has another mask and
595 * doesn't support the DSP reference. Eg. the lowest bit of the
596 * mask. The upper two bits select the same frequencies. Mask
597 * the lowest bit here.
600 * There was no datasheet for the AR8030 available so this is
601 * just a guess. But the AR8035 is listed as pin compatible
602 * to the AR8030 so there might be a good chance it works on
605 if (at803x_match_phy_id(phydev, ATH8030_PHY_ID) ||
606 at803x_match_phy_id(phydev, ATH8035_PHY_ID)) {
607 priv->clk_25m_reg &= AT8035_CLK_OUT_MASK;
608 priv->clk_25m_mask &= AT8035_CLK_OUT_MASK;
612 ret = of_property_read_u32(node, "qca,clk-out-strength", &strength);
614 priv->clk_25m_mask |= AT803X_CLK_OUT_STRENGTH_MASK;
616 case AR803X_STRENGTH_FULL:
617 priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_FULL;
619 case AR803X_STRENGTH_HALF:
620 priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_HALF;
622 case AR803X_STRENGTH_QUARTER:
623 priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_QUARTER;
626 phydev_err(phydev, "invalid qca,clk-out-strength\n");
631 /* Only supported on AR8031/AR8033, the AR8030/AR8035 use strapping
634 if (at803x_match_phy_id(phydev, ATH8031_PHY_ID)) {
635 if (of_property_read_bool(node, "qca,keep-pll-enabled"))
636 priv->flags |= AT803X_KEEP_PLL_ENABLED;
638 ret = at8031_register_regulators(phydev);
642 priv->vddio = devm_regulator_get_optional(&phydev->mdio.dev,
644 if (IS_ERR(priv->vddio)) {
645 phydev_err(phydev, "failed to get VDDIO regulator\n");
646 return PTR_ERR(priv->vddio);
653 static int at803x_probe(struct phy_device *phydev)
655 struct device *dev = &phydev->mdio.dev;
656 struct at803x_priv *priv;
659 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
665 ret = at803x_parse_dt(phydev);
670 ret = regulator_enable(priv->vddio);
675 /* Some bootloaders leave the fiber page selected.
676 * Switch to the copper page, as otherwise we read
677 * the PHY capabilities from the fiber side.
679 if (at803x_match_phy_id(phydev, ATH8031_PHY_ID)) {
680 phy_lock_mdio_bus(phydev);
681 ret = at803x_write_page(phydev, AT803X_PAGE_COPPER);
682 phy_unlock_mdio_bus(phydev);
691 regulator_disable(priv->vddio);
696 static void at803x_remove(struct phy_device *phydev)
698 struct at803x_priv *priv = phydev->priv;
701 regulator_disable(priv->vddio);
704 static int at803x_get_features(struct phy_device *phydev)
708 err = genphy_read_abilities(phydev);
712 if (!at803x_match_phy_id(phydev, ATH8031_PHY_ID))
715 /* AR8031/AR8033 have different status registers
716 * for copper and fiber operation. However, the
717 * extended status register is the same for both
720 * As a result of that, ESTATUS_1000_XFULL is set
721 * to 1 even when operating in copper TP mode.
723 * Remove this mode from the supported link modes,
724 * as this driver currently only supports copper
727 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
732 static int at803x_smarteee_config(struct phy_device *phydev)
734 struct at803x_priv *priv = phydev->priv;
735 u16 mask = 0, val = 0;
738 if (priv->flags & AT803X_DISABLE_SMARTEEE)
739 return phy_modify_mmd(phydev, MDIO_MMD_PCS,
740 AT803X_MMD3_SMARTEEE_CTL3,
741 AT803X_MMD3_SMARTEEE_CTL3_LPI_EN, 0);
743 if (priv->smarteee_lpi_tw_1g) {
745 val |= priv->smarteee_lpi_tw_1g << 8;
747 if (priv->smarteee_lpi_tw_100m) {
749 val |= priv->smarteee_lpi_tw_100m;
754 ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL1,
759 return phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL3,
760 AT803X_MMD3_SMARTEEE_CTL3_LPI_EN,
761 AT803X_MMD3_SMARTEEE_CTL3_LPI_EN);
764 static int at803x_clk_out_config(struct phy_device *phydev)
766 struct at803x_priv *priv = phydev->priv;
768 if (!priv->clk_25m_mask)
771 return phy_modify_mmd(phydev, MDIO_MMD_AN, AT803X_MMD7_CLK25M,
772 priv->clk_25m_mask, priv->clk_25m_reg);
775 static int at8031_pll_config(struct phy_device *phydev)
777 struct at803x_priv *priv = phydev->priv;
779 /* The default after hardware reset is PLL OFF. After a soft reset, the
780 * values are retained.
782 if (priv->flags & AT803X_KEEP_PLL_ENABLED)
783 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
784 0, AT803X_DEBUG_PLL_ON);
786 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
787 AT803X_DEBUG_PLL_ON, 0);
790 static int at803x_config_init(struct phy_device *phydev)
794 /* The RX and TX delay default is:
795 * after HW reset: RX delay enabled and TX delay disabled
796 * after SW reset: RX delay enabled, while TX delay retains the
797 * value before reset.
799 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
800 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
801 ret = at803x_enable_rx_delay(phydev);
803 ret = at803x_disable_rx_delay(phydev);
807 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
808 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
809 ret = at803x_enable_tx_delay(phydev);
811 ret = at803x_disable_tx_delay(phydev);
815 ret = at803x_smarteee_config(phydev);
819 ret = at803x_clk_out_config(phydev);
823 if (at803x_match_phy_id(phydev, ATH8031_PHY_ID)) {
824 ret = at8031_pll_config(phydev);
829 /* Ar803x extended next page bit is enabled by default. Cisco
830 * multigig switches read this bit and attempt to negotiate 10Gbps
831 * rates even if the next page bit is disabled. This is incorrect
832 * behaviour but we still need to accommodate it. XNP is only needed
833 * for 10Gbps support, so disable XNP.
835 return phy_modify(phydev, MII_ADVERTISE, MDIO_AN_CTRL1_XNP, 0);
838 static int at803x_ack_interrupt(struct phy_device *phydev)
842 err = phy_read(phydev, AT803X_INTR_STATUS);
844 return (err < 0) ? err : 0;
847 static int at803x_config_intr(struct phy_device *phydev)
852 value = phy_read(phydev, AT803X_INTR_ENABLE);
854 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
855 /* Clear any pending interrupts */
856 err = at803x_ack_interrupt(phydev);
860 value |= AT803X_INTR_ENABLE_AUTONEG_ERR;
861 value |= AT803X_INTR_ENABLE_SPEED_CHANGED;
862 value |= AT803X_INTR_ENABLE_DUPLEX_CHANGED;
863 value |= AT803X_INTR_ENABLE_LINK_FAIL;
864 value |= AT803X_INTR_ENABLE_LINK_SUCCESS;
866 err = phy_write(phydev, AT803X_INTR_ENABLE, value);
868 err = phy_write(phydev, AT803X_INTR_ENABLE, 0);
872 /* Clear any pending interrupts */
873 err = at803x_ack_interrupt(phydev);
879 static irqreturn_t at803x_handle_interrupt(struct phy_device *phydev)
881 int irq_status, int_enabled;
883 irq_status = phy_read(phydev, AT803X_INTR_STATUS);
884 if (irq_status < 0) {
889 /* Read the current enabled interrupts */
890 int_enabled = phy_read(phydev, AT803X_INTR_ENABLE);
891 if (int_enabled < 0) {
896 /* See if this was one of our enabled interrupts */
897 if (!(irq_status & int_enabled))
900 phy_trigger_machine(phydev);
905 static void at803x_link_change_notify(struct phy_device *phydev)
908 * Conduct a hardware reset for AT8030 every time a link loss is
909 * signalled. This is necessary to circumvent a hardware bug that
910 * occurs when the cable is unplugged while TX packets are pending
911 * in the FIFO. In such cases, the FIFO enters an error mode it
912 * cannot recover from by software.
914 if (phydev->state == PHY_NOLINK && phydev->mdio.reset_gpio) {
915 struct at803x_context context;
917 at803x_context_save(phydev, &context);
919 phy_device_reset(phydev, 1);
921 phy_device_reset(phydev, 0);
924 at803x_context_restore(phydev, &context);
926 phydev_dbg(phydev, "%s(): phy was reset\n", __func__);
930 static int at803x_read_status(struct phy_device *phydev)
932 int ss, err, old_link = phydev->link;
934 /* Update the link, but return if there was an error */
935 err = genphy_update_link(phydev);
939 /* why bother the PHY if nothing can have changed */
940 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
943 phydev->speed = SPEED_UNKNOWN;
944 phydev->duplex = DUPLEX_UNKNOWN;
946 phydev->asym_pause = 0;
948 err = genphy_read_lpa(phydev);
952 /* Read the AT8035 PHY-Specific Status register, which indicates the
953 * speed and duplex that the PHY is actually using, irrespective of
954 * whether we are in autoneg mode or not.
956 ss = phy_read(phydev, AT803X_SPECIFIC_STATUS);
960 if (ss & AT803X_SS_SPEED_DUPLEX_RESOLVED) {
963 sfc = phy_read(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL);
967 switch (ss & AT803X_SS_SPEED_MASK) {
968 case AT803X_SS_SPEED_10:
969 phydev->speed = SPEED_10;
971 case AT803X_SS_SPEED_100:
972 phydev->speed = SPEED_100;
974 case AT803X_SS_SPEED_1000:
975 phydev->speed = SPEED_1000;
978 if (ss & AT803X_SS_DUPLEX)
979 phydev->duplex = DUPLEX_FULL;
981 phydev->duplex = DUPLEX_HALF;
983 if (ss & AT803X_SS_MDIX)
984 phydev->mdix = ETH_TP_MDI_X;
986 phydev->mdix = ETH_TP_MDI;
988 switch (FIELD_GET(AT803X_SFC_MDI_CROSSOVER_MODE_M, sfc)) {
989 case AT803X_SFC_MANUAL_MDI:
990 phydev->mdix_ctrl = ETH_TP_MDI;
992 case AT803X_SFC_MANUAL_MDIX:
993 phydev->mdix_ctrl = ETH_TP_MDI_X;
995 case AT803X_SFC_AUTOMATIC_CROSSOVER:
996 phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
1001 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete)
1002 phy_resolve_aneg_pause(phydev);
1007 static int at803x_config_mdix(struct phy_device *phydev, u8 ctrl)
1013 val = AT803X_SFC_MANUAL_MDI;
1016 val = AT803X_SFC_MANUAL_MDIX;
1018 case ETH_TP_MDI_AUTO:
1019 val = AT803X_SFC_AUTOMATIC_CROSSOVER;
1025 return phy_modify_changed(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL,
1026 AT803X_SFC_MDI_CROSSOVER_MODE_M,
1027 FIELD_PREP(AT803X_SFC_MDI_CROSSOVER_MODE_M, val));
1030 static int at803x_config_aneg(struct phy_device *phydev)
1034 ret = at803x_config_mdix(phydev, phydev->mdix_ctrl);
1038 /* Changes of the midx bits are disruptive to the normal operation;
1039 * therefore any changes to these registers must be followed by a
1040 * software reset to take effect.
1043 ret = genphy_soft_reset(phydev);
1048 return genphy_config_aneg(phydev);
1051 static int at803x_get_downshift(struct phy_device *phydev, u8 *d)
1055 val = phy_read(phydev, AT803X_SMART_SPEED);
1059 if (val & AT803X_SMART_SPEED_ENABLE)
1060 *d = FIELD_GET(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, val) + 2;
1062 *d = DOWNSHIFT_DEV_DISABLE;
1067 static int at803x_set_downshift(struct phy_device *phydev, u8 cnt)
1073 case DOWNSHIFT_DEV_DEFAULT_COUNT:
1074 cnt = AT803X_DEFAULT_DOWNSHIFT;
1076 case AT803X_MIN_DOWNSHIFT ... AT803X_MAX_DOWNSHIFT:
1077 set = AT803X_SMART_SPEED_ENABLE |
1078 AT803X_SMART_SPEED_BYPASS_TIMER |
1079 FIELD_PREP(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, cnt - 2);
1080 mask = AT803X_SMART_SPEED_RETRY_LIMIT_MASK;
1082 case DOWNSHIFT_DEV_DISABLE:
1084 mask = AT803X_SMART_SPEED_ENABLE |
1085 AT803X_SMART_SPEED_BYPASS_TIMER;
1091 ret = phy_modify_changed(phydev, AT803X_SMART_SPEED, mask, set);
1093 /* After changing the smart speed settings, we need to perform a
1094 * software reset, use phy_init_hw() to make sure we set the
1095 * reapply any values which might got lost during software reset.
1098 ret = phy_init_hw(phydev);
1103 static int at803x_get_tunable(struct phy_device *phydev,
1104 struct ethtool_tunable *tuna, void *data)
1107 case ETHTOOL_PHY_DOWNSHIFT:
1108 return at803x_get_downshift(phydev, data);
1114 static int at803x_set_tunable(struct phy_device *phydev,
1115 struct ethtool_tunable *tuna, const void *data)
1118 case ETHTOOL_PHY_DOWNSHIFT:
1119 return at803x_set_downshift(phydev, *(const u8 *)data);
1125 static int at803x_cable_test_result_trans(u16 status)
1127 switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
1128 case AT803X_CDT_STATUS_STAT_NORMAL:
1129 return ETHTOOL_A_CABLE_RESULT_CODE_OK;
1130 case AT803X_CDT_STATUS_STAT_SHORT:
1131 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
1132 case AT803X_CDT_STATUS_STAT_OPEN:
1133 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
1134 case AT803X_CDT_STATUS_STAT_FAIL:
1136 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
1140 static bool at803x_cdt_test_failed(u16 status)
1142 return FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status) ==
1143 AT803X_CDT_STATUS_STAT_FAIL;
1146 static bool at803x_cdt_fault_length_valid(u16 status)
1148 switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
1149 case AT803X_CDT_STATUS_STAT_OPEN:
1150 case AT803X_CDT_STATUS_STAT_SHORT:
1156 static int at803x_cdt_fault_length(u16 status)
1160 /* According to the datasheet the distance to the fault is
1161 * DELTA_TIME * 0.824 meters.
1163 * The author suspect the correct formula is:
1165 * fault_distance = DELTA_TIME * (c * VF) / 125MHz / 2
1167 * where c is the speed of light, VF is the velocity factor of
1168 * the twisted pair cable, 125MHz the counter frequency and
1169 * we need to divide by 2 because the hardware will measure the
1170 * round trip time to the fault and back to the PHY.
1172 * With a VF of 0.69 we get the factor 0.824 mentioned in the
1175 dt = FIELD_GET(AT803X_CDT_STATUS_DELTA_TIME_MASK, status);
1177 return (dt * 824) / 10;
1180 static int at803x_cdt_start(struct phy_device *phydev, int pair)
1184 cdt = FIELD_PREP(AT803X_CDT_MDI_PAIR_MASK, pair) |
1185 AT803X_CDT_ENABLE_TEST;
1187 return phy_write(phydev, AT803X_CDT, cdt);
1190 static int at803x_cdt_wait_for_completion(struct phy_device *phydev)
1194 /* One test run takes about 25ms */
1195 ret = phy_read_poll_timeout(phydev, AT803X_CDT, val,
1196 !(val & AT803X_CDT_ENABLE_TEST),
1197 30000, 100000, true);
1199 return ret < 0 ? ret : 0;
1202 static int at803x_cable_test_one_pair(struct phy_device *phydev, int pair)
1204 static const int ethtool_pair[] = {
1205 ETHTOOL_A_CABLE_PAIR_A,
1206 ETHTOOL_A_CABLE_PAIR_B,
1207 ETHTOOL_A_CABLE_PAIR_C,
1208 ETHTOOL_A_CABLE_PAIR_D,
1212 ret = at803x_cdt_start(phydev, pair);
1216 ret = at803x_cdt_wait_for_completion(phydev);
1220 val = phy_read(phydev, AT803X_CDT_STATUS);
1224 if (at803x_cdt_test_failed(val))
1227 ethnl_cable_test_result(phydev, ethtool_pair[pair],
1228 at803x_cable_test_result_trans(val));
1230 if (at803x_cdt_fault_length_valid(val))
1231 ethnl_cable_test_fault_length(phydev, ethtool_pair[pair],
1232 at803x_cdt_fault_length(val));
1237 static int at803x_cable_test_get_status(struct phy_device *phydev,
1240 unsigned long pair_mask;
1244 if (phydev->phy_id == ATH9331_PHY_ID ||
1245 phydev->phy_id == ATH8032_PHY_ID)
1252 /* According to the datasheet the CDT can be performed when
1253 * there is no link partner or when the link partner is
1254 * auto-negotiating. Starting the test will restart the AN
1255 * automatically. It seems that doing this repeatedly we will
1256 * get a slot where our link partner won't disturb our
1259 while (pair_mask && retries--) {
1260 for_each_set_bit(pair, &pair_mask, 4) {
1261 ret = at803x_cable_test_one_pair(phydev, pair);
1265 clear_bit(pair, &pair_mask);
1276 static int at803x_cable_test_start(struct phy_device *phydev)
1278 /* Enable auto-negotiation, but advertise no capabilities, no link
1279 * will be established. A restart of the auto-negotiation is not
1280 * required, because the cable test will automatically break the link.
1282 phy_write(phydev, MII_BMCR, BMCR_ANENABLE);
1283 phy_write(phydev, MII_ADVERTISE, ADVERTISE_CSMA);
1284 if (phydev->phy_id != ATH9331_PHY_ID &&
1285 phydev->phy_id != ATH8032_PHY_ID)
1286 phy_write(phydev, MII_CTRL1000, 0);
1288 /* we do all the (time consuming) work later */
1292 static int qca83xx_config_init(struct phy_device *phydev)
1296 switch_revision = phydev->dev_flags & QCA8K_DEVFLAGS_REVISION_MASK;
1298 switch (switch_revision) {
1300 /* For 100M waveform */
1301 at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_0, 0x02ea);
1302 /* Turn on Gigabit clock */
1303 at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_3D, 0x68a0);
1307 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0x0);
1310 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_AZ_DEBUG, 0x803f);
1311 at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_3D, 0x6860);
1312 at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_5, 0x2c46);
1313 at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_3C, 0x6000);
1320 static struct phy_driver at803x_driver[] = {
1322 /* Qualcomm Atheros AR8035 */
1323 PHY_ID_MATCH_EXACT(ATH8035_PHY_ID),
1324 .name = "Qualcomm Atheros AR8035",
1325 .flags = PHY_POLL_CABLE_TEST,
1326 .probe = at803x_probe,
1327 .remove = at803x_remove,
1328 .config_aneg = at803x_config_aneg,
1329 .config_init = at803x_config_init,
1330 .soft_reset = genphy_soft_reset,
1331 .set_wol = at803x_set_wol,
1332 .get_wol = at803x_get_wol,
1333 .suspend = at803x_suspend,
1334 .resume = at803x_resume,
1335 /* PHY_GBIT_FEATURES */
1336 .read_status = at803x_read_status,
1337 .config_intr = at803x_config_intr,
1338 .handle_interrupt = at803x_handle_interrupt,
1339 .get_tunable = at803x_get_tunable,
1340 .set_tunable = at803x_set_tunable,
1341 .cable_test_start = at803x_cable_test_start,
1342 .cable_test_get_status = at803x_cable_test_get_status,
1344 /* Qualcomm Atheros AR8030 */
1345 .phy_id = ATH8030_PHY_ID,
1346 .name = "Qualcomm Atheros AR8030",
1347 .phy_id_mask = AT8030_PHY_ID_MASK,
1348 .probe = at803x_probe,
1349 .remove = at803x_remove,
1350 .config_init = at803x_config_init,
1351 .link_change_notify = at803x_link_change_notify,
1352 .set_wol = at803x_set_wol,
1353 .get_wol = at803x_get_wol,
1354 .suspend = at803x_suspend,
1355 .resume = at803x_resume,
1356 /* PHY_BASIC_FEATURES */
1357 .config_intr = at803x_config_intr,
1358 .handle_interrupt = at803x_handle_interrupt,
1360 /* Qualcomm Atheros AR8031/AR8033 */
1361 PHY_ID_MATCH_EXACT(ATH8031_PHY_ID),
1362 .name = "Qualcomm Atheros AR8031/AR8033",
1363 .flags = PHY_POLL_CABLE_TEST,
1364 .probe = at803x_probe,
1365 .remove = at803x_remove,
1366 .config_init = at803x_config_init,
1367 .config_aneg = at803x_config_aneg,
1368 .soft_reset = genphy_soft_reset,
1369 .set_wol = at803x_set_wol,
1370 .get_wol = at803x_get_wol,
1371 .suspend = at803x_suspend,
1372 .resume = at803x_resume,
1373 .read_page = at803x_read_page,
1374 .write_page = at803x_write_page,
1375 .get_features = at803x_get_features,
1376 .read_status = at803x_read_status,
1377 .config_intr = &at803x_config_intr,
1378 .handle_interrupt = at803x_handle_interrupt,
1379 .get_tunable = at803x_get_tunable,
1380 .set_tunable = at803x_set_tunable,
1381 .cable_test_start = at803x_cable_test_start,
1382 .cable_test_get_status = at803x_cable_test_get_status,
1384 /* Qualcomm Atheros AR8032 */
1385 PHY_ID_MATCH_EXACT(ATH8032_PHY_ID),
1386 .name = "Qualcomm Atheros AR8032",
1387 .probe = at803x_probe,
1388 .remove = at803x_remove,
1389 .flags = PHY_POLL_CABLE_TEST,
1390 .config_init = at803x_config_init,
1391 .link_change_notify = at803x_link_change_notify,
1392 .set_wol = at803x_set_wol,
1393 .get_wol = at803x_get_wol,
1394 .suspend = at803x_suspend,
1395 .resume = at803x_resume,
1396 /* PHY_BASIC_FEATURES */
1397 .config_intr = at803x_config_intr,
1398 .handle_interrupt = at803x_handle_interrupt,
1399 .cable_test_start = at803x_cable_test_start,
1400 .cable_test_get_status = at803x_cable_test_get_status,
1402 /* ATHEROS AR9331 */
1403 PHY_ID_MATCH_EXACT(ATH9331_PHY_ID),
1404 .name = "Qualcomm Atheros AR9331 built-in PHY",
1405 .suspend = at803x_suspend,
1406 .resume = at803x_resume,
1407 .flags = PHY_POLL_CABLE_TEST,
1408 /* PHY_BASIC_FEATURES */
1409 .config_intr = &at803x_config_intr,
1410 .handle_interrupt = at803x_handle_interrupt,
1411 .cable_test_start = at803x_cable_test_start,
1412 .cable_test_get_status = at803x_cable_test_get_status,
1413 .read_status = at803x_read_status,
1414 .soft_reset = genphy_soft_reset,
1415 .config_aneg = at803x_config_aneg,
1418 .phy_id = QCA8337_PHY_ID,
1419 .phy_id_mask = QCA8K_PHY_ID_MASK,
1420 .name = "QCA PHY 8337",
1421 /* PHY_GBIT_FEATURES */
1422 .probe = at803x_probe,
1423 .flags = PHY_IS_INTERNAL,
1424 .config_init = qca83xx_config_init,
1425 .soft_reset = genphy_soft_reset,
1426 .get_sset_count = at803x_get_sset_count,
1427 .get_strings = at803x_get_strings,
1428 .get_stats = at803x_get_stats,
1431 module_phy_driver(at803x_driver);
1433 static struct mdio_device_id __maybe_unused atheros_tbl[] = {
1434 { ATH8030_PHY_ID, AT8030_PHY_ID_MASK },
1435 { PHY_ID_MATCH_EXACT(ATH8031_PHY_ID) },
1436 { PHY_ID_MATCH_EXACT(ATH8032_PHY_ID) },
1437 { PHY_ID_MATCH_EXACT(ATH8035_PHY_ID) },
1438 { PHY_ID_MATCH_EXACT(ATH9331_PHY_ID) },
1442 MODULE_DEVICE_TABLE(mdio, atheros_tbl);