1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Derived from arch/powerpc/platforms/powernv/rng.c, which is:
4  * Copyright 2013, Michael Ellerman, IBM Corporation.
5  */
6 
7 #define pr_fmt(fmt)	"microwatt-rng: " fmt
8 
9 #include <linux/kernel.h>
10 #include <linux/smp.h>
11 #include <asm/archrandom.h>
12 #include <asm/cputable.h>
13 #include <asm/machdep.h>
14 #include "microwatt.h"
15 
16 #define DARN_ERR 0xFFFFFFFFFFFFFFFFul
17 
18 static int microwatt_get_random_darn(unsigned long *v)
19 {
20 	unsigned long val;
21 
22 	/* Using DARN with L=1 - 64-bit conditioned random number */
23 	asm volatile(PPC_DARN(%0, 1) : "=r"(val));
24 
25 	if (val == DARN_ERR)
26 		return 0;
27 
28 	*v = val;
29 
30 	return 1;
31 }
32 
33 void __init microwatt_rng_init(void)
34 {
35 	unsigned long val;
36 	int i;
37 
38 	for (i = 0; i < 10; i++) {
39 		if (microwatt_get_random_darn(&val)) {
40 			ppc_md.get_random_seed = microwatt_get_random_darn;
41 			return;
42 		}
43 	}
44 }
45