]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/char/hw_random/st-rng.c
Merge tag 'block-5.6-2020-02-05' of git://git.kernel.dk/linux-block
[linux.git] / drivers / char / hw_random / st-rng.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ST Random Number Generator Driver ST's Platforms
4  *
5  * Author: Pankaj Dev: <pankaj.dev@st.com>
6  *         Lee Jones <lee.jones@linaro.org>
7  *
8  * Copyright (C) 2015 STMicroelectronics (R&D) Limited
9  */
10
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/hw_random.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19
20 /* Registers */
21 #define ST_RNG_STATUS_REG               0x20
22 #define ST_RNG_DATA_REG                 0x24
23
24 /* Registers fields */
25 #define ST_RNG_STATUS_BAD_SEQUENCE      BIT(0)
26 #define ST_RNG_STATUS_BAD_ALTERNANCE    BIT(1)
27 #define ST_RNG_STATUS_FIFO_FULL         BIT(5)
28
29 #define ST_RNG_SAMPLE_SIZE              2 /* 2 Byte (16bit) samples */
30 #define ST_RNG_FIFO_DEPTH               4
31 #define ST_RNG_FIFO_SIZE                (ST_RNG_FIFO_DEPTH * ST_RNG_SAMPLE_SIZE)
32
33 /*
34  * Samples are documented to be available every 0.667us, so in theory
35  * the 4 sample deep FIFO should take 2.668us to fill.  However, during
36  * thorough testing, it became apparent that filling the FIFO actually
37  * takes closer to 12us.  We then multiply by 2 in order to account for
38  * the lack of udelay()'s reliability, suggested by Russell King.
39  */
40 #define ST_RNG_FILL_FIFO_TIMEOUT        (12 * 2)
41
42 struct st_rng_data {
43         void __iomem    *base;
44         struct clk      *clk;
45         struct hwrng    ops;
46 };
47
48 static int st_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
49 {
50         struct st_rng_data *ddata = (struct st_rng_data *)rng->priv;
51         u32 status;
52         int i;
53
54         /* Wait until FIFO is full - max 4uS*/
55         for (i = 0; i < ST_RNG_FILL_FIFO_TIMEOUT; i++) {
56                 status = readl_relaxed(ddata->base + ST_RNG_STATUS_REG);
57                 if (status & ST_RNG_STATUS_FIFO_FULL)
58                         break;
59                 udelay(1);
60         }
61
62         if (i == ST_RNG_FILL_FIFO_TIMEOUT)
63                 return 0;
64
65         for (i = 0; i < ST_RNG_FIFO_SIZE && i < max; i += 2)
66                 *(u16 *)(data + i) =
67                         readl_relaxed(ddata->base + ST_RNG_DATA_REG);
68
69         return i;       /* No of bytes read */
70 }
71
72 static int st_rng_probe(struct platform_device *pdev)
73 {
74         struct st_rng_data *ddata;
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         base = devm_platform_ioremap_resource(pdev, 0);
84         if (IS_ERR(base))
85                 return PTR_ERR(base);
86
87         clk = devm_clk_get(&pdev->dev, NULL);
88         if (IS_ERR(clk))
89                 return PTR_ERR(clk);
90
91         ret = clk_prepare_enable(clk);
92         if (ret)
93                 return ret;
94
95         ddata->ops.priv = (unsigned long)ddata;
96         ddata->ops.read = st_rng_read;
97         ddata->ops.name = pdev->name;
98         ddata->base     = base;
99         ddata->clk      = clk;
100
101         dev_set_drvdata(&pdev->dev, ddata);
102
103         ret = devm_hwrng_register(&pdev->dev, &ddata->ops);
104         if (ret) {
105                 dev_err(&pdev->dev, "Failed to register HW RNG\n");
106                 clk_disable_unprepare(clk);
107                 return ret;
108         }
109
110         dev_info(&pdev->dev, "Successfully registered HW RNG\n");
111
112         return 0;
113 }
114
115 static int st_rng_remove(struct platform_device *pdev)
116 {
117         struct st_rng_data *ddata = dev_get_drvdata(&pdev->dev);
118
119         clk_disable_unprepare(ddata->clk);
120
121         return 0;
122 }
123
124 static const struct of_device_id st_rng_match[] = {
125         { .compatible = "st,rng" },
126         {},
127 };
128 MODULE_DEVICE_TABLE(of, st_rng_match);
129
130 static struct platform_driver st_rng_driver = {
131         .driver = {
132                 .name = "st-hwrandom",
133                 .of_match_table = of_match_ptr(st_rng_match),
134         },
135         .probe = st_rng_probe,
136         .remove = st_rng_remove
137 };
138
139 module_platform_driver(st_rng_driver);
140
141 MODULE_AUTHOR("Pankaj Dev <pankaj.dev@st.com>");
142 MODULE_LICENSE("GPL v2");