]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/net/phy/phy.c
phylib: remove a couple of redundant code lines
[people/ms/u-boot.git] / drivers / net / phy / phy.c
CommitLineData
5f184715
AF
1/*
2 * Generic PHY Management code
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17 * MA 02111-1307 USA
18 *
19 *
20 * Copyright 2011 Freescale Semiconductor, Inc.
21 * author Andy Fleming
22 *
23 * Based loosely off of Linux's PHY Lib
24 */
25
26#include <config.h>
27#include <common.h>
28#include <malloc.h>
29#include <net.h>
30#include <command.h>
31#include <miiphy.h>
32#include <phy.h>
33#include <errno.h>
34
35/* Generic PHY support and helper functions */
36
37/**
38 * genphy_config_advert - sanitize and advertise auto-negotation parameters
39 * @phydev: target phy_device struct
40 *
41 * Description: Writes MII_ADVERTISE with the appropriate values,
42 * after sanitizing the values to make sure we only advertise
43 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
44 * hasn't changed, and > 0 if it has changed.
45 */
46int genphy_config_advert(struct phy_device *phydev)
47{
48 u32 advertise;
49 int oldadv, adv;
50 int err, changed = 0;
51
52 /* Only allow advertising what
53 * this PHY supports */
54 phydev->advertising &= phydev->supported;
55 advertise = phydev->advertising;
56
57 /* Setup standard advertisement */
58 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
59
60 if (adv < 0)
61 return adv;
62
63 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
64 ADVERTISE_PAUSE_ASYM);
65 if (advertise & ADVERTISED_10baseT_Half)
66 adv |= ADVERTISE_10HALF;
67 if (advertise & ADVERTISED_10baseT_Full)
68 adv |= ADVERTISE_10FULL;
69 if (advertise & ADVERTISED_100baseT_Half)
70 adv |= ADVERTISE_100HALF;
71 if (advertise & ADVERTISED_100baseT_Full)
72 adv |= ADVERTISE_100FULL;
73 if (advertise & ADVERTISED_Pause)
74 adv |= ADVERTISE_PAUSE_CAP;
75 if (advertise & ADVERTISED_Asym_Pause)
76 adv |= ADVERTISE_PAUSE_ASYM;
77
78 if (adv != oldadv) {
79 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
80
81 if (err < 0)
82 return err;
83 changed = 1;
84 }
85
86 /* Configure gigabit if it's supported */
87 if (phydev->supported & (SUPPORTED_1000baseT_Half |
88 SUPPORTED_1000baseT_Full)) {
89 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
90
91 if (adv < 0)
92 return adv;
93
94 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
95 if (advertise & SUPPORTED_1000baseT_Half)
96 adv |= ADVERTISE_1000HALF;
97 if (advertise & SUPPORTED_1000baseT_Full)
98 adv |= ADVERTISE_1000FULL;
99
100 if (adv != oldadv) {
101 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000,
102 adv);
103
104 if (err < 0)
105 return err;
106 changed = 1;
107 }
108 }
109
110 return changed;
111}
112
113
114/**
115 * genphy_setup_forced - configures/forces speed/duplex from @phydev
116 * @phydev: target phy_device struct
117 *
118 * Description: Configures MII_BMCR to force speed/duplex
119 * to the values in phydev. Assumes that the values are valid.
120 */
121int genphy_setup_forced(struct phy_device *phydev)
122{
123 int err;
124 int ctl = 0;
125
126 phydev->pause = phydev->asym_pause = 0;
127
128 if (SPEED_1000 == phydev->speed)
129 ctl |= BMCR_SPEED1000;
130 else if (SPEED_100 == phydev->speed)
131 ctl |= BMCR_SPEED100;
132
133 if (DUPLEX_FULL == phydev->duplex)
134 ctl |= BMCR_FULLDPLX;
135
136 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
137
138 return err;
139}
140
141
142/**
143 * genphy_restart_aneg - Enable and Restart Autonegotiation
144 * @phydev: target phy_device struct
145 */
146int genphy_restart_aneg(struct phy_device *phydev)
147{
148 int ctl;
149
150 ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
151
152 if (ctl < 0)
153 return ctl;
154
155 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
156
157 /* Don't isolate the PHY if we're negotiating */
158 ctl &= ~(BMCR_ISOLATE);
159
160 ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
161
162 return ctl;
163}
164
165
166/**
167 * genphy_config_aneg - restart auto-negotiation or write BMCR
168 * @phydev: target phy_device struct
169 *
170 * Description: If auto-negotiation is enabled, we configure the
171 * advertising, and then restart auto-negotiation. If it is not
172 * enabled, then we write the BMCR.
173 */
174int genphy_config_aneg(struct phy_device *phydev)
175{
176 int result;
177
178 if (AUTONEG_ENABLE != phydev->autoneg)
179 return genphy_setup_forced(phydev);
180
181 result = genphy_config_advert(phydev);
182
183 if (result < 0) /* error */
184 return result;
185
186 if (result == 0) {
187 /* Advertisment hasn't changed, but maybe aneg was never on to
188 * begin with? Or maybe phy was isolated? */
189 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
190
191 if (ctl < 0)
192 return ctl;
193
194 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
195 result = 1; /* do restart aneg */
196 }
197
198 /* Only restart aneg if we are advertising something different
199 * than we were before. */
200 if (result > 0)
201 result = genphy_restart_aneg(phydev);
202
203 return result;
204}
205
206/**
207 * genphy_update_link - update link status in @phydev
208 * @phydev: target phy_device struct
209 *
210 * Description: Update the value in phydev->link to reflect the
211 * current link value. In order to do this, we need to read
212 * the status register twice, keeping the second value.
213 */
214int genphy_update_link(struct phy_device *phydev)
215{
216 unsigned int mii_reg;
217
218 /*
219 * Wait if the link is up, and autonegotiation is in progress
220 * (ie - we're capable and it's not done)
221 */
222 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
223
224 /*
225 * If we already saw the link up, and it hasn't gone down, then
226 * we don't need to wait for autoneg again
227 */
228 if (phydev->link && mii_reg & BMSR_LSTATUS)
229 return 0;
230
231 if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
232 int i = 0;
233
234 printf("%s Waiting for PHY auto negotiation to complete",
235 phydev->dev->name);
236 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
237 /*
238 * Timeout reached ?
239 */
240 if (i > PHY_ANEG_TIMEOUT) {
241 printf(" TIMEOUT !\n");
242 phydev->link = 0;
243 return 0;
244 }
245
246 if (ctrlc()) {
247 puts("user interrupt!\n");
248 phydev->link = 0;
249 return -EINTR;
250 }
251
252 if ((i++ % 500) == 0)
253 printf(".");
254
255 udelay(1000); /* 1 ms */
256 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
257 }
258 printf(" done\n");
259 phydev->link = 1;
260 } else {
261 /* Read the link a second time to clear the latched state */
262 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
263
264 if (mii_reg & BMSR_LSTATUS)
265 phydev->link = 1;
266 else
267 phydev->link = 0;
268 }
269
270 return 0;
271}
272
273/*
274 * Generic function which updates the speed and duplex. If
275 * autonegotiation is enabled, it uses the AND of the link
276 * partner's advertised capabilities and our advertised
277 * capabilities. If autonegotiation is disabled, we use the
278 * appropriate bits in the control register.
279 *
280 * Stolen from Linux's mii.c and phy_device.c
281 */
282static int genphy_parse_link(struct phy_device *phydev)
283{
284 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
285
286 /* We're using autonegotiation */
287 if (mii_reg & BMSR_ANEGCAPABLE) {
288 u32 lpa = 0;
289 u32 gblpa = 0;
290
291 /* Check for gigabit capability */
292 if (mii_reg & BMSR_ERCAP) {
293 /* We want a list of states supported by
294 * both PHYs in the link
295 */
296 gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
297 gblpa &= phy_read(phydev,
298 MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
299 }
300
301 /* Set the baseline so we only have to set them
302 * if they're different
303 */
304 phydev->speed = SPEED_10;
305 phydev->duplex = DUPLEX_HALF;
306
307 /* Check the gigabit fields */
308 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
309 phydev->speed = SPEED_1000;
310
311 if (gblpa & PHY_1000BTSR_1000FD)
312 phydev->duplex = DUPLEX_FULL;
313
314 /* We're done! */
315 return 0;
316 }
317
318 lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
319 lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
320
041c5422 321 if (lpa & (LPA_100FULL | LPA_100HALF))
5f184715
AF
322 phydev->speed = SPEED_100;
323
041c5422 324 if (lpa & (LPA_100FULL | LPA_10FULL))
5f184715
AF
325 phydev->duplex = DUPLEX_FULL;
326 } else {
327 u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
328
329 phydev->speed = SPEED_10;
330 phydev->duplex = DUPLEX_HALF;
331
332 if (bmcr & BMCR_FULLDPLX)
333 phydev->duplex = DUPLEX_FULL;
334
335 if (bmcr & BMCR_SPEED1000)
336 phydev->speed = SPEED_1000;
337 else if (bmcr & BMCR_SPEED100)
338 phydev->speed = SPEED_100;
339 }
340
341 return 0;
342}
343
344int genphy_config(struct phy_device *phydev)
345{
346 int val;
347 u32 features;
348
349 /* For now, I'll claim that the generic driver supports
350 * all possible port types */
351 features = (SUPPORTED_TP | SUPPORTED_MII
352 | SUPPORTED_AUI | SUPPORTED_FIBRE |
353 SUPPORTED_BNC);
354
355 /* Do we support autonegotiation? */
356 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
357
358 if (val < 0)
359 return val;
360
361 if (val & BMSR_ANEGCAPABLE)
362 features |= SUPPORTED_Autoneg;
363
364 if (val & BMSR_100FULL)
365 features |= SUPPORTED_100baseT_Full;
366 if (val & BMSR_100HALF)
367 features |= SUPPORTED_100baseT_Half;
368 if (val & BMSR_10FULL)
369 features |= SUPPORTED_10baseT_Full;
370 if (val & BMSR_10HALF)
371 features |= SUPPORTED_10baseT_Half;
372
373 if (val & BMSR_ESTATEN) {
374 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
375
376 if (val < 0)
377 return val;
378
379 if (val & ESTATUS_1000_TFULL)
380 features |= SUPPORTED_1000baseT_Full;
381 if (val & ESTATUS_1000_THALF)
382 features |= SUPPORTED_1000baseT_Half;
383 }
384
385 phydev->supported = features;
386 phydev->advertising = features;
387
388 genphy_config_aneg(phydev);
389
390 return 0;
391}
392
393int genphy_startup(struct phy_device *phydev)
394{
395 genphy_update_link(phydev);
396 genphy_parse_link(phydev);
397
398 return 0;
399}
400
401int genphy_shutdown(struct phy_device *phydev)
402{
403 return 0;
404}
405
406static struct phy_driver genphy_driver = {
407 .uid = 0xffffffff,
408 .mask = 0xffffffff,
409 .name = "Generic PHY",
410 .features = 0,
411 .config = genphy_config,
412 .startup = genphy_startup,
413 .shutdown = genphy_shutdown,
414};
415
416static LIST_HEAD(phy_drivers);
417
418int phy_init(void)
419{
9082eeac
AF
420#ifdef CONFIG_PHY_ATHEROS
421 phy_atheros_init();
422#endif
423#ifdef CONFIG_PHY_BROADCOM
424 phy_broadcom_init();
425#endif
426#ifdef CONFIG_PHY_DAVICOM
427 phy_davicom_init();
428#endif
429#ifdef CONFIG_PHY_LXT
430 phy_lxt_init();
431#endif
432#ifdef CONFIG_PHY_MARVELL
433 phy_marvell_init();
434#endif
435#ifdef CONFIG_PHY_MICREL
436 phy_micrel_init();
437#endif
438#ifdef CONFIG_PHY_NATSEMI
439 phy_natsemi_init();
440#endif
441#ifdef CONFIG_PHY_REALTEK
442 phy_realtek_init();
443#endif
444#ifdef CONFIG_PHY_TERANETICS
445 phy_teranetics_init();
446#endif
447#ifdef CONFIG_PHY_VITESSE
448 phy_vitesse_init();
449#endif
450
5f184715
AF
451 return 0;
452}
453
454int phy_register(struct phy_driver *drv)
455{
456 INIT_LIST_HEAD(&drv->list);
457 list_add_tail(&drv->list, &phy_drivers);
458
459 return 0;
460}
461
462int phy_probe(struct phy_device *phydev)
463{
464 int err = 0;
465
466 phydev->advertising = phydev->supported = phydev->drv->features;
467 phydev->mmds = phydev->drv->mmds;
468
469 if (phydev->drv->probe)
470 err = phydev->drv->probe(phydev);
471
472 return err;
473}
474
475static struct phy_driver *generic_for_interface(phy_interface_t interface)
476{
477#ifdef CONFIG_PHYLIB_10G
478 if (is_10g_interface(interface))
479 return &gen10g_driver;
480#endif
481
482 return &genphy_driver;
483}
484
485struct phy_driver *get_phy_driver(struct phy_device *phydev,
486 phy_interface_t interface)
487{
488 struct list_head *entry;
489 int phy_id = phydev->phy_id;
490 struct phy_driver *drv = NULL;
491
492 list_for_each(entry, &phy_drivers) {
493 drv = list_entry(entry, struct phy_driver, list);
494 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
495 return drv;
496 }
497
498 /* If we made it here, there's no driver for this PHY */
499 return generic_for_interface(interface);
500}
501
502struct phy_device *phy_device_create(struct mii_dev *bus, int addr, int phy_id,
503 phy_interface_t interface)
504{
505 struct phy_device *dev;
506
507 /* We allocate the device, and initialize the
508 * default values */
509 dev = malloc(sizeof(*dev));
510 if (!dev) {
511 printf("Failed to allocate PHY device for %s:%d\n",
512 bus->name, addr);
513 return NULL;
514 }
515
516 memset(dev, 0, sizeof(*dev));
517
518 dev->duplex = -1;
519 dev->link = 1;
520 dev->interface = interface;
521
522 dev->autoneg = AUTONEG_ENABLE;
523
524 dev->addr = addr;
525 dev->phy_id = phy_id;
526 dev->bus = bus;
527
528 dev->drv = get_phy_driver(dev, interface);
529
530 phy_probe(dev);
531
532 bus->phymap[addr] = dev;
533
534 return dev;
535}
536
537/**
538 * get_phy_id - reads the specified addr for its ID.
539 * @bus: the target MII bus
540 * @addr: PHY address on the MII bus
541 * @phy_id: where to store the ID retrieved.
542 *
543 * Description: Reads the ID registers of the PHY at @addr on the
544 * @bus, stores it in @phy_id and returns zero on success.
545 */
546int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
547{
548 int phy_reg;
549
550 /* Grab the bits from PHYIR1, and put them
551 * in the upper half */
552 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
553
554 if (phy_reg < 0)
555 return -EIO;
556
557 *phy_id = (phy_reg & 0xffff) << 16;
558
559 /* Grab the bits from PHYIR2, and put them in the lower half */
560 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
561
562 if (phy_reg < 0)
563 return -EIO;
564
565 *phy_id |= (phy_reg & 0xffff);
566
567 return 0;
568}
569
570/**
571 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
572 * @bus: the target MII bus
573 * @addr: PHY address on the MII bus
574 *
575 * Description: Reads the ID registers of the PHY at @addr on the
576 * @bus, then allocates and returns the phy_device to represent it.
577 */
578struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
579 phy_interface_t interface)
580{
581 u32 phy_id = 0x1fffffff;
582 int i;
583 int r;
584
585 /* If we have one, return the existing device, with new interface */
586 if (bus->phymap[addr]) {
587 bus->phymap[addr]->interface = interface;
588
589 return bus->phymap[addr];
590 }
591
592 /* Try Standard (ie Clause 22) access */
593 r = get_phy_id(bus, addr, MDIO_DEVAD_NONE, &phy_id);
594 if (r)
595 return NULL;
596
597 /* If the PHY ID is mostly f's, we didn't find anything */
598 if ((phy_id & 0x1fffffff) != 0x1fffffff)
599 return phy_device_create(bus, addr, phy_id, interface);
600
601 /* Otherwise we have to try Clause 45 */
602 for (i = 1; i < 5; i++) {
603 r = get_phy_id(bus, addr, i, &phy_id);
604 if (r)
605 return NULL;
606
607 /* If the phy_id is mostly Fs, there is no device there */
608 if ((phy_id & 0x1fffffff) != 0x1fffffff)
609 break;
610 }
611
612 return phy_device_create(bus, addr, phy_id, interface);
613}
614
615int phy_reset(struct phy_device *phydev)
616{
617 int reg;
618 int timeout = 500;
619 int devad = MDIO_DEVAD_NONE;
620
621#ifdef CONFIG_PHYLIB_10G
622 /* If it's 10G, we need to issue reset through one of the MMDs */
623 if (is_10g_interface(phydev->interface)) {
624 if (!phydev->mmds)
625 gen10g_discover_mmds(phydev);
626
627 devad = ffs(phydev->mmds) - 1;
628 }
629#endif
630
631 reg = phy_read(phydev, devad, MII_BMCR);
632 if (reg < 0) {
633 debug("PHY status read failed\n");
634 return -1;
635 }
636
637 reg |= BMCR_RESET;
638
639 if (phy_write(phydev, devad, MII_BMCR, reg) < 0) {
640 debug("PHY reset failed\n");
641 return -1;
642 }
643
644#ifdef CONFIG_PHY_RESET_DELAY
645 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
646#endif
647 /*
648 * Poll the control register for the reset bit to go to 0 (it is
649 * auto-clearing). This should happen within 0.5 seconds per the
650 * IEEE spec.
651 */
652 while ((reg & BMCR_RESET) && timeout--) {
653 reg = phy_read(phydev, devad, MII_BMCR);
654
655 if (reg < 0) {
656 debug("PHY status read failed\n");
657 return -1;
658 }
659 udelay(1000);
660 }
661
662 if (reg & BMCR_RESET) {
663 puts("PHY reset timed out\n");
664 return -1;
665 }
666
667 return 0;
668}
669
670int miiphy_reset(const char *devname, unsigned char addr)
671{
672 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
673 struct phy_device *phydev;
674
675 /*
676 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
677 * If later code tries to connect with the right interface, this will
678 * be corrected by get_phy_device in phy_connect()
679 */
680 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
681
682 return phy_reset(phydev);
683}
684
685struct phy_device *phy_connect(struct mii_dev *bus, int addr,
686 struct eth_device *dev,
687 phy_interface_t interface)
688{
689 struct phy_device *phydev;
690
691 /* Reset the bus */
e3a77218
VZ
692 if (bus->reset)
693 bus->reset(bus);
5f184715
AF
694
695 /* Wait 15ms to make sure the PHY has come out of hard reset */
696 udelay(15000);
697
698 phydev = get_phy_device(bus, addr, interface);
699
700 if (!phydev) {
701 printf("Could not get PHY for %s:%d\n", bus->name, addr);
702
703 return NULL;
704 }
705
706 /* Soft Reset the PHY */
707 phy_reset(phydev);
708
709 if (phydev->dev)
710 printf("%s:%d is connected to %s. Reconnecting to %s\n",
711 bus->name, addr, phydev->dev->name, dev->name);
712
713 phydev->dev = dev;
714
b91a9d9d 715 debug("%s connected to %s\n", dev->name, phydev->drv->name);
5f184715
AF
716
717 return phydev;
718}
719
720int phy_startup(struct phy_device *phydev)
721{
722 if (phydev->drv->startup)
723 phydev->drv->startup(phydev);
724
725 return 0;
726}
727
728static int __board_phy_config(struct phy_device *phydev)
729{
730 return 0;
731}
732
733int board_phy_config(struct phy_device *phydev)
734 __attribute__((weak, alias("__board_phy_config")));
735
736int phy_config(struct phy_device *phydev)
737{
738 if (phydev->drv->config)
739 phydev->drv->config(phydev);
740
741 /* Invoke an optional board-specific helper */
742 board_phy_config(phydev);
743
744 return 0;
745}
746
747int phy_shutdown(struct phy_device *phydev)
748{
749 if (phydev->drv->shutdown)
750 phydev->drv->shutdown(phydev);
751
752 return 0;
753}