1 // SPDX-License-Identifier: GPL-2.0
2 /* Driver for the Texas Instruments DP83867 PHY
4 * Copyright (C) 2015 Texas Instruments Inc.
7 #include <linux/ethtool.h>
8 #include <linux/kernel.h>
10 #include <linux/module.h>
12 #include <linux/phy.h>
13 #include <linux/delay.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/bitfield.h>
17 #include <linux/nvmem-consumer.h>
19 #include <dt-bindings/net/ti-dp83867.h>
21 #define DP83867_PHY_ID 0x2000a231
22 #define DP83867_DEVADDR 0x1f
24 #define MII_DP83867_PHYCTRL 0x10
25 #define MII_DP83867_PHYSTS 0x11
26 #define MII_DP83867_MICR 0x12
27 #define MII_DP83867_ISR 0x13
28 #define DP83867_CFG2 0x14
29 #define DP83867_LEDCR1 0x18
30 #define DP83867_LEDCR2 0x19
31 #define DP83867_CFG3 0x1e
32 #define DP83867_CTRL 0x1f
34 /* Extended Registers */
35 #define DP83867_FLD_THR_CFG 0x002e
36 #define DP83867_CFG4 0x0031
37 #define DP83867_CFG4_SGMII_ANEG_MASK (BIT(5) | BIT(6))
38 #define DP83867_CFG4_SGMII_ANEG_TIMER_11MS (3 << 5)
39 #define DP83867_CFG4_SGMII_ANEG_TIMER_800US (2 << 5)
40 #define DP83867_CFG4_SGMII_ANEG_TIMER_2US (1 << 5)
41 #define DP83867_CFG4_SGMII_ANEG_TIMER_16MS (0 << 5)
43 #define DP83867_RGMIICTL 0x0032
44 #define DP83867_STRAP_STS1 0x006E
45 #define DP83867_STRAP_STS2 0x006f
46 #define DP83867_RGMIIDCTL 0x0086
47 #define DP83867_DSP_FFE_CFG 0x012c
48 #define DP83867_RXFCFG 0x0134
49 #define DP83867_RXFPMD1 0x0136
50 #define DP83867_RXFPMD2 0x0137
51 #define DP83867_RXFPMD3 0x0138
52 #define DP83867_RXFSOP1 0x0139
53 #define DP83867_RXFSOP2 0x013A
54 #define DP83867_RXFSOP3 0x013B
55 #define DP83867_IO_MUX_CFG 0x0170
56 #define DP83867_SGMIICTL 0x00D3
57 #define DP83867_10M_SGMII_CFG 0x016F
58 #define DP83867_10M_SGMII_RATE_ADAPT_MASK BIT(7)
60 #define DP83867_SW_RESET BIT(15)
61 #define DP83867_SW_RESTART BIT(14)
63 /* MICR Interrupt bits */
64 #define MII_DP83867_MICR_AN_ERR_INT_EN BIT(15)
65 #define MII_DP83867_MICR_SPEED_CHNG_INT_EN BIT(14)
66 #define MII_DP83867_MICR_DUP_MODE_CHNG_INT_EN BIT(13)
67 #define MII_DP83867_MICR_PAGE_RXD_INT_EN BIT(12)
68 #define MII_DP83867_MICR_AUTONEG_COMP_INT_EN BIT(11)
69 #define MII_DP83867_MICR_LINK_STS_CHNG_INT_EN BIT(10)
70 #define MII_DP83867_MICR_FALSE_CARRIER_INT_EN BIT(8)
71 #define MII_DP83867_MICR_SLEEP_MODE_CHNG_INT_EN BIT(4)
72 #define MII_DP83867_MICR_WOL_INT_EN BIT(3)
73 #define MII_DP83867_MICR_XGMII_ERR_INT_EN BIT(2)
74 #define MII_DP83867_MICR_POL_CHNG_INT_EN BIT(1)
75 #define MII_DP83867_MICR_JABBER_INT_EN BIT(0)
78 #define DP83867_RGMII_TX_CLK_DELAY_EN BIT(1)
79 #define DP83867_RGMII_RX_CLK_DELAY_EN BIT(0)
82 #define DP83867_SGMII_TYPE BIT(14)
85 #define DP83867_WOL_MAGIC_EN BIT(0)
86 #define DP83867_WOL_BCAST_EN BIT(2)
87 #define DP83867_WOL_UCAST_EN BIT(4)
88 #define DP83867_WOL_SEC_EN BIT(5)
89 #define DP83867_WOL_ENH_MAC BIT(7)
92 #define DP83867_STRAP_STS1_RESERVED BIT(11)
95 #define DP83867_STRAP_STS2_CLK_SKEW_TX_MASK GENMASK(6, 4)
96 #define DP83867_STRAP_STS2_CLK_SKEW_TX_SHIFT 4
97 #define DP83867_STRAP_STS2_CLK_SKEW_RX_MASK GENMASK(2, 0)
98 #define DP83867_STRAP_STS2_CLK_SKEW_RX_SHIFT 0
99 #define DP83867_STRAP_STS2_CLK_SKEW_NONE BIT(2)
100 #define DP83867_STRAP_STS2_STRAP_FLD BIT(10)
103 #define DP83867_PHYCR_TX_FIFO_DEPTH_SHIFT 14
104 #define DP83867_PHYCR_RX_FIFO_DEPTH_SHIFT 12
105 #define DP83867_PHYCR_FIFO_DEPTH_MAX 0x03
106 #define DP83867_PHYCR_TX_FIFO_DEPTH_MASK GENMASK(15, 14)
107 #define DP83867_PHYCR_RX_FIFO_DEPTH_MASK GENMASK(13, 12)
108 #define DP83867_PHYCR_RESERVED_MASK BIT(11)
109 #define DP83867_PHYCR_FORCE_LINK_GOOD BIT(10)
112 #define DP83867_RGMII_TX_CLK_DELAY_MAX 0xf
113 #define DP83867_RGMII_TX_CLK_DELAY_SHIFT 4
114 #define DP83867_RGMII_TX_CLK_DELAY_INV (DP83867_RGMII_TX_CLK_DELAY_MAX + 1)
115 #define DP83867_RGMII_RX_CLK_DELAY_MAX 0xf
116 #define DP83867_RGMII_RX_CLK_DELAY_SHIFT 0
117 #define DP83867_RGMII_RX_CLK_DELAY_INV (DP83867_RGMII_RX_CLK_DELAY_MAX + 1)
119 /* IO_MUX_CFG bits */
120 #define DP83867_IO_MUX_CFG_IO_IMPEDANCE_MASK 0x1f
121 #define DP83867_IO_MUX_CFG_IO_IMPEDANCE_MAX 0x0
122 #define DP83867_IO_MUX_CFG_IO_IMPEDANCE_MIN 0x1f
123 #define DP83867_IO_MUX_CFG_CLK_O_DISABLE BIT(6)
124 #define DP83867_IO_MUX_CFG_CLK_O_SEL_MASK (0x1f << 8)
125 #define DP83867_IO_MUX_CFG_CLK_O_SEL_SHIFT 8
128 #define DP83867_PHYSTS_1000 BIT(15)
129 #define DP83867_PHYSTS_100 BIT(14)
130 #define DP83867_PHYSTS_DUPLEX BIT(13)
131 #define DP83867_PHYSTS_LINK BIT(10)
134 #define DP83867_DOWNSHIFT_EN (BIT(8) | BIT(9))
135 #define DP83867_DOWNSHIFT_ATTEMPT_MASK (BIT(10) | BIT(11))
136 #define DP83867_DOWNSHIFT_1_COUNT_VAL 0
137 #define DP83867_DOWNSHIFT_2_COUNT_VAL 1
138 #define DP83867_DOWNSHIFT_4_COUNT_VAL 2
139 #define DP83867_DOWNSHIFT_8_COUNT_VAL 3
140 #define DP83867_DOWNSHIFT_1_COUNT 1
141 #define DP83867_DOWNSHIFT_2_COUNT 2
142 #define DP83867_DOWNSHIFT_4_COUNT 4
143 #define DP83867_DOWNSHIFT_8_COUNT 8
144 #define DP83867_SGMII_AUTONEG_EN BIT(7)
147 #define DP83867_CFG3_INT_OE BIT(7)
148 #define DP83867_CFG3_ROBUST_AUTO_MDIX BIT(9)
151 #define DP83867_CFG4_PORT_MIRROR_EN BIT(0)
154 #define DP83867_FLD_THR_CFG_ENERGY_LOST_THR_MASK 0x7
156 #define DP83867_LED_COUNT 4
159 #define DP83867_LED_DRV_EN(x) BIT((x) * 4)
160 #define DP83867_LED_DRV_VAL(x) BIT((x) * 4 + 1)
161 #define DP83867_LED_POLARITY(x) BIT((x) * 4 + 2)
163 #define DP83867_LED_FN(idx, val) (((val) & 0xf) << ((idx) * 4))
164 #define DP83867_LED_FN_MASK(idx) (0xf << ((idx) * 4))
165 #define DP83867_LED_FN_RX_ERR 0xe /* Receive Error */
166 #define DP83867_LED_FN_RX_TX_ERR 0xd /* Receive Error or Transmit Error */
167 #define DP83867_LED_FN_LINK_RX_TX 0xb /* Link established, blink for rx or tx activity */
168 #define DP83867_LED_FN_FULL_DUPLEX 0xa /* Full duplex */
169 #define DP83867_LED_FN_LINK_100_1000_BT 0x9 /* 100/1000BT link established */
170 #define DP83867_LED_FN_LINK_10_100_BT 0x8 /* 10/100BT link established */
171 #define DP83867_LED_FN_LINK_10_BT 0x7 /* 10BT link established */
172 #define DP83867_LED_FN_LINK_100_BTX 0x6 /* 100 BTX link established */
173 #define DP83867_LED_FN_LINK_1000_BT 0x5 /* 1000 BT link established */
174 #define DP83867_LED_FN_COLLISION 0x4 /* Collision detected */
175 #define DP83867_LED_FN_RX 0x3 /* Receive activity */
176 #define DP83867_LED_FN_TX 0x2 /* Transmit activity */
177 #define DP83867_LED_FN_RX_TX 0x1 /* Receive or Transmit activity */
178 #define DP83867_LED_FN_LINK 0x0 /* Link established */
181 DP83867_PORT_MIRROING_KEEP
,
182 DP83867_PORT_MIRROING_EN
,
183 DP83867_PORT_MIRROING_DIS
,
186 struct dp83867_private
{
193 bool rxctrl_strap_quirk
;
196 bool sgmii_ref_clk_en
;
199 static int dp83867_ack_interrupt(struct phy_device
*phydev
)
201 int err
= phy_read(phydev
, MII_DP83867_ISR
);
209 static int dp83867_set_wol(struct phy_device
*phydev
,
210 struct ethtool_wolinfo
*wol
)
212 struct net_device
*ndev
= phydev
->attached_dev
;
213 u16 val_rxcfg
, val_micr
;
216 val_rxcfg
= phy_read_mmd(phydev
, DP83867_DEVADDR
, DP83867_RXFCFG
);
217 val_micr
= phy_read(phydev
, MII_DP83867_MICR
);
219 if (wol
->wolopts
& (WAKE_MAGIC
| WAKE_MAGICSECURE
| WAKE_UCAST
|
221 val_rxcfg
|= DP83867_WOL_ENH_MAC
;
222 val_micr
|= MII_DP83867_MICR_WOL_INT_EN
;
224 if (wol
->wolopts
& WAKE_MAGIC
) {
225 mac
= (const u8
*)ndev
->dev_addr
;
227 if (!is_valid_ether_addr(mac
))
230 phy_write_mmd(phydev
, DP83867_DEVADDR
, DP83867_RXFPMD1
,
231 (mac
[1] << 8 | mac
[0]));
232 phy_write_mmd(phydev
, DP83867_DEVADDR
, DP83867_RXFPMD2
,
233 (mac
[3] << 8 | mac
[2]));
234 phy_write_mmd(phydev
, DP83867_DEVADDR
, DP83867_RXFPMD3
,
235 (mac
[5] << 8 | mac
[4]));
237 val_rxcfg
|= DP83867_WOL_MAGIC_EN
;
239 val_rxcfg
&= ~DP83867_WOL_MAGIC_EN
;
242 if (wol
->wolopts
& WAKE_MAGICSECURE
) {
243 phy_write_mmd(phydev
, DP83867_DEVADDR
, DP83867_RXFSOP1
,
244 (wol
->sopass
[1] << 8) | wol
->sopass
[0]);
245 phy_write_mmd(phydev
, DP83867_DEVADDR
, DP83867_RXFSOP2
,
246 (wol
->sopass
[3] << 8) | wol
->sopass
[2]);
247 phy_write_mmd(phydev
, DP83867_DEVADDR
, DP83867_RXFSOP3
,
248 (wol
->sopass
[5] << 8) | wol
->sopass
[4]);
250 val_rxcfg
|= DP83867_WOL_SEC_EN
;
252 val_rxcfg
&= ~DP83867_WOL_SEC_EN
;
255 if (wol
->wolopts
& WAKE_UCAST
)
256 val_rxcfg
|= DP83867_WOL_UCAST_EN
;
258 val_rxcfg
&= ~DP83867_WOL_UCAST_EN
;
260 if (wol
->wolopts
& WAKE_BCAST
)
261 val_rxcfg
|= DP83867_WOL_BCAST_EN
;
263 val_rxcfg
&= ~DP83867_WOL_BCAST_EN
;
265 val_rxcfg
&= ~DP83867_WOL_ENH_MAC
;
266 val_micr
&= ~MII_DP83867_MICR_WOL_INT_EN
;
269 phy_write_mmd(phydev
, DP83867_DEVADDR
, DP83867_RXFCFG
, val_rxcfg
);
270 phy_write(phydev
, MII_DP83867_MICR
, val_micr
);
275 static void dp83867_get_wol(struct phy_device
*phydev
,
276 struct ethtool_wolinfo
*wol
)
278 u16 value
, sopass_val
;
280 wol
->supported
= (WAKE_UCAST
| WAKE_BCAST
| WAKE_MAGIC
|
284 value
= phy_read_mmd(phydev
, DP83867_DEVADDR
, DP83867_RXFCFG
);
286 if (value
& DP83867_WOL_UCAST_EN
)
287 wol
->wolopts
|= WAKE_UCAST
;
289 if (value
& DP83867_WOL_BCAST_EN
)
290 wol
->wolopts
|= WAKE_BCAST
;
292 if (value
& DP83867_WOL_MAGIC_EN
)
293 wol
->wolopts
|= WAKE_MAGIC
;
295 if (value
& DP83867_WOL_SEC_EN
) {
296 sopass_val
= phy_read_mmd(phydev
, DP83867_DEVADDR
,
298 wol
->sopass
[0] = (sopass_val
& 0xff);
299 wol
->sopass
[1] = (sopass_val
>> 8);
301 sopass_val
= phy_read_mmd(phydev
, DP83867_DEVADDR
,
303 wol
->sopass
[2] = (sopass_val
& 0xff);
304 wol
->sopass
[3] = (sopass_val
>> 8);
306 sopass_val
= phy_read_mmd(phydev
, DP83867_DEVADDR
,
308 wol
->sopass
[4] = (sopass_val
& 0xff);
309 wol
->sopass
[5] = (sopass_val
>> 8);
311 wol
->wolopts
|= WAKE_MAGICSECURE
;
314 if (!(value
& DP83867_WOL_ENH_MAC
))
318 static int dp83867_config_intr(struct phy_device
*phydev
)
320 int micr_status
, err
;
322 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
) {
323 err
= dp83867_ack_interrupt(phydev
);
327 micr_status
= phy_read(phydev
, MII_DP83867_MICR
);
332 (MII_DP83867_MICR_AN_ERR_INT_EN
|
333 MII_DP83867_MICR_SPEED_CHNG_INT_EN
|
334 MII_DP83867_MICR_AUTONEG_COMP_INT_EN
|
335 MII_DP83867_MICR_LINK_STS_CHNG_INT_EN
|
336 MII_DP83867_MICR_DUP_MODE_CHNG_INT_EN
|
337 MII_DP83867_MICR_SLEEP_MODE_CHNG_INT_EN
);
339 err
= phy_write(phydev
, MII_DP83867_MICR
, micr_status
);
342 err
= phy_write(phydev
, MII_DP83867_MICR
, micr_status
);
346 err
= dp83867_ack_interrupt(phydev
);
352 static irqreturn_t
dp83867_handle_interrupt(struct phy_device
*phydev
)
354 int irq_status
, irq_enabled
;
356 irq_status
= phy_read(phydev
, MII_DP83867_ISR
);
357 if (irq_status
< 0) {
362 irq_enabled
= phy_read(phydev
, MII_DP83867_MICR
);
363 if (irq_enabled
< 0) {
368 if (!(irq_status
& irq_enabled
))
371 phy_trigger_machine(phydev
);
376 static int dp83867_read_status(struct phy_device
*phydev
)
378 int status
= phy_read(phydev
, MII_DP83867_PHYSTS
);
381 ret
= genphy_read_status(phydev
);
388 if (status
& DP83867_PHYSTS_DUPLEX
)
389 phydev
->duplex
= DUPLEX_FULL
;
391 phydev
->duplex
= DUPLEX_HALF
;
393 if (status
& DP83867_PHYSTS_1000
)
394 phydev
->speed
= SPEED_1000
;
395 else if (status
& DP83867_PHYSTS_100
)
396 phydev
->speed
= SPEED_100
;
398 phydev
->speed
= SPEED_10
;
403 static int dp83867_get_downshift(struct phy_device
*phydev
, u8
*data
)
405 int val
, cnt
, enable
, count
;
407 val
= phy_read(phydev
, DP83867_CFG2
);
411 enable
= FIELD_GET(DP83867_DOWNSHIFT_EN
, val
);
412 cnt
= FIELD_GET(DP83867_DOWNSHIFT_ATTEMPT_MASK
, val
);
415 case DP83867_DOWNSHIFT_1_COUNT_VAL
:
416 count
= DP83867_DOWNSHIFT_1_COUNT
;
418 case DP83867_DOWNSHIFT_2_COUNT_VAL
:
419 count
= DP83867_DOWNSHIFT_2_COUNT
;
421 case DP83867_DOWNSHIFT_4_COUNT_VAL
:
422 count
= DP83867_DOWNSHIFT_4_COUNT
;
424 case DP83867_DOWNSHIFT_8_COUNT_VAL
:
425 count
= DP83867_DOWNSHIFT_8_COUNT
;
431 *data
= enable
? count
: DOWNSHIFT_DEV_DISABLE
;
436 static int dp83867_set_downshift(struct phy_device
*phydev
, u8 cnt
)
440 if (cnt
> DP83867_DOWNSHIFT_8_COUNT
)
444 return phy_clear_bits(phydev
, DP83867_CFG2
,
445 DP83867_DOWNSHIFT_EN
);
448 case DP83867_DOWNSHIFT_1_COUNT
:
449 count
= DP83867_DOWNSHIFT_1_COUNT_VAL
;
451 case DP83867_DOWNSHIFT_2_COUNT
:
452 count
= DP83867_DOWNSHIFT_2_COUNT_VAL
;
454 case DP83867_DOWNSHIFT_4_COUNT
:
455 count
= DP83867_DOWNSHIFT_4_COUNT_VAL
;
457 case DP83867_DOWNSHIFT_8_COUNT
:
458 count
= DP83867_DOWNSHIFT_8_COUNT_VAL
;
462 "Downshift count must be 1, 2, 4 or 8\n");
466 val
= DP83867_DOWNSHIFT_EN
;
467 val
|= FIELD_PREP(DP83867_DOWNSHIFT_ATTEMPT_MASK
, count
);
469 return phy_modify(phydev
, DP83867_CFG2
,
470 DP83867_DOWNSHIFT_EN
| DP83867_DOWNSHIFT_ATTEMPT_MASK
,
474 static int dp83867_get_tunable(struct phy_device
*phydev
,
475 struct ethtool_tunable
*tuna
, void *data
)
478 case ETHTOOL_PHY_DOWNSHIFT
:
479 return dp83867_get_downshift(phydev
, data
);
485 static int dp83867_set_tunable(struct phy_device
*phydev
,
486 struct ethtool_tunable
*tuna
, const void *data
)
489 case ETHTOOL_PHY_DOWNSHIFT
:
490 return dp83867_set_downshift(phydev
, *(const u8
*)data
);
496 static int dp83867_config_port_mirroring(struct phy_device
*phydev
)
498 struct dp83867_private
*dp83867
= phydev
->priv
;
500 if (dp83867
->port_mirroring
== DP83867_PORT_MIRROING_EN
)
501 phy_set_bits_mmd(phydev
, DP83867_DEVADDR
, DP83867_CFG4
,
502 DP83867_CFG4_PORT_MIRROR_EN
);
504 phy_clear_bits_mmd(phydev
, DP83867_DEVADDR
, DP83867_CFG4
,
505 DP83867_CFG4_PORT_MIRROR_EN
);
509 static int dp83867_verify_rgmii_cfg(struct phy_device
*phydev
)
511 struct dp83867_private
*dp83867
= phydev
->priv
;
513 /* Existing behavior was to use default pin strapping delay in rgmii
514 * mode, but rgmii should have meant no delay. Warn existing users.
516 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII
) {
517 const u16 val
= phy_read_mmd(phydev
, DP83867_DEVADDR
,
519 const u16 txskew
= (val
& DP83867_STRAP_STS2_CLK_SKEW_TX_MASK
) >>
520 DP83867_STRAP_STS2_CLK_SKEW_TX_SHIFT
;
521 const u16 rxskew
= (val
& DP83867_STRAP_STS2_CLK_SKEW_RX_MASK
) >>
522 DP83867_STRAP_STS2_CLK_SKEW_RX_SHIFT
;
524 if (txskew
!= DP83867_STRAP_STS2_CLK_SKEW_NONE
||
525 rxskew
!= DP83867_STRAP_STS2_CLK_SKEW_NONE
)
527 "PHY has delays via pin strapping, but phy-mode = 'rgmii'\n"
528 "Should be 'rgmii-id' to use internal delays txskew:%x rxskew:%x\n",
532 /* RX delay *must* be specified if internal delay of RX is used. */
533 if ((phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
||
534 phydev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
) &&
535 dp83867
->rx_id_delay
== DP83867_RGMII_RX_CLK_DELAY_INV
) {
536 phydev_err(phydev
, "ti,rx-internal-delay must be specified\n");
540 /* TX delay *must* be specified if internal delay of TX is used. */
541 if ((phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
||
542 phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
) &&
543 dp83867
->tx_id_delay
== DP83867_RGMII_TX_CLK_DELAY_INV
) {
544 phydev_err(phydev
, "ti,tx-internal-delay must be specified\n");
551 #if IS_ENABLED(CONFIG_OF_MDIO)
552 static int dp83867_of_init_io_impedance(struct phy_device
*phydev
)
554 struct dp83867_private
*dp83867
= phydev
->priv
;
555 struct device
*dev
= &phydev
->mdio
.dev
;
556 struct device_node
*of_node
= dev
->of_node
;
557 struct nvmem_cell
*cell
;
561 cell
= of_nvmem_cell_get(of_node
, "io_impedance_ctrl");
564 if (ret
!= -ENOENT
&& ret
!= -EOPNOTSUPP
)
565 return phydev_err_probe(phydev
, ret
,
566 "failed to get nvmem cell io_impedance_ctrl\n");
568 /* If no nvmem cell, check for the boolean properties. */
569 if (of_property_read_bool(of_node
, "ti,max-output-impedance"))
570 dp83867
->io_impedance
= DP83867_IO_MUX_CFG_IO_IMPEDANCE_MAX
;
571 else if (of_property_read_bool(of_node
, "ti,min-output-impedance"))
572 dp83867
->io_impedance
= DP83867_IO_MUX_CFG_IO_IMPEDANCE_MIN
;
574 dp83867
->io_impedance
= -1; /* leave at default */
579 buf
= nvmem_cell_read(cell
, NULL
);
580 nvmem_cell_put(cell
);
588 if ((val
& DP83867_IO_MUX_CFG_IO_IMPEDANCE_MASK
) != val
) {
589 phydev_err(phydev
, "nvmem cell 'io_impedance_ctrl' contents out of range\n");
592 dp83867
->io_impedance
= val
;
597 static int dp83867_of_init(struct phy_device
*phydev
)
599 struct dp83867_private
*dp83867
= phydev
->priv
;
600 struct device
*dev
= &phydev
->mdio
.dev
;
601 struct device_node
*of_node
= dev
->of_node
;
607 /* Optional configuration */
608 ret
= of_property_read_u32(of_node
, "ti,clk-output-sel",
609 &dp83867
->clk_output_sel
);
610 /* If not set, keep default */
612 dp83867
->set_clk_output
= true;
613 /* Valid values are 0 to DP83867_CLK_O_SEL_REF_CLK or
614 * DP83867_CLK_O_SEL_OFF.
616 if (dp83867
->clk_output_sel
> DP83867_CLK_O_SEL_REF_CLK
&&
617 dp83867
->clk_output_sel
!= DP83867_CLK_O_SEL_OFF
) {
618 phydev_err(phydev
, "ti,clk-output-sel value %u out of range\n",
619 dp83867
->clk_output_sel
);
624 ret
= dp83867_of_init_io_impedance(phydev
);
628 dp83867
->rxctrl_strap_quirk
= of_property_read_bool(of_node
,
629 "ti,dp83867-rxctrl-strap-quirk");
631 dp83867
->sgmii_ref_clk_en
= of_property_read_bool(of_node
,
632 "ti,sgmii-ref-clock-output-enable");
634 dp83867
->rx_id_delay
= DP83867_RGMII_RX_CLK_DELAY_INV
;
635 ret
= of_property_read_u32(of_node
, "ti,rx-internal-delay",
636 &dp83867
->rx_id_delay
);
637 if (!ret
&& dp83867
->rx_id_delay
> DP83867_RGMII_RX_CLK_DELAY_MAX
) {
639 "ti,rx-internal-delay value of %u out of range\n",
640 dp83867
->rx_id_delay
);
644 dp83867
->tx_id_delay
= DP83867_RGMII_TX_CLK_DELAY_INV
;
645 ret
= of_property_read_u32(of_node
, "ti,tx-internal-delay",
646 &dp83867
->tx_id_delay
);
647 if (!ret
&& dp83867
->tx_id_delay
> DP83867_RGMII_TX_CLK_DELAY_MAX
) {
649 "ti,tx-internal-delay value of %u out of range\n",
650 dp83867
->tx_id_delay
);
654 if (of_property_read_bool(of_node
, "enet-phy-lane-swap"))
655 dp83867
->port_mirroring
= DP83867_PORT_MIRROING_EN
;
657 if (of_property_read_bool(of_node
, "enet-phy-lane-no-swap"))
658 dp83867
->port_mirroring
= DP83867_PORT_MIRROING_DIS
;
660 ret
= of_property_read_u32(of_node
, "ti,fifo-depth",
661 &dp83867
->tx_fifo_depth
);
663 ret
= of_property_read_u32(of_node
, "tx-fifo-depth",
664 &dp83867
->tx_fifo_depth
);
666 dp83867
->tx_fifo_depth
=
667 DP83867_PHYCR_FIFO_DEPTH_4_B_NIB
;
670 if (dp83867
->tx_fifo_depth
> DP83867_PHYCR_FIFO_DEPTH_MAX
) {
671 phydev_err(phydev
, "tx-fifo-depth value %u out of range\n",
672 dp83867
->tx_fifo_depth
);
676 ret
= of_property_read_u32(of_node
, "rx-fifo-depth",
677 &dp83867
->rx_fifo_depth
);
679 dp83867
->rx_fifo_depth
= DP83867_PHYCR_FIFO_DEPTH_4_B_NIB
;
681 if (dp83867
->rx_fifo_depth
> DP83867_PHYCR_FIFO_DEPTH_MAX
) {
682 phydev_err(phydev
, "rx-fifo-depth value %u out of range\n",
683 dp83867
->rx_fifo_depth
);
690 static int dp83867_of_init(struct phy_device
*phydev
)
692 struct dp83867_private
*dp83867
= phydev
->priv
;
695 /* For non-OF device, the RX and TX ID values are either strapped
696 * or take from default value. So, we init RX & TX ID values here
697 * so that the RGMIIDCTL is configured correctly later in
698 * dp83867_config_init();
700 delay
= phy_read_mmd(phydev
, DP83867_DEVADDR
, DP83867_RGMIIDCTL
);
701 dp83867
->rx_id_delay
= delay
& DP83867_RGMII_RX_CLK_DELAY_MAX
;
702 dp83867
->tx_id_delay
= (delay
>> DP83867_RGMII_TX_CLK_DELAY_SHIFT
) &
703 DP83867_RGMII_TX_CLK_DELAY_MAX
;
705 /* Per datasheet, IO impedance is default to 50-ohm, so we set the
706 * same here or else the default '0' means highest IO impedance
709 dp83867
->io_impedance
= DP83867_IO_MUX_CFG_IO_IMPEDANCE_MIN
/ 2;
711 /* For non-OF device, the RX and TX FIFO depths are taken from
712 * default value. So, we init RX & TX FIFO depths here
713 * so that it is configured correctly later in dp83867_config_init();
715 dp83867
->tx_fifo_depth
= DP83867_PHYCR_FIFO_DEPTH_4_B_NIB
;
716 dp83867
->rx_fifo_depth
= DP83867_PHYCR_FIFO_DEPTH_4_B_NIB
;
720 #endif /* CONFIG_OF_MDIO */
722 static int dp83867_suspend(struct phy_device
*phydev
)
724 /* Disable PHY Interrupts */
725 if (phy_interrupt_is_valid(phydev
)) {
726 phydev
->interrupts
= PHY_INTERRUPT_DISABLED
;
727 dp83867_config_intr(phydev
);
730 return genphy_suspend(phydev
);
733 static int dp83867_resume(struct phy_device
*phydev
)
735 /* Enable PHY Interrupts */
736 if (phy_interrupt_is_valid(phydev
)) {
737 phydev
->interrupts
= PHY_INTERRUPT_ENABLED
;
738 dp83867_config_intr(phydev
);
741 genphy_resume(phydev
);
746 static int dp83867_probe(struct phy_device
*phydev
)
748 struct dp83867_private
*dp83867
;
750 dp83867
= devm_kzalloc(&phydev
->mdio
.dev
, sizeof(*dp83867
),
755 phydev
->priv
= dp83867
;
757 return dp83867_of_init(phydev
);
760 static int dp83867_config_init(struct phy_device
*phydev
)
762 struct dp83867_private
*dp83867
= phydev
->priv
;
766 /* Force speed optimization for the PHY even if it strapped */
767 ret
= phy_modify(phydev
, DP83867_CFG2
, DP83867_DOWNSHIFT_EN
,
768 DP83867_DOWNSHIFT_EN
);
772 ret
= dp83867_verify_rgmii_cfg(phydev
);
776 /* RX_DV/RX_CTRL strapped in mode 1 or mode 2 workaround */
777 if (dp83867
->rxctrl_strap_quirk
)
778 phy_clear_bits_mmd(phydev
, DP83867_DEVADDR
, DP83867_CFG4
,
781 bs
= phy_read_mmd(phydev
, DP83867_DEVADDR
, DP83867_STRAP_STS2
);
782 if (bs
& DP83867_STRAP_STS2_STRAP_FLD
) {
783 /* When using strap to enable FLD, the ENERGY_LOST_FLD_THR will
784 * be set to 0x2. This may causes the PHY link to be unstable -
785 * the default value 0x1 need to be restored.
787 ret
= phy_modify_mmd(phydev
, DP83867_DEVADDR
,
789 DP83867_FLD_THR_CFG_ENERGY_LOST_THR_MASK
,
795 if (phy_interface_is_rgmii(phydev
) ||
796 phydev
->interface
== PHY_INTERFACE_MODE_SGMII
) {
797 val
= phy_read(phydev
, MII_DP83867_PHYCTRL
);
801 val
&= ~DP83867_PHYCR_TX_FIFO_DEPTH_MASK
;
802 val
|= (dp83867
->tx_fifo_depth
<<
803 DP83867_PHYCR_TX_FIFO_DEPTH_SHIFT
);
805 if (phydev
->interface
== PHY_INTERFACE_MODE_SGMII
) {
806 val
&= ~DP83867_PHYCR_RX_FIFO_DEPTH_MASK
;
807 val
|= (dp83867
->rx_fifo_depth
<<
808 DP83867_PHYCR_RX_FIFO_DEPTH_SHIFT
);
811 ret
= phy_write(phydev
, MII_DP83867_PHYCTRL
, val
);
816 if (phy_interface_is_rgmii(phydev
)) {
817 val
= phy_read(phydev
, MII_DP83867_PHYCTRL
);
821 /* The code below checks if "port mirroring" N/A MODE4 has been
822 * enabled during power on bootstrap.
824 * Such N/A mode enabled by mistake can put PHY IC in some
825 * internal testing mode and disable RGMII transmission.
827 * In this particular case one needs to check STRAP_STS1
828 * register's bit 11 (marked as RESERVED).
831 bs
= phy_read_mmd(phydev
, DP83867_DEVADDR
, DP83867_STRAP_STS1
);
832 if (bs
& DP83867_STRAP_STS1_RESERVED
)
833 val
&= ~DP83867_PHYCR_RESERVED_MASK
;
835 ret
= phy_write(phydev
, MII_DP83867_PHYCTRL
, val
);
839 /* If rgmii mode with no internal delay is selected, we do NOT use
840 * aligned mode as one might expect. Instead we use the PHY's default
841 * based on pin strapping. And the "mode 0" default is to *use*
842 * internal delay with a value of 7 (2.00 ns).
844 * Set up RGMII delays
846 val
= phy_read_mmd(phydev
, DP83867_DEVADDR
, DP83867_RGMIICTL
);
848 val
&= ~(DP83867_RGMII_TX_CLK_DELAY_EN
| DP83867_RGMII_RX_CLK_DELAY_EN
);
849 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
)
850 val
|= (DP83867_RGMII_TX_CLK_DELAY_EN
| DP83867_RGMII_RX_CLK_DELAY_EN
);
852 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
)
853 val
|= DP83867_RGMII_TX_CLK_DELAY_EN
;
855 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
)
856 val
|= DP83867_RGMII_RX_CLK_DELAY_EN
;
858 phy_write_mmd(phydev
, DP83867_DEVADDR
, DP83867_RGMIICTL
, val
);
861 if (dp83867
->rx_id_delay
!= DP83867_RGMII_RX_CLK_DELAY_INV
)
862 delay
|= dp83867
->rx_id_delay
;
863 if (dp83867
->tx_id_delay
!= DP83867_RGMII_TX_CLK_DELAY_INV
)
864 delay
|= dp83867
->tx_id_delay
<<
865 DP83867_RGMII_TX_CLK_DELAY_SHIFT
;
867 phy_write_mmd(phydev
, DP83867_DEVADDR
, DP83867_RGMIIDCTL
,
871 /* If specified, set io impedance */
872 if (dp83867
->io_impedance
>= 0)
873 phy_modify_mmd(phydev
, DP83867_DEVADDR
, DP83867_IO_MUX_CFG
,
874 DP83867_IO_MUX_CFG_IO_IMPEDANCE_MASK
,
875 dp83867
->io_impedance
);
877 if (phydev
->interface
== PHY_INTERFACE_MODE_SGMII
) {
878 /* For support SPEED_10 in SGMII mode
879 * DP83867_10M_SGMII_RATE_ADAPT bit
880 * has to be cleared by software. That
881 * does not affect SPEED_100 and
884 ret
= phy_modify_mmd(phydev
, DP83867_DEVADDR
,
885 DP83867_10M_SGMII_CFG
,
886 DP83867_10M_SGMII_RATE_ADAPT_MASK
,
891 /* After reset SGMII Autoneg timer is set to 2us (bits 6 and 5
892 * are 01). That is not enough to finalize autoneg on some
893 * devices. Increase this timer duration to maximum 16ms.
895 ret
= phy_modify_mmd(phydev
, DP83867_DEVADDR
,
897 DP83867_CFG4_SGMII_ANEG_MASK
,
898 DP83867_CFG4_SGMII_ANEG_TIMER_16MS
);
903 val
= phy_read_mmd(phydev
, DP83867_DEVADDR
, DP83867_SGMIICTL
);
904 /* SGMII type is set to 4-wire mode by default.
905 * If we place appropriate property in dts (see above)
906 * switch on 6-wire mode.
908 if (dp83867
->sgmii_ref_clk_en
)
909 val
|= DP83867_SGMII_TYPE
;
911 val
&= ~DP83867_SGMII_TYPE
;
912 phy_write_mmd(phydev
, DP83867_DEVADDR
, DP83867_SGMIICTL
, val
);
914 /* This is a SW workaround for link instability if RX_CTRL is
915 * not strapped to mode 3 or 4 in HW. This is required for SGMII
916 * in addition to clearing bit 7, handled above.
918 if (dp83867
->rxctrl_strap_quirk
)
919 phy_set_bits_mmd(phydev
, DP83867_DEVADDR
, DP83867_CFG4
,
923 val
= phy_read(phydev
, DP83867_CFG3
);
924 /* Enable Interrupt output INT_OE in CFG3 register */
925 if (phy_interrupt_is_valid(phydev
))
926 val
|= DP83867_CFG3_INT_OE
;
928 val
|= DP83867_CFG3_ROBUST_AUTO_MDIX
;
929 phy_write(phydev
, DP83867_CFG3
, val
);
931 if (dp83867
->port_mirroring
!= DP83867_PORT_MIRROING_KEEP
)
932 dp83867_config_port_mirroring(phydev
);
934 /* Clock output selection if muxing property is set */
935 if (dp83867
->set_clk_output
) {
936 u16 mask
= DP83867_IO_MUX_CFG_CLK_O_DISABLE
;
938 if (dp83867
->clk_output_sel
== DP83867_CLK_O_SEL_OFF
) {
939 val
= DP83867_IO_MUX_CFG_CLK_O_DISABLE
;
941 mask
|= DP83867_IO_MUX_CFG_CLK_O_SEL_MASK
;
942 val
= dp83867
->clk_output_sel
<<
943 DP83867_IO_MUX_CFG_CLK_O_SEL_SHIFT
;
946 phy_modify_mmd(phydev
, DP83867_DEVADDR
, DP83867_IO_MUX_CFG
,
953 static int dp83867_phy_reset(struct phy_device
*phydev
)
957 err
= phy_write(phydev
, DP83867_CTRL
, DP83867_SW_RESET
);
961 usleep_range(10, 20);
963 err
= phy_modify(phydev
, MII_DP83867_PHYCTRL
,
964 DP83867_PHYCR_FORCE_LINK_GOOD
, 0);
968 /* Configure the DSP Feedforward Equalizer Configuration register to
969 * improve short cable (< 1 meter) performance. This will not affect
970 * long cable performance.
972 err
= phy_write_mmd(phydev
, DP83867_DEVADDR
, DP83867_DSP_FFE_CFG
,
977 err
= phy_write(phydev
, DP83867_CTRL
, DP83867_SW_RESTART
);
981 usleep_range(10, 20);
986 static void dp83867_link_change_notify(struct phy_device
*phydev
)
988 /* There is a limitation in DP83867 PHY device where SGMII AN is
989 * only triggered once after the device is booted up. Even after the
990 * PHY TPI is down and up again, SGMII AN is not triggered and
991 * hence no new in-band message from PHY to MAC side SGMII.
992 * This could cause an issue during power up, when PHY is up prior
993 * to MAC. At this condition, once MAC side SGMII is up, MAC side
994 * SGMII wouldn`t receive new in-band message from TI PHY with
995 * correct link status, speed and duplex info.
996 * Thus, implemented a SW solution here to retrigger SGMII Auto-Neg
997 * whenever there is a link change.
999 if (phydev
->interface
== PHY_INTERFACE_MODE_SGMII
) {
1002 val
= phy_clear_bits(phydev
, DP83867_CFG2
,
1003 DP83867_SGMII_AUTONEG_EN
);
1007 phy_set_bits(phydev
, DP83867_CFG2
,
1008 DP83867_SGMII_AUTONEG_EN
);
1012 static int dp83867_loopback(struct phy_device
*phydev
, bool enable
)
1014 return phy_modify(phydev
, MII_BMCR
, BMCR_LOOPBACK
,
1015 enable
? BMCR_LOOPBACK
: 0);
1019 dp83867_led_brightness_set(struct phy_device
*phydev
,
1020 u8 index
, enum led_brightness brightness
)
1024 if (index
>= DP83867_LED_COUNT
)
1027 /* DRV_EN==1: output is DRV_VAL */
1028 val
= DP83867_LED_DRV_EN(index
);
1031 val
|= DP83867_LED_DRV_VAL(index
);
1033 return phy_modify(phydev
, DP83867_LEDCR2
,
1034 DP83867_LED_DRV_VAL(index
) |
1035 DP83867_LED_DRV_EN(index
),
1039 static int dp83867_led_mode(u8 index
, unsigned long rules
)
1041 if (index
>= DP83867_LED_COUNT
)
1045 case BIT(TRIGGER_NETDEV_LINK
):
1046 return DP83867_LED_FN_LINK
;
1047 case BIT(TRIGGER_NETDEV_LINK_10
):
1048 return DP83867_LED_FN_LINK_10_BT
;
1049 case BIT(TRIGGER_NETDEV_LINK_100
):
1050 return DP83867_LED_FN_LINK_100_BTX
;
1051 case BIT(TRIGGER_NETDEV_FULL_DUPLEX
):
1052 return DP83867_LED_FN_FULL_DUPLEX
;
1053 case BIT(TRIGGER_NETDEV_TX
):
1054 return DP83867_LED_FN_TX
;
1055 case BIT(TRIGGER_NETDEV_RX
):
1056 return DP83867_LED_FN_RX
;
1057 case BIT(TRIGGER_NETDEV_LINK_1000
):
1058 return DP83867_LED_FN_LINK_1000_BT
;
1059 case BIT(TRIGGER_NETDEV_TX
) | BIT(TRIGGER_NETDEV_RX
):
1060 return DP83867_LED_FN_RX_TX
;
1061 case BIT(TRIGGER_NETDEV_LINK_100
) | BIT(TRIGGER_NETDEV_LINK_1000
):
1062 return DP83867_LED_FN_LINK_100_1000_BT
;
1063 case BIT(TRIGGER_NETDEV_LINK_10
) | BIT(TRIGGER_NETDEV_LINK_100
):
1064 return DP83867_LED_FN_LINK_10_100_BT
;
1065 case BIT(TRIGGER_NETDEV_LINK
) | BIT(TRIGGER_NETDEV_TX
) | BIT(TRIGGER_NETDEV_RX
):
1066 return DP83867_LED_FN_LINK_RX_TX
;
1072 static int dp83867_led_hw_is_supported(struct phy_device
*phydev
, u8 index
,
1073 unsigned long rules
)
1077 ret
= dp83867_led_mode(index
, rules
);
1084 static int dp83867_led_hw_control_set(struct phy_device
*phydev
, u8 index
,
1085 unsigned long rules
)
1089 mode
= dp83867_led_mode(index
, rules
);
1093 ret
= phy_modify(phydev
, DP83867_LEDCR1
, DP83867_LED_FN_MASK(index
),
1094 DP83867_LED_FN(index
, mode
));
1098 return phy_modify(phydev
, DP83867_LEDCR2
, DP83867_LED_DRV_EN(index
), 0);
1101 static int dp83867_led_hw_control_get(struct phy_device
*phydev
, u8 index
,
1102 unsigned long *rules
)
1106 val
= phy_read(phydev
, DP83867_LEDCR1
);
1110 val
&= DP83867_LED_FN_MASK(index
);
1114 case DP83867_LED_FN_LINK
:
1115 *rules
= BIT(TRIGGER_NETDEV_LINK
);
1117 case DP83867_LED_FN_LINK_10_BT
:
1118 *rules
= BIT(TRIGGER_NETDEV_LINK_10
);
1120 case DP83867_LED_FN_LINK_100_BTX
:
1121 *rules
= BIT(TRIGGER_NETDEV_LINK_100
);
1123 case DP83867_LED_FN_FULL_DUPLEX
:
1124 *rules
= BIT(TRIGGER_NETDEV_FULL_DUPLEX
);
1126 case DP83867_LED_FN_TX
:
1127 *rules
= BIT(TRIGGER_NETDEV_TX
);
1129 case DP83867_LED_FN_RX
:
1130 *rules
= BIT(TRIGGER_NETDEV_RX
);
1132 case DP83867_LED_FN_LINK_1000_BT
:
1133 *rules
= BIT(TRIGGER_NETDEV_LINK_1000
);
1135 case DP83867_LED_FN_RX_TX
:
1136 *rules
= BIT(TRIGGER_NETDEV_TX
) | BIT(TRIGGER_NETDEV_RX
);
1138 case DP83867_LED_FN_LINK_100_1000_BT
:
1139 *rules
= BIT(TRIGGER_NETDEV_LINK_100
) | BIT(TRIGGER_NETDEV_LINK_1000
);
1141 case DP83867_LED_FN_LINK_10_100_BT
:
1142 *rules
= BIT(TRIGGER_NETDEV_LINK_10
) | BIT(TRIGGER_NETDEV_LINK_100
);
1144 case DP83867_LED_FN_LINK_RX_TX
:
1145 *rules
= BIT(TRIGGER_NETDEV_LINK
) | BIT(TRIGGER_NETDEV_TX
) |
1146 BIT(TRIGGER_NETDEV_RX
);
1156 static int dp83867_led_polarity_set(struct phy_device
*phydev
, int index
,
1157 unsigned long modes
)
1159 /* Default active high */
1160 u16 polarity
= DP83867_LED_POLARITY(index
);
1163 for_each_set_bit(mode
, &modes
, __PHY_LED_MODES_NUM
) {
1165 case PHY_LED_ACTIVE_LOW
:
1172 return phy_modify(phydev
, DP83867_LEDCR2
,
1173 DP83867_LED_POLARITY(index
), polarity
);
1176 static struct phy_driver dp83867_driver
[] = {
1178 .phy_id
= DP83867_PHY_ID
,
1179 .phy_id_mask
= 0xfffffff0,
1180 .name
= "TI DP83867",
1181 /* PHY_GBIT_FEATURES */
1183 .probe
= dp83867_probe
,
1184 .config_init
= dp83867_config_init
,
1185 .soft_reset
= dp83867_phy_reset
,
1187 .read_status
= dp83867_read_status
,
1188 .get_tunable
= dp83867_get_tunable
,
1189 .set_tunable
= dp83867_set_tunable
,
1191 .get_wol
= dp83867_get_wol
,
1192 .set_wol
= dp83867_set_wol
,
1195 .config_intr
= dp83867_config_intr
,
1196 .handle_interrupt
= dp83867_handle_interrupt
,
1198 .suspend
= dp83867_suspend
,
1199 .resume
= dp83867_resume
,
1201 .link_change_notify
= dp83867_link_change_notify
,
1202 .set_loopback
= dp83867_loopback
,
1204 .led_brightness_set
= dp83867_led_brightness_set
,
1205 .led_hw_is_supported
= dp83867_led_hw_is_supported
,
1206 .led_hw_control_set
= dp83867_led_hw_control_set
,
1207 .led_hw_control_get
= dp83867_led_hw_control_get
,
1208 .led_polarity_set
= dp83867_led_polarity_set
,
1211 module_phy_driver(dp83867_driver
);
1213 static struct mdio_device_id __maybe_unused dp83867_tbl
[] = {
1214 { DP83867_PHY_ID
, 0xfffffff0 },
1218 MODULE_DEVICE_TABLE(mdio
, dp83867_tbl
);
1220 MODULE_DESCRIPTION("Texas Instruments DP83867 PHY driver");
1221 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com");
1222 MODULE_LICENSE("GPL v2");