2 * drivers/char/hw_random/omap-rng.c
4 * RNG driver for TI OMAP CPU family
6 * Author: Deepak Saxena <dsaxena@plexity.net>
8 * Copyright 2005 (c) MontaVista Software, Inc.
10 * Mostly based on original driver:
12 * Copyright (C) 2005 Nokia Corporation
13 * Author: Juha Yrjölä <juha.yrjola@nokia.com>
15 * This file is licensed under the terms of the GNU General Public
16 * License version 2. This program is licensed "as is" without any
17 * warranty of any kind, whether express or implied.
21 * - Make status updated be interrupt driven so we don't poll
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/random.h>
28 #include <linux/clk.h>
29 #include <linux/err.h>
30 #include <linux/platform_device.h>
31 #include <linux/hw_random.h>
32 #include <linux/delay.h>
36 #define RNG_OUT_REG 0x00 /* Output register */
37 #define RNG_STAT_REG 0x04 /* Status register
39 #define RNG_ALARM_REG 0x24 /* Alarm register
40 [7:0] = ALARM_COUNTER */
41 #define RNG_CONFIG_REG 0x28 /* Configuration register
44 [2:0] = RING1_DELAY */
45 #define RNG_REV_REG 0x3c /* Revision register
47 #define RNG_MASK_REG 0x40 /* Mask and reset register
51 #define RNG_SYSSTATUS 0x44 /* System status
54 static void __iomem *rng_base;
55 static struct clk *rng_ick;
56 static struct platform_device *rng_dev;
58 static u32 omap_rng_read_reg(int reg)
60 return __raw_readl(rng_base + reg);
63 static void omap_rng_write_reg(int reg, u32 val)
65 __raw_writel(val, rng_base + reg);
68 /* REVISIT: Does the status bit really work on 16xx? */
69 static int omap_rng_data_present(struct hwrng *rng, int wait)
73 for (i = 0; i < 20; i++) {
74 data = omap_rng_read_reg(RNG_STAT_REG) ? 0 : 1;
82 static int omap_rng_data_read(struct hwrng *rng, u32 *data)
84 *data = omap_rng_read_reg(RNG_OUT_REG);
89 static struct hwrng omap_rng_ops = {
91 .data_present = omap_rng_data_present,
92 .data_read = omap_rng_data_read,
95 static int __init omap_rng_probe(struct platform_device *pdev)
97 struct resource *res, *mem;
101 * A bit ugly, and it will never actually happen but there can
102 * be only one RNG and this catches any bork
106 if (cpu_is_omap24xx()) {
107 rng_ick = clk_get(NULL, "rng_ick");
108 if (IS_ERR(rng_ick)) {
109 dev_err(&pdev->dev, "Could not get rng_ick\n");
110 ret = PTR_ERR(rng_ick);
116 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
121 mem = request_mem_region(res->start, res->end - res->start + 1,
126 dev_set_drvdata(&pdev->dev, mem);
127 rng_base = (u32 __iomem *)io_p2v(res->start);
129 ret = hwrng_register(&omap_rng_ops);
131 release_resource(mem);
136 dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n",
137 omap_rng_read_reg(RNG_REV_REG));
138 omap_rng_write_reg(RNG_MASK_REG, 0x1);
145 static int __exit omap_rng_remove(struct platform_device *pdev)
147 struct resource *mem = dev_get_drvdata(&pdev->dev);
149 hwrng_unregister(&omap_rng_ops);
151 omap_rng_write_reg(RNG_MASK_REG, 0x0);
153 if (cpu_is_omap24xx()) {
154 clk_disable(rng_ick);
158 release_resource(mem);
166 static int omap_rng_suspend(struct platform_device *pdev, pm_message_t message)
168 omap_rng_write_reg(RNG_MASK_REG, 0x0);
172 static int omap_rng_resume(struct platform_device *pdev)
174 omap_rng_write_reg(RNG_MASK_REG, 0x1);
180 #define omap_rng_suspend NULL
181 #define omap_rng_resume NULL
186 static struct platform_driver omap_rng_driver = {
189 .owner = THIS_MODULE,
191 .probe = omap_rng_probe,
192 .remove = __exit_p(omap_rng_remove),
193 .suspend = omap_rng_suspend,
194 .resume = omap_rng_resume
197 static int __init omap_rng_init(void)
199 if (!cpu_is_omap16xx() && !cpu_is_omap24xx())
202 return platform_driver_register(&omap_rng_driver);
205 static void __exit omap_rng_exit(void)
207 platform_driver_unregister(&omap_rng_driver);
210 module_init(omap_rng_init);
211 module_exit(omap_rng_exit);
213 MODULE_AUTHOR("Deepak Saxena (and others)");
214 MODULE_LICENSE("GPL");