blob: d4e7dca06e4fa2c6e7bf0d26781ac5627abb97cc [file] [log] [blame]
Michael Bueschebc915a2006-06-26 00:25:03 -07001/*
David Brownellc49a7f12008-04-16 19:24:42 +08002 * omap-rng.c - RNG driver for TI OMAP CPU family
Michael Bueschebc915a2006-06-26 00:25:03 -07003 *
4 * Author: Deepak Saxena <dsaxena@plexity.net>
5 *
6 * Copyright 2005 (c) MontaVista Software, Inc.
7 *
8 * Mostly based on original driver:
9 *
10 * Copyright (C) 2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +020011 * Author: Juha Yrjölä <juha.yrjola@nokia.com>
Michael Bueschebc915a2006-06-26 00:25:03 -070012 *
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.
Michael Bueschebc915a2006-06-26 00:25:03 -070016 */
17
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/random.h>
David Brownellaf2bc7d2006-08-05 12:14:04 -070021#include <linux/clk.h>
Michael Bueschebc915a2006-06-26 00:25:03 -070022#include <linux/err.h>
David Brownellaf2bc7d2006-08-05 12:14:04 -070023#include <linux/platform_device.h>
Michael Bueschebc915a2006-06-26 00:25:03 -070024#include <linux/hw_random.h>
Patrick McHardy984e9762007-11-21 12:24:45 +080025#include <linux/delay.h>
Michael Bueschebc915a2006-06-26 00:25:03 -070026
27#include <asm/io.h>
Michael Bueschebc915a2006-06-26 00:25:03 -070028
29#define RNG_OUT_REG 0x00 /* Output register */
30#define RNG_STAT_REG 0x04 /* Status register
31 [0] = STAT_BUSY */
32#define RNG_ALARM_REG 0x24 /* Alarm register
33 [7:0] = ALARM_COUNTER */
34#define RNG_CONFIG_REG 0x28 /* Configuration register
35 [11:6] = RESET_COUNT
36 [5:3] = RING2_DELAY
37 [2:0] = RING1_DELAY */
38#define RNG_REV_REG 0x3c /* Revision register
39 [7:0] = REV_NB */
40#define RNG_MASK_REG 0x40 /* Mask and reset register
41 [2] = IT_EN
42 [1] = SOFTRESET
43 [0] = AUTOIDLE */
44#define RNG_SYSSTATUS 0x44 /* System status
45 [0] = RESETDONE */
46
47static void __iomem *rng_base;
48static struct clk *rng_ick;
David Brownellaf2bc7d2006-08-05 12:14:04 -070049static struct platform_device *rng_dev;
Michael Bueschebc915a2006-06-26 00:25:03 -070050
David Brownellc49a7f12008-04-16 19:24:42 +080051static inline u32 omap_rng_read_reg(int reg)
Michael Bueschebc915a2006-06-26 00:25:03 -070052{
53 return __raw_readl(rng_base + reg);
54}
55
David Brownellc49a7f12008-04-16 19:24:42 +080056static inline void omap_rng_write_reg(int reg, u32 val)
Michael Bueschebc915a2006-06-26 00:25:03 -070057{
58 __raw_writel(val, rng_base + reg);
59}
60
Patrick McHardy984e9762007-11-21 12:24:45 +080061static int omap_rng_data_present(struct hwrng *rng, int wait)
Michael Bueschebc915a2006-06-26 00:25:03 -070062{
Patrick McHardy984e9762007-11-21 12:24:45 +080063 int data, i;
64
65 for (i = 0; i < 20; i++) {
66 data = omap_rng_read_reg(RNG_STAT_REG) ? 0 : 1;
67 if (data || !wait)
68 break;
David Brownellc49a7f12008-04-16 19:24:42 +080069 /* 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?
73 */
Patrick McHardy984e9762007-11-21 12:24:45 +080074 udelay(10);
75 }
76 return data;
Michael Bueschebc915a2006-06-26 00:25:03 -070077}
78
79static int omap_rng_data_read(struct hwrng *rng, u32 *data)
80{
81 *data = omap_rng_read_reg(RNG_OUT_REG);
82
83 return 4;
84}
85
86static struct hwrng omap_rng_ops = {
87 .name = "omap",
88 .data_present = omap_rng_data_present,
89 .data_read = omap_rng_data_read,
90};
91
David Brownellaf2bc7d2006-08-05 12:14:04 -070092static int __init omap_rng_probe(struct platform_device *pdev)
Michael Bueschebc915a2006-06-26 00:25:03 -070093{
Michael Bueschebc915a2006-06-26 00:25:03 -070094 struct resource *res, *mem;
95 int ret;
96
97 /*
98 * A bit ugly, and it will never actually happen but there can
99 * be only one RNG and this catches any bork
100 */
David Brownellc49a7f12008-04-16 19:24:42 +0800101 if (rng_dev)
102 return -EBUSY;
Michael Bueschebc915a2006-06-26 00:25:03 -0700103
David Brownellaf2bc7d2006-08-05 12:14:04 -0700104 if (cpu_is_omap24xx()) {
Michael Bueschebc915a2006-06-26 00:25:03 -0700105 rng_ick = clk_get(NULL, "rng_ick");
106 if (IS_ERR(rng_ick)) {
David Brownellaf2bc7d2006-08-05 12:14:04 -0700107 dev_err(&pdev->dev, "Could not get rng_ick\n");
Michael Bueschebc915a2006-06-26 00:25:03 -0700108 ret = PTR_ERR(rng_ick);
109 return ret;
David Brownellaf2bc7d2006-08-05 12:14:04 -0700110 } else
111 clk_enable(rng_ick);
Michael Bueschebc915a2006-06-26 00:25:03 -0700112 }
113
114 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
115
116 if (!res)
117 return -ENOENT;
118
119 mem = request_mem_region(res->start, res->end - res->start + 1,
120 pdev->name);
Russell King55c381e2008-09-04 14:07:22 +0100121 if (mem == NULL) {
122 ret = -EBUSY;
123 goto err_region;
124 }
Michael Bueschebc915a2006-06-26 00:25:03 -0700125
David Brownellaf2bc7d2006-08-05 12:14:04 -0700126 dev_set_drvdata(&pdev->dev, mem);
Russell King55c381e2008-09-04 14:07:22 +0100127 rng_base = ioremap(res->start, res->end - res->start + 1);
128 if (!rng_base) {
129 ret = -ENOMEM;
130 goto err_ioremap;
131 }
Michael Bueschebc915a2006-06-26 00:25:03 -0700132
133 ret = hwrng_register(&omap_rng_ops);
Russell King55c381e2008-09-04 14:07:22 +0100134 if (ret)
135 goto err_register;
Michael Bueschebc915a2006-06-26 00:25:03 -0700136
David Brownellaf2bc7d2006-08-05 12:14:04 -0700137 dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n",
Michael Bueschebc915a2006-06-26 00:25:03 -0700138 omap_rng_read_reg(RNG_REV_REG));
139 omap_rng_write_reg(RNG_MASK_REG, 0x1);
140
David Brownellaf2bc7d2006-08-05 12:14:04 -0700141 rng_dev = pdev;
Michael Bueschebc915a2006-06-26 00:25:03 -0700142
143 return 0;
Russell King55c381e2008-09-04 14:07:22 +0100144
145err_register:
146 iounmap(rng_base);
147 rng_base = NULL;
148err_ioremap:
149 release_resource(mem);
150err_region:
151 if (cpu_is_omap24xx()) {
152 clk_disable(rng_ick);
153 clk_put(rng_ick);
154 }
155 return ret;
Michael Bueschebc915a2006-06-26 00:25:03 -0700156}
157
David Brownellaf2bc7d2006-08-05 12:14:04 -0700158static int __exit omap_rng_remove(struct platform_device *pdev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700159{
David Brownellaf2bc7d2006-08-05 12:14:04 -0700160 struct resource *mem = dev_get_drvdata(&pdev->dev);
Michael Bueschebc915a2006-06-26 00:25:03 -0700161
162 hwrng_unregister(&omap_rng_ops);
163
164 omap_rng_write_reg(RNG_MASK_REG, 0x0);
165
Russell King55c381e2008-09-04 14:07:22 +0100166 iounmap(rng_base);
167
Michael Bueschebc915a2006-06-26 00:25:03 -0700168 if (cpu_is_omap24xx()) {
David Brownellaf2bc7d2006-08-05 12:14:04 -0700169 clk_disable(rng_ick);
Michael Bueschebc915a2006-06-26 00:25:03 -0700170 clk_put(rng_ick);
171 }
172
173 release_resource(mem);
174 rng_base = NULL;
175
176 return 0;
177}
178
179#ifdef CONFIG_PM
180
David Brownellaf2bc7d2006-08-05 12:14:04 -0700181static int omap_rng_suspend(struct platform_device *pdev, pm_message_t message)
Michael Bueschebc915a2006-06-26 00:25:03 -0700182{
183 omap_rng_write_reg(RNG_MASK_REG, 0x0);
Michael Bueschebc915a2006-06-26 00:25:03 -0700184 return 0;
185}
186
David Brownellaf2bc7d2006-08-05 12:14:04 -0700187static int omap_rng_resume(struct platform_device *pdev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700188{
189 omap_rng_write_reg(RNG_MASK_REG, 0x1);
David Brownellaf2bc7d2006-08-05 12:14:04 -0700190 return 0;
Michael Bueschebc915a2006-06-26 00:25:03 -0700191}
192
193#else
194
195#define omap_rng_suspend NULL
196#define omap_rng_resume NULL
197
198#endif
199
David Brownellc49a7f12008-04-16 19:24:42 +0800200/* work with hotplug and coldplug */
201MODULE_ALIAS("platform:omap_rng");
Michael Bueschebc915a2006-06-26 00:25:03 -0700202
David Brownellaf2bc7d2006-08-05 12:14:04 -0700203static struct platform_driver omap_rng_driver = {
204 .driver = {
205 .name = "omap_rng",
206 .owner = THIS_MODULE,
207 },
Michael Bueschebc915a2006-06-26 00:25:03 -0700208 .probe = omap_rng_probe,
209 .remove = __exit_p(omap_rng_remove),
210 .suspend = omap_rng_suspend,
211 .resume = omap_rng_resume
212};
213
214static int __init omap_rng_init(void)
215{
216 if (!cpu_is_omap16xx() && !cpu_is_omap24xx())
217 return -ENODEV;
218
David Brownellaf2bc7d2006-08-05 12:14:04 -0700219 return platform_driver_register(&omap_rng_driver);
Michael Bueschebc915a2006-06-26 00:25:03 -0700220}
221
222static void __exit omap_rng_exit(void)
223{
David Brownellaf2bc7d2006-08-05 12:14:04 -0700224 platform_driver_unregister(&omap_rng_driver);
Michael Bueschebc915a2006-06-26 00:25:03 -0700225}
226
227module_init(omap_rng_init);
228module_exit(omap_rng_exit);
229
230MODULE_AUTHOR("Deepak Saxena (and others)");
231MODULE_LICENSE("GPL");