blob: 4e610bf054b1a9c6dc8427c2cf6944dcd1fa1917 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Flemingaecf6fc2011-04-08 02:10:27 -05002/*
3 * Generic PHY Management code
4 *
Andy Flemingaecf6fc2011-04-08 02:10:27 -05005 * Copyright 2011 Freescale Semiconductor, Inc.
6 * author Andy Fleming
7 *
8 * Based loosely off of Linux's PHY Lib
9 */
10
11#include <config.h>
12#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -070013#include <console.h>
Simon Glassdbad3462015-04-05 16:07:39 -060014#include <dm.h>
Andy Flemingaecf6fc2011-04-08 02:10:27 -050015#include <malloc.h>
16#include <net.h>
17#include <command.h>
18#include <miiphy.h>
19#include <phy.h>
20#include <errno.h>
Troy Kisky9519bc52012-10-22 16:40:43 +000021#include <linux/err.h>
Shengzhou Liufcfc7862014-04-11 16:14:17 +080022#include <linux/compiler.h>
Andy Flemingaecf6fc2011-04-08 02:10:27 -050023
Michal Simek5f676312015-05-13 13:40:40 +020024DECLARE_GLOBAL_DATA_PTR;
25
Andy Flemingaecf6fc2011-04-08 02:10:27 -050026/* Generic PHY support and helper functions */
27
28/**
Mario Six77577432018-01-15 11:08:27 +010029 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
Andy Flemingaecf6fc2011-04-08 02:10:27 -050030 * @phydev: target phy_device struct
31 *
32 * Description: Writes MII_ADVERTISE with the appropriate values,
33 * after sanitizing the values to make sure we only advertise
34 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
35 * hasn't changed, and > 0 if it has changed.
36 */
Kim Phillips914b0782012-10-29 13:34:34 +000037static int genphy_config_advert(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -050038{
39 u32 advertise;
Florian Fainelli6c8be842016-01-13 16:59:31 +030040 int oldadv, adv, bmsr;
Andy Flemingaecf6fc2011-04-08 02:10:27 -050041 int err, changed = 0;
42
Florian Fainelli6c8be842016-01-13 16:59:31 +030043 /* Only allow advertising what this PHY supports */
Andy Flemingaecf6fc2011-04-08 02:10:27 -050044 phydev->advertising &= phydev->supported;
45 advertise = phydev->advertising;
46
47 /* Setup standard advertisement */
Florian Fainelli6c8be842016-01-13 16:59:31 +030048 adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
49 oldadv = adv;
Andy Flemingaecf6fc2011-04-08 02:10:27 -050050
51 if (adv < 0)
52 return adv;
53
54 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
55 ADVERTISE_PAUSE_ASYM);
56 if (advertise & ADVERTISED_10baseT_Half)
57 adv |= ADVERTISE_10HALF;
58 if (advertise & ADVERTISED_10baseT_Full)
59 adv |= ADVERTISE_10FULL;
60 if (advertise & ADVERTISED_100baseT_Half)
61 adv |= ADVERTISE_100HALF;
62 if (advertise & ADVERTISED_100baseT_Full)
63 adv |= ADVERTISE_100FULL;
64 if (advertise & ADVERTISED_Pause)
65 adv |= ADVERTISE_PAUSE_CAP;
66 if (advertise & ADVERTISED_Asym_Pause)
67 adv |= ADVERTISE_PAUSE_ASYM;
Charles Coldwell23329412013-02-21 08:25:52 -050068 if (advertise & ADVERTISED_1000baseX_Half)
69 adv |= ADVERTISE_1000XHALF;
70 if (advertise & ADVERTISED_1000baseX_Full)
71 adv |= ADVERTISE_1000XFULL;
Andy Flemingaecf6fc2011-04-08 02:10:27 -050072
73 if (adv != oldadv) {
74 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
75
76 if (err < 0)
77 return err;
78 changed = 1;
79 }
80
Florian Fainelli6c8be842016-01-13 16:59:31 +030081 bmsr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
82 if (bmsr < 0)
83 return bmsr;
84
85 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
86 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
87 * logical 1.
88 */
89 if (!(bmsr & BMSR_ESTATEN))
90 return changed;
91
Andy Flemingaecf6fc2011-04-08 02:10:27 -050092 /* Configure gigabit if it's supported */
Florian Fainelli6c8be842016-01-13 16:59:31 +030093 adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
94 oldadv = adv;
95
96 if (adv < 0)
97 return adv;
Andy Flemingaecf6fc2011-04-08 02:10:27 -050098
Florian Fainelli6c8be842016-01-13 16:59:31 +030099 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500100
Florian Fainelli6c8be842016-01-13 16:59:31 +0300101 if (phydev->supported & (SUPPORTED_1000baseT_Half |
102 SUPPORTED_1000baseT_Full)) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500103 if (advertise & SUPPORTED_1000baseT_Half)
104 adv |= ADVERTISE_1000HALF;
105 if (advertise & SUPPORTED_1000baseT_Full)
106 adv |= ADVERTISE_1000FULL;
Florian Fainelli6c8be842016-01-13 16:59:31 +0300107 }
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500108
Florian Fainelli6c8be842016-01-13 16:59:31 +0300109 if (adv != oldadv)
110 changed = 1;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500111
Florian Fainelli6c8be842016-01-13 16:59:31 +0300112 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, adv);
113 if (err < 0)
114 return err;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500115
116 return changed;
117}
118
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500119/**
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 */
Kim Phillips914b0782012-10-29 13:34:34 +0000126static int genphy_setup_forced(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500127{
128 int err;
Alexandre Messier103a8c42016-01-22 14:16:15 -0500129 int ctl = BMCR_ANRESTART;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500130
Mario Six77577432018-01-15 11:08:27 +0100131 phydev->pause = 0;
132 phydev->asym_pause = 0;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500133
Mario Six77577432018-01-15 11:08:27 +0100134 if (phydev->speed == SPEED_1000)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500135 ctl |= BMCR_SPEED1000;
Mario Six77577432018-01-15 11:08:27 +0100136 else if (phydev->speed == SPEED_100)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500137 ctl |= BMCR_SPEED100;
138
Mario Six77577432018-01-15 11:08:27 +0100139 if (phydev->duplex == DUPLEX_FULL)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500140 ctl |= BMCR_FULLDPLX;
141
142 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
143
144 return err;
145}
146
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500147/**
148 * genphy_restart_aneg - Enable and Restart Autonegotiation
149 * @phydev: target phy_device struct
150 */
151int 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
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500170/**
171 * genphy_config_aneg - restart auto-negotiation or write BMCR
172 * @phydev: target phy_device struct
173 *
174 * Description: If auto-negotiation is enabled, we configure the
175 * advertising, and then restart auto-negotiation. If it is not
176 * enabled, then we write the BMCR.
177 */
178int genphy_config_aneg(struct phy_device *phydev)
179{
180 int result;
181
Mario Six77577432018-01-15 11:08:27 +0100182 if (phydev->autoneg != AUTONEG_ENABLE)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500183 return genphy_setup_forced(phydev);
184
185 result = genphy_config_advert(phydev);
186
187 if (result < 0) /* error */
188 return result;
189
190 if (result == 0) {
Mario Six77577432018-01-15 11:08:27 +0100191 /*
192 * Advertisment hasn't changed, but maybe aneg was never on to
193 * begin with? Or maybe phy was isolated?
194 */
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500195 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
196
197 if (ctl < 0)
198 return ctl;
199
200 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
201 result = 1; /* do restart aneg */
202 }
203
Mario Six77577432018-01-15 11:08:27 +0100204 /*
205 * Only restart aneg if we are advertising something different
206 * than we were before.
207 */
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500208 if (result > 0)
209 result = genphy_restart_aneg(phydev);
210
211 return result;
212}
213
214/**
215 * genphy_update_link - update link status in @phydev
216 * @phydev: target phy_device struct
217 *
218 * Description: Update the value in phydev->link to reflect the
219 * current link value. In order to do this, we need to read
220 * the status register twice, keeping the second value.
221 */
222int genphy_update_link(struct phy_device *phydev)
223{
224 unsigned int mii_reg;
225
226 /*
227 * Wait if the link is up, and autonegotiation is in progress
228 * (ie - we're capable and it's not done)
229 */
230 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
231
232 /*
233 * If we already saw the link up, and it hasn't gone down, then
234 * we don't need to wait for autoneg again
235 */
236 if (phydev->link && mii_reg & BMSR_LSTATUS)
237 return 0;
238
Alexandre Messier010c5ec2016-01-22 14:16:56 -0500239 if ((phydev->autoneg == AUTONEG_ENABLE) &&
240 !(mii_reg & BMSR_ANEGCOMPLETE)) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500241 int i = 0;
242
243 printf("%s Waiting for PHY auto negotiation to complete",
Mario Six77577432018-01-15 11:08:27 +0100244 phydev->dev->name);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500245 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
246 /*
247 * Timeout reached ?
248 */
249 if (i > PHY_ANEG_TIMEOUT) {
250 printf(" TIMEOUT !\n");
251 phydev->link = 0;
Michal Simekcf6677b2016-05-18 12:48:57 +0200252 return -ETIMEDOUT;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500253 }
254
255 if (ctrlc()) {
256 puts("user interrupt!\n");
257 phydev->link = 0;
258 return -EINTR;
259 }
260
261 if ((i++ % 500) == 0)
262 printf(".");
263
264 udelay(1000); /* 1 ms */
265 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
266 }
267 printf(" done\n");
268 phydev->link = 1;
269 } else {
270 /* Read the link a second time to clear the latched state */
271 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
272
273 if (mii_reg & BMSR_LSTATUS)
274 phydev->link = 1;
275 else
276 phydev->link = 0;
277 }
278
279 return 0;
280}
281
282/*
283 * Generic function which updates the speed and duplex. If
284 * autonegotiation is enabled, it uses the AND of the link
285 * partner's advertised capabilities and our advertised
286 * capabilities. If autonegotiation is disabled, we use the
287 * appropriate bits in the control register.
288 *
289 * Stolen from Linux's mii.c and phy_device.c
290 */
Yegor Yefremovc40f5d32012-11-28 11:15:17 +0100291int genphy_parse_link(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500292{
293 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
294
295 /* We're using autonegotiation */
Alexandre Messier010c5ec2016-01-22 14:16:56 -0500296 if (phydev->autoneg == AUTONEG_ENABLE) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500297 u32 lpa = 0;
Heiko Schocher94c35022013-07-23 15:32:36 +0200298 int gblpa = 0;
Charles Coldwell23329412013-02-21 08:25:52 -0500299 u32 estatus = 0;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500300
301 /* Check for gigabit capability */
David Duecked454232013-11-05 17:23:02 +0100302 if (phydev->supported & (SUPPORTED_1000baseT_Full |
303 SUPPORTED_1000baseT_Half)) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500304 /* We want a list of states supported by
305 * both PHYs in the link
306 */
307 gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
Heiko Schocher94c35022013-07-23 15:32:36 +0200308 if (gblpa < 0) {
Mario Six77577432018-01-15 11:08:27 +0100309 debug("Could not read MII_STAT1000. ");
310 debug("Ignoring gigabit capability\n");
Heiko Schocher94c35022013-07-23 15:32:36 +0200311 gblpa = 0;
312 }
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500313 gblpa &= phy_read(phydev,
314 MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
315 }
316
317 /* Set the baseline so we only have to set them
318 * if they're different
319 */
320 phydev->speed = SPEED_10;
321 phydev->duplex = DUPLEX_HALF;
322
323 /* Check the gigabit fields */
324 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
325 phydev->speed = SPEED_1000;
326
327 if (gblpa & PHY_1000BTSR_1000FD)
328 phydev->duplex = DUPLEX_FULL;
329
330 /* We're done! */
331 return 0;
332 }
333
334 lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
335 lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
336
Wolfgang Denka7b06ce2011-09-28 21:02:43 +0200337 if (lpa & (LPA_100FULL | LPA_100HALF)) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500338 phydev->speed = SPEED_100;
339
Wolfgang Denka7b06ce2011-09-28 21:02:43 +0200340 if (lpa & LPA_100FULL)
341 phydev->duplex = DUPLEX_FULL;
342
Mario Six77577432018-01-15 11:08:27 +0100343 } else if (lpa & LPA_10FULL) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500344 phydev->duplex = DUPLEX_FULL;
Mario Six77577432018-01-15 11:08:27 +0100345 }
Charles Coldwell23329412013-02-21 08:25:52 -0500346
Sascha Silbe2ac8d302013-07-19 12:25:10 +0200347 /*
348 * Extended status may indicate that the PHY supports
349 * 1000BASE-T/X even though the 1000BASE-T registers
350 * are missing. In this case we can't tell whether the
351 * peer also supports it, so we only check extended
352 * status if the 1000BASE-T registers are actually
353 * missing.
354 */
355 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
Charles Coldwell23329412013-02-21 08:25:52 -0500356 estatus = phy_read(phydev, MDIO_DEVAD_NONE,
357 MII_ESTATUS);
358
359 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
360 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
361 phydev->speed = SPEED_1000;
362 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
363 phydev->duplex = DUPLEX_FULL;
364 }
365
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500366 } else {
367 u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
368
369 phydev->speed = SPEED_10;
370 phydev->duplex = DUPLEX_HALF;
371
372 if (bmcr & BMCR_FULLDPLX)
373 phydev->duplex = DUPLEX_FULL;
374
375 if (bmcr & BMCR_SPEED1000)
376 phydev->speed = SPEED_1000;
377 else if (bmcr & BMCR_SPEED100)
378 phydev->speed = SPEED_100;
379 }
380
381 return 0;
382}
383
384int genphy_config(struct phy_device *phydev)
385{
386 int val;
387 u32 features;
388
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500389 features = (SUPPORTED_TP | SUPPORTED_MII
390 | SUPPORTED_AUI | SUPPORTED_FIBRE |
391 SUPPORTED_BNC);
392
393 /* Do we support autonegotiation? */
394 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
395
396 if (val < 0)
397 return val;
398
399 if (val & BMSR_ANEGCAPABLE)
400 features |= SUPPORTED_Autoneg;
401
402 if (val & BMSR_100FULL)
403 features |= SUPPORTED_100baseT_Full;
404 if (val & BMSR_100HALF)
405 features |= SUPPORTED_100baseT_Half;
406 if (val & BMSR_10FULL)
407 features |= SUPPORTED_10baseT_Full;
408 if (val & BMSR_10HALF)
409 features |= SUPPORTED_10baseT_Half;
410
411 if (val & BMSR_ESTATEN) {
412 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
413
414 if (val < 0)
415 return val;
416
417 if (val & ESTATUS_1000_TFULL)
418 features |= SUPPORTED_1000baseT_Full;
419 if (val & ESTATUS_1000_THALF)
420 features |= SUPPORTED_1000baseT_Half;
Charles Coldwell23329412013-02-21 08:25:52 -0500421 if (val & ESTATUS_1000_XFULL)
422 features |= SUPPORTED_1000baseX_Full;
423 if (val & ESTATUS_1000_XHALF)
Fabio Estevam45d601e2013-07-19 10:01:34 -0300424 features |= SUPPORTED_1000baseX_Half;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500425 }
426
Sascha Hauer6cc1e7d2016-01-13 16:59:32 +0300427 phydev->supported &= features;
428 phydev->advertising &= features;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500429
430 genphy_config_aneg(phydev);
431
432 return 0;
433}
434
435int genphy_startup(struct phy_device *phydev)
436{
Michal Simek5ff89662016-05-18 12:46:12 +0200437 int ret;
438
439 ret = genphy_update_link(phydev);
440 if (ret)
441 return ret;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500442
Michal Simek5ff89662016-05-18 12:46:12 +0200443 return genphy_parse_link(phydev);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500444}
445
446int genphy_shutdown(struct phy_device *phydev)
447{
448 return 0;
449}
450
451static struct phy_driver genphy_driver = {
452 .uid = 0xffffffff,
453 .mask = 0xffffffff,
454 .name = "Generic PHY",
Sascha Hauer6cc1e7d2016-01-13 16:59:32 +0300455 .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
456 SUPPORTED_AUI | SUPPORTED_FIBRE |
457 SUPPORTED_BNC,
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500458 .config = genphy_config,
459 .startup = genphy_startup,
460 .shutdown = genphy_shutdown,
461};
462
463static LIST_HEAD(phy_drivers);
464
465int phy_init(void)
466{
Florian Fainelli01b4ade2017-12-09 14:59:54 -0800467#ifdef CONFIG_B53_SWITCH
468 phy_b53_init();
469#endif
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000470#ifdef CONFIG_MV88E61XX_SWITCH
471 phy_mv88e61xx_init();
472#endif
Shaohui Xie0e548d72014-12-30 18:32:04 +0800473#ifdef CONFIG_PHY_AQUANTIA
474 phy_aquantia_init();
475#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500476#ifdef CONFIG_PHY_ATHEROS
477 phy_atheros_init();
478#endif
479#ifdef CONFIG_PHY_BROADCOM
480 phy_broadcom_init();
481#endif
Shengzhou Liuc878bdb2014-11-10 18:32:29 +0800482#ifdef CONFIG_PHY_CORTINA
483 phy_cortina_init();
484#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500485#ifdef CONFIG_PHY_DAVICOM
486 phy_davicom_init();
487#endif
Matt Porter3bbeb792013-03-20 05:38:13 +0000488#ifdef CONFIG_PHY_ET1011C
489 phy_et1011c_init();
490#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500491#ifdef CONFIG_PHY_LXT
492 phy_lxt_init();
493#endif
494#ifdef CONFIG_PHY_MARVELL
495 phy_marvell_init();
496#endif
Alexandru Gagniuc757bb672017-07-07 11:36:57 -0700497#ifdef CONFIG_PHY_MICREL_KSZ8XXX
498 phy_micrel_ksz8xxx_init();
499#endif
500#ifdef CONFIG_PHY_MICREL_KSZ90X1
501 phy_micrel_ksz90x1_init();
Andy Fleming60ca78b2011-04-07 21:56:05 -0500502#endif
Neil Armstrong7a4c90d2017-10-18 10:02:10 +0200503#ifdef CONFIG_PHY_MESON_GXL
504 phy_meson_gxl_init();
505#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500506#ifdef CONFIG_PHY_NATSEMI
507 phy_natsemi_init();
508#endif
509#ifdef CONFIG_PHY_REALTEK
510 phy_realtek_init();
511#endif
Nobuhiro Iwamatsu61134dc2011-11-23 21:24:15 +0000512#ifdef CONFIG_PHY_SMSC
513 phy_smsc_init();
514#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500515#ifdef CONFIG_PHY_TERANETICS
516 phy_teranetics_init();
517#endif
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700518#ifdef CONFIG_PHY_TI
519 phy_ti_init();
520#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500521#ifdef CONFIG_PHY_VITESSE
522 phy_vitesse_init();
523#endif
Siva Durga Prasad Paladugudd6cbd32016-02-05 13:22:10 +0530524#ifdef CONFIG_PHY_XILINX
525 phy_xilinx_init();
526#endif
John Haechtenee253f92016-12-09 22:15:17 +0000527#ifdef CONFIG_PHY_MSCC
528 phy_mscc_init();
529#endif
Hannes Schmelzerda494602017-03-23 15:11:43 +0100530#ifdef CONFIG_PHY_FIXED
531 phy_fixed_init();
532#endif
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500533 return 0;
534}
535
536int phy_register(struct phy_driver *drv)
537{
538 INIT_LIST_HEAD(&drv->list);
539 list_add_tail(&drv->list, &phy_drivers);
540
Michal Simek5f676312015-05-13 13:40:40 +0200541#ifdef CONFIG_NEEDS_MANUAL_RELOC
542 if (drv->probe)
543 drv->probe += gd->reloc_off;
544 if (drv->config)
545 drv->config += gd->reloc_off;
546 if (drv->startup)
547 drv->startup += gd->reloc_off;
548 if (drv->shutdown)
549 drv->shutdown += gd->reloc_off;
550 if (drv->readext)
551 drv->readext += gd->reloc_off;
552 if (drv->writeext)
553 drv->writeext += gd->reloc_off;
554#endif
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500555 return 0;
556}
557
Alexey Brodkine476bb22016-01-13 16:59:34 +0300558int phy_set_supported(struct phy_device *phydev, u32 max_speed)
559{
560 /* The default values for phydev->supported are provided by the PHY
561 * driver "features" member, we want to reset to sane defaults first
562 * before supporting higher speeds.
563 */
564 phydev->supported &= PHY_DEFAULT_FEATURES;
565
566 switch (max_speed) {
567 default:
568 return -ENOTSUPP;
569 case SPEED_1000:
570 phydev->supported |= PHY_1000BT_FEATURES;
571 /* fall through */
572 case SPEED_100:
573 phydev->supported |= PHY_100BT_FEATURES;
574 /* fall through */
575 case SPEED_10:
576 phydev->supported |= PHY_10BT_FEATURES;
577 }
578
579 return 0;
580}
581
Kim Phillips914b0782012-10-29 13:34:34 +0000582static int phy_probe(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500583{
584 int err = 0;
585
Mario Six77577432018-01-15 11:08:27 +0100586 phydev->advertising = phydev->drv->features;
587 phydev->supported = phydev->drv->features;
588
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500589 phydev->mmds = phydev->drv->mmds;
590
591 if (phydev->drv->probe)
592 err = phydev->drv->probe(phydev);
593
594 return err;
595}
596
597static struct phy_driver *generic_for_interface(phy_interface_t interface)
598{
599#ifdef CONFIG_PHYLIB_10G
600 if (is_10g_interface(interface))
601 return &gen10g_driver;
602#endif
603
604 return &genphy_driver;
605}
606
Kim Phillips914b0782012-10-29 13:34:34 +0000607static struct phy_driver *get_phy_driver(struct phy_device *phydev,
Mario Six77577432018-01-15 11:08:27 +0100608 phy_interface_t interface)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500609{
610 struct list_head *entry;
611 int phy_id = phydev->phy_id;
612 struct phy_driver *drv = NULL;
613
614 list_for_each(entry, &phy_drivers) {
615 drv = list_entry(entry, struct phy_driver, list);
616 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
617 return drv;
618 }
619
620 /* If we made it here, there's no driver for this PHY */
621 return generic_for_interface(interface);
622}
623
Kim Phillips914b0782012-10-29 13:34:34 +0000624static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
Jörg Krause8700a3d2015-07-15 14:58:49 +0200625 u32 phy_id,
Kim Phillips914b0782012-10-29 13:34:34 +0000626 phy_interface_t interface)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500627{
628 struct phy_device *dev;
629
Mario Six77577432018-01-15 11:08:27 +0100630 /*
631 * We allocate the device, and initialize the
632 * default values
633 */
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500634 dev = malloc(sizeof(*dev));
635 if (!dev) {
636 printf("Failed to allocate PHY device for %s:%d\n",
Mario Six77577432018-01-15 11:08:27 +0100637 bus->name, addr);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500638 return NULL;
639 }
640
641 memset(dev, 0, sizeof(*dev));
642
643 dev->duplex = -1;
Mugunthan V Nbbc97ba2015-09-03 15:50:21 +0530644 dev->link = 0;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500645 dev->interface = interface;
646
647 dev->autoneg = AUTONEG_ENABLE;
648
649 dev->addr = addr;
650 dev->phy_id = phy_id;
651 dev->bus = bus;
652
653 dev->drv = get_phy_driver(dev, interface);
654
655 phy_probe(dev);
656
657 bus->phymap[addr] = dev;
658
659 return dev;
660}
661
662/**
663 * get_phy_id - reads the specified addr for its ID.
664 * @bus: the target MII bus
665 * @addr: PHY address on the MII bus
666 * @phy_id: where to store the ID retrieved.
667 *
668 * Description: Reads the ID registers of the PHY at @addr on the
669 * @bus, stores it in @phy_id and returns zero on success.
670 */
Shengzhou Liu072b0fa2015-04-07 18:46:32 +0800671int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500672{
673 int phy_reg;
674
Mario Six77577432018-01-15 11:08:27 +0100675 /*
676 * Grab the bits from PHYIR1, and put them
677 * in the upper half
678 */
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500679 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
680
681 if (phy_reg < 0)
682 return -EIO;
683
684 *phy_id = (phy_reg & 0xffff) << 16;
685
686 /* Grab the bits from PHYIR2, and put them in the lower half */
687 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
688
689 if (phy_reg < 0)
690 return -EIO;
691
692 *phy_id |= (phy_reg & 0xffff);
693
694 return 0;
695}
696
Troy Kisky9519bc52012-10-22 16:40:43 +0000697static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
Mario Six77577432018-01-15 11:08:27 +0100698 uint phy_mask, int devad,
699 phy_interface_t interface)
Troy Kisky9519bc52012-10-22 16:40:43 +0000700{
701 u32 phy_id = 0xffffffff;
Mario Six77577432018-01-15 11:08:27 +0100702
Troy Kisky9519bc52012-10-22 16:40:43 +0000703 while (phy_mask) {
704 int addr = ffs(phy_mask) - 1;
705 int r = get_phy_id(bus, addr, devad, &phy_id);
Troy Kisky9519bc52012-10-22 16:40:43 +0000706 /* If the PHY ID is mostly f's, we didn't find anything */
Cormier, Jonathancdfc5762014-05-21 13:08:52 -0400707 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff)
Troy Kisky9519bc52012-10-22 16:40:43 +0000708 return phy_device_create(bus, addr, phy_id, interface);
709 phy_mask &= ~(1 << addr);
710 }
711 return NULL;
712}
713
714static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
Mario Six77577432018-01-15 11:08:27 +0100715 uint phy_mask,
716 phy_interface_t interface)
Troy Kisky9519bc52012-10-22 16:40:43 +0000717{
718 /* If we have one, return the existing device, with new interface */
719 while (phy_mask) {
720 int addr = ffs(phy_mask) - 1;
Mario Six77577432018-01-15 11:08:27 +0100721
Troy Kisky9519bc52012-10-22 16:40:43 +0000722 if (bus->phymap[addr]) {
723 bus->phymap[addr]->interface = interface;
724 return bus->phymap[addr];
725 }
726 phy_mask &= ~(1 << addr);
727 }
728 return NULL;
729}
730
731static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
Mario Six77577432018-01-15 11:08:27 +0100732 uint phy_mask,
733 phy_interface_t interface)
Troy Kisky9519bc52012-10-22 16:40:43 +0000734{
735 int i;
736 struct phy_device *phydev;
737
738 phydev = search_for_existing_phy(bus, phy_mask, interface);
739 if (phydev)
740 return phydev;
741 /* Try Standard (ie Clause 22) access */
742 /* Otherwise we have to try Clause 45 */
743 for (i = 0; i < 5; i++) {
744 phydev = create_phy_by_mask(bus, phy_mask,
Mario Six77577432018-01-15 11:08:27 +0100745 i ? i : MDIO_DEVAD_NONE, interface);
Troy Kisky9519bc52012-10-22 16:40:43 +0000746 if (IS_ERR(phydev))
747 return NULL;
748 if (phydev)
749 return phydev;
750 }
Bin Meng59c6d892015-10-07 21:19:30 -0700751
752 debug("\n%s PHY: ", bus->name);
753 while (phy_mask) {
754 int addr = ffs(phy_mask) - 1;
Mario Six77577432018-01-15 11:08:27 +0100755
Bin Meng59c6d892015-10-07 21:19:30 -0700756 debug("%d ", addr);
757 phy_mask &= ~(1 << addr);
758 }
759 debug("not found\n");
Bin Meng0776c572015-10-07 21:19:29 -0700760
761 return NULL;
Troy Kisky9519bc52012-10-22 16:40:43 +0000762}
763
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500764/**
Mario Six77577432018-01-15 11:08:27 +0100765 * get_phy_device - reads the specified PHY device and returns its
766 * @phy_device struct
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500767 * @bus: the target MII bus
768 * @addr: PHY address on the MII bus
769 *
770 * Description: Reads the ID registers of the PHY at @addr on the
771 * @bus, then allocates and returns the phy_device to represent it.
772 */
Kim Phillips914b0782012-10-29 13:34:34 +0000773static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
774 phy_interface_t interface)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500775{
Troy Kisky9519bc52012-10-22 16:40:43 +0000776 return get_phy_device_by_mask(bus, 1 << addr, interface);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500777}
778
779int phy_reset(struct phy_device *phydev)
780{
781 int reg;
782 int timeout = 500;
783 int devad = MDIO_DEVAD_NONE;
784
Shaohui Xie62a7b922016-01-28 15:55:46 +0800785 if (phydev->flags & PHY_FLAG_BROKEN_RESET)
786 return 0;
787
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500788#ifdef CONFIG_PHYLIB_10G
789 /* If it's 10G, we need to issue reset through one of the MMDs */
790 if (is_10g_interface(phydev->interface)) {
791 if (!phydev->mmds)
792 gen10g_discover_mmds(phydev);
793
794 devad = ffs(phydev->mmds) - 1;
795 }
796#endif
797
Stefan Agnere64013d2015-12-09 11:21:25 -0800798 if (phy_write(phydev, devad, MII_BMCR, BMCR_RESET) < 0) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500799 debug("PHY reset failed\n");
800 return -1;
801 }
802
803#ifdef CONFIG_PHY_RESET_DELAY
804 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
805#endif
806 /*
807 * Poll the control register for the reset bit to go to 0 (it is
808 * auto-clearing). This should happen within 0.5 seconds per the
809 * IEEE spec.
810 */
Stefan Agnere64013d2015-12-09 11:21:25 -0800811 reg = phy_read(phydev, devad, MII_BMCR);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500812 while ((reg & BMCR_RESET) && timeout--) {
813 reg = phy_read(phydev, devad, MII_BMCR);
814
815 if (reg < 0) {
816 debug("PHY status read failed\n");
817 return -1;
818 }
819 udelay(1000);
820 }
821
822 if (reg & BMCR_RESET) {
823 puts("PHY reset timed out\n");
824 return -1;
825 }
826
827 return 0;
828}
829
830int miiphy_reset(const char *devname, unsigned char addr)
831{
832 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
833 struct phy_device *phydev;
834
835 /*
836 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
837 * If later code tries to connect with the right interface, this will
838 * be corrected by get_phy_device in phy_connect()
839 */
840 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
841
842 return phy_reset(phydev);
843}
844
Mario Six77577432018-01-15 11:08:27 +0100845struct phy_device *phy_find_by_mask(struct mii_dev *bus, uint phy_mask,
846 phy_interface_t interface)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500847{
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500848 /* Reset the bus */
Jörg Krause71b1d862015-07-15 15:18:22 +0200849 if (bus->reset) {
Vladimir Zapolskiy46f10bb2011-09-05 07:24:07 +0000850 bus->reset(bus);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500851
Jörg Krause71b1d862015-07-15 15:18:22 +0200852 /* Wait 15ms to make sure the PHY has come out of hard reset */
Mario Six77577432018-01-15 11:08:27 +0100853 mdelay(15);
Jörg Krause71b1d862015-07-15 15:18:22 +0200854 }
855
Troy Kisky9519bc52012-10-22 16:40:43 +0000856 return get_phy_device_by_mask(bus, phy_mask, interface);
857}
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500858
Simon Glassdbad3462015-04-05 16:07:39 -0600859#ifdef CONFIG_DM_ETH
860void phy_connect_dev(struct phy_device *phydev, struct udevice *dev)
861#else
Troy Kisky9519bc52012-10-22 16:40:43 +0000862void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
Simon Glassdbad3462015-04-05 16:07:39 -0600863#endif
Troy Kisky9519bc52012-10-22 16:40:43 +0000864{
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500865 /* Soft Reset the PHY */
866 phy_reset(phydev);
Bin Mengf87a15c2015-10-07 21:19:31 -0700867 if (phydev->dev && phydev->dev != dev) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500868 printf("%s:%d is connected to %s. Reconnecting to %s\n",
Mario Six77577432018-01-15 11:08:27 +0100869 phydev->bus->name, phydev->addr,
870 phydev->dev->name, dev->name);
Troy Kisky9519bc52012-10-22 16:40:43 +0000871 }
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500872 phydev->dev = dev;
Wolfgang Denka71ebc42011-07-24 21:39:10 +0000873 debug("%s connected to %s\n", dev->name, phydev->drv->name);
Troy Kisky9519bc52012-10-22 16:40:43 +0000874}
875
Simon Glassdbad3462015-04-05 16:07:39 -0600876#ifdef CONFIG_DM_ETH
877struct phy_device *phy_connect(struct mii_dev *bus, int addr,
Mario Six77577432018-01-15 11:08:27 +0100878 struct udevice *dev,
879 phy_interface_t interface)
Simon Glassdbad3462015-04-05 16:07:39 -0600880#else
Troy Kisky9519bc52012-10-22 16:40:43 +0000881struct phy_device *phy_connect(struct mii_dev *bus, int addr,
Mario Six77577432018-01-15 11:08:27 +0100882 struct eth_device *dev,
883 phy_interface_t interface)
Simon Glassdbad3462015-04-05 16:07:39 -0600884#endif
Troy Kisky9519bc52012-10-22 16:40:43 +0000885{
Hannes Schmelzerda494602017-03-23 15:11:43 +0100886 struct phy_device *phydev = NULL;
887#ifdef CONFIG_PHY_FIXED
888 int sn;
889 const char *name;
Mario Six77577432018-01-15 11:08:27 +0100890
Simon Glass7a494432017-05-17 17:18:09 -0600891 sn = fdt_first_subnode(gd->fdt_blob, dev_of_offset(dev));
Hannes Schmelzerda494602017-03-23 15:11:43 +0100892 while (sn > 0) {
893 name = fdt_get_name(gd->fdt_blob, sn, NULL);
Mario Six77577432018-01-15 11:08:27 +0100894 if (name && strcmp(name, "fixed-link") == 0) {
Hannes Schmelzerda494602017-03-23 15:11:43 +0100895 phydev = phy_device_create(bus,
896 sn, PHY_FIXED_ID, interface);
897 break;
898 }
899 sn = fdt_next_subnode(gd->fdt_blob, sn);
900 }
901#endif
Mario Six77577432018-01-15 11:08:27 +0100902 if (!phydev)
Hannes Schmelzerda494602017-03-23 15:11:43 +0100903 phydev = phy_find_by_mask(bus, 1 << addr, interface);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500904
Troy Kisky9519bc52012-10-22 16:40:43 +0000905 if (phydev)
906 phy_connect_dev(phydev, dev);
907 else
908 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500909 return phydev;
910}
911
Timur Tabi251180b2012-07-05 10:33:18 +0000912/*
913 * Start the PHY. Returns 0 on success, or a negative error code.
914 */
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500915int phy_startup(struct phy_device *phydev)
916{
917 if (phydev->drv->startup)
Timur Tabi251180b2012-07-05 10:33:18 +0000918 return phydev->drv->startup(phydev);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500919
920 return 0;
921}
922
Jeroen Hofsteee4f89472014-10-08 22:57:26 +0200923__weak int board_phy_config(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500924{
Troy Kisky85846412012-02-07 14:08:49 +0000925 if (phydev->drv->config)
926 return phydev->drv->config(phydev);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500927 return 0;
928}
929
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500930int phy_config(struct phy_device *phydev)
931{
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500932 /* Invoke an optional board-specific helper */
Michal Simek24ce2322016-05-18 14:37:23 +0200933 return board_phy_config(phydev);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500934}
935
936int phy_shutdown(struct phy_device *phydev)
937{
938 if (phydev->drv->shutdown)
939 phydev->drv->shutdown(phydev);
940
941 return 0;
942}
Simon Glassdbad3462015-04-05 16:07:39 -0600943
944int phy_get_interface_by_name(const char *str)
945{
946 int i;
947
948 for (i = 0; i < PHY_INTERFACE_MODE_COUNT; i++) {
949 if (!strcmp(str, phy_interface_strings[i]))
950 return i;
951 }
952
953 return -1;
954}