xref: /linux/drivers/char/hw_random/pic32-rng.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * PIC32 RNG driver
4  *
5  * Joshua Henderson <joshua.henderson@microchip.com>
6  * Copyright (C) 2016 Microchip Technology Inc.  All rights reserved.
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/clkdev.h>
11 #include <linux/err.h>
12 #include <linux/hw_random.h>
13 #include <linux/io.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 
19 #define RNGCON		0x04
20 #define TRNGEN		BIT(8)
21 #define TRNGMOD		BIT(11)
22 #define RNGSEED1	0x18
23 #define RNGSEED2	0x1C
24 #define RNGRCNT		0x20
25 #define RCNT_MASK	0x7F
26 
27 struct pic32_rng {
28 	void __iomem	*base;
29 	struct hwrng	rng;
30 };
31 
32 /*
33  * The TRNG can generate up to 24Mbps. This is a timeout that should be safe
34  * enough given the instructions in the loop and that the TRNG may not always
35  * be at maximum rate.
36  */
37 #define RNG_TIMEOUT 500
38 
39 static int pic32_rng_init(struct hwrng *rng)
40 {
41 	struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
42 
43 	/* enable TRNG in enhanced mode */
44 	writel(TRNGEN | TRNGMOD, priv->base + RNGCON);
45 	return 0;
46 }
47 
48 static int pic32_rng_read(struct hwrng *rng, void *buf, size_t max,
49 			  bool wait)
50 {
51 	struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
52 	u64 *data = buf;
53 	u32 t;
54 	unsigned int timeout = RNG_TIMEOUT;
55 
56 	do {
57 		t = readl(priv->base + RNGRCNT) & RCNT_MASK;
58 		if (t == 64) {
59 			/* TRNG value comes through the seed registers */
60 			*data = ((u64)readl(priv->base + RNGSEED2) << 32) +
61 				readl(priv->base + RNGSEED1);
62 			return 8;
63 		}
64 	} while (wait && --timeout);
65 
66 	return -EIO;
67 }
68 
69 static void pic32_rng_cleanup(struct hwrng *rng)
70 {
71 	struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
72 
73 	writel(0, priv->base + RNGCON);
74 }
75 
76 static int pic32_rng_probe(struct platform_device *pdev)
77 {
78 	struct pic32_rng *priv;
79 	struct clk *clk;
80 
81 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
82 	if (!priv)
83 		return -ENOMEM;
84 
85 	priv->base = devm_platform_ioremap_resource(pdev, 0);
86 	if (IS_ERR(priv->base))
87 		return PTR_ERR(priv->base);
88 
89 	clk = devm_clk_get_enabled(&pdev->dev, NULL);
90 	if (IS_ERR(clk))
91 		return PTR_ERR(clk);
92 
93 	priv->rng.name = pdev->name;
94 	priv->rng.init = pic32_rng_init;
95 	priv->rng.read = pic32_rng_read;
96 	priv->rng.cleanup = pic32_rng_cleanup;
97 
98 	return devm_hwrng_register(&pdev->dev, &priv->rng);
99 }
100 
101 static const struct of_device_id pic32_rng_of_match[] __maybe_unused = {
102 	{ .compatible	= "microchip,pic32mzda-rng", },
103 	{ /* sentinel */ }
104 };
105 MODULE_DEVICE_TABLE(of, pic32_rng_of_match);
106 
107 static struct platform_driver pic32_rng_driver = {
108 	.probe		= pic32_rng_probe,
109 	.driver		= {
110 		.name	= "pic32-rng",
111 		.of_match_table = pic32_rng_of_match,
112 	},
113 };
114 
115 module_platform_driver(pic32_rng_driver);
116 
117 MODULE_LICENSE("GPL");
118 MODULE_AUTHOR("Joshua Henderson <joshua.henderson@microchip.com>");
119 MODULE_DESCRIPTION("Microchip PIC32 RNG Driver");
120