blob: e8c42d6a7d1c01244e4dcedd585a4a2b26f4a601 [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
stephen hemminger89ff05e2010-10-21 08:37:41 +000060static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
61 u32 flags, phy_interface_t interface);
62
Andy Flemingf62220d2008-04-18 17:29:54 -050063/*
64 * Creates a new phy_fixup and adds it to the list
65 * @bus_id: A string which matches phydev->dev.bus_id (or PHY_ANY_ID)
66 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
67 * It can also be PHY_ANY_UID
68 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
69 * comparison
70 * @run: The actual code to be run when a matching PHY is found
71 */
72int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
73 int (*run)(struct phy_device *))
74{
75 struct phy_fixup *fixup;
76
77 fixup = kzalloc(sizeof(struct phy_fixup), GFP_KERNEL);
78 if (!fixup)
79 return -ENOMEM;
80
Kay Sieversfb28ad32008-11-10 13:55:14 -080081 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
Andy Flemingf62220d2008-04-18 17:29:54 -050082 fixup->phy_uid = phy_uid;
83 fixup->phy_uid_mask = phy_uid_mask;
84 fixup->run = run;
85
86 mutex_lock(&phy_fixup_lock);
87 list_add_tail(&fixup->list, &phy_fixup_list);
88 mutex_unlock(&phy_fixup_lock);
89
90 return 0;
91}
92EXPORT_SYMBOL(phy_register_fixup);
93
94/* Registers a fixup to be run on any PHY with the UID in phy_uid */
95int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
96 int (*run)(struct phy_device *))
97{
98 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
99}
100EXPORT_SYMBOL(phy_register_fixup_for_uid);
101
102/* Registers a fixup to be run on the PHY with id string bus_id */
103int phy_register_fixup_for_id(const char *bus_id,
104 int (*run)(struct phy_device *))
105{
106 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
107}
108EXPORT_SYMBOL(phy_register_fixup_for_id);
109
110/*
111 * Returns 1 if fixup matches phydev in bus_id and phy_uid.
112 * Fixups can be set to match any in one or more fields.
113 */
114static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
115{
Kay Sieversfb28ad32008-11-10 13:55:14 -0800116 if (strcmp(fixup->bus_id, dev_name(&phydev->dev)) != 0)
Andy Flemingf62220d2008-04-18 17:29:54 -0500117 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
118 return 0;
119
120 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
121 (phydev->phy_id & fixup->phy_uid_mask))
122 if (fixup->phy_uid != PHY_ANY_UID)
123 return 0;
124
125 return 1;
126}
127
128/* Runs any matching fixups for this phydev */
129int phy_scan_fixups(struct phy_device *phydev)
130{
131 struct phy_fixup *fixup;
132
133 mutex_lock(&phy_fixup_lock);
134 list_for_each_entry(fixup, &phy_fixup_list, list) {
135 if (phy_needs_fixup(phydev, fixup)) {
136 int err;
137
138 err = fixup->run(phydev);
139
Jiri Slabybc23283c2009-07-13 11:23:39 +0000140 if (err < 0) {
141 mutex_unlock(&phy_fixup_lock);
Andy Flemingf62220d2008-04-18 17:29:54 -0500142 return err;
Jiri Slabybc23283c2009-07-13 11:23:39 +0000143 }
Andy Flemingf62220d2008-04-18 17:29:54 -0500144 }
145 }
146 mutex_unlock(&phy_fixup_lock);
147
148 return 0;
149}
150EXPORT_SYMBOL(phy_scan_fixups);
151
stephen hemminger89ff05e2010-10-21 08:37:41 +0000152static struct phy_device* phy_device_create(struct mii_bus *bus,
153 int addr, int phy_id)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700154{
155 struct phy_device *dev;
David Woodhouse8626d3b2010-04-02 01:05:27 +0000156
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700157 /* We allocate the device, and initialize the
158 * default values */
Robert P. J. Daycd861282006-12-13 00:34:52 -0800159 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700160
161 if (NULL == dev)
162 return (struct phy_device*) PTR_ERR((void*)-ENOMEM);
163
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300164 dev->dev.release = phy_device_release;
165
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700166 dev->speed = 0;
167 dev->duplex = -1;
168 dev->pause = dev->asym_pause = 0;
169 dev->link = 1;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600170 dev->interface = PHY_INTERFACE_MODE_GMII;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700171
172 dev->autoneg = AUTONEG_ENABLE;
173
174 dev->addr = addr;
175 dev->phy_id = phy_id;
176 dev->bus = bus;
Grant Likely4dea5472009-04-25 12:52:46 +0000177 dev->dev.parent = bus->parent;
178 dev->dev.bus = &mdio_bus_type;
179 dev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
180 dev_set_name(&dev->dev, PHY_ID_FMT, bus->id, addr);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700181
182 dev->state = PHY_DOWN;
183
Nate Case35b5f6b2008-01-29 10:05:09 -0600184 mutex_init(&dev->lock);
Anton Vorontsov4f9c85a2010-01-18 05:37:16 +0000185 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700186
David Woodhouse8626d3b2010-04-02 01:05:27 +0000187 /* Request the appropriate module unconditionally; don't
188 bother trying to do so only if it isn't already loaded,
189 because that gets complicated. A hotplug event would have
190 done an unconditional modprobe anyway.
191 We don't do normal hotplug because it won't work for MDIO
192 -- because it relies on the device staying around for long
193 enough for the driver to get loaded. With MDIO, the NIC
194 driver will get bored and give up as soon as it finds that
195 there's no driver _already_ loaded. */
196 request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
197
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700198 return dev;
199}
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700200
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800201/**
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400202 * get_phy_id - reads the specified addr for its ID.
203 * @bus: the target MII bus
204 * @addr: PHY address on the MII bus
205 * @phy_id: where to store the ID retrieved.
206 *
207 * Description: Reads the ID registers of the PHY at @addr on the
208 * @bus, stores it in @phy_id and returns zero on success.
209 */
210int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id)
211{
212 int phy_reg;
213
214 /* Grab the bits from PHYIR1, and put them
215 * in the upper half */
David Daney6fe32642011-09-30 11:51:22 +0000216 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400217
218 if (phy_reg < 0)
219 return -EIO;
220
221 *phy_id = (phy_reg & 0xffff) << 16;
222
223 /* Grab the bits from PHYIR2, and put them in the lower half */
David Daney6fe32642011-09-30 11:51:22 +0000224 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400225
226 if (phy_reg < 0)
227 return -EIO;
228
229 *phy_id |= (phy_reg & 0xffff);
230
231 return 0;
232}
Paul Gortmakera01b3d72008-05-22 12:43:50 -0400233EXPORT_SYMBOL(get_phy_id);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400234
235/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800236 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
237 * @bus: the target MII bus
238 * @addr: PHY address on the MII bus
Andy Fleming00db8182005-07-30 19:31:23 -0400239 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800240 * Description: Reads the ID registers of the PHY at @addr on the
241 * @bus, then allocates and returns the phy_device to represent it.
Andy Fleming00db8182005-07-30 19:31:23 -0400242 */
243struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
244{
Andy Fleming00db8182005-07-30 19:31:23 -0400245 struct phy_device *dev = NULL;
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400246 u32 phy_id;
247 int r;
Andy Fleming00db8182005-07-30 19:31:23 -0400248
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400249 r = get_phy_id(bus, addr, &phy_id);
250 if (r)
251 return ERR_PTR(r);
Andy Fleming00db8182005-07-30 19:31:23 -0400252
Giuseppe Cavallaro6436cbc2008-11-20 20:43:18 -0800253 /* If the phy_id is mostly Fs, there is no device there */
254 if ((phy_id & 0x1fffffff) == 0x1fffffff)
255 return NULL;
256
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700257 dev = phy_device_create(bus, addr, phy_id);
Andy Fleming00db8182005-07-30 19:31:23 -0400258
259 return dev;
260}
Grant Likely4dea5472009-04-25 12:52:46 +0000261EXPORT_SYMBOL(get_phy_device);
262
263/**
264 * phy_device_register - Register the phy device on the MDIO bus
Randy Dunlap1d4ac5d2009-06-16 16:56:33 +0000265 * @phydev: phy_device structure to be added to the MDIO bus
Grant Likely4dea5472009-04-25 12:52:46 +0000266 */
267int phy_device_register(struct phy_device *phydev)
268{
269 int err;
270
271 /* Don't register a phy if one is already registered at this
272 * address */
273 if (phydev->bus->phy_map[phydev->addr])
274 return -EINVAL;
275 phydev->bus->phy_map[phydev->addr] = phydev;
276
277 /* Run all of the fixups for this PHY */
278 phy_scan_fixups(phydev);
279
280 err = device_register(&phydev->dev);
281 if (err) {
282 pr_err("phy %d failed to register\n", phydev->addr);
283 goto out;
284 }
285
286 return 0;
287
288 out:
289 phydev->bus->phy_map[phydev->addr] = NULL;
290 return err;
291}
292EXPORT_SYMBOL(phy_device_register);
Andy Fleming00db8182005-07-30 19:31:23 -0400293
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800294/**
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800295 * phy_find_first - finds the first PHY device on the bus
296 * @bus: the target MII bus
297 */
298struct phy_device *phy_find_first(struct mii_bus *bus)
299{
300 int addr;
301
302 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
303 if (bus->phy_map[addr])
304 return bus->phy_map[addr];
305 }
306 return NULL;
307}
308EXPORT_SYMBOL(phy_find_first);
309
310/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800311 * phy_prepare_link - prepares the PHY layer to monitor link status
312 * @phydev: target phy_device struct
313 * @handler: callback function for link status change notifications
Andy Fleming00db8182005-07-30 19:31:23 -0400314 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800315 * Description: Tells the PHY infrastructure to handle the
Andy Fleming00db8182005-07-30 19:31:23 -0400316 * gory details on monitoring link status (whether through
317 * polling or an interrupt), and to call back to the
318 * connected device driver when the link status changes.
319 * If you want to monitor your own link state, don't call
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800320 * this function.
321 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000322static void phy_prepare_link(struct phy_device *phydev,
Andy Fleming00db8182005-07-30 19:31:23 -0400323 void (*handler)(struct net_device *))
324{
325 phydev->adjust_link = handler;
326}
327
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800328/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000329 * phy_connect_direct - connect an ethernet device to a specific phy_device
330 * @dev: the network device to connect
331 * @phydev: the pointer to the phy device
332 * @handler: callback function for state change notifications
333 * @flags: PHY device's dev_flags
334 * @interface: PHY device's interface
335 */
336int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
337 void (*handler)(struct net_device *), u32 flags,
338 phy_interface_t interface)
339{
340 int rc;
341
342 rc = phy_attach_direct(dev, phydev, flags, interface);
343 if (rc)
344 return rc;
345
346 phy_prepare_link(phydev, handler);
347 phy_start_machine(phydev, NULL);
348 if (phydev->irq > 0)
349 phy_start_interrupts(phydev);
350
351 return 0;
352}
353EXPORT_SYMBOL(phy_connect_direct);
354
355/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800356 * phy_connect - connect an ethernet device to a PHY device
357 * @dev: the network device to connect
Randy Dunlap5d12b132008-04-28 10:58:22 -0700358 * @bus_id: the id string of the PHY device to connect
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800359 * @handler: callback function for state change notifications
360 * @flags: PHY device's dev_flags
361 * @interface: PHY device's interface
Andy Fleminge1393452005-08-24 18:46:21 -0500362 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800363 * Description: Convenience function for connecting ethernet
Andy Fleminge1393452005-08-24 18:46:21 -0500364 * devices to PHY devices. The default behavior is for
365 * the PHY infrastructure to handle everything, and only notify
366 * the connected driver when the link status changes. If you
367 * don't want, or can't use the provided functionality, you may
368 * choose to call only the subset of functions which provide
369 * the desired functionality.
370 */
Andy Flemingf62220d2008-04-18 17:29:54 -0500371struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600372 void (*handler)(struct net_device *), u32 flags,
Randy Dunlap1a168932007-02-05 10:44:20 -0800373 phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500374{
375 struct phy_device *phydev;
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000376 struct device *d;
377 int rc;
Andy Fleminge1393452005-08-24 18:46:21 -0500378
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000379 /* Search the list of PHY devices on the mdio bus for the
380 * PHY with the requested name */
381 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
382 if (!d) {
383 pr_err("PHY %s not found\n", bus_id);
384 return ERR_PTR(-ENODEV);
385 }
386 phydev = to_phy_device(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500387
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000388 rc = phy_connect_direct(dev, phydev, handler, flags, interface);
389 if (rc)
390 return ERR_PTR(rc);
Andy Fleminge1393452005-08-24 18:46:21 -0500391
392 return phydev;
393}
394EXPORT_SYMBOL(phy_connect);
395
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800396/**
397 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device
398 * @phydev: target phy_device struct
399 */
Andy Fleminge1393452005-08-24 18:46:21 -0500400void phy_disconnect(struct phy_device *phydev)
401{
402 if (phydev->irq > 0)
403 phy_stop_interrupts(phydev);
404
405 phy_stop_machine(phydev);
406
407 phydev->adjust_link = NULL;
408
409 phy_detach(phydev);
410}
411EXPORT_SYMBOL(phy_disconnect);
412
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000413int phy_init_hw(struct phy_device *phydev)
414{
415 int ret;
416
417 if (!phydev->drv || !phydev->drv->config_init)
418 return 0;
419
420 ret = phy_scan_fixups(phydev);
421 if (ret < 0)
422 return ret;
423
424 return phydev->drv->config_init(phydev);
425}
426
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800427/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000428 * phy_attach_direct - attach a network device to a given PHY device pointer
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800429 * @dev: network device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000430 * @phydev: Pointer to phy_device to attach
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800431 * @flags: PHY device's dev_flags
432 * @interface: PHY device's interface
433 *
434 * Description: Called by drivers to attach to a particular PHY
435 * device. The phy_device is found, and properly hooked up
436 * to the phy_driver. If no driver is attached, then the
437 * genphy_driver is used. The phy_device is given a ptr to
438 * the attaching device, and given a callback for link status
439 * change. The phy_device is returned to the attaching driver.
440 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000441static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
442 u32 flags, phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500443{
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000444 struct device *d = &phydev->dev;
Marc Kleine-Budded005a092011-03-28 14:54:08 +0000445 int err;
Andy Fleminge1393452005-08-24 18:46:21 -0500446
447 /* Assume that if there is no driver, that it doesn't
448 * exist, and we should use the genphy driver. */
449 if (NULL == d->driver) {
Andy Fleminge1393452005-08-24 18:46:21 -0500450 d->driver = &genphy_driver.driver;
451
452 err = d->driver->probe(d);
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400453 if (err >= 0)
454 err = device_bind_driver(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500455
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400456 if (err)
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000457 return err;
Andy Fleminge1393452005-08-24 18:46:21 -0500458 }
459
460 if (phydev->attached_dev) {
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000461 dev_err(&dev->dev, "PHY already attached\n");
462 return -EBUSY;
Andy Fleminge1393452005-08-24 18:46:21 -0500463 }
464
465 phydev->attached_dev = dev;
Richard Cochranc1f19b52010-07-17 08:49:36 +0000466 dev->phydev = phydev;
Andy Fleminge1393452005-08-24 18:46:21 -0500467
468 phydev->dev_flags = flags;
469
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600470 phydev->interface = interface;
471
Anton Vorontsovef24b162010-08-24 14:46:12 -0700472 phydev->state = PHY_READY;
473
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600474 /* Do initial configuration here, now that
475 * we have certain key parameters
476 * (dev_flags and interface) */
Marc Kleine-Budded005a092011-03-28 14:54:08 +0000477 err = phy_init_hw(phydev);
478 if (err)
479 phy_detach(phydev);
480
481 return err;
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000482}
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000483
484/**
485 * phy_attach - attach a network device to a particular PHY device
486 * @dev: network device to attach
487 * @bus_id: Bus ID of PHY device to attach
488 * @flags: PHY device's dev_flags
489 * @interface: PHY device's interface
490 *
491 * Description: Same as phy_attach_direct() except that a PHY bus_id
492 * string is passed instead of a pointer to a struct phy_device.
493 */
494struct phy_device *phy_attach(struct net_device *dev,
495 const char *bus_id, u32 flags, phy_interface_t interface)
496{
497 struct bus_type *bus = &mdio_bus_type;
498 struct phy_device *phydev;
499 struct device *d;
500 int rc;
501
502 /* Search the list of PHY devices on the mdio bus for the
503 * PHY with the requested name */
504 d = bus_find_device_by_name(bus, NULL, bus_id);
505 if (!d) {
506 pr_err("PHY %s not found\n", bus_id);
507 return ERR_PTR(-ENODEV);
508 }
509 phydev = to_phy_device(d);
510
511 rc = phy_attach_direct(dev, phydev, flags, interface);
512 if (rc)
513 return ERR_PTR(rc);
514
Andy Fleminge1393452005-08-24 18:46:21 -0500515 return phydev;
516}
517EXPORT_SYMBOL(phy_attach);
518
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800519/**
520 * phy_detach - detach a PHY device from its network device
521 * @phydev: target phy_device struct
522 */
Andy Fleminge1393452005-08-24 18:46:21 -0500523void phy_detach(struct phy_device *phydev)
524{
Richard Cochranc1f19b52010-07-17 08:49:36 +0000525 phydev->attached_dev->phydev = NULL;
Andy Fleminge1393452005-08-24 18:46:21 -0500526 phydev->attached_dev = NULL;
527
528 /* If the device had no specific driver before (i.e. - it
529 * was using the generic driver), we unbind the device
530 * from the generic driver so that there's a chance a
531 * real driver could be loaded */
Greg Kroah-Hartman87aebe02007-04-09 11:52:31 -0400532 if (phydev->dev.driver == &genphy_driver.driver)
Andy Fleminge1393452005-08-24 18:46:21 -0500533 device_release_driver(&phydev->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500534}
535EXPORT_SYMBOL(phy_detach);
536
537
Andy Fleming00db8182005-07-30 19:31:23 -0400538/* Generic PHY support and helper functions */
539
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800540/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300541 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800542 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400543 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800544 * Description: Writes MII_ADVERTISE with the appropriate values,
Andy Fleming00db8182005-07-30 19:31:23 -0400545 * after sanitizing the values to make sure we only advertise
Trent Piepho51e2a382008-09-24 10:55:46 +0000546 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
547 * hasn't changed, and > 0 if it has changed.
Andy Fleming00db8182005-07-30 19:31:23 -0400548 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000549static int genphy_config_advert(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400550{
551 u32 advertise;
Trent Piepho51e2a382008-09-24 10:55:46 +0000552 int oldadv, adv;
553 int err, changed = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400554
555 /* Only allow advertising what
556 * this PHY supports */
557 phydev->advertising &= phydev->supported;
558 advertise = phydev->advertising;
559
560 /* Setup standard advertisement */
Trent Piepho51e2a382008-09-24 10:55:46 +0000561 oldadv = adv = phy_read(phydev, MII_ADVERTISE);
Andy Fleming00db8182005-07-30 19:31:23 -0400562
563 if (adv < 0)
564 return adv;
565
Matt Carlson28011cf2011-11-16 18:36:59 -0500566 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
Andy Fleming00db8182005-07-30 19:31:23 -0400567 ADVERTISE_PAUSE_ASYM);
Matt Carlson37f07022011-11-17 14:30:55 +0000568 adv |= ethtool_adv_to_mii_adv_t(advertise);
Andy Fleming00db8182005-07-30 19:31:23 -0400569
Trent Piepho51e2a382008-09-24 10:55:46 +0000570 if (adv != oldadv) {
571 err = phy_write(phydev, MII_ADVERTISE, adv);
Andy Fleming00db8182005-07-30 19:31:23 -0400572
Trent Piepho51e2a382008-09-24 10:55:46 +0000573 if (err < 0)
574 return err;
575 changed = 1;
576 }
Andy Fleming00db8182005-07-30 19:31:23 -0400577
578 /* Configure gigabit if it's supported */
579 if (phydev->supported & (SUPPORTED_1000baseT_Half |
580 SUPPORTED_1000baseT_Full)) {
Trent Piepho51e2a382008-09-24 10:55:46 +0000581 oldadv = adv = phy_read(phydev, MII_CTRL1000);
Andy Fleming00db8182005-07-30 19:31:23 -0400582
583 if (adv < 0)
584 return adv;
585
586 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
Matt Carlson37f07022011-11-17 14:30:55 +0000587 adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
Andy Fleming00db8182005-07-30 19:31:23 -0400588
Trent Piepho51e2a382008-09-24 10:55:46 +0000589 if (adv != oldadv) {
590 err = phy_write(phydev, MII_CTRL1000, adv);
591
592 if (err < 0)
593 return err;
594 changed = 1;
595 }
Andy Fleming00db8182005-07-30 19:31:23 -0400596 }
597
Trent Piepho51e2a382008-09-24 10:55:46 +0000598 return changed;
Andy Fleming00db8182005-07-30 19:31:23 -0400599}
Andy Fleming00db8182005-07-30 19:31:23 -0400600
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800601/**
602 * genphy_setup_forced - configures/forces speed/duplex from @phydev
603 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400604 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800605 * Description: Configures MII_BMCR to force speed/duplex
Andy Fleming00db8182005-07-30 19:31:23 -0400606 * to the values in phydev. Assumes that the values are valid.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800607 * Please see phy_sanitize_settings().
608 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000609static int genphy_setup_forced(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400610{
Andy Flemingf62220d2008-04-18 17:29:54 -0500611 int err;
Domen Puncerbc1e0a02007-08-17 08:54:45 +0200612 int ctl = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400613
614 phydev->pause = phydev->asym_pause = 0;
615
616 if (SPEED_1000 == phydev->speed)
617 ctl |= BMCR_SPEED1000;
618 else if (SPEED_100 == phydev->speed)
619 ctl |= BMCR_SPEED100;
620
621 if (DUPLEX_FULL == phydev->duplex)
622 ctl |= BMCR_FULLDPLX;
623
Andy Flemingf62220d2008-04-18 17:29:54 -0500624 err = phy_write(phydev, MII_BMCR, ctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400625
Andy Flemingf62220d2008-04-18 17:29:54 -0500626 return err;
Andy Fleming00db8182005-07-30 19:31:23 -0400627}
628
629
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800630/**
631 * genphy_restart_aneg - Enable and Restart Autonegotiation
632 * @phydev: target phy_device struct
633 */
Andy Fleming00db8182005-07-30 19:31:23 -0400634int genphy_restart_aneg(struct phy_device *phydev)
635{
636 int ctl;
637
638 ctl = phy_read(phydev, MII_BMCR);
639
640 if (ctl < 0)
641 return ctl;
642
643 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
644
645 /* Don't isolate the PHY if we're negotiating */
646 ctl &= ~(BMCR_ISOLATE);
647
648 ctl = phy_write(phydev, MII_BMCR, ctl);
649
650 return ctl;
651}
Adrian Bunk892871d2008-10-13 18:48:09 -0700652EXPORT_SYMBOL(genphy_restart_aneg);
Andy Fleming00db8182005-07-30 19:31:23 -0400653
654
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800655/**
656 * genphy_config_aneg - restart auto-negotiation or write BMCR
657 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400658 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800659 * Description: If auto-negotiation is enabled, we configure the
Andy Fleming00db8182005-07-30 19:31:23 -0400660 * advertising, and then restart auto-negotiation. If it is not
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800661 * enabled, then we write the BMCR.
Andy Fleming00db8182005-07-30 19:31:23 -0400662 */
663int genphy_config_aneg(struct phy_device *phydev)
664{
Trent Piephode339c22008-11-19 15:52:41 -0800665 int result;
Andy Fleming00db8182005-07-30 19:31:23 -0400666
Trent Piephode339c22008-11-19 15:52:41 -0800667 if (AUTONEG_ENABLE != phydev->autoneg)
668 return genphy_setup_forced(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400669
Trent Piephode339c22008-11-19 15:52:41 -0800670 result = genphy_config_advert(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400671
Trent Piephode339c22008-11-19 15:52:41 -0800672 if (result < 0) /* error */
673 return result;
674
675 if (result == 0) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300676 /* Advertisement hasn't changed, but maybe aneg was never on to
Trent Piephode339c22008-11-19 15:52:41 -0800677 * begin with? Or maybe phy was isolated? */
678 int ctl = phy_read(phydev, MII_BMCR);
679
680 if (ctl < 0)
681 return ctl;
682
683 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
684 result = 1; /* do restart aneg */
685 }
686
687 /* Only restart aneg if we are advertising something different
688 * than we were before. */
689 if (result > 0)
690 result = genphy_restart_aneg(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400691
Trent Piepho51e2a382008-09-24 10:55:46 +0000692 return result;
Andy Fleming00db8182005-07-30 19:31:23 -0400693}
694EXPORT_SYMBOL(genphy_config_aneg);
695
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800696/**
697 * genphy_update_link - update link status in @phydev
698 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400699 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800700 * Description: Update the value in phydev->link to reflect the
Andy Fleming00db8182005-07-30 19:31:23 -0400701 * current link value. In order to do this, we need to read
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800702 * the status register twice, keeping the second value.
Andy Fleming00db8182005-07-30 19:31:23 -0400703 */
704int genphy_update_link(struct phy_device *phydev)
705{
706 int status;
707
708 /* Do a fake read */
709 status = phy_read(phydev, MII_BMSR);
710
711 if (status < 0)
712 return status;
713
714 /* Read link and autonegotiation status */
715 status = phy_read(phydev, MII_BMSR);
716
717 if (status < 0)
718 return status;
719
720 if ((status & BMSR_LSTATUS) == 0)
721 phydev->link = 0;
722 else
723 phydev->link = 1;
724
725 return 0;
726}
Andy Fleming6b655522006-10-16 16:19:17 -0500727EXPORT_SYMBOL(genphy_update_link);
Andy Fleming00db8182005-07-30 19:31:23 -0400728
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800729/**
730 * genphy_read_status - check the link status and update current link state
731 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400732 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800733 * Description: Check the link, then figure out the current state
Andy Fleming00db8182005-07-30 19:31:23 -0400734 * by comparing what we advertise with what the link partner
735 * advertises. Start by checking the gigabit possibilities,
736 * then move on to 10/100.
737 */
738int genphy_read_status(struct phy_device *phydev)
739{
740 int adv;
741 int err;
742 int lpa;
743 int lpagb = 0;
744
745 /* Update the link, but return if there
746 * was an error */
747 err = genphy_update_link(phydev);
748 if (err)
749 return err;
750
751 if (AUTONEG_ENABLE == phydev->autoneg) {
752 if (phydev->supported & (SUPPORTED_1000baseT_Half
753 | SUPPORTED_1000baseT_Full)) {
754 lpagb = phy_read(phydev, MII_STAT1000);
755
756 if (lpagb < 0)
757 return lpagb;
758
759 adv = phy_read(phydev, MII_CTRL1000);
760
761 if (adv < 0)
762 return adv;
763
764 lpagb &= adv << 2;
765 }
766
767 lpa = phy_read(phydev, MII_LPA);
768
769 if (lpa < 0)
770 return lpa;
771
772 adv = phy_read(phydev, MII_ADVERTISE);
773
774 if (adv < 0)
775 return adv;
776
777 lpa &= adv;
778
779 phydev->speed = SPEED_10;
780 phydev->duplex = DUPLEX_HALF;
781 phydev->pause = phydev->asym_pause = 0;
782
783 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
784 phydev->speed = SPEED_1000;
785
786 if (lpagb & LPA_1000FULL)
787 phydev->duplex = DUPLEX_FULL;
788 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
789 phydev->speed = SPEED_100;
790
791 if (lpa & LPA_100FULL)
792 phydev->duplex = DUPLEX_FULL;
793 } else
794 if (lpa & LPA_10FULL)
795 phydev->duplex = DUPLEX_FULL;
796
797 if (phydev->duplex == DUPLEX_FULL){
798 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
799 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
800 }
801 } else {
802 int bmcr = phy_read(phydev, MII_BMCR);
803 if (bmcr < 0)
804 return bmcr;
805
806 if (bmcr & BMCR_FULLDPLX)
807 phydev->duplex = DUPLEX_FULL;
808 else
809 phydev->duplex = DUPLEX_HALF;
810
811 if (bmcr & BMCR_SPEED1000)
812 phydev->speed = SPEED_1000;
813 else if (bmcr & BMCR_SPEED100)
814 phydev->speed = SPEED_100;
815 else
816 phydev->speed = SPEED_10;
817
818 phydev->pause = phydev->asym_pause = 0;
819 }
820
821 return 0;
822}
823EXPORT_SYMBOL(genphy_read_status);
824
825static int genphy_config_init(struct phy_device *phydev)
826{
Eric Sesterhenn84c22d72006-09-25 16:39:22 -0700827 int val;
Andy Fleming00db8182005-07-30 19:31:23 -0400828 u32 features;
829
830 /* For now, I'll claim that the generic driver supports
831 * all possible port types */
832 features = (SUPPORTED_TP | SUPPORTED_MII
833 | SUPPORTED_AUI | SUPPORTED_FIBRE |
834 SUPPORTED_BNC);
835
836 /* Do we support autonegotiation? */
837 val = phy_read(phydev, MII_BMSR);
838
839 if (val < 0)
840 return val;
841
842 if (val & BMSR_ANEGCAPABLE)
843 features |= SUPPORTED_Autoneg;
844
845 if (val & BMSR_100FULL)
846 features |= SUPPORTED_100baseT_Full;
847 if (val & BMSR_100HALF)
848 features |= SUPPORTED_100baseT_Half;
849 if (val & BMSR_10FULL)
850 features |= SUPPORTED_10baseT_Full;
851 if (val & BMSR_10HALF)
852 features |= SUPPORTED_10baseT_Half;
853
854 if (val & BMSR_ESTATEN) {
855 val = phy_read(phydev, MII_ESTATUS);
856
857 if (val < 0)
858 return val;
859
860 if (val & ESTATUS_1000_TFULL)
861 features |= SUPPORTED_1000baseT_Full;
862 if (val & ESTATUS_1000_THALF)
863 features |= SUPPORTED_1000baseT_Half;
864 }
865
866 phydev->supported = features;
867 phydev->advertising = features;
868
869 return 0;
870}
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800871int genphy_suspend(struct phy_device *phydev)
872{
873 int value;
Andy Fleming00db8182005-07-30 19:31:23 -0400874
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800875 mutex_lock(&phydev->lock);
876
877 value = phy_read(phydev, MII_BMCR);
878 phy_write(phydev, MII_BMCR, (value | BMCR_PDOWN));
879
880 mutex_unlock(&phydev->lock);
881
882 return 0;
883}
884EXPORT_SYMBOL(genphy_suspend);
885
886int genphy_resume(struct phy_device *phydev)
887{
888 int value;
889
890 mutex_lock(&phydev->lock);
891
892 value = phy_read(phydev, MII_BMCR);
893 phy_write(phydev, MII_BMCR, (value & ~BMCR_PDOWN));
894
895 mutex_unlock(&phydev->lock);
896
897 return 0;
898}
899EXPORT_SYMBOL(genphy_resume);
Andy Fleming00db8182005-07-30 19:31:23 -0400900
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800901/**
902 * phy_probe - probe and init a PHY device
903 * @dev: device to probe and init
Andy Fleming00db8182005-07-30 19:31:23 -0400904 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800905 * Description: Take care of setting up the phy_device structure,
Andy Fleming00db8182005-07-30 19:31:23 -0400906 * set the state to READY (the driver's init function should
907 * set it to STARTING if needed).
908 */
909static int phy_probe(struct device *dev)
910{
911 struct phy_device *phydev;
912 struct phy_driver *phydrv;
913 struct device_driver *drv;
914 int err = 0;
915
916 phydev = to_phy_device(dev);
917
Alan Sternf3ff9242012-01-24 13:35:24 -0500918 drv = phydev->dev.driver;
Andy Fleming00db8182005-07-30 19:31:23 -0400919 phydrv = to_phy_driver(drv);
920 phydev->drv = phydrv;
921
922 /* Disable the interrupt if the PHY doesn't support it */
923 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
924 phydev->irq = PHY_POLL;
925
Nate Case35b5f6b2008-01-29 10:05:09 -0600926 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400927
928 /* Start out supporting everything. Eventually,
929 * a controller will attach, and may modify one
930 * or both of these values */
931 phydev->supported = phydrv->features;
932 phydev->advertising = phydrv->features;
933
934 /* Set the state to READY by default */
935 phydev->state = PHY_READY;
936
937 if (phydev->drv->probe)
938 err = phydev->drv->probe(phydev);
939
Nate Case35b5f6b2008-01-29 10:05:09 -0600940 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400941
Andy Fleming00db8182005-07-30 19:31:23 -0400942 return err;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600943
Andy Fleming00db8182005-07-30 19:31:23 -0400944}
945
946static int phy_remove(struct device *dev)
947{
948 struct phy_device *phydev;
949
950 phydev = to_phy_device(dev);
951
Nate Case35b5f6b2008-01-29 10:05:09 -0600952 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400953 phydev->state = PHY_DOWN;
Nate Case35b5f6b2008-01-29 10:05:09 -0600954 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400955
956 if (phydev->drv->remove)
957 phydev->drv->remove(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400958 phydev->drv = NULL;
959
960 return 0;
961}
962
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800963/**
964 * phy_driver_register - register a phy_driver with the PHY layer
965 * @new_driver: new phy_driver to register
966 */
Andy Fleming00db8182005-07-30 19:31:23 -0400967int phy_driver_register(struct phy_driver *new_driver)
968{
969 int retval;
970
Andy Fleming00db8182005-07-30 19:31:23 -0400971 new_driver->driver.name = new_driver->name;
972 new_driver->driver.bus = &mdio_bus_type;
973 new_driver->driver.probe = phy_probe;
974 new_driver->driver.remove = phy_remove;
975
976 retval = driver_register(&new_driver->driver);
977
978 if (retval) {
979 printk(KERN_ERR "%s: Error %d in registering driver\n",
980 new_driver->name, retval);
981
982 return retval;
983 }
984
Olof Johanssonf2511f12007-11-04 16:09:23 -0600985 pr_debug("%s: Registered new driver\n", new_driver->name);
Andy Fleming00db8182005-07-30 19:31:23 -0400986
987 return 0;
988}
989EXPORT_SYMBOL(phy_driver_register);
990
991void phy_driver_unregister(struct phy_driver *drv)
992{
993 driver_unregister(&drv->driver);
994}
995EXPORT_SYMBOL(phy_driver_unregister);
996
Andy Fleminge1393452005-08-24 18:46:21 -0500997static struct phy_driver genphy_driver = {
998 .phy_id = 0xffffffff,
999 .phy_id_mask = 0xffffffff,
1000 .name = "Generic PHY",
1001 .config_init = genphy_config_init,
1002 .features = 0,
1003 .config_aneg = genphy_config_aneg,
1004 .read_status = genphy_read_status,
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001005 .suspend = genphy_suspend,
1006 .resume = genphy_resume,
Andy Fleminge1393452005-08-24 18:46:21 -05001007 .driver = {.owner= THIS_MODULE, },
1008};
Andy Fleming00db8182005-07-30 19:31:23 -04001009
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001010static int __init phy_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -04001011{
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001012 int rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001013
1014 rc = mdio_bus_init();
1015 if (rc)
Andy Fleminge1393452005-08-24 18:46:21 -05001016 return rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001017
Andy Fleminge1393452005-08-24 18:46:21 -05001018 rc = phy_driver_register(&genphy_driver);
1019 if (rc)
1020 mdio_bus_exit();
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001021
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001022 return rc;
Andy Fleming00db8182005-07-30 19:31:23 -04001023}
1024
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001025static void __exit phy_exit(void)
Andy Fleming00db8182005-07-30 19:31:23 -04001026{
1027 phy_driver_unregister(&genphy_driver);
Andy Fleminge1393452005-08-24 18:46:21 -05001028 mdio_bus_exit();
Andy Fleming00db8182005-07-30 19:31:23 -04001029}
1030
Andy Fleminge1393452005-08-24 18:46:21 -05001031subsys_initcall(phy_init);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001032module_exit(phy_exit);