blob: d810f914aaa4d9928f43cd54bbcd447f9c00bcc7 [file] [log] [blame]
Andrew Lunna2443fd2019-01-21 19:05:50 +01001// SPDX-License-Identifier: GPL-2.0+
Vitaly Bordug11b0bac2006-08-14 23:00:29 -07002/*
Vitaly Borduga79d8e92007-12-07 01:51:22 +03003 * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -07004 *
Vitaly Borduga79d8e92007-12-07 01:51:22 +03005 * Author: Vitaly Bordug <vbordug@ru.mvista.com>
6 * Anton Vorontsov <avorontsov@ru.mvista.com>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -07007 *
Vitaly Borduga79d8e92007-12-07 01:51:22 +03008 * Copyright (c) 2006-2007 MontaVista Software, Inc.
Vitaly Bordug11b0bac2006-08-14 23:00:29 -07009 */
Vitaly Borduga79d8e92007-12-07 01:51:22 +030010
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070011#include <linux/kernel.h>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070012#include <linux/module.h>
Vitaly Borduga79d8e92007-12-07 01:51:22 +030013#include <linux/platform_device.h>
14#include <linux/list.h>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070015#include <linux/mii.h>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070016#include <linux/phy.h>
Vitaly Bordug7c32f472007-08-10 14:05:16 -070017#include <linux/phy_fixed.h>
Dan Carpenter57401d52009-04-11 01:52:29 -070018#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Thomas Petazzonia7595122014-05-16 16:14:04 +020020#include <linux/of.h>
Linus Walleij5468e822019-02-04 11:26:18 +010021#include <linux/gpio/consumer.h>
Russell Kingbf7afb22016-06-23 14:50:25 +010022#include <linux/seqlock.h>
Florian Fainelli69fc58a2016-06-24 16:25:24 -070023#include <linux/idr.h>
Joakim Tjernlundb3e54642018-12-14 15:17:05 +010024#include <linux/netdevice.h>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070025
Russell King5ae68b02016-06-23 14:50:05 +010026#include "swphy.h"
27
Vitaly Borduga79d8e92007-12-07 01:51:22 +030028struct fixed_mdio_bus {
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070029 struct mii_bus *mii_bus;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030030 struct list_head phys;
31};
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070032
Vitaly Borduga79d8e92007-12-07 01:51:22 +030033struct fixed_phy {
Thomas Petazzoni9b744942014-05-16 16:14:03 +020034 int addr;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030035 struct phy_device *phydev;
Russell Kingbf7afb22016-06-23 14:50:25 +010036 seqcount_t seqcount;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030037 struct fixed_phy_status status;
Joakim Tjernlundb3e54642018-12-14 15:17:05 +010038 bool no_carrier;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030039 int (*link_update)(struct net_device *, struct fixed_phy_status *);
40 struct list_head node;
Linus Walleij5468e822019-02-04 11:26:18 +010041 struct gpio_desc *link_gpiod;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030042};
43
44static struct platform_device *pdev;
45static struct fixed_mdio_bus platform_fmb = {
46 .phys = LIST_HEAD_INIT(platform_fmb.phys),
47};
48
Joakim Tjernlundb3e54642018-12-14 15:17:05 +010049int fixed_phy_change_carrier(struct net_device *dev, bool new_carrier)
50{
51 struct fixed_mdio_bus *fmb = &platform_fmb;
52 struct phy_device *phydev = dev->phydev;
53 struct fixed_phy *fp;
54
55 if (!phydev || !phydev->mdio.bus)
56 return -EINVAL;
57
58 list_for_each_entry(fp, &fmb->phys, node) {
59 if (fp->addr == phydev->mdio.addr) {
60 fp->no_carrier = !new_carrier;
61 return 0;
62 }
63 }
64 return -EINVAL;
65}
66EXPORT_SYMBOL_GPL(fixed_phy_change_carrier);
67
Russell King37688e32016-06-23 14:50:20 +010068static void fixed_phy_update(struct fixed_phy *fp)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070069{
Linus Walleij5468e822019-02-04 11:26:18 +010070 if (!fp->no_carrier && fp->link_gpiod)
71 fp->status.link = !!gpiod_get_value_cansleep(fp->link_gpiod);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070072}
73
Thomas Petazzoni9b744942014-05-16 16:14:03 +020074static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070075{
Lennert Buytenhekec2a5652008-10-09 09:45:04 -070076 struct fixed_mdio_bus *fmb = bus->priv;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030077 struct fixed_phy *fp;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070078
Vitaly Borduga79d8e92007-12-07 01:51:22 +030079 list_for_each_entry(fp, &fmb->phys, node) {
Thomas Petazzoni9b744942014-05-16 16:14:03 +020080 if (fp->addr == phy_addr) {
Russell Kingbf7afb22016-06-23 14:50:25 +010081 struct fixed_phy_status state;
82 int s;
83
84 do {
85 s = read_seqcount_begin(&fp->seqcount);
Joakim Tjernlundb3e54642018-12-14 15:17:05 +010086 fp->status.link = !fp->no_carrier;
Russell Kingbf7afb22016-06-23 14:50:25 +010087 /* Issue callback if user registered it. */
Moritz Fischer8f289802019-02-06 21:45:29 -080088 if (fp->link_update)
Russell Kingbf7afb22016-06-23 14:50:25 +010089 fp->link_update(fp->phydev->attached_dev,
90 &fp->status);
Moritz Fischer8f289802019-02-06 21:45:29 -080091 /* Check the GPIO for change in status */
92 fixed_phy_update(fp);
Russell Kingbf7afb22016-06-23 14:50:25 +010093 state = fp->status;
94 } while (read_seqcount_retry(&fp->seqcount, s));
95
96 return swphy_read_reg(reg_num, &state);
Vitaly Borduga79d8e92007-12-07 01:51:22 +030097 }
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070098 }
99
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300100 return 0xFFFF;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700101}
102
Thomas Petazzoni9b744942014-05-16 16:14:03 +0200103static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300104 u16 val)
105{
106 return 0;
107}
108
109/*
110 * If something weird is required to be done with link/speed,
111 * network driver is able to assign a function to implement this.
112 * May be useful for PHY's that need to be software-driven.
113 */
114int fixed_phy_set_link_update(struct phy_device *phydev,
115 int (*link_update)(struct net_device *,
116 struct fixed_phy_status *))
117{
118 struct fixed_mdio_bus *fmb = &platform_fmb;
119 struct fixed_phy *fp;
120
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100121 if (!phydev || !phydev->mdio.bus)
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300122 return -EINVAL;
123
124 list_for_each_entry(fp, &fmb->phys, node) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100125 if (fp->addr == phydev->mdio.addr) {
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300126 fp->link_update = link_update;
127 fp->phydev = phydev;
128 return 0;
129 }
130 }
131
132 return -ENOENT;
133}
134EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
135
Linus Walleij5468e822019-02-04 11:26:18 +0100136static int fixed_phy_add_gpiod(unsigned int irq, int phy_addr,
137 struct fixed_phy_status *status,
138 struct gpio_desc *gpiod)
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300139{
140 int ret;
141 struct fixed_mdio_bus *fmb = &platform_fmb;
142 struct fixed_phy *fp;
143
Russell King68888ce2016-06-23 14:50:15 +0100144 ret = swphy_validate_state(status);
145 if (ret < 0)
146 return ret;
147
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300148 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
149 if (!fp)
150 return -ENOMEM;
151
Russell Kingbf7afb22016-06-23 14:50:25 +0100152 seqcount_init(&fp->seqcount);
153
Rabin Vincent185be5a2016-05-18 12:47:31 +0200154 if (irq != PHY_POLL)
155 fmb->mii_bus->irq[phy_addr] = irq;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300156
Thomas Petazzoni9b744942014-05-16 16:14:03 +0200157 fp->addr = phy_addr;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300158 fp->status = *status;
Linus Walleij5468e822019-02-04 11:26:18 +0100159 fp->link_gpiod = gpiod;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300160
Russell King37688e32016-06-23 14:50:20 +0100161 fixed_phy_update(fp);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300162
163 list_add_tail(&fp->node, &fmb->phys);
164
165 return 0;
Linus Walleij5468e822019-02-04 11:26:18 +0100166}
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300167
Linus Walleij5468e822019-02-04 11:26:18 +0100168int fixed_phy_add(unsigned int irq, int phy_addr,
169 struct fixed_phy_status *status) {
170
171 return fixed_phy_add_gpiod(irq, phy_addr, status, NULL);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300172}
173EXPORT_SYMBOL_GPL(fixed_phy_add);
174
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700175static DEFINE_IDA(phy_fixed_ida);
176
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100177static void fixed_phy_del(int phy_addr)
Thomas Petazzonia7595122014-05-16 16:14:04 +0200178{
179 struct fixed_mdio_bus *fmb = &platform_fmb;
180 struct fixed_phy *fp, *tmp;
181
182 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
183 if (fp->addr == phy_addr) {
184 list_del(&fp->node);
Linus Walleij5468e822019-02-04 11:26:18 +0100185 if (fp->link_gpiod)
186 gpiod_put(fp->link_gpiod);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200187 kfree(fp);
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700188 ida_simple_remove(&phy_fixed_ida, phy_addr);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200189 return;
190 }
191 }
192}
Thomas Petazzonia7595122014-05-16 16:14:04 +0200193
Linus Walleij5468e822019-02-04 11:26:18 +0100194#ifdef CONFIG_OF_GPIO
195static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np)
196{
197 struct device_node *fixed_link_node;
198 struct gpio_desc *gpiod;
199
200 if (!np)
201 return NULL;
202
203 fixed_link_node = of_get_child_by_name(np, "fixed-link");
204 if (!fixed_link_node)
205 return NULL;
206
207 /*
208 * As the fixed link is just a device tree node without any
209 * Linux device associated with it, we simply have obtain
210 * the GPIO descriptor from the device tree like this.
211 */
212 gpiod = gpiod_get_from_of_node(fixed_link_node, "link-gpios", 0,
213 GPIOD_IN, "mdio");
214 of_node_put(fixed_link_node);
215 if (IS_ERR(gpiod)) {
216 if (PTR_ERR(gpiod) == -EPROBE_DEFER)
217 return gpiod;
218 pr_err("error getting GPIO for fixed link %pOF, proceed without\n",
219 fixed_link_node);
220 gpiod = NULL;
221 }
222
223 return gpiod;
224}
225#else
226static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np)
227{
228 return NULL;
229}
230#endif
231
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700232struct phy_device *fixed_phy_register(unsigned int irq,
233 struct fixed_phy_status *status,
234 struct device_node *np)
Thomas Petazzonia7595122014-05-16 16:14:04 +0200235{
236 struct fixed_mdio_bus *fmb = &platform_fmb;
Linus Walleij5468e822019-02-04 11:26:18 +0100237 struct gpio_desc *gpiod = NULL;
Thomas Petazzonia7595122014-05-16 16:14:04 +0200238 struct phy_device *phy;
239 int phy_addr;
240 int ret;
241
Rabin Vincent185be5a2016-05-18 12:47:31 +0200242 if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
243 return ERR_PTR(-EPROBE_DEFER);
244
Linus Walleij5468e822019-02-04 11:26:18 +0100245 /* Check if we have a GPIO associated with this fixed phy */
246 gpiod = fixed_phy_get_gpiod(np);
247 if (IS_ERR(gpiod))
248 return ERR_CAST(gpiod);
249
Thomas Petazzonia7595122014-05-16 16:14:04 +0200250 /* Get the next available PHY address, up to PHY_MAX_ADDR */
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700251 phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL);
252 if (phy_addr < 0)
253 return ERR_PTR(phy_addr);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200254
Linus Walleij5468e822019-02-04 11:26:18 +0100255 ret = fixed_phy_add_gpiod(irq, phy_addr, status, gpiod);
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700256 if (ret < 0) {
257 ida_simple_remove(&phy_fixed_ida, phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700258 return ERR_PTR(ret);
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700259 }
Thomas Petazzonia7595122014-05-16 16:14:04 +0200260
261 phy = get_phy_device(fmb->mii_bus, phy_addr, false);
Sergei Shtylyov4914a582016-04-24 20:29:23 +0300262 if (IS_ERR(phy)) {
Thomas Petazzonia7595122014-05-16 16:14:04 +0200263 fixed_phy_del(phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700264 return ERR_PTR(-EINVAL);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200265 }
266
Madalin Bucur4b195362015-08-26 17:58:47 +0300267 /* propagate the fixed link values to struct phy_device */
268 phy->link = status->link;
269 if (status->link) {
270 phy->speed = status->speed;
271 phy->duplex = status->duplex;
272 phy->pause = status->pause;
273 phy->asym_pause = status->asym_pause;
274 }
275
Thomas Petazzonia7595122014-05-16 16:14:04 +0200276 of_node_get(np);
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100277 phy->mdio.dev.of_node = np;
Florian Fainelli5a11dd72015-08-31 15:56:46 +0200278 phy->is_pseudo_fixed_link = true;
Thomas Petazzonia7595122014-05-16 16:14:04 +0200279
Andrew Lunn34b31da42015-08-31 15:56:48 +0200280 switch (status->speed) {
281 case SPEED_1000:
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100282 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
283 phy->supported);
284 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
285 phy->supported);
286 /* fall through */
Andrew Lunn34b31da42015-08-31 15:56:48 +0200287 case SPEED_100:
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100288 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
289 phy->supported);
290 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
291 phy->supported);
292 /* fall through */
Andrew Lunn34b31da42015-08-31 15:56:48 +0200293 case SPEED_10:
294 default:
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100295 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
296 phy->supported);
297 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
298 phy->supported);
Andrew Lunn34b31da42015-08-31 15:56:48 +0200299 }
300
Thomas Petazzonia7595122014-05-16 16:14:04 +0200301 ret = phy_device_register(phy);
302 if (ret) {
303 phy_device_free(phy);
304 of_node_put(np);
305 fixed_phy_del(phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700306 return ERR_PTR(ret);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200307 }
308
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700309 return phy;
Thomas Petazzonia7595122014-05-16 16:14:04 +0200310}
Mark Salter37e9a692014-12-11 23:03:26 -0500311EXPORT_SYMBOL_GPL(fixed_phy_register);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200312
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100313void fixed_phy_unregister(struct phy_device *phy)
314{
315 phy_device_remove(phy);
Johan Hovold13c9d932016-11-16 15:20:38 +0100316 of_node_put(phy->mdio.dev.of_node);
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100317 fixed_phy_del(phy->mdio.addr);
318}
319EXPORT_SYMBOL_GPL(fixed_phy_unregister);
320
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300321static int __init fixed_mdio_bus_init(void)
322{
323 struct fixed_mdio_bus *fmb = &platform_fmb;
324 int ret;
325
326 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
Fabio Estevamb0c16382018-06-23 21:28:22 -0300327 if (IS_ERR(pdev))
328 return PTR_ERR(pdev);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300329
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700330 fmb->mii_bus = mdiobus_alloc();
331 if (fmb->mii_bus == NULL) {
332 ret = -ENOMEM;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300333 goto err_mdiobus_reg;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700334 }
335
Florian Fainelli9e6c6432012-01-09 23:59:25 +0000336 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700337 fmb->mii_bus->name = "Fixed MDIO Bus";
Lennert Buytenhekec2a5652008-10-09 09:45:04 -0700338 fmb->mii_bus->priv = fmb;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700339 fmb->mii_bus->parent = &pdev->dev;
340 fmb->mii_bus->read = &fixed_mdio_read;
341 fmb->mii_bus->write = &fixed_mdio_write;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700342
343 ret = mdiobus_register(fmb->mii_bus);
344 if (ret)
345 goto err_mdiobus_alloc;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300346
347 return 0;
348
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700349err_mdiobus_alloc:
350 mdiobus_free(fmb->mii_bus);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300351err_mdiobus_reg:
352 platform_device_unregister(pdev);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300353 return ret;
354}
355module_init(fixed_mdio_bus_init);
356
357static void __exit fixed_mdio_bus_exit(void)
358{
359 struct fixed_mdio_bus *fmb = &platform_fmb;
Adrian Bunk651be3a2008-02-02 23:15:02 +0200360 struct fixed_phy *fp, *tmp;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300361
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700362 mdiobus_unregister(fmb->mii_bus);
363 mdiobus_free(fmb->mii_bus);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300364 platform_device_unregister(pdev);
365
Adrian Bunk651be3a2008-02-02 23:15:02 +0200366 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300367 list_del(&fp->node);
368 kfree(fp);
369 }
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700370 ida_destroy(&phy_fixed_ida);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300371}
372module_exit(fixed_mdio_bus_exit);
373
374MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700375MODULE_AUTHOR("Vitaly Bordug");
376MODULE_LICENSE("GPL");