blob: 1cde2b2aead0a45e260585bba3466622a3740d1c [file] [log] [blame]
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001/* Framework for finding and configuring PHYs.
Andy Fleming00db8182005-07-30 19:31:23 -04002 * Also contains generic PHY driver
3 *
4 * Author: Andy Fleming
5 *
6 * Copyright (c) 2004 Freescale Semiconductor, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 */
Joe Perches8d242482012-06-09 07:49:07 +000014
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030033#include <linux/io.h>
34#include <linux/uaccess.h>
Andy Fleming00db8182005-07-30 19:31:23 -040035
Andy Fleming00db8182005-07-30 19:31:23 -040036#include <asm/irq.h>
Andy Fleming00db8182005-07-30 19:31:23 -040037
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{
Petr Malatb2a43192013-02-28 01:01:52 +000044 put_device(&phydev->dev);
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030045}
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{
Petr Malatb2a43192013-02-28 01:01:52 +000050 kfree(to_phy_device(dev));
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030051}
52
Shaohui Xieab2145e2014-01-10 14:27:22 +080053enum genphy_driver {
54 GENPHY_DRV_1G,
55 GENPHY_DRV_MAX
56};
57
58static struct phy_driver genphy_driver[GENPHY_DRV_MAX];
Grant Likely4dea5472009-04-25 12:52:46 +000059
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
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030066/**
67 * phy_register_fixup - creates a new phy_fixup and adds it to the list
Andy Flemingf62220d2008-04-18 17:29:54 -050068 * @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)
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030070 * It can also be PHY_ANY_UID
Andy Flemingf62220d2008-04-18 17:29:54 -050071 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030072 * comparison
Andy Flemingf62220d2008-04-18 17:29:54 -050073 * @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,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030076 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -050077{
Sergei Shtylyov553fe922014-01-05 03:23:19 +030078 struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
Andy Flemingf62220d2008-04-18 17:29:54 -050079
Andy Flemingf62220d2008-04-18 17:29:54 -050080 if (!fixup)
81 return -ENOMEM;
82
Kay Sieversfb28ad32008-11-10 13:55:14 -080083 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
Andy Flemingf62220d2008-04-18 17:29:54 -050084 fixup->phy_uid = phy_uid;
85 fixup->phy_uid_mask = phy_uid_mask;
86 fixup->run = run;
87
88 mutex_lock(&phy_fixup_lock);
89 list_add_tail(&fixup->list, &phy_fixup_list);
90 mutex_unlock(&phy_fixup_lock);
91
92 return 0;
93}
94EXPORT_SYMBOL(phy_register_fixup);
95
96/* Registers a fixup to be run on any PHY with the UID in phy_uid */
97int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030098 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -050099{
100 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
101}
102EXPORT_SYMBOL(phy_register_fixup_for_uid);
103
104/* Registers a fixup to be run on the PHY with id string bus_id */
105int phy_register_fixup_for_id(const char *bus_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300106 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -0500107{
108 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
109}
110EXPORT_SYMBOL(phy_register_fixup_for_id);
111
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300112/* Returns 1 if fixup matches phydev in bus_id and phy_uid.
Andy Flemingf62220d2008-04-18 17:29:54 -0500113 * Fixups can be set to match any in one or more fields.
114 */
115static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
116{
Kay Sieversfb28ad32008-11-10 13:55:14 -0800117 if (strcmp(fixup->bus_id, dev_name(&phydev->dev)) != 0)
Andy Flemingf62220d2008-04-18 17:29:54 -0500118 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
119 return 0;
120
121 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300122 (phydev->phy_id & fixup->phy_uid_mask))
Andy Flemingf62220d2008-04-18 17:29:54 -0500123 if (fixup->phy_uid != PHY_ANY_UID)
124 return 0;
125
126 return 1;
127}
128
129/* Runs any matching fixups for this phydev */
Sergei Shtylyovfbfcec62014-01-05 03:28:27 +0300130static int phy_scan_fixups(struct phy_device *phydev)
Andy Flemingf62220d2008-04-18 17:29:54 -0500131{
132 struct phy_fixup *fixup;
133
134 mutex_lock(&phy_fixup_lock);
135 list_for_each_entry(fixup, &phy_fixup_list, list) {
136 if (phy_needs_fixup(phydev, fixup)) {
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300137 int err = fixup->run(phydev);
Andy Flemingf62220d2008-04-18 17:29:54 -0500138
Jiri Slabybc232832009-07-13 11:23:39 +0000139 if (err < 0) {
140 mutex_unlock(&phy_fixup_lock);
Andy Flemingf62220d2008-04-18 17:29:54 -0500141 return err;
Jiri Slabybc232832009-07-13 11:23:39 +0000142 }
Andy Flemingf62220d2008-04-18 17:29:54 -0500143 }
144 }
145 mutex_unlock(&phy_fixup_lock);
146
147 return 0;
148}
Andy Flemingf62220d2008-04-18 17:29:54 -0500149
David Daneyac28b9f2012-06-27 07:33:35 +0000150struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300151 bool is_c45,
152 struct phy_c45_device_ids *c45_ids)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700153{
154 struct phy_device *dev;
David Woodhouse8626d3b2010-04-02 01:05:27 +0000155
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300156 /* We allocate the device, and initialize the default values */
Robert P. J. Daycd861282006-12-13 00:34:52 -0800157 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700158 if (NULL == dev)
Florian Fainellie1093742013-12-17 21:38:12 -0800159 return (struct phy_device *)PTR_ERR((void *)-ENOMEM);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700160
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300161 dev->dev.release = phy_device_release;
162
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700163 dev->speed = 0;
164 dev->duplex = -1;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300165 dev->pause = 0;
166 dev->asym_pause = 0;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700167 dev->link = 1;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600168 dev->interface = PHY_INTERFACE_MODE_GMII;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700169
170 dev->autoneg = AUTONEG_ENABLE;
171
David Daneyac28b9f2012-06-27 07:33:35 +0000172 dev->is_c45 = is_c45;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700173 dev->addr = addr;
174 dev->phy_id = phy_id;
David Daneyac28b9f2012-06-27 07:33:35 +0000175 if (c45_ids)
176 dev->c45_ids = *c45_ids;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700177 dev->bus = bus;
Grant Likely4dea5472009-04-25 12:52:46 +0000178 dev->dev.parent = bus->parent;
179 dev->dev.bus = &mdio_bus_type;
180 dev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
181 dev_set_name(&dev->dev, PHY_ID_FMT, bus->id, addr);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700182
183 dev->state = PHY_DOWN;
184
Nate Case35b5f6b2008-01-29 10:05:09 -0600185 mutex_init(&dev->lock);
Anton Vorontsov4f9c85a2010-01-18 05:37:16 +0000186 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
Florian Fainelli5ea94e72013-05-19 22:53:43 +0000187 INIT_WORK(&dev->phy_queue, phy_change);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700188
David Woodhouse8626d3b2010-04-02 01:05:27 +0000189 /* Request the appropriate module unconditionally; don't
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300190 * bother trying to do so only if it isn't already loaded,
191 * because that gets complicated. A hotplug event would have
192 * done an unconditional modprobe anyway.
193 * We don't do normal hotplug because it won't work for MDIO
194 * -- because it relies on the device staying around for long
195 * enough for the driver to get loaded. With MDIO, the NIC
196 * driver will get bored and give up as soon as it finds that
197 * there's no driver _already_ loaded.
198 */
David Woodhouse8626d3b2010-04-02 01:05:27 +0000199 request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
200
Petr Malatb2a43192013-02-28 01:01:52 +0000201 device_initialize(&dev->dev);
202
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700203 return dev;
204}
David Daneyac28b9f2012-06-27 07:33:35 +0000205EXPORT_SYMBOL(phy_device_create);
206
207/**
208 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
209 * @bus: the target MII bus
210 * @addr: PHY address on the MII bus
211 * @phy_id: where to store the ID retrieved.
212 * @c45_ids: where to store the c45 ID information.
213 *
214 * If the PHY devices-in-package appears to be valid, it and the
215 * corresponding identifiers are stored in @c45_ids, zero is stored
216 * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns
217 * zero on success.
218 *
219 */
220static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
221 struct phy_c45_device_ids *c45_ids) {
222 int phy_reg;
223 int i, reg_addr;
224 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
225
226 /* Find first non-zero Devices In package. Device
227 * zero is reserved, so don't probe it.
228 */
229 for (i = 1;
230 i < num_ids && c45_ids->devices_in_package == 0;
231 i++) {
232 reg_addr = MII_ADDR_C45 | i << 16 | 6;
233 phy_reg = mdiobus_read(bus, addr, reg_addr);
234 if (phy_reg < 0)
235 return -EIO;
236 c45_ids->devices_in_package = (phy_reg & 0xffff) << 16;
237
238 reg_addr = MII_ADDR_C45 | i << 16 | 5;
239 phy_reg = mdiobus_read(bus, addr, reg_addr);
240 if (phy_reg < 0)
241 return -EIO;
242 c45_ids->devices_in_package |= (phy_reg & 0xffff);
243
244 /* If mostly Fs, there is no device there,
245 * let's get out of here.
246 */
247 if ((c45_ids->devices_in_package & 0x1fffffff) == 0x1fffffff) {
248 *phy_id = 0xffffffff;
249 return 0;
250 }
251 }
252
253 /* Now probe Device Identifiers for each device present. */
254 for (i = 1; i < num_ids; i++) {
255 if (!(c45_ids->devices_in_package & (1 << i)))
256 continue;
257
258 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
259 phy_reg = mdiobus_read(bus, addr, reg_addr);
260 if (phy_reg < 0)
261 return -EIO;
262 c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16;
263
264 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
265 phy_reg = mdiobus_read(bus, addr, reg_addr);
266 if (phy_reg < 0)
267 return -EIO;
268 c45_ids->device_ids[i] |= (phy_reg & 0xffff);
269 }
270 *phy_id = 0;
271 return 0;
272}
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700273
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800274/**
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400275 * get_phy_id - reads the specified addr for its ID.
276 * @bus: the target MII bus
277 * @addr: PHY address on the MII bus
278 * @phy_id: where to store the ID retrieved.
David Daneyac28b9f2012-06-27 07:33:35 +0000279 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
280 * @c45_ids: where to store the c45 ID information.
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400281 *
David Daneyac28b9f2012-06-27 07:33:35 +0000282 * Description: In the case of a 802.3-c22 PHY, reads the ID registers
283 * of the PHY at @addr on the @bus, stores it in @phy_id and returns
284 * zero on success.
285 *
286 * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
287 * its return value is in turn returned.
288 *
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400289 */
David Daneyac28b9f2012-06-27 07:33:35 +0000290static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
291 bool is_c45, struct phy_c45_device_ids *c45_ids)
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400292{
293 int phy_reg;
294
David Daneyac28b9f2012-06-27 07:33:35 +0000295 if (is_c45)
296 return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
297
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300298 /* Grab the bits from PHYIR1, and put them in the upper half */
David Daney6fe32642011-09-30 11:51:22 +0000299 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400300 if (phy_reg < 0)
301 return -EIO;
302
303 *phy_id = (phy_reg & 0xffff) << 16;
304
305 /* Grab the bits from PHYIR2, and put them in the lower half */
David Daney6fe32642011-09-30 11:51:22 +0000306 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400307 if (phy_reg < 0)
308 return -EIO;
309
310 *phy_id |= (phy_reg & 0xffff);
311
312 return 0;
313}
314
315/**
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300316 * get_phy_device - reads the specified PHY device and returns its @phy_device
317 * struct
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800318 * @bus: the target MII bus
319 * @addr: PHY address on the MII bus
David Daneyac28b9f2012-06-27 07:33:35 +0000320 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
Andy Fleming00db8182005-07-30 19:31:23 -0400321 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800322 * Description: Reads the ID registers of the PHY at @addr on the
323 * @bus, then allocates and returns the phy_device to represent it.
Andy Fleming00db8182005-07-30 19:31:23 -0400324 */
David Daneyac28b9f2012-06-27 07:33:35 +0000325struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
Andy Fleming00db8182005-07-30 19:31:23 -0400326{
David Daneyac28b9f2012-06-27 07:33:35 +0000327 struct phy_c45_device_ids c45_ids = {0};
David S. Miller160c85f2012-06-27 21:28:14 -0700328 u32 phy_id = 0;
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400329 int r;
Andy Fleming00db8182005-07-30 19:31:23 -0400330
David Daneyac28b9f2012-06-27 07:33:35 +0000331 r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400332 if (r)
333 return ERR_PTR(r);
Andy Fleming00db8182005-07-30 19:31:23 -0400334
Giuseppe Cavallaro6436cbc2008-11-20 20:43:18 -0800335 /* If the phy_id is mostly Fs, there is no device there */
336 if ((phy_id & 0x1fffffff) == 0x1fffffff)
337 return NULL;
338
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300339 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
Andy Fleming00db8182005-07-30 19:31:23 -0400340}
Grant Likely4dea5472009-04-25 12:52:46 +0000341EXPORT_SYMBOL(get_phy_device);
342
343/**
344 * phy_device_register - Register the phy device on the MDIO bus
Randy Dunlap1d4ac5d2009-06-16 16:56:33 +0000345 * @phydev: phy_device structure to be added to the MDIO bus
Grant Likely4dea5472009-04-25 12:52:46 +0000346 */
347int phy_device_register(struct phy_device *phydev)
348{
349 int err;
350
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300351 /* Don't register a phy if one is already registered at this address */
Grant Likely4dea5472009-04-25 12:52:46 +0000352 if (phydev->bus->phy_map[phydev->addr])
353 return -EINVAL;
354 phydev->bus->phy_map[phydev->addr] = phydev;
355
356 /* Run all of the fixups for this PHY */
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800357 err = phy_init_hw(phydev);
358 if (err) {
359 pr_err("PHY %d failed to initialize\n", phydev->addr);
360 goto out;
361 }
Grant Likely4dea5472009-04-25 12:52:46 +0000362
Petr Malatb2a43192013-02-28 01:01:52 +0000363 err = device_add(&phydev->dev);
Grant Likely4dea5472009-04-25 12:52:46 +0000364 if (err) {
Petr Malatb2a43192013-02-28 01:01:52 +0000365 pr_err("PHY %d failed to add\n", phydev->addr);
Grant Likely4dea5472009-04-25 12:52:46 +0000366 goto out;
367 }
368
369 return 0;
370
371 out:
372 phydev->bus->phy_map[phydev->addr] = NULL;
373 return err;
374}
375EXPORT_SYMBOL(phy_device_register);
Andy Fleming00db8182005-07-30 19:31:23 -0400376
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800377/**
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800378 * phy_find_first - finds the first PHY device on the bus
379 * @bus: the target MII bus
380 */
381struct phy_device *phy_find_first(struct mii_bus *bus)
382{
383 int addr;
384
385 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
386 if (bus->phy_map[addr])
387 return bus->phy_map[addr];
388 }
389 return NULL;
390}
391EXPORT_SYMBOL(phy_find_first);
392
393/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800394 * phy_prepare_link - prepares the PHY layer to monitor link status
395 * @phydev: target phy_device struct
396 * @handler: callback function for link status change notifications
Andy Fleming00db8182005-07-30 19:31:23 -0400397 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800398 * Description: Tells the PHY infrastructure to handle the
Andy Fleming00db8182005-07-30 19:31:23 -0400399 * gory details on monitoring link status (whether through
400 * polling or an interrupt), and to call back to the
401 * connected device driver when the link status changes.
402 * If you want to monitor your own link state, don't call
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800403 * this function.
404 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000405static void phy_prepare_link(struct phy_device *phydev,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300406 void (*handler)(struct net_device *))
Andy Fleming00db8182005-07-30 19:31:23 -0400407{
408 phydev->adjust_link = handler;
409}
410
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800411/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000412 * phy_connect_direct - connect an ethernet device to a specific phy_device
413 * @dev: the network device to connect
414 * @phydev: the pointer to the phy device
415 * @handler: callback function for state change notifications
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000416 * @interface: PHY device's interface
417 */
418int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
Florian Fainellif9a8f832013-01-14 00:52:52 +0000419 void (*handler)(struct net_device *),
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000420 phy_interface_t interface)
421{
422 int rc;
423
Florian Fainellif9a8f832013-01-14 00:52:52 +0000424 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000425 if (rc)
426 return rc;
427
428 phy_prepare_link(phydev, handler);
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300429 phy_start_machine(phydev);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000430 if (phydev->irq > 0)
431 phy_start_interrupts(phydev);
432
433 return 0;
434}
435EXPORT_SYMBOL(phy_connect_direct);
436
437/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800438 * phy_connect - connect an ethernet device to a PHY device
439 * @dev: the network device to connect
Randy Dunlap5d12b132008-04-28 10:58:22 -0700440 * @bus_id: the id string of the PHY device to connect
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800441 * @handler: callback function for state change notifications
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800442 * @interface: PHY device's interface
Andy Fleminge1393452005-08-24 18:46:21 -0500443 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800444 * Description: Convenience function for connecting ethernet
Andy Fleminge1393452005-08-24 18:46:21 -0500445 * devices to PHY devices. The default behavior is for
446 * the PHY infrastructure to handle everything, and only notify
447 * the connected driver when the link status changes. If you
448 * don't want, or can't use the provided functionality, you may
449 * choose to call only the subset of functions which provide
450 * the desired functionality.
451 */
Florian Fainellie1093742013-12-17 21:38:12 -0800452struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300453 void (*handler)(struct net_device *),
454 phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500455{
456 struct phy_device *phydev;
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000457 struct device *d;
458 int rc;
Andy Fleminge1393452005-08-24 18:46:21 -0500459
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000460 /* Search the list of PHY devices on the mdio bus for the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300461 * PHY with the requested name
462 */
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000463 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
464 if (!d) {
465 pr_err("PHY %s not found\n", bus_id);
466 return ERR_PTR(-ENODEV);
467 }
468 phydev = to_phy_device(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500469
Florian Fainellif9a8f832013-01-14 00:52:52 +0000470 rc = phy_connect_direct(dev, phydev, handler, interface);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000471 if (rc)
472 return ERR_PTR(rc);
Andy Fleminge1393452005-08-24 18:46:21 -0500473
474 return phydev;
475}
476EXPORT_SYMBOL(phy_connect);
477
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800478/**
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300479 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
480 * device
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800481 * @phydev: target phy_device struct
482 */
Andy Fleminge1393452005-08-24 18:46:21 -0500483void phy_disconnect(struct phy_device *phydev)
484{
485 if (phydev->irq > 0)
486 phy_stop_interrupts(phydev);
487
488 phy_stop_machine(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800489
Andy Fleminge1393452005-08-24 18:46:21 -0500490 phydev->adjust_link = NULL;
491
492 phy_detach(phydev);
493}
494EXPORT_SYMBOL(phy_disconnect);
495
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800496/**
497 * phy_poll_reset - Safely wait until a PHY reset has properly completed
498 * @phydev: The PHY device to poll
499 *
500 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
501 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
502 * register must be polled until the BMCR_RESET bit clears.
503 *
504 * Furthermore, any attempts to write to PHY registers may have no effect
505 * or even generate MDIO bus errors until this is complete.
506 *
507 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
508 * standard and do not fully reset after the BMCR_RESET bit is set, and may
509 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
510 * effort to support such broken PHYs, this function is separate from the
511 * standard phy_init_hw() which will zero all the other bits in the BMCR
512 * and reapply all driver-specific and board-specific fixups.
513 */
514static int phy_poll_reset(struct phy_device *phydev)
515{
516 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
517 unsigned int retries = 12;
518 int ret;
519
520 do {
521 msleep(50);
522 ret = phy_read(phydev, MII_BMCR);
523 if (ret < 0)
524 return ret;
525 } while (ret & BMCR_RESET && --retries);
526 if (ret & BMCR_RESET)
527 return -ETIMEDOUT;
528
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300529 /* Some chips (smsc911x) may still need up to another 1ms after the
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800530 * BMCR_RESET bit is cleared before they are usable.
531 */
532 msleep(1);
533 return 0;
534}
535
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000536int phy_init_hw(struct phy_device *phydev)
537{
538 int ret;
539
540 if (!phydev->drv || !phydev->drv->config_init)
541 return 0;
542
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800543 ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
544 if (ret < 0)
545 return ret;
546
547 ret = phy_poll_reset(phydev);
548 if (ret < 0)
549 return ret;
550
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000551 ret = phy_scan_fixups(phydev);
552 if (ret < 0)
553 return ret;
554
555 return phydev->drv->config_init(phydev);
556}
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800557EXPORT_SYMBOL(phy_init_hw);
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000558
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800559/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000560 * phy_attach_direct - attach a network device to a given PHY device pointer
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800561 * @dev: network device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000562 * @phydev: Pointer to phy_device to attach
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800563 * @flags: PHY device's dev_flags
564 * @interface: PHY device's interface
565 *
566 * Description: Called by drivers to attach to a particular PHY
567 * device. The phy_device is found, and properly hooked up
568 * to the phy_driver. If no driver is attached, then the
569 * genphy_driver is used. The phy_device is given a ptr to
570 * the attaching device, and given a callback for link status
571 * change. The phy_device is returned to the attaching driver.
572 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000573static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
574 u32 flags, phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500575{
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000576 struct device *d = &phydev->dev;
Marc Kleine-Budded005a092011-03-28 14:54:08 +0000577 int err;
Andy Fleminge1393452005-08-24 18:46:21 -0500578
579 /* Assume that if there is no driver, that it doesn't
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300580 * exist, and we should use the genphy driver.
581 */
Andy Fleminge1393452005-08-24 18:46:21 -0500582 if (NULL == d->driver) {
David Daneyac28b9f2012-06-27 07:33:35 +0000583 if (phydev->is_c45) {
584 pr_err("No driver for phy %x\n", phydev->phy_id);
585 return -ENODEV;
586 }
587
Shaohui Xieab2145e2014-01-10 14:27:22 +0800588 d->driver = &genphy_driver[GENPHY_DRV_1G].driver;
Andy Fleminge1393452005-08-24 18:46:21 -0500589
590 err = d->driver->probe(d);
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400591 if (err >= 0)
592 err = device_bind_driver(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500593
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400594 if (err)
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000595 return err;
Andy Fleminge1393452005-08-24 18:46:21 -0500596 }
597
598 if (phydev->attached_dev) {
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000599 dev_err(&dev->dev, "PHY already attached\n");
600 return -EBUSY;
Andy Fleminge1393452005-08-24 18:46:21 -0500601 }
602
603 phydev->attached_dev = dev;
Richard Cochranc1f19b52010-07-17 08:49:36 +0000604 dev->phydev = phydev;
Andy Fleminge1393452005-08-24 18:46:21 -0500605
606 phydev->dev_flags = flags;
607
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600608 phydev->interface = interface;
609
Anton Vorontsovef24b162010-08-24 14:46:12 -0700610 phydev->state = PHY_READY;
611
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600612 /* Do initial configuration here, now that
613 * we have certain key parameters
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300614 * (dev_flags and interface)
615 */
Marc Kleine-Budded005a092011-03-28 14:54:08 +0000616 err = phy_init_hw(phydev);
617 if (err)
618 phy_detach(phydev);
619
Sebastian Hesselbarth1211ce52013-12-13 10:20:28 +0100620 phy_resume(phydev);
621
Marc Kleine-Budded005a092011-03-28 14:54:08 +0000622 return err;
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000623}
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000624
625/**
626 * phy_attach - attach a network device to a particular PHY device
627 * @dev: network device to attach
628 * @bus_id: Bus ID of PHY device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000629 * @interface: PHY device's interface
630 *
631 * Description: Same as phy_attach_direct() except that a PHY bus_id
632 * string is passed instead of a pointer to a struct phy_device.
633 */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300634struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
635 phy_interface_t interface)
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000636{
637 struct bus_type *bus = &mdio_bus_type;
638 struct phy_device *phydev;
639 struct device *d;
640 int rc;
641
642 /* Search the list of PHY devices on the mdio bus for the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300643 * PHY with the requested name
644 */
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000645 d = bus_find_device_by_name(bus, NULL, bus_id);
646 if (!d) {
647 pr_err("PHY %s not found\n", bus_id);
648 return ERR_PTR(-ENODEV);
649 }
650 phydev = to_phy_device(d);
651
Florian Fainellif9a8f832013-01-14 00:52:52 +0000652 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000653 if (rc)
654 return ERR_PTR(rc);
655
Andy Fleminge1393452005-08-24 18:46:21 -0500656 return phydev;
657}
658EXPORT_SYMBOL(phy_attach);
659
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800660/**
661 * phy_detach - detach a PHY device from its network device
662 * @phydev: target phy_device struct
663 */
Andy Fleminge1393452005-08-24 18:46:21 -0500664void phy_detach(struct phy_device *phydev)
665{
Shaohui Xieab2145e2014-01-10 14:27:22 +0800666 int i;
Richard Cochranc1f19b52010-07-17 08:49:36 +0000667 phydev->attached_dev->phydev = NULL;
Andy Fleminge1393452005-08-24 18:46:21 -0500668 phydev->attached_dev = NULL;
Sebastian Hesselbarth1211ce52013-12-13 10:20:28 +0100669 phy_suspend(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500670
671 /* If the device had no specific driver before (i.e. - it
672 * was using the generic driver), we unbind the device
673 * from the generic driver so that there's a chance a
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300674 * real driver could be loaded
675 */
Shaohui Xieab2145e2014-01-10 14:27:22 +0800676 for (i = 0; i < ARRAY_SIZE(genphy_driver); i++) {
677 if (phydev->dev.driver == &genphy_driver[i].driver) {
678 device_release_driver(&phydev->dev);
679 break;
680 }
681 }
Andy Fleminge1393452005-08-24 18:46:21 -0500682}
683EXPORT_SYMBOL(phy_detach);
684
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +0100685int phy_suspend(struct phy_device *phydev)
686{
687 struct phy_driver *phydrv = to_phy_driver(phydev->dev.driver);
688 struct ethtool_wolinfo wol;
689
690 /* If the device has WOL enabled, we cannot suspend the PHY */
691 wol.cmd = ETHTOOL_GWOL;
692 phy_ethtool_get_wol(phydev, &wol);
693 if (wol.wolopts)
694 return -EBUSY;
695
696 if (phydrv->suspend)
697 return phydrv->suspend(phydev);
698 return 0;
699}
700
701int phy_resume(struct phy_device *phydev)
702{
703 struct phy_driver *phydrv = to_phy_driver(phydev->dev.driver);
704
705 if (phydrv->resume)
706 return phydrv->resume(phydev);
707 return 0;
708}
Andy Fleminge1393452005-08-24 18:46:21 -0500709
Andy Fleming00db8182005-07-30 19:31:23 -0400710/* Generic PHY support and helper functions */
711
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800712/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300713 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800714 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400715 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800716 * Description: Writes MII_ADVERTISE with the appropriate values,
Andy Fleming00db8182005-07-30 19:31:23 -0400717 * after sanitizing the values to make sure we only advertise
Trent Piepho51e2a382008-09-24 10:55:46 +0000718 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
719 * hasn't changed, and > 0 if it has changed.
Andy Fleming00db8182005-07-30 19:31:23 -0400720 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000721static int genphy_config_advert(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400722{
723 u32 advertise;
Trent Piepho51e2a382008-09-24 10:55:46 +0000724 int oldadv, adv;
725 int err, changed = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400726
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300727 /* Only allow advertising what this PHY supports */
Andy Fleming00db8182005-07-30 19:31:23 -0400728 phydev->advertising &= phydev->supported;
729 advertise = phydev->advertising;
730
731 /* Setup standard advertisement */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300732 adv = phy_read(phydev, MII_ADVERTISE);
Andy Fleming00db8182005-07-30 19:31:23 -0400733 if (adv < 0)
734 return adv;
735
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300736 oldadv = adv;
Matt Carlson28011cf2011-11-16 18:36:59 -0500737 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
Andy Fleming00db8182005-07-30 19:31:23 -0400738 ADVERTISE_PAUSE_ASYM);
Matt Carlson37f07022011-11-17 14:30:55 +0000739 adv |= ethtool_adv_to_mii_adv_t(advertise);
Andy Fleming00db8182005-07-30 19:31:23 -0400740
Trent Piepho51e2a382008-09-24 10:55:46 +0000741 if (adv != oldadv) {
742 err = phy_write(phydev, MII_ADVERTISE, adv);
Andy Fleming00db8182005-07-30 19:31:23 -0400743
Trent Piepho51e2a382008-09-24 10:55:46 +0000744 if (err < 0)
745 return err;
746 changed = 1;
747 }
Andy Fleming00db8182005-07-30 19:31:23 -0400748
749 /* Configure gigabit if it's supported */
750 if (phydev->supported & (SUPPORTED_1000baseT_Half |
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300751 SUPPORTED_1000baseT_Full)) {
752 adv = phy_read(phydev, MII_CTRL1000);
Andy Fleming00db8182005-07-30 19:31:23 -0400753 if (adv < 0)
754 return adv;
755
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300756 oldadv = adv;
Andy Fleming00db8182005-07-30 19:31:23 -0400757 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
Matt Carlson37f07022011-11-17 14:30:55 +0000758 adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
Andy Fleming00db8182005-07-30 19:31:23 -0400759
Trent Piepho51e2a382008-09-24 10:55:46 +0000760 if (adv != oldadv) {
761 err = phy_write(phydev, MII_CTRL1000, adv);
762
763 if (err < 0)
764 return err;
765 changed = 1;
766 }
Andy Fleming00db8182005-07-30 19:31:23 -0400767 }
768
Trent Piepho51e2a382008-09-24 10:55:46 +0000769 return changed;
Andy Fleming00db8182005-07-30 19:31:23 -0400770}
Andy Fleming00db8182005-07-30 19:31:23 -0400771
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800772/**
773 * genphy_setup_forced - configures/forces speed/duplex from @phydev
774 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400775 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800776 * Description: Configures MII_BMCR to force speed/duplex
Andy Fleming00db8182005-07-30 19:31:23 -0400777 * to the values in phydev. Assumes that the values are valid.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800778 * Please see phy_sanitize_settings().
779 */
Madalin Bucur3fb69bc2013-11-20 16:38:19 -0600780int genphy_setup_forced(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400781{
Domen Puncerbc1e0a02007-08-17 08:54:45 +0200782 int ctl = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400783
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300784 phydev->pause = 0;
785 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400786
787 if (SPEED_1000 == phydev->speed)
788 ctl |= BMCR_SPEED1000;
789 else if (SPEED_100 == phydev->speed)
790 ctl |= BMCR_SPEED100;
791
792 if (DUPLEX_FULL == phydev->duplex)
793 ctl |= BMCR_FULLDPLX;
Florian Fainellie1093742013-12-17 21:38:12 -0800794
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300795 return phy_write(phydev, MII_BMCR, ctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400796}
Madalin Bucur3fb69bc2013-11-20 16:38:19 -0600797EXPORT_SYMBOL(genphy_setup_forced);
Andy Fleming00db8182005-07-30 19:31:23 -0400798
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800799/**
800 * genphy_restart_aneg - Enable and Restart Autonegotiation
801 * @phydev: target phy_device struct
802 */
Andy Fleming00db8182005-07-30 19:31:23 -0400803int genphy_restart_aneg(struct phy_device *phydev)
804{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300805 int ctl = phy_read(phydev, MII_BMCR);
Andy Fleming00db8182005-07-30 19:31:23 -0400806
807 if (ctl < 0)
808 return ctl;
809
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300810 ctl |= BMCR_ANENABLE | BMCR_ANRESTART;
Andy Fleming00db8182005-07-30 19:31:23 -0400811
812 /* Don't isolate the PHY if we're negotiating */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300813 ctl &= ~BMCR_ISOLATE;
Andy Fleming00db8182005-07-30 19:31:23 -0400814
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300815 return phy_write(phydev, MII_BMCR, ctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400816}
Adrian Bunk892871d2008-10-13 18:48:09 -0700817EXPORT_SYMBOL(genphy_restart_aneg);
Andy Fleming00db8182005-07-30 19:31:23 -0400818
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800819/**
820 * genphy_config_aneg - restart auto-negotiation or write BMCR
821 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400822 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800823 * Description: If auto-negotiation is enabled, we configure the
Andy Fleming00db8182005-07-30 19:31:23 -0400824 * advertising, and then restart auto-negotiation. If it is not
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800825 * enabled, then we write the BMCR.
Andy Fleming00db8182005-07-30 19:31:23 -0400826 */
827int genphy_config_aneg(struct phy_device *phydev)
828{
Trent Piephode339c22008-11-19 15:52:41 -0800829 int result;
Andy Fleming00db8182005-07-30 19:31:23 -0400830
Trent Piephode339c22008-11-19 15:52:41 -0800831 if (AUTONEG_ENABLE != phydev->autoneg)
832 return genphy_setup_forced(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400833
Trent Piephode339c22008-11-19 15:52:41 -0800834 result = genphy_config_advert(phydev);
Trent Piephode339c22008-11-19 15:52:41 -0800835 if (result < 0) /* error */
836 return result;
Trent Piephode339c22008-11-19 15:52:41 -0800837 if (result == 0) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300838 /* Advertisement hasn't changed, but maybe aneg was never on to
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300839 * begin with? Or maybe phy was isolated?
840 */
Trent Piephode339c22008-11-19 15:52:41 -0800841 int ctl = phy_read(phydev, MII_BMCR);
842
843 if (ctl < 0)
844 return ctl;
845
846 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
847 result = 1; /* do restart aneg */
848 }
849
850 /* Only restart aneg if we are advertising something different
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300851 * than we were before.
852 */
Trent Piephode339c22008-11-19 15:52:41 -0800853 if (result > 0)
854 result = genphy_restart_aneg(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400855
Trent Piepho51e2a382008-09-24 10:55:46 +0000856 return result;
Andy Fleming00db8182005-07-30 19:31:23 -0400857}
858EXPORT_SYMBOL(genphy_config_aneg);
859
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800860/**
861 * genphy_update_link - update link status in @phydev
862 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400863 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800864 * Description: Update the value in phydev->link to reflect the
Andy Fleming00db8182005-07-30 19:31:23 -0400865 * current link value. In order to do this, we need to read
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800866 * the status register twice, keeping the second value.
Andy Fleming00db8182005-07-30 19:31:23 -0400867 */
868int genphy_update_link(struct phy_device *phydev)
869{
870 int status;
871
872 /* Do a fake read */
873 status = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -0400874 if (status < 0)
875 return status;
876
877 /* Read link and autonegotiation status */
878 status = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -0400879 if (status < 0)
880 return status;
881
882 if ((status & BMSR_LSTATUS) == 0)
883 phydev->link = 0;
884 else
885 phydev->link = 1;
886
887 return 0;
888}
Andy Fleming6b655522006-10-16 16:19:17 -0500889EXPORT_SYMBOL(genphy_update_link);
Andy Fleming00db8182005-07-30 19:31:23 -0400890
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800891/**
892 * genphy_read_status - check the link status and update current link state
893 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400894 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800895 * Description: Check the link, then figure out the current state
Andy Fleming00db8182005-07-30 19:31:23 -0400896 * by comparing what we advertise with what the link partner
897 * advertises. Start by checking the gigabit possibilities,
898 * then move on to 10/100.
899 */
900int genphy_read_status(struct phy_device *phydev)
901{
902 int adv;
903 int err;
904 int lpa;
905 int lpagb = 0;
906
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300907 /* Update the link, but return if there was an error */
Andy Fleming00db8182005-07-30 19:31:23 -0400908 err = genphy_update_link(phydev);
909 if (err)
910 return err;
911
Florian Fainelli114002b2013-12-06 13:01:30 -0800912 phydev->lp_advertising = 0;
913
Andy Fleming00db8182005-07-30 19:31:23 -0400914 if (AUTONEG_ENABLE == phydev->autoneg) {
915 if (phydev->supported & (SUPPORTED_1000baseT_Half
916 | SUPPORTED_1000baseT_Full)) {
917 lpagb = phy_read(phydev, MII_STAT1000);
Andy Fleming00db8182005-07-30 19:31:23 -0400918 if (lpagb < 0)
919 return lpagb;
920
921 adv = phy_read(phydev, MII_CTRL1000);
Andy Fleming00db8182005-07-30 19:31:23 -0400922 if (adv < 0)
923 return adv;
924
Florian Fainelli114002b2013-12-06 13:01:30 -0800925 phydev->lp_advertising =
926 mii_stat1000_to_ethtool_lpa_t(lpagb);
Andy Fleming00db8182005-07-30 19:31:23 -0400927 lpagb &= adv << 2;
928 }
929
930 lpa = phy_read(phydev, MII_LPA);
Andy Fleming00db8182005-07-30 19:31:23 -0400931 if (lpa < 0)
932 return lpa;
933
Florian Fainelli114002b2013-12-06 13:01:30 -0800934 phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
935
Andy Fleming00db8182005-07-30 19:31:23 -0400936 adv = phy_read(phydev, MII_ADVERTISE);
Andy Fleming00db8182005-07-30 19:31:23 -0400937 if (adv < 0)
938 return adv;
939
940 lpa &= adv;
941
942 phydev->speed = SPEED_10;
943 phydev->duplex = DUPLEX_HALF;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300944 phydev->pause = 0;
945 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400946
947 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
948 phydev->speed = SPEED_1000;
949
950 if (lpagb & LPA_1000FULL)
951 phydev->duplex = DUPLEX_FULL;
952 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
953 phydev->speed = SPEED_100;
Florian Fainellie1093742013-12-17 21:38:12 -0800954
Andy Fleming00db8182005-07-30 19:31:23 -0400955 if (lpa & LPA_100FULL)
956 phydev->duplex = DUPLEX_FULL;
957 } else
958 if (lpa & LPA_10FULL)
959 phydev->duplex = DUPLEX_FULL;
960
Florian Fainellie1093742013-12-17 21:38:12 -0800961 if (phydev->duplex == DUPLEX_FULL) {
Andy Fleming00db8182005-07-30 19:31:23 -0400962 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
963 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
964 }
965 } else {
966 int bmcr = phy_read(phydev, MII_BMCR);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300967
Andy Fleming00db8182005-07-30 19:31:23 -0400968 if (bmcr < 0)
969 return bmcr;
970
971 if (bmcr & BMCR_FULLDPLX)
972 phydev->duplex = DUPLEX_FULL;
973 else
974 phydev->duplex = DUPLEX_HALF;
975
976 if (bmcr & BMCR_SPEED1000)
977 phydev->speed = SPEED_1000;
978 else if (bmcr & BMCR_SPEED100)
979 phydev->speed = SPEED_100;
980 else
981 phydev->speed = SPEED_10;
982
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300983 phydev->pause = 0;
984 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400985 }
986
987 return 0;
988}
989EXPORT_SYMBOL(genphy_read_status);
990
991static int genphy_config_init(struct phy_device *phydev)
992{
Eric Sesterhenn84c22d72006-09-25 16:39:22 -0700993 int val;
Andy Fleming00db8182005-07-30 19:31:23 -0400994 u32 features;
995
996 /* For now, I'll claim that the generic driver supports
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300997 * all possible port types
998 */
Andy Fleming00db8182005-07-30 19:31:23 -0400999 features = (SUPPORTED_TP | SUPPORTED_MII
1000 | SUPPORTED_AUI | SUPPORTED_FIBRE |
1001 SUPPORTED_BNC);
1002
1003 /* Do we support autonegotiation? */
1004 val = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -04001005 if (val < 0)
1006 return val;
1007
1008 if (val & BMSR_ANEGCAPABLE)
1009 features |= SUPPORTED_Autoneg;
1010
1011 if (val & BMSR_100FULL)
1012 features |= SUPPORTED_100baseT_Full;
1013 if (val & BMSR_100HALF)
1014 features |= SUPPORTED_100baseT_Half;
1015 if (val & BMSR_10FULL)
1016 features |= SUPPORTED_10baseT_Full;
1017 if (val & BMSR_10HALF)
1018 features |= SUPPORTED_10baseT_Half;
1019
1020 if (val & BMSR_ESTATEN) {
1021 val = phy_read(phydev, MII_ESTATUS);
Andy Fleming00db8182005-07-30 19:31:23 -04001022 if (val < 0)
1023 return val;
1024
1025 if (val & ESTATUS_1000_TFULL)
1026 features |= SUPPORTED_1000baseT_Full;
1027 if (val & ESTATUS_1000_THALF)
1028 features |= SUPPORTED_1000baseT_Half;
1029 }
1030
1031 phydev->supported = features;
1032 phydev->advertising = features;
1033
1034 return 0;
1035}
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001036int genphy_suspend(struct phy_device *phydev)
1037{
1038 int value;
Andy Fleming00db8182005-07-30 19:31:23 -04001039
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001040 mutex_lock(&phydev->lock);
1041
1042 value = phy_read(phydev, MII_BMCR);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001043 phy_write(phydev, MII_BMCR, value | BMCR_PDOWN);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001044
1045 mutex_unlock(&phydev->lock);
1046
1047 return 0;
1048}
1049EXPORT_SYMBOL(genphy_suspend);
1050
1051int genphy_resume(struct phy_device *phydev)
1052{
1053 int value;
1054
1055 mutex_lock(&phydev->lock);
1056
1057 value = phy_read(phydev, MII_BMCR);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001058 phy_write(phydev, MII_BMCR, value & ~BMCR_PDOWN);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001059
1060 mutex_unlock(&phydev->lock);
1061
1062 return 0;
1063}
1064EXPORT_SYMBOL(genphy_resume);
Andy Fleming00db8182005-07-30 19:31:23 -04001065
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001066/**
1067 * phy_probe - probe and init a PHY device
1068 * @dev: device to probe and init
Andy Fleming00db8182005-07-30 19:31:23 -04001069 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001070 * Description: Take care of setting up the phy_device structure,
Andy Fleming00db8182005-07-30 19:31:23 -04001071 * set the state to READY (the driver's init function should
1072 * set it to STARTING if needed).
1073 */
1074static int phy_probe(struct device *dev)
1075{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001076 struct phy_device *phydev = to_phy_device(dev);
1077 struct device_driver *drv = phydev->dev.driver;
1078 struct phy_driver *phydrv = to_phy_driver(drv);
Andy Fleming00db8182005-07-30 19:31:23 -04001079 int err = 0;
1080
Andy Fleming00db8182005-07-30 19:31:23 -04001081 phydev->drv = phydrv;
1082
Florian Fainelli2c7b4922013-05-19 22:53:42 +00001083 /* Disable the interrupt if the PHY doesn't support it
1084 * but the interrupt is still a valid one
1085 */
1086 if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001087 phy_interrupt_is_valid(phydev))
Andy Fleming00db8182005-07-30 19:31:23 -04001088 phydev->irq = PHY_POLL;
1089
Florian Fainelli4284b6a2013-05-23 01:11:12 +00001090 if (phydrv->flags & PHY_IS_INTERNAL)
1091 phydev->is_internal = true;
1092
Nate Case35b5f6b2008-01-29 10:05:09 -06001093 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001094
1095 /* Start out supporting everything. Eventually,
1096 * a controller will attach, and may modify one
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001097 * or both of these values
1098 */
Andy Fleming00db8182005-07-30 19:31:23 -04001099 phydev->supported = phydrv->features;
1100 phydev->advertising = phydrv->features;
1101
1102 /* Set the state to READY by default */
1103 phydev->state = PHY_READY;
1104
1105 if (phydev->drv->probe)
1106 err = phydev->drv->probe(phydev);
1107
Nate Case35b5f6b2008-01-29 10:05:09 -06001108 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001109
Andy Fleming00db8182005-07-30 19:31:23 -04001110 return err;
1111}
1112
1113static int phy_remove(struct device *dev)
1114{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001115 struct phy_device *phydev = to_phy_device(dev);
Andy Fleming00db8182005-07-30 19:31:23 -04001116
Nate Case35b5f6b2008-01-29 10:05:09 -06001117 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001118 phydev->state = PHY_DOWN;
Nate Case35b5f6b2008-01-29 10:05:09 -06001119 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001120
1121 if (phydev->drv->remove)
1122 phydev->drv->remove(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04001123 phydev->drv = NULL;
1124
1125 return 0;
1126}
1127
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001128/**
1129 * phy_driver_register - register a phy_driver with the PHY layer
1130 * @new_driver: new phy_driver to register
1131 */
Andy Fleming00db8182005-07-30 19:31:23 -04001132int phy_driver_register(struct phy_driver *new_driver)
1133{
1134 int retval;
1135
Andy Fleming00db8182005-07-30 19:31:23 -04001136 new_driver->driver.name = new_driver->name;
1137 new_driver->driver.bus = &mdio_bus_type;
1138 new_driver->driver.probe = phy_probe;
1139 new_driver->driver.remove = phy_remove;
1140
1141 retval = driver_register(&new_driver->driver);
Andy Fleming00db8182005-07-30 19:31:23 -04001142 if (retval) {
Joe Perches8d242482012-06-09 07:49:07 +00001143 pr_err("%s: Error %d in registering driver\n",
1144 new_driver->name, retval);
Andy Fleming00db8182005-07-30 19:31:23 -04001145
1146 return retval;
1147 }
1148
Olof Johanssonf2511f12007-11-04 16:09:23 -06001149 pr_debug("%s: Registered new driver\n", new_driver->name);
Andy Fleming00db8182005-07-30 19:31:23 -04001150
1151 return 0;
1152}
1153EXPORT_SYMBOL(phy_driver_register);
1154
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00001155int phy_drivers_register(struct phy_driver *new_driver, int n)
1156{
1157 int i, ret = 0;
1158
1159 for (i = 0; i < n; i++) {
1160 ret = phy_driver_register(new_driver + i);
1161 if (ret) {
1162 while (i-- > 0)
1163 phy_driver_unregister(new_driver + i);
1164 break;
1165 }
1166 }
1167 return ret;
1168}
1169EXPORT_SYMBOL(phy_drivers_register);
1170
Andy Fleming00db8182005-07-30 19:31:23 -04001171void phy_driver_unregister(struct phy_driver *drv)
1172{
1173 driver_unregister(&drv->driver);
1174}
1175EXPORT_SYMBOL(phy_driver_unregister);
1176
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00001177void phy_drivers_unregister(struct phy_driver *drv, int n)
1178{
1179 int i;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001180
1181 for (i = 0; i < n; i++)
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00001182 phy_driver_unregister(drv + i);
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00001183}
1184EXPORT_SYMBOL(phy_drivers_unregister);
1185
Shaohui Xieab2145e2014-01-10 14:27:22 +08001186static struct phy_driver genphy_driver[] = {
1187{
Andy Fleminge1393452005-08-24 18:46:21 -05001188 .phy_id = 0xffffffff,
1189 .phy_id_mask = 0xffffffff,
1190 .name = "Generic PHY",
1191 .config_init = genphy_config_init,
1192 .features = 0,
1193 .config_aneg = genphy_config_aneg,
1194 .read_status = genphy_read_status,
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001195 .suspend = genphy_suspend,
1196 .resume = genphy_resume,
Florian Fainellie1093742013-12-17 21:38:12 -08001197 .driver = { .owner = THIS_MODULE, },
Shaohui Xieab2145e2014-01-10 14:27:22 +08001198} };
Andy Fleming00db8182005-07-30 19:31:23 -04001199
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001200static int __init phy_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -04001201{
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001202 int rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001203
1204 rc = mdio_bus_init();
1205 if (rc)
Andy Fleminge1393452005-08-24 18:46:21 -05001206 return rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001207
Shaohui Xieab2145e2014-01-10 14:27:22 +08001208 rc = phy_drivers_register(genphy_driver,
1209 ARRAY_SIZE(genphy_driver));
Andy Fleminge1393452005-08-24 18:46:21 -05001210 if (rc)
1211 mdio_bus_exit();
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001212
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001213 return rc;
Andy Fleming00db8182005-07-30 19:31:23 -04001214}
1215
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001216static void __exit phy_exit(void)
Andy Fleming00db8182005-07-30 19:31:23 -04001217{
Shaohui Xieab2145e2014-01-10 14:27:22 +08001218 phy_drivers_unregister(genphy_driver,
1219 ARRAY_SIZE(genphy_driver));
Andy Fleminge1393452005-08-24 18:46:21 -05001220 mdio_bus_exit();
Andy Fleming00db8182005-07-30 19:31:23 -04001221}
1222
Andy Fleminge1393452005-08-24 18:46:21 -05001223subsys_initcall(phy_init);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001224module_exit(phy_exit);