Path: blob/master/drivers/char/hw_random/pasemi-rng.c
15112 views
/*1* Copyright (C) 2006-2007 PA Semi, Inc2*3* Maintained by: Olof Johansson <[email protected]>4*5* Driver for the PWRficient onchip rng6*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License version 2 as9* published by the Free Software Foundation.10*11* This program is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14* GNU General Public License for more details.15*16* You should have received a copy of the GNU General Public License17* along with this program; if not, write to the Free Software18* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA19*/2021#include <linux/module.h>22#include <linux/kernel.h>23#include <linux/platform_device.h>24#include <linux/hw_random.h>25#include <linux/delay.h>26#include <linux/of_platform.h>27#include <asm/io.h>2829#define SDCRNG_CTL_REG 0x0030#define SDCRNG_CTL_FVLD_M 0x0000f00031#define SDCRNG_CTL_FVLD_S 1232#define SDCRNG_CTL_KSZ 0x0000080033#define SDCRNG_CTL_RSRC_CRG 0x0000001034#define SDCRNG_CTL_RSRC_RRG 0x0000000035#define SDCRNG_CTL_CE 0x0000000436#define SDCRNG_CTL_RE 0x0000000237#define SDCRNG_CTL_DR 0x0000000138#define SDCRNG_CTL_SELECT_RRG_RNG (SDCRNG_CTL_RE | SDCRNG_CTL_RSRC_RRG)39#define SDCRNG_CTL_SELECT_CRG_RNG (SDCRNG_CTL_CE | SDCRNG_CTL_RSRC_CRG)40#define SDCRNG_VAL_REG 0x204142#define MODULE_NAME "pasemi_rng"4344static int pasemi_rng_data_present(struct hwrng *rng, int wait)45{46void __iomem *rng_regs = (void __iomem *)rng->priv;47int data, i;4849for (i = 0; i < 20; i++) {50data = (in_le32(rng_regs + SDCRNG_CTL_REG)51& SDCRNG_CTL_FVLD_M) ? 1 : 0;52if (data || !wait)53break;54udelay(10);55}56return data;57}5859static int pasemi_rng_data_read(struct hwrng *rng, u32 *data)60{61void __iomem *rng_regs = (void __iomem *)rng->priv;62*data = in_le32(rng_regs + SDCRNG_VAL_REG);63return 4;64}6566static int pasemi_rng_init(struct hwrng *rng)67{68void __iomem *rng_regs = (void __iomem *)rng->priv;69u32 ctl;7071ctl = SDCRNG_CTL_DR | SDCRNG_CTL_SELECT_RRG_RNG | SDCRNG_CTL_KSZ;72out_le32(rng_regs + SDCRNG_CTL_REG, ctl);73out_le32(rng_regs + SDCRNG_CTL_REG, ctl & ~SDCRNG_CTL_DR);7475return 0;76}7778static void pasemi_rng_cleanup(struct hwrng *rng)79{80void __iomem *rng_regs = (void __iomem *)rng->priv;81u32 ctl;8283ctl = SDCRNG_CTL_RE | SDCRNG_CTL_CE;84out_le32(rng_regs + SDCRNG_CTL_REG,85in_le32(rng_regs + SDCRNG_CTL_REG) & ~ctl);86}8788static struct hwrng pasemi_rng = {89.name = MODULE_NAME,90.init = pasemi_rng_init,91.cleanup = pasemi_rng_cleanup,92.data_present = pasemi_rng_data_present,93.data_read = pasemi_rng_data_read,94};9596static int __devinit rng_probe(struct platform_device *ofdev)97{98void __iomem *rng_regs;99struct device_node *rng_np = ofdev->dev.of_node;100struct resource res;101int err = 0;102103err = of_address_to_resource(rng_np, 0, &res);104if (err)105return -ENODEV;106107rng_regs = ioremap(res.start, 0x100);108109if (!rng_regs)110return -ENOMEM;111112pasemi_rng.priv = (unsigned long)rng_regs;113114printk(KERN_INFO "Registering PA Semi RNG\n");115116err = hwrng_register(&pasemi_rng);117118if (err)119iounmap(rng_regs);120121return err;122}123124static int __devexit rng_remove(struct platform_device *dev)125{126void __iomem *rng_regs = (void __iomem *)pasemi_rng.priv;127128hwrng_unregister(&pasemi_rng);129iounmap(rng_regs);130131return 0;132}133134static struct of_device_id rng_match[] = {135{ .compatible = "1682m-rng", },136{ .compatible = "pasemi,pwrficient-rng", },137{ },138};139140static struct platform_driver rng_driver = {141.driver = {142.name = "pasemi-rng",143.owner = THIS_MODULE,144.of_match_table = rng_match,145},146.probe = rng_probe,147.remove = rng_remove,148};149150static int __init rng_init(void)151{152return platform_driver_register(&rng_driver);153}154module_init(rng_init);155156static void __exit rng_exit(void)157{158platform_driver_unregister(&rng_driver);159}160module_exit(rng_exit);161162MODULE_LICENSE("GPL");163MODULE_AUTHOR("Egor Martovetsky <[email protected]>");164MODULE_DESCRIPTION("H/W RNG driver for PA Semi processor");165166167