blob: 18d81f43f2a889be11095d4613dc720d34d0ab6c [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>
Florian Fainelli69fc58a2016-06-24 16:25:24 -070022#include <linux/idr.h>
Joakim Tjernlundb3e54642018-12-14 15:17:05 +010023#include <linux/netdevice.h>
Heiner Kallweit0f3b1cf2019-02-24 18:01:18 +010024#include <linux/linkmode.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;
36 struct fixed_phy_status status;
Joakim Tjernlundb3e54642018-12-14 15:17:05 +010037 bool no_carrier;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030038 int (*link_update)(struct net_device *, struct fixed_phy_status *);
39 struct list_head node;
Linus Walleij5468e822019-02-04 11:26:18 +010040 struct gpio_desc *link_gpiod;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030041};
42
43static struct platform_device *pdev;
44static struct fixed_mdio_bus platform_fmb = {
45 .phys = LIST_HEAD_INIT(platform_fmb.phys),
46};
47
Joakim Tjernlundb3e54642018-12-14 15:17:05 +010048int fixed_phy_change_carrier(struct net_device *dev, bool new_carrier)
49{
50 struct fixed_mdio_bus *fmb = &platform_fmb;
51 struct phy_device *phydev = dev->phydev;
52 struct fixed_phy *fp;
53
54 if (!phydev || !phydev->mdio.bus)
55 return -EINVAL;
56
57 list_for_each_entry(fp, &fmb->phys, node) {
58 if (fp->addr == phydev->mdio.addr) {
59 fp->no_carrier = !new_carrier;
60 return 0;
61 }
62 }
63 return -EINVAL;
64}
65EXPORT_SYMBOL_GPL(fixed_phy_change_carrier);
66
Russell King37688e32016-06-23 14:50:20 +010067static void fixed_phy_update(struct fixed_phy *fp)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070068{
Linus Walleij5468e822019-02-04 11:26:18 +010069 if (!fp->no_carrier && fp->link_gpiod)
70 fp->status.link = !!gpiod_get_value_cansleep(fp->link_gpiod);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070071}
72
Thomas Petazzoni9b744942014-05-16 16:14:03 +020073static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070074{
Lennert Buytenhekec2a5652008-10-09 09:45:04 -070075 struct fixed_mdio_bus *fmb = bus->priv;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030076 struct fixed_phy *fp;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070077
Vitaly Borduga79d8e92007-12-07 01:51:22 +030078 list_for_each_entry(fp, &fmb->phys, node) {
Thomas Petazzoni9b744942014-05-16 16:14:03 +020079 if (fp->addr == phy_addr) {
Russell Kingbf7afb22016-06-23 14:50:25 +010080 struct fixed_phy_status state;
Russell Kingbf7afb22016-06-23 14:50:25 +010081
Ahmed S. Darwish79cbb6b2020-06-03 16:49:45 +020082 fp->status.link = !fp->no_carrier;
83
84 /* Issue callback if user registered it. */
85 if (fp->link_update)
86 fp->link_update(fp->phydev->attached_dev,
87 &fp->status);
88
89 /* Check the GPIO for change in status */
90 fixed_phy_update(fp);
91 state = fp->status;
Russell Kingbf7afb22016-06-23 14:50:25 +010092
93 return swphy_read_reg(reg_num, &state);
Vitaly Borduga79d8e92007-12-07 01:51:22 +030094 }
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070095 }
96
Vitaly Borduga79d8e92007-12-07 01:51:22 +030097 return 0xFFFF;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070098}
99
Thomas Petazzoni9b744942014-05-16 16:14:03 +0200100static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300101 u16 val)
102{
103 return 0;
104}
105
106/*
107 * If something weird is required to be done with link/speed,
108 * network driver is able to assign a function to implement this.
109 * May be useful for PHY's that need to be software-driven.
110 */
111int fixed_phy_set_link_update(struct phy_device *phydev,
112 int (*link_update)(struct net_device *,
113 struct fixed_phy_status *))
114{
115 struct fixed_mdio_bus *fmb = &platform_fmb;
116 struct fixed_phy *fp;
117
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100118 if (!phydev || !phydev->mdio.bus)
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300119 return -EINVAL;
120
121 list_for_each_entry(fp, &fmb->phys, node) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100122 if (fp->addr == phydev->mdio.addr) {
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300123 fp->link_update = link_update;
124 fp->phydev = phydev;
125 return 0;
126 }
127 }
128
129 return -ENOENT;
130}
131EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
132
Linus Walleij5468e822019-02-04 11:26:18 +0100133static int fixed_phy_add_gpiod(unsigned int irq, int phy_addr,
134 struct fixed_phy_status *status,
135 struct gpio_desc *gpiod)
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300136{
137 int ret;
138 struct fixed_mdio_bus *fmb = &platform_fmb;
139 struct fixed_phy *fp;
140
Russell King68888ce2016-06-23 14:50:15 +0100141 ret = swphy_validate_state(status);
142 if (ret < 0)
143 return ret;
144
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300145 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
146 if (!fp)
147 return -ENOMEM;
148
Rabin Vincent185be5a2016-05-18 12:47:31 +0200149 if (irq != PHY_POLL)
150 fmb->mii_bus->irq[phy_addr] = irq;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300151
Thomas Petazzoni9b744942014-05-16 16:14:03 +0200152 fp->addr = phy_addr;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300153 fp->status = *status;
Linus Walleij5468e822019-02-04 11:26:18 +0100154 fp->link_gpiod = gpiod;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300155
Russell King37688e32016-06-23 14:50:20 +0100156 fixed_phy_update(fp);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300157
158 list_add_tail(&fp->node, &fmb->phys);
159
160 return 0;
Linus Walleij5468e822019-02-04 11:26:18 +0100161}
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300162
Linus Walleij5468e822019-02-04 11:26:18 +0100163int fixed_phy_add(unsigned int irq, int phy_addr,
164 struct fixed_phy_status *status) {
165
166 return fixed_phy_add_gpiod(irq, phy_addr, status, NULL);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300167}
168EXPORT_SYMBOL_GPL(fixed_phy_add);
169
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700170static DEFINE_IDA(phy_fixed_ida);
171
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100172static void fixed_phy_del(int phy_addr)
Thomas Petazzonia7595122014-05-16 16:14:04 +0200173{
174 struct fixed_mdio_bus *fmb = &platform_fmb;
175 struct fixed_phy *fp, *tmp;
176
177 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
178 if (fp->addr == phy_addr) {
179 list_del(&fp->node);
Linus Walleij5468e822019-02-04 11:26:18 +0100180 if (fp->link_gpiod)
181 gpiod_put(fp->link_gpiod);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200182 kfree(fp);
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700183 ida_simple_remove(&phy_fixed_ida, phy_addr);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200184 return;
185 }
186 }
187}
Thomas Petazzonia7595122014-05-16 16:14:04 +0200188
Linus Walleij5468e822019-02-04 11:26:18 +0100189#ifdef CONFIG_OF_GPIO
190static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np)
191{
192 struct device_node *fixed_link_node;
193 struct gpio_desc *gpiod;
194
195 if (!np)
196 return NULL;
197
198 fixed_link_node = of_get_child_by_name(np, "fixed-link");
199 if (!fixed_link_node)
200 return NULL;
201
202 /*
203 * As the fixed link is just a device tree node without any
204 * Linux device associated with it, we simply have obtain
205 * the GPIO descriptor from the device tree like this.
206 */
Dmitry Torokhov5ffcc852020-01-02 17:03:20 -0800207 gpiod = fwnode_gpiod_get_index(of_fwnode_handle(fixed_link_node),
208 "link", 0, GPIOD_IN, "mdio");
Dmitry Torokhovd266f192020-01-02 17:03:19 -0800209 if (IS_ERR(gpiod) && PTR_ERR(gpiod) != -EPROBE_DEFER) {
Hubert Feursteinab98c002019-07-30 11:46:23 +0200210 if (PTR_ERR(gpiod) != -ENOENT)
211 pr_err("error getting GPIO for fixed link %pOF, proceed without\n",
212 fixed_link_node);
Linus Walleij5468e822019-02-04 11:26:18 +0100213 gpiod = NULL;
214 }
Dmitry Torokhovd266f192020-01-02 17:03:19 -0800215 of_node_put(fixed_link_node);
Linus Walleij5468e822019-02-04 11:26:18 +0100216
217 return gpiod;
218}
219#else
220static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np)
221{
222 return NULL;
223}
224#endif
225
Moritz Fischer71bd1062019-02-07 09:52:10 -0800226static struct phy_device *__fixed_phy_register(unsigned int irq,
227 struct fixed_phy_status *status,
228 struct device_node *np,
229 struct gpio_desc *gpiod)
Thomas Petazzonia7595122014-05-16 16:14:04 +0200230{
231 struct fixed_mdio_bus *fmb = &platform_fmb;
232 struct phy_device *phy;
233 int phy_addr;
234 int ret;
235
Rabin Vincent185be5a2016-05-18 12:47:31 +0200236 if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
237 return ERR_PTR(-EPROBE_DEFER);
238
Linus Walleij5468e822019-02-04 11:26:18 +0100239 /* Check if we have a GPIO associated with this fixed phy */
Moritz Fischer71bd1062019-02-07 09:52:10 -0800240 if (!gpiod) {
241 gpiod = fixed_phy_get_gpiod(np);
242 if (IS_ERR(gpiod))
243 return ERR_CAST(gpiod);
244 }
Linus Walleij5468e822019-02-04 11:26:18 +0100245
Thomas Petazzonia7595122014-05-16 16:14:04 +0200246 /* Get the next available PHY address, up to PHY_MAX_ADDR */
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700247 phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL);
248 if (phy_addr < 0)
249 return ERR_PTR(phy_addr);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200250
Linus Walleij5468e822019-02-04 11:26:18 +0100251 ret = fixed_phy_add_gpiod(irq, phy_addr, status, gpiod);
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700252 if (ret < 0) {
253 ida_simple_remove(&phy_fixed_ida, phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700254 return ERR_PTR(ret);
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700255 }
Thomas Petazzonia7595122014-05-16 16:14:04 +0200256
257 phy = get_phy_device(fmb->mii_bus, phy_addr, false);
Sergei Shtylyov4914a582016-04-24 20:29:23 +0300258 if (IS_ERR(phy)) {
Thomas Petazzonia7595122014-05-16 16:14:04 +0200259 fixed_phy_del(phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700260 return ERR_PTR(-EINVAL);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200261 }
262
Madalin Bucur4b195362015-08-26 17:58:47 +0300263 /* propagate the fixed link values to struct phy_device */
264 phy->link = status->link;
265 if (status->link) {
266 phy->speed = status->speed;
267 phy->duplex = status->duplex;
268 phy->pause = status->pause;
269 phy->asym_pause = status->asym_pause;
270 }
271
Thomas Petazzonia7595122014-05-16 16:14:04 +0200272 of_node_get(np);
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100273 phy->mdio.dev.of_node = np;
Florian Fainelli5a11dd72015-08-31 15:56:46 +0200274 phy->is_pseudo_fixed_link = true;
Thomas Petazzonia7595122014-05-16 16:14:04 +0200275
Andrew Lunn34b31da42015-08-31 15:56:48 +0200276 switch (status->speed) {
277 case SPEED_1000:
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100278 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
279 phy->supported);
280 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
281 phy->supported);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500282 fallthrough;
Andrew Lunn34b31da42015-08-31 15:56:48 +0200283 case SPEED_100:
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100284 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
285 phy->supported);
286 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
287 phy->supported);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500288 fallthrough;
Andrew Lunn34b31da42015-08-31 15:56:48 +0200289 case SPEED_10:
290 default:
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100291 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
292 phy->supported);
293 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
294 phy->supported);
Andrew Lunn34b31da42015-08-31 15:56:48 +0200295 }
296
Heiner Kallweit22c0ef62019-05-01 21:34:43 +0200297 phy_advertise_supported(phy);
Heiner Kallweit0f3b1cf2019-02-24 18:01:18 +0100298
Thomas Petazzonia7595122014-05-16 16:14:04 +0200299 ret = phy_device_register(phy);
300 if (ret) {
301 phy_device_free(phy);
302 of_node_put(np);
303 fixed_phy_del(phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700304 return ERR_PTR(ret);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200305 }
306
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700307 return phy;
Thomas Petazzonia7595122014-05-16 16:14:04 +0200308}
Moritz Fischer71bd1062019-02-07 09:52:10 -0800309
310struct phy_device *fixed_phy_register(unsigned int irq,
311 struct fixed_phy_status *status,
312 struct device_node *np)
313{
314 return __fixed_phy_register(irq, status, np, NULL);
315}
Mark Salter37e9a692014-12-11 23:03:26 -0500316EXPORT_SYMBOL_GPL(fixed_phy_register);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200317
Moritz Fischer71bd1062019-02-07 09:52:10 -0800318struct phy_device *
319fixed_phy_register_with_gpiod(unsigned int irq,
320 struct fixed_phy_status *status,
321 struct gpio_desc *gpiod)
322{
323 return __fixed_phy_register(irq, status, NULL, gpiod);
324}
325EXPORT_SYMBOL_GPL(fixed_phy_register_with_gpiod);
326
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100327void fixed_phy_unregister(struct phy_device *phy)
328{
329 phy_device_remove(phy);
Johan Hovold13c9d932016-11-16 15:20:38 +0100330 of_node_put(phy->mdio.dev.of_node);
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100331 fixed_phy_del(phy->mdio.addr);
332}
333EXPORT_SYMBOL_GPL(fixed_phy_unregister);
334
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300335static int __init fixed_mdio_bus_init(void)
336{
337 struct fixed_mdio_bus *fmb = &platform_fmb;
338 int ret;
339
340 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
Fabio Estevamb0c16382018-06-23 21:28:22 -0300341 if (IS_ERR(pdev))
342 return PTR_ERR(pdev);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300343
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700344 fmb->mii_bus = mdiobus_alloc();
345 if (fmb->mii_bus == NULL) {
346 ret = -ENOMEM;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300347 goto err_mdiobus_reg;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700348 }
349
Florian Fainelli9e6c6432012-01-09 23:59:25 +0000350 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700351 fmb->mii_bus->name = "Fixed MDIO Bus";
Lennert Buytenhekec2a5652008-10-09 09:45:04 -0700352 fmb->mii_bus->priv = fmb;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700353 fmb->mii_bus->parent = &pdev->dev;
354 fmb->mii_bus->read = &fixed_mdio_read;
355 fmb->mii_bus->write = &fixed_mdio_write;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700356
357 ret = mdiobus_register(fmb->mii_bus);
358 if (ret)
359 goto err_mdiobus_alloc;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300360
361 return 0;
362
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700363err_mdiobus_alloc:
364 mdiobus_free(fmb->mii_bus);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300365err_mdiobus_reg:
366 platform_device_unregister(pdev);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300367 return ret;
368}
369module_init(fixed_mdio_bus_init);
370
371static void __exit fixed_mdio_bus_exit(void)
372{
373 struct fixed_mdio_bus *fmb = &platform_fmb;
Adrian Bunk651be3a2008-02-02 23:15:02 +0200374 struct fixed_phy *fp, *tmp;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300375
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700376 mdiobus_unregister(fmb->mii_bus);
377 mdiobus_free(fmb->mii_bus);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300378 platform_device_unregister(pdev);
379
Adrian Bunk651be3a2008-02-02 23:15:02 +0200380 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300381 list_del(&fp->node);
382 kfree(fp);
383 }
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700384 ida_destroy(&phy_fixed_ida);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300385}
386module_exit(fixed_mdio_bus_exit);
387
388MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700389MODULE_AUTHOR("Vitaly Bordug");
390MODULE_LICENSE("GPL");