blob: 001fe1df75572687e527c34eb608370d446bc670 [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 Buytenhek298cf9b2008-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
Thomas Petazzoni9b744942014-05-16 16:14:03 +0200118int fixed_phy_add(unsigned int irq, int phy_addr,
Andrew Lunna5597002015-08-31 15:56:53 +0200119 struct fixed_phy_status *status,
120 int link_gpio)
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300121{
122 int ret;
123 struct fixed_mdio_bus *fmb = &platform_fmb;
124 struct fixed_phy *fp;
125
Russell King68888ce2016-06-23 14:50:15 +0100126 ret = swphy_validate_state(status);
127 if (ret < 0)
128 return ret;
129
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300130 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
131 if (!fp)
132 return -ENOMEM;
133
Russell Kingbf7afb22016-06-23 14:50:25 +0100134 seqcount_init(&fp->seqcount);
135
Rabin Vincent185be5a2016-05-18 12:47:31 +0200136 if (irq != PHY_POLL)
137 fmb->mii_bus->irq[phy_addr] = irq;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300138
Thomas Petazzoni9b744942014-05-16 16:14:03 +0200139 fp->addr = phy_addr;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300140 fp->status = *status;
Andrew Lunna5597002015-08-31 15:56:53 +0200141 fp->link_gpio = link_gpio;
142
143 if (gpio_is_valid(fp->link_gpio)) {
144 ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN,
145 "fixed-link-gpio-link");
146 if (ret)
147 goto err_regs;
148 }
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300149
Russell King37688e32016-06-23 14:50:20 +0100150 fixed_phy_update(fp);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300151
152 list_add_tail(&fp->node, &fmb->phys);
153
154 return 0;
155
156err_regs:
157 kfree(fp);
158 return ret;
159}
160EXPORT_SYMBOL_GPL(fixed_phy_add);
161
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700162static DEFINE_IDA(phy_fixed_ida);
163
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100164static void fixed_phy_del(int phy_addr)
Thomas Petazzonia7595122014-05-16 16:14:04 +0200165{
166 struct fixed_mdio_bus *fmb = &platform_fmb;
167 struct fixed_phy *fp, *tmp;
168
169 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
170 if (fp->addr == phy_addr) {
171 list_del(&fp->node);
Andrew Lunna5597002015-08-31 15:56:53 +0200172 if (gpio_is_valid(fp->link_gpio))
173 gpio_free(fp->link_gpio);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200174 kfree(fp);
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700175 ida_simple_remove(&phy_fixed_ida, phy_addr);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200176 return;
177 }
178 }
179}
Thomas Petazzonia7595122014-05-16 16:14:04 +0200180
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700181struct phy_device *fixed_phy_register(unsigned int irq,
182 struct fixed_phy_status *status,
Andrew Lunna5597002015-08-31 15:56:53 +0200183 int link_gpio,
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700184 struct device_node *np)
Thomas Petazzonia7595122014-05-16 16:14:04 +0200185{
186 struct fixed_mdio_bus *fmb = &platform_fmb;
187 struct phy_device *phy;
188 int phy_addr;
189 int ret;
190
Rabin Vincent185be5a2016-05-18 12:47:31 +0200191 if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
192 return ERR_PTR(-EPROBE_DEFER);
193
Thomas Petazzonia7595122014-05-16 16:14:04 +0200194 /* Get the next available PHY address, up to PHY_MAX_ADDR */
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700195 phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL);
196 if (phy_addr < 0)
197 return ERR_PTR(phy_addr);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200198
Sergei Shtylyovbd1a05e2015-09-03 23:22:16 +0300199 ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700200 if (ret < 0) {
201 ida_simple_remove(&phy_fixed_ida, phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700202 return ERR_PTR(ret);
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700203 }
Thomas Petazzonia7595122014-05-16 16:14:04 +0200204
205 phy = get_phy_device(fmb->mii_bus, phy_addr, false);
Sergei Shtylyov4914a582016-04-24 20:29:23 +0300206 if (IS_ERR(phy)) {
Thomas Petazzonia7595122014-05-16 16:14:04 +0200207 fixed_phy_del(phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700208 return ERR_PTR(-EINVAL);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200209 }
210
Madalin Bucur4b195362015-08-26 17:58:47 +0300211 /* propagate the fixed link values to struct phy_device */
212 phy->link = status->link;
213 if (status->link) {
214 phy->speed = status->speed;
215 phy->duplex = status->duplex;
216 phy->pause = status->pause;
217 phy->asym_pause = status->asym_pause;
218 }
219
Thomas Petazzonia7595122014-05-16 16:14:04 +0200220 of_node_get(np);
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100221 phy->mdio.dev.of_node = np;
Florian Fainelli5a11dd72015-08-31 15:56:46 +0200222 phy->is_pseudo_fixed_link = true;
Thomas Petazzonia7595122014-05-16 16:14:04 +0200223
Andrew Lunn34b31da42015-08-31 15:56:48 +0200224 switch (status->speed) {
225 case SPEED_1000:
226 phy->supported = PHY_1000BT_FEATURES;
227 break;
228 case SPEED_100:
229 phy->supported = PHY_100BT_FEATURES;
230 break;
231 case SPEED_10:
232 default:
233 phy->supported = PHY_10BT_FEATURES;
234 }
235
Thomas Petazzonia7595122014-05-16 16:14:04 +0200236 ret = phy_device_register(phy);
237 if (ret) {
238 phy_device_free(phy);
239 of_node_put(np);
240 fixed_phy_del(phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700241 return ERR_PTR(ret);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200242 }
243
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700244 return phy;
Thomas Petazzonia7595122014-05-16 16:14:04 +0200245}
Mark Salter37e9a692014-12-11 23:03:26 -0500246EXPORT_SYMBOL_GPL(fixed_phy_register);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200247
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100248void fixed_phy_unregister(struct phy_device *phy)
249{
250 phy_device_remove(phy);
Johan Hovold13c9d932016-11-16 15:20:38 +0100251 of_node_put(phy->mdio.dev.of_node);
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100252 fixed_phy_del(phy->mdio.addr);
253}
254EXPORT_SYMBOL_GPL(fixed_phy_unregister);
255
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300256static int __init fixed_mdio_bus_init(void)
257{
258 struct fixed_mdio_bus *fmb = &platform_fmb;
259 int ret;
260
261 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
Dan Carpenter57401d52009-04-11 01:52:29 -0700262 if (IS_ERR(pdev)) {
263 ret = PTR_ERR(pdev);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300264 goto err_pdev;
265 }
266
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700267 fmb->mii_bus = mdiobus_alloc();
268 if (fmb->mii_bus == NULL) {
269 ret = -ENOMEM;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300270 goto err_mdiobus_reg;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700271 }
272
Florian Fainelli9e6c6432012-01-09 23:59:25 +0000273 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700274 fmb->mii_bus->name = "Fixed MDIO Bus";
Lennert Buytenhekec2a5652008-10-09 09:45:04 -0700275 fmb->mii_bus->priv = fmb;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700276 fmb->mii_bus->parent = &pdev->dev;
277 fmb->mii_bus->read = &fixed_mdio_read;
278 fmb->mii_bus->write = &fixed_mdio_write;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700279
280 ret = mdiobus_register(fmb->mii_bus);
281 if (ret)
282 goto err_mdiobus_alloc;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300283
284 return 0;
285
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700286err_mdiobus_alloc:
287 mdiobus_free(fmb->mii_bus);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300288err_mdiobus_reg:
289 platform_device_unregister(pdev);
290err_pdev:
291 return ret;
292}
293module_init(fixed_mdio_bus_init);
294
295static void __exit fixed_mdio_bus_exit(void)
296{
297 struct fixed_mdio_bus *fmb = &platform_fmb;
Adrian Bunk651be3a2008-02-02 23:15:02 +0200298 struct fixed_phy *fp, *tmp;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300299
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700300 mdiobus_unregister(fmb->mii_bus);
301 mdiobus_free(fmb->mii_bus);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300302 platform_device_unregister(pdev);
303
Adrian Bunk651be3a2008-02-02 23:15:02 +0200304 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300305 list_del(&fp->node);
306 kfree(fp);
307 }
Florian Fainelli69fc58a2016-06-24 16:25:24 -0700308 ida_destroy(&phy_fixed_ida);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300309}
310module_exit(fixed_mdio_bus_exit);
311
312MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700313MODULE_AUTHOR("Vitaly Bordug");
314MODULE_LICENSE("GPL");