blob: 40404a8f5f5e54e23e1a95384a85b265e0bd2e8e [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>
Andrew Lunn719655a2018-09-29 23:04:16 +020032#include <linux/bitmap.h>
Andy Fleming00db8182005-07-30 19:31:23 -040033#include <linux/phy.h>
Zach Brown2e0bc452016-10-17 10:49:55 -050034#include <linux/phy_led_triggers.h>
Andy Fleming124059f2014-01-10 14:27:37 +080035#include <linux/mdio.h>
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030036#include <linux/io.h>
37#include <linux/uaccess.h>
Sascha Hauerde906af2014-05-21 15:29:45 +020038#include <linux/of.h>
Andy Fleming00db8182005-07-30 19:31:23 -040039
Andy Fleming00db8182005-07-30 19:31:23 -040040#include <asm/irq.h>
Andy Fleming00db8182005-07-30 19:31:23 -040041
Olaf Heringafcceaa2005-12-14 00:33:49 +010042MODULE_DESCRIPTION("PHY library");
43MODULE_AUTHOR("Andy Fleming");
44MODULE_LICENSE("GPL");
45
Andrew Lunn719655a2018-09-29 23:04:16 +020046__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
47EXPORT_SYMBOL_GPL(phy_basic_features);
48
49__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
50EXPORT_SYMBOL_GPL(phy_basic_t1_features);
51
52__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
53EXPORT_SYMBOL_GPL(phy_gbit_features);
54
55__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
56EXPORT_SYMBOL_GPL(phy_gbit_fibre_features);
57
58__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
59EXPORT_SYMBOL_GPL(phy_gbit_all_ports_features);
60
61__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
62EXPORT_SYMBOL_GPL(phy_10gbit_features);
63
64static const int phy_basic_ports_array[] = {
65 ETHTOOL_LINK_MODE_Autoneg_BIT,
66 ETHTOOL_LINK_MODE_TP_BIT,
67 ETHTOOL_LINK_MODE_MII_BIT,
68};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010069EXPORT_SYMBOL_GPL(phy_basic_ports_array);
Andrew Lunn719655a2018-09-29 23:04:16 +020070
71static const int phy_fibre_port_array[] = {
72 ETHTOOL_LINK_MODE_FIBRE_BIT,
73};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010074EXPORT_SYMBOL_GPL(phy_fibre_port_array);
Andrew Lunn719655a2018-09-29 23:04:16 +020075
76static const int phy_all_ports_features_array[] = {
77 ETHTOOL_LINK_MODE_Autoneg_BIT,
78 ETHTOOL_LINK_MODE_TP_BIT,
79 ETHTOOL_LINK_MODE_MII_BIT,
80 ETHTOOL_LINK_MODE_FIBRE_BIT,
81 ETHTOOL_LINK_MODE_AUI_BIT,
82 ETHTOOL_LINK_MODE_BNC_BIT,
83 ETHTOOL_LINK_MODE_Backplane_BIT,
84};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010085EXPORT_SYMBOL_GPL(phy_all_ports_features_array);
Andrew Lunn719655a2018-09-29 23:04:16 +020086
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010087const int phy_10_100_features_array[4] = {
Andrew Lunn719655a2018-09-29 23:04:16 +020088 ETHTOOL_LINK_MODE_10baseT_Half_BIT,
89 ETHTOOL_LINK_MODE_10baseT_Full_BIT,
90 ETHTOOL_LINK_MODE_100baseT_Half_BIT,
91 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
92};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010093EXPORT_SYMBOL_GPL(phy_10_100_features_array);
Andrew Lunn719655a2018-09-29 23:04:16 +020094
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010095const int phy_basic_t1_features_array[2] = {
Andrew Lunn719655a2018-09-29 23:04:16 +020096 ETHTOOL_LINK_MODE_TP_BIT,
97 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
98};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010099EXPORT_SYMBOL_GPL(phy_basic_t1_features_array);
Andrew Lunn719655a2018-09-29 23:04:16 +0200100
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100101const int phy_gbit_features_array[2] = {
Andrew Lunn719655a2018-09-29 23:04:16 +0200102 ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
103 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
104};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100105EXPORT_SYMBOL_GPL(phy_gbit_features_array);
Andrew Lunn719655a2018-09-29 23:04:16 +0200106
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100107const int phy_10gbit_features_array[1] = {
Andrew Lunn719655a2018-09-29 23:04:16 +0200108 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
109};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100110EXPORT_SYMBOL_GPL(phy_10gbit_features_array);
Andrew Lunn719655a2018-09-29 23:04:16 +0200111
112__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
113EXPORT_SYMBOL_GPL(phy_10gbit_full_features);
114
115static const int phy_10gbit_full_features_array[] = {
116 ETHTOOL_LINK_MODE_10baseT_Full_BIT,
117 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
118 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
119 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
120};
121
122static void features_init(void)
123{
124 /* 10/100 half/full*/
125 linkmode_set_bit_array(phy_basic_ports_array,
126 ARRAY_SIZE(phy_basic_ports_array),
127 phy_basic_features);
128 linkmode_set_bit_array(phy_10_100_features_array,
129 ARRAY_SIZE(phy_10_100_features_array),
130 phy_basic_features);
131
132 /* 100 full, TP */
133 linkmode_set_bit_array(phy_basic_t1_features_array,
134 ARRAY_SIZE(phy_basic_t1_features_array),
135 phy_basic_t1_features);
136
137 /* 10/100 half/full + 1000 half/full */
138 linkmode_set_bit_array(phy_basic_ports_array,
139 ARRAY_SIZE(phy_basic_ports_array),
140 phy_gbit_features);
141 linkmode_set_bit_array(phy_10_100_features_array,
142 ARRAY_SIZE(phy_10_100_features_array),
143 phy_gbit_features);
144 linkmode_set_bit_array(phy_gbit_features_array,
145 ARRAY_SIZE(phy_gbit_features_array),
146 phy_gbit_features);
147
148 /* 10/100 half/full + 1000 half/full + fibre*/
149 linkmode_set_bit_array(phy_basic_ports_array,
150 ARRAY_SIZE(phy_basic_ports_array),
151 phy_gbit_fibre_features);
152 linkmode_set_bit_array(phy_10_100_features_array,
153 ARRAY_SIZE(phy_10_100_features_array),
154 phy_gbit_fibre_features);
155 linkmode_set_bit_array(phy_gbit_features_array,
156 ARRAY_SIZE(phy_gbit_features_array),
157 phy_gbit_fibre_features);
158 linkmode_set_bit_array(phy_fibre_port_array,
159 ARRAY_SIZE(phy_fibre_port_array),
160 phy_gbit_fibre_features);
161
162 /* 10/100 half/full + 1000 half/full + TP/MII/FIBRE/AUI/BNC/Backplane*/
163 linkmode_set_bit_array(phy_all_ports_features_array,
164 ARRAY_SIZE(phy_all_ports_features_array),
165 phy_gbit_all_ports_features);
166 linkmode_set_bit_array(phy_10_100_features_array,
167 ARRAY_SIZE(phy_10_100_features_array),
168 phy_gbit_all_ports_features);
169 linkmode_set_bit_array(phy_gbit_features_array,
170 ARRAY_SIZE(phy_gbit_features_array),
171 phy_gbit_all_ports_features);
172
173 /* 10/100 half/full + 1000 half/full + 10G full*/
174 linkmode_set_bit_array(phy_all_ports_features_array,
175 ARRAY_SIZE(phy_all_ports_features_array),
176 phy_10gbit_features);
177 linkmode_set_bit_array(phy_10_100_features_array,
178 ARRAY_SIZE(phy_10_100_features_array),
179 phy_10gbit_features);
180 linkmode_set_bit_array(phy_gbit_features_array,
181 ARRAY_SIZE(phy_gbit_features_array),
182 phy_10gbit_features);
183 linkmode_set_bit_array(phy_10gbit_features_array,
184 ARRAY_SIZE(phy_10gbit_features_array),
185 phy_10gbit_features);
186
187 /* 10/100/1000/10G full */
188 linkmode_set_bit_array(phy_all_ports_features_array,
189 ARRAY_SIZE(phy_all_ports_features_array),
190 phy_10gbit_full_features);
191 linkmode_set_bit_array(phy_10gbit_full_features_array,
192 ARRAY_SIZE(phy_10gbit_full_features_array),
193 phy_10gbit_full_features);
194}
195
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300196void phy_device_free(struct phy_device *phydev)
197{
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100198 put_device(&phydev->mdio.dev);
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300199}
Grant Likely4dea5472009-04-25 12:52:46 +0000200EXPORT_SYMBOL(phy_device_free);
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300201
Andrew Lunn711fdba2016-01-06 20:11:27 +0100202static void phy_mdio_device_free(struct mdio_device *mdiodev)
203{
204 struct phy_device *phydev;
205
206 phydev = container_of(mdiodev, struct phy_device, mdio);
207 phy_device_free(phydev);
208}
209
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300210static void phy_device_release(struct device *dev)
211{
Petr Malatb2a43192013-02-28 01:01:52 +0000212 kfree(to_phy_device(dev));
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300213}
214
Andrew Lunn711fdba2016-01-06 20:11:27 +0100215static void phy_mdio_device_remove(struct mdio_device *mdiodev)
216{
217 struct phy_device *phydev;
218
219 phydev = container_of(mdiodev, struct phy_device, mdio);
220 phy_device_remove(phydev);
221}
222
Russell King921690f2017-06-05 12:23:05 +0100223static struct phy_driver genphy_driver;
224extern struct phy_driver genphy_10g_driver;
Grant Likely4dea5472009-04-25 12:52:46 +0000225
Andy Flemingf62220d2008-04-18 17:29:54 -0500226static LIST_HEAD(phy_fixup_list);
227static DEFINE_MUTEX(phy_fixup_lock);
228
Andrew Lunnbc879222016-01-06 20:11:21 +0100229#ifdef CONFIG_PM
230static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
231{
232 struct device_driver *drv = phydev->mdio.dev.driver;
233 struct phy_driver *phydrv = to_phy_driver(drv);
234 struct net_device *netdev = phydev->attached_dev;
235
236 if (!drv || !phydrv->suspend)
237 return false;
238
239 /* PHY not attached? May suspend if the PHY has not already been
240 * suspended as part of a prior call to phy_disconnect() ->
241 * phy_detach() -> phy_suspend() because the parent netdev might be the
242 * MDIO bus driver and clock gated at this point.
243 */
244 if (!netdev)
245 return !phydev->suspended;
246
Heiner Kallweit93f41e62018-09-24 22:01:32 +0200247 if (netdev->wol_enabled)
248 return false;
249
250 /* As long as not all affected network drivers support the
251 * wol_enabled flag, let's check for hints that WoL is enabled.
252 * Don't suspend PHY if the attached netdev parent may wake up.
Andrew Lunnbc879222016-01-06 20:11:21 +0100253 * The parent may point to a PCI device, as in tg3 driver.
254 */
255 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
256 return false;
257
258 /* Also don't suspend PHY if the netdev itself may wakeup. This
259 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
260 * e.g. SoC devices.
261 */
262 if (device_may_wakeup(&netdev->dev))
263 return false;
264
265 return true;
266}
267
268static int mdio_bus_phy_suspend(struct device *dev)
269{
270 struct phy_device *phydev = to_phy_device(dev);
271
272 /* We must stop the state machine manually, otherwise it stops out of
273 * control, possibly with the phydev->lock held. Upon resume, netdev
274 * may call phy routines that try to grab the same lock, and that may
275 * lead to a deadlock.
276 */
277 if (phydev->attached_dev && phydev->adjust_link)
278 phy_stop_machine(phydev);
279
280 if (!mdio_bus_phy_may_suspend(phydev))
281 return 0;
282
283 return phy_suspend(phydev);
284}
285
286static int mdio_bus_phy_resume(struct device *dev)
287{
288 struct phy_device *phydev = to_phy_device(dev);
289 int ret;
290
291 if (!mdio_bus_phy_may_suspend(phydev))
292 goto no_resume;
293
294 ret = phy_resume(phydev);
295 if (ret < 0)
296 return ret;
297
298no_resume:
299 if (phydev->attached_dev && phydev->adjust_link)
300 phy_start_machine(phydev);
301
302 return 0;
303}
304
305static int mdio_bus_phy_restore(struct device *dev)
306{
307 struct phy_device *phydev = to_phy_device(dev);
308 struct net_device *netdev = phydev->attached_dev;
309 int ret;
310
311 if (!netdev)
312 return 0;
313
314 ret = phy_init_hw(phydev);
315 if (ret < 0)
316 return ret;
317
318 /* The PHY needs to renegotiate. */
319 phydev->link = 0;
320 phydev->state = PHY_UP;
321
322 phy_start_machine(phydev);
323
324 return 0;
325}
326
327static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
328 .suspend = mdio_bus_phy_suspend,
329 .resume = mdio_bus_phy_resume,
330 .freeze = mdio_bus_phy_suspend,
331 .thaw = mdio_bus_phy_resume,
332 .restore = mdio_bus_phy_restore,
333};
334
335#define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
336
337#else
338
339#define MDIO_BUS_PHY_PM_OPS NULL
340
341#endif /* CONFIG_PM */
342
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300343/**
344 * phy_register_fixup - creates a new phy_fixup and adds it to the list
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100345 * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
Andy Flemingf62220d2008-04-18 17:29:54 -0500346 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300347 * It can also be PHY_ANY_UID
Andy Flemingf62220d2008-04-18 17:29:54 -0500348 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300349 * comparison
Andy Flemingf62220d2008-04-18 17:29:54 -0500350 * @run: The actual code to be run when a matching PHY is found
351 */
352int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300353 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -0500354{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300355 struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
Andy Flemingf62220d2008-04-18 17:29:54 -0500356
Andy Flemingf62220d2008-04-18 17:29:54 -0500357 if (!fixup)
358 return -ENOMEM;
359
Kay Sieversfb28ad32008-11-10 13:55:14 -0800360 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
Andy Flemingf62220d2008-04-18 17:29:54 -0500361 fixup->phy_uid = phy_uid;
362 fixup->phy_uid_mask = phy_uid_mask;
363 fixup->run = run;
364
365 mutex_lock(&phy_fixup_lock);
366 list_add_tail(&fixup->list, &phy_fixup_list);
367 mutex_unlock(&phy_fixup_lock);
368
369 return 0;
370}
371EXPORT_SYMBOL(phy_register_fixup);
372
373/* Registers a fixup to be run on any PHY with the UID in phy_uid */
374int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300375 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -0500376{
377 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
378}
379EXPORT_SYMBOL(phy_register_fixup_for_uid);
380
381/* Registers a fixup to be run on the PHY with id string bus_id */
382int phy_register_fixup_for_id(const char *bus_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300383 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -0500384{
385 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
386}
387EXPORT_SYMBOL(phy_register_fixup_for_id);
388
Woojung.Huh@microchip.comf38e7a32016-12-07 20:26:07 +0000389/**
390 * phy_unregister_fixup - remove a phy_fixup from the list
391 * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
392 * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
393 * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
394 */
395int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
396{
397 struct list_head *pos, *n;
398 struct phy_fixup *fixup;
399 int ret;
400
401 ret = -ENODEV;
402
403 mutex_lock(&phy_fixup_lock);
404 list_for_each_safe(pos, n, &phy_fixup_list) {
405 fixup = list_entry(pos, struct phy_fixup, list);
406
407 if ((!strcmp(fixup->bus_id, bus_id)) &&
408 ((fixup->phy_uid & phy_uid_mask) ==
409 (phy_uid & phy_uid_mask))) {
410 list_del(&fixup->list);
411 kfree(fixup);
412 ret = 0;
413 break;
414 }
415 }
416 mutex_unlock(&phy_fixup_lock);
417
418 return ret;
419}
420EXPORT_SYMBOL(phy_unregister_fixup);
421
422/* Unregisters a fixup of any PHY with the UID in phy_uid */
423int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
424{
425 return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
426}
427EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
428
429/* Unregisters a fixup of the PHY with id string bus_id */
430int phy_unregister_fixup_for_id(const char *bus_id)
431{
432 return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
433}
434EXPORT_SYMBOL(phy_unregister_fixup_for_id);
435
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300436/* Returns 1 if fixup matches phydev in bus_id and phy_uid.
Andy Flemingf62220d2008-04-18 17:29:54 -0500437 * Fixups can be set to match any in one or more fields.
438 */
439static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
440{
Andrew Lunn84eff6d2016-01-06 20:11:10 +0100441 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
Andy Flemingf62220d2008-04-18 17:29:54 -0500442 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
443 return 0;
444
445 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300446 (phydev->phy_id & fixup->phy_uid_mask))
Andy Flemingf62220d2008-04-18 17:29:54 -0500447 if (fixup->phy_uid != PHY_ANY_UID)
448 return 0;
449
450 return 1;
451}
452
453/* Runs any matching fixups for this phydev */
Sergei Shtylyovfbfcec62014-01-05 03:28:27 +0300454static int phy_scan_fixups(struct phy_device *phydev)
Andy Flemingf62220d2008-04-18 17:29:54 -0500455{
456 struct phy_fixup *fixup;
457
458 mutex_lock(&phy_fixup_lock);
459 list_for_each_entry(fixup, &phy_fixup_list, list) {
460 if (phy_needs_fixup(phydev, fixup)) {
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300461 int err = fixup->run(phydev);
Andy Flemingf62220d2008-04-18 17:29:54 -0500462
Jiri Slabybc23283c2009-07-13 11:23:39 +0000463 if (err < 0) {
464 mutex_unlock(&phy_fixup_lock);
Andy Flemingf62220d2008-04-18 17:29:54 -0500465 return err;
Jiri Slabybc23283c2009-07-13 11:23:39 +0000466 }
Florian Fainellib0ae0092014-02-11 17:27:41 -0800467 phydev->has_fixups = true;
Andy Flemingf62220d2008-04-18 17:29:54 -0500468 }
469 }
470 mutex_unlock(&phy_fixup_lock);
471
472 return 0;
473}
Andy Flemingf62220d2008-04-18 17:29:54 -0500474
Andrew Lunne76a4952016-01-06 20:11:23 +0100475static int phy_bus_match(struct device *dev, struct device_driver *drv)
476{
477 struct phy_device *phydev = to_phy_device(dev);
478 struct phy_driver *phydrv = to_phy_driver(drv);
479 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
480 int i;
481
Andrew Lunna9049e02016-01-06 20:11:26 +0100482 if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
483 return 0;
484
Andrew Lunne76a4952016-01-06 20:11:23 +0100485 if (phydrv->match_phy_device)
486 return phydrv->match_phy_device(phydev);
487
488 if (phydev->is_c45) {
489 for (i = 1; i < num_ids; i++) {
490 if (!(phydev->c45_ids.devices_in_package & (1 << i)))
491 continue;
492
493 if ((phydrv->phy_id & phydrv->phy_id_mask) ==
494 (phydev->c45_ids.device_ids[i] &
495 phydrv->phy_id_mask))
496 return 1;
497 }
498 return 0;
499 } else {
500 return (phydrv->phy_id & phydrv->phy_id_mask) ==
501 (phydev->phy_id & phydrv->phy_id_mask);
502 }
503}
504
Heiner Kallweit7f4828f2018-06-02 22:36:06 +0200505static ssize_t
506phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
507{
508 struct phy_device *phydev = to_phy_device(dev);
509
510 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
511}
512static DEVICE_ATTR_RO(phy_id);
513
514static ssize_t
515phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
516{
517 struct phy_device *phydev = to_phy_device(dev);
518 const char *mode = NULL;
519
520 if (phy_is_internal(phydev))
521 mode = "internal";
522 else
523 mode = phy_modes(phydev->interface);
524
525 return sprintf(buf, "%s\n", mode);
526}
527static DEVICE_ATTR_RO(phy_interface);
528
529static ssize_t
530phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
531 char *buf)
532{
533 struct phy_device *phydev = to_phy_device(dev);
534
535 return sprintf(buf, "%d\n", phydev->has_fixups);
536}
537static DEVICE_ATTR_RO(phy_has_fixups);
538
539static struct attribute *phy_dev_attrs[] = {
540 &dev_attr_phy_id.attr,
541 &dev_attr_phy_interface.attr,
542 &dev_attr_phy_has_fixups.attr,
543 NULL,
544};
545ATTRIBUTE_GROUPS(phy_dev);
546
547static const struct device_type mdio_bus_phy_type = {
548 .name = "PHY",
549 .groups = phy_dev_groups,
550 .release = phy_device_release,
551 .pm = MDIO_BUS_PHY_PM_OPS,
552};
553
David Daneyac28b9f2012-06-27 07:33:35 +0000554struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300555 bool is_c45,
556 struct phy_c45_device_ids *c45_ids)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700557{
558 struct phy_device *dev;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100559 struct mdio_device *mdiodev;
David Woodhouse8626d3b2010-04-02 01:05:27 +0000560
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300561 /* We allocate the device, and initialize the default values */
Robert P. J. Daycd861282006-12-13 00:34:52 -0800562 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Sergei Shtylyovef899c02015-08-28 21:35:14 +0300563 if (!dev)
Sergei Shtylyovd3765f02015-08-28 21:34:34 +0300564 return ERR_PTR(-ENOMEM);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700565
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100566 mdiodev = &dev->mdio;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100567 mdiodev->dev.parent = &bus->dev;
568 mdiodev->dev.bus = &mdio_bus_type;
Heiner Kallweit7f4828f2018-06-02 22:36:06 +0200569 mdiodev->dev.type = &mdio_bus_phy_type;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100570 mdiodev->bus = bus;
Andrew Lunne76a4952016-01-06 20:11:23 +0100571 mdiodev->bus_match = phy_bus_match;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100572 mdiodev->addr = addr;
Andrew Lunn7f854422016-01-06 20:11:18 +0100573 mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
Andrew Lunn711fdba2016-01-06 20:11:27 +0100574 mdiodev->device_free = phy_mdio_device_free;
575 mdiodev->device_remove = phy_mdio_device_remove;
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300576
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700577 dev->speed = 0;
578 dev->duplex = -1;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300579 dev->pause = 0;
580 dev->asym_pause = 0;
Heiner Kallweitc69851e2018-03-11 15:00:37 +0100581 dev->link = 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600582 dev->interface = PHY_INTERFACE_MODE_GMII;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700583
584 dev->autoneg = AUTONEG_ENABLE;
585
David Daneyac28b9f2012-06-27 07:33:35 +0000586 dev->is_c45 = is_c45;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700587 dev->phy_id = phy_id;
David Daneyac28b9f2012-06-27 07:33:35 +0000588 if (c45_ids)
589 dev->c45_ids = *c45_ids;
Dan Carpenter47b356e2016-01-12 12:36:21 +0300590 dev->irq = bus->irq[addr];
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100591 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700592
593 dev->state = PHY_DOWN;
594
Nate Case35b5f6b2008-01-29 10:05:09 -0600595 mutex_init(&dev->lock);
Anton Vorontsov4f9c85a2010-01-18 05:37:16 +0000596 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700597
David Woodhouse8626d3b2010-04-02 01:05:27 +0000598 /* Request the appropriate module unconditionally; don't
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300599 * bother trying to do so only if it isn't already loaded,
600 * because that gets complicated. A hotplug event would have
601 * done an unconditional modprobe anyway.
602 * We don't do normal hotplug because it won't work for MDIO
603 * -- because it relies on the device staying around for long
604 * enough for the driver to get loaded. With MDIO, the NIC
605 * driver will get bored and give up as soon as it finds that
606 * there's no driver _already_ loaded.
607 */
David Woodhouse8626d3b2010-04-02 01:05:27 +0000608 request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
609
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100610 device_initialize(&mdiodev->dev);
Petr Malatb2a43192013-02-28 01:01:52 +0000611
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700612 return dev;
613}
David Daneyac28b9f2012-06-27 07:33:35 +0000614EXPORT_SYMBOL(phy_device_create);
615
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800616/* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
617 * @bus: the target MII bus
618 * @addr: PHY address on the MII bus
619 * @dev_addr: MMD address in the PHY.
620 * @devices_in_package: where to store the devices in package information.
621 *
622 * Description: reads devices in package registers of a MMD at @dev_addr
623 * from PHY at @addr on @bus.
624 *
625 * Returns: 0 on success, -EIO on failure.
626 */
627static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
628 u32 *devices_in_package)
629{
630 int phy_reg, reg_addr;
631
632 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
633 phy_reg = mdiobus_read(bus, addr, reg_addr);
634 if (phy_reg < 0)
635 return -EIO;
636 *devices_in_package = (phy_reg & 0xffff) << 16;
637
638 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
639 phy_reg = mdiobus_read(bus, addr, reg_addr);
640 if (phy_reg < 0)
641 return -EIO;
642 *devices_in_package |= (phy_reg & 0xffff);
643
644 return 0;
645}
646
David Daneyac28b9f2012-06-27 07:33:35 +0000647/**
648 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
649 * @bus: the target MII bus
650 * @addr: PHY address on the MII bus
651 * @phy_id: where to store the ID retrieved.
652 * @c45_ids: where to store the c45 ID information.
653 *
654 * If the PHY devices-in-package appears to be valid, it and the
655 * corresponding identifiers are stored in @c45_ids, zero is stored
656 * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns
657 * zero on success.
658 *
659 */
660static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
661 struct phy_c45_device_ids *c45_ids) {
662 int phy_reg;
663 int i, reg_addr;
664 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800665 u32 *devs = &c45_ids->devices_in_package;
David Daneyac28b9f2012-06-27 07:33:35 +0000666
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800667 /* Find first non-zero Devices In package. Device zero is reserved
668 * for 802.3 c45 complied PHYs, so don't probe it at first.
David Daneyac28b9f2012-06-27 07:33:35 +0000669 */
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800670 for (i = 1; i < num_ids && *devs == 0; i++) {
671 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
David Daneyac28b9f2012-06-27 07:33:35 +0000672 if (phy_reg < 0)
673 return -EIO;
David Daneyac28b9f2012-06-27 07:33:35 +0000674
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800675 if ((*devs & 0x1fffffff) == 0x1fffffff) {
676 /* If mostly Fs, there is no device there,
677 * then let's continue to probe more, as some
678 * 10G PHYs have zero Devices In package,
679 * e.g. Cortina CS4315/CS4340 PHY.
680 */
681 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
682 if (phy_reg < 0)
683 return -EIO;
684 /* no device there, let's get out of here */
685 if ((*devs & 0x1fffffff) == 0x1fffffff) {
Shengzhou Liuda1da282015-06-26 17:58:52 +0800686 *phy_id = 0xffffffff;
687 return 0;
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800688 } else {
689 break;
Shengzhou Liuda1da282015-06-26 17:58:52 +0800690 }
David Daneyac28b9f2012-06-27 07:33:35 +0000691 }
692 }
693
694 /* Now probe Device Identifiers for each device present. */
695 for (i = 1; i < num_ids; i++) {
696 if (!(c45_ids->devices_in_package & (1 << i)))
697 continue;
698
699 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
700 phy_reg = mdiobus_read(bus, addr, reg_addr);
701 if (phy_reg < 0)
702 return -EIO;
703 c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16;
704
705 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
706 phy_reg = mdiobus_read(bus, addr, reg_addr);
707 if (phy_reg < 0)
708 return -EIO;
709 c45_ids->device_ids[i] |= (phy_reg & 0xffff);
710 }
711 *phy_id = 0;
712 return 0;
713}
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700714
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800715/**
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400716 * get_phy_id - reads the specified addr for its ID.
717 * @bus: the target MII bus
718 * @addr: PHY address on the MII bus
719 * @phy_id: where to store the ID retrieved.
David Daneyac28b9f2012-06-27 07:33:35 +0000720 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
721 * @c45_ids: where to store the c45 ID information.
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400722 *
David Daneyac28b9f2012-06-27 07:33:35 +0000723 * Description: In the case of a 802.3-c22 PHY, reads the ID registers
724 * of the PHY at @addr on the @bus, stores it in @phy_id and returns
725 * zero on success.
726 *
727 * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
728 * its return value is in turn returned.
729 *
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400730 */
David Daneyac28b9f2012-06-27 07:33:35 +0000731static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
732 bool is_c45, struct phy_c45_device_ids *c45_ids)
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400733{
734 int phy_reg;
735
David Daneyac28b9f2012-06-27 07:33:35 +0000736 if (is_c45)
737 return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
738
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300739 /* Grab the bits from PHYIR1, and put them in the upper half */
David Daney6fe32642011-09-30 11:51:22 +0000740 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
Alexandre Belloni02a6efc2018-04-24 18:09:04 +0200741 if (phy_reg < 0) {
742 /* if there is no device, return without an error so scanning
743 * the bus works properly
744 */
745 if (phy_reg == -EIO || phy_reg == -ENODEV) {
746 *phy_id = 0xffffffff;
747 return 0;
748 }
749
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400750 return -EIO;
Alexandre Belloni02a6efc2018-04-24 18:09:04 +0200751 }
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400752
753 *phy_id = (phy_reg & 0xffff) << 16;
754
755 /* Grab the bits from PHYIR2, and put them in the lower half */
David Daney6fe32642011-09-30 11:51:22 +0000756 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400757 if (phy_reg < 0)
758 return -EIO;
759
760 *phy_id |= (phy_reg & 0xffff);
761
762 return 0;
763}
764
765/**
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300766 * get_phy_device - reads the specified PHY device and returns its @phy_device
767 * struct
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800768 * @bus: the target MII bus
769 * @addr: PHY address on the MII bus
David Daneyac28b9f2012-06-27 07:33:35 +0000770 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
Andy Fleming00db8182005-07-30 19:31:23 -0400771 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800772 * Description: Reads the ID registers of the PHY at @addr on the
773 * @bus, then allocates and returns the phy_device to represent it.
Andy Fleming00db8182005-07-30 19:31:23 -0400774 */
David Daneyac28b9f2012-06-27 07:33:35 +0000775struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
Andy Fleming00db8182005-07-30 19:31:23 -0400776{
David Daneyac28b9f2012-06-27 07:33:35 +0000777 struct phy_c45_device_ids c45_ids = {0};
David S. Miller160c85f2012-06-27 21:28:14 -0700778 u32 phy_id = 0;
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400779 int r;
Andy Fleming00db8182005-07-30 19:31:23 -0400780
David Daneyac28b9f2012-06-27 07:33:35 +0000781 r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400782 if (r)
783 return ERR_PTR(r);
Andy Fleming00db8182005-07-30 19:31:23 -0400784
Giuseppe Cavallaro6436cbc2008-11-20 20:43:18 -0800785 /* If the phy_id is mostly Fs, there is no device there */
786 if ((phy_id & 0x1fffffff) == 0x1fffffff)
Sergei Shtylyovb74766a2016-04-24 20:25:23 +0300787 return ERR_PTR(-ENODEV);
Giuseppe Cavallaro6436cbc2008-11-20 20:43:18 -0800788
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300789 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
Andy Fleming00db8182005-07-30 19:31:23 -0400790}
Grant Likely4dea5472009-04-25 12:52:46 +0000791EXPORT_SYMBOL(get_phy_device);
792
793/**
794 * phy_device_register - Register the phy device on the MDIO bus
Randy Dunlap1d4ac5d2009-06-16 16:56:33 +0000795 * @phydev: phy_device structure to be added to the MDIO bus
Grant Likely4dea5472009-04-25 12:52:46 +0000796 */
797int phy_device_register(struct phy_device *phydev)
798{
799 int err;
800
Andrew Lunn7f854422016-01-06 20:11:18 +0100801 err = mdiobus_register_device(&phydev->mdio);
802 if (err)
803 return err;
Grant Likely4dea5472009-04-25 12:52:46 +0000804
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +0100805 /* Deassert the reset signal */
806 phy_device_reset(phydev, 0);
807
Grant Likely4dea5472009-04-25 12:52:46 +0000808 /* Run all of the fixups for this PHY */
Florian Fainellid92f5de2014-07-28 16:28:07 -0700809 err = phy_scan_fixups(phydev);
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800810 if (err) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100811 pr_err("PHY %d failed to initialize\n", phydev->mdio.addr);
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800812 goto out;
813 }
Grant Likely4dea5472009-04-25 12:52:46 +0000814
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100815 err = device_add(&phydev->mdio.dev);
Grant Likely4dea5472009-04-25 12:52:46 +0000816 if (err) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100817 pr_err("PHY %d failed to add\n", phydev->mdio.addr);
Grant Likely4dea5472009-04-25 12:52:46 +0000818 goto out;
819 }
820
821 return 0;
822
823 out:
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +0100824 /* Assert the reset signal */
825 phy_device_reset(phydev, 1);
826
Andrew Lunn7f854422016-01-06 20:11:18 +0100827 mdiobus_unregister_device(&phydev->mdio);
Grant Likely4dea5472009-04-25 12:52:46 +0000828 return err;
829}
830EXPORT_SYMBOL(phy_device_register);
Andy Fleming00db8182005-07-30 19:31:23 -0400831
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800832/**
Russell King38737e42015-09-24 20:36:28 +0100833 * phy_device_remove - Remove a previously registered phy device from the MDIO bus
834 * @phydev: phy_device structure to remove
835 *
836 * This doesn't free the phy_device itself, it merely reverses the effects
837 * of phy_device_register(). Use phy_device_free() to free the device
838 * after calling this function.
839 */
840void phy_device_remove(struct phy_device *phydev)
841{
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100842 device_del(&phydev->mdio.dev);
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +0100843
844 /* Assert the reset signal */
845 phy_device_reset(phydev, 1);
846
Andrew Lunn7f854422016-01-06 20:11:18 +0100847 mdiobus_unregister_device(&phydev->mdio);
Russell King38737e42015-09-24 20:36:28 +0100848}
849EXPORT_SYMBOL(phy_device_remove);
850
851/**
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800852 * phy_find_first - finds the first PHY device on the bus
853 * @bus: the target MII bus
854 */
855struct phy_device *phy_find_first(struct mii_bus *bus)
856{
Andrew Lunn7f854422016-01-06 20:11:18 +0100857 struct phy_device *phydev;
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800858 int addr;
859
860 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
Andrew Lunn7f854422016-01-06 20:11:18 +0100861 phydev = mdiobus_get_phy(bus, addr);
862 if (phydev)
863 return phydev;
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800864 }
865 return NULL;
866}
867EXPORT_SYMBOL(phy_find_first);
868
Russell Kinga81497b2017-07-25 15:02:58 +0100869static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier)
870{
871 struct net_device *netdev = phydev->attached_dev;
872
873 if (do_carrier) {
874 if (up)
875 netif_carrier_on(netdev);
876 else
877 netif_carrier_off(netdev);
878 }
879 phydev->adjust_link(netdev);
880}
881
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800882/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800883 * phy_prepare_link - prepares the PHY layer to monitor link status
884 * @phydev: target phy_device struct
885 * @handler: callback function for link status change notifications
Andy Fleming00db8182005-07-30 19:31:23 -0400886 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800887 * Description: Tells the PHY infrastructure to handle the
Andy Fleming00db8182005-07-30 19:31:23 -0400888 * gory details on monitoring link status (whether through
889 * polling or an interrupt), and to call back to the
890 * connected device driver when the link status changes.
891 * If you want to monitor your own link state, don't call
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800892 * this function.
893 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000894static void phy_prepare_link(struct phy_device *phydev,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300895 void (*handler)(struct net_device *))
Andy Fleming00db8182005-07-30 19:31:23 -0400896{
897 phydev->adjust_link = handler;
898}
899
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800900/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000901 * phy_connect_direct - connect an ethernet device to a specific phy_device
902 * @dev: the network device to connect
903 * @phydev: the pointer to the phy device
904 * @handler: callback function for state change notifications
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000905 * @interface: PHY device's interface
906 */
907int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
Florian Fainellif9a8f832013-01-14 00:52:52 +0000908 void (*handler)(struct net_device *),
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000909 phy_interface_t interface)
910{
911 int rc;
912
Florian Fainellif9a8f832013-01-14 00:52:52 +0000913 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000914 if (rc)
915 return rc;
916
917 phy_prepare_link(phydev, handler);
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300918 phy_start_machine(phydev);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000919 if (phydev->irq > 0)
920 phy_start_interrupts(phydev);
921
922 return 0;
923}
924EXPORT_SYMBOL(phy_connect_direct);
925
926/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800927 * phy_connect - connect an ethernet device to a PHY device
928 * @dev: the network device to connect
Randy Dunlap5d12b132008-04-28 10:58:22 -0700929 * @bus_id: the id string of the PHY device to connect
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800930 * @handler: callback function for state change notifications
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800931 * @interface: PHY device's interface
Andy Fleminge1393452005-08-24 18:46:21 -0500932 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800933 * Description: Convenience function for connecting ethernet
Andy Fleminge1393452005-08-24 18:46:21 -0500934 * devices to PHY devices. The default behavior is for
935 * the PHY infrastructure to handle everything, and only notify
936 * the connected driver when the link status changes. If you
937 * don't want, or can't use the provided functionality, you may
938 * choose to call only the subset of functions which provide
939 * the desired functionality.
940 */
Florian Fainellie1093742013-12-17 21:38:12 -0800941struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300942 void (*handler)(struct net_device *),
943 phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500944{
945 struct phy_device *phydev;
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000946 struct device *d;
947 int rc;
Andy Fleminge1393452005-08-24 18:46:21 -0500948
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000949 /* Search the list of PHY devices on the mdio bus for the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300950 * PHY with the requested name
951 */
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000952 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
953 if (!d) {
954 pr_err("PHY %s not found\n", bus_id);
955 return ERR_PTR(-ENODEV);
956 }
957 phydev = to_phy_device(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500958
Florian Fainellif9a8f832013-01-14 00:52:52 +0000959 rc = phy_connect_direct(dev, phydev, handler, interface);
Johan Hovold17ae1c62016-11-03 18:40:19 +0100960 put_device(d);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000961 if (rc)
962 return ERR_PTR(rc);
Andy Fleminge1393452005-08-24 18:46:21 -0500963
964 return phydev;
965}
966EXPORT_SYMBOL(phy_connect);
967
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800968/**
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300969 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
970 * device
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800971 * @phydev: target phy_device struct
972 */
Andy Fleminge1393452005-08-24 18:46:21 -0500973void phy_disconnect(struct phy_device *phydev)
974{
975 if (phydev->irq > 0)
976 phy_stop_interrupts(phydev);
977
978 phy_stop_machine(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800979
Andy Fleminge1393452005-08-24 18:46:21 -0500980 phydev->adjust_link = NULL;
981
982 phy_detach(phydev);
983}
984EXPORT_SYMBOL(phy_disconnect);
985
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800986/**
987 * phy_poll_reset - Safely wait until a PHY reset has properly completed
988 * @phydev: The PHY device to poll
989 *
990 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
991 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
992 * register must be polled until the BMCR_RESET bit clears.
993 *
994 * Furthermore, any attempts to write to PHY registers may have no effect
995 * or even generate MDIO bus errors until this is complete.
996 *
997 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
998 * standard and do not fully reset after the BMCR_RESET bit is set, and may
999 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
1000 * effort to support such broken PHYs, this function is separate from the
1001 * standard phy_init_hw() which will zero all the other bits in the BMCR
1002 * and reapply all driver-specific and board-specific fixups.
1003 */
1004static int phy_poll_reset(struct phy_device *phydev)
1005{
1006 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
1007 unsigned int retries = 12;
1008 int ret;
1009
1010 do {
1011 msleep(50);
1012 ret = phy_read(phydev, MII_BMCR);
1013 if (ret < 0)
1014 return ret;
1015 } while (ret & BMCR_RESET && --retries);
1016 if (ret & BMCR_RESET)
1017 return -ETIMEDOUT;
1018
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001019 /* Some chips (smsc911x) may still need up to another 1ms after the
Florian Fainelli87aa9f92013-12-06 13:01:34 -08001020 * BMCR_RESET bit is cleared before they are usable.
1021 */
1022 msleep(1);
1023 return 0;
1024}
1025
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001026int phy_init_hw(struct phy_device *phydev)
1027{
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001028 int ret = 0;
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001029
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01001030 /* Deassert the reset signal */
1031 phy_device_reset(phydev, 0);
1032
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001033 if (!phydev->drv || !phydev->drv->config_init)
1034 return 0;
1035
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001036 if (phydev->drv->soft_reset)
1037 ret = phydev->drv->soft_reset(phydev);
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001038
Florian Fainelli87aa9f92013-12-06 13:01:34 -08001039 if (ret < 0)
1040 return ret;
1041
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001042 ret = phy_scan_fixups(phydev);
1043 if (ret < 0)
1044 return ret;
1045
1046 return phydev->drv->config_init(phydev);
1047}
Florian Fainelli87aa9f92013-12-06 13:01:34 -08001048EXPORT_SYMBOL(phy_init_hw);
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001049
Andrew Lunn22209432016-01-06 20:11:13 +01001050void phy_attached_info(struct phy_device *phydev)
1051{
1052 phy_attached_print(phydev, NULL);
1053}
1054EXPORT_SYMBOL(phy_attached_info);
1055
Romain Perier5e369ae2017-09-04 10:41:36 +02001056#define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
Andrew Lunn22209432016-01-06 20:11:13 +01001057void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1058{
Florian Fainellifcd03e32017-08-22 14:26:47 -07001059 const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
Romain Perier5e369ae2017-09-04 10:41:36 +02001060 char *irq_str;
Geert Uytterhoeven059fbe82017-09-21 13:27:02 +02001061 char irq_num[8];
Romain Perier5e369ae2017-09-04 10:41:36 +02001062
1063 switch(phydev->irq) {
1064 case PHY_POLL:
1065 irq_str = "POLL";
1066 break;
1067 case PHY_IGNORE_INTERRUPT:
1068 irq_str = "IGNORE";
1069 break;
1070 default:
1071 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
1072 irq_str = irq_num;
1073 break;
1074 }
1075
Florian Fainellifcd03e32017-08-22 14:26:47 -07001076
Andrew Lunn22209432016-01-06 20:11:13 +01001077 if (!fmt) {
Andrew Lunnc4fabb82018-09-29 23:04:11 +02001078 phydev_info(phydev, ATTACHED_FMT "\n",
Florian Fainellifcd03e32017-08-22 14:26:47 -07001079 drv_name, phydev_name(phydev),
Romain Perier5e369ae2017-09-04 10:41:36 +02001080 irq_str);
Andrew Lunn22209432016-01-06 20:11:13 +01001081 } else {
1082 va_list ap;
1083
Andrew Lunnc4fabb82018-09-29 23:04:11 +02001084 phydev_info(phydev, ATTACHED_FMT,
Florian Fainellifcd03e32017-08-22 14:26:47 -07001085 drv_name, phydev_name(phydev),
Romain Perier5e369ae2017-09-04 10:41:36 +02001086 irq_str);
Andrew Lunn22209432016-01-06 20:11:13 +01001087
1088 va_start(ap, fmt);
1089 vprintk(fmt, ap);
1090 va_end(ap);
1091 }
1092}
1093EXPORT_SYMBOL(phy_attached_print);
1094
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001095/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001096 * phy_attach_direct - attach a network device to a given PHY device pointer
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001097 * @dev: network device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001098 * @phydev: Pointer to phy_device to attach
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001099 * @flags: PHY device's dev_flags
1100 * @interface: PHY device's interface
1101 *
1102 * Description: Called by drivers to attach to a particular PHY
1103 * device. The phy_device is found, and properly hooked up
Andy Fleming257184d2014-01-10 14:27:54 +08001104 * to the phy_driver. If no driver is attached, then a
1105 * generic driver is used. The phy_device is given a ptr to
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001106 * the attaching device, and given a callback for link status
1107 * change. The phy_device is returned to the attaching driver.
Russell King73229672015-09-24 20:36:08 +01001108 * This function takes a reference on the phy device.
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001109 */
Andy Fleming257184d2014-01-10 14:27:54 +08001110int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1111 u32 flags, phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -05001112{
Florian Fainelliec988ad2016-12-06 20:54:43 -08001113 struct module *ndev_owner = dev->dev.parent->driver->owner;
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001114 struct mii_bus *bus = phydev->mdio.bus;
1115 struct device *d = &phydev->mdio.dev;
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001116 bool using_genphy = false;
Marc Kleine-Budded005a092011-03-28 14:54:08 +00001117 int err;
Andy Fleminge1393452005-08-24 18:46:21 -05001118
Florian Fainelliec988ad2016-12-06 20:54:43 -08001119 /* For Ethernet device drivers that register their own MDIO bus, we
1120 * will have bus->owner match ndev_mod, so we do not want to increment
1121 * our own module->refcnt here, otherwise we would not be able to
1122 * unload later on.
1123 */
1124 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
Russell King3e3aaf62015-09-24 20:36:02 +01001125 dev_err(&dev->dev, "failed to get the bus module\n");
1126 return -EIO;
1127 }
1128
Russell King73229672015-09-24 20:36:08 +01001129 get_device(d);
1130
Andy Fleminge1393452005-08-24 18:46:21 -05001131 /* Assume that if there is no driver, that it doesn't
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001132 * exist, and we should use the genphy driver.
1133 */
Sergei Shtylyovef899c02015-08-28 21:35:14 +03001134 if (!d->driver) {
Andy Fleming257184d2014-01-10 14:27:54 +08001135 if (phydev->is_c45)
Russell King921690f2017-06-05 12:23:05 +01001136 d->driver = &genphy_10g_driver.mdiodrv.driver;
Andy Fleming257184d2014-01-10 14:27:54 +08001137 else
Russell King921690f2017-06-05 12:23:05 +01001138 d->driver = &genphy_driver.mdiodrv.driver;
Andy Fleminge1393452005-08-24 18:46:21 -05001139
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001140 using_genphy = true;
1141 }
1142
1143 if (!try_module_get(d->driver->owner)) {
1144 dev_err(&dev->dev, "failed to get the device driver module\n");
1145 err = -EIO;
1146 goto error_put_device;
1147 }
1148
1149 if (using_genphy) {
Andy Fleminge1393452005-08-24 18:46:21 -05001150 err = d->driver->probe(d);
Jeff Garzikb7a00ec2006-10-01 07:27:46 -04001151 if (err >= 0)
1152 err = device_bind_driver(d);
Andy Fleminge1393452005-08-24 18:46:21 -05001153
Jeff Garzikb7a00ec2006-10-01 07:27:46 -04001154 if (err)
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001155 goto error_module_put;
Andy Fleminge1393452005-08-24 18:46:21 -05001156 }
1157
1158 if (phydev->attached_dev) {
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001159 dev_err(&dev->dev, "PHY already attached\n");
Russell King3e3aaf62015-09-24 20:36:02 +01001160 err = -EBUSY;
1161 goto error;
Ezequiel Garciab3565f22014-07-23 16:47:32 -03001162 }
1163
Russell Kinga81497b2017-07-25 15:02:58 +01001164 phydev->phy_link_change = phy_link_change;
Andy Fleminge1393452005-08-24 18:46:21 -05001165 phydev->attached_dev = dev;
Richard Cochranc1f19b52010-07-17 08:49:36 +00001166 dev->phydev = phydev;
Florian Fainellia3995462017-05-27 10:42:25 -07001167
1168 /* Some Ethernet drivers try to connect to a PHY device before
1169 * calling register_netdevice() -> netdev_register_kobject() and
1170 * does the dev->dev.kobj initialization. Here we only check for
1171 * success which indicates that the network device kobject is
1172 * ready. Once we do that we still need to keep track of whether
1173 * links were successfully set up or not for phy_detach() to
1174 * remove them accordingly.
1175 */
1176 phydev->sysfs_links = false;
1177
Florian Fainelli55683632017-05-25 09:21:41 -07001178 err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1179 "attached_dev");
Florian Fainellia3995462017-05-27 10:42:25 -07001180 if (!err) {
Grygorii Strashko4414b3e2018-03-16 17:08:35 -05001181 err = sysfs_create_link_nowarn(&dev->dev.kobj,
1182 &phydev->mdio.dev.kobj,
1183 "phydev");
1184 if (err) {
1185 dev_err(&dev->dev, "could not add device link to %s err %d\n",
1186 kobject_name(&phydev->mdio.dev.kobj),
1187 err);
1188 /* non-fatal - some net drivers can use one netdevice
1189 * with more then one phy
1190 */
1191 }
Florian Fainelli55683632017-05-25 09:21:41 -07001192
Florian Fainellia3995462017-05-27 10:42:25 -07001193 phydev->sysfs_links = true;
1194 }
Andy Fleminge1393452005-08-24 18:46:21 -05001195
1196 phydev->dev_flags = flags;
1197
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001198 phydev->interface = interface;
1199
Anton Vorontsovef24b162010-08-24 14:46:12 -07001200 phydev->state = PHY_READY;
1201
Sjoerd Simons113c74d2016-01-14 21:57:18 +01001202 /* Initial carrier state is off as the phy is about to be
1203 * (re)initialized.
1204 */
1205 netif_carrier_off(phydev->attached_dev);
1206
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001207 /* Do initial configuration here, now that
1208 * we have certain key parameters
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001209 * (dev_flags and interface)
1210 */
Marc Kleine-Budded005a092011-03-28 14:54:08 +00001211 err = phy_init_hw(phydev);
1212 if (err)
Woojung Huha7dac9f2016-11-23 23:10:33 +00001213 goto error;
Sebastian Hesselbarth1211ce52013-12-13 10:20:28 +01001214
Woojung Huha7dac9f2016-11-23 23:10:33 +00001215 phy_resume(phydev);
Zach Brown2e0bc452016-10-17 10:49:55 -05001216 phy_led_triggers_register(phydev);
1217
Marc Kleine-Budded005a092011-03-28 14:54:08 +00001218 return err;
Russell King3e3aaf62015-09-24 20:36:02 +01001219
1220error:
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001221 /* phy_detach() does all of the cleanup below */
Woojung Huha7dac9f2016-11-23 23:10:33 +00001222 phy_detach(phydev);
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001223 return err;
1224
1225error_module_put:
Mao Wenancafe8df2017-01-31 18:46:43 -08001226 module_put(d->driver->owner);
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001227error_put_device:
1228 put_device(d);
Florian Fainelliec988ad2016-12-06 20:54:43 -08001229 if (ndev_owner != bus->owner)
1230 module_put(bus->owner);
Russell King3e3aaf62015-09-24 20:36:02 +01001231 return err;
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001232}
Andy Fleming257184d2014-01-10 14:27:54 +08001233EXPORT_SYMBOL(phy_attach_direct);
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001234
1235/**
1236 * phy_attach - attach a network device to a particular PHY device
1237 * @dev: network device to attach
1238 * @bus_id: Bus ID of PHY device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001239 * @interface: PHY device's interface
1240 *
1241 * Description: Same as phy_attach_direct() except that a PHY bus_id
1242 * string is passed instead of a pointer to a struct phy_device.
1243 */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001244struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1245 phy_interface_t interface)
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001246{
1247 struct bus_type *bus = &mdio_bus_type;
1248 struct phy_device *phydev;
1249 struct device *d;
1250 int rc;
1251
1252 /* Search the list of PHY devices on the mdio bus for the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001253 * PHY with the requested name
1254 */
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001255 d = bus_find_device_by_name(bus, NULL, bus_id);
1256 if (!d) {
1257 pr_err("PHY %s not found\n", bus_id);
1258 return ERR_PTR(-ENODEV);
1259 }
1260 phydev = to_phy_device(d);
1261
Florian Fainellif9a8f832013-01-14 00:52:52 +00001262 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
Johan Hovold17ae1c62016-11-03 18:40:19 +01001263 put_device(d);
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001264 if (rc)
1265 return ERR_PTR(rc);
1266
Andy Fleminge1393452005-08-24 18:46:21 -05001267 return phydev;
1268}
1269EXPORT_SYMBOL(phy_attach);
1270
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001271/**
1272 * phy_detach - detach a PHY device from its network device
1273 * @phydev: target phy_device struct
Russell King73229672015-09-24 20:36:08 +01001274 *
1275 * This detaches the phy device from its network device and the phy
1276 * driver, and drops the reference count taken in phy_attach_direct().
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001277 */
Andy Fleminge1393452005-08-24 18:46:21 -05001278void phy_detach(struct phy_device *phydev)
1279{
Florian Fainelliec988ad2016-12-06 20:54:43 -08001280 struct net_device *dev = phydev->attached_dev;
1281 struct module *ndev_owner = dev->dev.parent->driver->owner;
Russell King3e3aaf62015-09-24 20:36:02 +01001282 struct mii_bus *bus;
Ezequiel Garciab3565f22014-07-23 16:47:32 -03001283
Florian Fainellia3995462017-05-27 10:42:25 -07001284 if (phydev->sysfs_links) {
1285 sysfs_remove_link(&dev->dev.kobj, "phydev");
1286 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1287 }
Heiner Kallweit93f41e62018-09-24 22:01:32 +02001288 phy_suspend(phydev);
Richard Cochranc1f19b52010-07-17 08:49:36 +00001289 phydev->attached_dev->phydev = NULL;
Andy Fleminge1393452005-08-24 18:46:21 -05001290 phydev->attached_dev = NULL;
Russell King9525ae82017-07-25 15:03:13 +01001291 phydev->phylink = NULL;
Andy Fleminge1393452005-08-24 18:46:21 -05001292
Geert Uytterhoeven0075bd62016-11-28 15:18:31 +01001293 phy_led_triggers_unregister(phydev);
1294
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001295 module_put(phydev->mdio.dev.driver->owner);
1296
Andy Fleminge1393452005-08-24 18:46:21 -05001297 /* If the device had no specific driver before (i.e. - it
1298 * was using the generic driver), we unbind the device
1299 * from the generic driver so that there's a chance a
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001300 * real driver could be loaded
1301 */
Russell King921690f2017-06-05 12:23:05 +01001302 if (phydev->mdio.dev.driver == &genphy_10g_driver.mdiodrv.driver ||
1303 phydev->mdio.dev.driver == &genphy_driver.mdiodrv.driver)
1304 device_release_driver(&phydev->mdio.dev);
Russell King3e3aaf62015-09-24 20:36:02 +01001305
Russell King73229672015-09-24 20:36:08 +01001306 /*
1307 * The phydev might go away on the put_device() below, so avoid
1308 * a use-after-free bug by reading the underlying bus first.
1309 */
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001310 bus = phydev->mdio.bus;
Russell King3e3aaf62015-09-24 20:36:02 +01001311
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001312 put_device(&phydev->mdio.dev);
Florian Fainelliec988ad2016-12-06 20:54:43 -08001313 if (ndev_owner != bus->owner)
1314 module_put(bus->owner);
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01001315
1316 /* Assert the reset signal */
1317 phy_device_reset(phydev, 1);
Andy Fleminge1393452005-08-24 18:46:21 -05001318}
1319EXPORT_SYMBOL(phy_detach);
1320
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001321int phy_suspend(struct phy_device *phydev)
1322{
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001323 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
Heiner Kallweit93f41e62018-09-24 22:01:32 +02001324 struct net_device *netdev = phydev->attached_dev;
Sebastian Hesselbarth32fc3fd2014-03-14 10:07:44 +01001325 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Florian Fainelli8a477a62015-01-26 22:05:39 -08001326 int ret = 0;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001327
1328 /* If the device has WOL enabled, we cannot suspend the PHY */
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001329 phy_ethtool_get_wol(phydev, &wol);
Heiner Kallweit93f41e62018-09-24 22:01:32 +02001330 if (wol.wolopts || (netdev && netdev->wol_enabled))
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001331 return -EBUSY;
1332
Florian Fainelli25149ef2017-02-17 16:07:34 -08001333 if (phydev->drv && phydrv->suspend)
Florian Fainelli8a477a62015-01-26 22:05:39 -08001334 ret = phydrv->suspend(phydev);
1335
1336 if (ret)
1337 return ret;
1338
1339 phydev->suspended = true;
1340
1341 return ret;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001342}
Florian Fainellica127692014-07-08 10:38:36 -07001343EXPORT_SYMBOL(phy_suspend);
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001344
Andrew Lunn9c2c2e62018-02-27 01:56:06 +01001345int __phy_resume(struct phy_device *phydev)
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001346{
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001347 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
Florian Fainelli8a477a62015-01-26 22:05:39 -08001348 int ret = 0;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001349
Russell Kingf5e64032017-12-12 10:45:36 +00001350 WARN_ON(!mutex_is_locked(&phydev->lock));
1351
Florian Fainelli25149ef2017-02-17 16:07:34 -08001352 if (phydev->drv && phydrv->resume)
Florian Fainelli8a477a62015-01-26 22:05:39 -08001353 ret = phydrv->resume(phydev);
1354
1355 if (ret)
1356 return ret;
1357
1358 phydev->suspended = false;
1359
1360 return ret;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001361}
Andrew Lunn9c2c2e62018-02-27 01:56:06 +01001362EXPORT_SYMBOL(__phy_resume);
1363
1364int phy_resume(struct phy_device *phydev)
1365{
1366 int ret;
1367
1368 mutex_lock(&phydev->lock);
1369 ret = __phy_resume(phydev);
1370 mutex_unlock(&phydev->lock);
1371
1372 return ret;
1373}
Florian Fainellica127692014-07-08 10:38:36 -07001374EXPORT_SYMBOL(phy_resume);
Andy Fleminge1393452005-08-24 18:46:21 -05001375
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08001376int phy_loopback(struct phy_device *phydev, bool enable)
1377{
1378 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1379 int ret = 0;
1380
1381 mutex_lock(&phydev->lock);
1382
1383 if (enable && phydev->loopback_enabled) {
1384 ret = -EBUSY;
1385 goto out;
1386 }
1387
1388 if (!enable && !phydev->loopback_enabled) {
1389 ret = -EINVAL;
1390 goto out;
1391 }
1392
1393 if (phydev->drv && phydrv->set_loopback)
1394 ret = phydrv->set_loopback(phydev, enable);
1395 else
1396 ret = -EOPNOTSUPP;
1397
1398 if (ret)
1399 goto out;
1400
1401 phydev->loopback_enabled = enable;
1402
1403out:
1404 mutex_unlock(&phydev->lock);
1405 return ret;
1406}
1407EXPORT_SYMBOL(phy_loopback);
1408
Richard Leitnera9668492017-12-11 13:16:58 +01001409/**
1410 * phy_reset_after_clk_enable - perform a PHY reset if needed
1411 * @phydev: target phy_device struct
1412 *
1413 * Description: Some PHYs are known to need a reset after their refclk was
1414 * enabled. This function evaluates the flags and perform the reset if it's
1415 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
1416 * was reset.
1417 */
1418int phy_reset_after_clk_enable(struct phy_device *phydev)
1419{
1420 if (!phydev || !phydev->drv)
1421 return -ENODEV;
1422
1423 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1424 phy_device_reset(phydev, 1);
1425 phy_device_reset(phydev, 0);
1426 return 1;
1427 }
1428
1429 return 0;
1430}
1431EXPORT_SYMBOL(phy_reset_after_clk_enable);
1432
Andy Fleming00db8182005-07-30 19:31:23 -04001433/* Generic PHY support and helper functions */
1434
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001435/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001436 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001437 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001438 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001439 * Description: Writes MII_ADVERTISE with the appropriate values,
Andy Fleming00db8182005-07-30 19:31:23 -04001440 * after sanitizing the values to make sure we only advertise
Trent Piepho51e2a382008-09-24 10:55:46 +00001441 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1442 * hasn't changed, and > 0 if it has changed.
Andy Fleming00db8182005-07-30 19:31:23 -04001443 */
stephen hemminger89ff05e2010-10-21 08:37:41 +00001444static int genphy_config_advert(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -04001445{
1446 u32 advertise;
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001447 int oldadv, adv, bmsr;
Trent Piepho51e2a382008-09-24 10:55:46 +00001448 int err, changed = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001449
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001450 /* Only allow advertising what this PHY supports */
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001451 linkmode_and(phydev->advertising, phydev->advertising,
1452 phydev->supported);
1453 if (!ethtool_convert_link_mode_to_legacy_u32(&advertise,
1454 phydev->advertising))
1455 phydev_warn(phydev, "PHY advertising (%*pb) more modes than genphy supports, some modes not advertised.\n",
1456 __ETHTOOL_LINK_MODE_MASK_NBITS,
1457 phydev->advertising);
Andy Fleming00db8182005-07-30 19:31:23 -04001458
1459 /* Setup standard advertisement */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001460 adv = phy_read(phydev, MII_ADVERTISE);
Andy Fleming00db8182005-07-30 19:31:23 -04001461 if (adv < 0)
1462 return adv;
1463
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001464 oldadv = adv;
Matt Carlson28011cf2011-11-16 18:36:59 -05001465 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
Andy Fleming00db8182005-07-30 19:31:23 -04001466 ADVERTISE_PAUSE_ASYM);
Matt Carlson37f07022011-11-17 14:30:55 +00001467 adv |= ethtool_adv_to_mii_adv_t(advertise);
Andy Fleming00db8182005-07-30 19:31:23 -04001468
Trent Piepho51e2a382008-09-24 10:55:46 +00001469 if (adv != oldadv) {
1470 err = phy_write(phydev, MII_ADVERTISE, adv);
Andy Fleming00db8182005-07-30 19:31:23 -04001471
Trent Piepho51e2a382008-09-24 10:55:46 +00001472 if (err < 0)
1473 return err;
1474 changed = 1;
1475 }
Andy Fleming00db8182005-07-30 19:31:23 -04001476
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001477 bmsr = phy_read(phydev, MII_BMSR);
1478 if (bmsr < 0)
1479 return bmsr;
1480
1481 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1482 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1483 * logical 1.
1484 */
1485 if (!(bmsr & BMSR_ESTATEN))
1486 return changed;
1487
Andy Fleming00db8182005-07-30 19:31:23 -04001488 /* Configure gigabit if it's supported */
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001489 adv = phy_read(phydev, MII_CTRL1000);
1490 if (adv < 0)
1491 return adv;
1492
1493 oldadv = adv;
1494 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1495
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001496 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1497 phydev->supported) ||
1498 linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1499 phydev->supported))
Matt Carlson37f07022011-11-17 14:30:55 +00001500 adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
Andy Fleming00db8182005-07-30 19:31:23 -04001501
Mugunthan V Neb686232015-06-25 22:21:02 +05301502 if (adv != oldadv)
1503 changed = 1;
1504
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001505 err = phy_write(phydev, MII_CTRL1000, adv);
1506 if (err < 0)
1507 return err;
1508
Trent Piepho51e2a382008-09-24 10:55:46 +00001509 return changed;
Andy Fleming00db8182005-07-30 19:31:23 -04001510}
Andy Fleming00db8182005-07-30 19:31:23 -04001511
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001512/**
jbrunetd853d142016-11-28 10:46:46 +01001513 * genphy_config_eee_advert - disable unwanted eee mode advertisement
1514 * @phydev: target phy_device struct
1515 *
1516 * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
1517 * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
1518 * changed, and 1 if it has changed.
1519 */
1520static int genphy_config_eee_advert(struct phy_device *phydev)
1521{
jbrunet3bb9ab62016-12-19 16:05:36 +01001522 int broken = phydev->eee_broken_modes;
1523 int old_adv, adv;
jbrunetd853d142016-11-28 10:46:46 +01001524
1525 /* Nothing to disable */
1526 if (!broken)
1527 return 0;
1528
1529 /* If the following call fails, we assume that EEE is not
1530 * supported by the phy. If we read 0, EEE is not advertised
1531 * In both case, we don't need to continue
1532 */
Russell Kinga6d99fc2017-03-21 16:36:53 +00001533 adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
jbrunetd853d142016-11-28 10:46:46 +01001534 if (adv <= 0)
1535 return 0;
1536
1537 old_adv = adv;
1538 adv &= ~broken;
1539
1540 /* Advertising remains unchanged with the broken mask */
1541 if (old_adv == adv)
1542 return 0;
1543
Russell Kinga6d99fc2017-03-21 16:36:53 +00001544 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
jbrunetd853d142016-11-28 10:46:46 +01001545
1546 return 1;
1547}
1548
1549/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001550 * genphy_setup_forced - configures/forces speed/duplex from @phydev
1551 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001552 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001553 * Description: Configures MII_BMCR to force speed/duplex
Andy Fleming00db8182005-07-30 19:31:23 -04001554 * to the values in phydev. Assumes that the values are valid.
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001555 * Please see phy_sanitize_settings().
1556 */
Madalin Bucur3fb69bc2013-11-20 16:38:19 -06001557int genphy_setup_forced(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -04001558{
Russell Kingfea23fb2018-01-02 10:58:58 +00001559 u16 ctl = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001560
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001561 phydev->pause = 0;
1562 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001563
1564 if (SPEED_1000 == phydev->speed)
1565 ctl |= BMCR_SPEED1000;
1566 else if (SPEED_100 == phydev->speed)
1567 ctl |= BMCR_SPEED100;
1568
1569 if (DUPLEX_FULL == phydev->duplex)
1570 ctl |= BMCR_FULLDPLX;
Florian Fainellie1093742013-12-17 21:38:12 -08001571
Russell Kingfea23fb2018-01-02 10:58:58 +00001572 return phy_modify(phydev, MII_BMCR,
Ingo van Lil18a5b052018-02-12 12:02:52 +01001573 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
Andy Fleming00db8182005-07-30 19:31:23 -04001574}
Madalin Bucur3fb69bc2013-11-20 16:38:19 -06001575EXPORT_SYMBOL(genphy_setup_forced);
Andy Fleming00db8182005-07-30 19:31:23 -04001576
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001577/**
1578 * genphy_restart_aneg - Enable and Restart Autonegotiation
1579 * @phydev: target phy_device struct
1580 */
Andy Fleming00db8182005-07-30 19:31:23 -04001581int genphy_restart_aneg(struct phy_device *phydev)
1582{
Andy Fleming00db8182005-07-30 19:31:23 -04001583 /* Don't isolate the PHY if we're negotiating */
Russell Kingf1028522018-01-05 16:07:10 +00001584 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
Russell Kingfea23fb2018-01-02 10:58:58 +00001585 BMCR_ANENABLE | BMCR_ANRESTART);
Andy Fleming00db8182005-07-30 19:31:23 -04001586}
Adrian Bunk892871d2008-10-13 18:48:09 -07001587EXPORT_SYMBOL(genphy_restart_aneg);
Andy Fleming00db8182005-07-30 19:31:23 -04001588
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001589/**
1590 * genphy_config_aneg - restart auto-negotiation or write BMCR
1591 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001592 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001593 * Description: If auto-negotiation is enabled, we configure the
Andy Fleming00db8182005-07-30 19:31:23 -04001594 * advertising, and then restart auto-negotiation. If it is not
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001595 * enabled, then we write the BMCR.
Andy Fleming00db8182005-07-30 19:31:23 -04001596 */
1597int genphy_config_aneg(struct phy_device *phydev)
1598{
jbrunetd853d142016-11-28 10:46:46 +01001599 int err, changed;
1600
1601 changed = genphy_config_eee_advert(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04001602
Trent Piephode339c22008-11-19 15:52:41 -08001603 if (AUTONEG_ENABLE != phydev->autoneg)
1604 return genphy_setup_forced(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04001605
jbrunetd853d142016-11-28 10:46:46 +01001606 err = genphy_config_advert(phydev);
1607 if (err < 0) /* error */
1608 return err;
1609
1610 changed |= err;
1611
1612 if (changed == 0) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001613 /* Advertisement hasn't changed, but maybe aneg was never on to
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001614 * begin with? Or maybe phy was isolated?
1615 */
Trent Piephode339c22008-11-19 15:52:41 -08001616 int ctl = phy_read(phydev, MII_BMCR);
1617
1618 if (ctl < 0)
1619 return ctl;
1620
1621 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
jbrunetd853d142016-11-28 10:46:46 +01001622 changed = 1; /* do restart aneg */
Trent Piephode339c22008-11-19 15:52:41 -08001623 }
1624
1625 /* Only restart aneg if we are advertising something different
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001626 * than we were before.
1627 */
jbrunetd853d142016-11-28 10:46:46 +01001628 if (changed > 0)
1629 return genphy_restart_aneg(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04001630
jbrunetd853d142016-11-28 10:46:46 +01001631 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001632}
1633EXPORT_SYMBOL(genphy_config_aneg);
1634
Florian Fainellia9fa6e62014-02-11 17:27:36 -08001635/**
1636 * genphy_aneg_done - return auto-negotiation status
1637 * @phydev: target phy_device struct
1638 *
1639 * Description: Reads the status register and returns 0 either if
1640 * auto-negotiation is incomplete, or if there was an error.
1641 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
1642 */
1643int genphy_aneg_done(struct phy_device *phydev)
1644{
1645 int retval = phy_read(phydev, MII_BMSR);
1646
1647 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1648}
1649EXPORT_SYMBOL(genphy_aneg_done);
1650
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001651/**
1652 * genphy_update_link - update link status in @phydev
1653 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001654 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001655 * Description: Update the value in phydev->link to reflect the
Andy Fleming00db8182005-07-30 19:31:23 -04001656 * current link value. In order to do this, we need to read
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001657 * the status register twice, keeping the second value.
Andy Fleming00db8182005-07-30 19:31:23 -04001658 */
1659int genphy_update_link(struct phy_device *phydev)
1660{
1661 int status;
1662
1663 /* Do a fake read */
1664 status = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -04001665 if (status < 0)
1666 return status;
1667
1668 /* Read link and autonegotiation status */
1669 status = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -04001670 if (status < 0)
1671 return status;
1672
1673 if ((status & BMSR_LSTATUS) == 0)
1674 phydev->link = 0;
1675 else
1676 phydev->link = 1;
1677
1678 return 0;
1679}
Andy Fleming6b655522006-10-16 16:19:17 -05001680EXPORT_SYMBOL(genphy_update_link);
Andy Fleming00db8182005-07-30 19:31:23 -04001681
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001682/**
1683 * genphy_read_status - check the link status and update current link state
1684 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001685 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001686 * Description: Check the link, then figure out the current state
Andy Fleming00db8182005-07-30 19:31:23 -04001687 * by comparing what we advertise with what the link partner
1688 * advertises. Start by checking the gigabit possibilities,
1689 * then move on to 10/100.
1690 */
1691int genphy_read_status(struct phy_device *phydev)
1692{
1693 int adv;
1694 int err;
1695 int lpa;
1696 int lpagb = 0;
Cristian Bercarua4572e02014-02-25 10:22:48 +02001697 int common_adv;
1698 int common_adv_gb = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001699
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001700 /* Update the link, but return if there was an error */
Andy Fleming00db8182005-07-30 19:31:23 -04001701 err = genphy_update_link(phydev);
1702 if (err)
1703 return err;
1704
Andrew Lunnc0ec3c22018-11-10 23:43:34 +01001705 linkmode_zero(phydev->lp_advertising);
Florian Fainelli114002b2013-12-06 13:01:30 -08001706
Andy Fleming00db8182005-07-30 19:31:23 -04001707 if (AUTONEG_ENABLE == phydev->autoneg) {
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001708 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1709 phydev->supported) ||
1710 linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1711 phydev->supported)) {
Andy Fleming00db8182005-07-30 19:31:23 -04001712 lpagb = phy_read(phydev, MII_STAT1000);
Andy Fleming00db8182005-07-30 19:31:23 -04001713 if (lpagb < 0)
1714 return lpagb;
1715
1716 adv = phy_read(phydev, MII_CTRL1000);
Andy Fleming00db8182005-07-30 19:31:23 -04001717 if (adv < 0)
1718 return adv;
1719
Heiner Kallweitb8f8c8e2018-07-21 15:48:47 +02001720 if (lpagb & LPA_1000MSFAIL) {
1721 if (adv & CTL1000_ENABLE_MASTER)
1722 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
1723 else
1724 phydev_err(phydev, "Master/Slave resolution failed\n");
1725 return -ENOLINK;
1726 }
1727
Andrew Lunnc0ec3c22018-11-10 23:43:34 +01001728 mii_stat1000_to_linkmode_lpa_t(phydev->lp_advertising,
1729 lpagb);
Cristian Bercarua4572e02014-02-25 10:22:48 +02001730 common_adv_gb = lpagb & adv << 2;
Andy Fleming00db8182005-07-30 19:31:23 -04001731 }
1732
1733 lpa = phy_read(phydev, MII_LPA);
Andy Fleming00db8182005-07-30 19:31:23 -04001734 if (lpa < 0)
1735 return lpa;
1736
Andrew Lunnc0ec3c22018-11-10 23:43:34 +01001737 mii_lpa_to_linkmode_lpa_t(phydev->lp_advertising, lpa);
Florian Fainelli114002b2013-12-06 13:01:30 -08001738
Andy Fleming00db8182005-07-30 19:31:23 -04001739 adv = phy_read(phydev, MII_ADVERTISE);
Andy Fleming00db8182005-07-30 19:31:23 -04001740 if (adv < 0)
1741 return adv;
1742
Cristian Bercarua4572e02014-02-25 10:22:48 +02001743 common_adv = lpa & adv;
Andy Fleming00db8182005-07-30 19:31:23 -04001744
1745 phydev->speed = SPEED_10;
1746 phydev->duplex = DUPLEX_HALF;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001747 phydev->pause = 0;
1748 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001749
Cristian Bercarua4572e02014-02-25 10:22:48 +02001750 if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) {
Andy Fleming00db8182005-07-30 19:31:23 -04001751 phydev->speed = SPEED_1000;
1752
Cristian Bercarua4572e02014-02-25 10:22:48 +02001753 if (common_adv_gb & LPA_1000FULL)
Andy Fleming00db8182005-07-30 19:31:23 -04001754 phydev->duplex = DUPLEX_FULL;
Cristian Bercarua4572e02014-02-25 10:22:48 +02001755 } else if (common_adv & (LPA_100FULL | LPA_100HALF)) {
Andy Fleming00db8182005-07-30 19:31:23 -04001756 phydev->speed = SPEED_100;
Florian Fainellie1093742013-12-17 21:38:12 -08001757
Cristian Bercarua4572e02014-02-25 10:22:48 +02001758 if (common_adv & LPA_100FULL)
Andy Fleming00db8182005-07-30 19:31:23 -04001759 phydev->duplex = DUPLEX_FULL;
1760 } else
Cristian Bercarua4572e02014-02-25 10:22:48 +02001761 if (common_adv & LPA_10FULL)
Andy Fleming00db8182005-07-30 19:31:23 -04001762 phydev->duplex = DUPLEX_FULL;
1763
Florian Fainellie1093742013-12-17 21:38:12 -08001764 if (phydev->duplex == DUPLEX_FULL) {
Andy Fleming00db8182005-07-30 19:31:23 -04001765 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
1766 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
1767 }
1768 } else {
1769 int bmcr = phy_read(phydev, MII_BMCR);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001770
Andy Fleming00db8182005-07-30 19:31:23 -04001771 if (bmcr < 0)
1772 return bmcr;
1773
1774 if (bmcr & BMCR_FULLDPLX)
1775 phydev->duplex = DUPLEX_FULL;
1776 else
1777 phydev->duplex = DUPLEX_HALF;
1778
1779 if (bmcr & BMCR_SPEED1000)
1780 phydev->speed = SPEED_1000;
1781 else if (bmcr & BMCR_SPEED100)
1782 phydev->speed = SPEED_100;
1783 else
1784 phydev->speed = SPEED_10;
1785
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001786 phydev->pause = 0;
1787 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001788 }
1789
1790 return 0;
1791}
1792EXPORT_SYMBOL(genphy_read_status);
1793
Florian Fainelli797ac072014-02-17 13:34:02 -08001794/**
1795 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
1796 * @phydev: target phy_device struct
1797 *
1798 * Description: Perform a software PHY reset using the standard
1799 * BMCR_RESET bit and poll for the reset bit to be cleared.
1800 *
1801 * Returns: 0 on success, < 0 on failure
1802 */
1803int genphy_soft_reset(struct phy_device *phydev)
1804{
1805 int ret;
1806
1807 ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
1808 if (ret < 0)
1809 return ret;
1810
1811 return phy_poll_reset(phydev);
1812}
1813EXPORT_SYMBOL(genphy_soft_reset);
1814
Daniel Mackaf6b6962014-04-16 17:19:12 +02001815int genphy_config_init(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -04001816{
Eric Sesterhenn84c22d72006-09-25 16:39:22 -07001817 int val;
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001818 __ETHTOOL_DECLARE_LINK_MODE_MASK(features) = { 0, };
Andy Fleming00db8182005-07-30 19:31:23 -04001819
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001820 linkmode_set_bit_array(phy_basic_ports_array,
1821 ARRAY_SIZE(phy_basic_ports_array),
1822 features);
1823 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, features);
1824 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, features);
Andy Fleming00db8182005-07-30 19:31:23 -04001825
1826 /* Do we support autonegotiation? */
1827 val = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -04001828 if (val < 0)
1829 return val;
1830
1831 if (val & BMSR_ANEGCAPABLE)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001832 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, features);
Andy Fleming00db8182005-07-30 19:31:23 -04001833
1834 if (val & BMSR_100FULL)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001835 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, features);
Andy Fleming00db8182005-07-30 19:31:23 -04001836 if (val & BMSR_100HALF)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001837 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, features);
Andy Fleming00db8182005-07-30 19:31:23 -04001838 if (val & BMSR_10FULL)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001839 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, features);
Andy Fleming00db8182005-07-30 19:31:23 -04001840 if (val & BMSR_10HALF)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001841 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, features);
Andy Fleming00db8182005-07-30 19:31:23 -04001842
1843 if (val & BMSR_ESTATEN) {
1844 val = phy_read(phydev, MII_ESTATUS);
Andy Fleming00db8182005-07-30 19:31:23 -04001845 if (val < 0)
1846 return val;
1847
1848 if (val & ESTATUS_1000_TFULL)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001849 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1850 features);
Andy Fleming00db8182005-07-30 19:31:23 -04001851 if (val & ESTATUS_1000_THALF)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001852 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1853 features);
Andy Fleming00db8182005-07-30 19:31:23 -04001854 }
1855
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001856 linkmode_and(phydev->supported, phydev->supported, features);
1857 linkmode_and(phydev->advertising, phydev->advertising, features);
Andy Fleming00db8182005-07-30 19:31:23 -04001858
1859 return 0;
1860}
Uwe Kleine-Königa0a32d32017-05-24 00:26:07 +02001861EXPORT_SYMBOL(genphy_config_init);
Andy Fleming124059f2014-01-10 14:27:37 +08001862
Kevin Hao5df7af82018-03-20 09:44:52 +08001863/* This is used for the phy device which doesn't support the MMD extended
1864 * register access, but it does have side effect when we are trying to access
1865 * the MMD register via indirect method.
1866 */
1867int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
1868{
1869 return -EOPNOTSUPP;
1870}
1871EXPORT_SYMBOL(genphy_read_mmd_unsupported);
1872
1873int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
1874 u16 regnum, u16 val)
1875{
1876 return -EOPNOTSUPP;
1877}
1878EXPORT_SYMBOL(genphy_write_mmd_unsupported);
1879
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001880int genphy_suspend(struct phy_device *phydev)
1881{
Heiner Kallweit032f4702018-01-12 21:20:36 +01001882 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001883}
1884EXPORT_SYMBOL(genphy_suspend);
1885
1886int genphy_resume(struct phy_device *phydev)
1887{
Heiner Kallweit032f4702018-01-12 21:20:36 +01001888 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001889}
1890EXPORT_SYMBOL(genphy_resume);
Andy Fleming00db8182005-07-30 19:31:23 -04001891
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08001892int genphy_loopback(struct phy_device *phydev, bool enable)
1893{
Russell Kingf1028522018-01-05 16:07:10 +00001894 return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
Russell Kingfea23fb2018-01-02 10:58:58 +00001895 enable ? BMCR_LOOPBACK : 0);
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08001896}
1897EXPORT_SYMBOL(genphy_loopback);
1898
Simon Hormanf3a6bd32015-09-30 15:15:52 +09001899static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
1900{
Simon Hormanf3a6bd32015-09-30 15:15:52 +09001901 switch (max_speed) {
Heiner Kallweit6915bf32018-12-03 08:04:57 +01001902 case SPEED_10:
1903 linkmode_clear_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
1904 phydev->supported);
1905 linkmode_clear_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1906 phydev->supported);
Simon Hormanf3a6bd32015-09-30 15:15:52 +09001907 /* fall through */
1908 case SPEED_100:
Heiner Kallweit6915bf32018-12-03 08:04:57 +01001909 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1910 phydev->supported);
1911 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1912 phydev->supported);
1913 break;
1914 case SPEED_1000:
1915 break;
1916 default:
1917 return -ENOTSUPP;
Simon Hormanf3a6bd32015-09-30 15:15:52 +09001918 }
1919
1920 return 0;
1921}
1922
1923int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
1924{
1925 int err;
1926
1927 err = __set_phy_supported(phydev, max_speed);
1928 if (err)
1929 return err;
1930
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001931 linkmode_copy(phydev->advertising, phydev->supported);
Simon Hormanf3a6bd32015-09-30 15:15:52 +09001932
1933 return 0;
1934}
1935EXPORT_SYMBOL(phy_set_max_speed);
1936
Andrew Lunn41124fa2018-09-12 01:53:14 +02001937/**
1938 * phy_remove_link_mode - Remove a supported link mode
1939 * @phydev: phy_device structure to remove link mode from
1940 * @link_mode: Link mode to be removed
1941 *
1942 * Description: Some MACs don't support all link modes which the PHY
1943 * does. e.g. a 1G MAC often does not support 1000Half. Add a helper
1944 * to remove a link mode.
1945 */
1946void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
1947{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001948 linkmode_clear_bit(link_mode, phydev->supported);
1949 linkmode_copy(phydev->advertising, phydev->supported);
Andrew Lunn41124fa2018-09-12 01:53:14 +02001950}
1951EXPORT_SYMBOL(phy_remove_link_mode);
1952
Andrew Lunnaf8d9bb2018-09-12 01:53:15 +02001953/**
Andrew Lunnc306ad32018-09-12 01:53:16 +02001954 * phy_support_sym_pause - Enable support of symmetrical pause
1955 * @phydev: target phy_device struct
1956 *
1957 * Description: Called by the MAC to indicate is supports symmetrical
1958 * Pause, but not asym pause.
1959 */
1960void phy_support_sym_pause(struct phy_device *phydev)
1961{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001962 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
1963 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
1964 linkmode_copy(phydev->advertising, phydev->supported);
Andrew Lunnc306ad32018-09-12 01:53:16 +02001965}
1966EXPORT_SYMBOL(phy_support_sym_pause);
1967
1968/**
Andrew Lunnaf8d9bb2018-09-12 01:53:15 +02001969 * phy_support_asym_pause - Enable support of asym pause
1970 * @phydev: target phy_device struct
1971 *
1972 * Description: Called by the MAC to indicate is supports Asym Pause.
1973 */
1974void phy_support_asym_pause(struct phy_device *phydev)
1975{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001976 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
1977 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
1978 linkmode_copy(phydev->advertising, phydev->supported);
Andrew Lunnaf8d9bb2018-09-12 01:53:15 +02001979}
1980EXPORT_SYMBOL(phy_support_asym_pause);
1981
Andrew Lunn70814e82018-09-12 01:53:17 +02001982/**
Andrew Lunn0c122402018-09-12 01:53:18 +02001983 * phy_set_sym_pause - Configure symmetric Pause
1984 * @phydev: target phy_device struct
1985 * @rx: Receiver Pause is supported
1986 * @tx: Transmit Pause is supported
1987 * @autoneg: Auto neg should be used
1988 *
1989 * Description: Configure advertised Pause support depending on if
1990 * receiver pause and pause auto neg is supported. Generally called
1991 * from the set_pauseparam .ndo.
1992 */
1993void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
1994 bool autoneg)
1995{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001996 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
Andrew Lunn0c122402018-09-12 01:53:18 +02001997
1998 if (rx && tx && autoneg)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001999 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2000 phydev->supported);
Andrew Lunn0c122402018-09-12 01:53:18 +02002001
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002002 linkmode_copy(phydev->advertising, phydev->supported);
Andrew Lunn0c122402018-09-12 01:53:18 +02002003}
2004EXPORT_SYMBOL(phy_set_sym_pause);
2005
2006/**
Andrew Lunn70814e82018-09-12 01:53:17 +02002007 * phy_set_asym_pause - Configure Pause and Asym Pause
2008 * @phydev: target phy_device struct
2009 * @rx: Receiver Pause is supported
2010 * @tx: Transmit Pause is supported
2011 *
2012 * Description: Configure advertised Pause support depending on if
2013 * transmit and receiver pause is supported. If there has been a
2014 * change in adverting, trigger a new autoneg. Generally called from
2015 * the set_pauseparam .ndo.
2016 */
2017void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
2018{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002019 __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
Andrew Lunn70814e82018-09-12 01:53:17 +02002020
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002021 linkmode_copy(oldadv, phydev->advertising);
Andrew Lunn70814e82018-09-12 01:53:17 +02002022
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002023 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2024 phydev->advertising);
2025 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2026 phydev->advertising);
Andrew Lunn70814e82018-09-12 01:53:17 +02002027
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002028 if (rx) {
2029 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2030 phydev->advertising);
2031 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2032 phydev->advertising);
Andrew Lunn70814e82018-09-12 01:53:17 +02002033 }
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002034
2035 if (tx)
2036 linkmode_change_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2037 phydev->advertising);
2038
2039 if (!linkmode_equal(oldadv, phydev->advertising) &&
2040 phydev->autoneg)
2041 phy_start_aneg(phydev);
Andrew Lunn70814e82018-09-12 01:53:17 +02002042}
2043EXPORT_SYMBOL(phy_set_asym_pause);
2044
Andrew Lunn22b7d292018-09-12 01:53:19 +02002045/**
2046 * phy_validate_pause - Test if the PHY/MAC support the pause configuration
2047 * @phydev: phy_device struct
2048 * @pp: requested pause configuration
2049 *
2050 * Description: Test if the PHY/MAC combination supports the Pause
2051 * configuration the user is requesting. Returns True if it is
2052 * supported, false otherwise.
2053 */
2054bool phy_validate_pause(struct phy_device *phydev,
2055 struct ethtool_pauseparam *pp)
2056{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002057 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2058 phydev->supported) ||
2059 (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2060 phydev->supported) &&
Andrew Lunn22b7d292018-09-12 01:53:19 +02002061 pp->rx_pause != pp->tx_pause))
2062 return false;
2063 return true;
2064}
2065EXPORT_SYMBOL(phy_validate_pause);
2066
Sascha Hauerde906af2014-05-21 15:29:45 +02002067static void of_set_phy_supported(struct phy_device *phydev)
2068{
Andrew Lunne5a03bf2016-01-06 20:11:16 +01002069 struct device_node *node = phydev->mdio.dev.of_node;
Sascha Hauerde906af2014-05-21 15:29:45 +02002070 u32 max_speed;
2071
2072 if (!IS_ENABLED(CONFIG_OF_MDIO))
2073 return;
2074
2075 if (!node)
2076 return;
2077
Simon Hormanf3a6bd32015-09-30 15:15:52 +09002078 if (!of_property_read_u32(node, "max-speed", &max_speed))
2079 __set_phy_supported(phydev, max_speed);
Sascha Hauerde906af2014-05-21 15:29:45 +02002080}
2081
jbrunetd853d142016-11-28 10:46:46 +01002082static void of_set_phy_eee_broken(struct phy_device *phydev)
2083{
2084 struct device_node *node = phydev->mdio.dev.of_node;
jbrunet57f39862016-12-19 16:05:37 +01002085 u32 broken = 0;
jbrunetd853d142016-11-28 10:46:46 +01002086
2087 if (!IS_ENABLED(CONFIG_OF_MDIO))
2088 return;
2089
2090 if (!node)
2091 return;
2092
jbrunet57f39862016-12-19 16:05:37 +01002093 if (of_property_read_bool(node, "eee-broken-100tx"))
2094 broken |= MDIO_EEE_100TX;
2095 if (of_property_read_bool(node, "eee-broken-1000t"))
2096 broken |= MDIO_EEE_1000T;
2097 if (of_property_read_bool(node, "eee-broken-10gt"))
2098 broken |= MDIO_EEE_10GT;
2099 if (of_property_read_bool(node, "eee-broken-1000kx"))
2100 broken |= MDIO_EEE_1000KX;
2101 if (of_property_read_bool(node, "eee-broken-10gkx4"))
2102 broken |= MDIO_EEE_10GKX4;
2103 if (of_property_read_bool(node, "eee-broken-10gkr"))
2104 broken |= MDIO_EEE_10GKR;
2105
2106 phydev->eee_broken_modes = broken;
jbrunetd853d142016-11-28 10:46:46 +01002107}
2108
Heiner Kallweit0d2e7782018-11-09 18:16:28 +01002109static bool phy_drv_supports_irq(struct phy_driver *phydrv)
2110{
Heiner Kallweita21ff3c2018-11-12 21:16:06 +01002111 return phydrv->config_intr && phydrv->ack_interrupt;
Heiner Kallweit0d2e7782018-11-09 18:16:28 +01002112}
2113
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002114/**
2115 * phy_probe - probe and init a PHY device
2116 * @dev: device to probe and init
Andy Fleming00db8182005-07-30 19:31:23 -04002117 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002118 * Description: Take care of setting up the phy_device structure,
Andy Fleming00db8182005-07-30 19:31:23 -04002119 * set the state to READY (the driver's init function should
2120 * set it to STARTING if needed).
2121 */
2122static int phy_probe(struct device *dev)
2123{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03002124 struct phy_device *phydev = to_phy_device(dev);
Andrew Lunne5a03bf2016-01-06 20:11:16 +01002125 struct device_driver *drv = phydev->mdio.dev.driver;
Sergei Shtylyov553fe922014-01-05 03:23:19 +03002126 struct phy_driver *phydrv = to_phy_driver(drv);
Andrew Lunn719655a2018-09-29 23:04:16 +02002127 u32 features;
Andy Fleming00db8182005-07-30 19:31:23 -04002128 int err = 0;
2129
Andy Fleming00db8182005-07-30 19:31:23 -04002130 phydev->drv = phydrv;
2131
Florian Fainelli2c7b4922013-05-19 22:53:42 +00002132 /* Disable the interrupt if the PHY doesn't support it
2133 * but the interrupt is still a valid one
2134 */
Heiner Kallweit0d2e7782018-11-09 18:16:28 +01002135 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
Andy Fleming00db8182005-07-30 19:31:23 -04002136 phydev->irq = PHY_POLL;
2137
Florian Fainelli4284b6a2013-05-23 01:11:12 +00002138 if (phydrv->flags & PHY_IS_INTERNAL)
2139 phydev->is_internal = true;
2140
Nate Case35b5f6b2008-01-29 10:05:09 -06002141 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04002142
2143 /* Start out supporting everything. Eventually,
2144 * a controller will attach, and may modify one
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03002145 * or both of these values
2146 */
Andrew Lunn719655a2018-09-29 23:04:16 +02002147 ethtool_convert_link_mode_to_legacy_u32(&features, phydrv->features);
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002148 linkmode_copy(phydev->supported, phydrv->features);
Sascha Hauerde906af2014-05-21 15:29:45 +02002149 of_set_phy_supported(phydev);
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002150 linkmode_copy(phydev->advertising, phydev->supported);
Andy Fleming00db8182005-07-30 19:31:23 -04002151
jbrunetd853d142016-11-28 10:46:46 +01002152 /* Get the EEE modes we want to prohibit. We will ask
2153 * the PHY stop advertising these mode later on
2154 */
2155 of_set_phy_eee_broken(phydev);
2156
Timur Tabi529ed122016-12-07 13:20:51 -06002157 /* The Pause Frame bits indicate that the PHY can support passing
2158 * pause frames. During autonegotiation, the PHYs will determine if
2159 * they should allow pause frames to pass. The MAC driver should then
2160 * use that result to determine whether to enable flow control via
2161 * pause frames.
2162 *
2163 * Normally, PHY drivers should not set the Pause bits, and instead
2164 * allow phylib to do that. However, there may be some situations
2165 * (e.g. hardware erratum) where the driver wants to set only one
2166 * of these bits.
2167 */
Andrew Lunn719655a2018-09-29 23:04:16 +02002168 if (test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydrv->features) ||
2169 test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydrv->features)) {
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002170 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2171 phydev->supported);
2172 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2173 phydev->supported);
Andrew Lunn719655a2018-09-29 23:04:16 +02002174 if (test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydrv->features))
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002175 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2176 phydev->supported);
Andrew Lunn719655a2018-09-29 23:04:16 +02002177 if (test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2178 phydrv->features))
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002179 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2180 phydev->supported);
Timur Tabi529ed122016-12-07 13:20:51 -06002181 } else {
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002182 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2183 phydev->supported);
2184 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2185 phydev->supported);
Timur Tabi529ed122016-12-07 13:20:51 -06002186 }
2187
Andy Fleming00db8182005-07-30 19:31:23 -04002188 /* Set the state to READY by default */
2189 phydev->state = PHY_READY;
2190
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01002191 if (phydev->drv->probe) {
2192 /* Deassert the reset signal */
2193 phy_device_reset(phydev, 0);
2194
Andy Fleming00db8182005-07-30 19:31:23 -04002195 err = phydev->drv->probe(phydev);
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01002196 if (err) {
2197 /* Assert the reset signal */
2198 phy_device_reset(phydev, 1);
2199 }
2200 }
Andy Fleming00db8182005-07-30 19:31:23 -04002201
Nate Case35b5f6b2008-01-29 10:05:09 -06002202 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04002203
Andy Fleming00db8182005-07-30 19:31:23 -04002204 return err;
2205}
2206
2207static int phy_remove(struct device *dev)
2208{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03002209 struct phy_device *phydev = to_phy_device(dev);
Andy Fleming00db8182005-07-30 19:31:23 -04002210
Florian Fainelli7b9a88a2017-02-17 16:07:33 -08002211 cancel_delayed_work_sync(&phydev->state_queue);
2212
Nate Case35b5f6b2008-01-29 10:05:09 -06002213 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04002214 phydev->state = PHY_DOWN;
Nate Case35b5f6b2008-01-29 10:05:09 -06002215 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04002216
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01002217 if (phydev->drv && phydev->drv->remove) {
Andy Fleming00db8182005-07-30 19:31:23 -04002218 phydev->drv->remove(phydev);
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01002219
2220 /* Assert the reset signal */
2221 phy_device_reset(phydev, 1);
2222 }
Andy Fleming00db8182005-07-30 19:31:23 -04002223 phydev->drv = NULL;
2224
2225 return 0;
2226}
2227
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002228/**
2229 * phy_driver_register - register a phy_driver with the PHY layer
2230 * @new_driver: new phy_driver to register
Andrew Lunnbe01da72016-01-06 20:11:22 +01002231 * @owner: module owning this PHY
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002232 */
Andrew Lunnbe01da72016-01-06 20:11:22 +01002233int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
Andy Fleming00db8182005-07-30 19:31:23 -04002234{
2235 int retval;
2236
Andrew Lunna9049e02016-01-06 20:11:26 +01002237 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
2238 new_driver->mdiodrv.driver.name = new_driver->name;
2239 new_driver->mdiodrv.driver.bus = &mdio_bus_type;
2240 new_driver->mdiodrv.driver.probe = phy_probe;
2241 new_driver->mdiodrv.driver.remove = phy_remove;
2242 new_driver->mdiodrv.driver.owner = owner;
Andy Fleming00db8182005-07-30 19:31:23 -04002243
Heiner Kallweitc85ddec2018-11-23 19:41:29 +01002244 /* The following works around an issue where the PHY driver doesn't bind
2245 * to the device, resulting in the genphy driver being used instead of
2246 * the dedicated driver. The root cause of the issue isn't known yet
2247 * and seems to be in the base driver core. Once this is fixed we may
2248 * remove this workaround.
2249 */
2250 new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
2251
Andrew Lunna9049e02016-01-06 20:11:26 +01002252 retval = driver_register(&new_driver->mdiodrv.driver);
Andy Fleming00db8182005-07-30 19:31:23 -04002253 if (retval) {
Joe Perches8d242482012-06-09 07:49:07 +00002254 pr_err("%s: Error %d in registering driver\n",
2255 new_driver->name, retval);
Andy Fleming00db8182005-07-30 19:31:23 -04002256
2257 return retval;
2258 }
2259
Olof Johanssonf2511f12007-11-04 16:09:23 -06002260 pr_debug("%s: Registered new driver\n", new_driver->name);
Andy Fleming00db8182005-07-30 19:31:23 -04002261
2262 return 0;
2263}
2264EXPORT_SYMBOL(phy_driver_register);
2265
Andrew Lunnbe01da72016-01-06 20:11:22 +01002266int phy_drivers_register(struct phy_driver *new_driver, int n,
2267 struct module *owner)
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00002268{
2269 int i, ret = 0;
2270
2271 for (i = 0; i < n; i++) {
Andrew Lunnbe01da72016-01-06 20:11:22 +01002272 ret = phy_driver_register(new_driver + i, owner);
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00002273 if (ret) {
2274 while (i-- > 0)
2275 phy_driver_unregister(new_driver + i);
2276 break;
2277 }
2278 }
2279 return ret;
2280}
2281EXPORT_SYMBOL(phy_drivers_register);
2282
Andy Fleming00db8182005-07-30 19:31:23 -04002283void phy_driver_unregister(struct phy_driver *drv)
2284{
Andrew Lunna9049e02016-01-06 20:11:26 +01002285 driver_unregister(&drv->mdiodrv.driver);
Andy Fleming00db8182005-07-30 19:31:23 -04002286}
2287EXPORT_SYMBOL(phy_driver_unregister);
2288
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00002289void phy_drivers_unregister(struct phy_driver *drv, int n)
2290{
2291 int i;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03002292
2293 for (i = 0; i < n; i++)
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00002294 phy_driver_unregister(drv + i);
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00002295}
2296EXPORT_SYMBOL(phy_drivers_unregister);
2297
Russell King921690f2017-06-05 12:23:05 +01002298static struct phy_driver genphy_driver = {
Andy Fleminge1393452005-08-24 18:46:21 -05002299 .phy_id = 0xffffffff,
2300 .phy_id_mask = 0xffffffff,
2301 .name = "Generic PHY",
Florian Fainelli0878fff2017-03-05 12:34:49 -08002302 .soft_reset = genphy_no_soft_reset,
Andy Fleminge1393452005-08-24 18:46:21 -05002303 .config_init = genphy_config_init,
Andrew Lunn719655a2018-09-29 23:04:16 +02002304 .features = PHY_GBIT_ALL_PORTS_FEATURES,
Florian Fainelli76a423a2014-02-11 17:27:37 -08002305 .aneg_done = genphy_aneg_done,
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08002306 .suspend = genphy_suspend,
2307 .resume = genphy_resume,
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08002308 .set_loopback = genphy_loopback,
Russell King921690f2017-06-05 12:23:05 +01002309};
Andy Fleming00db8182005-07-30 19:31:23 -04002310
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002311static int __init phy_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -04002312{
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002313 int rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002314
2315 rc = mdio_bus_init();
2316 if (rc)
Andy Fleminge1393452005-08-24 18:46:21 -05002317 return rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002318
Andrew Lunn719655a2018-09-29 23:04:16 +02002319 features_init();
2320
Russell King921690f2017-06-05 12:23:05 +01002321 rc = phy_driver_register(&genphy_10g_driver, THIS_MODULE);
Andy Fleminge1393452005-08-24 18:46:21 -05002322 if (rc)
Russell King921690f2017-06-05 12:23:05 +01002323 goto err_10g;
2324
2325 rc = phy_driver_register(&genphy_driver, THIS_MODULE);
2326 if (rc) {
2327 phy_driver_unregister(&genphy_10g_driver);
2328err_10g:
Andy Fleminge1393452005-08-24 18:46:21 -05002329 mdio_bus_exit();
Russell King921690f2017-06-05 12:23:05 +01002330 }
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002331
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002332 return rc;
Andy Fleming00db8182005-07-30 19:31:23 -04002333}
2334
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002335static void __exit phy_exit(void)
Andy Fleming00db8182005-07-30 19:31:23 -04002336{
Russell King921690f2017-06-05 12:23:05 +01002337 phy_driver_unregister(&genphy_10g_driver);
2338 phy_driver_unregister(&genphy_driver);
Andy Fleminge1393452005-08-24 18:46:21 -05002339 mdio_bus_exit();
Andy Fleming00db8182005-07-30 19:31:23 -04002340}
2341
Andy Fleminge1393452005-08-24 18:46:21 -05002342subsys_initcall(phy_init);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002343module_exit(phy_exit);