1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * sun4i-ss-core.c - hardware cryptographic accelerator for Allwinner A20 SoC
5 * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com>
7 * Core file which registers crypto algorithms supported by the SS.
9 * You could find a link for the datasheet in Documentation/arm/sunxi.rst
11 #include <linux/clk.h>
12 #include <linux/crypto.h>
14 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <crypto/scatterwalk.h>
19 #include <linux/scatterlist.h>
20 #include <linux/interrupt.h>
21 #include <linux/delay.h>
22 #include <linux/reset.h>
26 static const struct ss_variant ss_a10_variant
= {
30 static const struct ss_variant ss_a33_variant
= {
34 static struct sun4i_ss_alg_template ss_algs
[] = {
35 { .type
= CRYPTO_ALG_TYPE_AHASH
,
38 .init
= sun4i_hash_init
,
39 .update
= sun4i_hash_update
,
40 .final
= sun4i_hash_final
,
41 .finup
= sun4i_hash_finup
,
42 .digest
= sun4i_hash_digest
,
43 .export
= sun4i_hash_export_md5
,
44 .import
= sun4i_hash_import_md5
,
46 .digestsize
= MD5_DIGEST_SIZE
,
47 .statesize
= sizeof(struct md5_state
),
50 .cra_driver_name
= "md5-sun4i-ss",
53 .cra_blocksize
= MD5_HMAC_BLOCK_SIZE
,
54 .cra_ctxsize
= sizeof(struct sun4i_req_ctx
),
55 .cra_module
= THIS_MODULE
,
56 .cra_init
= sun4i_hash_crainit
,
57 .cra_exit
= sun4i_hash_craexit
,
62 { .type
= CRYPTO_ALG_TYPE_AHASH
,
65 .init
= sun4i_hash_init
,
66 .update
= sun4i_hash_update
,
67 .final
= sun4i_hash_final
,
68 .finup
= sun4i_hash_finup
,
69 .digest
= sun4i_hash_digest
,
70 .export
= sun4i_hash_export_sha1
,
71 .import
= sun4i_hash_import_sha1
,
73 .digestsize
= SHA1_DIGEST_SIZE
,
74 .statesize
= sizeof(struct sha1_state
),
77 .cra_driver_name
= "sha1-sun4i-ss",
80 .cra_blocksize
= SHA1_BLOCK_SIZE
,
81 .cra_ctxsize
= sizeof(struct sun4i_req_ctx
),
82 .cra_module
= THIS_MODULE
,
83 .cra_init
= sun4i_hash_crainit
,
84 .cra_exit
= sun4i_hash_craexit
,
89 { .type
= CRYPTO_ALG_TYPE_SKCIPHER
,
91 .setkey
= sun4i_ss_aes_setkey
,
92 .encrypt
= sun4i_ss_cbc_aes_encrypt
,
93 .decrypt
= sun4i_ss_cbc_aes_decrypt
,
94 .min_keysize
= AES_MIN_KEY_SIZE
,
95 .max_keysize
= AES_MAX_KEY_SIZE
,
96 .ivsize
= AES_BLOCK_SIZE
,
98 .cra_name
= "cbc(aes)",
99 .cra_driver_name
= "cbc-aes-sun4i-ss",
101 .cra_blocksize
= AES_BLOCK_SIZE
,
102 .cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
| CRYPTO_ALG_NEED_FALLBACK
,
103 .cra_ctxsize
= sizeof(struct sun4i_tfm_ctx
),
104 .cra_module
= THIS_MODULE
,
106 .cra_init
= sun4i_ss_cipher_init
,
107 .cra_exit
= sun4i_ss_cipher_exit
,
111 { .type
= CRYPTO_ALG_TYPE_SKCIPHER
,
113 .setkey
= sun4i_ss_aes_setkey
,
114 .encrypt
= sun4i_ss_ecb_aes_encrypt
,
115 .decrypt
= sun4i_ss_ecb_aes_decrypt
,
116 .min_keysize
= AES_MIN_KEY_SIZE
,
117 .max_keysize
= AES_MAX_KEY_SIZE
,
119 .cra_name
= "ecb(aes)",
120 .cra_driver_name
= "ecb-aes-sun4i-ss",
122 .cra_blocksize
= AES_BLOCK_SIZE
,
123 .cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
| CRYPTO_ALG_NEED_FALLBACK
,
124 .cra_ctxsize
= sizeof(struct sun4i_tfm_ctx
),
125 .cra_module
= THIS_MODULE
,
127 .cra_init
= sun4i_ss_cipher_init
,
128 .cra_exit
= sun4i_ss_cipher_exit
,
132 { .type
= CRYPTO_ALG_TYPE_SKCIPHER
,
134 .setkey
= sun4i_ss_des_setkey
,
135 .encrypt
= sun4i_ss_cbc_des_encrypt
,
136 .decrypt
= sun4i_ss_cbc_des_decrypt
,
137 .min_keysize
= DES_KEY_SIZE
,
138 .max_keysize
= DES_KEY_SIZE
,
139 .ivsize
= DES_BLOCK_SIZE
,
141 .cra_name
= "cbc(des)",
142 .cra_driver_name
= "cbc-des-sun4i-ss",
144 .cra_blocksize
= DES_BLOCK_SIZE
,
145 .cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
| CRYPTO_ALG_NEED_FALLBACK
,
146 .cra_ctxsize
= sizeof(struct sun4i_req_ctx
),
147 .cra_module
= THIS_MODULE
,
149 .cra_init
= sun4i_ss_cipher_init
,
150 .cra_exit
= sun4i_ss_cipher_exit
,
154 { .type
= CRYPTO_ALG_TYPE_SKCIPHER
,
156 .setkey
= sun4i_ss_des_setkey
,
157 .encrypt
= sun4i_ss_ecb_des_encrypt
,
158 .decrypt
= sun4i_ss_ecb_des_decrypt
,
159 .min_keysize
= DES_KEY_SIZE
,
160 .max_keysize
= DES_KEY_SIZE
,
162 .cra_name
= "ecb(des)",
163 .cra_driver_name
= "ecb-des-sun4i-ss",
165 .cra_blocksize
= DES_BLOCK_SIZE
,
166 .cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
| CRYPTO_ALG_NEED_FALLBACK
,
167 .cra_ctxsize
= sizeof(struct sun4i_req_ctx
),
168 .cra_module
= THIS_MODULE
,
170 .cra_init
= sun4i_ss_cipher_init
,
171 .cra_exit
= sun4i_ss_cipher_exit
,
175 { .type
= CRYPTO_ALG_TYPE_SKCIPHER
,
177 .setkey
= sun4i_ss_des3_setkey
,
178 .encrypt
= sun4i_ss_cbc_des3_encrypt
,
179 .decrypt
= sun4i_ss_cbc_des3_decrypt
,
180 .min_keysize
= DES3_EDE_KEY_SIZE
,
181 .max_keysize
= DES3_EDE_KEY_SIZE
,
182 .ivsize
= DES3_EDE_BLOCK_SIZE
,
184 .cra_name
= "cbc(des3_ede)",
185 .cra_driver_name
= "cbc-des3-sun4i-ss",
187 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
188 .cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
| CRYPTO_ALG_NEED_FALLBACK
,
189 .cra_ctxsize
= sizeof(struct sun4i_req_ctx
),
190 .cra_module
= THIS_MODULE
,
192 .cra_init
= sun4i_ss_cipher_init
,
193 .cra_exit
= sun4i_ss_cipher_exit
,
197 { .type
= CRYPTO_ALG_TYPE_SKCIPHER
,
199 .setkey
= sun4i_ss_des3_setkey
,
200 .encrypt
= sun4i_ss_ecb_des3_encrypt
,
201 .decrypt
= sun4i_ss_ecb_des3_decrypt
,
202 .min_keysize
= DES3_EDE_KEY_SIZE
,
203 .max_keysize
= DES3_EDE_KEY_SIZE
,
205 .cra_name
= "ecb(des3_ede)",
206 .cra_driver_name
= "ecb-des3-sun4i-ss",
208 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
209 .cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
| CRYPTO_ALG_NEED_FALLBACK
,
210 .cra_ctxsize
= sizeof(struct sun4i_req_ctx
),
211 .cra_module
= THIS_MODULE
,
213 .cra_init
= sun4i_ss_cipher_init
,
214 .cra_exit
= sun4i_ss_cipher_exit
,
218 #ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_PRNG
220 .type
= CRYPTO_ALG_TYPE_RNG
,
223 .cra_name
= "stdrng",
224 .cra_driver_name
= "sun4i_ss_rng",
227 .cra_module
= THIS_MODULE
,
229 .generate
= sun4i_ss_prng_generate
,
230 .seed
= sun4i_ss_prng_seed
,
231 .seedsize
= SS_SEED_LEN
/ BITS_PER_BYTE
,
238 * Power management strategy: The device is suspended unless a TFM exists for
239 * one of the algorithms proposed by this driver.
241 static int sun4i_ss_pm_suspend(struct device
*dev
)
243 struct sun4i_ss_ctx
*ss
= dev_get_drvdata(dev
);
246 reset_control_assert(ss
->reset
);
248 clk_disable_unprepare(ss
->ssclk
);
249 clk_disable_unprepare(ss
->busclk
);
253 static int sun4i_ss_pm_resume(struct device
*dev
)
255 struct sun4i_ss_ctx
*ss
= dev_get_drvdata(dev
);
259 err
= clk_prepare_enable(ss
->busclk
);
261 dev_err(ss
->dev
, "Cannot prepare_enable busclk\n");
265 err
= clk_prepare_enable(ss
->ssclk
);
267 dev_err(ss
->dev
, "Cannot prepare_enable ssclk\n");
272 err
= reset_control_deassert(ss
->reset
);
274 dev_err(ss
->dev
, "Cannot deassert reset control\n");
281 sun4i_ss_pm_suspend(dev
);
285 static const struct dev_pm_ops sun4i_ss_pm_ops
= {
286 SET_RUNTIME_PM_OPS(sun4i_ss_pm_suspend
, sun4i_ss_pm_resume
, NULL
)
290 * When power management is enabled, this function enables the PM and set the
291 * device as suspended
292 * When power management is disabled, this function just enables the device
294 static int sun4i_ss_pm_init(struct sun4i_ss_ctx
*ss
)
298 pm_runtime_use_autosuspend(ss
->dev
);
299 pm_runtime_set_autosuspend_delay(ss
->dev
, 2000);
301 err
= pm_runtime_set_suspended(ss
->dev
);
304 pm_runtime_enable(ss
->dev
);
308 static void sun4i_ss_pm_exit(struct sun4i_ss_ctx
*ss
)
310 pm_runtime_disable(ss
->dev
);
313 static int sun4i_ss_probe(struct platform_device
*pdev
)
318 const unsigned long cr_ahb
= 24 * 1000 * 1000;
319 const unsigned long cr_mod
= 150 * 1000 * 1000;
320 struct sun4i_ss_ctx
*ss
;
322 if (!pdev
->dev
.of_node
)
325 ss
= devm_kzalloc(&pdev
->dev
, sizeof(*ss
), GFP_KERNEL
);
329 ss
->base
= devm_platform_ioremap_resource(pdev
, 0);
330 if (IS_ERR(ss
->base
)) {
331 dev_err(&pdev
->dev
, "Cannot request MMIO\n");
332 return PTR_ERR(ss
->base
);
335 ss
->variant
= of_device_get_match_data(&pdev
->dev
);
337 dev_err(&pdev
->dev
, "Missing Security System variant\n");
341 ss
->ssclk
= devm_clk_get(&pdev
->dev
, "mod");
342 if (IS_ERR(ss
->ssclk
)) {
343 err
= PTR_ERR(ss
->ssclk
);
344 dev_err(&pdev
->dev
, "Cannot get SS clock err=%d\n", err
);
347 dev_dbg(&pdev
->dev
, "clock ss acquired\n");
349 ss
->busclk
= devm_clk_get(&pdev
->dev
, "ahb");
350 if (IS_ERR(ss
->busclk
)) {
351 err
= PTR_ERR(ss
->busclk
);
352 dev_err(&pdev
->dev
, "Cannot get AHB SS clock err=%d\n", err
);
355 dev_dbg(&pdev
->dev
, "clock ahb_ss acquired\n");
357 ss
->reset
= devm_reset_control_get_optional(&pdev
->dev
, "ahb");
358 if (IS_ERR(ss
->reset
)) {
359 if (PTR_ERR(ss
->reset
) == -EPROBE_DEFER
)
360 return PTR_ERR(ss
->reset
);
361 dev_info(&pdev
->dev
, "no reset control found\n");
366 * Check that clock have the correct rates given in the datasheet
367 * Try to set the clock to the maximum allowed
369 err
= clk_set_rate(ss
->ssclk
, cr_mod
);
371 dev_err(&pdev
->dev
, "Cannot set clock rate to ssclk\n");
376 * The only impact on clocks below requirement are bad performance,
377 * so do not print "errors"
378 * warn on Overclocked clocks
380 cr
= clk_get_rate(ss
->busclk
);
382 dev_dbg(&pdev
->dev
, "Clock bus %lu (%lu MHz) (must be >= %lu)\n",
383 cr
, cr
/ 1000000, cr_ahb
);
385 dev_warn(&pdev
->dev
, "Clock bus %lu (%lu MHz) (must be >= %lu)\n",
386 cr
, cr
/ 1000000, cr_ahb
);
388 cr
= clk_get_rate(ss
->ssclk
);
391 dev_warn(&pdev
->dev
, "Clock ss %lu (%lu MHz) (must be <= %lu)\n",
392 cr
, cr
/ 1000000, cr_mod
);
394 dev_dbg(&pdev
->dev
, "Clock ss %lu (%lu MHz) (must be <= %lu)\n",
395 cr
, cr
/ 1000000, cr_mod
);
397 dev_warn(&pdev
->dev
, "Clock ss is at %lu (%lu MHz) (must be <= %lu)\n",
398 cr
, cr
/ 1000000, cr_mod
);
400 ss
->dev
= &pdev
->dev
;
401 platform_set_drvdata(pdev
, ss
);
403 spin_lock_init(&ss
->slock
);
405 err
= sun4i_ss_pm_init(ss
);
410 * Datasheet named it "Die Bonding ID"
411 * I expect to be a sort of Security System Revision number.
412 * Since the A80 seems to have an other version of SS
413 * this info could be useful
416 err
= pm_runtime_get_sync(ss
->dev
);
420 writel(SS_ENABLED
, ss
->base
+ SS_CTL
);
421 v
= readl(ss
->base
+ SS_CTL
);
424 dev_info(&pdev
->dev
, "Die ID %d\n", v
);
425 writel(0, ss
->base
+ SS_CTL
);
427 pm_runtime_put_sync(ss
->dev
);
429 for (i
= 0; i
< ARRAY_SIZE(ss_algs
); i
++) {
431 switch (ss_algs
[i
].type
) {
432 case CRYPTO_ALG_TYPE_SKCIPHER
:
433 err
= crypto_register_skcipher(&ss_algs
[i
].alg
.crypto
);
435 dev_err(ss
->dev
, "Fail to register %s\n",
436 ss_algs
[i
].alg
.crypto
.base
.cra_name
);
440 case CRYPTO_ALG_TYPE_AHASH
:
441 err
= crypto_register_ahash(&ss_algs
[i
].alg
.hash
);
443 dev_err(ss
->dev
, "Fail to register %s\n",
444 ss_algs
[i
].alg
.hash
.halg
.base
.cra_name
);
448 case CRYPTO_ALG_TYPE_RNG
:
449 err
= crypto_register_rng(&ss_algs
[i
].alg
.rng
);
451 dev_err(ss
->dev
, "Fail to register %s\n",
452 ss_algs
[i
].alg
.rng
.base
.cra_name
);
460 for (; i
>= 0; i
--) {
461 switch (ss_algs
[i
].type
) {
462 case CRYPTO_ALG_TYPE_SKCIPHER
:
463 crypto_unregister_skcipher(&ss_algs
[i
].alg
.crypto
);
465 case CRYPTO_ALG_TYPE_AHASH
:
466 crypto_unregister_ahash(&ss_algs
[i
].alg
.hash
);
468 case CRYPTO_ALG_TYPE_RNG
:
469 crypto_unregister_rng(&ss_algs
[i
].alg
.rng
);
474 sun4i_ss_pm_exit(ss
);
478 static int sun4i_ss_remove(struct platform_device
*pdev
)
481 struct sun4i_ss_ctx
*ss
= platform_get_drvdata(pdev
);
483 for (i
= 0; i
< ARRAY_SIZE(ss_algs
); i
++) {
484 switch (ss_algs
[i
].type
) {
485 case CRYPTO_ALG_TYPE_SKCIPHER
:
486 crypto_unregister_skcipher(&ss_algs
[i
].alg
.crypto
);
488 case CRYPTO_ALG_TYPE_AHASH
:
489 crypto_unregister_ahash(&ss_algs
[i
].alg
.hash
);
491 case CRYPTO_ALG_TYPE_RNG
:
492 crypto_unregister_rng(&ss_algs
[i
].alg
.rng
);
497 sun4i_ss_pm_exit(ss
);
501 static const struct of_device_id a20ss_crypto_of_match_table
[] = {
502 { .compatible
= "allwinner,sun4i-a10-crypto",
503 .data
= &ss_a10_variant
505 { .compatible
= "allwinner,sun8i-a33-crypto",
506 .data
= &ss_a33_variant
510 MODULE_DEVICE_TABLE(of
, a20ss_crypto_of_match_table
);
512 static struct platform_driver sun4i_ss_driver
= {
513 .probe
= sun4i_ss_probe
,
514 .remove
= sun4i_ss_remove
,
517 .pm
= &sun4i_ss_pm_ops
,
518 .of_match_table
= a20ss_crypto_of_match_table
,
522 module_platform_driver(sun4i_ss_driver
);
524 MODULE_ALIAS("platform:sun4i-ss");
525 MODULE_DESCRIPTION("Allwinner Security System cryptographic accelerator");
526 MODULE_LICENSE("GPL");
527 MODULE_AUTHOR("Corentin LABBE <clabbe.montjoie@gmail.com>");