4 * Joshua Henderson <joshua.henderson@microchip.com>
5 * Copyright (C) 2016 Microchip Technology Inc. All rights reserved.
7 * This program is free software; you can distribute it and/or modify it
8 * under the terms of the GNU General Public License (Version 2) as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include <linux/clk.h>
18 #include <linux/clkdev.h>
19 #include <linux/err.h>
20 #include <linux/hw_random.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
25 #include <linux/of_device.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
32 #define PRNGCONT BIT(10)
33 #define TRNGMOD BIT(11)
34 #define SEEDLOAD BIT(12)
37 #define RNGNUMGEN1 0x10
38 #define RNGNUMGEN2 0x14
42 #define RCNT_MASK 0x7F
51 * The TRNG can generate up to 24Mbps. This is a timeout that should be safe
52 * enough given the instructions in the loop and that the TRNG may not always
55 #define RNG_TIMEOUT 500
57 static int pic32_rng_read(struct hwrng
*rng
, void *buf
, size_t max
,
60 struct pic32_rng
*priv
= container_of(rng
, struct pic32_rng
, rng
);
63 unsigned int timeout
= RNG_TIMEOUT
;
69 t
= readl(priv
->base
+ RNGRCNT
) & RCNT_MASK
;
71 /* TRNG value comes through the seed registers */
72 *data
= ((u64
)readl(priv
->base
+ RNGSEED2
) << 32) +
73 readl(priv
->base
+ RNGSEED1
);
76 } while (wait
&& --timeout
);
81 static int pic32_rng_probe(struct platform_device
*pdev
)
83 struct pic32_rng
*priv
;
88 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
92 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
93 priv
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
94 if (IS_ERR(priv
->base
))
95 return PTR_ERR(priv
->base
);
97 priv
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
98 if (IS_ERR(priv
->clk
))
99 return PTR_ERR(priv
->clk
);
101 ret
= clk_prepare_enable(priv
->clk
);
105 /* enable TRNG in enhanced mode */
106 v
= TRNGEN
| TRNGMOD
;
107 writel(v
, priv
->base
+ RNGCON
);
109 priv
->rng
.name
= pdev
->name
;
110 priv
->rng
.read
= pic32_rng_read
;
112 ret
= hwrng_register(&priv
->rng
);
116 platform_set_drvdata(pdev
, priv
);
121 clk_disable_unprepare(priv
->clk
);
125 static int pic32_rng_remove(struct platform_device
*pdev
)
127 struct pic32_rng
*rng
= platform_get_drvdata(pdev
);
129 hwrng_unregister(&rng
->rng
);
130 writel(0, rng
->base
+ RNGCON
);
131 clk_disable_unprepare(rng
->clk
);
135 static const struct of_device_id pic32_rng_of_match
[] = {
136 { .compatible
= "microchip,pic32mzda-rng", },
139 MODULE_DEVICE_TABLE(of
, pic32_rng_of_match
);
141 static struct platform_driver pic32_rng_driver
= {
142 .probe
= pic32_rng_probe
,
143 .remove
= pic32_rng_remove
,
146 .owner
= THIS_MODULE
,
147 .of_match_table
= of_match_ptr(pic32_rng_of_match
),
151 module_platform_driver(pic32_rng_driver
);
153 MODULE_LICENSE("GPL");
154 MODULE_AUTHOR("Joshua Henderson <joshua.henderson@microchip.com>");
155 MODULE_DESCRIPTION("Microchip PIC32 RNG Driver");