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> |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 21 | #include <linux/clk.h> |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 22 | #include <linux/err.h> |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 23 | #include <linux/platform_device.h> |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 24 | #include <linux/hw_random.h> |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 25 | #include <linux/delay.h> |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 26 | #include <linux/slab.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 | |
Tony Lindgren | 2c799ce | 2012-02-24 10:34:35 -0800 | [diff] [blame] | 30 | #include <plat/cpu.h> |
| 31 | |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 32 | #define RNG_OUT_REG 0x00 /* Output register */ |
| 33 | #define RNG_STAT_REG 0x04 /* Status register |
| 34 | [0] = STAT_BUSY */ |
| 35 | #define RNG_ALARM_REG 0x24 /* Alarm register |
| 36 | [7:0] = ALARM_COUNTER */ |
| 37 | #define RNG_CONFIG_REG 0x28 /* Configuration register |
| 38 | [11:6] = RESET_COUNT |
| 39 | [5:3] = RING2_DELAY |
| 40 | [2:0] = RING1_DELAY */ |
| 41 | #define RNG_REV_REG 0x3c /* Revision register |
| 42 | [7:0] = REV_NB */ |
| 43 | #define RNG_MASK_REG 0x40 /* Mask and reset register |
| 44 | [2] = IT_EN |
| 45 | [1] = SOFTRESET |
| 46 | [0] = AUTOIDLE */ |
| 47 | #define RNG_SYSSTATUS 0x44 /* System status |
| 48 | [0] = RESETDONE */ |
| 49 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 50 | /** |
| 51 | * struct omap_rng_private_data - RNG IP block-specific data |
| 52 | * @base: virtual address of the beginning of the RNG IP block registers |
| 53 | * @clk: RNG clock |
| 54 | * @mem_res: struct resource * for the IP block registers physical memory |
| 55 | */ |
| 56 | struct omap_rng_private_data { |
| 57 | void __iomem *base; |
| 58 | struct clk *clk; |
| 59 | struct resource *mem_res; |
| 60 | }; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 61 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 62 | 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] | 63 | { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 64 | return __raw_readl(priv->base + reg); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 67 | static inline void omap_rng_write_reg(struct omap_rng_private_data *priv, |
| 68 | int reg, u32 val) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 69 | { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 70 | __raw_writel(val, priv->base + reg); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 73 | static int omap_rng_data_present(struct hwrng *rng, int wait) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 74 | { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 75 | struct omap_rng_private_data *priv; |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 76 | int data, i; |
| 77 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 78 | priv = (struct omap_rng_private_data *)rng->priv; |
| 79 | |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 80 | for (i = 0; i < 20; i++) { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 81 | data = omap_rng_read_reg(priv, RNG_STAT_REG) ? 0 : 1; |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 82 | if (data || !wait) |
| 83 | break; |
David Brownell | c49a7f1 | 2008-04-16 19:24:42 +0800 | [diff] [blame] | 84 | /* RNG produces data fast enough (2+ MBit/sec, even |
| 85 | * during "rngtest" loads, that these delays don't |
| 86 | * seem to trigger. We *could* use the RNG IRQ, but |
| 87 | * that'd be higher overhead ... so why bother? |
| 88 | */ |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 89 | udelay(10); |
| 90 | } |
| 91 | return data; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static int omap_rng_data_read(struct hwrng *rng, u32 *data) |
| 95 | { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 96 | struct omap_rng_private_data *priv; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 97 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 98 | priv = (struct omap_rng_private_data *)rng->priv; |
| 99 | |
| 100 | *data = omap_rng_read_reg(priv, RNG_OUT_REG); |
| 101 | |
| 102 | return sizeof(u32); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static struct hwrng omap_rng_ops = { |
| 106 | .name = "omap", |
| 107 | .data_present = omap_rng_data_present, |
| 108 | .data_read = omap_rng_data_read, |
| 109 | }; |
| 110 | |
Uwe Kleine-König | 9f171ad | 2009-03-29 15:47:06 +0800 | [diff] [blame] | 111 | static int __devinit omap_rng_probe(struct platform_device *pdev) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 112 | { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 113 | struct omap_rng_private_data *priv; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 114 | int ret; |
| 115 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 116 | priv = kzalloc(sizeof(struct omap_rng_private_data), GFP_KERNEL); |
| 117 | if (!priv) { |
| 118 | dev_err(&pdev->dev, "could not allocate memory\n"); |
| 119 | return -ENOMEM; |
| 120 | }; |
| 121 | |
| 122 | omap_rng_ops.priv = (unsigned long)priv; |
| 123 | dev_set_drvdata(&pdev->dev, priv); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 124 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 125 | if (cpu_is_omap24xx()) { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 126 | priv->clk = clk_get(&pdev->dev, "ick"); |
| 127 | if (IS_ERR(priv->clk)) { |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 128 | dev_err(&pdev->dev, "Could not get rng_ick\n"); |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 129 | ret = PTR_ERR(priv->clk); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 130 | return ret; |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 131 | } else { |
| 132 | clk_enable(priv->clk); |
| 133 | } |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 136 | priv->mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 137 | if (!priv->mem_res) { |
| 138 | ret = -ENOENT; |
| 139 | goto err_ioremap; |
| 140 | } |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 141 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 142 | priv->base = devm_request_and_ioremap(&pdev->dev, priv->mem_res); |
| 143 | if (!priv->base) { |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 144 | ret = -ENOMEM; |
| 145 | goto err_ioremap; |
| 146 | } |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 147 | dev_set_drvdata(&pdev->dev, priv); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 148 | |
| 149 | ret = hwrng_register(&omap_rng_ops); |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 150 | if (ret) |
| 151 | goto err_register; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 152 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 153 | dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n", |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 154 | omap_rng_read_reg(priv, RNG_REV_REG)); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 155 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 156 | |
| 157 | omap_rng_write_reg(priv, RNG_MASK_REG, 0x1); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 158 | |
| 159 | return 0; |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 160 | |
| 161 | err_register: |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 162 | priv->base = NULL; |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 163 | err_ioremap: |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 164 | if (cpu_is_omap24xx()) { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 165 | clk_disable(priv->clk); |
| 166 | clk_put(priv->clk); |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 167 | } |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 168 | |
| 169 | kfree(priv); |
| 170 | |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 171 | return ret; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 172 | } |
| 173 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 174 | static int __exit omap_rng_remove(struct platform_device *pdev) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 175 | { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 176 | struct omap_rng_private_data *priv = dev_get_drvdata(&pdev->dev); |
| 177 | |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 178 | hwrng_unregister(&omap_rng_ops); |
| 179 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 180 | omap_rng_write_reg(priv, RNG_MASK_REG, 0x0); |
| 181 | |
| 182 | iounmap(priv->base); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 183 | |
| 184 | if (cpu_is_omap24xx()) { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 185 | clk_disable(priv->clk); |
| 186 | clk_put(priv->clk); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 189 | release_mem_region(priv->mem_res->start, resource_size(priv->mem_res)); |
| 190 | |
| 191 | kfree(priv); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Arnd Bergmann | 59596df | 2012-08-04 07:11:34 +0000 | [diff] [blame] | 196 | #ifdef CONFIG_PM_SLEEP |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 197 | |
Rafael J. Wysocki | 7650572 | 2012-07-06 19:08:53 +0200 | [diff] [blame] | 198 | static int omap_rng_suspend(struct device *dev) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 199 | { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 200 | struct omap_rng_private_data *priv = dev_get_drvdata(dev); |
| 201 | |
| 202 | omap_rng_write_reg(priv, RNG_MASK_REG, 0x0); |
| 203 | |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
Rafael J. Wysocki | 7650572 | 2012-07-06 19:08:53 +0200 | [diff] [blame] | 207 | static int omap_rng_resume(struct device *dev) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 208 | { |
Paul Walmsley | 0266636 | 2012-09-23 17:28:26 -0600 | [diff] [blame^] | 209 | struct omap_rng_private_data *priv = dev_get_drvdata(dev); |
| 210 | |
| 211 | omap_rng_write_reg(priv, RNG_MASK_REG, 0x1); |
| 212 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 213 | return 0; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Rafael J. Wysocki | 7650572 | 2012-07-06 19:08:53 +0200 | [diff] [blame] | 216 | static SIMPLE_DEV_PM_OPS(omap_rng_pm, omap_rng_suspend, omap_rng_resume); |
| 217 | #define OMAP_RNG_PM (&omap_rng_pm) |
| 218 | |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 219 | #else |
| 220 | |
Rafael J. Wysocki | 7650572 | 2012-07-06 19:08:53 +0200 | [diff] [blame] | 221 | #define OMAP_RNG_PM NULL |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 222 | |
| 223 | #endif |
| 224 | |
David Brownell | c49a7f1 | 2008-04-16 19:24:42 +0800 | [diff] [blame] | 225 | /* work with hotplug and coldplug */ |
| 226 | MODULE_ALIAS("platform:omap_rng"); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 227 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 228 | static struct platform_driver omap_rng_driver = { |
| 229 | .driver = { |
| 230 | .name = "omap_rng", |
| 231 | .owner = THIS_MODULE, |
Rafael J. Wysocki | 7650572 | 2012-07-06 19:08:53 +0200 | [diff] [blame] | 232 | .pm = OMAP_RNG_PM, |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 233 | }, |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 234 | .probe = omap_rng_probe, |
| 235 | .remove = __exit_p(omap_rng_remove), |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | static int __init omap_rng_init(void) |
| 239 | { |
| 240 | if (!cpu_is_omap16xx() && !cpu_is_omap24xx()) |
| 241 | return -ENODEV; |
| 242 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 243 | return platform_driver_register(&omap_rng_driver); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static void __exit omap_rng_exit(void) |
| 247 | { |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 248 | platform_driver_unregister(&omap_rng_driver); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | module_init(omap_rng_init); |
| 252 | module_exit(omap_rng_exit); |
| 253 | |
| 254 | MODULE_AUTHOR("Deepak Saxena (and others)"); |
| 255 | MODULE_LICENSE("GPL"); |