blob: bd206ffc25dfbf1f4c7876869ea714d02c60d4c9 [file] [log] [blame]
Andreas Westin2789c082012-04-30 10:11:17 +02001/**
2 * Copyright (C) ST-Ericsson SA 2010
3 * Author: Shujuan Chen <shujuan.chen@stericsson.com> for ST-Ericsson.
4 * Author: Jonas Linde <jonas.linde@stericsson.com> for ST-Ericsson.
5 * Author: Niklas Hernaeus <niklas.hernaeus@stericsson.com> for ST-Ericsson.
6 * Author: Joakim Bech <joakim.xx.bech@stericsson.com> for ST-Ericsson.
7 * Author: Berne Hebark <berne.herbark@stericsson.com> for ST-Ericsson.
8 * License terms: GNU General Public License (GPL) version 2
9 */
10
11#include <linux/errno.h>
12#include <linux/kernel.h>
13#include <linux/types.h>
14
15#include <mach/hardware.h>
16
17#include "cryp_p.h"
18#include "cryp.h"
19
20/**
21 * cryp_wait_until_done - wait until the device logic is not busy
22 */
23void cryp_wait_until_done(struct cryp_device_data *device_data)
24{
25 while (cryp_is_logic_busy(device_data))
26 cpu_relax();
27}
28
29/**
30 * cryp_check - This routine checks Peripheral and PCell Id
31 * @device_data: Pointer to the device data struct for base address.
32 */
33int cryp_check(struct cryp_device_data *device_data)
34{
35 int peripheralid2 = 0;
36
37 if (NULL == device_data)
38 return -EINVAL;
39
40 if (cpu_is_u8500())
41 peripheralid2 = CRYP_PERIPHERAL_ID2_DB8500;
42 else if (cpu_is_u5500())
43 peripheralid2 = CRYP_PERIPHERAL_ID2_DB5500;
44
45 /* Check Peripheral and Pcell Id Register for CRYP */
46 if ((CRYP_PERIPHERAL_ID0 ==
47 readl_relaxed(&device_data->base->periphId0))
48 && (CRYP_PERIPHERAL_ID1 ==
49 readl_relaxed(&device_data->base->periphId1))
50 && (peripheralid2 ==
51 readl_relaxed(&device_data->base->periphId2))
52 && (CRYP_PERIPHERAL_ID3 ==
53 readl_relaxed(&device_data->base->periphId3))
54 && (CRYP_PCELL_ID0 ==
55 readl_relaxed(&device_data->base->pcellId0))
56 && (CRYP_PCELL_ID1 ==
57 readl_relaxed(&device_data->base->pcellId1))
58 && (CRYP_PCELL_ID2 ==
59 readl_relaxed(&device_data->base->pcellId2))
60 && (CRYP_PCELL_ID3 ==
61 readl_relaxed(&device_data->base->pcellId3))) {
62 return 0;
63 }
64
65 return -EPERM;
66}
67
68/**
69 * cryp_activity - This routine enables/disable the cryptography function.
70 * @device_data: Pointer to the device data struct for base address.
71 * @cryp_crypen: Enable/Disable functionality
72 */
73void cryp_activity(struct cryp_device_data *device_data,
74 enum cryp_crypen cryp_crypen)
75{
76 CRYP_PUT_BITS(&device_data->base->cr,
77 cryp_crypen,
78 CRYP_CR_CRYPEN_POS,
79 CRYP_CR_CRYPEN_MASK);
80}
81
82/**
83 * cryp_flush_inoutfifo - Resets both the input and the output FIFOs
84 * @device_data: Pointer to the device data struct for base address.
85 */
86void cryp_flush_inoutfifo(struct cryp_device_data *device_data)
87{
88 /*
89 * We always need to disble the hardware before trying to flush the
90 * FIFO. This is something that isn't written in the design
91 * specification, but we have been informed by the hardware designers
92 * that this must be done.
93 */
94 cryp_activity(device_data, CRYP_CRYPEN_DISABLE);
95 cryp_wait_until_done(device_data);
96
97 CRYP_SET_BITS(&device_data->base->cr, CRYP_CR_FFLUSH_MASK);
98 /*
99 * CRYP_SR_INFIFO_READY_MASK is the expected value on the status
100 * register when starting a new calculation, which means Input FIFO is
101 * not full and input FIFO is empty.
102 */
103 while (readl_relaxed(&device_data->base->sr) !=
104 CRYP_SR_INFIFO_READY_MASK)
105 cpu_relax();
106}
107
108/**
109 * cryp_set_configuration - This routine set the cr CRYP IP
110 * @device_data: Pointer to the device data struct for base address.
111 * @cryp_config: Pointer to the configuration parameter
112 * @control_register: The control register to be written later on.
113 */
114int cryp_set_configuration(struct cryp_device_data *device_data,
115 struct cryp_config *cryp_config,
116 u32 *control_register)
117{
118 u32 cr_for_kse;
119
120 if (NULL == device_data || NULL == cryp_config)
121 return -EINVAL;
122
123 *control_register |= (cryp_config->keysize << CRYP_CR_KEYSIZE_POS);
124
125 /* Prepare key for decryption in AES_ECB and AES_CBC mode. */
126 if ((CRYP_ALGORITHM_DECRYPT == cryp_config->algodir) &&
127 ((CRYP_ALGO_AES_ECB == cryp_config->algomode) ||
128 (CRYP_ALGO_AES_CBC == cryp_config->algomode))) {
129 cr_for_kse = *control_register;
130 /*
131 * This seems a bit odd, but it is indeed needed to set this to
132 * encrypt even though it is a decryption that we are doing. It
133 * also mentioned in the design spec that you need to do this.
134 * After the keyprepartion for decrypting is done you should set
135 * algodir back to decryption, which is done outside this if
136 * statement.
137 *
138 * According to design specification we should set mode ECB
139 * during key preparation even though we might be running CBC
140 * when enter this function.
141 *
142 * Writing to KSE_ENABLED will drop CRYPEN when key preparation
143 * is done. Therefore we need to set CRYPEN again outside this
144 * if statement when running decryption.
145 */
146 cr_for_kse |= ((CRYP_ALGORITHM_ENCRYPT << CRYP_CR_ALGODIR_POS) |
147 (CRYP_ALGO_AES_ECB << CRYP_CR_ALGOMODE_POS) |
148 (CRYP_CRYPEN_ENABLE << CRYP_CR_CRYPEN_POS) |
149 (KSE_ENABLED << CRYP_CR_KSE_POS));
150
151 writel_relaxed(cr_for_kse, &device_data->base->cr);
152 cryp_wait_until_done(device_data);
153 }
154
155 *control_register |=
156 ((cryp_config->algomode << CRYP_CR_ALGOMODE_POS) |
157 (cryp_config->algodir << CRYP_CR_ALGODIR_POS));
158
159 return 0;
160}
161
162/**
163 * cryp_configure_protection - set the protection bits in the CRYP logic.
164 * @device_data: Pointer to the device data struct for base address.
165 * @p_protect_config: Pointer to the protection mode and
166 * secure mode configuration
167 */
168int cryp_configure_protection(struct cryp_device_data *device_data,
169 struct cryp_protection_config *p_protect_config)
170{
171 if (NULL == p_protect_config)
172 return -EINVAL;
173
174 CRYP_WRITE_BIT(&device_data->base->cr,
175 (u32) p_protect_config->secure_access,
176 CRYP_CR_SECURE_MASK);
177 CRYP_PUT_BITS(&device_data->base->cr,
178 p_protect_config->privilege_access,
179 CRYP_CR_PRLG_POS,
180 CRYP_CR_PRLG_MASK);
181
182 return 0;
183}
184
185/**
186 * cryp_is_logic_busy - returns the busy status of the CRYP logic
187 * @device_data: Pointer to the device data struct for base address.
188 */
189int cryp_is_logic_busy(struct cryp_device_data *device_data)
190{
191 return CRYP_TEST_BITS(&device_data->base->sr,
192 CRYP_SR_BUSY_MASK);
193}
194
195/**
196 * cryp_configure_for_dma - configures the CRYP IP for DMA operation
197 * @device_data: Pointer to the device data struct for base address.
198 * @dma_req: Specifies the DMA request type value.
199 */
200void cryp_configure_for_dma(struct cryp_device_data *device_data,
201 enum cryp_dma_req_type dma_req)
202{
203 CRYP_SET_BITS(&device_data->base->dmacr,
204 (u32) dma_req);
205}
206
207/**
208 * cryp_configure_key_values - configures the key values for CRYP operations
209 * @device_data: Pointer to the device data struct for base address.
210 * @key_reg_index: Key value index register
211 * @key_value: The key value struct
212 */
213int cryp_configure_key_values(struct cryp_device_data *device_data,
214 enum cryp_key_reg_index key_reg_index,
215 struct cryp_key_value key_value)
216{
217 while (cryp_is_logic_busy(device_data))
218 cpu_relax();
219
220 switch (key_reg_index) {
221 case CRYP_KEY_REG_1:
222 writel_relaxed(key_value.key_value_left,
223 &device_data->base->key_1_l);
224 writel_relaxed(key_value.key_value_right,
225 &device_data->base->key_1_r);
226 break;
227 case CRYP_KEY_REG_2:
228 writel_relaxed(key_value.key_value_left,
229 &device_data->base->key_2_l);
230 writel_relaxed(key_value.key_value_right,
231 &device_data->base->key_2_r);
232 break;
233 case CRYP_KEY_REG_3:
234 writel_relaxed(key_value.key_value_left,
235 &device_data->base->key_3_l);
236 writel_relaxed(key_value.key_value_right,
237 &device_data->base->key_3_r);
238 break;
239 case CRYP_KEY_REG_4:
240 writel_relaxed(key_value.key_value_left,
241 &device_data->base->key_4_l);
242 writel_relaxed(key_value.key_value_right,
243 &device_data->base->key_4_r);
244 break;
245 default:
246 return -EINVAL;
247 }
248
249 return 0;
250}
251
252/**
253 * cryp_configure_init_vector - configures the initialization vector register
254 * @device_data: Pointer to the device data struct for base address.
255 * @init_vector_index: Specifies the index of the init vector.
256 * @init_vector_value: Specifies the value for the init vector.
257 */
258int cryp_configure_init_vector(struct cryp_device_data *device_data,
259 enum cryp_init_vector_index
260 init_vector_index,
261 struct cryp_init_vector_value
262 init_vector_value)
263{
264 while (cryp_is_logic_busy(device_data))
265 cpu_relax();
266
267 switch (init_vector_index) {
268 case CRYP_INIT_VECTOR_INDEX_0:
269 writel_relaxed(init_vector_value.init_value_left,
270 &device_data->base->init_vect_0_l);
271 writel_relaxed(init_vector_value.init_value_right,
272 &device_data->base->init_vect_0_r);
273 break;
274 case CRYP_INIT_VECTOR_INDEX_1:
275 writel_relaxed(init_vector_value.init_value_left,
276 &device_data->base->init_vect_1_l);
277 writel_relaxed(init_vector_value.init_value_right,
278 &device_data->base->init_vect_1_r);
279 break;
280 default:
281 return -EINVAL;
282 }
283
284 return 0;
285}
286
287/**
288 * cryp_save_device_context - Store hardware registers and
289 * other device context parameter
290 * @device_data: Pointer to the device data struct for base address.
291 * @ctx: Crypto device context
292 */
293void cryp_save_device_context(struct cryp_device_data *device_data,
294 struct cryp_device_context *ctx,
295 int cryp_mode)
296{
297 enum cryp_algo_mode algomode;
298 struct cryp_register *src_reg = device_data->base;
299 struct cryp_config *config =
300 (struct cryp_config *)device_data->current_ctx;
301
302 /*
303 * Always start by disable the hardware and wait for it to finish the
304 * ongoing calculations before trying to reprogram it.
305 */
306 cryp_activity(device_data, CRYP_CRYPEN_DISABLE);
307 cryp_wait_until_done(device_data);
308
309 if (cryp_mode == CRYP_MODE_DMA)
310 cryp_configure_for_dma(device_data, CRYP_DMA_DISABLE_BOTH);
311
312 if (CRYP_TEST_BITS(&src_reg->sr, CRYP_SR_IFEM_MASK) == 0)
313 ctx->din = readl_relaxed(&src_reg->din);
314
315 ctx->cr = readl_relaxed(&src_reg->cr) & CRYP_CR_CONTEXT_SAVE_MASK;
316
317 switch (config->keysize) {
318 case CRYP_KEY_SIZE_256:
319 ctx->key_4_l = readl_relaxed(&src_reg->key_4_l);
320 ctx->key_4_r = readl_relaxed(&src_reg->key_4_r);
321
322 case CRYP_KEY_SIZE_192:
323 ctx->key_3_l = readl_relaxed(&src_reg->key_3_l);
324 ctx->key_3_r = readl_relaxed(&src_reg->key_3_r);
325
326 case CRYP_KEY_SIZE_128:
327 ctx->key_2_l = readl_relaxed(&src_reg->key_2_l);
328 ctx->key_2_r = readl_relaxed(&src_reg->key_2_r);
329
330 default:
331 ctx->key_1_l = readl_relaxed(&src_reg->key_1_l);
332 ctx->key_1_r = readl_relaxed(&src_reg->key_1_r);
333 }
334
335 /* Save IV for CBC mode for both AES and DES. */
336 algomode = ((ctx->cr & CRYP_CR_ALGOMODE_MASK) >> CRYP_CR_ALGOMODE_POS);
337 if (algomode == CRYP_ALGO_TDES_CBC ||
338 algomode == CRYP_ALGO_DES_CBC ||
339 algomode == CRYP_ALGO_AES_CBC) {
340 ctx->init_vect_0_l = readl_relaxed(&src_reg->init_vect_0_l);
341 ctx->init_vect_0_r = readl_relaxed(&src_reg->init_vect_0_r);
342 ctx->init_vect_1_l = readl_relaxed(&src_reg->init_vect_1_l);
343 ctx->init_vect_1_r = readl_relaxed(&src_reg->init_vect_1_r);
344 }
345}
346
347/**
348 * cryp_restore_device_context - Restore hardware registers and
349 * other device context parameter
350 * @device_data: Pointer to the device data struct for base address.
351 * @ctx: Crypto device context
352 */
353void cryp_restore_device_context(struct cryp_device_data *device_data,
354 struct cryp_device_context *ctx)
355{
356 struct cryp_register *reg = device_data->base;
357 struct cryp_config *config =
358 (struct cryp_config *)device_data->current_ctx;
359
360 /*
361 * Fall through for all items in switch statement. DES is captured in
362 * the default.
363 */
364 switch (config->keysize) {
365 case CRYP_KEY_SIZE_256:
366 writel_relaxed(ctx->key_4_l, &reg->key_4_l);
367 writel_relaxed(ctx->key_4_r, &reg->key_4_r);
368
369 case CRYP_KEY_SIZE_192:
370 writel_relaxed(ctx->key_3_l, &reg->key_3_l);
371 writel_relaxed(ctx->key_3_r, &reg->key_3_r);
372
373 case CRYP_KEY_SIZE_128:
374 writel_relaxed(ctx->key_2_l, &reg->key_2_l);
375 writel_relaxed(ctx->key_2_r, &reg->key_2_r);
376
377 default:
378 writel_relaxed(ctx->key_1_l, &reg->key_1_l);
379 writel_relaxed(ctx->key_1_r, &reg->key_1_r);
380 }
381
382 /* Restore IV for CBC mode for AES and DES. */
383 if (config->algomode == CRYP_ALGO_TDES_CBC ||
384 config->algomode == CRYP_ALGO_DES_CBC ||
385 config->algomode == CRYP_ALGO_AES_CBC) {
386 writel_relaxed(ctx->init_vect_0_l, &reg->init_vect_0_l);
387 writel_relaxed(ctx->init_vect_0_r, &reg->init_vect_0_r);
388 writel_relaxed(ctx->init_vect_1_l, &reg->init_vect_1_l);
389 writel_relaxed(ctx->init_vect_1_r, &reg->init_vect_1_r);
390 }
391}