aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/phy/phy_device.c
diff options
context:
space:
mode:
authorAndy Fleming <afleming@freescale.com>2005-07-30 19:31:23 -0400
committerJeff Garzik <jgarzik@pobox.com>2005-07-30 19:31:23 -0400
commit00db8189d984d6c51226dafbbe4a667ce9b7d5da (patch)
treef19468548c938523e3519670f8554e7a1b9c0c31 /drivers/net/phy/phy_device.c
parentb0825488a642cadcf39709961dde61440cb0731c (diff)
This patch adds a PHY Abstraction Layer to the Linux Kernel, enabling
ethernet drivers to remain as ignorant as is reasonable of the connected PHY's design and operation details. Signed-off-by: Andy Fleming <afleming@freescale.com> Signed-off-by: Jeff Garzik <jgarzik@pobox.com>
Diffstat (limited to 'drivers/net/phy/phy_device.c')
-rw-r--r--drivers/net/phy/phy_device.c682
1 files changed, 682 insertions, 0 deletions
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
new file mode 100644
index 000000000000..f0595af4c837
--- /dev/null
+++ b/drivers/net/phy/phy_device.c
@@ -0,0 +1,682 @@
1/*
2 * drivers/net/phy/phy_device.c
3 *
4 * Framework for finding and configuring PHYs.
5 * Also contains generic PHY driver
6 *
7 * Author: Andy Fleming
8 *
9 * Copyright (c) 2004 Freescale Semiconductor, Inc.
10 *
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.
15 *
16 */
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>
31#include <linux/mm.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>
37
38#include <asm/io.h>
39#include <asm/irq.h>
40#include <asm/uaccess.h>
41
42/* get_phy_device
43 *
44 * description: Reads the ID registers of the PHY at addr on the
45 * bus, then allocates and returns the phy_device to
46 * represent it.
47 */
48struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
49{
50 int phy_reg;
51 u32 phy_id;
52 struct phy_device *dev = NULL;
53
54 /* Grab the bits from PHYIR1, and put them
55 * in the upper half */
56 phy_reg = bus->read(bus, addr, MII_PHYSID1);
57
58 if (phy_reg < 0)
59 return ERR_PTR(phy_reg);
60
61 phy_id = (phy_reg & 0xffff) << 16;
62
63 /* Grab the bits from PHYIR2, and put them in the lower half */
64 phy_reg = bus->read(bus, addr, MII_PHYSID2);
65
66 if (phy_reg < 0)
67 return ERR_PTR(phy_reg);
68
69 phy_id |= (phy_reg & 0xffff);
70
71 /* If the phy_id is all Fs, there is no device there */
72 if (0xffffffff == phy_id)
73 return NULL;
74
75 /* Otherwise, we allocate the device, and initialize the
76 * default values */
77 dev = kcalloc(1, sizeof(*dev), GFP_KERNEL);
78
79 if (NULL == dev)
80 return ERR_PTR(-ENOMEM);
81
82 dev->speed = 0;
83 dev->duplex = -1;
84 dev->pause = dev->asym_pause = 0;
85 dev->link = 1;
86
87 dev->autoneg = AUTONEG_ENABLE;
88
89 dev->addr = addr;
90 dev->phy_id = phy_id;
91 dev->bus = bus;
92
93 dev->state = PHY_DOWN;
94
95 spin_lock_init(&dev->lock);
96
97 return dev;
98}
99
100/* phy_prepare_link:
101 *
102 * description: Tells the PHY infrastructure to handle the
103 * gory details on monitoring link status (whether through
104 * polling or an interrupt), and to call back to the
105 * connected device driver when the link status changes.
106 * If you want to monitor your own link state, don't call
107 * this function */
108void phy_prepare_link(struct phy_device *phydev,
109 void (*handler)(struct net_device *))
110{
111 phydev->adjust_link = handler;
112}
113
114#ifdef CONFIG_PHYCONTROL
115/* phy_connect:
116 *
117 * description: Convenience function for connecting ethernet
118 * devices to PHY devices. The default behavior is for
119 * the PHY infrastructure to handle everything, and only notify
120 * the connected driver when the link status changes. If you
121 * don't want, or can't use the provided functionality, you may
122 * choose to call only the subset of functions which provide
123 * the desired functionality.
124 */
125struct phy_device * phy_connect(struct net_device *dev, const char *phy_id,
126 void (*handler)(struct net_device *), u32 flags)
127{
128 struct phy_device *phydev;
129
130 phydev = phy_attach(dev, phy_id, flags);
131
132 if (IS_ERR(phydev))
133 return phydev;
134
135 phy_prepare_link(phydev, handler);
136
137 phy_start_machine(phydev, NULL);
138
139 if (phydev->irq > 0)
140 phy_start_interrupts(phydev);
141
142 return phydev;
143}
144EXPORT_SYMBOL(phy_connect);
145
146void phy_disconnect(struct phy_device *phydev)
147{
148 if (phydev->irq > 0)
149 phy_stop_interrupts(phydev);
150
151 phy_stop_machine(phydev);
152
153 phydev->adjust_link = NULL;
154
155 phy_detach(phydev);
156}
157EXPORT_SYMBOL(phy_disconnect);
158
159#endif /* CONFIG_PHYCONTROL */
160
161/* phy_attach:
162 *
163 * description: Called by drivers to attach to a particular PHY
164 * device. The phy_device is found, and properly hooked up
165 * to the phy_driver. If no driver is attached, then the
166 * genphy_driver is used. The phy_device is given a ptr to
167 * the attaching device, and given a callback for link status
168 * change. The phy_device is returned to the attaching
169 * driver.
170 */
171static int phy_compare_id(struct device *dev, void *data)
172{
173 return strcmp((char *)data, dev->bus_id) ? 0 : 1;
174}
175
176struct phy_device *phy_attach(struct net_device *dev,
177 const char *phy_id, u32 flags)
178{
179 struct bus_type *bus = &mdio_bus_type;
180 struct phy_device *phydev;
181 struct device *d;
182
183 /* Search the list of PHY devices on the mdio bus for the
184 * PHY with the requested name */
185 d = bus_find_device(bus, NULL, (void *)phy_id, phy_compare_id);
186
187 if (d) {
188 phydev = to_phy_device(d);
189 } else {
190 printk(KERN_ERR "%s not found\n", phy_id);
191 return ERR_PTR(-ENODEV);
192 }
193
194 /* Assume that if there is no driver, that it doesn't
195 * exist, and we should use the genphy driver. */
196 if (NULL == d->driver) {
197 int err;
198 down_write(&d->bus->subsys.rwsem);
199 d->driver = &genphy_driver.driver;
200
201 err = d->driver->probe(d);
202
203 if (err < 0)
204 return ERR_PTR(err);
205
206 device_bind_driver(d);
207 up_write(&d->bus->subsys.rwsem);
208 }
209
210 if (phydev->attached_dev) {
211 printk(KERN_ERR "%s: %s already attached\n",
212 dev->name, phy_id);
213 return ERR_PTR(-EBUSY);
214 }
215
216 phydev->attached_dev = dev;
217
218 phydev->dev_flags = flags;
219
220 return phydev;
221}
222EXPORT_SYMBOL(phy_attach);
223
224void phy_detach(struct phy_device *phydev)
225{
226 phydev->attached_dev = NULL;
227
228 /* If the device had no specific driver before (i.e. - it
229 * was using the generic driver), we unbind the device
230 * from the generic driver so that there's a chance a
231 * real driver could be loaded */
232 if (phydev->dev.driver == &genphy_driver.driver) {
233 down_write(&phydev->dev.bus->subsys.rwsem);
234 device_release_driver(&phydev->dev);
235 up_write(&phydev->dev.bus->subsys.rwsem);
236 }
237}
238EXPORT_SYMBOL(phy_detach);
239
240
241/* Generic PHY support and helper functions */
242
243/* genphy_config_advert
244 *
245 * description: Writes MII_ADVERTISE with the appropriate values,
246 * after sanitizing the values to make sure we only advertise
247 * what is supported
248 */
249int genphy_config_advert(struct phy_device *phydev)
250{
251 u32 advertise;
252 int adv;
253 int err;
254
255 /* Only allow advertising what
256 * this PHY supports */
257 phydev->advertising &= phydev->supported;
258 advertise = phydev->advertising;
259
260 /* Setup standard advertisement */
261 adv = phy_read(phydev, MII_ADVERTISE);
262
263 if (adv < 0)
264 return adv;
265
266 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
267 ADVERTISE_PAUSE_ASYM);
268 if (advertise & ADVERTISED_10baseT_Half)
269 adv |= ADVERTISE_10HALF;
270 if (advertise & ADVERTISED_10baseT_Full)
271 adv |= ADVERTISE_10FULL;
272 if (advertise & ADVERTISED_100baseT_Half)
273 adv |= ADVERTISE_100HALF;
274 if (advertise & ADVERTISED_100baseT_Full)
275 adv |= ADVERTISE_100FULL;
276 if (advertise & ADVERTISED_Pause)
277 adv |= ADVERTISE_PAUSE_CAP;
278 if (advertise & ADVERTISED_Asym_Pause)
279 adv |= ADVERTISE_PAUSE_ASYM;
280
281 err = phy_write(phydev, MII_ADVERTISE, adv);
282
283 if (err < 0)
284 return err;
285
286 /* Configure gigabit if it's supported */
287 if (phydev->supported & (SUPPORTED_1000baseT_Half |
288 SUPPORTED_1000baseT_Full)) {
289 adv = phy_read(phydev, MII_CTRL1000);
290
291 if (adv < 0)
292 return adv;
293
294 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
295 if (advertise & SUPPORTED_1000baseT_Half)
296 adv |= ADVERTISE_1000HALF;
297 if (advertise & SUPPORTED_1000baseT_Full)
298 adv |= ADVERTISE_1000FULL;
299 err = phy_write(phydev, MII_CTRL1000, adv);
300
301 if (err < 0)
302 return err;
303 }
304
305 return adv;
306}
307EXPORT_SYMBOL(genphy_config_advert);
308
309/* genphy_setup_forced
310 *
311 * description: Configures MII_BMCR to force speed/duplex
312 * to the values in phydev. Assumes that the values are valid.
313 * Please see phy_sanitize_settings() */
314int genphy_setup_forced(struct phy_device *phydev)
315{
316 int ctl = BMCR_RESET;
317
318 phydev->pause = phydev->asym_pause = 0;
319
320 if (SPEED_1000 == phydev->speed)
321 ctl |= BMCR_SPEED1000;
322 else if (SPEED_100 == phydev->speed)
323 ctl |= BMCR_SPEED100;
324
325 if (DUPLEX_FULL == phydev->duplex)
326 ctl |= BMCR_FULLDPLX;
327
328 ctl = phy_write(phydev, MII_BMCR, ctl);
329
330 if (ctl < 0)
331 return ctl;
332
333 /* We just reset the device, so we'd better configure any
334 * settings the PHY requires to operate */
335 if (phydev->drv->config_init)
336 ctl = phydev->drv->config_init(phydev);
337
338 return ctl;
339}
340
341
342/* Enable and Restart Autonegotiation */
343int genphy_restart_aneg(struct phy_device *phydev)
344{
345 int ctl;
346
347 ctl = phy_read(phydev, MII_BMCR);
348
349 if (ctl < 0)
350 return ctl;
351
352 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
353
354 /* Don't isolate the PHY if we're negotiating */
355 ctl &= ~(BMCR_ISOLATE);
356
357 ctl = phy_write(phydev, MII_BMCR, ctl);
358
359 return ctl;
360}
361
362
363/* genphy_config_aneg
364 *
365 * description: If auto-negotiation is enabled, we configure the
366 * advertising, and then restart auto-negotiation. If it is not
367 * enabled, then we write the BMCR
368 */
369int genphy_config_aneg(struct phy_device *phydev)
370{
371 int err = 0;
372
373 if (AUTONEG_ENABLE == phydev->autoneg) {
374 err = genphy_config_advert(phydev);
375
376 if (err < 0)
377 return err;
378
379 err = genphy_restart_aneg(phydev);
380 } else
381 err = genphy_setup_forced(phydev);
382
383 return err;
384}
385EXPORT_SYMBOL(genphy_config_aneg);
386
387/* genphy_update_link
388 *
389 * description: Update the value in phydev->link to reflect the
390 * current link value. In order to do this, we need to read
391 * the status register twice, keeping the second value
392 */
393int genphy_update_link(struct phy_device *phydev)
394{
395 int status;
396
397 /* Do a fake read */
398 status = phy_read(phydev, MII_BMSR);
399
400 if (status < 0)
401 return status;
402
403 /* Read link and autonegotiation status */
404 status = phy_read(phydev, MII_BMSR);
405
406 if (status < 0)
407 return status;
408
409 if ((status & BMSR_LSTATUS) == 0)
410 phydev->link = 0;
411 else
412 phydev->link = 1;
413
414 return 0;
415}
416
417/* genphy_read_status
418 *
419 * description: Check the link, then figure out the current state
420 * by comparing what we advertise with what the link partner
421 * advertises. Start by checking the gigabit possibilities,
422 * then move on to 10/100.
423 */
424int genphy_read_status(struct phy_device *phydev)
425{
426 int adv;
427 int err;
428 int lpa;
429 int lpagb = 0;
430
431 /* Update the link, but return if there
432 * was an error */
433 err = genphy_update_link(phydev);
434 if (err)
435 return err;
436
437 if (AUTONEG_ENABLE == phydev->autoneg) {
438 if (phydev->supported & (SUPPORTED_1000baseT_Half
439 | SUPPORTED_1000baseT_Full)) {
440 lpagb = phy_read(phydev, MII_STAT1000);
441
442 if (lpagb < 0)
443 return lpagb;
444
445 adv = phy_read(phydev, MII_CTRL1000);
446
447 if (adv < 0)
448 return adv;
449
450 lpagb &= adv << 2;
451 }
452
453 lpa = phy_read(phydev, MII_LPA);
454
455 if (lpa < 0)
456 return lpa;
457
458 adv = phy_read(phydev, MII_ADVERTISE);
459
460 if (adv < 0)
461 return adv;
462
463 lpa &= adv;
464
465 phydev->speed = SPEED_10;
466 phydev->duplex = DUPLEX_HALF;
467 phydev->pause = phydev->asym_pause = 0;
468
469 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
470 phydev->speed = SPEED_1000;
471
472 if (lpagb & LPA_1000FULL)
473 phydev->duplex = DUPLEX_FULL;
474 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
475 phydev->speed = SPEED_100;
476
477 if (lpa & LPA_100FULL)
478 phydev->duplex = DUPLEX_FULL;
479 } else
480 if (lpa & LPA_10FULL)
481 phydev->duplex = DUPLEX_FULL;
482
483 if (phydev->duplex == DUPLEX_FULL){
484 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
485 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
486 }
487 } else {
488 int bmcr = phy_read(phydev, MII_BMCR);
489 if (bmcr < 0)
490 return bmcr;
491
492 if (bmcr & BMCR_FULLDPLX)
493 phydev->duplex = DUPLEX_FULL;
494 else
495 phydev->duplex = DUPLEX_HALF;
496
497 if (bmcr & BMCR_SPEED1000)
498 phydev->speed = SPEED_1000;
499 else if (bmcr & BMCR_SPEED100)
500 phydev->speed = SPEED_100;
501 else
502 phydev->speed = SPEED_10;
503
504 phydev->pause = phydev->asym_pause = 0;
505 }
506
507 return 0;
508}
509EXPORT_SYMBOL(genphy_read_status);
510
511static int genphy_config_init(struct phy_device *phydev)
512{
513 u32 val;
514 u32 features;
515
516 /* For now, I'll claim that the generic driver supports
517 * all possible port types */
518 features = (SUPPORTED_TP | SUPPORTED_MII
519 | SUPPORTED_AUI | SUPPORTED_FIBRE |
520 SUPPORTED_BNC);
521
522 /* Do we support autonegotiation? */
523 val = phy_read(phydev, MII_BMSR);
524
525 if (val < 0)
526 return val;
527
528 if (val & BMSR_ANEGCAPABLE)
529 features |= SUPPORTED_Autoneg;
530
531 if (val & BMSR_100FULL)
532 features |= SUPPORTED_100baseT_Full;
533 if (val & BMSR_100HALF)
534 features |= SUPPORTED_100baseT_Half;
535 if (val & BMSR_10FULL)
536 features |= SUPPORTED_10baseT_Full;
537 if (val & BMSR_10HALF)
538 features |= SUPPORTED_10baseT_Half;
539
540 if (val & BMSR_ESTATEN) {
541 val = phy_read(phydev, MII_ESTATUS);
542
543 if (val < 0)
544 return val;
545
546 if (val & ESTATUS_1000_TFULL)
547 features |= SUPPORTED_1000baseT_Full;
548 if (val & ESTATUS_1000_THALF)
549 features |= SUPPORTED_1000baseT_Half;
550 }
551
552 phydev->supported = features;
553 phydev->advertising = features;
554
555 return 0;
556}
557
558
559/* phy_probe
560 *
561 * description: Take care of setting up the phy_device structure,
562 * set the state to READY (the driver's init function should
563 * set it to STARTING if needed).
564 */
565static int phy_probe(struct device *dev)
566{
567 struct phy_device *phydev;
568 struct phy_driver *phydrv;
569 struct device_driver *drv;
570 int err = 0;
571
572 phydev = to_phy_device(dev);
573
574 /* Make sure the driver is held.
575 * XXX -- Is this correct? */
576 drv = get_driver(phydev->dev.driver);
577 phydrv = to_phy_driver(drv);
578 phydev->drv = phydrv;
579
580 /* Disable the interrupt if the PHY doesn't support it */
581 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
582 phydev->irq = PHY_POLL;
583
584 spin_lock(&phydev->lock);
585
586 /* Start out supporting everything. Eventually,
587 * a controller will attach, and may modify one
588 * or both of these values */
589 phydev->supported = phydrv->features;
590 phydev->advertising = phydrv->features;
591
592 /* Set the state to READY by default */
593 phydev->state = PHY_READY;
594
595 if (phydev->drv->probe)
596 err = phydev->drv->probe(phydev);
597
598 spin_unlock(&phydev->lock);
599
600 if (err < 0)
601 return err;
602
603 if (phydev->drv->config_init)
604 err = phydev->drv->config_init(phydev);
605
606 return err;
607}
608
609static int phy_remove(struct device *dev)
610{
611 struct phy_device *phydev;
612
613 phydev = to_phy_device(dev);
614
615 spin_lock(&phydev->lock);
616 phydev->state = PHY_DOWN;
617 spin_unlock(&phydev->lock);
618
619 if (phydev->drv->remove)
620 phydev->drv->remove(phydev);
621
622 put_driver(dev->driver);
623 phydev->drv = NULL;
624
625 return 0;
626}
627
628int phy_driver_register(struct phy_driver *new_driver)
629{
630 int retval;
631
632 memset(&new_driver->driver, 0, sizeof(new_driver->driver));
633 new_driver->driver.name = new_driver->name;
634 new_driver->driver.bus = &mdio_bus_type;
635 new_driver->driver.probe = phy_probe;
636 new_driver->driver.remove = phy_remove;
637
638 retval = driver_register(&new_driver->driver);
639
640 if (retval) {
641 printk(KERN_ERR "%s: Error %d in registering driver\n",
642 new_driver->name, retval);
643
644 return retval;
645 }
646
647 pr_info("%s: Registered new driver\n", new_driver->name);
648
649 return 0;
650}
651EXPORT_SYMBOL(phy_driver_register);
652
653void phy_driver_unregister(struct phy_driver *drv)
654{
655 driver_unregister(&drv->driver);
656}
657EXPORT_SYMBOL(phy_driver_unregister);
658
659static struct phy_driver genphy_driver = {
660 .phy_id = 0xffffffff,
661 .phy_id_mask = 0xffffffff,
662 .name = "Generic PHY",
663 .config_init = genphy_config_init,
664 .features = 0,
665 .config_aneg = genphy_config_aneg,
666 .read_status = genphy_read_status,
667 .driver = {.owner = THIS_MODULE, },
668};
669
670static int __init genphy_init(void)
671{
672 return phy_driver_register(&genphy_driver);
673
674}
675
676static void __exit genphy_exit(void)
677{
678 phy_driver_unregister(&genphy_driver);
679}
680
681module_init(genphy_init);
682module_exit(genphy_exit);