| .. | .. |
|---|
| 4 | 4 | * Copyright (C) 2009 Nokia Corporation |
|---|
| 5 | 5 | * Author: Juha Yrjola <juha.yrjola@solidboot.com> |
|---|
| 6 | 6 | * |
|---|
| 7 | | - * Copyright (C) 2013 Pali Rohár <pali.rohar@gmail.com> |
|---|
| 7 | + * Copyright (C) 2013 Pali Rohár <pali@kernel.org> |
|---|
| 8 | 8 | * |
|---|
| 9 | 9 | * This file is licensed under the terms of the GNU General Public |
|---|
| 10 | 10 | * License version 2. This program is licensed "as is" without any |
|---|
| 11 | 11 | * warranty of any kind, whether express or implied. |
|---|
| 12 | 12 | */ |
|---|
| 13 | | - |
|---|
| 14 | | -#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
|---|
| 15 | 13 | |
|---|
| 16 | 14 | #include <linux/module.h> |
|---|
| 17 | 15 | #include <linux/init.h> |
|---|
| .. | .. |
|---|
| 20 | 18 | #include <linux/workqueue.h> |
|---|
| 21 | 19 | #include <linux/clk.h> |
|---|
| 22 | 20 | #include <linux/err.h> |
|---|
| 21 | +#include <linux/io.h> |
|---|
| 23 | 22 | #include <linux/of.h> |
|---|
| 24 | 23 | #include <linux/of_device.h> |
|---|
| 25 | 24 | #include <linux/platform_device.h> |
|---|
| 25 | +#include <linux/pm_runtime.h> |
|---|
| 26 | 26 | |
|---|
| 27 | 27 | #define RNG_RESET 0x01 |
|---|
| 28 | 28 | #define RNG_GEN_PRNG_HW_INIT 0x02 |
|---|
| 29 | 29 | #define RNG_GEN_HW 0x08 |
|---|
| 30 | 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 | | - |
|---|
| 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 | +}; |
|---|
| 78 | 37 | |
|---|
| 79 | 38 | static int omap3_rom_rng_read(struct hwrng *rng, void *data, size_t max, bool w) |
|---|
| 80 | 39 | { |
|---|
| 40 | + struct omap_rom_rng *ddata; |
|---|
| 41 | + u32 ptr; |
|---|
| 81 | 42 | int r; |
|---|
| 82 | 43 | |
|---|
| 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 | + |
|---|
| 85 | 50 | 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; |
|---|
| 87 | 64 | } |
|---|
| 88 | 65 | |
|---|
| 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 | +} |
|---|
| 92 | 111 | |
|---|
| 93 | 112 | static int omap3_rom_rng_probe(struct platform_device *pdev) |
|---|
| 94 | 113 | { |
|---|
| 114 | + struct omap_rom_rng *ddata; |
|---|
| 95 | 115 | int ret = 0; |
|---|
| 96 | 116 | |
|---|
| 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) { |
|---|
| 99 | 127 | dev_err(&pdev->dev, "missing rom code handler\n"); |
|---|
| 100 | 128 | |
|---|
| 101 | 129 | return -ENODEV; |
|---|
| 102 | 130 | } |
|---|
| 131 | + dev_set_drvdata(ddata->dev, ddata); |
|---|
| 103 | 132 | |
|---|
| 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"); |
|---|
| 107 | 136 | return -EINVAL; |
|---|
| 108 | 137 | } |
|---|
| 109 | 138 | |
|---|
| 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); |
|---|
| 115 | 143 | } |
|---|
| 116 | 144 | |
|---|
| 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); |
|---|
| 119 | 151 | if (ret) |
|---|
| 120 | 152 | return ret; |
|---|
| 121 | | - omap3_rom_rng_idle(0); |
|---|
| 122 | 153 | |
|---|
| 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); |
|---|
| 133 | 155 | } |
|---|
| 134 | 156 | |
|---|
| 135 | 157 | static const struct of_device_id omap_rom_rng_match[] = { |
|---|
| .. | .. |
|---|
| 138 | 160 | }; |
|---|
| 139 | 161 | MODULE_DEVICE_TABLE(of, omap_rom_rng_match); |
|---|
| 140 | 162 | |
|---|
| 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 | + |
|---|
| 141 | 168 | static struct platform_driver omap3_rom_rng_driver = { |
|---|
| 142 | 169 | .driver = { |
|---|
| 143 | 170 | .name = "omap3-rom-rng", |
|---|
| 144 | 171 | .of_match_table = omap_rom_rng_match, |
|---|
| 172 | + .pm = &omap_rom_rng_pm_ops, |
|---|
| 145 | 173 | }, |
|---|
| 146 | 174 | .probe = omap3_rom_rng_probe, |
|---|
| 147 | | - .remove = omap3_rom_rng_remove, |
|---|
| 148 | 175 | }; |
|---|
| 149 | 176 | |
|---|
| 150 | 177 | module_platform_driver(omap3_rom_rng_driver); |
|---|
| 151 | 178 | |
|---|
| 152 | 179 | MODULE_ALIAS("platform:omap3-rom-rng"); |
|---|
| 153 | 180 | MODULE_AUTHOR("Juha Yrjola"); |
|---|
| 154 | | -MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>"); |
|---|
| 181 | +MODULE_AUTHOR("Pali Rohár <pali@kernel.org>"); |
|---|
| 155 | 182 | MODULE_LICENSE("GPL"); |
|---|