xref: /openbmc/u-boot/drivers/net/phy/phy.c (revision 04735e9c5578dd4f3584be5454b9779e8e5c2af9)
1 /*
2  * Generic PHY Management code
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307 USA
18  *
19  *
20  * Copyright 2011 Freescale Semiconductor, Inc.
21  * author Andy Fleming
22  *
23  * Based loosely off of Linux's PHY Lib
24  */
25 
26 #include <config.h>
27 #include <common.h>
28 #include <malloc.h>
29 #include <net.h>
30 #include <command.h>
31 #include <miiphy.h>
32 #include <phy.h>
33 #include <errno.h>
34 #include <linux/err.h>
35 
36 /* Generic PHY support and helper functions */
37 
38 /**
39  * genphy_config_advert - sanitize and advertise auto-negotation parameters
40  * @phydev: target phy_device struct
41  *
42  * Description: Writes MII_ADVERTISE with the appropriate values,
43  *   after sanitizing the values to make sure we only advertise
44  *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
45  *   hasn't changed, and > 0 if it has changed.
46  */
47 static int genphy_config_advert(struct phy_device *phydev)
48 {
49 	u32 advertise;
50 	int oldadv, adv;
51 	int err, changed = 0;
52 
53 	/* Only allow advertising what
54 	 * this PHY supports */
55 	phydev->advertising &= phydev->supported;
56 	advertise = phydev->advertising;
57 
58 	/* Setup standard advertisement */
59 	oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
60 
61 	if (adv < 0)
62 		return adv;
63 
64 	adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
65 		 ADVERTISE_PAUSE_ASYM);
66 	if (advertise & ADVERTISED_10baseT_Half)
67 		adv |= ADVERTISE_10HALF;
68 	if (advertise & ADVERTISED_10baseT_Full)
69 		adv |= ADVERTISE_10FULL;
70 	if (advertise & ADVERTISED_100baseT_Half)
71 		adv |= ADVERTISE_100HALF;
72 	if (advertise & ADVERTISED_100baseT_Full)
73 		adv |= ADVERTISE_100FULL;
74 	if (advertise & ADVERTISED_Pause)
75 		adv |= ADVERTISE_PAUSE_CAP;
76 	if (advertise & ADVERTISED_Asym_Pause)
77 		adv |= ADVERTISE_PAUSE_ASYM;
78 	if (advertise & ADVERTISED_1000baseX_Half)
79 		adv |= ADVERTISE_1000XHALF;
80 	if (advertise & ADVERTISED_1000baseX_Full)
81 		adv |= ADVERTISE_1000XFULL;
82 
83 	if (adv != oldadv) {
84 		err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
85 
86 		if (err < 0)
87 			return err;
88 		changed = 1;
89 	}
90 
91 	/* Configure gigabit if it's supported */
92 	if (phydev->supported & (SUPPORTED_1000baseT_Half |
93 				SUPPORTED_1000baseT_Full)) {
94 		oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
95 
96 		if (adv < 0)
97 			return adv;
98 
99 		adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
100 		if (advertise & SUPPORTED_1000baseT_Half)
101 			adv |= ADVERTISE_1000HALF;
102 		if (advertise & SUPPORTED_1000baseT_Full)
103 			adv |= ADVERTISE_1000FULL;
104 
105 		if (adv != oldadv) {
106 			err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000,
107 					adv);
108 
109 			if (err < 0)
110 				return err;
111 			changed = 1;
112 		}
113 	}
114 
115 	return changed;
116 }
117 
118 
119 /**
120  * genphy_setup_forced - configures/forces speed/duplex from @phydev
121  * @phydev: target phy_device struct
122  *
123  * Description: Configures MII_BMCR to force speed/duplex
124  *   to the values in phydev. Assumes that the values are valid.
125  */
126 static int genphy_setup_forced(struct phy_device *phydev)
127 {
128 	int err;
129 	int ctl = 0;
130 
131 	phydev->pause = phydev->asym_pause = 0;
132 
133 	if (SPEED_1000 == phydev->speed)
134 		ctl |= BMCR_SPEED1000;
135 	else if (SPEED_100 == phydev->speed)
136 		ctl |= BMCR_SPEED100;
137 
138 	if (DUPLEX_FULL == phydev->duplex)
139 		ctl |= BMCR_FULLDPLX;
140 
141 	err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
142 
143 	return err;
144 }
145 
146 
147 /**
148  * genphy_restart_aneg - Enable and Restart Autonegotiation
149  * @phydev: target phy_device struct
150  */
151 int genphy_restart_aneg(struct phy_device *phydev)
152 {
153 	int ctl;
154 
155 	ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
156 
157 	if (ctl < 0)
158 		return ctl;
159 
160 	ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
161 
162 	/* Don't isolate the PHY if we're negotiating */
163 	ctl &= ~(BMCR_ISOLATE);
164 
165 	ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
166 
167 	return ctl;
168 }
169 
170 
171 /**
172  * genphy_config_aneg - restart auto-negotiation or write BMCR
173  * @phydev: target phy_device struct
174  *
175  * Description: If auto-negotiation is enabled, we configure the
176  *   advertising, and then restart auto-negotiation.  If it is not
177  *   enabled, then we write the BMCR.
178  */
179 int genphy_config_aneg(struct phy_device *phydev)
180 {
181 	int result;
182 
183 	if (AUTONEG_ENABLE != phydev->autoneg)
184 		return genphy_setup_forced(phydev);
185 
186 	result = genphy_config_advert(phydev);
187 
188 	if (result < 0) /* error */
189 		return result;
190 
191 	if (result == 0) {
192 		/* Advertisment hasn't changed, but maybe aneg was never on to
193 		 * begin with?  Or maybe phy was isolated? */
194 		int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
195 
196 		if (ctl < 0)
197 			return ctl;
198 
199 		if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
200 			result = 1; /* do restart aneg */
201 	}
202 
203 	/* Only restart aneg if we are advertising something different
204 	 * than we were before.	 */
205 	if (result > 0)
206 		result = genphy_restart_aneg(phydev);
207 
208 	return result;
209 }
210 
211 /**
212  * genphy_update_link - update link status in @phydev
213  * @phydev: target phy_device struct
214  *
215  * Description: Update the value in phydev->link to reflect the
216  *   current link value.  In order to do this, we need to read
217  *   the status register twice, keeping the second value.
218  */
219 int genphy_update_link(struct phy_device *phydev)
220 {
221 	unsigned int mii_reg;
222 
223 	/*
224 	 * Wait if the link is up, and autonegotiation is in progress
225 	 * (ie - we're capable and it's not done)
226 	 */
227 	mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
228 
229 	/*
230 	 * If we already saw the link up, and it hasn't gone down, then
231 	 * we don't need to wait for autoneg again
232 	 */
233 	if (phydev->link && mii_reg & BMSR_LSTATUS)
234 		return 0;
235 
236 	if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
237 		int i = 0;
238 
239 		printf("%s Waiting for PHY auto negotiation to complete",
240 			phydev->dev->name);
241 		while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
242 			/*
243 			 * Timeout reached ?
244 			 */
245 			if (i > PHY_ANEG_TIMEOUT) {
246 				printf(" TIMEOUT !\n");
247 				phydev->link = 0;
248 				return 0;
249 			}
250 
251 			if (ctrlc()) {
252 				puts("user interrupt!\n");
253 				phydev->link = 0;
254 				return -EINTR;
255 			}
256 
257 			if ((i++ % 500) == 0)
258 				printf(".");
259 
260 			udelay(1000);	/* 1 ms */
261 			mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
262 		}
263 		printf(" done\n");
264 		phydev->link = 1;
265 	} else {
266 		/* Read the link a second time to clear the latched state */
267 		mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
268 
269 		if (mii_reg & BMSR_LSTATUS)
270 			phydev->link = 1;
271 		else
272 			phydev->link = 0;
273 	}
274 
275 	return 0;
276 }
277 
278 /*
279  * Generic function which updates the speed and duplex.  If
280  * autonegotiation is enabled, it uses the AND of the link
281  * partner's advertised capabilities and our advertised
282  * capabilities.  If autonegotiation is disabled, we use the
283  * appropriate bits in the control register.
284  *
285  * Stolen from Linux's mii.c and phy_device.c
286  */
287 int genphy_parse_link(struct phy_device *phydev)
288 {
289 	int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
290 
291 	/* We're using autonegotiation */
292 	if (mii_reg & BMSR_ANEGCAPABLE) {
293 		u32 lpa = 0;
294 		u32 gblpa = 0;
295 		u32 estatus = 0;
296 
297 		/* Check for gigabit capability */
298 		if (mii_reg & BMSR_ERCAP) {
299 			/* We want a list of states supported by
300 			 * both PHYs in the link
301 			 */
302 			gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
303 			gblpa &= phy_read(phydev,
304 					MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
305 		}
306 
307 		/* Set the baseline so we only have to set them
308 		 * if they're different
309 		 */
310 		phydev->speed = SPEED_10;
311 		phydev->duplex = DUPLEX_HALF;
312 
313 		/* Check the gigabit fields */
314 		if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
315 			phydev->speed = SPEED_1000;
316 
317 			if (gblpa & PHY_1000BTSR_1000FD)
318 				phydev->duplex = DUPLEX_FULL;
319 
320 			/* We're done! */
321 			return 0;
322 		}
323 
324 		lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
325 		lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
326 
327 		if (lpa & (LPA_100FULL | LPA_100HALF)) {
328 			phydev->speed = SPEED_100;
329 
330 			if (lpa & LPA_100FULL)
331 				phydev->duplex = DUPLEX_FULL;
332 
333 		} else if (lpa & LPA_10FULL)
334 			phydev->duplex = DUPLEX_FULL;
335 
336 		if (mii_reg & BMSR_ESTATEN)
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_Full;
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_ICPLUS
458 	phy_icplus_init();
459 #endif
460 #ifdef CONFIG_PHY_LXT
461 	phy_lxt_init();
462 #endif
463 #ifdef CONFIG_PHY_MARVELL
464 	phy_marvell_init();
465 #endif
466 #ifdef CONFIG_PHY_MICREL
467 	phy_micrel_init();
468 #endif
469 #ifdef CONFIG_PHY_NATSEMI
470 	phy_natsemi_init();
471 #endif
472 #ifdef CONFIG_PHY_REALTEK
473 	phy_realtek_init();
474 #endif
475 #ifdef CONFIG_PHY_SMSC
476 	phy_smsc_init();
477 #endif
478 #ifdef CONFIG_PHY_TERANETICS
479 	phy_teranetics_init();
480 #endif
481 #ifdef CONFIG_PHY_VITESSE
482 	phy_vitesse_init();
483 #endif
484 
485 	return 0;
486 }
487 
488 int phy_register(struct phy_driver *drv)
489 {
490 	INIT_LIST_HEAD(&drv->list);
491 	list_add_tail(&drv->list, &phy_drivers);
492 
493 	return 0;
494 }
495 
496 static int phy_probe(struct phy_device *phydev)
497 {
498 	int err = 0;
499 
500 	phydev->advertising = phydev->supported = phydev->drv->features;
501 	phydev->mmds = phydev->drv->mmds;
502 
503 	if (phydev->drv->probe)
504 		err = phydev->drv->probe(phydev);
505 
506 	return err;
507 }
508 
509 static struct phy_driver *generic_for_interface(phy_interface_t interface)
510 {
511 #ifdef CONFIG_PHYLIB_10G
512 	if (is_10g_interface(interface))
513 		return &gen10g_driver;
514 #endif
515 
516 	return &genphy_driver;
517 }
518 
519 static struct phy_driver *get_phy_driver(struct phy_device *phydev,
520 				phy_interface_t interface)
521 {
522 	struct list_head *entry;
523 	int phy_id = phydev->phy_id;
524 	struct phy_driver *drv = NULL;
525 
526 	list_for_each(entry, &phy_drivers) {
527 		drv = list_entry(entry, struct phy_driver, list);
528 		if ((drv->uid & drv->mask) == (phy_id & drv->mask))
529 			return drv;
530 	}
531 
532 	/* If we made it here, there's no driver for this PHY */
533 	return generic_for_interface(interface);
534 }
535 
536 static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
537 					    int phy_id,
538 					    phy_interface_t interface)
539 {
540 	struct phy_device *dev;
541 
542 	/* We allocate the device, and initialize the
543 	 * default values */
544 	dev = malloc(sizeof(*dev));
545 	if (!dev) {
546 		printf("Failed to allocate PHY device for %s:%d\n",
547 			bus->name, addr);
548 		return NULL;
549 	}
550 
551 	memset(dev, 0, sizeof(*dev));
552 
553 	dev->duplex = -1;
554 	dev->link = 1;
555 	dev->interface = interface;
556 
557 	dev->autoneg = AUTONEG_ENABLE;
558 
559 	dev->addr = addr;
560 	dev->phy_id = phy_id;
561 	dev->bus = bus;
562 
563 	dev->drv = get_phy_driver(dev, interface);
564 
565 	phy_probe(dev);
566 
567 	bus->phymap[addr] = dev;
568 
569 	return dev;
570 }
571 
572 /**
573  * get_phy_id - reads the specified addr for its ID.
574  * @bus: the target MII bus
575  * @addr: PHY address on the MII bus
576  * @phy_id: where to store the ID retrieved.
577  *
578  * Description: Reads the ID registers of the PHY at @addr on the
579  *   @bus, stores it in @phy_id and returns zero on success.
580  */
581 static int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
582 {
583 	int phy_reg;
584 
585 	/* Grab the bits from PHYIR1, and put them
586 	 * in the upper half */
587 	phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
588 
589 	if (phy_reg < 0)
590 		return -EIO;
591 
592 	*phy_id = (phy_reg & 0xffff) << 16;
593 
594 	/* Grab the bits from PHYIR2, and put them in the lower half */
595 	phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
596 
597 	if (phy_reg < 0)
598 		return -EIO;
599 
600 	*phy_id |= (phy_reg & 0xffff);
601 
602 	return 0;
603 }
604 
605 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
606 		unsigned phy_mask, int devad, phy_interface_t interface)
607 {
608 	u32 phy_id = 0xffffffff;
609 	while (phy_mask) {
610 		int addr = ffs(phy_mask) - 1;
611 		int r = get_phy_id(bus, addr, devad, &phy_id);
612 		if (r < 0)
613 			return ERR_PTR(r);
614 		/* If the PHY ID is mostly f's, we didn't find anything */
615 		if ((phy_id & 0x1fffffff) != 0x1fffffff)
616 			return phy_device_create(bus, addr, phy_id, interface);
617 		phy_mask &= ~(1 << addr);
618 	}
619 	return NULL;
620 }
621 
622 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
623 		unsigned phy_mask, phy_interface_t interface)
624 {
625 	/* If we have one, return the existing device, with new interface */
626 	while (phy_mask) {
627 		int addr = ffs(phy_mask) - 1;
628 		if (bus->phymap[addr]) {
629 			bus->phymap[addr]->interface = interface;
630 			return bus->phymap[addr];
631 		}
632 		phy_mask &= ~(1 << addr);
633 	}
634 	return NULL;
635 }
636 
637 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
638 		unsigned phy_mask, phy_interface_t interface)
639 {
640 	int i;
641 	struct phy_device *phydev;
642 
643 	phydev = search_for_existing_phy(bus, phy_mask, interface);
644 	if (phydev)
645 		return phydev;
646 	/* Try Standard (ie Clause 22) access */
647 	/* Otherwise we have to try Clause 45 */
648 	for (i = 0; i < 5; i++) {
649 		phydev = create_phy_by_mask(bus, phy_mask,
650 				i ? i : MDIO_DEVAD_NONE, interface);
651 		if (IS_ERR(phydev))
652 			return NULL;
653 		if (phydev)
654 			return phydev;
655 	}
656 	printf("Phy not found\n");
657 	return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
658 }
659 
660 /**
661  * get_phy_device - reads the specified PHY device and returns its @phy_device struct
662  * @bus: the target MII bus
663  * @addr: PHY address on the MII bus
664  *
665  * Description: Reads the ID registers of the PHY at @addr on the
666  *   @bus, then allocates and returns the phy_device to represent it.
667  */
668 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
669 					 phy_interface_t interface)
670 {
671 	return get_phy_device_by_mask(bus, 1 << addr, interface);
672 }
673 
674 int phy_reset(struct phy_device *phydev)
675 {
676 	int reg;
677 	int timeout = 500;
678 	int devad = MDIO_DEVAD_NONE;
679 
680 #ifdef CONFIG_PHYLIB_10G
681 	/* If it's 10G, we need to issue reset through one of the MMDs */
682 	if (is_10g_interface(phydev->interface)) {
683 		if (!phydev->mmds)
684 			gen10g_discover_mmds(phydev);
685 
686 		devad = ffs(phydev->mmds) - 1;
687 	}
688 #endif
689 
690 	reg = phy_read(phydev, devad, MII_BMCR);
691 	if (reg < 0) {
692 		debug("PHY status read failed\n");
693 		return -1;
694 	}
695 
696 	reg |= BMCR_RESET;
697 
698 	if (phy_write(phydev, devad, MII_BMCR, reg) < 0) {
699 		debug("PHY reset failed\n");
700 		return -1;
701 	}
702 
703 #ifdef CONFIG_PHY_RESET_DELAY
704 	udelay(CONFIG_PHY_RESET_DELAY);	/* Intel LXT971A needs this */
705 #endif
706 	/*
707 	 * Poll the control register for the reset bit to go to 0 (it is
708 	 * auto-clearing).  This should happen within 0.5 seconds per the
709 	 * IEEE spec.
710 	 */
711 	while ((reg & BMCR_RESET) && timeout--) {
712 		reg = phy_read(phydev, devad, MII_BMCR);
713 
714 		if (reg < 0) {
715 			debug("PHY status read failed\n");
716 			return -1;
717 		}
718 		udelay(1000);
719 	}
720 
721 	if (reg & BMCR_RESET) {
722 		puts("PHY reset timed out\n");
723 		return -1;
724 	}
725 
726 	return 0;
727 }
728 
729 int miiphy_reset(const char *devname, unsigned char addr)
730 {
731 	struct mii_dev *bus = miiphy_get_dev_by_name(devname);
732 	struct phy_device *phydev;
733 
734 	/*
735 	 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
736 	 * If later code tries to connect with the right interface, this will
737 	 * be corrected by get_phy_device in phy_connect()
738 	 */
739 	phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
740 
741 	return phy_reset(phydev);
742 }
743 
744 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
745 		phy_interface_t interface)
746 {
747 	/* Reset the bus */
748 	if (bus->reset)
749 		bus->reset(bus);
750 
751 	/* Wait 15ms to make sure the PHY has come out of hard reset */
752 	udelay(15000);
753 	return get_phy_device_by_mask(bus, phy_mask, interface);
754 }
755 
756 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
757 {
758 	/* Soft Reset the PHY */
759 	phy_reset(phydev);
760 	if (phydev->dev) {
761 		printf("%s:%d is connected to %s.  Reconnecting to %s\n",
762 				phydev->bus->name, phydev->addr,
763 				phydev->dev->name, dev->name);
764 	}
765 	phydev->dev = dev;
766 	debug("%s connected to %s\n", dev->name, phydev->drv->name);
767 }
768 
769 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
770 		struct eth_device *dev, phy_interface_t interface)
771 {
772 	struct phy_device *phydev;
773 
774 	phydev = phy_find_by_mask(bus, 1 << addr, interface);
775 	if (phydev)
776 		phy_connect_dev(phydev, dev);
777 	else
778 		printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
779 	return phydev;
780 }
781 
782 /*
783  * Start the PHY.  Returns 0 on success, or a negative error code.
784  */
785 int phy_startup(struct phy_device *phydev)
786 {
787 	if (phydev->drv->startup)
788 		return phydev->drv->startup(phydev);
789 
790 	return 0;
791 }
792 
793 static int __board_phy_config(struct phy_device *phydev)
794 {
795 	if (phydev->drv->config)
796 		return phydev->drv->config(phydev);
797 	return 0;
798 }
799 
800 int board_phy_config(struct phy_device *phydev)
801 	__attribute__((weak, alias("__board_phy_config")));
802 
803 int phy_config(struct phy_device *phydev)
804 {
805 	/* Invoke an optional board-specific helper */
806 	board_phy_config(phydev);
807 
808 	return 0;
809 }
810 
811 int phy_shutdown(struct phy_device *phydev)
812 {
813 	if (phydev->drv->shutdown)
814 		phydev->drv->shutdown(phydev);
815 
816 	return 0;
817 }
818