]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/net/phy/phy.c
net/phy: fixup for get_phy_id
[karo-tx-uboot.git] / drivers / net / phy / phy.c
1 /*
2  * Generic PHY Management code
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  *
6  * Copyright 2011 Freescale Semiconductor, Inc.
7  * author Andy Fleming
8  *
9  * Based loosely off of Linux's PHY Lib
10  */
11
12 #include <config.h>
13 #include <common.h>
14 #include <dm.h>
15 #include <malloc.h>
16 #include <net.h>
17 #include <command.h>
18 #include <miiphy.h>
19 #include <phy.h>
20 #include <errno.h>
21 #include <linux/err.h>
22 #include <linux/compiler.h>
23
24 /* Generic PHY support and helper functions */
25
26 /**
27  * genphy_config_advert - sanitize and advertise auto-negotation parameters
28  * @phydev: target phy_device struct
29  *
30  * Description: Writes MII_ADVERTISE with the appropriate values,
31  *   after sanitizing the values to make sure we only advertise
32  *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
33  *   hasn't changed, and > 0 if it has changed.
34  */
35 static int genphy_config_advert(struct phy_device *phydev)
36 {
37         u32 advertise;
38         int oldadv, adv;
39         int err, changed = 0;
40
41         /* Only allow advertising what
42          * this PHY supports */
43         phydev->advertising &= phydev->supported;
44         advertise = phydev->advertising;
45
46         /* Setup standard advertisement */
47         oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
48         if (adv < 0)
49                 return adv;
50
51         adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
52                  ADVERTISE_PAUSE_ASYM);
53         if (advertise & ADVERTISED_10baseT_Half)
54                 adv |= ADVERTISE_10HALF;
55         if (advertise & ADVERTISED_10baseT_Full)
56                 adv |= ADVERTISE_10FULL;
57         if (advertise & ADVERTISED_100baseT_Half)
58                 adv |= ADVERTISE_100HALF;
59         if (advertise & ADVERTISED_100baseT_Full)
60                 adv |= ADVERTISE_100FULL;
61         if (advertise & ADVERTISED_Pause)
62                 adv |= ADVERTISE_PAUSE_CAP;
63         if (advertise & ADVERTISED_Asym_Pause)
64                 adv |= ADVERTISE_PAUSE_ASYM;
65         if (advertise & ADVERTISED_1000baseX_Half)
66                 adv |= ADVERTISE_1000XHALF;
67         if (advertise & ADVERTISED_1000baseX_Full)
68                 adv |= ADVERTISE_1000XFULL;
69
70         if (adv != oldadv) {
71                 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
72
73                 if (err < 0)
74                         return err;
75                 changed = 1;
76         }
77
78         /* Configure gigabit if it's supported */
79         if (phydev->supported & (SUPPORTED_1000baseT_Half |
80                                 SUPPORTED_1000baseT_Full)) {
81                 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
82                 if (adv < 0)
83                         return adv;
84
85                 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
86                 if (advertise & SUPPORTED_1000baseT_Half)
87                         adv |= ADVERTISE_1000HALF;
88                 if (advertise & SUPPORTED_1000baseT_Full)
89                         adv |= ADVERTISE_1000FULL;
90
91                 if (adv != oldadv) {
92                         err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000,
93                                         adv);
94
95                         if (err < 0)
96                                 return err;
97                         changed = 1;
98                 }
99         }
100
101         return changed;
102 }
103
104
105 /**
106  * genphy_setup_forced - configures/forces speed/duplex from @phydev
107  * @phydev: target phy_device struct
108  *
109  * Description: Configures MII_BMCR to force speed/duplex
110  *   to the values in phydev. Assumes that the values are valid.
111  */
112 static int genphy_setup_forced(struct phy_device *phydev)
113 {
114         int ctl = 0;
115
116         phydev->pause = phydev->asym_pause = 0;
117
118         if (SPEED_1000 == phydev->speed)
119                 ctl |= BMCR_SPEED1000;
120         else if (SPEED_100 == phydev->speed)
121                 ctl |= BMCR_SPEED100;
122
123         if (DUPLEX_FULL == phydev->duplex)
124                 ctl |= BMCR_FULLDPLX;
125
126         return phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
127 }
128
129
130 /**
131  * genphy_restart_aneg - Enable and Restart Autonegotiation
132  * @phydev: target phy_device struct
133  */
134 int genphy_restart_aneg(struct phy_device *phydev)
135 {
136         int ctl;
137
138         ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
139         if (ctl < 0)
140                 return ctl;
141
142         ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
143
144         /* Don't isolate the PHY if we're negotiating */
145         ctl &= ~(BMCR_ISOLATE);
146
147         return phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
148 }
149
150
151 /**
152  * genphy_config_aneg - restart auto-negotiation or write BMCR
153  * @phydev: target phy_device struct
154  *
155  * Description: If auto-negotiation is enabled, we configure the
156  *   advertising, and then restart auto-negotiation.  If it is not
157  *   enabled, then we write the BMCR.
158  */
159 int genphy_config_aneg(struct phy_device *phydev)
160 {
161         int result;
162
163         if (AUTONEG_ENABLE != phydev->autoneg)
164                 return genphy_setup_forced(phydev);
165
166         result = genphy_config_advert(phydev);
167
168         if (result < 0) /* error */
169                 return result;
170
171         if (result == 0) {
172                 /* Advertisment hasn't changed, but maybe aneg was never on to
173                  * begin with?  Or maybe phy was isolated? */
174                 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
175
176                 if (ctl < 0)
177                         return ctl;
178
179                 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
180                         result = 1; /* do restart aneg */
181         }
182
183         /* Only restart aneg if we are advertising something different
184          * than we were before.  */
185         if (result > 0)
186                 result = genphy_restart_aneg(phydev);
187
188         return result;
189 }
190
191 /**
192  * genphy_update_link - update link status in @phydev
193  * @phydev: target phy_device struct
194  *
195  * Description: Update the value in phydev->link to reflect the
196  *   current link value.  In order to do this, we need to read
197  *   the status register twice, keeping the second value.
198  */
199 int genphy_update_link(struct phy_device *phydev)
200 {
201         int mii_reg;
202         int bmcr;
203
204         /*
205          * Wait if the link is up, and autonegotiation is in progress
206          * (ie - we're capable and it's not done)
207          */
208         mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
209         if (mii_reg < 0)
210                 return mii_reg;
211
212         /*
213          * If we already saw the link up, and it hasn't gone down, then
214          * we don't need to wait for autoneg again
215          */
216         if (phydev->link && mii_reg & BMSR_LSTATUS)
217                 return 0;
218
219         bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
220         if (bmcr < 0)
221                 return bmcr;
222
223         if (!(bmcr & BMCR_ANENABLE))
224                 return 0;
225
226         if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
227                 int i = 0;
228
229                 printf("%s Waiting for PHY auto negotiation to complete",
230                         phydev->dev->name);
231                 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
232                         /*
233                          * Timeout reached ?
234                          */
235                         if (i > PHY_ANEG_TIMEOUT) {
236                                 printf(" TIMEOUT !\n");
237                                 phydev->link = 0;
238                                 return 0;
239                         }
240
241                         if (ctrlc()) {
242                                 puts("user interrupt!\n");
243                                 phydev->link = 0;
244                                 return -EINTR;
245                         }
246
247                         if ((i++ % 500) == 0)
248                                 printf(".");
249
250                         udelay(1000);   /* 1 ms */
251                         mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
252                         if (mii_reg < 0)
253                                 return mii_reg;
254                 }
255                 printf(" done\n");
256                 phydev->link = 1;
257         } else {
258                 /* Read the link a second time to clear the latched state */
259                 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
260                 if (mii_reg < 0)
261                         return mii_reg;
262
263                 if (mii_reg & BMSR_LSTATUS)
264                         phydev->link = 1;
265                 else
266                         phydev->link = 0;
267         }
268
269         return 0;
270 }
271
272 /*
273  * Generic function which updates the speed and duplex.  If
274  * autonegotiation is enabled, it uses the AND of the link
275  * partner's advertised capabilities and our advertised
276  * capabilities.  If autonegotiation is disabled, we use the
277  * appropriate bits in the control register.
278  *
279  * Stolen from Linux's mii.c and phy_device.c
280  */
281 int genphy_parse_link(struct phy_device *phydev)
282 {
283         int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
284
285         if (mii_reg < 0)
286                 return mii_reg;
287
288         /* We're using autonegotiation */
289         if (phydev->supported & SUPPORTED_Autoneg) {
290                 int ret;
291                 u32 lpa = 0;
292                 u32 gblpa = 0;
293                 u32 estatus = 0;
294
295                 /* Check for gigabit capability */
296                 if (phydev->supported & (SUPPORTED_1000baseT_Full |
297                                         SUPPORTED_1000baseT_Half)) {
298                         /* We want a list of states supported by
299                          * both PHYs in the link
300                          */
301                         ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
302                         if (ret < 0) {
303                                 debug("Could not read MII_STAT1000. Ignoring gigabit capability\n");
304                                 ret = 0;
305                         }
306                         gblpa = ret;
307
308                         ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
309                         if (ret < 0)
310                                 return ret;
311                         gblpa &= ret << 2;
312                 }
313
314                 /* Set the baseline so we only have to set them
315                  * if they're different
316                  */
317                 phydev->speed = SPEED_10;
318                 phydev->duplex = DUPLEX_HALF;
319
320                 /* Check the gigabit fields */
321                 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
322                         phydev->speed = SPEED_1000;
323
324                         if (gblpa & PHY_1000BTSR_1000FD)
325                                 phydev->duplex = DUPLEX_FULL;
326
327                         /* We're done! */
328                         return 0;
329                 }
330
331                 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
332                 if (ret < 0)
333                         return ret;
334                 lpa = ret;
335
336                 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
337                 if (ret < 0)
338                         return ret;
339                 lpa &= ret;
340
341                 if (lpa & (LPA_100FULL | LPA_100HALF)) {
342                         phydev->speed = SPEED_100;
343
344                         if (lpa & LPA_100FULL)
345                                 phydev->duplex = DUPLEX_FULL;
346
347                 } else if (lpa & LPA_10FULL)
348                         phydev->duplex = DUPLEX_FULL;
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                 if (estatus < 0)
362                         return estatus;
363
364                 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
365                                 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
366                         phydev->speed = SPEED_1000;
367                         if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
368                                 phydev->duplex = DUPLEX_FULL;
369                 }
370
371         } else {
372                 int bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
373
374                 if (bmcr < 0)
375                         return bmcr;
376
377                 phydev->speed = SPEED_10;
378                 phydev->duplex = DUPLEX_HALF;
379
380                 if (bmcr & BMCR_FULLDPLX)
381                         phydev->duplex = DUPLEX_FULL;
382
383                 if (bmcr & BMCR_SPEED1000)
384                         phydev->speed = SPEED_1000;
385                 else if (bmcr & BMCR_SPEED100)
386                         phydev->speed = SPEED_100;
387         }
388
389         return 0;
390 }
391
392 int genphy_config(struct phy_device *phydev)
393 {
394         int val;
395         u32 features;
396
397         /* For now, I'll claim that the generic driver supports
398          * all possible port types */
399         features = (SUPPORTED_TP | SUPPORTED_MII
400                         | SUPPORTED_AUI | SUPPORTED_FIBRE |
401                         SUPPORTED_BNC);
402
403         /* Do we support autonegotiation? */
404         val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
405         if (val < 0)
406                 return val;
407
408         if (val & BMSR_ANEGCAPABLE)
409                 features |= SUPPORTED_Autoneg;
410
411         if (val & BMSR_100FULL)
412                 features |= SUPPORTED_100baseT_Full;
413         if (val & BMSR_100HALF)
414                 features |= SUPPORTED_100baseT_Half;
415         if (val & BMSR_10FULL)
416                 features |= SUPPORTED_10baseT_Full;
417         if (val & BMSR_10HALF)
418                 features |= SUPPORTED_10baseT_Half;
419
420         if (val & BMSR_ESTATEN) {
421                 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
422
423                 if (val < 0)
424                         return val;
425
426                 if (val & ESTATUS_1000_TFULL)
427                         features |= SUPPORTED_1000baseT_Full;
428                 if (val & ESTATUS_1000_THALF)
429                         features |= SUPPORTED_1000baseT_Half;
430                 if (val & ESTATUS_1000_XFULL)
431                         features |= SUPPORTED_1000baseX_Full;
432                 if (val & ESTATUS_1000_XHALF)
433                         features |= SUPPORTED_1000baseX_Half;
434         }
435
436         phydev->supported = features;
437         phydev->advertising = features;
438
439         genphy_config_aneg(phydev);
440
441         return 0;
442 }
443
444 int genphy_startup(struct phy_device *phydev)
445 {
446         genphy_update_link(phydev);
447         genphy_parse_link(phydev);
448
449         return 0;
450 }
451
452 int genphy_shutdown(struct phy_device *phydev)
453 {
454         return 0;
455 }
456
457 static struct phy_driver genphy_driver = {
458         .uid            = 0xffffffff,
459         .mask           = 0xffffffff,
460         .name           = "Generic PHY",
461         .features       = 0,
462         .config         = genphy_config,
463         .startup        = genphy_startup,
464         .shutdown       = genphy_shutdown,
465 };
466
467 static LIST_HEAD(phy_drivers);
468
469 int phy_init(void)
470 {
471 #ifdef CONFIG_PHY_AQUANTIA
472         phy_aquantia_init();
473 #endif
474 #ifdef CONFIG_PHY_ATHEROS
475         phy_atheros_init();
476 #endif
477 #ifdef CONFIG_PHY_BROADCOM
478         phy_broadcom_init();
479 #endif
480 #ifdef CONFIG_PHY_CORTINA
481         phy_cortina_init();
482 #endif
483 #ifdef CONFIG_PHY_DAVICOM
484         phy_davicom_init();
485 #endif
486 #ifdef CONFIG_PHY_ET1011C
487         phy_et1011c_init();
488 #endif
489 #ifdef CONFIG_PHY_LXT
490         phy_lxt_init();
491 #endif
492 #ifdef CONFIG_PHY_MARVELL
493         phy_marvell_init();
494 #endif
495 #ifdef CONFIG_PHY_MICREL
496         phy_micrel_init();
497 #endif
498 #ifdef CONFIG_PHY_NATSEMI
499         phy_natsemi_init();
500 #endif
501 #ifdef CONFIG_PHY_REALTEK
502         phy_realtek_init();
503 #endif
504 #ifdef CONFIG_PHY_SMSC
505         phy_smsc_init();
506 #endif
507 #ifdef CONFIG_PHY_TERANETICS
508         phy_teranetics_init();
509 #endif
510 #ifdef CONFIG_PHY_VITESSE
511         phy_vitesse_init();
512 #endif
513
514         return 0;
515 }
516
517 int phy_register(struct phy_driver *drv)
518 {
519         INIT_LIST_HEAD(&drv->list);
520         list_add_tail(&drv->list, &phy_drivers);
521
522         return 0;
523 }
524
525 static int phy_probe(struct phy_device *phydev)
526 {
527         int err = 0;
528
529         phydev->advertising = phydev->supported = phydev->drv->features;
530         phydev->mmds = phydev->drv->mmds;
531
532         if (phydev->drv->probe)
533                 err = phydev->drv->probe(phydev);
534
535         return err;
536 }
537
538 static struct phy_driver *generic_for_interface(phy_interface_t interface)
539 {
540 #ifdef CONFIG_PHYLIB_10G
541         if (is_10g_interface(interface))
542                 return &gen10g_driver;
543 #endif
544
545         return &genphy_driver;
546 }
547
548 static struct phy_driver *get_phy_driver(struct phy_device *phydev,
549                                 phy_interface_t interface)
550 {
551         struct list_head *entry;
552         int phy_id = phydev->phy_id;
553         struct phy_driver *drv = NULL;
554
555         list_for_each(entry, &phy_drivers) {
556                 drv = list_entry(entry, struct phy_driver, list);
557                 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
558                         return drv;
559         }
560
561         /* If we made it here, there's no driver for this PHY */
562         return generic_for_interface(interface);
563 }
564
565 static int aneg_enabled(struct phy_device *phydev)
566 {
567         static const char *aneg = "_aneg";
568         char varname[strlen(phydev->bus->name) + strlen(aneg) + 1];
569
570         snprintf(varname, sizeof(varname), "%s%s", phydev->bus->name, aneg);
571         return getenv_yesno(varname);
572 }
573
574 static int phy_get_speed(struct phy_device *phydev)
575 {
576         int ret;
577         static const char *aneg = "_speed";
578         char varname[strlen(phydev->bus->name) + strlen(aneg) + 1];
579         ulong val;
580
581         snprintf(varname, sizeof(varname), "%s%s", phydev->bus->name, aneg);
582
583         val = getenv_ulong(varname, 10, 100);
584         switch (val) {
585         case 1000:
586                 ret = SPEED_1000;
587                 break;
588         case 100:
589                 ret = SPEED_100;
590                 break;
591         case 10:
592                 ret = SPEED_10;
593                 break;
594         default:
595                 printf("Improper setting '%s' for %s; assuming 100\n",
596                         getenv(varname), varname);
597                 ret = SPEED_100;
598         }
599         return ret;
600 }
601
602 static int phy_get_duplex(struct phy_device *phydev)
603 {
604         int ret = DUPLEX_FULL;
605         static const char *aneg = "_duplex";
606         char varname[strlen(phydev->bus->name) + strlen(aneg) + 4];
607         const char *val;
608
609         snprintf(varname, sizeof(varname), "%s%d%s",
610                 phydev->bus->name, phydev->addr, aneg);
611
612         val = getenv(varname);
613         if (val != NULL) {
614                 if (strcasecmp(val, "full") != 0) {
615                         if (strcasecmp(val, "half") == 0) {
616                                 ret = DUPLEX_HALF;
617                         } else {
618                                 printf("Improper setting '%s' for %s; assuming 'full'\n",
619                                         val, varname);
620                                 printf("Expected one of: 'full', 'half'\n");
621                         }
622                 }
623         }
624
625         return ret;
626 }
627
628 static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
629                                             int phy_id,
630                                             phy_interface_t interface)
631 {
632         struct phy_device *dev;
633
634         /* We allocate the device, and initialize the
635          * default values */
636         dev = malloc(sizeof(*dev));
637         if (!dev) {
638                 printf("Failed to allocate PHY device for %s:%d\n",
639                         bus->name, addr);
640                 return NULL;
641         }
642
643         memset(dev, 0, sizeof(*dev));
644
645         dev->duplex = -1;
646         dev->link = 1;
647         dev->interface = interface;
648
649         dev->addr = addr;
650         dev->phy_id = phy_id;
651         dev->bus = bus;
652
653         dev->drv = get_phy_driver(dev, interface);
654
655         if (aneg_enabled(dev)) {
656                 dev->autoneg = AUTONEG_ENABLE;
657         } else {
658                 dev->autoneg = AUTONEG_DISABLE;
659                 dev->speed = phy_get_speed(dev);
660                 dev->duplex = phy_get_duplex(dev);
661
662                 switch (dev->speed) {
663                 case SPEED_1000:
664                         if (dev->duplex == DUPLEX_FULL)
665                                 dev->supported &= SUPPORTED_1000baseT_Full;
666                         else
667                                 dev->supported &= SUPPORTED_1000baseT_Half;
668                         break;
669                 case SPEED_100:
670                         if (dev->duplex == DUPLEX_FULL)
671                                 dev->supported &= SUPPORTED_100baseT_Full;
672                         else
673                                 dev->supported &= SUPPORTED_100baseT_Half;
674                         break;
675                 case SPEED_10:
676                         if (dev->duplex == DUPLEX_FULL)
677                                 dev->supported &= SUPPORTED_10baseT_Full;
678                         else
679                                 dev->supported &= SUPPORTED_10baseT_Half;
680                         break;
681                 default:
682                         printf("Unsupported speed: %d\n", dev->speed);
683                 }
684         }
685
686         phy_probe(dev);
687
688         bus->phymap[addr] = dev;
689
690         return dev;
691 }
692
693 /**
694  * get_phy_id - reads the specified addr for its ID.
695  * @bus: the target MII bus
696  * @addr: PHY address on the MII bus
697  * @phy_id: where to store the ID retrieved.
698  *
699  * Description: Reads the ID registers of the PHY at @addr on the
700  *   @bus, stores it in @phy_id and returns zero on success.
701  */
702 int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
703 {
704         int phy_reg;
705
706         /* Grab the bits from PHYIR1, and put them
707          * in the upper half */
708         phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
709
710         if (phy_reg < 0)
711                 return -EIO;
712
713         *phy_id = (phy_reg & 0xffff) << 16;
714
715         /* Grab the bits from PHYIR2, and put them in the lower half */
716         phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
717
718         if (phy_reg < 0)
719                 return -EIO;
720
721         *phy_id |= (phy_reg & 0xffff);
722
723         return 0;
724 }
725
726 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
727                 unsigned phy_mask, int devad, phy_interface_t interface)
728 {
729         u32 phy_id = 0xffffffff;
730         while (phy_mask) {
731                 int addr = ffs(phy_mask) - 1;
732                 int r = get_phy_id(bus, addr, devad, &phy_id);
733                 /* If the PHY ID is mostly f's, we didn't find anything */
734                 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff)
735                         return phy_device_create(bus, addr, phy_id, interface);
736                 phy_mask &= ~(1 << addr);
737         }
738         return NULL;
739 }
740
741 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
742                 unsigned phy_mask, phy_interface_t interface)
743 {
744         /* If we have one, return the existing device, with new interface */
745         while (phy_mask) {
746                 int addr = ffs(phy_mask) - 1;
747                 if (bus->phymap[addr]) {
748                         bus->phymap[addr]->interface = interface;
749                         return bus->phymap[addr];
750                 }
751                 phy_mask &= ~(1 << addr);
752         }
753         return NULL;
754 }
755
756 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
757                 unsigned phy_mask, phy_interface_t interface)
758 {
759         int i;
760         struct phy_device *phydev;
761
762         phydev = search_for_existing_phy(bus, phy_mask, interface);
763         if (phydev)
764                 return phydev;
765         /* Try Standard (ie Clause 22) access */
766         /* Otherwise we have to try Clause 45 */
767         for (i = 0; i < 5; i++) {
768                 phydev = create_phy_by_mask(bus, phy_mask,
769                                 i ? i : MDIO_DEVAD_NONE, interface);
770                 if (IS_ERR(phydev))
771                         return NULL;
772                 if (phydev)
773                         return phydev;
774         }
775         printf("Phy %d not found\n", ffs(phy_mask) - 1);
776         return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
777 }
778
779 /**
780  * get_phy_device - reads the specified PHY device and returns its @phy_device struct
781  * @bus: the target MII bus
782  * @addr: PHY address on the MII bus
783  *
784  * Description: Reads the ID registers of the PHY at @addr on the
785  *   @bus, then allocates and returns the phy_device to represent it.
786  */
787 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
788                                          phy_interface_t interface)
789 {
790         return get_phy_device_by_mask(bus, 1 << addr, interface);
791 }
792
793 int phy_reset(struct phy_device *phydev)
794 {
795         int err;
796         int reg;
797         int timeout = 500;
798         int devad = MDIO_DEVAD_NONE;
799
800 #ifdef CONFIG_PHYLIB_10G
801         /* If it's 10G, we need to issue reset through one of the MMDs */
802         if (is_10g_interface(phydev->interface)) {
803                 if (!phydev->mmds)
804                         gen10g_discover_mmds(phydev);
805
806                 devad = ffs(phydev->mmds) - 1;
807         }
808 #endif
809
810         reg = phy_read(phydev, devad, MII_BMCR);
811         if (reg < 0) {
812                 debug("PHY status read failed\n");
813                 return -1;
814         }
815
816         reg |= BMCR_RESET;
817
818         err = phy_write(phydev, devad, MII_BMCR, reg);
819         if (err < 0) {
820                 debug("PHY reset failed\n");
821                 return err;
822         }
823
824 #ifdef CONFIG_PHY_RESET_DELAY
825         udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
826 #endif
827         /*
828          * Poll the control register for the reset bit to go to 0 (it is
829          * auto-clearing).  This should happen within 0.5 seconds per the
830          * IEEE spec.
831          */
832         while ((reg & BMCR_RESET) && timeout-- >= 0) {
833                 reg = phy_read(phydev, devad, MII_BMCR);
834
835                 if (reg < 0) {
836                         debug("PHY status read failed\n");
837                         return reg;
838                 }
839                 udelay(1000);
840         }
841
842         if (reg & BMCR_RESET) {
843                 puts("PHY reset timed out\n");
844                 return -ETIMEDOUT;
845         }
846
847         return 0;
848 }
849
850 int miiphy_reset(const char *devname, unsigned char addr)
851 {
852         struct mii_dev *bus = miiphy_get_dev_by_name(devname);
853         struct phy_device *phydev;
854
855         /*
856          * miiphy_reset was only used on standard PHYs, so we'll fake it here.
857          * If later code tries to connect with the right interface, this will
858          * be corrected by get_phy_device in phy_connect()
859          */
860         phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
861
862         return phy_reset(phydev);
863 }
864
865 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
866                 phy_interface_t interface)
867 {
868         /* Reset the bus */
869         if (bus->reset)
870                 bus->reset(bus);
871
872         /* Wait 15ms to make sure the PHY has come out of hard reset */
873         udelay(15000);
874         return get_phy_device_by_mask(bus, phy_mask, interface);
875 }
876
877 #ifdef CONFIG_DM_ETH
878 void phy_connect_dev(struct phy_device *phydev, struct udevice *dev)
879 #else
880 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
881 #endif
882 {
883         /* Soft Reset the PHY */
884         phy_reset(phydev);
885         if (phydev->dev) {
886                 printf("%s:%d is connected to %s.  Reconnecting to %s\n",
887                                 phydev->bus->name, phydev->addr,
888                                 phydev->dev->name, dev->name);
889         }
890         phydev->dev = dev;
891         debug("%s connected to %s\n", dev->name, phydev->drv->name);
892 }
893
894 #ifdef CONFIG_DM_ETH
895 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
896                 struct udevice *dev, phy_interface_t interface)
897 #else
898 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
899                 struct eth_device *dev, phy_interface_t interface)
900 #endif
901 {
902         struct phy_device *phydev;
903
904         phydev = phy_find_by_mask(bus, 1 << addr, interface);
905         if (phydev)
906                 phy_connect_dev(phydev, dev);
907         else
908                 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
909         return phydev;
910 }
911
912 /*
913  * Start the PHY.  Returns 0 on success, or a negative error code.
914  */
915 int phy_startup(struct phy_device *phydev)
916 {
917         if (phydev->drv->startup)
918                 return phydev->drv->startup(phydev);
919
920         return 0;
921 }
922
923 __weak int board_phy_config(struct phy_device *phydev)
924 {
925         if (phydev->drv->config)
926                 return phydev->drv->config(phydev);
927         return 0;
928 }
929
930 int phy_config(struct phy_device *phydev)
931 {
932         /* Invoke an optional board-specific helper */
933         board_phy_config(phydev);
934
935         return 0;
936 }
937
938 int phy_shutdown(struct phy_device *phydev)
939 {
940         if (phydev->drv->shutdown)
941                 phydev->drv->shutdown(phydev);
942
943         return 0;
944 }
945
946 int phy_get_interface_by_name(const char *str)
947 {
948         int i;
949
950         for (i = 0; i < PHY_INTERFACE_MODE_COUNT; i++) {
951                 if (!strcmp(str, phy_interface_strings[i]))
952                         return i;
953         }
954
955         return -1;
956 }