2 * omap-rng.c - RNG driver for TI OMAP CPU family
4 * Author: Deepak Saxena <dsaxena@plexity.net>
6 * Copyright 2005 (c) MontaVista Software, Inc.
8 * Mostly based on original driver:
10 * Copyright (C) 2005 Nokia Corporation
11 * Author: Juha Yrjölä <juha.yrjola@nokia.com>
13 * This file is licensed under the terms of the GNU General Public
14 * License version 2. This program is licensed "as is" without any
15 * warranty of any kind, whether express or implied.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/random.h>
21 #include <linux/err.h>
22 #include <linux/platform_device.h>
23 #include <linux/hw_random.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pm_runtime.h>
28 #include <linux/of_device.h>
29 #include <linux/of_address.h>
30 #include <linux/interrupt.h>
31 #include <linux/clk.h>
35 #define RNG_REG_STATUS_RDY (1 << 0)
37 #define RNG_REG_INTACK_RDY_MASK (1 << 0)
38 #define RNG_REG_INTACK_SHUTDOWN_OFLO_MASK (1 << 1)
39 #define RNG_SHUTDOWN_OFLO_MASK (1 << 1)
41 #define RNG_CONTROL_STARTUP_CYCLES_SHIFT 16
42 #define RNG_CONTROL_STARTUP_CYCLES_MASK (0xffff << 16)
43 #define RNG_CONTROL_ENABLE_TRNG_SHIFT 10
44 #define RNG_CONTROL_ENABLE_TRNG_MASK (1 << 10)
46 #define RNG_CONFIG_MAX_REFIL_CYCLES_SHIFT 16
47 #define RNG_CONFIG_MAX_REFIL_CYCLES_MASK (0xffff << 16)
48 #define RNG_CONFIG_MIN_REFIL_CYCLES_SHIFT 0
49 #define RNG_CONFIG_MIN_REFIL_CYCLES_MASK (0xff << 0)
51 #define RNG_CONTROL_STARTUP_CYCLES 0xff
52 #define RNG_CONFIG_MIN_REFIL_CYCLES 0x21
53 #define RNG_CONFIG_MAX_REFIL_CYCLES 0x22
55 #define RNG_ALARMCNT_ALARM_TH_SHIFT 0x0
56 #define RNG_ALARMCNT_ALARM_TH_MASK (0xff << 0)
57 #define RNG_ALARMCNT_SHUTDOWN_TH_SHIFT 16
58 #define RNG_ALARMCNT_SHUTDOWN_TH_MASK (0x1f << 16)
59 #define RNG_ALARM_THRESHOLD 0xff
60 #define RNG_SHUTDOWN_THRESHOLD 0x4
62 #define RNG_REG_FROENABLE_MASK 0xffffff
63 #define RNG_REG_FRODETUNE_MASK 0xffffff
65 #define OMAP2_RNG_OUTPUT_SIZE 0x4
66 #define OMAP4_RNG_OUTPUT_SIZE 0x8
67 #define EIP76_RNG_OUTPUT_SIZE 0x10
88 static const u16 reg_map_omap2
[] = {
89 [RNG_OUTPUT_0_REG
] = 0x0,
90 [RNG_STATUS_REG
] = 0x4,
91 [RNG_CONFIG_REG
] = 0x28,
93 [RNG_SYSCONFIG_REG
] = 0x40,
96 static const u16 reg_map_omap4
[] = {
97 [RNG_OUTPUT_0_REG
] = 0x0,
98 [RNG_OUTPUT_1_REG
] = 0x4,
99 [RNG_STATUS_REG
] = 0x8,
100 [RNG_INTMASK_REG
] = 0xc,
101 [RNG_INTACK_REG
] = 0x10,
102 [RNG_CONTROL_REG
] = 0x14,
103 [RNG_CONFIG_REG
] = 0x18,
104 [RNG_ALARMCNT_REG
] = 0x1c,
105 [RNG_FROENABLE_REG
] = 0x20,
106 [RNG_FRODETUNE_REG
] = 0x24,
107 [RNG_ALARMMASK_REG
] = 0x28,
108 [RNG_ALARMSTOP_REG
] = 0x2c,
109 [RNG_REV_REG
] = 0x1FE0,
110 [RNG_SYSCONFIG_REG
] = 0x1FE4,
113 static const u16 reg_map_eip76
[] = {
114 [RNG_OUTPUT_0_REG
] = 0x0,
115 [RNG_OUTPUT_1_REG
] = 0x4,
116 [RNG_OUTPUT_2_REG
] = 0x8,
117 [RNG_OUTPUT_3_REG
] = 0xc,
118 [RNG_STATUS_REG
] = 0x10,
119 [RNG_INTACK_REG
] = 0x10,
120 [RNG_CONTROL_REG
] = 0x14,
121 [RNG_CONFIG_REG
] = 0x18,
122 [RNG_ALARMCNT_REG
] = 0x1c,
123 [RNG_FROENABLE_REG
] = 0x20,
124 [RNG_FRODETUNE_REG
] = 0x24,
125 [RNG_ALARMMASK_REG
] = 0x28,
126 [RNG_ALARMSTOP_REG
] = 0x2c,
127 [RNG_REV_REG
] = 0x7c,
132 * struct omap_rng_pdata - RNG IP block-specific data
133 * @regs: Pointer to the register offsets structure.
134 * @data_size: No. of bytes in RNG output.
135 * @data_present: Callback to determine if data is available.
136 * @init: Callback for IP specific initialization sequence.
137 * @cleanup: Callback for IP specific cleanup sequence.
139 struct omap_rng_pdata
{
142 u32 (*data_present
)(struct omap_rng_dev
*priv
);
143 int (*init
)(struct omap_rng_dev
*priv
);
144 void (*cleanup
)(struct omap_rng_dev
*priv
);
147 struct omap_rng_dev
{
150 const struct omap_rng_pdata
*pdata
;
156 static inline u32
omap_rng_read(struct omap_rng_dev
*priv
, u16 reg
)
158 return __raw_readl(priv
->base
+ priv
->pdata
->regs
[reg
]);
161 static inline void omap_rng_write(struct omap_rng_dev
*priv
, u16 reg
,
164 __raw_writel(val
, priv
->base
+ priv
->pdata
->regs
[reg
]);
168 static int omap_rng_do_read(struct hwrng
*rng
, void *data
, size_t max
,
171 struct omap_rng_dev
*priv
;
174 priv
= (struct omap_rng_dev
*)rng
->priv
;
176 if (max
< priv
->pdata
->data_size
)
179 for (i
= 0; i
< 20; i
++) {
180 present
= priv
->pdata
->data_present(priv
);
181 if (present
|| !wait
)
189 memcpy_fromio(data
, priv
->base
+ priv
->pdata
->regs
[RNG_OUTPUT_0_REG
],
190 priv
->pdata
->data_size
);
192 if (priv
->pdata
->regs
[RNG_INTACK_REG
])
193 omap_rng_write(priv
, RNG_INTACK_REG
, RNG_REG_INTACK_RDY_MASK
);
195 return priv
->pdata
->data_size
;
198 static int omap_rng_init(struct hwrng
*rng
)
200 struct omap_rng_dev
*priv
;
202 priv
= (struct omap_rng_dev
*)rng
->priv
;
203 return priv
->pdata
->init(priv
);
206 static void omap_rng_cleanup(struct hwrng
*rng
)
208 struct omap_rng_dev
*priv
;
210 priv
= (struct omap_rng_dev
*)rng
->priv
;
211 priv
->pdata
->cleanup(priv
);
215 static inline u32
omap2_rng_data_present(struct omap_rng_dev
*priv
)
217 return omap_rng_read(priv
, RNG_STATUS_REG
) ? 0 : 1;
220 static int omap2_rng_init(struct omap_rng_dev
*priv
)
222 omap_rng_write(priv
, RNG_SYSCONFIG_REG
, 0x1);
226 static void omap2_rng_cleanup(struct omap_rng_dev
*priv
)
228 omap_rng_write(priv
, RNG_SYSCONFIG_REG
, 0x0);
231 static struct omap_rng_pdata omap2_rng_pdata
= {
232 .regs
= (u16
*)reg_map_omap2
,
233 .data_size
= OMAP2_RNG_OUTPUT_SIZE
,
234 .data_present
= omap2_rng_data_present
,
235 .init
= omap2_rng_init
,
236 .cleanup
= omap2_rng_cleanup
,
239 #if defined(CONFIG_OF)
240 static inline u32
omap4_rng_data_present(struct omap_rng_dev
*priv
)
242 return omap_rng_read(priv
, RNG_STATUS_REG
) & RNG_REG_STATUS_RDY
;
245 static int eip76_rng_init(struct omap_rng_dev
*priv
)
249 /* Return if RNG is already running. */
250 if (omap_rng_read(priv
, RNG_CONTROL_REG
) & RNG_CONTROL_ENABLE_TRNG_MASK
)
253 /* Number of 512 bit blocks of raw Noise Source output data that must
254 * be processed by either the Conditioning Function or the
255 * SP 800-90 DRBG ‘BC_DF’ functionality to yield a ‘full entropy’
258 val
= 0x5 << RNG_CONFIG_MIN_REFIL_CYCLES_SHIFT
;
260 /* Number of FRO samples that are XOR-ed together into one bit to be
261 * shifted into the main shift register
263 val
|= RNG_CONFIG_MAX_REFIL_CYCLES
<< RNG_CONFIG_MAX_REFIL_CYCLES_SHIFT
;
264 omap_rng_write(priv
, RNG_CONFIG_REG
, val
);
266 /* Enable all available FROs */
267 omap_rng_write(priv
, RNG_FRODETUNE_REG
, 0x0);
268 omap_rng_write(priv
, RNG_FROENABLE_REG
, RNG_REG_FROENABLE_MASK
);
271 val
= RNG_CONTROL_ENABLE_TRNG_MASK
;
272 omap_rng_write(priv
, RNG_CONTROL_REG
, val
);
277 static int omap4_rng_init(struct omap_rng_dev
*priv
)
281 /* Return if RNG is already running. */
282 if (omap_rng_read(priv
, RNG_CONTROL_REG
) & RNG_CONTROL_ENABLE_TRNG_MASK
)
285 val
= RNG_CONFIG_MIN_REFIL_CYCLES
<< RNG_CONFIG_MIN_REFIL_CYCLES_SHIFT
;
286 val
|= RNG_CONFIG_MAX_REFIL_CYCLES
<< RNG_CONFIG_MAX_REFIL_CYCLES_SHIFT
;
287 omap_rng_write(priv
, RNG_CONFIG_REG
, val
);
289 omap_rng_write(priv
, RNG_FRODETUNE_REG
, 0x0);
290 omap_rng_write(priv
, RNG_FROENABLE_REG
, RNG_REG_FROENABLE_MASK
);
291 val
= RNG_ALARM_THRESHOLD
<< RNG_ALARMCNT_ALARM_TH_SHIFT
;
292 val
|= RNG_SHUTDOWN_THRESHOLD
<< RNG_ALARMCNT_SHUTDOWN_TH_SHIFT
;
293 omap_rng_write(priv
, RNG_ALARMCNT_REG
, val
);
295 val
= RNG_CONTROL_STARTUP_CYCLES
<< RNG_CONTROL_STARTUP_CYCLES_SHIFT
;
296 val
|= RNG_CONTROL_ENABLE_TRNG_MASK
;
297 omap_rng_write(priv
, RNG_CONTROL_REG
, val
);
302 static void omap4_rng_cleanup(struct omap_rng_dev
*priv
)
306 val
= omap_rng_read(priv
, RNG_CONTROL_REG
);
307 val
&= ~RNG_CONTROL_ENABLE_TRNG_MASK
;
308 omap_rng_write(priv
, RNG_CONTROL_REG
, val
);
311 static irqreturn_t
omap4_rng_irq(int irq
, void *dev_id
)
313 struct omap_rng_dev
*priv
= dev_id
;
314 u32 fro_detune
, fro_enable
;
317 * Interrupt raised by a fro shutdown threshold, do the following:
318 * 1. Clear the alarm events.
319 * 2. De tune the FROs which are shutdown.
320 * 3. Re enable the shutdown FROs.
322 omap_rng_write(priv
, RNG_ALARMMASK_REG
, 0x0);
323 omap_rng_write(priv
, RNG_ALARMSTOP_REG
, 0x0);
325 fro_enable
= omap_rng_read(priv
, RNG_FROENABLE_REG
);
326 fro_detune
= ~fro_enable
& RNG_REG_FRODETUNE_MASK
;
327 fro_detune
= fro_detune
| omap_rng_read(priv
, RNG_FRODETUNE_REG
);
328 fro_enable
= RNG_REG_FROENABLE_MASK
;
330 omap_rng_write(priv
, RNG_FRODETUNE_REG
, fro_detune
);
331 omap_rng_write(priv
, RNG_FROENABLE_REG
, fro_enable
);
333 omap_rng_write(priv
, RNG_INTACK_REG
, RNG_REG_INTACK_SHUTDOWN_OFLO_MASK
);
338 static struct omap_rng_pdata omap4_rng_pdata
= {
339 .regs
= (u16
*)reg_map_omap4
,
340 .data_size
= OMAP4_RNG_OUTPUT_SIZE
,
341 .data_present
= omap4_rng_data_present
,
342 .init
= omap4_rng_init
,
343 .cleanup
= omap4_rng_cleanup
,
346 static struct omap_rng_pdata eip76_rng_pdata
= {
347 .regs
= (u16
*)reg_map_eip76
,
348 .data_size
= EIP76_RNG_OUTPUT_SIZE
,
349 .data_present
= omap4_rng_data_present
,
350 .init
= eip76_rng_init
,
351 .cleanup
= omap4_rng_cleanup
,
354 static const struct of_device_id omap_rng_of_match
[] = {
356 .compatible
= "ti,omap2-rng",
357 .data
= &omap2_rng_pdata
,
360 .compatible
= "ti,omap4-rng",
361 .data
= &omap4_rng_pdata
,
364 .compatible
= "inside-secure,safexcel-eip76",
365 .data
= &eip76_rng_pdata
,
369 MODULE_DEVICE_TABLE(of
, omap_rng_of_match
);
371 static int of_get_omap_rng_device_details(struct omap_rng_dev
*priv
,
372 struct platform_device
*pdev
)
374 const struct of_device_id
*match
;
375 struct device
*dev
= &pdev
->dev
;
378 match
= of_match_device(of_match_ptr(omap_rng_of_match
), dev
);
380 dev_err(dev
, "no compatible OF match\n");
383 priv
->pdata
= match
->data
;
385 if (of_device_is_compatible(dev
->of_node
, "ti,omap4-rng") ||
386 of_device_is_compatible(dev
->of_node
, "inside-secure,safexcel-eip76")) {
387 irq
= platform_get_irq(pdev
, 0);
389 dev_err(dev
, "%s: error getting IRQ resource - %d\n",
394 err
= devm_request_irq(dev
, irq
, omap4_rng_irq
,
395 IRQF_TRIGGER_NONE
, dev_name(dev
), priv
);
397 dev_err(dev
, "unable to request irq %d, err = %d\n",
403 * On OMAP4, enabling the shutdown_oflo interrupt is
404 * done in the interrupt mask register. There is no
405 * such register on EIP76, and it's enabled by the
406 * same bit in the control register
408 if (priv
->pdata
->regs
[RNG_INTMASK_REG
])
409 omap_rng_write(priv
, RNG_INTMASK_REG
,
410 RNG_SHUTDOWN_OFLO_MASK
);
412 omap_rng_write(priv
, RNG_CONTROL_REG
,
413 RNG_SHUTDOWN_OFLO_MASK
);
418 static int of_get_omap_rng_device_details(struct omap_rng_dev
*omap_rng
,
419 struct platform_device
*pdev
)
425 static int get_omap_rng_device_details(struct omap_rng_dev
*omap_rng
)
427 /* Only OMAP2/3 can be non-DT */
428 omap_rng
->pdata
= &omap2_rng_pdata
;
432 static int omap_rng_probe(struct platform_device
*pdev
)
434 struct omap_rng_dev
*priv
;
435 struct resource
*res
;
436 struct device
*dev
= &pdev
->dev
;
439 priv
= devm_kzalloc(dev
, sizeof(struct omap_rng_dev
), GFP_KERNEL
);
443 priv
->rng
.read
= omap_rng_do_read
;
444 priv
->rng
.init
= omap_rng_init
;
445 priv
->rng
.cleanup
= omap_rng_cleanup
;
447 priv
->rng
.priv
= (unsigned long)priv
;
448 platform_set_drvdata(pdev
, priv
);
451 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
452 priv
->base
= devm_ioremap_resource(dev
, res
);
453 if (IS_ERR(priv
->base
)) {
454 ret
= PTR_ERR(priv
->base
);
458 priv
->rng
.name
= devm_kstrdup(dev
, dev_name(dev
), GFP_KERNEL
);
459 if (!priv
->rng
.name
) {
464 pm_runtime_enable(&pdev
->dev
);
465 ret
= pm_runtime_get_sync(&pdev
->dev
);
467 dev_err(&pdev
->dev
, "Failed to runtime_get device: %d\n", ret
);
468 pm_runtime_put_noidle(&pdev
->dev
);
472 priv
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
473 if (IS_ERR(priv
->clk
) && PTR_ERR(priv
->clk
) == -EPROBE_DEFER
)
474 return -EPROBE_DEFER
;
475 if (!IS_ERR(priv
->clk
)) {
476 ret
= clk_prepare_enable(priv
->clk
);
479 "Unable to enable the clk: %d\n", ret
);
484 priv
->clk_reg
= devm_clk_get(&pdev
->dev
, "reg");
485 if (IS_ERR(priv
->clk_reg
) && PTR_ERR(priv
->clk_reg
) == -EPROBE_DEFER
)
486 return -EPROBE_DEFER
;
487 if (!IS_ERR(priv
->clk_reg
)) {
488 ret
= clk_prepare_enable(priv
->clk_reg
);
491 "Unable to enable the register clk: %d\n",
497 ret
= (dev
->of_node
) ? of_get_omap_rng_device_details(priv
, pdev
) :
498 get_omap_rng_device_details(priv
);
502 ret
= hwrng_register(&priv
->rng
);
506 dev_info(&pdev
->dev
, "Random Number Generator ver. %02x\n",
507 omap_rng_read(priv
, RNG_REV_REG
));
513 pm_runtime_put_sync(&pdev
->dev
);
514 pm_runtime_disable(&pdev
->dev
);
516 clk_disable_unprepare(priv
->clk_reg
);
517 clk_disable_unprepare(priv
->clk
);
519 dev_err(dev
, "initialization failed.\n");
523 static int omap_rng_remove(struct platform_device
*pdev
)
525 struct omap_rng_dev
*priv
= platform_get_drvdata(pdev
);
527 hwrng_unregister(&priv
->rng
);
529 priv
->pdata
->cleanup(priv
);
531 pm_runtime_put_sync(&pdev
->dev
);
532 pm_runtime_disable(&pdev
->dev
);
534 clk_disable_unprepare(priv
->clk
);
535 clk_disable_unprepare(priv
->clk_reg
);
540 static int __maybe_unused
omap_rng_suspend(struct device
*dev
)
542 struct omap_rng_dev
*priv
= dev_get_drvdata(dev
);
544 priv
->pdata
->cleanup(priv
);
545 pm_runtime_put_sync(dev
);
550 static int __maybe_unused
omap_rng_resume(struct device
*dev
)
552 struct omap_rng_dev
*priv
= dev_get_drvdata(dev
);
555 ret
= pm_runtime_get_sync(dev
);
557 dev_err(dev
, "Failed to runtime_get device: %d\n", ret
);
558 pm_runtime_put_noidle(dev
);
562 priv
->pdata
->init(priv
);
567 static SIMPLE_DEV_PM_OPS(omap_rng_pm
, omap_rng_suspend
, omap_rng_resume
);
569 static struct platform_driver omap_rng_driver
= {
573 .of_match_table
= of_match_ptr(omap_rng_of_match
),
575 .probe
= omap_rng_probe
,
576 .remove
= omap_rng_remove
,
579 module_platform_driver(omap_rng_driver
);
580 MODULE_ALIAS("platform:omap_rng");
581 MODULE_AUTHOR("Deepak Saxena (and others)");
582 MODULE_LICENSE("GPL");