[PATCH v2 1/2] hwrng: histb - fix read timeout

From: David Yang

Date: Wed Nov 04 2026 - 06:42:52 EST


The old figures were per-word averages, but the device produces 4
words in one go, with each unit of post-process depth taking ~200us.

The old 30ms timeout was chosen on that per-word basis. At a depth
of 144, a 4-word batch may take as long as ~30ms, causing random
read timeouts.

Also, a restart after rewriting the control word may take ~300ms, so
use a 500ms timeout instead to cover all of the above.

Fixes: a2216e187471 ("crypto: hisilicon/trng - add support for HiSTB TRNG")
Signed-off-by: David Yang <mmyangfl@xxxxxxxxx>
---
drivers/char/hw_random/histb-rng.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/drivers/char/hw_random/histb-rng.c b/drivers/char/hw_random/histb-rng.c
index 445b80beed62..d435d4ab9aa7 100644
--- a/drivers/char/hw_random/histb-rng.c
+++ b/drivers/char/hw_random/histb-rng.c
@@ -26,16 +26,21 @@ struct histb_rng_priv {
};

/*
- * Observed:
- * depth = 1 -> ~1ms
- * depth = 255 -> ~16ms
+ * Observed: the FIFO fills with 4 words at a time, roughly every 200us per
+ * unit of post-process depth, i.e. one batch per:
+ * depth = 144 -> ~30ms
+ * depth = 255 -> ~52ms
+ * Rewriting the control word delays the first batch by up to 300ms.
*/
+#define BATCH_TIMEOUT_US (500 * USEC_PER_MSEC)
+
static int histb_rng_wait(void __iomem *base)
{
u32 val;

return readl_relaxed_poll_timeout(base + RNG_STAT, val,
- val & DATA_COUNT, 1000, 30 * 1000);
+ val & DATA_COUNT, 1000,
+ BATCH_TIMEOUT_US);
}

static void histb_rng_init(void __iomem *base, unsigned int depth)
--
2.53.0