Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / net / phy / phy_device.c
blob7da0e3dd5fe3ee44c5830761a77f7a1979e4a203
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/mii.h>
34 #include <linux/ethtool.h>
35 #include <linux/phy.h>
37 #include <asm/io.h>
38 #include <asm/irq.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);
49 /* get_phy_device
51 * description: Reads the ID registers of the PHY at addr on the
52 * bus, then allocates and returns the phy_device to
53 * represent it.
55 struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
57 int phy_reg;
58 u32 phy_id;
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);
65 if (phy_reg < 0)
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);
73 if (phy_reg < 0)
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)
80 return NULL;
82 /* Otherwise, we allocate the device, and initialize the
83 * default values */
84 dev = kcalloc(1, sizeof(*dev), GFP_KERNEL);
86 if (NULL == dev)
87 return ERR_PTR(-ENOMEM);
89 dev->speed = 0;
90 dev->duplex = -1;
91 dev->pause = dev->asym_pause = 0;
92 dev->link = 1;
94 dev->autoneg = AUTONEG_ENABLE;
96 dev->addr = addr;
97 dev->phy_id = phy_id;
98 dev->bus = bus;
100 dev->state = PHY_DOWN;
102 spin_lock_init(&dev->lock);
104 return dev;
107 /* phy_prepare_link:
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
114 * this function */
115 void phy_prepare_link(struct phy_device *phydev,
116 void (*handler)(struct net_device *))
118 phydev->adjust_link = handler;
121 /* phy_connect:
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);
138 if (IS_ERR(phydev))
139 return phydev;
141 phy_prepare_link(phydev, handler);
143 phy_start_machine(phydev, NULL);
145 if (phydev->irq > 0)
146 phy_start_interrupts(phydev);
148 return phydev;
150 EXPORT_SYMBOL(phy_connect);
152 void phy_disconnect(struct phy_device *phydev)
154 if (phydev->irq > 0)
155 phy_stop_interrupts(phydev);
157 phy_stop_machine(phydev);
159 phydev->adjust_link = NULL;
161 phy_detach(phydev);
163 EXPORT_SYMBOL(phy_disconnect);
165 /* phy_attach:
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
173 * driver.
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;
185 struct device *d;
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);
191 if (d) {
192 phydev = to_phy_device(d);
193 } else {
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) {
201 int err;
202 down_write(&d->bus->subsys.rwsem);
203 d->driver = &genphy_driver.driver;
205 err = d->driver->probe(d);
207 if (err < 0)
208 return ERR_PTR(err);
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",
216 dev->name, phy_id);
217 return ERR_PTR(-EBUSY);
220 phydev->attached_dev = dev;
222 phydev->dev_flags = flags;
224 return phydev;
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
251 * what is supported
253 int genphy_config_advert(struct phy_device *phydev)
255 u32 advertise;
256 int adv;
257 int err;
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);
267 if (adv < 0)
268 return adv;
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);
287 if (err < 0)
288 return err;
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);
295 if (adv < 0)
296 return adv;
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);
305 if (err < 0)
306 return err;
309 return 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);
334 if (ctl < 0)
335 return 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);
342 return ctl;
346 /* Enable and Restart Autonegotiation */
347 int genphy_restart_aneg(struct phy_device *phydev)
349 int ctl;
351 ctl = phy_read(phydev, MII_BMCR);
353 if (ctl < 0)
354 return ctl;
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);
363 return 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)
375 int err = 0;
377 if (AUTONEG_ENABLE == phydev->autoneg) {
378 err = genphy_config_advert(phydev);
380 if (err < 0)
381 return err;
383 err = genphy_restart_aneg(phydev);
384 } else
385 err = genphy_setup_forced(phydev);
387 return err;
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)
399 int status;
401 /* Do a fake read */
402 status = phy_read(phydev, MII_BMSR);
404 if (status < 0)
405 return status;
407 /* Read link and autonegotiation status */
408 status = phy_read(phydev, MII_BMSR);
410 if (status < 0)
411 return status;
413 if ((status & BMSR_LSTATUS) == 0)
414 phydev->link = 0;
415 else
416 phydev->link = 1;
418 return 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)
430 int adv;
431 int err;
432 int lpa;
433 int lpagb = 0;
435 /* Update the link, but return if there
436 * was an error */
437 err = genphy_update_link(phydev);
438 if (err)
439 return err;
441 if (AUTONEG_ENABLE == phydev->autoneg) {
442 if (phydev->supported & (SUPPORTED_1000baseT_Half
443 | SUPPORTED_1000baseT_Full)) {
444 lpagb = phy_read(phydev, MII_STAT1000);
446 if (lpagb < 0)
447 return lpagb;
449 adv = phy_read(phydev, MII_CTRL1000);
451 if (adv < 0)
452 return adv;
454 lpagb &= adv << 2;
457 lpa = phy_read(phydev, MII_LPA);
459 if (lpa < 0)
460 return lpa;
462 adv = phy_read(phydev, MII_ADVERTISE);
464 if (adv < 0)
465 return adv;
467 lpa &= adv;
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;
483 } else
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;
491 } else {
492 int bmcr = phy_read(phydev, MII_BMCR);
493 if (bmcr < 0)
494 return bmcr;
496 if (bmcr & BMCR_FULLDPLX)
497 phydev->duplex = DUPLEX_FULL;
498 else
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;
505 else
506 phydev->speed = SPEED_10;
508 phydev->pause = phydev->asym_pause = 0;
511 return 0;
513 EXPORT_SYMBOL(genphy_read_status);
515 static int genphy_config_init(struct phy_device *phydev)
517 u32 val;
518 u32 features;
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 |
524 SUPPORTED_BNC);
526 /* Do we support autonegotiation? */
527 val = phy_read(phydev, MII_BMSR);
529 if (val < 0)
530 return val;
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);
547 if (val < 0)
548 return val;
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;
559 return 0;
563 /* phy_probe
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;
574 int err = 0;
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);
604 if (err < 0)
605 return err;
607 if (phydev->drv->config_init)
608 err = phydev->drv->config_init(phydev);
610 return err;
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);
627 phydev->drv = NULL;
629 return 0;
632 int phy_driver_register(struct phy_driver *new_driver)
634 int retval;
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);
644 if (retval) {
645 printk(KERN_ERR "%s: Error %d in registering driver\n",
646 new_driver->name, retval);
648 return retval;
651 pr_info("%s: Registered new driver\n", new_driver->name);
653 return 0;
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,
668 .features = 0,
669 .config_aneg = genphy_config_aneg,
670 .read_status = genphy_read_status,
671 .driver = {.owner= THIS_MODULE, },
674 static int __init phy_init(void)
676 int rc;
678 rc = mdio_bus_init();
679 if (rc)
680 return rc;
682 rc = phy_driver_register(&genphy_driver);
683 if (rc)
684 mdio_bus_exit();
686 return rc;
689 static void __exit phy_exit(void)
691 phy_driver_unregister(&genphy_driver);
692 mdio_bus_exit();
695 subsys_initcall(phy_init);
696 module_exit(phy_exit);