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/clk.h>
22 #include <linux/err.h>
23 #include <linux/platform_device.h>
24 #include <linux/hw_random.h>
25 #include <linux/delay.h>
29 #define RNG_OUT_REG 0x00 /* Output register */
30 #define RNG_STAT_REG 0x04 /* Status register
32 #define RNG_ALARM_REG 0x24 /* Alarm register
33 [7:0] = ALARM_COUNTER */
34 #define RNG_CONFIG_REG 0x28 /* Configuration register
37 [2:0] = RING1_DELAY */
38 #define RNG_REV_REG 0x3c /* Revision register
40 #define RNG_MASK_REG 0x40 /* Mask and reset register
44 #define RNG_SYSSTATUS 0x44 /* System status
47 static void __iomem
*rng_base
;
48 static struct clk
*rng_ick
;
49 static struct platform_device
*rng_dev
;
51 static inline u32
omap_rng_read_reg(int reg
)
53 return __raw_readl(rng_base
+ reg
);
56 static inline void omap_rng_write_reg(int reg
, u32 val
)
58 __raw_writel(val
, rng_base
+ reg
);
61 static int omap_rng_data_present(struct hwrng
*rng
, int wait
)
65 for (i
= 0; i
< 20; i
++) {
66 data
= omap_rng_read_reg(RNG_STAT_REG
) ? 0 : 1;
69 /* RNG produces data fast enough (2+ MBit/sec, even
70 * during "rngtest" loads, that these delays don't
71 * seem to trigger. We *could* use the RNG IRQ, but
72 * that'd be higher overhead ... so why bother?
79 static int omap_rng_data_read(struct hwrng
*rng
, u32
*data
)
81 *data
= omap_rng_read_reg(RNG_OUT_REG
);
86 static struct hwrng omap_rng_ops
= {
88 .data_present
= omap_rng_data_present
,
89 .data_read
= omap_rng_data_read
,
92 static int __devinit
omap_rng_probe(struct platform_device
*pdev
)
98 * A bit ugly, and it will never actually happen but there can
99 * be only one RNG and this catches any bork
104 if (cpu_is_omap24xx()) {
105 rng_ick
= clk_get(&pdev
->dev
, "ick");
106 if (IS_ERR(rng_ick
)) {
107 dev_err(&pdev
->dev
, "Could not get rng_ick\n");
108 ret
= PTR_ERR(rng_ick
);
114 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
119 if (!request_mem_region(res
->start
, resource_size(res
), pdev
->name
)) {
124 dev_set_drvdata(&pdev
->dev
, res
);
125 rng_base
= ioremap(res
->start
, resource_size(res
));
131 ret
= hwrng_register(&omap_rng_ops
);
135 dev_info(&pdev
->dev
, "OMAP Random Number Generator ver. %02x\n",
136 omap_rng_read_reg(RNG_REV_REG
));
137 omap_rng_write_reg(RNG_MASK_REG
, 0x1);
147 release_mem_region(res
->start
, resource_size(res
));
149 if (cpu_is_omap24xx()) {
150 clk_disable(rng_ick
);
156 static int __exit
omap_rng_remove(struct platform_device
*pdev
)
158 struct resource
*res
= dev_get_drvdata(&pdev
->dev
);
160 hwrng_unregister(&omap_rng_ops
);
162 omap_rng_write_reg(RNG_MASK_REG
, 0x0);
166 if (cpu_is_omap24xx()) {
167 clk_disable(rng_ick
);
171 release_mem_region(res
->start
, resource_size(res
));
179 static int omap_rng_suspend(struct platform_device
*pdev
, pm_message_t message
)
181 omap_rng_write_reg(RNG_MASK_REG
, 0x0);
185 static int omap_rng_resume(struct platform_device
*pdev
)
187 omap_rng_write_reg(RNG_MASK_REG
, 0x1);
193 #define omap_rng_suspend NULL
194 #define omap_rng_resume NULL
198 /* work with hotplug and coldplug */
199 MODULE_ALIAS("platform:omap_rng");
201 static struct platform_driver omap_rng_driver
= {
204 .owner
= THIS_MODULE
,
206 .probe
= omap_rng_probe
,
207 .remove
= __exit_p(omap_rng_remove
),
208 .suspend
= omap_rng_suspend
,
209 .resume
= omap_rng_resume
212 static int __init
omap_rng_init(void)
214 if (!cpu_is_omap16xx() && !cpu_is_omap24xx())
217 return platform_driver_register(&omap_rng_driver
);
220 static void __exit
omap_rng_exit(void)
222 platform_driver_unregister(&omap_rng_driver
);
225 module_init(omap_rng_init
);
226 module_exit(omap_rng_exit
);
228 MODULE_AUTHOR("Deepak Saxena (and others)");
229 MODULE_LICENSE("GPL");