omap-rng.c 5.4 KB
Newer Older
1
/*
D
David Brownell 已提交
2
 * omap-rng.c - RNG driver for TI OMAP CPU family
3 4 5 6 7 8 9 10
 *
 * Author: Deepak Saxena <dsaxena@plexity.net>
 *
 * Copyright 2005 (c) MontaVista Software, Inc.
 *
 * Mostly based on original driver:
 *
 * Copyright (C) 2005 Nokia Corporation
11
 * Author: Juha Yrjölä <juha.yrjola@nokia.com>
12 13 14 15 16 17 18 19 20 21
 *
 * This file is licensed under  the terms of the GNU General Public
 * License version 2. This program is licensed "as is" without any
 * warranty of any kind, whether express or implied.
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/random.h>
#include <linux/err.h>
D
David Brownell 已提交
22
#include <linux/platform_device.h>
23
#include <linux/hw_random.h>
24
#include <linux/delay.h>
25
#include <linux/slab.h>
26
#include <linux/pm_runtime.h>
27 28 29

#include <asm/io.h>

30 31
#include <plat/cpu.h>

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#define RNG_OUT_REG		0x00		/* Output register */
#define RNG_STAT_REG		0x04		/* Status register
							[0] = STAT_BUSY */
#define RNG_ALARM_REG		0x24		/* Alarm register
							[7:0] = ALARM_COUNTER */
#define RNG_CONFIG_REG		0x28		/* Configuration register
							[11:6] = RESET_COUNT
							[5:3]  = RING2_DELAY
							[2:0]  = RING1_DELAY */
#define RNG_REV_REG		0x3c		/* Revision register
							[7:0] = REV_NB */
#define RNG_MASK_REG		0x40		/* Mask and reset register
							[2] = IT_EN
							[1] = SOFTRESET
							[0] = AUTOIDLE */
#define RNG_SYSSTATUS		0x44		/* System status
							[0] = RESETDONE */

50 51 52 53 54 55 56 57 58
/**
 * struct omap_rng_private_data - RNG IP block-specific data
 * @base: virtual address of the beginning of the RNG IP block registers
 * @mem_res: struct resource * for the IP block registers physical memory
 */
struct omap_rng_private_data {
	void __iomem *base;
	struct resource *mem_res;
};
59

60
static inline u32 omap_rng_read_reg(struct omap_rng_private_data *priv, int reg)
61
{
62
	return __raw_readl(priv->base + reg);
63 64
}

65 66
static inline void omap_rng_write_reg(struct omap_rng_private_data *priv,
				      int reg, u32 val)
67
{
68
	__raw_writel(val, priv->base + reg);
69 70
}

71
static int omap_rng_data_present(struct hwrng *rng, int wait)
72
{
73
	struct omap_rng_private_data *priv;
74 75
	int data, i;

76 77
	priv = (struct omap_rng_private_data *)rng->priv;

78
	for (i = 0; i < 20; i++) {
79
		data = omap_rng_read_reg(priv, RNG_STAT_REG) ? 0 : 1;
80 81
		if (data || !wait)
			break;
D
David Brownell 已提交
82 83 84 85 86
		/* RNG produces data fast enough (2+ MBit/sec, even
		 * during "rngtest" loads, that these delays don't
		 * seem to trigger.  We *could* use the RNG IRQ, but
		 * that'd be higher overhead ... so why bother?
		 */
87 88 89
		udelay(10);
	}
	return data;
90 91 92 93
}

static int omap_rng_data_read(struct hwrng *rng, u32 *data)
{
94 95 96 97 98
	struct omap_rng_private_data *priv;

	priv = (struct omap_rng_private_data *)rng->priv;

	*data = omap_rng_read_reg(priv, RNG_OUT_REG);
99

100
	return sizeof(u32);
101 102 103 104 105 106 107 108
}

static struct hwrng omap_rng_ops = {
	.name		= "omap",
	.data_present	= omap_rng_data_present,
	.data_read	= omap_rng_data_read,
};

109
static int __devinit omap_rng_probe(struct platform_device *pdev)
110
{
111
	struct omap_rng_private_data *priv;
112 113
	int ret;

114 115 116 117 118 119 120 121
	priv = kzalloc(sizeof(struct omap_rng_private_data), GFP_KERNEL);
	if (!priv) {
		dev_err(&pdev->dev, "could not allocate memory\n");
		return -ENOMEM;
	};

	omap_rng_ops.priv = (unsigned long)priv;
	dev_set_drvdata(&pdev->dev, priv);
122

123 124 125 126 127
	priv->mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!priv->mem_res) {
		ret = -ENOENT;
		goto err_ioremap;
	}
128

129 130
	priv->base = devm_request_and_ioremap(&pdev->dev, priv->mem_res);
	if (!priv->base) {
131 132 133
		ret = -ENOMEM;
		goto err_ioremap;
	}
134
	dev_set_drvdata(&pdev->dev, priv);
135

136 137 138
	pm_runtime_enable(&pdev->dev);
	pm_runtime_get_sync(&pdev->dev);

139
	ret = hwrng_register(&omap_rng_ops);
140 141
	if (ret)
		goto err_register;
142

D
David Brownell 已提交
143
	dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n",
144 145 146
		 omap_rng_read_reg(priv, RNG_REV_REG));

	omap_rng_write_reg(priv, RNG_MASK_REG, 0x1);
147 148

	return 0;
149 150

err_register:
151
	priv->base = NULL;
152
	pm_runtime_disable(&pdev->dev);
153
err_ioremap:
154 155
	kfree(priv);

156
	return ret;
157 158
}

D
David Brownell 已提交
159
static int __exit omap_rng_remove(struct platform_device *pdev)
160
{
161 162
	struct omap_rng_private_data *priv = dev_get_drvdata(&pdev->dev);

163 164
	hwrng_unregister(&omap_rng_ops);

165 166
	omap_rng_write_reg(priv, RNG_MASK_REG, 0x0);

167 168
	pm_runtime_put_sync(&pdev->dev);
	pm_runtime_disable(&pdev->dev);
169

170 171 172
	release_mem_region(priv->mem_res->start, resource_size(priv->mem_res));

	kfree(priv);
173 174 175 176

	return 0;
}

177
#ifdef CONFIG_PM_SLEEP
178

179
static int omap_rng_suspend(struct device *dev)
180
{
181 182 183
	struct omap_rng_private_data *priv = dev_get_drvdata(dev);

	omap_rng_write_reg(priv, RNG_MASK_REG, 0x0);
184
	pm_runtime_put_sync(dev);
185

186 187 188
	return 0;
}

189
static int omap_rng_resume(struct device *dev)
190
{
191 192
	struct omap_rng_private_data *priv = dev_get_drvdata(dev);

193
	pm_runtime_get_sync(dev);
194 195
	omap_rng_write_reg(priv, RNG_MASK_REG, 0x1);

D
David Brownell 已提交
196
	return 0;
197 198
}

199 200 201
static SIMPLE_DEV_PM_OPS(omap_rng_pm, omap_rng_suspend, omap_rng_resume);
#define	OMAP_RNG_PM	(&omap_rng_pm)

202 203
#else

204
#define	OMAP_RNG_PM	NULL
205 206 207

#endif

D
David Brownell 已提交
208 209
/* work with hotplug and coldplug */
MODULE_ALIAS("platform:omap_rng");
210

D
David Brownell 已提交
211 212 213 214
static struct platform_driver omap_rng_driver = {
	.driver = {
		.name		= "omap_rng",
		.owner		= THIS_MODULE,
215
		.pm		= OMAP_RNG_PM,
D
David Brownell 已提交
216
	},
217 218 219 220 221 222 223 224 225
	.probe		= omap_rng_probe,
	.remove		= __exit_p(omap_rng_remove),
};

static int __init omap_rng_init(void)
{
	if (!cpu_is_omap16xx() && !cpu_is_omap24xx())
		return -ENODEV;

D
David Brownell 已提交
226
	return platform_driver_register(&omap_rng_driver);
227 228 229 230
}

static void __exit omap_rng_exit(void)
{
D
David Brownell 已提交
231
	platform_driver_unregister(&omap_rng_driver);
232 233 234 235 236 237 238
}

module_init(omap_rng_init);
module_exit(omap_rng_exit);

MODULE_AUTHOR("Deepak Saxena (and others)");
MODULE_LICENSE("GPL");