Merge tag 'driver-core-4.6-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / drivers / char / hw_random / bcm63xx-rng.c
CommitLineData
553072b2
FF
1/*
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>
47cd3060 15#include <linux/of.h>
553072b2 16
b515e0f9
FF
17#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
553072b2
FF
26
27struct bcm63xx_rng_priv {
6229c160 28 struct hwrng rng;
553072b2
FF
29 struct clk *clk;
30 void __iomem *regs;
31};
32
6229c160 33#define to_rng_priv(rng) container_of(rng, struct bcm63xx_rng_priv, rng)
553072b2
FF
34
35static int bcm63xx_rng_init(struct hwrng *rng)
36{
37 struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
38 u32 val;
6229c160
DT
39 int error;
40
41 error = clk_prepare_enable(priv->clk);
42 if (error)
43 return error;
553072b2 44
f7591fae 45 val = __raw_readl(priv->regs + RNG_CTRL);
553072b2 46 val |= RNG_EN;
f7591fae 47 __raw_writel(val, priv->regs + RNG_CTRL);
553072b2
FF
48
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
f7591fae 57 val = __raw_readl(priv->regs + RNG_CTRL);
553072b2 58 val &= ~RNG_EN;
f7591fae 59 __raw_writel(val, priv->regs + RNG_CTRL);
6229c160 60
f440c4ee 61 clk_disable_unprepare(priv->clk);
553072b2
FF
62}
63
64static int bcm63xx_rng_data_present(struct hwrng *rng, int wait)
65{
66 struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
67
f7591fae 68 return __raw_readl(priv->regs + RNG_STAT) & RNG_AVAIL_MASK;
553072b2
FF
69}
70
71static int bcm63xx_rng_data_read(struct hwrng *rng, u32 *data)
72{
73 struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
74
f7591fae 75 *data = __raw_readl(priv->regs + RNG_DATA);
553072b2
FF
76
77 return 4;
78}
79
bcd2982a 80static int bcm63xx_rng_probe(struct platform_device *pdev)
553072b2
FF
81{
82 struct resource *r;
553072b2
FF
83 int ret;
84 struct bcm63xx_rng_priv *priv;
553072b2
FF
85
86 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
87 if (!r) {
88 dev_err(&pdev->dev, "no iomem resource\n");
6229c160 89 return -ENXIO;
553072b2
FF
90 }
91
0052a654 92 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
6229c160
DT
93 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;
f440c4ee 99 priv->rng.data_present = bcm63xx_rng_data_present;
6229c160
DT
100 priv->rng.data_read = bcm63xx_rng_data_read;
101
102 priv->clk = devm_clk_get(&pdev->dev, "ipsec");
103 if (IS_ERR(priv->clk)) {
f440c4ee
ÁFR
104 ret = PTR_ERR(priv->clk);
105 dev_err(&pdev->dev, "no clock for device: %d\n", ret);
106 return ret;
553072b2
FF
107 }
108
553072b2
FF
109 if (!devm_request_mem_region(&pdev->dev, r->start,
110 resource_size(r), pdev->name)) {
111 dev_err(&pdev->dev, "request mem failed");
6229c160 112 return -EBUSY;
553072b2
FF
113 }
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");
6229c160 119 return -ENOMEM;
553072b2
FF
120 }
121
f440c4ee
ÁFR
122 ret = devm_hwrng_register(&pdev->dev, &priv->rng);
123 if (ret) {
6229c160 124 dev_err(&pdev->dev, "failed to register rng device: %d\n",
f440c4ee
ÁFR
125 ret);
126 return ret;
553072b2
FF
127 }
128
129 dev_info(&pdev->dev, "registered RNG driver\n");
130
553072b2
FF
131 return 0;
132}
133
1879f40a 134#ifdef CONFIG_OF
7b651706
ÁFR
135static const struct of_device_id bcm63xx_rng_of_match[] = {
136 { .compatible = "brcm,bcm6368-rng", },
137 {},
138};
139MODULE_DEVICE_TABLE(of, bcm63xx_rng_of_match);
1879f40a 140#endif
7b651706 141
553072b2
FF
142static struct platform_driver bcm63xx_rng_driver = {
143 .probe = bcm63xx_rng_probe,
553072b2
FF
144 .driver = {
145 .name = "bcm63xx-rng",
1879f40a 146 .of_match_table = of_match_ptr(bcm63xx_rng_of_match),
553072b2
FF
147 },
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");
This page took 0.196566 seconds and 5 git commands to generate.