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/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/spinlock.h>
30 #include <linux/module.h>
31 #include <linux/mii.h>
32 #include <linux/ethtool.h>
33 #include <linux/phy.h>
37 #include <asm/uaccess.h>
39 MODULE_DESCRIPTION("PHY library");
40 MODULE_AUTHOR("Andy Fleming");
41 MODULE_LICENSE("GPL");
43 static struct phy_driver genphy_driver
;
44 extern int mdio_bus_init(void);
45 extern void mdio_bus_exit(void);
47 struct phy_device
* phy_device_create(struct mii_bus
*bus
, int addr
, int phy_id
)
49 struct phy_device
*dev
;
50 /* We allocate the device, and initialize the
52 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
55 return (struct phy_device
*) PTR_ERR((void*)-ENOMEM
);
59 dev
->pause
= dev
->asym_pause
= 0;
61 dev
->interface
= PHY_INTERFACE_MODE_GMII
;
63 dev
->autoneg
= AUTONEG_ENABLE
;
69 dev
->state
= PHY_DOWN
;
71 spin_lock_init(&dev
->lock
);
75 EXPORT_SYMBOL(phy_device_create
);
78 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
79 * @bus: the target MII bus
80 * @addr: PHY address on the MII bus
82 * Description: Reads the ID registers of the PHY at @addr on the
83 * @bus, then allocates and returns the phy_device to represent it.
85 struct phy_device
* get_phy_device(struct mii_bus
*bus
, int addr
)
89 struct phy_device
*dev
= NULL
;
91 /* Grab the bits from PHYIR1, and put them
92 * in the upper half */
93 phy_reg
= bus
->read(bus
, addr
, MII_PHYSID1
);
96 return ERR_PTR(phy_reg
);
98 phy_id
= (phy_reg
& 0xffff) << 16;
100 /* Grab the bits from PHYIR2, and put them in the lower half */
101 phy_reg
= bus
->read(bus
, addr
, MII_PHYSID2
);
104 return ERR_PTR(phy_reg
);
106 phy_id
|= (phy_reg
& 0xffff);
108 /* If the phy_id is all Fs, there is no device there */
109 if (0xffffffff == phy_id
)
112 dev
= phy_device_create(bus
, addr
, phy_id
);
118 * phy_prepare_link - prepares the PHY layer to monitor link status
119 * @phydev: target phy_device struct
120 * @handler: callback function for link status change notifications
122 * Description: Tells the PHY infrastructure to handle the
123 * gory details on monitoring link status (whether through
124 * polling or an interrupt), and to call back to the
125 * connected device driver when the link status changes.
126 * If you want to monitor your own link state, don't call
129 void phy_prepare_link(struct phy_device
*phydev
,
130 void (*handler
)(struct net_device
*))
132 phydev
->adjust_link
= handler
;
136 * phy_connect - connect an ethernet device to a PHY device
137 * @dev: the network device to connect
138 * @phy_id: the PHY device to connect
139 * @handler: callback function for state change notifications
140 * @flags: PHY device's dev_flags
141 * @interface: PHY device's interface
143 * Description: Convenience function for connecting ethernet
144 * devices to PHY devices. The default behavior is for
145 * the PHY infrastructure to handle everything, and only notify
146 * the connected driver when the link status changes. If you
147 * don't want, or can't use the provided functionality, you may
148 * choose to call only the subset of functions which provide
149 * the desired functionality.
151 struct phy_device
* phy_connect(struct net_device
*dev
, const char *phy_id
,
152 void (*handler
)(struct net_device
*), u32 flags
,
153 phy_interface_t interface
)
155 struct phy_device
*phydev
;
157 phydev
= phy_attach(dev
, phy_id
, flags
, interface
);
162 phy_prepare_link(phydev
, handler
);
164 phy_start_machine(phydev
, NULL
);
167 phy_start_interrupts(phydev
);
171 EXPORT_SYMBOL(phy_connect
);
174 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device
175 * @phydev: target phy_device struct
177 void phy_disconnect(struct phy_device
*phydev
)
180 phy_stop_interrupts(phydev
);
182 phy_stop_machine(phydev
);
184 phydev
->adjust_link
= NULL
;
188 EXPORT_SYMBOL(phy_disconnect
);
190 static int phy_compare_id(struct device
*dev
, void *data
)
192 return strcmp((char *)data
, dev
->bus_id
) ? 0 : 1;
196 * phy_attach - attach a network device to a particular PHY device
197 * @dev: network device to attach
198 * @phy_id: PHY device to attach
199 * @flags: PHY device's dev_flags
200 * @interface: PHY device's interface
202 * Description: Called by drivers to attach to a particular PHY
203 * device. The phy_device is found, and properly hooked up
204 * to the phy_driver. If no driver is attached, then the
205 * genphy_driver is used. The phy_device is given a ptr to
206 * the attaching device, and given a callback for link status
207 * change. The phy_device is returned to the attaching driver.
209 struct phy_device
*phy_attach(struct net_device
*dev
,
210 const char *phy_id
, u32 flags
, phy_interface_t interface
)
212 struct bus_type
*bus
= &mdio_bus_type
;
213 struct phy_device
*phydev
;
216 /* Search the list of PHY devices on the mdio bus for the
217 * PHY with the requested name */
218 d
= bus_find_device(bus
, NULL
, (void *)phy_id
, phy_compare_id
);
221 phydev
= to_phy_device(d
);
223 printk(KERN_ERR
"%s not found\n", phy_id
);
224 return ERR_PTR(-ENODEV
);
227 /* Assume that if there is no driver, that it doesn't
228 * exist, and we should use the genphy driver. */
229 if (NULL
== d
->driver
) {
231 d
->driver
= &genphy_driver
.driver
;
233 err
= d
->driver
->probe(d
);
235 err
= device_bind_driver(d
);
241 if (phydev
->attached_dev
) {
242 printk(KERN_ERR
"%s: %s already attached\n",
244 return ERR_PTR(-EBUSY
);
247 phydev
->attached_dev
= dev
;
249 phydev
->dev_flags
= flags
;
251 phydev
->interface
= interface
;
253 /* Do initial configuration here, now that
254 * we have certain key parameters
255 * (dev_flags and interface) */
256 if (phydev
->drv
->config_init
) {
259 err
= phydev
->drv
->config_init(phydev
);
267 EXPORT_SYMBOL(phy_attach
);
270 * phy_detach - detach a PHY device from its network device
271 * @phydev: target phy_device struct
273 void phy_detach(struct phy_device
*phydev
)
275 phydev
->attached_dev
= NULL
;
277 /* If the device had no specific driver before (i.e. - it
278 * was using the generic driver), we unbind the device
279 * from the generic driver so that there's a chance a
280 * real driver could be loaded */
281 if (phydev
->dev
.driver
== &genphy_driver
.driver
)
282 device_release_driver(&phydev
->dev
);
284 EXPORT_SYMBOL(phy_detach
);
287 /* Generic PHY support and helper functions */
290 * genphy_config_advert - sanitize and advertise auto-negotation parameters
291 * @phydev: target phy_device struct
293 * Description: Writes MII_ADVERTISE with the appropriate values,
294 * after sanitizing the values to make sure we only advertise
297 int genphy_config_advert(struct phy_device
*phydev
)
303 /* Only allow advertising what
304 * this PHY supports */
305 phydev
->advertising
&= phydev
->supported
;
306 advertise
= phydev
->advertising
;
308 /* Setup standard advertisement */
309 adv
= phy_read(phydev
, MII_ADVERTISE
);
314 adv
&= ~(ADVERTISE_ALL
| ADVERTISE_100BASE4
| ADVERTISE_PAUSE_CAP
|
315 ADVERTISE_PAUSE_ASYM
);
316 if (advertise
& ADVERTISED_10baseT_Half
)
317 adv
|= ADVERTISE_10HALF
;
318 if (advertise
& ADVERTISED_10baseT_Full
)
319 adv
|= ADVERTISE_10FULL
;
320 if (advertise
& ADVERTISED_100baseT_Half
)
321 adv
|= ADVERTISE_100HALF
;
322 if (advertise
& ADVERTISED_100baseT_Full
)
323 adv
|= ADVERTISE_100FULL
;
324 if (advertise
& ADVERTISED_Pause
)
325 adv
|= ADVERTISE_PAUSE_CAP
;
326 if (advertise
& ADVERTISED_Asym_Pause
)
327 adv
|= ADVERTISE_PAUSE_ASYM
;
329 err
= phy_write(phydev
, MII_ADVERTISE
, adv
);
334 /* Configure gigabit if it's supported */
335 if (phydev
->supported
& (SUPPORTED_1000baseT_Half
|
336 SUPPORTED_1000baseT_Full
)) {
337 adv
= phy_read(phydev
, MII_CTRL1000
);
342 adv
&= ~(ADVERTISE_1000FULL
| ADVERTISE_1000HALF
);
343 if (advertise
& SUPPORTED_1000baseT_Half
)
344 adv
|= ADVERTISE_1000HALF
;
345 if (advertise
& SUPPORTED_1000baseT_Full
)
346 adv
|= ADVERTISE_1000FULL
;
347 err
= phy_write(phydev
, MII_CTRL1000
, adv
);
355 EXPORT_SYMBOL(genphy_config_advert
);
358 * genphy_setup_forced - configures/forces speed/duplex from @phydev
359 * @phydev: target phy_device struct
361 * Description: Configures MII_BMCR to force speed/duplex
362 * to the values in phydev. Assumes that the values are valid.
363 * Please see phy_sanitize_settings().
365 int genphy_setup_forced(struct phy_device
*phydev
)
367 int ctl
= BMCR_RESET
;
369 phydev
->pause
= phydev
->asym_pause
= 0;
371 if (SPEED_1000
== phydev
->speed
)
372 ctl
|= BMCR_SPEED1000
;
373 else if (SPEED_100
== phydev
->speed
)
374 ctl
|= BMCR_SPEED100
;
376 if (DUPLEX_FULL
== phydev
->duplex
)
377 ctl
|= BMCR_FULLDPLX
;
379 ctl
= phy_write(phydev
, MII_BMCR
, ctl
);
384 /* We just reset the device, so we'd better configure any
385 * settings the PHY requires to operate */
386 if (phydev
->drv
->config_init
)
387 ctl
= phydev
->drv
->config_init(phydev
);
394 * genphy_restart_aneg - Enable and Restart Autonegotiation
395 * @phydev: target phy_device struct
397 int genphy_restart_aneg(struct phy_device
*phydev
)
401 ctl
= phy_read(phydev
, MII_BMCR
);
406 ctl
|= (BMCR_ANENABLE
| BMCR_ANRESTART
);
408 /* Don't isolate the PHY if we're negotiating */
409 ctl
&= ~(BMCR_ISOLATE
);
411 ctl
= phy_write(phydev
, MII_BMCR
, ctl
);
418 * genphy_config_aneg - restart auto-negotiation or write BMCR
419 * @phydev: target phy_device struct
421 * Description: If auto-negotiation is enabled, we configure the
422 * advertising, and then restart auto-negotiation. If it is not
423 * enabled, then we write the BMCR.
425 int genphy_config_aneg(struct phy_device
*phydev
)
429 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
430 err
= genphy_config_advert(phydev
);
435 err
= genphy_restart_aneg(phydev
);
437 err
= genphy_setup_forced(phydev
);
441 EXPORT_SYMBOL(genphy_config_aneg
);
444 * genphy_update_link - update link status in @phydev
445 * @phydev: target phy_device struct
447 * Description: Update the value in phydev->link to reflect the
448 * current link value. In order to do this, we need to read
449 * the status register twice, keeping the second value.
451 int genphy_update_link(struct phy_device
*phydev
)
456 status
= phy_read(phydev
, MII_BMSR
);
461 /* Read link and autonegotiation status */
462 status
= phy_read(phydev
, MII_BMSR
);
467 if ((status
& BMSR_LSTATUS
) == 0)
474 EXPORT_SYMBOL(genphy_update_link
);
477 * genphy_read_status - check the link status and update current link state
478 * @phydev: target phy_device struct
480 * Description: Check the link, then figure out the current state
481 * by comparing what we advertise with what the link partner
482 * advertises. Start by checking the gigabit possibilities,
483 * then move on to 10/100.
485 int genphy_read_status(struct phy_device
*phydev
)
492 /* Update the link, but return if there
494 err
= genphy_update_link(phydev
);
498 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
499 if (phydev
->supported
& (SUPPORTED_1000baseT_Half
500 | SUPPORTED_1000baseT_Full
)) {
501 lpagb
= phy_read(phydev
, MII_STAT1000
);
506 adv
= phy_read(phydev
, MII_CTRL1000
);
514 lpa
= phy_read(phydev
, MII_LPA
);
519 adv
= phy_read(phydev
, MII_ADVERTISE
);
526 phydev
->speed
= SPEED_10
;
527 phydev
->duplex
= DUPLEX_HALF
;
528 phydev
->pause
= phydev
->asym_pause
= 0;
530 if (lpagb
& (LPA_1000FULL
| LPA_1000HALF
)) {
531 phydev
->speed
= SPEED_1000
;
533 if (lpagb
& LPA_1000FULL
)
534 phydev
->duplex
= DUPLEX_FULL
;
535 } else if (lpa
& (LPA_100FULL
| LPA_100HALF
)) {
536 phydev
->speed
= SPEED_100
;
538 if (lpa
& LPA_100FULL
)
539 phydev
->duplex
= DUPLEX_FULL
;
541 if (lpa
& LPA_10FULL
)
542 phydev
->duplex
= DUPLEX_FULL
;
544 if (phydev
->duplex
== DUPLEX_FULL
){
545 phydev
->pause
= lpa
& LPA_PAUSE_CAP
? 1 : 0;
546 phydev
->asym_pause
= lpa
& LPA_PAUSE_ASYM
? 1 : 0;
549 int bmcr
= phy_read(phydev
, MII_BMCR
);
553 if (bmcr
& BMCR_FULLDPLX
)
554 phydev
->duplex
= DUPLEX_FULL
;
556 phydev
->duplex
= DUPLEX_HALF
;
558 if (bmcr
& BMCR_SPEED1000
)
559 phydev
->speed
= SPEED_1000
;
560 else if (bmcr
& BMCR_SPEED100
)
561 phydev
->speed
= SPEED_100
;
563 phydev
->speed
= SPEED_10
;
565 phydev
->pause
= phydev
->asym_pause
= 0;
570 EXPORT_SYMBOL(genphy_read_status
);
572 static int genphy_config_init(struct phy_device
*phydev
)
577 /* For now, I'll claim that the generic driver supports
578 * all possible port types */
579 features
= (SUPPORTED_TP
| SUPPORTED_MII
580 | SUPPORTED_AUI
| SUPPORTED_FIBRE
|
583 /* Do we support autonegotiation? */
584 val
= phy_read(phydev
, MII_BMSR
);
589 if (val
& BMSR_ANEGCAPABLE
)
590 features
|= SUPPORTED_Autoneg
;
592 if (val
& BMSR_100FULL
)
593 features
|= SUPPORTED_100baseT_Full
;
594 if (val
& BMSR_100HALF
)
595 features
|= SUPPORTED_100baseT_Half
;
596 if (val
& BMSR_10FULL
)
597 features
|= SUPPORTED_10baseT_Full
;
598 if (val
& BMSR_10HALF
)
599 features
|= SUPPORTED_10baseT_Half
;
601 if (val
& BMSR_ESTATEN
) {
602 val
= phy_read(phydev
, MII_ESTATUS
);
607 if (val
& ESTATUS_1000_TFULL
)
608 features
|= SUPPORTED_1000baseT_Full
;
609 if (val
& ESTATUS_1000_THALF
)
610 features
|= SUPPORTED_1000baseT_Half
;
613 phydev
->supported
= features
;
614 phydev
->advertising
= features
;
621 * phy_probe - probe and init a PHY device
622 * @dev: device to probe and init
624 * Description: Take care of setting up the phy_device structure,
625 * set the state to READY (the driver's init function should
626 * set it to STARTING if needed).
628 static int phy_probe(struct device
*dev
)
630 struct phy_device
*phydev
;
631 struct phy_driver
*phydrv
;
632 struct device_driver
*drv
;
635 phydev
= to_phy_device(dev
);
637 /* Make sure the driver is held.
638 * XXX -- Is this correct? */
639 drv
= get_driver(phydev
->dev
.driver
);
640 phydrv
= to_phy_driver(drv
);
641 phydev
->drv
= phydrv
;
643 /* Disable the interrupt if the PHY doesn't support it */
644 if (!(phydrv
->flags
& PHY_HAS_INTERRUPT
))
645 phydev
->irq
= PHY_POLL
;
647 spin_lock(&phydev
->lock
);
649 /* Start out supporting everything. Eventually,
650 * a controller will attach, and may modify one
651 * or both of these values */
652 phydev
->supported
= phydrv
->features
;
653 phydev
->advertising
= phydrv
->features
;
655 /* Set the state to READY by default */
656 phydev
->state
= PHY_READY
;
658 if (phydev
->drv
->probe
)
659 err
= phydev
->drv
->probe(phydev
);
661 spin_unlock(&phydev
->lock
);
667 static int phy_remove(struct device
*dev
)
669 struct phy_device
*phydev
;
671 phydev
= to_phy_device(dev
);
673 spin_lock(&phydev
->lock
);
674 phydev
->state
= PHY_DOWN
;
675 spin_unlock(&phydev
->lock
);
677 if (phydev
->drv
->remove
)
678 phydev
->drv
->remove(phydev
);
680 put_driver(dev
->driver
);
687 * phy_driver_register - register a phy_driver with the PHY layer
688 * @new_driver: new phy_driver to register
690 int phy_driver_register(struct phy_driver
*new_driver
)
694 memset(&new_driver
->driver
, 0, sizeof(new_driver
->driver
));
695 new_driver
->driver
.name
= new_driver
->name
;
696 new_driver
->driver
.bus
= &mdio_bus_type
;
697 new_driver
->driver
.probe
= phy_probe
;
698 new_driver
->driver
.remove
= phy_remove
;
700 retval
= driver_register(&new_driver
->driver
);
703 printk(KERN_ERR
"%s: Error %d in registering driver\n",
704 new_driver
->name
, retval
);
709 pr_info("%s: Registered new driver\n", new_driver
->name
);
713 EXPORT_SYMBOL(phy_driver_register
);
715 void phy_driver_unregister(struct phy_driver
*drv
)
717 driver_unregister(&drv
->driver
);
719 EXPORT_SYMBOL(phy_driver_unregister
);
721 static struct phy_driver genphy_driver
= {
722 .phy_id
= 0xffffffff,
723 .phy_id_mask
= 0xffffffff,
724 .name
= "Generic PHY",
725 .config_init
= genphy_config_init
,
727 .config_aneg
= genphy_config_aneg
,
728 .read_status
= genphy_read_status
,
729 .driver
= {.owner
= THIS_MODULE
, },
732 static int __init
phy_init(void)
736 rc
= mdio_bus_init();
740 rc
= phy_driver_register(&genphy_driver
);
747 static void __exit
phy_exit(void)
749 phy_driver_unregister(&genphy_driver
);
753 subsys_initcall(phy_init
);
754 module_exit(phy_exit
);