1
2
3
4
5
6
7
8
9
10
11
12
13
14
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/mm.h>
29#include <linux/module.h>
30#include <linux/mii.h>
31#include <linux/ethtool.h>
32#include <linux/phy.h>
33
34#include <asm/io.h>
35#include <asm/irq.h>
36#include <asm/uaccess.h>
37
38MODULE_DESCRIPTION("PHY library");
39MODULE_AUTHOR("Andy Fleming");
40MODULE_LICENSE("GPL");
41
42static struct phy_driver genphy_driver;
43extern int mdio_bus_init(void);
44extern void mdio_bus_exit(void);
45
46void phy_device_free(struct phy_device *phydev)
47{
48 kfree(phydev);
49}
50
51static void phy_device_release(struct device *dev)
52{
53 phy_device_free(to_phy_device(dev));
54}
55
56static LIST_HEAD(phy_fixup_list);
57static DEFINE_MUTEX(phy_fixup_lock);
58
59
60
61
62
63
64
65
66
67
68int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
69 int (*run)(struct phy_device *))
70{
71 struct phy_fixup *fixup;
72
73 fixup = kzalloc(sizeof(struct phy_fixup), GFP_KERNEL);
74 if (!fixup)
75 return -ENOMEM;
76
77 strncpy(fixup->bus_id, bus_id, BUS_ID_SIZE);
78 fixup->phy_uid = phy_uid;
79 fixup->phy_uid_mask = phy_uid_mask;
80 fixup->run = run;
81
82 mutex_lock(&phy_fixup_lock);
83 list_add_tail(&fixup->list, &phy_fixup_list);
84 mutex_unlock(&phy_fixup_lock);
85
86 return 0;
87}
88EXPORT_SYMBOL(phy_register_fixup);
89
90
91int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
92 int (*run)(struct phy_device *))
93{
94 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
95}
96EXPORT_SYMBOL(phy_register_fixup_for_uid);
97
98
99int phy_register_fixup_for_id(const char *bus_id,
100 int (*run)(struct phy_device *))
101{
102 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
103}
104EXPORT_SYMBOL(phy_register_fixup_for_id);
105
106
107
108
109
110static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
111{
112 if (strcmp(fixup->bus_id, phydev->dev.bus_id) != 0)
113 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
114 return 0;
115
116 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
117 (phydev->phy_id & fixup->phy_uid_mask))
118 if (fixup->phy_uid != PHY_ANY_UID)
119 return 0;
120
121 return 1;
122}
123
124
125int phy_scan_fixups(struct phy_device *phydev)
126{
127 struct phy_fixup *fixup;
128
129 mutex_lock(&phy_fixup_lock);
130 list_for_each_entry(fixup, &phy_fixup_list, list) {
131 if (phy_needs_fixup(phydev, fixup)) {
132 int err;
133
134 err = fixup->run(phydev);
135
136 if (err < 0)
137 return err;
138 }
139 }
140 mutex_unlock(&phy_fixup_lock);
141
142 return 0;
143}
144EXPORT_SYMBOL(phy_scan_fixups);
145
146struct phy_device* phy_device_create(struct mii_bus *bus, int addr, int phy_id)
147{
148 struct phy_device *dev;
149
150
151 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
152
153 if (NULL == dev)
154 return (struct phy_device*) PTR_ERR((void*)-ENOMEM);
155
156 dev->dev.release = phy_device_release;
157
158 dev->speed = 0;
159 dev->duplex = -1;
160 dev->pause = dev->asym_pause = 0;
161 dev->link = 1;
162 dev->interface = PHY_INTERFACE_MODE_GMII;
163
164 dev->autoneg = AUTONEG_ENABLE;
165
166 dev->addr = addr;
167 dev->phy_id = phy_id;
168 dev->bus = bus;
169
170 dev->state = PHY_DOWN;
171
172 mutex_init(&dev->lock);
173
174 return dev;
175}
176EXPORT_SYMBOL(phy_device_create);
177
178
179
180
181
182
183
184
185
186
187int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id)
188{
189 int phy_reg;
190
191
192
193 phy_reg = bus->read(bus, addr, MII_PHYSID1);
194
195 if (phy_reg < 0)
196 return -EIO;
197
198 *phy_id = (phy_reg & 0xffff) << 16;
199
200
201 phy_reg = bus->read(bus, addr, MII_PHYSID2);
202
203 if (phy_reg < 0)
204 return -EIO;
205
206 *phy_id |= (phy_reg & 0xffff);
207
208 return 0;
209}
210EXPORT_SYMBOL(get_phy_id);
211
212
213
214
215
216
217
218
219
220struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
221{
222 struct phy_device *dev = NULL;
223 u32 phy_id;
224 int r;
225
226 r = get_phy_id(bus, addr, &phy_id);
227 if (r)
228 return ERR_PTR(r);
229
230
231 if ((phy_id & 0x1fffffff) == 0x1fffffff)
232 return NULL;
233
234
235
236
237
238
239
240 if (phy_id == 0)
241 return NULL;
242
243 dev = phy_device_create(bus, addr, phy_id);
244
245 return dev;
246}
247
248
249
250
251
252
253
254
255
256
257
258
259
260void phy_prepare_link(struct phy_device *phydev,
261 void (*handler)(struct net_device *))
262{
263 phydev->adjust_link = handler;
264}
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
283 void (*handler)(struct net_device *), u32 flags,
284 phy_interface_t interface)
285{
286 struct phy_device *phydev;
287
288 phydev = phy_attach(dev, bus_id, flags, interface);
289
290 if (IS_ERR(phydev))
291 return phydev;
292
293 phy_prepare_link(phydev, handler);
294
295 phy_start_machine(phydev, NULL);
296
297 if (phydev->irq > 0)
298 phy_start_interrupts(phydev);
299
300 return phydev;
301}
302EXPORT_SYMBOL(phy_connect);
303
304
305
306
307
308void phy_disconnect(struct phy_device *phydev)
309{
310 if (phydev->irq > 0)
311 phy_stop_interrupts(phydev);
312
313 phy_stop_machine(phydev);
314
315 phydev->adjust_link = NULL;
316
317 phy_detach(phydev);
318}
319EXPORT_SYMBOL(phy_disconnect);
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335struct phy_device *phy_attach(struct net_device *dev,
336 const char *bus_id, u32 flags, phy_interface_t interface)
337{
338 struct bus_type *bus = &mdio_bus_type;
339 struct phy_device *phydev;
340 struct device *d;
341
342
343
344 d = bus_find_device_by_name(bus, NULL, bus_id);
345 if (d) {
346 phydev = to_phy_device(d);
347 } else {
348 printk(KERN_ERR "%s not found\n", bus_id);
349 return ERR_PTR(-ENODEV);
350 }
351
352
353
354 if (NULL == d->driver) {
355 int err;
356 d->driver = &genphy_driver.driver;
357
358 err = d->driver->probe(d);
359 if (err >= 0)
360 err = device_bind_driver(d);
361
362 if (err)
363 return ERR_PTR(err);
364 }
365
366 if (phydev->attached_dev) {
367 printk(KERN_ERR "%s: %s already attached\n",
368 dev->name, bus_id);
369 return ERR_PTR(-EBUSY);
370 }
371
372 phydev->attached_dev = dev;
373
374 phydev->dev_flags = flags;
375
376 phydev->interface = interface;
377
378
379
380
381 if (phydev->drv->config_init) {
382 int err;
383
384 err = phy_scan_fixups(phydev);
385
386 if (err < 0)
387 return ERR_PTR(err);
388
389 err = phydev->drv->config_init(phydev);
390
391 if (err < 0)
392 return ERR_PTR(err);
393 }
394
395 return phydev;
396}
397EXPORT_SYMBOL(phy_attach);
398
399
400
401
402
403void phy_detach(struct phy_device *phydev)
404{
405 phydev->attached_dev = NULL;
406
407
408
409
410
411 if (phydev->dev.driver == &genphy_driver.driver)
412 device_release_driver(&phydev->dev);
413}
414EXPORT_SYMBOL(phy_detach);
415
416
417
418
419
420
421
422
423
424
425
426
427
428int genphy_config_advert(struct phy_device *phydev)
429{
430 u32 advertise;
431 int oldadv, adv;
432 int err, changed = 0;
433
434
435
436 phydev->advertising &= phydev->supported;
437 advertise = phydev->advertising;
438
439
440 oldadv = adv = phy_read(phydev, MII_ADVERTISE);
441
442 if (adv < 0)
443 return adv;
444
445 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
446 ADVERTISE_PAUSE_ASYM);
447 if (advertise & ADVERTISED_10baseT_Half)
448 adv |= ADVERTISE_10HALF;
449 if (advertise & ADVERTISED_10baseT_Full)
450 adv |= ADVERTISE_10FULL;
451 if (advertise & ADVERTISED_100baseT_Half)
452 adv |= ADVERTISE_100HALF;
453 if (advertise & ADVERTISED_100baseT_Full)
454 adv |= ADVERTISE_100FULL;
455 if (advertise & ADVERTISED_Pause)
456 adv |= ADVERTISE_PAUSE_CAP;
457 if (advertise & ADVERTISED_Asym_Pause)
458 adv |= ADVERTISE_PAUSE_ASYM;
459
460 if (adv != oldadv) {
461 err = phy_write(phydev, MII_ADVERTISE, adv);
462
463 if (err < 0)
464 return err;
465 changed = 1;
466 }
467
468
469 if (phydev->supported & (SUPPORTED_1000baseT_Half |
470 SUPPORTED_1000baseT_Full)) {
471 oldadv = adv = phy_read(phydev, MII_CTRL1000);
472
473 if (adv < 0)
474 return adv;
475
476 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
477 if (advertise & SUPPORTED_1000baseT_Half)
478 adv |= ADVERTISE_1000HALF;
479 if (advertise & SUPPORTED_1000baseT_Full)
480 adv |= ADVERTISE_1000FULL;
481
482 if (adv != oldadv) {
483 err = phy_write(phydev, MII_CTRL1000, adv);
484
485 if (err < 0)
486 return err;
487 changed = 1;
488 }
489 }
490
491 return changed;
492}
493EXPORT_SYMBOL(genphy_config_advert);
494
495
496
497
498
499
500
501
502
503int genphy_setup_forced(struct phy_device *phydev)
504{
505 int err;
506 int ctl = 0;
507
508 phydev->pause = phydev->asym_pause = 0;
509
510 if (SPEED_1000 == phydev->speed)
511 ctl |= BMCR_SPEED1000;
512 else if (SPEED_100 == phydev->speed)
513 ctl |= BMCR_SPEED100;
514
515 if (DUPLEX_FULL == phydev->duplex)
516 ctl |= BMCR_FULLDPLX;
517
518 err = phy_write(phydev, MII_BMCR, ctl);
519
520 if (err < 0)
521 return err;
522
523
524
525
526
527 err = phy_scan_fixups(phydev);
528
529 if (err < 0)
530 return err;
531
532
533
534 if (phydev->drv->config_init)
535 err = phydev->drv->config_init(phydev);
536
537 return err;
538}
539
540
541
542
543
544
545int genphy_restart_aneg(struct phy_device *phydev)
546{
547 int ctl;
548
549 ctl = phy_read(phydev, MII_BMCR);
550
551 if (ctl < 0)
552 return ctl;
553
554 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
555
556
557 ctl &= ~(BMCR_ISOLATE);
558
559 ctl = phy_write(phydev, MII_BMCR, ctl);
560
561 return ctl;
562}
563EXPORT_SYMBOL(genphy_restart_aneg);
564
565
566
567
568
569
570
571
572
573
574int genphy_config_aneg(struct phy_device *phydev)
575{
576 int result;
577
578 if (AUTONEG_ENABLE != phydev->autoneg)
579 return genphy_setup_forced(phydev);
580
581 result = genphy_config_advert(phydev);
582
583 if (result < 0)
584 return result;
585
586 if (result == 0) {
587
588
589 int ctl = phy_read(phydev, MII_BMCR);
590
591 if (ctl < 0)
592 return ctl;
593
594 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
595 result = 1;
596 }
597
598
599
600 if (result > 0)
601 result = genphy_restart_aneg(phydev);
602
603 return result;
604}
605EXPORT_SYMBOL(genphy_config_aneg);
606
607
608
609
610
611
612
613
614
615int genphy_update_link(struct phy_device *phydev)
616{
617 int status;
618
619
620 status = phy_read(phydev, MII_BMSR);
621
622 if (status < 0)
623 return status;
624
625
626 status = phy_read(phydev, MII_BMSR);
627
628 if (status < 0)
629 return status;
630
631 if ((status & BMSR_LSTATUS) == 0)
632 phydev->link = 0;
633 else
634 phydev->link = 1;
635
636 return 0;
637}
638EXPORT_SYMBOL(genphy_update_link);
639
640
641
642
643
644
645
646
647
648
649int genphy_read_status(struct phy_device *phydev)
650{
651 int adv;
652 int err;
653 int lpa;
654 int lpagb = 0;
655
656
657
658 err = genphy_update_link(phydev);
659 if (err)
660 return err;
661
662 if (AUTONEG_ENABLE == phydev->autoneg) {
663 if (phydev->supported & (SUPPORTED_1000baseT_Half
664 | SUPPORTED_1000baseT_Full)) {
665 lpagb = phy_read(phydev, MII_STAT1000);
666
667 if (lpagb < 0)
668 return lpagb;
669
670 adv = phy_read(phydev, MII_CTRL1000);
671
672 if (adv < 0)
673 return adv;
674
675 lpagb &= adv << 2;
676 }
677
678 lpa = phy_read(phydev, MII_LPA);
679
680 if (lpa < 0)
681 return lpa;
682
683 adv = phy_read(phydev, MII_ADVERTISE);
684
685 if (adv < 0)
686 return adv;
687
688 lpa &= adv;
689
690 phydev->speed = SPEED_10;
691 phydev->duplex = DUPLEX_HALF;
692 phydev->pause = phydev->asym_pause = 0;
693
694 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
695 phydev->speed = SPEED_1000;
696
697 if (lpagb & LPA_1000FULL)
698 phydev->duplex = DUPLEX_FULL;
699 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
700 phydev->speed = SPEED_100;
701
702 if (lpa & LPA_100FULL)
703 phydev->duplex = DUPLEX_FULL;
704 } else
705 if (lpa & LPA_10FULL)
706 phydev->duplex = DUPLEX_FULL;
707
708 if (phydev->duplex == DUPLEX_FULL){
709 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
710 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
711 }
712 } else {
713 int bmcr = phy_read(phydev, MII_BMCR);
714 if (bmcr < 0)
715 return bmcr;
716
717 if (bmcr & BMCR_FULLDPLX)
718 phydev->duplex = DUPLEX_FULL;
719 else
720 phydev->duplex = DUPLEX_HALF;
721
722 if (bmcr & BMCR_SPEED1000)
723 phydev->speed = SPEED_1000;
724 else if (bmcr & BMCR_SPEED100)
725 phydev->speed = SPEED_100;
726 else
727 phydev->speed = SPEED_10;
728
729 phydev->pause = phydev->asym_pause = 0;
730 }
731
732 return 0;
733}
734EXPORT_SYMBOL(genphy_read_status);
735
736static int genphy_config_init(struct phy_device *phydev)
737{
738 int val;
739 u32 features;
740
741
742
743 features = (SUPPORTED_TP | SUPPORTED_MII
744 | SUPPORTED_AUI | SUPPORTED_FIBRE |
745 SUPPORTED_BNC);
746
747
748 val = phy_read(phydev, MII_BMSR);
749
750 if (val < 0)
751 return val;
752
753 if (val & BMSR_ANEGCAPABLE)
754 features |= SUPPORTED_Autoneg;
755
756 if (val & BMSR_100FULL)
757 features |= SUPPORTED_100baseT_Full;
758 if (val & BMSR_100HALF)
759 features |= SUPPORTED_100baseT_Half;
760 if (val & BMSR_10FULL)
761 features |= SUPPORTED_10baseT_Full;
762 if (val & BMSR_10HALF)
763 features |= SUPPORTED_10baseT_Half;
764
765 if (val & BMSR_ESTATEN) {
766 val = phy_read(phydev, MII_ESTATUS);
767
768 if (val < 0)
769 return val;
770
771 if (val & ESTATUS_1000_TFULL)
772 features |= SUPPORTED_1000baseT_Full;
773 if (val & ESTATUS_1000_THALF)
774 features |= SUPPORTED_1000baseT_Half;
775 }
776
777 phydev->supported = features;
778 phydev->advertising = features;
779
780 return 0;
781}
782
783
784
785
786
787
788
789
790
791
792static int phy_probe(struct device *dev)
793{
794 struct phy_device *phydev;
795 struct phy_driver *phydrv;
796 struct device_driver *drv;
797 int err = 0;
798
799 phydev = to_phy_device(dev);
800
801
802
803 drv = get_driver(phydev->dev.driver);
804 phydrv = to_phy_driver(drv);
805 phydev->drv = phydrv;
806
807
808 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
809 phydev->irq = PHY_POLL;
810
811 mutex_lock(&phydev->lock);
812
813
814
815
816 phydev->supported = phydrv->features;
817 phydev->advertising = phydrv->features;
818
819
820 phydev->state = PHY_READY;
821
822 if (phydev->drv->probe)
823 err = phydev->drv->probe(phydev);
824
825 mutex_unlock(&phydev->lock);
826
827 return err;
828
829}
830
831static int phy_remove(struct device *dev)
832{
833 struct phy_device *phydev;
834
835 phydev = to_phy_device(dev);
836
837 mutex_lock(&phydev->lock);
838 phydev->state = PHY_DOWN;
839 mutex_unlock(&phydev->lock);
840
841 if (phydev->drv->remove)
842 phydev->drv->remove(phydev);
843
844 put_driver(dev->driver);
845 phydev->drv = NULL;
846
847 return 0;
848}
849
850
851
852
853
854int phy_driver_register(struct phy_driver *new_driver)
855{
856 int retval;
857
858 memset(&new_driver->driver, 0, sizeof(new_driver->driver));
859 new_driver->driver.name = new_driver->name;
860 new_driver->driver.bus = &mdio_bus_type;
861 new_driver->driver.probe = phy_probe;
862 new_driver->driver.remove = phy_remove;
863
864 retval = driver_register(&new_driver->driver);
865
866 if (retval) {
867 printk(KERN_ERR "%s: Error %d in registering driver\n",
868 new_driver->name, retval);
869
870 return retval;
871 }
872
873 pr_debug("%s: Registered new driver\n", new_driver->name);
874
875 return 0;
876}
877EXPORT_SYMBOL(phy_driver_register);
878
879void phy_driver_unregister(struct phy_driver *drv)
880{
881 driver_unregister(&drv->driver);
882}
883EXPORT_SYMBOL(phy_driver_unregister);
884
885static struct phy_driver genphy_driver = {
886 .phy_id = 0xffffffff,
887 .phy_id_mask = 0xffffffff,
888 .name = "Generic PHY",
889 .config_init = genphy_config_init,
890 .features = 0,
891 .config_aneg = genphy_config_aneg,
892 .read_status = genphy_read_status,
893 .driver = {.owner= THIS_MODULE, },
894};
895
896static int __init phy_init(void)
897{
898 int rc;
899
900 rc = mdio_bus_init();
901 if (rc)
902 return rc;
903
904 rc = phy_driver_register(&genphy_driver);
905 if (rc)
906 mdio_bus_exit();
907
908 return rc;
909}
910
911static void __exit phy_exit(void)
912{
913 phy_driver_unregister(&genphy_driver);
914 mdio_bus_exit();
915}
916
917subsys_initcall(phy_init);
918module_exit(phy_exit);