Path: blob/master/drivers/char/hw_random/picoxcell-rng.c
15111 views
/*1* Copyright (c) 2010-2011 Picochip Ltd., Jamie Iles2*3* This program is free software; you can redistribute it and/or modify4* it under the terms of the GNU General Public License version 2 as5* published by the Free Software Foundation.6*7* All enquiries to [email protected]8*/9#include <linux/clk.h>10#include <linux/delay.h>11#include <linux/err.h>12#include <linux/hw_random.h>13#include <linux/io.h>14#include <linux/kernel.h>15#include <linux/module.h>16#include <linux/platform_device.h>1718#define DATA_REG_OFFSET 0x020019#define CSR_REG_OFFSET 0x027820#define CSR_OUT_EMPTY_MASK (1 << 24)21#define CSR_FAULT_MASK (1 << 1)22#define TRNG_BLOCK_RESET_MASK (1 << 0)23#define TAI_REG_OFFSET 0x03802425/*26* The maximum amount of time in microseconds to spend waiting for data if the27* core wants us to wait. The TRNG should generate 32 bits every 320ns so a28* timeout of 20us seems reasonable. The TRNG does builtin tests of the data29* for randomness so we can't always assume there is data present.30*/31#define PICO_TRNG_TIMEOUT 203233static void __iomem *rng_base;34static struct clk *rng_clk;35struct device *rng_dev;3637static inline u32 picoxcell_trng_read_csr(void)38{39return __raw_readl(rng_base + CSR_REG_OFFSET);40}4142static inline bool picoxcell_trng_is_empty(void)43{44return picoxcell_trng_read_csr() & CSR_OUT_EMPTY_MASK;45}4647/*48* Take the random number generator out of reset and make sure the interrupts49* are masked. We shouldn't need to get large amounts of random bytes so just50* poll the status register. The hardware generates 32 bits every 320ns so we51* shouldn't have to wait long enough to warrant waiting for an IRQ.52*/53static void picoxcell_trng_start(void)54{55__raw_writel(0, rng_base + TAI_REG_OFFSET);56__raw_writel(0, rng_base + CSR_REG_OFFSET);57}5859static void picoxcell_trng_reset(void)60{61__raw_writel(TRNG_BLOCK_RESET_MASK, rng_base + CSR_REG_OFFSET);62__raw_writel(TRNG_BLOCK_RESET_MASK, rng_base + TAI_REG_OFFSET);63picoxcell_trng_start();64}6566/*67* Get some random data from the random number generator. The hw_random core68* layer provides us with locking.69*/70static int picoxcell_trng_read(struct hwrng *rng, void *buf, size_t max,71bool wait)72{73int i;7475/* Wait for some data to become available. */76for (i = 0; i < PICO_TRNG_TIMEOUT && picoxcell_trng_is_empty(); ++i) {77if (!wait)78return 0;7980udelay(1);81}8283if (picoxcell_trng_read_csr() & CSR_FAULT_MASK) {84dev_err(rng_dev, "fault detected, resetting TRNG\n");85picoxcell_trng_reset();86return -EIO;87}8889if (i == PICO_TRNG_TIMEOUT)90return 0;9192*(u32 *)buf = __raw_readl(rng_base + DATA_REG_OFFSET);93return sizeof(u32);94}9596static struct hwrng picoxcell_trng = {97.name = "picoxcell",98.read = picoxcell_trng_read,99};100101static int picoxcell_trng_probe(struct platform_device *pdev)102{103int ret;104struct resource *mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);105106if (!mem) {107dev_warn(&pdev->dev, "no memory resource\n");108return -ENOMEM;109}110111if (!devm_request_mem_region(&pdev->dev, mem->start, resource_size(mem),112"picoxcell_trng")) {113dev_warn(&pdev->dev, "unable to request io mem\n");114return -EBUSY;115}116117rng_base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));118if (!rng_base) {119dev_warn(&pdev->dev, "unable to remap io mem\n");120return -ENOMEM;121}122123rng_clk = clk_get(&pdev->dev, NULL);124if (IS_ERR(rng_clk)) {125dev_warn(&pdev->dev, "no clk\n");126return PTR_ERR(rng_clk);127}128129ret = clk_enable(rng_clk);130if (ret) {131dev_warn(&pdev->dev, "unable to enable clk\n");132goto err_enable;133}134135picoxcell_trng_start();136ret = hwrng_register(&picoxcell_trng);137if (ret)138goto err_register;139140rng_dev = &pdev->dev;141dev_info(&pdev->dev, "pixoxcell random number generator active\n");142143return 0;144145err_register:146clk_disable(rng_clk);147err_enable:148clk_put(rng_clk);149150return ret;151}152153static int __devexit picoxcell_trng_remove(struct platform_device *pdev)154{155hwrng_unregister(&picoxcell_trng);156clk_disable(rng_clk);157clk_put(rng_clk);158159return 0;160}161162#ifdef CONFIG_PM163static int picoxcell_trng_suspend(struct device *dev)164{165clk_disable(rng_clk);166167return 0;168}169170static int picoxcell_trng_resume(struct device *dev)171{172return clk_enable(rng_clk);173}174175static const struct dev_pm_ops picoxcell_trng_pm_ops = {176.suspend = picoxcell_trng_suspend,177.resume = picoxcell_trng_resume,178};179#endif /* CONFIG_PM */180181static struct platform_driver picoxcell_trng_driver = {182.probe = picoxcell_trng_probe,183.remove = __devexit_p(picoxcell_trng_remove),184.driver = {185.name = "picoxcell-trng",186.owner = THIS_MODULE,187#ifdef CONFIG_PM188.pm = &picoxcell_trng_pm_ops,189#endif /* CONFIG_PM */190},191};192193static int __init picoxcell_trng_init(void)194{195return platform_driver_register(&picoxcell_trng_driver);196}197module_init(picoxcell_trng_init);198199static void __exit picoxcell_trng_exit(void)200{201platform_driver_unregister(&picoxcell_trng_driver);202}203module_exit(picoxcell_trng_exit);204205MODULE_LICENSE("GPL");206MODULE_AUTHOR("Jamie Iles");207MODULE_DESCRIPTION("Picochip picoXcell TRNG driver");208209210