1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
3 * Driver for Analog Devices Industrial Ethernet T1L PHYs
5 * Copyright 2020 Analog Devices Inc.
7 #include <linux/kernel.h>
8 #include <linux/bitfield.h>
9 #include <linux/delay.h>
10 #include <linux/errno.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/mii.h>
14 #include <linux/phy.h>
15 #include <linux/property.h>
17 #define PHY_ID_ADIN1100 0x0283bc81
18 #define PHY_ID_ADIN1110 0x0283bc91
19 #define PHY_ID_ADIN2111 0x0283bca1
21 #define ADIN_PHY_SUBSYS_IRQ_MASK 0x0021
22 #define ADIN_LINK_STAT_CHNG_IRQ_EN BIT(1)
24 #define ADIN_PHY_SUBSYS_IRQ_STATUS 0x0011
25 #define ADIN_LINK_STAT_CHNG BIT(1)
27 #define ADIN_FORCED_MODE 0x8000
28 #define ADIN_FORCED_MODE_EN BIT(0)
30 #define ADIN_CRSM_SFT_RST 0x8810
31 #define ADIN_CRSM_SFT_RST_EN BIT(0)
33 #define ADIN_CRSM_SFT_PD_CNTRL 0x8812
34 #define ADIN_CRSM_SFT_PD_CNTRL_EN BIT(0)
36 #define ADIN_AN_PHY_INST_STATUS 0x8030
37 #define ADIN_IS_CFG_SLV BIT(2)
38 #define ADIN_IS_CFG_MST BIT(3)
40 #define ADIN_CRSM_STAT 0x8818
41 #define ADIN_CRSM_SFT_PD_RDY BIT(1)
42 #define ADIN_CRSM_SYS_RDY BIT(0)
44 #define ADIN_MSE_VAL 0x830B
46 #define ADIN_SQI_MAX 7
48 struct adin_mse_sqi_range
{
53 static const struct adin_mse_sqi_range adin_mse_sqi_map
[] = {
65 * struct adin_priv - ADIN PHY driver private data
66 * @tx_level_2v4_able: set if the PHY supports 2.4V TX levels (10BASE-T1L)
67 * @tx_level_2v4: set if the PHY requests 2.4V TX levels (10BASE-T1L)
68 * @tx_level_prop_present: set if the TX level is specified in DT
71 unsigned int tx_level_2v4_able
:1;
72 unsigned int tx_level_2v4
:1;
73 unsigned int tx_level_prop_present
:1;
76 static int adin_read_status(struct phy_device
*phydev
)
80 ret
= genphy_c45_read_status(phydev
);
84 ret
= phy_read_mmd(phydev
, MDIO_MMD_AN
, ADIN_AN_PHY_INST_STATUS
);
88 if (ret
& ADIN_IS_CFG_SLV
)
89 phydev
->master_slave_state
= MASTER_SLAVE_STATE_SLAVE
;
91 if (ret
& ADIN_IS_CFG_MST
)
92 phydev
->master_slave_state
= MASTER_SLAVE_STATE_MASTER
;
97 static int adin_config_aneg(struct phy_device
*phydev
)
99 struct adin_priv
*priv
= phydev
->priv
;
102 if (phydev
->autoneg
== AUTONEG_DISABLE
) {
103 ret
= genphy_c45_pma_setup_forced(phydev
);
107 if (priv
->tx_level_prop_present
&& priv
->tx_level_2v4
)
108 ret
= phy_set_bits_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_B10L_PMA_CTRL
,
109 MDIO_PMA_10T1L_CTRL_2V4_EN
);
111 ret
= phy_clear_bits_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_B10L_PMA_CTRL
,
112 MDIO_PMA_10T1L_CTRL_2V4_EN
);
116 /* Force PHY to use above configurations */
117 return phy_set_bits_mmd(phydev
, MDIO_MMD_AN
, ADIN_FORCED_MODE
, ADIN_FORCED_MODE_EN
);
120 ret
= phy_clear_bits_mmd(phydev
, MDIO_MMD_AN
, ADIN_FORCED_MODE
, ADIN_FORCED_MODE_EN
);
124 /* Request increased transmit level from LP. */
125 if (priv
->tx_level_prop_present
&& priv
->tx_level_2v4
) {
126 ret
= phy_set_bits_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_T1_ADV_H
,
127 MDIO_AN_T1_ADV_H_10L_TX_HI
|
128 MDIO_AN_T1_ADV_H_10L_TX_HI_REQ
);
133 /* Disable 2.4 Vpp transmit level. */
134 if ((priv
->tx_level_prop_present
&& !priv
->tx_level_2v4
) || !priv
->tx_level_2v4_able
) {
135 ret
= phy_clear_bits_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_T1_ADV_H
,
136 MDIO_AN_T1_ADV_H_10L_TX_HI
|
137 MDIO_AN_T1_ADV_H_10L_TX_HI_REQ
);
142 return genphy_c45_config_aneg(phydev
);
145 static int adin_phy_ack_intr(struct phy_device
*phydev
)
147 /* Clear pending interrupts */
148 int rc
= phy_read_mmd(phydev
, MDIO_MMD_VEND2
,
149 ADIN_PHY_SUBSYS_IRQ_STATUS
);
151 return rc
< 0 ? rc
: 0;
154 static int adin_config_intr(struct phy_device
*phydev
)
159 ret
= adin_phy_ack_intr(phydev
);
163 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
)
164 irq_mask
= ADIN_LINK_STAT_CHNG_IRQ_EN
;
168 return phy_modify_mmd(phydev
, MDIO_MMD_VEND2
,
169 ADIN_PHY_SUBSYS_IRQ_MASK
,
170 ADIN_LINK_STAT_CHNG_IRQ_EN
, irq_mask
);
173 static irqreturn_t
adin_phy_handle_interrupt(struct phy_device
*phydev
)
177 irq_status
= phy_read_mmd(phydev
, MDIO_MMD_VEND2
,
178 ADIN_PHY_SUBSYS_IRQ_STATUS
);
179 if (irq_status
< 0) {
184 if (!(irq_status
& ADIN_LINK_STAT_CHNG
))
187 phy_trigger_machine(phydev
);
192 static int adin_set_powerdown_mode(struct phy_device
*phydev
, bool en
)
197 val
= en
? ADIN_CRSM_SFT_PD_CNTRL_EN
: 0;
198 ret
= phy_write_mmd(phydev
, MDIO_MMD_VEND1
,
199 ADIN_CRSM_SFT_PD_CNTRL
, val
);
203 return phy_read_mmd_poll_timeout(phydev
, MDIO_MMD_VEND1
, ADIN_CRSM_STAT
, ret
,
204 (ret
& ADIN_CRSM_SFT_PD_RDY
) == val
,
208 static int adin_suspend(struct phy_device
*phydev
)
210 return adin_set_powerdown_mode(phydev
, true);
213 static int adin_resume(struct phy_device
*phydev
)
215 return adin_set_powerdown_mode(phydev
, false);
218 static int adin_set_loopback(struct phy_device
*phydev
, bool enable
)
221 return phy_set_bits_mmd(phydev
, MDIO_MMD_PCS
, MDIO_PCS_10T1L_CTRL
,
224 /* PCS loopback (according to 10BASE-T1L spec) */
225 return phy_clear_bits_mmd(phydev
, MDIO_MMD_PCS
, MDIO_PCS_10T1L_CTRL
,
229 static int adin_soft_reset(struct phy_device
*phydev
)
233 ret
= phy_set_bits_mmd(phydev
, MDIO_MMD_VEND1
, ADIN_CRSM_SFT_RST
, ADIN_CRSM_SFT_RST_EN
);
237 return phy_read_mmd_poll_timeout(phydev
, MDIO_MMD_VEND1
, ADIN_CRSM_STAT
, ret
,
238 (ret
& ADIN_CRSM_SYS_RDY
),
242 static int adin_get_features(struct phy_device
*phydev
)
244 struct adin_priv
*priv
= phydev
->priv
;
245 struct device
*dev
= &phydev
->mdio
.dev
;
249 ret
= phy_read_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_PMA_10T1L_STAT
);
253 /* This depends on the voltage level from the power source */
254 priv
->tx_level_2v4_able
= !!(ret
& MDIO_PMA_10T1L_STAT_2V4_ABLE
);
256 phydev_dbg(phydev
, "PHY supports 2.4V TX level: %s\n",
257 priv
->tx_level_2v4_able
? "yes" : "no");
259 priv
->tx_level_prop_present
= device_property_present(dev
, "phy-10base-t1l-2.4vpp");
260 if (priv
->tx_level_prop_present
) {
261 ret
= device_property_read_u8(dev
, "phy-10base-t1l-2.4vpp", &val
);
265 priv
->tx_level_2v4
= val
;
266 if (!priv
->tx_level_2v4
&& priv
->tx_level_2v4_able
)
268 "PHY supports 2.4V TX level, but disabled via config\n");
271 linkmode_set_bit_array(phy_basic_ports_array
, ARRAY_SIZE(phy_basic_ports_array
),
274 return genphy_c45_pma_read_abilities(phydev
);
277 static int adin_get_sqi(struct phy_device
*phydev
)
283 ret
= phy_read_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_STAT1
);
286 else if (!(ret
& MDIO_STAT1_LSTATUS
))
289 ret
= phy_read_mmd(phydev
, MDIO_STAT1
, ADIN_MSE_VAL
);
293 mse_val
= 0xFFFF & ret
;
294 for (sqi
= 0; sqi
< ARRAY_SIZE(adin_mse_sqi_map
); sqi
++) {
295 if (mse_val
>= adin_mse_sqi_map
[sqi
].start
&& mse_val
<= adin_mse_sqi_map
[sqi
].end
)
302 static int adin_get_sqi_max(struct phy_device
*phydev
)
307 static int adin_probe(struct phy_device
*phydev
)
309 struct device
*dev
= &phydev
->mdio
.dev
;
310 struct adin_priv
*priv
;
312 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
321 static struct phy_driver adin_driver
[] = {
323 .phy_id
= PHY_ID_ADIN1100
,
324 .phy_id_mask
= 0xffffffcf,
326 .get_features
= adin_get_features
,
327 .soft_reset
= adin_soft_reset
,
329 .config_aneg
= adin_config_aneg
,
330 .read_status
= adin_read_status
,
331 .config_intr
= adin_config_intr
,
332 .handle_interrupt
= adin_phy_handle_interrupt
,
333 .set_loopback
= adin_set_loopback
,
334 .suspend
= adin_suspend
,
335 .resume
= adin_resume
,
336 .get_sqi
= adin_get_sqi
,
337 .get_sqi_max
= adin_get_sqi_max
,
341 module_phy_driver(adin_driver
);
343 static struct mdio_device_id __maybe_unused adin_tbl
[] = {
344 { PHY_ID_MATCH_MODEL(PHY_ID_ADIN1100
) },
345 { PHY_ID_MATCH_MODEL(PHY_ID_ADIN1110
) },
346 { PHY_ID_MATCH_MODEL(PHY_ID_ADIN2111
) },
350 MODULE_DEVICE_TABLE(mdio
, adin_tbl
);
351 MODULE_DESCRIPTION("Analog Devices Industrial Ethernet T1L PHY driver");
352 MODULE_LICENSE("Dual BSD/GPL");