Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Freescale i.MX23/i.MX28 Data Co-Processor driver
4 *
5 * Copyright (C) 2013 Marek Vasut <marex@denx.de>
6 */
7
8#include <linux/dma-mapping.h>
9#include <linux/interrupt.h>
10#include <linux/io.h>
11#include <linux/kernel.h>
12#include <linux/kthread.h>
13#include <linux/module.h>
14#include <linux/of.h>
15#include <linux/platform_device.h>
16#include <linux/stmp_device.h>
17#include <linux/clk.h>
18
19#include <crypto/aes.h>
20#include <crypto/sha1.h>
21#include <crypto/sha2.h>
22#include <crypto/internal/hash.h>
23#include <crypto/internal/skcipher.h>
24#include <crypto/scatterwalk.h>
25
26#define DCP_MAX_CHANS 4
27#define DCP_BUF_SZ PAGE_SIZE
28#define DCP_SHA_PAY_SZ 64
29
30#define DCP_ALIGNMENT 64
31
32/*
33 * Null hashes to align with hw behavior on imx6sl and ull
34 * these are flipped for consistency with hw output
35 */
36static const uint8_t sha1_null_hash[] =
37 "\x09\x07\xd8\xaf\x90\x18\x60\x95\xef\xbf"
38 "\x55\x32\x0d\x4b\x6b\x5e\xee\xa3\x39\xda";
39
40static const uint8_t sha256_null_hash[] =
41 "\x55\xb8\x52\x78\x1b\x99\x95\xa4"
42 "\x4c\x93\x9b\x64\xe4\x41\xae\x27"
43 "\x24\xb9\x6f\x99\xc8\xf4\xfb\x9a"
44 "\x14\x1c\xfc\x98\x42\xc4\xb0\xe3";
45
46/* DCP DMA descriptor. */
47struct dcp_dma_desc {
48 uint32_t next_cmd_addr;
49 uint32_t control0;
50 uint32_t control1;
51 uint32_t source;
52 uint32_t destination;
53 uint32_t size;
54 uint32_t payload;
55 uint32_t status;
56};
57
58/* Coherent aligned block for bounce buffering. */
59struct dcp_coherent_block {
60 uint8_t aes_in_buf[DCP_BUF_SZ];
61 uint8_t aes_out_buf[DCP_BUF_SZ];
62 uint8_t sha_in_buf[DCP_BUF_SZ];
63 uint8_t sha_out_buf[DCP_SHA_PAY_SZ];
64
65 uint8_t aes_key[2 * AES_KEYSIZE_128];
66
67 struct dcp_dma_desc desc[DCP_MAX_CHANS];
68};
69
70struct dcp {
71 struct device *dev;
72 void __iomem *base;
73
74 uint32_t caps;
75
76 struct dcp_coherent_block *coh;
77
78 struct completion completion[DCP_MAX_CHANS];
79 spinlock_t lock[DCP_MAX_CHANS];
80 struct task_struct *thread[DCP_MAX_CHANS];
81 struct crypto_queue queue[DCP_MAX_CHANS];
82 struct clk *dcp_clk;
83};
84
85enum dcp_chan {
86 DCP_CHAN_HASH_SHA = 0,
87 DCP_CHAN_CRYPTO = 2,
88};
89
90struct dcp_async_ctx {
91 /* Common context */
92 enum dcp_chan chan;
93 uint32_t fill;
94
95 /* SHA Hash-specific context */
96 struct mutex mutex;
97 uint32_t alg;
98 unsigned int hot:1;
99
100 /* Crypto-specific context */
101 struct crypto_skcipher *fallback;
102 unsigned int key_len;
103 uint8_t key[AES_KEYSIZE_128];
104};
105
106struct dcp_aes_req_ctx {
107 unsigned int enc:1;
108 unsigned int ecb:1;
109 struct skcipher_request fallback_req; // keep at the end
110};
111
112struct dcp_sha_req_ctx {
113 unsigned int init:1;
114 unsigned int fini:1;
115};
116
117struct dcp_export_state {
118 struct dcp_sha_req_ctx req_ctx;
119 struct dcp_async_ctx async_ctx;
120};
121
122/*
123 * There can even be only one instance of the MXS DCP due to the
124 * design of Linux Crypto API.
125 */
126static struct dcp *global_sdcp;
127
128/* DCP register layout. */
129#define MXS_DCP_CTRL 0x00
130#define MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES (1 << 23)
131#define MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING (1 << 22)
132
133#define MXS_DCP_STAT 0x10
134#define MXS_DCP_STAT_CLR 0x18
135#define MXS_DCP_STAT_IRQ_MASK 0xf
136
137#define MXS_DCP_CHANNELCTRL 0x20
138#define MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK 0xff
139
140#define MXS_DCP_CAPABILITY1 0x40
141#define MXS_DCP_CAPABILITY1_SHA256 (4 << 16)
142#define MXS_DCP_CAPABILITY1_SHA1 (1 << 16)
143#define MXS_DCP_CAPABILITY1_AES128 (1 << 0)
144
145#define MXS_DCP_CONTEXT 0x50
146
147#define MXS_DCP_CH_N_CMDPTR(n) (0x100 + ((n) * 0x40))
148
149#define MXS_DCP_CH_N_SEMA(n) (0x110 + ((n) * 0x40))
150
151#define MXS_DCP_CH_N_STAT(n) (0x120 + ((n) * 0x40))
152#define MXS_DCP_CH_N_STAT_CLR(n) (0x128 + ((n) * 0x40))
153
154/* DMA descriptor bits. */
155#define MXS_DCP_CONTROL0_HASH_TERM (1 << 13)
156#define MXS_DCP_CONTROL0_HASH_INIT (1 << 12)
157#define MXS_DCP_CONTROL0_PAYLOAD_KEY (1 << 11)
158#define MXS_DCP_CONTROL0_CIPHER_ENCRYPT (1 << 8)
159#define MXS_DCP_CONTROL0_CIPHER_INIT (1 << 9)
160#define MXS_DCP_CONTROL0_ENABLE_HASH (1 << 6)
161#define MXS_DCP_CONTROL0_ENABLE_CIPHER (1 << 5)
162#define MXS_DCP_CONTROL0_DECR_SEMAPHORE (1 << 1)
163#define MXS_DCP_CONTROL0_INTERRUPT (1 << 0)
164
165#define MXS_DCP_CONTROL1_HASH_SELECT_SHA256 (2 << 16)
166#define MXS_DCP_CONTROL1_HASH_SELECT_SHA1 (0 << 16)
167#define MXS_DCP_CONTROL1_CIPHER_MODE_CBC (1 << 4)
168#define MXS_DCP_CONTROL1_CIPHER_MODE_ECB (0 << 4)
169#define MXS_DCP_CONTROL1_CIPHER_SELECT_AES128 (0 << 0)
170
171static int mxs_dcp_start_dma(struct dcp_async_ctx *actx)
172{
173 int dma_err;
174 struct dcp *sdcp = global_sdcp;
175 const int chan = actx->chan;
176 uint32_t stat;
177 unsigned long ret;
178 struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
179 dma_addr_t desc_phys = dma_map_single(sdcp->dev, desc, sizeof(*desc),
180 DMA_TO_DEVICE);
181
182 dma_err = dma_mapping_error(sdcp->dev, desc_phys);
183 if (dma_err)
184 return dma_err;
185
186 reinit_completion(&sdcp->completion[chan]);
187
188 /* Clear status register. */
189 writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(chan));
190
191 /* Load the DMA descriptor. */
192 writel(desc_phys, sdcp->base + MXS_DCP_CH_N_CMDPTR(chan));
193
194 /* Increment the semaphore to start the DMA transfer. */
195 writel(1, sdcp->base + MXS_DCP_CH_N_SEMA(chan));
196
197 ret = wait_for_completion_timeout(&sdcp->completion[chan],
198 msecs_to_jiffies(1000));
199 if (!ret) {
200 dev_err(sdcp->dev, "Channel %i timeout (DCP_STAT=0x%08x)\n",
201 chan, readl(sdcp->base + MXS_DCP_STAT));
202 return -ETIMEDOUT;
203 }
204
205 stat = readl(sdcp->base + MXS_DCP_CH_N_STAT(chan));
206 if (stat & 0xff) {
207 dev_err(sdcp->dev, "Channel %i error (CH_STAT=0x%08x)\n",
208 chan, stat);
209 return -EINVAL;
210 }
211
212 dma_unmap_single(sdcp->dev, desc_phys, sizeof(*desc), DMA_TO_DEVICE);
213
214 return 0;
215}
216
217/*
218 * Encryption (AES128)
219 */
220static int mxs_dcp_run_aes(struct dcp_async_ctx *actx,
221 struct skcipher_request *req, int init)
222{
223 dma_addr_t key_phys, src_phys, dst_phys;
224 struct dcp *sdcp = global_sdcp;
225 struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
226 struct dcp_aes_req_ctx *rctx = skcipher_request_ctx(req);
227 int ret;
228
229 key_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_key,
230 2 * AES_KEYSIZE_128, DMA_TO_DEVICE);
231 ret = dma_mapping_error(sdcp->dev, key_phys);
232 if (ret)
233 return ret;
234
235 src_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_in_buf,
236 DCP_BUF_SZ, DMA_TO_DEVICE);
237 ret = dma_mapping_error(sdcp->dev, src_phys);
238 if (ret)
239 goto err_src;
240
241 dst_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_out_buf,
242 DCP_BUF_SZ, DMA_FROM_DEVICE);
243 ret = dma_mapping_error(sdcp->dev, dst_phys);
244 if (ret)
245 goto err_dst;
246
247 if (actx->fill % AES_BLOCK_SIZE) {
248 dev_err(sdcp->dev, "Invalid block size!\n");
249 ret = -EINVAL;
250 goto aes_done_run;
251 }
252
253 /* Fill in the DMA descriptor. */
254 desc->control0 = MXS_DCP_CONTROL0_DECR_SEMAPHORE |
255 MXS_DCP_CONTROL0_INTERRUPT |
256 MXS_DCP_CONTROL0_ENABLE_CIPHER;
257
258 /* Payload contains the key. */
259 desc->control0 |= MXS_DCP_CONTROL0_PAYLOAD_KEY;
260
261 if (rctx->enc)
262 desc->control0 |= MXS_DCP_CONTROL0_CIPHER_ENCRYPT;
263 if (init)
264 desc->control0 |= MXS_DCP_CONTROL0_CIPHER_INIT;
265
266 desc->control1 = MXS_DCP_CONTROL1_CIPHER_SELECT_AES128;
267
268 if (rctx->ecb)
269 desc->control1 |= MXS_DCP_CONTROL1_CIPHER_MODE_ECB;
270 else
271 desc->control1 |= MXS_DCP_CONTROL1_CIPHER_MODE_CBC;
272
273 desc->next_cmd_addr = 0;
274 desc->source = src_phys;
275 desc->destination = dst_phys;
276 desc->size = actx->fill;
277 desc->payload = key_phys;
278 desc->status = 0;
279
280 ret = mxs_dcp_start_dma(actx);
281
282aes_done_run:
283 dma_unmap_single(sdcp->dev, dst_phys, DCP_BUF_SZ, DMA_FROM_DEVICE);
284err_dst:
285 dma_unmap_single(sdcp->dev, src_phys, DCP_BUF_SZ, DMA_TO_DEVICE);
286err_src:
287 dma_unmap_single(sdcp->dev, key_phys, 2 * AES_KEYSIZE_128,
288 DMA_TO_DEVICE);
289
290 return ret;
291}
292
293static int mxs_dcp_aes_block_crypt(struct crypto_async_request *arq)
294{
295 struct dcp *sdcp = global_sdcp;
296
297 struct skcipher_request *req = skcipher_request_cast(arq);
298 struct dcp_async_ctx *actx = crypto_tfm_ctx(arq->tfm);
299 struct dcp_aes_req_ctx *rctx = skcipher_request_ctx(req);
300
301 struct scatterlist *dst = req->dst;
302 struct scatterlist *src = req->src;
303 int dst_nents = sg_nents(dst);
304
305 const int out_off = DCP_BUF_SZ;
306 uint8_t *in_buf = sdcp->coh->aes_in_buf;
307 uint8_t *out_buf = sdcp->coh->aes_out_buf;
308
309 uint32_t dst_off = 0;
310 uint8_t *src_buf = NULL;
311 uint32_t last_out_len = 0;
312
313 uint8_t *key = sdcp->coh->aes_key;
314
315 int ret = 0;
316 unsigned int i, len, clen, tlen = 0;
317 int init = 0;
318 bool limit_hit = false;
319
320 actx->fill = 0;
321
322 /* Copy the key from the temporary location. */
323 memcpy(key, actx->key, actx->key_len);
324
325 if (!rctx->ecb) {
326 /* Copy the CBC IV just past the key. */
327 memcpy(key + AES_KEYSIZE_128, req->iv, AES_KEYSIZE_128);
328 /* CBC needs the INIT set. */
329 init = 1;
330 } else {
331 memset(key + AES_KEYSIZE_128, 0, AES_KEYSIZE_128);
332 }
333
334 for_each_sg(req->src, src, sg_nents(req->src), i) {
335 src_buf = sg_virt(src);
336 len = sg_dma_len(src);
337 tlen += len;
338 limit_hit = tlen > req->cryptlen;
339
340 if (limit_hit)
341 len = req->cryptlen - (tlen - len);
342
343 do {
344 if (actx->fill + len > out_off)
345 clen = out_off - actx->fill;
346 else
347 clen = len;
348
349 memcpy(in_buf + actx->fill, src_buf, clen);
350 len -= clen;
351 src_buf += clen;
352 actx->fill += clen;
353
354 /*
355 * If we filled the buffer or this is the last SG,
356 * submit the buffer.
357 */
358 if (actx->fill == out_off || sg_is_last(src) ||
359 limit_hit) {
360 ret = mxs_dcp_run_aes(actx, req, init);
361 if (ret)
362 return ret;
363 init = 0;
364
365 sg_pcopy_from_buffer(dst, dst_nents, out_buf,
366 actx->fill, dst_off);
367 dst_off += actx->fill;
368 last_out_len = actx->fill;
369 actx->fill = 0;
370 }
371 } while (len);
372
373 if (limit_hit)
374 break;
375 }
376
377 /* Copy the IV for CBC for chaining */
378 if (!rctx->ecb) {
379 if (rctx->enc)
380 memcpy(req->iv, out_buf+(last_out_len-AES_BLOCK_SIZE),
381 AES_BLOCK_SIZE);
382 else
383 memcpy(req->iv, in_buf+(last_out_len-AES_BLOCK_SIZE),
384 AES_BLOCK_SIZE);
385 }
386
387 return ret;
388}
389
390static int dcp_chan_thread_aes(void *data)
391{
392 struct dcp *sdcp = global_sdcp;
393 const int chan = DCP_CHAN_CRYPTO;
394
395 struct crypto_async_request *backlog;
396 struct crypto_async_request *arq;
397
398 int ret;
399
400 while (!kthread_should_stop()) {
401 set_current_state(TASK_INTERRUPTIBLE);
402
403 spin_lock(&sdcp->lock[chan]);
404 backlog = crypto_get_backlog(&sdcp->queue[chan]);
405 arq = crypto_dequeue_request(&sdcp->queue[chan]);
406 spin_unlock(&sdcp->lock[chan]);
407
408 if (!backlog && !arq) {
409 schedule();
410 continue;
411 }
412
413 set_current_state(TASK_RUNNING);
414
415 if (backlog)
416 crypto_request_complete(backlog, -EINPROGRESS);
417
418 if (arq) {
419 ret = mxs_dcp_aes_block_crypt(arq);
420 crypto_request_complete(arq, ret);
421 }
422 }
423
424 return 0;
425}
426
427static int mxs_dcp_block_fallback(struct skcipher_request *req, int enc)
428{
429 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
430 struct dcp_aes_req_ctx *rctx = skcipher_request_ctx(req);
431 struct dcp_async_ctx *ctx = crypto_skcipher_ctx(tfm);
432 int ret;
433
434 skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback);
435 skcipher_request_set_callback(&rctx->fallback_req, req->base.flags,
436 req->base.complete, req->base.data);
437 skcipher_request_set_crypt(&rctx->fallback_req, req->src, req->dst,
438 req->cryptlen, req->iv);
439
440 if (enc)
441 ret = crypto_skcipher_encrypt(&rctx->fallback_req);
442 else
443 ret = crypto_skcipher_decrypt(&rctx->fallback_req);
444
445 return ret;
446}
447
448static int mxs_dcp_aes_enqueue(struct skcipher_request *req, int enc, int ecb)
449{
450 struct dcp *sdcp = global_sdcp;
451 struct crypto_async_request *arq = &req->base;
452 struct dcp_async_ctx *actx = crypto_tfm_ctx(arq->tfm);
453 struct dcp_aes_req_ctx *rctx = skcipher_request_ctx(req);
454 int ret;
455
456 if (unlikely(actx->key_len != AES_KEYSIZE_128))
457 return mxs_dcp_block_fallback(req, enc);
458
459 rctx->enc = enc;
460 rctx->ecb = ecb;
461 actx->chan = DCP_CHAN_CRYPTO;
462
463 spin_lock(&sdcp->lock[actx->chan]);
464 ret = crypto_enqueue_request(&sdcp->queue[actx->chan], &req->base);
465 spin_unlock(&sdcp->lock[actx->chan]);
466
467 wake_up_process(sdcp->thread[actx->chan]);
468
469 return ret;
470}
471
472static int mxs_dcp_aes_ecb_decrypt(struct skcipher_request *req)
473{
474 return mxs_dcp_aes_enqueue(req, 0, 1);
475}
476
477static int mxs_dcp_aes_ecb_encrypt(struct skcipher_request *req)
478{
479 return mxs_dcp_aes_enqueue(req, 1, 1);
480}
481
482static int mxs_dcp_aes_cbc_decrypt(struct skcipher_request *req)
483{
484 return mxs_dcp_aes_enqueue(req, 0, 0);
485}
486
487static int mxs_dcp_aes_cbc_encrypt(struct skcipher_request *req)
488{
489 return mxs_dcp_aes_enqueue(req, 1, 0);
490}
491
492static int mxs_dcp_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
493 unsigned int len)
494{
495 struct dcp_async_ctx *actx = crypto_skcipher_ctx(tfm);
496
497 /*
498 * AES 128 is supposed by the hardware, store key into temporary
499 * buffer and exit. We must use the temporary buffer here, since
500 * there can still be an operation in progress.
501 */
502 actx->key_len = len;
503 if (len == AES_KEYSIZE_128) {
504 memcpy(actx->key, key, len);
505 return 0;
506 }
507
508 /*
509 * If the requested AES key size is not supported by the hardware,
510 * but is supported by in-kernel software implementation, we use
511 * software fallback.
512 */
513 crypto_skcipher_clear_flags(actx->fallback, CRYPTO_TFM_REQ_MASK);
514 crypto_skcipher_set_flags(actx->fallback,
515 tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
516 return crypto_skcipher_setkey(actx->fallback, key, len);
517}
518
519static int mxs_dcp_aes_fallback_init_tfm(struct crypto_skcipher *tfm)
520{
521 const char *name = crypto_tfm_alg_name(crypto_skcipher_tfm(tfm));
522 struct dcp_async_ctx *actx = crypto_skcipher_ctx(tfm);
523 struct crypto_skcipher *blk;
524
525 blk = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
526 if (IS_ERR(blk))
527 return PTR_ERR(blk);
528
529 actx->fallback = blk;
530 crypto_skcipher_set_reqsize(tfm, sizeof(struct dcp_aes_req_ctx) +
531 crypto_skcipher_reqsize(blk));
532 return 0;
533}
534
535static void mxs_dcp_aes_fallback_exit_tfm(struct crypto_skcipher *tfm)
536{
537 struct dcp_async_ctx *actx = crypto_skcipher_ctx(tfm);
538
539 crypto_free_skcipher(actx->fallback);
540}
541
542/*
543 * Hashing (SHA1/SHA256)
544 */
545static int mxs_dcp_run_sha(struct ahash_request *req)
546{
547 struct dcp *sdcp = global_sdcp;
548 int ret;
549
550 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
551 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
552 struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
553 struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
554
555 dma_addr_t digest_phys = 0;
556 dma_addr_t buf_phys = dma_map_single(sdcp->dev, sdcp->coh->sha_in_buf,
557 DCP_BUF_SZ, DMA_TO_DEVICE);
558
559 ret = dma_mapping_error(sdcp->dev, buf_phys);
560 if (ret)
561 return ret;
562
563 /* Fill in the DMA descriptor. */
564 desc->control0 = MXS_DCP_CONTROL0_DECR_SEMAPHORE |
565 MXS_DCP_CONTROL0_INTERRUPT |
566 MXS_DCP_CONTROL0_ENABLE_HASH;
567 if (rctx->init)
568 desc->control0 |= MXS_DCP_CONTROL0_HASH_INIT;
569
570 desc->control1 = actx->alg;
571 desc->next_cmd_addr = 0;
572 desc->source = buf_phys;
573 desc->destination = 0;
574 desc->size = actx->fill;
575 desc->payload = 0;
576 desc->status = 0;
577
578 /*
579 * Align driver with hw behavior when generating null hashes
580 */
581 if (rctx->init && rctx->fini && desc->size == 0) {
582 struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
583 const uint8_t *sha_buf =
584 (actx->alg == MXS_DCP_CONTROL1_HASH_SELECT_SHA1) ?
585 sha1_null_hash : sha256_null_hash;
586 memcpy(sdcp->coh->sha_out_buf, sha_buf, halg->digestsize);
587 ret = 0;
588 goto done_run;
589 }
590
591 /* Set HASH_TERM bit for last transfer block. */
592 if (rctx->fini) {
593 digest_phys = dma_map_single(sdcp->dev, sdcp->coh->sha_out_buf,
594 DCP_SHA_PAY_SZ, DMA_FROM_DEVICE);
595 ret = dma_mapping_error(sdcp->dev, digest_phys);
596 if (ret)
597 goto done_run;
598
599 desc->control0 |= MXS_DCP_CONTROL0_HASH_TERM;
600 desc->payload = digest_phys;
601 }
602
603 ret = mxs_dcp_start_dma(actx);
604
605 if (rctx->fini)
606 dma_unmap_single(sdcp->dev, digest_phys, DCP_SHA_PAY_SZ,
607 DMA_FROM_DEVICE);
608
609done_run:
610 dma_unmap_single(sdcp->dev, buf_phys, DCP_BUF_SZ, DMA_TO_DEVICE);
611
612 return ret;
613}
614
615static int dcp_sha_req_to_buf(struct crypto_async_request *arq)
616{
617 struct dcp *sdcp = global_sdcp;
618
619 struct ahash_request *req = ahash_request_cast(arq);
620 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
621 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
622 struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
623 struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
624
625 uint8_t *in_buf = sdcp->coh->sha_in_buf;
626 uint8_t *out_buf = sdcp->coh->sha_out_buf;
627
628 struct scatterlist *src;
629
630 unsigned int i, len, clen, oft = 0;
631 int ret;
632
633 int fin = rctx->fini;
634 if (fin)
635 rctx->fini = 0;
636
637 src = req->src;
638 len = req->nbytes;
639
640 while (len) {
641 if (actx->fill + len > DCP_BUF_SZ)
642 clen = DCP_BUF_SZ - actx->fill;
643 else
644 clen = len;
645
646 scatterwalk_map_and_copy(in_buf + actx->fill, src, oft, clen,
647 0);
648
649 len -= clen;
650 oft += clen;
651 actx->fill += clen;
652
653 /*
654 * If we filled the buffer and still have some
655 * more data, submit the buffer.
656 */
657 if (len && actx->fill == DCP_BUF_SZ) {
658 ret = mxs_dcp_run_sha(req);
659 if (ret)
660 return ret;
661 actx->fill = 0;
662 rctx->init = 0;
663 }
664 }
665
666 if (fin) {
667 rctx->fini = 1;
668
669 /* Submit whatever is left. */
670 if (!req->result)
671 return -EINVAL;
672
673 ret = mxs_dcp_run_sha(req);
674 if (ret)
675 return ret;
676
677 actx->fill = 0;
678
679 /* For some reason the result is flipped */
680 for (i = 0; i < halg->digestsize; i++)
681 req->result[i] = out_buf[halg->digestsize - i - 1];
682 }
683
684 return 0;
685}
686
687static int dcp_chan_thread_sha(void *data)
688{
689 struct dcp *sdcp = global_sdcp;
690 const int chan = DCP_CHAN_HASH_SHA;
691
692 struct crypto_async_request *backlog;
693 struct crypto_async_request *arq;
694 int ret;
695
696 while (!kthread_should_stop()) {
697 set_current_state(TASK_INTERRUPTIBLE);
698
699 spin_lock(&sdcp->lock[chan]);
700 backlog = crypto_get_backlog(&sdcp->queue[chan]);
701 arq = crypto_dequeue_request(&sdcp->queue[chan]);
702 spin_unlock(&sdcp->lock[chan]);
703
704 if (!backlog && !arq) {
705 schedule();
706 continue;
707 }
708
709 set_current_state(TASK_RUNNING);
710
711 if (backlog)
712 crypto_request_complete(backlog, -EINPROGRESS);
713
714 if (arq) {
715 ret = dcp_sha_req_to_buf(arq);
716 crypto_request_complete(arq, ret);
717 }
718 }
719
720 return 0;
721}
722
723static int dcp_sha_init(struct ahash_request *req)
724{
725 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
726 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
727
728 struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
729
730 /*
731 * Start hashing session. The code below only inits the
732 * hashing session context, nothing more.
733 */
734 memset(actx, 0, sizeof(*actx));
735
736 if (strcmp(halg->base.cra_name, "sha1") == 0)
737 actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA1;
738 else
739 actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA256;
740
741 actx->fill = 0;
742 actx->hot = 0;
743 actx->chan = DCP_CHAN_HASH_SHA;
744
745 mutex_init(&actx->mutex);
746
747 return 0;
748}
749
750static int dcp_sha_update_fx(struct ahash_request *req, int fini)
751{
752 struct dcp *sdcp = global_sdcp;
753
754 struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
755 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
756 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
757
758 int ret;
759
760 /*
761 * Ignore requests that have no data in them and are not
762 * the trailing requests in the stream of requests.
763 */
764 if (!req->nbytes && !fini)
765 return 0;
766
767 mutex_lock(&actx->mutex);
768
769 rctx->fini = fini;
770
771 if (!actx->hot) {
772 actx->hot = 1;
773 rctx->init = 1;
774 }
775
776 spin_lock(&sdcp->lock[actx->chan]);
777 ret = crypto_enqueue_request(&sdcp->queue[actx->chan], &req->base);
778 spin_unlock(&sdcp->lock[actx->chan]);
779
780 wake_up_process(sdcp->thread[actx->chan]);
781 mutex_unlock(&actx->mutex);
782
783 return ret;
784}
785
786static int dcp_sha_update(struct ahash_request *req)
787{
788 return dcp_sha_update_fx(req, 0);
789}
790
791static int dcp_sha_final(struct ahash_request *req)
792{
793 ahash_request_set_crypt(req, NULL, req->result, 0);
794 req->nbytes = 0;
795 return dcp_sha_update_fx(req, 1);
796}
797
798static int dcp_sha_finup(struct ahash_request *req)
799{
800 return dcp_sha_update_fx(req, 1);
801}
802
803static int dcp_sha_digest(struct ahash_request *req)
804{
805 int ret;
806
807 ret = dcp_sha_init(req);
808 if (ret)
809 return ret;
810
811 return dcp_sha_finup(req);
812}
813
814static int dcp_sha_import(struct ahash_request *req, const void *in)
815{
816 struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
817 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
818 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
819 const struct dcp_export_state *export = in;
820
821 memset(rctx, 0, sizeof(struct dcp_sha_req_ctx));
822 memset(actx, 0, sizeof(struct dcp_async_ctx));
823 memcpy(rctx, &export->req_ctx, sizeof(struct dcp_sha_req_ctx));
824 memcpy(actx, &export->async_ctx, sizeof(struct dcp_async_ctx));
825
826 return 0;
827}
828
829static int dcp_sha_export(struct ahash_request *req, void *out)
830{
831 struct dcp_sha_req_ctx *rctx_state = ahash_request_ctx(req);
832 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
833 struct dcp_async_ctx *actx_state = crypto_ahash_ctx(tfm);
834 struct dcp_export_state *export = out;
835
836 memcpy(&export->req_ctx, rctx_state, sizeof(struct dcp_sha_req_ctx));
837 memcpy(&export->async_ctx, actx_state, sizeof(struct dcp_async_ctx));
838
839 return 0;
840}
841
842static int dcp_sha_cra_init(struct crypto_tfm *tfm)
843{
844 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
845 sizeof(struct dcp_sha_req_ctx));
846 return 0;
847}
848
849static void dcp_sha_cra_exit(struct crypto_tfm *tfm)
850{
851}
852
853/* AES 128 ECB and AES 128 CBC */
854static struct skcipher_alg dcp_aes_algs[] = {
855 {
856 .base.cra_name = "ecb(aes)",
857 .base.cra_driver_name = "ecb-aes-dcp",
858 .base.cra_priority = 400,
859 .base.cra_alignmask = 15,
860 .base.cra_flags = CRYPTO_ALG_ASYNC |
861 CRYPTO_ALG_NEED_FALLBACK,
862 .base.cra_blocksize = AES_BLOCK_SIZE,
863 .base.cra_ctxsize = sizeof(struct dcp_async_ctx),
864 .base.cra_module = THIS_MODULE,
865
866 .min_keysize = AES_MIN_KEY_SIZE,
867 .max_keysize = AES_MAX_KEY_SIZE,
868 .setkey = mxs_dcp_aes_setkey,
869 .encrypt = mxs_dcp_aes_ecb_encrypt,
870 .decrypt = mxs_dcp_aes_ecb_decrypt,
871 .init = mxs_dcp_aes_fallback_init_tfm,
872 .exit = mxs_dcp_aes_fallback_exit_tfm,
873 }, {
874 .base.cra_name = "cbc(aes)",
875 .base.cra_driver_name = "cbc-aes-dcp",
876 .base.cra_priority = 400,
877 .base.cra_alignmask = 15,
878 .base.cra_flags = CRYPTO_ALG_ASYNC |
879 CRYPTO_ALG_NEED_FALLBACK,
880 .base.cra_blocksize = AES_BLOCK_SIZE,
881 .base.cra_ctxsize = sizeof(struct dcp_async_ctx),
882 .base.cra_module = THIS_MODULE,
883
884 .min_keysize = AES_MIN_KEY_SIZE,
885 .max_keysize = AES_MAX_KEY_SIZE,
886 .setkey = mxs_dcp_aes_setkey,
887 .encrypt = mxs_dcp_aes_cbc_encrypt,
888 .decrypt = mxs_dcp_aes_cbc_decrypt,
889 .ivsize = AES_BLOCK_SIZE,
890 .init = mxs_dcp_aes_fallback_init_tfm,
891 .exit = mxs_dcp_aes_fallback_exit_tfm,
892 },
893};
894
895/* SHA1 */
896static struct ahash_alg dcp_sha1_alg = {
897 .init = dcp_sha_init,
898 .update = dcp_sha_update,
899 .final = dcp_sha_final,
900 .finup = dcp_sha_finup,
901 .digest = dcp_sha_digest,
902 .import = dcp_sha_import,
903 .export = dcp_sha_export,
904 .halg = {
905 .digestsize = SHA1_DIGEST_SIZE,
906 .statesize = sizeof(struct dcp_export_state),
907 .base = {
908 .cra_name = "sha1",
909 .cra_driver_name = "sha1-dcp",
910 .cra_priority = 400,
911 .cra_flags = CRYPTO_ALG_ASYNC,
912 .cra_blocksize = SHA1_BLOCK_SIZE,
913 .cra_ctxsize = sizeof(struct dcp_async_ctx),
914 .cra_module = THIS_MODULE,
915 .cra_init = dcp_sha_cra_init,
916 .cra_exit = dcp_sha_cra_exit,
917 },
918 },
919};
920
921/* SHA256 */
922static struct ahash_alg dcp_sha256_alg = {
923 .init = dcp_sha_init,
924 .update = dcp_sha_update,
925 .final = dcp_sha_final,
926 .finup = dcp_sha_finup,
927 .digest = dcp_sha_digest,
928 .import = dcp_sha_import,
929 .export = dcp_sha_export,
930 .halg = {
931 .digestsize = SHA256_DIGEST_SIZE,
932 .statesize = sizeof(struct dcp_export_state),
933 .base = {
934 .cra_name = "sha256",
935 .cra_driver_name = "sha256-dcp",
936 .cra_priority = 400,
937 .cra_flags = CRYPTO_ALG_ASYNC,
938 .cra_blocksize = SHA256_BLOCK_SIZE,
939 .cra_ctxsize = sizeof(struct dcp_async_ctx),
940 .cra_module = THIS_MODULE,
941 .cra_init = dcp_sha_cra_init,
942 .cra_exit = dcp_sha_cra_exit,
943 },
944 },
945};
946
947static irqreturn_t mxs_dcp_irq(int irq, void *context)
948{
949 struct dcp *sdcp = context;
950 uint32_t stat;
951 int i;
952
953 stat = readl(sdcp->base + MXS_DCP_STAT);
954 stat &= MXS_DCP_STAT_IRQ_MASK;
955 if (!stat)
956 return IRQ_NONE;
957
958 /* Clear the interrupts. */
959 writel(stat, sdcp->base + MXS_DCP_STAT_CLR);
960
961 /* Complete the DMA requests that finished. */
962 for (i = 0; i < DCP_MAX_CHANS; i++)
963 if (stat & (1 << i))
964 complete(&sdcp->completion[i]);
965
966 return IRQ_HANDLED;
967}
968
969static int mxs_dcp_probe(struct platform_device *pdev)
970{
971 struct device *dev = &pdev->dev;
972 struct dcp *sdcp = NULL;
973 int i, ret;
974 int dcp_vmi_irq, dcp_irq;
975
976 if (global_sdcp) {
977 dev_err(dev, "Only one DCP instance allowed!\n");
978 return -ENODEV;
979 }
980
981 dcp_vmi_irq = platform_get_irq(pdev, 0);
982 if (dcp_vmi_irq < 0)
983 return dcp_vmi_irq;
984
985 dcp_irq = platform_get_irq(pdev, 1);
986 if (dcp_irq < 0)
987 return dcp_irq;
988
989 sdcp = devm_kzalloc(dev, sizeof(*sdcp), GFP_KERNEL);
990 if (!sdcp)
991 return -ENOMEM;
992
993 sdcp->dev = dev;
994 sdcp->base = devm_platform_ioremap_resource(pdev, 0);
995 if (IS_ERR(sdcp->base))
996 return PTR_ERR(sdcp->base);
997
998
999 ret = devm_request_irq(dev, dcp_vmi_irq, mxs_dcp_irq, 0,
1000 "dcp-vmi-irq", sdcp);
1001 if (ret) {
1002 dev_err(dev, "Failed to claim DCP VMI IRQ!\n");
1003 return ret;
1004 }
1005
1006 ret = devm_request_irq(dev, dcp_irq, mxs_dcp_irq, 0,
1007 "dcp-irq", sdcp);
1008 if (ret) {
1009 dev_err(dev, "Failed to claim DCP IRQ!\n");
1010 return ret;
1011 }
1012
1013 /* Allocate coherent helper block. */
1014 sdcp->coh = devm_kzalloc(dev, sizeof(*sdcp->coh) + DCP_ALIGNMENT,
1015 GFP_KERNEL);
1016 if (!sdcp->coh)
1017 return -ENOMEM;
1018
1019 /* Re-align the structure so it fits the DCP constraints. */
1020 sdcp->coh = PTR_ALIGN(sdcp->coh, DCP_ALIGNMENT);
1021
1022 /* DCP clock is optional, only used on some SOCs */
1023 sdcp->dcp_clk = devm_clk_get_optional_enabled(dev, "dcp");
1024 if (IS_ERR(sdcp->dcp_clk))
1025 return PTR_ERR(sdcp->dcp_clk);
1026
1027 /* Restart the DCP block. */
1028 ret = stmp_reset_block(sdcp->base);
1029 if (ret) {
1030 dev_err(dev, "Failed reset\n");
1031 return ret;
1032 }
1033
1034 /* Initialize control register. */
1035 writel(MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES |
1036 MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING | 0xf,
1037 sdcp->base + MXS_DCP_CTRL);
1038
1039 /* Enable all DCP DMA channels. */
1040 writel(MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK,
1041 sdcp->base + MXS_DCP_CHANNELCTRL);
1042
1043 /*
1044 * We do not enable context switching. Give the context buffer a
1045 * pointer to an illegal address so if context switching is
1046 * inadvertantly enabled, the DCP will return an error instead of
1047 * trashing good memory. The DCP DMA cannot access ROM, so any ROM
1048 * address will do.
1049 */
1050 writel(0xffff0000, sdcp->base + MXS_DCP_CONTEXT);
1051 for (i = 0; i < DCP_MAX_CHANS; i++)
1052 writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(i));
1053 writel(0xffffffff, sdcp->base + MXS_DCP_STAT_CLR);
1054
1055 global_sdcp = sdcp;
1056
1057 platform_set_drvdata(pdev, sdcp);
1058
1059 for (i = 0; i < DCP_MAX_CHANS; i++) {
1060 spin_lock_init(&sdcp->lock[i]);
1061 init_completion(&sdcp->completion[i]);
1062 crypto_init_queue(&sdcp->queue[i], 50);
1063 }
1064
1065 /* Create the SHA and AES handler threads. */
1066 sdcp->thread[DCP_CHAN_HASH_SHA] = kthread_run(dcp_chan_thread_sha,
1067 NULL, "mxs_dcp_chan/sha");
1068 if (IS_ERR(sdcp->thread[DCP_CHAN_HASH_SHA])) {
1069 dev_err(dev, "Error starting SHA thread!\n");
1070 ret = PTR_ERR(sdcp->thread[DCP_CHAN_HASH_SHA]);
1071 return ret;
1072 }
1073
1074 sdcp->thread[DCP_CHAN_CRYPTO] = kthread_run(dcp_chan_thread_aes,
1075 NULL, "mxs_dcp_chan/aes");
1076 if (IS_ERR(sdcp->thread[DCP_CHAN_CRYPTO])) {
1077 dev_err(dev, "Error starting SHA thread!\n");
1078 ret = PTR_ERR(sdcp->thread[DCP_CHAN_CRYPTO]);
1079 goto err_destroy_sha_thread;
1080 }
1081
1082 /* Register the various crypto algorithms. */
1083 sdcp->caps = readl(sdcp->base + MXS_DCP_CAPABILITY1);
1084
1085 if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128) {
1086 ret = crypto_register_skciphers(dcp_aes_algs,
1087 ARRAY_SIZE(dcp_aes_algs));
1088 if (ret) {
1089 /* Failed to register algorithm. */
1090 dev_err(dev, "Failed to register AES crypto!\n");
1091 goto err_destroy_aes_thread;
1092 }
1093 }
1094
1095 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1) {
1096 ret = crypto_register_ahash(&dcp_sha1_alg);
1097 if (ret) {
1098 dev_err(dev, "Failed to register %s hash!\n",
1099 dcp_sha1_alg.halg.base.cra_name);
1100 goto err_unregister_aes;
1101 }
1102 }
1103
1104 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256) {
1105 ret = crypto_register_ahash(&dcp_sha256_alg);
1106 if (ret) {
1107 dev_err(dev, "Failed to register %s hash!\n",
1108 dcp_sha256_alg.halg.base.cra_name);
1109 goto err_unregister_sha1;
1110 }
1111 }
1112
1113 return 0;
1114
1115err_unregister_sha1:
1116 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1)
1117 crypto_unregister_ahash(&dcp_sha1_alg);
1118
1119err_unregister_aes:
1120 if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128)
1121 crypto_unregister_skciphers(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs));
1122
1123err_destroy_aes_thread:
1124 kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]);
1125
1126err_destroy_sha_thread:
1127 kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]);
1128
1129 return ret;
1130}
1131
1132static void mxs_dcp_remove(struct platform_device *pdev)
1133{
1134 struct dcp *sdcp = platform_get_drvdata(pdev);
1135
1136 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256)
1137 crypto_unregister_ahash(&dcp_sha256_alg);
1138
1139 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1)
1140 crypto_unregister_ahash(&dcp_sha1_alg);
1141
1142 if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128)
1143 crypto_unregister_skciphers(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs));
1144
1145 kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]);
1146 kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]);
1147
1148 platform_set_drvdata(pdev, NULL);
1149
1150 global_sdcp = NULL;
1151}
1152
1153static const struct of_device_id mxs_dcp_dt_ids[] = {
1154 { .compatible = "fsl,imx23-dcp", .data = NULL, },
1155 { .compatible = "fsl,imx28-dcp", .data = NULL, },
1156 { /* sentinel */ }
1157};
1158
1159MODULE_DEVICE_TABLE(of, mxs_dcp_dt_ids);
1160
1161static struct platform_driver mxs_dcp_driver = {
1162 .probe = mxs_dcp_probe,
1163 .remove_new = mxs_dcp_remove,
1164 .driver = {
1165 .name = "mxs-dcp",
1166 .of_match_table = mxs_dcp_dt_ids,
1167 },
1168};
1169
1170module_platform_driver(mxs_dcp_driver);
1171
1172MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
1173MODULE_DESCRIPTION("Freescale MXS DCP Driver");
1174MODULE_LICENSE("GPL");
1175MODULE_ALIAS("platform:mxs-dcp");
1/*
2 * Freescale i.MX23/i.MX28 Data Co-Processor driver
3 *
4 * Copyright (C) 2013 Marek Vasut <marex@denx.de>
5 *
6 * The code contained herein is licensed under the GNU General Public
7 * License. You may obtain a copy of the GNU General Public License
8 * Version 2 or later at the following locations:
9 *
10 * http://www.opensource.org/licenses/gpl-license.html
11 * http://www.gnu.org/copyleft/gpl.html
12 */
13
14#include <linux/crypto.h>
15#include <linux/dma-mapping.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
18#include <linux/kernel.h>
19#include <linux/kthread.h>
20#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/platform_device.h>
23#include <linux/stmp_device.h>
24
25#include <crypto/aes.h>
26#include <crypto/sha.h>
27#include <crypto/internal/hash.h>
28
29#define DCP_MAX_CHANS 4
30#define DCP_BUF_SZ PAGE_SIZE
31
32#define DCP_ALIGNMENT 64
33
34/* DCP DMA descriptor. */
35struct dcp_dma_desc {
36 uint32_t next_cmd_addr;
37 uint32_t control0;
38 uint32_t control1;
39 uint32_t source;
40 uint32_t destination;
41 uint32_t size;
42 uint32_t payload;
43 uint32_t status;
44};
45
46/* Coherent aligned block for bounce buffering. */
47struct dcp_coherent_block {
48 uint8_t aes_in_buf[DCP_BUF_SZ];
49 uint8_t aes_out_buf[DCP_BUF_SZ];
50 uint8_t sha_in_buf[DCP_BUF_SZ];
51
52 uint8_t aes_key[2 * AES_KEYSIZE_128];
53
54 struct dcp_dma_desc desc[DCP_MAX_CHANS];
55};
56
57struct dcp {
58 struct device *dev;
59 void __iomem *base;
60
61 uint32_t caps;
62
63 struct dcp_coherent_block *coh;
64
65 struct completion completion[DCP_MAX_CHANS];
66 struct mutex mutex[DCP_MAX_CHANS];
67 struct task_struct *thread[DCP_MAX_CHANS];
68 struct crypto_queue queue[DCP_MAX_CHANS];
69};
70
71enum dcp_chan {
72 DCP_CHAN_HASH_SHA = 0,
73 DCP_CHAN_CRYPTO = 2,
74};
75
76struct dcp_async_ctx {
77 /* Common context */
78 enum dcp_chan chan;
79 uint32_t fill;
80
81 /* SHA Hash-specific context */
82 struct mutex mutex;
83 uint32_t alg;
84 unsigned int hot:1;
85
86 /* Crypto-specific context */
87 struct crypto_ablkcipher *fallback;
88 unsigned int key_len;
89 uint8_t key[AES_KEYSIZE_128];
90};
91
92struct dcp_aes_req_ctx {
93 unsigned int enc:1;
94 unsigned int ecb:1;
95};
96
97struct dcp_sha_req_ctx {
98 unsigned int init:1;
99 unsigned int fini:1;
100};
101
102/*
103 * There can even be only one instance of the MXS DCP due to the
104 * design of Linux Crypto API.
105 */
106static struct dcp *global_sdcp;
107static DEFINE_MUTEX(global_mutex);
108
109/* DCP register layout. */
110#define MXS_DCP_CTRL 0x00
111#define MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES (1 << 23)
112#define MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING (1 << 22)
113
114#define MXS_DCP_STAT 0x10
115#define MXS_DCP_STAT_CLR 0x18
116#define MXS_DCP_STAT_IRQ_MASK 0xf
117
118#define MXS_DCP_CHANNELCTRL 0x20
119#define MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK 0xff
120
121#define MXS_DCP_CAPABILITY1 0x40
122#define MXS_DCP_CAPABILITY1_SHA256 (4 << 16)
123#define MXS_DCP_CAPABILITY1_SHA1 (1 << 16)
124#define MXS_DCP_CAPABILITY1_AES128 (1 << 0)
125
126#define MXS_DCP_CONTEXT 0x50
127
128#define MXS_DCP_CH_N_CMDPTR(n) (0x100 + ((n) * 0x40))
129
130#define MXS_DCP_CH_N_SEMA(n) (0x110 + ((n) * 0x40))
131
132#define MXS_DCP_CH_N_STAT(n) (0x120 + ((n) * 0x40))
133#define MXS_DCP_CH_N_STAT_CLR(n) (0x128 + ((n) * 0x40))
134
135/* DMA descriptor bits. */
136#define MXS_DCP_CONTROL0_HASH_TERM (1 << 13)
137#define MXS_DCP_CONTROL0_HASH_INIT (1 << 12)
138#define MXS_DCP_CONTROL0_PAYLOAD_KEY (1 << 11)
139#define MXS_DCP_CONTROL0_CIPHER_ENCRYPT (1 << 8)
140#define MXS_DCP_CONTROL0_CIPHER_INIT (1 << 9)
141#define MXS_DCP_CONTROL0_ENABLE_HASH (1 << 6)
142#define MXS_DCP_CONTROL0_ENABLE_CIPHER (1 << 5)
143#define MXS_DCP_CONTROL0_DECR_SEMAPHORE (1 << 1)
144#define MXS_DCP_CONTROL0_INTERRUPT (1 << 0)
145
146#define MXS_DCP_CONTROL1_HASH_SELECT_SHA256 (2 << 16)
147#define MXS_DCP_CONTROL1_HASH_SELECT_SHA1 (0 << 16)
148#define MXS_DCP_CONTROL1_CIPHER_MODE_CBC (1 << 4)
149#define MXS_DCP_CONTROL1_CIPHER_MODE_ECB (0 << 4)
150#define MXS_DCP_CONTROL1_CIPHER_SELECT_AES128 (0 << 0)
151
152static int mxs_dcp_start_dma(struct dcp_async_ctx *actx)
153{
154 struct dcp *sdcp = global_sdcp;
155 const int chan = actx->chan;
156 uint32_t stat;
157 int ret;
158 struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
159
160 dma_addr_t desc_phys = dma_map_single(sdcp->dev, desc, sizeof(*desc),
161 DMA_TO_DEVICE);
162
163 reinit_completion(&sdcp->completion[chan]);
164
165 /* Clear status register. */
166 writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(chan));
167
168 /* Load the DMA descriptor. */
169 writel(desc_phys, sdcp->base + MXS_DCP_CH_N_CMDPTR(chan));
170
171 /* Increment the semaphore to start the DMA transfer. */
172 writel(1, sdcp->base + MXS_DCP_CH_N_SEMA(chan));
173
174 ret = wait_for_completion_timeout(&sdcp->completion[chan],
175 msecs_to_jiffies(1000));
176 if (!ret) {
177 dev_err(sdcp->dev, "Channel %i timeout (DCP_STAT=0x%08x)\n",
178 chan, readl(sdcp->base + MXS_DCP_STAT));
179 return -ETIMEDOUT;
180 }
181
182 stat = readl(sdcp->base + MXS_DCP_CH_N_STAT(chan));
183 if (stat & 0xff) {
184 dev_err(sdcp->dev, "Channel %i error (CH_STAT=0x%08x)\n",
185 chan, stat);
186 return -EINVAL;
187 }
188
189 dma_unmap_single(sdcp->dev, desc_phys, sizeof(*desc), DMA_TO_DEVICE);
190
191 return 0;
192}
193
194/*
195 * Encryption (AES128)
196 */
197static int mxs_dcp_run_aes(struct dcp_async_ctx *actx,
198 struct ablkcipher_request *req, int init)
199{
200 struct dcp *sdcp = global_sdcp;
201 struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
202 struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
203 int ret;
204
205 dma_addr_t key_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_key,
206 2 * AES_KEYSIZE_128,
207 DMA_TO_DEVICE);
208 dma_addr_t src_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_in_buf,
209 DCP_BUF_SZ, DMA_TO_DEVICE);
210 dma_addr_t dst_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_out_buf,
211 DCP_BUF_SZ, DMA_FROM_DEVICE);
212
213 /* Fill in the DMA descriptor. */
214 desc->control0 = MXS_DCP_CONTROL0_DECR_SEMAPHORE |
215 MXS_DCP_CONTROL0_INTERRUPT |
216 MXS_DCP_CONTROL0_ENABLE_CIPHER;
217
218 /* Payload contains the key. */
219 desc->control0 |= MXS_DCP_CONTROL0_PAYLOAD_KEY;
220
221 if (rctx->enc)
222 desc->control0 |= MXS_DCP_CONTROL0_CIPHER_ENCRYPT;
223 if (init)
224 desc->control0 |= MXS_DCP_CONTROL0_CIPHER_INIT;
225
226 desc->control1 = MXS_DCP_CONTROL1_CIPHER_SELECT_AES128;
227
228 if (rctx->ecb)
229 desc->control1 |= MXS_DCP_CONTROL1_CIPHER_MODE_ECB;
230 else
231 desc->control1 |= MXS_DCP_CONTROL1_CIPHER_MODE_CBC;
232
233 desc->next_cmd_addr = 0;
234 desc->source = src_phys;
235 desc->destination = dst_phys;
236 desc->size = actx->fill;
237 desc->payload = key_phys;
238 desc->status = 0;
239
240 ret = mxs_dcp_start_dma(actx);
241
242 dma_unmap_single(sdcp->dev, key_phys, 2 * AES_KEYSIZE_128,
243 DMA_TO_DEVICE);
244 dma_unmap_single(sdcp->dev, src_phys, DCP_BUF_SZ, DMA_TO_DEVICE);
245 dma_unmap_single(sdcp->dev, dst_phys, DCP_BUF_SZ, DMA_FROM_DEVICE);
246
247 return ret;
248}
249
250static int mxs_dcp_aes_block_crypt(struct crypto_async_request *arq)
251{
252 struct dcp *sdcp = global_sdcp;
253
254 struct ablkcipher_request *req = ablkcipher_request_cast(arq);
255 struct dcp_async_ctx *actx = crypto_tfm_ctx(arq->tfm);
256 struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
257
258 struct scatterlist *dst = req->dst;
259 struct scatterlist *src = req->src;
260 const int nents = sg_nents(req->src);
261
262 const int out_off = DCP_BUF_SZ;
263 uint8_t *in_buf = sdcp->coh->aes_in_buf;
264 uint8_t *out_buf = sdcp->coh->aes_out_buf;
265
266 uint8_t *out_tmp, *src_buf, *dst_buf = NULL;
267 uint32_t dst_off = 0;
268
269 uint8_t *key = sdcp->coh->aes_key;
270
271 int ret = 0;
272 int split = 0;
273 unsigned int i, len, clen, rem = 0;
274 int init = 0;
275
276 actx->fill = 0;
277
278 /* Copy the key from the temporary location. */
279 memcpy(key, actx->key, actx->key_len);
280
281 if (!rctx->ecb) {
282 /* Copy the CBC IV just past the key. */
283 memcpy(key + AES_KEYSIZE_128, req->info, AES_KEYSIZE_128);
284 /* CBC needs the INIT set. */
285 init = 1;
286 } else {
287 memset(key + AES_KEYSIZE_128, 0, AES_KEYSIZE_128);
288 }
289
290 for_each_sg(req->src, src, nents, i) {
291 src_buf = sg_virt(src);
292 len = sg_dma_len(src);
293
294 do {
295 if (actx->fill + len > out_off)
296 clen = out_off - actx->fill;
297 else
298 clen = len;
299
300 memcpy(in_buf + actx->fill, src_buf, clen);
301 len -= clen;
302 src_buf += clen;
303 actx->fill += clen;
304
305 /*
306 * If we filled the buffer or this is the last SG,
307 * submit the buffer.
308 */
309 if (actx->fill == out_off || sg_is_last(src)) {
310 ret = mxs_dcp_run_aes(actx, req, init);
311 if (ret)
312 return ret;
313 init = 0;
314
315 out_tmp = out_buf;
316 while (dst && actx->fill) {
317 if (!split) {
318 dst_buf = sg_virt(dst);
319 dst_off = 0;
320 }
321 rem = min(sg_dma_len(dst) - dst_off,
322 actx->fill);
323
324 memcpy(dst_buf + dst_off, out_tmp, rem);
325 out_tmp += rem;
326 dst_off += rem;
327 actx->fill -= rem;
328
329 if (dst_off == sg_dma_len(dst)) {
330 dst = sg_next(dst);
331 split = 0;
332 } else {
333 split = 1;
334 }
335 }
336 }
337 } while (len);
338 }
339
340 return ret;
341}
342
343static int dcp_chan_thread_aes(void *data)
344{
345 struct dcp *sdcp = global_sdcp;
346 const int chan = DCP_CHAN_CRYPTO;
347
348 struct crypto_async_request *backlog;
349 struct crypto_async_request *arq;
350
351 int ret;
352
353 do {
354 __set_current_state(TASK_INTERRUPTIBLE);
355
356 mutex_lock(&sdcp->mutex[chan]);
357 backlog = crypto_get_backlog(&sdcp->queue[chan]);
358 arq = crypto_dequeue_request(&sdcp->queue[chan]);
359 mutex_unlock(&sdcp->mutex[chan]);
360
361 if (backlog)
362 backlog->complete(backlog, -EINPROGRESS);
363
364 if (arq) {
365 ret = mxs_dcp_aes_block_crypt(arq);
366 arq->complete(arq, ret);
367 continue;
368 }
369
370 schedule();
371 } while (!kthread_should_stop());
372
373 return 0;
374}
375
376static int mxs_dcp_block_fallback(struct ablkcipher_request *req, int enc)
377{
378 struct crypto_tfm *tfm =
379 crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req));
380 struct dcp_async_ctx *ctx = crypto_ablkcipher_ctx(
381 crypto_ablkcipher_reqtfm(req));
382 int ret;
383
384 ablkcipher_request_set_tfm(req, ctx->fallback);
385
386 if (enc)
387 ret = crypto_ablkcipher_encrypt(req);
388 else
389 ret = crypto_ablkcipher_decrypt(req);
390
391 ablkcipher_request_set_tfm(req, __crypto_ablkcipher_cast(tfm));
392
393 return ret;
394}
395
396static int mxs_dcp_aes_enqueue(struct ablkcipher_request *req, int enc, int ecb)
397{
398 struct dcp *sdcp = global_sdcp;
399 struct crypto_async_request *arq = &req->base;
400 struct dcp_async_ctx *actx = crypto_tfm_ctx(arq->tfm);
401 struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
402 int ret;
403
404 if (unlikely(actx->key_len != AES_KEYSIZE_128))
405 return mxs_dcp_block_fallback(req, enc);
406
407 rctx->enc = enc;
408 rctx->ecb = ecb;
409 actx->chan = DCP_CHAN_CRYPTO;
410
411 mutex_lock(&sdcp->mutex[actx->chan]);
412 ret = crypto_enqueue_request(&sdcp->queue[actx->chan], &req->base);
413 mutex_unlock(&sdcp->mutex[actx->chan]);
414
415 wake_up_process(sdcp->thread[actx->chan]);
416
417 return -EINPROGRESS;
418}
419
420static int mxs_dcp_aes_ecb_decrypt(struct ablkcipher_request *req)
421{
422 return mxs_dcp_aes_enqueue(req, 0, 1);
423}
424
425static int mxs_dcp_aes_ecb_encrypt(struct ablkcipher_request *req)
426{
427 return mxs_dcp_aes_enqueue(req, 1, 1);
428}
429
430static int mxs_dcp_aes_cbc_decrypt(struct ablkcipher_request *req)
431{
432 return mxs_dcp_aes_enqueue(req, 0, 0);
433}
434
435static int mxs_dcp_aes_cbc_encrypt(struct ablkcipher_request *req)
436{
437 return mxs_dcp_aes_enqueue(req, 1, 0);
438}
439
440static int mxs_dcp_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
441 unsigned int len)
442{
443 struct dcp_async_ctx *actx = crypto_ablkcipher_ctx(tfm);
444 unsigned int ret;
445
446 /*
447 * AES 128 is supposed by the hardware, store key into temporary
448 * buffer and exit. We must use the temporary buffer here, since
449 * there can still be an operation in progress.
450 */
451 actx->key_len = len;
452 if (len == AES_KEYSIZE_128) {
453 memcpy(actx->key, key, len);
454 return 0;
455 }
456
457 /* Check if the key size is supported by kernel at all. */
458 if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256) {
459 tfm->base.crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
460 return -EINVAL;
461 }
462
463 /*
464 * If the requested AES key size is not supported by the hardware,
465 * but is supported by in-kernel software implementation, we use
466 * software fallback.
467 */
468 actx->fallback->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
469 actx->fallback->base.crt_flags |=
470 tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK;
471
472 ret = crypto_ablkcipher_setkey(actx->fallback, key, len);
473 if (!ret)
474 return 0;
475
476 tfm->base.crt_flags &= ~CRYPTO_TFM_RES_MASK;
477 tfm->base.crt_flags |=
478 actx->fallback->base.crt_flags & CRYPTO_TFM_RES_MASK;
479
480 return ret;
481}
482
483static int mxs_dcp_aes_fallback_init(struct crypto_tfm *tfm)
484{
485 const char *name = tfm->__crt_alg->cra_name;
486 const uint32_t flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK;
487 struct dcp_async_ctx *actx = crypto_tfm_ctx(tfm);
488 struct crypto_ablkcipher *blk;
489
490 blk = crypto_alloc_ablkcipher(name, 0, flags);
491 if (IS_ERR(blk))
492 return PTR_ERR(blk);
493
494 actx->fallback = blk;
495 tfm->crt_ablkcipher.reqsize = sizeof(struct dcp_aes_req_ctx);
496 return 0;
497}
498
499static void mxs_dcp_aes_fallback_exit(struct crypto_tfm *tfm)
500{
501 struct dcp_async_ctx *actx = crypto_tfm_ctx(tfm);
502
503 crypto_free_ablkcipher(actx->fallback);
504 actx->fallback = NULL;
505}
506
507/*
508 * Hashing (SHA1/SHA256)
509 */
510static int mxs_dcp_run_sha(struct ahash_request *req)
511{
512 struct dcp *sdcp = global_sdcp;
513 int ret;
514
515 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
516 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
517 struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
518 struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
519
520 struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
521
522 dma_addr_t digest_phys = 0;
523 dma_addr_t buf_phys = dma_map_single(sdcp->dev, sdcp->coh->sha_in_buf,
524 DCP_BUF_SZ, DMA_TO_DEVICE);
525
526 /* Fill in the DMA descriptor. */
527 desc->control0 = MXS_DCP_CONTROL0_DECR_SEMAPHORE |
528 MXS_DCP_CONTROL0_INTERRUPT |
529 MXS_DCP_CONTROL0_ENABLE_HASH;
530 if (rctx->init)
531 desc->control0 |= MXS_DCP_CONTROL0_HASH_INIT;
532
533 desc->control1 = actx->alg;
534 desc->next_cmd_addr = 0;
535 desc->source = buf_phys;
536 desc->destination = 0;
537 desc->size = actx->fill;
538 desc->payload = 0;
539 desc->status = 0;
540
541 /* Set HASH_TERM bit for last transfer block. */
542 if (rctx->fini) {
543 digest_phys = dma_map_single(sdcp->dev, req->result,
544 halg->digestsize, DMA_FROM_DEVICE);
545 desc->control0 |= MXS_DCP_CONTROL0_HASH_TERM;
546 desc->payload = digest_phys;
547 }
548
549 ret = mxs_dcp_start_dma(actx);
550
551 if (rctx->fini)
552 dma_unmap_single(sdcp->dev, digest_phys, halg->digestsize,
553 DMA_FROM_DEVICE);
554
555 dma_unmap_single(sdcp->dev, buf_phys, DCP_BUF_SZ, DMA_TO_DEVICE);
556
557 return ret;
558}
559
560static int dcp_sha_req_to_buf(struct crypto_async_request *arq)
561{
562 struct dcp *sdcp = global_sdcp;
563
564 struct ahash_request *req = ahash_request_cast(arq);
565 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
566 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
567 struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
568 struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
569 const int nents = sg_nents(req->src);
570
571 uint8_t *in_buf = sdcp->coh->sha_in_buf;
572
573 uint8_t *src_buf;
574
575 struct scatterlist *src;
576
577 unsigned int i, len, clen;
578 int ret;
579
580 int fin = rctx->fini;
581 if (fin)
582 rctx->fini = 0;
583
584 for_each_sg(req->src, src, nents, i) {
585 src_buf = sg_virt(src);
586 len = sg_dma_len(src);
587
588 do {
589 if (actx->fill + len > DCP_BUF_SZ)
590 clen = DCP_BUF_SZ - actx->fill;
591 else
592 clen = len;
593
594 memcpy(in_buf + actx->fill, src_buf, clen);
595 len -= clen;
596 src_buf += clen;
597 actx->fill += clen;
598
599 /*
600 * If we filled the buffer and still have some
601 * more data, submit the buffer.
602 */
603 if (len && actx->fill == DCP_BUF_SZ) {
604 ret = mxs_dcp_run_sha(req);
605 if (ret)
606 return ret;
607 actx->fill = 0;
608 rctx->init = 0;
609 }
610 } while (len);
611 }
612
613 if (fin) {
614 rctx->fini = 1;
615
616 /* Submit whatever is left. */
617 if (!req->result)
618 return -EINVAL;
619
620 ret = mxs_dcp_run_sha(req);
621 if (ret)
622 return ret;
623
624 actx->fill = 0;
625
626 /* For some reason, the result is flipped. */
627 for (i = 0; i < halg->digestsize / 2; i++) {
628 swap(req->result[i],
629 req->result[halg->digestsize - i - 1]);
630 }
631 }
632
633 return 0;
634}
635
636static int dcp_chan_thread_sha(void *data)
637{
638 struct dcp *sdcp = global_sdcp;
639 const int chan = DCP_CHAN_HASH_SHA;
640
641 struct crypto_async_request *backlog;
642 struct crypto_async_request *arq;
643
644 struct dcp_sha_req_ctx *rctx;
645
646 struct ahash_request *req;
647 int ret, fini;
648
649 do {
650 __set_current_state(TASK_INTERRUPTIBLE);
651
652 mutex_lock(&sdcp->mutex[chan]);
653 backlog = crypto_get_backlog(&sdcp->queue[chan]);
654 arq = crypto_dequeue_request(&sdcp->queue[chan]);
655 mutex_unlock(&sdcp->mutex[chan]);
656
657 if (backlog)
658 backlog->complete(backlog, -EINPROGRESS);
659
660 if (arq) {
661 req = ahash_request_cast(arq);
662 rctx = ahash_request_ctx(req);
663
664 ret = dcp_sha_req_to_buf(arq);
665 fini = rctx->fini;
666 arq->complete(arq, ret);
667 if (!fini)
668 continue;
669 }
670
671 schedule();
672 } while (!kthread_should_stop());
673
674 return 0;
675}
676
677static int dcp_sha_init(struct ahash_request *req)
678{
679 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
680 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
681
682 struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
683
684 /*
685 * Start hashing session. The code below only inits the
686 * hashing session context, nothing more.
687 */
688 memset(actx, 0, sizeof(*actx));
689
690 if (strcmp(halg->base.cra_name, "sha1") == 0)
691 actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA1;
692 else
693 actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA256;
694
695 actx->fill = 0;
696 actx->hot = 0;
697 actx->chan = DCP_CHAN_HASH_SHA;
698
699 mutex_init(&actx->mutex);
700
701 return 0;
702}
703
704static int dcp_sha_update_fx(struct ahash_request *req, int fini)
705{
706 struct dcp *sdcp = global_sdcp;
707
708 struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
709 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
710 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
711
712 int ret;
713
714 /*
715 * Ignore requests that have no data in them and are not
716 * the trailing requests in the stream of requests.
717 */
718 if (!req->nbytes && !fini)
719 return 0;
720
721 mutex_lock(&actx->mutex);
722
723 rctx->fini = fini;
724
725 if (!actx->hot) {
726 actx->hot = 1;
727 rctx->init = 1;
728 }
729
730 mutex_lock(&sdcp->mutex[actx->chan]);
731 ret = crypto_enqueue_request(&sdcp->queue[actx->chan], &req->base);
732 mutex_unlock(&sdcp->mutex[actx->chan]);
733
734 wake_up_process(sdcp->thread[actx->chan]);
735 mutex_unlock(&actx->mutex);
736
737 return -EINPROGRESS;
738}
739
740static int dcp_sha_update(struct ahash_request *req)
741{
742 return dcp_sha_update_fx(req, 0);
743}
744
745static int dcp_sha_final(struct ahash_request *req)
746{
747 ahash_request_set_crypt(req, NULL, req->result, 0);
748 req->nbytes = 0;
749 return dcp_sha_update_fx(req, 1);
750}
751
752static int dcp_sha_finup(struct ahash_request *req)
753{
754 return dcp_sha_update_fx(req, 1);
755}
756
757static int dcp_sha_digest(struct ahash_request *req)
758{
759 int ret;
760
761 ret = dcp_sha_init(req);
762 if (ret)
763 return ret;
764
765 return dcp_sha_finup(req);
766}
767
768static int dcp_sha_cra_init(struct crypto_tfm *tfm)
769{
770 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
771 sizeof(struct dcp_sha_req_ctx));
772 return 0;
773}
774
775static void dcp_sha_cra_exit(struct crypto_tfm *tfm)
776{
777}
778
779/* AES 128 ECB and AES 128 CBC */
780static struct crypto_alg dcp_aes_algs[] = {
781 {
782 .cra_name = "ecb(aes)",
783 .cra_driver_name = "ecb-aes-dcp",
784 .cra_priority = 400,
785 .cra_alignmask = 15,
786 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
787 CRYPTO_ALG_ASYNC |
788 CRYPTO_ALG_NEED_FALLBACK,
789 .cra_init = mxs_dcp_aes_fallback_init,
790 .cra_exit = mxs_dcp_aes_fallback_exit,
791 .cra_blocksize = AES_BLOCK_SIZE,
792 .cra_ctxsize = sizeof(struct dcp_async_ctx),
793 .cra_type = &crypto_ablkcipher_type,
794 .cra_module = THIS_MODULE,
795 .cra_u = {
796 .ablkcipher = {
797 .min_keysize = AES_MIN_KEY_SIZE,
798 .max_keysize = AES_MAX_KEY_SIZE,
799 .setkey = mxs_dcp_aes_setkey,
800 .encrypt = mxs_dcp_aes_ecb_encrypt,
801 .decrypt = mxs_dcp_aes_ecb_decrypt
802 },
803 },
804 }, {
805 .cra_name = "cbc(aes)",
806 .cra_driver_name = "cbc-aes-dcp",
807 .cra_priority = 400,
808 .cra_alignmask = 15,
809 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
810 CRYPTO_ALG_ASYNC |
811 CRYPTO_ALG_NEED_FALLBACK,
812 .cra_init = mxs_dcp_aes_fallback_init,
813 .cra_exit = mxs_dcp_aes_fallback_exit,
814 .cra_blocksize = AES_BLOCK_SIZE,
815 .cra_ctxsize = sizeof(struct dcp_async_ctx),
816 .cra_type = &crypto_ablkcipher_type,
817 .cra_module = THIS_MODULE,
818 .cra_u = {
819 .ablkcipher = {
820 .min_keysize = AES_MIN_KEY_SIZE,
821 .max_keysize = AES_MAX_KEY_SIZE,
822 .setkey = mxs_dcp_aes_setkey,
823 .encrypt = mxs_dcp_aes_cbc_encrypt,
824 .decrypt = mxs_dcp_aes_cbc_decrypt,
825 .ivsize = AES_BLOCK_SIZE,
826 },
827 },
828 },
829};
830
831/* SHA1 */
832static struct ahash_alg dcp_sha1_alg = {
833 .init = dcp_sha_init,
834 .update = dcp_sha_update,
835 .final = dcp_sha_final,
836 .finup = dcp_sha_finup,
837 .digest = dcp_sha_digest,
838 .halg = {
839 .digestsize = SHA1_DIGEST_SIZE,
840 .base = {
841 .cra_name = "sha1",
842 .cra_driver_name = "sha1-dcp",
843 .cra_priority = 400,
844 .cra_alignmask = 63,
845 .cra_flags = CRYPTO_ALG_ASYNC,
846 .cra_blocksize = SHA1_BLOCK_SIZE,
847 .cra_ctxsize = sizeof(struct dcp_async_ctx),
848 .cra_module = THIS_MODULE,
849 .cra_init = dcp_sha_cra_init,
850 .cra_exit = dcp_sha_cra_exit,
851 },
852 },
853};
854
855/* SHA256 */
856static struct ahash_alg dcp_sha256_alg = {
857 .init = dcp_sha_init,
858 .update = dcp_sha_update,
859 .final = dcp_sha_final,
860 .finup = dcp_sha_finup,
861 .digest = dcp_sha_digest,
862 .halg = {
863 .digestsize = SHA256_DIGEST_SIZE,
864 .base = {
865 .cra_name = "sha256",
866 .cra_driver_name = "sha256-dcp",
867 .cra_priority = 400,
868 .cra_alignmask = 63,
869 .cra_flags = CRYPTO_ALG_ASYNC,
870 .cra_blocksize = SHA256_BLOCK_SIZE,
871 .cra_ctxsize = sizeof(struct dcp_async_ctx),
872 .cra_module = THIS_MODULE,
873 .cra_init = dcp_sha_cra_init,
874 .cra_exit = dcp_sha_cra_exit,
875 },
876 },
877};
878
879static irqreturn_t mxs_dcp_irq(int irq, void *context)
880{
881 struct dcp *sdcp = context;
882 uint32_t stat;
883 int i;
884
885 stat = readl(sdcp->base + MXS_DCP_STAT);
886 stat &= MXS_DCP_STAT_IRQ_MASK;
887 if (!stat)
888 return IRQ_NONE;
889
890 /* Clear the interrupts. */
891 writel(stat, sdcp->base + MXS_DCP_STAT_CLR);
892
893 /* Complete the DMA requests that finished. */
894 for (i = 0; i < DCP_MAX_CHANS; i++)
895 if (stat & (1 << i))
896 complete(&sdcp->completion[i]);
897
898 return IRQ_HANDLED;
899}
900
901static int mxs_dcp_probe(struct platform_device *pdev)
902{
903 struct device *dev = &pdev->dev;
904 struct dcp *sdcp = NULL;
905 int i, ret;
906
907 struct resource *iores;
908 int dcp_vmi_irq, dcp_irq;
909
910 mutex_lock(&global_mutex);
911 if (global_sdcp) {
912 dev_err(dev, "Only one DCP instance allowed!\n");
913 ret = -ENODEV;
914 goto err_mutex;
915 }
916
917 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
918 dcp_vmi_irq = platform_get_irq(pdev, 0);
919 if (dcp_vmi_irq < 0) {
920 ret = dcp_vmi_irq;
921 goto err_mutex;
922 }
923
924 dcp_irq = platform_get_irq(pdev, 1);
925 if (dcp_irq < 0) {
926 ret = dcp_irq;
927 goto err_mutex;
928 }
929
930 sdcp = devm_kzalloc(dev, sizeof(*sdcp), GFP_KERNEL);
931 if (!sdcp) {
932 ret = -ENOMEM;
933 goto err_mutex;
934 }
935
936 sdcp->dev = dev;
937 sdcp->base = devm_ioremap_resource(dev, iores);
938 if (IS_ERR(sdcp->base)) {
939 ret = PTR_ERR(sdcp->base);
940 goto err_mutex;
941 }
942
943 ret = devm_request_irq(dev, dcp_vmi_irq, mxs_dcp_irq, 0,
944 "dcp-vmi-irq", sdcp);
945 if (ret) {
946 dev_err(dev, "Failed to claim DCP VMI IRQ!\n");
947 goto err_mutex;
948 }
949
950 ret = devm_request_irq(dev, dcp_irq, mxs_dcp_irq, 0,
951 "dcp-irq", sdcp);
952 if (ret) {
953 dev_err(dev, "Failed to claim DCP IRQ!\n");
954 goto err_mutex;
955 }
956
957 /* Allocate coherent helper block. */
958 sdcp->coh = devm_kzalloc(dev, sizeof(*sdcp->coh) + DCP_ALIGNMENT,
959 GFP_KERNEL);
960 if (!sdcp->coh) {
961 ret = -ENOMEM;
962 goto err_mutex;
963 }
964
965 /* Re-align the structure so it fits the DCP constraints. */
966 sdcp->coh = PTR_ALIGN(sdcp->coh, DCP_ALIGNMENT);
967
968 /* Restart the DCP block. */
969 ret = stmp_reset_block(sdcp->base);
970 if (ret)
971 goto err_mutex;
972
973 /* Initialize control register. */
974 writel(MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES |
975 MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING | 0xf,
976 sdcp->base + MXS_DCP_CTRL);
977
978 /* Enable all DCP DMA channels. */
979 writel(MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK,
980 sdcp->base + MXS_DCP_CHANNELCTRL);
981
982 /*
983 * We do not enable context switching. Give the context buffer a
984 * pointer to an illegal address so if context switching is
985 * inadvertantly enabled, the DCP will return an error instead of
986 * trashing good memory. The DCP DMA cannot access ROM, so any ROM
987 * address will do.
988 */
989 writel(0xffff0000, sdcp->base + MXS_DCP_CONTEXT);
990 for (i = 0; i < DCP_MAX_CHANS; i++)
991 writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(i));
992 writel(0xffffffff, sdcp->base + MXS_DCP_STAT_CLR);
993
994 global_sdcp = sdcp;
995
996 platform_set_drvdata(pdev, sdcp);
997
998 for (i = 0; i < DCP_MAX_CHANS; i++) {
999 mutex_init(&sdcp->mutex[i]);
1000 init_completion(&sdcp->completion[i]);
1001 crypto_init_queue(&sdcp->queue[i], 50);
1002 }
1003
1004 /* Create the SHA and AES handler threads. */
1005 sdcp->thread[DCP_CHAN_HASH_SHA] = kthread_run(dcp_chan_thread_sha,
1006 NULL, "mxs_dcp_chan/sha");
1007 if (IS_ERR(sdcp->thread[DCP_CHAN_HASH_SHA])) {
1008 dev_err(dev, "Error starting SHA thread!\n");
1009 ret = PTR_ERR(sdcp->thread[DCP_CHAN_HASH_SHA]);
1010 goto err_mutex;
1011 }
1012
1013 sdcp->thread[DCP_CHAN_CRYPTO] = kthread_run(dcp_chan_thread_aes,
1014 NULL, "mxs_dcp_chan/aes");
1015 if (IS_ERR(sdcp->thread[DCP_CHAN_CRYPTO])) {
1016 dev_err(dev, "Error starting SHA thread!\n");
1017 ret = PTR_ERR(sdcp->thread[DCP_CHAN_CRYPTO]);
1018 goto err_destroy_sha_thread;
1019 }
1020
1021 /* Register the various crypto algorithms. */
1022 sdcp->caps = readl(sdcp->base + MXS_DCP_CAPABILITY1);
1023
1024 if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128) {
1025 ret = crypto_register_algs(dcp_aes_algs,
1026 ARRAY_SIZE(dcp_aes_algs));
1027 if (ret) {
1028 /* Failed to register algorithm. */
1029 dev_err(dev, "Failed to register AES crypto!\n");
1030 goto err_destroy_aes_thread;
1031 }
1032 }
1033
1034 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1) {
1035 ret = crypto_register_ahash(&dcp_sha1_alg);
1036 if (ret) {
1037 dev_err(dev, "Failed to register %s hash!\n",
1038 dcp_sha1_alg.halg.base.cra_name);
1039 goto err_unregister_aes;
1040 }
1041 }
1042
1043 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256) {
1044 ret = crypto_register_ahash(&dcp_sha256_alg);
1045 if (ret) {
1046 dev_err(dev, "Failed to register %s hash!\n",
1047 dcp_sha256_alg.halg.base.cra_name);
1048 goto err_unregister_sha1;
1049 }
1050 }
1051
1052 return 0;
1053
1054err_unregister_sha1:
1055 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1)
1056 crypto_unregister_ahash(&dcp_sha1_alg);
1057
1058err_unregister_aes:
1059 if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128)
1060 crypto_unregister_algs(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs));
1061
1062err_destroy_aes_thread:
1063 kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]);
1064
1065err_destroy_sha_thread:
1066 kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]);
1067
1068err_mutex:
1069 mutex_unlock(&global_mutex);
1070 return ret;
1071}
1072
1073static int mxs_dcp_remove(struct platform_device *pdev)
1074{
1075 struct dcp *sdcp = platform_get_drvdata(pdev);
1076
1077 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256)
1078 crypto_unregister_ahash(&dcp_sha256_alg);
1079
1080 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1)
1081 crypto_unregister_ahash(&dcp_sha1_alg);
1082
1083 if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128)
1084 crypto_unregister_algs(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs));
1085
1086 kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]);
1087 kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]);
1088
1089 platform_set_drvdata(pdev, NULL);
1090
1091 mutex_lock(&global_mutex);
1092 global_sdcp = NULL;
1093 mutex_unlock(&global_mutex);
1094
1095 return 0;
1096}
1097
1098static const struct of_device_id mxs_dcp_dt_ids[] = {
1099 { .compatible = "fsl,imx23-dcp", .data = NULL, },
1100 { .compatible = "fsl,imx28-dcp", .data = NULL, },
1101 { /* sentinel */ }
1102};
1103
1104MODULE_DEVICE_TABLE(of, mxs_dcp_dt_ids);
1105
1106static struct platform_driver mxs_dcp_driver = {
1107 .probe = mxs_dcp_probe,
1108 .remove = mxs_dcp_remove,
1109 .driver = {
1110 .name = "mxs-dcp",
1111 .owner = THIS_MODULE,
1112 .of_match_table = mxs_dcp_dt_ids,
1113 },
1114};
1115
1116module_platform_driver(mxs_dcp_driver);
1117
1118MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
1119MODULE_DESCRIPTION("Freescale MXS DCP Driver");
1120MODULE_LICENSE("GPL");
1121MODULE_ALIAS("platform:mxs-dcp");