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