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/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>
32 #include <linux/module.h>
33 #include <linux/mii.h>
34 #include <linux/ethtool.h>
35 #include <linux/phy.h>
39 #include <asm/uaccess.h>
41 static struct phy_driver genphy_driver;
42 extern int mdio_bus_init(void);
43 extern void mdio_bus_exit(void);
47 * description: Reads the ID registers of the PHY at addr on the
48 * bus, then allocates and returns the phy_device to
51 struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
55 struct phy_device *dev = NULL;
57 /* Grab the bits from PHYIR1, and put them
58 * in the upper half */
59 phy_reg = bus->read(bus, addr, MII_PHYSID1);
62 return ERR_PTR(phy_reg);
64 phy_id = (phy_reg & 0xffff) << 16;
66 /* Grab the bits from PHYIR2, and put them in the lower half */
67 phy_reg = bus->read(bus, addr, MII_PHYSID2);
70 return ERR_PTR(phy_reg);
72 phy_id |= (phy_reg & 0xffff);
74 /* If the phy_id is all Fs, there is no device there */
75 if (0xffffffff == phy_id)
78 /* Otherwise, we allocate the device, and initialize the
80 dev = kcalloc(1, sizeof(*dev), GFP_KERNEL);
83 return ERR_PTR(-ENOMEM);
87 dev->pause = dev->asym_pause = 0;
90 dev->autoneg = AUTONEG_ENABLE;
96 dev->state = PHY_DOWN;
98 spin_lock_init(&dev->lock);
105 * description: Tells the PHY infrastructure to handle the
106 * gory details on monitoring link status (whether through
107 * polling or an interrupt), and to call back to the
108 * connected device driver when the link status changes.
109 * If you want to monitor your own link state, don't call
111 void phy_prepare_link(struct phy_device *phydev,
112 void (*handler)(struct net_device *))
114 phydev->adjust_link = handler;
119 * description: Convenience function for connecting ethernet
120 * devices to PHY devices. The default behavior is for
121 * the PHY infrastructure to handle everything, and only notify
122 * the connected driver when the link status changes. If you
123 * don't want, or can't use the provided functionality, you may
124 * choose to call only the subset of functions which provide
125 * the desired functionality.
127 struct phy_device * phy_connect(struct net_device *dev, const char *phy_id,
128 void (*handler)(struct net_device *), u32 flags)
130 struct phy_device *phydev;
132 phydev = phy_attach(dev, phy_id, flags);
137 phy_prepare_link(phydev, handler);
139 phy_start_machine(phydev, NULL);
142 phy_start_interrupts(phydev);
146 EXPORT_SYMBOL(phy_connect);
148 void phy_disconnect(struct phy_device *phydev)
151 phy_stop_interrupts(phydev);
153 phy_stop_machine(phydev);
155 phydev->adjust_link = NULL;
159 EXPORT_SYMBOL(phy_disconnect);
163 * description: Called by drivers to attach to a particular PHY
164 * device. The phy_device is found, and properly hooked up
165 * to the phy_driver. If no driver is attached, then the
166 * genphy_driver is used. The phy_device is given a ptr to
167 * the attaching device, and given a callback for link status
168 * change. The phy_device is returned to the attaching
171 static int phy_compare_id(struct device *dev, void *data)
173 return strcmp((char *)data, dev->bus_id) ? 0 : 1;
176 struct phy_device *phy_attach(struct net_device *dev,
177 const char *phy_id, u32 flags)
179 struct bus_type *bus = &mdio_bus_type;
180 struct phy_device *phydev;
183 /* Search the list of PHY devices on the mdio bus for the
184 * PHY with the requested name */
185 d = bus_find_device(bus, NULL, (void *)phy_id, phy_compare_id);
188 phydev = to_phy_device(d);
190 printk(KERN_ERR "%s not found\n", phy_id);
191 return ERR_PTR(-ENODEV);
194 /* Assume that if there is no driver, that it doesn't
195 * exist, and we should use the genphy driver. */
196 if (NULL == d->driver) {
198 down_write(&d->bus->subsys.rwsem);
199 d->driver = &genphy_driver.driver;
201 err = d->driver->probe(d);
206 device_bind_driver(d);
207 up_write(&d->bus->subsys.rwsem);
210 if (phydev->attached_dev) {
211 printk(KERN_ERR "%s: %s already attached\n",
213 return ERR_PTR(-EBUSY);
216 phydev->attached_dev = dev;
218 phydev->dev_flags = flags;
222 EXPORT_SYMBOL(phy_attach);
224 void phy_detach(struct phy_device *phydev)
226 phydev->attached_dev = NULL;
228 /* If the device had no specific driver before (i.e. - it
229 * was using the generic driver), we unbind the device
230 * from the generic driver so that there's a chance a
231 * real driver could be loaded */
232 if (phydev->dev.driver == &genphy_driver.driver) {
233 down_write(&phydev->dev.bus->subsys.rwsem);
234 device_release_driver(&phydev->dev);
235 up_write(&phydev->dev.bus->subsys.rwsem);
238 EXPORT_SYMBOL(phy_detach);
241 /* Generic PHY support and helper functions */
243 /* genphy_config_advert
245 * description: Writes MII_ADVERTISE with the appropriate values,
246 * after sanitizing the values to make sure we only advertise
249 int genphy_config_advert(struct phy_device *phydev)
255 /* Only allow advertising what
256 * this PHY supports */
257 phydev->advertising &= phydev->supported;
258 advertise = phydev->advertising;
260 /* Setup standard advertisement */
261 adv = phy_read(phydev, MII_ADVERTISE);
266 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
267 ADVERTISE_PAUSE_ASYM);
268 if (advertise & ADVERTISED_10baseT_Half)
269 adv |= ADVERTISE_10HALF;
270 if (advertise & ADVERTISED_10baseT_Full)
271 adv |= ADVERTISE_10FULL;
272 if (advertise & ADVERTISED_100baseT_Half)
273 adv |= ADVERTISE_100HALF;
274 if (advertise & ADVERTISED_100baseT_Full)
275 adv |= ADVERTISE_100FULL;
276 if (advertise & ADVERTISED_Pause)
277 adv |= ADVERTISE_PAUSE_CAP;
278 if (advertise & ADVERTISED_Asym_Pause)
279 adv |= ADVERTISE_PAUSE_ASYM;
281 err = phy_write(phydev, MII_ADVERTISE, adv);
286 /* Configure gigabit if it's supported */
287 if (phydev->supported & (SUPPORTED_1000baseT_Half |
288 SUPPORTED_1000baseT_Full)) {
289 adv = phy_read(phydev, MII_CTRL1000);
294 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
295 if (advertise & SUPPORTED_1000baseT_Half)
296 adv |= ADVERTISE_1000HALF;
297 if (advertise & SUPPORTED_1000baseT_Full)
298 adv |= ADVERTISE_1000FULL;
299 err = phy_write(phydev, MII_CTRL1000, adv);
307 EXPORT_SYMBOL(genphy_config_advert);
309 /* genphy_setup_forced
311 * description: Configures MII_BMCR to force speed/duplex
312 * to the values in phydev. Assumes that the values are valid.
313 * Please see phy_sanitize_settings() */
314 int genphy_setup_forced(struct phy_device *phydev)
316 int ctl = BMCR_RESET;
318 phydev->pause = phydev->asym_pause = 0;
320 if (SPEED_1000 == phydev->speed)
321 ctl |= BMCR_SPEED1000;
322 else if (SPEED_100 == phydev->speed)
323 ctl |= BMCR_SPEED100;
325 if (DUPLEX_FULL == phydev->duplex)
326 ctl |= BMCR_FULLDPLX;
328 ctl = phy_write(phydev, MII_BMCR, ctl);
333 /* We just reset the device, so we'd better configure any
334 * settings the PHY requires to operate */
335 if (phydev->drv->config_init)
336 ctl = phydev->drv->config_init(phydev);
342 /* Enable and Restart Autonegotiation */
343 int genphy_restart_aneg(struct phy_device *phydev)
347 ctl = phy_read(phydev, MII_BMCR);
352 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
354 /* Don't isolate the PHY if we're negotiating */
355 ctl &= ~(BMCR_ISOLATE);
357 ctl = phy_write(phydev, MII_BMCR, ctl);
363 /* genphy_config_aneg
365 * description: If auto-negotiation is enabled, we configure the
366 * advertising, and then restart auto-negotiation. If it is not
367 * enabled, then we write the BMCR
369 int genphy_config_aneg(struct phy_device *phydev)
373 if (AUTONEG_ENABLE == phydev->autoneg) {
374 err = genphy_config_advert(phydev);
379 err = genphy_restart_aneg(phydev);
381 err = genphy_setup_forced(phydev);
385 EXPORT_SYMBOL(genphy_config_aneg);
387 /* genphy_update_link
389 * description: Update the value in phydev->link to reflect the
390 * current link value. In order to do this, we need to read
391 * the status register twice, keeping the second value
393 int genphy_update_link(struct phy_device *phydev)
398 status = phy_read(phydev, MII_BMSR);
403 /* Read link and autonegotiation status */
404 status = phy_read(phydev, MII_BMSR);
409 if ((status & BMSR_LSTATUS) == 0)
417 /* genphy_read_status
419 * description: Check the link, then figure out the current state
420 * by comparing what we advertise with what the link partner
421 * advertises. Start by checking the gigabit possibilities,
422 * then move on to 10/100.
424 int genphy_read_status(struct phy_device *phydev)
431 /* Update the link, but return if there
433 err = genphy_update_link(phydev);
437 if (AUTONEG_ENABLE == phydev->autoneg) {
438 if (phydev->supported & (SUPPORTED_1000baseT_Half
439 | SUPPORTED_1000baseT_Full)) {
440 lpagb = phy_read(phydev, MII_STAT1000);
445 adv = phy_read(phydev, MII_CTRL1000);
453 lpa = phy_read(phydev, MII_LPA);
458 adv = phy_read(phydev, MII_ADVERTISE);
465 phydev->speed = SPEED_10;
466 phydev->duplex = DUPLEX_HALF;
467 phydev->pause = phydev->asym_pause = 0;
469 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
470 phydev->speed = SPEED_1000;
472 if (lpagb & LPA_1000FULL)
473 phydev->duplex = DUPLEX_FULL;
474 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
475 phydev->speed = SPEED_100;
477 if (lpa & LPA_100FULL)
478 phydev->duplex = DUPLEX_FULL;
480 if (lpa & LPA_10FULL)
481 phydev->duplex = DUPLEX_FULL;
483 if (phydev->duplex == DUPLEX_FULL){
484 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
485 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
488 int bmcr = phy_read(phydev, MII_BMCR);
492 if (bmcr & BMCR_FULLDPLX)
493 phydev->duplex = DUPLEX_FULL;
495 phydev->duplex = DUPLEX_HALF;
497 if (bmcr & BMCR_SPEED1000)
498 phydev->speed = SPEED_1000;
499 else if (bmcr & BMCR_SPEED100)
500 phydev->speed = SPEED_100;
502 phydev->speed = SPEED_10;
504 phydev->pause = phydev->asym_pause = 0;
509 EXPORT_SYMBOL(genphy_read_status);
511 static int genphy_config_init(struct phy_device *phydev)
516 /* For now, I'll claim that the generic driver supports
517 * all possible port types */
518 features = (SUPPORTED_TP | SUPPORTED_MII
519 | SUPPORTED_AUI | SUPPORTED_FIBRE |
522 /* Do we support autonegotiation? */
523 val = phy_read(phydev, MII_BMSR);
528 if (val & BMSR_ANEGCAPABLE)
529 features |= SUPPORTED_Autoneg;
531 if (val & BMSR_100FULL)
532 features |= SUPPORTED_100baseT_Full;
533 if (val & BMSR_100HALF)
534 features |= SUPPORTED_100baseT_Half;
535 if (val & BMSR_10FULL)
536 features |= SUPPORTED_10baseT_Full;
537 if (val & BMSR_10HALF)
538 features |= SUPPORTED_10baseT_Half;
540 if (val & BMSR_ESTATEN) {
541 val = phy_read(phydev, MII_ESTATUS);
546 if (val & ESTATUS_1000_TFULL)
547 features |= SUPPORTED_1000baseT_Full;
548 if (val & ESTATUS_1000_THALF)
549 features |= SUPPORTED_1000baseT_Half;
552 phydev->supported = features;
553 phydev->advertising = features;
561 * description: Take care of setting up the phy_device structure,
562 * set the state to READY (the driver's init function should
563 * set it to STARTING if needed).
565 static int phy_probe(struct device *dev)
567 struct phy_device *phydev;
568 struct phy_driver *phydrv;
569 struct device_driver *drv;
572 phydev = to_phy_device(dev);
574 /* Make sure the driver is held.
575 * XXX -- Is this correct? */
576 drv = get_driver(phydev->dev.driver);
577 phydrv = to_phy_driver(drv);
578 phydev->drv = phydrv;
580 /* Disable the interrupt if the PHY doesn't support it */
581 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
582 phydev->irq = PHY_POLL;
584 spin_lock(&phydev->lock);
586 /* Start out supporting everything. Eventually,
587 * a controller will attach, and may modify one
588 * or both of these values */
589 phydev->supported = phydrv->features;
590 phydev->advertising = phydrv->features;
592 /* Set the state to READY by default */
593 phydev->state = PHY_READY;
595 if (phydev->drv->probe)
596 err = phydev->drv->probe(phydev);
598 spin_unlock(&phydev->lock);
603 if (phydev->drv->config_init)
604 err = phydev->drv->config_init(phydev);
609 static int phy_remove(struct device *dev)
611 struct phy_device *phydev;
613 phydev = to_phy_device(dev);
615 spin_lock(&phydev->lock);
616 phydev->state = PHY_DOWN;
617 spin_unlock(&phydev->lock);
619 if (phydev->drv->remove)
620 phydev->drv->remove(phydev);
622 put_driver(dev->driver);
628 int phy_driver_register(struct phy_driver *new_driver)
632 memset(&new_driver->driver, 0, sizeof(new_driver->driver));
633 new_driver->driver.name = new_driver->name;
634 new_driver->driver.bus = &mdio_bus_type;
635 new_driver->driver.probe = phy_probe;
636 new_driver->driver.remove = phy_remove;
638 retval = driver_register(&new_driver->driver);
641 printk(KERN_ERR "%s: Error %d in registering driver\n",
642 new_driver->name, retval);
647 pr_info("%s: Registered new driver\n", new_driver->name);
651 EXPORT_SYMBOL(phy_driver_register);
653 void phy_driver_unregister(struct phy_driver *drv)
655 driver_unregister(&drv->driver);
657 EXPORT_SYMBOL(phy_driver_unregister);
659 static struct phy_driver genphy_driver = {
660 .phy_id = 0xffffffff,
661 .phy_id_mask = 0xffffffff,
662 .name = "Generic PHY",
663 .config_init = genphy_config_init,
665 .config_aneg = genphy_config_aneg,
666 .read_status = genphy_read_status,
667 .driver = {.owner= THIS_MODULE, },
670 static int __init phy_init(void)
674 rc = mdio_bus_init();
678 rc = phy_driver_register(&genphy_driver);
685 static void __exit phy_exit(void)
687 phy_driver_unregister(&genphy_driver);
691 subsys_initcall(phy_init);
692 module_exit(phy_exit);