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 int genphy_config_init(struct phy_device
*phydev
);
44 static struct phy_driver genphy_driver
= {
46 .phy_id_mask
= 0xffffffff,
47 .name
= "Generic PHY",
48 .config_init
= genphy_config_init
,
50 .config_aneg
= genphy_config_aneg
,
51 .read_status
= genphy_read_status
,
52 .driver
= {.owner
= THIS_MODULE
, },
57 * description: Reads the ID registers of the PHY at addr on the
58 * bus, then allocates and returns the phy_device to
61 struct phy_device
* get_phy_device(struct mii_bus
*bus
, int addr
)
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
);
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
);
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
)
88 /* Otherwise, we allocate the device, and initialize the
90 dev
= kcalloc(1, sizeof(*dev
), GFP_KERNEL
);
93 return ERR_PTR(-ENOMEM
);
97 dev
->pause
= dev
->asym_pause
= 0;
100 dev
->autoneg
= AUTONEG_ENABLE
;
103 dev
->phy_id
= phy_id
;
106 dev
->state
= PHY_DOWN
;
108 spin_lock_init(&dev
->lock
);
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
121 void phy_prepare_link(struct phy_device
*phydev
,
122 void (*handler
)(struct net_device
*))
124 phydev
->adjust_link
= handler
;
127 #ifdef CONFIG_PHYCONTROL
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
);
148 phy_prepare_link(phydev
, handler
);
150 phy_start_machine(phydev
, NULL
);
153 phy_start_interrupts(phydev
);
157 EXPORT_SYMBOL(phy_connect
);
159 void phy_disconnect(struct phy_device
*phydev
)
162 phy_stop_interrupts(phydev
);
164 phy_stop_machine(phydev
);
166 phydev
->adjust_link
= NULL
;
170 EXPORT_SYMBOL(phy_disconnect
);
172 #endif /* CONFIG_PHYCONTROL */
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
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
;
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
);
201 phydev
= to_phy_device(d
);
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
) {
211 down_write(&d
->bus
->subsys
.rwsem
);
212 d
->driver
= &genphy_driver
.driver
;
214 err
= d
->driver
->probe(d
);
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",
226 return ERR_PTR(-EBUSY
);
229 phydev
->attached_dev
= dev
;
231 phydev
->dev_flags
= flags
;
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
262 int genphy_config_advert(struct phy_device
*phydev
)
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
);
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
);
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
);
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
);
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
);
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
);
355 /* Enable and Restart Autonegotiation */
356 int genphy_restart_aneg(struct phy_device
*phydev
)
360 ctl
= phy_read(phydev
, MII_BMCR
);
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
);
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
)
386 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
387 err
= genphy_config_advert(phydev
);
392 err
= genphy_restart_aneg(phydev
);
394 err
= genphy_setup_forced(phydev
);
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
)
411 status
= phy_read(phydev
, MII_BMSR
);
416 /* Read link and autonegotiation status */
417 status
= phy_read(phydev
, MII_BMSR
);
422 if ((status
& BMSR_LSTATUS
) == 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
)
444 /* Update the link, but return if there
446 err
= genphy_update_link(phydev
);
450 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
451 if (phydev
->supported
& (SUPPORTED_1000baseT_Half
452 | SUPPORTED_1000baseT_Full
)) {
453 lpagb
= phy_read(phydev
, MII_STAT1000
);
458 adv
= phy_read(phydev
, MII_CTRL1000
);
466 lpa
= phy_read(phydev
, MII_LPA
);
471 adv
= phy_read(phydev
, MII_ADVERTISE
);
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
;
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;
501 int bmcr
= phy_read(phydev
, MII_BMCR
);
505 if (bmcr
& BMCR_FULLDPLX
)
506 phydev
->duplex
= DUPLEX_FULL
;
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
;
515 phydev
->speed
= SPEED_10
;
517 phydev
->pause
= phydev
->asym_pause
= 0;
522 EXPORT_SYMBOL(genphy_read_status
);
524 static int genphy_config_init(struct phy_device
*phydev
)
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
|
535 /* Do we support autonegotiation? */
536 val
= phy_read(phydev
, MII_BMSR
);
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
);
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
;
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
;
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
);
616 if (phydev
->drv
->config_init
)
617 err
= phydev
->drv
->config_init(phydev
);
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
);
641 int phy_driver_register(struct phy_driver
*new_driver
)
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
);
654 printk(KERN_ERR
"%s: Error %d in registering driver\n",
655 new_driver
->name
, retval
);
660 pr_info("%s: Registered new driver\n", new_driver
->name
);
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)
676 extern int mdio_bus_init(void);
678 rc
= phy_driver_register(&genphy_driver
);
682 rc
= mdio_bus_init();
689 phy_driver_unregister(&genphy_driver
);
694 static void __exit
phy_exit(void)
696 phy_driver_unregister(&genphy_driver
);
699 module_init(phy_init
);
700 module_exit(phy_exit
);