1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * APM X-Gene SoC RNG Driver
5 * Copyright (c) 2014, Applied Micro Circuits Corporation
6 * Author: Rameshwar Prasad Sahu <rsahu@apm.com>
7 * Shamal Winchurkar <swinchurkar@apm.com>
8 * Feng Kan <fkan@apm.com>
11 #include <linux/acpi.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/hw_random.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/platform_device.h>
21 #include <linux/timer.h>
23 #define RNG_MAX_DATUM 4
25 #define XGENE_RNG_RETRY_COUNT 20
26 #define XGENE_RNG_RETRY_INTERVAL 10
29 #define RNG_INOUT_0 0x00
30 #define RNG_INTR_STS_ACK 0x10
31 #define RNG_CONTROL 0x14
32 #define RNG_CONFIG 0x18
33 #define RNG_ALARMCNT 0x1c
34 #define RNG_FROENABLE 0x20
35 #define RNG_FRODETUNE 0x24
36 #define RNG_ALARMMASK 0x28
37 #define RNG_ALARMSTOP 0x2c
38 #define RNG_OPTIONS 0x78
39 #define RNG_EIP_REV 0x7c
41 #define MONOBIT_FAIL_MASK BIT(7)
42 #define POKER_FAIL_MASK BIT(6)
43 #define LONG_RUN_FAIL_MASK BIT(5)
44 #define RUN_FAIL_MASK BIT(4)
45 #define NOISE_FAIL_MASK BIT(3)
46 #define STUCK_OUT_MASK BIT(2)
47 #define SHUTDOWN_OFLO_MASK BIT(1)
48 #define READY_MASK BIT(0)
50 #define MAJOR_HW_REV_RD(src) (((src) & 0x0f000000) >> 24)
51 #define MINOR_HW_REV_RD(src) (((src) & 0x00f00000) >> 20)
52 #define HW_PATCH_LEVEL_RD(src) (((src) & 0x000f0000) >> 16)
53 #define MAX_REFILL_CYCLES_SET(dst, src) \
54 ((dst & ~0xffff0000) | (((u32)src << 16) & 0xffff0000))
55 #define MIN_REFILL_CYCLES_SET(dst, src) \
56 ((dst & ~0x000000ff) | (((u32)src) & 0x000000ff))
57 #define ALARM_THRESHOLD_SET(dst, src) \
58 ((dst & ~0x000000ff) | (((u32)src) & 0x000000ff))
59 #define ENABLE_RNG_SET(dst, src) \
60 ((dst & ~BIT(10)) | (((u32)src << 10) & BIT(10)))
61 #define REGSPEC_TEST_MODE_SET(dst, src) \
62 ((dst & ~BIT(8)) | (((u32)src << 8) & BIT(8)))
63 #define MONOBIT_FAIL_MASK_SET(dst, src) \
64 ((dst & ~BIT(7)) | (((u32)src << 7) & BIT(7)))
65 #define POKER_FAIL_MASK_SET(dst, src) \
66 ((dst & ~BIT(6)) | (((u32)src << 6) & BIT(6)))
67 #define LONG_RUN_FAIL_MASK_SET(dst, src) \
68 ((dst & ~BIT(5)) | (((u32)src << 5) & BIT(5)))
69 #define RUN_FAIL_MASK_SET(dst, src) \
70 ((dst & ~BIT(4)) | (((u32)src << 4) & BIT(4)))
71 #define NOISE_FAIL_MASK_SET(dst, src) \
72 ((dst & ~BIT(3)) | (((u32)src << 3) & BIT(3)))
73 #define STUCK_OUT_MASK_SET(dst, src) \
74 ((dst & ~BIT(2)) | (((u32)src << 2) & BIT(2)))
75 #define SHUTDOWN_OFLO_MASK_SET(dst, src) \
76 ((dst & ~BIT(1)) | (((u32)src << 1) & BIT(1)))
78 struct xgene_rng_dev
{
80 void __iomem
*csr_base
;
83 u32 failure_cnt
; /* Failure count last minute */
84 unsigned long failure_ts
;/* First failure timestamp */
85 struct timer_list failure_timer
;
89 static void xgene_rng_expired_timer(struct timer_list
*t
)
91 struct xgene_rng_dev
*ctx
= from_timer(ctx
, t
, failure_timer
);
93 /* Clear failure counter as timer expired */
94 disable_irq(ctx
->irq
);
96 del_timer(&ctx
->failure_timer
);
100 static void xgene_rng_start_timer(struct xgene_rng_dev
*ctx
)
102 ctx
->failure_timer
.expires
= jiffies
+ 120 * HZ
;
103 add_timer(&ctx
->failure_timer
);
107 * Initialize or reinit free running oscillators (FROs)
109 static void xgene_rng_init_fro(struct xgene_rng_dev
*ctx
, u32 fro_val
)
111 writel(fro_val
, ctx
->csr_base
+ RNG_FRODETUNE
);
112 writel(0x00000000, ctx
->csr_base
+ RNG_ALARMMASK
);
113 writel(0x00000000, ctx
->csr_base
+ RNG_ALARMSTOP
);
114 writel(0xFFFFFFFF, ctx
->csr_base
+ RNG_FROENABLE
);
117 static void xgene_rng_chk_overflow(struct xgene_rng_dev
*ctx
)
121 val
= readl(ctx
->csr_base
+ RNG_INTR_STS_ACK
);
122 if (val
& MONOBIT_FAIL_MASK
)
124 * LFSR detected an out-of-bounds number of 1s after
125 * checking 20,000 bits (test T1 as specified in the
128 dev_err(ctx
->dev
, "test monobit failure error 0x%08X\n", val
);
129 if (val
& POKER_FAIL_MASK
)
131 * LFSR detected an out-of-bounds value in at least one
132 * of the 16 poker_count_X counters or an out of bounds sum
133 * of squares value after checking 20,000 bits (test T2 as
134 * specified in the AIS-31 standard)
136 dev_err(ctx
->dev
, "test poker failure error 0x%08X\n", val
);
137 if (val
& LONG_RUN_FAIL_MASK
)
139 * LFSR detected a sequence of 34 identical bits
140 * (test T4 as specified in the AIS-31 standard)
142 dev_err(ctx
->dev
, "test long run failure error 0x%08X\n", val
);
143 if (val
& RUN_FAIL_MASK
)
145 * LFSR detected an outof-bounds value for at least one
146 * of the running counters after checking 20,000 bits
147 * (test T3 as specified in the AIS-31 standard)
149 dev_err(ctx
->dev
, "test run failure error 0x%08X\n", val
);
150 if (val
& NOISE_FAIL_MASK
)
151 /* LFSR detected a sequence of 48 identical bits */
152 dev_err(ctx
->dev
, "noise failure error 0x%08X\n", val
);
153 if (val
& STUCK_OUT_MASK
)
155 * Detected output data registers generated same value twice
158 dev_err(ctx
->dev
, "stuck out failure error 0x%08X\n", val
);
160 if (val
& SHUTDOWN_OFLO_MASK
) {
163 /* FROs shut down after a second error event. Try recover. */
164 if (++ctx
->failure_cnt
== 1) {
165 /* 1st time, just recover */
166 ctx
->failure_ts
= jiffies
;
167 frostopped
= readl(ctx
->csr_base
+ RNG_ALARMSTOP
);
168 xgene_rng_init_fro(ctx
, frostopped
);
171 * We must start a timer to clear out this error
172 * in case the system timer wrap around
174 xgene_rng_start_timer(ctx
);
176 /* 2nd time failure in lesser than 1 minute? */
177 if (time_after(ctx
->failure_ts
+ 60 * HZ
, jiffies
)) {
179 "FRO shutdown failure error 0x%08X\n",
182 /* 2nd time failure after 1 minutes, recover */
183 ctx
->failure_ts
= jiffies
;
184 ctx
->failure_cnt
= 1;
186 * We must start a timer to clear out this
187 * error in case the system timer wrap
190 xgene_rng_start_timer(ctx
);
192 frostopped
= readl(ctx
->csr_base
+ RNG_ALARMSTOP
);
193 xgene_rng_init_fro(ctx
, frostopped
);
197 writel(val
, ctx
->csr_base
+ RNG_INTR_STS_ACK
);
200 static irqreturn_t
xgene_rng_irq_handler(int irq
, void *id
)
202 struct xgene_rng_dev
*ctx
= id
;
204 /* RNG Alarm Counter overflow */
205 xgene_rng_chk_overflow(ctx
);
210 static int xgene_rng_data_present(struct hwrng
*rng
, int wait
)
212 struct xgene_rng_dev
*ctx
= (struct xgene_rng_dev
*) rng
->priv
;
215 for (i
= 0; i
< XGENE_RNG_RETRY_COUNT
; i
++) {
216 val
= readl(ctx
->csr_base
+ RNG_INTR_STS_ACK
);
217 if ((val
& READY_MASK
) || !wait
)
219 udelay(XGENE_RNG_RETRY_INTERVAL
);
222 return (val
& READY_MASK
);
225 static int xgene_rng_data_read(struct hwrng
*rng
, u32
*data
)
227 struct xgene_rng_dev
*ctx
= (struct xgene_rng_dev
*) rng
->priv
;
230 for (i
= 0; i
< ctx
->datum_size
; i
++)
231 data
[i
] = readl(ctx
->csr_base
+ RNG_INOUT_0
+ i
* 4);
233 /* Clear ready bit to start next transaction */
234 writel(READY_MASK
, ctx
->csr_base
+ RNG_INTR_STS_ACK
);
236 return ctx
->datum_size
<< 2;
239 static void xgene_rng_init_internal(struct xgene_rng_dev
*ctx
)
243 writel(0x00000000, ctx
->csr_base
+ RNG_CONTROL
);
245 val
= MAX_REFILL_CYCLES_SET(0, 10);
246 val
= MIN_REFILL_CYCLES_SET(val
, 10);
247 writel(val
, ctx
->csr_base
+ RNG_CONFIG
);
249 val
= ALARM_THRESHOLD_SET(0, 0xFF);
250 writel(val
, ctx
->csr_base
+ RNG_ALARMCNT
);
252 xgene_rng_init_fro(ctx
, 0);
254 writel(MONOBIT_FAIL_MASK
|
261 READY_MASK
, ctx
->csr_base
+ RNG_INTR_STS_ACK
);
263 val
= ENABLE_RNG_SET(0, 1);
264 val
= MONOBIT_FAIL_MASK_SET(val
, 1);
265 val
= POKER_FAIL_MASK_SET(val
, 1);
266 val
= LONG_RUN_FAIL_MASK_SET(val
, 1);
267 val
= RUN_FAIL_MASK_SET(val
, 1);
268 val
= NOISE_FAIL_MASK_SET(val
, 1);
269 val
= STUCK_OUT_MASK_SET(val
, 1);
270 val
= SHUTDOWN_OFLO_MASK_SET(val
, 1);
271 writel(val
, ctx
->csr_base
+ RNG_CONTROL
);
274 static int xgene_rng_init(struct hwrng
*rng
)
276 struct xgene_rng_dev
*ctx
= (struct xgene_rng_dev
*) rng
->priv
;
278 ctx
->failure_cnt
= 0;
279 timer_setup(&ctx
->failure_timer
, xgene_rng_expired_timer
, 0);
281 ctx
->revision
= readl(ctx
->csr_base
+ RNG_EIP_REV
);
283 dev_dbg(ctx
->dev
, "Rev %d.%d.%d\n",
284 MAJOR_HW_REV_RD(ctx
->revision
),
285 MINOR_HW_REV_RD(ctx
->revision
),
286 HW_PATCH_LEVEL_RD(ctx
->revision
));
288 dev_dbg(ctx
->dev
, "Options 0x%08X",
289 readl(ctx
->csr_base
+ RNG_OPTIONS
));
291 xgene_rng_init_internal(ctx
);
293 ctx
->datum_size
= RNG_MAX_DATUM
;
299 static const struct acpi_device_id xgene_rng_acpi_match
[] = {
303 MODULE_DEVICE_TABLE(acpi
, xgene_rng_acpi_match
);
306 static struct hwrng xgene_rng_func
= {
308 .init
= xgene_rng_init
,
309 .data_present
= xgene_rng_data_present
,
310 .data_read
= xgene_rng_data_read
,
313 static int xgene_rng_probe(struct platform_device
*pdev
)
315 struct xgene_rng_dev
*ctx
;
319 ctx
= devm_kzalloc(&pdev
->dev
, sizeof(*ctx
), GFP_KERNEL
);
323 ctx
->dev
= &pdev
->dev
;
325 ctx
->csr_base
= devm_platform_ioremap_resource(pdev
, 0);
326 if (IS_ERR(ctx
->csr_base
))
327 return PTR_ERR(ctx
->csr_base
);
329 rc
= platform_get_irq(pdev
, 0);
334 dev_dbg(&pdev
->dev
, "APM X-Gene RNG BASE %p ALARM IRQ %d",
335 ctx
->csr_base
, ctx
->irq
);
337 rc
= devm_request_irq(&pdev
->dev
, ctx
->irq
, xgene_rng_irq_handler
, 0,
338 dev_name(&pdev
->dev
), ctx
);
340 return dev_err_probe(&pdev
->dev
, rc
, "Could not request RNG alarm IRQ\n");
342 /* Enable IP clock */
343 clk
= devm_clk_get_optional_enabled(&pdev
->dev
, NULL
);
345 return dev_err_probe(&pdev
->dev
, PTR_ERR(clk
), "Couldn't get the clock for RNG\n");
347 xgene_rng_func
.priv
= (unsigned long) ctx
;
349 rc
= devm_hwrng_register(&pdev
->dev
, &xgene_rng_func
);
351 return dev_err_probe(&pdev
->dev
, rc
, "RNG registering failed\n");
353 rc
= device_init_wakeup(&pdev
->dev
, 1);
355 return dev_err_probe(&pdev
->dev
, rc
, "RNG device_init_wakeup failed\n");
360 static void xgene_rng_remove(struct platform_device
*pdev
)
364 rc
= device_init_wakeup(&pdev
->dev
, 0);
366 dev_err(&pdev
->dev
, "RNG init wakeup failed error %d\n", rc
);
369 static const struct of_device_id xgene_rng_of_match
[] = {
370 { .compatible
= "apm,xgene-rng" },
374 MODULE_DEVICE_TABLE(of
, xgene_rng_of_match
);
376 static struct platform_driver xgene_rng_driver
= {
377 .probe
= xgene_rng_probe
,
378 .remove
= xgene_rng_remove
,
381 .of_match_table
= xgene_rng_of_match
,
382 .acpi_match_table
= ACPI_PTR(xgene_rng_acpi_match
),
386 module_platform_driver(xgene_rng_driver
);
387 MODULE_DESCRIPTION("APM X-Gene RNG driver");
388 MODULE_LICENSE("GPL");