1 From 21a474b37f905387d8908384a24c13d4f8513113 Mon Sep 17 00:00:00 2001
2 From: Florian Fainelli <florian@openwrt.org>
3 Date: Wed, 25 Jan 2012 17:40:02 +0100
4 Subject: [PATCH 18/63] hw_random: add Broadcom BCM63xx RNG driver
6 Signed-off-by: Florian Fainelli <florian@openwrt.org>
8 drivers/char/hw_random/Kconfig | 14 +++
9 drivers/char/hw_random/Makefile | 1 +
10 drivers/char/hw_random/bcm63xx-rng.c | 175 ++++++++++++++++++++++++++++++++++
11 3 files changed, 190 insertions(+), 0 deletions(-)
12 create mode 100644 drivers/char/hw_random/bcm63xx-rng.c
14 --- a/drivers/char/hw_random/Kconfig
15 +++ b/drivers/char/hw_random/Kconfig
16 @@ -73,6 +73,20 @@ config HW_RANDOM_ATMEL
20 +config HW_RANDOM_BCM63XX
21 + tristate "Broadcom BCM63xx Random Number Generator support"
22 + depends on HW_RANDOM && BCM63XX
25 + This driver provides kernel-side support for the Random Number
26 + Generator hardware found on the Broadcom BCM63xx SoCs.
28 + To compile this driver as a module, choose M here: the
29 + module will be called bcm63xx-rng
34 config HW_RANDOM_GEODE
35 tristate "AMD Geode HW Random Number Generator support"
36 depends on HW_RANDOM && X86_32 && PCI
37 --- a/drivers/char/hw_random/Makefile
38 +++ b/drivers/char/hw_random/Makefile
39 @@ -8,6 +8,7 @@ obj-$(CONFIG_HW_RANDOM_TIMERIOMEM) += ti
40 obj-$(CONFIG_HW_RANDOM_INTEL) += intel-rng.o
41 obj-$(CONFIG_HW_RANDOM_AMD) += amd-rng.o
42 obj-$(CONFIG_HW_RANDOM_ATMEL) += atmel-rng.o
43 +obj-$(CONFIG_HW_RANDOM_BCM63XX) += bcm63xx-rng.o
44 obj-$(CONFIG_HW_RANDOM_GEODE) += geode-rng.o
45 obj-$(CONFIG_HW_RANDOM_N2RNG) += n2-rng.o
46 n2-rng-y := n2-drv.o n2-asm.o
48 +++ b/drivers/char/hw_random/bcm63xx-rng.c
51 + * Broadcom BCM63xx Random Number Generator support
53 + * Copyright (C) 2011, Florian Fainelli <florian@openwrt.org>
54 + * Copyright (C) 2009, Broadcom Corporation
57 +#include <linux/module.h>
58 +#include <linux/slab.h>
59 +#include <linux/io.h>
60 +#include <linux/err.h>
61 +#include <linux/clk.h>
62 +#include <linux/platform_device.h>
63 +#include <linux/hw_random.h>
65 +#include <bcm63xx_io.h>
66 +#include <bcm63xx_regs.h>
68 +struct bcm63xx_trng_priv {
73 +#define to_trng_priv(rng) ((struct bcm63xx_trng_priv *)rng->priv)
75 +static int bcm63xx_trng_init(struct hwrng *rng)
77 + struct bcm63xx_trng_priv *priv = to_trng_priv(rng);
80 + val = bcm_readl(priv->regs + TRNG_CTRL);
82 + bcm_writel(val, priv->regs + TRNG_CTRL);
87 +static void bcm63xx_trng_cleanup(struct hwrng *rng)
89 + struct bcm63xx_trng_priv *priv = to_trng_priv(rng);
92 + val = bcm_readl(priv->regs + TRNG_CTRL);
94 + bcm_writel(val, priv->regs + TRNG_CTRL);
97 +static int bcm63xx_trng_data_present(struct hwrng *rng, int wait)
99 + struct bcm63xx_trng_priv *priv = to_trng_priv(rng);
101 + return bcm_readl(priv->regs + TRNG_STAT) & TRNG_AVAIL_MASK;
104 +static int bcm63xx_trng_data_read(struct hwrng *rng, u32 *data)
106 + struct bcm63xx_trng_priv *priv = to_trng_priv(rng);
108 + *data = bcm_readl(priv->regs + TRNG_DATA);
113 +static int __init bcm63xx_trng_probe(struct platform_device *pdev)
115 + struct resource *r;
118 + struct bcm63xx_trng_priv *priv;
121 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
123 + dev_err(&pdev->dev, "no iomem resource\n");
128 + priv = kzalloc(sizeof(*priv), GFP_KERNEL);
130 + dev_err(&pdev->dev, "no memory for private structure\n");
135 + rng = kzalloc(sizeof(*rng), GFP_KERNEL);
137 + dev_err(&pdev->dev, "no memory for rng structure\n");
139 + goto out_free_priv;
142 + platform_set_drvdata(pdev, rng);
143 + rng->priv = (unsigned long)priv;
144 + rng->name = pdev->name;
145 + rng->init = bcm63xx_trng_init;
146 + rng->cleanup = bcm63xx_trng_cleanup;
147 + rng->data_present = bcm63xx_trng_data_present;
148 + rng->data_read = bcm63xx_trng_data_read;
150 + clk = clk_get(&pdev->dev, "ipsec");
152 + dev_err(&pdev->dev, "no clock for device\n");
153 + ret = PTR_ERR(clk);
159 + if (!devm_request_mem_region(&pdev->dev, r->start,
160 + resource_size(r), pdev->name)) {
161 + dev_err(&pdev->dev, "request mem failed");
166 + priv->regs = devm_ioremap_nocache(&pdev->dev, r->start,
169 + dev_err(&pdev->dev, "ioremap failed");
176 + ret = hwrng_register(rng);
178 + dev_err(&pdev->dev, "failed to register rng device\n");
179 + goto out_clk_disable;
182 + dev_info(&pdev->dev, "registered RNG driver\n");
189 + platform_set_drvdata(pdev, NULL);
197 +static int __devexit bcm63xx_trng_remove(struct platform_device *pdev)
199 + struct hwrng *rng = platform_get_drvdata(pdev);
200 + struct bcm63xx_trng_priv *priv = to_trng_priv(rng);
202 + hwrng_unregister(rng);
203 + clk_disable(priv->clk);
206 + platform_set_drvdata(pdev, NULL);
211 +static struct platform_driver bcm63xx_trng_driver = {
212 + .probe = bcm63xx_trng_probe,
213 + .remove = __devexit_p(bcm63xx_trng_remove),
215 + .name = "bcm63xx-trng",
216 + .owner = THIS_MODULE,
220 +module_platform_driver(bcm63xx_trng_driver);
222 +MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
223 +MODULE_DESCRIPTION("Broadcom BCM63xx RNG driver");
224 +MODULE_LICENSE("GPL");