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