blob: b1e8b7847e9ac0b29651186e031dd185ee6f2549 [file] [log] [blame]
Lubomir Rintel8c4196a2013-03-28 07:19:38 +01001/**
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>
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010011#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
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -040022#define RNG_INT_MASK 0x10
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010023
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
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -040030#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
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010042static 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;
46
47 while ((__raw_readl(rng_base + RNG_STATUS) >> 24) == 0) {
48 if (!wait)
49 return 0;
50 cpu_relax();
51 }
52
53 *(u32 *)buf = __raw_readl(rng_base + RNG_DATA);
54 return sizeof(u32);
55}
56
57static struct hwrng bcm2835_rng_ops = {
58 .name = "bcm2835",
59 .read = bcm2835_rng_read,
60};
61
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -040062static const struct of_device_id bcm2835_rng_of_match[] = {
63 { .compatible = "brcm,bcm2835-rng"},
64 { .compatible = "brcm,bcm-nsp-rng", .data = nsp_rng_init},
65 {},
66};
67
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010068static int bcm2835_rng_probe(struct platform_device *pdev)
69{
70 struct device *dev = &pdev->dev;
71 struct device_node *np = dev->of_node;
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -040072 void (*rng_setup)(void __iomem *base);
73 const struct of_device_id *rng_id;
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010074 void __iomem *rng_base;
75 int err;
76
77 /* map peripheral */
78 rng_base = of_iomap(np, 0);
79 if (!rng_base) {
80 dev_err(dev, "failed to remap rng regs");
81 return -ENODEV;
82 }
83 bcm2835_rng_ops.priv = (unsigned long)rng_base;
84
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -040085 rng_id = of_match_node(bcm2835_rng_of_match, np);
86 if (!rng_id)
87 return -EINVAL;
88
89 /* Check for rng init function, execute it */
90 rng_setup = rng_id->data;
91 if (rng_setup)
92 rng_setup(rng_base);
93
Matt Portereb4a5342014-04-10 15:22:10 -040094 /* set warm-up count & enable */
95 __raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
96 __raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
97
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010098 /* register driver */
99 err = hwrng_register(&bcm2835_rng_ops);
100 if (err) {
101 dev_err(dev, "hwrng registration failed\n");
102 iounmap(rng_base);
Matt Portereb4a5342014-04-10 15:22:10 -0400103 } else
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100104 dev_info(dev, "hwrng registered\n");
105
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100106 return err;
107}
108
109static int bcm2835_rng_remove(struct platform_device *pdev)
110{
111 void __iomem *rng_base = (void __iomem *)bcm2835_rng_ops.priv;
112
113 /* disable rng hardware */
114 __raw_writel(0, rng_base + RNG_CTRL);
115
116 /* unregister driver */
117 hwrng_unregister(&bcm2835_rng_ops);
118 iounmap(rng_base);
119
120 return 0;
121}
122
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100123MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
124
125static struct platform_driver bcm2835_rng_driver = {
126 .driver = {
127 .name = "bcm2835-rng",
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100128 .of_match_table = bcm2835_rng_of_match,
129 },
130 .probe = bcm2835_rng_probe,
131 .remove = bcm2835_rng_remove,
132};
133module_platform_driver(bcm2835_rng_driver);
134
135MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
136MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
Arnd Bergmann22e80992013-04-23 15:39:50 +0200137MODULE_LICENSE("GPL v2");