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