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��<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>
35 #define RNG_OUT_REG 0x00 /* Output register */
36 #define RNG_STAT_REG 0x04 /* Status register
38 #define RNG_ALARM_REG 0x24 /* Alarm register
39 [7:0] = ALARM_COUNTER */
40 #define RNG_CONFIG_REG 0x28 /* Configuration register
43 [2:0] = RING1_DELAY */
44 #define RNG_REV_REG 0x3c /* Revision register
46 #define RNG_MASK_REG 0x40 /* Mask and reset register
50 #define RNG_SYSSTATUS 0x44 /* System status
53 static void __iomem *rng_base;
54 static struct clk *rng_ick;
55 static struct platform_device *rng_dev;
57 static u32 omap_rng_read_reg(int reg)
59 return __raw_readl(rng_base + reg);
62 static void omap_rng_write_reg(int reg, u32 val)
64 __raw_writel(val, rng_base + reg);
67 /* REVISIT: Does the status bit really work on 16xx? */
68 static int omap_rng_data_present(struct hwrng *rng)
70 return omap_rng_read_reg(RNG_STAT_REG) ? 0 : 1;
73 static int omap_rng_data_read(struct hwrng *rng, u32 *data)
75 *data = omap_rng_read_reg(RNG_OUT_REG);
80 static struct hwrng omap_rng_ops = {
82 .data_present = omap_rng_data_present,
83 .data_read = omap_rng_data_read,
86 static int __init omap_rng_probe(struct platform_device *pdev)
88 struct resource *res, *mem;
92 * A bit ugly, and it will never actually happen but there can
93 * be only one RNG and this catches any bork
97 if (cpu_is_omap24xx()) {
98 rng_ick = clk_get(NULL, "rng_ick");
99 if (IS_ERR(rng_ick)) {
100 dev_err(&pdev->dev, "Could not get rng_ick\n");
101 ret = PTR_ERR(rng_ick);
107 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
112 mem = request_mem_region(res->start, res->end - res->start + 1,
117 dev_set_drvdata(&pdev->dev, mem);
118 rng_base = (u32 __iomem *)io_p2v(res->start);
120 ret = hwrng_register(&omap_rng_ops);
122 release_resource(mem);
127 dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n",
128 omap_rng_read_reg(RNG_REV_REG));
129 omap_rng_write_reg(RNG_MASK_REG, 0x1);
136 static int __exit omap_rng_remove(struct platform_device *pdev)
138 struct resource *mem = dev_get_drvdata(&pdev->dev);
140 hwrng_unregister(&omap_rng_ops);
142 omap_rng_write_reg(RNG_MASK_REG, 0x0);
144 if (cpu_is_omap24xx()) {
145 clk_disable(rng_ick);
149 release_resource(mem);
157 static int omap_rng_suspend(struct platform_device *pdev, pm_message_t message)
159 omap_rng_write_reg(RNG_MASK_REG, 0x0);
163 static int omap_rng_resume(struct platform_device *pdev)
165 omap_rng_write_reg(RNG_MASK_REG, 0x1);
171 #define omap_rng_suspend NULL
172 #define omap_rng_resume NULL
177 static struct platform_driver omap_rng_driver = {
180 .owner = THIS_MODULE,
182 .probe = omap_rng_probe,
183 .remove = __exit_p(omap_rng_remove),
184 .suspend = omap_rng_suspend,
185 .resume = omap_rng_resume
188 static int __init omap_rng_init(void)
190 if (!cpu_is_omap16xx() && !cpu_is_omap24xx())
193 return platform_driver_register(&omap_rng_driver);
196 static void __exit omap_rng_exit(void)
198 platform_driver_unregister(&omap_rng_driver);
201 module_init(omap_rng_init);
202 module_exit(omap_rng_exit);
204 MODULE_AUTHOR("Deepak Saxena (and others)");
205 MODULE_LICENSE("GPL");