omap-rng.c 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * driver/char/hw_random/omap-rng.c
 *
 * RNG driver for TI OMAP CPU family
 *
 * Author: Deepak Saxena <dsaxena@plexity.net>
 *
 * Copyright 2005 (c) MontaVista Software, Inc.
 *
 * Mostly based on original driver:
 *
 * Copyright (C) 2005 Nokia Corporation
 * Author: Juha Yrj��<juha.yrjola@nokia.com>
 *
 * 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.
 *
 * TODO:
 *
 * - Make status updated be interrupt driven so we don't poll
 *
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/random.h>
D
David Brownell 已提交
28
#include <linux/clk.h>
29
#include <linux/err.h>
D
David Brownell 已提交
30
#include <linux/platform_device.h>
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <linux/hw_random.h>

#include <asm/io.h>

#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 已提交
55
static struct platform_device *rng_dev;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

static u32 omap_rng_read_reg(int reg)
{
	return __raw_readl(rng_base + reg);
}

static void omap_rng_write_reg(int reg, u32 val)
{
	__raw_writel(val, rng_base + reg);
}

/* REVISIT: Does the status bit really work on 16xx? */
static int omap_rng_data_present(struct hwrng *rng)
{
	return omap_rng_read_reg(RNG_STAT_REG) ? 0 : 1;
}

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,
};

D
David Brownell 已提交
86
static int __init omap_rng_probe(struct platform_device *pdev)
87 88 89 90 91 92 93 94 95 96
{
	struct resource *res, *mem;
	int ret;

	/*
	 * A bit ugly, and it will never actually happen but there can
	 * be only one RNG and this catches any bork
	 */
	BUG_ON(rng_dev);

D
David Brownell 已提交
97
	if (cpu_is_omap24xx()) {
98 99
		rng_ick = clk_get(NULL, "rng_ick");
		if (IS_ERR(rng_ick)) {
D
David Brownell 已提交
100
			dev_err(&pdev->dev, "Could not get rng_ick\n");
101 102
			ret = PTR_ERR(rng_ick);
			return ret;
D
David Brownell 已提交
103 104
		} else
			clk_enable(rng_ick);
105 106 107 108 109 110 111 112 113 114 115 116
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

	if (!res)
		return -ENOENT;

	mem = request_mem_region(res->start, res->end - res->start + 1,
				 pdev->name);
	if (mem == NULL)
		return -EBUSY;

D
David Brownell 已提交
117
	dev_set_drvdata(&pdev->dev, mem);
118 119 120 121 122 123 124 125 126
	rng_base = (u32 __iomem *)io_p2v(res->start);

	ret = hwrng_register(&omap_rng_ops);
	if (ret) {
		release_resource(mem);
		rng_base = NULL;
		return ret;
	}

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

D
David Brownell 已提交
131
	rng_dev = pdev;
132 133 134 135

	return 0;
}

D
David Brownell 已提交
136
static int __exit omap_rng_remove(struct platform_device *pdev)
137
{
D
David Brownell 已提交
138
	struct resource *mem = dev_get_drvdata(&pdev->dev);
139 140 141 142 143 144

	hwrng_unregister(&omap_rng_ops);

	omap_rng_write_reg(RNG_MASK_REG, 0x0);

	if (cpu_is_omap24xx()) {
D
David Brownell 已提交
145
		clk_disable(rng_ick);
146 147 148 149 150 151 152 153 154 155 156
		clk_put(rng_ick);
	}

	release_resource(mem);
	rng_base = NULL;

	return 0;
}

#ifdef CONFIG_PM

D
David Brownell 已提交
157
static int omap_rng_suspend(struct platform_device *pdev, pm_message_t message)
158 159 160 161 162
{
	omap_rng_write_reg(RNG_MASK_REG, 0x0);
	return 0;
}

D
David Brownell 已提交
163
static int omap_rng_resume(struct platform_device *pdev)
164 165
{
	omap_rng_write_reg(RNG_MASK_REG, 0x1);
D
David Brownell 已提交
166
	return 0;
167 168 169 170 171 172 173 174 175 176
}

#else

#define	omap_rng_suspend	NULL
#define	omap_rng_resume		NULL

#endif


D
David Brownell 已提交
177 178 179 180 181
static struct platform_driver omap_rng_driver = {
	.driver = {
		.name		= "omap_rng",
		.owner		= THIS_MODULE,
	},
182 183 184 185 186 187 188 189 190 191 192
	.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 已提交
193
	return platform_driver_register(&omap_rng_driver);
194 195 196 197
}

static void __exit omap_rng_exit(void)
{
D
David Brownell 已提交
198
	platform_driver_unregister(&omap_rng_driver);
199 200 201 202 203 204 205
}

module_init(omap_rng_init);
module_exit(omap_rng_exit);

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