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