18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef __RK3288_CRYPTO_H__
38c2ecf20Sopenharmony_ci#define __RK3288_CRYPTO_H__
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <crypto/aes.h>
68c2ecf20Sopenharmony_ci#include <crypto/internal/des.h>
78c2ecf20Sopenharmony_ci#include <crypto/algapi.h>
88c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
98c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
128c2ecf20Sopenharmony_ci#include <crypto/engine.h>
138c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h>
148c2ecf20Sopenharmony_ci#include <crypto/internal/skcipher.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <crypto/md5.h>
178c2ecf20Sopenharmony_ci#include <crypto/sha.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define _SBF(v, f)			((v) << (f))
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/* Crypto control registers*/
228c2ecf20Sopenharmony_ci#define RK_CRYPTO_INTSTS		0x0000
238c2ecf20Sopenharmony_ci#define RK_CRYPTO_PKA_DONE_INT		BIT(5)
248c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_DONE_INT		BIT(4)
258c2ecf20Sopenharmony_ci#define RK_CRYPTO_HRDMA_ERR_INT		BIT(3)
268c2ecf20Sopenharmony_ci#define RK_CRYPTO_HRDMA_DONE_INT	BIT(2)
278c2ecf20Sopenharmony_ci#define RK_CRYPTO_BCDMA_ERR_INT		BIT(1)
288c2ecf20Sopenharmony_ci#define RK_CRYPTO_BCDMA_DONE_INT	BIT(0)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define RK_CRYPTO_INTENA		0x0004
318c2ecf20Sopenharmony_ci#define RK_CRYPTO_PKA_DONE_ENA		BIT(5)
328c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_DONE_ENA		BIT(4)
338c2ecf20Sopenharmony_ci#define RK_CRYPTO_HRDMA_ERR_ENA		BIT(3)
348c2ecf20Sopenharmony_ci#define RK_CRYPTO_HRDMA_DONE_ENA	BIT(2)
358c2ecf20Sopenharmony_ci#define RK_CRYPTO_BCDMA_ERR_ENA		BIT(1)
368c2ecf20Sopenharmony_ci#define RK_CRYPTO_BCDMA_DONE_ENA	BIT(0)
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define RK_CRYPTO_CTRL			0x0008
398c2ecf20Sopenharmony_ci#define RK_CRYPTO_WRITE_MASK		_SBF(0xFFFF, 16)
408c2ecf20Sopenharmony_ci#define RK_CRYPTO_TRNG_FLUSH		BIT(9)
418c2ecf20Sopenharmony_ci#define RK_CRYPTO_TRNG_START		BIT(8)
428c2ecf20Sopenharmony_ci#define RK_CRYPTO_PKA_FLUSH		BIT(7)
438c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_FLUSH		BIT(6)
448c2ecf20Sopenharmony_ci#define RK_CRYPTO_BLOCK_FLUSH		BIT(5)
458c2ecf20Sopenharmony_ci#define RK_CRYPTO_PKA_START		BIT(4)
468c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_START		BIT(3)
478c2ecf20Sopenharmony_ci#define RK_CRYPTO_BLOCK_START		BIT(2)
488c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_START		BIT(1)
498c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_START		BIT(0)
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define RK_CRYPTO_CONF			0x000c
528c2ecf20Sopenharmony_ci/* HASH Receive DMA Address Mode:   fix | increment */
538c2ecf20Sopenharmony_ci#define RK_CRYPTO_HR_ADDR_MODE		BIT(8)
548c2ecf20Sopenharmony_ci/* Block Transmit DMA Address Mode: fix | increment */
558c2ecf20Sopenharmony_ci#define RK_CRYPTO_BT_ADDR_MODE		BIT(7)
568c2ecf20Sopenharmony_ci/* Block Receive DMA Address Mode:  fix | increment */
578c2ecf20Sopenharmony_ci#define RK_CRYPTO_BR_ADDR_MODE		BIT(6)
588c2ecf20Sopenharmony_ci#define RK_CRYPTO_BYTESWAP_HRFIFO	BIT(5)
598c2ecf20Sopenharmony_ci#define RK_CRYPTO_BYTESWAP_BTFIFO	BIT(4)
608c2ecf20Sopenharmony_ci#define RK_CRYPTO_BYTESWAP_BRFIFO	BIT(3)
618c2ecf20Sopenharmony_ci/* AES = 0 OR DES = 1 */
628c2ecf20Sopenharmony_ci#define RK_CRYPTO_DESSEL				BIT(2)
638c2ecf20Sopenharmony_ci#define RK_CYYPTO_HASHINSEL_INDEPENDENT_SOURCE		_SBF(0x00, 0)
648c2ecf20Sopenharmony_ci#define RK_CYYPTO_HASHINSEL_BLOCK_CIPHER_INPUT		_SBF(0x01, 0)
658c2ecf20Sopenharmony_ci#define RK_CYYPTO_HASHINSEL_BLOCK_CIPHER_OUTPUT		_SBF(0x02, 0)
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/* Block Receiving DMA Start Address Register */
688c2ecf20Sopenharmony_ci#define RK_CRYPTO_BRDMAS		0x0010
698c2ecf20Sopenharmony_ci/* Block Transmitting DMA Start Address Register */
708c2ecf20Sopenharmony_ci#define RK_CRYPTO_BTDMAS		0x0014
718c2ecf20Sopenharmony_ci/* Block Receiving DMA Length Register */
728c2ecf20Sopenharmony_ci#define RK_CRYPTO_BRDMAL		0x0018
738c2ecf20Sopenharmony_ci/* Hash Receiving DMA Start Address Register */
748c2ecf20Sopenharmony_ci#define RK_CRYPTO_HRDMAS		0x001c
758c2ecf20Sopenharmony_ci/* Hash Receiving DMA Length Register */
768c2ecf20Sopenharmony_ci#define RK_CRYPTO_HRDMAL		0x0020
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/* AES registers */
798c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_CTRL			  0x0080
808c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_BYTESWAP_CNT	BIT(11)
818c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_BYTESWAP_KEY	BIT(10)
828c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_BYTESWAP_IV	BIT(9)
838c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_BYTESWAP_DO	BIT(8)
848c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_BYTESWAP_DI	BIT(7)
858c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_KEY_CHANGE	BIT(6)
868c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_ECB_MODE		_SBF(0x00, 4)
878c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_CBC_MODE		_SBF(0x01, 4)
888c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_CTR_MODE		_SBF(0x02, 4)
898c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_128BIT_key	_SBF(0x00, 2)
908c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_192BIT_key	_SBF(0x01, 2)
918c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_256BIT_key	_SBF(0x02, 2)
928c2ecf20Sopenharmony_ci/* Slave = 0 / fifo = 1 */
938c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_FIFO_MODE		BIT(1)
948c2ecf20Sopenharmony_ci/* Encryption = 0 , Decryption = 1 */
958c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_DEC		BIT(0)
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_STS		0x0084
988c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_DONE		BIT(0)
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/* AES Input Data 0-3 Register */
1018c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_DIN_0		0x0088
1028c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_DIN_1		0x008c
1038c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_DIN_2		0x0090
1048c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_DIN_3		0x0094
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/* AES output Data 0-3 Register */
1078c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_DOUT_0		0x0098
1088c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_DOUT_1		0x009c
1098c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_DOUT_2		0x00a0
1108c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_DOUT_3		0x00a4
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/* AES IV Data 0-3 Register */
1138c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_IV_0		0x00a8
1148c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_IV_1		0x00ac
1158c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_IV_2		0x00b0
1168c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_IV_3		0x00b4
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci/* AES Key Data 0-3 Register */
1198c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_KEY_0		0x00b8
1208c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_KEY_1		0x00bc
1218c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_KEY_2		0x00c0
1228c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_KEY_3		0x00c4
1238c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_KEY_4		0x00c8
1248c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_KEY_5		0x00cc
1258c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_KEY_6		0x00d0
1268c2ecf20Sopenharmony_ci#define RK_CRYPTO_AES_KEY_7		0x00d4
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/* des/tdes */
1298c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_CTRL		0x0100
1308c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_BYTESWAP_KEY	BIT(8)
1318c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_BYTESWAP_IV	BIT(7)
1328c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_BYTESWAP_DO	BIT(6)
1338c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_BYTESWAP_DI	BIT(5)
1348c2ecf20Sopenharmony_ci/* 0: ECB, 1: CBC */
1358c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_CHAINMODE_CBC	BIT(4)
1368c2ecf20Sopenharmony_ci/* TDES Key Mode, 0 : EDE, 1 : EEE */
1378c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_EEE		BIT(3)
1388c2ecf20Sopenharmony_ci/* 0: DES, 1:TDES */
1398c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_SELECT		BIT(2)
1408c2ecf20Sopenharmony_ci/* 0: Slave, 1:Fifo */
1418c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_FIFO_MODE	BIT(1)
1428c2ecf20Sopenharmony_ci/* Encryption = 0 , Decryption = 1 */
1438c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_DEC		BIT(0)
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_STS		0x0104
1468c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_DONE		BIT(0)
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_DIN_0		0x0108
1498c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_DIN_1		0x010c
1508c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_DOUT_0		0x0110
1518c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_DOUT_1		0x0114
1528c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_IV_0		0x0118
1538c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_IV_1		0x011c
1548c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_KEY1_0		0x0120
1558c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_KEY1_1		0x0124
1568c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_KEY2_0		0x0128
1578c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_KEY2_1		0x012c
1588c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_KEY3_0		0x0130
1598c2ecf20Sopenharmony_ci#define RK_CRYPTO_TDES_KEY3_1		0x0134
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci/* HASH */
1628c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_CTRL		0x0180
1638c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_SWAP_DO		BIT(3)
1648c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_SWAP_DI		BIT(2)
1658c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_SHA1		_SBF(0x00, 0)
1668c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_MD5		_SBF(0x01, 0)
1678c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_SHA256		_SBF(0x02, 0)
1688c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_PRNG		_SBF(0x03, 0)
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_STS		0x0184
1718c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_DONE		BIT(0)
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_MSG_LEN		0x0188
1748c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_DOUT_0		0x018c
1758c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_DOUT_1		0x0190
1768c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_DOUT_2		0x0194
1778c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_DOUT_3		0x0198
1788c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_DOUT_4		0x019c
1798c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_DOUT_5		0x01a0
1808c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_DOUT_6		0x01a4
1818c2ecf20Sopenharmony_ci#define RK_CRYPTO_HASH_DOUT_7		0x01a8
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci#define CRYPTO_READ(dev, offset)		  \
1848c2ecf20Sopenharmony_ci		readl_relaxed(((dev)->reg + (offset)))
1858c2ecf20Sopenharmony_ci#define CRYPTO_WRITE(dev, offset, val)	  \
1868c2ecf20Sopenharmony_ci		writel_relaxed((val), ((dev)->reg + (offset)))
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistruct rk_crypto_info {
1898c2ecf20Sopenharmony_ci	struct device			*dev;
1908c2ecf20Sopenharmony_ci	struct clk			*aclk;
1918c2ecf20Sopenharmony_ci	struct clk			*hclk;
1928c2ecf20Sopenharmony_ci	struct clk			*sclk;
1938c2ecf20Sopenharmony_ci	struct clk			*dmaclk;
1948c2ecf20Sopenharmony_ci	struct reset_control		*rst;
1958c2ecf20Sopenharmony_ci	void __iomem			*reg;
1968c2ecf20Sopenharmony_ci	int				irq;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	struct crypto_engine *engine;
1998c2ecf20Sopenharmony_ci	struct completion complete;
2008c2ecf20Sopenharmony_ci	int status;
2018c2ecf20Sopenharmony_ci};
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci/* the private variable of hash */
2048c2ecf20Sopenharmony_cistruct rk_ahash_ctx {
2058c2ecf20Sopenharmony_ci	struct crypto_engine_ctx enginectx;
2068c2ecf20Sopenharmony_ci	struct rk_crypto_info		*dev;
2078c2ecf20Sopenharmony_ci	/* for fallback */
2088c2ecf20Sopenharmony_ci	struct crypto_ahash		*fallback_tfm;
2098c2ecf20Sopenharmony_ci};
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci/* the privete variable of hash for fallback */
2128c2ecf20Sopenharmony_cistruct rk_ahash_rctx {
2138c2ecf20Sopenharmony_ci	struct ahash_request		fallback_req;
2148c2ecf20Sopenharmony_ci	u32				mode;
2158c2ecf20Sopenharmony_ci	int nrsg;
2168c2ecf20Sopenharmony_ci};
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci/* the private variable of cipher */
2198c2ecf20Sopenharmony_cistruct rk_cipher_ctx {
2208c2ecf20Sopenharmony_ci	struct crypto_engine_ctx enginectx;
2218c2ecf20Sopenharmony_ci	struct rk_crypto_info		*dev;
2228c2ecf20Sopenharmony_ci	unsigned int			keylen;
2238c2ecf20Sopenharmony_ci	u8				key[AES_MAX_KEY_SIZE];
2248c2ecf20Sopenharmony_ci	u8				iv[AES_BLOCK_SIZE];
2258c2ecf20Sopenharmony_ci	struct crypto_skcipher *fallback_tfm;
2268c2ecf20Sopenharmony_ci};
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistruct rk_cipher_rctx {
2298c2ecf20Sopenharmony_ci	u8 backup_iv[AES_BLOCK_SIZE];
2308c2ecf20Sopenharmony_ci	u32				mode;
2318c2ecf20Sopenharmony_ci	struct skcipher_request fallback_req;   // keep at the end
2328c2ecf20Sopenharmony_ci};
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cienum alg_type {
2358c2ecf20Sopenharmony_ci	ALG_TYPE_HASH,
2368c2ecf20Sopenharmony_ci	ALG_TYPE_CIPHER,
2378c2ecf20Sopenharmony_ci};
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistruct rk_crypto_tmp {
2408c2ecf20Sopenharmony_ci	struct rk_crypto_info		*dev;
2418c2ecf20Sopenharmony_ci	union {
2428c2ecf20Sopenharmony_ci		struct skcipher_alg	skcipher;
2438c2ecf20Sopenharmony_ci		struct ahash_alg	hash;
2448c2ecf20Sopenharmony_ci	} alg;
2458c2ecf20Sopenharmony_ci	enum alg_type			type;
2468c2ecf20Sopenharmony_ci};
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ciextern struct rk_crypto_tmp rk_ecb_aes_alg;
2498c2ecf20Sopenharmony_ciextern struct rk_crypto_tmp rk_cbc_aes_alg;
2508c2ecf20Sopenharmony_ciextern struct rk_crypto_tmp rk_ecb_des_alg;
2518c2ecf20Sopenharmony_ciextern struct rk_crypto_tmp rk_cbc_des_alg;
2528c2ecf20Sopenharmony_ciextern struct rk_crypto_tmp rk_ecb_des3_ede_alg;
2538c2ecf20Sopenharmony_ciextern struct rk_crypto_tmp rk_cbc_des3_ede_alg;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ciextern struct rk_crypto_tmp rk_ahash_sha1;
2568c2ecf20Sopenharmony_ciextern struct rk_crypto_tmp rk_ahash_sha256;
2578c2ecf20Sopenharmony_ciextern struct rk_crypto_tmp rk_ahash_md5;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci#endif
260