1/* 2 * RNG driver for Moschip MCS814x SoC 3 * 4 * Copyright 2012 (C), Florian Fainelli <florian@openwrt.org> 5 * 6 * This file is licensed under the terms of the GNU General Public 7 * License version 2. This program is licensed "as is" without any 8 * warranty of any kind, whether express or implied. 9 */ 10 11#include <linux/kernel.h> 12#include <linux/slab.h> 13#include <linux/module.h> 14#include <linux/types.h> 15#include <linux/platform_device.h> 16#include <linux/hw_random.h> 17#include <linux/io.h> 18#include <linux/of.h> 19 20#define STAT 0x00 21#define RND 0x04 22 23struct mcs814x_rng_priv { 24 void __iomem *regs; 25}; 26 27static int mcs814x_rng_data_read(struct hwrng *rng, u32 *buffer) 28{ 29 struct mcs814x_rng_priv *priv = (struct mcs814x_rng_priv *)rng->priv; 30 31 *buffer = readl_relaxed(priv->regs + RND); 32 33 return 4; 34} 35 36static int mcs814x_rng_probe(struct platform_device *pdev) 37{ 38 struct resource *res; 39 struct mcs814x_rng_priv *priv; 40 struct hwrng *rng; 41 int ret; 42 43 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 44 if (!res) 45 return -ENODEV; 46 47 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 48 if (!priv) { 49 ret = -ENOMEM; 50 goto out; 51 } 52 53 rng = kzalloc(sizeof(*rng), GFP_KERNEL); 54 if (!rng) { 55 ret = -ENOMEM; 56 goto out_priv; 57 } 58 59 platform_set_drvdata(pdev, rng); 60 rng->priv = (unsigned long)priv; 61 rng->name = pdev->name; 62 rng->data_read = mcs814x_rng_data_read; 63 64 priv->regs = devm_ioremap_resource(&pdev->dev, res); 65 if (!priv->regs) { 66 ret = -ENOMEM; 67 goto out_rng; 68 } 69 70 ret = hwrng_register(rng); 71 if (ret) { 72 dev_err(&pdev->dev, "failed to register hwrng driver\n"); 73 goto out; 74 } 75 76 dev_info(&pdev->dev, "registered\n"); 77 78 return ret; 79 80out_rng: 81 platform_set_drvdata(pdev, NULL); 82 kfree(rng); 83out_priv: 84 kfree(priv); 85out: 86 return ret; 87} 88 89static int mcs814x_rng_remove(struct platform_device *pdev) 90{ 91 struct hwrng *rng = platform_get_drvdata(pdev); 92 struct mcs814x_rng_priv *priv = (struct mcs814x_rng_priv *)rng->priv; 93 94 hwrng_unregister(rng); 95 kfree(priv); 96 kfree(rng); 97 platform_set_drvdata(pdev, NULL); 98 99 return 0; 100} 101 102static const struct of_device_id mcs814x_rng_ids[] = { 103 { .compatible = "moschip,mcs814x-rng", }, 104 { /* sentinel */ }, 105}; 106 107static struct platform_driver mcs814x_rng_driver = { 108 .driver = { 109 .name = "mcs814x-rng", 110 .owner = THIS_MODULE, 111 .of_match_table = mcs814x_rng_ids, 112 }, 113 .probe = mcs814x_rng_probe, 114 .remove = mcs814x_rng_remove, 115}; 116 117module_platform_driver(mcs814x_rng_driver); 118 119MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>"); 120MODULE_DESCRIPTION("H/W Random Number Generator (RNG) for Moschip MCS814x"); 121MODULE_LICENSE("GPL"); 122