blob: c649c101bbaba97ec44255b9ee1e7a878c26ada5 [file] [log] [blame]
Vitaly Bordug11b0bac2006-08-14 23:00:29 -07001/*
Vitaly Borduga79d8e92007-12-07 01:51:22 +03002 * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -07003 *
Vitaly Borduga79d8e92007-12-07 01:51:22 +03004 * Author: Vitaly Bordug <vbordug@ru.mvista.com>
5 * Anton Vorontsov <avorontsov@ru.mvista.com>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -07006 *
Vitaly Borduga79d8e92007-12-07 01:51:22 +03007 * Copyright (c) 2006-2007 MontaVista Software, Inc.
Vitaly Bordug11b0bac2006-08-14 23:00:29 -07008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070013 */
Vitaly Borduga79d8e92007-12-07 01:51:22 +030014
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070015#include <linux/kernel.h>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070016#include <linux/module.h>
Vitaly Borduga79d8e92007-12-07 01:51:22 +030017#include <linux/platform_device.h>
18#include <linux/list.h>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070019#include <linux/mii.h>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070020#include <linux/phy.h>
Vitaly Bordug7c32f472007-08-10 14:05:16 -070021#include <linux/phy_fixed.h>
Dan Carpenter57401d52009-04-11 01:52:29 -070022#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Thomas Petazzonia7595122014-05-16 16:14:04 +020024#include <linux/of.h>
Andrew Lunna5597002015-08-31 15:56:53 +020025#include <linux/gpio.h>
Russell Kingbf7afb22016-06-23 14:50:25 +010026#include <linux/seqlock.h>
Florian Fainelli69fc58a2016-06-24 16:25:24 -070027#include <linux/idr.h>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070028
Russell King5ae68b02016-06-23 14:50:05 +010029#include "swphy.h"
30
Vitaly Borduga79d8e92007-12-07 01:51:22 +030031struct fixed_mdio_bus {
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -070032 struct mii_bus *mii_bus;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030033 struct list_head phys;
34};
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070035
Vitaly Borduga79d8e92007-12-07 01:51:22 +030036struct fixed_phy {
Thomas Petazzoni9b744942014-05-16 16:14:03 +020037 int addr;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030038 struct phy_device *phydev;
Russell Kingbf7afb22016-06-23 14:50:25 +010039 seqcount_t seqcount;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030040 struct fixed_phy_status status;
41 int (*link_update)(struct net_device *, struct fixed_phy_status *);
42 struct list_head node;
Andrew Lunna5597002015-08-31 15:56:53 +020043 int link_gpio;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030044};
45
46static struct platform_device *pdev;
47static struct fixed_mdio_bus platform_fmb = {
48 .phys = LIST_HEAD_INIT(platform_fmb.phys),
49};
50
Russell King37688e32016-06-23 14:50:20 +010051static void fixed_phy_update(struct fixed_phy *fp)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070052{
Andrew Lunna5597002015-08-31 15:56:53 +020053 if (gpio_is_valid(fp->link_gpio))
54 fp->status.link = !!gpio_get_value_cansleep(fp->link_gpio);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070055}
56
Thomas Petazzoni9b744942014-05-16 16:14:03 +020057static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070058{
Lennert Buytenhekec2a5652008-10-09 09:45:04 -070059 struct fixed_mdio_bus *fmb = bus->priv;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030060 struct fixed_phy *fp;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070061
Vitaly Borduga79d8e92007-12-07 01:51:22 +030062 list_for_each_entry(fp, &fmb->phys, node) {
Thomas Petazzoni9b744942014-05-16 16:14:03 +020063 if (fp->addr == phy_addr) {
Russell Kingbf7afb22016-06-23 14:50:25 +010064 struct fixed_phy_status state;
65 int s;
66
67 do {
68 s = read_seqcount_begin(&fp->seqcount);
69 /* Issue callback if user registered it. */
70 if (fp->link_update) {
71 fp->link_update(fp->phydev->attached_dev,
72 &fp->status);
73 fixed_phy_update(fp);
74 }
75 state = fp->status;
76 } while (read_seqcount_retry(&fp->seqcount, s));
77
78 return swphy_read_reg(reg_num, &state);
Vitaly Borduga79d8e92007-12-07 01:51:22 +030079 }
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070080 }
81
Vitaly Borduga79d8e92007-12-07 01:51:22 +030082 return 0xFFFF;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070083}
84
Thomas Petazzoni9b744942014-05-16 16:14:03 +020085static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
Vitaly Borduga79d8e92007-12-07 01:51:22 +030086 u16 val)
87{
88 return 0;
89}
90
91/*
92 * If something weird is required to be done with link/speed,
93 * network driver is able to assign a function to implement this.
94 * May be useful for PHY's that need to be software-driven.
95 */
96int fixed_phy_set_link_update(struct phy_device *phydev,
97 int (*link_update)(struct net_device *,
98 struct fixed_phy_status *))
99{
100 struct fixed_mdio_bus *fmb = &platform_fmb;
101 struct fixed_phy *fp;
102
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100103 if (!phydev || !phydev->mdio.bus)
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300104 return -EINVAL;
105
106 list_for_each_entry(fp, &fmb->phys, node) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100107 if (fp->addr == phydev->mdio.addr) {
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300108 fp->link_update = link_update;
109 fp->phydev = phydev;
110 return 0;
111 }
112 }
113
114 return -ENOENT;
115}
116EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
117
Stas Sergeeva3bebdc2015-04-01 20:30:31 +0300118int fixed_phy_update_state(struct phy_device *phydev,
119 const struct fixed_phy_status *status,
120 const struct fixed_phy_status *changed)
121{
122 struct fixed_mdio_bus *fmb = &platform_fmb;
123 struct fixed_phy *fp;
124
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100125 if (!phydev || phydev->mdio.bus != fmb->mii_bus)
Stas Sergeeva3bebdc2015-04-01 20:30:31 +0300126 return -EINVAL;
127
128 list_for_each_entry(fp, &fmb->phys, node) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100129 if (fp->addr == phydev->mdio.addr) {
Russell Kingbf7afb22016-06-23 14:50:25 +0100130 write_seqcount_begin(&fp->seqcount);
Stas Sergeeva3bebdc2015-04-01 20:30:31 +0300131#define _UPD(x) if (changed->x) \
132 fp->status.x = status->x
133 _UPD(link);
134 _UPD(speed);
135 _UPD(duplex);
136 _UPD(pause);
137 _UPD(asym_pause);
138#undef _UPD
Russell King37688e32016-06-23 14:50:20 +0100139 fixed_phy_update(fp);
Russell Kingbf7afb22016-06-23 14:50:25 +0100140 write_seqcount_end(&fp->seqcount);
Stas Sergeeva3bebdc2015-04-01 20:30:31 +0300141 return 0;
142 }
143 }
144
145 return -ENOENT;
146}
147EXPORT_SYMBOL(fixed_phy_update_state);
148
Thomas Petazzoni9b744942014-05-16 16:14:03 +0200149int fixed_phy_add(unsigned int irq, int phy_addr,
Andrew Lunna5597002015-08-31 15:56:53 +0200150 struct fixed_phy_status *status,
151 int link_gpio)
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300152{
153 int ret;
154 struct fixed_mdio_bus *fmb = &platform_fmb;
155 struct fixed_phy *fp;
156
Russell King68888ce2016-06-23 14:50:15 +0100157 ret = swphy_validate_state(status);
158 if (ret < 0)
159 return ret;
160
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300161 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
162 if (!fp)
163 return -ENOMEM;
164
Russell Kingbf7afb22016-06-23 14:50:25 +0100165 seqcount_init(&fp->seqcount);
166
Rabin Vincent185be5a2016-05-18 12:47:31 +0200167 if (irq != PHY_POLL)
168 fmb->mii_bus->irq[phy_addr] = irq;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300169
Thomas Petazzoni9b744942014-05-16 16:14:03 +0200170 fp->addr = phy_addr;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300171 fp->status = *status;
Andrew Lunna5597002015-08-31 15:56:53 +0200172 fp->link_gpio = link_gpio;
173
174 if (gpio_is_valid(fp->link_gpio)) {
175 ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN,
176 "fixed-link-gpio-link");
177 if (ret)
178 goto err_regs;
179 }
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300180
Russell King37688e32016-06-23 14:50:20 +0100181 fixed_phy_update(fp);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300182
183 list_add_tail(&fp->node, &fmb->phys);
184
185 return 0;
186
187err_regs:
188 kfree(fp);
189 return ret;
190}
191EXPORT_SYMBOL_GPL(fixed_phy_add);
192
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700193static DEFINE_IDA(phy_fixed_ida);
194
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100195static void fixed_phy_del(int phy_addr)
Thomas Petazzonia7595122014-05-16 16:14:04 +0200196{
197 struct fixed_mdio_bus *fmb = &platform_fmb;
198 struct fixed_phy *fp, *tmp;
199
200 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
201 if (fp->addr == phy_addr) {
202 list_del(&fp->node);
Andrew Lunna5597002015-08-31 15:56:53 +0200203 if (gpio_is_valid(fp->link_gpio))
204 gpio_free(fp->link_gpio);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200205 kfree(fp);
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700206 ida_simple_remove(&phy_fixed_ida, phy_addr);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200207 return;
208 }
209 }
210}
Thomas Petazzonia7595122014-05-16 16:14:04 +0200211
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700212struct phy_device *fixed_phy_register(unsigned int irq,
213 struct fixed_phy_status *status,
Andrew Lunna5597002015-08-31 15:56:53 +0200214 int link_gpio,
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700215 struct device_node *np)
Thomas Petazzonia7595122014-05-16 16:14:04 +0200216{
217 struct fixed_mdio_bus *fmb = &platform_fmb;
218 struct phy_device *phy;
219 int phy_addr;
220 int ret;
221
Rabin Vincent185be5a2016-05-18 12:47:31 +0200222 if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
223 return ERR_PTR(-EPROBE_DEFER);
224
Thomas Petazzonia7595122014-05-16 16:14:04 +0200225 /* Get the next available PHY address, up to PHY_MAX_ADDR */
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700226 phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL);
227 if (phy_addr < 0)
228 return ERR_PTR(phy_addr);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200229
Sergei Shtylyovbd1a05e2015-09-03 23:22:16 +0300230 ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700231 if (ret < 0) {
232 ida_simple_remove(&phy_fixed_ida, phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700233 return ERR_PTR(ret);
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700234 }
Thomas Petazzonia7595122014-05-16 16:14:04 +0200235
236 phy = get_phy_device(fmb->mii_bus, phy_addr, false);
Sergei Shtylyov4914a582016-04-24 20:29:23 +0300237 if (IS_ERR(phy)) {
Thomas Petazzonia7595122014-05-16 16:14:04 +0200238 fixed_phy_del(phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700239 return ERR_PTR(-EINVAL);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200240 }
241
Madalin Bucur4b195362015-08-26 17:58:47 +0300242 /* propagate the fixed link values to struct phy_device */
243 phy->link = status->link;
244 if (status->link) {
245 phy->speed = status->speed;
246 phy->duplex = status->duplex;
247 phy->pause = status->pause;
248 phy->asym_pause = status->asym_pause;
249 }
250
Thomas Petazzonia7595122014-05-16 16:14:04 +0200251 of_node_get(np);
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100252 phy->mdio.dev.of_node = np;
Florian Fainelli5a11dd72015-08-31 15:56:46 +0200253 phy->is_pseudo_fixed_link = true;
Thomas Petazzonia7595122014-05-16 16:14:04 +0200254
Andrew Lunn34b31da42015-08-31 15:56:48 +0200255 switch (status->speed) {
256 case SPEED_1000:
257 phy->supported = PHY_1000BT_FEATURES;
258 break;
259 case SPEED_100:
260 phy->supported = PHY_100BT_FEATURES;
261 break;
262 case SPEED_10:
263 default:
264 phy->supported = PHY_10BT_FEATURES;
265 }
266
Thomas Petazzonia7595122014-05-16 16:14:04 +0200267 ret = phy_device_register(phy);
268 if (ret) {
269 phy_device_free(phy);
270 of_node_put(np);
271 fixed_phy_del(phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700272 return ERR_PTR(ret);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200273 }
274
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700275 return phy;
Thomas Petazzonia7595122014-05-16 16:14:04 +0200276}
Mark Salter37e9a692014-12-11 23:03:26 -0500277EXPORT_SYMBOL_GPL(fixed_phy_register);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200278
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100279void fixed_phy_unregister(struct phy_device *phy)
280{
281 phy_device_remove(phy);
282
283 fixed_phy_del(phy->mdio.addr);
284}
285EXPORT_SYMBOL_GPL(fixed_phy_unregister);
286
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300287static int __init fixed_mdio_bus_init(void)
288{
289 struct fixed_mdio_bus *fmb = &platform_fmb;
290 int ret;
291
292 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
Dan Carpenter57401d52009-04-11 01:52:29 -0700293 if (IS_ERR(pdev)) {
294 ret = PTR_ERR(pdev);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300295 goto err_pdev;
296 }
297
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700298 fmb->mii_bus = mdiobus_alloc();
299 if (fmb->mii_bus == NULL) {
300 ret = -ENOMEM;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300301 goto err_mdiobus_reg;
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700302 }
303
Florian Fainelli9e6c6432012-01-09 23:59:25 +0000304 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700305 fmb->mii_bus->name = "Fixed MDIO Bus";
Lennert Buytenhekec2a5652008-10-09 09:45:04 -0700306 fmb->mii_bus->priv = fmb;
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700307 fmb->mii_bus->parent = &pdev->dev;
308 fmb->mii_bus->read = &fixed_mdio_read;
309 fmb->mii_bus->write = &fixed_mdio_write;
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700310
311 ret = mdiobus_register(fmb->mii_bus);
312 if (ret)
313 goto err_mdiobus_alloc;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300314
315 return 0;
316
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700317err_mdiobus_alloc:
318 mdiobus_free(fmb->mii_bus);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300319err_mdiobus_reg:
320 platform_device_unregister(pdev);
321err_pdev:
322 return ret;
323}
324module_init(fixed_mdio_bus_init);
325
326static void __exit fixed_mdio_bus_exit(void)
327{
328 struct fixed_mdio_bus *fmb = &platform_fmb;
Adrian Bunk651be3a2008-02-02 23:15:02 +0200329 struct fixed_phy *fp, *tmp;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300330
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700331 mdiobus_unregister(fmb->mii_bus);
332 mdiobus_free(fmb->mii_bus);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300333 platform_device_unregister(pdev);
334
Adrian Bunk651be3a2008-02-02 23:15:02 +0200335 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300336 list_del(&fp->node);
337 kfree(fp);
338 }
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700339 ida_destroy(&phy_fixed_ida);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300340}
341module_exit(fixed_mdio_bus_exit);
342
343MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700344MODULE_AUTHOR("Vitaly Bordug");
345MODULE_LICENSE("GPL");