|
| 1 | +/** |
| 2 | + * Copyright (c) 2010-2012 Broadcom. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * 1. Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions, and the following disclaimer, |
| 9 | + * without modification. |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer in the |
| 12 | + * documentation and/or other materials provided with the distribution. |
| 13 | + * 3. The names of the above-listed copyright holders may not be used |
| 14 | + * to endorse or promote products derived from this software without |
| 15 | + * specific prior written permission. |
| 16 | + * |
| 17 | + * ALTERNATIVELY, this software may be distributed under the terms of the |
| 18 | + * GNU General Public License ("GPL") version 2, as published by the Free |
| 19 | + * Software Foundation. |
| 20 | + * |
| 21 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 22 | + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 23 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 24 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 25 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 26 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 27 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | + */ |
| 33 | + |
| 34 | +#include <linux/kernel.h> |
| 35 | +#include <linux/module.h> |
| 36 | +#include <linux/init.h> |
| 37 | +#include <linux/hw_random.h> |
| 38 | +#include <linux/printk.h> |
| 39 | + |
| 40 | +#include <asm/io.h> |
| 41 | +#include <mach/hardware.h> |
| 42 | +#include <mach/platform.h> |
| 43 | + |
| 44 | +#define RNG_CTRL (0x0) |
| 45 | +#define RNG_STATUS (0x4) |
| 46 | +#define RNG_DATA (0x8) |
| 47 | +#define RNG_FF_THRESHOLD (0xc) |
| 48 | + |
| 49 | +/* enable rng */ |
| 50 | +#define RNG_RBGEN 0x1 |
| 51 | +/* double speed, less random mode */ |
| 52 | +#define RNG_RBG2X 0x2 |
| 53 | + |
| 54 | +/* the initial numbers generated are "less random" so will be discarded */ |
| 55 | +#define RNG_WARMUP_COUNT 0x40000 |
| 56 | + |
| 57 | +static int bcm2708_rng_data_read(struct hwrng *rng, u32 *buffer) |
| 58 | +{ |
| 59 | + void __iomem *rng_base = (void __iomem *)rng->priv; |
| 60 | + unsigned words; |
| 61 | + /* wait for a random number to be in fifo */ |
| 62 | + do { |
| 63 | + words = __raw_readl(rng_base + RNG_STATUS)>>24; |
| 64 | + } |
| 65 | + while (words == 0); |
| 66 | + /* read the random number */ |
| 67 | + *buffer = __raw_readl(rng_base + RNG_DATA); |
| 68 | + return 4; |
| 69 | +} |
| 70 | + |
| 71 | +static struct hwrng bcm2708_rng_ops = { |
| 72 | + .name = "bcm2708", |
| 73 | + .data_read = bcm2708_rng_data_read, |
| 74 | +}; |
| 75 | + |
| 76 | +static int __init bcm2708_rng_init(void) |
| 77 | +{ |
| 78 | + void __iomem *rng_base; |
| 79 | + int err; |
| 80 | + |
| 81 | + /* map peripheral */ |
| 82 | + rng_base = ioremap(RNG_BASE, 0x10); |
| 83 | + pr_info("bcm2708_rng_init=%p\n", rng_base); |
| 84 | + if (!rng_base) { |
| 85 | + pr_err("bcm2708_rng_init failed to ioremap\n"); |
| 86 | + return -ENOMEM; |
| 87 | + } |
| 88 | + bcm2708_rng_ops.priv = (unsigned long)rng_base; |
| 89 | + /* register driver */ |
| 90 | + err = hwrng_register(&bcm2708_rng_ops); |
| 91 | + if (err) { |
| 92 | + pr_err("bcm2708_rng_init hwrng_register()=%d\n", err); |
| 93 | + iounmap(rng_base); |
| 94 | + } else { |
| 95 | + /* set warm-up count & enable */ |
| 96 | + __raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS); |
| 97 | + __raw_writel(RNG_RBGEN, rng_base + RNG_CTRL); |
| 98 | + } |
| 99 | + return err; |
| 100 | +} |
| 101 | + |
| 102 | +static void __exit bcm2708_rng_exit(void) |
| 103 | +{ |
| 104 | + void __iomem *rng_base = (void __iomem *)bcm2708_rng_ops.priv; |
| 105 | + pr_info("bcm2708_rng_exit\n"); |
| 106 | + /* disable rng hardware */ |
| 107 | + __raw_writel(0, rng_base + RNG_CTRL); |
| 108 | + /* unregister driver */ |
| 109 | + hwrng_unregister(&bcm2708_rng_ops); |
| 110 | + iounmap(rng_base); |
| 111 | +} |
| 112 | + |
| 113 | +module_init(bcm2708_rng_init); |
| 114 | +module_exit(bcm2708_rng_exit); |
| 115 | + |
| 116 | +MODULE_DESCRIPTION("BCM2708 H/W Random Number Generator (RNG) driver"); |
| 117 | +MODULE_LICENSE("GPL and additional rights"); |
0 commit comments