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_INIT 0xec00
24 #define AT803X_INTR_STATUS 0x13
26 #define AT803X_SMART_SPEED 0x14
27 #define AT803X_LED_CONTROL 0x18
29 #define AT803X_WOL_ENABLE 0x01
30 #define AT803X_DEVICE_ADDR 0x03
31 #define AT803X_LOC_MAC_ADDR_0_15_OFFSET 0x804C
32 #define AT803X_LOC_MAC_ADDR_16_31_OFFSET 0x804B
33 #define AT803X_LOC_MAC_ADDR_32_47_OFFSET 0x804A
34 #define AT803X_MMD_ACCESS_CONTROL 0x0D
35 #define AT803X_MMD_ACCESS_CONTROL_DATA 0x0E
36 #define AT803X_FUNC_DATA 0x4003
38 #define AT803X_DEBUG_ADDR 0x1D
39 #define AT803X_DEBUG_DATA 0x1E
41 #define AT803X_DEBUG_REG_0 0x00
42 #define AT803X_DEBUG_RX_CLK_DLY_EN BIT(15)
44 #define AT803X_DEBUG_REG_5 0x05
45 #define AT803X_DEBUG_TX_CLK_DLY_EN BIT(8)
47 #define ATH8030_PHY_ID 0x004dd076
48 #define ATH8031_PHY_ID 0x004dd074
49 #define ATH8035_PHY_ID 0x004dd072
51 MODULE_DESCRIPTION("Atheros 803x PHY driver");
52 MODULE_AUTHOR("Matus Ujhelyi");
53 MODULE_LICENSE("GPL");
57 struct gpio_desc
*gpiod_reset
;
60 struct at803x_context
{
69 static int at803x_debug_reg_read(struct phy_device
*phydev
, u16 reg
)
73 ret
= phy_write(phydev
, AT803X_DEBUG_ADDR
, reg
);
77 return phy_read(phydev
, AT803X_DEBUG_DATA
);
80 static int at803x_debug_reg_mask(struct phy_device
*phydev
, u16 reg
,
86 ret
= at803x_debug_reg_read(phydev
, reg
);
94 return phy_write(phydev
, AT803X_DEBUG_DATA
, val
);
97 static inline int at803x_enable_rx_delay(struct phy_device
*phydev
)
99 return at803x_debug_reg_mask(phydev
, AT803X_DEBUG_REG_0
, 0,
100 AT803X_DEBUG_RX_CLK_DLY_EN
);
103 static inline int at803x_enable_tx_delay(struct phy_device
*phydev
)
105 return at803x_debug_reg_mask(phydev
, AT803X_DEBUG_REG_5
, 0,
106 AT803X_DEBUG_TX_CLK_DLY_EN
);
109 /* save relevant PHY registers to private copy */
110 static void at803x_context_save(struct phy_device
*phydev
,
111 struct at803x_context
*context
)
113 context
->bmcr
= phy_read(phydev
, MII_BMCR
);
114 context
->advertise
= phy_read(phydev
, MII_ADVERTISE
);
115 context
->control1000
= phy_read(phydev
, MII_CTRL1000
);
116 context
->int_enable
= phy_read(phydev
, AT803X_INTR_ENABLE
);
117 context
->smart_speed
= phy_read(phydev
, AT803X_SMART_SPEED
);
118 context
->led_control
= phy_read(phydev
, AT803X_LED_CONTROL
);
121 /* restore relevant PHY registers from private copy */
122 static void at803x_context_restore(struct phy_device
*phydev
,
123 const struct at803x_context
*context
)
125 phy_write(phydev
, MII_BMCR
, context
->bmcr
);
126 phy_write(phydev
, MII_ADVERTISE
, context
->advertise
);
127 phy_write(phydev
, MII_CTRL1000
, context
->control1000
);
128 phy_write(phydev
, AT803X_INTR_ENABLE
, context
->int_enable
);
129 phy_write(phydev
, AT803X_SMART_SPEED
, context
->smart_speed
);
130 phy_write(phydev
, AT803X_LED_CONTROL
, context
->led_control
);
133 static int at803x_set_wol(struct phy_device
*phydev
,
134 struct ethtool_wolinfo
*wol
)
136 struct net_device
*ndev
= phydev
->attached_dev
;
140 unsigned int i
, offsets
[] = {
141 AT803X_LOC_MAC_ADDR_32_47_OFFSET
,
142 AT803X_LOC_MAC_ADDR_16_31_OFFSET
,
143 AT803X_LOC_MAC_ADDR_0_15_OFFSET
,
149 if (wol
->wolopts
& WAKE_MAGIC
) {
150 mac
= (const u8
*) ndev
->dev_addr
;
152 if (!is_valid_ether_addr(mac
))
155 for (i
= 0; i
< 3; i
++) {
156 phy_write(phydev
, AT803X_MMD_ACCESS_CONTROL
,
158 phy_write(phydev
, AT803X_MMD_ACCESS_CONTROL_DATA
,
160 phy_write(phydev
, AT803X_MMD_ACCESS_CONTROL
,
162 phy_write(phydev
, AT803X_MMD_ACCESS_CONTROL_DATA
,
163 mac
[(i
* 2) + 1] | (mac
[(i
* 2)] << 8));
166 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
167 value
|= AT803X_WOL_ENABLE
;
168 ret
= phy_write(phydev
, AT803X_INTR_ENABLE
, value
);
171 value
= phy_read(phydev
, AT803X_INTR_STATUS
);
173 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
174 value
&= (~AT803X_WOL_ENABLE
);
175 ret
= phy_write(phydev
, AT803X_INTR_ENABLE
, value
);
178 value
= phy_read(phydev
, AT803X_INTR_STATUS
);
184 static void at803x_get_wol(struct phy_device
*phydev
,
185 struct ethtool_wolinfo
*wol
)
189 wol
->supported
= WAKE_MAGIC
;
192 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
193 if (value
& AT803X_WOL_ENABLE
)
194 wol
->wolopts
|= WAKE_MAGIC
;
197 static int at803x_suspend(struct phy_device
*phydev
)
202 mutex_lock(&phydev
->lock
);
204 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
205 wol_enabled
= value
& AT803X_WOL_ENABLE
;
207 value
= phy_read(phydev
, MII_BMCR
);
210 value
|= BMCR_ISOLATE
;
214 phy_write(phydev
, MII_BMCR
, value
);
216 mutex_unlock(&phydev
->lock
);
221 static int at803x_resume(struct phy_device
*phydev
)
225 mutex_lock(&phydev
->lock
);
227 value
= phy_read(phydev
, MII_BMCR
);
228 value
&= ~(BMCR_PDOWN
| BMCR_ISOLATE
);
229 phy_write(phydev
, MII_BMCR
, value
);
231 mutex_unlock(&phydev
->lock
);
236 static int at803x_probe(struct phy_device
*phydev
)
238 struct device
*dev
= &phydev
->mdio
.dev
;
239 struct at803x_priv
*priv
;
240 struct gpio_desc
*gpiod_reset
;
242 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
246 gpiod_reset
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_HIGH
);
247 if (IS_ERR(gpiod_reset
))
248 return PTR_ERR(gpiod_reset
);
250 priv
->gpiod_reset
= gpiod_reset
;
257 static int at803x_config_init(struct phy_device
*phydev
)
261 ret
= genphy_config_init(phydev
);
265 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
||
266 phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
) {
267 ret
= at803x_enable_rx_delay(phydev
);
272 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
||
273 phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
) {
274 ret
= at803x_enable_tx_delay(phydev
);
282 static int at803x_ack_interrupt(struct phy_device
*phydev
)
286 err
= phy_read(phydev
, AT803X_INTR_STATUS
);
288 return (err
< 0) ? err
: 0;
291 static int at803x_config_intr(struct phy_device
*phydev
)
296 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
298 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
)
299 err
= phy_write(phydev
, AT803X_INTR_ENABLE
,
300 value
| AT803X_INTR_ENABLE_INIT
);
302 err
= phy_write(phydev
, AT803X_INTR_ENABLE
, 0);
307 static void at803x_link_change_notify(struct phy_device
*phydev
)
309 struct at803x_priv
*priv
= phydev
->priv
;
312 * Conduct a hardware reset for AT8030 every time a link loss is
313 * signalled. This is necessary to circumvent a hardware bug that
314 * occurs when the cable is unplugged while TX packets are pending
315 * in the FIFO. In such cases, the FIFO enters an error mode it
316 * cannot recover from by software.
318 if (phydev
->drv
->phy_id
== ATH8030_PHY_ID
) {
319 if (phydev
->state
== PHY_NOLINK
) {
320 if (priv
->gpiod_reset
&& !priv
->phy_reset
) {
321 struct at803x_context context
;
323 at803x_context_save(phydev
, &context
);
325 gpiod_set_value(priv
->gpiod_reset
, 0);
327 gpiod_set_value(priv
->gpiod_reset
, 1);
330 at803x_context_restore(phydev
, &context
);
332 phydev_dbg(phydev
, "%s(): phy was reset\n",
334 priv
->phy_reset
= true;
337 priv
->phy_reset
= false;
342 static struct phy_driver at803x_driver
[] = {
345 .phy_id
= ATH8035_PHY_ID
,
346 .name
= "Atheros 8035 ethernet",
347 .phy_id_mask
= 0xffffffef,
348 .probe
= at803x_probe
,
349 .config_init
= at803x_config_init
,
350 .link_change_notify
= at803x_link_change_notify
,
351 .set_wol
= at803x_set_wol
,
352 .get_wol
= at803x_get_wol
,
353 .suspend
= at803x_suspend
,
354 .resume
= at803x_resume
,
355 .features
= PHY_GBIT_FEATURES
,
356 .flags
= PHY_HAS_INTERRUPT
,
357 .config_aneg
= genphy_config_aneg
,
358 .read_status
= genphy_read_status
,
359 .ack_interrupt
= at803x_ack_interrupt
,
360 .config_intr
= at803x_config_intr
,
363 .phy_id
= ATH8030_PHY_ID
,
364 .name
= "Atheros 8030 ethernet",
365 .phy_id_mask
= 0xffffffef,
366 .probe
= at803x_probe
,
367 .config_init
= at803x_config_init
,
368 .link_change_notify
= at803x_link_change_notify
,
369 .set_wol
= at803x_set_wol
,
370 .get_wol
= at803x_get_wol
,
371 .suspend
= at803x_suspend
,
372 .resume
= at803x_resume
,
373 .features
= PHY_BASIC_FEATURES
,
374 .flags
= PHY_HAS_INTERRUPT
,
375 .config_aneg
= genphy_config_aneg
,
376 .read_status
= genphy_read_status
,
377 .ack_interrupt
= at803x_ack_interrupt
,
378 .config_intr
= at803x_config_intr
,
381 .phy_id
= ATH8031_PHY_ID
,
382 .name
= "Atheros 8031 ethernet",
383 .phy_id_mask
= 0xffffffef,
384 .probe
= at803x_probe
,
385 .config_init
= at803x_config_init
,
386 .link_change_notify
= at803x_link_change_notify
,
387 .set_wol
= at803x_set_wol
,
388 .get_wol
= at803x_get_wol
,
389 .suspend
= at803x_suspend
,
390 .resume
= at803x_resume
,
391 .features
= PHY_GBIT_FEATURES
,
392 .flags
= PHY_HAS_INTERRUPT
,
393 .config_aneg
= genphy_config_aneg
,
394 .read_status
= genphy_read_status
,
395 .ack_interrupt
= &at803x_ack_interrupt
,
396 .config_intr
= &at803x_config_intr
,
399 module_phy_driver(at803x_driver
);
401 static struct mdio_device_id __maybe_unused atheros_tbl
[] = {
402 { ATH8030_PHY_ID
, 0xffffffef },
403 { ATH8031_PHY_ID
, 0xffffffef },
404 { ATH8035_PHY_ID
, 0xffffffef },
408 MODULE_DEVICE_TABLE(mdio
, atheros_tbl
);