2 * drivers/net/phy/phy_device.c
4 * Framework for finding and configuring PHYs.
5 * Also contains generic PHY driver
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/sched.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/unistd.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/spinlock.h>
31 #include <linux/module.h>
32 #include <linux/mii.h>
33 #include <linux/ethtool.h>
34 #include <linux/phy.h>
38 #include <asm/uaccess.h>
40 MODULE_DESCRIPTION("PHY library");
41 MODULE_AUTHOR("Andy Fleming");
42 MODULE_LICENSE("GPL");
44 static struct phy_driver genphy_driver;
45 extern int mdio_bus_init(void);
46 extern void mdio_bus_exit(void);
48 struct phy_device* phy_device_create(struct mii_bus *bus, int addr, int phy_id)
50 struct phy_device *dev;
51 /* We allocate the device, and initialize the
53 dev = kcalloc(1, sizeof(*dev), GFP_KERNEL);
56 return (struct phy_device*) PTR_ERR((void*)-ENOMEM);
60 dev->pause = dev->asym_pause = 0;
63 dev->autoneg = AUTONEG_ENABLE;
69 dev->state = PHY_DOWN;
71 spin_lock_init(&dev->lock);
75 EXPORT_SYMBOL(phy_device_create);
79 * description: Reads the ID registers of the PHY at addr on the
80 * bus, then allocates and returns the phy_device to
83 struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
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);
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);
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)
110 dev = phy_device_create(bus, addr, phy_id);
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
123 void phy_prepare_link(struct phy_device *phydev,
124 void (*handler)(struct net_device *))
126 phydev->adjust_link = handler;
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)
142 struct phy_device *phydev;
144 phydev = phy_attach(dev, phy_id, flags);
149 phy_prepare_link(phydev, handler);
151 phy_start_machine(phydev, NULL);
154 phy_start_interrupts(phydev);
158 EXPORT_SYMBOL(phy_connect);
160 void phy_disconnect(struct phy_device *phydev)
163 phy_stop_interrupts(phydev);
165 phy_stop_machine(phydev);
167 phydev->adjust_link = NULL;
171 EXPORT_SYMBOL(phy_disconnect);
175 * description: Called by drivers to attach to a particular PHY
176 * device. The phy_device is found, and properly hooked up
177 * to the phy_driver. If no driver is attached, then the
178 * genphy_driver is used. The phy_device is given a ptr to
179 * the attaching device, and given a callback for link status
180 * change. The phy_device is returned to the attaching
183 static int phy_compare_id(struct device *dev, void *data)
185 return strcmp((char *)data, dev->bus_id) ? 0 : 1;
188 struct phy_device *phy_attach(struct net_device *dev,
189 const char *phy_id, u32 flags)
191 struct bus_type *bus = &mdio_bus_type;
192 struct phy_device *phydev;
195 /* Search the list of PHY devices on the mdio bus for the
196 * PHY with the requested name */
197 d = bus_find_device(bus, NULL, (void *)phy_id, phy_compare_id);
200 phydev = to_phy_device(d);
202 printk(KERN_ERR "%s not found\n", phy_id);
203 return ERR_PTR(-ENODEV);
206 /* Assume that if there is no driver, that it doesn't
207 * exist, and we should use the genphy driver. */
208 if (NULL == d->driver) {
210 down_write(&d->bus->subsys.rwsem);
211 d->driver = &genphy_driver.driver;
213 err = d->driver->probe(d);
216 err = device_bind_driver(d);
218 up_write(&d->bus->subsys.rwsem);
224 if (phydev->attached_dev) {
225 printk(KERN_ERR "%s: %s already attached\n",
227 return ERR_PTR(-EBUSY);
230 phydev->attached_dev = dev;
232 phydev->dev_flags = flags;
236 EXPORT_SYMBOL(phy_attach);
238 void phy_detach(struct phy_device *phydev)
240 phydev->attached_dev = NULL;
242 /* If the device had no specific driver before (i.e. - it
243 * was using the generic driver), we unbind the device
244 * from the generic driver so that there's a chance a
245 * real driver could be loaded */
246 if (phydev->dev.driver == &genphy_driver.driver) {
247 down_write(&phydev->dev.bus->subsys.rwsem);
248 device_release_driver(&phydev->dev);
249 up_write(&phydev->dev.bus->subsys.rwsem);
252 EXPORT_SYMBOL(phy_detach);
255 /* Generic PHY support and helper functions */
257 /* genphy_config_advert
259 * description: Writes MII_ADVERTISE with the appropriate values,
260 * after sanitizing the values to make sure we only advertise
263 int genphy_config_advert(struct phy_device *phydev)
269 /* Only allow advertising what
270 * this PHY supports */
271 phydev->advertising &= phydev->supported;
272 advertise = phydev->advertising;
274 /* Setup standard advertisement */
275 adv = phy_read(phydev, MII_ADVERTISE);
280 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
281 ADVERTISE_PAUSE_ASYM);
282 if (advertise & ADVERTISED_10baseT_Half)
283 adv |= ADVERTISE_10HALF;
284 if (advertise & ADVERTISED_10baseT_Full)
285 adv |= ADVERTISE_10FULL;
286 if (advertise & ADVERTISED_100baseT_Half)
287 adv |= ADVERTISE_100HALF;
288 if (advertise & ADVERTISED_100baseT_Full)
289 adv |= ADVERTISE_100FULL;
290 if (advertise & ADVERTISED_Pause)
291 adv |= ADVERTISE_PAUSE_CAP;
292 if (advertise & ADVERTISED_Asym_Pause)
293 adv |= ADVERTISE_PAUSE_ASYM;
295 err = phy_write(phydev, MII_ADVERTISE, adv);
300 /* Configure gigabit if it's supported */
301 if (phydev->supported & (SUPPORTED_1000baseT_Half |
302 SUPPORTED_1000baseT_Full)) {
303 adv = phy_read(phydev, MII_CTRL1000);
308 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
309 if (advertise & SUPPORTED_1000baseT_Half)
310 adv |= ADVERTISE_1000HALF;
311 if (advertise & SUPPORTED_1000baseT_Full)
312 adv |= ADVERTISE_1000FULL;
313 err = phy_write(phydev, MII_CTRL1000, adv);
321 EXPORT_SYMBOL(genphy_config_advert);
323 /* genphy_setup_forced
325 * description: Configures MII_BMCR to force speed/duplex
326 * to the values in phydev. Assumes that the values are valid.
327 * Please see phy_sanitize_settings() */
328 int genphy_setup_forced(struct phy_device *phydev)
330 int ctl = BMCR_RESET;
332 phydev->pause = phydev->asym_pause = 0;
334 if (SPEED_1000 == phydev->speed)
335 ctl |= BMCR_SPEED1000;
336 else if (SPEED_100 == phydev->speed)
337 ctl |= BMCR_SPEED100;
339 if (DUPLEX_FULL == phydev->duplex)
340 ctl |= BMCR_FULLDPLX;
342 ctl = phy_write(phydev, MII_BMCR, ctl);
347 /* We just reset the device, so we'd better configure any
348 * settings the PHY requires to operate */
349 if (phydev->drv->config_init)
350 ctl = phydev->drv->config_init(phydev);
356 /* Enable and Restart Autonegotiation */
357 int genphy_restart_aneg(struct phy_device *phydev)
361 ctl = phy_read(phydev, MII_BMCR);
366 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
368 /* Don't isolate the PHY if we're negotiating */
369 ctl &= ~(BMCR_ISOLATE);
371 ctl = phy_write(phydev, MII_BMCR, ctl);
377 /* genphy_config_aneg
379 * description: If auto-negotiation is enabled, we configure the
380 * advertising, and then restart auto-negotiation. If it is not
381 * enabled, then we write the BMCR
383 int genphy_config_aneg(struct phy_device *phydev)
387 if (AUTONEG_ENABLE == phydev->autoneg) {
388 err = genphy_config_advert(phydev);
393 err = genphy_restart_aneg(phydev);
395 err = genphy_setup_forced(phydev);
399 EXPORT_SYMBOL(genphy_config_aneg);
401 /* genphy_update_link
403 * description: Update the value in phydev->link to reflect the
404 * current link value. In order to do this, we need to read
405 * the status register twice, keeping the second value
407 int genphy_update_link(struct phy_device *phydev)
412 status = phy_read(phydev, MII_BMSR);
417 /* Read link and autonegotiation status */
418 status = phy_read(phydev, MII_BMSR);
423 if ((status & BMSR_LSTATUS) == 0)
431 /* genphy_read_status
433 * description: Check the link, then figure out the current state
434 * by comparing what we advertise with what the link partner
435 * advertises. Start by checking the gigabit possibilities,
436 * then move on to 10/100.
438 int genphy_read_status(struct phy_device *phydev)
445 /* Update the link, but return if there
447 err = genphy_update_link(phydev);
451 if (AUTONEG_ENABLE == phydev->autoneg) {
452 if (phydev->supported & (SUPPORTED_1000baseT_Half
453 | SUPPORTED_1000baseT_Full)) {
454 lpagb = phy_read(phydev, MII_STAT1000);
459 adv = phy_read(phydev, MII_CTRL1000);
467 lpa = phy_read(phydev, MII_LPA);
472 adv = phy_read(phydev, MII_ADVERTISE);
479 phydev->speed = SPEED_10;
480 phydev->duplex = DUPLEX_HALF;
481 phydev->pause = phydev->asym_pause = 0;
483 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
484 phydev->speed = SPEED_1000;
486 if (lpagb & LPA_1000FULL)
487 phydev->duplex = DUPLEX_FULL;
488 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
489 phydev->speed = SPEED_100;
491 if (lpa & LPA_100FULL)
492 phydev->duplex = DUPLEX_FULL;
494 if (lpa & LPA_10FULL)
495 phydev->duplex = DUPLEX_FULL;
497 if (phydev->duplex == DUPLEX_FULL){
498 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
499 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
502 int bmcr = phy_read(phydev, MII_BMCR);
506 if (bmcr & BMCR_FULLDPLX)
507 phydev->duplex = DUPLEX_FULL;
509 phydev->duplex = DUPLEX_HALF;
511 if (bmcr & BMCR_SPEED1000)
512 phydev->speed = SPEED_1000;
513 else if (bmcr & BMCR_SPEED100)
514 phydev->speed = SPEED_100;
516 phydev->speed = SPEED_10;
518 phydev->pause = phydev->asym_pause = 0;
523 EXPORT_SYMBOL(genphy_read_status);
525 static int genphy_config_init(struct phy_device *phydev)
530 /* For now, I'll claim that the generic driver supports
531 * all possible port types */
532 features = (SUPPORTED_TP | SUPPORTED_MII
533 | SUPPORTED_AUI | SUPPORTED_FIBRE |
536 /* Do we support autonegotiation? */
537 val = phy_read(phydev, MII_BMSR);
542 if (val & BMSR_ANEGCAPABLE)
543 features |= SUPPORTED_Autoneg;
545 if (val & BMSR_100FULL)
546 features |= SUPPORTED_100baseT_Full;
547 if (val & BMSR_100HALF)
548 features |= SUPPORTED_100baseT_Half;
549 if (val & BMSR_10FULL)
550 features |= SUPPORTED_10baseT_Full;
551 if (val & BMSR_10HALF)
552 features |= SUPPORTED_10baseT_Half;
554 if (val & BMSR_ESTATEN) {
555 val = phy_read(phydev, MII_ESTATUS);
560 if (val & ESTATUS_1000_TFULL)
561 features |= SUPPORTED_1000baseT_Full;
562 if (val & ESTATUS_1000_THALF)
563 features |= SUPPORTED_1000baseT_Half;
566 phydev->supported = features;
567 phydev->advertising = features;
575 * description: Take care of setting up the phy_device structure,
576 * set the state to READY (the driver's init function should
577 * set it to STARTING if needed).
579 static int phy_probe(struct device *dev)
581 struct phy_device *phydev;
582 struct phy_driver *phydrv;
583 struct device_driver *drv;
586 phydev = to_phy_device(dev);
588 /* Make sure the driver is held.
589 * XXX -- Is this correct? */
590 drv = get_driver(phydev->dev.driver);
591 phydrv = to_phy_driver(drv);
592 phydev->drv = phydrv;
594 /* Disable the interrupt if the PHY doesn't support it */
595 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
596 phydev->irq = PHY_POLL;
598 spin_lock(&phydev->lock);
600 /* Start out supporting everything. Eventually,
601 * a controller will attach, and may modify one
602 * or both of these values */
603 phydev->supported = phydrv->features;
604 phydev->advertising = phydrv->features;
606 /* Set the state to READY by default */
607 phydev->state = PHY_READY;
609 if (phydev->drv->probe)
610 err = phydev->drv->probe(phydev);
612 spin_unlock(&phydev->lock);
617 if (phydev->drv->config_init)
618 err = phydev->drv->config_init(phydev);
623 static int phy_remove(struct device *dev)
625 struct phy_device *phydev;
627 phydev = to_phy_device(dev);
629 spin_lock(&phydev->lock);
630 phydev->state = PHY_DOWN;
631 spin_unlock(&phydev->lock);
633 if (phydev->drv->remove)
634 phydev->drv->remove(phydev);
636 put_driver(dev->driver);
642 int phy_driver_register(struct phy_driver *new_driver)
646 memset(&new_driver->driver, 0, sizeof(new_driver->driver));
647 new_driver->driver.name = new_driver->name;
648 new_driver->driver.bus = &mdio_bus_type;
649 new_driver->driver.probe = phy_probe;
650 new_driver->driver.remove = phy_remove;
652 retval = driver_register(&new_driver->driver);
655 printk(KERN_ERR "%s: Error %d in registering driver\n",
656 new_driver->name, retval);
661 pr_info("%s: Registered new driver\n", new_driver->name);
665 EXPORT_SYMBOL(phy_driver_register);
667 void phy_driver_unregister(struct phy_driver *drv)
669 driver_unregister(&drv->driver);
671 EXPORT_SYMBOL(phy_driver_unregister);
673 static struct phy_driver genphy_driver = {
674 .phy_id = 0xffffffff,
675 .phy_id_mask = 0xffffffff,
676 .name = "Generic PHY",
677 .config_init = genphy_config_init,
679 .config_aneg = genphy_config_aneg,
680 .read_status = genphy_read_status,
681 .driver = {.owner= THIS_MODULE, },
684 static int __init phy_init(void)
688 rc = mdio_bus_init();
692 rc = phy_driver_register(&genphy_driver);
699 static void __exit phy_exit(void)
701 phy_driver_unregister(&genphy_driver);
705 subsys_initcall(phy_init);
706 module_exit(phy_exit);