cxgb3: extend copyrights to 2008
[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 17#include <linux/kernel.h>
00db8189
AF
18#include <linux/string.h>
19#include <linux/errno.h>
20#include <linux/unistd.h>
21#include <linux/slab.h>
22#include <linux/interrupt.h>
23#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/netdevice.h>
26#include <linux/etherdevice.h>
27#include <linux/skbuff.h>
00db8189
AF
28#include <linux/mm.h>
29#include <linux/module.h>
00db8189
AF
30#include <linux/mii.h>
31#include <linux/ethtool.h>
32#include <linux/phy.h>
33
34#include <asm/io.h>
35#include <asm/irq.h>
36#include <asm/uaccess.h>
37
afcceaa3
OH
38MODULE_DESCRIPTION("PHY library");
39MODULE_AUTHOR("Andy Fleming");
40MODULE_LICENSE("GPL");
41
e1393456
AF
42static struct phy_driver genphy_driver;
43extern int mdio_bus_init(void);
44extern void mdio_bus_exit(void);
67c4f3fa 45
6f4a7f41
AV
46void phy_device_free(struct phy_device *phydev)
47{
48 kfree(phydev);
49}
50
51static void phy_device_release(struct device *dev)
52{
53 phy_device_free(to_phy_device(dev));
54}
55
f62220d3
AF
56static LIST_HEAD(phy_fixup_list);
57static DEFINE_MUTEX(phy_fixup_lock);
58
59/*
60 * Creates a new phy_fixup and adds it to the list
61 * @bus_id: A string which matches phydev->dev.bus_id (or PHY_ANY_ID)
62 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
63 * It can also be PHY_ANY_UID
64 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
65 * comparison
66 * @run: The actual code to be run when a matching PHY is found
67 */
68int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
69 int (*run)(struct phy_device *))
70{
71 struct phy_fixup *fixup;
72
73 fixup = kzalloc(sizeof(struct phy_fixup), GFP_KERNEL);
74 if (!fixup)
75 return -ENOMEM;
76
77 strncpy(fixup->bus_id, bus_id, BUS_ID_SIZE);
78 fixup->phy_uid = phy_uid;
79 fixup->phy_uid_mask = phy_uid_mask;
80 fixup->run = run;
81
82 mutex_lock(&phy_fixup_lock);
83 list_add_tail(&fixup->list, &phy_fixup_list);
84 mutex_unlock(&phy_fixup_lock);
85
86 return 0;
87}
88EXPORT_SYMBOL(phy_register_fixup);
89
90/* Registers a fixup to be run on any PHY with the UID in phy_uid */
91int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
92 int (*run)(struct phy_device *))
93{
94 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
95}
96EXPORT_SYMBOL(phy_register_fixup_for_uid);
97
98/* Registers a fixup to be run on the PHY with id string bus_id */
99int phy_register_fixup_for_id(const char *bus_id,
100 int (*run)(struct phy_device *))
101{
102 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
103}
104EXPORT_SYMBOL(phy_register_fixup_for_id);
105
106/*
107 * Returns 1 if fixup matches phydev in bus_id and phy_uid.
108 * Fixups can be set to match any in one or more fields.
109 */
110static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
111{
112 if (strcmp(fixup->bus_id, phydev->dev.bus_id) != 0)
113 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
114 return 0;
115
116 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
117 (phydev->phy_id & fixup->phy_uid_mask))
118 if (fixup->phy_uid != PHY_ANY_UID)
119 return 0;
120
121 return 1;
122}
123
124/* Runs any matching fixups for this phydev */
125int phy_scan_fixups(struct phy_device *phydev)
126{
127 struct phy_fixup *fixup;
128
129 mutex_lock(&phy_fixup_lock);
130 list_for_each_entry(fixup, &phy_fixup_list, list) {
131 if (phy_needs_fixup(phydev, fixup)) {
132 int err;
133
134 err = fixup->run(phydev);
135
136 if (err < 0)
137 return err;
138 }
139 }
140 mutex_unlock(&phy_fixup_lock);
141
142 return 0;
143}
144EXPORT_SYMBOL(phy_scan_fixups);
145
11b0bacd
VB
146struct phy_device* phy_device_create(struct mii_bus *bus, int addr, int phy_id)
147{
148 struct phy_device *dev;
149 /* We allocate the device, and initialize the
150 * default values */
cd861280 151 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
11b0bacd
VB
152
153 if (NULL == dev)
154 return (struct phy_device*) PTR_ERR((void*)-ENOMEM);
155
6f4a7f41
AV
156 dev->dev.release = phy_device_release;
157
11b0bacd
VB
158 dev->speed = 0;
159 dev->duplex = -1;
160 dev->pause = dev->asym_pause = 0;
161 dev->link = 1;
e8a2b6a4 162 dev->interface = PHY_INTERFACE_MODE_GMII;
11b0bacd
VB
163
164 dev->autoneg = AUTONEG_ENABLE;
165
166 dev->addr = addr;
167 dev->phy_id = phy_id;
168 dev->bus = bus;
169
170 dev->state = PHY_DOWN;
171
35b5f6b1 172 mutex_init(&dev->lock);
11b0bacd
VB
173
174 return dev;
175}
176EXPORT_SYMBOL(phy_device_create);
177
b3df0da8 178/**
cac1f3c8 179 * get_phy_id - reads the specified addr for its ID.
b3df0da8
RD
180 * @bus: the target MII bus
181 * @addr: PHY address on the MII bus
cac1f3c8 182 * @phy_id: where to store the ID retrieved.
00db8189 183 *
b3df0da8 184 * Description: Reads the ID registers of the PHY at @addr on the
cac1f3c8 185 * @bus, stores it in @phy_id and returns zero on success.
00db8189 186 */
cac1f3c8 187int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id)
00db8189
AF
188{
189 int phy_reg;
00db8189
AF
190
191 /* Grab the bits from PHYIR1, and put them
192 * in the upper half */
193 phy_reg = bus->read(bus, addr, MII_PHYSID1);
194
195 if (phy_reg < 0)
cac1f3c8 196 return -EIO;
00db8189 197
cac1f3c8 198 *phy_id = (phy_reg & 0xffff) << 16;
00db8189
AF
199
200 /* Grab the bits from PHYIR2, and put them in the lower half */
201 phy_reg = bus->read(bus, addr, MII_PHYSID2);
202
203 if (phy_reg < 0)
cac1f3c8
PG
204 return -EIO;
205
206 *phy_id |= (phy_reg & 0xffff);
207
208 return 0;
209}
a01b3d76 210EXPORT_SYMBOL(get_phy_id);
cac1f3c8
PG
211
212/**
213 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
214 * @bus: the target MII bus
215 * @addr: PHY address on the MII bus
216 *
217 * Description: Reads the ID registers of the PHY at @addr on the
218 * @bus, then allocates and returns the phy_device to represent it.
219 */
220struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
221{
222 struct phy_device *dev = NULL;
223 u32 phy_id;
224 int r;
00db8189 225
cac1f3c8
PG
226 r = get_phy_id(bus, addr, &phy_id);
227 if (r)
228 return ERR_PTR(r);
00db8189
AF
229
230 /* If the phy_id is all Fs, there is no device there */
231 if (0xffffffff == phy_id)
232 return NULL;
233
11b0bacd 234 dev = phy_device_create(bus, addr, phy_id);
00db8189
AF
235
236 return dev;
237}
238
b3df0da8
RD
239/**
240 * phy_prepare_link - prepares the PHY layer to monitor link status
241 * @phydev: target phy_device struct
242 * @handler: callback function for link status change notifications
00db8189 243 *
b3df0da8 244 * Description: Tells the PHY infrastructure to handle the
00db8189
AF
245 * gory details on monitoring link status (whether through
246 * polling or an interrupt), and to call back to the
247 * connected device driver when the link status changes.
248 * If you want to monitor your own link state, don't call
b3df0da8
RD
249 * this function.
250 */
00db8189
AF
251void phy_prepare_link(struct phy_device *phydev,
252 void (*handler)(struct net_device *))
253{
254 phydev->adjust_link = handler;
255}
256
b3df0da8
RD
257/**
258 * phy_connect - connect an ethernet device to a PHY device
259 * @dev: the network device to connect
5d12b132 260 * @bus_id: the id string of the PHY device to connect
b3df0da8
RD
261 * @handler: callback function for state change notifications
262 * @flags: PHY device's dev_flags
263 * @interface: PHY device's interface
e1393456 264 *
b3df0da8 265 * Description: Convenience function for connecting ethernet
e1393456
AF
266 * devices to PHY devices. The default behavior is for
267 * the PHY infrastructure to handle everything, and only notify
268 * the connected driver when the link status changes. If you
269 * don't want, or can't use the provided functionality, you may
270 * choose to call only the subset of functions which provide
271 * the desired functionality.
272 */
f62220d3 273struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
e8a2b6a4 274 void (*handler)(struct net_device *), u32 flags,
1a168934 275 phy_interface_t interface)
e1393456
AF
276{
277 struct phy_device *phydev;
278
f62220d3 279 phydev = phy_attach(dev, bus_id, flags, interface);
e1393456
AF
280
281 if (IS_ERR(phydev))
282 return phydev;
283
284 phy_prepare_link(phydev, handler);
285
286 phy_start_machine(phydev, NULL);
287
288 if (phydev->irq > 0)
289 phy_start_interrupts(phydev);
290
291 return phydev;
292}
293EXPORT_SYMBOL(phy_connect);
294
b3df0da8
RD
295/**
296 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device
297 * @phydev: target phy_device struct
298 */
e1393456
AF
299void phy_disconnect(struct phy_device *phydev)
300{
301 if (phydev->irq > 0)
302 phy_stop_interrupts(phydev);
303
304 phy_stop_machine(phydev);
305
306 phydev->adjust_link = NULL;
307
308 phy_detach(phydev);
309}
310EXPORT_SYMBOL(phy_disconnect);
311
b3df0da8
RD
312static int phy_compare_id(struct device *dev, void *data)
313{
314 return strcmp((char *)data, dev->bus_id) ? 0 : 1;
315}
316
317/**
318 * phy_attach - attach a network device to a particular PHY device
319 * @dev: network device to attach
f62220d3 320 * @bus_id: PHY device to attach
b3df0da8
RD
321 * @flags: PHY device's dev_flags
322 * @interface: PHY device's interface
e1393456 323 *
b3df0da8 324 * Description: Called by drivers to attach to a particular PHY
e1393456
AF
325 * device. The phy_device is found, and properly hooked up
326 * to the phy_driver. If no driver is attached, then the
327 * genphy_driver is used. The phy_device is given a ptr to
328 * the attaching device, and given a callback for link status
b3df0da8 329 * change. The phy_device is returned to the attaching driver.
e1393456 330 */
e1393456 331struct phy_device *phy_attach(struct net_device *dev,
f62220d3 332 const char *bus_id, u32 flags, phy_interface_t interface)
e1393456
AF
333{
334 struct bus_type *bus = &mdio_bus_type;
335 struct phy_device *phydev;
336 struct device *d;
337
338 /* Search the list of PHY devices on the mdio bus for the
339 * PHY with the requested name */
f62220d3 340 d = bus_find_device(bus, NULL, (void *)bus_id, phy_compare_id);
e1393456
AF
341
342 if (d) {
343 phydev = to_phy_device(d);
344 } else {
f62220d3 345 printk(KERN_ERR "%s not found\n", bus_id);
e1393456
AF
346 return ERR_PTR(-ENODEV);
347 }
348
349 /* Assume that if there is no driver, that it doesn't
350 * exist, and we should use the genphy driver. */
351 if (NULL == d->driver) {
352 int err;
e1393456
AF
353 d->driver = &genphy_driver.driver;
354
355 err = d->driver->probe(d);
b7a00ecd
JG
356 if (err >= 0)
357 err = device_bind_driver(d);
e1393456 358
b7a00ecd
JG
359 if (err)
360 return ERR_PTR(err);
e1393456
AF
361 }
362
363 if (phydev->attached_dev) {
364 printk(KERN_ERR "%s: %s already attached\n",
f62220d3 365 dev->name, bus_id);
e1393456
AF
366 return ERR_PTR(-EBUSY);
367 }
368
369 phydev->attached_dev = dev;
370
371 phydev->dev_flags = flags;
372
e8a2b6a4
AF
373 phydev->interface = interface;
374
375 /* Do initial configuration here, now that
376 * we have certain key parameters
377 * (dev_flags and interface) */
378 if (phydev->drv->config_init) {
379 int err;
380
f62220d3
AF
381 err = phy_scan_fixups(phydev);
382
383 if (err < 0)
384 return ERR_PTR(err);
385
e8a2b6a4
AF
386 err = phydev->drv->config_init(phydev);
387
388 if (err < 0)
389 return ERR_PTR(err);
390 }
391
e1393456
AF
392 return phydev;
393}
394EXPORT_SYMBOL(phy_attach);
395
b3df0da8
RD
396/**
397 * phy_detach - detach a PHY device from its network device
398 * @phydev: target phy_device struct
399 */
e1393456
AF
400void phy_detach(struct phy_device *phydev)
401{
402 phydev->attached_dev = NULL;
403
404 /* If the device had no specific driver before (i.e. - it
405 * was using the generic driver), we unbind the device
406 * from the generic driver so that there's a chance a
407 * real driver could be loaded */
87aebe07 408 if (phydev->dev.driver == &genphy_driver.driver)
e1393456 409 device_release_driver(&phydev->dev);
e1393456
AF
410}
411EXPORT_SYMBOL(phy_detach);
412
413
00db8189
AF
414/* Generic PHY support and helper functions */
415
b3df0da8
RD
416/**
417 * genphy_config_advert - sanitize and advertise auto-negotation parameters
418 * @phydev: target phy_device struct
00db8189 419 *
b3df0da8 420 * Description: Writes MII_ADVERTISE with the appropriate values,
00db8189 421 * after sanitizing the values to make sure we only advertise
51e2a384
TP
422 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
423 * hasn't changed, and > 0 if it has changed.
00db8189 424 */
e1393456 425int genphy_config_advert(struct phy_device *phydev)
00db8189
AF
426{
427 u32 advertise;
51e2a384
TP
428 int oldadv, adv;
429 int err, changed = 0;
00db8189
AF
430
431 /* Only allow advertising what
432 * this PHY supports */
433 phydev->advertising &= phydev->supported;
434 advertise = phydev->advertising;
435
436 /* Setup standard advertisement */
51e2a384 437 oldadv = adv = phy_read(phydev, MII_ADVERTISE);
00db8189
AF
438
439 if (adv < 0)
440 return adv;
441
442 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
443 ADVERTISE_PAUSE_ASYM);
444 if (advertise & ADVERTISED_10baseT_Half)
445 adv |= ADVERTISE_10HALF;
446 if (advertise & ADVERTISED_10baseT_Full)
447 adv |= ADVERTISE_10FULL;
448 if (advertise & ADVERTISED_100baseT_Half)
449 adv |= ADVERTISE_100HALF;
450 if (advertise & ADVERTISED_100baseT_Full)
451 adv |= ADVERTISE_100FULL;
452 if (advertise & ADVERTISED_Pause)
453 adv |= ADVERTISE_PAUSE_CAP;
454 if (advertise & ADVERTISED_Asym_Pause)
455 adv |= ADVERTISE_PAUSE_ASYM;
456
51e2a384
TP
457 if (adv != oldadv) {
458 err = phy_write(phydev, MII_ADVERTISE, adv);
00db8189 459
51e2a384
TP
460 if (err < 0)
461 return err;
462 changed = 1;
463 }
00db8189
AF
464
465 /* Configure gigabit if it's supported */
466 if (phydev->supported & (SUPPORTED_1000baseT_Half |
467 SUPPORTED_1000baseT_Full)) {
51e2a384 468 oldadv = adv = phy_read(phydev, MII_CTRL1000);
00db8189
AF
469
470 if (adv < 0)
471 return adv;
472
473 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
474 if (advertise & SUPPORTED_1000baseT_Half)
475 adv |= ADVERTISE_1000HALF;
476 if (advertise & SUPPORTED_1000baseT_Full)
477 adv |= ADVERTISE_1000FULL;
00db8189 478
51e2a384
TP
479 if (adv != oldadv) {
480 err = phy_write(phydev, MII_CTRL1000, adv);
481
482 if (err < 0)
483 return err;
484 changed = 1;
485 }
00db8189
AF
486 }
487
51e2a384 488 return changed;
00db8189 489}
e1393456 490EXPORT_SYMBOL(genphy_config_advert);
00db8189 491
b3df0da8
RD
492/**
493 * genphy_setup_forced - configures/forces speed/duplex from @phydev
494 * @phydev: target phy_device struct
00db8189 495 *
b3df0da8 496 * Description: Configures MII_BMCR to force speed/duplex
00db8189 497 * to the values in phydev. Assumes that the values are valid.
b3df0da8
RD
498 * Please see phy_sanitize_settings().
499 */
00db8189
AF
500int genphy_setup_forced(struct phy_device *phydev)
501{
f62220d3 502 int err;
bc1e0a09 503 int ctl = 0;
00db8189
AF
504
505 phydev->pause = phydev->asym_pause = 0;
506
507 if (SPEED_1000 == phydev->speed)
508 ctl |= BMCR_SPEED1000;
509 else if (SPEED_100 == phydev->speed)
510 ctl |= BMCR_SPEED100;
511
512 if (DUPLEX_FULL == phydev->duplex)
513 ctl |= BMCR_FULLDPLX;
514
f62220d3 515 err = phy_write(phydev, MII_BMCR, ctl);
00db8189 516
f62220d3
AF
517 if (err < 0)
518 return err;
519
520 /*
521 * Run the fixups on this PHY, just in case the
522 * board code needs to change something after a reset
523 */
524 err = phy_scan_fixups(phydev);
525
526 if (err < 0)
527 return err;
00db8189
AF
528
529 /* We just reset the device, so we'd better configure any
530 * settings the PHY requires to operate */
531 if (phydev->drv->config_init)
f62220d3 532 err = phydev->drv->config_init(phydev);
00db8189 533
f62220d3 534 return err;
00db8189
AF
535}
536
537
b3df0da8
RD
538/**
539 * genphy_restart_aneg - Enable and Restart Autonegotiation
540 * @phydev: target phy_device struct
541 */
00db8189
AF
542int genphy_restart_aneg(struct phy_device *phydev)
543{
544 int ctl;
545
546 ctl = phy_read(phydev, MII_BMCR);
547
548 if (ctl < 0)
549 return ctl;
550
551 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
552
553 /* Don't isolate the PHY if we're negotiating */
554 ctl &= ~(BMCR_ISOLATE);
555
556 ctl = phy_write(phydev, MII_BMCR, ctl);
557
558 return ctl;
559}
560
561
b3df0da8
RD
562/**
563 * genphy_config_aneg - restart auto-negotiation or write BMCR
564 * @phydev: target phy_device struct
00db8189 565 *
b3df0da8 566 * Description: If auto-negotiation is enabled, we configure the
00db8189 567 * advertising, and then restart auto-negotiation. If it is not
b3df0da8 568 * enabled, then we write the BMCR.
00db8189
AF
569 */
570int genphy_config_aneg(struct phy_device *phydev)
571{
51e2a384 572 int result = 0;
00db8189
AF
573
574 if (AUTONEG_ENABLE == phydev->autoneg) {
51e2a384 575 int result = genphy_config_advert(phydev);
00db8189 576
51e2a384
TP
577 if (result < 0) /* error */
578 return result;
00db8189 579
51e2a384
TP
580 /* Only restart aneg if we are advertising something different
581 * than we were before. */
582 if (result > 0)
583 result = genphy_restart_aneg(phydev);
00db8189 584 } else
51e2a384 585 result = genphy_setup_forced(phydev);
00db8189 586
51e2a384 587 return result;
00db8189
AF
588}
589EXPORT_SYMBOL(genphy_config_aneg);
590
b3df0da8
RD
591/**
592 * genphy_update_link - update link status in @phydev
593 * @phydev: target phy_device struct
00db8189 594 *
b3df0da8 595 * Description: Update the value in phydev->link to reflect the
00db8189 596 * current link value. In order to do this, we need to read
b3df0da8 597 * the status register twice, keeping the second value.
00db8189
AF
598 */
599int genphy_update_link(struct phy_device *phydev)
600{
601 int status;
602
603 /* Do a fake read */
604 status = phy_read(phydev, MII_BMSR);
605
606 if (status < 0)
607 return status;
608
609 /* Read link and autonegotiation status */
610 status = phy_read(phydev, MII_BMSR);
611
612 if (status < 0)
613 return status;
614
615 if ((status & BMSR_LSTATUS) == 0)
616 phydev->link = 0;
617 else
618 phydev->link = 1;
619
620 return 0;
621}
6b655529 622EXPORT_SYMBOL(genphy_update_link);
00db8189 623
b3df0da8
RD
624/**
625 * genphy_read_status - check the link status and update current link state
626 * @phydev: target phy_device struct
00db8189 627 *
b3df0da8 628 * Description: Check the link, then figure out the current state
00db8189
AF
629 * by comparing what we advertise with what the link partner
630 * advertises. Start by checking the gigabit possibilities,
631 * then move on to 10/100.
632 */
633int genphy_read_status(struct phy_device *phydev)
634{
635 int adv;
636 int err;
637 int lpa;
638 int lpagb = 0;
639
640 /* Update the link, but return if there
641 * was an error */
642 err = genphy_update_link(phydev);
643 if (err)
644 return err;
645
646 if (AUTONEG_ENABLE == phydev->autoneg) {
647 if (phydev->supported & (SUPPORTED_1000baseT_Half
648 | SUPPORTED_1000baseT_Full)) {
649 lpagb = phy_read(phydev, MII_STAT1000);
650
651 if (lpagb < 0)
652 return lpagb;
653
654 adv = phy_read(phydev, MII_CTRL1000);
655
656 if (adv < 0)
657 return adv;
658
659 lpagb &= adv << 2;
660 }
661
662 lpa = phy_read(phydev, MII_LPA);
663
664 if (lpa < 0)
665 return lpa;
666
667 adv = phy_read(phydev, MII_ADVERTISE);
668
669 if (adv < 0)
670 return adv;
671
672 lpa &= adv;
673
674 phydev->speed = SPEED_10;
675 phydev->duplex = DUPLEX_HALF;
676 phydev->pause = phydev->asym_pause = 0;
677
678 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
679 phydev->speed = SPEED_1000;
680
681 if (lpagb & LPA_1000FULL)
682 phydev->duplex = DUPLEX_FULL;
683 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
684 phydev->speed = SPEED_100;
685
686 if (lpa & LPA_100FULL)
687 phydev->duplex = DUPLEX_FULL;
688 } else
689 if (lpa & LPA_10FULL)
690 phydev->duplex = DUPLEX_FULL;
691
692 if (phydev->duplex == DUPLEX_FULL){
693 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
694 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
695 }
696 } else {
697 int bmcr = phy_read(phydev, MII_BMCR);
698 if (bmcr < 0)
699 return bmcr;
700
701 if (bmcr & BMCR_FULLDPLX)
702 phydev->duplex = DUPLEX_FULL;
703 else
704 phydev->duplex = DUPLEX_HALF;
705
706 if (bmcr & BMCR_SPEED1000)
707 phydev->speed = SPEED_1000;
708 else if (bmcr & BMCR_SPEED100)
709 phydev->speed = SPEED_100;
710 else
711 phydev->speed = SPEED_10;
712
713 phydev->pause = phydev->asym_pause = 0;
714 }
715
716 return 0;
717}
718EXPORT_SYMBOL(genphy_read_status);
719
720static int genphy_config_init(struct phy_device *phydev)
721{
84c22d79 722 int val;
00db8189
AF
723 u32 features;
724
725 /* For now, I'll claim that the generic driver supports
726 * all possible port types */
727 features = (SUPPORTED_TP | SUPPORTED_MII
728 | SUPPORTED_AUI | SUPPORTED_FIBRE |
729 SUPPORTED_BNC);
730
731 /* Do we support autonegotiation? */
732 val = phy_read(phydev, MII_BMSR);
733
734 if (val < 0)
735 return val;
736
737 if (val & BMSR_ANEGCAPABLE)
738 features |= SUPPORTED_Autoneg;
739
740 if (val & BMSR_100FULL)
741 features |= SUPPORTED_100baseT_Full;
742 if (val & BMSR_100HALF)
743 features |= SUPPORTED_100baseT_Half;
744 if (val & BMSR_10FULL)
745 features |= SUPPORTED_10baseT_Full;
746 if (val & BMSR_10HALF)
747 features |= SUPPORTED_10baseT_Half;
748
749 if (val & BMSR_ESTATEN) {
750 val = phy_read(phydev, MII_ESTATUS);
751
752 if (val < 0)
753 return val;
754
755 if (val & ESTATUS_1000_TFULL)
756 features |= SUPPORTED_1000baseT_Full;
757 if (val & ESTATUS_1000_THALF)
758 features |= SUPPORTED_1000baseT_Half;
759 }
760
761 phydev->supported = features;
762 phydev->advertising = features;
763
764 return 0;
765}
766
767
b3df0da8
RD
768/**
769 * phy_probe - probe and init a PHY device
770 * @dev: device to probe and init
00db8189 771 *
b3df0da8 772 * Description: Take care of setting up the phy_device structure,
00db8189
AF
773 * set the state to READY (the driver's init function should
774 * set it to STARTING if needed).
775 */
776static int phy_probe(struct device *dev)
777{
778 struct phy_device *phydev;
779 struct phy_driver *phydrv;
780 struct device_driver *drv;
781 int err = 0;
782
783 phydev = to_phy_device(dev);
784
785 /* Make sure the driver is held.
786 * XXX -- Is this correct? */
787 drv = get_driver(phydev->dev.driver);
788 phydrv = to_phy_driver(drv);
789 phydev->drv = phydrv;
790
791 /* Disable the interrupt if the PHY doesn't support it */
792 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
793 phydev->irq = PHY_POLL;
794
35b5f6b1 795 mutex_lock(&phydev->lock);
00db8189
AF
796
797 /* Start out supporting everything. Eventually,
798 * a controller will attach, and may modify one
799 * or both of these values */
800 phydev->supported = phydrv->features;
801 phydev->advertising = phydrv->features;
802
803 /* Set the state to READY by default */
804 phydev->state = PHY_READY;
805
806 if (phydev->drv->probe)
807 err = phydev->drv->probe(phydev);
808
35b5f6b1 809 mutex_unlock(&phydev->lock);
00db8189 810
00db8189 811 return err;
e8a2b6a4 812
00db8189
AF
813}
814
815static int phy_remove(struct device *dev)
816{
817 struct phy_device *phydev;
818
819 phydev = to_phy_device(dev);
820
35b5f6b1 821 mutex_lock(&phydev->lock);
00db8189 822 phydev->state = PHY_DOWN;
35b5f6b1 823 mutex_unlock(&phydev->lock);
00db8189
AF
824
825 if (phydev->drv->remove)
826 phydev->drv->remove(phydev);
827
828 put_driver(dev->driver);
829 phydev->drv = NULL;
830
831 return 0;
832}
833
b3df0da8
RD
834/**
835 * phy_driver_register - register a phy_driver with the PHY layer
836 * @new_driver: new phy_driver to register
837 */
00db8189
AF
838int phy_driver_register(struct phy_driver *new_driver)
839{
840 int retval;
841
842 memset(&new_driver->driver, 0, sizeof(new_driver->driver));
843 new_driver->driver.name = new_driver->name;
844 new_driver->driver.bus = &mdio_bus_type;
845 new_driver->driver.probe = phy_probe;
846 new_driver->driver.remove = phy_remove;
847
848 retval = driver_register(&new_driver->driver);
849
850 if (retval) {
851 printk(KERN_ERR "%s: Error %d in registering driver\n",
852 new_driver->name, retval);
853
854 return retval;
855 }
856
f2511f13 857 pr_debug("%s: Registered new driver\n", new_driver->name);
00db8189
AF
858
859 return 0;
860}
861EXPORT_SYMBOL(phy_driver_register);
862
863void phy_driver_unregister(struct phy_driver *drv)
864{
865 driver_unregister(&drv->driver);
866}
867EXPORT_SYMBOL(phy_driver_unregister);
868
e1393456
AF
869static struct phy_driver genphy_driver = {
870 .phy_id = 0xffffffff,
871 .phy_id_mask = 0xffffffff,
872 .name = "Generic PHY",
873 .config_init = genphy_config_init,
874 .features = 0,
875 .config_aneg = genphy_config_aneg,
876 .read_status = genphy_read_status,
877 .driver = {.owner= THIS_MODULE, },
878};
00db8189 879
67c4f3fa 880static int __init phy_init(void)
00db8189 881{
67c4f3fa 882 int rc;
67c4f3fa
JG
883
884 rc = mdio_bus_init();
885 if (rc)
e1393456 886 return rc;
00db8189 887
e1393456
AF
888 rc = phy_driver_register(&genphy_driver);
889 if (rc)
890 mdio_bus_exit();
67c4f3fa 891
67c4f3fa 892 return rc;
00db8189
AF
893}
894
67c4f3fa 895static void __exit phy_exit(void)
00db8189
AF
896{
897 phy_driver_unregister(&genphy_driver);
e1393456 898 mdio_bus_exit();
00db8189
AF
899}
900
e1393456 901subsys_initcall(phy_init);
67c4f3fa 902module_exit(phy_exit);
This page took 0.460017 seconds and 5 git commands to generate.