omap-rng.c 4.9 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
 *
 * 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>
D
David Brownell 已提交
21
#include <linux/clk.h>
22
#include <linux/err.h>
D
David Brownell 已提交
23
#include <linux/platform_device.h>
24
#include <linux/hw_random.h>
25
#include <linux/delay.h>
26 27 28

#include <asm/io.h>

29 30
#include <plat/cpu.h>

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#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 */

static void __iomem *rng_base;
static struct clk *rng_ick;
D
David Brownell 已提交
51
static struct platform_device *rng_dev;
52

D
David Brownell 已提交
53
static inline u32 omap_rng_read_reg(int reg)
54 55 56 57
{
	return __raw_readl(rng_base + reg);
}

D
David Brownell 已提交
58
static inline void omap_rng_write_reg(int reg, u32 val)
59 60 61 62
{
	__raw_writel(val, rng_base + reg);
}

63
static int omap_rng_data_present(struct hwrng *rng, int wait)
64
{
65 66 67 68 69 70
	int data, i;

	for (i = 0; i < 20; i++) {
		data = omap_rng_read_reg(RNG_STAT_REG) ? 0 : 1;
		if (data || !wait)
			break;
D
David Brownell 已提交
71 72 73 74 75
		/* 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?
		 */
76 77 78
		udelay(10);
	}
	return data;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
}

static int omap_rng_data_read(struct hwrng *rng, u32 *data)
{
	*data = omap_rng_read_reg(RNG_OUT_REG);

	return 4;
}

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

94
static int __devinit omap_rng_probe(struct platform_device *pdev)
95
{
96
	struct resource *res;
97 98 99 100 101 102
	int ret;

	/*
	 * A bit ugly, and it will never actually happen but there can
	 * be only one RNG and this catches any bork
	 */
D
David Brownell 已提交
103 104
	if (rng_dev)
		return -EBUSY;
105

D
David Brownell 已提交
106
	if (cpu_is_omap24xx()) {
107
		rng_ick = clk_get(&pdev->dev, "ick");
108
		if (IS_ERR(rng_ick)) {
D
David Brownell 已提交
109
			dev_err(&pdev->dev, "Could not get rng_ick\n");
110 111
			ret = PTR_ERR(rng_ick);
			return ret;
D
David Brownell 已提交
112 113
		} else
			clk_enable(rng_ick);
114 115 116 117
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

J
Julia Lawall 已提交
118 119 120 121
	if (!res) {
		ret = -ENOENT;
		goto err_region;
	}
122

123
	if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
124 125 126
		ret = -EBUSY;
		goto err_region;
	}
127

128
	dev_set_drvdata(&pdev->dev, res);
129
	rng_base = ioremap(res->start, resource_size(res));
130 131 132 133
	if (!rng_base) {
		ret = -ENOMEM;
		goto err_ioremap;
	}
134 135

	ret = hwrng_register(&omap_rng_ops);
136 137
	if (ret)
		goto err_register;
138

D
David Brownell 已提交
139
	dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n",
140 141 142
		omap_rng_read_reg(RNG_REV_REG));
	omap_rng_write_reg(RNG_MASK_REG, 0x1);

D
David Brownell 已提交
143
	rng_dev = pdev;
144 145

	return 0;
146 147 148 149 150

err_register:
	iounmap(rng_base);
	rng_base = NULL;
err_ioremap:
151
	release_mem_region(res->start, resource_size(res));
152 153 154 155 156 157
err_region:
	if (cpu_is_omap24xx()) {
		clk_disable(rng_ick);
		clk_put(rng_ick);
	}
	return ret;
158 159
}

D
David Brownell 已提交
160
static int __exit omap_rng_remove(struct platform_device *pdev)
161
{
162
	struct resource *res = dev_get_drvdata(&pdev->dev);
163 164 165 166 167

	hwrng_unregister(&omap_rng_ops);

	omap_rng_write_reg(RNG_MASK_REG, 0x0);

168 169
	iounmap(rng_base);

170
	if (cpu_is_omap24xx()) {
D
David Brownell 已提交
171
		clk_disable(rng_ick);
172 173 174
		clk_put(rng_ick);
	}

175
	release_mem_region(res->start, resource_size(res));
176 177 178 179 180 181 182
	rng_base = NULL;

	return 0;
}

#ifdef CONFIG_PM

D
David Brownell 已提交
183
static int omap_rng_suspend(struct platform_device *pdev, pm_message_t message)
184 185 186 187 188
{
	omap_rng_write_reg(RNG_MASK_REG, 0x0);
	return 0;
}

D
David Brownell 已提交
189
static int omap_rng_resume(struct platform_device *pdev)
190 191
{
	omap_rng_write_reg(RNG_MASK_REG, 0x1);
D
David Brownell 已提交
192
	return 0;
193 194 195 196 197 198 199 200 201
}

#else

#define	omap_rng_suspend	NULL
#define	omap_rng_resume		NULL

#endif

D
David Brownell 已提交
202 203
/* work with hotplug and coldplug */
MODULE_ALIAS("platform:omap_rng");
204

D
David Brownell 已提交
205 206 207 208 209
static struct platform_driver omap_rng_driver = {
	.driver = {
		.name		= "omap_rng",
		.owner		= THIS_MODULE,
	},
210 211 212 213 214 215 216 217 218 219 220
	.probe		= omap_rng_probe,
	.remove		= __exit_p(omap_rng_remove),
	.suspend	= omap_rng_suspend,
	.resume		= omap_rng_resume
};

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

D
David Brownell 已提交
221
	return platform_driver_register(&omap_rng_driver);
222 223 224 225
}

static void __exit omap_rng_exit(void)
{
D
David Brownell 已提交
226
	platform_driver_unregister(&omap_rng_driver);
227 228 229 230 231 232 233
}

module_init(omap_rng_init);
module_exit(omap_rng_exit);

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