| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * RNG driver for Freescale RNGC |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2008-2012 Freescale Semiconductor, Inc. |
|---|
| 5 | 6 | * Copyright (C) 2017 Martin Kaiser <martin@kaiser.cx> |
|---|
| 6 | | - * |
|---|
| 7 | | - * The code contained herein is licensed under the GNU General Public |
|---|
| 8 | | - * License. You may obtain a copy of the GNU General Public License |
|---|
| 9 | | - * Version 2 or later at the following locations: |
|---|
| 10 | | - * |
|---|
| 11 | | - * http://www.opensource.org/licenses/gpl-license.html |
|---|
| 12 | | - * http://www.gnu.org/copyleft/gpl.html |
|---|
| 13 | 7 | */ |
|---|
| 14 | 8 | |
|---|
| 15 | 9 | #include <linux/module.h> |
|---|
| .. | .. |
|---|
| 24 | 18 | #include <linux/completion.h> |
|---|
| 25 | 19 | #include <linux/io.h> |
|---|
| 26 | 20 | |
|---|
| 21 | +#define RNGC_VER_ID 0x0000 |
|---|
| 27 | 22 | #define RNGC_COMMAND 0x0004 |
|---|
| 28 | 23 | #define RNGC_CONTROL 0x0008 |
|---|
| 29 | 24 | #define RNGC_STATUS 0x000C |
|---|
| 30 | 25 | #define RNGC_ERROR 0x0010 |
|---|
| 31 | 26 | #define RNGC_FIFO 0x0014 |
|---|
| 27 | + |
|---|
| 28 | +/* the fields in the ver id register */ |
|---|
| 29 | +#define RNGC_TYPE_SHIFT 28 |
|---|
| 30 | +#define RNGC_VER_MAJ_SHIFT 8 |
|---|
| 31 | + |
|---|
| 32 | +/* the rng_type field */ |
|---|
| 33 | +#define RNGC_TYPE_RNGB 0x1 |
|---|
| 34 | +#define RNGC_TYPE_RNGC 0x2 |
|---|
| 35 | + |
|---|
| 32 | 36 | |
|---|
| 33 | 37 | #define RNGC_CMD_CLR_ERR 0x00000020 |
|---|
| 34 | 38 | #define RNGC_CMD_CLR_INT 0x00000010 |
|---|
| .. | .. |
|---|
| 37 | 41 | |
|---|
| 38 | 42 | #define RNGC_CTRL_MASK_ERROR 0x00000040 |
|---|
| 39 | 43 | #define RNGC_CTRL_MASK_DONE 0x00000020 |
|---|
| 44 | +#define RNGC_CTRL_AUTO_SEED 0x00000010 |
|---|
| 40 | 45 | |
|---|
| 41 | 46 | #define RNGC_STATUS_ERROR 0x00010000 |
|---|
| 42 | 47 | #define RNGC_STATUS_FIFO_LEVEL_MASK 0x00000f00 |
|---|
| .. | .. |
|---|
| 105 | 110 | cmd = readl(rngc->base + RNGC_COMMAND); |
|---|
| 106 | 111 | writel(cmd | RNGC_CMD_SELF_TEST, rngc->base + RNGC_COMMAND); |
|---|
| 107 | 112 | |
|---|
| 108 | | - ret = wait_for_completion_timeout(&rngc->rng_op_done, RNGC_TIMEOUT); |
|---|
| 109 | | - if (!ret) { |
|---|
| 110 | | - imx_rngc_irq_mask_clear(rngc); |
|---|
| 113 | + ret = wait_for_completion_timeout(&rngc->rng_op_done, msecs_to_jiffies(RNGC_TIMEOUT)); |
|---|
| 114 | + imx_rngc_irq_mask_clear(rngc); |
|---|
| 115 | + if (!ret) |
|---|
| 111 | 116 | return -ETIMEDOUT; |
|---|
| 112 | | - } |
|---|
| 113 | 117 | |
|---|
| 114 | | - if (rngc->err_reg != 0) { |
|---|
| 115 | | - imx_rngc_irq_mask_clear(rngc); |
|---|
| 116 | | - return -EIO; |
|---|
| 117 | | - } |
|---|
| 118 | | - |
|---|
| 119 | | - return 0; |
|---|
| 118 | + return rngc->err_reg ? -EIO : 0; |
|---|
| 120 | 119 | } |
|---|
| 121 | 120 | |
|---|
| 122 | 121 | static int imx_rngc_read(struct hwrng *rng, void *data, size_t max, bool wait) |
|---|
| .. | .. |
|---|
| 173 | 172 | static int imx_rngc_init(struct hwrng *rng) |
|---|
| 174 | 173 | { |
|---|
| 175 | 174 | struct imx_rngc *rngc = container_of(rng, struct imx_rngc, rng); |
|---|
| 176 | | - u32 cmd; |
|---|
| 175 | + u32 cmd, ctrl; |
|---|
| 177 | 176 | int ret; |
|---|
| 178 | 177 | |
|---|
| 179 | 178 | /* clear error */ |
|---|
| 180 | 179 | cmd = readl(rngc->base + RNGC_COMMAND); |
|---|
| 181 | 180 | writel(cmd | RNGC_CMD_CLR_ERR, rngc->base + RNGC_COMMAND); |
|---|
| 182 | 181 | |
|---|
| 182 | + imx_rngc_irq_unmask(rngc); |
|---|
| 183 | + |
|---|
| 183 | 184 | /* create seed, repeat while there is some statistical error */ |
|---|
| 184 | 185 | do { |
|---|
| 185 | | - imx_rngc_irq_unmask(rngc); |
|---|
| 186 | | - |
|---|
| 187 | 186 | /* seed creation */ |
|---|
| 188 | 187 | cmd = readl(rngc->base + RNGC_COMMAND); |
|---|
| 189 | 188 | writel(cmd | RNGC_CMD_SEED, rngc->base + RNGC_COMMAND); |
|---|
| 190 | 189 | |
|---|
| 191 | | - ret = wait_for_completion_timeout(&rngc->rng_op_done, |
|---|
| 192 | | - RNGC_TIMEOUT); |
|---|
| 193 | | - |
|---|
| 190 | + ret = wait_for_completion_timeout(&rngc->rng_op_done, msecs_to_jiffies(RNGC_TIMEOUT)); |
|---|
| 194 | 191 | if (!ret) { |
|---|
| 195 | | - imx_rngc_irq_mask_clear(rngc); |
|---|
| 196 | | - return -ETIMEDOUT; |
|---|
| 192 | + ret = -ETIMEDOUT; |
|---|
| 193 | + goto err; |
|---|
| 197 | 194 | } |
|---|
| 198 | 195 | |
|---|
| 199 | 196 | } while (rngc->err_reg == RNGC_ERROR_STATUS_STAT_ERR); |
|---|
| 200 | 197 | |
|---|
| 201 | | - return rngc->err_reg ? -EIO : 0; |
|---|
| 198 | + if (rngc->err_reg) { |
|---|
| 199 | + ret = -EIO; |
|---|
| 200 | + goto err; |
|---|
| 201 | + } |
|---|
| 202 | + |
|---|
| 203 | + /* |
|---|
| 204 | + * enable automatic seeding, the rngc creates a new seed automatically |
|---|
| 205 | + * after serving 2^20 random 160-bit words |
|---|
| 206 | + */ |
|---|
| 207 | + ctrl = readl(rngc->base + RNGC_CONTROL); |
|---|
| 208 | + ctrl |= RNGC_CTRL_AUTO_SEED; |
|---|
| 209 | + writel(ctrl, rngc->base + RNGC_CONTROL); |
|---|
| 210 | + |
|---|
| 211 | + /* |
|---|
| 212 | + * if initialisation was successful, we keep the interrupt |
|---|
| 213 | + * unmasked until imx_rngc_cleanup is called |
|---|
| 214 | + * we mask the interrupt ourselves if we return an error |
|---|
| 215 | + */ |
|---|
| 216 | + return 0; |
|---|
| 217 | + |
|---|
| 218 | +err: |
|---|
| 219 | + imx_rngc_irq_mask_clear(rngc); |
|---|
| 220 | + return ret; |
|---|
| 221 | +} |
|---|
| 222 | + |
|---|
| 223 | +static void imx_rngc_cleanup(struct hwrng *rng) |
|---|
| 224 | +{ |
|---|
| 225 | + struct imx_rngc *rngc = container_of(rng, struct imx_rngc, rng); |
|---|
| 226 | + |
|---|
| 227 | + imx_rngc_irq_mask_clear(rngc); |
|---|
| 202 | 228 | } |
|---|
| 203 | 229 | |
|---|
| 204 | 230 | static int imx_rngc_probe(struct platform_device *pdev) |
|---|
| 205 | 231 | { |
|---|
| 206 | 232 | struct imx_rngc *rngc; |
|---|
| 207 | | - struct resource *res; |
|---|
| 208 | 233 | int ret; |
|---|
| 209 | 234 | int irq; |
|---|
| 235 | + u32 ver_id; |
|---|
| 236 | + u8 rng_type; |
|---|
| 210 | 237 | |
|---|
| 211 | 238 | rngc = devm_kzalloc(&pdev->dev, sizeof(*rngc), GFP_KERNEL); |
|---|
| 212 | 239 | if (!rngc) |
|---|
| 213 | 240 | return -ENOMEM; |
|---|
| 214 | 241 | |
|---|
| 215 | | - res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
|---|
| 216 | | - rngc->base = devm_ioremap_resource(&pdev->dev, res); |
|---|
| 242 | + rngc->base = devm_platform_ioremap_resource(pdev, 0); |
|---|
| 217 | 243 | if (IS_ERR(rngc->base)) |
|---|
| 218 | 244 | return PTR_ERR(rngc->base); |
|---|
| 219 | 245 | |
|---|
| .. | .. |
|---|
| 233 | 259 | if (ret) |
|---|
| 234 | 260 | return ret; |
|---|
| 235 | 261 | |
|---|
| 236 | | - ret = devm_request_irq(&pdev->dev, |
|---|
| 237 | | - irq, imx_rngc_irq, 0, pdev->name, (void *)rngc); |
|---|
| 238 | | - if (ret) { |
|---|
| 239 | | - dev_err(rngc->dev, "Can't get interrupt working.\n"); |
|---|
| 262 | + ver_id = readl(rngc->base + RNGC_VER_ID); |
|---|
| 263 | + rng_type = ver_id >> RNGC_TYPE_SHIFT; |
|---|
| 264 | + /* |
|---|
| 265 | + * This driver supports only RNGC and RNGB. (There's a different |
|---|
| 266 | + * driver for RNGA.) |
|---|
| 267 | + */ |
|---|
| 268 | + if (rng_type != RNGC_TYPE_RNGC && rng_type != RNGC_TYPE_RNGB) { |
|---|
| 269 | + ret = -ENODEV; |
|---|
| 240 | 270 | goto err; |
|---|
| 241 | 271 | } |
|---|
| 242 | 272 | |
|---|
| .. | .. |
|---|
| 245 | 275 | rngc->rng.name = pdev->name; |
|---|
| 246 | 276 | rngc->rng.init = imx_rngc_init; |
|---|
| 247 | 277 | rngc->rng.read = imx_rngc_read; |
|---|
| 278 | + rngc->rng.cleanup = imx_rngc_cleanup; |
|---|
| 279 | + rngc->rng.quality = 19; |
|---|
| 248 | 280 | |
|---|
| 249 | 281 | rngc->dev = &pdev->dev; |
|---|
| 250 | 282 | platform_set_drvdata(pdev, rngc); |
|---|
| 251 | 283 | |
|---|
| 252 | 284 | imx_rngc_irq_mask_clear(rngc); |
|---|
| 253 | 285 | |
|---|
| 286 | + ret = devm_request_irq(&pdev->dev, |
|---|
| 287 | + irq, imx_rngc_irq, 0, pdev->name, (void *)rngc); |
|---|
| 288 | + if (ret) { |
|---|
| 289 | + dev_err(rngc->dev, "Can't get interrupt working.\n"); |
|---|
| 290 | + return ret; |
|---|
| 291 | + } |
|---|
| 292 | + |
|---|
| 254 | 293 | if (self_test) { |
|---|
| 255 | 294 | ret = imx_rngc_self_test(rngc); |
|---|
| 256 | 295 | if (ret) { |
|---|
| 257 | | - dev_err(rngc->dev, "FSL RNGC self test failed.\n"); |
|---|
| 296 | + dev_err(rngc->dev, "self test failed\n"); |
|---|
| 258 | 297 | goto err; |
|---|
| 259 | 298 | } |
|---|
| 260 | 299 | } |
|---|
| 261 | 300 | |
|---|
| 262 | 301 | ret = hwrng_register(&rngc->rng); |
|---|
| 263 | 302 | if (ret) { |
|---|
| 264 | | - dev_err(&pdev->dev, "FSL RNGC registering failed (%d)\n", ret); |
|---|
| 303 | + dev_err(&pdev->dev, "hwrng registration failed\n"); |
|---|
| 265 | 304 | goto err; |
|---|
| 266 | 305 | } |
|---|
| 267 | 306 | |
|---|
| 268 | | - dev_info(&pdev->dev, "Freescale RNGC registered.\n"); |
|---|
| 307 | + dev_info(&pdev->dev, |
|---|
| 308 | + "Freescale RNG%c registered (HW revision %d.%02d)\n", |
|---|
| 309 | + rng_type == RNGC_TYPE_RNGB ? 'B' : 'C', |
|---|
| 310 | + (ver_id >> RNGC_VER_MAJ_SHIFT) & 0xff, ver_id & 0xff); |
|---|
| 269 | 311 | return 0; |
|---|
| 270 | 312 | |
|---|
| 271 | 313 | err: |
|---|