x86: cpa: move clflush_cache_range()
[wrt350n-kernel.git] / drivers / char / hw_random / omap-rng.c
blob7e319951fa4111fec37688f052596a29cd27c211
1 /*
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.
19 * TODO:
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>
34 #include <asm/io.h>
36 #define RNG_OUT_REG 0x00 /* Output register */
37 #define RNG_STAT_REG 0x04 /* Status register
38 [0] = STAT_BUSY */
39 #define RNG_ALARM_REG 0x24 /* Alarm register
40 [7:0] = ALARM_COUNTER */
41 #define RNG_CONFIG_REG 0x28 /* Configuration register
42 [11:6] = RESET_COUNT
43 [5:3] = RING2_DELAY
44 [2:0] = RING1_DELAY */
45 #define RNG_REV_REG 0x3c /* Revision register
46 [7:0] = REV_NB */
47 #define RNG_MASK_REG 0x40 /* Mask and reset register
48 [2] = IT_EN
49 [1] = SOFTRESET
50 [0] = AUTOIDLE */
51 #define RNG_SYSSTATUS 0x44 /* System status
52 [0] = RESETDONE */
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)
71 int data, i;
73 for (i = 0; i < 20; i++) {
74 data = omap_rng_read_reg(RNG_STAT_REG) ? 0 : 1;
75 if (data || !wait)
76 break;
77 udelay(10);
79 return data;
82 static int omap_rng_data_read(struct hwrng *rng, u32 *data)
84 *data = omap_rng_read_reg(RNG_OUT_REG);
86 return 4;
89 static struct hwrng omap_rng_ops = {
90 .name = "omap",
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;
98 int ret;
101 * A bit ugly, and it will never actually happen but there can
102 * be only one RNG and this catches any bork
104 BUG_ON(rng_dev);
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);
111 return ret;
112 } else
113 clk_enable(rng_ick);
116 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
118 if (!res)
119 return -ENOENT;
121 mem = request_mem_region(res->start, res->end - res->start + 1,
122 pdev->name);
123 if (mem == NULL)
124 return -EBUSY;
126 dev_set_drvdata(&pdev->dev, mem);
127 rng_base = (u32 __iomem *)io_p2v(res->start);
129 ret = hwrng_register(&omap_rng_ops);
130 if (ret) {
131 release_resource(mem);
132 rng_base = NULL;
133 return ret;
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);
140 rng_dev = pdev;
142 return 0;
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);
155 clk_put(rng_ick);
158 release_resource(mem);
159 rng_base = NULL;
161 return 0;
164 #ifdef CONFIG_PM
166 static int omap_rng_suspend(struct platform_device *pdev, pm_message_t message)
168 omap_rng_write_reg(RNG_MASK_REG, 0x0);
169 return 0;
172 static int omap_rng_resume(struct platform_device *pdev)
174 omap_rng_write_reg(RNG_MASK_REG, 0x1);
175 return 0;
178 #else
180 #define omap_rng_suspend NULL
181 #define omap_rng_resume NULL
183 #endif
186 static struct platform_driver omap_rng_driver = {
187 .driver = {
188 .name = "omap_rng",
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())
200 return -ENODEV;
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");