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