2013-09-20 21:25:06 +08:00
|
|
|
/*
|
|
|
|
* omap3-rom-rng.c - RNG driver for TI OMAP3 CPU family
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Nokia Corporation
|
|
|
|
* Author: Juha Yrjola <juha.yrjola@solidboot.com>
|
|
|
|
*
|
2020-04-11 05:34:00 +08:00
|
|
|
* Copyright (C) 2013 Pali Rohár <pali@kernel.org>
|
2013-09-20 21:25:06 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/random.h>
|
|
|
|
#include <linux/hw_random.h>
|
2015-11-19 03:59:01 +08:00
|
|
|
#include <linux/workqueue.h>
|
2013-09-20 21:25:06 +08:00
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/err.h>
|
2020-02-22 09:38:55 +08:00
|
|
|
#include <linux/io.h>
|
2019-09-15 05:02:55 +08:00
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_device.h>
|
2013-09-20 21:25:06 +08:00
|
|
|
#include <linux/platform_device.h>
|
2019-09-15 05:02:59 +08:00
|
|
|
#include <linux/pm_runtime.h>
|
2013-09-20 21:25:06 +08:00
|
|
|
|
|
|
|
#define RNG_RESET 0x01
|
|
|
|
#define RNG_GEN_PRNG_HW_INIT 0x02
|
|
|
|
#define RNG_GEN_HW 0x08
|
|
|
|
|
2019-09-15 05:02:58 +08:00
|
|
|
struct omap_rom_rng {
|
|
|
|
struct clk *clk;
|
|
|
|
struct device *dev;
|
|
|
|
struct hwrng ops;
|
2019-09-15 05:02:59 +08:00
|
|
|
u32 (*rom_rng_call)(u32 ptr, u32 count, u32 flag);
|
2019-09-15 05:02:58 +08:00
|
|
|
};
|
|
|
|
|
2019-09-15 05:02:59 +08:00
|
|
|
static int omap3_rom_rng_read(struct hwrng *rng, void *data, size_t max, bool w)
|
2013-09-20 21:25:06 +08:00
|
|
|
{
|
2019-09-15 05:02:59 +08:00
|
|
|
struct omap_rom_rng *ddata;
|
|
|
|
u32 ptr;
|
2013-09-20 21:25:06 +08:00
|
|
|
int r;
|
|
|
|
|
2019-09-15 05:02:59 +08:00
|
|
|
ddata = (struct omap_rom_rng *)rng->priv;
|
|
|
|
|
|
|
|
r = pm_runtime_get_sync(ddata->dev);
|
|
|
|
if (r < 0) {
|
|
|
|
pm_runtime_put_noidle(ddata->dev);
|
|
|
|
|
|
|
|
return r;
|
2013-09-20 21:25:06 +08:00
|
|
|
}
|
2019-09-15 05:02:59 +08:00
|
|
|
|
|
|
|
ptr = virt_to_phys(data);
|
|
|
|
r = ddata->rom_rng_call(ptr, 4, RNG_GEN_HW);
|
|
|
|
if (r != 0)
|
|
|
|
r = -EINVAL;
|
|
|
|
else
|
|
|
|
r = 4;
|
|
|
|
|
|
|
|
pm_runtime_mark_last_busy(ddata->dev);
|
|
|
|
pm_runtime_put_autosuspend(ddata->dev);
|
|
|
|
|
|
|
|
return r;
|
2013-09-20 21:25:06 +08:00
|
|
|
}
|
|
|
|
|
2019-10-22 22:27:31 +08:00
|
|
|
static int __maybe_unused omap_rom_rng_runtime_suspend(struct device *dev)
|
2013-09-20 21:25:06 +08:00
|
|
|
{
|
2019-09-15 05:02:59 +08:00
|
|
|
struct omap_rom_rng *ddata;
|
|
|
|
int r;
|
2013-09-20 21:25:06 +08:00
|
|
|
|
2019-09-15 05:02:59 +08:00
|
|
|
ddata = dev_get_drvdata(dev);
|
2013-09-20 21:25:06 +08:00
|
|
|
|
2019-09-15 05:02:59 +08:00
|
|
|
r = ddata->rom_rng_call(0, 0, RNG_RESET);
|
2013-09-20 21:25:06 +08:00
|
|
|
if (r != 0)
|
2019-09-15 05:02:59 +08:00
|
|
|
dev_err(dev, "reset failed: %d\n", r);
|
|
|
|
|
|
|
|
clk_disable_unprepare(ddata->clk);
|
|
|
|
|
2013-09-20 21:25:06 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-22 22:27:31 +08:00
|
|
|
static int __maybe_unused omap_rom_rng_runtime_resume(struct device *dev)
|
2013-09-20 21:25:06 +08:00
|
|
|
{
|
2019-09-15 05:02:59 +08:00
|
|
|
struct omap_rom_rng *ddata;
|
2013-09-20 21:25:06 +08:00
|
|
|
int r;
|
|
|
|
|
2019-09-15 05:02:59 +08:00
|
|
|
ddata = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
r = clk_prepare_enable(ddata->clk);
|
2013-09-20 21:25:06 +08:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-09-15 05:02:59 +08:00
|
|
|
|
|
|
|
r = ddata->rom_rng_call(0, 0, RNG_GEN_PRNG_HW_INIT);
|
|
|
|
if (r != 0) {
|
|
|
|
clk_disable(ddata->clk);
|
|
|
|
dev_err(dev, "HW init failed: %d\n", r);
|
|
|
|
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2013-09-20 21:25:06 +08:00
|
|
|
}
|
|
|
|
|
2019-09-15 05:03:00 +08:00
|
|
|
static void omap_rom_rng_finish(void *data)
|
|
|
|
{
|
|
|
|
struct omap_rom_rng *ddata = data;
|
|
|
|
|
|
|
|
pm_runtime_dont_use_autosuspend(ddata->dev);
|
|
|
|
pm_runtime_disable(ddata->dev);
|
|
|
|
}
|
|
|
|
|
2013-09-20 21:25:06 +08:00
|
|
|
static int omap3_rom_rng_probe(struct platform_device *pdev)
|
|
|
|
{
|
2019-09-15 05:02:58 +08:00
|
|
|
struct omap_rom_rng *ddata;
|
2017-05-15 16:22:03 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
2019-09-15 05:02:58 +08:00
|
|
|
ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
|
|
|
|
if (!ddata)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ddata->dev = &pdev->dev;
|
|
|
|
ddata->ops.priv = (unsigned long)ddata;
|
|
|
|
ddata->ops.name = "omap3-rom";
|
|
|
|
ddata->ops.read = of_device_get_match_data(&pdev->dev);
|
|
|
|
ddata->ops.quality = 900;
|
|
|
|
if (!ddata->ops.read) {
|
2019-09-15 05:02:55 +08:00
|
|
|
dev_err(&pdev->dev, "missing rom code handler\n");
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2019-09-15 05:02:58 +08:00
|
|
|
dev_set_drvdata(ddata->dev, ddata);
|
2013-09-20 21:25:06 +08:00
|
|
|
|
2019-09-15 05:02:59 +08:00
|
|
|
ddata->rom_rng_call = pdev->dev.platform_data;
|
|
|
|
if (!ddata->rom_rng_call) {
|
2019-09-15 05:02:58 +08:00
|
|
|
dev_err(ddata->dev, "rom_rng_call is NULL\n");
|
2013-09-20 21:25:06 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-09-15 05:02:58 +08:00
|
|
|
ddata->clk = devm_clk_get(ddata->dev, "ick");
|
|
|
|
if (IS_ERR(ddata->clk)) {
|
|
|
|
dev_err(ddata->dev, "unable to get RNG clock\n");
|
|
|
|
return PTR_ERR(ddata->clk);
|
2013-09-20 21:25:06 +08:00
|
|
|
}
|
|
|
|
|
2019-09-15 05:03:00 +08:00
|
|
|
pm_runtime_enable(&pdev->dev);
|
|
|
|
pm_runtime_set_autosuspend_delay(&pdev->dev, 500);
|
|
|
|
pm_runtime_use_autosuspend(&pdev->dev);
|
2019-09-15 05:02:58 +08:00
|
|
|
|
2019-09-15 05:03:00 +08:00
|
|
|
ret = devm_add_action_or_reset(ddata->dev, omap_rom_rng_finish,
|
|
|
|
ddata);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-09-15 05:02:59 +08:00
|
|
|
|
2019-09-15 05:03:00 +08:00
|
|
|
return devm_hwrng_register(ddata->dev, &ddata->ops);
|
2013-09-20 21:25:06 +08:00
|
|
|
}
|
|
|
|
|
2019-09-15 05:02:55 +08:00
|
|
|
static const struct of_device_id omap_rom_rng_match[] = {
|
|
|
|
{ .compatible = "nokia,n900-rom-rng", .data = omap3_rom_rng_read, },
|
|
|
|
{ /* sentinel */ },
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, omap_rom_rng_match);
|
|
|
|
|
2019-09-15 05:02:59 +08:00
|
|
|
static const struct dev_pm_ops omap_rom_rng_pm_ops = {
|
|
|
|
SET_SYSTEM_SLEEP_PM_OPS(omap_rom_rng_runtime_suspend,
|
|
|
|
omap_rom_rng_runtime_resume)
|
|
|
|
};
|
|
|
|
|
2013-09-20 21:25:06 +08:00
|
|
|
static struct platform_driver omap3_rom_rng_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "omap3-rom-rng",
|
2019-09-15 05:02:55 +08:00
|
|
|
.of_match_table = omap_rom_rng_match,
|
2019-09-15 05:02:59 +08:00
|
|
|
.pm = &omap_rom_rng_pm_ops,
|
2013-09-20 21:25:06 +08:00
|
|
|
},
|
|
|
|
.probe = omap3_rom_rng_probe,
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(omap3_rom_rng_driver);
|
|
|
|
|
|
|
|
MODULE_ALIAS("platform:omap3-rom-rng");
|
|
|
|
MODULE_AUTHOR("Juha Yrjola");
|
2020-04-11 05:34:00 +08:00
|
|
|
MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
|
2013-09-20 21:25:06 +08:00
|
|
|
MODULE_LICENSE("GPL");
|