blob: 38b719017186ef4f27e3b93a013165f5d9bd8d5d [file] [log] [blame]
Pali Rohár1c6b7c22013-09-20 15:25:06 +02001/*
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>
Aaro Koskinen4c13ac12015-11-18 21:59:01 +020020#include <linux/workqueue.h>
Pali Rohár1c6b7c22013-09-20 15:25:06 +020021#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
Aaro Koskinen4c13ac12015-11-18 21:59:01 +020032static struct delayed_work idle_work;
Pali Rohár1c6b7c22013-09-20 15:25:06 +020033static int rng_idle;
34static struct clk *rng_clk;
35
Aaro Koskinen4c13ac12015-11-18 21:59:01 +020036static void omap3_rom_rng_idle(struct work_struct *work)
Pali Rohár1c6b7c22013-09-20 15:25:06 +020037{
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
Aaro Koskinen4c13ac12015-11-18 21:59:01 +020054 cancel_delayed_work_sync(&idle_work);
Pali Rohár1c6b7c22013-09-20 15:25:06 +020055 if (rng_idle) {
Arvind Yadav986130b2017-05-15 13:52:03 +053056 r = clk_prepare_enable(rng_clk);
57 if (r)
58 return r;
59
Pali Rohár1c6b7c22013-09-20 15:25:06 +020060 r = omap3_rom_rng_call(0, 0, RNG_GEN_PRNG_HW_INIT);
61 if (r != 0) {
62 clk_disable_unprepare(rng_clk);
63 pr_err("HW init failed: %d\n", r);
64 return -EIO;
65 }
66 rng_idle = 0;
67 }
68
69 ptr = virt_to_phys(buf);
70 r = omap3_rom_rng_call(ptr, count, RNG_GEN_HW);
Aaro Koskinen4c13ac12015-11-18 21:59:01 +020071 schedule_delayed_work(&idle_work, msecs_to_jiffies(500));
Pali Rohár1c6b7c22013-09-20 15:25:06 +020072 if (r != 0)
73 return -EINVAL;
74 return 0;
75}
76
PrasannaKumar Muralidharan161040f2016-08-20 21:31:06 +053077static int omap3_rom_rng_read(struct hwrng *rng, void *data, size_t max, bool w)
Pali Rohár1c6b7c22013-09-20 15:25:06 +020078{
79 int r;
80
81 r = omap3_rom_rng_get_random(data, 4);
82 if (r < 0)
83 return r;
84 return 4;
85}
86
87static struct hwrng omap3_rom_rng_ops = {
88 .name = "omap3-rom",
PrasannaKumar Muralidharan161040f2016-08-20 21:31:06 +053089 .read = omap3_rom_rng_read,
Pali Rohár1c6b7c22013-09-20 15:25:06 +020090};
91
92static int omap3_rom_rng_probe(struct platform_device *pdev)
93{
Arvind Yadav986130b2017-05-15 13:52:03 +053094 int ret = 0;
95
Pali Rohár1c6b7c22013-09-20 15:25:06 +020096 pr_info("initializing\n");
97
98 omap3_rom_rng_call = pdev->dev.platform_data;
99 if (!omap3_rom_rng_call) {
100 pr_err("omap3_rom_rng_call is NULL\n");
101 return -EINVAL;
102 }
103
Aaro Koskinen4c13ac12015-11-18 21:59:01 +0200104 INIT_DELAYED_WORK(&idle_work, omap3_rom_rng_idle);
Jingoo Han0574bce2014-02-27 14:02:24 +0900105 rng_clk = devm_clk_get(&pdev->dev, "ick");
Pali Rohár1c6b7c22013-09-20 15:25:06 +0200106 if (IS_ERR(rng_clk)) {
107 pr_err("unable to get RNG clock\n");
108 return PTR_ERR(rng_clk);
109 }
110
111 /* Leave the RNG in reset state. */
Arvind Yadav986130b2017-05-15 13:52:03 +0530112 ret = clk_prepare_enable(rng_clk);
113 if (ret)
114 return ret;
Pali Rohár1c6b7c22013-09-20 15:25:06 +0200115 omap3_rom_rng_idle(0);
116
117 return hwrng_register(&omap3_rom_rng_ops);
118}
119
120static int omap3_rom_rng_remove(struct platform_device *pdev)
121{
Aaro Koskinen4c13ac12015-11-18 21:59:01 +0200122 cancel_delayed_work_sync(&idle_work);
Pali Rohár1c6b7c22013-09-20 15:25:06 +0200123 hwrng_unregister(&omap3_rom_rng_ops);
124 clk_disable_unprepare(rng_clk);
Pali Rohár1c6b7c22013-09-20 15:25:06 +0200125 return 0;
126}
127
128static struct platform_driver omap3_rom_rng_driver = {
129 .driver = {
130 .name = "omap3-rom-rng",
Pali Rohár1c6b7c22013-09-20 15:25:06 +0200131 },
132 .probe = omap3_rom_rng_probe,
133 .remove = omap3_rom_rng_remove,
134};
135
136module_platform_driver(omap3_rom_rng_driver);
137
138MODULE_ALIAS("platform:omap3-rom-rng");
139MODULE_AUTHOR("Juha Yrjola");
140MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
141MODULE_LICENSE("GPL");