blob: b376ada83598a08eb12b9114268d7ecb37f95e72 [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>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070027
Russell King5ae68b02016-06-23 14:50:05 +010028#include "swphy.h"
29
Vitaly Borduga79d8e92007-12-07 01:51:22 +030030struct fixed_mdio_bus {
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070031 struct mii_bus *mii_bus;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030032 struct list_head phys;
33};
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070034
Vitaly Borduga79d8e92007-12-07 01:51:22 +030035struct fixed_phy {
Thomas Petazzoni9b744942014-05-16 16:14:03 +020036 int addr;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030037 struct phy_device *phydev;
Russell Kingbf7afb22016-06-23 14:50:25 +010038 seqcount_t seqcount;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030039 struct fixed_phy_status status;
40 int (*link_update)(struct net_device *, struct fixed_phy_status *);
41 struct list_head node;
Andrew Lunna5597002015-08-31 15:56:53 +020042 int link_gpio;
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
Russell King37688e32016-06-23 14:50:20 +010050static void fixed_phy_update(struct fixed_phy *fp)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070051{
Andrew Lunna5597002015-08-31 15:56:53 +020052 if (gpio_is_valid(fp->link_gpio))
53 fp->status.link = !!gpio_get_value_cansleep(fp->link_gpio);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070054}
55
Thomas Petazzoni9b744942014-05-16 16:14:03 +020056static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070057{
Lennert Buytenhekec2a5652008-10-09 09:45:04 -070058 struct fixed_mdio_bus *fmb = bus->priv;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030059 struct fixed_phy *fp;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070060
Vitaly Borduga79d8e92007-12-07 01:51:22 +030061 list_for_each_entry(fp, &fmb->phys, node) {
Thomas Petazzoni9b744942014-05-16 16:14:03 +020062 if (fp->addr == phy_addr) {
Russell Kingbf7afb22016-06-23 14:50:25 +010063 struct fixed_phy_status state;
64 int s;
65
66 do {
67 s = read_seqcount_begin(&fp->seqcount);
68 /* Issue callback if user registered it. */
69 if (fp->link_update) {
70 fp->link_update(fp->phydev->attached_dev,
71 &fp->status);
72 fixed_phy_update(fp);
73 }
74 state = fp->status;
75 } while (read_seqcount_retry(&fp->seqcount, s));
76
77 return swphy_read_reg(reg_num, &state);
Vitaly Borduga79d8e92007-12-07 01:51:22 +030078 }
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070079 }
80
Vitaly Borduga79d8e92007-12-07 01:51:22 +030081 return 0xFFFF;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070082}
83
Thomas Petazzoni9b744942014-05-16 16:14:03 +020084static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
Vitaly Borduga79d8e92007-12-07 01:51:22 +030085 u16 val)
86{
87 return 0;
88}
89
90/*
91 * If something weird is required to be done with link/speed,
92 * network driver is able to assign a function to implement this.
93 * May be useful for PHY's that need to be software-driven.
94 */
95int fixed_phy_set_link_update(struct phy_device *phydev,
96 int (*link_update)(struct net_device *,
97 struct fixed_phy_status *))
98{
99 struct fixed_mdio_bus *fmb = &platform_fmb;
100 struct fixed_phy *fp;
101
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100102 if (!phydev || !phydev->mdio.bus)
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300103 return -EINVAL;
104
105 list_for_each_entry(fp, &fmb->phys, node) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100106 if (fp->addr == phydev->mdio.addr) {
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300107 fp->link_update = link_update;
108 fp->phydev = phydev;
109 return 0;
110 }
111 }
112
113 return -ENOENT;
114}
115EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
116
Stas Sergeeva3bebdc2015-04-01 20:30:31 +0300117int fixed_phy_update_state(struct phy_device *phydev,
118 const struct fixed_phy_status *status,
119 const struct fixed_phy_status *changed)
120{
121 struct fixed_mdio_bus *fmb = &platform_fmb;
122 struct fixed_phy *fp;
123
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100124 if (!phydev || phydev->mdio.bus != fmb->mii_bus)
Stas Sergeeva3bebdc2015-04-01 20:30:31 +0300125 return -EINVAL;
126
127 list_for_each_entry(fp, &fmb->phys, node) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100128 if (fp->addr == phydev->mdio.addr) {
Russell Kingbf7afb22016-06-23 14:50:25 +0100129 write_seqcount_begin(&fp->seqcount);
Stas Sergeeva3bebdc2015-04-01 20:30:31 +0300130#define _UPD(x) if (changed->x) \
131 fp->status.x = status->x
132 _UPD(link);
133 _UPD(speed);
134 _UPD(duplex);
135 _UPD(pause);
136 _UPD(asym_pause);
137#undef _UPD
Russell King37688e32016-06-23 14:50:20 +0100138 fixed_phy_update(fp);
Russell Kingbf7afb22016-06-23 14:50:25 +0100139 write_seqcount_end(&fp->seqcount);
Stas Sergeeva3bebdc2015-04-01 20:30:31 +0300140 return 0;
141 }
142 }
143
144 return -ENOENT;
145}
146EXPORT_SYMBOL(fixed_phy_update_state);
147
Thomas Petazzoni9b744942014-05-16 16:14:03 +0200148int fixed_phy_add(unsigned int irq, int phy_addr,
Andrew Lunna5597002015-08-31 15:56:53 +0200149 struct fixed_phy_status *status,
150 int link_gpio)
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300151{
152 int ret;
153 struct fixed_mdio_bus *fmb = &platform_fmb;
154 struct fixed_phy *fp;
155
Russell King68888ce2016-06-23 14:50:15 +0100156 ret = swphy_validate_state(status);
157 if (ret < 0)
158 return ret;
159
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300160 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
161 if (!fp)
162 return -ENOMEM;
163
Russell Kingbf7afb22016-06-23 14:50:25 +0100164 seqcount_init(&fp->seqcount);
165
Rabin Vincent185be5a2016-05-18 12:47:31 +0200166 if (irq != PHY_POLL)
167 fmb->mii_bus->irq[phy_addr] = irq;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300168
Thomas Petazzoni9b744942014-05-16 16:14:03 +0200169 fp->addr = phy_addr;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300170 fp->status = *status;
Andrew Lunna5597002015-08-31 15:56:53 +0200171 fp->link_gpio = link_gpio;
172
173 if (gpio_is_valid(fp->link_gpio)) {
174 ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN,
175 "fixed-link-gpio-link");
176 if (ret)
177 goto err_regs;
178 }
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300179
Russell King37688e32016-06-23 14:50:20 +0100180 fixed_phy_update(fp);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300181
182 list_add_tail(&fp->node, &fmb->phys);
183
184 return 0;
185
186err_regs:
187 kfree(fp);
188 return ret;
189}
190EXPORT_SYMBOL_GPL(fixed_phy_add);
191
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100192static void fixed_phy_del(int phy_addr)
Thomas Petazzonia7595122014-05-16 16:14:04 +0200193{
194 struct fixed_mdio_bus *fmb = &platform_fmb;
195 struct fixed_phy *fp, *tmp;
196
197 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
198 if (fp->addr == phy_addr) {
199 list_del(&fp->node);
Andrew Lunna5597002015-08-31 15:56:53 +0200200 if (gpio_is_valid(fp->link_gpio))
201 gpio_free(fp->link_gpio);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200202 kfree(fp);
203 return;
204 }
205 }
206}
Thomas Petazzonia7595122014-05-16 16:14:04 +0200207
208static int phy_fixed_addr;
209static DEFINE_SPINLOCK(phy_fixed_addr_lock);
210
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700211struct phy_device *fixed_phy_register(unsigned int irq,
212 struct fixed_phy_status *status,
Andrew Lunna5597002015-08-31 15:56:53 +0200213 int link_gpio,
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700214 struct device_node *np)
Thomas Petazzonia7595122014-05-16 16:14:04 +0200215{
216 struct fixed_mdio_bus *fmb = &platform_fmb;
217 struct phy_device *phy;
218 int phy_addr;
219 int ret;
220
Rabin Vincent185be5a2016-05-18 12:47:31 +0200221 if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
222 return ERR_PTR(-EPROBE_DEFER);
223
Thomas Petazzonia7595122014-05-16 16:14:04 +0200224 /* Get the next available PHY address, up to PHY_MAX_ADDR */
225 spin_lock(&phy_fixed_addr_lock);
226 if (phy_fixed_addr == PHY_MAX_ADDR) {
227 spin_unlock(&phy_fixed_addr_lock);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700228 return ERR_PTR(-ENOSPC);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200229 }
230 phy_addr = phy_fixed_addr++;
231 spin_unlock(&phy_fixed_addr_lock);
232
Sergei Shtylyovbd1a05e2015-09-03 23:22:16 +0300233 ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200234 if (ret < 0)
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700235 return ERR_PTR(ret);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200236
237 phy = get_phy_device(fmb->mii_bus, phy_addr, false);
Sergei Shtylyov4914a582016-04-24 20:29:23 +0300238 if (IS_ERR(phy)) {
Thomas Petazzonia7595122014-05-16 16:14:04 +0200239 fixed_phy_del(phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700240 return ERR_PTR(-EINVAL);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200241 }
242
Madalin Bucur4b195362015-08-26 17:58:47 +0300243 /* propagate the fixed link values to struct phy_device */
244 phy->link = status->link;
245 if (status->link) {
246 phy->speed = status->speed;
247 phy->duplex = status->duplex;
248 phy->pause = status->pause;
249 phy->asym_pause = status->asym_pause;
250 }
251
Thomas Petazzonia7595122014-05-16 16:14:04 +0200252 of_node_get(np);
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100253 phy->mdio.dev.of_node = np;
Florian Fainelli5a11dd72015-08-31 15:56:46 +0200254 phy->is_pseudo_fixed_link = true;
Thomas Petazzonia7595122014-05-16 16:14:04 +0200255
Andrew Lunn34b31da42015-08-31 15:56:48 +0200256 switch (status->speed) {
257 case SPEED_1000:
258 phy->supported = PHY_1000BT_FEATURES;
259 break;
260 case SPEED_100:
261 phy->supported = PHY_100BT_FEATURES;
262 break;
263 case SPEED_10:
264 default:
265 phy->supported = PHY_10BT_FEATURES;
266 }
267
Thomas Petazzonia7595122014-05-16 16:14:04 +0200268 ret = phy_device_register(phy);
269 if (ret) {
270 phy_device_free(phy);
271 of_node_put(np);
272 fixed_phy_del(phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700273 return ERR_PTR(ret);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200274 }
275
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700276 return phy;
Thomas Petazzonia7595122014-05-16 16:14:04 +0200277}
Mark Salter37e9a692014-12-11 23:03:26 -0500278EXPORT_SYMBOL_GPL(fixed_phy_register);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200279
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100280void fixed_phy_unregister(struct phy_device *phy)
281{
282 phy_device_remove(phy);
283
284 fixed_phy_del(phy->mdio.addr);
285}
286EXPORT_SYMBOL_GPL(fixed_phy_unregister);
287
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300288static int __init fixed_mdio_bus_init(void)
289{
290 struct fixed_mdio_bus *fmb = &platform_fmb;
291 int ret;
292
293 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
Dan Carpenter57401d52009-04-11 01:52:29 -0700294 if (IS_ERR(pdev)) {
295 ret = PTR_ERR(pdev);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300296 goto err_pdev;
297 }
298
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700299 fmb->mii_bus = mdiobus_alloc();
300 if (fmb->mii_bus == NULL) {
301 ret = -ENOMEM;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300302 goto err_mdiobus_reg;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700303 }
304
Florian Fainelli9e6c6432012-01-09 23:59:25 +0000305 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700306 fmb->mii_bus->name = "Fixed MDIO Bus";
Lennert Buytenhekec2a5652008-10-09 09:45:04 -0700307 fmb->mii_bus->priv = fmb;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700308 fmb->mii_bus->parent = &pdev->dev;
309 fmb->mii_bus->read = &fixed_mdio_read;
310 fmb->mii_bus->write = &fixed_mdio_write;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700311
312 ret = mdiobus_register(fmb->mii_bus);
313 if (ret)
314 goto err_mdiobus_alloc;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300315
316 return 0;
317
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700318err_mdiobus_alloc:
319 mdiobus_free(fmb->mii_bus);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300320err_mdiobus_reg:
321 platform_device_unregister(pdev);
322err_pdev:
323 return ret;
324}
325module_init(fixed_mdio_bus_init);
326
327static void __exit fixed_mdio_bus_exit(void)
328{
329 struct fixed_mdio_bus *fmb = &platform_fmb;
Adrian Bunk651be3a2008-02-02 23:15:02 +0200330 struct fixed_phy *fp, *tmp;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300331
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700332 mdiobus_unregister(fmb->mii_bus);
333 mdiobus_free(fmb->mii_bus);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300334 platform_device_unregister(pdev);
335
Adrian Bunk651be3a2008-02-02 23:15:02 +0200336 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300337 list_del(&fp->node);
338 kfree(fp);
339 }
340}
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");