Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 1 | /* |
David Brownell | c49a7f1 | 2008-04-16 19:24:42 +0800 | [diff] [blame] | 2 | * omap-rng.c - RNG driver for TI OMAP CPU family |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 3 | * |
| 4 | * Author: Deepak Saxena <dsaxena@plexity.net> |
| 5 | * |
| 6 | * Copyright 2005 (c) MontaVista Software, Inc. |
| 7 | * |
| 8 | * Mostly based on original driver: |
| 9 | * |
| 10 | * Copyright (C) 2005 Nokia Corporation |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 11 | * Author: Juha Yrjölä <juha.yrjola@nokia.com> |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 12 | * |
| 13 | * This file is licensed under the terms of the GNU General Public |
| 14 | * License version 2. This program is licensed "as is" without any |
| 15 | * warranty of any kind, whether express or implied. |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/random.h> |
| 21 | #include <linux/err.h> |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 23 | #include <linux/hw_random.h> |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 24 | #include <linux/delay.h> |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 25 | #include <linux/slab.h> |
Paul Walmsley | 665d92f | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 26 | #include <linux/pm_runtime.h> |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 27 | |
| 28 | #include <asm/io.h> |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 29 | |
| 30 | #define RNG_OUT_REG 0x00 /* Output register */ |
| 31 | #define RNG_STAT_REG 0x04 /* Status register |
| 32 | [0] = STAT_BUSY */ |
| 33 | #define RNG_ALARM_REG 0x24 /* Alarm register |
| 34 | [7:0] = ALARM_COUNTER */ |
| 35 | #define RNG_CONFIG_REG 0x28 /* Configuration register |
| 36 | [11:6] = RESET_COUNT |
| 37 | [5:3] = RING2_DELAY |
| 38 | [2:0] = RING1_DELAY */ |
| 39 | #define RNG_REV_REG 0x3c /* Revision register |
| 40 | [7:0] = REV_NB */ |
| 41 | #define RNG_MASK_REG 0x40 /* Mask and reset register |
| 42 | [2] = IT_EN |
| 43 | [1] = SOFTRESET |
| 44 | [0] = AUTOIDLE */ |
| 45 | #define RNG_SYSSTATUS 0x44 /* System status |
| 46 | [0] = RESETDONE */ |
| 47 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 48 | /** |
| 49 | * struct omap_rng_private_data - RNG IP block-specific data |
| 50 | * @base: virtual address of the beginning of the RNG IP block registers |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 51 | * @mem_res: struct resource * for the IP block registers physical memory |
| 52 | */ |
| 53 | struct omap_rng_private_data { |
| 54 | void __iomem *base; |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 55 | struct resource *mem_res; |
| 56 | }; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 57 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 58 | static inline u32 omap_rng_read_reg(struct omap_rng_private_data *priv, int reg) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 59 | { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 60 | return __raw_readl(priv->base + reg); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 63 | static inline void omap_rng_write_reg(struct omap_rng_private_data *priv, |
| 64 | int reg, u32 val) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 65 | { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 66 | __raw_writel(val, priv->base + reg); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 69 | static int omap_rng_data_present(struct hwrng *rng, int wait) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 70 | { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 71 | struct omap_rng_private_data *priv; |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 72 | int data, i; |
| 73 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 74 | priv = (struct omap_rng_private_data *)rng->priv; |
| 75 | |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 76 | for (i = 0; i < 20; i++) { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 77 | data = omap_rng_read_reg(priv, RNG_STAT_REG) ? 0 : 1; |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 78 | if (data || !wait) |
| 79 | break; |
David Brownell | c49a7f1 | 2008-04-16 19:24:42 +0800 | [diff] [blame] | 80 | /* RNG produces data fast enough (2+ MBit/sec, even |
| 81 | * during "rngtest" loads, that these delays don't |
| 82 | * seem to trigger. We *could* use the RNG IRQ, but |
| 83 | * that'd be higher overhead ... so why bother? |
| 84 | */ |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 85 | udelay(10); |
| 86 | } |
| 87 | return data; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static int omap_rng_data_read(struct hwrng *rng, u32 *data) |
| 91 | { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 92 | struct omap_rng_private_data *priv; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 93 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 94 | priv = (struct omap_rng_private_data *)rng->priv; |
| 95 | |
| 96 | *data = omap_rng_read_reg(priv, RNG_OUT_REG); |
| 97 | |
| 98 | return sizeof(u32); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static struct hwrng omap_rng_ops = { |
| 102 | .name = "omap", |
| 103 | .data_present = omap_rng_data_present, |
| 104 | .data_read = omap_rng_data_read, |
| 105 | }; |
| 106 | |
Greg Kroah-Hartman | bcd2982 | 2012-12-21 15:12:08 -0800 | [diff] [blame] | 107 | static int omap_rng_probe(struct platform_device *pdev) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 108 | { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 109 | struct omap_rng_private_data *priv; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 110 | int ret; |
| 111 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 112 | priv = kzalloc(sizeof(struct omap_rng_private_data), GFP_KERNEL); |
| 113 | if (!priv) { |
| 114 | dev_err(&pdev->dev, "could not allocate memory\n"); |
| 115 | return -ENOMEM; |
| 116 | }; |
| 117 | |
| 118 | omap_rng_ops.priv = (unsigned long)priv; |
| 119 | dev_set_drvdata(&pdev->dev, priv); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 120 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 121 | priv->mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Thierry Reding | c7c9e1c | 2013-01-21 11:08:59 +0100 | [diff] [blame] | 122 | priv->base = devm_ioremap_resource(&pdev->dev, priv->mem_res); |
| 123 | if (IS_ERR(priv->base)) { |
| 124 | ret = PTR_ERR(priv->base); |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 125 | goto err_ioremap; |
| 126 | } |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 127 | dev_set_drvdata(&pdev->dev, priv); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 128 | |
Paul Walmsley | 665d92f | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 129 | pm_runtime_enable(&pdev->dev); |
| 130 | pm_runtime_get_sync(&pdev->dev); |
| 131 | |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 132 | ret = hwrng_register(&omap_rng_ops); |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 133 | if (ret) |
| 134 | goto err_register; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 135 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 136 | dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n", |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 137 | omap_rng_read_reg(priv, RNG_REV_REG)); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 138 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 139 | omap_rng_write_reg(priv, RNG_MASK_REG, 0x1); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 140 | |
| 141 | return 0; |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 142 | |
| 143 | err_register: |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 144 | priv->base = NULL; |
Paul Walmsley | 665d92f | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 145 | pm_runtime_disable(&pdev->dev); |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 146 | err_ioremap: |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 147 | kfree(priv); |
| 148 | |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 149 | return ret; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 150 | } |
| 151 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 152 | static int __exit omap_rng_remove(struct platform_device *pdev) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 153 | { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 154 | struct omap_rng_private_data *priv = dev_get_drvdata(&pdev->dev); |
| 155 | |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 156 | hwrng_unregister(&omap_rng_ops); |
| 157 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 158 | omap_rng_write_reg(priv, RNG_MASK_REG, 0x0); |
| 159 | |
Paul Walmsley | 665d92f | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 160 | pm_runtime_put_sync(&pdev->dev); |
| 161 | pm_runtime_disable(&pdev->dev); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 162 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 163 | release_mem_region(priv->mem_res->start, resource_size(priv->mem_res)); |
| 164 | |
| 165 | kfree(priv); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Arnd Bergmann | 59596df | 2012-08-04 07:11:34 +0000 | [diff] [blame] | 170 | #ifdef CONFIG_PM_SLEEP |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 171 | |
Rafael J. Wysocki | 7650572 | 2012-07-06 19:08:53 +0200 | [diff] [blame] | 172 | static int omap_rng_suspend(struct device *dev) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 173 | { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 174 | struct omap_rng_private_data *priv = dev_get_drvdata(dev); |
| 175 | |
| 176 | omap_rng_write_reg(priv, RNG_MASK_REG, 0x0); |
Paul Walmsley | 665d92f | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 177 | pm_runtime_put_sync(dev); |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 178 | |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
Rafael J. Wysocki | 7650572 | 2012-07-06 19:08:53 +0200 | [diff] [blame] | 182 | static int omap_rng_resume(struct device *dev) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 183 | { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 184 | struct omap_rng_private_data *priv = dev_get_drvdata(dev); |
| 185 | |
Paul Walmsley | 665d92f | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 186 | pm_runtime_get_sync(dev); |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame] | 187 | omap_rng_write_reg(priv, RNG_MASK_REG, 0x1); |
| 188 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 189 | return 0; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Rafael J. Wysocki | 7650572 | 2012-07-06 19:08:53 +0200 | [diff] [blame] | 192 | static SIMPLE_DEV_PM_OPS(omap_rng_pm, omap_rng_suspend, omap_rng_resume); |
| 193 | #define OMAP_RNG_PM (&omap_rng_pm) |
| 194 | |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 195 | #else |
| 196 | |
Rafael J. Wysocki | 7650572 | 2012-07-06 19:08:53 +0200 | [diff] [blame] | 197 | #define OMAP_RNG_PM NULL |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 198 | |
| 199 | #endif |
| 200 | |
David Brownell | c49a7f1 | 2008-04-16 19:24:42 +0800 | [diff] [blame] | 201 | /* work with hotplug and coldplug */ |
| 202 | MODULE_ALIAS("platform:omap_rng"); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 203 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 204 | static struct platform_driver omap_rng_driver = { |
| 205 | .driver = { |
| 206 | .name = "omap_rng", |
| 207 | .owner = THIS_MODULE, |
Rafael J. Wysocki | 7650572 | 2012-07-06 19:08:53 +0200 | [diff] [blame] | 208 | .pm = OMAP_RNG_PM, |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 209 | }, |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 210 | .probe = omap_rng_probe, |
| 211 | .remove = __exit_p(omap_rng_remove), |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | static int __init omap_rng_init(void) |
| 215 | { |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 216 | return platform_driver_register(&omap_rng_driver); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static void __exit omap_rng_exit(void) |
| 220 | { |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 221 | platform_driver_unregister(&omap_rng_driver); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | module_init(omap_rng_init); |
| 225 | module_exit(omap_rng_exit); |
| 226 | |
| 227 | MODULE_AUTHOR("Deepak Saxena (and others)"); |
| 228 | MODULE_LICENSE("GPL"); |