Commit | Line | Data |
---|---|---|
a2443fd1 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
2f53e904 | 2 | /* Framework for finding and configuring PHYs. |
00db8189 AF |
3 | * Also contains generic PHY driver |
4 | * | |
5 | * Author: Andy Fleming | |
6 | * | |
7 | * Copyright (c) 2004 Freescale Semiconductor, Inc. | |
00db8189 | 8 | */ |
8d242488 JP |
9 | |
10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
11 | ||
425775ed | 12 | #include <linux/acpi.h> |
e42bcd0f | 13 | #include <linux/bitmap.h> |
00db8189 | 14 | #include <linux/delay.h> |
e42bcd0f | 15 | #include <linux/errno.h> |
00db8189 | 16 | #include <linux/etherdevice.h> |
e42bcd0f BG |
17 | #include <linux/ethtool.h> |
18 | #include <linux/init.h> | |
19 | #include <linux/interrupt.h> | |
20 | #include <linux/io.h> | |
21 | #include <linux/kernel.h> | |
01e5b728 | 22 | #include <linux/list.h> |
e42bcd0f BG |
23 | #include <linux/mdio.h> |
24 | #include <linux/mii.h> | |
00db8189 AF |
25 | #include <linux/mm.h> |
26 | #include <linux/module.h> | |
01e5b728 | 27 | #include <linux/of.h> |
e42bcd0f | 28 | #include <linux/netdevice.h> |
00db8189 | 29 | #include <linux/phy.h> |
60495b66 | 30 | #include <linux/phylib_stubs.h> |
2e0bc452 | 31 | #include <linux/phy_led_triggers.h> |
38496878 | 32 | #include <linux/phy_link_topology.h> |
5e82147d | 33 | #include <linux/pse-pd/pse.h> |
e42bcd0f | 34 | #include <linux/property.h> |
35f7cad1 | 35 | #include <linux/ptp_clock_kernel.h> |
70ef7d87 | 36 | #include <linux/rtnetlink.h> |
298e54fa | 37 | #include <linux/sfp.h> |
e42bcd0f BG |
38 | #include <linux/skbuff.h> |
39 | #include <linux/slab.h> | |
40 | #include <linux/string.h> | |
2f53e904 | 41 | #include <linux/uaccess.h> |
e42bcd0f | 42 | #include <linux/unistd.h> |
00db8189 | 43 | |
a3e51d47 | 44 | #include "phylib-internal.h" |
d8c838a5 | 45 | #include "phy-caps.h" |
a3e51d47 | 46 | |
afcceaa3 OH |
47 | MODULE_DESCRIPTION("PHY library"); |
48 | MODULE_AUTHOR("Andy Fleming"); | |
49 | MODULE_LICENSE("GPL"); | |
50 | ||
ea47e70e HK |
51 | #define PHY_ANY_ID "MATCH ANY PHY" |
52 | #define PHY_ANY_UID 0xffffffff | |
53 | ||
54 | struct phy_fixup { | |
55 | struct list_head list; | |
56 | char bus_id[MII_BUS_ID_SIZE + 3]; | |
57 | u32 phy_uid; | |
58 | u32 phy_uid_mask; | |
59 | int (*run)(struct phy_device *phydev); | |
60 | }; | |
61 | ||
719655a1 AL |
62 | __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init; |
63 | EXPORT_SYMBOL_GPL(phy_basic_features); | |
64 | ||
65 | __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init; | |
66 | EXPORT_SYMBOL_GPL(phy_basic_t1_features); | |
67 | ||
16178c8e PB |
68 | __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1s_p2mp_features) __ro_after_init; |
69 | EXPORT_SYMBOL_GPL(phy_basic_t1s_p2mp_features); | |
70 | ||
719655a1 AL |
71 | __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init; |
72 | EXPORT_SYMBOL_GPL(phy_gbit_features); | |
73 | ||
74 | __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init; | |
75 | EXPORT_SYMBOL_GPL(phy_gbit_fibre_features); | |
76 | ||
719655a1 AL |
77 | __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init; |
78 | EXPORT_SYMBOL_GPL(phy_10gbit_features); | |
79 | ||
54638c6e | 80 | const int phy_basic_ports_array[3] = { |
719655a1 AL |
81 | ETHTOOL_LINK_MODE_Autoneg_BIT, |
82 | ETHTOOL_LINK_MODE_TP_BIT, | |
83 | ETHTOOL_LINK_MODE_MII_BIT, | |
84 | }; | |
3c1bcc86 | 85 | EXPORT_SYMBOL_GPL(phy_basic_ports_array); |
719655a1 | 86 | |
12d5151b | 87 | static const int phy_all_ports_features_array[7] = { |
719655a1 AL |
88 | ETHTOOL_LINK_MODE_Autoneg_BIT, |
89 | ETHTOOL_LINK_MODE_TP_BIT, | |
90 | ETHTOOL_LINK_MODE_MII_BIT, | |
91 | ETHTOOL_LINK_MODE_FIBRE_BIT, | |
92 | ETHTOOL_LINK_MODE_AUI_BIT, | |
93 | ETHTOOL_LINK_MODE_BNC_BIT, | |
94 | ETHTOOL_LINK_MODE_Backplane_BIT, | |
95 | }; | |
96 | ||
d3a0e217 | 97 | static const int phy_10_100_features_array[4] = { |
719655a1 AL |
98 | ETHTOOL_LINK_MODE_10baseT_Half_BIT, |
99 | ETHTOOL_LINK_MODE_10baseT_Full_BIT, | |
100 | ETHTOOL_LINK_MODE_100baseT_Half_BIT, | |
101 | ETHTOOL_LINK_MODE_100baseT_Full_BIT, | |
102 | }; | |
103 | ||
d3a0e217 | 104 | static const int phy_basic_t1_features_array[3] = { |
719655a1 | 105 | ETHTOOL_LINK_MODE_TP_BIT, |
3254e0b9 | 106 | ETHTOOL_LINK_MODE_10baseT1L_Full_BIT, |
e5fb32c6 | 107 | ETHTOOL_LINK_MODE_100baseT1_Full_BIT, |
719655a1 AL |
108 | }; |
109 | ||
d3a0e217 | 110 | static const int phy_basic_t1s_p2mp_features_array[2] = { |
16178c8e PB |
111 | ETHTOOL_LINK_MODE_TP_BIT, |
112 | ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT, | |
113 | }; | |
16178c8e | 114 | |
d3a0e217 | 115 | static const int phy_gbit_features_array[2] = { |
719655a1 AL |
116 | ETHTOOL_LINK_MODE_1000baseT_Half_BIT, |
117 | ETHTOOL_LINK_MODE_1000baseT_Full_BIT, | |
118 | }; | |
719655a1 | 119 | |
14e47d1f OR |
120 | static const int phy_eee_cap1_features_array[] = { |
121 | ETHTOOL_LINK_MODE_100baseT_Full_BIT, | |
122 | ETHTOOL_LINK_MODE_1000baseT_Full_BIT, | |
123 | ETHTOOL_LINK_MODE_10000baseT_Full_BIT, | |
124 | ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, | |
125 | ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, | |
126 | ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, | |
127 | }; | |
128 | ||
129 | __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap1_features) __ro_after_init; | |
130 | EXPORT_SYMBOL_GPL(phy_eee_cap1_features); | |
131 | ||
ef6ee3a3 HK |
132 | static const int phy_eee_cap2_features_array[] = { |
133 | ETHTOOL_LINK_MODE_2500baseT_Full_BIT, | |
134 | ETHTOOL_LINK_MODE_5000baseT_Full_BIT, | |
135 | }; | |
136 | ||
137 | __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap2_features) __ro_after_init; | |
138 | EXPORT_SYMBOL_GPL(phy_eee_cap2_features); | |
139 | ||
719655a1 AL |
140 | static void features_init(void) |
141 | { | |
142 | /* 10/100 half/full*/ | |
143 | linkmode_set_bit_array(phy_basic_ports_array, | |
144 | ARRAY_SIZE(phy_basic_ports_array), | |
145 | phy_basic_features); | |
146 | linkmode_set_bit_array(phy_10_100_features_array, | |
147 | ARRAY_SIZE(phy_10_100_features_array), | |
148 | phy_basic_features); | |
149 | ||
150 | /* 100 full, TP */ | |
151 | linkmode_set_bit_array(phy_basic_t1_features_array, | |
152 | ARRAY_SIZE(phy_basic_t1_features_array), | |
153 | phy_basic_t1_features); | |
154 | ||
16178c8e PB |
155 | /* 10 half, P2MP, TP */ |
156 | linkmode_set_bit_array(phy_basic_t1s_p2mp_features_array, | |
157 | ARRAY_SIZE(phy_basic_t1s_p2mp_features_array), | |
158 | phy_basic_t1s_p2mp_features); | |
159 | ||
719655a1 AL |
160 | /* 10/100 half/full + 1000 half/full */ |
161 | linkmode_set_bit_array(phy_basic_ports_array, | |
162 | ARRAY_SIZE(phy_basic_ports_array), | |
163 | phy_gbit_features); | |
164 | linkmode_set_bit_array(phy_10_100_features_array, | |
165 | ARRAY_SIZE(phy_10_100_features_array), | |
166 | phy_gbit_features); | |
167 | linkmode_set_bit_array(phy_gbit_features_array, | |
168 | ARRAY_SIZE(phy_gbit_features_array), | |
169 | phy_gbit_features); | |
170 | ||
171 | /* 10/100 half/full + 1000 half/full + fibre*/ | |
172 | linkmode_set_bit_array(phy_basic_ports_array, | |
173 | ARRAY_SIZE(phy_basic_ports_array), | |
174 | phy_gbit_fibre_features); | |
175 | linkmode_set_bit_array(phy_10_100_features_array, | |
176 | ARRAY_SIZE(phy_10_100_features_array), | |
177 | phy_gbit_fibre_features); | |
178 | linkmode_set_bit_array(phy_gbit_features_array, | |
179 | ARRAY_SIZE(phy_gbit_features_array), | |
180 | phy_gbit_fibre_features); | |
12d5151b | 181 | linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, phy_gbit_fibre_features); |
719655a1 AL |
182 | |
183 | /* 10/100 half/full + 1000 half/full + 10G full*/ | |
184 | linkmode_set_bit_array(phy_all_ports_features_array, | |
185 | ARRAY_SIZE(phy_all_ports_features_array), | |
186 | phy_10gbit_features); | |
187 | linkmode_set_bit_array(phy_10_100_features_array, | |
188 | ARRAY_SIZE(phy_10_100_features_array), | |
189 | phy_10gbit_features); | |
190 | linkmode_set_bit_array(phy_gbit_features_array, | |
191 | ARRAY_SIZE(phy_gbit_features_array), | |
192 | phy_10gbit_features); | |
d3a0e217 HK |
193 | linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, |
194 | phy_10gbit_features); | |
719655a1 | 195 | |
14e47d1f OR |
196 | linkmode_set_bit_array(phy_eee_cap1_features_array, |
197 | ARRAY_SIZE(phy_eee_cap1_features_array), | |
198 | phy_eee_cap1_features); | |
ef6ee3a3 HK |
199 | linkmode_set_bit_array(phy_eee_cap2_features_array, |
200 | ARRAY_SIZE(phy_eee_cap2_features_array), | |
201 | phy_eee_cap2_features); | |
14e47d1f | 202 | |
719655a1 AL |
203 | } |
204 | ||
6f4a7f41 AV |
205 | void phy_device_free(struct phy_device *phydev) |
206 | { | |
e5a03bfd | 207 | put_device(&phydev->mdio.dev); |
6f4a7f41 | 208 | } |
4dea547f | 209 | EXPORT_SYMBOL(phy_device_free); |
6f4a7f41 | 210 | |
711fdba3 AL |
211 | static void phy_mdio_device_free(struct mdio_device *mdiodev) |
212 | { | |
213 | struct phy_device *phydev; | |
214 | ||
215 | phydev = container_of(mdiodev, struct phy_device, mdio); | |
216 | phy_device_free(phydev); | |
217 | } | |
218 | ||
6f4a7f41 AV |
219 | static void phy_device_release(struct device *dev) |
220 | { | |
cdde1560 | 221 | fwnode_handle_put(dev->fwnode); |
b2a43191 | 222 | kfree(to_phy_device(dev)); |
6f4a7f41 AV |
223 | } |
224 | ||
711fdba3 AL |
225 | static void phy_mdio_device_remove(struct mdio_device *mdiodev) |
226 | { | |
227 | struct phy_device *phydev; | |
228 | ||
229 | phydev = container_of(mdiodev, struct phy_device, mdio); | |
230 | phy_device_remove(phydev); | |
231 | } | |
232 | ||
921690f2 | 233 | static struct phy_driver genphy_driver; |
4dea547f | 234 | |
f62220d3 AF |
235 | static LIST_HEAD(phy_fixup_list); |
236 | static DEFINE_MUTEX(phy_fixup_lock); | |
237 | ||
4f534b7f YW |
238 | static bool phy_drv_wol_enabled(struct phy_device *phydev) |
239 | { | |
240 | struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; | |
241 | ||
242 | phy_ethtool_get_wol(phydev, &wol); | |
243 | ||
244 | return wol.wolopts != 0; | |
245 | } | |
246 | ||
f40a673d VO |
247 | static void phy_link_change(struct phy_device *phydev, bool up) |
248 | { | |
249 | struct net_device *netdev = phydev->attached_dev; | |
250 | ||
251 | if (up) | |
252 | netif_carrier_on(netdev); | |
253 | else | |
254 | netif_carrier_off(netdev); | |
255 | phydev->adjust_link(netdev); | |
256 | if (phydev->mii_ts && phydev->mii_ts->link_state) | |
257 | phydev->mii_ts->link_state(phydev->mii_ts, phydev); | |
258 | } | |
259 | ||
fc75ea20 VO |
260 | /** |
261 | * phy_uses_state_machine - test whether consumer driver uses PAL state machine | |
262 | * @phydev: the target PHY device structure | |
263 | * | |
264 | * Ultimately, this aims to indirectly determine whether the PHY is attached | |
265 | * to a consumer which uses the state machine by calling phy_start() and | |
266 | * phy_stop(). | |
267 | * | |
268 | * When the PHY driver consumer uses phylib, it must have previously called | |
269 | * phy_connect_direct() or one of its derivatives, so that phy_prepare_link() | |
270 | * has set up a hook for monitoring state changes. | |
271 | * | |
272 | * When the PHY driver is used by the MAC driver consumer through phylink (the | |
273 | * only other provider of a phy_link_change() method), using the PHY state | |
274 | * machine is not optional. | |
275 | * | |
276 | * Return: true if consumer calls phy_start() and phy_stop(), false otherwise. | |
277 | */ | |
278 | static bool phy_uses_state_machine(struct phy_device *phydev) | |
279 | { | |
280 | if (phydev->phy_link_change == phy_link_change) | |
281 | return phydev->attached_dev && phydev->adjust_link; | |
282 | ||
283 | /* phydev->phy_link_change is implicitly phylink_phy_change() */ | |
284 | return true; | |
285 | } | |
286 | ||
bc87922f AL |
287 | static bool mdio_bus_phy_may_suspend(struct phy_device *phydev) |
288 | { | |
301de697 VO |
289 | struct device_driver *drv = phydev->mdio.dev.driver; |
290 | struct phy_driver *phydrv = to_phy_driver(drv); | |
bc87922f AL |
291 | struct net_device *netdev = phydev->attached_dev; |
292 | ||
301de697 | 293 | if (!drv || !phydrv->suspend) |
bc87922f AL |
294 | return false; |
295 | ||
4f534b7f YW |
296 | /* If the PHY on the mido bus is not attached but has WOL enabled |
297 | * we cannot suspend the PHY. | |
298 | */ | |
299 | if (!netdev && phy_drv_wol_enabled(phydev)) | |
300 | return false; | |
301 | ||
bc87922f AL |
302 | /* PHY not attached? May suspend if the PHY has not already been |
303 | * suspended as part of a prior call to phy_disconnect() -> | |
304 | * phy_detach() -> phy_suspend() because the parent netdev might be the | |
305 | * MDIO bus driver and clock gated at this point. | |
306 | */ | |
307 | if (!netdev) | |
503ba7c6 | 308 | goto out; |
bc87922f | 309 | |
3ebbd9f6 | 310 | if (netdev->ethtool->wol_enabled) |
93f41e67 HK |
311 | return false; |
312 | ||
313 | /* As long as not all affected network drivers support the | |
314 | * wol_enabled flag, let's check for hints that WoL is enabled. | |
315 | * Don't suspend PHY if the attached netdev parent may wake up. | |
bc87922f AL |
316 | * The parent may point to a PCI device, as in tg3 driver. |
317 | */ | |
318 | if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent)) | |
319 | return false; | |
320 | ||
321 | /* Also don't suspend PHY if the netdev itself may wakeup. This | |
322 | * is the case for devices w/o underlaying pwr. mgmt. aware bus, | |
323 | * e.g. SoC devices. | |
324 | */ | |
325 | if (device_may_wakeup(&netdev->dev)) | |
326 | return false; | |
327 | ||
503ba7c6 FF |
328 | out: |
329 | return !phydev->suspended; | |
bc87922f AL |
330 | } |
331 | ||
7f654157 | 332 | static __maybe_unused int mdio_bus_phy_suspend(struct device *dev) |
bc87922f AL |
333 | { |
334 | struct phy_device *phydev = to_phy_device(dev); | |
335 | ||
fba863b8 HK |
336 | if (phydev->mac_managed_pm) |
337 | return 0; | |
338 | ||
1758bde2 LW |
339 | /* Wakeup interrupts may occur during the system sleep transition when |
340 | * the PHY is inaccessible. Set flag to postpone handling until the PHY | |
341 | * has resumed. Wait for concurrent interrupt handler to complete. | |
342 | */ | |
343 | if (phy_interrupt_is_valid(phydev)) { | |
344 | phydev->irq_suspended = 1; | |
345 | synchronize_irq(phydev->irq); | |
346 | } | |
347 | ||
bc87922f AL |
348 | /* We must stop the state machine manually, otherwise it stops out of |
349 | * control, possibly with the phydev->lock held. Upon resume, netdev | |
350 | * may call phy routines that try to grab the same lock, and that may | |
351 | * lead to a deadlock. | |
352 | */ | |
fc75ea20 | 353 | if (phy_uses_state_machine(phydev)) |
bc87922f AL |
354 | phy_stop_machine(phydev); |
355 | ||
356 | if (!mdio_bus_phy_may_suspend(phydev)) | |
357 | return 0; | |
358 | ||
611d779a HK |
359 | phydev->suspended_by_mdio_bus = 1; |
360 | ||
bc87922f AL |
361 | return phy_suspend(phydev); |
362 | } | |
363 | ||
7f654157 | 364 | static __maybe_unused int mdio_bus_phy_resume(struct device *dev) |
bc87922f AL |
365 | { |
366 | struct phy_device *phydev = to_phy_device(dev); | |
367 | int ret; | |
368 | ||
fba863b8 HK |
369 | if (phydev->mac_managed_pm) |
370 | return 0; | |
371 | ||
611d779a | 372 | if (!phydev->suspended_by_mdio_bus) |
bc87922f AL |
373 | goto no_resume; |
374 | ||
611d779a HK |
375 | phydev->suspended_by_mdio_bus = 0; |
376 | ||
ea64cdfa LW |
377 | /* If we managed to get here with the PHY state machine in a state |
378 | * neither PHY_HALTED, PHY_READY nor PHY_UP, this is an indication | |
379 | * that something went wrong and we should most likely be using | |
380 | * MAC managed PM, but we are not. | |
744d23c7 | 381 | */ |
ea64cdfa LW |
382 | WARN_ON(phydev->state != PHY_HALTED && phydev->state != PHY_READY && |
383 | phydev->state != PHY_UP); | |
744d23c7 | 384 | |
4c0d2e96 | 385 | ret = phy_init_hw(phydev); |
bc87922f AL |
386 | if (ret < 0) |
387 | return ret; | |
388 | ||
4c0d2e96 | 389 | ret = phy_resume(phydev); |
bc87922f AL |
390 | if (ret < 0) |
391 | return ret; | |
4c0d2e96 | 392 | no_resume: |
1758bde2 LW |
393 | if (phy_interrupt_is_valid(phydev)) { |
394 | phydev->irq_suspended = 0; | |
395 | synchronize_irq(phydev->irq); | |
396 | ||
397 | /* Rerun interrupts which were postponed by phy_interrupt() | |
398 | * because they occurred during the system sleep transition. | |
399 | */ | |
400 | if (phydev->irq_rerun) { | |
401 | phydev->irq_rerun = 0; | |
402 | enable_irq(phydev->irq); | |
403 | irq_wake_thread(phydev->irq, phydev); | |
404 | } | |
405 | } | |
406 | ||
fc75ea20 | 407 | if (phy_uses_state_machine(phydev)) |
8742beb5 | 408 | phy_start_machine(phydev); |
bc87922f AL |
409 | |
410 | return 0; | |
411 | } | |
412 | ||
4c0d2e96 HK |
413 | static SIMPLE_DEV_PM_OPS(mdio_bus_phy_pm_ops, mdio_bus_phy_suspend, |
414 | mdio_bus_phy_resume); | |
bc87922f | 415 | |
2f53e904 SS |
416 | /** |
417 | * phy_register_fixup - creates a new phy_fixup and adds it to the list | |
e5a03bfd | 418 | * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID) |
f62220d3 | 419 | * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY) |
2f53e904 | 420 | * It can also be PHY_ANY_UID |
f62220d3 | 421 | * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before |
2f53e904 | 422 | * comparison |
f62220d3 AF |
423 | * @run: The actual code to be run when a matching PHY is found |
424 | */ | |
ea47e70e HK |
425 | static int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask, |
426 | int (*run)(struct phy_device *)) | |
f62220d3 | 427 | { |
553fe92b | 428 | struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL); |
f62220d3 | 429 | |
f62220d3 AF |
430 | if (!fixup) |
431 | return -ENOMEM; | |
432 | ||
fb3ceec1 | 433 | strscpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id)); |
f62220d3 AF |
434 | fixup->phy_uid = phy_uid; |
435 | fixup->phy_uid_mask = phy_uid_mask; | |
436 | fixup->run = run; | |
437 | ||
438 | mutex_lock(&phy_fixup_lock); | |
439 | list_add_tail(&fixup->list, &phy_fixup_list); | |
440 | mutex_unlock(&phy_fixup_lock); | |
441 | ||
442 | return 0; | |
443 | } | |
f62220d3 AF |
444 | |
445 | /* Registers a fixup to be run on any PHY with the UID in phy_uid */ | |
446 | int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask, | |
2f53e904 | 447 | int (*run)(struct phy_device *)) |
f62220d3 AF |
448 | { |
449 | return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run); | |
450 | } | |
451 | EXPORT_SYMBOL(phy_register_fixup_for_uid); | |
452 | ||
453 | /* Registers a fixup to be run on the PHY with id string bus_id */ | |
454 | int phy_register_fixup_for_id(const char *bus_id, | |
2f53e904 | 455 | int (*run)(struct phy_device *)) |
f62220d3 AF |
456 | { |
457 | return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run); | |
458 | } | |
459 | EXPORT_SYMBOL(phy_register_fixup_for_id); | |
460 | ||
f38e7a32 WH |
461 | /** |
462 | * phy_unregister_fixup - remove a phy_fixup from the list | |
463 | * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list | |
464 | * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list | |
465 | * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison | |
466 | */ | |
467 | int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask) | |
468 | { | |
469 | struct list_head *pos, *n; | |
470 | struct phy_fixup *fixup; | |
471 | int ret; | |
472 | ||
473 | ret = -ENODEV; | |
474 | ||
475 | mutex_lock(&phy_fixup_lock); | |
476 | list_for_each_safe(pos, n, &phy_fixup_list) { | |
477 | fixup = list_entry(pos, struct phy_fixup, list); | |
478 | ||
479 | if ((!strcmp(fixup->bus_id, bus_id)) && | |
4b159f50 | 480 | phy_id_compare(fixup->phy_uid, phy_uid, phy_uid_mask)) { |
f38e7a32 WH |
481 | list_del(&fixup->list); |
482 | kfree(fixup); | |
483 | ret = 0; | |
484 | break; | |
485 | } | |
486 | } | |
487 | mutex_unlock(&phy_fixup_lock); | |
488 | ||
489 | return ret; | |
490 | } | |
491 | EXPORT_SYMBOL(phy_unregister_fixup); | |
492 | ||
493 | /* Unregisters a fixup of any PHY with the UID in phy_uid */ | |
494 | int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask) | |
495 | { | |
496 | return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask); | |
497 | } | |
498 | EXPORT_SYMBOL(phy_unregister_fixup_for_uid); | |
499 | ||
500 | /* Unregisters a fixup of the PHY with id string bus_id */ | |
501 | int phy_unregister_fixup_for_id(const char *bus_id) | |
502 | { | |
503 | return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff); | |
504 | } | |
505 | EXPORT_SYMBOL(phy_unregister_fixup_for_id); | |
506 | ||
2f53e904 | 507 | /* Returns 1 if fixup matches phydev in bus_id and phy_uid. |
f62220d3 AF |
508 | * Fixups can be set to match any in one or more fields. |
509 | */ | |
510 | static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup) | |
511 | { | |
84eff6d1 | 512 | if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0) |
f62220d3 AF |
513 | if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0) |
514 | return 0; | |
515 | ||
4b159f50 RK |
516 | if (!phy_id_compare(phydev->phy_id, fixup->phy_uid, |
517 | fixup->phy_uid_mask)) | |
f62220d3 AF |
518 | if (fixup->phy_uid != PHY_ANY_UID) |
519 | return 0; | |
520 | ||
521 | return 1; | |
522 | } | |
523 | ||
524 | /* Runs any matching fixups for this phydev */ | |
fbfcec63 | 525 | static int phy_scan_fixups(struct phy_device *phydev) |
f62220d3 AF |
526 | { |
527 | struct phy_fixup *fixup; | |
528 | ||
529 | mutex_lock(&phy_fixup_lock); | |
530 | list_for_each_entry(fixup, &phy_fixup_list, list) { | |
531 | if (phy_needs_fixup(phydev, fixup)) { | |
553fe92b | 532 | int err = fixup->run(phydev); |
f62220d3 | 533 | |
bc23283c JS |
534 | if (err < 0) { |
535 | mutex_unlock(&phy_fixup_lock); | |
f62220d3 | 536 | return err; |
bc23283c | 537 | } |
b0ae009f | 538 | phydev->has_fixups = true; |
f62220d3 AF |
539 | } |
540 | } | |
541 | mutex_unlock(&phy_fixup_lock); | |
542 | ||
543 | return 0; | |
544 | } | |
f62220d3 | 545 | |
d6c45707 CM |
546 | /** |
547 | * genphy_match_phy_device - match a PHY device with a PHY driver | |
548 | * @phydev: target phy_device struct | |
549 | * @phydrv: target phy_driver struct | |
550 | * | |
551 | * Description: Checks whether the given PHY device matches the specified | |
552 | * PHY driver. For Clause 45 PHYs, iterates over the available device | |
553 | * identifiers and compares them against the driver's expected PHY ID, | |
554 | * applying the provided mask. For Clause 22 PHYs, a direct ID comparison | |
555 | * is performed. | |
556 | * | |
557 | * Return: 1 if the PHY device matches the driver, 0 otherwise. | |
558 | */ | |
559 | int genphy_match_phy_device(struct phy_device *phydev, | |
560 | const struct phy_driver *phydrv) | |
e76a4957 | 561 | { |
e76a4957 | 562 | if (phydev->is_c45) { |
d6c45707 CM |
563 | const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids); |
564 | int i; | |
565 | ||
e76a4957 | 566 | for (i = 1; i < num_ids; i++) { |
b95e86d8 | 567 | if (phydev->c45_ids.device_ids[i] == 0xffffffff) |
e76a4957 AL |
568 | continue; |
569 | ||
4b159f50 RK |
570 | if (phy_id_compare(phydev->c45_ids.device_ids[i], |
571 | phydrv->phy_id, phydrv->phy_id_mask)) | |
e76a4957 AL |
572 | return 1; |
573 | } | |
d6c45707 | 574 | |
e76a4957 | 575 | return 0; |
e76a4957 | 576 | } |
d6c45707 CM |
577 | |
578 | return phy_id_compare(phydev->phy_id, phydrv->phy_id, | |
579 | phydrv->phy_id_mask); | |
580 | } | |
581 | EXPORT_SYMBOL_GPL(genphy_match_phy_device); | |
582 | ||
583 | static int phy_bus_match(struct device *dev, const struct device_driver *drv) | |
584 | { | |
585 | struct phy_device *phydev = to_phy_device(dev); | |
586 | const struct phy_driver *phydrv = to_phy_driver(drv); | |
587 | ||
588 | if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY)) | |
589 | return 0; | |
590 | ||
591 | if (phydrv->match_phy_device) | |
592 | return phydrv->match_phy_device(phydev, phydrv); | |
593 | ||
594 | return genphy_match_phy_device(phydev, phydrv); | |
e76a4957 AL |
595 | } |
596 | ||
7f4828ff HK |
597 | static ssize_t |
598 | phy_id_show(struct device *dev, struct device_attribute *attr, char *buf) | |
599 | { | |
600 | struct phy_device *phydev = to_phy_device(dev); | |
601 | ||
b5155ddd | 602 | return sysfs_emit(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id); |
7f4828ff HK |
603 | } |
604 | static DEVICE_ATTR_RO(phy_id); | |
605 | ||
606 | static ssize_t | |
607 | phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf) | |
608 | { | |
609 | struct phy_device *phydev = to_phy_device(dev); | |
610 | const char *mode = NULL; | |
611 | ||
6b2edfba | 612 | if (phydev->is_internal) |
7f4828ff HK |
613 | mode = "internal"; |
614 | else | |
615 | mode = phy_modes(phydev->interface); | |
616 | ||
b5155ddd | 617 | return sysfs_emit(buf, "%s\n", mode); |
7f4828ff HK |
618 | } |
619 | static DEVICE_ATTR_RO(phy_interface); | |
620 | ||
621 | static ssize_t | |
622 | phy_has_fixups_show(struct device *dev, struct device_attribute *attr, | |
623 | char *buf) | |
624 | { | |
625 | struct phy_device *phydev = to_phy_device(dev); | |
626 | ||
b5155ddd | 627 | return sysfs_emit(buf, "%d\n", phydev->has_fixups); |
7f4828ff HK |
628 | } |
629 | static DEVICE_ATTR_RO(phy_has_fixups); | |
630 | ||
b0bade51 FF |
631 | static ssize_t phy_dev_flags_show(struct device *dev, |
632 | struct device_attribute *attr, | |
633 | char *buf) | |
634 | { | |
635 | struct phy_device *phydev = to_phy_device(dev); | |
636 | ||
b5155ddd | 637 | return sysfs_emit(buf, "0x%08x\n", phydev->dev_flags); |
b0bade51 FF |
638 | } |
639 | static DEVICE_ATTR_RO(phy_dev_flags); | |
640 | ||
7f4828ff HK |
641 | static struct attribute *phy_dev_attrs[] = { |
642 | &dev_attr_phy_id.attr, | |
643 | &dev_attr_phy_interface.attr, | |
644 | &dev_attr_phy_has_fixups.attr, | |
b0bade51 | 645 | &dev_attr_phy_dev_flags.attr, |
7f4828ff HK |
646 | NULL, |
647 | }; | |
648 | ATTRIBUTE_GROUPS(phy_dev); | |
649 | ||
650 | static const struct device_type mdio_bus_phy_type = { | |
651 | .name = "PHY", | |
652 | .groups = phy_dev_groups, | |
653 | .release = phy_device_release, | |
4c0d2e96 | 654 | .pm = pm_ptr(&mdio_bus_phy_pm_ops), |
7f4828ff HK |
655 | }; |
656 | ||
7d49a32a | 657 | static int phy_request_driver_module(struct phy_device *dev, u32 phy_id) |
13d0ab67 HK |
658 | { |
659 | int ret; | |
660 | ||
661 | ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, | |
662 | MDIO_ID_ARGS(phy_id)); | |
21e19442 HK |
663 | /* We only check for failures in executing the usermode binary, |
664 | * not whether a PHY driver module exists for the PHY ID. | |
665 | * Accept -ENOENT because this may occur in case no initramfs exists, | |
666 | * then modprobe isn't available. | |
13d0ab67 | 667 | */ |
21e19442 | 668 | if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) { |
7d49a32a RK |
669 | phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n", |
670 | ret, (unsigned long)phy_id); | |
13d0ab67 HK |
671 | return ret; |
672 | } | |
673 | ||
674 | return 0; | |
675 | } | |
676 | ||
7d49a32a | 677 | struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id, |
2f53e904 SS |
678 | bool is_c45, |
679 | struct phy_c45_device_ids *c45_ids) | |
11b0bacd VB |
680 | { |
681 | struct phy_device *dev; | |
e5a03bfd | 682 | struct mdio_device *mdiodev; |
13d0ab67 | 683 | int ret = 0; |
8626d3b4 | 684 | |
2f53e904 | 685 | /* We allocate the device, and initialize the default values */ |
cd861280 | 686 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
ef899c07 | 687 | if (!dev) |
d3765f08 | 688 | return ERR_PTR(-ENOMEM); |
11b0bacd | 689 | |
e5a03bfd | 690 | mdiodev = &dev->mdio; |
e5a03bfd AL |
691 | mdiodev->dev.parent = &bus->dev; |
692 | mdiodev->dev.bus = &mdio_bus_type; | |
7f4828ff | 693 | mdiodev->dev.type = &mdio_bus_phy_type; |
e5a03bfd | 694 | mdiodev->bus = bus; |
e76a4957 | 695 | mdiodev->bus_match = phy_bus_match; |
e5a03bfd | 696 | mdiodev->addr = addr; |
7f854420 | 697 | mdiodev->flags = MDIO_DEVICE_FLAG_PHY; |
711fdba3 AL |
698 | mdiodev->device_free = phy_mdio_device_free; |
699 | mdiodev->device_remove = phy_mdio_device_remove; | |
df16c1c5 | 700 | mdiodev->reset_state = -1; |
6f4a7f41 | 701 | |
a5d66f81 RK |
702 | dev->speed = SPEED_UNKNOWN; |
703 | dev->duplex = DUPLEX_UNKNOWN; | |
2f53e904 SS |
704 | dev->pause = 0; |
705 | dev->asym_pause = 0; | |
c69851e9 | 706 | dev->link = 0; |
4217a64e | 707 | dev->port = PORT_TP; |
e8a2b6a4 | 708 | dev->interface = PHY_INTERFACE_MODE_GMII; |
11b0bacd VB |
709 | |
710 | dev->autoneg = AUTONEG_ENABLE; | |
711 | ||
3da8ffd8 | 712 | dev->pma_extable = -ENODATA; |
ac28b9f8 | 713 | dev->is_c45 = is_c45; |
11b0bacd | 714 | dev->phy_id = phy_id; |
ac28b9f8 DD |
715 | if (c45_ids) |
716 | dev->c45_ids = *c45_ids; | |
47b356e4 | 717 | dev->irq = bus->irq[addr]; |
d02cbc46 | 718 | |
e5a03bfd | 719 | dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr); |
d02cbc46 | 720 | device_initialize(&mdiodev->dev); |
11b0bacd VB |
721 | |
722 | dev->state = PHY_DOWN; | |
01e5b728 | 723 | INIT_LIST_HEAD(&dev->leds); |
11b0bacd | 724 | |
35b5f6b1 | 725 | mutex_init(&dev->lock); |
4f9c85a1 | 726 | INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine); |
11b0bacd | 727 | |
8626d3b4 | 728 | /* Request the appropriate module unconditionally; don't |
2f53e904 SS |
729 | * bother trying to do so only if it isn't already loaded, |
730 | * because that gets complicated. A hotplug event would have | |
731 | * done an unconditional modprobe anyway. | |
732 | * We don't do normal hotplug because it won't work for MDIO | |
733 | * -- because it relies on the device staying around for long | |
734 | * enough for the driver to get loaded. With MDIO, the NIC | |
735 | * driver will get bored and give up as soon as it finds that | |
736 | * there's no driver _already_ loaded. | |
737 | */ | |
30fcd6a9 JA |
738 | if (is_c45 && c45_ids) { |
739 | const int num_ids = ARRAY_SIZE(c45_ids->device_ids); | |
740 | int i; | |
741 | ||
742 | for (i = 1; i < num_ids; i++) { | |
b95e86d8 | 743 | if (c45_ids->device_ids[i] == 0xffffffff) |
30fcd6a9 JA |
744 | continue; |
745 | ||
13d0ab67 HK |
746 | ret = phy_request_driver_module(dev, |
747 | c45_ids->device_ids[i]); | |
748 | if (ret) | |
749 | break; | |
30fcd6a9 JA |
750 | } |
751 | } else { | |
13d0ab67 | 752 | ret = phy_request_driver_module(dev, phy_id); |
30fcd6a9 | 753 | } |
8626d3b4 | 754 | |
d02cbc46 JH |
755 | if (ret) { |
756 | put_device(&mdiodev->dev); | |
13d0ab67 HK |
757 | dev = ERR_PTR(ret); |
758 | } | |
b2a43191 | 759 | |
11b0bacd VB |
760 | return dev; |
761 | } | |
ac28b9f8 DD |
762 | EXPORT_SYMBOL(phy_device_create); |
763 | ||
c746053d RK |
764 | /* phy_c45_probe_present - checks to see if a MMD is present in the package |
765 | * @bus: the target MII bus | |
766 | * @prtad: PHY package address on the MII bus | |
767 | * @devad: PHY device (MMD) address | |
768 | * | |
769 | * Read the MDIO_STAT2 register, and check whether a device is responding | |
770 | * at this address. | |
771 | * | |
772 | * Returns: negative error number on bus access error, zero if no device | |
773 | * is responding, or positive if a device is present. | |
774 | */ | |
775 | static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad) | |
776 | { | |
777 | int stat2; | |
778 | ||
779 | stat2 = mdiobus_c45_read(bus, prtad, devad, MDIO_STAT2); | |
780 | if (stat2 < 0) | |
781 | return stat2; | |
782 | ||
783 | return (stat2 & MDIO_STAT2_DEVPRST) == MDIO_STAT2_DEVPRST_VAL; | |
784 | } | |
785 | ||
5f6c99e0 SX |
786 | /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers. |
787 | * @bus: the target MII bus | |
788 | * @addr: PHY address on the MII bus | |
789 | * @dev_addr: MMD address in the PHY. | |
790 | * @devices_in_package: where to store the devices in package information. | |
791 | * | |
792 | * Description: reads devices in package registers of a MMD at @dev_addr | |
793 | * from PHY at @addr on @bus. | |
794 | * | |
795 | * Returns: 0 on success, -EIO on failure. | |
796 | */ | |
797 | static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr, | |
798 | u32 *devices_in_package) | |
799 | { | |
90ce665c | 800 | int phy_reg; |
5f6c99e0 | 801 | |
90ce665c | 802 | phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS2); |
5f6c99e0 SX |
803 | if (phy_reg < 0) |
804 | return -EIO; | |
50684da7 | 805 | *devices_in_package = phy_reg << 16; |
5f6c99e0 | 806 | |
90ce665c | 807 | phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS1); |
5f6c99e0 SX |
808 | if (phy_reg < 0) |
809 | return -EIO; | |
50684da7 | 810 | *devices_in_package |= phy_reg; |
5f6c99e0 SX |
811 | |
812 | return 0; | |
813 | } | |
814 | ||
ac28b9f8 DD |
815 | /** |
816 | * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs. | |
817 | * @bus: the target MII bus | |
818 | * @addr: PHY address on the MII bus | |
ac28b9f8 DD |
819 | * @c45_ids: where to store the c45 ID information. |
820 | * | |
48c54388 RK |
821 | * Read the PHY "devices in package". If this appears to be valid, read |
822 | * the PHY identifiers for each device. Return the "devices in package" | |
823 | * and identifiers in @c45_ids. | |
ac28b9f8 | 824 | * |
48c54388 | 825 | * Returns zero on success, %-EIO on bus access error, or %-ENODEV if |
17b44753 | 826 | * the "devices in package" is invalid or no device responds. |
ac28b9f8 | 827 | */ |
48c54388 | 828 | static int get_phy_c45_ids(struct mii_bus *bus, int addr, |
90ce665c RK |
829 | struct phy_c45_device_ids *c45_ids) |
830 | { | |
ac28b9f8 | 831 | const int num_ids = ARRAY_SIZE(c45_ids->device_ids); |
5ba33cf4 | 832 | u32 devs_in_pkg = 0; |
c746053d | 833 | int i, ret, phy_reg; |
ac28b9f8 | 834 | |
5f6c99e0 SX |
835 | /* Find first non-zero Devices In package. Device zero is reserved |
836 | * for 802.3 c45 complied PHYs, so don't probe it at first. | |
ac28b9f8 | 837 | */ |
fb16d465 VO |
838 | for (i = 1; i < MDIO_MMD_NUM && (devs_in_pkg == 0 || |
839 | (devs_in_pkg & 0x1fffffff) == 0x1fffffff); i++) { | |
c746053d RK |
840 | if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) { |
841 | /* Check that there is a device present at this | |
842 | * address before reading the devices-in-package | |
843 | * register to avoid reading garbage from the PHY. | |
844 | * Some PHYs (88x3310) vendor space is not IEEE802.3 | |
845 | * compliant. | |
846 | */ | |
847 | ret = phy_c45_probe_present(bus, addr, i); | |
848 | if (ret < 0) | |
17b44753 AL |
849 | /* returning -ENODEV doesn't stop bus |
850 | * scanning | |
851 | */ | |
852 | return (phy_reg == -EIO || | |
853 | phy_reg == -ENODEV) ? -ENODEV : -EIO; | |
c746053d RK |
854 | |
855 | if (!ret) | |
856 | continue; | |
857 | } | |
5ba33cf4 | 858 | phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, &devs_in_pkg); |
ac28b9f8 DD |
859 | if (phy_reg < 0) |
860 | return -EIO; | |
454a78d1 RK |
861 | } |
862 | ||
5ba33cf4 | 863 | if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff) { |
454a78d1 RK |
864 | /* If mostly Fs, there is no device there, then let's probe |
865 | * MMD 0, as some 10G PHYs have zero Devices In package, | |
866 | * e.g. Cortina CS4315/CS4340 PHY. | |
867 | */ | |
5ba33cf4 | 868 | phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, &devs_in_pkg); |
454a78d1 RK |
869 | if (phy_reg < 0) |
870 | return -EIO; | |
ac28b9f8 | 871 | |
454a78d1 | 872 | /* no device there, let's get out of here */ |
5ba33cf4 | 873 | if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff) |
48c54388 | 874 | return -ENODEV; |
ac28b9f8 DD |
875 | } |
876 | ||
877 | /* Now probe Device Identifiers for each device present. */ | |
878 | for (i = 1; i < num_ids; i++) { | |
5ba33cf4 | 879 | if (!(devs_in_pkg & (1 << i))) |
ac28b9f8 DD |
880 | continue; |
881 | ||
389a3389 RK |
882 | if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) { |
883 | /* Probe the "Device Present" bits for the vendor MMDs | |
884 | * to ignore these if they do not contain IEEE 802.3 | |
885 | * registers. | |
886 | */ | |
887 | ret = phy_c45_probe_present(bus, addr, i); | |
888 | if (ret < 0) | |
889 | return ret; | |
890 | ||
891 | if (!ret) | |
892 | continue; | |
893 | } | |
894 | ||
90ce665c | 895 | phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID1); |
ac28b9f8 DD |
896 | if (phy_reg < 0) |
897 | return -EIO; | |
50684da7 | 898 | c45_ids->device_ids[i] = phy_reg << 16; |
ac28b9f8 | 899 | |
90ce665c | 900 | phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID2); |
ac28b9f8 DD |
901 | if (phy_reg < 0) |
902 | return -EIO; | |
50684da7 | 903 | c45_ids->device_ids[i] |= phy_reg; |
ac28b9f8 | 904 | } |
48c54388 | 905 | |
5ba33cf4 | 906 | c45_ids->devices_in_package = devs_in_pkg; |
320ed3bf RK |
907 | /* Bit 0 doesn't represent a device, it indicates c22 regs presence */ |
908 | c45_ids->mmds_present = devs_in_pkg & ~BIT(0); | |
5ba33cf4 | 909 | |
ac28b9f8 DD |
910 | return 0; |
911 | } | |
11b0bacd | 912 | |
b3df0da8 | 913 | /** |
e6306261 | 914 | * get_phy_c22_id - reads the specified addr for its clause 22 ID. |
b3df0da8 RD |
915 | * @bus: the target MII bus |
916 | * @addr: PHY address on the MII bus | |
cac1f3c8 | 917 | * @phy_id: where to store the ID retrieved. |
00db8189 | 918 | * |
ee951005 RK |
919 | * Read the 802.3 clause 22 PHY ID from the PHY at @addr on the @bus, |
920 | * placing it in @phy_id. Return zero on successful read and the ID is | |
921 | * valid, %-EIO on bus access error, or %-ENODEV if no device responds | |
922 | * or invalid ID. | |
00db8189 | 923 | */ |
e6306261 | 924 | static int get_phy_c22_id(struct mii_bus *bus, int addr, u32 *phy_id) |
00db8189 AF |
925 | { |
926 | int phy_reg; | |
00db8189 | 927 | |
2f53e904 | 928 | /* Grab the bits from PHYIR1, and put them in the upper half */ |
6fe32649 | 929 | phy_reg = mdiobus_read(bus, addr, MII_PHYSID1); |
02a6efca | 930 | if (phy_reg < 0) { |
d8cce3a1 HK |
931 | /* returning -ENODEV doesn't stop bus scanning */ |
932 | return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO; | |
02a6efca | 933 | } |
00db8189 | 934 | |
50684da7 | 935 | *phy_id = phy_reg << 16; |
00db8189 AF |
936 | |
937 | /* Grab the bits from PHYIR2, and put them in the lower half */ | |
6fe32649 | 938 | phy_reg = mdiobus_read(bus, addr, MII_PHYSID2); |
b2ffc75e FF |
939 | if (phy_reg < 0) { |
940 | /* returning -ENODEV doesn't stop bus scanning */ | |
941 | return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO; | |
942 | } | |
cac1f3c8 | 943 | |
50684da7 | 944 | *phy_id |= phy_reg; |
cac1f3c8 | 945 | |
ee951005 RK |
946 | /* If the phy_id is mostly Fs, there is no device there */ |
947 | if ((*phy_id & 0x1fffffff) == 0x1fffffff) | |
948 | return -ENODEV; | |
949 | ||
cac1f3c8 PG |
950 | return 0; |
951 | } | |
952 | ||
114dea60 CJ |
953 | /* Extract the phy ID from the compatible string of the form |
954 | * ethernet-phy-idAAAA.BBBB. | |
955 | */ | |
956 | int fwnode_get_phy_id(struct fwnode_handle *fwnode, u32 *phy_id) | |
957 | { | |
958 | unsigned int upper, lower; | |
959 | const char *cp; | |
960 | int ret; | |
961 | ||
962 | ret = fwnode_property_read_string(fwnode, "compatible", &cp); | |
963 | if (ret) | |
964 | return ret; | |
965 | ||
966 | if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) != 2) | |
967 | return -EINVAL; | |
968 | ||
969 | *phy_id = ((upper & GENMASK(15, 0)) << 16) | (lower & GENMASK(15, 0)); | |
970 | return 0; | |
971 | } | |
972 | EXPORT_SYMBOL(fwnode_get_phy_id); | |
973 | ||
cac1f3c8 | 974 | /** |
2f53e904 SS |
975 | * get_phy_device - reads the specified PHY device and returns its @phy_device |
976 | * struct | |
cac1f3c8 PG |
977 | * @bus: the target MII bus |
978 | * @addr: PHY address on the MII bus | |
ac28b9f8 | 979 | * @is_c45: If true the PHY uses the 802.3 clause 45 protocol |
cac1f3c8 | 980 | * |
439625a7 RK |
981 | * Probe for a PHY at @addr on @bus. |
982 | * | |
983 | * When probing for a clause 22 PHY, then read the ID registers. If we find | |
984 | * a valid ID, allocate and return a &struct phy_device. | |
985 | * | |
986 | * When probing for a clause 45 PHY, read the "devices in package" registers. | |
987 | * If the "devices in package" appears valid, read the ID registers for each | |
988 | * MMD, allocate and return a &struct phy_device. | |
989 | * | |
990 | * Returns an allocated &struct phy_device on success, %-ENODEV if there is | |
991 | * no PHY present, or %-EIO on bus access error. | |
cac1f3c8 | 992 | */ |
ac28b9f8 | 993 | struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45) |
cac1f3c8 | 994 | { |
b95e86d8 | 995 | struct phy_c45_device_ids c45_ids; |
160c85f0 | 996 | u32 phy_id = 0; |
cac1f3c8 | 997 | int r; |
00db8189 | 998 | |
b95e86d8 | 999 | c45_ids.devices_in_package = 0; |
320ed3bf | 1000 | c45_ids.mmds_present = 0; |
b95e86d8 RK |
1001 | memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids)); |
1002 | ||
e6306261 | 1003 | if (is_c45) |
48c54388 | 1004 | r = get_phy_c45_ids(bus, addr, &c45_ids); |
e6306261 RK |
1005 | else |
1006 | r = get_phy_c22_id(bus, addr, &phy_id); | |
1007 | ||
cac1f3c8 PG |
1008 | if (r) |
1009 | return ERR_PTR(r); | |
00db8189 | 1010 | |
b040aab7 WVK |
1011 | /* PHY device such as the Marvell Alaska 88E2110 will return a PHY ID |
1012 | * of 0 when probed using get_phy_c22_id() with no error. Proceed to | |
1013 | * probe with C45 to see if we're able to get a valid PHY ID in the C45 | |
1014 | * space, if successful, create the C45 PHY device. | |
1015 | */ | |
fbfe9759 | 1016 | if (!is_c45 && phy_id == 0 && bus->read_c45) { |
b040aab7 WVK |
1017 | r = get_phy_c45_ids(bus, addr, &c45_ids); |
1018 | if (!r) | |
1019 | return phy_device_create(bus, addr, phy_id, | |
1020 | true, &c45_ids); | |
1021 | } | |
1022 | ||
e62a768f | 1023 | return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids); |
00db8189 | 1024 | } |
4dea547f GL |
1025 | EXPORT_SYMBOL(get_phy_device); |
1026 | ||
1027 | /** | |
1028 | * phy_device_register - Register the phy device on the MDIO bus | |
1d4ac5d5 | 1029 | * @phydev: phy_device structure to be added to the MDIO bus |
4dea547f GL |
1030 | */ |
1031 | int phy_device_register(struct phy_device *phydev) | |
1032 | { | |
1033 | int err; | |
1034 | ||
7f854420 AL |
1035 | err = mdiobus_register_device(&phydev->mdio); |
1036 | if (err) | |
1037 | return err; | |
4dea547f | 1038 | |
bafbdd52 SS |
1039 | /* Deassert the reset signal */ |
1040 | phy_device_reset(phydev, 0); | |
1041 | ||
4dea547f | 1042 | /* Run all of the fixups for this PHY */ |
d92f5dec | 1043 | err = phy_scan_fixups(phydev); |
87aa9f9c | 1044 | if (err) { |
c3a6a174 | 1045 | phydev_err(phydev, "failed to initialize\n"); |
87aa9f9c FF |
1046 | goto out; |
1047 | } | |
4dea547f | 1048 | |
e5a03bfd | 1049 | err = device_add(&phydev->mdio.dev); |
4dea547f | 1050 | if (err) { |
c3a6a174 | 1051 | phydev_err(phydev, "failed to add\n"); |
4dea547f GL |
1052 | goto out; |
1053 | } | |
1054 | ||
1055 | return 0; | |
1056 | ||
1057 | out: | |
bafbdd52 SS |
1058 | /* Assert the reset signal */ |
1059 | phy_device_reset(phydev, 1); | |
1060 | ||
7f854420 | 1061 | mdiobus_unregister_device(&phydev->mdio); |
4dea547f GL |
1062 | return err; |
1063 | } | |
1064 | EXPORT_SYMBOL(phy_device_register); | |
00db8189 | 1065 | |
38737e49 RK |
1066 | /** |
1067 | * phy_device_remove - Remove a previously registered phy device from the MDIO bus | |
1068 | * @phydev: phy_device structure to remove | |
1069 | * | |
1070 | * This doesn't free the phy_device itself, it merely reverses the effects | |
1071 | * of phy_device_register(). Use phy_device_free() to free the device | |
1072 | * after calling this function. | |
1073 | */ | |
1074 | void phy_device_remove(struct phy_device *phydev) | |
1075 | { | |
b9926da0 | 1076 | unregister_mii_timestamper(phydev->mii_ts); |
5e82147d | 1077 | pse_control_put(phydev->psec); |
1dca22b1 | 1078 | |
e5a03bfd | 1079 | device_del(&phydev->mdio.dev); |
bafbdd52 SS |
1080 | |
1081 | /* Assert the reset signal */ | |
1082 | phy_device_reset(phydev, 1); | |
1083 | ||
7f854420 | 1084 | mdiobus_unregister_device(&phydev->mdio); |
38737e49 RK |
1085 | } |
1086 | EXPORT_SYMBOL(phy_device_remove); | |
1087 | ||
8b72b301 XL |
1088 | /** |
1089 | * phy_get_c45_ids - Read 802.3-c45 IDs for phy device. | |
1090 | * @phydev: phy_device structure to read 802.3-c45 IDs | |
1091 | * | |
1092 | * Returns zero on success, %-EIO on bus access error, or %-ENODEV if | |
1093 | * the "devices in package" is invalid. | |
1094 | */ | |
1095 | int phy_get_c45_ids(struct phy_device *phydev) | |
1096 | { | |
1097 | return get_phy_c45_ids(phydev->mdio.bus, phydev->mdio.addr, | |
1098 | &phydev->c45_ids); | |
1099 | } | |
1100 | EXPORT_SYMBOL(phy_get_c45_ids); | |
1101 | ||
f8f76db1 JP |
1102 | /** |
1103 | * phy_find_first - finds the first PHY device on the bus | |
1104 | * @bus: the target MII bus | |
1105 | */ | |
1106 | struct phy_device *phy_find_first(struct mii_bus *bus) | |
1107 | { | |
7f854420 | 1108 | struct phy_device *phydev; |
f8f76db1 JP |
1109 | int addr; |
1110 | ||
1111 | for (addr = 0; addr < PHY_MAX_ADDR; addr++) { | |
7f854420 AL |
1112 | phydev = mdiobus_get_phy(bus, addr); |
1113 | if (phydev) | |
1114 | return phydev; | |
f8f76db1 JP |
1115 | } |
1116 | return NULL; | |
1117 | } | |
1118 | EXPORT_SYMBOL(phy_find_first); | |
1119 | ||
b3df0da8 RD |
1120 | /** |
1121 | * phy_prepare_link - prepares the PHY layer to monitor link status | |
1122 | * @phydev: target phy_device struct | |
1123 | * @handler: callback function for link status change notifications | |
00db8189 | 1124 | * |
b3df0da8 | 1125 | * Description: Tells the PHY infrastructure to handle the |
00db8189 AF |
1126 | * gory details on monitoring link status (whether through |
1127 | * polling or an interrupt), and to call back to the | |
1128 | * connected device driver when the link status changes. | |
1129 | * If you want to monitor your own link state, don't call | |
b3df0da8 RD |
1130 | * this function. |
1131 | */ | |
89ff05ec | 1132 | static void phy_prepare_link(struct phy_device *phydev, |
2f53e904 | 1133 | void (*handler)(struct net_device *)) |
00db8189 AF |
1134 | { |
1135 | phydev->adjust_link = handler; | |
1136 | } | |
1137 | ||
fa94f6d9 GL |
1138 | /** |
1139 | * phy_connect_direct - connect an ethernet device to a specific phy_device | |
1140 | * @dev: the network device to connect | |
1141 | * @phydev: the pointer to the phy device | |
1142 | * @handler: callback function for state change notifications | |
fa94f6d9 GL |
1143 | * @interface: PHY device's interface |
1144 | */ | |
1145 | int phy_connect_direct(struct net_device *dev, struct phy_device *phydev, | |
f9a8f83b | 1146 | void (*handler)(struct net_device *), |
fa94f6d9 GL |
1147 | phy_interface_t interface) |
1148 | { | |
1149 | int rc; | |
1150 | ||
82c76aca IC |
1151 | if (!dev) |
1152 | return -EINVAL; | |
1153 | ||
f9a8f83b | 1154 | rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); |
fa94f6d9 GL |
1155 | if (rc) |
1156 | return rc; | |
1157 | ||
1158 | phy_prepare_link(phydev, handler); | |
434a4315 HK |
1159 | if (phy_interrupt_is_valid(phydev)) |
1160 | phy_request_interrupt(phydev); | |
fa94f6d9 GL |
1161 | |
1162 | return 0; | |
1163 | } | |
1164 | EXPORT_SYMBOL(phy_connect_direct); | |
1165 | ||
b3df0da8 RD |
1166 | /** |
1167 | * phy_connect - connect an ethernet device to a PHY device | |
1168 | * @dev: the network device to connect | |
5d12b132 | 1169 | * @bus_id: the id string of the PHY device to connect |
b3df0da8 | 1170 | * @handler: callback function for state change notifications |
b3df0da8 | 1171 | * @interface: PHY device's interface |
e1393456 | 1172 | * |
b3df0da8 | 1173 | * Description: Convenience function for connecting ethernet |
e1393456 AF |
1174 | * devices to PHY devices. The default behavior is for |
1175 | * the PHY infrastructure to handle everything, and only notify | |
1176 | * the connected driver when the link status changes. If you | |
1177 | * don't want, or can't use the provided functionality, you may | |
1178 | * choose to call only the subset of functions which provide | |
1179 | * the desired functionality. | |
1180 | */ | |
e109374f | 1181 | struct phy_device *phy_connect(struct net_device *dev, const char *bus_id, |
2f53e904 SS |
1182 | void (*handler)(struct net_device *), |
1183 | phy_interface_t interface) | |
e1393456 AF |
1184 | { |
1185 | struct phy_device *phydev; | |
fa94f6d9 GL |
1186 | struct device *d; |
1187 | int rc; | |
e1393456 | 1188 | |
fa94f6d9 | 1189 | /* Search the list of PHY devices on the mdio bus for the |
2f53e904 SS |
1190 | * PHY with the requested name |
1191 | */ | |
fa94f6d9 GL |
1192 | d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id); |
1193 | if (!d) { | |
1194 | pr_err("PHY %s not found\n", bus_id); | |
1195 | return ERR_PTR(-ENODEV); | |
1196 | } | |
1197 | phydev = to_phy_device(d); | |
e1393456 | 1198 | |
f9a8f83b | 1199 | rc = phy_connect_direct(dev, phydev, handler, interface); |
17ae1c65 | 1200 | put_device(d); |
fa94f6d9 GL |
1201 | if (rc) |
1202 | return ERR_PTR(rc); | |
e1393456 AF |
1203 | |
1204 | return phydev; | |
1205 | } | |
1206 | EXPORT_SYMBOL(phy_connect); | |
1207 | ||
b3df0da8 | 1208 | /** |
2f53e904 SS |
1209 | * phy_disconnect - disable interrupts, stop state machine, and detach a PHY |
1210 | * device | |
b3df0da8 RD |
1211 | * @phydev: target phy_device struct |
1212 | */ | |
e1393456 AF |
1213 | void phy_disconnect(struct phy_device *phydev) |
1214 | { | |
472115d9 HK |
1215 | if (phy_is_started(phydev)) |
1216 | phy_stop(phydev); | |
1217 | ||
bb658ab7 | 1218 | if (phy_interrupt_is_valid(phydev)) |
07b09289 | 1219 | phy_free_interrupt(phydev); |
e1393456 | 1220 | |
e1393456 AF |
1221 | phydev->adjust_link = NULL; |
1222 | ||
1223 | phy_detach(phydev); | |
1224 | } | |
1225 | EXPORT_SYMBOL(phy_disconnect); | |
1226 | ||
87aa9f9c FF |
1227 | /** |
1228 | * phy_poll_reset - Safely wait until a PHY reset has properly completed | |
1229 | * @phydev: The PHY device to poll | |
1230 | * | |
1231 | * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as | |
1232 | * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR | |
1233 | * register must be polled until the BMCR_RESET bit clears. | |
1234 | * | |
1235 | * Furthermore, any attempts to write to PHY registers may have no effect | |
1236 | * or even generate MDIO bus errors until this is complete. | |
1237 | * | |
1238 | * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the | |
1239 | * standard and do not fully reset after the BMCR_RESET bit is set, and may | |
1240 | * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an | |
1241 | * effort to support such broken PHYs, this function is separate from the | |
1242 | * standard phy_init_hw() which will zero all the other bits in the BMCR | |
1243 | * and reapply all driver-specific and board-specific fixups. | |
1244 | */ | |
1245 | static int phy_poll_reset(struct phy_device *phydev) | |
1246 | { | |
1247 | /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */ | |
745a237c | 1248 | int ret, val; |
87aa9f9c | 1249 | |
745a237c DZ |
1250 | ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET), |
1251 | 50000, 600000, true); | |
1252 | if (ret) | |
1253 | return ret; | |
2f53e904 | 1254 | /* Some chips (smsc911x) may still need up to another 1ms after the |
87aa9f9c FF |
1255 | * BMCR_RESET bit is cleared before they are usable. |
1256 | */ | |
1257 | msleep(1); | |
1258 | return 0; | |
1259 | } | |
1260 | ||
2f5cb434 AV |
1261 | int phy_init_hw(struct phy_device *phydev) |
1262 | { | |
9df81dd7 | 1263 | int ret = 0; |
2f5cb434 | 1264 | |
bafbdd52 SS |
1265 | /* Deassert the reset signal */ |
1266 | phy_device_reset(phydev, 0); | |
1267 | ||
a5996989 | 1268 | if (!phydev->drv) |
2f5cb434 AV |
1269 | return 0; |
1270 | ||
9576e9fa | 1271 | if (phydev->drv->soft_reset) { |
9df81dd7 | 1272 | ret = phydev->drv->soft_reset(phydev); |
aadbd27f CM |
1273 | if (ret < 0) |
1274 | return ret; | |
1275 | ||
9576e9fa | 1276 | /* see comment in genphy_soft_reset for an explanation */ |
aadbd27f | 1277 | phydev->suspended = 0; |
9576e9fa | 1278 | } |
9df81dd7 | 1279 | |
2f5cb434 AV |
1280 | ret = phy_scan_fixups(phydev); |
1281 | if (ret < 0) | |
1282 | return ret; | |
1283 | ||
243ad8df RKO |
1284 | phy_interface_zero(phydev->possible_interfaces); |
1285 | ||
4c0d2e96 | 1286 | if (phydev->drv->config_init) { |
a5996989 | 1287 | ret = phydev->drv->config_init(phydev); |
4c0d2e96 HK |
1288 | if (ret < 0) |
1289 | return ret; | |
1290 | } | |
a5996989 | 1291 | |
4c0d2e96 HK |
1292 | if (phydev->drv->config_intr) { |
1293 | ret = phydev->drv->config_intr(phydev); | |
1294 | if (ret < 0) | |
1295 | return ret; | |
1296 | } | |
1297 | ||
1298 | return 0; | |
2f5cb434 | 1299 | } |
87aa9f9c | 1300 | EXPORT_SYMBOL(phy_init_hw); |
2f5cb434 | 1301 | |
2220943a AL |
1302 | void phy_attached_info(struct phy_device *phydev) |
1303 | { | |
1304 | phy_attached_print(phydev, NULL); | |
1305 | } | |
1306 | EXPORT_SYMBOL(phy_attached_info); | |
1307 | ||
a98cabdb | 1308 | #define ATTACHED_FMT "attached PHY driver %s(mii_bus:phy_addr=%s, irq=%s)" |
e27f1787 | 1309 | char *phy_attached_info_irq(struct phy_device *phydev) |
2220943a | 1310 | { |
5e369aef | 1311 | char *irq_str; |
059fbe8b | 1312 | char irq_num[8]; |
5e369aef RP |
1313 | |
1314 | switch(phydev->irq) { | |
1315 | case PHY_POLL: | |
1316 | irq_str = "POLL"; | |
1317 | break; | |
93e8990c HK |
1318 | case PHY_MAC_INTERRUPT: |
1319 | irq_str = "MAC"; | |
5e369aef RP |
1320 | break; |
1321 | default: | |
1322 | snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq); | |
1323 | irq_str = irq_num; | |
1324 | break; | |
1325 | } | |
1326 | ||
e27f1787 FF |
1327 | return kasprintf(GFP_KERNEL, "%s", irq_str); |
1328 | } | |
1329 | EXPORT_SYMBOL(phy_attached_info_irq); | |
1330 | ||
1331 | void phy_attached_print(struct phy_device *phydev, const char *fmt, ...) | |
1332 | { | |
a98cabdb | 1333 | const char *unbound = phydev->drv ? "" : "[unbound] "; |
e27f1787 | 1334 | char *irq_str = phy_attached_info_irq(phydev); |
fcd03e36 | 1335 | |
2220943a | 1336 | if (!fmt) { |
a98cabdb HK |
1337 | phydev_info(phydev, ATTACHED_FMT "\n", unbound, |
1338 | phydev_name(phydev), irq_str); | |
2220943a AL |
1339 | } else { |
1340 | va_list ap; | |
1341 | ||
a98cabdb HK |
1342 | phydev_info(phydev, ATTACHED_FMT, unbound, |
1343 | phydev_name(phydev), irq_str); | |
2220943a AL |
1344 | |
1345 | va_start(ap, fmt); | |
1346 | vprintk(fmt, ap); | |
1347 | va_end(ap); | |
1348 | } | |
e27f1787 | 1349 | kfree(irq_str); |
2220943a AL |
1350 | } |
1351 | EXPORT_SYMBOL(phy_attached_print); | |
1352 | ||
53cfca2d VO |
1353 | static void phy_sysfs_create_links(struct phy_device *phydev) |
1354 | { | |
1355 | struct net_device *dev = phydev->attached_dev; | |
1356 | int err; | |
1357 | ||
2db2d9d1 IC |
1358 | if (!dev) |
1359 | return; | |
1360 | ||
53cfca2d VO |
1361 | err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj, |
1362 | "attached_dev"); | |
1363 | if (err) | |
1364 | return; | |
1365 | ||
1366 | err = sysfs_create_link_nowarn(&dev->dev.kobj, | |
1367 | &phydev->mdio.dev.kobj, | |
1368 | "phydev"); | |
1369 | if (err) { | |
1370 | dev_err(&dev->dev, "could not add device link to %s err %d\n", | |
1371 | kobject_name(&phydev->mdio.dev.kobj), | |
1372 | err); | |
1373 | /* non-fatal - some net drivers can use one netdevice | |
1374 | * with more then one phy | |
1375 | */ | |
1376 | } | |
1377 | ||
1378 | phydev->sysfs_links = true; | |
1379 | } | |
1380 | ||
c920f745 IC |
1381 | static ssize_t |
1382 | phy_standalone_show(struct device *dev, struct device_attribute *attr, | |
1383 | char *buf) | |
1384 | { | |
1385 | struct phy_device *phydev = to_phy_device(dev); | |
1386 | ||
b5155ddd | 1387 | return sysfs_emit(buf, "%d\n", !phydev->attached_dev); |
c920f745 IC |
1388 | } |
1389 | static DEVICE_ATTR_RO(phy_standalone); | |
1390 | ||
b2db6f4a MC |
1391 | /** |
1392 | * phy_sfp_connect_phy - Connect the SFP module's PHY to the upstream PHY | |
1393 | * @upstream: pointer to the upstream phy device | |
1394 | * @phy: pointer to the SFP module's phy device | |
1395 | * | |
1396 | * This helper allows keeping track of PHY devices on the link. It adds the | |
1397 | * SFP module's phy to the phy namespace of the upstream phy | |
1398 | * | |
1399 | * Return: 0 on success, otherwise a negative error code. | |
1400 | */ | |
1401 | int phy_sfp_connect_phy(void *upstream, struct phy_device *phy) | |
1402 | { | |
1403 | struct phy_device *phydev = upstream; | |
1404 | struct net_device *dev = phydev->attached_dev; | |
1405 | ||
1406 | if (dev) | |
1407 | return phy_link_topo_add_phy(dev, phy, PHY_UPSTREAM_PHY, phydev); | |
1408 | ||
1409 | return 0; | |
1410 | } | |
1411 | EXPORT_SYMBOL(phy_sfp_connect_phy); | |
1412 | ||
1413 | /** | |
1414 | * phy_sfp_disconnect_phy - Disconnect the SFP module's PHY from the upstream PHY | |
1415 | * @upstream: pointer to the upstream phy device | |
1416 | * @phy: pointer to the SFP module's phy device | |
1417 | * | |
1418 | * This helper allows keeping track of PHY devices on the link. It removes the | |
1419 | * SFP module's phy to the phy namespace of the upstream phy. As the module phy | |
1420 | * will be destroyed, re-inserting the same module will add a new phy with a | |
1421 | * new index. | |
1422 | */ | |
1423 | void phy_sfp_disconnect_phy(void *upstream, struct phy_device *phy) | |
1424 | { | |
1425 | struct phy_device *phydev = upstream; | |
1426 | struct net_device *dev = phydev->attached_dev; | |
1427 | ||
1428 | if (dev) | |
1429 | phy_link_topo_del_phy(dev, phy); | |
1430 | } | |
1431 | EXPORT_SYMBOL(phy_sfp_disconnect_phy); | |
1432 | ||
298e54fa RK |
1433 | /** |
1434 | * phy_sfp_attach - attach the SFP bus to the PHY upstream network device | |
1435 | * @upstream: pointer to the phy device | |
1436 | * @bus: sfp bus representing cage being attached | |
1437 | * | |
1438 | * This is used to fill in the sfp_upstream_ops .attach member. | |
1439 | */ | |
1440 | void phy_sfp_attach(void *upstream, struct sfp_bus *bus) | |
1441 | { | |
1442 | struct phy_device *phydev = upstream; | |
1443 | ||
1444 | if (phydev->attached_dev) | |
1445 | phydev->attached_dev->sfp_bus = bus; | |
1446 | phydev->sfp_bus_attached = true; | |
1447 | } | |
1448 | EXPORT_SYMBOL(phy_sfp_attach); | |
1449 | ||
1450 | /** | |
1451 | * phy_sfp_detach - detach the SFP bus from the PHY upstream network device | |
1452 | * @upstream: pointer to the phy device | |
1453 | * @bus: sfp bus representing cage being attached | |
1454 | * | |
1455 | * This is used to fill in the sfp_upstream_ops .detach member. | |
1456 | */ | |
1457 | void phy_sfp_detach(void *upstream, struct sfp_bus *bus) | |
1458 | { | |
1459 | struct phy_device *phydev = upstream; | |
1460 | ||
1461 | if (phydev->attached_dev) | |
1462 | phydev->attached_dev->sfp_bus = NULL; | |
1463 | phydev->sfp_bus_attached = false; | |
1464 | } | |
1465 | EXPORT_SYMBOL(phy_sfp_detach); | |
1466 | ||
1467 | /** | |
1468 | * phy_sfp_probe - probe for a SFP cage attached to this PHY device | |
1469 | * @phydev: Pointer to phy_device | |
1470 | * @ops: SFP's upstream operations | |
1471 | */ | |
1472 | int phy_sfp_probe(struct phy_device *phydev, | |
1473 | const struct sfp_upstream_ops *ops) | |
1474 | { | |
1475 | struct sfp_bus *bus; | |
e3f2d557 | 1476 | int ret = 0; |
298e54fa RK |
1477 | |
1478 | if (phydev->mdio.dev.fwnode) { | |
1479 | bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode); | |
1480 | if (IS_ERR(bus)) | |
1481 | return PTR_ERR(bus); | |
1482 | ||
1483 | phydev->sfp_bus = bus; | |
1484 | ||
1485 | ret = sfp_bus_add_upstream(bus, phydev, ops); | |
1486 | sfp_bus_put(bus); | |
1487 | } | |
e3f2d557 | 1488 | return ret; |
298e54fa RK |
1489 | } |
1490 | EXPORT_SYMBOL(phy_sfp_probe); | |
1491 | ||
0bd199fd | 1492 | static bool phy_drv_supports_irq(const struct phy_driver *phydrv) |
61c81872 AW |
1493 | { |
1494 | return phydrv->config_intr && phydrv->handle_interrupt; | |
1495 | } | |
1496 | ||
b3df0da8 | 1497 | /** |
fa94f6d9 | 1498 | * phy_attach_direct - attach a network device to a given PHY device pointer |
b3df0da8 | 1499 | * @dev: network device to attach |
fa94f6d9 | 1500 | * @phydev: Pointer to phy_device to attach |
b3df0da8 RD |
1501 | * @flags: PHY device's dev_flags |
1502 | * @interface: PHY device's interface | |
e1393456 | 1503 | * |
b3df0da8 | 1504 | * Description: Called by drivers to attach to a particular PHY |
e1393456 | 1505 | * device. The phy_device is found, and properly hooked up |
257184d7 AF |
1506 | * to the phy_driver. If no driver is attached, then a |
1507 | * generic driver is used. The phy_device is given a ptr to | |
e1393456 | 1508 | * the attaching device, and given a callback for link status |
b3df0da8 | 1509 | * change. The phy_device is returned to the attaching driver. |
7322967b | 1510 | * This function takes a reference on the phy device. |
e1393456 | 1511 | */ |
257184d7 AF |
1512 | int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, |
1513 | u32 flags, phy_interface_t interface) | |
e1393456 | 1514 | { |
e5a03bfd AL |
1515 | struct mii_bus *bus = phydev->mdio.bus; |
1516 | struct device *d = &phydev->mdio.dev; | |
2db2d9d1 | 1517 | struct module *ndev_owner = NULL; |
6d9f66ac | 1518 | bool using_genphy = false; |
d005a09e | 1519 | int err; |
e1393456 | 1520 | |
ec988ad7 FF |
1521 | /* For Ethernet device drivers that register their own MDIO bus, we |
1522 | * will have bus->owner match ndev_mod, so we do not want to increment | |
1523 | * our own module->refcnt here, otherwise we would not be able to | |
1524 | * unload later on. | |
1525 | */ | |
2db2d9d1 IC |
1526 | if (dev) |
1527 | ndev_owner = dev->dev.parent->driver->owner; | |
ec988ad7 | 1528 | if (ndev_owner != bus->owner && !try_module_get(bus->owner)) { |
2db2d9d1 | 1529 | phydev_err(phydev, "failed to get the bus module\n"); |
3e3aaf64 RK |
1530 | return -EIO; |
1531 | } | |
1532 | ||
7322967b RK |
1533 | get_device(d); |
1534 | ||
e1393456 | 1535 | /* Assume that if there is no driver, that it doesn't |
2f53e904 SS |
1536 | * exist, and we should use the genphy driver. |
1537 | */ | |
ef899c07 | 1538 | if (!d->driver) { |
257184d7 | 1539 | if (phydev->is_c45) |
22b56e82 | 1540 | d->driver = &genphy_c45_driver.mdiodrv.driver; |
257184d7 | 1541 | else |
921690f2 | 1542 | d->driver = &genphy_driver.mdiodrv.driver; |
e1393456 | 1543 | |
6d9f66ac FF |
1544 | using_genphy = true; |
1545 | } | |
1546 | ||
1547 | if (!try_module_get(d->driver->owner)) { | |
2db2d9d1 | 1548 | phydev_err(phydev, "failed to get the device driver module\n"); |
6d9f66ac FF |
1549 | err = -EIO; |
1550 | goto error_put_device; | |
1551 | } | |
1552 | ||
1553 | if (using_genphy) { | |
e1393456 | 1554 | err = d->driver->probe(d); |
b7a00ecd JG |
1555 | if (err >= 0) |
1556 | err = device_bind_driver(d); | |
e1393456 | 1557 | |
b7a00ecd | 1558 | if (err) |
6d9f66ac | 1559 | goto error_module_put; |
e1393456 AF |
1560 | } |
1561 | ||
1562 | if (phydev->attached_dev) { | |
fa94f6d9 | 1563 | dev_err(&dev->dev, "PHY already attached\n"); |
3e3aaf64 RK |
1564 | err = -EBUSY; |
1565 | goto error; | |
b3565f27 EG |
1566 | } |
1567 | ||
a81497be | 1568 | phydev->phy_link_change = phy_link_change; |
2db2d9d1 IC |
1569 | if (dev) { |
1570 | phydev->attached_dev = dev; | |
1571 | dev->phydev = phydev; | |
298e54fa RK |
1572 | |
1573 | if (phydev->sfp_bus_attached) | |
1574 | dev->sfp_bus = phydev->sfp_bus; | |
38496878 MC |
1575 | |
1576 | err = phy_link_topo_add_phy(dev, phydev, PHY_UPSTREAM_MAC, dev); | |
1577 | if (err) | |
1578 | goto error; | |
2db2d9d1 | 1579 | } |
a3995460 FF |
1580 | |
1581 | /* Some Ethernet drivers try to connect to a PHY device before | |
1582 | * calling register_netdevice() -> netdev_register_kobject() and | |
1583 | * does the dev->dev.kobj initialization. Here we only check for | |
1584 | * success which indicates that the network device kobject is | |
1585 | * ready. Once we do that we still need to keep track of whether | |
1586 | * links were successfully set up or not for phy_detach() to | |
1587 | * remove them accordingly. | |
1588 | */ | |
1589 | phydev->sysfs_links = false; | |
1590 | ||
53cfca2d | 1591 | phy_sysfs_create_links(phydev); |
e1393456 | 1592 | |
c920f745 IC |
1593 | if (!phydev->attached_dev) { |
1594 | err = sysfs_create_file(&phydev->mdio.dev.kobj, | |
1595 | &dev_attr_phy_standalone.attr); | |
1596 | if (err) | |
1597 | phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n"); | |
1598 | } | |
1599 | ||
e7312efb | 1600 | phydev->dev_flags |= flags; |
e1393456 | 1601 | |
e8a2b6a4 AF |
1602 | phydev->interface = interface; |
1603 | ||
ef24b16b AV |
1604 | phydev->state = PHY_READY; |
1605 | ||
07caad0b LW |
1606 | phydev->interrupts = PHY_INTERRUPT_DISABLED; |
1607 | ||
7d885863 MW |
1608 | /* PHYs can request to use poll mode even though they have an |
1609 | * associated interrupt line. This could be the case if they | |
1610 | * detect a broken interrupt handling. | |
1611 | */ | |
1612 | if (phydev->dev_flags & PHY_F_NO_IRQ) | |
1613 | phydev->irq = PHY_POLL; | |
1614 | ||
61c81872 AW |
1615 | if (!phy_drv_supports_irq(phydev->drv) && phy_interrupt_is_valid(phydev)) |
1616 | phydev->irq = PHY_POLL; | |
1617 | ||
4217a64e MW |
1618 | /* Port is set to PORT_TP by default and the actual PHY driver will set |
1619 | * it to different value depending on the PHY configuration. If we have | |
1620 | * the generic PHY driver we can't figure it out, thus set the old | |
1621 | * legacy PORT_MII value. | |
1622 | */ | |
1623 | if (using_genphy) | |
1624 | phydev->port = PORT_MII; | |
1625 | ||
113c74d8 SS |
1626 | /* Initial carrier state is off as the phy is about to be |
1627 | * (re)initialized. | |
1628 | */ | |
2db2d9d1 IC |
1629 | if (dev) |
1630 | netif_carrier_off(phydev->attached_dev); | |
113c74d8 | 1631 | |
e8a2b6a4 AF |
1632 | /* Do initial configuration here, now that |
1633 | * we have certain key parameters | |
2f53e904 SS |
1634 | * (dev_flags and interface) |
1635 | */ | |
d005a09e MKB |
1636 | err = phy_init_hw(phydev); |
1637 | if (err) | |
a7dac9f9 | 1638 | goto error; |
1211ce53 | 1639 | |
a7dac9f9 | 1640 | phy_resume(phydev); |
b1dfc0f7 DG |
1641 | if (!phydev->is_on_sfp_module) |
1642 | phy_led_triggers_register(phydev); | |
2e0bc452 | 1643 | |
bc66fa87 XW |
1644 | /** |
1645 | * If the external phy used by current mac interface is managed by | |
1646 | * another mac interface, so we should create a device link between | |
1647 | * phy dev and mac dev. | |
1648 | */ | |
73a87602 | 1649 | if (dev && phydev->mdio.bus->parent && dev->dev.parent != phydev->mdio.bus->parent) |
bc66fa87 XW |
1650 | phydev->devlink = device_link_add(dev->dev.parent, &phydev->mdio.dev, |
1651 | DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS); | |
1652 | ||
d005a09e | 1653 | return err; |
3e3aaf64 RK |
1654 | |
1655 | error: | |
6d9f66ac | 1656 | /* phy_detach() does all of the cleanup below */ |
a7dac9f9 | 1657 | phy_detach(phydev); |
6d9f66ac FF |
1658 | return err; |
1659 | ||
1660 | error_module_put: | |
cafe8df8 | 1661 | module_put(d->driver->owner); |
369eb2c9 | 1662 | d->driver = NULL; |
6d9f66ac FF |
1663 | error_put_device: |
1664 | put_device(d); | |
ec988ad7 FF |
1665 | if (ndev_owner != bus->owner) |
1666 | module_put(bus->owner); | |
3e3aaf64 | 1667 | return err; |
fa94f6d9 | 1668 | } |
257184d7 | 1669 | EXPORT_SYMBOL(phy_attach_direct); |
fa94f6d9 GL |
1670 | |
1671 | /** | |
1672 | * phy_attach - attach a network device to a particular PHY device | |
1673 | * @dev: network device to attach | |
1674 | * @bus_id: Bus ID of PHY device to attach | |
fa94f6d9 GL |
1675 | * @interface: PHY device's interface |
1676 | * | |
1677 | * Description: Same as phy_attach_direct() except that a PHY bus_id | |
1678 | * string is passed instead of a pointer to a struct phy_device. | |
1679 | */ | |
2f53e904 SS |
1680 | struct phy_device *phy_attach(struct net_device *dev, const char *bus_id, |
1681 | phy_interface_t interface) | |
fa94f6d9 | 1682 | { |
fa94f6d9 GL |
1683 | struct phy_device *phydev; |
1684 | struct device *d; | |
1685 | int rc; | |
1686 | ||
82c76aca IC |
1687 | if (!dev) |
1688 | return ERR_PTR(-EINVAL); | |
1689 | ||
fa94f6d9 | 1690 | /* Search the list of PHY devices on the mdio bus for the |
2f53e904 SS |
1691 | * PHY with the requested name |
1692 | */ | |
81800aef | 1693 | d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id); |
fa94f6d9 GL |
1694 | if (!d) { |
1695 | pr_err("PHY %s not found\n", bus_id); | |
1696 | return ERR_PTR(-ENODEV); | |
e8a2b6a4 | 1697 | } |
fa94f6d9 GL |
1698 | phydev = to_phy_device(d); |
1699 | ||
f9a8f83b | 1700 | rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); |
17ae1c65 | 1701 | put_device(d); |
fa94f6d9 GL |
1702 | if (rc) |
1703 | return ERR_PTR(rc); | |
e8a2b6a4 | 1704 | |
e1393456 AF |
1705 | return phydev; |
1706 | } | |
1707 | EXPORT_SYMBOL(phy_attach); | |
1708 | ||
5db5ea99 FF |
1709 | static bool phy_driver_is_genphy_kind(struct phy_device *phydev, |
1710 | struct device_driver *driver) | |
1711 | { | |
1712 | struct device *d = &phydev->mdio.dev; | |
1713 | bool ret = false; | |
1714 | ||
1715 | if (!phydev->drv) | |
1716 | return ret; | |
1717 | ||
1718 | get_device(d); | |
1719 | ret = d->driver == driver; | |
1720 | put_device(d); | |
1721 | ||
1722 | return ret; | |
1723 | } | |
1724 | ||
1725 | bool phy_driver_is_genphy(struct phy_device *phydev) | |
1726 | { | |
1727 | return phy_driver_is_genphy_kind(phydev, | |
1728 | &genphy_driver.mdiodrv.driver); | |
1729 | } | |
1730 | EXPORT_SYMBOL_GPL(phy_driver_is_genphy); | |
1731 | ||
1732 | bool phy_driver_is_genphy_10g(struct phy_device *phydev) | |
1733 | { | |
1734 | return phy_driver_is_genphy_kind(phydev, | |
22b56e82 | 1735 | &genphy_c45_driver.mdiodrv.driver); |
5db5ea99 FF |
1736 | } |
1737 | EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g); | |
1738 | ||
b3df0da8 RD |
1739 | /** |
1740 | * phy_detach - detach a PHY device from its network device | |
1741 | * @phydev: target phy_device struct | |
7322967b RK |
1742 | * |
1743 | * This detaches the phy device from its network device and the phy | |
1744 | * driver, and drops the reference count taken in phy_attach_direct(). | |
b3df0da8 | 1745 | */ |
e1393456 AF |
1746 | void phy_detach(struct phy_device *phydev) |
1747 | { | |
ec988ad7 | 1748 | struct net_device *dev = phydev->attached_dev; |
2db2d9d1 | 1749 | struct module *ndev_owner = NULL; |
3e3aaf64 | 1750 | struct mii_bus *bus; |
b3565f27 | 1751 | |
0795b05a | 1752 | if (phydev->devlink) { |
bc66fa87 | 1753 | device_link_del(phydev->devlink); |
0795b05a WF |
1754 | phydev->devlink = NULL; |
1755 | } | |
bc66fa87 | 1756 | |
a3995460 | 1757 | if (phydev->sysfs_links) { |
2db2d9d1 IC |
1758 | if (dev) |
1759 | sysfs_remove_link(&dev->dev.kobj, "phydev"); | |
a3995460 FF |
1760 | sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev"); |
1761 | } | |
c920f745 IC |
1762 | |
1763 | if (!phydev->attached_dev) | |
1764 | sysfs_remove_file(&phydev->mdio.dev.kobj, | |
1765 | &dev_attr_phy_standalone.attr); | |
1766 | ||
93f41e67 | 1767 | phy_suspend(phydev); |
2db2d9d1 | 1768 | if (dev) { |
35f7cad1 KM |
1769 | struct hwtstamp_provider *hwprov; |
1770 | ||
1771 | hwprov = rtnl_dereference(dev->hwprov); | |
1772 | /* Disable timestamp if it is the one selected */ | |
1773 | if (hwprov && hwprov->phydev == phydev) { | |
1774 | rcu_assign_pointer(dev->hwprov, NULL); | |
1775 | kfree_rcu(hwprov, rcu_head); | |
1776 | } | |
1777 | ||
2db2d9d1 IC |
1778 | phydev->attached_dev->phydev = NULL; |
1779 | phydev->attached_dev = NULL; | |
38496878 | 1780 | phy_link_topo_del_phy(dev, phydev); |
2db2d9d1 | 1781 | } |
9525ae83 | 1782 | phydev->phylink = NULL; |
e1393456 | 1783 | |
b1dfc0f7 DG |
1784 | if (!phydev->is_on_sfp_module) |
1785 | phy_led_triggers_unregister(phydev); | |
0075bd69 | 1786 | |
c2b727df FF |
1787 | if (phydev->mdio.dev.driver) |
1788 | module_put(phydev->mdio.dev.driver->owner); | |
6d9f66ac | 1789 | |
e1393456 AF |
1790 | /* If the device had no specific driver before (i.e. - it |
1791 | * was using the generic driver), we unbind the device | |
1792 | * from the generic driver so that there's a chance a | |
2f53e904 SS |
1793 | * real driver could be loaded |
1794 | */ | |
5db5ea99 FF |
1795 | if (phy_driver_is_genphy(phydev) || |
1796 | phy_driver_is_genphy_10g(phydev)) | |
921690f2 | 1797 | device_release_driver(&phydev->mdio.dev); |
3e3aaf64 | 1798 | |
cbda1b16 MB |
1799 | /* Assert the reset signal */ |
1800 | phy_device_reset(phydev, 1); | |
1801 | ||
7322967b RK |
1802 | /* |
1803 | * The phydev might go away on the put_device() below, so avoid | |
1804 | * a use-after-free bug by reading the underlying bus first. | |
1805 | */ | |
e5a03bfd | 1806 | bus = phydev->mdio.bus; |
3e3aaf64 | 1807 | |
e5a03bfd | 1808 | put_device(&phydev->mdio.dev); |
2db2d9d1 IC |
1809 | if (dev) |
1810 | ndev_owner = dev->dev.parent->driver->owner; | |
ec988ad7 FF |
1811 | if (ndev_owner != bus->owner) |
1812 | module_put(bus->owner); | |
e1393456 AF |
1813 | } |
1814 | EXPORT_SYMBOL(phy_detach); | |
1815 | ||
481b5d93 SH |
1816 | int phy_suspend(struct phy_device *phydev) |
1817 | { | |
8a8f8281 | 1818 | struct net_device *netdev = phydev->attached_dev; |
0bd199fd | 1819 | const struct phy_driver *phydrv = phydev->drv; |
8a8f8281 | 1820 | int ret; |
481b5d93 | 1821 | |
19e6ad2c | 1822 | if (phydev->suspended || !phydrv) |
d70c47c8 HK |
1823 | return 0; |
1824 | ||
4f534b7f | 1825 | phydev->wol_enabled = phy_drv_wol_enabled(phydev) || |
3ebbd9f6 | 1826 | (netdev && netdev->ethtool->wol_enabled); |
a7e34480 FF |
1827 | /* If the device has WOL enabled, we cannot suspend the PHY */ |
1828 | if (phydev->wol_enabled && !(phydrv->flags & PHY_ALWAYS_CALL_SUSPEND)) | |
481b5d93 SH |
1829 | return -EBUSY; |
1830 | ||
19e6ad2c | 1831 | if (!phydrv->suspend) |
8a8f8281 | 1832 | return 0; |
8a477a6f | 1833 | |
8a8f8281 HK |
1834 | ret = phydrv->suspend(phydev); |
1835 | if (!ret) | |
1836 | phydev->suspended = true; | |
8a477a6f FF |
1837 | |
1838 | return ret; | |
481b5d93 | 1839 | } |
ca127697 | 1840 | EXPORT_SYMBOL(phy_suspend); |
481b5d93 | 1841 | |
9c2c2e62 | 1842 | int __phy_resume(struct phy_device *phydev) |
481b5d93 | 1843 | { |
0bd199fd | 1844 | const struct phy_driver *phydrv = phydev->drv; |
8a8f8281 | 1845 | int ret; |
481b5d93 | 1846 | |
e6e918d4 | 1847 | lockdep_assert_held(&phydev->lock); |
f5e64032 | 1848 | |
8a8f8281 HK |
1849 | if (!phydrv || !phydrv->resume) |
1850 | return 0; | |
8a477a6f | 1851 | |
8a8f8281 HK |
1852 | ret = phydrv->resume(phydev); |
1853 | if (!ret) | |
1854 | phydev->suspended = false; | |
8a477a6f FF |
1855 | |
1856 | return ret; | |
481b5d93 | 1857 | } |
9c2c2e62 AL |
1858 | EXPORT_SYMBOL(__phy_resume); |
1859 | ||
1860 | int phy_resume(struct phy_device *phydev) | |
1861 | { | |
1862 | int ret; | |
1863 | ||
1864 | mutex_lock(&phydev->lock); | |
1865 | ret = __phy_resume(phydev); | |
1866 | mutex_unlock(&phydev->lock); | |
1867 | ||
1868 | return ret; | |
1869 | } | |
ca127697 | 1870 | EXPORT_SYMBOL(phy_resume); |
e1393456 | 1871 | |
a9668491 RL |
1872 | /** |
1873 | * phy_reset_after_clk_enable - perform a PHY reset if needed | |
1874 | * @phydev: target phy_device struct | |
1875 | * | |
1876 | * Description: Some PHYs are known to need a reset after their refclk was | |
1877 | * enabled. This function evaluates the flags and perform the reset if it's | |
1878 | * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy | |
1879 | * was reset. | |
1880 | */ | |
1881 | int phy_reset_after_clk_enable(struct phy_device *phydev) | |
1882 | { | |
1883 | if (!phydev || !phydev->drv) | |
1884 | return -ENODEV; | |
1885 | ||
1886 | if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) { | |
1887 | phy_device_reset(phydev, 1); | |
1888 | phy_device_reset(phydev, 0); | |
1889 | return 1; | |
1890 | } | |
1891 | ||
1892 | return 0; | |
1893 | } | |
1894 | EXPORT_SYMBOL(phy_reset_after_clk_enable); | |
1895 | ||
00db8189 AF |
1896 | /* Generic PHY support and helper functions */ |
1897 | ||
b3df0da8 | 1898 | /** |
25985edc | 1899 | * genphy_config_advert - sanitize and advertise auto-negotiation parameters |
b3df0da8 | 1900 | * @phydev: target phy_device struct |
6ff3cddc | 1901 | * @advert: auto-negotiation parameters to advertise |
00db8189 | 1902 | * |
b3df0da8 | 1903 | * Description: Writes MII_ADVERTISE with the appropriate values, |
00db8189 | 1904 | * after sanitizing the values to make sure we only advertise |
51e2a384 TP |
1905 | * what is supported. Returns < 0 on error, 0 if the PHY's advertisement |
1906 | * hasn't changed, and > 0 if it has changed. | |
00db8189 | 1907 | */ |
6ff3cddc RKO |
1908 | static int genphy_config_advert(struct phy_device *phydev, |
1909 | const unsigned long *advert) | |
00db8189 | 1910 | { |
3eef8689 HK |
1911 | int err, bmsr, changed = 0; |
1912 | u32 adv; | |
00db8189 | 1913 | |
6ff3cddc | 1914 | adv = linkmode_adv_to_mii_adv_t(advert); |
00db8189 AF |
1915 | |
1916 | /* Setup standard advertisement */ | |
4f9744ed HK |
1917 | err = phy_modify_changed(phydev, MII_ADVERTISE, |
1918 | ADVERTISE_ALL | ADVERTISE_100BASE4 | | |
1919 | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM, | |
3eef8689 | 1920 | adv); |
4f9744ed HK |
1921 | if (err < 0) |
1922 | return err; | |
1923 | if (err > 0) | |
51e2a384 | 1924 | changed = 1; |
00db8189 | 1925 | |
5273e3a5 FF |
1926 | bmsr = phy_read(phydev, MII_BMSR); |
1927 | if (bmsr < 0) | |
1928 | return bmsr; | |
1929 | ||
1930 | /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all | |
1931 | * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a | |
1932 | * logical 1. | |
1933 | */ | |
1934 | if (!(bmsr & BMSR_ESTATEN)) | |
1935 | return changed; | |
1936 | ||
6ff3cddc | 1937 | adv = linkmode_adv_to_mii_ctrl1000_t(advert); |
00db8189 | 1938 | |
4f9744ed HK |
1939 | err = phy_modify_changed(phydev, MII_CTRL1000, |
1940 | ADVERTISE_1000FULL | ADVERTISE_1000HALF, | |
1941 | adv); | |
5273e3a5 FF |
1942 | if (err < 0) |
1943 | return err; | |
4f9744ed HK |
1944 | if (err > 0) |
1945 | changed = 1; | |
5273e3a5 | 1946 | |
51e2a384 | 1947 | return changed; |
00db8189 | 1948 | } |
00db8189 | 1949 | |
fa6e98ce HK |
1950 | /** |
1951 | * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters | |
1952 | * @phydev: target phy_device struct | |
1953 | * | |
1954 | * Description: Writes MII_ADVERTISE with the appropriate values, | |
1955 | * after sanitizing the values to make sure we only advertise | |
1956 | * what is supported. Returns < 0 on error, 0 if the PHY's advertisement | |
1957 | * hasn't changed, and > 0 if it has changed. This function is intended | |
1958 | * for Clause 37 1000Base-X mode. | |
1959 | */ | |
1960 | static int genphy_c37_config_advert(struct phy_device *phydev) | |
1961 | { | |
1962 | u16 adv = 0; | |
1963 | ||
1964 | /* Only allow advertising what this PHY supports */ | |
1965 | linkmode_and(phydev->advertising, phydev->advertising, | |
1966 | phydev->supported); | |
1967 | ||
1968 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, | |
1969 | phydev->advertising)) | |
1970 | adv |= ADVERTISE_1000XFULL; | |
1971 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, | |
1972 | phydev->advertising)) | |
1973 | adv |= ADVERTISE_1000XPAUSE; | |
1974 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, | |
1975 | phydev->advertising)) | |
1976 | adv |= ADVERTISE_1000XPSE_ASYM; | |
1977 | ||
1978 | return phy_modify_changed(phydev, MII_ADVERTISE, | |
1979 | ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE | | |
1980 | ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM, | |
1981 | adv); | |
1982 | } | |
1983 | ||
b3df0da8 RD |
1984 | /** |
1985 | * genphy_setup_forced - configures/forces speed/duplex from @phydev | |
1986 | * @phydev: target phy_device struct | |
00db8189 | 1987 | * |
b3df0da8 | 1988 | * Description: Configures MII_BMCR to force speed/duplex |
00db8189 | 1989 | * to the values in phydev. Assumes that the values are valid. |
b3df0da8 RD |
1990 | * Please see phy_sanitize_settings(). |
1991 | */ | |
3fb69bca | 1992 | int genphy_setup_forced(struct phy_device *phydev) |
00db8189 | 1993 | { |
f28a602b | 1994 | u16 ctl; |
00db8189 | 1995 | |
2f53e904 SS |
1996 | phydev->pause = 0; |
1997 | phydev->asym_pause = 0; | |
00db8189 | 1998 | |
f28a602b | 1999 | ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex); |
e109374f | 2000 | |
fea23fb5 | 2001 | return phy_modify(phydev, MII_BMCR, |
18a5b052 | 2002 | ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl); |
00db8189 | 2003 | } |
3fb69bca | 2004 | EXPORT_SYMBOL(genphy_setup_forced); |
00db8189 | 2005 | |
bdbdac76 OR |
2006 | static int genphy_setup_master_slave(struct phy_device *phydev) |
2007 | { | |
2008 | u16 ctl = 0; | |
2009 | ||
2010 | if (!phydev->is_gigabit_capable) | |
2011 | return 0; | |
2012 | ||
2013 | switch (phydev->master_slave_set) { | |
2014 | case MASTER_SLAVE_CFG_MASTER_PREFERRED: | |
2015 | ctl |= CTL1000_PREFER_MASTER; | |
2016 | break; | |
2017 | case MASTER_SLAVE_CFG_SLAVE_PREFERRED: | |
2018 | break; | |
2019 | case MASTER_SLAVE_CFG_MASTER_FORCE: | |
2020 | ctl |= CTL1000_AS_MASTER; | |
df561f66 | 2021 | fallthrough; |
bdbdac76 OR |
2022 | case MASTER_SLAVE_CFG_SLAVE_FORCE: |
2023 | ctl |= CTL1000_ENABLE_MASTER; | |
2024 | break; | |
2025 | case MASTER_SLAVE_CFG_UNKNOWN: | |
2026 | case MASTER_SLAVE_CFG_UNSUPPORTED: | |
2027 | return 0; | |
2028 | default: | |
2029 | phydev_warn(phydev, "Unsupported Master/Slave mode\n"); | |
2030 | return -EOPNOTSUPP; | |
2031 | } | |
2032 | ||
2033 | return phy_modify_changed(phydev, MII_CTRL1000, | |
2034 | (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER | | |
2035 | CTL1000_PREFER_MASTER), ctl); | |
2036 | } | |
2037 | ||
64807c23 | 2038 | int genphy_read_master_slave(struct phy_device *phydev) |
bdbdac76 OR |
2039 | { |
2040 | int cfg, state; | |
3a13f98b | 2041 | int val; |
bdbdac76 | 2042 | |
bdbdac76 OR |
2043 | phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN; |
2044 | phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN; | |
2045 | ||
2046 | val = phy_read(phydev, MII_CTRL1000); | |
2047 | if (val < 0) | |
2048 | return val; | |
2049 | ||
2050 | if (val & CTL1000_ENABLE_MASTER) { | |
2051 | if (val & CTL1000_AS_MASTER) | |
2052 | cfg = MASTER_SLAVE_CFG_MASTER_FORCE; | |
2053 | else | |
2054 | cfg = MASTER_SLAVE_CFG_SLAVE_FORCE; | |
2055 | } else { | |
2056 | if (val & CTL1000_PREFER_MASTER) | |
2057 | cfg = MASTER_SLAVE_CFG_MASTER_PREFERRED; | |
2058 | else | |
2059 | cfg = MASTER_SLAVE_CFG_SLAVE_PREFERRED; | |
2060 | } | |
2061 | ||
2062 | val = phy_read(phydev, MII_STAT1000); | |
2063 | if (val < 0) | |
2064 | return val; | |
2065 | ||
2066 | if (val & LPA_1000MSFAIL) { | |
2067 | state = MASTER_SLAVE_STATE_ERR; | |
2068 | } else if (phydev->link) { | |
2069 | /* this bits are valid only for active link */ | |
2070 | if (val & LPA_1000MSRES) | |
2071 | state = MASTER_SLAVE_STATE_MASTER; | |
2072 | else | |
2073 | state = MASTER_SLAVE_STATE_SLAVE; | |
2074 | } else { | |
2075 | state = MASTER_SLAVE_STATE_UNKNOWN; | |
2076 | } | |
2077 | ||
2078 | phydev->master_slave_get = cfg; | |
2079 | phydev->master_slave_state = state; | |
2080 | ||
2081 | return 0; | |
2082 | } | |
64807c23 | 2083 | EXPORT_SYMBOL(genphy_read_master_slave); |
bdbdac76 | 2084 | |
b3df0da8 RD |
2085 | /** |
2086 | * genphy_restart_aneg - Enable and Restart Autonegotiation | |
2087 | * @phydev: target phy_device struct | |
2088 | */ | |
00db8189 AF |
2089 | int genphy_restart_aneg(struct phy_device *phydev) |
2090 | { | |
00db8189 | 2091 | /* Don't isolate the PHY if we're negotiating */ |
f102852f | 2092 | return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, |
fea23fb5 | 2093 | BMCR_ANENABLE | BMCR_ANRESTART); |
00db8189 | 2094 | } |
892871dc | 2095 | EXPORT_SYMBOL(genphy_restart_aneg); |
00db8189 | 2096 | |
2a10ab04 RK |
2097 | /** |
2098 | * genphy_check_and_restart_aneg - Enable and restart auto-negotiation | |
2099 | * @phydev: target phy_device struct | |
2100 | * @restart: whether aneg restart is requested | |
2101 | * | |
2102 | * Check, and restart auto-negotiation if needed. | |
2103 | */ | |
2104 | int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart) | |
2105 | { | |
4f31c532 | 2106 | int ret; |
2a10ab04 RK |
2107 | |
2108 | if (!restart) { | |
2109 | /* Advertisement hasn't changed, but maybe aneg was never on to | |
2110 | * begin with? Or maybe phy was isolated? | |
2111 | */ | |
2112 | ret = phy_read(phydev, MII_BMCR); | |
2113 | if (ret < 0) | |
2114 | return ret; | |
2115 | ||
2116 | if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE)) | |
2117 | restart = true; | |
2118 | } | |
2119 | ||
2120 | if (restart) | |
4f31c532 | 2121 | return genphy_restart_aneg(phydev); |
2a10ab04 | 2122 | |
4f31c532 | 2123 | return 0; |
2a10ab04 RK |
2124 | } |
2125 | EXPORT_SYMBOL(genphy_check_and_restart_aneg); | |
2126 | ||
b3df0da8 | 2127 | /** |
f4069cd7 | 2128 | * __genphy_config_aneg - restart auto-negotiation or write BMCR |
b3df0da8 | 2129 | * @phydev: target phy_device struct |
f4069cd7 | 2130 | * @changed: whether autoneg is requested |
00db8189 | 2131 | * |
b3df0da8 | 2132 | * Description: If auto-negotiation is enabled, we configure the |
00db8189 | 2133 | * advertising, and then restart auto-negotiation. If it is not |
b3df0da8 | 2134 | * enabled, then we write the BMCR. |
00db8189 | 2135 | */ |
f4069cd7 | 2136 | int __genphy_config_aneg(struct phy_device *phydev, bool changed) |
00db8189 | 2137 | { |
6ff3cddc | 2138 | __ETHTOOL_DECLARE_LINK_MODE_MASK(fixed_advert); |
c7ae89c6 | 2139 | const struct link_capabilities *c; |
6ff3cddc | 2140 | unsigned long *advert; |
f4069cd7 | 2141 | int err; |
d853d145 | 2142 | |
b6478b8c | 2143 | err = genphy_c45_an_config_eee_aneg(phydev); |
9b01c885 OR |
2144 | if (err < 0) |
2145 | return err; | |
2146 | else if (err) | |
f4069cd7 | 2147 | changed = true; |
00db8189 | 2148 | |
bdbdac76 OR |
2149 | err = genphy_setup_master_slave(phydev); |
2150 | if (err < 0) | |
2151 | return err; | |
2152 | else if (err) | |
2153 | changed = true; | |
2154 | ||
6ff3cddc RKO |
2155 | if (phydev->autoneg == AUTONEG_ENABLE) { |
2156 | /* Only allow advertising what this PHY supports */ | |
2157 | linkmode_and(phydev->advertising, phydev->advertising, | |
2158 | phydev->supported); | |
2159 | advert = phydev->advertising; | |
2160 | } else if (phydev->speed < SPEED_1000) { | |
de339c2a | 2161 | return genphy_setup_forced(phydev); |
6ff3cddc RKO |
2162 | } else { |
2163 | linkmode_zero(fixed_advert); | |
2164 | ||
c7ae89c6 MC |
2165 | c = phy_caps_lookup(phydev->speed, phydev->duplex, |
2166 | phydev->supported, true); | |
2167 | if (c) | |
2168 | linkmode_and(fixed_advert, phydev->supported, | |
2169 | c->linkmodes); | |
6ff3cddc RKO |
2170 | |
2171 | advert = fixed_advert; | |
2172 | } | |
00db8189 | 2173 | |
6ff3cddc | 2174 | err = genphy_config_advert(phydev, advert); |
d853d145 | 2175 | if (err < 0) /* error */ |
2176 | return err; | |
f4069cd7 HK |
2177 | else if (err) |
2178 | changed = true; | |
d853d145 | 2179 | |
2a10ab04 | 2180 | return genphy_check_and_restart_aneg(phydev, changed); |
00db8189 | 2181 | } |
f4069cd7 | 2182 | EXPORT_SYMBOL(__genphy_config_aneg); |
00db8189 | 2183 | |
fa6e98ce HK |
2184 | /** |
2185 | * genphy_c37_config_aneg - restart auto-negotiation or write BMCR | |
2186 | * @phydev: target phy_device struct | |
2187 | * | |
2188 | * Description: If auto-negotiation is enabled, we configure the | |
2189 | * advertising, and then restart auto-negotiation. If it is not | |
2190 | * enabled, then we write the BMCR. This function is intended | |
2191 | * for use with Clause 37 1000Base-X mode. | |
2192 | */ | |
2193 | int genphy_c37_config_aneg(struct phy_device *phydev) | |
2194 | { | |
2195 | int err, changed; | |
2196 | ||
2197 | if (phydev->autoneg != AUTONEG_ENABLE) | |
2198 | return genphy_setup_forced(phydev); | |
2199 | ||
2200 | err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100, | |
2201 | BMCR_SPEED1000); | |
2202 | if (err) | |
2203 | return err; | |
2204 | ||
2205 | changed = genphy_c37_config_advert(phydev); | |
2206 | if (changed < 0) /* error */ | |
2207 | return changed; | |
2208 | ||
2209 | if (!changed) { | |
2210 | /* Advertisement hasn't changed, but maybe aneg was never on to | |
2211 | * begin with? Or maybe phy was isolated? | |
2212 | */ | |
2213 | int ctl = phy_read(phydev, MII_BMCR); | |
2214 | ||
2215 | if (ctl < 0) | |
2216 | return ctl; | |
2217 | ||
2218 | if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) | |
2219 | changed = 1; /* do restart aneg */ | |
2220 | } | |
2221 | ||
2222 | /* Only restart aneg if we are advertising something different | |
2223 | * than we were before. | |
2224 | */ | |
2225 | if (changed > 0) | |
2226 | return genphy_restart_aneg(phydev); | |
2227 | ||
2228 | return 0; | |
2229 | } | |
2230 | EXPORT_SYMBOL(genphy_c37_config_aneg); | |
2231 | ||
a9fa6e6a FF |
2232 | /** |
2233 | * genphy_aneg_done - return auto-negotiation status | |
2234 | * @phydev: target phy_device struct | |
2235 | * | |
2236 | * Description: Reads the status register and returns 0 either if | |
2237 | * auto-negotiation is incomplete, or if there was an error. | |
2238 | * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done. | |
2239 | */ | |
2240 | int genphy_aneg_done(struct phy_device *phydev) | |
2241 | { | |
2242 | int retval = phy_read(phydev, MII_BMSR); | |
2243 | ||
2244 | return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE); | |
2245 | } | |
2246 | EXPORT_SYMBOL(genphy_aneg_done); | |
2247 | ||
b3df0da8 RD |
2248 | /** |
2249 | * genphy_update_link - update link status in @phydev | |
2250 | * @phydev: target phy_device struct | |
00db8189 | 2251 | * |
b3df0da8 | 2252 | * Description: Update the value in phydev->link to reflect the |
00db8189 | 2253 | * current link value. In order to do this, we need to read |
b3df0da8 | 2254 | * the status register twice, keeping the second value. |
00db8189 AF |
2255 | */ |
2256 | int genphy_update_link(struct phy_device *phydev) | |
2257 | { | |
c36757eb HK |
2258 | int status = 0, bmcr; |
2259 | ||
2260 | bmcr = phy_read(phydev, MII_BMCR); | |
2261 | if (bmcr < 0) | |
2262 | return bmcr; | |
2263 | ||
2264 | /* Autoneg is being started, therefore disregard BMSR value and | |
2265 | * report link as down. | |
2266 | */ | |
2267 | if (bmcr & BMCR_ANRESTART) | |
2268 | goto done; | |
00db8189 | 2269 | |
93c09704 HK |
2270 | /* The link state is latched low so that momentary link |
2271 | * drops can be detected. Do not double-read the status | |
e96bd2d3 PO |
2272 | * in polling mode to detect such short link drops except |
2273 | * the link was already down. | |
93c09704 | 2274 | */ |
e96bd2d3 | 2275 | if (!phy_polling_mode(phydev) || !phydev->link) { |
93c09704 | 2276 | status = phy_read(phydev, MII_BMSR); |
b7f29f8c | 2277 | if (status < 0) |
93c09704 | 2278 | return status; |
b7f29f8c HK |
2279 | else if (status & BMSR_LSTATUS) |
2280 | goto done; | |
93c09704 | 2281 | } |
00db8189 AF |
2282 | |
2283 | /* Read link and autonegotiation status */ | |
2284 | status = phy_read(phydev, MII_BMSR); | |
00db8189 AF |
2285 | if (status < 0) |
2286 | return status; | |
b7f29f8c | 2287 | done: |
4950c2ba HK |
2288 | phydev->link = status & BMSR_LSTATUS ? 1 : 0; |
2289 | phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0; | |
00db8189 | 2290 | |
aa6b1956 HK |
2291 | /* Consider the case that autoneg was started and "aneg complete" |
2292 | * bit has been reset, but "link up" bit not yet. | |
2293 | */ | |
2294 | if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete) | |
2295 | phydev->link = 0; | |
2296 | ||
00db8189 AF |
2297 | return 0; |
2298 | } | |
6b655529 | 2299 | EXPORT_SYMBOL(genphy_update_link); |
00db8189 | 2300 | |
8d3dc3ac | 2301 | int genphy_read_lpa(struct phy_device *phydev) |
00db8189 | 2302 | { |
8d3dc3ac | 2303 | int lpa, lpagb; |
b6163f19 | 2304 | |
3de5ae54 YL |
2305 | if (phydev->autoneg == AUTONEG_ENABLE) { |
2306 | if (!phydev->autoneg_complete) { | |
2307 | mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, | |
2308 | 0); | |
2309 | mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0); | |
2310 | return 0; | |
2311 | } | |
2312 | ||
3b8b11f9 | 2313 | if (phydev->is_gigabit_capable) { |
00db8189 | 2314 | lpagb = phy_read(phydev, MII_STAT1000); |
00db8189 AF |
2315 | if (lpagb < 0) |
2316 | return lpagb; | |
2317 | ||
b8f8c8eb | 2318 | if (lpagb & LPA_1000MSFAIL) { |
916e571e HK |
2319 | int adv = phy_read(phydev, MII_CTRL1000); |
2320 | ||
2321 | if (adv < 0) | |
2322 | return adv; | |
2323 | ||
b8f8c8eb HK |
2324 | if (adv & CTL1000_ENABLE_MASTER) |
2325 | phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n"); | |
2326 | else | |
2327 | phydev_err(phydev, "Master/Slave resolution failed\n"); | |
2328 | return -ENOLINK; | |
2329 | } | |
2330 | ||
78a24df3 AL |
2331 | mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, |
2332 | lpagb); | |
00db8189 AF |
2333 | } |
2334 | ||
2335 | lpa = phy_read(phydev, MII_LPA); | |
00db8189 AF |
2336 | if (lpa < 0) |
2337 | return lpa; | |
2338 | ||
d3351931 | 2339 | mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa); |
3de5ae54 YL |
2340 | } else { |
2341 | linkmode_zero(phydev->lp_advertising); | |
8d3dc3ac RK |
2342 | } |
2343 | ||
2344 | return 0; | |
2345 | } | |
2346 | EXPORT_SYMBOL(genphy_read_lpa); | |
2347 | ||
0efc286a RK |
2348 | /** |
2349 | * genphy_read_status_fixed - read the link parameters for !aneg mode | |
2350 | * @phydev: target phy_device struct | |
2351 | * | |
2352 | * Read the current duplex and speed state for a PHY operating with | |
2353 | * autonegotiation disabled. | |
2354 | */ | |
2355 | int genphy_read_status_fixed(struct phy_device *phydev) | |
2356 | { | |
2357 | int bmcr = phy_read(phydev, MII_BMCR); | |
2358 | ||
2359 | if (bmcr < 0) | |
2360 | return bmcr; | |
2361 | ||
2362 | if (bmcr & BMCR_FULLDPLX) | |
2363 | phydev->duplex = DUPLEX_FULL; | |
2364 | else | |
2365 | phydev->duplex = DUPLEX_HALF; | |
2366 | ||
2367 | if (bmcr & BMCR_SPEED1000) | |
2368 | phydev->speed = SPEED_1000; | |
2369 | else if (bmcr & BMCR_SPEED100) | |
2370 | phydev->speed = SPEED_100; | |
2371 | else | |
2372 | phydev->speed = SPEED_10; | |
2373 | ||
2374 | return 0; | |
2375 | } | |
2376 | EXPORT_SYMBOL(genphy_read_status_fixed); | |
2377 | ||
8d3dc3ac RK |
2378 | /** |
2379 | * genphy_read_status - check the link status and update current link state | |
2380 | * @phydev: target phy_device struct | |
2381 | * | |
2382 | * Description: Check the link, then figure out the current state | |
2383 | * by comparing what we advertise with what the link partner | |
2384 | * advertises. Start by checking the gigabit possibilities, | |
2385 | * then move on to 10/100. | |
2386 | */ | |
2387 | int genphy_read_status(struct phy_device *phydev) | |
2388 | { | |
2389 | int err, old_link = phydev->link; | |
2390 | ||
2391 | /* Update the link, but return if there was an error */ | |
2392 | err = genphy_update_link(phydev); | |
2393 | if (err) | |
2394 | return err; | |
2395 | ||
2396 | /* why bother the PHY if nothing can have changed */ | |
2397 | if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) | |
2398 | return 0; | |
2399 | ||
64807c23 AR |
2400 | phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED; |
2401 | phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED; | |
8d3dc3ac RK |
2402 | phydev->speed = SPEED_UNKNOWN; |
2403 | phydev->duplex = DUPLEX_UNKNOWN; | |
2404 | phydev->pause = 0; | |
2405 | phydev->asym_pause = 0; | |
2406 | ||
64807c23 AR |
2407 | if (phydev->is_gigabit_capable) { |
2408 | err = genphy_read_master_slave(phydev); | |
2409 | if (err < 0) | |
2410 | return err; | |
2411 | } | |
bdbdac76 | 2412 | |
8d3dc3ac RK |
2413 | err = genphy_read_lpa(phydev); |
2414 | if (err < 0) | |
2415 | return err; | |
2416 | ||
2417 | if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) { | |
5502b218 | 2418 | phy_resolve_aneg_linkmode(phydev); |
b6163f19 | 2419 | } else if (phydev->autoneg == AUTONEG_DISABLE) { |
0efc286a RK |
2420 | err = genphy_read_status_fixed(phydev); |
2421 | if (err < 0) | |
2422 | return err; | |
00db8189 AF |
2423 | } |
2424 | ||
2425 | return 0; | |
2426 | } | |
2427 | EXPORT_SYMBOL(genphy_read_status); | |
2428 | ||
fa6e98ce HK |
2429 | /** |
2430 | * genphy_c37_read_status - check the link status and update current link state | |
2431 | * @phydev: target phy_device struct | |
9b1d5e05 | 2432 | * @changed: pointer where to store if link changed |
fa6e98ce HK |
2433 | * |
2434 | * Description: Check the link, then figure out the current state | |
2435 | * by comparing what we advertise with what the link partner | |
2436 | * advertises. This function is for Clause 37 1000Base-X mode. | |
9b1d5e05 CM |
2437 | * |
2438 | * If link has changed, @changed is set to true, false otherwise. | |
fa6e98ce | 2439 | */ |
9b1d5e05 | 2440 | int genphy_c37_read_status(struct phy_device *phydev, bool *changed) |
fa6e98ce HK |
2441 | { |
2442 | int lpa, err, old_link = phydev->link; | |
2443 | ||
2444 | /* Update the link, but return if there was an error */ | |
2445 | err = genphy_update_link(phydev); | |
2446 | if (err) | |
2447 | return err; | |
2448 | ||
2449 | /* why bother the PHY if nothing can have changed */ | |
9b1d5e05 CM |
2450 | if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) { |
2451 | *changed = false; | |
fa6e98ce | 2452 | return 0; |
9b1d5e05 | 2453 | } |
fa6e98ce | 2454 | |
9b1d5e05 CM |
2455 | /* Signal link has changed */ |
2456 | *changed = true; | |
fa6e98ce HK |
2457 | phydev->duplex = DUPLEX_UNKNOWN; |
2458 | phydev->pause = 0; | |
2459 | phydev->asym_pause = 0; | |
2460 | ||
2461 | if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) { | |
2462 | lpa = phy_read(phydev, MII_LPA); | |
2463 | if (lpa < 0) | |
2464 | return lpa; | |
2465 | ||
2466 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, | |
2467 | phydev->lp_advertising, lpa & LPA_LPACK); | |
2468 | linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, | |
2469 | phydev->lp_advertising, lpa & LPA_1000XFULL); | |
2470 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, | |
2471 | phydev->lp_advertising, lpa & LPA_1000XPAUSE); | |
2472 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, | |
2473 | phydev->lp_advertising, | |
2474 | lpa & LPA_1000XPAUSE_ASYM); | |
2475 | ||
2476 | phy_resolve_aneg_linkmode(phydev); | |
2477 | } else if (phydev->autoneg == AUTONEG_DISABLE) { | |
2478 | int bmcr = phy_read(phydev, MII_BMCR); | |
2479 | ||
2480 | if (bmcr < 0) | |
2481 | return bmcr; | |
2482 | ||
2483 | if (bmcr & BMCR_FULLDPLX) | |
2484 | phydev->duplex = DUPLEX_FULL; | |
2485 | else | |
2486 | phydev->duplex = DUPLEX_HALF; | |
2487 | } | |
2488 | ||
2489 | return 0; | |
2490 | } | |
2491 | EXPORT_SYMBOL(genphy_c37_read_status); | |
2492 | ||
797ac071 FF |
2493 | /** |
2494 | * genphy_soft_reset - software reset the PHY via BMCR_RESET bit | |
2495 | * @phydev: target phy_device struct | |
2496 | * | |
2497 | * Description: Perform a software PHY reset using the standard | |
2498 | * BMCR_RESET bit and poll for the reset bit to be cleared. | |
2499 | * | |
2500 | * Returns: 0 on success, < 0 on failure | |
2501 | */ | |
2502 | int genphy_soft_reset(struct phy_device *phydev) | |
2503 | { | |
8c90b795 | 2504 | u16 res = BMCR_RESET; |
797ac071 FF |
2505 | int ret; |
2506 | ||
8c90b795 HK |
2507 | if (phydev->autoneg == AUTONEG_ENABLE) |
2508 | res |= BMCR_ANRESTART; | |
2509 | ||
2510 | ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res); | |
797ac071 FF |
2511 | if (ret < 0) |
2512 | return ret; | |
2513 | ||
9576e9fa HK |
2514 | /* Clause 22 states that setting bit BMCR_RESET sets control registers |
2515 | * to their default value. Therefore the POWER DOWN bit is supposed to | |
2516 | * be cleared after soft reset. | |
2517 | */ | |
2518 | phydev->suspended = 0; | |
2519 | ||
8c90b795 HK |
2520 | ret = phy_poll_reset(phydev); |
2521 | if (ret) | |
2522 | return ret; | |
2523 | ||
2524 | /* BMCR may be reset to defaults */ | |
2525 | if (phydev->autoneg == AUTONEG_DISABLE) | |
2526 | ret = genphy_setup_forced(phydev); | |
2527 | ||
2528 | return ret; | |
797ac071 FF |
2529 | } |
2530 | EXPORT_SYMBOL(genphy_soft_reset); | |
2531 | ||
87de1f05 IC |
2532 | irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev) |
2533 | { | |
2534 | /* It seems there are cases where the interrupts are handled by another | |
2535 | * entity (ie an IRQ controller embedded inside the PHY) and do not | |
2536 | * need any other interraction from phylib. In this case, just trigger | |
2537 | * the state machine directly. | |
2538 | */ | |
2539 | phy_trigger_machine(phydev); | |
2540 | ||
2541 | return 0; | |
2542 | } | |
2543 | EXPORT_SYMBOL(genphy_handle_interrupt_no_ack); | |
2544 | ||
045925e3 HK |
2545 | /** |
2546 | * genphy_read_abilities - read PHY abilities from Clause 22 registers | |
2547 | * @phydev: target phy_device struct | |
2548 | * | |
2549 | * Description: Reads the PHY's abilities and populates | |
2550 | * phydev->supported accordingly. | |
2551 | * | |
2552 | * Returns: 0 on success, < 0 on failure | |
2553 | */ | |
2554 | int genphy_read_abilities(struct phy_device *phydev) | |
2555 | { | |
2556 | int val; | |
2557 | ||
2558 | linkmode_set_bit_array(phy_basic_ports_array, | |
2559 | ARRAY_SIZE(phy_basic_ports_array), | |
2560 | phydev->supported); | |
2561 | ||
2562 | val = phy_read(phydev, MII_BMSR); | |
2563 | if (val < 0) | |
2564 | return val; | |
2565 | ||
2566 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported, | |
2567 | val & BMSR_ANEGCAPABLE); | |
2568 | ||
2569 | linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported, | |
2570 | val & BMSR_100FULL); | |
2571 | linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported, | |
2572 | val & BMSR_100HALF); | |
2573 | linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported, | |
2574 | val & BMSR_10FULL); | |
2575 | linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported, | |
2576 | val & BMSR_10HALF); | |
2577 | ||
2578 | if (val & BMSR_ESTATEN) { | |
2579 | val = phy_read(phydev, MII_ESTATUS); | |
2580 | if (val < 0) | |
2581 | return val; | |
2582 | ||
2583 | linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, | |
2584 | phydev->supported, val & ESTATUS_1000_TFULL); | |
2585 | linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, | |
2586 | phydev->supported, val & ESTATUS_1000_THALF); | |
f30e33bc RH |
2587 | linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, |
2588 | phydev->supported, val & ESTATUS_1000_XFULL); | |
045925e3 HK |
2589 | } |
2590 | ||
9b01c885 OR |
2591 | /* This is optional functionality. If not supported, we may get an error |
2592 | * which should be ignored. | |
2593 | */ | |
2594 | genphy_c45_read_eee_abilities(phydev); | |
2595 | ||
045925e3 HK |
2596 | return 0; |
2597 | } | |
2598 | EXPORT_SYMBOL(genphy_read_abilities); | |
2599 | ||
5df7af85 KH |
2600 | /* This is used for the phy device which doesn't support the MMD extended |
2601 | * register access, but it does have side effect when we are trying to access | |
2602 | * the MMD register via indirect method. | |
2603 | */ | |
2604 | int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum) | |
2605 | { | |
2606 | return -EOPNOTSUPP; | |
2607 | } | |
2608 | EXPORT_SYMBOL(genphy_read_mmd_unsupported); | |
2609 | ||
2610 | int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum, | |
2611 | u16 regnum, u16 val) | |
2612 | { | |
2613 | return -EOPNOTSUPP; | |
2614 | } | |
2615 | EXPORT_SYMBOL(genphy_write_mmd_unsupported); | |
2616 | ||
0f0ca340 GC |
2617 | int genphy_suspend(struct phy_device *phydev) |
2618 | { | |
032f4700 | 2619 | return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN); |
0f0ca340 GC |
2620 | } |
2621 | EXPORT_SYMBOL(genphy_suspend); | |
00db8189 | 2622 | |
0f0ca340 GC |
2623 | int genphy_resume(struct phy_device *phydev) |
2624 | { | |
032f4700 | 2625 | return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN); |
0f0ca340 GC |
2626 | } |
2627 | EXPORT_SYMBOL(genphy_resume); | |
00db8189 | 2628 | |
45456e38 | 2629 | int genphy_loopback(struct phy_device *phydev, bool enable, int speed) |
f0f9b4ed | 2630 | { |
014068dc | 2631 | if (enable) { |
32fa4366 NK |
2632 | u16 ctl = BMCR_LOOPBACK; |
2633 | int ret, val; | |
014068dc | 2634 | |
45456e38 GE |
2635 | if (speed == SPEED_10 || speed == SPEED_100 || |
2636 | speed == SPEED_1000) | |
2637 | phydev->speed = speed; | |
2638 | else if (speed) | |
2639 | return -EINVAL; | |
2640 | ||
f28a602b | 2641 | ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex); |
014068dc OR |
2642 | |
2643 | phy_modify(phydev, MII_BMCR, ~0, ctl); | |
2644 | ||
2645 | ret = phy_read_poll_timeout(phydev, MII_BMSR, val, | |
2646 | val & BMSR_LSTATUS, | |
2647 | 5000, 500000, true); | |
2648 | if (ret) | |
2649 | return ret; | |
2650 | } else { | |
2651 | phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, 0); | |
2652 | ||
2653 | phy_config_aneg(phydev); | |
2654 | } | |
2655 | ||
2656 | return 0; | |
f0f9b4ed LYS |
2657 | } |
2658 | EXPORT_SYMBOL(genphy_loopback); | |
2659 | ||
41124fa6 AL |
2660 | /** |
2661 | * phy_remove_link_mode - Remove a supported link mode | |
2662 | * @phydev: phy_device structure to remove link mode from | |
2663 | * @link_mode: Link mode to be removed | |
2664 | * | |
2665 | * Description: Some MACs don't support all link modes which the PHY | |
2666 | * does. e.g. a 1G MAC often does not support 1000Half. Add a helper | |
2667 | * to remove a link mode. | |
2668 | */ | |
2669 | void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode) | |
2670 | { | |
3c1bcc86 | 2671 | linkmode_clear_bit(link_mode, phydev->supported); |
22c0ef6b | 2672 | phy_advertise_supported(phydev); |
41124fa6 AL |
2673 | } |
2674 | EXPORT_SYMBOL(phy_remove_link_mode); | |
2675 | ||
22c0ef6b HK |
2676 | static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src) |
2677 | { | |
2678 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst, | |
2679 | linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src)); | |
2680 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst, | |
2681 | linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src)); | |
2682 | } | |
2683 | ||
2684 | /** | |
2685 | * phy_advertise_supported - Advertise all supported modes | |
2686 | * @phydev: target phy_device struct | |
2687 | * | |
2688 | * Description: Called to advertise all supported modes, doesn't touch | |
2689 | * pause mode advertising. | |
2690 | */ | |
2691 | void phy_advertise_supported(struct phy_device *phydev) | |
2692 | { | |
2693 | __ETHTOOL_DECLARE_LINK_MODE_MASK(new); | |
2694 | ||
2695 | linkmode_copy(new, phydev->supported); | |
2696 | phy_copy_pause_bits(new, phydev->advertising); | |
2697 | linkmode_copy(phydev->advertising, new); | |
2698 | } | |
2699 | EXPORT_SYMBOL(phy_advertise_supported); | |
2700 | ||
b6469127 HK |
2701 | /** |
2702 | * phy_advertise_eee_all - Advertise all supported EEE modes | |
2703 | * @phydev: target phy_device struct | |
2704 | * | |
2705 | * Description: Per default phylib preserves the EEE advertising at the time of | |
2706 | * phy probing, which might be a subset of the supported EEE modes. Use this | |
2707 | * function when all supported EEE modes should be advertised. This does not | |
2708 | * trigger auto-negotiation, so must be called before phy_start()/ | |
2709 | * phylink_start() which will start auto-negotiation. | |
2710 | */ | |
2711 | void phy_advertise_eee_all(struct phy_device *phydev) | |
2712 | { | |
2713 | linkmode_copy(phydev->advertising_eee, phydev->supported_eee); | |
2714 | } | |
2715 | EXPORT_SYMBOL_GPL(phy_advertise_eee_all); | |
2716 | ||
49168d19 AL |
2717 | /** |
2718 | * phy_support_eee - Set initial EEE policy configuration | |
2719 | * @phydev: Target phy_device struct | |
2720 | * | |
2721 | * This function configures the initial policy for Energy Efficient Ethernet | |
2722 | * (EEE) on the specified PHY device, influencing that EEE capabilities are | |
2723 | * advertised before the link is established. It should be called during PHY | |
2724 | * registration by the MAC driver and/or the PHY driver (for SmartEEE PHYs) | |
2725 | * if MAC supports LPI or PHY is capable to compensate missing LPI functionality | |
2726 | * of the MAC. | |
2727 | * | |
2728 | * The function sets default EEE policy parameters, including preparing the PHY | |
2729 | * to advertise EEE capabilities based on hardware support. | |
2730 | * | |
2731 | * It also sets the expected configuration for Low Power Idle (LPI) in the MAC | |
2732 | * driver. If the PHY framework determines that both local and remote | |
2733 | * advertisements support EEE, and the negotiated link mode is compatible with | |
2734 | * EEE, it will set enable_tx_lpi = true. The MAC driver is expected to act on | |
2735 | * this setting by enabling the LPI timer if enable_tx_lpi is set. | |
2736 | */ | |
2737 | void phy_support_eee(struct phy_device *phydev) | |
2738 | { | |
2739 | linkmode_copy(phydev->advertising_eee, phydev->supported_eee); | |
2740 | phydev->eee_cfg.tx_lpi_enabled = true; | |
2741 | phydev->eee_cfg.eee_enabled = true; | |
2742 | } | |
2743 | EXPORT_SYMBOL(phy_support_eee); | |
2744 | ||
b55498ff HK |
2745 | /** |
2746 | * phy_disable_eee - Disable EEE for the PHY | |
2747 | * @phydev: Target phy_device struct | |
2748 | * | |
2749 | * This function is used by MAC drivers for MAC's which don't support EEE. | |
2750 | * It disables EEE on the PHY layer. | |
2751 | */ | |
2752 | void phy_disable_eee(struct phy_device *phydev) | |
2753 | { | |
b55498ff HK |
2754 | linkmode_zero(phydev->advertising_eee); |
2755 | phydev->eee_cfg.tx_lpi_enabled = false; | |
2756 | phydev->eee_cfg.eee_enabled = false; | |
c83ca5a4 | 2757 | /* don't let userspace re-enable EEE advertisement */ |
8eb0d381 | 2758 | linkmode_fill(phydev->eee_disabled_modes); |
b55498ff HK |
2759 | } |
2760 | EXPORT_SYMBOL_GPL(phy_disable_eee); | |
2761 | ||
c306ad36 AL |
2762 | /** |
2763 | * phy_support_sym_pause - Enable support of symmetrical pause | |
2764 | * @phydev: target phy_device struct | |
2765 | * | |
2766 | * Description: Called by the MAC to indicate is supports symmetrical | |
2767 | * Pause, but not asym pause. | |
2768 | */ | |
2769 | void phy_support_sym_pause(struct phy_device *phydev) | |
2770 | { | |
3c1bcc86 | 2771 | linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported); |
22c0ef6b | 2772 | phy_copy_pause_bits(phydev->advertising, phydev->supported); |
c306ad36 AL |
2773 | } |
2774 | EXPORT_SYMBOL(phy_support_sym_pause); | |
2775 | ||
af8d9bb2 AL |
2776 | /** |
2777 | * phy_support_asym_pause - Enable support of asym pause | |
2778 | * @phydev: target phy_device struct | |
2779 | * | |
2780 | * Description: Called by the MAC to indicate is supports Asym Pause. | |
2781 | */ | |
2782 | void phy_support_asym_pause(struct phy_device *phydev) | |
2783 | { | |
22c0ef6b | 2784 | phy_copy_pause_bits(phydev->advertising, phydev->supported); |
af8d9bb2 AL |
2785 | } |
2786 | EXPORT_SYMBOL(phy_support_asym_pause); | |
2787 | ||
0c122405 AL |
2788 | /** |
2789 | * phy_set_sym_pause - Configure symmetric Pause | |
2790 | * @phydev: target phy_device struct | |
2791 | * @rx: Receiver Pause is supported | |
2792 | * @tx: Transmit Pause is supported | |
2793 | * @autoneg: Auto neg should be used | |
2794 | * | |
2795 | * Description: Configure advertised Pause support depending on if | |
2796 | * receiver pause and pause auto neg is supported. Generally called | |
2797 | * from the set_pauseparam .ndo. | |
2798 | */ | |
2799 | void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx, | |
2800 | bool autoneg) | |
2801 | { | |
3c1bcc86 | 2802 | linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); |
0c122405 AL |
2803 | |
2804 | if (rx && tx && autoneg) | |
3c1bcc86 AL |
2805 | linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
2806 | phydev->supported); | |
0c122405 | 2807 | |
3c1bcc86 | 2808 | linkmode_copy(phydev->advertising, phydev->supported); |
0c122405 AL |
2809 | } |
2810 | EXPORT_SYMBOL(phy_set_sym_pause); | |
2811 | ||
70814e81 AL |
2812 | /** |
2813 | * phy_set_asym_pause - Configure Pause and Asym Pause | |
2814 | * @phydev: target phy_device struct | |
2815 | * @rx: Receiver Pause is supported | |
2816 | * @tx: Transmit Pause is supported | |
2817 | * | |
2818 | * Description: Configure advertised Pause support depending on if | |
2819 | * transmit and receiver pause is supported. If there has been a | |
2820 | * change in adverting, trigger a new autoneg. Generally called from | |
2821 | * the set_pauseparam .ndo. | |
2822 | */ | |
2823 | void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx) | |
2824 | { | |
3c1bcc86 | 2825 | __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv); |
70814e81 | 2826 | |
3c1bcc86 | 2827 | linkmode_copy(oldadv, phydev->advertising); |
45c767fa | 2828 | linkmode_set_pause(phydev->advertising, tx, rx); |
3c1bcc86 AL |
2829 | |
2830 | if (!linkmode_equal(oldadv, phydev->advertising) && | |
2831 | phydev->autoneg) | |
2832 | phy_start_aneg(phydev); | |
70814e81 AL |
2833 | } |
2834 | EXPORT_SYMBOL(phy_set_asym_pause); | |
2835 | ||
22b7d299 AL |
2836 | /** |
2837 | * phy_validate_pause - Test if the PHY/MAC support the pause configuration | |
2838 | * @phydev: phy_device struct | |
2839 | * @pp: requested pause configuration | |
2840 | * | |
2841 | * Description: Test if the PHY/MAC combination supports the Pause | |
2842 | * configuration the user is requesting. Returns True if it is | |
2843 | * supported, false otherwise. | |
2844 | */ | |
2845 | bool phy_validate_pause(struct phy_device *phydev, | |
2846 | struct ethtool_pauseparam *pp) | |
2847 | { | |
3c1bcc86 | 2848 | if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
b4010af9 | 2849 | phydev->supported) && pp->rx_pause) |
22b7d299 | 2850 | return false; |
b4010af9 HK |
2851 | |
2852 | if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, | |
2853 | phydev->supported) && | |
2854 | pp->rx_pause != pp->tx_pause) | |
2855 | return false; | |
2856 | ||
22b7d299 AL |
2857 | return true; |
2858 | } | |
2859 | EXPORT_SYMBOL(phy_validate_pause); | |
2860 | ||
a87ae8a9 RK |
2861 | /** |
2862 | * phy_get_pause - resolve negotiated pause modes | |
2863 | * @phydev: phy_device struct | |
2864 | * @tx_pause: pointer to bool to indicate whether transmit pause should be | |
2865 | * enabled. | |
2866 | * @rx_pause: pointer to bool to indicate whether receive pause should be | |
2867 | * enabled. | |
2868 | * | |
2869 | * Resolve and return the flow control modes according to the negotiation | |
2870 | * result. This includes checking that we are operating in full duplex mode. | |
2871 | * See linkmode_resolve_pause() for further details. | |
2872 | */ | |
2873 | void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause) | |
2874 | { | |
2875 | if (phydev->duplex != DUPLEX_FULL) { | |
2876 | *tx_pause = false; | |
2877 | *rx_pause = false; | |
2878 | return; | |
2879 | } | |
2880 | ||
2881 | return linkmode_resolve_pause(phydev->advertising, | |
2882 | phydev->lp_advertising, | |
2883 | tx_pause, rx_pause); | |
2884 | } | |
2885 | EXPORT_SYMBOL(phy_get_pause); | |
2886 | ||
92252eec | 2887 | #if IS_ENABLED(CONFIG_OF_MDIO) |
961ee5ae | 2888 | static int phy_get_u32_property(struct device *dev, const char *name, u32 *val) |
92252eec | 2889 | { |
961ee5ae | 2890 | return device_property_read_u32(dev, name, val); |
92252eec DM |
2891 | } |
2892 | #else | |
961ee5ae | 2893 | static int phy_get_u32_property(struct device *dev, const char *name, u32 *val) |
92252eec DM |
2894 | { |
2895 | return -EINVAL; | |
2896 | } | |
2897 | #endif | |
2898 | ||
2899 | /** | |
69280228 | 2900 | * phy_get_internal_delay - returns the index of the internal delay |
92252eec DM |
2901 | * @phydev: phy_device struct |
2902 | * @dev: pointer to the devices device struct | |
2903 | * @delay_values: array of delays the PHY supports | |
2904 | * @size: the size of the delay array | |
2905 | * @is_rx: boolean to indicate to get the rx internal delay | |
2906 | * | |
2907 | * Returns the index within the array of internal delay passed in. | |
2908 | * If the device property is not present then the interface type is checked | |
2909 | * if the interface defines use of internal delay then a 1 is returned otherwise | |
2910 | * a 0 is returned. | |
2911 | * The array must be in ascending order. If PHY does not have an ascending order | |
2912 | * array then size = 0 and the value of the delay property is returned. | |
2913 | * Return -EINVAL if the delay is invalid or cannot be found. | |
2914 | */ | |
2915 | s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev, | |
2916 | const int *delay_values, int size, bool is_rx) | |
2917 | { | |
961ee5ae DF |
2918 | int i, ret; |
2919 | u32 delay; | |
92252eec DM |
2920 | |
2921 | if (is_rx) { | |
961ee5ae DF |
2922 | ret = phy_get_u32_property(dev, "rx-internal-delay-ps", &delay); |
2923 | if (ret < 0 && size == 0) { | |
92252eec DM |
2924 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || |
2925 | phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) | |
2926 | return 1; | |
2927 | else | |
2928 | return 0; | |
2929 | } | |
2930 | ||
2931 | } else { | |
961ee5ae DF |
2932 | ret = phy_get_u32_property(dev, "tx-internal-delay-ps", &delay); |
2933 | if (ret < 0 && size == 0) { | |
92252eec DM |
2934 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || |
2935 | phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) | |
2936 | return 1; | |
2937 | else | |
2938 | return 0; | |
2939 | } | |
2940 | } | |
2941 | ||
961ee5ae DF |
2942 | if (ret < 0) |
2943 | return ret; | |
92252eec | 2944 | |
4469c0c5 | 2945 | if (size == 0) |
92252eec DM |
2946 | return delay; |
2947 | ||
2948 | if (delay < delay_values[0] || delay > delay_values[size - 1]) { | |
2949 | phydev_err(phydev, "Delay %d is out of range\n", delay); | |
2950 | return -EINVAL; | |
2951 | } | |
2952 | ||
2953 | if (delay == delay_values[0]) | |
2954 | return 0; | |
2955 | ||
2956 | for (i = 1; i < size; i++) { | |
2957 | if (delay == delay_values[i]) | |
2958 | return i; | |
2959 | ||
2960 | /* Find an approximate index by looking up the table */ | |
2961 | if (delay > delay_values[i - 1] && | |
2962 | delay < delay_values[i]) { | |
2963 | if (delay - delay_values[i - 1] < | |
2964 | delay_values[i] - delay) | |
2965 | return i - 1; | |
2966 | else | |
2967 | return i; | |
2968 | } | |
2969 | } | |
2970 | ||
2971 | phydev_err(phydev, "error finding internal delay index for %d\n", | |
2972 | delay); | |
2973 | ||
2974 | return -EINVAL; | |
2975 | } | |
2976 | EXPORT_SYMBOL(phy_get_internal_delay); | |
2977 | ||
961ee5ae DF |
2978 | /** |
2979 | * phy_get_tx_amplitude_gain - stores tx amplitude gain in @val | |
2980 | * @phydev: phy_device struct | |
2981 | * @dev: pointer to the devices device struct | |
2982 | * @linkmode: linkmode for which the tx amplitude gain should be retrieved | |
2983 | * @val: tx amplitude gain | |
2984 | * | |
2985 | * Returns: 0 on success, < 0 on failure | |
2986 | */ | |
2987 | int phy_get_tx_amplitude_gain(struct phy_device *phydev, struct device *dev, | |
2988 | enum ethtool_link_mode_bit_indices linkmode, | |
2989 | u32 *val) | |
2990 | { | |
2991 | switch (linkmode) { | |
2992 | case ETHTOOL_LINK_MODE_100baseT_Full_BIT: | |
2993 | return phy_get_u32_property(dev, | |
2994 | "tx-amplitude-100base-tx-percent", | |
2995 | val); | |
2996 | default: | |
2997 | return -EINVAL; | |
2998 | } | |
2999 | } | |
3000 | EXPORT_SYMBOL_GPL(phy_get_tx_amplitude_gain); | |
3001 | ||
145436ae DF |
3002 | /** |
3003 | * phy_get_mac_termination - stores MAC termination in @val | |
3004 | * @phydev: phy_device struct | |
3005 | * @dev: pointer to the devices device struct | |
3006 | * @val: MAC termination | |
3007 | * | |
3008 | * Returns: 0 on success, < 0 on failure | |
3009 | */ | |
3010 | int phy_get_mac_termination(struct phy_device *phydev, struct device *dev, | |
3011 | u32 *val) | |
3012 | { | |
3013 | return phy_get_u32_property(dev, "mac-termination-ohms", val); | |
3014 | } | |
3015 | EXPORT_SYMBOL_GPL(phy_get_mac_termination); | |
3016 | ||
01e5b728 AL |
3017 | static int phy_led_set_brightness(struct led_classdev *led_cdev, |
3018 | enum led_brightness value) | |
3019 | { | |
68481818 AL |
3020 | struct phy_led *phyled = to_phy_led(led_cdev); |
3021 | struct phy_device *phydev = phyled->phydev; | |
3022 | int err; | |
3023 | ||
3024 | mutex_lock(&phydev->lock); | |
3025 | err = phydev->drv->led_brightness_set(phydev, phyled->index, value); | |
3026 | mutex_unlock(&phydev->lock); | |
3027 | ||
3028 | return err; | |
01e5b728 AL |
3029 | } |
3030 | ||
4e901018 AL |
3031 | static int phy_led_blink_set(struct led_classdev *led_cdev, |
3032 | unsigned long *delay_on, | |
3033 | unsigned long *delay_off) | |
3034 | { | |
3035 | struct phy_led *phyled = to_phy_led(led_cdev); | |
3036 | struct phy_device *phydev = phyled->phydev; | |
3037 | int err; | |
3038 | ||
3039 | mutex_lock(&phydev->lock); | |
3040 | err = phydev->drv->led_blink_set(phydev, phyled->index, | |
3041 | delay_on, delay_off); | |
3042 | mutex_unlock(&phydev->lock); | |
3043 | ||
3044 | return err; | |
3045 | } | |
3046 | ||
1dcc03c9 AL |
3047 | static __maybe_unused struct device * |
3048 | phy_led_hw_control_get_device(struct led_classdev *led_cdev) | |
3049 | { | |
3050 | struct phy_led *phyled = to_phy_led(led_cdev); | |
3051 | struct phy_device *phydev = phyled->phydev; | |
3052 | ||
3053 | if (phydev->attached_dev) | |
3054 | return &phydev->attached_dev->dev; | |
3055 | return NULL; | |
3056 | } | |
3057 | ||
3058 | static int __maybe_unused | |
3059 | phy_led_hw_control_get(struct led_classdev *led_cdev, | |
3060 | unsigned long *rules) | |
3061 | { | |
3062 | struct phy_led *phyled = to_phy_led(led_cdev); | |
3063 | struct phy_device *phydev = phyled->phydev; | |
3064 | int err; | |
3065 | ||
3066 | mutex_lock(&phydev->lock); | |
3067 | err = phydev->drv->led_hw_control_get(phydev, phyled->index, rules); | |
3068 | mutex_unlock(&phydev->lock); | |
3069 | ||
3070 | return err; | |
3071 | } | |
3072 | ||
3073 | static int __maybe_unused | |
3074 | phy_led_hw_control_set(struct led_classdev *led_cdev, | |
3075 | unsigned long rules) | |
3076 | { | |
3077 | struct phy_led *phyled = to_phy_led(led_cdev); | |
3078 | struct phy_device *phydev = phyled->phydev; | |
3079 | int err; | |
3080 | ||
3081 | mutex_lock(&phydev->lock); | |
3082 | err = phydev->drv->led_hw_control_set(phydev, phyled->index, rules); | |
3083 | mutex_unlock(&phydev->lock); | |
3084 | ||
3085 | return err; | |
3086 | } | |
3087 | ||
3088 | static __maybe_unused int phy_led_hw_is_supported(struct led_classdev *led_cdev, | |
3089 | unsigned long rules) | |
3090 | { | |
3091 | struct phy_led *phyled = to_phy_led(led_cdev); | |
3092 | struct phy_device *phydev = phyled->phydev; | |
3093 | int err; | |
3094 | ||
3095 | mutex_lock(&phydev->lock); | |
3096 | err = phydev->drv->led_hw_is_supported(phydev, phyled->index, rules); | |
3097 | mutex_unlock(&phydev->lock); | |
3098 | ||
3099 | return err; | |
3100 | } | |
3101 | ||
c938ab4d AL |
3102 | static void phy_leds_unregister(struct phy_device *phydev) |
3103 | { | |
f50b5d74 | 3104 | struct phy_led *phyled, *tmp; |
c938ab4d | 3105 | |
f50b5d74 | 3106 | list_for_each_entry_safe(phyled, tmp, &phydev->leds, list) { |
c938ab4d | 3107 | led_classdev_unregister(&phyled->led_cdev); |
f50b5d74 | 3108 | list_del(&phyled->list); |
c938ab4d AL |
3109 | } |
3110 | } | |
3111 | ||
01e5b728 AL |
3112 | static int of_phy_led(struct phy_device *phydev, |
3113 | struct device_node *led) | |
3114 | { | |
3115 | struct device *dev = &phydev->mdio.dev; | |
3116 | struct led_init_data init_data = {}; | |
3117 | struct led_classdev *cdev; | |
7ae215ee | 3118 | unsigned long modes = 0; |
01e5b728 | 3119 | struct phy_led *phyled; |
aed8fdad | 3120 | u32 index; |
01e5b728 AL |
3121 | int err; |
3122 | ||
3123 | phyled = devm_kzalloc(dev, sizeof(*phyled), GFP_KERNEL); | |
3124 | if (!phyled) | |
3125 | return -ENOMEM; | |
3126 | ||
3127 | cdev = &phyled->led_cdev; | |
68481818 | 3128 | phyled->phydev = phydev; |
01e5b728 | 3129 | |
aed8fdad | 3130 | err = of_property_read_u32(led, "reg", &index); |
01e5b728 AL |
3131 | if (err) |
3132 | return err; | |
aed8fdad AS |
3133 | if (index > U8_MAX) |
3134 | return -EINVAL; | |
01e5b728 | 3135 | |
a274465c DG |
3136 | if (of_property_read_bool(led, "active-high")) |
3137 | set_bit(PHY_LED_ACTIVE_HIGH, &modes); | |
7ae215ee CM |
3138 | if (of_property_read_bool(led, "active-low")) |
3139 | set_bit(PHY_LED_ACTIVE_LOW, &modes); | |
3140 | if (of_property_read_bool(led, "inactive-high-impedance")) | |
3141 | set_bit(PHY_LED_INACTIVE_HIGH_IMPEDANCE, &modes); | |
3142 | ||
a274465c DG |
3143 | if (WARN_ON(modes & BIT(PHY_LED_ACTIVE_LOW) && |
3144 | modes & BIT(PHY_LED_ACTIVE_HIGH))) | |
3145 | return -EINVAL; | |
3146 | ||
7ae215ee CM |
3147 | if (modes) { |
3148 | /* Return error if asked to set polarity modes but not supported */ | |
3149 | if (!phydev->drv->led_polarity_set) | |
3150 | return -EINVAL; | |
3151 | ||
3152 | err = phydev->drv->led_polarity_set(phydev, index, modes); | |
3153 | if (err) | |
3154 | return err; | |
3155 | } | |
3156 | ||
aed8fdad | 3157 | phyled->index = index; |
68481818 AL |
3158 | if (phydev->drv->led_brightness_set) |
3159 | cdev->brightness_set_blocking = phy_led_set_brightness; | |
4e901018 AL |
3160 | if (phydev->drv->led_blink_set) |
3161 | cdev->blink_set = phy_led_blink_set; | |
1dcc03c9 AL |
3162 | |
3163 | #ifdef CONFIG_LEDS_TRIGGERS | |
3164 | if (phydev->drv->led_hw_is_supported && | |
3165 | phydev->drv->led_hw_control_set && | |
3166 | phydev->drv->led_hw_control_get) { | |
3167 | cdev->hw_control_is_supported = phy_led_hw_is_supported; | |
3168 | cdev->hw_control_set = phy_led_hw_control_set; | |
3169 | cdev->hw_control_get = phy_led_hw_control_get; | |
3170 | cdev->hw_control_trigger = "netdev"; | |
3171 | } | |
3172 | ||
3173 | cdev->hw_control_get_device = phy_led_hw_control_get_device; | |
3174 | #endif | |
01e5b728 AL |
3175 | cdev->max_brightness = 1; |
3176 | init_data.devicename = dev_name(&phydev->mdio.dev); | |
3177 | init_data.fwnode = of_fwnode_handle(led); | |
3178 | init_data.devname_mandatory = true; | |
3179 | ||
c938ab4d | 3180 | err = led_classdev_register_ext(dev, cdev, &init_data); |
01e5b728 AL |
3181 | if (err) |
3182 | return err; | |
3183 | ||
3184 | list_add(&phyled->list, &phydev->leds); | |
3185 | ||
3186 | return 0; | |
3187 | } | |
3188 | ||
3189 | static int of_phy_leds(struct phy_device *phydev) | |
3190 | { | |
3191 | struct device_node *node = phydev->mdio.dev.of_node; | |
1dce520a | 3192 | struct device_node *leds; |
01e5b728 AL |
3193 | int err; |
3194 | ||
3195 | if (!IS_ENABLED(CONFIG_OF_MDIO)) | |
3196 | return 0; | |
3197 | ||
3198 | if (!node) | |
3199 | return 0; | |
3200 | ||
3201 | leds = of_get_child_by_name(node, "leds"); | |
3202 | if (!leds) | |
3203 | return 0; | |
3204 | ||
16aef666 CM |
3205 | /* Check if the PHY driver have at least an OP to |
3206 | * set the LEDs. | |
3207 | */ | |
3208 | if (!(phydev->drv->led_brightness_set || | |
3209 | phydev->drv->led_blink_set || | |
3210 | phydev->drv->led_hw_control_set)) { | |
3211 | phydev_dbg(phydev, "ignoring leds node defined with no PHY driver support\n"); | |
3212 | goto exit; | |
3213 | } | |
3214 | ||
1dce520a | 3215 | for_each_available_child_of_node_scoped(leds, led) { |
01e5b728 AL |
3216 | err = of_phy_led(phydev, led); |
3217 | if (err) { | |
2560db6e | 3218 | of_node_put(leds); |
c938ab4d | 3219 | phy_leds_unregister(phydev); |
01e5b728 AL |
3220 | return err; |
3221 | } | |
3222 | } | |
3223 | ||
16aef666 | 3224 | exit: |
2560db6e | 3225 | of_node_put(leds); |
01e5b728 AL |
3226 | return 0; |
3227 | } | |
3228 | ||
0fb16976 CJ |
3229 | /** |
3230 | * fwnode_mdio_find_device - Given a fwnode, find the mdio_device | |
3231 | * @fwnode: pointer to the mdio_device's fwnode | |
3232 | * | |
3233 | * If successful, returns a pointer to the mdio_device with the embedded | |
3234 | * struct device refcount incremented by one, or NULL on failure. | |
3235 | * The caller should call put_device() on the mdio_device after its use. | |
3236 | */ | |
3237 | struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode) | |
3238 | { | |
3239 | struct device *d; | |
3240 | ||
3241 | if (!fwnode) | |
3242 | return NULL; | |
3243 | ||
3244 | d = bus_find_device_by_fwnode(&mdio_bus_type, fwnode); | |
3245 | if (!d) | |
3246 | return NULL; | |
3247 | ||
3248 | return to_mdio_device(d); | |
3249 | } | |
3250 | EXPORT_SYMBOL(fwnode_mdio_find_device); | |
3251 | ||
425775ed CJ |
3252 | /** |
3253 | * fwnode_phy_find_device - For provided phy_fwnode, find phy_device. | |
3254 | * | |
3255 | * @phy_fwnode: Pointer to the phy's fwnode. | |
3256 | * | |
3257 | * If successful, returns a pointer to the phy_device with the embedded | |
3258 | * struct device refcount incremented by one, or NULL on failure. | |
3259 | */ | |
3260 | struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode) | |
3261 | { | |
3262 | struct mdio_device *mdiodev; | |
3263 | ||
3264 | mdiodev = fwnode_mdio_find_device(phy_fwnode); | |
3265 | if (!mdiodev) | |
3266 | return NULL; | |
3267 | ||
3268 | if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) | |
3269 | return to_phy_device(&mdiodev->dev); | |
3270 | ||
3271 | put_device(&mdiodev->dev); | |
3272 | ||
3273 | return NULL; | |
3274 | } | |
3275 | EXPORT_SYMBOL(fwnode_phy_find_device); | |
3276 | ||
425775ed CJ |
3277 | /** |
3278 | * fwnode_get_phy_node - Get the phy_node using the named reference. | |
3279 | * @fwnode: Pointer to fwnode from which phy_node has to be obtained. | |
3280 | * | |
3281 | * Refer return conditions of fwnode_find_reference(). | |
3282 | * For ACPI, only "phy-handle" is supported. Legacy DT properties "phy" | |
3283 | * and "phy-device" are not supported in ACPI. DT supports all the three | |
3284 | * named references to the phy node. | |
3285 | */ | |
4a0faa02 | 3286 | struct fwnode_handle *fwnode_get_phy_node(const struct fwnode_handle *fwnode) |
425775ed CJ |
3287 | { |
3288 | struct fwnode_handle *phy_node; | |
3289 | ||
3290 | /* Only phy-handle is used for ACPI */ | |
3291 | phy_node = fwnode_find_reference(fwnode, "phy-handle", 0); | |
1f586017 | 3292 | if (!IS_ERR(phy_node) || is_acpi_node(fwnode)) |
425775ed CJ |
3293 | return phy_node; |
3294 | phy_node = fwnode_find_reference(fwnode, "phy", 0); | |
1f586017 AS |
3295 | if (!IS_ERR(phy_node)) |
3296 | return phy_node; | |
3297 | return fwnode_find_reference(fwnode, "phy-device", 0); | |
425775ed CJ |
3298 | } |
3299 | EXPORT_SYMBOL_GPL(fwnode_get_phy_node); | |
3300 | ||
b3df0da8 RD |
3301 | /** |
3302 | * phy_probe - probe and init a PHY device | |
3303 | * @dev: device to probe and init | |
00db8189 | 3304 | * |
83456576 | 3305 | * Take care of setting up the phy_device structure, set the state to READY. |
00db8189 AF |
3306 | */ |
3307 | static int phy_probe(struct device *dev) | |
3308 | { | |
553fe92b | 3309 | struct phy_device *phydev = to_phy_device(dev); |
e5a03bfd | 3310 | struct device_driver *drv = phydev->mdio.dev.driver; |
553fe92b | 3311 | struct phy_driver *phydrv = to_phy_driver(drv); |
00db8189 AF |
3312 | int err = 0; |
3313 | ||
00db8189 AF |
3314 | phydev->drv = phydrv; |
3315 | ||
2c7b4921 FF |
3316 | /* Disable the interrupt if the PHY doesn't support it |
3317 | * but the interrupt is still a valid one | |
3318 | */ | |
b269875f | 3319 | if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev)) |
00db8189 AF |
3320 | phydev->irq = PHY_POLL; |
3321 | ||
4284b6a5 FF |
3322 | if (phydrv->flags & PHY_IS_INTERNAL) |
3323 | phydev->is_internal = true; | |
3324 | ||
1dba6995 BG |
3325 | /* Deassert the reset signal */ |
3326 | phy_device_reset(phydev, 0); | |
92ed2eb7 | 3327 | |
1dba6995 | 3328 | if (phydev->drv->probe) { |
92ed2eb7 | 3329 | err = phydev->drv->probe(phydev); |
1dba6995 | 3330 | if (err) |
92ed2eb7 | 3331 | goto out; |
92ed2eb7 AL |
3332 | } |
3333 | ||
cc941e54 RKO |
3334 | phy_disable_interrupts(phydev); |
3335 | ||
00db8189 AF |
3336 | /* Start out supporting everything. Eventually, |
3337 | * a controller will attach, and may modify one | |
2f53e904 SS |
3338 | * or both of these values |
3339 | */ | |
c2a978c1 | 3340 | if (phydrv->features) { |
efbdfdc2 | 3341 | linkmode_copy(phydev->supported, phydrv->features); |
c2a978c1 AL |
3342 | genphy_c45_read_eee_abilities(phydev); |
3343 | } | |
169d7a40 | 3344 | else if (phydrv->get_features) |
efbdfdc2 | 3345 | err = phydrv->get_features(phydev); |
169d7a40 | 3346 | else if (phydev->is_c45) |
a1deab17 | 3347 | err = genphy_c45_pma_read_abilities(phydev); |
169d7a40 | 3348 | else |
a1deab17 | 3349 | err = genphy_read_abilities(phydev); |
efbdfdc2 | 3350 | |
a1deab17 HK |
3351 | if (err) |
3352 | goto out; | |
3353 | ||
5e42574b HK |
3354 | if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, |
3355 | phydev->supported)) | |
3356 | phydev->autoneg = 0; | |
3357 | ||
3b8b11f9 HK |
3358 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, |
3359 | phydev->supported)) | |
3360 | phydev->is_gigabit_capable = 1; | |
3361 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, | |
3362 | phydev->supported)) | |
3363 | phydev->is_gigabit_capable = 1; | |
3364 | ||
de906af1 | 3365 | of_set_phy_supported(phydev); |
22c0ef6b | 3366 | phy_advertise_supported(phydev); |
00db8189 | 3367 | |
3eeca4e1 OR |
3368 | /* Get PHY default EEE advertising modes and handle them as potentially |
3369 | * safe initial configuration. | |
3370 | */ | |
3371 | err = genphy_c45_read_eee_adv(phydev, phydev->advertising_eee); | |
3372 | if (err) | |
8f9850dd | 3373 | goto out; |
3eeca4e1 | 3374 | |
7f33fea6 HK |
3375 | /* Get the EEE modes we want to prohibit. */ |
3376 | of_set_phy_eee_broken(phydev); | |
3eeca4e1 OR |
3377 | |
3378 | /* Some PHYs may advertise, by default, not support EEE modes. So, | |
7f33fea6 | 3379 | * we need to clean them. In addition remove all disabled EEE modes. |
3eeca4e1 | 3380 | */ |
7f33fea6 HK |
3381 | linkmode_and(phydev->advertising_eee, phydev->supported_eee, |
3382 | phydev->advertising_eee); | |
3383 | linkmode_andnot(phydev->advertising_eee, phydev->advertising_eee, | |
3384 | phydev->eee_disabled_modes); | |
3eeca4e1 | 3385 | |
7f33fea6 HK |
3386 | /* There is no "enabled" flag. If PHY is advertising, assume it is |
3387 | * kind of enabled. | |
d853d145 | 3388 | */ |
7f33fea6 | 3389 | phydev->eee_cfg.eee_enabled = !linkmode_empty(phydev->advertising_eee); |
d853d145 | 3390 | |
20a4da20 OR |
3391 | /* Get master/slave strap overrides */ |
3392 | of_set_phy_timing_role(phydev); | |
3393 | ||
529ed127 TT |
3394 | /* The Pause Frame bits indicate that the PHY can support passing |
3395 | * pause frames. During autonegotiation, the PHYs will determine if | |
3396 | * they should allow pause frames to pass. The MAC driver should then | |
3397 | * use that result to determine whether to enable flow control via | |
3398 | * pause frames. | |
3399 | * | |
3400 | * Normally, PHY drivers should not set the Pause bits, and instead | |
3401 | * allow phylib to do that. However, there may be some situations | |
3402 | * (e.g. hardware erratum) where the driver wants to set only one | |
3403 | * of these bits. | |
3404 | */ | |
efbdfdc2 AL |
3405 | if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) && |
3406 | !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) { | |
3c1bcc86 AL |
3407 | linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
3408 | phydev->supported); | |
3409 | linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, | |
3410 | phydev->supported); | |
529ed127 TT |
3411 | } |
3412 | ||
00db8189 AF |
3413 | /* Set the state to READY by default */ |
3414 | phydev->state = PHY_READY; | |
3415 | ||
01e5b728 AL |
3416 | /* Get the LEDs from the device tree, and instantiate standard |
3417 | * LEDs for them. | |
3418 | */ | |
4bb7aac7 AB |
3419 | if (IS_ENABLED(CONFIG_PHYLIB_LEDS)) |
3420 | err = of_phy_leds(phydev); | |
01e5b728 | 3421 | |
92ed2eb7 | 3422 | out: |
f4b47a2e | 3423 | /* Re-assert the reset signal on error */ |
1dba6995 BG |
3424 | if (err) |
3425 | phy_device_reset(phydev, 1); | |
3426 | ||
00db8189 AF |
3427 | return err; |
3428 | } | |
3429 | ||
3430 | static int phy_remove(struct device *dev) | |
3431 | { | |
553fe92b | 3432 | struct phy_device *phydev = to_phy_device(dev); |
00db8189 | 3433 | |
7b9a88a3 FF |
3434 | cancel_delayed_work_sync(&phydev->state_queue); |
3435 | ||
c938ab4d AL |
3436 | if (IS_ENABLED(CONFIG_PHYLIB_LEDS)) |
3437 | phy_leds_unregister(phydev); | |
3438 | ||
00db8189 | 3439 | phydev->state = PHY_DOWN; |
00db8189 | 3440 | |
298e54fa RK |
3441 | sfp_bus_del_upstream(phydev->sfp_bus); |
3442 | phydev->sfp_bus = NULL; | |
3443 | ||
1dba6995 | 3444 | if (phydev->drv && phydev->drv->remove) |
00db8189 | 3445 | phydev->drv->remove(phydev); |
bafbdd52 | 3446 | |
1dba6995 BG |
3447 | /* Assert the reset signal */ |
3448 | phy_device_reset(phydev, 1); | |
3449 | ||
00db8189 AF |
3450 | phydev->drv = NULL; |
3451 | ||
3452 | return 0; | |
3453 | } | |
3454 | ||
b3df0da8 RD |
3455 | /** |
3456 | * phy_driver_register - register a phy_driver with the PHY layer | |
3457 | * @new_driver: new phy_driver to register | |
be01da72 | 3458 | * @owner: module owning this PHY |
b3df0da8 | 3459 | */ |
be01da72 | 3460 | int phy_driver_register(struct phy_driver *new_driver, struct module *owner) |
00db8189 AF |
3461 | { |
3462 | int retval; | |
3463 | ||
efbdfdc2 | 3464 | /* Either the features are hard coded, or dynamically |
a1deab17 | 3465 | * determined. It cannot be both. |
efbdfdc2 | 3466 | */ |
a1deab17 HK |
3467 | if (WARN_ON(new_driver->features && new_driver->get_features)) { |
3468 | pr_err("%s: features and get_features must not both be set\n", | |
3469 | new_driver->name); | |
3e64cf7a CG |
3470 | return -EINVAL; |
3471 | } | |
3472 | ||
2b12d51c RKO |
3473 | /* PHYLIB device drivers must not match using a DT compatible table |
3474 | * as this bypasses our checks that the mdiodev that is being matched | |
3475 | * is backed by a struct phy_device. If such a case happens, we will | |
3476 | * make out-of-bounds accesses and lockup in phydev->lock. | |
3477 | */ | |
3478 | if (WARN(new_driver->mdiodrv.driver.of_match_table, | |
3479 | "%s: driver must not provide a DT match table\n", | |
3480 | new_driver->name)) | |
3481 | return -EINVAL; | |
3482 | ||
a9049e0c AL |
3483 | new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY; |
3484 | new_driver->mdiodrv.driver.name = new_driver->name; | |
3485 | new_driver->mdiodrv.driver.bus = &mdio_bus_type; | |
3486 | new_driver->mdiodrv.driver.probe = phy_probe; | |
3487 | new_driver->mdiodrv.driver.remove = phy_remove; | |
3488 | new_driver->mdiodrv.driver.owner = owner; | |
16983507 | 3489 | new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS; |
00db8189 | 3490 | |
a9049e0c | 3491 | retval = driver_register(&new_driver->mdiodrv.driver); |
00db8189 | 3492 | if (retval) { |
8d242488 JP |
3493 | pr_err("%s: Error %d in registering driver\n", |
3494 | new_driver->name, retval); | |
00db8189 AF |
3495 | |
3496 | return retval; | |
3497 | } | |
3498 | ||
f2511f13 | 3499 | pr_debug("%s: Registered new driver\n", new_driver->name); |
00db8189 AF |
3500 | |
3501 | return 0; | |
3502 | } | |
3503 | EXPORT_SYMBOL(phy_driver_register); | |
3504 | ||
be01da72 AL |
3505 | int phy_drivers_register(struct phy_driver *new_driver, int n, |
3506 | struct module *owner) | |
d5bf9071 CH |
3507 | { |
3508 | int i, ret = 0; | |
3509 | ||
3510 | for (i = 0; i < n; i++) { | |
be01da72 | 3511 | ret = phy_driver_register(new_driver + i, owner); |
d5bf9071 CH |
3512 | if (ret) { |
3513 | while (i-- > 0) | |
3514 | phy_driver_unregister(new_driver + i); | |
3515 | break; | |
3516 | } | |
3517 | } | |
3518 | return ret; | |
3519 | } | |
3520 | EXPORT_SYMBOL(phy_drivers_register); | |
3521 | ||
00db8189 AF |
3522 | void phy_driver_unregister(struct phy_driver *drv) |
3523 | { | |
a9049e0c | 3524 | driver_unregister(&drv->mdiodrv.driver); |
00db8189 AF |
3525 | } |
3526 | EXPORT_SYMBOL(phy_driver_unregister); | |
3527 | ||
d5bf9071 CH |
3528 | void phy_drivers_unregister(struct phy_driver *drv, int n) |
3529 | { | |
3530 | int i; | |
2f53e904 SS |
3531 | |
3532 | for (i = 0; i < n; i++) | |
d5bf9071 | 3533 | phy_driver_unregister(drv + i); |
d5bf9071 CH |
3534 | } |
3535 | EXPORT_SYMBOL(phy_drivers_unregister); | |
3536 | ||
921690f2 | 3537 | static struct phy_driver genphy_driver = { |
e1393456 AF |
3538 | .phy_id = 0xffffffff, |
3539 | .phy_id_mask = 0xffffffff, | |
3540 | .name = "Generic PHY", | |
2a4d8674 | 3541 | .get_features = genphy_read_abilities, |
0f0ca340 GC |
3542 | .suspend = genphy_suspend, |
3543 | .resume = genphy_resume, | |
f0f9b4ed | 3544 | .set_loopback = genphy_loopback, |
921690f2 | 3545 | }; |
00db8189 | 3546 | |
55d8f053 | 3547 | static const struct ethtool_phy_ops phy_ethtool_phy_ops = { |
bd36ed1c FF |
3548 | .get_sset_count = phy_ethtool_get_sset_count, |
3549 | .get_strings = phy_ethtool_get_strings, | |
3550 | .get_stats = phy_ethtool_get_stats, | |
a23a1e57 PB |
3551 | .get_plca_cfg = phy_ethtool_get_plca_cfg, |
3552 | .set_plca_cfg = phy_ethtool_set_plca_cfg, | |
3553 | .get_plca_status = phy_ethtool_get_plca_status, | |
55d8f053 FF |
3554 | .start_cable_test = phy_start_cable_test, |
3555 | .start_cable_test_tdr = phy_start_cable_test_tdr, | |
3556 | }; | |
3557 | ||
60495b66 VO |
3558 | static const struct phylib_stubs __phylib_stubs = { |
3559 | .hwtstamp_get = __phy_hwtstamp_get, | |
3560 | .hwtstamp_set = __phy_hwtstamp_set, | |
b7a2c1fe JK |
3561 | .get_phy_stats = __phy_ethtool_get_phy_stats, |
3562 | .get_link_ext_stats = __phy_ethtool_get_link_ext_stats, | |
60495b66 VO |
3563 | }; |
3564 | ||
3565 | static void phylib_register_stubs(void) | |
3566 | { | |
3567 | phylib_stubs = &__phylib_stubs; | |
3568 | } | |
3569 | ||
3570 | static void phylib_unregister_stubs(void) | |
3571 | { | |
3572 | phylib_stubs = NULL; | |
3573 | } | |
3574 | ||
67c4f3fa | 3575 | static int __init phy_init(void) |
00db8189 | 3576 | { |
67c4f3fa | 3577 | int rc; |
67c4f3fa | 3578 | |
70ef7d87 | 3579 | rtnl_lock(); |
1c613bea | 3580 | ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops); |
60495b66 | 3581 | phylib_register_stubs(); |
70ef7d87 | 3582 | rtnl_unlock(); |
1c613bea | 3583 | |
d8c838a5 MC |
3584 | rc = phy_caps_init(); |
3585 | if (rc) | |
31be641d | 3586 | goto err_ethtool_phy_ops; |
d8c838a5 | 3587 | |
719655a1 AL |
3588 | features_init(); |
3589 | ||
22b56e82 | 3590 | rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE); |
e1393456 | 3591 | if (rc) |
31be641d | 3592 | goto err_ethtool_phy_ops; |
921690f2 RK |
3593 | |
3594 | rc = phy_driver_register(&genphy_driver, THIS_MODULE); | |
1c613bea VO |
3595 | if (rc) |
3596 | goto err_c45; | |
3597 | ||
3598 | return 0; | |
3599 | ||
22b56e82 | 3600 | err_c45: |
1c613bea | 3601 | phy_driver_unregister(&genphy_c45_driver); |
1c613bea | 3602 | err_ethtool_phy_ops: |
70ef7d87 | 3603 | rtnl_lock(); |
60495b66 | 3604 | phylib_unregister_stubs(); |
1c613bea | 3605 | ethtool_set_ethtool_phy_ops(NULL); |
70ef7d87 | 3606 | rtnl_unlock(); |
67c4f3fa | 3607 | |
67c4f3fa | 3608 | return rc; |
00db8189 AF |
3609 | } |
3610 | ||
67c4f3fa | 3611 | static void __exit phy_exit(void) |
00db8189 | 3612 | { |
22b56e82 | 3613 | phy_driver_unregister(&genphy_c45_driver); |
921690f2 | 3614 | phy_driver_unregister(&genphy_driver); |
70ef7d87 | 3615 | rtnl_lock(); |
60495b66 | 3616 | phylib_unregister_stubs(); |
55d8f053 | 3617 | ethtool_set_ethtool_phy_ops(NULL); |
70ef7d87 | 3618 | rtnl_unlock(); |
00db8189 AF |
3619 | } |
3620 | ||
e1393456 | 3621 | subsys_initcall(phy_init); |
67c4f3fa | 3622 | module_exit(phy_exit); |