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