blob: 7cd5f98d25a52fb4caee77424bd7797016fd40b8 [file] [log] [blame]
Ryder Lee785e5c62016-12-19 10:20:44 +08001/*
2 * Driver for EIP97 cryptographic accelerator.
3 *
4 * Copyright (c) 2016 Ryder Lee <ryder.lee@mediatek.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 */
11
12#ifndef __MTK_PLATFORM_H_
13#define __MTK_PLATFORM_H_
14
15#include <crypto/algapi.h>
16#include <crypto/internal/hash.h>
17#include <crypto/scatterwalk.h>
18#include <linux/crypto.h>
19#include <linux/dma-mapping.h>
20#include <linux/interrupt.h>
21#include <linux/scatterlist.h>
22#include "mtk-regs.h"
23
24#define MTK_RDR_PROC_THRESH BIT(0)
25#define MTK_RDR_PROC_MODE BIT(23)
26#define MTK_CNT_RST BIT(31)
27#define MTK_IRQ_RDR0 BIT(1)
28#define MTK_IRQ_RDR1 BIT(3)
29#define MTK_IRQ_RDR2 BIT(5)
30#define MTK_IRQ_RDR3 BIT(7)
31
32#define SIZE_IN_WORDS(x) ((x) >> 2)
33
34/**
35 * Ring 0/1 are used by AES encrypt and decrypt.
36 * Ring 2/3 are used by SHA.
37 */
38enum {
39 RING0 = 0,
40 RING1,
41 RING2,
42 RING3,
43 RING_MAX,
44};
45
46#define MTK_REC_NUM (RING_MAX / 2)
47#define MTK_IRQ_NUM 5
48
49/**
50 * struct mtk_desc - DMA descriptor
51 * @hdr: the descriptor control header
52 * @buf: DMA address of input buffer segment
53 * @ct: DMA address of command token that control operation flow
54 * @ct_hdr: the command token control header
55 * @tag: the user-defined field
56 * @tfm: DMA address of transform state
57 * @bound: align descriptors offset boundary
58 *
59 * Structure passed to the crypto engine to describe where source
60 * data needs to be fetched and how it needs to be processed.
61 */
62struct mtk_desc {
63 __le32 hdr;
64 __le32 buf;
65 __le32 ct;
66 __le32 ct_hdr;
67 __le32 tag;
68 __le32 tfm;
69 __le32 bound[2];
70};
71
72#define MTK_DESC_NUM 512
73#define MTK_DESC_OFF SIZE_IN_WORDS(sizeof(struct mtk_desc))
74#define MTK_DESC_SZ (MTK_DESC_OFF - 2)
75#define MTK_DESC_RING_SZ ((sizeof(struct mtk_desc) * MTK_DESC_NUM))
76#define MTK_DESC_CNT(x) ((MTK_DESC_OFF * (x)) << 2)
77#define MTK_DESC_LAST cpu_to_le32(BIT(22))
78#define MTK_DESC_FIRST cpu_to_le32(BIT(23))
79#define MTK_DESC_BUF_LEN(x) cpu_to_le32(x)
80#define MTK_DESC_CT_LEN(x) cpu_to_le32((x) << 24)
81
82/**
83 * struct mtk_ring - Descriptor ring
84 * @cmd_base: pointer to command descriptor ring base
85 * @cmd_dma: DMA address of command descriptor ring
Ryder Lee44328612017-01-20 13:41:09 +080086 * @cmd_pos: current position in the command descriptor ring
Ryder Lee785e5c62016-12-19 10:20:44 +080087 * @res_base: pointer to result descriptor ring base
88 * @res_dma: DMA address of result descriptor ring
Ryder Lee44328612017-01-20 13:41:09 +080089 * @res_pos: current position in the result descriptor ring
Ryder Lee785e5c62016-12-19 10:20:44 +080090 *
91 * A descriptor ring is a circular buffer that is used to manage
92 * one or more descriptors. There are two type of descriptor rings;
93 * the command descriptor ring and result descriptor ring.
94 */
95struct mtk_ring {
96 struct mtk_desc *cmd_base;
97 dma_addr_t cmd_dma;
Ryder Lee44328612017-01-20 13:41:09 +080098 u32 cmd_pos;
Ryder Lee785e5c62016-12-19 10:20:44 +080099 struct mtk_desc *res_base;
100 dma_addr_t res_dma;
Ryder Lee44328612017-01-20 13:41:09 +0800101 u32 res_pos;
Ryder Lee785e5c62016-12-19 10:20:44 +0800102};
103
104/**
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
110 */
111struct mtk_aes_dma {
112 struct scatterlist *sg;
113 int nents;
114 u32 remainder;
115 u32 sg_len;
116};
117
Ryder Lee382ae572017-01-20 13:41:10 +0800118struct mtk_aes_base_ctx;
119struct mtk_aes_rec;
120struct mtk_cryp;
121
122typedef int (*mtk_aes_fn)(struct mtk_cryp *cryp, struct mtk_aes_rec *aes);
Ryder Leea87399622017-01-20 13:41:08 +0800123
Ryder Lee785e5c62016-12-19 10:20:44 +0800124/**
125 * struct mtk_aes_rec - AES operation record
126 * @queue: crypto request queue
Ryder Lee059b1492017-01-20 13:41:13 +0800127 * @areq: pointer to async request
Ryder Lee785e5c62016-12-19 10:20:44 +0800128 * @task: the tasklet is use in AES interrupt
Ryder Leea87399622017-01-20 13:41:08 +0800129 * @ctx: pointer to current context
Ryder Lee785e5c62016-12-19 10:20:44 +0800130 * @src: the structure that holds source sg list info
131 * @dst: the structure that holds destination sg list info
132 * @aligned_sg: the scatter list is use to alignment
133 * @real_dst: pointer to the destination sg list
Ryder Lee87421982017-01-20 13:41:11 +0800134 * @resume: pointer to resume function
Ryder Lee785e5c62016-12-19 10:20:44 +0800135 * @total: request buffer length
136 * @buf: pointer to page buffer
Ryder Lee785e5c62016-12-19 10:20:44 +0800137 * @id: record identification
138 * @flags: it's describing AES operation state
Ryder Lee382ae572017-01-20 13:41:10 +0800139 * @lock: the async queue lock
Ryder Lee785e5c62016-12-19 10:20:44 +0800140 *
141 * Structure used to record AES execution state.
142 */
143struct mtk_aes_rec {
144 struct crypto_queue queue;
Ryder Lee382ae572017-01-20 13:41:10 +0800145 struct crypto_async_request *areq;
Ryder Lee785e5c62016-12-19 10:20:44 +0800146 struct tasklet_struct task;
Ryder Lee382ae572017-01-20 13:41:10 +0800147 struct mtk_aes_base_ctx *ctx;
Ryder Lee785e5c62016-12-19 10:20:44 +0800148 struct mtk_aes_dma src;
149 struct mtk_aes_dma dst;
150
151 struct scatterlist aligned_sg;
152 struct scatterlist *real_dst;
153
Ryder Lee87421982017-01-20 13:41:11 +0800154 mtk_aes_fn resume;
155
Ryder Lee785e5c62016-12-19 10:20:44 +0800156 size_t total;
157 void *buf;
158
Ryder Lee785e5c62016-12-19 10:20:44 +0800159 u8 id;
160 unsigned long flags;
161 /* queue lock */
162 spinlock_t lock;
163};
164
165/**
166 * struct mtk_sha_rec - SHA operation record
167 * @queue: crypto request queue
168 * @req: pointer to ahash request
169 * @task: the tasklet is use in SHA interrupt
Ryder Lee785e5c62016-12-19 10:20:44 +0800170 * @id: record identification
171 * @flags: it's describing SHA operation state
172 * @lock: the ablkcipher queue lock
173 *
174 * Structure used to record SHA execution state.
175 */
176struct mtk_sha_rec {
177 struct crypto_queue queue;
178 struct ahash_request *req;
179 struct tasklet_struct task;
180
Ryder Lee785e5c62016-12-19 10:20:44 +0800181 u8 id;
182 unsigned long flags;
183 /* queue lock */
184 spinlock_t lock;
185};
186
187/**
188 * struct mtk_cryp - Cryptographic device
189 * @base: pointer to mapped register I/O base
190 * @dev: pointer to device
191 * @clk_ethif: pointer to ethif clock
192 * @clk_cryp: pointer to crypto clock
193 * @irq: global system and rings IRQ
194 * @ring: pointer to execution state of AES
195 * @aes: pointer to execution state of SHA
196 * @sha: each execution record map to a ring
197 * @aes_list: device list of AES
198 * @sha_list: device list of SHA
199 * @tmp: pointer to temporary buffer for internal use
200 * @tmp_dma: DMA address of temporary buffer
201 * @rec: it's used to select SHA record for tfm
202 *
203 * Structure storing cryptographic device information.
204 */
205struct mtk_cryp {
206 void __iomem *base;
207 struct device *dev;
208 struct clk *clk_ethif;
209 struct clk *clk_cryp;
210 int irq[MTK_IRQ_NUM];
211
212 struct mtk_ring *ring[RING_MAX];
213 struct mtk_aes_rec *aes[MTK_REC_NUM];
214 struct mtk_sha_rec *sha[MTK_REC_NUM];
215
216 struct list_head aes_list;
217 struct list_head sha_list;
218
219 void *tmp;
220 dma_addr_t tmp_dma;
221 bool rec;
222};
223
224int mtk_cipher_alg_register(struct mtk_cryp *cryp);
225void mtk_cipher_alg_release(struct mtk_cryp *cryp);
226int mtk_hash_alg_register(struct mtk_cryp *cryp);
227void mtk_hash_alg_release(struct mtk_cryp *cryp);
228
229#endif /* __MTK_PLATFORM_H_ */