1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * RNG driver for Freescale RNGC
5 * Copyright (C) 2008-2012 Freescale Semiconductor, Inc.
6 * Copyright (C) 2017 Martin Kaiser <martin@kaiser.cx>
9 #include <linux/module.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/clk.h>
14 #include <linux/err.h>
15 #include <linux/platform_device.h>
16 #include <linux/interrupt.h>
17 #include <linux/hw_random.h>
18 #include <linux/completion.h>
21 #define RNGC_VER_ID 0x0000
22 #define RNGC_COMMAND 0x0004
23 #define RNGC_CONTROL 0x0008
24 #define RNGC_STATUS 0x000C
25 #define RNGC_ERROR 0x0010
26 #define RNGC_FIFO 0x0014
28 /* the fields in the ver id register */
29 #define RNGC_TYPE_SHIFT 28
30 #define RNGC_VER_MAJ_SHIFT 8
32 /* the rng_type field */
33 #define RNGC_TYPE_RNGB 0x1
34 #define RNGC_TYPE_RNGC 0x2
37 #define RNGC_CMD_CLR_ERR 0x00000020
38 #define RNGC_CMD_CLR_INT 0x00000010
39 #define RNGC_CMD_SEED 0x00000002
40 #define RNGC_CMD_SELF_TEST 0x00000001
42 #define RNGC_CTRL_MASK_ERROR 0x00000040
43 #define RNGC_CTRL_MASK_DONE 0x00000020
44 #define RNGC_CTRL_AUTO_SEED 0x00000010
46 #define RNGC_STATUS_ERROR 0x00010000
47 #define RNGC_STATUS_FIFO_LEVEL_MASK 0x00000f00
48 #define RNGC_STATUS_FIFO_LEVEL_SHIFT 8
49 #define RNGC_STATUS_SEED_DONE 0x00000020
50 #define RNGC_STATUS_ST_DONE 0x00000010
52 #define RNGC_ERROR_STATUS_STAT_ERR 0x00000008
54 #define RNGC_TIMEOUT 3000 /* 3 sec */
57 static bool self_test = true;
58 module_param(self_test, bool, 0);
65 struct completion rng_op_done;
67 * err_reg is written only by the irq handler and read only
68 * when interrupts are masked, we need no spinlock
74 static inline void imx_rngc_irq_mask_clear(struct imx_rngc *rngc)
79 ctrl = readl(rngc->base + RNGC_CONTROL);
80 ctrl |= RNGC_CTRL_MASK_DONE | RNGC_CTRL_MASK_ERROR;
81 writel(ctrl, rngc->base + RNGC_CONTROL);
84 * CLR_INT clears the interrupt only if there's no error
85 * CLR_ERR clear the interrupt and the error register if there
88 cmd = readl(rngc->base + RNGC_COMMAND);
89 cmd |= RNGC_CMD_CLR_INT | RNGC_CMD_CLR_ERR;
90 writel(cmd, rngc->base + RNGC_COMMAND);
93 static inline void imx_rngc_irq_unmask(struct imx_rngc *rngc)
97 ctrl = readl(rngc->base + RNGC_CONTROL);
98 ctrl &= ~(RNGC_CTRL_MASK_DONE | RNGC_CTRL_MASK_ERROR);
99 writel(ctrl, rngc->base + RNGC_CONTROL);
102 static int imx_rngc_self_test(struct imx_rngc *rngc)
107 imx_rngc_irq_unmask(rngc);
110 cmd = readl(rngc->base + RNGC_COMMAND);
111 writel(cmd | RNGC_CMD_SELF_TEST, rngc->base + RNGC_COMMAND);
113 ret = wait_for_completion_timeout(&rngc->rng_op_done, msecs_to_jiffies(RNGC_TIMEOUT));
114 imx_rngc_irq_mask_clear(rngc);
118 return rngc->err_reg ? -EIO : 0;
121 static int imx_rngc_read(struct hwrng *rng, void *data, size_t max, bool wait)
123 struct imx_rngc *rngc = container_of(rng, struct imx_rngc, rng);
128 while (max >= sizeof(u32)) {
129 status = readl(rngc->base + RNGC_STATUS);
131 /* is there some error while reading this random number? */
132 if (status & RNGC_STATUS_ERROR)
135 /* how many random numbers are in FIFO? [0-16] */
136 level = (status & RNGC_STATUS_FIFO_LEVEL_MASK) >>
137 RNGC_STATUS_FIFO_LEVEL_SHIFT;
140 /* retrieve a random number from FIFO */
141 *(u32 *)data = readl(rngc->base + RNGC_FIFO);
143 retval += sizeof(u32);
149 return retval ? retval : -EIO;
152 static irqreturn_t imx_rngc_irq(int irq, void *priv)
154 struct imx_rngc *rngc = (struct imx_rngc *)priv;
158 * clearing the interrupt will also clear the error register
159 * read error and status before clearing
161 status = readl(rngc->base + RNGC_STATUS);
162 rngc->err_reg = readl(rngc->base + RNGC_ERROR);
164 imx_rngc_irq_mask_clear(rngc);
166 if (status & (RNGC_STATUS_SEED_DONE | RNGC_STATUS_ST_DONE))
167 complete(&rngc->rng_op_done);
172 static int imx_rngc_init(struct hwrng *rng)
174 struct imx_rngc *rngc = container_of(rng, struct imx_rngc, rng);
179 cmd = readl(rngc->base + RNGC_COMMAND);
180 writel(cmd | RNGC_CMD_CLR_ERR, rngc->base + RNGC_COMMAND);
182 imx_rngc_irq_unmask(rngc);
184 /* create seed, repeat while there is some statistical error */
187 cmd = readl(rngc->base + RNGC_COMMAND);
188 writel(cmd | RNGC_CMD_SEED, rngc->base + RNGC_COMMAND);
190 ret = wait_for_completion_timeout(&rngc->rng_op_done, msecs_to_jiffies(RNGC_TIMEOUT));
196 } while (rngc->err_reg == RNGC_ERROR_STATUS_STAT_ERR);
204 * enable automatic seeding, the rngc creates a new seed automatically
205 * after serving 2^20 random 160-bit words
207 ctrl = readl(rngc->base + RNGC_CONTROL);
208 ctrl |= RNGC_CTRL_AUTO_SEED;
209 writel(ctrl, rngc->base + RNGC_CONTROL);
212 * if initialisation was successful, we keep the interrupt
213 * unmasked until imx_rngc_cleanup is called
214 * we mask the interrupt ourselves if we return an error
219 imx_rngc_irq_mask_clear(rngc);
223 static void imx_rngc_cleanup(struct hwrng *rng)
225 struct imx_rngc *rngc = container_of(rng, struct imx_rngc, rng);
227 imx_rngc_irq_mask_clear(rngc);
230 static int imx_rngc_probe(struct platform_device *pdev)
232 struct imx_rngc *rngc;
238 rngc = devm_kzalloc(&pdev->dev, sizeof(*rngc), GFP_KERNEL);
242 rngc->base = devm_platform_ioremap_resource(pdev, 0);
243 if (IS_ERR(rngc->base))
244 return PTR_ERR(rngc->base);
246 rngc->clk = devm_clk_get(&pdev->dev, NULL);
247 if (IS_ERR(rngc->clk)) {
248 dev_err(&pdev->dev, "Can not get rng_clk\n");
249 return PTR_ERR(rngc->clk);
252 irq = platform_get_irq(pdev, 0);
254 dev_err(&pdev->dev, "Couldn't get irq %d\n", irq);
258 ret = clk_prepare_enable(rngc->clk);
262 ver_id = readl(rngc->base + RNGC_VER_ID);
263 rng_type = ver_id >> RNGC_TYPE_SHIFT;
265 * This driver supports only RNGC and RNGB. (There's a different
268 if (rng_type != RNGC_TYPE_RNGC && rng_type != RNGC_TYPE_RNGB) {
273 init_completion(&rngc->rng_op_done);
275 rngc->rng.name = pdev->name;
276 rngc->rng.init = imx_rngc_init;
277 rngc->rng.read = imx_rngc_read;
278 rngc->rng.cleanup = imx_rngc_cleanup;
279 rngc->rng.quality = 19;
281 rngc->dev = &pdev->dev;
282 platform_set_drvdata(pdev, rngc);
284 imx_rngc_irq_mask_clear(rngc);
286 ret = devm_request_irq(&pdev->dev,
287 irq, imx_rngc_irq, 0, pdev->name, (void *)rngc);
289 dev_err(rngc->dev, "Can't get interrupt working.\n");
294 ret = imx_rngc_self_test(rngc);
296 dev_err(rngc->dev, "self test failed\n");
301 ret = hwrng_register(&rngc->rng);
303 dev_err(&pdev->dev, "hwrng registration failed\n");
308 "Freescale RNG%c registered (HW revision %d.%02d)\n",
309 rng_type == RNGC_TYPE_RNGB ? 'B' : 'C',
310 (ver_id >> RNGC_VER_MAJ_SHIFT) & 0xff, ver_id & 0xff);
314 clk_disable_unprepare(rngc->clk);
319 static int __exit imx_rngc_remove(struct platform_device *pdev)
321 struct imx_rngc *rngc = platform_get_drvdata(pdev);
323 hwrng_unregister(&rngc->rng);
325 clk_disable_unprepare(rngc->clk);
330 static int __maybe_unused imx_rngc_suspend(struct device *dev)
332 struct imx_rngc *rngc = dev_get_drvdata(dev);
334 clk_disable_unprepare(rngc->clk);
339 static int __maybe_unused imx_rngc_resume(struct device *dev)
341 struct imx_rngc *rngc = dev_get_drvdata(dev);
343 clk_prepare_enable(rngc->clk);
348 static SIMPLE_DEV_PM_OPS(imx_rngc_pm_ops, imx_rngc_suspend, imx_rngc_resume);
350 static const struct of_device_id imx_rngc_dt_ids[] = {
351 { .compatible = "fsl,imx25-rngb", .data = NULL, },
354 MODULE_DEVICE_TABLE(of, imx_rngc_dt_ids);
356 static struct platform_driver imx_rngc_driver = {
359 .pm = &imx_rngc_pm_ops,
360 .of_match_table = imx_rngc_dt_ids,
362 .remove = __exit_p(imx_rngc_remove),
365 module_platform_driver_probe(imx_rngc_driver, imx_rngc_probe);
367 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
368 MODULE_DESCRIPTION("H/W RNGC driver for i.MX");
369 MODULE_LICENSE("GPL");