8f01952c48500457315624a2b6f962a8043ab838
[firefly-linux-kernel-4.4.55.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 /* 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                 phy_interface_t 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, phy_interface_t 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                 d->driver = &genphy_driver.driver;
212
213                 err = d->driver->probe(d);
214                 if (err >= 0)
215                         err = device_bind_driver(d);
216
217                 if (err)
218                         return ERR_PTR(err);
219         }
220
221         if (phydev->attached_dev) {
222                 printk(KERN_ERR "%s: %s already attached\n",
223                                 dev->name, phy_id);
224                 return ERR_PTR(-EBUSY);
225         }
226
227         phydev->attached_dev = dev;
228
229         phydev->dev_flags = flags;
230
231         phydev->interface = interface;
232
233         /* Do initial configuration here, now that
234          * we have certain key parameters
235          * (dev_flags and interface) */
236         if (phydev->drv->config_init) {
237                 int err;
238
239                 err = phydev->drv->config_init(phydev);
240
241                 if (err < 0)
242                         return ERR_PTR(err);
243         }
244
245         return phydev;
246 }
247 EXPORT_SYMBOL(phy_attach);
248
249 void phy_detach(struct phy_device *phydev)
250 {
251         phydev->attached_dev = NULL;
252
253         /* If the device had no specific driver before (i.e. - it
254          * was using the generic driver), we unbind the device
255          * from the generic driver so that there's a chance a
256          * real driver could be loaded */
257         if (phydev->dev.driver == &genphy_driver.driver)
258                 device_release_driver(&phydev->dev);
259 }
260 EXPORT_SYMBOL(phy_detach);
261
262
263 /* Generic PHY support and helper functions */
264
265 /* genphy_config_advert
266  *
267  * description: Writes MII_ADVERTISE with the appropriate values,
268  *   after sanitizing the values to make sure we only advertise
269  *   what is supported
270  */
271 int genphy_config_advert(struct phy_device *phydev)
272 {
273         u32 advertise;
274         int adv;
275         int err;
276
277         /* Only allow advertising what
278          * this PHY supports */
279         phydev->advertising &= phydev->supported;
280         advertise = phydev->advertising;
281
282         /* Setup standard advertisement */
283         adv = phy_read(phydev, MII_ADVERTISE);
284
285         if (adv < 0)
286                 return adv;
287
288         adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | 
289                  ADVERTISE_PAUSE_ASYM);
290         if (advertise & ADVERTISED_10baseT_Half)
291                 adv |= ADVERTISE_10HALF;
292         if (advertise & ADVERTISED_10baseT_Full)
293                 adv |= ADVERTISE_10FULL;
294         if (advertise & ADVERTISED_100baseT_Half)
295                 adv |= ADVERTISE_100HALF;
296         if (advertise & ADVERTISED_100baseT_Full)
297                 adv |= ADVERTISE_100FULL;
298         if (advertise & ADVERTISED_Pause)
299                 adv |= ADVERTISE_PAUSE_CAP;
300         if (advertise & ADVERTISED_Asym_Pause)
301                 adv |= ADVERTISE_PAUSE_ASYM;
302
303         err = phy_write(phydev, MII_ADVERTISE, adv);
304
305         if (err < 0)
306                 return err;
307
308         /* Configure gigabit if it's supported */
309         if (phydev->supported & (SUPPORTED_1000baseT_Half |
310                                 SUPPORTED_1000baseT_Full)) {
311                 adv = phy_read(phydev, MII_CTRL1000);
312
313                 if (adv < 0)
314                         return adv;
315
316                 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
317                 if (advertise & SUPPORTED_1000baseT_Half)
318                         adv |= ADVERTISE_1000HALF;
319                 if (advertise & SUPPORTED_1000baseT_Full)
320                         adv |= ADVERTISE_1000FULL;
321                 err = phy_write(phydev, MII_CTRL1000, adv);
322
323                 if (err < 0)
324                         return err;
325         }
326
327         return adv;
328 }
329 EXPORT_SYMBOL(genphy_config_advert);
330
331 /* genphy_setup_forced
332  *
333  * description: Configures MII_BMCR to force speed/duplex
334  *   to the values in phydev. Assumes that the values are valid.
335  *   Please see phy_sanitize_settings() */
336 int genphy_setup_forced(struct phy_device *phydev)
337 {
338         int ctl = BMCR_RESET;
339
340         phydev->pause = phydev->asym_pause = 0;
341
342         if (SPEED_1000 == phydev->speed)
343                 ctl |= BMCR_SPEED1000;
344         else if (SPEED_100 == phydev->speed)
345                 ctl |= BMCR_SPEED100;
346
347         if (DUPLEX_FULL == phydev->duplex)
348                 ctl |= BMCR_FULLDPLX;
349         
350         ctl = phy_write(phydev, MII_BMCR, ctl);
351
352         if (ctl < 0)
353                 return ctl;
354
355         /* We just reset the device, so we'd better configure any
356          * settings the PHY requires to operate */
357         if (phydev->drv->config_init)
358                 ctl = phydev->drv->config_init(phydev);
359
360         return ctl;
361 }
362
363
364 /* Enable and Restart Autonegotiation */
365 int genphy_restart_aneg(struct phy_device *phydev)
366 {
367         int ctl;
368
369         ctl = phy_read(phydev, MII_BMCR);
370
371         if (ctl < 0)
372                 return ctl;
373
374         ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
375
376         /* Don't isolate the PHY if we're negotiating */
377         ctl &= ~(BMCR_ISOLATE);
378
379         ctl = phy_write(phydev, MII_BMCR, ctl);
380
381         return ctl;
382 }
383
384
385 /* genphy_config_aneg
386  *
387  * description: If auto-negotiation is enabled, we configure the
388  *   advertising, and then restart auto-negotiation.  If it is not
389  *   enabled, then we write the BMCR
390  */
391 int genphy_config_aneg(struct phy_device *phydev)
392 {
393         int err = 0;
394
395         if (AUTONEG_ENABLE == phydev->autoneg) {
396                 err = genphy_config_advert(phydev);
397
398                 if (err < 0)
399                         return err;
400
401                 err = genphy_restart_aneg(phydev);
402         } else
403                 err = genphy_setup_forced(phydev);
404
405         return err;
406 }
407 EXPORT_SYMBOL(genphy_config_aneg);
408
409 /* genphy_update_link
410  *
411  * description: Update the value in phydev->link to reflect the
412  *   current link value.  In order to do this, we need to read
413  *   the status register twice, keeping the second value
414  */
415 int genphy_update_link(struct phy_device *phydev)
416 {
417         int status;
418
419         /* Do a fake read */
420         status = phy_read(phydev, MII_BMSR);
421
422         if (status < 0)
423                 return status;
424
425         /* Read link and autonegotiation status */
426         status = phy_read(phydev, MII_BMSR);
427
428         if (status < 0)
429                 return status;
430
431         if ((status & BMSR_LSTATUS) == 0)
432                 phydev->link = 0;
433         else
434                 phydev->link = 1;
435
436         return 0;
437 }
438 EXPORT_SYMBOL(genphy_update_link);
439
440 /* genphy_read_status
441  *
442  * description: Check the link, then figure out the current state
443  *   by comparing what we advertise with what the link partner
444  *   advertises.  Start by checking the gigabit possibilities,
445  *   then move on to 10/100.
446  */
447 int genphy_read_status(struct phy_device *phydev)
448 {
449         int adv;
450         int err;
451         int lpa;
452         int lpagb = 0;
453
454         /* Update the link, but return if there
455          * was an error */
456         err = genphy_update_link(phydev);
457         if (err)
458                 return err;
459
460         if (AUTONEG_ENABLE == phydev->autoneg) {
461                 if (phydev->supported & (SUPPORTED_1000baseT_Half
462                                         | SUPPORTED_1000baseT_Full)) {
463                         lpagb = phy_read(phydev, MII_STAT1000);
464
465                         if (lpagb < 0)
466                                 return lpagb;
467
468                         adv = phy_read(phydev, MII_CTRL1000);
469
470                         if (adv < 0)
471                                 return adv;
472
473                         lpagb &= adv << 2;
474                 }
475
476                 lpa = phy_read(phydev, MII_LPA);
477
478                 if (lpa < 0)
479                         return lpa;
480
481                 adv = phy_read(phydev, MII_ADVERTISE);
482
483                 if (adv < 0)
484                         return adv;
485
486                 lpa &= adv;
487
488                 phydev->speed = SPEED_10;
489                 phydev->duplex = DUPLEX_HALF;
490                 phydev->pause = phydev->asym_pause = 0;
491
492                 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
493                         phydev->speed = SPEED_1000;
494
495                         if (lpagb & LPA_1000FULL)
496                                 phydev->duplex = DUPLEX_FULL;
497                 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
498                         phydev->speed = SPEED_100;
499                         
500                         if (lpa & LPA_100FULL)
501                                 phydev->duplex = DUPLEX_FULL;
502                 } else
503                         if (lpa & LPA_10FULL)
504                                 phydev->duplex = DUPLEX_FULL;
505
506                 if (phydev->duplex == DUPLEX_FULL){
507                         phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
508                         phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
509                 }
510         } else {
511                 int bmcr = phy_read(phydev, MII_BMCR);
512                 if (bmcr < 0)
513                         return bmcr;
514
515                 if (bmcr & BMCR_FULLDPLX)
516                         phydev->duplex = DUPLEX_FULL;
517                 else
518                         phydev->duplex = DUPLEX_HALF;
519
520                 if (bmcr & BMCR_SPEED1000)
521                         phydev->speed = SPEED_1000;
522                 else if (bmcr & BMCR_SPEED100)
523                         phydev->speed = SPEED_100;
524                 else
525                         phydev->speed = SPEED_10;
526
527                 phydev->pause = phydev->asym_pause = 0;
528         }
529
530         return 0;
531 }
532 EXPORT_SYMBOL(genphy_read_status);
533
534 static int genphy_config_init(struct phy_device *phydev)
535 {
536         int val;
537         u32 features;
538
539         /* For now, I'll claim that the generic driver supports
540          * all possible port types */
541         features = (SUPPORTED_TP | SUPPORTED_MII
542                         | SUPPORTED_AUI | SUPPORTED_FIBRE |
543                         SUPPORTED_BNC);
544
545         /* Do we support autonegotiation? */
546         val = phy_read(phydev, MII_BMSR);
547
548         if (val < 0)
549                 return val;
550
551         if (val & BMSR_ANEGCAPABLE)
552                 features |= SUPPORTED_Autoneg;
553
554         if (val & BMSR_100FULL)
555                 features |= SUPPORTED_100baseT_Full;
556         if (val & BMSR_100HALF)
557                 features |= SUPPORTED_100baseT_Half;
558         if (val & BMSR_10FULL)
559                 features |= SUPPORTED_10baseT_Full;
560         if (val & BMSR_10HALF)
561                 features |= SUPPORTED_10baseT_Half;
562
563         if (val & BMSR_ESTATEN) {
564                 val = phy_read(phydev, MII_ESTATUS);
565
566                 if (val < 0)
567                         return val;
568
569                 if (val & ESTATUS_1000_TFULL)
570                         features |= SUPPORTED_1000baseT_Full;
571                 if (val & ESTATUS_1000_THALF)
572                         features |= SUPPORTED_1000baseT_Half;
573         }
574
575         phydev->supported = features;
576         phydev->advertising = features;
577
578         return 0;
579 }
580
581
582 /* phy_probe
583  *
584  * description: Take care of setting up the phy_device structure,
585  *   set the state to READY (the driver's init function should
586  *   set it to STARTING if needed).
587  */
588 static int phy_probe(struct device *dev)
589 {
590         struct phy_device *phydev;
591         struct phy_driver *phydrv;
592         struct device_driver *drv;
593         int err = 0;
594
595         phydev = to_phy_device(dev);
596
597         /* Make sure the driver is held.
598          * XXX -- Is this correct? */
599         drv = get_driver(phydev->dev.driver);
600         phydrv = to_phy_driver(drv);
601         phydev->drv = phydrv;
602
603         /* Disable the interrupt if the PHY doesn't support it */
604         if (!(phydrv->flags & PHY_HAS_INTERRUPT))
605                 phydev->irq = PHY_POLL;
606
607         spin_lock(&phydev->lock);
608
609         /* Start out supporting everything. Eventually,
610          * a controller will attach, and may modify one
611          * or both of these values */
612         phydev->supported = phydrv->features;
613         phydev->advertising = phydrv->features;
614
615         /* Set the state to READY by default */
616         phydev->state = PHY_READY;
617
618         if (phydev->drv->probe)
619                 err = phydev->drv->probe(phydev);
620
621         spin_unlock(&phydev->lock);
622
623         return err;
624
625 }
626
627 static int phy_remove(struct device *dev)
628 {
629         struct phy_device *phydev;
630
631         phydev = to_phy_device(dev);
632
633         spin_lock(&phydev->lock);
634         phydev->state = PHY_DOWN;
635         spin_unlock(&phydev->lock);
636
637         if (phydev->drv->remove)
638                 phydev->drv->remove(phydev);
639
640         put_driver(dev->driver);
641         phydev->drv = NULL;
642
643         return 0;
644 }
645
646 int phy_driver_register(struct phy_driver *new_driver)
647 {
648         int retval;
649
650         memset(&new_driver->driver, 0, sizeof(new_driver->driver));
651         new_driver->driver.name = new_driver->name;
652         new_driver->driver.bus = &mdio_bus_type;
653         new_driver->driver.probe = phy_probe;
654         new_driver->driver.remove = phy_remove;
655
656         retval = driver_register(&new_driver->driver);
657
658         if (retval) {
659                 printk(KERN_ERR "%s: Error %d in registering driver\n",
660                                 new_driver->name, retval);
661
662                 return retval;
663         }
664
665         pr_info("%s: Registered new driver\n", new_driver->name);
666
667         return 0;
668 }
669 EXPORT_SYMBOL(phy_driver_register);
670
671 void phy_driver_unregister(struct phy_driver *drv)
672 {
673         driver_unregister(&drv->driver);
674 }
675 EXPORT_SYMBOL(phy_driver_unregister);
676
677 static struct phy_driver genphy_driver = {
678         .phy_id         = 0xffffffff,
679         .phy_id_mask    = 0xffffffff,
680         .name           = "Generic PHY",
681         .config_init    = genphy_config_init,
682         .features       = 0,
683         .config_aneg    = genphy_config_aneg,
684         .read_status    = genphy_read_status,
685         .driver         = {.owner= THIS_MODULE, },
686 };
687
688 static int __init phy_init(void)
689 {
690         int rc;
691
692         rc = mdio_bus_init();
693         if (rc)
694                 return rc;
695
696         rc = phy_driver_register(&genphy_driver);
697         if (rc)
698                 mdio_bus_exit();
699
700         return rc;
701 }
702
703 static void __exit phy_exit(void)
704 {
705         phy_driver_unregister(&genphy_driver);
706         mdio_bus_exit();
707 }
708
709 subsys_initcall(phy_init);
710 module_exit(phy_exit);