From: Marc Kleine-Budde <mkl@pengutronix.de>
To: Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Matthias Klein <matthias.klein@optimeas.de>
Cc: wg@grandegger.com, linux-can@vger.kernel.org,
support@karo-electronics.de
Subject: Re: [PATCH] net: can: flexcan: reset the error counter after leaving passive state
Date: Fri, 25 Jul 2014 22:46:34 +0200 [thread overview]
Message-ID: <53D2C22A.80406@pengutronix.de> (raw)
In-Reply-To: <20140725185117.GB24839@linutronix.de>
[-- Attachment #1: Type: text/plain, Size: 5257 bytes --]
On 07/25/2014 08:51 PM, Sebastian Andrzej Siewior wrote:
> The reasoning of why is done what is done is described in the longer
> comment which I do not repeat here.
> While pleaying with the HALT/Freeze mode/bit I noticed that the manual
> says that one has to wait until the core completed the request or
> "otherwise FLEXCAN may operate in an unpredictable way"
> therefore flexcan_wait_for_frz() is added to places where the HALT bit
> is set / cleared.
Please have a look at a recent kernel, especially patch
b1aa1c7 can: flexcan: fix transition from and to freeze mode
in chip_{,un}freeze
>
> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> ---
> drivers/net/can/flexcan.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 61 insertions(+)
>
> diff --git a/drivers/net/can/flexcan.c b/drivers/net/can/flexcan.c
> index f6f95ae..0ac99c5 100644
> --- a/drivers/net/can/flexcan.c
> +++ b/drivers/net/can/flexcan.c
> @@ -441,6 +441,61 @@ static int flexcan_poll_bus_err(struct net_device *dev, u32 reg_esr)
> return 1;
> }
>
> +static void flexcan_wait_for_frz(struct net_device *dev, int en)
> +{
> + struct flexcan_priv *priv = netdev_priv(dev);
> + struct flexcan_regs __iomem *regs = priv->base;
> + u32 reg_mcr;
> + u32 loops = 25;
> +
> + do {
> + reg_mcr = flexcan_read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK;
> + if ((en && reg_mcr) || (!en && !reg_mcr))
> + return;
> +
> + loops--;
> + udelay(1);
> + } while (loops);
> +
> + netdev_err(dev, "Timeout during for the FRZ bit\n");
> +}
> +
> +static void flexcan_reset_err_reg(struct net_device *dev)
> +{
> + struct flexcan_priv *priv = netdev_priv(dev);
> + struct flexcan_regs __iomem *regs = priv->base;
> + u32 reg_mcr;
> +
> + /*
> + * The reset of the error counter is ugly but I don't see any other way.
> + * Open the CAN bus, send packet => HW goes to ERR-pasive, TX-err
> + * counter is around 128 (maybe 129). Close the CAN-Bus, the TX-err
> + * counter drops down to somewhere between 126 … 127, HW goes to
> + * ERR-Warning, everything is fine.
> + *
> + * Now: Repeat the above procedure. What happens is that on send the
> + * TX-err increases to around 134…136 and on connect it drops to
> + * around 130. Occording to ESR the HW is still in passive mode _but_ it
> + * is possible send packets - that means can send packets but has no
> + * clue about it.
As far as I know, it's perfectly legal to send CAN frame while in "error
passive" mode. While in error passive mode you cannot send an active
error flag, just a passive one. I don't have a link to the standard at
hand, but have a look at the CAN wiki:
http://www.can-wiki.info/doku.php?id=can_faq_erors
> + * To get a consistent behavior here, the error counter are reset so we
> + * fall back to Err-Active mode and the second "can send on open bus"
> + * behaves just like the first one.
> + */
> +
> + reg_mcr = flexcan_read(®s->mcr);
> + reg_mcr |= FLEXCAN_MCR_HALT;
> + flexcan_write(reg_mcr, ®s->mcr);
> +
> + flexcan_wait_for_frz(dev, 1);
> +
> + flexcan_write(0, ®s->ecr);
> +
> + reg_mcr &= ~FLEXCAN_MCR_HALT;
> + flexcan_write(reg_mcr, ®s->mcr);
> + flexcan_wait_for_frz(dev, 0);
> +}
> +
> static void do_state(struct net_device *dev,
> struct can_frame *cf, enum can_state new_state)
> {
> @@ -499,6 +554,8 @@ static void do_state(struct net_device *dev,
> cf->data[1] = (bec.txerr > bec.rxerr) ?
> CAN_ERR_CRTL_TX_WARNING :
> CAN_ERR_CRTL_RX_WARNING;
> +
> + flexcan_reset_err_reg(dev);
> break;
> case CAN_STATE_ERROR_ACTIVE:
> netdev_dbg(dev, "Error Active\n");
> @@ -801,6 +858,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, ®s->mcr);
> + flexcan_wait_for_frz(dev, 1);
>
> /*
> * CTRL
> @@ -852,6 +910,7 @@ static int flexcan_chip_start(struct net_device *dev)
> reg_mcr = flexcan_read(®s->mcr);
> reg_mcr &= ~FLEXCAN_MCR_HALT;
> flexcan_write(reg_mcr, ®s->mcr);
> + flexcan_wait_for_frz(dev, 0);
>
> priv->can.state = CAN_STATE_ERROR_ACTIVE;
>
> @@ -885,6 +944,7 @@ static void flexcan_chip_stop(struct net_device *dev)
> reg = flexcan_read(®s->mcr);
> reg |= FLEXCAN_MCR_MDIS | FLEXCAN_MCR_HALT;
> flexcan_write(reg, ®s->mcr);
> + flexcan_wait_for_frz(dev, 1);
>
> /* Disable all interrupts */
> flexcan_write(0, ®s->imask1);
> @@ -1018,6 +1078,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, ®s->mcr);
> + flexcan_wait_for_frz(dev, 1);
>
> /*
> * Currently we only support newer versions of this core
>
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Industrial Linux Solutions | Phone: +49-231-2826-924 |
Vertretung West/Dortmund | Fax: +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 242 bytes --]
next prev parent reply other threads:[~2014-07-25 20:46 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 ` [PATCH 4/5] net: can: flexcan: wait for completion when entering freeze mode Matthias Klein
2014-07-25 18:50 ` 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 [this message]
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=53D2C22A.80406@pengutronix.de \
--to=mkl@pengutronix.de \
--cc=bigeasy@linutronix.de \
--cc=linux-can@vger.kernel.org \
--cc=matthias.klein@optimeas.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).