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