linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/3] mtd: atmel_nand: add NFC status error check
@ 2014-06-10  9:50 Josh Wu
  2014-06-10  9:50 ` [PATCH v2 2/3] mtd: atmel_nand: implement the nfc_device_ready() by checking the R/B bit Josh Wu
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Josh Wu @ 2014-06-10  9:50 UTC (permalink / raw)
  To: linux-arm-kernel

Add a new function to read the NFC status. Meantime, this function will
theck if there is any errors in NFC.

Signed-off-by: Josh Wu <josh.wu@atmel.com>
---
 drivers/mtd/nand/atmel_nand.c     |   24 +++++++++++++++++++++++-
 drivers/mtd/nand/atmel_nand_nfc.h |    4 ++++
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/atmel_nand.c b/drivers/mtd/nand/atmel_nand.c
index ec97ccb..25d9921 100644
--- a/drivers/mtd/nand/atmel_nand.c
+++ b/drivers/mtd/nand/atmel_nand.c
@@ -1572,6 +1572,25 @@ static int atmel_hw_nand_init_params(struct platform_device *pdev,
 	return 0;
 }
 
+static inline u32 nfc_read_status(struct atmel_nand_host *host)
+{
+	u32 err_flags = NFC_SR_DTOE | NFC_SR_UNDEF | NFC_SR_AWB | NFC_SR_ASE;
+	u32 nfc_status = nfc_readl(host->nfc->hsmc_regs, SR);
+
+	if (unlikely(nfc_status & err_flags)) {
+		if (nfc_status & NFC_SR_DTOE)
+			dev_err(host->dev, "NFC: Waiting Nand R/B Timeout Error\n");
+		else if (nfc_status & NFC_SR_UNDEF)
+			dev_err(host->dev, "NFC: Access Undefined Area Error\n");
+		else if (nfc_status & NFC_SR_AWB)
+			dev_err(host->dev, "NFC: Access memory While NFC is busy\n");
+		else if (nfc_status & NFC_SR_ASE)
+			dev_err(host->dev, "NFC: Access memory Size Error\n");
+	}
+
+	return nfc_status;
+}
+
 /* SMC interrupt service routine */
 static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
 {
@@ -1579,7 +1598,7 @@ static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
 	u32 status, mask, pending;
 	irqreturn_t ret = IRQ_HANDLED;
 
-	status = nfc_readl(host->nfc->hsmc_regs, SR);
+	status = nfc_read_status(host);
 	mask = nfc_readl(host->nfc->hsmc_regs, IMR);
 	pending = status & mask;
 
@@ -2210,6 +2229,9 @@ static int atmel_nand_nfc_probe(struct platform_device *pdev)
 		}
 	}
 
+	nfc_writel(nfc->hsmc_regs, IDR, 0xffffffff);
+	nfc_readl(nfc->hsmc_regs, SR);	/* clear the NFC_SR */
+
 	nfc->is_initialized = true;
 	dev_info(&pdev->dev, "NFC is probed.\n");
 	return 0;
diff --git a/drivers/mtd/nand/atmel_nand_nfc.h b/drivers/mtd/nand/atmel_nand_nfc.h
index 4efd117..85b8ca6 100644
--- a/drivers/mtd/nand/atmel_nand_nfc.h
+++ b/drivers/mtd/nand/atmel_nand_nfc.h
@@ -37,6 +37,10 @@
 #define ATMEL_HSMC_NFC_SR	0x08		/* NFC Status Register */
 #define		NFC_SR_XFR_DONE		(1 << 16)
 #define		NFC_SR_CMD_DONE		(1 << 17)
+#define		NFC_SR_DTOE		(1 << 20)
+#define		NFC_SR_UNDEF		(1 << 21)
+#define		NFC_SR_AWB		(1 << 22)
+#define		NFC_SR_ASE		(1 << 23)
 #define		NFC_SR_RB_EDGE		(1 << 24)
 
 #define ATMEL_HSMC_NFC_IER	0x0c
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 2/3] mtd: atmel_nand: implement the nfc_device_ready() by checking the R/B bit
  2014-06-10  9:50 [PATCH v2 1/3] mtd: atmel_nand: add NFC status error check Josh Wu
@ 2014-06-10  9:50 ` Josh Wu
  2014-06-10  9:50 ` [PATCH v2 3/3] mtd: atmel_nand: NFC: support multiple interrupt handling Josh Wu
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Josh Wu @ 2014-06-10  9:50 UTC (permalink / raw)
  To: linux-arm-kernel

In nfc_device_ready(), it's more reasonable to check R/B bit in NFC_SR
than waiting for the R/B interrupt. It cost less time.

Signed-off-by: Josh Wu <josh.wu@atmel.com>
---
 drivers/mtd/nand/atmel_nand.c |   14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/nand/atmel_nand.c b/drivers/mtd/nand/atmel_nand.c
index 25d9921..88f6dc0 100644
--- a/drivers/mtd/nand/atmel_nand.c
+++ b/drivers/mtd/nand/atmel_nand.c
@@ -1661,11 +1661,19 @@ static int nfc_send_command(struct atmel_nand_host *host,
 
 static int nfc_device_ready(struct mtd_info *mtd)
 {
+	u32 status, mask;
 	struct nand_chip *nand_chip = mtd->priv;
 	struct atmel_nand_host *host = nand_chip->priv;
-	if (!nfc_wait_interrupt(host, NFC_SR_RB_EDGE))
-		return 1;
-	return 0;
+
+	status = nfc_read_status(host);
+	mask = nfc_readl(host->nfc->hsmc_regs, IMR);
+
+	/* The mask should be 0. If not we may lost interrupts */
+	if (unlikely(mask & status))
+		dev_err(host->dev, "Lost the interrupt flags: 0x%08x\n",
+				mask & status);
+
+	return status & NFC_SR_RB_EDGE;
 }
 
 static void nfc_select_chip(struct mtd_info *mtd, int chip)
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 3/3] mtd: atmel_nand: NFC: support multiple interrupt handling
  2014-06-10  9:50 [PATCH v2 1/3] mtd: atmel_nand: add NFC status error check Josh Wu
  2014-06-10  9:50 ` [PATCH v2 2/3] mtd: atmel_nand: implement the nfc_device_ready() by checking the R/B bit Josh Wu
@ 2014-06-10  9:50 ` Josh Wu
  2014-07-16 10:06 ` [PATCH v2 1/3] mtd: atmel_nand: add NFC status error check Josh Wu
  2014-07-22  3:12 ` Brian Norris
  3 siblings, 0 replies; 6+ messages in thread
From: Josh Wu @ 2014-06-10  9:50 UTC (permalink / raw)
  To: linux-arm-kernel

From: Josh Wu <Josh.wu@atmel.com>

It fixes following error sometime happens during the NFC data transfer:
  atmel_nand 80000000.nand: Time out to wait for interrupt: 0x00010000
  atmel_nand 80000000.nand: something wrong, No XFR_DONE interrupt comes.

The root cause is in current interrupt handler, we read the ISR but
only handle one interrupt. If there are more than one interrupt come
together, then the second one will be lost.

During the NFC data transfer. There is possible two NFC interrupts:
NFC_CMD_DONE and NFC_XFR_DONE, come in same time.

NFC_CMD_DONE means NFC command is send. and NFC_XFR_DONE means NFC data
is transfered.

This patch can handle multiple NFC interrupts in same time. And during
the NFC data transfer, we need to wait for two NFC interrupts:
NFC_CMD_DONE and NFC_XFR_DONE.

Also we seperate the completion initialization code to a
nfc_prepare_interrupt(), which is pair with nfc_wait_interrupt().

We call nfc_prepare_interrupt() before send out nfc command to make sure
no interrupt lost.

Reported-by: Matthieu CRAPET <Matthieu.CRAPET@ingenico.com>
Signed-off-by: Josh Wu <josh.wu@atmel.com>
---
v1 -> v2:
  1. merged two patches into one.
  2. split the original nfc_wait_interrupt() into two functions to make
     sure completion is initialized before we wait the interrupt.
  3. Disable the interrupt if nfc_wait_interrupt() is timeout.

 drivers/mtd/nand/atmel_nand.c |   84 ++++++++++++++++++++++++++++++-----------
 1 file changed, 62 insertions(+), 22 deletions(-)

diff --git a/drivers/mtd/nand/atmel_nand.c b/drivers/mtd/nand/atmel_nand.c
index 88f6dc0..182aa40 100644
--- a/drivers/mtd/nand/atmel_nand.c
+++ b/drivers/mtd/nand/atmel_nand.c
@@ -97,7 +97,9 @@ struct atmel_nfc {
 	bool			write_by_sram;
 
 	bool			is_initialized;
-	struct completion	comp_nfc;
+	struct completion	comp_ready;
+	struct completion	comp_cmd_done;
+	struct completion	comp_xfer_done;
 
 	/* Point to the sram bank which include readed data via NFC */
 	void __iomem		*data_in_sram;
@@ -1596,44 +1598,80 @@ static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
 {
 	struct atmel_nand_host *host = dev_id;
 	u32 status, mask, pending;
-	irqreturn_t ret = IRQ_HANDLED;
+	irqreturn_t ret = IRQ_NONE;
 
 	status = nfc_read_status(host);
 	mask = nfc_readl(host->nfc->hsmc_regs, IMR);
 	pending = status & mask;
 
 	if (pending & NFC_SR_XFR_DONE) {
-		complete(&host->nfc->comp_nfc);
+		complete(&host->nfc->comp_xfer_done);
 		nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE);
-	} else if (pending & NFC_SR_RB_EDGE) {
-		complete(&host->nfc->comp_nfc);
+		ret = IRQ_HANDLED;
+	}
+	if (pending & NFC_SR_RB_EDGE) {
+		complete(&host->nfc->comp_ready);
 		nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_RB_EDGE);
-	} else if (pending & NFC_SR_CMD_DONE) {
-		complete(&host->nfc->comp_nfc);
+		ret = IRQ_HANDLED;
+	}
+	if (pending & NFC_SR_CMD_DONE) {
+		complete(&host->nfc->comp_cmd_done);
 		nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE);
-	} else {
-		ret = IRQ_NONE;
+		ret = IRQ_HANDLED;
 	}
 
 	return ret;
 }
 
 /* NFC(Nand Flash Controller) related functions */
-static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
+static void nfc_prepare_interrupt(struct atmel_nand_host *host, u32 flag)
 {
-	unsigned long timeout;
-	init_completion(&host->nfc->comp_nfc);
+	if (flag & NFC_SR_XFR_DONE)
+		init_completion(&host->nfc->comp_xfer_done);
+
+	if (flag & NFC_SR_RB_EDGE)
+		init_completion(&host->nfc->comp_ready);
+
+	if (flag & NFC_SR_CMD_DONE)
+		init_completion(&host->nfc->comp_cmd_done);
 
 	/* Enable interrupt that need to wait for */
 	nfc_writel(host->nfc->hsmc_regs, IER, flag);
+}
 
-	timeout = wait_for_completion_timeout(&host->nfc->comp_nfc,
-			msecs_to_jiffies(NFC_TIME_OUT_MS));
-	if (timeout)
-		return 0;
+static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
+{
+	int i, index = 0;
+	struct completion *comp[3];	/* Support 3 interrupt completion */
+
+	if (flag & NFC_SR_XFR_DONE)
+		comp[index++] = &host->nfc->comp_xfer_done;
+
+	if (flag & NFC_SR_RB_EDGE)
+		comp[index++] = &host->nfc->comp_ready;
 
-	/* Time out to wait for the interrupt */
+	if (flag & NFC_SR_CMD_DONE)
+		comp[index++] = &host->nfc->comp_cmd_done;
+
+	if (index == 0) {
+		dev_err(host->dev, "Unkown interrupt flag: 0x%08x\n", flag);
+		return -EINVAL;
+	}
+
+	for (i = 0; i < index; i++) {
+		if (wait_for_completion_timeout(comp[i],
+				msecs_to_jiffies(NFC_TIME_OUT_MS)))
+			continue;	/* wait for next completion */
+		else
+			goto err_timeout;
+	}
+
+	return 0;
+
+err_timeout:
 	dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag);
+	/* Disable the interrupt as it is not handled by interrupt handler */
+	nfc_writel(host->nfc->hsmc_regs, IDR, flag);
 	return -ETIMEDOUT;
 }
 
@@ -1641,6 +1679,9 @@ static int nfc_send_command(struct atmel_nand_host *host,
 	unsigned int cmd, unsigned int addr, unsigned char cycle0)
 {
 	unsigned long timeout;
+	u32 flag = NFC_SR_CMD_DONE;
+	flag |= cmd & NFCADDR_CMD_DATAEN ? NFC_SR_XFR_DONE : 0;
+
 	dev_dbg(host->dev,
 		"nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
 		cmd, addr, cycle0);
@@ -1654,9 +1695,11 @@ static int nfc_send_command(struct atmel_nand_host *host,
 			return -ETIMEDOUT;
 		}
 	}
+
+	nfc_prepare_interrupt(host, flag);
 	nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0);
 	nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs);
-	return nfc_wait_interrupt(host, NFC_SR_CMD_DONE);
+	return nfc_wait_interrupt(host, flag);
 }
 
 static int nfc_device_ready(struct mtd_info *mtd)
@@ -1822,10 +1865,6 @@ static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
 	nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr;
 	nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
 
-	if (dataen == NFCADDR_CMD_DATAEN)
-		if (nfc_wait_interrupt(host, NFC_SR_XFR_DONE))
-			dev_err(host->dev, "something wrong, No XFR_DONE interrupt comes.\n");
-
 	/*
 	 * Program and erase have their own busy handlers status, sequential
 	 * in, and deplete1 need no delay.
@@ -1850,6 +1889,7 @@ static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
 		}
 		/* fall through */
 	default:
+		nfc_prepare_interrupt(host, NFC_SR_RB_EDGE);
 		nfc_wait_interrupt(host, NFC_SR_RB_EDGE);
 	}
 }
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 1/3] mtd: atmel_nand: add NFC status error check
  2014-06-10  9:50 [PATCH v2 1/3] mtd: atmel_nand: add NFC status error check Josh Wu
  2014-06-10  9:50 ` [PATCH v2 2/3] mtd: atmel_nand: implement the nfc_device_ready() by checking the R/B bit Josh Wu
  2014-06-10  9:50 ` [PATCH v2 3/3] mtd: atmel_nand: NFC: support multiple interrupt handling Josh Wu
@ 2014-07-16 10:06 ` Josh Wu
  2014-07-16 10:16   ` Josh Wu
  2014-07-22  3:12 ` Brian Norris
  3 siblings, 1 reply; 6+ messages in thread
From: Josh Wu @ 2014-07-16 10:06 UTC (permalink / raw)
  To: linux-arm-kernel

On 6/10/2014 5:50 PM, Josh Wu wrote:
> Add a new function to read the NFC status. Meantime, this function will
> theck if there is any errors in NFC.
>
> Signed-off-by: Josh Wu <josh.wu@atmel.com>

Tested-by: Matthieu Crapet <Matthieu.Crapet@ingenico.com>

I add a Tested-by tag from Matthieu Crapet.
He didn't reply this email thread directly. His test-by tag is in 
email:http://lists.infradead.org/pipermail/linux-mtd/2014-June/054443.html

Best Regards,
Josh Wu

> ---
>   drivers/mtd/nand/atmel_nand.c     |   24 +++++++++++++++++++++++-
>   drivers/mtd/nand/atmel_nand_nfc.h |    4 ++++
>   2 files changed, 27 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/mtd/nand/atmel_nand.c b/drivers/mtd/nand/atmel_nand.c
> index ec97ccb..25d9921 100644
> --- a/drivers/mtd/nand/atmel_nand.c
> +++ b/drivers/mtd/nand/atmel_nand.c
> @@ -1572,6 +1572,25 @@ static int atmel_hw_nand_init_params(struct platform_device *pdev,
>   	return 0;
>   }
>   
> +static inline u32 nfc_read_status(struct atmel_nand_host *host)
> +{
> +	u32 err_flags = NFC_SR_DTOE | NFC_SR_UNDEF | NFC_SR_AWB | NFC_SR_ASE;
> +	u32 nfc_status = nfc_readl(host->nfc->hsmc_regs, SR);
> +
> +	if (unlikely(nfc_status & err_flags)) {
> +		if (nfc_status & NFC_SR_DTOE)
> +			dev_err(host->dev, "NFC: Waiting Nand R/B Timeout Error\n");
> +		else if (nfc_status & NFC_SR_UNDEF)
> +			dev_err(host->dev, "NFC: Access Undefined Area Error\n");
> +		else if (nfc_status & NFC_SR_AWB)
> +			dev_err(host->dev, "NFC: Access memory While NFC is busy\n");
> +		else if (nfc_status & NFC_SR_ASE)
> +			dev_err(host->dev, "NFC: Access memory Size Error\n");
> +	}
> +
> +	return nfc_status;
> +}
> +
>   /* SMC interrupt service routine */
>   static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
>   {
> @@ -1579,7 +1598,7 @@ static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
>   	u32 status, mask, pending;
>   	irqreturn_t ret = IRQ_HANDLED;
>   
> -	status = nfc_readl(host->nfc->hsmc_regs, SR);
> +	status = nfc_read_status(host);
>   	mask = nfc_readl(host->nfc->hsmc_regs, IMR);
>   	pending = status & mask;
>   
> @@ -2210,6 +2229,9 @@ static int atmel_nand_nfc_probe(struct platform_device *pdev)
>   		}
>   	}
>   
> +	nfc_writel(nfc->hsmc_regs, IDR, 0xffffffff);
> +	nfc_readl(nfc->hsmc_regs, SR);	/* clear the NFC_SR */
> +
>   	nfc->is_initialized = true;
>   	dev_info(&pdev->dev, "NFC is probed.\n");
>   	return 0;
> diff --git a/drivers/mtd/nand/atmel_nand_nfc.h b/drivers/mtd/nand/atmel_nand_nfc.h
> index 4efd117..85b8ca6 100644
> --- a/drivers/mtd/nand/atmel_nand_nfc.h
> +++ b/drivers/mtd/nand/atmel_nand_nfc.h
> @@ -37,6 +37,10 @@
>   #define ATMEL_HSMC_NFC_SR	0x08		/* NFC Status Register */
>   #define		NFC_SR_XFR_DONE		(1 << 16)
>   #define		NFC_SR_CMD_DONE		(1 << 17)
> +#define		NFC_SR_DTOE		(1 << 20)
> +#define		NFC_SR_UNDEF		(1 << 21)
> +#define		NFC_SR_AWB		(1 << 22)
> +#define		NFC_SR_ASE		(1 << 23)
>   #define		NFC_SR_RB_EDGE		(1 << 24)
>   
>   #define ATMEL_HSMC_NFC_IER	0x0c

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/3] mtd: atmel_nand: add NFC status error check
  2014-07-16 10:06 ` [PATCH v2 1/3] mtd: atmel_nand: add NFC status error check Josh Wu
@ 2014-07-16 10:16   ` Josh Wu
  0 siblings, 0 replies; 6+ messages in thread
From: Josh Wu @ 2014-07-16 10:16 UTC (permalink / raw)
  To: linux-arm-kernel

On 7/16/2014 6:06 PM, Josh Wu wrote:
> On 6/10/2014 5:50 PM, Josh Wu wrote:
>> Add a new function to read the NFC status. Meantime, this function will
>> theck if there is any errors in NFC.
>>
>> Signed-off-by: Josh Wu <josh.wu@atmel.com>
>
> Tested-by: Matthieu Crapet <Matthieu.Crapet@ingenico.com>
>

Sorry, I forgot to mention this Tested-by tag is for these patch series.

Best Regards,
Josh Wu

> I add a Tested-by tag from Matthieu Crapet.
> He didn't reply this email thread directly. His test-by tag is in 
> email:http://lists.infradead.org/pipermail/linux-mtd/2014-June/054443.html 
>
>
> Best Regards,
> Josh Wu
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/3] mtd: atmel_nand: add NFC status error check
  2014-06-10  9:50 [PATCH v2 1/3] mtd: atmel_nand: add NFC status error check Josh Wu
                   ` (2 preceding siblings ...)
  2014-07-16 10:06 ` [PATCH v2 1/3] mtd: atmel_nand: add NFC status error check Josh Wu
@ 2014-07-22  3:12 ` Brian Norris
  3 siblings, 0 replies; 6+ messages in thread
From: Brian Norris @ 2014-07-22  3:12 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Jun 10, 2014 at 05:50:09PM +0800, Josh Wu wrote:
> Add a new function to read the NFC status. Meantime, this function will
> theck if there is any errors in NFC.
> 
> Signed-off-by: Josh Wu <josh.wu@atmel.com>

Fixed up some of the wording and pushed to l2-mtd.git. Thanks!

Brian

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-07-22  3:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-10  9:50 [PATCH v2 1/3] mtd: atmel_nand: add NFC status error check Josh Wu
2014-06-10  9:50 ` [PATCH v2 2/3] mtd: atmel_nand: implement the nfc_device_ready() by checking the R/B bit Josh Wu
2014-06-10  9:50 ` [PATCH v2 3/3] mtd: atmel_nand: NFC: support multiple interrupt handling Josh Wu
2014-07-16 10:06 ` [PATCH v2 1/3] mtd: atmel_nand: add NFC status error check Josh Wu
2014-07-16 10:16   ` Josh Wu
2014-07-22  3:12 ` Brian Norris

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).