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