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