PHY: remove rwsem use from phy core
[linux-2.6/verdex.git] / drivers / net / phy / phy_device.c
blob8f01952c48500457315624a2b6f962a8043ab838
1 /*
2 * drivers/net/phy/phy_device.c
4 * Framework for finding and configuring PHYs.
5 * Also contains generic PHY driver
7 * Author: Andy Fleming
9 * Copyright (c) 2004 Freescale Semiconductor, Inc.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20 #include <linux/unistd.h>
21 #include <linux/slab.h>
22 #include <linux/interrupt.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/spinlock.h>
29 #include <linux/mm.h>
30 #include <linux/module.h>
31 #include <linux/mii.h>
32 #include <linux/ethtool.h>
33 #include <linux/phy.h>
35 #include <asm/io.h>
36 #include <asm/irq.h>
37 #include <asm/uaccess.h>
39 MODULE_DESCRIPTION("PHY library");
40 MODULE_AUTHOR("Andy Fleming");
41 MODULE_LICENSE("GPL");
43 static struct phy_driver genphy_driver;
44 extern int mdio_bus_init(void);
45 extern void mdio_bus_exit(void);
47 struct phy_device* phy_device_create(struct mii_bus *bus, int addr, int phy_id)
49 struct phy_device *dev;
50 /* We allocate the device, and initialize the
51 * default values */
52 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
54 if (NULL == dev)
55 return (struct phy_device*) PTR_ERR((void*)-ENOMEM);
57 dev->speed = 0;
58 dev->duplex = -1;
59 dev->pause = dev->asym_pause = 0;
60 dev->link = 1;
61 dev->interface = PHY_INTERFACE_MODE_GMII;
63 dev->autoneg = AUTONEG_ENABLE;
65 dev->addr = addr;
66 dev->phy_id = phy_id;
67 dev->bus = bus;
69 dev->state = PHY_DOWN;
71 spin_lock_init(&dev->lock);
73 return dev;
75 EXPORT_SYMBOL(phy_device_create);
77 /* get_phy_device
79 * description: Reads the ID registers of the PHY at addr on the
80 * bus, then allocates and returns the phy_device to
81 * represent it.
83 struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
85 int phy_reg;
86 u32 phy_id;
87 struct phy_device *dev = NULL;
89 /* Grab the bits from PHYIR1, and put them
90 * in the upper half */
91 phy_reg = bus->read(bus, addr, MII_PHYSID1);
93 if (phy_reg < 0)
94 return ERR_PTR(phy_reg);
96 phy_id = (phy_reg & 0xffff) << 16;
98 /* Grab the bits from PHYIR2, and put them in the lower half */
99 phy_reg = bus->read(bus, addr, MII_PHYSID2);
101 if (phy_reg < 0)
102 return ERR_PTR(phy_reg);
104 phy_id |= (phy_reg & 0xffff);
106 /* If the phy_id is all Fs, there is no device there */
107 if (0xffffffff == phy_id)
108 return NULL;
110 dev = phy_device_create(bus, addr, phy_id);
112 return dev;
115 /* phy_prepare_link:
117 * description: Tells the PHY infrastructure to handle the
118 * gory details on monitoring link status (whether through
119 * polling or an interrupt), and to call back to the
120 * connected device driver when the link status changes.
121 * If you want to monitor your own link state, don't call
122 * this function */
123 void phy_prepare_link(struct phy_device *phydev,
124 void (*handler)(struct net_device *))
126 phydev->adjust_link = handler;
129 /* phy_connect:
131 * description: Convenience function for connecting ethernet
132 * devices to PHY devices. The default behavior is for
133 * the PHY infrastructure to handle everything, and only notify
134 * the connected driver when the link status changes. If you
135 * don't want, or can't use the provided functionality, you may
136 * choose to call only the subset of functions which provide
137 * the desired functionality.
139 struct phy_device * phy_connect(struct net_device *dev, const char *phy_id,
140 void (*handler)(struct net_device *), u32 flags,
141 phy_interface_t interface)
143 struct phy_device *phydev;
145 phydev = phy_attach(dev, phy_id, flags, interface);
147 if (IS_ERR(phydev))
148 return phydev;
150 phy_prepare_link(phydev, handler);
152 phy_start_machine(phydev, NULL);
154 if (phydev->irq > 0)
155 phy_start_interrupts(phydev);
157 return phydev;
159 EXPORT_SYMBOL(phy_connect);
161 void phy_disconnect(struct phy_device *phydev)
163 if (phydev->irq > 0)
164 phy_stop_interrupts(phydev);
166 phy_stop_machine(phydev);
168 phydev->adjust_link = NULL;
170 phy_detach(phydev);
172 EXPORT_SYMBOL(phy_disconnect);
174 /* phy_attach:
176 * description: Called by drivers to attach to a particular PHY
177 * device. The phy_device is found, and properly hooked up
178 * to the phy_driver. If no driver is attached, then the
179 * genphy_driver is used. The phy_device is given a ptr to
180 * the attaching device, and given a callback for link status
181 * change. The phy_device is returned to the attaching
182 * driver.
184 static int phy_compare_id(struct device *dev, void *data)
186 return strcmp((char *)data, dev->bus_id) ? 0 : 1;
189 struct phy_device *phy_attach(struct net_device *dev,
190 const char *phy_id, u32 flags, phy_interface_t interface)
192 struct bus_type *bus = &mdio_bus_type;
193 struct phy_device *phydev;
194 struct device *d;
196 /* Search the list of PHY devices on the mdio bus for the
197 * PHY with the requested name */
198 d = bus_find_device(bus, NULL, (void *)phy_id, phy_compare_id);
200 if (d) {
201 phydev = to_phy_device(d);
202 } else {
203 printk(KERN_ERR "%s not found\n", phy_id);
204 return ERR_PTR(-ENODEV);
207 /* Assume that if there is no driver, that it doesn't
208 * exist, and we should use the genphy driver. */
209 if (NULL == d->driver) {
210 int err;
211 d->driver = &genphy_driver.driver;
213 err = d->driver->probe(d);
214 if (err >= 0)
215 err = device_bind_driver(d);
217 if (err)
218 return ERR_PTR(err);
221 if (phydev->attached_dev) {
222 printk(KERN_ERR "%s: %s already attached\n",
223 dev->name, phy_id);
224 return ERR_PTR(-EBUSY);
227 phydev->attached_dev = dev;
229 phydev->dev_flags = flags;
231 phydev->interface = interface;
233 /* Do initial configuration here, now that
234 * we have certain key parameters
235 * (dev_flags and interface) */
236 if (phydev->drv->config_init) {
237 int err;
239 err = phydev->drv->config_init(phydev);
241 if (err < 0)
242 return ERR_PTR(err);
245 return phydev;
247 EXPORT_SYMBOL(phy_attach);
249 void phy_detach(struct phy_device *phydev)
251 phydev->attached_dev = NULL;
253 /* If the device had no specific driver before (i.e. - it
254 * was using the generic driver), we unbind the device
255 * from the generic driver so that there's a chance a
256 * real driver could be loaded */
257 if (phydev->dev.driver == &genphy_driver.driver)
258 device_release_driver(&phydev->dev);
260 EXPORT_SYMBOL(phy_detach);
263 /* Generic PHY support and helper functions */
265 /* genphy_config_advert
267 * description: Writes MII_ADVERTISE with the appropriate values,
268 * after sanitizing the values to make sure we only advertise
269 * what is supported
271 int genphy_config_advert(struct phy_device *phydev)
273 u32 advertise;
274 int adv;
275 int err;
277 /* Only allow advertising what
278 * this PHY supports */
279 phydev->advertising &= phydev->supported;
280 advertise = phydev->advertising;
282 /* Setup standard advertisement */
283 adv = phy_read(phydev, MII_ADVERTISE);
285 if (adv < 0)
286 return adv;
288 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
289 ADVERTISE_PAUSE_ASYM);
290 if (advertise & ADVERTISED_10baseT_Half)
291 adv |= ADVERTISE_10HALF;
292 if (advertise & ADVERTISED_10baseT_Full)
293 adv |= ADVERTISE_10FULL;
294 if (advertise & ADVERTISED_100baseT_Half)
295 adv |= ADVERTISE_100HALF;
296 if (advertise & ADVERTISED_100baseT_Full)
297 adv |= ADVERTISE_100FULL;
298 if (advertise & ADVERTISED_Pause)
299 adv |= ADVERTISE_PAUSE_CAP;
300 if (advertise & ADVERTISED_Asym_Pause)
301 adv |= ADVERTISE_PAUSE_ASYM;
303 err = phy_write(phydev, MII_ADVERTISE, adv);
305 if (err < 0)
306 return err;
308 /* Configure gigabit if it's supported */
309 if (phydev->supported & (SUPPORTED_1000baseT_Half |
310 SUPPORTED_1000baseT_Full)) {
311 adv = phy_read(phydev, MII_CTRL1000);
313 if (adv < 0)
314 return adv;
316 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
317 if (advertise & SUPPORTED_1000baseT_Half)
318 adv |= ADVERTISE_1000HALF;
319 if (advertise & SUPPORTED_1000baseT_Full)
320 adv |= ADVERTISE_1000FULL;
321 err = phy_write(phydev, MII_CTRL1000, adv);
323 if (err < 0)
324 return err;
327 return adv;
329 EXPORT_SYMBOL(genphy_config_advert);
331 /* genphy_setup_forced
333 * description: Configures MII_BMCR to force speed/duplex
334 * to the values in phydev. Assumes that the values are valid.
335 * Please see phy_sanitize_settings() */
336 int genphy_setup_forced(struct phy_device *phydev)
338 int ctl = BMCR_RESET;
340 phydev->pause = phydev->asym_pause = 0;
342 if (SPEED_1000 == phydev->speed)
343 ctl |= BMCR_SPEED1000;
344 else if (SPEED_100 == phydev->speed)
345 ctl |= BMCR_SPEED100;
347 if (DUPLEX_FULL == phydev->duplex)
348 ctl |= BMCR_FULLDPLX;
350 ctl = phy_write(phydev, MII_BMCR, ctl);
352 if (ctl < 0)
353 return ctl;
355 /* We just reset the device, so we'd better configure any
356 * settings the PHY requires to operate */
357 if (phydev->drv->config_init)
358 ctl = phydev->drv->config_init(phydev);
360 return ctl;
364 /* Enable and Restart Autonegotiation */
365 int genphy_restart_aneg(struct phy_device *phydev)
367 int ctl;
369 ctl = phy_read(phydev, MII_BMCR);
371 if (ctl < 0)
372 return ctl;
374 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
376 /* Don't isolate the PHY if we're negotiating */
377 ctl &= ~(BMCR_ISOLATE);
379 ctl = phy_write(phydev, MII_BMCR, ctl);
381 return ctl;
385 /* genphy_config_aneg
387 * description: If auto-negotiation is enabled, we configure the
388 * advertising, and then restart auto-negotiation. If it is not
389 * enabled, then we write the BMCR
391 int genphy_config_aneg(struct phy_device *phydev)
393 int err = 0;
395 if (AUTONEG_ENABLE == phydev->autoneg) {
396 err = genphy_config_advert(phydev);
398 if (err < 0)
399 return err;
401 err = genphy_restart_aneg(phydev);
402 } else
403 err = genphy_setup_forced(phydev);
405 return err;
407 EXPORT_SYMBOL(genphy_config_aneg);
409 /* genphy_update_link
411 * description: Update the value in phydev->link to reflect the
412 * current link value. In order to do this, we need to read
413 * the status register twice, keeping the second value
415 int genphy_update_link(struct phy_device *phydev)
417 int status;
419 /* Do a fake read */
420 status = phy_read(phydev, MII_BMSR);
422 if (status < 0)
423 return status;
425 /* Read link and autonegotiation status */
426 status = phy_read(phydev, MII_BMSR);
428 if (status < 0)
429 return status;
431 if ((status & BMSR_LSTATUS) == 0)
432 phydev->link = 0;
433 else
434 phydev->link = 1;
436 return 0;
438 EXPORT_SYMBOL(genphy_update_link);
440 /* genphy_read_status
442 * description: Check the link, then figure out the current state
443 * by comparing what we advertise with what the link partner
444 * advertises. Start by checking the gigabit possibilities,
445 * then move on to 10/100.
447 int genphy_read_status(struct phy_device *phydev)
449 int adv;
450 int err;
451 int lpa;
452 int lpagb = 0;
454 /* Update the link, but return if there
455 * was an error */
456 err = genphy_update_link(phydev);
457 if (err)
458 return err;
460 if (AUTONEG_ENABLE == phydev->autoneg) {
461 if (phydev->supported & (SUPPORTED_1000baseT_Half
462 | SUPPORTED_1000baseT_Full)) {
463 lpagb = phy_read(phydev, MII_STAT1000);
465 if (lpagb < 0)
466 return lpagb;
468 adv = phy_read(phydev, MII_CTRL1000);
470 if (adv < 0)
471 return adv;
473 lpagb &= adv << 2;
476 lpa = phy_read(phydev, MII_LPA);
478 if (lpa < 0)
479 return lpa;
481 adv = phy_read(phydev, MII_ADVERTISE);
483 if (adv < 0)
484 return adv;
486 lpa &= adv;
488 phydev->speed = SPEED_10;
489 phydev->duplex = DUPLEX_HALF;
490 phydev->pause = phydev->asym_pause = 0;
492 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
493 phydev->speed = SPEED_1000;
495 if (lpagb & LPA_1000FULL)
496 phydev->duplex = DUPLEX_FULL;
497 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
498 phydev->speed = SPEED_100;
500 if (lpa & LPA_100FULL)
501 phydev->duplex = DUPLEX_FULL;
502 } else
503 if (lpa & LPA_10FULL)
504 phydev->duplex = DUPLEX_FULL;
506 if (phydev->duplex == DUPLEX_FULL){
507 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
508 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
510 } else {
511 int bmcr = phy_read(phydev, MII_BMCR);
512 if (bmcr < 0)
513 return bmcr;
515 if (bmcr & BMCR_FULLDPLX)
516 phydev->duplex = DUPLEX_FULL;
517 else
518 phydev->duplex = DUPLEX_HALF;
520 if (bmcr & BMCR_SPEED1000)
521 phydev->speed = SPEED_1000;
522 else if (bmcr & BMCR_SPEED100)
523 phydev->speed = SPEED_100;
524 else
525 phydev->speed = SPEED_10;
527 phydev->pause = phydev->asym_pause = 0;
530 return 0;
532 EXPORT_SYMBOL(genphy_read_status);
534 static int genphy_config_init(struct phy_device *phydev)
536 int val;
537 u32 features;
539 /* For now, I'll claim that the generic driver supports
540 * all possible port types */
541 features = (SUPPORTED_TP | SUPPORTED_MII
542 | SUPPORTED_AUI | SUPPORTED_FIBRE |
543 SUPPORTED_BNC);
545 /* Do we support autonegotiation? */
546 val = phy_read(phydev, MII_BMSR);
548 if (val < 0)
549 return val;
551 if (val & BMSR_ANEGCAPABLE)
552 features |= SUPPORTED_Autoneg;
554 if (val & BMSR_100FULL)
555 features |= SUPPORTED_100baseT_Full;
556 if (val & BMSR_100HALF)
557 features |= SUPPORTED_100baseT_Half;
558 if (val & BMSR_10FULL)
559 features |= SUPPORTED_10baseT_Full;
560 if (val & BMSR_10HALF)
561 features |= SUPPORTED_10baseT_Half;
563 if (val & BMSR_ESTATEN) {
564 val = phy_read(phydev, MII_ESTATUS);
566 if (val < 0)
567 return val;
569 if (val & ESTATUS_1000_TFULL)
570 features |= SUPPORTED_1000baseT_Full;
571 if (val & ESTATUS_1000_THALF)
572 features |= SUPPORTED_1000baseT_Half;
575 phydev->supported = features;
576 phydev->advertising = features;
578 return 0;
582 /* phy_probe
584 * description: Take care of setting up the phy_device structure,
585 * set the state to READY (the driver's init function should
586 * set it to STARTING if needed).
588 static int phy_probe(struct device *dev)
590 struct phy_device *phydev;
591 struct phy_driver *phydrv;
592 struct device_driver *drv;
593 int err = 0;
595 phydev = to_phy_device(dev);
597 /* Make sure the driver is held.
598 * XXX -- Is this correct? */
599 drv = get_driver(phydev->dev.driver);
600 phydrv = to_phy_driver(drv);
601 phydev->drv = phydrv;
603 /* Disable the interrupt if the PHY doesn't support it */
604 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
605 phydev->irq = PHY_POLL;
607 spin_lock(&phydev->lock);
609 /* Start out supporting everything. Eventually,
610 * a controller will attach, and may modify one
611 * or both of these values */
612 phydev->supported = phydrv->features;
613 phydev->advertising = phydrv->features;
615 /* Set the state to READY by default */
616 phydev->state = PHY_READY;
618 if (phydev->drv->probe)
619 err = phydev->drv->probe(phydev);
621 spin_unlock(&phydev->lock);
623 return err;
627 static int phy_remove(struct device *dev)
629 struct phy_device *phydev;
631 phydev = to_phy_device(dev);
633 spin_lock(&phydev->lock);
634 phydev->state = PHY_DOWN;
635 spin_unlock(&phydev->lock);
637 if (phydev->drv->remove)
638 phydev->drv->remove(phydev);
640 put_driver(dev->driver);
641 phydev->drv = NULL;
643 return 0;
646 int phy_driver_register(struct phy_driver *new_driver)
648 int retval;
650 memset(&new_driver->driver, 0, sizeof(new_driver->driver));
651 new_driver->driver.name = new_driver->name;
652 new_driver->driver.bus = &mdio_bus_type;
653 new_driver->driver.probe = phy_probe;
654 new_driver->driver.remove = phy_remove;
656 retval = driver_register(&new_driver->driver);
658 if (retval) {
659 printk(KERN_ERR "%s: Error %d in registering driver\n",
660 new_driver->name, retval);
662 return retval;
665 pr_info("%s: Registered new driver\n", new_driver->name);
667 return 0;
669 EXPORT_SYMBOL(phy_driver_register);
671 void phy_driver_unregister(struct phy_driver *drv)
673 driver_unregister(&drv->driver);
675 EXPORT_SYMBOL(phy_driver_unregister);
677 static struct phy_driver genphy_driver = {
678 .phy_id = 0xffffffff,
679 .phy_id_mask = 0xffffffff,
680 .name = "Generic PHY",
681 .config_init = genphy_config_init,
682 .features = 0,
683 .config_aneg = genphy_config_aneg,
684 .read_status = genphy_read_status,
685 .driver = {.owner= THIS_MODULE, },
688 static int __init phy_init(void)
690 int rc;
692 rc = mdio_bus_init();
693 if (rc)
694 return rc;
696 rc = phy_driver_register(&genphy_driver);
697 if (rc)
698 mdio_bus_exit();
700 return rc;
703 static void __exit phy_exit(void)
705 phy_driver_unregister(&genphy_driver);
706 mdio_bus_exit();
709 subsys_initcall(phy_init);
710 module_exit(phy_exit);