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