Path: blob/master/drivers/char/hw_random/omap-rng.c
15111 views
/*1* omap-rng.c - RNG driver for TI OMAP CPU family2*3* Author: Deepak Saxena <[email protected]>4*5* Copyright 2005 (c) MontaVista Software, Inc.6*7* Mostly based on original driver:8*9* Copyright (C) 2005 Nokia Corporation10* Author: Juha Yrjölä <[email protected]>11*12* This file is licensed under the terms of the GNU General Public13* License version 2. This program is licensed "as is" without any14* warranty of any kind, whether express or implied.15*/1617#include <linux/module.h>18#include <linux/init.h>19#include <linux/random.h>20#include <linux/clk.h>21#include <linux/err.h>22#include <linux/platform_device.h>23#include <linux/hw_random.h>24#include <linux/delay.h>2526#include <asm/io.h>2728#define RNG_OUT_REG 0x00 /* Output register */29#define RNG_STAT_REG 0x04 /* Status register30[0] = STAT_BUSY */31#define RNG_ALARM_REG 0x24 /* Alarm register32[7:0] = ALARM_COUNTER */33#define RNG_CONFIG_REG 0x28 /* Configuration register34[11:6] = RESET_COUNT35[5:3] = RING2_DELAY36[2:0] = RING1_DELAY */37#define RNG_REV_REG 0x3c /* Revision register38[7:0] = REV_NB */39#define RNG_MASK_REG 0x40 /* Mask and reset register40[2] = IT_EN41[1] = SOFTRESET42[0] = AUTOIDLE */43#define RNG_SYSSTATUS 0x44 /* System status44[0] = RESETDONE */4546static void __iomem *rng_base;47static struct clk *rng_ick;48static struct platform_device *rng_dev;4950static inline u32 omap_rng_read_reg(int reg)51{52return __raw_readl(rng_base + reg);53}5455static inline void omap_rng_write_reg(int reg, u32 val)56{57__raw_writel(val, rng_base + reg);58}5960static int omap_rng_data_present(struct hwrng *rng, int wait)61{62int data, i;6364for (i = 0; i < 20; i++) {65data = omap_rng_read_reg(RNG_STAT_REG) ? 0 : 1;66if (data || !wait)67break;68/* RNG produces data fast enough (2+ MBit/sec, even69* during "rngtest" loads, that these delays don't70* seem to trigger. We *could* use the RNG IRQ, but71* that'd be higher overhead ... so why bother?72*/73udelay(10);74}75return data;76}7778static int omap_rng_data_read(struct hwrng *rng, u32 *data)79{80*data = omap_rng_read_reg(RNG_OUT_REG);8182return 4;83}8485static struct hwrng omap_rng_ops = {86.name = "omap",87.data_present = omap_rng_data_present,88.data_read = omap_rng_data_read,89};9091static int __devinit omap_rng_probe(struct platform_device *pdev)92{93struct resource *res;94int ret;9596/*97* A bit ugly, and it will never actually happen but there can98* be only one RNG and this catches any bork99*/100if (rng_dev)101return -EBUSY;102103if (cpu_is_omap24xx()) {104rng_ick = clk_get(&pdev->dev, "ick");105if (IS_ERR(rng_ick)) {106dev_err(&pdev->dev, "Could not get rng_ick\n");107ret = PTR_ERR(rng_ick);108return ret;109} else110clk_enable(rng_ick);111}112113res = platform_get_resource(pdev, IORESOURCE_MEM, 0);114115if (!res)116return -ENOENT;117118if (!request_mem_region(res->start, resource_size(res), pdev->name)) {119ret = -EBUSY;120goto err_region;121}122123dev_set_drvdata(&pdev->dev, res);124rng_base = ioremap(res->start, resource_size(res));125if (!rng_base) {126ret = -ENOMEM;127goto err_ioremap;128}129130ret = hwrng_register(&omap_rng_ops);131if (ret)132goto err_register;133134dev_info(&pdev->dev, "OMAP Random Number Generator ver. %02x\n",135omap_rng_read_reg(RNG_REV_REG));136omap_rng_write_reg(RNG_MASK_REG, 0x1);137138rng_dev = pdev;139140return 0;141142err_register:143iounmap(rng_base);144rng_base = NULL;145err_ioremap:146release_mem_region(res->start, resource_size(res));147err_region:148if (cpu_is_omap24xx()) {149clk_disable(rng_ick);150clk_put(rng_ick);151}152return ret;153}154155static int __exit omap_rng_remove(struct platform_device *pdev)156{157struct resource *res = dev_get_drvdata(&pdev->dev);158159hwrng_unregister(&omap_rng_ops);160161omap_rng_write_reg(RNG_MASK_REG, 0x0);162163iounmap(rng_base);164165if (cpu_is_omap24xx()) {166clk_disable(rng_ick);167clk_put(rng_ick);168}169170release_mem_region(res->start, resource_size(res));171rng_base = NULL;172173return 0;174}175176#ifdef CONFIG_PM177178static int omap_rng_suspend(struct platform_device *pdev, pm_message_t message)179{180omap_rng_write_reg(RNG_MASK_REG, 0x0);181return 0;182}183184static int omap_rng_resume(struct platform_device *pdev)185{186omap_rng_write_reg(RNG_MASK_REG, 0x1);187return 0;188}189190#else191192#define omap_rng_suspend NULL193#define omap_rng_resume NULL194195#endif196197/* work with hotplug and coldplug */198MODULE_ALIAS("platform:omap_rng");199200static struct platform_driver omap_rng_driver = {201.driver = {202.name = "omap_rng",203.owner = THIS_MODULE,204},205.probe = omap_rng_probe,206.remove = __exit_p(omap_rng_remove),207.suspend = omap_rng_suspend,208.resume = omap_rng_resume209};210211static int __init omap_rng_init(void)212{213if (!cpu_is_omap16xx() && !cpu_is_omap24xx())214return -ENODEV;215216return platform_driver_register(&omap_rng_driver);217}218219static void __exit omap_rng_exit(void)220{221platform_driver_unregister(&omap_rng_driver);222}223224module_init(omap_rng_init);225module_exit(omap_rng_exit);226227MODULE_AUTHOR("Deepak Saxena (and others)");228MODULE_LICENSE("GPL");229230231