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
46 #define AT803X_DEBUG_ADDR 0x1D
47 #define AT803X_DEBUG_DATA 0x1E
49 #define AT803X_DEBUG_REG_0 0x00
50 #define AT803X_DEBUG_RX_CLK_DLY_EN BIT(15)
52 #define AT803X_DEBUG_REG_5 0x05
53 #define AT803X_DEBUG_TX_CLK_DLY_EN BIT(8)
55 #define AT803X_REG_CHIP_CONFIG 0x1f
56 #define AT803X_BT_BX_REG_SEL 0x8000
58 #define ATH8030_PHY_ID 0x004dd076
59 #define ATH8031_PHY_ID 0x004dd074
60 #define ATH8035_PHY_ID 0x004dd072
62 MODULE_DESCRIPTION("Atheros 803x PHY driver");
63 MODULE_AUTHOR("Matus Ujhelyi");
64 MODULE_LICENSE("GPL");
68 struct gpio_desc
*gpiod_reset
;
71 struct at803x_context
{
80 static int at803x_debug_reg_read(struct phy_device
*phydev
, u16 reg
)
84 ret
= phy_write(phydev
, AT803X_DEBUG_ADDR
, reg
);
88 return phy_read(phydev
, AT803X_DEBUG_DATA
);
91 static int at803x_debug_reg_mask(struct phy_device
*phydev
, u16 reg
,
97 ret
= at803x_debug_reg_read(phydev
, reg
);
105 return phy_write(phydev
, AT803X_DEBUG_DATA
, val
);
108 static inline int at803x_enable_rx_delay(struct phy_device
*phydev
)
110 return at803x_debug_reg_mask(phydev
, AT803X_DEBUG_REG_0
, 0,
111 AT803X_DEBUG_RX_CLK_DLY_EN
);
114 static inline int at803x_enable_tx_delay(struct phy_device
*phydev
)
116 return at803x_debug_reg_mask(phydev
, AT803X_DEBUG_REG_5
, 0,
117 AT803X_DEBUG_TX_CLK_DLY_EN
);
120 /* save relevant PHY registers to private copy */
121 static void at803x_context_save(struct phy_device
*phydev
,
122 struct at803x_context
*context
)
124 context
->bmcr
= phy_read(phydev
, MII_BMCR
);
125 context
->advertise
= phy_read(phydev
, MII_ADVERTISE
);
126 context
->control1000
= phy_read(phydev
, MII_CTRL1000
);
127 context
->int_enable
= phy_read(phydev
, AT803X_INTR_ENABLE
);
128 context
->smart_speed
= phy_read(phydev
, AT803X_SMART_SPEED
);
129 context
->led_control
= phy_read(phydev
, AT803X_LED_CONTROL
);
132 /* restore relevant PHY registers from private copy */
133 static void at803x_context_restore(struct phy_device
*phydev
,
134 const struct at803x_context
*context
)
136 phy_write(phydev
, MII_BMCR
, context
->bmcr
);
137 phy_write(phydev
, MII_ADVERTISE
, context
->advertise
);
138 phy_write(phydev
, MII_CTRL1000
, context
->control1000
);
139 phy_write(phydev
, AT803X_INTR_ENABLE
, context
->int_enable
);
140 phy_write(phydev
, AT803X_SMART_SPEED
, context
->smart_speed
);
141 phy_write(phydev
, AT803X_LED_CONTROL
, context
->led_control
);
144 static int at803x_set_wol(struct phy_device
*phydev
,
145 struct ethtool_wolinfo
*wol
)
147 struct net_device
*ndev
= phydev
->attached_dev
;
151 unsigned int i
, offsets
[] = {
152 AT803X_LOC_MAC_ADDR_32_47_OFFSET
,
153 AT803X_LOC_MAC_ADDR_16_31_OFFSET
,
154 AT803X_LOC_MAC_ADDR_0_15_OFFSET
,
160 if (wol
->wolopts
& WAKE_MAGIC
) {
161 mac
= (const u8
*) ndev
->dev_addr
;
163 if (!is_valid_ether_addr(mac
))
166 for (i
= 0; i
< 3; i
++) {
167 phy_write(phydev
, AT803X_MMD_ACCESS_CONTROL
,
169 phy_write(phydev
, AT803X_MMD_ACCESS_CONTROL_DATA
,
171 phy_write(phydev
, AT803X_MMD_ACCESS_CONTROL
,
173 phy_write(phydev
, AT803X_MMD_ACCESS_CONTROL_DATA
,
174 mac
[(i
* 2) + 1] | (mac
[(i
* 2)] << 8));
177 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
178 value
|= AT803X_INTR_ENABLE_WOL
;
179 ret
= phy_write(phydev
, AT803X_INTR_ENABLE
, value
);
182 value
= phy_read(phydev
, AT803X_INTR_STATUS
);
184 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
185 value
&= (~AT803X_INTR_ENABLE_WOL
);
186 ret
= phy_write(phydev
, AT803X_INTR_ENABLE
, value
);
189 value
= phy_read(phydev
, AT803X_INTR_STATUS
);
195 static void at803x_get_wol(struct phy_device
*phydev
,
196 struct ethtool_wolinfo
*wol
)
200 wol
->supported
= WAKE_MAGIC
;
203 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
204 if (value
& AT803X_INTR_ENABLE_WOL
)
205 wol
->wolopts
|= WAKE_MAGIC
;
208 static int at803x_suspend(struct phy_device
*phydev
)
214 mutex_lock(&phydev
->lock
);
216 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
217 wol_enabled
= value
& AT803X_INTR_ENABLE_WOL
;
219 value
= phy_read(phydev
, MII_BMCR
);
222 value
|= BMCR_ISOLATE
;
226 phy_write(phydev
, MII_BMCR
, value
);
228 if (phydev
->interface
!= PHY_INTERFACE_MODE_SGMII
)
231 /* also power-down SGMII interface */
232 ccr
= phy_read(phydev
, AT803X_REG_CHIP_CONFIG
);
233 phy_write(phydev
, AT803X_REG_CHIP_CONFIG
, ccr
& ~AT803X_BT_BX_REG_SEL
);
234 phy_write(phydev
, MII_BMCR
, phy_read(phydev
, MII_BMCR
) | BMCR_PDOWN
);
235 phy_write(phydev
, AT803X_REG_CHIP_CONFIG
, ccr
| AT803X_BT_BX_REG_SEL
);
238 mutex_unlock(&phydev
->lock
);
243 static int at803x_resume(struct phy_device
*phydev
)
248 mutex_lock(&phydev
->lock
);
250 value
= phy_read(phydev
, MII_BMCR
);
251 value
&= ~(BMCR_PDOWN
| BMCR_ISOLATE
);
252 phy_write(phydev
, MII_BMCR
, value
);
254 if (phydev
->interface
!= PHY_INTERFACE_MODE_SGMII
)
257 /* also power-up SGMII interface */
258 ccr
= phy_read(phydev
, AT803X_REG_CHIP_CONFIG
);
259 phy_write(phydev
, AT803X_REG_CHIP_CONFIG
, ccr
& ~AT803X_BT_BX_REG_SEL
);
260 value
= phy_read(phydev
, MII_BMCR
) & ~(BMCR_PDOWN
| BMCR_ISOLATE
);
261 phy_write(phydev
, MII_BMCR
, value
);
262 phy_write(phydev
, AT803X_REG_CHIP_CONFIG
, ccr
| AT803X_BT_BX_REG_SEL
);
265 mutex_unlock(&phydev
->lock
);
270 static int at803x_probe(struct phy_device
*phydev
)
272 struct device
*dev
= &phydev
->mdio
.dev
;
273 struct at803x_priv
*priv
;
274 struct gpio_desc
*gpiod_reset
;
276 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
280 if (phydev
->drv
->phy_id
!= ATH8030_PHY_ID
)
281 goto does_not_require_reset_workaround
;
283 gpiod_reset
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_LOW
);
284 if (IS_ERR(gpiod_reset
))
285 return PTR_ERR(gpiod_reset
);
287 priv
->gpiod_reset
= gpiod_reset
;
289 does_not_require_reset_workaround
:
295 static int at803x_config_init(struct phy_device
*phydev
)
299 ret
= genphy_config_init(phydev
);
303 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
||
304 phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
) {
305 ret
= at803x_enable_rx_delay(phydev
);
310 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
||
311 phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
) {
312 ret
= at803x_enable_tx_delay(phydev
);
320 static int at803x_ack_interrupt(struct phy_device
*phydev
)
324 err
= phy_read(phydev
, AT803X_INTR_STATUS
);
326 return (err
< 0) ? err
: 0;
329 static int at803x_config_intr(struct phy_device
*phydev
)
334 value
= phy_read(phydev
, AT803X_INTR_ENABLE
);
336 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
) {
337 value
|= AT803X_INTR_ENABLE_AUTONEG_ERR
;
338 value
|= AT803X_INTR_ENABLE_SPEED_CHANGED
;
339 value
|= AT803X_INTR_ENABLE_DUPLEX_CHANGED
;
340 value
|= AT803X_INTR_ENABLE_LINK_FAIL
;
341 value
|= AT803X_INTR_ENABLE_LINK_SUCCESS
;
343 err
= phy_write(phydev
, AT803X_INTR_ENABLE
, value
);
346 err
= phy_write(phydev
, AT803X_INTR_ENABLE
, 0);
351 static void at803x_link_change_notify(struct phy_device
*phydev
)
353 struct at803x_priv
*priv
= phydev
->priv
;
356 * Conduct a hardware reset for AT8030 every time a link loss is
357 * signalled. This is necessary to circumvent a hardware bug that
358 * occurs when the cable is unplugged while TX packets are pending
359 * in the FIFO. In such cases, the FIFO enters an error mode it
360 * cannot recover from by software.
362 if (phydev
->drv
->phy_id
== ATH8030_PHY_ID
) {
363 if (phydev
->state
== PHY_NOLINK
) {
364 if (priv
->gpiod_reset
&& !priv
->phy_reset
) {
365 struct at803x_context context
;
367 at803x_context_save(phydev
, &context
);
369 gpiod_set_value(priv
->gpiod_reset
, 1);
371 gpiod_set_value(priv
->gpiod_reset
, 0);
374 at803x_context_restore(phydev
, &context
);
376 phydev_dbg(phydev
, "%s(): phy was reset\n",
378 priv
->phy_reset
= true;
381 priv
->phy_reset
= false;
386 static struct phy_driver at803x_driver
[] = {
389 .phy_id
= ATH8035_PHY_ID
,
390 .name
= "Atheros 8035 ethernet",
391 .phy_id_mask
= 0xffffffef,
392 .probe
= at803x_probe
,
393 .config_init
= at803x_config_init
,
394 .link_change_notify
= at803x_link_change_notify
,
395 .set_wol
= at803x_set_wol
,
396 .get_wol
= at803x_get_wol
,
397 .suspend
= at803x_suspend
,
398 .resume
= at803x_resume
,
399 .features
= PHY_GBIT_FEATURES
,
400 .flags
= PHY_HAS_INTERRUPT
,
401 .config_aneg
= genphy_config_aneg
,
402 .read_status
= genphy_read_status
,
403 .ack_interrupt
= at803x_ack_interrupt
,
404 .config_intr
= at803x_config_intr
,
407 .phy_id
= ATH8030_PHY_ID
,
408 .name
= "Atheros 8030 ethernet",
409 .phy_id_mask
= 0xffffffef,
410 .probe
= at803x_probe
,
411 .config_init
= at803x_config_init
,
412 .link_change_notify
= at803x_link_change_notify
,
413 .set_wol
= at803x_set_wol
,
414 .get_wol
= at803x_get_wol
,
415 .suspend
= at803x_suspend
,
416 .resume
= at803x_resume
,
417 .features
= PHY_BASIC_FEATURES
,
418 .flags
= PHY_HAS_INTERRUPT
,
419 .config_aneg
= genphy_config_aneg
,
420 .read_status
= genphy_read_status
,
421 .ack_interrupt
= at803x_ack_interrupt
,
422 .config_intr
= at803x_config_intr
,
425 .phy_id
= ATH8031_PHY_ID
,
426 .name
= "Atheros 8031 ethernet",
427 .phy_id_mask
= 0xffffffef,
428 .probe
= at803x_probe
,
429 .config_init
= at803x_config_init
,
430 .link_change_notify
= at803x_link_change_notify
,
431 .set_wol
= at803x_set_wol
,
432 .get_wol
= at803x_get_wol
,
433 .suspend
= at803x_suspend
,
434 .resume
= at803x_resume
,
435 .features
= PHY_GBIT_FEATURES
,
436 .flags
= PHY_HAS_INTERRUPT
,
437 .config_aneg
= genphy_config_aneg
,
438 .read_status
= genphy_read_status
,
439 .ack_interrupt
= &at803x_ack_interrupt
,
440 .config_intr
= &at803x_config_intr
,
443 module_phy_driver(at803x_driver
);
445 static struct mdio_device_id __maybe_unused atheros_tbl
[] = {
446 { ATH8030_PHY_ID
, 0xffffffef },
447 { ATH8031_PHY_ID
, 0xffffffef },
448 { ATH8035_PHY_ID
, 0xffffffef },
452 MODULE_DEVICE_TABLE(mdio
, atheros_tbl
);