Fix numerous minor problems with new phy subsystem.
[linux-2.6/verdex.git] / drivers / net / phy / phy_device.c
blobc11138330fed162b191a013cf291ce06b10024c7
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/version.h>
34 #include <linux/mii.h>
35 #include <linux/ethtool.h>
36 #include <linux/phy.h>
38 #include <asm/io.h>
39 #include <asm/irq.h>
40 #include <asm/uaccess.h>
42 static int genphy_config_init(struct phy_device *phydev);
44 static struct phy_driver genphy_driver = {
45 .phy_id = 0xffffffff,
46 .phy_id_mask = 0xffffffff,
47 .name = "Generic PHY",
48 .config_init = genphy_config_init,
49 .features = 0,
50 .config_aneg = genphy_config_aneg,
51 .read_status = genphy_read_status,
52 .driver = {.owner = THIS_MODULE, },
55 /* get_phy_device
57 * description: Reads the ID registers of the PHY at addr on the
58 * bus, then allocates and returns the phy_device to
59 * represent it.
61 struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
63 int phy_reg;
64 u32 phy_id;
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);
71 if (phy_reg < 0)
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);
79 if (phy_reg < 0)
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)
86 return NULL;
88 /* Otherwise, we allocate the device, and initialize the
89 * default values */
90 dev = kcalloc(1, sizeof(*dev), GFP_KERNEL);
92 if (NULL == dev)
93 return ERR_PTR(-ENOMEM);
95 dev->speed = 0;
96 dev->duplex = -1;
97 dev->pause = dev->asym_pause = 0;
98 dev->link = 1;
100 dev->autoneg = AUTONEG_ENABLE;
102 dev->addr = addr;
103 dev->phy_id = phy_id;
104 dev->bus = bus;
106 dev->state = PHY_DOWN;
108 spin_lock_init(&dev->lock);
110 return dev;
113 /* phy_prepare_link:
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
120 * this function */
121 void phy_prepare_link(struct phy_device *phydev,
122 void (*handler)(struct net_device *))
124 phydev->adjust_link = handler;
127 #ifdef CONFIG_PHYCONTROL
128 /* phy_connect:
130 * description: Convenience function for connecting ethernet
131 * devices to PHY devices. The default behavior is for
132 * the PHY infrastructure to handle everything, and only notify
133 * the connected driver when the link status changes. If you
134 * don't want, or can't use the provided functionality, you may
135 * choose to call only the subset of functions which provide
136 * the desired functionality.
138 struct phy_device * phy_connect(struct net_device *dev, const char *phy_id,
139 void (*handler)(struct net_device *), u32 flags)
141 struct phy_device *phydev;
143 phydev = phy_attach(dev, phy_id, flags);
145 if (IS_ERR(phydev))
146 return phydev;
148 phy_prepare_link(phydev, handler);
150 phy_start_machine(phydev, NULL);
152 if (phydev->irq > 0)
153 phy_start_interrupts(phydev);
155 return phydev;
157 EXPORT_SYMBOL(phy_connect);
159 void phy_disconnect(struct phy_device *phydev)
161 if (phydev->irq > 0)
162 phy_stop_interrupts(phydev);
164 phy_stop_machine(phydev);
166 phydev->adjust_link = NULL;
168 phy_detach(phydev);
170 EXPORT_SYMBOL(phy_disconnect);
172 #endif /* CONFIG_PHYCONTROL */
174 /* phy_attach:
176 * description: Called by drivers to attach to a particular PHY
177 * device. The phy_device is found, and properly hooked up
178 * to the phy_driver. If no driver is attached, then the
179 * genphy_driver is used. The phy_device is given a ptr to
180 * the attaching device, and given a callback for link status
181 * change. The phy_device is returned to the attaching
182 * driver.
184 static int phy_compare_id(struct device *dev, void *data)
186 return strcmp((char *)data, dev->bus_id) ? 0 : 1;
189 struct phy_device *phy_attach(struct net_device *dev,
190 const char *phy_id, u32 flags)
192 struct bus_type *bus = &mdio_bus_type;
193 struct phy_device *phydev;
194 struct device *d;
196 /* Search the list of PHY devices on the mdio bus for the
197 * PHY with the requested name */
198 d = bus_find_device(bus, NULL, (void *)phy_id, phy_compare_id);
200 if (d) {
201 phydev = to_phy_device(d);
202 } else {
203 printk(KERN_ERR "%s not found\n", phy_id);
204 return ERR_PTR(-ENODEV);
207 /* Assume that if there is no driver, that it doesn't
208 * exist, and we should use the genphy driver. */
209 if (NULL == d->driver) {
210 int err;
211 down_write(&d->bus->subsys.rwsem);
212 d->driver = &genphy_driver.driver;
214 err = d->driver->probe(d);
216 if (err < 0)
217 return ERR_PTR(err);
219 device_bind_driver(d);
220 up_write(&d->bus->subsys.rwsem);
223 if (phydev->attached_dev) {
224 printk(KERN_ERR "%s: %s already attached\n",
225 dev->name, phy_id);
226 return ERR_PTR(-EBUSY);
229 phydev->attached_dev = dev;
231 phydev->dev_flags = flags;
233 return phydev;
235 EXPORT_SYMBOL(phy_attach);
237 void phy_detach(struct phy_device *phydev)
239 phydev->attached_dev = NULL;
241 /* If the device had no specific driver before (i.e. - it
242 * was using the generic driver), we unbind the device
243 * from the generic driver so that there's a chance a
244 * real driver could be loaded */
245 if (phydev->dev.driver == &genphy_driver.driver) {
246 down_write(&phydev->dev.bus->subsys.rwsem);
247 device_release_driver(&phydev->dev);
248 up_write(&phydev->dev.bus->subsys.rwsem);
251 EXPORT_SYMBOL(phy_detach);
254 /* Generic PHY support and helper functions */
256 /* genphy_config_advert
258 * description: Writes MII_ADVERTISE with the appropriate values,
259 * after sanitizing the values to make sure we only advertise
260 * what is supported
262 int genphy_config_advert(struct phy_device *phydev)
264 u32 advertise;
265 int adv;
266 int err;
268 /* Only allow advertising what
269 * this PHY supports */
270 phydev->advertising &= phydev->supported;
271 advertise = phydev->advertising;
273 /* Setup standard advertisement */
274 adv = phy_read(phydev, MII_ADVERTISE);
276 if (adv < 0)
277 return adv;
279 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
280 ADVERTISE_PAUSE_ASYM);
281 if (advertise & ADVERTISED_10baseT_Half)
282 adv |= ADVERTISE_10HALF;
283 if (advertise & ADVERTISED_10baseT_Full)
284 adv |= ADVERTISE_10FULL;
285 if (advertise & ADVERTISED_100baseT_Half)
286 adv |= ADVERTISE_100HALF;
287 if (advertise & ADVERTISED_100baseT_Full)
288 adv |= ADVERTISE_100FULL;
289 if (advertise & ADVERTISED_Pause)
290 adv |= ADVERTISE_PAUSE_CAP;
291 if (advertise & ADVERTISED_Asym_Pause)
292 adv |= ADVERTISE_PAUSE_ASYM;
294 err = phy_write(phydev, MII_ADVERTISE, adv);
296 if (err < 0)
297 return err;
299 /* Configure gigabit if it's supported */
300 if (phydev->supported & (SUPPORTED_1000baseT_Half |
301 SUPPORTED_1000baseT_Full)) {
302 adv = phy_read(phydev, MII_CTRL1000);
304 if (adv < 0)
305 return adv;
307 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
308 if (advertise & SUPPORTED_1000baseT_Half)
309 adv |= ADVERTISE_1000HALF;
310 if (advertise & SUPPORTED_1000baseT_Full)
311 adv |= ADVERTISE_1000FULL;
312 err = phy_write(phydev, MII_CTRL1000, adv);
314 if (err < 0)
315 return err;
318 return adv;
320 EXPORT_SYMBOL(genphy_config_advert);
322 /* genphy_setup_forced
324 * description: Configures MII_BMCR to force speed/duplex
325 * to the values in phydev. Assumes that the values are valid.
326 * Please see phy_sanitize_settings() */
327 int genphy_setup_forced(struct phy_device *phydev)
329 int ctl = BMCR_RESET;
331 phydev->pause = phydev->asym_pause = 0;
333 if (SPEED_1000 == phydev->speed)
334 ctl |= BMCR_SPEED1000;
335 else if (SPEED_100 == phydev->speed)
336 ctl |= BMCR_SPEED100;
338 if (DUPLEX_FULL == phydev->duplex)
339 ctl |= BMCR_FULLDPLX;
341 ctl = phy_write(phydev, MII_BMCR, ctl);
343 if (ctl < 0)
344 return ctl;
346 /* We just reset the device, so we'd better configure any
347 * settings the PHY requires to operate */
348 if (phydev->drv->config_init)
349 ctl = phydev->drv->config_init(phydev);
351 return ctl;
355 /* Enable and Restart Autonegotiation */
356 int genphy_restart_aneg(struct phy_device *phydev)
358 int ctl;
360 ctl = phy_read(phydev, MII_BMCR);
362 if (ctl < 0)
363 return ctl;
365 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
367 /* Don't isolate the PHY if we're negotiating */
368 ctl &= ~(BMCR_ISOLATE);
370 ctl = phy_write(phydev, MII_BMCR, ctl);
372 return ctl;
376 /* genphy_config_aneg
378 * description: If auto-negotiation is enabled, we configure the
379 * advertising, and then restart auto-negotiation. If it is not
380 * enabled, then we write the BMCR
382 int genphy_config_aneg(struct phy_device *phydev)
384 int err = 0;
386 if (AUTONEG_ENABLE == phydev->autoneg) {
387 err = genphy_config_advert(phydev);
389 if (err < 0)
390 return err;
392 err = genphy_restart_aneg(phydev);
393 } else
394 err = genphy_setup_forced(phydev);
396 return err;
398 EXPORT_SYMBOL(genphy_config_aneg);
400 /* genphy_update_link
402 * description: Update the value in phydev->link to reflect the
403 * current link value. In order to do this, we need to read
404 * the status register twice, keeping the second value
406 int genphy_update_link(struct phy_device *phydev)
408 int status;
410 /* Do a fake read */
411 status = phy_read(phydev, MII_BMSR);
413 if (status < 0)
414 return status;
416 /* Read link and autonegotiation status */
417 status = phy_read(phydev, MII_BMSR);
419 if (status < 0)
420 return status;
422 if ((status & BMSR_LSTATUS) == 0)
423 phydev->link = 0;
424 else
425 phydev->link = 1;
427 return 0;
430 /* genphy_read_status
432 * description: Check the link, then figure out the current state
433 * by comparing what we advertise with what the link partner
434 * advertises. Start by checking the gigabit possibilities,
435 * then move on to 10/100.
437 int genphy_read_status(struct phy_device *phydev)
439 int adv;
440 int err;
441 int lpa;
442 int lpagb = 0;
444 /* Update the link, but return if there
445 * was an error */
446 err = genphy_update_link(phydev);
447 if (err)
448 return err;
450 if (AUTONEG_ENABLE == phydev->autoneg) {
451 if (phydev->supported & (SUPPORTED_1000baseT_Half
452 | SUPPORTED_1000baseT_Full)) {
453 lpagb = phy_read(phydev, MII_STAT1000);
455 if (lpagb < 0)
456 return lpagb;
458 adv = phy_read(phydev, MII_CTRL1000);
460 if (adv < 0)
461 return adv;
463 lpagb &= adv << 2;
466 lpa = phy_read(phydev, MII_LPA);
468 if (lpa < 0)
469 return lpa;
471 adv = phy_read(phydev, MII_ADVERTISE);
473 if (adv < 0)
474 return adv;
476 lpa &= adv;
478 phydev->speed = SPEED_10;
479 phydev->duplex = DUPLEX_HALF;
480 phydev->pause = phydev->asym_pause = 0;
482 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
483 phydev->speed = SPEED_1000;
485 if (lpagb & LPA_1000FULL)
486 phydev->duplex = DUPLEX_FULL;
487 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
488 phydev->speed = SPEED_100;
490 if (lpa & LPA_100FULL)
491 phydev->duplex = DUPLEX_FULL;
492 } else
493 if (lpa & LPA_10FULL)
494 phydev->duplex = DUPLEX_FULL;
496 if (phydev->duplex == DUPLEX_FULL){
497 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
498 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
500 } else {
501 int bmcr = phy_read(phydev, MII_BMCR);
502 if (bmcr < 0)
503 return bmcr;
505 if (bmcr & BMCR_FULLDPLX)
506 phydev->duplex = DUPLEX_FULL;
507 else
508 phydev->duplex = DUPLEX_HALF;
510 if (bmcr & BMCR_SPEED1000)
511 phydev->speed = SPEED_1000;
512 else if (bmcr & BMCR_SPEED100)
513 phydev->speed = SPEED_100;
514 else
515 phydev->speed = SPEED_10;
517 phydev->pause = phydev->asym_pause = 0;
520 return 0;
522 EXPORT_SYMBOL(genphy_read_status);
524 static int genphy_config_init(struct phy_device *phydev)
526 u32 val;
527 u32 features;
529 /* For now, I'll claim that the generic driver supports
530 * all possible port types */
531 features = (SUPPORTED_TP | SUPPORTED_MII
532 | SUPPORTED_AUI | SUPPORTED_FIBRE |
533 SUPPORTED_BNC);
535 /* Do we support autonegotiation? */
536 val = phy_read(phydev, MII_BMSR);
538 if (val < 0)
539 return val;
541 if (val & BMSR_ANEGCAPABLE)
542 features |= SUPPORTED_Autoneg;
544 if (val & BMSR_100FULL)
545 features |= SUPPORTED_100baseT_Full;
546 if (val & BMSR_100HALF)
547 features |= SUPPORTED_100baseT_Half;
548 if (val & BMSR_10FULL)
549 features |= SUPPORTED_10baseT_Full;
550 if (val & BMSR_10HALF)
551 features |= SUPPORTED_10baseT_Half;
553 if (val & BMSR_ESTATEN) {
554 val = phy_read(phydev, MII_ESTATUS);
556 if (val < 0)
557 return val;
559 if (val & ESTATUS_1000_TFULL)
560 features |= SUPPORTED_1000baseT_Full;
561 if (val & ESTATUS_1000_THALF)
562 features |= SUPPORTED_1000baseT_Half;
565 phydev->supported = features;
566 phydev->advertising = features;
568 return 0;
572 /* phy_probe
574 * description: Take care of setting up the phy_device structure,
575 * set the state to READY (the driver's init function should
576 * set it to STARTING if needed).
578 static int phy_probe(struct device *dev)
580 struct phy_device *phydev;
581 struct phy_driver *phydrv;
582 struct device_driver *drv;
583 int err = 0;
585 phydev = to_phy_device(dev);
587 /* Make sure the driver is held.
588 * XXX -- Is this correct? */
589 drv = get_driver(phydev->dev.driver);
590 phydrv = to_phy_driver(drv);
591 phydev->drv = phydrv;
593 /* Disable the interrupt if the PHY doesn't support it */
594 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
595 phydev->irq = PHY_POLL;
597 spin_lock(&phydev->lock);
599 /* Start out supporting everything. Eventually,
600 * a controller will attach, and may modify one
601 * or both of these values */
602 phydev->supported = phydrv->features;
603 phydev->advertising = phydrv->features;
605 /* Set the state to READY by default */
606 phydev->state = PHY_READY;
608 if (phydev->drv->probe)
609 err = phydev->drv->probe(phydev);
611 spin_unlock(&phydev->lock);
613 if (err < 0)
614 return err;
616 if (phydev->drv->config_init)
617 err = phydev->drv->config_init(phydev);
619 return err;
622 static int phy_remove(struct device *dev)
624 struct phy_device *phydev;
626 phydev = to_phy_device(dev);
628 spin_lock(&phydev->lock);
629 phydev->state = PHY_DOWN;
630 spin_unlock(&phydev->lock);
632 if (phydev->drv->remove)
633 phydev->drv->remove(phydev);
635 put_driver(dev->driver);
636 phydev->drv = NULL;
638 return 0;
641 int phy_driver_register(struct phy_driver *new_driver)
643 int retval;
645 memset(&new_driver->driver, 0, sizeof(new_driver->driver));
646 new_driver->driver.name = new_driver->name;
647 new_driver->driver.bus = &mdio_bus_type;
648 new_driver->driver.probe = phy_probe;
649 new_driver->driver.remove = phy_remove;
651 retval = driver_register(&new_driver->driver);
653 if (retval) {
654 printk(KERN_ERR "%s: Error %d in registering driver\n",
655 new_driver->name, retval);
657 return retval;
660 pr_info("%s: Registered new driver\n", new_driver->name);
662 return 0;
664 EXPORT_SYMBOL(phy_driver_register);
666 void phy_driver_unregister(struct phy_driver *drv)
668 driver_unregister(&drv->driver);
670 EXPORT_SYMBOL(phy_driver_unregister);
673 static int __init phy_init(void)
675 int rc;
676 extern int mdio_bus_init(void);
678 rc = phy_driver_register(&genphy_driver);
679 if (rc)
680 goto out;
682 rc = mdio_bus_init();
683 if (rc)
684 goto out_unreg;
686 return 0;
688 out_unreg:
689 phy_driver_unregister(&genphy_driver);
690 out:
691 return rc;
694 static void __exit phy_exit(void)
696 phy_driver_unregister(&genphy_driver);
699 module_init(phy_init);
700 module_exit(phy_exit);