blob: af64a9320fb084d28d4703d3f4389f9b808be47a [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>
Zach Brown2e0bc452016-10-17 10:49:55 -050033#include <linux/phy_led_triggers.h>
Andy Fleming124059f2014-01-10 14:27:37 +080034#include <linux/mdio.h>
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030035#include <linux/io.h>
36#include <linux/uaccess.h>
Sascha Hauerde906af2014-05-21 15:29:45 +020037#include <linux/of.h>
Andy Fleming00db8182005-07-30 19:31:23 -040038
Andy Fleming00db8182005-07-30 19:31:23 -040039#include <asm/irq.h>
Andy Fleming00db8182005-07-30 19:31:23 -040040
Olaf Heringafcceaa2005-12-14 00:33:49 +010041MODULE_DESCRIPTION("PHY library");
42MODULE_AUTHOR("Andy Fleming");
43MODULE_LICENSE("GPL");
44
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030045void phy_device_free(struct phy_device *phydev)
46{
Andrew Lunne5a03bf2016-01-06 20:11:16 +010047 put_device(&phydev->mdio.dev);
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030048}
Grant Likely4dea5472009-04-25 12:52:46 +000049EXPORT_SYMBOL(phy_device_free);
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030050
Andrew Lunn711fdba2016-01-06 20:11:27 +010051static void phy_mdio_device_free(struct mdio_device *mdiodev)
52{
53 struct phy_device *phydev;
54
55 phydev = container_of(mdiodev, struct phy_device, mdio);
56 phy_device_free(phydev);
57}
58
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030059static void phy_device_release(struct device *dev)
60{
Petr Malatb2a43192013-02-28 01:01:52 +000061 kfree(to_phy_device(dev));
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030062}
63
Andrew Lunn711fdba2016-01-06 20:11:27 +010064static void phy_mdio_device_remove(struct mdio_device *mdiodev)
65{
66 struct phy_device *phydev;
67
68 phydev = container_of(mdiodev, struct phy_device, mdio);
69 phy_device_remove(phydev);
70}
71
Russell King921690f2017-06-05 12:23:05 +010072static struct phy_driver genphy_driver;
73extern struct phy_driver genphy_10g_driver;
Grant Likely4dea5472009-04-25 12:52:46 +000074
Andy Flemingf62220d2008-04-18 17:29:54 -050075static LIST_HEAD(phy_fixup_list);
76static DEFINE_MUTEX(phy_fixup_lock);
77
Andrew Lunnbc879222016-01-06 20:11:21 +010078#ifdef CONFIG_PM
79static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
80{
81 struct device_driver *drv = phydev->mdio.dev.driver;
82 struct phy_driver *phydrv = to_phy_driver(drv);
83 struct net_device *netdev = phydev->attached_dev;
84
85 if (!drv || !phydrv->suspend)
86 return false;
87
88 /* PHY not attached? May suspend if the PHY has not already been
89 * suspended as part of a prior call to phy_disconnect() ->
90 * phy_detach() -> phy_suspend() because the parent netdev might be the
91 * MDIO bus driver and clock gated at this point.
92 */
93 if (!netdev)
94 return !phydev->suspended;
95
96 /* Don't suspend PHY if the attached netdev parent may wakeup.
97 * The parent may point to a PCI device, as in tg3 driver.
98 */
99 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
100 return false;
101
102 /* Also don't suspend PHY if the netdev itself may wakeup. This
103 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
104 * e.g. SoC devices.
105 */
106 if (device_may_wakeup(&netdev->dev))
107 return false;
108
109 return true;
110}
111
112static int mdio_bus_phy_suspend(struct device *dev)
113{
114 struct phy_device *phydev = to_phy_device(dev);
115
116 /* We must stop the state machine manually, otherwise it stops out of
117 * control, possibly with the phydev->lock held. Upon resume, netdev
118 * may call phy routines that try to grab the same lock, and that may
119 * lead to a deadlock.
120 */
121 if (phydev->attached_dev && phydev->adjust_link)
122 phy_stop_machine(phydev);
123
124 if (!mdio_bus_phy_may_suspend(phydev))
125 return 0;
126
127 return phy_suspend(phydev);
128}
129
130static int mdio_bus_phy_resume(struct device *dev)
131{
132 struct phy_device *phydev = to_phy_device(dev);
133 int ret;
134
135 if (!mdio_bus_phy_may_suspend(phydev))
136 goto no_resume;
137
138 ret = phy_resume(phydev);
139 if (ret < 0)
140 return ret;
141
142no_resume:
143 if (phydev->attached_dev && phydev->adjust_link)
144 phy_start_machine(phydev);
145
146 return 0;
147}
148
149static int mdio_bus_phy_restore(struct device *dev)
150{
151 struct phy_device *phydev = to_phy_device(dev);
152 struct net_device *netdev = phydev->attached_dev;
153 int ret;
154
155 if (!netdev)
156 return 0;
157
158 ret = phy_init_hw(phydev);
159 if (ret < 0)
160 return ret;
161
162 /* The PHY needs to renegotiate. */
163 phydev->link = 0;
164 phydev->state = PHY_UP;
165
166 phy_start_machine(phydev);
167
168 return 0;
169}
170
171static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
172 .suspend = mdio_bus_phy_suspend,
173 .resume = mdio_bus_phy_resume,
174 .freeze = mdio_bus_phy_suspend,
175 .thaw = mdio_bus_phy_resume,
176 .restore = mdio_bus_phy_restore,
177};
178
179#define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
180
181#else
182
183#define MDIO_BUS_PHY_PM_OPS NULL
184
185#endif /* CONFIG_PM */
186
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300187/**
188 * phy_register_fixup - creates a new phy_fixup and adds it to the list
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100189 * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
Andy Flemingf62220d2008-04-18 17:29:54 -0500190 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300191 * It can also be PHY_ANY_UID
Andy Flemingf62220d2008-04-18 17:29:54 -0500192 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300193 * comparison
Andy Flemingf62220d2008-04-18 17:29:54 -0500194 * @run: The actual code to be run when a matching PHY is found
195 */
196int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300197 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -0500198{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300199 struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
Andy Flemingf62220d2008-04-18 17:29:54 -0500200
Andy Flemingf62220d2008-04-18 17:29:54 -0500201 if (!fixup)
202 return -ENOMEM;
203
Kay Sieversfb28ad32008-11-10 13:55:14 -0800204 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
Andy Flemingf62220d2008-04-18 17:29:54 -0500205 fixup->phy_uid = phy_uid;
206 fixup->phy_uid_mask = phy_uid_mask;
207 fixup->run = run;
208
209 mutex_lock(&phy_fixup_lock);
210 list_add_tail(&fixup->list, &phy_fixup_list);
211 mutex_unlock(&phy_fixup_lock);
212
213 return 0;
214}
215EXPORT_SYMBOL(phy_register_fixup);
216
217/* Registers a fixup to be run on any PHY with the UID in phy_uid */
218int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300219 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -0500220{
221 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
222}
223EXPORT_SYMBOL(phy_register_fixup_for_uid);
224
225/* Registers a fixup to be run on the PHY with id string bus_id */
226int phy_register_fixup_for_id(const char *bus_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300227 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -0500228{
229 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
230}
231EXPORT_SYMBOL(phy_register_fixup_for_id);
232
Woojung.Huh@microchip.comf38e7a32016-12-07 20:26:07 +0000233/**
234 * phy_unregister_fixup - remove a phy_fixup from the list
235 * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
236 * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
237 * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
238 */
239int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
240{
241 struct list_head *pos, *n;
242 struct phy_fixup *fixup;
243 int ret;
244
245 ret = -ENODEV;
246
247 mutex_lock(&phy_fixup_lock);
248 list_for_each_safe(pos, n, &phy_fixup_list) {
249 fixup = list_entry(pos, struct phy_fixup, list);
250
251 if ((!strcmp(fixup->bus_id, bus_id)) &&
252 ((fixup->phy_uid & phy_uid_mask) ==
253 (phy_uid & phy_uid_mask))) {
254 list_del(&fixup->list);
255 kfree(fixup);
256 ret = 0;
257 break;
258 }
259 }
260 mutex_unlock(&phy_fixup_lock);
261
262 return ret;
263}
264EXPORT_SYMBOL(phy_unregister_fixup);
265
266/* Unregisters a fixup of any PHY with the UID in phy_uid */
267int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
268{
269 return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
270}
271EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
272
273/* Unregisters a fixup of the PHY with id string bus_id */
274int phy_unregister_fixup_for_id(const char *bus_id)
275{
276 return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
277}
278EXPORT_SYMBOL(phy_unregister_fixup_for_id);
279
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300280/* Returns 1 if fixup matches phydev in bus_id and phy_uid.
Andy Flemingf62220d2008-04-18 17:29:54 -0500281 * Fixups can be set to match any in one or more fields.
282 */
283static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
284{
Andrew Lunn84eff6d2016-01-06 20:11:10 +0100285 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
Andy Flemingf62220d2008-04-18 17:29:54 -0500286 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
287 return 0;
288
289 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300290 (phydev->phy_id & fixup->phy_uid_mask))
Andy Flemingf62220d2008-04-18 17:29:54 -0500291 if (fixup->phy_uid != PHY_ANY_UID)
292 return 0;
293
294 return 1;
295}
296
297/* Runs any matching fixups for this phydev */
Sergei Shtylyovfbfcec62014-01-05 03:28:27 +0300298static int phy_scan_fixups(struct phy_device *phydev)
Andy Flemingf62220d2008-04-18 17:29:54 -0500299{
300 struct phy_fixup *fixup;
301
302 mutex_lock(&phy_fixup_lock);
303 list_for_each_entry(fixup, &phy_fixup_list, list) {
304 if (phy_needs_fixup(phydev, fixup)) {
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300305 int err = fixup->run(phydev);
Andy Flemingf62220d2008-04-18 17:29:54 -0500306
Jiri Slabybc23283c2009-07-13 11:23:39 +0000307 if (err < 0) {
308 mutex_unlock(&phy_fixup_lock);
Andy Flemingf62220d2008-04-18 17:29:54 -0500309 return err;
Jiri Slabybc23283c2009-07-13 11:23:39 +0000310 }
Florian Fainellib0ae0092014-02-11 17:27:41 -0800311 phydev->has_fixups = true;
Andy Flemingf62220d2008-04-18 17:29:54 -0500312 }
313 }
314 mutex_unlock(&phy_fixup_lock);
315
316 return 0;
317}
Andy Flemingf62220d2008-04-18 17:29:54 -0500318
Andrew Lunne76a4952016-01-06 20:11:23 +0100319static int phy_bus_match(struct device *dev, struct device_driver *drv)
320{
321 struct phy_device *phydev = to_phy_device(dev);
322 struct phy_driver *phydrv = to_phy_driver(drv);
323 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
324 int i;
325
Andrew Lunna9049e02016-01-06 20:11:26 +0100326 if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
327 return 0;
328
Andrew Lunne76a4952016-01-06 20:11:23 +0100329 if (phydrv->match_phy_device)
330 return phydrv->match_phy_device(phydev);
331
332 if (phydev->is_c45) {
333 for (i = 1; i < num_ids; i++) {
334 if (!(phydev->c45_ids.devices_in_package & (1 << i)))
335 continue;
336
337 if ((phydrv->phy_id & phydrv->phy_id_mask) ==
338 (phydev->c45_ids.device_ids[i] &
339 phydrv->phy_id_mask))
340 return 1;
341 }
342 return 0;
343 } else {
344 return (phydrv->phy_id & phydrv->phy_id_mask) ==
345 (phydev->phy_id & phydrv->phy_id_mask);
346 }
347}
348
Heiner Kallweit7f4828f2018-06-02 22:36:06 +0200349static ssize_t
350phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
351{
352 struct phy_device *phydev = to_phy_device(dev);
353
354 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
355}
356static DEVICE_ATTR_RO(phy_id);
357
358static ssize_t
359phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
360{
361 struct phy_device *phydev = to_phy_device(dev);
362 const char *mode = NULL;
363
364 if (phy_is_internal(phydev))
365 mode = "internal";
366 else
367 mode = phy_modes(phydev->interface);
368
369 return sprintf(buf, "%s\n", mode);
370}
371static DEVICE_ATTR_RO(phy_interface);
372
373static ssize_t
374phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
375 char *buf)
376{
377 struct phy_device *phydev = to_phy_device(dev);
378
379 return sprintf(buf, "%d\n", phydev->has_fixups);
380}
381static DEVICE_ATTR_RO(phy_has_fixups);
382
383static struct attribute *phy_dev_attrs[] = {
384 &dev_attr_phy_id.attr,
385 &dev_attr_phy_interface.attr,
386 &dev_attr_phy_has_fixups.attr,
387 NULL,
388};
389ATTRIBUTE_GROUPS(phy_dev);
390
391static const struct device_type mdio_bus_phy_type = {
392 .name = "PHY",
393 .groups = phy_dev_groups,
394 .release = phy_device_release,
395 .pm = MDIO_BUS_PHY_PM_OPS,
396};
397
David Daneyac28b9f2012-06-27 07:33:35 +0000398struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300399 bool is_c45,
400 struct phy_c45_device_ids *c45_ids)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700401{
402 struct phy_device *dev;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100403 struct mdio_device *mdiodev;
David Woodhouse8626d3b2010-04-02 01:05:27 +0000404
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300405 /* We allocate the device, and initialize the default values */
Robert P. J. Daycd861282006-12-13 00:34:52 -0800406 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Sergei Shtylyovef899c02015-08-28 21:35:14 +0300407 if (!dev)
Sergei Shtylyovd3765f02015-08-28 21:34:34 +0300408 return ERR_PTR(-ENOMEM);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700409
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100410 mdiodev = &dev->mdio;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100411 mdiodev->dev.parent = &bus->dev;
412 mdiodev->dev.bus = &mdio_bus_type;
Heiner Kallweit7f4828f2018-06-02 22:36:06 +0200413 mdiodev->dev.type = &mdio_bus_phy_type;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100414 mdiodev->bus = bus;
Andrew Lunne76a4952016-01-06 20:11:23 +0100415 mdiodev->bus_match = phy_bus_match;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100416 mdiodev->addr = addr;
Andrew Lunn7f854422016-01-06 20:11:18 +0100417 mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
Andrew Lunn711fdba2016-01-06 20:11:27 +0100418 mdiodev->device_free = phy_mdio_device_free;
419 mdiodev->device_remove = phy_mdio_device_remove;
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300420
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700421 dev->speed = 0;
422 dev->duplex = -1;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300423 dev->pause = 0;
424 dev->asym_pause = 0;
Heiner Kallweitc69851e2018-03-11 15:00:37 +0100425 dev->link = 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600426 dev->interface = PHY_INTERFACE_MODE_GMII;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700427
428 dev->autoneg = AUTONEG_ENABLE;
429
David Daneyac28b9f2012-06-27 07:33:35 +0000430 dev->is_c45 = is_c45;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700431 dev->phy_id = phy_id;
David Daneyac28b9f2012-06-27 07:33:35 +0000432 if (c45_ids)
433 dev->c45_ids = *c45_ids;
Dan Carpenter47b356e2016-01-12 12:36:21 +0300434 dev->irq = bus->irq[addr];
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100435 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700436
437 dev->state = PHY_DOWN;
438
Nate Case35b5f6b2008-01-29 10:05:09 -0600439 mutex_init(&dev->lock);
Anton Vorontsov4f9c85a2010-01-18 05:37:16 +0000440 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
Andrew Lunn664fcf12016-10-16 19:56:51 +0200441 INIT_WORK(&dev->phy_queue, phy_change_work);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700442
David Woodhouse8626d3b2010-04-02 01:05:27 +0000443 /* Request the appropriate module unconditionally; don't
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300444 * bother trying to do so only if it isn't already loaded,
445 * because that gets complicated. A hotplug event would have
446 * done an unconditional modprobe anyway.
447 * We don't do normal hotplug because it won't work for MDIO
448 * -- because it relies on the device staying around for long
449 * enough for the driver to get loaded. With MDIO, the NIC
450 * driver will get bored and give up as soon as it finds that
451 * there's no driver _already_ loaded.
452 */
David Woodhouse8626d3b2010-04-02 01:05:27 +0000453 request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
454
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100455 device_initialize(&mdiodev->dev);
Petr Malatb2a43192013-02-28 01:01:52 +0000456
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700457 return dev;
458}
David Daneyac28b9f2012-06-27 07:33:35 +0000459EXPORT_SYMBOL(phy_device_create);
460
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800461/* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
462 * @bus: the target MII bus
463 * @addr: PHY address on the MII bus
464 * @dev_addr: MMD address in the PHY.
465 * @devices_in_package: where to store the devices in package information.
466 *
467 * Description: reads devices in package registers of a MMD at @dev_addr
468 * from PHY at @addr on @bus.
469 *
470 * Returns: 0 on success, -EIO on failure.
471 */
472static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
473 u32 *devices_in_package)
474{
475 int phy_reg, reg_addr;
476
477 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
478 phy_reg = mdiobus_read(bus, addr, reg_addr);
479 if (phy_reg < 0)
480 return -EIO;
481 *devices_in_package = (phy_reg & 0xffff) << 16;
482
483 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
484 phy_reg = mdiobus_read(bus, addr, reg_addr);
485 if (phy_reg < 0)
486 return -EIO;
487 *devices_in_package |= (phy_reg & 0xffff);
488
489 return 0;
490}
491
David Daneyac28b9f2012-06-27 07:33:35 +0000492/**
493 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
494 * @bus: the target MII bus
495 * @addr: PHY address on the MII bus
496 * @phy_id: where to store the ID retrieved.
497 * @c45_ids: where to store the c45 ID information.
498 *
499 * If the PHY devices-in-package appears to be valid, it and the
500 * corresponding identifiers are stored in @c45_ids, zero is stored
501 * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns
502 * zero on success.
503 *
504 */
505static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
506 struct phy_c45_device_ids *c45_ids) {
507 int phy_reg;
508 int i, reg_addr;
509 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800510 u32 *devs = &c45_ids->devices_in_package;
David Daneyac28b9f2012-06-27 07:33:35 +0000511
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800512 /* Find first non-zero Devices In package. Device zero is reserved
513 * for 802.3 c45 complied PHYs, so don't probe it at first.
David Daneyac28b9f2012-06-27 07:33:35 +0000514 */
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800515 for (i = 1; i < num_ids && *devs == 0; i++) {
516 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
David Daneyac28b9f2012-06-27 07:33:35 +0000517 if (phy_reg < 0)
518 return -EIO;
David Daneyac28b9f2012-06-27 07:33:35 +0000519
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800520 if ((*devs & 0x1fffffff) == 0x1fffffff) {
521 /* If mostly Fs, there is no device there,
522 * then let's continue to probe more, as some
523 * 10G PHYs have zero Devices In package,
524 * e.g. Cortina CS4315/CS4340 PHY.
525 */
526 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
527 if (phy_reg < 0)
528 return -EIO;
529 /* no device there, let's get out of here */
530 if ((*devs & 0x1fffffff) == 0x1fffffff) {
Shengzhou Liuda1da282015-06-26 17:58:52 +0800531 *phy_id = 0xffffffff;
532 return 0;
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800533 } else {
534 break;
Shengzhou Liuda1da282015-06-26 17:58:52 +0800535 }
David Daneyac28b9f2012-06-27 07:33:35 +0000536 }
537 }
538
539 /* Now probe Device Identifiers for each device present. */
540 for (i = 1; i < num_ids; i++) {
541 if (!(c45_ids->devices_in_package & (1 << i)))
542 continue;
543
544 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
545 phy_reg = mdiobus_read(bus, addr, reg_addr);
546 if (phy_reg < 0)
547 return -EIO;
548 c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16;
549
550 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
551 phy_reg = mdiobus_read(bus, addr, reg_addr);
552 if (phy_reg < 0)
553 return -EIO;
554 c45_ids->device_ids[i] |= (phy_reg & 0xffff);
555 }
556 *phy_id = 0;
557 return 0;
558}
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700559
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800560/**
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400561 * get_phy_id - reads the specified addr for its ID.
562 * @bus: the target MII bus
563 * @addr: PHY address on the MII bus
564 * @phy_id: where to store the ID retrieved.
David Daneyac28b9f2012-06-27 07:33:35 +0000565 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
566 * @c45_ids: where to store the c45 ID information.
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400567 *
David Daneyac28b9f2012-06-27 07:33:35 +0000568 * Description: In the case of a 802.3-c22 PHY, reads the ID registers
569 * of the PHY at @addr on the @bus, stores it in @phy_id and returns
570 * zero on success.
571 *
572 * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
573 * its return value is in turn returned.
574 *
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400575 */
David Daneyac28b9f2012-06-27 07:33:35 +0000576static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
577 bool is_c45, struct phy_c45_device_ids *c45_ids)
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400578{
579 int phy_reg;
580
David Daneyac28b9f2012-06-27 07:33:35 +0000581 if (is_c45)
582 return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
583
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300584 /* Grab the bits from PHYIR1, and put them in the upper half */
David Daney6fe32642011-09-30 11:51:22 +0000585 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
Alexandre Belloni02a6efc2018-04-24 18:09:04 +0200586 if (phy_reg < 0) {
587 /* if there is no device, return without an error so scanning
588 * the bus works properly
589 */
590 if (phy_reg == -EIO || phy_reg == -ENODEV) {
591 *phy_id = 0xffffffff;
592 return 0;
593 }
594
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400595 return -EIO;
Alexandre Belloni02a6efc2018-04-24 18:09:04 +0200596 }
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400597
598 *phy_id = (phy_reg & 0xffff) << 16;
599
600 /* Grab the bits from PHYIR2, and put them in the lower half */
David Daney6fe32642011-09-30 11:51:22 +0000601 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400602 if (phy_reg < 0)
603 return -EIO;
604
605 *phy_id |= (phy_reg & 0xffff);
606
607 return 0;
608}
609
610/**
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300611 * get_phy_device - reads the specified PHY device and returns its @phy_device
612 * struct
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800613 * @bus: the target MII bus
614 * @addr: PHY address on the MII bus
David Daneyac28b9f2012-06-27 07:33:35 +0000615 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
Andy Fleming00db8182005-07-30 19:31:23 -0400616 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800617 * Description: Reads the ID registers of the PHY at @addr on the
618 * @bus, then allocates and returns the phy_device to represent it.
Andy Fleming00db8182005-07-30 19:31:23 -0400619 */
David Daneyac28b9f2012-06-27 07:33:35 +0000620struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
Andy Fleming00db8182005-07-30 19:31:23 -0400621{
David Daneyac28b9f2012-06-27 07:33:35 +0000622 struct phy_c45_device_ids c45_ids = {0};
David S. Miller160c85f2012-06-27 21:28:14 -0700623 u32 phy_id = 0;
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400624 int r;
Andy Fleming00db8182005-07-30 19:31:23 -0400625
David Daneyac28b9f2012-06-27 07:33:35 +0000626 r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400627 if (r)
628 return ERR_PTR(r);
Andy Fleming00db8182005-07-30 19:31:23 -0400629
Giuseppe Cavallaro6436cbc2008-11-20 20:43:18 -0800630 /* If the phy_id is mostly Fs, there is no device there */
631 if ((phy_id & 0x1fffffff) == 0x1fffffff)
Sergei Shtylyovb74766a2016-04-24 20:25:23 +0300632 return ERR_PTR(-ENODEV);
Giuseppe Cavallaro6436cbc2008-11-20 20:43:18 -0800633
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300634 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
Andy Fleming00db8182005-07-30 19:31:23 -0400635}
Grant Likely4dea5472009-04-25 12:52:46 +0000636EXPORT_SYMBOL(get_phy_device);
637
638/**
639 * phy_device_register - Register the phy device on the MDIO bus
Randy Dunlap1d4ac5d2009-06-16 16:56:33 +0000640 * @phydev: phy_device structure to be added to the MDIO bus
Grant Likely4dea5472009-04-25 12:52:46 +0000641 */
642int phy_device_register(struct phy_device *phydev)
643{
644 int err;
645
Andrew Lunn7f854422016-01-06 20:11:18 +0100646 err = mdiobus_register_device(&phydev->mdio);
647 if (err)
648 return err;
Grant Likely4dea5472009-04-25 12:52:46 +0000649
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +0100650 /* Deassert the reset signal */
651 phy_device_reset(phydev, 0);
652
Grant Likely4dea5472009-04-25 12:52:46 +0000653 /* Run all of the fixups for this PHY */
Florian Fainellid92f5de2014-07-28 16:28:07 -0700654 err = phy_scan_fixups(phydev);
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800655 if (err) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100656 pr_err("PHY %d failed to initialize\n", phydev->mdio.addr);
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800657 goto out;
658 }
Grant Likely4dea5472009-04-25 12:52:46 +0000659
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100660 err = device_add(&phydev->mdio.dev);
Grant Likely4dea5472009-04-25 12:52:46 +0000661 if (err) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100662 pr_err("PHY %d failed to add\n", phydev->mdio.addr);
Grant Likely4dea5472009-04-25 12:52:46 +0000663 goto out;
664 }
665
666 return 0;
667
668 out:
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +0100669 /* Assert the reset signal */
670 phy_device_reset(phydev, 1);
671
Andrew Lunn7f854422016-01-06 20:11:18 +0100672 mdiobus_unregister_device(&phydev->mdio);
Grant Likely4dea5472009-04-25 12:52:46 +0000673 return err;
674}
675EXPORT_SYMBOL(phy_device_register);
Andy Fleming00db8182005-07-30 19:31:23 -0400676
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800677/**
Russell King38737e42015-09-24 20:36:28 +0100678 * phy_device_remove - Remove a previously registered phy device from the MDIO bus
679 * @phydev: phy_device structure to remove
680 *
681 * This doesn't free the phy_device itself, it merely reverses the effects
682 * of phy_device_register(). Use phy_device_free() to free the device
683 * after calling this function.
684 */
685void phy_device_remove(struct phy_device *phydev)
686{
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100687 device_del(&phydev->mdio.dev);
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +0100688
689 /* Assert the reset signal */
690 phy_device_reset(phydev, 1);
691
Andrew Lunn7f854422016-01-06 20:11:18 +0100692 mdiobus_unregister_device(&phydev->mdio);
Russell King38737e42015-09-24 20:36:28 +0100693}
694EXPORT_SYMBOL(phy_device_remove);
695
696/**
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800697 * phy_find_first - finds the first PHY device on the bus
698 * @bus: the target MII bus
699 */
700struct phy_device *phy_find_first(struct mii_bus *bus)
701{
Andrew Lunn7f854422016-01-06 20:11:18 +0100702 struct phy_device *phydev;
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800703 int addr;
704
705 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
Andrew Lunn7f854422016-01-06 20:11:18 +0100706 phydev = mdiobus_get_phy(bus, addr);
707 if (phydev)
708 return phydev;
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800709 }
710 return NULL;
711}
712EXPORT_SYMBOL(phy_find_first);
713
Russell Kinga81497b2017-07-25 15:02:58 +0100714static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier)
715{
716 struct net_device *netdev = phydev->attached_dev;
717
718 if (do_carrier) {
719 if (up)
720 netif_carrier_on(netdev);
721 else
722 netif_carrier_off(netdev);
723 }
724 phydev->adjust_link(netdev);
725}
726
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800727/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800728 * phy_prepare_link - prepares the PHY layer to monitor link status
729 * @phydev: target phy_device struct
730 * @handler: callback function for link status change notifications
Andy Fleming00db8182005-07-30 19:31:23 -0400731 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800732 * Description: Tells the PHY infrastructure to handle the
Andy Fleming00db8182005-07-30 19:31:23 -0400733 * gory details on monitoring link status (whether through
734 * polling or an interrupt), and to call back to the
735 * connected device driver when the link status changes.
736 * If you want to monitor your own link state, don't call
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800737 * this function.
738 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000739static void phy_prepare_link(struct phy_device *phydev,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300740 void (*handler)(struct net_device *))
Andy Fleming00db8182005-07-30 19:31:23 -0400741{
742 phydev->adjust_link = handler;
743}
744
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800745/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000746 * phy_connect_direct - connect an ethernet device to a specific phy_device
747 * @dev: the network device to connect
748 * @phydev: the pointer to the phy device
749 * @handler: callback function for state change notifications
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000750 * @interface: PHY device's interface
751 */
752int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
Florian Fainellif9a8f832013-01-14 00:52:52 +0000753 void (*handler)(struct net_device *),
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000754 phy_interface_t interface)
755{
756 int rc;
757
Florian Fainellif9a8f832013-01-14 00:52:52 +0000758 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000759 if (rc)
760 return rc;
761
762 phy_prepare_link(phydev, handler);
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300763 phy_start_machine(phydev);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000764 if (phydev->irq > 0)
765 phy_start_interrupts(phydev);
766
767 return 0;
768}
769EXPORT_SYMBOL(phy_connect_direct);
770
771/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800772 * phy_connect - connect an ethernet device to a PHY device
773 * @dev: the network device to connect
Randy Dunlap5d12b132008-04-28 10:58:22 -0700774 * @bus_id: the id string of the PHY device to connect
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800775 * @handler: callback function for state change notifications
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800776 * @interface: PHY device's interface
Andy Fleminge1393452005-08-24 18:46:21 -0500777 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800778 * Description: Convenience function for connecting ethernet
Andy Fleminge1393452005-08-24 18:46:21 -0500779 * devices to PHY devices. The default behavior is for
780 * the PHY infrastructure to handle everything, and only notify
781 * the connected driver when the link status changes. If you
782 * don't want, or can't use the provided functionality, you may
783 * choose to call only the subset of functions which provide
784 * the desired functionality.
785 */
Florian Fainellie1093742013-12-17 21:38:12 -0800786struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300787 void (*handler)(struct net_device *),
788 phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500789{
790 struct phy_device *phydev;
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000791 struct device *d;
792 int rc;
Andy Fleminge1393452005-08-24 18:46:21 -0500793
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000794 /* Search the list of PHY devices on the mdio bus for the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300795 * PHY with the requested name
796 */
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000797 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
798 if (!d) {
799 pr_err("PHY %s not found\n", bus_id);
800 return ERR_PTR(-ENODEV);
801 }
802 phydev = to_phy_device(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500803
Florian Fainellif9a8f832013-01-14 00:52:52 +0000804 rc = phy_connect_direct(dev, phydev, handler, interface);
Johan Hovold17ae1c62016-11-03 18:40:19 +0100805 put_device(d);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000806 if (rc)
807 return ERR_PTR(rc);
Andy Fleminge1393452005-08-24 18:46:21 -0500808
809 return phydev;
810}
811EXPORT_SYMBOL(phy_connect);
812
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800813/**
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300814 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
815 * device
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800816 * @phydev: target phy_device struct
817 */
Andy Fleminge1393452005-08-24 18:46:21 -0500818void phy_disconnect(struct phy_device *phydev)
819{
820 if (phydev->irq > 0)
821 phy_stop_interrupts(phydev);
822
823 phy_stop_machine(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800824
Andy Fleminge1393452005-08-24 18:46:21 -0500825 phydev->adjust_link = NULL;
826
827 phy_detach(phydev);
828}
829EXPORT_SYMBOL(phy_disconnect);
830
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800831/**
832 * phy_poll_reset - Safely wait until a PHY reset has properly completed
833 * @phydev: The PHY device to poll
834 *
835 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
836 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
837 * register must be polled until the BMCR_RESET bit clears.
838 *
839 * Furthermore, any attempts to write to PHY registers may have no effect
840 * or even generate MDIO bus errors until this is complete.
841 *
842 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
843 * standard and do not fully reset after the BMCR_RESET bit is set, and may
844 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
845 * effort to support such broken PHYs, this function is separate from the
846 * standard phy_init_hw() which will zero all the other bits in the BMCR
847 * and reapply all driver-specific and board-specific fixups.
848 */
849static int phy_poll_reset(struct phy_device *phydev)
850{
851 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
852 unsigned int retries = 12;
853 int ret;
854
855 do {
856 msleep(50);
857 ret = phy_read(phydev, MII_BMCR);
858 if (ret < 0)
859 return ret;
860 } while (ret & BMCR_RESET && --retries);
861 if (ret & BMCR_RESET)
862 return -ETIMEDOUT;
863
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300864 /* Some chips (smsc911x) may still need up to another 1ms after the
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800865 * BMCR_RESET bit is cleared before they are usable.
866 */
867 msleep(1);
868 return 0;
869}
870
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000871int phy_init_hw(struct phy_device *phydev)
872{
Florian Fainelli9df81dd2014-02-17 13:34:03 -0800873 int ret = 0;
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000874
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +0100875 /* Deassert the reset signal */
876 phy_device_reset(phydev, 0);
877
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000878 if (!phydev->drv || !phydev->drv->config_init)
879 return 0;
880
Florian Fainelli9df81dd2014-02-17 13:34:03 -0800881 if (phydev->drv->soft_reset)
882 ret = phydev->drv->soft_reset(phydev);
883 else
884 ret = genphy_soft_reset(phydev);
885
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800886 if (ret < 0)
887 return ret;
888
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000889 ret = phy_scan_fixups(phydev);
890 if (ret < 0)
891 return ret;
892
893 return phydev->drv->config_init(phydev);
894}
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800895EXPORT_SYMBOL(phy_init_hw);
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000896
Andrew Lunn22209432016-01-06 20:11:13 +0100897void phy_attached_info(struct phy_device *phydev)
898{
899 phy_attached_print(phydev, NULL);
900}
901EXPORT_SYMBOL(phy_attached_info);
902
Romain Perier5e369ae2017-09-04 10:41:36 +0200903#define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
Andrew Lunn22209432016-01-06 20:11:13 +0100904void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
905{
Florian Fainellifcd03e32017-08-22 14:26:47 -0700906 const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
Romain Perier5e369ae2017-09-04 10:41:36 +0200907 char *irq_str;
Geert Uytterhoeven059fbe82017-09-21 13:27:02 +0200908 char irq_num[8];
Romain Perier5e369ae2017-09-04 10:41:36 +0200909
910 switch(phydev->irq) {
911 case PHY_POLL:
912 irq_str = "POLL";
913 break;
914 case PHY_IGNORE_INTERRUPT:
915 irq_str = "IGNORE";
916 break;
917 default:
918 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
919 irq_str = irq_num;
920 break;
921 }
922
Florian Fainellifcd03e32017-08-22 14:26:47 -0700923
Andrew Lunn22209432016-01-06 20:11:13 +0100924 if (!fmt) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100925 dev_info(&phydev->mdio.dev, ATTACHED_FMT "\n",
Florian Fainellifcd03e32017-08-22 14:26:47 -0700926 drv_name, phydev_name(phydev),
Romain Perier5e369ae2017-09-04 10:41:36 +0200927 irq_str);
Andrew Lunn22209432016-01-06 20:11:13 +0100928 } else {
929 va_list ap;
930
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100931 dev_info(&phydev->mdio.dev, ATTACHED_FMT,
Florian Fainellifcd03e32017-08-22 14:26:47 -0700932 drv_name, phydev_name(phydev),
Romain Perier5e369ae2017-09-04 10:41:36 +0200933 irq_str);
Andrew Lunn22209432016-01-06 20:11:13 +0100934
935 va_start(ap, fmt);
936 vprintk(fmt, ap);
937 va_end(ap);
938 }
939}
940EXPORT_SYMBOL(phy_attached_print);
941
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800942/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000943 * phy_attach_direct - attach a network device to a given PHY device pointer
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800944 * @dev: network device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000945 * @phydev: Pointer to phy_device to attach
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800946 * @flags: PHY device's dev_flags
947 * @interface: PHY device's interface
948 *
949 * Description: Called by drivers to attach to a particular PHY
950 * device. The phy_device is found, and properly hooked up
Andy Fleming257184d2014-01-10 14:27:54 +0800951 * to the phy_driver. If no driver is attached, then a
952 * generic driver is used. The phy_device is given a ptr to
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800953 * the attaching device, and given a callback for link status
954 * change. The phy_device is returned to the attaching driver.
Russell King73229672015-09-24 20:36:08 +0100955 * This function takes a reference on the phy device.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800956 */
Andy Fleming257184d2014-01-10 14:27:54 +0800957int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
958 u32 flags, phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500959{
Florian Fainelliec988ad2016-12-06 20:54:43 -0800960 struct module *ndev_owner = dev->dev.parent->driver->owner;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100961 struct mii_bus *bus = phydev->mdio.bus;
962 struct device *d = &phydev->mdio.dev;
Florian Fainelli6d9f66a2017-02-08 19:05:26 -0800963 bool using_genphy = false;
Marc Kleine-Budded005a092011-03-28 14:54:08 +0000964 int err;
Andy Fleminge1393452005-08-24 18:46:21 -0500965
Florian Fainelliec988ad2016-12-06 20:54:43 -0800966 /* For Ethernet device drivers that register their own MDIO bus, we
967 * will have bus->owner match ndev_mod, so we do not want to increment
968 * our own module->refcnt here, otherwise we would not be able to
969 * unload later on.
970 */
971 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
Russell King3e3aaf62015-09-24 20:36:02 +0100972 dev_err(&dev->dev, "failed to get the bus module\n");
973 return -EIO;
974 }
975
Russell King73229672015-09-24 20:36:08 +0100976 get_device(d);
977
Andy Fleminge1393452005-08-24 18:46:21 -0500978 /* Assume that if there is no driver, that it doesn't
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300979 * exist, and we should use the genphy driver.
980 */
Sergei Shtylyovef899c02015-08-28 21:35:14 +0300981 if (!d->driver) {
Andy Fleming257184d2014-01-10 14:27:54 +0800982 if (phydev->is_c45)
Russell King921690f2017-06-05 12:23:05 +0100983 d->driver = &genphy_10g_driver.mdiodrv.driver;
Andy Fleming257184d2014-01-10 14:27:54 +0800984 else
Russell King921690f2017-06-05 12:23:05 +0100985 d->driver = &genphy_driver.mdiodrv.driver;
Andy Fleminge1393452005-08-24 18:46:21 -0500986
Florian Fainelli6d9f66a2017-02-08 19:05:26 -0800987 using_genphy = true;
988 }
989
990 if (!try_module_get(d->driver->owner)) {
991 dev_err(&dev->dev, "failed to get the device driver module\n");
992 err = -EIO;
993 goto error_put_device;
994 }
995
996 if (using_genphy) {
Andy Fleminge1393452005-08-24 18:46:21 -0500997 err = d->driver->probe(d);
Jeff Garzikb7a00ec2006-10-01 07:27:46 -0400998 if (err >= 0)
999 err = device_bind_driver(d);
Andy Fleminge1393452005-08-24 18:46:21 -05001000
Jeff Garzikb7a00ec2006-10-01 07:27:46 -04001001 if (err)
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001002 goto error_module_put;
Andy Fleminge1393452005-08-24 18:46:21 -05001003 }
1004
1005 if (phydev->attached_dev) {
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001006 dev_err(&dev->dev, "PHY already attached\n");
Russell King3e3aaf62015-09-24 20:36:02 +01001007 err = -EBUSY;
1008 goto error;
Ezequiel Garciab3565f22014-07-23 16:47:32 -03001009 }
1010
Russell Kinga81497b2017-07-25 15:02:58 +01001011 phydev->phy_link_change = phy_link_change;
Andy Fleminge1393452005-08-24 18:46:21 -05001012 phydev->attached_dev = dev;
Richard Cochranc1f19b52010-07-17 08:49:36 +00001013 dev->phydev = phydev;
Florian Fainellia3995462017-05-27 10:42:25 -07001014
1015 /* Some Ethernet drivers try to connect to a PHY device before
1016 * calling register_netdevice() -> netdev_register_kobject() and
1017 * does the dev->dev.kobj initialization. Here we only check for
1018 * success which indicates that the network device kobject is
1019 * ready. Once we do that we still need to keep track of whether
1020 * links were successfully set up or not for phy_detach() to
1021 * remove them accordingly.
1022 */
1023 phydev->sysfs_links = false;
1024
Florian Fainelli55683632017-05-25 09:21:41 -07001025 err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1026 "attached_dev");
Florian Fainellia3995462017-05-27 10:42:25 -07001027 if (!err) {
Grygorii Strashko4414b3e2018-03-16 17:08:35 -05001028 err = sysfs_create_link_nowarn(&dev->dev.kobj,
1029 &phydev->mdio.dev.kobj,
1030 "phydev");
1031 if (err) {
1032 dev_err(&dev->dev, "could not add device link to %s err %d\n",
1033 kobject_name(&phydev->mdio.dev.kobj),
1034 err);
1035 /* non-fatal - some net drivers can use one netdevice
1036 * with more then one phy
1037 */
1038 }
Florian Fainelli55683632017-05-25 09:21:41 -07001039
Florian Fainellia3995462017-05-27 10:42:25 -07001040 phydev->sysfs_links = true;
1041 }
Andy Fleminge1393452005-08-24 18:46:21 -05001042
1043 phydev->dev_flags = flags;
1044
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001045 phydev->interface = interface;
1046
Anton Vorontsovef24b162010-08-24 14:46:12 -07001047 phydev->state = PHY_READY;
1048
Sjoerd Simons113c74d2016-01-14 21:57:18 +01001049 /* Initial carrier state is off as the phy is about to be
1050 * (re)initialized.
1051 */
1052 netif_carrier_off(phydev->attached_dev);
1053
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001054 /* Do initial configuration here, now that
1055 * we have certain key parameters
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001056 * (dev_flags and interface)
1057 */
Marc Kleine-Budded005a092011-03-28 14:54:08 +00001058 err = phy_init_hw(phydev);
1059 if (err)
Woojung Huha7dac9f2016-11-23 23:10:33 +00001060 goto error;
Sebastian Hesselbarth1211ce52013-12-13 10:20:28 +01001061
Woojung Huha7dac9f2016-11-23 23:10:33 +00001062 phy_resume(phydev);
Zach Brown2e0bc452016-10-17 10:49:55 -05001063 phy_led_triggers_register(phydev);
1064
Marc Kleine-Budded005a092011-03-28 14:54:08 +00001065 return err;
Russell King3e3aaf62015-09-24 20:36:02 +01001066
1067error:
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001068 /* phy_detach() does all of the cleanup below */
Woojung Huha7dac9f2016-11-23 23:10:33 +00001069 phy_detach(phydev);
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001070 return err;
1071
1072error_module_put:
Mao Wenancafe8df2017-01-31 18:46:43 -08001073 module_put(d->driver->owner);
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001074error_put_device:
1075 put_device(d);
Florian Fainelliec988ad2016-12-06 20:54:43 -08001076 if (ndev_owner != bus->owner)
1077 module_put(bus->owner);
Russell King3e3aaf62015-09-24 20:36:02 +01001078 return err;
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001079}
Andy Fleming257184d2014-01-10 14:27:54 +08001080EXPORT_SYMBOL(phy_attach_direct);
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001081
1082/**
1083 * phy_attach - attach a network device to a particular PHY device
1084 * @dev: network device to attach
1085 * @bus_id: Bus ID of PHY device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001086 * @interface: PHY device's interface
1087 *
1088 * Description: Same as phy_attach_direct() except that a PHY bus_id
1089 * string is passed instead of a pointer to a struct phy_device.
1090 */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001091struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1092 phy_interface_t interface)
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001093{
1094 struct bus_type *bus = &mdio_bus_type;
1095 struct phy_device *phydev;
1096 struct device *d;
1097 int rc;
1098
1099 /* Search the list of PHY devices on the mdio bus for the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001100 * PHY with the requested name
1101 */
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001102 d = bus_find_device_by_name(bus, NULL, bus_id);
1103 if (!d) {
1104 pr_err("PHY %s not found\n", bus_id);
1105 return ERR_PTR(-ENODEV);
1106 }
1107 phydev = to_phy_device(d);
1108
Florian Fainellif9a8f832013-01-14 00:52:52 +00001109 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
Johan Hovold17ae1c62016-11-03 18:40:19 +01001110 put_device(d);
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001111 if (rc)
1112 return ERR_PTR(rc);
1113
Andy Fleminge1393452005-08-24 18:46:21 -05001114 return phydev;
1115}
1116EXPORT_SYMBOL(phy_attach);
1117
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001118/**
1119 * phy_detach - detach a PHY device from its network device
1120 * @phydev: target phy_device struct
Russell King73229672015-09-24 20:36:08 +01001121 *
1122 * This detaches the phy device from its network device and the phy
1123 * driver, and drops the reference count taken in phy_attach_direct().
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001124 */
Andy Fleminge1393452005-08-24 18:46:21 -05001125void phy_detach(struct phy_device *phydev)
1126{
Florian Fainelliec988ad2016-12-06 20:54:43 -08001127 struct net_device *dev = phydev->attached_dev;
1128 struct module *ndev_owner = dev->dev.parent->driver->owner;
Russell King3e3aaf62015-09-24 20:36:02 +01001129 struct mii_bus *bus;
Ezequiel Garciab3565f22014-07-23 16:47:32 -03001130
Florian Fainellia3995462017-05-27 10:42:25 -07001131 if (phydev->sysfs_links) {
1132 sysfs_remove_link(&dev->dev.kobj, "phydev");
1133 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1134 }
Richard Cochranc1f19b52010-07-17 08:49:36 +00001135 phydev->attached_dev->phydev = NULL;
Andy Fleminge1393452005-08-24 18:46:21 -05001136 phydev->attached_dev = NULL;
Sebastian Hesselbarth1211ce52013-12-13 10:20:28 +01001137 phy_suspend(phydev);
Russell King9525ae82017-07-25 15:03:13 +01001138 phydev->phylink = NULL;
Andy Fleminge1393452005-08-24 18:46:21 -05001139
Geert Uytterhoeven0075bd62016-11-28 15:18:31 +01001140 phy_led_triggers_unregister(phydev);
1141
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001142 module_put(phydev->mdio.dev.driver->owner);
1143
Andy Fleminge1393452005-08-24 18:46:21 -05001144 /* If the device had no specific driver before (i.e. - it
1145 * was using the generic driver), we unbind the device
1146 * from the generic driver so that there's a chance a
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001147 * real driver could be loaded
1148 */
Russell King921690f2017-06-05 12:23:05 +01001149 if (phydev->mdio.dev.driver == &genphy_10g_driver.mdiodrv.driver ||
1150 phydev->mdio.dev.driver == &genphy_driver.mdiodrv.driver)
1151 device_release_driver(&phydev->mdio.dev);
Russell King3e3aaf62015-09-24 20:36:02 +01001152
Russell King73229672015-09-24 20:36:08 +01001153 /*
1154 * The phydev might go away on the put_device() below, so avoid
1155 * a use-after-free bug by reading the underlying bus first.
1156 */
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001157 bus = phydev->mdio.bus;
Russell King3e3aaf62015-09-24 20:36:02 +01001158
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001159 put_device(&phydev->mdio.dev);
Florian Fainelliec988ad2016-12-06 20:54:43 -08001160 if (ndev_owner != bus->owner)
1161 module_put(bus->owner);
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01001162
1163 /* Assert the reset signal */
1164 phy_device_reset(phydev, 1);
Andy Fleminge1393452005-08-24 18:46:21 -05001165}
1166EXPORT_SYMBOL(phy_detach);
1167
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001168int phy_suspend(struct phy_device *phydev)
1169{
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001170 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
Sebastian Hesselbarth32fc3fd2014-03-14 10:07:44 +01001171 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Florian Fainelli8a477a62015-01-26 22:05:39 -08001172 int ret = 0;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001173
1174 /* If the device has WOL enabled, we cannot suspend the PHY */
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001175 phy_ethtool_get_wol(phydev, &wol);
1176 if (wol.wolopts)
1177 return -EBUSY;
1178
Florian Fainelli25149ef2017-02-17 16:07:34 -08001179 if (phydev->drv && phydrv->suspend)
Florian Fainelli8a477a62015-01-26 22:05:39 -08001180 ret = phydrv->suspend(phydev);
1181
1182 if (ret)
1183 return ret;
1184
1185 phydev->suspended = true;
1186
1187 return ret;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001188}
Florian Fainellica127692014-07-08 10:38:36 -07001189EXPORT_SYMBOL(phy_suspend);
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001190
Andrew Lunn9c2c2e62018-02-27 01:56:06 +01001191int __phy_resume(struct phy_device *phydev)
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001192{
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001193 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
Florian Fainelli8a477a62015-01-26 22:05:39 -08001194 int ret = 0;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001195
Russell Kingf5e64032017-12-12 10:45:36 +00001196 WARN_ON(!mutex_is_locked(&phydev->lock));
1197
Florian Fainelli25149ef2017-02-17 16:07:34 -08001198 if (phydev->drv && phydrv->resume)
Florian Fainelli8a477a62015-01-26 22:05:39 -08001199 ret = phydrv->resume(phydev);
1200
1201 if (ret)
1202 return ret;
1203
1204 phydev->suspended = false;
1205
1206 return ret;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001207}
Andrew Lunn9c2c2e62018-02-27 01:56:06 +01001208EXPORT_SYMBOL(__phy_resume);
1209
1210int phy_resume(struct phy_device *phydev)
1211{
1212 int ret;
1213
1214 mutex_lock(&phydev->lock);
1215 ret = __phy_resume(phydev);
1216 mutex_unlock(&phydev->lock);
1217
1218 return ret;
1219}
Florian Fainellica127692014-07-08 10:38:36 -07001220EXPORT_SYMBOL(phy_resume);
Andy Fleminge1393452005-08-24 18:46:21 -05001221
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08001222int phy_loopback(struct phy_device *phydev, bool enable)
1223{
1224 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1225 int ret = 0;
1226
1227 mutex_lock(&phydev->lock);
1228
1229 if (enable && phydev->loopback_enabled) {
1230 ret = -EBUSY;
1231 goto out;
1232 }
1233
1234 if (!enable && !phydev->loopback_enabled) {
1235 ret = -EINVAL;
1236 goto out;
1237 }
1238
1239 if (phydev->drv && phydrv->set_loopback)
1240 ret = phydrv->set_loopback(phydev, enable);
1241 else
1242 ret = -EOPNOTSUPP;
1243
1244 if (ret)
1245 goto out;
1246
1247 phydev->loopback_enabled = enable;
1248
1249out:
1250 mutex_unlock(&phydev->lock);
1251 return ret;
1252}
1253EXPORT_SYMBOL(phy_loopback);
1254
Richard Leitnera9668492017-12-11 13:16:58 +01001255/**
1256 * phy_reset_after_clk_enable - perform a PHY reset if needed
1257 * @phydev: target phy_device struct
1258 *
1259 * Description: Some PHYs are known to need a reset after their refclk was
1260 * enabled. This function evaluates the flags and perform the reset if it's
1261 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
1262 * was reset.
1263 */
1264int phy_reset_after_clk_enable(struct phy_device *phydev)
1265{
1266 if (!phydev || !phydev->drv)
1267 return -ENODEV;
1268
1269 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1270 phy_device_reset(phydev, 1);
1271 phy_device_reset(phydev, 0);
1272 return 1;
1273 }
1274
1275 return 0;
1276}
1277EXPORT_SYMBOL(phy_reset_after_clk_enable);
1278
Andy Fleming00db8182005-07-30 19:31:23 -04001279/* Generic PHY support and helper functions */
1280
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001281/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001282 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001283 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001284 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001285 * Description: Writes MII_ADVERTISE with the appropriate values,
Andy Fleming00db8182005-07-30 19:31:23 -04001286 * after sanitizing the values to make sure we only advertise
Trent Piepho51e2a382008-09-24 10:55:46 +00001287 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1288 * hasn't changed, and > 0 if it has changed.
Andy Fleming00db8182005-07-30 19:31:23 -04001289 */
stephen hemminger89ff05e2010-10-21 08:37:41 +00001290static int genphy_config_advert(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -04001291{
1292 u32 advertise;
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001293 int oldadv, adv, bmsr;
Trent Piepho51e2a382008-09-24 10:55:46 +00001294 int err, changed = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001295
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001296 /* Only allow advertising what this PHY supports */
Andy Fleming00db8182005-07-30 19:31:23 -04001297 phydev->advertising &= phydev->supported;
1298 advertise = phydev->advertising;
1299
1300 /* Setup standard advertisement */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001301 adv = phy_read(phydev, MII_ADVERTISE);
Andy Fleming00db8182005-07-30 19:31:23 -04001302 if (adv < 0)
1303 return adv;
1304
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001305 oldadv = adv;
Matt Carlson28011cf2011-11-16 18:36:59 -05001306 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
Andy Fleming00db8182005-07-30 19:31:23 -04001307 ADVERTISE_PAUSE_ASYM);
Matt Carlson37f07022011-11-17 14:30:55 +00001308 adv |= ethtool_adv_to_mii_adv_t(advertise);
Andy Fleming00db8182005-07-30 19:31:23 -04001309
Trent Piepho51e2a382008-09-24 10:55:46 +00001310 if (adv != oldadv) {
1311 err = phy_write(phydev, MII_ADVERTISE, adv);
Andy Fleming00db8182005-07-30 19:31:23 -04001312
Trent Piepho51e2a382008-09-24 10:55:46 +00001313 if (err < 0)
1314 return err;
1315 changed = 1;
1316 }
Andy Fleming00db8182005-07-30 19:31:23 -04001317
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001318 bmsr = phy_read(phydev, MII_BMSR);
1319 if (bmsr < 0)
1320 return bmsr;
1321
1322 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1323 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1324 * logical 1.
1325 */
1326 if (!(bmsr & BMSR_ESTATEN))
1327 return changed;
1328
Andy Fleming00db8182005-07-30 19:31:23 -04001329 /* Configure gigabit if it's supported */
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001330 adv = phy_read(phydev, MII_CTRL1000);
1331 if (adv < 0)
1332 return adv;
1333
1334 oldadv = adv;
1335 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1336
Andy Fleming00db8182005-07-30 19:31:23 -04001337 if (phydev->supported & (SUPPORTED_1000baseT_Half |
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001338 SUPPORTED_1000baseT_Full)) {
Matt Carlson37f07022011-11-17 14:30:55 +00001339 adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
Andy Fleming00db8182005-07-30 19:31:23 -04001340 }
1341
Mugunthan V Neb686232015-06-25 22:21:02 +05301342 if (adv != oldadv)
1343 changed = 1;
1344
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001345 err = phy_write(phydev, MII_CTRL1000, adv);
1346 if (err < 0)
1347 return err;
1348
Trent Piepho51e2a382008-09-24 10:55:46 +00001349 return changed;
Andy Fleming00db8182005-07-30 19:31:23 -04001350}
Andy Fleming00db8182005-07-30 19:31:23 -04001351
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001352/**
jbrunetd853d142016-11-28 10:46:46 +01001353 * genphy_config_eee_advert - disable unwanted eee mode advertisement
1354 * @phydev: target phy_device struct
1355 *
1356 * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
1357 * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
1358 * changed, and 1 if it has changed.
1359 */
1360static int genphy_config_eee_advert(struct phy_device *phydev)
1361{
jbrunet3bb9ab62016-12-19 16:05:36 +01001362 int broken = phydev->eee_broken_modes;
1363 int old_adv, adv;
jbrunetd853d142016-11-28 10:46:46 +01001364
1365 /* Nothing to disable */
1366 if (!broken)
1367 return 0;
1368
1369 /* If the following call fails, we assume that EEE is not
1370 * supported by the phy. If we read 0, EEE is not advertised
1371 * In both case, we don't need to continue
1372 */
Russell Kinga6d99fc2017-03-21 16:36:53 +00001373 adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
jbrunetd853d142016-11-28 10:46:46 +01001374 if (adv <= 0)
1375 return 0;
1376
1377 old_adv = adv;
1378 adv &= ~broken;
1379
1380 /* Advertising remains unchanged with the broken mask */
1381 if (old_adv == adv)
1382 return 0;
1383
Russell Kinga6d99fc2017-03-21 16:36:53 +00001384 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
jbrunetd853d142016-11-28 10:46:46 +01001385
1386 return 1;
1387}
1388
1389/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001390 * genphy_setup_forced - configures/forces speed/duplex from @phydev
1391 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001392 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001393 * Description: Configures MII_BMCR to force speed/duplex
Andy Fleming00db8182005-07-30 19:31:23 -04001394 * to the values in phydev. Assumes that the values are valid.
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001395 * Please see phy_sanitize_settings().
1396 */
Madalin Bucur3fb69bc2013-11-20 16:38:19 -06001397int genphy_setup_forced(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -04001398{
Russell Kingfea23fb2018-01-02 10:58:58 +00001399 u16 ctl = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001400
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001401 phydev->pause = 0;
1402 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001403
1404 if (SPEED_1000 == phydev->speed)
1405 ctl |= BMCR_SPEED1000;
1406 else if (SPEED_100 == phydev->speed)
1407 ctl |= BMCR_SPEED100;
1408
1409 if (DUPLEX_FULL == phydev->duplex)
1410 ctl |= BMCR_FULLDPLX;
Florian Fainellie1093742013-12-17 21:38:12 -08001411
Russell Kingfea23fb2018-01-02 10:58:58 +00001412 return phy_modify(phydev, MII_BMCR,
Ingo van Lil18a5b052018-02-12 12:02:52 +01001413 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
Andy Fleming00db8182005-07-30 19:31:23 -04001414}
Madalin Bucur3fb69bc2013-11-20 16:38:19 -06001415EXPORT_SYMBOL(genphy_setup_forced);
Andy Fleming00db8182005-07-30 19:31:23 -04001416
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001417/**
1418 * genphy_restart_aneg - Enable and Restart Autonegotiation
1419 * @phydev: target phy_device struct
1420 */
Andy Fleming00db8182005-07-30 19:31:23 -04001421int genphy_restart_aneg(struct phy_device *phydev)
1422{
Andy Fleming00db8182005-07-30 19:31:23 -04001423 /* Don't isolate the PHY if we're negotiating */
Russell Kingf1028522018-01-05 16:07:10 +00001424 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
Russell Kingfea23fb2018-01-02 10:58:58 +00001425 BMCR_ANENABLE | BMCR_ANRESTART);
Andy Fleming00db8182005-07-30 19:31:23 -04001426}
Adrian Bunk892871d2008-10-13 18:48:09 -07001427EXPORT_SYMBOL(genphy_restart_aneg);
Andy Fleming00db8182005-07-30 19:31:23 -04001428
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001429/**
1430 * genphy_config_aneg - restart auto-negotiation or write BMCR
1431 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001432 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001433 * Description: If auto-negotiation is enabled, we configure the
Andy Fleming00db8182005-07-30 19:31:23 -04001434 * advertising, and then restart auto-negotiation. If it is not
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001435 * enabled, then we write the BMCR.
Andy Fleming00db8182005-07-30 19:31:23 -04001436 */
1437int genphy_config_aneg(struct phy_device *phydev)
1438{
jbrunetd853d142016-11-28 10:46:46 +01001439 int err, changed;
1440
1441 changed = genphy_config_eee_advert(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04001442
Trent Piephode339c22008-11-19 15:52:41 -08001443 if (AUTONEG_ENABLE != phydev->autoneg)
1444 return genphy_setup_forced(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04001445
jbrunetd853d142016-11-28 10:46:46 +01001446 err = genphy_config_advert(phydev);
1447 if (err < 0) /* error */
1448 return err;
1449
1450 changed |= err;
1451
1452 if (changed == 0) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001453 /* Advertisement hasn't changed, but maybe aneg was never on to
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001454 * begin with? Or maybe phy was isolated?
1455 */
Trent Piephode339c22008-11-19 15:52:41 -08001456 int ctl = phy_read(phydev, MII_BMCR);
1457
1458 if (ctl < 0)
1459 return ctl;
1460
1461 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
jbrunetd853d142016-11-28 10:46:46 +01001462 changed = 1; /* do restart aneg */
Trent Piephode339c22008-11-19 15:52:41 -08001463 }
1464
1465 /* Only restart aneg if we are advertising something different
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001466 * than we were before.
1467 */
jbrunetd853d142016-11-28 10:46:46 +01001468 if (changed > 0)
1469 return genphy_restart_aneg(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04001470
jbrunetd853d142016-11-28 10:46:46 +01001471 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001472}
1473EXPORT_SYMBOL(genphy_config_aneg);
1474
Florian Fainellia9fa6e62014-02-11 17:27:36 -08001475/**
1476 * genphy_aneg_done - return auto-negotiation status
1477 * @phydev: target phy_device struct
1478 *
1479 * Description: Reads the status register and returns 0 either if
1480 * auto-negotiation is incomplete, or if there was an error.
1481 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
1482 */
1483int genphy_aneg_done(struct phy_device *phydev)
1484{
1485 int retval = phy_read(phydev, MII_BMSR);
1486
1487 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1488}
1489EXPORT_SYMBOL(genphy_aneg_done);
1490
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001491/**
1492 * genphy_update_link - update link status in @phydev
1493 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001494 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001495 * Description: Update the value in phydev->link to reflect the
Andy Fleming00db8182005-07-30 19:31:23 -04001496 * current link value. In order to do this, we need to read
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001497 * the status register twice, keeping the second value.
Andy Fleming00db8182005-07-30 19:31:23 -04001498 */
1499int genphy_update_link(struct phy_device *phydev)
1500{
1501 int status;
1502
1503 /* Do a fake read */
1504 status = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -04001505 if (status < 0)
1506 return status;
1507
1508 /* Read link and autonegotiation status */
1509 status = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -04001510 if (status < 0)
1511 return status;
1512
1513 if ((status & BMSR_LSTATUS) == 0)
1514 phydev->link = 0;
1515 else
1516 phydev->link = 1;
1517
1518 return 0;
1519}
Andy Fleming6b655522006-10-16 16:19:17 -05001520EXPORT_SYMBOL(genphy_update_link);
Andy Fleming00db8182005-07-30 19:31:23 -04001521
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001522/**
1523 * genphy_read_status - check the link status and update current link state
1524 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001525 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001526 * Description: Check the link, then figure out the current state
Andy Fleming00db8182005-07-30 19:31:23 -04001527 * by comparing what we advertise with what the link partner
1528 * advertises. Start by checking the gigabit possibilities,
1529 * then move on to 10/100.
1530 */
1531int genphy_read_status(struct phy_device *phydev)
1532{
1533 int adv;
1534 int err;
1535 int lpa;
1536 int lpagb = 0;
Cristian Bercarua4572e02014-02-25 10:22:48 +02001537 int common_adv;
1538 int common_adv_gb = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001539
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001540 /* Update the link, but return if there was an error */
Andy Fleming00db8182005-07-30 19:31:23 -04001541 err = genphy_update_link(phydev);
1542 if (err)
1543 return err;
1544
Florian Fainelli114002b2013-12-06 13:01:30 -08001545 phydev->lp_advertising = 0;
1546
Andy Fleming00db8182005-07-30 19:31:23 -04001547 if (AUTONEG_ENABLE == phydev->autoneg) {
1548 if (phydev->supported & (SUPPORTED_1000baseT_Half
1549 | SUPPORTED_1000baseT_Full)) {
1550 lpagb = phy_read(phydev, MII_STAT1000);
Andy Fleming00db8182005-07-30 19:31:23 -04001551 if (lpagb < 0)
1552 return lpagb;
1553
1554 adv = phy_read(phydev, MII_CTRL1000);
Andy Fleming00db8182005-07-30 19:31:23 -04001555 if (adv < 0)
1556 return adv;
1557
Heiner Kallweitb8f8c8e2018-07-21 15:48:47 +02001558 if (lpagb & LPA_1000MSFAIL) {
1559 if (adv & CTL1000_ENABLE_MASTER)
1560 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
1561 else
1562 phydev_err(phydev, "Master/Slave resolution failed\n");
1563 return -ENOLINK;
1564 }
1565
Florian Fainelli114002b2013-12-06 13:01:30 -08001566 phydev->lp_advertising =
1567 mii_stat1000_to_ethtool_lpa_t(lpagb);
Cristian Bercarua4572e02014-02-25 10:22:48 +02001568 common_adv_gb = lpagb & adv << 2;
Andy Fleming00db8182005-07-30 19:31:23 -04001569 }
1570
1571 lpa = phy_read(phydev, MII_LPA);
Andy Fleming00db8182005-07-30 19:31:23 -04001572 if (lpa < 0)
1573 return lpa;
1574
Florian Fainelli114002b2013-12-06 13:01:30 -08001575 phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
1576
Andy Fleming00db8182005-07-30 19:31:23 -04001577 adv = phy_read(phydev, MII_ADVERTISE);
Andy Fleming00db8182005-07-30 19:31:23 -04001578 if (adv < 0)
1579 return adv;
1580
Cristian Bercarua4572e02014-02-25 10:22:48 +02001581 common_adv = lpa & adv;
Andy Fleming00db8182005-07-30 19:31:23 -04001582
1583 phydev->speed = SPEED_10;
1584 phydev->duplex = DUPLEX_HALF;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001585 phydev->pause = 0;
1586 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001587
Cristian Bercarua4572e02014-02-25 10:22:48 +02001588 if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) {
Andy Fleming00db8182005-07-30 19:31:23 -04001589 phydev->speed = SPEED_1000;
1590
Cristian Bercarua4572e02014-02-25 10:22:48 +02001591 if (common_adv_gb & LPA_1000FULL)
Andy Fleming00db8182005-07-30 19:31:23 -04001592 phydev->duplex = DUPLEX_FULL;
Cristian Bercarua4572e02014-02-25 10:22:48 +02001593 } else if (common_adv & (LPA_100FULL | LPA_100HALF)) {
Andy Fleming00db8182005-07-30 19:31:23 -04001594 phydev->speed = SPEED_100;
Florian Fainellie1093742013-12-17 21:38:12 -08001595
Cristian Bercarua4572e02014-02-25 10:22:48 +02001596 if (common_adv & LPA_100FULL)
Andy Fleming00db8182005-07-30 19:31:23 -04001597 phydev->duplex = DUPLEX_FULL;
1598 } else
Cristian Bercarua4572e02014-02-25 10:22:48 +02001599 if (common_adv & LPA_10FULL)
Andy Fleming00db8182005-07-30 19:31:23 -04001600 phydev->duplex = DUPLEX_FULL;
1601
Florian Fainellie1093742013-12-17 21:38:12 -08001602 if (phydev->duplex == DUPLEX_FULL) {
Andy Fleming00db8182005-07-30 19:31:23 -04001603 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
1604 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
1605 }
1606 } else {
1607 int bmcr = phy_read(phydev, MII_BMCR);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001608
Andy Fleming00db8182005-07-30 19:31:23 -04001609 if (bmcr < 0)
1610 return bmcr;
1611
1612 if (bmcr & BMCR_FULLDPLX)
1613 phydev->duplex = DUPLEX_FULL;
1614 else
1615 phydev->duplex = DUPLEX_HALF;
1616
1617 if (bmcr & BMCR_SPEED1000)
1618 phydev->speed = SPEED_1000;
1619 else if (bmcr & BMCR_SPEED100)
1620 phydev->speed = SPEED_100;
1621 else
1622 phydev->speed = SPEED_10;
1623
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001624 phydev->pause = 0;
1625 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001626 }
1627
1628 return 0;
1629}
1630EXPORT_SYMBOL(genphy_read_status);
1631
Florian Fainelli797ac072014-02-17 13:34:02 -08001632/**
1633 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
1634 * @phydev: target phy_device struct
1635 *
1636 * Description: Perform a software PHY reset using the standard
1637 * BMCR_RESET bit and poll for the reset bit to be cleared.
1638 *
1639 * Returns: 0 on success, < 0 on failure
1640 */
1641int genphy_soft_reset(struct phy_device *phydev)
1642{
1643 int ret;
1644
1645 ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
1646 if (ret < 0)
1647 return ret;
1648
1649 return phy_poll_reset(phydev);
1650}
1651EXPORT_SYMBOL(genphy_soft_reset);
1652
Daniel Mackaf6b6962014-04-16 17:19:12 +02001653int genphy_config_init(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -04001654{
Eric Sesterhenn84c22d72006-09-25 16:39:22 -07001655 int val;
Andy Fleming00db8182005-07-30 19:31:23 -04001656 u32 features;
1657
Andy Fleming00db8182005-07-30 19:31:23 -04001658 features = (SUPPORTED_TP | SUPPORTED_MII
1659 | SUPPORTED_AUI | SUPPORTED_FIBRE |
Woojung.Huh@microchip.comcd772de2016-02-11 17:29:47 +00001660 SUPPORTED_BNC | SUPPORTED_Pause | SUPPORTED_Asym_Pause);
Andy Fleming00db8182005-07-30 19:31:23 -04001661
1662 /* Do we support autonegotiation? */
1663 val = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -04001664 if (val < 0)
1665 return val;
1666
1667 if (val & BMSR_ANEGCAPABLE)
1668 features |= SUPPORTED_Autoneg;
1669
1670 if (val & BMSR_100FULL)
1671 features |= SUPPORTED_100baseT_Full;
1672 if (val & BMSR_100HALF)
1673 features |= SUPPORTED_100baseT_Half;
1674 if (val & BMSR_10FULL)
1675 features |= SUPPORTED_10baseT_Full;
1676 if (val & BMSR_10HALF)
1677 features |= SUPPORTED_10baseT_Half;
1678
1679 if (val & BMSR_ESTATEN) {
1680 val = phy_read(phydev, MII_ESTATUS);
Andy Fleming00db8182005-07-30 19:31:23 -04001681 if (val < 0)
1682 return val;
1683
1684 if (val & ESTATUS_1000_TFULL)
1685 features |= SUPPORTED_1000baseT_Full;
1686 if (val & ESTATUS_1000_THALF)
1687 features |= SUPPORTED_1000baseT_Half;
1688 }
1689
Sascha Hauerc242a472014-05-21 15:29:44 +02001690 phydev->supported &= features;
1691 phydev->advertising &= features;
Andy Fleming00db8182005-07-30 19:31:23 -04001692
1693 return 0;
1694}
Uwe Kleine-Königa0a32d32017-05-24 00:26:07 +02001695EXPORT_SYMBOL(genphy_config_init);
Andy Fleming124059f2014-01-10 14:27:37 +08001696
Kevin Hao5df7af82018-03-20 09:44:52 +08001697/* This is used for the phy device which doesn't support the MMD extended
1698 * register access, but it does have side effect when we are trying to access
1699 * the MMD register via indirect method.
1700 */
1701int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
1702{
1703 return -EOPNOTSUPP;
1704}
1705EXPORT_SYMBOL(genphy_read_mmd_unsupported);
1706
1707int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
1708 u16 regnum, u16 val)
1709{
1710 return -EOPNOTSUPP;
1711}
1712EXPORT_SYMBOL(genphy_write_mmd_unsupported);
1713
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001714int genphy_suspend(struct phy_device *phydev)
1715{
Heiner Kallweit032f4702018-01-12 21:20:36 +01001716 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001717}
1718EXPORT_SYMBOL(genphy_suspend);
1719
1720int genphy_resume(struct phy_device *phydev)
1721{
Heiner Kallweit032f4702018-01-12 21:20:36 +01001722 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001723}
1724EXPORT_SYMBOL(genphy_resume);
Andy Fleming00db8182005-07-30 19:31:23 -04001725
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08001726int genphy_loopback(struct phy_device *phydev, bool enable)
1727{
Russell Kingf1028522018-01-05 16:07:10 +00001728 return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
Russell Kingfea23fb2018-01-02 10:58:58 +00001729 enable ? BMCR_LOOPBACK : 0);
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08001730}
1731EXPORT_SYMBOL(genphy_loopback);
1732
Simon Hormanf3a6bd32015-09-30 15:15:52 +09001733static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
1734{
Heiner Kallweitdf8ed342018-07-03 22:34:54 +02001735 phydev->supported &= ~(PHY_1000BT_FEATURES | PHY_100BT_FEATURES |
1736 PHY_10BT_FEATURES);
Simon Hormanf3a6bd32015-09-30 15:15:52 +09001737
1738 switch (max_speed) {
1739 default:
1740 return -ENOTSUPP;
1741 case SPEED_1000:
1742 phydev->supported |= PHY_1000BT_FEATURES;
1743 /* fall through */
1744 case SPEED_100:
1745 phydev->supported |= PHY_100BT_FEATURES;
1746 /* fall through */
1747 case SPEED_10:
1748 phydev->supported |= PHY_10BT_FEATURES;
1749 }
1750
1751 return 0;
1752}
1753
1754int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
1755{
1756 int err;
1757
1758 err = __set_phy_supported(phydev, max_speed);
1759 if (err)
1760 return err;
1761
1762 phydev->advertising = phydev->supported;
1763
1764 return 0;
1765}
1766EXPORT_SYMBOL(phy_set_max_speed);
1767
Andrew Lunn41124fa2018-09-12 01:53:14 +02001768/**
1769 * phy_remove_link_mode - Remove a supported link mode
1770 * @phydev: phy_device structure to remove link mode from
1771 * @link_mode: Link mode to be removed
1772 *
1773 * Description: Some MACs don't support all link modes which the PHY
1774 * does. e.g. a 1G MAC often does not support 1000Half. Add a helper
1775 * to remove a link mode.
1776 */
1777void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
1778{
1779 WARN_ON(link_mode > 31);
1780
1781 phydev->supported &= ~BIT(link_mode);
1782 phydev->advertising = phydev->supported;
1783}
1784EXPORT_SYMBOL(phy_remove_link_mode);
1785
Andrew Lunnaf8d9bb2018-09-12 01:53:15 +02001786/**
Andrew Lunnc306ad32018-09-12 01:53:16 +02001787 * phy_support_sym_pause - Enable support of symmetrical pause
1788 * @phydev: target phy_device struct
1789 *
1790 * Description: Called by the MAC to indicate is supports symmetrical
1791 * Pause, but not asym pause.
1792 */
1793void phy_support_sym_pause(struct phy_device *phydev)
1794{
1795 phydev->supported |= SUPPORTED_Pause;
1796 phydev->advertising = phydev->supported;
1797}
1798EXPORT_SYMBOL(phy_support_sym_pause);
1799
1800/**
Andrew Lunnaf8d9bb2018-09-12 01:53:15 +02001801 * phy_support_asym_pause - Enable support of asym pause
1802 * @phydev: target phy_device struct
1803 *
1804 * Description: Called by the MAC to indicate is supports Asym Pause.
1805 */
1806void phy_support_asym_pause(struct phy_device *phydev)
1807{
1808 phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
1809 phydev->advertising = phydev->supported;
1810}
1811EXPORT_SYMBOL(phy_support_asym_pause);
1812
Andrew Lunn70814e82018-09-12 01:53:17 +02001813/**
Andrew Lunn0c122402018-09-12 01:53:18 +02001814 * phy_set_sym_pause - Configure symmetric Pause
1815 * @phydev: target phy_device struct
1816 * @rx: Receiver Pause is supported
1817 * @tx: Transmit Pause is supported
1818 * @autoneg: Auto neg should be used
1819 *
1820 * Description: Configure advertised Pause support depending on if
1821 * receiver pause and pause auto neg is supported. Generally called
1822 * from the set_pauseparam .ndo.
1823 */
1824void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
1825 bool autoneg)
1826{
1827 phydev->supported &= ~SUPPORTED_Pause;
1828
1829 if (rx && tx && autoneg)
1830 phydev->supported |= SUPPORTED_Pause;
1831
1832 phydev->advertising = phydev->supported;
1833}
1834EXPORT_SYMBOL(phy_set_sym_pause);
1835
1836/**
Andrew Lunn70814e82018-09-12 01:53:17 +02001837 * phy_set_asym_pause - Configure Pause and Asym Pause
1838 * @phydev: target phy_device struct
1839 * @rx: Receiver Pause is supported
1840 * @tx: Transmit Pause is supported
1841 *
1842 * Description: Configure advertised Pause support depending on if
1843 * transmit and receiver pause is supported. If there has been a
1844 * change in adverting, trigger a new autoneg. Generally called from
1845 * the set_pauseparam .ndo.
1846 */
1847void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
1848{
1849 u16 oldadv = phydev->advertising;
1850 u16 newadv = oldadv &= ~(SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1851
1852 if (rx)
1853 newadv |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
1854 if (tx)
1855 newadv ^= SUPPORTED_Asym_Pause;
1856
1857 if (oldadv != newadv) {
1858 phydev->advertising = newadv;
1859
1860 if (phydev->autoneg)
1861 phy_start_aneg(phydev);
1862 }
1863}
1864EXPORT_SYMBOL(phy_set_asym_pause);
1865
Andrew Lunn22b7d292018-09-12 01:53:19 +02001866/**
1867 * phy_validate_pause - Test if the PHY/MAC support the pause configuration
1868 * @phydev: phy_device struct
1869 * @pp: requested pause configuration
1870 *
1871 * Description: Test if the PHY/MAC combination supports the Pause
1872 * configuration the user is requesting. Returns True if it is
1873 * supported, false otherwise.
1874 */
1875bool phy_validate_pause(struct phy_device *phydev,
1876 struct ethtool_pauseparam *pp)
1877{
1878 if (!(phydev->supported & SUPPORTED_Pause) ||
1879 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
1880 pp->rx_pause != pp->tx_pause))
1881 return false;
1882 return true;
1883}
1884EXPORT_SYMBOL(phy_validate_pause);
1885
Sascha Hauerde906af2014-05-21 15:29:45 +02001886static void of_set_phy_supported(struct phy_device *phydev)
1887{
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001888 struct device_node *node = phydev->mdio.dev.of_node;
Sascha Hauerde906af2014-05-21 15:29:45 +02001889 u32 max_speed;
1890
1891 if (!IS_ENABLED(CONFIG_OF_MDIO))
1892 return;
1893
1894 if (!node)
1895 return;
1896
Simon Hormanf3a6bd32015-09-30 15:15:52 +09001897 if (!of_property_read_u32(node, "max-speed", &max_speed))
1898 __set_phy_supported(phydev, max_speed);
Sascha Hauerde906af2014-05-21 15:29:45 +02001899}
1900
jbrunetd853d142016-11-28 10:46:46 +01001901static void of_set_phy_eee_broken(struct phy_device *phydev)
1902{
1903 struct device_node *node = phydev->mdio.dev.of_node;
jbrunet57f39862016-12-19 16:05:37 +01001904 u32 broken = 0;
jbrunetd853d142016-11-28 10:46:46 +01001905
1906 if (!IS_ENABLED(CONFIG_OF_MDIO))
1907 return;
1908
1909 if (!node)
1910 return;
1911
jbrunet57f39862016-12-19 16:05:37 +01001912 if (of_property_read_bool(node, "eee-broken-100tx"))
1913 broken |= MDIO_EEE_100TX;
1914 if (of_property_read_bool(node, "eee-broken-1000t"))
1915 broken |= MDIO_EEE_1000T;
1916 if (of_property_read_bool(node, "eee-broken-10gt"))
1917 broken |= MDIO_EEE_10GT;
1918 if (of_property_read_bool(node, "eee-broken-1000kx"))
1919 broken |= MDIO_EEE_1000KX;
1920 if (of_property_read_bool(node, "eee-broken-10gkx4"))
1921 broken |= MDIO_EEE_10GKX4;
1922 if (of_property_read_bool(node, "eee-broken-10gkr"))
1923 broken |= MDIO_EEE_10GKR;
1924
1925 phydev->eee_broken_modes = broken;
jbrunetd853d142016-11-28 10:46:46 +01001926}
1927
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001928/**
1929 * phy_probe - probe and init a PHY device
1930 * @dev: device to probe and init
Andy Fleming00db8182005-07-30 19:31:23 -04001931 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001932 * Description: Take care of setting up the phy_device structure,
Andy Fleming00db8182005-07-30 19:31:23 -04001933 * set the state to READY (the driver's init function should
1934 * set it to STARTING if needed).
1935 */
1936static int phy_probe(struct device *dev)
1937{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001938 struct phy_device *phydev = to_phy_device(dev);
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001939 struct device_driver *drv = phydev->mdio.dev.driver;
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001940 struct phy_driver *phydrv = to_phy_driver(drv);
Andy Fleming00db8182005-07-30 19:31:23 -04001941 int err = 0;
1942
Andy Fleming00db8182005-07-30 19:31:23 -04001943 phydev->drv = phydrv;
1944
Florian Fainelli2c7b4922013-05-19 22:53:42 +00001945 /* Disable the interrupt if the PHY doesn't support it
1946 * but the interrupt is still a valid one
1947 */
1948 if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001949 phy_interrupt_is_valid(phydev))
Andy Fleming00db8182005-07-30 19:31:23 -04001950 phydev->irq = PHY_POLL;
1951
Florian Fainelli4284b6a2013-05-23 01:11:12 +00001952 if (phydrv->flags & PHY_IS_INTERNAL)
1953 phydev->is_internal = true;
1954
Nate Case35b5f6b2008-01-29 10:05:09 -06001955 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001956
1957 /* Start out supporting everything. Eventually,
1958 * a controller will attach, and may modify one
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001959 * or both of these values
1960 */
Andy Fleming00db8182005-07-30 19:31:23 -04001961 phydev->supported = phydrv->features;
Sascha Hauerde906af2014-05-21 15:29:45 +02001962 of_set_phy_supported(phydev);
1963 phydev->advertising = phydev->supported;
Andy Fleming00db8182005-07-30 19:31:23 -04001964
jbrunetd853d142016-11-28 10:46:46 +01001965 /* Get the EEE modes we want to prohibit. We will ask
1966 * the PHY stop advertising these mode later on
1967 */
1968 of_set_phy_eee_broken(phydev);
1969
Timur Tabi529ed122016-12-07 13:20:51 -06001970 /* The Pause Frame bits indicate that the PHY can support passing
1971 * pause frames. During autonegotiation, the PHYs will determine if
1972 * they should allow pause frames to pass. The MAC driver should then
1973 * use that result to determine whether to enable flow control via
1974 * pause frames.
1975 *
1976 * Normally, PHY drivers should not set the Pause bits, and instead
1977 * allow phylib to do that. However, there may be some situations
1978 * (e.g. hardware erratum) where the driver wants to set only one
1979 * of these bits.
1980 */
1981 if (phydrv->features & (SUPPORTED_Pause | SUPPORTED_Asym_Pause)) {
1982 phydev->supported &= ~(SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1983 phydev->supported |= phydrv->features &
1984 (SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1985 } else {
1986 phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
1987 }
1988
Andy Fleming00db8182005-07-30 19:31:23 -04001989 /* Set the state to READY by default */
1990 phydev->state = PHY_READY;
1991
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01001992 if (phydev->drv->probe) {
1993 /* Deassert the reset signal */
1994 phy_device_reset(phydev, 0);
1995
Andy Fleming00db8182005-07-30 19:31:23 -04001996 err = phydev->drv->probe(phydev);
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01001997 if (err) {
1998 /* Assert the reset signal */
1999 phy_device_reset(phydev, 1);
2000 }
2001 }
Andy Fleming00db8182005-07-30 19:31:23 -04002002
Nate Case35b5f6b2008-01-29 10:05:09 -06002003 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04002004
Andy Fleming00db8182005-07-30 19:31:23 -04002005 return err;
2006}
2007
2008static int phy_remove(struct device *dev)
2009{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03002010 struct phy_device *phydev = to_phy_device(dev);
Andy Fleming00db8182005-07-30 19:31:23 -04002011
Florian Fainelli7b9a88a2017-02-17 16:07:33 -08002012 cancel_delayed_work_sync(&phydev->state_queue);
2013
Nate Case35b5f6b2008-01-29 10:05:09 -06002014 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04002015 phydev->state = PHY_DOWN;
Nate Case35b5f6b2008-01-29 10:05:09 -06002016 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04002017
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01002018 if (phydev->drv && phydev->drv->remove) {
Andy Fleming00db8182005-07-30 19:31:23 -04002019 phydev->drv->remove(phydev);
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01002020
2021 /* Assert the reset signal */
2022 phy_device_reset(phydev, 1);
2023 }
Andy Fleming00db8182005-07-30 19:31:23 -04002024 phydev->drv = NULL;
2025
2026 return 0;
2027}
2028
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002029/**
2030 * phy_driver_register - register a phy_driver with the PHY layer
2031 * @new_driver: new phy_driver to register
Andrew Lunnbe01da72016-01-06 20:11:22 +01002032 * @owner: module owning this PHY
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002033 */
Andrew Lunnbe01da72016-01-06 20:11:22 +01002034int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
Andy Fleming00db8182005-07-30 19:31:23 -04002035{
2036 int retval;
2037
Andrew Lunna9049e02016-01-06 20:11:26 +01002038 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
2039 new_driver->mdiodrv.driver.name = new_driver->name;
2040 new_driver->mdiodrv.driver.bus = &mdio_bus_type;
2041 new_driver->mdiodrv.driver.probe = phy_probe;
2042 new_driver->mdiodrv.driver.remove = phy_remove;
2043 new_driver->mdiodrv.driver.owner = owner;
Andy Fleming00db8182005-07-30 19:31:23 -04002044
Andrew Lunna9049e02016-01-06 20:11:26 +01002045 retval = driver_register(&new_driver->mdiodrv.driver);
Andy Fleming00db8182005-07-30 19:31:23 -04002046 if (retval) {
Joe Perches8d242482012-06-09 07:49:07 +00002047 pr_err("%s: Error %d in registering driver\n",
2048 new_driver->name, retval);
Andy Fleming00db8182005-07-30 19:31:23 -04002049
2050 return retval;
2051 }
2052
Olof Johanssonf2511f12007-11-04 16:09:23 -06002053 pr_debug("%s: Registered new driver\n", new_driver->name);
Andy Fleming00db8182005-07-30 19:31:23 -04002054
2055 return 0;
2056}
2057EXPORT_SYMBOL(phy_driver_register);
2058
Andrew Lunnbe01da72016-01-06 20:11:22 +01002059int phy_drivers_register(struct phy_driver *new_driver, int n,
2060 struct module *owner)
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00002061{
2062 int i, ret = 0;
2063
2064 for (i = 0; i < n; i++) {
Andrew Lunnbe01da72016-01-06 20:11:22 +01002065 ret = phy_driver_register(new_driver + i, owner);
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00002066 if (ret) {
2067 while (i-- > 0)
2068 phy_driver_unregister(new_driver + i);
2069 break;
2070 }
2071 }
2072 return ret;
2073}
2074EXPORT_SYMBOL(phy_drivers_register);
2075
Andy Fleming00db8182005-07-30 19:31:23 -04002076void phy_driver_unregister(struct phy_driver *drv)
2077{
Andrew Lunna9049e02016-01-06 20:11:26 +01002078 driver_unregister(&drv->mdiodrv.driver);
Andy Fleming00db8182005-07-30 19:31:23 -04002079}
2080EXPORT_SYMBOL(phy_driver_unregister);
2081
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00002082void phy_drivers_unregister(struct phy_driver *drv, int n)
2083{
2084 int i;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03002085
2086 for (i = 0; i < n; i++)
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00002087 phy_driver_unregister(drv + i);
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00002088}
2089EXPORT_SYMBOL(phy_drivers_unregister);
2090
Russell King921690f2017-06-05 12:23:05 +01002091static struct phy_driver genphy_driver = {
Andy Fleminge1393452005-08-24 18:46:21 -05002092 .phy_id = 0xffffffff,
2093 .phy_id_mask = 0xffffffff,
2094 .name = "Generic PHY",
Florian Fainelli0878fff2017-03-05 12:34:49 -08002095 .soft_reset = genphy_no_soft_reset,
Andy Fleminge1393452005-08-24 18:46:21 -05002096 .config_init = genphy_config_init,
Sascha Hauerc242a472014-05-21 15:29:44 +02002097 .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
2098 SUPPORTED_AUI | SUPPORTED_FIBRE |
2099 SUPPORTED_BNC,
Florian Fainelli76a423a2014-02-11 17:27:37 -08002100 .aneg_done = genphy_aneg_done,
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08002101 .suspend = genphy_suspend,
2102 .resume = genphy_resume,
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08002103 .set_loopback = genphy_loopback,
Russell King921690f2017-06-05 12:23:05 +01002104};
Andy Fleming00db8182005-07-30 19:31:23 -04002105
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002106static int __init phy_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -04002107{
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002108 int rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002109
2110 rc = mdio_bus_init();
2111 if (rc)
Andy Fleminge1393452005-08-24 18:46:21 -05002112 return rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002113
Russell King921690f2017-06-05 12:23:05 +01002114 rc = phy_driver_register(&genphy_10g_driver, THIS_MODULE);
Andy Fleminge1393452005-08-24 18:46:21 -05002115 if (rc)
Russell King921690f2017-06-05 12:23:05 +01002116 goto err_10g;
2117
2118 rc = phy_driver_register(&genphy_driver, THIS_MODULE);
2119 if (rc) {
2120 phy_driver_unregister(&genphy_10g_driver);
2121err_10g:
Andy Fleminge1393452005-08-24 18:46:21 -05002122 mdio_bus_exit();
Russell King921690f2017-06-05 12:23:05 +01002123 }
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002124
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002125 return rc;
Andy Fleming00db8182005-07-30 19:31:23 -04002126}
2127
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002128static void __exit phy_exit(void)
Andy Fleming00db8182005-07-30 19:31:23 -04002129{
Russell King921690f2017-06-05 12:23:05 +01002130 phy_driver_unregister(&genphy_10g_driver);
2131 phy_driver_unregister(&genphy_driver);
Andy Fleminge1393452005-08-24 18:46:21 -05002132 mdio_bus_exit();
Andy Fleming00db8182005-07-30 19:31:23 -04002133}
2134
Andy Fleminge1393452005-08-24 18:46:21 -05002135subsys_initcall(phy_init);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002136module_exit(phy_exit);