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/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>
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>
40 #include <asm/uaccess.h>
42 static struct phy_driver genphy_driver
;
43 extern int mdio_bus_init(void);
44 extern void mdio_bus_exit(void);
48 * description: Reads the ID registers of the PHY at addr on the
49 * bus, then allocates and returns the phy_device to
52 struct phy_device
* get_phy_device(struct mii_bus
*bus
, int addr
)
56 struct phy_device
*dev
= NULL
;
58 /* Grab the bits from PHYIR1, and put them
59 * in the upper half */
60 phy_reg
= bus
->read(bus
, addr
, MII_PHYSID1
);
63 return ERR_PTR(phy_reg
);
65 phy_id
= (phy_reg
& 0xffff) << 16;
67 /* Grab the bits from PHYIR2, and put them in the lower half */
68 phy_reg
= bus
->read(bus
, addr
, MII_PHYSID2
);
71 return ERR_PTR(phy_reg
);
73 phy_id
|= (phy_reg
& 0xffff);
75 /* If the phy_id is all Fs, there is no device there */
76 if (0xffffffff == phy_id
)
79 /* Otherwise, we allocate the device, and initialize the
81 dev
= kcalloc(1, sizeof(*dev
), GFP_KERNEL
);
84 return ERR_PTR(-ENOMEM
);
88 dev
->pause
= dev
->asym_pause
= 0;
91 dev
->autoneg
= AUTONEG_ENABLE
;
97 dev
->state
= PHY_DOWN
;
99 spin_lock_init(&dev
->lock
);
106 * description: Tells the PHY infrastructure to handle the
107 * gory details on monitoring link status (whether through
108 * polling or an interrupt), and to call back to the
109 * connected device driver when the link status changes.
110 * If you want to monitor your own link state, don't call
112 void phy_prepare_link(struct phy_device
*phydev
,
113 void (*handler
)(struct net_device
*))
115 phydev
->adjust_link
= handler
;
120 * description: Convenience function for connecting ethernet
121 * devices to PHY devices. The default behavior is for
122 * the PHY infrastructure to handle everything, and only notify
123 * the connected driver when the link status changes. If you
124 * don't want, or can't use the provided functionality, you may
125 * choose to call only the subset of functions which provide
126 * the desired functionality.
128 struct phy_device
* phy_connect(struct net_device
*dev
, const char *phy_id
,
129 void (*handler
)(struct net_device
*), u32 flags
)
131 struct phy_device
*phydev
;
133 phydev
= phy_attach(dev
, phy_id
, flags
);
138 phy_prepare_link(phydev
, handler
);
140 phy_start_machine(phydev
, NULL
);
143 phy_start_interrupts(phydev
);
147 EXPORT_SYMBOL(phy_connect
);
149 void phy_disconnect(struct phy_device
*phydev
)
152 phy_stop_interrupts(phydev
);
154 phy_stop_machine(phydev
);
156 phydev
->adjust_link
= NULL
;
160 EXPORT_SYMBOL(phy_disconnect
);
164 * description: Called by drivers to attach to a particular PHY
165 * device. The phy_device is found, and properly hooked up
166 * to the phy_driver. If no driver is attached, then the
167 * genphy_driver is used. The phy_device is given a ptr to
168 * the attaching device, and given a callback for link status
169 * change. The phy_device is returned to the attaching
172 static int phy_compare_id(struct device
*dev
, void *data
)
174 return strcmp((char *)data
, dev
->bus_id
) ? 0 : 1;
177 struct phy_device
*phy_attach(struct net_device
*dev
,
178 const char *phy_id
, u32 flags
)
180 struct bus_type
*bus
= &mdio_bus_type
;
181 struct phy_device
*phydev
;
184 /* Search the list of PHY devices on the mdio bus for the
185 * PHY with the requested name */
186 d
= bus_find_device(bus
, NULL
, (void *)phy_id
, phy_compare_id
);
189 phydev
= to_phy_device(d
);
191 printk(KERN_ERR
"%s not found\n", phy_id
);
192 return ERR_PTR(-ENODEV
);
195 /* Assume that if there is no driver, that it doesn't
196 * exist, and we should use the genphy driver. */
197 if (NULL
== d
->driver
) {
199 down_write(&d
->bus
->subsys
.rwsem
);
200 d
->driver
= &genphy_driver
.driver
;
202 err
= d
->driver
->probe(d
);
207 device_bind_driver(d
);
208 up_write(&d
->bus
->subsys
.rwsem
);
211 if (phydev
->attached_dev
) {
212 printk(KERN_ERR
"%s: %s already attached\n",
214 return ERR_PTR(-EBUSY
);
217 phydev
->attached_dev
= dev
;
219 phydev
->dev_flags
= flags
;
223 EXPORT_SYMBOL(phy_attach
);
225 void phy_detach(struct phy_device
*phydev
)
227 phydev
->attached_dev
= NULL
;
229 /* If the device had no specific driver before (i.e. - it
230 * was using the generic driver), we unbind the device
231 * from the generic driver so that there's a chance a
232 * real driver could be loaded */
233 if (phydev
->dev
.driver
== &genphy_driver
.driver
) {
234 down_write(&phydev
->dev
.bus
->subsys
.rwsem
);
235 device_release_driver(&phydev
->dev
);
236 up_write(&phydev
->dev
.bus
->subsys
.rwsem
);
239 EXPORT_SYMBOL(phy_detach
);
242 /* Generic PHY support and helper functions */
244 /* genphy_config_advert
246 * description: Writes MII_ADVERTISE with the appropriate values,
247 * after sanitizing the values to make sure we only advertise
250 int genphy_config_advert(struct phy_device
*phydev
)
256 /* Only allow advertising what
257 * this PHY supports */
258 phydev
->advertising
&= phydev
->supported
;
259 advertise
= phydev
->advertising
;
261 /* Setup standard advertisement */
262 adv
= phy_read(phydev
, MII_ADVERTISE
);
267 adv
&= ~(ADVERTISE_ALL
| ADVERTISE_100BASE4
| ADVERTISE_PAUSE_CAP
|
268 ADVERTISE_PAUSE_ASYM
);
269 if (advertise
& ADVERTISED_10baseT_Half
)
270 adv
|= ADVERTISE_10HALF
;
271 if (advertise
& ADVERTISED_10baseT_Full
)
272 adv
|= ADVERTISE_10FULL
;
273 if (advertise
& ADVERTISED_100baseT_Half
)
274 adv
|= ADVERTISE_100HALF
;
275 if (advertise
& ADVERTISED_100baseT_Full
)
276 adv
|= ADVERTISE_100FULL
;
277 if (advertise
& ADVERTISED_Pause
)
278 adv
|= ADVERTISE_PAUSE_CAP
;
279 if (advertise
& ADVERTISED_Asym_Pause
)
280 adv
|= ADVERTISE_PAUSE_ASYM
;
282 err
= phy_write(phydev
, MII_ADVERTISE
, adv
);
287 /* Configure gigabit if it's supported */
288 if (phydev
->supported
& (SUPPORTED_1000baseT_Half
|
289 SUPPORTED_1000baseT_Full
)) {
290 adv
= phy_read(phydev
, MII_CTRL1000
);
295 adv
&= ~(ADVERTISE_1000FULL
| ADVERTISE_1000HALF
);
296 if (advertise
& SUPPORTED_1000baseT_Half
)
297 adv
|= ADVERTISE_1000HALF
;
298 if (advertise
& SUPPORTED_1000baseT_Full
)
299 adv
|= ADVERTISE_1000FULL
;
300 err
= phy_write(phydev
, MII_CTRL1000
, adv
);
308 EXPORT_SYMBOL(genphy_config_advert
);
310 /* genphy_setup_forced
312 * description: Configures MII_BMCR to force speed/duplex
313 * to the values in phydev. Assumes that the values are valid.
314 * Please see phy_sanitize_settings() */
315 int genphy_setup_forced(struct phy_device
*phydev
)
317 int ctl
= BMCR_RESET
;
319 phydev
->pause
= phydev
->asym_pause
= 0;
321 if (SPEED_1000
== phydev
->speed
)
322 ctl
|= BMCR_SPEED1000
;
323 else if (SPEED_100
== phydev
->speed
)
324 ctl
|= BMCR_SPEED100
;
326 if (DUPLEX_FULL
== phydev
->duplex
)
327 ctl
|= BMCR_FULLDPLX
;
329 ctl
= phy_write(phydev
, MII_BMCR
, ctl
);
334 /* We just reset the device, so we'd better configure any
335 * settings the PHY requires to operate */
336 if (phydev
->drv
->config_init
)
337 ctl
= phydev
->drv
->config_init(phydev
);
343 /* Enable and Restart Autonegotiation */
344 int genphy_restart_aneg(struct phy_device
*phydev
)
348 ctl
= phy_read(phydev
, MII_BMCR
);
353 ctl
|= (BMCR_ANENABLE
| BMCR_ANRESTART
);
355 /* Don't isolate the PHY if we're negotiating */
356 ctl
&= ~(BMCR_ISOLATE
);
358 ctl
= phy_write(phydev
, MII_BMCR
, ctl
);
364 /* genphy_config_aneg
366 * description: If auto-negotiation is enabled, we configure the
367 * advertising, and then restart auto-negotiation. If it is not
368 * enabled, then we write the BMCR
370 int genphy_config_aneg(struct phy_device
*phydev
)
374 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
375 err
= genphy_config_advert(phydev
);
380 err
= genphy_restart_aneg(phydev
);
382 err
= genphy_setup_forced(phydev
);
386 EXPORT_SYMBOL(genphy_config_aneg
);
388 /* genphy_update_link
390 * description: Update the value in phydev->link to reflect the
391 * current link value. In order to do this, we need to read
392 * the status register twice, keeping the second value
394 int genphy_update_link(struct phy_device
*phydev
)
399 status
= phy_read(phydev
, MII_BMSR
);
404 /* Read link and autonegotiation status */
405 status
= phy_read(phydev
, MII_BMSR
);
410 if ((status
& BMSR_LSTATUS
) == 0)
418 /* genphy_read_status
420 * description: Check the link, then figure out the current state
421 * by comparing what we advertise with what the link partner
422 * advertises. Start by checking the gigabit possibilities,
423 * then move on to 10/100.
425 int genphy_read_status(struct phy_device
*phydev
)
432 /* Update the link, but return if there
434 err
= genphy_update_link(phydev
);
438 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
439 if (phydev
->supported
& (SUPPORTED_1000baseT_Half
440 | SUPPORTED_1000baseT_Full
)) {
441 lpagb
= phy_read(phydev
, MII_STAT1000
);
446 adv
= phy_read(phydev
, MII_CTRL1000
);
454 lpa
= phy_read(phydev
, MII_LPA
);
459 adv
= phy_read(phydev
, MII_ADVERTISE
);
466 phydev
->speed
= SPEED_10
;
467 phydev
->duplex
= DUPLEX_HALF
;
468 phydev
->pause
= phydev
->asym_pause
= 0;
470 if (lpagb
& (LPA_1000FULL
| LPA_1000HALF
)) {
471 phydev
->speed
= SPEED_1000
;
473 if (lpagb
& LPA_1000FULL
)
474 phydev
->duplex
= DUPLEX_FULL
;
475 } else if (lpa
& (LPA_100FULL
| LPA_100HALF
)) {
476 phydev
->speed
= SPEED_100
;
478 if (lpa
& LPA_100FULL
)
479 phydev
->duplex
= DUPLEX_FULL
;
481 if (lpa
& LPA_10FULL
)
482 phydev
->duplex
= DUPLEX_FULL
;
484 if (phydev
->duplex
== DUPLEX_FULL
){
485 phydev
->pause
= lpa
& LPA_PAUSE_CAP
? 1 : 0;
486 phydev
->asym_pause
= lpa
& LPA_PAUSE_ASYM
? 1 : 0;
489 int bmcr
= phy_read(phydev
, MII_BMCR
);
493 if (bmcr
& BMCR_FULLDPLX
)
494 phydev
->duplex
= DUPLEX_FULL
;
496 phydev
->duplex
= DUPLEX_HALF
;
498 if (bmcr
& BMCR_SPEED1000
)
499 phydev
->speed
= SPEED_1000
;
500 else if (bmcr
& BMCR_SPEED100
)
501 phydev
->speed
= SPEED_100
;
503 phydev
->speed
= SPEED_10
;
505 phydev
->pause
= phydev
->asym_pause
= 0;
510 EXPORT_SYMBOL(genphy_read_status
);
512 static int genphy_config_init(struct phy_device
*phydev
)
517 /* For now, I'll claim that the generic driver supports
518 * all possible port types */
519 features
= (SUPPORTED_TP
| SUPPORTED_MII
520 | SUPPORTED_AUI
| SUPPORTED_FIBRE
|
523 /* Do we support autonegotiation? */
524 val
= phy_read(phydev
, MII_BMSR
);
529 if (val
& BMSR_ANEGCAPABLE
)
530 features
|= SUPPORTED_Autoneg
;
532 if (val
& BMSR_100FULL
)
533 features
|= SUPPORTED_100baseT_Full
;
534 if (val
& BMSR_100HALF
)
535 features
|= SUPPORTED_100baseT_Half
;
536 if (val
& BMSR_10FULL
)
537 features
|= SUPPORTED_10baseT_Full
;
538 if (val
& BMSR_10HALF
)
539 features
|= SUPPORTED_10baseT_Half
;
541 if (val
& BMSR_ESTATEN
) {
542 val
= phy_read(phydev
, MII_ESTATUS
);
547 if (val
& ESTATUS_1000_TFULL
)
548 features
|= SUPPORTED_1000baseT_Full
;
549 if (val
& ESTATUS_1000_THALF
)
550 features
|= SUPPORTED_1000baseT_Half
;
553 phydev
->supported
= features
;
554 phydev
->advertising
= features
;
562 * description: Take care of setting up the phy_device structure,
563 * set the state to READY (the driver's init function should
564 * set it to STARTING if needed).
566 static int phy_probe(struct device
*dev
)
568 struct phy_device
*phydev
;
569 struct phy_driver
*phydrv
;
570 struct device_driver
*drv
;
573 phydev
= to_phy_device(dev
);
575 /* Make sure the driver is held.
576 * XXX -- Is this correct? */
577 drv
= get_driver(phydev
->dev
.driver
);
578 phydrv
= to_phy_driver(drv
);
579 phydev
->drv
= phydrv
;
581 /* Disable the interrupt if the PHY doesn't support it */
582 if (!(phydrv
->flags
& PHY_HAS_INTERRUPT
))
583 phydev
->irq
= PHY_POLL
;
585 spin_lock(&phydev
->lock
);
587 /* Start out supporting everything. Eventually,
588 * a controller will attach, and may modify one
589 * or both of these values */
590 phydev
->supported
= phydrv
->features
;
591 phydev
->advertising
= phydrv
->features
;
593 /* Set the state to READY by default */
594 phydev
->state
= PHY_READY
;
596 if (phydev
->drv
->probe
)
597 err
= phydev
->drv
->probe(phydev
);
599 spin_unlock(&phydev
->lock
);
604 if (phydev
->drv
->config_init
)
605 err
= phydev
->drv
->config_init(phydev
);
610 static int phy_remove(struct device
*dev
)
612 struct phy_device
*phydev
;
614 phydev
= to_phy_device(dev
);
616 spin_lock(&phydev
->lock
);
617 phydev
->state
= PHY_DOWN
;
618 spin_unlock(&phydev
->lock
);
620 if (phydev
->drv
->remove
)
621 phydev
->drv
->remove(phydev
);
623 put_driver(dev
->driver
);
629 int phy_driver_register(struct phy_driver
*new_driver
)
633 memset(&new_driver
->driver
, 0, sizeof(new_driver
->driver
));
634 new_driver
->driver
.name
= new_driver
->name
;
635 new_driver
->driver
.bus
= &mdio_bus_type
;
636 new_driver
->driver
.probe
= phy_probe
;
637 new_driver
->driver
.remove
= phy_remove
;
639 retval
= driver_register(&new_driver
->driver
);
642 printk(KERN_ERR
"%s: Error %d in registering driver\n",
643 new_driver
->name
, retval
);
648 pr_info("%s: Registered new driver\n", new_driver
->name
);
652 EXPORT_SYMBOL(phy_driver_register
);
654 void phy_driver_unregister(struct phy_driver
*drv
)
656 driver_unregister(&drv
->driver
);
658 EXPORT_SYMBOL(phy_driver_unregister
);
660 static struct phy_driver genphy_driver
= {
661 .phy_id
= 0xffffffff,
662 .phy_id_mask
= 0xffffffff,
663 .name
= "Generic PHY",
664 .config_init
= genphy_config_init
,
666 .config_aneg
= genphy_config_aneg
,
667 .read_status
= genphy_read_status
,
668 .driver
= {.owner
= THIS_MODULE
, },
671 static int __init
phy_init(void)
675 rc
= mdio_bus_init();
679 rc
= phy_driver_register(&genphy_driver
);
686 static void __exit
phy_exit(void)
688 phy_driver_unregister(&genphy_driver
);
692 subsys_initcall(phy_init
);
693 module_exit(phy_exit
);