linux/drivers/char/hw_random/omap-rng.c
<<
>>
Prefs
   1/*
   2 * omap-rng.c - RNG driver for TI OMAP CPU family
   3 *
   4 * Author: Deepak Saxena <dsaxena@plexity.net>
   5 *
   6 * Copyright 2005 (c) MontaVista Software, Inc.
   7 *
   8 * Mostly based on original driver:
   9 *
  10 * Copyright (C) 2005 Nokia Corporation
  11 * Author: Juha Yrjölä <juha.yrjola@nokia.com>
  12 *
  13 * This file is licensed under  the terms of the GNU General Public
  14 * License version 2. This program is licensed "as is" without any
  15 * warranty of any kind, whether express or implied.
  16 */
  17
  18#include <linux/module.h>
  19#include <linux/init.h>
  20#include <linux/random.h>
  21#include <linux/clk.h>
  22#include <linux/err.h>
  23#include <linux/platform_device.h>
  24#include <linux/hw_random.h>
  25#include <linux/delay.h>
  26
  27#include <asm/io.h>
  28
  29#define RNG_OUT_REG             0x00            /* Output register */
  30#define RNG_STAT_REG            0x04            /* Status register
  31                                                        [0] = STAT_BUSY */
  32#define RNG_ALARM_REG           0x24            /* Alarm register
  33                                                        [7:0] = ALARM_COUNTER */
  34#define RNG_CONFIG_REG          0x28            /* Configuration register
  35                                                        [11:6] = RESET_COUNT
  36                                                        [5:3]  = RING2_DELAY
  37                                                        [2:0]  = RING1_DELAY */
  38#define RNG_REV_REG             0x3c            /* Revision register
  39                                                        [7:0] = REV_NB */
  40#define RNG_MASK_REG            0x40            /* Mask and reset register
  41                                                        [2] = IT_EN
  42                                                        [1] = SOFTRESET
  43                                                        [0] = AUTOIDLE */
  44#define RNG_SYSSTATUS           0x44            /* System status
  45                                                        [0] = RESETDONE */
  46
  47static void __iomem *rng_base;
  48static struct clk *rng_ick;
  49static struct platform_device *rng_dev;
  50
  51static inline u32 omap_rng_read_reg(int reg)
  52{
  53        return __raw_readl(rng_base + reg);
  54}
  55
  56static inline void omap_rng_write_reg(int reg, u32 val)
  57{
  58        __raw_writel(val, rng_base + reg);
  59}
  60
  61static int omap_rng_data_present(struct hwrng *rng, int wait)
  62{
  63        int data, i;
  64
  65        for (i = 0; i < 20; i++) {
  66                data = omap_rng_read_reg(RNG_STAT_REG) ? 0 : 1;
  67                if (data || !wait)
  68                        break;
  69                /* RNG produces data fast enough (2+ MBit/sec, even
  70                 * during "rngtest" loads, that these delays don't
  71                 * seem to trigger.  We *could* use the RNG IRQ, but
  72                 * that'd be higher overhead ... so why bother?
  73                 */
  74                udelay(10);
  75        }
  76        return data;
  77}
  78
  79static int omap_rng_data_read(struct hwrng *rng, u32 *data)
  80{
  81        *data = omap_rng_read_reg(RNG_OUT_REG);
  82
  83        return 4;
  84}
  85
  86static struct hwrng omap_rng_ops = {
  87        .name           = "omap",
  88        .data_present   = omap_rng_data_present,
  89        .data_read      = omap_rng_data_read,
  90};
  91
  92static int __devinit omap_rng_probe(struct platform_device *pdev)
  93{
  94        struct resource *res;
  95        int ret;
  96
  97        /*
  98         * A bit ugly, and it will never actually happen but there can
  99         * be only one RNG and this catches any bork
 100         */
 101        if (rng_dev)
 102                return -EBUSY;
 103
 104        if (cpu_is_omap24xx()) {
 105                rng_ick = clk_get(&pdev->dev, "ick");
 106                if (IS_ERR(rng_ick)) {
 107                        dev_err(&pdev->dev, "Could not get rng_ick\n");
 108                        ret = PTR_ERR(rng_ick);
 109                        return ret;
 110                } else
 111                        clk_enable(rng_ick);
 112        }
 113
 114        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 115
 116        if (!res)
 117                return -ENOENT;
 118
 119        if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
 120                ret = -EBUSY;
 121                goto err_region;
 122        }
 123
 124        dev_set_drvdata(&pdev->dev, res);
 125        rng_base = ioremap(res->start, resource_size(res));
 126        if (!rng_base) {
 127                ret = -ENOMEM;
 128                goto err_ioremap;
 129        }
 130
 131        ret = hwrng_register(&omap_rng_ops);
 132        if (ret)
 133                goto err_register;
 134
 135        dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n",
 136                omap_rng_read_reg(RNG_REV_REG));
 137        omap_rng_write_reg(RNG_MASK_REG, 0x1);
 138
 139        rng_dev = pdev;
 140
 141        return 0;
 142
 143err_register:
 144        iounmap(rng_base);
 145        rng_base = NULL;
 146err_ioremap:
 147        release_mem_region(res->start, resource_size(res));
 148err_region:
 149        if (cpu_is_omap24xx()) {
 150                clk_disable(rng_ick);
 151                clk_put(rng_ick);
 152        }
 153        return ret;
 154}
 155
 156static int __exit omap_rng_remove(struct platform_device *pdev)
 157{
 158        struct resource *res = dev_get_drvdata(&pdev->dev);
 159
 160        hwrng_unregister(&omap_rng_ops);
 161
 162        omap_rng_write_reg(RNG_MASK_REG, 0x0);
 163
 164        iounmap(rng_base);
 165
 166        if (cpu_is_omap24xx()) {
 167                clk_disable(rng_ick);
 168                clk_put(rng_ick);
 169        }
 170
 171        release_mem_region(res->start, resource_size(res));
 172        rng_base = NULL;
 173
 174        return 0;
 175}
 176
 177#ifdef CONFIG_PM
 178
 179static int omap_rng_suspend(struct platform_device *pdev, pm_message_t message)
 180{
 181        omap_rng_write_reg(RNG_MASK_REG, 0x0);
 182        return 0;
 183}
 184
 185static int omap_rng_resume(struct platform_device *pdev)
 186{
 187        omap_rng_write_reg(RNG_MASK_REG, 0x1);
 188        return 0;
 189}
 190
 191#else
 192
 193#define omap_rng_suspend        NULL
 194#define omap_rng_resume         NULL
 195
 196#endif
 197
 198/* work with hotplug and coldplug */
 199MODULE_ALIAS("platform:omap_rng");
 200
 201static struct platform_driver omap_rng_driver = {
 202        .driver = {
 203                .name           = "omap_rng",
 204                .owner          = THIS_MODULE,
 205        },
 206        .probe          = omap_rng_probe,
 207        .remove         = __exit_p(omap_rng_remove),
 208        .suspend        = omap_rng_suspend,
 209        .resume         = omap_rng_resume
 210};
 211
 212static int __init omap_rng_init(void)
 213{
 214        if (!cpu_is_omap16xx() && !cpu_is_omap24xx())
 215                return -ENODEV;
 216
 217        return platform_driver_register(&omap_rng_driver);
 218}
 219
 220static void __exit omap_rng_exit(void)
 221{
 222        platform_driver_unregister(&omap_rng_driver);
 223}
 224
 225module_init(omap_rng_init);
 226module_exit(omap_rng_exit);
 227
 228MODULE_AUTHOR("Deepak Saxena (and others)");
 229MODULE_LICENSE("GPL");
 230