]> Git Repo - J-u-boot.git/blame - drivers/net/phy/phy.c
Merge tag 'u-boot-imx-master-20250127' of https://gitlab.denx.de/u-boot/custodians...
[J-u-boot.git] / drivers / net / phy / phy.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
5f184715
AF
2/*
3 * Generic PHY Management code
4 *
5f184715
AF
5 * Copyright 2011 Freescale Semiconductor, Inc.
6 * author Andy Fleming
7 *
8 * Based loosely off of Linux's PHY Lib
9 */
24b852a7 10#include <console.h>
c74c8e66 11#include <dm.h>
f7ae49fc 12#include <log.h>
5f184715
AF
13#include <malloc.h>
14#include <net.h>
15#include <command.h>
16#include <miiphy.h>
17#include <phy.h>
18#include <errno.h>
401d1c4f 19#include <asm/global_data.h>
e99a6efa
MV
20#include <asm-generic/gpio.h>
21#include <dm/device_compat.h>
676fbd3d 22#include <dm/of_extra.h>
cd93d625 23#include <linux/bitops.h>
c05ed00a 24#include <linux/delay.h>
1adb406b 25#include <linux/err.h>
597fe041 26#include <linux/compiler.h>
5f184715 27
abbfcbe5
MS
28DECLARE_GLOBAL_DATA_PTR;
29
5f184715
AF
30/* Generic PHY support and helper functions */
31
32/**
8d631203 33 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
5f184715
AF
34 * @phydev: target phy_device struct
35 *
36 * Description: Writes MII_ADVERTISE with the appropriate values,
37 * after sanitizing the values to make sure we only advertise
38 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
39 * hasn't changed, and > 0 if it has changed.
40 */
960d70c6 41static int genphy_config_advert(struct phy_device *phydev)
5f184715
AF
42{
43 u32 advertise;
bbdcaff1 44 int oldadv, adv, bmsr;
5f184715
AF
45 int err, changed = 0;
46
bbdcaff1 47 /* Only allow advertising what this PHY supports */
5f184715
AF
48 phydev->advertising &= phydev->supported;
49 advertise = phydev->advertising;
50
51 /* Setup standard advertisement */
bbdcaff1
FF
52 adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
53 oldadv = adv;
5f184715
AF
54
55 if (adv < 0)
56 return adv;
57
58 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
59 ADVERTISE_PAUSE_ASYM);
60 if (advertise & ADVERTISED_10baseT_Half)
61 adv |= ADVERTISE_10HALF;
62 if (advertise & ADVERTISED_10baseT_Full)
63 adv |= ADVERTISE_10FULL;
64 if (advertise & ADVERTISED_100baseT_Half)
65 adv |= ADVERTISE_100HALF;
66 if (advertise & ADVERTISED_100baseT_Full)
67 adv |= ADVERTISE_100FULL;
68 if (advertise & ADVERTISED_Pause)
69 adv |= ADVERTISE_PAUSE_CAP;
70 if (advertise & ADVERTISED_Asym_Pause)
71 adv |= ADVERTISE_PAUSE_ASYM;
de1d786e
CC
72 if (advertise & ADVERTISED_1000baseX_Half)
73 adv |= ADVERTISE_1000XHALF;
74 if (advertise & ADVERTISED_1000baseX_Full)
75 adv |= ADVERTISE_1000XFULL;
5f184715
AF
76
77 if (adv != oldadv) {
78 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
79
80 if (err < 0)
81 return err;
82 changed = 1;
83 }
84
bbdcaff1
FF
85 bmsr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
86 if (bmsr < 0)
87 return bmsr;
88
89 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
90 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
91 * logical 1.
92 */
93 if (!(bmsr & BMSR_ESTATEN))
94 return changed;
95
5f184715 96 /* Configure gigabit if it's supported */
bbdcaff1
FF
97 adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
98 oldadv = adv;
99
100 if (adv < 0)
101 return adv;
5f184715 102
bbdcaff1 103 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
5f184715 104
bbdcaff1
FF
105 if (phydev->supported & (SUPPORTED_1000baseT_Half |
106 SUPPORTED_1000baseT_Full)) {
5f184715
AF
107 if (advertise & SUPPORTED_1000baseT_Half)
108 adv |= ADVERTISE_1000HALF;
109 if (advertise & SUPPORTED_1000baseT_Full)
110 adv |= ADVERTISE_1000FULL;
bbdcaff1 111 }
5f184715 112
bbdcaff1
FF
113 if (adv != oldadv)
114 changed = 1;
5f184715 115
bbdcaff1
FF
116 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, adv);
117 if (err < 0)
118 return err;
5f184715
AF
119
120 return changed;
121}
122
5f184715
AF
123/**
124 * genphy_setup_forced - configures/forces speed/duplex from @phydev
125 * @phydev: target phy_device struct
126 *
127 * Description: Configures MII_BMCR to force speed/duplex
128 * to the values in phydev. Assumes that the values are valid.
129 */
960d70c6 130static int genphy_setup_forced(struct phy_device *phydev)
5f184715
AF
131{
132 int err;
53b0c38c 133 int ctl = BMCR_ANRESTART;
5f184715 134
8d631203
MS
135 phydev->pause = 0;
136 phydev->asym_pause = 0;
5f184715 137
8d631203 138 if (phydev->speed == SPEED_1000)
5f184715 139 ctl |= BMCR_SPEED1000;
8d631203 140 else if (phydev->speed == SPEED_100)
5f184715
AF
141 ctl |= BMCR_SPEED100;
142
8d631203 143 if (phydev->duplex == DUPLEX_FULL)
5f184715
AF
144 ctl |= BMCR_FULLDPLX;
145
146 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
147
148 return err;
149}
150
5f184715
AF
151/**
152 * genphy_restart_aneg - Enable and Restart Autonegotiation
153 * @phydev: target phy_device struct
154 */
155int genphy_restart_aneg(struct phy_device *phydev)
156{
157 int ctl;
158
159 ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
160
161 if (ctl < 0)
162 return ctl;
163
164 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
165
166 /* Don't isolate the PHY if we're negotiating */
167 ctl &= ~(BMCR_ISOLATE);
168
169 ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
170
171 return ctl;
172}
173
5f184715
AF
174/**
175 * genphy_config_aneg - restart auto-negotiation or write BMCR
176 * @phydev: target phy_device struct
177 *
178 * Description: If auto-negotiation is enabled, we configure the
179 * advertising, and then restart auto-negotiation. If it is not
180 * enabled, then we write the BMCR.
181 */
182int genphy_config_aneg(struct phy_device *phydev)
183{
184 int result;
185
8d631203 186 if (phydev->autoneg != AUTONEG_ENABLE)
5f184715
AF
187 return genphy_setup_forced(phydev);
188
189 result = genphy_config_advert(phydev);
190
191 if (result < 0) /* error */
192 return result;
193
194 if (result == 0) {
8d631203
MS
195 /*
196 * Advertisment hasn't changed, but maybe aneg was never on to
197 * begin with? Or maybe phy was isolated?
198 */
5f184715
AF
199 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
200
201 if (ctl < 0)
202 return ctl;
203
204 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
205 result = 1; /* do restart aneg */
206 }
207
8d631203
MS
208 /*
209 * Only restart aneg if we are advertising something different
210 * than we were before.
211 */
5f184715
AF
212 if (result > 0)
213 result = genphy_restart_aneg(phydev);
214
215 return result;
216}
217
218/**
219 * genphy_update_link - update link status in @phydev
220 * @phydev: target phy_device struct
221 *
222 * Description: Update the value in phydev->link to reflect the
223 * current link value. In order to do this, we need to read
224 * the status register twice, keeping the second value.
225 */
226int genphy_update_link(struct phy_device *phydev)
227{
228 unsigned int mii_reg;
229
230 /*
231 * Wait if the link is up, and autonegotiation is in progress
232 * (ie - we're capable and it's not done)
233 */
234 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
235
236 /*
237 * If we already saw the link up, and it hasn't gone down, then
238 * we don't need to wait for autoneg again
239 */
240 if (phydev->link && mii_reg & BMSR_LSTATUS)
241 return 0;
242
1f9e672c
AM
243 if ((phydev->autoneg == AUTONEG_ENABLE) &&
244 !(mii_reg & BMSR_ANEGCOMPLETE)) {
5f184715
AF
245 int i = 0;
246
247 printf("%s Waiting for PHY auto negotiation to complete",
8d631203 248 phydev->dev->name);
5f184715
AF
249 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
250 /*
251 * Timeout reached ?
252 */
66103759 253 if (i > (CONFIG_PHY_ANEG_TIMEOUT / 50)) {
5f184715
AF
254 printf(" TIMEOUT !\n");
255 phydev->link = 0;
ef5e821b 256 return -ETIMEDOUT;
5f184715
AF
257 }
258
259 if (ctrlc()) {
260 puts("user interrupt!\n");
261 phydev->link = 0;
262 return -EINTR;
263 }
264
27c3f70f 265 if ((i++ % 10) == 0)
5f184715
AF
266 printf(".");
267
5f184715 268 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
27c3f70f 269 mdelay(50); /* 50 ms */
5f184715
AF
270 }
271 printf(" done\n");
272 phydev->link = 1;
273 } else {
274 /* Read the link a second time to clear the latched state */
275 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
276
277 if (mii_reg & BMSR_LSTATUS)
278 phydev->link = 1;
279 else
280 phydev->link = 0;
281 }
282
283 return 0;
284}
285
286/*
287 * Generic function which updates the speed and duplex. If
288 * autonegotiation is enabled, it uses the AND of the link
289 * partner's advertised capabilities and our advertised
290 * capabilities. If autonegotiation is disabled, we use the
291 * appropriate bits in the control register.
292 *
293 * Stolen from Linux's mii.c and phy_device.c
294 */
e2043f5c 295int genphy_parse_link(struct phy_device *phydev)
5f184715
AF
296{
297 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
298
299 /* We're using autonegotiation */
1f9e672c 300 if (phydev->autoneg == AUTONEG_ENABLE) {
5f184715 301 u32 lpa = 0;
f6d1f6e4 302 int gblpa = 0;
de1d786e 303 u32 estatus = 0;
5f184715
AF
304
305 /* Check for gigabit capability */
3a530d1b
DD
306 if (phydev->supported & (SUPPORTED_1000baseT_Full |
307 SUPPORTED_1000baseT_Half)) {
5f184715
AF
308 /* We want a list of states supported by
309 * both PHYs in the link
310 */
311 gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
f6d1f6e4 312 if (gblpa < 0) {
8d631203
MS
313 debug("Could not read MII_STAT1000. ");
314 debug("Ignoring gigabit capability\n");
f6d1f6e4
HS
315 gblpa = 0;
316 }
5f184715
AF
317 gblpa &= phy_read(phydev,
318 MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
319 }
320
321 /* Set the baseline so we only have to set them
322 * if they're different
323 */
324 phydev->speed = SPEED_10;
325 phydev->duplex = DUPLEX_HALF;
326
327 /* Check the gigabit fields */
328 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
329 phydev->speed = SPEED_1000;
330
331 if (gblpa & PHY_1000BTSR_1000FD)
332 phydev->duplex = DUPLEX_FULL;
333
334 /* We're done! */
335 return 0;
336 }
337
338 lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
339 lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
340
0dcfb0fc 341 if (lpa & (LPA_100FULL | LPA_100HALF)) {
5f184715
AF
342 phydev->speed = SPEED_100;
343
0dcfb0fc
WD
344 if (lpa & LPA_100FULL)
345 phydev->duplex = DUPLEX_FULL;
346
8d631203 347 } else if (lpa & LPA_10FULL) {
5f184715 348 phydev->duplex = DUPLEX_FULL;
8d631203 349 }
de1d786e 350
9ba30f6b
SS
351 /*
352 * Extended status may indicate that the PHY supports
353 * 1000BASE-T/X even though the 1000BASE-T registers
354 * are missing. In this case we can't tell whether the
355 * peer also supports it, so we only check extended
356 * status if the 1000BASE-T registers are actually
357 * missing.
358 */
359 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
de1d786e
CC
360 estatus = phy_read(phydev, MDIO_DEVAD_NONE,
361 MII_ESTATUS);
362
363 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
364 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
365 phydev->speed = SPEED_1000;
366 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
367 phydev->duplex = DUPLEX_FULL;
368 }
369
5f184715
AF
370 } else {
371 u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
372
373 phydev->speed = SPEED_10;
374 phydev->duplex = DUPLEX_HALF;
375
376 if (bmcr & BMCR_FULLDPLX)
377 phydev->duplex = DUPLEX_FULL;
378
379 if (bmcr & BMCR_SPEED1000)
380 phydev->speed = SPEED_1000;
381 else if (bmcr & BMCR_SPEED100)
382 phydev->speed = SPEED_100;
383 }
384
385 return 0;
386}
387
388int genphy_config(struct phy_device *phydev)
389{
390 int val;
391 u32 features;
392
5f184715
AF
393 features = (SUPPORTED_TP | SUPPORTED_MII
394 | SUPPORTED_AUI | SUPPORTED_FIBRE |
395 SUPPORTED_BNC);
396
397 /* Do we support autonegotiation? */
398 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
399
400 if (val < 0)
401 return val;
402
403 if (val & BMSR_ANEGCAPABLE)
404 features |= SUPPORTED_Autoneg;
405
406 if (val & BMSR_100FULL)
407 features |= SUPPORTED_100baseT_Full;
408 if (val & BMSR_100HALF)
409 features |= SUPPORTED_100baseT_Half;
410 if (val & BMSR_10FULL)
411 features |= SUPPORTED_10baseT_Full;
412 if (val & BMSR_10HALF)
413 features |= SUPPORTED_10baseT_Half;
414
415 if (val & BMSR_ESTATEN) {
416 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
417
418 if (val < 0)
419 return val;
420
421 if (val & ESTATUS_1000_TFULL)
422 features |= SUPPORTED_1000baseT_Full;
423 if (val & ESTATUS_1000_THALF)
424 features |= SUPPORTED_1000baseT_Half;
de1d786e
CC
425 if (val & ESTATUS_1000_XFULL)
426 features |= SUPPORTED_1000baseX_Full;
427 if (val & ESTATUS_1000_XHALF)
9a5dad23 428 features |= SUPPORTED_1000baseX_Half;
5f184715
AF
429 }
430
44bc3174
SH
431 phydev->supported &= features;
432 phydev->advertising &= features;
5f184715
AF
433
434 genphy_config_aneg(phydev);
435
436 return 0;
437}
438
439int genphy_startup(struct phy_device *phydev)
440{
b733c278 441 int ret;
5f184715 442
b733c278
MS
443 ret = genphy_update_link(phydev);
444 if (ret)
445 return ret;
446
447 return genphy_parse_link(phydev);
5f184715
AF
448}
449
450int genphy_shutdown(struct phy_device *phydev)
451{
452 return 0;
453}
454
f7053296 455U_BOOT_PHY_DRIVER(genphy) = {
5f184715
AF
456 .uid = 0xffffffff,
457 .mask = 0xffffffff,
458 .name = "Generic PHY",
44bc3174
SH
459 .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
460 SUPPORTED_AUI | SUPPORTED_FIBRE |
461 SUPPORTED_BNC,
5f184715
AF
462 .config = genphy_config,
463 .startup = genphy_startup,
464 .shutdown = genphy_shutdown,
465};
466
b18acb0a
AB
467int phy_set_supported(struct phy_device *phydev, u32 max_speed)
468{
469 /* The default values for phydev->supported are provided by the PHY
470 * driver "features" member, we want to reset to sane defaults first
471 * before supporting higher speeds.
472 */
473 phydev->supported &= PHY_DEFAULT_FEATURES;
474
475 switch (max_speed) {
476 default:
477 return -ENOTSUPP;
478 case SPEED_1000:
479 phydev->supported |= PHY_1000BT_FEATURES;
480 /* fall through */
481 case SPEED_100:
482 phydev->supported |= PHY_100BT_FEATURES;
483 /* fall through */
484 case SPEED_10:
485 phydev->supported |= PHY_10BT_FEATURES;
486 }
487
488 return 0;
489}
490
960d70c6 491static int phy_probe(struct phy_device *phydev)
5f184715
AF
492{
493 int err = 0;
494
8d631203
MS
495 phydev->advertising = phydev->drv->features;
496 phydev->supported = phydev->drv->features;
497
5f184715
AF
498 phydev->mmds = phydev->drv->mmds;
499
500 if (phydev->drv->probe)
501 err = phydev->drv->probe(phydev);
502
503 return err;
504}
505
79bef5fb 506static struct phy_driver *generic_for_phy(struct phy_device *phydev)
5f184715
AF
507{
508#ifdef CONFIG_PHYLIB_10G
79bef5fb 509 if (phydev->is_c45)
20bd8e4f 510 return ll_entry_get(struct phy_driver, gen10g, phy_driver);
5f184715
AF
511#endif
512
f7053296 513 return ll_entry_get(struct phy_driver, genphy, phy_driver);
5f184715
AF
514}
515
e24b58f5 516static struct phy_driver *get_phy_driver(struct phy_device *phydev)
5f184715 517{
7940a93e 518 const int ll_n_ents = ll_entry_count(struct phy_driver, phy_driver);
5f184715 519 int phy_id = phydev->phy_id;
8728d4c0
MV
520 struct phy_driver *ll_entry;
521 struct phy_driver *drv;
5f184715 522
7940a93e
MV
523 ll_entry = ll_entry_start(struct phy_driver, phy_driver);
524 for (drv = ll_entry; drv != ll_entry + ll_n_ents; drv++)
525 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
526 return drv;
527
5f184715 528 /* If we made it here, there's no driver for this PHY */
79bef5fb 529 return generic_for_phy(phydev);
5f184715
AF
530}
531
3249116d 532struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
e24b58f5 533 u32 phy_id, bool is_c45)
5f184715
AF
534{
535 struct phy_device *dev;
536
8d631203
MS
537 /*
538 * We allocate the device, and initialize the
539 * default values
540 */
5f184715
AF
541 dev = malloc(sizeof(*dev));
542 if (!dev) {
543 printf("Failed to allocate PHY device for %s:%d\n",
15c49df8 544 bus ? bus->name : "(null bus)", addr);
5f184715
AF
545 return NULL;
546 }
547
548 memset(dev, 0, sizeof(*dev));
549
550 dev->duplex = -1;
26d3acda 551 dev->link = 0;
e24b58f5 552 dev->interface = PHY_INTERFACE_MODE_NA;
5f184715 553
eef0b8a9 554 dev->node = ofnode_null();
eef0b8a9 555
5f184715
AF
556 dev->autoneg = AUTONEG_ENABLE;
557
558 dev->addr = addr;
559 dev->phy_id = phy_id;
b3eabd82 560 dev->is_c45 = is_c45;
5f184715
AF
561 dev->bus = bus;
562
e24b58f5 563 dev->drv = get_phy_driver(dev);
5f184715 564
05eb6a69
SDPP
565 if (phy_probe(dev)) {
566 printf("%s, PHY probe failed\n", __func__);
567 return NULL;
568 }
5f184715 569
f146c446
JC
570 if (addr >= 0 && addr < PHY_MAX_ADDR && phy_id != PHY_FIXED_ID &&
571 phy_id != PHY_NCSI_ID)
7b4ea2d8 572 bus->phymap[addr] = dev;
5f184715
AF
573
574 return dev;
575}
576
577/**
578 * get_phy_id - reads the specified addr for its ID.
579 * @bus: the target MII bus
580 * @addr: PHY address on the MII bus
581 * @phy_id: where to store the ID retrieved.
582 *
583 * Description: Reads the ID registers of the PHY at @addr on the
584 * @bus, stores it in @phy_id and returns zero on success.
585 */
5707d5ff 586int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
5f184715
AF
587{
588 int phy_reg;
589
8d631203
MS
590 /*
591 * Grab the bits from PHYIR1, and put them
592 * in the upper half
593 */
5f184715
AF
594 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
595
596 if (phy_reg < 0)
597 return -EIO;
598
599 *phy_id = (phy_reg & 0xffff) << 16;
600
601 /* Grab the bits from PHYIR2, and put them in the lower half */
602 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
603
604 if (phy_reg < 0)
605 return -EIO;
606
607 *phy_id |= (phy_reg & 0xffff);
608
609 return 0;
610}
611
1adb406b 612static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
e24b58f5 613 uint phy_mask, int devad)
1adb406b
TK
614{
615 u32 phy_id = 0xffffffff;
b3eabd82 616 bool is_c45;
8d631203 617
1adb406b
TK
618 while (phy_mask) {
619 int addr = ffs(phy_mask) - 1;
620 int r = get_phy_id(bus, addr, devad, &phy_id);
3bf135b6
AM
621
622 /*
623 * If the PHY ID is flat 0 we ignore it. There are C45 PHYs
624 * that return all 0s for C22 reads (like Aquantia AQR112) and
625 * there are C22 PHYs that return all 0s for C45 reads (like
626 * Atheros AR8035).
627 */
628 if (r == 0 && phy_id == 0)
629 goto next;
630
1adb406b 631 /* If the PHY ID is mostly f's, we didn't find anything */
b3eabd82
PB
632 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff) {
633 is_c45 = (devad == MDIO_DEVAD_NONE) ? false : true;
e24b58f5 634 return phy_device_create(bus, addr, phy_id, is_c45);
b3eabd82 635 }
3bf135b6 636next:
1adb406b
TK
637 phy_mask &= ~(1 << addr);
638 }
639 return NULL;
640}
641
642static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
e24b58f5 643 uint phy_mask)
1adb406b
TK
644{
645 /* If we have one, return the existing device, with new interface */
646 while (phy_mask) {
f5dbfc82 647 unsigned int addr = ffs(phy_mask) - 1;
8d631203 648
e24b58f5 649 if (bus->phymap[addr])
1adb406b 650 return bus->phymap[addr];
e24b58f5 651
f5dbfc82 652 phy_mask &= ~(1U << addr);
1adb406b
TK
653 }
654 return NULL;
655}
656
657static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
e24b58f5 658 uint phy_mask)
1adb406b 659{
1adb406b 660 struct phy_device *phydev;
9c6de508
FC
661 int devad[] = {
662 /* Clause-22 */
663 MDIO_DEVAD_NONE,
664 /* Clause-45 */
665 MDIO_MMD_PMAPMD,
666 MDIO_MMD_WIS,
667 MDIO_MMD_PCS,
668 MDIO_MMD_PHYXS,
669 MDIO_MMD_VEND1,
670 };
671 int i, devad_cnt;
672
673 devad_cnt = sizeof(devad)/sizeof(int);
e24b58f5 674 phydev = search_for_existing_phy(bus, phy_mask);
1adb406b
TK
675 if (phydev)
676 return phydev;
9c6de508
FC
677 /* try different access clauses */
678 for (i = 0; i < devad_cnt; i++) {
e24b58f5 679 phydev = create_phy_by_mask(bus, phy_mask, devad[i]);
1adb406b
TK
680 if (IS_ERR(phydev))
681 return NULL;
682 if (phydev)
683 return phydev;
684 }
3e1949d7
BM
685
686 debug("\n%s PHY: ", bus->name);
687 while (phy_mask) {
688 int addr = ffs(phy_mask) - 1;
8d631203 689
3e1949d7
BM
690 debug("%d ", addr);
691 phy_mask &= ~(1 << addr);
692 }
693 debug("not found\n");
0132b9ab
BM
694
695 return NULL;
1adb406b
TK
696}
697
5f184715 698/**
8d631203
MS
699 * get_phy_device - reads the specified PHY device and returns its
700 * @phy_device struct
5f184715
AF
701 * @bus: the target MII bus
702 * @addr: PHY address on the MII bus
703 *
704 * Description: Reads the ID registers of the PHY at @addr on the
705 * @bus, then allocates and returns the phy_device to represent it.
706 */
e24b58f5 707static struct phy_device *get_phy_device(struct mii_dev *bus, int addr)
5f184715 708{
e24b58f5 709 return get_phy_device_by_mask(bus, 1 << addr);
5f184715
AF
710}
711
712int phy_reset(struct phy_device *phydev)
713{
714 int reg;
715 int timeout = 500;
716 int devad = MDIO_DEVAD_NONE;
717
ddcd1f30
SX
718 if (phydev->flags & PHY_FLAG_BROKEN_RESET)
719 return 0;
720
5f184715
AF
721#ifdef CONFIG_PHYLIB_10G
722 /* If it's 10G, we need to issue reset through one of the MMDs */
79bef5fb 723 if (phydev->is_c45) {
5f184715
AF
724 if (!phydev->mmds)
725 gen10g_discover_mmds(phydev);
726
727 devad = ffs(phydev->mmds) - 1;
728 }
729#endif
730
a058052c 731 if (phy_write(phydev, devad, MII_BMCR, BMCR_RESET) < 0) {
5f184715
AF
732 debug("PHY reset failed\n");
733 return -1;
734 }
735
16199a8b 736#if CONFIG_PHY_RESET_DELAY > 0
5f184715
AF
737 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
738#endif
739 /*
740 * Poll the control register for the reset bit to go to 0 (it is
741 * auto-clearing). This should happen within 0.5 seconds per the
742 * IEEE spec.
743 */
a058052c 744 reg = phy_read(phydev, devad, MII_BMCR);
5f184715
AF
745 while ((reg & BMCR_RESET) && timeout--) {
746 reg = phy_read(phydev, devad, MII_BMCR);
747
748 if (reg < 0) {
749 debug("PHY status read failed\n");
750 return -1;
751 }
752 udelay(1000);
753 }
754
755 if (reg & BMCR_RESET) {
756 puts("PHY reset timed out\n");
757 return -1;
758 }
759
760 return 0;
761}
762
763int miiphy_reset(const char *devname, unsigned char addr)
764{
765 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
766 struct phy_device *phydev;
767
e24b58f5 768 phydev = get_phy_device(bus, addr);
5f184715
AF
769
770 return phy_reset(phydev);
771}
772
e99a6efa
MV
773#if CONFIG_IS_ENABLED(DM_GPIO) && CONFIG_IS_ENABLED(OF_REAL) && \
774 !IS_ENABLED(CONFIG_DM_ETH_PHY)
775int phy_gpio_reset(struct udevice *dev)
776{
777 struct ofnode_phandle_args phandle_args;
778 struct gpio_desc gpio;
779 u32 assert, deassert;
780 ofnode node;
781 int ret;
782
783 ret = dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
784 &phandle_args);
785 /* No PHY handle is OK */
786 if (ret)
787 return 0;
788
789 node = phandle_args.node;
790 if (!ofnode_valid(node))
791 return -EINVAL;
792
793 ret = gpio_request_by_name_nodev(node, "reset-gpios", 0, &gpio,
794 GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
795 /* No PHY reset GPIO is OK */
796 if (ret)
797 return 0;
798
799 assert = ofnode_read_u32_default(node, "reset-assert-us", 20000);
800 deassert = ofnode_read_u32_default(node, "reset-deassert-us", 1000);
801 ret = dm_gpio_set_value(&gpio, 1);
802 if (ret) {
803 dev_err(dev, "Failed assert gpio, err: %d\n", ret);
804 return ret;
805 }
806
807 udelay(assert);
808
809 ret = dm_gpio_set_value(&gpio, 0);
810 if (ret) {
811 dev_err(dev, "Failed deassert gpio, err: %d\n", ret);
812 return ret;
813 }
814
815 udelay(deassert);
816
817 return 0;
818}
819#else
820int phy_gpio_reset(struct udevice *dev)
821{
822 return 0;
823}
824#endif
825
e24b58f5 826struct phy_device *phy_find_by_mask(struct mii_dev *bus, uint phy_mask)
5f184715 827{
5f184715 828 /* Reset the bus */
59370f3f 829 if (bus->reset) {
e3a77218 830 bus->reset(bus);
5f184715 831
59370f3f 832 /* Wait 15ms to make sure the PHY has come out of hard reset */
8d631203 833 mdelay(15);
59370f3f
JK
834 }
835
e24b58f5 836 return get_phy_device_by_mask(bus, phy_mask);
1adb406b 837}
5f184715 838
495fc04b
MV
839static void phy_connect_dev(struct phy_device *phydev, struct udevice *dev,
840 phy_interface_t interface)
1adb406b 841{
5f184715
AF
842 /* Soft Reset the PHY */
843 phy_reset(phydev);
17ecfa9b 844 if (phydev->dev && phydev->dev != dev) {
5f184715 845 printf("%s:%d is connected to %s. Reconnecting to %s\n",
8d631203
MS
846 phydev->bus->name, phydev->addr,
847 phydev->dev->name, dev->name);
1adb406b 848 }
5f184715 849 phydev->dev = dev;
e24b58f5
MB
850 phydev->interface = interface;
851 debug("%s connected to %s mode %s\n", dev->name, phydev->drv->name,
852 phy_string_for_interface(interface));
1adb406b
TK
853}
854
f41e588c 855#ifdef CONFIG_PHY_XILINX_GMII2RGMII
f41e588c 856static struct phy_device *phy_connect_gmii2rgmii(struct mii_dev *bus,
e24b58f5 857 struct udevice *dev)
f41e588c
SDPP
858{
859 struct phy_device *phydev = NULL;
0a9f0e0d 860 ofnode node;
6c993815 861
0a9f0e0d 862 ofnode_for_each_subnode(node, dev_ofnode(dev)) {
6c993815
BM
863 node = ofnode_by_compatible(node, "xlnx,gmii-to-rgmii-1.0");
864 if (ofnode_valid(node)) {
e31d707d
TB
865 int gmiirgmii_phyaddr;
866
867 gmiirgmii_phyaddr = ofnode_read_u32_default(node, "reg", 0);
868 phydev = phy_device_create(bus, gmiirgmii_phyaddr,
e24b58f5 869 PHY_GMII2RGMII_ID, false);
6c993815
BM
870 if (phydev)
871 phydev->node = node;
f41e588c
SDPP
872 break;
873 }
6c993815
BM
874
875 node = ofnode_first_subnode(node);
f41e588c
SDPP
876 }
877
878 return phydev;
879}
880#endif
881
c256d3f7 882#ifdef CONFIG_PHY_FIXED
d0781c95
VO
883/**
884 * fixed_phy_create() - create an unconnected fixed-link pseudo-PHY device
885 * @node: OF node for the container of the fixed-link node
886 *
887 * Description: Creates a struct phy_device based on a fixed-link of_node
888 * description. Can be used without phy_connect by drivers which do not expose
889 * a UCLASS_ETH udevice.
890 */
891struct phy_device *fixed_phy_create(ofnode node)
892{
f27bc8af 893 struct phy_device *phydev;
d0781c95
VO
894 ofnode subnode;
895
d0781c95
VO
896 subnode = ofnode_find_subnode(node, "fixed-link");
897 if (!ofnode_valid(subnode)) {
898 return NULL;
899 }
900
e24b58f5 901 phydev = phy_device_create(NULL, 0, PHY_FIXED_ID, false);
ebb8ff61 902 if (phydev) {
f27bc8af 903 phydev->node = subnode;
ebb8ff61
HS
904 phydev->interface = ofnode_read_phy_mode(node);
905 }
e24b58f5 906
f27bc8af 907 return phydev;
d0781c95
VO
908}
909
c256d3f7 910static struct phy_device *phy_connect_fixed(struct mii_dev *bus,
e24b58f5 911 struct udevice *dev)
1adb406b 912{
f27bc8af 913 ofnode node = dev_ofnode(dev), subnode;
676fbd3d 914 struct phy_device *phydev = NULL;
f27bc8af 915
676fbd3d 916 if (ofnode_phy_is_fixed_link(node, &subnode)) {
e24b58f5 917 phydev = phy_device_create(bus, 0, PHY_FIXED_ID, false);
676fbd3d
BM
918 if (phydev)
919 phydev->node = subnode;
920 }
c256d3f7
SDPP
921
922 return phydev;
923}
db40c1aa 924#endif
c256d3f7 925
c256d3f7
SDPP
926struct phy_device *phy_connect(struct mii_dev *bus, int addr,
927 struct udevice *dev,
928 phy_interface_t interface)
c256d3f7
SDPP
929{
930 struct phy_device *phydev = NULL;
1f607896 931 uint mask = (addr >= 0) ? (1 << addr) : 0xffffffff;
c256d3f7
SDPP
932
933#ifdef CONFIG_PHY_FIXED
e24b58f5 934 phydev = phy_connect_fixed(bus, dev);
c256d3f7 935#endif
e2ffeaa1
SMJ
936
937#ifdef CONFIG_PHY_NCSI
09bd3d0b 938 if (!phydev && interface == PHY_INTERFACE_MODE_NCSI)
e24b58f5 939 phydev = phy_device_create(bus, 0, PHY_NCSI_ID, false);
e2ffeaa1
SMJ
940#endif
941
a744a284
MS
942#ifdef CONFIG_PHY_ETHERNET_ID
943 if (!phydev)
7f418ea5 944 phydev = phy_connect_phy_id(bus, dev, addr);
a744a284
MS
945#endif
946
f41e588c
SDPP
947#ifdef CONFIG_PHY_XILINX_GMII2RGMII
948 if (!phydev)
e24b58f5 949 phydev = phy_connect_gmii2rgmii(bus, dev);
f41e588c 950#endif
c256d3f7 951
8d631203 952 if (!phydev)
e24b58f5 953 phydev = phy_find_by_mask(bus, mask);
5f184715 954
1adb406b 955 if (phydev)
e24b58f5 956 phy_connect_dev(phydev, dev, interface);
1adb406b
TK
957 else
958 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
5f184715
AF
959 return phydev;
960}
961
6e5b9ac0
TT
962/*
963 * Start the PHY. Returns 0 on success, or a negative error code.
964 */
5f184715
AF
965int phy_startup(struct phy_device *phydev)
966{
967 if (phydev->drv->startup)
6e5b9ac0 968 return phydev->drv->startup(phydev);
5f184715
AF
969
970 return 0;
971}
972
3c6928fd 973__weak int board_phy_config(struct phy_device *phydev)
5f184715 974{
9fafe7da
TK
975 if (phydev->drv->config)
976 return phydev->drv->config(phydev);
5f184715
AF
977 return 0;
978}
979
5f184715
AF
980int phy_config(struct phy_device *phydev)
981{
5f184715 982 /* Invoke an optional board-specific helper */
7a673f0b 983 return board_phy_config(phydev);
5f184715
AF
984}
985
986int phy_shutdown(struct phy_device *phydev)
987{
988 if (phydev->drv->shutdown)
989 phydev->drv->shutdown(phydev);
990
991 return 0;
992}
c74c8e66 993
087baf80
AA
994/**
995 * phy_modify - Convenience function for modifying a given PHY register
996 * @phydev: the phy_device struct
997 * @devad: The MMD to read from
998 * @regnum: register number to write
999 * @mask: bit mask of bits to clear
1000 * @set: new value of bits set in mask to write to @regnum
1001 */
1002int phy_modify(struct phy_device *phydev, int devad, int regnum, u16 mask,
1003 u16 set)
1004{
1005 int ret;
1006
1007 ret = phy_read(phydev, devad, regnum);
1008 if (ret < 0)
1009 return ret;
1010
1011 return phy_write(phydev, devad, regnum, (ret & ~mask) | set);
1012}
65f2266e
RF
1013
1014/**
1015 * phy_read - Convenience function for reading a given PHY register
1016 * @phydev: the phy_device struct
1017 * @devad: The MMD to read from
1018 * @regnum: register number to read
1019 * @return: value for success or negative errno for failure
1020 */
1021int phy_read(struct phy_device *phydev, int devad, int regnum)
1022{
1023 struct mii_dev *bus = phydev->bus;
1024
1025 if (!bus || !bus->read) {
1026 debug("%s: No bus configured\n", __func__);
1027 return -1;
1028 }
1029
1030 return bus->read(bus, phydev->addr, devad, regnum);
1031}
1032
1033/**
1034 * phy_write - Convenience function for writing a given PHY register
1035 * @phydev: the phy_device struct
1036 * @devad: The MMD to read from
1037 * @regnum: register number to write
1038 * @val: value to write to @regnum
1039 * @return: 0 for success or negative errno for failure
1040 */
1041int phy_write(struct phy_device *phydev, int devad, int regnum, u16 val)
1042{
1043 struct mii_dev *bus = phydev->bus;
1044
1045 if (!bus || !bus->write) {
1046 debug("%s: No bus configured\n", __func__);
1047 return -1;
1048 }
1049
1050 return bus->write(bus, phydev->addr, devad, regnum, val);
1051}
1052
1053/**
1054 * phy_mmd_start_indirect - Convenience function for writing MMD registers
1055 * @phydev: the phy_device struct
1056 * @devad: The MMD to read from
1057 * @regnum: register number to write
1058 * @return: None
1059 */
1060void phy_mmd_start_indirect(struct phy_device *phydev, int devad, int regnum)
1061{
1062 /* Write the desired MMD Devad */
1063 phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_CTRL, devad);
1064
1065 /* Write the desired MMD register address */
1066 phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA, regnum);
1067
1068 /* Select the Function : DATA with no post increment */
1069 phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_CTRL,
1070 (devad | MII_MMD_CTRL_NOINCR));
1071}
1072
1073/**
1074 * phy_read_mmd - Convenience function for reading a register
1075 * from an MMD on a given PHY.
1076 * @phydev: The phy_device struct
1077 * @devad: The MMD to read from
1078 * @regnum: The register on the MMD to read
1079 * @return: Value for success or negative errno for failure
1080 */
1081int phy_read_mmd(struct phy_device *phydev, int devad, int regnum)
1082{
1083 struct phy_driver *drv = phydev->drv;
1084
1085 if (regnum > (u16)~0 || devad > 32)
1086 return -EINVAL;
1087
1088 /* driver-specific access */
1089 if (drv->read_mmd)
1090 return drv->read_mmd(phydev, devad, regnum);
1091
1092 /* direct C45 / C22 access */
1093 if ((drv->features & PHY_10G_FEATURES) == PHY_10G_FEATURES ||
1094 devad == MDIO_DEVAD_NONE || !devad)
1095 return phy_read(phydev, devad, regnum);
1096
1097 /* indirect C22 access */
1098 phy_mmd_start_indirect(phydev, devad, regnum);
1099
1100 /* Read the content of the MMD's selected register */
1101 return phy_read(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA);
1102}
1103
1104/**
1105 * phy_write_mmd - Convenience function for writing a register
1106 * on an MMD on a given PHY.
1107 * @phydev: The phy_device struct
1108 * @devad: The MMD to read from
1109 * @regnum: The register on the MMD to read
1110 * @val: value to write to @regnum
1111 * @return: 0 for success or negative errno for failure
1112 */
1113int phy_write_mmd(struct phy_device *phydev, int devad, int regnum, u16 val)
1114{
1115 struct phy_driver *drv = phydev->drv;
1116
1117 if (regnum > (u16)~0 || devad > 32)
1118 return -EINVAL;
1119
1120 /* driver-specific access */
1121 if (drv->write_mmd)
1122 return drv->write_mmd(phydev, devad, regnum, val);
1123
1124 /* direct C45 / C22 access */
1125 if ((drv->features & PHY_10G_FEATURES) == PHY_10G_FEATURES ||
1126 devad == MDIO_DEVAD_NONE || !devad)
1127 return phy_write(phydev, devad, regnum, val);
1128
1129 /* indirect C22 access */
1130 phy_mmd_start_indirect(phydev, devad, regnum);
1131
1132 /* Write the data into MMD's selected register */
1133 return phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA, val);
1134}
1135
1136/**
1137 * phy_set_bits_mmd - Convenience function for setting bits in a register
1138 * on MMD
1139 * @phydev: the phy_device struct
1140 * @devad: the MMD containing register to modify
1141 * @regnum: register number to modify
1142 * @val: bits to set
1143 * @return: 0 for success or negative errno for failure
1144 */
1145int phy_set_bits_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
1146{
1147 int value, ret;
1148
1149 value = phy_read_mmd(phydev, devad, regnum);
1150 if (value < 0)
1151 return value;
1152
1153 value |= val;
1154
1155 ret = phy_write_mmd(phydev, devad, regnum, value);
1156 if (ret < 0)
1157 return ret;
1158
1159 return 0;
1160}
1161
1162/**
1163 * phy_clear_bits_mmd - Convenience function for clearing bits in a register
1164 * on MMD
1165 * @phydev: the phy_device struct
1166 * @devad: the MMD containing register to modify
1167 * @regnum: register number to modify
1168 * @val: bits to clear
1169 * @return: 0 for success or negative errno for failure
1170 */
1171int phy_clear_bits_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
1172{
1173 int value, ret;
1174
1175 value = phy_read_mmd(phydev, devad, regnum);
1176 if (value < 0)
1177 return value;
1178
1179 value &= ~val;
1180
1181 ret = phy_write_mmd(phydev, devad, regnum, value);
1182 if (ret < 0)
1183 return ret;
1184
1185 return 0;
1186}
09bd3d0b 1187
87b75028
MV
1188/**
1189 * phy_modify_mmd_changed - Function for modifying a register on MMD
1190 * @phydev: the phy_device struct
1191 * @devad: the MMD containing register to modify
1192 * @regnum: register number to modify
1193 * @mask: bit mask of bits to clear
1194 * @set: new value of bits set in mask to write to @regnum
1195 *
1196 * NOTE: MUST NOT be called from interrupt context,
1197 * because the bus read/write functions may wait for an interrupt
1198 * to conclude the operation.
1199 *
1200 * Returns negative errno, 0 if there was no change, and 1 in case of change
1201 */
1202int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
1203 u16 mask, u16 set)
1204{
1205 int new, ret;
1206
1207 ret = phy_read_mmd(phydev, devad, regnum);
1208 if (ret < 0)
1209 return ret;
1210
1211 new = (ret & ~mask) | set;
1212 if (new == ret)
1213 return 0;
1214
1215 ret = phy_write_mmd(phydev, devad, regnum, new);
1216
1217 return ret < 0 ? ret : 1;
1218}
1219
1220/**
1221 * phy_modify_mmd - Convenience function for modifying a register on MMD
1222 * @phydev: the phy_device struct
1223 * @devad: the MMD containing register to modify
1224 * @regnum: register number to modify
1225 * @mask: bit mask of bits to clear
1226 * @set: new value of bits set in mask to write to @regnum
1227 *
1228 * NOTE: MUST NOT be called from interrupt context,
1229 * because the bus read/write functions may wait for an interrupt
1230 * to conclude the operation.
1231 */
1232int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
1233 u16 mask, u16 set)
1234{
1235 int ret;
1236
1237 ret = phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
1238
1239 return ret < 0 ? ret : 0;
1240}
1241
09bd3d0b
SMJ
1242bool phy_interface_is_ncsi(void)
1243{
75d28899 1244#ifdef CONFIG_PHY_NCSI
09bd3d0b
SMJ
1245 struct eth_pdata *pdata = dev_get_plat(eth_get_dev());
1246
1247 return pdata->phy_interface == PHY_INTERFACE_MODE_NCSI;
75d28899
MV
1248#else
1249 return 0;
1250#endif
09bd3d0b 1251}
This page took 0.904826 seconds and 5 git commands to generate.