blob: e13dd1892bfdf95bb8c8e731528c12eab1a4fb4d [file] [log] [blame]
Michael Bueschebc915a2006-06-26 00:25:03 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * drivers/char/hw_random/omap-rng.c
Michael Bueschebc915a2006-06-26 00:25:03 -07003 *
4 * RNG driver for TI OMAP CPU family
5 *
6 * Author: Deepak Saxena <dsaxena@plexity.net>
7 *
8 * Copyright 2005 (c) MontaVista Software, Inc.
9 *
10 * Mostly based on original driver:
11 *
12 * Copyright (C) 2005 Nokia Corporation
13 * Author: Juha Yrj��<juha.yrjola@nokia.com>
14 *
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.
18 *
19 * TODO:
20 *
21 * - Make status updated be interrupt driven so we don't poll
22 *
23 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/random.h>
David Brownellaf2bc7d2006-08-05 12:14:04 -070028#include <linux/clk.h>
Michael Bueschebc915a2006-06-26 00:25:03 -070029#include <linux/err.h>
David Brownellaf2bc7d2006-08-05 12:14:04 -070030#include <linux/platform_device.h>
Michael Bueschebc915a2006-06-26 00:25:03 -070031#include <linux/hw_random.h>
32
33#include <asm/io.h>
Michael Bueschebc915a2006-06-26 00:25:03 -070034
35#define RNG_OUT_REG 0x00 /* Output register */
36#define RNG_STAT_REG 0x04 /* Status register
37 [0] = STAT_BUSY */
38#define RNG_ALARM_REG 0x24 /* Alarm register
39 [7:0] = ALARM_COUNTER */
40#define RNG_CONFIG_REG 0x28 /* Configuration register
41 [11:6] = RESET_COUNT
42 [5:3] = RING2_DELAY
43 [2:0] = RING1_DELAY */
44#define RNG_REV_REG 0x3c /* Revision register
45 [7:0] = REV_NB */
46#define RNG_MASK_REG 0x40 /* Mask and reset register
47 [2] = IT_EN
48 [1] = SOFTRESET
49 [0] = AUTOIDLE */
50#define RNG_SYSSTATUS 0x44 /* System status
51 [0] = RESETDONE */
52
53static void __iomem *rng_base;
54static struct clk *rng_ick;
David Brownellaf2bc7d2006-08-05 12:14:04 -070055static struct platform_device *rng_dev;
Michael Bueschebc915a2006-06-26 00:25:03 -070056
57static u32 omap_rng_read_reg(int reg)
58{
59 return __raw_readl(rng_base + reg);
60}
61
62static void omap_rng_write_reg(int reg, u32 val)
63{
64 __raw_writel(val, rng_base + reg);
65}
66
67/* REVISIT: Does the status bit really work on 16xx? */
68static int omap_rng_data_present(struct hwrng *rng)
69{
70 return omap_rng_read_reg(RNG_STAT_REG) ? 0 : 1;
71}
72
73static int omap_rng_data_read(struct hwrng *rng, u32 *data)
74{
75 *data = omap_rng_read_reg(RNG_OUT_REG);
76
77 return 4;
78}
79
80static struct hwrng omap_rng_ops = {
81 .name = "omap",
82 .data_present = omap_rng_data_present,
83 .data_read = omap_rng_data_read,
84};
85
David Brownellaf2bc7d2006-08-05 12:14:04 -070086static int __init omap_rng_probe(struct platform_device *pdev)
Michael Bueschebc915a2006-06-26 00:25:03 -070087{
Michael Bueschebc915a2006-06-26 00:25:03 -070088 struct resource *res, *mem;
89 int ret;
90
91 /*
92 * A bit ugly, and it will never actually happen but there can
93 * be only one RNG and this catches any bork
94 */
95 BUG_ON(rng_dev);
96
David Brownellaf2bc7d2006-08-05 12:14:04 -070097 if (cpu_is_omap24xx()) {
Michael Bueschebc915a2006-06-26 00:25:03 -070098 rng_ick = clk_get(NULL, "rng_ick");
99 if (IS_ERR(rng_ick)) {
David Brownellaf2bc7d2006-08-05 12:14:04 -0700100 dev_err(&pdev->dev, "Could not get rng_ick\n");
Michael Bueschebc915a2006-06-26 00:25:03 -0700101 ret = PTR_ERR(rng_ick);
102 return ret;
David Brownellaf2bc7d2006-08-05 12:14:04 -0700103 } else
104 clk_enable(rng_ick);
Michael Bueschebc915a2006-06-26 00:25:03 -0700105 }
106
107 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
108
109 if (!res)
110 return -ENOENT;
111
112 mem = request_mem_region(res->start, res->end - res->start + 1,
113 pdev->name);
114 if (mem == NULL)
115 return -EBUSY;
116
David Brownellaf2bc7d2006-08-05 12:14:04 -0700117 dev_set_drvdata(&pdev->dev, mem);
Michael Bueschebc915a2006-06-26 00:25:03 -0700118 rng_base = (u32 __iomem *)io_p2v(res->start);
119
120 ret = hwrng_register(&omap_rng_ops);
121 if (ret) {
122 release_resource(mem);
123 rng_base = NULL;
124 return ret;
125 }
126
David Brownellaf2bc7d2006-08-05 12:14:04 -0700127 dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n",
Michael Bueschebc915a2006-06-26 00:25:03 -0700128 omap_rng_read_reg(RNG_REV_REG));
129 omap_rng_write_reg(RNG_MASK_REG, 0x1);
130
David Brownellaf2bc7d2006-08-05 12:14:04 -0700131 rng_dev = pdev;
Michael Bueschebc915a2006-06-26 00:25:03 -0700132
133 return 0;
134}
135
David Brownellaf2bc7d2006-08-05 12:14:04 -0700136static int __exit omap_rng_remove(struct platform_device *pdev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700137{
David Brownellaf2bc7d2006-08-05 12:14:04 -0700138 struct resource *mem = dev_get_drvdata(&pdev->dev);
Michael Bueschebc915a2006-06-26 00:25:03 -0700139
140 hwrng_unregister(&omap_rng_ops);
141
142 omap_rng_write_reg(RNG_MASK_REG, 0x0);
143
144 if (cpu_is_omap24xx()) {
David Brownellaf2bc7d2006-08-05 12:14:04 -0700145 clk_disable(rng_ick);
Michael Bueschebc915a2006-06-26 00:25:03 -0700146 clk_put(rng_ick);
147 }
148
149 release_resource(mem);
150 rng_base = NULL;
151
152 return 0;
153}
154
155#ifdef CONFIG_PM
156
David Brownellaf2bc7d2006-08-05 12:14:04 -0700157static int omap_rng_suspend(struct platform_device *pdev, pm_message_t message)
Michael Bueschebc915a2006-06-26 00:25:03 -0700158{
159 omap_rng_write_reg(RNG_MASK_REG, 0x0);
Michael Bueschebc915a2006-06-26 00:25:03 -0700160 return 0;
161}
162
David Brownellaf2bc7d2006-08-05 12:14:04 -0700163static int omap_rng_resume(struct platform_device *pdev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700164{
165 omap_rng_write_reg(RNG_MASK_REG, 0x1);
David Brownellaf2bc7d2006-08-05 12:14:04 -0700166 return 0;
Michael Bueschebc915a2006-06-26 00:25:03 -0700167}
168
169#else
170
171#define omap_rng_suspend NULL
172#define omap_rng_resume NULL
173
174#endif
175
176
David Brownellaf2bc7d2006-08-05 12:14:04 -0700177static struct platform_driver omap_rng_driver = {
178 .driver = {
179 .name = "omap_rng",
180 .owner = THIS_MODULE,
181 },
Michael Bueschebc915a2006-06-26 00:25:03 -0700182 .probe = omap_rng_probe,
183 .remove = __exit_p(omap_rng_remove),
184 .suspend = omap_rng_suspend,
185 .resume = omap_rng_resume
186};
187
188static int __init omap_rng_init(void)
189{
190 if (!cpu_is_omap16xx() && !cpu_is_omap24xx())
191 return -ENODEV;
192
David Brownellaf2bc7d2006-08-05 12:14:04 -0700193 return platform_driver_register(&omap_rng_driver);
Michael Bueschebc915a2006-06-26 00:25:03 -0700194}
195
196static void __exit omap_rng_exit(void)
197{
David Brownellaf2bc7d2006-08-05 12:14:04 -0700198 platform_driver_unregister(&omap_rng_driver);
Michael Bueschebc915a2006-06-26 00:25:03 -0700199}
200
201module_init(omap_rng_init);
202module_exit(omap_rng_exit);
203
204MODULE_AUTHOR("Deepak Saxena (and others)");
205MODULE_LICENSE("GPL");