From: Matthias Klein <matthias.klein@optimeas.de>
To: wg@grandegger.com, mkl@pengutronix.de, linux-can@vger.kernel.org,
support@karo-electronics.de
Cc: bigeasy@linutronix.de, linux-stable <stable@vger.kernel.org>
Subject: [PATCH V2 4/5] can: flexcan: fix transition from and to freeze mode in chip_{,un}freeze
Date: Mon, 28 Jul 2014 08:34:27 +0200 [thread overview]
Message-ID: <1406529268-20126-4-git-send-email-matthias.klein@optimeas.de> (raw)
In-Reply-To: <1406529268-20126-1-git-send-email-matthias.klein@optimeas.de>
From: Marc Kleine-Budde <mkl@pengutronix.de>
This patch factors out freeze and unfreeze of the CAN core into seperate
functions. Experiments have shown that the transition from and to freeze mode
may take several microseconds, especially the time entering the freeze mode
depends on the current bitrate.
This patch adds a while loop which polls the Freeze Mode ACK bit (FRZ_ACK) that
indicates a successfull mode change. If the function runs into a timeout a
error value is returned.
Cc: linux-stable <stable@vger.kernel.org>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: Matthias Klein <matthias.klein@optimeas.de>
---
drivers/net/can/flexcan.c | 60 ++++++++++++++++++++++++++++++++++++++---------
1 file changed, 49 insertions(+), 11 deletions(-)
diff --git a/drivers/net/can/flexcan.c b/drivers/net/can/flexcan.c
index f6f95ae..b77f1da 100644
--- a/drivers/net/can/flexcan.c
+++ b/drivers/net/can/flexcan.c
@@ -322,6 +322,44 @@ static int flexcan_chip_disable(struct flexcan_priv *priv)
return 0;
}
+static int flexcan_chip_freeze(struct flexcan_priv *priv)
+{
+ struct flexcan_regs __iomem *regs = priv->base;
+ unsigned int timeout = 1000 * 1000 * 10 / priv->can.bittiming.bitrate;
+ u32 reg;
+
+ reg = flexcan_read(®s->mcr);
+ reg |= FLEXCAN_MCR_HALT;
+ flexcan_write(reg, ®s->mcr);
+
+ while (timeout-- && !(flexcan_read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK))
+ usleep_range(100, 200);
+
+ if (!(flexcan_read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK))
+ return -ETIMEDOUT;
+
+ return 0;
+}
+
+static int flexcan_chip_unfreeze(struct flexcan_priv *priv)
+{
+ struct flexcan_regs __iomem *regs = priv->base;
+ unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
+ u32 reg;
+
+ reg = flexcan_read(®s->mcr);
+ reg &= ~FLEXCAN_MCR_HALT;
+ flexcan_write(reg, ®s->mcr);
+
+ while (timeout-- && (flexcan_read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK))
+ usleep_range(10, 20);
+
+ if (flexcan_read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK)
+ return -ETIMEDOUT;
+
+ return 0;
+}
+
static int flexcan_get_berr_counter(const struct net_device *dev,
struct can_berr_counter *bec)
{
@@ -776,7 +814,7 @@ static int flexcan_chip_start(struct net_device *dev)
netdev_err(dev, "Failed to softreset can module (mcr=0x%08x)\n",
reg_mcr);
err = -ENODEV;
- goto out;
+ goto out_chip_disable;
}
flexcan_set_bittiming(dev);
@@ -846,12 +884,12 @@ static int flexcan_chip_start(struct net_device *dev)
err = flexcan_transceiver_enable(priv);
if (err)
- goto out;
+ goto out_chip_disable;
/* synchronize with the can bus */
- reg_mcr = flexcan_read(®s->mcr);
- reg_mcr &= ~FLEXCAN_MCR_HALT;
- flexcan_write(reg_mcr, ®s->mcr);
+ err = flexcan_chip_unfreeze(priv);
+ if (err)
+ goto out_transceiver_disable;
priv->can.state = CAN_STATE_ERROR_ACTIVE;
@@ -864,7 +902,9 @@ static int flexcan_chip_start(struct net_device *dev)
return 0;
- out:
+ out_transceiver_disable:
+ flexcan_transceiver_disable(priv);
+ out_chip_disable:
flexcan_chip_disable(priv);
return err;
}
@@ -879,12 +919,10 @@ static void flexcan_chip_stop(struct net_device *dev)
{
struct flexcan_priv *priv = netdev_priv(dev);
struct flexcan_regs __iomem *regs = priv->base;
- u32 reg;
- /* Disable + halt module */
- reg = flexcan_read(®s->mcr);
- reg |= FLEXCAN_MCR_MDIS | FLEXCAN_MCR_HALT;
- flexcan_write(reg, ®s->mcr);
+ /* freeze + disable module */
+ flexcan_chip_freeze(priv);
+ flexcan_chip_disable(priv);
/* Disable all interrupts */
flexcan_write(0, ®s->imask1);
--
2.0.1
next prev parent reply other threads:[~2014-07-28 6:34 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-28 6:34 [PATCH V2 1/5] net: can: flexcan: provide propper return code in ISR Matthias Klein
2014-07-28 6:34 ` [PATCH V2 2/5] net: can: flexcan: disable error interrupts in non ERR-Active state Matthias Klein
2014-07-28 6:40 ` Varka Bhadram
2014-07-28 6:34 ` [PATCH V2 3/5] net: can: flexcan: handle state passive -> warning transition Matthias Klein
2014-07-28 6:34 ` Matthias Klein [this message]
2014-07-28 6:38 ` [PATCH V2 4/5] can: flexcan: fix transition from and to freeze mode in chip_{,un}freeze Marc Kleine-Budde
2014-07-28 6:34 ` [PATCH V2 5/5] net: can: flexcan: fix for wrong TX error count behaviour on i.MX53 Matthias Klein
2014-07-28 6:39 ` Varka Bhadram
2014-07-28 7:05 ` Marc Kleine-Budde
2014-07-28 7:08 ` Sebastian Andrzej Siewior
2014-07-28 6:36 ` [PATCH V2 1/5] net: can: flexcan: provide propper return code in ISR Varka Bhadram
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=1406529268-20126-4-git-send-email-matthias.klein@optimeas.de \
--to=matthias.klein@optimeas.de \
--cc=bigeasy@linutronix.de \
--cc=linux-can@vger.kernel.org \
--cc=mkl@pengutronix.de \
--cc=stable@vger.kernel.org \
--cc=support@karo-electronics.de \
--cc=wg@grandegger.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).