18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (C) 2020 Xiphera Ltd. */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/kernel.h>
58c2ecf20Sopenharmony_ci#include <linux/module.h>
68c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h>
78c2ecf20Sopenharmony_ci#include <linux/err.h>
88c2ecf20Sopenharmony_ci#include <linux/io.h>
98c2ecf20Sopenharmony_ci#include <linux/hw_random.h>
108c2ecf20Sopenharmony_ci#include <linux/of_device.h>
118c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
128c2ecf20Sopenharmony_ci#include <linux/delay.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define CONTROL_REG			0x00000000
158c2ecf20Sopenharmony_ci#define STATUS_REG			0x00000004
168c2ecf20Sopenharmony_ci#define RAND_REG			0x00000000
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define HOST_TO_TRNG_RESET		0x00000001
198c2ecf20Sopenharmony_ci#define HOST_TO_TRNG_RELEASE_RESET	0x00000002
208c2ecf20Sopenharmony_ci#define HOST_TO_TRNG_ENABLE		0x80000000
218c2ecf20Sopenharmony_ci#define HOST_TO_TRNG_ZEROIZE		0x80000004
228c2ecf20Sopenharmony_ci#define HOST_TO_TRNG_ACK_ZEROIZE	0x80000008
238c2ecf20Sopenharmony_ci#define HOST_TO_TRNG_READ		0x8000000F
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* trng statuses */
268c2ecf20Sopenharmony_ci#define TRNG_ACK_RESET			0x000000AC
278c2ecf20Sopenharmony_ci#define TRNG_SUCCESSFUL_STARTUP		0x00000057
288c2ecf20Sopenharmony_ci#define TRNG_FAILED_STARTUP		0x000000FA
298c2ecf20Sopenharmony_ci#define TRNG_NEW_RAND_AVAILABLE		0x000000ED
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistruct xiphera_trng {
328c2ecf20Sopenharmony_ci	void __iomem *mem;
338c2ecf20Sopenharmony_ci	struct hwrng rng;
348c2ecf20Sopenharmony_ci};
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic int xiphera_trng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	struct xiphera_trng *trng = container_of(rng, struct xiphera_trng, rng);
398c2ecf20Sopenharmony_ci	int ret = 0;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	while (max >= sizeof(u32)) {
428c2ecf20Sopenharmony_ci		/* check for data */
438c2ecf20Sopenharmony_ci		if (readl(trng->mem + STATUS_REG) == TRNG_NEW_RAND_AVAILABLE) {
448c2ecf20Sopenharmony_ci			*(u32 *)buf = readl(trng->mem + RAND_REG);
458c2ecf20Sopenharmony_ci			/*
468c2ecf20Sopenharmony_ci			 * Inform the trng of the read
478c2ecf20Sopenharmony_ci			 * and re-enable it to produce a new random number
488c2ecf20Sopenharmony_ci			 */
498c2ecf20Sopenharmony_ci			writel(HOST_TO_TRNG_READ, trng->mem + CONTROL_REG);
508c2ecf20Sopenharmony_ci			writel(HOST_TO_TRNG_ENABLE, trng->mem + CONTROL_REG);
518c2ecf20Sopenharmony_ci			ret += sizeof(u32);
528c2ecf20Sopenharmony_ci			buf += sizeof(u32);
538c2ecf20Sopenharmony_ci			max -= sizeof(u32);
548c2ecf20Sopenharmony_ci		} else {
558c2ecf20Sopenharmony_ci			break;
568c2ecf20Sopenharmony_ci		}
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci	return ret;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic int xiphera_trng_probe(struct platform_device *pdev)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	int ret;
648c2ecf20Sopenharmony_ci	struct xiphera_trng *trng;
658c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
668c2ecf20Sopenharmony_ci	struct resource *res;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	trng = devm_kzalloc(dev, sizeof(*trng), GFP_KERNEL);
698c2ecf20Sopenharmony_ci	if (!trng)
708c2ecf20Sopenharmony_ci		return -ENOMEM;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
738c2ecf20Sopenharmony_ci	trng->mem = devm_ioremap_resource(dev, res);
748c2ecf20Sopenharmony_ci	if (IS_ERR(trng->mem))
758c2ecf20Sopenharmony_ci		return PTR_ERR(trng->mem);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/*
788c2ecf20Sopenharmony_ci	 * the trng needs to be reset first which might not happen in time,
798c2ecf20Sopenharmony_ci	 * hence we incorporate a small delay to ensure proper behaviour
808c2ecf20Sopenharmony_ci	 */
818c2ecf20Sopenharmony_ci	writel(HOST_TO_TRNG_RESET, trng->mem + CONTROL_REG);
828c2ecf20Sopenharmony_ci	usleep_range(100, 200);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	if (readl(trng->mem + STATUS_REG) != TRNG_ACK_RESET) {
858c2ecf20Sopenharmony_ci		/*
868c2ecf20Sopenharmony_ci		 * there is a small chance the trng is just not ready yet,
878c2ecf20Sopenharmony_ci		 * so we try one more time. If the second time fails, we give up
888c2ecf20Sopenharmony_ci		 */
898c2ecf20Sopenharmony_ci		usleep_range(100, 200);
908c2ecf20Sopenharmony_ci		if (readl(trng->mem + STATUS_REG) != TRNG_ACK_RESET) {
918c2ecf20Sopenharmony_ci			dev_err(dev, "failed to reset the trng ip\n");
928c2ecf20Sopenharmony_ci			return -ENODEV;
938c2ecf20Sopenharmony_ci		}
948c2ecf20Sopenharmony_ci	}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/*
978c2ecf20Sopenharmony_ci	 * once again, to ensure proper behaviour we sleep
988c2ecf20Sopenharmony_ci	 * for a while after zeroizing the trng
998c2ecf20Sopenharmony_ci	 */
1008c2ecf20Sopenharmony_ci	writel(HOST_TO_TRNG_RELEASE_RESET, trng->mem + CONTROL_REG);
1018c2ecf20Sopenharmony_ci	writel(HOST_TO_TRNG_ENABLE, trng->mem + CONTROL_REG);
1028c2ecf20Sopenharmony_ci	writel(HOST_TO_TRNG_ZEROIZE, trng->mem + CONTROL_REG);
1038c2ecf20Sopenharmony_ci	msleep(20);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	if (readl(trng->mem + STATUS_REG) != TRNG_SUCCESSFUL_STARTUP) {
1068c2ecf20Sopenharmony_ci		/* diagnose the reason for the failure */
1078c2ecf20Sopenharmony_ci		if (readl(trng->mem + STATUS_REG) == TRNG_FAILED_STARTUP) {
1088c2ecf20Sopenharmony_ci			dev_err(dev, "trng ip startup-tests failed\n");
1098c2ecf20Sopenharmony_ci			return -ENODEV;
1108c2ecf20Sopenharmony_ci		}
1118c2ecf20Sopenharmony_ci		dev_err(dev, "startup-tests yielded no response\n");
1128c2ecf20Sopenharmony_ci		return -ENODEV;
1138c2ecf20Sopenharmony_ci	}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	writel(HOST_TO_TRNG_ACK_ZEROIZE, trng->mem + CONTROL_REG);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	trng->rng.name = pdev->name;
1188c2ecf20Sopenharmony_ci	trng->rng.read = xiphera_trng_read;
1198c2ecf20Sopenharmony_ci	trng->rng.quality = 900;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	ret = devm_hwrng_register(dev, &trng->rng);
1228c2ecf20Sopenharmony_ci	if (ret) {
1238c2ecf20Sopenharmony_ci		dev_err(dev, "failed to register rng device: %d\n", ret);
1248c2ecf20Sopenharmony_ci		return ret;
1258c2ecf20Sopenharmony_ci	}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, trng);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	return 0;
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic const struct of_device_id xiphera_trng_of_match[] = {
1338c2ecf20Sopenharmony_ci	{ .compatible = "xiphera,xip8001b-trng", },
1348c2ecf20Sopenharmony_ci	{},
1358c2ecf20Sopenharmony_ci};
1368c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, xiphera_trng_of_match);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic struct platform_driver xiphera_trng_driver = {
1398c2ecf20Sopenharmony_ci	.driver = {
1408c2ecf20Sopenharmony_ci		.name = "xiphera-trng",
1418c2ecf20Sopenharmony_ci		.of_match_table	= xiphera_trng_of_match,
1428c2ecf20Sopenharmony_ci	},
1438c2ecf20Sopenharmony_ci	.probe = xiphera_trng_probe,
1448c2ecf20Sopenharmony_ci};
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cimodule_platform_driver(xiphera_trng_driver);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1498c2ecf20Sopenharmony_ciMODULE_AUTHOR("Atte Tommiska");
1508c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Xiphera FPGA-based true random number generator driver");
151