blob: 4d0356110b1bef8de40035229719a7704cddb260 [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
Florian Fainellib7884792017-11-07 16:44:39 -080032struct bcm2835_rng_priv {
33 struct hwrng rng;
34 void __iomem *base;
35};
36
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -040037static void __init nsp_rng_init(void __iomem *base)
38{
39 u32 val;
40
41 /* mask the interrupt */
42 val = readl(base + RNG_INT_MASK);
43 val |= RNG_INT_OFF;
44 writel(val, base + RNG_INT_MASK);
45}
46
Florian Fainellib7884792017-11-07 16:44:39 -080047static inline struct bcm2835_rng_priv *to_rng_priv(struct hwrng *rng)
48{
49 return container_of(rng, struct bcm2835_rng_priv, rng);
50}
51
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010052static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
53 bool wait)
54{
Florian Fainellib7884792017-11-07 16:44:39 -080055 struct bcm2835_rng_priv *priv = to_rng_priv(rng);
Yendapally Reddy Dhananjaya Reddy4f8de652016-05-27 06:10:41 -040056 u32 max_words = max / sizeof(u32);
57 u32 num_words, count;
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010058
Florian Fainellib7884792017-11-07 16:44:39 -080059 while ((__raw_readl(priv->base + RNG_STATUS) >> 24) == 0) {
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010060 if (!wait)
61 return 0;
62 cpu_relax();
63 }
64
Florian Fainellib7884792017-11-07 16:44:39 -080065 num_words = readl(priv->base + RNG_STATUS) >> 24;
Yendapally Reddy Dhananjaya Reddy4f8de652016-05-27 06:10:41 -040066 if (num_words > max_words)
67 num_words = max_words;
68
69 for (count = 0; count < num_words; count++)
Florian Fainellib7884792017-11-07 16:44:39 -080070 ((u32 *)buf)[count] = readl(priv->base + RNG_DATA);
Yendapally Reddy Dhananjaya Reddy4f8de652016-05-27 06:10:41 -040071
72 return num_words * sizeof(u32);
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010073}
74
Florian Fainellia8157772017-11-07 16:44:40 -080075static int bcm2835_rng_init(struct hwrng *rng)
76{
77 struct bcm2835_rng_priv *priv = to_rng_priv(rng);
78
79 /* set warm-up count & enable */
80 __raw_writel(RNG_WARMUP_COUNT, priv->base + RNG_STATUS);
81 __raw_writel(RNG_RBGEN, priv->base + RNG_CTRL);
82
83 return 0;
84}
85
Florian Fainelliec94bca2017-11-07 16:44:41 -080086static void bcm2835_rng_cleanup(struct hwrng *rng)
87{
88 struct bcm2835_rng_priv *priv = to_rng_priv(rng);
89
90 /* disable rng hardware */
91 __raw_writel(0, priv->base + RNG_CTRL);
92}
93
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -040094static const struct of_device_id bcm2835_rng_of_match[] = {
95 { .compatible = "brcm,bcm2835-rng"},
96 { .compatible = "brcm,bcm-nsp-rng", .data = nsp_rng_init},
Florian Fainelli52140992016-06-22 17:27:02 -070097 { .compatible = "brcm,bcm5301x-rng", .data = nsp_rng_init},
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -040098 {},
99};
100
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100101static int bcm2835_rng_probe(struct platform_device *pdev)
102{
103 struct device *dev = &pdev->dev;
104 struct device_node *np = dev->of_node;
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400105 void (*rng_setup)(void __iomem *base);
106 const struct of_device_id *rng_id;
Florian Fainellib7884792017-11-07 16:44:39 -0800107 struct bcm2835_rng_priv *priv;
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800108 struct resource *r;
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100109 int err;
110
Florian Fainellib7884792017-11-07 16:44:39 -0800111 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
112 if (!priv)
113 return -ENOMEM;
114
115 platform_set_drvdata(pdev, priv);
116
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800117 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
118
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100119 /* map peripheral */
Florian Fainellib7884792017-11-07 16:44:39 -0800120 priv->base = devm_ioremap_resource(dev, r);
121 if (IS_ERR(priv->base)) {
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100122 dev_err(dev, "failed to remap rng regs");
Florian Fainellib7884792017-11-07 16:44:39 -0800123 return PTR_ERR(priv->base);
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100124 }
Florian Fainellib7884792017-11-07 16:44:39 -0800125
126 priv->rng.name = "bcm2835-rng";
Florian Fainellia8157772017-11-07 16:44:40 -0800127 priv->rng.init = bcm2835_rng_init;
Florian Fainellib7884792017-11-07 16:44:39 -0800128 priv->rng.read = bcm2835_rng_read;
Florian Fainelliec94bca2017-11-07 16:44:41 -0800129 priv->rng.cleanup = bcm2835_rng_cleanup;
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100130
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400131 rng_id = of_match_node(bcm2835_rng_of_match, np);
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800132 if (!rng_id)
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400133 return -EINVAL;
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800134
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400135 /* Check for rng init function, execute it */
136 rng_setup = rng_id->data;
137 if (rng_setup)
Florian Fainellib7884792017-11-07 16:44:39 -0800138 rng_setup(priv->base);
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400139
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100140 /* register driver */
Florian Fainellib7884792017-11-07 16:44:39 -0800141 err = hwrng_register(&priv->rng);
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800142 if (err)
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100143 dev_err(dev, "hwrng registration failed\n");
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800144 else
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100145 dev_info(dev, "hwrng registered\n");
146
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100147 return err;
148}
149
150static int bcm2835_rng_remove(struct platform_device *pdev)
151{
Florian Fainellib7884792017-11-07 16:44:39 -0800152 struct bcm2835_rng_priv *priv = platform_get_drvdata(pdev);
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100153
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100154 /* unregister driver */
Florian Fainellib7884792017-11-07 16:44:39 -0800155 hwrng_unregister(&priv->rng);
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100156
157 return 0;
158}
159
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100160MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
161
162static struct platform_driver bcm2835_rng_driver = {
163 .driver = {
164 .name = "bcm2835-rng",
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100165 .of_match_table = bcm2835_rng_of_match,
166 },
167 .probe = bcm2835_rng_probe,
168 .remove = bcm2835_rng_remove,
169};
170module_platform_driver(bcm2835_rng_driver);
171
172MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
173MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
Arnd Bergmann22e80992013-04-23 15:39:50 +0200174MODULE_LICENSE("GPL v2");