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