Prepare v2023.10
[platform/kernel/u-boot.git] / drivers / net / phy / phy.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Generic PHY Management code
4  *
5  * Copyright 2011 Freescale Semiconductor, Inc.
6  * author Andy Fleming
7  *
8  * Based loosely off of Linux's PHY Lib
9  */
10 #include <common.h>
11 #include <console.h>
12 #include <dm.h>
13 #include <log.h>
14 #include <malloc.h>
15 #include <net.h>
16 #include <command.h>
17 #include <miiphy.h>
18 #include <phy.h>
19 #include <errno.h>
20 #include <asm/global_data.h>
21 #include <dm/of_extra.h>
22 #include <linux/bitops.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/compiler.h>
26
27 DECLARE_GLOBAL_DATA_PTR;
28
29 /* Generic PHY support and helper functions */
30
31 /**
32  * genphy_config_advert - sanitize and advertise auto-negotiation parameters
33  * @phydev: target phy_device struct
34  *
35  * Description: Writes MII_ADVERTISE with the appropriate values,
36  *   after sanitizing the values to make sure we only advertise
37  *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
38  *   hasn't changed, and > 0 if it has changed.
39  */
40 static int genphy_config_advert(struct phy_device *phydev)
41 {
42         u32 advertise;
43         int oldadv, adv, bmsr;
44         int err, changed = 0;
45
46         /* Only allow advertising what this PHY supports */
47         phydev->advertising &= phydev->supported;
48         advertise = phydev->advertising;
49
50         /* Setup standard advertisement */
51         adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
52         oldadv = adv;
53
54         if (adv < 0)
55                 return adv;
56
57         adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
58                  ADVERTISE_PAUSE_ASYM);
59         if (advertise & ADVERTISED_10baseT_Half)
60                 adv |= ADVERTISE_10HALF;
61         if (advertise & ADVERTISED_10baseT_Full)
62                 adv |= ADVERTISE_10FULL;
63         if (advertise & ADVERTISED_100baseT_Half)
64                 adv |= ADVERTISE_100HALF;
65         if (advertise & ADVERTISED_100baseT_Full)
66                 adv |= ADVERTISE_100FULL;
67         if (advertise & ADVERTISED_Pause)
68                 adv |= ADVERTISE_PAUSE_CAP;
69         if (advertise & ADVERTISED_Asym_Pause)
70                 adv |= ADVERTISE_PAUSE_ASYM;
71         if (advertise & ADVERTISED_1000baseX_Half)
72                 adv |= ADVERTISE_1000XHALF;
73         if (advertise & ADVERTISED_1000baseX_Full)
74                 adv |= ADVERTISE_1000XFULL;
75
76         if (adv != oldadv) {
77                 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
78
79                 if (err < 0)
80                         return err;
81                 changed = 1;
82         }
83
84         bmsr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
85         if (bmsr < 0)
86                 return bmsr;
87
88         /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
89          * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
90          * logical 1.
91          */
92         if (!(bmsr & BMSR_ESTATEN))
93                 return changed;
94
95         /* Configure gigabit if it's supported */
96         adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
97         oldadv = adv;
98
99         if (adv < 0)
100                 return adv;
101
102         adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
103
104         if (phydev->supported & (SUPPORTED_1000baseT_Half |
105                                 SUPPORTED_1000baseT_Full)) {
106                 if (advertise & SUPPORTED_1000baseT_Half)
107                         adv |= ADVERTISE_1000HALF;
108                 if (advertise & SUPPORTED_1000baseT_Full)
109                         adv |= ADVERTISE_1000FULL;
110         }
111
112         if (adv != oldadv)
113                 changed = 1;
114
115         err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, adv);
116         if (err < 0)
117                 return err;
118
119         return changed;
120 }
121
122 /**
123  * genphy_setup_forced - configures/forces speed/duplex from @phydev
124  * @phydev: target phy_device struct
125  *
126  * Description: Configures MII_BMCR to force speed/duplex
127  *   to the values in phydev. Assumes that the values are valid.
128  */
129 static int genphy_setup_forced(struct phy_device *phydev)
130 {
131         int err;
132         int ctl = BMCR_ANRESTART;
133
134         phydev->pause = 0;
135         phydev->asym_pause = 0;
136
137         if (phydev->speed == SPEED_1000)
138                 ctl |= BMCR_SPEED1000;
139         else if (phydev->speed == SPEED_100)
140                 ctl |= BMCR_SPEED100;
141
142         if (phydev->duplex == DUPLEX_FULL)
143                 ctl |= BMCR_FULLDPLX;
144
145         err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
146
147         return err;
148 }
149
150 /**
151  * genphy_restart_aneg - Enable and Restart Autonegotiation
152  * @phydev: target phy_device struct
153  */
154 int genphy_restart_aneg(struct phy_device *phydev)
155 {
156         int ctl;
157
158         ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
159
160         if (ctl < 0)
161                 return ctl;
162
163         ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
164
165         /* Don't isolate the PHY if we're negotiating */
166         ctl &= ~(BMCR_ISOLATE);
167
168         ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
169
170         return ctl;
171 }
172
173 /**
174  * genphy_config_aneg - restart auto-negotiation or write BMCR
175  * @phydev: target phy_device struct
176  *
177  * Description: If auto-negotiation is enabled, we configure the
178  *   advertising, and then restart auto-negotiation.  If it is not
179  *   enabled, then we write the BMCR.
180  */
181 int genphy_config_aneg(struct phy_device *phydev)
182 {
183         int result;
184
185         if (phydev->autoneg != AUTONEG_ENABLE)
186                 return genphy_setup_forced(phydev);
187
188         result = genphy_config_advert(phydev);
189
190         if (result < 0) /* error */
191                 return result;
192
193         if (result == 0) {
194                 /*
195                  * Advertisment hasn't changed, but maybe aneg was never on to
196                  * begin with?  Or maybe phy was isolated?
197                  */
198                 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
199
200                 if (ctl < 0)
201                         return ctl;
202
203                 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
204                         result = 1; /* do restart aneg */
205         }
206
207         /*
208          * Only restart aneg if we are advertising something different
209          * than we were before.
210          */
211         if (result > 0)
212                 result = genphy_restart_aneg(phydev);
213
214         return result;
215 }
216
217 /**
218  * genphy_update_link - update link status in @phydev
219  * @phydev: target phy_device struct
220  *
221  * Description: Update the value in phydev->link to reflect the
222  *   current link value.  In order to do this, we need to read
223  *   the status register twice, keeping the second value.
224  */
225 int genphy_update_link(struct phy_device *phydev)
226 {
227         unsigned int mii_reg;
228
229         /*
230          * Wait if the link is up, and autonegotiation is in progress
231          * (ie - we're capable and it's not done)
232          */
233         mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
234
235         /*
236          * If we already saw the link up, and it hasn't gone down, then
237          * we don't need to wait for autoneg again
238          */
239         if (phydev->link && mii_reg & BMSR_LSTATUS)
240                 return 0;
241
242         if ((phydev->autoneg == AUTONEG_ENABLE) &&
243             !(mii_reg & BMSR_ANEGCOMPLETE)) {
244                 int i = 0;
245
246                 printf("%s Waiting for PHY auto negotiation to complete",
247                        phydev->dev->name);
248                 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
249                         /*
250                          * Timeout reached ?
251                          */
252                         if (i > (PHY_ANEG_TIMEOUT / 50)) {
253                                 printf(" TIMEOUT !\n");
254                                 phydev->link = 0;
255                                 return -ETIMEDOUT;
256                         }
257
258                         if (ctrlc()) {
259                                 puts("user interrupt!\n");
260                                 phydev->link = 0;
261                                 return -EINTR;
262                         }
263
264                         if ((i++ % 10) == 0)
265                                 printf(".");
266
267                         mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
268                         mdelay(50);     /* 50 ms */
269                 }
270                 printf(" done\n");
271                 phydev->link = 1;
272         } else {
273                 /* Read the link a second time to clear the latched state */
274                 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
275
276                 if (mii_reg & BMSR_LSTATUS)
277                         phydev->link = 1;
278                 else
279                         phydev->link = 0;
280         }
281
282         return 0;
283 }
284
285 /*
286  * Generic function which updates the speed and duplex.  If
287  * autonegotiation is enabled, it uses the AND of the link
288  * partner's advertised capabilities and our advertised
289  * capabilities.  If autonegotiation is disabled, we use the
290  * appropriate bits in the control register.
291  *
292  * Stolen from Linux's mii.c and phy_device.c
293  */
294 int genphy_parse_link(struct phy_device *phydev)
295 {
296         int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
297
298         /* We're using autonegotiation */
299         if (phydev->autoneg == AUTONEG_ENABLE) {
300                 u32 lpa = 0;
301                 int gblpa = 0;
302                 u32 estatus = 0;
303
304                 /* Check for gigabit capability */
305                 if (phydev->supported & (SUPPORTED_1000baseT_Full |
306                                         SUPPORTED_1000baseT_Half)) {
307                         /* We want a list of states supported by
308                          * both PHYs in the link
309                          */
310                         gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
311                         if (gblpa < 0) {
312                                 debug("Could not read MII_STAT1000. ");
313                                 debug("Ignoring gigabit capability\n");
314                                 gblpa = 0;
315                         }
316                         gblpa &= phy_read(phydev,
317                                         MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
318                 }
319
320                 /* Set the baseline so we only have to set them
321                  * if they're different
322                  */
323                 phydev->speed = SPEED_10;
324                 phydev->duplex = DUPLEX_HALF;
325
326                 /* Check the gigabit fields */
327                 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
328                         phydev->speed = SPEED_1000;
329
330                         if (gblpa & PHY_1000BTSR_1000FD)
331                                 phydev->duplex = DUPLEX_FULL;
332
333                         /* We're done! */
334                         return 0;
335                 }
336
337                 lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
338                 lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
339
340                 if (lpa & (LPA_100FULL | LPA_100HALF)) {
341                         phydev->speed = SPEED_100;
342
343                         if (lpa & LPA_100FULL)
344                                 phydev->duplex = DUPLEX_FULL;
345
346                 } else if (lpa & LPA_10FULL) {
347                         phydev->duplex = DUPLEX_FULL;
348                 }
349
350                 /*
351                  * Extended status may indicate that the PHY supports
352                  * 1000BASE-T/X even though the 1000BASE-T registers
353                  * are missing. In this case we can't tell whether the
354                  * peer also supports it, so we only check extended
355                  * status if the 1000BASE-T registers are actually
356                  * missing.
357                  */
358                 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
359                         estatus = phy_read(phydev, MDIO_DEVAD_NONE,
360                                            MII_ESTATUS);
361
362                 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
363                                 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
364                         phydev->speed = SPEED_1000;
365                         if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
366                                 phydev->duplex = DUPLEX_FULL;
367                 }
368
369         } else {
370                 u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
371
372                 phydev->speed = SPEED_10;
373                 phydev->duplex = DUPLEX_HALF;
374
375                 if (bmcr & BMCR_FULLDPLX)
376                         phydev->duplex = DUPLEX_FULL;
377
378                 if (bmcr & BMCR_SPEED1000)
379                         phydev->speed = SPEED_1000;
380                 else if (bmcr & BMCR_SPEED100)
381                         phydev->speed = SPEED_100;
382         }
383
384         return 0;
385 }
386
387 int genphy_config(struct phy_device *phydev)
388 {
389         int val;
390         u32 features;
391
392         features = (SUPPORTED_TP | SUPPORTED_MII
393                         | SUPPORTED_AUI | SUPPORTED_FIBRE |
394                         SUPPORTED_BNC);
395
396         /* Do we support autonegotiation? */
397         val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
398
399         if (val < 0)
400                 return val;
401
402         if (val & BMSR_ANEGCAPABLE)
403                 features |= SUPPORTED_Autoneg;
404
405         if (val & BMSR_100FULL)
406                 features |= SUPPORTED_100baseT_Full;
407         if (val & BMSR_100HALF)
408                 features |= SUPPORTED_100baseT_Half;
409         if (val & BMSR_10FULL)
410                 features |= SUPPORTED_10baseT_Full;
411         if (val & BMSR_10HALF)
412                 features |= SUPPORTED_10baseT_Half;
413
414         if (val & BMSR_ESTATEN) {
415                 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
416
417                 if (val < 0)
418                         return val;
419
420                 if (val & ESTATUS_1000_TFULL)
421                         features |= SUPPORTED_1000baseT_Full;
422                 if (val & ESTATUS_1000_THALF)
423                         features |= SUPPORTED_1000baseT_Half;
424                 if (val & ESTATUS_1000_XFULL)
425                         features |= SUPPORTED_1000baseX_Full;
426                 if (val & ESTATUS_1000_XHALF)
427                         features |= SUPPORTED_1000baseX_Half;
428         }
429
430         phydev->supported &= features;
431         phydev->advertising &= features;
432
433         genphy_config_aneg(phydev);
434
435         return 0;
436 }
437
438 int genphy_startup(struct phy_device *phydev)
439 {
440         int ret;
441
442         ret = genphy_update_link(phydev);
443         if (ret)
444                 return ret;
445
446         return genphy_parse_link(phydev);
447 }
448
449 int genphy_shutdown(struct phy_device *phydev)
450 {
451         return 0;
452 }
453
454 U_BOOT_PHY_DRIVER(genphy) = {
455         .uid            = 0xffffffff,
456         .mask           = 0xffffffff,
457         .name           = "Generic PHY",
458         .features       = PHY_GBIT_FEATURES | SUPPORTED_MII |
459                           SUPPORTED_AUI | SUPPORTED_FIBRE |
460                           SUPPORTED_BNC,
461         .config         = genphy_config,
462         .startup        = genphy_startup,
463         .shutdown       = genphy_shutdown,
464 };
465
466 #ifdef CONFIG_NEEDS_MANUAL_RELOC
467 int phy_init(void)
468 {
469         const int ll_n_ents = ll_entry_count(struct phy_driver, phy_driver);
470         struct phy_driver *drv, *ll_entry;
471
472         /* Perform manual relocation on linker list based PHY drivers */
473         ll_entry = ll_entry_start(struct phy_driver, phy_driver);
474         for (drv = ll_entry; drv != ll_entry + ll_n_ents; drv++) {
475                 if (drv->probe)
476                         drv->probe += gd->reloc_off;
477                 if (drv->config)
478                         drv->config += gd->reloc_off;
479                 if (drv->startup)
480                         drv->startup += gd->reloc_off;
481                 if (drv->shutdown)
482                         drv->shutdown += gd->reloc_off;
483                 if (drv->readext)
484                         drv->readext += gd->reloc_off;
485                 if (drv->writeext)
486                         drv->writeext += gd->reloc_off;
487                 if (drv->read_mmd)
488                         drv->read_mmd += gd->reloc_off;
489                 if (drv->write_mmd)
490                         drv->write_mmd += gd->reloc_off;
491         }
492
493         return 0;
494 }
495 #endif
496
497 int phy_set_supported(struct phy_device *phydev, u32 max_speed)
498 {
499         /* The default values for phydev->supported are provided by the PHY
500          * driver "features" member, we want to reset to sane defaults first
501          * before supporting higher speeds.
502          */
503         phydev->supported &= PHY_DEFAULT_FEATURES;
504
505         switch (max_speed) {
506         default:
507                 return -ENOTSUPP;
508         case SPEED_1000:
509                 phydev->supported |= PHY_1000BT_FEATURES;
510                 /* fall through */
511         case SPEED_100:
512                 phydev->supported |= PHY_100BT_FEATURES;
513                 /* fall through */
514         case SPEED_10:
515                 phydev->supported |= PHY_10BT_FEATURES;
516         }
517
518         return 0;
519 }
520
521 static int phy_probe(struct phy_device *phydev)
522 {
523         int err = 0;
524
525         phydev->advertising = phydev->drv->features;
526         phydev->supported = phydev->drv->features;
527
528         phydev->mmds = phydev->drv->mmds;
529
530         if (phydev->drv->probe)
531                 err = phydev->drv->probe(phydev);
532
533         return err;
534 }
535
536 static struct phy_driver *generic_for_phy(struct phy_device *phydev)
537 {
538 #ifdef CONFIG_PHYLIB_10G
539         if (phydev->is_c45)
540                 return ll_entry_get(struct phy_driver, gen10g, phy_driver);
541 #endif
542
543         return ll_entry_get(struct phy_driver, genphy, phy_driver);
544 }
545
546 static struct phy_driver *get_phy_driver(struct phy_device *phydev)
547 {
548         const int ll_n_ents = ll_entry_count(struct phy_driver, phy_driver);
549         int phy_id = phydev->phy_id;
550         struct phy_driver *ll_entry;
551         struct phy_driver *drv;
552
553         ll_entry = ll_entry_start(struct phy_driver, phy_driver);
554         for (drv = ll_entry; drv != ll_entry + ll_n_ents; drv++)
555                 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
556                         return drv;
557
558         /* If we made it here, there's no driver for this PHY */
559         return generic_for_phy(phydev);
560 }
561
562 struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
563                                      u32 phy_id, bool is_c45)
564 {
565         struct phy_device *dev;
566
567         /*
568          * We allocate the device, and initialize the
569          * default values
570          */
571         dev = malloc(sizeof(*dev));
572         if (!dev) {
573                 printf("Failed to allocate PHY device for %s:%d\n",
574                        bus ? bus->name : "(null bus)", addr);
575                 return NULL;
576         }
577
578         memset(dev, 0, sizeof(*dev));
579
580         dev->duplex = -1;
581         dev->link = 0;
582         dev->interface = PHY_INTERFACE_MODE_NA;
583
584         dev->node = ofnode_null();
585
586         dev->autoneg = AUTONEG_ENABLE;
587
588         dev->addr = addr;
589         dev->phy_id = phy_id;
590         dev->is_c45 = is_c45;
591         dev->bus = bus;
592
593         dev->drv = get_phy_driver(dev);
594
595         if (phy_probe(dev)) {
596                 printf("%s, PHY probe failed\n", __func__);
597                 return NULL;
598         }
599
600         if (addr >= 0 && addr < PHY_MAX_ADDR && phy_id != PHY_FIXED_ID)
601                 bus->phymap[addr] = dev;
602
603         return dev;
604 }
605
606 /**
607  * get_phy_id - reads the specified addr for its ID.
608  * @bus: the target MII bus
609  * @addr: PHY address on the MII bus
610  * @phy_id: where to store the ID retrieved.
611  *
612  * Description: Reads the ID registers of the PHY at @addr on the
613  *   @bus, stores it in @phy_id and returns zero on success.
614  */
615 int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
616 {
617         int phy_reg;
618
619         /*
620          * Grab the bits from PHYIR1, and put them
621          * in the upper half
622          */
623         phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
624
625         if (phy_reg < 0)
626                 return -EIO;
627
628         *phy_id = (phy_reg & 0xffff) << 16;
629
630         /* Grab the bits from PHYIR2, and put them in the lower half */
631         phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
632
633         if (phy_reg < 0)
634                 return -EIO;
635
636         *phy_id |= (phy_reg & 0xffff);
637
638         return 0;
639 }
640
641 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
642                                              uint phy_mask, int devad)
643 {
644         u32 phy_id = 0xffffffff;
645         bool is_c45;
646
647         while (phy_mask) {
648                 int addr = ffs(phy_mask) - 1;
649                 int r = get_phy_id(bus, addr, devad, &phy_id);
650
651                 /*
652                  * If the PHY ID is flat 0 we ignore it.  There are C45 PHYs
653                  * that return all 0s for C22 reads (like Aquantia AQR112) and
654                  * there are C22 PHYs that return all 0s for C45 reads (like
655                  * Atheros AR8035).
656                  */
657                 if (r == 0 && phy_id == 0)
658                         goto next;
659
660                 /* If the PHY ID is mostly f's, we didn't find anything */
661                 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff) {
662                         is_c45 = (devad == MDIO_DEVAD_NONE) ? false : true;
663                         return phy_device_create(bus, addr, phy_id, is_c45);
664                 }
665 next:
666                 phy_mask &= ~(1 << addr);
667         }
668         return NULL;
669 }
670
671 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
672                                                   uint phy_mask)
673 {
674         /* If we have one, return the existing device, with new interface */
675         while (phy_mask) {
676                 int addr = ffs(phy_mask) - 1;
677
678                 if (bus->phymap[addr])
679                         return bus->phymap[addr];
680
681                 phy_mask &= ~(1 << addr);
682         }
683         return NULL;
684 }
685
686 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
687                                                  uint phy_mask)
688 {
689         struct phy_device *phydev;
690         int devad[] = {
691                 /* Clause-22 */
692                 MDIO_DEVAD_NONE,
693                 /* Clause-45 */
694                 MDIO_MMD_PMAPMD,
695                 MDIO_MMD_WIS,
696                 MDIO_MMD_PCS,
697                 MDIO_MMD_PHYXS,
698                 MDIO_MMD_VEND1,
699         };
700         int i, devad_cnt;
701
702         devad_cnt = sizeof(devad)/sizeof(int);
703         phydev = search_for_existing_phy(bus, phy_mask);
704         if (phydev)
705                 return phydev;
706         /* try different access clauses  */
707         for (i = 0; i < devad_cnt; i++) {
708                 phydev = create_phy_by_mask(bus, phy_mask, devad[i]);
709                 if (IS_ERR(phydev))
710                         return NULL;
711                 if (phydev)
712                         return phydev;
713         }
714
715         debug("\n%s PHY: ", bus->name);
716         while (phy_mask) {
717                 int addr = ffs(phy_mask) - 1;
718
719                 debug("%d ", addr);
720                 phy_mask &= ~(1 << addr);
721         }
722         debug("not found\n");
723
724         return NULL;
725 }
726
727 /**
728  * get_phy_device - reads the specified PHY device and returns its
729  *                  @phy_device struct
730  * @bus: the target MII bus
731  * @addr: PHY address on the MII bus
732  *
733  * Description: Reads the ID registers of the PHY at @addr on the
734  *   @bus, then allocates and returns the phy_device to represent it.
735  */
736 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr)
737 {
738         return get_phy_device_by_mask(bus, 1 << addr);
739 }
740
741 int phy_reset(struct phy_device *phydev)
742 {
743         int reg;
744         int timeout = 500;
745         int devad = MDIO_DEVAD_NONE;
746
747         if (phydev->flags & PHY_FLAG_BROKEN_RESET)
748                 return 0;
749
750 #ifdef CONFIG_PHYLIB_10G
751         /* If it's 10G, we need to issue reset through one of the MMDs */
752         if (phydev->is_c45) {
753                 if (!phydev->mmds)
754                         gen10g_discover_mmds(phydev);
755
756                 devad = ffs(phydev->mmds) - 1;
757         }
758 #endif
759
760         if (phy_write(phydev, devad, MII_BMCR, BMCR_RESET) < 0) {
761                 debug("PHY reset failed\n");
762                 return -1;
763         }
764
765 #if CONFIG_PHY_RESET_DELAY > 0
766         udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
767 #endif
768         /*
769          * Poll the control register for the reset bit to go to 0 (it is
770          * auto-clearing).  This should happen within 0.5 seconds per the
771          * IEEE spec.
772          */
773         reg = phy_read(phydev, devad, MII_BMCR);
774         while ((reg & BMCR_RESET) && timeout--) {
775                 reg = phy_read(phydev, devad, MII_BMCR);
776
777                 if (reg < 0) {
778                         debug("PHY status read failed\n");
779                         return -1;
780                 }
781                 udelay(1000);
782         }
783
784         if (reg & BMCR_RESET) {
785                 puts("PHY reset timed out\n");
786                 return -1;
787         }
788
789         return 0;
790 }
791
792 int miiphy_reset(const char *devname, unsigned char addr)
793 {
794         struct mii_dev *bus = miiphy_get_dev_by_name(devname);
795         struct phy_device *phydev;
796
797         phydev = get_phy_device(bus, addr);
798
799         return phy_reset(phydev);
800 }
801
802 struct phy_device *phy_find_by_mask(struct mii_dev *bus, uint phy_mask)
803 {
804         /* Reset the bus */
805         if (bus->reset) {
806                 bus->reset(bus);
807
808                 /* Wait 15ms to make sure the PHY has come out of hard reset */
809                 mdelay(15);
810         }
811
812         return get_phy_device_by_mask(bus, phy_mask);
813 }
814
815 static void phy_connect_dev(struct phy_device *phydev, struct udevice *dev,
816                             phy_interface_t interface)
817 {
818         /* Soft Reset the PHY */
819         phy_reset(phydev);
820         if (phydev->dev && phydev->dev != dev) {
821                 printf("%s:%d is connected to %s.  Reconnecting to %s\n",
822                        phydev->bus->name, phydev->addr,
823                        phydev->dev->name, dev->name);
824         }
825         phydev->dev = dev;
826         phydev->interface = interface;
827         debug("%s connected to %s mode %s\n", dev->name, phydev->drv->name,
828               phy_string_for_interface(interface));
829 }
830
831 #ifdef CONFIG_PHY_XILINX_GMII2RGMII
832 static struct phy_device *phy_connect_gmii2rgmii(struct mii_dev *bus,
833                                                  struct udevice *dev)
834 {
835         struct phy_device *phydev = NULL;
836         ofnode node;
837
838         ofnode_for_each_subnode(node, dev_ofnode(dev)) {
839                 node = ofnode_by_compatible(node, "xlnx,gmii-to-rgmii-1.0");
840                 if (ofnode_valid(node)) {
841                         phydev = phy_device_create(bus, 0,
842                                                    PHY_GMII2RGMII_ID, false);
843                         if (phydev)
844                                 phydev->node = node;
845                         break;
846                 }
847
848                 node = ofnode_first_subnode(node);
849         }
850
851         return phydev;
852 }
853 #endif
854
855 #ifdef CONFIG_PHY_FIXED
856 /**
857  * fixed_phy_create() - create an unconnected fixed-link pseudo-PHY device
858  * @node: OF node for the container of the fixed-link node
859  *
860  * Description: Creates a struct phy_device based on a fixed-link of_node
861  * description. Can be used without phy_connect by drivers which do not expose
862  * a UCLASS_ETH udevice.
863  */
864 struct phy_device *fixed_phy_create(ofnode node)
865 {
866         struct phy_device *phydev;
867         ofnode subnode;
868
869         subnode = ofnode_find_subnode(node, "fixed-link");
870         if (!ofnode_valid(subnode)) {
871                 return NULL;
872         }
873
874         phydev = phy_device_create(NULL, 0, PHY_FIXED_ID, false);
875         if (phydev) {
876                 phydev->node = subnode;
877                 phydev->interface = ofnode_read_phy_mode(node);
878         }
879
880         return phydev;
881 }
882
883 static struct phy_device *phy_connect_fixed(struct mii_dev *bus,
884                                             struct udevice *dev)
885 {
886         ofnode node = dev_ofnode(dev), subnode;
887         struct phy_device *phydev = NULL;
888
889         if (ofnode_phy_is_fixed_link(node, &subnode)) {
890                 phydev = phy_device_create(bus, 0, PHY_FIXED_ID, false);
891                 if (phydev)
892                         phydev->node = subnode;
893         }
894
895         return phydev;
896 }
897 #endif
898
899 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
900                                struct udevice *dev,
901                                phy_interface_t interface)
902 {
903         struct phy_device *phydev = NULL;
904         uint mask = (addr >= 0) ? (1 << addr) : 0xffffffff;
905
906 #ifdef CONFIG_PHY_FIXED
907         phydev = phy_connect_fixed(bus, dev);
908 #endif
909
910 #ifdef CONFIG_PHY_NCSI
911         if (!phydev && interface == PHY_INTERFACE_MODE_NCSI)
912                 phydev = phy_device_create(bus, 0, PHY_NCSI_ID, false);
913 #endif
914
915 #ifdef CONFIG_PHY_ETHERNET_ID
916         if (!phydev)
917                 phydev = phy_connect_phy_id(bus, dev, addr);
918 #endif
919
920 #ifdef CONFIG_PHY_XILINX_GMII2RGMII
921         if (!phydev)
922                 phydev = phy_connect_gmii2rgmii(bus, dev);
923 #endif
924
925         if (!phydev)
926                 phydev = phy_find_by_mask(bus, mask);
927
928         if (phydev)
929                 phy_connect_dev(phydev, dev, interface);
930         else
931                 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
932         return phydev;
933 }
934
935 /*
936  * Start the PHY.  Returns 0 on success, or a negative error code.
937  */
938 int phy_startup(struct phy_device *phydev)
939 {
940         if (phydev->drv->startup)
941                 return phydev->drv->startup(phydev);
942
943         return 0;
944 }
945
946 __weak int board_phy_config(struct phy_device *phydev)
947 {
948         if (phydev->drv->config)
949                 return phydev->drv->config(phydev);
950         return 0;
951 }
952
953 int phy_config(struct phy_device *phydev)
954 {
955         /* Invoke an optional board-specific helper */
956         return board_phy_config(phydev);
957 }
958
959 int phy_shutdown(struct phy_device *phydev)
960 {
961         if (phydev->drv->shutdown)
962                 phydev->drv->shutdown(phydev);
963
964         return 0;
965 }
966
967 /**
968  * phy_modify - Convenience function for modifying a given PHY register
969  * @phydev: the phy_device struct
970  * @devad: The MMD to read from
971  * @regnum: register number to write
972  * @mask: bit mask of bits to clear
973  * @set: new value of bits set in mask to write to @regnum
974  */
975 int phy_modify(struct phy_device *phydev, int devad, int regnum, u16 mask,
976                u16 set)
977 {
978         int ret;
979
980         ret = phy_read(phydev, devad, regnum);
981         if (ret < 0)
982                 return ret;
983
984         return phy_write(phydev, devad, regnum, (ret & ~mask) | set);
985 }
986
987 /**
988  * phy_read - Convenience function for reading a given PHY register
989  * @phydev: the phy_device struct
990  * @devad: The MMD to read from
991  * @regnum: register number to read
992  * @return: value for success or negative errno for failure
993  */
994 int phy_read(struct phy_device *phydev, int devad, int regnum)
995 {
996         struct mii_dev *bus = phydev->bus;
997
998         if (!bus || !bus->read) {
999                 debug("%s: No bus configured\n", __func__);
1000                 return -1;
1001         }
1002
1003         return bus->read(bus, phydev->addr, devad, regnum);
1004 }
1005
1006 /**
1007  * phy_write - Convenience function for writing a given PHY register
1008  * @phydev: the phy_device struct
1009  * @devad: The MMD to read from
1010  * @regnum: register number to write
1011  * @val: value to write to @regnum
1012  * @return: 0 for success or negative errno for failure
1013  */
1014 int phy_write(struct phy_device *phydev, int devad, int regnum, u16 val)
1015 {
1016         struct mii_dev *bus = phydev->bus;
1017
1018         if (!bus || !bus->write) {
1019                 debug("%s: No bus configured\n", __func__);
1020                 return -1;
1021         }
1022
1023         return bus->write(bus, phydev->addr, devad, regnum, val);
1024 }
1025
1026 /**
1027  * phy_mmd_start_indirect - Convenience function for writing MMD registers
1028  * @phydev: the phy_device struct
1029  * @devad: The MMD to read from
1030  * @regnum: register number to write
1031  * @return: None
1032  */
1033 void phy_mmd_start_indirect(struct phy_device *phydev, int devad, int regnum)
1034 {
1035         /* Write the desired MMD Devad */
1036         phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_CTRL, devad);
1037
1038         /* Write the desired MMD register address */
1039         phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA, regnum);
1040
1041         /* Select the Function : DATA with no post increment */
1042         phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_CTRL,
1043                   (devad | MII_MMD_CTRL_NOINCR));
1044 }
1045
1046 /**
1047  * phy_read_mmd - Convenience function for reading a register
1048  * from an MMD on a given PHY.
1049  * @phydev: The phy_device struct
1050  * @devad: The MMD to read from
1051  * @regnum: The register on the MMD to read
1052  * @return: Value for success or negative errno for failure
1053  */
1054 int phy_read_mmd(struct phy_device *phydev, int devad, int regnum)
1055 {
1056         struct phy_driver *drv = phydev->drv;
1057
1058         if (regnum > (u16)~0 || devad > 32)
1059                 return -EINVAL;
1060
1061         /* driver-specific access */
1062         if (drv->read_mmd)
1063                 return drv->read_mmd(phydev, devad, regnum);
1064
1065         /* direct C45 / C22 access */
1066         if ((drv->features & PHY_10G_FEATURES) == PHY_10G_FEATURES ||
1067             devad == MDIO_DEVAD_NONE || !devad)
1068                 return phy_read(phydev, devad, regnum);
1069
1070         /* indirect C22 access */
1071         phy_mmd_start_indirect(phydev, devad, regnum);
1072
1073         /* Read the content of the MMD's selected register */
1074         return phy_read(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA);
1075 }
1076
1077 /**
1078  * phy_write_mmd - Convenience function for writing a register
1079  * on an MMD on a given PHY.
1080  * @phydev: The phy_device struct
1081  * @devad: The MMD to read from
1082  * @regnum: The register on the MMD to read
1083  * @val: value to write to @regnum
1084  * @return: 0 for success or negative errno for failure
1085  */
1086 int phy_write_mmd(struct phy_device *phydev, int devad, int regnum, u16 val)
1087 {
1088         struct phy_driver *drv = phydev->drv;
1089
1090         if (regnum > (u16)~0 || devad > 32)
1091                 return -EINVAL;
1092
1093         /* driver-specific access */
1094         if (drv->write_mmd)
1095                 return drv->write_mmd(phydev, devad, regnum, val);
1096
1097         /* direct C45 / C22 access */
1098         if ((drv->features & PHY_10G_FEATURES) == PHY_10G_FEATURES ||
1099             devad == MDIO_DEVAD_NONE || !devad)
1100                 return phy_write(phydev, devad, regnum, val);
1101
1102         /* indirect C22 access */
1103         phy_mmd_start_indirect(phydev, devad, regnum);
1104
1105         /* Write the data into MMD's selected register */
1106         return phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA, val);
1107 }
1108
1109 /**
1110  * phy_set_bits_mmd - Convenience function for setting bits in a register
1111  * on MMD
1112  * @phydev: the phy_device struct
1113  * @devad: the MMD containing register to modify
1114  * @regnum: register number to modify
1115  * @val: bits to set
1116  * @return: 0 for success or negative errno for failure
1117  */
1118 int phy_set_bits_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
1119 {
1120         int value, ret;
1121
1122         value = phy_read_mmd(phydev, devad, regnum);
1123         if (value < 0)
1124                 return value;
1125
1126         value |= val;
1127
1128         ret = phy_write_mmd(phydev, devad, regnum, value);
1129         if (ret < 0)
1130                 return ret;
1131
1132         return 0;
1133 }
1134
1135 /**
1136  * phy_clear_bits_mmd - Convenience function for clearing bits in a register
1137  * on MMD
1138  * @phydev: the phy_device struct
1139  * @devad: the MMD containing register to modify
1140  * @regnum: register number to modify
1141  * @val: bits to clear
1142  * @return: 0 for success or negative errno for failure
1143  */
1144 int phy_clear_bits_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
1145 {
1146         int value, ret;
1147
1148         value = phy_read_mmd(phydev, devad, regnum);
1149         if (value < 0)
1150                 return value;
1151
1152         value &= ~val;
1153
1154         ret = phy_write_mmd(phydev, devad, regnum, value);
1155         if (ret < 0)
1156                 return ret;
1157
1158         return 0;
1159 }
1160
1161 /**
1162  * phy_modify_mmd_changed - Function for modifying a register on MMD
1163  * @phydev: the phy_device struct
1164  * @devad: the MMD containing register to modify
1165  * @regnum: register number to modify
1166  * @mask: bit mask of bits to clear
1167  * @set: new value of bits set in mask to write to @regnum
1168  *
1169  * NOTE: MUST NOT be called from interrupt context,
1170  * because the bus read/write functions may wait for an interrupt
1171  * to conclude the operation.
1172  *
1173  * Returns negative errno, 0 if there was no change, and 1 in case of change
1174  */
1175 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
1176                            u16 mask, u16 set)
1177 {
1178         int new, ret;
1179
1180         ret = phy_read_mmd(phydev, devad, regnum);
1181         if (ret < 0)
1182                 return ret;
1183
1184         new = (ret & ~mask) | set;
1185         if (new == ret)
1186                 return 0;
1187
1188         ret = phy_write_mmd(phydev, devad, regnum, new);
1189
1190         return ret < 0 ? ret : 1;
1191 }
1192
1193 /**
1194  * phy_modify_mmd - Convenience function for modifying a register on MMD
1195  * @phydev: the phy_device struct
1196  * @devad: the MMD containing register to modify
1197  * @regnum: register number to modify
1198  * @mask: bit mask of bits to clear
1199  * @set: new value of bits set in mask to write to @regnum
1200  *
1201  * NOTE: MUST NOT be called from interrupt context,
1202  * because the bus read/write functions may wait for an interrupt
1203  * to conclude the operation.
1204  */
1205 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
1206                    u16 mask, u16 set)
1207 {
1208         int ret;
1209
1210         ret = phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
1211
1212         return ret < 0 ? ret : 0;
1213 }
1214
1215 bool phy_interface_is_ncsi(void)
1216 {
1217 #ifdef CONFIG_PHY_NCSI
1218         struct eth_pdata *pdata = dev_get_plat(eth_get_dev());
1219
1220         return pdata->phy_interface == PHY_INTERFACE_MODE_NCSI;
1221 #else
1222         return 0;
1223 #endif
1224 }