105ba8846SStefan Wahren // SPDX-License-Identifier: GPL-2.0 205ba8846SStefan Wahren /* 38c4196a2SLubomir Rintel * Copyright (c) 2010-2012 Broadcom. All rights reserved. 48c4196a2SLubomir Rintel * Copyright (c) 2013 Lubomir Rintel 58c4196a2SLubomir Rintel */ 68c4196a2SLubomir Rintel 78c4196a2SLubomir Rintel #include <linux/hw_random.h> 88c4196a2SLubomir Rintel #include <linux/io.h> 98c4196a2SLubomir Rintel #include <linux/kernel.h> 108c4196a2SLubomir Rintel #include <linux/module.h> 118c4196a2SLubomir Rintel #include <linux/of_address.h> 128c4196a2SLubomir Rintel #include <linux/of_platform.h> 138c4196a2SLubomir Rintel #include <linux/platform_device.h> 148c4196a2SLubomir Rintel #include <linux/printk.h> 15791af4f4SFlorian Fainelli #include <linux/clk.h> 168c4196a2SLubomir Rintel 178c4196a2SLubomir Rintel #define RNG_CTRL 0x0 188c4196a2SLubomir Rintel #define RNG_STATUS 0x4 198c4196a2SLubomir Rintel #define RNG_DATA 0x8 20422a7491SYendapally Reddy Dhananjaya Reddy #define RNG_INT_MASK 0x10 218c4196a2SLubomir Rintel 228c4196a2SLubomir Rintel /* enable rng */ 238c4196a2SLubomir Rintel #define RNG_RBGEN 0x1 248c4196a2SLubomir Rintel 258c4196a2SLubomir Rintel /* the initial numbers generated are "less random" so will be discarded */ 268c4196a2SLubomir Rintel #define RNG_WARMUP_COUNT 0x40000 278c4196a2SLubomir Rintel 28422a7491SYendapally Reddy Dhananjaya Reddy #define RNG_INT_OFF 0x1 29422a7491SYendapally Reddy Dhananjaya Reddy 30b788479fSFlorian Fainelli struct bcm2835_rng_priv { 31b788479fSFlorian Fainelli struct hwrng rng; 32b788479fSFlorian Fainelli void __iomem *base; 3304b154faSFlorian Fainelli bool mask_interrupts; 34791af4f4SFlorian Fainelli struct clk *clk; 35b788479fSFlorian Fainelli }; 36b788479fSFlorian Fainelli 37b788479fSFlorian Fainelli static inline struct bcm2835_rng_priv *to_rng_priv(struct hwrng *rng) 38b788479fSFlorian Fainelli { 39b788479fSFlorian Fainelli return container_of(rng, struct bcm2835_rng_priv, rng); 40b788479fSFlorian Fainelli } 41b788479fSFlorian Fainelli 42abd42026SFlorian Fainelli static inline u32 rng_readl(struct bcm2835_rng_priv *priv, u32 offset) 43abd42026SFlorian Fainelli { 446f09359aSFlorian Fainelli /* MIPS chips strapped for BE will automagically configure the 456f09359aSFlorian Fainelli * peripheral registers for CPU-native byte order. 466f09359aSFlorian Fainelli */ 476f09359aSFlorian Fainelli if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) 486f09359aSFlorian Fainelli return __raw_readl(priv->base + offset); 496f09359aSFlorian Fainelli else 50abd42026SFlorian Fainelli return readl(priv->base + offset); 51abd42026SFlorian Fainelli } 52abd42026SFlorian Fainelli 53abd42026SFlorian Fainelli static inline void rng_writel(struct bcm2835_rng_priv *priv, u32 val, 54abd42026SFlorian Fainelli u32 offset) 55abd42026SFlorian Fainelli { 566f09359aSFlorian Fainelli if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) 576f09359aSFlorian Fainelli __raw_writel(val, priv->base + offset); 586f09359aSFlorian Fainelli else 59abd42026SFlorian Fainelli writel(val, priv->base + offset); 60abd42026SFlorian Fainelli } 61abd42026SFlorian Fainelli 628c4196a2SLubomir Rintel static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max, 638c4196a2SLubomir Rintel bool wait) 648c4196a2SLubomir Rintel { 65b788479fSFlorian Fainelli struct bcm2835_rng_priv *priv = to_rng_priv(rng); 664f8de65bSYendapally Reddy Dhananjaya Reddy u32 max_words = max / sizeof(u32); 674f8de65bSYendapally Reddy Dhananjaya Reddy u32 num_words, count; 688c4196a2SLubomir Rintel 69abd42026SFlorian Fainelli while ((rng_readl(priv, RNG_STATUS) >> 24) == 0) { 708c4196a2SLubomir Rintel if (!wait) 718c4196a2SLubomir Rintel return 0; 728c4196a2SLubomir Rintel cpu_relax(); 738c4196a2SLubomir Rintel } 748c4196a2SLubomir Rintel 75abd42026SFlorian Fainelli num_words = rng_readl(priv, RNG_STATUS) >> 24; 764f8de65bSYendapally Reddy Dhananjaya Reddy if (num_words > max_words) 774f8de65bSYendapally Reddy Dhananjaya Reddy num_words = max_words; 784f8de65bSYendapally Reddy Dhananjaya Reddy 794f8de65bSYendapally Reddy Dhananjaya Reddy for (count = 0; count < num_words; count++) 80abd42026SFlorian Fainelli ((u32 *)buf)[count] = rng_readl(priv, RNG_DATA); 814f8de65bSYendapally Reddy Dhananjaya Reddy 824f8de65bSYendapally Reddy Dhananjaya Reddy return num_words * sizeof(u32); 838c4196a2SLubomir Rintel } 848c4196a2SLubomir Rintel 85a8157775SFlorian Fainelli static int bcm2835_rng_init(struct hwrng *rng) 86a8157775SFlorian Fainelli { 87a8157775SFlorian Fainelli struct bcm2835_rng_priv *priv = to_rng_priv(rng); 88791af4f4SFlorian Fainelli int ret = 0; 8904b154faSFlorian Fainelli u32 val; 9004b154faSFlorian Fainelli 91791af4f4SFlorian Fainelli if (!IS_ERR(priv->clk)) { 92791af4f4SFlorian Fainelli ret = clk_prepare_enable(priv->clk); 93791af4f4SFlorian Fainelli if (ret) 94791af4f4SFlorian Fainelli return ret; 95791af4f4SFlorian Fainelli } 96791af4f4SFlorian Fainelli 9704b154faSFlorian Fainelli if (priv->mask_interrupts) { 9804b154faSFlorian Fainelli /* mask the interrupt */ 99abd42026SFlorian Fainelli val = rng_readl(priv, RNG_INT_MASK); 10004b154faSFlorian Fainelli val |= RNG_INT_OFF; 101abd42026SFlorian Fainelli rng_writel(priv, val, RNG_INT_MASK); 10204b154faSFlorian Fainelli } 103a8157775SFlorian Fainelli 104a8157775SFlorian Fainelli /* set warm-up count & enable */ 105abd42026SFlorian Fainelli rng_writel(priv, RNG_WARMUP_COUNT, RNG_STATUS); 106abd42026SFlorian Fainelli rng_writel(priv, RNG_RBGEN, RNG_CTRL); 107a8157775SFlorian Fainelli 108791af4f4SFlorian Fainelli return ret; 109a8157775SFlorian Fainelli } 110a8157775SFlorian Fainelli 111ec94bca7SFlorian Fainelli static void bcm2835_rng_cleanup(struct hwrng *rng) 112ec94bca7SFlorian Fainelli { 113ec94bca7SFlorian Fainelli struct bcm2835_rng_priv *priv = to_rng_priv(rng); 114ec94bca7SFlorian Fainelli 115ec94bca7SFlorian Fainelli /* disable rng hardware */ 116abd42026SFlorian Fainelli rng_writel(priv, 0, RNG_CTRL); 117791af4f4SFlorian Fainelli 118791af4f4SFlorian Fainelli if (!IS_ERR(priv->clk)) 119791af4f4SFlorian Fainelli clk_disable_unprepare(priv->clk); 120ec94bca7SFlorian Fainelli } 121ec94bca7SFlorian Fainelli 12204b154faSFlorian Fainelli struct bcm2835_rng_of_data { 12304b154faSFlorian Fainelli bool mask_interrupts; 12404b154faSFlorian Fainelli }; 12504b154faSFlorian Fainelli 12604b154faSFlorian Fainelli static const struct bcm2835_rng_of_data nsp_rng_of_data = { 12704b154faSFlorian Fainelli .mask_interrupts = true, 12804b154faSFlorian Fainelli }; 12904b154faSFlorian Fainelli 130422a7491SYendapally Reddy Dhananjaya Reddy static const struct of_device_id bcm2835_rng_of_match[] = { 131422a7491SYendapally Reddy Dhananjaya Reddy { .compatible = "brcm,bcm2835-rng"}, 13204b154faSFlorian Fainelli { .compatible = "brcm,bcm-nsp-rng", .data = &nsp_rng_of_data }, 13304b154faSFlorian Fainelli { .compatible = "brcm,bcm5301x-rng", .data = &nsp_rng_of_data }, 1348705f24fSFlorian Fainelli { .compatible = "brcm,bcm6368-rng"}, 135422a7491SYendapally Reddy Dhananjaya Reddy {}, 136422a7491SYendapally Reddy Dhananjaya Reddy }; 137422a7491SYendapally Reddy Dhananjaya Reddy 1388c4196a2SLubomir Rintel static int bcm2835_rng_probe(struct platform_device *pdev) 1398c4196a2SLubomir Rintel { 14004b154faSFlorian Fainelli const struct bcm2835_rng_of_data *of_data; 1418c4196a2SLubomir Rintel struct device *dev = &pdev->dev; 1428c4196a2SLubomir Rintel struct device_node *np = dev->of_node; 143422a7491SYendapally Reddy Dhananjaya Reddy const struct of_device_id *rng_id; 144b788479fSFlorian Fainelli struct bcm2835_rng_priv *priv; 1458c4196a2SLubomir Rintel int err; 1468c4196a2SLubomir Rintel 147b788479fSFlorian Fainelli priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 148b788479fSFlorian Fainelli if (!priv) 149b788479fSFlorian Fainelli return -ENOMEM; 150b788479fSFlorian Fainelli 151b788479fSFlorian Fainelli platform_set_drvdata(pdev, priv); 152b788479fSFlorian Fainelli 1538c4196a2SLubomir Rintel /* map peripheral */ 154*3e46bd34SYueHaibing priv->base = devm_platform_ioremap_resource(pdev, 0); 155c4fc5d6dSweiyongjun \(A\) if (IS_ERR(priv->base)) 156b788479fSFlorian Fainelli return PTR_ERR(priv->base); 157b788479fSFlorian Fainelli 158791af4f4SFlorian Fainelli /* Clock is optional on most platforms */ 159791af4f4SFlorian Fainelli priv->clk = devm_clk_get(dev, NULL); 1607b4c5d30SStefan Wahren if (IS_ERR(priv->clk) && PTR_ERR(priv->clk) == -EPROBE_DEFER) 1617b4c5d30SStefan Wahren return -EPROBE_DEFER; 162791af4f4SFlorian Fainelli 1638705f24fSFlorian Fainelli priv->rng.name = pdev->name; 164a8157775SFlorian Fainelli priv->rng.init = bcm2835_rng_init; 165b788479fSFlorian Fainelli priv->rng.read = bcm2835_rng_read; 166ec94bca7SFlorian Fainelli priv->rng.cleanup = bcm2835_rng_cleanup; 1678c4196a2SLubomir Rintel 168984798deSJonas Gorski if (dev_of_node(dev)) { 169422a7491SYendapally Reddy Dhananjaya Reddy rng_id = of_match_node(bcm2835_rng_of_match, np); 17021bb0ef4SFlorian Fainelli if (!rng_id) 171422a7491SYendapally Reddy Dhananjaya Reddy return -EINVAL; 17221bb0ef4SFlorian Fainelli 173422a7491SYendapally Reddy Dhananjaya Reddy /* Check for rng init function, execute it */ 17404b154faSFlorian Fainelli of_data = rng_id->data; 17504b154faSFlorian Fainelli if (of_data) 17604b154faSFlorian Fainelli priv->mask_interrupts = of_data->mask_interrupts; 177984798deSJonas Gorski } 178422a7491SYendapally Reddy Dhananjaya Reddy 1798c4196a2SLubomir Rintel /* register driver */ 18016a4c04bSFlorian Fainelli err = devm_hwrng_register(dev, &priv->rng); 18121bb0ef4SFlorian Fainelli if (err) 1828c4196a2SLubomir Rintel dev_err(dev, "hwrng registration failed\n"); 18321bb0ef4SFlorian Fainelli else 1848c4196a2SLubomir Rintel dev_info(dev, "hwrng registered\n"); 1858c4196a2SLubomir Rintel 1868c4196a2SLubomir Rintel return err; 1878c4196a2SLubomir Rintel } 1888c4196a2SLubomir Rintel 1898c4196a2SLubomir Rintel MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match); 1908c4196a2SLubomir Rintel 1918705f24fSFlorian Fainelli static struct platform_device_id bcm2835_rng_devtype[] = { 1928705f24fSFlorian Fainelli { .name = "bcm2835-rng" }, 1938705f24fSFlorian Fainelli { .name = "bcm63xx-rng" }, 1948705f24fSFlorian Fainelli { /* sentinel */ } 1958705f24fSFlorian Fainelli }; 1968705f24fSFlorian Fainelli MODULE_DEVICE_TABLE(platform, bcm2835_rng_devtype); 1978705f24fSFlorian Fainelli 1988c4196a2SLubomir Rintel static struct platform_driver bcm2835_rng_driver = { 1998c4196a2SLubomir Rintel .driver = { 2008c4196a2SLubomir Rintel .name = "bcm2835-rng", 2018c4196a2SLubomir Rintel .of_match_table = bcm2835_rng_of_match, 2028c4196a2SLubomir Rintel }, 2038c4196a2SLubomir Rintel .probe = bcm2835_rng_probe, 2048705f24fSFlorian Fainelli .id_table = bcm2835_rng_devtype, 2058c4196a2SLubomir Rintel }; 2068c4196a2SLubomir Rintel module_platform_driver(bcm2835_rng_driver); 2078c4196a2SLubomir Rintel 2088c4196a2SLubomir Rintel MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>"); 2098c4196a2SLubomir Rintel MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver"); 2108c4196a2SLubomir Rintel MODULE_LICENSE("GPL v2"); 211