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.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>
20 #include <linux/workqueue.h>
21 #include <linux/clk.h>
22 #include <linux/err.h>
23 #include <linux/of.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26
27 #define RNG_RESET 0x01
28 #define RNG_GEN_PRNG_HW_INIT 0x02
29 #define RNG_GEN_HW 0x08
30
31 /* param1: ptr, param2: count, param3: flag */
32 static u32 (*omap3_rom_rng_call)(u32, u32, u32);
33
34 static struct delayed_work idle_work;
35 static int rng_idle;
36 static struct clk *rng_clk;
37
omap3_rom_rng_idle(struct work_struct * work)38 static void omap3_rom_rng_idle(struct work_struct *work)
39 {
40 int r;
41
42 r = omap3_rom_rng_call(0, 0, RNG_RESET);
43 if (r != 0) {
44 pr_err("reset failed: %d\n", r);
45 return;
46 }
47 clk_disable_unprepare(rng_clk);
48 rng_idle = 1;
49 }
50
omap3_rom_rng_get_random(void * buf,unsigned int count)51 static int omap3_rom_rng_get_random(void *buf, unsigned int count)
52 {
53 u32 r;
54 u32 ptr;
55
56 cancel_delayed_work_sync(&idle_work);
57 if (rng_idle) {
58 r = clk_prepare_enable(rng_clk);
59 if (r)
60 return r;
61
62 r = omap3_rom_rng_call(0, 0, RNG_GEN_PRNG_HW_INIT);
63 if (r != 0) {
64 clk_disable_unprepare(rng_clk);
65 pr_err("HW init failed: %d\n", r);
66 return -EIO;
67 }
68 rng_idle = 0;
69 }
70
71 ptr = virt_to_phys(buf);
72 r = omap3_rom_rng_call(ptr, count, RNG_GEN_HW);
73 schedule_delayed_work(&idle_work, msecs_to_jiffies(500));
74 if (r != 0)
75 return -EINVAL;
76 return 0;
77 }
78
omap3_rom_rng_read(struct hwrng * rng,void * data,size_t max,bool w)79 static int omap3_rom_rng_read(struct hwrng *rng, void *data, size_t max, bool w)
80 {
81 int r;
82
83 r = omap3_rom_rng_get_random(data, 4);
84 if (r < 0)
85 return r;
86 return 4;
87 }
88
89 static struct hwrng omap3_rom_rng_ops = {
90 .name = "omap3-rom",
91 };
92
omap3_rom_rng_probe(struct platform_device * pdev)93 static int omap3_rom_rng_probe(struct platform_device *pdev)
94 {
95 int ret = 0;
96
97 omap3_rom_rng_ops.read = of_device_get_match_data(&pdev->dev);
98 if (!omap3_rom_rng_ops.read) {
99 dev_err(&pdev->dev, "missing rom code handler\n");
100
101 return -ENODEV;
102 }
103
104 omap3_rom_rng_call = pdev->dev.platform_data;
105 if (!omap3_rom_rng_call) {
106 pr_err("omap3_rom_rng_call is NULL\n");
107 return -EINVAL;
108 }
109
110 INIT_DELAYED_WORK(&idle_work, omap3_rom_rng_idle);
111 rng_clk = devm_clk_get(&pdev->dev, "ick");
112 if (IS_ERR(rng_clk)) {
113 pr_err("unable to get RNG clock\n");
114 return PTR_ERR(rng_clk);
115 }
116
117 /* Leave the RNG in reset state. */
118 ret = clk_prepare_enable(rng_clk);
119 if (ret)
120 return ret;
121 omap3_rom_rng_idle(0);
122
123 return hwrng_register(&omap3_rom_rng_ops);
124 }
125
omap3_rom_rng_remove(struct platform_device * pdev)126 static int omap3_rom_rng_remove(struct platform_device *pdev)
127 {
128 cancel_delayed_work_sync(&idle_work);
129 hwrng_unregister(&omap3_rom_rng_ops);
130 if (!rng_idle)
131 clk_disable_unprepare(rng_clk);
132 return 0;
133 }
134
135 static const struct of_device_id omap_rom_rng_match[] = {
136 { .compatible = "nokia,n900-rom-rng", .data = omap3_rom_rng_read, },
137 { /* sentinel */ },
138 };
139 MODULE_DEVICE_TABLE(of, omap_rom_rng_match);
140
141 static struct platform_driver omap3_rom_rng_driver = {
142 .driver = {
143 .name = "omap3-rom-rng",
144 .of_match_table = omap_rom_rng_match,
145 },
146 .probe = omap3_rom_rng_probe,
147 .remove = omap3_rom_rng_remove,
148 };
149
150 module_platform_driver(omap3_rom_rng_driver);
151
152 MODULE_ALIAS("platform:omap3-rom-rng");
153 MODULE_AUTHOR("Juha Yrjola");
154 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
155 MODULE_LICENSE("GPL");
156