117513547SCorentin Labbe /* SPDX-License-Identifier: GPL-2.0-only */
217513547SCorentin Labbe /*
317513547SCorentin Labbe  * sun4i-ss.h - 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  * Support AES cipher with 128,192,256 bits keysize.
817513547SCorentin Labbe  * Support MD5 and SHA1 hash algorithms.
917513547SCorentin Labbe  * Support DES and 3DES
1017513547SCorentin Labbe  *
11*39db3f15SJonathan Corbet  * You could find the datasheet in Documentation/arch/arm/sunxi.rst
1217513547SCorentin Labbe  */
1317513547SCorentin Labbe 
1417513547SCorentin Labbe #include <linux/clk.h>
1517513547SCorentin Labbe #include <linux/crypto.h>
1617513547SCorentin Labbe #include <linux/io.h>
1717513547SCorentin Labbe #include <linux/module.h>
1817513547SCorentin Labbe #include <linux/of.h>
1917513547SCorentin Labbe #include <linux/platform_device.h>
2017513547SCorentin Labbe #include <linux/reset.h>
2117513547SCorentin Labbe #include <crypto/scatterwalk.h>
2217513547SCorentin Labbe #include <linux/scatterlist.h>
2317513547SCorentin Labbe #include <linux/interrupt.h>
2417513547SCorentin Labbe #include <linux/delay.h>
2517513547SCorentin Labbe #include <linux/pm_runtime.h>
2617513547SCorentin Labbe #include <crypto/md5.h>
2717513547SCorentin Labbe #include <crypto/skcipher.h>
28a24d22b2SEric Biggers #include <crypto/sha1.h>
2917513547SCorentin Labbe #include <crypto/hash.h>
3017513547SCorentin Labbe #include <crypto/internal/hash.h>
3117513547SCorentin Labbe #include <crypto/internal/skcipher.h>
3217513547SCorentin Labbe #include <crypto/aes.h>
3317513547SCorentin Labbe #include <crypto/internal/des.h>
3417513547SCorentin Labbe #include <crypto/internal/rng.h>
3517513547SCorentin Labbe #include <crypto/rng.h>
3617513547SCorentin Labbe 
3717513547SCorentin Labbe #define SS_CTL            0x00
3817513547SCorentin Labbe #define SS_KEY0           0x04
3917513547SCorentin Labbe #define SS_KEY1           0x08
4017513547SCorentin Labbe #define SS_KEY2           0x0C
4117513547SCorentin Labbe #define SS_KEY3           0x10
4217513547SCorentin Labbe #define SS_KEY4           0x14
4317513547SCorentin Labbe #define SS_KEY5           0x18
4417513547SCorentin Labbe #define SS_KEY6           0x1C
4517513547SCorentin Labbe #define SS_KEY7           0x20
4617513547SCorentin Labbe 
4717513547SCorentin Labbe #define SS_IV0            0x24
4817513547SCorentin Labbe #define SS_IV1            0x28
4917513547SCorentin Labbe #define SS_IV2            0x2C
5017513547SCorentin Labbe #define SS_IV3            0x30
5117513547SCorentin Labbe 
5217513547SCorentin Labbe #define SS_FCSR           0x44
5317513547SCorentin Labbe 
5417513547SCorentin Labbe #define SS_MD0            0x4C
5517513547SCorentin Labbe #define SS_MD1            0x50
5617513547SCorentin Labbe #define SS_MD2            0x54
5717513547SCorentin Labbe #define SS_MD3            0x58
5817513547SCorentin Labbe #define SS_MD4            0x5C
5917513547SCorentin Labbe 
6017513547SCorentin Labbe #define SS_RXFIFO         0x200
6117513547SCorentin Labbe #define SS_TXFIFO         0x204
6217513547SCorentin Labbe 
6317513547SCorentin Labbe /* SS_CTL configuration values */
6417513547SCorentin Labbe 
6517513547SCorentin Labbe /* PRNG generator mode - bit 15 */
6617513547SCorentin Labbe #define SS_PRNG_ONESHOT		(0 << 15)
6717513547SCorentin Labbe #define SS_PRNG_CONTINUE	(1 << 15)
6817513547SCorentin Labbe 
6917513547SCorentin Labbe /* IV mode for hash */
7017513547SCorentin Labbe #define SS_IV_ARBITRARY		(1 << 14)
7117513547SCorentin Labbe 
7217513547SCorentin Labbe /* SS operation mode - bits 12-13 */
7317513547SCorentin Labbe #define SS_ECB			(0 << 12)
7417513547SCorentin Labbe #define SS_CBC			(1 << 12)
7517513547SCorentin Labbe #define SS_CTS			(3 << 12)
7617513547SCorentin Labbe 
7717513547SCorentin Labbe /* Counter width for CNT mode - bits 10-11 */
7817513547SCorentin Labbe #define SS_CNT_16BITS		(0 << 10)
7917513547SCorentin Labbe #define SS_CNT_32BITS		(1 << 10)
8017513547SCorentin Labbe #define SS_CNT_64BITS		(2 << 10)
8117513547SCorentin Labbe 
8217513547SCorentin Labbe /* Key size for AES - bits 8-9 */
8317513547SCorentin Labbe #define SS_AES_128BITS		(0 << 8)
8417513547SCorentin Labbe #define SS_AES_192BITS		(1 << 8)
8517513547SCorentin Labbe #define SS_AES_256BITS		(2 << 8)
8617513547SCorentin Labbe 
8717513547SCorentin Labbe /* Operation direction - bit 7 */
8817513547SCorentin Labbe #define SS_ENCRYPTION		(0 << 7)
8917513547SCorentin Labbe #define SS_DECRYPTION		(1 << 7)
9017513547SCorentin Labbe 
9117513547SCorentin Labbe /* SS Method - bits 4-6 */
9217513547SCorentin Labbe #define SS_OP_AES		(0 << 4)
9317513547SCorentin Labbe #define SS_OP_DES		(1 << 4)
9417513547SCorentin Labbe #define SS_OP_3DES		(2 << 4)
9517513547SCorentin Labbe #define SS_OP_SHA1		(3 << 4)
9617513547SCorentin Labbe #define SS_OP_MD5		(4 << 4)
9717513547SCorentin Labbe #define SS_OP_PRNG		(5 << 4)
9817513547SCorentin Labbe 
9917513547SCorentin Labbe /* Data end bit - bit 2 */
10017513547SCorentin Labbe #define SS_DATA_END		(1 << 2)
10117513547SCorentin Labbe 
10217513547SCorentin Labbe /* PRNG start bit - bit 1 */
10317513547SCorentin Labbe #define SS_PRNG_START		(1 << 1)
10417513547SCorentin Labbe 
10517513547SCorentin Labbe /* SS Enable bit - bit 0 */
10617513547SCorentin Labbe #define SS_DISABLED		(0 << 0)
10717513547SCorentin Labbe #define SS_ENABLED		(1 << 0)
10817513547SCorentin Labbe 
10917513547SCorentin Labbe /* SS_FCSR configuration values */
11017513547SCorentin Labbe /* RX FIFO status - bit 30 */
11117513547SCorentin Labbe #define SS_RXFIFO_FREE		(1 << 30)
11217513547SCorentin Labbe 
11317513547SCorentin Labbe /* RX FIFO empty spaces - bits 24-29 */
11417513547SCorentin Labbe #define SS_RXFIFO_SPACES(val)	(((val) >> 24) & 0x3f)
11517513547SCorentin Labbe 
11617513547SCorentin Labbe /* TX FIFO status - bit 22 */
11717513547SCorentin Labbe #define SS_TXFIFO_AVAILABLE	(1 << 22)
11817513547SCorentin Labbe 
11917513547SCorentin Labbe /* TX FIFO available spaces - bits 16-21 */
12017513547SCorentin Labbe #define SS_TXFIFO_SPACES(val)	(((val) >> 16) & 0x3f)
12117513547SCorentin Labbe 
12217513547SCorentin Labbe #define SS_RX_MAX	32
12317513547SCorentin Labbe #define SS_RX_DEFAULT	SS_RX_MAX
12417513547SCorentin Labbe #define SS_TX_MAX	33
12517513547SCorentin Labbe 
12617513547SCorentin Labbe #define SS_RXFIFO_EMP_INT_PENDING	(1 << 10)
12717513547SCorentin Labbe #define SS_TXFIFO_AVA_INT_PENDING	(1 << 8)
12817513547SCorentin Labbe #define SS_RXFIFO_EMP_INT_ENABLE	(1 << 2)
12917513547SCorentin Labbe #define SS_TXFIFO_AVA_INT_ENABLE	(1 << 0)
13017513547SCorentin Labbe 
13117513547SCorentin Labbe #define SS_SEED_LEN 192
13217513547SCorentin Labbe #define SS_DATA_LEN 160
13317513547SCorentin Labbe 
1341e02e6fbSCorentin Labbe /*
1351e02e6fbSCorentin Labbe  * struct ss_variant - Describe SS hardware variant
1361e02e6fbSCorentin Labbe  * @sha1_in_be:		The SHA1 digest is given by SS in BE, and so need to be inverted.
1371e02e6fbSCorentin Labbe  */
1381e02e6fbSCorentin Labbe struct ss_variant {
1391e02e6fbSCorentin Labbe 	bool sha1_in_be;
1401e02e6fbSCorentin Labbe };
1411e02e6fbSCorentin Labbe 
14217513547SCorentin Labbe struct sun4i_ss_ctx {
1431e02e6fbSCorentin Labbe 	const struct ss_variant *variant;
14417513547SCorentin Labbe 	void __iomem *base;
14517513547SCorentin Labbe 	int irq;
14617513547SCorentin Labbe 	struct clk *busclk;
14717513547SCorentin Labbe 	struct clk *ssclk;
14817513547SCorentin Labbe 	struct reset_control *reset;
14917513547SCorentin Labbe 	struct device *dev;
15017513547SCorentin Labbe 	struct resource *res;
15158351351SCorentin Labbe 	char buf[4 * SS_RX_MAX];/* buffer for linearize SG src */
15258351351SCorentin Labbe 	char bufo[4 * SS_TX_MAX]; /* buffer for linearize SG dst */
15317513547SCorentin Labbe 	spinlock_t slock; /* control the use of the device */
15417513547SCorentin Labbe #ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_PRNG
15517513547SCorentin Labbe 	u32 seed[SS_SEED_LEN / BITS_PER_LONG];
15617513547SCorentin Labbe #endif
157b1f578b8SCorentin Labbe 	struct dentry *dbgfs_dir;
158b1f578b8SCorentin Labbe 	struct dentry *dbgfs_stats;
15917513547SCorentin Labbe };
16017513547SCorentin Labbe 
16117513547SCorentin Labbe struct sun4i_ss_alg_template {
16217513547SCorentin Labbe 	u32 type;
16317513547SCorentin Labbe 	u32 mode;
16417513547SCorentin Labbe 	union {
16517513547SCorentin Labbe 		struct skcipher_alg crypto;
16617513547SCorentin Labbe 		struct ahash_alg hash;
16717513547SCorentin Labbe 		struct rng_alg rng;
16817513547SCorentin Labbe 	} alg;
16917513547SCorentin Labbe 	struct sun4i_ss_ctx *ss;
170b1f578b8SCorentin Labbe 	unsigned long stat_req;
171b1f578b8SCorentin Labbe 	unsigned long stat_fb;
172b1f578b8SCorentin Labbe 	unsigned long stat_bytes;
173b1f578b8SCorentin Labbe 	unsigned long stat_opti;
17417513547SCorentin Labbe };
17517513547SCorentin Labbe 
17617513547SCorentin Labbe struct sun4i_tfm_ctx {
17717513547SCorentin Labbe 	u32 key[AES_MAX_KEY_SIZE / 4];/* divided by sizeof(u32) */
17817513547SCorentin Labbe 	u32 keylen;
17917513547SCorentin Labbe 	u32 keymode;
18017513547SCorentin Labbe 	struct sun4i_ss_ctx *ss;
18189fb00f2SArd Biesheuvel 	struct crypto_skcipher *fallback_tfm;
18217513547SCorentin Labbe };
18317513547SCorentin Labbe 
18417513547SCorentin Labbe struct sun4i_cipher_req_ctx {
18517513547SCorentin Labbe 	u32 mode;
18622d03a0aSCorentin Labbe 	u8 backup_iv[AES_BLOCK_SIZE];
18789fb00f2SArd Biesheuvel 	struct skcipher_request fallback_req;   // keep at the end
18817513547SCorentin Labbe };
18917513547SCorentin Labbe 
19017513547SCorentin Labbe struct sun4i_req_ctx {
19117513547SCorentin Labbe 	u32 mode;
19217513547SCorentin Labbe 	u64 byte_count; /* number of bytes "uploaded" to the device */
19317513547SCorentin Labbe 	u32 hash[5]; /* for storing SS_IVx register */
19417513547SCorentin Labbe 	char buf[64];
19517513547SCorentin Labbe 	unsigned int len;
19617513547SCorentin Labbe 	int flags;
19717513547SCorentin Labbe };
19817513547SCorentin Labbe 
19917513547SCorentin Labbe int sun4i_hash_crainit(struct crypto_tfm *tfm);
20017513547SCorentin Labbe void sun4i_hash_craexit(struct crypto_tfm *tfm);
20117513547SCorentin Labbe int sun4i_hash_init(struct ahash_request *areq);
20217513547SCorentin Labbe int sun4i_hash_update(struct ahash_request *areq);
20317513547SCorentin Labbe int sun4i_hash_final(struct ahash_request *areq);
20417513547SCorentin Labbe int sun4i_hash_finup(struct ahash_request *areq);
20517513547SCorentin Labbe int sun4i_hash_digest(struct ahash_request *areq);
20617513547SCorentin Labbe int sun4i_hash_export_md5(struct ahash_request *areq, void *out);
20717513547SCorentin Labbe int sun4i_hash_import_md5(struct ahash_request *areq, const void *in);
20817513547SCorentin Labbe int sun4i_hash_export_sha1(struct ahash_request *areq, void *out);
20917513547SCorentin Labbe int sun4i_hash_import_sha1(struct ahash_request *areq, const void *in);
21017513547SCorentin Labbe 
21117513547SCorentin Labbe int sun4i_ss_cbc_aes_encrypt(struct skcipher_request *areq);
21217513547SCorentin Labbe int sun4i_ss_cbc_aes_decrypt(struct skcipher_request *areq);
21317513547SCorentin Labbe int sun4i_ss_ecb_aes_encrypt(struct skcipher_request *areq);
21417513547SCorentin Labbe int sun4i_ss_ecb_aes_decrypt(struct skcipher_request *areq);
21517513547SCorentin Labbe 
21617513547SCorentin Labbe int sun4i_ss_cbc_des_encrypt(struct skcipher_request *areq);
21717513547SCorentin Labbe int sun4i_ss_cbc_des_decrypt(struct skcipher_request *areq);
21817513547SCorentin Labbe int sun4i_ss_ecb_des_encrypt(struct skcipher_request *areq);
21917513547SCorentin Labbe int sun4i_ss_ecb_des_decrypt(struct skcipher_request *areq);
22017513547SCorentin Labbe 
22117513547SCorentin Labbe int sun4i_ss_cbc_des3_encrypt(struct skcipher_request *areq);
22217513547SCorentin Labbe int sun4i_ss_cbc_des3_decrypt(struct skcipher_request *areq);
22317513547SCorentin Labbe int sun4i_ss_ecb_des3_encrypt(struct skcipher_request *areq);
22417513547SCorentin Labbe int sun4i_ss_ecb_des3_decrypt(struct skcipher_request *areq);
22517513547SCorentin Labbe 
22617513547SCorentin Labbe int sun4i_ss_cipher_init(struct crypto_tfm *tfm);
22717513547SCorentin Labbe void sun4i_ss_cipher_exit(struct crypto_tfm *tfm);
22817513547SCorentin Labbe int sun4i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
22917513547SCorentin Labbe 			unsigned int keylen);
23017513547SCorentin Labbe int sun4i_ss_des_setkey(struct crypto_skcipher *tfm, const u8 *key,
23117513547SCorentin Labbe 			unsigned int keylen);
23217513547SCorentin Labbe int sun4i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
23317513547SCorentin Labbe 			 unsigned int keylen);
23417513547SCorentin Labbe int sun4i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
23517513547SCorentin Labbe 			   unsigned int slen, u8 *dst, unsigned int dlen);
23617513547SCorentin Labbe int sun4i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
237