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