blob: 495d86b4af7cf42858d5d77f7ba7b4fabe27e1b5 [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
Bartosz Golaszewskie42bcd02020-06-26 17:53:22 +020012#include <linux/bitmap.h>
Andy Fleming00db8182005-07-30 19:31:23 -040013#include <linux/delay.h>
Bartosz Golaszewskie42bcd02020-06-26 17:53:22 +020014#include <linux/errno.h>
Andy Fleming00db8182005-07-30 19:31:23 -040015#include <linux/etherdevice.h>
Bartosz Golaszewskie42bcd02020-06-26 17:53:22 +020016#include <linux/ethtool.h>
17#include <linux/init.h>
18#include <linux/interrupt.h>
19#include <linux/io.h>
20#include <linux/kernel.h>
21#include <linux/mdio.h>
22#include <linux/mii.h>
Andy Fleming00db8182005-07-30 19:31:23 -040023#include <linux/mm.h>
24#include <linux/module.h>
Bartosz Golaszewskie42bcd02020-06-26 17:53:22 +020025#include <linux/netdevice.h>
Andy Fleming00db8182005-07-30 19:31:23 -040026#include <linux/phy.h>
Zach Brown2e0bc452016-10-17 10:49:55 -050027#include <linux/phy_led_triggers.h>
Dan Murphy92252ee2020-06-24 07:16:02 -050028#include <linux/property.h>
Bartosz Golaszewskie42bcd02020-06-26 17:53:22 +020029#include <linux/sfp.h>
30#include <linux/skbuff.h>
31#include <linux/slab.h>
32#include <linux/string.h>
33#include <linux/uaccess.h>
34#include <linux/unistd.h>
Andy Fleming00db8182005-07-30 19:31:23 -040035
Olaf Heringafcceaa2005-12-14 00:33:49 +010036MODULE_DESCRIPTION("PHY library");
37MODULE_AUTHOR("Andy Fleming");
38MODULE_LICENSE("GPL");
39
Andrew Lunn719655a2018-09-29 23:04:16 +020040__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
41EXPORT_SYMBOL_GPL(phy_basic_features);
42
43__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
44EXPORT_SYMBOL_GPL(phy_basic_t1_features);
45
46__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
47EXPORT_SYMBOL_GPL(phy_gbit_features);
48
49__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
50EXPORT_SYMBOL_GPL(phy_gbit_fibre_features);
51
52__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
53EXPORT_SYMBOL_GPL(phy_gbit_all_ports_features);
54
55__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
56EXPORT_SYMBOL_GPL(phy_10gbit_features);
57
Andrew Lunn9e857a42019-01-15 16:55:30 +010058__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
59EXPORT_SYMBOL_GPL(phy_10gbit_fec_features);
60
Denis Efremov54638c62019-07-10 21:03:24 +030061const int phy_basic_ports_array[3] = {
Andrew Lunn719655a2018-09-29 23:04:16 +020062 ETHTOOL_LINK_MODE_Autoneg_BIT,
63 ETHTOOL_LINK_MODE_TP_BIT,
64 ETHTOOL_LINK_MODE_MII_BIT,
65};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010066EXPORT_SYMBOL_GPL(phy_basic_ports_array);
Andrew Lunn719655a2018-09-29 23:04:16 +020067
Denis Efremov54638c62019-07-10 21:03:24 +030068const int phy_fibre_port_array[1] = {
Andrew Lunn719655a2018-09-29 23:04:16 +020069 ETHTOOL_LINK_MODE_FIBRE_BIT,
70};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010071EXPORT_SYMBOL_GPL(phy_fibre_port_array);
Andrew Lunn719655a2018-09-29 23:04:16 +020072
Denis Efremov54638c62019-07-10 21:03:24 +030073const int phy_all_ports_features_array[7] = {
Andrew Lunn719655a2018-09-29 23:04:16 +020074 ETHTOOL_LINK_MODE_Autoneg_BIT,
75 ETHTOOL_LINK_MODE_TP_BIT,
76 ETHTOOL_LINK_MODE_MII_BIT,
77 ETHTOOL_LINK_MODE_FIBRE_BIT,
78 ETHTOOL_LINK_MODE_AUI_BIT,
79 ETHTOOL_LINK_MODE_BNC_BIT,
80 ETHTOOL_LINK_MODE_Backplane_BIT,
81};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010082EXPORT_SYMBOL_GPL(phy_all_ports_features_array);
Andrew Lunn719655a2018-09-29 23:04:16 +020083
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010084const int phy_10_100_features_array[4] = {
Andrew Lunn719655a2018-09-29 23:04:16 +020085 ETHTOOL_LINK_MODE_10baseT_Half_BIT,
86 ETHTOOL_LINK_MODE_10baseT_Full_BIT,
87 ETHTOOL_LINK_MODE_100baseT_Half_BIT,
88 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
89};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010090EXPORT_SYMBOL_GPL(phy_10_100_features_array);
Andrew Lunn719655a2018-09-29 23:04:16 +020091
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010092const int phy_basic_t1_features_array[2] = {
Andrew Lunn719655a2018-09-29 23:04:16 +020093 ETHTOOL_LINK_MODE_TP_BIT,
Andrew Lunne5fb32c2019-05-22 20:47:04 +020094 ETHTOOL_LINK_MODE_100baseT1_Full_BIT,
Andrew Lunn719655a2018-09-29 23:04:16 +020095};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010096EXPORT_SYMBOL_GPL(phy_basic_t1_features_array);
Andrew Lunn719655a2018-09-29 23:04:16 +020097
Andrew Lunn3c1bcc82018-11-10 23:43:33 +010098const int phy_gbit_features_array[2] = {
Andrew Lunn719655a2018-09-29 23:04:16 +020099 ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
100 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
101};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100102EXPORT_SYMBOL_GPL(phy_gbit_features_array);
Andrew Lunn719655a2018-09-29 23:04:16 +0200103
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100104const int phy_10gbit_features_array[1] = {
Andrew Lunn719655a2018-09-29 23:04:16 +0200105 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
106};
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100107EXPORT_SYMBOL_GPL(phy_10gbit_features_array);
Andrew Lunn719655a2018-09-29 23:04:16 +0200108
Andrew Lunn4f2b38e2020-07-07 03:49:36 +0200109static const int phy_10gbit_fec_features_array[1] = {
Andrew Lunn9e857a42019-01-15 16:55:30 +0100110 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
111};
Andrew Lunn9e857a42019-01-15 16:55:30 +0100112
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;
Grant Likely4dea5472009-04-25 12:52:46 +0000229
Andy Flemingf62220d2008-04-18 17:29:54 -0500230static LIST_HEAD(phy_fixup_list);
231static DEFINE_MUTEX(phy_fixup_lock);
232
Andrew Lunnbc879222016-01-06 20:11:21 +0100233static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
234{
235 struct device_driver *drv = phydev->mdio.dev.driver;
236 struct phy_driver *phydrv = to_phy_driver(drv);
237 struct net_device *netdev = phydev->attached_dev;
238
239 if (!drv || !phydrv->suspend)
240 return false;
241
242 /* PHY not attached? May suspend if the PHY has not already been
243 * suspended as part of a prior call to phy_disconnect() ->
244 * phy_detach() -> phy_suspend() because the parent netdev might be the
245 * MDIO bus driver and clock gated at this point.
246 */
247 if (!netdev)
Florian Fainelli503ba7c2020-02-20 15:34:53 -0800248 goto out;
Andrew Lunnbc879222016-01-06 20:11:21 +0100249
Heiner Kallweit93f41e62018-09-24 22:01:32 +0200250 if (netdev->wol_enabled)
251 return false;
252
253 /* As long as not all affected network drivers support the
254 * wol_enabled flag, let's check for hints that WoL is enabled.
255 * Don't suspend PHY if the attached netdev parent may wake up.
Andrew Lunnbc879222016-01-06 20:11:21 +0100256 * The parent may point to a PCI device, as in tg3 driver.
257 */
258 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
259 return false;
260
261 /* Also don't suspend PHY if the netdev itself may wakeup. This
262 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
263 * e.g. SoC devices.
264 */
265 if (device_may_wakeup(&netdev->dev))
266 return false;
267
Florian Fainelli503ba7c2020-02-20 15:34:53 -0800268out:
269 return !phydev->suspended;
Andrew Lunnbc879222016-01-06 20:11:21 +0100270}
271
Arnd Bergmann7f654152021-02-25 15:57:27 +0100272static __maybe_unused int mdio_bus_phy_suspend(struct device *dev)
Andrew Lunnbc879222016-01-06 20:11:21 +0100273{
274 struct phy_device *phydev = to_phy_device(dev);
275
Heiner Kallweitfba863b2021-04-07 17:51:56 +0200276 if (phydev->mac_managed_pm)
277 return 0;
278
Andrew Lunnbc879222016-01-06 20:11:21 +0100279 /* We must stop the state machine manually, otherwise it stops out of
280 * control, possibly with the phydev->lock held. Upon resume, netdev
281 * may call phy routines that try to grab the same lock, and that may
282 * lead to a deadlock.
283 */
284 if (phydev->attached_dev && phydev->adjust_link)
285 phy_stop_machine(phydev);
286
287 if (!mdio_bus_phy_may_suspend(phydev))
288 return 0;
289
Heiner Kallweit611d7792020-03-12 22:25:20 +0100290 phydev->suspended_by_mdio_bus = 1;
291
Andrew Lunnbc879222016-01-06 20:11:21 +0100292 return phy_suspend(phydev);
293}
294
Arnd Bergmann7f654152021-02-25 15:57:27 +0100295static __maybe_unused int mdio_bus_phy_resume(struct device *dev)
Andrew Lunnbc879222016-01-06 20:11:21 +0100296{
297 struct phy_device *phydev = to_phy_device(dev);
298 int ret;
299
Heiner Kallweitfba863b2021-04-07 17:51:56 +0200300 if (phydev->mac_managed_pm)
301 return 0;
302
Heiner Kallweit611d7792020-03-12 22:25:20 +0100303 if (!phydev->suspended_by_mdio_bus)
Andrew Lunnbc879222016-01-06 20:11:21 +0100304 goto no_resume;
305
Heiner Kallweit611d7792020-03-12 22:25:20 +0100306 phydev->suspended_by_mdio_bus = 0;
307
Heiner Kallweit4c0d2e92021-02-11 22:32:52 +0100308 ret = phy_init_hw(phydev);
Andrew Lunnbc879222016-01-06 20:11:21 +0100309 if (ret < 0)
310 return ret;
311
Heiner Kallweit4c0d2e92021-02-11 22:32:52 +0100312 ret = phy_resume(phydev);
313 if (ret < 0)
314 return ret;
Andrew Lunnbc879222016-01-06 20:11:21 +0100315no_resume:
316 if (phydev->attached_dev && phydev->adjust_link)
317 phy_start_machine(phydev);
318
319 return 0;
320}
321
Heiner Kallweit4c0d2e92021-02-11 22:32:52 +0100322static SIMPLE_DEV_PM_OPS(mdio_bus_phy_pm_ops, mdio_bus_phy_suspend,
323 mdio_bus_phy_resume);
Andrew Lunnbc879222016-01-06 20:11:21 +0100324
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300325/**
326 * phy_register_fixup - creates a new phy_fixup and adds it to the list
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100327 * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
Andy Flemingf62220d2008-04-18 17:29:54 -0500328 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300329 * It can also be PHY_ANY_UID
Andy Flemingf62220d2008-04-18 17:29:54 -0500330 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300331 * comparison
Andy Flemingf62220d2008-04-18 17:29:54 -0500332 * @run: The actual code to be run when a matching PHY is found
333 */
334int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300335 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -0500336{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300337 struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
Andy Flemingf62220d2008-04-18 17:29:54 -0500338
Andy Flemingf62220d2008-04-18 17:29:54 -0500339 if (!fixup)
340 return -ENOMEM;
341
Kay Sieversfb28ad32008-11-10 13:55:14 -0800342 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
Andy Flemingf62220d2008-04-18 17:29:54 -0500343 fixup->phy_uid = phy_uid;
344 fixup->phy_uid_mask = phy_uid_mask;
345 fixup->run = run;
346
347 mutex_lock(&phy_fixup_lock);
348 list_add_tail(&fixup->list, &phy_fixup_list);
349 mutex_unlock(&phy_fixup_lock);
350
351 return 0;
352}
353EXPORT_SYMBOL(phy_register_fixup);
354
355/* Registers a fixup to be run on any PHY with the UID in phy_uid */
356int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300357 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -0500358{
359 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
360}
361EXPORT_SYMBOL(phy_register_fixup_for_uid);
362
363/* Registers a fixup to be run on the PHY with id string bus_id */
364int phy_register_fixup_for_id(const char *bus_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300365 int (*run)(struct phy_device *))
Andy Flemingf62220d2008-04-18 17:29:54 -0500366{
367 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
368}
369EXPORT_SYMBOL(phy_register_fixup_for_id);
370
Woojung.Huh@microchip.comf38e7a32016-12-07 20:26:07 +0000371/**
372 * phy_unregister_fixup - remove a phy_fixup from the list
373 * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
374 * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
375 * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
376 */
377int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
378{
379 struct list_head *pos, *n;
380 struct phy_fixup *fixup;
381 int ret;
382
383 ret = -ENODEV;
384
385 mutex_lock(&phy_fixup_lock);
386 list_for_each_safe(pos, n, &phy_fixup_list) {
387 fixup = list_entry(pos, struct phy_fixup, list);
388
389 if ((!strcmp(fixup->bus_id, bus_id)) &&
390 ((fixup->phy_uid & phy_uid_mask) ==
391 (phy_uid & phy_uid_mask))) {
392 list_del(&fixup->list);
393 kfree(fixup);
394 ret = 0;
395 break;
396 }
397 }
398 mutex_unlock(&phy_fixup_lock);
399
400 return ret;
401}
402EXPORT_SYMBOL(phy_unregister_fixup);
403
404/* Unregisters a fixup of any PHY with the UID in phy_uid */
405int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
406{
407 return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
408}
409EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
410
411/* Unregisters a fixup of the PHY with id string bus_id */
412int phy_unregister_fixup_for_id(const char *bus_id)
413{
414 return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
415}
416EXPORT_SYMBOL(phy_unregister_fixup_for_id);
417
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300418/* Returns 1 if fixup matches phydev in bus_id and phy_uid.
Andy Flemingf62220d2008-04-18 17:29:54 -0500419 * Fixups can be set to match any in one or more fields.
420 */
421static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
422{
Andrew Lunn84eff6d2016-01-06 20:11:10 +0100423 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
Andy Flemingf62220d2008-04-18 17:29:54 -0500424 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
425 return 0;
426
427 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300428 (phydev->phy_id & fixup->phy_uid_mask))
Andy Flemingf62220d2008-04-18 17:29:54 -0500429 if (fixup->phy_uid != PHY_ANY_UID)
430 return 0;
431
432 return 1;
433}
434
435/* Runs any matching fixups for this phydev */
Sergei Shtylyovfbfcec62014-01-05 03:28:27 +0300436static int phy_scan_fixups(struct phy_device *phydev)
Andy Flemingf62220d2008-04-18 17:29:54 -0500437{
438 struct phy_fixup *fixup;
439
440 mutex_lock(&phy_fixup_lock);
441 list_for_each_entry(fixup, &phy_fixup_list, list) {
442 if (phy_needs_fixup(phydev, fixup)) {
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300443 int err = fixup->run(phydev);
Andy Flemingf62220d2008-04-18 17:29:54 -0500444
Jiri Slabybc23283c2009-07-13 11:23:39 +0000445 if (err < 0) {
446 mutex_unlock(&phy_fixup_lock);
Andy Flemingf62220d2008-04-18 17:29:54 -0500447 return err;
Jiri Slabybc23283c2009-07-13 11:23:39 +0000448 }
Florian Fainellib0ae0092014-02-11 17:27:41 -0800449 phydev->has_fixups = true;
Andy Flemingf62220d2008-04-18 17:29:54 -0500450 }
451 }
452 mutex_unlock(&phy_fixup_lock);
453
454 return 0;
455}
Andy Flemingf62220d2008-04-18 17:29:54 -0500456
Andrew Lunne76a4952016-01-06 20:11:23 +0100457static int phy_bus_match(struct device *dev, struct device_driver *drv)
458{
459 struct phy_device *phydev = to_phy_device(dev);
460 struct phy_driver *phydrv = to_phy_driver(drv);
461 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
462 int i;
463
Andrew Lunna9049e02016-01-06 20:11:26 +0100464 if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
465 return 0;
466
Andrew Lunne76a4952016-01-06 20:11:23 +0100467 if (phydrv->match_phy_device)
468 return phydrv->match_phy_device(phydev);
469
470 if (phydev->is_c45) {
471 for (i = 1; i < num_ids; i++) {
Russell Kingb95e86d2019-11-15 20:08:37 +0000472 if (phydev->c45_ids.device_ids[i] == 0xffffffff)
Andrew Lunne76a4952016-01-06 20:11:23 +0100473 continue;
474
475 if ((phydrv->phy_id & phydrv->phy_id_mask) ==
476 (phydev->c45_ids.device_ids[i] &
477 phydrv->phy_id_mask))
478 return 1;
479 }
480 return 0;
481 } else {
482 return (phydrv->phy_id & phydrv->phy_id_mask) ==
483 (phydev->phy_id & phydrv->phy_id_mask);
484 }
485}
486
Heiner Kallweit7f4828f2018-06-02 22:36:06 +0200487static ssize_t
488phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
489{
490 struct phy_device *phydev = to_phy_device(dev);
491
492 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
493}
494static DEVICE_ATTR_RO(phy_id);
495
496static ssize_t
497phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
498{
499 struct phy_device *phydev = to_phy_device(dev);
500 const char *mode = NULL;
501
502 if (phy_is_internal(phydev))
503 mode = "internal";
504 else
505 mode = phy_modes(phydev->interface);
506
507 return sprintf(buf, "%s\n", mode);
508}
509static DEVICE_ATTR_RO(phy_interface);
510
511static ssize_t
512phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
513 char *buf)
514{
515 struct phy_device *phydev = to_phy_device(dev);
516
517 return sprintf(buf, "%d\n", phydev->has_fixups);
518}
519static DEVICE_ATTR_RO(phy_has_fixups);
520
Florian Fainellib0bade52021-03-10 14:12:43 -0800521static ssize_t phy_dev_flags_show(struct device *dev,
522 struct device_attribute *attr,
523 char *buf)
524{
525 struct phy_device *phydev = to_phy_device(dev);
526
527 return sprintf(buf, "0x%08x\n", phydev->dev_flags);
528}
529static DEVICE_ATTR_RO(phy_dev_flags);
530
Heiner Kallweit7f4828f2018-06-02 22:36:06 +0200531static struct attribute *phy_dev_attrs[] = {
532 &dev_attr_phy_id.attr,
533 &dev_attr_phy_interface.attr,
534 &dev_attr_phy_has_fixups.attr,
Florian Fainellib0bade52021-03-10 14:12:43 -0800535 &dev_attr_phy_dev_flags.attr,
Heiner Kallweit7f4828f2018-06-02 22:36:06 +0200536 NULL,
537};
538ATTRIBUTE_GROUPS(phy_dev);
539
540static const struct device_type mdio_bus_phy_type = {
541 .name = "PHY",
542 .groups = phy_dev_groups,
543 .release = phy_device_release,
Heiner Kallweit4c0d2e92021-02-11 22:32:52 +0100544 .pm = pm_ptr(&mdio_bus_phy_pm_ops),
Heiner Kallweit7f4828f2018-06-02 22:36:06 +0200545};
546
Russell King7d49a322019-12-19 23:24:52 +0000547static int phy_request_driver_module(struct phy_device *dev, u32 phy_id)
Heiner Kallweit13d0ab62019-01-16 08:07:38 +0100548{
549 int ret;
550
551 ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
552 MDIO_ID_ARGS(phy_id));
Heiner Kallweit21e19442019-01-19 10:30:21 +0100553 /* We only check for failures in executing the usermode binary,
554 * not whether a PHY driver module exists for the PHY ID.
555 * Accept -ENOENT because this may occur in case no initramfs exists,
556 * then modprobe isn't available.
Heiner Kallweit13d0ab62019-01-16 08:07:38 +0100557 */
Heiner Kallweit21e19442019-01-19 10:30:21 +0100558 if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) {
Russell King7d49a322019-12-19 23:24:52 +0000559 phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n",
560 ret, (unsigned long)phy_id);
Heiner Kallweit13d0ab62019-01-16 08:07:38 +0100561 return ret;
562 }
563
564 return 0;
565}
566
Russell King7d49a322019-12-19 23:24:52 +0000567struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300568 bool is_c45,
569 struct phy_c45_device_ids *c45_ids)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700570{
571 struct phy_device *dev;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100572 struct mdio_device *mdiodev;
Heiner Kallweit13d0ab62019-01-16 08:07:38 +0100573 int ret = 0;
David Woodhouse8626d3b2010-04-02 01:05:27 +0000574
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300575 /* We allocate the device, and initialize the default values */
Robert P. J. Daycd861282006-12-13 00:34:52 -0800576 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Sergei Shtylyovef899c02015-08-28 21:35:14 +0300577 if (!dev)
Sergei Shtylyovd3765f02015-08-28 21:34:34 +0300578 return ERR_PTR(-ENOMEM);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700579
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100580 mdiodev = &dev->mdio;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100581 mdiodev->dev.parent = &bus->dev;
582 mdiodev->dev.bus = &mdio_bus_type;
Heiner Kallweit7f4828f2018-06-02 22:36:06 +0200583 mdiodev->dev.type = &mdio_bus_phy_type;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100584 mdiodev->bus = bus;
Andrew Lunne76a4952016-01-06 20:11:23 +0100585 mdiodev->bus_match = phy_bus_match;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100586 mdiodev->addr = addr;
Andrew Lunn7f854422016-01-06 20:11:18 +0100587 mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
Andrew Lunn711fdba2016-01-06 20:11:27 +0100588 mdiodev->device_free = phy_mdio_device_free;
589 mdiodev->device_remove = phy_mdio_device_remove;
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300590
Russell Kinga5d66f82019-11-22 15:23:23 +0000591 dev->speed = SPEED_UNKNOWN;
592 dev->duplex = DUPLEX_UNKNOWN;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300593 dev->pause = 0;
594 dev->asym_pause = 0;
Heiner Kallweitc69851e2018-03-11 15:00:37 +0100595 dev->link = 0;
Michael Walle4217a642021-02-09 17:38:52 +0100596 dev->port = PORT_TP;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600597 dev->interface = PHY_INTERFACE_MODE_GMII;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700598
599 dev->autoneg = AUTONEG_ENABLE;
600
David Daneyac28b9f2012-06-27 07:33:35 +0000601 dev->is_c45 = is_c45;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700602 dev->phy_id = phy_id;
David Daneyac28b9f2012-06-27 07:33:35 +0000603 if (c45_ids)
604 dev->c45_ids = *c45_ids;
Dan Carpenter47b356e2016-01-12 12:36:21 +0300605 dev->irq = bus->irq[addr];
Johan Hovoldd02cbc42020-08-06 17:37:53 +0200606
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100607 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
Johan Hovoldd02cbc42020-08-06 17:37:53 +0200608 device_initialize(&mdiodev->dev);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700609
610 dev->state = PHY_DOWN;
611
Nate Case35b5f6b2008-01-29 10:05:09 -0600612 mutex_init(&dev->lock);
Anton Vorontsov4f9c85a2010-01-18 05:37:16 +0000613 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700614
David Woodhouse8626d3b2010-04-02 01:05:27 +0000615 /* Request the appropriate module unconditionally; don't
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300616 * bother trying to do so only if it isn't already loaded,
617 * because that gets complicated. A hotplug event would have
618 * done an unconditional modprobe anyway.
619 * We don't do normal hotplug because it won't work for MDIO
620 * -- because it relies on the device staying around for long
621 * enough for the driver to get loaded. With MDIO, the NIC
622 * driver will get bored and give up as soon as it finds that
623 * there's no driver _already_ loaded.
624 */
Jose Abreu30fcd6a2018-12-02 16:33:14 +0100625 if (is_c45 && c45_ids) {
626 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
627 int i;
628
629 for (i = 1; i < num_ids; i++) {
Russell Kingb95e86d2019-11-15 20:08:37 +0000630 if (c45_ids->device_ids[i] == 0xffffffff)
Jose Abreu30fcd6a2018-12-02 16:33:14 +0100631 continue;
632
Heiner Kallweit13d0ab62019-01-16 08:07:38 +0100633 ret = phy_request_driver_module(dev,
634 c45_ids->device_ids[i]);
635 if (ret)
636 break;
Jose Abreu30fcd6a2018-12-02 16:33:14 +0100637 }
638 } else {
Heiner Kallweit13d0ab62019-01-16 08:07:38 +0100639 ret = phy_request_driver_module(dev, phy_id);
Jose Abreu30fcd6a2018-12-02 16:33:14 +0100640 }
David Woodhouse8626d3b2010-04-02 01:05:27 +0000641
Johan Hovoldd02cbc42020-08-06 17:37:53 +0200642 if (ret) {
643 put_device(&mdiodev->dev);
Heiner Kallweit13d0ab62019-01-16 08:07:38 +0100644 dev = ERR_PTR(ret);
645 }
Petr Malatb2a43192013-02-28 01:01:52 +0000646
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700647 return dev;
648}
David Daneyac28b9f2012-06-27 07:33:35 +0000649EXPORT_SYMBOL(phy_device_create);
650
Russell Kingc7460532020-06-18 14:45:58 +0100651/* phy_c45_probe_present - checks to see if a MMD is present in the package
652 * @bus: the target MII bus
653 * @prtad: PHY package address on the MII bus
654 * @devad: PHY device (MMD) address
655 *
656 * Read the MDIO_STAT2 register, and check whether a device is responding
657 * at this address.
658 *
659 * Returns: negative error number on bus access error, zero if no device
660 * is responding, or positive if a device is present.
661 */
662static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad)
663{
664 int stat2;
665
666 stat2 = mdiobus_c45_read(bus, prtad, devad, MDIO_STAT2);
667 if (stat2 < 0)
668 return stat2;
669
670 return (stat2 & MDIO_STAT2_DEVPRST) == MDIO_STAT2_DEVPRST_VAL;
671}
672
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800673/* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
674 * @bus: the target MII bus
675 * @addr: PHY address on the MII bus
676 * @dev_addr: MMD address in the PHY.
677 * @devices_in_package: where to store the devices in package information.
678 *
679 * Description: reads devices in package registers of a MMD at @dev_addr
680 * from PHY at @addr on @bus.
681 *
682 * Returns: 0 on success, -EIO on failure.
683 */
684static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
685 u32 *devices_in_package)
686{
Russell King90ce6652020-05-26 16:29:36 +0100687 int phy_reg;
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800688
Russell King90ce6652020-05-26 16:29:36 +0100689 phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS2);
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800690 if (phy_reg < 0)
691 return -EIO;
Heiner Kallweit50684da2019-02-09 09:46:53 +0100692 *devices_in_package = phy_reg << 16;
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800693
Russell King90ce6652020-05-26 16:29:36 +0100694 phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS1);
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800695 if (phy_reg < 0)
696 return -EIO;
Heiner Kallweit50684da2019-02-09 09:46:53 +0100697 *devices_in_package |= phy_reg;
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800698
699 return 0;
700}
701
David Daneyac28b9f2012-06-27 07:33:35 +0000702/**
703 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
704 * @bus: the target MII bus
705 * @addr: PHY address on the MII bus
David Daneyac28b9f2012-06-27 07:33:35 +0000706 * @c45_ids: where to store the c45 ID information.
707 *
Russell King48c54382020-06-18 14:45:43 +0100708 * Read the PHY "devices in package". If this appears to be valid, read
709 * the PHY identifiers for each device. Return the "devices in package"
710 * and identifiers in @c45_ids.
David Daneyac28b9f2012-06-27 07:33:35 +0000711 *
Russell King48c54382020-06-18 14:45:43 +0100712 * Returns zero on success, %-EIO on bus access error, or %-ENODEV if
713 * the "devices in package" is invalid.
David Daneyac28b9f2012-06-27 07:33:35 +0000714 */
Russell King48c54382020-06-18 14:45:43 +0100715static int get_phy_c45_ids(struct mii_bus *bus, int addr,
Russell King90ce6652020-05-26 16:29:36 +0100716 struct phy_c45_device_ids *c45_ids)
717{
David Daneyac28b9f2012-06-27 07:33:35 +0000718 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
Russell King5ba33cf2020-06-18 14:46:03 +0100719 u32 devs_in_pkg = 0;
Russell Kingc7460532020-06-18 14:45:58 +0100720 int i, ret, phy_reg;
David Daneyac28b9f2012-06-27 07:33:35 +0000721
Shaohui Xie5f6c99e2015-11-03 12:27:33 +0800722 /* Find first non-zero Devices In package. Device zero is reserved
723 * for 802.3 c45 complied PHYs, so don't probe it at first.
David Daneyac28b9f2012-06-27 07:33:35 +0000724 */
Vladimir Olteanfb16d462020-07-20 20:26:54 +0300725 for (i = 1; i < MDIO_MMD_NUM && (devs_in_pkg == 0 ||
726 (devs_in_pkg & 0x1fffffff) == 0x1fffffff); i++) {
Russell Kingc7460532020-06-18 14:45:58 +0100727 if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) {
728 /* Check that there is a device present at this
729 * address before reading the devices-in-package
730 * register to avoid reading garbage from the PHY.
731 * Some PHYs (88x3310) vendor space is not IEEE802.3
732 * compliant.
733 */
734 ret = phy_c45_probe_present(bus, addr, i);
735 if (ret < 0)
736 return -EIO;
737
738 if (!ret)
739 continue;
740 }
Russell King5ba33cf2020-06-18 14:46:03 +0100741 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, &devs_in_pkg);
David Daneyac28b9f2012-06-27 07:33:35 +0000742 if (phy_reg < 0)
743 return -EIO;
Russell King454a78d2020-06-18 14:45:32 +0100744 }
David Daneyac28b9f2012-06-27 07:33:35 +0000745
Russell King5ba33cf2020-06-18 14:46:03 +0100746 if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff) {
Russell King454a78d2020-06-18 14:45:32 +0100747 /* If mostly Fs, there is no device there, then let's probe
748 * MMD 0, as some 10G PHYs have zero Devices In package,
749 * e.g. Cortina CS4315/CS4340 PHY.
750 */
Russell King5ba33cf2020-06-18 14:46:03 +0100751 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, &devs_in_pkg);
Russell King454a78d2020-06-18 14:45:32 +0100752 if (phy_reg < 0)
753 return -EIO;
754
755 /* no device there, let's get out of here */
Russell King5ba33cf2020-06-18 14:46:03 +0100756 if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff)
Russell King48c54382020-06-18 14:45:43 +0100757 return -ENODEV;
David Daneyac28b9f2012-06-27 07:33:35 +0000758 }
759
760 /* Now probe Device Identifiers for each device present. */
761 for (i = 1; i < num_ids; i++) {
Russell King5ba33cf2020-06-18 14:46:03 +0100762 if (!(devs_in_pkg & (1 << i)))
David Daneyac28b9f2012-06-27 07:33:35 +0000763 continue;
764
Russell King389a3382020-06-18 14:46:13 +0100765 if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) {
766 /* Probe the "Device Present" bits for the vendor MMDs
767 * to ignore these if they do not contain IEEE 802.3
768 * registers.
769 */
770 ret = phy_c45_probe_present(bus, addr, i);
771 if (ret < 0)
772 return ret;
773
774 if (!ret)
775 continue;
776 }
777
Russell King90ce6652020-05-26 16:29:36 +0100778 phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID1);
David Daneyac28b9f2012-06-27 07:33:35 +0000779 if (phy_reg < 0)
780 return -EIO;
Heiner Kallweit50684da2019-02-09 09:46:53 +0100781 c45_ids->device_ids[i] = phy_reg << 16;
David Daneyac28b9f2012-06-27 07:33:35 +0000782
Russell King90ce6652020-05-26 16:29:36 +0100783 phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID2);
David Daneyac28b9f2012-06-27 07:33:35 +0000784 if (phy_reg < 0)
785 return -EIO;
Heiner Kallweit50684da2019-02-09 09:46:53 +0100786 c45_ids->device_ids[i] |= phy_reg;
David Daneyac28b9f2012-06-27 07:33:35 +0000787 }
Russell King48c54382020-06-18 14:45:43 +0100788
Russell King5ba33cf2020-06-18 14:46:03 +0100789 c45_ids->devices_in_package = devs_in_pkg;
Russell King320ed3b2020-06-18 14:46:08 +0100790 /* Bit 0 doesn't represent a device, it indicates c22 regs presence */
791 c45_ids->mmds_present = devs_in_pkg & ~BIT(0);
Russell King5ba33cf2020-06-18 14:46:03 +0100792
David Daneyac28b9f2012-06-27 07:33:35 +0000793 return 0;
794}
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700795
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800796/**
Russell Kinge6306262020-06-18 14:45:37 +0100797 * get_phy_c22_id - reads the specified addr for its clause 22 ID.
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400798 * @bus: the target MII bus
799 * @addr: PHY address on the MII bus
800 * @phy_id: where to store the ID retrieved.
801 *
Russell Kingee951002020-06-18 14:45:48 +0100802 * Read the 802.3 clause 22 PHY ID from the PHY at @addr on the @bus,
803 * placing it in @phy_id. Return zero on successful read and the ID is
804 * valid, %-EIO on bus access error, or %-ENODEV if no device responds
805 * or invalid ID.
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400806 */
Russell Kinge6306262020-06-18 14:45:37 +0100807static int get_phy_c22_id(struct mii_bus *bus, int addr, u32 *phy_id)
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400808{
809 int phy_reg;
810
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300811 /* Grab the bits from PHYIR1, and put them in the upper half */
David Daney6fe32642011-09-30 11:51:22 +0000812 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
Alexandre Belloni02a6efc2018-04-24 18:09:04 +0200813 if (phy_reg < 0) {
Heiner Kallweitd8cce3a2019-01-16 19:52:51 +0100814 /* returning -ENODEV doesn't stop bus scanning */
815 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
Alexandre Belloni02a6efc2018-04-24 18:09:04 +0200816 }
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400817
Heiner Kallweit50684da2019-02-09 09:46:53 +0100818 *phy_id = phy_reg << 16;
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400819
820 /* Grab the bits from PHYIR2, and put them in the lower half */
David Daney6fe32642011-09-30 11:51:22 +0000821 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
Florian Fainellib2ffc752020-06-19 11:47:47 -0700822 if (phy_reg < 0) {
823 /* returning -ENODEV doesn't stop bus scanning */
824 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
825 }
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400826
Heiner Kallweit50684da2019-02-09 09:46:53 +0100827 *phy_id |= phy_reg;
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400828
Russell Kingee951002020-06-18 14:45:48 +0100829 /* If the phy_id is mostly Fs, there is no device there */
830 if ((*phy_id & 0x1fffffff) == 0x1fffffff)
831 return -ENODEV;
832
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400833 return 0;
834}
835
836/**
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300837 * get_phy_device - reads the specified PHY device and returns its @phy_device
838 * struct
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800839 * @bus: the target MII bus
840 * @addr: PHY address on the MII bus
David Daneyac28b9f2012-06-27 07:33:35 +0000841 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
Andy Fleming00db8182005-07-30 19:31:23 -0400842 *
Russell King439625a2020-06-18 14:45:53 +0100843 * Probe for a PHY at @addr on @bus.
844 *
845 * When probing for a clause 22 PHY, then read the ID registers. If we find
846 * a valid ID, allocate and return a &struct phy_device.
847 *
848 * When probing for a clause 45 PHY, read the "devices in package" registers.
849 * If the "devices in package" appears valid, read the ID registers for each
850 * MMD, allocate and return a &struct phy_device.
851 *
852 * Returns an allocated &struct phy_device on success, %-ENODEV if there is
853 * no PHY present, or %-EIO on bus access error.
Andy Fleming00db8182005-07-30 19:31:23 -0400854 */
David Daneyac28b9f2012-06-27 07:33:35 +0000855struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
Andy Fleming00db8182005-07-30 19:31:23 -0400856{
Russell Kingb95e86d2019-11-15 20:08:37 +0000857 struct phy_c45_device_ids c45_ids;
David S. Miller160c85f2012-06-27 21:28:14 -0700858 u32 phy_id = 0;
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400859 int r;
Andy Fleming00db8182005-07-30 19:31:23 -0400860
Russell Kingb95e86d2019-11-15 20:08:37 +0000861 c45_ids.devices_in_package = 0;
Russell King320ed3b2020-06-18 14:46:08 +0100862 c45_ids.mmds_present = 0;
Russell Kingb95e86d2019-11-15 20:08:37 +0000863 memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids));
864
Russell Kinge6306262020-06-18 14:45:37 +0100865 if (is_c45)
Russell King48c54382020-06-18 14:45:43 +0100866 r = get_phy_c45_ids(bus, addr, &c45_ids);
Russell Kinge6306262020-06-18 14:45:37 +0100867 else
868 r = get_phy_c22_id(bus, addr, &phy_id);
869
Paul Gortmakercac1f3c2008-04-15 12:49:21 -0400870 if (r)
871 return ERR_PTR(r);
Andy Fleming00db8182005-07-30 19:31:23 -0400872
Wong Vee Kheeb040aab2021-06-07 10:36:45 +0800873 /* PHY device such as the Marvell Alaska 88E2110 will return a PHY ID
874 * of 0 when probed using get_phy_c22_id() with no error. Proceed to
875 * probe with C45 to see if we're able to get a valid PHY ID in the C45
876 * space, if successful, create the C45 PHY device.
877 */
878 if (!is_c45 && phy_id == 0 && bus->probe_capabilities >= MDIOBUS_C45) {
879 r = get_phy_c45_ids(bus, addr, &c45_ids);
880 if (!r)
881 return phy_device_create(bus, addr, phy_id,
882 true, &c45_ids);
883 }
884
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300885 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
Andy Fleming00db8182005-07-30 19:31:23 -0400886}
Grant Likely4dea5472009-04-25 12:52:46 +0000887EXPORT_SYMBOL(get_phy_device);
888
889/**
890 * phy_device_register - Register the phy device on the MDIO bus
Randy Dunlap1d4ac5d2009-06-16 16:56:33 +0000891 * @phydev: phy_device structure to be added to the MDIO bus
Grant Likely4dea5472009-04-25 12:52:46 +0000892 */
893int phy_device_register(struct phy_device *phydev)
894{
895 int err;
896
Andrew Lunn7f854422016-01-06 20:11:18 +0100897 err = mdiobus_register_device(&phydev->mdio);
898 if (err)
899 return err;
Grant Likely4dea5472009-04-25 12:52:46 +0000900
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +0100901 /* Deassert the reset signal */
902 phy_device_reset(phydev, 0);
903
Grant Likely4dea5472009-04-25 12:52:46 +0000904 /* Run all of the fixups for this PHY */
Florian Fainellid92f5de2014-07-28 16:28:07 -0700905 err = phy_scan_fixups(phydev);
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800906 if (err) {
Heiner Kallweitc3a6a172019-01-15 21:50:25 +0100907 phydev_err(phydev, "failed to initialize\n");
Florian Fainelli87aa9f92013-12-06 13:01:34 -0800908 goto out;
909 }
Grant Likely4dea5472009-04-25 12:52:46 +0000910
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100911 err = device_add(&phydev->mdio.dev);
Grant Likely4dea5472009-04-25 12:52:46 +0000912 if (err) {
Heiner Kallweitc3a6a172019-01-15 21:50:25 +0100913 phydev_err(phydev, "failed to add\n");
Grant Likely4dea5472009-04-25 12:52:46 +0000914 goto out;
915 }
916
917 return 0;
918
919 out:
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +0100920 /* Assert the reset signal */
921 phy_device_reset(phydev, 1);
922
Andrew Lunn7f854422016-01-06 20:11:18 +0100923 mdiobus_unregister_device(&phydev->mdio);
Grant Likely4dea5472009-04-25 12:52:46 +0000924 return err;
925}
926EXPORT_SYMBOL(phy_device_register);
Andy Fleming00db8182005-07-30 19:31:23 -0400927
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800928/**
Russell King38737e42015-09-24 20:36:28 +0100929 * phy_device_remove - Remove a previously registered phy device from the MDIO bus
930 * @phydev: phy_device structure to remove
931 *
932 * This doesn't free the phy_device itself, it merely reverses the effects
933 * of phy_device_register(). Use phy_device_free() to free the device
934 * after calling this function.
935 */
936void phy_device_remove(struct phy_device *phydev)
937{
Richard Cochran1dca22b2019-12-25 18:16:18 -0800938 if (phydev->mii_ts)
939 unregister_mii_timestamper(phydev->mii_ts);
940
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100941 device_del(&phydev->mdio.dev);
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +0100942
943 /* Assert the reset signal */
944 phy_device_reset(phydev, 1);
945
Andrew Lunn7f854422016-01-06 20:11:18 +0100946 mdiobus_unregister_device(&phydev->mdio);
Russell King38737e42015-09-24 20:36:28 +0100947}
948EXPORT_SYMBOL(phy_device_remove);
949
950/**
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800951 * phy_find_first - finds the first PHY device on the bus
952 * @bus: the target MII bus
953 */
954struct phy_device *phy_find_first(struct mii_bus *bus)
955{
Andrew Lunn7f854422016-01-06 20:11:18 +0100956 struct phy_device *phydev;
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800957 int addr;
958
959 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
Andrew Lunn7f854422016-01-06 20:11:18 +0100960 phydev = mdiobus_get_phy(bus, addr);
961 if (phydev)
962 return phydev;
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800963 }
964 return NULL;
965}
966EXPORT_SYMBOL(phy_find_first);
967
Doug Bergera3075932020-05-18 15:23:59 -0700968static void phy_link_change(struct phy_device *phydev, bool up)
Russell Kinga81497b2017-07-25 15:02:58 +0100969{
970 struct net_device *netdev = phydev->attached_dev;
971
Doug Bergera3075932020-05-18 15:23:59 -0700972 if (up)
973 netif_carrier_on(netdev);
974 else
975 netif_carrier_off(netdev);
Russell Kinga81497b2017-07-25 15:02:58 +0100976 phydev->adjust_link(netdev);
Richard Cochran4715f652019-12-25 18:16:15 -0800977 if (phydev->mii_ts && phydev->mii_ts->link_state)
978 phydev->mii_ts->link_state(phydev->mii_ts, phydev);
Russell Kinga81497b2017-07-25 15:02:58 +0100979}
980
Jiri Pirkof8f76db2010-02-04 10:23:02 -0800981/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800982 * phy_prepare_link - prepares the PHY layer to monitor link status
983 * @phydev: target phy_device struct
984 * @handler: callback function for link status change notifications
Andy Fleming00db8182005-07-30 19:31:23 -0400985 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800986 * Description: Tells the PHY infrastructure to handle the
Andy Fleming00db8182005-07-30 19:31:23 -0400987 * gory details on monitoring link status (whether through
988 * polling or an interrupt), and to call back to the
989 * connected device driver when the link status changes.
990 * If you want to monitor your own link state, don't call
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800991 * this function.
992 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000993static void phy_prepare_link(struct phy_device *phydev,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300994 void (*handler)(struct net_device *))
Andy Fleming00db8182005-07-30 19:31:23 -0400995{
996 phydev->adjust_link = handler;
997}
998
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800999/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001000 * phy_connect_direct - connect an ethernet device to a specific phy_device
1001 * @dev: the network device to connect
1002 * @phydev: the pointer to the phy device
1003 * @handler: callback function for state change notifications
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001004 * @interface: PHY device's interface
1005 */
1006int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
Florian Fainellif9a8f832013-01-14 00:52:52 +00001007 void (*handler)(struct net_device *),
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001008 phy_interface_t interface)
1009{
1010 int rc;
1011
Ioana Ciornei82c76ac2019-05-28 20:38:09 +03001012 if (!dev)
1013 return -EINVAL;
1014
Florian Fainellif9a8f832013-01-14 00:52:52 +00001015 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001016 if (rc)
1017 return rc;
1018
1019 phy_prepare_link(phydev, handler);
Heiner Kallweit434a4312019-01-23 07:31:58 +01001020 if (phy_interrupt_is_valid(phydev))
1021 phy_request_interrupt(phydev);
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001022
1023 return 0;
1024}
1025EXPORT_SYMBOL(phy_connect_direct);
1026
1027/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001028 * phy_connect - connect an ethernet device to a PHY device
1029 * @dev: the network device to connect
Randy Dunlap5d12b132008-04-28 10:58:22 -07001030 * @bus_id: the id string of the PHY device to connect
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001031 * @handler: callback function for state change notifications
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001032 * @interface: PHY device's interface
Andy Fleminge1393452005-08-24 18:46:21 -05001033 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001034 * Description: Convenience function for connecting ethernet
Andy Fleminge1393452005-08-24 18:46:21 -05001035 * devices to PHY devices. The default behavior is for
1036 * the PHY infrastructure to handle everything, and only notify
1037 * the connected driver when the link status changes. If you
1038 * don't want, or can't use the provided functionality, you may
1039 * choose to call only the subset of functions which provide
1040 * the desired functionality.
1041 */
Florian Fainellie1093742013-12-17 21:38:12 -08001042struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001043 void (*handler)(struct net_device *),
1044 phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -05001045{
1046 struct phy_device *phydev;
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001047 struct device *d;
1048 int rc;
Andy Fleminge1393452005-08-24 18:46:21 -05001049
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001050 /* Search the list of PHY devices on the mdio bus for the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001051 * PHY with the requested name
1052 */
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001053 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
1054 if (!d) {
1055 pr_err("PHY %s not found\n", bus_id);
1056 return ERR_PTR(-ENODEV);
1057 }
1058 phydev = to_phy_device(d);
Andy Fleminge1393452005-08-24 18:46:21 -05001059
Florian Fainellif9a8f832013-01-14 00:52:52 +00001060 rc = phy_connect_direct(dev, phydev, handler, interface);
Johan Hovold17ae1c62016-11-03 18:40:19 +01001061 put_device(d);
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001062 if (rc)
1063 return ERR_PTR(rc);
Andy Fleminge1393452005-08-24 18:46:21 -05001064
1065 return phydev;
1066}
1067EXPORT_SYMBOL(phy_connect);
1068
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001069/**
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001070 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
1071 * device
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001072 * @phydev: target phy_device struct
1073 */
Andy Fleminge1393452005-08-24 18:46:21 -05001074void phy_disconnect(struct phy_device *phydev)
1075{
Heiner Kallweit472115d2019-01-17 20:07:54 +01001076 if (phy_is_started(phydev))
1077 phy_stop(phydev);
1078
Heiner Kallweitbb658ab2019-01-17 20:09:21 +01001079 if (phy_interrupt_is_valid(phydev))
Heiner Kallweit07b09282019-05-30 15:09:15 +02001080 phy_free_interrupt(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -05001081
Andy Fleminge1393452005-08-24 18:46:21 -05001082 phydev->adjust_link = NULL;
1083
1084 phy_detach(phydev);
1085}
1086EXPORT_SYMBOL(phy_disconnect);
1087
Florian Fainelli87aa9f92013-12-06 13:01:34 -08001088/**
1089 * phy_poll_reset - Safely wait until a PHY reset has properly completed
1090 * @phydev: The PHY device to poll
1091 *
1092 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
1093 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
1094 * register must be polled until the BMCR_RESET bit clears.
1095 *
1096 * Furthermore, any attempts to write to PHY registers may have no effect
1097 * or even generate MDIO bus errors until this is complete.
1098 *
1099 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
1100 * standard and do not fully reset after the BMCR_RESET bit is set, and may
1101 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
1102 * effort to support such broken PHYs, this function is separate from the
1103 * standard phy_init_hw() which will zero all the other bits in the BMCR
1104 * and reapply all driver-specific and board-specific fixups.
1105 */
1106static int phy_poll_reset(struct phy_device *phydev)
1107{
1108 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
Dejin Zheng745a2372020-03-23 23:05:58 +08001109 int ret, val;
Florian Fainelli87aa9f92013-12-06 13:01:34 -08001110
Dejin Zheng745a2372020-03-23 23:05:58 +08001111 ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET),
1112 50000, 600000, true);
1113 if (ret)
1114 return ret;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001115 /* Some chips (smsc911x) may still need up to another 1ms after the
Florian Fainelli87aa9f92013-12-06 13:01:34 -08001116 * BMCR_RESET bit is cleared before they are usable.
1117 */
1118 msleep(1);
1119 return 0;
1120}
1121
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001122int phy_init_hw(struct phy_device *phydev)
1123{
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001124 int ret = 0;
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001125
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01001126 /* Deassert the reset signal */
1127 phy_device_reset(phydev, 0);
1128
Heiner Kallweita5996982019-01-19 10:43:07 +01001129 if (!phydev->drv)
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001130 return 0;
1131
Heiner Kallweit9576e9f2020-04-23 21:38:42 +02001132 if (phydev->drv->soft_reset) {
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001133 ret = phydev->drv->soft_reset(phydev);
Heiner Kallweit9576e9f2020-04-23 21:38:42 +02001134 /* see comment in genphy_soft_reset for an explanation */
1135 if (!ret)
1136 phydev->suspended = 0;
1137 }
Florian Fainelli9df81dd2014-02-17 13:34:03 -08001138
Florian Fainelli87aa9f92013-12-06 13:01:34 -08001139 if (ret < 0)
1140 return ret;
1141
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001142 ret = phy_scan_fixups(phydev);
1143 if (ret < 0)
1144 return ret;
1145
Heiner Kallweit4c0d2e92021-02-11 22:32:52 +01001146 if (phydev->drv->config_init) {
Heiner Kallweita5996982019-01-19 10:43:07 +01001147 ret = phydev->drv->config_init(phydev);
Heiner Kallweit4c0d2e92021-02-11 22:32:52 +01001148 if (ret < 0)
1149 return ret;
1150 }
Heiner Kallweita5996982019-01-19 10:43:07 +01001151
Heiner Kallweit4c0d2e92021-02-11 22:32:52 +01001152 if (phydev->drv->config_intr) {
1153 ret = phydev->drv->config_intr(phydev);
1154 if (ret < 0)
1155 return ret;
1156 }
1157
1158 return 0;
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001159}
Florian Fainelli87aa9f92013-12-06 13:01:34 -08001160EXPORT_SYMBOL(phy_init_hw);
Anton Vorontsov2f5cb432009-12-30 08:23:30 +00001161
Andrew Lunn22209432016-01-06 20:11:13 +01001162void phy_attached_info(struct phy_device *phydev)
1163{
1164 phy_attached_print(phydev, NULL);
1165}
1166EXPORT_SYMBOL(phy_attached_info);
1167
Heiner Kallweita98cabd2020-11-15 16:03:10 +01001168#define ATTACHED_FMT "attached PHY driver %s(mii_bus:phy_addr=%s, irq=%s)"
Florian Fainellie27f1782020-01-12 09:35:38 -08001169char *phy_attached_info_irq(struct phy_device *phydev)
Andrew Lunn22209432016-01-06 20:11:13 +01001170{
Romain Perier5e369ae2017-09-04 10:41:36 +02001171 char *irq_str;
Geert Uytterhoeven059fbe82017-09-21 13:27:02 +02001172 char irq_num[8];
Romain Perier5e369ae2017-09-04 10:41:36 +02001173
1174 switch(phydev->irq) {
1175 case PHY_POLL:
1176 irq_str = "POLL";
1177 break;
Heiner Kallweit93e89902021-02-14 15:16:23 +01001178 case PHY_MAC_INTERRUPT:
1179 irq_str = "MAC";
Romain Perier5e369ae2017-09-04 10:41:36 +02001180 break;
1181 default:
1182 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
1183 irq_str = irq_num;
1184 break;
1185 }
1186
Florian Fainellie27f1782020-01-12 09:35:38 -08001187 return kasprintf(GFP_KERNEL, "%s", irq_str);
1188}
1189EXPORT_SYMBOL(phy_attached_info_irq);
1190
1191void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1192{
Heiner Kallweita98cabd2020-11-15 16:03:10 +01001193 const char *unbound = phydev->drv ? "" : "[unbound] ";
Florian Fainellie27f1782020-01-12 09:35:38 -08001194 char *irq_str = phy_attached_info_irq(phydev);
Florian Fainellifcd03e32017-08-22 14:26:47 -07001195
Andrew Lunn22209432016-01-06 20:11:13 +01001196 if (!fmt) {
Heiner Kallweita98cabd2020-11-15 16:03:10 +01001197 phydev_info(phydev, ATTACHED_FMT "\n", unbound,
1198 phydev_name(phydev), irq_str);
Andrew Lunn22209432016-01-06 20:11:13 +01001199 } else {
1200 va_list ap;
1201
Heiner Kallweita98cabd2020-11-15 16:03:10 +01001202 phydev_info(phydev, ATTACHED_FMT, unbound,
1203 phydev_name(phydev), irq_str);
Andrew Lunn22209432016-01-06 20:11:13 +01001204
1205 va_start(ap, fmt);
1206 vprintk(fmt, ap);
1207 va_end(ap);
1208 }
Florian Fainellie27f1782020-01-12 09:35:38 -08001209 kfree(irq_str);
Andrew Lunn22209432016-01-06 20:11:13 +01001210}
1211EXPORT_SYMBOL(phy_attached_print);
1212
Vladimir Oltean53cfca22019-05-28 20:38:07 +03001213static void phy_sysfs_create_links(struct phy_device *phydev)
1214{
1215 struct net_device *dev = phydev->attached_dev;
1216 int err;
1217
Ioana Ciornei2db2d9d12019-05-28 20:38:08 +03001218 if (!dev)
1219 return;
1220
Vladimir Oltean53cfca22019-05-28 20:38:07 +03001221 err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1222 "attached_dev");
1223 if (err)
1224 return;
1225
1226 err = sysfs_create_link_nowarn(&dev->dev.kobj,
1227 &phydev->mdio.dev.kobj,
1228 "phydev");
1229 if (err) {
1230 dev_err(&dev->dev, "could not add device link to %s err %d\n",
1231 kobject_name(&phydev->mdio.dev.kobj),
1232 err);
1233 /* non-fatal - some net drivers can use one netdevice
1234 * with more then one phy
1235 */
1236 }
1237
1238 phydev->sysfs_links = true;
1239}
1240
Ioana Ciorneic920f742019-05-28 20:38:10 +03001241static ssize_t
1242phy_standalone_show(struct device *dev, struct device_attribute *attr,
1243 char *buf)
1244{
1245 struct phy_device *phydev = to_phy_device(dev);
1246
1247 return sprintf(buf, "%d\n", !phydev->attached_dev);
1248}
1249static DEVICE_ATTR_RO(phy_standalone);
1250
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001251/**
Russell King298e54f2019-11-15 19:56:51 +00001252 * phy_sfp_attach - attach the SFP bus to the PHY upstream network device
1253 * @upstream: pointer to the phy device
1254 * @bus: sfp bus representing cage being attached
1255 *
1256 * This is used to fill in the sfp_upstream_ops .attach member.
1257 */
1258void phy_sfp_attach(void *upstream, struct sfp_bus *bus)
1259{
1260 struct phy_device *phydev = upstream;
1261
1262 if (phydev->attached_dev)
1263 phydev->attached_dev->sfp_bus = bus;
1264 phydev->sfp_bus_attached = true;
1265}
1266EXPORT_SYMBOL(phy_sfp_attach);
1267
1268/**
1269 * phy_sfp_detach - detach the SFP bus from the PHY upstream network device
1270 * @upstream: pointer to the phy device
1271 * @bus: sfp bus representing cage being attached
1272 *
1273 * This is used to fill in the sfp_upstream_ops .detach member.
1274 */
1275void phy_sfp_detach(void *upstream, struct sfp_bus *bus)
1276{
1277 struct phy_device *phydev = upstream;
1278
1279 if (phydev->attached_dev)
1280 phydev->attached_dev->sfp_bus = NULL;
1281 phydev->sfp_bus_attached = false;
1282}
1283EXPORT_SYMBOL(phy_sfp_detach);
1284
1285/**
1286 * phy_sfp_probe - probe for a SFP cage attached to this PHY device
1287 * @phydev: Pointer to phy_device
1288 * @ops: SFP's upstream operations
1289 */
1290int phy_sfp_probe(struct phy_device *phydev,
1291 const struct sfp_upstream_ops *ops)
1292{
1293 struct sfp_bus *bus;
Leon Romanovskye3f2d552020-05-17 14:53:40 +03001294 int ret = 0;
Russell King298e54f2019-11-15 19:56:51 +00001295
1296 if (phydev->mdio.dev.fwnode) {
1297 bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode);
1298 if (IS_ERR(bus))
1299 return PTR_ERR(bus);
1300
1301 phydev->sfp_bus = bus;
1302
1303 ret = sfp_bus_add_upstream(bus, phydev, ops);
1304 sfp_bus_put(bus);
1305 }
Leon Romanovskye3f2d552020-05-17 14:53:40 +03001306 return ret;
Russell King298e54f2019-11-15 19:56:51 +00001307}
1308EXPORT_SYMBOL(phy_sfp_probe);
1309
1310/**
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001311 * phy_attach_direct - attach a network device to a given PHY device pointer
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001312 * @dev: network device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001313 * @phydev: Pointer to phy_device to attach
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001314 * @flags: PHY device's dev_flags
1315 * @interface: PHY device's interface
1316 *
1317 * Description: Called by drivers to attach to a particular PHY
1318 * device. The phy_device is found, and properly hooked up
Andy Fleming257184d2014-01-10 14:27:54 +08001319 * to the phy_driver. If no driver is attached, then a
1320 * generic driver is used. The phy_device is given a ptr to
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001321 * the attaching device, and given a callback for link status
1322 * change. The phy_device is returned to the attaching driver.
Russell King73229672015-09-24 20:36:08 +01001323 * This function takes a reference on the phy device.
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001324 */
Andy Fleming257184d2014-01-10 14:27:54 +08001325int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1326 u32 flags, phy_interface_t interface)
Andy Fleminge1393452005-08-24 18:46:21 -05001327{
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001328 struct mii_bus *bus = phydev->mdio.bus;
1329 struct device *d = &phydev->mdio.dev;
Ioana Ciornei2db2d9d12019-05-28 20:38:08 +03001330 struct module *ndev_owner = NULL;
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001331 bool using_genphy = false;
Marc Kleine-Budded005a092011-03-28 14:54:08 +00001332 int err;
Andy Fleminge1393452005-08-24 18:46:21 -05001333
Florian Fainelliec988ad2016-12-06 20:54:43 -08001334 /* For Ethernet device drivers that register their own MDIO bus, we
1335 * will have bus->owner match ndev_mod, so we do not want to increment
1336 * our own module->refcnt here, otherwise we would not be able to
1337 * unload later on.
1338 */
Ioana Ciornei2db2d9d12019-05-28 20:38:08 +03001339 if (dev)
1340 ndev_owner = dev->dev.parent->driver->owner;
Florian Fainelliec988ad2016-12-06 20:54:43 -08001341 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
Ioana Ciornei2db2d9d12019-05-28 20:38:08 +03001342 phydev_err(phydev, "failed to get the bus module\n");
Russell King3e3aaf62015-09-24 20:36:02 +01001343 return -EIO;
1344 }
1345
Russell King73229672015-09-24 20:36:08 +01001346 get_device(d);
1347
Andy Fleminge1393452005-08-24 18:46:21 -05001348 /* Assume that if there is no driver, that it doesn't
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001349 * exist, and we should use the genphy driver.
1350 */
Sergei Shtylyovef899c02015-08-28 21:35:14 +03001351 if (!d->driver) {
Andy Fleming257184d2014-01-10 14:27:54 +08001352 if (phydev->is_c45)
Heiner Kallweit22b56e82019-04-07 13:55:01 +02001353 d->driver = &genphy_c45_driver.mdiodrv.driver;
Andy Fleming257184d2014-01-10 14:27:54 +08001354 else
Russell King921690f2017-06-05 12:23:05 +01001355 d->driver = &genphy_driver.mdiodrv.driver;
Andy Fleminge1393452005-08-24 18:46:21 -05001356
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001357 using_genphy = true;
1358 }
1359
1360 if (!try_module_get(d->driver->owner)) {
Ioana Ciornei2db2d9d12019-05-28 20:38:08 +03001361 phydev_err(phydev, "failed to get the device driver module\n");
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001362 err = -EIO;
1363 goto error_put_device;
1364 }
1365
1366 if (using_genphy) {
Andy Fleminge1393452005-08-24 18:46:21 -05001367 err = d->driver->probe(d);
Jeff Garzikb7a00ec2006-10-01 07:27:46 -04001368 if (err >= 0)
1369 err = device_bind_driver(d);
Andy Fleminge1393452005-08-24 18:46:21 -05001370
Jeff Garzikb7a00ec2006-10-01 07:27:46 -04001371 if (err)
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001372 goto error_module_put;
Andy Fleminge1393452005-08-24 18:46:21 -05001373 }
1374
1375 if (phydev->attached_dev) {
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001376 dev_err(&dev->dev, "PHY already attached\n");
Russell King3e3aaf62015-09-24 20:36:02 +01001377 err = -EBUSY;
1378 goto error;
Ezequiel Garciab3565f22014-07-23 16:47:32 -03001379 }
1380
Russell Kinga81497b2017-07-25 15:02:58 +01001381 phydev->phy_link_change = phy_link_change;
Ioana Ciornei2db2d9d12019-05-28 20:38:08 +03001382 if (dev) {
1383 phydev->attached_dev = dev;
1384 dev->phydev = phydev;
Russell King298e54f2019-11-15 19:56:51 +00001385
1386 if (phydev->sfp_bus_attached)
1387 dev->sfp_bus = phydev->sfp_bus;
Robert Hancockb8344892021-02-16 16:54:53 -06001388 else if (dev->sfp_bus)
1389 phydev->is_on_sfp_module = true;
Ioana Ciornei2db2d9d12019-05-28 20:38:08 +03001390 }
Florian Fainellia3995462017-05-27 10:42:25 -07001391
1392 /* Some Ethernet drivers try to connect to a PHY device before
1393 * calling register_netdevice() -> netdev_register_kobject() and
1394 * does the dev->dev.kobj initialization. Here we only check for
1395 * success which indicates that the network device kobject is
1396 * ready. Once we do that we still need to keep track of whether
1397 * links were successfully set up or not for phy_detach() to
1398 * remove them accordingly.
1399 */
1400 phydev->sysfs_links = false;
1401
Vladimir Oltean53cfca22019-05-28 20:38:07 +03001402 phy_sysfs_create_links(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -05001403
Ioana Ciorneic920f742019-05-28 20:38:10 +03001404 if (!phydev->attached_dev) {
1405 err = sysfs_create_file(&phydev->mdio.dev.kobj,
1406 &dev_attr_phy_standalone.attr);
1407 if (err)
1408 phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n");
1409 }
1410
Tao Rene7312ef2019-10-22 11:31:06 -07001411 phydev->dev_flags |= flags;
Andy Fleminge1393452005-08-24 18:46:21 -05001412
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001413 phydev->interface = interface;
1414
Anton Vorontsovef24b162010-08-24 14:46:12 -07001415 phydev->state = PHY_READY;
1416
Michael Walle4217a642021-02-09 17:38:52 +01001417 /* Port is set to PORT_TP by default and the actual PHY driver will set
1418 * it to different value depending on the PHY configuration. If we have
1419 * the generic PHY driver we can't figure it out, thus set the old
1420 * legacy PORT_MII value.
1421 */
1422 if (using_genphy)
1423 phydev->port = PORT_MII;
1424
Sjoerd Simons113c74d2016-01-14 21:57:18 +01001425 /* Initial carrier state is off as the phy is about to be
1426 * (re)initialized.
1427 */
Ioana Ciornei2db2d9d12019-05-28 20:38:08 +03001428 if (dev)
1429 netif_carrier_off(phydev->attached_dev);
Sjoerd Simons113c74d2016-01-14 21:57:18 +01001430
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001431 /* Do initial configuration here, now that
1432 * we have certain key parameters
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001433 * (dev_flags and interface)
1434 */
Marc Kleine-Budded005a092011-03-28 14:54:08 +00001435 err = phy_init_hw(phydev);
1436 if (err)
Woojung Huha7dac9f2016-11-23 23:10:33 +00001437 goto error;
Sebastian Hesselbarth1211ce52013-12-13 10:20:28 +01001438
Yoshihiro Shimoda7d3ba932020-09-09 14:43:14 +09001439 err = phy_disable_interrupts(phydev);
1440 if (err)
1441 return err;
1442
Woojung Huha7dac9f2016-11-23 23:10:33 +00001443 phy_resume(phydev);
Zach Brown2e0bc452016-10-17 10:49:55 -05001444 phy_led_triggers_register(phydev);
1445
Marc Kleine-Budded005a092011-03-28 14:54:08 +00001446 return err;
Russell King3e3aaf62015-09-24 20:36:02 +01001447
1448error:
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001449 /* phy_detach() does all of the cleanup below */
Woojung Huha7dac9f2016-11-23 23:10:33 +00001450 phy_detach(phydev);
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001451 return err;
1452
1453error_module_put:
Mao Wenancafe8df2017-01-31 18:46:43 -08001454 module_put(d->driver->owner);
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001455error_put_device:
1456 put_device(d);
Florian Fainelliec988ad2016-12-06 20:54:43 -08001457 if (ndev_owner != bus->owner)
1458 module_put(bus->owner);
Russell King3e3aaf62015-09-24 20:36:02 +01001459 return err;
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001460}
Andy Fleming257184d2014-01-10 14:27:54 +08001461EXPORT_SYMBOL(phy_attach_direct);
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001462
1463/**
1464 * phy_attach - attach a network device to a particular PHY device
1465 * @dev: network device to attach
1466 * @bus_id: Bus ID of PHY device to attach
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001467 * @interface: PHY device's interface
1468 *
1469 * Description: Same as phy_attach_direct() except that a PHY bus_id
1470 * string is passed instead of a pointer to a struct phy_device.
1471 */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001472struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1473 phy_interface_t interface)
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001474{
1475 struct bus_type *bus = &mdio_bus_type;
1476 struct phy_device *phydev;
1477 struct device *d;
1478 int rc;
1479
Ioana Ciornei82c76ac2019-05-28 20:38:09 +03001480 if (!dev)
1481 return ERR_PTR(-EINVAL);
1482
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001483 /* Search the list of PHY devices on the mdio bus for the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001484 * PHY with the requested name
1485 */
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001486 d = bus_find_device_by_name(bus, NULL, bus_id);
1487 if (!d) {
1488 pr_err("PHY %s not found\n", bus_id);
1489 return ERR_PTR(-ENODEV);
1490 }
1491 phydev = to_phy_device(d);
1492
Florian Fainellif9a8f832013-01-14 00:52:52 +00001493 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
Johan Hovold17ae1c62016-11-03 18:40:19 +01001494 put_device(d);
Grant Likelyfa94f6d2009-04-25 12:52:51 +00001495 if (rc)
1496 return ERR_PTR(rc);
1497
Andy Fleminge1393452005-08-24 18:46:21 -05001498 return phydev;
1499}
1500EXPORT_SYMBOL(phy_attach);
1501
Florian Fainelli5db5ea92019-01-15 15:09:35 -08001502static bool phy_driver_is_genphy_kind(struct phy_device *phydev,
1503 struct device_driver *driver)
1504{
1505 struct device *d = &phydev->mdio.dev;
1506 bool ret = false;
1507
1508 if (!phydev->drv)
1509 return ret;
1510
1511 get_device(d);
1512 ret = d->driver == driver;
1513 put_device(d);
1514
1515 return ret;
1516}
1517
1518bool phy_driver_is_genphy(struct phy_device *phydev)
1519{
1520 return phy_driver_is_genphy_kind(phydev,
1521 &genphy_driver.mdiodrv.driver);
1522}
1523EXPORT_SYMBOL_GPL(phy_driver_is_genphy);
1524
1525bool phy_driver_is_genphy_10g(struct phy_device *phydev)
1526{
1527 return phy_driver_is_genphy_kind(phydev,
Heiner Kallweit22b56e82019-04-07 13:55:01 +02001528 &genphy_c45_driver.mdiodrv.driver);
Florian Fainelli5db5ea92019-01-15 15:09:35 -08001529}
1530EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
1531
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001532/**
Michael Walle63490842020-05-06 16:53:13 +02001533 * phy_package_join - join a common PHY group
1534 * @phydev: target phy_device struct
1535 * @addr: cookie and PHY address for global register access
1536 * @priv_size: if non-zero allocate this amount of bytes for private data
1537 *
1538 * This joins a PHY group and provides a shared storage for all phydevs in
1539 * this group. This is intended to be used for packages which contain
1540 * more than one PHY, for example a quad PHY transceiver.
1541 *
1542 * The addr parameter serves as a cookie which has to have the same value
1543 * for all members of one group and as a PHY address to access generic
1544 * registers of a PHY package. Usually, one of the PHY addresses of the
1545 * different PHYs in the package provides access to these global registers.
1546 * The address which is given here, will be used in the phy_package_read()
1547 * and phy_package_write() convenience functions. If your PHY doesn't have
1548 * global registers you can just pick any of the PHY addresses.
1549 *
1550 * This will set the shared pointer of the phydev to the shared storage.
1551 * If this is the first call for a this cookie the shared storage will be
1552 * allocated. If priv_size is non-zero, the given amount of bytes are
1553 * allocated for the priv member.
1554 *
1555 * Returns < 1 on error, 0 on success. Esp. calling phy_package_join()
1556 * with the same cookie but a different priv_size is an error.
1557 */
1558int phy_package_join(struct phy_device *phydev, int addr, size_t priv_size)
1559{
1560 struct mii_bus *bus = phydev->mdio.bus;
1561 struct phy_package_shared *shared;
1562 int ret;
1563
1564 if (addr < 0 || addr >= PHY_MAX_ADDR)
1565 return -EINVAL;
1566
1567 mutex_lock(&bus->shared_lock);
1568 shared = bus->shared[addr];
1569 if (!shared) {
1570 ret = -ENOMEM;
1571 shared = kzalloc(sizeof(*shared), GFP_KERNEL);
1572 if (!shared)
1573 goto err_unlock;
1574 if (priv_size) {
1575 shared->priv = kzalloc(priv_size, GFP_KERNEL);
1576 if (!shared->priv)
1577 goto err_free;
1578 shared->priv_size = priv_size;
1579 }
1580 shared->addr = addr;
1581 refcount_set(&shared->refcnt, 1);
1582 bus->shared[addr] = shared;
1583 } else {
1584 ret = -EINVAL;
1585 if (priv_size && priv_size != shared->priv_size)
1586 goto err_unlock;
1587 refcount_inc(&shared->refcnt);
1588 }
1589 mutex_unlock(&bus->shared_lock);
1590
1591 phydev->shared = shared;
1592
1593 return 0;
1594
1595err_free:
1596 kfree(shared);
1597err_unlock:
1598 mutex_unlock(&bus->shared_lock);
1599 return ret;
1600}
1601EXPORT_SYMBOL_GPL(phy_package_join);
1602
1603/**
1604 * phy_package_leave - leave a common PHY group
1605 * @phydev: target phy_device struct
1606 *
1607 * This leaves a PHY group created by phy_package_join(). If this phydev
1608 * was the last user of the shared data between the group, this data is
1609 * freed. Resets the phydev->shared pointer to NULL.
1610 */
1611void phy_package_leave(struct phy_device *phydev)
1612{
1613 struct phy_package_shared *shared = phydev->shared;
1614 struct mii_bus *bus = phydev->mdio.bus;
1615
1616 if (!shared)
1617 return;
1618
1619 if (refcount_dec_and_mutex_lock(&shared->refcnt, &bus->shared_lock)) {
1620 bus->shared[shared->addr] = NULL;
1621 mutex_unlock(&bus->shared_lock);
1622 kfree(shared->priv);
1623 kfree(shared);
1624 }
1625
1626 phydev->shared = NULL;
1627}
1628EXPORT_SYMBOL_GPL(phy_package_leave);
1629
1630static void devm_phy_package_leave(struct device *dev, void *res)
1631{
1632 phy_package_leave(*(struct phy_device **)res);
1633}
1634
1635/**
1636 * devm_phy_package_join - resource managed phy_package_join()
1637 * @dev: device that is registering this PHY package
1638 * @phydev: target phy_device struct
1639 * @addr: cookie and PHY address for global register access
1640 * @priv_size: if non-zero allocate this amount of bytes for private data
1641 *
1642 * Managed phy_package_join(). Shared storage fetched by this function,
1643 * phy_package_leave() is automatically called on driver detach. See
1644 * phy_package_join() for more information.
1645 */
1646int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
1647 int addr, size_t priv_size)
1648{
1649 struct phy_device **ptr;
1650 int ret;
1651
1652 ptr = devres_alloc(devm_phy_package_leave, sizeof(*ptr),
1653 GFP_KERNEL);
1654 if (!ptr)
1655 return -ENOMEM;
1656
1657 ret = phy_package_join(phydev, addr, priv_size);
1658
1659 if (!ret) {
1660 *ptr = phydev;
1661 devres_add(dev, ptr);
1662 } else {
1663 devres_free(ptr);
1664 }
1665
1666 return ret;
1667}
1668EXPORT_SYMBOL_GPL(devm_phy_package_join);
1669
1670/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001671 * phy_detach - detach a PHY device from its network device
1672 * @phydev: target phy_device struct
Russell King73229672015-09-24 20:36:08 +01001673 *
1674 * This detaches the phy device from its network device and the phy
1675 * driver, and drops the reference count taken in phy_attach_direct().
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001676 */
Andy Fleminge1393452005-08-24 18:46:21 -05001677void phy_detach(struct phy_device *phydev)
1678{
Florian Fainelliec988ad2016-12-06 20:54:43 -08001679 struct net_device *dev = phydev->attached_dev;
Ioana Ciornei2db2d9d12019-05-28 20:38:08 +03001680 struct module *ndev_owner = NULL;
Russell King3e3aaf62015-09-24 20:36:02 +01001681 struct mii_bus *bus;
Ezequiel Garciab3565f22014-07-23 16:47:32 -03001682
Florian Fainellia3995462017-05-27 10:42:25 -07001683 if (phydev->sysfs_links) {
Ioana Ciornei2db2d9d12019-05-28 20:38:08 +03001684 if (dev)
1685 sysfs_remove_link(&dev->dev.kobj, "phydev");
Florian Fainellia3995462017-05-27 10:42:25 -07001686 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1687 }
Ioana Ciorneic920f742019-05-28 20:38:10 +03001688
1689 if (!phydev->attached_dev)
1690 sysfs_remove_file(&phydev->mdio.dev.kobj,
1691 &dev_attr_phy_standalone.attr);
1692
Heiner Kallweit93f41e62018-09-24 22:01:32 +02001693 phy_suspend(phydev);
Ioana Ciornei2db2d9d12019-05-28 20:38:08 +03001694 if (dev) {
1695 phydev->attached_dev->phydev = NULL;
1696 phydev->attached_dev = NULL;
1697 }
Russell King9525ae82017-07-25 15:03:13 +01001698 phydev->phylink = NULL;
Andy Fleminge1393452005-08-24 18:46:21 -05001699
Geert Uytterhoeven0075bd62016-11-28 15:18:31 +01001700 phy_led_triggers_unregister(phydev);
1701
Florian Fainellic2b727d2020-09-16 20:43:09 -07001702 if (phydev->mdio.dev.driver)
1703 module_put(phydev->mdio.dev.driver->owner);
Florian Fainelli6d9f66a2017-02-08 19:05:26 -08001704
Andy Fleminge1393452005-08-24 18:46:21 -05001705 /* If the device had no specific driver before (i.e. - it
1706 * was using the generic driver), we unbind the device
1707 * from the generic driver so that there's a chance a
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001708 * real driver could be loaded
1709 */
Florian Fainelli5db5ea92019-01-15 15:09:35 -08001710 if (phy_driver_is_genphy(phydev) ||
1711 phy_driver_is_genphy_10g(phydev))
Russell King921690f2017-06-05 12:23:05 +01001712 device_release_driver(&phydev->mdio.dev);
Russell King3e3aaf62015-09-24 20:36:02 +01001713
Russell King73229672015-09-24 20:36:08 +01001714 /*
1715 * The phydev might go away on the put_device() below, so avoid
1716 * a use-after-free bug by reading the underlying bus first.
1717 */
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001718 bus = phydev->mdio.bus;
Russell King3e3aaf62015-09-24 20:36:02 +01001719
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001720 put_device(&phydev->mdio.dev);
Ioana Ciornei2db2d9d12019-05-28 20:38:08 +03001721 if (dev)
1722 ndev_owner = dev->dev.parent->driver->owner;
Florian Fainelliec988ad2016-12-06 20:54:43 -08001723 if (ndev_owner != bus->owner)
1724 module_put(bus->owner);
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01001725
1726 /* Assert the reset signal */
1727 phy_device_reset(phydev, 1);
Andy Fleminge1393452005-08-24 18:46:21 -05001728}
1729EXPORT_SYMBOL(phy_detach);
1730
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001731int phy_suspend(struct phy_device *phydev)
1732{
Sebastian Hesselbarth32fc3fd2014-03-14 10:07:44 +01001733 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Heiner Kallweit8a8f8282020-03-26 18:58:24 +01001734 struct net_device *netdev = phydev->attached_dev;
1735 struct phy_driver *phydrv = phydev->drv;
1736 int ret;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001737
Heiner Kallweitd70c47c2020-04-23 21:34:33 +02001738 if (phydev->suspended)
1739 return 0;
1740
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001741 /* If the device has WOL enabled, we cannot suspend the PHY */
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001742 phy_ethtool_get_wol(phydev, &wol);
Heiner Kallweit93f41e62018-09-24 22:01:32 +02001743 if (wol.wolopts || (netdev && netdev->wol_enabled))
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001744 return -EBUSY;
1745
Heiner Kallweit8a8f8282020-03-26 18:58:24 +01001746 if (!phydrv || !phydrv->suspend)
1747 return 0;
Florian Fainelli8a477a62015-01-26 22:05:39 -08001748
Heiner Kallweit8a8f8282020-03-26 18:58:24 +01001749 ret = phydrv->suspend(phydev);
1750 if (!ret)
1751 phydev->suspended = true;
Florian Fainelli8a477a62015-01-26 22:05:39 -08001752
1753 return ret;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001754}
Florian Fainellica127692014-07-08 10:38:36 -07001755EXPORT_SYMBOL(phy_suspend);
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001756
Andrew Lunn9c2c2e62018-02-27 01:56:06 +01001757int __phy_resume(struct phy_device *phydev)
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001758{
Heiner Kallweit8a8f8282020-03-26 18:58:24 +01001759 struct phy_driver *phydrv = phydev->drv;
1760 int ret;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001761
Heiner Kallweite6e918d2021-01-06 14:03:40 +01001762 lockdep_assert_held(&phydev->lock);
Russell Kingf5e64032017-12-12 10:45:36 +00001763
Heiner Kallweit8a8f8282020-03-26 18:58:24 +01001764 if (!phydrv || !phydrv->resume)
1765 return 0;
Florian Fainelli8a477a62015-01-26 22:05:39 -08001766
Heiner Kallweit8a8f8282020-03-26 18:58:24 +01001767 ret = phydrv->resume(phydev);
1768 if (!ret)
1769 phydev->suspended = false;
Florian Fainelli8a477a62015-01-26 22:05:39 -08001770
1771 return ret;
Sebastian Hesselbarth481b5d92013-12-13 10:20:27 +01001772}
Andrew Lunn9c2c2e62018-02-27 01:56:06 +01001773EXPORT_SYMBOL(__phy_resume);
1774
1775int phy_resume(struct phy_device *phydev)
1776{
1777 int ret;
1778
1779 mutex_lock(&phydev->lock);
1780 ret = __phy_resume(phydev);
1781 mutex_unlock(&phydev->lock);
1782
1783 return ret;
1784}
Florian Fainellica127692014-07-08 10:38:36 -07001785EXPORT_SYMBOL(phy_resume);
Andy Fleminge1393452005-08-24 18:46:21 -05001786
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08001787int phy_loopback(struct phy_device *phydev, bool enable)
1788{
1789 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1790 int ret = 0;
1791
Oleksij Rempelf4f86d82021-04-19 15:01:01 +02001792 if (!phydrv)
1793 return -ENODEV;
1794
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08001795 mutex_lock(&phydev->lock);
1796
1797 if (enable && phydev->loopback_enabled) {
1798 ret = -EBUSY;
1799 goto out;
1800 }
1801
1802 if (!enable && !phydev->loopback_enabled) {
1803 ret = -EINVAL;
1804 goto out;
1805 }
1806
Oleksij Rempelf4f86d82021-04-19 15:01:01 +02001807 if (phydrv->set_loopback)
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08001808 ret = phydrv->set_loopback(phydev, enable);
1809 else
Oleksij Rempelf4f86d82021-04-19 15:01:01 +02001810 ret = genphy_loopback(phydev, enable);
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08001811
1812 if (ret)
1813 goto out;
1814
1815 phydev->loopback_enabled = enable;
1816
1817out:
1818 mutex_unlock(&phydev->lock);
1819 return ret;
1820}
1821EXPORT_SYMBOL(phy_loopback);
1822
Richard Leitnera9668492017-12-11 13:16:58 +01001823/**
1824 * phy_reset_after_clk_enable - perform a PHY reset if needed
1825 * @phydev: target phy_device struct
1826 *
1827 * Description: Some PHYs are known to need a reset after their refclk was
1828 * enabled. This function evaluates the flags and perform the reset if it's
1829 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
1830 * was reset.
1831 */
1832int phy_reset_after_clk_enable(struct phy_device *phydev)
1833{
1834 if (!phydev || !phydev->drv)
1835 return -ENODEV;
1836
1837 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1838 phy_device_reset(phydev, 1);
1839 phy_device_reset(phydev, 0);
1840 return 1;
1841 }
1842
1843 return 0;
1844}
1845EXPORT_SYMBOL(phy_reset_after_clk_enable);
1846
Andy Fleming00db8182005-07-30 19:31:23 -04001847/* Generic PHY support and helper functions */
1848
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001849/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001850 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001851 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001852 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001853 * Description: Writes MII_ADVERTISE with the appropriate values,
Andy Fleming00db8182005-07-30 19:31:23 -04001854 * after sanitizing the values to make sure we only advertise
Trent Piepho51e2a382008-09-24 10:55:46 +00001855 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1856 * hasn't changed, and > 0 if it has changed.
Andy Fleming00db8182005-07-30 19:31:23 -04001857 */
stephen hemminger89ff05e2010-10-21 08:37:41 +00001858static int genphy_config_advert(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -04001859{
Heiner Kallweit3eef8682019-08-09 20:43:04 +02001860 int err, bmsr, changed = 0;
1861 u32 adv;
Andy Fleming00db8182005-07-30 19:31:23 -04001862
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001863 /* Only allow advertising what this PHY supports */
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001864 linkmode_and(phydev->advertising, phydev->advertising,
1865 phydev->supported);
Heiner Kallweit3eef8682019-08-09 20:43:04 +02001866
1867 adv = linkmode_adv_to_mii_adv_t(phydev->advertising);
Andy Fleming00db8182005-07-30 19:31:23 -04001868
1869 /* Setup standard advertisement */
Heiner Kallweit4f9744e2019-02-10 19:59:57 +01001870 err = phy_modify_changed(phydev, MII_ADVERTISE,
1871 ADVERTISE_ALL | ADVERTISE_100BASE4 |
1872 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
Heiner Kallweit3eef8682019-08-09 20:43:04 +02001873 adv);
Heiner Kallweit4f9744e2019-02-10 19:59:57 +01001874 if (err < 0)
1875 return err;
1876 if (err > 0)
Trent Piepho51e2a382008-09-24 10:55:46 +00001877 changed = 1;
Andy Fleming00db8182005-07-30 19:31:23 -04001878
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001879 bmsr = phy_read(phydev, MII_BMSR);
1880 if (bmsr < 0)
1881 return bmsr;
1882
1883 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1884 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1885 * logical 1.
1886 */
1887 if (!(bmsr & BMSR_ESTATEN))
1888 return changed;
1889
Heiner Kallweit3eef8682019-08-09 20:43:04 +02001890 adv = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
Andy Fleming00db8182005-07-30 19:31:23 -04001891
Heiner Kallweit4f9744e2019-02-10 19:59:57 +01001892 err = phy_modify_changed(phydev, MII_CTRL1000,
1893 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
1894 adv);
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001895 if (err < 0)
1896 return err;
Heiner Kallweit4f9744e2019-02-10 19:59:57 +01001897 if (err > 0)
1898 changed = 1;
Florian Fainelli5273e3a2014-02-03 12:35:46 -08001899
Trent Piepho51e2a382008-09-24 10:55:46 +00001900 return changed;
Andy Fleming00db8182005-07-30 19:31:23 -04001901}
Andy Fleming00db8182005-07-30 19:31:23 -04001902
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001903/**
Heiner Kallweitfa6e98c2019-10-22 11:31:07 -07001904 * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters
1905 * @phydev: target phy_device struct
1906 *
1907 * Description: Writes MII_ADVERTISE with the appropriate values,
1908 * after sanitizing the values to make sure we only advertise
1909 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1910 * hasn't changed, and > 0 if it has changed. This function is intended
1911 * for Clause 37 1000Base-X mode.
1912 */
1913static int genphy_c37_config_advert(struct phy_device *phydev)
1914{
1915 u16 adv = 0;
1916
1917 /* Only allow advertising what this PHY supports */
1918 linkmode_and(phydev->advertising, phydev->advertising,
1919 phydev->supported);
1920
1921 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
1922 phydev->advertising))
1923 adv |= ADVERTISE_1000XFULL;
1924 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
1925 phydev->advertising))
1926 adv |= ADVERTISE_1000XPAUSE;
1927 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
1928 phydev->advertising))
1929 adv |= ADVERTISE_1000XPSE_ASYM;
1930
1931 return phy_modify_changed(phydev, MII_ADVERTISE,
1932 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1933 ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM,
1934 adv);
1935}
1936
1937/**
jbrunetd853d142016-11-28 10:46:46 +01001938 * genphy_config_eee_advert - disable unwanted eee mode advertisement
1939 * @phydev: target phy_device struct
1940 *
1941 * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
1942 * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
1943 * changed, and 1 if it has changed.
1944 */
Heiner Kallweitcd344992019-02-18 21:26:58 +01001945int genphy_config_eee_advert(struct phy_device *phydev)
jbrunetd853d142016-11-28 10:46:46 +01001946{
Heiner Kallweit9f771f12019-02-11 22:16:13 +01001947 int err;
jbrunetd853d142016-11-28 10:46:46 +01001948
1949 /* Nothing to disable */
Heiner Kallweit9f771f12019-02-11 22:16:13 +01001950 if (!phydev->eee_broken_modes)
jbrunetd853d142016-11-28 10:46:46 +01001951 return 0;
1952
Heiner Kallweit9f771f12019-02-11 22:16:13 +01001953 err = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
1954 phydev->eee_broken_modes, 0);
1955 /* If the call failed, we assume that EEE is not supported */
1956 return err < 0 ? 0 : err;
jbrunetd853d142016-11-28 10:46:46 +01001957}
Heiner Kallweitcd344992019-02-18 21:26:58 +01001958EXPORT_SYMBOL(genphy_config_eee_advert);
jbrunetd853d142016-11-28 10:46:46 +01001959
1960/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001961 * genphy_setup_forced - configures/forces speed/duplex from @phydev
1962 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04001963 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001964 * Description: Configures MII_BMCR to force speed/duplex
Andy Fleming00db8182005-07-30 19:31:23 -04001965 * to the values in phydev. Assumes that the values are valid.
Randy Dunlapb3df0da2007-03-06 02:41:48 -08001966 * Please see phy_sanitize_settings().
1967 */
Madalin Bucur3fb69bc2013-11-20 16:38:19 -06001968int genphy_setup_forced(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -04001969{
Russell Kingfea23fb2018-01-02 10:58:58 +00001970 u16 ctl = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001971
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001972 phydev->pause = 0;
1973 phydev->asym_pause = 0;
Andy Fleming00db8182005-07-30 19:31:23 -04001974
1975 if (SPEED_1000 == phydev->speed)
1976 ctl |= BMCR_SPEED1000;
1977 else if (SPEED_100 == phydev->speed)
1978 ctl |= BMCR_SPEED100;
1979
1980 if (DUPLEX_FULL == phydev->duplex)
1981 ctl |= BMCR_FULLDPLX;
Florian Fainellie1093742013-12-17 21:38:12 -08001982
Russell Kingfea23fb2018-01-02 10:58:58 +00001983 return phy_modify(phydev, MII_BMCR,
Ingo van Lil18a5b052018-02-12 12:02:52 +01001984 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
Andy Fleming00db8182005-07-30 19:31:23 -04001985}
Madalin Bucur3fb69bc2013-11-20 16:38:19 -06001986EXPORT_SYMBOL(genphy_setup_forced);
Andy Fleming00db8182005-07-30 19:31:23 -04001987
Oleksij Rempelbdbdac72020-05-05 08:35:05 +02001988static int genphy_setup_master_slave(struct phy_device *phydev)
1989{
1990 u16 ctl = 0;
1991
1992 if (!phydev->is_gigabit_capable)
1993 return 0;
1994
1995 switch (phydev->master_slave_set) {
1996 case MASTER_SLAVE_CFG_MASTER_PREFERRED:
1997 ctl |= CTL1000_PREFER_MASTER;
1998 break;
1999 case MASTER_SLAVE_CFG_SLAVE_PREFERRED:
2000 break;
2001 case MASTER_SLAVE_CFG_MASTER_FORCE:
2002 ctl |= CTL1000_AS_MASTER;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002003 fallthrough;
Oleksij Rempelbdbdac72020-05-05 08:35:05 +02002004 case MASTER_SLAVE_CFG_SLAVE_FORCE:
2005 ctl |= CTL1000_ENABLE_MASTER;
2006 break;
2007 case MASTER_SLAVE_CFG_UNKNOWN:
2008 case MASTER_SLAVE_CFG_UNSUPPORTED:
2009 return 0;
2010 default:
2011 phydev_warn(phydev, "Unsupported Master/Slave mode\n");
2012 return -EOPNOTSUPP;
2013 }
2014
2015 return phy_modify_changed(phydev, MII_CTRL1000,
2016 (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER |
2017 CTL1000_PREFER_MASTER), ctl);
2018}
2019
2020static int genphy_read_master_slave(struct phy_device *phydev)
2021{
2022 int cfg, state;
Colin Ian King3a13f982020-05-07 15:34:30 +01002023 int val;
Oleksij Rempelbdbdac72020-05-05 08:35:05 +02002024
2025 if (!phydev->is_gigabit_capable) {
2026 phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED;
2027 phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
2028 return 0;
2029 }
2030
2031 phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN;
2032 phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN;
2033
2034 val = phy_read(phydev, MII_CTRL1000);
2035 if (val < 0)
2036 return val;
2037
2038 if (val & CTL1000_ENABLE_MASTER) {
2039 if (val & CTL1000_AS_MASTER)
2040 cfg = MASTER_SLAVE_CFG_MASTER_FORCE;
2041 else
2042 cfg = MASTER_SLAVE_CFG_SLAVE_FORCE;
2043 } else {
2044 if (val & CTL1000_PREFER_MASTER)
2045 cfg = MASTER_SLAVE_CFG_MASTER_PREFERRED;
2046 else
2047 cfg = MASTER_SLAVE_CFG_SLAVE_PREFERRED;
2048 }
2049
2050 val = phy_read(phydev, MII_STAT1000);
2051 if (val < 0)
2052 return val;
2053
2054 if (val & LPA_1000MSFAIL) {
2055 state = MASTER_SLAVE_STATE_ERR;
2056 } else if (phydev->link) {
2057 /* this bits are valid only for active link */
2058 if (val & LPA_1000MSRES)
2059 state = MASTER_SLAVE_STATE_MASTER;
2060 else
2061 state = MASTER_SLAVE_STATE_SLAVE;
2062 } else {
2063 state = MASTER_SLAVE_STATE_UNKNOWN;
2064 }
2065
2066 phydev->master_slave_get = cfg;
2067 phydev->master_slave_state = state;
2068
2069 return 0;
2070}
2071
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002072/**
2073 * genphy_restart_aneg - Enable and Restart Autonegotiation
2074 * @phydev: target phy_device struct
2075 */
Andy Fleming00db8182005-07-30 19:31:23 -04002076int genphy_restart_aneg(struct phy_device *phydev)
2077{
Andy Fleming00db8182005-07-30 19:31:23 -04002078 /* Don't isolate the PHY if we're negotiating */
Russell Kingf1028522018-01-05 16:07:10 +00002079 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
Russell Kingfea23fb2018-01-02 10:58:58 +00002080 BMCR_ANENABLE | BMCR_ANRESTART);
Andy Fleming00db8182005-07-30 19:31:23 -04002081}
Adrian Bunk892871d2008-10-13 18:48:09 -07002082EXPORT_SYMBOL(genphy_restart_aneg);
Andy Fleming00db8182005-07-30 19:31:23 -04002083
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002084/**
Russell King2a10ab02019-12-17 13:39:11 +00002085 * genphy_check_and_restart_aneg - Enable and restart auto-negotiation
2086 * @phydev: target phy_device struct
2087 * @restart: whether aneg restart is requested
2088 *
2089 * Check, and restart auto-negotiation if needed.
2090 */
2091int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart)
2092{
Sudheesh Mavila4f31c532020-02-26 12:40:45 +05302093 int ret;
Russell King2a10ab02019-12-17 13:39:11 +00002094
2095 if (!restart) {
2096 /* Advertisement hasn't changed, but maybe aneg was never on to
2097 * begin with? Or maybe phy was isolated?
2098 */
2099 ret = phy_read(phydev, MII_BMCR);
2100 if (ret < 0)
2101 return ret;
2102
2103 if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE))
2104 restart = true;
2105 }
2106
2107 if (restart)
Sudheesh Mavila4f31c532020-02-26 12:40:45 +05302108 return genphy_restart_aneg(phydev);
Russell King2a10ab02019-12-17 13:39:11 +00002109
Sudheesh Mavila4f31c532020-02-26 12:40:45 +05302110 return 0;
Russell King2a10ab02019-12-17 13:39:11 +00002111}
2112EXPORT_SYMBOL(genphy_check_and_restart_aneg);
2113
2114/**
Heiner Kallweitf4069cd2019-08-09 20:43:50 +02002115 * __genphy_config_aneg - restart auto-negotiation or write BMCR
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002116 * @phydev: target phy_device struct
Heiner Kallweitf4069cd2019-08-09 20:43:50 +02002117 * @changed: whether autoneg is requested
Andy Fleming00db8182005-07-30 19:31:23 -04002118 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002119 * Description: If auto-negotiation is enabled, we configure the
Andy Fleming00db8182005-07-30 19:31:23 -04002120 * advertising, and then restart auto-negotiation. If it is not
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002121 * enabled, then we write the BMCR.
Andy Fleming00db8182005-07-30 19:31:23 -04002122 */
Heiner Kallweitf4069cd2019-08-09 20:43:50 +02002123int __genphy_config_aneg(struct phy_device *phydev, bool changed)
Andy Fleming00db8182005-07-30 19:31:23 -04002124{
Heiner Kallweitf4069cd2019-08-09 20:43:50 +02002125 int err;
jbrunetd853d142016-11-28 10:46:46 +01002126
Heiner Kallweitf4069cd2019-08-09 20:43:50 +02002127 if (genphy_config_eee_advert(phydev))
2128 changed = true;
Andy Fleming00db8182005-07-30 19:31:23 -04002129
Oleksij Rempelbdbdac72020-05-05 08:35:05 +02002130 err = genphy_setup_master_slave(phydev);
2131 if (err < 0)
2132 return err;
2133 else if (err)
2134 changed = true;
2135
Trent Piephode339c22008-11-19 15:52:41 -08002136 if (AUTONEG_ENABLE != phydev->autoneg)
2137 return genphy_setup_forced(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04002138
jbrunetd853d142016-11-28 10:46:46 +01002139 err = genphy_config_advert(phydev);
2140 if (err < 0) /* error */
2141 return err;
Heiner Kallweitf4069cd2019-08-09 20:43:50 +02002142 else if (err)
2143 changed = true;
jbrunetd853d142016-11-28 10:46:46 +01002144
Russell King2a10ab02019-12-17 13:39:11 +00002145 return genphy_check_and_restart_aneg(phydev, changed);
Andy Fleming00db8182005-07-30 19:31:23 -04002146}
Heiner Kallweitf4069cd2019-08-09 20:43:50 +02002147EXPORT_SYMBOL(__genphy_config_aneg);
Andy Fleming00db8182005-07-30 19:31:23 -04002148
Florian Fainellia9fa6e62014-02-11 17:27:36 -08002149/**
Heiner Kallweitfa6e98c2019-10-22 11:31:07 -07002150 * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
2151 * @phydev: target phy_device struct
2152 *
2153 * Description: If auto-negotiation is enabled, we configure the
2154 * advertising, and then restart auto-negotiation. If it is not
2155 * enabled, then we write the BMCR. This function is intended
2156 * for use with Clause 37 1000Base-X mode.
2157 */
2158int genphy_c37_config_aneg(struct phy_device *phydev)
2159{
2160 int err, changed;
2161
2162 if (phydev->autoneg != AUTONEG_ENABLE)
2163 return genphy_setup_forced(phydev);
2164
2165 err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100,
2166 BMCR_SPEED1000);
2167 if (err)
2168 return err;
2169
2170 changed = genphy_c37_config_advert(phydev);
2171 if (changed < 0) /* error */
2172 return changed;
2173
2174 if (!changed) {
2175 /* Advertisement hasn't changed, but maybe aneg was never on to
2176 * begin with? Or maybe phy was isolated?
2177 */
2178 int ctl = phy_read(phydev, MII_BMCR);
2179
2180 if (ctl < 0)
2181 return ctl;
2182
2183 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
2184 changed = 1; /* do restart aneg */
2185 }
2186
2187 /* Only restart aneg if we are advertising something different
2188 * than we were before.
2189 */
2190 if (changed > 0)
2191 return genphy_restart_aneg(phydev);
2192
2193 return 0;
2194}
2195EXPORT_SYMBOL(genphy_c37_config_aneg);
2196
2197/**
Florian Fainellia9fa6e62014-02-11 17:27:36 -08002198 * genphy_aneg_done - return auto-negotiation status
2199 * @phydev: target phy_device struct
2200 *
2201 * Description: Reads the status register and returns 0 either if
2202 * auto-negotiation is incomplete, or if there was an error.
2203 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
2204 */
2205int genphy_aneg_done(struct phy_device *phydev)
2206{
2207 int retval = phy_read(phydev, MII_BMSR);
2208
2209 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
2210}
2211EXPORT_SYMBOL(genphy_aneg_done);
2212
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002213/**
2214 * genphy_update_link - update link status in @phydev
2215 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -04002216 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002217 * Description: Update the value in phydev->link to reflect the
Andy Fleming00db8182005-07-30 19:31:23 -04002218 * current link value. In order to do this, we need to read
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002219 * the status register twice, keeping the second value.
Andy Fleming00db8182005-07-30 19:31:23 -04002220 */
2221int genphy_update_link(struct phy_device *phydev)
2222{
Heiner Kallweitc36757eb2019-08-12 21:20:02 +02002223 int status = 0, bmcr;
2224
2225 bmcr = phy_read(phydev, MII_BMCR);
2226 if (bmcr < 0)
2227 return bmcr;
2228
2229 /* Autoneg is being started, therefore disregard BMSR value and
2230 * report link as down.
2231 */
2232 if (bmcr & BMCR_ANRESTART)
2233 goto done;
Andy Fleming00db8182005-07-30 19:31:23 -04002234
Heiner Kallweit93c09702019-02-06 19:39:52 +01002235 /* The link state is latched low so that momentary link
2236 * drops can be detected. Do not double-read the status
Petr Orose96bd2d2020-02-18 10:35:55 +01002237 * in polling mode to detect such short link drops except
2238 * the link was already down.
Heiner Kallweit93c09702019-02-06 19:39:52 +01002239 */
Petr Orose96bd2d2020-02-18 10:35:55 +01002240 if (!phy_polling_mode(phydev) || !phydev->link) {
Heiner Kallweit93c09702019-02-06 19:39:52 +01002241 status = phy_read(phydev, MII_BMSR);
Heiner Kallweitb7f29f82019-04-08 19:28:28 +02002242 if (status < 0)
Heiner Kallweit93c09702019-02-06 19:39:52 +01002243 return status;
Heiner Kallweitb7f29f82019-04-08 19:28:28 +02002244 else if (status & BMSR_LSTATUS)
2245 goto done;
Heiner Kallweit93c09702019-02-06 19:39:52 +01002246 }
Andy Fleming00db8182005-07-30 19:31:23 -04002247
2248 /* Read link and autonegotiation status */
2249 status = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -04002250 if (status < 0)
2251 return status;
Heiner Kallweitb7f29f82019-04-08 19:28:28 +02002252done:
Heiner Kallweit4950c2b2019-04-02 20:43:30 +02002253 phydev->link = status & BMSR_LSTATUS ? 1 : 0;
2254 phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
Andy Fleming00db8182005-07-30 19:31:23 -04002255
Heiner Kallweitaa6b1952019-07-31 23:05:10 +02002256 /* Consider the case that autoneg was started and "aneg complete"
2257 * bit has been reset, but "link up" bit not yet.
2258 */
2259 if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
2260 phydev->link = 0;
2261
Andy Fleming00db8182005-07-30 19:31:23 -04002262 return 0;
2263}
Andy Fleming6b655522006-10-16 16:19:17 -05002264EXPORT_SYMBOL(genphy_update_link);
Andy Fleming00db8182005-07-30 19:31:23 -04002265
Russell King8d3dc3ac2019-10-04 17:06:04 +01002266int genphy_read_lpa(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -04002267{
Russell King8d3dc3ac2019-10-04 17:06:04 +01002268 int lpa, lpagb;
Heiner Kallweitb6163f12019-03-30 10:22:45 +01002269
Yonglong Liu3de5ae52019-10-16 10:30:39 +08002270 if (phydev->autoneg == AUTONEG_ENABLE) {
2271 if (!phydev->autoneg_complete) {
2272 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2273 0);
2274 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
2275 return 0;
2276 }
2277
Heiner Kallweit3b8b11f2019-04-05 21:23:13 +02002278 if (phydev->is_gigabit_capable) {
Andy Fleming00db8182005-07-30 19:31:23 -04002279 lpagb = phy_read(phydev, MII_STAT1000);
Andy Fleming00db8182005-07-30 19:31:23 -04002280 if (lpagb < 0)
2281 return lpagb;
2282
Heiner Kallweitb8f8c8e2018-07-21 15:48:47 +02002283 if (lpagb & LPA_1000MSFAIL) {
Heiner Kallweit916e5712019-08-15 13:15:19 +02002284 int adv = phy_read(phydev, MII_CTRL1000);
2285
2286 if (adv < 0)
2287 return adv;
2288
Heiner Kallweitb8f8c8e2018-07-21 15:48:47 +02002289 if (adv & CTL1000_ENABLE_MASTER)
2290 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
2291 else
2292 phydev_err(phydev, "Master/Slave resolution failed\n");
2293 return -ENOLINK;
2294 }
2295
Andrew Lunn78a24df2018-12-05 21:49:41 +01002296 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2297 lpagb);
Andy Fleming00db8182005-07-30 19:31:23 -04002298 }
2299
2300 lpa = phy_read(phydev, MII_LPA);
Andy Fleming00db8182005-07-30 19:31:23 -04002301 if (lpa < 0)
2302 return lpa;
2303
Andrew Lunnd3351932018-12-05 21:49:43 +01002304 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
Yonglong Liu3de5ae52019-10-16 10:30:39 +08002305 } else {
2306 linkmode_zero(phydev->lp_advertising);
Russell King8d3dc3ac2019-10-04 17:06:04 +01002307 }
2308
2309 return 0;
2310}
2311EXPORT_SYMBOL(genphy_read_lpa);
2312
2313/**
Russell King0efc2862019-12-17 13:39:16 +00002314 * genphy_read_status_fixed - read the link parameters for !aneg mode
2315 * @phydev: target phy_device struct
2316 *
2317 * Read the current duplex and speed state for a PHY operating with
2318 * autonegotiation disabled.
2319 */
2320int genphy_read_status_fixed(struct phy_device *phydev)
2321{
2322 int bmcr = phy_read(phydev, MII_BMCR);
2323
2324 if (bmcr < 0)
2325 return bmcr;
2326
2327 if (bmcr & BMCR_FULLDPLX)
2328 phydev->duplex = DUPLEX_FULL;
2329 else
2330 phydev->duplex = DUPLEX_HALF;
2331
2332 if (bmcr & BMCR_SPEED1000)
2333 phydev->speed = SPEED_1000;
2334 else if (bmcr & BMCR_SPEED100)
2335 phydev->speed = SPEED_100;
2336 else
2337 phydev->speed = SPEED_10;
2338
2339 return 0;
2340}
2341EXPORT_SYMBOL(genphy_read_status_fixed);
2342
2343/**
Russell King8d3dc3ac2019-10-04 17:06:04 +01002344 * genphy_read_status - check the link status and update current link state
2345 * @phydev: target phy_device struct
2346 *
2347 * Description: Check the link, then figure out the current state
2348 * by comparing what we advertise with what the link partner
2349 * advertises. Start by checking the gigabit possibilities,
2350 * then move on to 10/100.
2351 */
2352int genphy_read_status(struct phy_device *phydev)
2353{
2354 int err, old_link = phydev->link;
2355
2356 /* Update the link, but return if there was an error */
2357 err = genphy_update_link(phydev);
2358 if (err)
2359 return err;
2360
2361 /* why bother the PHY if nothing can have changed */
2362 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2363 return 0;
2364
2365 phydev->speed = SPEED_UNKNOWN;
2366 phydev->duplex = DUPLEX_UNKNOWN;
2367 phydev->pause = 0;
2368 phydev->asym_pause = 0;
2369
Oleksij Rempelbdbdac72020-05-05 08:35:05 +02002370 err = genphy_read_master_slave(phydev);
2371 if (err < 0)
2372 return err;
2373
Russell King8d3dc3ac2019-10-04 17:06:04 +01002374 err = genphy_read_lpa(phydev);
2375 if (err < 0)
2376 return err;
2377
2378 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
Heiner Kallweit5502b212019-02-14 22:16:27 +01002379 phy_resolve_aneg_linkmode(phydev);
Heiner Kallweitb6163f12019-03-30 10:22:45 +01002380 } else if (phydev->autoneg == AUTONEG_DISABLE) {
Russell King0efc2862019-12-17 13:39:16 +00002381 err = genphy_read_status_fixed(phydev);
2382 if (err < 0)
2383 return err;
Andy Fleming00db8182005-07-30 19:31:23 -04002384 }
2385
2386 return 0;
2387}
2388EXPORT_SYMBOL(genphy_read_status);
2389
Florian Fainelli797ac072014-02-17 13:34:02 -08002390/**
Heiner Kallweitfa6e98c2019-10-22 11:31:07 -07002391 * genphy_c37_read_status - check the link status and update current link state
2392 * @phydev: target phy_device struct
2393 *
2394 * Description: Check the link, then figure out the current state
2395 * by comparing what we advertise with what the link partner
2396 * advertises. This function is for Clause 37 1000Base-X mode.
2397 */
2398int genphy_c37_read_status(struct phy_device *phydev)
2399{
2400 int lpa, err, old_link = phydev->link;
2401
2402 /* Update the link, but return if there was an error */
2403 err = genphy_update_link(phydev);
2404 if (err)
2405 return err;
2406
2407 /* why bother the PHY if nothing can have changed */
2408 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2409 return 0;
2410
2411 phydev->duplex = DUPLEX_UNKNOWN;
2412 phydev->pause = 0;
2413 phydev->asym_pause = 0;
2414
2415 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2416 lpa = phy_read(phydev, MII_LPA);
2417 if (lpa < 0)
2418 return lpa;
2419
2420 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2421 phydev->lp_advertising, lpa & LPA_LPACK);
2422 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2423 phydev->lp_advertising, lpa & LPA_1000XFULL);
2424 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2425 phydev->lp_advertising, lpa & LPA_1000XPAUSE);
2426 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2427 phydev->lp_advertising,
2428 lpa & LPA_1000XPAUSE_ASYM);
2429
2430 phy_resolve_aneg_linkmode(phydev);
2431 } else if (phydev->autoneg == AUTONEG_DISABLE) {
2432 int bmcr = phy_read(phydev, MII_BMCR);
2433
2434 if (bmcr < 0)
2435 return bmcr;
2436
2437 if (bmcr & BMCR_FULLDPLX)
2438 phydev->duplex = DUPLEX_FULL;
2439 else
2440 phydev->duplex = DUPLEX_HALF;
2441 }
2442
2443 return 0;
2444}
2445EXPORT_SYMBOL(genphy_c37_read_status);
2446
2447/**
Florian Fainelli797ac072014-02-17 13:34:02 -08002448 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
2449 * @phydev: target phy_device struct
2450 *
2451 * Description: Perform a software PHY reset using the standard
2452 * BMCR_RESET bit and poll for the reset bit to be cleared.
2453 *
2454 * Returns: 0 on success, < 0 on failure
2455 */
2456int genphy_soft_reset(struct phy_device *phydev)
2457{
Heiner Kallweit8c90b792019-04-24 21:41:06 +02002458 u16 res = BMCR_RESET;
Florian Fainelli797ac072014-02-17 13:34:02 -08002459 int ret;
2460
Heiner Kallweit8c90b792019-04-24 21:41:06 +02002461 if (phydev->autoneg == AUTONEG_ENABLE)
2462 res |= BMCR_ANRESTART;
2463
2464 ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res);
Florian Fainelli797ac072014-02-17 13:34:02 -08002465 if (ret < 0)
2466 return ret;
2467
Heiner Kallweit9576e9f2020-04-23 21:38:42 +02002468 /* Clause 22 states that setting bit BMCR_RESET sets control registers
2469 * to their default value. Therefore the POWER DOWN bit is supposed to
2470 * be cleared after soft reset.
2471 */
2472 phydev->suspended = 0;
2473
Heiner Kallweit8c90b792019-04-24 21:41:06 +02002474 ret = phy_poll_reset(phydev);
2475 if (ret)
2476 return ret;
2477
2478 /* BMCR may be reset to defaults */
2479 if (phydev->autoneg == AUTONEG_DISABLE)
2480 ret = genphy_setup_forced(phydev);
2481
2482 return ret;
Florian Fainelli797ac072014-02-17 13:34:02 -08002483}
2484EXPORT_SYMBOL(genphy_soft_reset);
2485
Ioana Ciornei87de1f02020-11-01 14:51:12 +02002486irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev)
2487{
2488 /* It seems there are cases where the interrupts are handled by another
2489 * entity (ie an IRQ controller embedded inside the PHY) and do not
2490 * need any other interraction from phylib. In this case, just trigger
2491 * the state machine directly.
2492 */
2493 phy_trigger_machine(phydev);
2494
2495 return 0;
2496}
2497EXPORT_SYMBOL(genphy_handle_interrupt_no_ack);
2498
Heiner Kallweit045925e2019-03-27 21:58:44 +01002499/**
2500 * genphy_read_abilities - read PHY abilities from Clause 22 registers
2501 * @phydev: target phy_device struct
2502 *
2503 * Description: Reads the PHY's abilities and populates
2504 * phydev->supported accordingly.
2505 *
2506 * Returns: 0 on success, < 0 on failure
2507 */
2508int genphy_read_abilities(struct phy_device *phydev)
2509{
2510 int val;
2511
2512 linkmode_set_bit_array(phy_basic_ports_array,
2513 ARRAY_SIZE(phy_basic_ports_array),
2514 phydev->supported);
2515
2516 val = phy_read(phydev, MII_BMSR);
2517 if (val < 0)
2518 return val;
2519
2520 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
2521 val & BMSR_ANEGCAPABLE);
2522
2523 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
2524 val & BMSR_100FULL);
2525 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
2526 val & BMSR_100HALF);
2527 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
2528 val & BMSR_10FULL);
2529 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
2530 val & BMSR_10HALF);
2531
2532 if (val & BMSR_ESTATEN) {
2533 val = phy_read(phydev, MII_ESTATUS);
2534 if (val < 0)
2535 return val;
2536
2537 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2538 phydev->supported, val & ESTATUS_1000_TFULL);
2539 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2540 phydev->supported, val & ESTATUS_1000_THALF);
Robert Hancockf30e33b2019-06-11 16:06:09 -06002541 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2542 phydev->supported, val & ESTATUS_1000_XFULL);
Heiner Kallweit045925e2019-03-27 21:58:44 +01002543 }
2544
2545 return 0;
2546}
2547EXPORT_SYMBOL(genphy_read_abilities);
2548
Kevin Hao5df7af82018-03-20 09:44:52 +08002549/* This is used for the phy device which doesn't support the MMD extended
2550 * register access, but it does have side effect when we are trying to access
2551 * the MMD register via indirect method.
2552 */
2553int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
2554{
2555 return -EOPNOTSUPP;
2556}
2557EXPORT_SYMBOL(genphy_read_mmd_unsupported);
2558
2559int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
2560 u16 regnum, u16 val)
2561{
2562 return -EOPNOTSUPP;
2563}
2564EXPORT_SYMBOL(genphy_write_mmd_unsupported);
2565
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08002566int genphy_suspend(struct phy_device *phydev)
2567{
Heiner Kallweit032f4702018-01-12 21:20:36 +01002568 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08002569}
2570EXPORT_SYMBOL(genphy_suspend);
2571
2572int genphy_resume(struct phy_device *phydev)
2573{
Heiner Kallweit032f4702018-01-12 21:20:36 +01002574 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08002575}
2576EXPORT_SYMBOL(genphy_resume);
Andy Fleming00db8182005-07-30 19:31:23 -04002577
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08002578int genphy_loopback(struct phy_device *phydev, bool enable)
2579{
Oleksij Rempel014068d2021-04-19 15:01:02 +02002580 if (enable) {
2581 u16 val, ctl = BMCR_LOOPBACK;
2582 int ret;
2583
2584 if (phydev->speed == SPEED_1000)
2585 ctl |= BMCR_SPEED1000;
2586 else if (phydev->speed == SPEED_100)
2587 ctl |= BMCR_SPEED100;
2588
2589 if (phydev->duplex == DUPLEX_FULL)
2590 ctl |= BMCR_FULLDPLX;
2591
2592 phy_modify(phydev, MII_BMCR, ~0, ctl);
2593
2594 ret = phy_read_poll_timeout(phydev, MII_BMSR, val,
2595 val & BMSR_LSTATUS,
2596 5000, 500000, true);
2597 if (ret)
2598 return ret;
2599 } else {
2600 phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, 0);
2601
2602 phy_config_aneg(phydev);
2603 }
2604
2605 return 0;
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08002606}
2607EXPORT_SYMBOL(genphy_loopback);
2608
Andrew Lunn41124fa2018-09-12 01:53:14 +02002609/**
2610 * phy_remove_link_mode - Remove a supported link mode
2611 * @phydev: phy_device structure to remove link mode from
2612 * @link_mode: Link mode to be removed
2613 *
2614 * Description: Some MACs don't support all link modes which the PHY
2615 * does. e.g. a 1G MAC often does not support 1000Half. Add a helper
2616 * to remove a link mode.
2617 */
2618void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
2619{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002620 linkmode_clear_bit(link_mode, phydev->supported);
Heiner Kallweit22c0ef62019-05-01 21:34:43 +02002621 phy_advertise_supported(phydev);
Andrew Lunn41124fa2018-09-12 01:53:14 +02002622}
2623EXPORT_SYMBOL(phy_remove_link_mode);
2624
Heiner Kallweit22c0ef62019-05-01 21:34:43 +02002625static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src)
2626{
2627 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst,
2628 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src));
2629 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst,
2630 linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src));
2631}
2632
2633/**
2634 * phy_advertise_supported - Advertise all supported modes
2635 * @phydev: target phy_device struct
2636 *
2637 * Description: Called to advertise all supported modes, doesn't touch
2638 * pause mode advertising.
2639 */
2640void phy_advertise_supported(struct phy_device *phydev)
2641{
2642 __ETHTOOL_DECLARE_LINK_MODE_MASK(new);
2643
2644 linkmode_copy(new, phydev->supported);
2645 phy_copy_pause_bits(new, phydev->advertising);
2646 linkmode_copy(phydev->advertising, new);
2647}
2648EXPORT_SYMBOL(phy_advertise_supported);
2649
Andrew Lunnaf8d9bb2018-09-12 01:53:15 +02002650/**
Andrew Lunnc306ad32018-09-12 01:53:16 +02002651 * phy_support_sym_pause - Enable support of symmetrical pause
2652 * @phydev: target phy_device struct
2653 *
2654 * Description: Called by the MAC to indicate is supports symmetrical
2655 * Pause, but not asym pause.
2656 */
2657void phy_support_sym_pause(struct phy_device *phydev)
2658{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002659 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
Heiner Kallweit22c0ef62019-05-01 21:34:43 +02002660 phy_copy_pause_bits(phydev->advertising, phydev->supported);
Andrew Lunnc306ad32018-09-12 01:53:16 +02002661}
2662EXPORT_SYMBOL(phy_support_sym_pause);
2663
2664/**
Andrew Lunnaf8d9bb2018-09-12 01:53:15 +02002665 * phy_support_asym_pause - Enable support of asym pause
2666 * @phydev: target phy_device struct
2667 *
2668 * Description: Called by the MAC to indicate is supports Asym Pause.
2669 */
2670void phy_support_asym_pause(struct phy_device *phydev)
2671{
Heiner Kallweit22c0ef62019-05-01 21:34:43 +02002672 phy_copy_pause_bits(phydev->advertising, phydev->supported);
Andrew Lunnaf8d9bb2018-09-12 01:53:15 +02002673}
2674EXPORT_SYMBOL(phy_support_asym_pause);
2675
Andrew Lunn70814e82018-09-12 01:53:17 +02002676/**
Andrew Lunn0c122402018-09-12 01:53:18 +02002677 * phy_set_sym_pause - Configure symmetric Pause
2678 * @phydev: target phy_device struct
2679 * @rx: Receiver Pause is supported
2680 * @tx: Transmit Pause is supported
2681 * @autoneg: Auto neg should be used
2682 *
2683 * Description: Configure advertised Pause support depending on if
2684 * receiver pause and pause auto neg is supported. Generally called
2685 * from the set_pauseparam .ndo.
2686 */
2687void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
2688 bool autoneg)
2689{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002690 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
Andrew Lunn0c122402018-09-12 01:53:18 +02002691
2692 if (rx && tx && autoneg)
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002693 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2694 phydev->supported);
Andrew Lunn0c122402018-09-12 01:53:18 +02002695
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002696 linkmode_copy(phydev->advertising, phydev->supported);
Andrew Lunn0c122402018-09-12 01:53:18 +02002697}
2698EXPORT_SYMBOL(phy_set_sym_pause);
2699
2700/**
Andrew Lunn70814e82018-09-12 01:53:17 +02002701 * phy_set_asym_pause - Configure Pause and Asym Pause
2702 * @phydev: target phy_device struct
2703 * @rx: Receiver Pause is supported
2704 * @tx: Transmit Pause is supported
2705 *
2706 * Description: Configure advertised Pause support depending on if
2707 * transmit and receiver pause is supported. If there has been a
2708 * change in adverting, trigger a new autoneg. Generally called from
2709 * the set_pauseparam .ndo.
2710 */
2711void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
2712{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002713 __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
Andrew Lunn70814e82018-09-12 01:53:17 +02002714
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002715 linkmode_copy(oldadv, phydev->advertising);
Russell King45c767f2020-02-15 15:49:32 +00002716 linkmode_set_pause(phydev->advertising, tx, rx);
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002717
2718 if (!linkmode_equal(oldadv, phydev->advertising) &&
2719 phydev->autoneg)
2720 phy_start_aneg(phydev);
Andrew Lunn70814e82018-09-12 01:53:17 +02002721}
2722EXPORT_SYMBOL(phy_set_asym_pause);
2723
Andrew Lunn22b7d292018-09-12 01:53:19 +02002724/**
2725 * phy_validate_pause - Test if the PHY/MAC support the pause configuration
2726 * @phydev: phy_device struct
2727 * @pp: requested pause configuration
2728 *
2729 * Description: Test if the PHY/MAC combination supports the Pause
2730 * configuration the user is requesting. Returns True if it is
2731 * supported, false otherwise.
2732 */
2733bool phy_validate_pause(struct phy_device *phydev,
2734 struct ethtool_pauseparam *pp)
2735{
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002736 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
Heiner Kallweitb4010af2019-05-01 21:54:28 +02002737 phydev->supported) && pp->rx_pause)
Andrew Lunn22b7d292018-09-12 01:53:19 +02002738 return false;
Heiner Kallweitb4010af2019-05-01 21:54:28 +02002739
2740 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2741 phydev->supported) &&
2742 pp->rx_pause != pp->tx_pause)
2743 return false;
2744
Andrew Lunn22b7d292018-09-12 01:53:19 +02002745 return true;
2746}
2747EXPORT_SYMBOL(phy_validate_pause);
2748
Russell Kinga87ae8a2020-02-15 15:49:27 +00002749/**
2750 * phy_get_pause - resolve negotiated pause modes
2751 * @phydev: phy_device struct
2752 * @tx_pause: pointer to bool to indicate whether transmit pause should be
2753 * enabled.
2754 * @rx_pause: pointer to bool to indicate whether receive pause should be
2755 * enabled.
2756 *
2757 * Resolve and return the flow control modes according to the negotiation
2758 * result. This includes checking that we are operating in full duplex mode.
2759 * See linkmode_resolve_pause() for further details.
2760 */
2761void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause)
2762{
2763 if (phydev->duplex != DUPLEX_FULL) {
2764 *tx_pause = false;
2765 *rx_pause = false;
2766 return;
2767 }
2768
2769 return linkmode_resolve_pause(phydev->advertising,
2770 phydev->lp_advertising,
2771 tx_pause, rx_pause);
2772}
2773EXPORT_SYMBOL(phy_get_pause);
2774
Dan Murphy92252ee2020-06-24 07:16:02 -05002775#if IS_ENABLED(CONFIG_OF_MDIO)
2776static int phy_get_int_delay_property(struct device *dev, const char *name)
2777{
2778 s32 int_delay;
2779 int ret;
2780
2781 ret = device_property_read_u32(dev, name, &int_delay);
2782 if (ret)
2783 return ret;
2784
2785 return int_delay;
2786}
2787#else
2788static int phy_get_int_delay_property(struct device *dev, const char *name)
2789{
2790 return -EINVAL;
2791}
2792#endif
2793
2794/**
Mauro Carvalho Chehab69280222020-11-16 11:17:57 +01002795 * phy_get_internal_delay - returns the index of the internal delay
Dan Murphy92252ee2020-06-24 07:16:02 -05002796 * @phydev: phy_device struct
2797 * @dev: pointer to the devices device struct
2798 * @delay_values: array of delays the PHY supports
2799 * @size: the size of the delay array
2800 * @is_rx: boolean to indicate to get the rx internal delay
2801 *
2802 * Returns the index within the array of internal delay passed in.
2803 * If the device property is not present then the interface type is checked
2804 * if the interface defines use of internal delay then a 1 is returned otherwise
2805 * a 0 is returned.
2806 * The array must be in ascending order. If PHY does not have an ascending order
2807 * array then size = 0 and the value of the delay property is returned.
2808 * Return -EINVAL if the delay is invalid or cannot be found.
2809 */
2810s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev,
2811 const int *delay_values, int size, bool is_rx)
2812{
2813 s32 delay;
2814 int i;
2815
2816 if (is_rx) {
2817 delay = phy_get_int_delay_property(dev, "rx-internal-delay-ps");
2818 if (delay < 0 && size == 0) {
2819 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
2820 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
2821 return 1;
2822 else
2823 return 0;
2824 }
2825
2826 } else {
2827 delay = phy_get_int_delay_property(dev, "tx-internal-delay-ps");
2828 if (delay < 0 && size == 0) {
2829 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
2830 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
2831 return 1;
2832 else
2833 return 0;
2834 }
2835 }
2836
2837 if (delay < 0)
2838 return delay;
2839
2840 if (delay && size == 0)
2841 return delay;
2842
2843 if (delay < delay_values[0] || delay > delay_values[size - 1]) {
2844 phydev_err(phydev, "Delay %d is out of range\n", delay);
2845 return -EINVAL;
2846 }
2847
2848 if (delay == delay_values[0])
2849 return 0;
2850
2851 for (i = 1; i < size; i++) {
2852 if (delay == delay_values[i])
2853 return i;
2854
2855 /* Find an approximate index by looking up the table */
2856 if (delay > delay_values[i - 1] &&
2857 delay < delay_values[i]) {
2858 if (delay - delay_values[i - 1] <
2859 delay_values[i] - delay)
2860 return i - 1;
2861 else
2862 return i;
2863 }
2864 }
2865
2866 phydev_err(phydev, "error finding internal delay index for %d\n",
2867 delay);
2868
2869 return -EINVAL;
2870}
2871EXPORT_SYMBOL(phy_get_internal_delay);
2872
Heiner Kallweit0d2e7782018-11-09 18:16:28 +01002873static bool phy_drv_supports_irq(struct phy_driver *phydrv)
2874{
Ioana Ciornei6527b932020-11-23 17:38:17 +02002875 return phydrv->config_intr && phydrv->handle_interrupt;
Heiner Kallweit0d2e7782018-11-09 18:16:28 +01002876}
2877
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002878/**
2879 * phy_probe - probe and init a PHY device
2880 * @dev: device to probe and init
Andy Fleming00db8182005-07-30 19:31:23 -04002881 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -08002882 * Description: Take care of setting up the phy_device structure,
Andy Fleming00db8182005-07-30 19:31:23 -04002883 * set the state to READY (the driver's init function should
2884 * set it to STARTING if needed).
2885 */
2886static int phy_probe(struct device *dev)
2887{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03002888 struct phy_device *phydev = to_phy_device(dev);
Andrew Lunne5a03bf2016-01-06 20:11:16 +01002889 struct device_driver *drv = phydev->mdio.dev.driver;
Sergei Shtylyov553fe922014-01-05 03:23:19 +03002890 struct phy_driver *phydrv = to_phy_driver(drv);
Andy Fleming00db8182005-07-30 19:31:23 -04002891 int err = 0;
2892
Andy Fleming00db8182005-07-30 19:31:23 -04002893 phydev->drv = phydrv;
2894
Florian Fainelli2c7b4922013-05-19 22:53:42 +00002895 /* Disable the interrupt if the PHY doesn't support it
2896 * but the interrupt is still a valid one
2897 */
Jiapeng Chongb2698752021-05-21 17:40:14 +08002898 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
Andy Fleming00db8182005-07-30 19:31:23 -04002899 phydev->irq = PHY_POLL;
2900
Florian Fainelli4284b6a2013-05-23 01:11:12 +00002901 if (phydrv->flags & PHY_IS_INTERNAL)
2902 phydev->is_internal = true;
2903
Nate Case35b5f6b2008-01-29 10:05:09 -06002904 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04002905
Bartosz Golaszewski1dba6992020-06-26 17:53:24 +02002906 /* Deassert the reset signal */
2907 phy_device_reset(phydev, 0);
Andrew Lunn92ed2eb2019-02-09 14:46:30 +01002908
Bartosz Golaszewski1dba6992020-06-26 17:53:24 +02002909 if (phydev->drv->probe) {
Andrew Lunn92ed2eb2019-02-09 14:46:30 +01002910 err = phydev->drv->probe(phydev);
Bartosz Golaszewski1dba6992020-06-26 17:53:24 +02002911 if (err)
Andrew Lunn92ed2eb2019-02-09 14:46:30 +01002912 goto out;
Andrew Lunn92ed2eb2019-02-09 14:46:30 +01002913 }
2914
Andy Fleming00db8182005-07-30 19:31:23 -04002915 /* Start out supporting everything. Eventually,
2916 * a controller will attach, and may modify one
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03002917 * or both of these values
2918 */
Andrew Lunnefbdfdc2019-02-09 15:24:47 +01002919 if (phydrv->features) {
2920 linkmode_copy(phydev->supported, phydrv->features);
Heiner Kallweita1deab12019-04-03 23:14:33 +02002921 } else if (phydrv->get_features) {
Andrew Lunnefbdfdc2019-02-09 15:24:47 +01002922 err = phydrv->get_features(phydev);
Heiner Kallweita1deab12019-04-03 23:14:33 +02002923 } else if (phydev->is_c45) {
2924 err = genphy_c45_pma_read_abilities(phydev);
2925 } else {
2926 err = genphy_read_abilities(phydev);
Andrew Lunnefbdfdc2019-02-09 15:24:47 +01002927 }
2928
Heiner Kallweita1deab12019-04-03 23:14:33 +02002929 if (err)
2930 goto out;
2931
Heiner Kallweit5e425742019-04-16 23:54:00 +02002932 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2933 phydev->supported))
2934 phydev->autoneg = 0;
2935
Heiner Kallweit3b8b11f2019-04-05 21:23:13 +02002936 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2937 phydev->supported))
2938 phydev->is_gigabit_capable = 1;
2939 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2940 phydev->supported))
2941 phydev->is_gigabit_capable = 1;
2942
Sascha Hauerde906af2014-05-21 15:29:45 +02002943 of_set_phy_supported(phydev);
Heiner Kallweit22c0ef62019-05-01 21:34:43 +02002944 phy_advertise_supported(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -04002945
jbrunetd853d142016-11-28 10:46:46 +01002946 /* Get the EEE modes we want to prohibit. We will ask
2947 * the PHY stop advertising these mode later on
2948 */
2949 of_set_phy_eee_broken(phydev);
2950
Timur Tabi529ed122016-12-07 13:20:51 -06002951 /* The Pause Frame bits indicate that the PHY can support passing
2952 * pause frames. During autonegotiation, the PHYs will determine if
2953 * they should allow pause frames to pass. The MAC driver should then
2954 * use that result to determine whether to enable flow control via
2955 * pause frames.
2956 *
2957 * Normally, PHY drivers should not set the Pause bits, and instead
2958 * allow phylib to do that. However, there may be some situations
2959 * (e.g. hardware erratum) where the driver wants to set only one
2960 * of these bits.
2961 */
Andrew Lunnefbdfdc2019-02-09 15:24:47 +01002962 if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
2963 !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01002964 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2965 phydev->supported);
2966 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2967 phydev->supported);
Timur Tabi529ed122016-12-07 13:20:51 -06002968 }
2969
Andy Fleming00db8182005-07-30 19:31:23 -04002970 /* Set the state to READY by default */
2971 phydev->state = PHY_READY;
2972
Andrew Lunn92ed2eb2019-02-09 14:46:30 +01002973out:
Bartosz Golaszewski1dba6992020-06-26 17:53:24 +02002974 /* Assert the reset signal */
2975 if (err)
2976 phy_device_reset(phydev, 1);
2977
Nate Case35b5f6b2008-01-29 10:05:09 -06002978 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04002979
Andy Fleming00db8182005-07-30 19:31:23 -04002980 return err;
2981}
2982
2983static int phy_remove(struct device *dev)
2984{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03002985 struct phy_device *phydev = to_phy_device(dev);
Andy Fleming00db8182005-07-30 19:31:23 -04002986
Florian Fainelli7b9a88a2017-02-17 16:07:33 -08002987 cancel_delayed_work_sync(&phydev->state_queue);
2988
Nate Case35b5f6b2008-01-29 10:05:09 -06002989 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04002990 phydev->state = PHY_DOWN;
Nate Case35b5f6b2008-01-29 10:05:09 -06002991 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04002992
Russell King298e54f2019-11-15 19:56:51 +00002993 sfp_bus_del_upstream(phydev->sfp_bus);
2994 phydev->sfp_bus = NULL;
2995
Bartosz Golaszewski1dba6992020-06-26 17:53:24 +02002996 if (phydev->drv && phydev->drv->remove)
Andy Fleming00db8182005-07-30 19:31:23 -04002997 phydev->drv->remove(phydev);
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +01002998
Bartosz Golaszewski1dba6992020-06-26 17:53:24 +02002999 /* Assert the reset signal */
3000 phy_device_reset(phydev, 1);
3001
Andy Fleming00db8182005-07-30 19:31:23 -04003002 phydev->drv = NULL;
3003
3004 return 0;
3005}
3006
Ioana Ciorneie2f016c2020-11-01 14:50:57 +02003007static void phy_shutdown(struct device *dev)
3008{
3009 struct phy_device *phydev = to_phy_device(dev);
3010
3011 phy_disable_interrupts(phydev);
3012}
3013
Randy Dunlapb3df0da2007-03-06 02:41:48 -08003014/**
3015 * phy_driver_register - register a phy_driver with the PHY layer
3016 * @new_driver: new phy_driver to register
Andrew Lunnbe01da72016-01-06 20:11:22 +01003017 * @owner: module owning this PHY
Randy Dunlapb3df0da2007-03-06 02:41:48 -08003018 */
Andrew Lunnbe01da72016-01-06 20:11:22 +01003019int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
Andy Fleming00db8182005-07-30 19:31:23 -04003020{
3021 int retval;
3022
Andrew Lunnefbdfdc2019-02-09 15:24:47 +01003023 /* Either the features are hard coded, or dynamically
Heiner Kallweita1deab12019-04-03 23:14:33 +02003024 * determined. It cannot be both.
Andrew Lunnefbdfdc2019-02-09 15:24:47 +01003025 */
Heiner Kallweita1deab12019-04-03 23:14:33 +02003026 if (WARN_ON(new_driver->features && new_driver->get_features)) {
3027 pr_err("%s: features and get_features must not both be set\n",
3028 new_driver->name);
Camelia Groza3e64cf72019-01-17 14:22:36 +02003029 return -EINVAL;
3030 }
3031
Andrew Lunna9049e02016-01-06 20:11:26 +01003032 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
3033 new_driver->mdiodrv.driver.name = new_driver->name;
3034 new_driver->mdiodrv.driver.bus = &mdio_bus_type;
3035 new_driver->mdiodrv.driver.probe = phy_probe;
3036 new_driver->mdiodrv.driver.remove = phy_remove;
Ioana Ciorneie2f016c2020-11-01 14:50:57 +02003037 new_driver->mdiodrv.driver.shutdown = phy_shutdown;
Andrew Lunna9049e02016-01-06 20:11:26 +01003038 new_driver->mdiodrv.driver.owner = owner;
Heiner Kallweit16983502020-03-27 01:00:22 +01003039 new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
Andy Fleming00db8182005-07-30 19:31:23 -04003040
Andrew Lunna9049e02016-01-06 20:11:26 +01003041 retval = driver_register(&new_driver->mdiodrv.driver);
Andy Fleming00db8182005-07-30 19:31:23 -04003042 if (retval) {
Joe Perches8d242482012-06-09 07:49:07 +00003043 pr_err("%s: Error %d in registering driver\n",
3044 new_driver->name, retval);
Andy Fleming00db8182005-07-30 19:31:23 -04003045
3046 return retval;
3047 }
3048
Olof Johanssonf2511f12007-11-04 16:09:23 -06003049 pr_debug("%s: Registered new driver\n", new_driver->name);
Andy Fleming00db8182005-07-30 19:31:23 -04003050
3051 return 0;
3052}
3053EXPORT_SYMBOL(phy_driver_register);
3054
Andrew Lunnbe01da72016-01-06 20:11:22 +01003055int phy_drivers_register(struct phy_driver *new_driver, int n,
3056 struct module *owner)
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00003057{
3058 int i, ret = 0;
3059
3060 for (i = 0; i < n; i++) {
Andrew Lunnbe01da72016-01-06 20:11:22 +01003061 ret = phy_driver_register(new_driver + i, owner);
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00003062 if (ret) {
3063 while (i-- > 0)
3064 phy_driver_unregister(new_driver + i);
3065 break;
3066 }
3067 }
3068 return ret;
3069}
3070EXPORT_SYMBOL(phy_drivers_register);
3071
Andy Fleming00db8182005-07-30 19:31:23 -04003072void phy_driver_unregister(struct phy_driver *drv)
3073{
Andrew Lunna9049e02016-01-06 20:11:26 +01003074 driver_unregister(&drv->mdiodrv.driver);
Andy Fleming00db8182005-07-30 19:31:23 -04003075}
3076EXPORT_SYMBOL(phy_driver_unregister);
3077
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00003078void phy_drivers_unregister(struct phy_driver *drv, int n)
3079{
3080 int i;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03003081
3082 for (i = 0; i < n; i++)
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00003083 phy_driver_unregister(drv + i);
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +00003084}
3085EXPORT_SYMBOL(phy_drivers_unregister);
3086
Russell King921690f2017-06-05 12:23:05 +01003087static struct phy_driver genphy_driver = {
Andy Fleminge1393452005-08-24 18:46:21 -05003088 .phy_id = 0xffffffff,
3089 .phy_id_mask = 0xffffffff,
3090 .name = "Generic PHY",
Heiner Kallweit2a4d8672019-03-27 21:59:33 +01003091 .get_features = genphy_read_abilities,
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -08003092 .suspend = genphy_suspend,
3093 .resume = genphy_resume,
Lin Yun Shengf0f9b4e2017-06-30 17:44:15 +08003094 .set_loopback = genphy_loopback,
Russell King921690f2017-06-05 12:23:05 +01003095};
Andy Fleming00db8182005-07-30 19:31:23 -04003096
Florian Fainelli55d8f052020-07-05 21:27:57 -07003097static const struct ethtool_phy_ops phy_ethtool_phy_ops = {
Florian Fainellibd36ed12020-07-08 09:46:24 -07003098 .get_sset_count = phy_ethtool_get_sset_count,
3099 .get_strings = phy_ethtool_get_strings,
3100 .get_stats = phy_ethtool_get_stats,
Florian Fainelli55d8f052020-07-05 21:27:57 -07003101 .start_cable_test = phy_start_cable_test,
3102 .start_cable_test_tdr = phy_start_cable_test_tdr,
3103};
3104
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04003105static int __init phy_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -04003106{
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04003107 int rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04003108
3109 rc = mdio_bus_init();
3110 if (rc)
Andy Fleminge1393452005-08-24 18:46:21 -05003111 return rc;
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04003112
Florian Fainelli55d8f052020-07-05 21:27:57 -07003113 ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops);
Andrew Lunn719655a2018-09-29 23:04:16 +02003114 features_init();
3115
Heiner Kallweit22b56e82019-04-07 13:55:01 +02003116 rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE);
Andy Fleminge1393452005-08-24 18:46:21 -05003117 if (rc)
Heiner Kallweit22b56e82019-04-07 13:55:01 +02003118 goto err_c45;
Russell King921690f2017-06-05 12:23:05 +01003119
3120 rc = phy_driver_register(&genphy_driver, THIS_MODULE);
3121 if (rc) {
Heiner Kallweit22b56e82019-04-07 13:55:01 +02003122 phy_driver_unregister(&genphy_c45_driver);
3123err_c45:
Andy Fleminge1393452005-08-24 18:46:21 -05003124 mdio_bus_exit();
Russell King921690f2017-06-05 12:23:05 +01003125 }
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04003126
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04003127 return rc;
Andy Fleming00db8182005-07-30 19:31:23 -04003128}
3129
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04003130static void __exit phy_exit(void)
Andy Fleming00db8182005-07-30 19:31:23 -04003131{
Heiner Kallweit22b56e82019-04-07 13:55:01 +02003132 phy_driver_unregister(&genphy_c45_driver);
Russell King921690f2017-06-05 12:23:05 +01003133 phy_driver_unregister(&genphy_driver);
Andy Fleminge1393452005-08-24 18:46:21 -05003134 mdio_bus_exit();
Florian Fainelli55d8f052020-07-05 21:27:57 -07003135 ethtool_set_ethtool_phy_ops(NULL);
Andy Fleming00db8182005-07-30 19:31:23 -04003136}
3137
Andy Fleminge1393452005-08-24 18:46:21 -05003138subsys_initcall(phy_init);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04003139module_exit(phy_exit);