blob: f8c54814777882e83d17e8ad22b69abcd047f42f [file] [log] [blame]
Andy Fleming5f184712011-04-08 02:10:27 -05001/*
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>
Troy Kisky1adb4062012-10-22 16:40:43 +000034#include <linux/err.h>
Andy Fleming5f184712011-04-08 02:10:27 -050035
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 */
Kim Phillips960d70c2012-10-29 13:34:34 +000047static int genphy_config_advert(struct phy_device *phydev)
Andy Fleming5f184712011-04-08 02:10:27 -050048{
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
79 if (adv != oldadv) {
80 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
81
82 if (err < 0)
83 return err;
84 changed = 1;
85 }
86
87 /* Configure gigabit if it's supported */
88 if (phydev->supported & (SUPPORTED_1000baseT_Half |
89 SUPPORTED_1000baseT_Full)) {
90 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
91
92 if (adv < 0)
93 return adv;
94
95 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
96 if (advertise & SUPPORTED_1000baseT_Half)
97 adv |= ADVERTISE_1000HALF;
98 if (advertise & SUPPORTED_1000baseT_Full)
99 adv |= ADVERTISE_1000FULL;
100
101 if (adv != oldadv) {
102 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000,
103 adv);
104
105 if (err < 0)
106 return err;
107 changed = 1;
108 }
109 }
110
111 return changed;
112}
113
114
115/**
116 * genphy_setup_forced - configures/forces speed/duplex from @phydev
117 * @phydev: target phy_device struct
118 *
119 * Description: Configures MII_BMCR to force speed/duplex
120 * to the values in phydev. Assumes that the values are valid.
121 */
Kim Phillips960d70c2012-10-29 13:34:34 +0000122static int genphy_setup_forced(struct phy_device *phydev)
Andy Fleming5f184712011-04-08 02:10:27 -0500123{
124 int err;
125 int ctl = 0;
126
127 phydev->pause = phydev->asym_pause = 0;
128
129 if (SPEED_1000 == phydev->speed)
130 ctl |= BMCR_SPEED1000;
131 else if (SPEED_100 == phydev->speed)
132 ctl |= BMCR_SPEED100;
133
134 if (DUPLEX_FULL == phydev->duplex)
135 ctl |= BMCR_FULLDPLX;
136
137 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
138
139 return err;
140}
141
142
143/**
144 * genphy_restart_aneg - Enable and Restart Autonegotiation
145 * @phydev: target phy_device struct
146 */
147int genphy_restart_aneg(struct phy_device *phydev)
148{
149 int ctl;
150
151 ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
152
153 if (ctl < 0)
154 return ctl;
155
156 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
157
158 /* Don't isolate the PHY if we're negotiating */
159 ctl &= ~(BMCR_ISOLATE);
160
161 ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
162
163 return ctl;
164}
165
166
167/**
168 * genphy_config_aneg - restart auto-negotiation or write BMCR
169 * @phydev: target phy_device struct
170 *
171 * Description: If auto-negotiation is enabled, we configure the
172 * advertising, and then restart auto-negotiation. If it is not
173 * enabled, then we write the BMCR.
174 */
175int genphy_config_aneg(struct phy_device *phydev)
176{
177 int result;
178
179 if (AUTONEG_ENABLE != phydev->autoneg)
180 return genphy_setup_forced(phydev);
181
182 result = genphy_config_advert(phydev);
183
184 if (result < 0) /* error */
185 return result;
186
187 if (result == 0) {
188 /* Advertisment hasn't changed, but maybe aneg was never on to
189 * begin with? Or maybe phy was isolated? */
190 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
191
192 if (ctl < 0)
193 return ctl;
194
195 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
196 result = 1; /* do restart aneg */
197 }
198
199 /* Only restart aneg if we are advertising something different
200 * than we were before. */
201 if (result > 0)
202 result = genphy_restart_aneg(phydev);
203
204 return result;
205}
206
207/**
208 * genphy_update_link - update link status in @phydev
209 * @phydev: target phy_device struct
210 *
211 * Description: Update the value in phydev->link to reflect the
212 * current link value. In order to do this, we need to read
213 * the status register twice, keeping the second value.
214 */
215int genphy_update_link(struct phy_device *phydev)
216{
217 unsigned int mii_reg;
218
219 /*
220 * Wait if the link is up, and autonegotiation is in progress
221 * (ie - we're capable and it's not done)
222 */
223 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
224
225 /*
226 * If we already saw the link up, and it hasn't gone down, then
227 * we don't need to wait for autoneg again
228 */
229 if (phydev->link && mii_reg & BMSR_LSTATUS)
230 return 0;
231
232 if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
233 int i = 0;
234
235 printf("%s Waiting for PHY auto negotiation to complete",
236 phydev->dev->name);
237 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
238 /*
239 * Timeout reached ?
240 */
241 if (i > PHY_ANEG_TIMEOUT) {
242 printf(" TIMEOUT !\n");
243 phydev->link = 0;
244 return 0;
245 }
246
247 if (ctrlc()) {
248 puts("user interrupt!\n");
249 phydev->link = 0;
250 return -EINTR;
251 }
252
253 if ((i++ % 500) == 0)
254 printf(".");
255
256 udelay(1000); /* 1 ms */
257 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
258 }
259 printf(" done\n");
260 phydev->link = 1;
261 } else {
262 /* Read the link a second time to clear the latched state */
263 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
264
265 if (mii_reg & BMSR_LSTATUS)
266 phydev->link = 1;
267 else
268 phydev->link = 0;
269 }
270
271 return 0;
272}
273
274/*
275 * Generic function which updates the speed and duplex. If
276 * autonegotiation is enabled, it uses the AND of the link
277 * partner's advertised capabilities and our advertised
278 * capabilities. If autonegotiation is disabled, we use the
279 * appropriate bits in the control register.
280 *
281 * Stolen from Linux's mii.c and phy_device.c
282 */
283static int genphy_parse_link(struct phy_device *phydev)
284{
285 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
286
287 /* We're using autonegotiation */
288 if (mii_reg & BMSR_ANEGCAPABLE) {
289 u32 lpa = 0;
290 u32 gblpa = 0;
291
292 /* Check for gigabit capability */
293 if (mii_reg & BMSR_ERCAP) {
294 /* We want a list of states supported by
295 * both PHYs in the link
296 */
297 gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
298 gblpa &= phy_read(phydev,
299 MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
300 }
301
302 /* Set the baseline so we only have to set them
303 * if they're different
304 */
305 phydev->speed = SPEED_10;
306 phydev->duplex = DUPLEX_HALF;
307
308 /* Check the gigabit fields */
309 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
310 phydev->speed = SPEED_1000;
311
312 if (gblpa & PHY_1000BTSR_1000FD)
313 phydev->duplex = DUPLEX_FULL;
314
315 /* We're done! */
316 return 0;
317 }
318
319 lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
320 lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
321
Wolfgang Denk0dcfb0f2011-09-28 21:02:43 +0200322 if (lpa & (LPA_100FULL | LPA_100HALF)) {
Andy Fleming5f184712011-04-08 02:10:27 -0500323 phydev->speed = SPEED_100;
324
Wolfgang Denk0dcfb0f2011-09-28 21:02:43 +0200325 if (lpa & LPA_100FULL)
326 phydev->duplex = DUPLEX_FULL;
327
328 } else if (lpa & LPA_10FULL)
Andy Fleming5f184712011-04-08 02:10:27 -0500329 phydev->duplex = DUPLEX_FULL;
330 } else {
331 u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
332
333 phydev->speed = SPEED_10;
334 phydev->duplex = DUPLEX_HALF;
335
336 if (bmcr & BMCR_FULLDPLX)
337 phydev->duplex = DUPLEX_FULL;
338
339 if (bmcr & BMCR_SPEED1000)
340 phydev->speed = SPEED_1000;
341 else if (bmcr & BMCR_SPEED100)
342 phydev->speed = SPEED_100;
343 }
344
345 return 0;
346}
347
348int genphy_config(struct phy_device *phydev)
349{
350 int val;
351 u32 features;
352
353 /* For now, I'll claim that the generic driver supports
354 * all possible port types */
355 features = (SUPPORTED_TP | SUPPORTED_MII
356 | SUPPORTED_AUI | SUPPORTED_FIBRE |
357 SUPPORTED_BNC);
358
359 /* Do we support autonegotiation? */
360 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
361
362 if (val < 0)
363 return val;
364
365 if (val & BMSR_ANEGCAPABLE)
366 features |= SUPPORTED_Autoneg;
367
368 if (val & BMSR_100FULL)
369 features |= SUPPORTED_100baseT_Full;
370 if (val & BMSR_100HALF)
371 features |= SUPPORTED_100baseT_Half;
372 if (val & BMSR_10FULL)
373 features |= SUPPORTED_10baseT_Full;
374 if (val & BMSR_10HALF)
375 features |= SUPPORTED_10baseT_Half;
376
377 if (val & BMSR_ESTATEN) {
378 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
379
380 if (val < 0)
381 return val;
382
383 if (val & ESTATUS_1000_TFULL)
384 features |= SUPPORTED_1000baseT_Full;
385 if (val & ESTATUS_1000_THALF)
386 features |= SUPPORTED_1000baseT_Half;
387 }
388
389 phydev->supported = features;
390 phydev->advertising = features;
391
392 genphy_config_aneg(phydev);
393
394 return 0;
395}
396
397int genphy_startup(struct phy_device *phydev)
398{
399 genphy_update_link(phydev);
400 genphy_parse_link(phydev);
401
402 return 0;
403}
404
405int genphy_shutdown(struct phy_device *phydev)
406{
407 return 0;
408}
409
410static struct phy_driver genphy_driver = {
411 .uid = 0xffffffff,
412 .mask = 0xffffffff,
413 .name = "Generic PHY",
414 .features = 0,
415 .config = genphy_config,
416 .startup = genphy_startup,
417 .shutdown = genphy_shutdown,
418};
419
420static LIST_HEAD(phy_drivers);
421
422int phy_init(void)
423{
Andy Fleming9082eea2011-04-07 21:56:05 -0500424#ifdef CONFIG_PHY_ATHEROS
425 phy_atheros_init();
426#endif
427#ifdef CONFIG_PHY_BROADCOM
428 phy_broadcom_init();
429#endif
430#ifdef CONFIG_PHY_DAVICOM
431 phy_davicom_init();
432#endif
Matt Porterf485c8a2013-03-20 05:38:13 +0000433#ifdef CONFIG_PHY_ET1011C
434 phy_et1011c_init();
435#endif
Andy Fleming9082eea2011-04-07 21:56:05 -0500436#ifdef CONFIG_PHY_LXT
437 phy_lxt_init();
438#endif
439#ifdef CONFIG_PHY_MARVELL
440 phy_marvell_init();
441#endif
442#ifdef CONFIG_PHY_MICREL
443 phy_micrel_init();
444#endif
445#ifdef CONFIG_PHY_NATSEMI
446 phy_natsemi_init();
447#endif
448#ifdef CONFIG_PHY_REALTEK
449 phy_realtek_init();
450#endif
Nobuhiro Iwamatsu5751aa22011-11-23 21:24:15 +0000451#ifdef CONFIG_PHY_SMSC
452 phy_smsc_init();
453#endif
Andy Fleming9082eea2011-04-07 21:56:05 -0500454#ifdef CONFIG_PHY_TERANETICS
455 phy_teranetics_init();
456#endif
457#ifdef CONFIG_PHY_VITESSE
458 phy_vitesse_init();
459#endif
460
Andy Fleming5f184712011-04-08 02:10:27 -0500461 return 0;
462}
463
464int phy_register(struct phy_driver *drv)
465{
466 INIT_LIST_HEAD(&drv->list);
467 list_add_tail(&drv->list, &phy_drivers);
468
469 return 0;
470}
471
Kim Phillips960d70c2012-10-29 13:34:34 +0000472static int phy_probe(struct phy_device *phydev)
Andy Fleming5f184712011-04-08 02:10:27 -0500473{
474 int err = 0;
475
476 phydev->advertising = phydev->supported = phydev->drv->features;
477 phydev->mmds = phydev->drv->mmds;
478
479 if (phydev->drv->probe)
480 err = phydev->drv->probe(phydev);
481
482 return err;
483}
484
485static struct phy_driver *generic_for_interface(phy_interface_t interface)
486{
487#ifdef CONFIG_PHYLIB_10G
488 if (is_10g_interface(interface))
489 return &gen10g_driver;
490#endif
491
492 return &genphy_driver;
493}
494
Kim Phillips960d70c2012-10-29 13:34:34 +0000495static struct phy_driver *get_phy_driver(struct phy_device *phydev,
Andy Fleming5f184712011-04-08 02:10:27 -0500496 phy_interface_t interface)
497{
498 struct list_head *entry;
499 int phy_id = phydev->phy_id;
500 struct phy_driver *drv = NULL;
501
502 list_for_each(entry, &phy_drivers) {
503 drv = list_entry(entry, struct phy_driver, list);
504 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
505 return drv;
506 }
507
508 /* If we made it here, there's no driver for this PHY */
509 return generic_for_interface(interface);
510}
511
Kim Phillips960d70c2012-10-29 13:34:34 +0000512static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
513 int phy_id,
514 phy_interface_t interface)
Andy Fleming5f184712011-04-08 02:10:27 -0500515{
516 struct phy_device *dev;
517
518 /* We allocate the device, and initialize the
519 * default values */
520 dev = malloc(sizeof(*dev));
521 if (!dev) {
522 printf("Failed to allocate PHY device for %s:%d\n",
523 bus->name, addr);
524 return NULL;
525 }
526
527 memset(dev, 0, sizeof(*dev));
528
529 dev->duplex = -1;
530 dev->link = 1;
531 dev->interface = interface;
532
533 dev->autoneg = AUTONEG_ENABLE;
534
535 dev->addr = addr;
536 dev->phy_id = phy_id;
537 dev->bus = bus;
538
539 dev->drv = get_phy_driver(dev, interface);
540
541 phy_probe(dev);
542
543 bus->phymap[addr] = dev;
544
545 return dev;
546}
547
548/**
549 * get_phy_id - reads the specified addr for its ID.
550 * @bus: the target MII bus
551 * @addr: PHY address on the MII bus
552 * @phy_id: where to store the ID retrieved.
553 *
554 * Description: Reads the ID registers of the PHY at @addr on the
555 * @bus, stores it in @phy_id and returns zero on success.
556 */
Kim Phillips960d70c2012-10-29 13:34:34 +0000557static int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
Andy Fleming5f184712011-04-08 02:10:27 -0500558{
559 int phy_reg;
560
561 /* Grab the bits from PHYIR1, and put them
562 * in the upper half */
563 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
564
565 if (phy_reg < 0)
566 return -EIO;
567
568 *phy_id = (phy_reg & 0xffff) << 16;
569
570 /* Grab the bits from PHYIR2, and put them in the lower half */
571 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
572
573 if (phy_reg < 0)
574 return -EIO;
575
576 *phy_id |= (phy_reg & 0xffff);
577
578 return 0;
579}
580
Troy Kisky1adb4062012-10-22 16:40:43 +0000581static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
582 unsigned phy_mask, int devad, phy_interface_t interface)
583{
584 u32 phy_id = 0xffffffff;
585 while (phy_mask) {
586 int addr = ffs(phy_mask) - 1;
587 int r = get_phy_id(bus, addr, devad, &phy_id);
588 if (r < 0)
589 return ERR_PTR(r);
590 /* If the PHY ID is mostly f's, we didn't find anything */
591 if ((phy_id & 0x1fffffff) != 0x1fffffff)
592 return phy_device_create(bus, addr, phy_id, interface);
593 phy_mask &= ~(1 << addr);
594 }
595 return NULL;
596}
597
598static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
599 unsigned phy_mask, phy_interface_t interface)
600{
601 /* If we have one, return the existing device, with new interface */
602 while (phy_mask) {
603 int addr = ffs(phy_mask) - 1;
604 if (bus->phymap[addr]) {
605 bus->phymap[addr]->interface = interface;
606 return bus->phymap[addr];
607 }
608 phy_mask &= ~(1 << addr);
609 }
610 return NULL;
611}
612
613static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
614 unsigned phy_mask, phy_interface_t interface)
615{
616 int i;
617 struct phy_device *phydev;
618
619 phydev = search_for_existing_phy(bus, phy_mask, interface);
620 if (phydev)
621 return phydev;
622 /* Try Standard (ie Clause 22) access */
623 /* Otherwise we have to try Clause 45 */
624 for (i = 0; i < 5; i++) {
625 phydev = create_phy_by_mask(bus, phy_mask,
626 i ? i : MDIO_DEVAD_NONE, interface);
627 if (IS_ERR(phydev))
628 return NULL;
629 if (phydev)
630 return phydev;
631 }
632 printf("Phy not found\n");
633 return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
634}
635
Andy Fleming5f184712011-04-08 02:10:27 -0500636/**
637 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
638 * @bus: the target MII bus
639 * @addr: PHY address on the MII bus
640 *
641 * Description: Reads the ID registers of the PHY at @addr on the
642 * @bus, then allocates and returns the phy_device to represent it.
643 */
Kim Phillips960d70c2012-10-29 13:34:34 +0000644static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
645 phy_interface_t interface)
Andy Fleming5f184712011-04-08 02:10:27 -0500646{
Troy Kisky1adb4062012-10-22 16:40:43 +0000647 return get_phy_device_by_mask(bus, 1 << addr, interface);
Andy Fleming5f184712011-04-08 02:10:27 -0500648}
649
650int phy_reset(struct phy_device *phydev)
651{
652 int reg;
653 int timeout = 500;
654 int devad = MDIO_DEVAD_NONE;
655
656#ifdef CONFIG_PHYLIB_10G
657 /* If it's 10G, we need to issue reset through one of the MMDs */
658 if (is_10g_interface(phydev->interface)) {
659 if (!phydev->mmds)
660 gen10g_discover_mmds(phydev);
661
662 devad = ffs(phydev->mmds) - 1;
663 }
664#endif
665
666 reg = phy_read(phydev, devad, MII_BMCR);
667 if (reg < 0) {
668 debug("PHY status read failed\n");
669 return -1;
670 }
671
672 reg |= BMCR_RESET;
673
674 if (phy_write(phydev, devad, MII_BMCR, reg) < 0) {
675 debug("PHY reset failed\n");
676 return -1;
677 }
678
679#ifdef CONFIG_PHY_RESET_DELAY
680 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
681#endif
682 /*
683 * Poll the control register for the reset bit to go to 0 (it is
684 * auto-clearing). This should happen within 0.5 seconds per the
685 * IEEE spec.
686 */
687 while ((reg & BMCR_RESET) && timeout--) {
688 reg = phy_read(phydev, devad, MII_BMCR);
689
690 if (reg < 0) {
691 debug("PHY status read failed\n");
692 return -1;
693 }
694 udelay(1000);
695 }
696
697 if (reg & BMCR_RESET) {
698 puts("PHY reset timed out\n");
699 return -1;
700 }
701
702 return 0;
703}
704
705int miiphy_reset(const char *devname, unsigned char addr)
706{
707 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
708 struct phy_device *phydev;
709
710 /*
711 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
712 * If later code tries to connect with the right interface, this will
713 * be corrected by get_phy_device in phy_connect()
714 */
715 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
716
717 return phy_reset(phydev);
718}
719
Troy Kisky1adb4062012-10-22 16:40:43 +0000720struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
721 phy_interface_t interface)
Andy Fleming5f184712011-04-08 02:10:27 -0500722{
Andy Fleming5f184712011-04-08 02:10:27 -0500723 /* Reset the bus */
Vladimir Zapolskiye3a77212011-09-05 07:24:07 +0000724 if (bus->reset)
725 bus->reset(bus);
Andy Fleming5f184712011-04-08 02:10:27 -0500726
727 /* Wait 15ms to make sure the PHY has come out of hard reset */
728 udelay(15000);
Troy Kisky1adb4062012-10-22 16:40:43 +0000729 return get_phy_device_by_mask(bus, phy_mask, interface);
730}
Andy Fleming5f184712011-04-08 02:10:27 -0500731
Troy Kisky1adb4062012-10-22 16:40:43 +0000732void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
733{
Andy Fleming5f184712011-04-08 02:10:27 -0500734 /* Soft Reset the PHY */
735 phy_reset(phydev);
Troy Kisky1adb4062012-10-22 16:40:43 +0000736 if (phydev->dev) {
Andy Fleming5f184712011-04-08 02:10:27 -0500737 printf("%s:%d is connected to %s. Reconnecting to %s\n",
Troy Kisky1adb4062012-10-22 16:40:43 +0000738 phydev->bus->name, phydev->addr,
739 phydev->dev->name, dev->name);
740 }
Andy Fleming5f184712011-04-08 02:10:27 -0500741 phydev->dev = dev;
Wolfgang Denkb91a9d92011-07-24 21:39:10 +0000742 debug("%s connected to %s\n", dev->name, phydev->drv->name);
Troy Kisky1adb4062012-10-22 16:40:43 +0000743}
Andy Fleming5f184712011-04-08 02:10:27 -0500744
Troy Kisky1adb4062012-10-22 16:40:43 +0000745struct phy_device *phy_connect(struct mii_dev *bus, int addr,
746 struct eth_device *dev, phy_interface_t interface)
747{
748 struct phy_device *phydev;
749
750 phydev = phy_find_by_mask(bus, 1 << addr, interface);
751 if (phydev)
752 phy_connect_dev(phydev, dev);
753 else
754 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
Andy Fleming5f184712011-04-08 02:10:27 -0500755 return phydev;
756}
757
Timur Tabi6e5b9ac2012-07-05 10:33:18 +0000758/*
759 * Start the PHY. Returns 0 on success, or a negative error code.
760 */
Andy Fleming5f184712011-04-08 02:10:27 -0500761int phy_startup(struct phy_device *phydev)
762{
763 if (phydev->drv->startup)
Timur Tabi6e5b9ac2012-07-05 10:33:18 +0000764 return phydev->drv->startup(phydev);
Andy Fleming5f184712011-04-08 02:10:27 -0500765
766 return 0;
767}
768
769static int __board_phy_config(struct phy_device *phydev)
770{
Troy Kisky9fafe7d2012-02-07 14:08:49 +0000771 if (phydev->drv->config)
772 return phydev->drv->config(phydev);
Andy Fleming5f184712011-04-08 02:10:27 -0500773 return 0;
774}
775
776int board_phy_config(struct phy_device *phydev)
777 __attribute__((weak, alias("__board_phy_config")));
778
779int phy_config(struct phy_device *phydev)
780{
Andy Fleming5f184712011-04-08 02:10:27 -0500781 /* Invoke an optional board-specific helper */
782 board_phy_config(phydev);
783
784 return 0;
785}
786
787int phy_shutdown(struct phy_device *phydev)
788{
789 if (phydev->drv->shutdown)
790 phydev->drv->shutdown(phydev);
791
792 return 0;
793}