Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 1 | /* |
| 2 | * RNG driver for Freescale RNGA |
| 3 | * |
| 4 | * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved. |
| 5 | * Author: Alan Carvalho de Assis <acassis@gmail.com> |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * The code contained herein is licensed under the GNU General Public |
| 10 | * License. You may obtain a copy of the GNU General Public License |
| 11 | * Version 2 or later at the following locations: |
| 12 | * |
| 13 | * http://www.opensource.org/licenses/gpl-license.html |
| 14 | * http://www.gnu.org/copyleft/gpl.html |
| 15 | * |
| 16 | * This driver is based on other RNG drivers. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/clk.h> |
| 23 | #include <linux/err.h> |
| 24 | #include <linux/ioport.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/hw_random.h> |
Benoît Thébaudeau | 3621189 | 2012-06-13 18:15:34 +0200 | [diff] [blame] | 27 | #include <linux/delay.h> |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 28 | #include <linux/io.h> |
| 29 | |
| 30 | /* RNGA Registers */ |
| 31 | #define RNGA_CONTROL 0x00 |
| 32 | #define RNGA_STATUS 0x04 |
| 33 | #define RNGA_ENTROPY 0x08 |
| 34 | #define RNGA_OUTPUT_FIFO 0x0c |
| 35 | #define RNGA_MODE 0x10 |
| 36 | #define RNGA_VERIFICATION_CONTROL 0x14 |
| 37 | #define RNGA_OSC_CONTROL_COUNTER 0x18 |
| 38 | #define RNGA_OSC1_COUNTER 0x1c |
| 39 | #define RNGA_OSC2_COUNTER 0x20 |
| 40 | #define RNGA_OSC_COUNTER_STATUS 0x24 |
| 41 | |
| 42 | /* RNGA Registers Range */ |
| 43 | #define RNG_ADDR_RANGE 0x28 |
| 44 | |
| 45 | /* RNGA Control Register */ |
| 46 | #define RNGA_CONTROL_SLEEP 0x00000010 |
| 47 | #define RNGA_CONTROL_CLEAR_INT 0x00000008 |
| 48 | #define RNGA_CONTROL_MASK_INTS 0x00000004 |
| 49 | #define RNGA_CONTROL_HIGH_ASSURANCE 0x00000002 |
| 50 | #define RNGA_CONTROL_GO 0x00000001 |
| 51 | |
| 52 | #define RNGA_STATUS_LEVEL_MASK 0x0000ff00 |
| 53 | |
| 54 | /* RNGA Status Register */ |
| 55 | #define RNGA_STATUS_OSC_DEAD 0x80000000 |
| 56 | #define RNGA_STATUS_SLEEP 0x00000010 |
| 57 | #define RNGA_STATUS_ERROR_INT 0x00000008 |
| 58 | #define RNGA_STATUS_FIFO_UNDERFLOW 0x00000004 |
| 59 | #define RNGA_STATUS_LAST_READ_STATUS 0x00000002 |
| 60 | #define RNGA_STATUS_SECURITY_VIOLATION 0x00000001 |
| 61 | |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 62 | struct mxc_rng { |
| 63 | struct device *dev; |
| 64 | struct hwrng rng; |
| 65 | void __iomem *mem; |
| 66 | struct clk *clk; |
| 67 | }; |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 68 | |
Benoît Thébaudeau | 3621189 | 2012-06-13 18:15:34 +0200 | [diff] [blame] | 69 | static int mxc_rnga_data_present(struct hwrng *rng, int wait) |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 70 | { |
Benoît Thébaudeau | 3621189 | 2012-06-13 18:15:34 +0200 | [diff] [blame] | 71 | int i; |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 72 | struct mxc_rng *mxc_rng = container_of(rng, struct mxc_rng, rng); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 73 | |
Benoît Thébaudeau | 3621189 | 2012-06-13 18:15:34 +0200 | [diff] [blame] | 74 | for (i = 0; i < 20; i++) { |
| 75 | /* how many random numbers are in FIFO? [0-16] */ |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 76 | int level = (__raw_readl(mxc_rng->mem + RNGA_STATUS) & |
Benoît Thébaudeau | 3621189 | 2012-06-13 18:15:34 +0200 | [diff] [blame] | 77 | RNGA_STATUS_LEVEL_MASK) >> 8; |
| 78 | if (level || !wait) |
| 79 | return !!level; |
| 80 | udelay(10); |
| 81 | } |
| 82 | return 0; |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static int mxc_rnga_data_read(struct hwrng *rng, u32 * data) |
| 86 | { |
| 87 | int err; |
| 88 | u32 ctrl; |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 89 | struct mxc_rng *mxc_rng = container_of(rng, struct mxc_rng, rng); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 90 | |
| 91 | /* retrieve a random number from FIFO */ |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 92 | *data = __raw_readl(mxc_rng->mem + RNGA_OUTPUT_FIFO); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 93 | |
| 94 | /* some error while reading this random number? */ |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 95 | err = __raw_readl(mxc_rng->mem + RNGA_STATUS) & RNGA_STATUS_ERROR_INT; |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 96 | |
| 97 | /* if error: clear error interrupt, but doesn't return random number */ |
| 98 | if (err) { |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 99 | dev_dbg(mxc_rng->dev, "Error while reading random number!\n"); |
| 100 | ctrl = __raw_readl(mxc_rng->mem + RNGA_CONTROL); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 101 | __raw_writel(ctrl | RNGA_CONTROL_CLEAR_INT, |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 102 | mxc_rng->mem + RNGA_CONTROL); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 103 | return 0; |
| 104 | } else |
| 105 | return 4; |
| 106 | } |
| 107 | |
| 108 | static int mxc_rnga_init(struct hwrng *rng) |
| 109 | { |
| 110 | u32 ctrl, osc; |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 111 | struct mxc_rng *mxc_rng = container_of(rng, struct mxc_rng, rng); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 112 | |
| 113 | /* wake up */ |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 114 | ctrl = __raw_readl(mxc_rng->mem + RNGA_CONTROL); |
| 115 | __raw_writel(ctrl & ~RNGA_CONTROL_SLEEP, mxc_rng->mem + RNGA_CONTROL); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 116 | |
| 117 | /* verify if oscillator is working */ |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 118 | osc = __raw_readl(mxc_rng->mem + RNGA_STATUS); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 119 | if (osc & RNGA_STATUS_OSC_DEAD) { |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 120 | dev_err(mxc_rng->dev, "RNGA Oscillator is dead!\n"); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 121 | return -ENODEV; |
| 122 | } |
| 123 | |
| 124 | /* go running */ |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 125 | ctrl = __raw_readl(mxc_rng->mem + RNGA_CONTROL); |
| 126 | __raw_writel(ctrl | RNGA_CONTROL_GO, mxc_rng->mem + RNGA_CONTROL); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static void mxc_rnga_cleanup(struct hwrng *rng) |
| 132 | { |
| 133 | u32 ctrl; |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 134 | struct mxc_rng *mxc_rng = container_of(rng, struct mxc_rng, rng); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 135 | |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 136 | ctrl = __raw_readl(mxc_rng->mem + RNGA_CONTROL); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 137 | |
| 138 | /* stop rnga */ |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 139 | __raw_writel(ctrl & ~RNGA_CONTROL_GO, mxc_rng->mem + RNGA_CONTROL); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 140 | } |
| 141 | |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 142 | static int __init mxc_rnga_probe(struct platform_device *pdev) |
| 143 | { |
| 144 | int err = -ENODEV; |
Fabio Estevam | 264878e | 2013-03-13 00:57:27 -0300 | [diff] [blame^] | 145 | struct resource *res; |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 146 | struct mxc_rng *mxc_rng; |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 147 | |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 148 | mxc_rng = devm_kzalloc(&pdev->dev, sizeof(struct mxc_rng), |
| 149 | GFP_KERNEL); |
| 150 | if (!mxc_rng) |
| 151 | return -ENOMEM; |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 152 | |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 153 | mxc_rng->dev = &pdev->dev; |
| 154 | mxc_rng->rng.name = "mxc-rnga"; |
| 155 | mxc_rng->rng.init = mxc_rnga_init; |
| 156 | mxc_rng->rng.cleanup = mxc_rnga_cleanup, |
| 157 | mxc_rng->rng.data_present = mxc_rnga_data_present, |
| 158 | mxc_rng->rng.data_read = mxc_rnga_data_read, |
| 159 | |
| 160 | mxc_rng->clk = devm_clk_get(&pdev->dev, NULL); |
| 161 | if (IS_ERR(mxc_rng->clk)) { |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 162 | dev_err(&pdev->dev, "Could not get rng_clk!\n"); |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 163 | err = PTR_ERR(mxc_rng->clk); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 164 | goto out; |
| 165 | } |
| 166 | |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 167 | clk_prepare_enable(mxc_rng->clk); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 168 | |
| 169 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 170 | if (!res) { |
| 171 | err = -ENOENT; |
| 172 | goto err_region; |
| 173 | } |
| 174 | |
Fabio Estevam | 264878e | 2013-03-13 00:57:27 -0300 | [diff] [blame^] | 175 | mxc_rng->mem = devm_ioremap_resource(&pdev->dev, res); |
| 176 | if (IS_ERR(mxc_rng->mem)) { |
| 177 | err = PTR_ERR(mxc_rng->mem); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 178 | goto err_ioremap; |
| 179 | } |
| 180 | |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 181 | err = hwrng_register(&mxc_rng->rng); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 182 | if (err) { |
| 183 | dev_err(&pdev->dev, "MXC RNGA registering failed (%d)\n", err); |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 184 | goto err_ioremap; |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 185 | } |
| 186 | |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 187 | dev_info(&pdev->dev, "MXC RNGA Registered.\n"); |
| 188 | |
| 189 | return 0; |
| 190 | |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 191 | err_ioremap: |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 192 | err_region: |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 193 | clk_disable_unprepare(mxc_rng->clk); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 194 | |
| 195 | out: |
| 196 | return err; |
| 197 | } |
| 198 | |
| 199 | static int __exit mxc_rnga_remove(struct platform_device *pdev) |
| 200 | { |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 201 | struct mxc_rng *mxc_rng = platform_get_drvdata(pdev); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 202 | |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 203 | hwrng_unregister(&mxc_rng->rng); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 204 | |
Fabio Estevam | 821873a | 2012-09-04 18:35:22 -0300 | [diff] [blame] | 205 | clk_disable_unprepare(mxc_rng->clk); |
Alan Carvalho de Assis | 45001e9 | 2009-04-02 12:38:41 -0300 | [diff] [blame] | 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static struct platform_driver mxc_rnga_driver = { |
| 211 | .driver = { |
| 212 | .name = "mxc_rnga", |
| 213 | .owner = THIS_MODULE, |
| 214 | }, |
| 215 | .remove = __exit_p(mxc_rnga_remove), |
| 216 | }; |
| 217 | |
| 218 | static int __init mod_init(void) |
| 219 | { |
| 220 | return platform_driver_probe(&mxc_rnga_driver, mxc_rnga_probe); |
| 221 | } |
| 222 | |
| 223 | static void __exit mod_exit(void) |
| 224 | { |
| 225 | platform_driver_unregister(&mxc_rnga_driver); |
| 226 | } |
| 227 | |
| 228 | module_init(mod_init); |
| 229 | module_exit(mod_exit); |
| 230 | |
| 231 | MODULE_AUTHOR("Freescale Semiconductor, Inc."); |
| 232 | MODULE_DESCRIPTION("H/W RNGA driver for i.MX"); |
| 233 | MODULE_LICENSE("GPL"); |