Skip to contentSkip to conversation

flexcan: add err_irq handler for flexcan

PATCH Superseded Zhao Qiang 3 participants 6 messages Jul 1, 2014 – Jul 2, 2014 CAN network drivers

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Zhao Qiang

author
#0 lore raw

PATCH v4 1/1 flexcan: add err_irq handler for flexcan

when flexcan is not physically linked, command 'cantest' will trigger an err_irq, add err_irq handler for it.

---
Changes for v2:
- use a space instead of tab
- use flexcan_poll_state instead of print
Changes for v3:
- return IRQ_HANDLED if err is triggered
- stop transmitted packets when there is an err_interrupt
Changes for v4:
- call flexcan_irq

 drivers/net/can/flexcan.c | 38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)
drivers/net/can/flexcan.c +37 -1
index f425ec2..098fcac 100644
@@ -208,6 +208,7 @@ struct flexcan_priv {
 	void __iomem *base;
 	u32 reg_esr;
 	u32 reg_ctrl_default;
+	int err_irq;
 
 	struct clk *clk_ipg;
 	struct clk *clk_per;
@@ -744,6 +745,27 @@ static irqreturn_t flexcan_irq(int irq, void *dev_id)
 	return IRQ_HANDLED;
 }
 
+static irqreturn_t flexcan_err_irq(int irq, void *dev_id)
+{
+	struct net_device *dev = dev_id;
+	struct flexcan_priv *priv = netdev_priv(dev);
+	struct flexcan_regs __iomem *regs = priv->base;
+	u32 reg_ctrl, reg_esr;
+
+	reg_esr = flexcan_read(&regs->esr);
+	reg_ctrl = flexcan_read(&regs->ctrl);
+
+	if (reg_esr & FLEXCAN_ESR_ALL_INT) {
+		if (reg_esr & FLEXCAN_ESR_ERR_INT)
+			flexcan_write(reg_ctrl & ~FLEXCAN_CTRL_ERR_MSK,
+				      &regs->ctrl);
+		flexcan_irq(irq, dev);
+		return IRQ_HANDLED;
+	}
+
+	return IRQ_NONE;
+}
+
 static void flexcan_set_bittiming(struct net_device *dev)
 {
 	const struct flexcan_priv *priv = netdev_priv(dev);
@@ -944,6 +966,15 @@ static int flexcan_open(struct net_device *dev)
 	if (err)
 		goto out_close;
 
+	if (priv->err_irq) {
+		err = request_irq(priv->err_irq, flexcan_err_irq, IRQF_SHARED,
+				  dev->name, dev);
+		if (err) {
+			free_irq(priv->err_irq, dev);
+			goto out_free_irq;
+		}
+	}
+
 	/* start chip and queuing */
 	err = flexcan_chip_start(dev);
 	if (err)
@@ -1099,7 +1130,7 @@ static int flexcan_probe(struct platform_device *pdev)
 	struct resource *mem;
 	struct clk *clk_ipg = NULL, *clk_per = NULL;
 	void __iomem *base;
-	int err, irq;
+	int err, irq, err_irq;
 	u32 clock_freq = 0;
 
 	if (pdev->dev.of_node)
@@ -1126,6 +1157,10 @@ static int flexcan_probe(struct platform_device *pdev)
 	if (irq <= 0)
 		return -ENODEV;
 
+	err_irq = platform_get_irq(pdev, 1);
+	if (err_irq <= 0)
+		err_irq = 0;
+
 	base = devm_ioremap_resource(&pdev->dev, mem);
 	if (IS_ERR(base))
 		return PTR_ERR(base);
@@ -1149,6 +1184,7 @@ static int flexcan_probe(struct platform_device *pdev)
 	dev->flags |= IFF_ECHO;
 
 	priv = netdev_priv(dev);
+	priv->err_irq = err_irq;
 	priv->can.clock.freq = clock_freq;
 	priv->can.bittiming_const = &flexcan_bittiming_const;
 	priv->can.do_set_mode = flexcan_set_mode;
Signature · 1 line
1.8.5
#1 lore raw
Quoted 38 lines · Zhao Qiang

when flexcan is not physically linked, command 'cantest' will trigger an err_irq, add err_irq handler for it.

---
Changes for v2:
- use a space instead of tab
- use flexcan_poll_state instead of print
Changes for v3:
- return IRQ_HANDLED if err is triggered
- stop transmitted packets when there is an err_interrupt
Changes for v4:
- call flexcan_irq

 drivers/net/can/flexcan.c | 38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)
drivers/net/can/flexcan.c +6 -0
index f425ec2..098fcac 100644
@@ -208,6 +208,7 @@ struct flexcan_priv {
 	void __iomem *base;
 	u32 reg_esr;
 	u32 reg_ctrl_default;
+	int err_irq;
 
 	struct clk *clk_ipg;
 	struct clk *clk_per;
@@ -744,6 +745,27 @@ static irqreturn_t flexcan_irq(int irq, void *dev_id)
 	return IRQ_HANDLED;
 }
 
+static irqreturn_t flexcan_err_irq(int irq, void *dev_id)
+{
+	struct net_device *dev = dev_id;
+	struct flexcan_priv *priv = netdev_priv(dev);
+	struct flexcan_regs __iomem *regs = priv->base;

drivers/net/can/flexcan.c static irqreturn_t flexcan_irq(int irq, void *dev_id) · line 762

Quoted 10 lines
drivers/net/can/flexcan.c +10 -0
+	u32 reg_ctrl, reg_esr;
+
+	reg_esr = flexcan_read(&regs->esr);
+	reg_ctrl = flexcan_read(&regs->ctrl);
+
+	if (reg_esr & FLEXCAN_ESR_ALL_INT) {
+		if (reg_esr & FLEXCAN_ESR_ERR_INT)
+			flexcan_write(reg_ctrl & ~FLEXCAN_CTRL_ERR_MSK,
+				      &regs->ctrl);
+		flexcan_irq(irq, dev);

I still don't understand why you need a special flexcan_err_irq() function. Why don't you just call:

request_irq(priv->err_irq, flexcan_irq, IRQF_SHARED, dev->name, dev);

instead?

Quoted 10 lines
drivers/net/can/flexcan.c +6 -0
+		return IRQ_HANDLED;
+	}
+
+	return IRQ_NONE;
+}
+
 static void flexcan_set_bittiming(struct net_device *dev)
 {
 	const struct flexcan_priv *priv = netdev_priv(dev);

drivers/net/can/flexcan.c static int flexcan_open(struct net_device *dev) · line 973

Quoted 8 lines
drivers/net/can/flexcan.c +5 -0
@@ -944,6 +966,15 @@ static int flexcan_open(struct net_device *dev)
 	if (err)
 		goto out_close;
 
+	if (priv->err_irq) {
+		err = request_irq(priv->err_irq, flexcan_err_irq, IRQF_SHARED,
+				  dev->name, dev);
+		if (err) {
+			free_irq(priv->err_irq, dev);

Why do you want to free the err_irq, if requesting the err_irq fails? However you should adjust all error cleanup path following this request irq, so that it will be freed.

BTW: where is the corresponding free_irq() in flexcan_close()?

Quoted 29 lines
drivers/net/can/flexcan.c +9 -1
+			goto out_free_irq;
+		}
+	}
+
 	/* start chip and queuing */
 	err = flexcan_chip_start(dev);
 	if (err)
@@ -1099,7 +1130,7 @@ static int flexcan_probe(struct platform_device *pdev)
 	struct resource *mem;
 	struct clk *clk_ipg = NULL, *clk_per = NULL;
 	void __iomem *base;
-	int err, irq;
+	int err, irq, err_irq;
 	u32 clock_freq = 0;
 
 	if (pdev->dev.of_node)
@@ -1126,6 +1157,10 @@ static int flexcan_probe(struct platform_device *pdev)
 	if (irq <= 0)
 		return -ENODEV;
 
+	err_irq = platform_get_irq(pdev, 1);
+	if (err_irq <= 0)
+		err_irq = 0;
+
 	base = devm_ioremap_resource(&pdev->dev, mem);
 	if (IS_ERR(base))
 		return PTR_ERR(base);

drivers/net/can/flexcan.c static int flexcan_probe(struct platform_device *pdev) · line 1190

Quoted 7 lines
drivers/net/can/flexcan.c +1 -0
@@ -1149,6 +1184,7 @@ static int flexcan_probe(struct platform_device *pdev)
 	dev->flags |= IFF_ECHO;
 
 	priv = netdev_priv(dev);
+	priv->err_irq = err_irq;
 	priv->can.clock.freq = clock_freq;
 	priv->can.bittiming_const = &flexcan_bittiming_const;
 	priv->can.do_set_mode = flexcan_set_mode;

Marc

Signature · 4 lines
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   |
#2 lore raw
Quoted 60 lines · Marc Kleine-Budde

-----Original Message-----
From: Marc Kleine-Budde [mailto:[email protected]]
Sent: Tuesday, July 01, 2014 6:04 PM
To: Zhao Qiang-B45475; [email protected]; [email protected]; [email protected]; Wood Scott-B07421
Subject: Re: [PATCH v4] flexcan: add err_irq handler for flexcan

Quoted 10 lines · Zhao Qiang

when flexcan is not physically linked, command 'cantest' will trigger an err_irq, add err_irq handler for it.

---
Changes for v2:
- use a space instead of tab
- use flexcan_poll_state instead of print Changes for v3:
- return IRQ_HANDLED if err is triggered
- stop transmitted packets when there is an err_interrupt Changes

for

drivers/net/can/flexcan.c static irqreturn_t flexcan_irq(int irq, void

Quoted 19 lines

v4:
- call flexcan_irq

 drivers/net/can/flexcan.c | 38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)
drivers/net/can/flexcan.c +1 -0
index f425ec2..098fcac 100644
@@ -208,6 +208,7 @@ struct flexcan_priv {
 	void __iomem *base;
 	u32 reg_esr;
 	u32 reg_ctrl_default;
+	int err_irq;
 
 	struct clk *clk_ipg;
 	struct clk *clk_per;
@@ -744,6 +745,27 @@ static irqreturn_t flexcan_irq(int irq, void

*dev_id)

Quoted 7 lines
drivers/net/can/flexcan.c +4 -0
 	return IRQ_HANDLED;
 }
 
+static irqreturn_t flexcan_err_irq(int irq, void *dev_id) {
+	struct net_device *dev = dev_id;
+	struct flexcan_priv *priv = netdev_priv(dev);
+	struct flexcan_regs __iomem *regs = priv->base;

drivers/net/can/flexcan.c static irqreturn_t flexcan_irq(int irq, void *dev_id) · line 762

Quoted 10 lines
drivers/net/can/flexcan.c +10 -0
+	u32 reg_ctrl, reg_esr;
+
+	reg_esr = flexcan_read(&regs->esr);
+	reg_ctrl = flexcan_read(&regs->ctrl);
+
+	if (reg_esr & FLEXCAN_ESR_ALL_INT) {
+		if (reg_esr & FLEXCAN_ESR_ERR_INT)
+			flexcan_write(reg_ctrl & ~FLEXCAN_CTRL_ERR_MSK,
+				      &regs->ctrl);
+		flexcan_irq(irq, dev);

I still don't understand why you need a special flexcan_err_irq() function. Why don't you just call:

Quoted 3 lines

request_irq(priv->err_irq, flexcan_irq, IRQF_SHARED, dev->name, dev);

instead?

Flexcan_irq is for flexcan normal interrupt(such as Message Buffer, Wake up and so on). And it will return IRQ_HANDLED if flexcan_irq is triggered.
But err_irq is shared with other devices, it should return IRQ_HANDLED when the interrupt is triggered by flexcan device, if not return IRQ_NONE.

Quoted 23 lines
Quoted 11 lines
drivers/net/can/flexcan.c +7 -0
+		return IRQ_HANDLED;
+	}
+
+	return IRQ_NONE;
+}
+
 static void flexcan_set_bittiming(struct net_device *dev)  {
 	const struct flexcan_priv *priv = netdev_priv(dev); @@ -944,6
+966,15 @@ static int flexcan_open(struct net_device *dev)
 	if (err)

drivers/net/can/flexcan.c static int flexcan_open(struct net_device *dev) · line 973

Quoted 7 lines
drivers/net/can/flexcan.c +5 -0
 		goto out_close;
 
+	if (priv->err_irq) {
+		err = request_irq(priv->err_irq, flexcan_err_irq, IRQF_SHARED,
+				  dev->name, dev);
+		if (err) {
+			free_irq(priv->err_irq, dev);

Why do you want to free the err_irq, if requesting the err_irq fails? However you should adjust all error cleanup path following this request irq, so that it will be freed.

Quoted 1 line

BTW: where is the corresponding free_irq() in flexcan_close()?

Here, I think you are right. Thanks for your reminder. I will modify it.

Quoted 48 lines

drivers/net/can/flexcan.c static int flexcan_probe(struct platform_device

Quoted 9 lines
drivers/net/can/flexcan.c +4 -0
+			goto out_free_irq;
+		}
+	}
+
 	/* start chip and queuing */
 	err = flexcan_chip_start(dev);
 	if (err)
@@ -1099,7 +1130,7 @@ static int flexcan_probe(struct platform_device

*pdev)

drivers/net/can/flexcan.c static int flexcan_probe(struct platform_device

Quoted 9 lines
drivers/net/can/flexcan.c +1 -1
 	struct resource *mem;
 	struct clk *clk_ipg = NULL, *clk_per = NULL;
 	void __iomem *base;
-	int err, irq;
+	int err, irq, err_irq;
 	u32 clock_freq = 0;
 
 	if (pdev->dev.of_node)
@@ -1126,6 +1157,10 @@ static int flexcan_probe(struct platform_device

*pdev)

drivers/net/can/flexcan.c static int flexcan_probe(struct platform_device

Quoted 11 lines
drivers/net/can/flexcan.c +4 -0
 	if (irq <= 0)
 		return -ENODEV;
 
+	err_irq = platform_get_irq(pdev, 1);
+	if (err_irq <= 0)
+		err_irq = 0;
+
 	base = devm_ioremap_resource(&pdev->dev, mem);
 	if (IS_ERR(base))
 		return PTR_ERR(base);
@@ -1149,6 +1184,7 @@ static int flexcan_probe(struct platform_device

*pdev)

drivers/net/can/flexcan.c static int flexcan_probe(struct platform_device *pdev) · line 1190

Quoted 8 lines
drivers/net/can/flexcan.c +1 -0
 	dev->flags |= IFF_ECHO;
 
 	priv = netdev_priv(dev);
+	priv->err_irq = err_irq;
 	priv->can.clock.freq = clock_freq;
 	priv->can.bittiming_const = &flexcan_bittiming_const;
 	priv->can.do_set_mode = flexcan_set_mode;

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   |
#3 lore raw
Quoted 18 lines · On 07/02/2014 04:00 AM, [email protected] wrote:
Quoted 18 lines
Quoted 4 lines
drivers/net/can/flexcan.c +4 -0
+static irqreturn_t flexcan_err_irq(int irq, void *dev_id) {
+	struct net_device *dev = dev_id;
+	struct flexcan_priv *priv = netdev_priv(dev);
+	struct flexcan_regs __iomem *regs = priv->base;

drivers/net/can/flexcan.c static irqreturn_t flexcan_irq(int irq, void *dev_id) · line 762

Quoted 10 lines
drivers/net/can/flexcan.c +10 -0
+	u32 reg_ctrl, reg_esr;
+
+	reg_esr = flexcan_read(&regs->esr);
+	reg_ctrl = flexcan_read(&regs->ctrl);
+
+	if (reg_esr & FLEXCAN_ESR_ALL_INT) {
+		if (reg_esr & FLEXCAN_ESR_ERR_INT)
+			flexcan_write(reg_ctrl & ~FLEXCAN_CTRL_ERR_MSK,
+				      &regs->ctrl);
+		flexcan_irq(irq, dev);

I still don't understand why you need a special flexcan_err_irq() function. Why don't you just call:

Quoted 8 lines
Quoted 3 lines

request_irq(priv->err_irq, flexcan_irq, IRQF_SHARED, dev->name, dev);

instead?

Flexcan_irq is for flexcan normal interrupt(such as Message Buffer, Wake up and so on). And it will return IRQ_HANDLED if flexcan_irq is triggered.
But err_irq is shared with other devices, it should return IRQ_HANDLED when the interrupt is triggered by flexcan device, if not return IRQ_NONE.

What about fixing flexcan_irq() first and the make use of it?

Marc

Signature · 4 lines
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   |
#4 lore raw
Quoted 30 lines · Marc Kleine-Budde

-----Original Message-----
From: Marc Kleine-Budde [mailto:[email protected]]
Sent: Wednesday, July 02, 2014 3:04 PM
To: Zhao Qiang-B45475; [email protected]; [email protected]; [email protected]; Wood Scott-B07421
Subject: Re: [PATCH v4] flexcan: add err_irq handler for flexcan

Quoted 22 lines · On 07/02/2014 04:00 AM, [email protected] wrote:
Quoted 21 lines
Quoted 4 lines
drivers/net/can/flexcan.c +4 -0
+static irqreturn_t flexcan_err_irq(int irq, void *dev_id) {
+	struct net_device *dev = dev_id;
+	struct flexcan_priv *priv = netdev_priv(dev);
+	struct flexcan_regs __iomem *regs = priv->base;

drivers/net/can/flexcan.c static irqreturn_t flexcan_irq(int irq, void *dev_id) · line 762

Quoted 10 lines
drivers/net/can/flexcan.c +10 -0
+	u32 reg_ctrl, reg_esr;
+
+	reg_esr = flexcan_read(&regs->esr);
+	reg_ctrl = flexcan_read(&regs->ctrl);
+
+	if (reg_esr & FLEXCAN_ESR_ALL_INT) {
+		if (reg_esr & FLEXCAN_ESR_ERR_INT)
+			flexcan_write(reg_ctrl & ~FLEXCAN_CTRL_ERR_MSK,
+				      &regs->ctrl);
+		flexcan_irq(irq, dev);

I still don't understand why you need a special flexcan_err_irq() function. Why don't you just call:

request_irq(priv->err_irq, flexcan_irq, IRQF_SHARED, dev->name, dev);

instead?

Quoted 8 lines
Quoted 1 line

Flexcan_irq is for flexcan normal interrupt(such as Message Buffer,

Wake up and so on).

Quoted 3 lines

And it will return IRQ_HANDLED if flexcan_irq is triggered. But err_irq is shared with other devices, it should return IRQ_HANDLED when the interrupt is triggered by flexcan device, if not return

IRQ_NONE.

What about fixing flexcan_irq() first and the make use of it?

Err_irq is a shared interrupt with other device,
I hope that its handler is independent.
However, if you persist in your opinion, I will do it as you said.

Quoted 8 lines

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   |
#5 lore raw
Quoted 36 lines · On 07/02/2014 10:32 AM, [email protected] wrote:
Quoted 35 lines · Marc Kleine-Budde

-----Original Message-----
From: Marc Kleine-Budde [mailto:[email protected]]
Sent: Wednesday, July 02, 2014 3:04 PM
To: Zhao Qiang-B45475; [email protected]; [email protected]; [email protected]; Wood Scott-B07421
Subject: Re: [PATCH v4] flexcan: add err_irq handler for flexcan

Quoted 18 lines · On 07/02/2014 04:00 AM, [email protected] wrote:
Quoted 18 lines
Quoted 4 lines
drivers/net/can/flexcan.c +4 -0
+static irqreturn_t flexcan_err_irq(int irq, void *dev_id) {
+	struct net_device *dev = dev_id;
+	struct flexcan_priv *priv = netdev_priv(dev);
+	struct flexcan_regs __iomem *regs = priv->base;

drivers/net/can/flexcan.c static irqreturn_t flexcan_irq(int irq, void *dev_id) · line 762

Quoted 10 lines
drivers/net/can/flexcan.c +10 -0
+	u32 reg_ctrl, reg_esr;
+
+	reg_esr = flexcan_read(&regs->esr);
+	reg_ctrl = flexcan_read(&regs->ctrl);
+
+	if (reg_esr & FLEXCAN_ESR_ALL_INT) {
+		if (reg_esr & FLEXCAN_ESR_ERR_INT)
+			flexcan_write(reg_ctrl & ~FLEXCAN_CTRL_ERR_MSK,
+				      &regs->ctrl);
+		flexcan_irq(irq, dev);

I still don't understand why you need a special flexcan_err_irq() function. Why don't you just call:

Quoted 5 lines
Quoted 3 lines

request_irq(priv->err_irq, flexcan_irq, IRQF_SHARED, dev->name, dev);

instead?

Flexcan_irq is for flexcan normal interrupt(such as Message Buffer,

Wake up and so on).

Quoted 3 lines

And it will return IRQ_HANDLED if flexcan_irq is triggered. But err_irq is shared with other devices, it should return IRQ_HANDLED when the interrupt is triggered by flexcan device, if not return

Quoted 7 lines
Quoted 3 lines

IRQ_NONE.

What about fixing flexcan_irq() first and the make use of it?

Err_irq is a shared interrupt with other device,
I hope that its handler is independent.
However, if you persist in your opinion, I will do it as you said.

There is another option, you can move all of the error interrupt handling code from flexcan_irq() to flexcan_irq_err(). To keep the ARM SoCs supported, you need to call flexcan_irq_err() form the flexcan_irq() handler.

What I don't want is a) code duplication and b) no fishy wrapper functions around flexcan_irq() that work around flexcan_irq() returning IRQ_HANDLED unconditionally.

Marc

Signature · 4 lines
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   |