blob: 18ab0daf44901e1363c67c6a7f09f1810020eb9d [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 */
Joe Perches8d242482012-06-09 07:49:07 +000017
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Andy Fleming00db8182005-07-30 19:31:23 -040020#include <linux/kernel.h>
Andy Fleming00db8182005-07-30 19:31:23 -040021#include <linux/string.h>
22#include <linux/errno.h>
23#include <linux/unistd.h>
24#include <linux/slab.h>
25#include <linux/interrupt.h>
26#include <linux/init.h>
27#include <linux/delay.h>
28#include <linux/netdevice.h>
29#include <linux/etherdevice.h>
30#include <linux/skbuff.h>
Andy Fleming00db8182005-07-30 19:31:23 -040031#include <linux/mm.h>
32#include <linux/module.h>
Andy Fleming00db8182005-07-30 19:31:23 -040033#include <linux/mii.h>
34#include <linux/ethtool.h>
35#include <linux/phy.h>
36
37#include <asm/io.h>
38#include <asm/irq.h>
39#include <asm/uaccess.h>
40
Olaf Heringafcceaa2005-12-14 00:33:49 +010041MODULE_DESCRIPTION("PHY library");
42MODULE_AUTHOR("Andy Fleming");
43MODULE_LICENSE("GPL");
44
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030045void phy_device_free(struct phy_device *phydev)
46{
47 kfree(phydev);
48}
Grant Likely4dea5472009-04-25 12:52:46 +000049EXPORT_SYMBOL(phy_device_free);
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030050
51static void phy_device_release(struct device *dev)
52{
53 phy_device_free(to_phy_device(dev));
54}
55
Grant Likely4dea5472009-04-25 12:52:46 +000056static struct phy_driver genphy_driver;
57extern int mdio_bus_init(void);
58extern void mdio_bus_exit(void);
59
Andy Flemingf62220d2008-04-18 17:29:54 -050060static LIST_HEAD(phy_fixup_list);
61static DEFINE_MUTEX(phy_fixup_lock);
62
stephen hemminger89ff05e2010-10-21 08:37:41 +000063static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
64 u32 flags, phy_interface_t interface);
65
Andy Flemingf62220d2008-04-18 17:29:54 -050066/*
67 * Creates a new phy_fixup and adds it to the list
68 * @bus_id: A string which matches phydev->dev.bus_id (or PHY_ANY_ID)
69 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
70 * It can also be PHY_ANY_UID
71 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
72 * comparison
73 * @run: The actual code to be run when a matching PHY is found
74 */
75int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
76 int (*run)(struct phy_device *))
77{
78 struct phy_fixup *fixup;
79
80 fixup = kzalloc(sizeof(struct phy_fixup), GFP_KERNEL);
81 if (!fixup)
82 return -ENOMEM;
83
Kay Sieversfb28ad352008-11-10 13:55:14 -080084 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
Andy Flemingf62220d2008-04-18 17:29:54 -050085 fixup->phy_uid = phy_uid;
86 fixup->phy_uid_mask = phy_uid_mask;
87 fixup->run = run;
88
89 mutex_lock(&phy_fixup_lock);
90 list_add_tail(&fixup->list, &phy_fixup_list);
91 mutex_unlock(&phy_fixup_lock);
92
93 return 0;
94}
95EXPORT_SYMBOL(phy_register_fixup);
96
97/* Registers a fixup to be run on any PHY with the UID in phy_uid */
98int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
99 int (*run)(struct phy_device *))
100{
101 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
102}
103EXPORT_SYMBOL(phy_register_fixup_for_uid);
104
105/* Registers a fixup to be run on the PHY with id string bus_id */
106int phy_register_fixup_for_id(const char *bus_id,
107 int (*run)(struct phy_device *))
108{
109 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
110}
111EXPORT_SYMBOL(phy_register_fixup_for_id);
112
113/*
114 * Returns 1 if fixup matches phydev in bus_id and phy_uid.
115 * Fixups can be set to match any in one or more fields.
116 */
117static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
118{
Kay Sieversfb28ad352008-11-10 13:55:14 -0800119 if (strcmp(fixup->bus_id, dev_name(&phydev->dev)) != 0)
Andy Flemingf62220d2008-04-18 17:29:54 -0500120 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
121 return 0;
122
123 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
124 (phydev->phy_id & fixup->phy_uid_mask))
125 if (fixup->phy_uid != PHY_ANY_UID)
126 return 0;
127
128 return 1;
129}
130
131/* Runs any matching fixups for this phydev */
132int phy_scan_fixups(struct phy_device *phydev)
133{
134 struct phy_fixup *fixup;
135
136 mutex_lock(&phy_fixup_lock);
137 list_for_each_entry(fixup, &phy_fixup_list, list) {
138 if (phy_needs_fixup(phydev, fixup)) {
139 int err;
140
141 err = fixup->run(phydev);
142
Jiri Slabybc232832009-07-13 11:23:39 +0000143 if (err < 0) {
144 mutex_unlock(&phy_fixup_lock);
Andy Flemingf62220d2008-04-18 17:29:54 -0500145 return err;
Jiri Slabybc232832009-07-13 11:23:39 +0000146 }
Andy Flemingf62220d2008-04-18 17:29:54 -0500147 }
148 }
149 mutex_unlock(&phy_fixup_lock);
150
151 return 0;
152}
153EXPORT_SYMBOL(phy_scan_fixups);
154
stephen hemminger89ff05e2010-10-21 08:37:41 +0000155static struct phy_device* phy_device_create(struct mii_bus *bus,
156 int addr, int phy_id)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700157{
158 struct phy_device *dev;
David Woodhouse8626d3b2010-04-02 01:05:27 +0000159
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700160 /* We allocate the device, and initialize the
161 * default values */
Robert P. J. Daycd861282006-12-13 00:34:52 -0800162 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700163
164 if (NULL == dev)
165 return (struct phy_device*) PTR_ERR((void*)-ENOMEM);
166
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300167 dev->dev.release = phy_device_release;
168
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700169 dev->speed = 0;
170 dev->duplex = -1;
171 dev->pause = dev->asym_pause = 0;
172 dev->link = 1;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600173 dev->interface = PHY_INTERFACE_MODE_GMII;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700174
175 dev->autoneg = AUTONEG_ENABLE;
176
177 dev->addr = addr;
178 dev->phy_id = phy_id;
179 dev->bus = bus;
Grant Likely4dea5472009-04-25 12:52:46 +0000180 dev->dev.parent = bus->parent;
181 dev->dev.bus = &mdio_bus_type;
182 dev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
183 dev_set_name(&dev->dev, PHY_ID_FMT, bus->id, addr);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700184
185 dev->state = PHY_DOWN;
186
Nate Case35b5f6b2008-01-29 10:05:09 -0600187 mutex_init(&dev->lock);
Anton Vorontsov4f9c85a2010-01-18 05:37:16 +0000188 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700189
David Woodhouse8626d3b2010-04-02 01:05:27 +0000190 /* Request the appropriate module unconditionally; don't
191 bother trying to do so only if it isn't already loaded,
192 because that gets complicated. A hotplug event would have
193 done an unconditional modprobe anyway.
194 We don't do normal hotplug because it won't work for MDIO
195 -- because it relies on the device staying around for long
196 enough for the driver to get loaded. With MDIO, the NIC
197 driver will get bored and give up as soon as it finds that
198 there's no driver _already_ loaded. */
199 request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
200
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700201 return dev;
202}
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700203
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800204/**
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400205 * get_phy_id - reads the specified addr for its ID.
206 * @bus: the target MII bus
207 * @addr: PHY address on the MII bus
208 * @phy_id: where to store the ID retrieved.
209 *
210 * Description: Reads the ID registers of the PHY at @addr on the
211 * @bus, stores it in @phy_id and returns zero on success.
212 */
David Daney82251de2012-05-15 10:46:52 +0000213static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id)
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400214{
215 int phy_reg;
216
217 /* Grab the bits from PHYIR1, and put them
218 * in the upper half */
David Daney6fe32642011-09-30 11:51:22 +0000219 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400220
221 if (phy_reg < 0)
222 return -EIO;
223
224 *phy_id = (phy_reg & 0xffff) << 16;
225
226 /* Grab the bits from PHYIR2, and put them in the lower half */
David Daney6fe32642011-09-30 11:51:22 +0000227 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400228
229 if (phy_reg < 0)
230 return -EIO;
231
232 *phy_id |= (phy_reg & 0xffff);
233
234 return 0;
235}
236
237/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800238 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
239 * @bus: the target MII bus
240 * @addr: PHY address on the MII bus
Andy Fleming00db8182005-07-30 19:31:23 -0400241 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800242 * Description: Reads the ID registers of the PHY at @addr on the
243 * @bus, then allocates and returns the phy_device to represent it.
Andy Fleming00db8182005-07-30 19:31:23 -0400244 */
245struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
246{
Andy Fleming00db8182005-07-30 19:31:23 -0400247 struct phy_device *dev = NULL;
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400248 u32 phy_id;
249 int r;
Andy Fleming00db8182005-07-30 19:31:23 -0400250
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400251 r = get_phy_id(bus, addr, &phy_id);
252 if (r)
253 return ERR_PTR(r);
Andy Fleming00db8182005-07-30 19:31:23 -0400254
Giuseppe Cavallaro6436cbc2008-11-20 20:43:18 -0800255 /* If the phy_id is mostly Fs, there is no device there */
256 if ((phy_id & 0x1fffffff) == 0x1fffffff)
257 return NULL;
258
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700259 dev = phy_device_create(bus, addr, phy_id);
Andy Fleming00db8182005-07-30 19:31:23 -0400260
261 return dev;
262}
Grant Likely4dea5472009-04-25 12:52:46 +0000263EXPORT_SYMBOL(get_phy_device);
264
265/**
266 * phy_device_register - Register the phy device on the MDIO bus
Randy Dunlap1d4ac5d2009-06-16 16:56:33 +0000267 * @phydev: phy_device structure to be added to the MDIO bus
Grant Likely4dea5472009-04-25 12:52:46 +0000268 */
269int phy_device_register(struct phy_device *phydev)
270{
271 int err;
272
273 /* Don't register a phy if one is already registered at this
274 * address */
275 if (phydev->bus->phy_map[phydev->addr])
276 return -EINVAL;
277 phydev->bus->phy_map[phydev->addr] = phydev;
278
279 /* Run all of the fixups for this PHY */
280 phy_scan_fixups(phydev);
281
282 err = device_register(&phydev->dev);
283 if (err) {
284 pr_err("phy %d failed to register\n", phydev->addr);
285 goto out;
286 }
287
288 return 0;
289
290 out:
291 phydev->bus->phy_map[phydev->addr] = NULL;
292 return err;
293}
294EXPORT_SYMBOL(phy_device_register);
Andy Fleming00db8182005-07-30 19:31:23 -0400295
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800296/**
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800297 * phy_find_first - finds the first PHY device on the bus
298 * @bus: the target MII bus
299 */
300struct phy_device *phy_find_first(struct mii_bus *bus)
301{
302 int addr;
303
304 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
305 if (bus->phy_map[addr])
306 return bus->phy_map[addr];
307 }
308 return NULL;
309}
310EXPORT_SYMBOL(phy_find_first);
311
312/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800313 * phy_prepare_link - prepares the PHY layer to monitor link status
314 * @phydev: target phy_device struct
315 * @handler: callback function for link status change notifications
Andy Fleming00db8182005-07-30 19:31:23 -0400316 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800317 * Description: Tells the PHY infrastructure to handle the
Andy Fleming00db8182005-07-30 19:31:23 -0400318 * gory details on monitoring link status (whether through
319 * polling or an interrupt), and to call back to the
320 * connected device driver when the link status changes.
321 * If you want to monitor your own link state, don't call
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800322 * this function.
323 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000324static void phy_prepare_link(struct phy_device *phydev,
Andy Fleming00db8182005-07-30 19:31:23 -0400325 void (*handler)(struct net_device *))
326{
327 phydev->adjust_link = handler;
328}
329
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800330/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000331 * phy_connect_direct - connect an ethernet device to a specific phy_device
332 * @dev: the network device to connect
333 * @phydev: the pointer to the phy device
334 * @handler: callback function for state change notifications
335 * @flags: PHY device's dev_flags
336 * @interface: PHY device's interface
337 */
338int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
339 void (*handler)(struct net_device *), u32 flags,
340 phy_interface_t interface)
341{
342 int rc;
343
344 rc = phy_attach_direct(dev, phydev, flags, interface);
345 if (rc)
346 return rc;
347
348 phy_prepare_link(phydev, handler);
349 phy_start_machine(phydev, NULL);
350 if (phydev->irq > 0)
351 phy_start_interrupts(phydev);
352
353 return 0;
354}
355EXPORT_SYMBOL(phy_connect_direct);
356
357/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800358 * phy_connect - connect an ethernet device to a PHY device
359 * @dev: the network device to connect
Randy Dunlap5d12b132008-04-28 10:58:22 -0700360 * @bus_id: the id string of the PHY device to connect
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800361 * @handler: callback function for state change notifications
362 * @flags: PHY device's dev_flags
363 * @interface: PHY device's interface
Andy Fleminge1393452005-08-24 18:46:21 -0500364 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800365 * Description: Convenience function for connecting ethernet
Andy Fleminge1393452005-08-24 18:46:21 -0500366 * devices to PHY devices. The default behavior is for
367 * the PHY infrastructure to handle everything, and only notify
368 * the connected driver when the link status changes. If you
369 * don't want, or can't use the provided functionality, you may
370 * choose to call only the subset of functions which provide
371 * the desired functionality.
372 */
Andy Flemingf62220d2008-04-18 17:29:54 -0500373struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600374 void (*handler)(struct net_device *), u32 flags,
Randy Dunlap1a168932007-02-05 10:44:20 -0800375 phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500376{
377 struct phy_device *phydev;
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000378 struct device *d;
379 int rc;
Andy Fleminge1393452005-08-24 18:46:21 -0500380
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000381 /* Search the list of PHY devices on the mdio bus for the
382 * PHY with the requested name */
383 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
384 if (!d) {
385 pr_err("PHY %s not found\n", bus_id);
386 return ERR_PTR(-ENODEV);
387 }
388 phydev = to_phy_device(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500389
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000390 rc = phy_connect_direct(dev, phydev, handler, flags, interface);
391 if (rc)
392 return ERR_PTR(rc);
Andy Fleminge1393452005-08-24 18:46:21 -0500393
394 return phydev;
395}
396EXPORT_SYMBOL(phy_connect);
397
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800398/**
399 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device
400 * @phydev: target phy_device struct
401 */
Andy Fleminge1393452005-08-24 18:46:21 -0500402void phy_disconnect(struct phy_device *phydev)
403{
404 if (phydev->irq > 0)
405 phy_stop_interrupts(phydev);
406
407 phy_stop_machine(phydev);
408
409 phydev->adjust_link = NULL;
410
411 phy_detach(phydev);
412}
413EXPORT_SYMBOL(phy_disconnect);
414
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000415int phy_init_hw(struct phy_device *phydev)
416{
417 int ret;
418
419 if (!phydev->drv || !phydev->drv->config_init)
420 return 0;
421
422 ret = phy_scan_fixups(phydev);
423 if (ret < 0)
424 return ret;
425
426 return phydev->drv->config_init(phydev);
427}
428
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800429/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000430 * phy_attach_direct - attach a network device to a given PHY device pointer
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800431 * @dev: network device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000432 * @phydev: Pointer to phy_device to attach
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800433 * @flags: PHY device's dev_flags
434 * @interface: PHY device's interface
435 *
436 * Description: Called by drivers to attach to a particular PHY
437 * device. The phy_device is found, and properly hooked up
438 * to the phy_driver. If no driver is attached, then the
439 * genphy_driver is used. The phy_device is given a ptr to
440 * the attaching device, and given a callback for link status
441 * change. The phy_device is returned to the attaching driver.
442 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000443static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
444 u32 flags, phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500445{
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000446 struct device *d = &phydev->dev;
Marc Kleine-Budded005a092011-03-28 14:54:08 +0000447 int err;
Andy Fleminge1393452005-08-24 18:46:21 -0500448
449 /* Assume that if there is no driver, that it doesn't
450 * exist, and we should use the genphy driver. */
451 if (NULL == d->driver) {
Andy Fleminge1393452005-08-24 18:46:21 -0500452 d->driver = &genphy_driver.driver;
453
454 err = d->driver->probe(d);
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400455 if (err >= 0)
456 err = device_bind_driver(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500457
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400458 if (err)
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000459 return err;
Andy Fleminge1393452005-08-24 18:46:21 -0500460 }
461
462 if (phydev->attached_dev) {
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000463 dev_err(&dev->dev, "PHY already attached\n");
464 return -EBUSY;
Andy Fleminge1393452005-08-24 18:46:21 -0500465 }
466
467 phydev->attached_dev = dev;
Richard Cochranc1f19b52010-07-17 08:49:36 +0000468 dev->phydev = phydev;
Andy Fleminge1393452005-08-24 18:46:21 -0500469
470 phydev->dev_flags = flags;
471
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600472 phydev->interface = interface;
473
Anton Vorontsovef24b162010-08-24 14:46:12 -0700474 phydev->state = PHY_READY;
475
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600476 /* Do initial configuration here, now that
477 * we have certain key parameters
478 * (dev_flags and interface) */
Marc Kleine-Budded005a092011-03-28 14:54:08 +0000479 err = phy_init_hw(phydev);
480 if (err)
481 phy_detach(phydev);
482
483 return err;
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000484}
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000485
486/**
487 * phy_attach - attach a network device to a particular PHY device
488 * @dev: network device to attach
489 * @bus_id: Bus ID of PHY device to attach
490 * @flags: PHY device's dev_flags
491 * @interface: PHY device's interface
492 *
493 * Description: Same as phy_attach_direct() except that a PHY bus_id
494 * string is passed instead of a pointer to a struct phy_device.
495 */
496struct phy_device *phy_attach(struct net_device *dev,
497 const char *bus_id, u32 flags, phy_interface_t interface)
498{
499 struct bus_type *bus = &mdio_bus_type;
500 struct phy_device *phydev;
501 struct device *d;
502 int rc;
503
504 /* Search the list of PHY devices on the mdio bus for the
505 * PHY with the requested name */
506 d = bus_find_device_by_name(bus, NULL, bus_id);
507 if (!d) {
508 pr_err("PHY %s not found\n", bus_id);
509 return ERR_PTR(-ENODEV);
510 }
511 phydev = to_phy_device(d);
512
513 rc = phy_attach_direct(dev, phydev, flags, interface);
514 if (rc)
515 return ERR_PTR(rc);
516
Andy Fleminge1393452005-08-24 18:46:21 -0500517 return phydev;
518}
519EXPORT_SYMBOL(phy_attach);
520
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800521/**
522 * phy_detach - detach a PHY device from its network device
523 * @phydev: target phy_device struct
524 */
Andy Fleminge1393452005-08-24 18:46:21 -0500525void phy_detach(struct phy_device *phydev)
526{
Richard Cochranc1f19b52010-07-17 08:49:36 +0000527 phydev->attached_dev->phydev = NULL;
Andy Fleminge1393452005-08-24 18:46:21 -0500528 phydev->attached_dev = NULL;
529
530 /* If the device had no specific driver before (i.e. - it
531 * was using the generic driver), we unbind the device
532 * from the generic driver so that there's a chance a
533 * real driver could be loaded */
Greg Kroah-Hartman87aebe02007-04-09 11:52:31 -0400534 if (phydev->dev.driver == &genphy_driver.driver)
Andy Fleminge1393452005-08-24 18:46:21 -0500535 device_release_driver(&phydev->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500536}
537EXPORT_SYMBOL(phy_detach);
538
539
Andy Fleming00db8182005-07-30 19:31:23 -0400540/* Generic PHY support and helper functions */
541
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800542/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300543 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800544 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400545 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800546 * Description: Writes MII_ADVERTISE with the appropriate values,
Andy Fleming00db8182005-07-30 19:31:23 -0400547 * after sanitizing the values to make sure we only advertise
Trent Piepho51e2a382008-09-24 10:55:46 +0000548 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
549 * hasn't changed, and > 0 if it has changed.
Andy Fleming00db8182005-07-30 19:31:23 -0400550 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000551static int genphy_config_advert(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400552{
553 u32 advertise;
Trent Piepho51e2a382008-09-24 10:55:46 +0000554 int oldadv, adv;
555 int err, changed = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400556
557 /* Only allow advertising what
558 * this PHY supports */
559 phydev->advertising &= phydev->supported;
560 advertise = phydev->advertising;
561
562 /* Setup standard advertisement */
Trent Piepho51e2a382008-09-24 10:55:46 +0000563 oldadv = adv = phy_read(phydev, MII_ADVERTISE);
Andy Fleming00db8182005-07-30 19:31:23 -0400564
565 if (adv < 0)
566 return adv;
567
Matt Carlson28011cf2011-11-16 18:36:59 -0500568 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
Andy Fleming00db8182005-07-30 19:31:23 -0400569 ADVERTISE_PAUSE_ASYM);
Matt Carlson37f07022011-11-17 14:30:55 +0000570 adv |= ethtool_adv_to_mii_adv_t(advertise);
Andy Fleming00db8182005-07-30 19:31:23 -0400571
Trent Piepho51e2a382008-09-24 10:55:46 +0000572 if (adv != oldadv) {
573 err = phy_write(phydev, MII_ADVERTISE, adv);
Andy Fleming00db8182005-07-30 19:31:23 -0400574
Trent Piepho51e2a382008-09-24 10:55:46 +0000575 if (err < 0)
576 return err;
577 changed = 1;
578 }
Andy Fleming00db8182005-07-30 19:31:23 -0400579
580 /* Configure gigabit if it's supported */
581 if (phydev->supported & (SUPPORTED_1000baseT_Half |
582 SUPPORTED_1000baseT_Full)) {
Trent Piepho51e2a382008-09-24 10:55:46 +0000583 oldadv = adv = phy_read(phydev, MII_CTRL1000);
Andy Fleming00db8182005-07-30 19:31:23 -0400584
585 if (adv < 0)
586 return adv;
587
588 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
Matt Carlson37f07022011-11-17 14:30:55 +0000589 adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
Andy Fleming00db8182005-07-30 19:31:23 -0400590
Trent Piepho51e2a382008-09-24 10:55:46 +0000591 if (adv != oldadv) {
592 err = phy_write(phydev, MII_CTRL1000, adv);
593
594 if (err < 0)
595 return err;
596 changed = 1;
597 }
Andy Fleming00db8182005-07-30 19:31:23 -0400598 }
599
Trent Piepho51e2a382008-09-24 10:55:46 +0000600 return changed;
Andy Fleming00db8182005-07-30 19:31:23 -0400601}
Andy Fleming00db8182005-07-30 19:31:23 -0400602
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800603/**
604 * genphy_setup_forced - configures/forces speed/duplex from @phydev
605 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400606 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800607 * Description: Configures MII_BMCR to force speed/duplex
Andy Fleming00db8182005-07-30 19:31:23 -0400608 * to the values in phydev. Assumes that the values are valid.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800609 * Please see phy_sanitize_settings().
610 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000611static int genphy_setup_forced(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400612{
Andy Flemingf62220d2008-04-18 17:29:54 -0500613 int err;
Domen Puncerbc1e0a02007-08-17 08:54:45 +0200614 int ctl = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400615
616 phydev->pause = phydev->asym_pause = 0;
617
618 if (SPEED_1000 == phydev->speed)
619 ctl |= BMCR_SPEED1000;
620 else if (SPEED_100 == phydev->speed)
621 ctl |= BMCR_SPEED100;
622
623 if (DUPLEX_FULL == phydev->duplex)
624 ctl |= BMCR_FULLDPLX;
625
Andy Flemingf62220d2008-04-18 17:29:54 -0500626 err = phy_write(phydev, MII_BMCR, ctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400627
Andy Flemingf62220d2008-04-18 17:29:54 -0500628 return err;
Andy Fleming00db8182005-07-30 19:31:23 -0400629}
630
631
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800632/**
633 * genphy_restart_aneg - Enable and Restart Autonegotiation
634 * @phydev: target phy_device struct
635 */
Andy Fleming00db8182005-07-30 19:31:23 -0400636int genphy_restart_aneg(struct phy_device *phydev)
637{
638 int ctl;
639
640 ctl = phy_read(phydev, MII_BMCR);
641
642 if (ctl < 0)
643 return ctl;
644
645 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
646
647 /* Don't isolate the PHY if we're negotiating */
648 ctl &= ~(BMCR_ISOLATE);
649
650 ctl = phy_write(phydev, MII_BMCR, ctl);
651
652 return ctl;
653}
Adrian Bunk892871d2008-10-13 18:48:09 -0700654EXPORT_SYMBOL(genphy_restart_aneg);
Andy Fleming00db8182005-07-30 19:31:23 -0400655
656
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800657/**
658 * genphy_config_aneg - restart auto-negotiation or write BMCR
659 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400660 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800661 * Description: If auto-negotiation is enabled, we configure the
Andy Fleming00db8182005-07-30 19:31:23 -0400662 * advertising, and then restart auto-negotiation. If it is not
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800663 * enabled, then we write the BMCR.
Andy Fleming00db8182005-07-30 19:31:23 -0400664 */
665int genphy_config_aneg(struct phy_device *phydev)
666{
Trent Piephode339c22008-11-19 15:52:41 -0800667 int result;
Andy Fleming00db8182005-07-30 19:31:23 -0400668
Trent Piephode339c22008-11-19 15:52:41 -0800669 if (AUTONEG_ENABLE != phydev->autoneg)
670 return genphy_setup_forced(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400671
Trent Piephode339c22008-11-19 15:52:41 -0800672 result = genphy_config_advert(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400673
Trent Piephode339c22008-11-19 15:52:41 -0800674 if (result < 0) /* error */
675 return result;
676
677 if (result == 0) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300678 /* Advertisement hasn't changed, but maybe aneg was never on to
Trent Piephode339c22008-11-19 15:52:41 -0800679 * begin with? Or maybe phy was isolated? */
680 int ctl = phy_read(phydev, MII_BMCR);
681
682 if (ctl < 0)
683 return ctl;
684
685 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
686 result = 1; /* do restart aneg */
687 }
688
689 /* Only restart aneg if we are advertising something different
690 * than we were before. */
691 if (result > 0)
692 result = genphy_restart_aneg(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400693
Trent Piepho51e2a382008-09-24 10:55:46 +0000694 return result;
Andy Fleming00db8182005-07-30 19:31:23 -0400695}
696EXPORT_SYMBOL(genphy_config_aneg);
697
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800698/**
699 * genphy_update_link - update link status in @phydev
700 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400701 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800702 * Description: Update the value in phydev->link to reflect the
Andy Fleming00db8182005-07-30 19:31:23 -0400703 * current link value. In order to do this, we need to read
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800704 * the status register twice, keeping the second value.
Andy Fleming00db8182005-07-30 19:31:23 -0400705 */
706int genphy_update_link(struct phy_device *phydev)
707{
708 int status;
709
710 /* Do a fake read */
711 status = phy_read(phydev, MII_BMSR);
712
713 if (status < 0)
714 return status;
715
716 /* Read link and autonegotiation status */
717 status = phy_read(phydev, MII_BMSR);
718
719 if (status < 0)
720 return status;
721
722 if ((status & BMSR_LSTATUS) == 0)
723 phydev->link = 0;
724 else
725 phydev->link = 1;
726
727 return 0;
728}
Andy Fleming6b655522006-10-16 16:19:17 -0500729EXPORT_SYMBOL(genphy_update_link);
Andy Fleming00db8182005-07-30 19:31:23 -0400730
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800731/**
732 * genphy_read_status - check the link status and update current link state
733 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400734 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800735 * Description: Check the link, then figure out the current state
Andy Fleming00db8182005-07-30 19:31:23 -0400736 * by comparing what we advertise with what the link partner
737 * advertises. Start by checking the gigabit possibilities,
738 * then move on to 10/100.
739 */
740int genphy_read_status(struct phy_device *phydev)
741{
742 int adv;
743 int err;
744 int lpa;
745 int lpagb = 0;
746
747 /* Update the link, but return if there
748 * was an error */
749 err = genphy_update_link(phydev);
750 if (err)
751 return err;
752
753 if (AUTONEG_ENABLE == phydev->autoneg) {
754 if (phydev->supported & (SUPPORTED_1000baseT_Half
755 | SUPPORTED_1000baseT_Full)) {
756 lpagb = phy_read(phydev, MII_STAT1000);
757
758 if (lpagb < 0)
759 return lpagb;
760
761 adv = phy_read(phydev, MII_CTRL1000);
762
763 if (adv < 0)
764 return adv;
765
766 lpagb &= adv << 2;
767 }
768
769 lpa = phy_read(phydev, MII_LPA);
770
771 if (lpa < 0)
772 return lpa;
773
774 adv = phy_read(phydev, MII_ADVERTISE);
775
776 if (adv < 0)
777 return adv;
778
779 lpa &= adv;
780
781 phydev->speed = SPEED_10;
782 phydev->duplex = DUPLEX_HALF;
783 phydev->pause = phydev->asym_pause = 0;
784
785 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
786 phydev->speed = SPEED_1000;
787
788 if (lpagb & LPA_1000FULL)
789 phydev->duplex = DUPLEX_FULL;
790 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
791 phydev->speed = SPEED_100;
792
793 if (lpa & LPA_100FULL)
794 phydev->duplex = DUPLEX_FULL;
795 } else
796 if (lpa & LPA_10FULL)
797 phydev->duplex = DUPLEX_FULL;
798
799 if (phydev->duplex == DUPLEX_FULL){
800 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
801 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
802 }
803 } else {
804 int bmcr = phy_read(phydev, MII_BMCR);
805 if (bmcr < 0)
806 return bmcr;
807
808 if (bmcr & BMCR_FULLDPLX)
809 phydev->duplex = DUPLEX_FULL;
810 else
811 phydev->duplex = DUPLEX_HALF;
812
813 if (bmcr & BMCR_SPEED1000)
814 phydev->speed = SPEED_1000;
815 else if (bmcr & BMCR_SPEED100)
816 phydev->speed = SPEED_100;
817 else
818 phydev->speed = SPEED_10;
819
820 phydev->pause = phydev->asym_pause = 0;
821 }
822
823 return 0;
824}
825EXPORT_SYMBOL(genphy_read_status);
826
827static int genphy_config_init(struct phy_device *phydev)
828{
Eric Sesterhenn84c22d72006-09-25 16:39:22 -0700829 int val;
Andy Fleming00db8182005-07-30 19:31:23 -0400830 u32 features;
831
832 /* For now, I'll claim that the generic driver supports
833 * all possible port types */
834 features = (SUPPORTED_TP | SUPPORTED_MII
835 | SUPPORTED_AUI | SUPPORTED_FIBRE |
836 SUPPORTED_BNC);
837
838 /* Do we support autonegotiation? */
839 val = phy_read(phydev, MII_BMSR);
840
841 if (val < 0)
842 return val;
843
844 if (val & BMSR_ANEGCAPABLE)
845 features |= SUPPORTED_Autoneg;
846
847 if (val & BMSR_100FULL)
848 features |= SUPPORTED_100baseT_Full;
849 if (val & BMSR_100HALF)
850 features |= SUPPORTED_100baseT_Half;
851 if (val & BMSR_10FULL)
852 features |= SUPPORTED_10baseT_Full;
853 if (val & BMSR_10HALF)
854 features |= SUPPORTED_10baseT_Half;
855
856 if (val & BMSR_ESTATEN) {
857 val = phy_read(phydev, MII_ESTATUS);
858
859 if (val < 0)
860 return val;
861
862 if (val & ESTATUS_1000_TFULL)
863 features |= SUPPORTED_1000baseT_Full;
864 if (val & ESTATUS_1000_THALF)
865 features |= SUPPORTED_1000baseT_Half;
866 }
867
868 phydev->supported = features;
869 phydev->advertising = features;
870
871 return 0;
872}
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800873int genphy_suspend(struct phy_device *phydev)
874{
875 int value;
Andy Fleming00db8182005-07-30 19:31:23 -0400876
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800877 mutex_lock(&phydev->lock);
878
879 value = phy_read(phydev, MII_BMCR);
880 phy_write(phydev, MII_BMCR, (value | BMCR_PDOWN));
881
882 mutex_unlock(&phydev->lock);
883
884 return 0;
885}
886EXPORT_SYMBOL(genphy_suspend);
887
888int genphy_resume(struct phy_device *phydev)
889{
890 int value;
891
892 mutex_lock(&phydev->lock);
893
894 value = phy_read(phydev, MII_BMCR);
895 phy_write(phydev, MII_BMCR, (value & ~BMCR_PDOWN));
896
897 mutex_unlock(&phydev->lock);
898
899 return 0;
900}
901EXPORT_SYMBOL(genphy_resume);
Andy Fleming00db8182005-07-30 19:31:23 -0400902
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800903/**
904 * phy_probe - probe and init a PHY device
905 * @dev: device to probe and init
Andy Fleming00db8182005-07-30 19:31:23 -0400906 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800907 * Description: Take care of setting up the phy_device structure,
Andy Fleming00db8182005-07-30 19:31:23 -0400908 * set the state to READY (the driver's init function should
909 * set it to STARTING if needed).
910 */
911static int phy_probe(struct device *dev)
912{
913 struct phy_device *phydev;
914 struct phy_driver *phydrv;
915 struct device_driver *drv;
916 int err = 0;
917
918 phydev = to_phy_device(dev);
919
Alan Sternf3ff9242012-01-24 13:35:24 -0500920 drv = phydev->dev.driver;
Andy Fleming00db8182005-07-30 19:31:23 -0400921 phydrv = to_phy_driver(drv);
922 phydev->drv = phydrv;
923
924 /* Disable the interrupt if the PHY doesn't support it */
925 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
926 phydev->irq = PHY_POLL;
927
Nate Case35b5f6b2008-01-29 10:05:09 -0600928 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400929
930 /* Start out supporting everything. Eventually,
931 * a controller will attach, and may modify one
932 * or both of these values */
933 phydev->supported = phydrv->features;
934 phydev->advertising = phydrv->features;
935
936 /* Set the state to READY by default */
937 phydev->state = PHY_READY;
938
939 if (phydev->drv->probe)
940 err = phydev->drv->probe(phydev);
941
Nate Case35b5f6b2008-01-29 10:05:09 -0600942 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400943
Andy Fleming00db8182005-07-30 19:31:23 -0400944 return err;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600945
Andy Fleming00db8182005-07-30 19:31:23 -0400946}
947
948static int phy_remove(struct device *dev)
949{
950 struct phy_device *phydev;
951
952 phydev = to_phy_device(dev);
953
Nate Case35b5f6b2008-01-29 10:05:09 -0600954 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400955 phydev->state = PHY_DOWN;
Nate Case35b5f6b2008-01-29 10:05:09 -0600956 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400957
958 if (phydev->drv->remove)
959 phydev->drv->remove(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400960 phydev->drv = NULL;
961
962 return 0;
963}
964
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800965/**
966 * phy_driver_register - register a phy_driver with the PHY layer
967 * @new_driver: new phy_driver to register
968 */
Andy Fleming00db8182005-07-30 19:31:23 -0400969int phy_driver_register(struct phy_driver *new_driver)
970{
971 int retval;
972
Andy Fleming00db8182005-07-30 19:31:23 -0400973 new_driver->driver.name = new_driver->name;
974 new_driver->driver.bus = &mdio_bus_type;
975 new_driver->driver.probe = phy_probe;
976 new_driver->driver.remove = phy_remove;
977
978 retval = driver_register(&new_driver->driver);
979
980 if (retval) {
Joe Perches8d242482012-06-09 07:49:07 +0000981 pr_err("%s: Error %d in registering driver\n",
982 new_driver->name, retval);
Andy Fleming00db8182005-07-30 19:31:23 -0400983
984 return retval;
985 }
986
Olof Johanssonf2511f12007-11-04 16:09:23 -0600987 pr_debug("%s: Registered new driver\n", new_driver->name);
Andy Fleming00db8182005-07-30 19:31:23 -0400988
989 return 0;
990}
991EXPORT_SYMBOL(phy_driver_register);
992
993void phy_driver_unregister(struct phy_driver *drv)
994{
995 driver_unregister(&drv->driver);
996}
997EXPORT_SYMBOL(phy_driver_unregister);
998
Andy Fleminge1393452005-08-24 18:46:21 -0500999static struct phy_driver genphy_driver = {
1000 .phy_id = 0xffffffff,
1001 .phy_id_mask = 0xffffffff,
1002 .name = "Generic PHY",
1003 .config_init = genphy_config_init,
1004 .features = 0,
1005 .config_aneg = genphy_config_aneg,
1006 .read_status = genphy_read_status,
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001007 .suspend = genphy_suspend,
1008 .resume = genphy_resume,
Andy Fleminge1393452005-08-24 18:46:21 -05001009 .driver = {.owner= THIS_MODULE, },
1010};
Andy Fleming00db8182005-07-30 19:31:23 -04001011
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001012static int __init phy_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -04001013{
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001014 int rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001015
1016 rc = mdio_bus_init();
1017 if (rc)
Andy Fleminge1393452005-08-24 18:46:21 -05001018 return rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001019
Andy Fleminge1393452005-08-24 18:46:21 -05001020 rc = phy_driver_register(&genphy_driver);
1021 if (rc)
1022 mdio_bus_exit();
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001023
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001024 return rc;
Andy Fleming00db8182005-07-30 19:31:23 -04001025}
1026
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001027static void __exit phy_exit(void)
Andy Fleming00db8182005-07-30 19:31:23 -04001028{
1029 phy_driver_unregister(&genphy_driver);
Andy Fleminge1393452005-08-24 18:46:21 -05001030 mdio_bus_exit();
Andy Fleming00db8182005-07-30 19:31:23 -04001031}
1032
Andy Fleminge1393452005-08-24 18:46:21 -05001033subsys_initcall(phy_init);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001034module_exit(phy_exit);