blob: 4cc75a290c06ffb8d0241ceec03fe55d93842212 [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 */
Andy Fleming00db8182005-07-30 19:31:23 -040017#include <linux/kernel.h>
Andy Fleming00db8182005-07-30 19:31:23 -040018#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>
Andy Fleming00db8182005-07-30 19:31:23 -040028#include <linux/mm.h>
29#include <linux/module.h>
Andy Fleming00db8182005-07-30 19:31:23 -040030#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
Olaf Heringafcceaa2005-12-14 00:33:49 +010038MODULE_DESCRIPTION("PHY library");
39MODULE_AUTHOR("Andy Fleming");
40MODULE_LICENSE("GPL");
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
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030046void 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
Andy Flemingf62220d2008-04-18 17:29:54 -050056static 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
Kay Sieversfb28ad32008-11-10 13:55:14 -080077 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
Andy Flemingf62220d2008-04-18 17:29:54 -050078 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{
Kay Sieversfb28ad32008-11-10 13:55:14 -0800112 if (strcmp(fixup->bus_id, dev_name(&phydev->dev)) != 0)
Andy Flemingf62220d2008-04-18 17:29:54 -0500113 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
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700146struct 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 */
Robert P. J. Daycd861282006-12-13 00:34:52 -0800151 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700152
153 if (NULL == dev)
154 return (struct phy_device*) PTR_ERR((void*)-ENOMEM);
155
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300156 dev->dev.release = phy_device_release;
157
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700158 dev->speed = 0;
159 dev->duplex = -1;
160 dev->pause = dev->asym_pause = 0;
161 dev->link = 1;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600162 dev->interface = PHY_INTERFACE_MODE_GMII;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700163
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
Nate Case35b5f6b2008-01-29 10:05:09 -0600172 mutex_init(&dev->lock);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700173
174 return dev;
175}
176EXPORT_SYMBOL(phy_device_create);
177
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800178/**
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400179 * get_phy_id - reads the specified addr for its ID.
180 * @bus: the target MII bus
181 * @addr: PHY address on the MII bus
182 * @phy_id: where to store the ID retrieved.
183 *
184 * Description: Reads the ID registers of the PHY at @addr on the
185 * @bus, stores it in @phy_id and returns zero on success.
186 */
187int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id)
188{
189 int phy_reg;
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)
196 return -EIO;
197
198 *phy_id = (phy_reg & 0xffff) << 16;
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)
204 return -EIO;
205
206 *phy_id |= (phy_reg & 0xffff);
207
208 return 0;
209}
Paul Gortmakera01b3d72008-05-22 12:43:50 -0400210EXPORT_SYMBOL(get_phy_id);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400211
212/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800213 * 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
Andy Fleming00db8182005-07-30 19:31:23 -0400216 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800217 * Description: Reads the ID registers of the PHY at @addr on the
218 * @bus, then allocates and returns the phy_device to represent it.
Andy Fleming00db8182005-07-30 19:31:23 -0400219 */
220struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
221{
Andy Fleming00db8182005-07-30 19:31:23 -0400222 struct phy_device *dev = NULL;
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400223 u32 phy_id;
224 int r;
Andy Fleming00db8182005-07-30 19:31:23 -0400225
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400226 r = get_phy_id(bus, addr, &phy_id);
227 if (r)
228 return ERR_PTR(r);
Andy Fleming00db8182005-07-30 19:31:23 -0400229
Giuseppe Cavallaro6436cbc2008-11-20 20:43:18 -0800230 /* If the phy_id is mostly Fs, there is no device there */
231 if ((phy_id & 0x1fffffff) == 0x1fffffff)
232 return NULL;
233
234 /*
235 * Broken hardware is sometimes missing the pull down resistor on the
236 * MDIO line, which results in reads to non-existent devices returning
237 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
238 * device as well.
239 */
240 if (phy_id == 0)
Andy Fleming00db8182005-07-30 19:31:23 -0400241 return NULL;
242
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700243 dev = phy_device_create(bus, addr, phy_id);
Andy Fleming00db8182005-07-30 19:31:23 -0400244
245 return dev;
246}
247
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800248/**
249 * phy_prepare_link - prepares the PHY layer to monitor link status
250 * @phydev: target phy_device struct
251 * @handler: callback function for link status change notifications
Andy Fleming00db8182005-07-30 19:31:23 -0400252 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800253 * Description: Tells the PHY infrastructure to handle the
Andy Fleming00db8182005-07-30 19:31:23 -0400254 * gory details on monitoring link status (whether through
255 * polling or an interrupt), and to call back to the
256 * connected device driver when the link status changes.
257 * If you want to monitor your own link state, don't call
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800258 * this function.
259 */
Andy Fleming00db8182005-07-30 19:31:23 -0400260void phy_prepare_link(struct phy_device *phydev,
261 void (*handler)(struct net_device *))
262{
263 phydev->adjust_link = handler;
264}
265
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800266/**
267 * phy_connect - connect an ethernet device to a PHY device
268 * @dev: the network device to connect
Randy Dunlap5d12b132008-04-28 10:58:22 -0700269 * @bus_id: the id string of the PHY device to connect
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800270 * @handler: callback function for state change notifications
271 * @flags: PHY device's dev_flags
272 * @interface: PHY device's interface
Andy Fleminge1393452005-08-24 18:46:21 -0500273 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800274 * Description: Convenience function for connecting ethernet
Andy Fleminge1393452005-08-24 18:46:21 -0500275 * devices to PHY devices. The default behavior is for
276 * the PHY infrastructure to handle everything, and only notify
277 * the connected driver when the link status changes. If you
278 * don't want, or can't use the provided functionality, you may
279 * choose to call only the subset of functions which provide
280 * the desired functionality.
281 */
Andy Flemingf62220d2008-04-18 17:29:54 -0500282struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600283 void (*handler)(struct net_device *), u32 flags,
Randy Dunlap1a168932007-02-05 10:44:20 -0800284 phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500285{
286 struct phy_device *phydev;
287
Andy Flemingf62220d2008-04-18 17:29:54 -0500288 phydev = phy_attach(dev, bus_id, flags, interface);
Andy Fleminge1393452005-08-24 18:46:21 -0500289
290 if (IS_ERR(phydev))
291 return phydev;
292
293 phy_prepare_link(phydev, handler);
294
295 phy_start_machine(phydev, NULL);
296
297 if (phydev->irq > 0)
298 phy_start_interrupts(phydev);
299
300 return phydev;
301}
302EXPORT_SYMBOL(phy_connect);
303
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800304/**
305 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device
306 * @phydev: target phy_device struct
307 */
Andy Fleminge1393452005-08-24 18:46:21 -0500308void phy_disconnect(struct phy_device *phydev)
309{
310 if (phydev->irq > 0)
311 phy_stop_interrupts(phydev);
312
313 phy_stop_machine(phydev);
314
315 phydev->adjust_link = NULL;
316
317 phy_detach(phydev);
318}
319EXPORT_SYMBOL(phy_disconnect);
320
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800321/**
322 * phy_attach - attach a network device to a particular PHY device
323 * @dev: network device to attach
Andy Flemingf62220d2008-04-18 17:29:54 -0500324 * @bus_id: PHY device to attach
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800325 * @flags: PHY device's dev_flags
326 * @interface: PHY device's interface
327 *
328 * Description: Called by drivers to attach to a particular PHY
329 * device. The phy_device is found, and properly hooked up
330 * to the phy_driver. If no driver is attached, then the
331 * genphy_driver is used. The phy_device is given a ptr to
332 * the attaching device, and given a callback for link status
333 * change. The phy_device is returned to the attaching driver.
334 */
Andy Fleminge1393452005-08-24 18:46:21 -0500335struct phy_device *phy_attach(struct net_device *dev,
Andy Flemingf62220d2008-04-18 17:29:54 -0500336 const char *bus_id, u32 flags, phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500337{
338 struct bus_type *bus = &mdio_bus_type;
339 struct phy_device *phydev;
340 struct device *d;
341
342 /* Search the list of PHY devices on the mdio bus for the
343 * PHY with the requested name */
Greg Kroah-Hartman26853ab2008-01-27 10:29:20 -0800344 d = bus_find_device_by_name(bus, NULL, bus_id);
Andy Fleminge1393452005-08-24 18:46:21 -0500345 if (d) {
346 phydev = to_phy_device(d);
347 } else {
Andy Flemingf62220d2008-04-18 17:29:54 -0500348 printk(KERN_ERR "%s not found\n", bus_id);
Andy Fleminge1393452005-08-24 18:46:21 -0500349 return ERR_PTR(-ENODEV);
350 }
351
352 /* Assume that if there is no driver, that it doesn't
353 * exist, and we should use the genphy driver. */
354 if (NULL == d->driver) {
355 int err;
Andy Fleminge1393452005-08-24 18:46:21 -0500356 d->driver = &genphy_driver.driver;
357
358 err = d->driver->probe(d);
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400359 if (err >= 0)
360 err = device_bind_driver(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500361
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400362 if (err)
363 return ERR_PTR(err);
Andy Fleminge1393452005-08-24 18:46:21 -0500364 }
365
366 if (phydev->attached_dev) {
367 printk(KERN_ERR "%s: %s already attached\n",
Andy Flemingf62220d2008-04-18 17:29:54 -0500368 dev->name, bus_id);
Andy Fleminge1393452005-08-24 18:46:21 -0500369 return ERR_PTR(-EBUSY);
370 }
371
372 phydev->attached_dev = dev;
373
374 phydev->dev_flags = flags;
375
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600376 phydev->interface = interface;
377
378 /* Do initial configuration here, now that
379 * we have certain key parameters
380 * (dev_flags and interface) */
381 if (phydev->drv->config_init) {
382 int err;
383
Andy Flemingf62220d2008-04-18 17:29:54 -0500384 err = phy_scan_fixups(phydev);
385
386 if (err < 0)
387 return ERR_PTR(err);
388
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600389 err = phydev->drv->config_init(phydev);
390
391 if (err < 0)
392 return ERR_PTR(err);
393 }
394
Andy Fleminge1393452005-08-24 18:46:21 -0500395 return phydev;
396}
397EXPORT_SYMBOL(phy_attach);
398
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800399/**
400 * phy_detach - detach a PHY device from its network device
401 * @phydev: target phy_device struct
402 */
Andy Fleminge1393452005-08-24 18:46:21 -0500403void phy_detach(struct phy_device *phydev)
404{
405 phydev->attached_dev = NULL;
406
407 /* If the device had no specific driver before (i.e. - it
408 * was using the generic driver), we unbind the device
409 * from the generic driver so that there's a chance a
410 * real driver could be loaded */
Greg Kroah-Hartman87aebe02007-04-09 11:52:31 -0400411 if (phydev->dev.driver == &genphy_driver.driver)
Andy Fleminge1393452005-08-24 18:46:21 -0500412 device_release_driver(&phydev->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500413}
414EXPORT_SYMBOL(phy_detach);
415
416
Andy Fleming00db8182005-07-30 19:31:23 -0400417/* Generic PHY support and helper functions */
418
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800419/**
420 * genphy_config_advert - sanitize and advertise auto-negotation parameters
421 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400422 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800423 * Description: Writes MII_ADVERTISE with the appropriate values,
Andy Fleming00db8182005-07-30 19:31:23 -0400424 * after sanitizing the values to make sure we only advertise
Trent Piepho51e2a382008-09-24 10:55:46 +0000425 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
426 * hasn't changed, and > 0 if it has changed.
Andy Fleming00db8182005-07-30 19:31:23 -0400427 */
Andy Fleminge1393452005-08-24 18:46:21 -0500428int genphy_config_advert(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400429{
430 u32 advertise;
Trent Piepho51e2a382008-09-24 10:55:46 +0000431 int oldadv, adv;
432 int err, changed = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400433
434 /* Only allow advertising what
435 * this PHY supports */
436 phydev->advertising &= phydev->supported;
437 advertise = phydev->advertising;
438
439 /* Setup standard advertisement */
Trent Piepho51e2a382008-09-24 10:55:46 +0000440 oldadv = adv = phy_read(phydev, MII_ADVERTISE);
Andy Fleming00db8182005-07-30 19:31:23 -0400441
442 if (adv < 0)
443 return adv;
444
445 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
446 ADVERTISE_PAUSE_ASYM);
447 if (advertise & ADVERTISED_10baseT_Half)
448 adv |= ADVERTISE_10HALF;
449 if (advertise & ADVERTISED_10baseT_Full)
450 adv |= ADVERTISE_10FULL;
451 if (advertise & ADVERTISED_100baseT_Half)
452 adv |= ADVERTISE_100HALF;
453 if (advertise & ADVERTISED_100baseT_Full)
454 adv |= ADVERTISE_100FULL;
455 if (advertise & ADVERTISED_Pause)
456 adv |= ADVERTISE_PAUSE_CAP;
457 if (advertise & ADVERTISED_Asym_Pause)
458 adv |= ADVERTISE_PAUSE_ASYM;
459
Trent Piepho51e2a382008-09-24 10:55:46 +0000460 if (adv != oldadv) {
461 err = phy_write(phydev, MII_ADVERTISE, adv);
Andy Fleming00db8182005-07-30 19:31:23 -0400462
Trent Piepho51e2a382008-09-24 10:55:46 +0000463 if (err < 0)
464 return err;
465 changed = 1;
466 }
Andy Fleming00db8182005-07-30 19:31:23 -0400467
468 /* Configure gigabit if it's supported */
469 if (phydev->supported & (SUPPORTED_1000baseT_Half |
470 SUPPORTED_1000baseT_Full)) {
Trent Piepho51e2a382008-09-24 10:55:46 +0000471 oldadv = adv = phy_read(phydev, MII_CTRL1000);
Andy Fleming00db8182005-07-30 19:31:23 -0400472
473 if (adv < 0)
474 return adv;
475
476 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
477 if (advertise & SUPPORTED_1000baseT_Half)
478 adv |= ADVERTISE_1000HALF;
479 if (advertise & SUPPORTED_1000baseT_Full)
480 adv |= ADVERTISE_1000FULL;
Andy Fleming00db8182005-07-30 19:31:23 -0400481
Trent Piepho51e2a382008-09-24 10:55:46 +0000482 if (adv != oldadv) {
483 err = phy_write(phydev, MII_CTRL1000, adv);
484
485 if (err < 0)
486 return err;
487 changed = 1;
488 }
Andy Fleming00db8182005-07-30 19:31:23 -0400489 }
490
Trent Piepho51e2a382008-09-24 10:55:46 +0000491 return changed;
Andy Fleming00db8182005-07-30 19:31:23 -0400492}
Andy Fleminge1393452005-08-24 18:46:21 -0500493EXPORT_SYMBOL(genphy_config_advert);
Andy Fleming00db8182005-07-30 19:31:23 -0400494
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800495/**
496 * genphy_setup_forced - configures/forces speed/duplex from @phydev
497 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400498 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800499 * Description: Configures MII_BMCR to force speed/duplex
Andy Fleming00db8182005-07-30 19:31:23 -0400500 * to the values in phydev. Assumes that the values are valid.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800501 * Please see phy_sanitize_settings().
502 */
Andy Fleming00db8182005-07-30 19:31:23 -0400503int genphy_setup_forced(struct phy_device *phydev)
504{
Andy Flemingf62220d2008-04-18 17:29:54 -0500505 int err;
Domen Puncerbc1e0a02007-08-17 08:54:45 +0200506 int ctl = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400507
508 phydev->pause = phydev->asym_pause = 0;
509
510 if (SPEED_1000 == phydev->speed)
511 ctl |= BMCR_SPEED1000;
512 else if (SPEED_100 == phydev->speed)
513 ctl |= BMCR_SPEED100;
514
515 if (DUPLEX_FULL == phydev->duplex)
516 ctl |= BMCR_FULLDPLX;
517
Andy Flemingf62220d2008-04-18 17:29:54 -0500518 err = phy_write(phydev, MII_BMCR, ctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400519
Andy Flemingf62220d2008-04-18 17:29:54 -0500520 if (err < 0)
521 return err;
522
523 /*
524 * Run the fixups on this PHY, just in case the
525 * board code needs to change something after a reset
526 */
527 err = phy_scan_fixups(phydev);
528
529 if (err < 0)
530 return err;
Andy Fleming00db8182005-07-30 19:31:23 -0400531
532 /* We just reset the device, so we'd better configure any
533 * settings the PHY requires to operate */
534 if (phydev->drv->config_init)
Andy Flemingf62220d2008-04-18 17:29:54 -0500535 err = phydev->drv->config_init(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400536
Andy Flemingf62220d2008-04-18 17:29:54 -0500537 return err;
Andy Fleming00db8182005-07-30 19:31:23 -0400538}
539
540
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800541/**
542 * genphy_restart_aneg - Enable and Restart Autonegotiation
543 * @phydev: target phy_device struct
544 */
Andy Fleming00db8182005-07-30 19:31:23 -0400545int genphy_restart_aneg(struct phy_device *phydev)
546{
547 int ctl;
548
549 ctl = phy_read(phydev, MII_BMCR);
550
551 if (ctl < 0)
552 return ctl;
553
554 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
555
556 /* Don't isolate the PHY if we're negotiating */
557 ctl &= ~(BMCR_ISOLATE);
558
559 ctl = phy_write(phydev, MII_BMCR, ctl);
560
561 return ctl;
562}
Adrian Bunk892871d2008-10-13 18:48:09 -0700563EXPORT_SYMBOL(genphy_restart_aneg);
Andy Fleming00db8182005-07-30 19:31:23 -0400564
565
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800566/**
567 * genphy_config_aneg - restart auto-negotiation or write BMCR
568 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400569 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800570 * Description: If auto-negotiation is enabled, we configure the
Andy Fleming00db8182005-07-30 19:31:23 -0400571 * advertising, and then restart auto-negotiation. If it is not
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800572 * enabled, then we write the BMCR.
Andy Fleming00db8182005-07-30 19:31:23 -0400573 */
574int genphy_config_aneg(struct phy_device *phydev)
575{
Trent Piephode339c22008-11-19 15:52:41 -0800576 int result;
Andy Fleming00db8182005-07-30 19:31:23 -0400577
Trent Piephode339c22008-11-19 15:52:41 -0800578 if (AUTONEG_ENABLE != phydev->autoneg)
579 return genphy_setup_forced(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400580
Trent Piephode339c22008-11-19 15:52:41 -0800581 result = genphy_config_advert(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400582
Trent Piephode339c22008-11-19 15:52:41 -0800583 if (result < 0) /* error */
584 return result;
585
586 if (result == 0) {
587 /* Advertisment hasn't changed, but maybe aneg was never on to
588 * begin with? Or maybe phy was isolated? */
589 int ctl = phy_read(phydev, MII_BMCR);
590
591 if (ctl < 0)
592 return ctl;
593
594 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
595 result = 1; /* do restart aneg */
596 }
597
598 /* Only restart aneg if we are advertising something different
599 * than we were before. */
600 if (result > 0)
601 result = genphy_restart_aneg(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400602
Trent Piepho51e2a382008-09-24 10:55:46 +0000603 return result;
Andy Fleming00db8182005-07-30 19:31:23 -0400604}
605EXPORT_SYMBOL(genphy_config_aneg);
606
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800607/**
608 * genphy_update_link - update link status in @phydev
609 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400610 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800611 * Description: Update the value in phydev->link to reflect the
Andy Fleming00db8182005-07-30 19:31:23 -0400612 * current link value. In order to do this, we need to read
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800613 * the status register twice, keeping the second value.
Andy Fleming00db8182005-07-30 19:31:23 -0400614 */
615int genphy_update_link(struct phy_device *phydev)
616{
617 int status;
618
619 /* Do a fake read */
620 status = phy_read(phydev, MII_BMSR);
621
622 if (status < 0)
623 return status;
624
625 /* Read link and autonegotiation status */
626 status = phy_read(phydev, MII_BMSR);
627
628 if (status < 0)
629 return status;
630
631 if ((status & BMSR_LSTATUS) == 0)
632 phydev->link = 0;
633 else
634 phydev->link = 1;
635
636 return 0;
637}
Andy Fleming6b655522006-10-16 16:19:17 -0500638EXPORT_SYMBOL(genphy_update_link);
Andy Fleming00db8182005-07-30 19:31:23 -0400639
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800640/**
641 * genphy_read_status - check the link status and update current link state
642 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400643 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800644 * Description: Check the link, then figure out the current state
Andy Fleming00db8182005-07-30 19:31:23 -0400645 * by comparing what we advertise with what the link partner
646 * advertises. Start by checking the gigabit possibilities,
647 * then move on to 10/100.
648 */
649int genphy_read_status(struct phy_device *phydev)
650{
651 int adv;
652 int err;
653 int lpa;
654 int lpagb = 0;
655
656 /* Update the link, but return if there
657 * was an error */
658 err = genphy_update_link(phydev);
659 if (err)
660 return err;
661
662 if (AUTONEG_ENABLE == phydev->autoneg) {
663 if (phydev->supported & (SUPPORTED_1000baseT_Half
664 | SUPPORTED_1000baseT_Full)) {
665 lpagb = phy_read(phydev, MII_STAT1000);
666
667 if (lpagb < 0)
668 return lpagb;
669
670 adv = phy_read(phydev, MII_CTRL1000);
671
672 if (adv < 0)
673 return adv;
674
675 lpagb &= adv << 2;
676 }
677
678 lpa = phy_read(phydev, MII_LPA);
679
680 if (lpa < 0)
681 return lpa;
682
683 adv = phy_read(phydev, MII_ADVERTISE);
684
685 if (adv < 0)
686 return adv;
687
688 lpa &= adv;
689
690 phydev->speed = SPEED_10;
691 phydev->duplex = DUPLEX_HALF;
692 phydev->pause = phydev->asym_pause = 0;
693
694 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
695 phydev->speed = SPEED_1000;
696
697 if (lpagb & LPA_1000FULL)
698 phydev->duplex = DUPLEX_FULL;
699 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
700 phydev->speed = SPEED_100;
701
702 if (lpa & LPA_100FULL)
703 phydev->duplex = DUPLEX_FULL;
704 } else
705 if (lpa & LPA_10FULL)
706 phydev->duplex = DUPLEX_FULL;
707
708 if (phydev->duplex == DUPLEX_FULL){
709 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
710 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
711 }
712 } else {
713 int bmcr = phy_read(phydev, MII_BMCR);
714 if (bmcr < 0)
715 return bmcr;
716
717 if (bmcr & BMCR_FULLDPLX)
718 phydev->duplex = DUPLEX_FULL;
719 else
720 phydev->duplex = DUPLEX_HALF;
721
722 if (bmcr & BMCR_SPEED1000)
723 phydev->speed = SPEED_1000;
724 else if (bmcr & BMCR_SPEED100)
725 phydev->speed = SPEED_100;
726 else
727 phydev->speed = SPEED_10;
728
729 phydev->pause = phydev->asym_pause = 0;
730 }
731
732 return 0;
733}
734EXPORT_SYMBOL(genphy_read_status);
735
736static int genphy_config_init(struct phy_device *phydev)
737{
Eric Sesterhenn84c22d72006-09-25 16:39:22 -0700738 int val;
Andy Fleming00db8182005-07-30 19:31:23 -0400739 u32 features;
740
741 /* For now, I'll claim that the generic driver supports
742 * all possible port types */
743 features = (SUPPORTED_TP | SUPPORTED_MII
744 | SUPPORTED_AUI | SUPPORTED_FIBRE |
745 SUPPORTED_BNC);
746
747 /* Do we support autonegotiation? */
748 val = phy_read(phydev, MII_BMSR);
749
750 if (val < 0)
751 return val;
752
753 if (val & BMSR_ANEGCAPABLE)
754 features |= SUPPORTED_Autoneg;
755
756 if (val & BMSR_100FULL)
757 features |= SUPPORTED_100baseT_Full;
758 if (val & BMSR_100HALF)
759 features |= SUPPORTED_100baseT_Half;
760 if (val & BMSR_10FULL)
761 features |= SUPPORTED_10baseT_Full;
762 if (val & BMSR_10HALF)
763 features |= SUPPORTED_10baseT_Half;
764
765 if (val & BMSR_ESTATEN) {
766 val = phy_read(phydev, MII_ESTATUS);
767
768 if (val < 0)
769 return val;
770
771 if (val & ESTATUS_1000_TFULL)
772 features |= SUPPORTED_1000baseT_Full;
773 if (val & ESTATUS_1000_THALF)
774 features |= SUPPORTED_1000baseT_Half;
775 }
776
777 phydev->supported = features;
778 phydev->advertising = features;
779
780 return 0;
781}
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800782int genphy_suspend(struct phy_device *phydev)
783{
784 int value;
Andy Fleming00db8182005-07-30 19:31:23 -0400785
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800786 mutex_lock(&phydev->lock);
787
788 value = phy_read(phydev, MII_BMCR);
789 phy_write(phydev, MII_BMCR, (value | BMCR_PDOWN));
790
791 mutex_unlock(&phydev->lock);
792
793 return 0;
794}
795EXPORT_SYMBOL(genphy_suspend);
796
797int genphy_resume(struct phy_device *phydev)
798{
799 int value;
800
801 mutex_lock(&phydev->lock);
802
803 value = phy_read(phydev, MII_BMCR);
804 phy_write(phydev, MII_BMCR, (value & ~BMCR_PDOWN));
805
806 mutex_unlock(&phydev->lock);
807
808 return 0;
809}
810EXPORT_SYMBOL(genphy_resume);
Andy Fleming00db8182005-07-30 19:31:23 -0400811
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800812/**
813 * phy_probe - probe and init a PHY device
814 * @dev: device to probe and init
Andy Fleming00db8182005-07-30 19:31:23 -0400815 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800816 * Description: Take care of setting up the phy_device structure,
Andy Fleming00db8182005-07-30 19:31:23 -0400817 * set the state to READY (the driver's init function should
818 * set it to STARTING if needed).
819 */
820static int phy_probe(struct device *dev)
821{
822 struct phy_device *phydev;
823 struct phy_driver *phydrv;
824 struct device_driver *drv;
825 int err = 0;
826
827 phydev = to_phy_device(dev);
828
829 /* Make sure the driver is held.
830 * XXX -- Is this correct? */
831 drv = get_driver(phydev->dev.driver);
832 phydrv = to_phy_driver(drv);
833 phydev->drv = phydrv;
834
835 /* Disable the interrupt if the PHY doesn't support it */
836 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
837 phydev->irq = PHY_POLL;
838
Nate Case35b5f6b2008-01-29 10:05:09 -0600839 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400840
841 /* Start out supporting everything. Eventually,
842 * a controller will attach, and may modify one
843 * or both of these values */
844 phydev->supported = phydrv->features;
845 phydev->advertising = phydrv->features;
846
847 /* Set the state to READY by default */
848 phydev->state = PHY_READY;
849
850 if (phydev->drv->probe)
851 err = phydev->drv->probe(phydev);
852
Nate Case35b5f6b2008-01-29 10:05:09 -0600853 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400854
Andy Fleming00db8182005-07-30 19:31:23 -0400855 return err;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600856
Andy Fleming00db8182005-07-30 19:31:23 -0400857}
858
859static int phy_remove(struct device *dev)
860{
861 struct phy_device *phydev;
862
863 phydev = to_phy_device(dev);
864
Nate Case35b5f6b2008-01-29 10:05:09 -0600865 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400866 phydev->state = PHY_DOWN;
Nate Case35b5f6b2008-01-29 10:05:09 -0600867 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400868
869 if (phydev->drv->remove)
870 phydev->drv->remove(phydev);
871
872 put_driver(dev->driver);
873 phydev->drv = NULL;
874
875 return 0;
876}
877
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800878/**
879 * phy_driver_register - register a phy_driver with the PHY layer
880 * @new_driver: new phy_driver to register
881 */
Andy Fleming00db8182005-07-30 19:31:23 -0400882int phy_driver_register(struct phy_driver *new_driver)
883{
884 int retval;
885
Andy Fleming00db8182005-07-30 19:31:23 -0400886 new_driver->driver.name = new_driver->name;
887 new_driver->driver.bus = &mdio_bus_type;
888 new_driver->driver.probe = phy_probe;
889 new_driver->driver.remove = phy_remove;
890
891 retval = driver_register(&new_driver->driver);
892
893 if (retval) {
894 printk(KERN_ERR "%s: Error %d in registering driver\n",
895 new_driver->name, retval);
896
897 return retval;
898 }
899
Olof Johanssonf2511f12007-11-04 16:09:23 -0600900 pr_debug("%s: Registered new driver\n", new_driver->name);
Andy Fleming00db8182005-07-30 19:31:23 -0400901
902 return 0;
903}
904EXPORT_SYMBOL(phy_driver_register);
905
906void phy_driver_unregister(struct phy_driver *drv)
907{
908 driver_unregister(&drv->driver);
909}
910EXPORT_SYMBOL(phy_driver_unregister);
911
Andy Fleminge1393452005-08-24 18:46:21 -0500912static struct phy_driver genphy_driver = {
913 .phy_id = 0xffffffff,
914 .phy_id_mask = 0xffffffff,
915 .name = "Generic PHY",
916 .config_init = genphy_config_init,
917 .features = 0,
918 .config_aneg = genphy_config_aneg,
919 .read_status = genphy_read_status,
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800920 .suspend = genphy_suspend,
921 .resume = genphy_resume,
Andy Fleminge1393452005-08-24 18:46:21 -0500922 .driver = {.owner= THIS_MODULE, },
923};
Andy Fleming00db8182005-07-30 19:31:23 -0400924
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400925static int __init phy_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -0400926{
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400927 int rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400928
929 rc = mdio_bus_init();
930 if (rc)
Andy Fleminge1393452005-08-24 18:46:21 -0500931 return rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400932
Andy Fleminge1393452005-08-24 18:46:21 -0500933 rc = phy_driver_register(&genphy_driver);
934 if (rc)
935 mdio_bus_exit();
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400936
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400937 return rc;
Andy Fleming00db8182005-07-30 19:31:23 -0400938}
939
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400940static void __exit phy_exit(void)
Andy Fleming00db8182005-07-30 19:31:23 -0400941{
942 phy_driver_unregister(&genphy_driver);
Andy Fleminge1393452005-08-24 18:46:21 -0500943 mdio_bus_exit();
Andy Fleming00db8182005-07-30 19:31:23 -0400944}
945
Andy Fleminge1393452005-08-24 18:46:21 -0500946subsys_initcall(phy_init);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400947module_exit(phy_exit);