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