Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
  4 *
  5 * Author: Vitaly Bordug <vbordug@ru.mvista.com>
  6 *         Anton Vorontsov <avorontsov@ru.mvista.com>
  7 *
  8 * Copyright (c) 2006-2007 MontaVista Software, Inc.
 
 
 
 
 
  9 */
 10
 11#include <linux/kernel.h>
 12#include <linux/module.h>
 13#include <linux/platform_device.h>
 14#include <linux/list.h>
 15#include <linux/mii.h>
 16#include <linux/phy.h>
 17#include <linux/phy_fixed.h>
 18#include <linux/err.h>
 19#include <linux/slab.h>
 20#include <linux/of.h>
 21#include <linux/gpio/consumer.h>
 22#include <linux/seqlock.h>
 23#include <linux/idr.h>
 24#include <linux/netdevice.h>
 25#include <linux/linkmode.h>
 26
 27#include "swphy.h"
 28
 29struct fixed_mdio_bus {
 30	struct mii_bus *mii_bus;
 31	struct list_head phys;
 32};
 33
 34struct fixed_phy {
 35	int addr;
 
 36	struct phy_device *phydev;
 37	seqcount_t seqcount;
 38	struct fixed_phy_status status;
 39	bool no_carrier;
 40	int (*link_update)(struct net_device *, struct fixed_phy_status *);
 41	struct list_head node;
 42	struct gpio_desc *link_gpiod;
 43};
 44
 45static struct platform_device *pdev;
 46static struct fixed_mdio_bus platform_fmb = {
 47	.phys = LIST_HEAD_INIT(platform_fmb.phys),
 48};
 49
 50int fixed_phy_change_carrier(struct net_device *dev, bool new_carrier)
 51{
 52	struct fixed_mdio_bus *fmb = &platform_fmb;
 53	struct phy_device *phydev = dev->phydev;
 54	struct fixed_phy *fp;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 55
 56	if (!phydev || !phydev->mdio.bus)
 57		return -EINVAL;
 58
 59	list_for_each_entry(fp, &fmb->phys, node) {
 60		if (fp->addr == phydev->mdio.addr) {
 61			fp->no_carrier = !new_carrier;
 62			return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 63		}
 
 
 
 
 
 
 64	}
 65	return -EINVAL;
 66}
 67EXPORT_SYMBOL_GPL(fixed_phy_change_carrier);
 68
 69static void fixed_phy_update(struct fixed_phy *fp)
 70{
 71	if (!fp->no_carrier && fp->link_gpiod)
 72		fp->status.link = !!gpiod_get_value_cansleep(fp->link_gpiod);
 
 
 
 
 
 73}
 74
 75static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
 76{
 77	struct fixed_mdio_bus *fmb = bus->priv;
 78	struct fixed_phy *fp;
 79
 80	list_for_each_entry(fp, &fmb->phys, node) {
 81		if (fp->addr == phy_addr) {
 82			struct fixed_phy_status state;
 83			int s;
 84
 85			do {
 86				s = read_seqcount_begin(&fp->seqcount);
 87				fp->status.link = !fp->no_carrier;
 88				/* Issue callback if user registered it. */
 89				if (fp->link_update)
 90					fp->link_update(fp->phydev->attached_dev,
 91							&fp->status);
 92				/* Check the GPIO for change in status */
 93				fixed_phy_update(fp);
 94				state = fp->status;
 95			} while (read_seqcount_retry(&fp->seqcount, s));
 96
 97			return swphy_read_reg(reg_num, &state);
 
 
 
 
 
 
 
 
 98		}
 99	}
100
101	return 0xFFFF;
102}
103
104static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
105			    u16 val)
106{
107	return 0;
108}
109
110/*
111 * If something weird is required to be done with link/speed,
112 * network driver is able to assign a function to implement this.
113 * May be useful for PHY's that need to be software-driven.
114 */
115int fixed_phy_set_link_update(struct phy_device *phydev,
116			      int (*link_update)(struct net_device *,
117						 struct fixed_phy_status *))
118{
119	struct fixed_mdio_bus *fmb = &platform_fmb;
120	struct fixed_phy *fp;
121
122	if (!phydev || !phydev->mdio.bus)
123		return -EINVAL;
124
125	list_for_each_entry(fp, &fmb->phys, node) {
126		if (fp->addr == phydev->mdio.addr) {
127			fp->link_update = link_update;
128			fp->phydev = phydev;
129			return 0;
130		}
131	}
132
133	return -ENOENT;
134}
135EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
136
137static int fixed_phy_add_gpiod(unsigned int irq, int phy_addr,
138			       struct fixed_phy_status *status,
139			       struct gpio_desc *gpiod)
140{
141	int ret;
142	struct fixed_mdio_bus *fmb = &platform_fmb;
143	struct fixed_phy *fp;
144
145	ret = swphy_validate_state(status);
146	if (ret < 0)
147		return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
149	fp = kzalloc(sizeof(*fp), GFP_KERNEL);
150	if (!fp)
151		return -ENOMEM;
152
153	seqcount_init(&fp->seqcount);
154
155	if (irq != PHY_POLL)
156		fmb->mii_bus->irq[phy_addr] = irq;
157
158	fp->addr = phy_addr;
159	fp->status = *status;
160	fp->link_gpiod = gpiod;
 
 
 
 
 
 
 
161
162	fixed_phy_update(fp);
 
 
163
164	list_add_tail(&fp->node, &fmb->phys);
165
166	return 0;
167}
168
169int fixed_phy_add(unsigned int irq, int phy_addr,
170		  struct fixed_phy_status *status) {
171
172	return fixed_phy_add_gpiod(irq, phy_addr, status, NULL);
 
 
173}
174EXPORT_SYMBOL_GPL(fixed_phy_add);
175
176static DEFINE_IDA(phy_fixed_ida);
177
178static void fixed_phy_del(int phy_addr)
179{
180	struct fixed_mdio_bus *fmb = &platform_fmb;
181	struct fixed_phy *fp, *tmp;
182
183	list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
184		if (fp->addr == phy_addr) {
185			list_del(&fp->node);
186			if (fp->link_gpiod)
187				gpiod_put(fp->link_gpiod);
188			kfree(fp);
189			ida_simple_remove(&phy_fixed_ida, phy_addr);
190			return;
191		}
192	}
193}
194
195#ifdef CONFIG_OF_GPIO
196static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np)
197{
198	struct device_node *fixed_link_node;
199	struct gpio_desc *gpiod;
200
201	if (!np)
202		return NULL;
203
204	fixed_link_node = of_get_child_by_name(np, "fixed-link");
205	if (!fixed_link_node)
206		return NULL;
207
208	/*
209	 * As the fixed link is just a device tree node without any
210	 * Linux device associated with it, we simply have obtain
211	 * the GPIO descriptor from the device tree like this.
212	 */
213	gpiod = gpiod_get_from_of_node(fixed_link_node, "link-gpios", 0,
214				       GPIOD_IN, "mdio");
215	of_node_put(fixed_link_node);
216	if (IS_ERR(gpiod)) {
217		if (PTR_ERR(gpiod) == -EPROBE_DEFER)
218			return gpiod;
219
220		if (PTR_ERR(gpiod) != -ENOENT)
221			pr_err("error getting GPIO for fixed link %pOF, proceed without\n",
222			       fixed_link_node);
223		gpiod = NULL;
224	}
225
226	return gpiod;
227}
228#else
229static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np)
230{
231	return NULL;
232}
233#endif
234
235static struct phy_device *__fixed_phy_register(unsigned int irq,
236					       struct fixed_phy_status *status,
237					       struct device_node *np,
238					       struct gpio_desc *gpiod)
239{
240	struct fixed_mdio_bus *fmb = &platform_fmb;
241	struct phy_device *phy;
242	int phy_addr;
243	int ret;
244
245	if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
246		return ERR_PTR(-EPROBE_DEFER);
247
248	/* Check if we have a GPIO associated with this fixed phy */
249	if (!gpiod) {
250		gpiod = fixed_phy_get_gpiod(np);
251		if (IS_ERR(gpiod))
252			return ERR_CAST(gpiod);
253	}
 
 
254
255	/* Get the next available PHY address, up to PHY_MAX_ADDR */
256	phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL);
257	if (phy_addr < 0)
258		return ERR_PTR(phy_addr);
259
260	ret = fixed_phy_add_gpiod(irq, phy_addr, status, gpiod);
261	if (ret < 0) {
262		ida_simple_remove(&phy_fixed_ida, phy_addr);
263		return ERR_PTR(ret);
264	}
265
266	phy = get_phy_device(fmb->mii_bus, phy_addr, false);
267	if (IS_ERR(phy)) {
268		fixed_phy_del(phy_addr);
269		return ERR_PTR(-EINVAL);
270	}
271
272	/* propagate the fixed link values to struct phy_device */
273	phy->link = status->link;
274	if (status->link) {
275		phy->speed = status->speed;
276		phy->duplex = status->duplex;
277		phy->pause = status->pause;
278		phy->asym_pause = status->asym_pause;
279	}
280
281	of_node_get(np);
282	phy->mdio.dev.of_node = np;
283	phy->is_pseudo_fixed_link = true;
284
285	switch (status->speed) {
286	case SPEED_1000:
287		linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
288				 phy->supported);
289		linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
290				 phy->supported);
291		/* fall through */
292	case SPEED_100:
293		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
294				 phy->supported);
295		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
296				 phy->supported);
297		/* fall through */
298	case SPEED_10:
299	default:
300		linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
301				 phy->supported);
302		linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
303				 phy->supported);
304	}
305
306	phy_advertise_supported(phy);
307
308	ret = phy_device_register(phy);
309	if (ret) {
310		phy_device_free(phy);
311		of_node_put(np);
312		fixed_phy_del(phy_addr);
313		return ERR_PTR(ret);
314	}
315
316	return phy;
317}
318
319struct phy_device *fixed_phy_register(unsigned int irq,
320				      struct fixed_phy_status *status,
321				      struct device_node *np)
322{
323	return __fixed_phy_register(irq, status, np, NULL);
324}
325EXPORT_SYMBOL_GPL(fixed_phy_register);
326
327struct phy_device *
328fixed_phy_register_with_gpiod(unsigned int irq,
329			      struct fixed_phy_status *status,
330			      struct gpio_desc *gpiod)
331{
332	return __fixed_phy_register(irq, status, NULL, gpiod);
333}
334EXPORT_SYMBOL_GPL(fixed_phy_register_with_gpiod);
335
336void fixed_phy_unregister(struct phy_device *phy)
337{
338	phy_device_remove(phy);
339	of_node_put(phy->mdio.dev.of_node);
340	fixed_phy_del(phy->mdio.addr);
341}
342EXPORT_SYMBOL_GPL(fixed_phy_unregister);
343
344static int __init fixed_mdio_bus_init(void)
345{
346	struct fixed_mdio_bus *fmb = &platform_fmb;
347	int ret;
348
349	pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
350	if (IS_ERR(pdev))
351		return PTR_ERR(pdev);
 
 
352
353	fmb->mii_bus = mdiobus_alloc();
354	if (fmb->mii_bus == NULL) {
355		ret = -ENOMEM;
356		goto err_mdiobus_reg;
357	}
358
359	snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
360	fmb->mii_bus->name = "Fixed MDIO Bus";
361	fmb->mii_bus->priv = fmb;
362	fmb->mii_bus->parent = &pdev->dev;
363	fmb->mii_bus->read = &fixed_mdio_read;
364	fmb->mii_bus->write = &fixed_mdio_write;
365
366	ret = mdiobus_register(fmb->mii_bus);
367	if (ret)
368		goto err_mdiobus_alloc;
369
370	return 0;
371
372err_mdiobus_alloc:
373	mdiobus_free(fmb->mii_bus);
374err_mdiobus_reg:
375	platform_device_unregister(pdev);
 
376	return ret;
377}
378module_init(fixed_mdio_bus_init);
379
380static void __exit fixed_mdio_bus_exit(void)
381{
382	struct fixed_mdio_bus *fmb = &platform_fmb;
383	struct fixed_phy *fp, *tmp;
384
385	mdiobus_unregister(fmb->mii_bus);
386	mdiobus_free(fmb->mii_bus);
387	platform_device_unregister(pdev);
388
389	list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
390		list_del(&fp->node);
391		kfree(fp);
392	}
393	ida_destroy(&phy_fixed_ida);
394}
395module_exit(fixed_mdio_bus_exit);
396
397MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
398MODULE_AUTHOR("Vitaly Bordug");
399MODULE_LICENSE("GPL");
v4.6
 
  1/*
  2 * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
  3 *
  4 * Author: Vitaly Bordug <vbordug@ru.mvista.com>
  5 *         Anton Vorontsov <avorontsov@ru.mvista.com>
  6 *
  7 * Copyright (c) 2006-2007 MontaVista Software, Inc.
  8 *
  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.
 13 */
 14
 15#include <linux/kernel.h>
 16#include <linux/module.h>
 17#include <linux/platform_device.h>
 18#include <linux/list.h>
 19#include <linux/mii.h>
 20#include <linux/phy.h>
 21#include <linux/phy_fixed.h>
 22#include <linux/err.h>
 23#include <linux/slab.h>
 24#include <linux/of.h>
 25#include <linux/gpio.h>
 
 
 
 
 26
 27#define MII_REGS_NUM 29
 28
 29struct fixed_mdio_bus {
 30	struct mii_bus *mii_bus;
 31	struct list_head phys;
 32};
 33
 34struct fixed_phy {
 35	int addr;
 36	u16 regs[MII_REGS_NUM];
 37	struct phy_device *phydev;
 
 38	struct fixed_phy_status status;
 
 39	int (*link_update)(struct net_device *, struct fixed_phy_status *);
 40	struct list_head node;
 41	int link_gpio;
 42};
 43
 44static struct platform_device *pdev;
 45static struct fixed_mdio_bus platform_fmb = {
 46	.phys = LIST_HEAD_INIT(platform_fmb.phys),
 47};
 48
 49static int fixed_phy_update_regs(struct fixed_phy *fp)
 50{
 51	u16 bmsr = BMSR_ANEGCAPABLE;
 52	u16 bmcr = 0;
 53	u16 lpagb = 0;
 54	u16 lpa = 0;
 55
 56	if (gpio_is_valid(fp->link_gpio))
 57		fp->status.link = !!gpio_get_value_cansleep(fp->link_gpio);
 58
 59	if (fp->status.duplex) {
 60		switch (fp->status.speed) {
 61		case 1000:
 62			bmsr |= BMSR_ESTATEN;
 63			break;
 64		case 100:
 65			bmsr |= BMSR_100FULL;
 66			break;
 67		case 10:
 68			bmsr |= BMSR_10FULL;
 69			break;
 70		default:
 71			break;
 72		}
 73	} else {
 74		switch (fp->status.speed) {
 75		case 1000:
 76			bmsr |= BMSR_ESTATEN;
 77			break;
 78		case 100:
 79			bmsr |= BMSR_100HALF;
 80			break;
 81		case 10:
 82			bmsr |= BMSR_10HALF;
 83			break;
 84		default:
 85			break;
 86		}
 87	}
 88
 89	if (fp->status.link) {
 90		bmsr |= BMSR_LSTATUS | BMSR_ANEGCOMPLETE;
 91
 92		if (fp->status.duplex) {
 93			bmcr |= BMCR_FULLDPLX;
 94
 95			switch (fp->status.speed) {
 96			case 1000:
 97				bmcr |= BMCR_SPEED1000;
 98				lpagb |= LPA_1000FULL;
 99				break;
100			case 100:
101				bmcr |= BMCR_SPEED100;
102				lpa |= LPA_100FULL;
103				break;
104			case 10:
105				lpa |= LPA_10FULL;
106				break;
107			default:
108				pr_warn("fixed phy: unknown speed\n");
109				return -EINVAL;
110			}
111		} else {
112			switch (fp->status.speed) {
113			case 1000:
114				bmcr |= BMCR_SPEED1000;
115				lpagb |= LPA_1000HALF;
116				break;
117			case 100:
118				bmcr |= BMCR_SPEED100;
119				lpa |= LPA_100HALF;
120				break;
121			case 10:
122				lpa |= LPA_10HALF;
123				break;
124			default:
125				pr_warn("fixed phy: unknown speed\n");
126			return -EINVAL;
127			}
128		}
129
130		if (fp->status.pause)
131			lpa |= LPA_PAUSE_CAP;
132
133		if (fp->status.asym_pause)
134			lpa |= LPA_PAUSE_ASYM;
135	}
 
 
 
136
137	fp->regs[MII_PHYSID1] = 0;
138	fp->regs[MII_PHYSID2] = 0;
139
140	fp->regs[MII_BMSR] = bmsr;
141	fp->regs[MII_BMCR] = bmcr;
142	fp->regs[MII_LPA] = lpa;
143	fp->regs[MII_STAT1000] = lpagb;
144
145	return 0;
146}
147
148static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
149{
150	struct fixed_mdio_bus *fmb = bus->priv;
151	struct fixed_phy *fp;
152
153	if (reg_num >= MII_REGS_NUM)
154		return -1;
 
 
155
156	/* We do not support emulating Clause 45 over Clause 22 register reads
157	 * return an error instead of bogus data.
158	 */
159	switch (reg_num) {
160	case MII_MMD_CTRL:
161	case MII_MMD_DATA:
162		return -1;
163	default:
164		break;
165	}
 
166
167	list_for_each_entry(fp, &fmb->phys, node) {
168		if (fp->addr == phy_addr) {
169			/* Issue callback if user registered it. */
170			if (fp->link_update) {
171				fp->link_update(fp->phydev->attached_dev,
172						&fp->status);
173				fixed_phy_update_regs(fp);
174			}
175			return fp->regs[reg_num];
176		}
177	}
178
179	return 0xFFFF;
180}
181
182static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
183			    u16 val)
184{
185	return 0;
186}
187
188/*
189 * If something weird is required to be done with link/speed,
190 * network driver is able to assign a function to implement this.
191 * May be useful for PHY's that need to be software-driven.
192 */
193int fixed_phy_set_link_update(struct phy_device *phydev,
194			      int (*link_update)(struct net_device *,
195						 struct fixed_phy_status *))
196{
197	struct fixed_mdio_bus *fmb = &platform_fmb;
198	struct fixed_phy *fp;
199
200	if (!phydev || !phydev->mdio.bus)
201		return -EINVAL;
202
203	list_for_each_entry(fp, &fmb->phys, node) {
204		if (fp->addr == phydev->mdio.addr) {
205			fp->link_update = link_update;
206			fp->phydev = phydev;
207			return 0;
208		}
209	}
210
211	return -ENOENT;
212}
213EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
214
215int fixed_phy_update_state(struct phy_device *phydev,
216			   const struct fixed_phy_status *status,
217			   const struct fixed_phy_status *changed)
218{
 
219	struct fixed_mdio_bus *fmb = &platform_fmb;
220	struct fixed_phy *fp;
221
222	if (!phydev || phydev->mdio.bus != fmb->mii_bus)
223		return -EINVAL;
224
225	list_for_each_entry(fp, &fmb->phys, node) {
226		if (fp->addr == phydev->mdio.addr) {
227#define _UPD(x) if (changed->x) \
228	fp->status.x = status->x
229			_UPD(link);
230			_UPD(speed);
231			_UPD(duplex);
232			_UPD(pause);
233			_UPD(asym_pause);
234#undef _UPD
235			fixed_phy_update_regs(fp);
236			return 0;
237		}
238	}
239
240	return -ENOENT;
241}
242EXPORT_SYMBOL(fixed_phy_update_state);
243
244int fixed_phy_add(unsigned int irq, int phy_addr,
245		  struct fixed_phy_status *status,
246		  int link_gpio)
247{
248	int ret;
249	struct fixed_mdio_bus *fmb = &platform_fmb;
250	struct fixed_phy *fp;
251
252	fp = kzalloc(sizeof(*fp), GFP_KERNEL);
253	if (!fp)
254		return -ENOMEM;
255
256	memset(fp->regs, 0xFF,  sizeof(fp->regs[0]) * MII_REGS_NUM);
257
258	fmb->mii_bus->irq[phy_addr] = irq;
 
259
260	fp->addr = phy_addr;
261	fp->status = *status;
262	fp->link_gpio = link_gpio;
263
264	if (gpio_is_valid(fp->link_gpio)) {
265		ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN,
266				       "fixed-link-gpio-link");
267		if (ret)
268			goto err_regs;
269	}
270
271	ret = fixed_phy_update_regs(fp);
272	if (ret)
273		goto err_gpio;
274
275	list_add_tail(&fp->node, &fmb->phys);
276
277	return 0;
 
278
279err_gpio:
280	if (gpio_is_valid(fp->link_gpio))
281		gpio_free(fp->link_gpio);
282err_regs:
283	kfree(fp);
284	return ret;
285}
286EXPORT_SYMBOL_GPL(fixed_phy_add);
287
 
 
288static void fixed_phy_del(int phy_addr)
289{
290	struct fixed_mdio_bus *fmb = &platform_fmb;
291	struct fixed_phy *fp, *tmp;
292
293	list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
294		if (fp->addr == phy_addr) {
295			list_del(&fp->node);
296			if (gpio_is_valid(fp->link_gpio))
297				gpio_free(fp->link_gpio);
298			kfree(fp);
 
299			return;
300		}
301	}
302}
303
304static int phy_fixed_addr;
305static DEFINE_SPINLOCK(phy_fixed_addr_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
307struct phy_device *fixed_phy_register(unsigned int irq,
308				      struct fixed_phy_status *status,
309				      int link_gpio,
310				      struct device_node *np)
311{
312	struct fixed_mdio_bus *fmb = &platform_fmb;
313	struct phy_device *phy;
314	int phy_addr;
315	int ret;
316
317	/* Get the next available PHY address, up to PHY_MAX_ADDR */
318	spin_lock(&phy_fixed_addr_lock);
319	if (phy_fixed_addr == PHY_MAX_ADDR) {
320		spin_unlock(&phy_fixed_addr_lock);
321		return ERR_PTR(-ENOSPC);
 
 
 
322	}
323	phy_addr = phy_fixed_addr++;
324	spin_unlock(&phy_fixed_addr_lock);
325
326	ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
327	if (ret < 0)
 
 
 
 
 
 
328		return ERR_PTR(ret);
 
329
330	phy = get_phy_device(fmb->mii_bus, phy_addr, false);
331	if (!phy || IS_ERR(phy)) {
332		fixed_phy_del(phy_addr);
333		return ERR_PTR(-EINVAL);
334	}
335
336	/* propagate the fixed link values to struct phy_device */
337	phy->link = status->link;
338	if (status->link) {
339		phy->speed = status->speed;
340		phy->duplex = status->duplex;
341		phy->pause = status->pause;
342		phy->asym_pause = status->asym_pause;
343	}
344
345	of_node_get(np);
346	phy->mdio.dev.of_node = np;
347	phy->is_pseudo_fixed_link = true;
348
349	switch (status->speed) {
350	case SPEED_1000:
351		phy->supported = PHY_1000BT_FEATURES;
352		break;
 
 
 
353	case SPEED_100:
354		phy->supported = PHY_100BT_FEATURES;
355		break;
 
 
 
356	case SPEED_10:
357	default:
358		phy->supported = PHY_10BT_FEATURES;
 
 
 
359	}
360
 
 
361	ret = phy_device_register(phy);
362	if (ret) {
363		phy_device_free(phy);
364		of_node_put(np);
365		fixed_phy_del(phy_addr);
366		return ERR_PTR(ret);
367	}
368
369	return phy;
370}
 
 
 
 
 
 
 
371EXPORT_SYMBOL_GPL(fixed_phy_register);
372
 
 
 
 
 
 
 
 
 
373void fixed_phy_unregister(struct phy_device *phy)
374{
375	phy_device_remove(phy);
376
377	fixed_phy_del(phy->mdio.addr);
378}
379EXPORT_SYMBOL_GPL(fixed_phy_unregister);
380
381static int __init fixed_mdio_bus_init(void)
382{
383	struct fixed_mdio_bus *fmb = &platform_fmb;
384	int ret;
385
386	pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
387	if (IS_ERR(pdev)) {
388		ret = PTR_ERR(pdev);
389		goto err_pdev;
390	}
391
392	fmb->mii_bus = mdiobus_alloc();
393	if (fmb->mii_bus == NULL) {
394		ret = -ENOMEM;
395		goto err_mdiobus_reg;
396	}
397
398	snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
399	fmb->mii_bus->name = "Fixed MDIO Bus";
400	fmb->mii_bus->priv = fmb;
401	fmb->mii_bus->parent = &pdev->dev;
402	fmb->mii_bus->read = &fixed_mdio_read;
403	fmb->mii_bus->write = &fixed_mdio_write;
404
405	ret = mdiobus_register(fmb->mii_bus);
406	if (ret)
407		goto err_mdiobus_alloc;
408
409	return 0;
410
411err_mdiobus_alloc:
412	mdiobus_free(fmb->mii_bus);
413err_mdiobus_reg:
414	platform_device_unregister(pdev);
415err_pdev:
416	return ret;
417}
418module_init(fixed_mdio_bus_init);
419
420static void __exit fixed_mdio_bus_exit(void)
421{
422	struct fixed_mdio_bus *fmb = &platform_fmb;
423	struct fixed_phy *fp, *tmp;
424
425	mdiobus_unregister(fmb->mii_bus);
426	mdiobus_free(fmb->mii_bus);
427	platform_device_unregister(pdev);
428
429	list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
430		list_del(&fp->node);
431		kfree(fp);
432	}
 
433}
434module_exit(fixed_mdio_bus_exit);
435
436MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
437MODULE_AUTHOR("Vitaly Bordug");
438MODULE_LICENSE("GPL");