]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/net/phy/phy.c
net: phy: don't try autonegotiation if it is not enabled in the PHY
[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
22 /* Generic PHY support and helper functions */
23
24 /**
25  * genphy_config_advert - sanitize and advertise auto-negotation parameters
26  * @phydev: target phy_device struct
27  *
28  * Description: Writes MII_ADVERTISE with the appropriate values,
29  *   after sanitizing the values to make sure we only advertise
30  *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
31  *   hasn't changed, and > 0 if it has changed.
32  */
33 static int genphy_config_advert(struct phy_device *phydev)
34 {
35         u32 advertise;
36         int oldadv, adv;
37         int err, changed = 0;
38
39         /* Only allow advertising what
40          * this PHY supports */
41         phydev->advertising &= phydev->supported;
42         advertise = phydev->advertising;
43
44         /* Setup standard advertisement */
45         oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
46
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
82                 if (adv < 0)
83                         return adv;
84
85                 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
86                 if (advertise & SUPPORTED_1000baseT_Half)
87                         adv |= ADVERTISE_1000HALF;
88                 if (advertise & SUPPORTED_1000baseT_Full)
89                         adv |= ADVERTISE_1000FULL;
90
91                 if (adv != oldadv) {
92                         err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000,
93                                         adv);
94
95                         if (err < 0)
96                                 return err;
97                         changed = 1;
98                 }
99         }
100
101         return changed;
102 }
103
104
105 /**
106  * genphy_setup_forced - configures/forces speed/duplex from @phydev
107  * @phydev: target phy_device struct
108  *
109  * Description: Configures MII_BMCR to force speed/duplex
110  *   to the values in phydev. Assumes that the values are valid.
111  */
112 static int genphy_setup_forced(struct phy_device *phydev)
113 {
114         int ctl = 0;
115
116         phydev->pause = phydev->asym_pause = 0;
117
118         if (SPEED_1000 == phydev->speed)
119                 ctl |= BMCR_SPEED1000;
120         else if (SPEED_100 == phydev->speed)
121                 ctl |= BMCR_SPEED100;
122
123         if (DUPLEX_FULL == phydev->duplex)
124                 ctl |= BMCR_FULLDPLX;
125
126         return phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
127 }
128
129
130 /**
131  * genphy_restart_aneg - Enable and Restart Autonegotiation
132  * @phydev: target phy_device struct
133  */
134 int genphy_restart_aneg(struct phy_device *phydev)
135 {
136         int ctl;
137
138         ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
139
140         if (ctl < 0)
141                 return ctl;
142
143         ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
144
145         /* Don't isolate the PHY if we're negotiating */
146         ctl &= ~(BMCR_ISOLATE);
147
148         return phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
149 }
150
151
152 /**
153  * genphy_config_aneg - restart auto-negotiation or write BMCR
154  * @phydev: target phy_device struct
155  *
156  * Description: If auto-negotiation is enabled, we configure the
157  *   advertising, and then restart auto-negotiation.  If it is not
158  *   enabled, then we write the BMCR.
159  */
160 int genphy_config_aneg(struct phy_device *phydev)
161 {
162         int result;
163
164         if (AUTONEG_ENABLE != phydev->autoneg)
165                 return genphy_setup_forced(phydev);
166
167         result = genphy_config_advert(phydev);
168
169         if (result < 0) /* error */
170                 return result;
171
172         if (result == 0) {
173                 /* Advertisment hasn't changed, but maybe aneg was never on to
174                  * begin with?  Or maybe phy was isolated? */
175                 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
176
177                 if (ctl < 0)
178                         return ctl;
179
180                 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
181                         result = 1; /* do restart aneg */
182         }
183
184         /* Only restart aneg if we are advertising something different
185          * than we were before.  */
186         if (result > 0)
187                 result = genphy_restart_aneg(phydev);
188
189         return result;
190 }
191
192 /**
193  * genphy_update_link - update link status in @phydev
194  * @phydev: target phy_device struct
195  *
196  * Description: Update the value in phydev->link to reflect the
197  *   current link value.  In order to do this, we need to read
198  *   the status register twice, keeping the second value.
199  */
200 int genphy_update_link(struct phy_device *phydev)
201 {
202         int mii_reg;
203         int bmcr;
204
205         /*
206          * Wait if the link is up, and autonegotiation is in progress
207          * (ie - we're capable and it's not done)
208          */
209         mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
210         if (mii_reg < 0)
211                 return mii_reg;
212
213         /*
214          * If we already saw the link up, and it hasn't gone down, then
215          * we don't need to wait for autoneg again
216          */
217         if (phydev->link && mii_reg & BMSR_LSTATUS)
218                 return 0;
219
220         bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
221         if (bmcr < 0)
222                 return bmcr;
223
224         if (!(bmcr & BMCR_ANENABLE))
225                 return 0;
226
227         if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
228                 int i = 0;
229
230                 printf("%s Waiting for PHY auto negotiation to complete",
231                         phydev->dev->name);
232                 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
233                         /*
234                          * Timeout reached ?
235                          */
236                         if (i > PHY_ANEG_TIMEOUT) {
237                                 printf(" TIMEOUT !\n");
238                                 phydev->link = 0;
239                                 return 0;
240                         }
241
242                         if (ctrlc()) {
243                                 puts("user interrupt!\n");
244                                 phydev->link = 0;
245                                 return -EINTR;
246                         }
247
248                         if ((i++ % 500) == 0)
249                                 printf(".");
250
251                         udelay(1000);   /* 1 ms */
252                         mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
253                         if (mii_reg < 0)
254                                 return mii_reg;
255                 }
256                 printf(" done\n");
257                 phydev->link = 1;
258         } else {
259                 /* Read the link a second time to clear the latched state */
260                 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
261                 if (mii_reg < 0)
262                         return mii_reg;
263
264                 if (mii_reg & BMSR_LSTATUS)
265                         phydev->link = 1;
266                 else
267                         phydev->link = 0;
268         }
269
270         return 0;
271 }
272
273 /*
274  * Generic function which updates the speed and duplex.  If
275  * autonegotiation is enabled, it uses the AND of the link
276  * partner's advertised capabilities and our advertised
277  * capabilities.  If autonegotiation is disabled, we use the
278  * appropriate bits in the control register.
279  *
280  * Stolen from Linux's mii.c and phy_device.c
281  */
282 int genphy_parse_link(struct phy_device *phydev)
283 {
284         int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
285
286         if (mii_reg < 0)
287                 return mii_reg;
288
289         /* We're using autonegotiation */
290         if (mii_reg & BMSR_ANEGCAPABLE) {
291                 int ret;
292                 u16 lpa;
293                 u16 gblpa = 0;
294                 int estatus = 0;
295
296                 /* Check for gigabit capability */
297                 if (mii_reg & BMSR_ERCAP) {
298                         /* We want a list of states supported by
299                          * both PHYs in the link
300                          */
301                         ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
302                         if (ret < 0)
303                                 return ret;
304                         gblpa = ret;
305
306                         ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
307                         if (ret < 0)
308                                 return ret;
309                         gblpa &= ret << 2;
310                 }
311
312                 /* Set the baseline so we only have to set them
313                  * if they're different
314                  */
315                 phydev->speed = SPEED_10;
316                 phydev->duplex = DUPLEX_HALF;
317
318                 /* Check the gigabit fields */
319                 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
320                         phydev->speed = SPEED_1000;
321
322                         if (gblpa & PHY_1000BTSR_1000FD)
323                                 phydev->duplex = DUPLEX_FULL;
324
325                         /* We're done! */
326                         return 0;
327                 }
328
329                 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
330                 if (ret < 0)
331                         return ret;
332                 lpa = ret;
333
334                 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
335                 if (ret < 0)
336                         return ret;
337                 lpa &= ret;
338
339                 if (lpa & (LPA_100FULL | LPA_100HALF)) {
340                         phydev->speed = SPEED_100;
341
342                         if (lpa & LPA_100FULL)
343                                 phydev->duplex = DUPLEX_FULL;
344
345                 } else if (lpa & LPA_10FULL)
346                         phydev->duplex = DUPLEX_FULL;
347
348                 /*
349                  * Extended status may indicate that the PHY supports
350                  * 1000BASE-T/X even though the 1000BASE-T registers
351                  * are missing. In this case we can't tell whether the
352                  * peer also supports it, so we only check extended
353                  * status if the 1000BASE-T registers are actually
354                  * missing.
355                  */
356                 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
357                         estatus = phy_read(phydev, MDIO_DEVAD_NONE,
358                                            MII_ESTATUS);
359                 if (estatus < 0)
360                         return estatus;
361
362                 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
363                                 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
364                         phydev->speed = SPEED_1000;
365                         if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
366                                 phydev->duplex = DUPLEX_FULL;
367                 }
368
369         } else {
370                 int bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
371
372                 if (bmcr < 0)
373                         return bmcr;
374
375                 phydev->speed = SPEED_10;
376                 phydev->duplex = DUPLEX_HALF;
377
378                 if (bmcr & BMCR_FULLDPLX)
379                         phydev->duplex = DUPLEX_FULL;
380
381                 if (bmcr & BMCR_SPEED1000)
382                         phydev->speed = SPEED_1000;
383                 else if (bmcr & BMCR_SPEED100)
384                         phydev->speed = SPEED_100;
385         }
386
387         return 0;
388 }
389
390 int genphy_config(struct phy_device *phydev)
391 {
392         int val;
393         u32 features;
394
395         /* For now, I'll claim that the generic driver supports
396          * all possible port types */
397         features = (SUPPORTED_TP | SUPPORTED_MII
398                         | SUPPORTED_AUI | SUPPORTED_FIBRE |
399                         SUPPORTED_BNC);
400
401         /* Do we support autonegotiation? */
402         val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
403
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_DAVICOM
477         phy_davicom_init();
478 #endif
479 #ifdef CONFIG_PHY_ET1011C
480         phy_et1011c_init();
481 #endif
482 #ifdef CONFIG_PHY_ICPLUS
483         phy_icplus_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 struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
562                                             int phy_id,
563                                             phy_interface_t interface)
564 {
565         struct phy_device *dev;
566
567         /* We allocate the device, and initialize the
568          * default values */
569         dev = malloc(sizeof(*dev));
570         if (!dev) {
571                 printf("Failed to allocate PHY device for %s:%d\n",
572                         bus->name, addr);
573                 return NULL;
574         }
575
576         memset(dev, 0, sizeof(*dev));
577
578         dev->duplex = -1;
579         dev->link = 1;
580         dev->interface = interface;
581
582         dev->autoneg = AUTONEG_ENABLE;
583
584         dev->addr = addr;
585         dev->phy_id = phy_id;
586         dev->bus = bus;
587
588         dev->drv = get_phy_driver(dev, interface);
589
590         phy_probe(dev);
591
592         bus->phymap[addr] = dev;
593
594         return dev;
595 }
596
597 /**
598  * get_phy_id - reads the specified addr for its ID.
599  * @bus: the target MII bus
600  * @addr: PHY address on the MII bus
601  * @phy_id: where to store the ID retrieved.
602  *
603  * Description: Reads the ID registers of the PHY at @addr on the
604  *   @bus, stores it in @phy_id and returns zero on success.
605  */
606 static int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
607 {
608         int phy_reg;
609
610         /* Grab the bits from PHYIR1, and put them
611          * in the upper half */
612         phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
613
614         if (phy_reg < 0)
615                 return -EIO;
616
617         *phy_id = (phy_reg & 0xffff) << 16;
618
619         /* Grab the bits from PHYIR2, and put them in the lower half */
620         phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
621
622         if (phy_reg < 0)
623                 return -EIO;
624
625         *phy_id |= (phy_reg & 0xffff);
626
627         return 0;
628 }
629
630 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
631                 unsigned phy_mask, int devad, phy_interface_t interface)
632 {
633         u32 phy_id = 0xffffffff;
634         while (phy_mask) {
635                 int addr = ffs(phy_mask) - 1;
636                 int r = get_phy_id(bus, addr, devad, &phy_id);
637                 if (r < 0)
638                         return ERR_PTR(r);
639                 /* If the PHY ID is mostly f's, we didn't find anything */
640                 if ((phy_id & 0x1fffffff) != 0x1fffffff)
641                         return phy_device_create(bus, addr, phy_id, interface);
642                 phy_mask &= ~(1 << addr);
643         }
644         return NULL;
645 }
646
647 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
648                 unsigned phy_mask, phy_interface_t interface)
649 {
650         /* If we have one, return the existing device, with new interface */
651         while (phy_mask) {
652                 int addr = ffs(phy_mask) - 1;
653                 if (bus->phymap[addr]) {
654                         bus->phymap[addr]->interface = interface;
655                         return bus->phymap[addr];
656                 }
657                 phy_mask &= ~(1 << addr);
658         }
659         return NULL;
660 }
661
662 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
663                 unsigned phy_mask, phy_interface_t interface)
664 {
665         int i;
666         struct phy_device *phydev;
667
668         phydev = search_for_existing_phy(bus, phy_mask, interface);
669         if (phydev)
670                 return phydev;
671         /* Try Standard (ie Clause 22) access */
672         /* Otherwise we have to try Clause 45 */
673         for (i = 0; i < 5; i++) {
674                 phydev = create_phy_by_mask(bus, phy_mask,
675                                 i ? i : MDIO_DEVAD_NONE, interface);
676                 if (IS_ERR(phydev))
677                         return NULL;
678                 if (phydev)
679                         return phydev;
680         }
681         printf("Phy not found\n");
682         return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
683 }
684
685 /**
686  * get_phy_device - reads the specified PHY device and returns its @phy_device struct
687  * @bus: the target MII bus
688  * @addr: PHY address on the MII bus
689  *
690  * Description: Reads the ID registers of the PHY at @addr on the
691  *   @bus, then allocates and returns the phy_device to represent it.
692  */
693 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
694                                          phy_interface_t interface)
695 {
696         return get_phy_device_by_mask(bus, 1 << addr, interface);
697 }
698
699 int phy_reset(struct phy_device *phydev)
700 {
701         int err;
702         int reg;
703         int timeout = 500;
704         int devad = MDIO_DEVAD_NONE;
705
706 #ifdef CONFIG_PHYLIB_10G
707         /* If it's 10G, we need to issue reset through one of the MMDs */
708         if (is_10g_interface(phydev->interface)) {
709                 if (!phydev->mmds)
710                         gen10g_discover_mmds(phydev);
711
712                 devad = ffs(phydev->mmds) - 1;
713         }
714 #endif
715
716         reg = phy_read(phydev, devad, MII_BMCR);
717         if (reg < 0) {
718                 debug("PHY status read failed\n");
719                 return -1;
720         }
721
722         reg |= BMCR_RESET;
723
724         err = phy_write(phydev, devad, MII_BMCR, reg);
725         if (err < 0) {
726                 debug("PHY reset failed\n");
727                 return err;
728         }
729
730 #ifdef CONFIG_PHY_RESET_DELAY
731         udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
732 #endif
733         /*
734          * Poll the control register for the reset bit to go to 0 (it is
735          * auto-clearing).  This should happen within 0.5 seconds per the
736          * IEEE spec.
737          */
738         while ((reg & BMCR_RESET) && timeout-- >= 0) {
739                 reg = phy_read(phydev, devad, MII_BMCR);
740
741                 if (reg < 0) {
742                         debug("PHY status read failed\n");
743                         return reg;
744                 }
745                 udelay(1000);
746         }
747
748         if (reg & BMCR_RESET) {
749                 puts("PHY reset timed out\n");
750                 return -ETIMEDOUT;
751         }
752
753         return 0;
754 }
755
756 int miiphy_reset(const char *devname, unsigned char addr)
757 {
758         struct mii_dev *bus = miiphy_get_dev_by_name(devname);
759         struct phy_device *phydev;
760
761         /*
762          * miiphy_reset was only used on standard PHYs, so we'll fake it here.
763          * If later code tries to connect with the right interface, this will
764          * be corrected by get_phy_device in phy_connect()
765          */
766         phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
767
768         return phy_reset(phydev);
769 }
770
771 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
772                 phy_interface_t interface)
773 {
774         /* Reset the bus */
775         if (bus->reset)
776                 bus->reset(bus);
777
778         /* Wait 15ms to make sure the PHY has come out of hard reset */
779         udelay(15000);
780         return get_phy_device_by_mask(bus, phy_mask, interface);
781 }
782
783 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
784 {
785         /* Soft Reset the PHY */
786         phy_reset(phydev);
787         if (phydev->dev) {
788                 printf("%s:%d is connected to %s.  Reconnecting to %s\n",
789                                 phydev->bus->name, phydev->addr,
790                                 phydev->dev->name, dev->name);
791         }
792         phydev->dev = dev;
793         debug("%s connected to %s\n", dev->name, phydev->drv->name);
794 }
795
796 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
797                 struct eth_device *dev, phy_interface_t interface)
798 {
799         struct phy_device *phydev;
800
801         phydev = phy_find_by_mask(bus, 1 << addr, interface);
802         if (phydev)
803                 phy_connect_dev(phydev, dev);
804         else
805                 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
806         return phydev;
807 }
808
809 /*
810  * Start the PHY.  Returns 0 on success, or a negative error code.
811  */
812 int phy_startup(struct phy_device *phydev)
813 {
814         if (phydev->drv->startup)
815                 return phydev->drv->startup(phydev);
816
817         return 0;
818 }
819
820 static int __board_phy_config(struct phy_device *phydev)
821 {
822         if (phydev->drv->config)
823                 return phydev->drv->config(phydev);
824         return 0;
825 }
826
827 int board_phy_config(struct phy_device *phydev)
828         __attribute__((weak, alias("__board_phy_config")));
829
830 int phy_config(struct phy_device *phydev)
831 {
832         /* Invoke an optional board-specific helper */
833         board_phy_config(phydev);
834
835         return 0;
836 }
837
838 int phy_shutdown(struct phy_device *phydev)
839 {
840         if (phydev->drv->shutdown)
841                 phydev->drv->shutdown(phydev);
842
843         return 0;
844 }