File Index
Files and directories
Sign in
FILE

omap3-rom-rng.c

File preview with syntax highlighting.

linux-6.19.14/drivers/char/hw_random/omap3-rom-rng.c C
  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/platform_device.h>
 24#include <linux/pm_runtime.h>
 25
 26#define RNG_RESET			0x01
 27#define RNG_GEN_PRNG_HW_INIT		0x02
 28#define RNG_GEN_HW			0x08
 29
 30struct omap_rom_rng {
 31	struct clk *clk;
 32	struct device *dev;
 33	struct hwrng ops;
 34	u32 (*rom_rng_call)(u32 ptr, u32 count, u32 flag);
 35};
 36
 37static int omap3_rom_rng_read(struct hwrng *rng, void *data, size_t max, bool w)
 38{
 39	struct omap_rom_rng *ddata;
 40	u32 ptr;
 41	int r;
 42
 43	ddata = (struct omap_rom_rng *)rng->priv;
 44
 45	r = pm_runtime_get_sync(ddata->dev);
 46	if (r < 0) {
 47		pm_runtime_put_noidle(ddata->dev);
 48
 49		return r;
 50	}
 51
 52	ptr = virt_to_phys(data);
 53	r = ddata->rom_rng_call(ptr, 4, RNG_GEN_HW);
 54	if (r != 0)
 55		r = -EINVAL;
 56	else
 57		r = 4;
 58
 59	pm_runtime_put_autosuspend(ddata->dev);
 60
 61	return r;
 62}
 63
 64static int __maybe_unused omap_rom_rng_runtime_suspend(struct device *dev)
 65{
 66	struct omap_rom_rng *ddata;
 67	int r;
 68
 69	ddata = dev_get_drvdata(dev);
 70
 71	r = ddata->rom_rng_call(0, 0, RNG_RESET);
 72	if (r != 0)
 73		dev_err(dev, "reset failed: %d\n", r);
 74
 75	clk_disable_unprepare(ddata->clk);
 76
 77	return 0;
 78}
 79
 80static int __maybe_unused omap_rom_rng_runtime_resume(struct device *dev)
 81{
 82	struct omap_rom_rng *ddata;
 83	int r;
 84
 85	ddata = dev_get_drvdata(dev);
 86
 87	r = clk_prepare_enable(ddata->clk);
 88	if (r < 0)
 89		return r;
 90
 91	r = ddata->rom_rng_call(0, 0, RNG_GEN_PRNG_HW_INIT);
 92	if (r != 0) {
 93		clk_disable_unprepare(ddata->clk);
 94		dev_err(dev, "HW init failed: %d\n", r);
 95
 96		return -EIO;
 97	}
 98
 99	return 0;
100}
101
102static void omap_rom_rng_finish(void *data)
103{
104	struct omap_rom_rng *ddata = data;
105
106	pm_runtime_dont_use_autosuspend(ddata->dev);
107	pm_runtime_disable(ddata->dev);
108}
109
110static int omap3_rom_rng_probe(struct platform_device *pdev)
111{
112	struct omap_rom_rng *ddata;
113	int ret = 0;
114
115	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
116	if (!ddata)
117		return -ENOMEM;
118
119	ddata->dev = &pdev->dev;
120	ddata->ops.priv = (unsigned long)ddata;
121	ddata->ops.name = "omap3-rom";
122	ddata->ops.read = of_device_get_match_data(&pdev->dev);
123	ddata->ops.quality = 900;
124	if (!ddata->ops.read) {
125		dev_err(&pdev->dev, "missing rom code handler\n");
126
127		return -ENODEV;
128	}
129	dev_set_drvdata(ddata->dev, ddata);
130
131	ddata->rom_rng_call = pdev->dev.platform_data;
132	if (!ddata->rom_rng_call) {
133		dev_err(ddata->dev, "rom_rng_call is NULL\n");
134		return -EINVAL;
135	}
136
137	ddata->clk = devm_clk_get(ddata->dev, "ick");
138	if (IS_ERR(ddata->clk)) {
139		dev_err(ddata->dev, "unable to get RNG clock\n");
140		return PTR_ERR(ddata->clk);
141	}
142
143	pm_runtime_enable(&pdev->dev);
144	pm_runtime_set_autosuspend_delay(&pdev->dev, 500);
145	pm_runtime_use_autosuspend(&pdev->dev);
146
147	ret = devm_add_action_or_reset(ddata->dev, omap_rom_rng_finish,
148				       ddata);
149	if (ret)
150		return ret;
151
152	return devm_hwrng_register(ddata->dev, &ddata->ops);
153}
154
155static const struct of_device_id omap_rom_rng_match[] = {
156	{ .compatible = "nokia,n900-rom-rng", .data = omap3_rom_rng_read, },
157	{ /* sentinel */ },
158};
159MODULE_DEVICE_TABLE(of, omap_rom_rng_match);
160
161static const struct dev_pm_ops omap_rom_rng_pm_ops = {
162	SET_SYSTEM_SLEEP_PM_OPS(omap_rom_rng_runtime_suspend,
163				omap_rom_rng_runtime_resume)
164};
165
166static struct platform_driver omap3_rom_rng_driver = {
167	.driver = {
168		.name		= "omap3-rom-rng",
169		.of_match_table = omap_rom_rng_match,
170		.pm = &omap_rom_rng_pm_ops,
171	},
172	.probe		= omap3_rom_rng_probe,
173};
174
175module_platform_driver(omap3_rom_rng_driver);
176
177MODULE_ALIAS("platform:omap3-rom-rng");
178MODULE_AUTHOR("Juha Yrjola");
179MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
180MODULE_DESCRIPTION("RNG driver for TI OMAP3 CPU family");
181MODULE_LICENSE("GPL");
~/ Read-only file preview.