]>
Commit | Line | Data |
---|---|---|
2f53e904 | 1 | /* Framework for finding and configuring PHYs. |
00db8189 AF |
2 | * Also contains generic PHY driver |
3 | * | |
4 | * Author: Andy Fleming | |
5 | * | |
6 | * Copyright (c) 2004 Freescale Semiconductor, Inc. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2 of the License, or (at your | |
11 | * option) any later version. | |
12 | * | |
13 | */ | |
8d242488 JP |
14 | |
15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
16 | ||
00db8189 | 17 | #include <linux/kernel.h> |
00db8189 AF |
18 | #include <linux/string.h> |
19 | #include <linux/errno.h> | |
20 | #include <linux/unistd.h> | |
21 | #include <linux/slab.h> | |
22 | #include <linux/interrupt.h> | |
23 | #include <linux/init.h> | |
24 | #include <linux/delay.h> | |
25 | #include <linux/netdevice.h> | |
26 | #include <linux/etherdevice.h> | |
27 | #include <linux/skbuff.h> | |
00db8189 AF |
28 | #include <linux/mm.h> |
29 | #include <linux/module.h> | |
00db8189 AF |
30 | #include <linux/mii.h> |
31 | #include <linux/ethtool.h> | |
32 | #include <linux/phy.h> | |
124059fd | 33 | #include <linux/mdio.h> |
2f53e904 SS |
34 | #include <linux/io.h> |
35 | #include <linux/uaccess.h> | |
de906af1 | 36 | #include <linux/of.h> |
00db8189 | 37 | |
00db8189 | 38 | #include <asm/irq.h> |
00db8189 | 39 | |
afcceaa3 OH |
40 | MODULE_DESCRIPTION("PHY library"); |
41 | MODULE_AUTHOR("Andy Fleming"); | |
42 | MODULE_LICENSE("GPL"); | |
43 | ||
6f4a7f41 AV |
44 | void phy_device_free(struct phy_device *phydev) |
45 | { | |
e5a03bfd | 46 | put_device(&phydev->mdio.dev); |
6f4a7f41 | 47 | } |
4dea547f | 48 | EXPORT_SYMBOL(phy_device_free); |
6f4a7f41 AV |
49 | |
50 | static void phy_device_release(struct device *dev) | |
51 | { | |
b2a43191 | 52 | kfree(to_phy_device(dev)); |
6f4a7f41 AV |
53 | } |
54 | ||
ab2145ed SX |
55 | enum genphy_driver { |
56 | GENPHY_DRV_1G, | |
124059fd | 57 | GENPHY_DRV_10G, |
ab2145ed SX |
58 | GENPHY_DRV_MAX |
59 | }; | |
60 | ||
61 | static struct phy_driver genphy_driver[GENPHY_DRV_MAX]; | |
4dea547f | 62 | |
f62220d3 AF |
63 | static LIST_HEAD(phy_fixup_list); |
64 | static DEFINE_MUTEX(phy_fixup_lock); | |
65 | ||
bc87922f AL |
66 | #ifdef CONFIG_PM |
67 | static bool mdio_bus_phy_may_suspend(struct phy_device *phydev) | |
68 | { | |
69 | struct device_driver *drv = phydev->mdio.dev.driver; | |
70 | struct phy_driver *phydrv = to_phy_driver(drv); | |
71 | struct net_device *netdev = phydev->attached_dev; | |
72 | ||
73 | if (!drv || !phydrv->suspend) | |
74 | return false; | |
75 | ||
76 | /* PHY not attached? May suspend if the PHY has not already been | |
77 | * suspended as part of a prior call to phy_disconnect() -> | |
78 | * phy_detach() -> phy_suspend() because the parent netdev might be the | |
79 | * MDIO bus driver and clock gated at this point. | |
80 | */ | |
81 | if (!netdev) | |
82 | return !phydev->suspended; | |
83 | ||
84 | /* Don't suspend PHY if the attached netdev parent may wakeup. | |
85 | * The parent may point to a PCI device, as in tg3 driver. | |
86 | */ | |
87 | if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent)) | |
88 | return false; | |
89 | ||
90 | /* Also don't suspend PHY if the netdev itself may wakeup. This | |
91 | * is the case for devices w/o underlaying pwr. mgmt. aware bus, | |
92 | * e.g. SoC devices. | |
93 | */ | |
94 | if (device_may_wakeup(&netdev->dev)) | |
95 | return false; | |
96 | ||
97 | return true; | |
98 | } | |
99 | ||
100 | static int mdio_bus_phy_suspend(struct device *dev) | |
101 | { | |
102 | struct phy_device *phydev = to_phy_device(dev); | |
103 | ||
104 | /* We must stop the state machine manually, otherwise it stops out of | |
105 | * control, possibly with the phydev->lock held. Upon resume, netdev | |
106 | * may call phy routines that try to grab the same lock, and that may | |
107 | * lead to a deadlock. | |
108 | */ | |
109 | if (phydev->attached_dev && phydev->adjust_link) | |
110 | phy_stop_machine(phydev); | |
111 | ||
112 | if (!mdio_bus_phy_may_suspend(phydev)) | |
113 | return 0; | |
114 | ||
115 | return phy_suspend(phydev); | |
116 | } | |
117 | ||
118 | static int mdio_bus_phy_resume(struct device *dev) | |
119 | { | |
120 | struct phy_device *phydev = to_phy_device(dev); | |
121 | int ret; | |
122 | ||
123 | if (!mdio_bus_phy_may_suspend(phydev)) | |
124 | goto no_resume; | |
125 | ||
126 | ret = phy_resume(phydev); | |
127 | if (ret < 0) | |
128 | return ret; | |
129 | ||
130 | no_resume: | |
131 | if (phydev->attached_dev && phydev->adjust_link) | |
132 | phy_start_machine(phydev); | |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
137 | static int mdio_bus_phy_restore(struct device *dev) | |
138 | { | |
139 | struct phy_device *phydev = to_phy_device(dev); | |
140 | struct net_device *netdev = phydev->attached_dev; | |
141 | int ret; | |
142 | ||
143 | if (!netdev) | |
144 | return 0; | |
145 | ||
146 | ret = phy_init_hw(phydev); | |
147 | if (ret < 0) | |
148 | return ret; | |
149 | ||
150 | /* The PHY needs to renegotiate. */ | |
151 | phydev->link = 0; | |
152 | phydev->state = PHY_UP; | |
153 | ||
154 | phy_start_machine(phydev); | |
155 | ||
156 | return 0; | |
157 | } | |
158 | ||
159 | static const struct dev_pm_ops mdio_bus_phy_pm_ops = { | |
160 | .suspend = mdio_bus_phy_suspend, | |
161 | .resume = mdio_bus_phy_resume, | |
162 | .freeze = mdio_bus_phy_suspend, | |
163 | .thaw = mdio_bus_phy_resume, | |
164 | .restore = mdio_bus_phy_restore, | |
165 | }; | |
166 | ||
167 | #define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops) | |
168 | ||
169 | #else | |
170 | ||
171 | #define MDIO_BUS_PHY_PM_OPS NULL | |
172 | ||
173 | #endif /* CONFIG_PM */ | |
174 | ||
2f53e904 SS |
175 | /** |
176 | * phy_register_fixup - creates a new phy_fixup and adds it to the list | |
e5a03bfd | 177 | * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID) |
f62220d3 | 178 | * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY) |
2f53e904 | 179 | * It can also be PHY_ANY_UID |
f62220d3 | 180 | * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before |
2f53e904 | 181 | * comparison |
f62220d3 AF |
182 | * @run: The actual code to be run when a matching PHY is found |
183 | */ | |
184 | int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask, | |
2f53e904 | 185 | int (*run)(struct phy_device *)) |
f62220d3 | 186 | { |
553fe92b | 187 | struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL); |
f62220d3 | 188 | |
f62220d3 AF |
189 | if (!fixup) |
190 | return -ENOMEM; | |
191 | ||
fb28ad35 | 192 | strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id)); |
f62220d3 AF |
193 | fixup->phy_uid = phy_uid; |
194 | fixup->phy_uid_mask = phy_uid_mask; | |
195 | fixup->run = run; | |
196 | ||
197 | mutex_lock(&phy_fixup_lock); | |
198 | list_add_tail(&fixup->list, &phy_fixup_list); | |
199 | mutex_unlock(&phy_fixup_lock); | |
200 | ||
201 | return 0; | |
202 | } | |
203 | EXPORT_SYMBOL(phy_register_fixup); | |
204 | ||
205 | /* Registers a fixup to be run on any PHY with the UID in phy_uid */ | |
206 | int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask, | |
2f53e904 | 207 | int (*run)(struct phy_device *)) |
f62220d3 AF |
208 | { |
209 | return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run); | |
210 | } | |
211 | EXPORT_SYMBOL(phy_register_fixup_for_uid); | |
212 | ||
213 | /* Registers a fixup to be run on the PHY with id string bus_id */ | |
214 | int phy_register_fixup_for_id(const char *bus_id, | |
2f53e904 | 215 | int (*run)(struct phy_device *)) |
f62220d3 AF |
216 | { |
217 | return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run); | |
218 | } | |
219 | EXPORT_SYMBOL(phy_register_fixup_for_id); | |
220 | ||
2f53e904 | 221 | /* Returns 1 if fixup matches phydev in bus_id and phy_uid. |
f62220d3 AF |
222 | * Fixups can be set to match any in one or more fields. |
223 | */ | |
224 | static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup) | |
225 | { | |
84eff6d1 | 226 | if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0) |
f62220d3 AF |
227 | if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0) |
228 | return 0; | |
229 | ||
230 | if ((fixup->phy_uid & fixup->phy_uid_mask) != | |
2f53e904 | 231 | (phydev->phy_id & fixup->phy_uid_mask)) |
f62220d3 AF |
232 | if (fixup->phy_uid != PHY_ANY_UID) |
233 | return 0; | |
234 | ||
235 | return 1; | |
236 | } | |
237 | ||
238 | /* Runs any matching fixups for this phydev */ | |
fbfcec63 | 239 | static int phy_scan_fixups(struct phy_device *phydev) |
f62220d3 AF |
240 | { |
241 | struct phy_fixup *fixup; | |
242 | ||
243 | mutex_lock(&phy_fixup_lock); | |
244 | list_for_each_entry(fixup, &phy_fixup_list, list) { | |
245 | if (phy_needs_fixup(phydev, fixup)) { | |
553fe92b | 246 | int err = fixup->run(phydev); |
f62220d3 | 247 | |
bc23283c JS |
248 | if (err < 0) { |
249 | mutex_unlock(&phy_fixup_lock); | |
f62220d3 | 250 | return err; |
bc23283c | 251 | } |
b0ae009f | 252 | phydev->has_fixups = true; |
f62220d3 AF |
253 | } |
254 | } | |
255 | mutex_unlock(&phy_fixup_lock); | |
256 | ||
257 | return 0; | |
258 | } | |
f62220d3 | 259 | |
ac28b9f8 | 260 | struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id, |
2f53e904 SS |
261 | bool is_c45, |
262 | struct phy_c45_device_ids *c45_ids) | |
11b0bacd VB |
263 | { |
264 | struct phy_device *dev; | |
e5a03bfd | 265 | struct mdio_device *mdiodev; |
8626d3b4 | 266 | |
2f53e904 | 267 | /* We allocate the device, and initialize the default values */ |
cd861280 | 268 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
ef899c07 | 269 | if (!dev) |
d3765f08 | 270 | return ERR_PTR(-ENOMEM); |
11b0bacd | 271 | |
e5a03bfd AL |
272 | mdiodev = &dev->mdio; |
273 | mdiodev->dev.release = phy_device_release; | |
274 | mdiodev->dev.parent = &bus->dev; | |
275 | mdiodev->dev.bus = &mdio_bus_type; | |
276 | mdiodev->bus = bus; | |
bc87922f | 277 | mdiodev->pm_ops = MDIO_BUS_PHY_PM_OPS; |
e5a03bfd | 278 | mdiodev->addr = addr; |
7f854420 | 279 | mdiodev->flags = MDIO_DEVICE_FLAG_PHY; |
6f4a7f41 | 280 | |
11b0bacd VB |
281 | dev->speed = 0; |
282 | dev->duplex = -1; | |
2f53e904 SS |
283 | dev->pause = 0; |
284 | dev->asym_pause = 0; | |
11b0bacd | 285 | dev->link = 1; |
e8a2b6a4 | 286 | dev->interface = PHY_INTERFACE_MODE_GMII; |
11b0bacd VB |
287 | |
288 | dev->autoneg = AUTONEG_ENABLE; | |
289 | ||
ac28b9f8 | 290 | dev->is_c45 = is_c45; |
11b0bacd | 291 | dev->phy_id = phy_id; |
ac28b9f8 DD |
292 | if (c45_ids) |
293 | dev->c45_ids = *c45_ids; | |
ef899c07 | 294 | dev->irq = bus->irq ? bus->irq[addr] : PHY_POLL; |
e5a03bfd | 295 | dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr); |
11b0bacd VB |
296 | |
297 | dev->state = PHY_DOWN; | |
298 | ||
35b5f6b1 | 299 | mutex_init(&dev->lock); |
4f9c85a1 | 300 | INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine); |
5ea94e76 | 301 | INIT_WORK(&dev->phy_queue, phy_change); |
11b0bacd | 302 | |
8626d3b4 | 303 | /* Request the appropriate module unconditionally; don't |
2f53e904 SS |
304 | * bother trying to do so only if it isn't already loaded, |
305 | * because that gets complicated. A hotplug event would have | |
306 | * done an unconditional modprobe anyway. | |
307 | * We don't do normal hotplug because it won't work for MDIO | |
308 | * -- because it relies on the device staying around for long | |
309 | * enough for the driver to get loaded. With MDIO, the NIC | |
310 | * driver will get bored and give up as soon as it finds that | |
311 | * there's no driver _already_ loaded. | |
312 | */ | |
8626d3b4 DW |
313 | request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id)); |
314 | ||
e5a03bfd | 315 | device_initialize(&mdiodev->dev); |
b2a43191 | 316 | |
11b0bacd VB |
317 | return dev; |
318 | } | |
ac28b9f8 DD |
319 | EXPORT_SYMBOL(phy_device_create); |
320 | ||
5f6c99e0 SX |
321 | /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers. |
322 | * @bus: the target MII bus | |
323 | * @addr: PHY address on the MII bus | |
324 | * @dev_addr: MMD address in the PHY. | |
325 | * @devices_in_package: where to store the devices in package information. | |
326 | * | |
327 | * Description: reads devices in package registers of a MMD at @dev_addr | |
328 | * from PHY at @addr on @bus. | |
329 | * | |
330 | * Returns: 0 on success, -EIO on failure. | |
331 | */ | |
332 | static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr, | |
333 | u32 *devices_in_package) | |
334 | { | |
335 | int phy_reg, reg_addr; | |
336 | ||
337 | reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2; | |
338 | phy_reg = mdiobus_read(bus, addr, reg_addr); | |
339 | if (phy_reg < 0) | |
340 | return -EIO; | |
341 | *devices_in_package = (phy_reg & 0xffff) << 16; | |
342 | ||
343 | reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1; | |
344 | phy_reg = mdiobus_read(bus, addr, reg_addr); | |
345 | if (phy_reg < 0) | |
346 | return -EIO; | |
347 | *devices_in_package |= (phy_reg & 0xffff); | |
348 | ||
349 | return 0; | |
350 | } | |
351 | ||
ac28b9f8 DD |
352 | /** |
353 | * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs. | |
354 | * @bus: the target MII bus | |
355 | * @addr: PHY address on the MII bus | |
356 | * @phy_id: where to store the ID retrieved. | |
357 | * @c45_ids: where to store the c45 ID information. | |
358 | * | |
359 | * If the PHY devices-in-package appears to be valid, it and the | |
360 | * corresponding identifiers are stored in @c45_ids, zero is stored | |
361 | * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns | |
362 | * zero on success. | |
363 | * | |
364 | */ | |
365 | static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id, | |
366 | struct phy_c45_device_ids *c45_ids) { | |
367 | int phy_reg; | |
368 | int i, reg_addr; | |
369 | const int num_ids = ARRAY_SIZE(c45_ids->device_ids); | |
5f6c99e0 | 370 | u32 *devs = &c45_ids->devices_in_package; |
ac28b9f8 | 371 | |
5f6c99e0 SX |
372 | /* Find first non-zero Devices In package. Device zero is reserved |
373 | * for 802.3 c45 complied PHYs, so don't probe it at first. | |
ac28b9f8 | 374 | */ |
5f6c99e0 SX |
375 | for (i = 1; i < num_ids && *devs == 0; i++) { |
376 | phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs); | |
ac28b9f8 DD |
377 | if (phy_reg < 0) |
378 | return -EIO; | |
ac28b9f8 | 379 | |
5f6c99e0 SX |
380 | if ((*devs & 0x1fffffff) == 0x1fffffff) { |
381 | /* If mostly Fs, there is no device there, | |
382 | * then let's continue to probe more, as some | |
383 | * 10G PHYs have zero Devices In package, | |
384 | * e.g. Cortina CS4315/CS4340 PHY. | |
385 | */ | |
386 | phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs); | |
387 | if (phy_reg < 0) | |
388 | return -EIO; | |
389 | /* no device there, let's get out of here */ | |
390 | if ((*devs & 0x1fffffff) == 0x1fffffff) { | |
da1da284 SL |
391 | *phy_id = 0xffffffff; |
392 | return 0; | |
5f6c99e0 SX |
393 | } else { |
394 | break; | |
da1da284 | 395 | } |
ac28b9f8 DD |
396 | } |
397 | } | |
398 | ||
399 | /* Now probe Device Identifiers for each device present. */ | |
400 | for (i = 1; i < num_ids; i++) { | |
401 | if (!(c45_ids->devices_in_package & (1 << i))) | |
402 | continue; | |
403 | ||
404 | reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1; | |
405 | phy_reg = mdiobus_read(bus, addr, reg_addr); | |
406 | if (phy_reg < 0) | |
407 | return -EIO; | |
408 | c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16; | |
409 | ||
410 | reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2; | |
411 | phy_reg = mdiobus_read(bus, addr, reg_addr); | |
412 | if (phy_reg < 0) | |
413 | return -EIO; | |
414 | c45_ids->device_ids[i] |= (phy_reg & 0xffff); | |
415 | } | |
416 | *phy_id = 0; | |
417 | return 0; | |
418 | } | |
11b0bacd | 419 | |
b3df0da8 | 420 | /** |
cac1f3c8 | 421 | * get_phy_id - reads the specified addr for its ID. |
b3df0da8 RD |
422 | * @bus: the target MII bus |
423 | * @addr: PHY address on the MII bus | |
cac1f3c8 | 424 | * @phy_id: where to store the ID retrieved. |
ac28b9f8 DD |
425 | * @is_c45: If true the PHY uses the 802.3 clause 45 protocol |
426 | * @c45_ids: where to store the c45 ID information. | |
427 | * | |
428 | * Description: In the case of a 802.3-c22 PHY, reads the ID registers | |
429 | * of the PHY at @addr on the @bus, stores it in @phy_id and returns | |
430 | * zero on success. | |
431 | * | |
432 | * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and | |
433 | * its return value is in turn returned. | |
00db8189 | 434 | * |
00db8189 | 435 | */ |
ac28b9f8 DD |
436 | static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id, |
437 | bool is_c45, struct phy_c45_device_ids *c45_ids) | |
00db8189 AF |
438 | { |
439 | int phy_reg; | |
00db8189 | 440 | |
ac28b9f8 DD |
441 | if (is_c45) |
442 | return get_phy_c45_ids(bus, addr, phy_id, c45_ids); | |
443 | ||
2f53e904 | 444 | /* Grab the bits from PHYIR1, and put them in the upper half */ |
6fe32649 | 445 | phy_reg = mdiobus_read(bus, addr, MII_PHYSID1); |
00db8189 | 446 | if (phy_reg < 0) |
cac1f3c8 | 447 | return -EIO; |
00db8189 | 448 | |
cac1f3c8 | 449 | *phy_id = (phy_reg & 0xffff) << 16; |
00db8189 AF |
450 | |
451 | /* Grab the bits from PHYIR2, and put them in the lower half */ | |
6fe32649 | 452 | phy_reg = mdiobus_read(bus, addr, MII_PHYSID2); |
00db8189 | 453 | if (phy_reg < 0) |
cac1f3c8 PG |
454 | return -EIO; |
455 | ||
456 | *phy_id |= (phy_reg & 0xffff); | |
457 | ||
458 | return 0; | |
459 | } | |
460 | ||
461 | /** | |
2f53e904 SS |
462 | * get_phy_device - reads the specified PHY device and returns its @phy_device |
463 | * struct | |
cac1f3c8 PG |
464 | * @bus: the target MII bus |
465 | * @addr: PHY address on the MII bus | |
ac28b9f8 | 466 | * @is_c45: If true the PHY uses the 802.3 clause 45 protocol |
cac1f3c8 PG |
467 | * |
468 | * Description: Reads the ID registers of the PHY at @addr on the | |
469 | * @bus, then allocates and returns the phy_device to represent it. | |
470 | */ | |
ac28b9f8 | 471 | struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45) |
cac1f3c8 | 472 | { |
ac28b9f8 | 473 | struct phy_c45_device_ids c45_ids = {0}; |
160c85f0 | 474 | u32 phy_id = 0; |
cac1f3c8 | 475 | int r; |
00db8189 | 476 | |
ac28b9f8 | 477 | r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids); |
cac1f3c8 PG |
478 | if (r) |
479 | return ERR_PTR(r); | |
00db8189 | 480 | |
6436cbcd GC |
481 | /* If the phy_id is mostly Fs, there is no device there */ |
482 | if ((phy_id & 0x1fffffff) == 0x1fffffff) | |
483 | return NULL; | |
484 | ||
e62a768f | 485 | return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids); |
00db8189 | 486 | } |
4dea547f GL |
487 | EXPORT_SYMBOL(get_phy_device); |
488 | ||
5cf11bee AL |
489 | static ssize_t |
490 | phy_id_show(struct device *dev, struct device_attribute *attr, char *buf) | |
491 | { | |
492 | struct phy_device *phydev = to_phy_device(dev); | |
493 | ||
494 | return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id); | |
495 | } | |
496 | static DEVICE_ATTR_RO(phy_id); | |
497 | ||
498 | static ssize_t | |
499 | phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf) | |
500 | { | |
501 | struct phy_device *phydev = to_phy_device(dev); | |
502 | const char *mode = NULL; | |
503 | ||
504 | if (phy_is_internal(phydev)) | |
505 | mode = "internal"; | |
506 | else | |
507 | mode = phy_modes(phydev->interface); | |
508 | ||
509 | return sprintf(buf, "%s\n", mode); | |
510 | } | |
511 | static DEVICE_ATTR_RO(phy_interface); | |
512 | ||
513 | static ssize_t | |
514 | phy_has_fixups_show(struct device *dev, struct device_attribute *attr, | |
515 | char *buf) | |
516 | { | |
517 | struct phy_device *phydev = to_phy_device(dev); | |
518 | ||
519 | return sprintf(buf, "%d\n", phydev->has_fixups); | |
520 | } | |
521 | static DEVICE_ATTR_RO(phy_has_fixups); | |
522 | ||
523 | static struct attribute *phy_dev_attrs[] = { | |
524 | &dev_attr_phy_id.attr, | |
525 | &dev_attr_phy_interface.attr, | |
526 | &dev_attr_phy_has_fixups.attr, | |
527 | NULL, | |
528 | }; | |
529 | ATTRIBUTE_GROUPS(phy_dev); | |
530 | ||
4dea547f GL |
531 | /** |
532 | * phy_device_register - Register the phy device on the MDIO bus | |
1d4ac5d5 | 533 | * @phydev: phy_device structure to be added to the MDIO bus |
4dea547f GL |
534 | */ |
535 | int phy_device_register(struct phy_device *phydev) | |
536 | { | |
537 | int err; | |
538 | ||
7f854420 AL |
539 | err = mdiobus_register_device(&phydev->mdio); |
540 | if (err) | |
541 | return err; | |
4dea547f GL |
542 | |
543 | /* Run all of the fixups for this PHY */ | |
d92f5dec | 544 | err = phy_scan_fixups(phydev); |
87aa9f9c | 545 | if (err) { |
e5a03bfd | 546 | pr_err("PHY %d failed to initialize\n", phydev->mdio.addr); |
87aa9f9c FF |
547 | goto out; |
548 | } | |
4dea547f | 549 | |
5cf11bee AL |
550 | phydev->mdio.dev.groups = phy_dev_groups; |
551 | ||
e5a03bfd | 552 | err = device_add(&phydev->mdio.dev); |
4dea547f | 553 | if (err) { |
e5a03bfd | 554 | pr_err("PHY %d failed to add\n", phydev->mdio.addr); |
4dea547f GL |
555 | goto out; |
556 | } | |
557 | ||
558 | return 0; | |
559 | ||
560 | out: | |
7f854420 | 561 | mdiobus_unregister_device(&phydev->mdio); |
4dea547f GL |
562 | return err; |
563 | } | |
564 | EXPORT_SYMBOL(phy_device_register); | |
00db8189 | 565 | |
38737e49 RK |
566 | /** |
567 | * phy_device_remove - Remove a previously registered phy device from the MDIO bus | |
568 | * @phydev: phy_device structure to remove | |
569 | * | |
570 | * This doesn't free the phy_device itself, it merely reverses the effects | |
571 | * of phy_device_register(). Use phy_device_free() to free the device | |
572 | * after calling this function. | |
573 | */ | |
574 | void phy_device_remove(struct phy_device *phydev) | |
575 | { | |
e5a03bfd | 576 | device_del(&phydev->mdio.dev); |
7f854420 | 577 | mdiobus_unregister_device(&phydev->mdio); |
38737e49 RK |
578 | } |
579 | EXPORT_SYMBOL(phy_device_remove); | |
580 | ||
f8f76db1 JP |
581 | /** |
582 | * phy_find_first - finds the first PHY device on the bus | |
583 | * @bus: the target MII bus | |
584 | */ | |
585 | struct phy_device *phy_find_first(struct mii_bus *bus) | |
586 | { | |
7f854420 | 587 | struct phy_device *phydev; |
f8f76db1 JP |
588 | int addr; |
589 | ||
590 | for (addr = 0; addr < PHY_MAX_ADDR; addr++) { | |
7f854420 AL |
591 | phydev = mdiobus_get_phy(bus, addr); |
592 | if (phydev) | |
593 | return phydev; | |
f8f76db1 JP |
594 | } |
595 | return NULL; | |
596 | } | |
597 | EXPORT_SYMBOL(phy_find_first); | |
598 | ||
b3df0da8 RD |
599 | /** |
600 | * phy_prepare_link - prepares the PHY layer to monitor link status | |
601 | * @phydev: target phy_device struct | |
602 | * @handler: callback function for link status change notifications | |
00db8189 | 603 | * |
b3df0da8 | 604 | * Description: Tells the PHY infrastructure to handle the |
00db8189 AF |
605 | * gory details on monitoring link status (whether through |
606 | * polling or an interrupt), and to call back to the | |
607 | * connected device driver when the link status changes. | |
608 | * If you want to monitor your own link state, don't call | |
b3df0da8 RD |
609 | * this function. |
610 | */ | |
89ff05ec | 611 | static void phy_prepare_link(struct phy_device *phydev, |
2f53e904 | 612 | void (*handler)(struct net_device *)) |
00db8189 AF |
613 | { |
614 | phydev->adjust_link = handler; | |
615 | } | |
616 | ||
fa94f6d9 GL |
617 | /** |
618 | * phy_connect_direct - connect an ethernet device to a specific phy_device | |
619 | * @dev: the network device to connect | |
620 | * @phydev: the pointer to the phy device | |
621 | * @handler: callback function for state change notifications | |
fa94f6d9 GL |
622 | * @interface: PHY device's interface |
623 | */ | |
624 | int phy_connect_direct(struct net_device *dev, struct phy_device *phydev, | |
f9a8f83b | 625 | void (*handler)(struct net_device *), |
fa94f6d9 GL |
626 | phy_interface_t interface) |
627 | { | |
628 | int rc; | |
629 | ||
f9a8f83b | 630 | rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); |
fa94f6d9 GL |
631 | if (rc) |
632 | return rc; | |
633 | ||
634 | phy_prepare_link(phydev, handler); | |
29935aeb | 635 | phy_start_machine(phydev); |
fa94f6d9 GL |
636 | if (phydev->irq > 0) |
637 | phy_start_interrupts(phydev); | |
638 | ||
639 | return 0; | |
640 | } | |
641 | EXPORT_SYMBOL(phy_connect_direct); | |
642 | ||
b3df0da8 RD |
643 | /** |
644 | * phy_connect - connect an ethernet device to a PHY device | |
645 | * @dev: the network device to connect | |
5d12b132 | 646 | * @bus_id: the id string of the PHY device to connect |
b3df0da8 | 647 | * @handler: callback function for state change notifications |
b3df0da8 | 648 | * @interface: PHY device's interface |
e1393456 | 649 | * |
b3df0da8 | 650 | * Description: Convenience function for connecting ethernet |
e1393456 AF |
651 | * devices to PHY devices. The default behavior is for |
652 | * the PHY infrastructure to handle everything, and only notify | |
653 | * the connected driver when the link status changes. If you | |
654 | * don't want, or can't use the provided functionality, you may | |
655 | * choose to call only the subset of functions which provide | |
656 | * the desired functionality. | |
657 | */ | |
e109374f | 658 | struct phy_device *phy_connect(struct net_device *dev, const char *bus_id, |
2f53e904 SS |
659 | void (*handler)(struct net_device *), |
660 | phy_interface_t interface) | |
e1393456 AF |
661 | { |
662 | struct phy_device *phydev; | |
fa94f6d9 GL |
663 | struct device *d; |
664 | int rc; | |
e1393456 | 665 | |
fa94f6d9 | 666 | /* Search the list of PHY devices on the mdio bus for the |
2f53e904 SS |
667 | * PHY with the requested name |
668 | */ | |
fa94f6d9 GL |
669 | d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id); |
670 | if (!d) { | |
671 | pr_err("PHY %s not found\n", bus_id); | |
672 | return ERR_PTR(-ENODEV); | |
673 | } | |
674 | phydev = to_phy_device(d); | |
e1393456 | 675 | |
f9a8f83b | 676 | rc = phy_connect_direct(dev, phydev, handler, interface); |
fa94f6d9 GL |
677 | if (rc) |
678 | return ERR_PTR(rc); | |
e1393456 AF |
679 | |
680 | return phydev; | |
681 | } | |
682 | EXPORT_SYMBOL(phy_connect); | |
683 | ||
b3df0da8 | 684 | /** |
2f53e904 SS |
685 | * phy_disconnect - disable interrupts, stop state machine, and detach a PHY |
686 | * device | |
b3df0da8 RD |
687 | * @phydev: target phy_device struct |
688 | */ | |
e1393456 AF |
689 | void phy_disconnect(struct phy_device *phydev) |
690 | { | |
691 | if (phydev->irq > 0) | |
692 | phy_stop_interrupts(phydev); | |
693 | ||
694 | phy_stop_machine(phydev); | |
e109374f | 695 | |
e1393456 AF |
696 | phydev->adjust_link = NULL; |
697 | ||
698 | phy_detach(phydev); | |
699 | } | |
700 | EXPORT_SYMBOL(phy_disconnect); | |
701 | ||
87aa9f9c FF |
702 | /** |
703 | * phy_poll_reset - Safely wait until a PHY reset has properly completed | |
704 | * @phydev: The PHY device to poll | |
705 | * | |
706 | * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as | |
707 | * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR | |
708 | * register must be polled until the BMCR_RESET bit clears. | |
709 | * | |
710 | * Furthermore, any attempts to write to PHY registers may have no effect | |
711 | * or even generate MDIO bus errors until this is complete. | |
712 | * | |
713 | * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the | |
714 | * standard and do not fully reset after the BMCR_RESET bit is set, and may | |
715 | * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an | |
716 | * effort to support such broken PHYs, this function is separate from the | |
717 | * standard phy_init_hw() which will zero all the other bits in the BMCR | |
718 | * and reapply all driver-specific and board-specific fixups. | |
719 | */ | |
720 | static int phy_poll_reset(struct phy_device *phydev) | |
721 | { | |
722 | /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */ | |
723 | unsigned int retries = 12; | |
724 | int ret; | |
725 | ||
726 | do { | |
727 | msleep(50); | |
728 | ret = phy_read(phydev, MII_BMCR); | |
729 | if (ret < 0) | |
730 | return ret; | |
731 | } while (ret & BMCR_RESET && --retries); | |
732 | if (ret & BMCR_RESET) | |
733 | return -ETIMEDOUT; | |
734 | ||
2f53e904 | 735 | /* Some chips (smsc911x) may still need up to another 1ms after the |
87aa9f9c FF |
736 | * BMCR_RESET bit is cleared before they are usable. |
737 | */ | |
738 | msleep(1); | |
739 | return 0; | |
740 | } | |
741 | ||
2f5cb434 AV |
742 | int phy_init_hw(struct phy_device *phydev) |
743 | { | |
9df81dd7 | 744 | int ret = 0; |
2f5cb434 AV |
745 | |
746 | if (!phydev->drv || !phydev->drv->config_init) | |
747 | return 0; | |
748 | ||
9df81dd7 FF |
749 | if (phydev->drv->soft_reset) |
750 | ret = phydev->drv->soft_reset(phydev); | |
751 | else | |
752 | ret = genphy_soft_reset(phydev); | |
753 | ||
87aa9f9c FF |
754 | if (ret < 0) |
755 | return ret; | |
756 | ||
2f5cb434 AV |
757 | ret = phy_scan_fixups(phydev); |
758 | if (ret < 0) | |
759 | return ret; | |
760 | ||
761 | return phydev->drv->config_init(phydev); | |
762 | } | |
87aa9f9c | 763 | EXPORT_SYMBOL(phy_init_hw); |
2f5cb434 | 764 | |
2220943a AL |
765 | void phy_attached_info(struct phy_device *phydev) |
766 | { | |
767 | phy_attached_print(phydev, NULL); | |
768 | } | |
769 | EXPORT_SYMBOL(phy_attached_info); | |
770 | ||
771 | #define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)" | |
772 | void phy_attached_print(struct phy_device *phydev, const char *fmt, ...) | |
773 | { | |
774 | if (!fmt) { | |
e5a03bfd | 775 | dev_info(&phydev->mdio.dev, ATTACHED_FMT "\n", |
2220943a AL |
776 | phydev->drv->name, phydev_name(phydev), |
777 | phydev->irq); | |
778 | } else { | |
779 | va_list ap; | |
780 | ||
e5a03bfd | 781 | dev_info(&phydev->mdio.dev, ATTACHED_FMT, |
2220943a AL |
782 | phydev->drv->name, phydev_name(phydev), |
783 | phydev->irq); | |
784 | ||
785 | va_start(ap, fmt); | |
786 | vprintk(fmt, ap); | |
787 | va_end(ap); | |
788 | } | |
789 | } | |
790 | EXPORT_SYMBOL(phy_attached_print); | |
791 | ||
b3df0da8 | 792 | /** |
fa94f6d9 | 793 | * phy_attach_direct - attach a network device to a given PHY device pointer |
b3df0da8 | 794 | * @dev: network device to attach |
fa94f6d9 | 795 | * @phydev: Pointer to phy_device to attach |
b3df0da8 RD |
796 | * @flags: PHY device's dev_flags |
797 | * @interface: PHY device's interface | |
e1393456 | 798 | * |
b3df0da8 | 799 | * Description: Called by drivers to attach to a particular PHY |
e1393456 | 800 | * device. The phy_device is found, and properly hooked up |
257184d7 AF |
801 | * to the phy_driver. If no driver is attached, then a |
802 | * generic driver is used. The phy_device is given a ptr to | |
e1393456 | 803 | * the attaching device, and given a callback for link status |
b3df0da8 | 804 | * change. The phy_device is returned to the attaching driver. |
7322967b | 805 | * This function takes a reference on the phy device. |
e1393456 | 806 | */ |
257184d7 AF |
807 | int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, |
808 | u32 flags, phy_interface_t interface) | |
e1393456 | 809 | { |
e5a03bfd AL |
810 | struct mii_bus *bus = phydev->mdio.bus; |
811 | struct device *d = &phydev->mdio.dev; | |
d005a09e | 812 | int err; |
e1393456 | 813 | |
3e3aaf64 RK |
814 | if (!try_module_get(bus->owner)) { |
815 | dev_err(&dev->dev, "failed to get the bus module\n"); | |
816 | return -EIO; | |
817 | } | |
818 | ||
7322967b RK |
819 | get_device(d); |
820 | ||
e1393456 | 821 | /* Assume that if there is no driver, that it doesn't |
2f53e904 SS |
822 | * exist, and we should use the genphy driver. |
823 | */ | |
ef899c07 | 824 | if (!d->driver) { |
257184d7 AF |
825 | if (phydev->is_c45) |
826 | d->driver = &genphy_driver[GENPHY_DRV_10G].driver; | |
827 | else | |
828 | d->driver = &genphy_driver[GENPHY_DRV_1G].driver; | |
e1393456 AF |
829 | |
830 | err = d->driver->probe(d); | |
b7a00ecd JG |
831 | if (err >= 0) |
832 | err = device_bind_driver(d); | |
e1393456 | 833 | |
b7a00ecd | 834 | if (err) |
3e3aaf64 | 835 | goto error; |
e1393456 AF |
836 | } |
837 | ||
838 | if (phydev->attached_dev) { | |
fa94f6d9 | 839 | dev_err(&dev->dev, "PHY already attached\n"); |
3e3aaf64 RK |
840 | err = -EBUSY; |
841 | goto error; | |
b3565f27 EG |
842 | } |
843 | ||
e1393456 | 844 | phydev->attached_dev = dev; |
c1f19b51 | 845 | dev->phydev = phydev; |
e1393456 AF |
846 | |
847 | phydev->dev_flags = flags; | |
848 | ||
e8a2b6a4 AF |
849 | phydev->interface = interface; |
850 | ||
ef24b16b AV |
851 | phydev->state = PHY_READY; |
852 | ||
e8a2b6a4 AF |
853 | /* Do initial configuration here, now that |
854 | * we have certain key parameters | |
2f53e904 SS |
855 | * (dev_flags and interface) |
856 | */ | |
d005a09e MKB |
857 | err = phy_init_hw(phydev); |
858 | if (err) | |
859 | phy_detach(phydev); | |
b394745d GR |
860 | else |
861 | phy_resume(phydev); | |
1211ce53 | 862 | |
d005a09e | 863 | return err; |
3e3aaf64 RK |
864 | |
865 | error: | |
7322967b | 866 | put_device(d); |
3e3aaf64 RK |
867 | module_put(bus->owner); |
868 | return err; | |
fa94f6d9 | 869 | } |
257184d7 | 870 | EXPORT_SYMBOL(phy_attach_direct); |
fa94f6d9 GL |
871 | |
872 | /** | |
873 | * phy_attach - attach a network device to a particular PHY device | |
874 | * @dev: network device to attach | |
875 | * @bus_id: Bus ID of PHY device to attach | |
fa94f6d9 GL |
876 | * @interface: PHY device's interface |
877 | * | |
878 | * Description: Same as phy_attach_direct() except that a PHY bus_id | |
879 | * string is passed instead of a pointer to a struct phy_device. | |
880 | */ | |
2f53e904 SS |
881 | struct phy_device *phy_attach(struct net_device *dev, const char *bus_id, |
882 | phy_interface_t interface) | |
fa94f6d9 GL |
883 | { |
884 | struct bus_type *bus = &mdio_bus_type; | |
885 | struct phy_device *phydev; | |
886 | struct device *d; | |
887 | int rc; | |
888 | ||
889 | /* Search the list of PHY devices on the mdio bus for the | |
2f53e904 SS |
890 | * PHY with the requested name |
891 | */ | |
fa94f6d9 GL |
892 | d = bus_find_device_by_name(bus, NULL, bus_id); |
893 | if (!d) { | |
894 | pr_err("PHY %s not found\n", bus_id); | |
895 | return ERR_PTR(-ENODEV); | |
e8a2b6a4 | 896 | } |
fa94f6d9 GL |
897 | phydev = to_phy_device(d); |
898 | ||
f9a8f83b | 899 | rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); |
fa94f6d9 GL |
900 | if (rc) |
901 | return ERR_PTR(rc); | |
e8a2b6a4 | 902 | |
e1393456 AF |
903 | return phydev; |
904 | } | |
905 | EXPORT_SYMBOL(phy_attach); | |
906 | ||
b3df0da8 RD |
907 | /** |
908 | * phy_detach - detach a PHY device from its network device | |
909 | * @phydev: target phy_device struct | |
7322967b RK |
910 | * |
911 | * This detaches the phy device from its network device and the phy | |
912 | * driver, and drops the reference count taken in phy_attach_direct(). | |
b3df0da8 | 913 | */ |
e1393456 AF |
914 | void phy_detach(struct phy_device *phydev) |
915 | { | |
3e3aaf64 | 916 | struct mii_bus *bus; |
ab2145ed | 917 | int i; |
b3565f27 | 918 | |
c1f19b51 | 919 | phydev->attached_dev->phydev = NULL; |
e1393456 | 920 | phydev->attached_dev = NULL; |
1211ce53 | 921 | phy_suspend(phydev); |
e1393456 AF |
922 | |
923 | /* If the device had no specific driver before (i.e. - it | |
924 | * was using the generic driver), we unbind the device | |
925 | * from the generic driver so that there's a chance a | |
2f53e904 SS |
926 | * real driver could be loaded |
927 | */ | |
ab2145ed | 928 | for (i = 0; i < ARRAY_SIZE(genphy_driver); i++) { |
e5a03bfd AL |
929 | if (phydev->mdio.dev.driver == &genphy_driver[i].driver) { |
930 | device_release_driver(&phydev->mdio.dev); | |
ab2145ed SX |
931 | break; |
932 | } | |
933 | } | |
3e3aaf64 | 934 | |
7322967b RK |
935 | /* |
936 | * The phydev might go away on the put_device() below, so avoid | |
937 | * a use-after-free bug by reading the underlying bus first. | |
938 | */ | |
e5a03bfd | 939 | bus = phydev->mdio.bus; |
3e3aaf64 | 940 | |
e5a03bfd | 941 | put_device(&phydev->mdio.dev); |
3e3aaf64 | 942 | module_put(bus->owner); |
e1393456 AF |
943 | } |
944 | EXPORT_SYMBOL(phy_detach); | |
945 | ||
481b5d93 SH |
946 | int phy_suspend(struct phy_device *phydev) |
947 | { | |
e5a03bfd | 948 | struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver); |
32fc3fd4 | 949 | struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; |
8a477a6f | 950 | int ret = 0; |
481b5d93 SH |
951 | |
952 | /* If the device has WOL enabled, we cannot suspend the PHY */ | |
481b5d93 SH |
953 | phy_ethtool_get_wol(phydev, &wol); |
954 | if (wol.wolopts) | |
955 | return -EBUSY; | |
956 | ||
957 | if (phydrv->suspend) | |
8a477a6f FF |
958 | ret = phydrv->suspend(phydev); |
959 | ||
960 | if (ret) | |
961 | return ret; | |
962 | ||
963 | phydev->suspended = true; | |
964 | ||
965 | return ret; | |
481b5d93 | 966 | } |
ca127697 | 967 | EXPORT_SYMBOL(phy_suspend); |
481b5d93 SH |
968 | |
969 | int phy_resume(struct phy_device *phydev) | |
970 | { | |
e5a03bfd | 971 | struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver); |
8a477a6f | 972 | int ret = 0; |
481b5d93 SH |
973 | |
974 | if (phydrv->resume) | |
8a477a6f FF |
975 | ret = phydrv->resume(phydev); |
976 | ||
977 | if (ret) | |
978 | return ret; | |
979 | ||
980 | phydev->suspended = false; | |
981 | ||
982 | return ret; | |
481b5d93 | 983 | } |
ca127697 | 984 | EXPORT_SYMBOL(phy_resume); |
e1393456 | 985 | |
00db8189 AF |
986 | /* Generic PHY support and helper functions */ |
987 | ||
b3df0da8 | 988 | /** |
25985edc | 989 | * genphy_config_advert - sanitize and advertise auto-negotiation parameters |
b3df0da8 | 990 | * @phydev: target phy_device struct |
00db8189 | 991 | * |
b3df0da8 | 992 | * Description: Writes MII_ADVERTISE with the appropriate values, |
00db8189 | 993 | * after sanitizing the values to make sure we only advertise |
51e2a384 TP |
994 | * what is supported. Returns < 0 on error, 0 if the PHY's advertisement |
995 | * hasn't changed, and > 0 if it has changed. | |
00db8189 | 996 | */ |
89ff05ec | 997 | static int genphy_config_advert(struct phy_device *phydev) |
00db8189 AF |
998 | { |
999 | u32 advertise; | |
5273e3a5 | 1000 | int oldadv, adv, bmsr; |
51e2a384 | 1001 | int err, changed = 0; |
00db8189 | 1002 | |
2f53e904 | 1003 | /* Only allow advertising what this PHY supports */ |
00db8189 AF |
1004 | phydev->advertising &= phydev->supported; |
1005 | advertise = phydev->advertising; | |
1006 | ||
1007 | /* Setup standard advertisement */ | |
2f53e904 | 1008 | adv = phy_read(phydev, MII_ADVERTISE); |
00db8189 AF |
1009 | if (adv < 0) |
1010 | return adv; | |
1011 | ||
2f53e904 | 1012 | oldadv = adv; |
28011cf1 | 1013 | adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | |
00db8189 | 1014 | ADVERTISE_PAUSE_ASYM); |
37f07023 | 1015 | adv |= ethtool_adv_to_mii_adv_t(advertise); |
00db8189 | 1016 | |
51e2a384 TP |
1017 | if (adv != oldadv) { |
1018 | err = phy_write(phydev, MII_ADVERTISE, adv); | |
00db8189 | 1019 | |
51e2a384 TP |
1020 | if (err < 0) |
1021 | return err; | |
1022 | changed = 1; | |
1023 | } | |
00db8189 | 1024 | |
5273e3a5 FF |
1025 | bmsr = phy_read(phydev, MII_BMSR); |
1026 | if (bmsr < 0) | |
1027 | return bmsr; | |
1028 | ||
1029 | /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all | |
1030 | * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a | |
1031 | * logical 1. | |
1032 | */ | |
1033 | if (!(bmsr & BMSR_ESTATEN)) | |
1034 | return changed; | |
1035 | ||
00db8189 | 1036 | /* Configure gigabit if it's supported */ |
5273e3a5 FF |
1037 | adv = phy_read(phydev, MII_CTRL1000); |
1038 | if (adv < 0) | |
1039 | return adv; | |
1040 | ||
1041 | oldadv = adv; | |
1042 | adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); | |
1043 | ||
00db8189 | 1044 | if (phydev->supported & (SUPPORTED_1000baseT_Half | |
2f53e904 | 1045 | SUPPORTED_1000baseT_Full)) { |
37f07023 | 1046 | adv |= ethtool_adv_to_mii_ctrl1000_t(advertise); |
00db8189 AF |
1047 | } |
1048 | ||
eb686231 M |
1049 | if (adv != oldadv) |
1050 | changed = 1; | |
1051 | ||
5273e3a5 FF |
1052 | err = phy_write(phydev, MII_CTRL1000, adv); |
1053 | if (err < 0) | |
1054 | return err; | |
1055 | ||
51e2a384 | 1056 | return changed; |
00db8189 | 1057 | } |
00db8189 | 1058 | |
b3df0da8 RD |
1059 | /** |
1060 | * genphy_setup_forced - configures/forces speed/duplex from @phydev | |
1061 | * @phydev: target phy_device struct | |
00db8189 | 1062 | * |
b3df0da8 | 1063 | * Description: Configures MII_BMCR to force speed/duplex |
00db8189 | 1064 | * to the values in phydev. Assumes that the values are valid. |
b3df0da8 RD |
1065 | * Please see phy_sanitize_settings(). |
1066 | */ | |
3fb69bca | 1067 | int genphy_setup_forced(struct phy_device *phydev) |
00db8189 | 1068 | { |
bc1e0a09 | 1069 | int ctl = 0; |
00db8189 | 1070 | |
2f53e904 SS |
1071 | phydev->pause = 0; |
1072 | phydev->asym_pause = 0; | |
00db8189 AF |
1073 | |
1074 | if (SPEED_1000 == phydev->speed) | |
1075 | ctl |= BMCR_SPEED1000; | |
1076 | else if (SPEED_100 == phydev->speed) | |
1077 | ctl |= BMCR_SPEED100; | |
1078 | ||
1079 | if (DUPLEX_FULL == phydev->duplex) | |
1080 | ctl |= BMCR_FULLDPLX; | |
e109374f | 1081 | |
e62a768f | 1082 | return phy_write(phydev, MII_BMCR, ctl); |
00db8189 | 1083 | } |
3fb69bca | 1084 | EXPORT_SYMBOL(genphy_setup_forced); |
00db8189 | 1085 | |
b3df0da8 RD |
1086 | /** |
1087 | * genphy_restart_aneg - Enable and Restart Autonegotiation | |
1088 | * @phydev: target phy_device struct | |
1089 | */ | |
00db8189 AF |
1090 | int genphy_restart_aneg(struct phy_device *phydev) |
1091 | { | |
553fe92b | 1092 | int ctl = phy_read(phydev, MII_BMCR); |
00db8189 AF |
1093 | |
1094 | if (ctl < 0) | |
1095 | return ctl; | |
1096 | ||
2f53e904 | 1097 | ctl |= BMCR_ANENABLE | BMCR_ANRESTART; |
00db8189 AF |
1098 | |
1099 | /* Don't isolate the PHY if we're negotiating */ | |
2f53e904 | 1100 | ctl &= ~BMCR_ISOLATE; |
00db8189 | 1101 | |
553fe92b | 1102 | return phy_write(phydev, MII_BMCR, ctl); |
00db8189 | 1103 | } |
892871dc | 1104 | EXPORT_SYMBOL(genphy_restart_aneg); |
00db8189 | 1105 | |
b3df0da8 RD |
1106 | /** |
1107 | * genphy_config_aneg - restart auto-negotiation or write BMCR | |
1108 | * @phydev: target phy_device struct | |
00db8189 | 1109 | * |
b3df0da8 | 1110 | * Description: If auto-negotiation is enabled, we configure the |
00db8189 | 1111 | * advertising, and then restart auto-negotiation. If it is not |
b3df0da8 | 1112 | * enabled, then we write the BMCR. |
00db8189 AF |
1113 | */ |
1114 | int genphy_config_aneg(struct phy_device *phydev) | |
1115 | { | |
de339c2a | 1116 | int result; |
00db8189 | 1117 | |
de339c2a TP |
1118 | if (AUTONEG_ENABLE != phydev->autoneg) |
1119 | return genphy_setup_forced(phydev); | |
00db8189 | 1120 | |
de339c2a | 1121 | result = genphy_config_advert(phydev); |
de339c2a TP |
1122 | if (result < 0) /* error */ |
1123 | return result; | |
de339c2a | 1124 | if (result == 0) { |
25985edc | 1125 | /* Advertisement hasn't changed, but maybe aneg was never on to |
2f53e904 SS |
1126 | * begin with? Or maybe phy was isolated? |
1127 | */ | |
de339c2a TP |
1128 | int ctl = phy_read(phydev, MII_BMCR); |
1129 | ||
1130 | if (ctl < 0) | |
1131 | return ctl; | |
1132 | ||
1133 | if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) | |
1134 | result = 1; /* do restart aneg */ | |
1135 | } | |
1136 | ||
1137 | /* Only restart aneg if we are advertising something different | |
2f53e904 SS |
1138 | * than we were before. |
1139 | */ | |
de339c2a TP |
1140 | if (result > 0) |
1141 | result = genphy_restart_aneg(phydev); | |
00db8189 | 1142 | |
51e2a384 | 1143 | return result; |
00db8189 AF |
1144 | } |
1145 | EXPORT_SYMBOL(genphy_config_aneg); | |
1146 | ||
a9fa6e6a FF |
1147 | /** |
1148 | * genphy_aneg_done - return auto-negotiation status | |
1149 | * @phydev: target phy_device struct | |
1150 | * | |
1151 | * Description: Reads the status register and returns 0 either if | |
1152 | * auto-negotiation is incomplete, or if there was an error. | |
1153 | * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done. | |
1154 | */ | |
1155 | int genphy_aneg_done(struct phy_device *phydev) | |
1156 | { | |
1157 | int retval = phy_read(phydev, MII_BMSR); | |
1158 | ||
1159 | return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE); | |
1160 | } | |
1161 | EXPORT_SYMBOL(genphy_aneg_done); | |
1162 | ||
395056ed | 1163 | static int gen10g_config_aneg(struct phy_device *phydev) |
124059fd AF |
1164 | { |
1165 | return 0; | |
1166 | } | |
124059fd | 1167 | |
b3df0da8 RD |
1168 | /** |
1169 | * genphy_update_link - update link status in @phydev | |
1170 | * @phydev: target phy_device struct | |
00db8189 | 1171 | * |
b3df0da8 | 1172 | * Description: Update the value in phydev->link to reflect the |
00db8189 | 1173 | * current link value. In order to do this, we need to read |
b3df0da8 | 1174 | * the status register twice, keeping the second value. |
00db8189 AF |
1175 | */ |
1176 | int genphy_update_link(struct phy_device *phydev) | |
1177 | { | |
1178 | int status; | |
1179 | ||
1180 | /* Do a fake read */ | |
1181 | status = phy_read(phydev, MII_BMSR); | |
00db8189 AF |
1182 | if (status < 0) |
1183 | return status; | |
1184 | ||
1185 | /* Read link and autonegotiation status */ | |
1186 | status = phy_read(phydev, MII_BMSR); | |
00db8189 AF |
1187 | if (status < 0) |
1188 | return status; | |
1189 | ||
1190 | if ((status & BMSR_LSTATUS) == 0) | |
1191 | phydev->link = 0; | |
1192 | else | |
1193 | phydev->link = 1; | |
1194 | ||
1195 | return 0; | |
1196 | } | |
6b655529 | 1197 | EXPORT_SYMBOL(genphy_update_link); |
00db8189 | 1198 | |
b3df0da8 RD |
1199 | /** |
1200 | * genphy_read_status - check the link status and update current link state | |
1201 | * @phydev: target phy_device struct | |
00db8189 | 1202 | * |
b3df0da8 | 1203 | * Description: Check the link, then figure out the current state |
00db8189 AF |
1204 | * by comparing what we advertise with what the link partner |
1205 | * advertises. Start by checking the gigabit possibilities, | |
1206 | * then move on to 10/100. | |
1207 | */ | |
1208 | int genphy_read_status(struct phy_device *phydev) | |
1209 | { | |
1210 | int adv; | |
1211 | int err; | |
1212 | int lpa; | |
1213 | int lpagb = 0; | |
a4572e0c CB |
1214 | int common_adv; |
1215 | int common_adv_gb = 0; | |
00db8189 | 1216 | |
2f53e904 | 1217 | /* Update the link, but return if there was an error */ |
00db8189 AF |
1218 | err = genphy_update_link(phydev); |
1219 | if (err) | |
1220 | return err; | |
1221 | ||
114002bc FF |
1222 | phydev->lp_advertising = 0; |
1223 | ||
00db8189 AF |
1224 | if (AUTONEG_ENABLE == phydev->autoneg) { |
1225 | if (phydev->supported & (SUPPORTED_1000baseT_Half | |
1226 | | SUPPORTED_1000baseT_Full)) { | |
1227 | lpagb = phy_read(phydev, MII_STAT1000); | |
00db8189 AF |
1228 | if (lpagb < 0) |
1229 | return lpagb; | |
1230 | ||
1231 | adv = phy_read(phydev, MII_CTRL1000); | |
00db8189 AF |
1232 | if (adv < 0) |
1233 | return adv; | |
1234 | ||
114002bc FF |
1235 | phydev->lp_advertising = |
1236 | mii_stat1000_to_ethtool_lpa_t(lpagb); | |
a4572e0c | 1237 | common_adv_gb = lpagb & adv << 2; |
00db8189 AF |
1238 | } |
1239 | ||
1240 | lpa = phy_read(phydev, MII_LPA); | |
00db8189 AF |
1241 | if (lpa < 0) |
1242 | return lpa; | |
1243 | ||
114002bc FF |
1244 | phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa); |
1245 | ||
00db8189 | 1246 | adv = phy_read(phydev, MII_ADVERTISE); |
00db8189 AF |
1247 | if (adv < 0) |
1248 | return adv; | |
1249 | ||
a4572e0c | 1250 | common_adv = lpa & adv; |
00db8189 AF |
1251 | |
1252 | phydev->speed = SPEED_10; | |
1253 | phydev->duplex = DUPLEX_HALF; | |
2f53e904 SS |
1254 | phydev->pause = 0; |
1255 | phydev->asym_pause = 0; | |
00db8189 | 1256 | |
a4572e0c | 1257 | if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) { |
00db8189 AF |
1258 | phydev->speed = SPEED_1000; |
1259 | ||
a4572e0c | 1260 | if (common_adv_gb & LPA_1000FULL) |
00db8189 | 1261 | phydev->duplex = DUPLEX_FULL; |
a4572e0c | 1262 | } else if (common_adv & (LPA_100FULL | LPA_100HALF)) { |
00db8189 | 1263 | phydev->speed = SPEED_100; |
e109374f | 1264 | |
a4572e0c | 1265 | if (common_adv & LPA_100FULL) |
00db8189 AF |
1266 | phydev->duplex = DUPLEX_FULL; |
1267 | } else | |
a4572e0c | 1268 | if (common_adv & LPA_10FULL) |
00db8189 AF |
1269 | phydev->duplex = DUPLEX_FULL; |
1270 | ||
e109374f | 1271 | if (phydev->duplex == DUPLEX_FULL) { |
00db8189 AF |
1272 | phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0; |
1273 | phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0; | |
1274 | } | |
1275 | } else { | |
1276 | int bmcr = phy_read(phydev, MII_BMCR); | |
2f53e904 | 1277 | |
00db8189 AF |
1278 | if (bmcr < 0) |
1279 | return bmcr; | |
1280 | ||
1281 | if (bmcr & BMCR_FULLDPLX) | |
1282 | phydev->duplex = DUPLEX_FULL; | |
1283 | else | |
1284 | phydev->duplex = DUPLEX_HALF; | |
1285 | ||
1286 | if (bmcr & BMCR_SPEED1000) | |
1287 | phydev->speed = SPEED_1000; | |
1288 | else if (bmcr & BMCR_SPEED100) | |
1289 | phydev->speed = SPEED_100; | |
1290 | else | |
1291 | phydev->speed = SPEED_10; | |
1292 | ||
2f53e904 SS |
1293 | phydev->pause = 0; |
1294 | phydev->asym_pause = 0; | |
00db8189 AF |
1295 | } |
1296 | ||
1297 | return 0; | |
1298 | } | |
1299 | EXPORT_SYMBOL(genphy_read_status); | |
1300 | ||
395056ed | 1301 | static int gen10g_read_status(struct phy_device *phydev) |
124059fd AF |
1302 | { |
1303 | int devad, reg; | |
1304 | u32 mmd_mask = phydev->c45_ids.devices_in_package; | |
1305 | ||
1306 | phydev->link = 1; | |
1307 | ||
1308 | /* For now just lie and say it's 10G all the time */ | |
1309 | phydev->speed = SPEED_10000; | |
1310 | phydev->duplex = DUPLEX_FULL; | |
1311 | ||
1312 | for (devad = 0; mmd_mask; devad++, mmd_mask = mmd_mask >> 1) { | |
1313 | if (!(mmd_mask & 1)) | |
1314 | continue; | |
1315 | ||
1316 | /* Read twice because link state is latched and a | |
1317 | * read moves the current state into the register | |
1318 | */ | |
1319 | phy_read_mmd(phydev, devad, MDIO_STAT1); | |
1320 | reg = phy_read_mmd(phydev, devad, MDIO_STAT1); | |
1321 | if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS)) | |
1322 | phydev->link = 0; | |
1323 | } | |
1324 | ||
1325 | return 0; | |
1326 | } | |
124059fd | 1327 | |
797ac071 FF |
1328 | /** |
1329 | * genphy_soft_reset - software reset the PHY via BMCR_RESET bit | |
1330 | * @phydev: target phy_device struct | |
1331 | * | |
1332 | * Description: Perform a software PHY reset using the standard | |
1333 | * BMCR_RESET bit and poll for the reset bit to be cleared. | |
1334 | * | |
1335 | * Returns: 0 on success, < 0 on failure | |
1336 | */ | |
1337 | int genphy_soft_reset(struct phy_device *phydev) | |
1338 | { | |
1339 | int ret; | |
1340 | ||
1341 | ret = phy_write(phydev, MII_BMCR, BMCR_RESET); | |
1342 | if (ret < 0) | |
1343 | return ret; | |
1344 | ||
1345 | return phy_poll_reset(phydev); | |
1346 | } | |
1347 | EXPORT_SYMBOL(genphy_soft_reset); | |
1348 | ||
af6b6967 | 1349 | int genphy_config_init(struct phy_device *phydev) |
00db8189 | 1350 | { |
84c22d79 | 1351 | int val; |
00db8189 AF |
1352 | u32 features; |
1353 | ||
00db8189 AF |
1354 | features = (SUPPORTED_TP | SUPPORTED_MII |
1355 | | SUPPORTED_AUI | SUPPORTED_FIBRE | | |
1356 | SUPPORTED_BNC); | |
1357 | ||
1358 | /* Do we support autonegotiation? */ | |
1359 | val = phy_read(phydev, MII_BMSR); | |
00db8189 AF |
1360 | if (val < 0) |
1361 | return val; | |
1362 | ||
1363 | if (val & BMSR_ANEGCAPABLE) | |
1364 | features |= SUPPORTED_Autoneg; | |
1365 | ||
1366 | if (val & BMSR_100FULL) | |
1367 | features |= SUPPORTED_100baseT_Full; | |
1368 | if (val & BMSR_100HALF) | |
1369 | features |= SUPPORTED_100baseT_Half; | |
1370 | if (val & BMSR_10FULL) | |
1371 | features |= SUPPORTED_10baseT_Full; | |
1372 | if (val & BMSR_10HALF) | |
1373 | features |= SUPPORTED_10baseT_Half; | |
1374 | ||
1375 | if (val & BMSR_ESTATEN) { | |
1376 | val = phy_read(phydev, MII_ESTATUS); | |
00db8189 AF |
1377 | if (val < 0) |
1378 | return val; | |
1379 | ||
1380 | if (val & ESTATUS_1000_TFULL) | |
1381 | features |= SUPPORTED_1000baseT_Full; | |
1382 | if (val & ESTATUS_1000_THALF) | |
1383 | features |= SUPPORTED_1000baseT_Half; | |
1384 | } | |
1385 | ||
c242a472 SH |
1386 | phydev->supported &= features; |
1387 | phydev->advertising &= features; | |
00db8189 AF |
1388 | |
1389 | return 0; | |
1390 | } | |
124059fd | 1391 | |
9df81dd7 FF |
1392 | static int gen10g_soft_reset(struct phy_device *phydev) |
1393 | { | |
1394 | /* Do nothing for now */ | |
1395 | return 0; | |
1396 | } | |
af6b6967 | 1397 | EXPORT_SYMBOL(genphy_config_init); |
9df81dd7 | 1398 | |
124059fd AF |
1399 | static int gen10g_config_init(struct phy_device *phydev) |
1400 | { | |
1401 | /* Temporarily just say we support everything */ | |
1402 | phydev->supported = SUPPORTED_10000baseT_Full; | |
1403 | phydev->advertising = SUPPORTED_10000baseT_Full; | |
1404 | ||
1405 | return 0; | |
1406 | } | |
1407 | ||
0f0ca340 GC |
1408 | int genphy_suspend(struct phy_device *phydev) |
1409 | { | |
1410 | int value; | |
1411 | ||
1412 | mutex_lock(&phydev->lock); | |
1413 | ||
1414 | value = phy_read(phydev, MII_BMCR); | |
2f53e904 | 1415 | phy_write(phydev, MII_BMCR, value | BMCR_PDOWN); |
0f0ca340 GC |
1416 | |
1417 | mutex_unlock(&phydev->lock); | |
1418 | ||
1419 | return 0; | |
1420 | } | |
1421 | EXPORT_SYMBOL(genphy_suspend); | |
00db8189 | 1422 | |
395056ed | 1423 | static int gen10g_suspend(struct phy_device *phydev) |
124059fd AF |
1424 | { |
1425 | return 0; | |
1426 | } | |
124059fd | 1427 | |
0f0ca340 GC |
1428 | int genphy_resume(struct phy_device *phydev) |
1429 | { | |
1430 | int value; | |
1431 | ||
1432 | mutex_lock(&phydev->lock); | |
1433 | ||
1434 | value = phy_read(phydev, MII_BMCR); | |
2f53e904 | 1435 | phy_write(phydev, MII_BMCR, value & ~BMCR_PDOWN); |
0f0ca340 GC |
1436 | |
1437 | mutex_unlock(&phydev->lock); | |
1438 | ||
1439 | return 0; | |
1440 | } | |
1441 | EXPORT_SYMBOL(genphy_resume); | |
00db8189 | 1442 | |
395056ed | 1443 | static int gen10g_resume(struct phy_device *phydev) |
124059fd AF |
1444 | { |
1445 | return 0; | |
1446 | } | |
124059fd | 1447 | |
f3a6bd39 SH |
1448 | static int __set_phy_supported(struct phy_device *phydev, u32 max_speed) |
1449 | { | |
1450 | /* The default values for phydev->supported are provided by the PHY | |
1451 | * driver "features" member, we want to reset to sane defaults first | |
1452 | * before supporting higher speeds. | |
1453 | */ | |
1454 | phydev->supported &= PHY_DEFAULT_FEATURES; | |
1455 | ||
1456 | switch (max_speed) { | |
1457 | default: | |
1458 | return -ENOTSUPP; | |
1459 | case SPEED_1000: | |
1460 | phydev->supported |= PHY_1000BT_FEATURES; | |
1461 | /* fall through */ | |
1462 | case SPEED_100: | |
1463 | phydev->supported |= PHY_100BT_FEATURES; | |
1464 | /* fall through */ | |
1465 | case SPEED_10: | |
1466 | phydev->supported |= PHY_10BT_FEATURES; | |
1467 | } | |
1468 | ||
1469 | return 0; | |
1470 | } | |
1471 | ||
1472 | int phy_set_max_speed(struct phy_device *phydev, u32 max_speed) | |
1473 | { | |
1474 | int err; | |
1475 | ||
1476 | err = __set_phy_supported(phydev, max_speed); | |
1477 | if (err) | |
1478 | return err; | |
1479 | ||
1480 | phydev->advertising = phydev->supported; | |
1481 | ||
1482 | return 0; | |
1483 | } | |
1484 | EXPORT_SYMBOL(phy_set_max_speed); | |
1485 | ||
de906af1 SH |
1486 | static void of_set_phy_supported(struct phy_device *phydev) |
1487 | { | |
e5a03bfd | 1488 | struct device_node *node = phydev->mdio.dev.of_node; |
de906af1 SH |
1489 | u32 max_speed; |
1490 | ||
1491 | if (!IS_ENABLED(CONFIG_OF_MDIO)) | |
1492 | return; | |
1493 | ||
1494 | if (!node) | |
1495 | return; | |
1496 | ||
f3a6bd39 SH |
1497 | if (!of_property_read_u32(node, "max-speed", &max_speed)) |
1498 | __set_phy_supported(phydev, max_speed); | |
de906af1 SH |
1499 | } |
1500 | ||
b3df0da8 RD |
1501 | /** |
1502 | * phy_probe - probe and init a PHY device | |
1503 | * @dev: device to probe and init | |
00db8189 | 1504 | * |
b3df0da8 | 1505 | * Description: Take care of setting up the phy_device structure, |
00db8189 AF |
1506 | * set the state to READY (the driver's init function should |
1507 | * set it to STARTING if needed). | |
1508 | */ | |
1509 | static int phy_probe(struct device *dev) | |
1510 | { | |
553fe92b | 1511 | struct phy_device *phydev = to_phy_device(dev); |
e5a03bfd | 1512 | struct device_driver *drv = phydev->mdio.dev.driver; |
553fe92b | 1513 | struct phy_driver *phydrv = to_phy_driver(drv); |
00db8189 AF |
1514 | int err = 0; |
1515 | ||
00db8189 AF |
1516 | phydev->drv = phydrv; |
1517 | ||
2c7b4921 FF |
1518 | /* Disable the interrupt if the PHY doesn't support it |
1519 | * but the interrupt is still a valid one | |
1520 | */ | |
1521 | if (!(phydrv->flags & PHY_HAS_INTERRUPT) && | |
2f53e904 | 1522 | phy_interrupt_is_valid(phydev)) |
00db8189 AF |
1523 | phydev->irq = PHY_POLL; |
1524 | ||
4284b6a5 FF |
1525 | if (phydrv->flags & PHY_IS_INTERNAL) |
1526 | phydev->is_internal = true; | |
1527 | ||
35b5f6b1 | 1528 | mutex_lock(&phydev->lock); |
00db8189 AF |
1529 | |
1530 | /* Start out supporting everything. Eventually, | |
1531 | * a controller will attach, and may modify one | |
2f53e904 SS |
1532 | * or both of these values |
1533 | */ | |
00db8189 | 1534 | phydev->supported = phydrv->features; |
de906af1 SH |
1535 | of_set_phy_supported(phydev); |
1536 | phydev->advertising = phydev->supported; | |
00db8189 AF |
1537 | |
1538 | /* Set the state to READY by default */ | |
1539 | phydev->state = PHY_READY; | |
1540 | ||
1541 | if (phydev->drv->probe) | |
1542 | err = phydev->drv->probe(phydev); | |
1543 | ||
35b5f6b1 | 1544 | mutex_unlock(&phydev->lock); |
00db8189 | 1545 | |
00db8189 AF |
1546 | return err; |
1547 | } | |
1548 | ||
1549 | static int phy_remove(struct device *dev) | |
1550 | { | |
553fe92b | 1551 | struct phy_device *phydev = to_phy_device(dev); |
00db8189 | 1552 | |
35b5f6b1 | 1553 | mutex_lock(&phydev->lock); |
00db8189 | 1554 | phydev->state = PHY_DOWN; |
35b5f6b1 | 1555 | mutex_unlock(&phydev->lock); |
00db8189 AF |
1556 | |
1557 | if (phydev->drv->remove) | |
1558 | phydev->drv->remove(phydev); | |
00db8189 AF |
1559 | phydev->drv = NULL; |
1560 | ||
1561 | return 0; | |
1562 | } | |
1563 | ||
b3df0da8 RD |
1564 | /** |
1565 | * phy_driver_register - register a phy_driver with the PHY layer | |
1566 | * @new_driver: new phy_driver to register | |
be01da72 | 1567 | * @owner: module owning this PHY |
b3df0da8 | 1568 | */ |
be01da72 | 1569 | int phy_driver_register(struct phy_driver *new_driver, struct module *owner) |
00db8189 AF |
1570 | { |
1571 | int retval; | |
1572 | ||
00db8189 AF |
1573 | new_driver->driver.name = new_driver->name; |
1574 | new_driver->driver.bus = &mdio_bus_type; | |
1575 | new_driver->driver.probe = phy_probe; | |
1576 | new_driver->driver.remove = phy_remove; | |
be01da72 | 1577 | new_driver->driver.owner = owner; |
00db8189 AF |
1578 | |
1579 | retval = driver_register(&new_driver->driver); | |
00db8189 | 1580 | if (retval) { |
8d242488 JP |
1581 | pr_err("%s: Error %d in registering driver\n", |
1582 | new_driver->name, retval); | |
00db8189 AF |
1583 | |
1584 | return retval; | |
1585 | } | |
1586 | ||
f2511f13 | 1587 | pr_debug("%s: Registered new driver\n", new_driver->name); |
00db8189 AF |
1588 | |
1589 | return 0; | |
1590 | } | |
1591 | EXPORT_SYMBOL(phy_driver_register); | |
1592 | ||
be01da72 AL |
1593 | int phy_drivers_register(struct phy_driver *new_driver, int n, |
1594 | struct module *owner) | |
d5bf9071 CH |
1595 | { |
1596 | int i, ret = 0; | |
1597 | ||
1598 | for (i = 0; i < n; i++) { | |
be01da72 | 1599 | ret = phy_driver_register(new_driver + i, owner); |
d5bf9071 CH |
1600 | if (ret) { |
1601 | while (i-- > 0) | |
1602 | phy_driver_unregister(new_driver + i); | |
1603 | break; | |
1604 | } | |
1605 | } | |
1606 | return ret; | |
1607 | } | |
1608 | EXPORT_SYMBOL(phy_drivers_register); | |
1609 | ||
00db8189 AF |
1610 | void phy_driver_unregister(struct phy_driver *drv) |
1611 | { | |
1612 | driver_unregister(&drv->driver); | |
1613 | } | |
1614 | EXPORT_SYMBOL(phy_driver_unregister); | |
1615 | ||
d5bf9071 CH |
1616 | void phy_drivers_unregister(struct phy_driver *drv, int n) |
1617 | { | |
1618 | int i; | |
2f53e904 SS |
1619 | |
1620 | for (i = 0; i < n; i++) | |
d5bf9071 | 1621 | phy_driver_unregister(drv + i); |
d5bf9071 CH |
1622 | } |
1623 | EXPORT_SYMBOL(phy_drivers_unregister); | |
1624 | ||
ab2145ed SX |
1625 | static struct phy_driver genphy_driver[] = { |
1626 | { | |
e1393456 AF |
1627 | .phy_id = 0xffffffff, |
1628 | .phy_id_mask = 0xffffffff, | |
1629 | .name = "Generic PHY", | |
9df81dd7 | 1630 | .soft_reset = genphy_soft_reset, |
e1393456 | 1631 | .config_init = genphy_config_init, |
c242a472 SH |
1632 | .features = PHY_GBIT_FEATURES | SUPPORTED_MII | |
1633 | SUPPORTED_AUI | SUPPORTED_FIBRE | | |
1634 | SUPPORTED_BNC, | |
e1393456 | 1635 | .config_aneg = genphy_config_aneg, |
76a423a3 | 1636 | .aneg_done = genphy_aneg_done, |
e1393456 | 1637 | .read_status = genphy_read_status, |
0f0ca340 GC |
1638 | .suspend = genphy_suspend, |
1639 | .resume = genphy_resume, | |
124059fd AF |
1640 | }, { |
1641 | .phy_id = 0xffffffff, | |
1642 | .phy_id_mask = 0xffffffff, | |
1643 | .name = "Generic 10G PHY", | |
9df81dd7 | 1644 | .soft_reset = gen10g_soft_reset, |
124059fd AF |
1645 | .config_init = gen10g_config_init, |
1646 | .features = 0, | |
1647 | .config_aneg = gen10g_config_aneg, | |
1648 | .read_status = gen10g_read_status, | |
1649 | .suspend = gen10g_suspend, | |
1650 | .resume = gen10g_resume, | |
ab2145ed | 1651 | } }; |
00db8189 | 1652 | |
67c4f3fa | 1653 | static int __init phy_init(void) |
00db8189 | 1654 | { |
67c4f3fa | 1655 | int rc; |
67c4f3fa JG |
1656 | |
1657 | rc = mdio_bus_init(); | |
1658 | if (rc) | |
e1393456 | 1659 | return rc; |
00db8189 | 1660 | |
ab2145ed | 1661 | rc = phy_drivers_register(genphy_driver, |
be01da72 | 1662 | ARRAY_SIZE(genphy_driver), THIS_MODULE); |
e1393456 AF |
1663 | if (rc) |
1664 | mdio_bus_exit(); | |
67c4f3fa | 1665 | |
67c4f3fa | 1666 | return rc; |
00db8189 AF |
1667 | } |
1668 | ||
67c4f3fa | 1669 | static void __exit phy_exit(void) |
00db8189 | 1670 | { |
ab2145ed SX |
1671 | phy_drivers_unregister(genphy_driver, |
1672 | ARRAY_SIZE(genphy_driver)); | |
e1393456 | 1673 | mdio_bus_exit(); |
00db8189 AF |
1674 | } |
1675 | ||
e1393456 | 1676 | subsys_initcall(phy_init); |
67c4f3fa | 1677 | module_exit(phy_exit); |