Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2016 HiSilicon Co., Ltd.
0004  */
0005 
0006 #include <linux/err.h>
0007 #include <linux/kernel.h>
0008 #include <linux/hw_random.h>
0009 #include <linux/io.h>
0010 #include <linux/module.h>
0011 #include <linux/of.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/random.h>
0014 
0015 #define RNG_SEED    0x0
0016 #define RNG_CTRL    0x4
0017   #define RNG_SEED_SEL  BIT(2)
0018   #define RNG_RING_EN   BIT(1)
0019   #define RNG_EN    BIT(0)
0020 #define RNG_RAN_NUM 0x10
0021 #define RNG_PHY_SEED    0x14
0022 
0023 #define to_hisi_rng(p)  container_of(p, struct hisi_rng, rng)
0024 
0025 static int seed_sel;
0026 module_param(seed_sel, int, S_IRUGO);
0027 MODULE_PARM_DESC(seed_sel, "Auto reload seed. 0, use LFSR(default); 1, use ring oscillator.");
0028 
0029 struct hisi_rng {
0030     void __iomem *base;
0031     struct hwrng rng;
0032 };
0033 
0034 static int hisi_rng_init(struct hwrng *rng)
0035 {
0036     struct hisi_rng *hrng = to_hisi_rng(rng);
0037     int val = RNG_EN;
0038     u32 seed;
0039 
0040     /* get a random number as initial seed */
0041     get_random_bytes(&seed, sizeof(seed));
0042 
0043     writel_relaxed(seed, hrng->base + RNG_SEED);
0044 
0045     /**
0046      * The seed is reload periodically, there are two choice
0047      * of seeds, default seed using the value from LFSR, or
0048      * will use seed generated by ring oscillator.
0049      */
0050     if (seed_sel == 1)
0051         val |= RNG_RING_EN | RNG_SEED_SEL;
0052 
0053     writel_relaxed(val, hrng->base + RNG_CTRL);
0054     return 0;
0055 }
0056 
0057 static void hisi_rng_cleanup(struct hwrng *rng)
0058 {
0059     struct hisi_rng *hrng = to_hisi_rng(rng);
0060 
0061     writel_relaxed(0, hrng->base + RNG_CTRL);
0062 }
0063 
0064 static int hisi_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
0065 {
0066     struct hisi_rng *hrng = to_hisi_rng(rng);
0067     u32 *data = buf;
0068 
0069     *data = readl_relaxed(hrng->base + RNG_RAN_NUM);
0070     return 4;
0071 }
0072 
0073 static int hisi_rng_probe(struct platform_device *pdev)
0074 {
0075     struct hisi_rng *rng;
0076     int ret;
0077 
0078     rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
0079     if (!rng)
0080         return -ENOMEM;
0081 
0082     platform_set_drvdata(pdev, rng);
0083 
0084     rng->base = devm_platform_ioremap_resource(pdev, 0);
0085     if (IS_ERR(rng->base))
0086         return PTR_ERR(rng->base);
0087 
0088     rng->rng.name = pdev->name;
0089     rng->rng.init = hisi_rng_init;
0090     rng->rng.cleanup = hisi_rng_cleanup;
0091     rng->rng.read = hisi_rng_read;
0092 
0093     ret = devm_hwrng_register(&pdev->dev, &rng->rng);
0094     if (ret) {
0095         dev_err(&pdev->dev, "failed to register hwrng\n");
0096         return ret;
0097     }
0098 
0099     return 0;
0100 }
0101 
0102 static const struct of_device_id hisi_rng_dt_ids[] __maybe_unused = {
0103     { .compatible = "hisilicon,hip04-rng" },
0104     { .compatible = "hisilicon,hip05-rng" },
0105     { }
0106 };
0107 MODULE_DEVICE_TABLE(of, hisi_rng_dt_ids);
0108 
0109 static struct platform_driver hisi_rng_driver = {
0110     .probe      = hisi_rng_probe,
0111     .driver     = {
0112         .name   = "hisi-rng",
0113         .of_match_table = of_match_ptr(hisi_rng_dt_ids),
0114     },
0115 };
0116 
0117 module_platform_driver(hisi_rng_driver);
0118 
0119 MODULE_LICENSE("GPL");
0120 MODULE_AUTHOR("Kefeng Wang <wangkefeng.wang@huawei>");
0121 MODULE_DESCRIPTION("Hisilicon random number generator driver");