Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * PIC32 RNG driver
0004  *
0005  * Joshua Henderson <joshua.henderson@microchip.com>
0006  * Copyright (C) 2016 Microchip Technology Inc.  All rights reserved.
0007  */
0008 
0009 #include <linux/clk.h>
0010 #include <linux/clkdev.h>
0011 #include <linux/err.h>
0012 #include <linux/hw_random.h>
0013 #include <linux/io.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/of.h>
0017 #include <linux/of_device.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/slab.h>
0020 
0021 #define RNGCON      0x04
0022 #define  TRNGEN     BIT(8)
0023 #define  PRNGEN     BIT(9)
0024 #define  PRNGCONT   BIT(10)
0025 #define  TRNGMOD    BIT(11)
0026 #define  SEEDLOAD   BIT(12)
0027 #define RNGPOLY1    0x08
0028 #define RNGPOLY2    0x0C
0029 #define RNGNUMGEN1  0x10
0030 #define RNGNUMGEN2  0x14
0031 #define RNGSEED1    0x18
0032 #define RNGSEED2    0x1C
0033 #define RNGRCNT     0x20
0034 #define  RCNT_MASK  0x7F
0035 
0036 struct pic32_rng {
0037     void __iomem    *base;
0038     struct hwrng    rng;
0039     struct clk  *clk;
0040 };
0041 
0042 /*
0043  * The TRNG can generate up to 24Mbps. This is a timeout that should be safe
0044  * enough given the instructions in the loop and that the TRNG may not always
0045  * be at maximum rate.
0046  */
0047 #define RNG_TIMEOUT 500
0048 
0049 static int pic32_rng_read(struct hwrng *rng, void *buf, size_t max,
0050               bool wait)
0051 {
0052     struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
0053     u64 *data = buf;
0054     u32 t;
0055     unsigned int timeout = RNG_TIMEOUT;
0056 
0057     do {
0058         t = readl(priv->base + RNGRCNT) & RCNT_MASK;
0059         if (t == 64) {
0060             /* TRNG value comes through the seed registers */
0061             *data = ((u64)readl(priv->base + RNGSEED2) << 32) +
0062                 readl(priv->base + RNGSEED1);
0063             return 8;
0064         }
0065     } while (wait && --timeout);
0066 
0067     return -EIO;
0068 }
0069 
0070 static int pic32_rng_probe(struct platform_device *pdev)
0071 {
0072     struct pic32_rng *priv;
0073     u32 v;
0074     int ret;
0075 
0076     priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
0077     if (!priv)
0078         return -ENOMEM;
0079 
0080     priv->base = devm_platform_ioremap_resource(pdev, 0);
0081     if (IS_ERR(priv->base))
0082         return PTR_ERR(priv->base);
0083 
0084     priv->clk = devm_clk_get(&pdev->dev, NULL);
0085     if (IS_ERR(priv->clk))
0086         return PTR_ERR(priv->clk);
0087 
0088     ret = clk_prepare_enable(priv->clk);
0089     if (ret)
0090         return ret;
0091 
0092     /* enable TRNG in enhanced mode */
0093     v = TRNGEN | TRNGMOD;
0094     writel(v, priv->base + RNGCON);
0095 
0096     priv->rng.name = pdev->name;
0097     priv->rng.read = pic32_rng_read;
0098 
0099     ret = devm_hwrng_register(&pdev->dev, &priv->rng);
0100     if (ret)
0101         goto err_register;
0102 
0103     platform_set_drvdata(pdev, priv);
0104 
0105     return 0;
0106 
0107 err_register:
0108     clk_disable_unprepare(priv->clk);
0109     return ret;
0110 }
0111 
0112 static int pic32_rng_remove(struct platform_device *pdev)
0113 {
0114     struct pic32_rng *rng = platform_get_drvdata(pdev);
0115 
0116     writel(0, rng->base + RNGCON);
0117     clk_disable_unprepare(rng->clk);
0118     return 0;
0119 }
0120 
0121 static const struct of_device_id pic32_rng_of_match[] __maybe_unused = {
0122     { .compatible   = "microchip,pic32mzda-rng", },
0123     { /* sentinel */ }
0124 };
0125 MODULE_DEVICE_TABLE(of, pic32_rng_of_match);
0126 
0127 static struct platform_driver pic32_rng_driver = {
0128     .probe      = pic32_rng_probe,
0129     .remove     = pic32_rng_remove,
0130     .driver     = {
0131         .name   = "pic32-rng",
0132         .of_match_table = of_match_ptr(pic32_rng_of_match),
0133     },
0134 };
0135 
0136 module_platform_driver(pic32_rng_driver);
0137 
0138 MODULE_LICENSE("GPL");
0139 MODULE_AUTHOR("Joshua Henderson <joshua.henderson@microchip.com>");
0140 MODULE_DESCRIPTION("Microchip PIC32 RNG Driver");