2019-05-30 07:57:47 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2007-05-09 17:33:35 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006-2007 PA Semi, Inc
|
|
|
|
*
|
|
|
|
* Maintained by: Olof Johansson <olof@lixom.net>
|
|
|
|
*
|
|
|
|
* Driver for the PWRficient onchip rng
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/hw_random.h>
|
2007-11-21 12:24:45 +08:00
|
|
|
#include <linux/delay.h>
|
2013-09-18 03:28:33 +08:00
|
|
|
#include <linux/of_address.h>
|
2008-05-23 14:20:05 +08:00
|
|
|
#include <linux/of_platform.h>
|
2016-09-06 16:28:39 +08:00
|
|
|
#include <linux/io.h>
|
2007-05-09 17:33:35 +08:00
|
|
|
|
|
|
|
#define SDCRNG_CTL_REG 0x00
|
|
|
|
#define SDCRNG_CTL_FVLD_M 0x0000f000
|
|
|
|
#define SDCRNG_CTL_FVLD_S 12
|
|
|
|
#define SDCRNG_CTL_KSZ 0x00000800
|
|
|
|
#define SDCRNG_CTL_RSRC_CRG 0x00000010
|
|
|
|
#define SDCRNG_CTL_RSRC_RRG 0x00000000
|
|
|
|
#define SDCRNG_CTL_CE 0x00000004
|
|
|
|
#define SDCRNG_CTL_RE 0x00000002
|
|
|
|
#define SDCRNG_CTL_DR 0x00000001
|
|
|
|
#define SDCRNG_CTL_SELECT_RRG_RNG (SDCRNG_CTL_RE | SDCRNG_CTL_RSRC_RRG)
|
|
|
|
#define SDCRNG_CTL_SELECT_CRG_RNG (SDCRNG_CTL_CE | SDCRNG_CTL_RSRC_CRG)
|
|
|
|
#define SDCRNG_VAL_REG 0x20
|
|
|
|
|
|
|
|
#define MODULE_NAME "pasemi_rng"
|
|
|
|
|
2007-12-01 09:52:35 +08:00
|
|
|
static int pasemi_rng_data_present(struct hwrng *rng, int wait)
|
2007-05-09 17:33:35 +08:00
|
|
|
{
|
|
|
|
void __iomem *rng_regs = (void __iomem *)rng->priv;
|
2007-11-21 12:24:45 +08:00
|
|
|
int data, i;
|
2007-05-09 17:33:35 +08:00
|
|
|
|
2007-11-21 12:24:45 +08:00
|
|
|
for (i = 0; i < 20; i++) {
|
|
|
|
data = (in_le32(rng_regs + SDCRNG_CTL_REG)
|
|
|
|
& SDCRNG_CTL_FVLD_M) ? 1 : 0;
|
|
|
|
if (data || !wait)
|
|
|
|
break;
|
|
|
|
udelay(10);
|
|
|
|
}
|
|
|
|
return data;
|
2007-05-09 17:33:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pasemi_rng_data_read(struct hwrng *rng, u32 *data)
|
|
|
|
{
|
|
|
|
void __iomem *rng_regs = (void __iomem *)rng->priv;
|
|
|
|
*data = in_le32(rng_regs + SDCRNG_VAL_REG);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pasemi_rng_init(struct hwrng *rng)
|
|
|
|
{
|
|
|
|
void __iomem *rng_regs = (void __iomem *)rng->priv;
|
|
|
|
u32 ctl;
|
|
|
|
|
|
|
|
ctl = SDCRNG_CTL_DR | SDCRNG_CTL_SELECT_RRG_RNG | SDCRNG_CTL_KSZ;
|
|
|
|
out_le32(rng_regs + SDCRNG_CTL_REG, ctl);
|
|
|
|
out_le32(rng_regs + SDCRNG_CTL_REG, ctl & ~SDCRNG_CTL_DR);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pasemi_rng_cleanup(struct hwrng *rng)
|
|
|
|
{
|
|
|
|
void __iomem *rng_regs = (void __iomem *)rng->priv;
|
|
|
|
u32 ctl;
|
|
|
|
|
|
|
|
ctl = SDCRNG_CTL_RE | SDCRNG_CTL_CE;
|
|
|
|
out_le32(rng_regs + SDCRNG_CTL_REG,
|
|
|
|
in_le32(rng_regs + SDCRNG_CTL_REG) & ~ctl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct hwrng pasemi_rng = {
|
|
|
|
.name = MODULE_NAME,
|
|
|
|
.init = pasemi_rng_init,
|
|
|
|
.cleanup = pasemi_rng_cleanup,
|
|
|
|
.data_present = pasemi_rng_data_present,
|
|
|
|
.data_read = pasemi_rng_data_read,
|
|
|
|
};
|
|
|
|
|
2016-09-05 02:13:08 +08:00
|
|
|
static int rng_probe(struct platform_device *pdev)
|
2007-05-09 17:33:35 +08:00
|
|
|
{
|
|
|
|
void __iomem *rng_regs;
|
2016-09-05 02:13:08 +08:00
|
|
|
struct resource *res;
|
2007-05-09 17:33:35 +08:00
|
|
|
|
2016-09-05 02:13:08 +08:00
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
|
|
rng_regs = devm_ioremap_resource(&pdev->dev, res);
|
|
|
|
if (IS_ERR(rng_regs))
|
|
|
|
return PTR_ERR(rng_regs);
|
2007-05-09 17:33:35 +08:00
|
|
|
|
|
|
|
pasemi_rng.priv = (unsigned long)rng_regs;
|
|
|
|
|
2014-09-15 23:01:20 +08:00
|
|
|
pr_info("Registering PA Semi RNG\n");
|
2016-09-05 02:13:08 +08:00
|
|
|
return devm_hwrng_register(&pdev->dev, &pasemi_rng);
|
2007-05-09 17:33:35 +08:00
|
|
|
}
|
|
|
|
|
2015-03-17 03:17:13 +08:00
|
|
|
static const struct of_device_id rng_match[] = {
|
2007-11-05 10:57:45 +08:00
|
|
|
{ .compatible = "1682m-rng", },
|
|
|
|
{ .compatible = "pasemi,pwrficient-rng", },
|
|
|
|
{ },
|
2007-05-09 17:33:35 +08:00
|
|
|
};
|
2015-09-16 00:41:33 +08:00
|
|
|
MODULE_DEVICE_TABLE(of, rng_match);
|
2007-05-09 17:33:35 +08:00
|
|
|
|
2011-02-23 10:59:54 +08:00
|
|
|
static struct platform_driver rng_driver = {
|
2010-04-14 07:13:02 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "pasemi-rng",
|
|
|
|
.of_match_table = rng_match,
|
|
|
|
},
|
2007-05-09 17:33:35 +08:00
|
|
|
.probe = rng_probe,
|
|
|
|
};
|
|
|
|
|
2011-11-26 21:11:06 +08:00
|
|
|
module_platform_driver(rng_driver);
|
2007-05-09 17:33:35 +08:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>");
|
|
|
|
MODULE_DESCRIPTION("H/W RNG driver for PA Semi processor");
|