1/*	$NetBSD: sun8i_crypto.h,v 1.2 2020/06/13 18:57:54 riastradh Exp $	*/
2
3/*-
4 * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef	_ARM_SUN8I_CRYPTO_H
33#define	_ARM_SUN8I_CRYPTO_H
34
35#include <sys/cdefs.h>
36
37#define	SUN8I_CRYPTO_NCHAN	4
38
39/* task_descriptor_queue common control bitmap (32bit), p. 234 */
40#define	SUN8I_CRYPTO_TDQC_INTR_EN	__BIT(31)
41#define	SUN8I_CRYPTO_TDQC_IV_MODE	__BIT(16) /* MD5/SHA-1/SHA-2 IV */
42#define	SUN8I_CRYPTO_TDQC_IV_MODE_CONST		0 /* standard constants */
43#define	SUN8I_CRYPTO_TDQC_IV_MODE_ARBITRARY	1 /* arbitrary */
44#define	SUN8I_CRYPTO_TDQC_HMAC_PT_LAST	__BIT(15)
45#define	SUN8I_CRYPTO_TDQC_OP_DIR	__BIT(8)
46#define	SUN8I_CRYPTO_TDQC_OP_DIR_ENC		0
47#define	SUN8I_CRYPTO_TDQC_OP_DIR_DEC		1
48#define	SUN8I_CRYPTO_TDQC_METHOD	__BITS(6,0)
49#define	SUN8I_CRYPTO_TDQC_METHOD_AES		0
50#define	SUN8I_CRYPTO_TDQC_METHOD_DES		1
51#define	SUN8I_CRYPTO_TDQC_METHOD_3DES		2
52#define	SUN8I_CRYPTO_TDQC_METHOD_MD5		16
53#define	SUN8I_CRYPTO_TDQC_METHOD_SHA1		17
54#define	SUN8I_CRYPTO_TDQC_METHOD_SHA224		18
55#define	SUN8I_CRYPTO_TDQC_METHOD_SHA256		19
56#define	SUN8I_CRYPTO_TDQC_METHOD_HMAC_SHA1	22
57#define	SUN8I_CRYPTO_TDQC_METHOD_HMAC_SHA256	23
58#define	SUN8I_CRYPTO_TDQC_METHOD_RSA		32
59#define	SUN8I_CRYPTO_TDQC_METHOD_TRNG		48
60#define	SUN8I_CRYPTO_TDQC_METHOD_PRNG		49
61
62/* task_descriptor_queue symmetric control (32bit), p. 235 */
63#define	SUN8I_CRYPTO_TDQS_SKEY_SELECT	__BITS(23,20)
64#define	SUN8I_CRYPTO_TDQS_SKEY_SELECT_SS_KEYx	0
65#define	SUN8I_CRYPTO_TDQS_SKEY_SELECT_SSK	1
66#define	SUN8I_CRYPTO_TDQS_SKEY_SELECT_HUK	2
67#define	SUN8I_CRYPTO_TDQS_SKEY_SELECT_RSSK	3
68#define	SUN8I_CRYPTO_TDQS_SKEY_SELECT_INTERNAL(n)	(8 + (n))
69#define	SUN8I_CRYPTO_TDQS_AES_CTS_LAST	__BIT(16)
70#define	SUN8I_CRYPTO_TDQS_OP_MODE	__BITS(11,8)
71#define	SUN8I_CRYPTO_TDQS_OP_MODE_ECB		0
72#define	SUN8I_CRYPTO_TDQS_OP_MODE_CBC		1
73#define	SUN8I_CRYPTO_TDQS_OP_MODE_CTR		2
74#define	SUN8I_CRYPTO_TDQS_OP_MODE_CTS		3
75#define	SUN8I_CRYPTO_TDQS_CTR_WIDTH	__BITS(3,2)
76#define	SUN8I_CRYPTO_TDQS_CTR_WIDTH_16		0
77#define	SUN8I_CRYPTO_TDQS_CTR_WIDTH_32		1
78#define	SUN8I_CRYPTO_TDQS_CTR_WIDTH_64		2
79#define	SUN8I_CRYPTO_TDQS_CTR_WIDTH_128		3
80#define	SUN8I_CRYPTO_TDQS_AES_KEYSIZE	__BITS(1,0)
81#define	SUN8I_CRYPTO_TDQS_AES_KEYSIZE_128	0
82#define	SUN8I_CRYPTO_TDQS_AES_KEYSIZE_192	1
83#define	SUN8I_CRYPTO_TDQS_AES_KEYSIZE_256	2
84
85/* task_descriptor_queue asymmetric control (32bit), p. 236 */
86#define	SUN8I_CRYPTO_TDQA_RSA_MODSIZE	__BITS(30,28)
87#define	SUN8I_CRYPTO_TDQA_RSA_MODSIZE_512	0
88#define	SUN8I_CRYPTO_TDQA_RSA_MODSIZE_1024	1
89#define	SUN8I_CRYPTO_TDQA_RSA_MODSIZE_2048	2
90
91/* Crypto Engine Register List */
92
93/* Module */
94#define	SUN8I_CRYPTO_NS		0x01c15000
95#define	SUN8I_CRYPTO_S		0x01c15800
96
97/* Register */
98#define	SUN8I_CRYPTO_TDQ	0x00 /* Task Descriptor Queue Address */
99#define	SUN8I_CRYPTO_CTR	0x04 /* Gating Control Register */
100#define	SUN8I_CRYPTO_ICR	0x08 /* Interrupt Control Register */
101#define	SUN8I_CRYPTO_ISR	0x0c /* Interrupt Status Register */
102#define	SUN8I_CRYPTO_TLR	0x10 /* Task Load Register */
103#define	SUN8I_CRYPTO_TSR	0x14 /* Task Status Register */
104#define	SUN8I_CRYPTO_ESR	0x18 /* Task Error type Register */
105#define	SUN8I_CRYPTO_CSAR	0x24 /* Current Source Address Register */
106#define	SUN8I_CRYPTO_CDAR	0x28 /* Current Destination Address Register */
107#define	SUN8I_CRYPTO_TPR	0x2c /* Throughput Register */
108
109#define	SUN8I_CRYPTO_CTR_RSA_CLK_EN	__BIT(3)
110#define	SUN8I_CRYPTO_CTR_DIE_ID		__BITS(2,0)
111
112#define	SUN8I_CRYPTO_ICR_INTR_EN	__BITS(3,0)
113#define	SUN8I_CRYPTO_ICR_INTR_EN_CHAN(n)	(__BIT(0) << (n))
114
115#define	SUN8I_CRYPTO_ISR_DONE		__BITS(3,0)
116#define	SUN8I_CRYPTO_ISR_DONE_CHAN(n)		(__BIT(0) << (n))
117
118#define	SUN8I_CRYPTO_TLR_LOAD		__BIT(0)
119
120#define	SUN8I_CRYPTO_TSR_TASK		__BITS(1,0)
121/* task number, 0-3 */
122
123#define	SUN8I_CRYPTO_ESR_CHAN(n)	(__BITS(3,0) << (n))
124#define	SUN8I_CRYPTO_ESR_CHAN_ALGNOTSUP		__BIT(0)
125#define	SUN8I_CRYPTO_ESR_CHAN_DATALENERR	__BIT(1)
126#define	SUN8I_CRYPTO_ESR_CHAN_KEYSRAMERR	__BIT(2)
127
128#define	SUN8I_CRYPTO_MAXKEYBYTES	256	/* e.g. 2048-bit RSA */
129#define	SUN8I_CRYPTO_MAXIVBYTES		64	/* e.g. SHA-512 */
130#define	SUN8I_CRYPTO_MAXCTRBYTES	16	/* e.g. AES */
131
132/*
133 * Eight src segments and eight dst segments per task.  The datalen
134 * field, measured either in bytes or words, is 32 bits.  Each segment
135 * length is measured in words and is also 32 bits.  We never do very
136 * long transfers anyway so rounding the maximum size down to 2^32 - 1
137 * is not a real limitation.
138 */
139#define	SUN8I_CRYPTO_MAXSEGS		8
140#define	SUN8I_CRYPTO_MAXSEGLEN		UINT32_MAX
141#define	SUN8I_CRYPTO_MAXDATALEN		UINT32_MAX
142
143struct sun8i_crypto_taskdesc {
144	uint32_t	td_cid;		/* task channel id */
145	uint32_t	td_tdqc;	/* task descriptor queue common */
146	uint32_t	td_tdqs;	/* task descriptor queue symmetric */
147	uint32_t	td_tdqa;	/* task descriptor queue asymmetric */
148	uint32_t	td_keydesc;	/* key descriptor */
149	uint32_t	td_ivdesc;	/* IV descriptor */
150	uint32_t	td_ctrdesc;	/* CTR descriptor */
151	uint32_t	td_datalen;	/* data length */
152	struct sun8i_crypto_adrlen {
153		uint32_t	adr;
154		uint32_t	len;
155	}		td_src[SUN8I_CRYPTO_MAXSEGS],
156			td_dst[SUN8I_CRYPTO_MAXSEGS];
157	uint32_t	td_nextdesc;	/* next descriptor */
158	uint32_t	td_reserved[3];
159} __packed;
160
161CTASSERT(sizeof(struct sun8i_crypto_taskdesc) == 44*4);
162
163#endif	/* _ARM_SUN8I_CRYPTO_H */
164