1 // SPDX-License-Identifier: GPL-2.0+
3 * drivers/net/phy/at803x.c
5 * Driver for Atheros 803x PHY
7 * Author: Matus Ujhelyi <ujhelyi.m@gmail.com>
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/of_gpio.h>
16 #include <linux/gpio/consumer.h>
18 #define AT803X_INTR_ENABLE 0x12
19 #define AT803X_INTR_ENABLE_AUTONEG_ERR BIT(15)
20 #define AT803X_INTR_ENABLE_SPEED_CHANGED BIT(14)
21 #define AT803X_INTR_ENABLE_DUPLEX_CHANGED BIT(13)
22 #define AT803X_INTR_ENABLE_PAGE_RECEIVED BIT(12)
23 #define AT803X_INTR_ENABLE_LINK_FAIL BIT(11)
24 #define AT803X_INTR_ENABLE_LINK_SUCCESS BIT(10)
25 #define AT803X_INTR_ENABLE_WIRESPEED_DOWNGRADE BIT(5)
26 #define AT803X_INTR_ENABLE_POLARITY_CHANGED BIT(1)
27 #define AT803X_INTR_ENABLE_WOL BIT(0)
29 #define AT803X_INTR_STATUS 0x13
31 #define AT803X_SMART_SPEED 0x14
32 #define AT803X_LED_CONTROL 0x18
34 #define AT803X_DEVICE_ADDR 0x03
35 #define AT803X_LOC_MAC_ADDR_0_15_OFFSET 0x804C
36 #define AT803X_LOC_MAC_ADDR_16_31_OFFSET 0x804B
37 #define AT803X_LOC_MAC_ADDR_32_47_OFFSET 0x804A
38 #define AT803X_REG_CHIP_CONFIG 0x1f
39 #define AT803X_BT_BX_REG_SEL 0x8000
41 #define AT803X_DEBUG_ADDR 0x1D
42 #define AT803X_DEBUG_DATA 0x1E
44 #define AT803X_MODE_CFG_MASK 0x0F
45 #define AT803X_MODE_CFG_SGMII 0x01
47 #define AT803X_PSSR 0x11 /*PHY-Specific Status Register*/
48 #define AT803X_PSSR_MR_AN_COMPLETE 0x0200
50 #define AT803X_DEBUG_REG_0 0x00
51 #define AT803X_DEBUG_RX_CLK_DLY_EN BIT(15)
53 #define AT803X_DEBUG_REG_5 0x05
54 #define AT803X_DEBUG_TX_CLK_DLY_EN BIT(8)
56 #define ATH8030_PHY_ID 0x004dd076
57 #define ATH8031_PHY_ID 0x004dd074
58 #define ATH8035_PHY_ID 0x004dd072
59 #define AT803X_PHY_ID_MASK 0xffffffef
61 MODULE_DESCRIPTION("Atheros 803x PHY driver");
62 MODULE_AUTHOR("Matus Ujhelyi");
63 MODULE_LICENSE("GPL");
69 struct at803x_context
{
78 static int at803x_debug_reg_read(struct phy_device
*phydev
, u16 reg
)
82 ret
= phy_write(phydev
, AT803X_DEBUG_ADDR
, reg
);
86 return phy_read(phydev
, AT803X_DEBUG_DATA
);
89 static int at803x_debug_reg_mask(struct phy_device
*phydev
, u16 reg
,
95 ret
= at803x_debug_reg_read(phydev
, reg
);
103 return phy_write(phydev
, AT803X_DEBUG_DATA
, val
);
106 static int at803x_enable_rx_delay(struct phy_device
*phydev
)
108 return at803x_debug_reg_mask(phydev
, AT803X_DEBUG_REG_0
, 0,
109 AT803X_DEBUG_RX_CLK_DLY_EN
);
112 static int at803x_enable_tx_delay(struct phy_device
*phydev
)
114 return at803x_debug_reg_mask(phydev
, AT803X_DEBUG_REG_5
, 0,
115 AT803X_DEBUG_TX_CLK_DLY_EN
);
118 static int at803x_disable_rx_delay(struct phy_device
*phydev
)
120 return at803x_debug_reg_mask(phydev
, AT803X_DEBUG_REG_0
,
121 AT803X_DEBUG_RX_CLK_DLY_EN
, 0);
124 static int at803x_disable_tx_delay(struct phy_device
*phydev
)
126 return at803x_debug_reg_mask(phydev
, AT803X_DEBUG_REG_5
,
127 AT803X_DEBUG_TX_CLK_DLY_EN
, 0);
130 /* save relevant PHY registers to private copy */
131 static void at803x_context_save(struct phy_device
*phydev
,
132 struct at803x_context
*context
)
134 context
->bmcr
= phy_read(phydev
, MII_BMCR
);
135 context
->advertise
= phy_read(phydev
, MII_ADVERTISE
);
136 context
->control1000
= phy_read(phydev
, MII_CTRL1000
);
137 context
->int_enable
= phy_read(phydev
, AT803X_INTR_ENABLE
);
138 context
->smart_speed
= phy_read(phydev
, AT803X_SMART_SPEED
);
139 context
->led_control
= phy_read(phydev
, AT803X_LED_CONTROL
);
142 /* restore relevant PHY registers from private copy */
143 static void at803x_context_restore(struct phy_device
*phydev
,
144 const struct at803x_context
*context
)
146 phy_write(phydev
, MII_BMCR
, context
->bmcr
);
147 phy_write(phydev
, MII_ADVERTISE
, context
->advertise
);
148 phy_write(phydev
, MII_CTRL1000
, context
->control1000
);
149 phy_write(phydev
, AT803X_INTR_ENABLE
, context
->int_enable
);
150 phy_write(phydev
, AT803X_SMART_SPEED
, context
->smart_speed
);
151 phy_write(phydev
, AT803X_LED_CONTROL
, context
->led_control
);
154 static int at803x_set_wol(struct phy_device
*phydev
,
155 struct ethtool_wolinfo
*wol
)
157 struct net_device
*ndev
= phydev
->attached_dev
;
161 unsigned int i
, offsets
[] = {
162 AT803X_LOC_MAC_ADDR_32_47_OFFSET
,
163 AT803X_LOC_MAC_ADDR_16_31_OFFSET
,
164 AT803X_LOC_MAC_ADDR_0_15_OFFSET
,
170 if (wol
->wolopts
& WAKE_MAGIC
) {
171 mac
= (const u8
*) ndev
->dev_addr
;
173 if (!is_valid_ether_addr(mac
))
176 for (i
= 0; i
< 3; i
++)
177 phy_write_mmd(phydev
, AT803X_DEVICE_ADDR
, offsets
[i
],
178 mac
[(i
* 2) + 1] | (mac
[(i
* 2)] << 8));
180 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
181 value
|= AT803X_INTR_ENABLE_WOL
;
182 ret
= phy_write(phydev
, AT803X_INTR_ENABLE
, value
);
185 value
= phy_read(phydev
, AT803X_INTR_STATUS
);
187 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
188 value
&= (~AT803X_INTR_ENABLE_WOL
);
189 ret
= phy_write(phydev
, AT803X_INTR_ENABLE
, value
);
192 value
= phy_read(phydev
, AT803X_INTR_STATUS
);
198 static void at803x_get_wol(struct phy_device
*phydev
,
199 struct ethtool_wolinfo
*wol
)
203 wol
->supported
= WAKE_MAGIC
;
206 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
207 if (value
& AT803X_INTR_ENABLE_WOL
)
208 wol
->wolopts
|= WAKE_MAGIC
;
211 static int at803x_suspend(struct phy_device
*phydev
)
216 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
217 wol_enabled
= value
& AT803X_INTR_ENABLE_WOL
;
220 value
= BMCR_ISOLATE
;
224 phy_modify(phydev
, MII_BMCR
, 0, value
);
229 static int at803x_resume(struct phy_device
*phydev
)
231 return phy_modify(phydev
, MII_BMCR
, BMCR_PDOWN
| BMCR_ISOLATE
, 0);
234 static int at803x_probe(struct phy_device
*phydev
)
236 struct device
*dev
= &phydev
->mdio
.dev
;
237 struct at803x_priv
*priv
;
239 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
248 static int at803x_config_init(struct phy_device
*phydev
)
252 ret
= genphy_config_init(phydev
);
256 /* The RX and TX delay default is:
257 * after HW reset: RX delay enabled and TX delay disabled
258 * after SW reset: RX delay enabled, while TX delay retains the
259 * value before reset.
261 * So let's first disable the RX and TX delays in PHY and enable
262 * them based on the mode selected (this also takes care of RGMII
263 * mode where we expect delays to be disabled)
266 ret
= at803x_disable_rx_delay(phydev
);
269 ret
= at803x_disable_tx_delay(phydev
);
273 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
||
274 phydev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
) {
275 /* If RGMII_ID or RGMII_RXID are specified enable RX delay,
276 * otherwise keep it disabled
278 ret
= at803x_enable_rx_delay(phydev
);
283 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
||
284 phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
) {
285 /* If RGMII_ID or RGMII_TXID are specified enable TX delay,
286 * otherwise keep it disabled
288 ret
= at803x_enable_tx_delay(phydev
);
294 static int at803x_ack_interrupt(struct phy_device
*phydev
)
298 err
= phy_read(phydev
, AT803X_INTR_STATUS
);
300 return (err
< 0) ? err
: 0;
303 static int at803x_config_intr(struct phy_device
*phydev
)
308 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
310 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
) {
311 value
|= AT803X_INTR_ENABLE_AUTONEG_ERR
;
312 value
|= AT803X_INTR_ENABLE_SPEED_CHANGED
;
313 value
|= AT803X_INTR_ENABLE_DUPLEX_CHANGED
;
314 value
|= AT803X_INTR_ENABLE_LINK_FAIL
;
315 value
|= AT803X_INTR_ENABLE_LINK_SUCCESS
;
317 err
= phy_write(phydev
, AT803X_INTR_ENABLE
, value
);
320 err
= phy_write(phydev
, AT803X_INTR_ENABLE
, 0);
325 static void at803x_link_change_notify(struct phy_device
*phydev
)
327 struct at803x_priv
*priv
= phydev
->priv
;
330 * Conduct a hardware reset for AT8030 every time a link loss is
331 * signalled. This is necessary to circumvent a hardware bug that
332 * occurs when the cable is unplugged while TX packets are pending
333 * in the FIFO. In such cases, the FIFO enters an error mode it
334 * cannot recover from by software.
336 if (phydev
->state
== PHY_NOLINK
) {
337 if (phydev
->mdio
.reset
&& !priv
->phy_reset
) {
338 struct at803x_context context
;
340 at803x_context_save(phydev
, &context
);
342 phy_device_reset(phydev
, 1);
344 phy_device_reset(phydev
, 0);
347 at803x_context_restore(phydev
, &context
);
349 phydev_dbg(phydev
, "%s(): phy was reset\n",
351 priv
->phy_reset
= true;
354 priv
->phy_reset
= false;
358 static int at803x_aneg_done(struct phy_device
*phydev
)
362 int aneg_done
= genphy_aneg_done(phydev
);
363 if (aneg_done
!= BMSR_ANEGCOMPLETE
)
367 * in SGMII mode, if copper side autoneg is successful,
368 * also check SGMII side autoneg result
370 ccr
= phy_read(phydev
, AT803X_REG_CHIP_CONFIG
);
371 if ((ccr
& AT803X_MODE_CFG_MASK
) != AT803X_MODE_CFG_SGMII
)
374 /* switch to SGMII/fiber page */
375 phy_write(phydev
, AT803X_REG_CHIP_CONFIG
, ccr
& ~AT803X_BT_BX_REG_SEL
);
377 /* check if the SGMII link is OK. */
378 if (!(phy_read(phydev
, AT803X_PSSR
) & AT803X_PSSR_MR_AN_COMPLETE
)) {
379 phydev_warn(phydev
, "803x_aneg_done: SGMII link is not ok\n");
382 /* switch back to copper page */
383 phy_write(phydev
, AT803X_REG_CHIP_CONFIG
, ccr
| AT803X_BT_BX_REG_SEL
);
388 static struct phy_driver at803x_driver
[] = {
391 .phy_id
= ATH8035_PHY_ID
,
392 .name
= "Atheros 8035 ethernet",
393 .phy_id_mask
= AT803X_PHY_ID_MASK
,
394 .probe
= at803x_probe
,
395 .config_init
= at803x_config_init
,
396 .set_wol
= at803x_set_wol
,
397 .get_wol
= at803x_get_wol
,
398 .suspend
= at803x_suspend
,
399 .resume
= at803x_resume
,
400 .features
= PHY_GBIT_FEATURES
,
401 .ack_interrupt
= at803x_ack_interrupt
,
402 .config_intr
= at803x_config_intr
,
405 .phy_id
= ATH8030_PHY_ID
,
406 .name
= "Atheros 8030 ethernet",
407 .phy_id_mask
= AT803X_PHY_ID_MASK
,
408 .probe
= at803x_probe
,
409 .config_init
= at803x_config_init
,
410 .link_change_notify
= at803x_link_change_notify
,
411 .set_wol
= at803x_set_wol
,
412 .get_wol
= at803x_get_wol
,
413 .suspend
= at803x_suspend
,
414 .resume
= at803x_resume
,
415 .features
= PHY_BASIC_FEATURES
,
416 .ack_interrupt
= at803x_ack_interrupt
,
417 .config_intr
= at803x_config_intr
,
420 .phy_id
= ATH8031_PHY_ID
,
421 .name
= "Atheros 8031 ethernet",
422 .phy_id_mask
= AT803X_PHY_ID_MASK
,
423 .probe
= at803x_probe
,
424 .config_init
= at803x_config_init
,
425 .set_wol
= at803x_set_wol
,
426 .get_wol
= at803x_get_wol
,
427 .suspend
= at803x_suspend
,
428 .resume
= at803x_resume
,
429 .features
= PHY_GBIT_FEATURES
,
430 .aneg_done
= at803x_aneg_done
,
431 .ack_interrupt
= &at803x_ack_interrupt
,
432 .config_intr
= &at803x_config_intr
,
435 module_phy_driver(at803x_driver
);
437 static struct mdio_device_id __maybe_unused atheros_tbl
[] = {
438 { ATH8030_PHY_ID
, AT803X_PHY_ID_MASK
},
439 { ATH8031_PHY_ID
, AT803X_PHY_ID_MASK
},
440 { ATH8035_PHY_ID
, AT803X_PHY_ID_MASK
},
444 MODULE_DEVICE_TABLE(mdio
, atheros_tbl
);