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