1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Analog Devices Industrial Ethernet PHYs
5 * Copyright 2019 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/ethtool_netlink.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/mii.h>
15 #include <linux/phy.h>
16 #include <linux/property.h>
18 #define PHY_ID_ADIN1200 0x0283bc20
19 #define PHY_ID_ADIN1300 0x0283bc30
21 #define ADIN1300_MII_EXT_REG_PTR 0x0010
22 #define ADIN1300_MII_EXT_REG_DATA 0x0011
24 #define ADIN1300_PHY_CTRL1 0x0012
25 #define ADIN1300_AUTO_MDI_EN BIT(10)
26 #define ADIN1300_MAN_MDIX_EN BIT(9)
27 #define ADIN1300_DIAG_CLK_EN BIT(2)
29 #define ADIN1300_RX_ERR_CNT 0x0014
31 #define ADIN1300_PHY_CTRL_STATUS2 0x0015
32 #define ADIN1300_NRG_PD_EN BIT(3)
33 #define ADIN1300_NRG_PD_TX_EN BIT(2)
34 #define ADIN1300_NRG_PD_STATUS BIT(1)
36 #define ADIN1300_PHY_CTRL2 0x0016
37 #define ADIN1300_DOWNSPEED_AN_100_EN BIT(11)
38 #define ADIN1300_DOWNSPEED_AN_10_EN BIT(10)
39 #define ADIN1300_GROUP_MDIO_EN BIT(6)
40 #define ADIN1300_DOWNSPEEDS_EN \
41 (ADIN1300_DOWNSPEED_AN_100_EN | ADIN1300_DOWNSPEED_AN_10_EN)
43 #define ADIN1300_PHY_CTRL3 0x0017
44 #define ADIN1300_LINKING_EN BIT(13)
45 #define ADIN1300_DOWNSPEED_RETRIES_MSK GENMASK(12, 10)
47 #define ADIN1300_INT_MASK_REG 0x0018
48 #define ADIN1300_INT_MDIO_SYNC_EN BIT(9)
49 #define ADIN1300_INT_ANEG_STAT_CHNG_EN BIT(8)
50 #define ADIN1300_INT_ANEG_PAGE_RX_EN BIT(6)
51 #define ADIN1300_INT_IDLE_ERR_CNT_EN BIT(5)
52 #define ADIN1300_INT_MAC_FIFO_OU_EN BIT(4)
53 #define ADIN1300_INT_RX_STAT_CHNG_EN BIT(3)
54 #define ADIN1300_INT_LINK_STAT_CHNG_EN BIT(2)
55 #define ADIN1300_INT_SPEED_CHNG_EN BIT(1)
56 #define ADIN1300_INT_HW_IRQ_EN BIT(0)
57 #define ADIN1300_INT_MASK_EN \
58 (ADIN1300_INT_LINK_STAT_CHNG_EN | ADIN1300_INT_HW_IRQ_EN)
59 #define ADIN1300_INT_STATUS_REG 0x0019
61 #define ADIN1300_PHY_STATUS1 0x001a
62 #define ADIN1300_PAIR_01_SWAP BIT(11)
64 /* EEE register addresses, accessible via Clause 22 access using
65 * ADIN1300_MII_EXT_REG_PTR & ADIN1300_MII_EXT_REG_DATA.
66 * The bit-fields are the same as specified by IEEE for EEE.
68 #define ADIN1300_EEE_CAP_REG 0x8000
69 #define ADIN1300_EEE_ADV_REG 0x8001
70 #define ADIN1300_EEE_LPABLE_REG 0x8002
71 #define ADIN1300_CLOCK_STOP_REG 0x9400
72 #define ADIN1300_LPI_WAKE_ERR_CNT_REG 0xa000
74 #define ADIN1300_CDIAG_RUN 0xba1b
75 #define ADIN1300_CDIAG_RUN_EN BIT(0)
78 * The XSIM3/2/1 and XSHRT3/2/1 are actually relative.
79 * For CDIAG_DTLD_RSLTS(0) it's ADIN1300_CDIAG_RSLT_XSIM3/2/1
80 * For CDIAG_DTLD_RSLTS(1) it's ADIN1300_CDIAG_RSLT_XSIM3/2/0
81 * For CDIAG_DTLD_RSLTS(2) it's ADIN1300_CDIAG_RSLT_XSIM3/1/0
82 * For CDIAG_DTLD_RSLTS(3) it's ADIN1300_CDIAG_RSLT_XSIM2/1/0
84 #define ADIN1300_CDIAG_DTLD_RSLTS(x) (0xba1d + (x))
85 #define ADIN1300_CDIAG_RSLT_BUSY BIT(10)
86 #define ADIN1300_CDIAG_RSLT_XSIM3 BIT(9)
87 #define ADIN1300_CDIAG_RSLT_XSIM2 BIT(8)
88 #define ADIN1300_CDIAG_RSLT_XSIM1 BIT(7)
89 #define ADIN1300_CDIAG_RSLT_SIM BIT(6)
90 #define ADIN1300_CDIAG_RSLT_XSHRT3 BIT(5)
91 #define ADIN1300_CDIAG_RSLT_XSHRT2 BIT(4)
92 #define ADIN1300_CDIAG_RSLT_XSHRT1 BIT(3)
93 #define ADIN1300_CDIAG_RSLT_SHRT BIT(2)
94 #define ADIN1300_CDIAG_RSLT_OPEN BIT(1)
95 #define ADIN1300_CDIAG_RSLT_GOOD BIT(0)
97 #define ADIN1300_CDIAG_FLT_DIST(x) (0xba21 + (x))
99 #define ADIN1300_GE_SOFT_RESET_REG 0xff0c
100 #define ADIN1300_GE_SOFT_RESET BIT(0)
102 #define ADIN1300_GE_RGMII_CFG_REG 0xff23
103 #define ADIN1300_GE_RGMII_RX_MSK GENMASK(8, 6)
104 #define ADIN1300_GE_RGMII_RX_SEL(x) \
105 FIELD_PREP(ADIN1300_GE_RGMII_RX_MSK, x)
106 #define ADIN1300_GE_RGMII_GTX_MSK GENMASK(5, 3)
107 #define ADIN1300_GE_RGMII_GTX_SEL(x) \
108 FIELD_PREP(ADIN1300_GE_RGMII_GTX_MSK, x)
109 #define ADIN1300_GE_RGMII_RXID_EN BIT(2)
110 #define ADIN1300_GE_RGMII_TXID_EN BIT(1)
111 #define ADIN1300_GE_RGMII_EN BIT(0)
113 /* RGMII internal delay settings for rx and tx for ADIN1300 */
114 #define ADIN1300_RGMII_1_60_NS 0x0001
115 #define ADIN1300_RGMII_1_80_NS 0x0002
116 #define ADIN1300_RGMII_2_00_NS 0x0000
117 #define ADIN1300_RGMII_2_20_NS 0x0006
118 #define ADIN1300_RGMII_2_40_NS 0x0007
120 #define ADIN1300_GE_RMII_CFG_REG 0xff24
121 #define ADIN1300_GE_RMII_FIFO_DEPTH_MSK GENMASK(6, 4)
122 #define ADIN1300_GE_RMII_FIFO_DEPTH_SEL(x) \
123 FIELD_PREP(ADIN1300_GE_RMII_FIFO_DEPTH_MSK, x)
124 #define ADIN1300_GE_RMII_EN BIT(0)
126 /* RMII fifo depth values */
127 #define ADIN1300_RMII_4_BITS 0x0000
128 #define ADIN1300_RMII_8_BITS 0x0001
129 #define ADIN1300_RMII_12_BITS 0x0002
130 #define ADIN1300_RMII_16_BITS 0x0003
131 #define ADIN1300_RMII_20_BITS 0x0004
132 #define ADIN1300_RMII_24_BITS 0x0005
135 * struct adin_cfg_reg_map - map a config value to aregister value
136 * @cfg: value in device configuration
137 * @reg: value in the register
139 struct adin_cfg_reg_map
{
144 static const struct adin_cfg_reg_map adin_rgmii_delays
[] = {
145 { 1600, ADIN1300_RGMII_1_60_NS
},
146 { 1800, ADIN1300_RGMII_1_80_NS
},
147 { 2000, ADIN1300_RGMII_2_00_NS
},
148 { 2200, ADIN1300_RGMII_2_20_NS
},
149 { 2400, ADIN1300_RGMII_2_40_NS
},
153 static const struct adin_cfg_reg_map adin_rmii_fifo_depths
[] = {
154 { 4, ADIN1300_RMII_4_BITS
},
155 { 8, ADIN1300_RMII_8_BITS
},
156 { 12, ADIN1300_RMII_12_BITS
},
157 { 16, ADIN1300_RMII_16_BITS
},
158 { 20, ADIN1300_RMII_20_BITS
},
159 { 24, ADIN1300_RMII_24_BITS
},
164 * struct adin_clause45_mmd_map - map to convert Clause 45 regs to Clause 22
165 * @devad: device address used in Clause 45 access
166 * @cl45_regnum: register address defined by Clause 45
167 * @adin_regnum: equivalent register address accessible via Clause 22
169 struct adin_clause45_mmd_map
{
175 static const struct adin_clause45_mmd_map adin_clause45_mmd_map
[] = {
176 { MDIO_MMD_PCS
, MDIO_PCS_EEE_ABLE
, ADIN1300_EEE_CAP_REG
},
177 { MDIO_MMD_AN
, MDIO_AN_EEE_LPABLE
, ADIN1300_EEE_LPABLE_REG
},
178 { MDIO_MMD_AN
, MDIO_AN_EEE_ADV
, ADIN1300_EEE_ADV_REG
},
179 { MDIO_MMD_PCS
, MDIO_CTRL1
, ADIN1300_CLOCK_STOP_REG
},
180 { MDIO_MMD_PCS
, MDIO_PCS_EEE_WK_ERR
, ADIN1300_LPI_WAKE_ERR_CNT_REG
},
183 struct adin_hw_stat
{
189 static const struct adin_hw_stat adin_hw_stats
[] = {
190 { "total_frames_checked_count", 0x940A, 0x940B }, /* hi + lo */
191 { "length_error_frames_count", 0x940C },
192 { "alignment_error_frames_count", 0x940D },
193 { "symbol_error_count", 0x940E },
194 { "oversized_frames_count", 0x940F },
195 { "undersized_frames_count", 0x9410 },
196 { "odd_nibble_frames_count", 0x9411 },
197 { "odd_preamble_packet_count", 0x9412 },
198 { "dribble_bits_frames_count", 0x9413 },
199 { "false_carrier_events_count", 0x9414 },
203 * struct adin_priv - ADIN PHY driver private data
204 * @stats: statistic counters for the PHY
207 u64 stats
[ARRAY_SIZE(adin_hw_stats
)];
210 static int adin_lookup_reg_value(const struct adin_cfg_reg_map
*tbl
, int cfg
)
214 for (i
= 0; tbl
[i
].cfg
; i
++) {
215 if (tbl
[i
].cfg
== cfg
)
222 static u32
adin_get_reg_value(struct phy_device
*phydev
,
223 const char *prop_name
,
224 const struct adin_cfg_reg_map
*tbl
,
227 struct device
*dev
= &phydev
->mdio
.dev
;
231 if (device_property_read_u32(dev
, prop_name
, &val
))
234 rc
= adin_lookup_reg_value(tbl
, val
);
237 "Unsupported value %u for %s using default (%u)\n",
238 val
, prop_name
, dflt
);
245 static int adin_config_rgmii_mode(struct phy_device
*phydev
)
250 if (!phy_interface_is_rgmii(phydev
))
251 return phy_clear_bits_mmd(phydev
, MDIO_MMD_VEND1
,
252 ADIN1300_GE_RGMII_CFG_REG
,
253 ADIN1300_GE_RGMII_EN
);
255 reg
= phy_read_mmd(phydev
, MDIO_MMD_VEND1
, ADIN1300_GE_RGMII_CFG_REG
);
259 reg
|= ADIN1300_GE_RGMII_EN
;
261 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
||
262 phydev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
) {
263 reg
|= ADIN1300_GE_RGMII_RXID_EN
;
265 val
= adin_get_reg_value(phydev
, "adi,rx-internal-delay-ps",
267 ADIN1300_RGMII_2_00_NS
);
268 reg
&= ~ADIN1300_GE_RGMII_RX_MSK
;
269 reg
|= ADIN1300_GE_RGMII_RX_SEL(val
);
271 reg
&= ~ADIN1300_GE_RGMII_RXID_EN
;
274 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
||
275 phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
) {
276 reg
|= ADIN1300_GE_RGMII_TXID_EN
;
278 val
= adin_get_reg_value(phydev
, "adi,tx-internal-delay-ps",
280 ADIN1300_RGMII_2_00_NS
);
281 reg
&= ~ADIN1300_GE_RGMII_GTX_MSK
;
282 reg
|= ADIN1300_GE_RGMII_GTX_SEL(val
);
284 reg
&= ~ADIN1300_GE_RGMII_TXID_EN
;
287 return phy_write_mmd(phydev
, MDIO_MMD_VEND1
,
288 ADIN1300_GE_RGMII_CFG_REG
, reg
);
291 static int adin_config_rmii_mode(struct phy_device
*phydev
)
296 if (phydev
->interface
!= PHY_INTERFACE_MODE_RMII
)
297 return phy_clear_bits_mmd(phydev
, MDIO_MMD_VEND1
,
298 ADIN1300_GE_RMII_CFG_REG
,
299 ADIN1300_GE_RMII_EN
);
301 reg
= phy_read_mmd(phydev
, MDIO_MMD_VEND1
, ADIN1300_GE_RMII_CFG_REG
);
305 reg
|= ADIN1300_GE_RMII_EN
;
307 val
= adin_get_reg_value(phydev
, "adi,fifo-depth-bits",
308 adin_rmii_fifo_depths
,
309 ADIN1300_RMII_8_BITS
);
311 reg
&= ~ADIN1300_GE_RMII_FIFO_DEPTH_MSK
;
312 reg
|= ADIN1300_GE_RMII_FIFO_DEPTH_SEL(val
);
314 return phy_write_mmd(phydev
, MDIO_MMD_VEND1
,
315 ADIN1300_GE_RMII_CFG_REG
, reg
);
318 static int adin_get_downshift(struct phy_device
*phydev
, u8
*data
)
320 int val
, cnt
, enable
;
322 val
= phy_read(phydev
, ADIN1300_PHY_CTRL2
);
326 cnt
= phy_read(phydev
, ADIN1300_PHY_CTRL3
);
330 enable
= FIELD_GET(ADIN1300_DOWNSPEEDS_EN
, val
);
331 cnt
= FIELD_GET(ADIN1300_DOWNSPEED_RETRIES_MSK
, cnt
);
333 *data
= (enable
&& cnt
) ? cnt
: DOWNSHIFT_DEV_DISABLE
;
338 static int adin_set_downshift(struct phy_device
*phydev
, u8 cnt
)
343 if (cnt
== DOWNSHIFT_DEV_DISABLE
)
344 return phy_clear_bits(phydev
, ADIN1300_PHY_CTRL2
,
345 ADIN1300_DOWNSPEEDS_EN
);
350 val
= FIELD_PREP(ADIN1300_DOWNSPEED_RETRIES_MSK
, cnt
);
352 rc
= phy_modify(phydev
, ADIN1300_PHY_CTRL3
,
353 ADIN1300_DOWNSPEED_RETRIES_MSK
,
358 return phy_set_bits(phydev
, ADIN1300_PHY_CTRL2
,
359 ADIN1300_DOWNSPEEDS_EN
);
362 static int adin_get_edpd(struct phy_device
*phydev
, u16
*tx_interval
)
366 val
= phy_read(phydev
, ADIN1300_PHY_CTRL_STATUS2
);
370 if (ADIN1300_NRG_PD_EN
& val
) {
371 if (val
& ADIN1300_NRG_PD_TX_EN
)
372 /* default is 1 second */
373 *tx_interval
= ETHTOOL_PHY_EDPD_DFLT_TX_MSECS
;
375 *tx_interval
= ETHTOOL_PHY_EDPD_NO_TX
;
377 *tx_interval
= ETHTOOL_PHY_EDPD_DISABLE
;
383 static int adin_set_edpd(struct phy_device
*phydev
, u16 tx_interval
)
387 if (tx_interval
== ETHTOOL_PHY_EDPD_DISABLE
)
388 return phy_clear_bits(phydev
, ADIN1300_PHY_CTRL_STATUS2
,
389 (ADIN1300_NRG_PD_EN
| ADIN1300_NRG_PD_TX_EN
));
391 val
= ADIN1300_NRG_PD_EN
;
393 switch (tx_interval
) {
394 case 1000: /* 1 second */
396 case ETHTOOL_PHY_EDPD_DFLT_TX_MSECS
:
397 val
|= ADIN1300_NRG_PD_TX_EN
;
399 case ETHTOOL_PHY_EDPD_NO_TX
:
405 return phy_modify(phydev
, ADIN1300_PHY_CTRL_STATUS2
,
406 (ADIN1300_NRG_PD_EN
| ADIN1300_NRG_PD_TX_EN
),
410 static int adin_get_tunable(struct phy_device
*phydev
,
411 struct ethtool_tunable
*tuna
, void *data
)
414 case ETHTOOL_PHY_DOWNSHIFT
:
415 return adin_get_downshift(phydev
, data
);
416 case ETHTOOL_PHY_EDPD
:
417 return adin_get_edpd(phydev
, data
);
423 static int adin_set_tunable(struct phy_device
*phydev
,
424 struct ethtool_tunable
*tuna
, const void *data
)
427 case ETHTOOL_PHY_DOWNSHIFT
:
428 return adin_set_downshift(phydev
, *(const u8
*)data
);
429 case ETHTOOL_PHY_EDPD
:
430 return adin_set_edpd(phydev
, *(const u16
*)data
);
436 static int adin_config_init(struct phy_device
*phydev
)
440 phydev
->mdix_ctrl
= ETH_TP_MDI_AUTO
;
442 rc
= adin_config_rgmii_mode(phydev
);
446 rc
= adin_config_rmii_mode(phydev
);
450 rc
= adin_set_downshift(phydev
, 4);
454 rc
= adin_set_edpd(phydev
, ETHTOOL_PHY_EDPD_DFLT_TX_MSECS
);
458 phydev_dbg(phydev
, "PHY is using mode '%s'\n",
459 phy_modes(phydev
->interface
));
464 static int adin_phy_ack_intr(struct phy_device
*phydev
)
466 /* Clear pending interrupts */
467 int rc
= phy_read(phydev
, ADIN1300_INT_STATUS_REG
);
469 return rc
< 0 ? rc
: 0;
472 static int adin_phy_config_intr(struct phy_device
*phydev
)
476 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
) {
477 err
= adin_phy_ack_intr(phydev
);
481 err
= phy_set_bits(phydev
, ADIN1300_INT_MASK_REG
,
482 ADIN1300_INT_MASK_EN
);
484 err
= phy_clear_bits(phydev
, ADIN1300_INT_MASK_REG
,
485 ADIN1300_INT_MASK_EN
);
489 err
= adin_phy_ack_intr(phydev
);
495 static irqreturn_t
adin_phy_handle_interrupt(struct phy_device
*phydev
)
499 irq_status
= phy_read(phydev
, ADIN1300_INT_STATUS_REG
);
500 if (irq_status
< 0) {
505 if (!(irq_status
& ADIN1300_INT_LINK_STAT_CHNG_EN
))
508 phy_trigger_machine(phydev
);
513 static int adin_cl45_to_adin_reg(struct phy_device
*phydev
, int devad
,
516 const struct adin_clause45_mmd_map
*m
;
519 if (devad
== MDIO_MMD_VEND1
)
522 for (i
= 0; i
< ARRAY_SIZE(adin_clause45_mmd_map
); i
++) {
523 m
= &adin_clause45_mmd_map
[i
];
524 if (m
->devad
== devad
&& m
->cl45_regnum
== cl45_regnum
)
525 return m
->adin_regnum
;
529 "No translation available for devad: %d reg: %04x\n",
535 static int adin_read_mmd(struct phy_device
*phydev
, int devad
, u16 regnum
)
537 struct mii_bus
*bus
= phydev
->mdio
.bus
;
538 int phy_addr
= phydev
->mdio
.addr
;
542 adin_regnum
= adin_cl45_to_adin_reg(phydev
, devad
, regnum
);
546 err
= __mdiobus_write(bus
, phy_addr
, ADIN1300_MII_EXT_REG_PTR
,
551 return __mdiobus_read(bus
, phy_addr
, ADIN1300_MII_EXT_REG_DATA
);
554 static int adin_write_mmd(struct phy_device
*phydev
, int devad
, u16 regnum
,
557 struct mii_bus
*bus
= phydev
->mdio
.bus
;
558 int phy_addr
= phydev
->mdio
.addr
;
562 adin_regnum
= adin_cl45_to_adin_reg(phydev
, devad
, regnum
);
566 err
= __mdiobus_write(bus
, phy_addr
, ADIN1300_MII_EXT_REG_PTR
,
571 return __mdiobus_write(bus
, phy_addr
, ADIN1300_MII_EXT_REG_DATA
, val
);
574 static int adin_config_mdix(struct phy_device
*phydev
)
576 bool auto_en
, mdix_en
;
581 switch (phydev
->mdix_ctrl
) {
587 case ETH_TP_MDI_AUTO
:
594 reg
= phy_read(phydev
, ADIN1300_PHY_CTRL1
);
599 reg
|= ADIN1300_MAN_MDIX_EN
;
601 reg
&= ~ADIN1300_MAN_MDIX_EN
;
604 reg
|= ADIN1300_AUTO_MDI_EN
;
606 reg
&= ~ADIN1300_AUTO_MDI_EN
;
608 return phy_write(phydev
, ADIN1300_PHY_CTRL1
, reg
);
611 static int adin_config_aneg(struct phy_device
*phydev
)
615 ret
= phy_clear_bits(phydev
, ADIN1300_PHY_CTRL1
, ADIN1300_DIAG_CLK_EN
);
619 ret
= phy_set_bits(phydev
, ADIN1300_PHY_CTRL3
, ADIN1300_LINKING_EN
);
623 ret
= adin_config_mdix(phydev
);
627 return genphy_config_aneg(phydev
);
630 static int adin_mdix_update(struct phy_device
*phydev
)
632 bool auto_en
, mdix_en
;
636 reg
= phy_read(phydev
, ADIN1300_PHY_CTRL1
);
640 auto_en
= !!(reg
& ADIN1300_AUTO_MDI_EN
);
641 mdix_en
= !!(reg
& ADIN1300_MAN_MDIX_EN
);
643 /* If MDI/MDIX is forced, just read it from the control reg */
646 phydev
->mdix
= ETH_TP_MDI_X
;
648 phydev
->mdix
= ETH_TP_MDI
;
653 * Otherwise, we need to deduce it from the PHY status2 reg.
654 * When Auto-MDI is enabled, the ADIN1300_MAN_MDIX_EN bit implies
655 * a preference for MDIX when it is set.
657 reg
= phy_read(phydev
, ADIN1300_PHY_STATUS1
);
661 swapped
= !!(reg
& ADIN1300_PAIR_01_SWAP
);
663 if (mdix_en
!= swapped
)
664 phydev
->mdix
= ETH_TP_MDI_X
;
666 phydev
->mdix
= ETH_TP_MDI
;
671 static int adin_read_status(struct phy_device
*phydev
)
675 ret
= adin_mdix_update(phydev
);
679 return genphy_read_status(phydev
);
682 static int adin_soft_reset(struct phy_device
*phydev
)
686 /* The reset bit is self-clearing, set it and wait */
687 rc
= phy_set_bits_mmd(phydev
, MDIO_MMD_VEND1
,
688 ADIN1300_GE_SOFT_RESET_REG
,
689 ADIN1300_GE_SOFT_RESET
);
695 /* If we get a read error something may be wrong */
696 rc
= phy_read_mmd(phydev
, MDIO_MMD_VEND1
,
697 ADIN1300_GE_SOFT_RESET_REG
);
699 return rc
< 0 ? rc
: 0;
702 static int adin_get_sset_count(struct phy_device
*phydev
)
704 return ARRAY_SIZE(adin_hw_stats
);
707 static void adin_get_strings(struct phy_device
*phydev
, u8
*data
)
711 for (i
= 0; i
< ARRAY_SIZE(adin_hw_stats
); i
++) {
712 strlcpy(&data
[i
* ETH_GSTRING_LEN
],
713 adin_hw_stats
[i
].string
, ETH_GSTRING_LEN
);
717 static int adin_read_mmd_stat_regs(struct phy_device
*phydev
,
718 const struct adin_hw_stat
*stat
,
723 ret
= phy_read_mmd(phydev
, MDIO_MMD_VEND1
, stat
->reg1
);
727 *val
= (ret
& 0xffff);
732 ret
= phy_read_mmd(phydev
, MDIO_MMD_VEND1
, stat
->reg2
);
737 *val
|= (ret
& 0xffff);
742 static u64
adin_get_stat(struct phy_device
*phydev
, int i
)
744 const struct adin_hw_stat
*stat
= &adin_hw_stats
[i
];
745 struct adin_priv
*priv
= phydev
->priv
;
749 if (stat
->reg1
> 0x1f) {
750 ret
= adin_read_mmd_stat_regs(phydev
, stat
, &val
);
754 ret
= phy_read(phydev
, stat
->reg1
);
757 val
= (ret
& 0xffff);
760 priv
->stats
[i
] += val
;
762 return priv
->stats
[i
];
765 static void adin_get_stats(struct phy_device
*phydev
,
766 struct ethtool_stats
*stats
, u64
*data
)
770 /* latch copies of all the frame-checker counters */
771 rc
= phy_read(phydev
, ADIN1300_RX_ERR_CNT
);
775 for (i
= 0; i
< ARRAY_SIZE(adin_hw_stats
); i
++)
776 data
[i
] = adin_get_stat(phydev
, i
);
779 static int adin_probe(struct phy_device
*phydev
)
781 struct device
*dev
= &phydev
->mdio
.dev
;
782 struct adin_priv
*priv
;
784 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
793 static int adin_cable_test_start(struct phy_device
*phydev
)
797 ret
= phy_clear_bits(phydev
, ADIN1300_PHY_CTRL3
, ADIN1300_LINKING_EN
);
801 ret
= phy_clear_bits(phydev
, ADIN1300_PHY_CTRL1
, ADIN1300_DIAG_CLK_EN
);
805 /* wait a bit for the clock to stabilize */
808 return phy_set_bits_mmd(phydev
, MDIO_MMD_VEND1
, ADIN1300_CDIAG_RUN
,
809 ADIN1300_CDIAG_RUN_EN
);
812 static int adin_cable_test_report_trans(int result
)
816 if (result
& ADIN1300_CDIAG_RSLT_GOOD
)
817 return ETHTOOL_A_CABLE_RESULT_CODE_OK
;
818 if (result
& ADIN1300_CDIAG_RSLT_OPEN
)
819 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN
;
821 /* short with other pairs */
822 mask
= ADIN1300_CDIAG_RSLT_XSHRT3
|
823 ADIN1300_CDIAG_RSLT_XSHRT2
|
824 ADIN1300_CDIAG_RSLT_XSHRT1
;
826 return ETHTOOL_A_CABLE_RESULT_CODE_CROSS_SHORT
;
828 if (result
& ADIN1300_CDIAG_RSLT_SHRT
)
829 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT
;
831 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC
;
834 static int adin_cable_test_report_pair(struct phy_device
*phydev
,
840 ret
= phy_read_mmd(phydev
, MDIO_MMD_VEND1
,
841 ADIN1300_CDIAG_DTLD_RSLTS(pair
));
845 fault_rslt
= adin_cable_test_report_trans(ret
);
847 ret
= ethnl_cable_test_result(phydev
, pair
, fault_rslt
);
851 ret
= phy_read_mmd(phydev
, MDIO_MMD_VEND1
,
852 ADIN1300_CDIAG_FLT_DIST(pair
));
856 switch (fault_rslt
) {
857 case ETHTOOL_A_CABLE_RESULT_CODE_OPEN
:
858 case ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT
:
859 case ETHTOOL_A_CABLE_RESULT_CODE_CROSS_SHORT
:
860 return ethnl_cable_test_fault_length(phydev
, pair
, ret
* 100);
866 static int adin_cable_test_report(struct phy_device
*phydev
)
871 for (pair
= ETHTOOL_A_CABLE_PAIR_A
; pair
<= ETHTOOL_A_CABLE_PAIR_D
; pair
++) {
872 ret
= adin_cable_test_report_pair(phydev
, pair
);
880 static int adin_cable_test_get_status(struct phy_device
*phydev
,
887 ret
= phy_read_mmd(phydev
, MDIO_MMD_VEND1
, ADIN1300_CDIAG_RUN
);
891 if (ret
& ADIN1300_CDIAG_RUN_EN
)
896 return adin_cable_test_report(phydev
);
899 static struct phy_driver adin_driver
[] = {
901 PHY_ID_MATCH_MODEL(PHY_ID_ADIN1200
),
903 .flags
= PHY_POLL_CABLE_TEST
,
905 .config_init
= adin_config_init
,
906 .soft_reset
= adin_soft_reset
,
907 .config_aneg
= adin_config_aneg
,
908 .read_status
= adin_read_status
,
909 .get_tunable
= adin_get_tunable
,
910 .set_tunable
= adin_set_tunable
,
911 .config_intr
= adin_phy_config_intr
,
912 .handle_interrupt
= adin_phy_handle_interrupt
,
913 .get_sset_count
= adin_get_sset_count
,
914 .get_strings
= adin_get_strings
,
915 .get_stats
= adin_get_stats
,
916 .resume
= genphy_resume
,
917 .suspend
= genphy_suspend
,
918 .read_mmd
= adin_read_mmd
,
919 .write_mmd
= adin_write_mmd
,
920 .cable_test_start
= adin_cable_test_start
,
921 .cable_test_get_status
= adin_cable_test_get_status
,
924 PHY_ID_MATCH_MODEL(PHY_ID_ADIN1300
),
926 .flags
= PHY_POLL_CABLE_TEST
,
928 .config_init
= adin_config_init
,
929 .soft_reset
= adin_soft_reset
,
930 .config_aneg
= adin_config_aneg
,
931 .read_status
= adin_read_status
,
932 .get_tunable
= adin_get_tunable
,
933 .set_tunable
= adin_set_tunable
,
934 .config_intr
= adin_phy_config_intr
,
935 .handle_interrupt
= adin_phy_handle_interrupt
,
936 .get_sset_count
= adin_get_sset_count
,
937 .get_strings
= adin_get_strings
,
938 .get_stats
= adin_get_stats
,
939 .resume
= genphy_resume
,
940 .suspend
= genphy_suspend
,
941 .read_mmd
= adin_read_mmd
,
942 .write_mmd
= adin_write_mmd
,
943 .cable_test_start
= adin_cable_test_start
,
944 .cable_test_get_status
= adin_cable_test_get_status
,
948 module_phy_driver(adin_driver
);
950 static struct mdio_device_id __maybe_unused adin_tbl
[] = {
951 { PHY_ID_MATCH_MODEL(PHY_ID_ADIN1200
) },
952 { PHY_ID_MATCH_MODEL(PHY_ID_ADIN1300
) },
956 MODULE_DEVICE_TABLE(mdio
, adin_tbl
);
957 MODULE_DESCRIPTION("Analog Devices Industrial Ethernet PHY driver");
958 MODULE_LICENSE("GPL");