18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Ingenic True Random Number Generator driver 48c2ecf20Sopenharmony_ci * Copyright (c) 2019 漆鹏振 (Qi Pengzhen) <aric.pzqi@ingenic.com> 58c2ecf20Sopenharmony_ci * Copyright (c) 2020 周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/clk.h> 98c2ecf20Sopenharmony_ci#include <linux/err.h> 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/hw_random.h> 128c2ecf20Sopenharmony_ci#include <linux/io.h> 138c2ecf20Sopenharmony_ci#include <linux/iopoll.h> 148c2ecf20Sopenharmony_ci#include <linux/module.h> 158c2ecf20Sopenharmony_ci#include <linux/of_device.h> 168c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 178c2ecf20Sopenharmony_ci#include <linux/slab.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci/* DTRNG register offsets */ 208c2ecf20Sopenharmony_ci#define TRNG_REG_CFG_OFFSET 0x00 218c2ecf20Sopenharmony_ci#define TRNG_REG_RANDOMNUM_OFFSET 0x04 228c2ecf20Sopenharmony_ci#define TRNG_REG_STATUS_OFFSET 0x08 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci/* bits within the CFG register */ 258c2ecf20Sopenharmony_ci#define CFG_RDY_CLR BIT(12) 268c2ecf20Sopenharmony_ci#define CFG_INT_MASK BIT(11) 278c2ecf20Sopenharmony_ci#define CFG_GEN_EN BIT(0) 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci/* bits within the STATUS register */ 308c2ecf20Sopenharmony_ci#define STATUS_RANDOM_RDY BIT(0) 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_cistruct ingenic_trng { 338c2ecf20Sopenharmony_ci void __iomem *base; 348c2ecf20Sopenharmony_ci struct clk *clk; 358c2ecf20Sopenharmony_ci struct hwrng rng; 368c2ecf20Sopenharmony_ci}; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistatic int ingenic_trng_init(struct hwrng *rng) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci struct ingenic_trng *trng = container_of(rng, struct ingenic_trng, rng); 418c2ecf20Sopenharmony_ci unsigned int ctrl; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci ctrl = readl(trng->base + TRNG_REG_CFG_OFFSET); 448c2ecf20Sopenharmony_ci ctrl |= CFG_GEN_EN; 458c2ecf20Sopenharmony_ci writel(ctrl, trng->base + TRNG_REG_CFG_OFFSET); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci return 0; 488c2ecf20Sopenharmony_ci} 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_cistatic void ingenic_trng_cleanup(struct hwrng *rng) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci struct ingenic_trng *trng = container_of(rng, struct ingenic_trng, rng); 538c2ecf20Sopenharmony_ci unsigned int ctrl; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci ctrl = readl(trng->base + TRNG_REG_CFG_OFFSET); 568c2ecf20Sopenharmony_ci ctrl &= ~CFG_GEN_EN; 578c2ecf20Sopenharmony_ci writel(ctrl, trng->base + TRNG_REG_CFG_OFFSET); 588c2ecf20Sopenharmony_ci} 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_cistatic int ingenic_trng_read(struct hwrng *rng, void *buf, size_t max, bool wait) 618c2ecf20Sopenharmony_ci{ 628c2ecf20Sopenharmony_ci struct ingenic_trng *trng = container_of(rng, struct ingenic_trng, rng); 638c2ecf20Sopenharmony_ci u32 *data = buf; 648c2ecf20Sopenharmony_ci u32 status; 658c2ecf20Sopenharmony_ci int ret; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci ret = readl_poll_timeout(trng->base + TRNG_REG_STATUS_OFFSET, status, 688c2ecf20Sopenharmony_ci status & STATUS_RANDOM_RDY, 10, 1000); 698c2ecf20Sopenharmony_ci if (ret == -ETIMEDOUT) { 708c2ecf20Sopenharmony_ci pr_err("%s: Wait for DTRNG data ready timeout\n", __func__); 718c2ecf20Sopenharmony_ci return ret; 728c2ecf20Sopenharmony_ci } 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci *data = readl(trng->base + TRNG_REG_RANDOMNUM_OFFSET); 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci return 4; 778c2ecf20Sopenharmony_ci} 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_cistatic int ingenic_trng_probe(struct platform_device *pdev) 808c2ecf20Sopenharmony_ci{ 818c2ecf20Sopenharmony_ci struct ingenic_trng *trng; 828c2ecf20Sopenharmony_ci int ret; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL); 858c2ecf20Sopenharmony_ci if (!trng) 868c2ecf20Sopenharmony_ci return -ENOMEM; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci trng->base = devm_platform_ioremap_resource(pdev, 0); 898c2ecf20Sopenharmony_ci if (IS_ERR(trng->base)) { 908c2ecf20Sopenharmony_ci pr_err("%s: Failed to map DTRNG registers\n", __func__); 918c2ecf20Sopenharmony_ci ret = PTR_ERR(trng->base); 928c2ecf20Sopenharmony_ci return PTR_ERR(trng->base); 938c2ecf20Sopenharmony_ci } 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci trng->clk = devm_clk_get(&pdev->dev, NULL); 968c2ecf20Sopenharmony_ci if (IS_ERR(trng->clk)) { 978c2ecf20Sopenharmony_ci ret = PTR_ERR(trng->clk); 988c2ecf20Sopenharmony_ci pr_crit("%s: Cannot get DTRNG clock\n", __func__); 998c2ecf20Sopenharmony_ci return PTR_ERR(trng->clk); 1008c2ecf20Sopenharmony_ci } 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci ret = clk_prepare_enable(trng->clk); 1038c2ecf20Sopenharmony_ci if (ret) { 1048c2ecf20Sopenharmony_ci pr_crit("%s: Unable to enable DTRNG clock\n", __func__); 1058c2ecf20Sopenharmony_ci return ret; 1068c2ecf20Sopenharmony_ci } 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci trng->rng.name = pdev->name; 1098c2ecf20Sopenharmony_ci trng->rng.init = ingenic_trng_init; 1108c2ecf20Sopenharmony_ci trng->rng.cleanup = ingenic_trng_cleanup; 1118c2ecf20Sopenharmony_ci trng->rng.read = ingenic_trng_read; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci ret = hwrng_register(&trng->rng); 1148c2ecf20Sopenharmony_ci if (ret) { 1158c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Failed to register hwrng\n"); 1168c2ecf20Sopenharmony_ci goto err_unprepare_clk; 1178c2ecf20Sopenharmony_ci } 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, trng); 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci dev_info(&pdev->dev, "Ingenic DTRNG driver registered\n"); 1228c2ecf20Sopenharmony_ci return 0; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cierr_unprepare_clk: 1258c2ecf20Sopenharmony_ci clk_disable_unprepare(trng->clk); 1268c2ecf20Sopenharmony_ci return ret; 1278c2ecf20Sopenharmony_ci} 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_cistatic int ingenic_trng_remove(struct platform_device *pdev) 1308c2ecf20Sopenharmony_ci{ 1318c2ecf20Sopenharmony_ci struct ingenic_trng *trng = platform_get_drvdata(pdev); 1328c2ecf20Sopenharmony_ci unsigned int ctrl; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci hwrng_unregister(&trng->rng); 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci ctrl = readl(trng->base + TRNG_REG_CFG_OFFSET); 1378c2ecf20Sopenharmony_ci ctrl &= ~CFG_GEN_EN; 1388c2ecf20Sopenharmony_ci writel(ctrl, trng->base + TRNG_REG_CFG_OFFSET); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci clk_disable_unprepare(trng->clk); 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci return 0; 1438c2ecf20Sopenharmony_ci} 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_cistatic const struct of_device_id ingenic_trng_of_match[] = { 1468c2ecf20Sopenharmony_ci { .compatible = "ingenic,x1830-dtrng" }, 1478c2ecf20Sopenharmony_ci { /* sentinel */ } 1488c2ecf20Sopenharmony_ci}; 1498c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, ingenic_trng_of_match); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_cistatic struct platform_driver ingenic_trng_driver = { 1528c2ecf20Sopenharmony_ci .probe = ingenic_trng_probe, 1538c2ecf20Sopenharmony_ci .remove = ingenic_trng_remove, 1548c2ecf20Sopenharmony_ci .driver = { 1558c2ecf20Sopenharmony_ci .name = "ingenic-trng", 1568c2ecf20Sopenharmony_ci .of_match_table = ingenic_trng_of_match, 1578c2ecf20Sopenharmony_ci }, 1588c2ecf20Sopenharmony_ci}; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_cimodule_platform_driver(ingenic_trng_driver); 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 1638c2ecf20Sopenharmony_ciMODULE_AUTHOR("漆鹏振 (Qi Pengzhen) <aric.pzqi@ingenic.com>"); 1648c2ecf20Sopenharmony_ciMODULE_AUTHOR("周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>"); 1658c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Ingenic True Random Number Generator driver"); 166