blob: a07a5caa599cde1f0acf41c6dc4bbd32c9d92f36 [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
Tony Lindgren2c799ce2012-02-24 10:34:35 -080029#include <plat/cpu.h>
30
Michael Bueschebc915a2006-06-26 00:25:03 -070031#define RNG_OUT_REG 0x00 /* Output register */
32#define RNG_STAT_REG 0x04 /* Status register
33 [0] = STAT_BUSY */
34#define RNG_ALARM_REG 0x24 /* Alarm register
35 [7:0] = ALARM_COUNTER */
36#define RNG_CONFIG_REG 0x28 /* Configuration register
37 [11:6] = RESET_COUNT
38 [5:3] = RING2_DELAY
39 [2:0] = RING1_DELAY */
40#define RNG_REV_REG 0x3c /* Revision register
41 [7:0] = REV_NB */
42#define RNG_MASK_REG 0x40 /* Mask and reset register
43 [2] = IT_EN
44 [1] = SOFTRESET
45 [0] = AUTOIDLE */
46#define RNG_SYSSTATUS 0x44 /* System status
47 [0] = RESETDONE */
48
49static void __iomem *rng_base;
50static struct clk *rng_ick;
David Brownellaf2bc7d2006-08-05 12:14:04 -070051static struct platform_device *rng_dev;
Michael Bueschebc915a2006-06-26 00:25:03 -070052
David Brownellc49a7f12008-04-16 19:24:42 +080053static inline u32 omap_rng_read_reg(int reg)
Michael Bueschebc915a2006-06-26 00:25:03 -070054{
55 return __raw_readl(rng_base + reg);
56}
57
David Brownellc49a7f12008-04-16 19:24:42 +080058static inline void omap_rng_write_reg(int reg, u32 val)
Michael Bueschebc915a2006-06-26 00:25:03 -070059{
60 __raw_writel(val, rng_base + reg);
61}
62
Patrick McHardy984e9762007-11-21 12:24:45 +080063static int omap_rng_data_present(struct hwrng *rng, int wait)
Michael Bueschebc915a2006-06-26 00:25:03 -070064{
Patrick McHardy984e9762007-11-21 12:24:45 +080065 int data, i;
66
67 for (i = 0; i < 20; i++) {
68 data = omap_rng_read_reg(RNG_STAT_REG) ? 0 : 1;
69 if (data || !wait)
70 break;
David Brownellc49a7f12008-04-16 19:24:42 +080071 /* RNG produces data fast enough (2+ MBit/sec, even
72 * during "rngtest" loads, that these delays don't
73 * seem to trigger. We *could* use the RNG IRQ, but
74 * that'd be higher overhead ... so why bother?
75 */
Patrick McHardy984e9762007-11-21 12:24:45 +080076 udelay(10);
77 }
78 return data;
Michael Bueschebc915a2006-06-26 00:25:03 -070079}
80
81static int omap_rng_data_read(struct hwrng *rng, u32 *data)
82{
83 *data = omap_rng_read_reg(RNG_OUT_REG);
84
85 return 4;
86}
87
88static struct hwrng omap_rng_ops = {
89 .name = "omap",
90 .data_present = omap_rng_data_present,
91 .data_read = omap_rng_data_read,
92};
93
Uwe Kleine-König9f171ad2009-03-29 15:47:06 +080094static int __devinit omap_rng_probe(struct platform_device *pdev)
Michael Bueschebc915a2006-06-26 00:25:03 -070095{
Julia Lawallc6527592011-02-16 13:05:54 +110096 struct resource *res;
Michael Bueschebc915a2006-06-26 00:25:03 -070097 int ret;
98
99 /*
100 * A bit ugly, and it will never actually happen but there can
101 * be only one RNG and this catches any bork
102 */
David Brownellc49a7f12008-04-16 19:24:42 +0800103 if (rng_dev)
104 return -EBUSY;
Michael Bueschebc915a2006-06-26 00:25:03 -0700105
David Brownellaf2bc7d2006-08-05 12:14:04 -0700106 if (cpu_is_omap24xx()) {
Russell Kingeeec7c82009-01-19 20:58:56 +0000107 rng_ick = clk_get(&pdev->dev, "ick");
Michael Bueschebc915a2006-06-26 00:25:03 -0700108 if (IS_ERR(rng_ick)) {
David Brownellaf2bc7d2006-08-05 12:14:04 -0700109 dev_err(&pdev->dev, "Could not get rng_ick\n");
Michael Bueschebc915a2006-06-26 00:25:03 -0700110 ret = PTR_ERR(rng_ick);
111 return ret;
David Brownellaf2bc7d2006-08-05 12:14:04 -0700112 } else
113 clk_enable(rng_ick);
Michael Bueschebc915a2006-06-26 00:25:03 -0700114 }
115
116 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
117
Julia Lawall6ba1a312011-06-08 20:59:38 +0800118 if (!res) {
119 ret = -ENOENT;
120 goto err_region;
121 }
Michael Bueschebc915a2006-06-26 00:25:03 -0700122
Julia Lawallc6527592011-02-16 13:05:54 +1100123 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
Russell King55c381e2008-09-04 14:07:22 +0100124 ret = -EBUSY;
125 goto err_region;
126 }
Michael Bueschebc915a2006-06-26 00:25:03 -0700127
Julia Lawallc6527592011-02-16 13:05:54 +1100128 dev_set_drvdata(&pdev->dev, res);
Tobias Klauser0fd92a12009-09-24 16:23:18 -0700129 rng_base = ioremap(res->start, resource_size(res));
Russell King55c381e2008-09-04 14:07:22 +0100130 if (!rng_base) {
131 ret = -ENOMEM;
132 goto err_ioremap;
133 }
Michael Bueschebc915a2006-06-26 00:25:03 -0700134
135 ret = hwrng_register(&omap_rng_ops);
Russell King55c381e2008-09-04 14:07:22 +0100136 if (ret)
137 goto err_register;
Michael Bueschebc915a2006-06-26 00:25:03 -0700138
David Brownellaf2bc7d2006-08-05 12:14:04 -0700139 dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n",
Michael Bueschebc915a2006-06-26 00:25:03 -0700140 omap_rng_read_reg(RNG_REV_REG));
141 omap_rng_write_reg(RNG_MASK_REG, 0x1);
142
David Brownellaf2bc7d2006-08-05 12:14:04 -0700143 rng_dev = pdev;
Michael Bueschebc915a2006-06-26 00:25:03 -0700144
145 return 0;
Russell King55c381e2008-09-04 14:07:22 +0100146
147err_register:
148 iounmap(rng_base);
149 rng_base = NULL;
150err_ioremap:
Julia Lawallc6527592011-02-16 13:05:54 +1100151 release_mem_region(res->start, resource_size(res));
Russell King55c381e2008-09-04 14:07:22 +0100152err_region:
153 if (cpu_is_omap24xx()) {
154 clk_disable(rng_ick);
155 clk_put(rng_ick);
156 }
157 return ret;
Michael Bueschebc915a2006-06-26 00:25:03 -0700158}
159
David Brownellaf2bc7d2006-08-05 12:14:04 -0700160static int __exit omap_rng_remove(struct platform_device *pdev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700161{
Julia Lawallc6527592011-02-16 13:05:54 +1100162 struct resource *res = dev_get_drvdata(&pdev->dev);
Michael Bueschebc915a2006-06-26 00:25:03 -0700163
164 hwrng_unregister(&omap_rng_ops);
165
166 omap_rng_write_reg(RNG_MASK_REG, 0x0);
167
Russell King55c381e2008-09-04 14:07:22 +0100168 iounmap(rng_base);
169
Michael Bueschebc915a2006-06-26 00:25:03 -0700170 if (cpu_is_omap24xx()) {
David Brownellaf2bc7d2006-08-05 12:14:04 -0700171 clk_disable(rng_ick);
Michael Bueschebc915a2006-06-26 00:25:03 -0700172 clk_put(rng_ick);
173 }
174
Julia Lawallc6527592011-02-16 13:05:54 +1100175 release_mem_region(res->start, resource_size(res));
Michael Bueschebc915a2006-06-26 00:25:03 -0700176 rng_base = NULL;
177
178 return 0;
179}
180
181#ifdef CONFIG_PM
182
David Brownellaf2bc7d2006-08-05 12:14:04 -0700183static int omap_rng_suspend(struct platform_device *pdev, pm_message_t message)
Michael Bueschebc915a2006-06-26 00:25:03 -0700184{
185 omap_rng_write_reg(RNG_MASK_REG, 0x0);
Michael Bueschebc915a2006-06-26 00:25:03 -0700186 return 0;
187}
188
David Brownellaf2bc7d2006-08-05 12:14:04 -0700189static int omap_rng_resume(struct platform_device *pdev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700190{
191 omap_rng_write_reg(RNG_MASK_REG, 0x1);
David Brownellaf2bc7d2006-08-05 12:14:04 -0700192 return 0;
Michael Bueschebc915a2006-06-26 00:25:03 -0700193}
194
195#else
196
197#define omap_rng_suspend NULL
198#define omap_rng_resume NULL
199
200#endif
201
David Brownellc49a7f12008-04-16 19:24:42 +0800202/* work with hotplug and coldplug */
203MODULE_ALIAS("platform:omap_rng");
Michael Bueschebc915a2006-06-26 00:25:03 -0700204
David Brownellaf2bc7d2006-08-05 12:14:04 -0700205static struct platform_driver omap_rng_driver = {
206 .driver = {
207 .name = "omap_rng",
208 .owner = THIS_MODULE,
209 },
Michael Bueschebc915a2006-06-26 00:25:03 -0700210 .probe = omap_rng_probe,
211 .remove = __exit_p(omap_rng_remove),
212 .suspend = omap_rng_suspend,
213 .resume = omap_rng_resume
214};
215
216static int __init omap_rng_init(void)
217{
218 if (!cpu_is_omap16xx() && !cpu_is_omap24xx())
219 return -ENODEV;
220
David Brownellaf2bc7d2006-08-05 12:14:04 -0700221 return platform_driver_register(&omap_rng_driver);
Michael Bueschebc915a2006-06-26 00:25:03 -0700222}
223
224static void __exit omap_rng_exit(void)
225{
David Brownellaf2bc7d2006-08-05 12:14:04 -0700226 platform_driver_unregister(&omap_rng_driver);
Michael Bueschebc915a2006-06-26 00:25:03 -0700227}
228
229module_init(omap_rng_init);
230module_exit(omap_rng_exit);
231
232MODULE_AUTHOR("Deepak Saxena (and others)");
233MODULE_LICENSE("GPL");