blob: 7a5222daff93092b5ae6e06768e41b8523a232c4 [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>
Andy Fleming124059f2014-01-10 14:27:37 +080033#include <linux/mdio.h>
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030034#include <linux/io.h>
35#include <linux/uaccess.h>
Sascha Hauerde906af2014-05-21 15:29:45 +020036#include <linux/of.h>
Andy Fleming00db8182005-07-30 19:31:23 -040037
Andy Fleming00db8182005-07-30 19:31:23 -040038#include <asm/irq.h>
Andy Fleming00db8182005-07-30 19:31:23 -040039
Olaf Heringafcceaa2005-12-14 00:33:49 +010040MODULE_DESCRIPTION("PHY library");
41MODULE_AUTHOR("Andy Fleming");
42MODULE_LICENSE("GPL");
43
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030044void phy_device_free(struct phy_device *phydev)
45{
Andrew Lunne5a03bf2016-01-06 20:11:16 +010046 put_device(&phydev->mdio.dev);
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030047}
Grant Likely4dea5472009-04-25 12:52:46 +000048EXPORT_SYMBOL(phy_device_free);
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030049
50static void phy_device_release(struct device *dev)
51{
Petr Malatb2a43192013-02-28 01:01:52 +000052 kfree(to_phy_device(dev));
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030053}
54
Shaohui Xieab2145e2014-01-10 14:27:22 +080055enum genphy_driver {
56 GENPHY_DRV_1G,
Andy Fleming124059f2014-01-10 14:27:37 +080057 GENPHY_DRV_10G,
Shaohui Xieab2145e2014-01-10 14:27:22 +080058 GENPHY_DRV_MAX
59};
60
61static struct phy_driver genphy_driver[GENPHY_DRV_MAX];
Grant Likely4dea5472009-04-25 12:52:46 +000062
Andy Flemingf62220d2008-04-18 17:29:54 -050063static LIST_HEAD(phy_fixup_list);
64static DEFINE_MUTEX(phy_fixup_lock);
65
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030066/**
67 * phy_register_fixup - creates a new phy_fixup and adds it to the list
Andrew Lunne5a03bf2016-01-06 20:11:16 +010068 * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
Andy Flemingf62220d2008-04-18 17:29:54 -050069 * @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 Sieversfb28ad352008-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{
Andrew Lunn84eff6d2016-01-06 20:11:10 +0100117 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 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 }
Florian Fainellib0ae0092014-02-11 17:27:41 -0800143 phydev->has_fixups = true;
Andy Flemingf62220d2008-04-18 17:29:54 -0500144 }
145 }
146 mutex_unlock(&phy_fixup_lock);
147
148 return 0;
149}
Andy Flemingf62220d2008-04-18 17:29:54 -0500150
David Daneyac28b9f2012-06-27 07:33:35 +0000151struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300152 bool is_c45,
153 struct phy_c45_device_ids *c45_ids)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700154{
155 struct phy_device *dev;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100156 struct mdio_device *mdiodev;
David Woodhouse8626d3b2010-04-02 01:05:27 +0000157
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300158 /* We allocate the device, and initialize the default values */
Robert P. J. Daycd861282006-12-13 00:34:52 -0800159 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Sergei Shtylyovef899c02015-08-28 21:35:14 +0300160 if (!dev)
Sergei Shtylyovd3765f02015-08-28 21:34:34 +0300161 return ERR_PTR(-ENOMEM);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700162
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100163 mdiodev = &dev->mdio;
164 mdiodev->dev.release = phy_device_release;
165 mdiodev->dev.parent = &bus->dev;
166 mdiodev->dev.bus = &mdio_bus_type;
167 mdiodev->bus = bus;
168 mdiodev->addr = addr;
Andrew Lunn7f854422016-01-06 20:11:18 +0100169 mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300170
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700171 dev->speed = 0;
172 dev->duplex = -1;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300173 dev->pause = 0;
174 dev->asym_pause = 0;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700175 dev->link = 1;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600176 dev->interface = PHY_INTERFACE_MODE_GMII;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700177
178 dev->autoneg = AUTONEG_ENABLE;
179
David Daneyac28b9f2012-06-27 07:33:35 +0000180 dev->is_c45 = is_c45;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700181 dev->phy_id = phy_id;
David Daneyac28b9f2012-06-27 07:33:35 +0000182 if (c45_ids)
183 dev->c45_ids = *c45_ids;
Sergei Shtylyovef899c02015-08-28 21:35:14 +0300184 dev->irq = bus->irq ? bus->irq[addr] : PHY_POLL;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100185 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700186
187 dev->state = PHY_DOWN;
188
Nate Case35b5f6b2008-01-29 10:05:09 -0600189 mutex_init(&dev->lock);
Anton Vorontsov4f9c85a2010-01-18 05:37:16 +0000190 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
Florian Fainelli5ea94e72013-05-19 22:53:43 +0000191 INIT_WORK(&dev->phy_queue, phy_change);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700192
David Woodhouse8626d3b2010-04-02 01:05:27 +0000193 /* Request the appropriate module unconditionally; don't
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300194 * bother trying to do so only if it isn't already loaded,
195 * because that gets complicated. A hotplug event would have
196 * done an unconditional modprobe anyway.
197 * We don't do normal hotplug because it won't work for MDIO
198 * -- because it relies on the device staying around for long
199 * enough for the driver to get loaded. With MDIO, the NIC
200 * driver will get bored and give up as soon as it finds that
201 * there's no driver _already_ loaded.
202 */
David Woodhouse8626d3b2010-04-02 01:05:27 +0000203 request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
204
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100205 device_initialize(&mdiodev->dev);
Petr Malatb2a43192013-02-28 01:01:52 +0000206
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700207 return dev;
208}
David Daneyac28b9f2012-06-27 07:33:35 +0000209EXPORT_SYMBOL(phy_device_create);
210
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800211/* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
212 * @bus: the target MII bus
213 * @addr: PHY address on the MII bus
214 * @dev_addr: MMD address in the PHY.
215 * @devices_in_package: where to store the devices in package information.
216 *
217 * Description: reads devices in package registers of a MMD at @dev_addr
218 * from PHY at @addr on @bus.
219 *
220 * Returns: 0 on success, -EIO on failure.
221 */
222static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
223 u32 *devices_in_package)
224{
225 int phy_reg, reg_addr;
226
227 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
228 phy_reg = mdiobus_read(bus, addr, reg_addr);
229 if (phy_reg < 0)
230 return -EIO;
231 *devices_in_package = (phy_reg & 0xffff) << 16;
232
233 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
234 phy_reg = mdiobus_read(bus, addr, reg_addr);
235 if (phy_reg < 0)
236 return -EIO;
237 *devices_in_package |= (phy_reg & 0xffff);
238
239 return 0;
240}
241
David Daneyac28b9f2012-06-27 07:33:35 +0000242/**
243 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
244 * @bus: the target MII bus
245 * @addr: PHY address on the MII bus
246 * @phy_id: where to store the ID retrieved.
247 * @c45_ids: where to store the c45 ID information.
248 *
249 * If the PHY devices-in-package appears to be valid, it and the
250 * corresponding identifiers are stored in @c45_ids, zero is stored
251 * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns
252 * zero on success.
253 *
254 */
255static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
256 struct phy_c45_device_ids *c45_ids) {
257 int phy_reg;
258 int i, reg_addr;
259 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800260 u32 *devs = &c45_ids->devices_in_package;
David Daneyac28b9f2012-06-27 07:33:35 +0000261
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800262 /* Find first non-zero Devices In package. Device zero is reserved
263 * for 802.3 c45 complied PHYs, so don't probe it at first.
David Daneyac28b9f2012-06-27 07:33:35 +0000264 */
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800265 for (i = 1; i < num_ids && *devs == 0; i++) {
266 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
David Daneyac28b9f2012-06-27 07:33:35 +0000267 if (phy_reg < 0)
268 return -EIO;
David Daneyac28b9f2012-06-27 07:33:35 +0000269
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800270 if ((*devs & 0x1fffffff) == 0x1fffffff) {
271 /* If mostly Fs, there is no device there,
272 * then let's continue to probe more, as some
273 * 10G PHYs have zero Devices In package,
274 * e.g. Cortina CS4315/CS4340 PHY.
275 */
276 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
277 if (phy_reg < 0)
278 return -EIO;
279 /* no device there, let's get out of here */
280 if ((*devs & 0x1fffffff) == 0x1fffffff) {
Shengzhou Liuda1da282015-06-26 17:58:52 +0800281 *phy_id = 0xffffffff;
282 return 0;
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800283 } else {
284 break;
Shengzhou Liuda1da282015-06-26 17:58:52 +0800285 }
David Daneyac28b9f2012-06-27 07:33:35 +0000286 }
287 }
288
289 /* Now probe Device Identifiers for each device present. */
290 for (i = 1; i < num_ids; i++) {
291 if (!(c45_ids->devices_in_package & (1 << i)))
292 continue;
293
294 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
295 phy_reg = mdiobus_read(bus, addr, reg_addr);
296 if (phy_reg < 0)
297 return -EIO;
298 c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16;
299
300 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
301 phy_reg = mdiobus_read(bus, addr, reg_addr);
302 if (phy_reg < 0)
303 return -EIO;
304 c45_ids->device_ids[i] |= (phy_reg & 0xffff);
305 }
306 *phy_id = 0;
307 return 0;
308}
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700309
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800310/**
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400311 * get_phy_id - reads the specified addr for its ID.
312 * @bus: the target MII bus
313 * @addr: PHY address on the MII bus
314 * @phy_id: where to store the ID retrieved.
David Daneyac28b9f2012-06-27 07:33:35 +0000315 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
316 * @c45_ids: where to store the c45 ID information.
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400317 *
David Daneyac28b9f2012-06-27 07:33:35 +0000318 * Description: In the case of a 802.3-c22 PHY, reads the ID registers
319 * of the PHY at @addr on the @bus, stores it in @phy_id and returns
320 * zero on success.
321 *
322 * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
323 * its return value is in turn returned.
324 *
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400325 */
David Daneyac28b9f2012-06-27 07:33:35 +0000326static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
327 bool is_c45, struct phy_c45_device_ids *c45_ids)
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400328{
329 int phy_reg;
330
David Daneyac28b9f2012-06-27 07:33:35 +0000331 if (is_c45)
332 return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
333
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300334 /* Grab the bits from PHYIR1, and put them in the upper half */
David Daney6fe32642011-09-30 11:51:22 +0000335 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400336 if (phy_reg < 0)
337 return -EIO;
338
339 *phy_id = (phy_reg & 0xffff) << 16;
340
341 /* Grab the bits from PHYIR2, and put them in the lower half */
David Daney6fe32642011-09-30 11:51:22 +0000342 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400343 if (phy_reg < 0)
344 return -EIO;
345
346 *phy_id |= (phy_reg & 0xffff);
347
348 return 0;
349}
350
351/**
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300352 * get_phy_device - reads the specified PHY device and returns its @phy_device
353 * struct
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800354 * @bus: the target MII bus
355 * @addr: PHY address on the MII bus
David Daneyac28b9f2012-06-27 07:33:35 +0000356 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
Andy Fleming00db8182005-07-30 19:31:23 -0400357 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800358 * Description: Reads the ID registers of the PHY at @addr on the
359 * @bus, then allocates and returns the phy_device to represent it.
Andy Fleming00db8182005-07-30 19:31:23 -0400360 */
David Daneyac28b9f2012-06-27 07:33:35 +0000361struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
Andy Fleming00db8182005-07-30 19:31:23 -0400362{
David Daneyac28b9f2012-06-27 07:33:35 +0000363 struct phy_c45_device_ids c45_ids = {0};
David S. Miller160c85f2012-06-27 21:28:14 -0700364 u32 phy_id = 0;
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400365 int r;
Andy Fleming00db8182005-07-30 19:31:23 -0400366
David Daneyac28b9f2012-06-27 07:33:35 +0000367 r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400368 if (r)
369 return ERR_PTR(r);
Andy Fleming00db8182005-07-30 19:31:23 -0400370
Giuseppe Cavallaro6436cbc2008-11-20 20:43:18 -0800371 /* If the phy_id is mostly Fs, there is no device there */
372 if ((phy_id & 0x1fffffff) == 0x1fffffff)
373 return NULL;
374
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300375 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
Andy Fleming00db8182005-07-30 19:31:23 -0400376}
Grant Likely4dea5472009-04-25 12:52:46 +0000377EXPORT_SYMBOL(get_phy_device);
378
Andrew Lunn5cf11be2016-01-06 20:11:19 +0100379static ssize_t
380phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
381{
382 struct phy_device *phydev = to_phy_device(dev);
383
384 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
385}
386static DEVICE_ATTR_RO(phy_id);
387
388static ssize_t
389phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
390{
391 struct phy_device *phydev = to_phy_device(dev);
392 const char *mode = NULL;
393
394 if (phy_is_internal(phydev))
395 mode = "internal";
396 else
397 mode = phy_modes(phydev->interface);
398
399 return sprintf(buf, "%s\n", mode);
400}
401static DEVICE_ATTR_RO(phy_interface);
402
403static ssize_t
404phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
405 char *buf)
406{
407 struct phy_device *phydev = to_phy_device(dev);
408
409 return sprintf(buf, "%d\n", phydev->has_fixups);
410}
411static DEVICE_ATTR_RO(phy_has_fixups);
412
413static struct attribute *phy_dev_attrs[] = {
414 &dev_attr_phy_id.attr,
415 &dev_attr_phy_interface.attr,
416 &dev_attr_phy_has_fixups.attr,
417 NULL,
418};
419ATTRIBUTE_GROUPS(phy_dev);
420
Grant Likely4dea5472009-04-25 12:52:46 +0000421/**
422 * phy_device_register - Register the phy device on the MDIO bus
Randy Dunlap1d4ac5d2009-06-16 16:56:33 +0000423 * @phydev: phy_device structure to be added to the MDIO bus
Grant Likely4dea5472009-04-25 12:52:46 +0000424 */
425int phy_device_register(struct phy_device *phydev)
426{
427 int err;
428
Andrew Lunn7f854422016-01-06 20:11:18 +0100429 err = mdiobus_register_device(&phydev->mdio);
430 if (err)
431 return err;
Grant Likely4dea5472009-04-25 12:52:46 +0000432
433 /* Run all of the fixups for this PHY */
Florian Fainellid92f5de2014-07-28 16:28:07 -0700434 err = phy_scan_fixups(phydev);
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800435 if (err) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100436 pr_err("PHY %d failed to initialize\n", phydev->mdio.addr);
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800437 goto out;
438 }
Grant Likely4dea5472009-04-25 12:52:46 +0000439
Andrew Lunn5cf11be2016-01-06 20:11:19 +0100440 phydev->mdio.dev.groups = phy_dev_groups;
441
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100442 err = device_add(&phydev->mdio.dev);
Grant Likely4dea5472009-04-25 12:52:46 +0000443 if (err) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100444 pr_err("PHY %d failed to add\n", phydev->mdio.addr);
Grant Likely4dea5472009-04-25 12:52:46 +0000445 goto out;
446 }
447
448 return 0;
449
450 out:
Andrew Lunn7f854422016-01-06 20:11:18 +0100451 mdiobus_unregister_device(&phydev->mdio);
Grant Likely4dea5472009-04-25 12:52:46 +0000452 return err;
453}
454EXPORT_SYMBOL(phy_device_register);
Andy Fleming00db8182005-07-30 19:31:23 -0400455
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800456/**
Russell King38737e42015-09-24 20:36:28 +0100457 * phy_device_remove - Remove a previously registered phy device from the MDIO bus
458 * @phydev: phy_device structure to remove
459 *
460 * This doesn't free the phy_device itself, it merely reverses the effects
461 * of phy_device_register(). Use phy_device_free() to free the device
462 * after calling this function.
463 */
464void phy_device_remove(struct phy_device *phydev)
465{
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100466 device_del(&phydev->mdio.dev);
Andrew Lunn7f854422016-01-06 20:11:18 +0100467 mdiobus_unregister_device(&phydev->mdio);
Russell King38737e42015-09-24 20:36:28 +0100468}
469EXPORT_SYMBOL(phy_device_remove);
470
471/**
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800472 * phy_find_first - finds the first PHY device on the bus
473 * @bus: the target MII bus
474 */
475struct phy_device *phy_find_first(struct mii_bus *bus)
476{
Andrew Lunn7f854422016-01-06 20:11:18 +0100477 struct phy_device *phydev;
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800478 int addr;
479
480 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
Andrew Lunn7f854422016-01-06 20:11:18 +0100481 phydev = mdiobus_get_phy(bus, addr);
482 if (phydev)
483 return phydev;
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800484 }
485 return NULL;
486}
487EXPORT_SYMBOL(phy_find_first);
488
489/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800490 * phy_prepare_link - prepares the PHY layer to monitor link status
491 * @phydev: target phy_device struct
492 * @handler: callback function for link status change notifications
Andy Fleming00db8182005-07-30 19:31:23 -0400493 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800494 * Description: Tells the PHY infrastructure to handle the
Andy Fleming00db8182005-07-30 19:31:23 -0400495 * gory details on monitoring link status (whether through
496 * polling or an interrupt), and to call back to the
497 * connected device driver when the link status changes.
498 * If you want to monitor your own link state, don't call
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800499 * this function.
500 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000501static void phy_prepare_link(struct phy_device *phydev,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300502 void (*handler)(struct net_device *))
Andy Fleming00db8182005-07-30 19:31:23 -0400503{
504 phydev->adjust_link = handler;
505}
506
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800507/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000508 * phy_connect_direct - connect an ethernet device to a specific phy_device
509 * @dev: the network device to connect
510 * @phydev: the pointer to the phy device
511 * @handler: callback function for state change notifications
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000512 * @interface: PHY device's interface
513 */
514int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
Florian Fainellif9a8f832013-01-14 00:52:52 +0000515 void (*handler)(struct net_device *),
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000516 phy_interface_t interface)
517{
518 int rc;
519
Florian Fainellif9a8f832013-01-14 00:52:52 +0000520 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000521 if (rc)
522 return rc;
523
524 phy_prepare_link(phydev, handler);
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300525 phy_start_machine(phydev);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000526 if (phydev->irq > 0)
527 phy_start_interrupts(phydev);
528
529 return 0;
530}
531EXPORT_SYMBOL(phy_connect_direct);
532
533/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800534 * phy_connect - connect an ethernet device to a PHY device
535 * @dev: the network device to connect
Randy Dunlap5d12b132008-04-28 10:58:22 -0700536 * @bus_id: the id string of the PHY device to connect
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800537 * @handler: callback function for state change notifications
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800538 * @interface: PHY device's interface
Andy Fleminge1393452005-08-24 18:46:21 -0500539 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800540 * Description: Convenience function for connecting ethernet
Andy Fleminge1393452005-08-24 18:46:21 -0500541 * devices to PHY devices. The default behavior is for
542 * the PHY infrastructure to handle everything, and only notify
543 * the connected driver when the link status changes. If you
544 * don't want, or can't use the provided functionality, you may
545 * choose to call only the subset of functions which provide
546 * the desired functionality.
547 */
Florian Fainellie1093742013-12-17 21:38:12 -0800548struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300549 void (*handler)(struct net_device *),
550 phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500551{
552 struct phy_device *phydev;
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000553 struct device *d;
554 int rc;
Andy Fleminge1393452005-08-24 18:46:21 -0500555
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000556 /* Search the list of PHY devices on the mdio bus for the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300557 * PHY with the requested name
558 */
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000559 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
560 if (!d) {
561 pr_err("PHY %s not found\n", bus_id);
562 return ERR_PTR(-ENODEV);
563 }
564 phydev = to_phy_device(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500565
Florian Fainellif9a8f832013-01-14 00:52:52 +0000566 rc = phy_connect_direct(dev, phydev, handler, interface);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000567 if (rc)
568 return ERR_PTR(rc);
Andy Fleminge1393452005-08-24 18:46:21 -0500569
570 return phydev;
571}
572EXPORT_SYMBOL(phy_connect);
573
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800574/**
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300575 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
576 * device
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800577 * @phydev: target phy_device struct
578 */
Andy Fleminge1393452005-08-24 18:46:21 -0500579void phy_disconnect(struct phy_device *phydev)
580{
581 if (phydev->irq > 0)
582 phy_stop_interrupts(phydev);
583
584 phy_stop_machine(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800585
Andy Fleminge1393452005-08-24 18:46:21 -0500586 phydev->adjust_link = NULL;
587
588 phy_detach(phydev);
589}
590EXPORT_SYMBOL(phy_disconnect);
591
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800592/**
593 * phy_poll_reset - Safely wait until a PHY reset has properly completed
594 * @phydev: The PHY device to poll
595 *
596 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
597 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
598 * register must be polled until the BMCR_RESET bit clears.
599 *
600 * Furthermore, any attempts to write to PHY registers may have no effect
601 * or even generate MDIO bus errors until this is complete.
602 *
603 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
604 * standard and do not fully reset after the BMCR_RESET bit is set, and may
605 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
606 * effort to support such broken PHYs, this function is separate from the
607 * standard phy_init_hw() which will zero all the other bits in the BMCR
608 * and reapply all driver-specific and board-specific fixups.
609 */
610static int phy_poll_reset(struct phy_device *phydev)
611{
612 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
613 unsigned int retries = 12;
614 int ret;
615
616 do {
617 msleep(50);
618 ret = phy_read(phydev, MII_BMCR);
619 if (ret < 0)
620 return ret;
621 } while (ret & BMCR_RESET && --retries);
622 if (ret & BMCR_RESET)
623 return -ETIMEDOUT;
624
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300625 /* Some chips (smsc911x) may still need up to another 1ms after the
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800626 * BMCR_RESET bit is cleared before they are usable.
627 */
628 msleep(1);
629 return 0;
630}
631
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000632int phy_init_hw(struct phy_device *phydev)
633{
Florian Fainelli9df81dd2014-02-17 13:34:03 -0800634 int ret = 0;
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000635
636 if (!phydev->drv || !phydev->drv->config_init)
637 return 0;
638
Florian Fainelli9df81dd2014-02-17 13:34:03 -0800639 if (phydev->drv->soft_reset)
640 ret = phydev->drv->soft_reset(phydev);
641 else
642 ret = genphy_soft_reset(phydev);
643
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800644 if (ret < 0)
645 return ret;
646
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000647 ret = phy_scan_fixups(phydev);
648 if (ret < 0)
649 return ret;
650
651 return phydev->drv->config_init(phydev);
652}
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800653EXPORT_SYMBOL(phy_init_hw);
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000654
Andrew Lunn22209432016-01-06 20:11:13 +0100655void phy_attached_info(struct phy_device *phydev)
656{
657 phy_attached_print(phydev, NULL);
658}
659EXPORT_SYMBOL(phy_attached_info);
660
661#define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)"
662void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
663{
664 if (!fmt) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100665 dev_info(&phydev->mdio.dev, ATTACHED_FMT "\n",
Andrew Lunn22209432016-01-06 20:11:13 +0100666 phydev->drv->name, phydev_name(phydev),
667 phydev->irq);
668 } else {
669 va_list ap;
670
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100671 dev_info(&phydev->mdio.dev, ATTACHED_FMT,
Andrew Lunn22209432016-01-06 20:11:13 +0100672 phydev->drv->name, phydev_name(phydev),
673 phydev->irq);
674
675 va_start(ap, fmt);
676 vprintk(fmt, ap);
677 va_end(ap);
678 }
679}
680EXPORT_SYMBOL(phy_attached_print);
681
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800682/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000683 * phy_attach_direct - attach a network device to a given PHY device pointer
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800684 * @dev: network device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000685 * @phydev: Pointer to phy_device to attach
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800686 * @flags: PHY device's dev_flags
687 * @interface: PHY device's interface
688 *
689 * Description: Called by drivers to attach to a particular PHY
690 * device. The phy_device is found, and properly hooked up
Andy Fleming257184d2014-01-10 14:27:54 +0800691 * to the phy_driver. If no driver is attached, then a
692 * generic driver is used. The phy_device is given a ptr to
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800693 * the attaching device, and given a callback for link status
694 * change. The phy_device is returned to the attaching driver.
Russell King73229672015-09-24 20:36:08 +0100695 * This function takes a reference on the phy device.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800696 */
Andy Fleming257184d2014-01-10 14:27:54 +0800697int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
698 u32 flags, phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500699{
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100700 struct mii_bus *bus = phydev->mdio.bus;
701 struct device *d = &phydev->mdio.dev;
Marc Kleine-Budded005a092011-03-28 14:54:08 +0000702 int err;
Andy Fleminge1393452005-08-24 18:46:21 -0500703
Russell King3e3aaf62015-09-24 20:36:02 +0100704 if (!try_module_get(bus->owner)) {
705 dev_err(&dev->dev, "failed to get the bus module\n");
706 return -EIO;
707 }
708
Russell King73229672015-09-24 20:36:08 +0100709 get_device(d);
710
Andy Fleminge1393452005-08-24 18:46:21 -0500711 /* Assume that if there is no driver, that it doesn't
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300712 * exist, and we should use the genphy driver.
713 */
Sergei Shtylyovef899c02015-08-28 21:35:14 +0300714 if (!d->driver) {
Andy Fleming257184d2014-01-10 14:27:54 +0800715 if (phydev->is_c45)
716 d->driver = &genphy_driver[GENPHY_DRV_10G].driver;
717 else
718 d->driver = &genphy_driver[GENPHY_DRV_1G].driver;
Andy Fleminge1393452005-08-24 18:46:21 -0500719
720 err = d->driver->probe(d);
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400721 if (err >= 0)
722 err = device_bind_driver(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500723
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400724 if (err)
Russell King3e3aaf62015-09-24 20:36:02 +0100725 goto error;
Andy Fleminge1393452005-08-24 18:46:21 -0500726 }
727
728 if (phydev->attached_dev) {
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000729 dev_err(&dev->dev, "PHY already attached\n");
Russell King3e3aaf62015-09-24 20:36:02 +0100730 err = -EBUSY;
731 goto error;
Ezequiel Garciab3565f22014-07-23 16:47:32 -0300732 }
733
Andy Fleminge1393452005-08-24 18:46:21 -0500734 phydev->attached_dev = dev;
Richard Cochranc1f19b52010-07-17 08:49:36 +0000735 dev->phydev = phydev;
Andy Fleminge1393452005-08-24 18:46:21 -0500736
737 phydev->dev_flags = flags;
738
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600739 phydev->interface = interface;
740
Anton Vorontsovef24b162010-08-24 14:46:12 -0700741 phydev->state = PHY_READY;
742
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600743 /* Do initial configuration here, now that
744 * we have certain key parameters
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300745 * (dev_flags and interface)
746 */
Marc Kleine-Budded005a092011-03-28 14:54:08 +0000747 err = phy_init_hw(phydev);
748 if (err)
749 phy_detach(phydev);
Guenter Roeckb3947452014-05-14 13:12:49 -0700750 else
751 phy_resume(phydev);
Sebastian Hesselbarth1211ce52013-12-13 10:20:28 +0100752
Marc Kleine-Budded005a092011-03-28 14:54:08 +0000753 return err;
Russell King3e3aaf62015-09-24 20:36:02 +0100754
755error:
Russell King73229672015-09-24 20:36:08 +0100756 put_device(d);
Russell King3e3aaf62015-09-24 20:36:02 +0100757 module_put(bus->owner);
758 return err;
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000759}
Andy Fleming257184d2014-01-10 14:27:54 +0800760EXPORT_SYMBOL(phy_attach_direct);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000761
762/**
763 * phy_attach - attach a network device to a particular PHY device
764 * @dev: network device to attach
765 * @bus_id: Bus ID of PHY device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000766 * @interface: PHY device's interface
767 *
768 * Description: Same as phy_attach_direct() except that a PHY bus_id
769 * string is passed instead of a pointer to a struct phy_device.
770 */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300771struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
772 phy_interface_t interface)
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000773{
774 struct bus_type *bus = &mdio_bus_type;
775 struct phy_device *phydev;
776 struct device *d;
777 int rc;
778
779 /* Search the list of PHY devices on the mdio bus for the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300780 * PHY with the requested name
781 */
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000782 d = bus_find_device_by_name(bus, NULL, bus_id);
783 if (!d) {
784 pr_err("PHY %s not found\n", bus_id);
785 return ERR_PTR(-ENODEV);
786 }
787 phydev = to_phy_device(d);
788
Florian Fainellif9a8f832013-01-14 00:52:52 +0000789 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000790 if (rc)
791 return ERR_PTR(rc);
792
Andy Fleminge1393452005-08-24 18:46:21 -0500793 return phydev;
794}
795EXPORT_SYMBOL(phy_attach);
796
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800797/**
798 * phy_detach - detach a PHY device from its network device
799 * @phydev: target phy_device struct
Russell King73229672015-09-24 20:36:08 +0100800 *
801 * This detaches the phy device from its network device and the phy
802 * driver, and drops the reference count taken in phy_attach_direct().
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800803 */
Andy Fleminge1393452005-08-24 18:46:21 -0500804void phy_detach(struct phy_device *phydev)
805{
Russell King3e3aaf62015-09-24 20:36:02 +0100806 struct mii_bus *bus;
Shaohui Xieab2145e2014-01-10 14:27:22 +0800807 int i;
Ezequiel Garciab3565f22014-07-23 16:47:32 -0300808
Richard Cochranc1f19b52010-07-17 08:49:36 +0000809 phydev->attached_dev->phydev = NULL;
Andy Fleminge1393452005-08-24 18:46:21 -0500810 phydev->attached_dev = NULL;
Sebastian Hesselbarth1211ce52013-12-13 10:20:28 +0100811 phy_suspend(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500812
813 /* If the device had no specific driver before (i.e. - it
814 * was using the generic driver), we unbind the device
815 * from the generic driver so that there's a chance a
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300816 * real driver could be loaded
817 */
Shaohui Xieab2145e2014-01-10 14:27:22 +0800818 for (i = 0; i < ARRAY_SIZE(genphy_driver); i++) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100819 if (phydev->mdio.dev.driver == &genphy_driver[i].driver) {
820 device_release_driver(&phydev->mdio.dev);
Shaohui Xieab2145e2014-01-10 14:27:22 +0800821 break;
822 }
823 }
Russell King3e3aaf62015-09-24 20:36:02 +0100824
Russell King73229672015-09-24 20:36:08 +0100825 /*
826 * The phydev might go away on the put_device() below, so avoid
827 * a use-after-free bug by reading the underlying bus first.
828 */
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100829 bus = phydev->mdio.bus;
Russell King3e3aaf62015-09-24 20:36:02 +0100830
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100831 put_device(&phydev->mdio.dev);
Russell King3e3aaf62015-09-24 20:36:02 +0100832 module_put(bus->owner);
Andy Fleminge1393452005-08-24 18:46:21 -0500833}
834EXPORT_SYMBOL(phy_detach);
835
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +0100836int phy_suspend(struct phy_device *phydev)
837{
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100838 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
Sebastian Hesselbarth32fc3fd2014-03-14 10:07:44 +0100839 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Florian Fainelli8a477a62015-01-26 22:05:39 -0800840 int ret = 0;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +0100841
842 /* If the device has WOL enabled, we cannot suspend the PHY */
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +0100843 phy_ethtool_get_wol(phydev, &wol);
844 if (wol.wolopts)
845 return -EBUSY;
846
847 if (phydrv->suspend)
Florian Fainelli8a477a62015-01-26 22:05:39 -0800848 ret = phydrv->suspend(phydev);
849
850 if (ret)
851 return ret;
852
853 phydev->suspended = true;
854
855 return ret;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +0100856}
Florian Fainellica127692014-07-08 10:38:36 -0700857EXPORT_SYMBOL(phy_suspend);
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +0100858
859int phy_resume(struct phy_device *phydev)
860{
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100861 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
Florian Fainelli8a477a62015-01-26 22:05:39 -0800862 int ret = 0;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +0100863
864 if (phydrv->resume)
Florian Fainelli8a477a62015-01-26 22:05:39 -0800865 ret = phydrv->resume(phydev);
866
867 if (ret)
868 return ret;
869
870 phydev->suspended = false;
871
872 return ret;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +0100873}
Florian Fainellica127692014-07-08 10:38:36 -0700874EXPORT_SYMBOL(phy_resume);
Andy Fleminge1393452005-08-24 18:46:21 -0500875
Andy Fleming00db8182005-07-30 19:31:23 -0400876/* Generic PHY support and helper functions */
877
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800878/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300879 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800880 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400881 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800882 * Description: Writes MII_ADVERTISE with the appropriate values,
Andy Fleming00db8182005-07-30 19:31:23 -0400883 * after sanitizing the values to make sure we only advertise
Trent Piepho51e2a382008-09-24 10:55:46 +0000884 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
885 * hasn't changed, and > 0 if it has changed.
Andy Fleming00db8182005-07-30 19:31:23 -0400886 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000887static int genphy_config_advert(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400888{
889 u32 advertise;
Florian Fainelli5273e3a2014-02-03 12:35:46 -0800890 int oldadv, adv, bmsr;
Trent Piepho51e2a382008-09-24 10:55:46 +0000891 int err, changed = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400892
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300893 /* Only allow advertising what this PHY supports */
Andy Fleming00db8182005-07-30 19:31:23 -0400894 phydev->advertising &= phydev->supported;
895 advertise = phydev->advertising;
896
897 /* Setup standard advertisement */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300898 adv = phy_read(phydev, MII_ADVERTISE);
Andy Fleming00db8182005-07-30 19:31:23 -0400899 if (adv < 0)
900 return adv;
901
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300902 oldadv = adv;
Matt Carlson28011cf2011-11-16 18:36:59 -0500903 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
Andy Fleming00db8182005-07-30 19:31:23 -0400904 ADVERTISE_PAUSE_ASYM);
Matt Carlson37f07022011-11-17 14:30:55 +0000905 adv |= ethtool_adv_to_mii_adv_t(advertise);
Andy Fleming00db8182005-07-30 19:31:23 -0400906
Trent Piepho51e2a382008-09-24 10:55:46 +0000907 if (adv != oldadv) {
908 err = phy_write(phydev, MII_ADVERTISE, adv);
Andy Fleming00db8182005-07-30 19:31:23 -0400909
Trent Piepho51e2a382008-09-24 10:55:46 +0000910 if (err < 0)
911 return err;
912 changed = 1;
913 }
Andy Fleming00db8182005-07-30 19:31:23 -0400914
Florian Fainelli5273e3a2014-02-03 12:35:46 -0800915 bmsr = phy_read(phydev, MII_BMSR);
916 if (bmsr < 0)
917 return bmsr;
918
919 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
920 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
921 * logical 1.
922 */
923 if (!(bmsr & BMSR_ESTATEN))
924 return changed;
925
Andy Fleming00db8182005-07-30 19:31:23 -0400926 /* Configure gigabit if it's supported */
Florian Fainelli5273e3a2014-02-03 12:35:46 -0800927 adv = phy_read(phydev, MII_CTRL1000);
928 if (adv < 0)
929 return adv;
930
931 oldadv = adv;
932 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
933
Andy Fleming00db8182005-07-30 19:31:23 -0400934 if (phydev->supported & (SUPPORTED_1000baseT_Half |
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300935 SUPPORTED_1000baseT_Full)) {
Matt Carlson37f07022011-11-17 14:30:55 +0000936 adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
Andy Fleming00db8182005-07-30 19:31:23 -0400937 }
938
Mugunthan V Neb686232015-06-25 22:21:02 +0530939 if (adv != oldadv)
940 changed = 1;
941
Florian Fainelli5273e3a2014-02-03 12:35:46 -0800942 err = phy_write(phydev, MII_CTRL1000, adv);
943 if (err < 0)
944 return err;
945
Trent Piepho51e2a382008-09-24 10:55:46 +0000946 return changed;
Andy Fleming00db8182005-07-30 19:31:23 -0400947}
Andy Fleming00db8182005-07-30 19:31:23 -0400948
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800949/**
950 * genphy_setup_forced - configures/forces speed/duplex from @phydev
951 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400952 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800953 * Description: Configures MII_BMCR to force speed/duplex
Andy Fleming00db8182005-07-30 19:31:23 -0400954 * to the values in phydev. Assumes that the values are valid.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800955 * Please see phy_sanitize_settings().
956 */
Madalin Bucur3fb69bc2013-11-20 16:38:19 -0600957int genphy_setup_forced(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400958{
Domen Puncerbc1e0a02007-08-17 08:54:45 +0200959 int ctl = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400960
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300961 phydev->pause = 0;
962 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400963
964 if (SPEED_1000 == phydev->speed)
965 ctl |= BMCR_SPEED1000;
966 else if (SPEED_100 == phydev->speed)
967 ctl |= BMCR_SPEED100;
968
969 if (DUPLEX_FULL == phydev->duplex)
970 ctl |= BMCR_FULLDPLX;
Florian Fainellie1093742013-12-17 21:38:12 -0800971
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300972 return phy_write(phydev, MII_BMCR, ctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400973}
Madalin Bucur3fb69bc2013-11-20 16:38:19 -0600974EXPORT_SYMBOL(genphy_setup_forced);
Andy Fleming00db8182005-07-30 19:31:23 -0400975
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800976/**
977 * genphy_restart_aneg - Enable and Restart Autonegotiation
978 * @phydev: target phy_device struct
979 */
Andy Fleming00db8182005-07-30 19:31:23 -0400980int genphy_restart_aneg(struct phy_device *phydev)
981{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300982 int ctl = phy_read(phydev, MII_BMCR);
Andy Fleming00db8182005-07-30 19:31:23 -0400983
984 if (ctl < 0)
985 return ctl;
986
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300987 ctl |= BMCR_ANENABLE | BMCR_ANRESTART;
Andy Fleming00db8182005-07-30 19:31:23 -0400988
989 /* Don't isolate the PHY if we're negotiating */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300990 ctl &= ~BMCR_ISOLATE;
Andy Fleming00db8182005-07-30 19:31:23 -0400991
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300992 return phy_write(phydev, MII_BMCR, ctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400993}
Adrian Bunk892871d2008-10-13 18:48:09 -0700994EXPORT_SYMBOL(genphy_restart_aneg);
Andy Fleming00db8182005-07-30 19:31:23 -0400995
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800996/**
997 * genphy_config_aneg - restart auto-negotiation or write BMCR
998 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400999 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001000 * Description: If auto-negotiation is enabled, we configure the
Andy Fleming00db8182005-07-30 19:31:23 -04001001 * advertising, and then restart auto-negotiation. If it is not
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001002 * enabled, then we write the BMCR.
Andy Fleming00db8182005-07-30 19:31:23 -04001003 */
1004int genphy_config_aneg(struct phy_device *phydev)
1005{
Trent Piephode339c22008-11-19 15:52:41 -08001006 int result;
Andy Fleming00db8182005-07-30 19:31:23 -04001007
Trent Piephode339c22008-11-19 15:52:41 -08001008 if (AUTONEG_ENABLE != phydev->autoneg)
1009 return genphy_setup_forced(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04001010
Trent Piephode339c22008-11-19 15:52:41 -08001011 result = genphy_config_advert(phydev);
Trent Piephode339c22008-11-19 15:52:41 -08001012 if (result < 0) /* error */
1013 return result;
Trent Piephode339c22008-11-19 15:52:41 -08001014 if (result == 0) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001015 /* Advertisement hasn't changed, but maybe aneg was never on to
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001016 * begin with? Or maybe phy was isolated?
1017 */
Trent Piephode339c22008-11-19 15:52:41 -08001018 int ctl = phy_read(phydev, MII_BMCR);
1019
1020 if (ctl < 0)
1021 return ctl;
1022
1023 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
1024 result = 1; /* do restart aneg */
1025 }
1026
1027 /* Only restart aneg if we are advertising something different
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001028 * than we were before.
1029 */
Trent Piephode339c22008-11-19 15:52:41 -08001030 if (result > 0)
1031 result = genphy_restart_aneg(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04001032
Trent Piepho51e2a382008-09-24 10:55:46 +00001033 return result;
Andy Fleming00db8182005-07-30 19:31:23 -04001034}
1035EXPORT_SYMBOL(genphy_config_aneg);
1036
Florian Fainellia9fa6e62014-02-11 17:27:36 -08001037/**
1038 * genphy_aneg_done - return auto-negotiation status
1039 * @phydev: target phy_device struct
1040 *
1041 * Description: Reads the status register and returns 0 either if
1042 * auto-negotiation is incomplete, or if there was an error.
1043 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
1044 */
1045int genphy_aneg_done(struct phy_device *phydev)
1046{
1047 int retval = phy_read(phydev, MII_BMSR);
1048
1049 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1050}
1051EXPORT_SYMBOL(genphy_aneg_done);
1052
stephen hemminger395056e2014-01-19 11:48:20 -08001053static int gen10g_config_aneg(struct phy_device *phydev)
Andy Fleming124059f2014-01-10 14:27:37 +08001054{
1055 return 0;
1056}
Andy Fleming124059f2014-01-10 14:27:37 +08001057
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001058/**
1059 * genphy_update_link - update link status in @phydev
1060 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001061 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001062 * Description: Update the value in phydev->link to reflect the
Andy Fleming00db8182005-07-30 19:31:23 -04001063 * current link value. In order to do this, we need to read
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001064 * the status register twice, keeping the second value.
Andy Fleming00db8182005-07-30 19:31:23 -04001065 */
1066int genphy_update_link(struct phy_device *phydev)
1067{
1068 int status;
1069
1070 /* Do a fake read */
1071 status = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -04001072 if (status < 0)
1073 return status;
1074
1075 /* Read link and autonegotiation status */
1076 status = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -04001077 if (status < 0)
1078 return status;
1079
1080 if ((status & BMSR_LSTATUS) == 0)
1081 phydev->link = 0;
1082 else
1083 phydev->link = 1;
1084
1085 return 0;
1086}
Andy Fleming6b655522006-10-16 16:19:17 -05001087EXPORT_SYMBOL(genphy_update_link);
Andy Fleming00db8182005-07-30 19:31:23 -04001088
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001089/**
1090 * genphy_read_status - check the link status and update current link state
1091 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001092 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001093 * Description: Check the link, then figure out the current state
Andy Fleming00db8182005-07-30 19:31:23 -04001094 * by comparing what we advertise with what the link partner
1095 * advertises. Start by checking the gigabit possibilities,
1096 * then move on to 10/100.
1097 */
1098int genphy_read_status(struct phy_device *phydev)
1099{
1100 int adv;
1101 int err;
1102 int lpa;
1103 int lpagb = 0;
Cristian Bercarua4572e02014-02-25 10:22:48 +02001104 int common_adv;
1105 int common_adv_gb = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001106
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001107 /* Update the link, but return if there was an error */
Andy Fleming00db8182005-07-30 19:31:23 -04001108 err = genphy_update_link(phydev);
1109 if (err)
1110 return err;
1111
Florian Fainelli114002b2013-12-06 13:01:30 -08001112 phydev->lp_advertising = 0;
1113
Andy Fleming00db8182005-07-30 19:31:23 -04001114 if (AUTONEG_ENABLE == phydev->autoneg) {
1115 if (phydev->supported & (SUPPORTED_1000baseT_Half
1116 | SUPPORTED_1000baseT_Full)) {
1117 lpagb = phy_read(phydev, MII_STAT1000);
Andy Fleming00db8182005-07-30 19:31:23 -04001118 if (lpagb < 0)
1119 return lpagb;
1120
1121 adv = phy_read(phydev, MII_CTRL1000);
Andy Fleming00db8182005-07-30 19:31:23 -04001122 if (adv < 0)
1123 return adv;
1124
Florian Fainelli114002b2013-12-06 13:01:30 -08001125 phydev->lp_advertising =
1126 mii_stat1000_to_ethtool_lpa_t(lpagb);
Cristian Bercarua4572e02014-02-25 10:22:48 +02001127 common_adv_gb = lpagb & adv << 2;
Andy Fleming00db8182005-07-30 19:31:23 -04001128 }
1129
1130 lpa = phy_read(phydev, MII_LPA);
Andy Fleming00db8182005-07-30 19:31:23 -04001131 if (lpa < 0)
1132 return lpa;
1133
Florian Fainelli114002b2013-12-06 13:01:30 -08001134 phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
1135
Andy Fleming00db8182005-07-30 19:31:23 -04001136 adv = phy_read(phydev, MII_ADVERTISE);
Andy Fleming00db8182005-07-30 19:31:23 -04001137 if (adv < 0)
1138 return adv;
1139
Cristian Bercarua4572e02014-02-25 10:22:48 +02001140 common_adv = lpa & adv;
Andy Fleming00db8182005-07-30 19:31:23 -04001141
1142 phydev->speed = SPEED_10;
1143 phydev->duplex = DUPLEX_HALF;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001144 phydev->pause = 0;
1145 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001146
Cristian Bercarua4572e02014-02-25 10:22:48 +02001147 if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) {
Andy Fleming00db8182005-07-30 19:31:23 -04001148 phydev->speed = SPEED_1000;
1149
Cristian Bercarua4572e02014-02-25 10:22:48 +02001150 if (common_adv_gb & LPA_1000FULL)
Andy Fleming00db8182005-07-30 19:31:23 -04001151 phydev->duplex = DUPLEX_FULL;
Cristian Bercarua4572e02014-02-25 10:22:48 +02001152 } else if (common_adv & (LPA_100FULL | LPA_100HALF)) {
Andy Fleming00db8182005-07-30 19:31:23 -04001153 phydev->speed = SPEED_100;
Florian Fainellie1093742013-12-17 21:38:12 -08001154
Cristian Bercarua4572e02014-02-25 10:22:48 +02001155 if (common_adv & LPA_100FULL)
Andy Fleming00db8182005-07-30 19:31:23 -04001156 phydev->duplex = DUPLEX_FULL;
1157 } else
Cristian Bercarua4572e02014-02-25 10:22:48 +02001158 if (common_adv & LPA_10FULL)
Andy Fleming00db8182005-07-30 19:31:23 -04001159 phydev->duplex = DUPLEX_FULL;
1160
Florian Fainellie1093742013-12-17 21:38:12 -08001161 if (phydev->duplex == DUPLEX_FULL) {
Andy Fleming00db8182005-07-30 19:31:23 -04001162 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
1163 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
1164 }
1165 } else {
1166 int bmcr = phy_read(phydev, MII_BMCR);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001167
Andy Fleming00db8182005-07-30 19:31:23 -04001168 if (bmcr < 0)
1169 return bmcr;
1170
1171 if (bmcr & BMCR_FULLDPLX)
1172 phydev->duplex = DUPLEX_FULL;
1173 else
1174 phydev->duplex = DUPLEX_HALF;
1175
1176 if (bmcr & BMCR_SPEED1000)
1177 phydev->speed = SPEED_1000;
1178 else if (bmcr & BMCR_SPEED100)
1179 phydev->speed = SPEED_100;
1180 else
1181 phydev->speed = SPEED_10;
1182
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001183 phydev->pause = 0;
1184 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001185 }
1186
1187 return 0;
1188}
1189EXPORT_SYMBOL(genphy_read_status);
1190
stephen hemminger395056e2014-01-19 11:48:20 -08001191static int gen10g_read_status(struct phy_device *phydev)
Andy Fleming124059f2014-01-10 14:27:37 +08001192{
1193 int devad, reg;
1194 u32 mmd_mask = phydev->c45_ids.devices_in_package;
1195
1196 phydev->link = 1;
1197
1198 /* For now just lie and say it's 10G all the time */
1199 phydev->speed = SPEED_10000;
1200 phydev->duplex = DUPLEX_FULL;
1201
1202 for (devad = 0; mmd_mask; devad++, mmd_mask = mmd_mask >> 1) {
1203 if (!(mmd_mask & 1))
1204 continue;
1205
1206 /* Read twice because link state is latched and a
1207 * read moves the current state into the register
1208 */
1209 phy_read_mmd(phydev, devad, MDIO_STAT1);
1210 reg = phy_read_mmd(phydev, devad, MDIO_STAT1);
1211 if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS))
1212 phydev->link = 0;
1213 }
1214
1215 return 0;
1216}
Andy Fleming124059f2014-01-10 14:27:37 +08001217
Florian Fainelli797ac072014-02-17 13:34:02 -08001218/**
1219 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
1220 * @phydev: target phy_device struct
1221 *
1222 * Description: Perform a software PHY reset using the standard
1223 * BMCR_RESET bit and poll for the reset bit to be cleared.
1224 *
1225 * Returns: 0 on success, < 0 on failure
1226 */
1227int genphy_soft_reset(struct phy_device *phydev)
1228{
1229 int ret;
1230
1231 ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
1232 if (ret < 0)
1233 return ret;
1234
1235 return phy_poll_reset(phydev);
1236}
1237EXPORT_SYMBOL(genphy_soft_reset);
1238
Daniel Mackaf6b6962014-04-16 17:19:12 +02001239int genphy_config_init(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -04001240{
Eric Sesterhenn84c22d72006-09-25 16:39:22 -07001241 int val;
Andy Fleming00db8182005-07-30 19:31:23 -04001242 u32 features;
1243
Andy Fleming00db8182005-07-30 19:31:23 -04001244 features = (SUPPORTED_TP | SUPPORTED_MII
1245 | SUPPORTED_AUI | SUPPORTED_FIBRE |
1246 SUPPORTED_BNC);
1247
1248 /* Do we support autonegotiation? */
1249 val = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -04001250 if (val < 0)
1251 return val;
1252
1253 if (val & BMSR_ANEGCAPABLE)
1254 features |= SUPPORTED_Autoneg;
1255
1256 if (val & BMSR_100FULL)
1257 features |= SUPPORTED_100baseT_Full;
1258 if (val & BMSR_100HALF)
1259 features |= SUPPORTED_100baseT_Half;
1260 if (val & BMSR_10FULL)
1261 features |= SUPPORTED_10baseT_Full;
1262 if (val & BMSR_10HALF)
1263 features |= SUPPORTED_10baseT_Half;
1264
1265 if (val & BMSR_ESTATEN) {
1266 val = phy_read(phydev, MII_ESTATUS);
Andy Fleming00db8182005-07-30 19:31:23 -04001267 if (val < 0)
1268 return val;
1269
1270 if (val & ESTATUS_1000_TFULL)
1271 features |= SUPPORTED_1000baseT_Full;
1272 if (val & ESTATUS_1000_THALF)
1273 features |= SUPPORTED_1000baseT_Half;
1274 }
1275
Sascha Hauerc242a472014-05-21 15:29:44 +02001276 phydev->supported &= features;
1277 phydev->advertising &= features;
Andy Fleming00db8182005-07-30 19:31:23 -04001278
1279 return 0;
1280}
Andy Fleming124059f2014-01-10 14:27:37 +08001281
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001282static int gen10g_soft_reset(struct phy_device *phydev)
1283{
1284 /* Do nothing for now */
1285 return 0;
1286}
Daniel Mackaf6b6962014-04-16 17:19:12 +02001287EXPORT_SYMBOL(genphy_config_init);
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001288
Andy Fleming124059f2014-01-10 14:27:37 +08001289static int gen10g_config_init(struct phy_device *phydev)
1290{
1291 /* Temporarily just say we support everything */
1292 phydev->supported = SUPPORTED_10000baseT_Full;
1293 phydev->advertising = SUPPORTED_10000baseT_Full;
1294
1295 return 0;
1296}
1297
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001298int genphy_suspend(struct phy_device *phydev)
1299{
1300 int value;
Andy Fleming00db8182005-07-30 19:31:23 -04001301
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001302 mutex_lock(&phydev->lock);
1303
1304 value = phy_read(phydev, MII_BMCR);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001305 phy_write(phydev, MII_BMCR, value | BMCR_PDOWN);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001306
1307 mutex_unlock(&phydev->lock);
1308
1309 return 0;
1310}
1311EXPORT_SYMBOL(genphy_suspend);
1312
stephen hemminger395056e2014-01-19 11:48:20 -08001313static int gen10g_suspend(struct phy_device *phydev)
Andy Fleming124059f2014-01-10 14:27:37 +08001314{
1315 return 0;
1316}
Andy Fleming124059f2014-01-10 14:27:37 +08001317
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001318int genphy_resume(struct phy_device *phydev)
1319{
1320 int value;
1321
1322 mutex_lock(&phydev->lock);
1323
1324 value = phy_read(phydev, MII_BMCR);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001325 phy_write(phydev, MII_BMCR, value & ~BMCR_PDOWN);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001326
1327 mutex_unlock(&phydev->lock);
1328
1329 return 0;
1330}
1331EXPORT_SYMBOL(genphy_resume);
Andy Fleming00db8182005-07-30 19:31:23 -04001332
stephen hemminger395056e2014-01-19 11:48:20 -08001333static int gen10g_resume(struct phy_device *phydev)
Andy Fleming124059f2014-01-10 14:27:37 +08001334{
1335 return 0;
1336}
Andy Fleming124059f2014-01-10 14:27:37 +08001337
Simon Hormanf3a6bd32015-09-30 15:15:52 +09001338static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
1339{
1340 /* The default values for phydev->supported are provided by the PHY
1341 * driver "features" member, we want to reset to sane defaults first
1342 * before supporting higher speeds.
1343 */
1344 phydev->supported &= PHY_DEFAULT_FEATURES;
1345
1346 switch (max_speed) {
1347 default:
1348 return -ENOTSUPP;
1349 case SPEED_1000:
1350 phydev->supported |= PHY_1000BT_FEATURES;
1351 /* fall through */
1352 case SPEED_100:
1353 phydev->supported |= PHY_100BT_FEATURES;
1354 /* fall through */
1355 case SPEED_10:
1356 phydev->supported |= PHY_10BT_FEATURES;
1357 }
1358
1359 return 0;
1360}
1361
1362int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
1363{
1364 int err;
1365
1366 err = __set_phy_supported(phydev, max_speed);
1367 if (err)
1368 return err;
1369
1370 phydev->advertising = phydev->supported;
1371
1372 return 0;
1373}
1374EXPORT_SYMBOL(phy_set_max_speed);
1375
Sascha Hauerde906af2014-05-21 15:29:45 +02001376static void of_set_phy_supported(struct phy_device *phydev)
1377{
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001378 struct device_node *node = phydev->mdio.dev.of_node;
Sascha Hauerde906af2014-05-21 15:29:45 +02001379 u32 max_speed;
1380
1381 if (!IS_ENABLED(CONFIG_OF_MDIO))
1382 return;
1383
1384 if (!node)
1385 return;
1386
Simon Hormanf3a6bd32015-09-30 15:15:52 +09001387 if (!of_property_read_u32(node, "max-speed", &max_speed))
1388 __set_phy_supported(phydev, max_speed);
Sascha Hauerde906af2014-05-21 15:29:45 +02001389}
1390
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001391/**
1392 * phy_probe - probe and init a PHY device
1393 * @dev: device to probe and init
Andy Fleming00db8182005-07-30 19:31:23 -04001394 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001395 * Description: Take care of setting up the phy_device structure,
Andy Fleming00db8182005-07-30 19:31:23 -04001396 * set the state to READY (the driver's init function should
1397 * set it to STARTING if needed).
1398 */
1399static int phy_probe(struct device *dev)
1400{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001401 struct phy_device *phydev = to_phy_device(dev);
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001402 struct device_driver *drv = phydev->mdio.dev.driver;
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001403 struct phy_driver *phydrv = to_phy_driver(drv);
Andy Fleming00db8182005-07-30 19:31:23 -04001404 int err = 0;
1405
Andy Fleming00db8182005-07-30 19:31:23 -04001406 phydev->drv = phydrv;
1407
Florian Fainelli2c7b4922013-05-19 22:53:42 +00001408 /* Disable the interrupt if the PHY doesn't support it
1409 * but the interrupt is still a valid one
1410 */
1411 if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001412 phy_interrupt_is_valid(phydev))
Andy Fleming00db8182005-07-30 19:31:23 -04001413 phydev->irq = PHY_POLL;
1414
Florian Fainelli4284b6a2013-05-23 01:11:12 +00001415 if (phydrv->flags & PHY_IS_INTERNAL)
1416 phydev->is_internal = true;
1417
Nate Case35b5f6b2008-01-29 10:05:09 -06001418 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001419
1420 /* Start out supporting everything. Eventually,
1421 * a controller will attach, and may modify one
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001422 * or both of these values
1423 */
Andy Fleming00db8182005-07-30 19:31:23 -04001424 phydev->supported = phydrv->features;
Sascha Hauerde906af2014-05-21 15:29:45 +02001425 of_set_phy_supported(phydev);
1426 phydev->advertising = phydev->supported;
Andy Fleming00db8182005-07-30 19:31:23 -04001427
1428 /* Set the state to READY by default */
1429 phydev->state = PHY_READY;
1430
1431 if (phydev->drv->probe)
1432 err = phydev->drv->probe(phydev);
1433
Nate Case35b5f6b2008-01-29 10:05:09 -06001434 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001435
Andy Fleming00db8182005-07-30 19:31:23 -04001436 return err;
1437}
1438
1439static int phy_remove(struct device *dev)
1440{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001441 struct phy_device *phydev = to_phy_device(dev);
Andy Fleming00db8182005-07-30 19:31:23 -04001442
Nate Case35b5f6b2008-01-29 10:05:09 -06001443 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001444 phydev->state = PHY_DOWN;
Nate Case35b5f6b2008-01-29 10:05:09 -06001445 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001446
1447 if (phydev->drv->remove)
1448 phydev->drv->remove(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04001449 phydev->drv = NULL;
1450
1451 return 0;
1452}
1453
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001454/**
1455 * phy_driver_register - register a phy_driver with the PHY layer
1456 * @new_driver: new phy_driver to register
1457 */
Andy Fleming00db8182005-07-30 19:31:23 -04001458int phy_driver_register(struct phy_driver *new_driver)
1459{
1460 int retval;
1461
Andy Fleming00db8182005-07-30 19:31:23 -04001462 new_driver->driver.name = new_driver->name;
1463 new_driver->driver.bus = &mdio_bus_type;
1464 new_driver->driver.probe = phy_probe;
1465 new_driver->driver.remove = phy_remove;
1466
1467 retval = driver_register(&new_driver->driver);
Andy Fleming00db8182005-07-30 19:31:23 -04001468 if (retval) {
Joe Perches8d242482012-06-09 07:49:07 +00001469 pr_err("%s: Error %d in registering driver\n",
1470 new_driver->name, retval);
Andy Fleming00db8182005-07-30 19:31:23 -04001471
1472 return retval;
1473 }
1474
Olof Johanssonf2511f12007-11-04 16:09:23 -06001475 pr_debug("%s: Registered new driver\n", new_driver->name);
Andy Fleming00db8182005-07-30 19:31:23 -04001476
1477 return 0;
1478}
1479EXPORT_SYMBOL(phy_driver_register);
1480
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00001481int phy_drivers_register(struct phy_driver *new_driver, int n)
1482{
1483 int i, ret = 0;
1484
1485 for (i = 0; i < n; i++) {
1486 ret = phy_driver_register(new_driver + i);
1487 if (ret) {
1488 while (i-- > 0)
1489 phy_driver_unregister(new_driver + i);
1490 break;
1491 }
1492 }
1493 return ret;
1494}
1495EXPORT_SYMBOL(phy_drivers_register);
1496
Andy Fleming00db8182005-07-30 19:31:23 -04001497void phy_driver_unregister(struct phy_driver *drv)
1498{
1499 driver_unregister(&drv->driver);
1500}
1501EXPORT_SYMBOL(phy_driver_unregister);
1502
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00001503void phy_drivers_unregister(struct phy_driver *drv, int n)
1504{
1505 int i;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001506
1507 for (i = 0; i < n; i++)
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00001508 phy_driver_unregister(drv + i);
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00001509}
1510EXPORT_SYMBOL(phy_drivers_unregister);
1511
Shaohui Xieab2145e2014-01-10 14:27:22 +08001512static struct phy_driver genphy_driver[] = {
1513{
Andy Fleminge1393452005-08-24 18:46:21 -05001514 .phy_id = 0xffffffff,
1515 .phy_id_mask = 0xffffffff,
1516 .name = "Generic PHY",
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001517 .soft_reset = genphy_soft_reset,
Andy Fleminge1393452005-08-24 18:46:21 -05001518 .config_init = genphy_config_init,
Sascha Hauerc242a472014-05-21 15:29:44 +02001519 .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
1520 SUPPORTED_AUI | SUPPORTED_FIBRE |
1521 SUPPORTED_BNC,
Andy Fleminge1393452005-08-24 18:46:21 -05001522 .config_aneg = genphy_config_aneg,
Florian Fainelli76a423a2014-02-11 17:27:37 -08001523 .aneg_done = genphy_aneg_done,
Andy Fleminge1393452005-08-24 18:46:21 -05001524 .read_status = genphy_read_status,
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001525 .suspend = genphy_suspend,
1526 .resume = genphy_resume,
Florian Fainellie1093742013-12-17 21:38:12 -08001527 .driver = { .owner = THIS_MODULE, },
Andy Fleming124059f2014-01-10 14:27:37 +08001528}, {
1529 .phy_id = 0xffffffff,
1530 .phy_id_mask = 0xffffffff,
1531 .name = "Generic 10G PHY",
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001532 .soft_reset = gen10g_soft_reset,
Andy Fleming124059f2014-01-10 14:27:37 +08001533 .config_init = gen10g_config_init,
1534 .features = 0,
1535 .config_aneg = gen10g_config_aneg,
1536 .read_status = gen10g_read_status,
1537 .suspend = gen10g_suspend,
1538 .resume = gen10g_resume,
1539 .driver = {.owner = THIS_MODULE, },
Shaohui Xieab2145e2014-01-10 14:27:22 +08001540} };
Andy Fleming00db8182005-07-30 19:31:23 -04001541
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001542static int __init phy_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -04001543{
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001544 int rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001545
1546 rc = mdio_bus_init();
1547 if (rc)
Andy Fleminge1393452005-08-24 18:46:21 -05001548 return rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001549
Shaohui Xieab2145e2014-01-10 14:27:22 +08001550 rc = phy_drivers_register(genphy_driver,
1551 ARRAY_SIZE(genphy_driver));
Andy Fleminge1393452005-08-24 18:46:21 -05001552 if (rc)
1553 mdio_bus_exit();
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001554
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001555 return rc;
Andy Fleming00db8182005-07-30 19:31:23 -04001556}
1557
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001558static void __exit phy_exit(void)
Andy Fleming00db8182005-07-30 19:31:23 -04001559{
Shaohui Xieab2145e2014-01-10 14:27:22 +08001560 phy_drivers_unregister(genphy_driver,
1561 ARRAY_SIZE(genphy_driver));
Andy Fleminge1393452005-08-24 18:46:21 -05001562 mdio_bus_exit();
Andy Fleming00db8182005-07-30 19:31:23 -04001563}
1564
Andy Fleminge1393452005-08-24 18:46:21 -05001565subsys_initcall(phy_init);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001566module_exit(phy_exit);