Skip to contentSkip to conversation

can: flexcan: fixes for stop mode

PATCH In review Joakim Zhang 2 participants 11 messages Dec 4, 2019 – Dec 9, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover can: flexcan: fixes for stop mode

Hi Marc,

I removed the patch (can: flexcan: try to exit stop mode during probe stage)
out of this patch set for now. This patch should further discuss with Sean and I will prepare it according to final conclusion. Thanks.

Regards,
Joakim Zhang

Joakim Zhang (5):
can: flexcan: Ack wakeup interrupt separately
can: flexcan: add low power enter/exit acknowledgment helper
can: flexcan: change the way of stop mode acknowledgment
can: flexcan: propagate error value of flexcan_chip_stop()
can: flexcan: add LPSR mode support

Sean Nyekjaer (1):
can: flexcan: fix deadlock when using self wakeup

 drivers/net/can/flexcan.c | 131 +++++++++++++++++++++++---------------
 1 file changed, 79 insertions(+), 52 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v3 1/6 can: flexcan: fix deadlock when using self wakeup

From: Sean Nyekjaer <[email protected]>

When suspending, when there is still can traffic on the interfaces the flexcan immediately wakes the platform again. As it should :-). But it throws this error msg:

[ 3169.378661] PM: noirq suspend of devices failed

On the way down to suspend the interface that throws the error message does call flexcan_suspend but fails to call flexcan_noirq_suspend. That means flexcan_enter_stop_mode is called, but on the way out of suspend the driver only calls flexcan_resume and skips flexcan_noirq_resume, thus it doesn't call flexcan_exit_stop_mode. This leaves the flexcan in stop mode, and with the current driver it can't recover from this even with a soft reboot, it requires a hard reboot.

This patch can fix deadlock when using self wakeup, it happenes to be able to fix another issue that frames out-of-order in first IRQ handler run after wakeup.

In wakeup case, after system resume, frames received out-of-order in first IRQ handler, the problem is wakeup latency from frame reception to IRQ handler is much bigger than the counter overflow. This means it's impossible to sort the CAN frames by timestamp. The reason is that controller exits stop mode during noirq resume, then it can receive the frame immediately. If noirq reusme stage consumes much time, it will extend interrupt response time. So exit stop mode during resume stage instead of noirq resume can fix this issue.

Fixes de3578c198c6 ("can: flexcan: add self wakeup support")

------
ChangeLog:

	V1->V2: *no change.

	V2->V3: *split wakeup interrupt ack into another patch.
---
 drivers/net/can/flexcan.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/net/can/flexcan.c +4 -6
index 2efa06119f68..99aae90c1cdd 100644
@@ -1722,6 +1722,9 @@ static int __maybe_unused flexcan_resume(struct device *device)
 		netif_start_queue(dev);
 		if (device_may_wakeup(device)) {
 			disable_irq_wake(dev->irq);
+			err = flexcan_exit_stop_mode(priv);
+			if (err)
+				return err;
 		} else {
 			err = pm_runtime_force_resume(device);
 			if (err)
@@ -1767,14 +1770,9 @@ static int __maybe_unused flexcan_noirq_resume(struct device *device)
 {
 	struct net_device *dev = dev_get_drvdata(device);
 	struct flexcan_priv *priv = netdev_priv(dev);
-	int err;
 
-	if (netif_running(dev) && device_may_wakeup(device)) {
+	if (netif_running(dev) && device_may_wakeup(device))
 		flexcan_enable_wakeup_irq(priv, false);
-		err = flexcan_exit_stop_mode(priv);
-		if (err)
-			return err;
-	}
 
 	return 0;
 }
Signature · 1 line
2.17.1
#2 lore raw

PATCH v3 2/6 can: flexcan: Ack wakeup interrupt separately

In reply to Joakim Zhang

As FLEXCAN_ESR_ALL_INT is for all bus errors and state change IRQ sources, and FLEXCAN_ESR_WAK_INT does not belong to these. So add wakeup interrupt ack separately to existing ack of the interrupts.

------
ChangeLog:
V3: *split from the patch
can: flexcan: fix deadlock when using self wakeup

---
 drivers/net/can/flexcan.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/net/can/flexcan.c +4 -5
index 99aae90c1cdd..1b33936790b4 100644
@@ -134,8 +134,7 @@
 	(FLEXCAN_ESR_ERR_BUS | FLEXCAN_ESR_ERR_STATE)
 #define FLEXCAN_ESR_ALL_INT \
 	(FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | \
-	 FLEXCAN_ESR_BOFF_INT | FLEXCAN_ESR_ERR_INT | \
-	 FLEXCAN_ESR_WAK_INT)
+	 FLEXCAN_ESR_BOFF_INT | FLEXCAN_ESR_ERR_INT)
 
 /* FLEXCAN interrupt flag register (IFLAG) bits */
 /* Errata ERR005829 step7: Reserve first valid MB */
@@ -960,10 +959,10 @@ static irqreturn_t flexcan_irq(int irq, void *dev_id)
 
 	reg_esr = priv->read(&regs->esr);
 
-	/* ACK all bus error and state change IRQ sources */
-	if (reg_esr & FLEXCAN_ESR_ALL_INT) {
+	/* ACK all bus error, state change and wake IRQ sources */
+	if (reg_esr & (FLEXCAN_ESR_ALL_INT | FLEXCAN_ESR_WAK_INT)) {
 		handled = IRQ_HANDLED;
-		priv->write(reg_esr & FLEXCAN_ESR_ALL_INT, &regs->esr);
+		priv->write(reg_esr & (FLEXCAN_ESR_ALL_INT | FLEXCAN_ESR_WAK_INT), &regs->esr);
 	}
 
 	/* state change interrupt or broken error state quirk fix is enabled */
Signature · 1 line
2.17.1
#3 lore raw

PATCH v3 3/6 can: flexcan: add low power enter/exit acknowledgment helper

In reply to Joakim Zhang

MCR[LPMACK] this read-only bit indicates that FlexCAN is in a lower-power mode (Disabled mode, Doze mode, Stop mode), CPU can poll this bit to know when FlexCAN has actually entered low power mode. Low power enter/exit acknowledgment helper will reduce code duplication for disabled mode, doze mode and stop mode.

-----
ChangeLog:
V3: * split from patch
can: flexcan: change the way of stop mode acknowledgment

---
 drivers/net/can/flexcan.c | 46 +++++++++++++++++++++++++--------------
 1 file changed, 30 insertions(+), 16 deletions(-)
drivers/net/can/flexcan.c +30 -16
index 1b33936790b4..6c4f1bab7042 100644
@@ -388,6 +388,34 @@ static struct flexcan_mb __iomem *flexcan_get_mb(const struct flexcan_priv *priv
 		(&priv->regs->mb[bank][priv->mb_size * mb_index]);
 }
 
+static int flexcan_low_power_enter_ack(struct flexcan_priv *priv)
+{
+	struct flexcan_regs __iomem *regs = priv->regs;
+	unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
+
+	while (timeout-- && !(priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
+		udelay(10);
+
+	if (!(priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
+		return -ETIMEDOUT;
+
+	return 0;
+}
+
+static int flexcan_low_power_exit_ack(struct flexcan_priv *priv)
+{
+	struct flexcan_regs __iomem *regs = priv->regs;
+	unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
+
+	while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
+		udelay(10);
+
+	if (priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK)
+		return -ETIMEDOUT;
+
+	return 0;
+}
+
 static void flexcan_enable_wakeup_irq(struct flexcan_priv *priv, bool enable)
 {
 	struct flexcan_regs __iomem *regs = priv->regs;
@@ -505,39 +533,25 @@ static inline int flexcan_transceiver_disable(const struct flexcan_priv *priv)
 static int flexcan_chip_enable(struct flexcan_priv *priv)
 {
 	struct flexcan_regs __iomem *regs = priv->regs;
-	unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
 	u32 reg;
 
 	reg = priv->read(&regs->mcr);
 	reg &= ~FLEXCAN_MCR_MDIS;
 	priv->write(reg, &regs->mcr);
 
-	while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
-		udelay(10);
-
-	if (priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK)
-		return -ETIMEDOUT;
-
-	return 0;
+	return flexcan_low_power_exit_ack(priv);
 }
 
 static int flexcan_chip_disable(struct flexcan_priv *priv)
 {
 	struct flexcan_regs __iomem *regs = priv->regs;
-	unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
 	u32 reg;
 
 	reg = priv->read(&regs->mcr);
 	reg |= FLEXCAN_MCR_MDIS;
 	priv->write(reg, &regs->mcr);
 
-	while (timeout-- && !(priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
-		udelay(10);
-
-	if (!(priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
-		return -ETIMEDOUT;
-
-	return 0;
+	return flexcan_low_power_enter_ack(priv);
 }
 
 static int flexcan_chip_freeze(struct flexcan_priv *priv)
Signature · 1 line
2.17.1
#4 lore raw

PATCH v3 4/6 can: flexcan: change the way of stop mode acknowledgment

In reply to Joakim Zhang

Stop mode is entered when Stop mode is requested at chip level and MCR[LPM_ACK] is asserted by the FlexCAN.

Double check with IP owner, should poll MCR[LPM_ACK] for stop mode acknowledgment, not the acknowledgment from chip level which is used to gate flexcan clocks.

Fixes 5f186c257fa4 (can: flexcan: fix stop mode acknowledgment)

-----
ChangeLog:

	V1->V2: * no change.

	V2->V3: * spilt the patch.
---
 drivers/net/can/flexcan.c | 17 ++---------------
 1 file changed, 2 insertions(+), 15 deletions(-)
drivers/net/can/flexcan.c +2 -15
index 6c4f1bab7042..19602b77907f 100644
@@ -434,7 +434,6 @@ static void flexcan_enable_wakeup_irq(struct flexcan_priv *priv, bool enable)
 static inline int flexcan_enter_stop_mode(struct flexcan_priv *priv)
 {
 	struct flexcan_regs __iomem *regs = priv->regs;
-	unsigned int ackval;
 	u32 reg_mcr;
 
 	reg_mcr = priv->read(&regs->mcr);
@@ -445,36 +444,24 @@ static inline int flexcan_enter_stop_mode(struct flexcan_priv *priv)
 	regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr,
 			   1 << priv->stm.req_bit, 1 << priv->stm.req_bit);
 
-	/* get stop acknowledgment */
-	if (regmap_read_poll_timeout(priv->stm.gpr, priv->stm.ack_gpr,
-				     ackval, ackval & (1 << priv->stm.ack_bit),
-				     0, FLEXCAN_TIMEOUT_US))
-		return -ETIMEDOUT;
-
-	return 0;
+	return flexcan_low_power_enter_ack(priv);
 }
 
 static inline int flexcan_exit_stop_mode(struct flexcan_priv *priv)
 {
 	struct flexcan_regs __iomem *regs = priv->regs;
-	unsigned int ackval;
 	u32 reg_mcr;
 
 	/* remove stop request */
 	regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr,
 			   1 << priv->stm.req_bit, 0);
 
-	/* get stop acknowledgment */
-	if (regmap_read_poll_timeout(priv->stm.gpr, priv->stm.ack_gpr,
-				     ackval, !(ackval & (1 << priv->stm.ack_bit)),
-				     0, FLEXCAN_TIMEOUT_US))
-		return -ETIMEDOUT;
 
 	reg_mcr = priv->read(&regs->mcr);
 	reg_mcr &= ~FLEXCAN_MCR_SLF_WAK;
 	priv->write(reg_mcr, &regs->mcr);
 
-	return 0;
+	return flexcan_low_power_exit_ack(priv);
 }
 
 static inline void flexcan_error_irq_enable(const struct flexcan_priv *priv)
Signature · 1 line
2.17.1
#5 lore raw

PATCH v3 5/6 can: flexcan: propagate error value of flexcan_chip_stop()

In reply to Joakim Zhang

Propagate error value of flexcan_chip_stop(), since this could be called from flexcan_suspend() in some SoCs which support LPSR mode.

------
ChangeLog:

	V3: * new add.
---
 drivers/net/can/flexcan.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
drivers/net/can/flexcan.c +20 -4
index 19602b77907f..c5e4b6928dee 100644
@@ -1263,14 +1263,19 @@ static int flexcan_chip_start(struct net_device *dev)
  *
  * this functions is entered with clocks enabled
  */
-static void flexcan_chip_stop(struct net_device *dev)
+static int flexcan_chip_stop(struct net_device *dev)
 {
 	struct flexcan_priv *priv = netdev_priv(dev);
 	struct flexcan_regs __iomem *regs = priv->regs;
+	int err;
 
 	/* freeze + disable module */
-	flexcan_chip_freeze(priv);
-	flexcan_chip_disable(priv);
+	err = flexcan_chip_freeze(priv);
+	if (err)
+		return err;
+	err = flexcan_chip_disable(priv);
+	if (err)
+		goto out_chip_unfreeze;
 
 	/* Disable all interrupts */
 	priv->write(0, &regs->imask2);
@@ -1278,8 +1283,19 @@ static void flexcan_chip_stop(struct net_device *dev)
 	priv->write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
 		    &regs->ctrl);
 
-	flexcan_transceiver_disable(priv);
+	err = flexcan_transceiver_disable(priv);
+	if (err)
+		goto out_chip_enable;
+
 	priv->can.state = CAN_STATE_STOPPED;
+
+	return 0;
+
+out_chip_enable:
+	flexcan_chip_enable(priv);
+out_chip_unfreeze:
+	flexcan_chip_unfreeze(priv);
+	return err;
 }
 
 static int flexcan_open(struct net_device *dev)
Signature · 1 line
2.17.1
#6 lore raw

PATCH v3 6/6 can: flexcan: add LPSR mode support

In reply to Joakim Zhang

For i.MX7D LPSR mode, the controller will lost power and got the configuration state lost after system resume back. (coming i.MX8QM/QXP will also completely power off the domain, the controller state will be lost and needs restore).
So we need to set pinctrl state again and re-start chip to do re-configuration after resume.

For wakeup case, it should not set pinctrl to sleep state by pinctrl_pm_select_sleep_state.
For interface is not up before suspend case, we don't need re-configure as it will be configured by user later by interface up.

------
ChangeLog:

	V1->V2: *no change.

	V2->V3: *add error handling for pinctrl_pm_xx_xx_state()
		 function.
---
 drivers/net/can/flexcan.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
drivers/net/can/flexcan.c +19 -6
index c5e4b6928dee..3570ebe3b8f2 100644
@@ -26,6 +26,7 @@
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 #include <linux/regulator/consumer.h>
+#include <linux/pinctrl/consumer.h>
 #include <linux/regmap.h>
 
 #define DRV_NAME			"flexcan"
@@ -1700,7 +1701,7 @@ static int __maybe_unused flexcan_suspend(struct device *device)
 {
 	struct net_device *dev = dev_get_drvdata(device);
 	struct flexcan_priv *priv = netdev_priv(dev);
-	int err = 0;
+	int err;
 
 	if (netif_running(dev)) {
 		/* if wakeup is enabled, enter stop mode
@@ -1712,25 +1713,31 @@ static int __maybe_unused flexcan_suspend(struct device *device)
 			if (err)
 				return err;
 		} else {
-			err = flexcan_chip_disable(priv);
+			err = flexcan_chip_stop(dev);
 			if (err)
 				return err;
 
 			err = pm_runtime_force_suspend(device);
+			if (err)
+				return err;
+
+			err = pinctrl_pm_select_sleep_state(device);
+			if (err)
+				return err;
 		}
 		netif_stop_queue(dev);
 		netif_device_detach(dev);
 	}
 	priv->can.state = CAN_STATE_SLEEPING;
 
-	return err;
+	return 0;
 }
 
 static int __maybe_unused flexcan_resume(struct device *device)
 {
 	struct net_device *dev = dev_get_drvdata(device);
 	struct flexcan_priv *priv = netdev_priv(dev);
-	int err = 0;
+	int err;
 
 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
 	if (netif_running(dev)) {
@@ -1742,15 +1749,21 @@ static int __maybe_unused flexcan_resume(struct device *device)
 			if (err)
 				return err;
 		} else {
+			err = pinctrl_pm_select_default_state(device);
+			if (err)
+				return err;
+
 			err = pm_runtime_force_resume(device);
 			if (err)
 				return err;
 
-			err = flexcan_chip_enable(priv);
+			err = flexcan_chip_start(dev);
+			if (err)
+				return err;
 		}
 	}
 
-	return err;
+	return 0;
 }
 
 static int __maybe_unused flexcan_runtime_suspend(struct device *device)
Signature · 1 line
2.17.1
#7 lore raw
In reply to Joakim Zhang
Quoted 9 lines

-----Original Message-----
From: Joakim Zhang <[email protected]>
Sent: 2019年12月4日 19:36
To: [email protected]; [email protected]; [email protected]
Cc: dl-linux-imx <[email protected]>; [email protected]; Joakim Zhang <[email protected]>
Subject: [PATCH V3 0/6] can: flexcan: fixes for stop mode

Quoted 5 lines

Hi Marc,

I removed the patch (can: flexcan: try to exit stop mode during probe stage)
out of this patch set for now. This patch should further discuss with Sean and I will prepare it according to final conclusion. Thanks.

Hi Marc,

After discussed with Sean, CAN-IP stuck in stop mode disappeared with patch(can: flexcan: fix deadlock when using self wakeup), so I think we don't need another patch to fix the same issue. But this fix patch had better go to stable tree.

Any comments about this patch set?

Best Regards,
Joakim Zhang

Quoted 18 lines

Regards,
Joakim Zhang

Joakim Zhang (5):
can: flexcan: Ack wakeup interrupt separately
can: flexcan: add low power enter/exit acknowledgment helper
can: flexcan: change the way of stop mode acknowledgment
can: flexcan: propagate error value of flexcan_chip_stop()
can: flexcan: add LPSR mode support

Sean Nyekjaer (1):
can: flexcan: fix deadlock when using self wakeup

 drivers/net/can/flexcan.c | 131 +++++++++++++++++++++++---------------
 1 file changed, 79 insertions(+), 52 deletions(-)

--
2.17.1

#8 lore raw
In reply to Joakim Zhang
Quoted 6 lines · Joakim Zhang

Hi Marc,

I removed the patch (can: flexcan: try to exit stop mode during probe stage)
out of this patch set for now. This patch should further discuss with Sean and I will prepare it according to final conclusion. Thanks.

Quoted 7 lines

Regards,
Joakim Zhang

Joakim Zhang (5):
can: flexcan: Ack wakeup interrupt separately
can: flexcan: add low power enter/exit acknowledgment helper
can: flexcan: change the way of stop mode acknowledgment

Above 3 applied to linux-can.

Quoted 2 lines

can: flexcan: propagate error value of flexcan_chip_stop()
can: flexcan: add LPSR mode support

Above 2 applied to linux-can-next

Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |
#9 lore raw
Quoted 19 lines

-----Original Message-----
From: Marc Kleine-Budde <[email protected]>
Sent: 2019年12月7日 22:11
To: Joakim Zhang <[email protected]>; [email protected]; [email protected]
Cc: dl-linux-imx <[email protected]>; [email protected]
Subject: Re: [PATCH V3 0/6] can: flexcan: fixes for stop mode

Quoted 9 lines · Joakim Zhang

Hi Marc,

I removed the patch (can: flexcan: try to exit stop mode during
probe stage) out of this patch set for now. This patch should further discuss with Sean and I will prepare it according to final conclusion. Thanks.

Regards,
Joakim Zhang

Quoted 6 lines
Quoted 4 lines

Joakim Zhang (5):
can: flexcan: Ack wakeup interrupt separately
can: flexcan: add low power enter/exit acknowledgment helper
can: flexcan: change the way of stop mode acknowledgment

Above 3 applied to linux-can.

Hi Marc,

From below link, I have not found the patch: can: flexcan: Ack wakeup interrupt separately
https://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can.git/log/?h=linux-can-fixes-for-5.5-20191208

Quoted 4 lines
Quoted 2 lines

can: flexcan: propagate error value of flexcan_chip_stop()
can: flexcan: add LPSR mode support

Above 2 applied to linux-can-next

I also have not found these two patch on linux-can-next, which branch has you pushed?

Thanks.

Best Regards,
Joakim Zhang

Quoted 6 lines

Marc
--

Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |
#10 lore raw
Quoted 24 lines · Joakim Zhang
Quoted 24 lines

-----Original Message-----
From: Marc Kleine-Budde <[email protected]>
Sent: 2019年12月7日 22:11
To: Joakim Zhang <[email protected]>; [email protected]; [email protected]
Cc: dl-linux-imx <[email protected]>; [email protected]
Subject: Re: [PATCH V3 0/6] can: flexcan: fixes for stop mode

Quoted 13 lines · Joakim Zhang

Hi Marc,

I removed the patch (can: flexcan: try to exit stop mode during
probe stage) out of this patch set for now. This patch should further discuss with Sean and I will prepare it according to final conclusion. Thanks.

Regards,
Joakim Zhang

Joakim Zhang (5):
can: flexcan: Ack wakeup interrupt separately
can: flexcan: add low power enter/exit acknowledgment helper
can: flexcan: change the way of stop mode acknowledgment

Quoted 4 lines
Quoted 1 line

Above 3 applied to linux-can.

From below link, I have not found the patch: can: flexcan: Ack wakeup interrupt separately
https://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can.git/log/?h=linux-can-fixes-for-5.5-20191208

Right, "Ack wakeup interrupt separately" went into linux-can-next.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

can: flexcan: propagate error value of flexcan_chip_stop()
can: flexcan: add LPSR mode support

Above 2 applied to linux-can-next

I also have not found these two patch on linux-can-next, which branch has you pushed?

It's in linux-can-test/testing.

I just pushed both linux-can/testing and linux-can-test/testing.

Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |