18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2016 HiSilicon Co., Ltd.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/err.h>
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/hw_random.h>
98c2ecf20Sopenharmony_ci#include <linux/io.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/of.h>
128c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
138c2ecf20Sopenharmony_ci#include <linux/random.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define RNG_SEED	0x0
168c2ecf20Sopenharmony_ci#define RNG_CTRL	0x4
178c2ecf20Sopenharmony_ci  #define RNG_SEED_SEL	BIT(2)
188c2ecf20Sopenharmony_ci  #define RNG_RING_EN	BIT(1)
198c2ecf20Sopenharmony_ci  #define RNG_EN	BIT(0)
208c2ecf20Sopenharmony_ci#define RNG_RAN_NUM	0x10
218c2ecf20Sopenharmony_ci#define RNG_PHY_SEED	0x14
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define to_hisi_rng(p)	container_of(p, struct hisi_rng, rng)
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic int seed_sel;
268c2ecf20Sopenharmony_cimodule_param(seed_sel, int, S_IRUGO);
278c2ecf20Sopenharmony_ciMODULE_PARM_DESC(seed_sel, "Auto reload seed. 0, use LFSR(default); 1, use ring oscillator.");
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistruct hisi_rng {
308c2ecf20Sopenharmony_ci	void __iomem *base;
318c2ecf20Sopenharmony_ci	struct hwrng rng;
328c2ecf20Sopenharmony_ci};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic int hisi_rng_init(struct hwrng *rng)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	struct hisi_rng *hrng = to_hisi_rng(rng);
378c2ecf20Sopenharmony_ci	int val = RNG_EN;
388c2ecf20Sopenharmony_ci	u32 seed;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	/* get a random number as initial seed */
418c2ecf20Sopenharmony_ci	get_random_bytes(&seed, sizeof(seed));
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	writel_relaxed(seed, hrng->base + RNG_SEED);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	/**
468c2ecf20Sopenharmony_ci	 * The seed is reload periodically, there are two choice
478c2ecf20Sopenharmony_ci	 * of seeds, default seed using the value from LFSR, or
488c2ecf20Sopenharmony_ci	 * will use seed generated by ring oscillator.
498c2ecf20Sopenharmony_ci	 */
508c2ecf20Sopenharmony_ci	if (seed_sel == 1)
518c2ecf20Sopenharmony_ci		val |= RNG_RING_EN | RNG_SEED_SEL;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	writel_relaxed(val, hrng->base + RNG_CTRL);
548c2ecf20Sopenharmony_ci	return 0;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic void hisi_rng_cleanup(struct hwrng *rng)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct hisi_rng *hrng = to_hisi_rng(rng);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	writel_relaxed(0, hrng->base + RNG_CTRL);
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic int hisi_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	struct hisi_rng *hrng = to_hisi_rng(rng);
678c2ecf20Sopenharmony_ci	u32 *data = buf;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	*data = readl_relaxed(hrng->base + RNG_RAN_NUM);
708c2ecf20Sopenharmony_ci	return 4;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic int hisi_rng_probe(struct platform_device *pdev)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	struct hisi_rng *rng;
768c2ecf20Sopenharmony_ci	int ret;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
798c2ecf20Sopenharmony_ci	if (!rng)
808c2ecf20Sopenharmony_ci		return -ENOMEM;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, rng);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	rng->base = devm_platform_ioremap_resource(pdev, 0);
858c2ecf20Sopenharmony_ci	if (IS_ERR(rng->base))
868c2ecf20Sopenharmony_ci		return PTR_ERR(rng->base);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	rng->rng.name = pdev->name;
898c2ecf20Sopenharmony_ci	rng->rng.init = hisi_rng_init;
908c2ecf20Sopenharmony_ci	rng->rng.cleanup = hisi_rng_cleanup;
918c2ecf20Sopenharmony_ci	rng->rng.read = hisi_rng_read;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	ret = devm_hwrng_register(&pdev->dev, &rng->rng);
948c2ecf20Sopenharmony_ci	if (ret) {
958c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to register hwrng\n");
968c2ecf20Sopenharmony_ci		return ret;
978c2ecf20Sopenharmony_ci	}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	return 0;
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic const struct of_device_id hisi_rng_dt_ids[] __maybe_unused = {
1038c2ecf20Sopenharmony_ci	{ .compatible = "hisilicon,hip04-rng" },
1048c2ecf20Sopenharmony_ci	{ .compatible = "hisilicon,hip05-rng" },
1058c2ecf20Sopenharmony_ci	{ }
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, hisi_rng_dt_ids);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic struct platform_driver hisi_rng_driver = {
1108c2ecf20Sopenharmony_ci	.probe		= hisi_rng_probe,
1118c2ecf20Sopenharmony_ci	.driver		= {
1128c2ecf20Sopenharmony_ci		.name	= "hisi-rng",
1138c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(hisi_rng_dt_ids),
1148c2ecf20Sopenharmony_ci	},
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cimodule_platform_driver(hisi_rng_driver);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1208c2ecf20Sopenharmony_ciMODULE_AUTHOR("Kefeng Wang <wangkefeng.wang@huawei>");
1218c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Hisilicon random number generator driver");
122