Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * drivers/char/hw_random/timeriomem-rng.c
  3 *
  4 * Copyright (C) 2009 Alexander Clouter <alex@digriz.org.uk>
  5 *
  6 * Derived from drivers/char/hw_random/omap-rng.c
  7 *   Copyright 2005 (c) MontaVista Software, Inc.
  8 *   Author: Deepak Saxena <dsaxena@plexity.net>
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License version 2 as
 12 * published by the Free Software Foundation.
 13 *
 14 * Overview:
 15 *   This driver is useful for platforms that have an IO range that provides
 16 *   periodic random data from a single IO memory address.  All the platform
 17 *   has to do is provide the address and 'wait time' that new data becomes
 18 *   available.
 19 *
 20 * TODO: add support for reading sizes other than 32bits and masking
 21 */
 22
 23#include <linux/module.h>
 24#include <linux/kernel.h>
 25#include <linux/platform_device.h>
 
 26#include <linux/hw_random.h>
 27#include <linux/io.h>
 
 28#include <linux/timeriomem-rng.h>
 29#include <linux/jiffies.h>
 30#include <linux/sched.h>
 31#include <linux/timer.h>
 32#include <linux/completion.h>
 33
 34static struct timeriomem_rng_data *timeriomem_rng_data;
 
 
 
 
 35
 36static void timeriomem_rng_trigger(unsigned long);
 37static DEFINE_TIMER(timeriomem_rng_timer, timeriomem_rng_trigger, 0, 0);
 
 
 
 
 
 
 38
 39/*
 40 * have data return 1, however return 0 if we have nothing
 41 */
 42static int timeriomem_rng_data_present(struct hwrng *rng, int wait)
 43{
 44	if (rng->priv == 0)
 45		return 1;
 46
 47	if (!wait || timeriomem_rng_data->present)
 48		return timeriomem_rng_data->present;
 49
 50	wait_for_completion(&timeriomem_rng_data->completion);
 51
 52	return 1;
 53}
 54
 55static int timeriomem_rng_data_read(struct hwrng *rng, u32 *data)
 56{
 
 57	unsigned long cur;
 58	s32 delay;
 59
 60	*data = readl(timeriomem_rng_data->address);
 61
 62	if (rng->priv != 0) {
 63		cur = jiffies;
 64
 65		delay = cur - timeriomem_rng_timer.expires;
 66		delay = rng->priv - (delay % rng->priv);
 67
 68		timeriomem_rng_timer.expires = cur + delay;
 69		timeriomem_rng_data->present = 0;
 70
 71		init_completion(&timeriomem_rng_data->completion);
 72		add_timer(&timeriomem_rng_timer);
 73	}
 74
 75	return 4;
 76}
 77
 78static void timeriomem_rng_trigger(unsigned long dummy)
 79{
 80	timeriomem_rng_data->present = 1;
 81	complete(&timeriomem_rng_data->completion);
 82}
 83
 84static struct hwrng timeriomem_rng_ops = {
 85	.name		= "timeriomem",
 86	.data_present	= timeriomem_rng_data_present,
 87	.data_read	= timeriomem_rng_data_read,
 88	.priv		= 0,
 89};
 90
 91static int __devinit timeriomem_rng_probe(struct platform_device *pdev)
 92{
 
 
 93	struct resource *res;
 94	int ret;
 
 95
 96	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 
 
 
 97
 
 98	if (!res)
 99		return -ENOENT;
 
 
 
 
 
 
100
101	timeriomem_rng_data = pdev->dev.platform_data;
 
 
 
 
 
 
 
 
102
103	timeriomem_rng_data->address = ioremap(res->start, resource_size(res));
104	if (!timeriomem_rng_data->address)
105		return -EIO;
106
107	if (timeriomem_rng_data->period != 0
108		&& usecs_to_jiffies(timeriomem_rng_data->period) > 0) {
109		timeriomem_rng_timer.expires = jiffies;
 
 
 
 
 
 
 
110
111		timeriomem_rng_ops.priv = usecs_to_jiffies(
112						timeriomem_rng_data->period);
 
 
113	}
114	timeriomem_rng_data->present = 1;
115
116	ret = hwrng_register(&timeriomem_rng_ops);
117	if (ret)
118		goto failed;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
120	dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
121			timeriomem_rng_data->address,
122			timeriomem_rng_data->period);
123
124	return 0;
125
126failed:
127	dev_err(&pdev->dev, "problem registering\n");
128	iounmap(timeriomem_rng_data->address);
129
130	return ret;
131}
132
133static int __devexit timeriomem_rng_remove(struct platform_device *pdev)
134{
135	del_timer_sync(&timeriomem_rng_timer);
136	hwrng_unregister(&timeriomem_rng_ops);
137
138	iounmap(timeriomem_rng_data->address);
 
 
139
140	return 0;
141}
142
 
 
 
 
 
 
143static struct platform_driver timeriomem_rng_driver = {
144	.driver = {
145		.name		= "timeriomem_rng",
146		.owner		= THIS_MODULE,
 
147	},
148	.probe		= timeriomem_rng_probe,
149	.remove		= __devexit_p(timeriomem_rng_remove),
150};
151
152static int __init timeriomem_rng_init(void)
153{
154	return platform_driver_register(&timeriomem_rng_driver);
155}
156
157static void __exit timeriomem_rng_exit(void)
158{
159	platform_driver_unregister(&timeriomem_rng_driver);
160}
161
162module_init(timeriomem_rng_init);
163module_exit(timeriomem_rng_exit);
164
165MODULE_LICENSE("GPL");
166MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
167MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver");
v3.15
  1/*
  2 * drivers/char/hw_random/timeriomem-rng.c
  3 *
  4 * Copyright (C) 2009 Alexander Clouter <alex@digriz.org.uk>
  5 *
  6 * Derived from drivers/char/hw_random/omap-rng.c
  7 *   Copyright 2005 (c) MontaVista Software, Inc.
  8 *   Author: Deepak Saxena <dsaxena@plexity.net>
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License version 2 as
 12 * published by the Free Software Foundation.
 13 *
 14 * Overview:
 15 *   This driver is useful for platforms that have an IO range that provides
 16 *   periodic random data from a single IO memory address.  All the platform
 17 *   has to do is provide the address and 'wait time' that new data becomes
 18 *   available.
 19 *
 20 * TODO: add support for reading sizes other than 32bits and masking
 21 */
 22
 23#include <linux/module.h>
 24#include <linux/kernel.h>
 25#include <linux/platform_device.h>
 26#include <linux/of.h>
 27#include <linux/hw_random.h>
 28#include <linux/io.h>
 29#include <linux/slab.h>
 30#include <linux/timeriomem-rng.h>
 31#include <linux/jiffies.h>
 32#include <linux/sched.h>
 33#include <linux/timer.h>
 34#include <linux/completion.h>
 35
 36struct timeriomem_rng_private_data {
 37	void __iomem		*io_base;
 38	unsigned int		expires;
 39	unsigned int		period;
 40	unsigned int		present:1;
 41
 42	struct timer_list	timer;
 43	struct completion	completion;
 44
 45	struct hwrng		timeriomem_rng_ops;
 46};
 47
 48#define to_rng_priv(rng) \
 49		((struct timeriomem_rng_private_data *)rng->priv)
 50
 51/*
 52 * have data return 1, however return 0 if we have nothing
 53 */
 54static int timeriomem_rng_data_present(struct hwrng *rng, int wait)
 55{
 56	struct timeriomem_rng_private_data *priv = to_rng_priv(rng);
 
 57
 58	if (!wait || priv->present)
 59		return priv->present;
 60
 61	wait_for_completion(&priv->completion);
 62
 63	return 1;
 64}
 65
 66static int timeriomem_rng_data_read(struct hwrng *rng, u32 *data)
 67{
 68	struct timeriomem_rng_private_data *priv = to_rng_priv(rng);
 69	unsigned long cur;
 70	s32 delay;
 71
 72	*data = readl(priv->io_base);
 73
 74	cur = jiffies;
 
 75
 76	delay = cur - priv->expires;
 77	delay = priv->period - (delay % priv->period);
 78
 79	priv->expires = cur + delay;
 80	priv->present = 0;
 81
 82	reinit_completion(&priv->completion);
 83	mod_timer(&priv->timer, priv->expires);
 
 84
 85	return 4;
 86}
 87
 88static void timeriomem_rng_trigger(unsigned long data)
 89{
 90	struct timeriomem_rng_private_data *priv
 91			= (struct timeriomem_rng_private_data *)data;
 
 92
 93	priv->present = 1;
 94	complete(&priv->completion);
 95}
 
 
 
 96
 97static int timeriomem_rng_probe(struct platform_device *pdev)
 98{
 99	struct timeriomem_rng_data *pdata = pdev->dev.platform_data;
100	struct timeriomem_rng_private_data *priv;
101	struct resource *res;
102	int err = 0;
103	int period;
104
105	if (!pdev->dev.of_node && !pdata) {
106		dev_err(&pdev->dev, "timeriomem_rng_data is missing\n");
107		return -EINVAL;
108	}
109
110	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
111	if (!res)
112		return -ENXIO;
113
114	if (res->start % 4 != 0 || resource_size(res) != 4) {
115		dev_err(&pdev->dev,
116			"address must be four bytes wide and aligned\n");
117		return -EINVAL;
118	}
119
120	/* Allocate memory for the device structure (and zero it) */
121	priv = devm_kzalloc(&pdev->dev,
122			sizeof(struct timeriomem_rng_private_data), GFP_KERNEL);
123	if (!priv) {
124		dev_err(&pdev->dev, "failed to allocate device structure.\n");
125		return -ENOMEM;
126	}
127
128	platform_set_drvdata(pdev, priv);
129
130	if (pdev->dev.of_node) {
131		int i;
 
132
133		if (!of_property_read_u32(pdev->dev.of_node,
134						"period", &i))
135			period = i;
136		else {
137			dev_err(&pdev->dev, "missing period\n");
138			return -EINVAL;
139		}
140	} else {
141		period = pdata->period;
142	}
143
144	priv->period = usecs_to_jiffies(period);
145	if (priv->period < 1) {
146		dev_err(&pdev->dev, "period is less than one jiffy\n");
147		return -EINVAL;
148	}
 
149
150	priv->expires	= jiffies;
151	priv->present	= 1;
152
153	init_completion(&priv->completion);
154	complete(&priv->completion);
155
156	setup_timer(&priv->timer, timeriomem_rng_trigger, (unsigned long)priv);
157
158	priv->timeriomem_rng_ops.name		= dev_name(&pdev->dev);
159	priv->timeriomem_rng_ops.data_present	= timeriomem_rng_data_present;
160	priv->timeriomem_rng_ops.data_read	= timeriomem_rng_data_read;
161	priv->timeriomem_rng_ops.priv		= (unsigned long)priv;
162
163	priv->io_base = devm_ioremap_resource(&pdev->dev, res);
164	if (IS_ERR(priv->io_base)) {
165		err = PTR_ERR(priv->io_base);
166		goto out_timer;
167	}
168
169	err = hwrng_register(&priv->timeriomem_rng_ops);
170	if (err) {
171		dev_err(&pdev->dev, "problem registering\n");
172		goto out_timer;
173	}
174
175	dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
176			priv->io_base, period);
 
177
178	return 0;
179
180out_timer:
181	del_timer_sync(&priv->timer);
182	return err;
 
 
183}
184
185static int timeriomem_rng_remove(struct platform_device *pdev)
186{
187	struct timeriomem_rng_private_data *priv = platform_get_drvdata(pdev);
 
188
189	hwrng_unregister(&priv->timeriomem_rng_ops);
190
191	del_timer_sync(&priv->timer);
192
193	return 0;
194}
195
196static const struct of_device_id timeriomem_rng_match[] = {
197	{ .compatible = "timeriomem_rng" },
198	{},
199};
200MODULE_DEVICE_TABLE(of, timeriomem_rng_match);
201
202static struct platform_driver timeriomem_rng_driver = {
203	.driver = {
204		.name		= "timeriomem_rng",
205		.owner		= THIS_MODULE,
206		.of_match_table	= timeriomem_rng_match,
207	},
208	.probe		= timeriomem_rng_probe,
209	.remove		= timeriomem_rng_remove,
210};
211
212module_platform_driver(timeriomem_rng_driver);
 
 
 
 
 
 
 
 
 
 
 
213
214MODULE_LICENSE("GPL");
215MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
216MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver");