diff mbox series

net: phy: mdio: add clock support for PHYs

Message ID 20220704004533.17762-1-andre.przywara@arm.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series net: phy: mdio: add clock support for PHYs | expand

Checks

Context Check Description
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit fail Errors and warnings before: 581 this patch: 581
netdev/cc_maintainers success CCed 8 of 8 maintainers
netdev/build_clang success Errors and warnings before: 312 this patch: 312
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 545 this patch: 545
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 84 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Andre Przywara July 4, 2022, 12:45 a.m. UTC
So far the generic Ethernet PHY subsystem supports PHYs having a reset
line, which needs to be de-asserted before the PHY can be used. This
corresponds to an "RST" pin on most external PHY chips.
But most PHY chips also need an external clock signal, which may feed
some internal PLL, and/or is used to drive the internal logic. In many
systems this clock signal is provided by a fixed crystal oscillator, so
is of no particular interest to software.

However some systems use a more complex clock source, or try to save a
few pennies by avoiding the crystal. The X-Powers AC200 mixed signal PHY
chip, for instance, uses a software-controlled clock gate, and the
Lindenis V5 development board drives its RTL8211 PHY via a clock pin
on the SoC.

On those systems the clock source needs to be actively enabled by
software, before the PHY can be used. To support those machines, add a
struct clk, populate it from firmware tables, and enable or disable it
when needed, similar to toggling the reset line.

In contrast to exclusive reset lines, calls to clock_disable() need to
be balanced with calls to clock_enable() before, also the gate is
supposed to be initially disabled. This means we cannot treat it exactly
the same as the reset line, but have to skip the initial handling, and
just enable or disable the gate in the probe and remove handlers.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
---
 drivers/net/phy/mdio_bus.c    | 18 ++++++++++++++++++
 drivers/net/phy/mdio_device.c | 12 ++++++++++++
 include/linux/mdio.h          |  1 +
 3 files changed, 31 insertions(+)

Comments

Andrew Lunn July 4, 2022, 8:05 a.m. UTC | #1
> +static int mdiobus_register_clock(struct mdio_device *mdiodev)
> +{
> +	struct clk *clk;
> +
> +	clk = devm_clk_get_optional(&mdiodev->dev, NULL);
> +	if (IS_ERR(clk))
> +		return PTR_ERR(clk);

How does this interact with the clock code of the micrel and smsc
drivers?

Please document this clock in the binding, ethernet-phy.yaml.

> diff --git a/drivers/net/phy/mdio_device.c b/drivers/net/phy/mdio_device.c
> index 250742ffdfd9..e8424a46a81e 100644
> --- a/drivers/net/phy/mdio_device.c
> +++ b/drivers/net/phy/mdio_device.c
> @@ -6,6 +6,7 @@
>  
>  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
>  
> +#include <linux/clk.h>
>  #include <linux/delay.h>
>  #include <linux/errno.h>
>  #include <linux/gpio.h>
> @@ -136,6 +137,14 @@ void mdio_device_reset(struct mdio_device *mdiodev, int value)
>  }
>  EXPORT_SYMBOL(mdio_device_reset);
>  
> +static void mdio_device_toggle_clock(struct mdio_device *mdiodev, int value)

Not sure this is the best name, you are not toggling the clock, you
are toggling the clock gate. And you are not really toggling it, since
you pass value.

    Andrew
diff mbox series

Patch

diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c
index 8a2dbe849866..5cf84f92dab4 100644
--- a/drivers/net/phy/mdio_bus.c
+++ b/drivers/net/phy/mdio_bus.c
@@ -8,6 +8,7 @@ 
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
+#include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/errno.h>
@@ -67,6 +68,19 @@  static int mdiobus_register_reset(struct mdio_device *mdiodev)
 	return 0;
 }
 
+static int mdiobus_register_clock(struct mdio_device *mdiodev)
+{
+	struct clk *clk;
+
+	clk = devm_clk_get_optional(&mdiodev->dev, NULL);
+	if (IS_ERR(clk))
+		return PTR_ERR(clk);
+
+	mdiodev->clk = clk;
+
+	return 0;
+}
+
 int mdiobus_register_device(struct mdio_device *mdiodev)
 {
 	int err;
@@ -83,6 +97,10 @@  int mdiobus_register_device(struct mdio_device *mdiodev)
 		if (err)
 			return err;
 
+		err = mdiobus_register_clock(mdiodev);
+		if (err)
+			return err;
+
 		/* Assert the reset signal */
 		mdio_device_reset(mdiodev, 1);
 	}
diff --git a/drivers/net/phy/mdio_device.c b/drivers/net/phy/mdio_device.c
index 250742ffdfd9..e8424a46a81e 100644
--- a/drivers/net/phy/mdio_device.c
+++ b/drivers/net/phy/mdio_device.c
@@ -6,6 +6,7 @@ 
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
+#include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/errno.h>
 #include <linux/gpio.h>
@@ -136,6 +137,14 @@  void mdio_device_reset(struct mdio_device *mdiodev, int value)
 }
 EXPORT_SYMBOL(mdio_device_reset);
 
+static void mdio_device_toggle_clock(struct mdio_device *mdiodev, int value)
+{
+	if (value)
+		clk_prepare_enable(mdiodev->clk);
+	else
+		clk_disable_unprepare(mdiodev->clk);
+}
+
 /**
  * mdio_probe - probe an MDIO device
  * @dev: device to probe
@@ -152,11 +161,13 @@  static int mdio_probe(struct device *dev)
 
 	/* Deassert the reset signal */
 	mdio_device_reset(mdiodev, 0);
+	mdio_device_toggle_clock(mdiodev, 1);
 
 	if (mdiodrv->probe) {
 		err = mdiodrv->probe(mdiodev);
 		if (err) {
 			/* Assert the reset signal */
+			mdio_device_toggle_clock(mdiodev, 0);
 			mdio_device_reset(mdiodev, 1);
 		}
 	}
@@ -174,6 +185,7 @@  static int mdio_remove(struct device *dev)
 		mdiodrv->remove(mdiodev);
 
 	/* Assert the reset signal */
+	mdio_device_toggle_clock(mdiodev, 0);
 	mdio_device_reset(mdiodev, 1);
 
 	return 0;
diff --git a/include/linux/mdio.h b/include/linux/mdio.h
index 00177567cfef..95c13bdb312b 100644
--- a/include/linux/mdio.h
+++ b/include/linux/mdio.h
@@ -50,6 +50,7 @@  struct mdio_device {
 	struct reset_control *reset_ctrl;
 	unsigned int reset_assert_delay;
 	unsigned int reset_deassert_delay;
+	struct clk *clk;
 };
 
 static inline struct mdio_device *to_mdio_device(const struct device *dev)