| 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 | |
| 23 | struct mcs814x_rng_priv { |
| 24 | void __iomem *regs; |
| 25 | }; |
| 26 | |
| 27 | static 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 | |
| 36 | static 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 | if (!devm_request_mem_region(&pdev->dev, |
| 65 | res->start, resource_size(res), |
| 66 | pdev->name)) { |
| 67 | ret = -EBUSY; |
| 68 | goto out_rng; |
| 69 | } |
| 70 | |
| 71 | priv->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res)); |
| 72 | if (!priv->regs) { |
| 73 | ret = -ENOMEM; |
| 74 | goto out_rng; |
| 75 | } |
| 76 | |
| 77 | ret = hwrng_register(rng); |
| 78 | if (ret) { |
| 79 | dev_err(&pdev->dev, "failed to register hwrng driver\n"); |
| 80 | goto out; |
| 81 | } |
| 82 | |
| 83 | dev_info(&pdev->dev, "registered\n"); |
| 84 | |
| 85 | return ret; |
| 86 | |
| 87 | out_rng: |
| 88 | platform_set_drvdata(pdev, NULL); |
| 89 | kfree(rng); |
| 90 | out_priv: |
| 91 | kfree(priv); |
| 92 | out: |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | static int mcs814x_rng_remove(struct platform_device *pdev) |
| 97 | { |
| 98 | struct hwrng *rng = platform_get_drvdata(pdev); |
| 99 | struct mcs814x_rng_priv *priv = (struct mcs814x_rng_priv *)rng->priv; |
| 100 | |
| 101 | hwrng_unregister(rng); |
| 102 | kfree(priv); |
| 103 | kfree(rng); |
| 104 | platform_set_drvdata(pdev, NULL); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static const struct of_device_id mcs814x_rng_ids[] = { |
| 110 | { .compatible = "moschip,mcs814x-rng", }, |
| 111 | { /* sentinel */ }, |
| 112 | }; |
| 113 | |
| 114 | static struct platform_driver mcs814x_rng_driver = { |
| 115 | .driver = { |
| 116 | .name = "mcs814x-rng", |
| 117 | .owner = THIS_MODULE, |
| 118 | .of_match_table = mcs814x_rng_ids, |
| 119 | }, |
| 120 | .probe = mcs814x_rng_probe, |
| 121 | .remove = __devexit_p(mcs814x_rng_remove), |
| 122 | }; |
| 123 | |
| 124 | module_platform_driver(mcs814x_rng_driver); |
| 125 | |
| 126 | MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>"); |
| 127 | MODULE_DESCRIPTION("H/W Random Number Generator (RNG) for Moschip MCS814x"); |
| 128 | MODULE_LICENSE("GPL"); |
| 129 | |