hwrng: bcm2835 - Read as much data as available
[deliverable/linux.git] / drivers / char / hw_random / bcm2835-rng.c
CommitLineData
8c4196a2
LR
1/**
2 * Copyright (c) 2010-2012 Broadcom. All rights reserved.
3 * Copyright (c) 2013 Lubomir Rintel
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License ("GPL")
7 * version 2, as published by the Free Software Foundation.
8 */
9
10#include <linux/hw_random.h>
8c4196a2
LR
11#include <linux/io.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/of_address.h>
15#include <linux/of_platform.h>
16#include <linux/platform_device.h>
17#include <linux/printk.h>
18
19#define RNG_CTRL 0x0
20#define RNG_STATUS 0x4
21#define RNG_DATA 0x8
422a7491 22#define RNG_INT_MASK 0x10
8c4196a2
LR
23
24/* enable rng */
25#define RNG_RBGEN 0x1
26
27/* the initial numbers generated are "less random" so will be discarded */
28#define RNG_WARMUP_COUNT 0x40000
29
422a7491
YRDR
30#define RNG_INT_OFF 0x1
31
32static void __init nsp_rng_init(void __iomem *base)
33{
34 u32 val;
35
36 /* mask the interrupt */
37 val = readl(base + RNG_INT_MASK);
38 val |= RNG_INT_OFF;
39 writel(val, base + RNG_INT_MASK);
40}
41
8c4196a2
LR
42static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
43 bool wait)
44{
45 void __iomem *rng_base = (void __iomem *)rng->priv;
4f8de65b
YRDR
46 u32 max_words = max / sizeof(u32);
47 u32 num_words, count;
8c4196a2
LR
48
49 while ((__raw_readl(rng_base + RNG_STATUS) >> 24) == 0) {
50 if (!wait)
51 return 0;
52 cpu_relax();
53 }
54
4f8de65b
YRDR
55 num_words = readl(rng_base + RNG_STATUS) >> 24;
56 if (num_words > max_words)
57 num_words = max_words;
58
59 for (count = 0; count < num_words; count++)
60 ((u32 *)buf)[count] = readl(rng_base + RNG_DATA);
61
62 return num_words * sizeof(u32);
8c4196a2
LR
63}
64
65static struct hwrng bcm2835_rng_ops = {
66 .name = "bcm2835",
67 .read = bcm2835_rng_read,
68};
69
422a7491
YRDR
70static const struct of_device_id bcm2835_rng_of_match[] = {
71 { .compatible = "brcm,bcm2835-rng"},
72 { .compatible = "brcm,bcm-nsp-rng", .data = nsp_rng_init},
73 {},
74};
75
8c4196a2
LR
76static int bcm2835_rng_probe(struct platform_device *pdev)
77{
78 struct device *dev = &pdev->dev;
79 struct device_node *np = dev->of_node;
422a7491
YRDR
80 void (*rng_setup)(void __iomem *base);
81 const struct of_device_id *rng_id;
8c4196a2
LR
82 void __iomem *rng_base;
83 int err;
84
85 /* map peripheral */
86 rng_base = of_iomap(np, 0);
87 if (!rng_base) {
88 dev_err(dev, "failed to remap rng regs");
89 return -ENODEV;
90 }
91 bcm2835_rng_ops.priv = (unsigned long)rng_base;
92
422a7491
YRDR
93 rng_id = of_match_node(bcm2835_rng_of_match, np);
94 if (!rng_id)
95 return -EINVAL;
96
97 /* Check for rng init function, execute it */
98 rng_setup = rng_id->data;
99 if (rng_setup)
100 rng_setup(rng_base);
101
eb4a5346
MP
102 /* set warm-up count & enable */
103 __raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
104 __raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
105
8c4196a2
LR
106 /* register driver */
107 err = hwrng_register(&bcm2835_rng_ops);
108 if (err) {
109 dev_err(dev, "hwrng registration failed\n");
110 iounmap(rng_base);
eb4a5346 111 } else
8c4196a2
LR
112 dev_info(dev, "hwrng registered\n");
113
8c4196a2
LR
114 return err;
115}
116
117static int bcm2835_rng_remove(struct platform_device *pdev)
118{
119 void __iomem *rng_base = (void __iomem *)bcm2835_rng_ops.priv;
120
121 /* disable rng hardware */
122 __raw_writel(0, rng_base + RNG_CTRL);
123
124 /* unregister driver */
125 hwrng_unregister(&bcm2835_rng_ops);
126 iounmap(rng_base);
127
128 return 0;
129}
130
8c4196a2
LR
131MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
132
133static struct platform_driver bcm2835_rng_driver = {
134 .driver = {
135 .name = "bcm2835-rng",
8c4196a2
LR
136 .of_match_table = bcm2835_rng_of_match,
137 },
138 .probe = bcm2835_rng_probe,
139 .remove = bcm2835_rng_remove,
140};
141module_platform_driver(bcm2835_rng_driver);
142
143MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
144MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
22e8099f 145MODULE_LICENSE("GPL v2");
This page took 0.17327 seconds and 5 git commands to generate.