Merge branch 'master' into upstream
[deliverable/linux.git] / drivers / net / phy / phy_device.c
CommitLineData
00db8189
AF
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 */
00db8189
AF
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>
00db8189
AF
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
afcceaa3
OH
40MODULE_DESCRIPTION("PHY library");
41MODULE_AUTHOR("Andy Fleming");
42MODULE_LICENSE("GPL");
43
e1393456
AF
44static struct phy_driver genphy_driver;
45extern int mdio_bus_init(void);
46extern void mdio_bus_exit(void);
67c4f3fa 47
11b0bacd
VB
48struct 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
00db8189
AF
77/* 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
11b0bacd 110 dev = phy_device_create(bus, addr, phy_id);
00db8189
AF
111
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
e1393456
AF
129/* 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
e1393456
AF
173/* 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
215 if (err < 0)
216 return ERR_PTR(err);
217
218 device_bind_driver(d);
219 up_write(&d->bus->subsys.rwsem);
220 }
221
222 if (phydev->attached_dev) {
223 printk(KERN_ERR "%s: %s already attached\n",
224 dev->name, phy_id);
225 return ERR_PTR(-EBUSY);
226 }
227
228 phydev->attached_dev = dev;
229
230 phydev->dev_flags = flags;
231
232 return phydev;
233}
234EXPORT_SYMBOL(phy_attach);
235
236void phy_detach(struct phy_device *phydev)
237{
238 phydev->attached_dev = NULL;
239
240 /* If the device had no specific driver before (i.e. - it
241 * was using the generic driver), we unbind the device
242 * from the generic driver so that there's a chance a
243 * real driver could be loaded */
244 if (phydev->dev.driver == &genphy_driver.driver) {
245 down_write(&phydev->dev.bus->subsys.rwsem);
246 device_release_driver(&phydev->dev);
247 up_write(&phydev->dev.bus->subsys.rwsem);
248 }
249}
250EXPORT_SYMBOL(phy_detach);
251
252
00db8189
AF
253/* Generic PHY support and helper functions */
254
255/* genphy_config_advert
256 *
257 * description: Writes MII_ADVERTISE with the appropriate values,
258 * after sanitizing the values to make sure we only advertise
259 * what is supported
260 */
e1393456 261int genphy_config_advert(struct phy_device *phydev)
00db8189
AF
262{
263 u32 advertise;
264 int adv;
265 int err;
266
267 /* Only allow advertising what
268 * this PHY supports */
269 phydev->advertising &= phydev->supported;
270 advertise = phydev->advertising;
271
272 /* Setup standard advertisement */
273 adv = phy_read(phydev, MII_ADVERTISE);
274
275 if (adv < 0)
276 return adv;
277
278 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
279 ADVERTISE_PAUSE_ASYM);
280 if (advertise & ADVERTISED_10baseT_Half)
281 adv |= ADVERTISE_10HALF;
282 if (advertise & ADVERTISED_10baseT_Full)
283 adv |= ADVERTISE_10FULL;
284 if (advertise & ADVERTISED_100baseT_Half)
285 adv |= ADVERTISE_100HALF;
286 if (advertise & ADVERTISED_100baseT_Full)
287 adv |= ADVERTISE_100FULL;
288 if (advertise & ADVERTISED_Pause)
289 adv |= ADVERTISE_PAUSE_CAP;
290 if (advertise & ADVERTISED_Asym_Pause)
291 adv |= ADVERTISE_PAUSE_ASYM;
292
293 err = phy_write(phydev, MII_ADVERTISE, adv);
294
295 if (err < 0)
296 return err;
297
298 /* Configure gigabit if it's supported */
299 if (phydev->supported & (SUPPORTED_1000baseT_Half |
300 SUPPORTED_1000baseT_Full)) {
301 adv = phy_read(phydev, MII_CTRL1000);
302
303 if (adv < 0)
304 return adv;
305
306 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
307 if (advertise & SUPPORTED_1000baseT_Half)
308 adv |= ADVERTISE_1000HALF;
309 if (advertise & SUPPORTED_1000baseT_Full)
310 adv |= ADVERTISE_1000FULL;
311 err = phy_write(phydev, MII_CTRL1000, adv);
312
313 if (err < 0)
314 return err;
315 }
316
317 return adv;
318}
e1393456 319EXPORT_SYMBOL(genphy_config_advert);
00db8189
AF
320
321/* genphy_setup_forced
322 *
323 * description: Configures MII_BMCR to force speed/duplex
324 * to the values in phydev. Assumes that the values are valid.
325 * Please see phy_sanitize_settings() */
326int genphy_setup_forced(struct phy_device *phydev)
327{
328 int ctl = BMCR_RESET;
329
330 phydev->pause = phydev->asym_pause = 0;
331
332 if (SPEED_1000 == phydev->speed)
333 ctl |= BMCR_SPEED1000;
334 else if (SPEED_100 == phydev->speed)
335 ctl |= BMCR_SPEED100;
336
337 if (DUPLEX_FULL == phydev->duplex)
338 ctl |= BMCR_FULLDPLX;
339
340 ctl = phy_write(phydev, MII_BMCR, ctl);
341
342 if (ctl < 0)
343 return ctl;
344
345 /* We just reset the device, so we'd better configure any
346 * settings the PHY requires to operate */
347 if (phydev->drv->config_init)
348 ctl = phydev->drv->config_init(phydev);
349
350 return ctl;
351}
352
353
354/* Enable and Restart Autonegotiation */
355int genphy_restart_aneg(struct phy_device *phydev)
356{
357 int ctl;
358
359 ctl = phy_read(phydev, MII_BMCR);
360
361 if (ctl < 0)
362 return ctl;
363
364 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
365
366 /* Don't isolate the PHY if we're negotiating */
367 ctl &= ~(BMCR_ISOLATE);
368
369 ctl = phy_write(phydev, MII_BMCR, ctl);
370
371 return ctl;
372}
373
374
375/* genphy_config_aneg
376 *
377 * description: If auto-negotiation is enabled, we configure the
378 * advertising, and then restart auto-negotiation. If it is not
379 * enabled, then we write the BMCR
380 */
381int genphy_config_aneg(struct phy_device *phydev)
382{
383 int err = 0;
384
385 if (AUTONEG_ENABLE == phydev->autoneg) {
386 err = genphy_config_advert(phydev);
387
388 if (err < 0)
389 return err;
390
391 err = genphy_restart_aneg(phydev);
392 } else
393 err = genphy_setup_forced(phydev);
394
395 return err;
396}
397EXPORT_SYMBOL(genphy_config_aneg);
398
399/* genphy_update_link
400 *
401 * description: Update the value in phydev->link to reflect the
402 * current link value. In order to do this, we need to read
403 * the status register twice, keeping the second value
404 */
405int genphy_update_link(struct phy_device *phydev)
406{
407 int status;
408
409 /* Do a fake read */
410 status = phy_read(phydev, MII_BMSR);
411
412 if (status < 0)
413 return status;
414
415 /* Read link and autonegotiation status */
416 status = phy_read(phydev, MII_BMSR);
417
418 if (status < 0)
419 return status;
420
421 if ((status & BMSR_LSTATUS) == 0)
422 phydev->link = 0;
423 else
424 phydev->link = 1;
425
426 return 0;
427}
428
429/* genphy_read_status
430 *
431 * description: Check the link, then figure out the current state
432 * by comparing what we advertise with what the link partner
433 * advertises. Start by checking the gigabit possibilities,
434 * then move on to 10/100.
435 */
436int genphy_read_status(struct phy_device *phydev)
437{
438 int adv;
439 int err;
440 int lpa;
441 int lpagb = 0;
442
443 /* Update the link, but return if there
444 * was an error */
445 err = genphy_update_link(phydev);
446 if (err)
447 return err;
448
449 if (AUTONEG_ENABLE == phydev->autoneg) {
450 if (phydev->supported & (SUPPORTED_1000baseT_Half
451 | SUPPORTED_1000baseT_Full)) {
452 lpagb = phy_read(phydev, MII_STAT1000);
453
454 if (lpagb < 0)
455 return lpagb;
456
457 adv = phy_read(phydev, MII_CTRL1000);
458
459 if (adv < 0)
460 return adv;
461
462 lpagb &= adv << 2;
463 }
464
465 lpa = phy_read(phydev, MII_LPA);
466
467 if (lpa < 0)
468 return lpa;
469
470 adv = phy_read(phydev, MII_ADVERTISE);
471
472 if (adv < 0)
473 return adv;
474
475 lpa &= adv;
476
477 phydev->speed = SPEED_10;
478 phydev->duplex = DUPLEX_HALF;
479 phydev->pause = phydev->asym_pause = 0;
480
481 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
482 phydev->speed = SPEED_1000;
483
484 if (lpagb & LPA_1000FULL)
485 phydev->duplex = DUPLEX_FULL;
486 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
487 phydev->speed = SPEED_100;
488
489 if (lpa & LPA_100FULL)
490 phydev->duplex = DUPLEX_FULL;
491 } else
492 if (lpa & LPA_10FULL)
493 phydev->duplex = DUPLEX_FULL;
494
495 if (phydev->duplex == DUPLEX_FULL){
496 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
497 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
498 }
499 } else {
500 int bmcr = phy_read(phydev, MII_BMCR);
501 if (bmcr < 0)
502 return bmcr;
503
504 if (bmcr & BMCR_FULLDPLX)
505 phydev->duplex = DUPLEX_FULL;
506 else
507 phydev->duplex = DUPLEX_HALF;
508
509 if (bmcr & BMCR_SPEED1000)
510 phydev->speed = SPEED_1000;
511 else if (bmcr & BMCR_SPEED100)
512 phydev->speed = SPEED_100;
513 else
514 phydev->speed = SPEED_10;
515
516 phydev->pause = phydev->asym_pause = 0;
517 }
518
519 return 0;
520}
521EXPORT_SYMBOL(genphy_read_status);
522
523static int genphy_config_init(struct phy_device *phydev)
524{
84c22d79 525 int val;
00db8189
AF
526 u32 features;
527
528 /* For now, I'll claim that the generic driver supports
529 * all possible port types */
530 features = (SUPPORTED_TP | SUPPORTED_MII
531 | SUPPORTED_AUI | SUPPORTED_FIBRE |
532 SUPPORTED_BNC);
533
534 /* Do we support autonegotiation? */
535 val = phy_read(phydev, MII_BMSR);
536
537 if (val < 0)
538 return val;
539
540 if (val & BMSR_ANEGCAPABLE)
541 features |= SUPPORTED_Autoneg;
542
543 if (val & BMSR_100FULL)
544 features |= SUPPORTED_100baseT_Full;
545 if (val & BMSR_100HALF)
546 features |= SUPPORTED_100baseT_Half;
547 if (val & BMSR_10FULL)
548 features |= SUPPORTED_10baseT_Full;
549 if (val & BMSR_10HALF)
550 features |= SUPPORTED_10baseT_Half;
551
552 if (val & BMSR_ESTATEN) {
553 val = phy_read(phydev, MII_ESTATUS);
554
555 if (val < 0)
556 return val;
557
558 if (val & ESTATUS_1000_TFULL)
559 features |= SUPPORTED_1000baseT_Full;
560 if (val & ESTATUS_1000_THALF)
561 features |= SUPPORTED_1000baseT_Half;
562 }
563
564 phydev->supported = features;
565 phydev->advertising = features;
566
567 return 0;
568}
569
570
571/* phy_probe
572 *
573 * description: Take care of setting up the phy_device structure,
574 * set the state to READY (the driver's init function should
575 * set it to STARTING if needed).
576 */
577static int phy_probe(struct device *dev)
578{
579 struct phy_device *phydev;
580 struct phy_driver *phydrv;
581 struct device_driver *drv;
582 int err = 0;
583
584 phydev = to_phy_device(dev);
585
586 /* Make sure the driver is held.
587 * XXX -- Is this correct? */
588 drv = get_driver(phydev->dev.driver);
589 phydrv = to_phy_driver(drv);
590 phydev->drv = phydrv;
591
592 /* Disable the interrupt if the PHY doesn't support it */
593 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
594 phydev->irq = PHY_POLL;
595
596 spin_lock(&phydev->lock);
597
598 /* Start out supporting everything. Eventually,
599 * a controller will attach, and may modify one
600 * or both of these values */
601 phydev->supported = phydrv->features;
602 phydev->advertising = phydrv->features;
603
604 /* Set the state to READY by default */
605 phydev->state = PHY_READY;
606
607 if (phydev->drv->probe)
608 err = phydev->drv->probe(phydev);
609
610 spin_unlock(&phydev->lock);
611
612 if (err < 0)
613 return err;
614
615 if (phydev->drv->config_init)
616 err = phydev->drv->config_init(phydev);
617
618 return err;
619}
620
621static int phy_remove(struct device *dev)
622{
623 struct phy_device *phydev;
624
625 phydev = to_phy_device(dev);
626
627 spin_lock(&phydev->lock);
628 phydev->state = PHY_DOWN;
629 spin_unlock(&phydev->lock);
630
631 if (phydev->drv->remove)
632 phydev->drv->remove(phydev);
633
634 put_driver(dev->driver);
635 phydev->drv = NULL;
636
637 return 0;
638}
639
640int phy_driver_register(struct phy_driver *new_driver)
641{
642 int retval;
643
644 memset(&new_driver->driver, 0, sizeof(new_driver->driver));
645 new_driver->driver.name = new_driver->name;
646 new_driver->driver.bus = &mdio_bus_type;
647 new_driver->driver.probe = phy_probe;
648 new_driver->driver.remove = phy_remove;
649
650 retval = driver_register(&new_driver->driver);
651
652 if (retval) {
653 printk(KERN_ERR "%s: Error %d in registering driver\n",
654 new_driver->name, retval);
655
656 return retval;
657 }
658
659 pr_info("%s: Registered new driver\n", new_driver->name);
660
661 return 0;
662}
663EXPORT_SYMBOL(phy_driver_register);
664
665void phy_driver_unregister(struct phy_driver *drv)
666{
667 driver_unregister(&drv->driver);
668}
669EXPORT_SYMBOL(phy_driver_unregister);
670
e1393456
AF
671static struct phy_driver genphy_driver = {
672 .phy_id = 0xffffffff,
673 .phy_id_mask = 0xffffffff,
674 .name = "Generic PHY",
675 .config_init = genphy_config_init,
676 .features = 0,
677 .config_aneg = genphy_config_aneg,
678 .read_status = genphy_read_status,
679 .driver = {.owner= THIS_MODULE, },
680};
00db8189 681
67c4f3fa 682static int __init phy_init(void)
00db8189 683{
67c4f3fa 684 int rc;
67c4f3fa
JG
685
686 rc = mdio_bus_init();
687 if (rc)
e1393456 688 return rc;
00db8189 689
e1393456
AF
690 rc = phy_driver_register(&genphy_driver);
691 if (rc)
692 mdio_bus_exit();
67c4f3fa 693
67c4f3fa 694 return rc;
00db8189
AF
695}
696
67c4f3fa 697static void __exit phy_exit(void)
00db8189
AF
698{
699 phy_driver_unregister(&genphy_driver);
e1393456 700 mdio_bus_exit();
00db8189
AF
701}
702
e1393456 703subsys_initcall(phy_init);
67c4f3fa 704module_exit(phy_exit);
This page took 0.195705 seconds and 5 git commands to generate.