xref: /openbmc/linux/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-core.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
117513547SCorentin Labbe // SPDX-License-Identifier: GPL-2.0-or-later
217513547SCorentin Labbe /*
317513547SCorentin Labbe  * sun4i-ss-core.c - hardware cryptographic accelerator for Allwinner A20 SoC
417513547SCorentin Labbe  *
517513547SCorentin Labbe  * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com>
617513547SCorentin Labbe  *
717513547SCorentin Labbe  * Core file which registers crypto algorithms supported by the SS.
817513547SCorentin Labbe  *
9*39db3f15SJonathan Corbet  * You could find a link for the datasheet in Documentation/arch/arm/sunxi.rst
1017513547SCorentin Labbe  */
1117513547SCorentin Labbe #include <linux/clk.h>
1217513547SCorentin Labbe #include <linux/crypto.h>
13b1f578b8SCorentin Labbe #include <linux/debugfs.h>
1417513547SCorentin Labbe #include <linux/io.h>
1517513547SCorentin Labbe #include <linux/module.h>
1617513547SCorentin Labbe #include <linux/of.h>
1717513547SCorentin Labbe #include <linux/platform_device.h>
1817513547SCorentin Labbe #include <crypto/scatterwalk.h>
1917513547SCorentin Labbe #include <linux/scatterlist.h>
2017513547SCorentin Labbe #include <linux/interrupt.h>
2117513547SCorentin Labbe #include <linux/delay.h>
2217513547SCorentin Labbe #include <linux/reset.h>
2317513547SCorentin Labbe 
2417513547SCorentin Labbe #include "sun4i-ss.h"
2517513547SCorentin Labbe 
261e02e6fbSCorentin Labbe static const struct ss_variant ss_a10_variant = {
271e02e6fbSCorentin Labbe 	.sha1_in_be = false,
281e02e6fbSCorentin Labbe };
291e02e6fbSCorentin Labbe 
301e02e6fbSCorentin Labbe static const struct ss_variant ss_a33_variant = {
311e02e6fbSCorentin Labbe 	.sha1_in_be = true,
321e02e6fbSCorentin Labbe };
331e02e6fbSCorentin Labbe 
3417513547SCorentin Labbe static struct sun4i_ss_alg_template ss_algs[] = {
3517513547SCorentin Labbe {       .type = CRYPTO_ALG_TYPE_AHASH,
3617513547SCorentin Labbe 	.mode = SS_OP_MD5,
3717513547SCorentin Labbe 	.alg.hash = {
3817513547SCorentin Labbe 		.init = sun4i_hash_init,
3917513547SCorentin Labbe 		.update = sun4i_hash_update,
4017513547SCorentin Labbe 		.final = sun4i_hash_final,
4117513547SCorentin Labbe 		.finup = sun4i_hash_finup,
4217513547SCorentin Labbe 		.digest = sun4i_hash_digest,
4317513547SCorentin Labbe 		.export = sun4i_hash_export_md5,
4417513547SCorentin Labbe 		.import = sun4i_hash_import_md5,
4517513547SCorentin Labbe 		.halg = {
4617513547SCorentin Labbe 			.digestsize = MD5_DIGEST_SIZE,
4717513547SCorentin Labbe 			.statesize = sizeof(struct md5_state),
4817513547SCorentin Labbe 			.base = {
4917513547SCorentin Labbe 				.cra_name = "md5",
5017513547SCorentin Labbe 				.cra_driver_name = "md5-sun4i-ss",
5117513547SCorentin Labbe 				.cra_priority = 300,
5217513547SCorentin Labbe 				.cra_alignmask = 3,
5317513547SCorentin Labbe 				.cra_blocksize = MD5_HMAC_BLOCK_SIZE,
5417513547SCorentin Labbe 				.cra_ctxsize = sizeof(struct sun4i_req_ctx),
5517513547SCorentin Labbe 				.cra_module = THIS_MODULE,
5617513547SCorentin Labbe 				.cra_init = sun4i_hash_crainit,
5717513547SCorentin Labbe 				.cra_exit = sun4i_hash_craexit,
5817513547SCorentin Labbe 			}
5917513547SCorentin Labbe 		}
6017513547SCorentin Labbe 	}
6117513547SCorentin Labbe },
6217513547SCorentin Labbe {       .type = CRYPTO_ALG_TYPE_AHASH,
6317513547SCorentin Labbe 	.mode = SS_OP_SHA1,
6417513547SCorentin Labbe 	.alg.hash = {
6517513547SCorentin Labbe 		.init = sun4i_hash_init,
6617513547SCorentin Labbe 		.update = sun4i_hash_update,
6717513547SCorentin Labbe 		.final = sun4i_hash_final,
6817513547SCorentin Labbe 		.finup = sun4i_hash_finup,
6917513547SCorentin Labbe 		.digest = sun4i_hash_digest,
7017513547SCorentin Labbe 		.export = sun4i_hash_export_sha1,
7117513547SCorentin Labbe 		.import = sun4i_hash_import_sha1,
7217513547SCorentin Labbe 		.halg = {
7317513547SCorentin Labbe 			.digestsize = SHA1_DIGEST_SIZE,
7417513547SCorentin Labbe 			.statesize = sizeof(struct sha1_state),
7517513547SCorentin Labbe 			.base = {
7617513547SCorentin Labbe 				.cra_name = "sha1",
7717513547SCorentin Labbe 				.cra_driver_name = "sha1-sun4i-ss",
7817513547SCorentin Labbe 				.cra_priority = 300,
7917513547SCorentin Labbe 				.cra_alignmask = 3,
8017513547SCorentin Labbe 				.cra_blocksize = SHA1_BLOCK_SIZE,
8117513547SCorentin Labbe 				.cra_ctxsize = sizeof(struct sun4i_req_ctx),
8217513547SCorentin Labbe 				.cra_module = THIS_MODULE,
8317513547SCorentin Labbe 				.cra_init = sun4i_hash_crainit,
8417513547SCorentin Labbe 				.cra_exit = sun4i_hash_craexit,
8517513547SCorentin Labbe 			}
8617513547SCorentin Labbe 		}
8717513547SCorentin Labbe 	}
8817513547SCorentin Labbe },
8917513547SCorentin Labbe {       .type = CRYPTO_ALG_TYPE_SKCIPHER,
9017513547SCorentin Labbe 	.alg.crypto = {
9117513547SCorentin Labbe 		.setkey         = sun4i_ss_aes_setkey,
9217513547SCorentin Labbe 		.encrypt        = sun4i_ss_cbc_aes_encrypt,
9317513547SCorentin Labbe 		.decrypt        = sun4i_ss_cbc_aes_decrypt,
9417513547SCorentin Labbe 		.min_keysize	= AES_MIN_KEY_SIZE,
9517513547SCorentin Labbe 		.max_keysize	= AES_MAX_KEY_SIZE,
9617513547SCorentin Labbe 		.ivsize		= AES_BLOCK_SIZE,
9717513547SCorentin Labbe 		.base = {
9817513547SCorentin Labbe 			.cra_name = "cbc(aes)",
9917513547SCorentin Labbe 			.cra_driver_name = "cbc-aes-sun4i-ss",
10017513547SCorentin Labbe 			.cra_priority = 300,
10117513547SCorentin Labbe 			.cra_blocksize = AES_BLOCK_SIZE,
10217513547SCorentin Labbe 			.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_NEED_FALLBACK,
10317513547SCorentin Labbe 			.cra_ctxsize = sizeof(struct sun4i_tfm_ctx),
10417513547SCorentin Labbe 			.cra_module = THIS_MODULE,
10517513547SCorentin Labbe 			.cra_alignmask = 3,
10617513547SCorentin Labbe 			.cra_init = sun4i_ss_cipher_init,
10717513547SCorentin Labbe 			.cra_exit = sun4i_ss_cipher_exit,
10817513547SCorentin Labbe 		}
10917513547SCorentin Labbe 	}
11017513547SCorentin Labbe },
11117513547SCorentin Labbe {       .type = CRYPTO_ALG_TYPE_SKCIPHER,
11217513547SCorentin Labbe 	.alg.crypto = {
11317513547SCorentin Labbe 		.setkey         = sun4i_ss_aes_setkey,
11417513547SCorentin Labbe 		.encrypt        = sun4i_ss_ecb_aes_encrypt,
11517513547SCorentin Labbe 		.decrypt        = sun4i_ss_ecb_aes_decrypt,
11617513547SCorentin Labbe 		.min_keysize	= AES_MIN_KEY_SIZE,
11717513547SCorentin Labbe 		.max_keysize	= AES_MAX_KEY_SIZE,
11817513547SCorentin Labbe 		.base = {
11917513547SCorentin Labbe 			.cra_name = "ecb(aes)",
12017513547SCorentin Labbe 			.cra_driver_name = "ecb-aes-sun4i-ss",
12117513547SCorentin Labbe 			.cra_priority = 300,
12217513547SCorentin Labbe 			.cra_blocksize = AES_BLOCK_SIZE,
12317513547SCorentin Labbe 			.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_NEED_FALLBACK,
12417513547SCorentin Labbe 			.cra_ctxsize = sizeof(struct sun4i_tfm_ctx),
12517513547SCorentin Labbe 			.cra_module = THIS_MODULE,
12617513547SCorentin Labbe 			.cra_alignmask = 3,
12717513547SCorentin Labbe 			.cra_init = sun4i_ss_cipher_init,
12817513547SCorentin Labbe 			.cra_exit = sun4i_ss_cipher_exit,
12917513547SCorentin Labbe 		}
13017513547SCorentin Labbe 	}
13117513547SCorentin Labbe },
13217513547SCorentin Labbe {       .type = CRYPTO_ALG_TYPE_SKCIPHER,
13317513547SCorentin Labbe 	.alg.crypto = {
13417513547SCorentin Labbe 		.setkey         = sun4i_ss_des_setkey,
13517513547SCorentin Labbe 		.encrypt        = sun4i_ss_cbc_des_encrypt,
13617513547SCorentin Labbe 		.decrypt        = sun4i_ss_cbc_des_decrypt,
13717513547SCorentin Labbe 		.min_keysize    = DES_KEY_SIZE,
13817513547SCorentin Labbe 		.max_keysize    = DES_KEY_SIZE,
13917513547SCorentin Labbe 		.ivsize         = DES_BLOCK_SIZE,
14017513547SCorentin Labbe 		.base = {
14117513547SCorentin Labbe 			.cra_name = "cbc(des)",
14217513547SCorentin Labbe 			.cra_driver_name = "cbc-des-sun4i-ss",
14317513547SCorentin Labbe 			.cra_priority = 300,
14417513547SCorentin Labbe 			.cra_blocksize = DES_BLOCK_SIZE,
14517513547SCorentin Labbe 			.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_NEED_FALLBACK,
14617513547SCorentin Labbe 			.cra_ctxsize = sizeof(struct sun4i_req_ctx),
14717513547SCorentin Labbe 			.cra_module = THIS_MODULE,
14817513547SCorentin Labbe 			.cra_alignmask = 3,
14917513547SCorentin Labbe 			.cra_init = sun4i_ss_cipher_init,
15017513547SCorentin Labbe 			.cra_exit = sun4i_ss_cipher_exit,
15117513547SCorentin Labbe 		}
15217513547SCorentin Labbe 	}
15317513547SCorentin Labbe },
15417513547SCorentin Labbe {       .type = CRYPTO_ALG_TYPE_SKCIPHER,
15517513547SCorentin Labbe 	.alg.crypto = {
15617513547SCorentin Labbe 		.setkey         = sun4i_ss_des_setkey,
15717513547SCorentin Labbe 		.encrypt        = sun4i_ss_ecb_des_encrypt,
15817513547SCorentin Labbe 		.decrypt        = sun4i_ss_ecb_des_decrypt,
15917513547SCorentin Labbe 		.min_keysize    = DES_KEY_SIZE,
16017513547SCorentin Labbe 		.max_keysize    = DES_KEY_SIZE,
16117513547SCorentin Labbe 		.base = {
16217513547SCorentin Labbe 			.cra_name = "ecb(des)",
16317513547SCorentin Labbe 			.cra_driver_name = "ecb-des-sun4i-ss",
16417513547SCorentin Labbe 			.cra_priority = 300,
16517513547SCorentin Labbe 			.cra_blocksize = DES_BLOCK_SIZE,
16617513547SCorentin Labbe 			.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_NEED_FALLBACK,
16717513547SCorentin Labbe 			.cra_ctxsize = sizeof(struct sun4i_req_ctx),
16817513547SCorentin Labbe 			.cra_module = THIS_MODULE,
16917513547SCorentin Labbe 			.cra_alignmask = 3,
17017513547SCorentin Labbe 			.cra_init = sun4i_ss_cipher_init,
17117513547SCorentin Labbe 			.cra_exit = sun4i_ss_cipher_exit,
17217513547SCorentin Labbe 		}
17317513547SCorentin Labbe 	}
17417513547SCorentin Labbe },
17517513547SCorentin Labbe {       .type = CRYPTO_ALG_TYPE_SKCIPHER,
17617513547SCorentin Labbe 	.alg.crypto = {
17717513547SCorentin Labbe 		.setkey         = sun4i_ss_des3_setkey,
17817513547SCorentin Labbe 		.encrypt        = sun4i_ss_cbc_des3_encrypt,
17917513547SCorentin Labbe 		.decrypt        = sun4i_ss_cbc_des3_decrypt,
18017513547SCorentin Labbe 		.min_keysize    = DES3_EDE_KEY_SIZE,
18117513547SCorentin Labbe 		.max_keysize    = DES3_EDE_KEY_SIZE,
18217513547SCorentin Labbe 		.ivsize         = DES3_EDE_BLOCK_SIZE,
18317513547SCorentin Labbe 		.base = {
18417513547SCorentin Labbe 			.cra_name = "cbc(des3_ede)",
18517513547SCorentin Labbe 			.cra_driver_name = "cbc-des3-sun4i-ss",
18617513547SCorentin Labbe 			.cra_priority = 300,
18717513547SCorentin Labbe 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
18817513547SCorentin Labbe 			.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_NEED_FALLBACK,
18917513547SCorentin Labbe 			.cra_ctxsize = sizeof(struct sun4i_req_ctx),
19017513547SCorentin Labbe 			.cra_module = THIS_MODULE,
19117513547SCorentin Labbe 			.cra_alignmask = 3,
19217513547SCorentin Labbe 			.cra_init = sun4i_ss_cipher_init,
19317513547SCorentin Labbe 			.cra_exit = sun4i_ss_cipher_exit,
19417513547SCorentin Labbe 		}
19517513547SCorentin Labbe 	}
19617513547SCorentin Labbe },
19717513547SCorentin Labbe {       .type = CRYPTO_ALG_TYPE_SKCIPHER,
19817513547SCorentin Labbe 	.alg.crypto = {
19917513547SCorentin Labbe 		.setkey         = sun4i_ss_des3_setkey,
20017513547SCorentin Labbe 		.encrypt        = sun4i_ss_ecb_des3_encrypt,
20117513547SCorentin Labbe 		.decrypt        = sun4i_ss_ecb_des3_decrypt,
20217513547SCorentin Labbe 		.min_keysize    = DES3_EDE_KEY_SIZE,
20317513547SCorentin Labbe 		.max_keysize    = DES3_EDE_KEY_SIZE,
20417513547SCorentin Labbe 		.base = {
20517513547SCorentin Labbe 			.cra_name = "ecb(des3_ede)",
20617513547SCorentin Labbe 			.cra_driver_name = "ecb-des3-sun4i-ss",
20717513547SCorentin Labbe 			.cra_priority = 300,
20817513547SCorentin Labbe 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
20917513547SCorentin Labbe 			.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_NEED_FALLBACK,
21017513547SCorentin Labbe 			.cra_ctxsize = sizeof(struct sun4i_req_ctx),
21117513547SCorentin Labbe 			.cra_module = THIS_MODULE,
21217513547SCorentin Labbe 			.cra_alignmask = 3,
21317513547SCorentin Labbe 			.cra_init = sun4i_ss_cipher_init,
21417513547SCorentin Labbe 			.cra_exit = sun4i_ss_cipher_exit,
21517513547SCorentin Labbe 		}
21617513547SCorentin Labbe 	}
21717513547SCorentin Labbe },
21817513547SCorentin Labbe #ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_PRNG
21917513547SCorentin Labbe {
22017513547SCorentin Labbe 	.type = CRYPTO_ALG_TYPE_RNG,
22117513547SCorentin Labbe 	.alg.rng = {
22217513547SCorentin Labbe 		.base = {
22317513547SCorentin Labbe 			.cra_name		= "stdrng",
22417513547SCorentin Labbe 			.cra_driver_name	= "sun4i_ss_rng",
22517513547SCorentin Labbe 			.cra_priority		= 300,
22617513547SCorentin Labbe 			.cra_ctxsize		= 0,
22717513547SCorentin Labbe 			.cra_module		= THIS_MODULE,
22817513547SCorentin Labbe 		},
22917513547SCorentin Labbe 		.generate               = sun4i_ss_prng_generate,
23017513547SCorentin Labbe 		.seed                   = sun4i_ss_prng_seed,
23117513547SCorentin Labbe 		.seedsize               = SS_SEED_LEN / BITS_PER_BYTE,
23217513547SCorentin Labbe 	}
23317513547SCorentin Labbe },
23417513547SCorentin Labbe #endif
23517513547SCorentin Labbe };
23617513547SCorentin Labbe 
sun4i_ss_debugfs_show(struct seq_file * seq,void * v)237b21dc631SLiu Shixin static int sun4i_ss_debugfs_show(struct seq_file *seq, void *v)
238b1f578b8SCorentin Labbe {
239b1f578b8SCorentin Labbe 	unsigned int i;
240b1f578b8SCorentin Labbe 
241b1f578b8SCorentin Labbe 	for (i = 0; i < ARRAY_SIZE(ss_algs); i++) {
242b1f578b8SCorentin Labbe 		if (!ss_algs[i].ss)
243b1f578b8SCorentin Labbe 			continue;
244b1f578b8SCorentin Labbe 		switch (ss_algs[i].type) {
245b1f578b8SCorentin Labbe 		case CRYPTO_ALG_TYPE_SKCIPHER:
246b1f578b8SCorentin Labbe 			seq_printf(seq, "%s %s reqs=%lu opti=%lu fallback=%lu tsize=%lu\n",
247b1f578b8SCorentin Labbe 				   ss_algs[i].alg.crypto.base.cra_driver_name,
248b1f578b8SCorentin Labbe 				   ss_algs[i].alg.crypto.base.cra_name,
249b1f578b8SCorentin Labbe 				   ss_algs[i].stat_req, ss_algs[i].stat_opti, ss_algs[i].stat_fb,
250b1f578b8SCorentin Labbe 				   ss_algs[i].stat_bytes);
251b1f578b8SCorentin Labbe 			break;
252b1f578b8SCorentin Labbe 		case CRYPTO_ALG_TYPE_RNG:
253b1f578b8SCorentin Labbe 			seq_printf(seq, "%s %s reqs=%lu tsize=%lu\n",
254b1f578b8SCorentin Labbe 				   ss_algs[i].alg.rng.base.cra_driver_name,
255b1f578b8SCorentin Labbe 				   ss_algs[i].alg.rng.base.cra_name,
256b1f578b8SCorentin Labbe 				   ss_algs[i].stat_req, ss_algs[i].stat_bytes);
257b1f578b8SCorentin Labbe 			break;
258b1f578b8SCorentin Labbe 		case CRYPTO_ALG_TYPE_AHASH:
259b1f578b8SCorentin Labbe 			seq_printf(seq, "%s %s reqs=%lu\n",
260b1f578b8SCorentin Labbe 				   ss_algs[i].alg.hash.halg.base.cra_driver_name,
261b1f578b8SCorentin Labbe 				   ss_algs[i].alg.hash.halg.base.cra_name,
262b1f578b8SCorentin Labbe 				   ss_algs[i].stat_req);
263b1f578b8SCorentin Labbe 			break;
264b1f578b8SCorentin Labbe 		}
265b1f578b8SCorentin Labbe 	}
266b1f578b8SCorentin Labbe 	return 0;
267b1f578b8SCorentin Labbe }
268b21dc631SLiu Shixin DEFINE_SHOW_ATTRIBUTE(sun4i_ss_debugfs);
269b1f578b8SCorentin Labbe 
27017513547SCorentin Labbe /*
27117513547SCorentin Labbe  * Power management strategy: The device is suspended unless a TFM exists for
27217513547SCorentin Labbe  * one of the algorithms proposed by this driver.
27317513547SCorentin Labbe  */
sun4i_ss_pm_suspend(struct device * dev)27417513547SCorentin Labbe static int sun4i_ss_pm_suspend(struct device *dev)
27517513547SCorentin Labbe {
27617513547SCorentin Labbe 	struct sun4i_ss_ctx *ss = dev_get_drvdata(dev);
27717513547SCorentin Labbe 
27817513547SCorentin Labbe 	reset_control_assert(ss->reset);
27917513547SCorentin Labbe 
28017513547SCorentin Labbe 	clk_disable_unprepare(ss->ssclk);
28117513547SCorentin Labbe 	clk_disable_unprepare(ss->busclk);
28217513547SCorentin Labbe 	return 0;
28317513547SCorentin Labbe }
28417513547SCorentin Labbe 
sun4i_ss_pm_resume(struct device * dev)28517513547SCorentin Labbe static int sun4i_ss_pm_resume(struct device *dev)
28617513547SCorentin Labbe {
28717513547SCorentin Labbe 	struct sun4i_ss_ctx *ss = dev_get_drvdata(dev);
28817513547SCorentin Labbe 
28917513547SCorentin Labbe 	int err;
29017513547SCorentin Labbe 
29117513547SCorentin Labbe 	err = clk_prepare_enable(ss->busclk);
29217513547SCorentin Labbe 	if (err) {
29317513547SCorentin Labbe 		dev_err(ss->dev, "Cannot prepare_enable busclk\n");
29417513547SCorentin Labbe 		goto err_enable;
29517513547SCorentin Labbe 	}
29617513547SCorentin Labbe 
29717513547SCorentin Labbe 	err = clk_prepare_enable(ss->ssclk);
29817513547SCorentin Labbe 	if (err) {
29917513547SCorentin Labbe 		dev_err(ss->dev, "Cannot prepare_enable ssclk\n");
30017513547SCorentin Labbe 		goto err_enable;
30117513547SCorentin Labbe 	}
30217513547SCorentin Labbe 
30317513547SCorentin Labbe 	err = reset_control_deassert(ss->reset);
30417513547SCorentin Labbe 	if (err) {
30517513547SCorentin Labbe 		dev_err(ss->dev, "Cannot deassert reset control\n");
30617513547SCorentin Labbe 		goto err_enable;
30717513547SCorentin Labbe 	}
30817513547SCorentin Labbe 
30917513547SCorentin Labbe 	return err;
31017513547SCorentin Labbe err_enable:
31117513547SCorentin Labbe 	sun4i_ss_pm_suspend(dev);
31217513547SCorentin Labbe 	return err;
31317513547SCorentin Labbe }
31417513547SCorentin Labbe 
3153932aa1cSBen Dooks (Codethink) static const struct dev_pm_ops sun4i_ss_pm_ops = {
31617513547SCorentin Labbe 	SET_RUNTIME_PM_OPS(sun4i_ss_pm_suspend, sun4i_ss_pm_resume, NULL)
31717513547SCorentin Labbe };
31817513547SCorentin Labbe 
31917513547SCorentin Labbe /*
32017513547SCorentin Labbe  * When power management is enabled, this function enables the PM and set the
32117513547SCorentin Labbe  * device as suspended
32217513547SCorentin Labbe  * When power management is disabled, this function just enables the device
32317513547SCorentin Labbe  */
sun4i_ss_pm_init(struct sun4i_ss_ctx * ss)32417513547SCorentin Labbe static int sun4i_ss_pm_init(struct sun4i_ss_ctx *ss)
32517513547SCorentin Labbe {
32617513547SCorentin Labbe 	int err;
32717513547SCorentin Labbe 
32817513547SCorentin Labbe 	pm_runtime_use_autosuspend(ss->dev);
32917513547SCorentin Labbe 	pm_runtime_set_autosuspend_delay(ss->dev, 2000);
33017513547SCorentin Labbe 
33117513547SCorentin Labbe 	err = pm_runtime_set_suspended(ss->dev);
33217513547SCorentin Labbe 	if (err)
33317513547SCorentin Labbe 		return err;
33417513547SCorentin Labbe 	pm_runtime_enable(ss->dev);
33517513547SCorentin Labbe 	return err;
33617513547SCorentin Labbe }
33717513547SCorentin Labbe 
sun4i_ss_pm_exit(struct sun4i_ss_ctx * ss)33817513547SCorentin Labbe static void sun4i_ss_pm_exit(struct sun4i_ss_ctx *ss)
33917513547SCorentin Labbe {
34017513547SCorentin Labbe 	pm_runtime_disable(ss->dev);
34117513547SCorentin Labbe }
34217513547SCorentin Labbe 
sun4i_ss_probe(struct platform_device * pdev)34317513547SCorentin Labbe static int sun4i_ss_probe(struct platform_device *pdev)
34417513547SCorentin Labbe {
34517513547SCorentin Labbe 	u32 v;
34617513547SCorentin Labbe 	int err, i;
34717513547SCorentin Labbe 	unsigned long cr;
34817513547SCorentin Labbe 	const unsigned long cr_ahb = 24 * 1000 * 1000;
34917513547SCorentin Labbe 	const unsigned long cr_mod = 150 * 1000 * 1000;
35017513547SCorentin Labbe 	struct sun4i_ss_ctx *ss;
35117513547SCorentin Labbe 
35217513547SCorentin Labbe 	if (!pdev->dev.of_node)
35317513547SCorentin Labbe 		return -ENODEV;
35417513547SCorentin Labbe 
35517513547SCorentin Labbe 	ss = devm_kzalloc(&pdev->dev, sizeof(*ss), GFP_KERNEL);
35617513547SCorentin Labbe 	if (!ss)
35717513547SCorentin Labbe 		return -ENOMEM;
35817513547SCorentin Labbe 
35917513547SCorentin Labbe 	ss->base = devm_platform_ioremap_resource(pdev, 0);
36017513547SCorentin Labbe 	if (IS_ERR(ss->base)) {
36117513547SCorentin Labbe 		dev_err(&pdev->dev, "Cannot request MMIO\n");
36217513547SCorentin Labbe 		return PTR_ERR(ss->base);
36317513547SCorentin Labbe 	}
36417513547SCorentin Labbe 
3651e02e6fbSCorentin Labbe 	ss->variant = of_device_get_match_data(&pdev->dev);
3661e02e6fbSCorentin Labbe 	if (!ss->variant) {
3671e02e6fbSCorentin Labbe 		dev_err(&pdev->dev, "Missing Security System variant\n");
3681e02e6fbSCorentin Labbe 		return -EINVAL;
3691e02e6fbSCorentin Labbe 	}
3701e02e6fbSCorentin Labbe 
37117513547SCorentin Labbe 	ss->ssclk = devm_clk_get(&pdev->dev, "mod");
37217513547SCorentin Labbe 	if (IS_ERR(ss->ssclk)) {
37317513547SCorentin Labbe 		err = PTR_ERR(ss->ssclk);
37417513547SCorentin Labbe 		dev_err(&pdev->dev, "Cannot get SS clock err=%d\n", err);
37517513547SCorentin Labbe 		return err;
37617513547SCorentin Labbe 	}
37717513547SCorentin Labbe 	dev_dbg(&pdev->dev, "clock ss acquired\n");
37817513547SCorentin Labbe 
37917513547SCorentin Labbe 	ss->busclk = devm_clk_get(&pdev->dev, "ahb");
38017513547SCorentin Labbe 	if (IS_ERR(ss->busclk)) {
38117513547SCorentin Labbe 		err = PTR_ERR(ss->busclk);
38217513547SCorentin Labbe 		dev_err(&pdev->dev, "Cannot get AHB SS clock err=%d\n", err);
38317513547SCorentin Labbe 		return err;
38417513547SCorentin Labbe 	}
38517513547SCorentin Labbe 	dev_dbg(&pdev->dev, "clock ahb_ss acquired\n");
38617513547SCorentin Labbe 
38717513547SCorentin Labbe 	ss->reset = devm_reset_control_get_optional(&pdev->dev, "ahb");
388aa31e559SPhilipp Zabel 	if (IS_ERR(ss->reset))
38917513547SCorentin Labbe 		return PTR_ERR(ss->reset);
390aa31e559SPhilipp Zabel 	if (!ss->reset)
39117513547SCorentin Labbe 		dev_info(&pdev->dev, "no reset control found\n");
39217513547SCorentin Labbe 
39317513547SCorentin Labbe 	/*
39417513547SCorentin Labbe 	 * Check that clock have the correct rates given in the datasheet
39517513547SCorentin Labbe 	 * Try to set the clock to the maximum allowed
39617513547SCorentin Labbe 	 */
39717513547SCorentin Labbe 	err = clk_set_rate(ss->ssclk, cr_mod);
39817513547SCorentin Labbe 	if (err) {
39917513547SCorentin Labbe 		dev_err(&pdev->dev, "Cannot set clock rate to ssclk\n");
40017513547SCorentin Labbe 		return err;
40117513547SCorentin Labbe 	}
40217513547SCorentin Labbe 
40317513547SCorentin Labbe 	/*
40417513547SCorentin Labbe 	 * The only impact on clocks below requirement are bad performance,
40517513547SCorentin Labbe 	 * so do not print "errors"
40617513547SCorentin Labbe 	 * warn on Overclocked clocks
40717513547SCorentin Labbe 	 */
40817513547SCorentin Labbe 	cr = clk_get_rate(ss->busclk);
40917513547SCorentin Labbe 	if (cr >= cr_ahb)
41017513547SCorentin Labbe 		dev_dbg(&pdev->dev, "Clock bus %lu (%lu MHz) (must be >= %lu)\n",
41117513547SCorentin Labbe 			cr, cr / 1000000, cr_ahb);
41217513547SCorentin Labbe 	else
41317513547SCorentin Labbe 		dev_warn(&pdev->dev, "Clock bus %lu (%lu MHz) (must be >= %lu)\n",
41417513547SCorentin Labbe 			 cr, cr / 1000000, cr_ahb);
41517513547SCorentin Labbe 
41617513547SCorentin Labbe 	cr = clk_get_rate(ss->ssclk);
41717513547SCorentin Labbe 	if (cr <= cr_mod)
41817513547SCorentin Labbe 		if (cr < cr_mod)
41917513547SCorentin Labbe 			dev_warn(&pdev->dev, "Clock ss %lu (%lu MHz) (must be <= %lu)\n",
42017513547SCorentin Labbe 				 cr, cr / 1000000, cr_mod);
42117513547SCorentin Labbe 		else
42217513547SCorentin Labbe 			dev_dbg(&pdev->dev, "Clock ss %lu (%lu MHz) (must be <= %lu)\n",
42317513547SCorentin Labbe 				cr, cr / 1000000, cr_mod);
42417513547SCorentin Labbe 	else
42517513547SCorentin Labbe 		dev_warn(&pdev->dev, "Clock ss is at %lu (%lu MHz) (must be <= %lu)\n",
42617513547SCorentin Labbe 			 cr, cr / 1000000, cr_mod);
42717513547SCorentin Labbe 
42817513547SCorentin Labbe 	ss->dev = &pdev->dev;
42917513547SCorentin Labbe 	platform_set_drvdata(pdev, ss);
43017513547SCorentin Labbe 
43117513547SCorentin Labbe 	spin_lock_init(&ss->slock);
43217513547SCorentin Labbe 
43317513547SCorentin Labbe 	err = sun4i_ss_pm_init(ss);
43417513547SCorentin Labbe 	if (err)
43517513547SCorentin Labbe 		return err;
43617513547SCorentin Labbe 
43717513547SCorentin Labbe 	/*
43817513547SCorentin Labbe 	 * Datasheet named it "Die Bonding ID"
43917513547SCorentin Labbe 	 * I expect to be a sort of Security System Revision number.
44017513547SCorentin Labbe 	 * Since the A80 seems to have an other version of SS
44117513547SCorentin Labbe 	 * this info could be useful
44217513547SCorentin Labbe 	 */
44317513547SCorentin Labbe 
444ac98fc5eSShixin Liu 	err = pm_runtime_resume_and_get(ss->dev);
44517513547SCorentin Labbe 	if (err < 0)
44617513547SCorentin Labbe 		goto error_pm;
44717513547SCorentin Labbe 
44817513547SCorentin Labbe 	writel(SS_ENABLED, ss->base + SS_CTL);
44917513547SCorentin Labbe 	v = readl(ss->base + SS_CTL);
45017513547SCorentin Labbe 	v >>= 16;
45117513547SCorentin Labbe 	v &= 0x07;
45217513547SCorentin Labbe 	dev_info(&pdev->dev, "Die ID %d\n", v);
45317513547SCorentin Labbe 	writel(0, ss->base + SS_CTL);
45417513547SCorentin Labbe 
45517513547SCorentin Labbe 	pm_runtime_put_sync(ss->dev);
45617513547SCorentin Labbe 
45717513547SCorentin Labbe 	for (i = 0; i < ARRAY_SIZE(ss_algs); i++) {
45817513547SCorentin Labbe 		ss_algs[i].ss = ss;
45917513547SCorentin Labbe 		switch (ss_algs[i].type) {
46017513547SCorentin Labbe 		case CRYPTO_ALG_TYPE_SKCIPHER:
46117513547SCorentin Labbe 			err = crypto_register_skcipher(&ss_algs[i].alg.crypto);
46217513547SCorentin Labbe 			if (err) {
46317513547SCorentin Labbe 				dev_err(ss->dev, "Fail to register %s\n",
46417513547SCorentin Labbe 					ss_algs[i].alg.crypto.base.cra_name);
46517513547SCorentin Labbe 				goto error_alg;
46617513547SCorentin Labbe 			}
46717513547SCorentin Labbe 			break;
46817513547SCorentin Labbe 		case CRYPTO_ALG_TYPE_AHASH:
46917513547SCorentin Labbe 			err = crypto_register_ahash(&ss_algs[i].alg.hash);
47017513547SCorentin Labbe 			if (err) {
47117513547SCorentin Labbe 				dev_err(ss->dev, "Fail to register %s\n",
47217513547SCorentin Labbe 					ss_algs[i].alg.hash.halg.base.cra_name);
47317513547SCorentin Labbe 				goto error_alg;
47417513547SCorentin Labbe 			}
47517513547SCorentin Labbe 			break;
47617513547SCorentin Labbe 		case CRYPTO_ALG_TYPE_RNG:
47717513547SCorentin Labbe 			err = crypto_register_rng(&ss_algs[i].alg.rng);
47817513547SCorentin Labbe 			if (err) {
47917513547SCorentin Labbe 				dev_err(ss->dev, "Fail to register %s\n",
48017513547SCorentin Labbe 					ss_algs[i].alg.rng.base.cra_name);
48117513547SCorentin Labbe 			}
48217513547SCorentin Labbe 			break;
48317513547SCorentin Labbe 		}
48417513547SCorentin Labbe 	}
485b1f578b8SCorentin Labbe 
486b1f578b8SCorentin Labbe 	/* Ignore error of debugfs */
487b1f578b8SCorentin Labbe 	ss->dbgfs_dir = debugfs_create_dir("sun4i-ss", NULL);
488b1f578b8SCorentin Labbe 	ss->dbgfs_stats = debugfs_create_file("stats", 0444, ss->dbgfs_dir, ss,
489b1f578b8SCorentin Labbe 					      &sun4i_ss_debugfs_fops);
490b1f578b8SCorentin Labbe 
49117513547SCorentin Labbe 	return 0;
49217513547SCorentin Labbe error_alg:
49317513547SCorentin Labbe 	i--;
49417513547SCorentin Labbe 	for (; i >= 0; i--) {
49517513547SCorentin Labbe 		switch (ss_algs[i].type) {
49617513547SCorentin Labbe 		case CRYPTO_ALG_TYPE_SKCIPHER:
49717513547SCorentin Labbe 			crypto_unregister_skcipher(&ss_algs[i].alg.crypto);
49817513547SCorentin Labbe 			break;
49917513547SCorentin Labbe 		case CRYPTO_ALG_TYPE_AHASH:
50017513547SCorentin Labbe 			crypto_unregister_ahash(&ss_algs[i].alg.hash);
50117513547SCorentin Labbe 			break;
50217513547SCorentin Labbe 		case CRYPTO_ALG_TYPE_RNG:
50317513547SCorentin Labbe 			crypto_unregister_rng(&ss_algs[i].alg.rng);
50417513547SCorentin Labbe 			break;
50517513547SCorentin Labbe 		}
50617513547SCorentin Labbe 	}
50717513547SCorentin Labbe error_pm:
50817513547SCorentin Labbe 	sun4i_ss_pm_exit(ss);
50917513547SCorentin Labbe 	return err;
51017513547SCorentin Labbe }
51117513547SCorentin Labbe 
sun4i_ss_remove(struct platform_device * pdev)51217513547SCorentin Labbe static int sun4i_ss_remove(struct platform_device *pdev)
51317513547SCorentin Labbe {
51417513547SCorentin Labbe 	int i;
51517513547SCorentin Labbe 	struct sun4i_ss_ctx *ss = platform_get_drvdata(pdev);
51617513547SCorentin Labbe 
51717513547SCorentin Labbe 	for (i = 0; i < ARRAY_SIZE(ss_algs); i++) {
51817513547SCorentin Labbe 		switch (ss_algs[i].type) {
51917513547SCorentin Labbe 		case CRYPTO_ALG_TYPE_SKCIPHER:
52017513547SCorentin Labbe 			crypto_unregister_skcipher(&ss_algs[i].alg.crypto);
52117513547SCorentin Labbe 			break;
52217513547SCorentin Labbe 		case CRYPTO_ALG_TYPE_AHASH:
52317513547SCorentin Labbe 			crypto_unregister_ahash(&ss_algs[i].alg.hash);
52417513547SCorentin Labbe 			break;
52517513547SCorentin Labbe 		case CRYPTO_ALG_TYPE_RNG:
52617513547SCorentin Labbe 			crypto_unregister_rng(&ss_algs[i].alg.rng);
52717513547SCorentin Labbe 			break;
52817513547SCorentin Labbe 		}
52917513547SCorentin Labbe 	}
53017513547SCorentin Labbe 
53117513547SCorentin Labbe 	sun4i_ss_pm_exit(ss);
53217513547SCorentin Labbe 	return 0;
53317513547SCorentin Labbe }
53417513547SCorentin Labbe 
53517513547SCorentin Labbe static const struct of_device_id a20ss_crypto_of_match_table[] = {
5361e02e6fbSCorentin Labbe 	{ .compatible = "allwinner,sun4i-a10-crypto",
5371e02e6fbSCorentin Labbe 	  .data = &ss_a10_variant
5381e02e6fbSCorentin Labbe 	},
5391e02e6fbSCorentin Labbe 	{ .compatible = "allwinner,sun8i-a33-crypto",
5401e02e6fbSCorentin Labbe 	  .data = &ss_a33_variant
5411e02e6fbSCorentin Labbe 	},
54217513547SCorentin Labbe 	{}
54317513547SCorentin Labbe };
54417513547SCorentin Labbe MODULE_DEVICE_TABLE(of, a20ss_crypto_of_match_table);
54517513547SCorentin Labbe 
54617513547SCorentin Labbe static struct platform_driver sun4i_ss_driver = {
54717513547SCorentin Labbe 	.probe          = sun4i_ss_probe,
54817513547SCorentin Labbe 	.remove         = sun4i_ss_remove,
54917513547SCorentin Labbe 	.driver         = {
55017513547SCorentin Labbe 		.name           = "sun4i-ss",
55117513547SCorentin Labbe 		.pm		= &sun4i_ss_pm_ops,
55217513547SCorentin Labbe 		.of_match_table	= a20ss_crypto_of_match_table,
55317513547SCorentin Labbe 	},
55417513547SCorentin Labbe };
55517513547SCorentin Labbe 
55617513547SCorentin Labbe module_platform_driver(sun4i_ss_driver);
55717513547SCorentin Labbe 
55817513547SCorentin Labbe MODULE_ALIAS("platform:sun4i-ss");
55917513547SCorentin Labbe MODULE_DESCRIPTION("Allwinner Security System cryptographic accelerator");
56017513547SCorentin Labbe MODULE_LICENSE("GPL");
56117513547SCorentin Labbe MODULE_AUTHOR("Corentin LABBE <clabbe.montjoie@gmail.com>");
562