18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * APM X-Gene SoC RNG Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2014, Applied Micro Circuits Corporation
68c2ecf20Sopenharmony_ci * Author: Rameshwar Prasad Sahu <rsahu@apm.com>
78c2ecf20Sopenharmony_ci *	   Shamal Winchurkar <swinchurkar@apm.com>
88c2ecf20Sopenharmony_ci *	   Feng Kan <fkan@apm.com>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/acpi.h>
128c2ecf20Sopenharmony_ci#include <linux/clk.h>
138c2ecf20Sopenharmony_ci#include <linux/delay.h>
148c2ecf20Sopenharmony_ci#include <linux/hw_random.h>
158c2ecf20Sopenharmony_ci#include <linux/init.h>
168c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
178c2ecf20Sopenharmony_ci#include <linux/module.h>
188c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
198c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
208c2ecf20Sopenharmony_ci#include <linux/of_address.h>
218c2ecf20Sopenharmony_ci#include <linux/timer.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define RNG_MAX_DATUM			4
248c2ecf20Sopenharmony_ci#define MAX_TRY				100
258c2ecf20Sopenharmony_ci#define XGENE_RNG_RETRY_COUNT		20
268c2ecf20Sopenharmony_ci#define XGENE_RNG_RETRY_INTERVAL	10
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* RNG  Registers */
298c2ecf20Sopenharmony_ci#define RNG_INOUT_0			0x00
308c2ecf20Sopenharmony_ci#define RNG_INTR_STS_ACK		0x10
318c2ecf20Sopenharmony_ci#define RNG_CONTROL			0x14
328c2ecf20Sopenharmony_ci#define RNG_CONFIG			0x18
338c2ecf20Sopenharmony_ci#define RNG_ALARMCNT			0x1c
348c2ecf20Sopenharmony_ci#define RNG_FROENABLE			0x20
358c2ecf20Sopenharmony_ci#define RNG_FRODETUNE			0x24
368c2ecf20Sopenharmony_ci#define RNG_ALARMMASK			0x28
378c2ecf20Sopenharmony_ci#define RNG_ALARMSTOP			0x2c
388c2ecf20Sopenharmony_ci#define RNG_OPTIONS			0x78
398c2ecf20Sopenharmony_ci#define RNG_EIP_REV			0x7c
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define MONOBIT_FAIL_MASK		BIT(7)
428c2ecf20Sopenharmony_ci#define POKER_FAIL_MASK			BIT(6)
438c2ecf20Sopenharmony_ci#define LONG_RUN_FAIL_MASK		BIT(5)
448c2ecf20Sopenharmony_ci#define RUN_FAIL_MASK			BIT(4)
458c2ecf20Sopenharmony_ci#define NOISE_FAIL_MASK			BIT(3)
468c2ecf20Sopenharmony_ci#define STUCK_OUT_MASK			BIT(2)
478c2ecf20Sopenharmony_ci#define SHUTDOWN_OFLO_MASK		BIT(1)
488c2ecf20Sopenharmony_ci#define READY_MASK			BIT(0)
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define MAJOR_HW_REV_RD(src)		(((src) & 0x0f000000) >> 24)
518c2ecf20Sopenharmony_ci#define MINOR_HW_REV_RD(src)		(((src) & 0x00f00000) >> 20)
528c2ecf20Sopenharmony_ci#define HW_PATCH_LEVEL_RD(src)		(((src) & 0x000f0000) >> 16)
538c2ecf20Sopenharmony_ci#define MAX_REFILL_CYCLES_SET(dst, src) \
548c2ecf20Sopenharmony_ci			((dst & ~0xffff0000) | (((u32)src << 16) & 0xffff0000))
558c2ecf20Sopenharmony_ci#define MIN_REFILL_CYCLES_SET(dst, src) \
568c2ecf20Sopenharmony_ci			((dst & ~0x000000ff) | (((u32)src) & 0x000000ff))
578c2ecf20Sopenharmony_ci#define ALARM_THRESHOLD_SET(dst, src) \
588c2ecf20Sopenharmony_ci			((dst & ~0x000000ff) | (((u32)src) & 0x000000ff))
598c2ecf20Sopenharmony_ci#define ENABLE_RNG_SET(dst, src) \
608c2ecf20Sopenharmony_ci			((dst & ~BIT(10)) | (((u32)src << 10) & BIT(10)))
618c2ecf20Sopenharmony_ci#define REGSPEC_TEST_MODE_SET(dst, src) \
628c2ecf20Sopenharmony_ci			((dst & ~BIT(8)) | (((u32)src << 8) & BIT(8)))
638c2ecf20Sopenharmony_ci#define MONOBIT_FAIL_MASK_SET(dst, src) \
648c2ecf20Sopenharmony_ci			((dst & ~BIT(7)) | (((u32)src << 7) & BIT(7)))
658c2ecf20Sopenharmony_ci#define POKER_FAIL_MASK_SET(dst, src) \
668c2ecf20Sopenharmony_ci			((dst & ~BIT(6)) | (((u32)src << 6) & BIT(6)))
678c2ecf20Sopenharmony_ci#define LONG_RUN_FAIL_MASK_SET(dst, src) \
688c2ecf20Sopenharmony_ci			((dst & ~BIT(5)) | (((u32)src << 5) & BIT(5)))
698c2ecf20Sopenharmony_ci#define RUN_FAIL_MASK_SET(dst, src) \
708c2ecf20Sopenharmony_ci			((dst & ~BIT(4)) | (((u32)src << 4) & BIT(4)))
718c2ecf20Sopenharmony_ci#define NOISE_FAIL_MASK_SET(dst, src) \
728c2ecf20Sopenharmony_ci			((dst & ~BIT(3)) | (((u32)src << 3) & BIT(3)))
738c2ecf20Sopenharmony_ci#define STUCK_OUT_MASK_SET(dst, src) \
748c2ecf20Sopenharmony_ci			((dst & ~BIT(2)) | (((u32)src << 2) & BIT(2)))
758c2ecf20Sopenharmony_ci#define SHUTDOWN_OFLO_MASK_SET(dst, src) \
768c2ecf20Sopenharmony_ci			((dst & ~BIT(1)) | (((u32)src << 1) & BIT(1)))
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistruct xgene_rng_dev {
798c2ecf20Sopenharmony_ci	u32 irq;
808c2ecf20Sopenharmony_ci	void  __iomem *csr_base;
818c2ecf20Sopenharmony_ci	u32 revision;
828c2ecf20Sopenharmony_ci	u32 datum_size;
838c2ecf20Sopenharmony_ci	u32 failure_cnt;	/* Failure count last minute */
848c2ecf20Sopenharmony_ci	unsigned long failure_ts;/* First failure timestamp */
858c2ecf20Sopenharmony_ci	struct timer_list failure_timer;
868c2ecf20Sopenharmony_ci	struct device *dev;
878c2ecf20Sopenharmony_ci	struct clk *clk;
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic void xgene_rng_expired_timer(struct timer_list *t)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	struct xgene_rng_dev *ctx = from_timer(ctx, t, failure_timer);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	/* Clear failure counter as timer expired */
958c2ecf20Sopenharmony_ci	disable_irq(ctx->irq);
968c2ecf20Sopenharmony_ci	ctx->failure_cnt = 0;
978c2ecf20Sopenharmony_ci	del_timer(&ctx->failure_timer);
988c2ecf20Sopenharmony_ci	enable_irq(ctx->irq);
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic void xgene_rng_start_timer(struct xgene_rng_dev *ctx)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	ctx->failure_timer.expires = jiffies + 120 * HZ;
1048c2ecf20Sopenharmony_ci	add_timer(&ctx->failure_timer);
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/*
1088c2ecf20Sopenharmony_ci * Initialize or reinit free running oscillators (FROs)
1098c2ecf20Sopenharmony_ci */
1108c2ecf20Sopenharmony_cistatic void xgene_rng_init_fro(struct xgene_rng_dev *ctx, u32 fro_val)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	writel(fro_val, ctx->csr_base + RNG_FRODETUNE);
1138c2ecf20Sopenharmony_ci	writel(0x00000000, ctx->csr_base + RNG_ALARMMASK);
1148c2ecf20Sopenharmony_ci	writel(0x00000000, ctx->csr_base + RNG_ALARMSTOP);
1158c2ecf20Sopenharmony_ci	writel(0xFFFFFFFF, ctx->csr_base + RNG_FROENABLE);
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic void xgene_rng_chk_overflow(struct xgene_rng_dev *ctx)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	u32 val;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	val = readl(ctx->csr_base + RNG_INTR_STS_ACK);
1238c2ecf20Sopenharmony_ci	if (val & MONOBIT_FAIL_MASK)
1248c2ecf20Sopenharmony_ci		/*
1258c2ecf20Sopenharmony_ci		 * LFSR detected an out-of-bounds number of 1s after
1268c2ecf20Sopenharmony_ci		 * checking 20,000 bits (test T1 as specified in the
1278c2ecf20Sopenharmony_ci		 * AIS-31 standard)
1288c2ecf20Sopenharmony_ci		 */
1298c2ecf20Sopenharmony_ci		dev_err(ctx->dev, "test monobit failure error 0x%08X\n", val);
1308c2ecf20Sopenharmony_ci	if (val & POKER_FAIL_MASK)
1318c2ecf20Sopenharmony_ci		/*
1328c2ecf20Sopenharmony_ci		 * LFSR detected an out-of-bounds value in at least one
1338c2ecf20Sopenharmony_ci		 * of the 16 poker_count_X counters or an out of bounds sum
1348c2ecf20Sopenharmony_ci		 * of squares value after checking 20,000 bits (test T2 as
1358c2ecf20Sopenharmony_ci		 * specified in the AIS-31 standard)
1368c2ecf20Sopenharmony_ci		 */
1378c2ecf20Sopenharmony_ci		dev_err(ctx->dev, "test poker failure error 0x%08X\n", val);
1388c2ecf20Sopenharmony_ci	if (val & LONG_RUN_FAIL_MASK)
1398c2ecf20Sopenharmony_ci		/*
1408c2ecf20Sopenharmony_ci		 * LFSR detected a sequence of 34 identical bits
1418c2ecf20Sopenharmony_ci		 * (test T4 as specified in the AIS-31 standard)
1428c2ecf20Sopenharmony_ci		 */
1438c2ecf20Sopenharmony_ci		dev_err(ctx->dev, "test long run failure error 0x%08X\n", val);
1448c2ecf20Sopenharmony_ci	if (val & RUN_FAIL_MASK)
1458c2ecf20Sopenharmony_ci		/*
1468c2ecf20Sopenharmony_ci		 * LFSR detected an outof-bounds value for at least one
1478c2ecf20Sopenharmony_ci		 * of the running counters after checking 20,000 bits
1488c2ecf20Sopenharmony_ci		 * (test T3 as specified in the AIS-31 standard)
1498c2ecf20Sopenharmony_ci		 */
1508c2ecf20Sopenharmony_ci		dev_err(ctx->dev, "test run failure error 0x%08X\n", val);
1518c2ecf20Sopenharmony_ci	if (val & NOISE_FAIL_MASK)
1528c2ecf20Sopenharmony_ci		/* LFSR detected a sequence of 48 identical bits */
1538c2ecf20Sopenharmony_ci		dev_err(ctx->dev, "noise failure error 0x%08X\n", val);
1548c2ecf20Sopenharmony_ci	if (val & STUCK_OUT_MASK)
1558c2ecf20Sopenharmony_ci		/*
1568c2ecf20Sopenharmony_ci		 * Detected output data registers generated same value twice
1578c2ecf20Sopenharmony_ci		 * in a row
1588c2ecf20Sopenharmony_ci		 */
1598c2ecf20Sopenharmony_ci		dev_err(ctx->dev, "stuck out failure error 0x%08X\n", val);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	if (val & SHUTDOWN_OFLO_MASK) {
1628c2ecf20Sopenharmony_ci		u32 frostopped;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci		/* FROs shut down after a second error event. Try recover. */
1658c2ecf20Sopenharmony_ci		if (++ctx->failure_cnt == 1) {
1668c2ecf20Sopenharmony_ci			/* 1st time, just recover */
1678c2ecf20Sopenharmony_ci			ctx->failure_ts = jiffies;
1688c2ecf20Sopenharmony_ci			frostopped = readl(ctx->csr_base + RNG_ALARMSTOP);
1698c2ecf20Sopenharmony_ci			xgene_rng_init_fro(ctx, frostopped);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci			/*
1728c2ecf20Sopenharmony_ci			 * We must start a timer to clear out this error
1738c2ecf20Sopenharmony_ci			 * in case the system timer wrap around
1748c2ecf20Sopenharmony_ci			 */
1758c2ecf20Sopenharmony_ci			xgene_rng_start_timer(ctx);
1768c2ecf20Sopenharmony_ci		} else {
1778c2ecf20Sopenharmony_ci			/* 2nd time failure in lesser than 1 minute? */
1788c2ecf20Sopenharmony_ci			if (time_after(ctx->failure_ts + 60 * HZ, jiffies)) {
1798c2ecf20Sopenharmony_ci				dev_err(ctx->dev,
1808c2ecf20Sopenharmony_ci					"FRO shutdown failure error 0x%08X\n",
1818c2ecf20Sopenharmony_ci					val);
1828c2ecf20Sopenharmony_ci			} else {
1838c2ecf20Sopenharmony_ci				/* 2nd time failure after 1 minutes, recover */
1848c2ecf20Sopenharmony_ci				ctx->failure_ts = jiffies;
1858c2ecf20Sopenharmony_ci				ctx->failure_cnt = 1;
1868c2ecf20Sopenharmony_ci				/*
1878c2ecf20Sopenharmony_ci				 * We must start a timer to clear out this
1888c2ecf20Sopenharmony_ci				 * error in case the system timer wrap
1898c2ecf20Sopenharmony_ci				 * around
1908c2ecf20Sopenharmony_ci				 */
1918c2ecf20Sopenharmony_ci				xgene_rng_start_timer(ctx);
1928c2ecf20Sopenharmony_ci			}
1938c2ecf20Sopenharmony_ci			frostopped = readl(ctx->csr_base + RNG_ALARMSTOP);
1948c2ecf20Sopenharmony_ci			xgene_rng_init_fro(ctx, frostopped);
1958c2ecf20Sopenharmony_ci		}
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci	/* Clear them all */
1988c2ecf20Sopenharmony_ci	writel(val, ctx->csr_base + RNG_INTR_STS_ACK);
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic irqreturn_t xgene_rng_irq_handler(int irq, void *id)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	struct xgene_rng_dev *ctx = (struct xgene_rng_dev *) id;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	/* RNG Alarm Counter overflow */
2068c2ecf20Sopenharmony_ci	xgene_rng_chk_overflow(ctx);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cistatic int xgene_rng_data_present(struct hwrng *rng, int wait)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	struct xgene_rng_dev *ctx = (struct xgene_rng_dev *) rng->priv;
2148c2ecf20Sopenharmony_ci	u32 i, val = 0;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	for (i = 0; i < XGENE_RNG_RETRY_COUNT; i++) {
2178c2ecf20Sopenharmony_ci		val = readl(ctx->csr_base + RNG_INTR_STS_ACK);
2188c2ecf20Sopenharmony_ci		if ((val & READY_MASK) || !wait)
2198c2ecf20Sopenharmony_ci			break;
2208c2ecf20Sopenharmony_ci		udelay(XGENE_RNG_RETRY_INTERVAL);
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	return (val & READY_MASK);
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic int xgene_rng_data_read(struct hwrng *rng, u32 *data)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	struct xgene_rng_dev *ctx = (struct xgene_rng_dev *) rng->priv;
2298c2ecf20Sopenharmony_ci	int i;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	for (i = 0; i < ctx->datum_size; i++)
2328c2ecf20Sopenharmony_ci		data[i] = readl(ctx->csr_base + RNG_INOUT_0 + i * 4);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	/* Clear ready bit to start next transaction */
2358c2ecf20Sopenharmony_ci	writel(READY_MASK, ctx->csr_base + RNG_INTR_STS_ACK);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	return ctx->datum_size << 2;
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cistatic void xgene_rng_init_internal(struct xgene_rng_dev *ctx)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	u32 val;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	writel(0x00000000, ctx->csr_base + RNG_CONTROL);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	val = MAX_REFILL_CYCLES_SET(0, 10);
2478c2ecf20Sopenharmony_ci	val = MIN_REFILL_CYCLES_SET(val, 10);
2488c2ecf20Sopenharmony_ci	writel(val, ctx->csr_base + RNG_CONFIG);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	val = ALARM_THRESHOLD_SET(0, 0xFF);
2518c2ecf20Sopenharmony_ci	writel(val, ctx->csr_base + RNG_ALARMCNT);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	xgene_rng_init_fro(ctx, 0);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	writel(MONOBIT_FAIL_MASK |
2568c2ecf20Sopenharmony_ci		POKER_FAIL_MASK	|
2578c2ecf20Sopenharmony_ci		LONG_RUN_FAIL_MASK |
2588c2ecf20Sopenharmony_ci		RUN_FAIL_MASK |
2598c2ecf20Sopenharmony_ci		NOISE_FAIL_MASK |
2608c2ecf20Sopenharmony_ci		STUCK_OUT_MASK |
2618c2ecf20Sopenharmony_ci		SHUTDOWN_OFLO_MASK |
2628c2ecf20Sopenharmony_ci		READY_MASK, ctx->csr_base + RNG_INTR_STS_ACK);
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	val = ENABLE_RNG_SET(0, 1);
2658c2ecf20Sopenharmony_ci	val = MONOBIT_FAIL_MASK_SET(val, 1);
2668c2ecf20Sopenharmony_ci	val = POKER_FAIL_MASK_SET(val, 1);
2678c2ecf20Sopenharmony_ci	val = LONG_RUN_FAIL_MASK_SET(val, 1);
2688c2ecf20Sopenharmony_ci	val = RUN_FAIL_MASK_SET(val, 1);
2698c2ecf20Sopenharmony_ci	val = NOISE_FAIL_MASK_SET(val, 1);
2708c2ecf20Sopenharmony_ci	val = STUCK_OUT_MASK_SET(val, 1);
2718c2ecf20Sopenharmony_ci	val = SHUTDOWN_OFLO_MASK_SET(val, 1);
2728c2ecf20Sopenharmony_ci	writel(val, ctx->csr_base + RNG_CONTROL);
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_cistatic int xgene_rng_init(struct hwrng *rng)
2768c2ecf20Sopenharmony_ci{
2778c2ecf20Sopenharmony_ci	struct xgene_rng_dev *ctx = (struct xgene_rng_dev *) rng->priv;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	ctx->failure_cnt = 0;
2808c2ecf20Sopenharmony_ci	timer_setup(&ctx->failure_timer, xgene_rng_expired_timer, 0);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	ctx->revision = readl(ctx->csr_base + RNG_EIP_REV);
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	dev_dbg(ctx->dev, "Rev %d.%d.%d\n",
2858c2ecf20Sopenharmony_ci		MAJOR_HW_REV_RD(ctx->revision),
2868c2ecf20Sopenharmony_ci		MINOR_HW_REV_RD(ctx->revision),
2878c2ecf20Sopenharmony_ci		HW_PATCH_LEVEL_RD(ctx->revision));
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	dev_dbg(ctx->dev, "Options 0x%08X",
2908c2ecf20Sopenharmony_ci		readl(ctx->csr_base + RNG_OPTIONS));
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	xgene_rng_init_internal(ctx);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	ctx->datum_size = RNG_MAX_DATUM;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	return 0;
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci#ifdef CONFIG_ACPI
3008c2ecf20Sopenharmony_cistatic const struct acpi_device_id xgene_rng_acpi_match[] = {
3018c2ecf20Sopenharmony_ci	{ "APMC0D18", },
3028c2ecf20Sopenharmony_ci	{ }
3038c2ecf20Sopenharmony_ci};
3048c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, xgene_rng_acpi_match);
3058c2ecf20Sopenharmony_ci#endif
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_cistatic struct hwrng xgene_rng_func = {
3088c2ecf20Sopenharmony_ci	.name		= "xgene-rng",
3098c2ecf20Sopenharmony_ci	.init		= xgene_rng_init,
3108c2ecf20Sopenharmony_ci	.data_present	= xgene_rng_data_present,
3118c2ecf20Sopenharmony_ci	.data_read	= xgene_rng_data_read,
3128c2ecf20Sopenharmony_ci};
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic int xgene_rng_probe(struct platform_device *pdev)
3158c2ecf20Sopenharmony_ci{
3168c2ecf20Sopenharmony_ci	struct xgene_rng_dev *ctx;
3178c2ecf20Sopenharmony_ci	int rc = 0;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
3208c2ecf20Sopenharmony_ci	if (!ctx)
3218c2ecf20Sopenharmony_ci		return -ENOMEM;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	ctx->dev = &pdev->dev;
3248c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, ctx);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	ctx->csr_base = devm_platform_ioremap_resource(pdev, 0);
3278c2ecf20Sopenharmony_ci	if (IS_ERR(ctx->csr_base))
3288c2ecf20Sopenharmony_ci		return PTR_ERR(ctx->csr_base);
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	rc = platform_get_irq(pdev, 0);
3318c2ecf20Sopenharmony_ci	if (rc < 0)
3328c2ecf20Sopenharmony_ci		return rc;
3338c2ecf20Sopenharmony_ci	ctx->irq = rc;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	dev_dbg(&pdev->dev, "APM X-Gene RNG BASE %p ALARM IRQ %d",
3368c2ecf20Sopenharmony_ci		ctx->csr_base, ctx->irq);
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	rc = devm_request_irq(&pdev->dev, ctx->irq, xgene_rng_irq_handler, 0,
3398c2ecf20Sopenharmony_ci				dev_name(&pdev->dev), ctx);
3408c2ecf20Sopenharmony_ci	if (rc) {
3418c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Could not request RNG alarm IRQ\n");
3428c2ecf20Sopenharmony_ci		return rc;
3438c2ecf20Sopenharmony_ci	}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	/* Enable IP clock */
3468c2ecf20Sopenharmony_ci	ctx->clk = devm_clk_get(&pdev->dev, NULL);
3478c2ecf20Sopenharmony_ci	if (IS_ERR(ctx->clk)) {
3488c2ecf20Sopenharmony_ci		dev_warn(&pdev->dev, "Couldn't get the clock for RNG\n");
3498c2ecf20Sopenharmony_ci	} else {
3508c2ecf20Sopenharmony_ci		rc = clk_prepare_enable(ctx->clk);
3518c2ecf20Sopenharmony_ci		if (rc) {
3528c2ecf20Sopenharmony_ci			dev_warn(&pdev->dev,
3538c2ecf20Sopenharmony_ci				 "clock prepare enable failed for RNG");
3548c2ecf20Sopenharmony_ci			return rc;
3558c2ecf20Sopenharmony_ci		}
3568c2ecf20Sopenharmony_ci	}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	xgene_rng_func.priv = (unsigned long) ctx;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	rc = devm_hwrng_register(&pdev->dev, &xgene_rng_func);
3618c2ecf20Sopenharmony_ci	if (rc) {
3628c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "RNG registering failed error %d\n", rc);
3638c2ecf20Sopenharmony_ci		if (!IS_ERR(ctx->clk))
3648c2ecf20Sopenharmony_ci			clk_disable_unprepare(ctx->clk);
3658c2ecf20Sopenharmony_ci		return rc;
3668c2ecf20Sopenharmony_ci	}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	rc = device_init_wakeup(&pdev->dev, 1);
3698c2ecf20Sopenharmony_ci	if (rc) {
3708c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "RNG device_init_wakeup failed error %d\n",
3718c2ecf20Sopenharmony_ci			rc);
3728c2ecf20Sopenharmony_ci		if (!IS_ERR(ctx->clk))
3738c2ecf20Sopenharmony_ci			clk_disable_unprepare(ctx->clk);
3748c2ecf20Sopenharmony_ci		return rc;
3758c2ecf20Sopenharmony_ci	}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	return 0;
3788c2ecf20Sopenharmony_ci}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_cistatic int xgene_rng_remove(struct platform_device *pdev)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci	struct xgene_rng_dev *ctx = platform_get_drvdata(pdev);
3838c2ecf20Sopenharmony_ci	int rc;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	rc = device_init_wakeup(&pdev->dev, 0);
3868c2ecf20Sopenharmony_ci	if (rc)
3878c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "RNG init wakeup failed error %d\n", rc);
3888c2ecf20Sopenharmony_ci	if (!IS_ERR(ctx->clk))
3898c2ecf20Sopenharmony_ci		clk_disable_unprepare(ctx->clk);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	return rc;
3928c2ecf20Sopenharmony_ci}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_cistatic const struct of_device_id xgene_rng_of_match[] = {
3958c2ecf20Sopenharmony_ci	{ .compatible = "apm,xgene-rng" },
3968c2ecf20Sopenharmony_ci	{ }
3978c2ecf20Sopenharmony_ci};
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, xgene_rng_of_match);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistatic struct platform_driver xgene_rng_driver = {
4028c2ecf20Sopenharmony_ci	.probe = xgene_rng_probe,
4038c2ecf20Sopenharmony_ci	.remove	= xgene_rng_remove,
4048c2ecf20Sopenharmony_ci	.driver = {
4058c2ecf20Sopenharmony_ci		.name		= "xgene-rng",
4068c2ecf20Sopenharmony_ci		.of_match_table = xgene_rng_of_match,
4078c2ecf20Sopenharmony_ci		.acpi_match_table = ACPI_PTR(xgene_rng_acpi_match),
4088c2ecf20Sopenharmony_ci	},
4098c2ecf20Sopenharmony_ci};
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_cimodule_platform_driver(xgene_rng_driver);
4128c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("APM X-Gene RNG driver");
4138c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
414