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_COMMAND 0x0004
22 #define RNGC_CONTROL 0x0008
23 #define RNGC_STATUS 0x000C
24 #define RNGC_ERROR 0x0010
25 #define RNGC_FIFO 0x0014
27 #define RNGC_CMD_CLR_ERR 0x00000020
28 #define RNGC_CMD_CLR_INT 0x00000010
29 #define RNGC_CMD_SEED 0x00000002
30 #define RNGC_CMD_SELF_TEST 0x00000001
32 #define RNGC_CTRL_MASK_ERROR 0x00000040
33 #define RNGC_CTRL_MASK_DONE 0x00000020
35 #define RNGC_STATUS_ERROR 0x00010000
36 #define RNGC_STATUS_FIFO_LEVEL_MASK 0x00000f00
37 #define RNGC_STATUS_FIFO_LEVEL_SHIFT 8
38 #define RNGC_STATUS_SEED_DONE 0x00000020
39 #define RNGC_STATUS_ST_DONE 0x00000010
41 #define RNGC_ERROR_STATUS_STAT_ERR 0x00000008
43 #define RNGC_TIMEOUT 3000 /* 3 sec */
46 static bool self_test
= true;
47 module_param(self_test
, bool, 0);
54 struct completion rng_op_done
;
56 * err_reg is written only by the irq handler and read only
57 * when interrupts are masked, we need no spinlock
63 static inline void imx_rngc_irq_mask_clear(struct imx_rngc
*rngc
)
68 ctrl
= readl(rngc
->base
+ RNGC_CONTROL
);
69 ctrl
|= RNGC_CTRL_MASK_DONE
| RNGC_CTRL_MASK_ERROR
;
70 writel(ctrl
, rngc
->base
+ RNGC_CONTROL
);
73 * CLR_INT clears the interrupt only if there's no error
74 * CLR_ERR clear the interrupt and the error register if there
77 cmd
= readl(rngc
->base
+ RNGC_COMMAND
);
78 cmd
|= RNGC_CMD_CLR_INT
| RNGC_CMD_CLR_ERR
;
79 writel(cmd
, rngc
->base
+ RNGC_COMMAND
);
82 static inline void imx_rngc_irq_unmask(struct imx_rngc
*rngc
)
86 ctrl
= readl(rngc
->base
+ RNGC_CONTROL
);
87 ctrl
&= ~(RNGC_CTRL_MASK_DONE
| RNGC_CTRL_MASK_ERROR
);
88 writel(ctrl
, rngc
->base
+ RNGC_CONTROL
);
91 static int imx_rngc_self_test(struct imx_rngc
*rngc
)
96 imx_rngc_irq_unmask(rngc
);
99 cmd
= readl(rngc
->base
+ RNGC_COMMAND
);
100 writel(cmd
| RNGC_CMD_SELF_TEST
, rngc
->base
+ RNGC_COMMAND
);
102 ret
= wait_for_completion_timeout(&rngc
->rng_op_done
, RNGC_TIMEOUT
);
104 imx_rngc_irq_mask_clear(rngc
);
108 if (rngc
->err_reg
!= 0)
114 static int imx_rngc_read(struct hwrng
*rng
, void *data
, size_t max
, bool wait
)
116 struct imx_rngc
*rngc
= container_of(rng
, struct imx_rngc
, rng
);
121 while (max
>= sizeof(u32
)) {
122 status
= readl(rngc
->base
+ RNGC_STATUS
);
124 /* is there some error while reading this random number? */
125 if (status
& RNGC_STATUS_ERROR
)
128 /* how many random numbers are in FIFO? [0-16] */
129 level
= (status
& RNGC_STATUS_FIFO_LEVEL_MASK
) >>
130 RNGC_STATUS_FIFO_LEVEL_SHIFT
;
133 /* retrieve a random number from FIFO */
134 *(u32
*)data
= readl(rngc
->base
+ RNGC_FIFO
);
136 retval
+= sizeof(u32
);
142 return retval
? retval
: -EIO
;
145 static irqreturn_t
imx_rngc_irq(int irq
, void *priv
)
147 struct imx_rngc
*rngc
= (struct imx_rngc
*)priv
;
151 * clearing the interrupt will also clear the error register
152 * read error and status before clearing
154 status
= readl(rngc
->base
+ RNGC_STATUS
);
155 rngc
->err_reg
= readl(rngc
->base
+ RNGC_ERROR
);
157 imx_rngc_irq_mask_clear(rngc
);
159 if (status
& (RNGC_STATUS_SEED_DONE
| RNGC_STATUS_ST_DONE
))
160 complete(&rngc
->rng_op_done
);
165 static int imx_rngc_init(struct hwrng
*rng
)
167 struct imx_rngc
*rngc
= container_of(rng
, struct imx_rngc
, rng
);
172 cmd
= readl(rngc
->base
+ RNGC_COMMAND
);
173 writel(cmd
| RNGC_CMD_CLR_ERR
, rngc
->base
+ RNGC_COMMAND
);
175 /* create seed, repeat while there is some statistical error */
177 imx_rngc_irq_unmask(rngc
);
180 cmd
= readl(rngc
->base
+ RNGC_COMMAND
);
181 writel(cmd
| RNGC_CMD_SEED
, rngc
->base
+ RNGC_COMMAND
);
183 ret
= wait_for_completion_timeout(&rngc
->rng_op_done
,
187 imx_rngc_irq_mask_clear(rngc
);
191 } while (rngc
->err_reg
== RNGC_ERROR_STATUS_STAT_ERR
);
193 return rngc
->err_reg
? -EIO
: 0;
196 static int imx_rngc_probe(struct platform_device
*pdev
)
198 struct imx_rngc
*rngc
;
202 rngc
= devm_kzalloc(&pdev
->dev
, sizeof(*rngc
), GFP_KERNEL
);
206 rngc
->base
= devm_platform_ioremap_resource(pdev
, 0);
207 if (IS_ERR(rngc
->base
))
208 return PTR_ERR(rngc
->base
);
210 rngc
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
211 if (IS_ERR(rngc
->clk
)) {
212 dev_err(&pdev
->dev
, "Can not get rng_clk\n");
213 return PTR_ERR(rngc
->clk
);
216 irq
= platform_get_irq(pdev
, 0);
218 dev_err(&pdev
->dev
, "Couldn't get irq %d\n", irq
);
222 ret
= clk_prepare_enable(rngc
->clk
);
226 ret
= devm_request_irq(&pdev
->dev
,
227 irq
, imx_rngc_irq
, 0, pdev
->name
, (void *)rngc
);
229 dev_err(rngc
->dev
, "Can't get interrupt working.\n");
233 init_completion(&rngc
->rng_op_done
);
235 rngc
->rng
.name
= pdev
->name
;
236 rngc
->rng
.init
= imx_rngc_init
;
237 rngc
->rng
.read
= imx_rngc_read
;
239 rngc
->dev
= &pdev
->dev
;
240 platform_set_drvdata(pdev
, rngc
);
242 imx_rngc_irq_mask_clear(rngc
);
245 ret
= imx_rngc_self_test(rngc
);
247 dev_err(rngc
->dev
, "FSL RNGC self test failed.\n");
252 ret
= hwrng_register(&rngc
->rng
);
254 dev_err(&pdev
->dev
, "FSL RNGC registering failed (%d)\n", ret
);
258 dev_info(&pdev
->dev
, "Freescale RNGC registered.\n");
262 clk_disable_unprepare(rngc
->clk
);
267 static int __exit
imx_rngc_remove(struct platform_device
*pdev
)
269 struct imx_rngc
*rngc
= platform_get_drvdata(pdev
);
271 hwrng_unregister(&rngc
->rng
);
273 clk_disable_unprepare(rngc
->clk
);
278 static int __maybe_unused
imx_rngc_suspend(struct device
*dev
)
280 struct imx_rngc
*rngc
= dev_get_drvdata(dev
);
282 clk_disable_unprepare(rngc
->clk
);
287 static int __maybe_unused
imx_rngc_resume(struct device
*dev
)
289 struct imx_rngc
*rngc
= dev_get_drvdata(dev
);
291 clk_prepare_enable(rngc
->clk
);
296 static SIMPLE_DEV_PM_OPS(imx_rngc_pm_ops
, imx_rngc_suspend
, imx_rngc_resume
);
298 static const struct of_device_id imx_rngc_dt_ids
[] = {
299 { .compatible
= "fsl,imx25-rngb", .data
= NULL
, },
302 MODULE_DEVICE_TABLE(of
, imx_rngc_dt_ids
);
304 static struct platform_driver imx_rngc_driver
= {
307 .pm
= &imx_rngc_pm_ops
,
308 .of_match_table
= imx_rngc_dt_ids
,
310 .remove
= __exit_p(imx_rngc_remove
),
313 module_platform_driver_probe(imx_rngc_driver
, imx_rngc_probe
);
315 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
316 MODULE_DESCRIPTION("H/W RNGC driver for i.MX");
317 MODULE_LICENSE("GPL");