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_STATUS 0x13
24 #define AT803X_SMART_SPEED 0x14
25 #define AT803X_LED_CONTROL 0x18
26 #define AT803X_WOL_ENABLE 0x01
27 #define AT803X_DEVICE_ADDR 0x03
28 #define AT803X_LOC_MAC_ADDR_0_15_OFFSET 0x804C
29 #define AT803X_LOC_MAC_ADDR_16_31_OFFSET 0x804B
30 #define AT803X_LOC_MAC_ADDR_32_47_OFFSET 0x804A
31 #define AT803X_MMD_ACCESS_CONTROL 0x0D
32 #define AT803X_MMD_ACCESS_CONTROL_DATA 0x0E
33 #define AT803X_FUNC_DATA 0x4003
34 #define AT803X_INER 0x0012
35 #define AT803X_INER_INIT 0xec00
36 #define AT803X_INSR 0x0013
37 #define AT803X_DEBUG_ADDR 0x1D
38 #define AT803X_DEBUG_DATA 0x1E
39 #define AT803X_DEBUG_SYSTEM_MODE_CTRL 0x05
40 #define AT803X_DEBUG_RGMII_TX_CLK_DLY BIT(8)
42 #define ATH8030_PHY_ID 0x004dd076
43 #define ATH8031_PHY_ID 0x004dd074
44 #define ATH8035_PHY_ID 0x004dd072
46 MODULE_DESCRIPTION("Atheros 803x PHY driver");
47 MODULE_AUTHOR("Matus Ujhelyi");
48 MODULE_LICENSE("GPL");
52 struct gpio_desc
*gpiod_reset
;
55 struct at803x_context
{
64 /* save relevant PHY registers to private copy */
65 static void at803x_context_save(struct phy_device
*phydev
,
66 struct at803x_context
*context
)
68 context
->bmcr
= phy_read(phydev
, MII_BMCR
);
69 context
->advertise
= phy_read(phydev
, MII_ADVERTISE
);
70 context
->control1000
= phy_read(phydev
, MII_CTRL1000
);
71 context
->int_enable
= phy_read(phydev
, AT803X_INTR_ENABLE
);
72 context
->smart_speed
= phy_read(phydev
, AT803X_SMART_SPEED
);
73 context
->led_control
= phy_read(phydev
, AT803X_LED_CONTROL
);
76 /* restore relevant PHY registers from private copy */
77 static void at803x_context_restore(struct phy_device
*phydev
,
78 const struct at803x_context
*context
)
80 phy_write(phydev
, MII_BMCR
, context
->bmcr
);
81 phy_write(phydev
, MII_ADVERTISE
, context
->advertise
);
82 phy_write(phydev
, MII_CTRL1000
, context
->control1000
);
83 phy_write(phydev
, AT803X_INTR_ENABLE
, context
->int_enable
);
84 phy_write(phydev
, AT803X_SMART_SPEED
, context
->smart_speed
);
85 phy_write(phydev
, AT803X_LED_CONTROL
, context
->led_control
);
88 static int at803x_set_wol(struct phy_device
*phydev
,
89 struct ethtool_wolinfo
*wol
)
91 struct net_device
*ndev
= phydev
->attached_dev
;
95 unsigned int i
, offsets
[] = {
96 AT803X_LOC_MAC_ADDR_32_47_OFFSET
,
97 AT803X_LOC_MAC_ADDR_16_31_OFFSET
,
98 AT803X_LOC_MAC_ADDR_0_15_OFFSET
,
104 if (wol
->wolopts
& WAKE_MAGIC
) {
105 mac
= (const u8
*) ndev
->dev_addr
;
107 if (!is_valid_ether_addr(mac
))
110 for (i
= 0; i
< 3; i
++) {
111 phy_write(phydev
, AT803X_MMD_ACCESS_CONTROL
,
113 phy_write(phydev
, AT803X_MMD_ACCESS_CONTROL_DATA
,
115 phy_write(phydev
, AT803X_MMD_ACCESS_CONTROL
,
117 phy_write(phydev
, AT803X_MMD_ACCESS_CONTROL_DATA
,
118 mac
[(i
* 2) + 1] | (mac
[(i
* 2)] << 8));
121 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
122 value
|= AT803X_WOL_ENABLE
;
123 ret
= phy_write(phydev
, AT803X_INTR_ENABLE
, value
);
126 value
= phy_read(phydev
, AT803X_INTR_STATUS
);
128 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
129 value
&= (~AT803X_WOL_ENABLE
);
130 ret
= phy_write(phydev
, AT803X_INTR_ENABLE
, value
);
133 value
= phy_read(phydev
, AT803X_INTR_STATUS
);
139 static void at803x_get_wol(struct phy_device
*phydev
,
140 struct ethtool_wolinfo
*wol
)
144 wol
->supported
= WAKE_MAGIC
;
147 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
148 if (value
& AT803X_WOL_ENABLE
)
149 wol
->wolopts
|= WAKE_MAGIC
;
152 static int at803x_suspend(struct phy_device
*phydev
)
157 mutex_lock(&phydev
->lock
);
159 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
160 wol_enabled
= value
& AT803X_WOL_ENABLE
;
162 value
= phy_read(phydev
, MII_BMCR
);
165 value
|= BMCR_ISOLATE
;
169 phy_write(phydev
, MII_BMCR
, value
);
171 mutex_unlock(&phydev
->lock
);
176 static int at803x_resume(struct phy_device
*phydev
)
180 mutex_lock(&phydev
->lock
);
182 value
= phy_read(phydev
, MII_BMCR
);
183 value
&= ~(BMCR_PDOWN
| BMCR_ISOLATE
);
184 phy_write(phydev
, MII_BMCR
, value
);
186 mutex_unlock(&phydev
->lock
);
191 static int at803x_probe(struct phy_device
*phydev
)
193 struct device
*dev
= &phydev
->dev
;
194 struct at803x_priv
*priv
;
196 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
200 priv
->gpiod_reset
= devm_gpiod_get(dev
, "reset");
201 if (IS_ERR(priv
->gpiod_reset
))
202 priv
->gpiod_reset
= NULL
;
204 gpiod_direction_output(priv
->gpiod_reset
, 1);
211 static int at803x_config_init(struct phy_device
*phydev
)
215 ret
= genphy_config_init(phydev
);
219 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
) {
220 ret
= phy_write(phydev
, AT803X_DEBUG_ADDR
,
221 AT803X_DEBUG_SYSTEM_MODE_CTRL
);
224 ret
= phy_write(phydev
, AT803X_DEBUG_DATA
,
225 AT803X_DEBUG_RGMII_TX_CLK_DLY
);
233 static int at803x_ack_interrupt(struct phy_device
*phydev
)
237 err
= phy_read(phydev
, AT803X_INSR
);
239 return (err
< 0) ? err
: 0;
242 static int at803x_config_intr(struct phy_device
*phydev
)
247 value
= phy_read(phydev
, AT803X_INER
);
249 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
)
250 err
= phy_write(phydev
, AT803X_INER
,
251 value
| AT803X_INER_INIT
);
253 err
= phy_write(phydev
, AT803X_INER
, 0);
258 static void at803x_link_change_notify(struct phy_device
*phydev
)
260 struct at803x_priv
*priv
= phydev
->priv
;
263 * Conduct a hardware reset for AT8030 every time a link loss is
264 * signalled. This is necessary to circumvent a hardware bug that
265 * occurs when the cable is unplugged while TX packets are pending
266 * in the FIFO. In such cases, the FIFO enters an error mode it
267 * cannot recover from by software.
269 if (phydev
->drv
->phy_id
== ATH8030_PHY_ID
) {
270 if (phydev
->state
== PHY_NOLINK
) {
271 if (priv
->gpiod_reset
&& !priv
->phy_reset
) {
272 struct at803x_context context
;
274 at803x_context_save(phydev
, &context
);
276 gpiod_set_value(priv
->gpiod_reset
, 0);
278 gpiod_set_value(priv
->gpiod_reset
, 1);
281 at803x_context_restore(phydev
, &context
);
283 dev_dbg(&phydev
->dev
, "%s(): phy was reset\n",
285 priv
->phy_reset
= true;
288 priv
->phy_reset
= false;
293 static struct phy_driver at803x_driver
[] = {
296 .phy_id
= ATH8035_PHY_ID
,
297 .name
= "Atheros 8035 ethernet",
298 .phy_id_mask
= 0xffffffef,
299 .probe
= at803x_probe
,
300 .config_init
= at803x_config_init
,
301 .link_change_notify
= at803x_link_change_notify
,
302 .set_wol
= at803x_set_wol
,
303 .get_wol
= at803x_get_wol
,
304 .suspend
= at803x_suspend
,
305 .resume
= at803x_resume
,
306 .features
= PHY_GBIT_FEATURES
,
307 .flags
= PHY_HAS_INTERRUPT
,
308 .config_aneg
= genphy_config_aneg
,
309 .read_status
= genphy_read_status
,
311 .owner
= THIS_MODULE
,
315 .phy_id
= ATH8030_PHY_ID
,
316 .name
= "Atheros 8030 ethernet",
317 .phy_id_mask
= 0xffffffef,
318 .probe
= at803x_probe
,
319 .config_init
= at803x_config_init
,
320 .link_change_notify
= at803x_link_change_notify
,
321 .set_wol
= at803x_set_wol
,
322 .get_wol
= at803x_get_wol
,
323 .suspend
= at803x_suspend
,
324 .resume
= at803x_resume
,
325 .features
= PHY_GBIT_FEATURES
,
326 .flags
= PHY_HAS_INTERRUPT
,
327 .config_aneg
= genphy_config_aneg
,
328 .read_status
= genphy_read_status
,
330 .owner
= THIS_MODULE
,
334 .phy_id
= ATH8031_PHY_ID
,
335 .name
= "Atheros 8031 ethernet",
336 .phy_id_mask
= 0xffffffef,
337 .probe
= at803x_probe
,
338 .config_init
= at803x_config_init
,
339 .link_change_notify
= at803x_link_change_notify
,
340 .set_wol
= at803x_set_wol
,
341 .get_wol
= at803x_get_wol
,
342 .suspend
= at803x_suspend
,
343 .resume
= at803x_resume
,
344 .features
= PHY_GBIT_FEATURES
,
345 .flags
= PHY_HAS_INTERRUPT
,
346 .config_aneg
= genphy_config_aneg
,
347 .read_status
= genphy_read_status
,
348 .ack_interrupt
= &at803x_ack_interrupt
,
349 .config_intr
= &at803x_config_intr
,
351 .owner
= THIS_MODULE
,
355 static int __init
atheros_init(void)
357 return phy_drivers_register(at803x_driver
,
358 ARRAY_SIZE(at803x_driver
));
361 static void __exit
atheros_exit(void)
363 phy_drivers_unregister(at803x_driver
, ARRAY_SIZE(at803x_driver
));
366 module_init(atheros_init
);
367 module_exit(atheros_exit
);
369 static struct mdio_device_id __maybe_unused atheros_tbl
[] = {
370 { ATH8030_PHY_ID
, 0xffffffef },
371 { ATH8031_PHY_ID
, 0xffffffef },
372 { ATH8035_PHY_ID
, 0xffffffef },
376 MODULE_DEVICE_TABLE(mdio
, atheros_tbl
);