Skip to content

Commit 60fd112

Browse files
allemanmfabiobaltieri
authored andcommitted
drivers: ethernet: phy: phy_mii: add gpio reset
Add support for hardware reset to the phy_mii driver Signed-off-by: Matthias Alleman <[email protected]>
1 parent 6b2e014 commit 60fd112

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

drivers/ethernet/phy/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ config PHY_GENERIC_MII
3333
default y
3434
depends on DT_HAS_ETHERNET_PHY_ENABLED
3535
select MDIO
36+
select GPIO if $(dt_compat_any_has_prop,$(DT_COMPAT_ETHERNET_PHY),reset-gpios)
3637
help
3738
This is a generic MII PHY interface that communicates with the
3839
PHY using the MDIO bus.

drivers/ethernet/phy/phy_mii.c

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <zephyr/device.h>
1212
#include <zephyr/init.h>
1313
#include <zephyr/kernel.h>
14+
#include <zephyr/drivers/gpio.h>
1415
#include <zephyr/drivers/mdio.h>
1516
#include <zephyr/net/phy.h>
1617
#include <zephyr/net/mii.h>
@@ -22,6 +23,7 @@ LOG_MODULE_REGISTER(phy_mii, CONFIG_PHY_LOG_LEVEL);
2223

2324
#define ANY_DYNAMIC_LINK UTIL_NOT(DT_ALL_INST_HAS_PROP_STATUS_OKAY(fixed_link))
2425
#define ANY_FIXED_LINK DT_ANY_INST_HAS_PROP_STATUS_OKAY(fixed_link)
26+
#define ANY_RESET_GPIO DT_ANY_INST_HAS_PROP_STATUS_OKAY(reset_gpios)
2527

2628
struct phy_mii_dev_config {
2729
uint8_t phy_addr;
@@ -30,6 +32,11 @@ struct phy_mii_dev_config {
3032
int fixed_speed;
3133
enum phy_link_speed default_speeds;
3234
const struct device * const mdio;
35+
#if ANY_RESET_GPIO
36+
const struct gpio_dt_spec reset_gpio;
37+
uint32_t reset_assert_duration_us;
38+
uint32_t reset_deassertion_timeout_ms;
39+
#endif /* ANY_RESET_GPIO */
3340
};
3441

3542
struct phy_mii_dev_data {
@@ -120,6 +127,33 @@ static int reset(const struct device *dev)
120127
uint32_t timeout = 12U;
121128
uint16_t value;
122129

130+
#if ANY_RESET_GPIO
131+
const struct phy_mii_dev_config *const cfg = dev->config;
132+
int ret;
133+
134+
if (gpio_is_ready_dt(&cfg->reset_gpio)) {
135+
/* Issue a hard reset */
136+
ret = gpio_pin_configure_dt(&cfg->reset_gpio, GPIO_OUTPUT_ACTIVE);
137+
if (ret < 0) {
138+
LOG_ERR("Failed to configure RST pin (%d)", ret);
139+
return ret;
140+
}
141+
142+
/* assertion time */
143+
k_busy_wait(cfg->reset_assert_duration_us);
144+
145+
ret = gpio_pin_set_dt(&cfg->reset_gpio, 0);
146+
if (ret < 0) {
147+
LOG_ERR("Failed to de-assert RST pin (%d)", ret);
148+
return ret;
149+
}
150+
151+
k_msleep(cfg->reset_deassertion_timeout_ms);
152+
153+
return 0;
154+
}
155+
#endif /* ANY_RESET_GPIO */
156+
123157
/* Issue a soft reset */
124158
if (phy_mii_reg_write(dev, MII_BMCR, MII_BMCR_RESET) < 0) {
125159
return -EIO;
@@ -547,6 +581,18 @@ static DEVICE_API(ethphy, phy_mii_driver_api) = {
547581
#endif
548582
};
549583

584+
#if ANY_RESET_GPIO
585+
#define RESET_GPIO(n) \
586+
.reset_gpio = GPIO_DT_SPEC_INST_GET_OR(n, \
587+
reset_gpios, {0}), \
588+
.reset_assert_duration_us = DT_INST_PROP_OR(n, \
589+
reset_assert_duration_us, 0), \
590+
.reset_deassertion_timeout_ms = DT_INST_PROP_OR(n, \
591+
reset_deassertion_timeout_ms, 0),
592+
#else
593+
#define RESET_GPIO(n)
594+
#endif /* ANY_RESET_GPIO */
595+
550596
#define PHY_MII_CONFIG(n) \
551597
BUILD_ASSERT(PHY_INST_GENERATE_DEFAULT_SPEEDS(n) != 0, \
552598
"At least one valid speed must be configured for this driver"); \
@@ -558,7 +604,8 @@ static const struct phy_mii_dev_config phy_mii_dev_config_##n = { \
558604
.fixed_speed = DT_INST_ENUM_IDX_OR(n, fixed_link, 0), \
559605
.default_speeds = PHY_INST_GENERATE_DEFAULT_SPEEDS(n), \
560606
.mdio = UTIL_AND(UTIL_NOT(IS_FIXED_LINK(n)), \
561-
DEVICE_DT_GET(DT_INST_BUS(n))) \
607+
DEVICE_DT_GET(DT_INST_BUS(n))), \
608+
RESET_GPIO(n) \
562609
};
563610

564611
#define PHY_MII_DATA(n) \

dts/bindings/ethernet/phy/ethernet-phy.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,15 @@ properties:
2626
default-speeds:
2727
default: ["10BASE Half-Duplex", "10BASE Full-Duplex", "100BASE Half-Duplex",
2828
"100BASE Full-Duplex", "1000BASE Half-Duplex", "1000BASE Full-Duplex"]
29+
reset-gpios:
30+
type: phandle-array
31+
description: Reset pin.
32+
Optional GPIO used to reset the PHY
33+
reset-assert-duration-us:
34+
type: int
35+
description:
36+
Minimum duration in microseconds to assert the reset-gpios pin.
37+
reset-deassertion-timeout-ms:
38+
type: int
39+
description:
40+
Timeout in milliseconds after the reset-gpios pin is deasserted.

0 commit comments

Comments
 (0)