Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Ingenic Random Number Generator driver
0004  * Copyright (c) 2017 PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>
0005  * Copyright (c) 2020 周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>
0006  */
0007 
0008 #include <linux/err.h>
0009 #include <linux/kernel.h>
0010 #include <linux/hw_random.h>
0011 #include <linux/io.h>
0012 #include <linux/iopoll.h>
0013 #include <linux/module.h>
0014 #include <linux/of_device.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/slab.h>
0017 
0018 /* RNG register offsets */
0019 #define RNG_REG_ERNG_OFFSET     0x0
0020 #define RNG_REG_RNG_OFFSET      0x4
0021 
0022 /* bits within the ERND register */
0023 #define ERNG_READY              BIT(31)
0024 #define ERNG_ENABLE             BIT(0)
0025 
0026 enum ingenic_rng_version {
0027     ID_JZ4780,
0028     ID_X1000,
0029 };
0030 
0031 /* Device associated memory */
0032 struct ingenic_rng {
0033     enum ingenic_rng_version version;
0034 
0035     void __iomem *base;
0036     struct hwrng rng;
0037 };
0038 
0039 static int ingenic_rng_init(struct hwrng *rng)
0040 {
0041     struct ingenic_rng *priv = container_of(rng, struct ingenic_rng, rng);
0042 
0043     writel(ERNG_ENABLE, priv->base + RNG_REG_ERNG_OFFSET);
0044 
0045     return 0;
0046 }
0047 
0048 static void ingenic_rng_cleanup(struct hwrng *rng)
0049 {
0050     struct ingenic_rng *priv = container_of(rng, struct ingenic_rng, rng);
0051 
0052     writel(0, priv->base + RNG_REG_ERNG_OFFSET);
0053 }
0054 
0055 static int ingenic_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
0056 {
0057     struct ingenic_rng *priv = container_of(rng, struct ingenic_rng, rng);
0058     u32 *data = buf;
0059     u32 status;
0060     int ret;
0061 
0062     if (priv->version >= ID_X1000) {
0063         ret = readl_poll_timeout(priv->base + RNG_REG_ERNG_OFFSET, status,
0064                      status & ERNG_READY, 10, 1000);
0065         if (ret == -ETIMEDOUT) {
0066             pr_err("%s: Wait for RNG data ready timeout\n", __func__);
0067             return ret;
0068         }
0069     } else {
0070         /*
0071          * A delay is required so that the current RNG data is not bit shifted
0072          * version of previous RNG data which could happen if random data is
0073          * read continuously from this device.
0074          */
0075         udelay(20);
0076     }
0077 
0078     *data = readl(priv->base + RNG_REG_RNG_OFFSET);
0079 
0080     return 4;
0081 }
0082 
0083 static int ingenic_rng_probe(struct platform_device *pdev)
0084 {
0085     struct ingenic_rng *priv;
0086     int ret;
0087 
0088     priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
0089     if (!priv)
0090         return -ENOMEM;
0091 
0092     priv->base = devm_platform_ioremap_resource(pdev, 0);
0093     if (IS_ERR(priv->base)) {
0094         pr_err("%s: Failed to map RNG registers\n", __func__);
0095         return PTR_ERR(priv->base);
0096     }
0097 
0098     priv->version = (enum ingenic_rng_version)of_device_get_match_data(&pdev->dev);
0099 
0100     priv->rng.name = pdev->name;
0101     priv->rng.init = ingenic_rng_init;
0102     priv->rng.cleanup = ingenic_rng_cleanup;
0103     priv->rng.read = ingenic_rng_read;
0104 
0105     ret = hwrng_register(&priv->rng);
0106     if (ret) {
0107         dev_err(&pdev->dev, "Failed to register hwrng\n");
0108         return ret;
0109     }
0110 
0111     platform_set_drvdata(pdev, priv);
0112 
0113     dev_info(&pdev->dev, "Ingenic RNG driver registered\n");
0114     return 0;
0115 }
0116 
0117 static int ingenic_rng_remove(struct platform_device *pdev)
0118 {
0119     struct ingenic_rng *priv = platform_get_drvdata(pdev);
0120 
0121     hwrng_unregister(&priv->rng);
0122 
0123     writel(0, priv->base + RNG_REG_ERNG_OFFSET);
0124 
0125     return 0;
0126 }
0127 
0128 static const struct of_device_id ingenic_rng_of_match[] = {
0129     { .compatible = "ingenic,jz4780-rng", .data = (void *) ID_JZ4780 },
0130     { .compatible = "ingenic,x1000-rng", .data = (void *) ID_X1000 },
0131     { /* sentinel */ }
0132 };
0133 MODULE_DEVICE_TABLE(of, ingenic_rng_of_match);
0134 
0135 static struct platform_driver ingenic_rng_driver = {
0136     .probe      = ingenic_rng_probe,
0137     .remove     = ingenic_rng_remove,
0138     .driver     = {
0139         .name   = "ingenic-rng",
0140         .of_match_table = ingenic_rng_of_match,
0141     },
0142 };
0143 
0144 module_platform_driver(ingenic_rng_driver);
0145 
0146 MODULE_LICENSE("GPL");
0147 MODULE_AUTHOR("PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>");
0148 MODULE_AUTHOR("周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>");
0149 MODULE_DESCRIPTION("Ingenic Random Number Generator driver");