1 // SPDX-License-Identifier: GPL-2.0
2 /* Driver for the Texas Instruments DP83TG720 PHY
5 #include <linux/bitfield.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
10 #define DP83TG720S_PHY_ID 0x2000a284
12 /* MDIO_MMD_VEND2 registers */
13 #define DP83TG720S_MII_REG_10 0x10
14 #define DP83TG720S_STS_MII_INT BIT(7)
15 #define DP83TG720S_LINK_STATUS BIT(0)
17 #define DP83TG720S_PHY_RESET 0x1f
18 #define DP83TG720S_HW_RESET BIT(15)
20 #define DP83TG720S_LPS_CFG3 0x18c
21 /* Power modes are documented as bit fields but used as values */
22 /* Power Mode 0 is Normal mode */
23 #define DP83TG720S_LPS_CFG3_PWR_MODE_0 BIT(0)
25 #define DP83TG720S_RGMII_DELAY_CTRL 0x602
26 /* In RGMII mode, Enable or disable the internal delay for RXD */
27 #define DP83TG720S_RGMII_RX_CLK_SEL BIT(1)
28 /* In RGMII mode, Enable or disable the internal delay for TXD */
29 #define DP83TG720S_RGMII_TX_CLK_SEL BIT(0)
31 #define DP83TG720S_SQI_REG_1 0x871
32 #define DP83TG720S_SQI_OUT_WORST GENMASK(7, 5)
33 #define DP83TG720S_SQI_OUT GENMASK(3, 1)
35 #define DP83TG720_SQI_MAX 7
37 static int dp83tg720_config_aneg(struct phy_device *phydev)
41 /* Autoneg is not supported and this PHY supports only one speed.
42 * We need to care only about master/slave configuration if it was
45 ret = genphy_c45_pma_baset1_setup_master_slave(phydev);
49 /* Re-read role configuration to make changes visible even if
50 * the link is in administrative down state.
52 return genphy_c45_pma_baset1_read_master_slave(phydev);
55 static int dp83tg720_read_status(struct phy_device *phydev)
61 phydev->asym_pause = 0;
63 /* Most of Clause 45 registers are not present, so we can't use
64 * genphy_c45_read_status() here.
66 phy_sts = phy_read(phydev, DP83TG720S_MII_REG_10);
67 phydev->link = !!(phy_sts & DP83TG720S_LINK_STATUS);
69 /* According to the "DP83TC81x, DP83TG72x Software
70 * Implementation Guide", the PHY needs to be reset after a
71 * link loss or if no link is created after at least 100ms.
73 * Currently we are polling with the PHY_STATE_TIME (1000ms)
74 * interval, which is still enough for not automotive use cases.
76 ret = phy_init_hw(phydev);
80 /* After HW reset we need to restore master/slave configuration.
81 * genphy_c45_pma_baset1_read_master_slave() call will be done
82 * by the dp83tg720_config_aneg() function.
84 ret = dp83tg720_config_aneg(phydev);
88 phydev->speed = SPEED_UNKNOWN;
89 phydev->duplex = DUPLEX_UNKNOWN;
91 /* PMA/PMD control 1 register (Register 1.0) is present, but it
92 * doesn't contain the link speed information.
93 * So genphy_c45_read_pma() can't be used here.
95 ret = genphy_c45_pma_baset1_read_master_slave(phydev);
99 phydev->duplex = DUPLEX_FULL;
100 phydev->speed = SPEED_1000;
106 static int dp83tg720_get_sqi(struct phy_device *phydev)
113 ret = phy_read_mmd(phydev, MDIO_MMD_VEND2, DP83TG720S_SQI_REG_1);
117 return FIELD_GET(DP83TG720S_SQI_OUT, ret);
120 static int dp83tg720_get_sqi_max(struct phy_device *phydev)
122 return DP83TG720_SQI_MAX;
125 static int dp83tg720_config_rgmii_delay(struct phy_device *phydev)
127 u16 rgmii_delay_mask;
130 switch (phydev->interface) {
131 case PHY_INTERFACE_MODE_RGMII:
134 case PHY_INTERFACE_MODE_RGMII_ID:
135 rgmii_delay = DP83TG720S_RGMII_RX_CLK_SEL |
136 DP83TG720S_RGMII_TX_CLK_SEL;
138 case PHY_INTERFACE_MODE_RGMII_RXID:
139 rgmii_delay = DP83TG720S_RGMII_RX_CLK_SEL;
141 case PHY_INTERFACE_MODE_RGMII_TXID:
142 rgmii_delay = DP83TG720S_RGMII_TX_CLK_SEL;
148 rgmii_delay_mask = DP83TG720S_RGMII_RX_CLK_SEL |
149 DP83TG720S_RGMII_TX_CLK_SEL;
151 return phy_modify_mmd(phydev, MDIO_MMD_VEND2,
152 DP83TG720S_RGMII_DELAY_CTRL, rgmii_delay_mask,
156 static int dp83tg720_config_init(struct phy_device *phydev)
160 /* Software Restart is not enough to recover from a link failure.
161 * Using Hardware Reset instead.
163 ret = phy_write(phydev, DP83TG720S_PHY_RESET, DP83TG720S_HW_RESET);
167 /* Wait until MDC can be used again.
168 * The wait value of one 1ms is documented in "DP83TG720S-Q1 1000BASE-T1
169 * Automotive Ethernet PHY with SGMII and RGMII" datasheet.
171 usleep_range(1000, 2000);
173 if (phy_interface_is_rgmii(phydev)) {
174 ret = dp83tg720_config_rgmii_delay(phydev);
179 /* In case the PHY is bootstrapped in managed mode, we need to
182 ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, DP83TG720S_LPS_CFG3,
183 DP83TG720S_LPS_CFG3_PWR_MODE_0);
187 /* Make role configuration visible for ethtool on init and after
190 return genphy_c45_pma_baset1_read_master_slave(phydev);
193 static struct phy_driver dp83tg720_driver[] = {
195 PHY_ID_MATCH_MODEL(DP83TG720S_PHY_ID),
196 .name = "TI DP83TG720S",
198 .config_aneg = dp83tg720_config_aneg,
199 .read_status = dp83tg720_read_status,
200 .get_features = genphy_c45_pma_read_ext_abilities,
201 .config_init = dp83tg720_config_init,
202 .get_sqi = dp83tg720_get_sqi,
203 .get_sqi_max = dp83tg720_get_sqi_max,
205 .suspend = genphy_suspend,
206 .resume = genphy_resume,
208 module_phy_driver(dp83tg720_driver);
210 static struct mdio_device_id __maybe_unused dp83tg720_tbl[] = {
211 { PHY_ID_MATCH_MODEL(DP83TG720S_PHY_ID) },
214 MODULE_DEVICE_TABLE(mdio, dp83tg720_tbl);
216 MODULE_DESCRIPTION("Texas Instruments DP83TG720S PHY driver");
218 MODULE_LICENSE("GPL");