18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2006-2007 PA Semi, Inc
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Maintained by: Olof Johansson <olof@lixom.net>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Driver for the PWRficient onchip rng
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
138c2ecf20Sopenharmony_ci#include <linux/hw_random.h>
148c2ecf20Sopenharmony_ci#include <linux/delay.h>
158c2ecf20Sopenharmony_ci#include <linux/of_address.h>
168c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
178c2ecf20Sopenharmony_ci#include <linux/io.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define SDCRNG_CTL_REG			0x00
208c2ecf20Sopenharmony_ci#define   SDCRNG_CTL_FVLD_M		0x0000f000
218c2ecf20Sopenharmony_ci#define   SDCRNG_CTL_FVLD_S		12
228c2ecf20Sopenharmony_ci#define   SDCRNG_CTL_KSZ		0x00000800
238c2ecf20Sopenharmony_ci#define   SDCRNG_CTL_RSRC_CRG		0x00000010
248c2ecf20Sopenharmony_ci#define   SDCRNG_CTL_RSRC_RRG		0x00000000
258c2ecf20Sopenharmony_ci#define   SDCRNG_CTL_CE			0x00000004
268c2ecf20Sopenharmony_ci#define   SDCRNG_CTL_RE			0x00000002
278c2ecf20Sopenharmony_ci#define   SDCRNG_CTL_DR			0x00000001
288c2ecf20Sopenharmony_ci#define   SDCRNG_CTL_SELECT_RRG_RNG	(SDCRNG_CTL_RE | SDCRNG_CTL_RSRC_RRG)
298c2ecf20Sopenharmony_ci#define   SDCRNG_CTL_SELECT_CRG_RNG	(SDCRNG_CTL_CE | SDCRNG_CTL_RSRC_CRG)
308c2ecf20Sopenharmony_ci#define SDCRNG_VAL_REG			0x20
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define MODULE_NAME "pasemi_rng"
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic int pasemi_rng_data_present(struct hwrng *rng, int wait)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	void __iomem *rng_regs = (void __iomem *)rng->priv;
378c2ecf20Sopenharmony_ci	int data, i;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	for (i = 0; i < 20; i++) {
408c2ecf20Sopenharmony_ci		data = (in_le32(rng_regs + SDCRNG_CTL_REG)
418c2ecf20Sopenharmony_ci			& SDCRNG_CTL_FVLD_M) ? 1 : 0;
428c2ecf20Sopenharmony_ci		if (data || !wait)
438c2ecf20Sopenharmony_ci			break;
448c2ecf20Sopenharmony_ci		udelay(10);
458c2ecf20Sopenharmony_ci	}
468c2ecf20Sopenharmony_ci	return data;
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic int pasemi_rng_data_read(struct hwrng *rng, u32 *data)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	void __iomem *rng_regs = (void __iomem *)rng->priv;
528c2ecf20Sopenharmony_ci	*data = in_le32(rng_regs + SDCRNG_VAL_REG);
538c2ecf20Sopenharmony_ci	return 4;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic int pasemi_rng_init(struct hwrng *rng)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	void __iomem *rng_regs = (void __iomem *)rng->priv;
598c2ecf20Sopenharmony_ci	u32 ctl;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	ctl = SDCRNG_CTL_DR | SDCRNG_CTL_SELECT_RRG_RNG | SDCRNG_CTL_KSZ;
628c2ecf20Sopenharmony_ci	out_le32(rng_regs + SDCRNG_CTL_REG, ctl);
638c2ecf20Sopenharmony_ci	out_le32(rng_regs + SDCRNG_CTL_REG, ctl & ~SDCRNG_CTL_DR);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	return 0;
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic void pasemi_rng_cleanup(struct hwrng *rng)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	void __iomem *rng_regs = (void __iomem *)rng->priv;
718c2ecf20Sopenharmony_ci	u32 ctl;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	ctl = SDCRNG_CTL_RE | SDCRNG_CTL_CE;
748c2ecf20Sopenharmony_ci	out_le32(rng_regs + SDCRNG_CTL_REG,
758c2ecf20Sopenharmony_ci		 in_le32(rng_regs + SDCRNG_CTL_REG) & ~ctl);
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic struct hwrng pasemi_rng = {
798c2ecf20Sopenharmony_ci	.name		= MODULE_NAME,
808c2ecf20Sopenharmony_ci	.init		= pasemi_rng_init,
818c2ecf20Sopenharmony_ci	.cleanup	= pasemi_rng_cleanup,
828c2ecf20Sopenharmony_ci	.data_present	= pasemi_rng_data_present,
838c2ecf20Sopenharmony_ci	.data_read	= pasemi_rng_data_read,
848c2ecf20Sopenharmony_ci};
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic int rng_probe(struct platform_device *pdev)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	void __iomem *rng_regs;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	rng_regs = devm_platform_ioremap_resource(pdev, 0);
918c2ecf20Sopenharmony_ci	if (IS_ERR(rng_regs))
928c2ecf20Sopenharmony_ci		return PTR_ERR(rng_regs);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	pasemi_rng.priv = (unsigned long)rng_regs;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	pr_info("Registering PA Semi RNG\n");
978c2ecf20Sopenharmony_ci	return devm_hwrng_register(&pdev->dev, &pasemi_rng);
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic const struct of_device_id rng_match[] = {
1018c2ecf20Sopenharmony_ci	{ .compatible      = "1682m-rng", },
1028c2ecf20Sopenharmony_ci	{ .compatible      = "pasemi,pwrficient-rng", },
1038c2ecf20Sopenharmony_ci	{ },
1048c2ecf20Sopenharmony_ci};
1058c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, rng_match);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic struct platform_driver rng_driver = {
1088c2ecf20Sopenharmony_ci	.driver = {
1098c2ecf20Sopenharmony_ci		.name = "pasemi-rng",
1108c2ecf20Sopenharmony_ci		.of_match_table = rng_match,
1118c2ecf20Sopenharmony_ci	},
1128c2ecf20Sopenharmony_ci	.probe		= rng_probe,
1138c2ecf20Sopenharmony_ci};
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cimodule_platform_driver(rng_driver);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1188c2ecf20Sopenharmony_ciMODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>");
1198c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("H/W RNG driver for PA Semi processor");
120