xref: /linux/drivers/net/phy/phy_device.c (revision b454cc6636d254fbf6049b73e9560aee76fb04a3)
1 /*
2  * drivers/net/phy/phy_device.c
3  *
4  * Framework for finding and configuring PHYs.
5  * Also contains generic PHY driver
6  *
7  * Author: Andy Fleming
8  *
9  * Copyright (c) 2004 Freescale Semiconductor, Inc.
10  *
11  * This program is free software; you can redistribute  it and/or modify it
12  * under  the terms of  the GNU General  Public License as published by the
13  * Free Software Foundation;  either version 2 of the  License, or (at your
14  * option) any later version.
15  *
16  */
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/unistd.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/spinlock.h>
30 #include <linux/mm.h>
31 #include <linux/module.h>
32 #include <linux/mii.h>
33 #include <linux/ethtool.h>
34 #include <linux/phy.h>
35 
36 #include <asm/io.h>
37 #include <asm/irq.h>
38 #include <asm/uaccess.h>
39 
40 MODULE_DESCRIPTION("PHY library");
41 MODULE_AUTHOR("Andy Fleming");
42 MODULE_LICENSE("GPL");
43 
44 static struct phy_driver genphy_driver;
45 extern int mdio_bus_init(void);
46 extern void mdio_bus_exit(void);
47 
48 struct phy_device* phy_device_create(struct mii_bus *bus, int addr, int phy_id)
49 {
50 	struct phy_device *dev;
51 	/* We allocate the device, and initialize the
52 	 * default values */
53 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
54 
55 	if (NULL == dev)
56 		return (struct phy_device*) PTR_ERR((void*)-ENOMEM);
57 
58 	dev->speed = 0;
59 	dev->duplex = -1;
60 	dev->pause = dev->asym_pause = 0;
61 	dev->link = 1;
62 	dev->interface = PHY_INTERFACE_MODE_GMII;
63 
64 	dev->autoneg = AUTONEG_ENABLE;
65 
66 	dev->addr = addr;
67 	dev->phy_id = phy_id;
68 	dev->bus = bus;
69 
70 	dev->state = PHY_DOWN;
71 
72 	spin_lock_init(&dev->lock);
73 
74 	return dev;
75 }
76 EXPORT_SYMBOL(phy_device_create);
77 
78 /* get_phy_device
79  *
80  * description: Reads the ID registers of the PHY at addr on the
81  *   bus, then allocates and returns the phy_device to
82  *   represent it.
83  */
84 struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
85 {
86 	int phy_reg;
87 	u32 phy_id;
88 	struct phy_device *dev = NULL;
89 
90 	/* Grab the bits from PHYIR1, and put them
91 	 * in the upper half */
92 	phy_reg = bus->read(bus, addr, MII_PHYSID1);
93 
94 	if (phy_reg < 0)
95 		return ERR_PTR(phy_reg);
96 
97 	phy_id = (phy_reg & 0xffff) << 16;
98 
99 	/* Grab the bits from PHYIR2, and put them in the lower half */
100 	phy_reg = bus->read(bus, addr, MII_PHYSID2);
101 
102 	if (phy_reg < 0)
103 		return ERR_PTR(phy_reg);
104 
105 	phy_id |= (phy_reg & 0xffff);
106 
107 	/* If the phy_id is all Fs, there is no device there */
108 	if (0xffffffff == phy_id)
109 		return NULL;
110 
111 	dev = phy_device_create(bus, addr, phy_id);
112 
113 	return dev;
114 }
115 
116 /* phy_prepare_link:
117  *
118  * description: Tells the PHY infrastructure to handle the
119  *   gory details on monitoring link status (whether through
120  *   polling or an interrupt), and to call back to the
121  *   connected device driver when the link status changes.
122  *   If you want to monitor your own link state, don't call
123  *   this function */
124 void phy_prepare_link(struct phy_device *phydev,
125 		void (*handler)(struct net_device *))
126 {
127 	phydev->adjust_link = handler;
128 }
129 
130 /* phy_connect:
131  *
132  * description: Convenience function for connecting ethernet
133  *   devices to PHY devices.  The default behavior is for
134  *   the PHY infrastructure to handle everything, and only notify
135  *   the connected driver when the link status changes.  If you
136  *   don't want, or can't use the provided functionality, you may
137  *   choose to call only the subset of functions which provide
138  *   the desired functionality.
139  */
140 struct phy_device * phy_connect(struct net_device *dev, const char *phy_id,
141 		void (*handler)(struct net_device *), u32 flags,
142 		u32 interface)
143 {
144 	struct phy_device *phydev;
145 
146 	phydev = phy_attach(dev, phy_id, flags, interface);
147 
148 	if (IS_ERR(phydev))
149 		return phydev;
150 
151 	phy_prepare_link(phydev, handler);
152 
153 	phy_start_machine(phydev, NULL);
154 
155 	if (phydev->irq > 0)
156 		phy_start_interrupts(phydev);
157 
158 	return phydev;
159 }
160 EXPORT_SYMBOL(phy_connect);
161 
162 void phy_disconnect(struct phy_device *phydev)
163 {
164 	if (phydev->irq > 0)
165 		phy_stop_interrupts(phydev);
166 
167 	phy_stop_machine(phydev);
168 
169 	phydev->adjust_link = NULL;
170 
171 	phy_detach(phydev);
172 }
173 EXPORT_SYMBOL(phy_disconnect);
174 
175 /* phy_attach:
176  *
177  *   description: Called by drivers to attach to a particular PHY
178  *     device. The phy_device is found, and properly hooked up
179  *     to the phy_driver.  If no driver is attached, then the
180  *     genphy_driver is used.  The phy_device is given a ptr to
181  *     the attaching device, and given a callback for link status
182  *     change.  The phy_device is returned to the attaching
183  *     driver.
184  */
185 static int phy_compare_id(struct device *dev, void *data)
186 {
187 	return strcmp((char *)data, dev->bus_id) ? 0 : 1;
188 }
189 
190 struct phy_device *phy_attach(struct net_device *dev,
191 		const char *phy_id, u32 flags, u32 interface)
192 {
193 	struct bus_type *bus = &mdio_bus_type;
194 	struct phy_device *phydev;
195 	struct device *d;
196 
197 	/* Search the list of PHY devices on the mdio bus for the
198 	 * PHY with the requested name */
199 	d = bus_find_device(bus, NULL, (void *)phy_id, phy_compare_id);
200 
201 	if (d) {
202 		phydev = to_phy_device(d);
203 	} else {
204 		printk(KERN_ERR "%s not found\n", phy_id);
205 		return ERR_PTR(-ENODEV);
206 	}
207 
208 	/* Assume that if there is no driver, that it doesn't
209 	 * exist, and we should use the genphy driver. */
210 	if (NULL == d->driver) {
211 		int err;
212 		down_write(&d->bus->subsys.rwsem);
213 		d->driver = &genphy_driver.driver;
214 
215 		err = d->driver->probe(d);
216 
217 		if (err >= 0)
218 			err = device_bind_driver(d);
219 
220 		up_write(&d->bus->subsys.rwsem);
221 
222 		if (err)
223 			return ERR_PTR(err);
224 	}
225 
226 	if (phydev->attached_dev) {
227 		printk(KERN_ERR "%s: %s already attached\n",
228 				dev->name, phy_id);
229 		return ERR_PTR(-EBUSY);
230 	}
231 
232 	phydev->attached_dev = dev;
233 
234 	phydev->dev_flags = flags;
235 
236 	phydev->interface = interface;
237 
238 	/* Do initial configuration here, now that
239 	 * we have certain key parameters
240 	 * (dev_flags and interface) */
241 	if (phydev->drv->config_init) {
242 		int err;
243 
244 		err = phydev->drv->config_init(phydev);
245 
246 		if (err < 0)
247 			return ERR_PTR(err);
248 	}
249 
250 	return phydev;
251 }
252 EXPORT_SYMBOL(phy_attach);
253 
254 void phy_detach(struct phy_device *phydev)
255 {
256 	phydev->attached_dev = NULL;
257 
258 	/* If the device had no specific driver before (i.e. - it
259 	 * was using the generic driver), we unbind the device
260 	 * from the generic driver so that there's a chance a
261 	 * real driver could be loaded */
262 	if (phydev->dev.driver == &genphy_driver.driver) {
263 		down_write(&phydev->dev.bus->subsys.rwsem);
264 		device_release_driver(&phydev->dev);
265 		up_write(&phydev->dev.bus->subsys.rwsem);
266 	}
267 }
268 EXPORT_SYMBOL(phy_detach);
269 
270 
271 /* Generic PHY support and helper functions */
272 
273 /* genphy_config_advert
274  *
275  * description: Writes MII_ADVERTISE with the appropriate values,
276  *   after sanitizing the values to make sure we only advertise
277  *   what is supported
278  */
279 int genphy_config_advert(struct phy_device *phydev)
280 {
281 	u32 advertise;
282 	int adv;
283 	int err;
284 
285 	/* Only allow advertising what
286 	 * this PHY supports */
287 	phydev->advertising &= phydev->supported;
288 	advertise = phydev->advertising;
289 
290 	/* Setup standard advertisement */
291 	adv = phy_read(phydev, MII_ADVERTISE);
292 
293 	if (adv < 0)
294 		return adv;
295 
296 	adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
297 		 ADVERTISE_PAUSE_ASYM);
298 	if (advertise & ADVERTISED_10baseT_Half)
299 		adv |= ADVERTISE_10HALF;
300 	if (advertise & ADVERTISED_10baseT_Full)
301 		adv |= ADVERTISE_10FULL;
302 	if (advertise & ADVERTISED_100baseT_Half)
303 		adv |= ADVERTISE_100HALF;
304 	if (advertise & ADVERTISED_100baseT_Full)
305 		adv |= ADVERTISE_100FULL;
306 	if (advertise & ADVERTISED_Pause)
307 		adv |= ADVERTISE_PAUSE_CAP;
308 	if (advertise & ADVERTISED_Asym_Pause)
309 		adv |= ADVERTISE_PAUSE_ASYM;
310 
311 	err = phy_write(phydev, MII_ADVERTISE, adv);
312 
313 	if (err < 0)
314 		return err;
315 
316 	/* Configure gigabit if it's supported */
317 	if (phydev->supported & (SUPPORTED_1000baseT_Half |
318 				SUPPORTED_1000baseT_Full)) {
319 		adv = phy_read(phydev, MII_CTRL1000);
320 
321 		if (adv < 0)
322 			return adv;
323 
324 		adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
325 		if (advertise & SUPPORTED_1000baseT_Half)
326 			adv |= ADVERTISE_1000HALF;
327 		if (advertise & SUPPORTED_1000baseT_Full)
328 			adv |= ADVERTISE_1000FULL;
329 		err = phy_write(phydev, MII_CTRL1000, adv);
330 
331 		if (err < 0)
332 			return err;
333 	}
334 
335 	return adv;
336 }
337 EXPORT_SYMBOL(genphy_config_advert);
338 
339 /* genphy_setup_forced
340  *
341  * description: Configures MII_BMCR to force speed/duplex
342  *   to the values in phydev. Assumes that the values are valid.
343  *   Please see phy_sanitize_settings() */
344 int genphy_setup_forced(struct phy_device *phydev)
345 {
346 	int ctl = BMCR_RESET;
347 
348 	phydev->pause = phydev->asym_pause = 0;
349 
350 	if (SPEED_1000 == phydev->speed)
351 		ctl |= BMCR_SPEED1000;
352 	else if (SPEED_100 == phydev->speed)
353 		ctl |= BMCR_SPEED100;
354 
355 	if (DUPLEX_FULL == phydev->duplex)
356 		ctl |= BMCR_FULLDPLX;
357 
358 	ctl = phy_write(phydev, MII_BMCR, ctl);
359 
360 	if (ctl < 0)
361 		return ctl;
362 
363 	/* We just reset the device, so we'd better configure any
364 	 * settings the PHY requires to operate */
365 	if (phydev->drv->config_init)
366 		ctl = phydev->drv->config_init(phydev);
367 
368 	return ctl;
369 }
370 
371 
372 /* Enable and Restart Autonegotiation */
373 int genphy_restart_aneg(struct phy_device *phydev)
374 {
375 	int ctl;
376 
377 	ctl = phy_read(phydev, MII_BMCR);
378 
379 	if (ctl < 0)
380 		return ctl;
381 
382 	ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
383 
384 	/* Don't isolate the PHY if we're negotiating */
385 	ctl &= ~(BMCR_ISOLATE);
386 
387 	ctl = phy_write(phydev, MII_BMCR, ctl);
388 
389 	return ctl;
390 }
391 
392 
393 /* genphy_config_aneg
394  *
395  * description: If auto-negotiation is enabled, we configure the
396  *   advertising, and then restart auto-negotiation.  If it is not
397  *   enabled, then we write the BMCR
398  */
399 int genphy_config_aneg(struct phy_device *phydev)
400 {
401 	int err = 0;
402 
403 	if (AUTONEG_ENABLE == phydev->autoneg) {
404 		err = genphy_config_advert(phydev);
405 
406 		if (err < 0)
407 			return err;
408 
409 		err = genphy_restart_aneg(phydev);
410 	} else
411 		err = genphy_setup_forced(phydev);
412 
413 	return err;
414 }
415 EXPORT_SYMBOL(genphy_config_aneg);
416 
417 /* genphy_update_link
418  *
419  * description: Update the value in phydev->link to reflect the
420  *   current link value.  In order to do this, we need to read
421  *   the status register twice, keeping the second value
422  */
423 int genphy_update_link(struct phy_device *phydev)
424 {
425 	int status;
426 
427 	/* Do a fake read */
428 	status = phy_read(phydev, MII_BMSR);
429 
430 	if (status < 0)
431 		return status;
432 
433 	/* Read link and autonegotiation status */
434 	status = phy_read(phydev, MII_BMSR);
435 
436 	if (status < 0)
437 		return status;
438 
439 	if ((status & BMSR_LSTATUS) == 0)
440 		phydev->link = 0;
441 	else
442 		phydev->link = 1;
443 
444 	return 0;
445 }
446 EXPORT_SYMBOL(genphy_update_link);
447 
448 /* genphy_read_status
449  *
450  * description: Check the link, then figure out the current state
451  *   by comparing what we advertise with what the link partner
452  *   advertises.  Start by checking the gigabit possibilities,
453  *   then move on to 10/100.
454  */
455 int genphy_read_status(struct phy_device *phydev)
456 {
457 	int adv;
458 	int err;
459 	int lpa;
460 	int lpagb = 0;
461 
462 	/* Update the link, but return if there
463 	 * was an error */
464 	err = genphy_update_link(phydev);
465 	if (err)
466 		return err;
467 
468 	if (AUTONEG_ENABLE == phydev->autoneg) {
469 		if (phydev->supported & (SUPPORTED_1000baseT_Half
470 					| SUPPORTED_1000baseT_Full)) {
471 			lpagb = phy_read(phydev, MII_STAT1000);
472 
473 			if (lpagb < 0)
474 				return lpagb;
475 
476 			adv = phy_read(phydev, MII_CTRL1000);
477 
478 			if (adv < 0)
479 				return adv;
480 
481 			lpagb &= adv << 2;
482 		}
483 
484 		lpa = phy_read(phydev, MII_LPA);
485 
486 		if (lpa < 0)
487 			return lpa;
488 
489 		adv = phy_read(phydev, MII_ADVERTISE);
490 
491 		if (adv < 0)
492 			return adv;
493 
494 		lpa &= adv;
495 
496 		phydev->speed = SPEED_10;
497 		phydev->duplex = DUPLEX_HALF;
498 		phydev->pause = phydev->asym_pause = 0;
499 
500 		if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
501 			phydev->speed = SPEED_1000;
502 
503 			if (lpagb & LPA_1000FULL)
504 				phydev->duplex = DUPLEX_FULL;
505 		} else if (lpa & (LPA_100FULL | LPA_100HALF)) {
506 			phydev->speed = SPEED_100;
507 
508 			if (lpa & LPA_100FULL)
509 				phydev->duplex = DUPLEX_FULL;
510 		} else
511 			if (lpa & LPA_10FULL)
512 				phydev->duplex = DUPLEX_FULL;
513 
514 		if (phydev->duplex == DUPLEX_FULL){
515 			phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
516 			phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
517 		}
518 	} else {
519 		int bmcr = phy_read(phydev, MII_BMCR);
520 		if (bmcr < 0)
521 			return bmcr;
522 
523 		if (bmcr & BMCR_FULLDPLX)
524 			phydev->duplex = DUPLEX_FULL;
525 		else
526 			phydev->duplex = DUPLEX_HALF;
527 
528 		if (bmcr & BMCR_SPEED1000)
529 			phydev->speed = SPEED_1000;
530 		else if (bmcr & BMCR_SPEED100)
531 			phydev->speed = SPEED_100;
532 		else
533 			phydev->speed = SPEED_10;
534 
535 		phydev->pause = phydev->asym_pause = 0;
536 	}
537 
538 	return 0;
539 }
540 EXPORT_SYMBOL(genphy_read_status);
541 
542 static int genphy_config_init(struct phy_device *phydev)
543 {
544 	int val;
545 	u32 features;
546 
547 	/* For now, I'll claim that the generic driver supports
548 	 * all possible port types */
549 	features = (SUPPORTED_TP | SUPPORTED_MII
550 			| SUPPORTED_AUI | SUPPORTED_FIBRE |
551 			SUPPORTED_BNC);
552 
553 	/* Do we support autonegotiation? */
554 	val = phy_read(phydev, MII_BMSR);
555 
556 	if (val < 0)
557 		return val;
558 
559 	if (val & BMSR_ANEGCAPABLE)
560 		features |= SUPPORTED_Autoneg;
561 
562 	if (val & BMSR_100FULL)
563 		features |= SUPPORTED_100baseT_Full;
564 	if (val & BMSR_100HALF)
565 		features |= SUPPORTED_100baseT_Half;
566 	if (val & BMSR_10FULL)
567 		features |= SUPPORTED_10baseT_Full;
568 	if (val & BMSR_10HALF)
569 		features |= SUPPORTED_10baseT_Half;
570 
571 	if (val & BMSR_ESTATEN) {
572 		val = phy_read(phydev, MII_ESTATUS);
573 
574 		if (val < 0)
575 			return val;
576 
577 		if (val & ESTATUS_1000_TFULL)
578 			features |= SUPPORTED_1000baseT_Full;
579 		if (val & ESTATUS_1000_THALF)
580 			features |= SUPPORTED_1000baseT_Half;
581 	}
582 
583 	phydev->supported = features;
584 	phydev->advertising = features;
585 
586 	return 0;
587 }
588 
589 
590 /* phy_probe
591  *
592  * description: Take care of setting up the phy_device structure,
593  *   set the state to READY (the driver's init function should
594  *   set it to STARTING if needed).
595  */
596 static int phy_probe(struct device *dev)
597 {
598 	struct phy_device *phydev;
599 	struct phy_driver *phydrv;
600 	struct device_driver *drv;
601 	int err = 0;
602 
603 	phydev = to_phy_device(dev);
604 
605 	/* Make sure the driver is held.
606 	 * XXX -- Is this correct? */
607 	drv = get_driver(phydev->dev.driver);
608 	phydrv = to_phy_driver(drv);
609 	phydev->drv = phydrv;
610 
611 	/* Disable the interrupt if the PHY doesn't support it */
612 	if (!(phydrv->flags & PHY_HAS_INTERRUPT))
613 		phydev->irq = PHY_POLL;
614 
615 	spin_lock(&phydev->lock);
616 
617 	/* Start out supporting everything. Eventually,
618 	 * a controller will attach, and may modify one
619 	 * or both of these values */
620 	phydev->supported = phydrv->features;
621 	phydev->advertising = phydrv->features;
622 
623 	/* Set the state to READY by default */
624 	phydev->state = PHY_READY;
625 
626 	if (phydev->drv->probe)
627 		err = phydev->drv->probe(phydev);
628 
629 	spin_unlock(&phydev->lock);
630 
631 	return err;
632 
633 }
634 
635 static int phy_remove(struct device *dev)
636 {
637 	struct phy_device *phydev;
638 
639 	phydev = to_phy_device(dev);
640 
641 	spin_lock(&phydev->lock);
642 	phydev->state = PHY_DOWN;
643 	spin_unlock(&phydev->lock);
644 
645 	if (phydev->drv->remove)
646 		phydev->drv->remove(phydev);
647 
648 	put_driver(dev->driver);
649 	phydev->drv = NULL;
650 
651 	return 0;
652 }
653 
654 int phy_driver_register(struct phy_driver *new_driver)
655 {
656 	int retval;
657 
658 	memset(&new_driver->driver, 0, sizeof(new_driver->driver));
659 	new_driver->driver.name = new_driver->name;
660 	new_driver->driver.bus = &mdio_bus_type;
661 	new_driver->driver.probe = phy_probe;
662 	new_driver->driver.remove = phy_remove;
663 
664 	retval = driver_register(&new_driver->driver);
665 
666 	if (retval) {
667 		printk(KERN_ERR "%s: Error %d in registering driver\n",
668 				new_driver->name, retval);
669 
670 		return retval;
671 	}
672 
673 	pr_info("%s: Registered new driver\n", new_driver->name);
674 
675 	return 0;
676 }
677 EXPORT_SYMBOL(phy_driver_register);
678 
679 void phy_driver_unregister(struct phy_driver *drv)
680 {
681 	driver_unregister(&drv->driver);
682 }
683 EXPORT_SYMBOL(phy_driver_unregister);
684 
685 static struct phy_driver genphy_driver = {
686 	.phy_id		= 0xffffffff,
687 	.phy_id_mask	= 0xffffffff,
688 	.name		= "Generic PHY",
689 	.config_init	= genphy_config_init,
690 	.features	= 0,
691 	.config_aneg	= genphy_config_aneg,
692 	.read_status	= genphy_read_status,
693 	.driver		= {.owner= THIS_MODULE, },
694 };
695 
696 static int __init phy_init(void)
697 {
698 	int rc;
699 
700 	rc = mdio_bus_init();
701 	if (rc)
702 		return rc;
703 
704 	rc = phy_driver_register(&genphy_driver);
705 	if (rc)
706 		mdio_bus_exit();
707 
708 	return rc;
709 }
710 
711 static void __exit phy_exit(void)
712 {
713 	phy_driver_unregister(&genphy_driver);
714 	mdio_bus_exit();
715 }
716 
717 subsys_initcall(phy_init);
718 module_exit(phy_exit);
719