blob: 1acd8bfdb3bc30e7855e36eb18baee4cf9baf7c3 [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>
Heiner Kallweit0f3b1cf2019-02-24 18:01:18 +010025#include <linux/linkmode.h>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070026
Russell King5ae68b02016-06-23 14:50:05 +010027#include "swphy.h"
28
Vitaly Borduga79d8e92007-12-07 01:51:22 +030029struct fixed_mdio_bus {
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070030 struct mii_bus *mii_bus;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030031 struct list_head phys;
32};
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070033
Vitaly Borduga79d8e92007-12-07 01:51:22 +030034struct fixed_phy {
Thomas Petazzoni9b744942014-05-16 16:14:03 +020035 int addr;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030036 struct phy_device *phydev;
Russell Kingbf7afb22016-06-23 14:50:25 +010037 seqcount_t seqcount;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030038 struct fixed_phy_status status;
Joakim Tjernlundb3e54642018-12-14 15:17:05 +010039 bool no_carrier;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030040 int (*link_update)(struct net_device *, struct fixed_phy_status *);
41 struct list_head node;
Linus Walleij5468e822019-02-04 11:26:18 +010042 struct gpio_desc *link_gpiod;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030043};
44
45static struct platform_device *pdev;
46static struct fixed_mdio_bus platform_fmb = {
47 .phys = LIST_HEAD_INIT(platform_fmb.phys),
48};
49
Joakim Tjernlundb3e54642018-12-14 15:17:05 +010050int fixed_phy_change_carrier(struct net_device *dev, bool new_carrier)
51{
52 struct fixed_mdio_bus *fmb = &platform_fmb;
53 struct phy_device *phydev = dev->phydev;
54 struct fixed_phy *fp;
55
56 if (!phydev || !phydev->mdio.bus)
57 return -EINVAL;
58
59 list_for_each_entry(fp, &fmb->phys, node) {
60 if (fp->addr == phydev->mdio.addr) {
61 fp->no_carrier = !new_carrier;
62 return 0;
63 }
64 }
65 return -EINVAL;
66}
67EXPORT_SYMBOL_GPL(fixed_phy_change_carrier);
68
Russell King37688e32016-06-23 14:50:20 +010069static void fixed_phy_update(struct fixed_phy *fp)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070070{
Linus Walleij5468e822019-02-04 11:26:18 +010071 if (!fp->no_carrier && fp->link_gpiod)
72 fp->status.link = !!gpiod_get_value_cansleep(fp->link_gpiod);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070073}
74
Thomas Petazzoni9b744942014-05-16 16:14:03 +020075static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070076{
Lennert Buytenhekec2a5652008-10-09 09:45:04 -070077 struct fixed_mdio_bus *fmb = bus->priv;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030078 struct fixed_phy *fp;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070079
Vitaly Borduga79d8e92007-12-07 01:51:22 +030080 list_for_each_entry(fp, &fmb->phys, node) {
Thomas Petazzoni9b744942014-05-16 16:14:03 +020081 if (fp->addr == phy_addr) {
Russell Kingbf7afb22016-06-23 14:50:25 +010082 struct fixed_phy_status state;
83 int s;
84
85 do {
86 s = read_seqcount_begin(&fp->seqcount);
Joakim Tjernlundb3e54642018-12-14 15:17:05 +010087 fp->status.link = !fp->no_carrier;
Russell Kingbf7afb22016-06-23 14:50:25 +010088 /* Issue callback if user registered it. */
Moritz Fischer8f289802019-02-06 21:45:29 -080089 if (fp->link_update)
Russell Kingbf7afb22016-06-23 14:50:25 +010090 fp->link_update(fp->phydev->attached_dev,
91 &fp->status);
Moritz Fischer8f289802019-02-06 21:45:29 -080092 /* Check the GPIO for change in status */
93 fixed_phy_update(fp);
Russell Kingbf7afb22016-06-23 14:50:25 +010094 state = fp->status;
95 } while (read_seqcount_retry(&fp->seqcount, s));
96
97 return swphy_read_reg(reg_num, &state);
Vitaly Borduga79d8e92007-12-07 01:51:22 +030098 }
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070099 }
100
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300101 return 0xFFFF;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700102}
103
Thomas Petazzoni9b744942014-05-16 16:14:03 +0200104static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300105 u16 val)
106{
107 return 0;
108}
109
110/*
111 * If something weird is required to be done with link/speed,
112 * network driver is able to assign a function to implement this.
113 * May be useful for PHY's that need to be software-driven.
114 */
115int fixed_phy_set_link_update(struct phy_device *phydev,
116 int (*link_update)(struct net_device *,
117 struct fixed_phy_status *))
118{
119 struct fixed_mdio_bus *fmb = &platform_fmb;
120 struct fixed_phy *fp;
121
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100122 if (!phydev || !phydev->mdio.bus)
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300123 return -EINVAL;
124
125 list_for_each_entry(fp, &fmb->phys, node) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100126 if (fp->addr == phydev->mdio.addr) {
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300127 fp->link_update = link_update;
128 fp->phydev = phydev;
129 return 0;
130 }
131 }
132
133 return -ENOENT;
134}
135EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
136
Linus Walleij5468e822019-02-04 11:26:18 +0100137static int fixed_phy_add_gpiod(unsigned int irq, int phy_addr,
138 struct fixed_phy_status *status,
139 struct gpio_desc *gpiod)
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300140{
141 int ret;
142 struct fixed_mdio_bus *fmb = &platform_fmb;
143 struct fixed_phy *fp;
144
Russell King68888ce2016-06-23 14:50:15 +0100145 ret = swphy_validate_state(status);
146 if (ret < 0)
147 return ret;
148
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300149 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
150 if (!fp)
151 return -ENOMEM;
152
Russell Kingbf7afb22016-06-23 14:50:25 +0100153 seqcount_init(&fp->seqcount);
154
Rabin Vincent185be5a2016-05-18 12:47:31 +0200155 if (irq != PHY_POLL)
156 fmb->mii_bus->irq[phy_addr] = irq;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300157
Thomas Petazzoni9b744942014-05-16 16:14:03 +0200158 fp->addr = phy_addr;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300159 fp->status = *status;
Linus Walleij5468e822019-02-04 11:26:18 +0100160 fp->link_gpiod = gpiod;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300161
Russell King37688e32016-06-23 14:50:20 +0100162 fixed_phy_update(fp);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300163
164 list_add_tail(&fp->node, &fmb->phys);
165
166 return 0;
Linus Walleij5468e822019-02-04 11:26:18 +0100167}
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300168
Linus Walleij5468e822019-02-04 11:26:18 +0100169int fixed_phy_add(unsigned int irq, int phy_addr,
170 struct fixed_phy_status *status) {
171
172 return fixed_phy_add_gpiod(irq, phy_addr, status, NULL);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300173}
174EXPORT_SYMBOL_GPL(fixed_phy_add);
175
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700176static DEFINE_IDA(phy_fixed_ida);
177
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100178static void fixed_phy_del(int phy_addr)
Thomas Petazzonia7595122014-05-16 16:14:04 +0200179{
180 struct fixed_mdio_bus *fmb = &platform_fmb;
181 struct fixed_phy *fp, *tmp;
182
183 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
184 if (fp->addr == phy_addr) {
185 list_del(&fp->node);
Linus Walleij5468e822019-02-04 11:26:18 +0100186 if (fp->link_gpiod)
187 gpiod_put(fp->link_gpiod);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200188 kfree(fp);
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700189 ida_simple_remove(&phy_fixed_ida, phy_addr);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200190 return;
191 }
192 }
193}
Thomas Petazzonia7595122014-05-16 16:14:04 +0200194
Linus Walleij5468e822019-02-04 11:26:18 +0100195#ifdef CONFIG_OF_GPIO
196static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np)
197{
198 struct device_node *fixed_link_node;
199 struct gpio_desc *gpiod;
200
201 if (!np)
202 return NULL;
203
204 fixed_link_node = of_get_child_by_name(np, "fixed-link");
205 if (!fixed_link_node)
206 return NULL;
207
208 /*
209 * As the fixed link is just a device tree node without any
210 * Linux device associated with it, we simply have obtain
211 * the GPIO descriptor from the device tree like this.
212 */
213 gpiod = gpiod_get_from_of_node(fixed_link_node, "link-gpios", 0,
214 GPIOD_IN, "mdio");
215 of_node_put(fixed_link_node);
216 if (IS_ERR(gpiod)) {
217 if (PTR_ERR(gpiod) == -EPROBE_DEFER)
218 return gpiod;
219 pr_err("error getting GPIO for fixed link %pOF, proceed without\n",
220 fixed_link_node);
221 gpiod = NULL;
222 }
223
224 return gpiod;
225}
226#else
227static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np)
228{
229 return NULL;
230}
231#endif
232
Moritz Fischer71bd1062019-02-07 09:52:10 -0800233static struct phy_device *__fixed_phy_register(unsigned int irq,
234 struct fixed_phy_status *status,
235 struct device_node *np,
236 struct gpio_desc *gpiod)
Thomas Petazzonia7595122014-05-16 16:14:04 +0200237{
238 struct fixed_mdio_bus *fmb = &platform_fmb;
239 struct phy_device *phy;
240 int phy_addr;
241 int ret;
242
Rabin Vincent185be5a2016-05-18 12:47:31 +0200243 if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
244 return ERR_PTR(-EPROBE_DEFER);
245
Linus Walleij5468e822019-02-04 11:26:18 +0100246 /* Check if we have a GPIO associated with this fixed phy */
Moritz Fischer71bd1062019-02-07 09:52:10 -0800247 if (!gpiod) {
248 gpiod = fixed_phy_get_gpiod(np);
249 if (IS_ERR(gpiod))
250 return ERR_CAST(gpiod);
251 }
Linus Walleij5468e822019-02-04 11:26:18 +0100252
Thomas Petazzonia7595122014-05-16 16:14:04 +0200253 /* Get the next available PHY address, up to PHY_MAX_ADDR */
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700254 phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL);
255 if (phy_addr < 0)
256 return ERR_PTR(phy_addr);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200257
Linus Walleij5468e822019-02-04 11:26:18 +0100258 ret = fixed_phy_add_gpiod(irq, phy_addr, status, gpiod);
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700259 if (ret < 0) {
260 ida_simple_remove(&phy_fixed_ida, phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700261 return ERR_PTR(ret);
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700262 }
Thomas Petazzonia7595122014-05-16 16:14:04 +0200263
264 phy = get_phy_device(fmb->mii_bus, phy_addr, false);
Sergei Shtylyov4914a582016-04-24 20:29:23 +0300265 if (IS_ERR(phy)) {
Thomas Petazzonia7595122014-05-16 16:14:04 +0200266 fixed_phy_del(phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700267 return ERR_PTR(-EINVAL);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200268 }
269
Madalin Bucur4b195362015-08-26 17:58:47 +0300270 /* propagate the fixed link values to struct phy_device */
271 phy->link = status->link;
272 if (status->link) {
273 phy->speed = status->speed;
274 phy->duplex = status->duplex;
275 phy->pause = status->pause;
276 phy->asym_pause = status->asym_pause;
277 }
278
Thomas Petazzonia7595122014-05-16 16:14:04 +0200279 of_node_get(np);
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100280 phy->mdio.dev.of_node = np;
Florian Fainelli5a11dd72015-08-31 15:56:46 +0200281 phy->is_pseudo_fixed_link = true;
Thomas Petazzonia7595122014-05-16 16:14:04 +0200282
Andrew Lunn34b31da42015-08-31 15:56:48 +0200283 switch (status->speed) {
284 case SPEED_1000:
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100285 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
286 phy->supported);
287 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
288 phy->supported);
289 /* fall through */
Andrew Lunn34b31da42015-08-31 15:56:48 +0200290 case SPEED_100:
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100291 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
292 phy->supported);
293 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
294 phy->supported);
295 /* fall through */
Andrew Lunn34b31da42015-08-31 15:56:48 +0200296 case SPEED_10:
297 default:
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100298 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
299 phy->supported);
300 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
301 phy->supported);
Andrew Lunn34b31da42015-08-31 15:56:48 +0200302 }
303
Heiner Kallweit0f3b1cf2019-02-24 18:01:18 +0100304 linkmode_copy(phy->advertising, phy->supported);
305
Thomas Petazzonia7595122014-05-16 16:14:04 +0200306 ret = phy_device_register(phy);
307 if (ret) {
308 phy_device_free(phy);
309 of_node_put(np);
310 fixed_phy_del(phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700311 return ERR_PTR(ret);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200312 }
313
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700314 return phy;
Thomas Petazzonia7595122014-05-16 16:14:04 +0200315}
Moritz Fischer71bd1062019-02-07 09:52:10 -0800316
317struct phy_device *fixed_phy_register(unsigned int irq,
318 struct fixed_phy_status *status,
319 struct device_node *np)
320{
321 return __fixed_phy_register(irq, status, np, NULL);
322}
Mark Salter37e9a692014-12-11 23:03:26 -0500323EXPORT_SYMBOL_GPL(fixed_phy_register);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200324
Moritz Fischer71bd1062019-02-07 09:52:10 -0800325struct phy_device *
326fixed_phy_register_with_gpiod(unsigned int irq,
327 struct fixed_phy_status *status,
328 struct gpio_desc *gpiod)
329{
330 return __fixed_phy_register(irq, status, NULL, gpiod);
331}
332EXPORT_SYMBOL_GPL(fixed_phy_register_with_gpiod);
333
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100334void fixed_phy_unregister(struct phy_device *phy)
335{
336 phy_device_remove(phy);
Johan Hovold13c9d932016-11-16 15:20:38 +0100337 of_node_put(phy->mdio.dev.of_node);
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100338 fixed_phy_del(phy->mdio.addr);
339}
340EXPORT_SYMBOL_GPL(fixed_phy_unregister);
341
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300342static int __init fixed_mdio_bus_init(void)
343{
344 struct fixed_mdio_bus *fmb = &platform_fmb;
345 int ret;
346
347 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
Fabio Estevamb0c16382018-06-23 21:28:22 -0300348 if (IS_ERR(pdev))
349 return PTR_ERR(pdev);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300350
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700351 fmb->mii_bus = mdiobus_alloc();
352 if (fmb->mii_bus == NULL) {
353 ret = -ENOMEM;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300354 goto err_mdiobus_reg;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700355 }
356
Florian Fainelli9e6c6432012-01-09 23:59:25 +0000357 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700358 fmb->mii_bus->name = "Fixed MDIO Bus";
Lennert Buytenhekec2a5652008-10-09 09:45:04 -0700359 fmb->mii_bus->priv = fmb;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700360 fmb->mii_bus->parent = &pdev->dev;
361 fmb->mii_bus->read = &fixed_mdio_read;
362 fmb->mii_bus->write = &fixed_mdio_write;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700363
364 ret = mdiobus_register(fmb->mii_bus);
365 if (ret)
366 goto err_mdiobus_alloc;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300367
368 return 0;
369
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700370err_mdiobus_alloc:
371 mdiobus_free(fmb->mii_bus);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300372err_mdiobus_reg:
373 platform_device_unregister(pdev);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300374 return ret;
375}
376module_init(fixed_mdio_bus_init);
377
378static void __exit fixed_mdio_bus_exit(void)
379{
380 struct fixed_mdio_bus *fmb = &platform_fmb;
Adrian Bunk651be3a2008-02-02 23:15:02 +0200381 struct fixed_phy *fp, *tmp;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300382
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700383 mdiobus_unregister(fmb->mii_bus);
384 mdiobus_free(fmb->mii_bus);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300385 platform_device_unregister(pdev);
386
Adrian Bunk651be3a2008-02-02 23:15:02 +0200387 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300388 list_del(&fp->node);
389 kfree(fp);
390 }
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700391 ida_destroy(&phy_fixed_ida);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300392}
393module_exit(fixed_mdio_bus_exit);
394
395MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700396MODULE_AUTHOR("Vitaly Bordug");
397MODULE_LICENSE("GPL");