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 ATH8030_PHY_ID 0x004dd076
56 #define ATH8031_PHY_ID 0x004dd074
57 #define ATH8035_PHY_ID 0x004dd072
59 MODULE_DESCRIPTION("Atheros 803x PHY driver");
60 MODULE_AUTHOR("Matus Ujhelyi");
61 MODULE_LICENSE("GPL");
65 struct gpio_desc *gpiod_reset;
68 struct at803x_context {
77 static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg)
81 ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
85 return phy_read(phydev, AT803X_DEBUG_DATA);
88 static int at803x_debug_reg_mask(struct phy_device *phydev, u16 reg,
94 ret = at803x_debug_reg_read(phydev, reg);
102 return phy_write(phydev, AT803X_DEBUG_DATA, val);
105 static inline int at803x_enable_rx_delay(struct phy_device *phydev)
107 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_0, 0,
108 AT803X_DEBUG_RX_CLK_DLY_EN);
111 static inline int at803x_enable_tx_delay(struct phy_device *phydev)
113 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_5, 0,
114 AT803X_DEBUG_TX_CLK_DLY_EN);
117 /* save relevant PHY registers to private copy */
118 static void at803x_context_save(struct phy_device *phydev,
119 struct at803x_context *context)
121 context->bmcr = phy_read(phydev, MII_BMCR);
122 context->advertise = phy_read(phydev, MII_ADVERTISE);
123 context->control1000 = phy_read(phydev, MII_CTRL1000);
124 context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE);
125 context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED);
126 context->led_control = phy_read(phydev, AT803X_LED_CONTROL);
129 /* restore relevant PHY registers from private copy */
130 static void at803x_context_restore(struct phy_device *phydev,
131 const struct at803x_context *context)
133 phy_write(phydev, MII_BMCR, context->bmcr);
134 phy_write(phydev, MII_ADVERTISE, context->advertise);
135 phy_write(phydev, MII_CTRL1000, context->control1000);
136 phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable);
137 phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed);
138 phy_write(phydev, AT803X_LED_CONTROL, context->led_control);
141 static int at803x_set_wol(struct phy_device *phydev,
142 struct ethtool_wolinfo *wol)
144 struct net_device *ndev = phydev->attached_dev;
148 unsigned int i, offsets[] = {
149 AT803X_LOC_MAC_ADDR_32_47_OFFSET,
150 AT803X_LOC_MAC_ADDR_16_31_OFFSET,
151 AT803X_LOC_MAC_ADDR_0_15_OFFSET,
157 if (wol->wolopts & WAKE_MAGIC) {
158 mac = (const u8 *) ndev->dev_addr;
160 if (!is_valid_ether_addr(mac))
163 for (i = 0; i < 3; i++) {
164 phy_write(phydev, AT803X_MMD_ACCESS_CONTROL,
166 phy_write(phydev, AT803X_MMD_ACCESS_CONTROL_DATA,
168 phy_write(phydev, AT803X_MMD_ACCESS_CONTROL,
170 phy_write(phydev, AT803X_MMD_ACCESS_CONTROL_DATA,
171 mac[(i * 2) + 1] | (mac[(i * 2)] << 8));
174 value = phy_read(phydev, AT803X_INTR_ENABLE);
175 value |= AT803X_INTR_ENABLE_WOL;
176 ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
179 value = phy_read(phydev, AT803X_INTR_STATUS);
181 value = phy_read(phydev, AT803X_INTR_ENABLE);
182 value &= (~AT803X_INTR_ENABLE_WOL);
183 ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
186 value = phy_read(phydev, AT803X_INTR_STATUS);
192 static void at803x_get_wol(struct phy_device *phydev,
193 struct ethtool_wolinfo *wol)
197 wol->supported = WAKE_MAGIC;
200 value = phy_read(phydev, AT803X_INTR_ENABLE);
201 if (value & AT803X_INTR_ENABLE_WOL)
202 wol->wolopts |= WAKE_MAGIC;
205 static int at803x_suspend(struct phy_device *phydev)
210 mutex_lock(&phydev->lock);
212 value = phy_read(phydev, AT803X_INTR_ENABLE);
213 wol_enabled = value & AT803X_INTR_ENABLE_WOL;
215 value = phy_read(phydev, MII_BMCR);
218 value |= BMCR_ISOLATE;
222 phy_write(phydev, MII_BMCR, value);
224 mutex_unlock(&phydev->lock);
229 static int at803x_resume(struct phy_device *phydev)
233 mutex_lock(&phydev->lock);
235 value = phy_read(phydev, MII_BMCR);
236 value &= ~(BMCR_PDOWN | BMCR_ISOLATE);
237 phy_write(phydev, MII_BMCR, value);
239 mutex_unlock(&phydev->lock);
244 static int at803x_probe(struct phy_device *phydev)
246 struct device *dev = &phydev->mdio.dev;
247 struct at803x_priv *priv;
248 struct gpio_desc *gpiod_reset;
250 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
254 if (phydev->drv->phy_id != ATH8030_PHY_ID)
255 goto does_not_require_reset_workaround;
257 gpiod_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
258 if (IS_ERR(gpiod_reset))
259 return PTR_ERR(gpiod_reset);
261 priv->gpiod_reset = gpiod_reset;
263 does_not_require_reset_workaround:
269 static int at803x_config_init(struct phy_device *phydev)
273 ret = genphy_config_init(phydev);
277 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID ||
278 phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) {
279 ret = at803x_enable_rx_delay(phydev);
284 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID ||
285 phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) {
286 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 (priv->gpiod_reset && !priv->phy_reset) {
338 struct at803x_context context;
340 at803x_context_save(phydev, &context);
342 gpiod_set_value(priv->gpiod_reset, 1);
344 gpiod_set_value(priv->gpiod_reset, 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 struct phy_driver at803x_driver[] = {
361 .phy_id = ATH8035_PHY_ID,
362 .name = "Atheros 8035 ethernet",
363 .phy_id_mask = 0xffffffef,
364 .probe = at803x_probe,
365 .config_init = at803x_config_init,
366 .set_wol = at803x_set_wol,
367 .get_wol = at803x_get_wol,
368 .suspend = at803x_suspend,
369 .resume = at803x_resume,
370 .features = PHY_GBIT_FEATURES,
371 .flags = PHY_HAS_INTERRUPT,
372 .config_aneg = genphy_config_aneg,
373 .read_status = genphy_read_status,
374 .ack_interrupt = at803x_ack_interrupt,
375 .config_intr = at803x_config_intr,
378 .phy_id = ATH8030_PHY_ID,
379 .name = "Atheros 8030 ethernet",
380 .phy_id_mask = 0xffffffef,
381 .probe = at803x_probe,
382 .config_init = at803x_config_init,
383 .link_change_notify = at803x_link_change_notify,
384 .set_wol = at803x_set_wol,
385 .get_wol = at803x_get_wol,
386 .suspend = at803x_suspend,
387 .resume = at803x_resume,
388 .features = PHY_BASIC_FEATURES,
389 .flags = PHY_HAS_INTERRUPT,
390 .config_aneg = genphy_config_aneg,
391 .read_status = genphy_read_status,
392 .ack_interrupt = at803x_ack_interrupt,
393 .config_intr = at803x_config_intr,
396 .phy_id = ATH8031_PHY_ID,
397 .name = "Atheros 8031 ethernet",
398 .phy_id_mask = 0xffffffef,
399 .probe = at803x_probe,
400 .config_init = at803x_config_init,
401 .set_wol = at803x_set_wol,
402 .get_wol = at803x_get_wol,
403 .suspend = at803x_suspend,
404 .resume = at803x_resume,
405 .features = PHY_GBIT_FEATURES,
406 .flags = PHY_HAS_INTERRUPT,
407 .config_aneg = genphy_config_aneg,
408 .read_status = genphy_read_status,
409 .ack_interrupt = &at803x_ack_interrupt,
410 .config_intr = &at803x_config_intr,
413 module_phy_driver(at803x_driver);
415 static struct mdio_device_id __maybe_unused atheros_tbl[] = {
416 { ATH8030_PHY_ID, 0xffffffef },
417 { ATH8031_PHY_ID, 0xffffffef },
418 { ATH8035_PHY_ID, 0xffffffef },
422 MODULE_DEVICE_TABLE(mdio, atheros_tbl);