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 MODULE_DESCRIPTION("PHY library");
42 MODULE_AUTHOR("Andy Fleming");
43 MODULE_LICENSE("GPL");
45 static struct phy_driver genphy_driver;
46 extern int mdio_bus_init(void);
47 extern void mdio_bus_exit(void);
51 * description: Reads the ID registers of the PHY at addr on the
52 * bus, then allocates and returns the phy_device to
55 struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
59 struct phy_device *dev = NULL;
61 /* Grab the bits from PHYIR1, and put them
62 * in the upper half */
63 phy_reg = bus->read(bus, addr, MII_PHYSID1);
66 return ERR_PTR(phy_reg);
68 phy_id = (phy_reg & 0xffff) << 16;
70 /* Grab the bits from PHYIR2, and put them in the lower half */
71 phy_reg = bus->read(bus, addr, MII_PHYSID2);
74 return ERR_PTR(phy_reg);
76 phy_id |= (phy_reg & 0xffff);
78 /* If the phy_id is all Fs, there is no device there */
79 if (0xffffffff == phy_id)
82 /* Otherwise, we allocate the device, and initialize the
84 dev = kcalloc(1, sizeof(*dev), GFP_KERNEL);
87 return ERR_PTR(-ENOMEM);
91 dev->pause = dev->asym_pause = 0;
94 dev->autoneg = AUTONEG_ENABLE;
100 dev->state = PHY_DOWN;
102 spin_lock_init(&dev->lock);
109 * description: Tells the PHY infrastructure to handle the
110 * gory details on monitoring link status (whether through
111 * polling or an interrupt), and to call back to the
112 * connected device driver when the link status changes.
113 * If you want to monitor your own link state, don't call
115 void phy_prepare_link(struct phy_device *phydev,
116 void (*handler)(struct net_device *))
118 phydev->adjust_link = handler;
123 * description: Convenience function for connecting ethernet
124 * devices to PHY devices. The default behavior is for
125 * the PHY infrastructure to handle everything, and only notify
126 * the connected driver when the link status changes. If you
127 * don't want, or can't use the provided functionality, you may
128 * choose to call only the subset of functions which provide
129 * the desired functionality.
131 struct phy_device * phy_connect(struct net_device *dev, const char *phy_id,
132 void (*handler)(struct net_device *), u32 flags)
134 struct phy_device *phydev;
136 phydev = phy_attach(dev, phy_id, flags);
141 phy_prepare_link(phydev, handler);
143 phy_start_machine(phydev, NULL);
146 phy_start_interrupts(phydev);
150 EXPORT_SYMBOL(phy_connect);
152 void phy_disconnect(struct phy_device *phydev)
155 phy_stop_interrupts(phydev);
157 phy_stop_machine(phydev);
159 phydev->adjust_link = NULL;
163 EXPORT_SYMBOL(phy_disconnect);
167 * description: Called by drivers to attach to a particular PHY
168 * device. The phy_device is found, and properly hooked up
169 * to the phy_driver. If no driver is attached, then the
170 * genphy_driver is used. The phy_device is given a ptr to
171 * the attaching device, and given a callback for link status
172 * change. The phy_device is returned to the attaching
175 static int phy_compare_id(struct device *dev, void *data)
177 return strcmp((char *)data, dev->bus_id) ? 0 : 1;
180 struct phy_device *phy_attach(struct net_device *dev,
181 const char *phy_id, u32 flags)
183 struct bus_type *bus = &mdio_bus_type;
184 struct phy_device *phydev;
187 /* Search the list of PHY devices on the mdio bus for the
188 * PHY with the requested name */
189 d = bus_find_device(bus, NULL, (void *)phy_id, phy_compare_id);
192 phydev = to_phy_device(d);
194 printk(KERN_ERR "%s not found\n", phy_id);
195 return ERR_PTR(-ENODEV);
198 /* Assume that if there is no driver, that it doesn't
199 * exist, and we should use the genphy driver. */
200 if (NULL == d->driver) {
202 down_write(&d->bus->subsys.rwsem);
203 d->driver = &genphy_driver.driver;
205 err = d->driver->probe(d);
210 device_bind_driver(d);
211 up_write(&d->bus->subsys.rwsem);
214 if (phydev->attached_dev) {
215 printk(KERN_ERR "%s: %s already attached\n",
217 return ERR_PTR(-EBUSY);
220 phydev->attached_dev = dev;
222 phydev->dev_flags = flags;
226 EXPORT_SYMBOL(phy_attach);
228 void phy_detach(struct phy_device *phydev)
230 phydev->attached_dev = NULL;
232 /* If the device had no specific driver before (i.e. - it
233 * was using the generic driver), we unbind the device
234 * from the generic driver so that there's a chance a
235 * real driver could be loaded */
236 if (phydev->dev.driver == &genphy_driver.driver) {
237 down_write(&phydev->dev.bus->subsys.rwsem);
238 device_release_driver(&phydev->dev);
239 up_write(&phydev->dev.bus->subsys.rwsem);
242 EXPORT_SYMBOL(phy_detach);
245 /* Generic PHY support and helper functions */
247 /* genphy_config_advert
249 * description: Writes MII_ADVERTISE with the appropriate values,
250 * after sanitizing the values to make sure we only advertise
253 int genphy_config_advert(struct phy_device *phydev)
259 /* Only allow advertising what
260 * this PHY supports */
261 phydev->advertising &= phydev->supported;
262 advertise = phydev->advertising;
264 /* Setup standard advertisement */
265 adv = phy_read(phydev, MII_ADVERTISE);
270 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
271 ADVERTISE_PAUSE_ASYM);
272 if (advertise & ADVERTISED_10baseT_Half)
273 adv |= ADVERTISE_10HALF;
274 if (advertise & ADVERTISED_10baseT_Full)
275 adv |= ADVERTISE_10FULL;
276 if (advertise & ADVERTISED_100baseT_Half)
277 adv |= ADVERTISE_100HALF;
278 if (advertise & ADVERTISED_100baseT_Full)
279 adv |= ADVERTISE_100FULL;
280 if (advertise & ADVERTISED_Pause)
281 adv |= ADVERTISE_PAUSE_CAP;
282 if (advertise & ADVERTISED_Asym_Pause)
283 adv |= ADVERTISE_PAUSE_ASYM;
285 err = phy_write(phydev, MII_ADVERTISE, adv);
290 /* Configure gigabit if it's supported */
291 if (phydev->supported & (SUPPORTED_1000baseT_Half |
292 SUPPORTED_1000baseT_Full)) {
293 adv = phy_read(phydev, MII_CTRL1000);
298 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
299 if (advertise & SUPPORTED_1000baseT_Half)
300 adv |= ADVERTISE_1000HALF;
301 if (advertise & SUPPORTED_1000baseT_Full)
302 adv |= ADVERTISE_1000FULL;
303 err = phy_write(phydev, MII_CTRL1000, adv);
311 EXPORT_SYMBOL(genphy_config_advert);
313 /* genphy_setup_forced
315 * description: Configures MII_BMCR to force speed/duplex
316 * to the values in phydev. Assumes that the values are valid.
317 * Please see phy_sanitize_settings() */
318 int genphy_setup_forced(struct phy_device *phydev)
320 int ctl = BMCR_RESET;
322 phydev->pause = phydev->asym_pause = 0;
324 if (SPEED_1000 == phydev->speed)
325 ctl |= BMCR_SPEED1000;
326 else if (SPEED_100 == phydev->speed)
327 ctl |= BMCR_SPEED100;
329 if (DUPLEX_FULL == phydev->duplex)
330 ctl |= BMCR_FULLDPLX;
332 ctl = phy_write(phydev, MII_BMCR, ctl);
337 /* We just reset the device, so we'd better configure any
338 * settings the PHY requires to operate */
339 if (phydev->drv->config_init)
340 ctl = phydev->drv->config_init(phydev);
346 /* Enable and Restart Autonegotiation */
347 int genphy_restart_aneg(struct phy_device *phydev)
351 ctl = phy_read(phydev, MII_BMCR);
356 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
358 /* Don't isolate the PHY if we're negotiating */
359 ctl &= ~(BMCR_ISOLATE);
361 ctl = phy_write(phydev, MII_BMCR, ctl);
367 /* genphy_config_aneg
369 * description: If auto-negotiation is enabled, we configure the
370 * advertising, and then restart auto-negotiation. If it is not
371 * enabled, then we write the BMCR
373 int genphy_config_aneg(struct phy_device *phydev)
377 if (AUTONEG_ENABLE == phydev->autoneg) {
378 err = genphy_config_advert(phydev);
383 err = genphy_restart_aneg(phydev);
385 err = genphy_setup_forced(phydev);
389 EXPORT_SYMBOL(genphy_config_aneg);
391 /* genphy_update_link
393 * description: Update the value in phydev->link to reflect the
394 * current link value. In order to do this, we need to read
395 * the status register twice, keeping the second value
397 int genphy_update_link(struct phy_device *phydev)
402 status = phy_read(phydev, MII_BMSR);
407 /* Read link and autonegotiation status */
408 status = phy_read(phydev, MII_BMSR);
413 if ((status & BMSR_LSTATUS) == 0)
421 /* genphy_read_status
423 * description: Check the link, then figure out the current state
424 * by comparing what we advertise with what the link partner
425 * advertises. Start by checking the gigabit possibilities,
426 * then move on to 10/100.
428 int genphy_read_status(struct phy_device *phydev)
435 /* Update the link, but return if there
437 err = genphy_update_link(phydev);
441 if (AUTONEG_ENABLE == phydev->autoneg) {
442 if (phydev->supported & (SUPPORTED_1000baseT_Half
443 | SUPPORTED_1000baseT_Full)) {
444 lpagb = phy_read(phydev, MII_STAT1000);
449 adv = phy_read(phydev, MII_CTRL1000);
457 lpa = phy_read(phydev, MII_LPA);
462 adv = phy_read(phydev, MII_ADVERTISE);
469 phydev->speed = SPEED_10;
470 phydev->duplex = DUPLEX_HALF;
471 phydev->pause = phydev->asym_pause = 0;
473 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
474 phydev->speed = SPEED_1000;
476 if (lpagb & LPA_1000FULL)
477 phydev->duplex = DUPLEX_FULL;
478 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
479 phydev->speed = SPEED_100;
481 if (lpa & LPA_100FULL)
482 phydev->duplex = DUPLEX_FULL;
484 if (lpa & LPA_10FULL)
485 phydev->duplex = DUPLEX_FULL;
487 if (phydev->duplex == DUPLEX_FULL){
488 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
489 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
492 int bmcr = phy_read(phydev, MII_BMCR);
496 if (bmcr & BMCR_FULLDPLX)
497 phydev->duplex = DUPLEX_FULL;
499 phydev->duplex = DUPLEX_HALF;
501 if (bmcr & BMCR_SPEED1000)
502 phydev->speed = SPEED_1000;
503 else if (bmcr & BMCR_SPEED100)
504 phydev->speed = SPEED_100;
506 phydev->speed = SPEED_10;
508 phydev->pause = phydev->asym_pause = 0;
513 EXPORT_SYMBOL(genphy_read_status);
515 static int genphy_config_init(struct phy_device *phydev)
520 /* For now, I'll claim that the generic driver supports
521 * all possible port types */
522 features = (SUPPORTED_TP | SUPPORTED_MII
523 | SUPPORTED_AUI | SUPPORTED_FIBRE |
526 /* Do we support autonegotiation? */
527 val = phy_read(phydev, MII_BMSR);
532 if (val & BMSR_ANEGCAPABLE)
533 features |= SUPPORTED_Autoneg;
535 if (val & BMSR_100FULL)
536 features |= SUPPORTED_100baseT_Full;
537 if (val & BMSR_100HALF)
538 features |= SUPPORTED_100baseT_Half;
539 if (val & BMSR_10FULL)
540 features |= SUPPORTED_10baseT_Full;
541 if (val & BMSR_10HALF)
542 features |= SUPPORTED_10baseT_Half;
544 if (val & BMSR_ESTATEN) {
545 val = phy_read(phydev, MII_ESTATUS);
550 if (val & ESTATUS_1000_TFULL)
551 features |= SUPPORTED_1000baseT_Full;
552 if (val & ESTATUS_1000_THALF)
553 features |= SUPPORTED_1000baseT_Half;
556 phydev->supported = features;
557 phydev->advertising = features;
565 * description: Take care of setting up the phy_device structure,
566 * set the state to READY (the driver's init function should
567 * set it to STARTING if needed).
569 static int phy_probe(struct device *dev)
571 struct phy_device *phydev;
572 struct phy_driver *phydrv;
573 struct device_driver *drv;
576 phydev = to_phy_device(dev);
578 /* Make sure the driver is held.
579 * XXX -- Is this correct? */
580 drv = get_driver(phydev->dev.driver);
581 phydrv = to_phy_driver(drv);
582 phydev->drv = phydrv;
584 /* Disable the interrupt if the PHY doesn't support it */
585 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
586 phydev->irq = PHY_POLL;
588 spin_lock(&phydev->lock);
590 /* Start out supporting everything. Eventually,
591 * a controller will attach, and may modify one
592 * or both of these values */
593 phydev->supported = phydrv->features;
594 phydev->advertising = phydrv->features;
596 /* Set the state to READY by default */
597 phydev->state = PHY_READY;
599 if (phydev->drv->probe)
600 err = phydev->drv->probe(phydev);
602 spin_unlock(&phydev->lock);
607 if (phydev->drv->config_init)
608 err = phydev->drv->config_init(phydev);
613 static int phy_remove(struct device *dev)
615 struct phy_device *phydev;
617 phydev = to_phy_device(dev);
619 spin_lock(&phydev->lock);
620 phydev->state = PHY_DOWN;
621 spin_unlock(&phydev->lock);
623 if (phydev->drv->remove)
624 phydev->drv->remove(phydev);
626 put_driver(dev->driver);
632 int phy_driver_register(struct phy_driver *new_driver)
636 memset(&new_driver->driver, 0, sizeof(new_driver->driver));
637 new_driver->driver.name = new_driver->name;
638 new_driver->driver.bus = &mdio_bus_type;
639 new_driver->driver.probe = phy_probe;
640 new_driver->driver.remove = phy_remove;
642 retval = driver_register(&new_driver->driver);
645 printk(KERN_ERR "%s: Error %d in registering driver\n",
646 new_driver->name, retval);
651 pr_info("%s: Registered new driver\n", new_driver->name);
655 EXPORT_SYMBOL(phy_driver_register);
657 void phy_driver_unregister(struct phy_driver *drv)
659 driver_unregister(&drv->driver);
661 EXPORT_SYMBOL(phy_driver_unregister);
663 static struct phy_driver genphy_driver = {
664 .phy_id = 0xffffffff,
665 .phy_id_mask = 0xffffffff,
666 .name = "Generic PHY",
667 .config_init = genphy_config_init,
669 .config_aneg = genphy_config_aneg,
670 .read_status = genphy_read_status,
671 .driver = {.owner= THIS_MODULE, },
674 static int __init phy_init(void)
678 rc = mdio_bus_init();
682 rc = phy_driver_register(&genphy_driver);
689 static void __exit phy_exit(void)
691 phy_driver_unregister(&genphy_driver);
695 subsys_initcall(phy_init);
696 module_exit(phy_exit);