blob: e0f0b9839862c08488c4237cf641d386f0256259 [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>
Paul Walmsley02666362012-09-23 17:28:26 -060026#include <linux/slab.h>
Michael Bueschebc915a2006-06-26 00:25:03 -070027
28#include <asm/io.h>
Michael Bueschebc915a2006-06-26 00:25:03 -070029
Tony Lindgren2c799ce2012-02-24 10:34:35 -080030#include <plat/cpu.h>
31
Michael Bueschebc915a2006-06-26 00:25:03 -070032#define RNG_OUT_REG 0x00 /* Output register */
33#define RNG_STAT_REG 0x04 /* Status register
34 [0] = STAT_BUSY */
35#define RNG_ALARM_REG 0x24 /* Alarm register
36 [7:0] = ALARM_COUNTER */
37#define RNG_CONFIG_REG 0x28 /* Configuration register
38 [11:6] = RESET_COUNT
39 [5:3] = RING2_DELAY
40 [2:0] = RING1_DELAY */
41#define RNG_REV_REG 0x3c /* Revision register
42 [7:0] = REV_NB */
43#define RNG_MASK_REG 0x40 /* Mask and reset register
44 [2] = IT_EN
45 [1] = SOFTRESET
46 [0] = AUTOIDLE */
47#define RNG_SYSSTATUS 0x44 /* System status
48 [0] = RESETDONE */
49
Paul Walmsley02666362012-09-23 17:28:26 -060050/**
51 * struct omap_rng_private_data - RNG IP block-specific data
52 * @base: virtual address of the beginning of the RNG IP block registers
53 * @clk: RNG clock
54 * @mem_res: struct resource * for the IP block registers physical memory
55 */
56struct omap_rng_private_data {
57 void __iomem *base;
58 struct clk *clk;
59 struct resource *mem_res;
60};
Michael Bueschebc915a2006-06-26 00:25:03 -070061
Paul Walmsley02666362012-09-23 17:28:26 -060062static inline u32 omap_rng_read_reg(struct omap_rng_private_data *priv, int reg)
Michael Bueschebc915a2006-06-26 00:25:03 -070063{
Paul Walmsley02666362012-09-23 17:28:26 -060064 return __raw_readl(priv->base + reg);
Michael Bueschebc915a2006-06-26 00:25:03 -070065}
66
Paul Walmsley02666362012-09-23 17:28:26 -060067static inline void omap_rng_write_reg(struct omap_rng_private_data *priv,
68 int reg, u32 val)
Michael Bueschebc915a2006-06-26 00:25:03 -070069{
Paul Walmsley02666362012-09-23 17:28:26 -060070 __raw_writel(val, priv->base + reg);
Michael Bueschebc915a2006-06-26 00:25:03 -070071}
72
Patrick McHardy984e9762007-11-21 12:24:45 +080073static int omap_rng_data_present(struct hwrng *rng, int wait)
Michael Bueschebc915a2006-06-26 00:25:03 -070074{
Paul Walmsley02666362012-09-23 17:28:26 -060075 struct omap_rng_private_data *priv;
Patrick McHardy984e9762007-11-21 12:24:45 +080076 int data, i;
77
Paul Walmsley02666362012-09-23 17:28:26 -060078 priv = (struct omap_rng_private_data *)rng->priv;
79
Patrick McHardy984e9762007-11-21 12:24:45 +080080 for (i = 0; i < 20; i++) {
Paul Walmsley02666362012-09-23 17:28:26 -060081 data = omap_rng_read_reg(priv, RNG_STAT_REG) ? 0 : 1;
Patrick McHardy984e9762007-11-21 12:24:45 +080082 if (data || !wait)
83 break;
David Brownellc49a7f12008-04-16 19:24:42 +080084 /* RNG produces data fast enough (2+ MBit/sec, even
85 * during "rngtest" loads, that these delays don't
86 * seem to trigger. We *could* use the RNG IRQ, but
87 * that'd be higher overhead ... so why bother?
88 */
Patrick McHardy984e9762007-11-21 12:24:45 +080089 udelay(10);
90 }
91 return data;
Michael Bueschebc915a2006-06-26 00:25:03 -070092}
93
94static int omap_rng_data_read(struct hwrng *rng, u32 *data)
95{
Paul Walmsley02666362012-09-23 17:28:26 -060096 struct omap_rng_private_data *priv;
Michael Bueschebc915a2006-06-26 00:25:03 -070097
Paul Walmsley02666362012-09-23 17:28:26 -060098 priv = (struct omap_rng_private_data *)rng->priv;
99
100 *data = omap_rng_read_reg(priv, RNG_OUT_REG);
101
102 return sizeof(u32);
Michael Bueschebc915a2006-06-26 00:25:03 -0700103}
104
105static struct hwrng omap_rng_ops = {
106 .name = "omap",
107 .data_present = omap_rng_data_present,
108 .data_read = omap_rng_data_read,
109};
110
Uwe Kleine-König9f171ad2009-03-29 15:47:06 +0800111static int __devinit omap_rng_probe(struct platform_device *pdev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700112{
Paul Walmsley02666362012-09-23 17:28:26 -0600113 struct omap_rng_private_data *priv;
Michael Bueschebc915a2006-06-26 00:25:03 -0700114 int ret;
115
Paul Walmsley02666362012-09-23 17:28:26 -0600116 priv = kzalloc(sizeof(struct omap_rng_private_data), GFP_KERNEL);
117 if (!priv) {
118 dev_err(&pdev->dev, "could not allocate memory\n");
119 return -ENOMEM;
120 };
121
122 omap_rng_ops.priv = (unsigned long)priv;
123 dev_set_drvdata(&pdev->dev, priv);
Michael Bueschebc915a2006-06-26 00:25:03 -0700124
David Brownellaf2bc7d2006-08-05 12:14:04 -0700125 if (cpu_is_omap24xx()) {
Paul Walmsley02666362012-09-23 17:28:26 -0600126 priv->clk = clk_get(&pdev->dev, "ick");
127 if (IS_ERR(priv->clk)) {
David Brownellaf2bc7d2006-08-05 12:14:04 -0700128 dev_err(&pdev->dev, "Could not get rng_ick\n");
Paul Walmsley02666362012-09-23 17:28:26 -0600129 ret = PTR_ERR(priv->clk);
Michael Bueschebc915a2006-06-26 00:25:03 -0700130 return ret;
Paul Walmsley02666362012-09-23 17:28:26 -0600131 } else {
132 clk_enable(priv->clk);
133 }
Michael Bueschebc915a2006-06-26 00:25:03 -0700134 }
135
Paul Walmsley02666362012-09-23 17:28:26 -0600136 priv->mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
137 if (!priv->mem_res) {
138 ret = -ENOENT;
139 goto err_ioremap;
140 }
Michael Bueschebc915a2006-06-26 00:25:03 -0700141
Paul Walmsley02666362012-09-23 17:28:26 -0600142 priv->base = devm_request_and_ioremap(&pdev->dev, priv->mem_res);
143 if (!priv->base) {
Russell King55c381e2008-09-04 14:07:22 +0100144 ret = -ENOMEM;
145 goto err_ioremap;
146 }
Paul Walmsley02666362012-09-23 17:28:26 -0600147 dev_set_drvdata(&pdev->dev, priv);
Michael Bueschebc915a2006-06-26 00:25:03 -0700148
149 ret = hwrng_register(&omap_rng_ops);
Russell King55c381e2008-09-04 14:07:22 +0100150 if (ret)
151 goto err_register;
Michael Bueschebc915a2006-06-26 00:25:03 -0700152
David Brownellaf2bc7d2006-08-05 12:14:04 -0700153 dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n",
Paul Walmsley02666362012-09-23 17:28:26 -0600154 omap_rng_read_reg(priv, RNG_REV_REG));
Michael Bueschebc915a2006-06-26 00:25:03 -0700155
Paul Walmsley02666362012-09-23 17:28:26 -0600156
157 omap_rng_write_reg(priv, RNG_MASK_REG, 0x1);
Michael Bueschebc915a2006-06-26 00:25:03 -0700158
159 return 0;
Russell King55c381e2008-09-04 14:07:22 +0100160
161err_register:
Paul Walmsley02666362012-09-23 17:28:26 -0600162 priv->base = NULL;
Russell King55c381e2008-09-04 14:07:22 +0100163err_ioremap:
Russell King55c381e2008-09-04 14:07:22 +0100164 if (cpu_is_omap24xx()) {
Paul Walmsley02666362012-09-23 17:28:26 -0600165 clk_disable(priv->clk);
166 clk_put(priv->clk);
Russell King55c381e2008-09-04 14:07:22 +0100167 }
Paul Walmsley02666362012-09-23 17:28:26 -0600168
169 kfree(priv);
170
Russell King55c381e2008-09-04 14:07:22 +0100171 return ret;
Michael Bueschebc915a2006-06-26 00:25:03 -0700172}
173
David Brownellaf2bc7d2006-08-05 12:14:04 -0700174static int __exit omap_rng_remove(struct platform_device *pdev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700175{
Paul Walmsley02666362012-09-23 17:28:26 -0600176 struct omap_rng_private_data *priv = dev_get_drvdata(&pdev->dev);
177
Michael Bueschebc915a2006-06-26 00:25:03 -0700178 hwrng_unregister(&omap_rng_ops);
179
Paul Walmsley02666362012-09-23 17:28:26 -0600180 omap_rng_write_reg(priv, RNG_MASK_REG, 0x0);
181
182 iounmap(priv->base);
Michael Bueschebc915a2006-06-26 00:25:03 -0700183
184 if (cpu_is_omap24xx()) {
Paul Walmsley02666362012-09-23 17:28:26 -0600185 clk_disable(priv->clk);
186 clk_put(priv->clk);
Michael Bueschebc915a2006-06-26 00:25:03 -0700187 }
188
Paul Walmsley02666362012-09-23 17:28:26 -0600189 release_mem_region(priv->mem_res->start, resource_size(priv->mem_res));
190
191 kfree(priv);
Michael Bueschebc915a2006-06-26 00:25:03 -0700192
193 return 0;
194}
195
Arnd Bergmann59596df2012-08-04 07:11:34 +0000196#ifdef CONFIG_PM_SLEEP
Michael Bueschebc915a2006-06-26 00:25:03 -0700197
Rafael J. Wysocki76505722012-07-06 19:08:53 +0200198static int omap_rng_suspend(struct device *dev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700199{
Paul Walmsley02666362012-09-23 17:28:26 -0600200 struct omap_rng_private_data *priv = dev_get_drvdata(dev);
201
202 omap_rng_write_reg(priv, RNG_MASK_REG, 0x0);
203
Michael Bueschebc915a2006-06-26 00:25:03 -0700204 return 0;
205}
206
Rafael J. Wysocki76505722012-07-06 19:08:53 +0200207static int omap_rng_resume(struct device *dev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700208{
Paul Walmsley02666362012-09-23 17:28:26 -0600209 struct omap_rng_private_data *priv = dev_get_drvdata(dev);
210
211 omap_rng_write_reg(priv, RNG_MASK_REG, 0x1);
212
David Brownellaf2bc7d2006-08-05 12:14:04 -0700213 return 0;
Michael Bueschebc915a2006-06-26 00:25:03 -0700214}
215
Rafael J. Wysocki76505722012-07-06 19:08:53 +0200216static SIMPLE_DEV_PM_OPS(omap_rng_pm, omap_rng_suspend, omap_rng_resume);
217#define OMAP_RNG_PM (&omap_rng_pm)
218
Michael Bueschebc915a2006-06-26 00:25:03 -0700219#else
220
Rafael J. Wysocki76505722012-07-06 19:08:53 +0200221#define OMAP_RNG_PM NULL
Michael Bueschebc915a2006-06-26 00:25:03 -0700222
223#endif
224
David Brownellc49a7f12008-04-16 19:24:42 +0800225/* work with hotplug and coldplug */
226MODULE_ALIAS("platform:omap_rng");
Michael Bueschebc915a2006-06-26 00:25:03 -0700227
David Brownellaf2bc7d2006-08-05 12:14:04 -0700228static struct platform_driver omap_rng_driver = {
229 .driver = {
230 .name = "omap_rng",
231 .owner = THIS_MODULE,
Rafael J. Wysocki76505722012-07-06 19:08:53 +0200232 .pm = OMAP_RNG_PM,
David Brownellaf2bc7d2006-08-05 12:14:04 -0700233 },
Michael Bueschebc915a2006-06-26 00:25:03 -0700234 .probe = omap_rng_probe,
235 .remove = __exit_p(omap_rng_remove),
Michael Bueschebc915a2006-06-26 00:25:03 -0700236};
237
238static int __init omap_rng_init(void)
239{
240 if (!cpu_is_omap16xx() && !cpu_is_omap24xx())
241 return -ENODEV;
242
David Brownellaf2bc7d2006-08-05 12:14:04 -0700243 return platform_driver_register(&omap_rng_driver);
Michael Bueschebc915a2006-06-26 00:25:03 -0700244}
245
246static void __exit omap_rng_exit(void)
247{
David Brownellaf2bc7d2006-08-05 12:14:04 -0700248 platform_driver_unregister(&omap_rng_driver);
Michael Bueschebc915a2006-06-26 00:25:03 -0700249}
250
251module_init(omap_rng_init);
252module_exit(omap_rng_exit);
253
254MODULE_AUTHOR("Deepak Saxena (and others)");
255MODULE_LICENSE("GPL");