2017-09-29 10:41:02

by Masahiro Yamada

[permalink] [raw]
Subject: [PATCH v2 0/2] mtd: nand: wait for tWHR, and fix the setup_data_interface of Denali


1/2 : add NAND_WAIT_TWHR and nand_whr_delay().
You can set this new flag if you want nand_command(_lp)
to insert tWHR delay where needed.

2/2 : Fix Denali setup_data_interface.
Boris' suggestion in v1 was a good reminder that
made me realize tCCS was missing in the driver. Fix it now.


Changes in v2:
- Add nand_whr_delay() helper
Wait for tWHR only for drivers that explicitly set NAND_WAIT_TWHR flag
- newly added

Masahiro Yamada (2):
mtd: nand: wait for tWHR after NAND_CMD_STATUS / NAND_CMD_READID
mtd: nand: denali: fix setup_data_interface to meet tCCS delay

drivers/mtd/nand/denali.c | 10 ++++++++--
drivers/mtd/nand/nand_base.c | 21 +++++++++++++++++++--
include/linux/mtd/rawnand.h | 13 ++++++++-----
3 files changed, 35 insertions(+), 9 deletions(-)

--
2.7.4


2017-09-29 10:41:24

by Masahiro Yamada

[permalink] [raw]
Subject: [PATCH v2 2/2] mtd: nand: denali: fix setup_data_interface to meet tCCS delay

The WE_2_RE register specifies the number of clock cycles inserted
between the rising edge of #WE and the falling edge of #RE.

The current setup_data_interface implementation takes care of tWHR,
but tCCS is missing. Wait max(tCSS, tWHR) to meet the spec.

With setup_data_interface() is properly programmed, the Denali NAND
controller can observe the timing, so NAND_WAIT_{TCCS,TWHR} flag is
unneeded. Say this in the comment block.

Signed-off-by: Masahiro Yamada <[email protected]>
---

Changes in v2:
- newly added

drivers/mtd/nand/denali.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/mtd/nand/denali.c b/drivers/mtd/nand/denali.c
index 0b268ec..332c022 100644
--- a/drivers/mtd/nand/denali.c
+++ b/drivers/mtd/nand/denali.c
@@ -1004,8 +1004,14 @@ static int denali_setup_data_interface(struct mtd_info *mtd, int chipnr,
tmp |= FIELD_PREP(RE_2_RE__VALUE, re_2_re);
iowrite32(tmp, denali->reg + RE_2_RE);

- /* tWHR -> WE_2_RE */
- we_2_re = DIV_ROUND_UP(timings->tWHR_min, t_clk);
+ /*
+ * tCCS, tWHR -> WE_2_RE
+ *
+ * With WE_2_RE properly set, the Denali controller automatically takes
+ * care of tCCS, tWHR; the driver need not set NAND_WAIT_{TCCS,TWHR}.
+ */
+ we_2_re = DIV_ROUND_UP(max(timings->tCCS_min, timings->tWHR_min),
+ t_clk);
we_2_re = min_t(int, we_2_re, TWHR2_AND_WE_2_RE__WE_2_RE);

tmp = ioread32(denali->reg + TWHR2_AND_WE_2_RE);
--
2.7.4

2017-09-29 10:40:57

by Masahiro Yamada

[permalink] [raw]
Subject: [PATCH v2 1/2] mtd: nand: wait for tWHR after NAND_CMD_STATUS / NAND_CMD_READID

For commands such as Read Status, Read ID, etc. the controller needs
to wait for tWHR before it reads out the first data. If the controller
does not take care of it, the driver has to wait explicitly to make
sure to meet the spec.

Introduce NAND_WAIT_TWHR, which works like NAND_WAIT_TCCS. The
driver can set this flag to let the core to handle the tWHR delay.

Signed-off-by: Masahiro Yamada <[email protected]>
---

Changes in v2:
- Add nand_whr_delay() helper
Wait for tWHR only for drivers that explicitly set NAND_WAIT_TWHR flag

drivers/mtd/nand/nand_base.c | 21 +++++++++++++++++++--
include/linux/mtd/rawnand.h | 13 ++++++++-----
2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/drivers/mtd/nand/nand_base.c b/drivers/mtd/nand/nand_base.c
index b1cf32c..455085d 100644
--- a/drivers/mtd/nand/nand_base.c
+++ b/drivers/mtd/nand/nand_base.c
@@ -676,6 +676,17 @@ static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
} while (time_before(jiffies, timeo));
};

+static void nand_whr_delay(struct nand_chip *chip)
+{
+ if (!(chip->options & NAND_WAIT_TWHR))
+ return;
+
+ if (chip->data_interface && chip->data_interface->timings.sdr.tWHR_min)
+ ndelay(chip->data_interface->timings.sdr.tWHR_min / 1000);
+ else
+ ndelay(200);
+}
+
/**
* nand_command - [DEFAULT] Send command to NAND device
* @mtd: MTD device structure
@@ -742,9 +753,12 @@ static void nand_command(struct mtd_info *mtd, unsigned int command,
case NAND_CMD_ERASE1:
case NAND_CMD_ERASE2:
case NAND_CMD_SEQIN:
+ case NAND_CMD_SET_FEATURES:
+ return;
+
case NAND_CMD_STATUS:
case NAND_CMD_READID:
- case NAND_CMD_SET_FEATURES:
+ nand_whr_delay(chip);
return;

case NAND_CMD_RESET:
@@ -871,9 +885,12 @@ static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
case NAND_CMD_ERASE1:
case NAND_CMD_ERASE2:
case NAND_CMD_SEQIN:
+ case NAND_CMD_SET_FEATURES:
+ return;
+
case NAND_CMD_STATUS:
case NAND_CMD_READID:
- case NAND_CMD_SET_FEATURES:
+ nand_whr_delay(chip);
return;

case NAND_CMD_RNDIN:
diff --git a/include/linux/mtd/rawnand.h b/include/linux/mtd/rawnand.h
index 749bb08..bb0165b 100644
--- a/include/linux/mtd/rawnand.h
+++ b/include/linux/mtd/rawnand.h
@@ -213,13 +213,16 @@ enum nand_ecc_algo {

/*
* In case your controller is implementing ->cmd_ctrl() and is relying on the
- * default ->cmdfunc() implementation, you may want to let the core handle the
- * tCCS delay which is required when a column change (RNDIN or RNDOUT) is
- * requested.
- * If your controller already takes care of this delay, you don't need to set
- * this flag.
+ * default ->cmdfunc() implementation, you may want to let the core handle
+ * some delays to meet the timing specification.
+ * If your controller already takes care of these delays, you don't need to set
+ * the following flags.
*/
+
+/* tCCS for a column change (RNDIN or RNDOUT) */
#define NAND_WAIT_TCCS 0x00200000
+/* tWHR for STATUS, READID, etc. */
+#define NAND_WAIT_TWHR 0x00400000

/* Options set by nand scan */
/* Nand scan has allocated controller struct */
--
2.7.4

2017-09-29 12:26:38

by Boris Brezillon

[permalink] [raw]
Subject: Re: [PATCH v2 0/2] mtd: nand: wait for tWHR, and fix the setup_data_interface of Denali

On Fri, 29 Sep 2017 19:38:38 +0900
Masahiro Yamada <[email protected]> wrote:

> 1/2 : add NAND_WAIT_TWHR and nand_whr_delay().
> You can set this new flag if you want nand_command(_lp)
> to insert tWHR delay where needed.
>
> 2/2 : Fix Denali setup_data_interface.
> Boris' suggestion in v1 was a good reminder that
> made me realize tCCS was missing in the driver. Fix it now.
>
>
> Changes in v2:
> - Add nand_whr_delay() helper
> Wait for tWHR only for drivers that explicitly set NAND_WAIT_TWHR flag
> - newly added
>
> Masahiro Yamada (2):
> mtd: nand: wait for tWHR after NAND_CMD_STATUS / NAND_CMD_READID

Hm, I thought you were introducing this to then use it in the denali
driver. Sorry, but I don't want to apply something that nobody needs.
If someone ever complains about a missing delay I'll point him to your
patch, but until then I'll keep the core unchanged.

> mtd: nand: denali: fix setup_data_interface to meet tCCS delay

This one is valid. I'll queue it to nand/next soon.

Thanks,

Boris

>
> drivers/mtd/nand/denali.c | 10 ++++++++--
> drivers/mtd/nand/nand_base.c | 21 +++++++++++++++++++--
> include/linux/mtd/rawnand.h | 13 ++++++++-----
> 3 files changed, 35 insertions(+), 9 deletions(-)
>

2017-09-29 14:07:31

by Masahiro Yamada

[permalink] [raw]
Subject: Re: [PATCH v2 0/2] mtd: nand: wait for tWHR, and fix the setup_data_interface of Denali

2017-09-29 21:26 GMT+09:00 Boris Brezillon <[email protected]>:
> On Fri, 29 Sep 2017 19:38:38 +0900
> Masahiro Yamada <[email protected]> wrote:
>
>> 1/2 : add NAND_WAIT_TWHR and nand_whr_delay().
>> You can set this new flag if you want nand_command(_lp)
>> to insert tWHR delay where needed.
>>
>> 2/2 : Fix Denali setup_data_interface.
>> Boris' suggestion in v1 was a good reminder that
>> made me realize tCCS was missing in the driver. Fix it now.
>>
>>
>> Changes in v2:
>> - Add nand_whr_delay() helper
>> Wait for tWHR only for drivers that explicitly set NAND_WAIT_TWHR flag
>> - newly added
>>
>> Masahiro Yamada (2):
>> mtd: nand: wait for tWHR after NAND_CMD_STATUS / NAND_CMD_READID
>
> Hm, I thought you were introducing this to then use it in the denali
> driver. Sorry, but I don't want to apply something that nobody needs.
> If someone ever complains about a missing delay I'll point him to your
> patch, but until then I'll keep the core unchanged.


At first, I thought this was necessary for me,
but I realized it was my misunderstanding.


Please let me explain one more.

See commit 3158fa0e739615769cc047d2428f30f4c3b6640e.

Prior to that commit, READID waited for #R/B transition,
it was wrong, so I fixed it.

However, it dropped the delay completely.
If somebody was implicitly relying on the delay of chip->dev_ready,
the first byte might be read out before the valid data
is available.

This was motivation of v1, where inserted ndelay(200)
unconditionally.





>> mtd: nand: denali: fix setup_data_interface to meet tCCS delay
>
> This one is valid. I'll queue it to nand/next soon.

If you drop 1/2, please let me do v3.

V2 mentions NAND_WAIT_TWHR, this is strange.



--
Best Regards
Masahiro Yamada

2017-09-29 14:29:39

by Boris Brezillon

[permalink] [raw]
Subject: Re: [PATCH v2 0/2] mtd: nand: wait for tWHR, and fix the setup_data_interface of Denali

On Fri, 29 Sep 2017 23:06:42 +0900
Masahiro Yamada <[email protected]> wrote:

> 2017-09-29 21:26 GMT+09:00 Boris Brezillon <[email protected]>:
> > On Fri, 29 Sep 2017 19:38:38 +0900
> > Masahiro Yamada <[email protected]> wrote:
> >
> >> 1/2 : add NAND_WAIT_TWHR and nand_whr_delay().
> >> You can set this new flag if you want nand_command(_lp)
> >> to insert tWHR delay where needed.
> >>
> >> 2/2 : Fix Denali setup_data_interface.
> >> Boris' suggestion in v1 was a good reminder that
> >> made me realize tCCS was missing in the driver. Fix it now.
> >>
> >>
> >> Changes in v2:
> >> - Add nand_whr_delay() helper
> >> Wait for tWHR only for drivers that explicitly set NAND_WAIT_TWHR flag
> >> - newly added
> >>
> >> Masahiro Yamada (2):
> >> mtd: nand: wait for tWHR after NAND_CMD_STATUS / NAND_CMD_READID
> >
> > Hm, I thought you were introducing this to then use it in the denali
> > driver. Sorry, but I don't want to apply something that nobody needs.
> > If someone ever complains about a missing delay I'll point him to your
> > patch, but until then I'll keep the core unchanged.
>
>
> At first, I thought this was necessary for me,
> but I realized it was my misunderstanding.
>
>
> Please let me explain one more.
>
> See commit 3158fa0e739615769cc047d2428f30f4c3b6640e.
>
> Prior to that commit, READID waited for #R/B transition,
> it was wrong, so I fixed it.
>
> However, it dropped the delay completely.
> If somebody was implicitly relying on the delay of chip->dev_ready,
> the first byte might be read out before the valid data
> is available.
>
> This was motivation of v1, where inserted ndelay(200)
> unconditionally.

Okay. Anyway, this extra delay is activated with an opt-in flag (I
know, I'm the one who asked that), and noone set this flag in
chip->options, so, if there's a bug, it's still here even after
applying "mtd: nand: wait for tWHR after NAND_CMD_STATUS /
NAND_CMD_READID".

Honestly, I think all advanced controllers have the tWHR/tRHW timings
enforced in the HW logic (configurable through a reg). This leaves
basic controllers like the nand-gpio one, and even for these ones, the
delay between the chip->cmd_ctrl(ADDR) and chip->read_buf() calls is
probably long enough to hide the problem.

Note that I'm absolutely not against this patch, it's just that I'd
like to have a real user before merging this logic.

>
>
>
>
>
> >> mtd: nand: denali: fix setup_data_interface to meet tCCS delay
> >
> > This one is valid. I'll queue it to nand/next soon.
>
> If you drop 1/2, please let me do v3.
>
> V2 mentions NAND_WAIT_TWHR, this is strange.
>

Sure.

>
>

2017-09-29 14:34:17

by Masahiro Yamada

[permalink] [raw]
Subject: Re: [PATCH v2 0/2] mtd: nand: wait for tWHR, and fix the setup_data_interface of Denali

(+CC Marc Gonzalez)

2017-09-29 21:26 GMT+09:00 Boris Brezillon <[email protected]>:
> On Fri, 29 Sep 2017 19:38:38 +0900
> Masahiro Yamada <[email protected]> wrote:
>
>> 1/2 : add NAND_WAIT_TWHR and nand_whr_delay().
>> You can set this new flag if you want nand_command(_lp)
>> to insert tWHR delay where needed.
>>
>> 2/2 : Fix Denali setup_data_interface.
>> Boris' suggestion in v1 was a good reminder that
>> made me realize tCCS was missing in the driver. Fix it now.
>>
>>
>> Changes in v2:
>> - Add nand_whr_delay() helper
>> Wait for tWHR only for drivers that explicitly set NAND_WAIT_TWHR flag
>> - newly added
>>
>> Masahiro Yamada (2):
>> mtd: nand: wait for tWHR after NAND_CMD_STATUS / NAND_CMD_READID
>
> Hm, I thought you were introducing this to then use it in the denali
> driver. Sorry, but I don't want to apply something that nobody needs.
> If someone ever complains about a missing delay I'll point him to your
> patch, but until then I'll keep the core unchanged.

Perhaps, Marc Gonzalez is the person.


tango_nand.c is the only driver that sets NAND_WAIT_TCCS.

Now, there is completely no delay when reading out the ID.


One safe change might be apply this patch,
then set NAND_WAIT_TWHR to tango_nand.c


I am guessing NAND_WAIT_TCCS was added for it.
Theoretically, I do not see logical difference between tCCS and tWHR.

I am CCing Marc Gonzalez, the author of tango_nand.c




commit 6ea40a3ba93e1b14ffb349e276f9dfefc4334b99
Author: Boris Brezillon <[email protected]>
Date: Sat Oct 1 10:24:03 2016 +0200

mtd: nand: Wait tCCS after a column change

Drivers implementing ->cmd_ctrl() and relying on the default ->cmdfunc()
implementation usually don't wait tCCS when a column change (RNDIN or
RNDOUT) is requested.
Add an option flag to ask the core to do so (note that we keep this as
an opt-in to avoid breaking existing implementations), and make use of
the ->data_interface information is available (otherwise, wait 500ns).

Signed-off-by: Boris Brezillon <[email protected]>
Tested-by: Marc Gonzalez <[email protected]>


--
Best Regards
Masahiro Yamada