Skip to content

Commit 6922689

Browse files
Roger Quadrosdavem330
authored andcommitted
mdio_bus: Issue GPIO RESET to PHYs.
Some boards [1] leave the PHYs at an invalid state during system power-up or reset thus causing unreliability issues with the PHY which manifests as PHY not being detected or link not functional. To fix this, these PHYs need to be RESET via a GPIO connected to the PHY's RESET pin. Some boards have a single GPIO controlling the PHY RESET pin of all PHYs on the bus whereas some others have separate GPIOs controlling individual PHY RESETs. In both cases, the RESET de-assertion cannot be done in the PHY driver as the PHY will not probe till its reset is de-asserted. So do the RESET de-assertion in the MDIO bus driver. [1] - am572x-idk, am571x-idk, a437x-idk Signed-off-by: Roger Quadros <rogerq@ti.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 15769ff commit 6922689

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Common MDIO bus properties.
2+
3+
These are generic properties that can apply to any MDIO bus.
4+
5+
Optional properties:
6+
- reset-gpios: List of one or more GPIOs that control the RESET lines
7+
of the PHYs on that MDIO bus.
8+
- reset-delay-us: RESET pulse width in microseconds as per PHY datasheet.
9+
10+
A list of child nodes, one per device on the bus is expected. These
11+
should follow the generic phy.txt, or a device specific binding document.
12+
13+
Example :
14+
This example shows these optional properties, plus other properties
15+
required for the TI Davinci MDIO driver.
16+
17+
davinci_mdio: ethernet@0x5c030000 {
18+
compatible = "ti,davinci_mdio";
19+
reg = <0x5c030000 0x1000>;
20+
#address-cells = <1>;
21+
#size-cells = <0>;
22+
23+
reset-gpios = <&gpio2 5 GPIO_ACTIVE_LOW>;
24+
reset-delay-us = <2>; /* PHY datasheet states 1us min */
25+
26+
ethphy0: ethernet-phy@1 {
27+
reg = <1>;
28+
};
29+
30+
ethphy1: ethernet-phy@3 {
31+
reg = <3>;
32+
};
33+
};

drivers/net/phy/mdio_bus.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
#include <linux/init.h>
2323
#include <linux/delay.h>
2424
#include <linux/device.h>
25+
#include <linux/gpio.h>
26+
#include <linux/gpio/consumer.h>
2527
#include <linux/of_device.h>
2628
#include <linux/of_mdio.h>
29+
#include <linux/of_gpio.h>
2730
#include <linux/netdevice.h>
2831
#include <linux/etherdevice.h>
2932
#include <linux/skbuff.h>
@@ -337,6 +340,7 @@ int __mdiobus_register(struct mii_bus *bus, struct module *owner)
337340
{
338341
struct mdio_device *mdiodev;
339342
int i, err;
343+
struct gpio_desc *gpiod;
340344

341345
if (NULL == bus || NULL == bus->name ||
342346
NULL == bus->read || NULL == bus->write)
@@ -363,6 +367,35 @@ int __mdiobus_register(struct mii_bus *bus, struct module *owner)
363367
if (bus->reset)
364368
bus->reset(bus);
365369

370+
/* de-assert bus level PHY GPIO resets */
371+
if (bus->num_reset_gpios > 0) {
372+
bus->reset_gpiod = devm_kcalloc(&bus->dev,
373+
bus->num_reset_gpios,
374+
sizeof(struct gpio_desc *),
375+
GFP_KERNEL);
376+
if (!bus->reset_gpiod)
377+
return -ENOMEM;
378+
}
379+
380+
for (i = 0; i < bus->num_reset_gpios; i++) {
381+
gpiod = devm_gpiod_get_index(&bus->dev, "reset", i,
382+
GPIOD_OUT_LOW);
383+
if (IS_ERR(gpiod)) {
384+
err = PTR_ERR(gpiod);
385+
if (err != -ENOENT) {
386+
dev_err(&bus->dev,
387+
"mii_bus %s couldn't get reset GPIO\n",
388+
bus->id);
389+
return err;
390+
}
391+
} else {
392+
bus->reset_gpiod[i] = gpiod;
393+
gpiod_set_value_cansleep(gpiod, 1);
394+
udelay(bus->reset_delay_us);
395+
gpiod_set_value_cansleep(gpiod, 0);
396+
}
397+
}
398+
366399
for (i = 0; i < PHY_MAX_ADDR; i++) {
367400
if ((bus->phy_mask & (1 << i)) == 0) {
368401
struct phy_device *phydev;
@@ -390,6 +423,13 @@ int __mdiobus_register(struct mii_bus *bus, struct module *owner)
390423
mdiodev->device_remove(mdiodev);
391424
mdiodev->device_free(mdiodev);
392425
}
426+
427+
/* Put PHYs in RESET to save power */
428+
for (i = 0; i < bus->num_reset_gpios; i++) {
429+
if (bus->reset_gpiod[i])
430+
gpiod_set_value_cansleep(bus->reset_gpiod[i], 1);
431+
}
432+
393433
device_del(&bus->dev);
394434
return err;
395435
}
@@ -411,6 +451,13 @@ void mdiobus_unregister(struct mii_bus *bus)
411451
mdiodev->device_remove(mdiodev);
412452
mdiodev->device_free(mdiodev);
413453
}
454+
455+
/* Put PHYs in RESET to save power */
456+
for (i = 0; i < bus->num_reset_gpios; i++) {
457+
if (bus->reset_gpiod[i])
458+
gpiod_set_value_cansleep(bus->reset_gpiod[i], 1);
459+
}
460+
414461
device_del(&bus->dev);
415462
}
416463
EXPORT_SYMBOL(mdiobus_unregister);

drivers/of/of_mdio.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <linux/of_net.h>
2323
#include <linux/module.h>
2424

25+
#define DEFAULT_GPIO_RESET_DELAY 10 /* in microseconds */
26+
2527
MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
2628
MODULE_LICENSE("GPL");
2729

@@ -221,6 +223,11 @@ int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
221223

222224
mdio->dev.of_node = np;
223225

226+
/* Get bus level PHY reset GPIO details */
227+
mdio->reset_delay_us = DEFAULT_GPIO_RESET_DELAY;
228+
of_property_read_u32(np, "reset-delay-us", &mdio->reset_delay_us);
229+
mdio->num_reset_gpios = of_gpio_named_count(np, "reset-gpios");
230+
224231
/* Register the MDIO bus */
225232
rc = mdiobus_register(mdio);
226233
if (rc)

include/linux/phy.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ struct mii_bus {
217217
* matching its address
218218
*/
219219
int irq[PHY_MAX_ADDR];
220+
221+
/* GPIO reset pulse width in microseconds */
222+
int reset_delay_us;
223+
/* Number of reset GPIOs */
224+
int num_reset_gpios;
225+
/* Array of RESET GPIO descriptors */
226+
struct gpio_desc **reset_gpiod;
220227
};
221228
#define to_mii_bus(d) container_of(d, struct mii_bus, dev)
222229

0 commit comments

Comments
 (0)