blob: 1412565c01afb213407fea17469ce0037f301610 [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 Lawall945478a2012-04-19 19:19:08 +0200118 rng_base = devm_request_and_ioremap(&pdev->dev, res);
Russell King55c381e2008-09-04 14:07:22 +0100119 if (!rng_base) {
120 ret = -ENOMEM;
121 goto err_ioremap;
122 }
Julia Lawall945478a2012-04-19 19:19:08 +0200123 dev_set_drvdata(&pdev->dev, res);
Michael Bueschebc915a2006-06-26 00:25:03 -0700124
125 ret = hwrng_register(&omap_rng_ops);
Russell King55c381e2008-09-04 14:07:22 +0100126 if (ret)
127 goto err_register;
Michael Bueschebc915a2006-06-26 00:25:03 -0700128
David Brownellaf2bc7d2006-08-05 12:14:04 -0700129 dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n",
Michael Bueschebc915a2006-06-26 00:25:03 -0700130 omap_rng_read_reg(RNG_REV_REG));
131 omap_rng_write_reg(RNG_MASK_REG, 0x1);
132
David Brownellaf2bc7d2006-08-05 12:14:04 -0700133 rng_dev = pdev;
Michael Bueschebc915a2006-06-26 00:25:03 -0700134
135 return 0;
Russell King55c381e2008-09-04 14:07:22 +0100136
137err_register:
Russell King55c381e2008-09-04 14:07:22 +0100138 rng_base = NULL;
139err_ioremap:
Russell King55c381e2008-09-04 14:07:22 +0100140 if (cpu_is_omap24xx()) {
141 clk_disable(rng_ick);
142 clk_put(rng_ick);
143 }
144 return ret;
Michael Bueschebc915a2006-06-26 00:25:03 -0700145}
146
David Brownellaf2bc7d2006-08-05 12:14:04 -0700147static int __exit omap_rng_remove(struct platform_device *pdev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700148{
Michael Bueschebc915a2006-06-26 00:25:03 -0700149 hwrng_unregister(&omap_rng_ops);
150
151 omap_rng_write_reg(RNG_MASK_REG, 0x0);
152
153 if (cpu_is_omap24xx()) {
David Brownellaf2bc7d2006-08-05 12:14:04 -0700154 clk_disable(rng_ick);
Michael Bueschebc915a2006-06-26 00:25:03 -0700155 clk_put(rng_ick);
156 }
157
Michael Bueschebc915a2006-06-26 00:25:03 -0700158 rng_base = NULL;
159
160 return 0;
161}
162
163#ifdef CONFIG_PM
164
David Brownellaf2bc7d2006-08-05 12:14:04 -0700165static int omap_rng_suspend(struct platform_device *pdev, pm_message_t message)
Michael Bueschebc915a2006-06-26 00:25:03 -0700166{
167 omap_rng_write_reg(RNG_MASK_REG, 0x0);
Michael Bueschebc915a2006-06-26 00:25:03 -0700168 return 0;
169}
170
David Brownellaf2bc7d2006-08-05 12:14:04 -0700171static int omap_rng_resume(struct platform_device *pdev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700172{
173 omap_rng_write_reg(RNG_MASK_REG, 0x1);
David Brownellaf2bc7d2006-08-05 12:14:04 -0700174 return 0;
Michael Bueschebc915a2006-06-26 00:25:03 -0700175}
176
177#else
178
179#define omap_rng_suspend NULL
180#define omap_rng_resume NULL
181
182#endif
183
David Brownellc49a7f12008-04-16 19:24:42 +0800184/* work with hotplug and coldplug */
185MODULE_ALIAS("platform:omap_rng");
Michael Bueschebc915a2006-06-26 00:25:03 -0700186
David Brownellaf2bc7d2006-08-05 12:14:04 -0700187static struct platform_driver omap_rng_driver = {
188 .driver = {
189 .name = "omap_rng",
190 .owner = THIS_MODULE,
191 },
Michael Bueschebc915a2006-06-26 00:25:03 -0700192 .probe = omap_rng_probe,
193 .remove = __exit_p(omap_rng_remove),
194 .suspend = omap_rng_suspend,
195 .resume = omap_rng_resume
196};
197
198static int __init omap_rng_init(void)
199{
200 if (!cpu_is_omap16xx() && !cpu_is_omap24xx())
201 return -ENODEV;
202
David Brownellaf2bc7d2006-08-05 12:14:04 -0700203 return platform_driver_register(&omap_rng_driver);
Michael Bueschebc915a2006-06-26 00:25:03 -0700204}
205
206static void __exit omap_rng_exit(void)
207{
David Brownellaf2bc7d2006-08-05 12:14:04 -0700208 platform_driver_unregister(&omap_rng_driver);
Michael Bueschebc915a2006-06-26 00:25:03 -0700209}
210
211module_init(omap_rng_init);
212module_exit(omap_rng_exit);
213
214MODULE_AUTHOR("Deepak Saxena (and others)");
215MODULE_LICENSE("GPL");