Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 1 | /* |
| 2 | * exynos-rng.c - Random Number Generator driver for the exynos |
| 3 | * |
| 4 | * Copyright (C) 2012 Samsung Electronics |
Krzysztof Kozlowski | 043809d | 2016-04-05 11:04:28 +0900 | [diff] [blame] | 5 | * Jonghwa Lee <jonghwa3.lee@samsung.com> |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <linux/hw_random.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 25 | #include <linux/io.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/clk.h> |
| 28 | #include <linux/pm_runtime.h> |
| 29 | #include <linux/err.h> |
| 30 | |
| 31 | #define EXYNOS_PRNG_STATUS_OFFSET 0x10 |
| 32 | #define EXYNOS_PRNG_SEED_OFFSET 0x140 |
| 33 | #define EXYNOS_PRNG_OUT1_OFFSET 0x160 |
| 34 | #define SEED_SETTING_DONE BIT(1) |
| 35 | #define PRNG_START 0x18 |
| 36 | #define PRNG_DONE BIT(5) |
| 37 | #define EXYNOS_AUTOSUSPEND_DELAY 100 |
| 38 | |
| 39 | struct exynos_rng { |
| 40 | struct device *dev; |
| 41 | struct hwrng rng; |
| 42 | void __iomem *mem; |
| 43 | struct clk *clk; |
| 44 | }; |
| 45 | |
| 46 | static u32 exynos_rng_readl(struct exynos_rng *rng, u32 offset) |
| 47 | { |
Ben Dooks | 5ad67c1 | 2016-06-08 19:31:09 +0100 | [diff] [blame] | 48 | return readl_relaxed(rng->mem + offset); |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static void exynos_rng_writel(struct exynos_rng *rng, u32 val, u32 offset) |
| 52 | { |
Ben Dooks | 5ad67c1 | 2016-06-08 19:31:09 +0100 | [diff] [blame] | 53 | writel_relaxed(val, rng->mem + offset); |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 54 | } |
| 55 | |
Krzysztof Kozlowski | bd1dffb | 2015-10-19 13:37:41 +0900 | [diff] [blame] | 56 | static int exynos_rng_configure(struct exynos_rng *exynos_rng) |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 57 | { |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 58 | int i; |
| 59 | int ret = 0; |
| 60 | |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 61 | for (i = 0 ; i < 5 ; i++) |
| 62 | exynos_rng_writel(exynos_rng, jiffies, |
| 63 | EXYNOS_PRNG_SEED_OFFSET + 4*i); |
| 64 | |
| 65 | if (!(exynos_rng_readl(exynos_rng, EXYNOS_PRNG_STATUS_OFFSET) |
| 66 | & SEED_SETTING_DONE)) |
| 67 | ret = -EIO; |
| 68 | |
Krzysztof Kozlowski | bd1dffb | 2015-10-19 13:37:41 +0900 | [diff] [blame] | 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | static int exynos_init(struct hwrng *rng) |
| 73 | { |
| 74 | struct exynos_rng *exynos_rng = container_of(rng, |
| 75 | struct exynos_rng, rng); |
| 76 | int ret = 0; |
| 77 | |
| 78 | pm_runtime_get_sync(exynos_rng->dev); |
| 79 | ret = exynos_rng_configure(exynos_rng); |
Krzysztof Kozlowski | f0d947a | 2016-03-14 09:07:12 +0900 | [diff] [blame] | 80 | pm_runtime_mark_last_busy(exynos_rng->dev); |
| 81 | pm_runtime_put_autosuspend(exynos_rng->dev); |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 82 | |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | static int exynos_read(struct hwrng *rng, void *buf, |
| 87 | size_t max, bool wait) |
| 88 | { |
| 89 | struct exynos_rng *exynos_rng = container_of(rng, |
| 90 | struct exynos_rng, rng); |
| 91 | u32 *data = buf; |
Krzysztof Kozlowski | d7fd607 | 2015-10-19 13:37:40 +0900 | [diff] [blame] | 92 | int retry = 100; |
Krzysztof Kozlowski | f1925d7 | 2016-03-14 09:07:13 +0900 | [diff] [blame] | 93 | int ret = 4; |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 94 | |
| 95 | pm_runtime_get_sync(exynos_rng->dev); |
| 96 | |
| 97 | exynos_rng_writel(exynos_rng, PRNG_START, 0); |
| 98 | |
| 99 | while (!(exynos_rng_readl(exynos_rng, |
Krzysztof Kozlowski | d7fd607 | 2015-10-19 13:37:40 +0900 | [diff] [blame] | 100 | EXYNOS_PRNG_STATUS_OFFSET) & PRNG_DONE) && --retry) |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 101 | cpu_relax(); |
Krzysztof Kozlowski | f1925d7 | 2016-03-14 09:07:13 +0900 | [diff] [blame] | 102 | if (!retry) { |
| 103 | ret = -ETIMEDOUT; |
| 104 | goto out; |
| 105 | } |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 106 | |
| 107 | exynos_rng_writel(exynos_rng, PRNG_DONE, EXYNOS_PRNG_STATUS_OFFSET); |
| 108 | |
| 109 | *data = exynos_rng_readl(exynos_rng, EXYNOS_PRNG_OUT1_OFFSET); |
| 110 | |
Krzysztof Kozlowski | f1925d7 | 2016-03-14 09:07:13 +0900 | [diff] [blame] | 111 | out: |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 112 | pm_runtime_mark_last_busy(exynos_rng->dev); |
Daniel Thompson | f02b7d0 | 2015-10-16 17:01:51 +0100 | [diff] [blame] | 113 | pm_runtime_put_sync_autosuspend(exynos_rng->dev); |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 114 | |
Krzysztof Kozlowski | f1925d7 | 2016-03-14 09:07:13 +0900 | [diff] [blame] | 115 | return ret; |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 116 | } |
| 117 | |
Greg Kroah-Hartman | bcd2982 | 2012-12-21 15:12:08 -0800 | [diff] [blame] | 118 | static int exynos_rng_probe(struct platform_device *pdev) |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 119 | { |
| 120 | struct exynos_rng *exynos_rng; |
Thierry Reding | c7c9e1c | 2013-01-21 11:08:59 +0100 | [diff] [blame] | 121 | struct resource *res; |
Krzysztof Kozlowski | 48a61e1 | 2016-03-14 09:07:14 +0900 | [diff] [blame] | 122 | int ret; |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 123 | |
| 124 | exynos_rng = devm_kzalloc(&pdev->dev, sizeof(struct exynos_rng), |
| 125 | GFP_KERNEL); |
| 126 | if (!exynos_rng) |
| 127 | return -ENOMEM; |
| 128 | |
| 129 | exynos_rng->dev = &pdev->dev; |
| 130 | exynos_rng->rng.name = "exynos"; |
| 131 | exynos_rng->rng.init = exynos_init; |
| 132 | exynos_rng->rng.read = exynos_read; |
| 133 | exynos_rng->clk = devm_clk_get(&pdev->dev, "secss"); |
| 134 | if (IS_ERR(exynos_rng->clk)) { |
| 135 | dev_err(&pdev->dev, "Couldn't get clock.\n"); |
| 136 | return -ENOENT; |
| 137 | } |
| 138 | |
Thierry Reding | c7c9e1c | 2013-01-21 11:08:59 +0100 | [diff] [blame] | 139 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 140 | exynos_rng->mem = devm_ioremap_resource(&pdev->dev, res); |
| 141 | if (IS_ERR(exynos_rng->mem)) |
| 142 | return PTR_ERR(exynos_rng->mem); |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 143 | |
| 144 | platform_set_drvdata(pdev, exynos_rng); |
| 145 | |
| 146 | pm_runtime_set_autosuspend_delay(&pdev->dev, EXYNOS_AUTOSUSPEND_DELAY); |
| 147 | pm_runtime_use_autosuspend(&pdev->dev); |
| 148 | pm_runtime_enable(&pdev->dev); |
| 149 | |
Krzysztof Kozlowski | 48a61e1 | 2016-03-14 09:07:14 +0900 | [diff] [blame] | 150 | ret = devm_hwrng_register(&pdev->dev, &exynos_rng->rng); |
| 151 | if (ret) { |
| 152 | pm_runtime_dont_use_autosuspend(&pdev->dev); |
| 153 | pm_runtime_disable(&pdev->dev); |
| 154 | } |
| 155 | |
| 156 | return ret; |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 157 | } |
| 158 | |
Krzysztof Kozlowski | 27d80fa | 2016-03-14 09:07:15 +0900 | [diff] [blame] | 159 | static int exynos_rng_remove(struct platform_device *pdev) |
| 160 | { |
| 161 | pm_runtime_dont_use_autosuspend(&pdev->dev); |
| 162 | pm_runtime_disable(&pdev->dev); |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
Arnd Bergmann | b93f342 | 2016-03-02 16:58:59 +0100 | [diff] [blame] | 167 | static int __maybe_unused exynos_rng_runtime_suspend(struct device *dev) |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 168 | { |
| 169 | struct platform_device *pdev = to_platform_device(dev); |
| 170 | struct exynos_rng *exynos_rng = platform_get_drvdata(pdev); |
| 171 | |
| 172 | clk_disable_unprepare(exynos_rng->clk); |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
Arnd Bergmann | b93f342 | 2016-03-02 16:58:59 +0100 | [diff] [blame] | 177 | static int __maybe_unused exynos_rng_runtime_resume(struct device *dev) |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 178 | { |
| 179 | struct platform_device *pdev = to_platform_device(dev); |
| 180 | struct exynos_rng *exynos_rng = platform_get_drvdata(pdev); |
| 181 | |
| 182 | return clk_prepare_enable(exynos_rng->clk); |
| 183 | } |
Krzysztof Kozlowski | bd1dffb | 2015-10-19 13:37:41 +0900 | [diff] [blame] | 184 | |
Arnd Bergmann | b93f342 | 2016-03-02 16:58:59 +0100 | [diff] [blame] | 185 | static int __maybe_unused exynos_rng_suspend(struct device *dev) |
Krzysztof Kozlowski | bd1dffb | 2015-10-19 13:37:41 +0900 | [diff] [blame] | 186 | { |
| 187 | return pm_runtime_force_suspend(dev); |
| 188 | } |
| 189 | |
Arnd Bergmann | b93f342 | 2016-03-02 16:58:59 +0100 | [diff] [blame] | 190 | static int __maybe_unused exynos_rng_resume(struct device *dev) |
Krzysztof Kozlowski | bd1dffb | 2015-10-19 13:37:41 +0900 | [diff] [blame] | 191 | { |
| 192 | struct platform_device *pdev = to_platform_device(dev); |
| 193 | struct exynos_rng *exynos_rng = platform_get_drvdata(pdev); |
| 194 | int ret; |
| 195 | |
| 196 | ret = pm_runtime_force_resume(dev); |
| 197 | if (ret) |
| 198 | return ret; |
| 199 | |
| 200 | return exynos_rng_configure(exynos_rng); |
| 201 | } |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 202 | |
Krzysztof Kozlowski | bd1dffb | 2015-10-19 13:37:41 +0900 | [diff] [blame] | 203 | static const struct dev_pm_ops exynos_rng_pm_ops = { |
| 204 | SET_SYSTEM_SLEEP_PM_OPS(exynos_rng_suspend, exynos_rng_resume) |
| 205 | SET_RUNTIME_PM_OPS(exynos_rng_runtime_suspend, |
| 206 | exynos_rng_runtime_resume, NULL) |
| 207 | }; |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 208 | |
Krzysztof Kozlowski | 642c117 | 2015-10-19 13:37:42 +0900 | [diff] [blame] | 209 | static const struct of_device_id exynos_rng_dt_match[] = { |
| 210 | { |
| 211 | .compatible = "samsung,exynos4-rng", |
| 212 | }, |
| 213 | { }, |
| 214 | }; |
| 215 | MODULE_DEVICE_TABLE(of, exynos_rng_dt_match); |
| 216 | |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 217 | static struct platform_driver exynos_rng_driver = { |
| 218 | .driver = { |
| 219 | .name = "exynos-rng", |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 220 | .pm = &exynos_rng_pm_ops, |
Krzysztof Kozlowski | 642c117 | 2015-10-19 13:37:42 +0900 | [diff] [blame] | 221 | .of_match_table = exynos_rng_dt_match, |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 222 | }, |
| 223 | .probe = exynos_rng_probe, |
Krzysztof Kozlowski | 27d80fa | 2016-03-14 09:07:15 +0900 | [diff] [blame] | 224 | .remove = exynos_rng_remove, |
Jonghwa Lee | b329669 | 2012-06-29 09:43:26 +0900 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | module_platform_driver(exynos_rng_driver); |
| 228 | |
| 229 | MODULE_DESCRIPTION("EXYNOS 4 H/W Random Number Generator driver"); |
| 230 | MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>"); |
| 231 | MODULE_LICENSE("GPL"); |