linux/drivers/char/hw_random/hisi-rng.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (C) 2016 HiSilicon Co., Ltd.
   4 */
   5
   6#include <linux/err.h>
   7#include <linux/kernel.h>
   8#include <linux/hw_random.h>
   9#include <linux/io.h>
  10#include <linux/module.h>
  11#include <linux/of.h>
  12#include <linux/platform_device.h>
  13#include <linux/random.h>
  14
  15#define RNG_SEED        0x0
  16#define RNG_CTRL        0x4
  17  #define RNG_SEED_SEL  BIT(2)
  18  #define RNG_RING_EN   BIT(1)
  19  #define RNG_EN        BIT(0)
  20#define RNG_RAN_NUM     0x10
  21#define RNG_PHY_SEED    0x14
  22
  23#define to_hisi_rng(p)  container_of(p, struct hisi_rng, rng)
  24
  25static int seed_sel;
  26module_param(seed_sel, int, S_IRUGO);
  27MODULE_PARM_DESC(seed_sel, "Auto reload seed. 0, use LFSR(default); 1, use ring oscillator.");
  28
  29struct hisi_rng {
  30        void __iomem *base;
  31        struct hwrng rng;
  32};
  33
  34static int hisi_rng_init(struct hwrng *rng)
  35{
  36        struct hisi_rng *hrng = to_hisi_rng(rng);
  37        int val = RNG_EN;
  38        u32 seed;
  39
  40        /* get a random number as initial seed */
  41        get_random_bytes(&seed, sizeof(seed));
  42
  43        writel_relaxed(seed, hrng->base + RNG_SEED);
  44
  45        /**
  46         * The seed is reload periodically, there are two choice
  47         * of seeds, default seed using the value from LFSR, or
  48         * will use seed generated by ring oscillator.
  49         */
  50        if (seed_sel == 1)
  51                val |= RNG_RING_EN | RNG_SEED_SEL;
  52
  53        writel_relaxed(val, hrng->base + RNG_CTRL);
  54        return 0;
  55}
  56
  57static void hisi_rng_cleanup(struct hwrng *rng)
  58{
  59        struct hisi_rng *hrng = to_hisi_rng(rng);
  60
  61        writel_relaxed(0, hrng->base + RNG_CTRL);
  62}
  63
  64static int hisi_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
  65{
  66        struct hisi_rng *hrng = to_hisi_rng(rng);
  67        u32 *data = buf;
  68
  69        *data = readl_relaxed(hrng->base + RNG_RAN_NUM);
  70        return 4;
  71}
  72
  73static int hisi_rng_probe(struct platform_device *pdev)
  74{
  75        struct hisi_rng *rng;
  76        struct resource *res;
  77        int ret;
  78
  79        rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
  80        if (!rng)
  81                return -ENOMEM;
  82
  83        platform_set_drvdata(pdev, rng);
  84
  85        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  86        rng->base = devm_ioremap_resource(&pdev->dev, res);
  87        if (IS_ERR(rng->base))
  88                return PTR_ERR(rng->base);
  89
  90        rng->rng.name = pdev->name;
  91        rng->rng.init = hisi_rng_init;
  92        rng->rng.cleanup = hisi_rng_cleanup;
  93        rng->rng.read = hisi_rng_read;
  94
  95        ret = devm_hwrng_register(&pdev->dev, &rng->rng);
  96        if (ret) {
  97                dev_err(&pdev->dev, "failed to register hwrng\n");
  98                return ret;
  99        }
 100
 101        return 0;
 102}
 103
 104static const struct of_device_id hisi_rng_dt_ids[] = {
 105        { .compatible = "hisilicon,hip04-rng" },
 106        { .compatible = "hisilicon,hip05-rng" },
 107        { }
 108};
 109MODULE_DEVICE_TABLE(of, hisi_rng_dt_ids);
 110
 111static struct platform_driver hisi_rng_driver = {
 112        .probe          = hisi_rng_probe,
 113        .driver         = {
 114                .name   = "hisi-rng",
 115                .of_match_table = of_match_ptr(hisi_rng_dt_ids),
 116        },
 117};
 118
 119module_platform_driver(hisi_rng_driver);
 120
 121MODULE_LICENSE("GPL");
 122MODULE_AUTHOR("Kefeng Wang <wangkefeng.wang@huawei>");
 123MODULE_DESCRIPTION("Hisilicon random number generator driver");
 124