dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / char / hw_random / omap3-rom-rng.c
blob38b719017186ef4f27e3b93a013165f5d9bd8d5d
1 /*
2 * omap3-rom-rng.c - RNG driver for TI OMAP3 CPU family
4 * Copyright (C) 2009 Nokia Corporation
5 * Author: Juha Yrjola <juha.yrjola@solidboot.com>
7 * Copyright (C) 2013 Pali Rohár <pali.rohar@gmail.com>
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.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
25 #define RNG_RESET 0x01
26 #define RNG_GEN_PRNG_HW_INIT 0x02
27 #define RNG_GEN_HW 0x08
29 /* param1: ptr, param2: count, param3: flag */
30 static u32 (*omap3_rom_rng_call)(u32, u32, u32);
32 static struct delayed_work idle_work;
33 static int rng_idle;
34 static struct clk *rng_clk;
36 static void omap3_rom_rng_idle(struct work_struct *work)
38 int r;
40 r = omap3_rom_rng_call(0, 0, RNG_RESET);
41 if (r != 0) {
42 pr_err("reset failed: %d\n", r);
43 return;
45 clk_disable_unprepare(rng_clk);
46 rng_idle = 1;
49 static int omap3_rom_rng_get_random(void *buf, unsigned int count)
51 u32 r;
52 u32 ptr;
54 cancel_delayed_work_sync(&idle_work);
55 if (rng_idle) {
56 r = clk_prepare_enable(rng_clk);
57 if (r)
58 return r;
60 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;
66 rng_idle = 0;
69 ptr = virt_to_phys(buf);
70 r = omap3_rom_rng_call(ptr, count, RNG_GEN_HW);
71 schedule_delayed_work(&idle_work, msecs_to_jiffies(500));
72 if (r != 0)
73 return -EINVAL;
74 return 0;
77 static int omap3_rom_rng_read(struct hwrng *rng, void *data, size_t max, bool w)
79 int r;
81 r = omap3_rom_rng_get_random(data, 4);
82 if (r < 0)
83 return r;
84 return 4;
87 static struct hwrng omap3_rom_rng_ops = {
88 .name = "omap3-rom",
89 .read = omap3_rom_rng_read,
92 static int omap3_rom_rng_probe(struct platform_device *pdev)
94 int ret = 0;
96 pr_info("initializing\n");
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;
104 INIT_DELAYED_WORK(&idle_work, omap3_rom_rng_idle);
105 rng_clk = devm_clk_get(&pdev->dev, "ick");
106 if (IS_ERR(rng_clk)) {
107 pr_err("unable to get RNG clock\n");
108 return PTR_ERR(rng_clk);
111 /* Leave the RNG in reset state. */
112 ret = clk_prepare_enable(rng_clk);
113 if (ret)
114 return ret;
115 omap3_rom_rng_idle(0);
117 return hwrng_register(&omap3_rom_rng_ops);
120 static int omap3_rom_rng_remove(struct platform_device *pdev)
122 cancel_delayed_work_sync(&idle_work);
123 hwrng_unregister(&omap3_rom_rng_ops);
124 clk_disable_unprepare(rng_clk);
125 return 0;
128 static struct platform_driver omap3_rom_rng_driver = {
129 .driver = {
130 .name = "omap3-rom-rng",
132 .probe = omap3_rom_rng_probe,
133 .remove = omap3_rom_rng_remove,
136 module_platform_driver(omap3_rom_rng_driver);
138 MODULE_ALIAS("platform:omap3-rom-rng");
139 MODULE_AUTHOR("Juha Yrjola");
140 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
141 MODULE_LICENSE("GPL");