blob: ed20e0b6b7ae8709fe642f2c0543f5c04464a582 [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;
Florian Fainelli04b154f2017-11-07 16:44:43 -080035 bool mask_interrupts;
Florian Fainellib7884792017-11-07 16:44:39 -080036};
37
Florian Fainellib7884792017-11-07 16:44:39 -080038static inline struct bcm2835_rng_priv *to_rng_priv(struct hwrng *rng)
39{
40 return container_of(rng, struct bcm2835_rng_priv, rng);
41}
42
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010043static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
44 bool wait)
45{
Florian Fainellib7884792017-11-07 16:44:39 -080046 struct bcm2835_rng_priv *priv = to_rng_priv(rng);
Yendapally Reddy Dhananjaya Reddy4f8de652016-05-27 06:10:41 -040047 u32 max_words = max / sizeof(u32);
48 u32 num_words, count;
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010049
Florian Fainellib7884792017-11-07 16:44:39 -080050 while ((__raw_readl(priv->base + RNG_STATUS) >> 24) == 0) {
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010051 if (!wait)
52 return 0;
53 cpu_relax();
54 }
55
Florian Fainellib7884792017-11-07 16:44:39 -080056 num_words = readl(priv->base + RNG_STATUS) >> 24;
Yendapally Reddy Dhananjaya Reddy4f8de652016-05-27 06:10:41 -040057 if (num_words > max_words)
58 num_words = max_words;
59
60 for (count = 0; count < num_words; count++)
Florian Fainellib7884792017-11-07 16:44:39 -080061 ((u32 *)buf)[count] = readl(priv->base + RNG_DATA);
Yendapally Reddy Dhananjaya Reddy4f8de652016-05-27 06:10:41 -040062
63 return num_words * sizeof(u32);
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010064}
65
Florian Fainellia8157772017-11-07 16:44:40 -080066static int bcm2835_rng_init(struct hwrng *rng)
67{
68 struct bcm2835_rng_priv *priv = to_rng_priv(rng);
Florian Fainelli04b154f2017-11-07 16:44:43 -080069 u32 val;
70
71 if (priv->mask_interrupts) {
72 /* mask the interrupt */
73 val = readl(priv->base + RNG_INT_MASK);
74 val |= RNG_INT_OFF;
75 writel(val, priv->base + RNG_INT_MASK);
76 }
Florian Fainellia8157772017-11-07 16:44:40 -080077
78 /* set warm-up count & enable */
79 __raw_writel(RNG_WARMUP_COUNT, priv->base + RNG_STATUS);
80 __raw_writel(RNG_RBGEN, priv->base + RNG_CTRL);
81
82 return 0;
83}
84
Florian Fainelliec94bca2017-11-07 16:44:41 -080085static void bcm2835_rng_cleanup(struct hwrng *rng)
86{
87 struct bcm2835_rng_priv *priv = to_rng_priv(rng);
88
89 /* disable rng hardware */
90 __raw_writel(0, priv->base + RNG_CTRL);
91}
92
Florian Fainelli04b154f2017-11-07 16:44:43 -080093struct bcm2835_rng_of_data {
94 bool mask_interrupts;
95};
96
97static const struct bcm2835_rng_of_data nsp_rng_of_data = {
98 .mask_interrupts = true,
99};
100
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400101static const struct of_device_id bcm2835_rng_of_match[] = {
102 { .compatible = "brcm,bcm2835-rng"},
Florian Fainelli04b154f2017-11-07 16:44:43 -0800103 { .compatible = "brcm,bcm-nsp-rng", .data = &nsp_rng_of_data },
104 { .compatible = "brcm,bcm5301x-rng", .data = &nsp_rng_of_data },
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400105 {},
106};
107
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100108static int bcm2835_rng_probe(struct platform_device *pdev)
109{
Florian Fainelli04b154f2017-11-07 16:44:43 -0800110 const struct bcm2835_rng_of_data *of_data;
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100111 struct device *dev = &pdev->dev;
112 struct device_node *np = dev->of_node;
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400113 const struct of_device_id *rng_id;
Florian Fainellib7884792017-11-07 16:44:39 -0800114 struct bcm2835_rng_priv *priv;
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800115 struct resource *r;
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100116 int err;
117
Florian Fainellib7884792017-11-07 16:44:39 -0800118 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
119 if (!priv)
120 return -ENOMEM;
121
122 platform_set_drvdata(pdev, priv);
123
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800124 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
125
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100126 /* map peripheral */
Florian Fainellib7884792017-11-07 16:44:39 -0800127 priv->base = devm_ioremap_resource(dev, r);
128 if (IS_ERR(priv->base)) {
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100129 dev_err(dev, "failed to remap rng regs");
Florian Fainellib7884792017-11-07 16:44:39 -0800130 return PTR_ERR(priv->base);
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100131 }
Florian Fainellib7884792017-11-07 16:44:39 -0800132
133 priv->rng.name = "bcm2835-rng";
Florian Fainellia8157772017-11-07 16:44:40 -0800134 priv->rng.init = bcm2835_rng_init;
Florian Fainellib7884792017-11-07 16:44:39 -0800135 priv->rng.read = bcm2835_rng_read;
Florian Fainelliec94bca2017-11-07 16:44:41 -0800136 priv->rng.cleanup = bcm2835_rng_cleanup;
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100137
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400138 rng_id = of_match_node(bcm2835_rng_of_match, np);
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800139 if (!rng_id)
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400140 return -EINVAL;
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800141
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400142 /* Check for rng init function, execute it */
Florian Fainelli04b154f2017-11-07 16:44:43 -0800143 of_data = rng_id->data;
144 if (of_data)
145 priv->mask_interrupts = of_data->mask_interrupts;
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400146
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100147 /* register driver */
Florian Fainelli16a4c042017-11-07 16:44:42 -0800148 err = devm_hwrng_register(dev, &priv->rng);
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800149 if (err)
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100150 dev_err(dev, "hwrng registration failed\n");
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800151 else
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100152 dev_info(dev, "hwrng registered\n");
153
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100154 return err;
155}
156
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100157MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
158
159static struct platform_driver bcm2835_rng_driver = {
160 .driver = {
161 .name = "bcm2835-rng",
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100162 .of_match_table = bcm2835_rng_of_match,
163 },
164 .probe = bcm2835_rng_probe,
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100165};
166module_platform_driver(bcm2835_rng_driver);
167
168MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
169MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
Arnd Bergmann22e80992013-04-23 15:39:50 +0200170MODULE_LICENSE("GPL v2");