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
331
332
333
334
335
336
|
/**
*
* Copyright (c) 2008 Loc Ho <spulijala@amcc.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Detail Description:
* This file AMCC TRNG offload Linux device driver
*
* @file trng4xx.c
*
*
*/
#include <linux/version.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/of_platform.h>
#include <linux/hw_random.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <crypto/trng4xx.h>
#define TRNG4XX_VER_STR "0.1"
#define TRNG4XX_HDR "TRNG4XX: "
/* #define TRNG4XX_DEBUG */
#if !defined(TRNG4XX_DEBUG)
# define TRNG4XX_LOG(fmt, ...)
#else
# define TRNG4XX_LOG(fmt, ...) \
do { \
printk(KERN_INFO TRNG4XX_HDR fmt "\n", ##__VA_ARGS__); \
} while(0);
#endif
struct trng4xx_dev {
struct resource res;
u32 irq;
volatile char __iomem *csr;
struct semaphore access_prot;
u32 datum;
};
struct hal_config {
struct of_device *ofdev;
};
static struct trng4xx_dev trng4xx_dev;
static irqreturn_t trng4xx_irq_handler(int irq, void *id);
static void trng4xx_chk_overflow(void);
int trng4xx_config_set(struct hal_config *cfg)
{
struct device_node *rng_np = cfg->ofdev->node;
int rc = 0;
rc = of_address_to_resource(rng_np, 0, &trng4xx_dev.res);
if (rc)
return -ENODEV;
trng4xx_dev.csr = ioremap(trng4xx_dev.res.start,
trng4xx_dev.res.end -
trng4xx_dev.res.start + 1);
if (trng4xx_dev.csr == NULL) {
printk(KERN_ERR "unable to ioremap 0x%02X_%08X size %d\n",
(u32) (trng4xx_dev.res.start >> 32),
(u32) trng4xx_dev.res.start,
(u32) (trng4xx_dev.res.end -
trng4xx_dev.res.start + 1));
return -ENOMEM;
}
TRNG4XX_LOG("TRNG1 0x%02X_%08X size %d\n",
(u32) (trng4xx_dev.res.start >> 32),
(u32) trng4xx_dev.res.start,
(u32) (trng4xx_dev.res.end -
trng4xx_dev.res.start + 1));
trng4xx_dev.irq = of_irq_to_resource(rng_np, 0, NULL);
if (trng4xx_dev.irq == NO_IRQ) {
/* Un-map CSR */
iounmap(trng4xx_dev.csr);
trng4xx_dev.csr = NULL;
return -EINVAL;
}
return 0;
}
int trng4xx_pka_config_clear(void)
{
iounmap(trng4xx_dev.csr);
return 0;
}
inline int trng4xx_hw_read32(u32 reg_addr, u32 *data_val)
{
*data_val = in_be32((unsigned __iomem *)
(trng4xx_dev.csr + reg_addr));
return 0;
}
inline int trng4xx_hw_write32(u32 reg_addr, u32 data_val)
{
out_be32((unsigned __iomem *) (trng4xx_dev.csr + reg_addr),
data_val);
return 0;
}
int trng4xx_hw_init(void)
{
int rc;
rc = request_irq(trng4xx_dev.irq, trng4xx_irq_handler,
0, "TRNG", NULL);
if (rc != 0) {
printk(KERN_ERR "failed to register interrupt IRQ %d\n",
trng4xx_dev.irq);
return rc;
}
return 0;
}
int trng4xx_hw_deinit(void)
{
free_irq(trng4xx_dev.irq, NULL);
return 0;
}
static irqreturn_t trng4xx_irq_handler(int irq, void *id)
{
/* TRNG Alarm Counter overflow */
trng4xx_chk_overflow();
return IRQ_HANDLED;
}
/**
* TRNG Functions
*
*/
static void trng4xx_chk_overflow(void)
{
/* TRNG Alarm Counter overflow */
int rc;
u32 val;
struct trng4xx_cfg {
u32 ring1_delay_sel :3;
u32 ring2_delay_sel :3;
u32 reset_cnt :6;
} __attribute__((packed));
rc = trng4xx_hw_write32(TRNG4XX_ALARMCNT_ADDR, val);
if (rc != 0)
return;
if (val > 128) {
struct trng4xx_cfg *trng4xx_cfg;
/* Alarm count is half, reset it */
rc = trng4xx_hw_read32(TRNG4XX_CFG_ADDR, &val);
if (rc != 0)
return;
trng4xx_cfg = (struct trng4xx_cfg *) &val;
++trng4xx_cfg->ring1_delay_sel;
trng4xx_cfg->ring2_delay_sel =
(~trng4xx_cfg->ring1_delay_sel) & 0x07;
rc = trng4xx_hw_write32(TRNG4XX_CFG_ADDR, val);
if (rc != 0)
return;
trng4xx_hw_write32(TRNG4XX_ALARMCNT_ADDR, 0x00000000);
if (rc != 0)
return;
}
}
#define MAX_TRY 3
int trng4xx_random(u32 *rand_val)
{
u32 val = 0;
int rc;
u16 try_cnt = 0;
down(&trng4xx_dev.access_prot);
do {
rc = trng4xx_hw_read32(TRNG4XX_STATUS_ADDR, &val);
if (rc != 0)
goto err;
} while ((val & TRNG4XX_STATUS_BUSY) && ++try_cnt <= MAX_TRY);
if (val & TRNG4XX_STATUS_BUSY) {
rc = -EINPROGRESS;
goto err;
}
rc = trng4xx_hw_read32(TRNG4XX_OUTPUT_ADDR, rand_val);
err:
up(&trng4xx_dev.access_prot);
return rc;
}
EXPORT_SYMBOL_GPL(trng4xx_random);
static int trng4xx_data_present(struct hwrng *rng, int wait)
{
struct trng4xx_dev *dev = (struct trng4xx_dev *) rng->priv;
int i;
u32 val;
down(&trng4xx_dev.access_prot);
for (i = 0; i < 20; i++) {
udelay(10);
trng4xx_hw_read32(TRNG4XX_STATUS_ADDR, &val);
if (!(val & TRNG4XX_STATUS_BUSY)) {
trng4xx_hw_read32(TRNG4XX_OUTPUT_ADDR, &dev->datum);
break;
}
if (!wait)
break;
}
up(&trng4xx_dev.access_prot);
return (val & TRNG4XX_STATUS_BUSY) ? 0 : 1;
}
static int trng4xx_data_read(struct hwrng *rng, u32 *data)
{
struct trng4xx_dev *dev = (struct trng4xx_dev *) rng->priv;
*data = dev->datum;
//printk("*data = 0x%08x\n", *data);
return 4;
}
static int trng4xx_init(struct hwrng *rng)
{
return trng4xx_hw_init();
}
static void trng4xx_cleanup(struct hwrng *rng)
{
trng4xx_hw_deinit();
}
static struct hwrng trng4xx_func = {
.name = "ppc4xx-trng",
.init = trng4xx_init,
.cleanup = trng4xx_cleanup,
.data_present = trng4xx_data_present,
.data_read = trng4xx_data_read,
.priv = (unsigned long) &trng4xx_dev,
};
/**
* Setup Driver with platform registration
*
*/
static int __devinit trng4xx_probe(struct of_device *ofdev,
const struct of_device_id *match)
{
struct hal_config hw_cfg;
int rc;
hw_cfg.ofdev = ofdev;
rc = trng4xx_config_set(&hw_cfg);
if (rc != 0)
return rc;
TRNG4XX_LOG("AMCC 4xx TRNG v%s @0x%02X_%08X size %d IRQ %d\n",
TRNG4XX_VER_STR,
(u32) (trng4xx_dev.res.start >> 32),
(u32) trng4xx_dev.res.start,
(u32) (trng4xx_dev.res.end - trng4xx_dev.res.start + 1),
trng4xx_dev.irq);
init_MUTEX(&trng4xx_dev.access_prot);
rc = hwrng_register(&trng4xx_func);
if (rc) {
printk(KERN_ERR
"AMCC 4xx TRNG registering failed error %d\n", rc);
goto err;
}
return rc;
err:
trng4xx_pka_config_clear();
return rc;
}
static int __devexit trng4xx_remove(struct of_device *dev)
{
hwrng_unregister(&trng4xx_func);
trng4xx_pka_config_clear();
return 0;
}
static struct of_device_id trng4xx_match[] = {
{ .compatible = "ppc4xx-trng", },
{ .compatible = "amcc,ppc4xx-trng", },
{ },
};
static struct of_platform_driver trng4xx_driver = {
.name = "ppc4xx-trng",
.match_table = trng4xx_match,
.probe = trng4xx_probe,
.remove = trng4xx_remove,
};
static int __init mod_init(void)
{
return of_register_platform_driver(&trng4xx_driver);
}
static void __exit mod_exit(void)
{
of_unregister_platform_driver(&trng4xx_driver);
}
module_init(mod_init);
module_exit(mod_exit);
MODULE_DESCRIPTION("AMCC 4xx True Random Number Generator");
MODULE_LICENSE("GPL");
|