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