blob: b10fedd8214394b591bfb81bd102c8d38fce9fb1 [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
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030042void phy_device_free(struct phy_device *phydev)
43{
44 kfree(phydev);
45}
Grant Likely4dea5472009-04-25 12:52:46 +000046EXPORT_SYMBOL(phy_device_free);
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030047
48static void phy_device_release(struct device *dev)
49{
50 phy_device_free(to_phy_device(dev));
51}
52
Grant Likely4dea5472009-04-25 12:52:46 +000053static struct phy_driver genphy_driver;
54extern int mdio_bus_init(void);
55extern void mdio_bus_exit(void);
56
Andy Flemingf62220d2008-04-18 17:29:54 -050057static 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
Kay Sieversfb28ad32008-11-10 13:55:14 -080078 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
Andy Flemingf62220d2008-04-18 17:29:54 -050079 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{
Kay Sieversfb28ad32008-11-10 13:55:14 -0800113 if (strcmp(fixup->bus_id, dev_name(&phydev->dev)) != 0)
Andy Flemingf62220d2008-04-18 17:29:54 -0500114 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
Jiri Slabybc232832009-07-13 11:23:39 +0000137 if (err < 0) {
138 mutex_unlock(&phy_fixup_lock);
Andy Flemingf62220d2008-04-18 17:29:54 -0500139 return err;
Jiri Slabybc232832009-07-13 11:23:39 +0000140 }
Andy Flemingf62220d2008-04-18 17:29:54 -0500141 }
142 }
143 mutex_unlock(&phy_fixup_lock);
144
145 return 0;
146}
147EXPORT_SYMBOL(phy_scan_fixups);
148
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700149struct phy_device* phy_device_create(struct mii_bus *bus, int addr, int phy_id)
150{
151 struct phy_device *dev;
152 /* We allocate the device, and initialize the
153 * default values */
Robert P. J. Daycd861282006-12-13 00:34:52 -0800154 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700155
156 if (NULL == dev)
157 return (struct phy_device*) PTR_ERR((void*)-ENOMEM);
158
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300159 dev->dev.release = phy_device_release;
160
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700161 dev->speed = 0;
162 dev->duplex = -1;
163 dev->pause = dev->asym_pause = 0;
164 dev->link = 1;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600165 dev->interface = PHY_INTERFACE_MODE_GMII;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700166
167 dev->autoneg = AUTONEG_ENABLE;
168
169 dev->addr = addr;
170 dev->phy_id = phy_id;
171 dev->bus = bus;
Grant Likely4dea5472009-04-25 12:52:46 +0000172 dev->dev.parent = bus->parent;
173 dev->dev.bus = &mdio_bus_type;
174 dev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
175 dev_set_name(&dev->dev, PHY_ID_FMT, bus->id, addr);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700176
177 dev->state = PHY_DOWN;
178
Nate Case35b5f6b2008-01-29 10:05:09 -0600179 mutex_init(&dev->lock);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700180
181 return dev;
182}
183EXPORT_SYMBOL(phy_device_create);
184
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800185/**
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400186 * get_phy_id - reads the specified addr for its ID.
187 * @bus: the target MII bus
188 * @addr: PHY address on the MII bus
189 * @phy_id: where to store the ID retrieved.
190 *
191 * Description: Reads the ID registers of the PHY at @addr on the
192 * @bus, stores it in @phy_id and returns zero on success.
193 */
194int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id)
195{
196 int phy_reg;
197
198 /* Grab the bits from PHYIR1, and put them
199 * in the upper half */
200 phy_reg = bus->read(bus, addr, MII_PHYSID1);
201
202 if (phy_reg < 0)
203 return -EIO;
204
205 *phy_id = (phy_reg & 0xffff) << 16;
206
207 /* Grab the bits from PHYIR2, and put them in the lower half */
208 phy_reg = bus->read(bus, addr, MII_PHYSID2);
209
210 if (phy_reg < 0)
211 return -EIO;
212
213 *phy_id |= (phy_reg & 0xffff);
214
215 return 0;
216}
Paul Gortmakera01b3d72008-05-22 12:43:50 -0400217EXPORT_SYMBOL(get_phy_id);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400218
219/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800220 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
221 * @bus: the target MII bus
222 * @addr: PHY address on the MII bus
Andy Fleming00db8182005-07-30 19:31:23 -0400223 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800224 * Description: Reads the ID registers of the PHY at @addr on the
225 * @bus, then allocates and returns the phy_device to represent it.
Andy Fleming00db8182005-07-30 19:31:23 -0400226 */
227struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
228{
Andy Fleming00db8182005-07-30 19:31:23 -0400229 struct phy_device *dev = NULL;
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400230 u32 phy_id;
231 int r;
Andy Fleming00db8182005-07-30 19:31:23 -0400232
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400233 r = get_phy_id(bus, addr, &phy_id);
234 if (r)
235 return ERR_PTR(r);
Andy Fleming00db8182005-07-30 19:31:23 -0400236
Giuseppe Cavallaro6436cbc2008-11-20 20:43:18 -0800237 /* If the phy_id is mostly Fs, there is no device there */
238 if ((phy_id & 0x1fffffff) == 0x1fffffff)
239 return NULL;
240
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700241 dev = phy_device_create(bus, addr, phy_id);
Andy Fleming00db8182005-07-30 19:31:23 -0400242
243 return dev;
244}
Grant Likely4dea5472009-04-25 12:52:46 +0000245EXPORT_SYMBOL(get_phy_device);
246
247/**
248 * phy_device_register - Register the phy device on the MDIO bus
Randy Dunlap1d4ac5d2009-06-16 16:56:33 +0000249 * @phydev: phy_device structure to be added to the MDIO bus
Grant Likely4dea5472009-04-25 12:52:46 +0000250 */
251int phy_device_register(struct phy_device *phydev)
252{
253 int err;
254
255 /* Don't register a phy if one is already registered at this
256 * address */
257 if (phydev->bus->phy_map[phydev->addr])
258 return -EINVAL;
259 phydev->bus->phy_map[phydev->addr] = phydev;
260
261 /* Run all of the fixups for this PHY */
262 phy_scan_fixups(phydev);
263
264 err = device_register(&phydev->dev);
265 if (err) {
266 pr_err("phy %d failed to register\n", phydev->addr);
267 goto out;
268 }
269
270 return 0;
271
272 out:
273 phydev->bus->phy_map[phydev->addr] = NULL;
274 return err;
275}
276EXPORT_SYMBOL(phy_device_register);
Andy Fleming00db8182005-07-30 19:31:23 -0400277
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800278/**
279 * phy_prepare_link - prepares the PHY layer to monitor link status
280 * @phydev: target phy_device struct
281 * @handler: callback function for link status change notifications
Andy Fleming00db8182005-07-30 19:31:23 -0400282 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800283 * Description: Tells the PHY infrastructure to handle the
Andy Fleming00db8182005-07-30 19:31:23 -0400284 * gory details on monitoring link status (whether through
285 * polling or an interrupt), and to call back to the
286 * connected device driver when the link status changes.
287 * If you want to monitor your own link state, don't call
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800288 * this function.
289 */
Andy Fleming00db8182005-07-30 19:31:23 -0400290void phy_prepare_link(struct phy_device *phydev,
291 void (*handler)(struct net_device *))
292{
293 phydev->adjust_link = handler;
294}
295
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800296/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000297 * phy_connect_direct - connect an ethernet device to a specific phy_device
298 * @dev: the network device to connect
299 * @phydev: the pointer to the phy device
300 * @handler: callback function for state change notifications
301 * @flags: PHY device's dev_flags
302 * @interface: PHY device's interface
303 */
304int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
305 void (*handler)(struct net_device *), u32 flags,
306 phy_interface_t interface)
307{
308 int rc;
309
310 rc = phy_attach_direct(dev, phydev, flags, interface);
311 if (rc)
312 return rc;
313
314 phy_prepare_link(phydev, handler);
315 phy_start_machine(phydev, NULL);
316 if (phydev->irq > 0)
317 phy_start_interrupts(phydev);
318
319 return 0;
320}
321EXPORT_SYMBOL(phy_connect_direct);
322
323/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800324 * phy_connect - connect an ethernet device to a PHY device
325 * @dev: the network device to connect
Randy Dunlap5d12b132008-04-28 10:58:22 -0700326 * @bus_id: the id string of the PHY device to connect
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800327 * @handler: callback function for state change notifications
328 * @flags: PHY device's dev_flags
329 * @interface: PHY device's interface
Andy Fleminge1393452005-08-24 18:46:21 -0500330 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800331 * Description: Convenience function for connecting ethernet
Andy Fleminge1393452005-08-24 18:46:21 -0500332 * devices to PHY devices. The default behavior is for
333 * the PHY infrastructure to handle everything, and only notify
334 * the connected driver when the link status changes. If you
335 * don't want, or can't use the provided functionality, you may
336 * choose to call only the subset of functions which provide
337 * the desired functionality.
338 */
Andy Flemingf62220d2008-04-18 17:29:54 -0500339struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600340 void (*handler)(struct net_device *), u32 flags,
Randy Dunlap1a168932007-02-05 10:44:20 -0800341 phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500342{
343 struct phy_device *phydev;
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000344 struct device *d;
345 int rc;
Andy Fleminge1393452005-08-24 18:46:21 -0500346
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000347 /* Search the list of PHY devices on the mdio bus for the
348 * PHY with the requested name */
349 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
350 if (!d) {
351 pr_err("PHY %s not found\n", bus_id);
352 return ERR_PTR(-ENODEV);
353 }
354 phydev = to_phy_device(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500355
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000356 rc = phy_connect_direct(dev, phydev, handler, flags, interface);
357 if (rc)
358 return ERR_PTR(rc);
Andy Fleminge1393452005-08-24 18:46:21 -0500359
360 return phydev;
361}
362EXPORT_SYMBOL(phy_connect);
363
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800364/**
365 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device
366 * @phydev: target phy_device struct
367 */
Andy Fleminge1393452005-08-24 18:46:21 -0500368void phy_disconnect(struct phy_device *phydev)
369{
370 if (phydev->irq > 0)
371 phy_stop_interrupts(phydev);
372
373 phy_stop_machine(phydev);
374
375 phydev->adjust_link = NULL;
376
377 phy_detach(phydev);
378}
379EXPORT_SYMBOL(phy_disconnect);
380
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800381/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000382 * phy_attach_direct - attach a network device to a given PHY device pointer
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800383 * @dev: network device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000384 * @phydev: Pointer to phy_device to attach
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800385 * @flags: PHY device's dev_flags
386 * @interface: PHY device's interface
387 *
388 * Description: Called by drivers to attach to a particular PHY
389 * device. The phy_device is found, and properly hooked up
390 * to the phy_driver. If no driver is attached, then the
391 * genphy_driver is used. The phy_device is given a ptr to
392 * the attaching device, and given a callback for link status
393 * change. The phy_device is returned to the attaching driver.
394 */
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000395int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
396 u32 flags, phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500397{
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000398 struct device *d = &phydev->dev;
Andy Fleminge1393452005-08-24 18:46:21 -0500399
400 /* Assume that if there is no driver, that it doesn't
401 * exist, and we should use the genphy driver. */
402 if (NULL == d->driver) {
403 int err;
Andy Fleminge1393452005-08-24 18:46:21 -0500404 d->driver = &genphy_driver.driver;
405
406 err = d->driver->probe(d);
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400407 if (err >= 0)
408 err = device_bind_driver(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500409
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400410 if (err)
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000411 return err;
Andy Fleminge1393452005-08-24 18:46:21 -0500412 }
413
414 if (phydev->attached_dev) {
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000415 dev_err(&dev->dev, "PHY already attached\n");
416 return -EBUSY;
Andy Fleminge1393452005-08-24 18:46:21 -0500417 }
418
419 phydev->attached_dev = dev;
420
421 phydev->dev_flags = flags;
422
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600423 phydev->interface = interface;
424
425 /* Do initial configuration here, now that
426 * we have certain key parameters
427 * (dev_flags and interface) */
428 if (phydev->drv->config_init) {
429 int err;
430
Andy Flemingf62220d2008-04-18 17:29:54 -0500431 err = phy_scan_fixups(phydev);
432
433 if (err < 0)
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000434 return err;
Andy Flemingf62220d2008-04-18 17:29:54 -0500435
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600436 err = phydev->drv->config_init(phydev);
437
438 if (err < 0)
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000439 return err;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600440 }
441
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000442 return 0;
443}
444EXPORT_SYMBOL(phy_attach_direct);
445
446/**
447 * phy_attach - attach a network device to a particular PHY device
448 * @dev: network device to attach
449 * @bus_id: Bus ID of PHY device to attach
450 * @flags: PHY device's dev_flags
451 * @interface: PHY device's interface
452 *
453 * Description: Same as phy_attach_direct() except that a PHY bus_id
454 * string is passed instead of a pointer to a struct phy_device.
455 */
456struct phy_device *phy_attach(struct net_device *dev,
457 const char *bus_id, u32 flags, phy_interface_t interface)
458{
459 struct bus_type *bus = &mdio_bus_type;
460 struct phy_device *phydev;
461 struct device *d;
462 int rc;
463
464 /* Search the list of PHY devices on the mdio bus for the
465 * PHY with the requested name */
466 d = bus_find_device_by_name(bus, NULL, bus_id);
467 if (!d) {
468 pr_err("PHY %s not found\n", bus_id);
469 return ERR_PTR(-ENODEV);
470 }
471 phydev = to_phy_device(d);
472
473 rc = phy_attach_direct(dev, phydev, flags, interface);
474 if (rc)
475 return ERR_PTR(rc);
476
Andy Fleminge1393452005-08-24 18:46:21 -0500477 return phydev;
478}
479EXPORT_SYMBOL(phy_attach);
480
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800481/**
482 * phy_detach - detach a PHY device from its network device
483 * @phydev: target phy_device struct
484 */
Andy Fleminge1393452005-08-24 18:46:21 -0500485void phy_detach(struct phy_device *phydev)
486{
487 phydev->attached_dev = NULL;
488
489 /* If the device had no specific driver before (i.e. - it
490 * was using the generic driver), we unbind the device
491 * from the generic driver so that there's a chance a
492 * real driver could be loaded */
Greg Kroah-Hartman87aebe02007-04-09 11:52:31 -0400493 if (phydev->dev.driver == &genphy_driver.driver)
Andy Fleminge1393452005-08-24 18:46:21 -0500494 device_release_driver(&phydev->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500495}
496EXPORT_SYMBOL(phy_detach);
497
498
Andy Fleming00db8182005-07-30 19:31:23 -0400499/* Generic PHY support and helper functions */
500
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800501/**
502 * genphy_config_advert - sanitize and advertise auto-negotation parameters
503 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400504 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800505 * Description: Writes MII_ADVERTISE with the appropriate values,
Andy Fleming00db8182005-07-30 19:31:23 -0400506 * after sanitizing the values to make sure we only advertise
Trent Piepho51e2a382008-09-24 10:55:46 +0000507 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
508 * hasn't changed, and > 0 if it has changed.
Andy Fleming00db8182005-07-30 19:31:23 -0400509 */
Andy Fleminge1393452005-08-24 18:46:21 -0500510int genphy_config_advert(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400511{
512 u32 advertise;
Trent Piepho51e2a382008-09-24 10:55:46 +0000513 int oldadv, adv;
514 int err, changed = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400515
516 /* Only allow advertising what
517 * this PHY supports */
518 phydev->advertising &= phydev->supported;
519 advertise = phydev->advertising;
520
521 /* Setup standard advertisement */
Trent Piepho51e2a382008-09-24 10:55:46 +0000522 oldadv = adv = phy_read(phydev, MII_ADVERTISE);
Andy Fleming00db8182005-07-30 19:31:23 -0400523
524 if (adv < 0)
525 return adv;
526
527 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
528 ADVERTISE_PAUSE_ASYM);
529 if (advertise & ADVERTISED_10baseT_Half)
530 adv |= ADVERTISE_10HALF;
531 if (advertise & ADVERTISED_10baseT_Full)
532 adv |= ADVERTISE_10FULL;
533 if (advertise & ADVERTISED_100baseT_Half)
534 adv |= ADVERTISE_100HALF;
535 if (advertise & ADVERTISED_100baseT_Full)
536 adv |= ADVERTISE_100FULL;
537 if (advertise & ADVERTISED_Pause)
538 adv |= ADVERTISE_PAUSE_CAP;
539 if (advertise & ADVERTISED_Asym_Pause)
540 adv |= ADVERTISE_PAUSE_ASYM;
541
Trent Piepho51e2a382008-09-24 10:55:46 +0000542 if (adv != oldadv) {
543 err = phy_write(phydev, MII_ADVERTISE, adv);
Andy Fleming00db8182005-07-30 19:31:23 -0400544
Trent Piepho51e2a382008-09-24 10:55:46 +0000545 if (err < 0)
546 return err;
547 changed = 1;
548 }
Andy Fleming00db8182005-07-30 19:31:23 -0400549
550 /* Configure gigabit if it's supported */
551 if (phydev->supported & (SUPPORTED_1000baseT_Half |
552 SUPPORTED_1000baseT_Full)) {
Trent Piepho51e2a382008-09-24 10:55:46 +0000553 oldadv = adv = phy_read(phydev, MII_CTRL1000);
Andy Fleming00db8182005-07-30 19:31:23 -0400554
555 if (adv < 0)
556 return adv;
557
558 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
559 if (advertise & SUPPORTED_1000baseT_Half)
560 adv |= ADVERTISE_1000HALF;
561 if (advertise & SUPPORTED_1000baseT_Full)
562 adv |= ADVERTISE_1000FULL;
Andy Fleming00db8182005-07-30 19:31:23 -0400563
Trent Piepho51e2a382008-09-24 10:55:46 +0000564 if (adv != oldadv) {
565 err = phy_write(phydev, MII_CTRL1000, adv);
566
567 if (err < 0)
568 return err;
569 changed = 1;
570 }
Andy Fleming00db8182005-07-30 19:31:23 -0400571 }
572
Trent Piepho51e2a382008-09-24 10:55:46 +0000573 return changed;
Andy Fleming00db8182005-07-30 19:31:23 -0400574}
Andy Fleminge1393452005-08-24 18:46:21 -0500575EXPORT_SYMBOL(genphy_config_advert);
Andy Fleming00db8182005-07-30 19:31:23 -0400576
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800577/**
578 * genphy_setup_forced - configures/forces speed/duplex from @phydev
579 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400580 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800581 * Description: Configures MII_BMCR to force speed/duplex
Andy Fleming00db8182005-07-30 19:31:23 -0400582 * to the values in phydev. Assumes that the values are valid.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800583 * Please see phy_sanitize_settings().
584 */
Andy Fleming00db8182005-07-30 19:31:23 -0400585int genphy_setup_forced(struct phy_device *phydev)
586{
Andy Flemingf62220d2008-04-18 17:29:54 -0500587 int err;
Domen Puncerbc1e0a02007-08-17 08:54:45 +0200588 int ctl = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400589
590 phydev->pause = phydev->asym_pause = 0;
591
592 if (SPEED_1000 == phydev->speed)
593 ctl |= BMCR_SPEED1000;
594 else if (SPEED_100 == phydev->speed)
595 ctl |= BMCR_SPEED100;
596
597 if (DUPLEX_FULL == phydev->duplex)
598 ctl |= BMCR_FULLDPLX;
599
Andy Flemingf62220d2008-04-18 17:29:54 -0500600 err = phy_write(phydev, MII_BMCR, ctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400601
Andy Flemingf62220d2008-04-18 17:29:54 -0500602 return err;
Andy Fleming00db8182005-07-30 19:31:23 -0400603}
604
605
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800606/**
607 * genphy_restart_aneg - Enable and Restart Autonegotiation
608 * @phydev: target phy_device struct
609 */
Andy Fleming00db8182005-07-30 19:31:23 -0400610int genphy_restart_aneg(struct phy_device *phydev)
611{
612 int ctl;
613
614 ctl = phy_read(phydev, MII_BMCR);
615
616 if (ctl < 0)
617 return ctl;
618
619 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
620
621 /* Don't isolate the PHY if we're negotiating */
622 ctl &= ~(BMCR_ISOLATE);
623
624 ctl = phy_write(phydev, MII_BMCR, ctl);
625
626 return ctl;
627}
Adrian Bunk892871d2008-10-13 18:48:09 -0700628EXPORT_SYMBOL(genphy_restart_aneg);
Andy Fleming00db8182005-07-30 19:31:23 -0400629
630
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800631/**
632 * genphy_config_aneg - restart auto-negotiation or write BMCR
633 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400634 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800635 * Description: If auto-negotiation is enabled, we configure the
Andy Fleming00db8182005-07-30 19:31:23 -0400636 * advertising, and then restart auto-negotiation. If it is not
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800637 * enabled, then we write the BMCR.
Andy Fleming00db8182005-07-30 19:31:23 -0400638 */
639int genphy_config_aneg(struct phy_device *phydev)
640{
Trent Piephode339c22008-11-19 15:52:41 -0800641 int result;
Andy Fleming00db8182005-07-30 19:31:23 -0400642
Trent Piephode339c22008-11-19 15:52:41 -0800643 if (AUTONEG_ENABLE != phydev->autoneg)
644 return genphy_setup_forced(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400645
Trent Piephode339c22008-11-19 15:52:41 -0800646 result = genphy_config_advert(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400647
Trent Piephode339c22008-11-19 15:52:41 -0800648 if (result < 0) /* error */
649 return result;
650
651 if (result == 0) {
652 /* Advertisment hasn't changed, but maybe aneg was never on to
653 * begin with? Or maybe phy was isolated? */
654 int ctl = phy_read(phydev, MII_BMCR);
655
656 if (ctl < 0)
657 return ctl;
658
659 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
660 result = 1; /* do restart aneg */
661 }
662
663 /* Only restart aneg if we are advertising something different
664 * than we were before. */
665 if (result > 0)
666 result = genphy_restart_aneg(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400667
Trent Piepho51e2a382008-09-24 10:55:46 +0000668 return result;
Andy Fleming00db8182005-07-30 19:31:23 -0400669}
670EXPORT_SYMBOL(genphy_config_aneg);
671
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800672/**
673 * genphy_update_link - update link status in @phydev
674 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400675 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800676 * Description: Update the value in phydev->link to reflect the
Andy Fleming00db8182005-07-30 19:31:23 -0400677 * current link value. In order to do this, we need to read
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800678 * the status register twice, keeping the second value.
Andy Fleming00db8182005-07-30 19:31:23 -0400679 */
680int genphy_update_link(struct phy_device *phydev)
681{
682 int status;
683
684 /* Do a fake read */
685 status = phy_read(phydev, MII_BMSR);
686
687 if (status < 0)
688 return status;
689
690 /* Read link and autonegotiation status */
691 status = phy_read(phydev, MII_BMSR);
692
693 if (status < 0)
694 return status;
695
696 if ((status & BMSR_LSTATUS) == 0)
697 phydev->link = 0;
698 else
699 phydev->link = 1;
700
701 return 0;
702}
Andy Fleming6b655522006-10-16 16:19:17 -0500703EXPORT_SYMBOL(genphy_update_link);
Andy Fleming00db8182005-07-30 19:31:23 -0400704
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800705/**
706 * genphy_read_status - check the link status and update current link state
707 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400708 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800709 * Description: Check the link, then figure out the current state
Andy Fleming00db8182005-07-30 19:31:23 -0400710 * by comparing what we advertise with what the link partner
711 * advertises. Start by checking the gigabit possibilities,
712 * then move on to 10/100.
713 */
714int genphy_read_status(struct phy_device *phydev)
715{
716 int adv;
717 int err;
718 int lpa;
719 int lpagb = 0;
720
721 /* Update the link, but return if there
722 * was an error */
723 err = genphy_update_link(phydev);
724 if (err)
725 return err;
726
727 if (AUTONEG_ENABLE == phydev->autoneg) {
728 if (phydev->supported & (SUPPORTED_1000baseT_Half
729 | SUPPORTED_1000baseT_Full)) {
730 lpagb = phy_read(phydev, MII_STAT1000);
731
732 if (lpagb < 0)
733 return lpagb;
734
735 adv = phy_read(phydev, MII_CTRL1000);
736
737 if (adv < 0)
738 return adv;
739
740 lpagb &= adv << 2;
741 }
742
743 lpa = phy_read(phydev, MII_LPA);
744
745 if (lpa < 0)
746 return lpa;
747
748 adv = phy_read(phydev, MII_ADVERTISE);
749
750 if (adv < 0)
751 return adv;
752
753 lpa &= adv;
754
755 phydev->speed = SPEED_10;
756 phydev->duplex = DUPLEX_HALF;
757 phydev->pause = phydev->asym_pause = 0;
758
759 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
760 phydev->speed = SPEED_1000;
761
762 if (lpagb & LPA_1000FULL)
763 phydev->duplex = DUPLEX_FULL;
764 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
765 phydev->speed = SPEED_100;
766
767 if (lpa & LPA_100FULL)
768 phydev->duplex = DUPLEX_FULL;
769 } else
770 if (lpa & LPA_10FULL)
771 phydev->duplex = DUPLEX_FULL;
772
773 if (phydev->duplex == DUPLEX_FULL){
774 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
775 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
776 }
777 } else {
778 int bmcr = phy_read(phydev, MII_BMCR);
779 if (bmcr < 0)
780 return bmcr;
781
782 if (bmcr & BMCR_FULLDPLX)
783 phydev->duplex = DUPLEX_FULL;
784 else
785 phydev->duplex = DUPLEX_HALF;
786
787 if (bmcr & BMCR_SPEED1000)
788 phydev->speed = SPEED_1000;
789 else if (bmcr & BMCR_SPEED100)
790 phydev->speed = SPEED_100;
791 else
792 phydev->speed = SPEED_10;
793
794 phydev->pause = phydev->asym_pause = 0;
795 }
796
797 return 0;
798}
799EXPORT_SYMBOL(genphy_read_status);
800
801static int genphy_config_init(struct phy_device *phydev)
802{
Eric Sesterhenn84c22d72006-09-25 16:39:22 -0700803 int val;
Andy Fleming00db8182005-07-30 19:31:23 -0400804 u32 features;
805
806 /* For now, I'll claim that the generic driver supports
807 * all possible port types */
808 features = (SUPPORTED_TP | SUPPORTED_MII
809 | SUPPORTED_AUI | SUPPORTED_FIBRE |
810 SUPPORTED_BNC);
811
812 /* Do we support autonegotiation? */
813 val = phy_read(phydev, MII_BMSR);
814
815 if (val < 0)
816 return val;
817
818 if (val & BMSR_ANEGCAPABLE)
819 features |= SUPPORTED_Autoneg;
820
821 if (val & BMSR_100FULL)
822 features |= SUPPORTED_100baseT_Full;
823 if (val & BMSR_100HALF)
824 features |= SUPPORTED_100baseT_Half;
825 if (val & BMSR_10FULL)
826 features |= SUPPORTED_10baseT_Full;
827 if (val & BMSR_10HALF)
828 features |= SUPPORTED_10baseT_Half;
829
830 if (val & BMSR_ESTATEN) {
831 val = phy_read(phydev, MII_ESTATUS);
832
833 if (val < 0)
834 return val;
835
836 if (val & ESTATUS_1000_TFULL)
837 features |= SUPPORTED_1000baseT_Full;
838 if (val & ESTATUS_1000_THALF)
839 features |= SUPPORTED_1000baseT_Half;
840 }
841
842 phydev->supported = features;
843 phydev->advertising = features;
844
845 return 0;
846}
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800847int genphy_suspend(struct phy_device *phydev)
848{
849 int value;
Andy Fleming00db8182005-07-30 19:31:23 -0400850
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800851 mutex_lock(&phydev->lock);
852
853 value = phy_read(phydev, MII_BMCR);
854 phy_write(phydev, MII_BMCR, (value | BMCR_PDOWN));
855
856 mutex_unlock(&phydev->lock);
857
858 return 0;
859}
860EXPORT_SYMBOL(genphy_suspend);
861
862int genphy_resume(struct phy_device *phydev)
863{
864 int value;
865
866 mutex_lock(&phydev->lock);
867
868 value = phy_read(phydev, MII_BMCR);
869 phy_write(phydev, MII_BMCR, (value & ~BMCR_PDOWN));
870
871 mutex_unlock(&phydev->lock);
872
873 return 0;
874}
875EXPORT_SYMBOL(genphy_resume);
Andy Fleming00db8182005-07-30 19:31:23 -0400876
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800877/**
878 * phy_probe - probe and init a PHY device
879 * @dev: device to probe and init
Andy Fleming00db8182005-07-30 19:31:23 -0400880 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800881 * Description: Take care of setting up the phy_device structure,
Andy Fleming00db8182005-07-30 19:31:23 -0400882 * set the state to READY (the driver's init function should
883 * set it to STARTING if needed).
884 */
885static int phy_probe(struct device *dev)
886{
887 struct phy_device *phydev;
888 struct phy_driver *phydrv;
889 struct device_driver *drv;
890 int err = 0;
891
892 phydev = to_phy_device(dev);
893
894 /* Make sure the driver is held.
895 * XXX -- Is this correct? */
896 drv = get_driver(phydev->dev.driver);
897 phydrv = to_phy_driver(drv);
898 phydev->drv = phydrv;
899
900 /* Disable the interrupt if the PHY doesn't support it */
901 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
902 phydev->irq = PHY_POLL;
903
Nate Case35b5f6b2008-01-29 10:05:09 -0600904 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400905
906 /* Start out supporting everything. Eventually,
907 * a controller will attach, and may modify one
908 * or both of these values */
909 phydev->supported = phydrv->features;
910 phydev->advertising = phydrv->features;
911
912 /* Set the state to READY by default */
913 phydev->state = PHY_READY;
914
915 if (phydev->drv->probe)
916 err = phydev->drv->probe(phydev);
917
Nate Case35b5f6b2008-01-29 10:05:09 -0600918 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400919
Andy Fleming00db8182005-07-30 19:31:23 -0400920 return err;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600921
Andy Fleming00db8182005-07-30 19:31:23 -0400922}
923
924static int phy_remove(struct device *dev)
925{
926 struct phy_device *phydev;
927
928 phydev = to_phy_device(dev);
929
Nate Case35b5f6b2008-01-29 10:05:09 -0600930 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400931 phydev->state = PHY_DOWN;
Nate Case35b5f6b2008-01-29 10:05:09 -0600932 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400933
934 if (phydev->drv->remove)
935 phydev->drv->remove(phydev);
936
937 put_driver(dev->driver);
938 phydev->drv = NULL;
939
940 return 0;
941}
942
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800943/**
944 * phy_driver_register - register a phy_driver with the PHY layer
945 * @new_driver: new phy_driver to register
946 */
Andy Fleming00db8182005-07-30 19:31:23 -0400947int phy_driver_register(struct phy_driver *new_driver)
948{
949 int retval;
950
Andy Fleming00db8182005-07-30 19:31:23 -0400951 new_driver->driver.name = new_driver->name;
952 new_driver->driver.bus = &mdio_bus_type;
953 new_driver->driver.probe = phy_probe;
954 new_driver->driver.remove = phy_remove;
955
956 retval = driver_register(&new_driver->driver);
957
958 if (retval) {
959 printk(KERN_ERR "%s: Error %d in registering driver\n",
960 new_driver->name, retval);
961
962 return retval;
963 }
964
Olof Johanssonf2511f12007-11-04 16:09:23 -0600965 pr_debug("%s: Registered new driver\n", new_driver->name);
Andy Fleming00db8182005-07-30 19:31:23 -0400966
967 return 0;
968}
969EXPORT_SYMBOL(phy_driver_register);
970
971void phy_driver_unregister(struct phy_driver *drv)
972{
973 driver_unregister(&drv->driver);
974}
975EXPORT_SYMBOL(phy_driver_unregister);
976
Andy Fleminge1393452005-08-24 18:46:21 -0500977static struct phy_driver genphy_driver = {
978 .phy_id = 0xffffffff,
979 .phy_id_mask = 0xffffffff,
980 .name = "Generic PHY",
981 .config_init = genphy_config_init,
982 .features = 0,
983 .config_aneg = genphy_config_aneg,
984 .read_status = genphy_read_status,
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800985 .suspend = genphy_suspend,
986 .resume = genphy_resume,
Andy Fleminge1393452005-08-24 18:46:21 -0500987 .driver = {.owner= THIS_MODULE, },
988};
Andy Fleming00db8182005-07-30 19:31:23 -0400989
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400990static int __init phy_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -0400991{
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400992 int rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400993
994 rc = mdio_bus_init();
995 if (rc)
Andy Fleminge1393452005-08-24 18:46:21 -0500996 return rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400997
Andy Fleminge1393452005-08-24 18:46:21 -0500998 rc = phy_driver_register(&genphy_driver);
999 if (rc)
1000 mdio_bus_exit();
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001001
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001002 return rc;
Andy Fleming00db8182005-07-30 19:31:23 -04001003}
1004
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001005static void __exit phy_exit(void)
Andy Fleming00db8182005-07-30 19:31:23 -04001006{
1007 phy_driver_unregister(&genphy_driver);
Andy Fleminge1393452005-08-24 18:46:21 -05001008 mdio_bus_exit();
Andy Fleming00db8182005-07-30 19:31:23 -04001009}
1010
Andy Fleminge1393452005-08-24 18:46:21 -05001011subsys_initcall(phy_init);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001012module_exit(phy_exit);