1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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#include <linux/mdio.h>
34#include <linux/io.h>
35#include <linux/uaccess.h>
36#include <linux/of.h>
37
38#include <asm/irq.h>
39
40MODULE_DESCRIPTION("PHY library");
41MODULE_AUTHOR("Andy Fleming");
42MODULE_LICENSE("GPL");
43
44void phy_device_free(struct phy_device *phydev)
45{
46 put_device(&phydev->mdio_dev);
47}
48EXPORT_SYMBOL(phy_device_free);
49
50static void phy_device_release(struct device *dev)
51{
52 kfree(to_phy_device(dev));
53}
54
55enum genphy_driver {
56 GENPHY_DRV_1G,
57 GENPHY_DRV_10G,
58 GENPHY_DRV_MAX
59};
60
61static struct phy_driver genphy_driver[GENPHY_DRV_MAX];
62
63static LIST_HEAD(phy_fixup_list);
64static DEFINE_MUTEX(phy_fixup_lock);
65
66#ifdef CONFIG_PM
67static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
68{
69 struct device_driver *drv = phydev->mdio_dev.driver;
70 struct phy_driver *phydrv = to_phy_driver(drv);
71 struct net_device *netdev = phydev->attached_dev;
72
73 if (!drv || !phydrv->suspend)
74 return false;
75
76
77
78
79
80
81 if (!netdev)
82 return !phydev->suspended;
83
84
85
86
87 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
88 return false;
89
90
91
92
93
94 if (device_may_wakeup(&netdev->dev))
95 return false;
96
97 return true;
98}
99
100static int mdio_bus_phy_suspend(struct device *dev)
101{
102 struct phy_device *phydev = to_phy_device(dev);
103
104
105
106
107
108
109 if (phydev->attached_dev && phydev->adjust_link)
110 phy_stop_machine(phydev);
111
112 if (!mdio_bus_phy_may_suspend(phydev))
113 return 0;
114
115 return phy_suspend(phydev);
116}
117
118static int mdio_bus_phy_resume(struct device *dev)
119{
120 struct phy_device *phydev = to_phy_device(dev);
121 int ret;
122
123 if (!mdio_bus_phy_may_suspend(phydev))
124 goto no_resume;
125
126 ret = phy_resume(phydev);
127 if (ret < 0)
128 return ret;
129
130no_resume:
131 if (phydev->attached_dev && phydev->adjust_link)
132 phy_start_machine(phydev);
133
134 return 0;
135}
136
137static int mdio_bus_phy_restore(struct device *dev)
138{
139 struct phy_device *phydev = to_phy_device(dev);
140 struct net_device *netdev = phydev->attached_dev;
141 int ret;
142
143 if (!netdev)
144 return 0;
145
146 ret = phy_init_hw(phydev);
147 if (ret < 0)
148 return ret;
149
150 if (phydev->attached_dev && phydev->adjust_link)
151 phy_start_machine(phydev);
152
153 return 0;
154}
155
156static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
157 .suspend = mdio_bus_phy_suspend,
158 .resume = mdio_bus_phy_resume,
159 .freeze = mdio_bus_phy_suspend,
160 .thaw = mdio_bus_phy_resume,
161 .restore = mdio_bus_phy_restore,
162};
163
164#define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
165
166#else
167
168#define MDIO_BUS_PHY_PM_OPS NULL
169
170#endif
171
172
173
174
175
176
177
178
179
180
181int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
182 int (*run)(struct phy_device *))
183{
184 struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
185
186 if (!fixup)
187 return -ENOMEM;
188
189 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
190 fixup->phy_uid = phy_uid;
191 fixup->phy_uid_mask = phy_uid_mask;
192 fixup->run = run;
193
194 mutex_lock(&phy_fixup_lock);
195 list_add_tail(&fixup->list, &phy_fixup_list);
196 mutex_unlock(&phy_fixup_lock);
197
198 return 0;
199}
200EXPORT_SYMBOL(phy_register_fixup);
201
202
203int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
204 int (*run)(struct phy_device *))
205{
206 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
207}
208EXPORT_SYMBOL(phy_register_fixup_for_uid);
209
210
211int phy_register_fixup_for_id(const char *bus_id,
212 int (*run)(struct phy_device *))
213{
214 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
215}
216EXPORT_SYMBOL(phy_register_fixup_for_id);
217
218
219
220
221static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
222{
223 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
224 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
225 return 0;
226
227 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
228 (phydev->phy_id & fixup->phy_uid_mask))
229 if (fixup->phy_uid != PHY_ANY_UID)
230 return 0;
231
232 return 1;
233}
234
235
236static int phy_scan_fixups(struct phy_device *phydev)
237{
238 struct phy_fixup *fixup;
239
240 mutex_lock(&phy_fixup_lock);
241 list_for_each_entry(fixup, &phy_fixup_list, list) {
242 if (phy_needs_fixup(phydev, fixup)) {
243 int err = fixup->run(phydev);
244
245 if (err < 0) {
246 mutex_unlock(&phy_fixup_lock);
247 return err;
248 }
249 }
250 }
251 mutex_unlock(&phy_fixup_lock);
252
253 return 0;
254}
255
256struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
257 bool is_c45,
258 struct phy_c45_device_ids *c45_ids)
259{
260 struct phy_device *dev;
261
262
263 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
264 if (NULL == dev)
265 return (struct phy_device *)PTR_ERR((void *)-ENOMEM);
266
267 dev->mdio_dev.release = phy_device_release;
268 dev->mdio_dev.parent = &bus->dev;
269 dev->mdio_dev.bus = &mdio_bus_type;
270 dev->mdio_bus = bus;
271 dev->mdio_pm_ops = MDIO_BUS_PHY_PM_OPS;
272 dev->mdio_addr = addr;
273 dev->mdio_flags = MDIO_DEVICE_FLAG_PHY;
274
275 dev->speed = 0;
276 dev->duplex = -1;
277 dev->pause = 0;
278 dev->asym_pause = 0;
279 dev->link = 1;
280 dev->interface = PHY_INTERFACE_MODE_GMII;
281
282 dev->autoneg = AUTONEG_ENABLE;
283
284 dev->is_c45 = is_c45;
285 dev->phy_id = phy_id;
286 if (c45_ids)
287 dev->c45_ids = *c45_ids;
288 dev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
289 dev_set_name(&dev->mdio_dev, PHY_ID_FMT, bus->id, addr);
290
291 dev->state = PHY_DOWN;
292
293 mutex_init(&dev->lock);
294 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
295 INIT_WORK(&dev->phy_queue, phy_change);
296
297
298
299
300
301
302
303
304
305
306
307 request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
308
309 device_initialize(&dev->mdio_dev);
310
311 return dev;
312}
313EXPORT_SYMBOL(phy_device_create);
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
329 struct phy_c45_device_ids *c45_ids) {
330 int phy_reg;
331 int i, reg_addr;
332 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
333
334
335
336
337 for (i = 1;
338 i < num_ids && c45_ids->devices_in_package == 0;
339 i++) {
340 reg_addr = MII_ADDR_C45 | i << 16 | 6;
341 phy_reg = mdiobus_read(bus, addr, reg_addr);
342 if (phy_reg < 0)
343 return -EIO;
344 c45_ids->devices_in_package = (phy_reg & 0xffff) << 16;
345
346 reg_addr = MII_ADDR_C45 | i << 16 | 5;
347 phy_reg = mdiobus_read(bus, addr, reg_addr);
348 if (phy_reg < 0)
349 return -EIO;
350 c45_ids->devices_in_package |= (phy_reg & 0xffff);
351
352
353
354
355 if ((c45_ids->devices_in_package & 0x1fffffff) == 0x1fffffff) {
356 *phy_id = 0xffffffff;
357 return 0;
358 }
359 }
360
361
362 for (i = 1; i < num_ids; i++) {
363 if (!(c45_ids->devices_in_package & (1 << i)))
364 continue;
365
366 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
367 phy_reg = mdiobus_read(bus, addr, reg_addr);
368 if (phy_reg < 0)
369 return -EIO;
370 c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16;
371
372 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
373 phy_reg = mdiobus_read(bus, addr, reg_addr);
374 if (phy_reg < 0)
375 return -EIO;
376 c45_ids->device_ids[i] |= (phy_reg & 0xffff);
377 }
378 *phy_id = 0;
379 return 0;
380}
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
399 bool is_c45, struct phy_c45_device_ids *c45_ids)
400{
401 int phy_reg;
402
403 if (is_c45)
404 return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
405
406
407 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
408 if (phy_reg < 0) {
409
410
411
412 if (phy_reg == -EIO || phy_reg == -ENODEV) {
413 *phy_id = 0xffffffff;
414 return 0;
415 }
416
417 return -EIO;
418 }
419
420 *phy_id = (phy_reg & 0xffff) << 16;
421
422
423 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
424 if (phy_reg < 0)
425 return -EIO;
426
427 *phy_id |= (phy_reg & 0xffff);
428
429 return 0;
430}
431
432
433
434
435
436
437
438
439
440
441
442struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
443{
444 struct phy_c45_device_ids c45_ids = {0};
445 u32 phy_id = 0;
446 int r;
447
448 r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
449 if (r)
450 return ERR_PTR(r);
451
452
453 if ((phy_id & 0x1fffffff) == 0x1fffffff)
454 return ERR_PTR(-ENODEV);
455
456 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
457}
458EXPORT_SYMBOL(get_phy_device);
459
460
461
462
463
464int phy_device_register(struct phy_device *phydev)
465{
466 int err;
467
468 err = mdiobus_register_device(phydev);
469 if (err)
470 return err;
471
472
473 err = phy_scan_fixups(phydev);
474 if (err) {
475 pr_err("PHY %d failed to initialize\n", phydev->mdio_addr);
476 goto out;
477 }
478
479 err = device_add(&phydev->mdio_dev);
480 if (err) {
481 pr_err("PHY %d failed to add\n", phydev->mdio_addr);
482 goto out;
483 }
484
485 return 0;
486
487 out:
488 mdiobus_unregister_device(phydev);
489 return err;
490}
491EXPORT_SYMBOL(phy_device_register);
492
493
494
495
496
497
498
499
500
501void phy_device_remove(struct phy_device *phydev)
502{
503 device_del(&phydev->mdio_dev);
504 mdiobus_unregister_device(phydev);
505}
506EXPORT_SYMBOL(phy_device_remove);
507
508
509
510
511
512struct phy_device *phy_find_first(struct mii_bus *bus)
513{
514 struct phy_device *phydev;
515 int addr;
516
517 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
518 phydev = mdiobus_get_phy(bus, addr);
519 if (phydev)
520 return phydev;
521 }
522 return NULL;
523}
524EXPORT_SYMBOL(phy_find_first);
525
526
527
528
529
530
531
532
533
534
535
536
537
538static void phy_prepare_link(struct phy_device *phydev,
539 void (*handler)(struct net_device *))
540{
541 phydev->adjust_link = handler;
542}
543
544
545
546
547
548
549
550
551int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
552 void (*handler)(struct net_device *),
553 phy_interface_t interface)
554{
555 int rc;
556
557 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
558 if (rc)
559 return rc;
560
561 phy_prepare_link(phydev, handler);
562 phy_start_machine(phydev);
563 if (phydev->irq > 0)
564 phy_start_interrupts(phydev);
565
566 return 0;
567}
568EXPORT_SYMBOL(phy_connect_direct);
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
586 void (*handler)(struct net_device *),
587 phy_interface_t interface)
588{
589 struct phy_device *phydev;
590 struct device *d;
591 int rc;
592
593
594
595
596 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
597 if (!d) {
598 pr_err("PHY %s not found\n", bus_id);
599 return ERR_PTR(-ENODEV);
600 }
601 phydev = to_phy_device(d);
602
603 rc = phy_connect_direct(dev, phydev, handler, interface);
604 if (rc)
605 return ERR_PTR(rc);
606
607 return phydev;
608}
609EXPORT_SYMBOL(phy_connect);
610
611
612
613
614
615
616void phy_disconnect(struct phy_device *phydev)
617{
618 if (phydev->irq > 0)
619 phy_stop_interrupts(phydev);
620
621 phy_stop_machine(phydev);
622
623 phydev->adjust_link = NULL;
624
625 phy_detach(phydev);
626}
627EXPORT_SYMBOL(phy_disconnect);
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647static int phy_poll_reset(struct phy_device *phydev)
648{
649
650 unsigned int retries = 12;
651 int ret;
652
653 do {
654 msleep(50);
655 ret = phy_read(phydev, MII_BMCR);
656 if (ret < 0)
657 return ret;
658 } while (ret & BMCR_RESET && --retries);
659 if (ret & BMCR_RESET)
660 return -ETIMEDOUT;
661
662
663
664
665 msleep(1);
666 return 0;
667}
668
669int phy_init_hw(struct phy_device *phydev)
670{
671 int ret;
672
673 if (!phydev->drv || !phydev->drv->config_init)
674 return 0;
675
676 ret = genphy_soft_reset(phydev);
677 if (ret < 0)
678 return ret;
679
680 ret = phy_scan_fixups(phydev);
681 if (ret < 0)
682 return ret;
683
684 return phydev->drv->config_init(phydev);
685}
686EXPORT_SYMBOL(phy_init_hw);
687
688void phy_attached_info(struct phy_device *phydev)
689{
690 phy_attached_print(phydev, NULL);
691}
692EXPORT_SYMBOL(phy_attached_info);
693
694#define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)"
695void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
696{
697 if (!fmt) {
698 dev_info(&phydev->mdio_dev, ATTACHED_FMT "\n",
699 phydev->drv->name, phydev_name(phydev),
700 phydev->irq);
701 } else {
702 va_list ap;
703
704 dev_info(&phydev->mdio_dev, ATTACHED_FMT,
705 phydev->drv->name, phydev_name(phydev),
706 phydev->irq);
707
708 va_start(ap, fmt);
709 vprintk(fmt, ap);
710 va_end(ap);
711 }
712}
713EXPORT_SYMBOL(phy_attached_print);
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
731 u32 flags, phy_interface_t interface)
732{
733 struct module *ndev_owner = dev->dev.parent->driver->owner;
734 struct mii_bus *bus = phydev->mdio_bus;
735 struct device *d = &phydev->mdio_dev;
736 int err;
737
738
739
740
741
742
743 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
744 dev_err(&dev->dev, "failed to get the bus module\n");
745 return -EIO;
746 }
747
748 get_device(d);
749
750
751
752
753 if (NULL == d->driver) {
754 if (phydev->is_c45)
755 d->driver = &genphy_driver[GENPHY_DRV_10G].driver;
756 else
757 d->driver = &genphy_driver[GENPHY_DRV_1G].driver;
758
759 err = d->driver->probe(d);
760 if (err >= 0)
761 err = device_bind_driver(d);
762
763 if (err)
764 goto error;
765 }
766
767 if (phydev->attached_dev) {
768 dev_err(&dev->dev, "PHY already attached\n");
769 err = -EBUSY;
770 goto error;
771 }
772
773 phydev->attached_dev = dev;
774 dev->phydev = phydev;
775
776 phydev->dev_flags = flags;
777
778 phydev->interface = interface;
779
780 phydev->state = PHY_READY;
781
782
783
784
785 netif_carrier_off(phydev->attached_dev);
786
787
788
789
790
791 err = phy_init_hw(phydev);
792 if (err)
793 phy_detach(phydev);
794
795 phy_resume(phydev);
796
797 return err;
798
799error:
800 put_device(d);
801 if (ndev_owner != bus->owner)
802 module_put(bus->owner);
803 return err;
804}
805EXPORT_SYMBOL(phy_attach_direct);
806
807
808
809
810
811
812
813
814
815
816struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
817 phy_interface_t interface)
818{
819 struct bus_type *bus = &mdio_bus_type;
820 struct phy_device *phydev;
821 struct device *d;
822 int rc;
823
824
825
826
827 d = bus_find_device_by_name(bus, NULL, bus_id);
828 if (!d) {
829 pr_err("PHY %s not found\n", bus_id);
830 return ERR_PTR(-ENODEV);
831 }
832 phydev = to_phy_device(d);
833
834 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
835 if (rc)
836 return ERR_PTR(rc);
837
838 return phydev;
839}
840EXPORT_SYMBOL(phy_attach);
841
842
843
844
845
846
847
848
849void phy_detach(struct phy_device *phydev)
850{
851 struct net_device *dev = phydev->attached_dev;
852 struct module *ndev_owner = dev->dev.parent->driver->owner;
853 struct mii_bus *bus;
854 int i;
855
856 phydev->attached_dev->phydev = NULL;
857 phydev->attached_dev = NULL;
858 phy_suspend(phydev);
859
860
861
862
863
864
865 for (i = 0; i < ARRAY_SIZE(genphy_driver); i++) {
866 if (phydev->mdio_dev.driver == &genphy_driver[i].driver) {
867 device_release_driver(&phydev->mdio_dev);
868 break;
869 }
870 }
871
872
873
874
875
876 bus = phydev->mdio_bus;
877
878 put_device(&phydev->mdio_dev);
879 if (ndev_owner != bus->owner)
880 module_put(bus->owner);
881}
882EXPORT_SYMBOL(phy_detach);
883
884int phy_suspend(struct phy_device *phydev)
885{
886 struct phy_driver *phydrv = to_phy_driver(phydev->mdio_dev.driver);
887 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
888 int ret = 0;
889
890
891 phy_ethtool_get_wol(phydev, &wol);
892 if (wol.wolopts)
893 return -EBUSY;
894
895 if (phydev->drv && phydrv->suspend)
896 ret = phydrv->suspend(phydev);
897
898 if (ret)
899 return ret;
900
901 phydev->suspended = true;
902
903 return ret;
904}
905EXPORT_SYMBOL(phy_suspend);
906
907int __phy_resume(struct phy_device *phydev)
908{
909 struct phy_driver *phydrv = to_phy_driver(phydev->mdio_dev.driver);
910 int ret = 0;
911
912 WARN_ON(!mutex_is_locked(&phydev->lock));
913
914 if (phydev->drv && phydrv->resume)
915 ret = phydrv->resume(phydev);
916
917 if (ret)
918 return ret;
919
920 phydev->suspended = false;
921
922 return ret;
923}
924EXPORT_SYMBOL(__phy_resume);
925
926int phy_resume(struct phy_device *phydev)
927{
928 int ret;
929
930 mutex_lock(&phydev->lock);
931 ret = __phy_resume(phydev);
932 mutex_unlock(&phydev->lock);
933
934 return ret;
935}
936EXPORT_SYMBOL(phy_resume);
937
938
939
940
941
942
943
944
945
946
947
948
949static int genphy_config_advert(struct phy_device *phydev)
950{
951 u32 advertise;
952 int oldadv, adv;
953 int err, changed = 0;
954
955
956 phydev->advertising &= phydev->supported;
957 advertise = phydev->advertising;
958
959
960 adv = phy_read(phydev, MII_ADVERTISE);
961 if (adv < 0)
962 return adv;
963
964 oldadv = adv;
965 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
966 ADVERTISE_PAUSE_ASYM);
967 adv |= ethtool_adv_to_mii_adv_t(advertise);
968
969 if (adv != oldadv) {
970 err = phy_write(phydev, MII_ADVERTISE, adv);
971
972 if (err < 0)
973 return err;
974 changed = 1;
975 }
976
977
978 if (phydev->supported & (SUPPORTED_1000baseT_Half |
979 SUPPORTED_1000baseT_Full)) {
980 adv = phy_read(phydev, MII_CTRL1000);
981 if (adv < 0)
982 return adv;
983
984 oldadv = adv;
985 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
986 adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
987
988 if (adv != oldadv) {
989 err = phy_write(phydev, MII_CTRL1000, adv);
990
991 if (err < 0)
992 return err;
993 changed = 1;
994 }
995 }
996
997 return changed;
998}
999
1000
1001
1002
1003
1004
1005
1006
1007
1008static int genphy_config_eee_advert(struct phy_device *phydev)
1009{
1010 int err;
1011
1012
1013 if (!phydev->eee_broken_modes)
1014 return 0;
1015
1016 err = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
1017 phydev->eee_broken_modes, 0);
1018
1019 return err < 0 ? 0 : err;
1020}
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030static int genphy_setup_forced(struct phy_device *phydev)
1031{
1032 u16 ctl = 0;
1033
1034 phydev->pause = 0;
1035 phydev->asym_pause = 0;
1036
1037 if (SPEED_1000 == phydev->speed)
1038 ctl |= BMCR_SPEED1000;
1039 else if (SPEED_100 == phydev->speed)
1040 ctl |= BMCR_SPEED100;
1041
1042 if (DUPLEX_FULL == phydev->duplex)
1043 ctl |= BMCR_FULLDPLX;
1044
1045 return phy_modify(phydev, MII_BMCR,
1046 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
1047}
1048
1049
1050
1051
1052
1053
1054int genphy_restart_aneg(struct phy_device *phydev)
1055{
1056
1057 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
1058 BMCR_ANENABLE | BMCR_ANRESTART);
1059}
1060EXPORT_SYMBOL(genphy_restart_aneg);
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070int genphy_config_aneg(struct phy_device *phydev)
1071{
1072 int err, changed;
1073
1074 changed = genphy_config_eee_advert(phydev);
1075
1076 if (AUTONEG_ENABLE != phydev->autoneg)
1077 return genphy_setup_forced(phydev);
1078
1079 err = genphy_config_advert(phydev);
1080 if (err < 0)
1081 return err;
1082
1083 changed |= err;
1084
1085 if (changed == 0) {
1086
1087
1088
1089 int ctl = phy_read(phydev, MII_BMCR);
1090
1091 if (ctl < 0)
1092 return ctl;
1093
1094 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
1095 changed = 1;
1096 }
1097
1098
1099
1100
1101 if (changed > 0)
1102 return genphy_restart_aneg(phydev);
1103
1104 return 0;
1105}
1106EXPORT_SYMBOL(genphy_config_aneg);
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116int genphy_aneg_done(struct phy_device *phydev)
1117{
1118 int retval = phy_read(phydev, MII_BMSR);
1119
1120 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1121}
1122EXPORT_SYMBOL(genphy_aneg_done);
1123
1124static int gen10g_config_aneg(struct phy_device *phydev)
1125{
1126 return 0;
1127}
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137int genphy_update_link(struct phy_device *phydev)
1138{
1139 int status;
1140
1141
1142 status = phy_read(phydev, MII_BMSR);
1143 if (status < 0)
1144 return status;
1145
1146
1147 status = phy_read(phydev, MII_BMSR);
1148 if (status < 0)
1149 return status;
1150
1151 if ((status & BMSR_LSTATUS) == 0)
1152 phydev->link = 0;
1153 else
1154 phydev->link = 1;
1155
1156 return 0;
1157}
1158EXPORT_SYMBOL(genphy_update_link);
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169int genphy_read_status(struct phy_device *phydev)
1170{
1171 int adv;
1172 int err;
1173 int lpa;
1174 int lpagb = 0;
1175
1176
1177 err = genphy_update_link(phydev);
1178 if (err)
1179 return err;
1180
1181 phydev->lp_advertising = 0;
1182
1183 if (AUTONEG_ENABLE == phydev->autoneg) {
1184 if (phydev->supported & (SUPPORTED_1000baseT_Half
1185 | SUPPORTED_1000baseT_Full)) {
1186 lpagb = phy_read(phydev, MII_STAT1000);
1187 if (lpagb < 0)
1188 return lpagb;
1189
1190 adv = phy_read(phydev, MII_CTRL1000);
1191 if (adv < 0)
1192 return adv;
1193
1194 phydev->lp_advertising =
1195 mii_stat1000_to_ethtool_lpa_t(lpagb);
1196 lpagb &= adv << 2;
1197 }
1198
1199 lpa = phy_read(phydev, MII_LPA);
1200 if (lpa < 0)
1201 return lpa;
1202
1203 phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
1204
1205 adv = phy_read(phydev, MII_ADVERTISE);
1206 if (adv < 0)
1207 return adv;
1208
1209 lpa &= adv;
1210
1211 phydev->speed = SPEED_10;
1212 phydev->duplex = DUPLEX_HALF;
1213 phydev->pause = 0;
1214 phydev->asym_pause = 0;
1215
1216 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
1217 phydev->speed = SPEED_1000;
1218
1219 if (lpagb & LPA_1000FULL)
1220 phydev->duplex = DUPLEX_FULL;
1221 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
1222 phydev->speed = SPEED_100;
1223
1224 if (lpa & LPA_100FULL)
1225 phydev->duplex = DUPLEX_FULL;
1226 } else
1227 if (lpa & LPA_10FULL)
1228 phydev->duplex = DUPLEX_FULL;
1229
1230 if (phydev->duplex == DUPLEX_FULL) {
1231 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
1232 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
1233 }
1234 } else {
1235 int bmcr = phy_read(phydev, MII_BMCR);
1236
1237 if (bmcr < 0)
1238 return bmcr;
1239
1240 if (bmcr & BMCR_FULLDPLX)
1241 phydev->duplex = DUPLEX_FULL;
1242 else
1243 phydev->duplex = DUPLEX_HALF;
1244
1245 if (bmcr & BMCR_SPEED1000)
1246 phydev->speed = SPEED_1000;
1247 else if (bmcr & BMCR_SPEED100)
1248 phydev->speed = SPEED_100;
1249 else
1250 phydev->speed = SPEED_10;
1251
1252 phydev->pause = 0;
1253 phydev->asym_pause = 0;
1254 }
1255
1256 return 0;
1257}
1258EXPORT_SYMBOL(genphy_read_status);
1259
1260static int gen10g_read_status(struct phy_device *phydev)
1261{
1262 u32 mmd_mask = phydev->c45_ids.devices_in_package;
1263 int ret;
1264
1265
1266 phydev->speed = SPEED_10000;
1267 phydev->duplex = DUPLEX_FULL;
1268
1269
1270 mmd_mask &= ~(BIT(MDIO_MMD_VEND1) | BIT(MDIO_MMD_VEND2));
1271
1272 ret = genphy_c45_read_link(phydev, mmd_mask);
1273
1274 phydev->link = ret > 0 ? 1 : 0;
1275
1276 return 0;
1277}
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288int genphy_soft_reset(struct phy_device *phydev)
1289{
1290 int ret;
1291
1292 ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
1293 if (ret < 0)
1294 return ret;
1295
1296 return phy_poll_reset(phydev);
1297}
1298EXPORT_SYMBOL(genphy_soft_reset);
1299
1300int genphy_config_init(struct phy_device *phydev)
1301{
1302 int val;
1303 u32 features;
1304
1305
1306
1307
1308 features = (SUPPORTED_TP | SUPPORTED_MII
1309 | SUPPORTED_AUI | SUPPORTED_FIBRE |
1310 SUPPORTED_BNC);
1311
1312
1313 val = phy_read(phydev, MII_BMSR);
1314 if (val < 0)
1315 return val;
1316
1317 if (val & BMSR_ANEGCAPABLE)
1318 features |= SUPPORTED_Autoneg;
1319
1320 if (val & BMSR_100FULL)
1321 features |= SUPPORTED_100baseT_Full;
1322 if (val & BMSR_100HALF)
1323 features |= SUPPORTED_100baseT_Half;
1324 if (val & BMSR_10FULL)
1325 features |= SUPPORTED_10baseT_Full;
1326 if (val & BMSR_10HALF)
1327 features |= SUPPORTED_10baseT_Half;
1328
1329 if (val & BMSR_ESTATEN) {
1330 val = phy_read(phydev, MII_ESTATUS);
1331 if (val < 0)
1332 return val;
1333
1334 if (val & ESTATUS_1000_TFULL)
1335 features |= SUPPORTED_1000baseT_Full;
1336 if (val & ESTATUS_1000_THALF)
1337 features |= SUPPORTED_1000baseT_Half;
1338 }
1339
1340 phydev->supported = features;
1341 phydev->advertising = features;
1342
1343 return 0;
1344}
1345EXPORT_SYMBOL(genphy_config_init);
1346
1347static int gen10g_config_init(struct phy_device *phydev)
1348{
1349
1350 phydev->supported = SUPPORTED_10000baseT_Full;
1351 phydev->advertising = SUPPORTED_10000baseT_Full;
1352
1353 return 0;
1354}
1355
1356int genphy_suspend(struct phy_device *phydev)
1357{
1358 return phy_modify(phydev, MII_BMCR, 0, BMCR_PDOWN);
1359}
1360EXPORT_SYMBOL(genphy_suspend);
1361
1362static int gen10g_suspend(struct phy_device *phydev)
1363{
1364 return 0;
1365}
1366
1367int genphy_resume(struct phy_device *phydev)
1368{
1369 return phy_modify(phydev, MII_BMCR, BMCR_PDOWN, 0);
1370}
1371EXPORT_SYMBOL(genphy_resume);
1372
1373static int gen10g_resume(struct phy_device *phydev)
1374{
1375 return 0;
1376}
1377
1378static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
1379{
1380 phydev->supported &= ~(PHY_1000BT_FEATURES | PHY_100BT_FEATURES |
1381 PHY_10BT_FEATURES);
1382
1383 switch (max_speed) {
1384 default:
1385 return -ENOTSUPP;
1386 case SPEED_1000:
1387 phydev->supported |= PHY_1000BT_FEATURES;
1388
1389 case SPEED_100:
1390 phydev->supported |= PHY_100BT_FEATURES;
1391
1392 case SPEED_10:
1393 phydev->supported |= PHY_10BT_FEATURES;
1394 }
1395
1396 return 0;
1397}
1398
1399int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
1400{
1401 int err;
1402
1403 err = __set_phy_supported(phydev, max_speed);
1404 if (err)
1405 return err;
1406
1407 phydev->advertising = phydev->supported;
1408
1409 return 0;
1410}
1411EXPORT_SYMBOL(phy_set_max_speed);
1412
1413static void of_set_phy_eee_broken(struct phy_device *phydev)
1414{
1415 struct device_node *node = phydev->mdio_dev.of_node;
1416 u32 broken;
1417
1418 if (!IS_ENABLED(CONFIG_OF_MDIO))
1419 return;
1420
1421 if (!node)
1422 return;
1423
1424 if (!of_property_read_u32(node, "eee-broken-modes", &broken))
1425 phydev->eee_broken_modes = broken;
1426}
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436static int phy_probe(struct device *dev)
1437{
1438 struct phy_device *phydev = to_phy_device(dev);
1439 struct device_driver *drv = phydev->mdio_dev.driver;
1440 struct phy_driver *phydrv = to_phy_driver(drv);
1441 int err = 0;
1442
1443 phydev->drv = phydrv;
1444
1445
1446
1447
1448 if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
1449 phy_interrupt_is_valid(phydev))
1450 phydev->irq = PHY_POLL;
1451
1452 if (phydrv->flags & PHY_IS_INTERNAL)
1453 phydev->is_internal = true;
1454
1455 mutex_lock(&phydev->lock);
1456
1457
1458
1459
1460
1461 phydev->supported = phydrv->features;
1462 phydev->advertising = phydrv->features;
1463
1464
1465
1466
1467 of_set_phy_eee_broken(phydev);
1468
1469
1470 phydev->state = PHY_READY;
1471
1472 if (phydev->drv->probe)
1473 err = phydev->drv->probe(phydev);
1474
1475 mutex_unlock(&phydev->lock);
1476
1477 return err;
1478}
1479
1480static int phy_remove(struct device *dev)
1481{
1482 struct phy_device *phydev = to_phy_device(dev);
1483
1484 cancel_delayed_work_sync(&phydev->state_queue);
1485
1486 mutex_lock(&phydev->lock);
1487 phydev->state = PHY_DOWN;
1488 mutex_unlock(&phydev->lock);
1489
1490 if (phydev->drv && phydev->drv->remove)
1491 phydev->drv->remove(phydev);
1492 phydev->drv = NULL;
1493
1494 return 0;
1495}
1496
1497
1498
1499
1500
1501int phy_driver_register(struct phy_driver *new_driver)
1502{
1503 int retval;
1504
1505 new_driver->driver.name = new_driver->name;
1506 new_driver->driver.bus = &mdio_bus_type;
1507 new_driver->driver.probe = phy_probe;
1508 new_driver->driver.remove = phy_remove;
1509
1510 retval = driver_register(&new_driver->driver);
1511 if (retval) {
1512 pr_err("%s: Error %d in registering driver\n",
1513 new_driver->name, retval);
1514
1515 return retval;
1516 }
1517
1518 pr_debug("%s: Registered new driver\n", new_driver->name);
1519
1520 return 0;
1521}
1522EXPORT_SYMBOL(phy_driver_register);
1523
1524int phy_drivers_register(struct phy_driver *new_driver, int n)
1525{
1526 int i, ret = 0;
1527
1528 for (i = 0; i < n; i++) {
1529 ret = phy_driver_register(new_driver + i);
1530 if (ret) {
1531 while (i-- > 0)
1532 phy_driver_unregister(new_driver + i);
1533 break;
1534 }
1535 }
1536 return ret;
1537}
1538EXPORT_SYMBOL(phy_drivers_register);
1539
1540void phy_driver_unregister(struct phy_driver *drv)
1541{
1542 driver_unregister(&drv->driver);
1543}
1544EXPORT_SYMBOL(phy_driver_unregister);
1545
1546void phy_drivers_unregister(struct phy_driver *drv, int n)
1547{
1548 int i;
1549
1550 for (i = 0; i < n; i++)
1551 phy_driver_unregister(drv + i);
1552}
1553EXPORT_SYMBOL(phy_drivers_unregister);
1554
1555static struct phy_driver genphy_driver[] = {
1556{
1557 .phy_id = 0xffffffff,
1558 .phy_id_mask = 0xffffffff,
1559 .name = "Generic PHY",
1560 .config_init = genphy_config_init,
1561 .features = 0,
1562 .config_aneg = genphy_config_aneg,
1563 .aneg_done = genphy_aneg_done,
1564 .read_status = genphy_read_status,
1565 .suspend = genphy_suspend,
1566 .resume = genphy_resume,
1567 .driver = { .owner = THIS_MODULE, },
1568}, {
1569 .phy_id = 0xffffffff,
1570 .phy_id_mask = 0xffffffff,
1571 .name = "Generic 10G PHY",
1572 .config_init = gen10g_config_init,
1573 .features = 0,
1574 .config_aneg = gen10g_config_aneg,
1575 .read_status = gen10g_read_status,
1576 .suspend = gen10g_suspend,
1577 .resume = gen10g_resume,
1578 .driver = {.owner = THIS_MODULE, },
1579} };
1580
1581static int __init phy_init(void)
1582{
1583 int rc;
1584
1585 rc = mdio_bus_init();
1586 if (rc)
1587 return rc;
1588
1589 rc = phy_drivers_register(genphy_driver,
1590 ARRAY_SIZE(genphy_driver));
1591 if (rc)
1592 mdio_bus_exit();
1593
1594 return rc;
1595}
1596
1597static void __exit phy_exit(void)
1598{
1599 phy_drivers_unregister(genphy_driver,
1600 ARRAY_SIZE(genphy_driver));
1601 mdio_bus_exit();
1602}
1603
1604subsys_initcall(phy_init);
1605module_exit(phy_exit);
1606