18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (c) 2019 HiSilicon Limited. */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/acpi.h>
58c2ecf20Sopenharmony_ci#include <linux/err.h>
68c2ecf20Sopenharmony_ci#include <linux/hw_random.h>
78c2ecf20Sopenharmony_ci#include <linux/io.h>
88c2ecf20Sopenharmony_ci#include <linux/iopoll.h>
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
128c2ecf20Sopenharmony_ci#include <linux/random.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define HISI_TRNG_REG		0x00F0
158c2ecf20Sopenharmony_ci#define HISI_TRNG_BYTES		4
168c2ecf20Sopenharmony_ci#define HISI_TRNG_QUALITY	512
178c2ecf20Sopenharmony_ci#define SLEEP_US		10
188c2ecf20Sopenharmony_ci#define TIMEOUT_US		10000
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistruct hisi_trng {
218c2ecf20Sopenharmony_ci	void __iomem *base;
228c2ecf20Sopenharmony_ci	struct hwrng rng;
238c2ecf20Sopenharmony_ci};
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic int hisi_trng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	struct hisi_trng *trng;
288c2ecf20Sopenharmony_ci	int currsize = 0;
298c2ecf20Sopenharmony_ci	u32 val = 0;
308c2ecf20Sopenharmony_ci	u32 ret;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	trng = container_of(rng, struct hisi_trng, rng);
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	do {
358c2ecf20Sopenharmony_ci		ret = readl_poll_timeout(trng->base + HISI_TRNG_REG, val,
368c2ecf20Sopenharmony_ci					 val, SLEEP_US, TIMEOUT_US);
378c2ecf20Sopenharmony_ci		if (ret)
388c2ecf20Sopenharmony_ci			return currsize;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci		if (max - currsize >= HISI_TRNG_BYTES) {
418c2ecf20Sopenharmony_ci			memcpy(buf + currsize, &val, HISI_TRNG_BYTES);
428c2ecf20Sopenharmony_ci			currsize += HISI_TRNG_BYTES;
438c2ecf20Sopenharmony_ci			if (currsize == max)
448c2ecf20Sopenharmony_ci				return currsize;
458c2ecf20Sopenharmony_ci			continue;
468c2ecf20Sopenharmony_ci		}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci		/* copy remaining bytes */
498c2ecf20Sopenharmony_ci		memcpy(buf + currsize, &val, max - currsize);
508c2ecf20Sopenharmony_ci		currsize = max;
518c2ecf20Sopenharmony_ci	} while (currsize < max);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	return currsize;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic int hisi_trng_probe(struct platform_device *pdev)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	struct hisi_trng *trng;
598c2ecf20Sopenharmony_ci	int ret;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL);
628c2ecf20Sopenharmony_ci	if (!trng)
638c2ecf20Sopenharmony_ci		return -ENOMEM;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	trng->base = devm_platform_ioremap_resource(pdev, 0);
668c2ecf20Sopenharmony_ci	if (IS_ERR(trng->base))
678c2ecf20Sopenharmony_ci		return PTR_ERR(trng->base);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	trng->rng.name = pdev->name;
708c2ecf20Sopenharmony_ci	trng->rng.read = hisi_trng_read;
718c2ecf20Sopenharmony_ci	trng->rng.quality = HISI_TRNG_QUALITY;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	ret = devm_hwrng_register(&pdev->dev, &trng->rng);
748c2ecf20Sopenharmony_ci	if (ret)
758c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to register hwrng!\n");
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	return ret;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic const struct acpi_device_id hisi_trng_acpi_match[] = {
818c2ecf20Sopenharmony_ci	{ "HISI02B3", 0 },
828c2ecf20Sopenharmony_ci	{ }
838c2ecf20Sopenharmony_ci};
848c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, hisi_trng_acpi_match);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic struct platform_driver hisi_trng_driver = {
878c2ecf20Sopenharmony_ci	.probe		= hisi_trng_probe,
888c2ecf20Sopenharmony_ci	.driver		= {
898c2ecf20Sopenharmony_ci		.name	= "hisi-trng-v2",
908c2ecf20Sopenharmony_ci		.acpi_match_table = ACPI_PTR(hisi_trng_acpi_match),
918c2ecf20Sopenharmony_ci	},
928c2ecf20Sopenharmony_ci};
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cimodule_platform_driver(hisi_trng_driver);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
978c2ecf20Sopenharmony_ciMODULE_AUTHOR("Weili Qian <qianweili@huawei.com>");
988c2ecf20Sopenharmony_ciMODULE_AUTHOR("Zaibo Xu <xuzaibo@huawei.com>");
998c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("HiSilicon true random number generator V2 driver");
100