xref: /openbmc/u-boot/common/miiphyutil.c (revision 9ee16897)
1 /*
2  * (C) Copyright 2001
3  * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 /*
9  * This provides a bit-banged interface to the ethernet MII management
10  * channel.
11  */
12 
13 #include <common.h>
14 #include <dm.h>
15 #include <miiphy.h>
16 #include <phy.h>
17 
18 #include <asm/types.h>
19 #include <linux/list.h>
20 #include <malloc.h>
21 #include <net.h>
22 
23 /* local debug macro */
24 #undef MII_DEBUG
25 
26 #undef debug
27 #ifdef MII_DEBUG
28 #define debug(fmt, args...)	printf(fmt, ##args)
29 #else
30 #define debug(fmt, args...)
31 #endif /* MII_DEBUG */
32 
33 static struct list_head mii_devs;
34 static struct mii_dev *current_mii;
35 
36 /*
37  * Lookup the mii_dev struct by the registered device name.
38  */
39 struct mii_dev *miiphy_get_dev_by_name(const char *devname)
40 {
41 	struct list_head *entry;
42 	struct mii_dev *dev;
43 
44 	if (!devname) {
45 		printf("NULL device name!\n");
46 		return NULL;
47 	}
48 
49 	list_for_each(entry, &mii_devs) {
50 		dev = list_entry(entry, struct mii_dev, link);
51 		if (strcmp(dev->name, devname) == 0)
52 			return dev;
53 	}
54 
55 	return NULL;
56 }
57 
58 /*****************************************************************************
59  *
60  * Initialize global data. Need to be called before any other miiphy routine.
61  */
62 void miiphy_init(void)
63 {
64 	INIT_LIST_HEAD(&mii_devs);
65 	current_mii = NULL;
66 }
67 
68 static int legacy_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
69 {
70 	unsigned short val;
71 	int ret;
72 	struct legacy_mii_dev *ldev = bus->priv;
73 
74 	ret = ldev->read(bus->name, addr, reg, &val);
75 
76 	return ret ? -1 : (int)val;
77 }
78 
79 static int legacy_miiphy_write(struct mii_dev *bus, int addr, int devad,
80 				int reg, u16 val)
81 {
82 	struct legacy_mii_dev *ldev = bus->priv;
83 
84 	return ldev->write(bus->name, addr, reg, val);
85 }
86 
87 /*****************************************************************************
88  *
89  * Register read and write MII access routines for the device <name>.
90  * This API is now deprecated. Please use mdio_alloc and mdio_register, instead.
91  */
92 void miiphy_register(const char *name,
93 		      int (*read)(const char *devname, unsigned char addr,
94 				   unsigned char reg, unsigned short *value),
95 		      int (*write)(const char *devname, unsigned char addr,
96 				    unsigned char reg, unsigned short value))
97 {
98 	struct mii_dev *new_dev;
99 	struct legacy_mii_dev *ldev;
100 
101 	BUG_ON(strlen(name) >= MDIO_NAME_LEN);
102 
103 	/* check if we have unique name */
104 	new_dev = miiphy_get_dev_by_name(name);
105 	if (new_dev) {
106 		printf("miiphy_register: non unique device name '%s'\n", name);
107 		return;
108 	}
109 
110 	/* allocate memory */
111 	new_dev = mdio_alloc();
112 	ldev = malloc(sizeof(*ldev));
113 
114 	if (new_dev == NULL || ldev == NULL) {
115 		printf("miiphy_register: cannot allocate memory for '%s'\n",
116 			name);
117 		return;
118 	}
119 
120 	/* initalize mii_dev struct fields */
121 	new_dev->read = legacy_miiphy_read;
122 	new_dev->write = legacy_miiphy_write;
123 	strncpy(new_dev->name, name, MDIO_NAME_LEN);
124 	new_dev->name[MDIO_NAME_LEN - 1] = 0;
125 	ldev->read = read;
126 	ldev->write = write;
127 	new_dev->priv = ldev;
128 
129 	debug("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
130 	       new_dev->name, ldev->read, ldev->write);
131 
132 	/* add it to the list */
133 	list_add_tail(&new_dev->link, &mii_devs);
134 
135 	if (!current_mii)
136 		current_mii = new_dev;
137 }
138 
139 struct mii_dev *mdio_alloc(void)
140 {
141 	struct mii_dev *bus;
142 
143 	bus = malloc(sizeof(*bus));
144 	if (!bus)
145 		return bus;
146 
147 	memset(bus, 0, sizeof(*bus));
148 
149 	/* initalize mii_dev struct fields */
150 	INIT_LIST_HEAD(&bus->link);
151 
152 	return bus;
153 }
154 
155 int mdio_register(struct mii_dev *bus)
156 {
157 	if (!bus || !bus->name || !bus->read || !bus->write)
158 		return -1;
159 
160 	/* check if we have unique name */
161 	if (miiphy_get_dev_by_name(bus->name)) {
162 		printf("mdio_register: non unique device name '%s'\n",
163 			bus->name);
164 		return -1;
165 	}
166 
167 	/* add it to the list */
168 	list_add_tail(&bus->link, &mii_devs);
169 
170 	if (!current_mii)
171 		current_mii = bus;
172 
173 	return 0;
174 }
175 
176 void mdio_list_devices(void)
177 {
178 	struct list_head *entry;
179 
180 	list_for_each(entry, &mii_devs) {
181 		int i;
182 		struct mii_dev *bus = list_entry(entry, struct mii_dev, link);
183 
184 		printf("%s:\n", bus->name);
185 
186 		for (i = 0; i < PHY_MAX_ADDR; i++) {
187 			struct phy_device *phydev = bus->phymap[i];
188 
189 			if (phydev) {
190 				printf("%d - %s", i, phydev->drv->name);
191 
192 				if (phydev->dev)
193 					printf(" <--> %s\n", phydev->dev->name);
194 				else
195 					printf("\n");
196 			}
197 		}
198 	}
199 }
200 
201 int miiphy_set_current_dev(const char *devname)
202 {
203 	struct mii_dev *dev;
204 
205 	dev = miiphy_get_dev_by_name(devname);
206 	if (dev) {
207 		current_mii = dev;
208 		return 0;
209 	}
210 
211 	printf("No such device: %s\n", devname);
212 
213 	return 1;
214 }
215 
216 struct mii_dev *mdio_get_current_dev(void)
217 {
218 	return current_mii;
219 }
220 
221 struct phy_device *mdio_phydev_for_ethname(const char *ethname)
222 {
223 	struct list_head *entry;
224 	struct mii_dev *bus;
225 
226 	list_for_each(entry, &mii_devs) {
227 		int i;
228 		bus = list_entry(entry, struct mii_dev, link);
229 
230 		for (i = 0; i < PHY_MAX_ADDR; i++) {
231 			if (!bus->phymap[i] || !bus->phymap[i]->dev)
232 				continue;
233 
234 			if (strcmp(bus->phymap[i]->dev->name, ethname) == 0)
235 				return bus->phymap[i];
236 		}
237 	}
238 
239 	printf("%s is not a known ethernet\n", ethname);
240 	return NULL;
241 }
242 
243 const char *miiphy_get_current_dev(void)
244 {
245 	if (current_mii)
246 		return current_mii->name;
247 
248 	return NULL;
249 }
250 
251 static struct mii_dev *miiphy_get_active_dev(const char *devname)
252 {
253 	/* If the current mii is the one we want, return it */
254 	if (current_mii)
255 		if (strcmp(current_mii->name, devname) == 0)
256 			return current_mii;
257 
258 	/* Otherwise, set the active one to the one we want */
259 	if (miiphy_set_current_dev(devname))
260 		return NULL;
261 	else
262 		return current_mii;
263 }
264 
265 /*****************************************************************************
266  *
267  * Read to variable <value> from the PHY attached to device <devname>,
268  * use PHY address <addr> and register <reg>.
269  *
270  * This API is deprecated. Use phy_read on a phy_device found via phy_connect
271  *
272  * Returns:
273  *   0 on success
274  */
275 int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
276 		 unsigned short *value)
277 {
278 	struct mii_dev *bus;
279 	int ret;
280 
281 	bus = miiphy_get_active_dev(devname);
282 	if (!bus)
283 		return 1;
284 
285 	ret = bus->read(bus, addr, MDIO_DEVAD_NONE, reg);
286 	if (ret < 0)
287 		return 1;
288 
289 	*value = (unsigned short)ret;
290 	return 0;
291 }
292 
293 /*****************************************************************************
294  *
295  * Write <value> to the PHY attached to device <devname>,
296  * use PHY address <addr> and register <reg>.
297  *
298  * This API is deprecated. Use phy_write on a phy_device found by phy_connect
299  *
300  * Returns:
301  *   0 on success
302  */
303 int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
304 		  unsigned short value)
305 {
306 	struct mii_dev *bus;
307 
308 	bus = miiphy_get_active_dev(devname);
309 	if (bus)
310 		return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value);
311 
312 	return 1;
313 }
314 
315 /*****************************************************************************
316  *
317  * Print out list of registered MII capable devices.
318  */
319 void miiphy_listdev(void)
320 {
321 	struct list_head *entry;
322 	struct mii_dev *dev;
323 
324 	puts("MII devices: ");
325 	list_for_each(entry, &mii_devs) {
326 		dev = list_entry(entry, struct mii_dev, link);
327 		printf("'%s' ", dev->name);
328 	}
329 	puts("\n");
330 
331 	if (current_mii)
332 		printf("Current device: '%s'\n", current_mii->name);
333 }
334 
335 /*****************************************************************************
336  *
337  * Read the OUI, manufacture's model number, and revision number.
338  *
339  * OUI:     22 bits (unsigned int)
340  * Model:    6 bits (unsigned char)
341  * Revision: 4 bits (unsigned char)
342  *
343  * This API is deprecated.
344  *
345  * Returns:
346  *   0 on success
347  */
348 int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
349 		 unsigned char *model, unsigned char *rev)
350 {
351 	unsigned int reg = 0;
352 	unsigned short tmp;
353 
354 	if (miiphy_read(devname, addr, MII_PHYSID2, &tmp) != 0) {
355 		debug("PHY ID register 2 read failed\n");
356 		return -1;
357 	}
358 	reg = tmp;
359 
360 	debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg);
361 
362 	if (reg == 0xFFFF) {
363 		/* No physical device present at this address */
364 		return -1;
365 	}
366 
367 	if (miiphy_read(devname, addr, MII_PHYSID1, &tmp) != 0) {
368 		debug("PHY ID register 1 read failed\n");
369 		return -1;
370 	}
371 	reg |= tmp << 16;
372 	debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
373 
374 	*oui = (reg >> 10);
375 	*model = (unsigned char)((reg >> 4) & 0x0000003F);
376 	*rev = (unsigned char)(reg & 0x0000000F);
377 	return 0;
378 }
379 
380 #ifndef CONFIG_PHYLIB
381 /*****************************************************************************
382  *
383  * Reset the PHY.
384  *
385  * This API is deprecated. Use PHYLIB.
386  *
387  * Returns:
388  *   0 on success
389  */
390 int miiphy_reset(const char *devname, unsigned char addr)
391 {
392 	unsigned short reg;
393 	int timeout = 500;
394 
395 	if (miiphy_read(devname, addr, MII_BMCR, &reg) != 0) {
396 		debug("PHY status read failed\n");
397 		return -1;
398 	}
399 	if (miiphy_write(devname, addr, MII_BMCR, reg | BMCR_RESET) != 0) {
400 		debug("PHY reset failed\n");
401 		return -1;
402 	}
403 #ifdef CONFIG_PHY_RESET_DELAY
404 	udelay(CONFIG_PHY_RESET_DELAY);	/* Intel LXT971A needs this */
405 #endif
406 	/*
407 	 * Poll the control register for the reset bit to go to 0 (it is
408 	 * auto-clearing).  This should happen within 0.5 seconds per the
409 	 * IEEE spec.
410 	 */
411 	reg = 0x8000;
412 	while (((reg & 0x8000) != 0) && timeout--) {
413 		if (miiphy_read(devname, addr, MII_BMCR, &reg) != 0) {
414 			debug("PHY status read failed\n");
415 			return -1;
416 		}
417 		udelay(1000);
418 	}
419 	if ((reg & 0x8000) == 0) {
420 		return 0;
421 	} else {
422 		puts("PHY reset timed out\n");
423 		return -1;
424 	}
425 	return 0;
426 }
427 #endif /* !PHYLIB */
428 
429 /*****************************************************************************
430  *
431  * Determine the ethernet speed (10/100/1000).  Return 10 on error.
432  */
433 int miiphy_speed(const char *devname, unsigned char addr)
434 {
435 	u16 bmcr, anlpar;
436 
437 #if defined(CONFIG_PHY_GIGE)
438 	u16 btsr;
439 
440 	/*
441 	 * Check for 1000BASE-X.  If it is supported, then assume that the speed
442 	 * is 1000.
443 	 */
444 	if (miiphy_is_1000base_x(devname, addr))
445 		return _1000BASET;
446 
447 	/*
448 	 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
449 	 */
450 	/* Check for 1000BASE-T. */
451 	if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
452 		printf("PHY 1000BT status");
453 		goto miiphy_read_failed;
454 	}
455 	if (btsr != 0xFFFF &&
456 			(btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
457 		return _1000BASET;
458 #endif /* CONFIG_PHY_GIGE */
459 
460 	/* Check Basic Management Control Register first. */
461 	if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
462 		printf("PHY speed");
463 		goto miiphy_read_failed;
464 	}
465 	/* Check if auto-negotiation is on. */
466 	if (bmcr & BMCR_ANENABLE) {
467 		/* Get auto-negotiation results. */
468 		if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
469 			printf("PHY AN speed");
470 			goto miiphy_read_failed;
471 		}
472 		return (anlpar & LPA_100) ? _100BASET : _10BASET;
473 	}
474 	/* Get speed from basic control settings. */
475 	return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
476 
477 miiphy_read_failed:
478 	printf(" read failed, assuming 10BASE-T\n");
479 	return _10BASET;
480 }
481 
482 /*****************************************************************************
483  *
484  * Determine full/half duplex.  Return half on error.
485  */
486 int miiphy_duplex(const char *devname, unsigned char addr)
487 {
488 	u16 bmcr, anlpar;
489 
490 #if defined(CONFIG_PHY_GIGE)
491 	u16 btsr;
492 
493 	/* Check for 1000BASE-X. */
494 	if (miiphy_is_1000base_x(devname, addr)) {
495 		/* 1000BASE-X */
496 		if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
497 			printf("1000BASE-X PHY AN duplex");
498 			goto miiphy_read_failed;
499 		}
500 	}
501 	/*
502 	 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
503 	 */
504 	/* Check for 1000BASE-T. */
505 	if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
506 		printf("PHY 1000BT status");
507 		goto miiphy_read_failed;
508 	}
509 	if (btsr != 0xFFFF) {
510 		if (btsr & PHY_1000BTSR_1000FD) {
511 			return FULL;
512 		} else if (btsr & PHY_1000BTSR_1000HD) {
513 			return HALF;
514 		}
515 	}
516 #endif /* CONFIG_PHY_GIGE */
517 
518 	/* Check Basic Management Control Register first. */
519 	if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
520 		puts("PHY duplex");
521 		goto miiphy_read_failed;
522 	}
523 	/* Check if auto-negotiation is on. */
524 	if (bmcr & BMCR_ANENABLE) {
525 		/* Get auto-negotiation results. */
526 		if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
527 			puts("PHY AN duplex");
528 			goto miiphy_read_failed;
529 		}
530 		return (anlpar & (LPA_10FULL | LPA_100FULL)) ?
531 		    FULL : HALF;
532 	}
533 	/* Get speed from basic control settings. */
534 	return (bmcr & BMCR_FULLDPLX) ? FULL : HALF;
535 
536 miiphy_read_failed:
537 	printf(" read failed, assuming half duplex\n");
538 	return HALF;
539 }
540 
541 /*****************************************************************************
542  *
543  * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
544  * 1000BASE-T, or on error.
545  */
546 int miiphy_is_1000base_x(const char *devname, unsigned char addr)
547 {
548 #if defined(CONFIG_PHY_GIGE)
549 	u16 exsr;
550 
551 	if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) {
552 		printf("PHY extended status read failed, assuming no "
553 			"1000BASE-X\n");
554 		return 0;
555 	}
556 	return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH));
557 #else
558 	return 0;
559 #endif
560 }
561 
562 #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
563 /*****************************************************************************
564  *
565  * Determine link status
566  */
567 int miiphy_link(const char *devname, unsigned char addr)
568 {
569 	unsigned short reg;
570 
571 	/* dummy read; needed to latch some phys */
572 	(void)miiphy_read(devname, addr, MII_BMSR, &reg);
573 	if (miiphy_read(devname, addr, MII_BMSR, &reg)) {
574 		puts("MII_BMSR read failed, assuming no link\n");
575 		return 0;
576 	}
577 
578 	/* Determine if a link is active */
579 	if ((reg & BMSR_LSTATUS) != 0) {
580 		return 1;
581 	} else {
582 		return 0;
583 	}
584 }
585 #endif
586