]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/net/phy/phy.c
update to 2015.04-rc1
[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_AQUANTIA
471         phy_aquantia_init();
472 #endif
473 #ifdef CONFIG_PHY_ATHEROS
474         phy_atheros_init();
475 #endif
476 #ifdef CONFIG_PHY_BROADCOM
477         phy_broadcom_init();
478 #endif
479 #ifdef CONFIG_PHY_CORTINA
480         phy_cortina_init();
481 #endif
482 #ifdef CONFIG_PHY_DAVICOM
483         phy_davicom_init();
484 #endif
485 #ifdef CONFIG_PHY_ET1011C
486         phy_et1011c_init();
487 #endif
488 #ifdef CONFIG_PHY_LXT
489         phy_lxt_init();
490 #endif
491 #ifdef CONFIG_PHY_MARVELL
492         phy_marvell_init();
493 #endif
494 #ifdef CONFIG_PHY_MICREL
495         phy_micrel_init();
496 #endif
497 #ifdef CONFIG_PHY_NATSEMI
498         phy_natsemi_init();
499 #endif
500 #ifdef CONFIG_PHY_REALTEK
501         phy_realtek_init();
502 #endif
503 #ifdef CONFIG_PHY_SMSC
504         phy_smsc_init();
505 #endif
506 #ifdef CONFIG_PHY_TERANETICS
507         phy_teranetics_init();
508 #endif
509 #ifdef CONFIG_PHY_VITESSE
510         phy_vitesse_init();
511 #endif
512
513         return 0;
514 }
515
516 int phy_register(struct phy_driver *drv)
517 {
518         INIT_LIST_HEAD(&drv->list);
519         list_add_tail(&drv->list, &phy_drivers);
520
521         return 0;
522 }
523
524 static int phy_probe(struct phy_device *phydev)
525 {
526         int err = 0;
527
528         phydev->advertising = phydev->supported = phydev->drv->features;
529         phydev->mmds = phydev->drv->mmds;
530
531         if (phydev->drv->probe)
532                 err = phydev->drv->probe(phydev);
533
534         return err;
535 }
536
537 static struct phy_driver *generic_for_interface(phy_interface_t interface)
538 {
539 #ifdef CONFIG_PHYLIB_10G
540         if (is_10g_interface(interface))
541                 return &gen10g_driver;
542 #endif
543
544         return &genphy_driver;
545 }
546
547 static struct phy_driver *get_phy_driver(struct phy_device *phydev,
548                                 phy_interface_t interface)
549 {
550         struct list_head *entry;
551         int phy_id = phydev->phy_id;
552         struct phy_driver *drv = NULL;
553
554         list_for_each(entry, &phy_drivers) {
555                 drv = list_entry(entry, struct phy_driver, list);
556                 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
557                         return drv;
558         }
559
560         /* If we made it here, there's no driver for this PHY */
561         return generic_for_interface(interface);
562 }
563
564 static int aneg_enabled(struct phy_device *phydev)
565 {
566         static const char *aneg = "_aneg";
567         char varname[strlen(phydev->bus->name) + strlen(aneg) + 1];
568
569         snprintf(varname, sizeof(varname), "%s%s", phydev->bus->name, aneg);
570         return getenv_yesno(varname);
571 }
572
573 static int phy_get_speed(struct phy_device *phydev)
574 {
575         int ret;
576         static const char *aneg = "_speed";
577         char varname[strlen(phydev->bus->name) + strlen(aneg) + 1];
578         ulong val;
579
580         snprintf(varname, sizeof(varname), "%s%s", phydev->bus->name, aneg);
581
582         val = getenv_ulong(varname, 10, 100);
583         switch (val) {
584         case 1000:
585                 ret = SPEED_1000;
586                 break;
587         case 100:
588                 ret = SPEED_100;
589                 break;
590         case 10:
591                 ret = SPEED_10;
592                 break;
593         default:
594                 printf("Improper setting '%s' for %s; assuming 100\n",
595                         getenv(varname), varname);
596                 ret = SPEED_100;
597         }
598         return ret;
599 }
600
601 static int phy_get_duplex(struct phy_device *phydev)
602 {
603         int ret = DUPLEX_FULL;
604         static const char *aneg = "_duplex";
605         char varname[strlen(phydev->bus->name) + strlen(aneg) + 4];
606         const char *val;
607
608         snprintf(varname, sizeof(varname), "%s%d%s",
609                 phydev->bus->name, phydev->addr, aneg);
610
611         val = getenv(varname);
612         if (val != NULL) {
613                 if (strcasecmp(val, "full") != 0) {
614                         if (strcasecmp(val, "half") == 0) {
615                                 ret = DUPLEX_HALF;
616                         } else {
617                                 printf("Improper setting '%s' for %s; assuming 'full'\n",
618                                         val, varname);
619                                 printf("Expected one of: 'full', 'half'\n");
620                         }
621                 }
622         }
623
624         return ret;
625 }
626
627 static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
628                                             int phy_id,
629                                             phy_interface_t interface)
630 {
631         struct phy_device *dev;
632
633         /* We allocate the device, and initialize the
634          * default values */
635         dev = malloc(sizeof(*dev));
636         if (!dev) {
637                 printf("Failed to allocate PHY device for %s:%d\n",
638                         bus->name, addr);
639                 return NULL;
640         }
641
642         memset(dev, 0, sizeof(*dev));
643
644         dev->duplex = -1;
645         dev->link = 1;
646         dev->interface = interface;
647
648         dev->addr = addr;
649         dev->phy_id = phy_id;
650         dev->bus = bus;
651
652         dev->drv = get_phy_driver(dev, interface);
653
654         if (aneg_enabled(dev)) {
655                 dev->autoneg = AUTONEG_ENABLE;
656         } else {
657                 dev->autoneg = AUTONEG_DISABLE;
658                 dev->speed = phy_get_speed(dev);
659                 dev->duplex = phy_get_duplex(dev);
660
661                 switch (dev->speed) {
662                 case SPEED_1000:
663                         if (dev->duplex == DUPLEX_FULL)
664                                 dev->supported &= SUPPORTED_1000baseT_Full;
665                         else
666                                 dev->supported &= SUPPORTED_1000baseT_Half;
667                         break;
668                 case SPEED_100:
669                         if (dev->duplex == DUPLEX_FULL)
670                                 dev->supported &= SUPPORTED_100baseT_Full;
671                         else
672                                 dev->supported &= SUPPORTED_100baseT_Half;
673                         break;
674                 case SPEED_10:
675                         if (dev->duplex == DUPLEX_FULL)
676                                 dev->supported &= SUPPORTED_10baseT_Full;
677                         else
678                                 dev->supported &= SUPPORTED_10baseT_Half;
679                         break;
680                 default:
681                         printf("Unsupported speed: %d\n", dev->speed);
682                 }
683         }
684
685         phy_probe(dev);
686
687         bus->phymap[addr] = dev;
688
689         return dev;
690 }
691
692 /**
693  * get_phy_id - reads the specified addr for its ID.
694  * @bus: the target MII bus
695  * @addr: PHY address on the MII bus
696  * @phy_id: where to store the ID retrieved.
697  *
698  * Description: Reads the ID registers of the PHY at @addr on the
699  *   @bus, stores it in @phy_id and returns zero on success.
700  */
701 static int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
702 {
703         int phy_reg;
704
705         /* Grab the bits from PHYIR1, and put them
706          * in the upper half */
707         phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
708
709         if (phy_reg < 0)
710                 return -EIO;
711
712         *phy_id = (phy_reg & 0xffff) << 16;
713
714         /* Grab the bits from PHYIR2, and put them in the lower half */
715         phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
716
717         if (phy_reg < 0)
718                 return -EIO;
719
720         *phy_id |= (phy_reg & 0xffff);
721
722         return 0;
723 }
724
725 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
726                 unsigned phy_mask, int devad, phy_interface_t interface)
727 {
728         u32 phy_id = 0xffffffff;
729         while (phy_mask) {
730                 int addr = ffs(phy_mask) - 1;
731                 int r = get_phy_id(bus, addr, devad, &phy_id);
732                 /* If the PHY ID is mostly f's, we didn't find anything */
733                 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff)
734                         return phy_device_create(bus, addr, phy_id, interface);
735                 phy_mask &= ~(1 << addr);
736         }
737         return NULL;
738 }
739
740 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
741                 unsigned phy_mask, phy_interface_t interface)
742 {
743         /* If we have one, return the existing device, with new interface */
744         while (phy_mask) {
745                 int addr = ffs(phy_mask) - 1;
746                 if (bus->phymap[addr]) {
747                         bus->phymap[addr]->interface = interface;
748                         return bus->phymap[addr];
749                 }
750                 phy_mask &= ~(1 << addr);
751         }
752         return NULL;
753 }
754
755 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
756                 unsigned phy_mask, phy_interface_t interface)
757 {
758         int i;
759         struct phy_device *phydev;
760
761         phydev = search_for_existing_phy(bus, phy_mask, interface);
762         if (phydev)
763                 return phydev;
764         /* Try Standard (ie Clause 22) access */
765         /* Otherwise we have to try Clause 45 */
766         for (i = 0; i < 5; i++) {
767                 phydev = create_phy_by_mask(bus, phy_mask,
768                                 i ? i : MDIO_DEVAD_NONE, interface);
769                 if (IS_ERR(phydev))
770                         return NULL;
771                 if (phydev)
772                         return phydev;
773         }
774         printf("Phy %d not found\n", ffs(phy_mask) - 1);
775         return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
776 }
777
778 /**
779  * get_phy_device - reads the specified PHY device and returns its @phy_device struct
780  * @bus: the target MII bus
781  * @addr: PHY address on the MII bus
782  *
783  * Description: Reads the ID registers of the PHY at @addr on the
784  *   @bus, then allocates and returns the phy_device to represent it.
785  */
786 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
787                                          phy_interface_t interface)
788 {
789         return get_phy_device_by_mask(bus, 1 << addr, interface);
790 }
791
792 int phy_reset(struct phy_device *phydev)
793 {
794         int err;
795         int reg;
796         int timeout = 500;
797         int devad = MDIO_DEVAD_NONE;
798
799 #ifdef CONFIG_PHYLIB_10G
800         /* If it's 10G, we need to issue reset through one of the MMDs */
801         if (is_10g_interface(phydev->interface)) {
802                 if (!phydev->mmds)
803                         gen10g_discover_mmds(phydev);
804
805                 devad = ffs(phydev->mmds) - 1;
806         }
807 #endif
808
809         reg = phy_read(phydev, devad, MII_BMCR);
810         if (reg < 0) {
811                 debug("PHY status read failed\n");
812                 return -1;
813         }
814
815         reg |= BMCR_RESET;
816
817         err = phy_write(phydev, devad, MII_BMCR, reg);
818         if (err < 0) {
819                 debug("PHY reset failed\n");
820                 return err;
821         }
822
823 #ifdef CONFIG_PHY_RESET_DELAY
824         udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
825 #endif
826         /*
827          * Poll the control register for the reset bit to go to 0 (it is
828          * auto-clearing).  This should happen within 0.5 seconds per the
829          * IEEE spec.
830          */
831         while ((reg & BMCR_RESET) && timeout-- >= 0) {
832                 reg = phy_read(phydev, devad, MII_BMCR);
833
834                 if (reg < 0) {
835                         debug("PHY status read failed\n");
836                         return reg;
837                 }
838                 udelay(1000);
839         }
840
841         if (reg & BMCR_RESET) {
842                 puts("PHY reset timed out\n");
843                 return -ETIMEDOUT;
844         }
845
846         return 0;
847 }
848
849 int miiphy_reset(const char *devname, unsigned char addr)
850 {
851         struct mii_dev *bus = miiphy_get_dev_by_name(devname);
852         struct phy_device *phydev;
853
854         /*
855          * miiphy_reset was only used on standard PHYs, so we'll fake it here.
856          * If later code tries to connect with the right interface, this will
857          * be corrected by get_phy_device in phy_connect()
858          */
859         phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
860
861         return phy_reset(phydev);
862 }
863
864 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
865                 phy_interface_t interface)
866 {
867         /* Reset the bus */
868         if (bus->reset)
869                 bus->reset(bus);
870
871         /* Wait 15ms to make sure the PHY has come out of hard reset */
872         udelay(15000);
873         return get_phy_device_by_mask(bus, phy_mask, interface);
874 }
875
876 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
877 {
878         /* Soft Reset the PHY */
879         phy_reset(phydev);
880         if (phydev->dev) {
881                 printf("%s:%d is connected to %s.  Reconnecting to %s\n",
882                                 phydev->bus->name, phydev->addr,
883                                 phydev->dev->name, dev->name);
884         }
885         phydev->dev = dev;
886         debug("%s connected to %s\n", dev->name, phydev->drv->name);
887 }
888
889 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
890                 struct eth_device *dev, phy_interface_t interface)
891 {
892         struct phy_device *phydev;
893
894         phydev = phy_find_by_mask(bus, 1 << addr, interface);
895         if (phydev)
896                 phy_connect_dev(phydev, dev);
897         else
898                 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
899         return phydev;
900 }
901
902 /*
903  * Start the PHY.  Returns 0 on success, or a negative error code.
904  */
905 int phy_startup(struct phy_device *phydev)
906 {
907         if (phydev->drv->startup)
908                 return phydev->drv->startup(phydev);
909
910         return 0;
911 }
912
913 __weak int board_phy_config(struct phy_device *phydev)
914 {
915         if (phydev->drv->config)
916                 return phydev->drv->config(phydev);
917         return 0;
918 }
919
920 int phy_config(struct phy_device *phydev)
921 {
922         /* Invoke an optional board-specific helper */
923         board_phy_config(phydev);
924
925         return 0;
926 }
927
928 int phy_shutdown(struct phy_device *phydev)
929 {
930         if (phydev->drv->shutdown)
931                 phydev->drv->shutdown(phydev);
932
933         return 0;
934 }