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> 110788257aSRob Herring #include <linux/of.h> 128c4196a2SLubomir Rintel #include <linux/platform_device.h> 138c4196a2SLubomir Rintel #include <linux/printk.h> 14791af4f4SFlorian Fainelli #include <linux/clk.h> 15e5f9f41dSÁlvaro Fernández Rojas #include <linux/reset.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; 35e5f9f41dSÁlvaro Fernández Rojas struct reset_control *reset; 36b788479fSFlorian Fainelli }; 37b788479fSFlorian Fainelli 38b788479fSFlorian Fainelli static inline struct bcm2835_rng_priv *to_rng_priv(struct hwrng *rng) 39b788479fSFlorian Fainelli { 40b788479fSFlorian Fainelli return container_of(rng, struct bcm2835_rng_priv, rng); 41b788479fSFlorian Fainelli } 42b788479fSFlorian Fainelli 43abd42026SFlorian Fainelli static inline u32 rng_readl(struct bcm2835_rng_priv *priv, u32 offset) 44abd42026SFlorian Fainelli { 456f09359aSFlorian Fainelli /* MIPS chips strapped for BE will automagically configure the 466f09359aSFlorian Fainelli * peripheral registers for CPU-native byte order. 476f09359aSFlorian Fainelli */ 486f09359aSFlorian Fainelli if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) 496f09359aSFlorian Fainelli return __raw_readl(priv->base + offset); 506f09359aSFlorian Fainelli else 51abd42026SFlorian Fainelli return readl(priv->base + offset); 52abd42026SFlorian Fainelli } 53abd42026SFlorian Fainelli 54abd42026SFlorian Fainelli static inline void rng_writel(struct bcm2835_rng_priv *priv, u32 val, 55abd42026SFlorian Fainelli u32 offset) 56abd42026SFlorian Fainelli { 576f09359aSFlorian Fainelli if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) 586f09359aSFlorian Fainelli __raw_writel(val, priv->base + offset); 596f09359aSFlorian Fainelli else 60abd42026SFlorian Fainelli writel(val, priv->base + offset); 61abd42026SFlorian Fainelli } 62abd42026SFlorian Fainelli 638c4196a2SLubomir Rintel static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max, 648c4196a2SLubomir Rintel bool wait) 658c4196a2SLubomir Rintel { 66b788479fSFlorian Fainelli struct bcm2835_rng_priv *priv = to_rng_priv(rng); 674f8de65bSYendapally Reddy Dhananjaya Reddy u32 max_words = max / sizeof(u32); 684f8de65bSYendapally Reddy Dhananjaya Reddy u32 num_words, count; 698c4196a2SLubomir Rintel 70abd42026SFlorian Fainelli while ((rng_readl(priv, RNG_STATUS) >> 24) == 0) { 718c4196a2SLubomir Rintel if (!wait) 728c4196a2SLubomir Rintel return 0; 73d0f3994cSStefan Wahren hwrng_yield(rng); 748c4196a2SLubomir Rintel } 758c4196a2SLubomir Rintel 76abd42026SFlorian Fainelli num_words = rng_readl(priv, RNG_STATUS) >> 24; 774f8de65bSYendapally Reddy Dhananjaya Reddy if (num_words > max_words) 784f8de65bSYendapally Reddy Dhananjaya Reddy num_words = max_words; 794f8de65bSYendapally Reddy Dhananjaya Reddy 804f8de65bSYendapally Reddy Dhananjaya Reddy for (count = 0; count < num_words; count++) 81abd42026SFlorian Fainelli ((u32 *)buf)[count] = rng_readl(priv, RNG_DATA); 824f8de65bSYendapally Reddy Dhananjaya Reddy 834f8de65bSYendapally Reddy Dhananjaya Reddy return num_words * sizeof(u32); 848c4196a2SLubomir Rintel } 858c4196a2SLubomir Rintel 86a8157775SFlorian Fainelli static int bcm2835_rng_init(struct hwrng *rng) 87a8157775SFlorian Fainelli { 88a8157775SFlorian Fainelli struct bcm2835_rng_priv *priv = to_rng_priv(rng); 89791af4f4SFlorian Fainelli int ret = 0; 9004b154faSFlorian Fainelli u32 val; 9104b154faSFlorian Fainelli 92791af4f4SFlorian Fainelli ret = clk_prepare_enable(priv->clk); 93791af4f4SFlorian Fainelli if (ret) 94791af4f4SFlorian Fainelli return ret; 95791af4f4SFlorian Fainelli 96e5f9f41dSÁlvaro Fernández Rojas ret = reset_control_reset(priv->reset); 97*3fd8e444SGaosheng Cui if (ret) { 98*3fd8e444SGaosheng Cui clk_disable_unprepare(priv->clk); 99e5f9f41dSÁlvaro Fernández Rojas return ret; 100*3fd8e444SGaosheng Cui } 101e5f9f41dSÁlvaro Fernández Rojas 10204b154faSFlorian Fainelli if (priv->mask_interrupts) { 10304b154faSFlorian Fainelli /* mask the interrupt */ 104abd42026SFlorian Fainelli val = rng_readl(priv, RNG_INT_MASK); 10504b154faSFlorian Fainelli val |= RNG_INT_OFF; 106abd42026SFlorian Fainelli rng_writel(priv, val, RNG_INT_MASK); 10704b154faSFlorian Fainelli } 108a8157775SFlorian Fainelli 109a8157775SFlorian Fainelli /* set warm-up count & enable */ 110abd42026SFlorian Fainelli rng_writel(priv, RNG_WARMUP_COUNT, RNG_STATUS); 111abd42026SFlorian Fainelli rng_writel(priv, RNG_RBGEN, RNG_CTRL); 112a8157775SFlorian Fainelli 113791af4f4SFlorian Fainelli return ret; 114a8157775SFlorian Fainelli } 115a8157775SFlorian Fainelli 116ec94bca7SFlorian Fainelli static void bcm2835_rng_cleanup(struct hwrng *rng) 117ec94bca7SFlorian Fainelli { 118ec94bca7SFlorian Fainelli struct bcm2835_rng_priv *priv = to_rng_priv(rng); 119ec94bca7SFlorian Fainelli 120ec94bca7SFlorian Fainelli /* disable rng hardware */ 121abd42026SFlorian Fainelli rng_writel(priv, 0, RNG_CTRL); 122791af4f4SFlorian Fainelli 123791af4f4SFlorian Fainelli clk_disable_unprepare(priv->clk); 124ec94bca7SFlorian Fainelli } 125ec94bca7SFlorian Fainelli 12604b154faSFlorian Fainelli struct bcm2835_rng_of_data { 12704b154faSFlorian Fainelli bool mask_interrupts; 12804b154faSFlorian Fainelli }; 12904b154faSFlorian Fainelli 13004b154faSFlorian Fainelli static const struct bcm2835_rng_of_data nsp_rng_of_data = { 13104b154faSFlorian Fainelli .mask_interrupts = true, 13204b154faSFlorian Fainelli }; 13304b154faSFlorian Fainelli 134422a7491SYendapally Reddy Dhananjaya Reddy static const struct of_device_id bcm2835_rng_of_match[] = { 135422a7491SYendapally Reddy Dhananjaya Reddy { .compatible = "brcm,bcm2835-rng"}, 13604b154faSFlorian Fainelli { .compatible = "brcm,bcm-nsp-rng", .data = &nsp_rng_of_data }, 13704b154faSFlorian Fainelli { .compatible = "brcm,bcm5301x-rng", .data = &nsp_rng_of_data }, 1388705f24fSFlorian Fainelli { .compatible = "brcm,bcm6368-rng"}, 139422a7491SYendapally Reddy Dhananjaya Reddy {}, 140422a7491SYendapally Reddy Dhananjaya Reddy }; 141422a7491SYendapally Reddy Dhananjaya Reddy 1428c4196a2SLubomir Rintel static int bcm2835_rng_probe(struct platform_device *pdev) 1438c4196a2SLubomir Rintel { 14404b154faSFlorian Fainelli const struct bcm2835_rng_of_data *of_data; 1458c4196a2SLubomir Rintel struct device *dev = &pdev->dev; 146422a7491SYendapally Reddy Dhananjaya Reddy const struct of_device_id *rng_id; 147b788479fSFlorian Fainelli struct bcm2835_rng_priv *priv; 1488c4196a2SLubomir Rintel int err; 1498c4196a2SLubomir Rintel 150b788479fSFlorian Fainelli priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 151b788479fSFlorian Fainelli if (!priv) 152b788479fSFlorian Fainelli return -ENOMEM; 153b788479fSFlorian Fainelli 154b788479fSFlorian Fainelli platform_set_drvdata(pdev, priv); 155b788479fSFlorian Fainelli 1568c4196a2SLubomir Rintel /* map peripheral */ 1573e46bd34SYueHaibing priv->base = devm_platform_ioremap_resource(pdev, 0); 158c4fc5d6dSweiyongjun \(A\) if (IS_ERR(priv->base)) 159b788479fSFlorian Fainelli return PTR_ERR(priv->base); 160b788479fSFlorian Fainelli 161791af4f4SFlorian Fainelli /* Clock is optional on most platforms */ 162e06da499STian Tao priv->clk = devm_clk_get_optional(dev, NULL); 163e06da499STian Tao if (IS_ERR(priv->clk)) 164e06da499STian Tao return PTR_ERR(priv->clk); 165791af4f4SFlorian Fainelli 166e5f9f41dSÁlvaro Fernández Rojas priv->reset = devm_reset_control_get_optional_exclusive(dev, NULL); 167e5f9f41dSÁlvaro Fernández Rojas if (IS_ERR(priv->reset)) 168e5f9f41dSÁlvaro Fernández Rojas return PTR_ERR(priv->reset); 169e5f9f41dSÁlvaro Fernández Rojas 1708705f24fSFlorian Fainelli priv->rng.name = pdev->name; 171a8157775SFlorian Fainelli priv->rng.init = bcm2835_rng_init; 172b788479fSFlorian Fainelli priv->rng.read = bcm2835_rng_read; 173ec94bca7SFlorian Fainelli priv->rng.cleanup = bcm2835_rng_cleanup; 1748c4196a2SLubomir Rintel 175984798deSJonas Gorski if (dev_of_node(dev)) { 17664f8153aSHerbert Xu rng_id = of_match_node(bcm2835_rng_of_match, dev->of_node); 17721bb0ef4SFlorian Fainelli if (!rng_id) 178422a7491SYendapally Reddy Dhananjaya Reddy return -EINVAL; 17921bb0ef4SFlorian Fainelli 180422a7491SYendapally Reddy Dhananjaya Reddy /* Check for rng init function, execute it */ 18104b154faSFlorian Fainelli of_data = rng_id->data; 18204b154faSFlorian Fainelli if (of_data) 18304b154faSFlorian Fainelli priv->mask_interrupts = of_data->mask_interrupts; 184984798deSJonas Gorski } 185422a7491SYendapally Reddy Dhananjaya Reddy 1868c4196a2SLubomir Rintel /* register driver */ 18716a4c04bSFlorian Fainelli err = devm_hwrng_register(dev, &priv->rng); 18821bb0ef4SFlorian Fainelli if (err) 1898c4196a2SLubomir Rintel dev_err(dev, "hwrng registration failed\n"); 19021bb0ef4SFlorian Fainelli else 1918c4196a2SLubomir Rintel dev_info(dev, "hwrng registered\n"); 1928c4196a2SLubomir Rintel 1938c4196a2SLubomir Rintel return err; 1948c4196a2SLubomir Rintel } 1958c4196a2SLubomir Rintel 1968c4196a2SLubomir Rintel MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match); 1978c4196a2SLubomir Rintel 198d4f6d923SRikard Falkeborn static const struct platform_device_id bcm2835_rng_devtype[] = { 1998705f24fSFlorian Fainelli { .name = "bcm2835-rng" }, 2008705f24fSFlorian Fainelli { .name = "bcm63xx-rng" }, 2018705f24fSFlorian Fainelli { /* sentinel */ } 2028705f24fSFlorian Fainelli }; 2038705f24fSFlorian Fainelli MODULE_DEVICE_TABLE(platform, bcm2835_rng_devtype); 2048705f24fSFlorian Fainelli 2058c4196a2SLubomir Rintel static struct platform_driver bcm2835_rng_driver = { 2068c4196a2SLubomir Rintel .driver = { 2078c4196a2SLubomir Rintel .name = "bcm2835-rng", 2088c4196a2SLubomir Rintel .of_match_table = bcm2835_rng_of_match, 2098c4196a2SLubomir Rintel }, 2108c4196a2SLubomir Rintel .probe = bcm2835_rng_probe, 2118705f24fSFlorian Fainelli .id_table = bcm2835_rng_devtype, 2128c4196a2SLubomir Rintel }; 2138c4196a2SLubomir Rintel module_platform_driver(bcm2835_rng_driver); 2148c4196a2SLubomir Rintel 2158c4196a2SLubomir Rintel MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>"); 2168c4196a2SLubomir Rintel MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver"); 2178c4196a2SLubomir Rintel MODULE_LICENSE("GPL v2"); 218