netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Richard Leitner <dev@g0hl1n.net>
To: f.fainelli@gmail.com, fugang.duan@nxp.com, andrew@lunn.ch
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	richard.leitner@skidata.com
Subject: [PATCH v2 1/3] net: ethernet: freescale: simplify fec_reset_phy
Date: Mon, 20 Nov 2017 09:34:15 +0100	[thread overview]
Message-ID: <20171120083417.32558-2-dev@g0hl1n.net> (raw)
In-Reply-To: <20171120083417.32558-1-dev@g0hl1n.net>

From: Richard Leitner <richard.leitner@skidata.com>

The fec_reset_phy function allowed only one execution during probeing.
To make it more usable move the dt parsing and gpio allocation to the
probe function. The parameters of the phy reset are added to the
fec_enet_private struct. As a result the fec_reset_phy function may be
called anytime after probe.

One checkpatch.pl warning (too long line) is ignored. This is due to the
fact a string (dt property name) otherwise needs to be split over
multiple lines, which is counterproductive for the readability.

Signed-off-by: Richard Leitner <richard.leitner@skidata.com>
---
 drivers/net/ethernet/freescale/fec.h      |  4 ++
 drivers/net/ethernet/freescale/fec_main.c | 88 ++++++++++++++++---------------
 2 files changed, 50 insertions(+), 42 deletions(-)

diff --git a/drivers/net/ethernet/freescale/fec.h b/drivers/net/ethernet/freescale/fec.h
index 5385074b3b7d..401c4eabf08a 100644
--- a/drivers/net/ethernet/freescale/fec.h
+++ b/drivers/net/ethernet/freescale/fec.h
@@ -539,6 +539,10 @@ struct fec_enet_private {
 	int	pause_flag;
 	int	wol_flag;
 	u32	quirks;
+	int	phy_reset;
+	int	phy_reset_duration;
+	int	phy_reset_post_delay;
+	bool	phy_reset_active_high;
 
 	struct	napi_struct napi;
 	int	csum_flags;
diff --git a/drivers/net/ethernet/freescale/fec_main.c b/drivers/net/ethernet/freescale/fec_main.c
index 610573855213..06a7caca0cee 100644
--- a/drivers/net/ethernet/freescale/fec_main.c
+++ b/drivers/net/ethernet/freescale/fec_main.c
@@ -3212,62 +3212,36 @@ static int fec_enet_init(struct net_device *ndev)
 }
 
 #ifdef CONFIG_OF
-static int fec_reset_phy(struct platform_device *pdev)
+static int fec_reset_phy(struct net_device *ndev)
 {
-	int err, phy_reset;
-	bool active_high = false;
-	int msec = 1, phy_post_delay = 0;
-	struct device_node *np = pdev->dev.of_node;
-
-	if (!np)
-		return 0;
-
-	err = of_property_read_u32(np, "phy-reset-duration", &msec);
-	/* A sane reset duration should not be longer than 1s */
-	if (!err && msec > 1000)
-		msec = 1;
+	struct fec_enet_private *fep = netdev_priv(ndev);
 
-	phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
-	if (phy_reset == -EPROBE_DEFER)
-		return phy_reset;
-	else if (!gpio_is_valid(phy_reset))
+	if (!fep->phy_reset)
 		return 0;
 
-	err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay);
-	/* valid reset duration should be less than 1s */
-	if (!err && phy_post_delay > 1000)
-		return -EINVAL;
-
-	active_high = of_property_read_bool(np, "phy-reset-active-high");
+	gpio_set_value_cansleep(fep->phy_reset, fep->phy_reset_active_high);
 
-	err = devm_gpio_request_one(&pdev->dev, phy_reset,
-			active_high ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
-			"phy-reset");
-	if (err) {
-		dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
-		return err;
-	}
-
-	if (msec > 20)
-		msleep(msec);
+	if (fep->phy_reset_duration > 20)
+		msleep(fep->phy_reset_duration);
 	else
-		usleep_range(msec * 1000, msec * 1000 + 1000);
+		usleep_range(fep->phy_reset_duration * 1000,
+			     fep->phy_reset_duration * 1000 + 1000);
 
-	gpio_set_value_cansleep(phy_reset, !active_high);
+	gpio_set_value_cansleep(fep->phy_reset, !fep->phy_reset_active_high);
 
-	if (!phy_post_delay)
+	if (!fep->phy_reset_post_delay)
 		return 0;
 
-	if (phy_post_delay > 20)
-		msleep(phy_post_delay);
+	if (fep->phy_reset_post_delay > 20)
+		msleep(fep->phy_reset_post_delay);
 	else
-		usleep_range(phy_post_delay * 1000,
-			     phy_post_delay * 1000 + 1000);
+		usleep_range(fep->phy_reset_post_delay * 1000,
+			     fep->phy_reset_post_delay * 1000 + 1000);
 
 	return 0;
 }
 #else /* CONFIG_OF */
-static int fec_reset_phy(struct platform_device *pdev)
+static int fec_reset_phy(struct net_device *ndev)
 {
 	/*
 	 * In case of platform probe, the reset has been done
@@ -3400,6 +3374,36 @@ fec_probe(struct platform_device *pdev)
 	}
 	fep->phy_node = phy_node;
 
+	fep->phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
+	if (gpio_is_valid(fep->phy_reset)) {
+		ret = of_property_read_u32(np, "phy-reset-duration",
+					   &fep->phy_reset_duration);
+		/* A sane reset duration should not be longer than 1s */
+		if (!ret && fep->phy_reset_post_delay > 1000)
+			fep->phy_reset_post_delay = 1;
+
+		ret = of_property_read_u32(np, "phy-reset-post-delay",
+					   &fep->phy_reset_post_delay);
+		/* valid post reset delay should be less than 1s */
+		if (!ret && fep->phy_reset_post_delay > 1000)
+			fep->phy_reset_post_delay = 1;
+
+		fep->phy_reset_active_high = of_property_read_bool(np, "phy-reset-active-high");
+
+		ret = devm_gpio_request_one(&pdev->dev, fep->phy_reset,
+					    fep->phy_reset_active_high ?
+					    GPIOF_OUT_INIT_HIGH :
+					    GPIOF_OUT_INIT_LOW,
+					    "phy-reset");
+		if (ret) {
+			dev_err(&pdev->dev, "failed to get reset-gpios: %d\n",
+				ret);
+			goto failed_phy;
+		}
+	} else {
+		fep->phy_reset = 0;
+	}
+
 	ret = of_get_phy_mode(pdev->dev.of_node);
 	if (ret < 0) {
 		pdata = dev_get_platdata(&pdev->dev);
@@ -3472,7 +3476,7 @@ fec_probe(struct platform_device *pdev)
 	pm_runtime_set_active(&pdev->dev);
 	pm_runtime_enable(&pdev->dev);
 
-	ret = fec_reset_phy(pdev);
+	ret = fec_reset_phy(ndev);
 	if (ret)
 		goto failed_reset;
 
-- 
2.11.0

  reply	other threads:[~2017-11-20  8:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-20  8:34 [PATCH v2 0/3] net: ethernet: fec: fix refclk enable for SMSC LAN8710/20 Richard Leitner
2017-11-20  8:34 ` Richard Leitner [this message]
2017-11-20  9:35   ` [PATCH v2 1/3] net: ethernet: freescale: simplify fec_reset_phy Andy Duan
2017-11-20  8:34 ` [PATCH v2 2/3] include: linux: phy: harmonize phy_id{,_mask} type Richard Leitner
2017-11-20  8:34 ` [PATCH v2 3/3] net: ethernet: fec: fix refclk enable for SMSC LAN8710/20 Richard Leitner
2017-11-20  9:47   ` Andy Duan
2017-11-20  9:57     ` Richard Leitner
2017-11-20 10:35       ` Andy Duan
2017-11-20 12:55         ` Richard Leitner
2017-11-20 13:13           ` Geert Uytterhoeven
2017-11-20 13:21             ` Richard Leitner
2017-11-20 13:40               ` Geert Uytterhoeven
2017-11-20 13:43           ` Andy Duan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171120083417.32558-2-dev@g0hl1n.net \
    --to=dev@g0hl1n.net \
    --cc=andrew@lunn.ch \
    --cc=f.fainelli@gmail.com \
    --cc=fugang.duan@nxp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=richard.leitner@skidata.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).