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> |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 26 | |
| 27 | #include <asm/io.h> |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 28 | |
Tony Lindgren | 2c799ce | 2012-02-24 10:34:35 -0800 | [diff] [blame] | 29 | #include <plat/cpu.h> |
| 30 | |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 31 | #define RNG_OUT_REG 0x00 /* Output register */ |
| 32 | #define RNG_STAT_REG 0x04 /* Status register |
| 33 | [0] = STAT_BUSY */ |
| 34 | #define RNG_ALARM_REG 0x24 /* Alarm register |
| 35 | [7:0] = ALARM_COUNTER */ |
| 36 | #define RNG_CONFIG_REG 0x28 /* Configuration register |
| 37 | [11:6] = RESET_COUNT |
| 38 | [5:3] = RING2_DELAY |
| 39 | [2:0] = RING1_DELAY */ |
| 40 | #define RNG_REV_REG 0x3c /* Revision register |
| 41 | [7:0] = REV_NB */ |
| 42 | #define RNG_MASK_REG 0x40 /* Mask and reset register |
| 43 | [2] = IT_EN |
| 44 | [1] = SOFTRESET |
| 45 | [0] = AUTOIDLE */ |
| 46 | #define RNG_SYSSTATUS 0x44 /* System status |
| 47 | [0] = RESETDONE */ |
| 48 | |
| 49 | static void __iomem *rng_base; |
| 50 | static struct clk *rng_ick; |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 51 | static struct platform_device *rng_dev; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 52 | |
David Brownell | c49a7f1 | 2008-04-16 19:24:42 +0800 | [diff] [blame] | 53 | static inline u32 omap_rng_read_reg(int reg) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 54 | { |
| 55 | return __raw_readl(rng_base + reg); |
| 56 | } |
| 57 | |
David Brownell | c49a7f1 | 2008-04-16 19:24:42 +0800 | [diff] [blame] | 58 | static inline void omap_rng_write_reg(int reg, u32 val) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 59 | { |
| 60 | __raw_writel(val, rng_base + reg); |
| 61 | } |
| 62 | |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 63 | static int omap_rng_data_present(struct hwrng *rng, int wait) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 64 | { |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 65 | int data, i; |
| 66 | |
| 67 | for (i = 0; i < 20; i++) { |
| 68 | data = omap_rng_read_reg(RNG_STAT_REG) ? 0 : 1; |
| 69 | if (data || !wait) |
| 70 | break; |
David Brownell | c49a7f1 | 2008-04-16 19:24:42 +0800 | [diff] [blame] | 71 | /* RNG produces data fast enough (2+ MBit/sec, even |
| 72 | * during "rngtest" loads, that these delays don't |
| 73 | * seem to trigger. We *could* use the RNG IRQ, but |
| 74 | * that'd be higher overhead ... so why bother? |
| 75 | */ |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 76 | udelay(10); |
| 77 | } |
| 78 | return data; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static int omap_rng_data_read(struct hwrng *rng, u32 *data) |
| 82 | { |
| 83 | *data = omap_rng_read_reg(RNG_OUT_REG); |
| 84 | |
| 85 | return 4; |
| 86 | } |
| 87 | |
| 88 | static struct hwrng omap_rng_ops = { |
| 89 | .name = "omap", |
| 90 | .data_present = omap_rng_data_present, |
| 91 | .data_read = omap_rng_data_read, |
| 92 | }; |
| 93 | |
Uwe Kleine-König | 9f171ad | 2009-03-29 15:47:06 +0800 | [diff] [blame] | 94 | static int __devinit omap_rng_probe(struct platform_device *pdev) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 95 | { |
Julia Lawall | c652759 | 2011-02-16 13:05:54 +1100 | [diff] [blame] | 96 | struct resource *res; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 97 | int ret; |
| 98 | |
| 99 | /* |
| 100 | * A bit ugly, and it will never actually happen but there can |
| 101 | * be only one RNG and this catches any bork |
| 102 | */ |
David Brownell | c49a7f1 | 2008-04-16 19:24:42 +0800 | [diff] [blame] | 103 | if (rng_dev) |
| 104 | return -EBUSY; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 105 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 106 | if (cpu_is_omap24xx()) { |
Russell King | eeec7c8 | 2009-01-19 20:58:56 +0000 | [diff] [blame] | 107 | rng_ick = clk_get(&pdev->dev, "ick"); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 108 | if (IS_ERR(rng_ick)) { |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 109 | dev_err(&pdev->dev, "Could not get rng_ick\n"); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 110 | ret = PTR_ERR(rng_ick); |
| 111 | return ret; |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 112 | } else |
| 113 | clk_enable(rng_ick); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 117 | |
Julia Lawall | 945478a | 2012-04-19 19:19:08 +0200 | [diff] [blame] | 118 | rng_base = devm_request_and_ioremap(&pdev->dev, res); |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 119 | if (!rng_base) { |
| 120 | ret = -ENOMEM; |
| 121 | goto err_ioremap; |
| 122 | } |
Julia Lawall | 945478a | 2012-04-19 19:19:08 +0200 | [diff] [blame] | 123 | dev_set_drvdata(&pdev->dev, res); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 124 | |
| 125 | ret = hwrng_register(&omap_rng_ops); |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 126 | if (ret) |
| 127 | goto err_register; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 128 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 129 | dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n", |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 130 | omap_rng_read_reg(RNG_REV_REG)); |
| 131 | omap_rng_write_reg(RNG_MASK_REG, 0x1); |
| 132 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 133 | rng_dev = pdev; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 134 | |
| 135 | return 0; |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 136 | |
| 137 | err_register: |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 138 | rng_base = NULL; |
| 139 | err_ioremap: |
Russell King | 55c381e | 2008-09-04 14:07:22 +0100 | [diff] [blame] | 140 | if (cpu_is_omap24xx()) { |
| 141 | clk_disable(rng_ick); |
| 142 | clk_put(rng_ick); |
| 143 | } |
| 144 | return ret; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 145 | } |
| 146 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 147 | static int __exit omap_rng_remove(struct platform_device *pdev) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 148 | { |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 149 | hwrng_unregister(&omap_rng_ops); |
| 150 | |
| 151 | omap_rng_write_reg(RNG_MASK_REG, 0x0); |
| 152 | |
| 153 | if (cpu_is_omap24xx()) { |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 154 | clk_disable(rng_ick); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 155 | clk_put(rng_ick); |
| 156 | } |
| 157 | |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 158 | rng_base = NULL; |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | #ifdef CONFIG_PM |
| 164 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 165 | static int omap_rng_suspend(struct platform_device *pdev, pm_message_t message) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 166 | { |
| 167 | omap_rng_write_reg(RNG_MASK_REG, 0x0); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 171 | static int omap_rng_resume(struct platform_device *pdev) |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 172 | { |
| 173 | omap_rng_write_reg(RNG_MASK_REG, 0x1); |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 174 | return 0; |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | #else |
| 178 | |
| 179 | #define omap_rng_suspend NULL |
| 180 | #define omap_rng_resume NULL |
| 181 | |
| 182 | #endif |
| 183 | |
David Brownell | c49a7f1 | 2008-04-16 19:24:42 +0800 | [diff] [blame] | 184 | /* work with hotplug and coldplug */ |
| 185 | MODULE_ALIAS("platform:omap_rng"); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 186 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 187 | static struct platform_driver omap_rng_driver = { |
| 188 | .driver = { |
| 189 | .name = "omap_rng", |
| 190 | .owner = THIS_MODULE, |
| 191 | }, |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 192 | .probe = omap_rng_probe, |
| 193 | .remove = __exit_p(omap_rng_remove), |
| 194 | .suspend = omap_rng_suspend, |
| 195 | .resume = omap_rng_resume |
| 196 | }; |
| 197 | |
| 198 | static int __init omap_rng_init(void) |
| 199 | { |
| 200 | if (!cpu_is_omap16xx() && !cpu_is_omap24xx()) |
| 201 | return -ENODEV; |
| 202 | |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 203 | return platform_driver_register(&omap_rng_driver); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static void __exit omap_rng_exit(void) |
| 207 | { |
David Brownell | af2bc7d | 2006-08-05 12:14:04 -0700 | [diff] [blame] | 208 | platform_driver_unregister(&omap_rng_driver); |
Michael Buesch | ebc915a | 2006-06-26 00:25:03 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | module_init(omap_rng_init); |
| 212 | module_exit(omap_rng_exit); |
| 213 | |
| 214 | MODULE_AUTHOR("Deepak Saxena (and others)"); |
| 215 | MODULE_LICENSE("GPL"); |