hwrng: st - Use real-world device timings for timeout
[firefly-linux-kernel-4.4.55.git] / drivers / char / hw_random / st-rng.c
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 /*
36  * Samples are documented to be available every 0.667us, so in theory
37  * the 4 sample deep FIFO should take 2.668us to fill.  However, during
38  * thorough testing, it became apparent that filling the FIFO actually
39  * takes closer to 12us.  We then multiply by 2 in order to account for
40  * the lack of udelay()'s reliability, suggested by Russell King.
41  */
42 #define ST_RNG_FILL_FIFO_TIMEOUT        (12 * 2)
43
44 struct st_rng_data {
45         void __iomem    *base;
46         struct clk      *clk;
47         struct hwrng    ops;
48 };
49
50 static int st_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
51 {
52         struct st_rng_data *ddata = (struct st_rng_data *)rng->priv;
53         u32 status;
54         int i;
55
56         if (max < sizeof(u16))
57                 return -EINVAL;
58
59         /* Wait until FIFO is full - max 4uS*/
60         for (i = 0; i < ST_RNG_FILL_FIFO_TIMEOUT; i++) {
61                 status = readl_relaxed(ddata->base + ST_RNG_STATUS_REG);
62                 if (status & ST_RNG_STATUS_FIFO_FULL)
63                         break;
64                 udelay(1);
65         }
66
67         if (i == ST_RNG_FILL_FIFO_TIMEOUT)
68                 return 0;
69
70         for (i = 0; i < ST_RNG_FIFO_SIZE && i < max; i += 2)
71                 *(u16 *)(data + i) =
72                         readl_relaxed(ddata->base + ST_RNG_DATA_REG);
73
74         return i;       /* No of bytes read */
75 }
76
77 static int st_rng_probe(struct platform_device *pdev)
78 {
79         struct st_rng_data *ddata;
80         struct resource *res;
81         struct clk *clk;
82         void __iomem *base;
83         int ret;
84
85         ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
86         if (!ddata)
87                 return -ENOMEM;
88
89         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
90         base = devm_ioremap_resource(&pdev->dev, res);
91         if (IS_ERR(base))
92                 return PTR_ERR(base);
93
94         clk = devm_clk_get(&pdev->dev, NULL);
95         if (IS_ERR(clk))
96                 return PTR_ERR(clk);
97
98         ret = clk_prepare_enable(clk);
99         if (ret)
100                 return ret;
101
102         ddata->ops.priv = (unsigned long)ddata;
103         ddata->ops.read = st_rng_read;
104         ddata->ops.name = pdev->name;
105         ddata->base     = base;
106         ddata->clk      = clk;
107
108         dev_set_drvdata(&pdev->dev, ddata);
109
110         ret = hwrng_register(&ddata->ops);
111         if (ret) {
112                 dev_err(&pdev->dev, "Failed to register HW RNG\n");
113                 return ret;
114         }
115
116         dev_info(&pdev->dev, "Successfully registered HW RNG\n");
117
118         return 0;
119 }
120
121 static int st_rng_remove(struct platform_device *pdev)
122 {
123         struct st_rng_data *ddata = dev_get_drvdata(&pdev->dev);
124
125         hwrng_unregister(&ddata->ops);
126
127         clk_disable_unprepare(ddata->clk);
128
129         return 0;
130 }
131
132 static const struct of_device_id st_rng_match[] = {
133         { .compatible = "st,rng" },
134         {},
135 };
136 MODULE_DEVICE_TABLE(of, st_rng_match);
137
138 static struct platform_driver st_rng_driver = {
139         .driver = {
140                 .name = "st-hwrandom",
141                 .of_match_table = of_match_ptr(st_rng_match),
142         },
143         .probe = st_rng_probe,
144         .remove = st_rng_remove
145 };
146
147 module_platform_driver(st_rng_driver);
148
149 MODULE_AUTHOR("Pankaj Dev <pankaj.dev@st.com>");
150 MODULE_LICENSE("GPL v2");