162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci#ifndef __RK3288_CRYPTO_H__ 362306a36Sopenharmony_ci#define __RK3288_CRYPTO_H__ 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci#include <crypto/aes.h> 662306a36Sopenharmony_ci#include <crypto/engine.h> 762306a36Sopenharmony_ci#include <crypto/internal/des.h> 862306a36Sopenharmony_ci#include <crypto/internal/hash.h> 962306a36Sopenharmony_ci#include <crypto/internal/skcipher.h> 1062306a36Sopenharmony_ci#include <crypto/md5.h> 1162306a36Sopenharmony_ci#include <crypto/sha1.h> 1262306a36Sopenharmony_ci#include <crypto/sha2.h> 1362306a36Sopenharmony_ci#include <linux/dma-mapping.h> 1462306a36Sopenharmony_ci#include <linux/interrupt.h> 1562306a36Sopenharmony_ci#include <linux/pm_runtime.h> 1662306a36Sopenharmony_ci#include <linux/scatterlist.h> 1762306a36Sopenharmony_ci#include <linux/types.h> 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#define _SBF(v, f) ((v) << (f)) 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci/* Crypto control registers*/ 2262306a36Sopenharmony_ci#define RK_CRYPTO_INTSTS 0x0000 2362306a36Sopenharmony_ci#define RK_CRYPTO_PKA_DONE_INT BIT(5) 2462306a36Sopenharmony_ci#define RK_CRYPTO_HASH_DONE_INT BIT(4) 2562306a36Sopenharmony_ci#define RK_CRYPTO_HRDMA_ERR_INT BIT(3) 2662306a36Sopenharmony_ci#define RK_CRYPTO_HRDMA_DONE_INT BIT(2) 2762306a36Sopenharmony_ci#define RK_CRYPTO_BCDMA_ERR_INT BIT(1) 2862306a36Sopenharmony_ci#define RK_CRYPTO_BCDMA_DONE_INT BIT(0) 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci#define RK_CRYPTO_INTENA 0x0004 3162306a36Sopenharmony_ci#define RK_CRYPTO_PKA_DONE_ENA BIT(5) 3262306a36Sopenharmony_ci#define RK_CRYPTO_HASH_DONE_ENA BIT(4) 3362306a36Sopenharmony_ci#define RK_CRYPTO_HRDMA_ERR_ENA BIT(3) 3462306a36Sopenharmony_ci#define RK_CRYPTO_HRDMA_DONE_ENA BIT(2) 3562306a36Sopenharmony_ci#define RK_CRYPTO_BCDMA_ERR_ENA BIT(1) 3662306a36Sopenharmony_ci#define RK_CRYPTO_BCDMA_DONE_ENA BIT(0) 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci#define RK_CRYPTO_CTRL 0x0008 3962306a36Sopenharmony_ci#define RK_CRYPTO_WRITE_MASK _SBF(0xFFFF, 16) 4062306a36Sopenharmony_ci#define RK_CRYPTO_TRNG_FLUSH BIT(9) 4162306a36Sopenharmony_ci#define RK_CRYPTO_TRNG_START BIT(8) 4262306a36Sopenharmony_ci#define RK_CRYPTO_PKA_FLUSH BIT(7) 4362306a36Sopenharmony_ci#define RK_CRYPTO_HASH_FLUSH BIT(6) 4462306a36Sopenharmony_ci#define RK_CRYPTO_BLOCK_FLUSH BIT(5) 4562306a36Sopenharmony_ci#define RK_CRYPTO_PKA_START BIT(4) 4662306a36Sopenharmony_ci#define RK_CRYPTO_HASH_START BIT(3) 4762306a36Sopenharmony_ci#define RK_CRYPTO_BLOCK_START BIT(2) 4862306a36Sopenharmony_ci#define RK_CRYPTO_TDES_START BIT(1) 4962306a36Sopenharmony_ci#define RK_CRYPTO_AES_START BIT(0) 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci#define RK_CRYPTO_CONF 0x000c 5262306a36Sopenharmony_ci/* HASH Receive DMA Address Mode: fix | increment */ 5362306a36Sopenharmony_ci#define RK_CRYPTO_HR_ADDR_MODE BIT(8) 5462306a36Sopenharmony_ci/* Block Transmit DMA Address Mode: fix | increment */ 5562306a36Sopenharmony_ci#define RK_CRYPTO_BT_ADDR_MODE BIT(7) 5662306a36Sopenharmony_ci/* Block Receive DMA Address Mode: fix | increment */ 5762306a36Sopenharmony_ci#define RK_CRYPTO_BR_ADDR_MODE BIT(6) 5862306a36Sopenharmony_ci#define RK_CRYPTO_BYTESWAP_HRFIFO BIT(5) 5962306a36Sopenharmony_ci#define RK_CRYPTO_BYTESWAP_BTFIFO BIT(4) 6062306a36Sopenharmony_ci#define RK_CRYPTO_BYTESWAP_BRFIFO BIT(3) 6162306a36Sopenharmony_ci/* AES = 0 OR DES = 1 */ 6262306a36Sopenharmony_ci#define RK_CRYPTO_DESSEL BIT(2) 6362306a36Sopenharmony_ci#define RK_CYYPTO_HASHINSEL_INDEPENDENT_SOURCE _SBF(0x00, 0) 6462306a36Sopenharmony_ci#define RK_CYYPTO_HASHINSEL_BLOCK_CIPHER_INPUT _SBF(0x01, 0) 6562306a36Sopenharmony_ci#define RK_CYYPTO_HASHINSEL_BLOCK_CIPHER_OUTPUT _SBF(0x02, 0) 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci/* Block Receiving DMA Start Address Register */ 6862306a36Sopenharmony_ci#define RK_CRYPTO_BRDMAS 0x0010 6962306a36Sopenharmony_ci/* Block Transmitting DMA Start Address Register */ 7062306a36Sopenharmony_ci#define RK_CRYPTO_BTDMAS 0x0014 7162306a36Sopenharmony_ci/* Block Receiving DMA Length Register */ 7262306a36Sopenharmony_ci#define RK_CRYPTO_BRDMAL 0x0018 7362306a36Sopenharmony_ci/* Hash Receiving DMA Start Address Register */ 7462306a36Sopenharmony_ci#define RK_CRYPTO_HRDMAS 0x001c 7562306a36Sopenharmony_ci/* Hash Receiving DMA Length Register */ 7662306a36Sopenharmony_ci#define RK_CRYPTO_HRDMAL 0x0020 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci/* AES registers */ 7962306a36Sopenharmony_ci#define RK_CRYPTO_AES_CTRL 0x0080 8062306a36Sopenharmony_ci#define RK_CRYPTO_AES_BYTESWAP_CNT BIT(11) 8162306a36Sopenharmony_ci#define RK_CRYPTO_AES_BYTESWAP_KEY BIT(10) 8262306a36Sopenharmony_ci#define RK_CRYPTO_AES_BYTESWAP_IV BIT(9) 8362306a36Sopenharmony_ci#define RK_CRYPTO_AES_BYTESWAP_DO BIT(8) 8462306a36Sopenharmony_ci#define RK_CRYPTO_AES_BYTESWAP_DI BIT(7) 8562306a36Sopenharmony_ci#define RK_CRYPTO_AES_KEY_CHANGE BIT(6) 8662306a36Sopenharmony_ci#define RK_CRYPTO_AES_ECB_MODE _SBF(0x00, 4) 8762306a36Sopenharmony_ci#define RK_CRYPTO_AES_CBC_MODE _SBF(0x01, 4) 8862306a36Sopenharmony_ci#define RK_CRYPTO_AES_CTR_MODE _SBF(0x02, 4) 8962306a36Sopenharmony_ci#define RK_CRYPTO_AES_128BIT_key _SBF(0x00, 2) 9062306a36Sopenharmony_ci#define RK_CRYPTO_AES_192BIT_key _SBF(0x01, 2) 9162306a36Sopenharmony_ci#define RK_CRYPTO_AES_256BIT_key _SBF(0x02, 2) 9262306a36Sopenharmony_ci/* Slave = 0 / fifo = 1 */ 9362306a36Sopenharmony_ci#define RK_CRYPTO_AES_FIFO_MODE BIT(1) 9462306a36Sopenharmony_ci/* Encryption = 0 , Decryption = 1 */ 9562306a36Sopenharmony_ci#define RK_CRYPTO_AES_DEC BIT(0) 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci#define RK_CRYPTO_AES_STS 0x0084 9862306a36Sopenharmony_ci#define RK_CRYPTO_AES_DONE BIT(0) 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci/* AES Input Data 0-3 Register */ 10162306a36Sopenharmony_ci#define RK_CRYPTO_AES_DIN_0 0x0088 10262306a36Sopenharmony_ci#define RK_CRYPTO_AES_DIN_1 0x008c 10362306a36Sopenharmony_ci#define RK_CRYPTO_AES_DIN_2 0x0090 10462306a36Sopenharmony_ci#define RK_CRYPTO_AES_DIN_3 0x0094 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci/* AES output Data 0-3 Register */ 10762306a36Sopenharmony_ci#define RK_CRYPTO_AES_DOUT_0 0x0098 10862306a36Sopenharmony_ci#define RK_CRYPTO_AES_DOUT_1 0x009c 10962306a36Sopenharmony_ci#define RK_CRYPTO_AES_DOUT_2 0x00a0 11062306a36Sopenharmony_ci#define RK_CRYPTO_AES_DOUT_3 0x00a4 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci/* AES IV Data 0-3 Register */ 11362306a36Sopenharmony_ci#define RK_CRYPTO_AES_IV_0 0x00a8 11462306a36Sopenharmony_ci#define RK_CRYPTO_AES_IV_1 0x00ac 11562306a36Sopenharmony_ci#define RK_CRYPTO_AES_IV_2 0x00b0 11662306a36Sopenharmony_ci#define RK_CRYPTO_AES_IV_3 0x00b4 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci/* AES Key Data 0-3 Register */ 11962306a36Sopenharmony_ci#define RK_CRYPTO_AES_KEY_0 0x00b8 12062306a36Sopenharmony_ci#define RK_CRYPTO_AES_KEY_1 0x00bc 12162306a36Sopenharmony_ci#define RK_CRYPTO_AES_KEY_2 0x00c0 12262306a36Sopenharmony_ci#define RK_CRYPTO_AES_KEY_3 0x00c4 12362306a36Sopenharmony_ci#define RK_CRYPTO_AES_KEY_4 0x00c8 12462306a36Sopenharmony_ci#define RK_CRYPTO_AES_KEY_5 0x00cc 12562306a36Sopenharmony_ci#define RK_CRYPTO_AES_KEY_6 0x00d0 12662306a36Sopenharmony_ci#define RK_CRYPTO_AES_KEY_7 0x00d4 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci/* des/tdes */ 12962306a36Sopenharmony_ci#define RK_CRYPTO_TDES_CTRL 0x0100 13062306a36Sopenharmony_ci#define RK_CRYPTO_TDES_BYTESWAP_KEY BIT(8) 13162306a36Sopenharmony_ci#define RK_CRYPTO_TDES_BYTESWAP_IV BIT(7) 13262306a36Sopenharmony_ci#define RK_CRYPTO_TDES_BYTESWAP_DO BIT(6) 13362306a36Sopenharmony_ci#define RK_CRYPTO_TDES_BYTESWAP_DI BIT(5) 13462306a36Sopenharmony_ci/* 0: ECB, 1: CBC */ 13562306a36Sopenharmony_ci#define RK_CRYPTO_TDES_CHAINMODE_CBC BIT(4) 13662306a36Sopenharmony_ci/* TDES Key Mode, 0 : EDE, 1 : EEE */ 13762306a36Sopenharmony_ci#define RK_CRYPTO_TDES_EEE BIT(3) 13862306a36Sopenharmony_ci/* 0: DES, 1:TDES */ 13962306a36Sopenharmony_ci#define RK_CRYPTO_TDES_SELECT BIT(2) 14062306a36Sopenharmony_ci/* 0: Slave, 1:Fifo */ 14162306a36Sopenharmony_ci#define RK_CRYPTO_TDES_FIFO_MODE BIT(1) 14262306a36Sopenharmony_ci/* Encryption = 0 , Decryption = 1 */ 14362306a36Sopenharmony_ci#define RK_CRYPTO_TDES_DEC BIT(0) 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ci#define RK_CRYPTO_TDES_STS 0x0104 14662306a36Sopenharmony_ci#define RK_CRYPTO_TDES_DONE BIT(0) 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci#define RK_CRYPTO_TDES_DIN_0 0x0108 14962306a36Sopenharmony_ci#define RK_CRYPTO_TDES_DIN_1 0x010c 15062306a36Sopenharmony_ci#define RK_CRYPTO_TDES_DOUT_0 0x0110 15162306a36Sopenharmony_ci#define RK_CRYPTO_TDES_DOUT_1 0x0114 15262306a36Sopenharmony_ci#define RK_CRYPTO_TDES_IV_0 0x0118 15362306a36Sopenharmony_ci#define RK_CRYPTO_TDES_IV_1 0x011c 15462306a36Sopenharmony_ci#define RK_CRYPTO_TDES_KEY1_0 0x0120 15562306a36Sopenharmony_ci#define RK_CRYPTO_TDES_KEY1_1 0x0124 15662306a36Sopenharmony_ci#define RK_CRYPTO_TDES_KEY2_0 0x0128 15762306a36Sopenharmony_ci#define RK_CRYPTO_TDES_KEY2_1 0x012c 15862306a36Sopenharmony_ci#define RK_CRYPTO_TDES_KEY3_0 0x0130 15962306a36Sopenharmony_ci#define RK_CRYPTO_TDES_KEY3_1 0x0134 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci/* HASH */ 16262306a36Sopenharmony_ci#define RK_CRYPTO_HASH_CTRL 0x0180 16362306a36Sopenharmony_ci#define RK_CRYPTO_HASH_SWAP_DO BIT(3) 16462306a36Sopenharmony_ci#define RK_CRYPTO_HASH_SWAP_DI BIT(2) 16562306a36Sopenharmony_ci#define RK_CRYPTO_HASH_SHA1 _SBF(0x00, 0) 16662306a36Sopenharmony_ci#define RK_CRYPTO_HASH_MD5 _SBF(0x01, 0) 16762306a36Sopenharmony_ci#define RK_CRYPTO_HASH_SHA256 _SBF(0x02, 0) 16862306a36Sopenharmony_ci#define RK_CRYPTO_HASH_PRNG _SBF(0x03, 0) 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci#define RK_CRYPTO_HASH_STS 0x0184 17162306a36Sopenharmony_ci#define RK_CRYPTO_HASH_DONE BIT(0) 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_ci#define RK_CRYPTO_HASH_MSG_LEN 0x0188 17462306a36Sopenharmony_ci#define RK_CRYPTO_HASH_DOUT_0 0x018c 17562306a36Sopenharmony_ci#define RK_CRYPTO_HASH_DOUT_1 0x0190 17662306a36Sopenharmony_ci#define RK_CRYPTO_HASH_DOUT_2 0x0194 17762306a36Sopenharmony_ci#define RK_CRYPTO_HASH_DOUT_3 0x0198 17862306a36Sopenharmony_ci#define RK_CRYPTO_HASH_DOUT_4 0x019c 17962306a36Sopenharmony_ci#define RK_CRYPTO_HASH_DOUT_5 0x01a0 18062306a36Sopenharmony_ci#define RK_CRYPTO_HASH_DOUT_6 0x01a4 18162306a36Sopenharmony_ci#define RK_CRYPTO_HASH_DOUT_7 0x01a8 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ci#define CRYPTO_READ(dev, offset) \ 18462306a36Sopenharmony_ci readl_relaxed(((dev)->reg + (offset))) 18562306a36Sopenharmony_ci#define CRYPTO_WRITE(dev, offset, val) \ 18662306a36Sopenharmony_ci writel_relaxed((val), ((dev)->reg + (offset))) 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ci#define RK_MAX_CLKS 4 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci/* 19162306a36Sopenharmony_ci * struct rockchip_ip - struct for managing a list of RK crypto instance 19262306a36Sopenharmony_ci * @dev_list: Used for doing a list of rk_crypto_info 19362306a36Sopenharmony_ci * @lock: Control access to dev_list 19462306a36Sopenharmony_ci * @dbgfs_dir: Debugfs dentry for statistic directory 19562306a36Sopenharmony_ci * @dbgfs_stats: Debugfs dentry for statistic counters 19662306a36Sopenharmony_ci */ 19762306a36Sopenharmony_cistruct rockchip_ip { 19862306a36Sopenharmony_ci struct list_head dev_list; 19962306a36Sopenharmony_ci spinlock_t lock; /* Control access to dev_list */ 20062306a36Sopenharmony_ci struct dentry *dbgfs_dir; 20162306a36Sopenharmony_ci struct dentry *dbgfs_stats; 20262306a36Sopenharmony_ci}; 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_cistruct rk_clks { 20562306a36Sopenharmony_ci const char *name; 20662306a36Sopenharmony_ci unsigned long max; 20762306a36Sopenharmony_ci}; 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_cistruct rk_variant { 21062306a36Sopenharmony_ci int num_clks; 21162306a36Sopenharmony_ci struct rk_clks rkclks[RK_MAX_CLKS]; 21262306a36Sopenharmony_ci}; 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_cistruct rk_crypto_info { 21562306a36Sopenharmony_ci struct list_head list; 21662306a36Sopenharmony_ci struct device *dev; 21762306a36Sopenharmony_ci struct clk_bulk_data *clks; 21862306a36Sopenharmony_ci int num_clks; 21962306a36Sopenharmony_ci struct reset_control *rst; 22062306a36Sopenharmony_ci void __iomem *reg; 22162306a36Sopenharmony_ci int irq; 22262306a36Sopenharmony_ci const struct rk_variant *variant; 22362306a36Sopenharmony_ci unsigned long nreq; 22462306a36Sopenharmony_ci struct crypto_engine *engine; 22562306a36Sopenharmony_ci struct completion complete; 22662306a36Sopenharmony_ci int status; 22762306a36Sopenharmony_ci}; 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_ci/* the private variable of hash */ 23062306a36Sopenharmony_cistruct rk_ahash_ctx { 23162306a36Sopenharmony_ci /* for fallback */ 23262306a36Sopenharmony_ci struct crypto_ahash *fallback_tfm; 23362306a36Sopenharmony_ci}; 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_ci/* the private variable of hash for fallback */ 23662306a36Sopenharmony_cistruct rk_ahash_rctx { 23762306a36Sopenharmony_ci struct rk_crypto_info *dev; 23862306a36Sopenharmony_ci struct ahash_request fallback_req; 23962306a36Sopenharmony_ci u32 mode; 24062306a36Sopenharmony_ci int nrsg; 24162306a36Sopenharmony_ci}; 24262306a36Sopenharmony_ci 24362306a36Sopenharmony_ci/* the private variable of cipher */ 24462306a36Sopenharmony_cistruct rk_cipher_ctx { 24562306a36Sopenharmony_ci unsigned int keylen; 24662306a36Sopenharmony_ci u8 key[AES_MAX_KEY_SIZE]; 24762306a36Sopenharmony_ci u8 iv[AES_BLOCK_SIZE]; 24862306a36Sopenharmony_ci struct crypto_skcipher *fallback_tfm; 24962306a36Sopenharmony_ci}; 25062306a36Sopenharmony_ci 25162306a36Sopenharmony_cistruct rk_cipher_rctx { 25262306a36Sopenharmony_ci struct rk_crypto_info *dev; 25362306a36Sopenharmony_ci u8 backup_iv[AES_BLOCK_SIZE]; 25462306a36Sopenharmony_ci u32 mode; 25562306a36Sopenharmony_ci struct skcipher_request fallback_req; // keep at the end 25662306a36Sopenharmony_ci}; 25762306a36Sopenharmony_ci 25862306a36Sopenharmony_cistruct rk_crypto_tmp { 25962306a36Sopenharmony_ci u32 type; 26062306a36Sopenharmony_ci struct rk_crypto_info *dev; 26162306a36Sopenharmony_ci union { 26262306a36Sopenharmony_ci struct skcipher_engine_alg skcipher; 26362306a36Sopenharmony_ci struct ahash_engine_alg hash; 26462306a36Sopenharmony_ci } alg; 26562306a36Sopenharmony_ci unsigned long stat_req; 26662306a36Sopenharmony_ci unsigned long stat_fb; 26762306a36Sopenharmony_ci unsigned long stat_fb_len; 26862306a36Sopenharmony_ci unsigned long stat_fb_sglen; 26962306a36Sopenharmony_ci unsigned long stat_fb_align; 27062306a36Sopenharmony_ci unsigned long stat_fb_sgdiff; 27162306a36Sopenharmony_ci}; 27262306a36Sopenharmony_ci 27362306a36Sopenharmony_ciextern struct rk_crypto_tmp rk_ecb_aes_alg; 27462306a36Sopenharmony_ciextern struct rk_crypto_tmp rk_cbc_aes_alg; 27562306a36Sopenharmony_ciextern struct rk_crypto_tmp rk_ecb_des_alg; 27662306a36Sopenharmony_ciextern struct rk_crypto_tmp rk_cbc_des_alg; 27762306a36Sopenharmony_ciextern struct rk_crypto_tmp rk_ecb_des3_ede_alg; 27862306a36Sopenharmony_ciextern struct rk_crypto_tmp rk_cbc_des3_ede_alg; 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ciextern struct rk_crypto_tmp rk_ahash_sha1; 28162306a36Sopenharmony_ciextern struct rk_crypto_tmp rk_ahash_sha256; 28262306a36Sopenharmony_ciextern struct rk_crypto_tmp rk_ahash_md5; 28362306a36Sopenharmony_ci 28462306a36Sopenharmony_cistruct rk_crypto_info *get_rk_crypto(void); 28562306a36Sopenharmony_ci#endif 286