Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

hwrng: histb - Move driver to drivers/char/hw_random/histb-rng.c

Move to drivers/char/hw_random since histb-(t)rng does not provide
cryptography pseudo rng.

histb-rng is pretty like hisi-rng, but after investigation, we confirm
there is no RNG_PHY_SEED register on histb-rng so a separate driver is
needed.

Still we rename relevant function names to match those in hisi-rng.

Link: https://lore.kernel.org/r/20230401164448.1393336-1-mmyangfl@gmail.com
Signed-off-by: David Yang <mmyangfl@gmail.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

David Yang and committed by
Herbert Xu
903e6ada 69f1c387

+53 -54
+11
drivers/char/hw_random/Kconfig
··· 335 335 336 336 If unsure, say Y. 337 337 338 + config HW_RANDOM_HISTB 339 + tristate "Hisilicon STB Random Number Generator support" 340 + depends on ARCH_HISI || COMPILE_TEST 341 + default ARCH_HISI 342 + help 343 + This driver provides kernel-side support for the Random Number 344 + Generator hardware found on Hisilicon Hi37xx SoC. 345 + 346 + To compile this driver as a module, choose M here: the 347 + module will be called histb-rng. 348 + 338 349 config HW_RANDOM_ST 339 350 tristate "ST Microelectronics HW Random Number Generator support" 340 351 depends on HW_RANDOM && ARCH_STI
+1
drivers/char/hw_random/Makefile
··· 29 29 obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o 30 30 obj-$(CONFIG_HW_RANDOM_POWERNV) += powernv-rng.o 31 31 obj-$(CONFIG_HW_RANDOM_HISI) += hisi-rng.o 32 + obj-$(CONFIG_HW_RANDOM_HISTB) += histb-rng.o 32 33 obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o 33 34 obj-$(CONFIG_HW_RANDOM_IPROC_RNG200) += iproc-rng200.o 34 35 obj-$(CONFIG_HW_RANDOM_ST) += st-rng.o
-7
drivers/crypto/hisilicon/Kconfig
··· 82 82 select CRYPTO_RNG 83 83 help 84 84 Support for HiSilicon TRNG Driver. 85 - 86 - config CRYPTO_DEV_HISTB_TRNG 87 - tristate "Support for HiSTB TRNG Driver" 88 - depends on ARCH_HISI || COMPILE_TEST 89 - select HW_RANDOM 90 - help 91 - Support for HiSTB TRNG Driver.
+1 -1
drivers/crypto/hisilicon/Makefile
··· 5 5 obj-$(CONFIG_CRYPTO_DEV_HISI_QM) += hisi_qm.o 6 6 hisi_qm-objs = qm.o sgl.o debugfs.o 7 7 obj-$(CONFIG_CRYPTO_DEV_HISI_ZIP) += zip/ 8 - obj-y += trng/ 8 + obj-$(CONFIG_CRYPTO_DEV_HISI_TRNG) += trng/
-3
drivers/crypto/hisilicon/trng/Makefile
··· 1 1 obj-$(CONFIG_CRYPTO_DEV_HISI_TRNG) += hisi-trng-v2.o 2 2 hisi-trng-v2-objs = trng.o 3 - 4 - obj-$(CONFIG_CRYPTO_DEV_HISTB_TRNG) += histb-trng.o 5 - histb-trng-objs += trng-stb.o
+40 -43
drivers/crypto/hisilicon/trng/trng-stb.c drivers/char/hw_random/histb-rng.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-or-later OR MIT 2 2 /* 3 - * Device driver for True RNG in HiSTB SoCs 4 - * 5 3 * Copyright (c) 2023 David Yang 6 4 */ 7 5 8 - #include <crypto/internal/rng.h> 9 - #include <linux/device.h> 10 6 #include <linux/err.h> 11 7 #include <linux/hw_random.h> 12 8 #include <linux/io.h> 13 9 #include <linux/iopoll.h> 14 10 #include <linux/kernel.h> 11 + #include <linux/mod_devicetable.h> 15 12 #include <linux/module.h> 16 - #include <linux/mutex.h> 17 - #include <linux/of_device.h> 13 + #include <linux/platform_device.h> 18 14 19 - #define HISTB_TRNG_CTRL 0x0 15 + #define RNG_CTRL 0x0 20 16 #define RNG_SOURCE GENMASK(1, 0) 21 17 #define DROP_ENABLE BIT(5) 22 18 #define POST_PROCESS_ENABLE BIT(7) 23 19 #define POST_PROCESS_DEPTH GENMASK(15, 8) 24 - #define HISTB_TRNG_NUMBER 0x4 25 - #define HISTB_TRNG_STAT 0x8 20 + #define RNG_NUMBER 0x4 21 + #define RNG_STAT 0x8 26 22 #define DATA_COUNT GENMASK(2, 0) /* max 4 */ 27 23 28 - struct histb_trng_priv { 24 + struct histb_rng_priv { 29 25 struct hwrng rng; 30 26 void __iomem *base; 31 27 }; ··· 31 35 * depth = 1 -> ~1ms 32 36 * depth = 255 -> ~16ms 33 37 */ 34 - static int histb_trng_wait(void __iomem *base) 38 + static int histb_rng_wait(void __iomem *base) 35 39 { 36 40 u32 val; 37 41 38 - return readl_relaxed_poll_timeout(base + HISTB_TRNG_STAT, val, 42 + return readl_relaxed_poll_timeout(base + RNG_STAT, val, 39 43 val & DATA_COUNT, 1000, 30 * 1000); 40 44 } 41 45 42 - static void histb_trng_init(void __iomem *base, unsigned int depth) 46 + static void histb_rng_init(void __iomem *base, unsigned int depth) 43 47 { 44 48 u32 val; 45 49 46 - val = readl_relaxed(base + HISTB_TRNG_CTRL); 50 + val = readl_relaxed(base + RNG_CTRL); 47 51 48 52 val &= ~RNG_SOURCE; 49 53 val |= 2; ··· 54 58 val |= POST_PROCESS_ENABLE; 55 59 val |= DROP_ENABLE; 56 60 57 - writel_relaxed(val, base + HISTB_TRNG_CTRL); 61 + writel_relaxed(val, base + RNG_CTRL); 58 62 } 59 63 60 - static int histb_trng_read(struct hwrng *rng, void *data, size_t max, bool wait) 64 + static int histb_rng_read(struct hwrng *rng, void *data, size_t max, bool wait) 61 65 { 62 - struct histb_trng_priv *priv = container_of(rng, typeof(*priv), rng); 66 + struct histb_rng_priv *priv = container_of(rng, typeof(*priv), rng); 63 67 void __iomem *base = priv->base; 64 68 65 69 for (int i = 0; i < max; i += sizeof(u32)) { 66 - if (!(readl_relaxed(base + HISTB_TRNG_STAT) & DATA_COUNT)) { 70 + if (!(readl_relaxed(base + RNG_STAT) & DATA_COUNT)) { 67 71 if (!wait) 68 72 return i; 69 - if (histb_trng_wait(base)) { 73 + if (histb_rng_wait(base)) { 70 74 pr_err("failed to generate random number, generated %d\n", 71 75 i); 72 76 return i ? i : -ETIMEDOUT; 73 77 } 74 78 } 75 - *(u32 *) (data + i) = readl_relaxed(base + HISTB_TRNG_NUMBER); 79 + *(u32 *) (data + i) = readl_relaxed(base + RNG_NUMBER); 76 80 } 77 81 78 82 return max; 79 83 } 80 84 81 - static unsigned int histb_trng_get_depth(void __iomem *base) 85 + static unsigned int histb_rng_get_depth(void __iomem *base) 82 86 { 83 - return (readl_relaxed(base + HISTB_TRNG_CTRL) & POST_PROCESS_DEPTH) >> 8; 87 + return (readl_relaxed(base + RNG_CTRL) & POST_PROCESS_DEPTH) >> 8; 84 88 } 85 89 86 90 static ssize_t 87 91 depth_show(struct device *dev, struct device_attribute *attr, char *buf) 88 92 { 89 - struct histb_trng_priv *priv = dev_get_drvdata(dev); 93 + struct histb_rng_priv *priv = dev_get_drvdata(dev); 90 94 void __iomem *base = priv->base; 91 95 92 - return sprintf(buf, "%d\n", histb_trng_get_depth(base)); 96 + return sprintf(buf, "%d\n", histb_rng_get_depth(base)); 93 97 } 94 98 95 99 static ssize_t 96 100 depth_store(struct device *dev, struct device_attribute *attr, 97 101 const char *buf, size_t count) 98 102 { 99 - struct histb_trng_priv *priv = dev_get_drvdata(dev); 103 + struct histb_rng_priv *priv = dev_get_drvdata(dev); 100 104 void __iomem *base = priv->base; 101 105 unsigned int depth; 102 106 103 107 if (kstrtouint(buf, 0, &depth)) 104 108 return -ERANGE; 105 109 106 - histb_trng_init(base, depth); 110 + histb_rng_init(base, depth); 107 111 return count; 108 112 } 109 113 110 114 static DEVICE_ATTR_RW(depth); 111 115 112 - static struct attribute *histb_trng_attrs[] = { 116 + static struct attribute *histb_rng_attrs[] = { 113 117 &dev_attr_depth.attr, 114 118 NULL, 115 119 }; 116 120 117 - ATTRIBUTE_GROUPS(histb_trng); 121 + ATTRIBUTE_GROUPS(histb_rng); 118 122 119 - static int histb_trng_probe(struct platform_device *pdev) 123 + static int histb_rng_probe(struct platform_device *pdev) 120 124 { 121 125 struct device *dev = &pdev->dev; 122 - struct histb_trng_priv *priv; 126 + struct histb_rng_priv *priv; 123 127 void __iomem *base; 124 128 int ret; 125 129 ··· 129 133 130 134 base = devm_platform_ioremap_resource(pdev, 0); 131 135 if (IS_ERR(base)) 132 - return -ENOMEM; 136 + return PTR_ERR(base); 133 137 134 - histb_trng_init(base, 144); 135 - if (histb_trng_wait(base)) { 138 + histb_rng_init(base, 144); 139 + if (histb_rng_wait(base)) { 136 140 dev_err(dev, "cannot bring up device\n"); 137 141 return -ENODEV; 138 142 } 139 143 140 144 priv->base = base; 141 145 priv->rng.name = pdev->name; 142 - priv->rng.read = histb_trng_read; 146 + priv->rng.read = histb_rng_read; 143 147 ret = devm_hwrng_register(dev, &priv->rng); 144 148 if (ret) { 145 149 dev_err(dev, "failed to register hwrng: %d\n", ret); ··· 151 155 return 0; 152 156 } 153 157 154 - static const struct of_device_id histb_trng_of_match[] = { 155 - { .compatible = "hisilicon,histb-trng", }, 158 + static const struct of_device_id histb_rng_of_match[] = { 159 + { .compatible = "hisilicon,histb-rng", }, 156 160 { } 157 161 }; 162 + MODULE_DEVICE_TABLE(of, histb_rng_of_match); 158 163 159 - static struct platform_driver histb_trng_driver = { 160 - .probe = histb_trng_probe, 164 + static struct platform_driver histb_rng_driver = { 165 + .probe = histb_rng_probe, 161 166 .driver = { 162 - .name = "histb-trng", 163 - .of_match_table = histb_trng_of_match, 164 - .dev_groups = histb_trng_groups, 167 + .name = "histb-rng", 168 + .of_match_table = histb_rng_of_match, 169 + .dev_groups = histb_rng_groups, 165 170 }, 166 171 }; 167 172 168 - module_platform_driver(histb_trng_driver); 173 + module_platform_driver(histb_rng_driver); 169 174 170 - MODULE_DESCRIPTION("HiSTB True RNG"); 175 + MODULE_DESCRIPTION("Hisilicon STB random number generator driver"); 171 176 MODULE_LICENSE("Dual MIT/GPL"); 172 177 MODULE_AUTHOR("David Yang <mmyangfl@gmail.com>");