Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * drivers/char/hw_random/timeriomem-rng.c
  4 *
  5 * Copyright (C) 2009 Alexander Clouter <alex@digriz.org.uk>
  6 *
  7 * Derived from drivers/char/hw_random/omap-rng.c
  8 *   Copyright 2005 (c) MontaVista Software, Inc.
  9 *   Author: Deepak Saxena <dsaxena@plexity.net>
 10 *
 
 
 
 
 11 * Overview:
 12 *   This driver is useful for platforms that have an IO range that provides
 13 *   periodic random data from a single IO memory address.  All the platform
 14 *   has to do is provide the address and 'wait time' that new data becomes
 15 *   available.
 16 *
 17 * TODO: add support for reading sizes other than 32bits and masking
 18 */
 19
 20#include <linux/completion.h>
 21#include <linux/delay.h>
 22#include <linux/hrtimer.h>
 23#include <linux/hw_random.h>
 24#include <linux/io.h>
 25#include <linux/ktime.h>
 26#include <linux/module.h>
 27#include <linux/of.h>
 28#include <linux/platform_device.h>
 29#include <linux/slab.h>
 30#include <linux/time.h>
 31#include <linux/timeriomem-rng.h>
 32
 33struct timeriomem_rng_private {
 34	void __iomem		*io_base;
 35	ktime_t			period;
 36	unsigned int		present:1;
 37
 38	struct hrtimer		timer;
 39	struct completion	completion;
 40
 41	struct hwrng		rng_ops;
 42};
 43
 44static int timeriomem_rng_read(struct hwrng *hwrng, void *data,
 45				size_t max, bool wait)
 46{
 47	struct timeriomem_rng_private *priv =
 48		container_of(hwrng, struct timeriomem_rng_private, rng_ops);
 49	int retval = 0;
 50	int period_us = ktime_to_us(priv->period);
 51
 52	/*
 53	 * There may not have been enough time for new data to be generated
 54	 * since the last request.  If the caller doesn't want to wait, let them
 55	 * bail out.  Otherwise, wait for the completion.  If the new data has
 56	 * already been generated, the completion should already be available.
 57	 */
 58	if (!wait && !priv->present)
 59		return 0;
 60
 61	wait_for_completion(&priv->completion);
 62
 63	do {
 64		/*
 65		 * After the first read, all additional reads will need to wait
 66		 * for the RNG to generate new data.  Since the period can have
 67		 * a wide range of values (1us to 1s have been observed), allow
 68		 * for 1% tolerance in the sleep time rather than a fixed value.
 69		 */
 70		if (retval > 0)
 71			usleep_range(period_us,
 72					period_us + max(1, period_us / 100));
 73
 74		*(u32 *)data = readl(priv->io_base);
 75		retval += sizeof(u32);
 76		data += sizeof(u32);
 77		max -= sizeof(u32);
 78	} while (wait && max > sizeof(u32));
 79
 80	/*
 81	 * Block any new callers until the RNG has had time to generate new
 82	 * data.
 83	 */
 84	priv->present = 0;
 85	reinit_completion(&priv->completion);
 86	hrtimer_forward_now(&priv->timer, priv->period);
 87	hrtimer_restart(&priv->timer);
 88
 89	return retval;
 90}
 91
 92static enum hrtimer_restart timeriomem_rng_trigger(struct hrtimer *timer)
 93{
 94	struct timeriomem_rng_private *priv
 95		= container_of(timer, struct timeriomem_rng_private, timer);
 96
 97	priv->present = 1;
 98	complete(&priv->completion);
 99
100	return HRTIMER_NORESTART;
101}
102
103static int timeriomem_rng_probe(struct platform_device *pdev)
104{
105	struct timeriomem_rng_data *pdata = pdev->dev.platform_data;
106	struct timeriomem_rng_private *priv;
107	struct resource *res;
108	int err = 0;
109	int period;
110
111	if (!pdev->dev.of_node && !pdata) {
112		dev_err(&pdev->dev, "timeriomem_rng_data is missing\n");
113		return -EINVAL;
114	}
115
 
 
 
 
 
 
 
 
 
 
116	/* Allocate memory for the device structure (and zero it) */
117	priv = devm_kzalloc(&pdev->dev,
118			sizeof(struct timeriomem_rng_private), GFP_KERNEL);
119	if (!priv)
120		return -ENOMEM;
121
122	platform_set_drvdata(pdev, priv);
123
124	priv->io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
125	if (IS_ERR(priv->io_base))
126		return PTR_ERR(priv->io_base);
127
128	if (res->start % 4 != 0 || resource_size(res) < 4) {
129		dev_err(&pdev->dev,
130			"address must be at least four bytes wide and 32-bit aligned\n");
131		return -EINVAL;
132	}
133
134	if (pdev->dev.of_node) {
135		int i;
136
137		if (!of_property_read_u32(pdev->dev.of_node,
138						"period", &i))
139			period = i;
140		else {
141			dev_err(&pdev->dev, "missing period\n");
142			return -EINVAL;
143		}
144
145		if (!of_property_read_u32(pdev->dev.of_node,
146						"quality", &i))
147			priv->rng_ops.quality = i;
 
 
148	} else {
149		period = pdata->period;
150		priv->rng_ops.quality = pdata->quality;
151	}
152
153	priv->period = ns_to_ktime(period * NSEC_PER_USEC);
154	init_completion(&priv->completion);
155	hrtimer_init(&priv->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
156	priv->timer.function = timeriomem_rng_trigger;
157
158	priv->rng_ops.name = dev_name(&pdev->dev);
159	priv->rng_ops.read = timeriomem_rng_read;
160
 
 
 
 
 
161	/* Assume random data is already available. */
162	priv->present = 1;
163	complete(&priv->completion);
164
165	err = devm_hwrng_register(&pdev->dev, &priv->rng_ops);
166	if (err) {
167		dev_err(&pdev->dev, "problem registering\n");
168		return err;
169	}
170
171	dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
172			priv->io_base, period);
173
174	return 0;
175}
176
177static void timeriomem_rng_remove(struct platform_device *pdev)
178{
179	struct timeriomem_rng_private *priv = platform_get_drvdata(pdev);
180
 
181	hrtimer_cancel(&priv->timer);
 
 
182}
183
184static const struct of_device_id timeriomem_rng_match[] = {
185	{ .compatible = "timeriomem_rng" },
186	{},
187};
188MODULE_DEVICE_TABLE(of, timeriomem_rng_match);
189
190static struct platform_driver timeriomem_rng_driver = {
191	.driver = {
192		.name		= "timeriomem_rng",
193		.of_match_table	= timeriomem_rng_match,
194	},
195	.probe		= timeriomem_rng_probe,
196	.remove_new	= timeriomem_rng_remove,
197};
198
199module_platform_driver(timeriomem_rng_driver);
200
201MODULE_LICENSE("GPL");
202MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
203MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver");
v4.17
 
  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/completion.h>
 24#include <linux/delay.h>
 25#include <linux/hrtimer.h>
 26#include <linux/hw_random.h>
 27#include <linux/io.h>
 28#include <linux/ktime.h>
 29#include <linux/module.h>
 30#include <linux/of.h>
 31#include <linux/platform_device.h>
 32#include <linux/slab.h>
 33#include <linux/time.h>
 34#include <linux/timeriomem-rng.h>
 35
 36struct timeriomem_rng_private {
 37	void __iomem		*io_base;
 38	ktime_t			period;
 39	unsigned int		present:1;
 40
 41	struct hrtimer		timer;
 42	struct completion	completion;
 43
 44	struct hwrng		rng_ops;
 45};
 46
 47static int timeriomem_rng_read(struct hwrng *hwrng, void *data,
 48				size_t max, bool wait)
 49{
 50	struct timeriomem_rng_private *priv =
 51		container_of(hwrng, struct timeriomem_rng_private, rng_ops);
 52	int retval = 0;
 53	int period_us = ktime_to_us(priv->period);
 54
 55	/*
 56	 * There may not have been enough time for new data to be generated
 57	 * since the last request.  If the caller doesn't want to wait, let them
 58	 * bail out.  Otherwise, wait for the completion.  If the new data has
 59	 * already been generated, the completion should already be available.
 60	 */
 61	if (!wait && !priv->present)
 62		return 0;
 63
 64	wait_for_completion(&priv->completion);
 65
 66	do {
 67		/*
 68		 * After the first read, all additional reads will need to wait
 69		 * for the RNG to generate new data.  Since the period can have
 70		 * a wide range of values (1us to 1s have been observed), allow
 71		 * for 1% tolerance in the sleep time rather than a fixed value.
 72		 */
 73		if (retval > 0)
 74			usleep_range(period_us,
 75					period_us + min(1, period_us / 100));
 76
 77		*(u32 *)data = readl(priv->io_base);
 78		retval += sizeof(u32);
 79		data += sizeof(u32);
 80		max -= sizeof(u32);
 81	} while (wait && max > sizeof(u32));
 82
 83	/*
 84	 * Block any new callers until the RNG has had time to generate new
 85	 * data.
 86	 */
 87	priv->present = 0;
 88	reinit_completion(&priv->completion);
 89	hrtimer_forward_now(&priv->timer, priv->period);
 90	hrtimer_restart(&priv->timer);
 91
 92	return retval;
 93}
 94
 95static enum hrtimer_restart timeriomem_rng_trigger(struct hrtimer *timer)
 96{
 97	struct timeriomem_rng_private *priv
 98		= container_of(timer, struct timeriomem_rng_private, timer);
 99
100	priv->present = 1;
101	complete(&priv->completion);
102
103	return HRTIMER_NORESTART;
104}
105
106static int timeriomem_rng_probe(struct platform_device *pdev)
107{
108	struct timeriomem_rng_data *pdata = pdev->dev.platform_data;
109	struct timeriomem_rng_private *priv;
110	struct resource *res;
111	int err = 0;
112	int period;
113
114	if (!pdev->dev.of_node && !pdata) {
115		dev_err(&pdev->dev, "timeriomem_rng_data is missing\n");
116		return -EINVAL;
117	}
118
119	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
120	if (!res)
121		return -ENXIO;
122
123	if (res->start % 4 != 0 || resource_size(res) != 4) {
124		dev_err(&pdev->dev,
125			"address must be four bytes wide and aligned\n");
126		return -EINVAL;
127	}
128
129	/* Allocate memory for the device structure (and zero it) */
130	priv = devm_kzalloc(&pdev->dev,
131			sizeof(struct timeriomem_rng_private), GFP_KERNEL);
132	if (!priv)
133		return -ENOMEM;
134
135	platform_set_drvdata(pdev, priv);
136
 
 
 
 
 
 
 
 
 
 
137	if (pdev->dev.of_node) {
138		int i;
139
140		if (!of_property_read_u32(pdev->dev.of_node,
141						"period", &i))
142			period = i;
143		else {
144			dev_err(&pdev->dev, "missing period\n");
145			return -EINVAL;
146		}
147
148		if (!of_property_read_u32(pdev->dev.of_node,
149						"quality", &i))
150			priv->rng_ops.quality = i;
151		else
152			priv->rng_ops.quality = 0;
153	} else {
154		period = pdata->period;
155		priv->rng_ops.quality = pdata->quality;
156	}
157
158	priv->period = ns_to_ktime(period * NSEC_PER_USEC);
159	init_completion(&priv->completion);
160	hrtimer_init(&priv->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
161	priv->timer.function = timeriomem_rng_trigger;
162
163	priv->rng_ops.name = dev_name(&pdev->dev);
164	priv->rng_ops.read = timeriomem_rng_read;
165
166	priv->io_base = devm_ioremap_resource(&pdev->dev, res);
167	if (IS_ERR(priv->io_base)) {
168		return PTR_ERR(priv->io_base);
169	}
170
171	/* Assume random data is already available. */
172	priv->present = 1;
173	complete(&priv->completion);
174
175	err = hwrng_register(&priv->rng_ops);
176	if (err) {
177		dev_err(&pdev->dev, "problem registering\n");
178		return err;
179	}
180
181	dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
182			priv->io_base, period);
183
184	return 0;
185}
186
187static int timeriomem_rng_remove(struct platform_device *pdev)
188{
189	struct timeriomem_rng_private *priv = platform_get_drvdata(pdev);
190
191	hwrng_unregister(&priv->rng_ops);
192	hrtimer_cancel(&priv->timer);
193
194	return 0;
195}
196
197static const struct of_device_id timeriomem_rng_match[] = {
198	{ .compatible = "timeriomem_rng" },
199	{},
200};
201MODULE_DEVICE_TABLE(of, timeriomem_rng_match);
202
203static struct platform_driver timeriomem_rng_driver = {
204	.driver = {
205		.name		= "timeriomem_rng",
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");