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