blob: d84e30c4682461f3214ed5e04f5c68b25a897289 [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>
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 +030029#define MII_REGS_NUM 29
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070030
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 u16 regs[MII_REGS_NUM];
39 struct phy_device *phydev;
40 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 King68888ce2016-06-23 14:50:15 +010051static void fixed_phy_update_regs(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);
55
Russell King68888ce2016-06-23 14:50:15 +010056 swphy_update_regs(fp->regs, &fp->status);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070057}
58
Thomas Petazzoni9b744942014-05-16 16:14:03 +020059static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070060{
Lennert Buytenhekec2a5652008-10-09 09:45:04 -070061 struct fixed_mdio_bus *fmb = bus->priv;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030062 struct fixed_phy *fp;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070063
Vitaly Borduga79d8e92007-12-07 01:51:22 +030064 if (reg_num >= MII_REGS_NUM)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070065 return -1;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070066
Florian Fainellia2dbba72014-08-22 18:55:42 -070067 /* We do not support emulating Clause 45 over Clause 22 register reads
68 * return an error instead of bogus data.
69 */
70 switch (reg_num) {
71 case MII_MMD_CTRL:
72 case MII_MMD_DATA:
73 return -1;
74 default:
75 break;
76 }
77
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) {
Vitaly Borduga79d8e92007-12-07 01:51:22 +030080 /* Issue callback if user registered it. */
81 if (fp->link_update) {
82 fp->link_update(fp->phydev->attached_dev,
83 &fp->status);
84 fixed_phy_update_regs(fp);
85 }
86 return fp->regs[reg_num];
87 }
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070088 }
89
Vitaly Borduga79d8e92007-12-07 01:51:22 +030090 return 0xFFFF;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070091}
92
Thomas Petazzoni9b744942014-05-16 16:14:03 +020093static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
Vitaly Borduga79d8e92007-12-07 01:51:22 +030094 u16 val)
95{
96 return 0;
97}
98
99/*
100 * If something weird is required to be done with link/speed,
101 * network driver is able to assign a function to implement this.
102 * May be useful for PHY's that need to be software-driven.
103 */
104int fixed_phy_set_link_update(struct phy_device *phydev,
105 int (*link_update)(struct net_device *,
106 struct fixed_phy_status *))
107{
108 struct fixed_mdio_bus *fmb = &platform_fmb;
109 struct fixed_phy *fp;
110
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100111 if (!phydev || !phydev->mdio.bus)
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300112 return -EINVAL;
113
114 list_for_each_entry(fp, &fmb->phys, node) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100115 if (fp->addr == phydev->mdio.addr) {
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300116 fp->link_update = link_update;
117 fp->phydev = phydev;
118 return 0;
119 }
120 }
121
122 return -ENOENT;
123}
124EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
125
Stas Sergeeva3bebdc2015-04-01 20:30:31 +0300126int fixed_phy_update_state(struct phy_device *phydev,
127 const struct fixed_phy_status *status,
128 const struct fixed_phy_status *changed)
129{
130 struct fixed_mdio_bus *fmb = &platform_fmb;
131 struct fixed_phy *fp;
132
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100133 if (!phydev || phydev->mdio.bus != fmb->mii_bus)
Stas Sergeeva3bebdc2015-04-01 20:30:31 +0300134 return -EINVAL;
135
136 list_for_each_entry(fp, &fmb->phys, node) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100137 if (fp->addr == phydev->mdio.addr) {
Stas Sergeeva3bebdc2015-04-01 20:30:31 +0300138#define _UPD(x) if (changed->x) \
139 fp->status.x = status->x
140 _UPD(link);
141 _UPD(speed);
142 _UPD(duplex);
143 _UPD(pause);
144 _UPD(asym_pause);
145#undef _UPD
146 fixed_phy_update_regs(fp);
147 return 0;
148 }
149 }
150
151 return -ENOENT;
152}
153EXPORT_SYMBOL(fixed_phy_update_state);
154
Thomas Petazzoni9b744942014-05-16 16:14:03 +0200155int fixed_phy_add(unsigned int irq, int phy_addr,
Andrew Lunna5597002015-08-31 15:56:53 +0200156 struct fixed_phy_status *status,
157 int link_gpio)
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300158{
159 int ret;
160 struct fixed_mdio_bus *fmb = &platform_fmb;
161 struct fixed_phy *fp;
162
Russell King68888ce2016-06-23 14:50:15 +0100163 ret = swphy_validate_state(status);
164 if (ret < 0)
165 return ret;
166
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300167 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
168 if (!fp)
169 return -ENOMEM;
170
171 memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM);
172
Rabin Vincent185be5a2016-05-18 12:47:31 +0200173 if (irq != PHY_POLL)
174 fmb->mii_bus->irq[phy_addr] = irq;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300175
Thomas Petazzoni9b744942014-05-16 16:14:03 +0200176 fp->addr = phy_addr;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300177 fp->status = *status;
Andrew Lunna5597002015-08-31 15:56:53 +0200178 fp->link_gpio = link_gpio;
179
180 if (gpio_is_valid(fp->link_gpio)) {
181 ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN,
182 "fixed-link-gpio-link");
183 if (ret)
184 goto err_regs;
185 }
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300186
Russell King68888ce2016-06-23 14:50:15 +0100187 fixed_phy_update_regs(fp);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300188
189 list_add_tail(&fp->node, &fmb->phys);
190
191 return 0;
192
193err_regs:
194 kfree(fp);
195 return ret;
196}
197EXPORT_SYMBOL_GPL(fixed_phy_add);
198
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100199static void fixed_phy_del(int phy_addr)
Thomas Petazzonia7595122014-05-16 16:14:04 +0200200{
201 struct fixed_mdio_bus *fmb = &platform_fmb;
202 struct fixed_phy *fp, *tmp;
203
204 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
205 if (fp->addr == phy_addr) {
206 list_del(&fp->node);
Andrew Lunna5597002015-08-31 15:56:53 +0200207 if (gpio_is_valid(fp->link_gpio))
208 gpio_free(fp->link_gpio);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200209 kfree(fp);
210 return;
211 }
212 }
213}
Thomas Petazzonia7595122014-05-16 16:14:04 +0200214
215static int phy_fixed_addr;
216static DEFINE_SPINLOCK(phy_fixed_addr_lock);
217
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700218struct phy_device *fixed_phy_register(unsigned int irq,
219 struct fixed_phy_status *status,
Andrew Lunna5597002015-08-31 15:56:53 +0200220 int link_gpio,
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700221 struct device_node *np)
Thomas Petazzonia7595122014-05-16 16:14:04 +0200222{
223 struct fixed_mdio_bus *fmb = &platform_fmb;
224 struct phy_device *phy;
225 int phy_addr;
226 int ret;
227
Rabin Vincent185be5a2016-05-18 12:47:31 +0200228 if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
229 return ERR_PTR(-EPROBE_DEFER);
230
Thomas Petazzonia7595122014-05-16 16:14:04 +0200231 /* Get the next available PHY address, up to PHY_MAX_ADDR */
232 spin_lock(&phy_fixed_addr_lock);
233 if (phy_fixed_addr == PHY_MAX_ADDR) {
234 spin_unlock(&phy_fixed_addr_lock);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700235 return ERR_PTR(-ENOSPC);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200236 }
237 phy_addr = phy_fixed_addr++;
238 spin_unlock(&phy_fixed_addr_lock);
239
Sergei Shtylyovbd1a05e2015-09-03 23:22:16 +0300240 ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200241 if (ret < 0)
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700242 return ERR_PTR(ret);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200243
244 phy = get_phy_device(fmb->mii_bus, phy_addr, false);
Sergei Shtylyov4914a582016-04-24 20:29:23 +0300245 if (IS_ERR(phy)) {
Thomas Petazzonia7595122014-05-16 16:14:04 +0200246 fixed_phy_del(phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700247 return ERR_PTR(-EINVAL);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200248 }
249
Madalin Bucur4b195362015-08-26 17:58:47 +0300250 /* propagate the fixed link values to struct phy_device */
251 phy->link = status->link;
252 if (status->link) {
253 phy->speed = status->speed;
254 phy->duplex = status->duplex;
255 phy->pause = status->pause;
256 phy->asym_pause = status->asym_pause;
257 }
258
Thomas Petazzonia7595122014-05-16 16:14:04 +0200259 of_node_get(np);
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100260 phy->mdio.dev.of_node = np;
Florian Fainelli5a11dd72015-08-31 15:56:46 +0200261 phy->is_pseudo_fixed_link = true;
Thomas Petazzonia7595122014-05-16 16:14:04 +0200262
Andrew Lunn34b31da42015-08-31 15:56:48 +0200263 switch (status->speed) {
264 case SPEED_1000:
265 phy->supported = PHY_1000BT_FEATURES;
266 break;
267 case SPEED_100:
268 phy->supported = PHY_100BT_FEATURES;
269 break;
270 case SPEED_10:
271 default:
272 phy->supported = PHY_10BT_FEATURES;
273 }
274
Thomas Petazzonia7595122014-05-16 16:14:04 +0200275 ret = phy_device_register(phy);
276 if (ret) {
277 phy_device_free(phy);
278 of_node_put(np);
279 fixed_phy_del(phy_addr);
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700280 return ERR_PTR(ret);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200281 }
282
Petri Gyntherfd2ef0b2014-10-06 11:38:30 -0700283 return phy;
Thomas Petazzonia7595122014-05-16 16:14:04 +0200284}
Mark Salter37e9a692014-12-11 23:03:26 -0500285EXPORT_SYMBOL_GPL(fixed_phy_register);
Thomas Petazzonia7595122014-05-16 16:14:04 +0200286
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100287void fixed_phy_unregister(struct phy_device *phy)
288{
289 phy_device_remove(phy);
290
291 fixed_phy_del(phy->mdio.addr);
292}
293EXPORT_SYMBOL_GPL(fixed_phy_unregister);
294
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300295static int __init fixed_mdio_bus_init(void)
296{
297 struct fixed_mdio_bus *fmb = &platform_fmb;
298 int ret;
299
300 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
Dan Carpenter57401d52009-04-11 01:52:29 -0700301 if (IS_ERR(pdev)) {
302 ret = PTR_ERR(pdev);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300303 goto err_pdev;
304 }
305
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700306 fmb->mii_bus = mdiobus_alloc();
307 if (fmb->mii_bus == NULL) {
308 ret = -ENOMEM;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300309 goto err_mdiobus_reg;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700310 }
311
Florian Fainelli9e6c6432012-01-09 23:59:25 +0000312 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700313 fmb->mii_bus->name = "Fixed MDIO Bus";
Lennert Buytenhekec2a5652008-10-09 09:45:04 -0700314 fmb->mii_bus->priv = fmb;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700315 fmb->mii_bus->parent = &pdev->dev;
316 fmb->mii_bus->read = &fixed_mdio_read;
317 fmb->mii_bus->write = &fixed_mdio_write;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700318
319 ret = mdiobus_register(fmb->mii_bus);
320 if (ret)
321 goto err_mdiobus_alloc;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300322
323 return 0;
324
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700325err_mdiobus_alloc:
326 mdiobus_free(fmb->mii_bus);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300327err_mdiobus_reg:
328 platform_device_unregister(pdev);
329err_pdev:
330 return ret;
331}
332module_init(fixed_mdio_bus_init);
333
334static void __exit fixed_mdio_bus_exit(void)
335{
336 struct fixed_mdio_bus *fmb = &platform_fmb;
Adrian Bunk651be3a2008-02-02 23:15:02 +0200337 struct fixed_phy *fp, *tmp;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300338
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700339 mdiobus_unregister(fmb->mii_bus);
340 mdiobus_free(fmb->mii_bus);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300341 platform_device_unregister(pdev);
342
Adrian Bunk651be3a2008-02-02 23:15:02 +0200343 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300344 list_del(&fp->node);
345 kfree(fp);
346 }
347}
348module_exit(fixed_mdio_bus_exit);
349
350MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700351MODULE_AUTHOR("Vitaly Bordug");
352MODULE_LICENSE("GPL");