net: support time stamping in phy devices.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / phy / phy_device.c
blobc0761197c07e6a94e67e3d0d4f417d888e2ec68f
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/kernel.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20 #include <linux/unistd.h>
21 #include <linux/slab.h>
22 #include <linux/interrupt.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/mm.h>
29 #include <linux/module.h>
30 #include <linux/mii.h>
31 #include <linux/ethtool.h>
32 #include <linux/phy.h>
34 #include <asm/io.h>
35 #include <asm/irq.h>
36 #include <asm/uaccess.h>
38 MODULE_DESCRIPTION("PHY library");
39 MODULE_AUTHOR("Andy Fleming");
40 MODULE_LICENSE("GPL");
42 void phy_device_free(struct phy_device *phydev)
44 kfree(phydev);
46 EXPORT_SYMBOL(phy_device_free);
48 static void phy_device_release(struct device *dev)
50 phy_device_free(to_phy_device(dev));
53 static struct phy_driver genphy_driver;
54 extern int mdio_bus_init(void);
55 extern void mdio_bus_exit(void);
57 static LIST_HEAD(phy_fixup_list);
58 static DEFINE_MUTEX(phy_fixup_lock);
61 * Creates a new phy_fixup and adds it to the list
62 * @bus_id: A string which matches phydev->dev.bus_id (or PHY_ANY_ID)
63 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
64 * It can also be PHY_ANY_UID
65 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
66 * comparison
67 * @run: The actual code to be run when a matching PHY is found
69 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
70 int (*run)(struct phy_device *))
72 struct phy_fixup *fixup;
74 fixup = kzalloc(sizeof(struct phy_fixup), GFP_KERNEL);
75 if (!fixup)
76 return -ENOMEM;
78 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
79 fixup->phy_uid = phy_uid;
80 fixup->phy_uid_mask = phy_uid_mask;
81 fixup->run = run;
83 mutex_lock(&phy_fixup_lock);
84 list_add_tail(&fixup->list, &phy_fixup_list);
85 mutex_unlock(&phy_fixup_lock);
87 return 0;
89 EXPORT_SYMBOL(phy_register_fixup);
91 /* Registers a fixup to be run on any PHY with the UID in phy_uid */
92 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
93 int (*run)(struct phy_device *))
95 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
97 EXPORT_SYMBOL(phy_register_fixup_for_uid);
99 /* Registers a fixup to be run on the PHY with id string bus_id */
100 int phy_register_fixup_for_id(const char *bus_id,
101 int (*run)(struct phy_device *))
103 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
105 EXPORT_SYMBOL(phy_register_fixup_for_id);
108 * Returns 1 if fixup matches phydev in bus_id and phy_uid.
109 * Fixups can be set to match any in one or more fields.
111 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
113 if (strcmp(fixup->bus_id, dev_name(&phydev->dev)) != 0)
114 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
115 return 0;
117 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
118 (phydev->phy_id & fixup->phy_uid_mask))
119 if (fixup->phy_uid != PHY_ANY_UID)
120 return 0;
122 return 1;
125 /* Runs any matching fixups for this phydev */
126 int phy_scan_fixups(struct phy_device *phydev)
128 struct phy_fixup *fixup;
130 mutex_lock(&phy_fixup_lock);
131 list_for_each_entry(fixup, &phy_fixup_list, list) {
132 if (phy_needs_fixup(phydev, fixup)) {
133 int err;
135 err = fixup->run(phydev);
137 if (err < 0) {
138 mutex_unlock(&phy_fixup_lock);
139 return err;
143 mutex_unlock(&phy_fixup_lock);
145 return 0;
147 EXPORT_SYMBOL(phy_scan_fixups);
149 struct phy_device* phy_device_create(struct mii_bus *bus, int addr, int phy_id)
151 struct phy_device *dev;
153 /* We allocate the device, and initialize the
154 * default values */
155 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
157 if (NULL == dev)
158 return (struct phy_device*) PTR_ERR((void*)-ENOMEM);
160 dev->dev.release = phy_device_release;
162 dev->speed = 0;
163 dev->duplex = -1;
164 dev->pause = dev->asym_pause = 0;
165 dev->link = 1;
166 dev->interface = PHY_INTERFACE_MODE_GMII;
168 dev->autoneg = AUTONEG_ENABLE;
170 dev->addr = addr;
171 dev->phy_id = phy_id;
172 dev->bus = bus;
173 dev->dev.parent = bus->parent;
174 dev->dev.bus = &mdio_bus_type;
175 dev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
176 dev_set_name(&dev->dev, PHY_ID_FMT, bus->id, addr);
178 dev->state = PHY_DOWN;
180 mutex_init(&dev->lock);
181 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
183 /* Request the appropriate module unconditionally; don't
184 bother trying to do so only if it isn't already loaded,
185 because that gets complicated. A hotplug event would have
186 done an unconditional modprobe anyway.
187 We don't do normal hotplug because it won't work for MDIO
188 -- because it relies on the device staying around for long
189 enough for the driver to get loaded. With MDIO, the NIC
190 driver will get bored and give up as soon as it finds that
191 there's no driver _already_ loaded. */
192 request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
194 return dev;
196 EXPORT_SYMBOL(phy_device_create);
199 * get_phy_id - reads the specified addr for its ID.
200 * @bus: the target MII bus
201 * @addr: PHY address on the MII bus
202 * @phy_id: where to store the ID retrieved.
204 * Description: Reads the ID registers of the PHY at @addr on the
205 * @bus, stores it in @phy_id and returns zero on success.
207 int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id)
209 int phy_reg;
211 /* Grab the bits from PHYIR1, and put them
212 * in the upper half */
213 phy_reg = bus->read(bus, addr, MII_PHYSID1);
215 if (phy_reg < 0)
216 return -EIO;
218 *phy_id = (phy_reg & 0xffff) << 16;
220 /* Grab the bits from PHYIR2, and put them in the lower half */
221 phy_reg = bus->read(bus, addr, MII_PHYSID2);
223 if (phy_reg < 0)
224 return -EIO;
226 *phy_id |= (phy_reg & 0xffff);
228 return 0;
230 EXPORT_SYMBOL(get_phy_id);
233 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
234 * @bus: the target MII bus
235 * @addr: PHY address on the MII bus
237 * Description: Reads the ID registers of the PHY at @addr on the
238 * @bus, then allocates and returns the phy_device to represent it.
240 struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
242 struct phy_device *dev = NULL;
243 u32 phy_id;
244 int r;
246 r = get_phy_id(bus, addr, &phy_id);
247 if (r)
248 return ERR_PTR(r);
250 /* If the phy_id is mostly Fs, there is no device there */
251 if ((phy_id & 0x1fffffff) == 0x1fffffff)
252 return NULL;
254 dev = phy_device_create(bus, addr, phy_id);
256 return dev;
258 EXPORT_SYMBOL(get_phy_device);
261 * phy_device_register - Register the phy device on the MDIO bus
262 * @phydev: phy_device structure to be added to the MDIO bus
264 int phy_device_register(struct phy_device *phydev)
266 int err;
268 /* Don't register a phy if one is already registered at this
269 * address */
270 if (phydev->bus->phy_map[phydev->addr])
271 return -EINVAL;
272 phydev->bus->phy_map[phydev->addr] = phydev;
274 /* Run all of the fixups for this PHY */
275 phy_scan_fixups(phydev);
277 err = device_register(&phydev->dev);
278 if (err) {
279 pr_err("phy %d failed to register\n", phydev->addr);
280 goto out;
283 return 0;
285 out:
286 phydev->bus->phy_map[phydev->addr] = NULL;
287 return err;
289 EXPORT_SYMBOL(phy_device_register);
292 * phy_find_first - finds the first PHY device on the bus
293 * @bus: the target MII bus
295 struct phy_device *phy_find_first(struct mii_bus *bus)
297 int addr;
299 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
300 if (bus->phy_map[addr])
301 return bus->phy_map[addr];
303 return NULL;
305 EXPORT_SYMBOL(phy_find_first);
308 * phy_prepare_link - prepares the PHY layer to monitor link status
309 * @phydev: target phy_device struct
310 * @handler: callback function for link status change notifications
312 * Description: Tells the PHY infrastructure to handle the
313 * gory details on monitoring link status (whether through
314 * polling or an interrupt), and to call back to the
315 * connected device driver when the link status changes.
316 * If you want to monitor your own link state, don't call
317 * this function.
319 void phy_prepare_link(struct phy_device *phydev,
320 void (*handler)(struct net_device *))
322 phydev->adjust_link = handler;
326 * phy_connect_direct - connect an ethernet device to a specific phy_device
327 * @dev: the network device to connect
328 * @phydev: the pointer to the phy device
329 * @handler: callback function for state change notifications
330 * @flags: PHY device's dev_flags
331 * @interface: PHY device's interface
333 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
334 void (*handler)(struct net_device *), u32 flags,
335 phy_interface_t interface)
337 int rc;
339 rc = phy_attach_direct(dev, phydev, flags, interface);
340 if (rc)
341 return rc;
343 phy_prepare_link(phydev, handler);
344 phy_start_machine(phydev, NULL);
345 if (phydev->irq > 0)
346 phy_start_interrupts(phydev);
348 return 0;
350 EXPORT_SYMBOL(phy_connect_direct);
353 * phy_connect - connect an ethernet device to a PHY device
354 * @dev: the network device to connect
355 * @bus_id: the id string of the PHY device to connect
356 * @handler: callback function for state change notifications
357 * @flags: PHY device's dev_flags
358 * @interface: PHY device's interface
360 * Description: Convenience function for connecting ethernet
361 * devices to PHY devices. The default behavior is for
362 * the PHY infrastructure to handle everything, and only notify
363 * the connected driver when the link status changes. If you
364 * don't want, or can't use the provided functionality, you may
365 * choose to call only the subset of functions which provide
366 * the desired functionality.
368 struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
369 void (*handler)(struct net_device *), u32 flags,
370 phy_interface_t interface)
372 struct phy_device *phydev;
373 struct device *d;
374 int rc;
376 /* Search the list of PHY devices on the mdio bus for the
377 * PHY with the requested name */
378 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
379 if (!d) {
380 pr_err("PHY %s not found\n", bus_id);
381 return ERR_PTR(-ENODEV);
383 phydev = to_phy_device(d);
385 rc = phy_connect_direct(dev, phydev, handler, flags, interface);
386 if (rc)
387 return ERR_PTR(rc);
389 return phydev;
391 EXPORT_SYMBOL(phy_connect);
394 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device
395 * @phydev: target phy_device struct
397 void phy_disconnect(struct phy_device *phydev)
399 if (phydev->irq > 0)
400 phy_stop_interrupts(phydev);
402 phy_stop_machine(phydev);
404 phydev->adjust_link = NULL;
406 phy_detach(phydev);
408 EXPORT_SYMBOL(phy_disconnect);
410 int phy_init_hw(struct phy_device *phydev)
412 int ret;
414 if (!phydev->drv || !phydev->drv->config_init)
415 return 0;
417 ret = phy_scan_fixups(phydev);
418 if (ret < 0)
419 return ret;
421 return phydev->drv->config_init(phydev);
425 * phy_attach_direct - attach a network device to a given PHY device pointer
426 * @dev: network device to attach
427 * @phydev: Pointer to phy_device to attach
428 * @flags: PHY device's dev_flags
429 * @interface: PHY device's interface
431 * Description: Called by drivers to attach to a particular PHY
432 * device. The phy_device is found, and properly hooked up
433 * to the phy_driver. If no driver is attached, then the
434 * genphy_driver is used. The phy_device is given a ptr to
435 * the attaching device, and given a callback for link status
436 * change. The phy_device is returned to the attaching driver.
438 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
439 u32 flags, phy_interface_t interface)
441 struct device *d = &phydev->dev;
443 /* Assume that if there is no driver, that it doesn't
444 * exist, and we should use the genphy driver. */
445 if (NULL == d->driver) {
446 int err;
447 d->driver = &genphy_driver.driver;
449 err = d->driver->probe(d);
450 if (err >= 0)
451 err = device_bind_driver(d);
453 if (err)
454 return err;
457 if (phydev->attached_dev) {
458 dev_err(&dev->dev, "PHY already attached\n");
459 return -EBUSY;
462 phydev->attached_dev = dev;
463 dev->phydev = phydev;
465 phydev->dev_flags = flags;
467 phydev->interface = interface;
469 /* Do initial configuration here, now that
470 * we have certain key parameters
471 * (dev_flags and interface) */
472 return phy_init_hw(phydev);
474 EXPORT_SYMBOL(phy_attach_direct);
477 * phy_attach - attach a network device to a particular PHY device
478 * @dev: network device to attach
479 * @bus_id: Bus ID of PHY device to attach
480 * @flags: PHY device's dev_flags
481 * @interface: PHY device's interface
483 * Description: Same as phy_attach_direct() except that a PHY bus_id
484 * string is passed instead of a pointer to a struct phy_device.
486 struct phy_device *phy_attach(struct net_device *dev,
487 const char *bus_id, u32 flags, phy_interface_t interface)
489 struct bus_type *bus = &mdio_bus_type;
490 struct phy_device *phydev;
491 struct device *d;
492 int rc;
494 /* Search the list of PHY devices on the mdio bus for the
495 * PHY with the requested name */
496 d = bus_find_device_by_name(bus, NULL, bus_id);
497 if (!d) {
498 pr_err("PHY %s not found\n", bus_id);
499 return ERR_PTR(-ENODEV);
501 phydev = to_phy_device(d);
503 rc = phy_attach_direct(dev, phydev, flags, interface);
504 if (rc)
505 return ERR_PTR(rc);
507 return phydev;
509 EXPORT_SYMBOL(phy_attach);
512 * phy_detach - detach a PHY device from its network device
513 * @phydev: target phy_device struct
515 void phy_detach(struct phy_device *phydev)
517 phydev->attached_dev->phydev = NULL;
518 phydev->attached_dev = NULL;
520 /* If the device had no specific driver before (i.e. - it
521 * was using the generic driver), we unbind the device
522 * from the generic driver so that there's a chance a
523 * real driver could be loaded */
524 if (phydev->dev.driver == &genphy_driver.driver)
525 device_release_driver(&phydev->dev);
527 EXPORT_SYMBOL(phy_detach);
530 /* Generic PHY support and helper functions */
533 * genphy_config_advert - sanitize and advertise auto-negotation parameters
534 * @phydev: target phy_device struct
536 * Description: Writes MII_ADVERTISE with the appropriate values,
537 * after sanitizing the values to make sure we only advertise
538 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
539 * hasn't changed, and > 0 if it has changed.
541 int genphy_config_advert(struct phy_device *phydev)
543 u32 advertise;
544 int oldadv, adv;
545 int err, changed = 0;
547 /* Only allow advertising what
548 * this PHY supports */
549 phydev->advertising &= phydev->supported;
550 advertise = phydev->advertising;
552 /* Setup standard advertisement */
553 oldadv = adv = phy_read(phydev, MII_ADVERTISE);
555 if (adv < 0)
556 return adv;
558 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
559 ADVERTISE_PAUSE_ASYM);
560 if (advertise & ADVERTISED_10baseT_Half)
561 adv |= ADVERTISE_10HALF;
562 if (advertise & ADVERTISED_10baseT_Full)
563 adv |= ADVERTISE_10FULL;
564 if (advertise & ADVERTISED_100baseT_Half)
565 adv |= ADVERTISE_100HALF;
566 if (advertise & ADVERTISED_100baseT_Full)
567 adv |= ADVERTISE_100FULL;
568 if (advertise & ADVERTISED_Pause)
569 adv |= ADVERTISE_PAUSE_CAP;
570 if (advertise & ADVERTISED_Asym_Pause)
571 adv |= ADVERTISE_PAUSE_ASYM;
573 if (adv != oldadv) {
574 err = phy_write(phydev, MII_ADVERTISE, adv);
576 if (err < 0)
577 return err;
578 changed = 1;
581 /* Configure gigabit if it's supported */
582 if (phydev->supported & (SUPPORTED_1000baseT_Half |
583 SUPPORTED_1000baseT_Full)) {
584 oldadv = adv = phy_read(phydev, MII_CTRL1000);
586 if (adv < 0)
587 return adv;
589 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
590 if (advertise & SUPPORTED_1000baseT_Half)
591 adv |= ADVERTISE_1000HALF;
592 if (advertise & SUPPORTED_1000baseT_Full)
593 adv |= ADVERTISE_1000FULL;
595 if (adv != oldadv) {
596 err = phy_write(phydev, MII_CTRL1000, adv);
598 if (err < 0)
599 return err;
600 changed = 1;
604 return changed;
606 EXPORT_SYMBOL(genphy_config_advert);
609 * genphy_setup_forced - configures/forces speed/duplex from @phydev
610 * @phydev: target phy_device struct
612 * Description: Configures MII_BMCR to force speed/duplex
613 * to the values in phydev. Assumes that the values are valid.
614 * Please see phy_sanitize_settings().
616 int genphy_setup_forced(struct phy_device *phydev)
618 int err;
619 int ctl = 0;
621 phydev->pause = phydev->asym_pause = 0;
623 if (SPEED_1000 == phydev->speed)
624 ctl |= BMCR_SPEED1000;
625 else if (SPEED_100 == phydev->speed)
626 ctl |= BMCR_SPEED100;
628 if (DUPLEX_FULL == phydev->duplex)
629 ctl |= BMCR_FULLDPLX;
631 err = phy_write(phydev, MII_BMCR, ctl);
633 return err;
638 * genphy_restart_aneg - Enable and Restart Autonegotiation
639 * @phydev: target phy_device struct
641 int genphy_restart_aneg(struct phy_device *phydev)
643 int ctl;
645 ctl = phy_read(phydev, MII_BMCR);
647 if (ctl < 0)
648 return ctl;
650 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
652 /* Don't isolate the PHY if we're negotiating */
653 ctl &= ~(BMCR_ISOLATE);
655 ctl = phy_write(phydev, MII_BMCR, ctl);
657 return ctl;
659 EXPORT_SYMBOL(genphy_restart_aneg);
663 * genphy_config_aneg - restart auto-negotiation or write BMCR
664 * @phydev: target phy_device struct
666 * Description: If auto-negotiation is enabled, we configure the
667 * advertising, and then restart auto-negotiation. If it is not
668 * enabled, then we write the BMCR.
670 int genphy_config_aneg(struct phy_device *phydev)
672 int result;
674 if (AUTONEG_ENABLE != phydev->autoneg)
675 return genphy_setup_forced(phydev);
677 result = genphy_config_advert(phydev);
679 if (result < 0) /* error */
680 return result;
682 if (result == 0) {
683 /* Advertisment hasn't changed, but maybe aneg was never on to
684 * begin with? Or maybe phy was isolated? */
685 int ctl = phy_read(phydev, MII_BMCR);
687 if (ctl < 0)
688 return ctl;
690 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
691 result = 1; /* do restart aneg */
694 /* Only restart aneg if we are advertising something different
695 * than we were before. */
696 if (result > 0)
697 result = genphy_restart_aneg(phydev);
699 return result;
701 EXPORT_SYMBOL(genphy_config_aneg);
704 * genphy_update_link - update link status in @phydev
705 * @phydev: target phy_device struct
707 * Description: Update the value in phydev->link to reflect the
708 * current link value. In order to do this, we need to read
709 * the status register twice, keeping the second value.
711 int genphy_update_link(struct phy_device *phydev)
713 int status;
715 /* Do a fake read */
716 status = phy_read(phydev, MII_BMSR);
718 if (status < 0)
719 return status;
721 /* Read link and autonegotiation status */
722 status = phy_read(phydev, MII_BMSR);
724 if (status < 0)
725 return status;
727 if ((status & BMSR_LSTATUS) == 0)
728 phydev->link = 0;
729 else
730 phydev->link = 1;
732 return 0;
734 EXPORT_SYMBOL(genphy_update_link);
737 * genphy_read_status - check the link status and update current link state
738 * @phydev: target phy_device struct
740 * Description: Check the link, then figure out the current state
741 * by comparing what we advertise with what the link partner
742 * advertises. Start by checking the gigabit possibilities,
743 * then move on to 10/100.
745 int genphy_read_status(struct phy_device *phydev)
747 int adv;
748 int err;
749 int lpa;
750 int lpagb = 0;
752 /* Update the link, but return if there
753 * was an error */
754 err = genphy_update_link(phydev);
755 if (err)
756 return err;
758 if (AUTONEG_ENABLE == phydev->autoneg) {
759 if (phydev->supported & (SUPPORTED_1000baseT_Half
760 | SUPPORTED_1000baseT_Full)) {
761 lpagb = phy_read(phydev, MII_STAT1000);
763 if (lpagb < 0)
764 return lpagb;
766 adv = phy_read(phydev, MII_CTRL1000);
768 if (adv < 0)
769 return adv;
771 lpagb &= adv << 2;
774 lpa = phy_read(phydev, MII_LPA);
776 if (lpa < 0)
777 return lpa;
779 adv = phy_read(phydev, MII_ADVERTISE);
781 if (adv < 0)
782 return adv;
784 lpa &= adv;
786 phydev->speed = SPEED_10;
787 phydev->duplex = DUPLEX_HALF;
788 phydev->pause = phydev->asym_pause = 0;
790 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
791 phydev->speed = SPEED_1000;
793 if (lpagb & LPA_1000FULL)
794 phydev->duplex = DUPLEX_FULL;
795 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
796 phydev->speed = SPEED_100;
798 if (lpa & LPA_100FULL)
799 phydev->duplex = DUPLEX_FULL;
800 } else
801 if (lpa & LPA_10FULL)
802 phydev->duplex = DUPLEX_FULL;
804 if (phydev->duplex == DUPLEX_FULL){
805 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
806 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
808 } else {
809 int bmcr = phy_read(phydev, MII_BMCR);
810 if (bmcr < 0)
811 return bmcr;
813 if (bmcr & BMCR_FULLDPLX)
814 phydev->duplex = DUPLEX_FULL;
815 else
816 phydev->duplex = DUPLEX_HALF;
818 if (bmcr & BMCR_SPEED1000)
819 phydev->speed = SPEED_1000;
820 else if (bmcr & BMCR_SPEED100)
821 phydev->speed = SPEED_100;
822 else
823 phydev->speed = SPEED_10;
825 phydev->pause = phydev->asym_pause = 0;
828 return 0;
830 EXPORT_SYMBOL(genphy_read_status);
832 static int genphy_config_init(struct phy_device *phydev)
834 int val;
835 u32 features;
837 /* For now, I'll claim that the generic driver supports
838 * all possible port types */
839 features = (SUPPORTED_TP | SUPPORTED_MII
840 | SUPPORTED_AUI | SUPPORTED_FIBRE |
841 SUPPORTED_BNC);
843 /* Do we support autonegotiation? */
844 val = phy_read(phydev, MII_BMSR);
846 if (val < 0)
847 return val;
849 if (val & BMSR_ANEGCAPABLE)
850 features |= SUPPORTED_Autoneg;
852 if (val & BMSR_100FULL)
853 features |= SUPPORTED_100baseT_Full;
854 if (val & BMSR_100HALF)
855 features |= SUPPORTED_100baseT_Half;
856 if (val & BMSR_10FULL)
857 features |= SUPPORTED_10baseT_Full;
858 if (val & BMSR_10HALF)
859 features |= SUPPORTED_10baseT_Half;
861 if (val & BMSR_ESTATEN) {
862 val = phy_read(phydev, MII_ESTATUS);
864 if (val < 0)
865 return val;
867 if (val & ESTATUS_1000_TFULL)
868 features |= SUPPORTED_1000baseT_Full;
869 if (val & ESTATUS_1000_THALF)
870 features |= SUPPORTED_1000baseT_Half;
873 phydev->supported = features;
874 phydev->advertising = features;
876 return 0;
878 int genphy_suspend(struct phy_device *phydev)
880 int value;
882 mutex_lock(&phydev->lock);
884 value = phy_read(phydev, MII_BMCR);
885 phy_write(phydev, MII_BMCR, (value | BMCR_PDOWN));
887 mutex_unlock(&phydev->lock);
889 return 0;
891 EXPORT_SYMBOL(genphy_suspend);
893 int genphy_resume(struct phy_device *phydev)
895 int value;
897 mutex_lock(&phydev->lock);
899 value = phy_read(phydev, MII_BMCR);
900 phy_write(phydev, MII_BMCR, (value & ~BMCR_PDOWN));
902 mutex_unlock(&phydev->lock);
904 return 0;
906 EXPORT_SYMBOL(genphy_resume);
909 * phy_probe - probe and init a PHY device
910 * @dev: device to probe and init
912 * Description: Take care of setting up the phy_device structure,
913 * set the state to READY (the driver's init function should
914 * set it to STARTING if needed).
916 static int phy_probe(struct device *dev)
918 struct phy_device *phydev;
919 struct phy_driver *phydrv;
920 struct device_driver *drv;
921 int err = 0;
923 phydev = to_phy_device(dev);
925 /* Make sure the driver is held.
926 * XXX -- Is this correct? */
927 drv = get_driver(phydev->dev.driver);
928 phydrv = to_phy_driver(drv);
929 phydev->drv = phydrv;
931 /* Disable the interrupt if the PHY doesn't support it */
932 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
933 phydev->irq = PHY_POLL;
935 mutex_lock(&phydev->lock);
937 /* Start out supporting everything. Eventually,
938 * a controller will attach, and may modify one
939 * or both of these values */
940 phydev->supported = phydrv->features;
941 phydev->advertising = phydrv->features;
943 /* Set the state to READY by default */
944 phydev->state = PHY_READY;
946 if (phydev->drv->probe)
947 err = phydev->drv->probe(phydev);
949 mutex_unlock(&phydev->lock);
951 return err;
955 static int phy_remove(struct device *dev)
957 struct phy_device *phydev;
959 phydev = to_phy_device(dev);
961 mutex_lock(&phydev->lock);
962 phydev->state = PHY_DOWN;
963 mutex_unlock(&phydev->lock);
965 if (phydev->drv->remove)
966 phydev->drv->remove(phydev);
968 put_driver(dev->driver);
969 phydev->drv = NULL;
971 return 0;
975 * phy_driver_register - register a phy_driver with the PHY layer
976 * @new_driver: new phy_driver to register
978 int phy_driver_register(struct phy_driver *new_driver)
980 int retval;
982 new_driver->driver.name = new_driver->name;
983 new_driver->driver.bus = &mdio_bus_type;
984 new_driver->driver.probe = phy_probe;
985 new_driver->driver.remove = phy_remove;
987 retval = driver_register(&new_driver->driver);
989 if (retval) {
990 printk(KERN_ERR "%s: Error %d in registering driver\n",
991 new_driver->name, retval);
993 return retval;
996 pr_debug("%s: Registered new driver\n", new_driver->name);
998 return 0;
1000 EXPORT_SYMBOL(phy_driver_register);
1002 void phy_driver_unregister(struct phy_driver *drv)
1004 driver_unregister(&drv->driver);
1006 EXPORT_SYMBOL(phy_driver_unregister);
1008 static struct phy_driver genphy_driver = {
1009 .phy_id = 0xffffffff,
1010 .phy_id_mask = 0xffffffff,
1011 .name = "Generic PHY",
1012 .config_init = genphy_config_init,
1013 .features = 0,
1014 .config_aneg = genphy_config_aneg,
1015 .read_status = genphy_read_status,
1016 .suspend = genphy_suspend,
1017 .resume = genphy_resume,
1018 .driver = {.owner= THIS_MODULE, },
1021 static int __init phy_init(void)
1023 int rc;
1025 rc = mdio_bus_init();
1026 if (rc)
1027 return rc;
1029 rc = phy_driver_register(&genphy_driver);
1030 if (rc)
1031 mdio_bus_exit();
1033 return rc;
1036 static void __exit phy_exit(void)
1038 phy_driver_unregister(&genphy_driver);
1039 mdio_bus_exit();
1042 subsys_initcall(phy_init);
1043 module_exit(phy_exit);