blob: 89e3076bfd258b4e3b73d2e2e38191afe8fc9615 [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 */
Andy Flemingaecf6fc2011-04-08 02:10:27 -050010#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -070011#include <console.h>
Simon Glassdbad3462015-04-05 16:07:39 -060012#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Andy Flemingaecf6fc2011-04-08 02:10:27 -050014#include <malloc.h>
15#include <net.h>
16#include <command.h>
17#include <miiphy.h>
18#include <phy.h>
19#include <errno.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060020#include <asm/global_data.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060021#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
Troy Kisky9519bc52012-10-22 16:40:43 +000023#include <linux/err.h>
Shengzhou Liufcfc7862014-04-11 16:14:17 +080024#include <linux/compiler.h>
Andy Flemingaecf6fc2011-04-08 02:10:27 -050025
Michal Simek5f676312015-05-13 13:40:40 +020026DECLARE_GLOBAL_DATA_PTR;
27
Andy Flemingaecf6fc2011-04-08 02:10:27 -050028/* Generic PHY support and helper functions */
29
30/**
Mario Six77577432018-01-15 11:08:27 +010031 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
Andy Flemingaecf6fc2011-04-08 02:10:27 -050032 * @phydev: target phy_device struct
33 *
34 * Description: Writes MII_ADVERTISE with the appropriate values,
35 * after sanitizing the values to make sure we only advertise
36 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
37 * hasn't changed, and > 0 if it has changed.
38 */
Kim Phillips914b0782012-10-29 13:34:34 +000039static int genphy_config_advert(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -050040{
41 u32 advertise;
Florian Fainelli6c8be842016-01-13 16:59:31 +030042 int oldadv, adv, bmsr;
Andy Flemingaecf6fc2011-04-08 02:10:27 -050043 int err, changed = 0;
44
Florian Fainelli6c8be842016-01-13 16:59:31 +030045 /* Only allow advertising what this PHY supports */
Andy Flemingaecf6fc2011-04-08 02:10:27 -050046 phydev->advertising &= phydev->supported;
47 advertise = phydev->advertising;
48
49 /* Setup standard advertisement */
Florian Fainelli6c8be842016-01-13 16:59:31 +030050 adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
51 oldadv = adv;
Andy Flemingaecf6fc2011-04-08 02:10:27 -050052
53 if (adv < 0)
54 return adv;
55
56 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
57 ADVERTISE_PAUSE_ASYM);
58 if (advertise & ADVERTISED_10baseT_Half)
59 adv |= ADVERTISE_10HALF;
60 if (advertise & ADVERTISED_10baseT_Full)
61 adv |= ADVERTISE_10FULL;
62 if (advertise & ADVERTISED_100baseT_Half)
63 adv |= ADVERTISE_100HALF;
64 if (advertise & ADVERTISED_100baseT_Full)
65 adv |= ADVERTISE_100FULL;
66 if (advertise & ADVERTISED_Pause)
67 adv |= ADVERTISE_PAUSE_CAP;
68 if (advertise & ADVERTISED_Asym_Pause)
69 adv |= ADVERTISE_PAUSE_ASYM;
Charles Coldwell23329412013-02-21 08:25:52 -050070 if (advertise & ADVERTISED_1000baseX_Half)
71 adv |= ADVERTISE_1000XHALF;
72 if (advertise & ADVERTISED_1000baseX_Full)
73 adv |= ADVERTISE_1000XFULL;
Andy Flemingaecf6fc2011-04-08 02:10:27 -050074
75 if (adv != oldadv) {
76 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
77
78 if (err < 0)
79 return err;
80 changed = 1;
81 }
82
Florian Fainelli6c8be842016-01-13 16:59:31 +030083 bmsr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
84 if (bmsr < 0)
85 return bmsr;
86
87 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
88 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
89 * logical 1.
90 */
91 if (!(bmsr & BMSR_ESTATEN))
92 return changed;
93
Andy Flemingaecf6fc2011-04-08 02:10:27 -050094 /* Configure gigabit if it's supported */
Florian Fainelli6c8be842016-01-13 16:59:31 +030095 adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
96 oldadv = adv;
97
98 if (adv < 0)
99 return adv;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500100
Florian Fainelli6c8be842016-01-13 16:59:31 +0300101 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500102
Florian Fainelli6c8be842016-01-13 16:59:31 +0300103 if (phydev->supported & (SUPPORTED_1000baseT_Half |
104 SUPPORTED_1000baseT_Full)) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500105 if (advertise & SUPPORTED_1000baseT_Half)
106 adv |= ADVERTISE_1000HALF;
107 if (advertise & SUPPORTED_1000baseT_Full)
108 adv |= ADVERTISE_1000FULL;
Florian Fainelli6c8be842016-01-13 16:59:31 +0300109 }
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500110
Florian Fainelli6c8be842016-01-13 16:59:31 +0300111 if (adv != oldadv)
112 changed = 1;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500113
Florian Fainelli6c8be842016-01-13 16:59:31 +0300114 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, adv);
115 if (err < 0)
116 return err;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500117
118 return changed;
119}
120
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500121/**
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
Mario Six77577432018-01-15 11:08:27 +0100133 phydev->pause = 0;
134 phydev->asym_pause = 0;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500135
Mario Six77577432018-01-15 11:08:27 +0100136 if (phydev->speed == SPEED_1000)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500137 ctl |= BMCR_SPEED1000;
Mario Six77577432018-01-15 11:08:27 +0100138 else if (phydev->speed == SPEED_100)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500139 ctl |= BMCR_SPEED100;
140
Mario Six77577432018-01-15 11:08:27 +0100141 if (phydev->duplex == DUPLEX_FULL)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500142 ctl |= BMCR_FULLDPLX;
143
144 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
145
146 return err;
147}
148
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500149/**
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
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500172/**
173 * genphy_config_aneg - restart auto-negotiation or write BMCR
174 * @phydev: target phy_device struct
175 *
176 * Description: If auto-negotiation is enabled, we configure the
177 * advertising, and then restart auto-negotiation. If it is not
178 * enabled, then we write the BMCR.
179 */
180int genphy_config_aneg(struct phy_device *phydev)
181{
182 int result;
183
Mario Six77577432018-01-15 11:08:27 +0100184 if (phydev->autoneg != AUTONEG_ENABLE)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500185 return genphy_setup_forced(phydev);
186
187 result = genphy_config_advert(phydev);
188
189 if (result < 0) /* error */
190 return result;
191
192 if (result == 0) {
Mario Six77577432018-01-15 11:08:27 +0100193 /*
194 * Advertisment hasn't changed, but maybe aneg was never on to
195 * begin with? Or maybe phy was isolated?
196 */
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500197 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
198
199 if (ctl < 0)
200 return ctl;
201
202 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
203 result = 1; /* do restart aneg */
204 }
205
Mario Six77577432018-01-15 11:08:27 +0100206 /*
207 * Only restart aneg if we are advertising something different
208 * than we were before.
209 */
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500210 if (result > 0)
211 result = genphy_restart_aneg(phydev);
212
213 return result;
214}
215
216/**
217 * genphy_update_link - update link status in @phydev
218 * @phydev: target phy_device struct
219 *
220 * Description: Update the value in phydev->link to reflect the
221 * current link value. In order to do this, we need to read
222 * the status register twice, keeping the second value.
223 */
224int genphy_update_link(struct phy_device *phydev)
225{
226 unsigned int mii_reg;
227
228 /*
229 * Wait if the link is up, and autonegotiation is in progress
230 * (ie - we're capable and it's not done)
231 */
232 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
233
234 /*
235 * If we already saw the link up, and it hasn't gone down, then
236 * we don't need to wait for autoneg again
237 */
238 if (phydev->link && mii_reg & BMSR_LSTATUS)
239 return 0;
240
Alexandre Messier010c5ec2016-01-22 14:16:56 -0500241 if ((phydev->autoneg == AUTONEG_ENABLE) &&
242 !(mii_reg & BMSR_ANEGCOMPLETE)) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500243 int i = 0;
244
245 printf("%s Waiting for PHY auto negotiation to complete",
Mario Six77577432018-01-15 11:08:27 +0100246 phydev->dev->name);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500247 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
248 /*
249 * Timeout reached ?
250 */
Andre Przywara71a6a602020-01-03 22:08:47 +0000251 if (i > (PHY_ANEG_TIMEOUT / 50)) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500252 printf(" TIMEOUT !\n");
253 phydev->link = 0;
Michal Simekcf6677b2016-05-18 12:48:57 +0200254 return -ETIMEDOUT;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500255 }
256
257 if (ctrlc()) {
258 puts("user interrupt!\n");
259 phydev->link = 0;
260 return -EINTR;
261 }
262
Stefan Roese5cf96d12019-09-30 10:26:42 +0200263 if ((i++ % 10) == 0)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500264 printf(".");
265
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500266 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
Stefan Roese5cf96d12019-09-30 10:26:42 +0200267 mdelay(50); /* 50 ms */
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500268 }
269 printf(" done\n");
270 phydev->link = 1;
271 } else {
272 /* Read the link a second time to clear the latched state */
273 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
274
275 if (mii_reg & BMSR_LSTATUS)
276 phydev->link = 1;
277 else
278 phydev->link = 0;
279 }
280
281 return 0;
282}
283
284/*
285 * Generic function which updates the speed and duplex. If
286 * autonegotiation is enabled, it uses the AND of the link
287 * partner's advertised capabilities and our advertised
288 * capabilities. If autonegotiation is disabled, we use the
289 * appropriate bits in the control register.
290 *
291 * Stolen from Linux's mii.c and phy_device.c
292 */
Yegor Yefremovc40f5d32012-11-28 11:15:17 +0100293int genphy_parse_link(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500294{
295 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
296
297 /* We're using autonegotiation */
Alexandre Messier010c5ec2016-01-22 14:16:56 -0500298 if (phydev->autoneg == AUTONEG_ENABLE) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500299 u32 lpa = 0;
Heiko Schocher94c35022013-07-23 15:32:36 +0200300 int gblpa = 0;
Charles Coldwell23329412013-02-21 08:25:52 -0500301 u32 estatus = 0;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500302
303 /* Check for gigabit capability */
David Duecked454232013-11-05 17:23:02 +0100304 if (phydev->supported & (SUPPORTED_1000baseT_Full |
305 SUPPORTED_1000baseT_Half)) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500306 /* We want a list of states supported by
307 * both PHYs in the link
308 */
309 gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
Heiko Schocher94c35022013-07-23 15:32:36 +0200310 if (gblpa < 0) {
Mario Six77577432018-01-15 11:08:27 +0100311 debug("Could not read MII_STAT1000. ");
312 debug("Ignoring gigabit capability\n");
Heiko Schocher94c35022013-07-23 15:32:36 +0200313 gblpa = 0;
314 }
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500315 gblpa &= phy_read(phydev,
316 MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
317 }
318
319 /* Set the baseline so we only have to set them
320 * if they're different
321 */
322 phydev->speed = SPEED_10;
323 phydev->duplex = DUPLEX_HALF;
324
325 /* Check the gigabit fields */
326 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
327 phydev->speed = SPEED_1000;
328
329 if (gblpa & PHY_1000BTSR_1000FD)
330 phydev->duplex = DUPLEX_FULL;
331
332 /* We're done! */
333 return 0;
334 }
335
336 lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
337 lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
338
Wolfgang Denka7b06ce2011-09-28 21:02:43 +0200339 if (lpa & (LPA_100FULL | LPA_100HALF)) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500340 phydev->speed = SPEED_100;
341
Wolfgang Denka7b06ce2011-09-28 21:02:43 +0200342 if (lpa & LPA_100FULL)
343 phydev->duplex = DUPLEX_FULL;
344
Mario Six77577432018-01-15 11:08:27 +0100345 } else if (lpa & LPA_10FULL) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500346 phydev->duplex = DUPLEX_FULL;
Mario Six77577432018-01-15 11:08:27 +0100347 }
Charles Coldwell23329412013-02-21 08:25:52 -0500348
Sascha Silbe2ac8d302013-07-19 12:25:10 +0200349 /*
350 * Extended status may indicate that the PHY supports
351 * 1000BASE-T/X even though the 1000BASE-T registers
352 * are missing. In this case we can't tell whether the
353 * peer also supports it, so we only check extended
354 * status if the 1000BASE-T registers are actually
355 * missing.
356 */
357 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
Charles Coldwell23329412013-02-21 08:25:52 -0500358 estatus = phy_read(phydev, MDIO_DEVAD_NONE,
359 MII_ESTATUS);
360
361 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
362 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
363 phydev->speed = SPEED_1000;
364 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
365 phydev->duplex = DUPLEX_FULL;
366 }
367
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500368 } else {
369 u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
370
371 phydev->speed = SPEED_10;
372 phydev->duplex = DUPLEX_HALF;
373
374 if (bmcr & BMCR_FULLDPLX)
375 phydev->duplex = DUPLEX_FULL;
376
377 if (bmcr & BMCR_SPEED1000)
378 phydev->speed = SPEED_1000;
379 else if (bmcr & BMCR_SPEED100)
380 phydev->speed = SPEED_100;
381 }
382
383 return 0;
384}
385
386int genphy_config(struct phy_device *phydev)
387{
388 int val;
389 u32 features;
390
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500391 features = (SUPPORTED_TP | SUPPORTED_MII
392 | SUPPORTED_AUI | SUPPORTED_FIBRE |
393 SUPPORTED_BNC);
394
395 /* Do we support autonegotiation? */
396 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
397
398 if (val < 0)
399 return val;
400
401 if (val & BMSR_ANEGCAPABLE)
402 features |= SUPPORTED_Autoneg;
403
404 if (val & BMSR_100FULL)
405 features |= SUPPORTED_100baseT_Full;
406 if (val & BMSR_100HALF)
407 features |= SUPPORTED_100baseT_Half;
408 if (val & BMSR_10FULL)
409 features |= SUPPORTED_10baseT_Full;
410 if (val & BMSR_10HALF)
411 features |= SUPPORTED_10baseT_Half;
412
413 if (val & BMSR_ESTATEN) {
414 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
415
416 if (val < 0)
417 return val;
418
419 if (val & ESTATUS_1000_TFULL)
420 features |= SUPPORTED_1000baseT_Full;
421 if (val & ESTATUS_1000_THALF)
422 features |= SUPPORTED_1000baseT_Half;
Charles Coldwell23329412013-02-21 08:25:52 -0500423 if (val & ESTATUS_1000_XFULL)
424 features |= SUPPORTED_1000baseX_Full;
425 if (val & ESTATUS_1000_XHALF)
Fabio Estevam45d601e2013-07-19 10:01:34 -0300426 features |= SUPPORTED_1000baseX_Half;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500427 }
428
Sascha Hauer6cc1e7d2016-01-13 16:59:32 +0300429 phydev->supported &= features;
430 phydev->advertising &= features;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500431
432 genphy_config_aneg(phydev);
433
434 return 0;
435}
436
437int genphy_startup(struct phy_device *phydev)
438{
Michal Simek5ff89662016-05-18 12:46:12 +0200439 int ret;
440
441 ret = genphy_update_link(phydev);
442 if (ret)
443 return ret;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500444
Michal Simek5ff89662016-05-18 12:46:12 +0200445 return genphy_parse_link(phydev);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500446}
447
448int genphy_shutdown(struct phy_device *phydev)
449{
450 return 0;
451}
452
453static struct phy_driver genphy_driver = {
454 .uid = 0xffffffff,
455 .mask = 0xffffffff,
456 .name = "Generic PHY",
Sascha Hauer6cc1e7d2016-01-13 16:59:32 +0300457 .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
458 SUPPORTED_AUI | SUPPORTED_FIBRE |
459 SUPPORTED_BNC,
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500460 .config = genphy_config,
461 .startup = genphy_startup,
462 .shutdown = genphy_shutdown,
463};
464
Siva Durga Prasad Paladugu1daad9e2019-03-15 17:46:47 +0530465int genphy_init(void)
466{
467 return phy_register(&genphy_driver);
468}
469
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500470static LIST_HEAD(phy_drivers);
471
472int phy_init(void)
473{
Siva Durga Prasad Paladuguf1137542019-03-04 16:01:30 +0100474#ifdef CONFIG_NEEDS_MANUAL_RELOC
475 /*
476 * The pointers inside phy_drivers also needs to be updated incase of
477 * manual reloc, without which these points to some invalid
478 * pre reloc address and leads to invalid accesses, hangs.
479 */
480 struct list_head *head = &phy_drivers;
481
482 head->next = (void *)head->next + gd->reloc_off;
483 head->prev = (void *)head->prev + gd->reloc_off;
484#endif
485
Florian Fainelli01b4ade2017-12-09 14:59:54 -0800486#ifdef CONFIG_B53_SWITCH
487 phy_b53_init();
488#endif
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000489#ifdef CONFIG_MV88E61XX_SWITCH
490 phy_mv88e61xx_init();
491#endif
Shaohui Xie0e548d72014-12-30 18:32:04 +0800492#ifdef CONFIG_PHY_AQUANTIA
493 phy_aquantia_init();
494#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500495#ifdef CONFIG_PHY_ATHEROS
496 phy_atheros_init();
497#endif
498#ifdef CONFIG_PHY_BROADCOM
499 phy_broadcom_init();
500#endif
Shengzhou Liuc878bdb2014-11-10 18:32:29 +0800501#ifdef CONFIG_PHY_CORTINA
502 phy_cortina_init();
503#endif
Abbie Chang556872f2021-01-14 13:34:12 -0800504#ifdef CONFIG_PHY_CORTINA_ACCESS
505 phy_cortina_access_init();
506#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500507#ifdef CONFIG_PHY_DAVICOM
508 phy_davicom_init();
509#endif
Matt Porter3bbeb792013-03-20 05:38:13 +0000510#ifdef CONFIG_PHY_ET1011C
511 phy_et1011c_init();
512#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500513#ifdef CONFIG_PHY_LXT
514 phy_lxt_init();
515#endif
516#ifdef CONFIG_PHY_MARVELL
517 phy_marvell_init();
518#endif
Alexandru Gagniuc757bb672017-07-07 11:36:57 -0700519#ifdef CONFIG_PHY_MICREL_KSZ8XXX
520 phy_micrel_ksz8xxx_init();
521#endif
522#ifdef CONFIG_PHY_MICREL_KSZ90X1
523 phy_micrel_ksz90x1_init();
Andy Fleming60ca78b2011-04-07 21:56:05 -0500524#endif
Neil Armstrong7a4c90d2017-10-18 10:02:10 +0200525#ifdef CONFIG_PHY_MESON_GXL
526 phy_meson_gxl_init();
527#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500528#ifdef CONFIG_PHY_NATSEMI
529 phy_natsemi_init();
530#endif
531#ifdef CONFIG_PHY_REALTEK
532 phy_realtek_init();
533#endif
Nobuhiro Iwamatsu61134dc2011-11-23 21:24:15 +0000534#ifdef CONFIG_PHY_SMSC
535 phy_smsc_init();
536#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500537#ifdef CONFIG_PHY_TERANETICS
538 phy_teranetics_init();
539#endif
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700540#ifdef CONFIG_PHY_TI
541 phy_ti_init();
542#endif
Andy Fleming60ca78b2011-04-07 21:56:05 -0500543#ifdef CONFIG_PHY_VITESSE
544 phy_vitesse_init();
545#endif
Siva Durga Prasad Paladugudd6cbd32016-02-05 13:22:10 +0530546#ifdef CONFIG_PHY_XILINX
547 phy_xilinx_init();
548#endif
John Haechtenee253f92016-12-09 22:15:17 +0000549#ifdef CONFIG_PHY_MSCC
550 phy_mscc_init();
551#endif
Hannes Schmelzerda494602017-03-23 15:11:43 +0100552#ifdef CONFIG_PHY_FIXED
553 phy_fixed_init();
554#endif
Samuel Mendoza-Jonasb069c4a2019-06-18 11:37:18 +1000555#ifdef CONFIG_PHY_NCSI
556 phy_ncsi_init();
557#endif
Siva Durga Prasad Paladugud5c4e1e2018-11-27 11:49:11 +0530558#ifdef CONFIG_PHY_XILINX_GMII2RGMII
559 phy_xilinx_gmii2rgmii_init();
560#endif
Siva Durga Prasad Paladugu1daad9e2019-03-15 17:46:47 +0530561 genphy_init();
562
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500563 return 0;
564}
565
566int phy_register(struct phy_driver *drv)
567{
568 INIT_LIST_HEAD(&drv->list);
569 list_add_tail(&drv->list, &phy_drivers);
570
Michal Simek5f676312015-05-13 13:40:40 +0200571#ifdef CONFIG_NEEDS_MANUAL_RELOC
572 if (drv->probe)
573 drv->probe += gd->reloc_off;
574 if (drv->config)
575 drv->config += gd->reloc_off;
576 if (drv->startup)
577 drv->startup += gd->reloc_off;
578 if (drv->shutdown)
579 drv->shutdown += gd->reloc_off;
580 if (drv->readext)
581 drv->readext += gd->reloc_off;
582 if (drv->writeext)
583 drv->writeext += gd->reloc_off;
Carlo Caione4de87e22019-02-08 17:25:06 +0000584 if (drv->read_mmd)
585 drv->read_mmd += gd->reloc_off;
586 if (drv->write_mmd)
587 drv->write_mmd += gd->reloc_off;
Michal Simek5f676312015-05-13 13:40:40 +0200588#endif
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500589 return 0;
590}
591
Alexey Brodkine476bb22016-01-13 16:59:34 +0300592int phy_set_supported(struct phy_device *phydev, u32 max_speed)
593{
594 /* The default values for phydev->supported are provided by the PHY
595 * driver "features" member, we want to reset to sane defaults first
596 * before supporting higher speeds.
597 */
598 phydev->supported &= PHY_DEFAULT_FEATURES;
599
600 switch (max_speed) {
601 default:
602 return -ENOTSUPP;
603 case SPEED_1000:
604 phydev->supported |= PHY_1000BT_FEATURES;
605 /* fall through */
606 case SPEED_100:
607 phydev->supported |= PHY_100BT_FEATURES;
608 /* fall through */
609 case SPEED_10:
610 phydev->supported |= PHY_10BT_FEATURES;
611 }
612
613 return 0;
614}
615
Kim Phillips914b0782012-10-29 13:34:34 +0000616static int phy_probe(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500617{
618 int err = 0;
619
Mario Six77577432018-01-15 11:08:27 +0100620 phydev->advertising = phydev->drv->features;
621 phydev->supported = phydev->drv->features;
622
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500623 phydev->mmds = phydev->drv->mmds;
624
625 if (phydev->drv->probe)
626 err = phydev->drv->probe(phydev);
627
628 return err;
629}
630
631static struct phy_driver *generic_for_interface(phy_interface_t interface)
632{
633#ifdef CONFIG_PHYLIB_10G
634 if (is_10g_interface(interface))
635 return &gen10g_driver;
636#endif
637
638 return &genphy_driver;
639}
640
Kim Phillips914b0782012-10-29 13:34:34 +0000641static struct phy_driver *get_phy_driver(struct phy_device *phydev,
Mario Six77577432018-01-15 11:08:27 +0100642 phy_interface_t interface)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500643{
644 struct list_head *entry;
645 int phy_id = phydev->phy_id;
646 struct phy_driver *drv = NULL;
647
648 list_for_each(entry, &phy_drivers) {
649 drv = list_entry(entry, struct phy_driver, list);
650 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
651 return drv;
652 }
653
654 /* If we made it here, there's no driver for this PHY */
655 return generic_for_interface(interface);
656}
657
Kim Phillips914b0782012-10-29 13:34:34 +0000658static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
Pankaj Bansal3c43a482018-11-16 06:26:18 +0000659 u32 phy_id, bool is_c45,
Kim Phillips914b0782012-10-29 13:34:34 +0000660 phy_interface_t interface)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500661{
662 struct phy_device *dev;
663
Mario Six77577432018-01-15 11:08:27 +0100664 /*
665 * We allocate the device, and initialize the
666 * default values
667 */
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500668 dev = malloc(sizeof(*dev));
669 if (!dev) {
670 printf("Failed to allocate PHY device for %s:%d\n",
Vladimir Olteanfb73b122020-07-16 18:09:08 +0800671 bus ? bus->name : "(null bus)", addr);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500672 return NULL;
673 }
674
675 memset(dev, 0, sizeof(*dev));
676
677 dev->duplex = -1;
Mugunthan V Nbbc97ba2015-09-03 15:50:21 +0530678 dev->link = 0;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500679 dev->interface = interface;
680
Grygorii Strashko6189c062018-07-05 12:02:48 -0500681#ifdef CONFIG_DM_ETH
682 dev->node = ofnode_null();
683#endif
684
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500685 dev->autoneg = AUTONEG_ENABLE;
686
687 dev->addr = addr;
688 dev->phy_id = phy_id;
Pankaj Bansal3c43a482018-11-16 06:26:18 +0000689 dev->is_c45 = is_c45;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500690 dev->bus = bus;
691
692 dev->drv = get_phy_driver(dev, interface);
693
Siva Durga Prasad Paladugua7228482019-03-04 16:02:11 +0100694 if (phy_probe(dev)) {
695 printf("%s, PHY probe failed\n", __func__);
696 return NULL;
697 }
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500698
Vladimir Olteanfb73b122020-07-16 18:09:08 +0800699 if (addr >= 0 && addr < PHY_MAX_ADDR && phy_id != PHY_FIXED_ID)
Michal Simek02a99d72018-12-19 16:57:38 +0100700 bus->phymap[addr] = dev;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500701
702 return dev;
703}
704
705/**
706 * get_phy_id - reads the specified addr for its ID.
707 * @bus: the target MII bus
708 * @addr: PHY address on the MII bus
709 * @phy_id: where to store the ID retrieved.
710 *
711 * Description: Reads the ID registers of the PHY at @addr on the
712 * @bus, stores it in @phy_id and returns zero on success.
713 */
Shengzhou Liu072b0fa2015-04-07 18:46:32 +0800714int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500715{
716 int phy_reg;
717
Mario Six77577432018-01-15 11:08:27 +0100718 /*
719 * Grab the bits from PHYIR1, and put them
720 * in the upper half
721 */
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500722 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
723
724 if (phy_reg < 0)
725 return -EIO;
726
727 *phy_id = (phy_reg & 0xffff) << 16;
728
729 /* Grab the bits from PHYIR2, and put them in the lower half */
730 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
731
732 if (phy_reg < 0)
733 return -EIO;
734
735 *phy_id |= (phy_reg & 0xffff);
736
737 return 0;
738}
739
Troy Kisky9519bc52012-10-22 16:40:43 +0000740static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
Mario Six77577432018-01-15 11:08:27 +0100741 uint phy_mask, int devad,
742 phy_interface_t interface)
Troy Kisky9519bc52012-10-22 16:40:43 +0000743{
744 u32 phy_id = 0xffffffff;
Pankaj Bansal3c43a482018-11-16 06:26:18 +0000745 bool is_c45;
Mario Six77577432018-01-15 11:08:27 +0100746
Troy Kisky9519bc52012-10-22 16:40:43 +0000747 while (phy_mask) {
748 int addr = ffs(phy_mask) - 1;
749 int r = get_phy_id(bus, addr, devad, &phy_id);
Alex Marginean920fe672019-07-05 12:28:55 +0300750
751 /*
752 * If the PHY ID is flat 0 we ignore it. There are C45 PHYs
753 * that return all 0s for C22 reads (like Aquantia AQR112) and
754 * there are C22 PHYs that return all 0s for C45 reads (like
755 * Atheros AR8035).
756 */
757 if (r == 0 && phy_id == 0)
758 goto next;
759
Troy Kisky9519bc52012-10-22 16:40:43 +0000760 /* If the PHY ID is mostly f's, we didn't find anything */
Pankaj Bansal3c43a482018-11-16 06:26:18 +0000761 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff) {
762 is_c45 = (devad == MDIO_DEVAD_NONE) ? false : true;
763 return phy_device_create(bus, addr, phy_id, is_c45,
764 interface);
765 }
Alex Marginean920fe672019-07-05 12:28:55 +0300766next:
Troy Kisky9519bc52012-10-22 16:40:43 +0000767 phy_mask &= ~(1 << addr);
768 }
769 return NULL;
770}
771
772static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
Mario Six77577432018-01-15 11:08:27 +0100773 uint phy_mask,
774 phy_interface_t interface)
Troy Kisky9519bc52012-10-22 16:40:43 +0000775{
776 /* If we have one, return the existing device, with new interface */
777 while (phy_mask) {
778 int addr = ffs(phy_mask) - 1;
Mario Six77577432018-01-15 11:08:27 +0100779
Troy Kisky9519bc52012-10-22 16:40:43 +0000780 if (bus->phymap[addr]) {
781 bus->phymap[addr]->interface = interface;
782 return bus->phymap[addr];
783 }
784 phy_mask &= ~(1 << addr);
785 }
786 return NULL;
787}
788
789static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
Mario Six77577432018-01-15 11:08:27 +0100790 uint phy_mask,
791 phy_interface_t interface)
Troy Kisky9519bc52012-10-22 16:40:43 +0000792{
Troy Kisky9519bc52012-10-22 16:40:43 +0000793 struct phy_device *phydev;
Florin Chiculita0439bee2020-04-29 14:25:48 +0300794 int devad[] = {
795 /* Clause-22 */
796 MDIO_DEVAD_NONE,
797 /* Clause-45 */
798 MDIO_MMD_PMAPMD,
799 MDIO_MMD_WIS,
800 MDIO_MMD_PCS,
801 MDIO_MMD_PHYXS,
802 MDIO_MMD_VEND1,
803 };
804 int i, devad_cnt;
Troy Kisky9519bc52012-10-22 16:40:43 +0000805
Florin Chiculita0439bee2020-04-29 14:25:48 +0300806 devad_cnt = sizeof(devad)/sizeof(int);
Troy Kisky9519bc52012-10-22 16:40:43 +0000807 phydev = search_for_existing_phy(bus, phy_mask, interface);
808 if (phydev)
809 return phydev;
Florin Chiculita0439bee2020-04-29 14:25:48 +0300810 /* try different access clauses */
811 for (i = 0; i < devad_cnt; i++) {
Troy Kisky9519bc52012-10-22 16:40:43 +0000812 phydev = create_phy_by_mask(bus, phy_mask,
Florin Chiculita0439bee2020-04-29 14:25:48 +0300813 devad[i], interface);
Troy Kisky9519bc52012-10-22 16:40:43 +0000814 if (IS_ERR(phydev))
815 return NULL;
816 if (phydev)
817 return phydev;
818 }
Bin Meng59c6d892015-10-07 21:19:30 -0700819
820 debug("\n%s PHY: ", bus->name);
821 while (phy_mask) {
822 int addr = ffs(phy_mask) - 1;
Mario Six77577432018-01-15 11:08:27 +0100823
Bin Meng59c6d892015-10-07 21:19:30 -0700824 debug("%d ", addr);
825 phy_mask &= ~(1 << addr);
826 }
827 debug("not found\n");
Bin Meng0776c572015-10-07 21:19:29 -0700828
829 return NULL;
Troy Kisky9519bc52012-10-22 16:40:43 +0000830}
831
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500832/**
Mario Six77577432018-01-15 11:08:27 +0100833 * get_phy_device - reads the specified PHY device and returns its
834 * @phy_device struct
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500835 * @bus: the target MII bus
836 * @addr: PHY address on the MII bus
837 *
838 * Description: Reads the ID registers of the PHY at @addr on the
839 * @bus, then allocates and returns the phy_device to represent it.
840 */
Kim Phillips914b0782012-10-29 13:34:34 +0000841static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
842 phy_interface_t interface)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500843{
Troy Kisky9519bc52012-10-22 16:40:43 +0000844 return get_phy_device_by_mask(bus, 1 << addr, interface);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500845}
846
847int phy_reset(struct phy_device *phydev)
848{
849 int reg;
850 int timeout = 500;
851 int devad = MDIO_DEVAD_NONE;
852
Shaohui Xie62a7b922016-01-28 15:55:46 +0800853 if (phydev->flags & PHY_FLAG_BROKEN_RESET)
854 return 0;
855
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500856#ifdef CONFIG_PHYLIB_10G
857 /* If it's 10G, we need to issue reset through one of the MMDs */
858 if (is_10g_interface(phydev->interface)) {
859 if (!phydev->mmds)
860 gen10g_discover_mmds(phydev);
861
862 devad = ffs(phydev->mmds) - 1;
863 }
864#endif
865
Stefan Agnere64013d2015-12-09 11:21:25 -0800866 if (phy_write(phydev, devad, MII_BMCR, BMCR_RESET) < 0) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500867 debug("PHY reset failed\n");
868 return -1;
869 }
870
871#ifdef CONFIG_PHY_RESET_DELAY
872 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
873#endif
874 /*
875 * Poll the control register for the reset bit to go to 0 (it is
876 * auto-clearing). This should happen within 0.5 seconds per the
877 * IEEE spec.
878 */
Stefan Agnere64013d2015-12-09 11:21:25 -0800879 reg = phy_read(phydev, devad, MII_BMCR);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500880 while ((reg & BMCR_RESET) && timeout--) {
881 reg = phy_read(phydev, devad, MII_BMCR);
882
883 if (reg < 0) {
884 debug("PHY status read failed\n");
885 return -1;
886 }
887 udelay(1000);
888 }
889
890 if (reg & BMCR_RESET) {
891 puts("PHY reset timed out\n");
892 return -1;
893 }
894
895 return 0;
896}
897
898int miiphy_reset(const char *devname, unsigned char addr)
899{
900 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
901 struct phy_device *phydev;
902
903 /*
904 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
905 * If later code tries to connect with the right interface, this will
906 * be corrected by get_phy_device in phy_connect()
907 */
908 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
909
910 return phy_reset(phydev);
911}
912
Mario Six77577432018-01-15 11:08:27 +0100913struct phy_device *phy_find_by_mask(struct mii_dev *bus, uint phy_mask,
914 phy_interface_t interface)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500915{
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500916 /* Reset the bus */
Jörg Krause71b1d862015-07-15 15:18:22 +0200917 if (bus->reset) {
Vladimir Zapolskiy46f10bb2011-09-05 07:24:07 +0000918 bus->reset(bus);
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500919
Jörg Krause71b1d862015-07-15 15:18:22 +0200920 /* Wait 15ms to make sure the PHY has come out of hard reset */
Mario Six77577432018-01-15 11:08:27 +0100921 mdelay(15);
Jörg Krause71b1d862015-07-15 15:18:22 +0200922 }
923
Troy Kisky9519bc52012-10-22 16:40:43 +0000924 return get_phy_device_by_mask(bus, phy_mask, interface);
925}
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500926
Simon Glassdbad3462015-04-05 16:07:39 -0600927#ifdef CONFIG_DM_ETH
928void phy_connect_dev(struct phy_device *phydev, struct udevice *dev)
929#else
Troy Kisky9519bc52012-10-22 16:40:43 +0000930void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
Simon Glassdbad3462015-04-05 16:07:39 -0600931#endif
Troy Kisky9519bc52012-10-22 16:40:43 +0000932{
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500933 /* Soft Reset the PHY */
934 phy_reset(phydev);
Bin Mengf87a15c2015-10-07 21:19:31 -0700935 if (phydev->dev && phydev->dev != dev) {
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500936 printf("%s:%d is connected to %s. Reconnecting to %s\n",
Mario Six77577432018-01-15 11:08:27 +0100937 phydev->bus->name, phydev->addr,
938 phydev->dev->name, dev->name);
Troy Kisky9519bc52012-10-22 16:40:43 +0000939 }
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500940 phydev->dev = dev;
Wolfgang Denka71ebc42011-07-24 21:39:10 +0000941 debug("%s connected to %s\n", dev->name, phydev->drv->name);
Troy Kisky9519bc52012-10-22 16:40:43 +0000942}
Siva Durga Prasad Paladugud5c4e1e2018-11-27 11:49:11 +0530943
944#ifdef CONFIG_PHY_XILINX_GMII2RGMII
945#ifdef CONFIG_DM_ETH
946static struct phy_device *phy_connect_gmii2rgmii(struct mii_dev *bus,
947 struct udevice *dev,
948 phy_interface_t interface)
949#else
950static struct phy_device *phy_connect_gmii2rgmii(struct mii_dev *bus,
951 struct eth_device *dev,
952 phy_interface_t interface)
953#endif
954{
955 struct phy_device *phydev = NULL;
956 int sn = dev_of_offset(dev);
957 int off;
958
959 while (sn > 0) {
960 off = fdt_node_offset_by_compatible(gd->fdt_blob, sn,
961 "xlnx,gmii-to-rgmii-1.0");
962 if (off > 0) {
963 phydev = phy_device_create(bus, off,
964 PHY_GMII2RGMII_ID, false,
965 interface);
966 break;
967 }
968 if (off == -FDT_ERR_NOTFOUND)
969 sn = fdt_first_subnode(gd->fdt_blob, sn);
970 else
971 printf("%s: Error finding compat string:%d\n",
972 __func__, off);
973 }
974
975 return phydev;
976}
977#endif
Troy Kisky9519bc52012-10-22 16:40:43 +0000978
Siva Durga Prasad Paladugu03f937a2018-11-27 11:49:10 +0530979#ifdef CONFIG_PHY_FIXED
Vladimir Oltean8c089f72021-01-25 14:23:52 +0200980/**
981 * fixed_phy_create() - create an unconnected fixed-link pseudo-PHY device
982 * @node: OF node for the container of the fixed-link node
983 *
984 * Description: Creates a struct phy_device based on a fixed-link of_node
985 * description. Can be used without phy_connect by drivers which do not expose
986 * a UCLASS_ETH udevice.
987 */
988struct phy_device *fixed_phy_create(ofnode node)
989{
990 phy_interface_t interface = PHY_INTERFACE_MODE_NONE;
991 const char *if_str;
992 ofnode subnode;
993
994 if_str = ofnode_read_string(node, "phy-mode");
995 if (!if_str) {
996 if_str = ofnode_read_string(node, "phy-interface-type");
997 }
998 if (if_str) {
999 interface = phy_get_interface_by_name(if_str);
1000 }
1001
1002 subnode = ofnode_find_subnode(node, "fixed-link");
1003 if (!ofnode_valid(subnode)) {
1004 return NULL;
1005 }
1006
1007 return phy_device_create(NULL, ofnode_to_offset(subnode), PHY_FIXED_ID,
1008 false, interface);
1009}
1010
Simon Glassdbad3462015-04-05 16:07:39 -06001011#ifdef CONFIG_DM_ETH
Siva Durga Prasad Paladugu03f937a2018-11-27 11:49:10 +05301012static struct phy_device *phy_connect_fixed(struct mii_dev *bus,
1013 struct udevice *dev,
1014 phy_interface_t interface)
Simon Glassdbad3462015-04-05 16:07:39 -06001015#else
Siva Durga Prasad Paladugu03f937a2018-11-27 11:49:10 +05301016static struct phy_device *phy_connect_fixed(struct mii_dev *bus,
1017 struct eth_device *dev,
1018 phy_interface_t interface)
Simon Glassdbad3462015-04-05 16:07:39 -06001019#endif
Troy Kisky9519bc52012-10-22 16:40:43 +00001020{
Hannes Schmelzerda494602017-03-23 15:11:43 +01001021 struct phy_device *phydev = NULL;
Hannes Schmelzerda494602017-03-23 15:11:43 +01001022 int sn;
1023 const char *name;
Mario Six77577432018-01-15 11:08:27 +01001024
Simon Glass7a494432017-05-17 17:18:09 -06001025 sn = fdt_first_subnode(gd->fdt_blob, dev_of_offset(dev));
Hannes Schmelzerda494602017-03-23 15:11:43 +01001026 while (sn > 0) {
1027 name = fdt_get_name(gd->fdt_blob, sn, NULL);
Mario Six77577432018-01-15 11:08:27 +01001028 if (name && strcmp(name, "fixed-link") == 0) {
Pankaj Bansal3c43a482018-11-16 06:26:18 +00001029 phydev = phy_device_create(bus, sn, PHY_FIXED_ID, false,
1030 interface);
Hannes Schmelzerda494602017-03-23 15:11:43 +01001031 break;
1032 }
1033 sn = fdt_next_subnode(gd->fdt_blob, sn);
1034 }
Siva Durga Prasad Paladugu03f937a2018-11-27 11:49:10 +05301035
1036 return phydev;
1037}
Hannes Schmelzerda494602017-03-23 15:11:43 +01001038#endif
Siva Durga Prasad Paladugu03f937a2018-11-27 11:49:10 +05301039
1040#ifdef CONFIG_DM_ETH
1041struct phy_device *phy_connect(struct mii_dev *bus, int addr,
1042 struct udevice *dev,
1043 phy_interface_t interface)
1044#else
1045struct phy_device *phy_connect(struct mii_dev *bus, int addr,
1046 struct eth_device *dev,
1047 phy_interface_t interface)
1048#endif
1049{
1050 struct phy_device *phydev = NULL;
Priyanka Jain11691fd2019-11-05 04:05:11 +00001051 uint mask = (addr >= 0) ? (1 << addr) : 0xffffffff;
Siva Durga Prasad Paladugu03f937a2018-11-27 11:49:10 +05301052
1053#ifdef CONFIG_PHY_FIXED
1054 phydev = phy_connect_fixed(bus, dev, interface);
1055#endif
Samuel Mendoza-Jonasb069c4a2019-06-18 11:37:18 +10001056
1057#ifdef CONFIG_PHY_NCSI
1058 if (!phydev)
1059 phydev = phy_device_create(bus, 0, PHY_NCSI_ID, false, interface);
1060#endif
1061
Siva Durga Prasad Paladugud5c4e1e2018-11-27 11:49:11 +05301062#ifdef CONFIG_PHY_XILINX_GMII2RGMII
1063 if (!phydev)
1064 phydev = phy_connect_gmii2rgmii(bus, dev, interface);
1065#endif
Siva Durga Prasad Paladugu03f937a2018-11-27 11:49:10 +05301066
Mario Six77577432018-01-15 11:08:27 +01001067 if (!phydev)
Hannes Schmelzer02505e92019-03-29 09:54:05 +01001068 phydev = phy_find_by_mask(bus, mask, interface);
Andy Flemingaecf6fc2011-04-08 02:10:27 -05001069
Troy Kisky9519bc52012-10-22 16:40:43 +00001070 if (phydev)
1071 phy_connect_dev(phydev, dev);
1072 else
1073 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
Andy Flemingaecf6fc2011-04-08 02:10:27 -05001074 return phydev;
1075}
1076
Timur Tabi251180b2012-07-05 10:33:18 +00001077/*
1078 * Start the PHY. Returns 0 on success, or a negative error code.
1079 */
Andy Flemingaecf6fc2011-04-08 02:10:27 -05001080int phy_startup(struct phy_device *phydev)
1081{
1082 if (phydev->drv->startup)
Timur Tabi251180b2012-07-05 10:33:18 +00001083 return phydev->drv->startup(phydev);
Andy Flemingaecf6fc2011-04-08 02:10:27 -05001084
1085 return 0;
1086}
1087
Jeroen Hofsteee4f89472014-10-08 22:57:26 +02001088__weak int board_phy_config(struct phy_device *phydev)
Andy Flemingaecf6fc2011-04-08 02:10:27 -05001089{
Troy Kisky85846412012-02-07 14:08:49 +00001090 if (phydev->drv->config)
1091 return phydev->drv->config(phydev);
Andy Flemingaecf6fc2011-04-08 02:10:27 -05001092 return 0;
1093}
1094
Andy Flemingaecf6fc2011-04-08 02:10:27 -05001095int phy_config(struct phy_device *phydev)
1096{
Andy Flemingaecf6fc2011-04-08 02:10:27 -05001097 /* Invoke an optional board-specific helper */
Michal Simek24ce2322016-05-18 14:37:23 +02001098 return board_phy_config(phydev);
Andy Flemingaecf6fc2011-04-08 02:10:27 -05001099}
1100
1101int phy_shutdown(struct phy_device *phydev)
1102{
1103 if (phydev->drv->shutdown)
1104 phydev->drv->shutdown(phydev);
1105
1106 return 0;
1107}
Simon Glassdbad3462015-04-05 16:07:39 -06001108
1109int phy_get_interface_by_name(const char *str)
1110{
1111 int i;
1112
1113 for (i = 0; i < PHY_INTERFACE_MODE_COUNT; i++) {
1114 if (!strcmp(str, phy_interface_strings[i]))
1115 return i;
1116 }
1117
1118 return -1;
1119}