blob: af6cb6556cf90050520b86683c91daf1aee7276f [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
Andrew Lunnbc879222016-01-06 20:11:21 +010066#ifdef CONFIG_PM
67static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
68{
69 struct device_driver *drv = phydev->mdio.dev.driver;
70 struct phy_driver *phydrv = to_phy_driver(drv);
71 struct net_device *netdev = phydev->attached_dev;
72
73 if (!drv || !phydrv->suspend)
74 return false;
75
76 /* PHY not attached? May suspend if the PHY has not already been
77 * suspended as part of a prior call to phy_disconnect() ->
78 * phy_detach() -> phy_suspend() because the parent netdev might be the
79 * MDIO bus driver and clock gated at this point.
80 */
81 if (!netdev)
82 return !phydev->suspended;
83
84 /* Don't suspend PHY if the attached netdev parent may wakeup.
85 * The parent may point to a PCI device, as in tg3 driver.
86 */
87 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
88 return false;
89
90 /* Also don't suspend PHY if the netdev itself may wakeup. This
91 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
92 * e.g. SoC devices.
93 */
94 if (device_may_wakeup(&netdev->dev))
95 return false;
96
97 return true;
98}
99
100static int mdio_bus_phy_suspend(struct device *dev)
101{
102 struct phy_device *phydev = to_phy_device(dev);
103
104 /* We must stop the state machine manually, otherwise it stops out of
105 * control, possibly with the phydev->lock held. Upon resume, netdev
106 * may call phy routines that try to grab the same lock, and that may
107 * lead to a deadlock.
108 */
109 if (phydev->attached_dev && phydev->adjust_link)
110 phy_stop_machine(phydev);
111
112 if (!mdio_bus_phy_may_suspend(phydev))
113 return 0;
114
115 return phy_suspend(phydev);
116}
117
118static int mdio_bus_phy_resume(struct device *dev)
119{
120 struct phy_device *phydev = to_phy_device(dev);
121 int ret;
122
123 if (!mdio_bus_phy_may_suspend(phydev))
124 goto no_resume;
125
126 ret = phy_resume(phydev);
127 if (ret < 0)
128 return ret;
129
130no_resume:
131 if (phydev->attached_dev && phydev->adjust_link)
132 phy_start_machine(phydev);
133
134 return 0;
135}
136
137static int mdio_bus_phy_restore(struct device *dev)
138{
139 struct phy_device *phydev = to_phy_device(dev);
140 struct net_device *netdev = phydev->attached_dev;
141 int ret;
142
143 if (!netdev)
144 return 0;
145
146 ret = phy_init_hw(phydev);
147 if (ret < 0)
148 return ret;
149
150 /* The PHY needs to renegotiate. */
151 phydev->link = 0;
152 phydev->state = PHY_UP;
153
154 phy_start_machine(phydev);
155
156 return 0;
157}
158
159static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
160 .suspend = mdio_bus_phy_suspend,
161 .resume = mdio_bus_phy_resume,
162 .freeze = mdio_bus_phy_suspend,
163 .thaw = mdio_bus_phy_resume,
164 .restore = mdio_bus_phy_restore,
165};
166
167#define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
168
169#else
170
171#define MDIO_BUS_PHY_PM_OPS NULL
172
173#endif /* CONFIG_PM */
174
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300175/**
176 * phy_register_fixup - creates a new phy_fixup and adds it to the list
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100177 * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
Andy Flemingf62220d2008-04-18 17:29:54 -0500178 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300179 * It can also be PHY_ANY_UID
Andy Flemingf62220d2008-04-18 17:29:54 -0500180 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300181 * comparison
Andy Flemingf62220d2008-04-18 17:29:54 -0500182 * @run: The actual code to be run when a matching PHY is found
183 */
184int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300185 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -0500186{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300187 struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
Andy Flemingf62220d2008-04-18 17:29:54 -0500188
Andy Flemingf62220d2008-04-18 17:29:54 -0500189 if (!fixup)
190 return -ENOMEM;
191
Kay Sieversfb28ad32008-11-10 13:55:14 -0800192 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
Andy Flemingf62220d2008-04-18 17:29:54 -0500193 fixup->phy_uid = phy_uid;
194 fixup->phy_uid_mask = phy_uid_mask;
195 fixup->run = run;
196
197 mutex_lock(&phy_fixup_lock);
198 list_add_tail(&fixup->list, &phy_fixup_list);
199 mutex_unlock(&phy_fixup_lock);
200
201 return 0;
202}
203EXPORT_SYMBOL(phy_register_fixup);
204
205/* Registers a fixup to be run on any PHY with the UID in phy_uid */
206int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300207 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -0500208{
209 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
210}
211EXPORT_SYMBOL(phy_register_fixup_for_uid);
212
213/* Registers a fixup to be run on the PHY with id string bus_id */
214int phy_register_fixup_for_id(const char *bus_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300215 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -0500216{
217 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
218}
219EXPORT_SYMBOL(phy_register_fixup_for_id);
220
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300221/* Returns 1 if fixup matches phydev in bus_id and phy_uid.
Andy Flemingf62220d2008-04-18 17:29:54 -0500222 * Fixups can be set to match any in one or more fields.
223 */
224static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
225{
Andrew Lunn84eff6d2016-01-06 20:11:10 +0100226 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
Andy Flemingf62220d2008-04-18 17:29:54 -0500227 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
228 return 0;
229
230 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300231 (phydev->phy_id & fixup->phy_uid_mask))
Andy Flemingf62220d2008-04-18 17:29:54 -0500232 if (fixup->phy_uid != PHY_ANY_UID)
233 return 0;
234
235 return 1;
236}
237
238/* Runs any matching fixups for this phydev */
Sergei Shtylyovfbfcec62014-01-05 03:28:27 +0300239static int phy_scan_fixups(struct phy_device *phydev)
Andy Flemingf62220d2008-04-18 17:29:54 -0500240{
241 struct phy_fixup *fixup;
242
243 mutex_lock(&phy_fixup_lock);
244 list_for_each_entry(fixup, &phy_fixup_list, list) {
245 if (phy_needs_fixup(phydev, fixup)) {
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300246 int err = fixup->run(phydev);
Andy Flemingf62220d2008-04-18 17:29:54 -0500247
Jiri Slabybc232832009-07-13 11:23:39 +0000248 if (err < 0) {
249 mutex_unlock(&phy_fixup_lock);
Andy Flemingf62220d2008-04-18 17:29:54 -0500250 return err;
Jiri Slabybc232832009-07-13 11:23:39 +0000251 }
Florian Fainellib0ae0092014-02-11 17:27:41 -0800252 phydev->has_fixups = true;
Andy Flemingf62220d2008-04-18 17:29:54 -0500253 }
254 }
255 mutex_unlock(&phy_fixup_lock);
256
257 return 0;
258}
Andy Flemingf62220d2008-04-18 17:29:54 -0500259
Andrew Lunne76a4952016-01-06 20:11:23 +0100260static int phy_bus_match(struct device *dev, struct device_driver *drv)
261{
262 struct phy_device *phydev = to_phy_device(dev);
263 struct phy_driver *phydrv = to_phy_driver(drv);
264 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
265 int i;
266
Andrew Lunna9049e02016-01-06 20:11:26 +0100267 if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
268 return 0;
269
Andrew Lunne76a4952016-01-06 20:11:23 +0100270 if (phydrv->match_phy_device)
271 return phydrv->match_phy_device(phydev);
272
273 if (phydev->is_c45) {
274 for (i = 1; i < num_ids; i++) {
275 if (!(phydev->c45_ids.devices_in_package & (1 << i)))
276 continue;
277
278 if ((phydrv->phy_id & phydrv->phy_id_mask) ==
279 (phydev->c45_ids.device_ids[i] &
280 phydrv->phy_id_mask))
281 return 1;
282 }
283 return 0;
284 } else {
285 return (phydrv->phy_id & phydrv->phy_id_mask) ==
286 (phydev->phy_id & phydrv->phy_id_mask);
287 }
288}
289
David Daneyac28b9f2012-06-27 07:33:35 +0000290struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300291 bool is_c45,
292 struct phy_c45_device_ids *c45_ids)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700293{
294 struct phy_device *dev;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100295 struct mdio_device *mdiodev;
David Woodhouse8626d3b2010-04-02 01:05:27 +0000296
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300297 /* We allocate the device, and initialize the default values */
Robert P. J. Daycd861282006-12-13 00:34:52 -0800298 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Sergei Shtylyovef899c02015-08-28 21:35:14 +0300299 if (!dev)
Sergei Shtylyovd3765f02015-08-28 21:34:34 +0300300 return ERR_PTR(-ENOMEM);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700301
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100302 mdiodev = &dev->mdio;
303 mdiodev->dev.release = phy_device_release;
304 mdiodev->dev.parent = &bus->dev;
305 mdiodev->dev.bus = &mdio_bus_type;
306 mdiodev->bus = bus;
Andrew Lunnbc879222016-01-06 20:11:21 +0100307 mdiodev->pm_ops = MDIO_BUS_PHY_PM_OPS;
Andrew Lunne76a4952016-01-06 20:11:23 +0100308 mdiodev->bus_match = phy_bus_match;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100309 mdiodev->addr = addr;
Andrew Lunn7f854422016-01-06 20:11:18 +0100310 mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300311
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700312 dev->speed = 0;
313 dev->duplex = -1;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300314 dev->pause = 0;
315 dev->asym_pause = 0;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700316 dev->link = 1;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600317 dev->interface = PHY_INTERFACE_MODE_GMII;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700318
319 dev->autoneg = AUTONEG_ENABLE;
320
David Daneyac28b9f2012-06-27 07:33:35 +0000321 dev->is_c45 = is_c45;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700322 dev->phy_id = phy_id;
David Daneyac28b9f2012-06-27 07:33:35 +0000323 if (c45_ids)
324 dev->c45_ids = *c45_ids;
Sergei Shtylyovef899c02015-08-28 21:35:14 +0300325 dev->irq = bus->irq ? bus->irq[addr] : PHY_POLL;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100326 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700327
328 dev->state = PHY_DOWN;
329
Nate Case35b5f6b2008-01-29 10:05:09 -0600330 mutex_init(&dev->lock);
Anton Vorontsov4f9c85a2010-01-18 05:37:16 +0000331 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
Florian Fainelli5ea94e72013-05-19 22:53:43 +0000332 INIT_WORK(&dev->phy_queue, phy_change);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700333
David Woodhouse8626d3b2010-04-02 01:05:27 +0000334 /* Request the appropriate module unconditionally; don't
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300335 * bother trying to do so only if it isn't already loaded,
336 * because that gets complicated. A hotplug event would have
337 * done an unconditional modprobe anyway.
338 * We don't do normal hotplug because it won't work for MDIO
339 * -- because it relies on the device staying around for long
340 * enough for the driver to get loaded. With MDIO, the NIC
341 * driver will get bored and give up as soon as it finds that
342 * there's no driver _already_ loaded.
343 */
David Woodhouse8626d3b2010-04-02 01:05:27 +0000344 request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
345
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100346 device_initialize(&mdiodev->dev);
Petr Malatb2a43192013-02-28 01:01:52 +0000347
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700348 return dev;
349}
David Daneyac28b9f2012-06-27 07:33:35 +0000350EXPORT_SYMBOL(phy_device_create);
351
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800352/* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
353 * @bus: the target MII bus
354 * @addr: PHY address on the MII bus
355 * @dev_addr: MMD address in the PHY.
356 * @devices_in_package: where to store the devices in package information.
357 *
358 * Description: reads devices in package registers of a MMD at @dev_addr
359 * from PHY at @addr on @bus.
360 *
361 * Returns: 0 on success, -EIO on failure.
362 */
363static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
364 u32 *devices_in_package)
365{
366 int phy_reg, reg_addr;
367
368 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
369 phy_reg = mdiobus_read(bus, addr, reg_addr);
370 if (phy_reg < 0)
371 return -EIO;
372 *devices_in_package = (phy_reg & 0xffff) << 16;
373
374 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
375 phy_reg = mdiobus_read(bus, addr, reg_addr);
376 if (phy_reg < 0)
377 return -EIO;
378 *devices_in_package |= (phy_reg & 0xffff);
379
380 return 0;
381}
382
David Daneyac28b9f2012-06-27 07:33:35 +0000383/**
384 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
385 * @bus: the target MII bus
386 * @addr: PHY address on the MII bus
387 * @phy_id: where to store the ID retrieved.
388 * @c45_ids: where to store the c45 ID information.
389 *
390 * If the PHY devices-in-package appears to be valid, it and the
391 * corresponding identifiers are stored in @c45_ids, zero is stored
392 * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns
393 * zero on success.
394 *
395 */
396static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
397 struct phy_c45_device_ids *c45_ids) {
398 int phy_reg;
399 int i, reg_addr;
400 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800401 u32 *devs = &c45_ids->devices_in_package;
David Daneyac28b9f2012-06-27 07:33:35 +0000402
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800403 /* Find first non-zero Devices In package. Device zero is reserved
404 * for 802.3 c45 complied PHYs, so don't probe it at first.
David Daneyac28b9f2012-06-27 07:33:35 +0000405 */
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800406 for (i = 1; i < num_ids && *devs == 0; i++) {
407 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
David Daneyac28b9f2012-06-27 07:33:35 +0000408 if (phy_reg < 0)
409 return -EIO;
David Daneyac28b9f2012-06-27 07:33:35 +0000410
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800411 if ((*devs & 0x1fffffff) == 0x1fffffff) {
412 /* If mostly Fs, there is no device there,
413 * then let's continue to probe more, as some
414 * 10G PHYs have zero Devices In package,
415 * e.g. Cortina CS4315/CS4340 PHY.
416 */
417 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
418 if (phy_reg < 0)
419 return -EIO;
420 /* no device there, let's get out of here */
421 if ((*devs & 0x1fffffff) == 0x1fffffff) {
Shengzhou Liuda1da282015-06-26 17:58:52 +0800422 *phy_id = 0xffffffff;
423 return 0;
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800424 } else {
425 break;
Shengzhou Liuda1da282015-06-26 17:58:52 +0800426 }
David Daneyac28b9f2012-06-27 07:33:35 +0000427 }
428 }
429
430 /* Now probe Device Identifiers for each device present. */
431 for (i = 1; i < num_ids; i++) {
432 if (!(c45_ids->devices_in_package & (1 << i)))
433 continue;
434
435 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
436 phy_reg = mdiobus_read(bus, addr, reg_addr);
437 if (phy_reg < 0)
438 return -EIO;
439 c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16;
440
441 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
442 phy_reg = mdiobus_read(bus, addr, reg_addr);
443 if (phy_reg < 0)
444 return -EIO;
445 c45_ids->device_ids[i] |= (phy_reg & 0xffff);
446 }
447 *phy_id = 0;
448 return 0;
449}
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700450
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800451/**
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400452 * get_phy_id - reads the specified addr for its ID.
453 * @bus: the target MII bus
454 * @addr: PHY address on the MII bus
455 * @phy_id: where to store the ID retrieved.
David Daneyac28b9f2012-06-27 07:33:35 +0000456 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
457 * @c45_ids: where to store the c45 ID information.
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400458 *
David Daneyac28b9f2012-06-27 07:33:35 +0000459 * Description: In the case of a 802.3-c22 PHY, reads the ID registers
460 * of the PHY at @addr on the @bus, stores it in @phy_id and returns
461 * zero on success.
462 *
463 * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
464 * its return value is in turn returned.
465 *
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400466 */
David Daneyac28b9f2012-06-27 07:33:35 +0000467static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
468 bool is_c45, struct phy_c45_device_ids *c45_ids)
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400469{
470 int phy_reg;
471
David Daneyac28b9f2012-06-27 07:33:35 +0000472 if (is_c45)
473 return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
474
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300475 /* Grab the bits from PHYIR1, and put them in the upper half */
David Daney6fe32642011-09-30 11:51:22 +0000476 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400477 if (phy_reg < 0)
478 return -EIO;
479
480 *phy_id = (phy_reg & 0xffff) << 16;
481
482 /* Grab the bits from PHYIR2, and put them in the lower half */
David Daney6fe32642011-09-30 11:51:22 +0000483 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400484 if (phy_reg < 0)
485 return -EIO;
486
487 *phy_id |= (phy_reg & 0xffff);
488
489 return 0;
490}
491
492/**
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300493 * get_phy_device - reads the specified PHY device and returns its @phy_device
494 * struct
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800495 * @bus: the target MII bus
496 * @addr: PHY address on the MII bus
David Daneyac28b9f2012-06-27 07:33:35 +0000497 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
Andy Fleming00db8182005-07-30 19:31:23 -0400498 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800499 * Description: Reads the ID registers of the PHY at @addr on the
500 * @bus, then allocates and returns the phy_device to represent it.
Andy Fleming00db8182005-07-30 19:31:23 -0400501 */
David Daneyac28b9f2012-06-27 07:33:35 +0000502struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
Andy Fleming00db8182005-07-30 19:31:23 -0400503{
David Daneyac28b9f2012-06-27 07:33:35 +0000504 struct phy_c45_device_ids c45_ids = {0};
David S. Miller160c85f2012-06-27 21:28:14 -0700505 u32 phy_id = 0;
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400506 int r;
Andy Fleming00db8182005-07-30 19:31:23 -0400507
David Daneyac28b9f2012-06-27 07:33:35 +0000508 r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400509 if (r)
510 return ERR_PTR(r);
Andy Fleming00db8182005-07-30 19:31:23 -0400511
Giuseppe Cavallaro6436cbc2008-11-20 20:43:18 -0800512 /* If the phy_id is mostly Fs, there is no device there */
513 if ((phy_id & 0x1fffffff) == 0x1fffffff)
514 return NULL;
515
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300516 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
Andy Fleming00db8182005-07-30 19:31:23 -0400517}
Grant Likely4dea5472009-04-25 12:52:46 +0000518EXPORT_SYMBOL(get_phy_device);
519
Andrew Lunn5cf11be2016-01-06 20:11:19 +0100520static ssize_t
521phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
522{
523 struct phy_device *phydev = to_phy_device(dev);
524
525 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
526}
527static DEVICE_ATTR_RO(phy_id);
528
529static ssize_t
530phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
531{
532 struct phy_device *phydev = to_phy_device(dev);
533 const char *mode = NULL;
534
535 if (phy_is_internal(phydev))
536 mode = "internal";
537 else
538 mode = phy_modes(phydev->interface);
539
540 return sprintf(buf, "%s\n", mode);
541}
542static DEVICE_ATTR_RO(phy_interface);
543
544static ssize_t
545phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
546 char *buf)
547{
548 struct phy_device *phydev = to_phy_device(dev);
549
550 return sprintf(buf, "%d\n", phydev->has_fixups);
551}
552static DEVICE_ATTR_RO(phy_has_fixups);
553
554static struct attribute *phy_dev_attrs[] = {
555 &dev_attr_phy_id.attr,
556 &dev_attr_phy_interface.attr,
557 &dev_attr_phy_has_fixups.attr,
558 NULL,
559};
560ATTRIBUTE_GROUPS(phy_dev);
561
Grant Likely4dea5472009-04-25 12:52:46 +0000562/**
563 * phy_device_register - Register the phy device on the MDIO bus
Randy Dunlap1d4ac5d2009-06-16 16:56:33 +0000564 * @phydev: phy_device structure to be added to the MDIO bus
Grant Likely4dea5472009-04-25 12:52:46 +0000565 */
566int phy_device_register(struct phy_device *phydev)
567{
568 int err;
569
Andrew Lunn7f854422016-01-06 20:11:18 +0100570 err = mdiobus_register_device(&phydev->mdio);
571 if (err)
572 return err;
Grant Likely4dea5472009-04-25 12:52:46 +0000573
574 /* Run all of the fixups for this PHY */
Florian Fainellid92f5de2014-07-28 16:28:07 -0700575 err = phy_scan_fixups(phydev);
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800576 if (err) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100577 pr_err("PHY %d failed to initialize\n", phydev->mdio.addr);
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800578 goto out;
579 }
Grant Likely4dea5472009-04-25 12:52:46 +0000580
Andrew Lunn5cf11be2016-01-06 20:11:19 +0100581 phydev->mdio.dev.groups = phy_dev_groups;
582
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100583 err = device_add(&phydev->mdio.dev);
Grant Likely4dea5472009-04-25 12:52:46 +0000584 if (err) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100585 pr_err("PHY %d failed to add\n", phydev->mdio.addr);
Grant Likely4dea5472009-04-25 12:52:46 +0000586 goto out;
587 }
588
589 return 0;
590
591 out:
Andrew Lunn7f854422016-01-06 20:11:18 +0100592 mdiobus_unregister_device(&phydev->mdio);
Grant Likely4dea5472009-04-25 12:52:46 +0000593 return err;
594}
595EXPORT_SYMBOL(phy_device_register);
Andy Fleming00db8182005-07-30 19:31:23 -0400596
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800597/**
Russell King38737e42015-09-24 20:36:28 +0100598 * phy_device_remove - Remove a previously registered phy device from the MDIO bus
599 * @phydev: phy_device structure to remove
600 *
601 * This doesn't free the phy_device itself, it merely reverses the effects
602 * of phy_device_register(). Use phy_device_free() to free the device
603 * after calling this function.
604 */
605void phy_device_remove(struct phy_device *phydev)
606{
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100607 device_del(&phydev->mdio.dev);
Andrew Lunn7f854422016-01-06 20:11:18 +0100608 mdiobus_unregister_device(&phydev->mdio);
Russell King38737e42015-09-24 20:36:28 +0100609}
610EXPORT_SYMBOL(phy_device_remove);
611
612/**
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800613 * phy_find_first - finds the first PHY device on the bus
614 * @bus: the target MII bus
615 */
616struct phy_device *phy_find_first(struct mii_bus *bus)
617{
Andrew Lunn7f854422016-01-06 20:11:18 +0100618 struct phy_device *phydev;
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800619 int addr;
620
621 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
Andrew Lunn7f854422016-01-06 20:11:18 +0100622 phydev = mdiobus_get_phy(bus, addr);
623 if (phydev)
624 return phydev;
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800625 }
626 return NULL;
627}
628EXPORT_SYMBOL(phy_find_first);
629
630/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800631 * phy_prepare_link - prepares the PHY layer to monitor link status
632 * @phydev: target phy_device struct
633 * @handler: callback function for link status change notifications
Andy Fleming00db8182005-07-30 19:31:23 -0400634 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800635 * Description: Tells the PHY infrastructure to handle the
Andy Fleming00db8182005-07-30 19:31:23 -0400636 * gory details on monitoring link status (whether through
637 * polling or an interrupt), and to call back to the
638 * connected device driver when the link status changes.
639 * If you want to monitor your own link state, don't call
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800640 * this function.
641 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000642static void phy_prepare_link(struct phy_device *phydev,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300643 void (*handler)(struct net_device *))
Andy Fleming00db8182005-07-30 19:31:23 -0400644{
645 phydev->adjust_link = handler;
646}
647
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800648/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000649 * phy_connect_direct - connect an ethernet device to a specific phy_device
650 * @dev: the network device to connect
651 * @phydev: the pointer to the phy device
652 * @handler: callback function for state change notifications
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000653 * @interface: PHY device's interface
654 */
655int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
Florian Fainellif9a8f832013-01-14 00:52:52 +0000656 void (*handler)(struct net_device *),
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000657 phy_interface_t interface)
658{
659 int rc;
660
Florian Fainellif9a8f832013-01-14 00:52:52 +0000661 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000662 if (rc)
663 return rc;
664
665 phy_prepare_link(phydev, handler);
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300666 phy_start_machine(phydev);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000667 if (phydev->irq > 0)
668 phy_start_interrupts(phydev);
669
670 return 0;
671}
672EXPORT_SYMBOL(phy_connect_direct);
673
674/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800675 * phy_connect - connect an ethernet device to a PHY device
676 * @dev: the network device to connect
Randy Dunlap5d12b132008-04-28 10:58:22 -0700677 * @bus_id: the id string of the PHY device to connect
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800678 * @handler: callback function for state change notifications
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800679 * @interface: PHY device's interface
Andy Fleminge1393452005-08-24 18:46:21 -0500680 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800681 * Description: Convenience function for connecting ethernet
Andy Fleminge1393452005-08-24 18:46:21 -0500682 * devices to PHY devices. The default behavior is for
683 * the PHY infrastructure to handle everything, and only notify
684 * the connected driver when the link status changes. If you
685 * don't want, or can't use the provided functionality, you may
686 * choose to call only the subset of functions which provide
687 * the desired functionality.
688 */
Florian Fainellie1093742013-12-17 21:38:12 -0800689struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300690 void (*handler)(struct net_device *),
691 phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500692{
693 struct phy_device *phydev;
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000694 struct device *d;
695 int rc;
Andy Fleminge1393452005-08-24 18:46:21 -0500696
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000697 /* Search the list of PHY devices on the mdio bus for the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300698 * PHY with the requested name
699 */
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000700 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
701 if (!d) {
702 pr_err("PHY %s not found\n", bus_id);
703 return ERR_PTR(-ENODEV);
704 }
705 phydev = to_phy_device(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500706
Florian Fainellif9a8f832013-01-14 00:52:52 +0000707 rc = phy_connect_direct(dev, phydev, handler, interface);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000708 if (rc)
709 return ERR_PTR(rc);
Andy Fleminge1393452005-08-24 18:46:21 -0500710
711 return phydev;
712}
713EXPORT_SYMBOL(phy_connect);
714
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800715/**
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300716 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
717 * device
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800718 * @phydev: target phy_device struct
719 */
Andy Fleminge1393452005-08-24 18:46:21 -0500720void phy_disconnect(struct phy_device *phydev)
721{
722 if (phydev->irq > 0)
723 phy_stop_interrupts(phydev);
724
725 phy_stop_machine(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800726
Andy Fleminge1393452005-08-24 18:46:21 -0500727 phydev->adjust_link = NULL;
728
729 phy_detach(phydev);
730}
731EXPORT_SYMBOL(phy_disconnect);
732
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800733/**
734 * phy_poll_reset - Safely wait until a PHY reset has properly completed
735 * @phydev: The PHY device to poll
736 *
737 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
738 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
739 * register must be polled until the BMCR_RESET bit clears.
740 *
741 * Furthermore, any attempts to write to PHY registers may have no effect
742 * or even generate MDIO bus errors until this is complete.
743 *
744 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
745 * standard and do not fully reset after the BMCR_RESET bit is set, and may
746 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
747 * effort to support such broken PHYs, this function is separate from the
748 * standard phy_init_hw() which will zero all the other bits in the BMCR
749 * and reapply all driver-specific and board-specific fixups.
750 */
751static int phy_poll_reset(struct phy_device *phydev)
752{
753 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
754 unsigned int retries = 12;
755 int ret;
756
757 do {
758 msleep(50);
759 ret = phy_read(phydev, MII_BMCR);
760 if (ret < 0)
761 return ret;
762 } while (ret & BMCR_RESET && --retries);
763 if (ret & BMCR_RESET)
764 return -ETIMEDOUT;
765
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300766 /* Some chips (smsc911x) may still need up to another 1ms after the
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800767 * BMCR_RESET bit is cleared before they are usable.
768 */
769 msleep(1);
770 return 0;
771}
772
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000773int phy_init_hw(struct phy_device *phydev)
774{
Florian Fainelli9df81dd2014-02-17 13:34:03 -0800775 int ret = 0;
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000776
777 if (!phydev->drv || !phydev->drv->config_init)
778 return 0;
779
Florian Fainelli9df81dd2014-02-17 13:34:03 -0800780 if (phydev->drv->soft_reset)
781 ret = phydev->drv->soft_reset(phydev);
782 else
783 ret = genphy_soft_reset(phydev);
784
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800785 if (ret < 0)
786 return ret;
787
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000788 ret = phy_scan_fixups(phydev);
789 if (ret < 0)
790 return ret;
791
792 return phydev->drv->config_init(phydev);
793}
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800794EXPORT_SYMBOL(phy_init_hw);
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000795
Andrew Lunn22209432016-01-06 20:11:13 +0100796void phy_attached_info(struct phy_device *phydev)
797{
798 phy_attached_print(phydev, NULL);
799}
800EXPORT_SYMBOL(phy_attached_info);
801
802#define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)"
803void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
804{
805 if (!fmt) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100806 dev_info(&phydev->mdio.dev, ATTACHED_FMT "\n",
Andrew Lunn22209432016-01-06 20:11:13 +0100807 phydev->drv->name, phydev_name(phydev),
808 phydev->irq);
809 } else {
810 va_list ap;
811
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100812 dev_info(&phydev->mdio.dev, ATTACHED_FMT,
Andrew Lunn22209432016-01-06 20:11:13 +0100813 phydev->drv->name, phydev_name(phydev),
814 phydev->irq);
815
816 va_start(ap, fmt);
817 vprintk(fmt, ap);
818 va_end(ap);
819 }
820}
821EXPORT_SYMBOL(phy_attached_print);
822
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800823/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000824 * phy_attach_direct - attach a network device to a given PHY device pointer
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800825 * @dev: network device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000826 * @phydev: Pointer to phy_device to attach
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800827 * @flags: PHY device's dev_flags
828 * @interface: PHY device's interface
829 *
830 * Description: Called by drivers to attach to a particular PHY
831 * device. The phy_device is found, and properly hooked up
Andy Fleming257184d2014-01-10 14:27:54 +0800832 * to the phy_driver. If no driver is attached, then a
833 * generic driver is used. The phy_device is given a ptr to
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800834 * the attaching device, and given a callback for link status
835 * change. The phy_device is returned to the attaching driver.
Russell King73229672015-09-24 20:36:08 +0100836 * This function takes a reference on the phy device.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800837 */
Andy Fleming257184d2014-01-10 14:27:54 +0800838int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
839 u32 flags, phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500840{
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100841 struct mii_bus *bus = phydev->mdio.bus;
842 struct device *d = &phydev->mdio.dev;
Marc Kleine-Budded005a092011-03-28 14:54:08 +0000843 int err;
Andy Fleminge1393452005-08-24 18:46:21 -0500844
Russell King3e3aaf62015-09-24 20:36:02 +0100845 if (!try_module_get(bus->owner)) {
846 dev_err(&dev->dev, "failed to get the bus module\n");
847 return -EIO;
848 }
849
Russell King73229672015-09-24 20:36:08 +0100850 get_device(d);
851
Andy Fleminge1393452005-08-24 18:46:21 -0500852 /* Assume that if there is no driver, that it doesn't
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300853 * exist, and we should use the genphy driver.
854 */
Sergei Shtylyovef899c02015-08-28 21:35:14 +0300855 if (!d->driver) {
Andy Fleming257184d2014-01-10 14:27:54 +0800856 if (phydev->is_c45)
Andrew Lunna9049e02016-01-06 20:11:26 +0100857 d->driver =
858 &genphy_driver[GENPHY_DRV_10G].mdiodrv.driver;
Andy Fleming257184d2014-01-10 14:27:54 +0800859 else
Andrew Lunna9049e02016-01-06 20:11:26 +0100860 d->driver =
861 &genphy_driver[GENPHY_DRV_1G].mdiodrv.driver;
Andy Fleminge1393452005-08-24 18:46:21 -0500862
863 err = d->driver->probe(d);
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400864 if (err >= 0)
865 err = device_bind_driver(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500866
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400867 if (err)
Russell King3e3aaf62015-09-24 20:36:02 +0100868 goto error;
Andy Fleminge1393452005-08-24 18:46:21 -0500869 }
870
871 if (phydev->attached_dev) {
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000872 dev_err(&dev->dev, "PHY already attached\n");
Russell King3e3aaf62015-09-24 20:36:02 +0100873 err = -EBUSY;
874 goto error;
Ezequiel Garciab3565f22014-07-23 16:47:32 -0300875 }
876
Andy Fleminge1393452005-08-24 18:46:21 -0500877 phydev->attached_dev = dev;
Richard Cochranc1f19b52010-07-17 08:49:36 +0000878 dev->phydev = phydev;
Andy Fleminge1393452005-08-24 18:46:21 -0500879
880 phydev->dev_flags = flags;
881
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600882 phydev->interface = interface;
883
Anton Vorontsovef24b162010-08-24 14:46:12 -0700884 phydev->state = PHY_READY;
885
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600886 /* Do initial configuration here, now that
887 * we have certain key parameters
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300888 * (dev_flags and interface)
889 */
Marc Kleine-Budded005a092011-03-28 14:54:08 +0000890 err = phy_init_hw(phydev);
891 if (err)
892 phy_detach(phydev);
Guenter Roeckb3947452014-05-14 13:12:49 -0700893 else
894 phy_resume(phydev);
Sebastian Hesselbarth1211ce52013-12-13 10:20:28 +0100895
Marc Kleine-Budded005a092011-03-28 14:54:08 +0000896 return err;
Russell King3e3aaf62015-09-24 20:36:02 +0100897
898error:
Russell King73229672015-09-24 20:36:08 +0100899 put_device(d);
Russell King3e3aaf62015-09-24 20:36:02 +0100900 module_put(bus->owner);
901 return err;
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000902}
Andy Fleming257184d2014-01-10 14:27:54 +0800903EXPORT_SYMBOL(phy_attach_direct);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000904
905/**
906 * phy_attach - attach a network device to a particular PHY device
907 * @dev: network device to attach
908 * @bus_id: Bus ID of PHY device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000909 * @interface: PHY device's interface
910 *
911 * Description: Same as phy_attach_direct() except that a PHY bus_id
912 * string is passed instead of a pointer to a struct phy_device.
913 */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300914struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
915 phy_interface_t interface)
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000916{
917 struct bus_type *bus = &mdio_bus_type;
918 struct phy_device *phydev;
919 struct device *d;
920 int rc;
921
922 /* Search the list of PHY devices on the mdio bus for the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300923 * PHY with the requested name
924 */
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000925 d = bus_find_device_by_name(bus, NULL, bus_id);
926 if (!d) {
927 pr_err("PHY %s not found\n", bus_id);
928 return ERR_PTR(-ENODEV);
929 }
930 phydev = to_phy_device(d);
931
Florian Fainellif9a8f832013-01-14 00:52:52 +0000932 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000933 if (rc)
934 return ERR_PTR(rc);
935
Andy Fleminge1393452005-08-24 18:46:21 -0500936 return phydev;
937}
938EXPORT_SYMBOL(phy_attach);
939
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800940/**
941 * phy_detach - detach a PHY device from its network device
942 * @phydev: target phy_device struct
Russell King73229672015-09-24 20:36:08 +0100943 *
944 * This detaches the phy device from its network device and the phy
945 * driver, and drops the reference count taken in phy_attach_direct().
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800946 */
Andy Fleminge1393452005-08-24 18:46:21 -0500947void phy_detach(struct phy_device *phydev)
948{
Russell King3e3aaf62015-09-24 20:36:02 +0100949 struct mii_bus *bus;
Shaohui Xieab2145e2014-01-10 14:27:22 +0800950 int i;
Ezequiel Garciab3565f22014-07-23 16:47:32 -0300951
Richard Cochranc1f19b52010-07-17 08:49:36 +0000952 phydev->attached_dev->phydev = NULL;
Andy Fleminge1393452005-08-24 18:46:21 -0500953 phydev->attached_dev = NULL;
Sebastian Hesselbarth1211ce52013-12-13 10:20:28 +0100954 phy_suspend(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500955
956 /* If the device had no specific driver before (i.e. - it
957 * was using the generic driver), we unbind the device
958 * from the generic driver so that there's a chance a
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300959 * real driver could be loaded
960 */
Shaohui Xieab2145e2014-01-10 14:27:22 +0800961 for (i = 0; i < ARRAY_SIZE(genphy_driver); i++) {
Andrew Lunna9049e02016-01-06 20:11:26 +0100962 if (phydev->mdio.dev.driver ==
963 &genphy_driver[i].mdiodrv.driver) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100964 device_release_driver(&phydev->mdio.dev);
Shaohui Xieab2145e2014-01-10 14:27:22 +0800965 break;
966 }
967 }
Russell King3e3aaf62015-09-24 20:36:02 +0100968
Russell King73229672015-09-24 20:36:08 +0100969 /*
970 * The phydev might go away on the put_device() below, so avoid
971 * a use-after-free bug by reading the underlying bus first.
972 */
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100973 bus = phydev->mdio.bus;
Russell King3e3aaf62015-09-24 20:36:02 +0100974
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100975 put_device(&phydev->mdio.dev);
Russell King3e3aaf62015-09-24 20:36:02 +0100976 module_put(bus->owner);
Andy Fleminge1393452005-08-24 18:46:21 -0500977}
978EXPORT_SYMBOL(phy_detach);
979
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +0100980int phy_suspend(struct phy_device *phydev)
981{
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100982 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
Sebastian Hesselbarth32fc3fd2014-03-14 10:07:44 +0100983 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Florian Fainelli8a477a62015-01-26 22:05:39 -0800984 int ret = 0;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +0100985
986 /* If the device has WOL enabled, we cannot suspend the PHY */
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +0100987 phy_ethtool_get_wol(phydev, &wol);
988 if (wol.wolopts)
989 return -EBUSY;
990
991 if (phydrv->suspend)
Florian Fainelli8a477a62015-01-26 22:05:39 -0800992 ret = phydrv->suspend(phydev);
993
994 if (ret)
995 return ret;
996
997 phydev->suspended = true;
998
999 return ret;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001000}
Florian Fainellica127692014-07-08 10:38:36 -07001001EXPORT_SYMBOL(phy_suspend);
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001002
1003int phy_resume(struct phy_device *phydev)
1004{
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001005 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
Florian Fainelli8a477a62015-01-26 22:05:39 -08001006 int ret = 0;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001007
1008 if (phydrv->resume)
Florian Fainelli8a477a62015-01-26 22:05:39 -08001009 ret = phydrv->resume(phydev);
1010
1011 if (ret)
1012 return ret;
1013
1014 phydev->suspended = false;
1015
1016 return ret;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001017}
Florian Fainellica127692014-07-08 10:38:36 -07001018EXPORT_SYMBOL(phy_resume);
Andy Fleminge1393452005-08-24 18:46:21 -05001019
Andy Fleming00db8182005-07-30 19:31:23 -04001020/* Generic PHY support and helper functions */
1021
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001022/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001023 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001024 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001025 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001026 * Description: Writes MII_ADVERTISE with the appropriate values,
Andy Fleming00db8182005-07-30 19:31:23 -04001027 * after sanitizing the values to make sure we only advertise
Trent Piepho51e2a382008-09-24 10:55:46 +00001028 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1029 * hasn't changed, and > 0 if it has changed.
Andy Fleming00db8182005-07-30 19:31:23 -04001030 */
stephen hemminger89ff05e2010-10-21 08:37:41 +00001031static int genphy_config_advert(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -04001032{
1033 u32 advertise;
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001034 int oldadv, adv, bmsr;
Trent Piepho51e2a382008-09-24 10:55:46 +00001035 int err, changed = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001036
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001037 /* Only allow advertising what this PHY supports */
Andy Fleming00db8182005-07-30 19:31:23 -04001038 phydev->advertising &= phydev->supported;
1039 advertise = phydev->advertising;
1040
1041 /* Setup standard advertisement */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001042 adv = phy_read(phydev, MII_ADVERTISE);
Andy Fleming00db8182005-07-30 19:31:23 -04001043 if (adv < 0)
1044 return adv;
1045
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001046 oldadv = adv;
Matt Carlson28011cf2011-11-16 18:36:59 -05001047 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
Andy Fleming00db8182005-07-30 19:31:23 -04001048 ADVERTISE_PAUSE_ASYM);
Matt Carlson37f07022011-11-17 14:30:55 +00001049 adv |= ethtool_adv_to_mii_adv_t(advertise);
Andy Fleming00db8182005-07-30 19:31:23 -04001050
Trent Piepho51e2a382008-09-24 10:55:46 +00001051 if (adv != oldadv) {
1052 err = phy_write(phydev, MII_ADVERTISE, adv);
Andy Fleming00db8182005-07-30 19:31:23 -04001053
Trent Piepho51e2a382008-09-24 10:55:46 +00001054 if (err < 0)
1055 return err;
1056 changed = 1;
1057 }
Andy Fleming00db8182005-07-30 19:31:23 -04001058
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001059 bmsr = phy_read(phydev, MII_BMSR);
1060 if (bmsr < 0)
1061 return bmsr;
1062
1063 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1064 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1065 * logical 1.
1066 */
1067 if (!(bmsr & BMSR_ESTATEN))
1068 return changed;
1069
Andy Fleming00db8182005-07-30 19:31:23 -04001070 /* Configure gigabit if it's supported */
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001071 adv = phy_read(phydev, MII_CTRL1000);
1072 if (adv < 0)
1073 return adv;
1074
1075 oldadv = adv;
1076 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1077
Andy Fleming00db8182005-07-30 19:31:23 -04001078 if (phydev->supported & (SUPPORTED_1000baseT_Half |
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001079 SUPPORTED_1000baseT_Full)) {
Matt Carlson37f07022011-11-17 14:30:55 +00001080 adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
Andy Fleming00db8182005-07-30 19:31:23 -04001081 }
1082
Mugunthan V Neb686232015-06-25 22:21:02 +05301083 if (adv != oldadv)
1084 changed = 1;
1085
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001086 err = phy_write(phydev, MII_CTRL1000, adv);
1087 if (err < 0)
1088 return err;
1089
Trent Piepho51e2a382008-09-24 10:55:46 +00001090 return changed;
Andy Fleming00db8182005-07-30 19:31:23 -04001091}
Andy Fleming00db8182005-07-30 19:31:23 -04001092
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001093/**
1094 * genphy_setup_forced - configures/forces speed/duplex from @phydev
1095 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001096 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001097 * Description: Configures MII_BMCR to force speed/duplex
Andy Fleming00db8182005-07-30 19:31:23 -04001098 * to the values in phydev. Assumes that the values are valid.
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001099 * Please see phy_sanitize_settings().
1100 */
Madalin Bucur3fb69bc2013-11-20 16:38:19 -06001101int genphy_setup_forced(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -04001102{
Domen Puncerbc1e0a02007-08-17 08:54:45 +02001103 int ctl = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001104
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001105 phydev->pause = 0;
1106 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001107
1108 if (SPEED_1000 == phydev->speed)
1109 ctl |= BMCR_SPEED1000;
1110 else if (SPEED_100 == phydev->speed)
1111 ctl |= BMCR_SPEED100;
1112
1113 if (DUPLEX_FULL == phydev->duplex)
1114 ctl |= BMCR_FULLDPLX;
Florian Fainellie1093742013-12-17 21:38:12 -08001115
Sergei Shtylyove62a7682014-01-05 03:21:52 +03001116 return phy_write(phydev, MII_BMCR, ctl);
Andy Fleming00db8182005-07-30 19:31:23 -04001117}
Madalin Bucur3fb69bc2013-11-20 16:38:19 -06001118EXPORT_SYMBOL(genphy_setup_forced);
Andy Fleming00db8182005-07-30 19:31:23 -04001119
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001120/**
1121 * genphy_restart_aneg - Enable and Restart Autonegotiation
1122 * @phydev: target phy_device struct
1123 */
Andy Fleming00db8182005-07-30 19:31:23 -04001124int genphy_restart_aneg(struct phy_device *phydev)
1125{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001126 int ctl = phy_read(phydev, MII_BMCR);
Andy Fleming00db8182005-07-30 19:31:23 -04001127
1128 if (ctl < 0)
1129 return ctl;
1130
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001131 ctl |= BMCR_ANENABLE | BMCR_ANRESTART;
Andy Fleming00db8182005-07-30 19:31:23 -04001132
1133 /* Don't isolate the PHY if we're negotiating */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001134 ctl &= ~BMCR_ISOLATE;
Andy Fleming00db8182005-07-30 19:31:23 -04001135
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001136 return phy_write(phydev, MII_BMCR, ctl);
Andy Fleming00db8182005-07-30 19:31:23 -04001137}
Adrian Bunk892871d2008-10-13 18:48:09 -07001138EXPORT_SYMBOL(genphy_restart_aneg);
Andy Fleming00db8182005-07-30 19:31:23 -04001139
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001140/**
1141 * genphy_config_aneg - restart auto-negotiation or write BMCR
1142 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001143 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001144 * Description: If auto-negotiation is enabled, we configure the
Andy Fleming00db8182005-07-30 19:31:23 -04001145 * advertising, and then restart auto-negotiation. If it is not
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001146 * enabled, then we write the BMCR.
Andy Fleming00db8182005-07-30 19:31:23 -04001147 */
1148int genphy_config_aneg(struct phy_device *phydev)
1149{
Trent Piephode339c22008-11-19 15:52:41 -08001150 int result;
Andy Fleming00db8182005-07-30 19:31:23 -04001151
Trent Piephode339c22008-11-19 15:52:41 -08001152 if (AUTONEG_ENABLE != phydev->autoneg)
1153 return genphy_setup_forced(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04001154
Trent Piephode339c22008-11-19 15:52:41 -08001155 result = genphy_config_advert(phydev);
Trent Piephode339c22008-11-19 15:52:41 -08001156 if (result < 0) /* error */
1157 return result;
Trent Piephode339c22008-11-19 15:52:41 -08001158 if (result == 0) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001159 /* Advertisement hasn't changed, but maybe aneg was never on to
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001160 * begin with? Or maybe phy was isolated?
1161 */
Trent Piephode339c22008-11-19 15:52:41 -08001162 int ctl = phy_read(phydev, MII_BMCR);
1163
1164 if (ctl < 0)
1165 return ctl;
1166
1167 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
1168 result = 1; /* do restart aneg */
1169 }
1170
1171 /* Only restart aneg if we are advertising something different
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001172 * than we were before.
1173 */
Trent Piephode339c22008-11-19 15:52:41 -08001174 if (result > 0)
1175 result = genphy_restart_aneg(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04001176
Trent Piepho51e2a382008-09-24 10:55:46 +00001177 return result;
Andy Fleming00db8182005-07-30 19:31:23 -04001178}
1179EXPORT_SYMBOL(genphy_config_aneg);
1180
Florian Fainellia9fa6e62014-02-11 17:27:36 -08001181/**
1182 * genphy_aneg_done - return auto-negotiation status
1183 * @phydev: target phy_device struct
1184 *
1185 * Description: Reads the status register and returns 0 either if
1186 * auto-negotiation is incomplete, or if there was an error.
1187 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
1188 */
1189int genphy_aneg_done(struct phy_device *phydev)
1190{
1191 int retval = phy_read(phydev, MII_BMSR);
1192
1193 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1194}
1195EXPORT_SYMBOL(genphy_aneg_done);
1196
stephen hemminger395056e2014-01-19 11:48:20 -08001197static int gen10g_config_aneg(struct phy_device *phydev)
Andy Fleming124059f2014-01-10 14:27:37 +08001198{
1199 return 0;
1200}
Andy Fleming124059f2014-01-10 14:27:37 +08001201
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001202/**
1203 * genphy_update_link - update link status in @phydev
1204 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001205 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001206 * Description: Update the value in phydev->link to reflect the
Andy Fleming00db8182005-07-30 19:31:23 -04001207 * current link value. In order to do this, we need to read
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001208 * the status register twice, keeping the second value.
Andy Fleming00db8182005-07-30 19:31:23 -04001209 */
1210int genphy_update_link(struct phy_device *phydev)
1211{
1212 int status;
1213
1214 /* Do a fake read */
1215 status = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -04001216 if (status < 0)
1217 return status;
1218
1219 /* Read link and autonegotiation status */
1220 status = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -04001221 if (status < 0)
1222 return status;
1223
1224 if ((status & BMSR_LSTATUS) == 0)
1225 phydev->link = 0;
1226 else
1227 phydev->link = 1;
1228
1229 return 0;
1230}
Andy Fleming6b655522006-10-16 16:19:17 -05001231EXPORT_SYMBOL(genphy_update_link);
Andy Fleming00db8182005-07-30 19:31:23 -04001232
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001233/**
1234 * genphy_read_status - check the link status and update current link state
1235 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001236 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001237 * Description: Check the link, then figure out the current state
Andy Fleming00db8182005-07-30 19:31:23 -04001238 * by comparing what we advertise with what the link partner
1239 * advertises. Start by checking the gigabit possibilities,
1240 * then move on to 10/100.
1241 */
1242int genphy_read_status(struct phy_device *phydev)
1243{
1244 int adv;
1245 int err;
1246 int lpa;
1247 int lpagb = 0;
Cristian Bercarua4572e02014-02-25 10:22:48 +02001248 int common_adv;
1249 int common_adv_gb = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001250
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001251 /* Update the link, but return if there was an error */
Andy Fleming00db8182005-07-30 19:31:23 -04001252 err = genphy_update_link(phydev);
1253 if (err)
1254 return err;
1255
Florian Fainelli114002b2013-12-06 13:01:30 -08001256 phydev->lp_advertising = 0;
1257
Andy Fleming00db8182005-07-30 19:31:23 -04001258 if (AUTONEG_ENABLE == phydev->autoneg) {
1259 if (phydev->supported & (SUPPORTED_1000baseT_Half
1260 | SUPPORTED_1000baseT_Full)) {
1261 lpagb = phy_read(phydev, MII_STAT1000);
Andy Fleming00db8182005-07-30 19:31:23 -04001262 if (lpagb < 0)
1263 return lpagb;
1264
1265 adv = phy_read(phydev, MII_CTRL1000);
Andy Fleming00db8182005-07-30 19:31:23 -04001266 if (adv < 0)
1267 return adv;
1268
Florian Fainelli114002b2013-12-06 13:01:30 -08001269 phydev->lp_advertising =
1270 mii_stat1000_to_ethtool_lpa_t(lpagb);
Cristian Bercarua4572e02014-02-25 10:22:48 +02001271 common_adv_gb = lpagb & adv << 2;
Andy Fleming00db8182005-07-30 19:31:23 -04001272 }
1273
1274 lpa = phy_read(phydev, MII_LPA);
Andy Fleming00db8182005-07-30 19:31:23 -04001275 if (lpa < 0)
1276 return lpa;
1277
Florian Fainelli114002b2013-12-06 13:01:30 -08001278 phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
1279
Andy Fleming00db8182005-07-30 19:31:23 -04001280 adv = phy_read(phydev, MII_ADVERTISE);
Andy Fleming00db8182005-07-30 19:31:23 -04001281 if (adv < 0)
1282 return adv;
1283
Cristian Bercarua4572e02014-02-25 10:22:48 +02001284 common_adv = lpa & adv;
Andy Fleming00db8182005-07-30 19:31:23 -04001285
1286 phydev->speed = SPEED_10;
1287 phydev->duplex = DUPLEX_HALF;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001288 phydev->pause = 0;
1289 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001290
Cristian Bercarua4572e02014-02-25 10:22:48 +02001291 if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) {
Andy Fleming00db8182005-07-30 19:31:23 -04001292 phydev->speed = SPEED_1000;
1293
Cristian Bercarua4572e02014-02-25 10:22:48 +02001294 if (common_adv_gb & LPA_1000FULL)
Andy Fleming00db8182005-07-30 19:31:23 -04001295 phydev->duplex = DUPLEX_FULL;
Cristian Bercarua4572e02014-02-25 10:22:48 +02001296 } else if (common_adv & (LPA_100FULL | LPA_100HALF)) {
Andy Fleming00db8182005-07-30 19:31:23 -04001297 phydev->speed = SPEED_100;
Florian Fainellie1093742013-12-17 21:38:12 -08001298
Cristian Bercarua4572e02014-02-25 10:22:48 +02001299 if (common_adv & LPA_100FULL)
Andy Fleming00db8182005-07-30 19:31:23 -04001300 phydev->duplex = DUPLEX_FULL;
1301 } else
Cristian Bercarua4572e02014-02-25 10:22:48 +02001302 if (common_adv & LPA_10FULL)
Andy Fleming00db8182005-07-30 19:31:23 -04001303 phydev->duplex = DUPLEX_FULL;
1304
Florian Fainellie1093742013-12-17 21:38:12 -08001305 if (phydev->duplex == DUPLEX_FULL) {
Andy Fleming00db8182005-07-30 19:31:23 -04001306 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
1307 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
1308 }
1309 } else {
1310 int bmcr = phy_read(phydev, MII_BMCR);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001311
Andy Fleming00db8182005-07-30 19:31:23 -04001312 if (bmcr < 0)
1313 return bmcr;
1314
1315 if (bmcr & BMCR_FULLDPLX)
1316 phydev->duplex = DUPLEX_FULL;
1317 else
1318 phydev->duplex = DUPLEX_HALF;
1319
1320 if (bmcr & BMCR_SPEED1000)
1321 phydev->speed = SPEED_1000;
1322 else if (bmcr & BMCR_SPEED100)
1323 phydev->speed = SPEED_100;
1324 else
1325 phydev->speed = SPEED_10;
1326
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001327 phydev->pause = 0;
1328 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001329 }
1330
1331 return 0;
1332}
1333EXPORT_SYMBOL(genphy_read_status);
1334
stephen hemminger395056e2014-01-19 11:48:20 -08001335static int gen10g_read_status(struct phy_device *phydev)
Andy Fleming124059f2014-01-10 14:27:37 +08001336{
1337 int devad, reg;
1338 u32 mmd_mask = phydev->c45_ids.devices_in_package;
1339
1340 phydev->link = 1;
1341
1342 /* For now just lie and say it's 10G all the time */
1343 phydev->speed = SPEED_10000;
1344 phydev->duplex = DUPLEX_FULL;
1345
1346 for (devad = 0; mmd_mask; devad++, mmd_mask = mmd_mask >> 1) {
1347 if (!(mmd_mask & 1))
1348 continue;
1349
1350 /* Read twice because link state is latched and a
1351 * read moves the current state into the register
1352 */
1353 phy_read_mmd(phydev, devad, MDIO_STAT1);
1354 reg = phy_read_mmd(phydev, devad, MDIO_STAT1);
1355 if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS))
1356 phydev->link = 0;
1357 }
1358
1359 return 0;
1360}
Andy Fleming124059f2014-01-10 14:27:37 +08001361
Florian Fainelli797ac072014-02-17 13:34:02 -08001362/**
1363 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
1364 * @phydev: target phy_device struct
1365 *
1366 * Description: Perform a software PHY reset using the standard
1367 * BMCR_RESET bit and poll for the reset bit to be cleared.
1368 *
1369 * Returns: 0 on success, < 0 on failure
1370 */
1371int genphy_soft_reset(struct phy_device *phydev)
1372{
1373 int ret;
1374
1375 ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
1376 if (ret < 0)
1377 return ret;
1378
1379 return phy_poll_reset(phydev);
1380}
1381EXPORT_SYMBOL(genphy_soft_reset);
1382
Daniel Mackaf6b6962014-04-16 17:19:12 +02001383int genphy_config_init(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -04001384{
Eric Sesterhenn84c22d72006-09-25 16:39:22 -07001385 int val;
Andy Fleming00db8182005-07-30 19:31:23 -04001386 u32 features;
1387
Andy Fleming00db8182005-07-30 19:31:23 -04001388 features = (SUPPORTED_TP | SUPPORTED_MII
1389 | SUPPORTED_AUI | SUPPORTED_FIBRE |
1390 SUPPORTED_BNC);
1391
1392 /* Do we support autonegotiation? */
1393 val = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -04001394 if (val < 0)
1395 return val;
1396
1397 if (val & BMSR_ANEGCAPABLE)
1398 features |= SUPPORTED_Autoneg;
1399
1400 if (val & BMSR_100FULL)
1401 features |= SUPPORTED_100baseT_Full;
1402 if (val & BMSR_100HALF)
1403 features |= SUPPORTED_100baseT_Half;
1404 if (val & BMSR_10FULL)
1405 features |= SUPPORTED_10baseT_Full;
1406 if (val & BMSR_10HALF)
1407 features |= SUPPORTED_10baseT_Half;
1408
1409 if (val & BMSR_ESTATEN) {
1410 val = phy_read(phydev, MII_ESTATUS);
Andy Fleming00db8182005-07-30 19:31:23 -04001411 if (val < 0)
1412 return val;
1413
1414 if (val & ESTATUS_1000_TFULL)
1415 features |= SUPPORTED_1000baseT_Full;
1416 if (val & ESTATUS_1000_THALF)
1417 features |= SUPPORTED_1000baseT_Half;
1418 }
1419
Sascha Hauerc242a472014-05-21 15:29:44 +02001420 phydev->supported &= features;
1421 phydev->advertising &= features;
Andy Fleming00db8182005-07-30 19:31:23 -04001422
1423 return 0;
1424}
Andy Fleming124059f2014-01-10 14:27:37 +08001425
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001426static int gen10g_soft_reset(struct phy_device *phydev)
1427{
1428 /* Do nothing for now */
1429 return 0;
1430}
Daniel Mackaf6b6962014-04-16 17:19:12 +02001431EXPORT_SYMBOL(genphy_config_init);
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001432
Andy Fleming124059f2014-01-10 14:27:37 +08001433static int gen10g_config_init(struct phy_device *phydev)
1434{
1435 /* Temporarily just say we support everything */
1436 phydev->supported = SUPPORTED_10000baseT_Full;
1437 phydev->advertising = SUPPORTED_10000baseT_Full;
1438
1439 return 0;
1440}
1441
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001442int genphy_suspend(struct phy_device *phydev)
1443{
1444 int value;
Andy Fleming00db8182005-07-30 19:31:23 -04001445
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001446 mutex_lock(&phydev->lock);
1447
1448 value = phy_read(phydev, MII_BMCR);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001449 phy_write(phydev, MII_BMCR, value | BMCR_PDOWN);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001450
1451 mutex_unlock(&phydev->lock);
1452
1453 return 0;
1454}
1455EXPORT_SYMBOL(genphy_suspend);
1456
stephen hemminger395056e2014-01-19 11:48:20 -08001457static int gen10g_suspend(struct phy_device *phydev)
Andy Fleming124059f2014-01-10 14:27:37 +08001458{
1459 return 0;
1460}
Andy Fleming124059f2014-01-10 14:27:37 +08001461
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001462int genphy_resume(struct phy_device *phydev)
1463{
1464 int value;
1465
1466 mutex_lock(&phydev->lock);
1467
1468 value = phy_read(phydev, MII_BMCR);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001469 phy_write(phydev, MII_BMCR, value & ~BMCR_PDOWN);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001470
1471 mutex_unlock(&phydev->lock);
1472
1473 return 0;
1474}
1475EXPORT_SYMBOL(genphy_resume);
Andy Fleming00db8182005-07-30 19:31:23 -04001476
stephen hemminger395056e2014-01-19 11:48:20 -08001477static int gen10g_resume(struct phy_device *phydev)
Andy Fleming124059f2014-01-10 14:27:37 +08001478{
1479 return 0;
1480}
Andy Fleming124059f2014-01-10 14:27:37 +08001481
Simon Hormanf3a6bd32015-09-30 15:15:52 +09001482static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
1483{
1484 /* The default values for phydev->supported are provided by the PHY
1485 * driver "features" member, we want to reset to sane defaults first
1486 * before supporting higher speeds.
1487 */
1488 phydev->supported &= PHY_DEFAULT_FEATURES;
1489
1490 switch (max_speed) {
1491 default:
1492 return -ENOTSUPP;
1493 case SPEED_1000:
1494 phydev->supported |= PHY_1000BT_FEATURES;
1495 /* fall through */
1496 case SPEED_100:
1497 phydev->supported |= PHY_100BT_FEATURES;
1498 /* fall through */
1499 case SPEED_10:
1500 phydev->supported |= PHY_10BT_FEATURES;
1501 }
1502
1503 return 0;
1504}
1505
1506int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
1507{
1508 int err;
1509
1510 err = __set_phy_supported(phydev, max_speed);
1511 if (err)
1512 return err;
1513
1514 phydev->advertising = phydev->supported;
1515
1516 return 0;
1517}
1518EXPORT_SYMBOL(phy_set_max_speed);
1519
Sascha Hauerde906af2014-05-21 15:29:45 +02001520static void of_set_phy_supported(struct phy_device *phydev)
1521{
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001522 struct device_node *node = phydev->mdio.dev.of_node;
Sascha Hauerde906af2014-05-21 15:29:45 +02001523 u32 max_speed;
1524
1525 if (!IS_ENABLED(CONFIG_OF_MDIO))
1526 return;
1527
1528 if (!node)
1529 return;
1530
Simon Hormanf3a6bd32015-09-30 15:15:52 +09001531 if (!of_property_read_u32(node, "max-speed", &max_speed))
1532 __set_phy_supported(phydev, max_speed);
Sascha Hauerde906af2014-05-21 15:29:45 +02001533}
1534
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001535/**
1536 * phy_probe - probe and init a PHY device
1537 * @dev: device to probe and init
Andy Fleming00db8182005-07-30 19:31:23 -04001538 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001539 * Description: Take care of setting up the phy_device structure,
Andy Fleming00db8182005-07-30 19:31:23 -04001540 * set the state to READY (the driver's init function should
1541 * set it to STARTING if needed).
1542 */
1543static int phy_probe(struct device *dev)
1544{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001545 struct phy_device *phydev = to_phy_device(dev);
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001546 struct device_driver *drv = phydev->mdio.dev.driver;
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001547 struct phy_driver *phydrv = to_phy_driver(drv);
Andy Fleming00db8182005-07-30 19:31:23 -04001548 int err = 0;
1549
Andy Fleming00db8182005-07-30 19:31:23 -04001550 phydev->drv = phydrv;
1551
Florian Fainelli2c7b4922013-05-19 22:53:42 +00001552 /* Disable the interrupt if the PHY doesn't support it
1553 * but the interrupt is still a valid one
1554 */
1555 if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001556 phy_interrupt_is_valid(phydev))
Andy Fleming00db8182005-07-30 19:31:23 -04001557 phydev->irq = PHY_POLL;
1558
Florian Fainelli4284b6a2013-05-23 01:11:12 +00001559 if (phydrv->flags & PHY_IS_INTERNAL)
1560 phydev->is_internal = true;
1561
Nate Case35b5f6b2008-01-29 10:05:09 -06001562 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001563
1564 /* Start out supporting everything. Eventually,
1565 * a controller will attach, and may modify one
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001566 * or both of these values
1567 */
Andy Fleming00db8182005-07-30 19:31:23 -04001568 phydev->supported = phydrv->features;
Sascha Hauerde906af2014-05-21 15:29:45 +02001569 of_set_phy_supported(phydev);
1570 phydev->advertising = phydev->supported;
Andy Fleming00db8182005-07-30 19:31:23 -04001571
1572 /* Set the state to READY by default */
1573 phydev->state = PHY_READY;
1574
1575 if (phydev->drv->probe)
1576 err = phydev->drv->probe(phydev);
1577
Nate Case35b5f6b2008-01-29 10:05:09 -06001578 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001579
Andy Fleming00db8182005-07-30 19:31:23 -04001580 return err;
1581}
1582
1583static int phy_remove(struct device *dev)
1584{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001585 struct phy_device *phydev = to_phy_device(dev);
Andy Fleming00db8182005-07-30 19:31:23 -04001586
Nate Case35b5f6b2008-01-29 10:05:09 -06001587 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001588 phydev->state = PHY_DOWN;
Nate Case35b5f6b2008-01-29 10:05:09 -06001589 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001590
1591 if (phydev->drv->remove)
1592 phydev->drv->remove(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04001593 phydev->drv = NULL;
1594
1595 return 0;
1596}
1597
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001598/**
1599 * phy_driver_register - register a phy_driver with the PHY layer
1600 * @new_driver: new phy_driver to register
Andrew Lunnbe01da72016-01-06 20:11:22 +01001601 * @owner: module owning this PHY
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001602 */
Andrew Lunnbe01da72016-01-06 20:11:22 +01001603int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
Andy Fleming00db8182005-07-30 19:31:23 -04001604{
1605 int retval;
1606
Andrew Lunna9049e02016-01-06 20:11:26 +01001607 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
1608 new_driver->mdiodrv.driver.name = new_driver->name;
1609 new_driver->mdiodrv.driver.bus = &mdio_bus_type;
1610 new_driver->mdiodrv.driver.probe = phy_probe;
1611 new_driver->mdiodrv.driver.remove = phy_remove;
1612 new_driver->mdiodrv.driver.owner = owner;
Andy Fleming00db8182005-07-30 19:31:23 -04001613
Andrew Lunna9049e02016-01-06 20:11:26 +01001614 retval = driver_register(&new_driver->mdiodrv.driver);
Andy Fleming00db8182005-07-30 19:31:23 -04001615 if (retval) {
Joe Perches8d242482012-06-09 07:49:07 +00001616 pr_err("%s: Error %d in registering driver\n",
1617 new_driver->name, retval);
Andy Fleming00db8182005-07-30 19:31:23 -04001618
1619 return retval;
1620 }
1621
Olof Johanssonf2511f12007-11-04 16:09:23 -06001622 pr_debug("%s: Registered new driver\n", new_driver->name);
Andy Fleming00db8182005-07-30 19:31:23 -04001623
1624 return 0;
1625}
1626EXPORT_SYMBOL(phy_driver_register);
1627
Andrew Lunnbe01da72016-01-06 20:11:22 +01001628int phy_drivers_register(struct phy_driver *new_driver, int n,
1629 struct module *owner)
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00001630{
1631 int i, ret = 0;
1632
1633 for (i = 0; i < n; i++) {
Andrew Lunnbe01da72016-01-06 20:11:22 +01001634 ret = phy_driver_register(new_driver + i, owner);
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00001635 if (ret) {
1636 while (i-- > 0)
1637 phy_driver_unregister(new_driver + i);
1638 break;
1639 }
1640 }
1641 return ret;
1642}
1643EXPORT_SYMBOL(phy_drivers_register);
1644
Andy Fleming00db8182005-07-30 19:31:23 -04001645void phy_driver_unregister(struct phy_driver *drv)
1646{
Andrew Lunna9049e02016-01-06 20:11:26 +01001647 driver_unregister(&drv->mdiodrv.driver);
Andy Fleming00db8182005-07-30 19:31:23 -04001648}
1649EXPORT_SYMBOL(phy_driver_unregister);
1650
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00001651void phy_drivers_unregister(struct phy_driver *drv, int n)
1652{
1653 int i;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001654
1655 for (i = 0; i < n; i++)
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00001656 phy_driver_unregister(drv + i);
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00001657}
1658EXPORT_SYMBOL(phy_drivers_unregister);
1659
Shaohui Xieab2145e2014-01-10 14:27:22 +08001660static struct phy_driver genphy_driver[] = {
1661{
Andy Fleminge1393452005-08-24 18:46:21 -05001662 .phy_id = 0xffffffff,
1663 .phy_id_mask = 0xffffffff,
1664 .name = "Generic PHY",
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001665 .soft_reset = genphy_soft_reset,
Andy Fleminge1393452005-08-24 18:46:21 -05001666 .config_init = genphy_config_init,
Sascha Hauerc242a472014-05-21 15:29:44 +02001667 .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
1668 SUPPORTED_AUI | SUPPORTED_FIBRE |
1669 SUPPORTED_BNC,
Andy Fleminge1393452005-08-24 18:46:21 -05001670 .config_aneg = genphy_config_aneg,
Florian Fainelli76a423a2014-02-11 17:27:37 -08001671 .aneg_done = genphy_aneg_done,
Andy Fleminge1393452005-08-24 18:46:21 -05001672 .read_status = genphy_read_status,
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001673 .suspend = genphy_suspend,
1674 .resume = genphy_resume,
Andy Fleming124059f2014-01-10 14:27:37 +08001675}, {
1676 .phy_id = 0xffffffff,
1677 .phy_id_mask = 0xffffffff,
1678 .name = "Generic 10G PHY",
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001679 .soft_reset = gen10g_soft_reset,
Andy Fleming124059f2014-01-10 14:27:37 +08001680 .config_init = gen10g_config_init,
1681 .features = 0,
1682 .config_aneg = gen10g_config_aneg,
1683 .read_status = gen10g_read_status,
1684 .suspend = gen10g_suspend,
1685 .resume = gen10g_resume,
Shaohui Xieab2145e2014-01-10 14:27:22 +08001686} };
Andy Fleming00db8182005-07-30 19:31:23 -04001687
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001688static int __init phy_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -04001689{
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001690 int rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001691
1692 rc = mdio_bus_init();
1693 if (rc)
Andy Fleminge1393452005-08-24 18:46:21 -05001694 return rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001695
Shaohui Xieab2145e2014-01-10 14:27:22 +08001696 rc = phy_drivers_register(genphy_driver,
Andrew Lunnbe01da72016-01-06 20:11:22 +01001697 ARRAY_SIZE(genphy_driver), THIS_MODULE);
Andy Fleminge1393452005-08-24 18:46:21 -05001698 if (rc)
1699 mdio_bus_exit();
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001700
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001701 return rc;
Andy Fleming00db8182005-07-30 19:31:23 -04001702}
1703
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001704static void __exit phy_exit(void)
Andy Fleming00db8182005-07-30 19:31:23 -04001705{
Shaohui Xieab2145e2014-01-10 14:27:22 +08001706 phy_drivers_unregister(genphy_driver,
1707 ARRAY_SIZE(genphy_driver));
Andy Fleminge1393452005-08-24 18:46:21 -05001708 mdio_bus_exit();
Andy Fleming00db8182005-07-30 19:31:23 -04001709}
1710
Andy Fleminge1393452005-08-24 18:46:21 -05001711subsys_initcall(phy_init);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001712module_exit(phy_exit);