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