Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  2 * omap3-rom-rng.c - RNG driver for TI OMAP3 CPU family
  3 *
  4 * Copyright (C) 2009 Nokia Corporation
  5 * Author: Juha Yrjola <juha.yrjola@solidboot.com>
  6 *
  7 * Copyright (C) 2013 Pali Rohár <pali.rohar@gmail.com>
  8 *
  9 * This file is licensed under  the terms of the GNU General Public
 10 * License version 2. This program is licensed "as is" without any
 11 * warranty of any kind, whether express or implied.
 12 */
 13
 14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 15
 16#include <linux/module.h>
 17#include <linux/init.h>
 18#include <linux/random.h>
 19#include <linux/hw_random.h>
 20#include <linux/workqueue.h>
 21#include <linux/clk.h>
 22#include <linux/err.h>
 
 
 
 23#include <linux/platform_device.h>
 
 24
 25#define RNG_RESET			0x01
 26#define RNG_GEN_PRNG_HW_INIT		0x02
 27#define RNG_GEN_HW			0x08
 28
 29/* param1: ptr, param2: count, param3: flag */
 30static u32 (*omap3_rom_rng_call)(u32, u32, u32);
 31
 32static struct delayed_work idle_work;
 33static int rng_idle;
 34static struct clk *rng_clk;
 35
 36static void omap3_rom_rng_idle(struct work_struct *work)
 37{
 
 
 38	int r;
 39
 40	r = omap3_rom_rng_call(0, 0, RNG_RESET);
 41	if (r != 0) {
 42		pr_err("reset failed: %d\n", r);
 43		return;
 44	}
 45	clk_disable_unprepare(rng_clk);
 46	rng_idle = 1;
 47}
 48
 49static int omap3_rom_rng_get_random(void *buf, unsigned int count)
 50{
 51	u32 r;
 52	u32 ptr;
 53
 54	cancel_delayed_work_sync(&idle_work);
 55	if (rng_idle) {
 56		clk_prepare_enable(rng_clk);
 57		r = omap3_rom_rng_call(0, 0, RNG_GEN_PRNG_HW_INIT);
 58		if (r != 0) {
 59			clk_disable_unprepare(rng_clk);
 60			pr_err("HW init failed: %d\n", r);
 61			return -EIO;
 62		}
 63		rng_idle = 0;
 64	}
 65
 66	ptr = virt_to_phys(buf);
 67	r = omap3_rom_rng_call(ptr, count, RNG_GEN_HW);
 68	schedule_delayed_work(&idle_work, msecs_to_jiffies(500));
 69	if (r != 0)
 70		return -EINVAL;
 71	return 0;
 
 
 
 
 
 
 72}
 73
 74static int omap3_rom_rng_data_present(struct hwrng *rng, int wait)
 75{
 76	return 1;
 
 
 
 
 
 
 
 
 
 
 
 77}
 78
 79static int omap3_rom_rng_data_read(struct hwrng *rng, u32 *data)
 80{
 
 81	int r;
 82
 83	r = omap3_rom_rng_get_random(data, 4);
 
 
 84	if (r < 0)
 85		return r;
 86	return 4;
 
 
 
 
 
 
 
 
 
 87}
 88
 89static struct hwrng omap3_rom_rng_ops = {
 90	.name		= "omap3-rom",
 91	.data_present	= omap3_rom_rng_data_present,
 92	.data_read	= omap3_rom_rng_data_read,
 93};
 
 
 94
 95static int omap3_rom_rng_probe(struct platform_device *pdev)
 96{
 97	pr_info("initializing\n");
 
 98
 99	omap3_rom_rng_call = pdev->dev.platform_data;
100	if (!omap3_rom_rng_call) {
101		pr_err("omap3_rom_rng_call is NULL\n");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102		return -EINVAL;
103	}
104
105	INIT_DELAYED_WORK(&idle_work, omap3_rom_rng_idle);
106	rng_clk = devm_clk_get(&pdev->dev, "ick");
107	if (IS_ERR(rng_clk)) {
108		pr_err("unable to get RNG clock\n");
109		return PTR_ERR(rng_clk);
110	}
111
112	/* Leave the RNG in reset state. */
113	clk_prepare_enable(rng_clk);
114	omap3_rom_rng_idle(0);
115
116	return hwrng_register(&omap3_rom_rng_ops);
117}
 
 
118
119static int omap3_rom_rng_remove(struct platform_device *pdev)
120{
121	cancel_delayed_work_sync(&idle_work);
122	hwrng_unregister(&omap3_rom_rng_ops);
123	clk_disable_unprepare(rng_clk);
124	return 0;
125}
126
 
 
 
 
 
 
 
 
 
 
 
127static struct platform_driver omap3_rom_rng_driver = {
128	.driver = {
129		.name		= "omap3-rom-rng",
 
 
130	},
131	.probe		= omap3_rom_rng_probe,
132	.remove		= omap3_rom_rng_remove,
133};
134
135module_platform_driver(omap3_rom_rng_driver);
136
137MODULE_ALIAS("platform:omap3-rom-rng");
138MODULE_AUTHOR("Juha Yrjola");
139MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
140MODULE_LICENSE("GPL");
v6.2
  1/*
  2 * omap3-rom-rng.c - RNG driver for TI OMAP3 CPU family
  3 *
  4 * Copyright (C) 2009 Nokia Corporation
  5 * Author: Juha Yrjola <juha.yrjola@solidboot.com>
  6 *
  7 * Copyright (C) 2013 Pali Rohár <pali@kernel.org>
  8 *
  9 * This file is licensed under  the terms of the GNU General Public
 10 * License version 2. This program is licensed "as is" without any
 11 * warranty of any kind, whether express or implied.
 12 */
 13
 
 
 14#include <linux/module.h>
 15#include <linux/init.h>
 16#include <linux/random.h>
 17#include <linux/hw_random.h>
 18#include <linux/workqueue.h>
 19#include <linux/clk.h>
 20#include <linux/err.h>
 21#include <linux/io.h>
 22#include <linux/of.h>
 23#include <linux/of_device.h>
 24#include <linux/platform_device.h>
 25#include <linux/pm_runtime.h>
 26
 27#define RNG_RESET			0x01
 28#define RNG_GEN_PRNG_HW_INIT		0x02
 29#define RNG_GEN_HW			0x08
 30
 31struct omap_rom_rng {
 32	struct clk *clk;
 33	struct device *dev;
 34	struct hwrng ops;
 35	u32 (*rom_rng_call)(u32 ptr, u32 count, u32 flag);
 36};
 37
 38static int omap3_rom_rng_read(struct hwrng *rng, void *data, size_t max, bool w)
 39{
 40	struct omap_rom_rng *ddata;
 41	u32 ptr;
 42	int r;
 43
 44	ddata = (struct omap_rom_rng *)rng->priv;
 
 
 
 
 
 
 
 45
 46	r = pm_runtime_get_sync(ddata->dev);
 47	if (r < 0) {
 48		pm_runtime_put_noidle(ddata->dev);
 
 49
 50		return r;
 
 
 
 
 
 
 
 
 
 51	}
 52
 53	ptr = virt_to_phys(data);
 54	r = ddata->rom_rng_call(ptr, 4, RNG_GEN_HW);
 
 55	if (r != 0)
 56		r = -EINVAL;
 57	else
 58		r = 4;
 59
 60	pm_runtime_mark_last_busy(ddata->dev);
 61	pm_runtime_put_autosuspend(ddata->dev);
 62
 63	return r;
 64}
 65
 66static int __maybe_unused omap_rom_rng_runtime_suspend(struct device *dev)
 67{
 68	struct omap_rom_rng *ddata;
 69	int r;
 70
 71	ddata = dev_get_drvdata(dev);
 72
 73	r = ddata->rom_rng_call(0, 0, RNG_RESET);
 74	if (r != 0)
 75		dev_err(dev, "reset failed: %d\n", r);
 76
 77	clk_disable_unprepare(ddata->clk);
 78
 79	return 0;
 80}
 81
 82static int __maybe_unused omap_rom_rng_runtime_resume(struct device *dev)
 83{
 84	struct omap_rom_rng *ddata;
 85	int r;
 86
 87	ddata = dev_get_drvdata(dev);
 88
 89	r = clk_prepare_enable(ddata->clk);
 90	if (r < 0)
 91		return r;
 92
 93	r = ddata->rom_rng_call(0, 0, RNG_GEN_PRNG_HW_INIT);
 94	if (r != 0) {
 95		clk_disable_unprepare(ddata->clk);
 96		dev_err(dev, "HW init failed: %d\n", r);
 97
 98		return -EIO;
 99	}
100
101	return 0;
102}
103
104static void omap_rom_rng_finish(void *data)
105{
106	struct omap_rom_rng *ddata = data;
107
108	pm_runtime_dont_use_autosuspend(ddata->dev);
109	pm_runtime_disable(ddata->dev);
110}
111
112static int omap3_rom_rng_probe(struct platform_device *pdev)
113{
114	struct omap_rom_rng *ddata;
115	int ret = 0;
116
117	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
118	if (!ddata)
119		return -ENOMEM;
120
121	ddata->dev = &pdev->dev;
122	ddata->ops.priv = (unsigned long)ddata;
123	ddata->ops.name = "omap3-rom";
124	ddata->ops.read = of_device_get_match_data(&pdev->dev);
125	ddata->ops.quality = 900;
126	if (!ddata->ops.read) {
127		dev_err(&pdev->dev, "missing rom code handler\n");
128
129		return -ENODEV;
130	}
131	dev_set_drvdata(ddata->dev, ddata);
132
133	ddata->rom_rng_call = pdev->dev.platform_data;
134	if (!ddata->rom_rng_call) {
135		dev_err(ddata->dev, "rom_rng_call is NULL\n");
136		return -EINVAL;
137	}
138
139	ddata->clk = devm_clk_get(ddata->dev, "ick");
140	if (IS_ERR(ddata->clk)) {
141		dev_err(ddata->dev, "unable to get RNG clock\n");
142		return PTR_ERR(ddata->clk);
 
143	}
144
145	pm_runtime_enable(&pdev->dev);
146	pm_runtime_set_autosuspend_delay(&pdev->dev, 500);
147	pm_runtime_use_autosuspend(&pdev->dev);
148
149	ret = devm_add_action_or_reset(ddata->dev, omap_rom_rng_finish,
150				       ddata);
151	if (ret)
152		return ret;
153
154	return devm_hwrng_register(ddata->dev, &ddata->ops);
 
 
 
 
 
155}
156
157static const struct of_device_id omap_rom_rng_match[] = {
158	{ .compatible = "nokia,n900-rom-rng", .data = omap3_rom_rng_read, },
159	{ /* sentinel */ },
160};
161MODULE_DEVICE_TABLE(of, omap_rom_rng_match);
162
163static const struct dev_pm_ops omap_rom_rng_pm_ops = {
164	SET_SYSTEM_SLEEP_PM_OPS(omap_rom_rng_runtime_suspend,
165				omap_rom_rng_runtime_resume)
166};
167
168static struct platform_driver omap3_rom_rng_driver = {
169	.driver = {
170		.name		= "omap3-rom-rng",
171		.of_match_table = omap_rom_rng_match,
172		.pm = &omap_rom_rng_pm_ops,
173	},
174	.probe		= omap3_rom_rng_probe,
 
175};
176
177module_platform_driver(omap3_rom_rng_driver);
178
179MODULE_ALIAS("platform:omap3-rom-rng");
180MODULE_AUTHOR("Juha Yrjola");
181MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
182MODULE_LICENSE("GPL");