hwrng: st - Add support for ST's HW Random Number Generator
[linux-2.6-block.git] / drivers / char / hw_random / st-rng.c
CommitLineData
4a4da53c
LJ
1/*
2 * ST Random Number Generator Driver ST's Platforms
3 *
4 * Author: Pankaj Dev: <pankaj.dev@st.com>
5 * Lee Jones <lee.jones@linaro.org>
6 *
7 * Copyright (C) 2015 STMicroelectronics (R&D) Limited
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/hw_random.h>
17#include <linux/io.h>
18#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22
23/* Registers */
24#define ST_RNG_STATUS_REG 0x20
25#define ST_RNG_DATA_REG 0x24
26
27/* Registers fields */
28#define ST_RNG_STATUS_BAD_SEQUENCE BIT(0)
29#define ST_RNG_STATUS_BAD_ALTERNANCE BIT(1)
30#define ST_RNG_STATUS_FIFO_FULL BIT(5)
31
32#define ST_RNG_FIFO_SIZE 8
33#define ST_RNG_SAMPLE_SIZE 2 /* 2 Byte (16bit) samples */
34
35/* Samples are available every 0.667us, which we round to 1us */
36#define ST_RNG_FILL_FIFO_TIMEOUT (1 * (ST_RNG_FIFO_SIZE / ST_RNG_SAMPLE_SIZE))
37
38struct st_rng_data {
39 void __iomem *base;
40 struct clk *clk;
41 struct hwrng ops;
42};
43
44static int st_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
45{
46 struct st_rng_data *ddata = (struct st_rng_data *)rng->priv;
47 u32 status;
48 int i;
49
50 if (max < sizeof(u16))
51 return -EINVAL;
52
53 /* Wait until FIFO is full - max 4uS*/
54 for (i = 0; i < ST_RNG_FILL_FIFO_TIMEOUT; i++) {
55 status = readl_relaxed(ddata->base + ST_RNG_STATUS_REG);
56 if (status & ST_RNG_STATUS_FIFO_FULL)
57 break;
58 udelay(1);
59 }
60
61 if (i == ST_RNG_FILL_FIFO_TIMEOUT)
62 return 0;
63
64 for (i = 0; i < ST_RNG_FIFO_SIZE && i < max; i += 2)
65 *(u16 *)(data + i) =
66 readl_relaxed(ddata->base + ST_RNG_DATA_REG);
67
68 return i; /* No of bytes read */
69}
70
71static int st_rng_probe(struct platform_device *pdev)
72{
73 struct st_rng_data *ddata;
74 struct resource *res;
75 struct clk *clk;
76 void __iomem *base;
77 int ret;
78
79 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
80 if (!ddata)
81 return -ENOMEM;
82
83 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
84 base = devm_ioremap_resource(&pdev->dev, res);
85 if (IS_ERR(base))
86 return PTR_ERR(base);
87
88 clk = devm_clk_get(&pdev->dev, NULL);
89 if (IS_ERR(clk))
90 return PTR_ERR(clk);
91
92 ret = clk_prepare_enable(clk);
93 if (ret)
94 return ret;
95
96 ddata->ops.priv = (unsigned long)ddata;
97 ddata->ops.read = st_rng_read;
98 ddata->ops.name = pdev->name;
99 ddata->base = base;
100 ddata->clk = clk;
101
102 dev_set_drvdata(&pdev->dev, ddata);
103
104 ret = hwrng_register(&ddata->ops);
105 if (ret) {
106 dev_err(&pdev->dev, "Failed to register HW RNG\n");
107 return ret;
108 }
109
110 dev_info(&pdev->dev, "Successfully registered HW RNG\n");
111
112 return 0;
113}
114
115static int st_rng_remove(struct platform_device *pdev)
116{
117 struct st_rng_data *ddata = dev_get_drvdata(&pdev->dev);
118
119 hwrng_unregister(&ddata->ops);
120
121 clk_disable_unprepare(ddata->clk);
122
123 return 0;
124}
125
126static const struct of_device_id st_rng_match[] = {
127 { .compatible = "st,rng" },
128 {},
129};
130MODULE_DEVICE_TABLE(of, st_rng_match);
131
132static struct platform_driver st_rng_driver = {
133 .driver = {
134 .name = "st-hwrandom",
135 .of_match_table = of_match_ptr(st_rng_match),
136 },
137 .probe = st_rng_probe,
138 .remove = st_rng_remove
139};
140
141module_platform_driver(st_rng_driver);
142
143MODULE_AUTHOR("Pankaj Dev <pankaj.dev@st.com>");
144MODULE_LICENSE("GPL v2");