hwrng: bcm63xx - drop bcm_{readl,writel} macros
[linux-2.6-microblaze.git] / drivers / char / hw_random / bcm63xx-rng.c
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>
15
16 #include <bcm63xx_regs.h>
17
18 struct bcm63xx_rng_priv {
19         struct clk *clk;
20         void __iomem *regs;
21 };
22
23 #define to_rng_priv(rng)        ((struct bcm63xx_rng_priv *)rng->priv)
24
25 static int bcm63xx_rng_init(struct hwrng *rng)
26 {
27         struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
28         u32 val;
29
30         val = __raw_readl(priv->regs + RNG_CTRL);
31         val |= RNG_EN;
32         __raw_writel(val, priv->regs + RNG_CTRL);
33
34         return 0;
35 }
36
37 static void bcm63xx_rng_cleanup(struct hwrng *rng)
38 {
39         struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
40         u32 val;
41
42         val = __raw_readl(priv->regs + RNG_CTRL);
43         val &= ~RNG_EN;
44         __raw_writel(val, priv->regs + RNG_CTRL);
45 }
46
47 static int bcm63xx_rng_data_present(struct hwrng *rng, int wait)
48 {
49         struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
50
51         return __raw_readl(priv->regs + RNG_STAT) & RNG_AVAIL_MASK;
52 }
53
54 static int bcm63xx_rng_data_read(struct hwrng *rng, u32 *data)
55 {
56         struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
57
58         *data = __raw_readl(priv->regs + RNG_DATA);
59
60         return 4;
61 }
62
63 static int bcm63xx_rng_probe(struct platform_device *pdev)
64 {
65         struct resource *r;
66         struct clk *clk;
67         int ret;
68         struct bcm63xx_rng_priv *priv;
69         struct hwrng *rng;
70
71         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
72         if (!r) {
73                 dev_err(&pdev->dev, "no iomem resource\n");
74                 ret = -ENXIO;
75                 goto out;
76         }
77
78         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
79         if (!priv) {
80                 dev_err(&pdev->dev, "no memory for private structure\n");
81                 ret = -ENOMEM;
82                 goto out;
83         }
84
85         rng = kzalloc(sizeof(*rng), GFP_KERNEL);
86         if (!rng) {
87                 dev_err(&pdev->dev, "no memory for rng structure\n");
88                 ret = -ENOMEM;
89                 goto out_free_priv;
90         }
91
92         platform_set_drvdata(pdev, rng);
93         rng->priv = (unsigned long)priv;
94         rng->name = pdev->name;
95         rng->init = bcm63xx_rng_init;
96         rng->cleanup = bcm63xx_rng_cleanup;
97         rng->data_present = bcm63xx_rng_data_present;
98         rng->data_read = bcm63xx_rng_data_read;
99
100         clk = clk_get(&pdev->dev, "ipsec");
101         if (IS_ERR(clk)) {
102                 dev_err(&pdev->dev, "no clock for device\n");
103                 ret = PTR_ERR(clk);
104                 goto out_free_rng;
105         }
106
107         priv->clk = clk;
108
109         if (!devm_request_mem_region(&pdev->dev, r->start,
110                                         resource_size(r), pdev->name)) {
111                 dev_err(&pdev->dev, "request mem failed");
112                 ret = -ENOMEM;
113                 goto out_free_rng;
114         }
115
116         priv->regs = devm_ioremap_nocache(&pdev->dev, r->start,
117                                         resource_size(r));
118         if (!priv->regs) {
119                 dev_err(&pdev->dev, "ioremap failed");
120                 ret = -ENOMEM;
121                 goto out_free_rng;
122         }
123
124         clk_enable(clk);
125
126         ret = hwrng_register(rng);
127         if (ret) {
128                 dev_err(&pdev->dev, "failed to register rng device\n");
129                 goto out_clk_disable;
130         }
131
132         dev_info(&pdev->dev, "registered RNG driver\n");
133
134         return 0;
135
136 out_clk_disable:
137         clk_disable(clk);
138 out_free_rng:
139         kfree(rng);
140 out_free_priv:
141         kfree(priv);
142 out:
143         return ret;
144 }
145
146 static int bcm63xx_rng_remove(struct platform_device *pdev)
147 {
148         struct hwrng *rng = platform_get_drvdata(pdev);
149         struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
150
151         hwrng_unregister(rng);
152         clk_disable(priv->clk);
153         kfree(priv);
154         kfree(rng);
155
156         return 0;
157 }
158
159 static struct platform_driver bcm63xx_rng_driver = {
160         .probe          = bcm63xx_rng_probe,
161         .remove         = bcm63xx_rng_remove,
162         .driver         = {
163                 .name   = "bcm63xx-rng",
164         },
165 };
166
167 module_platform_driver(bcm63xx_rng_driver);
168
169 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
170 MODULE_DESCRIPTION("Broadcom BCM63xx RNG driver");
171 MODULE_LICENSE("GPL");