hwrng: omap - Add device tree support
[linux-2.6-block.git] / drivers / char / hw_random / omap-rng.c
CommitLineData
ebc915ad 1/*
c49a7f18 2 * omap-rng.c - RNG driver for TI OMAP CPU family
ebc915ad
MB
3 *
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
96de0e25 11 * Author: Juha Yrjölä <juha.yrjola@nokia.com>
ebc915ad
MB
12 *
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.
ebc915ad
MB
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/random.h>
21#include <linux/err.h>
af2bc7d2 22#include <linux/platform_device.h>
ebc915ad 23#include <linux/hw_random.h>
984e976f 24#include <linux/delay.h>
02666360 25#include <linux/slab.h>
665d92fa 26#include <linux/pm_runtime.h>
c903970c
LV
27#include <linux/of.h>
28#include <linux/of_device.h>
29#include <linux/of_address.h>
ebc915ad
MB
30
31#include <asm/io.h>
ebc915ad
MB
32
33#define RNG_OUT_REG 0x00 /* Output register */
34#define RNG_STAT_REG 0x04 /* Status register
35 [0] = STAT_BUSY */
36#define RNG_ALARM_REG 0x24 /* Alarm register
37 [7:0] = ALARM_COUNTER */
38#define RNG_CONFIG_REG 0x28 /* Configuration register
39 [11:6] = RESET_COUNT
40 [5:3] = RING2_DELAY
41 [2:0] = RING1_DELAY */
42#define RNG_REV_REG 0x3c /* Revision register
43 [7:0] = REV_NB */
44#define RNG_MASK_REG 0x40 /* Mask and reset register
45 [2] = IT_EN
46 [1] = SOFTRESET
47 [0] = AUTOIDLE */
48#define RNG_SYSSTATUS 0x44 /* System status
49 [0] = RESETDONE */
50
02666360
PW
51/**
52 * struct omap_rng_private_data - RNG IP block-specific data
53 * @base: virtual address of the beginning of the RNG IP block registers
02666360
PW
54 * @mem_res: struct resource * for the IP block registers physical memory
55 */
56struct omap_rng_private_data {
57 void __iomem *base;
02666360
PW
58 struct resource *mem_res;
59};
ebc915ad 60
02666360 61static inline u32 omap_rng_read_reg(struct omap_rng_private_data *priv, int reg)
ebc915ad 62{
02666360 63 return __raw_readl(priv->base + reg);
ebc915ad
MB
64}
65
02666360
PW
66static inline void omap_rng_write_reg(struct omap_rng_private_data *priv,
67 int reg, u32 val)
ebc915ad 68{
02666360 69 __raw_writel(val, priv->base + reg);
ebc915ad
MB
70}
71
984e976f 72static int omap_rng_data_present(struct hwrng *rng, int wait)
ebc915ad 73{
02666360 74 struct omap_rng_private_data *priv;
984e976f
PM
75 int data, i;
76
02666360
PW
77 priv = (struct omap_rng_private_data *)rng->priv;
78
984e976f 79 for (i = 0; i < 20; i++) {
02666360 80 data = omap_rng_read_reg(priv, RNG_STAT_REG) ? 0 : 1;
984e976f
PM
81 if (data || !wait)
82 break;
c49a7f18
DB
83 /* RNG produces data fast enough (2+ MBit/sec, even
84 * during "rngtest" loads, that these delays don't
85 * seem to trigger. We *could* use the RNG IRQ, but
86 * that'd be higher overhead ... so why bother?
87 */
984e976f
PM
88 udelay(10);
89 }
90 return data;
ebc915ad
MB
91}
92
93static int omap_rng_data_read(struct hwrng *rng, u32 *data)
94{
02666360
PW
95 struct omap_rng_private_data *priv;
96
97 priv = (struct omap_rng_private_data *)rng->priv;
98
99 *data = omap_rng_read_reg(priv, RNG_OUT_REG);
ebc915ad 100
02666360 101 return sizeof(u32);
ebc915ad
MB
102}
103
104static struct hwrng omap_rng_ops = {
105 .name = "omap",
106 .data_present = omap_rng_data_present,
107 .data_read = omap_rng_data_read,
108};
109
c903970c
LV
110#if defined(CONFIG_OF)
111static const struct of_device_id omap_rng_of_match[] = {
112 { .compatible = "ti,omap2-rng" },
113 {},
114};
115MODULE_DEVICE_TABLE(of, omap_rng_of_match);
116#endif
117
bcd2982a 118static int omap_rng_probe(struct platform_device *pdev)
ebc915ad 119{
02666360 120 struct omap_rng_private_data *priv;
ebc915ad
MB
121 int ret;
122
d52dc81e
LV
123 priv = devm_kzalloc(&pdev->dev, sizeof(struct omap_rng_private_data),
124 GFP_KERNEL);
02666360
PW
125 if (!priv) {
126 dev_err(&pdev->dev, "could not allocate memory\n");
127 return -ENOMEM;
128 };
129
130 omap_rng_ops.priv = (unsigned long)priv;
1f539bcb 131 platform_set_drvdata(pdev, priv);
ebc915ad 132
02666360 133 priv->mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
c7c9e1c3
TR
134 priv->base = devm_ioremap_resource(&pdev->dev, priv->mem_res);
135 if (IS_ERR(priv->base)) {
136 ret = PTR_ERR(priv->base);
55c381e4
RK
137 goto err_ioremap;
138 }
ebc915ad 139
665d92fa
PW
140 pm_runtime_enable(&pdev->dev);
141 pm_runtime_get_sync(&pdev->dev);
142
ebc915ad 143 ret = hwrng_register(&omap_rng_ops);
55c381e4
RK
144 if (ret)
145 goto err_register;
ebc915ad 146
af2bc7d2 147 dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n",
02666360
PW
148 omap_rng_read_reg(priv, RNG_REV_REG));
149
02666360 150 omap_rng_write_reg(priv, RNG_MASK_REG, 0x1);
ebc915ad
MB
151
152 return 0;
55c381e4
RK
153
154err_register:
02666360 155 priv->base = NULL;
665d92fa 156 pm_runtime_disable(&pdev->dev);
55c381e4 157err_ioremap:
55c381e4 158 return ret;
ebc915ad
MB
159}
160
af2bc7d2 161static int __exit omap_rng_remove(struct platform_device *pdev)
ebc915ad 162{
1f539bcb 163 struct omap_rng_private_data *priv = platform_get_drvdata(pdev);
02666360 164
ebc915ad
MB
165 hwrng_unregister(&omap_rng_ops);
166
02666360
PW
167 omap_rng_write_reg(priv, RNG_MASK_REG, 0x0);
168
665d92fa
PW
169 pm_runtime_put_sync(&pdev->dev);
170 pm_runtime_disable(&pdev->dev);
ebc915ad 171
ebc915ad
MB
172 return 0;
173}
174
59596df6 175#ifdef CONFIG_PM_SLEEP
ebc915ad 176
7650572a 177static int omap_rng_suspend(struct device *dev)
ebc915ad 178{
02666360
PW
179 struct omap_rng_private_data *priv = dev_get_drvdata(dev);
180
181 omap_rng_write_reg(priv, RNG_MASK_REG, 0x0);
665d92fa 182 pm_runtime_put_sync(dev);
02666360 183
ebc915ad
MB
184 return 0;
185}
186
7650572a 187static int omap_rng_resume(struct device *dev)
ebc915ad 188{
02666360
PW
189 struct omap_rng_private_data *priv = dev_get_drvdata(dev);
190
665d92fa 191 pm_runtime_get_sync(dev);
02666360
PW
192 omap_rng_write_reg(priv, RNG_MASK_REG, 0x1);
193
af2bc7d2 194 return 0;
ebc915ad
MB
195}
196
7650572a
RW
197static SIMPLE_DEV_PM_OPS(omap_rng_pm, omap_rng_suspend, omap_rng_resume);
198#define OMAP_RNG_PM (&omap_rng_pm)
199
ebc915ad
MB
200#else
201
7650572a 202#define OMAP_RNG_PM NULL
ebc915ad
MB
203
204#endif
205
af2bc7d2
DB
206static struct platform_driver omap_rng_driver = {
207 .driver = {
208 .name = "omap_rng",
209 .owner = THIS_MODULE,
7650572a 210 .pm = OMAP_RNG_PM,
c903970c 211 .of_match_table = of_match_ptr(omap_rng_of_match),
af2bc7d2 212 },
ebc915ad
MB
213 .probe = omap_rng_probe,
214 .remove = __exit_p(omap_rng_remove),
ebc915ad
MB
215};
216
4390f77b
LV
217module_platform_driver(omap_rng_driver);
218MODULE_ALIAS("platform:omap_rng");
ebc915ad
MB
219MODULE_AUTHOR("Deepak Saxena (and others)");
220MODULE_LICENSE("GPL");