blob: d8c54e253761c5e2025c2e3941868b7e282eb8b3 [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>
21#include <linux/err.h>
David Brownellaf2bc7d2006-08-05 12:14:04 -070022#include <linux/platform_device.h>
Michael Bueschebc915a2006-06-26 00:25:03 -070023#include <linux/hw_random.h>
Patrick McHardy984e9762007-11-21 12:24:45 +080024#include <linux/delay.h>
Paul Walmsley02666362012-09-23 17:28:26 -060025#include <linux/slab.h>
Paul Walmsley665d92f2012-09-23 17:28:26 -060026#include <linux/pm_runtime.h>
Michael Bueschebc915a2006-06-26 00:25:03 -070027
28#include <asm/io.h>
Michael Bueschebc915a2006-06-26 00:25:03 -070029
30#define RNG_OUT_REG 0x00 /* Output register */
31#define RNG_STAT_REG 0x04 /* Status register
32 [0] = STAT_BUSY */
33#define RNG_ALARM_REG 0x24 /* Alarm register
34 [7:0] = ALARM_COUNTER */
35#define RNG_CONFIG_REG 0x28 /* Configuration register
36 [11:6] = RESET_COUNT
37 [5:3] = RING2_DELAY
38 [2:0] = RING1_DELAY */
39#define RNG_REV_REG 0x3c /* Revision register
40 [7:0] = REV_NB */
41#define RNG_MASK_REG 0x40 /* Mask and reset register
42 [2] = IT_EN
43 [1] = SOFTRESET
44 [0] = AUTOIDLE */
45#define RNG_SYSSTATUS 0x44 /* System status
46 [0] = RESETDONE */
47
Paul Walmsley02666362012-09-23 17:28:26 -060048/**
49 * struct omap_rng_private_data - RNG IP block-specific data
50 * @base: virtual address of the beginning of the RNG IP block registers
Paul Walmsley02666362012-09-23 17:28:26 -060051 * @mem_res: struct resource * for the IP block registers physical memory
52 */
53struct omap_rng_private_data {
54 void __iomem *base;
Paul Walmsley02666362012-09-23 17:28:26 -060055 struct resource *mem_res;
56};
Michael Bueschebc915a2006-06-26 00:25:03 -070057
Paul Walmsley02666362012-09-23 17:28:26 -060058static inline u32 omap_rng_read_reg(struct omap_rng_private_data *priv, int reg)
Michael Bueschebc915a2006-06-26 00:25:03 -070059{
Paul Walmsley02666362012-09-23 17:28:26 -060060 return __raw_readl(priv->base + reg);
Michael Bueschebc915a2006-06-26 00:25:03 -070061}
62
Paul Walmsley02666362012-09-23 17:28:26 -060063static inline void omap_rng_write_reg(struct omap_rng_private_data *priv,
64 int reg, u32 val)
Michael Bueschebc915a2006-06-26 00:25:03 -070065{
Paul Walmsley02666362012-09-23 17:28:26 -060066 __raw_writel(val, priv->base + reg);
Michael Bueschebc915a2006-06-26 00:25:03 -070067}
68
Patrick McHardy984e9762007-11-21 12:24:45 +080069static int omap_rng_data_present(struct hwrng *rng, int wait)
Michael Bueschebc915a2006-06-26 00:25:03 -070070{
Paul Walmsley02666362012-09-23 17:28:26 -060071 struct omap_rng_private_data *priv;
Patrick McHardy984e9762007-11-21 12:24:45 +080072 int data, i;
73
Paul Walmsley02666362012-09-23 17:28:26 -060074 priv = (struct omap_rng_private_data *)rng->priv;
75
Patrick McHardy984e9762007-11-21 12:24:45 +080076 for (i = 0; i < 20; i++) {
Paul Walmsley02666362012-09-23 17:28:26 -060077 data = omap_rng_read_reg(priv, RNG_STAT_REG) ? 0 : 1;
Patrick McHardy984e9762007-11-21 12:24:45 +080078 if (data || !wait)
79 break;
David Brownellc49a7f12008-04-16 19:24:42 +080080 /* RNG produces data fast enough (2+ MBit/sec, even
81 * during "rngtest" loads, that these delays don't
82 * seem to trigger. We *could* use the RNG IRQ, but
83 * that'd be higher overhead ... so why bother?
84 */
Patrick McHardy984e9762007-11-21 12:24:45 +080085 udelay(10);
86 }
87 return data;
Michael Bueschebc915a2006-06-26 00:25:03 -070088}
89
90static int omap_rng_data_read(struct hwrng *rng, u32 *data)
91{
Paul Walmsley02666362012-09-23 17:28:26 -060092 struct omap_rng_private_data *priv;
Michael Bueschebc915a2006-06-26 00:25:03 -070093
Paul Walmsley02666362012-09-23 17:28:26 -060094 priv = (struct omap_rng_private_data *)rng->priv;
95
96 *data = omap_rng_read_reg(priv, RNG_OUT_REG);
97
98 return sizeof(u32);
Michael Bueschebc915a2006-06-26 00:25:03 -070099}
100
101static struct hwrng omap_rng_ops = {
102 .name = "omap",
103 .data_present = omap_rng_data_present,
104 .data_read = omap_rng_data_read,
105};
106
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -0800107static int omap_rng_probe(struct platform_device *pdev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700108{
Paul Walmsley02666362012-09-23 17:28:26 -0600109 struct omap_rng_private_data *priv;
Michael Bueschebc915a2006-06-26 00:25:03 -0700110 int ret;
111
Paul Walmsley02666362012-09-23 17:28:26 -0600112 priv = kzalloc(sizeof(struct omap_rng_private_data), GFP_KERNEL);
113 if (!priv) {
114 dev_err(&pdev->dev, "could not allocate memory\n");
115 return -ENOMEM;
116 };
117
118 omap_rng_ops.priv = (unsigned long)priv;
119 dev_set_drvdata(&pdev->dev, priv);
Michael Bueschebc915a2006-06-26 00:25:03 -0700120
Paul Walmsley02666362012-09-23 17:28:26 -0600121 priv->mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
122 if (!priv->mem_res) {
123 ret = -ENOENT;
124 goto err_ioremap;
125 }
Michael Bueschebc915a2006-06-26 00:25:03 -0700126
Paul Walmsley02666362012-09-23 17:28:26 -0600127 priv->base = devm_request_and_ioremap(&pdev->dev, priv->mem_res);
128 if (!priv->base) {
Russell King55c381e2008-09-04 14:07:22 +0100129 ret = -ENOMEM;
130 goto err_ioremap;
131 }
Paul Walmsley02666362012-09-23 17:28:26 -0600132 dev_set_drvdata(&pdev->dev, priv);
Michael Bueschebc915a2006-06-26 00:25:03 -0700133
Paul Walmsley665d92f2012-09-23 17:28:26 -0600134 pm_runtime_enable(&pdev->dev);
135 pm_runtime_get_sync(&pdev->dev);
136
Michael Bueschebc915a2006-06-26 00:25:03 -0700137 ret = hwrng_register(&omap_rng_ops);
Russell King55c381e2008-09-04 14:07:22 +0100138 if (ret)
139 goto err_register;
Michael Bueschebc915a2006-06-26 00:25:03 -0700140
David Brownellaf2bc7d2006-08-05 12:14:04 -0700141 dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n",
Paul Walmsley02666362012-09-23 17:28:26 -0600142 omap_rng_read_reg(priv, RNG_REV_REG));
Michael Bueschebc915a2006-06-26 00:25:03 -0700143
Paul Walmsley02666362012-09-23 17:28:26 -0600144 omap_rng_write_reg(priv, RNG_MASK_REG, 0x1);
Michael Bueschebc915a2006-06-26 00:25:03 -0700145
146 return 0;
Russell King55c381e2008-09-04 14:07:22 +0100147
148err_register:
Paul Walmsley02666362012-09-23 17:28:26 -0600149 priv->base = NULL;
Paul Walmsley665d92f2012-09-23 17:28:26 -0600150 pm_runtime_disable(&pdev->dev);
Russell King55c381e2008-09-04 14:07:22 +0100151err_ioremap:
Paul Walmsley02666362012-09-23 17:28:26 -0600152 kfree(priv);
153
Russell King55c381e2008-09-04 14:07:22 +0100154 return ret;
Michael Bueschebc915a2006-06-26 00:25:03 -0700155}
156
David Brownellaf2bc7d2006-08-05 12:14:04 -0700157static int __exit omap_rng_remove(struct platform_device *pdev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700158{
Paul Walmsley02666362012-09-23 17:28:26 -0600159 struct omap_rng_private_data *priv = dev_get_drvdata(&pdev->dev);
160
Michael Bueschebc915a2006-06-26 00:25:03 -0700161 hwrng_unregister(&omap_rng_ops);
162
Paul Walmsley02666362012-09-23 17:28:26 -0600163 omap_rng_write_reg(priv, RNG_MASK_REG, 0x0);
164
Paul Walmsley665d92f2012-09-23 17:28:26 -0600165 pm_runtime_put_sync(&pdev->dev);
166 pm_runtime_disable(&pdev->dev);
Michael Bueschebc915a2006-06-26 00:25:03 -0700167
Paul Walmsley02666362012-09-23 17:28:26 -0600168 release_mem_region(priv->mem_res->start, resource_size(priv->mem_res));
169
170 kfree(priv);
Michael Bueschebc915a2006-06-26 00:25:03 -0700171
172 return 0;
173}
174
Arnd Bergmann59596df2012-08-04 07:11:34 +0000175#ifdef CONFIG_PM_SLEEP
Michael Bueschebc915a2006-06-26 00:25:03 -0700176
Rafael J. Wysocki76505722012-07-06 19:08:53 +0200177static int omap_rng_suspend(struct device *dev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700178{
Paul Walmsley02666362012-09-23 17:28:26 -0600179 struct omap_rng_private_data *priv = dev_get_drvdata(dev);
180
181 omap_rng_write_reg(priv, RNG_MASK_REG, 0x0);
Paul Walmsley665d92f2012-09-23 17:28:26 -0600182 pm_runtime_put_sync(dev);
Paul Walmsley02666362012-09-23 17:28:26 -0600183
Michael Bueschebc915a2006-06-26 00:25:03 -0700184 return 0;
185}
186
Rafael J. Wysocki76505722012-07-06 19:08:53 +0200187static int omap_rng_resume(struct device *dev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700188{
Paul Walmsley02666362012-09-23 17:28:26 -0600189 struct omap_rng_private_data *priv = dev_get_drvdata(dev);
190
Paul Walmsley665d92f2012-09-23 17:28:26 -0600191 pm_runtime_get_sync(dev);
Paul Walmsley02666362012-09-23 17:28:26 -0600192 omap_rng_write_reg(priv, RNG_MASK_REG, 0x1);
193
David Brownellaf2bc7d2006-08-05 12:14:04 -0700194 return 0;
Michael Bueschebc915a2006-06-26 00:25:03 -0700195}
196
Rafael J. Wysocki76505722012-07-06 19:08:53 +0200197static SIMPLE_DEV_PM_OPS(omap_rng_pm, omap_rng_suspend, omap_rng_resume);
198#define OMAP_RNG_PM (&omap_rng_pm)
199
Michael Bueschebc915a2006-06-26 00:25:03 -0700200#else
201
Rafael J. Wysocki76505722012-07-06 19:08:53 +0200202#define OMAP_RNG_PM NULL
Michael Bueschebc915a2006-06-26 00:25:03 -0700203
204#endif
205
David Brownellc49a7f12008-04-16 19:24:42 +0800206/* work with hotplug and coldplug */
207MODULE_ALIAS("platform:omap_rng");
Michael Bueschebc915a2006-06-26 00:25:03 -0700208
David Brownellaf2bc7d2006-08-05 12:14:04 -0700209static struct platform_driver omap_rng_driver = {
210 .driver = {
211 .name = "omap_rng",
212 .owner = THIS_MODULE,
Rafael J. Wysocki76505722012-07-06 19:08:53 +0200213 .pm = OMAP_RNG_PM,
David Brownellaf2bc7d2006-08-05 12:14:04 -0700214 },
Michael Bueschebc915a2006-06-26 00:25:03 -0700215 .probe = omap_rng_probe,
216 .remove = __exit_p(omap_rng_remove),
Michael Bueschebc915a2006-06-26 00:25:03 -0700217};
218
219static int __init omap_rng_init(void)
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");