blob: 467c97224313328e2aa7e2f04edabc15def84792 [file] [log] [blame]
Andy Flemingaecf6fc2011-04-08 02:10:27 -05001/*
2 * Generic PHY Management code
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Andy Flemingaecf6fc2011-04-08 02:10:27 -05005 *
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>
Troy Kisky9519bc52012-10-22 16:40:43 +000020#include <linux/err.h>
Shengzhou Liufcfc7862014-04-11 16:14:17 +080021#include <linux/compiler.h>
Andy Flemingaecf6fc2011-04-08 02:10:27 -050022
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 */
Kim Phillips914b0782012-10-29 13:34:34 +000034static int genphy_config_advert(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -050035{
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;
Charles Coldwell23329412013-02-21 08:25:52 -050065 if (advertise & ADVERTISED_1000baseX_Half)
66 adv |= ADVERTISE_1000XHALF;
67 if (advertise & ADVERTISED_1000baseX_Full)
68 adv |= ADVERTISE_1000XFULL;
Andy Flemingaecf6fc2011-04-08 02:10:27 -050069
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 */
Kim Phillips914b0782012-10-29 13:34:34 +0000113static int genphy_setup_forced(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500114{
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 */
138int 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 */
166int 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 */
206int 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 */
Yegor Yefremovc40f5d32012-11-28 11:15:17 +0100274int genphy_parse_link(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500275{
276 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
277
278 /* We're using autonegotiation */
David Duecked454232013-11-05 17:23:02 +0100279 if (phydev->supported & SUPPORTED_Autoneg) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500280 u32 lpa = 0;
Heiko Schocher94c35022013-07-23 15:32:36 +0200281 int gblpa = 0;
Charles Coldwell23329412013-02-21 08:25:52 -0500282 u32 estatus = 0;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500283
284 /* Check for gigabit capability */
David Duecked454232013-11-05 17:23:02 +0100285 if (phydev->supported & (SUPPORTED_1000baseT_Full |
286 SUPPORTED_1000baseT_Half)) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500287 /* 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);
Heiko Schocher94c35022013-07-23 15:32:36 +0200291 if (gblpa < 0) {
292 debug("Could not read MII_STAT1000. Ignoring gigabit capability\n");
293 gblpa = 0;
294 }
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500295 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
Wolfgang Denka7b06ce2011-09-28 21:02:43 +0200319 if (lpa & (LPA_100FULL | LPA_100HALF)) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500320 phydev->speed = SPEED_100;
321
Wolfgang Denka7b06ce2011-09-28 21:02:43 +0200322 if (lpa & LPA_100FULL)
323 phydev->duplex = DUPLEX_FULL;
324
325 } else if (lpa & LPA_10FULL)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500326 phydev->duplex = DUPLEX_FULL;
Charles Coldwell23329412013-02-21 08:25:52 -0500327
Sascha Silbe2ac8d302013-07-19 12:25:10 +0200328 /*
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))
Charles Coldwell23329412013-02-21 08:25:52 -0500337 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
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500347 } 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
365int 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;
Charles Coldwell23329412013-02-21 08:25:52 -0500404 if (val & ESTATUS_1000_XFULL)
405 features |= SUPPORTED_1000baseX_Full;
406 if (val & ESTATUS_1000_XHALF)
Fabio Estevam45d601e2013-07-19 10:01:34 -0300407 features |= SUPPORTED_1000baseX_Half;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500408 }
409
410 phydev->supported = features;
411 phydev->advertising = features;
412
413 genphy_config_aneg(phydev);
414
415 return 0;
416}
417
418int genphy_startup(struct phy_device *phydev)
419{
420 genphy_update_link(phydev);
421 genphy_parse_link(phydev);
422
423 return 0;
424}
425
426int genphy_shutdown(struct phy_device *phydev)
427{
428 return 0;
429}
430
431static 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
441static LIST_HEAD(phy_drivers);
442
443int phy_init(void)
444{
Andy Fleming60ca78b2011-04-07 21:56:05 -0500445#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
Matt Porter3bbeb792013-03-20 05:38:13 +0000454#ifdef CONFIG_PHY_ET1011C
455 phy_et1011c_init();
456#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500457#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
Nobuhiro Iwamatsu61134dc2011-11-23 21:24:15 +0000472#ifdef CONFIG_PHY_SMSC
473 phy_smsc_init();
474#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500475#ifdef CONFIG_PHY_TERANETICS
476 phy_teranetics_init();
477#endif
478#ifdef CONFIG_PHY_VITESSE
479 phy_vitesse_init();
480#endif
481
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500482 return 0;
483}
484
485int 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
Kim Phillips914b0782012-10-29 13:34:34 +0000493static int phy_probe(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500494{
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
506static 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
Kim Phillips914b0782012-10-29 13:34:34 +0000516static struct phy_driver *get_phy_driver(struct phy_device *phydev,
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500517 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
Kim Phillips914b0782012-10-29 13:34:34 +0000533static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
534 int phy_id,
535 phy_interface_t interface)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500536{
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 */
Jeroen Hofsteee4f89472014-10-08 22:57:26 +0200578static int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500579{
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
Troy Kisky9519bc52012-10-22 16:40:43 +0000602static 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);
Troy Kisky9519bc52012-10-22 16:40:43 +0000609 /* If the PHY ID is mostly f's, we didn't find anything */
Cormier, Jonathancdfc5762014-05-21 13:08:52 -0400610 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff)
Troy Kisky9519bc52012-10-22 16:40:43 +0000611 return phy_device_create(bus, addr, phy_id, interface);
612 phy_mask &= ~(1 << addr);
613 }
614 return NULL;
615}
616
617static 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
632static 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 }
Khoronzhuk, Ivan3d6a5b82014-10-17 20:44:32 +0300651 printf("Phy %d not found\n", ffs(phy_mask) - 1);
Troy Kisky9519bc52012-10-22 16:40:43 +0000652 return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
653}
654
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500655/**
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 */
Kim Phillips914b0782012-10-29 13:34:34 +0000663static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
664 phy_interface_t interface)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500665{
Troy Kisky9519bc52012-10-22 16:40:43 +0000666 return get_phy_device_by_mask(bus, 1 << addr, interface);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500667}
668
669int 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
724int 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
Troy Kisky9519bc52012-10-22 16:40:43 +0000739struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
740 phy_interface_t interface)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500741{
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500742 /* Reset the bus */
Vladimir Zapolskiy46f10bb2011-09-05 07:24:07 +0000743 if (bus->reset)
744 bus->reset(bus);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500745
746 /* Wait 15ms to make sure the PHY has come out of hard reset */
747 udelay(15000);
Troy Kisky9519bc52012-10-22 16:40:43 +0000748 return get_phy_device_by_mask(bus, phy_mask, interface);
749}
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500750
Troy Kisky9519bc52012-10-22 16:40:43 +0000751void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
752{
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500753 /* Soft Reset the PHY */
754 phy_reset(phydev);
Troy Kisky9519bc52012-10-22 16:40:43 +0000755 if (phydev->dev) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500756 printf("%s:%d is connected to %s. Reconnecting to %s\n",
Troy Kisky9519bc52012-10-22 16:40:43 +0000757 phydev->bus->name, phydev->addr,
758 phydev->dev->name, dev->name);
759 }
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500760 phydev->dev = dev;
Wolfgang Denka71ebc42011-07-24 21:39:10 +0000761 debug("%s connected to %s\n", dev->name, phydev->drv->name);
Troy Kisky9519bc52012-10-22 16:40:43 +0000762}
763
764struct 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;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500768
Troy Kisky9519bc52012-10-22 16:40:43 +0000769 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);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500774 return phydev;
775}
776
Timur Tabi251180b2012-07-05 10:33:18 +0000777/*
778 * Start the PHY. Returns 0 on success, or a negative error code.
779 */
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500780int phy_startup(struct phy_device *phydev)
781{
782 if (phydev->drv->startup)
Timur Tabi251180b2012-07-05 10:33:18 +0000783 return phydev->drv->startup(phydev);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500784
785 return 0;
786}
787
Jeroen Hofsteee4f89472014-10-08 22:57:26 +0200788__weak int board_phy_config(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500789{
Troy Kisky85846412012-02-07 14:08:49 +0000790 if (phydev->drv->config)
791 return phydev->drv->config(phydev);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500792 return 0;
793}
794
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500795int phy_config(struct phy_device *phydev)
796{
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500797 /* Invoke an optional board-specific helper */
798 board_phy_config(phydev);
799
800 return 0;
801}
802
803int phy_shutdown(struct phy_device *phydev)
804{
805 if (phydev->drv->shutdown)
806 phydev->drv->shutdown(phydev);
807
808 return 0;
809}