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