Path: blob/master/drivers/char/hw_random/nomadik-rng.c
15111 views
/*1* Nomadik RNG support2* Copyright 2009 Alessandro Rubini3*4* This program is free software; you can redistribute it and/or modify5* it under the terms of the GNU General Public License as published by6* the Free Software Foundation; either version 2 of the License, or7* (at your option) any later version.8*/910#include <linux/kernel.h>11#include <linux/module.h>12#include <linux/init.h>13#include <linux/device.h>14#include <linux/amba/bus.h>15#include <linux/hw_random.h>16#include <linux/io.h>17#include <linux/clk.h>18#include <linux/err.h>1920static struct clk *rng_clk;2122static int nmk_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)23{24void __iomem *base = (void __iomem *)rng->priv;2526/*27* The register is 32 bits and gives 16 random bits (low half).28* A subsequent read will delay the core for 400ns, so we just read29* once and accept the very unlikely very small delay, even if wait==0.30*/31*(u16 *)data = __raw_readl(base + 8) & 0xffff;32return 2;33}3435/* we have at most one RNG per machine, granted */36static struct hwrng nmk_rng = {37.name = "nomadik",38.read = nmk_rng_read,39};4041static int nmk_rng_probe(struct amba_device *dev, const struct amba_id *id)42{43void __iomem *base;44int ret;4546rng_clk = clk_get(&dev->dev, NULL);47if (IS_ERR(rng_clk)) {48dev_err(&dev->dev, "could not get rng clock\n");49ret = PTR_ERR(rng_clk);50return ret;51}5253clk_enable(rng_clk);5455ret = amba_request_regions(dev, dev->dev.init_name);56if (ret)57return ret;58ret = -ENOMEM;59base = ioremap(dev->res.start, resource_size(&dev->res));60if (!base)61goto out_release;62nmk_rng.priv = (unsigned long)base;63ret = hwrng_register(&nmk_rng);64if (ret)65goto out_unmap;66return 0;6768out_unmap:69iounmap(base);70out_release:71amba_release_regions(dev);72clk_disable(rng_clk);73clk_put(rng_clk);74return ret;75}7677static int nmk_rng_remove(struct amba_device *dev)78{79void __iomem *base = (void __iomem *)nmk_rng.priv;80hwrng_unregister(&nmk_rng);81iounmap(base);82amba_release_regions(dev);83clk_disable(rng_clk);84clk_put(rng_clk);85return 0;86}8788static struct amba_id nmk_rng_ids[] = {89{90.id = 0x000805e1,91.mask = 0x000fffff, /* top bits are rev and cfg: accept all */92},93{0, 0},94};9596static struct amba_driver nmk_rng_driver = {97.drv = {98.owner = THIS_MODULE,99.name = "rng",100},101.probe = nmk_rng_probe,102.remove = nmk_rng_remove,103.id_table = nmk_rng_ids,104};105106static int __init nmk_rng_init(void)107{108return amba_driver_register(&nmk_rng_driver);109}110111static void __devexit nmk_rng_exit(void)112{113amba_driver_unregister(&nmk_rng_driver);114}115116module_init(nmk_rng_init);117module_exit(nmk_rng_exit);118119MODULE_LICENSE("GPL");120121122