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