blob: f11e900b437b66044b422da02e26fe4a4efdeff6 [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
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
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
230 /* If the phy_id is all Fs, there is no device there */
231 if (0xffffffff == phy_id)
232 return NULL;
233
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700234 dev = phy_device_create(bus, addr, phy_id);
Andy Fleming00db8182005-07-30 19:31:23 -0400235
236 return dev;
237}
238
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800239/**
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
Andy Fleming00db8182005-07-30 19:31:23 -0400243 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800244 * Description: Tells the PHY infrastructure to handle the
Andy Fleming00db8182005-07-30 19:31:23 -0400245 * 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
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800249 * this function.
250 */
Andy Fleming00db8182005-07-30 19:31:23 -0400251void phy_prepare_link(struct phy_device *phydev,
252 void (*handler)(struct net_device *))
253{
254 phydev->adjust_link = handler;
255}
256
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800257/**
258 * phy_connect - connect an ethernet device to a PHY device
259 * @dev: the network device to connect
Randy Dunlap5d12b132008-04-28 10:58:22 -0700260 * @bus_id: the id string of the PHY device to connect
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800261 * @handler: callback function for state change notifications
262 * @flags: PHY device's dev_flags
263 * @interface: PHY device's interface
Andy Fleminge1393452005-08-24 18:46:21 -0500264 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800265 * Description: Convenience function for connecting ethernet
Andy Fleminge1393452005-08-24 18:46:21 -0500266 * 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 */
Andy Flemingf62220d2008-04-18 17:29:54 -0500273struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600274 void (*handler)(struct net_device *), u32 flags,
Randy Dunlap1a168932007-02-05 10:44:20 -0800275 phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500276{
277 struct phy_device *phydev;
278
Andy Flemingf62220d2008-04-18 17:29:54 -0500279 phydev = phy_attach(dev, bus_id, flags, interface);
Andy Fleminge1393452005-08-24 18:46:21 -0500280
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
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800295/**
296 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device
297 * @phydev: target phy_device struct
298 */
Andy Fleminge1393452005-08-24 18:46:21 -0500299void 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
Andy Fleminge1393452005-08-24 18:46:21 -0500312static int phy_compare_id(struct device *dev, void *data)
313{
314 return strcmp((char *)data, dev->bus_id) ? 0 : 1;
315}
316
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800317/**
318 * phy_attach - attach a network device to a particular PHY device
319 * @dev: network device to attach
Andy Flemingf62220d2008-04-18 17:29:54 -0500320 * @bus_id: PHY device to attach
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800321 * @flags: PHY device's dev_flags
322 * @interface: PHY device's interface
323 *
324 * Description: Called by drivers to attach to a particular PHY
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
329 * change. The phy_device is returned to the attaching driver.
330 */
Andy Fleminge1393452005-08-24 18:46:21 -0500331struct phy_device *phy_attach(struct net_device *dev,
Andy Flemingf62220d2008-04-18 17:29:54 -0500332 const char *bus_id, u32 flags, phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500333{
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 */
Andy Flemingf62220d2008-04-18 17:29:54 -0500340 d = bus_find_device(bus, NULL, (void *)bus_id, phy_compare_id);
Andy Fleminge1393452005-08-24 18:46:21 -0500341
342 if (d) {
343 phydev = to_phy_device(d);
344 } else {
Andy Flemingf62220d2008-04-18 17:29:54 -0500345 printk(KERN_ERR "%s not found\n", bus_id);
Andy Fleminge1393452005-08-24 18:46:21 -0500346 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;
Andy Fleminge1393452005-08-24 18:46:21 -0500353 d->driver = &genphy_driver.driver;
354
355 err = d->driver->probe(d);
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400356 if (err >= 0)
357 err = device_bind_driver(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500358
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400359 if (err)
360 return ERR_PTR(err);
Andy Fleminge1393452005-08-24 18:46:21 -0500361 }
362
363 if (phydev->attached_dev) {
364 printk(KERN_ERR "%s: %s already attached\n",
Andy Flemingf62220d2008-04-18 17:29:54 -0500365 dev->name, bus_id);
Andy Fleminge1393452005-08-24 18:46:21 -0500366 return ERR_PTR(-EBUSY);
367 }
368
369 phydev->attached_dev = dev;
370
371 phydev->dev_flags = flags;
372
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600373 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
Andy Flemingf62220d2008-04-18 17:29:54 -0500381 err = phy_scan_fixups(phydev);
382
383 if (err < 0)
384 return ERR_PTR(err);
385
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600386 err = phydev->drv->config_init(phydev);
387
388 if (err < 0)
389 return ERR_PTR(err);
390 }
391
Andy Fleminge1393452005-08-24 18:46:21 -0500392 return phydev;
393}
394EXPORT_SYMBOL(phy_attach);
395
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800396/**
397 * phy_detach - detach a PHY device from its network device
398 * @phydev: target phy_device struct
399 */
Andy Fleminge1393452005-08-24 18:46:21 -0500400void 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 */
Greg Kroah-Hartman87aebe02007-04-09 11:52:31 -0400408 if (phydev->dev.driver == &genphy_driver.driver)
Andy Fleminge1393452005-08-24 18:46:21 -0500409 device_release_driver(&phydev->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500410}
411EXPORT_SYMBOL(phy_detach);
412
413
Andy Fleming00db8182005-07-30 19:31:23 -0400414/* Generic PHY support and helper functions */
415
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800416/**
417 * genphy_config_advert - sanitize and advertise auto-negotation parameters
418 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400419 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800420 * Description: Writes MII_ADVERTISE with the appropriate values,
Andy Fleming00db8182005-07-30 19:31:23 -0400421 * after sanitizing the values to make sure we only advertise
Trent Piepho51e2a382008-09-24 10:55:46 +0000422 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
423 * hasn't changed, and > 0 if it has changed.
Andy Fleming00db8182005-07-30 19:31:23 -0400424 */
Andy Fleminge1393452005-08-24 18:46:21 -0500425int genphy_config_advert(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400426{
427 u32 advertise;
Trent Piepho51e2a382008-09-24 10:55:46 +0000428 int oldadv, adv;
429 int err, changed = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400430
431 /* Only allow advertising what
432 * this PHY supports */
433 phydev->advertising &= phydev->supported;
434 advertise = phydev->advertising;
435
436 /* Setup standard advertisement */
Trent Piepho51e2a382008-09-24 10:55:46 +0000437 oldadv = adv = phy_read(phydev, MII_ADVERTISE);
Andy Fleming00db8182005-07-30 19:31:23 -0400438
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
Trent Piepho51e2a382008-09-24 10:55:46 +0000457 if (adv != oldadv) {
458 err = phy_write(phydev, MII_ADVERTISE, adv);
Andy Fleming00db8182005-07-30 19:31:23 -0400459
Trent Piepho51e2a382008-09-24 10:55:46 +0000460 if (err < 0)
461 return err;
462 changed = 1;
463 }
Andy Fleming00db8182005-07-30 19:31:23 -0400464
465 /* Configure gigabit if it's supported */
466 if (phydev->supported & (SUPPORTED_1000baseT_Half |
467 SUPPORTED_1000baseT_Full)) {
Trent Piepho51e2a382008-09-24 10:55:46 +0000468 oldadv = adv = phy_read(phydev, MII_CTRL1000);
Andy Fleming00db8182005-07-30 19:31:23 -0400469
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;
Andy Fleming00db8182005-07-30 19:31:23 -0400478
Trent Piepho51e2a382008-09-24 10:55:46 +0000479 if (adv != oldadv) {
480 err = phy_write(phydev, MII_CTRL1000, adv);
481
482 if (err < 0)
483 return err;
484 changed = 1;
485 }
Andy Fleming00db8182005-07-30 19:31:23 -0400486 }
487
Trent Piepho51e2a382008-09-24 10:55:46 +0000488 return changed;
Andy Fleming00db8182005-07-30 19:31:23 -0400489}
Andy Fleminge1393452005-08-24 18:46:21 -0500490EXPORT_SYMBOL(genphy_config_advert);
Andy Fleming00db8182005-07-30 19:31:23 -0400491
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800492/**
493 * genphy_setup_forced - configures/forces speed/duplex from @phydev
494 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400495 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800496 * Description: Configures MII_BMCR to force speed/duplex
Andy Fleming00db8182005-07-30 19:31:23 -0400497 * to the values in phydev. Assumes that the values are valid.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800498 * Please see phy_sanitize_settings().
499 */
Andy Fleming00db8182005-07-30 19:31:23 -0400500int genphy_setup_forced(struct phy_device *phydev)
501{
Andy Flemingf62220d2008-04-18 17:29:54 -0500502 int err;
Domen Puncerbc1e0a02007-08-17 08:54:45 +0200503 int ctl = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400504
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
Andy Flemingf62220d2008-04-18 17:29:54 -0500515 err = phy_write(phydev, MII_BMCR, ctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400516
Andy Flemingf62220d2008-04-18 17:29:54 -0500517 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;
Andy Fleming00db8182005-07-30 19:31:23 -0400528
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)
Andy Flemingf62220d2008-04-18 17:29:54 -0500532 err = phydev->drv->config_init(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400533
Andy Flemingf62220d2008-04-18 17:29:54 -0500534 return err;
Andy Fleming00db8182005-07-30 19:31:23 -0400535}
536
537
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800538/**
539 * genphy_restart_aneg - Enable and Restart Autonegotiation
540 * @phydev: target phy_device struct
541 */
Andy Fleming00db8182005-07-30 19:31:23 -0400542int 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}
Adrian Bunk892871d2008-10-13 18:48:09 -0700560EXPORT_SYMBOL(genphy_restart_aneg);
Andy Fleming00db8182005-07-30 19:31:23 -0400561
562
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800563/**
564 * genphy_config_aneg - restart auto-negotiation or write BMCR
565 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400566 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800567 * Description: If auto-negotiation is enabled, we configure the
Andy Fleming00db8182005-07-30 19:31:23 -0400568 * advertising, and then restart auto-negotiation. If it is not
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800569 * enabled, then we write the BMCR.
Andy Fleming00db8182005-07-30 19:31:23 -0400570 */
571int genphy_config_aneg(struct phy_device *phydev)
572{
Trent Piepho51e2a382008-09-24 10:55:46 +0000573 int result = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400574
575 if (AUTONEG_ENABLE == phydev->autoneg) {
Trent Piepho51e2a382008-09-24 10:55:46 +0000576 int result = genphy_config_advert(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400577
Trent Piepho51e2a382008-09-24 10:55:46 +0000578 if (result < 0) /* error */
579 return result;
Andy Fleming00db8182005-07-30 19:31:23 -0400580
Trent Piepho51e2a382008-09-24 10:55:46 +0000581 /* Only restart aneg if we are advertising something different
582 * than we were before. */
583 if (result > 0)
584 result = genphy_restart_aneg(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400585 } else
Trent Piepho51e2a382008-09-24 10:55:46 +0000586 result = genphy_setup_forced(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400587
Trent Piepho51e2a382008-09-24 10:55:46 +0000588 return result;
Andy Fleming00db8182005-07-30 19:31:23 -0400589}
590EXPORT_SYMBOL(genphy_config_aneg);
591
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800592/**
593 * genphy_update_link - update link status in @phydev
594 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400595 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800596 * Description: Update the value in phydev->link to reflect the
Andy Fleming00db8182005-07-30 19:31:23 -0400597 * current link value. In order to do this, we need to read
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800598 * the status register twice, keeping the second value.
Andy Fleming00db8182005-07-30 19:31:23 -0400599 */
600int genphy_update_link(struct phy_device *phydev)
601{
602 int status;
603
604 /* Do a fake read */
605 status = phy_read(phydev, MII_BMSR);
606
607 if (status < 0)
608 return status;
609
610 /* Read link and autonegotiation status */
611 status = phy_read(phydev, MII_BMSR);
612
613 if (status < 0)
614 return status;
615
616 if ((status & BMSR_LSTATUS) == 0)
617 phydev->link = 0;
618 else
619 phydev->link = 1;
620
621 return 0;
622}
Andy Fleming6b655522006-10-16 16:19:17 -0500623EXPORT_SYMBOL(genphy_update_link);
Andy Fleming00db8182005-07-30 19:31:23 -0400624
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800625/**
626 * genphy_read_status - check the link status and update current link state
627 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400628 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800629 * Description: Check the link, then figure out the current state
Andy Fleming00db8182005-07-30 19:31:23 -0400630 * by comparing what we advertise with what the link partner
631 * advertises. Start by checking the gigabit possibilities,
632 * then move on to 10/100.
633 */
634int genphy_read_status(struct phy_device *phydev)
635{
636 int adv;
637 int err;
638 int lpa;
639 int lpagb = 0;
640
641 /* Update the link, but return if there
642 * was an error */
643 err = genphy_update_link(phydev);
644 if (err)
645 return err;
646
647 if (AUTONEG_ENABLE == phydev->autoneg) {
648 if (phydev->supported & (SUPPORTED_1000baseT_Half
649 | SUPPORTED_1000baseT_Full)) {
650 lpagb = phy_read(phydev, MII_STAT1000);
651
652 if (lpagb < 0)
653 return lpagb;
654
655 adv = phy_read(phydev, MII_CTRL1000);
656
657 if (adv < 0)
658 return adv;
659
660 lpagb &= adv << 2;
661 }
662
663 lpa = phy_read(phydev, MII_LPA);
664
665 if (lpa < 0)
666 return lpa;
667
668 adv = phy_read(phydev, MII_ADVERTISE);
669
670 if (adv < 0)
671 return adv;
672
673 lpa &= adv;
674
675 phydev->speed = SPEED_10;
676 phydev->duplex = DUPLEX_HALF;
677 phydev->pause = phydev->asym_pause = 0;
678
679 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
680 phydev->speed = SPEED_1000;
681
682 if (lpagb & LPA_1000FULL)
683 phydev->duplex = DUPLEX_FULL;
684 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
685 phydev->speed = SPEED_100;
686
687 if (lpa & LPA_100FULL)
688 phydev->duplex = DUPLEX_FULL;
689 } else
690 if (lpa & LPA_10FULL)
691 phydev->duplex = DUPLEX_FULL;
692
693 if (phydev->duplex == DUPLEX_FULL){
694 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
695 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
696 }
697 } else {
698 int bmcr = phy_read(phydev, MII_BMCR);
699 if (bmcr < 0)
700 return bmcr;
701
702 if (bmcr & BMCR_FULLDPLX)
703 phydev->duplex = DUPLEX_FULL;
704 else
705 phydev->duplex = DUPLEX_HALF;
706
707 if (bmcr & BMCR_SPEED1000)
708 phydev->speed = SPEED_1000;
709 else if (bmcr & BMCR_SPEED100)
710 phydev->speed = SPEED_100;
711 else
712 phydev->speed = SPEED_10;
713
714 phydev->pause = phydev->asym_pause = 0;
715 }
716
717 return 0;
718}
719EXPORT_SYMBOL(genphy_read_status);
720
721static int genphy_config_init(struct phy_device *phydev)
722{
Eric Sesterhenn84c22d72006-09-25 16:39:22 -0700723 int val;
Andy Fleming00db8182005-07-30 19:31:23 -0400724 u32 features;
725
726 /* For now, I'll claim that the generic driver supports
727 * all possible port types */
728 features = (SUPPORTED_TP | SUPPORTED_MII
729 | SUPPORTED_AUI | SUPPORTED_FIBRE |
730 SUPPORTED_BNC);
731
732 /* Do we support autonegotiation? */
733 val = phy_read(phydev, MII_BMSR);
734
735 if (val < 0)
736 return val;
737
738 if (val & BMSR_ANEGCAPABLE)
739 features |= SUPPORTED_Autoneg;
740
741 if (val & BMSR_100FULL)
742 features |= SUPPORTED_100baseT_Full;
743 if (val & BMSR_100HALF)
744 features |= SUPPORTED_100baseT_Half;
745 if (val & BMSR_10FULL)
746 features |= SUPPORTED_10baseT_Full;
747 if (val & BMSR_10HALF)
748 features |= SUPPORTED_10baseT_Half;
749
750 if (val & BMSR_ESTATEN) {
751 val = phy_read(phydev, MII_ESTATUS);
752
753 if (val < 0)
754 return val;
755
756 if (val & ESTATUS_1000_TFULL)
757 features |= SUPPORTED_1000baseT_Full;
758 if (val & ESTATUS_1000_THALF)
759 features |= SUPPORTED_1000baseT_Half;
760 }
761
762 phydev->supported = features;
763 phydev->advertising = features;
764
765 return 0;
766}
767
768
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800769/**
770 * phy_probe - probe and init a PHY device
771 * @dev: device to probe and init
Andy Fleming00db8182005-07-30 19:31:23 -0400772 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800773 * Description: Take care of setting up the phy_device structure,
Andy Fleming00db8182005-07-30 19:31:23 -0400774 * set the state to READY (the driver's init function should
775 * set it to STARTING if needed).
776 */
777static int phy_probe(struct device *dev)
778{
779 struct phy_device *phydev;
780 struct phy_driver *phydrv;
781 struct device_driver *drv;
782 int err = 0;
783
784 phydev = to_phy_device(dev);
785
786 /* Make sure the driver is held.
787 * XXX -- Is this correct? */
788 drv = get_driver(phydev->dev.driver);
789 phydrv = to_phy_driver(drv);
790 phydev->drv = phydrv;
791
792 /* Disable the interrupt if the PHY doesn't support it */
793 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
794 phydev->irq = PHY_POLL;
795
Nate Case35b5f6b2008-01-29 10:05:09 -0600796 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400797
798 /* Start out supporting everything. Eventually,
799 * a controller will attach, and may modify one
800 * or both of these values */
801 phydev->supported = phydrv->features;
802 phydev->advertising = phydrv->features;
803
804 /* Set the state to READY by default */
805 phydev->state = PHY_READY;
806
807 if (phydev->drv->probe)
808 err = phydev->drv->probe(phydev);
809
Nate Case35b5f6b2008-01-29 10:05:09 -0600810 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400811
Andy Fleming00db8182005-07-30 19:31:23 -0400812 return err;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600813
Andy Fleming00db8182005-07-30 19:31:23 -0400814}
815
816static int phy_remove(struct device *dev)
817{
818 struct phy_device *phydev;
819
820 phydev = to_phy_device(dev);
821
Nate Case35b5f6b2008-01-29 10:05:09 -0600822 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400823 phydev->state = PHY_DOWN;
Nate Case35b5f6b2008-01-29 10:05:09 -0600824 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400825
826 if (phydev->drv->remove)
827 phydev->drv->remove(phydev);
828
829 put_driver(dev->driver);
830 phydev->drv = NULL;
831
832 return 0;
833}
834
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800835/**
836 * phy_driver_register - register a phy_driver with the PHY layer
837 * @new_driver: new phy_driver to register
838 */
Andy Fleming00db8182005-07-30 19:31:23 -0400839int phy_driver_register(struct phy_driver *new_driver)
840{
841 int retval;
842
843 memset(&new_driver->driver, 0, sizeof(new_driver->driver));
844 new_driver->driver.name = new_driver->name;
845 new_driver->driver.bus = &mdio_bus_type;
846 new_driver->driver.probe = phy_probe;
847 new_driver->driver.remove = phy_remove;
848
849 retval = driver_register(&new_driver->driver);
850
851 if (retval) {
852 printk(KERN_ERR "%s: Error %d in registering driver\n",
853 new_driver->name, retval);
854
855 return retval;
856 }
857
Olof Johanssonf2511f12007-11-04 16:09:23 -0600858 pr_debug("%s: Registered new driver\n", new_driver->name);
Andy Fleming00db8182005-07-30 19:31:23 -0400859
860 return 0;
861}
862EXPORT_SYMBOL(phy_driver_register);
863
864void phy_driver_unregister(struct phy_driver *drv)
865{
866 driver_unregister(&drv->driver);
867}
868EXPORT_SYMBOL(phy_driver_unregister);
869
Andy Fleminge1393452005-08-24 18:46:21 -0500870static struct phy_driver genphy_driver = {
871 .phy_id = 0xffffffff,
872 .phy_id_mask = 0xffffffff,
873 .name = "Generic PHY",
874 .config_init = genphy_config_init,
875 .features = 0,
876 .config_aneg = genphy_config_aneg,
877 .read_status = genphy_read_status,
878 .driver = {.owner= THIS_MODULE, },
879};
Andy Fleming00db8182005-07-30 19:31:23 -0400880
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400881static int __init phy_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -0400882{
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400883 int rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400884
885 rc = mdio_bus_init();
886 if (rc)
Andy Fleminge1393452005-08-24 18:46:21 -0500887 return rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400888
Andy Fleminge1393452005-08-24 18:46:21 -0500889 rc = phy_driver_register(&genphy_driver);
890 if (rc)
891 mdio_bus_exit();
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400892
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400893 return rc;
Andy Fleming00db8182005-07-30 19:31:23 -0400894}
895
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400896static void __exit phy_exit(void)
Andy Fleming00db8182005-07-30 19:31:23 -0400897{
898 phy_driver_unregister(&genphy_driver);
Andy Fleminge1393452005-08-24 18:46:21 -0500899 mdio_bus_exit();
Andy Fleming00db8182005-07-30 19:31:23 -0400900}
901
Andy Fleminge1393452005-08-24 18:46:21 -0500902subsys_initcall(phy_init);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400903module_exit(phy_exit);