1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __RK3288_CRYPTO_H__ 3 #define __RK3288_CRYPTO_H__ 4 5 #include <crypto/aes.h> 6 #include <crypto/internal/des.h> 7 #include <crypto/algapi.h> 8 #include <linux/dma-mapping.h> 9 #include <linux/interrupt.h> 10 #include <linux/delay.h> 11 #include <linux/scatterlist.h> 12 #include <crypto/engine.h> 13 #include <crypto/internal/hash.h> 14 #include <crypto/internal/skcipher.h> 15 16 #include <crypto/md5.h> 17 #include <crypto/sha1.h> 18 #include <crypto/sha2.h> 19 20 #define _SBF(v, f) ((v) << (f)) 21 22 /* Crypto control registers*/ 23 #define RK_CRYPTO_INTSTS 0x0000 24 #define RK_CRYPTO_PKA_DONE_INT BIT(5) 25 #define RK_CRYPTO_HASH_DONE_INT BIT(4) 26 #define RK_CRYPTO_HRDMA_ERR_INT BIT(3) 27 #define RK_CRYPTO_HRDMA_DONE_INT BIT(2) 28 #define RK_CRYPTO_BCDMA_ERR_INT BIT(1) 29 #define RK_CRYPTO_BCDMA_DONE_INT BIT(0) 30 31 #define RK_CRYPTO_INTENA 0x0004 32 #define RK_CRYPTO_PKA_DONE_ENA BIT(5) 33 #define RK_CRYPTO_HASH_DONE_ENA BIT(4) 34 #define RK_CRYPTO_HRDMA_ERR_ENA BIT(3) 35 #define RK_CRYPTO_HRDMA_DONE_ENA BIT(2) 36 #define RK_CRYPTO_BCDMA_ERR_ENA BIT(1) 37 #define RK_CRYPTO_BCDMA_DONE_ENA BIT(0) 38 39 #define RK_CRYPTO_CTRL 0x0008 40 #define RK_CRYPTO_WRITE_MASK _SBF(0xFFFF, 16) 41 #define RK_CRYPTO_TRNG_FLUSH BIT(9) 42 #define RK_CRYPTO_TRNG_START BIT(8) 43 #define RK_CRYPTO_PKA_FLUSH BIT(7) 44 #define RK_CRYPTO_HASH_FLUSH BIT(6) 45 #define RK_CRYPTO_BLOCK_FLUSH BIT(5) 46 #define RK_CRYPTO_PKA_START BIT(4) 47 #define RK_CRYPTO_HASH_START BIT(3) 48 #define RK_CRYPTO_BLOCK_START BIT(2) 49 #define RK_CRYPTO_TDES_START BIT(1) 50 #define RK_CRYPTO_AES_START BIT(0) 51 52 #define RK_CRYPTO_CONF 0x000c 53 /* HASH Receive DMA Address Mode: fix | increment */ 54 #define RK_CRYPTO_HR_ADDR_MODE BIT(8) 55 /* Block Transmit DMA Address Mode: fix | increment */ 56 #define RK_CRYPTO_BT_ADDR_MODE BIT(7) 57 /* Block Receive DMA Address Mode: fix | increment */ 58 #define RK_CRYPTO_BR_ADDR_MODE BIT(6) 59 #define RK_CRYPTO_BYTESWAP_HRFIFO BIT(5) 60 #define RK_CRYPTO_BYTESWAP_BTFIFO BIT(4) 61 #define RK_CRYPTO_BYTESWAP_BRFIFO BIT(3) 62 /* AES = 0 OR DES = 1 */ 63 #define RK_CRYPTO_DESSEL BIT(2) 64 #define RK_CYYPTO_HASHINSEL_INDEPENDENT_SOURCE _SBF(0x00, 0) 65 #define RK_CYYPTO_HASHINSEL_BLOCK_CIPHER_INPUT _SBF(0x01, 0) 66 #define RK_CYYPTO_HASHINSEL_BLOCK_CIPHER_OUTPUT _SBF(0x02, 0) 67 68 /* Block Receiving DMA Start Address Register */ 69 #define RK_CRYPTO_BRDMAS 0x0010 70 /* Block Transmitting DMA Start Address Register */ 71 #define RK_CRYPTO_BTDMAS 0x0014 72 /* Block Receiving DMA Length Register */ 73 #define RK_CRYPTO_BRDMAL 0x0018 74 /* Hash Receiving DMA Start Address Register */ 75 #define RK_CRYPTO_HRDMAS 0x001c 76 /* Hash Receiving DMA Length Register */ 77 #define RK_CRYPTO_HRDMAL 0x0020 78 79 /* AES registers */ 80 #define RK_CRYPTO_AES_CTRL 0x0080 81 #define RK_CRYPTO_AES_BYTESWAP_CNT BIT(11) 82 #define RK_CRYPTO_AES_BYTESWAP_KEY BIT(10) 83 #define RK_CRYPTO_AES_BYTESWAP_IV BIT(9) 84 #define RK_CRYPTO_AES_BYTESWAP_DO BIT(8) 85 #define RK_CRYPTO_AES_BYTESWAP_DI BIT(7) 86 #define RK_CRYPTO_AES_KEY_CHANGE BIT(6) 87 #define RK_CRYPTO_AES_ECB_MODE _SBF(0x00, 4) 88 #define RK_CRYPTO_AES_CBC_MODE _SBF(0x01, 4) 89 #define RK_CRYPTO_AES_CTR_MODE _SBF(0x02, 4) 90 #define RK_CRYPTO_AES_128BIT_key _SBF(0x00, 2) 91 #define RK_CRYPTO_AES_192BIT_key _SBF(0x01, 2) 92 #define RK_CRYPTO_AES_256BIT_key _SBF(0x02, 2) 93 /* Slave = 0 / fifo = 1 */ 94 #define RK_CRYPTO_AES_FIFO_MODE BIT(1) 95 /* Encryption = 0 , Decryption = 1 */ 96 #define RK_CRYPTO_AES_DEC BIT(0) 97 98 #define RK_CRYPTO_AES_STS 0x0084 99 #define RK_CRYPTO_AES_DONE BIT(0) 100 101 /* AES Input Data 0-3 Register */ 102 #define RK_CRYPTO_AES_DIN_0 0x0088 103 #define RK_CRYPTO_AES_DIN_1 0x008c 104 #define RK_CRYPTO_AES_DIN_2 0x0090 105 #define RK_CRYPTO_AES_DIN_3 0x0094 106 107 /* AES output Data 0-3 Register */ 108 #define RK_CRYPTO_AES_DOUT_0 0x0098 109 #define RK_CRYPTO_AES_DOUT_1 0x009c 110 #define RK_CRYPTO_AES_DOUT_2 0x00a0 111 #define RK_CRYPTO_AES_DOUT_3 0x00a4 112 113 /* AES IV Data 0-3 Register */ 114 #define RK_CRYPTO_AES_IV_0 0x00a8 115 #define RK_CRYPTO_AES_IV_1 0x00ac 116 #define RK_CRYPTO_AES_IV_2 0x00b0 117 #define RK_CRYPTO_AES_IV_3 0x00b4 118 119 /* AES Key Data 0-3 Register */ 120 #define RK_CRYPTO_AES_KEY_0 0x00b8 121 #define RK_CRYPTO_AES_KEY_1 0x00bc 122 #define RK_CRYPTO_AES_KEY_2 0x00c0 123 #define RK_CRYPTO_AES_KEY_3 0x00c4 124 #define RK_CRYPTO_AES_KEY_4 0x00c8 125 #define RK_CRYPTO_AES_KEY_5 0x00cc 126 #define RK_CRYPTO_AES_KEY_6 0x00d0 127 #define RK_CRYPTO_AES_KEY_7 0x00d4 128 129 /* des/tdes */ 130 #define RK_CRYPTO_TDES_CTRL 0x0100 131 #define RK_CRYPTO_TDES_BYTESWAP_KEY BIT(8) 132 #define RK_CRYPTO_TDES_BYTESWAP_IV BIT(7) 133 #define RK_CRYPTO_TDES_BYTESWAP_DO BIT(6) 134 #define RK_CRYPTO_TDES_BYTESWAP_DI BIT(5) 135 /* 0: ECB, 1: CBC */ 136 #define RK_CRYPTO_TDES_CHAINMODE_CBC BIT(4) 137 /* TDES Key Mode, 0 : EDE, 1 : EEE */ 138 #define RK_CRYPTO_TDES_EEE BIT(3) 139 /* 0: DES, 1:TDES */ 140 #define RK_CRYPTO_TDES_SELECT BIT(2) 141 /* 0: Slave, 1:Fifo */ 142 #define RK_CRYPTO_TDES_FIFO_MODE BIT(1) 143 /* Encryption = 0 , Decryption = 1 */ 144 #define RK_CRYPTO_TDES_DEC BIT(0) 145 146 #define RK_CRYPTO_TDES_STS 0x0104 147 #define RK_CRYPTO_TDES_DONE BIT(0) 148 149 #define RK_CRYPTO_TDES_DIN_0 0x0108 150 #define RK_CRYPTO_TDES_DIN_1 0x010c 151 #define RK_CRYPTO_TDES_DOUT_0 0x0110 152 #define RK_CRYPTO_TDES_DOUT_1 0x0114 153 #define RK_CRYPTO_TDES_IV_0 0x0118 154 #define RK_CRYPTO_TDES_IV_1 0x011c 155 #define RK_CRYPTO_TDES_KEY1_0 0x0120 156 #define RK_CRYPTO_TDES_KEY1_1 0x0124 157 #define RK_CRYPTO_TDES_KEY2_0 0x0128 158 #define RK_CRYPTO_TDES_KEY2_1 0x012c 159 #define RK_CRYPTO_TDES_KEY3_0 0x0130 160 #define RK_CRYPTO_TDES_KEY3_1 0x0134 161 162 /* HASH */ 163 #define RK_CRYPTO_HASH_CTRL 0x0180 164 #define RK_CRYPTO_HASH_SWAP_DO BIT(3) 165 #define RK_CRYPTO_HASH_SWAP_DI BIT(2) 166 #define RK_CRYPTO_HASH_SHA1 _SBF(0x00, 0) 167 #define RK_CRYPTO_HASH_MD5 _SBF(0x01, 0) 168 #define RK_CRYPTO_HASH_SHA256 _SBF(0x02, 0) 169 #define RK_CRYPTO_HASH_PRNG _SBF(0x03, 0) 170 171 #define RK_CRYPTO_HASH_STS 0x0184 172 #define RK_CRYPTO_HASH_DONE BIT(0) 173 174 #define RK_CRYPTO_HASH_MSG_LEN 0x0188 175 #define RK_CRYPTO_HASH_DOUT_0 0x018c 176 #define RK_CRYPTO_HASH_DOUT_1 0x0190 177 #define RK_CRYPTO_HASH_DOUT_2 0x0194 178 #define RK_CRYPTO_HASH_DOUT_3 0x0198 179 #define RK_CRYPTO_HASH_DOUT_4 0x019c 180 #define RK_CRYPTO_HASH_DOUT_5 0x01a0 181 #define RK_CRYPTO_HASH_DOUT_6 0x01a4 182 #define RK_CRYPTO_HASH_DOUT_7 0x01a8 183 184 #define CRYPTO_READ(dev, offset) \ 185 readl_relaxed(((dev)->reg + (offset))) 186 #define CRYPTO_WRITE(dev, offset, val) \ 187 writel_relaxed((val), ((dev)->reg + (offset))) 188 189 struct rk_crypto_info { 190 struct device *dev; 191 struct clk *aclk; 192 struct clk *hclk; 193 struct clk *sclk; 194 struct clk *dmaclk; 195 struct reset_control *rst; 196 void __iomem *reg; 197 int irq; 198 199 struct crypto_engine *engine; 200 struct completion complete; 201 int status; 202 }; 203 204 /* the private variable of hash */ 205 struct rk_ahash_ctx { 206 struct crypto_engine_ctx enginectx; 207 struct rk_crypto_info *dev; 208 /* for fallback */ 209 struct crypto_ahash *fallback_tfm; 210 }; 211 212 /* the privete variable of hash for fallback */ 213 struct rk_ahash_rctx { 214 struct ahash_request fallback_req; 215 u32 mode; 216 int nrsg; 217 }; 218 219 /* the private variable of cipher */ 220 struct rk_cipher_ctx { 221 struct crypto_engine_ctx enginectx; 222 struct rk_crypto_info *dev; 223 unsigned int keylen; 224 u8 key[AES_MAX_KEY_SIZE]; 225 u8 iv[AES_BLOCK_SIZE]; 226 struct crypto_skcipher *fallback_tfm; 227 }; 228 229 struct rk_cipher_rctx { 230 u8 backup_iv[AES_BLOCK_SIZE]; 231 u32 mode; 232 struct skcipher_request fallback_req; // keep at the end 233 }; 234 235 enum alg_type { 236 ALG_TYPE_HASH, 237 ALG_TYPE_CIPHER, 238 }; 239 240 struct rk_crypto_tmp { 241 struct rk_crypto_info *dev; 242 union { 243 struct skcipher_alg skcipher; 244 struct ahash_alg hash; 245 } alg; 246 enum alg_type type; 247 }; 248 249 extern struct rk_crypto_tmp rk_ecb_aes_alg; 250 extern struct rk_crypto_tmp rk_cbc_aes_alg; 251 extern struct rk_crypto_tmp rk_ecb_des_alg; 252 extern struct rk_crypto_tmp rk_cbc_des_alg; 253 extern struct rk_crypto_tmp rk_ecb_des3_ede_alg; 254 extern struct rk_crypto_tmp rk_cbc_des3_ede_alg; 255 256 extern struct rk_crypto_tmp rk_ahash_sha1; 257 extern struct rk_crypto_tmp rk_ahash_sha256; 258 extern struct rk_crypto_tmp rk_ahash_md5; 259 260 #endif 261