xref: /openbmc/linux/drivers/crypto/qcom-rng.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1ceec5f5bSVinod Koul // SPDX-License-Identifier: GPL-2.0
2ceec5f5bSVinod Koul // Copyright (c) 2017-18 Linaro Limited
3ceec5f5bSVinod Koul //
4ceec5f5bSVinod Koul // Based on msm-rng.c and downstream driver
5ceec5f5bSVinod Koul 
6ceec5f5bSVinod Koul #include <crypto/internal/rng.h>
7d96542acSTimur Tabi #include <linux/acpi.h>
8ceec5f5bSVinod Koul #include <linux/clk.h>
9ceec5f5bSVinod Koul #include <linux/crypto.h>
100c3dc787SHerbert Xu #include <linux/io.h>
11a680b183SBrian Masney #include <linux/iopoll.h>
12882aa652SHerbert Xu #include <linux/kernel.h>
13ceec5f5bSVinod Koul #include <linux/module.h>
14ceec5f5bSVinod Koul #include <linux/of.h>
15ceec5f5bSVinod Koul #include <linux/platform_device.h>
16ceec5f5bSVinod Koul 
17ceec5f5bSVinod Koul /* Device specific register offsets */
18ceec5f5bSVinod Koul #define PRNG_DATA_OUT		0x0000
19ceec5f5bSVinod Koul #define PRNG_STATUS		0x0004
20ceec5f5bSVinod Koul #define PRNG_LFSR_CFG		0x0100
21ceec5f5bSVinod Koul #define PRNG_CONFIG		0x0104
22ceec5f5bSVinod Koul 
23ceec5f5bSVinod Koul /* Device specific register masks and config values */
24ceec5f5bSVinod Koul #define PRNG_LFSR_CFG_MASK	0x0000ffff
25ceec5f5bSVinod Koul #define PRNG_LFSR_CFG_CLOCKS	0x0000dddd
26ceec5f5bSVinod Koul #define PRNG_CONFIG_HW_ENABLE	BIT(1)
27ceec5f5bSVinod Koul #define PRNG_STATUS_DATA_AVAIL	BIT(0)
28ceec5f5bSVinod Koul 
29ceec5f5bSVinod Koul #define WORD_SZ			4
30ceec5f5bSVinod Koul 
31ceec5f5bSVinod Koul struct qcom_rng {
32ceec5f5bSVinod Koul 	struct mutex lock;
33ceec5f5bSVinod Koul 	void __iomem *base;
34ceec5f5bSVinod Koul 	struct clk *clk;
35ba3ab637SVinod Koul 	unsigned int skip_init;
36ceec5f5bSVinod Koul };
37ceec5f5bSVinod Koul 
38ceec5f5bSVinod Koul struct qcom_rng_ctx {
39ceec5f5bSVinod Koul 	struct qcom_rng *rng;
40ceec5f5bSVinod Koul };
41ceec5f5bSVinod Koul 
42ceec5f5bSVinod Koul static struct qcom_rng *qcom_rng_dev;
43ceec5f5bSVinod Koul 
qcom_rng_read(struct qcom_rng * rng,u8 * data,unsigned int max)44ceec5f5bSVinod Koul static int qcom_rng_read(struct qcom_rng *rng, u8 *data, unsigned int max)
45ceec5f5bSVinod Koul {
46ceec5f5bSVinod Koul 	unsigned int currsize = 0;
47ceec5f5bSVinod Koul 	u32 val;
48a680b183SBrian Masney 	int ret;
49ceec5f5bSVinod Koul 
50ceec5f5bSVinod Koul 	/* read random data from hardware */
51ceec5f5bSVinod Koul 	do {
52a680b183SBrian Masney 		ret = readl_poll_timeout(rng->base + PRNG_STATUS, val,
53a680b183SBrian Masney 					 val & PRNG_STATUS_DATA_AVAIL,
54a680b183SBrian Masney 					 200, 10000);
55a680b183SBrian Masney 		if (ret)
56a680b183SBrian Masney 			return ret;
57ceec5f5bSVinod Koul 
58ceec5f5bSVinod Koul 		val = readl_relaxed(rng->base + PRNG_DATA_OUT);
59ceec5f5bSVinod Koul 		if (!val)
60a680b183SBrian Masney 			return -EINVAL;
61ceec5f5bSVinod Koul 
62ceec5f5bSVinod Koul 		if ((max - currsize) >= WORD_SZ) {
63ceec5f5bSVinod Koul 			memcpy(data, &val, WORD_SZ);
64ceec5f5bSVinod Koul 			data += WORD_SZ;
65ceec5f5bSVinod Koul 			currsize += WORD_SZ;
66ceec5f5bSVinod Koul 		} else {
67ceec5f5bSVinod Koul 			/* copy only remaining bytes */
68ceec5f5bSVinod Koul 			memcpy(data, &val, max - currsize);
6916287397SOndrej Mosnacek 			break;
70ceec5f5bSVinod Koul 		}
71ceec5f5bSVinod Koul 	} while (currsize < max);
72ceec5f5bSVinod Koul 
73a680b183SBrian Masney 	return 0;
74ceec5f5bSVinod Koul }
75ceec5f5bSVinod Koul 
qcom_rng_generate(struct crypto_rng * tfm,const u8 * src,unsigned int slen,u8 * dstn,unsigned int dlen)76ceec5f5bSVinod Koul static int qcom_rng_generate(struct crypto_rng *tfm,
77ceec5f5bSVinod Koul 			     const u8 *src, unsigned int slen,
78ceec5f5bSVinod Koul 			     u8 *dstn, unsigned int dlen)
79ceec5f5bSVinod Koul {
80ceec5f5bSVinod Koul 	struct qcom_rng_ctx *ctx = crypto_rng_ctx(tfm);
81ceec5f5bSVinod Koul 	struct qcom_rng *rng = ctx->rng;
82ceec5f5bSVinod Koul 	int ret;
83ceec5f5bSVinod Koul 
84ceec5f5bSVinod Koul 	ret = clk_prepare_enable(rng->clk);
85ceec5f5bSVinod Koul 	if (ret)
86ceec5f5bSVinod Koul 		return ret;
87ceec5f5bSVinod Koul 
88ceec5f5bSVinod Koul 	mutex_lock(&rng->lock);
89ceec5f5bSVinod Koul 
90ceec5f5bSVinod Koul 	ret = qcom_rng_read(rng, dstn, dlen);
91ceec5f5bSVinod Koul 
92ceec5f5bSVinod Koul 	mutex_unlock(&rng->lock);
93ceec5f5bSVinod Koul 	clk_disable_unprepare(rng->clk);
94ceec5f5bSVinod Koul 
95a680b183SBrian Masney 	return ret;
96ceec5f5bSVinod Koul }
97ceec5f5bSVinod Koul 
qcom_rng_seed(struct crypto_rng * tfm,const u8 * seed,unsigned int slen)98ceec5f5bSVinod Koul static int qcom_rng_seed(struct crypto_rng *tfm, const u8 *seed,
99ceec5f5bSVinod Koul 			 unsigned int slen)
100ceec5f5bSVinod Koul {
101ceec5f5bSVinod Koul 	return 0;
102ceec5f5bSVinod Koul }
103ceec5f5bSVinod Koul 
qcom_rng_enable(struct qcom_rng * rng)104ceec5f5bSVinod Koul static int qcom_rng_enable(struct qcom_rng *rng)
105ceec5f5bSVinod Koul {
106ceec5f5bSVinod Koul 	u32 val;
107ceec5f5bSVinod Koul 	int ret;
108ceec5f5bSVinod Koul 
109ceec5f5bSVinod Koul 	ret = clk_prepare_enable(rng->clk);
110ceec5f5bSVinod Koul 	if (ret)
111ceec5f5bSVinod Koul 		return ret;
112ceec5f5bSVinod Koul 
113ceec5f5bSVinod Koul 	/* Enable PRNG only if it is not already enabled */
114ceec5f5bSVinod Koul 	val = readl_relaxed(rng->base + PRNG_CONFIG);
115ceec5f5bSVinod Koul 	if (val & PRNG_CONFIG_HW_ENABLE)
116ceec5f5bSVinod Koul 		goto already_enabled;
117ceec5f5bSVinod Koul 
118ceec5f5bSVinod Koul 	val = readl_relaxed(rng->base + PRNG_LFSR_CFG);
119ceec5f5bSVinod Koul 	val &= ~PRNG_LFSR_CFG_MASK;
120ceec5f5bSVinod Koul 	val |= PRNG_LFSR_CFG_CLOCKS;
121ceec5f5bSVinod Koul 	writel(val, rng->base + PRNG_LFSR_CFG);
122ceec5f5bSVinod Koul 
123ceec5f5bSVinod Koul 	val = readl_relaxed(rng->base + PRNG_CONFIG);
124ceec5f5bSVinod Koul 	val |= PRNG_CONFIG_HW_ENABLE;
125ceec5f5bSVinod Koul 	writel(val, rng->base + PRNG_CONFIG);
126ceec5f5bSVinod Koul 
127ceec5f5bSVinod Koul already_enabled:
128ceec5f5bSVinod Koul 	clk_disable_unprepare(rng->clk);
129ceec5f5bSVinod Koul 
130ceec5f5bSVinod Koul 	return 0;
131ceec5f5bSVinod Koul }
132ceec5f5bSVinod Koul 
qcom_rng_init(struct crypto_tfm * tfm)133ceec5f5bSVinod Koul static int qcom_rng_init(struct crypto_tfm *tfm)
134ceec5f5bSVinod Koul {
135ceec5f5bSVinod Koul 	struct qcom_rng_ctx *ctx = crypto_tfm_ctx(tfm);
136ceec5f5bSVinod Koul 
137ceec5f5bSVinod Koul 	ctx->rng = qcom_rng_dev;
138ceec5f5bSVinod Koul 
139ba3ab637SVinod Koul 	if (!ctx->rng->skip_init)
140ceec5f5bSVinod Koul 		return qcom_rng_enable(ctx->rng);
141ba3ab637SVinod Koul 
142ba3ab637SVinod Koul 	return 0;
143ceec5f5bSVinod Koul }
144ceec5f5bSVinod Koul 
145ceec5f5bSVinod Koul static struct rng_alg qcom_rng_alg = {
146ceec5f5bSVinod Koul 	.generate	= qcom_rng_generate,
147ceec5f5bSVinod Koul 	.seed		= qcom_rng_seed,
148ceec5f5bSVinod Koul 	.seedsize	= 0,
149ceec5f5bSVinod Koul 	.base		= {
150ceec5f5bSVinod Koul 		.cra_name		= "stdrng",
151ceec5f5bSVinod Koul 		.cra_driver_name	= "qcom-rng",
152ceec5f5bSVinod Koul 		.cra_flags		= CRYPTO_ALG_TYPE_RNG,
153ceec5f5bSVinod Koul 		.cra_priority		= 300,
154ceec5f5bSVinod Koul 		.cra_ctxsize		= sizeof(struct qcom_rng_ctx),
155ceec5f5bSVinod Koul 		.cra_module		= THIS_MODULE,
156ceec5f5bSVinod Koul 		.cra_init		= qcom_rng_init,
157ceec5f5bSVinod Koul 	}
158ceec5f5bSVinod Koul };
159ceec5f5bSVinod Koul 
qcom_rng_probe(struct platform_device * pdev)160ceec5f5bSVinod Koul static int qcom_rng_probe(struct platform_device *pdev)
161ceec5f5bSVinod Koul {
162ceec5f5bSVinod Koul 	struct qcom_rng *rng;
163ceec5f5bSVinod Koul 	int ret;
164ceec5f5bSVinod Koul 
165ceec5f5bSVinod Koul 	rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
166ceec5f5bSVinod Koul 	if (!rng)
167ceec5f5bSVinod Koul 		return -ENOMEM;
168ceec5f5bSVinod Koul 
169ceec5f5bSVinod Koul 	platform_set_drvdata(pdev, rng);
170ceec5f5bSVinod Koul 	mutex_init(&rng->lock);
171ceec5f5bSVinod Koul 
1722229c740SYueHaibing 	rng->base = devm_platform_ioremap_resource(pdev, 0);
173ceec5f5bSVinod Koul 	if (IS_ERR(rng->base))
174ceec5f5bSVinod Koul 		return PTR_ERR(rng->base);
175ceec5f5bSVinod Koul 
176*cdb8b7e1SKonrad Dybcio 	rng->clk = devm_clk_get_optional(&pdev->dev, "core");
177ceec5f5bSVinod Koul 	if (IS_ERR(rng->clk))
178ceec5f5bSVinod Koul 		return PTR_ERR(rng->clk);
179ceec5f5bSVinod Koul 
180ba3ab637SVinod Koul 	rng->skip_init = (unsigned long)device_get_match_data(&pdev->dev);
181ba3ab637SVinod Koul 
182ceec5f5bSVinod Koul 	qcom_rng_dev = rng;
183ceec5f5bSVinod Koul 	ret = crypto_register_rng(&qcom_rng_alg);
184ceec5f5bSVinod Koul 	if (ret) {
185ceec5f5bSVinod Koul 		dev_err(&pdev->dev, "Register crypto rng failed: %d\n", ret);
186ceec5f5bSVinod Koul 		qcom_rng_dev = NULL;
187ceec5f5bSVinod Koul 	}
188ceec5f5bSVinod Koul 
189ceec5f5bSVinod Koul 	return ret;
190ceec5f5bSVinod Koul }
191ceec5f5bSVinod Koul 
qcom_rng_remove(struct platform_device * pdev)192ceec5f5bSVinod Koul static int qcom_rng_remove(struct platform_device *pdev)
193ceec5f5bSVinod Koul {
194ceec5f5bSVinod Koul 	crypto_unregister_rng(&qcom_rng_alg);
195ceec5f5bSVinod Koul 
196ceec5f5bSVinod Koul 	qcom_rng_dev = NULL;
197ceec5f5bSVinod Koul 
198ceec5f5bSVinod Koul 	return 0;
199ceec5f5bSVinod Koul }
200ceec5f5bSVinod Koul 
201882aa652SHerbert Xu static const struct acpi_device_id __maybe_unused qcom_rng_acpi_match[] = {
202d96542acSTimur Tabi 	{ .id = "QCOM8160", .driver_data = 1 },
203d96542acSTimur Tabi 	{}
204d96542acSTimur Tabi };
205d96542acSTimur Tabi MODULE_DEVICE_TABLE(acpi, qcom_rng_acpi_match);
206d96542acSTimur Tabi 
207882aa652SHerbert Xu static const struct of_device_id __maybe_unused qcom_rng_of_match[] = {
208ba3ab637SVinod Koul 	{ .compatible = "qcom,prng", .data = (void *)0},
209ba3ab637SVinod Koul 	{ .compatible = "qcom,prng-ee", .data = (void *)1},
210ceec5f5bSVinod Koul 	{}
211ceec5f5bSVinod Koul };
212ceec5f5bSVinod Koul MODULE_DEVICE_TABLE(of, qcom_rng_of_match);
213ceec5f5bSVinod Koul 
214ceec5f5bSVinod Koul static struct platform_driver qcom_rng_driver = {
215ceec5f5bSVinod Koul 	.probe = qcom_rng_probe,
216ceec5f5bSVinod Koul 	.remove =  qcom_rng_remove,
217ceec5f5bSVinod Koul 	.driver = {
218ceec5f5bSVinod Koul 		.name = KBUILD_MODNAME,
219ceec5f5bSVinod Koul 		.of_match_table = of_match_ptr(qcom_rng_of_match),
220d96542acSTimur Tabi 		.acpi_match_table = ACPI_PTR(qcom_rng_acpi_match),
221ceec5f5bSVinod Koul 	}
222ceec5f5bSVinod Koul };
223ceec5f5bSVinod Koul module_platform_driver(qcom_rng_driver);
224ceec5f5bSVinod Koul 
225ceec5f5bSVinod Koul MODULE_ALIAS("platform:" KBUILD_MODNAME);
226ceec5f5bSVinod Koul MODULE_DESCRIPTION("Qualcomm random number generator driver");
227ceec5f5bSVinod Koul MODULE_LICENSE("GPL v2");
228