1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2afc39d6eSKefeng Wang /*
3afc39d6eSKefeng Wang * Copyright (C) 2016 HiSilicon Co., Ltd.
4afc39d6eSKefeng Wang */
5afc39d6eSKefeng Wang
6afc39d6eSKefeng Wang #include <linux/err.h>
7afc39d6eSKefeng Wang #include <linux/kernel.h>
8afc39d6eSKefeng Wang #include <linux/hw_random.h>
9afc39d6eSKefeng Wang #include <linux/io.h>
10afc39d6eSKefeng Wang #include <linux/module.h>
11afc39d6eSKefeng Wang #include <linux/of.h>
12afc39d6eSKefeng Wang #include <linux/platform_device.h>
13afc39d6eSKefeng Wang #include <linux/random.h>
14afc39d6eSKefeng Wang
15afc39d6eSKefeng Wang #define RNG_SEED 0x0
16afc39d6eSKefeng Wang #define RNG_CTRL 0x4
17afc39d6eSKefeng Wang #define RNG_SEED_SEL BIT(2)
18afc39d6eSKefeng Wang #define RNG_RING_EN BIT(1)
19afc39d6eSKefeng Wang #define RNG_EN BIT(0)
20afc39d6eSKefeng Wang #define RNG_RAN_NUM 0x10
21afc39d6eSKefeng Wang #define RNG_PHY_SEED 0x14
22afc39d6eSKefeng Wang
23afc39d6eSKefeng Wang #define to_hisi_rng(p) container_of(p, struct hisi_rng, rng)
24afc39d6eSKefeng Wang
25afc39d6eSKefeng Wang static int seed_sel;
26afc39d6eSKefeng Wang module_param(seed_sel, int, S_IRUGO);
27afc39d6eSKefeng Wang MODULE_PARM_DESC(seed_sel, "Auto reload seed. 0, use LFSR(default); 1, use ring oscillator.");
28afc39d6eSKefeng Wang
29afc39d6eSKefeng Wang struct hisi_rng {
30afc39d6eSKefeng Wang void __iomem *base;
31afc39d6eSKefeng Wang struct hwrng rng;
32afc39d6eSKefeng Wang };
33afc39d6eSKefeng Wang
hisi_rng_init(struct hwrng * rng)34afc39d6eSKefeng Wang static int hisi_rng_init(struct hwrng *rng)
35afc39d6eSKefeng Wang {
36afc39d6eSKefeng Wang struct hisi_rng *hrng = to_hisi_rng(rng);
37afc39d6eSKefeng Wang int val = RNG_EN;
38afc39d6eSKefeng Wang u32 seed;
39afc39d6eSKefeng Wang
40afc39d6eSKefeng Wang /* get a random number as initial seed */
41afc39d6eSKefeng Wang get_random_bytes(&seed, sizeof(seed));
42afc39d6eSKefeng Wang
43afc39d6eSKefeng Wang writel_relaxed(seed, hrng->base + RNG_SEED);
44afc39d6eSKefeng Wang
45afc39d6eSKefeng Wang /**
46afc39d6eSKefeng Wang * The seed is reload periodically, there are two choice
47afc39d6eSKefeng Wang * of seeds, default seed using the value from LFSR, or
48afc39d6eSKefeng Wang * will use seed generated by ring oscillator.
49afc39d6eSKefeng Wang */
50afc39d6eSKefeng Wang if (seed_sel == 1)
51afc39d6eSKefeng Wang val |= RNG_RING_EN | RNG_SEED_SEL;
52afc39d6eSKefeng Wang
53afc39d6eSKefeng Wang writel_relaxed(val, hrng->base + RNG_CTRL);
54afc39d6eSKefeng Wang return 0;
55afc39d6eSKefeng Wang }
56afc39d6eSKefeng Wang
hisi_rng_cleanup(struct hwrng * rng)57afc39d6eSKefeng Wang static void hisi_rng_cleanup(struct hwrng *rng)
58afc39d6eSKefeng Wang {
59afc39d6eSKefeng Wang struct hisi_rng *hrng = to_hisi_rng(rng);
60afc39d6eSKefeng Wang
61afc39d6eSKefeng Wang writel_relaxed(0, hrng->base + RNG_CTRL);
62afc39d6eSKefeng Wang }
63afc39d6eSKefeng Wang
hisi_rng_read(struct hwrng * rng,void * buf,size_t max,bool wait)64afc39d6eSKefeng Wang static int hisi_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
65afc39d6eSKefeng Wang {
66afc39d6eSKefeng Wang struct hisi_rng *hrng = to_hisi_rng(rng);
67afc39d6eSKefeng Wang u32 *data = buf;
68afc39d6eSKefeng Wang
69afc39d6eSKefeng Wang *data = readl_relaxed(hrng->base + RNG_RAN_NUM);
70afc39d6eSKefeng Wang return 4;
71afc39d6eSKefeng Wang }
72afc39d6eSKefeng Wang
hisi_rng_probe(struct platform_device * pdev)73afc39d6eSKefeng Wang static int hisi_rng_probe(struct platform_device *pdev)
74afc39d6eSKefeng Wang {
75afc39d6eSKefeng Wang struct hisi_rng *rng;
76afc39d6eSKefeng Wang int ret;
77afc39d6eSKefeng Wang
78afc39d6eSKefeng Wang rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
79afc39d6eSKefeng Wang if (!rng)
80afc39d6eSKefeng Wang return -ENOMEM;
81afc39d6eSKefeng Wang
82afc39d6eSKefeng Wang platform_set_drvdata(pdev, rng);
83afc39d6eSKefeng Wang
8410304c76SYueHaibing rng->base = devm_platform_ioremap_resource(pdev, 0);
85afc39d6eSKefeng Wang if (IS_ERR(rng->base))
86afc39d6eSKefeng Wang return PTR_ERR(rng->base);
87afc39d6eSKefeng Wang
88afc39d6eSKefeng Wang rng->rng.name = pdev->name;
89afc39d6eSKefeng Wang rng->rng.init = hisi_rng_init;
90afc39d6eSKefeng Wang rng->rng.cleanup = hisi_rng_cleanup;
91afc39d6eSKefeng Wang rng->rng.read = hisi_rng_read;
92afc39d6eSKefeng Wang
93afc39d6eSKefeng Wang ret = devm_hwrng_register(&pdev->dev, &rng->rng);
94afc39d6eSKefeng Wang if (ret) {
95afc39d6eSKefeng Wang dev_err(&pdev->dev, "failed to register hwrng\n");
96afc39d6eSKefeng Wang return ret;
97afc39d6eSKefeng Wang }
98afc39d6eSKefeng Wang
99afc39d6eSKefeng Wang return 0;
100afc39d6eSKefeng Wang }
101afc39d6eSKefeng Wang
102*eb515c60SHerbert Xu static const struct of_device_id hisi_rng_dt_ids[] __maybe_unused = {
103afc39d6eSKefeng Wang { .compatible = "hisilicon,hip04-rng" },
104afc39d6eSKefeng Wang { .compatible = "hisilicon,hip05-rng" },
105afc39d6eSKefeng Wang { }
106afc39d6eSKefeng Wang };
107afc39d6eSKefeng Wang MODULE_DEVICE_TABLE(of, hisi_rng_dt_ids);
108afc39d6eSKefeng Wang
109afc39d6eSKefeng Wang static struct platform_driver hisi_rng_driver = {
110afc39d6eSKefeng Wang .probe = hisi_rng_probe,
111afc39d6eSKefeng Wang .driver = {
112afc39d6eSKefeng Wang .name = "hisi-rng",
113afc39d6eSKefeng Wang .of_match_table = of_match_ptr(hisi_rng_dt_ids),
114afc39d6eSKefeng Wang },
115afc39d6eSKefeng Wang };
116afc39d6eSKefeng Wang
117afc39d6eSKefeng Wang module_platform_driver(hisi_rng_driver);
118afc39d6eSKefeng Wang
119afc39d6eSKefeng Wang MODULE_LICENSE("GPL");
120afc39d6eSKefeng Wang MODULE_AUTHOR("Kefeng Wang <wangkefeng.wang@huawei>");
121afc39d6eSKefeng Wang MODULE_DESCRIPTION("Hisilicon random number generator driver");
122