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