xref: /linux/drivers/char/hw_random/bcm74110-rng.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2024 Broadcom
4  */
5 
6 #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
7 
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/io.h>
11 #include <linux/delay.h>
12 #include <linux/platform_device.h>
13 #include <linux/random.h>
14 #include <linux/hw_random.h>
15 
16 #define HOST_REV_ID		0x00
17 #define HOST_FIFO_DEPTH		0x04
18 #define HOST_FIFO_COUNT		0x08
19 #define HOST_FIFO_THRESHOLD	0x0c
20 #define HOST_FIFO_DATA		0x10
21 
22 #define HOST_FIFO_COUNT_MASK		0xffff
23 
24 /* Delay range in microseconds */
25 #define FIFO_DELAY_MIN_US		3
26 #define FIFO_DELAY_MAX_US		7
27 #define FIFO_DELAY_MAX_COUNT		10
28 
29 struct bcm74110_priv {
30 	void __iomem *base;
31 };
32 
33 static inline int bcm74110_rng_fifo_count(void __iomem *mem)
34 {
35 	return readl_relaxed(mem) & HOST_FIFO_COUNT_MASK;
36 }
37 
38 static int bcm74110_rng_read(struct hwrng *rng, void *buf, size_t max,
39 			bool wait)
40 {
41 	struct bcm74110_priv *priv = (struct bcm74110_priv *)rng->priv;
42 	void __iomem *fc_addr = priv->base + HOST_FIFO_COUNT;
43 	void __iomem *fd_addr = priv->base + HOST_FIFO_DATA;
44 	unsigned underrun_count = 0;
45 	u32 max_words = max / sizeof(u32);
46 	u32 num_words;
47 	unsigned i;
48 
49 	/*
50 	 * We need to check how many words are available in the RNG FIFO. If
51 	 * there aren't any, we need to wait for some to become available.
52 	 */
53 	while ((num_words = bcm74110_rng_fifo_count(fc_addr)) == 0) {
54 		if (!wait)
55 			return 0;
56 		/*
57 		 * As a precaution, limit how long we wait. If the FIFO doesn't
58 		 * refill within the allotted time, return 0 (=no data) to the
59 		 * caller.
60 		 */
61 		if (likely(underrun_count < FIFO_DELAY_MAX_COUNT))
62 			usleep_range(FIFO_DELAY_MIN_US, FIFO_DELAY_MAX_US);
63 		else
64 			return 0;
65 		underrun_count++;
66 	}
67 	if (num_words > max_words)
68 		num_words = max_words;
69 
70 	/* Bail early if we run out of random numbers unexpectedly */
71 	for (i = 0; i < num_words && bcm74110_rng_fifo_count(fc_addr) > 0; i++)
72 		((u32 *)buf)[i] = readl_relaxed(fd_addr);
73 
74 	return i * sizeof(u32);
75 }
76 
77 static struct hwrng bcm74110_hwrng = {
78 	.read = bcm74110_rng_read,
79 };
80 
81 static int bcm74110_rng_probe(struct platform_device *pdev)
82 {
83 	struct device *dev = &pdev->dev;
84 	struct bcm74110_priv *priv;
85 	int rc;
86 
87 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
88 	if (!priv)
89 		return -ENOMEM;
90 
91 	bcm74110_hwrng.name = pdev->name;
92 	bcm74110_hwrng.priv = (unsigned long)priv;
93 
94 	priv->base = devm_platform_ioremap_resource(pdev, 0);
95 	if (IS_ERR(priv->base))
96 		return PTR_ERR(priv->base);
97 
98 	rc = devm_hwrng_register(dev, &bcm74110_hwrng);
99 	if (rc)
100 		dev_err(dev, "hwrng registration failed (%d)\n", rc);
101 	else
102 		dev_info(dev, "hwrng registered\n");
103 
104 	return rc;
105 }
106 
107 static const struct of_device_id bcm74110_rng_match[] = {
108 	{ .compatible	= "brcm,bcm74110-rng", },
109 	{},
110 };
111 MODULE_DEVICE_TABLE(of, bcm74110_rng_match);
112 
113 static struct platform_driver bcm74110_rng_driver = {
114 	.driver = {
115 		.name = KBUILD_MODNAME,
116 		.of_match_table = bcm74110_rng_match,
117 	},
118 	.probe	= bcm74110_rng_probe,
119 };
120 module_platform_driver(bcm74110_rng_driver);
121 
122 MODULE_AUTHOR("Markus Mayer <mmayer@broadcom.com>");
123 MODULE_DESCRIPTION("BCM 74110 Random Number Generator (RNG) driver");
124 MODULE_LICENSE("GPL v2");
125