blob: 3d14e48aebc5140c8f1a297154b8a3f63858712b [file] [log] [blame]
Andrew Lunna2443fd2019-01-21 19:05:50 +01001// SPDX-License-Identifier: GPL-2.0+
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03002/* Framework for finding and configuring PHYs.
Andy Fleming00db8182005-07-30 19:31:23 -04003 * Also contains generic PHY driver
4 *
5 * Author: Andy Fleming
6 *
7 * Copyright (c) 2004 Freescale Semiconductor, Inc.
Andy Fleming00db8182005-07-30 19:31:23 -04008 */
Joe Perches8d242482012-06-09 07:49:07 +00009
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Andy Fleming00db8182005-07-30 19:31:23 -040012#include <linux/kernel.h>
Andy Fleming00db8182005-07-30 19:31:23 -040013#include <linux/string.h>
14#include <linux/errno.h>
15#include <linux/unistd.h>
16#include <linux/slab.h>
17#include <linux/interrupt.h>
18#include <linux/init.h>
19#include <linux/delay.h>
20#include <linux/netdevice.h>
21#include <linux/etherdevice.h>
22#include <linux/skbuff.h>
Andy Fleming00db8182005-07-30 19:31:23 -040023#include <linux/mm.h>
24#include <linux/module.h>
Andy Fleming00db8182005-07-30 19:31:23 -040025#include <linux/mii.h>
26#include <linux/ethtool.h>
Andrew Lunn719655a2018-09-29 23:04:16 +020027#include <linux/bitmap.h>
Andy Fleming00db8182005-07-30 19:31:23 -040028#include <linux/phy.h>
Zach Brown2e0bc452016-10-17 10:49:55 -050029#include <linux/phy_led_triggers.h>
Andy Fleming124059f2014-01-10 14:27:37 +080030#include <linux/mdio.h>
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030031#include <linux/io.h>
32#include <linux/uaccess.h>
Sascha Hauerde906af2014-05-21 15:29:45 +020033#include <linux/of.h>
Andy Fleming00db8182005-07-30 19:31:23 -040034
Olaf Heringafcceaa2005-12-14 00:33:49 +010035MODULE_DESCRIPTION("PHY library");
36MODULE_AUTHOR("Andy Fleming");
37MODULE_LICENSE("GPL");
38
Andrew Lunn719655a2018-09-29 23:04:16 +020039__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
40EXPORT_SYMBOL_GPL(phy_basic_features);
41
42__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
43EXPORT_SYMBOL_GPL(phy_basic_t1_features);
44
45__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
46EXPORT_SYMBOL_GPL(phy_gbit_features);
47
48__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
49EXPORT_SYMBOL_GPL(phy_gbit_fibre_features);
50
51__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
52EXPORT_SYMBOL_GPL(phy_gbit_all_ports_features);
53
54__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
55EXPORT_SYMBOL_GPL(phy_10gbit_features);
56
Andrew Lunn9e857a42019-01-15 16:55:30 +010057__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
58EXPORT_SYMBOL_GPL(phy_10gbit_fec_features);
59
Andrew Lunn719655a2018-09-29 23:04:16 +020060static const int phy_basic_ports_array[] = {
61 ETHTOOL_LINK_MODE_Autoneg_BIT,
62 ETHTOOL_LINK_MODE_TP_BIT,
63 ETHTOOL_LINK_MODE_MII_BIT,
64};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010065EXPORT_SYMBOL_GPL(phy_basic_ports_array);
Andrew Lunn719655a2018-09-29 23:04:16 +020066
67static const int phy_fibre_port_array[] = {
68 ETHTOOL_LINK_MODE_FIBRE_BIT,
69};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010070EXPORT_SYMBOL_GPL(phy_fibre_port_array);
Andrew Lunn719655a2018-09-29 23:04:16 +020071
72static const int phy_all_ports_features_array[] = {
73 ETHTOOL_LINK_MODE_Autoneg_BIT,
74 ETHTOOL_LINK_MODE_TP_BIT,
75 ETHTOOL_LINK_MODE_MII_BIT,
76 ETHTOOL_LINK_MODE_FIBRE_BIT,
77 ETHTOOL_LINK_MODE_AUI_BIT,
78 ETHTOOL_LINK_MODE_BNC_BIT,
79 ETHTOOL_LINK_MODE_Backplane_BIT,
80};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010081EXPORT_SYMBOL_GPL(phy_all_ports_features_array);
Andrew Lunn719655a2018-09-29 23:04:16 +020082
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010083const int phy_10_100_features_array[4] = {
Andrew Lunn719655a2018-09-29 23:04:16 +020084 ETHTOOL_LINK_MODE_10baseT_Half_BIT,
85 ETHTOOL_LINK_MODE_10baseT_Full_BIT,
86 ETHTOOL_LINK_MODE_100baseT_Half_BIT,
87 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
88};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010089EXPORT_SYMBOL_GPL(phy_10_100_features_array);
Andrew Lunn719655a2018-09-29 23:04:16 +020090
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010091const int phy_basic_t1_features_array[2] = {
Andrew Lunn719655a2018-09-29 23:04:16 +020092 ETHTOOL_LINK_MODE_TP_BIT,
93 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
94};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010095EXPORT_SYMBOL_GPL(phy_basic_t1_features_array);
Andrew Lunn719655a2018-09-29 23:04:16 +020096
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010097const int phy_gbit_features_array[2] = {
Andrew Lunn719655a2018-09-29 23:04:16 +020098 ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
99 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
100};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100101EXPORT_SYMBOL_GPL(phy_gbit_features_array);
Andrew Lunn719655a2018-09-29 23:04:16 +0200102
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100103const int phy_10gbit_features_array[1] = {
Andrew Lunn719655a2018-09-29 23:04:16 +0200104 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
105};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100106EXPORT_SYMBOL_GPL(phy_10gbit_features_array);
Andrew Lunn719655a2018-09-29 23:04:16 +0200107
Andrew Lunn9e857a42019-01-15 16:55:30 +0100108const int phy_10gbit_fec_features_array[1] = {
109 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
110};
111EXPORT_SYMBOL_GPL(phy_10gbit_fec_features_array);
112
Andrew Lunn719655a2018-09-29 23:04:16 +0200113__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
114EXPORT_SYMBOL_GPL(phy_10gbit_full_features);
115
116static const int phy_10gbit_full_features_array[] = {
117 ETHTOOL_LINK_MODE_10baseT_Full_BIT,
118 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
119 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
120 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
121};
122
123static void features_init(void)
124{
125 /* 10/100 half/full*/
126 linkmode_set_bit_array(phy_basic_ports_array,
127 ARRAY_SIZE(phy_basic_ports_array),
128 phy_basic_features);
129 linkmode_set_bit_array(phy_10_100_features_array,
130 ARRAY_SIZE(phy_10_100_features_array),
131 phy_basic_features);
132
133 /* 100 full, TP */
134 linkmode_set_bit_array(phy_basic_t1_features_array,
135 ARRAY_SIZE(phy_basic_t1_features_array),
136 phy_basic_t1_features);
137
138 /* 10/100 half/full + 1000 half/full */
139 linkmode_set_bit_array(phy_basic_ports_array,
140 ARRAY_SIZE(phy_basic_ports_array),
141 phy_gbit_features);
142 linkmode_set_bit_array(phy_10_100_features_array,
143 ARRAY_SIZE(phy_10_100_features_array),
144 phy_gbit_features);
145 linkmode_set_bit_array(phy_gbit_features_array,
146 ARRAY_SIZE(phy_gbit_features_array),
147 phy_gbit_features);
148
149 /* 10/100 half/full + 1000 half/full + fibre*/
150 linkmode_set_bit_array(phy_basic_ports_array,
151 ARRAY_SIZE(phy_basic_ports_array),
152 phy_gbit_fibre_features);
153 linkmode_set_bit_array(phy_10_100_features_array,
154 ARRAY_SIZE(phy_10_100_features_array),
155 phy_gbit_fibre_features);
156 linkmode_set_bit_array(phy_gbit_features_array,
157 ARRAY_SIZE(phy_gbit_features_array),
158 phy_gbit_fibre_features);
159 linkmode_set_bit_array(phy_fibre_port_array,
160 ARRAY_SIZE(phy_fibre_port_array),
161 phy_gbit_fibre_features);
162
163 /* 10/100 half/full + 1000 half/full + TP/MII/FIBRE/AUI/BNC/Backplane*/
164 linkmode_set_bit_array(phy_all_ports_features_array,
165 ARRAY_SIZE(phy_all_ports_features_array),
166 phy_gbit_all_ports_features);
167 linkmode_set_bit_array(phy_10_100_features_array,
168 ARRAY_SIZE(phy_10_100_features_array),
169 phy_gbit_all_ports_features);
170 linkmode_set_bit_array(phy_gbit_features_array,
171 ARRAY_SIZE(phy_gbit_features_array),
172 phy_gbit_all_ports_features);
173
174 /* 10/100 half/full + 1000 half/full + 10G full*/
175 linkmode_set_bit_array(phy_all_ports_features_array,
176 ARRAY_SIZE(phy_all_ports_features_array),
177 phy_10gbit_features);
178 linkmode_set_bit_array(phy_10_100_features_array,
179 ARRAY_SIZE(phy_10_100_features_array),
180 phy_10gbit_features);
181 linkmode_set_bit_array(phy_gbit_features_array,
182 ARRAY_SIZE(phy_gbit_features_array),
183 phy_10gbit_features);
184 linkmode_set_bit_array(phy_10gbit_features_array,
185 ARRAY_SIZE(phy_10gbit_features_array),
186 phy_10gbit_features);
187
188 /* 10/100/1000/10G full */
189 linkmode_set_bit_array(phy_all_ports_features_array,
190 ARRAY_SIZE(phy_all_ports_features_array),
191 phy_10gbit_full_features);
192 linkmode_set_bit_array(phy_10gbit_full_features_array,
193 ARRAY_SIZE(phy_10gbit_full_features_array),
194 phy_10gbit_full_features);
Andrew Lunn9e857a42019-01-15 16:55:30 +0100195 /* 10G FEC only */
196 linkmode_set_bit_array(phy_10gbit_fec_features_array,
197 ARRAY_SIZE(phy_10gbit_fec_features_array),
198 phy_10gbit_fec_features);
Andrew Lunn719655a2018-09-29 23:04:16 +0200199}
200
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300201void phy_device_free(struct phy_device *phydev)
202{
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100203 put_device(&phydev->mdio.dev);
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300204}
Grant Likely4dea5472009-04-25 12:52:46 +0000205EXPORT_SYMBOL(phy_device_free);
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300206
Andrew Lunn711fdba2016-01-06 20:11:27 +0100207static void phy_mdio_device_free(struct mdio_device *mdiodev)
208{
209 struct phy_device *phydev;
210
211 phydev = container_of(mdiodev, struct phy_device, mdio);
212 phy_device_free(phydev);
213}
214
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300215static void phy_device_release(struct device *dev)
216{
Petr Malatb2a43192013-02-28 01:01:52 +0000217 kfree(to_phy_device(dev));
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300218}
219
Andrew Lunn711fdba2016-01-06 20:11:27 +0100220static void phy_mdio_device_remove(struct mdio_device *mdiodev)
221{
222 struct phy_device *phydev;
223
224 phydev = container_of(mdiodev, struct phy_device, mdio);
225 phy_device_remove(phydev);
226}
227
Russell King921690f2017-06-05 12:23:05 +0100228static struct phy_driver genphy_driver;
229extern struct phy_driver genphy_10g_driver;
Grant Likely4dea5472009-04-25 12:52:46 +0000230
Andy Flemingf62220d2008-04-18 17:29:54 -0500231static LIST_HEAD(phy_fixup_list);
232static DEFINE_MUTEX(phy_fixup_lock);
233
Andrew Lunnbc879222016-01-06 20:11:21 +0100234#ifdef CONFIG_PM
235static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
236{
237 struct device_driver *drv = phydev->mdio.dev.driver;
238 struct phy_driver *phydrv = to_phy_driver(drv);
239 struct net_device *netdev = phydev->attached_dev;
240
241 if (!drv || !phydrv->suspend)
242 return false;
243
244 /* PHY not attached? May suspend if the PHY has not already been
245 * suspended as part of a prior call to phy_disconnect() ->
246 * phy_detach() -> phy_suspend() because the parent netdev might be the
247 * MDIO bus driver and clock gated at this point.
248 */
249 if (!netdev)
250 return !phydev->suspended;
251
Heiner Kallweit93f41e62018-09-24 22:01:32 +0200252 if (netdev->wol_enabled)
253 return false;
254
255 /* As long as not all affected network drivers support the
256 * wol_enabled flag, let's check for hints that WoL is enabled.
257 * Don't suspend PHY if the attached netdev parent may wake up.
Andrew Lunnbc879222016-01-06 20:11:21 +0100258 * The parent may point to a PCI device, as in tg3 driver.
259 */
260 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
261 return false;
262
263 /* Also don't suspend PHY if the netdev itself may wakeup. This
264 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
265 * e.g. SoC devices.
266 */
267 if (device_may_wakeup(&netdev->dev))
268 return false;
269
270 return true;
271}
272
273static int mdio_bus_phy_suspend(struct device *dev)
274{
275 struct phy_device *phydev = to_phy_device(dev);
276
277 /* We must stop the state machine manually, otherwise it stops out of
278 * control, possibly with the phydev->lock held. Upon resume, netdev
279 * may call phy routines that try to grab the same lock, and that may
280 * lead to a deadlock.
281 */
282 if (phydev->attached_dev && phydev->adjust_link)
283 phy_stop_machine(phydev);
284
285 if (!mdio_bus_phy_may_suspend(phydev))
286 return 0;
287
288 return phy_suspend(phydev);
289}
290
291static int mdio_bus_phy_resume(struct device *dev)
292{
293 struct phy_device *phydev = to_phy_device(dev);
294 int ret;
295
296 if (!mdio_bus_phy_may_suspend(phydev))
297 goto no_resume;
298
299 ret = phy_resume(phydev);
300 if (ret < 0)
301 return ret;
302
303no_resume:
304 if (phydev->attached_dev && phydev->adjust_link)
305 phy_start_machine(phydev);
306
307 return 0;
308}
309
310static int mdio_bus_phy_restore(struct device *dev)
311{
312 struct phy_device *phydev = to_phy_device(dev);
313 struct net_device *netdev = phydev->attached_dev;
314 int ret;
315
316 if (!netdev)
317 return 0;
318
319 ret = phy_init_hw(phydev);
320 if (ret < 0)
321 return ret;
322
Kunihiko Hayashi8742beb2018-12-18 16:57:04 +0900323 if (phydev->attached_dev && phydev->adjust_link)
324 phy_start_machine(phydev);
Andrew Lunnbc879222016-01-06 20:11:21 +0100325
326 return 0;
327}
328
329static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
330 .suspend = mdio_bus_phy_suspend,
331 .resume = mdio_bus_phy_resume,
332 .freeze = mdio_bus_phy_suspend,
333 .thaw = mdio_bus_phy_resume,
334 .restore = mdio_bus_phy_restore,
335};
336
337#define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
338
339#else
340
341#define MDIO_BUS_PHY_PM_OPS NULL
342
343#endif /* CONFIG_PM */
344
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300345/**
346 * phy_register_fixup - creates a new phy_fixup and adds it to the list
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100347 * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
Andy Flemingf62220d2008-04-18 17:29:54 -0500348 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300349 * It can also be PHY_ANY_UID
Andy Flemingf62220d2008-04-18 17:29:54 -0500350 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300351 * comparison
Andy Flemingf62220d2008-04-18 17:29:54 -0500352 * @run: The actual code to be run when a matching PHY is found
353 */
354int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300355 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -0500356{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300357 struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
Andy Flemingf62220d2008-04-18 17:29:54 -0500358
Andy Flemingf62220d2008-04-18 17:29:54 -0500359 if (!fixup)
360 return -ENOMEM;
361
Kay Sieversfb28ad32008-11-10 13:55:14 -0800362 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
Andy Flemingf62220d2008-04-18 17:29:54 -0500363 fixup->phy_uid = phy_uid;
364 fixup->phy_uid_mask = phy_uid_mask;
365 fixup->run = run;
366
367 mutex_lock(&phy_fixup_lock);
368 list_add_tail(&fixup->list, &phy_fixup_list);
369 mutex_unlock(&phy_fixup_lock);
370
371 return 0;
372}
373EXPORT_SYMBOL(phy_register_fixup);
374
375/* Registers a fixup to be run on any PHY with the UID in phy_uid */
376int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300377 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -0500378{
379 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
380}
381EXPORT_SYMBOL(phy_register_fixup_for_uid);
382
383/* Registers a fixup to be run on the PHY with id string bus_id */
384int phy_register_fixup_for_id(const char *bus_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300385 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -0500386{
387 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
388}
389EXPORT_SYMBOL(phy_register_fixup_for_id);
390
Woojung.Huh@microchip.comf38e7a32016-12-07 20:26:07 +0000391/**
392 * phy_unregister_fixup - remove a phy_fixup from the list
393 * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
394 * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
395 * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
396 */
397int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
398{
399 struct list_head *pos, *n;
400 struct phy_fixup *fixup;
401 int ret;
402
403 ret = -ENODEV;
404
405 mutex_lock(&phy_fixup_lock);
406 list_for_each_safe(pos, n, &phy_fixup_list) {
407 fixup = list_entry(pos, struct phy_fixup, list);
408
409 if ((!strcmp(fixup->bus_id, bus_id)) &&
410 ((fixup->phy_uid & phy_uid_mask) ==
411 (phy_uid & phy_uid_mask))) {
412 list_del(&fixup->list);
413 kfree(fixup);
414 ret = 0;
415 break;
416 }
417 }
418 mutex_unlock(&phy_fixup_lock);
419
420 return ret;
421}
422EXPORT_SYMBOL(phy_unregister_fixup);
423
424/* Unregisters a fixup of any PHY with the UID in phy_uid */
425int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
426{
427 return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
428}
429EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
430
431/* Unregisters a fixup of the PHY with id string bus_id */
432int phy_unregister_fixup_for_id(const char *bus_id)
433{
434 return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
435}
436EXPORT_SYMBOL(phy_unregister_fixup_for_id);
437
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300438/* Returns 1 if fixup matches phydev in bus_id and phy_uid.
Andy Flemingf62220d2008-04-18 17:29:54 -0500439 * Fixups can be set to match any in one or more fields.
440 */
441static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
442{
Andrew Lunn84eff6d2016-01-06 20:11:10 +0100443 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
Andy Flemingf62220d2008-04-18 17:29:54 -0500444 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
445 return 0;
446
447 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300448 (phydev->phy_id & fixup->phy_uid_mask))
Andy Flemingf62220d2008-04-18 17:29:54 -0500449 if (fixup->phy_uid != PHY_ANY_UID)
450 return 0;
451
452 return 1;
453}
454
455/* Runs any matching fixups for this phydev */
Sergei Shtylyovfbfcec62014-01-05 03:28:27 +0300456static int phy_scan_fixups(struct phy_device *phydev)
Andy Flemingf62220d2008-04-18 17:29:54 -0500457{
458 struct phy_fixup *fixup;
459
460 mutex_lock(&phy_fixup_lock);
461 list_for_each_entry(fixup, &phy_fixup_list, list) {
462 if (phy_needs_fixup(phydev, fixup)) {
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300463 int err = fixup->run(phydev);
Andy Flemingf62220d2008-04-18 17:29:54 -0500464
Jiri Slabybc23283c2009-07-13 11:23:39 +0000465 if (err < 0) {
466 mutex_unlock(&phy_fixup_lock);
Andy Flemingf62220d2008-04-18 17:29:54 -0500467 return err;
Jiri Slabybc23283c2009-07-13 11:23:39 +0000468 }
Florian Fainellib0ae0092014-02-11 17:27:41 -0800469 phydev->has_fixups = true;
Andy Flemingf62220d2008-04-18 17:29:54 -0500470 }
471 }
472 mutex_unlock(&phy_fixup_lock);
473
474 return 0;
475}
Andy Flemingf62220d2008-04-18 17:29:54 -0500476
Andrew Lunne76a4952016-01-06 20:11:23 +0100477static int phy_bus_match(struct device *dev, struct device_driver *drv)
478{
479 struct phy_device *phydev = to_phy_device(dev);
480 struct phy_driver *phydrv = to_phy_driver(drv);
481 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
482 int i;
483
Andrew Lunna9049e02016-01-06 20:11:26 +0100484 if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
485 return 0;
486
Andrew Lunne76a4952016-01-06 20:11:23 +0100487 if (phydrv->match_phy_device)
488 return phydrv->match_phy_device(phydev);
489
490 if (phydev->is_c45) {
491 for (i = 1; i < num_ids; i++) {
492 if (!(phydev->c45_ids.devices_in_package & (1 << i)))
493 continue;
494
495 if ((phydrv->phy_id & phydrv->phy_id_mask) ==
496 (phydev->c45_ids.device_ids[i] &
497 phydrv->phy_id_mask))
498 return 1;
499 }
500 return 0;
501 } else {
502 return (phydrv->phy_id & phydrv->phy_id_mask) ==
503 (phydev->phy_id & phydrv->phy_id_mask);
504 }
505}
506
Heiner Kallweit7f4828f2018-06-02 22:36:06 +0200507static ssize_t
508phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
509{
510 struct phy_device *phydev = to_phy_device(dev);
511
512 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
513}
514static DEVICE_ATTR_RO(phy_id);
515
516static ssize_t
517phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
518{
519 struct phy_device *phydev = to_phy_device(dev);
520 const char *mode = NULL;
521
522 if (phy_is_internal(phydev))
523 mode = "internal";
524 else
525 mode = phy_modes(phydev->interface);
526
527 return sprintf(buf, "%s\n", mode);
528}
529static DEVICE_ATTR_RO(phy_interface);
530
531static ssize_t
532phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
533 char *buf)
534{
535 struct phy_device *phydev = to_phy_device(dev);
536
537 return sprintf(buf, "%d\n", phydev->has_fixups);
538}
539static DEVICE_ATTR_RO(phy_has_fixups);
540
541static struct attribute *phy_dev_attrs[] = {
542 &dev_attr_phy_id.attr,
543 &dev_attr_phy_interface.attr,
544 &dev_attr_phy_has_fixups.attr,
545 NULL,
546};
547ATTRIBUTE_GROUPS(phy_dev);
548
549static const struct device_type mdio_bus_phy_type = {
550 .name = "PHY",
551 .groups = phy_dev_groups,
552 .release = phy_device_release,
553 .pm = MDIO_BUS_PHY_PM_OPS,
554};
555
Heiner Kallweit13d0ab62019-01-16 08:07:38 +0100556static int phy_request_driver_module(struct phy_device *dev, int phy_id)
557{
558 int ret;
559
560 ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
561 MDIO_ID_ARGS(phy_id));
Heiner Kallweit21e19442019-01-19 10:30:21 +0100562 /* We only check for failures in executing the usermode binary,
563 * not whether a PHY driver module exists for the PHY ID.
564 * Accept -ENOENT because this may occur in case no initramfs exists,
565 * then modprobe isn't available.
Heiner Kallweit13d0ab62019-01-16 08:07:38 +0100566 */
Heiner Kallweit21e19442019-01-19 10:30:21 +0100567 if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) {
Heiner Kallweit13d0ab62019-01-16 08:07:38 +0100568 phydev_err(dev, "error %d loading PHY driver module for ID 0x%08x\n",
569 ret, phy_id);
570 return ret;
571 }
572
573 return 0;
574}
575
David Daneyac28b9f2012-06-27 07:33:35 +0000576struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300577 bool is_c45,
578 struct phy_c45_device_ids *c45_ids)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700579{
580 struct phy_device *dev;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100581 struct mdio_device *mdiodev;
Heiner Kallweit13d0ab62019-01-16 08:07:38 +0100582 int ret = 0;
David Woodhouse8626d3b2010-04-02 01:05:27 +0000583
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300584 /* We allocate the device, and initialize the default values */
Robert P. J. Daycd861282006-12-13 00:34:52 -0800585 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Sergei Shtylyovef899c02015-08-28 21:35:14 +0300586 if (!dev)
Sergei Shtylyovd3765f02015-08-28 21:34:34 +0300587 return ERR_PTR(-ENOMEM);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700588
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100589 mdiodev = &dev->mdio;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100590 mdiodev->dev.parent = &bus->dev;
591 mdiodev->dev.bus = &mdio_bus_type;
Heiner Kallweit7f4828f2018-06-02 22:36:06 +0200592 mdiodev->dev.type = &mdio_bus_phy_type;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100593 mdiodev->bus = bus;
Andrew Lunne76a4952016-01-06 20:11:23 +0100594 mdiodev->bus_match = phy_bus_match;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100595 mdiodev->addr = addr;
Andrew Lunn7f854422016-01-06 20:11:18 +0100596 mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
Andrew Lunn711fdba2016-01-06 20:11:27 +0100597 mdiodev->device_free = phy_mdio_device_free;
598 mdiodev->device_remove = phy_mdio_device_remove;
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300599
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700600 dev->speed = 0;
601 dev->duplex = -1;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300602 dev->pause = 0;
603 dev->asym_pause = 0;
Heiner Kallweitc69851e2018-03-11 15:00:37 +0100604 dev->link = 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600605 dev->interface = PHY_INTERFACE_MODE_GMII;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700606
607 dev->autoneg = AUTONEG_ENABLE;
608
David Daneyac28b9f2012-06-27 07:33:35 +0000609 dev->is_c45 = is_c45;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700610 dev->phy_id = phy_id;
David Daneyac28b9f2012-06-27 07:33:35 +0000611 if (c45_ids)
612 dev->c45_ids = *c45_ids;
Dan Carpenter47b356e2016-01-12 12:36:21 +0300613 dev->irq = bus->irq[addr];
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100614 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700615
616 dev->state = PHY_DOWN;
617
Nate Case35b5f6b2008-01-29 10:05:09 -0600618 mutex_init(&dev->lock);
Anton Vorontsov4f9c85a2010-01-18 05:37:16 +0000619 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700620
David Woodhouse8626d3b2010-04-02 01:05:27 +0000621 /* Request the appropriate module unconditionally; don't
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300622 * bother trying to do so only if it isn't already loaded,
623 * because that gets complicated. A hotplug event would have
624 * done an unconditional modprobe anyway.
625 * We don't do normal hotplug because it won't work for MDIO
626 * -- because it relies on the device staying around for long
627 * enough for the driver to get loaded. With MDIO, the NIC
628 * driver will get bored and give up as soon as it finds that
629 * there's no driver _already_ loaded.
630 */
Jose Abreu30fcd6a2018-12-02 16:33:14 +0100631 if (is_c45 && c45_ids) {
632 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
633 int i;
634
635 for (i = 1; i < num_ids; i++) {
636 if (!(c45_ids->devices_in_package & (1 << i)))
637 continue;
638
Heiner Kallweit13d0ab62019-01-16 08:07:38 +0100639 ret = phy_request_driver_module(dev,
640 c45_ids->device_ids[i]);
641 if (ret)
642 break;
Jose Abreu30fcd6a2018-12-02 16:33:14 +0100643 }
644 } else {
Heiner Kallweit13d0ab62019-01-16 08:07:38 +0100645 ret = phy_request_driver_module(dev, phy_id);
Jose Abreu30fcd6a2018-12-02 16:33:14 +0100646 }
David Woodhouse8626d3b2010-04-02 01:05:27 +0000647
Heiner Kallweit13d0ab62019-01-16 08:07:38 +0100648 if (!ret) {
649 device_initialize(&mdiodev->dev);
650 } else {
651 kfree(dev);
652 dev = ERR_PTR(ret);
653 }
Petr Malatb2a43192013-02-28 01:01:52 +0000654
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700655 return dev;
656}
David Daneyac28b9f2012-06-27 07:33:35 +0000657EXPORT_SYMBOL(phy_device_create);
658
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800659/* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
660 * @bus: the target MII bus
661 * @addr: PHY address on the MII bus
662 * @dev_addr: MMD address in the PHY.
663 * @devices_in_package: where to store the devices in package information.
664 *
665 * Description: reads devices in package registers of a MMD at @dev_addr
666 * from PHY at @addr on @bus.
667 *
668 * Returns: 0 on success, -EIO on failure.
669 */
670static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
671 u32 *devices_in_package)
672{
673 int phy_reg, reg_addr;
674
675 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
676 phy_reg = mdiobus_read(bus, addr, reg_addr);
677 if (phy_reg < 0)
678 return -EIO;
Heiner Kallweit50684da2019-02-09 09:46:53 +0100679 *devices_in_package = phy_reg << 16;
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800680
681 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
682 phy_reg = mdiobus_read(bus, addr, reg_addr);
683 if (phy_reg < 0)
684 return -EIO;
Heiner Kallweit50684da2019-02-09 09:46:53 +0100685 *devices_in_package |= phy_reg;
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800686
Heiner Kallweit3b5e74e2019-02-08 19:25:22 +0100687 /* Bit 0 doesn't represent a device, it indicates c22 regs presence */
688 *devices_in_package &= ~BIT(0);
689
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800690 return 0;
691}
692
David Daneyac28b9f2012-06-27 07:33:35 +0000693/**
694 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
695 * @bus: the target MII bus
696 * @addr: PHY address on the MII bus
697 * @phy_id: where to store the ID retrieved.
698 * @c45_ids: where to store the c45 ID information.
699 *
700 * If the PHY devices-in-package appears to be valid, it and the
701 * corresponding identifiers are stored in @c45_ids, zero is stored
702 * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns
703 * zero on success.
704 *
705 */
706static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
707 struct phy_c45_device_ids *c45_ids) {
708 int phy_reg;
709 int i, reg_addr;
710 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800711 u32 *devs = &c45_ids->devices_in_package;
David Daneyac28b9f2012-06-27 07:33:35 +0000712
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800713 /* Find first non-zero Devices In package. Device zero is reserved
714 * for 802.3 c45 complied PHYs, so don't probe it at first.
David Daneyac28b9f2012-06-27 07:33:35 +0000715 */
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800716 for (i = 1; i < num_ids && *devs == 0; i++) {
717 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
David Daneyac28b9f2012-06-27 07:33:35 +0000718 if (phy_reg < 0)
719 return -EIO;
David Daneyac28b9f2012-06-27 07:33:35 +0000720
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800721 if ((*devs & 0x1fffffff) == 0x1fffffff) {
722 /* If mostly Fs, there is no device there,
723 * then let's continue to probe more, as some
724 * 10G PHYs have zero Devices In package,
725 * e.g. Cortina CS4315/CS4340 PHY.
726 */
727 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
728 if (phy_reg < 0)
729 return -EIO;
730 /* no device there, let's get out of here */
731 if ((*devs & 0x1fffffff) == 0x1fffffff) {
Shengzhou Liuda1da282015-06-26 17:58:52 +0800732 *phy_id = 0xffffffff;
733 return 0;
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800734 } else {
735 break;
Shengzhou Liuda1da282015-06-26 17:58:52 +0800736 }
David Daneyac28b9f2012-06-27 07:33:35 +0000737 }
738 }
739
740 /* Now probe Device Identifiers for each device present. */
741 for (i = 1; i < num_ids; i++) {
742 if (!(c45_ids->devices_in_package & (1 << i)))
743 continue;
744
745 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
746 phy_reg = mdiobus_read(bus, addr, reg_addr);
747 if (phy_reg < 0)
748 return -EIO;
Heiner Kallweit50684da2019-02-09 09:46:53 +0100749 c45_ids->device_ids[i] = phy_reg << 16;
David Daneyac28b9f2012-06-27 07:33:35 +0000750
751 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
752 phy_reg = mdiobus_read(bus, addr, reg_addr);
753 if (phy_reg < 0)
754 return -EIO;
Heiner Kallweit50684da2019-02-09 09:46:53 +0100755 c45_ids->device_ids[i] |= phy_reg;
David Daneyac28b9f2012-06-27 07:33:35 +0000756 }
757 *phy_id = 0;
758 return 0;
759}
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700760
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800761/**
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400762 * get_phy_id - reads the specified addr for its ID.
763 * @bus: the target MII bus
764 * @addr: PHY address on the MII bus
765 * @phy_id: where to store the ID retrieved.
David Daneyac28b9f2012-06-27 07:33:35 +0000766 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
767 * @c45_ids: where to store the c45 ID information.
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400768 *
David Daneyac28b9f2012-06-27 07:33:35 +0000769 * Description: In the case of a 802.3-c22 PHY, reads the ID registers
770 * of the PHY at @addr on the @bus, stores it in @phy_id and returns
771 * zero on success.
772 *
773 * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
774 * its return value is in turn returned.
775 *
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400776 */
David Daneyac28b9f2012-06-27 07:33:35 +0000777static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
778 bool is_c45, struct phy_c45_device_ids *c45_ids)
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400779{
780 int phy_reg;
781
David Daneyac28b9f2012-06-27 07:33:35 +0000782 if (is_c45)
783 return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
784
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300785 /* Grab the bits from PHYIR1, and put them in the upper half */
David Daney6fe32642011-09-30 11:51:22 +0000786 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
Alexandre Belloni02a6efc2018-04-24 18:09:04 +0200787 if (phy_reg < 0) {
Heiner Kallweitd8cce3a2019-01-16 19:52:51 +0100788 /* returning -ENODEV doesn't stop bus scanning */
789 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
Alexandre Belloni02a6efc2018-04-24 18:09:04 +0200790 }
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400791
Heiner Kallweit50684da2019-02-09 09:46:53 +0100792 *phy_id = phy_reg << 16;
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400793
794 /* Grab the bits from PHYIR2, and put them in the lower half */
David Daney6fe32642011-09-30 11:51:22 +0000795 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400796 if (phy_reg < 0)
797 return -EIO;
798
Heiner Kallweit50684da2019-02-09 09:46:53 +0100799 *phy_id |= phy_reg;
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400800
801 return 0;
802}
803
804/**
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300805 * get_phy_device - reads the specified PHY device and returns its @phy_device
806 * struct
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800807 * @bus: the target MII bus
808 * @addr: PHY address on the MII bus
David Daneyac28b9f2012-06-27 07:33:35 +0000809 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
Andy Fleming00db8182005-07-30 19:31:23 -0400810 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800811 * Description: Reads the ID registers of the PHY at @addr on the
812 * @bus, then allocates and returns the phy_device to represent it.
Andy Fleming00db8182005-07-30 19:31:23 -0400813 */
David Daneyac28b9f2012-06-27 07:33:35 +0000814struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
Andy Fleming00db8182005-07-30 19:31:23 -0400815{
David Daneyac28b9f2012-06-27 07:33:35 +0000816 struct phy_c45_device_ids c45_ids = {0};
David S. Miller160c85f2012-06-27 21:28:14 -0700817 u32 phy_id = 0;
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400818 int r;
Andy Fleming00db8182005-07-30 19:31:23 -0400819
David Daneyac28b9f2012-06-27 07:33:35 +0000820 r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400821 if (r)
822 return ERR_PTR(r);
Andy Fleming00db8182005-07-30 19:31:23 -0400823
Giuseppe Cavallaro6436cbc2008-11-20 20:43:18 -0800824 /* If the phy_id is mostly Fs, there is no device there */
825 if ((phy_id & 0x1fffffff) == 0x1fffffff)
Sergei Shtylyovb74766a2016-04-24 20:25:23 +0300826 return ERR_PTR(-ENODEV);
Giuseppe Cavallaro6436cbc2008-11-20 20:43:18 -0800827
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300828 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
Andy Fleming00db8182005-07-30 19:31:23 -0400829}
Grant Likely4dea5472009-04-25 12:52:46 +0000830EXPORT_SYMBOL(get_phy_device);
831
832/**
833 * phy_device_register - Register the phy device on the MDIO bus
Randy Dunlap1d4ac5d2009-06-16 16:56:33 +0000834 * @phydev: phy_device structure to be added to the MDIO bus
Grant Likely4dea5472009-04-25 12:52:46 +0000835 */
836int phy_device_register(struct phy_device *phydev)
837{
838 int err;
839
Andrew Lunn7f854422016-01-06 20:11:18 +0100840 err = mdiobus_register_device(&phydev->mdio);
841 if (err)
842 return err;
Grant Likely4dea5472009-04-25 12:52:46 +0000843
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +0100844 /* Deassert the reset signal */
845 phy_device_reset(phydev, 0);
846
Grant Likely4dea5472009-04-25 12:52:46 +0000847 /* Run all of the fixups for this PHY */
Florian Fainellid92f5de2014-07-28 16:28:07 -0700848 err = phy_scan_fixups(phydev);
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800849 if (err) {
Heiner Kallweitc3a6a172019-01-15 21:50:25 +0100850 phydev_err(phydev, "failed to initialize\n");
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800851 goto out;
852 }
Grant Likely4dea5472009-04-25 12:52:46 +0000853
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100854 err = device_add(&phydev->mdio.dev);
Grant Likely4dea5472009-04-25 12:52:46 +0000855 if (err) {
Heiner Kallweitc3a6a172019-01-15 21:50:25 +0100856 phydev_err(phydev, "failed to add\n");
Grant Likely4dea5472009-04-25 12:52:46 +0000857 goto out;
858 }
859
860 return 0;
861
862 out:
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +0100863 /* Assert the reset signal */
864 phy_device_reset(phydev, 1);
865
Andrew Lunn7f854422016-01-06 20:11:18 +0100866 mdiobus_unregister_device(&phydev->mdio);
Grant Likely4dea5472009-04-25 12:52:46 +0000867 return err;
868}
869EXPORT_SYMBOL(phy_device_register);
Andy Fleming00db8182005-07-30 19:31:23 -0400870
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800871/**
Russell King38737e42015-09-24 20:36:28 +0100872 * phy_device_remove - Remove a previously registered phy device from the MDIO bus
873 * @phydev: phy_device structure to remove
874 *
875 * This doesn't free the phy_device itself, it merely reverses the effects
876 * of phy_device_register(). Use phy_device_free() to free the device
877 * after calling this function.
878 */
879void phy_device_remove(struct phy_device *phydev)
880{
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100881 device_del(&phydev->mdio.dev);
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +0100882
883 /* Assert the reset signal */
884 phy_device_reset(phydev, 1);
885
Andrew Lunn7f854422016-01-06 20:11:18 +0100886 mdiobus_unregister_device(&phydev->mdio);
Russell King38737e42015-09-24 20:36:28 +0100887}
888EXPORT_SYMBOL(phy_device_remove);
889
890/**
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800891 * phy_find_first - finds the first PHY device on the bus
892 * @bus: the target MII bus
893 */
894struct phy_device *phy_find_first(struct mii_bus *bus)
895{
Andrew Lunn7f854422016-01-06 20:11:18 +0100896 struct phy_device *phydev;
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800897 int addr;
898
899 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
Andrew Lunn7f854422016-01-06 20:11:18 +0100900 phydev = mdiobus_get_phy(bus, addr);
901 if (phydev)
902 return phydev;
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800903 }
904 return NULL;
905}
906EXPORT_SYMBOL(phy_find_first);
907
Russell Kinga81497b2017-07-25 15:02:58 +0100908static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier)
909{
910 struct net_device *netdev = phydev->attached_dev;
911
912 if (do_carrier) {
913 if (up)
914 netif_carrier_on(netdev);
915 else
916 netif_carrier_off(netdev);
917 }
918 phydev->adjust_link(netdev);
919}
920
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800921/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800922 * phy_prepare_link - prepares the PHY layer to monitor link status
923 * @phydev: target phy_device struct
924 * @handler: callback function for link status change notifications
Andy Fleming00db8182005-07-30 19:31:23 -0400925 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800926 * Description: Tells the PHY infrastructure to handle the
Andy Fleming00db8182005-07-30 19:31:23 -0400927 * gory details on monitoring link status (whether through
928 * polling or an interrupt), and to call back to the
929 * connected device driver when the link status changes.
930 * If you want to monitor your own link state, don't call
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800931 * this function.
932 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000933static void phy_prepare_link(struct phy_device *phydev,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300934 void (*handler)(struct net_device *))
Andy Fleming00db8182005-07-30 19:31:23 -0400935{
936 phydev->adjust_link = handler;
937}
938
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800939/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000940 * phy_connect_direct - connect an ethernet device to a specific phy_device
941 * @dev: the network device to connect
942 * @phydev: the pointer to the phy device
943 * @handler: callback function for state change notifications
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000944 * @interface: PHY device's interface
945 */
946int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
Florian Fainellif9a8f832013-01-14 00:52:52 +0000947 void (*handler)(struct net_device *),
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000948 phy_interface_t interface)
949{
950 int rc;
951
Florian Fainellif9a8f832013-01-14 00:52:52 +0000952 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000953 if (rc)
954 return rc;
955
956 phy_prepare_link(phydev, handler);
Heiner Kallweit434a4312019-01-23 07:31:58 +0100957 if (phy_interrupt_is_valid(phydev))
958 phy_request_interrupt(phydev);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000959
960 return 0;
961}
962EXPORT_SYMBOL(phy_connect_direct);
963
964/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800965 * phy_connect - connect an ethernet device to a PHY device
966 * @dev: the network device to connect
Randy Dunlap5d12b132008-04-28 10:58:22 -0700967 * @bus_id: the id string of the PHY device to connect
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800968 * @handler: callback function for state change notifications
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800969 * @interface: PHY device's interface
Andy Fleminge1393452005-08-24 18:46:21 -0500970 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800971 * Description: Convenience function for connecting ethernet
Andy Fleminge1393452005-08-24 18:46:21 -0500972 * devices to PHY devices. The default behavior is for
973 * the PHY infrastructure to handle everything, and only notify
974 * the connected driver when the link status changes. If you
975 * don't want, or can't use the provided functionality, you may
976 * choose to call only the subset of functions which provide
977 * the desired functionality.
978 */
Florian Fainellie1093742013-12-17 21:38:12 -0800979struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300980 void (*handler)(struct net_device *),
981 phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -0500982{
983 struct phy_device *phydev;
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000984 struct device *d;
985 int rc;
Andy Fleminge1393452005-08-24 18:46:21 -0500986
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000987 /* Search the list of PHY devices on the mdio bus for the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300988 * PHY with the requested name
989 */
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000990 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
991 if (!d) {
992 pr_err("PHY %s not found\n", bus_id);
993 return ERR_PTR(-ENODEV);
994 }
995 phydev = to_phy_device(d);
Andy Fleminge1393452005-08-24 18:46:21 -0500996
Florian Fainellif9a8f832013-01-14 00:52:52 +0000997 rc = phy_connect_direct(dev, phydev, handler, interface);
Johan Hovold17ae1c62016-11-03 18:40:19 +0100998 put_device(d);
Grant Likelyfa94f6d2009-04-25 12:52:51 +0000999 if (rc)
1000 return ERR_PTR(rc);
Andy Fleminge1393452005-08-24 18:46:21 -05001001
1002 return phydev;
1003}
1004EXPORT_SYMBOL(phy_connect);
1005
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001006/**
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001007 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
1008 * device
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001009 * @phydev: target phy_device struct
1010 */
Andy Fleminge1393452005-08-24 18:46:21 -05001011void phy_disconnect(struct phy_device *phydev)
1012{
Heiner Kallweit472115d2019-01-17 20:07:54 +01001013 if (phy_is_started(phydev))
1014 phy_stop(phydev);
1015
Heiner Kallweitbb658ab2019-01-17 20:09:21 +01001016 if (phy_interrupt_is_valid(phydev))
1017 free_irq(phydev->irq, phydev);
Andy Fleminge1393452005-08-24 18:46:21 -05001018
Andy Fleminge1393452005-08-24 18:46:21 -05001019 phydev->adjust_link = NULL;
1020
1021 phy_detach(phydev);
1022}
1023EXPORT_SYMBOL(phy_disconnect);
1024
Florian Fainelli87aa9f92013-12-06 13:01:34 -08001025/**
1026 * phy_poll_reset - Safely wait until a PHY reset has properly completed
1027 * @phydev: The PHY device to poll
1028 *
1029 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
1030 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
1031 * register must be polled until the BMCR_RESET bit clears.
1032 *
1033 * Furthermore, any attempts to write to PHY registers may have no effect
1034 * or even generate MDIO bus errors until this is complete.
1035 *
1036 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
1037 * standard and do not fully reset after the BMCR_RESET bit is set, and may
1038 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
1039 * effort to support such broken PHYs, this function is separate from the
1040 * standard phy_init_hw() which will zero all the other bits in the BMCR
1041 * and reapply all driver-specific and board-specific fixups.
1042 */
1043static int phy_poll_reset(struct phy_device *phydev)
1044{
1045 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
1046 unsigned int retries = 12;
1047 int ret;
1048
1049 do {
1050 msleep(50);
1051 ret = phy_read(phydev, MII_BMCR);
1052 if (ret < 0)
1053 return ret;
1054 } while (ret & BMCR_RESET && --retries);
1055 if (ret & BMCR_RESET)
1056 return -ETIMEDOUT;
1057
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001058 /* Some chips (smsc911x) may still need up to another 1ms after the
Florian Fainelli87aa9f92013-12-06 13:01:34 -08001059 * BMCR_RESET bit is cleared before they are usable.
1060 */
1061 msleep(1);
1062 return 0;
1063}
1064
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001065int phy_init_hw(struct phy_device *phydev)
1066{
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001067 int ret = 0;
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001068
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01001069 /* Deassert the reset signal */
1070 phy_device_reset(phydev, 0);
1071
Heiner Kallweita5996982019-01-19 10:43:07 +01001072 if (!phydev->drv)
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001073 return 0;
1074
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001075 if (phydev->drv->soft_reset)
1076 ret = phydev->drv->soft_reset(phydev);
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001077
Florian Fainelli87aa9f92013-12-06 13:01:34 -08001078 if (ret < 0)
1079 return ret;
1080
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001081 ret = phy_scan_fixups(phydev);
1082 if (ret < 0)
1083 return ret;
1084
Heiner Kallweita5996982019-01-19 10:43:07 +01001085 if (phydev->drv->config_init)
1086 ret = phydev->drv->config_init(phydev);
1087
1088 return ret;
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001089}
Florian Fainelli87aa9f92013-12-06 13:01:34 -08001090EXPORT_SYMBOL(phy_init_hw);
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001091
Andrew Lunn22209432016-01-06 20:11:13 +01001092void phy_attached_info(struct phy_device *phydev)
1093{
1094 phy_attached_print(phydev, NULL);
1095}
1096EXPORT_SYMBOL(phy_attached_info);
1097
Romain Perier5e369ae2017-09-04 10:41:36 +02001098#define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
Andrew Lunn22209432016-01-06 20:11:13 +01001099void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1100{
Florian Fainellifcd03e32017-08-22 14:26:47 -07001101 const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
Romain Perier5e369ae2017-09-04 10:41:36 +02001102 char *irq_str;
Geert Uytterhoeven059fbe82017-09-21 13:27:02 +02001103 char irq_num[8];
Romain Perier5e369ae2017-09-04 10:41:36 +02001104
1105 switch(phydev->irq) {
1106 case PHY_POLL:
1107 irq_str = "POLL";
1108 break;
1109 case PHY_IGNORE_INTERRUPT:
1110 irq_str = "IGNORE";
1111 break;
1112 default:
1113 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
1114 irq_str = irq_num;
1115 break;
1116 }
1117
Florian Fainellifcd03e32017-08-22 14:26:47 -07001118
Andrew Lunn22209432016-01-06 20:11:13 +01001119 if (!fmt) {
Andrew Lunnc4fabb82018-09-29 23:04:11 +02001120 phydev_info(phydev, ATTACHED_FMT "\n",
Florian Fainellifcd03e32017-08-22 14:26:47 -07001121 drv_name, phydev_name(phydev),
Romain Perier5e369ae2017-09-04 10:41:36 +02001122 irq_str);
Andrew Lunn22209432016-01-06 20:11:13 +01001123 } else {
1124 va_list ap;
1125
Andrew Lunnc4fabb82018-09-29 23:04:11 +02001126 phydev_info(phydev, ATTACHED_FMT,
Florian Fainellifcd03e32017-08-22 14:26:47 -07001127 drv_name, phydev_name(phydev),
Romain Perier5e369ae2017-09-04 10:41:36 +02001128 irq_str);
Andrew Lunn22209432016-01-06 20:11:13 +01001129
1130 va_start(ap, fmt);
1131 vprintk(fmt, ap);
1132 va_end(ap);
1133 }
1134}
1135EXPORT_SYMBOL(phy_attached_print);
1136
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001137/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001138 * phy_attach_direct - attach a network device to a given PHY device pointer
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001139 * @dev: network device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001140 * @phydev: Pointer to phy_device to attach
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001141 * @flags: PHY device's dev_flags
1142 * @interface: PHY device's interface
1143 *
1144 * Description: Called by drivers to attach to a particular PHY
1145 * device. The phy_device is found, and properly hooked up
Andy Fleming257184d2014-01-10 14:27:54 +08001146 * to the phy_driver. If no driver is attached, then a
1147 * generic driver is used. The phy_device is given a ptr to
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001148 * the attaching device, and given a callback for link status
1149 * change. The phy_device is returned to the attaching driver.
Russell King73229672015-09-24 20:36:08 +01001150 * This function takes a reference on the phy device.
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001151 */
Andy Fleming257184d2014-01-10 14:27:54 +08001152int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1153 u32 flags, phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -05001154{
Florian Fainelliec988ad2016-12-06 20:54:43 -08001155 struct module *ndev_owner = dev->dev.parent->driver->owner;
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001156 struct mii_bus *bus = phydev->mdio.bus;
1157 struct device *d = &phydev->mdio.dev;
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001158 bool using_genphy = false;
Marc Kleine-Budded005a092011-03-28 14:54:08 +00001159 int err;
Andy Fleminge1393452005-08-24 18:46:21 -05001160
Florian Fainelliec988ad2016-12-06 20:54:43 -08001161 /* For Ethernet device drivers that register their own MDIO bus, we
1162 * will have bus->owner match ndev_mod, so we do not want to increment
1163 * our own module->refcnt here, otherwise we would not be able to
1164 * unload later on.
1165 */
1166 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
Russell King3e3aaf62015-09-24 20:36:02 +01001167 dev_err(&dev->dev, "failed to get the bus module\n");
1168 return -EIO;
1169 }
1170
Russell King73229672015-09-24 20:36:08 +01001171 get_device(d);
1172
Andy Fleminge1393452005-08-24 18:46:21 -05001173 /* Assume that if there is no driver, that it doesn't
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001174 * exist, and we should use the genphy driver.
1175 */
Sergei Shtylyovef899c02015-08-28 21:35:14 +03001176 if (!d->driver) {
Andy Fleming257184d2014-01-10 14:27:54 +08001177 if (phydev->is_c45)
Russell King921690f2017-06-05 12:23:05 +01001178 d->driver = &genphy_10g_driver.mdiodrv.driver;
Andy Fleming257184d2014-01-10 14:27:54 +08001179 else
Russell King921690f2017-06-05 12:23:05 +01001180 d->driver = &genphy_driver.mdiodrv.driver;
Andy Fleminge1393452005-08-24 18:46:21 -05001181
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001182 using_genphy = true;
1183 }
1184
1185 if (!try_module_get(d->driver->owner)) {
1186 dev_err(&dev->dev, "failed to get the device driver module\n");
1187 err = -EIO;
1188 goto error_put_device;
1189 }
1190
1191 if (using_genphy) {
Andy Fleminge1393452005-08-24 18:46:21 -05001192 err = d->driver->probe(d);
Jeff Garzikb7a00ec2006-10-01 07:27:46 -04001193 if (err >= 0)
1194 err = device_bind_driver(d);
Andy Fleminge1393452005-08-24 18:46:21 -05001195
Jeff Garzikb7a00ec2006-10-01 07:27:46 -04001196 if (err)
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001197 goto error_module_put;
Andy Fleminge1393452005-08-24 18:46:21 -05001198 }
1199
1200 if (phydev->attached_dev) {
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001201 dev_err(&dev->dev, "PHY already attached\n");
Russell King3e3aaf62015-09-24 20:36:02 +01001202 err = -EBUSY;
1203 goto error;
Ezequiel Garciab3565f22014-07-23 16:47:32 -03001204 }
1205
Russell Kinga81497b2017-07-25 15:02:58 +01001206 phydev->phy_link_change = phy_link_change;
Andy Fleminge1393452005-08-24 18:46:21 -05001207 phydev->attached_dev = dev;
Richard Cochranc1f19b52010-07-17 08:49:36 +00001208 dev->phydev = phydev;
Florian Fainellia3995462017-05-27 10:42:25 -07001209
1210 /* Some Ethernet drivers try to connect to a PHY device before
1211 * calling register_netdevice() -> netdev_register_kobject() and
1212 * does the dev->dev.kobj initialization. Here we only check for
1213 * success which indicates that the network device kobject is
1214 * ready. Once we do that we still need to keep track of whether
1215 * links were successfully set up or not for phy_detach() to
1216 * remove them accordingly.
1217 */
1218 phydev->sysfs_links = false;
1219
Florian Fainelli55683632017-05-25 09:21:41 -07001220 err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1221 "attached_dev");
Florian Fainellia3995462017-05-27 10:42:25 -07001222 if (!err) {
Grygorii Strashko4414b3e2018-03-16 17:08:35 -05001223 err = sysfs_create_link_nowarn(&dev->dev.kobj,
1224 &phydev->mdio.dev.kobj,
1225 "phydev");
1226 if (err) {
1227 dev_err(&dev->dev, "could not add device link to %s err %d\n",
1228 kobject_name(&phydev->mdio.dev.kobj),
1229 err);
1230 /* non-fatal - some net drivers can use one netdevice
1231 * with more then one phy
1232 */
1233 }
Florian Fainelli55683632017-05-25 09:21:41 -07001234
Florian Fainellia3995462017-05-27 10:42:25 -07001235 phydev->sysfs_links = true;
1236 }
Andy Fleminge1393452005-08-24 18:46:21 -05001237
1238 phydev->dev_flags = flags;
1239
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001240 phydev->interface = interface;
1241
Anton Vorontsovef24b162010-08-24 14:46:12 -07001242 phydev->state = PHY_READY;
1243
Sjoerd Simons113c74d2016-01-14 21:57:18 +01001244 /* Initial carrier state is off as the phy is about to be
1245 * (re)initialized.
1246 */
1247 netif_carrier_off(phydev->attached_dev);
1248
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001249 /* Do initial configuration here, now that
1250 * we have certain key parameters
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001251 * (dev_flags and interface)
1252 */
Marc Kleine-Budded005a092011-03-28 14:54:08 +00001253 err = phy_init_hw(phydev);
1254 if (err)
Woojung Huha7dac9f2016-11-23 23:10:33 +00001255 goto error;
Sebastian Hesselbarth1211ce52013-12-13 10:20:28 +01001256
Woojung Huha7dac9f2016-11-23 23:10:33 +00001257 phy_resume(phydev);
Zach Brown2e0bc452016-10-17 10:49:55 -05001258 phy_led_triggers_register(phydev);
1259
Marc Kleine-Budded005a092011-03-28 14:54:08 +00001260 return err;
Russell King3e3aaf62015-09-24 20:36:02 +01001261
1262error:
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001263 /* phy_detach() does all of the cleanup below */
Woojung Huha7dac9f2016-11-23 23:10:33 +00001264 phy_detach(phydev);
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001265 return err;
1266
1267error_module_put:
Mao Wenancafe8df2017-01-31 18:46:43 -08001268 module_put(d->driver->owner);
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001269error_put_device:
1270 put_device(d);
Florian Fainelliec988ad2016-12-06 20:54:43 -08001271 if (ndev_owner != bus->owner)
1272 module_put(bus->owner);
Russell King3e3aaf62015-09-24 20:36:02 +01001273 return err;
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001274}
Andy Fleming257184d2014-01-10 14:27:54 +08001275EXPORT_SYMBOL(phy_attach_direct);
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001276
1277/**
1278 * phy_attach - attach a network device to a particular PHY device
1279 * @dev: network device to attach
1280 * @bus_id: Bus ID of PHY device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001281 * @interface: PHY device's interface
1282 *
1283 * Description: Same as phy_attach_direct() except that a PHY bus_id
1284 * string is passed instead of a pointer to a struct phy_device.
1285 */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001286struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1287 phy_interface_t interface)
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001288{
1289 struct bus_type *bus = &mdio_bus_type;
1290 struct phy_device *phydev;
1291 struct device *d;
1292 int rc;
1293
1294 /* Search the list of PHY devices on the mdio bus for the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001295 * PHY with the requested name
1296 */
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001297 d = bus_find_device_by_name(bus, NULL, bus_id);
1298 if (!d) {
1299 pr_err("PHY %s not found\n", bus_id);
1300 return ERR_PTR(-ENODEV);
1301 }
1302 phydev = to_phy_device(d);
1303
Florian Fainellif9a8f832013-01-14 00:52:52 +00001304 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
Johan Hovold17ae1c62016-11-03 18:40:19 +01001305 put_device(d);
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001306 if (rc)
1307 return ERR_PTR(rc);
1308
Andy Fleminge1393452005-08-24 18:46:21 -05001309 return phydev;
1310}
1311EXPORT_SYMBOL(phy_attach);
1312
Florian Fainelli5db5ea92019-01-15 15:09:35 -08001313static bool phy_driver_is_genphy_kind(struct phy_device *phydev,
1314 struct device_driver *driver)
1315{
1316 struct device *d = &phydev->mdio.dev;
1317 bool ret = false;
1318
1319 if (!phydev->drv)
1320 return ret;
1321
1322 get_device(d);
1323 ret = d->driver == driver;
1324 put_device(d);
1325
1326 return ret;
1327}
1328
1329bool phy_driver_is_genphy(struct phy_device *phydev)
1330{
1331 return phy_driver_is_genphy_kind(phydev,
1332 &genphy_driver.mdiodrv.driver);
1333}
1334EXPORT_SYMBOL_GPL(phy_driver_is_genphy);
1335
1336bool phy_driver_is_genphy_10g(struct phy_device *phydev)
1337{
1338 return phy_driver_is_genphy_kind(phydev,
1339 &genphy_10g_driver.mdiodrv.driver);
1340}
1341EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
1342
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001343/**
1344 * phy_detach - detach a PHY device from its network device
1345 * @phydev: target phy_device struct
Russell King73229672015-09-24 20:36:08 +01001346 *
1347 * This detaches the phy device from its network device and the phy
1348 * driver, and drops the reference count taken in phy_attach_direct().
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001349 */
Andy Fleminge1393452005-08-24 18:46:21 -05001350void phy_detach(struct phy_device *phydev)
1351{
Florian Fainelliec988ad2016-12-06 20:54:43 -08001352 struct net_device *dev = phydev->attached_dev;
1353 struct module *ndev_owner = dev->dev.parent->driver->owner;
Russell King3e3aaf62015-09-24 20:36:02 +01001354 struct mii_bus *bus;
Ezequiel Garciab3565f22014-07-23 16:47:32 -03001355
Florian Fainellia3995462017-05-27 10:42:25 -07001356 if (phydev->sysfs_links) {
1357 sysfs_remove_link(&dev->dev.kobj, "phydev");
1358 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1359 }
Heiner Kallweit93f41e62018-09-24 22:01:32 +02001360 phy_suspend(phydev);
Richard Cochranc1f19b52010-07-17 08:49:36 +00001361 phydev->attached_dev->phydev = NULL;
Andy Fleminge1393452005-08-24 18:46:21 -05001362 phydev->attached_dev = NULL;
Russell King9525ae82017-07-25 15:03:13 +01001363 phydev->phylink = NULL;
Andy Fleminge1393452005-08-24 18:46:21 -05001364
Geert Uytterhoeven0075bd62016-11-28 15:18:31 +01001365 phy_led_triggers_unregister(phydev);
1366
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001367 module_put(phydev->mdio.dev.driver->owner);
1368
Andy Fleminge1393452005-08-24 18:46:21 -05001369 /* If the device had no specific driver before (i.e. - it
1370 * was using the generic driver), we unbind the device
1371 * from the generic driver so that there's a chance a
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001372 * real driver could be loaded
1373 */
Florian Fainelli5db5ea92019-01-15 15:09:35 -08001374 if (phy_driver_is_genphy(phydev) ||
1375 phy_driver_is_genphy_10g(phydev))
Russell King921690f2017-06-05 12:23:05 +01001376 device_release_driver(&phydev->mdio.dev);
Russell King3e3aaf62015-09-24 20:36:02 +01001377
Russell King73229672015-09-24 20:36:08 +01001378 /*
1379 * The phydev might go away on the put_device() below, so avoid
1380 * a use-after-free bug by reading the underlying bus first.
1381 */
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001382 bus = phydev->mdio.bus;
Russell King3e3aaf62015-09-24 20:36:02 +01001383
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001384 put_device(&phydev->mdio.dev);
Florian Fainelliec988ad2016-12-06 20:54:43 -08001385 if (ndev_owner != bus->owner)
1386 module_put(bus->owner);
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01001387
1388 /* Assert the reset signal */
1389 phy_device_reset(phydev, 1);
Andy Fleminge1393452005-08-24 18:46:21 -05001390}
1391EXPORT_SYMBOL(phy_detach);
1392
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001393int phy_suspend(struct phy_device *phydev)
1394{
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001395 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
Heiner Kallweit93f41e62018-09-24 22:01:32 +02001396 struct net_device *netdev = phydev->attached_dev;
Sebastian Hesselbarth32fc3fd2014-03-14 10:07:44 +01001397 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Florian Fainelli8a477a62015-01-26 22:05:39 -08001398 int ret = 0;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001399
1400 /* If the device has WOL enabled, we cannot suspend the PHY */
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001401 phy_ethtool_get_wol(phydev, &wol);
Heiner Kallweit93f41e62018-09-24 22:01:32 +02001402 if (wol.wolopts || (netdev && netdev->wol_enabled))
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001403 return -EBUSY;
1404
Florian Fainelli25149ef2017-02-17 16:07:34 -08001405 if (phydev->drv && phydrv->suspend)
Florian Fainelli8a477a62015-01-26 22:05:39 -08001406 ret = phydrv->suspend(phydev);
1407
1408 if (ret)
1409 return ret;
1410
1411 phydev->suspended = true;
1412
1413 return ret;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001414}
Florian Fainellica127692014-07-08 10:38:36 -07001415EXPORT_SYMBOL(phy_suspend);
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001416
Andrew Lunn9c2c2e62018-02-27 01:56:06 +01001417int __phy_resume(struct phy_device *phydev)
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001418{
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001419 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
Florian Fainelli8a477a62015-01-26 22:05:39 -08001420 int ret = 0;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001421
Russell Kingf5e64032017-12-12 10:45:36 +00001422 WARN_ON(!mutex_is_locked(&phydev->lock));
1423
Florian Fainelli25149ef2017-02-17 16:07:34 -08001424 if (phydev->drv && phydrv->resume)
Florian Fainelli8a477a62015-01-26 22:05:39 -08001425 ret = phydrv->resume(phydev);
1426
1427 if (ret)
1428 return ret;
1429
1430 phydev->suspended = false;
1431
1432 return ret;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001433}
Andrew Lunn9c2c2e62018-02-27 01:56:06 +01001434EXPORT_SYMBOL(__phy_resume);
1435
1436int phy_resume(struct phy_device *phydev)
1437{
1438 int ret;
1439
1440 mutex_lock(&phydev->lock);
1441 ret = __phy_resume(phydev);
1442 mutex_unlock(&phydev->lock);
1443
1444 return ret;
1445}
Florian Fainellica127692014-07-08 10:38:36 -07001446EXPORT_SYMBOL(phy_resume);
Andy Fleminge1393452005-08-24 18:46:21 -05001447
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08001448int phy_loopback(struct phy_device *phydev, bool enable)
1449{
1450 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1451 int ret = 0;
1452
1453 mutex_lock(&phydev->lock);
1454
1455 if (enable && phydev->loopback_enabled) {
1456 ret = -EBUSY;
1457 goto out;
1458 }
1459
1460 if (!enable && !phydev->loopback_enabled) {
1461 ret = -EINVAL;
1462 goto out;
1463 }
1464
1465 if (phydev->drv && phydrv->set_loopback)
1466 ret = phydrv->set_loopback(phydev, enable);
1467 else
1468 ret = -EOPNOTSUPP;
1469
1470 if (ret)
1471 goto out;
1472
1473 phydev->loopback_enabled = enable;
1474
1475out:
1476 mutex_unlock(&phydev->lock);
1477 return ret;
1478}
1479EXPORT_SYMBOL(phy_loopback);
1480
Richard Leitnera9668492017-12-11 13:16:58 +01001481/**
1482 * phy_reset_after_clk_enable - perform a PHY reset if needed
1483 * @phydev: target phy_device struct
1484 *
1485 * Description: Some PHYs are known to need a reset after their refclk was
1486 * enabled. This function evaluates the flags and perform the reset if it's
1487 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
1488 * was reset.
1489 */
1490int phy_reset_after_clk_enable(struct phy_device *phydev)
1491{
1492 if (!phydev || !phydev->drv)
1493 return -ENODEV;
1494
1495 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1496 phy_device_reset(phydev, 1);
1497 phy_device_reset(phydev, 0);
1498 return 1;
1499 }
1500
1501 return 0;
1502}
1503EXPORT_SYMBOL(phy_reset_after_clk_enable);
1504
Andy Fleming00db8182005-07-30 19:31:23 -04001505/* Generic PHY support and helper functions */
1506
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001507/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001508 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001509 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001510 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001511 * Description: Writes MII_ADVERTISE with the appropriate values,
Andy Fleming00db8182005-07-30 19:31:23 -04001512 * after sanitizing the values to make sure we only advertise
Trent Piepho51e2a382008-09-24 10:55:46 +00001513 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1514 * hasn't changed, and > 0 if it has changed.
Andy Fleming00db8182005-07-30 19:31:23 -04001515 */
stephen hemminger89ff05e2010-10-21 08:37:41 +00001516static int genphy_config_advert(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -04001517{
1518 u32 advertise;
Heiner Kallweit4f9744e2019-02-10 19:59:57 +01001519 int bmsr, adv;
Trent Piepho51e2a382008-09-24 10:55:46 +00001520 int err, changed = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001521
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001522 /* Only allow advertising what this PHY supports */
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001523 linkmode_and(phydev->advertising, phydev->advertising,
1524 phydev->supported);
1525 if (!ethtool_convert_link_mode_to_legacy_u32(&advertise,
1526 phydev->advertising))
1527 phydev_warn(phydev, "PHY advertising (%*pb) more modes than genphy supports, some modes not advertised.\n",
1528 __ETHTOOL_LINK_MODE_MASK_NBITS,
1529 phydev->advertising);
Andy Fleming00db8182005-07-30 19:31:23 -04001530
1531 /* Setup standard advertisement */
Heiner Kallweit4f9744e2019-02-10 19:59:57 +01001532 err = phy_modify_changed(phydev, MII_ADVERTISE,
1533 ADVERTISE_ALL | ADVERTISE_100BASE4 |
1534 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
1535 ethtool_adv_to_mii_adv_t(advertise));
1536 if (err < 0)
1537 return err;
1538 if (err > 0)
Trent Piepho51e2a382008-09-24 10:55:46 +00001539 changed = 1;
Andy Fleming00db8182005-07-30 19:31:23 -04001540
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001541 bmsr = phy_read(phydev, MII_BMSR);
1542 if (bmsr < 0)
1543 return bmsr;
1544
1545 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1546 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1547 * logical 1.
1548 */
1549 if (!(bmsr & BMSR_ESTATEN))
1550 return changed;
1551
Andy Fleming00db8182005-07-30 19:31:23 -04001552 /* Configure gigabit if it's supported */
Heiner Kallweit4f9744e2019-02-10 19:59:57 +01001553 adv = 0;
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001554 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1555 phydev->supported) ||
1556 linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1557 phydev->supported))
Heiner Kallweit4f9744e2019-02-10 19:59:57 +01001558 adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Andy Fleming00db8182005-07-30 19:31:23 -04001559
Heiner Kallweit4f9744e2019-02-10 19:59:57 +01001560 err = phy_modify_changed(phydev, MII_CTRL1000,
1561 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
1562 adv);
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001563 if (err < 0)
1564 return err;
Heiner Kallweit4f9744e2019-02-10 19:59:57 +01001565 if (err > 0)
1566 changed = 1;
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001567
Trent Piepho51e2a382008-09-24 10:55:46 +00001568 return changed;
Andy Fleming00db8182005-07-30 19:31:23 -04001569}
Andy Fleming00db8182005-07-30 19:31:23 -04001570
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001571/**
jbrunetd853d142016-11-28 10:46:46 +01001572 * genphy_config_eee_advert - disable unwanted eee mode advertisement
1573 * @phydev: target phy_device struct
1574 *
1575 * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
1576 * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
1577 * changed, and 1 if it has changed.
1578 */
1579static int genphy_config_eee_advert(struct phy_device *phydev)
1580{
jbrunet3bb9ab62016-12-19 16:05:36 +01001581 int broken = phydev->eee_broken_modes;
1582 int old_adv, adv;
jbrunetd853d142016-11-28 10:46:46 +01001583
1584 /* Nothing to disable */
1585 if (!broken)
1586 return 0;
1587
1588 /* If the following call fails, we assume that EEE is not
1589 * supported by the phy. If we read 0, EEE is not advertised
1590 * In both case, we don't need to continue
1591 */
Russell Kinga6d99fc2017-03-21 16:36:53 +00001592 adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
jbrunetd853d142016-11-28 10:46:46 +01001593 if (adv <= 0)
1594 return 0;
1595
1596 old_adv = adv;
1597 adv &= ~broken;
1598
1599 /* Advertising remains unchanged with the broken mask */
1600 if (old_adv == adv)
1601 return 0;
1602
Russell Kinga6d99fc2017-03-21 16:36:53 +00001603 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
jbrunetd853d142016-11-28 10:46:46 +01001604
1605 return 1;
1606}
1607
1608/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001609 * genphy_setup_forced - configures/forces speed/duplex from @phydev
1610 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001611 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001612 * Description: Configures MII_BMCR to force speed/duplex
Andy Fleming00db8182005-07-30 19:31:23 -04001613 * to the values in phydev. Assumes that the values are valid.
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001614 * Please see phy_sanitize_settings().
1615 */
Madalin Bucur3fb69bc2013-11-20 16:38:19 -06001616int genphy_setup_forced(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -04001617{
Russell Kingfea23fb2018-01-02 10:58:58 +00001618 u16 ctl = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001619
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001620 phydev->pause = 0;
1621 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001622
1623 if (SPEED_1000 == phydev->speed)
1624 ctl |= BMCR_SPEED1000;
1625 else if (SPEED_100 == phydev->speed)
1626 ctl |= BMCR_SPEED100;
1627
1628 if (DUPLEX_FULL == phydev->duplex)
1629 ctl |= BMCR_FULLDPLX;
Florian Fainellie1093742013-12-17 21:38:12 -08001630
Russell Kingfea23fb2018-01-02 10:58:58 +00001631 return phy_modify(phydev, MII_BMCR,
Ingo van Lil18a5b052018-02-12 12:02:52 +01001632 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
Andy Fleming00db8182005-07-30 19:31:23 -04001633}
Madalin Bucur3fb69bc2013-11-20 16:38:19 -06001634EXPORT_SYMBOL(genphy_setup_forced);
Andy Fleming00db8182005-07-30 19:31:23 -04001635
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001636/**
1637 * genphy_restart_aneg - Enable and Restart Autonegotiation
1638 * @phydev: target phy_device struct
1639 */
Andy Fleming00db8182005-07-30 19:31:23 -04001640int genphy_restart_aneg(struct phy_device *phydev)
1641{
Andy Fleming00db8182005-07-30 19:31:23 -04001642 /* Don't isolate the PHY if we're negotiating */
Russell Kingf1028522018-01-05 16:07:10 +00001643 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
Russell Kingfea23fb2018-01-02 10:58:58 +00001644 BMCR_ANENABLE | BMCR_ANRESTART);
Andy Fleming00db8182005-07-30 19:31:23 -04001645}
Adrian Bunk892871d2008-10-13 18:48:09 -07001646EXPORT_SYMBOL(genphy_restart_aneg);
Andy Fleming00db8182005-07-30 19:31:23 -04001647
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001648/**
1649 * genphy_config_aneg - restart auto-negotiation or write BMCR
1650 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001651 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001652 * Description: If auto-negotiation is enabled, we configure the
Andy Fleming00db8182005-07-30 19:31:23 -04001653 * advertising, and then restart auto-negotiation. If it is not
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001654 * enabled, then we write the BMCR.
Andy Fleming00db8182005-07-30 19:31:23 -04001655 */
1656int genphy_config_aneg(struct phy_device *phydev)
1657{
jbrunetd853d142016-11-28 10:46:46 +01001658 int err, changed;
1659
1660 changed = genphy_config_eee_advert(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04001661
Trent Piephode339c22008-11-19 15:52:41 -08001662 if (AUTONEG_ENABLE != phydev->autoneg)
1663 return genphy_setup_forced(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04001664
jbrunetd853d142016-11-28 10:46:46 +01001665 err = genphy_config_advert(phydev);
1666 if (err < 0) /* error */
1667 return err;
1668
1669 changed |= err;
1670
1671 if (changed == 0) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001672 /* Advertisement hasn't changed, but maybe aneg was never on to
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001673 * begin with? Or maybe phy was isolated?
1674 */
Trent Piephode339c22008-11-19 15:52:41 -08001675 int ctl = phy_read(phydev, MII_BMCR);
1676
1677 if (ctl < 0)
1678 return ctl;
1679
1680 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
jbrunetd853d142016-11-28 10:46:46 +01001681 changed = 1; /* do restart aneg */
Trent Piephode339c22008-11-19 15:52:41 -08001682 }
1683
1684 /* Only restart aneg if we are advertising something different
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001685 * than we were before.
1686 */
jbrunetd853d142016-11-28 10:46:46 +01001687 if (changed > 0)
1688 return genphy_restart_aneg(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04001689
jbrunetd853d142016-11-28 10:46:46 +01001690 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001691}
1692EXPORT_SYMBOL(genphy_config_aneg);
1693
Florian Fainellia9fa6e62014-02-11 17:27:36 -08001694/**
1695 * genphy_aneg_done - return auto-negotiation status
1696 * @phydev: target phy_device struct
1697 *
1698 * Description: Reads the status register and returns 0 either if
1699 * auto-negotiation is incomplete, or if there was an error.
1700 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
1701 */
1702int genphy_aneg_done(struct phy_device *phydev)
1703{
1704 int retval = phy_read(phydev, MII_BMSR);
1705
1706 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1707}
1708EXPORT_SYMBOL(genphy_aneg_done);
1709
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001710/**
1711 * genphy_update_link - update link status in @phydev
1712 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001713 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001714 * Description: Update the value in phydev->link to reflect the
Andy Fleming00db8182005-07-30 19:31:23 -04001715 * current link value. In order to do this, we need to read
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001716 * the status register twice, keeping the second value.
Andy Fleming00db8182005-07-30 19:31:23 -04001717 */
1718int genphy_update_link(struct phy_device *phydev)
1719{
1720 int status;
1721
Heiner Kallweit93c09702019-02-06 19:39:52 +01001722 /* The link state is latched low so that momentary link
1723 * drops can be detected. Do not double-read the status
1724 * in polling mode to detect such short link drops.
1725 */
1726 if (!phy_polling_mode(phydev)) {
1727 status = phy_read(phydev, MII_BMSR);
Heiner Kallweitc397ab22019-02-07 20:22:20 +01001728 if (status < 0) {
Heiner Kallweit93c09702019-02-06 19:39:52 +01001729 return status;
Heiner Kallweitc397ab22019-02-07 20:22:20 +01001730 } else if (status & BMSR_LSTATUS) {
1731 phydev->link = 1;
1732 return 0;
1733 }
Heiner Kallweit93c09702019-02-06 19:39:52 +01001734 }
Andy Fleming00db8182005-07-30 19:31:23 -04001735
1736 /* Read link and autonegotiation status */
1737 status = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -04001738 if (status < 0)
1739 return status;
1740
1741 if ((status & BMSR_LSTATUS) == 0)
1742 phydev->link = 0;
1743 else
1744 phydev->link = 1;
1745
1746 return 0;
1747}
Andy Fleming6b655522006-10-16 16:19:17 -05001748EXPORT_SYMBOL(genphy_update_link);
Andy Fleming00db8182005-07-30 19:31:23 -04001749
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001750/**
1751 * genphy_read_status - check the link status and update current link state
1752 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001753 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001754 * Description: Check the link, then figure out the current state
Andy Fleming00db8182005-07-30 19:31:23 -04001755 * by comparing what we advertise with what the link partner
1756 * advertises. Start by checking the gigabit possibilities,
1757 * then move on to 10/100.
1758 */
1759int genphy_read_status(struct phy_device *phydev)
1760{
1761 int adv;
1762 int err;
1763 int lpa;
1764 int lpagb = 0;
Cristian Bercarua4572e02014-02-25 10:22:48 +02001765 int common_adv;
1766 int common_adv_gb = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001767
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001768 /* Update the link, but return if there was an error */
Andy Fleming00db8182005-07-30 19:31:23 -04001769 err = genphy_update_link(phydev);
1770 if (err)
1771 return err;
1772
Andrew Lunnc0ec3c22018-11-10 23:43:34 +01001773 linkmode_zero(phydev->lp_advertising);
Florian Fainelli114002b2013-12-06 13:01:30 -08001774
Andy Fleming00db8182005-07-30 19:31:23 -04001775 if (AUTONEG_ENABLE == phydev->autoneg) {
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001776 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1777 phydev->supported) ||
1778 linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1779 phydev->supported)) {
Andy Fleming00db8182005-07-30 19:31:23 -04001780 lpagb = phy_read(phydev, MII_STAT1000);
Andy Fleming00db8182005-07-30 19:31:23 -04001781 if (lpagb < 0)
1782 return lpagb;
1783
1784 adv = phy_read(phydev, MII_CTRL1000);
Andy Fleming00db8182005-07-30 19:31:23 -04001785 if (adv < 0)
1786 return adv;
1787
Heiner Kallweitb8f8c8e2018-07-21 15:48:47 +02001788 if (lpagb & LPA_1000MSFAIL) {
1789 if (adv & CTL1000_ENABLE_MASTER)
1790 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
1791 else
1792 phydev_err(phydev, "Master/Slave resolution failed\n");
1793 return -ENOLINK;
1794 }
1795
Andrew Lunn78a24df2018-12-05 21:49:41 +01001796 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
1797 lpagb);
Cristian Bercarua4572e02014-02-25 10:22:48 +02001798 common_adv_gb = lpagb & adv << 2;
Andy Fleming00db8182005-07-30 19:31:23 -04001799 }
1800
1801 lpa = phy_read(phydev, MII_LPA);
Andy Fleming00db8182005-07-30 19:31:23 -04001802 if (lpa < 0)
1803 return lpa;
1804
Andrew Lunnd3351932018-12-05 21:49:43 +01001805 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
Florian Fainelli114002b2013-12-06 13:01:30 -08001806
Andy Fleming00db8182005-07-30 19:31:23 -04001807 adv = phy_read(phydev, MII_ADVERTISE);
Andy Fleming00db8182005-07-30 19:31:23 -04001808 if (adv < 0)
1809 return adv;
1810
Cristian Bercarua4572e02014-02-25 10:22:48 +02001811 common_adv = lpa & adv;
Andy Fleming00db8182005-07-30 19:31:23 -04001812
1813 phydev->speed = SPEED_10;
1814 phydev->duplex = DUPLEX_HALF;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001815 phydev->pause = 0;
1816 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001817
Cristian Bercarua4572e02014-02-25 10:22:48 +02001818 if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) {
Andy Fleming00db8182005-07-30 19:31:23 -04001819 phydev->speed = SPEED_1000;
1820
Cristian Bercarua4572e02014-02-25 10:22:48 +02001821 if (common_adv_gb & LPA_1000FULL)
Andy Fleming00db8182005-07-30 19:31:23 -04001822 phydev->duplex = DUPLEX_FULL;
Cristian Bercarua4572e02014-02-25 10:22:48 +02001823 } else if (common_adv & (LPA_100FULL | LPA_100HALF)) {
Andy Fleming00db8182005-07-30 19:31:23 -04001824 phydev->speed = SPEED_100;
Florian Fainellie1093742013-12-17 21:38:12 -08001825
Cristian Bercarua4572e02014-02-25 10:22:48 +02001826 if (common_adv & LPA_100FULL)
Andy Fleming00db8182005-07-30 19:31:23 -04001827 phydev->duplex = DUPLEX_FULL;
1828 } else
Cristian Bercarua4572e02014-02-25 10:22:48 +02001829 if (common_adv & LPA_10FULL)
Andy Fleming00db8182005-07-30 19:31:23 -04001830 phydev->duplex = DUPLEX_FULL;
1831
Florian Fainellie1093742013-12-17 21:38:12 -08001832 if (phydev->duplex == DUPLEX_FULL) {
Andy Fleming00db8182005-07-30 19:31:23 -04001833 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
1834 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
1835 }
1836 } else {
1837 int bmcr = phy_read(phydev, MII_BMCR);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001838
Andy Fleming00db8182005-07-30 19:31:23 -04001839 if (bmcr < 0)
1840 return bmcr;
1841
1842 if (bmcr & BMCR_FULLDPLX)
1843 phydev->duplex = DUPLEX_FULL;
1844 else
1845 phydev->duplex = DUPLEX_HALF;
1846
1847 if (bmcr & BMCR_SPEED1000)
1848 phydev->speed = SPEED_1000;
1849 else if (bmcr & BMCR_SPEED100)
1850 phydev->speed = SPEED_100;
1851 else
1852 phydev->speed = SPEED_10;
1853
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001854 phydev->pause = 0;
1855 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001856 }
1857
1858 return 0;
1859}
1860EXPORT_SYMBOL(genphy_read_status);
1861
Florian Fainelli797ac072014-02-17 13:34:02 -08001862/**
1863 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
1864 * @phydev: target phy_device struct
1865 *
1866 * Description: Perform a software PHY reset using the standard
1867 * BMCR_RESET bit and poll for the reset bit to be cleared.
1868 *
1869 * Returns: 0 on success, < 0 on failure
1870 */
1871int genphy_soft_reset(struct phy_device *phydev)
1872{
1873 int ret;
1874
1875 ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
1876 if (ret < 0)
1877 return ret;
1878
1879 return phy_poll_reset(phydev);
1880}
1881EXPORT_SYMBOL(genphy_soft_reset);
1882
Daniel Mackaf6b6962014-04-16 17:19:12 +02001883int genphy_config_init(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -04001884{
Eric Sesterhenn84c22d72006-09-25 16:39:22 -07001885 int val;
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001886 __ETHTOOL_DECLARE_LINK_MODE_MASK(features) = { 0, };
Andy Fleming00db8182005-07-30 19:31:23 -04001887
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001888 linkmode_set_bit_array(phy_basic_ports_array,
1889 ARRAY_SIZE(phy_basic_ports_array),
1890 features);
1891 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, features);
1892 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, features);
Andy Fleming00db8182005-07-30 19:31:23 -04001893
1894 /* Do we support autonegotiation? */
1895 val = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -04001896 if (val < 0)
1897 return val;
1898
1899 if (val & BMSR_ANEGCAPABLE)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001900 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, features);
Andy Fleming00db8182005-07-30 19:31:23 -04001901
1902 if (val & BMSR_100FULL)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001903 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, features);
Andy Fleming00db8182005-07-30 19:31:23 -04001904 if (val & BMSR_100HALF)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001905 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, features);
Andy Fleming00db8182005-07-30 19:31:23 -04001906 if (val & BMSR_10FULL)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001907 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, features);
Andy Fleming00db8182005-07-30 19:31:23 -04001908 if (val & BMSR_10HALF)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001909 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, features);
Andy Fleming00db8182005-07-30 19:31:23 -04001910
1911 if (val & BMSR_ESTATEN) {
1912 val = phy_read(phydev, MII_ESTATUS);
Andy Fleming00db8182005-07-30 19:31:23 -04001913 if (val < 0)
1914 return val;
1915
1916 if (val & ESTATUS_1000_TFULL)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001917 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1918 features);
Andy Fleming00db8182005-07-30 19:31:23 -04001919 if (val & ESTATUS_1000_THALF)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001920 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1921 features);
Andy Fleming00db8182005-07-30 19:31:23 -04001922 }
1923
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001924 linkmode_and(phydev->supported, phydev->supported, features);
1925 linkmode_and(phydev->advertising, phydev->advertising, features);
Andy Fleming00db8182005-07-30 19:31:23 -04001926
1927 return 0;
1928}
Uwe Kleine-Königa0a32d32017-05-24 00:26:07 +02001929EXPORT_SYMBOL(genphy_config_init);
Andy Fleming124059f2014-01-10 14:27:37 +08001930
Kevin Hao5df7af82018-03-20 09:44:52 +08001931/* This is used for the phy device which doesn't support the MMD extended
1932 * register access, but it does have side effect when we are trying to access
1933 * the MMD register via indirect method.
1934 */
1935int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
1936{
1937 return -EOPNOTSUPP;
1938}
1939EXPORT_SYMBOL(genphy_read_mmd_unsupported);
1940
1941int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
1942 u16 regnum, u16 val)
1943{
1944 return -EOPNOTSUPP;
1945}
1946EXPORT_SYMBOL(genphy_write_mmd_unsupported);
1947
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001948int genphy_suspend(struct phy_device *phydev)
1949{
Heiner Kallweit032f4702018-01-12 21:20:36 +01001950 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001951}
1952EXPORT_SYMBOL(genphy_suspend);
1953
1954int genphy_resume(struct phy_device *phydev)
1955{
Heiner Kallweit032f4702018-01-12 21:20:36 +01001956 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08001957}
1958EXPORT_SYMBOL(genphy_resume);
Andy Fleming00db8182005-07-30 19:31:23 -04001959
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08001960int genphy_loopback(struct phy_device *phydev, bool enable)
1961{
Russell Kingf1028522018-01-05 16:07:10 +00001962 return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
Russell Kingfea23fb2018-01-02 10:58:58 +00001963 enable ? BMCR_LOOPBACK : 0);
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08001964}
1965EXPORT_SYMBOL(genphy_loopback);
1966
Simon Hormanf3a6bd32015-09-30 15:15:52 +09001967static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
1968{
Simon Hormanf3a6bd32015-09-30 15:15:52 +09001969 switch (max_speed) {
Heiner Kallweit6915bf32018-12-03 08:04:57 +01001970 case SPEED_10:
1971 linkmode_clear_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
1972 phydev->supported);
1973 linkmode_clear_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1974 phydev->supported);
Simon Hormanf3a6bd32015-09-30 15:15:52 +09001975 /* fall through */
1976 case SPEED_100:
Heiner Kallweit6915bf32018-12-03 08:04:57 +01001977 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1978 phydev->supported);
1979 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1980 phydev->supported);
1981 break;
1982 case SPEED_1000:
1983 break;
1984 default:
1985 return -ENOTSUPP;
Simon Hormanf3a6bd32015-09-30 15:15:52 +09001986 }
1987
1988 return 0;
1989}
1990
1991int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
1992{
1993 int err;
1994
1995 err = __set_phy_supported(phydev, max_speed);
1996 if (err)
1997 return err;
1998
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001999 linkmode_copy(phydev->advertising, phydev->supported);
Simon Hormanf3a6bd32015-09-30 15:15:52 +09002000
2001 return 0;
2002}
2003EXPORT_SYMBOL(phy_set_max_speed);
2004
Andrew Lunn41124fa2018-09-12 01:53:14 +02002005/**
2006 * phy_remove_link_mode - Remove a supported link mode
2007 * @phydev: phy_device structure to remove link mode from
2008 * @link_mode: Link mode to be removed
2009 *
2010 * Description: Some MACs don't support all link modes which the PHY
2011 * does. e.g. a 1G MAC often does not support 1000Half. Add a helper
2012 * to remove a link mode.
2013 */
2014void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
2015{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002016 linkmode_clear_bit(link_mode, phydev->supported);
2017 linkmode_copy(phydev->advertising, phydev->supported);
Andrew Lunn41124fa2018-09-12 01:53:14 +02002018}
2019EXPORT_SYMBOL(phy_remove_link_mode);
2020
Andrew Lunnaf8d9bb2018-09-12 01:53:15 +02002021/**
Andrew Lunnc306ad32018-09-12 01:53:16 +02002022 * phy_support_sym_pause - Enable support of symmetrical pause
2023 * @phydev: target phy_device struct
2024 *
2025 * Description: Called by the MAC to indicate is supports symmetrical
2026 * Pause, but not asym pause.
2027 */
2028void phy_support_sym_pause(struct phy_device *phydev)
2029{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002030 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
2031 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
2032 linkmode_copy(phydev->advertising, phydev->supported);
Andrew Lunnc306ad32018-09-12 01:53:16 +02002033}
2034EXPORT_SYMBOL(phy_support_sym_pause);
2035
2036/**
Andrew Lunnaf8d9bb2018-09-12 01:53:15 +02002037 * phy_support_asym_pause - Enable support of asym pause
2038 * @phydev: target phy_device struct
2039 *
2040 * Description: Called by the MAC to indicate is supports Asym Pause.
2041 */
2042void phy_support_asym_pause(struct phy_device *phydev)
2043{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002044 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
2045 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
2046 linkmode_copy(phydev->advertising, phydev->supported);
Andrew Lunnaf8d9bb2018-09-12 01:53:15 +02002047}
2048EXPORT_SYMBOL(phy_support_asym_pause);
2049
Andrew Lunn70814e82018-09-12 01:53:17 +02002050/**
Andrew Lunn0c122402018-09-12 01:53:18 +02002051 * phy_set_sym_pause - Configure symmetric Pause
2052 * @phydev: target phy_device struct
2053 * @rx: Receiver Pause is supported
2054 * @tx: Transmit Pause is supported
2055 * @autoneg: Auto neg should be used
2056 *
2057 * Description: Configure advertised Pause support depending on if
2058 * receiver pause and pause auto neg is supported. Generally called
2059 * from the set_pauseparam .ndo.
2060 */
2061void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
2062 bool autoneg)
2063{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002064 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
Andrew Lunn0c122402018-09-12 01:53:18 +02002065
2066 if (rx && tx && autoneg)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002067 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2068 phydev->supported);
Andrew Lunn0c122402018-09-12 01:53:18 +02002069
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002070 linkmode_copy(phydev->advertising, phydev->supported);
Andrew Lunn0c122402018-09-12 01:53:18 +02002071}
2072EXPORT_SYMBOL(phy_set_sym_pause);
2073
2074/**
Andrew Lunn70814e82018-09-12 01:53:17 +02002075 * phy_set_asym_pause - Configure Pause and Asym Pause
2076 * @phydev: target phy_device struct
2077 * @rx: Receiver Pause is supported
2078 * @tx: Transmit Pause is supported
2079 *
2080 * Description: Configure advertised Pause support depending on if
2081 * transmit and receiver pause is supported. If there has been a
2082 * change in adverting, trigger a new autoneg. Generally called from
2083 * the set_pauseparam .ndo.
2084 */
2085void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
2086{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002087 __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
Andrew Lunn70814e82018-09-12 01:53:17 +02002088
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002089 linkmode_copy(oldadv, phydev->advertising);
Andrew Lunn70814e82018-09-12 01:53:17 +02002090
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002091 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2092 phydev->advertising);
2093 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2094 phydev->advertising);
Andrew Lunn70814e82018-09-12 01:53:17 +02002095
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002096 if (rx) {
2097 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2098 phydev->advertising);
2099 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2100 phydev->advertising);
Andrew Lunn70814e82018-09-12 01:53:17 +02002101 }
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002102
2103 if (tx)
2104 linkmode_change_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2105 phydev->advertising);
2106
2107 if (!linkmode_equal(oldadv, phydev->advertising) &&
2108 phydev->autoneg)
2109 phy_start_aneg(phydev);
Andrew Lunn70814e82018-09-12 01:53:17 +02002110}
2111EXPORT_SYMBOL(phy_set_asym_pause);
2112
Andrew Lunn22b7d292018-09-12 01:53:19 +02002113/**
2114 * phy_validate_pause - Test if the PHY/MAC support the pause configuration
2115 * @phydev: phy_device struct
2116 * @pp: requested pause configuration
2117 *
2118 * Description: Test if the PHY/MAC combination supports the Pause
2119 * configuration the user is requesting. Returns True if it is
2120 * supported, false otherwise.
2121 */
2122bool phy_validate_pause(struct phy_device *phydev,
2123 struct ethtool_pauseparam *pp)
2124{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002125 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2126 phydev->supported) ||
2127 (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2128 phydev->supported) &&
Andrew Lunn22b7d292018-09-12 01:53:19 +02002129 pp->rx_pause != pp->tx_pause))
2130 return false;
2131 return true;
2132}
2133EXPORT_SYMBOL(phy_validate_pause);
2134
Sascha Hauerde906af2014-05-21 15:29:45 +02002135static void of_set_phy_supported(struct phy_device *phydev)
2136{
Andrew Lunne5a03bf2016-01-06 20:11:16 +01002137 struct device_node *node = phydev->mdio.dev.of_node;
Sascha Hauerde906af2014-05-21 15:29:45 +02002138 u32 max_speed;
2139
2140 if (!IS_ENABLED(CONFIG_OF_MDIO))
2141 return;
2142
2143 if (!node)
2144 return;
2145
Simon Hormanf3a6bd32015-09-30 15:15:52 +09002146 if (!of_property_read_u32(node, "max-speed", &max_speed))
2147 __set_phy_supported(phydev, max_speed);
Sascha Hauerde906af2014-05-21 15:29:45 +02002148}
2149
jbrunetd853d142016-11-28 10:46:46 +01002150static void of_set_phy_eee_broken(struct phy_device *phydev)
2151{
2152 struct device_node *node = phydev->mdio.dev.of_node;
jbrunet57f39862016-12-19 16:05:37 +01002153 u32 broken = 0;
jbrunetd853d142016-11-28 10:46:46 +01002154
2155 if (!IS_ENABLED(CONFIG_OF_MDIO))
2156 return;
2157
2158 if (!node)
2159 return;
2160
jbrunet57f39862016-12-19 16:05:37 +01002161 if (of_property_read_bool(node, "eee-broken-100tx"))
2162 broken |= MDIO_EEE_100TX;
2163 if (of_property_read_bool(node, "eee-broken-1000t"))
2164 broken |= MDIO_EEE_1000T;
2165 if (of_property_read_bool(node, "eee-broken-10gt"))
2166 broken |= MDIO_EEE_10GT;
2167 if (of_property_read_bool(node, "eee-broken-1000kx"))
2168 broken |= MDIO_EEE_1000KX;
2169 if (of_property_read_bool(node, "eee-broken-10gkx4"))
2170 broken |= MDIO_EEE_10GKX4;
2171 if (of_property_read_bool(node, "eee-broken-10gkr"))
2172 broken |= MDIO_EEE_10GKR;
2173
2174 phydev->eee_broken_modes = broken;
jbrunetd853d142016-11-28 10:46:46 +01002175}
2176
Heiner Kallweit0d2e7782018-11-09 18:16:28 +01002177static bool phy_drv_supports_irq(struct phy_driver *phydrv)
2178{
Heiner Kallweita21ff3c2018-11-12 21:16:06 +01002179 return phydrv->config_intr && phydrv->ack_interrupt;
Heiner Kallweit0d2e7782018-11-09 18:16:28 +01002180}
2181
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002182/**
2183 * phy_probe - probe and init a PHY device
2184 * @dev: device to probe and init
Andy Fleming00db8182005-07-30 19:31:23 -04002185 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002186 * Description: Take care of setting up the phy_device structure,
Andy Fleming00db8182005-07-30 19:31:23 -04002187 * set the state to READY (the driver's init function should
2188 * set it to STARTING if needed).
2189 */
2190static int phy_probe(struct device *dev)
2191{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03002192 struct phy_device *phydev = to_phy_device(dev);
Andrew Lunne5a03bf2016-01-06 20:11:16 +01002193 struct device_driver *drv = phydev->mdio.dev.driver;
Sergei Shtylyov553fe922014-01-05 03:23:19 +03002194 struct phy_driver *phydrv = to_phy_driver(drv);
Andy Fleming00db8182005-07-30 19:31:23 -04002195 int err = 0;
2196
Andy Fleming00db8182005-07-30 19:31:23 -04002197 phydev->drv = phydrv;
2198
Florian Fainelli2c7b4922013-05-19 22:53:42 +00002199 /* Disable the interrupt if the PHY doesn't support it
2200 * but the interrupt is still a valid one
2201 */
Heiner Kallweit0d2e7782018-11-09 18:16:28 +01002202 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
Andy Fleming00db8182005-07-30 19:31:23 -04002203 phydev->irq = PHY_POLL;
2204
Florian Fainelli4284b6a2013-05-23 01:11:12 +00002205 if (phydrv->flags & PHY_IS_INTERNAL)
2206 phydev->is_internal = true;
2207
Nate Case35b5f6b2008-01-29 10:05:09 -06002208 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04002209
Andrew Lunn92ed2eb2019-02-09 14:46:30 +01002210 if (phydev->drv->probe) {
2211 /* Deassert the reset signal */
2212 phy_device_reset(phydev, 0);
2213
2214 err = phydev->drv->probe(phydev);
2215 if (err) {
2216 /* Assert the reset signal */
2217 phy_device_reset(phydev, 1);
2218 goto out;
2219 }
2220 }
2221
Andy Fleming00db8182005-07-30 19:31:23 -04002222 /* Start out supporting everything. Eventually,
2223 * a controller will attach, and may modify one
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03002224 * or both of these values
2225 */
Andrew Lunnefbdfdc2019-02-09 15:24:47 +01002226 if (phydrv->features) {
2227 linkmode_copy(phydev->supported, phydrv->features);
2228 } else {
2229 err = phydrv->get_features(phydev);
2230 if (err)
2231 goto out;
2232 }
2233
Sascha Hauerde906af2014-05-21 15:29:45 +02002234 of_set_phy_supported(phydev);
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002235 linkmode_copy(phydev->advertising, phydev->supported);
Andy Fleming00db8182005-07-30 19:31:23 -04002236
jbrunetd853d142016-11-28 10:46:46 +01002237 /* Get the EEE modes we want to prohibit. We will ask
2238 * the PHY stop advertising these mode later on
2239 */
2240 of_set_phy_eee_broken(phydev);
2241
Timur Tabi529ed122016-12-07 13:20:51 -06002242 /* The Pause Frame bits indicate that the PHY can support passing
2243 * pause frames. During autonegotiation, the PHYs will determine if
2244 * they should allow pause frames to pass. The MAC driver should then
2245 * use that result to determine whether to enable flow control via
2246 * pause frames.
2247 *
2248 * Normally, PHY drivers should not set the Pause bits, and instead
2249 * allow phylib to do that. However, there may be some situations
2250 * (e.g. hardware erratum) where the driver wants to set only one
2251 * of these bits.
2252 */
Andrew Lunnefbdfdc2019-02-09 15:24:47 +01002253 if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
2254 !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002255 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2256 phydev->supported);
2257 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2258 phydev->supported);
Timur Tabi529ed122016-12-07 13:20:51 -06002259 }
2260
Andy Fleming00db8182005-07-30 19:31:23 -04002261 /* Set the state to READY by default */
2262 phydev->state = PHY_READY;
2263
Andrew Lunn92ed2eb2019-02-09 14:46:30 +01002264out:
Nate Case35b5f6b2008-01-29 10:05:09 -06002265 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04002266
Andy Fleming00db8182005-07-30 19:31:23 -04002267 return err;
2268}
2269
2270static int phy_remove(struct device *dev)
2271{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03002272 struct phy_device *phydev = to_phy_device(dev);
Andy Fleming00db8182005-07-30 19:31:23 -04002273
Florian Fainelli7b9a88a2017-02-17 16:07:33 -08002274 cancel_delayed_work_sync(&phydev->state_queue);
2275
Nate Case35b5f6b2008-01-29 10:05:09 -06002276 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04002277 phydev->state = PHY_DOWN;
Nate Case35b5f6b2008-01-29 10:05:09 -06002278 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04002279
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01002280 if (phydev->drv && phydev->drv->remove) {
Andy Fleming00db8182005-07-30 19:31:23 -04002281 phydev->drv->remove(phydev);
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01002282
2283 /* Assert the reset signal */
2284 phy_device_reset(phydev, 1);
2285 }
Andy Fleming00db8182005-07-30 19:31:23 -04002286 phydev->drv = NULL;
2287
2288 return 0;
2289}
2290
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002291/**
2292 * phy_driver_register - register a phy_driver with the PHY layer
2293 * @new_driver: new phy_driver to register
Andrew Lunnbe01da72016-01-06 20:11:22 +01002294 * @owner: module owning this PHY
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002295 */
Andrew Lunnbe01da72016-01-06 20:11:22 +01002296int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
Andy Fleming00db8182005-07-30 19:31:23 -04002297{
2298 int retval;
2299
Andrew Lunnefbdfdc2019-02-09 15:24:47 +01002300 /* Either the features are hard coded, or dynamically
2301 * determine. It cannot be both or neither
2302 */
2303 if (WARN_ON((!new_driver->features && !new_driver->get_features) ||
2304 (new_driver->features && new_driver->get_features))) {
Camelia Groza3e64cf72019-01-17 14:22:36 +02002305 pr_err("%s: Driver features are missing\n", new_driver->name);
2306 return -EINVAL;
2307 }
2308
Andrew Lunna9049e02016-01-06 20:11:26 +01002309 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
2310 new_driver->mdiodrv.driver.name = new_driver->name;
2311 new_driver->mdiodrv.driver.bus = &mdio_bus_type;
2312 new_driver->mdiodrv.driver.probe = phy_probe;
2313 new_driver->mdiodrv.driver.remove = phy_remove;
2314 new_driver->mdiodrv.driver.owner = owner;
Andy Fleming00db8182005-07-30 19:31:23 -04002315
Andrew Lunna9049e02016-01-06 20:11:26 +01002316 retval = driver_register(&new_driver->mdiodrv.driver);
Andy Fleming00db8182005-07-30 19:31:23 -04002317 if (retval) {
Joe Perches8d242482012-06-09 07:49:07 +00002318 pr_err("%s: Error %d in registering driver\n",
2319 new_driver->name, retval);
Andy Fleming00db8182005-07-30 19:31:23 -04002320
2321 return retval;
2322 }
2323
Olof Johanssonf2511f12007-11-04 16:09:23 -06002324 pr_debug("%s: Registered new driver\n", new_driver->name);
Andy Fleming00db8182005-07-30 19:31:23 -04002325
2326 return 0;
2327}
2328EXPORT_SYMBOL(phy_driver_register);
2329
Andrew Lunnbe01da72016-01-06 20:11:22 +01002330int phy_drivers_register(struct phy_driver *new_driver, int n,
2331 struct module *owner)
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00002332{
2333 int i, ret = 0;
2334
2335 for (i = 0; i < n; i++) {
Andrew Lunnbe01da72016-01-06 20:11:22 +01002336 ret = phy_driver_register(new_driver + i, owner);
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00002337 if (ret) {
2338 while (i-- > 0)
2339 phy_driver_unregister(new_driver + i);
2340 break;
2341 }
2342 }
2343 return ret;
2344}
2345EXPORT_SYMBOL(phy_drivers_register);
2346
Andy Fleming00db8182005-07-30 19:31:23 -04002347void phy_driver_unregister(struct phy_driver *drv)
2348{
Andrew Lunna9049e02016-01-06 20:11:26 +01002349 driver_unregister(&drv->mdiodrv.driver);
Andy Fleming00db8182005-07-30 19:31:23 -04002350}
2351EXPORT_SYMBOL(phy_driver_unregister);
2352
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00002353void phy_drivers_unregister(struct phy_driver *drv, int n)
2354{
2355 int i;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03002356
2357 for (i = 0; i < n; i++)
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00002358 phy_driver_unregister(drv + i);
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00002359}
2360EXPORT_SYMBOL(phy_drivers_unregister);
2361
Russell King921690f2017-06-05 12:23:05 +01002362static struct phy_driver genphy_driver = {
Andy Fleminge1393452005-08-24 18:46:21 -05002363 .phy_id = 0xffffffff,
2364 .phy_id_mask = 0xffffffff,
2365 .name = "Generic PHY",
Florian Fainelli0878fff2017-03-05 12:34:49 -08002366 .soft_reset = genphy_no_soft_reset,
Andy Fleminge1393452005-08-24 18:46:21 -05002367 .config_init = genphy_config_init,
Andrew Lunn719655a2018-09-29 23:04:16 +02002368 .features = PHY_GBIT_ALL_PORTS_FEATURES,
Florian Fainelli76a423a2014-02-11 17:27:37 -08002369 .aneg_done = genphy_aneg_done,
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08002370 .suspend = genphy_suspend,
2371 .resume = genphy_resume,
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08002372 .set_loopback = genphy_loopback,
Russell King921690f2017-06-05 12:23:05 +01002373};
Andy Fleming00db8182005-07-30 19:31:23 -04002374
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002375static int __init phy_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -04002376{
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002377 int rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002378
2379 rc = mdio_bus_init();
2380 if (rc)
Andy Fleminge1393452005-08-24 18:46:21 -05002381 return rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002382
Andrew Lunn719655a2018-09-29 23:04:16 +02002383 features_init();
2384
Russell King921690f2017-06-05 12:23:05 +01002385 rc = phy_driver_register(&genphy_10g_driver, THIS_MODULE);
Andy Fleminge1393452005-08-24 18:46:21 -05002386 if (rc)
Russell King921690f2017-06-05 12:23:05 +01002387 goto err_10g;
2388
2389 rc = phy_driver_register(&genphy_driver, THIS_MODULE);
2390 if (rc) {
2391 phy_driver_unregister(&genphy_10g_driver);
2392err_10g:
Andy Fleminge1393452005-08-24 18:46:21 -05002393 mdio_bus_exit();
Russell King921690f2017-06-05 12:23:05 +01002394 }
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002395
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002396 return rc;
Andy Fleming00db8182005-07-30 19:31:23 -04002397}
2398
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002399static void __exit phy_exit(void)
Andy Fleming00db8182005-07-30 19:31:23 -04002400{
Russell King921690f2017-06-05 12:23:05 +01002401 phy_driver_unregister(&genphy_10g_driver);
2402 phy_driver_unregister(&genphy_driver);
Andy Fleminge1393452005-08-24 18:46:21 -05002403 mdio_bus_exit();
Andy Fleming00db8182005-07-30 19:31:23 -04002404}
2405
Andy Fleminge1393452005-08-24 18:46:21 -05002406subsys_initcall(phy_init);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04002407module_exit(phy_exit);