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