xref: /linux/drivers/crypto/starfive/jh7110-cryp.h (revision eed4edda910fe34dfae8c6bfbcf57f4593a54295)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __STARFIVE_STR_H__
3 #define __STARFIVE_STR_H__
4 
5 #include <crypto/aes.h>
6 #include <crypto/hash.h>
7 #include <crypto/scatterwalk.h>
8 #include <crypto/sha2.h>
9 #include <crypto/sm3.h>
10 #include <linux/delay.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/dmaengine.h>
13 #include <linux/interrupt.h>
14 
15 #define STARFIVE_ALG_CR_OFFSET			0x0
16 #define STARFIVE_ALG_FIFO_OFFSET		0x4
17 #define STARFIVE_IE_MASK_OFFSET			0x8
18 #define STARFIVE_IE_FLAG_OFFSET			0xc
19 #define STARFIVE_DMA_IN_LEN_OFFSET		0x10
20 #define STARFIVE_DMA_OUT_LEN_OFFSET		0x14
21 
22 #define STARFIVE_IE_MASK_AES_DONE		0x1
23 #define STARFIVE_IE_MASK_HASH_DONE		0x4
24 #define STARFIVE_IE_MASK_PKA_DONE		0x8
25 #define STARFIVE_IE_FLAG_AES_DONE		0x1
26 #define STARFIVE_IE_FLAG_HASH_DONE		0x4
27 #define STARFIVE_IE_FLAG_PKA_DONE		0x8
28 
29 #define STARFIVE_MSG_BUFFER_SIZE		SZ_16K
30 #define MAX_KEY_SIZE				SHA512_BLOCK_SIZE
31 #define STARFIVE_AES_IV_LEN			AES_BLOCK_SIZE
32 #define STARFIVE_AES_CTR_LEN			AES_BLOCK_SIZE
33 
34 union starfive_aes_csr {
35 	u32 v;
36 	struct {
37 		u32 cmode			:1;
38 #define STARFIVE_AES_KEYMODE_128		0x0
39 #define STARFIVE_AES_KEYMODE_192		0x1
40 #define STARFIVE_AES_KEYMODE_256		0x2
41 		u32 keymode			:2;
42 #define STARFIVE_AES_BUSY			BIT(3)
43 		u32 busy			:1;
44 		u32 done			:1;
45 #define STARFIVE_AES_KEY_DONE			BIT(5)
46 		u32 krdy			:1;
47 		u32 aesrst			:1;
48 		u32 ie				:1;
49 #define STARFIVE_AES_CCM_START			BIT(8)
50 		u32 ccm_start			:1;
51 #define STARFIVE_AES_MODE_ECB			0x0
52 #define STARFIVE_AES_MODE_CBC			0x1
53 #define STARFIVE_AES_MODE_CTR			0x4
54 #define STARFIVE_AES_MODE_CCM			0x5
55 #define STARFIVE_AES_MODE_GCM			0x6
56 		u32 mode			:3;
57 #define STARFIVE_AES_GCM_START			BIT(12)
58 		u32 gcm_start			:1;
59 #define STARFIVE_AES_GCM_DONE			BIT(13)
60 		u32 gcm_done			:1;
61 		u32 delay_aes			:1;
62 		u32 vaes_start			:1;
63 		u32 rsvd_0			:8;
64 #define STARFIVE_AES_MODE_XFB_1			0x0
65 #define STARFIVE_AES_MODE_XFB_128		0x5
66 		u32 stmode			:3;
67 		u32 rsvd_1			:5;
68 	};
69 };
70 
71 union starfive_hash_csr {
72 	u32 v;
73 	struct {
74 		u32 start			:1;
75 		u32 reset			:1;
76 		u32 ie				:1;
77 		u32 firstb			:1;
78 #define STARFIVE_HASH_SM3			0x0
79 #define STARFIVE_HASH_SHA224			0x3
80 #define STARFIVE_HASH_SHA256			0x4
81 #define STARFIVE_HASH_SHA384			0x5
82 #define STARFIVE_HASH_SHA512			0x6
83 #define STARFIVE_HASH_MODE_MASK			0x7
84 		u32 mode			:3;
85 		u32 rsvd_1			:1;
86 		u32 final			:1;
87 		u32 rsvd_2			:2;
88 #define STARFIVE_HASH_HMAC_FLAGS		0x800
89 		u32 hmac			:1;
90 		u32 rsvd_3			:1;
91 #define STARFIVE_HASH_KEY_DONE			BIT(13)
92 		u32 key_done			:1;
93 		u32 key_flag			:1;
94 		u32 hmac_done			:1;
95 #define STARFIVE_HASH_BUSY			BIT(16)
96 		u32 busy			:1;
97 		u32 hashdone			:1;
98 		u32 rsvd_4			:14;
99 	};
100 };
101 
102 union starfive_pka_cacr {
103 	u32 v;
104 	struct {
105 		u32 start			:1;
106 		u32 reset			:1;
107 		u32 ie				:1;
108 		u32 rsvd_0			:1;
109 		u32 fifo_mode			:1;
110 		u32 not_r2			:1;
111 		u32 ecc_sub			:1;
112 		u32 pre_expf			:1;
113 		u32 cmd				:4;
114 		u32 rsvd_1			:1;
115 		u32 ctrl_dummy			:1;
116 		u32 ctrl_false			:1;
117 		u32 cln_done			:1;
118 		u32 opsize			:6;
119 		u32 rsvd_2			:2;
120 		u32 exposize			:6;
121 		u32 rsvd_3			:1;
122 		u32 bigendian			:1;
123 	};
124 };
125 
126 union starfive_pka_casr {
127 	u32 v;
128 	struct {
129 #define STARFIVE_PKA_DONE			BIT(0)
130 		u32 done			:1;
131 		u32 rsvd_0			:31;
132 	};
133 };
134 
135 struct starfive_rsa_key {
136 	u8	*n;
137 	u8	*e;
138 	u8	*d;
139 	int	e_bitlen;
140 	int	d_bitlen;
141 	int	bitlen;
142 	size_t	key_sz;
143 };
144 
145 union starfive_alg_cr {
146 	u32 v;
147 	struct {
148 		u32 start			:1;
149 		u32 aes_dma_en			:1;
150 		u32 rsvd_0			:1;
151 		u32 hash_dma_en			:1;
152 		u32 alg_done			:1;
153 		u32 rsvd_1			:3;
154 		u32 clear			:1;
155 		u32 rsvd_2			:23;
156 	};
157 };
158 
159 struct starfive_cryp_ctx {
160 	struct starfive_cryp_dev		*cryp;
161 	struct starfive_cryp_request_ctx	*rctx;
162 
163 	unsigned int				hash_mode;
164 	u8					key[MAX_KEY_SIZE];
165 	int					keylen;
166 	bool					is_hmac;
167 	struct starfive_rsa_key			rsa_key;
168 	struct crypto_akcipher			*akcipher_fbk;
169 	struct crypto_ahash			*ahash_fbk;
170 	struct crypto_aead			*aead_fbk;
171 };
172 
173 struct starfive_cryp_dev {
174 	struct list_head			list;
175 	struct device				*dev;
176 	struct clk				*hclk;
177 	struct clk				*ahb;
178 	struct reset_control			*rst;
179 
180 	void __iomem				*base;
181 	phys_addr_t				phys_base;
182 
183 	u32					dma_maxburst;
184 	struct dma_chan				*tx;
185 	struct dma_chan				*rx;
186 	struct dma_slave_config			cfg_in;
187 	struct dma_slave_config			cfg_out;
188 	struct scatter_walk			in_walk;
189 	struct scatter_walk			out_walk;
190 	struct crypto_engine			*engine;
191 	struct tasklet_struct			aes_done;
192 	struct tasklet_struct			hash_done;
193 	size_t					assoclen;
194 	size_t					total_in;
195 	size_t					total_out;
196 	u32					tag_in[4];
197 	u32					tag_out[4];
198 	unsigned int				authsize;
199 	unsigned long				flags;
200 	int					err;
201 	bool					side_chan;
202 	union starfive_alg_cr			alg_cr;
203 	union {
204 		struct ahash_request		*hreq;
205 		struct aead_request		*areq;
206 		struct skcipher_request		*sreq;
207 	} req;
208 };
209 
210 struct starfive_cryp_request_ctx {
211 	union {
212 		union starfive_hash_csr		hash;
213 		union starfive_pka_cacr		pka;
214 		union starfive_aes_csr		aes;
215 	} csr;
216 
217 	struct scatterlist			*in_sg;
218 	struct scatterlist			*out_sg;
219 	struct ahash_request			ahash_fbk_req;
220 	size_t					total;
221 	size_t					nents;
222 	unsigned int				blksize;
223 	unsigned int				digsize;
224 	unsigned long				in_sg_len;
225 	unsigned char				*adata;
226 	u8 rsa_data[] __aligned(sizeof(u32));
227 };
228 
229 struct starfive_cryp_dev *starfive_cryp_find_dev(struct starfive_cryp_ctx *ctx);
230 
231 int starfive_hash_register_algs(void);
232 void starfive_hash_unregister_algs(void);
233 
234 int starfive_rsa_register_algs(void);
235 void starfive_rsa_unregister_algs(void);
236 
237 int starfive_aes_register_algs(void);
238 void starfive_aes_unregister_algs(void);
239 
240 void starfive_hash_done_task(unsigned long param);
241 void starfive_aes_done_task(unsigned long param);
242 #endif
243