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