aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ubicom32/files/arch/ubicom32/crypto/crypto_ubicom32.h
blob: e754c19b902bd5ae5f4fef7d5f76375579714833 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * arch/ubicom32/crypto/crypto_ubicom32.h
 *   Support for Ubicom32 cryptographic instructions.
 *
 * (C) Copyright 2009, Ubicom, Inc.
 *
 * This file is part of the Ubicom32 Linux Kernel Port.
 *
 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Ubicom32 Linux Kernel Port.  If not,
 * see <http://www.gnu.org/licenses/>.
 *
 * Ubicom32 implementation derived from (with many thanks):
 *   arch/m68knommu
 *   arch/blackfin
 *   arch/parisc
 */
#ifndef _CRYPTO_ARCH_UBICOM32_CRYPT_H
#define _CRYPTO_ARCH_UBICOM32_CRYPT_H

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/jiffies.h>
#include <linux/timer.h>
#include <linux/spinlock.h>
#include <asm/errno.h>
#include <asm/io.h>
#include <asm/ip5000.h>

#define CRYPTO_UBICOM32_LOOP_ASM 1
#define CRYPTO_UBICOM32_ALIGNMENT 4
#define SEC_ALIGNED(p) (((u32)p & 3) == 0)

#define SEC_BASE		SECURITY_BASE
#define SEC_KEY_OFFSET		SECURITY_KEY_VALUE(0)
#define SEC_INPUT_OFFSET	SECURITY_KEY_IN(0)
#define SEC_OUTPUT_OFFSET	SECURITY_KEY_OUT(0)
#define SEC_HASH_OFFSET		SECURITY_KEY_HASH(0)

#define SEC_KEY_128_BITS	SECURITY_CTRL_KEY_SIZE(0)
#define SEC_KEY_192_BITS	SECURITY_CTRL_KEY_SIZE(1)
#define SEC_KEY_256_BITS	SECURITY_CTRL_KEY_SIZE(2)

#define SEC_HASH_NONE		SECURITY_CTRL_HASH_ALG_NONE
#define SEC_HASH_MD5		SECURITY_CTRL_HASH_ALG_MD5
#define SEC_HASH_SHA1		SECURITY_CTRL_HASH_ALG_SHA1

#define SEC_CBC_SET		SECURITY_CTRL_CBC
#define SEC_CBC_NONE		0

#define SEC_ALG_AES		SECURITY_CTRL_CIPHER_ALG_AES
#define SEC_ALG_NONE		SECURITY_CTRL_CIPHER_ALG_NONE
#define SEC_ALG_DES		SECURITY_CTRL_CIPHER_ALG_DES
#define SEC_ALG_3DES		SECURITY_CTRL_CIPHER_ALG_3DES

#define SEC_DIR_ENCRYPT		SECURITY_CTRL_ENCIPHER
#define SEC_DIR_DECRYPT		0

#define CRYPTO_UBICOM32_PRIORITY 300
#define CRYPTO_UBICOM32_COMPOSITE_PRIORITY 400

#define HW_CRYPTO_PS_MAX_IDLE_MS 100    /* idle time (ms) before shuting down sm */

extern spinlock_t crypto_ubicom32_lock;
extern bool crypto_ubicom32_inited;
extern volatile bool crypto_ubicom32_on;
extern volatile unsigned long crypto_ubicom32_last_use;
extern struct timer_list crypto_ubicom32_ps_timer;
extern void crypto_ubicom32_ps_check(unsigned long data);

#define SEC_COPY_2W(t, s)				\
	asm volatile (					\
	"	move.4 0(%0), 0(%1)		\n\t"	\
	"	move.4 4(%0), 4(%1)		\n\t"	\
							\
		:					\
		: "a" (t), "a" (s)			\
	)

#define SEC_COPY_4W(t, s)				\
	asm volatile (					\
	"	move.4 0(%0), 0(%1)		\n\t"	\
	"	move.4 4(%0), 4(%1)		\n\t"	\
	"	move.4 8(%0), 8(%1)		\n\t"	\
	"	move.4 12(%0), 12(%1)		\n\t"	\
		:					\
		: "a" (t), "a" (s)			\
	)

#define SEC_COPY_5W(t, s)				\
	asm volatile (					\
	"	move.4 0(%0), 0(%1)		\n\t"	\
	"	move.4 4(%0), 4(%1)		\n\t"	\
	"	move.4 8(%0), 8(%1)		\n\t"	\
	"	move.4 12(%0), 12(%1)		\n\t"	\
	"	move.4 16(%0), 16(%1)		\n\t"	\
		:					\
		: "a" (t), "a" (s)			\
	)

#define SEC_SET_KEY_2W(x)				\
	asm volatile (					\
	"	; write key to Security Keyblock \n\t"	\
	"	move.4 0x10(%0), 0(%1)		\n\t"	\
	"	move.4 0x14(%0), 4(%1)		\n\t"	\
		:					\
		: "a" (SECURITY_BASE), "a" (x)		\
	)

#define SEC_SET_KEY_4W(x) \
	asm volatile ( \
	"	; write key to Security Keyblock \n\t"	\
	"	move.4 0x10(%0), 0(%1)		\n\t"	\
	"	move.4 0x14(%0), 4(%1)		\n\t"	\
	"	move.4 0x18(%0), 8(%1)		\n\t"	\
	"	move.4 0x1c(%0), 12(%1)		\n\t"	\
		:					\
		: "a"(SECURITY_BASE), "a"(x)		\
	)

#define SEC_SET_KEY_6W(x)				\
	asm volatile (					\
	"	; write key to Security Keyblock \n\t"	\
	"	move.4 0x10(%0), 0(%1)		\n\t"	\
	"	move.4 0x14(%0), 4(%1)		\n\t"	\
	"	move.4 0x18(%0), 8(%1)		\n\t"	\
	"	move.4 0x1c(%0), 12(%1)		\n\t"	\
	"	move.4 0x20(%0), 16(%1)		\n\t"	\
	"	move.4 0x24(%0), 20(%1)		\n\t"	\
		:					\
		: "a" (SECURITY_BASE), "a" (x)		\
	)

#define SEC_SET_KEY_8W(x)				\
	asm volatile (					\
	"	; write key to Security Keyblock \n\t"	\
	"	move.4 0x10(%0), 0(%1)		\n\t"	\
	"	move.4 0x14(%0), 4(%1)		\n\t"	\
	"	move.4 0x18(%0), 8(%1)		\n\t"	\
	"	move.4 0x1c(%0), 12(%1)		\n\t"	\
	"	move.4 0x20(%0), 16(%1)		\n\t"	\
	"	move.4 0x24(%0), 20(%1)		\n\t"	\
	"	move.4 0x28(%0), 24(%1)		\n\t"	\
	"	move.4 0x2c(%0), 28(%1)		\n\t"	\
		:					\
		: "a" (SECURITY_BASE), "a" (x)		\
	)

#define SEC_SET_KEY_64(k)	SEC_SET_KEY_2W(k)
#define SEC_SET_KEY_128(k)	SEC_SET_KEY_4W(k)
#define SEC_SET_KEY_192(k)	SEC_SET_KEY_6W(k)
#define SEC_SET_KEY_256(k)	SEC_SET_KEY_8W(k)

#define DES_SET_KEY(x) 		SEC_SET_KEY_64(x)
#define DES3_SET_KEY(x) 	SEC_SET_KEY_192(x)

#define SEC_SET_INPUT_2W(x)				\
	asm volatile (					\
	"	; write key to Security Keyblock \n\t"	\
	"	move.4 0x30(%0), 0(%1)		\n\t"	\
	"	move.4 0x34(%0), 4(%1)		\n\t"	\
		:					\
		: "a" (SECURITY_BASE), "a" (x)		\
	)

#define SEC_GET_OUTPUT_2W(x)				\
	asm volatile (					\
	"	; write key to Security Keyblock \n\t"	\
	"	move.4 0(%1), 0x50(%0)		\n\t"	\
	"	move.4 4(%1), 0x54(%0)		\n\t"	\
		:					\
		: "a" (SECURITY_BASE), "a" (x)		\
	)

#define SEC_SET_INPUT_4W(x) \
	asm volatile ( \
	"	; write key to Security Keyblock \n\t"	\
	"	move.4 0x30(%0), 0(%1)		\n\t"	\
	"	move.4 0x34(%0), 4(%1)		\n\t"	\
	"	move.4 0x38(%0), 8(%1)		\n\t"	\
	"	move.4 0x3c(%0), 12(%1)		\n\t"	\
		:					\
		: "a" (SECURITY_BASE), "a" (x)		\
	)

#define SEC_GET_OUTPUT_4W(x)				\
	asm volatile (					\
	"	; read output from Security Keyblock \n\t" \
	"	move.4 0(%1), 0x50(%0)		\n\t"	\
	"	move.4 4(%1), 0x54(%0)		\n\t"	\
	"	move.4 8(%1), 0x58(%0)		\n\t"	\
	"	move.4 12(%1), 0x5c(%0)		\n\t"	\
		:					\
		: "a" (SECURITY_BASE), "a" (x)		\
	)

#define SEC_SET_IV_4W(x)				\
	asm volatile (					\
	"	; write IV to Security Keyblock  \n\t"	\
	"	move.4 0x50(%0), 0(%1)		\n\t"	\
	"	move.4 0x54(%0), 4(%1)		\n\t"	\
	"	move.4 0x58(%0), 8(%1)		\n\t"	\
	"	move.4 0x5c(%0), 12(%1)		\n\t"	\
		:					\
		: "a" (SECURITY_BASE), "a" (x)		\
	)

#define SEC_PIPE_FLUSH() asm volatile ( " pipe_flush 0 \n\t" )

static inline void hw_crypto_set_ctrl(uint32_t c)
{
	asm volatile (
	"	move.4	0(%0), %1		\n\t"
		:
		: "a" (SECURITY_BASE + SECURITY_CTRL), "d" (c)
	);
}

static inline void hw_crypto_ps_start(void)
{
	crypto_ubicom32_ps_timer.expires = jiffies + msecs_to_jiffies(HW_CRYPTO_PS_MAX_IDLE_MS >> 1);
	add_timer(&crypto_ubicom32_ps_timer);
}

static inline void hw_crypto_turn_on(void)
{
	asm volatile (
	"	moveai  A4, %0			\n\t"
	"	bset    0x0(A4), 0x0(A4), %1	\n\t"
	"	cycles 11			\n\t"
		:
		: "i" (OCP_BASE >> 7), "i" (GEN_CLK_PLL_SECURITY_BIT_NO)
		: "a4", "cc"
	);
	crypto_ubicom32_on = true;
}

static inline void hw_crypto_turn_off(void)
{
	asm volatile (
	"	moveai  A4, %0			\n\t"
	"	bclr    0x0(A4), 0x0(A4), %1	\n\t"
		:
		: "i" (OCP_BASE >> 7), "i" (GEN_CLK_PLL_SECURITY_BIT_NO)
		: "a4", "cc"
	);
	crypto_ubicom32_on = false;
}

/*
 * hw_crypto_check
 * 	Most probably hw crypto is called in clusters and it makes no sense to turn it off
 * 	and on and waster 13 cycles every time.
 */
static inline void hw_crypto_check(void)
{
	if (likely(crypto_ubicom32_on)) {
		return;
	}
	crypto_ubicom32_last_use = jiffies;
	hw_crypto_turn_on();
	hw_crypto_ps_start();
}

/*
 * hw_crypto_ps_init
 * 	Init power save timer
 */
static inline void hw_crypto_ps_init(void)
{
	init_timer_deferrable(&crypto_ubicom32_ps_timer);
	crypto_ubicom32_ps_timer.function = crypto_ubicom32_ps_check;
	crypto_ubicom32_ps_timer.data = 0;
}

/*
 * hw_crypto_init()
 *      Initialize OCP security module lock and disables its clock.
 */
static inline void hw_crypto_init(void)
{
	if (!crypto_ubicom32_inited) {
		crypto_ubicom32_inited = true;
		spin_lock_init(&crypto_ubicom32_lock);
		hw_crypto_ps_init();
		hw_crypto_turn_off();
	}
}

/*
 * hw_crypto_lock()
 *      Locks the OCP security module and enables its clock.
 */
static inline void hw_crypto_lock(void)
{
	spin_lock_bh(&crypto_ubicom32_lock);
}

/*
 * hw_crypto_unlock()
 *      Unlocks the OCP security module and disables its clock.
 */
static inline void hw_crypto_unlock(void)
{
	crypto_ubicom32_last_use = jiffies;
	spin_unlock_bh(&crypto_ubicom32_lock);
}

#define CONFIG_CRYPTO_UBICOM32_DEBUG 1

#ifdef CONFIG_CRYPTO_UBICOM32_DEBUG
static inline void hex_dump(void *buf, int b_size, const char *msg)
{
	u8 *b = (u8 *)buf;
	int i;
	if (msg) {
		printk("%s:\t", msg);
	}

	for (i=0; i < b_size; i++) {
		printk("%02x ", b[i]);
		if ((i & 3) == 3) {
			printk(" ");
		}
		if ((i & 31) == 31) {
			printk("\n");
		}
	}
	printk("\n");
}
#define UBICOM32_SEC_DUMP(a, b, c) hex_dump(a, b, c)
#else
#define UBICOM32_SEC_DUMP(a, b, c)
#endif

#endif	/* _CRYPTO_ARCH_UBICOM32_CRYPT_H */