1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-3-08 GuEe-GUI the first version
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#define DBG_TAG "rng.bcm2835"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#define RNG_CTRL 0x0
#define RNG_STATUS 0x4
#define RNG_DATA 0x8
#define RNG_INT_MASK 0x10
/* enable rng */
#define RNG_RBGEN 0x1
/* the initial numbers generated are "less random" so will be discarded */
#define RNG_WARMUP_COUNT 0x40000
#define RNG_INT_OFF 0x1
struct bcm2835_rng_soc_data
{
rt_bool_t mask_interrupts;
};
struct bcm2835_rng
{
struct rt_hwcrypto_device parent;
void *regs;
rt_bool_t mask_interrupts;
struct rt_clk *clk;
struct rt_reset_control *rstc;
};
#define raw_to_bcm2835_rng(raw) rt_container_of(raw, struct bcm2835_rng, parent)
rt_inline rt_uint32_t rng_readl(struct bcm2835_rng *brng, int offset)
{
return HWREG32(brng->regs + offset);
}
rt_inline void rng_writel(struct bcm2835_rng *brng, int offset, rt_uint32_t val)
{
HWREG32(brng->regs + offset) = val;
}
static rt_uint32_t bcm2835_rng_read(struct bcm2835_rng *brng, void *buf,
rt_size_t max, rt_bool_t wait)
{
rt_uint32_t max_words = max / sizeof(rt_uint32_t), num_words;
while ((rng_readl(brng, RNG_STATUS) >> 24) == 0)
{
if (!wait)
{
return 0;
}
rt_thread_mdelay(1000);
}
num_words = rng_readl(brng, RNG_STATUS) >> 24;
if (num_words > max_words)
{
num_words = max_words;
}
for (int count = 0; count < num_words; ++count)
{
((rt_uint32_t *)buf)[count] = rng_readl(brng, RNG_DATA);
}
return num_words * sizeof(rt_uint32_t);
}
static rt_err_t bcm2835_rng_init(struct bcm2835_rng *brng)
{
rt_err_t err;
rt_uint32_t val;
if ((err = rt_clk_prepare_enable(brng->clk)))
{
LOG_E("CLK enable error = %s", rt_strerror(err));
return err;
}
if ((err = rt_reset_control_reset(brng->rstc)))
{
LOG_E("Reset error = %s", rt_strerror(err));
return err;
}
if (brng->mask_interrupts)
{
/* mask the interrupt */
val = rng_readl(brng, RNG_INT_MASK);
val |= RNG_INT_OFF;
rng_writel(brng, RNG_INT_MASK, val);
}
/* set warm-up count & enable */
rng_writel(brng, RNG_STATUS, RNG_WARMUP_COUNT);
rng_writel(brng, RNG_CTRL, RNG_RBGEN);
return RT_EOK;
}
static void bcm2835_rng_cleanup(struct bcm2835_rng *brng)
{
/* Disable rng hardware */
rng_writel(brng, RNG_CTRL, 0);
rt_clk_disable_unprepare(brng->clk);
}
static rt_uint32_t bcm2835_rng_rand(struct hwcrypto_rng *ctx)
{
rt_size_t size;
rt_uint32_t rand;
struct bcm2835_rng *brng = raw_to_bcm2835_rng(ctx->parent.device);
size = bcm2835_rng_read(brng, &rand, sizeof(rand), RT_TRUE);
if (size != sizeof(rand))
{
return 0;
}
return rand;
}
static const struct hwcrypto_rng_ops rng_ops =
{
.update = bcm2835_rng_rand,
};
static rt_err_t bcm2835_rng_create(struct rt_hwcrypto_ctx *ctx)
{
rt_err_t res = RT_EOK;
struct hwcrypto_rng *rng;
switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
{
case HWCRYPTO_TYPE_RNG:
ctx->contex = RT_NULL;
rng = rt_container_of(ctx, struct hwcrypto_rng, parent);
rng->ops = &rng_ops;
break;
default:
res = -RT_ENOSYS;
break;
}
return res;
}
static void bcm2835_rng_destroy(struct rt_hwcrypto_ctx *ctx)
{
struct bcm2835_rng *brng = raw_to_bcm2835_rng(ctx->device);
bcm2835_rng_cleanup(brng);
rt_free(ctx->contex);
}
static rt_err_t bcm2835_rng_copy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src)
{
rt_err_t err = RT_EOK;
switch (src->type & HWCRYPTO_MAIN_TYPE_MASK)
{
case HWCRYPTO_TYPE_RNG:
break;
default:
err = -RT_ENOSYS;
break;
}
return err;
}
static void bcm2835_rng_reset(struct rt_hwcrypto_ctx *ctx)
{
struct bcm2835_rng *brng = raw_to_bcm2835_rng(ctx->device);
bcm2835_rng_init(brng);
}
static const struct rt_hwcrypto_ops bcm2835_rng_ops =
{
.create = bcm2835_rng_create,
.destroy = bcm2835_rng_destroy,
.copy = bcm2835_rng_copy,
.reset = bcm2835_rng_reset,
};
static void bcm2835_rng_free(struct bcm2835_rng *brng)
{
if (!brng->regs)
{
rt_iounmap(brng->regs);
}
if (!rt_is_err_or_null(brng->rstc))
{
rt_reset_control_assert(brng->rstc);
rt_reset_control_put(brng->rstc);
}
if (!rt_is_err_or_null(brng->clk))
{
rt_clk_disable_unprepare(brng->clk);
rt_clk_put(brng->clk);
}
rt_free(brng);
}
static rt_err_t bcm2835_rng_probe(struct rt_platform_device *pdev)
{
rt_err_t err = RT_EOK;
struct rt_device *dev = &pdev->parent;
const struct bcm2835_rng_soc_data *soc_data;
struct bcm2835_rng *brng = rt_calloc(1, sizeof(*brng));
if (!brng)
{
return -RT_ENOMEM;
}
brng->regs = rt_dm_dev_iomap(dev, 0);
if (!brng->regs)
{
err = -RT_EIO;
goto _fail;
}
brng->clk = rt_clk_get_by_index(dev, 0);
if (rt_is_err(brng->clk))
{
err = rt_ptr_err(brng->clk);
goto _fail;
}
brng->rstc = rt_reset_control_get_by_index(dev, 0);
if (rt_is_err(brng->rstc))
{
err = rt_ptr_err(brng->rstc);
goto _fail;
}
if ((soc_data = pdev->id->data))
{
brng->mask_interrupts = soc_data->mask_interrupts;
}
dev->user_data = brng;
brng->parent.ops = &bcm2835_rng_ops;
if ((err = rt_hwcrypto_register(&brng->parent, "hwrng")))
{
goto _fail;
}
bcm2835_rng_init(brng);
return RT_EOK;
_fail:
bcm2835_rng_free(brng);
return err;
}
static rt_err_t bcm2835_rng_remove(struct rt_platform_device *pdev)
{
struct bcm2835_rng *brng = pdev->parent.user_data;
bcm2835_rng_cleanup(brng);
rt_device_unregister(&brng->parent.parent);
bcm2835_rng_free(brng);
return RT_EOK;
}
static const struct bcm2835_rng_soc_data nsp_rng_data =
{
.mask_interrupts = RT_TRUE,
};
static const struct rt_ofw_node_id bcm2835_rng_ofw_ids[] =
{
{ .compatible = "brcm,bcm2835-rng" },
{ .compatible = "brcm,bcm-nsp-rng", .data = &nsp_rng_data },
{ .compatible = "brcm,bcm5301x-rng", .data = &nsp_rng_data },
{ .compatible = "brcm,bcm6368-rng" },
{ /* sentinel */ }
};
static struct rt_platform_driver bcm2835_rng_driver =
{
.name = "bcm2835-rng",
.ids = bcm2835_rng_ofw_ids,
.probe = bcm2835_rng_probe,
.remove = bcm2835_rng_remove,
};
RT_PLATFORM_DRIVER_EXPORT(bcm2835_rng_driver);
|