xref: /OK3568_Linux_fs/kernel/drivers/crypto/amcc/crypto4xx_trng.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Generic PowerPC 44x RNG driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2011 IBM Corporation
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/module.h>
9*4882a593Smuzhiyun #include <linux/kernel.h>
10*4882a593Smuzhiyun #include <linux/interrupt.h>
11*4882a593Smuzhiyun #include <linux/platform_device.h>
12*4882a593Smuzhiyun #include <linux/hw_random.h>
13*4882a593Smuzhiyun #include <linux/delay.h>
14*4882a593Smuzhiyun #include <linux/of_address.h>
15*4882a593Smuzhiyun #include <linux/of_platform.h>
16*4882a593Smuzhiyun #include <linux/io.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include "crypto4xx_core.h"
19*4882a593Smuzhiyun #include "crypto4xx_trng.h"
20*4882a593Smuzhiyun #include "crypto4xx_reg_def.h"
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define PPC4XX_TRNG_CTRL	0x0008
23*4882a593Smuzhiyun #define PPC4XX_TRNG_CTRL_DALM	0x20
24*4882a593Smuzhiyun #define PPC4XX_TRNG_STAT	0x0004
25*4882a593Smuzhiyun #define PPC4XX_TRNG_STAT_B	0x1
26*4882a593Smuzhiyun #define PPC4XX_TRNG_DATA	0x0000
27*4882a593Smuzhiyun 
ppc4xx_trng_data_present(struct hwrng * rng,int wait)28*4882a593Smuzhiyun static int ppc4xx_trng_data_present(struct hwrng *rng, int wait)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	struct crypto4xx_device *dev = (void *)rng->priv;
31*4882a593Smuzhiyun 	int busy, i, present = 0;
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	for (i = 0; i < 20; i++) {
34*4882a593Smuzhiyun 		busy = (in_le32(dev->trng_base + PPC4XX_TRNG_STAT) &
35*4882a593Smuzhiyun 			PPC4XX_TRNG_STAT_B);
36*4882a593Smuzhiyun 		if (!busy || !wait) {
37*4882a593Smuzhiyun 			present = 1;
38*4882a593Smuzhiyun 			break;
39*4882a593Smuzhiyun 		}
40*4882a593Smuzhiyun 		udelay(10);
41*4882a593Smuzhiyun 	}
42*4882a593Smuzhiyun 	return present;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun 
ppc4xx_trng_data_read(struct hwrng * rng,u32 * data)45*4882a593Smuzhiyun static int ppc4xx_trng_data_read(struct hwrng *rng, u32 *data)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	struct crypto4xx_device *dev = (void *)rng->priv;
48*4882a593Smuzhiyun 	*data = in_le32(dev->trng_base + PPC4XX_TRNG_DATA);
49*4882a593Smuzhiyun 	return 4;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun 
ppc4xx_trng_enable(struct crypto4xx_device * dev,bool enable)52*4882a593Smuzhiyun static void ppc4xx_trng_enable(struct crypto4xx_device *dev, bool enable)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	u32 device_ctrl;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	device_ctrl = readl(dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
57*4882a593Smuzhiyun 	if (enable)
58*4882a593Smuzhiyun 		device_ctrl |= PPC4XX_TRNG_EN;
59*4882a593Smuzhiyun 	else
60*4882a593Smuzhiyun 		device_ctrl &= ~PPC4XX_TRNG_EN;
61*4882a593Smuzhiyun 	writel(device_ctrl, dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun static const struct of_device_id ppc4xx_trng_match[] = {
65*4882a593Smuzhiyun 	{ .compatible = "ppc4xx-rng", },
66*4882a593Smuzhiyun 	{ .compatible = "amcc,ppc460ex-rng", },
67*4882a593Smuzhiyun 	{ .compatible = "amcc,ppc440epx-rng", },
68*4882a593Smuzhiyun 	{},
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun 
ppc4xx_trng_probe(struct crypto4xx_core_device * core_dev)71*4882a593Smuzhiyun void ppc4xx_trng_probe(struct crypto4xx_core_device *core_dev)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	struct crypto4xx_device *dev = core_dev->dev;
74*4882a593Smuzhiyun 	struct device_node *trng = NULL;
75*4882a593Smuzhiyun 	struct hwrng *rng = NULL;
76*4882a593Smuzhiyun 	int err;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	/* Find the TRNG device node and map it */
79*4882a593Smuzhiyun 	trng = of_find_matching_node(NULL, ppc4xx_trng_match);
80*4882a593Smuzhiyun 	if (!trng || !of_device_is_available(trng)) {
81*4882a593Smuzhiyun 		of_node_put(trng);
82*4882a593Smuzhiyun 		return;
83*4882a593Smuzhiyun 	}
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	dev->trng_base = of_iomap(trng, 0);
86*4882a593Smuzhiyun 	of_node_put(trng);
87*4882a593Smuzhiyun 	if (!dev->trng_base)
88*4882a593Smuzhiyun 		goto err_out;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	rng = kzalloc(sizeof(*rng), GFP_KERNEL);
91*4882a593Smuzhiyun 	if (!rng)
92*4882a593Smuzhiyun 		goto err_out;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	rng->name = KBUILD_MODNAME;
95*4882a593Smuzhiyun 	rng->data_present = ppc4xx_trng_data_present;
96*4882a593Smuzhiyun 	rng->data_read = ppc4xx_trng_data_read;
97*4882a593Smuzhiyun 	rng->priv = (unsigned long) dev;
98*4882a593Smuzhiyun 	core_dev->trng = rng;
99*4882a593Smuzhiyun 	ppc4xx_trng_enable(dev, true);
100*4882a593Smuzhiyun 	out_le32(dev->trng_base + PPC4XX_TRNG_CTRL, PPC4XX_TRNG_CTRL_DALM);
101*4882a593Smuzhiyun 	err = devm_hwrng_register(core_dev->device, core_dev->trng);
102*4882a593Smuzhiyun 	if (err) {
103*4882a593Smuzhiyun 		ppc4xx_trng_enable(dev, false);
104*4882a593Smuzhiyun 		dev_err(core_dev->device, "failed to register hwrng (%d).\n",
105*4882a593Smuzhiyun 			err);
106*4882a593Smuzhiyun 		goto err_out;
107*4882a593Smuzhiyun 	}
108*4882a593Smuzhiyun 	return;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun err_out:
111*4882a593Smuzhiyun 	iounmap(dev->trng_base);
112*4882a593Smuzhiyun 	kfree(rng);
113*4882a593Smuzhiyun 	dev->trng_base = NULL;
114*4882a593Smuzhiyun 	core_dev->trng = NULL;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
ppc4xx_trng_remove(struct crypto4xx_core_device * core_dev)117*4882a593Smuzhiyun void ppc4xx_trng_remove(struct crypto4xx_core_device *core_dev)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun 	if (core_dev && core_dev->trng) {
120*4882a593Smuzhiyun 		struct crypto4xx_device *dev = core_dev->dev;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 		devm_hwrng_unregister(core_dev->device, core_dev->trng);
123*4882a593Smuzhiyun 		ppc4xx_trng_enable(dev, false);
124*4882a593Smuzhiyun 		iounmap(dev->trng_base);
125*4882a593Smuzhiyun 		kfree(core_dev->trng);
126*4882a593Smuzhiyun 	}
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun MODULE_ALIAS("ppc4xx_rng");
130