spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / drivers / net / phy / phy_device.c
blobf320f466f03b2fe9a2c30ef139d18ab5f6ae675d
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);
60 static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
61 u32 flags, phy_interface_t interface);
64 * Creates a new phy_fixup and adds it to the list
65 * @bus_id: A string which matches phydev->dev.bus_id (or PHY_ANY_ID)
66 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
67 * It can also be PHY_ANY_UID
68 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
69 * comparison
70 * @run: The actual code to be run when a matching PHY is found
72 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
73 int (*run)(struct phy_device *))
75 struct phy_fixup *fixup;
77 fixup = kzalloc(sizeof(struct phy_fixup), GFP_KERNEL);
78 if (!fixup)
79 return -ENOMEM;
81 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
82 fixup->phy_uid = phy_uid;
83 fixup->phy_uid_mask = phy_uid_mask;
84 fixup->run = run;
86 mutex_lock(&phy_fixup_lock);
87 list_add_tail(&fixup->list, &phy_fixup_list);
88 mutex_unlock(&phy_fixup_lock);
90 return 0;
92 EXPORT_SYMBOL(phy_register_fixup);
94 /* Registers a fixup to be run on any PHY with the UID in phy_uid */
95 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
96 int (*run)(struct phy_device *))
98 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
100 EXPORT_SYMBOL(phy_register_fixup_for_uid);
102 /* Registers a fixup to be run on the PHY with id string bus_id */
103 int phy_register_fixup_for_id(const char *bus_id,
104 int (*run)(struct phy_device *))
106 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
108 EXPORT_SYMBOL(phy_register_fixup_for_id);
111 * Returns 1 if fixup matches phydev in bus_id and phy_uid.
112 * Fixups can be set to match any in one or more fields.
114 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
116 if (strcmp(fixup->bus_id, dev_name(&phydev->dev)) != 0)
117 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
118 return 0;
120 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
121 (phydev->phy_id & fixup->phy_uid_mask))
122 if (fixup->phy_uid != PHY_ANY_UID)
123 return 0;
125 return 1;
128 /* Runs any matching fixups for this phydev */
129 int phy_scan_fixups(struct phy_device *phydev)
131 struct phy_fixup *fixup;
133 mutex_lock(&phy_fixup_lock);
134 list_for_each_entry(fixup, &phy_fixup_list, list) {
135 if (phy_needs_fixup(phydev, fixup)) {
136 int err;
138 err = fixup->run(phydev);
140 if (err < 0) {
141 mutex_unlock(&phy_fixup_lock);
142 return err;
146 mutex_unlock(&phy_fixup_lock);
148 return 0;
150 EXPORT_SYMBOL(phy_scan_fixups);
152 static struct phy_device* phy_device_create(struct mii_bus *bus,
153 int addr, int phy_id)
155 struct phy_device *dev;
157 /* We allocate the device, and initialize the
158 * default values */
159 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
161 if (NULL == dev)
162 return (struct phy_device*) PTR_ERR((void*)-ENOMEM);
164 dev->dev.release = phy_device_release;
166 dev->speed = 0;
167 dev->duplex = -1;
168 dev->pause = dev->asym_pause = 0;
169 dev->link = 1;
170 dev->interface = PHY_INTERFACE_MODE_GMII;
172 dev->autoneg = AUTONEG_ENABLE;
174 dev->addr = addr;
175 dev->phy_id = phy_id;
176 dev->bus = bus;
177 dev->dev.parent = bus->parent;
178 dev->dev.bus = &mdio_bus_type;
179 dev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
180 dev_set_name(&dev->dev, PHY_ID_FMT, bus->id, addr);
182 dev->state = PHY_DOWN;
184 mutex_init(&dev->lock);
185 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
187 /* Request the appropriate module unconditionally; don't
188 bother trying to do so only if it isn't already loaded,
189 because that gets complicated. A hotplug event would have
190 done an unconditional modprobe anyway.
191 We don't do normal hotplug because it won't work for MDIO
192 -- because it relies on the device staying around for long
193 enough for the driver to get loaded. With MDIO, the NIC
194 driver will get bored and give up as soon as it finds that
195 there's no driver _already_ loaded. */
196 request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
198 return dev;
202 * get_phy_id - reads the specified addr for its ID.
203 * @bus: the target MII bus
204 * @addr: PHY address on the MII bus
205 * @phy_id: where to store the ID retrieved.
207 * Description: Reads the ID registers of the PHY at @addr on the
208 * @bus, stores it in @phy_id and returns zero on success.
210 int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id)
212 int phy_reg;
214 /* Grab the bits from PHYIR1, and put them
215 * in the upper half */
216 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
218 if (phy_reg < 0)
219 return -EIO;
221 *phy_id = (phy_reg & 0xffff) << 16;
223 /* Grab the bits from PHYIR2, and put them in the lower half */
224 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
226 if (phy_reg < 0)
227 return -EIO;
229 *phy_id |= (phy_reg & 0xffff);
231 return 0;
233 EXPORT_SYMBOL(get_phy_id);
236 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
237 * @bus: the target MII bus
238 * @addr: PHY address on the MII bus
240 * Description: Reads the ID registers of the PHY at @addr on the
241 * @bus, then allocates and returns the phy_device to represent it.
243 struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
245 struct phy_device *dev = NULL;
246 u32 phy_id;
247 int r;
249 r = get_phy_id(bus, addr, &phy_id);
250 if (r)
251 return ERR_PTR(r);
253 /* If the phy_id is mostly Fs, there is no device there */
254 if ((phy_id & 0x1fffffff) == 0x1fffffff)
255 return NULL;
257 dev = phy_device_create(bus, addr, phy_id);
259 return dev;
261 EXPORT_SYMBOL(get_phy_device);
264 * phy_device_register - Register the phy device on the MDIO bus
265 * @phydev: phy_device structure to be added to the MDIO bus
267 int phy_device_register(struct phy_device *phydev)
269 int err;
271 /* Don't register a phy if one is already registered at this
272 * address */
273 if (phydev->bus->phy_map[phydev->addr])
274 return -EINVAL;
275 phydev->bus->phy_map[phydev->addr] = phydev;
277 /* Run all of the fixups for this PHY */
278 phy_scan_fixups(phydev);
280 err = device_register(&phydev->dev);
281 if (err) {
282 pr_err("phy %d failed to register\n", phydev->addr);
283 goto out;
286 return 0;
288 out:
289 phydev->bus->phy_map[phydev->addr] = NULL;
290 return err;
292 EXPORT_SYMBOL(phy_device_register);
295 * phy_find_first - finds the first PHY device on the bus
296 * @bus: the target MII bus
298 struct phy_device *phy_find_first(struct mii_bus *bus)
300 int addr;
302 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
303 if (bus->phy_map[addr])
304 return bus->phy_map[addr];
306 return NULL;
308 EXPORT_SYMBOL(phy_find_first);
311 * phy_prepare_link - prepares the PHY layer to monitor link status
312 * @phydev: target phy_device struct
313 * @handler: callback function for link status change notifications
315 * Description: Tells the PHY infrastructure to handle the
316 * gory details on monitoring link status (whether through
317 * polling or an interrupt), and to call back to the
318 * connected device driver when the link status changes.
319 * If you want to monitor your own link state, don't call
320 * this function.
322 static void phy_prepare_link(struct phy_device *phydev,
323 void (*handler)(struct net_device *))
325 phydev->adjust_link = handler;
329 * phy_connect_direct - connect an ethernet device to a specific phy_device
330 * @dev: the network device to connect
331 * @phydev: the pointer to the phy device
332 * @handler: callback function for state change notifications
333 * @flags: PHY device's dev_flags
334 * @interface: PHY device's interface
336 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
337 void (*handler)(struct net_device *), u32 flags,
338 phy_interface_t interface)
340 int rc;
342 rc = phy_attach_direct(dev, phydev, flags, interface);
343 if (rc)
344 return rc;
346 phy_prepare_link(phydev, handler);
347 phy_start_machine(phydev, NULL);
348 if (phydev->irq > 0)
349 phy_start_interrupts(phydev);
351 return 0;
353 EXPORT_SYMBOL(phy_connect_direct);
356 * phy_connect - connect an ethernet device to a PHY device
357 * @dev: the network device to connect
358 * @bus_id: the id string of the PHY device to connect
359 * @handler: callback function for state change notifications
360 * @flags: PHY device's dev_flags
361 * @interface: PHY device's interface
363 * Description: Convenience function for connecting ethernet
364 * devices to PHY devices. The default behavior is for
365 * the PHY infrastructure to handle everything, and only notify
366 * the connected driver when the link status changes. If you
367 * don't want, or can't use the provided functionality, you may
368 * choose to call only the subset of functions which provide
369 * the desired functionality.
371 struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
372 void (*handler)(struct net_device *), u32 flags,
373 phy_interface_t interface)
375 struct phy_device *phydev;
376 struct device *d;
377 int rc;
379 /* Search the list of PHY devices on the mdio bus for the
380 * PHY with the requested name */
381 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
382 if (!d) {
383 pr_err("PHY %s not found\n", bus_id);
384 return ERR_PTR(-ENODEV);
386 phydev = to_phy_device(d);
388 rc = phy_connect_direct(dev, phydev, handler, flags, interface);
389 if (rc)
390 return ERR_PTR(rc);
392 return phydev;
394 EXPORT_SYMBOL(phy_connect);
397 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device
398 * @phydev: target phy_device struct
400 void phy_disconnect(struct phy_device *phydev)
402 if (phydev->irq > 0)
403 phy_stop_interrupts(phydev);
405 phy_stop_machine(phydev);
407 phydev->adjust_link = NULL;
409 phy_detach(phydev);
411 EXPORT_SYMBOL(phy_disconnect);
413 int phy_init_hw(struct phy_device *phydev)
415 int ret;
417 if (!phydev->drv || !phydev->drv->config_init)
418 return 0;
420 ret = phy_scan_fixups(phydev);
421 if (ret < 0)
422 return ret;
424 return phydev->drv->config_init(phydev);
428 * phy_attach_direct - attach a network device to a given PHY device pointer
429 * @dev: network device to attach
430 * @phydev: Pointer to phy_device to attach
431 * @flags: PHY device's dev_flags
432 * @interface: PHY device's interface
434 * Description: Called by drivers to attach to a particular PHY
435 * device. The phy_device is found, and properly hooked up
436 * to the phy_driver. If no driver is attached, then the
437 * genphy_driver is used. The phy_device is given a ptr to
438 * the attaching device, and given a callback for link status
439 * change. The phy_device is returned to the attaching driver.
441 static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
442 u32 flags, phy_interface_t interface)
444 struct device *d = &phydev->dev;
445 int err;
447 /* Assume that if there is no driver, that it doesn't
448 * exist, and we should use the genphy driver. */
449 if (NULL == d->driver) {
450 d->driver = &genphy_driver.driver;
452 err = d->driver->probe(d);
453 if (err >= 0)
454 err = device_bind_driver(d);
456 if (err)
457 return err;
460 if (phydev->attached_dev) {
461 dev_err(&dev->dev, "PHY already attached\n");
462 return -EBUSY;
465 phydev->attached_dev = dev;
466 dev->phydev = phydev;
468 phydev->dev_flags = flags;
470 phydev->interface = interface;
472 phydev->state = PHY_READY;
474 /* Do initial configuration here, now that
475 * we have certain key parameters
476 * (dev_flags and interface) */
477 err = phy_init_hw(phydev);
478 if (err)
479 phy_detach(phydev);
481 return err;
485 * phy_attach - attach a network device to a particular PHY device
486 * @dev: network device to attach
487 * @bus_id: Bus ID of PHY device to attach
488 * @flags: PHY device's dev_flags
489 * @interface: PHY device's interface
491 * Description: Same as phy_attach_direct() except that a PHY bus_id
492 * string is passed instead of a pointer to a struct phy_device.
494 struct phy_device *phy_attach(struct net_device *dev,
495 const char *bus_id, u32 flags, phy_interface_t interface)
497 struct bus_type *bus = &mdio_bus_type;
498 struct phy_device *phydev;
499 struct device *d;
500 int rc;
502 /* Search the list of PHY devices on the mdio bus for the
503 * PHY with the requested name */
504 d = bus_find_device_by_name(bus, NULL, bus_id);
505 if (!d) {
506 pr_err("PHY %s not found\n", bus_id);
507 return ERR_PTR(-ENODEV);
509 phydev = to_phy_device(d);
511 rc = phy_attach_direct(dev, phydev, flags, interface);
512 if (rc)
513 return ERR_PTR(rc);
515 return phydev;
517 EXPORT_SYMBOL(phy_attach);
520 * phy_detach - detach a PHY device from its network device
521 * @phydev: target phy_device struct
523 void phy_detach(struct phy_device *phydev)
525 phydev->attached_dev->phydev = NULL;
526 phydev->attached_dev = NULL;
528 /* If the device had no specific driver before (i.e. - it
529 * was using the generic driver), we unbind the device
530 * from the generic driver so that there's a chance a
531 * real driver could be loaded */
532 if (phydev->dev.driver == &genphy_driver.driver)
533 device_release_driver(&phydev->dev);
535 EXPORT_SYMBOL(phy_detach);
538 /* Generic PHY support and helper functions */
541 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
542 * @phydev: target phy_device struct
544 * Description: Writes MII_ADVERTISE with the appropriate values,
545 * after sanitizing the values to make sure we only advertise
546 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
547 * hasn't changed, and > 0 if it has changed.
549 static int genphy_config_advert(struct phy_device *phydev)
551 u32 advertise;
552 int oldadv, adv;
553 int err, changed = 0;
555 /* Only allow advertising what
556 * this PHY supports */
557 phydev->advertising &= phydev->supported;
558 advertise = phydev->advertising;
560 /* Setup standard advertisement */
561 oldadv = adv = phy_read(phydev, MII_ADVERTISE);
563 if (adv < 0)
564 return adv;
566 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
567 ADVERTISE_PAUSE_ASYM);
568 adv |= ethtool_adv_to_mii_adv_t(advertise);
570 if (adv != oldadv) {
571 err = phy_write(phydev, MII_ADVERTISE, adv);
573 if (err < 0)
574 return err;
575 changed = 1;
578 /* Configure gigabit if it's supported */
579 if (phydev->supported & (SUPPORTED_1000baseT_Half |
580 SUPPORTED_1000baseT_Full)) {
581 oldadv = adv = phy_read(phydev, MII_CTRL1000);
583 if (adv < 0)
584 return adv;
586 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
587 adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
589 if (adv != oldadv) {
590 err = phy_write(phydev, MII_CTRL1000, adv);
592 if (err < 0)
593 return err;
594 changed = 1;
598 return changed;
602 * genphy_setup_forced - configures/forces speed/duplex from @phydev
603 * @phydev: target phy_device struct
605 * Description: Configures MII_BMCR to force speed/duplex
606 * to the values in phydev. Assumes that the values are valid.
607 * Please see phy_sanitize_settings().
609 static int genphy_setup_forced(struct phy_device *phydev)
611 int err;
612 int ctl = 0;
614 phydev->pause = phydev->asym_pause = 0;
616 if (SPEED_1000 == phydev->speed)
617 ctl |= BMCR_SPEED1000;
618 else if (SPEED_100 == phydev->speed)
619 ctl |= BMCR_SPEED100;
621 if (DUPLEX_FULL == phydev->duplex)
622 ctl |= BMCR_FULLDPLX;
624 err = phy_write(phydev, MII_BMCR, ctl);
626 return err;
631 * genphy_restart_aneg - Enable and Restart Autonegotiation
632 * @phydev: target phy_device struct
634 int genphy_restart_aneg(struct phy_device *phydev)
636 int ctl;
638 ctl = phy_read(phydev, MII_BMCR);
640 if (ctl < 0)
641 return ctl;
643 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
645 /* Don't isolate the PHY if we're negotiating */
646 ctl &= ~(BMCR_ISOLATE);
648 ctl = phy_write(phydev, MII_BMCR, ctl);
650 return ctl;
652 EXPORT_SYMBOL(genphy_restart_aneg);
656 * genphy_config_aneg - restart auto-negotiation or write BMCR
657 * @phydev: target phy_device struct
659 * Description: If auto-negotiation is enabled, we configure the
660 * advertising, and then restart auto-negotiation. If it is not
661 * enabled, then we write the BMCR.
663 int genphy_config_aneg(struct phy_device *phydev)
665 int result;
667 if (AUTONEG_ENABLE != phydev->autoneg)
668 return genphy_setup_forced(phydev);
670 result = genphy_config_advert(phydev);
672 if (result < 0) /* error */
673 return result;
675 if (result == 0) {
676 /* Advertisement hasn't changed, but maybe aneg was never on to
677 * begin with? Or maybe phy was isolated? */
678 int ctl = phy_read(phydev, MII_BMCR);
680 if (ctl < 0)
681 return ctl;
683 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
684 result = 1; /* do restart aneg */
687 /* Only restart aneg if we are advertising something different
688 * than we were before. */
689 if (result > 0)
690 result = genphy_restart_aneg(phydev);
692 return result;
694 EXPORT_SYMBOL(genphy_config_aneg);
697 * genphy_update_link - update link status in @phydev
698 * @phydev: target phy_device struct
700 * Description: Update the value in phydev->link to reflect the
701 * current link value. In order to do this, we need to read
702 * the status register twice, keeping the second value.
704 int genphy_update_link(struct phy_device *phydev)
706 int status;
708 /* Do a fake read */
709 status = phy_read(phydev, MII_BMSR);
711 if (status < 0)
712 return status;
714 /* Read link and autonegotiation status */
715 status = phy_read(phydev, MII_BMSR);
717 if (status < 0)
718 return status;
720 if ((status & BMSR_LSTATUS) == 0)
721 phydev->link = 0;
722 else
723 phydev->link = 1;
725 return 0;
727 EXPORT_SYMBOL(genphy_update_link);
730 * genphy_read_status - check the link status and update current link state
731 * @phydev: target phy_device struct
733 * Description: Check the link, then figure out the current state
734 * by comparing what we advertise with what the link partner
735 * advertises. Start by checking the gigabit possibilities,
736 * then move on to 10/100.
738 int genphy_read_status(struct phy_device *phydev)
740 int adv;
741 int err;
742 int lpa;
743 int lpagb = 0;
745 /* Update the link, but return if there
746 * was an error */
747 err = genphy_update_link(phydev);
748 if (err)
749 return err;
751 if (AUTONEG_ENABLE == phydev->autoneg) {
752 if (phydev->supported & (SUPPORTED_1000baseT_Half
753 | SUPPORTED_1000baseT_Full)) {
754 lpagb = phy_read(phydev, MII_STAT1000);
756 if (lpagb < 0)
757 return lpagb;
759 adv = phy_read(phydev, MII_CTRL1000);
761 if (adv < 0)
762 return adv;
764 lpagb &= adv << 2;
767 lpa = phy_read(phydev, MII_LPA);
769 if (lpa < 0)
770 return lpa;
772 adv = phy_read(phydev, MII_ADVERTISE);
774 if (adv < 0)
775 return adv;
777 lpa &= adv;
779 phydev->speed = SPEED_10;
780 phydev->duplex = DUPLEX_HALF;
781 phydev->pause = phydev->asym_pause = 0;
783 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
784 phydev->speed = SPEED_1000;
786 if (lpagb & LPA_1000FULL)
787 phydev->duplex = DUPLEX_FULL;
788 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
789 phydev->speed = SPEED_100;
791 if (lpa & LPA_100FULL)
792 phydev->duplex = DUPLEX_FULL;
793 } else
794 if (lpa & LPA_10FULL)
795 phydev->duplex = DUPLEX_FULL;
797 if (phydev->duplex == DUPLEX_FULL){
798 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
799 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
801 } else {
802 int bmcr = phy_read(phydev, MII_BMCR);
803 if (bmcr < 0)
804 return bmcr;
806 if (bmcr & BMCR_FULLDPLX)
807 phydev->duplex = DUPLEX_FULL;
808 else
809 phydev->duplex = DUPLEX_HALF;
811 if (bmcr & BMCR_SPEED1000)
812 phydev->speed = SPEED_1000;
813 else if (bmcr & BMCR_SPEED100)
814 phydev->speed = SPEED_100;
815 else
816 phydev->speed = SPEED_10;
818 phydev->pause = phydev->asym_pause = 0;
821 return 0;
823 EXPORT_SYMBOL(genphy_read_status);
825 static int genphy_config_init(struct phy_device *phydev)
827 int val;
828 u32 features;
830 /* For now, I'll claim that the generic driver supports
831 * all possible port types */
832 features = (SUPPORTED_TP | SUPPORTED_MII
833 | SUPPORTED_AUI | SUPPORTED_FIBRE |
834 SUPPORTED_BNC);
836 /* Do we support autonegotiation? */
837 val = phy_read(phydev, MII_BMSR);
839 if (val < 0)
840 return val;
842 if (val & BMSR_ANEGCAPABLE)
843 features |= SUPPORTED_Autoneg;
845 if (val & BMSR_100FULL)
846 features |= SUPPORTED_100baseT_Full;
847 if (val & BMSR_100HALF)
848 features |= SUPPORTED_100baseT_Half;
849 if (val & BMSR_10FULL)
850 features |= SUPPORTED_10baseT_Full;
851 if (val & BMSR_10HALF)
852 features |= SUPPORTED_10baseT_Half;
854 if (val & BMSR_ESTATEN) {
855 val = phy_read(phydev, MII_ESTATUS);
857 if (val < 0)
858 return val;
860 if (val & ESTATUS_1000_TFULL)
861 features |= SUPPORTED_1000baseT_Full;
862 if (val & ESTATUS_1000_THALF)
863 features |= SUPPORTED_1000baseT_Half;
866 phydev->supported = features;
867 phydev->advertising = features;
869 return 0;
871 int genphy_suspend(struct phy_device *phydev)
873 int value;
875 mutex_lock(&phydev->lock);
877 value = phy_read(phydev, MII_BMCR);
878 phy_write(phydev, MII_BMCR, (value | BMCR_PDOWN));
880 mutex_unlock(&phydev->lock);
882 return 0;
884 EXPORT_SYMBOL(genphy_suspend);
886 int genphy_resume(struct phy_device *phydev)
888 int value;
890 mutex_lock(&phydev->lock);
892 value = phy_read(phydev, MII_BMCR);
893 phy_write(phydev, MII_BMCR, (value & ~BMCR_PDOWN));
895 mutex_unlock(&phydev->lock);
897 return 0;
899 EXPORT_SYMBOL(genphy_resume);
902 * phy_probe - probe and init a PHY device
903 * @dev: device to probe and init
905 * Description: Take care of setting up the phy_device structure,
906 * set the state to READY (the driver's init function should
907 * set it to STARTING if needed).
909 static int phy_probe(struct device *dev)
911 struct phy_device *phydev;
912 struct phy_driver *phydrv;
913 struct device_driver *drv;
914 int err = 0;
916 phydev = to_phy_device(dev);
918 /* Make sure the driver is held.
919 * XXX -- Is this correct? */
920 drv = get_driver(phydev->dev.driver);
921 phydrv = to_phy_driver(drv);
922 phydev->drv = phydrv;
924 /* Disable the interrupt if the PHY doesn't support it */
925 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
926 phydev->irq = PHY_POLL;
928 mutex_lock(&phydev->lock);
930 /* Start out supporting everything. Eventually,
931 * a controller will attach, and may modify one
932 * or both of these values */
933 phydev->supported = phydrv->features;
934 phydev->advertising = phydrv->features;
936 /* Set the state to READY by default */
937 phydev->state = PHY_READY;
939 if (phydev->drv->probe)
940 err = phydev->drv->probe(phydev);
942 mutex_unlock(&phydev->lock);
944 return err;
948 static int phy_remove(struct device *dev)
950 struct phy_device *phydev;
952 phydev = to_phy_device(dev);
954 mutex_lock(&phydev->lock);
955 phydev->state = PHY_DOWN;
956 mutex_unlock(&phydev->lock);
958 if (phydev->drv->remove)
959 phydev->drv->remove(phydev);
961 put_driver(dev->driver);
962 phydev->drv = NULL;
964 return 0;
968 * phy_driver_register - register a phy_driver with the PHY layer
969 * @new_driver: new phy_driver to register
971 int phy_driver_register(struct phy_driver *new_driver)
973 int retval;
975 new_driver->driver.name = new_driver->name;
976 new_driver->driver.bus = &mdio_bus_type;
977 new_driver->driver.probe = phy_probe;
978 new_driver->driver.remove = phy_remove;
980 retval = driver_register(&new_driver->driver);
982 if (retval) {
983 printk(KERN_ERR "%s: Error %d in registering driver\n",
984 new_driver->name, retval);
986 return retval;
989 pr_debug("%s: Registered new driver\n", new_driver->name);
991 return 0;
993 EXPORT_SYMBOL(phy_driver_register);
995 void phy_driver_unregister(struct phy_driver *drv)
997 driver_unregister(&drv->driver);
999 EXPORT_SYMBOL(phy_driver_unregister);
1001 static struct phy_driver genphy_driver = {
1002 .phy_id = 0xffffffff,
1003 .phy_id_mask = 0xffffffff,
1004 .name = "Generic PHY",
1005 .config_init = genphy_config_init,
1006 .features = 0,
1007 .config_aneg = genphy_config_aneg,
1008 .read_status = genphy_read_status,
1009 .suspend = genphy_suspend,
1010 .resume = genphy_resume,
1011 .driver = {.owner= THIS_MODULE, },
1014 static int __init phy_init(void)
1016 int rc;
1018 rc = mdio_bus_init();
1019 if (rc)
1020 return rc;
1022 rc = phy_driver_register(&genphy_driver);
1023 if (rc)
1024 mdio_bus_exit();
1026 return rc;
1029 static void __exit phy_exit(void)
1031 phy_driver_unregister(&genphy_driver);
1032 mdio_bus_exit();
1035 subsys_initcall(phy_init);
1036 module_exit(phy_exit);