2 * drivers/net/phy/at803x.c
4 * Driver for Atheros 803x PHY
6 * Author: Matus Ujhelyi <ujhelyi.m@gmail.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/phy.h>
15 #include <linux/module.h>
16 #include <linux/string.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/of_gpio.h>
20 #include <linux/gpio/consumer.h>
22 #define AT803X_INTR_ENABLE 0x12
23 #define AT803X_INTR_ENABLE_AUTONEG_ERR BIT(15)
24 #define AT803X_INTR_ENABLE_SPEED_CHANGED BIT(14)
25 #define AT803X_INTR_ENABLE_DUPLEX_CHANGED BIT(13)
26 #define AT803X_INTR_ENABLE_PAGE_RECEIVED BIT(12)
27 #define AT803X_INTR_ENABLE_LINK_FAIL BIT(11)
28 #define AT803X_INTR_ENABLE_LINK_SUCCESS BIT(10)
29 #define AT803X_INTR_ENABLE_WIRESPEED_DOWNGRADE BIT(5)
30 #define AT803X_INTR_ENABLE_POLARITY_CHANGED BIT(1)
31 #define AT803X_INTR_ENABLE_WOL BIT(0)
33 #define AT803X_INTR_STATUS 0x13
35 #define AT803X_SMART_SPEED 0x14
36 #define AT803X_LED_CONTROL 0x18
38 #define AT803X_DEVICE_ADDR 0x03
39 #define AT803X_LOC_MAC_ADDR_0_15_OFFSET 0x804C
40 #define AT803X_LOC_MAC_ADDR_16_31_OFFSET 0x804B
41 #define AT803X_LOC_MAC_ADDR_32_47_OFFSET 0x804A
42 #define AT803X_MMD_ACCESS_CONTROL 0x0D
43 #define AT803X_MMD_ACCESS_CONTROL_DATA 0x0E
44 #define AT803X_FUNC_DATA 0x4003
45 #define AT803X_REG_CHIP_CONFIG 0x1f
46 #define AT803X_BT_BX_REG_SEL 0x8000
48 #define AT803X_DEBUG_ADDR 0x1D
49 #define AT803X_DEBUG_DATA 0x1E
51 #define AT803X_MODE_CFG_MASK 0x0F
52 #define AT803X_MODE_CFG_SGMII 0x01
54 #define AT803X_PSSR 0x11 /*PHY-Specific Status Register*/
55 #define AT803X_PSSR_MR_AN_COMPLETE 0x0200
57 #define AT803X_DEBUG_REG_0 0x00
58 #define AT803X_DEBUG_RX_CLK_DLY_EN BIT(15)
60 #define AT803X_DEBUG_REG_5 0x05
61 #define AT803X_DEBUG_TX_CLK_DLY_EN BIT(8)
63 #define ATH8030_PHY_ID 0x004dd076
64 #define ATH8031_PHY_ID 0x004dd074
65 #define ATH8035_PHY_ID 0x004dd072
66 #define AT803X_PHY_ID_MASK 0xffffffef
68 MODULE_DESCRIPTION("Atheros 803x PHY driver");
69 MODULE_AUTHOR("Matus Ujhelyi");
70 MODULE_LICENSE("GPL");
74 struct gpio_desc
*gpiod_reset
;
77 struct at803x_context
{
86 static int at803x_debug_reg_read(struct phy_device
*phydev
, u16 reg
)
90 ret
= phy_write(phydev
, AT803X_DEBUG_ADDR
, reg
);
94 return phy_read(phydev
, AT803X_DEBUG_DATA
);
97 static int at803x_debug_reg_mask(struct phy_device
*phydev
, u16 reg
,
103 ret
= at803x_debug_reg_read(phydev
, reg
);
111 return phy_write(phydev
, AT803X_DEBUG_DATA
, val
);
114 static inline int at803x_enable_rx_delay(struct phy_device
*phydev
)
116 return at803x_debug_reg_mask(phydev
, AT803X_DEBUG_REG_0
, 0,
117 AT803X_DEBUG_RX_CLK_DLY_EN
);
120 static inline int at803x_enable_tx_delay(struct phy_device
*phydev
)
122 return at803x_debug_reg_mask(phydev
, AT803X_DEBUG_REG_5
, 0,
123 AT803X_DEBUG_TX_CLK_DLY_EN
);
126 /* save relevant PHY registers to private copy */
127 static void at803x_context_save(struct phy_device
*phydev
,
128 struct at803x_context
*context
)
130 context
->bmcr
= phy_read(phydev
, MII_BMCR
);
131 context
->advertise
= phy_read(phydev
, MII_ADVERTISE
);
132 context
->control1000
= phy_read(phydev
, MII_CTRL1000
);
133 context
->int_enable
= phy_read(phydev
, AT803X_INTR_ENABLE
);
134 context
->smart_speed
= phy_read(phydev
, AT803X_SMART_SPEED
);
135 context
->led_control
= phy_read(phydev
, AT803X_LED_CONTROL
);
138 /* restore relevant PHY registers from private copy */
139 static void at803x_context_restore(struct phy_device
*phydev
,
140 const struct at803x_context
*context
)
142 phy_write(phydev
, MII_BMCR
, context
->bmcr
);
143 phy_write(phydev
, MII_ADVERTISE
, context
->advertise
);
144 phy_write(phydev
, MII_CTRL1000
, context
->control1000
);
145 phy_write(phydev
, AT803X_INTR_ENABLE
, context
->int_enable
);
146 phy_write(phydev
, AT803X_SMART_SPEED
, context
->smart_speed
);
147 phy_write(phydev
, AT803X_LED_CONTROL
, context
->led_control
);
150 static int at803x_set_wol(struct phy_device
*phydev
,
151 struct ethtool_wolinfo
*wol
)
153 struct net_device
*ndev
= phydev
->attached_dev
;
157 unsigned int i
, offsets
[] = {
158 AT803X_LOC_MAC_ADDR_32_47_OFFSET
,
159 AT803X_LOC_MAC_ADDR_16_31_OFFSET
,
160 AT803X_LOC_MAC_ADDR_0_15_OFFSET
,
166 if (wol
->wolopts
& WAKE_MAGIC
) {
167 mac
= (const u8
*) ndev
->dev_addr
;
169 if (!is_valid_ether_addr(mac
))
172 for (i
= 0; i
< 3; i
++) {
173 phy_write(phydev
, AT803X_MMD_ACCESS_CONTROL
,
175 phy_write(phydev
, AT803X_MMD_ACCESS_CONTROL_DATA
,
177 phy_write(phydev
, AT803X_MMD_ACCESS_CONTROL
,
179 phy_write(phydev
, AT803X_MMD_ACCESS_CONTROL_DATA
,
180 mac
[(i
* 2) + 1] | (mac
[(i
* 2)] << 8));
183 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
184 value
|= AT803X_INTR_ENABLE_WOL
;
185 ret
= phy_write(phydev
, AT803X_INTR_ENABLE
, value
);
188 value
= phy_read(phydev
, AT803X_INTR_STATUS
);
190 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
191 value
&= (~AT803X_INTR_ENABLE_WOL
);
192 ret
= phy_write(phydev
, AT803X_INTR_ENABLE
, value
);
195 value
= phy_read(phydev
, AT803X_INTR_STATUS
);
201 static void at803x_get_wol(struct phy_device
*phydev
,
202 struct ethtool_wolinfo
*wol
)
206 wol
->supported
= WAKE_MAGIC
;
209 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
210 if (value
& AT803X_INTR_ENABLE_WOL
)
211 wol
->wolopts
|= WAKE_MAGIC
;
214 static int at803x_suspend(struct phy_device
*phydev
)
219 mutex_lock(&phydev
->lock
);
221 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
222 wol_enabled
= value
& AT803X_INTR_ENABLE_WOL
;
224 value
= phy_read(phydev
, MII_BMCR
);
227 value
|= BMCR_ISOLATE
;
231 phy_write(phydev
, MII_BMCR
, value
);
233 mutex_unlock(&phydev
->lock
);
238 static int at803x_resume(struct phy_device
*phydev
)
242 mutex_lock(&phydev
->lock
);
244 value
= phy_read(phydev
, MII_BMCR
);
245 value
&= ~(BMCR_PDOWN
| BMCR_ISOLATE
);
246 phy_write(phydev
, MII_BMCR
, value
);
248 mutex_unlock(&phydev
->lock
);
253 static int at803x_probe(struct phy_device
*phydev
)
255 struct device
*dev
= &phydev
->mdio
.dev
;
256 struct at803x_priv
*priv
;
257 struct gpio_desc
*gpiod_reset
;
259 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
263 if (phydev
->drv
->phy_id
!= ATH8030_PHY_ID
)
264 goto does_not_require_reset_workaround
;
266 gpiod_reset
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_LOW
);
267 if (IS_ERR(gpiod_reset
))
268 return PTR_ERR(gpiod_reset
);
270 priv
->gpiod_reset
= gpiod_reset
;
272 does_not_require_reset_workaround
:
278 static int at803x_config_init(struct phy_device
*phydev
)
282 ret
= genphy_config_init(phydev
);
286 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
||
287 phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
) {
288 ret
= at803x_enable_rx_delay(phydev
);
293 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
||
294 phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
) {
295 ret
= at803x_enable_tx_delay(phydev
);
303 static int at803x_ack_interrupt(struct phy_device
*phydev
)
307 err
= phy_read(phydev
, AT803X_INTR_STATUS
);
309 return (err
< 0) ? err
: 0;
312 static int at803x_config_intr(struct phy_device
*phydev
)
317 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
319 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
) {
320 value
|= AT803X_INTR_ENABLE_AUTONEG_ERR
;
321 value
|= AT803X_INTR_ENABLE_SPEED_CHANGED
;
322 value
|= AT803X_INTR_ENABLE_DUPLEX_CHANGED
;
323 value
|= AT803X_INTR_ENABLE_LINK_FAIL
;
324 value
|= AT803X_INTR_ENABLE_LINK_SUCCESS
;
326 err
= phy_write(phydev
, AT803X_INTR_ENABLE
, value
);
329 err
= phy_write(phydev
, AT803X_INTR_ENABLE
, 0);
334 static void at803x_link_change_notify(struct phy_device
*phydev
)
336 struct at803x_priv
*priv
= phydev
->priv
;
339 * Conduct a hardware reset for AT8030 every time a link loss is
340 * signalled. This is necessary to circumvent a hardware bug that
341 * occurs when the cable is unplugged while TX packets are pending
342 * in the FIFO. In such cases, the FIFO enters an error mode it
343 * cannot recover from by software.
345 if (phydev
->state
== PHY_NOLINK
) {
346 if (priv
->gpiod_reset
&& !priv
->phy_reset
) {
347 struct at803x_context context
;
349 at803x_context_save(phydev
, &context
);
351 gpiod_set_value(priv
->gpiod_reset
, 1);
353 gpiod_set_value(priv
->gpiod_reset
, 0);
356 at803x_context_restore(phydev
, &context
);
358 phydev_dbg(phydev
, "%s(): phy was reset\n",
360 priv
->phy_reset
= true;
363 priv
->phy_reset
= false;
367 static int at803x_aneg_done(struct phy_device
*phydev
)
371 int aneg_done
= genphy_aneg_done(phydev
);
372 if (aneg_done
!= BMSR_ANEGCOMPLETE
)
376 * in SGMII mode, if copper side autoneg is successful,
377 * also check SGMII side autoneg result
379 ccr
= phy_read(phydev
, AT803X_REG_CHIP_CONFIG
);
380 if ((ccr
& AT803X_MODE_CFG_MASK
) != AT803X_MODE_CFG_SGMII
)
383 /* switch to SGMII/fiber page */
384 phy_write(phydev
, AT803X_REG_CHIP_CONFIG
, ccr
& ~AT803X_BT_BX_REG_SEL
);
386 /* check if the SGMII link is OK. */
387 if (!(phy_read(phydev
, AT803X_PSSR
) & AT803X_PSSR_MR_AN_COMPLETE
)) {
388 pr_warn("803x_aneg_done: SGMII link is not ok\n");
391 /* switch back to copper page */
392 phy_write(phydev
, AT803X_REG_CHIP_CONFIG
, ccr
| AT803X_BT_BX_REG_SEL
);
397 static struct phy_driver at803x_driver
[] = {
400 .phy_id
= ATH8035_PHY_ID
,
401 .name
= "Atheros 8035 ethernet",
402 .phy_id_mask
= AT803X_PHY_ID_MASK
,
403 .probe
= at803x_probe
,
404 .config_init
= at803x_config_init
,
405 .set_wol
= at803x_set_wol
,
406 .get_wol
= at803x_get_wol
,
407 .suspend
= at803x_suspend
,
408 .resume
= at803x_resume
,
409 .features
= PHY_GBIT_FEATURES
,
410 .flags
= PHY_HAS_INTERRUPT
,
411 .config_aneg
= genphy_config_aneg
,
412 .read_status
= genphy_read_status
,
413 .ack_interrupt
= at803x_ack_interrupt
,
414 .config_intr
= at803x_config_intr
,
417 .phy_id
= ATH8030_PHY_ID
,
418 .name
= "Atheros 8030 ethernet",
419 .phy_id_mask
= AT803X_PHY_ID_MASK
,
420 .probe
= at803x_probe
,
421 .config_init
= at803x_config_init
,
422 .link_change_notify
= at803x_link_change_notify
,
423 .set_wol
= at803x_set_wol
,
424 .get_wol
= at803x_get_wol
,
425 .suspend
= at803x_suspend
,
426 .resume
= at803x_resume
,
427 .features
= PHY_BASIC_FEATURES
,
428 .flags
= PHY_HAS_INTERRUPT
,
429 .config_aneg
= genphy_config_aneg
,
430 .read_status
= genphy_read_status
,
431 .ack_interrupt
= at803x_ack_interrupt
,
432 .config_intr
= at803x_config_intr
,
435 .phy_id
= ATH8031_PHY_ID
,
436 .name
= "Atheros 8031 ethernet",
437 .phy_id_mask
= AT803X_PHY_ID_MASK
,
438 .probe
= at803x_probe
,
439 .config_init
= at803x_config_init
,
440 .set_wol
= at803x_set_wol
,
441 .get_wol
= at803x_get_wol
,
442 .suspend
= at803x_suspend
,
443 .resume
= at803x_resume
,
444 .features
= PHY_GBIT_FEATURES
,
445 .flags
= PHY_HAS_INTERRUPT
,
446 .config_aneg
= genphy_config_aneg
,
447 .read_status
= genphy_read_status
,
448 .aneg_done
= at803x_aneg_done
,
449 .ack_interrupt
= &at803x_ack_interrupt
,
450 .config_intr
= &at803x_config_intr
,
453 module_phy_driver(at803x_driver
);
455 static struct mdio_device_id __maybe_unused atheros_tbl
[] = {
456 { ATH8030_PHY_ID
, AT803X_PHY_ID_MASK
},
457 { ATH8031_PHY_ID
, AT803X_PHY_ID_MASK
},
458 { ATH8035_PHY_ID
, AT803X_PHY_ID_MASK
},
462 MODULE_DEVICE_TABLE(mdio
, atheros_tbl
);