2011-10-06 17:56:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Peter Korsgaard <jacmet@sunsite.dk>
|
|
|
|
*
|
|
|
|
* This file is licensed under the terms of the GNU General Public
|
|
|
|
* License version 2. This program is licensed "as is" without any
|
|
|
|
* warranty of any kind, whether express or implied.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/hw_random.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
|
|
|
|
#define TRNG_CR 0x00
|
|
|
|
#define TRNG_ISR 0x1c
|
|
|
|
#define TRNG_ODATA 0x50
|
|
|
|
|
|
|
|
#define TRNG_KEY 0x524e4700 /* RNG */
|
|
|
|
|
|
|
|
struct atmel_trng {
|
|
|
|
struct clk *clk;
|
|
|
|
void __iomem *base;
|
|
|
|
struct hwrng rng;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int atmel_trng_read(struct hwrng *rng, void *buf, size_t max,
|
|
|
|
bool wait)
|
|
|
|
{
|
|
|
|
struct atmel_trng *trng = container_of(rng, struct atmel_trng, rng);
|
|
|
|
u32 *data = buf;
|
|
|
|
|
|
|
|
/* data ready? */
|
2012-06-12 00:27:04 +00:00
|
|
|
if (readl(trng->base + TRNG_ISR) & 1) {
|
2011-10-06 17:56:30 +00:00
|
|
|
*data = readl(trng->base + TRNG_ODATA);
|
2012-05-31 10:53:08 +00:00
|
|
|
/*
|
|
|
|
ensure data ready is only set again AFTER the next data
|
|
|
|
word is ready in case it got set between checking ISR
|
|
|
|
and reading ODATA, so we don't risk re-reading the
|
|
|
|
same word
|
|
|
|
*/
|
|
|
|
readl(trng->base + TRNG_ISR);
|
2011-10-06 17:56:30 +00:00
|
|
|
return 4;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-28 08:00:46 +00:00
|
|
|
static void atmel_trng_enable(struct atmel_trng *trng)
|
|
|
|
{
|
|
|
|
writel(TRNG_KEY | 1, trng->base + TRNG_CR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void atmel_trng_disable(struct atmel_trng *trng)
|
|
|
|
{
|
|
|
|
writel(TRNG_KEY, trng->base + TRNG_CR);
|
|
|
|
}
|
|
|
|
|
2011-10-06 17:56:30 +00:00
|
|
|
static int atmel_trng_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct atmel_trng *trng;
|
|
|
|
struct resource *res;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL);
|
|
|
|
if (!trng)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-02-12 05:17:08 +00:00
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
|
|
trng->base = devm_ioremap_resource(&pdev->dev, res);
|
|
|
|
if (IS_ERR(trng->base))
|
|
|
|
return PTR_ERR(trng->base);
|
2011-10-06 17:56:30 +00:00
|
|
|
|
2014-02-27 05:00:09 +00:00
|
|
|
trng->clk = devm_clk_get(&pdev->dev, NULL);
|
2011-10-06 17:56:30 +00:00
|
|
|
if (IS_ERR(trng->clk))
|
|
|
|
return PTR_ERR(trng->clk);
|
|
|
|
|
2014-11-20 09:43:22 +00:00
|
|
|
ret = clk_prepare_enable(trng->clk);
|
2011-10-06 17:56:30 +00:00
|
|
|
if (ret)
|
2014-02-27 05:00:09 +00:00
|
|
|
return ret;
|
2011-10-06 17:56:30 +00:00
|
|
|
|
2016-10-28 08:00:46 +00:00
|
|
|
atmel_trng_enable(trng);
|
2011-10-06 17:56:30 +00:00
|
|
|
trng->rng.name = pdev->name;
|
|
|
|
trng->rng.read = atmel_trng_read;
|
|
|
|
|
|
|
|
ret = hwrng_register(&trng->rng);
|
|
|
|
if (ret)
|
|
|
|
goto err_register;
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, trng);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_register:
|
2016-11-11 14:56:47 +00:00
|
|
|
clk_disable_unprepare(trng->clk);
|
2011-10-06 17:56:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-19 18:26:26 +00:00
|
|
|
static int atmel_trng_remove(struct platform_device *pdev)
|
2011-10-06 17:56:30 +00:00
|
|
|
{
|
|
|
|
struct atmel_trng *trng = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
hwrng_unregister(&trng->rng);
|
|
|
|
|
2016-10-28 08:00:46 +00:00
|
|
|
atmel_trng_disable(trng);
|
2014-11-20 09:43:22 +00:00
|
|
|
clk_disable_unprepare(trng->clk);
|
2011-10-06 17:56:30 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_PM
|
|
|
|
static int atmel_trng_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct atmel_trng *trng = dev_get_drvdata(dev);
|
|
|
|
|
2016-10-28 08:00:46 +00:00
|
|
|
atmel_trng_disable(trng);
|
2014-11-20 09:43:22 +00:00
|
|
|
clk_disable_unprepare(trng->clk);
|
2011-10-06 17:56:30 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int atmel_trng_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct atmel_trng *trng = dev_get_drvdata(dev);
|
2016-10-28 08:00:46 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = clk_prepare_enable(trng->clk);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2011-10-06 17:56:30 +00:00
|
|
|
|
2016-10-28 08:00:46 +00:00
|
|
|
atmel_trng_enable(trng);
|
|
|
|
|
|
|
|
return 0;
|
2011-10-06 17:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dev_pm_ops atmel_trng_pm_ops = {
|
|
|
|
.suspend = atmel_trng_suspend,
|
|
|
|
.resume = atmel_trng_resume,
|
|
|
|
};
|
|
|
|
#endif /* CONFIG_PM */
|
|
|
|
|
2014-11-20 09:43:23 +00:00
|
|
|
static const struct of_device_id atmel_trng_dt_ids[] = {
|
|
|
|
{ .compatible = "atmel,at91sam9g45-trng" },
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, atmel_trng_dt_ids);
|
|
|
|
|
2011-10-06 17:56:30 +00:00
|
|
|
static struct platform_driver atmel_trng_driver = {
|
|
|
|
.probe = atmel_trng_probe,
|
2012-12-21 23:12:08 +00:00
|
|
|
.remove = atmel_trng_remove,
|
2011-10-06 17:56:30 +00:00
|
|
|
.driver = {
|
|
|
|
.name = "atmel-trng",
|
|
|
|
#ifdef CONFIG_PM
|
|
|
|
.pm = &atmel_trng_pm_ops,
|
|
|
|
#endif /* CONFIG_PM */
|
2014-11-20 09:43:23 +00:00
|
|
|
.of_match_table = atmel_trng_dt_ids,
|
2011-10-06 17:56:30 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2011-11-26 13:11:06 +00:00
|
|
|
module_platform_driver(atmel_trng_driver);
|
2011-10-06 17:56:30 +00:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
|
|
|
|
MODULE_DESCRIPTION("Atmel true random number generator driver");
|