blob: 3a607472687ddfdeabdc97464be1554733f1e37a [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>
Florian Fainelli791af4f2017-11-07 16:44:44 -080018#include <linux/clk.h>
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010019
20#define RNG_CTRL 0x0
21#define RNG_STATUS 0x4
22#define RNG_DATA 0x8
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -040023#define RNG_INT_MASK 0x10
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010024
25/* enable rng */
26#define RNG_RBGEN 0x1
27
28/* the initial numbers generated are "less random" so will be discarded */
29#define RNG_WARMUP_COUNT 0x40000
30
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -040031#define RNG_INT_OFF 0x1
32
Florian Fainellib7884792017-11-07 16:44:39 -080033struct bcm2835_rng_priv {
34 struct hwrng rng;
35 void __iomem *base;
Florian Fainelli04b154f2017-11-07 16:44:43 -080036 bool mask_interrupts;
Florian Fainelli791af4f2017-11-07 16:44:44 -080037 struct clk *clk;
Florian Fainellib7884792017-11-07 16:44:39 -080038};
39
Florian Fainellib7884792017-11-07 16:44:39 -080040static inline struct bcm2835_rng_priv *to_rng_priv(struct hwrng *rng)
41{
42 return container_of(rng, struct bcm2835_rng_priv, rng);
43}
44
Florian Fainelliabd42022017-11-07 16:44:45 -080045static inline u32 rng_readl(struct bcm2835_rng_priv *priv, u32 offset)
46{
47 return readl(priv->base + offset);
48}
49
50static inline void rng_writel(struct bcm2835_rng_priv *priv, u32 val,
51 u32 offset)
52{
53 writel(val, priv->base + offset);
54}
55
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010056static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
57 bool wait)
58{
Florian Fainellib7884792017-11-07 16:44:39 -080059 struct bcm2835_rng_priv *priv = to_rng_priv(rng);
Yendapally Reddy Dhananjaya Reddy4f8de652016-05-27 06:10:41 -040060 u32 max_words = max / sizeof(u32);
61 u32 num_words, count;
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010062
Florian Fainelliabd42022017-11-07 16:44:45 -080063 while ((rng_readl(priv, RNG_STATUS) >> 24) == 0) {
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010064 if (!wait)
65 return 0;
66 cpu_relax();
67 }
68
Florian Fainelliabd42022017-11-07 16:44:45 -080069 num_words = rng_readl(priv, RNG_STATUS) >> 24;
Yendapally Reddy Dhananjaya Reddy4f8de652016-05-27 06:10:41 -040070 if (num_words > max_words)
71 num_words = max_words;
72
73 for (count = 0; count < num_words; count++)
Florian Fainelliabd42022017-11-07 16:44:45 -080074 ((u32 *)buf)[count] = rng_readl(priv, RNG_DATA);
Yendapally Reddy Dhananjaya Reddy4f8de652016-05-27 06:10:41 -040075
76 return num_words * sizeof(u32);
Lubomir Rintel8c4196a2013-03-28 07:19:38 +010077}
78
Florian Fainellia8157772017-11-07 16:44:40 -080079static int bcm2835_rng_init(struct hwrng *rng)
80{
81 struct bcm2835_rng_priv *priv = to_rng_priv(rng);
Florian Fainelli791af4f2017-11-07 16:44:44 -080082 int ret = 0;
Florian Fainelli04b154f2017-11-07 16:44:43 -080083 u32 val;
84
Florian Fainelli791af4f2017-11-07 16:44:44 -080085 if (!IS_ERR(priv->clk)) {
86 ret = clk_prepare_enable(priv->clk);
87 if (ret)
88 return ret;
89 }
90
Florian Fainelli04b154f2017-11-07 16:44:43 -080091 if (priv->mask_interrupts) {
92 /* mask the interrupt */
Florian Fainelliabd42022017-11-07 16:44:45 -080093 val = rng_readl(priv, RNG_INT_MASK);
Florian Fainelli04b154f2017-11-07 16:44:43 -080094 val |= RNG_INT_OFF;
Florian Fainelliabd42022017-11-07 16:44:45 -080095 rng_writel(priv, val, RNG_INT_MASK);
Florian Fainelli04b154f2017-11-07 16:44:43 -080096 }
Florian Fainellia8157772017-11-07 16:44:40 -080097
98 /* set warm-up count & enable */
Florian Fainelliabd42022017-11-07 16:44:45 -080099 rng_writel(priv, RNG_WARMUP_COUNT, RNG_STATUS);
100 rng_writel(priv, RNG_RBGEN, RNG_CTRL);
Florian Fainellia8157772017-11-07 16:44:40 -0800101
Florian Fainelli791af4f2017-11-07 16:44:44 -0800102 return ret;
Florian Fainellia8157772017-11-07 16:44:40 -0800103}
104
Florian Fainelliec94bca2017-11-07 16:44:41 -0800105static void bcm2835_rng_cleanup(struct hwrng *rng)
106{
107 struct bcm2835_rng_priv *priv = to_rng_priv(rng);
108
109 /* disable rng hardware */
Florian Fainelliabd42022017-11-07 16:44:45 -0800110 rng_writel(priv, 0, RNG_CTRL);
Florian Fainelli791af4f2017-11-07 16:44:44 -0800111
112 if (!IS_ERR(priv->clk))
113 clk_disable_unprepare(priv->clk);
Florian Fainelliec94bca2017-11-07 16:44:41 -0800114}
115
Florian Fainelli04b154f2017-11-07 16:44:43 -0800116struct bcm2835_rng_of_data {
117 bool mask_interrupts;
118};
119
120static const struct bcm2835_rng_of_data nsp_rng_of_data = {
121 .mask_interrupts = true,
122};
123
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400124static const struct of_device_id bcm2835_rng_of_match[] = {
125 { .compatible = "brcm,bcm2835-rng"},
Florian Fainelli04b154f2017-11-07 16:44:43 -0800126 { .compatible = "brcm,bcm-nsp-rng", .data = &nsp_rng_of_data },
127 { .compatible = "brcm,bcm5301x-rng", .data = &nsp_rng_of_data },
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400128 {},
129};
130
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100131static int bcm2835_rng_probe(struct platform_device *pdev)
132{
Florian Fainelli04b154f2017-11-07 16:44:43 -0800133 const struct bcm2835_rng_of_data *of_data;
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100134 struct device *dev = &pdev->dev;
135 struct device_node *np = dev->of_node;
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400136 const struct of_device_id *rng_id;
Florian Fainellib7884792017-11-07 16:44:39 -0800137 struct bcm2835_rng_priv *priv;
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800138 struct resource *r;
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100139 int err;
140
Florian Fainellib7884792017-11-07 16:44:39 -0800141 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
142 if (!priv)
143 return -ENOMEM;
144
145 platform_set_drvdata(pdev, priv);
146
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800147 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
148
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100149 /* map peripheral */
Florian Fainellib7884792017-11-07 16:44:39 -0800150 priv->base = devm_ioremap_resource(dev, r);
151 if (IS_ERR(priv->base)) {
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100152 dev_err(dev, "failed to remap rng regs");
Florian Fainellib7884792017-11-07 16:44:39 -0800153 return PTR_ERR(priv->base);
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100154 }
Florian Fainellib7884792017-11-07 16:44:39 -0800155
Florian Fainelli791af4f2017-11-07 16:44:44 -0800156 /* Clock is optional on most platforms */
157 priv->clk = devm_clk_get(dev, NULL);
158
Florian Fainellib7884792017-11-07 16:44:39 -0800159 priv->rng.name = "bcm2835-rng";
Florian Fainellia8157772017-11-07 16:44:40 -0800160 priv->rng.init = bcm2835_rng_init;
Florian Fainellib7884792017-11-07 16:44:39 -0800161 priv->rng.read = bcm2835_rng_read;
Florian Fainelliec94bca2017-11-07 16:44:41 -0800162 priv->rng.cleanup = bcm2835_rng_cleanup;
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100163
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400164 rng_id = of_match_node(bcm2835_rng_of_match, np);
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800165 if (!rng_id)
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400166 return -EINVAL;
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800167
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400168 /* Check for rng init function, execute it */
Florian Fainelli04b154f2017-11-07 16:44:43 -0800169 of_data = rng_id->data;
170 if (of_data)
171 priv->mask_interrupts = of_data->mask_interrupts;
Yendapally Reddy Dhananjaya Reddy422a7492016-05-27 06:10:39 -0400172
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100173 /* register driver */
Florian Fainelli16a4c042017-11-07 16:44:42 -0800174 err = devm_hwrng_register(dev, &priv->rng);
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800175 if (err)
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100176 dev_err(dev, "hwrng registration failed\n");
Florian Fainelli21bb0ef2017-11-07 16:44:38 -0800177 else
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100178 dev_info(dev, "hwrng registered\n");
179
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100180 return err;
181}
182
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100183MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
184
185static struct platform_driver bcm2835_rng_driver = {
186 .driver = {
187 .name = "bcm2835-rng",
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100188 .of_match_table = bcm2835_rng_of_match,
189 },
190 .probe = bcm2835_rng_probe,
Lubomir Rintel8c4196a2013-03-28 07:19:38 +0100191};
192module_platform_driver(bcm2835_rng_driver);
193
194MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
195MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
Arnd Bergmann22e80992013-04-23 15:39:50 +0200196MODULE_LICENSE("GPL v2");