blob: d2903e7722704843ac254390046b65b5b5540d6c [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);
Thierry Redingc7c9e1c2013-01-21 11:08:59 +0100122 priv->base = devm_ioremap_resource(&pdev->dev, priv->mem_res);
123 if (IS_ERR(priv->base)) {
124 ret = PTR_ERR(priv->base);
Russell King55c381e2008-09-04 14:07:22 +0100125 goto err_ioremap;
126 }
Paul Walmsley02666362012-09-23 17:28:26 -0600127 dev_set_drvdata(&pdev->dev, priv);
Michael Bueschebc915a2006-06-26 00:25:03 -0700128
Paul Walmsley665d92f2012-09-23 17:28:26 -0600129 pm_runtime_enable(&pdev->dev);
130 pm_runtime_get_sync(&pdev->dev);
131
Michael Bueschebc915a2006-06-26 00:25:03 -0700132 ret = hwrng_register(&omap_rng_ops);
Russell King55c381e2008-09-04 14:07:22 +0100133 if (ret)
134 goto err_register;
Michael Bueschebc915a2006-06-26 00:25:03 -0700135
David Brownellaf2bc7d2006-08-05 12:14:04 -0700136 dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n",
Paul Walmsley02666362012-09-23 17:28:26 -0600137 omap_rng_read_reg(priv, RNG_REV_REG));
Michael Bueschebc915a2006-06-26 00:25:03 -0700138
Paul Walmsley02666362012-09-23 17:28:26 -0600139 omap_rng_write_reg(priv, RNG_MASK_REG, 0x1);
Michael Bueschebc915a2006-06-26 00:25:03 -0700140
141 return 0;
Russell King55c381e2008-09-04 14:07:22 +0100142
143err_register:
Paul Walmsley02666362012-09-23 17:28:26 -0600144 priv->base = NULL;
Paul Walmsley665d92f2012-09-23 17:28:26 -0600145 pm_runtime_disable(&pdev->dev);
Russell King55c381e2008-09-04 14:07:22 +0100146err_ioremap:
Paul Walmsley02666362012-09-23 17:28:26 -0600147 kfree(priv);
148
Russell King55c381e2008-09-04 14:07:22 +0100149 return ret;
Michael Bueschebc915a2006-06-26 00:25:03 -0700150}
151
David Brownellaf2bc7d2006-08-05 12:14:04 -0700152static int __exit omap_rng_remove(struct platform_device *pdev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700153{
Paul Walmsley02666362012-09-23 17:28:26 -0600154 struct omap_rng_private_data *priv = dev_get_drvdata(&pdev->dev);
155
Michael Bueschebc915a2006-06-26 00:25:03 -0700156 hwrng_unregister(&omap_rng_ops);
157
Paul Walmsley02666362012-09-23 17:28:26 -0600158 omap_rng_write_reg(priv, RNG_MASK_REG, 0x0);
159
Paul Walmsley665d92f2012-09-23 17:28:26 -0600160 pm_runtime_put_sync(&pdev->dev);
161 pm_runtime_disable(&pdev->dev);
Michael Bueschebc915a2006-06-26 00:25:03 -0700162
Paul Walmsley02666362012-09-23 17:28:26 -0600163 release_mem_region(priv->mem_res->start, resource_size(priv->mem_res));
164
165 kfree(priv);
Michael Bueschebc915a2006-06-26 00:25:03 -0700166
167 return 0;
168}
169
Arnd Bergmann59596df2012-08-04 07:11:34 +0000170#ifdef CONFIG_PM_SLEEP
Michael Bueschebc915a2006-06-26 00:25:03 -0700171
Rafael J. Wysocki76505722012-07-06 19:08:53 +0200172static int omap_rng_suspend(struct device *dev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700173{
Paul Walmsley02666362012-09-23 17:28:26 -0600174 struct omap_rng_private_data *priv = dev_get_drvdata(dev);
175
176 omap_rng_write_reg(priv, RNG_MASK_REG, 0x0);
Paul Walmsley665d92f2012-09-23 17:28:26 -0600177 pm_runtime_put_sync(dev);
Paul Walmsley02666362012-09-23 17:28:26 -0600178
Michael Bueschebc915a2006-06-26 00:25:03 -0700179 return 0;
180}
181
Rafael J. Wysocki76505722012-07-06 19:08:53 +0200182static int omap_rng_resume(struct device *dev)
Michael Bueschebc915a2006-06-26 00:25:03 -0700183{
Paul Walmsley02666362012-09-23 17:28:26 -0600184 struct omap_rng_private_data *priv = dev_get_drvdata(dev);
185
Paul Walmsley665d92f2012-09-23 17:28:26 -0600186 pm_runtime_get_sync(dev);
Paul Walmsley02666362012-09-23 17:28:26 -0600187 omap_rng_write_reg(priv, RNG_MASK_REG, 0x1);
188
David Brownellaf2bc7d2006-08-05 12:14:04 -0700189 return 0;
Michael Bueschebc915a2006-06-26 00:25:03 -0700190}
191
Rafael J. Wysocki76505722012-07-06 19:08:53 +0200192static SIMPLE_DEV_PM_OPS(omap_rng_pm, omap_rng_suspend, omap_rng_resume);
193#define OMAP_RNG_PM (&omap_rng_pm)
194
Michael Bueschebc915a2006-06-26 00:25:03 -0700195#else
196
Rafael J. Wysocki76505722012-07-06 19:08:53 +0200197#define OMAP_RNG_PM NULL
Michael Bueschebc915a2006-06-26 00:25:03 -0700198
199#endif
200
David Brownellc49a7f12008-04-16 19:24:42 +0800201/* work with hotplug and coldplug */
202MODULE_ALIAS("platform:omap_rng");
Michael Bueschebc915a2006-06-26 00:25:03 -0700203
David Brownellaf2bc7d2006-08-05 12:14:04 -0700204static struct platform_driver omap_rng_driver = {
205 .driver = {
206 .name = "omap_rng",
207 .owner = THIS_MODULE,
Rafael J. Wysocki76505722012-07-06 19:08:53 +0200208 .pm = OMAP_RNG_PM,
David Brownellaf2bc7d2006-08-05 12:14:04 -0700209 },
Michael Bueschebc915a2006-06-26 00:25:03 -0700210 .probe = omap_rng_probe,
211 .remove = __exit_p(omap_rng_remove),
Michael Bueschebc915a2006-06-26 00:25:03 -0700212};
213
214static int __init omap_rng_init(void)
215{
David Brownellaf2bc7d2006-08-05 12:14:04 -0700216 return platform_driver_register(&omap_rng_driver);
Michael Bueschebc915a2006-06-26 00:25:03 -0700217}
218
219static void __exit omap_rng_exit(void)
220{
David Brownellaf2bc7d2006-08-05 12:14:04 -0700221 platform_driver_unregister(&omap_rng_driver);
Michael Bueschebc915a2006-06-26 00:25:03 -0700222}
223
224module_init(omap_rng_init);
225module_exit(omap_rng_exit);
226
227MODULE_AUTHOR("Deepak Saxena (and others)");
228MODULE_LICENSE("GPL");