1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __STARFIVE_STR_H__
3 #define __STARFIVE_STR_H__
4 
5 #include <linux/delay.h>
6 #include <linux/dma-mapping.h>
7 #include <linux/dmaengine.h>
8 
9 #include <crypto/engine.h>
10 #include <crypto/sha2.h>
11 #include <crypto/sm3.h>
12 
13 #define STARFIVE_ALG_CR_OFFSET			0x0
14 #define STARFIVE_ALG_FIFO_OFFSET		0x4
15 #define STARFIVE_IE_MASK_OFFSET			0x8
16 #define STARFIVE_IE_FLAG_OFFSET			0xc
17 #define STARFIVE_DMA_IN_LEN_OFFSET		0x10
18 #define STARFIVE_DMA_OUT_LEN_OFFSET		0x14
19 
20 #define STARFIVE_IE_MASK_HASH_DONE		0x4
21 #define STARFIVE_IE_MASK_PKA_DONE		0x8
22 #define STARFIVE_IE_FLAG_HASH_DONE		0x4
23 #define STARFIVE_IE_FLAG_PKA_DONE		0x8
24 
25 #define STARFIVE_MSG_BUFFER_SIZE		SZ_16K
26 #define MAX_KEY_SIZE				SHA512_BLOCK_SIZE
27 
28 union starfive_hash_csr {
29 	u32 v;
30 	struct {
31 		u32 start			:1;
32 		u32 reset			:1;
33 		u32 ie				:1;
34 		u32 firstb			:1;
35 #define STARFIVE_HASH_SM3			0x0
36 #define STARFIVE_HASH_SHA224			0x3
37 #define STARFIVE_HASH_SHA256			0x4
38 #define STARFIVE_HASH_SHA384			0x5
39 #define STARFIVE_HASH_SHA512			0x6
40 #define STARFIVE_HASH_MODE_MASK			0x7
41 		u32 mode			:3;
42 		u32 rsvd_1			:1;
43 		u32 final			:1;
44 		u32 rsvd_2			:2;
45 #define STARFIVE_HASH_HMAC_FLAGS		0x800
46 		u32 hmac			:1;
47 		u32 rsvd_3			:1;
48 #define STARFIVE_HASH_KEY_DONE			BIT(13)
49 		u32 key_done			:1;
50 		u32 key_flag			:1;
51 		u32 hmac_done			:1;
52 #define STARFIVE_HASH_BUSY			BIT(16)
53 		u32 busy			:1;
54 		u32 hashdone			:1;
55 		u32 rsvd_4			:14;
56 	};
57 };
58 
59 union starfive_pka_cacr {
60 	u32 v;
61 	struct {
62 		u32 start			:1;
63 		u32 reset			:1;
64 		u32 ie				:1;
65 		u32 rsvd_0			:1;
66 		u32 fifo_mode			:1;
67 		u32 not_r2			:1;
68 		u32 ecc_sub			:1;
69 		u32 pre_expf			:1;
70 		u32 cmd				:4;
71 		u32 rsvd_1			:1;
72 		u32 ctrl_dummy			:1;
73 		u32 ctrl_false			:1;
74 		u32 cln_done			:1;
75 		u32 opsize			:6;
76 		u32 rsvd_2			:2;
77 		u32 exposize			:6;
78 		u32 rsvd_3			:1;
79 		u32 bigendian			:1;
80 	};
81 };
82 
83 struct starfive_rsa_key {
84 	u8	*n;
85 	u8	*e;
86 	u8	*d;
87 	int	e_bitlen;
88 	int	d_bitlen;
89 	int	bitlen;
90 	size_t	key_sz;
91 };
92 
93 union starfive_alg_cr {
94 	u32 v;
95 	struct {
96 		u32 start			:1;
97 		u32 aes_dma_en			:1;
98 		u32 rsvd_0			:1;
99 		u32 hash_dma_en			:1;
100 		u32 alg_done			:1;
101 		u32 rsvd_1			:3;
102 		u32 clear			:1;
103 		u32 rsvd_2			:23;
104 	};
105 };
106 
107 struct starfive_cryp_ctx {
108 	struct crypto_engine_ctx		enginectx;
109 	struct starfive_cryp_dev		*cryp;
110 	struct starfive_cryp_request_ctx	*rctx;
111 
112 	unsigned int				hash_mode;
113 	u8					key[MAX_KEY_SIZE];
114 	int					keylen;
115 	bool					is_hmac;
116 	struct starfive_rsa_key			rsa_key;
117 	struct crypto_akcipher			*akcipher_fbk;
118 	struct crypto_ahash			*ahash_fbk;
119 };
120 
121 struct starfive_cryp_dev {
122 	struct list_head			list;
123 	struct device				*dev;
124 	struct clk				*hclk;
125 	struct clk				*ahb;
126 	struct reset_control			*rst;
127 
128 	void __iomem				*base;
129 	phys_addr_t				phys_base;
130 
131 	u32					dma_maxburst;
132 	struct dma_chan				*tx;
133 	struct dma_chan				*rx;
134 	struct dma_slave_config			cfg_in;
135 	struct dma_slave_config			cfg_out;
136 	struct crypto_engine			*engine;
137 	struct tasklet_struct			hash_done;
138 	struct completion			pka_done;
139 	int					err;
140 	union starfive_alg_cr			alg_cr;
141 	union {
142 		struct ahash_request		*hreq;
143 	} req;
144 };
145 
146 struct starfive_cryp_request_ctx {
147 	union {
148 		union starfive_hash_csr		hash;
149 		union starfive_pka_cacr		pka;
150 	} csr;
151 
152 	struct scatterlist			*in_sg;
153 	struct scatterlist			*out_sg;
154 	struct ahash_request			ahash_fbk_req;
155 	size_t					total;
156 	size_t					nents;
157 	unsigned int				blksize;
158 	unsigned int				digsize;
159 	unsigned long				in_sg_len;
160 	u8 rsa_data[] __aligned(sizeof(u32));
161 };
162 
163 struct starfive_cryp_dev *starfive_cryp_find_dev(struct starfive_cryp_ctx *ctx);
164 
165 int starfive_hash_register_algs(void);
166 void starfive_hash_unregister_algs(void);
167 
168 int starfive_rsa_register_algs(void);
169 void starfive_rsa_unregister_algs(void);
170 
171 void starfive_hash_done_task(unsigned long param);
172 #endif
173