1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Driver for EIP97 cryptographic accelerator.
5 * Copyright (c) 2016 Ryder Lee <ryder.lee@mediatek.com>
8 #ifndef __MTK_PLATFORM_H_
9 #define __MTK_PLATFORM_H_
11 #include <crypto/algapi.h>
12 #include <crypto/internal/aead.h>
13 #include <crypto/internal/hash.h>
14 #include <crypto/scatterwalk.h>
15 #include <crypto/skcipher.h>
16 #include <linux/crypto.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/interrupt.h>
19 #include <linux/scatterlist.h>
22 #define MTK_RDR_PROC_THRESH BIT(0)
23 #define MTK_RDR_PROC_MODE BIT(23)
24 #define MTK_CNT_RST BIT(31)
25 #define MTK_IRQ_RDR0 BIT(1)
26 #define MTK_IRQ_RDR1 BIT(3)
27 #define MTK_IRQ_RDR2 BIT(5)
28 #define MTK_IRQ_RDR3 BIT(7)
30 #define SIZE_IN_WORDS(x) ((x) >> 2)
33 * Ring 0/1 are used by AES encrypt and decrypt.
34 * Ring 2/3 are used by SHA.
44 #define MTK_REC_NUM (MTK_RING_MAX / 2)
48 * struct mtk_desc - DMA descriptor
49 * @hdr: the descriptor control header
50 * @buf: DMA address of input buffer segment
51 * @ct: DMA address of command token that control operation flow
52 * @ct_hdr: the command token control header
53 * @tag: the user-defined field
54 * @tfm: DMA address of transform state
55 * @bound: align descriptors offset boundary
57 * Structure passed to the crypto engine to describe where source
58 * data needs to be fetched and how it needs to be processed.
70 #define MTK_DESC_NUM 512
71 #define MTK_DESC_OFF SIZE_IN_WORDS(sizeof(struct mtk_desc))
72 #define MTK_DESC_SZ (MTK_DESC_OFF - 2)
73 #define MTK_DESC_RING_SZ ((sizeof(struct mtk_desc) * MTK_DESC_NUM))
74 #define MTK_DESC_CNT(x) ((MTK_DESC_OFF * (x)) << 2)
75 #define MTK_DESC_LAST cpu_to_le32(BIT(22))
76 #define MTK_DESC_FIRST cpu_to_le32(BIT(23))
77 #define MTK_DESC_BUF_LEN(x) cpu_to_le32(x)
78 #define MTK_DESC_CT_LEN(x) cpu_to_le32((x) << 24)
81 * struct mtk_ring - Descriptor ring
82 * @cmd_base: pointer to command descriptor ring base
83 * @cmd_next: pointer to the next command descriptor
84 * @cmd_dma: DMA address of command descriptor ring
85 * @res_base: pointer to result descriptor ring base
86 * @res_next: pointer to the next result descriptor
87 * @res_prev: pointer to the previous result descriptor
88 * @res_dma: DMA address of result descriptor ring
90 * A descriptor ring is a circular buffer that is used to manage
91 * one or more descriptors. There are two type of descriptor rings;
92 * the command descriptor ring and result descriptor ring.
95 struct mtk_desc
*cmd_base
;
96 struct mtk_desc
*cmd_next
;
98 struct mtk_desc
*res_base
;
99 struct mtk_desc
*res_next
;
100 struct mtk_desc
*res_prev
;
105 * struct mtk_aes_dma - Structure that holds sg list info
106 * @sg: pointer to scatter-gather list
107 * @nents: number of entries in the sg list
108 * @remainder: remainder of sg list
109 * @sg_len: number of entries in the sg mapped list
112 struct scatterlist
*sg
;
118 struct mtk_aes_base_ctx
;
122 typedef int (*mtk_aes_fn
)(struct mtk_cryp
*cryp
, struct mtk_aes_rec
*aes
);
125 * struct mtk_aes_rec - AES operation record
126 * @cryp: pointer to Cryptographic device
127 * @queue: crypto request queue
128 * @areq: pointer to async request
129 * @done_task: the tasklet is use in AES interrupt
130 * @queue_task: the tasklet is used to dequeue request
131 * @ctx: pointer to current context
132 * @src: the structure that holds source sg list info
133 * @dst: the structure that holds destination sg list info
134 * @aligned_sg: the scatter list is use to alignment
135 * @real_dst: pointer to the destination sg list
136 * @resume: pointer to resume function
137 * @total: request buffer length
138 * @buf: pointer to page buffer
139 * @id: the current use of ring
140 * @flags: it's describing AES operation state
141 * @lock: the async queue lock
143 * Structure used to record AES execution state.
146 struct mtk_cryp
*cryp
;
147 struct crypto_queue queue
;
148 struct crypto_async_request
*areq
;
149 struct tasklet_struct done_task
;
150 struct tasklet_struct queue_task
;
151 struct mtk_aes_base_ctx
*ctx
;
152 struct mtk_aes_dma src
;
153 struct mtk_aes_dma dst
;
155 struct scatterlist aligned_sg
;
156 struct scatterlist
*real_dst
;
170 * struct mtk_sha_rec - SHA operation record
171 * @cryp: pointer to Cryptographic device
172 * @queue: crypto request queue
173 * @req: pointer to ahash request
174 * @done_task: the tasklet is use in SHA interrupt
175 * @queue_task: the tasklet is used to dequeue request
176 * @id: the current use of ring
177 * @flags: it's describing SHA operation state
178 * @lock: the async queue lock
180 * Structure used to record SHA execution state.
183 struct mtk_cryp
*cryp
;
184 struct crypto_queue queue
;
185 struct ahash_request
*req
;
186 struct tasklet_struct done_task
;
187 struct tasklet_struct queue_task
;
196 * struct mtk_cryp - Cryptographic device
197 * @base: pointer to mapped register I/O base
198 * @dev: pointer to device
199 * @clk_cryp: pointer to crypto clock
200 * @irq: global system and rings IRQ
201 * @ring: pointer to descriptor rings
202 * @aes: pointer to operation record of AES
203 * @sha: pointer to operation record of SHA
204 * @aes_list: device list of AES
205 * @sha_list: device list of SHA
206 * @rec: it's used to select SHA record for tfm
208 * Structure storing cryptographic device information.
213 struct clk
*clk_cryp
;
214 int irq
[MTK_IRQ_NUM
];
216 struct mtk_ring
*ring
[MTK_RING_MAX
];
217 struct mtk_aes_rec
*aes
[MTK_REC_NUM
];
218 struct mtk_sha_rec
*sha
[MTK_REC_NUM
];
220 struct list_head aes_list
;
221 struct list_head sha_list
;
226 int mtk_cipher_alg_register(struct mtk_cryp
*cryp
);
227 void mtk_cipher_alg_release(struct mtk_cryp
*cryp
);
228 int mtk_hash_alg_register(struct mtk_cryp
*cryp
);
229 void mtk_hash_alg_release(struct mtk_cryp
*cryp
);
231 #endif /* __MTK_PLATFORM_H_ */