hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/char/hw_random/omap3-rom-rng.c
....@@ -4,14 +4,12 @@
44 * Copyright (C) 2009 Nokia Corporation
55 * Author: Juha Yrjola <juha.yrjola@solidboot.com>
66 *
7
- * Copyright (C) 2013 Pali Rohár <pali.rohar@gmail.com>
7
+ * Copyright (C) 2013 Pali Rohár <pali@kernel.org>
88 *
99 * This file is licensed under the terms of the GNU General Public
1010 * License version 2. This program is licensed "as is" without any
1111 * warranty of any kind, whether express or implied.
1212 */
13
-
14
-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1513
1614 #include <linux/module.h>
1715 #include <linux/init.h>
....@@ -20,116 +18,140 @@
2018 #include <linux/workqueue.h>
2119 #include <linux/clk.h>
2220 #include <linux/err.h>
21
+#include <linux/io.h>
2322 #include <linux/of.h>
2423 #include <linux/of_device.h>
2524 #include <linux/platform_device.h>
25
+#include <linux/pm_runtime.h>
2626
2727 #define RNG_RESET 0x01
2828 #define RNG_GEN_PRNG_HW_INIT 0x02
2929 #define RNG_GEN_HW 0x08
3030
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
-
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
-
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
-}
31
+struct omap_rom_rng {
32
+ struct clk *clk;
33
+ struct device *dev;
34
+ struct hwrng ops;
35
+ u32 (*rom_rng_call)(u32 ptr, u32 count, u32 flag);
36
+};
7837
7938 static int omap3_rom_rng_read(struct hwrng *rng, void *data, size_t max, bool w)
8039 {
40
+ struct omap_rom_rng *ddata;
41
+ u32 ptr;
8142 int r;
8243
83
- r = omap3_rom_rng_get_random(data, 4);
84
- if (r < 0)
44
+ ddata = (struct omap_rom_rng *)rng->priv;
45
+
46
+ r = pm_runtime_get_sync(ddata->dev);
47
+ if (r < 0) {
48
+ pm_runtime_put_noidle(ddata->dev);
49
+
8550 return r;
86
- return 4;
51
+ }
52
+
53
+ ptr = virt_to_phys(data);
54
+ r = ddata->rom_rng_call(ptr, 4, RNG_GEN_HW);
55
+ if (r != 0)
56
+ r = -EINVAL;
57
+ else
58
+ r = 4;
59
+
60
+ pm_runtime_mark_last_busy(ddata->dev);
61
+ pm_runtime_put_autosuspend(ddata->dev);
62
+
63
+ return r;
8764 }
8865
89
-static struct hwrng omap3_rom_rng_ops = {
90
- .name = "omap3-rom",
91
-};
66
+static int __maybe_unused omap_rom_rng_runtime_suspend(struct device *dev)
67
+{
68
+ struct omap_rom_rng *ddata;
69
+ int r;
70
+
71
+ ddata = dev_get_drvdata(dev);
72
+
73
+ r = ddata->rom_rng_call(0, 0, RNG_RESET);
74
+ if (r != 0)
75
+ dev_err(dev, "reset failed: %d\n", r);
76
+
77
+ clk_disable_unprepare(ddata->clk);
78
+
79
+ return 0;
80
+}
81
+
82
+static int __maybe_unused omap_rom_rng_runtime_resume(struct device *dev)
83
+{
84
+ struct omap_rom_rng *ddata;
85
+ int r;
86
+
87
+ ddata = dev_get_drvdata(dev);
88
+
89
+ r = clk_prepare_enable(ddata->clk);
90
+ if (r < 0)
91
+ return r;
92
+
93
+ r = ddata->rom_rng_call(0, 0, RNG_GEN_PRNG_HW_INIT);
94
+ if (r != 0) {
95
+ clk_disable_unprepare(ddata->clk);
96
+ dev_err(dev, "HW init failed: %d\n", r);
97
+
98
+ return -EIO;
99
+ }
100
+
101
+ return 0;
102
+}
103
+
104
+static void omap_rom_rng_finish(void *data)
105
+{
106
+ struct omap_rom_rng *ddata = data;
107
+
108
+ pm_runtime_dont_use_autosuspend(ddata->dev);
109
+ pm_runtime_disable(ddata->dev);
110
+}
92111
93112 static int omap3_rom_rng_probe(struct platform_device *pdev)
94113 {
114
+ struct omap_rom_rng *ddata;
95115 int ret = 0;
96116
97
- omap3_rom_rng_ops.read = of_device_get_match_data(&pdev->dev);
98
- if (!omap3_rom_rng_ops.read) {
117
+ ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
118
+ if (!ddata)
119
+ return -ENOMEM;
120
+
121
+ ddata->dev = &pdev->dev;
122
+ ddata->ops.priv = (unsigned long)ddata;
123
+ ddata->ops.name = "omap3-rom";
124
+ ddata->ops.read = of_device_get_match_data(&pdev->dev);
125
+ ddata->ops.quality = 900;
126
+ if (!ddata->ops.read) {
99127 dev_err(&pdev->dev, "missing rom code handler\n");
100128
101129 return -ENODEV;
102130 }
131
+ dev_set_drvdata(ddata->dev, ddata);
103132
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");
133
+ ddata->rom_rng_call = pdev->dev.platform_data;
134
+ if (!ddata->rom_rng_call) {
135
+ dev_err(ddata->dev, "rom_rng_call is NULL\n");
107136 return -EINVAL;
108137 }
109138
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);
139
+ ddata->clk = devm_clk_get(ddata->dev, "ick");
140
+ if (IS_ERR(ddata->clk)) {
141
+ dev_err(ddata->dev, "unable to get RNG clock\n");
142
+ return PTR_ERR(ddata->clk);
115143 }
116144
117
- /* Leave the RNG in reset state. */
118
- ret = clk_prepare_enable(rng_clk);
145
+ pm_runtime_enable(&pdev->dev);
146
+ pm_runtime_set_autosuspend_delay(&pdev->dev, 500);
147
+ pm_runtime_use_autosuspend(&pdev->dev);
148
+
149
+ ret = devm_add_action_or_reset(ddata->dev, omap_rom_rng_finish,
150
+ ddata);
119151 if (ret)
120152 return ret;
121
- omap3_rom_rng_idle(0);
122153
123
- return hwrng_register(&omap3_rom_rng_ops);
124
-}
125
-
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;
154
+ return devm_hwrng_register(ddata->dev, &ddata->ops);
133155 }
134156
135157 static const struct of_device_id omap_rom_rng_match[] = {
....@@ -138,18 +160,23 @@
138160 };
139161 MODULE_DEVICE_TABLE(of, omap_rom_rng_match);
140162
163
+static const struct dev_pm_ops omap_rom_rng_pm_ops = {
164
+ SET_SYSTEM_SLEEP_PM_OPS(omap_rom_rng_runtime_suspend,
165
+ omap_rom_rng_runtime_resume)
166
+};
167
+
141168 static struct platform_driver omap3_rom_rng_driver = {
142169 .driver = {
143170 .name = "omap3-rom-rng",
144171 .of_match_table = omap_rom_rng_match,
172
+ .pm = &omap_rom_rng_pm_ops,
145173 },
146174 .probe = omap3_rom_rng_probe,
147
- .remove = omap3_rom_rng_remove,
148175 };
149176
150177 module_platform_driver(omap3_rom_rng_driver);
151178
152179 MODULE_ALIAS("platform:omap3-rom-rng");
153180 MODULE_AUTHOR("Juha Yrjola");
154
-MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
181
+MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
155182 MODULE_LICENSE("GPL");