blob: 5132c9cde50dd9ed0d635419725961d28d42ccdf [file] [log] [blame]
Florian Fainelli553072b2012-07-24 16:33:11 +02001/*
2 * Broadcom BCM63xx Random Number Generator support
3 *
4 * Copyright (C) 2011, Florian Fainelli <florian@openwrt.org>
5 * Copyright (C) 2009, Broadcom Corporation
6 *
7 */
8#include <linux/module.h>
9#include <linux/slab.h>
10#include <linux/io.h>
11#include <linux/err.h>
12#include <linux/clk.h>
13#include <linux/platform_device.h>
14#include <linux/hw_random.h>
Álvaro Fernández Rojas47cd30602016-04-03 14:18:22 +020015#include <linux/of.h>
Florian Fainelli553072b2012-07-24 16:33:11 +020016
Florian Fainellib515e0f2015-02-16 18:09:14 -080017#define RNG_CTRL 0x00
18#define RNG_EN (1 << 0)
19
20#define RNG_STAT 0x04
21#define RNG_AVAIL_MASK (0xff000000)
22
23#define RNG_DATA 0x08
24#define RNG_THRES 0x0c
25#define RNG_MASK 0x10
Florian Fainelli553072b2012-07-24 16:33:11 +020026
27struct bcm63xx_rng_priv {
Dmitry Torokhov6229c162015-03-12 14:00:03 -070028 struct hwrng rng;
Florian Fainelli553072b2012-07-24 16:33:11 +020029 struct clk *clk;
30 void __iomem *regs;
31};
32
Dmitry Torokhov6229c162015-03-12 14:00:03 -070033#define to_rng_priv(rng) container_of(rng, struct bcm63xx_rng_priv, rng)
Florian Fainelli553072b2012-07-24 16:33:11 +020034
35static int bcm63xx_rng_init(struct hwrng *rng)
36{
37 struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
38 u32 val;
Dmitry Torokhov6229c162015-03-12 14:00:03 -070039 int error;
40
41 error = clk_prepare_enable(priv->clk);
42 if (error)
43 return error;
Florian Fainelli553072b2012-07-24 16:33:11 +020044
Florian Fainellif7591fa2015-02-16 18:09:13 -080045 val = __raw_readl(priv->regs + RNG_CTRL);
Florian Fainelli553072b2012-07-24 16:33:11 +020046 val |= RNG_EN;
Florian Fainellif7591fa2015-02-16 18:09:13 -080047 __raw_writel(val, priv->regs + RNG_CTRL);
Florian Fainelli553072b2012-07-24 16:33:11 +020048
49 return 0;
50}
51
52static void bcm63xx_rng_cleanup(struct hwrng *rng)
53{
54 struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
55 u32 val;
56
Florian Fainellif7591fa2015-02-16 18:09:13 -080057 val = __raw_readl(priv->regs + RNG_CTRL);
Florian Fainelli553072b2012-07-24 16:33:11 +020058 val &= ~RNG_EN;
Florian Fainellif7591fa2015-02-16 18:09:13 -080059 __raw_writel(val, priv->regs + RNG_CTRL);
Dmitry Torokhov6229c162015-03-12 14:00:03 -070060
Álvaro Fernández Rojasf440c4e2015-05-02 12:08:42 +020061 clk_disable_unprepare(priv->clk);
Florian Fainelli553072b2012-07-24 16:33:11 +020062}
63
64static int bcm63xx_rng_data_present(struct hwrng *rng, int wait)
65{
66 struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
67
Florian Fainellif7591fa2015-02-16 18:09:13 -080068 return __raw_readl(priv->regs + RNG_STAT) & RNG_AVAIL_MASK;
Florian Fainelli553072b2012-07-24 16:33:11 +020069}
70
71static int bcm63xx_rng_data_read(struct hwrng *rng, u32 *data)
72{
73 struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
74
Florian Fainellif7591fa2015-02-16 18:09:13 -080075 *data = __raw_readl(priv->regs + RNG_DATA);
Florian Fainelli553072b2012-07-24 16:33:11 +020076
77 return 4;
78}
79
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -080080static int bcm63xx_rng_probe(struct platform_device *pdev)
Florian Fainelli553072b2012-07-24 16:33:11 +020081{
82 struct resource *r;
Florian Fainelli553072b2012-07-24 16:33:11 +020083 int ret;
84 struct bcm63xx_rng_priv *priv;
Florian Fainelli553072b2012-07-24 16:33:11 +020085
86 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
87 if (!r) {
88 dev_err(&pdev->dev, "no iomem resource\n");
Dmitry Torokhov6229c162015-03-12 14:00:03 -070089 return -ENXIO;
Florian Fainelli553072b2012-07-24 16:33:11 +020090 }
91
Florian Fainelli0052a652015-02-16 18:09:16 -080092 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Dmitry Torokhov6229c162015-03-12 14:00:03 -070093 if (!priv)
94 return -ENOMEM;
95
96 priv->rng.name = pdev->name;
97 priv->rng.init = bcm63xx_rng_init;
98 priv->rng.cleanup = bcm63xx_rng_cleanup;
Álvaro Fernández Rojasf440c4e2015-05-02 12:08:42 +020099 priv->rng.data_present = bcm63xx_rng_data_present;
Dmitry Torokhov6229c162015-03-12 14:00:03 -0700100 priv->rng.data_read = bcm63xx_rng_data_read;
101
102 priv->clk = devm_clk_get(&pdev->dev, "ipsec");
103 if (IS_ERR(priv->clk)) {
Álvaro Fernández Rojasf440c4e2015-05-02 12:08:42 +0200104 ret = PTR_ERR(priv->clk);
105 dev_err(&pdev->dev, "no clock for device: %d\n", ret);
106 return ret;
Florian Fainelli553072b2012-07-24 16:33:11 +0200107 }
108
Florian Fainelli553072b2012-07-24 16:33:11 +0200109 if (!devm_request_mem_region(&pdev->dev, r->start,
110 resource_size(r), pdev->name)) {
111 dev_err(&pdev->dev, "request mem failed");
Dmitry Torokhov6229c162015-03-12 14:00:03 -0700112 return -EBUSY;
Florian Fainelli553072b2012-07-24 16:33:11 +0200113 }
114
115 priv->regs = devm_ioremap_nocache(&pdev->dev, r->start,
116 resource_size(r));
117 if (!priv->regs) {
118 dev_err(&pdev->dev, "ioremap failed");
Dmitry Torokhov6229c162015-03-12 14:00:03 -0700119 return -ENOMEM;
Florian Fainelli553072b2012-07-24 16:33:11 +0200120 }
121
Álvaro Fernández Rojasf440c4e2015-05-02 12:08:42 +0200122 ret = devm_hwrng_register(&pdev->dev, &priv->rng);
123 if (ret) {
Dmitry Torokhov6229c162015-03-12 14:00:03 -0700124 dev_err(&pdev->dev, "failed to register rng device: %d\n",
Álvaro Fernández Rojasf440c4e2015-05-02 12:08:42 +0200125 ret);
126 return ret;
Florian Fainelli553072b2012-07-24 16:33:11 +0200127 }
128
129 dev_info(&pdev->dev, "registered RNG driver\n");
130
131 return 0;
Florian Fainelli553072b2012-07-24 16:33:11 +0200132}
133
Álvaro Fernández Rojas1879f402016-02-21 10:53:35 +0100134#ifdef CONFIG_OF
Álvaro Fernández Rojas7b651702016-01-17 10:03:55 +0100135static const struct of_device_id bcm63xx_rng_of_match[] = {
136 { .compatible = "brcm,bcm6368-rng", },
137 {},
138};
139MODULE_DEVICE_TABLE(of, bcm63xx_rng_of_match);
Álvaro Fernández Rojas1879f402016-02-21 10:53:35 +0100140#endif
Álvaro Fernández Rojas7b651702016-01-17 10:03:55 +0100141
Florian Fainelli553072b2012-07-24 16:33:11 +0200142static struct platform_driver bcm63xx_rng_driver = {
143 .probe = bcm63xx_rng_probe,
Florian Fainelli553072b2012-07-24 16:33:11 +0200144 .driver = {
145 .name = "bcm63xx-rng",
Álvaro Fernández Rojas1879f402016-02-21 10:53:35 +0100146 .of_match_table = of_match_ptr(bcm63xx_rng_of_match),
Florian Fainelli553072b2012-07-24 16:33:11 +0200147 },
148};
149
150module_platform_driver(bcm63xx_rng_driver);
151
152MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
153MODULE_DESCRIPTION("Broadcom BCM63xx RNG driver");
154MODULE_LICENSE("GPL");