• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * drivers/net/phy/mdio_bus.c
3  *
4  * MDIO Bus interface
5  *
6  * Author: Andy Fleming
7  *
8  * Copyright (c) 2004 Freescale Semiconductor, Inc.
9  *
10  * This program is free software; you can redistribute  it and/or modify it
11  * under  the terms of  the GNU General  Public License as published by the
12  * Free Software Foundation;  either version 2 of the  License, or (at your
13  * option) any later version.
14  *
15  */
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/unistd.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/skbuff.h>
27 #include <linux/spinlock.h>
28 #include <linux/mm.h>
29 #include <linux/module.h>
30 #include <linux/mii.h>
31 #include <linux/ethtool.h>
32 #include <linux/phy.h>
33 
34 #include <asm/io.h>
35 #include <asm/irq.h>
36 #include <asm/uaccess.h>
37 
38 /**
39  * mdiobus_alloc - allocate a mii_bus structure
40  *
41  * Description: called by a bus driver to allocate an mii_bus
42  * structure to fill in.
43  */
mdiobus_alloc(void)44 struct mii_bus *mdiobus_alloc(void)
45 {
46 	struct mii_bus *bus;
47 
48 	bus = kzalloc(sizeof(*bus), GFP_KERNEL);
49 	if (bus != NULL)
50 		bus->state = MDIOBUS_ALLOCATED;
51 
52 	return bus;
53 }
54 EXPORT_SYMBOL(mdiobus_alloc);
55 
56 /**
57  * mdiobus_release - mii_bus device release callback
58  * @d: the target struct device that contains the mii_bus
59  *
60  * Description: called when the last reference to an mii_bus is
61  * dropped, to free the underlying memory.
62  */
mdiobus_release(struct device * d)63 static void mdiobus_release(struct device *d)
64 {
65 	struct mii_bus *bus = to_mii_bus(d);
66 	BUG_ON(bus->state != MDIOBUS_RELEASED &&
67 	       /* for compatibility with error handling in drivers */
68 	       bus->state != MDIOBUS_ALLOCATED);
69 	kfree(bus);
70 }
71 
72 static struct class mdio_bus_class = {
73 	.name		= "mdio_bus",
74 	.dev_release	= mdiobus_release,
75 };
76 
77 /**
78  * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
79  * @bus: target mii_bus
80  *
81  * Description: Called by a bus driver to bring up all the PHYs
82  *   on a given bus, and attach them to the bus.
83  *
84  * Returns 0 on success or < 0 on error.
85  */
mdiobus_register(struct mii_bus * bus)86 int mdiobus_register(struct mii_bus *bus)
87 {
88 	int i, err;
89 
90 	if (NULL == bus || NULL == bus->name ||
91 			NULL == bus->read ||
92 			NULL == bus->write)
93 		return -EINVAL;
94 
95 	BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
96 	       bus->state != MDIOBUS_UNREGISTERED);
97 
98 	bus->dev.parent = bus->parent;
99 	bus->dev.class = &mdio_bus_class;
100 	bus->dev.groups = NULL;
101 	dev_set_name(&bus->dev, bus->id);
102 
103 	err = device_register(&bus->dev);
104 	if (err) {
105 		printk(KERN_ERR "mii_bus %s failed to register\n", bus->id);
106 		return -EINVAL;
107 	}
108 
109 	mutex_init(&bus->mdio_lock);
110 
111 	if (bus->reset)
112 		bus->reset(bus);
113 
114 	for (i = 0; i < PHY_MAX_ADDR; i++) {
115 		bus->phy_map[i] = NULL;
116 		if ((bus->phy_mask & (1 << i)) == 0) {
117 			struct phy_device *phydev;
118 
119 			phydev = mdiobus_scan(bus, i);
120 			if (IS_ERR(phydev)) {
121 				err = PTR_ERR(phydev);
122 				goto error;
123 			}
124 		}
125 	}
126 
127 	bus->state = MDIOBUS_REGISTERED;
128 	pr_info("%s: probed\n", bus->name);
129 	return 0;
130 
131 error:
132 	while (--i >= 0) {
133 		if (bus->phy_map[i])
134 			device_unregister(&bus->phy_map[i]->dev);
135 	}
136 	device_del(&bus->dev);
137 	return err;
138 }
139 EXPORT_SYMBOL(mdiobus_register);
140 
mdiobus_unregister(struct mii_bus * bus)141 void mdiobus_unregister(struct mii_bus *bus)
142 {
143 	int i;
144 
145 	BUG_ON(bus->state != MDIOBUS_REGISTERED);
146 	bus->state = MDIOBUS_UNREGISTERED;
147 
148 	device_del(&bus->dev);
149 	for (i = 0; i < PHY_MAX_ADDR; i++) {
150 		if (bus->phy_map[i])
151 			device_unregister(&bus->phy_map[i]->dev);
152 	}
153 }
154 EXPORT_SYMBOL(mdiobus_unregister);
155 
156 /**
157  * mdiobus_free - free a struct mii_bus
158  * @bus: mii_bus to free
159  *
160  * This function releases the reference to the underlying device
161  * object in the mii_bus.  If this is the last reference, the mii_bus
162  * will be freed.
163  */
mdiobus_free(struct mii_bus * bus)164 void mdiobus_free(struct mii_bus *bus)
165 {
166 	/*
167 	 * For compatibility with error handling in drivers.
168 	 */
169 	if (bus->state == MDIOBUS_ALLOCATED) {
170 		kfree(bus);
171 		return;
172 	}
173 
174 	BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
175 	bus->state = MDIOBUS_RELEASED;
176 
177 	put_device(&bus->dev);
178 }
179 EXPORT_SYMBOL(mdiobus_free);
180 
mdiobus_scan(struct mii_bus * bus,int addr)181 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
182 {
183 	struct phy_device *phydev;
184 	int err;
185 
186 	phydev = get_phy_device(bus, addr);
187 	if (IS_ERR(phydev) || phydev == NULL)
188 		return phydev;
189 
190 	/* There's a PHY at this address
191 	 * We need to set:
192 	 * 1) IRQ
193 	 * 2) bus_id
194 	 * 3) parent
195 	 * 4) bus
196 	 * 5) mii_bus
197 	 * And, we need to register it */
198 
199 	phydev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
200 
201 	phydev->dev.parent = bus->parent;
202 	phydev->dev.bus = &mdio_bus_type;
203 	dev_set_name(&phydev->dev, PHY_ID_FMT, bus->id, addr);
204 
205 	phydev->bus = bus;
206 
207 	/* Run all of the fixups for this PHY */
208 	phy_scan_fixups(phydev);
209 
210 	err = device_register(&phydev->dev);
211 	if (err) {
212 		printk(KERN_ERR "phy %d failed to register\n", addr);
213 		phy_device_free(phydev);
214 		phydev = NULL;
215 	}
216 
217 	bus->phy_map[addr] = phydev;
218 
219 	return phydev;
220 }
221 EXPORT_SYMBOL(mdiobus_scan);
222 
223 /**
224  * mdiobus_read - Convenience function for reading a given MII mgmt register
225  * @bus: the mii_bus struct
226  * @addr: the phy address
227  * @regnum: register number to read
228  *
229  * NOTE: MUST NOT be called from interrupt context,
230  * because the bus read/write functions may wait for an interrupt
231  * to conclude the operation.
232  */
mdiobus_read(struct mii_bus * bus,int addr,u16 regnum)233 int mdiobus_read(struct mii_bus *bus, int addr, u16 regnum)
234 {
235 	int retval;
236 
237 	BUG_ON(in_interrupt());
238 
239 	mutex_lock(&bus->mdio_lock);
240 	retval = bus->read(bus, addr, regnum);
241 	mutex_unlock(&bus->mdio_lock);
242 
243 	return retval;
244 }
245 EXPORT_SYMBOL(mdiobus_read);
246 
247 /**
248  * mdiobus_write - Convenience function for writing a given MII mgmt register
249  * @bus: the mii_bus struct
250  * @addr: the phy address
251  * @regnum: register number to write
252  * @val: value to write to @regnum
253  *
254  * NOTE: MUST NOT be called from interrupt context,
255  * because the bus read/write functions may wait for an interrupt
256  * to conclude the operation.
257  */
mdiobus_write(struct mii_bus * bus,int addr,u16 regnum,u16 val)258 int mdiobus_write(struct mii_bus *bus, int addr, u16 regnum, u16 val)
259 {
260 	int err;
261 
262 	BUG_ON(in_interrupt());
263 
264 	mutex_lock(&bus->mdio_lock);
265 	err = bus->write(bus, addr, regnum, val);
266 	mutex_unlock(&bus->mdio_lock);
267 
268 	return err;
269 }
270 EXPORT_SYMBOL(mdiobus_write);
271 
272 /**
273  * mdio_bus_match - determine if given PHY driver supports the given PHY device
274  * @dev: target PHY device
275  * @drv: given PHY driver
276  *
277  * Description: Given a PHY device, and a PHY driver, return 1 if
278  *   the driver supports the device.  Otherwise, return 0.
279  */
mdio_bus_match(struct device * dev,struct device_driver * drv)280 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
281 {
282 	struct phy_device *phydev = to_phy_device(dev);
283 	struct phy_driver *phydrv = to_phy_driver(drv);
284 
285 	return ((phydrv->phy_id & phydrv->phy_id_mask) ==
286 		(phydev->phy_id & phydrv->phy_id_mask));
287 }
288 
289 /* Suspend and resume.  Copied from platform_suspend and
290  * platform_resume
291  */
mdio_bus_suspend(struct device * dev,pm_message_t state)292 static int mdio_bus_suspend(struct device * dev, pm_message_t state)
293 {
294 	int ret = 0;
295 	struct device_driver *drv = dev->driver;
296 	struct phy_driver *phydrv = to_phy_driver(drv);
297 	struct phy_device *phydev = to_phy_device(dev);
298 
299 	if (drv && phydrv->suspend && !device_may_wakeup(phydev->dev.parent))
300 		ret = phydrv->suspend(phydev);
301 
302 	return ret;
303 }
304 
mdio_bus_resume(struct device * dev)305 static int mdio_bus_resume(struct device * dev)
306 {
307 	int ret = 0;
308 	struct device_driver *drv = dev->driver;
309 	struct phy_driver *phydrv = to_phy_driver(drv);
310 	struct phy_device *phydev = to_phy_device(dev);
311 
312 	if (drv && phydrv->resume && !device_may_wakeup(phydev->dev.parent))
313 		ret = phydrv->resume(phydev);
314 
315 	return ret;
316 }
317 
318 struct bus_type mdio_bus_type = {
319 	.name		= "mdio_bus",
320 	.match		= mdio_bus_match,
321 	.suspend	= mdio_bus_suspend,
322 	.resume		= mdio_bus_resume,
323 };
324 EXPORT_SYMBOL(mdio_bus_type);
325 
mdio_bus_init(void)326 int __init mdio_bus_init(void)
327 {
328 	int ret;
329 
330 	ret = class_register(&mdio_bus_class);
331 	if (!ret) {
332 		ret = bus_register(&mdio_bus_type);
333 		if (ret)
334 			class_unregister(&mdio_bus_class);
335 	}
336 
337 	return ret;
338 }
339 
mdio_bus_exit(void)340 void mdio_bus_exit(void)
341 {
342 	class_unregister(&mdio_bus_class);
343 	bus_unregister(&mdio_bus_type);
344 }
345