]> err.no Git - linux-2.6/blob - drivers/net/phy/phy_device.c
Fix numerous minor problems with new phy subsystem.
[linux-2.6] / drivers / net / phy / phy_device.c
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 static int genphy_config_init(struct phy_device *phydev);
43
44 static struct phy_driver genphy_driver = {
45         .phy_id         = 0xffffffff,
46         .phy_id_mask    = 0xffffffff,
47         .name           = "Generic PHY",
48         .config_init    = genphy_config_init,
49         .features       = 0,
50         .config_aneg    = genphy_config_aneg,
51         .read_status    = genphy_read_status,
52         .driver =       {.owner = THIS_MODULE, },
53 };
54
55 /* get_phy_device
56  *
57  * description: Reads the ID registers of the PHY at addr on the
58  *   bus, then allocates and returns the phy_device to
59  *   represent it.
60  */
61 struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
62 {
63         int phy_reg;
64         u32 phy_id;
65         struct phy_device *dev = NULL;
66
67         /* Grab the bits from PHYIR1, and put them
68          * in the upper half */
69         phy_reg = bus->read(bus, addr, MII_PHYSID1);
70
71         if (phy_reg < 0)
72                 return ERR_PTR(phy_reg);
73
74         phy_id = (phy_reg & 0xffff) << 16;
75
76         /* Grab the bits from PHYIR2, and put them in the lower half */
77         phy_reg = bus->read(bus, addr, MII_PHYSID2);
78
79         if (phy_reg < 0)
80                 return ERR_PTR(phy_reg);
81
82         phy_id |= (phy_reg & 0xffff);
83
84         /* If the phy_id is all Fs, there is no device there */
85         if (0xffffffff == phy_id)
86                 return NULL;
87
88         /* Otherwise, we allocate the device, and initialize the
89          * default values */
90         dev = kcalloc(1, sizeof(*dev), GFP_KERNEL);
91
92         if (NULL == dev)
93                 return ERR_PTR(-ENOMEM);
94
95         dev->speed = 0;
96         dev->duplex = -1;
97         dev->pause = dev->asym_pause = 0;
98         dev->link = 1;
99
100         dev->autoneg = AUTONEG_ENABLE;
101
102         dev->addr = addr;
103         dev->phy_id = phy_id;
104         dev->bus = bus;
105
106         dev->state = PHY_DOWN;
107
108         spin_lock_init(&dev->lock);
109
110         return dev;
111 }
112
113 /* phy_prepare_link:
114  *
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
120  *   this function */
121 void phy_prepare_link(struct phy_device *phydev,
122                 void (*handler)(struct net_device *))
123 {
124         phydev->adjust_link = handler;
125 }
126
127 #ifdef CONFIG_PHYCONTROL
128 /* phy_connect:
129  *
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.
137  */
138 struct phy_device * phy_connect(struct net_device *dev, const char *phy_id,
139                 void (*handler)(struct net_device *), u32 flags)
140 {
141         struct phy_device *phydev;
142
143         phydev = phy_attach(dev, phy_id, flags);
144
145         if (IS_ERR(phydev))
146                 return phydev;
147
148         phy_prepare_link(phydev, handler);
149
150         phy_start_machine(phydev, NULL);
151
152         if (phydev->irq > 0)
153                 phy_start_interrupts(phydev);
154
155         return phydev;
156 }
157 EXPORT_SYMBOL(phy_connect);
158
159 void phy_disconnect(struct phy_device *phydev)
160 {
161         if (phydev->irq > 0)
162                 phy_stop_interrupts(phydev);
163
164         phy_stop_machine(phydev);
165         
166         phydev->adjust_link = NULL;
167
168         phy_detach(phydev);
169 }
170 EXPORT_SYMBOL(phy_disconnect);
171
172 #endif /* CONFIG_PHYCONTROL */
173
174 /* phy_attach:
175  *
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
182  *     driver.
183  */
184 static int phy_compare_id(struct device *dev, void *data)
185 {
186         return strcmp((char *)data, dev->bus_id) ? 0 : 1;
187 }
188
189 struct phy_device *phy_attach(struct net_device *dev,
190                 const char *phy_id, u32 flags)
191 {
192         struct bus_type *bus = &mdio_bus_type;
193         struct phy_device *phydev;
194         struct device *d;
195
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);
199
200         if (d) {
201                 phydev = to_phy_device(d);
202         } else {
203                 printk(KERN_ERR "%s not found\n", phy_id);
204                 return ERR_PTR(-ENODEV);
205         }
206
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) {
210                 int err;
211                 down_write(&d->bus->subsys.rwsem);
212                 d->driver = &genphy_driver.driver;
213
214                 err = d->driver->probe(d);
215
216                 if (err < 0)
217                         return ERR_PTR(err);
218
219                 device_bind_driver(d);
220                 up_write(&d->bus->subsys.rwsem);
221         }
222
223         if (phydev->attached_dev) {
224                 printk(KERN_ERR "%s: %s already attached\n",
225                                 dev->name, phy_id);
226                 return ERR_PTR(-EBUSY);
227         }
228
229         phydev->attached_dev = dev;
230
231         phydev->dev_flags = flags;
232
233         return phydev;
234 }
235 EXPORT_SYMBOL(phy_attach);
236
237 void phy_detach(struct phy_device *phydev)
238 {
239         phydev->attached_dev = NULL;
240
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);
249         }
250 }
251 EXPORT_SYMBOL(phy_detach);
252
253
254 /* Generic PHY support and helper functions */
255
256 /* genphy_config_advert
257  *
258  * description: Writes MII_ADVERTISE with the appropriate values,
259  *   after sanitizing the values to make sure we only advertise
260  *   what is supported
261  */
262 int genphy_config_advert(struct phy_device *phydev)
263 {
264         u32 advertise;
265         int adv;
266         int err;
267
268         /* Only allow advertising what
269          * this PHY supports */
270         phydev->advertising &= phydev->supported;
271         advertise = phydev->advertising;
272
273         /* Setup standard advertisement */
274         adv = phy_read(phydev, MII_ADVERTISE);
275
276         if (adv < 0)
277                 return adv;
278
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;
293
294         err = phy_write(phydev, MII_ADVERTISE, adv);
295
296         if (err < 0)
297                 return err;
298
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);
303
304                 if (adv < 0)
305                         return adv;
306
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);
313
314                 if (err < 0)
315                         return err;
316         }
317
318         return adv;
319 }
320 EXPORT_SYMBOL(genphy_config_advert);
321
322 /* genphy_setup_forced
323  *
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)
328 {
329         int ctl = BMCR_RESET;
330
331         phydev->pause = phydev->asym_pause = 0;
332
333         if (SPEED_1000 == phydev->speed)
334                 ctl |= BMCR_SPEED1000;
335         else if (SPEED_100 == phydev->speed)
336                 ctl |= BMCR_SPEED100;
337
338         if (DUPLEX_FULL == phydev->duplex)
339                 ctl |= BMCR_FULLDPLX;
340         
341         ctl = phy_write(phydev, MII_BMCR, ctl);
342
343         if (ctl < 0)
344                 return ctl;
345
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);
350
351         return ctl;
352 }
353
354
355 /* Enable and Restart Autonegotiation */
356 int genphy_restart_aneg(struct phy_device *phydev)
357 {
358         int ctl;
359
360         ctl = phy_read(phydev, MII_BMCR);
361
362         if (ctl < 0)
363                 return ctl;
364
365         ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
366
367         /* Don't isolate the PHY if we're negotiating */
368         ctl &= ~(BMCR_ISOLATE);
369
370         ctl = phy_write(phydev, MII_BMCR, ctl);
371
372         return ctl;
373 }
374
375
376 /* genphy_config_aneg
377  *
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
381  */
382 int genphy_config_aneg(struct phy_device *phydev)
383 {
384         int err = 0;
385
386         if (AUTONEG_ENABLE == phydev->autoneg) {
387                 err = genphy_config_advert(phydev);
388
389                 if (err < 0)
390                         return err;
391
392                 err = genphy_restart_aneg(phydev);
393         } else
394                 err = genphy_setup_forced(phydev);
395
396         return err;
397 }
398 EXPORT_SYMBOL(genphy_config_aneg);
399
400 /* genphy_update_link
401  *
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
405  */
406 int genphy_update_link(struct phy_device *phydev)
407 {
408         int status;
409
410         /* Do a fake read */
411         status = phy_read(phydev, MII_BMSR);
412
413         if (status < 0)
414                 return status;
415
416         /* Read link and autonegotiation status */
417         status = phy_read(phydev, MII_BMSR);
418
419         if (status < 0)
420                 return status;
421
422         if ((status & BMSR_LSTATUS) == 0)
423                 phydev->link = 0;
424         else
425                 phydev->link = 1;
426
427         return 0;
428 }
429
430 /* genphy_read_status
431  *
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.
436  */
437 int genphy_read_status(struct phy_device *phydev)
438 {
439         int adv;
440         int err;
441         int lpa;
442         int lpagb = 0;
443
444         /* Update the link, but return if there
445          * was an error */
446         err = genphy_update_link(phydev);
447         if (err)
448                 return err;
449
450         if (AUTONEG_ENABLE == phydev->autoneg) {
451                 if (phydev->supported & (SUPPORTED_1000baseT_Half
452                                         | SUPPORTED_1000baseT_Full)) {
453                         lpagb = phy_read(phydev, MII_STAT1000);
454
455                         if (lpagb < 0)
456                                 return lpagb;
457
458                         adv = phy_read(phydev, MII_CTRL1000);
459
460                         if (adv < 0)
461                                 return adv;
462
463                         lpagb &= adv << 2;
464                 }
465
466                 lpa = phy_read(phydev, MII_LPA);
467
468                 if (lpa < 0)
469                         return lpa;
470
471                 adv = phy_read(phydev, MII_ADVERTISE);
472
473                 if (adv < 0)
474                         return adv;
475
476                 lpa &= adv;
477
478                 phydev->speed = SPEED_10;
479                 phydev->duplex = DUPLEX_HALF;
480                 phydev->pause = phydev->asym_pause = 0;
481
482                 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
483                         phydev->speed = SPEED_1000;
484
485                         if (lpagb & LPA_1000FULL)
486                                 phydev->duplex = DUPLEX_FULL;
487                 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
488                         phydev->speed = SPEED_100;
489                         
490                         if (lpa & LPA_100FULL)
491                                 phydev->duplex = DUPLEX_FULL;
492                 } else
493                         if (lpa & LPA_10FULL)
494                                 phydev->duplex = DUPLEX_FULL;
495
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;
499                 }
500         } else {
501                 int bmcr = phy_read(phydev, MII_BMCR);
502                 if (bmcr < 0)
503                         return bmcr;
504
505                 if (bmcr & BMCR_FULLDPLX)
506                         phydev->duplex = DUPLEX_FULL;
507                 else
508                         phydev->duplex = DUPLEX_HALF;
509
510                 if (bmcr & BMCR_SPEED1000)
511                         phydev->speed = SPEED_1000;
512                 else if (bmcr & BMCR_SPEED100)
513                         phydev->speed = SPEED_100;
514                 else
515                         phydev->speed = SPEED_10;
516
517                 phydev->pause = phydev->asym_pause = 0;
518         }
519
520         return 0;
521 }
522 EXPORT_SYMBOL(genphy_read_status);
523
524 static int genphy_config_init(struct phy_device *phydev)
525 {
526         u32 val;
527         u32 features;
528
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 |
533                         SUPPORTED_BNC);
534
535         /* Do we support autonegotiation? */
536         val = phy_read(phydev, MII_BMSR);
537
538         if (val < 0)
539                 return val;
540
541         if (val & BMSR_ANEGCAPABLE)
542                 features |= SUPPORTED_Autoneg;
543
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;
552
553         if (val & BMSR_ESTATEN) {
554                 val = phy_read(phydev, MII_ESTATUS);
555
556                 if (val < 0)
557                         return val;
558
559                 if (val & ESTATUS_1000_TFULL)
560                         features |= SUPPORTED_1000baseT_Full;
561                 if (val & ESTATUS_1000_THALF)
562                         features |= SUPPORTED_1000baseT_Half;
563         }
564
565         phydev->supported = features;
566         phydev->advertising = features;
567
568         return 0;
569 }
570
571
572 /* phy_probe
573  *
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).
577  */
578 static int phy_probe(struct device *dev)
579 {
580         struct phy_device *phydev;
581         struct phy_driver *phydrv;
582         struct device_driver *drv;
583         int err = 0;
584
585         phydev = to_phy_device(dev);
586
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;
592
593         /* Disable the interrupt if the PHY doesn't support it */
594         if (!(phydrv->flags & PHY_HAS_INTERRUPT))
595                 phydev->irq = PHY_POLL;
596
597         spin_lock(&phydev->lock);
598
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;
604
605         /* Set the state to READY by default */
606         phydev->state = PHY_READY;
607
608         if (phydev->drv->probe)
609                 err = phydev->drv->probe(phydev);
610
611         spin_unlock(&phydev->lock);
612
613         if (err < 0)
614                 return err;
615
616         if (phydev->drv->config_init)
617                 err = phydev->drv->config_init(phydev);
618
619         return err;
620 }
621
622 static int phy_remove(struct device *dev)
623 {
624         struct phy_device *phydev;
625
626         phydev = to_phy_device(dev);
627
628         spin_lock(&phydev->lock);
629         phydev->state = PHY_DOWN;
630         spin_unlock(&phydev->lock);
631
632         if (phydev->drv->remove)
633                 phydev->drv->remove(phydev);
634
635         put_driver(dev->driver);
636         phydev->drv = NULL;
637
638         return 0;
639 }
640
641 int phy_driver_register(struct phy_driver *new_driver)
642 {
643         int retval;
644
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;
650
651         retval = driver_register(&new_driver->driver);
652
653         if (retval) {
654                 printk(KERN_ERR "%s: Error %d in registering driver\n",
655                                 new_driver->name, retval);
656
657                 return retval;
658         }
659
660         pr_info("%s: Registered new driver\n", new_driver->name);
661
662         return 0;
663 }
664 EXPORT_SYMBOL(phy_driver_register);
665
666 void phy_driver_unregister(struct phy_driver *drv)
667 {
668         driver_unregister(&drv->driver);
669 }
670 EXPORT_SYMBOL(phy_driver_unregister);
671
672
673 static int __init phy_init(void)
674 {
675         int rc;
676         extern int mdio_bus_init(void);
677
678         rc = phy_driver_register(&genphy_driver);
679         if (rc)
680                 goto out;
681
682         rc = mdio_bus_init();
683         if (rc)
684                 goto out_unreg;
685
686         return 0;
687
688 out_unreg:
689         phy_driver_unregister(&genphy_driver);
690 out:
691         return rc;
692 }
693
694 static void __exit phy_exit(void)
695 {
696         phy_driver_unregister(&genphy_driver);
697 }
698
699 module_init(phy_init);
700 module_exit(phy_exit);