public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] mtd: rawnand: denali: add more delays before latching incoming data
@ 2020-03-17  7:18 Masahiro Yamada
  2020-04-22 15:29 ` Marek Vasut
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Masahiro Yamada @ 2020-03-17  7:18 UTC (permalink / raw)
  To: linux-mtd
  Cc: Marek Vasut, Miquel Raynal, Masahiro Yamada, Richard Weinberger,
	Vignesh Raghavendra, linux-kernel

The Denali IP have several registers to specify how many clock cycles
should be waited between falling/rising signals. You can improve the
NAND access performance by programming these registers with optimized
values.

Because struct nand_sdr_timings represents the device requirement
in pico seconds, denali_setup_data_interface() computes the register
values by dividing the device timings with the clock period.

Marek Vasut reported this driver in the latest kernel does not work
on his SOCFPGA board. (The on-board NAND chip is mode 5)

The suspicious parameter is acc_clks, so this commit relaxes it.

The Denali NAND Flash Memory Controller User's Guide describes this
register as follows:

  acc_clks
    signifies the number of bus interface clk_x clock cycles,
    controller should wait from read enable going low to sending
    out a strobe of clk_x for capturing of incoming data.

Currently, acc_clks is calculated only based on tREA, the delay on the
chip side. This does not include additional delays that come from the
data path on the PCB and in the SoC, load capacity of the pins, etc.

This relatively becomes a big factor on faster timing modes like mode 5.

Before supporting the ->setup_data_interface() hook (e.g. Linux 4.12),
the Denali driver hacks acc_clks in a couple of ways [1] [2] to support
the timing mode 5.

We would not go back to the hard-coded acc_clks, but we need to include
this factor into the delay somehow. Let's say the amount of the additional
delay is 10000 pico sec.

In the new calculation, acc_clks is determined by timings->tREA_max +
data_setup_on_host.

Also, prolong the RE# low period to make sure the data hold is met.

Finally, re-center the data latch timing for extra safety.

[1] https://github.com/torvalds/linux/blob/v4.12/drivers/mtd/nand/denali.c#L276
[2] https://github.com/torvalds/linux/blob/v4.12/drivers/mtd/nand/denali.c#L282

Reported-by: Marek Vasut <marex@denx.de>
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
---

Changes in v2:
  - Use 'unsigned int' instead of 'u32' for data_setup_on_host
  - Fix 'line over 80 characters' warning from checkpatch.pl

 drivers/mtd/nand/raw/denali.c | 45 ++++++++++++++++++++++++++---------
 1 file changed, 34 insertions(+), 11 deletions(-)

diff --git a/drivers/mtd/nand/raw/denali.c b/drivers/mtd/nand/raw/denali.c
index 6a6c919b2569..2fcd2baf6e35 100644
--- a/drivers/mtd/nand/raw/denali.c
+++ b/drivers/mtd/nand/raw/denali.c
@@ -764,6 +764,7 @@ static int denali_write_page(struct nand_chip *chip, const u8 *buf,
 static int denali_setup_data_interface(struct nand_chip *chip, int chipnr,
 				       const struct nand_data_interface *conf)
 {
+	static const unsigned int data_setup_on_host = 10000;
 	struct denali_controller *denali = to_denali_controller(chip);
 	struct denali_chip_sel *sel;
 	const struct nand_sdr_timings *timings;
@@ -796,15 +797,6 @@ static int denali_setup_data_interface(struct nand_chip *chip, int chipnr,
 
 	sel = &to_denali_chip(chip)->sels[chipnr];
 
-	/* tREA -> ACC_CLKS */
-	acc_clks = DIV_ROUND_UP(timings->tREA_max, t_x);
-	acc_clks = min_t(int, acc_clks, ACC_CLKS__VALUE);
-
-	tmp = ioread32(denali->reg + ACC_CLKS);
-	tmp &= ~ACC_CLKS__VALUE;
-	tmp |= FIELD_PREP(ACC_CLKS__VALUE, acc_clks);
-	sel->acc_clks = tmp;
-
 	/* tRWH -> RE_2_WE */
 	re_2_we = DIV_ROUND_UP(timings->tRHW_min, t_x);
 	re_2_we = min_t(int, re_2_we, RE_2_WE__VALUE);
@@ -862,14 +854,45 @@ static int denali_setup_data_interface(struct nand_chip *chip, int chipnr,
 	tmp |= FIELD_PREP(RDWR_EN_HI_CNT__VALUE, rdwr_en_hi);
 	sel->rdwr_en_hi_cnt = tmp;
 
-	/* tRP, tWP -> RDWR_EN_LO_CNT */
+	/*
+	 * tREA -> ACC_CLKS
+	 * tRP, tWP, tRHOH, tRC, tWC -> RDWR_EN_LO_CNT
+	 */
+
+	/*
+	 * Determine the minimum of acc_clks to meet the setup timing when
+	 * capturing the incoming data.
+	 *
+	 * The delay on the chip side is well-defined as tREA, but we need to
+	 * take additional delay into account. This includes a certain degree
+	 * of unknowledge, such as signal propagation delays on the PCB and
+	 * in the SoC, load capacity of the I/O pins, etc.
+	 */
+	acc_clks = DIV_ROUND_UP(timings->tREA_max + data_setup_on_host, t_x);
+
+	/* Determine the minimum of rdwr_en_lo_cnt from RE#/WE# pulse width */
 	rdwr_en_lo = DIV_ROUND_UP(max(timings->tRP_min, timings->tWP_min), t_x);
+
+	/* Extend rdwr_en_lo to meet the data hold timing */
+	rdwr_en_lo = max_t(int, rdwr_en_lo,
+			   acc_clks - timings->tRHOH_min / t_x);
+
+	/* Extend rdwr_en_lo to meet the requirement for RE#/WE# cycle time */
 	rdwr_en_lo_hi = DIV_ROUND_UP(max(timings->tRC_min, timings->tWC_min),
 				     t_x);
-	rdwr_en_lo_hi = max_t(int, rdwr_en_lo_hi, mult_x);
 	rdwr_en_lo = max(rdwr_en_lo, rdwr_en_lo_hi - rdwr_en_hi);
 	rdwr_en_lo = min_t(int, rdwr_en_lo, RDWR_EN_LO_CNT__VALUE);
 
+	/* Center the data latch timing for extra safety */
+	acc_clks = (acc_clks + rdwr_en_lo +
+		    DIV_ROUND_UP(timings->tRHOH_min, t_x)) / 2;
+	acc_clks = min_t(int, acc_clks, ACC_CLKS__VALUE);
+
+	tmp = ioread32(denali->reg + ACC_CLKS);
+	tmp &= ~ACC_CLKS__VALUE;
+	tmp |= FIELD_PREP(ACC_CLKS__VALUE, acc_clks);
+	sel->acc_clks = tmp;
+
 	tmp = ioread32(denali->reg + RDWR_EN_LO_CNT);
 	tmp &= ~RDWR_EN_LO_CNT__VALUE;
 	tmp |= FIELD_PREP(RDWR_EN_LO_CNT__VALUE, rdwr_en_lo);
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread
* Re: [PATCH v2] mtd: rawnand: denali: add more delays before latching incoming data
@ 2026-03-04  5:53 Helen
  0 siblings, 0 replies; 9+ messages in thread
From: Helen @ 2026-03-04  5:53 UTC (permalink / raw)
  To: masahiroy
  Cc: linux-mtd, miquel.raynal, richard, vigneshr, marex, linux-kernel

On Mon, Jul 05, 2021 at 14:48 PM Ralph Siemsen <ralph.siemsen@linaro.org> wrote:
>
>Yes, it does seem to work with this value increased to 10001.
>
>Looking more closely, I compared timing parameters in struct 
>denali_chip_sel, for the kernel 5.4 (which works), for 5.10 (which 
>fails), and also for 5.10 with data_setup_host=10001 (which works).

>                 5.4     5.10    5.10+10001
>acc_clks        2       3       4
>rdwr_en_lo_cnt  3       2       3
>cs_setup_cnt    1       0       0
>
>So it seems that on my hardware, rdwr_en_lo_cnt must be >= 3.

I'm using a Cyclone V SoC system, the NAND device is Micron MT29F8G08ABACAWP,
NAND driver fails with "timeout while waiting for irq 0x4", which found in 2021:
https://lore.kernel.org/linux-mtd/20210705144823.GA1459782@maple.netwinder.org/

It worked fine in Mode5 in 5.10 kernels without the commit below:
https://github.com/torvalds/linux/commit/5756f2e8dad46eba6e2d3e530243b8eff4dd5a42
But after upgrading to 6.12 kernel, include this commit with data_setup_on_host=10001,
the NAND driver still fails with "timeout while waiting for irq 0x4".

Add some print to timing calculations, it seems the deletion of the line in the commit:
-	rdwr_en_lo_hi = max_t(int, rdwr_en_lo_hi, mult_x);
cause rdwr_en_lo_hi changed from 4 to 1 and indirectly changed rdwr_en_lo from 3 to 2.
It seems on my hardware rdwr_en_lo_cnt must be >= 3, nand works if add this line back.
I'd like to know why delete it?

Linux 5.10 without commit:
rdwr_en_hi = DIV_ROUD_UP(max(timings->tREH_min, timings->tWH_min), t_x);
       0x1 = (7000 + 20000 - 1)/20000 = 
acc_clks = DIV_ROUND_UP(timings->tREA_max, t_x);
     0x1 = DIV_ROUND_UP(16000, 20000);
rdwr_en_lo = DIV_ROUND_UP(max(timings->tRP_min, timings->tWP_min), t_x);
       0x1 = DIV_ROUND_UP(max(10000, 10000), 20000);
rdwr_en_lo_hi = DIV_ROUND_UP(max(timings->tRC_min, timings->tWC_min), t_x);
          0x1 = DIV_ROUND_UP(max(2000, 2000), 20000);
rdwr_en_lo_hi = max_t(int, rdwr_en_lo_hi, mult_x);
          0x4 = max_t(int, 0x1, 0x4);
rdwr_en_lo = max(rdwr_en_lo, rdwr_en_lo_hi - rdwr_en_hi);
       0x3 = max(0x1, 0x4 - 0x1)
rdwr_en_lo = min_t(int, rdwr_en_lo, RDWR_EN_LO_CNT__VALUE);
       0x3 = min_t(int, 0x3, mask);

Linux 6.12 with commit + 10001:
static const unsigned int data_setup_on_host = 10001;
rdwr_en_hi = DIV_ROUD_UP(max(timings->tREH_min, timings->tWH_min), t_x);
       0x1 = (7000 + 20000 - 1)/20000
acc_clks = DIV_ROUND_UP(timings->tREA_max + data_setup_on_host, t_x);
     0x2 = (16000 + 10001 + 20000 - 1)/20000
rdwr_en_lo = DIV_ROUND_UP(max(timings->tRP_min, timings->tWP_min), t_x);
       0x1 = (10000 + 20000 -1)/20000
rdwr_en_lo = max_t(int, rdwr_en_lo, acc_clks - timings->tRHOH_min / t_x);
       0x2 = max_t(int, 0x1, 0x2 - 15000 / 20000);
rdwr_en_lo_hi = DIV_ROUND_UP(max(timings->tRC_min, timings->tWC_min), t_x);
          0x1 = (20000 + 20000 - 1)/20000
rdwr_en_lo = max(rdwr_en_lo, rdwr_en_lo_hi - rdwr_en_hi);
       0x2 = max(0x2, 0x2 - 0x1);
rdwr_en_lo = min_t(int, rdwr_en_lo, RDWR_EN_LO_CNT__VALUE);
       0x2 = min_t(int, 0x2, mask);

BR,
Helen

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

end of thread, other threads:[~2026-03-04  5:53 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-03-17  7:18 [PATCH v2] mtd: rawnand: denali: add more delays before latching incoming data Masahiro Yamada
2020-04-22 15:29 ` Marek Vasut
2020-04-22 15:36   ` Miquel Raynal
2020-04-22 15:37     ` Marek Vasut
2020-05-10 20:06 ` Miquel Raynal
2021-06-30 18:47 ` Ralph Siemsen
2021-07-04 16:00   ` Masahiro Yamada
2021-07-05 14:48     ` Ralph Siemsen
  -- strict thread matches above, loose matches on Subject: below --
2026-03-04  5:53 Helen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox