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>
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Pali Rohár <pali.rohar@gmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
|
|
|
#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>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
|
|
|
|
#define RNG_RESET 0x01
|
|
|
|
#define RNG_GEN_PRNG_HW_INIT 0x02
|
|
|
|
#define RNG_GEN_HW 0x08
|
|
|
|
|
|
|
|
/* param1: ptr, param2: count, param3: flag */
|
|
|
|
static u32 (*omap3_rom_rng_call)(u32, u32, u32);
|
|
|
|
|
2015-11-19 03:59:01 +08:00
|
|
|
static struct delayed_work idle_work;
|
2013-09-20 21:25:06 +08:00
|
|
|
static int rng_idle;
|
|
|
|
static struct clk *rng_clk;
|
|
|
|
|
2015-11-19 03:59:01 +08:00
|
|
|
static void omap3_rom_rng_idle(struct work_struct *work)
|
2013-09-20 21:25:06 +08:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = omap3_rom_rng_call(0, 0, RNG_RESET);
|
|
|
|
if (r != 0) {
|
|
|
|
pr_err("reset failed: %d\n", r);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
clk_disable_unprepare(rng_clk);
|
|
|
|
rng_idle = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int omap3_rom_rng_get_random(void *buf, unsigned int count)
|
|
|
|
{
|
|
|
|
u32 r;
|
|
|
|
u32 ptr;
|
|
|
|
|
2015-11-19 03:59:01 +08:00
|
|
|
cancel_delayed_work_sync(&idle_work);
|
2013-09-20 21:25:06 +08:00
|
|
|
if (rng_idle) {
|
2017-05-15 16:22:03 +08:00
|
|
|
r = clk_prepare_enable(rng_clk);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
2013-09-20 21:25:06 +08:00
|
|
|
r = omap3_rom_rng_call(0, 0, RNG_GEN_PRNG_HW_INIT);
|
|
|
|
if (r != 0) {
|
|
|
|
clk_disable_unprepare(rng_clk);
|
|
|
|
pr_err("HW init failed: %d\n", r);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
rng_idle = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = virt_to_phys(buf);
|
|
|
|
r = omap3_rom_rng_call(ptr, count, RNG_GEN_HW);
|
2015-11-19 03:59:01 +08:00
|
|
|
schedule_delayed_work(&idle_work, msecs_to_jiffies(500));
|
2013-09-20 21:25:06 +08:00
|
|
|
if (r != 0)
|
|
|
|
return -EINVAL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-21 00:01:06 +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
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = omap3_rom_rng_get_random(data, 4);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct hwrng omap3_rom_rng_ops = {
|
|
|
|
.name = "omap3-rom",
|
2016-08-21 00:01:06 +08:00
|
|
|
.read = omap3_rom_rng_read,
|
2013-09-20 21:25:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int omap3_rom_rng_probe(struct platform_device *pdev)
|
|
|
|
{
|
2017-05-15 16:22:03 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
2013-09-20 21:25:06 +08:00
|
|
|
pr_info("initializing\n");
|
|
|
|
|
|
|
|
omap3_rom_rng_call = pdev->dev.platform_data;
|
|
|
|
if (!omap3_rom_rng_call) {
|
|
|
|
pr_err("omap3_rom_rng_call is NULL\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-11-19 03:59:01 +08:00
|
|
|
INIT_DELAYED_WORK(&idle_work, omap3_rom_rng_idle);
|
2014-02-27 13:02:24 +08:00
|
|
|
rng_clk = devm_clk_get(&pdev->dev, "ick");
|
2013-09-20 21:25:06 +08:00
|
|
|
if (IS_ERR(rng_clk)) {
|
|
|
|
pr_err("unable to get RNG clock\n");
|
|
|
|
return PTR_ERR(rng_clk);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Leave the RNG in reset state. */
|
2017-05-15 16:22:03 +08:00
|
|
|
ret = clk_prepare_enable(rng_clk);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2013-09-20 21:25:06 +08:00
|
|
|
omap3_rom_rng_idle(0);
|
|
|
|
|
|
|
|
return hwrng_register(&omap3_rom_rng_ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int omap3_rom_rng_remove(struct platform_device *pdev)
|
|
|
|
{
|
2015-11-19 03:59:01 +08:00
|
|
|
cancel_delayed_work_sync(&idle_work);
|
2013-09-20 21:25:06 +08:00
|
|
|
hwrng_unregister(&omap3_rom_rng_ops);
|
|
|
|
clk_disable_unprepare(rng_clk);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver omap3_rom_rng_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "omap3-rom-rng",
|
|
|
|
},
|
|
|
|
.probe = omap3_rom_rng_probe,
|
|
|
|
.remove = omap3_rom_rng_remove,
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(omap3_rom_rng_driver);
|
|
|
|
|
|
|
|
MODULE_ALIAS("platform:omap3-rom-rng");
|
|
|
|
MODULE_AUTHOR("Juha Yrjola");
|
|
|
|
MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
|
|
|
|
MODULE_LICENSE("GPL");
|