1 From 8eff8eb83fc0ae8b5f76220e2bb8644d836e99ff Mon Sep 17 00:00:00 2001
2 From: Nicolas Frattaroli <nicolas.frattaroli@collabora.com>
3 Date: Tue, 4 Feb 2025 16:35:50 +0100
4 Subject: [PATCH] hwrng: rockchip - add support for rk3588's standalone TRNG
6 The RK3588 SoC includes several TRNGs, one part of the Crypto IP block,
7 and the other one (referred to as "trngv1") as a standalone new IP.
9 Add support for this new standalone TRNG to the driver by both
10 generalising it to support multiple different rockchip RNGs and then
11 implementing the required functionality for the new hardware.
13 This work was partly based on the downstream vendor driver by Rockchip's
14 Lin Jinhan, which is why they are listed as a Co-author.
16 While the hardware does support notifying the CPU with an IRQ when the
17 random data is ready, I've discovered while implementing the code to use
18 this interrupt that this results in significantly slower throughput of
19 the TRNG even when under heavy CPU load. I assume this is because with
20 only 32 bytes of data per invocation, the overhead of reinitialising a
21 completion, enabling the interrupt, sleeping and then triggering the
22 completion in the IRQ handler is way more expensive than busylooping.
24 Speaking of busylooping, the poll interval for reading the ISTAT is an
25 atomic read with a delay of 0. In my testing, I've found that this gives
26 us the largest throughput, and it appears the random data is ready
27 pretty much the moment we begin polling, as increasing the poll delay
28 leads to a drop in throughput significant enough to not just be due to
29 the poll interval missing the ideal timing by a microsecond or two.
31 According to downstream, the IP should take 1024 clock cycles to
32 generate 56 bits of random data, which at 150MHz should work out to
33 6.8us. I did not test whether the data really does take 256/56*6.8us
34 to arrive, though changing the readl to a __raw_readl makes no
35 difference in throughput, and this data does pass the rngtest FIPS
36 checks, so I'm not entirely sure what's going on but I presume it's got
37 something to do with the AHB bus speed and the memory barriers that
38 mainline's readl/writel functions insert.
40 The only other current SoC that uses this new IP is the Rockchip RV1106,
41 but that SoC does not have mainline support as of the time of writing,
42 so we make no effort to declare it as supported for now.
44 Co-developed-by: Lin Jinhan <troy.lin@rock-chips.com>
45 Signed-off-by: Lin Jinhan <troy.lin@rock-chips.com>
46 Signed-off-by: Nicolas Frattaroli <nicolas.frattaroli@collabora.com>
47 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
49 drivers/char/hw_random/Kconfig | 3 +-
50 drivers/char/hw_random/rockchip-rng.c | 234 +++++++++++++++++++++++---
51 2 files changed, 216 insertions(+), 21 deletions(-)
53 --- a/drivers/char/hw_random/Kconfig
54 +++ b/drivers/char/hw_random/Kconfig
55 @@ -580,7 +580,8 @@ config HW_RANDOM_ROCKCHIP
58 This driver provides kernel-side support for the True Random Number
59 - Generator hardware found on some Rockchip SoC like RK3566 or RK3568.
60 + Generator hardware found on some Rockchip SoCs like RK3566, RK3568
63 To compile this driver as a module, choose M here: the
64 module will be called rockchip-rng.
65 --- a/drivers/char/hw_random/rockchip-rng.c
66 +++ b/drivers/char/hw_random/rockchip-rng.c
68 // SPDX-License-Identifier: GPL-2.0
70 - * rockchip-rng.c True Random Number Generator driver for Rockchip RK3568 SoC
71 + * rockchip-rng.c True Random Number Generator driver for Rockchip SoCs
73 * Copyright (c) 2018, Fuzhou Rockchip Electronics Co., Ltd.
74 * Copyright (c) 2022, Aurelien Jarno
75 + * Copyright (c) 2025, Collabora Ltd.
77 * Lin Jinhan <troy.lin@rock-chips.com>
78 * Aurelien Jarno <aurelien@aurel32.net>
79 + * Nicolas Frattaroli <nicolas.frattaroli@collabora.com>
81 #include <linux/clk.h>
82 #include <linux/hw_random.h>
85 #define RK_RNG_SAMPLE_CNT 1000
87 +/* after how many bytes of output TRNGv1 implementations should be reseeded */
88 +#define RK_TRNG_V1_AUTO_RESEED_CNT 16000
90 /* TRNG registers from RK3568 TRM-Part2, section 5.4.1 */
91 #define TRNG_RST_CTL 0x0004
92 #define TRNG_RNG_CTL 0x0400
94 #define TRNG_RNG_SAMPLE_CNT 0x0404
95 #define TRNG_RNG_DOUT 0x0410
98 + * TRNG V1 register definitions
99 + * The TRNG V1 IP is a stand-alone TRNG implementation (not part of a crypto IP)
100 + * and can be found in the Rockchip RK3588 SoC
102 +#define TRNG_V1_CTRL 0x0000
103 +#define TRNG_V1_CTRL_NOP 0x00
104 +#define TRNG_V1_CTRL_RAND 0x01
105 +#define TRNG_V1_CTRL_SEED 0x02
107 +#define TRNG_V1_STAT 0x0004
108 +#define TRNG_V1_STAT_SEEDED BIT(9)
109 +#define TRNG_V1_STAT_GENERATING BIT(30)
110 +#define TRNG_V1_STAT_RESEEDING BIT(31)
112 +#define TRNG_V1_MODE 0x0008
113 +#define TRNG_V1_MODE_128_BIT (0x00 << 3)
114 +#define TRNG_V1_MODE_256_BIT (0x01 << 3)
116 +/* Interrupt Enable register; unused because polling is faster */
117 +#define TRNG_V1_IE 0x0010
118 +#define TRNG_V1_IE_GLBL_EN BIT(31)
119 +#define TRNG_V1_IE_SEED_DONE_EN BIT(1)
120 +#define TRNG_V1_IE_RAND_RDY_EN BIT(0)
122 +#define TRNG_V1_ISTAT 0x0014
123 +#define TRNG_V1_ISTAT_RAND_RDY BIT(0)
126 +#define TRNG_V1_RAND0 0x0020
127 +#define TRNG_V1_RAND7 0x003C
129 +/* Auto Reseed Register */
130 +#define TRNG_V1_AUTO_RQSTS 0x0060
132 +#define TRNG_V1_VERSION 0x00F0
133 +#define TRNG_v1_VERSION_CODE 0x46bc
134 +/* end of TRNG_V1 register definitions */
136 +/* Before removing this assert, give rk3588_rng_read an upper bound of 32 */
137 +static_assert(RK_RNG_MAX_BYTE <= (TRNG_V1_RAND7 + 4 - TRNG_V1_RAND0),
138 + "You raised RK_RNG_MAX_BYTE and broke rk3588-rng, congrats.");
144 struct clk_bulk_data *clk_bulks;
145 + const struct rk_rng_soc_data *soc_data;
149 +struct rk_rng_soc_data {
150 + int (*rk_rng_init)(struct hwrng *rng);
151 + int (*rk_rng_read)(struct hwrng *rng, void *buf, size_t max, bool wait);
152 + void (*rk_rng_cleanup)(struct hwrng *rng);
153 + unsigned short quality;
154 + bool reset_optional;
157 /* The mask in the upper 16 bits determines the bits that are updated */
158 static void rk_rng_write_ctl(struct rk_rng *rng, u32 val, u32 mask)
160 writel((mask << 16) | val, rng->base + TRNG_RNG_CTL);
163 -static int rk_rng_init(struct hwrng *rng)
164 +static inline void rk_rng_writel(struct rk_rng *rng, u32 val, u32 offset)
166 - struct rk_rng *rk_rng = container_of(rng, struct rk_rng, rng);
168 + writel(val, rng->base + offset);
171 +static inline u32 rk_rng_readl(struct rk_rng *rng, u32 offset)
173 + return readl(rng->base + offset);
176 +static int rk_rng_enable_clks(struct rk_rng *rk_rng)
180 ret = clk_bulk_prepare_enable(rk_rng->clk_num, rk_rng->clk_bulks);
182 @@ -75,6 +140,18 @@ static int rk_rng_init(struct hwrng *rng
189 +static int rk3568_rng_init(struct hwrng *rng)
191 + struct rk_rng *rk_rng = container_of(rng, struct rk_rng, rng);
194 + ret = rk_rng_enable_clks(rk_rng);
198 /* set the sample period */
199 writel(RK_RNG_SAMPLE_CNT, rk_rng->base + TRNG_RNG_SAMPLE_CNT);
201 @@ -87,7 +164,7 @@ static int rk_rng_init(struct hwrng *rng
205 -static void rk_rng_cleanup(struct hwrng *rng)
206 +static void rk3568_rng_cleanup(struct hwrng *rng)
208 struct rk_rng *rk_rng = container_of(rng, struct rk_rng, rng);
210 @@ -98,7 +175,7 @@ static void rk_rng_cleanup(struct hwrng
211 clk_bulk_disable_unprepare(rk_rng->clk_num, rk_rng->clk_bulks);
214 -static int rk_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
215 +static int rk3568_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
217 struct rk_rng *rk_rng = container_of(rng, struct rk_rng, rng);
218 size_t to_read = min_t(size_t, max, RK_RNG_MAX_BYTE);
219 @@ -128,6 +205,114 @@ out:
220 return (ret < 0) ? ret : to_read;
223 +static int rk3588_rng_init(struct hwrng *rng)
225 + struct rk_rng *rk_rng = container_of(rng, struct rk_rng, rng);
226 + u32 version, status, mask, istat;
229 + ret = rk_rng_enable_clks(rk_rng);
233 + version = rk_rng_readl(rk_rng, TRNG_V1_VERSION);
234 + if (version != TRNG_v1_VERSION_CODE) {
235 + dev_err(rk_rng->dev,
236 + "wrong trng version, expected = %08x, actual = %08x\n",
237 + TRNG_V1_VERSION, version);
239 + goto err_disable_clk;
242 + mask = TRNG_V1_STAT_SEEDED | TRNG_V1_STAT_GENERATING |
243 + TRNG_V1_STAT_RESEEDING;
244 + if (readl_poll_timeout(rk_rng->base + TRNG_V1_STAT, status,
245 + (status & mask) == TRNG_V1_STAT_SEEDED,
246 + RK_RNG_POLL_PERIOD_US, RK_RNG_POLL_TIMEOUT_US) < 0) {
247 + dev_err(rk_rng->dev, "timed out waiting for hwrng to reseed\n");
249 + goto err_disable_clk;
253 + * clear ISTAT flag, downstream advises to do this to avoid
254 + * auto-reseeding "on power on"
256 + istat = rk_rng_readl(rk_rng, TRNG_V1_ISTAT);
257 + rk_rng_writel(rk_rng, istat, TRNG_V1_ISTAT);
259 + /* auto reseed after RK_TRNG_V1_AUTO_RESEED_CNT bytes */
260 + rk_rng_writel(rk_rng, RK_TRNG_V1_AUTO_RESEED_CNT / 16, TRNG_V1_AUTO_RQSTS);
264 + clk_bulk_disable_unprepare(rk_rng->clk_num, rk_rng->clk_bulks);
268 +static void rk3588_rng_cleanup(struct hwrng *rng)
270 + struct rk_rng *rk_rng = container_of(rng, struct rk_rng, rng);
272 + clk_bulk_disable_unprepare(rk_rng->clk_num, rk_rng->clk_bulks);
275 +static int rk3588_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
277 + struct rk_rng *rk_rng = container_of(rng, struct rk_rng, rng);
278 + size_t to_read = min_t(size_t, max, RK_RNG_MAX_BYTE);
282 + ret = pm_runtime_resume_and_get(rk_rng->dev);
286 + /* Clear ISTAT, even without interrupts enabled, this will be updated */
287 + reg = rk_rng_readl(rk_rng, TRNG_V1_ISTAT);
288 + rk_rng_writel(rk_rng, reg, TRNG_V1_ISTAT);
290 + /* generate 256 bits of random data */
291 + rk_rng_writel(rk_rng, TRNG_V1_MODE_256_BIT, TRNG_V1_MODE);
292 + rk_rng_writel(rk_rng, TRNG_V1_CTRL_RAND, TRNG_V1_CTRL);
294 + ret = readl_poll_timeout_atomic(rk_rng->base + TRNG_V1_ISTAT, reg,
295 + (reg & TRNG_V1_ISTAT_RAND_RDY), 0,
296 + RK_RNG_POLL_TIMEOUT_US);
300 + /* Read random data that's in registers TRNG_V1_RAND0 through RAND7 */
301 + memcpy_fromio(buf, rk_rng->base + TRNG_V1_RAND0, to_read);
305 + rk_rng_writel(rk_rng, reg, TRNG_V1_ISTAT);
306 + /* close the TRNG */
307 + rk_rng_writel(rk_rng, TRNG_V1_CTRL_NOP, TRNG_V1_CTRL);
309 + pm_runtime_mark_last_busy(rk_rng->dev);
310 + pm_runtime_put_sync_autosuspend(rk_rng->dev);
312 + return (ret < 0) ? ret : to_read;
315 +static const struct rk_rng_soc_data rk3568_soc_data = {
316 + .rk_rng_init = rk3568_rng_init,
317 + .rk_rng_read = rk3568_rng_read,
318 + .rk_rng_cleanup = rk3568_rng_cleanup,
320 + .reset_optional = false,
323 +static const struct rk_rng_soc_data rk3588_soc_data = {
324 + .rk_rng_init = rk3588_rng_init,
325 + .rk_rng_read = rk3588_rng_read,
326 + .rk_rng_cleanup = rk3588_rng_cleanup,
327 + .quality = 999, /* as determined by actual testing */
328 + .reset_optional = true,
331 static int rk_rng_probe(struct platform_device *pdev)
333 struct device *dev = &pdev->dev;
334 @@ -139,6 +324,7 @@ static int rk_rng_probe(struct platform_
338 + rk_rng->soc_data = of_device_get_match_data(dev);
339 rk_rng->base = devm_platform_ioremap_resource(pdev, 0);
340 if (IS_ERR(rk_rng->base))
341 return PTR_ERR(rk_rng->base);
342 @@ -148,24 +334,30 @@ static int rk_rng_probe(struct platform_
343 return dev_err_probe(dev, rk_rng->clk_num,
344 "Failed to get clks property\n");
346 - rst = devm_reset_control_array_get_exclusive(dev);
348 - return dev_err_probe(dev, PTR_ERR(rst), "Failed to get reset property\n");
350 - reset_control_assert(rst);
352 - reset_control_deassert(rst);
353 + if (rk_rng->soc_data->reset_optional)
354 + rst = devm_reset_control_array_get_optional_exclusive(dev);
356 + rst = devm_reset_control_array_get_exclusive(dev);
360 + return dev_err_probe(dev, PTR_ERR(rst), "Failed to get reset property\n");
362 + reset_control_assert(rst);
364 + reset_control_deassert(rst);
367 platform_set_drvdata(pdev, rk_rng);
369 rk_rng->rng.name = dev_driver_string(dev);
370 if (!IS_ENABLED(CONFIG_PM)) {
371 - rk_rng->rng.init = rk_rng_init;
372 - rk_rng->rng.cleanup = rk_rng_cleanup;
373 + rk_rng->rng.init = rk_rng->soc_data->rk_rng_init;
374 + rk_rng->rng.cleanup = rk_rng->soc_data->rk_rng_cleanup;
376 - rk_rng->rng.read = rk_rng_read;
377 + rk_rng->rng.read = rk_rng->soc_data->rk_rng_read;
379 - rk_rng->rng.quality = 900;
380 + rk_rng->rng.quality = rk_rng->soc_data->quality;
382 pm_runtime_set_autosuspend_delay(dev, RK_RNG_AUTOSUSPEND_DELAY);
383 pm_runtime_use_autosuspend(dev);
384 @@ -184,7 +376,7 @@ static int __maybe_unused rk_rng_runtime
386 struct rk_rng *rk_rng = dev_get_drvdata(dev);
388 - rk_rng_cleanup(&rk_rng->rng);
389 + rk_rng->soc_data->rk_rng_cleanup(&rk_rng->rng);
393 @@ -193,7 +385,7 @@ static int __maybe_unused rk_rng_runtime
395 struct rk_rng *rk_rng = dev_get_drvdata(dev);
397 - return rk_rng_init(&rk_rng->rng);
398 + return rk_rng->soc_data->rk_rng_init(&rk_rng->rng);
401 static const struct dev_pm_ops rk_rng_pm_ops = {
402 @@ -204,7 +396,8 @@ static const struct dev_pm_ops rk_rng_pm
405 static const struct of_device_id rk_rng_dt_match[] = {
406 - { .compatible = "rockchip,rk3568-rng", },
407 + { .compatible = "rockchip,rk3568-rng", .data = (void *)&rk3568_soc_data },
408 + { .compatible = "rockchip,rk3588-rng", .data = (void *)&rk3588_soc_data },
412 @@ -221,8 +414,9 @@ static struct platform_driver rk_rng_dri
414 module_platform_driver(rk_rng_driver);
416 -MODULE_DESCRIPTION("Rockchip RK3568 True Random Number Generator driver");
417 +MODULE_DESCRIPTION("Rockchip True Random Number Generator driver");
418 MODULE_AUTHOR("Lin Jinhan <troy.lin@rock-chips.com>");
419 MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
420 MODULE_AUTHOR("Daniel Golle <daniel@makrotopia.org>");
421 +MODULE_AUTHOR("Nicolas Frattaroli <nicolas.frattaroli@collabora.com>");
422 MODULE_LICENSE("GPL");