blob: 70c1c032b6d31d56b8a1299171217cfaec091863 [file] [log] [blame]
Jim Liu866eab12022-05-24 16:56:57 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2022 Nuvoton Technology Corp.
4 */
5
6#include <common.h>
7#include <dm.h>
8#include <malloc.h>
9#include <rng.h>
10#include <uboot_aes.h>
11#include <asm/io.h>
12
13#define RNGCS_RNGE BIT(0)
14#define RNGCS_DVALID BIT(1)
15#define RNGCS_CLKP(range) ((0x0f & (range)) << 2)
16#define RNGMODE_M1ROSEL_VAL (0x02) /* Ring Oscillator Select for Method I */
17
18enum {
19 RNG_CLKP_80_100_MHZ = 0x00, /*default */
20 RNG_CLKP_60_80_MHZ = 0x01,
21 RNG_CLKP_50_60_MHZ = 0x02,
22 RNG_CLKP_40_50_MHZ = 0x03,
23 RNG_CLKP_30_40_MHZ = 0x04,
24 RNG_CLKP_25_30_MHZ = 0x05,
25 RNG_CLKP_20_25_MHZ = 0x06,
26 RNG_CLKP_5_20_MHZ = 0x07,
27 RNG_CLKP_2_15_MHZ = 0x08,
28 RNG_CLKP_9_12_MHZ = 0x09,
29 RNG_CLKP_7_9_MHZ = 0x0A,
30 RNG_CLKP_6_7_MHZ = 0x0B,
31 RNG_CLKP_5_6_MHZ = 0x0C,
32 RNG_CLKP_4_5_MHZ = 0x0D,
33 RNG_CLKP_3_4_MHZ = 0x0E,
34 RNG_NUM_OF_CLKP
35};
36
37struct npcm_rng_regs {
38 unsigned int rngcs;
39 unsigned int rngd;
40 unsigned int rngmode;
41};
42
43struct npcm_rng_priv {
44 struct npcm_rng_regs *regs;
45};
46
47static struct npcm_rng_priv *rng_priv;
48
49void npcm_rng_init(void)
50{
51 struct npcm_rng_regs *regs = rng_priv->regs;
52 int init;
53
54 /* check if rng enabled */
55 init = readb(&regs->rngcs);
56 if ((init & RNGCS_RNGE) == 0) {
57 /* init rng */
58 writeb(RNGCS_CLKP(RNG_CLKP_20_25_MHZ) | RNGCS_RNGE, &regs->rngcs);
59 writeb(RNGMODE_M1ROSEL_VAL, &regs->rngmode);
60 }
61}
62
63void npcm_rng_disable(void)
64{
65 struct npcm_rng_regs *regs = rng_priv->regs;
66
67 /* disable rng */
68 writeb(0, &regs->rngcs);
69 writeb(0, &regs->rngmode);
70}
71
72void srand(unsigned int seed)
73{
74 /* no need to seed for now */
75}
76
77int npcm_rng_read(struct udevice *dev, void *data, size_t max)
78{
79 struct npcm_rng_regs *regs = rng_priv->regs;
80 int i;
81 int ret_val = 0;
82 char *buf = data;
83
84 npcm_rng_init();
85
86 printf("NPCM HW RNG\n");
87 /* Wait for RNG done (max bytes) */
88 for (i = 0; i < max; i++) {
89 /* wait until DVALID is set */
90 while ((readb(&regs->rngcs) & RNGCS_DVALID) == 0)
91 ;
92 buf[i] = ((unsigned int)readb(&regs->rngd) & 0x000000FF);
93 }
94
95 return ret_val;
96}
97
98unsigned int rand_r(unsigned int *seedp)
99{
100 struct npcm_rng_regs *regs = rng_priv->regs;
101 int i;
102 unsigned int ret_val = 0;
103
104 npcm_rng_init();
105
106 /* Wait for RNG done (4 bytes) */
107 for (i = 0; i < 4 ; i++) {
108 /* wait until DVALID is set */
109 while ((readb(&regs->rngcs) & RNGCS_DVALID) == 0)
110 ;
111 ret_val |= (((unsigned int)readb(&regs->rngd) & 0x000000FF) << (i * 8));
112 }
113
114 return ret_val;
115}
116
117unsigned int rand(void)
118{
119 return rand_r(NULL);
120}
121
122static int npcm_rng_bind(struct udevice *dev)
123{
124 rng_priv = calloc(1, sizeof(struct npcm_rng_priv));
125 if (!rng_priv)
126 return -ENOMEM;
127
128 rng_priv->regs = dev_remap_addr_index(dev, 0);
129 if (!rng_priv->regs) {
130 printf("Cannot find rng reg address, binding failed\n");
131 return -EINVAL;
132 }
133
134 printf("RNG: NPCM RNG module bind OK\n");
135
136 return 0;
137}
138
139static const struct udevice_id npcm_rng_ids[] = {
140 { .compatible = "nuvoton,npcm845-rng" },
141 { .compatible = "nuvoton,npcm750-rng" },
142 { }
143};
144
145static const struct dm_rng_ops npcm_rng_ops = {
146 .read = npcm_rng_read,
147};
148
149U_BOOT_DRIVER(npcm_rng) = {
150 .name = "npcm_rng",
151 .id = UCLASS_RNG,
152 .ops = &npcm_rng_ops,
153 .of_match = npcm_rng_ids,
154 .priv_auto = sizeof(struct npcm_rng_priv),
155 .bind = npcm_rng_bind,
156};