linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
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
Subject: [PATCH 4/5] net: can: flexcan: wait for completion when entering freeze mode
Date: Fri, 25 Jul 2014 20:16:41 +0200	[thread overview]
Message-ID: <1406312202-2542-4-git-send-email-matthias.klein@optimeas.de> (raw)
In-Reply-To: <1406312202-2542-1-git-send-email-matthias.klein@optimeas.de>

After requesting Freeze Mode, the user must wait for the FRZ_ACK
bit to be asserted in MCR before executing any other action,
otherwise FLEXCAN may operate in an unpredictable way.

Signed-off-by: Matthias Klein <matthias.klein@optimeas.de>
---
 drivers/net/can/flexcan.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/drivers/net/can/flexcan.c b/drivers/net/can/flexcan.c
index f6f95ae..0fbc571 100644
--- a/drivers/net/can/flexcan.c
+++ b/drivers/net/can/flexcan.c
@@ -441,6 +441,24 @@ static int flexcan_poll_bus_err(struct net_device *dev, u32 reg_esr)
 	return 1;
 }
 
+static int flexcan_wait_for_frz(struct net_device *dev,
+				struct flexcan_regs __iomem *regs, int en)
+{
+	u32 reg_mcr;
+	u32 loops = 100;
+
+	do {
+		reg_mcr = flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK;
+		if ((en && reg_mcr) || (!en && !reg_mcr))
+			return 0;
+		loops--;
+		udelay(1);
+	} while (loops);
+
+	netdev_err(dev, "Freeze Timeout\n");
+	return -ETIMEDOUT;
+}
+
 static void do_state(struct net_device *dev,
 		     struct can_frame *cf, enum can_state new_state)
 {
@@ -801,6 +819,7 @@ static int flexcan_chip_start(struct net_device *dev)
 		FLEXCAN_MCR_MAXMB(FLEXCAN_TX_BUF_ID);
 	netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr);
 	flexcan_write(reg_mcr, &regs->mcr);
+	flexcan_wait_for_frz(dev, regs, 1);
 
 	/*
 	 * CTRL
@@ -852,6 +871,7 @@ static int flexcan_chip_start(struct net_device *dev)
 	reg_mcr = flexcan_read(&regs->mcr);
 	reg_mcr &= ~FLEXCAN_MCR_HALT;
 	flexcan_write(reg_mcr, &regs->mcr);
+	flexcan_wait_for_frz(dev, regs, 0);
 
 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
 
@@ -885,6 +905,7 @@ static void flexcan_chip_stop(struct net_device *dev)
 	reg = flexcan_read(&regs->mcr);
 	reg |= FLEXCAN_MCR_MDIS | FLEXCAN_MCR_HALT;
 	flexcan_write(reg, &regs->mcr);
+	flexcan_wait_for_frz(dev, regs, 1);
 
 	/* Disable all interrupts */
 	flexcan_write(0, &regs->imask1);
@@ -1018,6 +1039,7 @@ static int register_flexcandev(struct net_device *dev)
 	reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT |
 		FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
 	flexcan_write(reg, &regs->mcr);
+	flexcan_wait_for_frz(dev, regs, 1);
 
 	/*
 	 * Currently we only support newer versions of this core
-- 
2.0.1


  parent reply	other threads:[~2014-07-25 18:24 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-25 18:16 [PATCH 1/5] net: can: flexcan: provide propper return code in ISR Matthias Klein
2014-07-25 18:16 ` [PATCH 2/5] net: can: flexcan: disable error interrupts in non ERR-Active state Matthias Klein
2014-07-25 22:17   ` Marc Kleine-Budde
2014-07-25 18:16 ` [PATCH 3/5] net: can: flexcan: handle state passive -> warning transition Matthias Klein
2014-07-25 22:21   ` Marc Kleine-Budde
2014-07-25 18:16 ` Matthias Klein [this message]
2014-07-25 18:50   ` [PATCH 4/5] net: can: flexcan: wait for completion when entering freeze mode Sebastian Andrzej Siewior
2014-07-25 18:51   ` [PATCH] net: can: flexcan: reset the error counter after leaving passive state Sebastian Andrzej Siewior
2014-07-25 20:46     ` Marc Kleine-Budde
2014-07-25 18:16 ` [PATCH 5/5] net: can: flexcan: fix for wrong TX error count behaviour on i.MX53 Matthias Klein
2014-07-25 22:31 ` [PATCH 1/5] net: can: flexcan: provide propper return code in ISR Marc Kleine-Budde

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=1406312202-2542-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=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).