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