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