]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/net/phy/phy.c
9e6578644ca9e92802d92c959f0a2017c60ca229
[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
204         /*
205          * Wait if the link is up, and autonegotiation is in progress
206          * (ie - we're capable and it's not done)
207          */
208         mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
209         if (mii_reg < 0)
210                 return mii_reg;
211
212         /*
213          * If we already saw the link up, and it hasn't gone down, then
214          * we don't need to wait for autoneg again
215          */
216         if (phydev->link && mii_reg & BMSR_LSTATUS)
217                 return 0;
218
219         if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
220                 int i = 0;
221
222                 printf("%s Waiting for PHY auto negotiation to complete",
223                         phydev->dev->name);
224                 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
225                         /*
226                          * Timeout reached ?
227                          */
228                         if (i > PHY_ANEG_TIMEOUT) {
229                                 printf(" TIMEOUT !\n");
230                                 phydev->link = 0;
231                                 return 0;
232                         }
233
234                         if (ctrlc()) {
235                                 puts("user interrupt!\n");
236                                 phydev->link = 0;
237                                 return -EINTR;
238                         }
239
240                         if ((i++ % 500) == 0)
241                                 printf(".");
242
243                         udelay(1000);   /* 1 ms */
244                         mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
245                         if (mii_reg < 0)
246                                 return mii_reg;
247                 }
248                 printf(" done\n");
249                 phydev->link = 1;
250         } else {
251                 /* Read the link a second time to clear the latched state */
252                 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
253                 if (mii_reg < 0)
254                         return mii_reg;
255
256                 if (mii_reg & BMSR_LSTATUS)
257                         phydev->link = 1;
258                 else
259                         phydev->link = 0;
260         }
261
262         return 0;
263 }
264
265 /*
266  * Generic function which updates the speed and duplex.  If
267  * autonegotiation is enabled, it uses the AND of the link
268  * partner's advertised capabilities and our advertised
269  * capabilities.  If autonegotiation is disabled, we use the
270  * appropriate bits in the control register.
271  *
272  * Stolen from Linux's mii.c and phy_device.c
273  */
274 int genphy_parse_link(struct phy_device *phydev)
275 {
276         int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
277
278         if (mii_reg < 0)
279                 return mii_reg;
280
281         /* We're using autonegotiation */
282         if (mii_reg & BMSR_ANEGCAPABLE) {
283                 int ret;
284                 u16 lpa;
285                 u16 gblpa = 0;
286                 int estatus = 0;
287
288                 /* Check for gigabit capability */
289                 if (mii_reg & BMSR_ERCAP) {
290                         /* We want a list of states supported by
291                          * both PHYs in the link
292                          */
293                         ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
294                         if (ret < 0)
295                                 return ret;
296                         gblpa = ret;
297
298                         ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
299                         if (ret < 0)
300                                 return ret;
301                         gblpa &= ret << 2;
302                 }
303
304                 /* Set the baseline so we only have to set them
305                  * if they're different
306                  */
307                 phydev->speed = SPEED_10;
308                 phydev->duplex = DUPLEX_HALF;
309
310                 /* Check the gigabit fields */
311                 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
312                         phydev->speed = SPEED_1000;
313
314                         if (gblpa & PHY_1000BTSR_1000FD)
315                                 phydev->duplex = DUPLEX_FULL;
316
317                         /* We're done! */
318                         return 0;
319                 }
320
321                 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
322                 if (ret < 0)
323                         return ret;
324                 lpa = ret;
325
326                 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
327                 if (ret < 0)
328                         return ret;
329                 lpa &= ret;
330
331                 if (lpa & (LPA_100FULL | LPA_100HALF)) {
332                         phydev->speed = SPEED_100;
333
334                         if (lpa & LPA_100FULL)
335                                 phydev->duplex = DUPLEX_FULL;
336
337                 } else if (lpa & LPA_10FULL)
338                         phydev->duplex = DUPLEX_FULL;
339
340                 /*
341                  * Extended status may indicate that the PHY supports
342                  * 1000BASE-T/X even though the 1000BASE-T registers
343                  * are missing. In this case we can't tell whether the
344                  * peer also supports it, so we only check extended
345                  * status if the 1000BASE-T registers are actually
346                  * missing.
347                  */
348                 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
349                         estatus = phy_read(phydev, MDIO_DEVAD_NONE,
350                                            MII_ESTATUS);
351                 if (estatus < 0)
352                         return estatus;
353
354                 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
355                                 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
356                         phydev->speed = SPEED_1000;
357                         if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
358                                 phydev->duplex = DUPLEX_FULL;
359                 }
360
361         } else {
362                 int bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
363
364                 if (bmcr < 0)
365                         return bmcr;
366
367                 phydev->speed = SPEED_10;
368                 phydev->duplex = DUPLEX_HALF;
369
370                 if (bmcr & BMCR_FULLDPLX)
371                         phydev->duplex = DUPLEX_FULL;
372
373                 if (bmcr & BMCR_SPEED1000)
374                         phydev->speed = SPEED_1000;
375                 else if (bmcr & BMCR_SPEED100)
376                         phydev->speed = SPEED_100;
377         }
378
379         return 0;
380 }
381
382 int genphy_config(struct phy_device *phydev)
383 {
384         int val;
385         u32 features;
386
387         /* For now, I'll claim that the generic driver supports
388          * all possible port types */
389         features = (SUPPORTED_TP | SUPPORTED_MII
390                         | SUPPORTED_AUI | SUPPORTED_FIBRE |
391                         SUPPORTED_BNC);
392
393         /* Do we support autonegotiation? */
394         val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
395
396         if (val < 0)
397                 return val;
398
399         if (val & BMSR_ANEGCAPABLE)
400                 features |= SUPPORTED_Autoneg;
401
402         if (val & BMSR_100FULL)
403                 features |= SUPPORTED_100baseT_Full;
404         if (val & BMSR_100HALF)
405                 features |= SUPPORTED_100baseT_Half;
406         if (val & BMSR_10FULL)
407                 features |= SUPPORTED_10baseT_Full;
408         if (val & BMSR_10HALF)
409                 features |= SUPPORTED_10baseT_Half;
410
411         if (val & BMSR_ESTATEN) {
412                 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
413
414                 if (val < 0)
415                         return val;
416
417                 if (val & ESTATUS_1000_TFULL)
418                         features |= SUPPORTED_1000baseT_Full;
419                 if (val & ESTATUS_1000_THALF)
420                         features |= SUPPORTED_1000baseT_Half;
421                 if (val & ESTATUS_1000_XFULL)
422                         features |= SUPPORTED_1000baseX_Full;
423                 if (val & ESTATUS_1000_XHALF)
424                         features |= SUPPORTED_1000baseX_Half;
425         }
426
427         phydev->supported = features;
428         phydev->advertising = features;
429
430         genphy_config_aneg(phydev);
431
432         return 0;
433 }
434
435 int genphy_startup(struct phy_device *phydev)
436 {
437         genphy_update_link(phydev);
438         genphy_parse_link(phydev);
439
440         return 0;
441 }
442
443 int genphy_shutdown(struct phy_device *phydev)
444 {
445         return 0;
446 }
447
448 static struct phy_driver genphy_driver = {
449         .uid            = 0xffffffff,
450         .mask           = 0xffffffff,
451         .name           = "Generic PHY",
452         .features       = 0,
453         .config         = genphy_config,
454         .startup        = genphy_startup,
455         .shutdown       = genphy_shutdown,
456 };
457
458 static LIST_HEAD(phy_drivers);
459
460 int phy_init(void)
461 {
462 #ifdef CONFIG_PHY_ATHEROS
463         phy_atheros_init();
464 #endif
465 #ifdef CONFIG_PHY_BROADCOM
466         phy_broadcom_init();
467 #endif
468 #ifdef CONFIG_PHY_DAVICOM
469         phy_davicom_init();
470 #endif
471 #ifdef CONFIG_PHY_ET1011C
472         phy_et1011c_init();
473 #endif
474 #ifdef CONFIG_PHY_ICPLUS
475         phy_icplus_init();
476 #endif
477 #ifdef CONFIG_PHY_LXT
478         phy_lxt_init();
479 #endif
480 #ifdef CONFIG_PHY_MARVELL
481         phy_marvell_init();
482 #endif
483 #ifdef CONFIG_PHY_MICREL
484         phy_micrel_init();
485 #endif
486 #ifdef CONFIG_PHY_NATSEMI
487         phy_natsemi_init();
488 #endif
489 #ifdef CONFIG_PHY_REALTEK
490         phy_realtek_init();
491 #endif
492 #ifdef CONFIG_PHY_SMSC
493         phy_smsc_init();
494 #endif
495 #ifdef CONFIG_PHY_TERANETICS
496         phy_teranetics_init();
497 #endif
498 #ifdef CONFIG_PHY_VITESSE
499         phy_vitesse_init();
500 #endif
501
502         return 0;
503 }
504
505 int phy_register(struct phy_driver *drv)
506 {
507         INIT_LIST_HEAD(&drv->list);
508         list_add_tail(&drv->list, &phy_drivers);
509
510         return 0;
511 }
512
513 static int phy_probe(struct phy_device *phydev)
514 {
515         int err = 0;
516
517         phydev->advertising = phydev->supported = phydev->drv->features;
518         phydev->mmds = phydev->drv->mmds;
519
520         if (phydev->drv->probe)
521                 err = phydev->drv->probe(phydev);
522
523         return err;
524 }
525
526 static struct phy_driver *generic_for_interface(phy_interface_t interface)
527 {
528 #ifdef CONFIG_PHYLIB_10G
529         if (is_10g_interface(interface))
530                 return &gen10g_driver;
531 #endif
532
533         return &genphy_driver;
534 }
535
536 static struct phy_driver *get_phy_driver(struct phy_device *phydev,
537                                 phy_interface_t interface)
538 {
539         struct list_head *entry;
540         int phy_id = phydev->phy_id;
541         struct phy_driver *drv = NULL;
542
543         list_for_each(entry, &phy_drivers) {
544                 drv = list_entry(entry, struct phy_driver, list);
545                 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
546                         return drv;
547         }
548
549         /* If we made it here, there's no driver for this PHY */
550         return generic_for_interface(interface);
551 }
552
553 static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
554                                             int phy_id,
555                                             phy_interface_t interface)
556 {
557         struct phy_device *dev;
558
559         /* We allocate the device, and initialize the
560          * default values */
561         dev = malloc(sizeof(*dev));
562         if (!dev) {
563                 printf("Failed to allocate PHY device for %s:%d\n",
564                         bus->name, addr);
565                 return NULL;
566         }
567
568         memset(dev, 0, sizeof(*dev));
569
570         dev->duplex = -1;
571         dev->link = 1;
572         dev->interface = interface;
573
574         dev->autoneg = AUTONEG_ENABLE;
575
576         dev->addr = addr;
577         dev->phy_id = phy_id;
578         dev->bus = bus;
579
580         dev->drv = get_phy_driver(dev, interface);
581
582         phy_probe(dev);
583
584         bus->phymap[addr] = dev;
585
586         return dev;
587 }
588
589 /**
590  * get_phy_id - reads the specified addr for its ID.
591  * @bus: the target MII bus
592  * @addr: PHY address on the MII bus
593  * @phy_id: where to store the ID retrieved.
594  *
595  * Description: Reads the ID registers of the PHY at @addr on the
596  *   @bus, stores it in @phy_id and returns zero on success.
597  */
598 static int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
599 {
600         int phy_reg;
601
602         /* Grab the bits from PHYIR1, and put them
603          * in the upper half */
604         phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
605
606         if (phy_reg < 0)
607                 return -EIO;
608
609         *phy_id = (phy_reg & 0xffff) << 16;
610
611         /* Grab the bits from PHYIR2, and put them in the lower half */
612         phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
613
614         if (phy_reg < 0)
615                 return -EIO;
616
617         *phy_id |= (phy_reg & 0xffff);
618
619         return 0;
620 }
621
622 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
623                 unsigned phy_mask, int devad, phy_interface_t interface)
624 {
625         u32 phy_id = 0xffffffff;
626         while (phy_mask) {
627                 int addr = ffs(phy_mask) - 1;
628                 int r = get_phy_id(bus, addr, devad, &phy_id);
629                 if (r < 0)
630                         return ERR_PTR(r);
631                 /* If the PHY ID is mostly f's, we didn't find anything */
632                 if ((phy_id & 0x1fffffff) != 0x1fffffff)
633                         return phy_device_create(bus, addr, phy_id, interface);
634                 phy_mask &= ~(1 << addr);
635         }
636         return NULL;
637 }
638
639 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
640                 unsigned phy_mask, phy_interface_t interface)
641 {
642         /* If we have one, return the existing device, with new interface */
643         while (phy_mask) {
644                 int addr = ffs(phy_mask) - 1;
645                 if (bus->phymap[addr]) {
646                         bus->phymap[addr]->interface = interface;
647                         return bus->phymap[addr];
648                 }
649                 phy_mask &= ~(1 << addr);
650         }
651         return NULL;
652 }
653
654 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
655                 unsigned phy_mask, phy_interface_t interface)
656 {
657         int i;
658         struct phy_device *phydev;
659
660         phydev = search_for_existing_phy(bus, phy_mask, interface);
661         if (phydev)
662                 return phydev;
663         /* Try Standard (ie Clause 22) access */
664         /* Otherwise we have to try Clause 45 */
665         for (i = 0; i < 5; i++) {
666                 phydev = create_phy_by_mask(bus, phy_mask,
667                                 i ? i : MDIO_DEVAD_NONE, interface);
668                 if (IS_ERR(phydev))
669                         return NULL;
670                 if (phydev)
671                         return phydev;
672         }
673         printf("Phy not found\n");
674         return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
675 }
676
677 /**
678  * get_phy_device - reads the specified PHY device and returns its @phy_device struct
679  * @bus: the target MII bus
680  * @addr: PHY address on the MII bus
681  *
682  * Description: Reads the ID registers of the PHY at @addr on the
683  *   @bus, then allocates and returns the phy_device to represent it.
684  */
685 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
686                                          phy_interface_t interface)
687 {
688         return get_phy_device_by_mask(bus, 1 << addr, interface);
689 }
690
691 int phy_reset(struct phy_device *phydev)
692 {
693         int err;
694         int reg;
695         int timeout = 500;
696         int devad = MDIO_DEVAD_NONE;
697
698 #ifdef CONFIG_PHYLIB_10G
699         /* If it's 10G, we need to issue reset through one of the MMDs */
700         if (is_10g_interface(phydev->interface)) {
701                 if (!phydev->mmds)
702                         gen10g_discover_mmds(phydev);
703
704                 devad = ffs(phydev->mmds) - 1;
705         }
706 #endif
707
708         reg = phy_read(phydev, devad, MII_BMCR);
709         if (reg < 0) {
710                 debug("PHY status read failed\n");
711                 return -1;
712         }
713
714         reg |= BMCR_RESET;
715
716         err = phy_write(phydev, devad, MII_BMCR, reg);
717         if (err < 0) {
718                 debug("PHY reset failed\n");
719                 return err;
720         }
721
722 #ifdef CONFIG_PHY_RESET_DELAY
723         udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
724 #endif
725         /*
726          * Poll the control register for the reset bit to go to 0 (it is
727          * auto-clearing).  This should happen within 0.5 seconds per the
728          * IEEE spec.
729          */
730         while ((reg & BMCR_RESET) && timeout-- >= 0) {
731                 reg = phy_read(phydev, devad, MII_BMCR);
732
733                 if (reg < 0) {
734                         debug("PHY status read failed\n");
735                         return reg;
736                 }
737                 udelay(1000);
738         }
739
740         if (reg & BMCR_RESET) {
741                 puts("PHY reset timed out\n");
742                 return -ETIMEDOUT;
743         }
744
745         return 0;
746 }
747
748 int miiphy_reset(const char *devname, unsigned char addr)
749 {
750         struct mii_dev *bus = miiphy_get_dev_by_name(devname);
751         struct phy_device *phydev;
752
753         /*
754          * miiphy_reset was only used on standard PHYs, so we'll fake it here.
755          * If later code tries to connect with the right interface, this will
756          * be corrected by get_phy_device in phy_connect()
757          */
758         phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
759
760         return phy_reset(phydev);
761 }
762
763 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
764                 phy_interface_t interface)
765 {
766         /* Reset the bus */
767         if (bus->reset)
768                 bus->reset(bus);
769
770         /* Wait 15ms to make sure the PHY has come out of hard reset */
771         udelay(15000);
772         return get_phy_device_by_mask(bus, phy_mask, interface);
773 }
774
775 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
776 {
777         /* Soft Reset the PHY */
778         phy_reset(phydev);
779         if (phydev->dev) {
780                 printf("%s:%d is connected to %s.  Reconnecting to %s\n",
781                                 phydev->bus->name, phydev->addr,
782                                 phydev->dev->name, dev->name);
783         }
784         phydev->dev = dev;
785         debug("%s connected to %s\n", dev->name, phydev->drv->name);
786 }
787
788 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
789                 struct eth_device *dev, phy_interface_t interface)
790 {
791         struct phy_device *phydev;
792
793         phydev = phy_find_by_mask(bus, 1 << addr, interface);
794         if (phydev)
795                 phy_connect_dev(phydev, dev);
796         else
797                 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
798         return phydev;
799 }
800
801 /*
802  * Start the PHY.  Returns 0 on success, or a negative error code.
803  */
804 int phy_startup(struct phy_device *phydev)
805 {
806         if (phydev->drv->startup)
807                 return phydev->drv->startup(phydev);
808
809         return 0;
810 }
811
812 static int __board_phy_config(struct phy_device *phydev)
813 {
814         if (phydev->drv->config)
815                 return phydev->drv->config(phydev);
816         return 0;
817 }
818
819 int board_phy_config(struct phy_device *phydev)
820         __attribute__((weak, alias("__board_phy_config")));
821
822 int phy_config(struct phy_device *phydev)
823 {
824         /* Invoke an optional board-specific helper */
825         board_phy_config(phydev);
826
827         return 0;
828 }
829
830 int phy_shutdown(struct phy_device *phydev)
831 {
832         if (phydev->drv->shutdown)
833                 phydev->drv->shutdown(phydev);
834
835         return 0;
836 }