1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017-18 Linaro Limited
4 // Based on msm-rng.c and downstream driver
6 #include <crypto/internal/rng.h>
7 #include <linux/acpi.h>
9 #include <linux/crypto.h>
10 #include <linux/hw_random.h>
12 #include <linux/iopoll.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
18 /* Device specific register offsets */
19 #define PRNG_DATA_OUT 0x0000
20 #define PRNG_STATUS 0x0004
21 #define PRNG_LFSR_CFG 0x0100
22 #define PRNG_CONFIG 0x0104
24 /* Device specific register masks and config values */
25 #define PRNG_LFSR_CFG_MASK 0x0000ffff
26 #define PRNG_LFSR_CFG_CLOCKS 0x0000dddd
27 #define PRNG_CONFIG_HW_ENABLE BIT(1)
28 #define PRNG_STATUS_DATA_AVAIL BIT(0)
32 #define QCOM_TRNG_QUALITY 1024
39 struct qcom_rng_match_data
*match_data
;
46 struct qcom_rng_match_data
{
51 static struct qcom_rng
*qcom_rng_dev
;
53 static int qcom_rng_read(struct qcom_rng
*rng
, u8
*data
, unsigned int max
)
55 unsigned int currsize
= 0;
59 /* read random data from hardware */
61 ret
= readl_poll_timeout(rng
->base
+ PRNG_STATUS
, val
,
62 val
& PRNG_STATUS_DATA_AVAIL
,
67 val
= readl_relaxed(rng
->base
+ PRNG_DATA_OUT
);
71 if ((max
- currsize
) >= WORD_SZ
) {
72 memcpy(data
, &val
, WORD_SZ
);
76 /* copy only remaining bytes */
77 memcpy(data
, &val
, max
- currsize
);
80 } while (currsize
< max
);
85 static int qcom_rng_generate(struct crypto_rng
*tfm
,
86 const u8
*src
, unsigned int slen
,
87 u8
*dstn
, unsigned int dlen
)
89 struct qcom_rng_ctx
*ctx
= crypto_rng_ctx(tfm
);
90 struct qcom_rng
*rng
= ctx
->rng
;
93 ret
= clk_prepare_enable(rng
->clk
);
97 mutex_lock(&rng
->lock
);
99 ret
= qcom_rng_read(rng
, dstn
, dlen
);
101 mutex_unlock(&rng
->lock
);
102 clk_disable_unprepare(rng
->clk
);
110 static int qcom_rng_seed(struct crypto_rng
*tfm
, const u8
*seed
,
116 static int qcom_hwrng_read(struct hwrng
*hwrng
, void *data
, size_t max
, bool wait
)
118 struct qcom_rng
*qrng
= container_of(hwrng
, struct qcom_rng
, hwrng
);
120 return qcom_rng_read(qrng
, data
, max
);
123 static int qcom_rng_enable(struct qcom_rng
*rng
)
128 ret
= clk_prepare_enable(rng
->clk
);
132 /* Enable PRNG only if it is not already enabled */
133 val
= readl_relaxed(rng
->base
+ PRNG_CONFIG
);
134 if (val
& PRNG_CONFIG_HW_ENABLE
)
135 goto already_enabled
;
137 val
= readl_relaxed(rng
->base
+ PRNG_LFSR_CFG
);
138 val
&= ~PRNG_LFSR_CFG_MASK
;
139 val
|= PRNG_LFSR_CFG_CLOCKS
;
140 writel(val
, rng
->base
+ PRNG_LFSR_CFG
);
142 val
= readl_relaxed(rng
->base
+ PRNG_CONFIG
);
143 val
|= PRNG_CONFIG_HW_ENABLE
;
144 writel(val
, rng
->base
+ PRNG_CONFIG
);
147 clk_disable_unprepare(rng
->clk
);
152 static int qcom_rng_init(struct crypto_tfm
*tfm
)
154 struct qcom_rng_ctx
*ctx
= crypto_tfm_ctx(tfm
);
156 ctx
->rng
= qcom_rng_dev
;
158 if (!ctx
->rng
->match_data
->skip_init
)
159 return qcom_rng_enable(ctx
->rng
);
164 static struct rng_alg qcom_rng_alg
= {
165 .generate
= qcom_rng_generate
,
166 .seed
= qcom_rng_seed
,
169 .cra_name
= "stdrng",
170 .cra_driver_name
= "qcom-rng",
171 .cra_flags
= CRYPTO_ALG_TYPE_RNG
,
173 .cra_ctxsize
= sizeof(struct qcom_rng_ctx
),
174 .cra_module
= THIS_MODULE
,
175 .cra_init
= qcom_rng_init
,
179 static int qcom_rng_probe(struct platform_device
*pdev
)
181 struct qcom_rng
*rng
;
184 rng
= devm_kzalloc(&pdev
->dev
, sizeof(*rng
), GFP_KERNEL
);
188 platform_set_drvdata(pdev
, rng
);
189 mutex_init(&rng
->lock
);
191 rng
->base
= devm_platform_ioremap_resource(pdev
, 0);
192 if (IS_ERR(rng
->base
))
193 return PTR_ERR(rng
->base
);
195 rng
->clk
= devm_clk_get_optional(&pdev
->dev
, "core");
196 if (IS_ERR(rng
->clk
))
197 return PTR_ERR(rng
->clk
);
199 rng
->match_data
= (struct qcom_rng_match_data
*)device_get_match_data(&pdev
->dev
);
202 ret
= crypto_register_rng(&qcom_rng_alg
);
204 dev_err(&pdev
->dev
, "Register crypto rng failed: %d\n", ret
);
209 if (rng
->match_data
->hwrng_support
) {
210 rng
->hwrng
.name
= "qcom_hwrng";
211 rng
->hwrng
.read
= qcom_hwrng_read
;
212 rng
->hwrng
.quality
= QCOM_TRNG_QUALITY
;
213 ret
= devm_hwrng_register(&pdev
->dev
, &rng
->hwrng
);
215 dev_err(&pdev
->dev
, "Register hwrng failed: %d\n", ret
);
223 crypto_unregister_rng(&qcom_rng_alg
);
227 static void qcom_rng_remove(struct platform_device
*pdev
)
229 crypto_unregister_rng(&qcom_rng_alg
);
234 static struct qcom_rng_match_data qcom_prng_match_data
= {
236 .hwrng_support
= false,
239 static struct qcom_rng_match_data qcom_prng_ee_match_data
= {
241 .hwrng_support
= false,
244 static struct qcom_rng_match_data qcom_trng_match_data
= {
246 .hwrng_support
= true,
249 static const struct acpi_device_id __maybe_unused qcom_rng_acpi_match
[] = {
250 { .id
= "QCOM8160", .driver_data
= (kernel_ulong_t
)&qcom_prng_ee_match_data
},
253 MODULE_DEVICE_TABLE(acpi
, qcom_rng_acpi_match
);
255 static const struct of_device_id __maybe_unused qcom_rng_of_match
[] = {
256 { .compatible
= "qcom,prng", .data
= &qcom_prng_match_data
},
257 { .compatible
= "qcom,prng-ee", .data
= &qcom_prng_ee_match_data
},
258 { .compatible
= "qcom,trng", .data
= &qcom_trng_match_data
},
261 MODULE_DEVICE_TABLE(of
, qcom_rng_of_match
);
263 static struct platform_driver qcom_rng_driver
= {
264 .probe
= qcom_rng_probe
,
265 .remove
= qcom_rng_remove
,
267 .name
= KBUILD_MODNAME
,
268 .of_match_table
= of_match_ptr(qcom_rng_of_match
),
269 .acpi_match_table
= ACPI_PTR(qcom_rng_acpi_match
),
272 module_platform_driver(qcom_rng_driver
);
274 MODULE_ALIAS("platform:" KBUILD_MODNAME
);
275 MODULE_DESCRIPTION("Qualcomm random number generator driver");
276 MODULE_LICENSE("GPL v2");