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/version.h>
34 #include <linux/mii.h>
35 #include <linux/ethtool.h>
36 #include <linux/phy.h>
40 #include <asm/uaccess.h>
42 static int genphy_config_init(struct phy_device *phydev);
44 static struct phy_driver genphy_driver = {
46 .phy_id_mask = 0xffffffff,
47 .name = "Generic PHY",
48 .config_init = genphy_config_init,
50 .config_aneg = genphy_config_aneg,
51 .read_status = genphy_read_status,
52 .driver = {.owner = THIS_MODULE, },
57 * description: Reads the ID registers of the PHY at addr on the
58 * bus, then allocates and returns the phy_device to
61 struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
65 struct phy_device *dev = NULL;
67 /* Grab the bits from PHYIR1, and put them
68 * in the upper half */
69 phy_reg = bus->read(bus, addr, MII_PHYSID1);
72 return ERR_PTR(phy_reg);
74 phy_id = (phy_reg & 0xffff) << 16;
76 /* Grab the bits from PHYIR2, and put them in the lower half */
77 phy_reg = bus->read(bus, addr, MII_PHYSID2);
80 return ERR_PTR(phy_reg);
82 phy_id |= (phy_reg & 0xffff);
84 /* If the phy_id is all Fs, there is no device there */
85 if (0xffffffff == phy_id)
88 /* Otherwise, we allocate the device, and initialize the
90 dev = kcalloc(1, sizeof(*dev), GFP_KERNEL);
93 return ERR_PTR(-ENOMEM);
97 dev->pause = dev->asym_pause = 0;
100 dev->autoneg = AUTONEG_ENABLE;
103 dev->phy_id = phy_id;
106 dev->state = PHY_DOWN;
108 spin_lock_init(&dev->lock);
115 * description: Tells the PHY infrastructure to handle the
116 * gory details on monitoring link status (whether through
117 * polling or an interrupt), and to call back to the
118 * connected device driver when the link status changes.
119 * If you want to monitor your own link state, don't call
121 void phy_prepare_link(struct phy_device *phydev,
122 void (*handler)(struct net_device *))
124 phydev->adjust_link = handler;
127 /* Generic PHY support and helper functions */
129 /* genphy_config_advert
131 * description: Writes MII_ADVERTISE with the appropriate values,
132 * after sanitizing the values to make sure we only advertise
135 static int genphy_config_advert(struct phy_device *phydev)
141 /* Only allow advertising what
142 * this PHY supports */
143 phydev->advertising &= phydev->supported;
144 advertise = phydev->advertising;
146 /* Setup standard advertisement */
147 adv = phy_read(phydev, MII_ADVERTISE);
152 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
153 ADVERTISE_PAUSE_ASYM);
154 if (advertise & ADVERTISED_10baseT_Half)
155 adv |= ADVERTISE_10HALF;
156 if (advertise & ADVERTISED_10baseT_Full)
157 adv |= ADVERTISE_10FULL;
158 if (advertise & ADVERTISED_100baseT_Half)
159 adv |= ADVERTISE_100HALF;
160 if (advertise & ADVERTISED_100baseT_Full)
161 adv |= ADVERTISE_100FULL;
162 if (advertise & ADVERTISED_Pause)
163 adv |= ADVERTISE_PAUSE_CAP;
164 if (advertise & ADVERTISED_Asym_Pause)
165 adv |= ADVERTISE_PAUSE_ASYM;
167 err = phy_write(phydev, MII_ADVERTISE, adv);
172 /* Configure gigabit if it's supported */
173 if (phydev->supported & (SUPPORTED_1000baseT_Half |
174 SUPPORTED_1000baseT_Full)) {
175 adv = phy_read(phydev, MII_CTRL1000);
180 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
181 if (advertise & SUPPORTED_1000baseT_Half)
182 adv |= ADVERTISE_1000HALF;
183 if (advertise & SUPPORTED_1000baseT_Full)
184 adv |= ADVERTISE_1000FULL;
185 err = phy_write(phydev, MII_CTRL1000, adv);
194 /* genphy_setup_forced
196 * description: Configures MII_BMCR to force speed/duplex
197 * to the values in phydev. Assumes that the values are valid.
198 * Please see phy_sanitize_settings() */
199 int genphy_setup_forced(struct phy_device *phydev)
201 int ctl = BMCR_RESET;
203 phydev->pause = phydev->asym_pause = 0;
205 if (SPEED_1000 == phydev->speed)
206 ctl |= BMCR_SPEED1000;
207 else if (SPEED_100 == phydev->speed)
208 ctl |= BMCR_SPEED100;
210 if (DUPLEX_FULL == phydev->duplex)
211 ctl |= BMCR_FULLDPLX;
213 ctl = phy_write(phydev, MII_BMCR, ctl);
218 /* We just reset the device, so we'd better configure any
219 * settings the PHY requires to operate */
220 if (phydev->drv->config_init)
221 ctl = phydev->drv->config_init(phydev);
227 /* Enable and Restart Autonegotiation */
228 int genphy_restart_aneg(struct phy_device *phydev)
232 ctl = phy_read(phydev, MII_BMCR);
237 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
239 /* Don't isolate the PHY if we're negotiating */
240 ctl &= ~(BMCR_ISOLATE);
242 ctl = phy_write(phydev, MII_BMCR, ctl);
248 /* genphy_config_aneg
250 * description: If auto-negotiation is enabled, we configure the
251 * advertising, and then restart auto-negotiation. If it is not
252 * enabled, then we write the BMCR
254 int genphy_config_aneg(struct phy_device *phydev)
258 if (AUTONEG_ENABLE == phydev->autoneg) {
259 err = genphy_config_advert(phydev);
264 err = genphy_restart_aneg(phydev);
266 err = genphy_setup_forced(phydev);
270 EXPORT_SYMBOL(genphy_config_aneg);
272 /* genphy_update_link
274 * description: Update the value in phydev->link to reflect the
275 * current link value. In order to do this, we need to read
276 * the status register twice, keeping the second value
278 int genphy_update_link(struct phy_device *phydev)
283 status = phy_read(phydev, MII_BMSR);
288 /* Read link and autonegotiation status */
289 status = phy_read(phydev, MII_BMSR);
294 if ((status & BMSR_LSTATUS) == 0)
302 /* genphy_read_status
304 * description: Check the link, then figure out the current state
305 * by comparing what we advertise with what the link partner
306 * advertises. Start by checking the gigabit possibilities,
307 * then move on to 10/100.
309 int genphy_read_status(struct phy_device *phydev)
316 /* Update the link, but return if there
318 err = genphy_update_link(phydev);
322 if (AUTONEG_ENABLE == phydev->autoneg) {
323 if (phydev->supported & (SUPPORTED_1000baseT_Half
324 | SUPPORTED_1000baseT_Full)) {
325 lpagb = phy_read(phydev, MII_STAT1000);
330 adv = phy_read(phydev, MII_CTRL1000);
338 lpa = phy_read(phydev, MII_LPA);
343 adv = phy_read(phydev, MII_ADVERTISE);
350 phydev->speed = SPEED_10;
351 phydev->duplex = DUPLEX_HALF;
352 phydev->pause = phydev->asym_pause = 0;
354 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
355 phydev->speed = SPEED_1000;
357 if (lpagb & LPA_1000FULL)
358 phydev->duplex = DUPLEX_FULL;
359 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
360 phydev->speed = SPEED_100;
362 if (lpa & LPA_100FULL)
363 phydev->duplex = DUPLEX_FULL;
365 if (lpa & LPA_10FULL)
366 phydev->duplex = DUPLEX_FULL;
368 if (phydev->duplex == DUPLEX_FULL){
369 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
370 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
373 int bmcr = phy_read(phydev, MII_BMCR);
377 if (bmcr & BMCR_FULLDPLX)
378 phydev->duplex = DUPLEX_FULL;
380 phydev->duplex = DUPLEX_HALF;
382 if (bmcr & BMCR_SPEED1000)
383 phydev->speed = SPEED_1000;
384 else if (bmcr & BMCR_SPEED100)
385 phydev->speed = SPEED_100;
387 phydev->speed = SPEED_10;
389 phydev->pause = phydev->asym_pause = 0;
394 EXPORT_SYMBOL(genphy_read_status);
396 static int genphy_config_init(struct phy_device *phydev)
401 /* For now, I'll claim that the generic driver supports
402 * all possible port types */
403 features = (SUPPORTED_TP | SUPPORTED_MII
404 | SUPPORTED_AUI | SUPPORTED_FIBRE |
407 /* Do we support autonegotiation? */
408 val = phy_read(phydev, MII_BMSR);
413 if (val & BMSR_ANEGCAPABLE)
414 features |= SUPPORTED_Autoneg;
416 if (val & BMSR_100FULL)
417 features |= SUPPORTED_100baseT_Full;
418 if (val & BMSR_100HALF)
419 features |= SUPPORTED_100baseT_Half;
420 if (val & BMSR_10FULL)
421 features |= SUPPORTED_10baseT_Full;
422 if (val & BMSR_10HALF)
423 features |= SUPPORTED_10baseT_Half;
425 if (val & BMSR_ESTATEN) {
426 val = phy_read(phydev, MII_ESTATUS);
431 if (val & ESTATUS_1000_TFULL)
432 features |= SUPPORTED_1000baseT_Full;
433 if (val & ESTATUS_1000_THALF)
434 features |= SUPPORTED_1000baseT_Half;
437 phydev->supported = features;
438 phydev->advertising = features;
446 * description: Take care of setting up the phy_device structure,
447 * set the state to READY (the driver's init function should
448 * set it to STARTING if needed).
450 static int phy_probe(struct device *dev)
452 struct phy_device *phydev;
453 struct phy_driver *phydrv;
454 struct device_driver *drv;
457 phydev = to_phy_device(dev);
459 /* Make sure the driver is held.
460 * XXX -- Is this correct? */
461 drv = get_driver(phydev->dev.driver);
462 phydrv = to_phy_driver(drv);
463 phydev->drv = phydrv;
465 /* Disable the interrupt if the PHY doesn't support it */
466 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
467 phydev->irq = PHY_POLL;
469 spin_lock(&phydev->lock);
471 /* Start out supporting everything. Eventually,
472 * a controller will attach, and may modify one
473 * or both of these values */
474 phydev->supported = phydrv->features;
475 phydev->advertising = phydrv->features;
477 /* Set the state to READY by default */
478 phydev->state = PHY_READY;
480 if (phydev->drv->probe)
481 err = phydev->drv->probe(phydev);
483 spin_unlock(&phydev->lock);
488 if (phydev->drv->config_init)
489 err = phydev->drv->config_init(phydev);
494 static int phy_remove(struct device *dev)
496 struct phy_device *phydev;
498 phydev = to_phy_device(dev);
500 spin_lock(&phydev->lock);
501 phydev->state = PHY_DOWN;
502 spin_unlock(&phydev->lock);
504 if (phydev->drv->remove)
505 phydev->drv->remove(phydev);
507 put_driver(dev->driver);
513 int phy_driver_register(struct phy_driver *new_driver)
517 memset(&new_driver->driver, 0, sizeof(new_driver->driver));
518 new_driver->driver.name = new_driver->name;
519 new_driver->driver.bus = &mdio_bus_type;
520 new_driver->driver.probe = phy_probe;
521 new_driver->driver.remove = phy_remove;
523 retval = driver_register(&new_driver->driver);
526 printk(KERN_ERR "%s: Error %d in registering driver\n",
527 new_driver->name, retval);
532 pr_info("%s: Registered new driver\n", new_driver->name);
536 EXPORT_SYMBOL(phy_driver_register);
538 void phy_driver_unregister(struct phy_driver *drv)
540 driver_unregister(&drv->driver);
542 EXPORT_SYMBOL(phy_driver_unregister);
545 static int __init phy_init(void)
548 extern int mdio_bus_init(void);
550 rc = phy_driver_register(&genphy_driver);
554 rc = mdio_bus_init();
561 phy_driver_unregister(&genphy_driver);
566 static void __exit phy_exit(void)
568 phy_driver_unregister(&genphy_driver);
571 module_init(phy_init);
572 module_exit(phy_exit);