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