blob: 8bacd991ad570929b0c0869718306cc9691fece8 [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>
Simon Glassa73bda42015-11-08 23:47:45 -070014#include <console.h>
Simon Glassdbad3462015-04-05 16:07:39 -060015#include <dm.h>
Andy Flemingaecf6fc2011-04-08 02:10:27 -050016#include <malloc.h>
17#include <net.h>
18#include <command.h>
19#include <miiphy.h>
20#include <phy.h>
21#include <errno.h>
Troy Kisky9519bc52012-10-22 16:40:43 +000022#include <linux/err.h>
Shengzhou Liufcfc7862014-04-11 16:14:17 +080023#include <linux/compiler.h>
Andy Flemingaecf6fc2011-04-08 02:10:27 -050024
Michal Simek5f676312015-05-13 13:40:40 +020025DECLARE_GLOBAL_DATA_PTR;
26
Andy Flemingaecf6fc2011-04-08 02:10:27 -050027/* Generic PHY support and helper functions */
28
29/**
30 * genphy_config_advert - sanitize and advertise auto-negotation parameters
31 * @phydev: target phy_device struct
32 *
33 * Description: Writes MII_ADVERTISE with the appropriate values,
34 * after sanitizing the values to make sure we only advertise
35 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
36 * hasn't changed, and > 0 if it has changed.
37 */
Kim Phillips914b0782012-10-29 13:34:34 +000038static int genphy_config_advert(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -050039{
40 u32 advertise;
Florian Fainelli6c8be842016-01-13 16:59:31 +030041 int oldadv, adv, bmsr;
Andy Flemingaecf6fc2011-04-08 02:10:27 -050042 int err, changed = 0;
43
Florian Fainelli6c8be842016-01-13 16:59:31 +030044 /* Only allow advertising what this PHY supports */
Andy Flemingaecf6fc2011-04-08 02:10:27 -050045 phydev->advertising &= phydev->supported;
46 advertise = phydev->advertising;
47
48 /* Setup standard advertisement */
Florian Fainelli6c8be842016-01-13 16:59:31 +030049 adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
50 oldadv = adv;
Andy Flemingaecf6fc2011-04-08 02:10:27 -050051
52 if (adv < 0)
53 return adv;
54
55 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
56 ADVERTISE_PAUSE_ASYM);
57 if (advertise & ADVERTISED_10baseT_Half)
58 adv |= ADVERTISE_10HALF;
59 if (advertise & ADVERTISED_10baseT_Full)
60 adv |= ADVERTISE_10FULL;
61 if (advertise & ADVERTISED_100baseT_Half)
62 adv |= ADVERTISE_100HALF;
63 if (advertise & ADVERTISED_100baseT_Full)
64 adv |= ADVERTISE_100FULL;
65 if (advertise & ADVERTISED_Pause)
66 adv |= ADVERTISE_PAUSE_CAP;
67 if (advertise & ADVERTISED_Asym_Pause)
68 adv |= ADVERTISE_PAUSE_ASYM;
Charles Coldwell23329412013-02-21 08:25:52 -050069 if (advertise & ADVERTISED_1000baseX_Half)
70 adv |= ADVERTISE_1000XHALF;
71 if (advertise & ADVERTISED_1000baseX_Full)
72 adv |= ADVERTISE_1000XFULL;
Andy Flemingaecf6fc2011-04-08 02:10:27 -050073
74 if (adv != oldadv) {
75 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
76
77 if (err < 0)
78 return err;
79 changed = 1;
80 }
81
Florian Fainelli6c8be842016-01-13 16:59:31 +030082 bmsr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
83 if (bmsr < 0)
84 return bmsr;
85
86 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
87 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
88 * logical 1.
89 */
90 if (!(bmsr & BMSR_ESTATEN))
91 return changed;
92
Andy Flemingaecf6fc2011-04-08 02:10:27 -050093 /* Configure gigabit if it's supported */
Florian Fainelli6c8be842016-01-13 16:59:31 +030094 adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
95 oldadv = adv;
96
97 if (adv < 0)
98 return adv;
Andy Flemingaecf6fc2011-04-08 02:10:27 -050099
Florian Fainelli6c8be842016-01-13 16:59:31 +0300100 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500101
Florian Fainelli6c8be842016-01-13 16:59:31 +0300102 if (phydev->supported & (SUPPORTED_1000baseT_Half |
103 SUPPORTED_1000baseT_Full)) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500104 if (advertise & SUPPORTED_1000baseT_Half)
105 adv |= ADVERTISE_1000HALF;
106 if (advertise & SUPPORTED_1000baseT_Full)
107 adv |= ADVERTISE_1000FULL;
Florian Fainelli6c8be842016-01-13 16:59:31 +0300108 }
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500109
Florian Fainelli6c8be842016-01-13 16:59:31 +0300110 if (adv != oldadv)
111 changed = 1;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500112
Florian Fainelli6c8be842016-01-13 16:59:31 +0300113 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, adv);
114 if (err < 0)
115 return err;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500116
117 return changed;
118}
119
120
121/**
122 * genphy_setup_forced - configures/forces speed/duplex from @phydev
123 * @phydev: target phy_device struct
124 *
125 * Description: Configures MII_BMCR to force speed/duplex
126 * to the values in phydev. Assumes that the values are valid.
127 */
Kim Phillips914b0782012-10-29 13:34:34 +0000128static int genphy_setup_forced(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500129{
130 int err;
Alexandre Messier103a8c42016-01-22 14:16:15 -0500131 int ctl = BMCR_ANRESTART;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500132
133 phydev->pause = phydev->asym_pause = 0;
134
135 if (SPEED_1000 == phydev->speed)
136 ctl |= BMCR_SPEED1000;
137 else if (SPEED_100 == phydev->speed)
138 ctl |= BMCR_SPEED100;
139
140 if (DUPLEX_FULL == phydev->duplex)
141 ctl |= BMCR_FULLDPLX;
142
143 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
144
145 return err;
146}
147
148
149/**
150 * genphy_restart_aneg - Enable and Restart Autonegotiation
151 * @phydev: target phy_device struct
152 */
153int genphy_restart_aneg(struct phy_device *phydev)
154{
155 int ctl;
156
157 ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
158
159 if (ctl < 0)
160 return ctl;
161
162 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
163
164 /* Don't isolate the PHY if we're negotiating */
165 ctl &= ~(BMCR_ISOLATE);
166
167 ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
168
169 return ctl;
170}
171
172
173/**
174 * genphy_config_aneg - restart auto-negotiation or write BMCR
175 * @phydev: target phy_device struct
176 *
177 * Description: If auto-negotiation is enabled, we configure the
178 * advertising, and then restart auto-negotiation. If it is not
179 * enabled, then we write the BMCR.
180 */
181int genphy_config_aneg(struct phy_device *phydev)
182{
183 int result;
184
185 if (AUTONEG_ENABLE != phydev->autoneg)
186 return genphy_setup_forced(phydev);
187
188 result = genphy_config_advert(phydev);
189
190 if (result < 0) /* error */
191 return result;
192
193 if (result == 0) {
194 /* Advertisment hasn't changed, but maybe aneg was never on to
195 * begin with? Or maybe phy was isolated? */
196 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
197
198 if (ctl < 0)
199 return ctl;
200
201 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
202 result = 1; /* do restart aneg */
203 }
204
205 /* Only restart aneg if we are advertising something different
206 * than we were before. */
207 if (result > 0)
208 result = genphy_restart_aneg(phydev);
209
210 return result;
211}
212
213/**
214 * genphy_update_link - update link status in @phydev
215 * @phydev: target phy_device struct
216 *
217 * Description: Update the value in phydev->link to reflect the
218 * current link value. In order to do this, we need to read
219 * the status register twice, keeping the second value.
220 */
221int genphy_update_link(struct phy_device *phydev)
222{
223 unsigned int mii_reg;
224
225 /*
226 * Wait if the link is up, and autonegotiation is in progress
227 * (ie - we're capable and it's not done)
228 */
229 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
230
231 /*
232 * If we already saw the link up, and it hasn't gone down, then
233 * we don't need to wait for autoneg again
234 */
235 if (phydev->link && mii_reg & BMSR_LSTATUS)
236 return 0;
237
Alexandre Messier010c5ec2016-01-22 14:16:56 -0500238 if ((phydev->autoneg == AUTONEG_ENABLE) &&
239 !(mii_reg & BMSR_ANEGCOMPLETE)) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500240 int i = 0;
241
242 printf("%s Waiting for PHY auto negotiation to complete",
243 phydev->dev->name);
244 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
245 /*
246 * Timeout reached ?
247 */
248 if (i > PHY_ANEG_TIMEOUT) {
249 printf(" TIMEOUT !\n");
250 phydev->link = 0;
Michal Simekcf6677b2016-05-18 12:48:57 +0200251 return -ETIMEDOUT;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500252 }
253
254 if (ctrlc()) {
255 puts("user interrupt!\n");
256 phydev->link = 0;
257 return -EINTR;
258 }
259
260 if ((i++ % 500) == 0)
261 printf(".");
262
263 udelay(1000); /* 1 ms */
264 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
265 }
266 printf(" done\n");
267 phydev->link = 1;
268 } else {
269 /* Read the link a second time to clear the latched state */
270 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
271
272 if (mii_reg & BMSR_LSTATUS)
273 phydev->link = 1;
274 else
275 phydev->link = 0;
276 }
277
278 return 0;
279}
280
281/*
282 * Generic function which updates the speed and duplex. If
283 * autonegotiation is enabled, it uses the AND of the link
284 * partner's advertised capabilities and our advertised
285 * capabilities. If autonegotiation is disabled, we use the
286 * appropriate bits in the control register.
287 *
288 * Stolen from Linux's mii.c and phy_device.c
289 */
Yegor Yefremovc40f5d32012-11-28 11:15:17 +0100290int genphy_parse_link(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500291{
292 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
293
294 /* We're using autonegotiation */
Alexandre Messier010c5ec2016-01-22 14:16:56 -0500295 if (phydev->autoneg == AUTONEG_ENABLE) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500296 u32 lpa = 0;
Heiko Schocher94c35022013-07-23 15:32:36 +0200297 int gblpa = 0;
Charles Coldwell23329412013-02-21 08:25:52 -0500298 u32 estatus = 0;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500299
300 /* Check for gigabit capability */
David Duecked454232013-11-05 17:23:02 +0100301 if (phydev->supported & (SUPPORTED_1000baseT_Full |
302 SUPPORTED_1000baseT_Half)) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500303 /* We want a list of states supported by
304 * both PHYs in the link
305 */
306 gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
Heiko Schocher94c35022013-07-23 15:32:36 +0200307 if (gblpa < 0) {
308 debug("Could not read MII_STAT1000. Ignoring gigabit capability\n");
309 gblpa = 0;
310 }
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500311 gblpa &= phy_read(phydev,
312 MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
313 }
314
315 /* Set the baseline so we only have to set them
316 * if they're different
317 */
318 phydev->speed = SPEED_10;
319 phydev->duplex = DUPLEX_HALF;
320
321 /* Check the gigabit fields */
322 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
323 phydev->speed = SPEED_1000;
324
325 if (gblpa & PHY_1000BTSR_1000FD)
326 phydev->duplex = DUPLEX_FULL;
327
328 /* We're done! */
329 return 0;
330 }
331
332 lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
333 lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
334
Wolfgang Denka7b06ce2011-09-28 21:02:43 +0200335 if (lpa & (LPA_100FULL | LPA_100HALF)) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500336 phydev->speed = SPEED_100;
337
Wolfgang Denka7b06ce2011-09-28 21:02:43 +0200338 if (lpa & LPA_100FULL)
339 phydev->duplex = DUPLEX_FULL;
340
341 } else if (lpa & LPA_10FULL)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500342 phydev->duplex = DUPLEX_FULL;
Charles Coldwell23329412013-02-21 08:25:52 -0500343
Sascha Silbe2ac8d302013-07-19 12:25:10 +0200344 /*
345 * Extended status may indicate that the PHY supports
346 * 1000BASE-T/X even though the 1000BASE-T registers
347 * are missing. In this case we can't tell whether the
348 * peer also supports it, so we only check extended
349 * status if the 1000BASE-T registers are actually
350 * missing.
351 */
352 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
Charles Coldwell23329412013-02-21 08:25:52 -0500353 estatus = phy_read(phydev, MDIO_DEVAD_NONE,
354 MII_ESTATUS);
355
356 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
357 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
358 phydev->speed = SPEED_1000;
359 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
360 phydev->duplex = DUPLEX_FULL;
361 }
362
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500363 } else {
364 u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
365
366 phydev->speed = SPEED_10;
367 phydev->duplex = DUPLEX_HALF;
368
369 if (bmcr & BMCR_FULLDPLX)
370 phydev->duplex = DUPLEX_FULL;
371
372 if (bmcr & BMCR_SPEED1000)
373 phydev->speed = SPEED_1000;
374 else if (bmcr & BMCR_SPEED100)
375 phydev->speed = SPEED_100;
376 }
377
378 return 0;
379}
380
381int genphy_config(struct phy_device *phydev)
382{
383 int val;
384 u32 features;
385
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500386 features = (SUPPORTED_TP | SUPPORTED_MII
387 | SUPPORTED_AUI | SUPPORTED_FIBRE |
388 SUPPORTED_BNC);
389
390 /* Do we support autonegotiation? */
391 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
392
393 if (val < 0)
394 return val;
395
396 if (val & BMSR_ANEGCAPABLE)
397 features |= SUPPORTED_Autoneg;
398
399 if (val & BMSR_100FULL)
400 features |= SUPPORTED_100baseT_Full;
401 if (val & BMSR_100HALF)
402 features |= SUPPORTED_100baseT_Half;
403 if (val & BMSR_10FULL)
404 features |= SUPPORTED_10baseT_Full;
405 if (val & BMSR_10HALF)
406 features |= SUPPORTED_10baseT_Half;
407
408 if (val & BMSR_ESTATEN) {
409 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
410
411 if (val < 0)
412 return val;
413
414 if (val & ESTATUS_1000_TFULL)
415 features |= SUPPORTED_1000baseT_Full;
416 if (val & ESTATUS_1000_THALF)
417 features |= SUPPORTED_1000baseT_Half;
Charles Coldwell23329412013-02-21 08:25:52 -0500418 if (val & ESTATUS_1000_XFULL)
419 features |= SUPPORTED_1000baseX_Full;
420 if (val & ESTATUS_1000_XHALF)
Fabio Estevam45d601e2013-07-19 10:01:34 -0300421 features |= SUPPORTED_1000baseX_Half;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500422 }
423
Sascha Hauer6cc1e7d2016-01-13 16:59:32 +0300424 phydev->supported &= features;
425 phydev->advertising &= features;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500426
427 genphy_config_aneg(phydev);
428
429 return 0;
430}
431
432int genphy_startup(struct phy_device *phydev)
433{
Michal Simek5ff89662016-05-18 12:46:12 +0200434 int ret;
435
436 ret = genphy_update_link(phydev);
437 if (ret)
438 return ret;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500439
Michal Simek5ff89662016-05-18 12:46:12 +0200440 return genphy_parse_link(phydev);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500441}
442
443int genphy_shutdown(struct phy_device *phydev)
444{
445 return 0;
446}
447
448static struct phy_driver genphy_driver = {
449 .uid = 0xffffffff,
450 .mask = 0xffffffff,
451 .name = "Generic PHY",
Sascha Hauer6cc1e7d2016-01-13 16:59:32 +0300452 .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
453 SUPPORTED_AUI | SUPPORTED_FIBRE |
454 SUPPORTED_BNC,
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500455 .config = genphy_config,
456 .startup = genphy_startup,
457 .shutdown = genphy_shutdown,
458};
459
460static LIST_HEAD(phy_drivers);
461
462int phy_init(void)
463{
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000464#ifdef CONFIG_MV88E61XX_SWITCH
465 phy_mv88e61xx_init();
466#endif
Shaohui Xie0e548d72014-12-30 18:32:04 +0800467#ifdef CONFIG_PHY_AQUANTIA
468 phy_aquantia_init();
469#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500470#ifdef CONFIG_PHY_ATHEROS
471 phy_atheros_init();
472#endif
473#ifdef CONFIG_PHY_BROADCOM
474 phy_broadcom_init();
475#endif
Shengzhou Liuc878bdb2014-11-10 18:32:29 +0800476#ifdef CONFIG_PHY_CORTINA
477 phy_cortina_init();
478#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500479#ifdef CONFIG_PHY_DAVICOM
480 phy_davicom_init();
481#endif
Matt Porter3bbeb792013-03-20 05:38:13 +0000482#ifdef CONFIG_PHY_ET1011C
483 phy_et1011c_init();
484#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500485#ifdef CONFIG_PHY_LXT
486 phy_lxt_init();
487#endif
488#ifdef CONFIG_PHY_MARVELL
489 phy_marvell_init();
490#endif
491#ifdef CONFIG_PHY_MICREL
492 phy_micrel_init();
493#endif
494#ifdef CONFIG_PHY_NATSEMI
495 phy_natsemi_init();
496#endif
497#ifdef CONFIG_PHY_REALTEK
498 phy_realtek_init();
499#endif
Nobuhiro Iwamatsu61134dc2011-11-23 21:24:15 +0000500#ifdef CONFIG_PHY_SMSC
501 phy_smsc_init();
502#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500503#ifdef CONFIG_PHY_TERANETICS
504 phy_teranetics_init();
505#endif
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700506#ifdef CONFIG_PHY_TI
507 phy_ti_init();
508#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500509#ifdef CONFIG_PHY_VITESSE
510 phy_vitesse_init();
511#endif
Siva Durga Prasad Paladugudd6cbd32016-02-05 13:22:10 +0530512#ifdef CONFIG_PHY_XILINX
513 phy_xilinx_init();
514#endif
John Haechtenee253f92016-12-09 22:15:17 +0000515#ifdef CONFIG_PHY_MSCC
516 phy_mscc_init();
517#endif
Hannes Schmelzerda494602017-03-23 15:11:43 +0100518#ifdef CONFIG_PHY_FIXED
519 phy_fixed_init();
520#endif
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500521 return 0;
522}
523
524int phy_register(struct phy_driver *drv)
525{
526 INIT_LIST_HEAD(&drv->list);
527 list_add_tail(&drv->list, &phy_drivers);
528
Michal Simek5f676312015-05-13 13:40:40 +0200529#ifdef CONFIG_NEEDS_MANUAL_RELOC
530 if (drv->probe)
531 drv->probe += gd->reloc_off;
532 if (drv->config)
533 drv->config += gd->reloc_off;
534 if (drv->startup)
535 drv->startup += gd->reloc_off;
536 if (drv->shutdown)
537 drv->shutdown += gd->reloc_off;
538 if (drv->readext)
539 drv->readext += gd->reloc_off;
540 if (drv->writeext)
541 drv->writeext += gd->reloc_off;
542#endif
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500543 return 0;
544}
545
Alexey Brodkine476bb22016-01-13 16:59:34 +0300546int phy_set_supported(struct phy_device *phydev, u32 max_speed)
547{
548 /* The default values for phydev->supported are provided by the PHY
549 * driver "features" member, we want to reset to sane defaults first
550 * before supporting higher speeds.
551 */
552 phydev->supported &= PHY_DEFAULT_FEATURES;
553
554 switch (max_speed) {
555 default:
556 return -ENOTSUPP;
557 case SPEED_1000:
558 phydev->supported |= PHY_1000BT_FEATURES;
559 /* fall through */
560 case SPEED_100:
561 phydev->supported |= PHY_100BT_FEATURES;
562 /* fall through */
563 case SPEED_10:
564 phydev->supported |= PHY_10BT_FEATURES;
565 }
566
567 return 0;
568}
569
Kim Phillips914b0782012-10-29 13:34:34 +0000570static int phy_probe(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500571{
572 int err = 0;
573
574 phydev->advertising = phydev->supported = phydev->drv->features;
575 phydev->mmds = phydev->drv->mmds;
576
577 if (phydev->drv->probe)
578 err = phydev->drv->probe(phydev);
579
580 return err;
581}
582
583static struct phy_driver *generic_for_interface(phy_interface_t interface)
584{
585#ifdef CONFIG_PHYLIB_10G
586 if (is_10g_interface(interface))
587 return &gen10g_driver;
588#endif
589
590 return &genphy_driver;
591}
592
Kim Phillips914b0782012-10-29 13:34:34 +0000593static struct phy_driver *get_phy_driver(struct phy_device *phydev,
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500594 phy_interface_t interface)
595{
596 struct list_head *entry;
597 int phy_id = phydev->phy_id;
598 struct phy_driver *drv = NULL;
599
600 list_for_each(entry, &phy_drivers) {
601 drv = list_entry(entry, struct phy_driver, list);
602 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
603 return drv;
604 }
605
606 /* If we made it here, there's no driver for this PHY */
607 return generic_for_interface(interface);
608}
609
Kim Phillips914b0782012-10-29 13:34:34 +0000610static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
Jörg Krause8700a3d2015-07-15 14:58:49 +0200611 u32 phy_id,
Kim Phillips914b0782012-10-29 13:34:34 +0000612 phy_interface_t interface)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500613{
614 struct phy_device *dev;
615
616 /* We allocate the device, and initialize the
617 * default values */
618 dev = malloc(sizeof(*dev));
619 if (!dev) {
620 printf("Failed to allocate PHY device for %s:%d\n",
621 bus->name, addr);
622 return NULL;
623 }
624
625 memset(dev, 0, sizeof(*dev));
626
627 dev->duplex = -1;
Mugunthan V Nbbc97ba2015-09-03 15:50:21 +0530628 dev->link = 0;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500629 dev->interface = interface;
630
631 dev->autoneg = AUTONEG_ENABLE;
632
633 dev->addr = addr;
634 dev->phy_id = phy_id;
635 dev->bus = bus;
636
637 dev->drv = get_phy_driver(dev, interface);
638
639 phy_probe(dev);
640
641 bus->phymap[addr] = dev;
642
643 return dev;
644}
645
646/**
647 * get_phy_id - reads the specified addr for its ID.
648 * @bus: the target MII bus
649 * @addr: PHY address on the MII bus
650 * @phy_id: where to store the ID retrieved.
651 *
652 * Description: Reads the ID registers of the PHY at @addr on the
653 * @bus, stores it in @phy_id and returns zero on success.
654 */
Shengzhou Liu072b0fa2015-04-07 18:46:32 +0800655int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500656{
657 int phy_reg;
658
659 /* Grab the bits from PHYIR1, and put them
660 * in the upper half */
661 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
662
663 if (phy_reg < 0)
664 return -EIO;
665
666 *phy_id = (phy_reg & 0xffff) << 16;
667
668 /* Grab the bits from PHYIR2, and put them in the lower half */
669 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
670
671 if (phy_reg < 0)
672 return -EIO;
673
674 *phy_id |= (phy_reg & 0xffff);
675
676 return 0;
677}
678
Troy Kisky9519bc52012-10-22 16:40:43 +0000679static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
680 unsigned phy_mask, int devad, phy_interface_t interface)
681{
682 u32 phy_id = 0xffffffff;
683 while (phy_mask) {
684 int addr = ffs(phy_mask) - 1;
685 int r = get_phy_id(bus, addr, devad, &phy_id);
Troy Kisky9519bc52012-10-22 16:40:43 +0000686 /* If the PHY ID is mostly f's, we didn't find anything */
Cormier, Jonathancdfc5762014-05-21 13:08:52 -0400687 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff)
Troy Kisky9519bc52012-10-22 16:40:43 +0000688 return phy_device_create(bus, addr, phy_id, interface);
689 phy_mask &= ~(1 << addr);
690 }
691 return NULL;
692}
693
694static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
695 unsigned phy_mask, phy_interface_t interface)
696{
697 /* If we have one, return the existing device, with new interface */
698 while (phy_mask) {
699 int addr = ffs(phy_mask) - 1;
700 if (bus->phymap[addr]) {
701 bus->phymap[addr]->interface = interface;
702 return bus->phymap[addr];
703 }
704 phy_mask &= ~(1 << addr);
705 }
706 return NULL;
707}
708
709static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
710 unsigned phy_mask, phy_interface_t interface)
711{
712 int i;
713 struct phy_device *phydev;
714
715 phydev = search_for_existing_phy(bus, phy_mask, interface);
716 if (phydev)
717 return phydev;
718 /* Try Standard (ie Clause 22) access */
719 /* Otherwise we have to try Clause 45 */
720 for (i = 0; i < 5; i++) {
721 phydev = create_phy_by_mask(bus, phy_mask,
722 i ? i : MDIO_DEVAD_NONE, interface);
723 if (IS_ERR(phydev))
724 return NULL;
725 if (phydev)
726 return phydev;
727 }
Bin Meng59c6d892015-10-07 21:19:30 -0700728
729 debug("\n%s PHY: ", bus->name);
730 while (phy_mask) {
731 int addr = ffs(phy_mask) - 1;
732 debug("%d ", addr);
733 phy_mask &= ~(1 << addr);
734 }
735 debug("not found\n");
Bin Meng0776c572015-10-07 21:19:29 -0700736
737 return NULL;
Troy Kisky9519bc52012-10-22 16:40:43 +0000738}
739
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500740/**
741 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
742 * @bus: the target MII bus
743 * @addr: PHY address on the MII bus
744 *
745 * Description: Reads the ID registers of the PHY at @addr on the
746 * @bus, then allocates and returns the phy_device to represent it.
747 */
Kim Phillips914b0782012-10-29 13:34:34 +0000748static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
749 phy_interface_t interface)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500750{
Troy Kisky9519bc52012-10-22 16:40:43 +0000751 return get_phy_device_by_mask(bus, 1 << addr, interface);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500752}
753
754int phy_reset(struct phy_device *phydev)
755{
756 int reg;
757 int timeout = 500;
758 int devad = MDIO_DEVAD_NONE;
759
Shaohui Xie62a7b922016-01-28 15:55:46 +0800760 if (phydev->flags & PHY_FLAG_BROKEN_RESET)
761 return 0;
762
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500763#ifdef CONFIG_PHYLIB_10G
764 /* If it's 10G, we need to issue reset through one of the MMDs */
765 if (is_10g_interface(phydev->interface)) {
766 if (!phydev->mmds)
767 gen10g_discover_mmds(phydev);
768
769 devad = ffs(phydev->mmds) - 1;
770 }
771#endif
772
Stefan Agnere64013d2015-12-09 11:21:25 -0800773 if (phy_write(phydev, devad, MII_BMCR, BMCR_RESET) < 0) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500774 debug("PHY reset failed\n");
775 return -1;
776 }
777
778#ifdef CONFIG_PHY_RESET_DELAY
779 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
780#endif
781 /*
782 * Poll the control register for the reset bit to go to 0 (it is
783 * auto-clearing). This should happen within 0.5 seconds per the
784 * IEEE spec.
785 */
Stefan Agnere64013d2015-12-09 11:21:25 -0800786 reg = phy_read(phydev, devad, MII_BMCR);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500787 while ((reg & BMCR_RESET) && timeout--) {
788 reg = phy_read(phydev, devad, MII_BMCR);
789
790 if (reg < 0) {
791 debug("PHY status read failed\n");
792 return -1;
793 }
794 udelay(1000);
795 }
796
797 if (reg & BMCR_RESET) {
798 puts("PHY reset timed out\n");
799 return -1;
800 }
801
802 return 0;
803}
804
805int miiphy_reset(const char *devname, unsigned char addr)
806{
807 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
808 struct phy_device *phydev;
809
810 /*
811 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
812 * If later code tries to connect with the right interface, this will
813 * be corrected by get_phy_device in phy_connect()
814 */
815 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
816
817 return phy_reset(phydev);
818}
819
Troy Kisky9519bc52012-10-22 16:40:43 +0000820struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
821 phy_interface_t interface)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500822{
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500823 /* Reset the bus */
Jörg Krause71b1d862015-07-15 15:18:22 +0200824 if (bus->reset) {
Vladimir Zapolskiy46f10bb2011-09-05 07:24:07 +0000825 bus->reset(bus);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500826
Jörg Krause71b1d862015-07-15 15:18:22 +0200827 /* Wait 15ms to make sure the PHY has come out of hard reset */
828 udelay(15000);
829 }
830
Troy Kisky9519bc52012-10-22 16:40:43 +0000831 return get_phy_device_by_mask(bus, phy_mask, interface);
832}
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500833
Simon Glassdbad3462015-04-05 16:07:39 -0600834#ifdef CONFIG_DM_ETH
835void phy_connect_dev(struct phy_device *phydev, struct udevice *dev)
836#else
Troy Kisky9519bc52012-10-22 16:40:43 +0000837void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
Simon Glassdbad3462015-04-05 16:07:39 -0600838#endif
Troy Kisky9519bc52012-10-22 16:40:43 +0000839{
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500840 /* Soft Reset the PHY */
841 phy_reset(phydev);
Bin Mengf87a15c2015-10-07 21:19:31 -0700842 if (phydev->dev && phydev->dev != dev) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500843 printf("%s:%d is connected to %s. Reconnecting to %s\n",
Troy Kisky9519bc52012-10-22 16:40:43 +0000844 phydev->bus->name, phydev->addr,
845 phydev->dev->name, dev->name);
846 }
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500847 phydev->dev = dev;
Wolfgang Denka71ebc42011-07-24 21:39:10 +0000848 debug("%s connected to %s\n", dev->name, phydev->drv->name);
Troy Kisky9519bc52012-10-22 16:40:43 +0000849}
850
Simon Glassdbad3462015-04-05 16:07:39 -0600851#ifdef CONFIG_DM_ETH
852struct phy_device *phy_connect(struct mii_dev *bus, int addr,
853 struct udevice *dev, phy_interface_t interface)
854#else
Troy Kisky9519bc52012-10-22 16:40:43 +0000855struct phy_device *phy_connect(struct mii_dev *bus, int addr,
856 struct eth_device *dev, phy_interface_t interface)
Simon Glassdbad3462015-04-05 16:07:39 -0600857#endif
Troy Kisky9519bc52012-10-22 16:40:43 +0000858{
Hannes Schmelzerda494602017-03-23 15:11:43 +0100859 struct phy_device *phydev = NULL;
860#ifdef CONFIG_PHY_FIXED
861 int sn;
862 const char *name;
863 sn = fdt_first_subnode(gd->fdt_blob, dev->of_offset);
864 while (sn > 0) {
865 name = fdt_get_name(gd->fdt_blob, sn, NULL);
866 if (name != NULL && strcmp(name, "fixed-link") == 0) {
867 phydev = phy_device_create(bus,
868 sn, PHY_FIXED_ID, interface);
869 break;
870 }
871 sn = fdt_next_subnode(gd->fdt_blob, sn);
872 }
873#endif
874 if (phydev == NULL)
875 phydev = phy_find_by_mask(bus, 1 << addr, interface);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500876
Troy Kisky9519bc52012-10-22 16:40:43 +0000877 if (phydev)
878 phy_connect_dev(phydev, dev);
879 else
880 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500881 return phydev;
882}
883
Timur Tabi251180b2012-07-05 10:33:18 +0000884/*
885 * Start the PHY. Returns 0 on success, or a negative error code.
886 */
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500887int phy_startup(struct phy_device *phydev)
888{
889 if (phydev->drv->startup)
Timur Tabi251180b2012-07-05 10:33:18 +0000890 return phydev->drv->startup(phydev);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500891
892 return 0;
893}
894
Jeroen Hofsteee4f89472014-10-08 22:57:26 +0200895__weak int board_phy_config(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500896{
Troy Kisky85846412012-02-07 14:08:49 +0000897 if (phydev->drv->config)
898 return phydev->drv->config(phydev);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500899 return 0;
900}
901
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500902int phy_config(struct phy_device *phydev)
903{
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500904 /* Invoke an optional board-specific helper */
Michal Simek24ce2322016-05-18 14:37:23 +0200905 return board_phy_config(phydev);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500906}
907
908int phy_shutdown(struct phy_device *phydev)
909{
910 if (phydev->drv->shutdown)
911 phydev->drv->shutdown(phydev);
912
913 return 0;
914}
Simon Glassdbad3462015-04-05 16:07:39 -0600915
916int phy_get_interface_by_name(const char *str)
917{
918 int i;
919
920 for (i = 0; i < PHY_INTERFACE_MODE_COUNT; i++) {
921 if (!strcmp(str, phy_interface_strings[i]))
922 return i;
923 }
924
925 return -1;
926}