[PATCH] USB: Rename hcd->hub_suspend to hcd->bus_suspend
[linux/fpc-iii.git] / drivers / net / phy / phy_device.c
blob6da1aa0706a1441ba227829ec529bd98089cf0a9
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/config.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/unistd.h>
23 #include <linux/slab.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/spinlock.h>
31 #include <linux/mm.h>
32 #include <linux/module.h>
33 #include <linux/version.h>
34 #include <linux/mii.h>
35 #include <linux/ethtool.h>
36 #include <linux/phy.h>
38 #include <asm/io.h>
39 #include <asm/irq.h>
40 #include <asm/uaccess.h>
42 static struct phy_driver genphy_driver;
43 extern int mdio_bus_init(void);
44 extern void mdio_bus_exit(void);
46 /* get_phy_device
48 * description: Reads the ID registers of the PHY at addr on the
49 * bus, then allocates and returns the phy_device to
50 * represent it.
52 struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
54 int phy_reg;
55 u32 phy_id;
56 struct phy_device *dev = NULL;
58 /* Grab the bits from PHYIR1, and put them
59 * in the upper half */
60 phy_reg = bus->read(bus, addr, MII_PHYSID1);
62 if (phy_reg < 0)
63 return ERR_PTR(phy_reg);
65 phy_id = (phy_reg & 0xffff) << 16;
67 /* Grab the bits from PHYIR2, and put them in the lower half */
68 phy_reg = bus->read(bus, addr, MII_PHYSID2);
70 if (phy_reg < 0)
71 return ERR_PTR(phy_reg);
73 phy_id |= (phy_reg & 0xffff);
75 /* If the phy_id is all Fs, there is no device there */
76 if (0xffffffff == phy_id)
77 return NULL;
79 /* Otherwise, we allocate the device, and initialize the
80 * default values */
81 dev = kcalloc(1, sizeof(*dev), GFP_KERNEL);
83 if (NULL == dev)
84 return ERR_PTR(-ENOMEM);
86 dev->speed = 0;
87 dev->duplex = -1;
88 dev->pause = dev->asym_pause = 0;
89 dev->link = 1;
91 dev->autoneg = AUTONEG_ENABLE;
93 dev->addr = addr;
94 dev->phy_id = phy_id;
95 dev->bus = bus;
97 dev->state = PHY_DOWN;
99 spin_lock_init(&dev->lock);
101 return dev;
104 /* phy_prepare_link:
106 * description: Tells the PHY infrastructure to handle the
107 * gory details on monitoring link status (whether through
108 * polling or an interrupt), and to call back to the
109 * connected device driver when the link status changes.
110 * If you want to monitor your own link state, don't call
111 * this function */
112 void phy_prepare_link(struct phy_device *phydev,
113 void (*handler)(struct net_device *))
115 phydev->adjust_link = handler;
118 /* phy_connect:
120 * description: Convenience function for connecting ethernet
121 * devices to PHY devices. The default behavior is for
122 * the PHY infrastructure to handle everything, and only notify
123 * the connected driver when the link status changes. If you
124 * don't want, or can't use the provided functionality, you may
125 * choose to call only the subset of functions which provide
126 * the desired functionality.
128 struct phy_device * phy_connect(struct net_device *dev, const char *phy_id,
129 void (*handler)(struct net_device *), u32 flags)
131 struct phy_device *phydev;
133 phydev = phy_attach(dev, phy_id, flags);
135 if (IS_ERR(phydev))
136 return phydev;
138 phy_prepare_link(phydev, handler);
140 phy_start_machine(phydev, NULL);
142 if (phydev->irq > 0)
143 phy_start_interrupts(phydev);
145 return phydev;
147 EXPORT_SYMBOL(phy_connect);
149 void phy_disconnect(struct phy_device *phydev)
151 if (phydev->irq > 0)
152 phy_stop_interrupts(phydev);
154 phy_stop_machine(phydev);
156 phydev->adjust_link = NULL;
158 phy_detach(phydev);
160 EXPORT_SYMBOL(phy_disconnect);
162 /* phy_attach:
164 * description: Called by drivers to attach to a particular PHY
165 * device. The phy_device is found, and properly hooked up
166 * to the phy_driver. If no driver is attached, then the
167 * genphy_driver is used. The phy_device is given a ptr to
168 * the attaching device, and given a callback for link status
169 * change. The phy_device is returned to the attaching
170 * driver.
172 static int phy_compare_id(struct device *dev, void *data)
174 return strcmp((char *)data, dev->bus_id) ? 0 : 1;
177 struct phy_device *phy_attach(struct net_device *dev,
178 const char *phy_id, u32 flags)
180 struct bus_type *bus = &mdio_bus_type;
181 struct phy_device *phydev;
182 struct device *d;
184 /* Search the list of PHY devices on the mdio bus for the
185 * PHY with the requested name */
186 d = bus_find_device(bus, NULL, (void *)phy_id, phy_compare_id);
188 if (d) {
189 phydev = to_phy_device(d);
190 } else {
191 printk(KERN_ERR "%s not found\n", phy_id);
192 return ERR_PTR(-ENODEV);
195 /* Assume that if there is no driver, that it doesn't
196 * exist, and we should use the genphy driver. */
197 if (NULL == d->driver) {
198 int err;
199 down_write(&d->bus->subsys.rwsem);
200 d->driver = &genphy_driver.driver;
202 err = d->driver->probe(d);
204 if (err < 0)
205 return ERR_PTR(err);
207 device_bind_driver(d);
208 up_write(&d->bus->subsys.rwsem);
211 if (phydev->attached_dev) {
212 printk(KERN_ERR "%s: %s already attached\n",
213 dev->name, phy_id);
214 return ERR_PTR(-EBUSY);
217 phydev->attached_dev = dev;
219 phydev->dev_flags = flags;
221 return phydev;
223 EXPORT_SYMBOL(phy_attach);
225 void phy_detach(struct phy_device *phydev)
227 phydev->attached_dev = NULL;
229 /* If the device had no specific driver before (i.e. - it
230 * was using the generic driver), we unbind the device
231 * from the generic driver so that there's a chance a
232 * real driver could be loaded */
233 if (phydev->dev.driver == &genphy_driver.driver) {
234 down_write(&phydev->dev.bus->subsys.rwsem);
235 device_release_driver(&phydev->dev);
236 up_write(&phydev->dev.bus->subsys.rwsem);
239 EXPORT_SYMBOL(phy_detach);
242 /* Generic PHY support and helper functions */
244 /* genphy_config_advert
246 * description: Writes MII_ADVERTISE with the appropriate values,
247 * after sanitizing the values to make sure we only advertise
248 * what is supported
250 int genphy_config_advert(struct phy_device *phydev)
252 u32 advertise;
253 int adv;
254 int err;
256 /* Only allow advertising what
257 * this PHY supports */
258 phydev->advertising &= phydev->supported;
259 advertise = phydev->advertising;
261 /* Setup standard advertisement */
262 adv = phy_read(phydev, MII_ADVERTISE);
264 if (adv < 0)
265 return adv;
267 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
268 ADVERTISE_PAUSE_ASYM);
269 if (advertise & ADVERTISED_10baseT_Half)
270 adv |= ADVERTISE_10HALF;
271 if (advertise & ADVERTISED_10baseT_Full)
272 adv |= ADVERTISE_10FULL;
273 if (advertise & ADVERTISED_100baseT_Half)
274 adv |= ADVERTISE_100HALF;
275 if (advertise & ADVERTISED_100baseT_Full)
276 adv |= ADVERTISE_100FULL;
277 if (advertise & ADVERTISED_Pause)
278 adv |= ADVERTISE_PAUSE_CAP;
279 if (advertise & ADVERTISED_Asym_Pause)
280 adv |= ADVERTISE_PAUSE_ASYM;
282 err = phy_write(phydev, MII_ADVERTISE, adv);
284 if (err < 0)
285 return err;
287 /* Configure gigabit if it's supported */
288 if (phydev->supported & (SUPPORTED_1000baseT_Half |
289 SUPPORTED_1000baseT_Full)) {
290 adv = phy_read(phydev, MII_CTRL1000);
292 if (adv < 0)
293 return adv;
295 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
296 if (advertise & SUPPORTED_1000baseT_Half)
297 adv |= ADVERTISE_1000HALF;
298 if (advertise & SUPPORTED_1000baseT_Full)
299 adv |= ADVERTISE_1000FULL;
300 err = phy_write(phydev, MII_CTRL1000, adv);
302 if (err < 0)
303 return err;
306 return adv;
308 EXPORT_SYMBOL(genphy_config_advert);
310 /* genphy_setup_forced
312 * description: Configures MII_BMCR to force speed/duplex
313 * to the values in phydev. Assumes that the values are valid.
314 * Please see phy_sanitize_settings() */
315 int genphy_setup_forced(struct phy_device *phydev)
317 int ctl = BMCR_RESET;
319 phydev->pause = phydev->asym_pause = 0;
321 if (SPEED_1000 == phydev->speed)
322 ctl |= BMCR_SPEED1000;
323 else if (SPEED_100 == phydev->speed)
324 ctl |= BMCR_SPEED100;
326 if (DUPLEX_FULL == phydev->duplex)
327 ctl |= BMCR_FULLDPLX;
329 ctl = phy_write(phydev, MII_BMCR, ctl);
331 if (ctl < 0)
332 return ctl;
334 /* We just reset the device, so we'd better configure any
335 * settings the PHY requires to operate */
336 if (phydev->drv->config_init)
337 ctl = phydev->drv->config_init(phydev);
339 return ctl;
343 /* Enable and Restart Autonegotiation */
344 int genphy_restart_aneg(struct phy_device *phydev)
346 int ctl;
348 ctl = phy_read(phydev, MII_BMCR);
350 if (ctl < 0)
351 return ctl;
353 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
355 /* Don't isolate the PHY if we're negotiating */
356 ctl &= ~(BMCR_ISOLATE);
358 ctl = phy_write(phydev, MII_BMCR, ctl);
360 return ctl;
364 /* genphy_config_aneg
366 * description: If auto-negotiation is enabled, we configure the
367 * advertising, and then restart auto-negotiation. If it is not
368 * enabled, then we write the BMCR
370 int genphy_config_aneg(struct phy_device *phydev)
372 int err = 0;
374 if (AUTONEG_ENABLE == phydev->autoneg) {
375 err = genphy_config_advert(phydev);
377 if (err < 0)
378 return err;
380 err = genphy_restart_aneg(phydev);
381 } else
382 err = genphy_setup_forced(phydev);
384 return err;
386 EXPORT_SYMBOL(genphy_config_aneg);
388 /* genphy_update_link
390 * description: Update the value in phydev->link to reflect the
391 * current link value. In order to do this, we need to read
392 * the status register twice, keeping the second value
394 int genphy_update_link(struct phy_device *phydev)
396 int status;
398 /* Do a fake read */
399 status = phy_read(phydev, MII_BMSR);
401 if (status < 0)
402 return status;
404 /* Read link and autonegotiation status */
405 status = phy_read(phydev, MII_BMSR);
407 if (status < 0)
408 return status;
410 if ((status & BMSR_LSTATUS) == 0)
411 phydev->link = 0;
412 else
413 phydev->link = 1;
415 return 0;
418 /* genphy_read_status
420 * description: Check the link, then figure out the current state
421 * by comparing what we advertise with what the link partner
422 * advertises. Start by checking the gigabit possibilities,
423 * then move on to 10/100.
425 int genphy_read_status(struct phy_device *phydev)
427 int adv;
428 int err;
429 int lpa;
430 int lpagb = 0;
432 /* Update the link, but return if there
433 * was an error */
434 err = genphy_update_link(phydev);
435 if (err)
436 return err;
438 if (AUTONEG_ENABLE == phydev->autoneg) {
439 if (phydev->supported & (SUPPORTED_1000baseT_Half
440 | SUPPORTED_1000baseT_Full)) {
441 lpagb = phy_read(phydev, MII_STAT1000);
443 if (lpagb < 0)
444 return lpagb;
446 adv = phy_read(phydev, MII_CTRL1000);
448 if (adv < 0)
449 return adv;
451 lpagb &= adv << 2;
454 lpa = phy_read(phydev, MII_LPA);
456 if (lpa < 0)
457 return lpa;
459 adv = phy_read(phydev, MII_ADVERTISE);
461 if (adv < 0)
462 return adv;
464 lpa &= adv;
466 phydev->speed = SPEED_10;
467 phydev->duplex = DUPLEX_HALF;
468 phydev->pause = phydev->asym_pause = 0;
470 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
471 phydev->speed = SPEED_1000;
473 if (lpagb & LPA_1000FULL)
474 phydev->duplex = DUPLEX_FULL;
475 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
476 phydev->speed = SPEED_100;
478 if (lpa & LPA_100FULL)
479 phydev->duplex = DUPLEX_FULL;
480 } else
481 if (lpa & LPA_10FULL)
482 phydev->duplex = DUPLEX_FULL;
484 if (phydev->duplex == DUPLEX_FULL){
485 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
486 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
488 } else {
489 int bmcr = phy_read(phydev, MII_BMCR);
490 if (bmcr < 0)
491 return bmcr;
493 if (bmcr & BMCR_FULLDPLX)
494 phydev->duplex = DUPLEX_FULL;
495 else
496 phydev->duplex = DUPLEX_HALF;
498 if (bmcr & BMCR_SPEED1000)
499 phydev->speed = SPEED_1000;
500 else if (bmcr & BMCR_SPEED100)
501 phydev->speed = SPEED_100;
502 else
503 phydev->speed = SPEED_10;
505 phydev->pause = phydev->asym_pause = 0;
508 return 0;
510 EXPORT_SYMBOL(genphy_read_status);
512 static int genphy_config_init(struct phy_device *phydev)
514 u32 val;
515 u32 features;
517 /* For now, I'll claim that the generic driver supports
518 * all possible port types */
519 features = (SUPPORTED_TP | SUPPORTED_MII
520 | SUPPORTED_AUI | SUPPORTED_FIBRE |
521 SUPPORTED_BNC);
523 /* Do we support autonegotiation? */
524 val = phy_read(phydev, MII_BMSR);
526 if (val < 0)
527 return val;
529 if (val & BMSR_ANEGCAPABLE)
530 features |= SUPPORTED_Autoneg;
532 if (val & BMSR_100FULL)
533 features |= SUPPORTED_100baseT_Full;
534 if (val & BMSR_100HALF)
535 features |= SUPPORTED_100baseT_Half;
536 if (val & BMSR_10FULL)
537 features |= SUPPORTED_10baseT_Full;
538 if (val & BMSR_10HALF)
539 features |= SUPPORTED_10baseT_Half;
541 if (val & BMSR_ESTATEN) {
542 val = phy_read(phydev, MII_ESTATUS);
544 if (val < 0)
545 return val;
547 if (val & ESTATUS_1000_TFULL)
548 features |= SUPPORTED_1000baseT_Full;
549 if (val & ESTATUS_1000_THALF)
550 features |= SUPPORTED_1000baseT_Half;
553 phydev->supported = features;
554 phydev->advertising = features;
556 return 0;
560 /* phy_probe
562 * description: Take care of setting up the phy_device structure,
563 * set the state to READY (the driver's init function should
564 * set it to STARTING if needed).
566 static int phy_probe(struct device *dev)
568 struct phy_device *phydev;
569 struct phy_driver *phydrv;
570 struct device_driver *drv;
571 int err = 0;
573 phydev = to_phy_device(dev);
575 /* Make sure the driver is held.
576 * XXX -- Is this correct? */
577 drv = get_driver(phydev->dev.driver);
578 phydrv = to_phy_driver(drv);
579 phydev->drv = phydrv;
581 /* Disable the interrupt if the PHY doesn't support it */
582 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
583 phydev->irq = PHY_POLL;
585 spin_lock(&phydev->lock);
587 /* Start out supporting everything. Eventually,
588 * a controller will attach, and may modify one
589 * or both of these values */
590 phydev->supported = phydrv->features;
591 phydev->advertising = phydrv->features;
593 /* Set the state to READY by default */
594 phydev->state = PHY_READY;
596 if (phydev->drv->probe)
597 err = phydev->drv->probe(phydev);
599 spin_unlock(&phydev->lock);
601 if (err < 0)
602 return err;
604 if (phydev->drv->config_init)
605 err = phydev->drv->config_init(phydev);
607 return err;
610 static int phy_remove(struct device *dev)
612 struct phy_device *phydev;
614 phydev = to_phy_device(dev);
616 spin_lock(&phydev->lock);
617 phydev->state = PHY_DOWN;
618 spin_unlock(&phydev->lock);
620 if (phydev->drv->remove)
621 phydev->drv->remove(phydev);
623 put_driver(dev->driver);
624 phydev->drv = NULL;
626 return 0;
629 int phy_driver_register(struct phy_driver *new_driver)
631 int retval;
633 memset(&new_driver->driver, 0, sizeof(new_driver->driver));
634 new_driver->driver.name = new_driver->name;
635 new_driver->driver.bus = &mdio_bus_type;
636 new_driver->driver.probe = phy_probe;
637 new_driver->driver.remove = phy_remove;
639 retval = driver_register(&new_driver->driver);
641 if (retval) {
642 printk(KERN_ERR "%s: Error %d in registering driver\n",
643 new_driver->name, retval);
645 return retval;
648 pr_info("%s: Registered new driver\n", new_driver->name);
650 return 0;
652 EXPORT_SYMBOL(phy_driver_register);
654 void phy_driver_unregister(struct phy_driver *drv)
656 driver_unregister(&drv->driver);
658 EXPORT_SYMBOL(phy_driver_unregister);
660 static struct phy_driver genphy_driver = {
661 .phy_id = 0xffffffff,
662 .phy_id_mask = 0xffffffff,
663 .name = "Generic PHY",
664 .config_init = genphy_config_init,
665 .features = 0,
666 .config_aneg = genphy_config_aneg,
667 .read_status = genphy_read_status,
668 .driver = {.owner= THIS_MODULE, },
671 static int __init phy_init(void)
673 int rc;
675 rc = mdio_bus_init();
676 if (rc)
677 return rc;
679 rc = phy_driver_register(&genphy_driver);
680 if (rc)
681 mdio_bus_exit();
683 return rc;
686 static void __exit phy_exit(void)
688 phy_driver_unregister(&genphy_driver);
689 mdio_bus_exit();
692 subsys_initcall(phy_init);
693 module_exit(phy_exit);