2 * Freescale i.MX23/i.MX28 Data Co-Processor driver
4 * Copyright (C) 2013 Marek Vasut <marex@denx.de>
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:
10 * http://www.opensource.org/licenses/gpl-license.html
11 * http://www.gnu.org/copyleft/gpl.html
14 #include <linux/dma-mapping.h>
15 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/kthread.h>
19 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/stmp_device.h>
24 #include <crypto/aes.h>
25 #include <crypto/sha.h>
26 #include <crypto/internal/hash.h>
27 #include <crypto/internal/skcipher.h>
29 #define DCP_MAX_CHANS 4
30 #define DCP_BUF_SZ PAGE_SIZE
32 #define DCP_ALIGNMENT 64
34 /* DCP DMA descriptor. */
36 uint32_t next_cmd_addr
;
46 /* Coherent aligned block for bounce buffering. */
47 struct 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
];
52 uint8_t aes_key
[2 * AES_KEYSIZE_128
];
54 struct dcp_dma_desc desc
[DCP_MAX_CHANS
];
63 struct dcp_coherent_block
*coh
;
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
];
72 DCP_CHAN_HASH_SHA
= 0,
76 struct dcp_async_ctx
{
81 /* SHA Hash-specific context */
86 /* Crypto-specific context */
87 struct crypto_skcipher
*fallback
;
89 uint8_t key
[AES_KEYSIZE_128
];
92 struct dcp_aes_req_ctx
{
97 struct dcp_sha_req_ctx
{
103 * There can even be only one instance of the MXS DCP due to the
104 * design of Linux Crypto API.
106 static struct dcp
*global_sdcp
;
108 /* DCP register layout. */
109 #define MXS_DCP_CTRL 0x00
110 #define MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES (1 << 23)
111 #define MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING (1 << 22)
113 #define MXS_DCP_STAT 0x10
114 #define MXS_DCP_STAT_CLR 0x18
115 #define MXS_DCP_STAT_IRQ_MASK 0xf
117 #define MXS_DCP_CHANNELCTRL 0x20
118 #define MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK 0xff
120 #define MXS_DCP_CAPABILITY1 0x40
121 #define MXS_DCP_CAPABILITY1_SHA256 (4 << 16)
122 #define MXS_DCP_CAPABILITY1_SHA1 (1 << 16)
123 #define MXS_DCP_CAPABILITY1_AES128 (1 << 0)
125 #define MXS_DCP_CONTEXT 0x50
127 #define MXS_DCP_CH_N_CMDPTR(n) (0x100 + ((n) * 0x40))
129 #define MXS_DCP_CH_N_SEMA(n) (0x110 + ((n) * 0x40))
131 #define MXS_DCP_CH_N_STAT(n) (0x120 + ((n) * 0x40))
132 #define MXS_DCP_CH_N_STAT_CLR(n) (0x128 + ((n) * 0x40))
134 /* DMA descriptor bits. */
135 #define MXS_DCP_CONTROL0_HASH_TERM (1 << 13)
136 #define MXS_DCP_CONTROL0_HASH_INIT (1 << 12)
137 #define MXS_DCP_CONTROL0_PAYLOAD_KEY (1 << 11)
138 #define MXS_DCP_CONTROL0_CIPHER_ENCRYPT (1 << 8)
139 #define MXS_DCP_CONTROL0_CIPHER_INIT (1 << 9)
140 #define MXS_DCP_CONTROL0_ENABLE_HASH (1 << 6)
141 #define MXS_DCP_CONTROL0_ENABLE_CIPHER (1 << 5)
142 #define MXS_DCP_CONTROL0_DECR_SEMAPHORE (1 << 1)
143 #define MXS_DCP_CONTROL0_INTERRUPT (1 << 0)
145 #define MXS_DCP_CONTROL1_HASH_SELECT_SHA256 (2 << 16)
146 #define MXS_DCP_CONTROL1_HASH_SELECT_SHA1 (0 << 16)
147 #define MXS_DCP_CONTROL1_CIPHER_MODE_CBC (1 << 4)
148 #define MXS_DCP_CONTROL1_CIPHER_MODE_ECB (0 << 4)
149 #define MXS_DCP_CONTROL1_CIPHER_SELECT_AES128 (0 << 0)
151 static int mxs_dcp_start_dma(struct dcp_async_ctx
*actx
)
153 struct dcp
*sdcp
= global_sdcp
;
154 const int chan
= actx
->chan
;
157 struct dcp_dma_desc
*desc
= &sdcp
->coh
->desc
[actx
->chan
];
159 dma_addr_t desc_phys
= dma_map_single(sdcp
->dev
, desc
, sizeof(*desc
),
162 reinit_completion(&sdcp
->completion
[chan
]);
164 /* Clear status register. */
165 writel(0xffffffff, sdcp
->base
+ MXS_DCP_CH_N_STAT_CLR(chan
));
167 /* Load the DMA descriptor. */
168 writel(desc_phys
, sdcp
->base
+ MXS_DCP_CH_N_CMDPTR(chan
));
170 /* Increment the semaphore to start the DMA transfer. */
171 writel(1, sdcp
->base
+ MXS_DCP_CH_N_SEMA(chan
));
173 ret
= wait_for_completion_timeout(&sdcp
->completion
[chan
],
174 msecs_to_jiffies(1000));
176 dev_err(sdcp
->dev
, "Channel %i timeout (DCP_STAT=0x%08x)\n",
177 chan
, readl(sdcp
->base
+ MXS_DCP_STAT
));
181 stat
= readl(sdcp
->base
+ MXS_DCP_CH_N_STAT(chan
));
183 dev_err(sdcp
->dev
, "Channel %i error (CH_STAT=0x%08x)\n",
188 dma_unmap_single(sdcp
->dev
, desc_phys
, sizeof(*desc
), DMA_TO_DEVICE
);
194 * Encryption (AES128)
196 static int mxs_dcp_run_aes(struct dcp_async_ctx
*actx
,
197 struct ablkcipher_request
*req
, int init
)
199 struct dcp
*sdcp
= global_sdcp
;
200 struct dcp_dma_desc
*desc
= &sdcp
->coh
->desc
[actx
->chan
];
201 struct dcp_aes_req_ctx
*rctx
= ablkcipher_request_ctx(req
);
204 dma_addr_t key_phys
= dma_map_single(sdcp
->dev
, sdcp
->coh
->aes_key
,
207 dma_addr_t src_phys
= dma_map_single(sdcp
->dev
, sdcp
->coh
->aes_in_buf
,
208 DCP_BUF_SZ
, DMA_TO_DEVICE
);
209 dma_addr_t dst_phys
= dma_map_single(sdcp
->dev
, sdcp
->coh
->aes_out_buf
,
210 DCP_BUF_SZ
, DMA_FROM_DEVICE
);
212 /* Fill in the DMA descriptor. */
213 desc
->control0
= MXS_DCP_CONTROL0_DECR_SEMAPHORE
|
214 MXS_DCP_CONTROL0_INTERRUPT
|
215 MXS_DCP_CONTROL0_ENABLE_CIPHER
;
217 /* Payload contains the key. */
218 desc
->control0
|= MXS_DCP_CONTROL0_PAYLOAD_KEY
;
221 desc
->control0
|= MXS_DCP_CONTROL0_CIPHER_ENCRYPT
;
223 desc
->control0
|= MXS_DCP_CONTROL0_CIPHER_INIT
;
225 desc
->control1
= MXS_DCP_CONTROL1_CIPHER_SELECT_AES128
;
228 desc
->control1
|= MXS_DCP_CONTROL1_CIPHER_MODE_ECB
;
230 desc
->control1
|= MXS_DCP_CONTROL1_CIPHER_MODE_CBC
;
232 desc
->next_cmd_addr
= 0;
233 desc
->source
= src_phys
;
234 desc
->destination
= dst_phys
;
235 desc
->size
= actx
->fill
;
236 desc
->payload
= key_phys
;
239 ret
= mxs_dcp_start_dma(actx
);
241 dma_unmap_single(sdcp
->dev
, key_phys
, 2 * AES_KEYSIZE_128
,
243 dma_unmap_single(sdcp
->dev
, src_phys
, DCP_BUF_SZ
, DMA_TO_DEVICE
);
244 dma_unmap_single(sdcp
->dev
, dst_phys
, DCP_BUF_SZ
, DMA_FROM_DEVICE
);
249 static int mxs_dcp_aes_block_crypt(struct crypto_async_request
*arq
)
251 struct dcp
*sdcp
= global_sdcp
;
253 struct ablkcipher_request
*req
= ablkcipher_request_cast(arq
);
254 struct dcp_async_ctx
*actx
= crypto_tfm_ctx(arq
->tfm
);
255 struct dcp_aes_req_ctx
*rctx
= ablkcipher_request_ctx(req
);
257 struct scatterlist
*dst
= req
->dst
;
258 struct scatterlist
*src
= req
->src
;
259 const int nents
= sg_nents(req
->src
);
261 const int out_off
= DCP_BUF_SZ
;
262 uint8_t *in_buf
= sdcp
->coh
->aes_in_buf
;
263 uint8_t *out_buf
= sdcp
->coh
->aes_out_buf
;
265 uint8_t *out_tmp
, *src_buf
, *dst_buf
= NULL
;
266 uint32_t dst_off
= 0;
268 uint8_t *key
= sdcp
->coh
->aes_key
;
272 unsigned int i
, len
, clen
, rem
= 0;
277 /* Copy the key from the temporary location. */
278 memcpy(key
, actx
->key
, actx
->key_len
);
281 /* Copy the CBC IV just past the key. */
282 memcpy(key
+ AES_KEYSIZE_128
, req
->info
, AES_KEYSIZE_128
);
283 /* CBC needs the INIT set. */
286 memset(key
+ AES_KEYSIZE_128
, 0, AES_KEYSIZE_128
);
289 for_each_sg(req
->src
, src
, nents
, i
) {
290 src_buf
= sg_virt(src
);
291 len
= sg_dma_len(src
);
294 if (actx
->fill
+ len
> out_off
)
295 clen
= out_off
- actx
->fill
;
299 memcpy(in_buf
+ actx
->fill
, src_buf
, clen
);
305 * If we filled the buffer or this is the last SG,
308 if (actx
->fill
== out_off
|| sg_is_last(src
)) {
309 ret
= mxs_dcp_run_aes(actx
, req
, init
);
315 while (dst
&& actx
->fill
) {
317 dst_buf
= sg_virt(dst
);
320 rem
= min(sg_dma_len(dst
) - dst_off
,
323 memcpy(dst_buf
+ dst_off
, out_tmp
, rem
);
328 if (dst_off
== sg_dma_len(dst
)) {
342 static int dcp_chan_thread_aes(void *data
)
344 struct dcp
*sdcp
= global_sdcp
;
345 const int chan
= DCP_CHAN_CRYPTO
;
347 struct crypto_async_request
*backlog
;
348 struct crypto_async_request
*arq
;
353 __set_current_state(TASK_INTERRUPTIBLE
);
355 mutex_lock(&sdcp
->mutex
[chan
]);
356 backlog
= crypto_get_backlog(&sdcp
->queue
[chan
]);
357 arq
= crypto_dequeue_request(&sdcp
->queue
[chan
]);
358 mutex_unlock(&sdcp
->mutex
[chan
]);
361 backlog
->complete(backlog
, -EINPROGRESS
);
364 ret
= mxs_dcp_aes_block_crypt(arq
);
365 arq
->complete(arq
, ret
);
370 } while (!kthread_should_stop());
375 static int mxs_dcp_block_fallback(struct ablkcipher_request
*req
, int enc
)
377 struct crypto_ablkcipher
*tfm
= crypto_ablkcipher_reqtfm(req
);
378 struct dcp_async_ctx
*ctx
= crypto_ablkcipher_ctx(tfm
);
379 SKCIPHER_REQUEST_ON_STACK(subreq
, ctx
->fallback
);
382 skcipher_request_set_tfm(subreq
, ctx
->fallback
);
383 skcipher_request_set_callback(subreq
, req
->base
.flags
, NULL
, NULL
);
384 skcipher_request_set_crypt(subreq
, req
->src
, req
->dst
,
385 req
->nbytes
, req
->info
);
388 ret
= crypto_skcipher_encrypt(subreq
);
390 ret
= crypto_skcipher_decrypt(subreq
);
392 skcipher_request_zero(subreq
);
397 static int mxs_dcp_aes_enqueue(struct ablkcipher_request
*req
, int enc
, int ecb
)
399 struct dcp
*sdcp
= global_sdcp
;
400 struct crypto_async_request
*arq
= &req
->base
;
401 struct dcp_async_ctx
*actx
= crypto_tfm_ctx(arq
->tfm
);
402 struct dcp_aes_req_ctx
*rctx
= ablkcipher_request_ctx(req
);
405 if (unlikely(actx
->key_len
!= AES_KEYSIZE_128
))
406 return mxs_dcp_block_fallback(req
, enc
);
410 actx
->chan
= DCP_CHAN_CRYPTO
;
412 mutex_lock(&sdcp
->mutex
[actx
->chan
]);
413 ret
= crypto_enqueue_request(&sdcp
->queue
[actx
->chan
], &req
->base
);
414 mutex_unlock(&sdcp
->mutex
[actx
->chan
]);
416 wake_up_process(sdcp
->thread
[actx
->chan
]);
421 static int mxs_dcp_aes_ecb_decrypt(struct ablkcipher_request
*req
)
423 return mxs_dcp_aes_enqueue(req
, 0, 1);
426 static int mxs_dcp_aes_ecb_encrypt(struct ablkcipher_request
*req
)
428 return mxs_dcp_aes_enqueue(req
, 1, 1);
431 static int mxs_dcp_aes_cbc_decrypt(struct ablkcipher_request
*req
)
433 return mxs_dcp_aes_enqueue(req
, 0, 0);
436 static int mxs_dcp_aes_cbc_encrypt(struct ablkcipher_request
*req
)
438 return mxs_dcp_aes_enqueue(req
, 1, 0);
441 static int mxs_dcp_aes_setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
444 struct dcp_async_ctx
*actx
= crypto_ablkcipher_ctx(tfm
);
448 * AES 128 is supposed by the hardware, store key into temporary
449 * buffer and exit. We must use the temporary buffer here, since
450 * there can still be an operation in progress.
453 if (len
== AES_KEYSIZE_128
) {
454 memcpy(actx
->key
, key
, len
);
459 * If the requested AES key size is not supported by the hardware,
460 * but is supported by in-kernel software implementation, we use
463 crypto_skcipher_clear_flags(actx
->fallback
, CRYPTO_TFM_REQ_MASK
);
464 crypto_skcipher_set_flags(actx
->fallback
,
465 tfm
->base
.crt_flags
& CRYPTO_TFM_REQ_MASK
);
467 ret
= crypto_skcipher_setkey(actx
->fallback
, key
, len
);
471 tfm
->base
.crt_flags
&= ~CRYPTO_TFM_RES_MASK
;
472 tfm
->base
.crt_flags
|= crypto_skcipher_get_flags(actx
->fallback
) &
478 static int mxs_dcp_aes_fallback_init(struct crypto_tfm
*tfm
)
480 const char *name
= crypto_tfm_alg_name(tfm
);
481 const uint32_t flags
= CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
;
482 struct dcp_async_ctx
*actx
= crypto_tfm_ctx(tfm
);
483 struct crypto_skcipher
*blk
;
485 blk
= crypto_alloc_skcipher(name
, 0, flags
);
489 actx
->fallback
= blk
;
490 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct dcp_aes_req_ctx
);
494 static void mxs_dcp_aes_fallback_exit(struct crypto_tfm
*tfm
)
496 struct dcp_async_ctx
*actx
= crypto_tfm_ctx(tfm
);
498 crypto_free_skcipher(actx
->fallback
);
502 * Hashing (SHA1/SHA256)
504 static int mxs_dcp_run_sha(struct ahash_request
*req
)
506 struct dcp
*sdcp
= global_sdcp
;
509 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
510 struct dcp_async_ctx
*actx
= crypto_ahash_ctx(tfm
);
511 struct dcp_sha_req_ctx
*rctx
= ahash_request_ctx(req
);
512 struct hash_alg_common
*halg
= crypto_hash_alg_common(tfm
);
514 struct dcp_dma_desc
*desc
= &sdcp
->coh
->desc
[actx
->chan
];
516 dma_addr_t digest_phys
= 0;
517 dma_addr_t buf_phys
= dma_map_single(sdcp
->dev
, sdcp
->coh
->sha_in_buf
,
518 DCP_BUF_SZ
, DMA_TO_DEVICE
);
520 /* Fill in the DMA descriptor. */
521 desc
->control0
= MXS_DCP_CONTROL0_DECR_SEMAPHORE
|
522 MXS_DCP_CONTROL0_INTERRUPT
|
523 MXS_DCP_CONTROL0_ENABLE_HASH
;
525 desc
->control0
|= MXS_DCP_CONTROL0_HASH_INIT
;
527 desc
->control1
= actx
->alg
;
528 desc
->next_cmd_addr
= 0;
529 desc
->source
= buf_phys
;
530 desc
->destination
= 0;
531 desc
->size
= actx
->fill
;
535 /* Set HASH_TERM bit for last transfer block. */
537 digest_phys
= dma_map_single(sdcp
->dev
, req
->result
,
538 halg
->digestsize
, DMA_FROM_DEVICE
);
539 desc
->control0
|= MXS_DCP_CONTROL0_HASH_TERM
;
540 desc
->payload
= digest_phys
;
543 ret
= mxs_dcp_start_dma(actx
);
546 dma_unmap_single(sdcp
->dev
, digest_phys
, halg
->digestsize
,
549 dma_unmap_single(sdcp
->dev
, buf_phys
, DCP_BUF_SZ
, DMA_TO_DEVICE
);
554 static int dcp_sha_req_to_buf(struct crypto_async_request
*arq
)
556 struct dcp
*sdcp
= global_sdcp
;
558 struct ahash_request
*req
= ahash_request_cast(arq
);
559 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
560 struct dcp_async_ctx
*actx
= crypto_ahash_ctx(tfm
);
561 struct dcp_sha_req_ctx
*rctx
= ahash_request_ctx(req
);
562 struct hash_alg_common
*halg
= crypto_hash_alg_common(tfm
);
563 const int nents
= sg_nents(req
->src
);
565 uint8_t *in_buf
= sdcp
->coh
->sha_in_buf
;
569 struct scatterlist
*src
;
571 unsigned int i
, len
, clen
;
574 int fin
= rctx
->fini
;
578 for_each_sg(req
->src
, src
, nents
, i
) {
579 src_buf
= sg_virt(src
);
580 len
= sg_dma_len(src
);
583 if (actx
->fill
+ len
> DCP_BUF_SZ
)
584 clen
= DCP_BUF_SZ
- actx
->fill
;
588 memcpy(in_buf
+ actx
->fill
, src_buf
, clen
);
594 * If we filled the buffer and still have some
595 * more data, submit the buffer.
597 if (len
&& actx
->fill
== DCP_BUF_SZ
) {
598 ret
= mxs_dcp_run_sha(req
);
610 /* Submit whatever is left. */
614 ret
= mxs_dcp_run_sha(req
);
620 /* For some reason, the result is flipped. */
621 for (i
= 0; i
< halg
->digestsize
/ 2; i
++) {
623 req
->result
[halg
->digestsize
- i
- 1]);
630 static int dcp_chan_thread_sha(void *data
)
632 struct dcp
*sdcp
= global_sdcp
;
633 const int chan
= DCP_CHAN_HASH_SHA
;
635 struct crypto_async_request
*backlog
;
636 struct crypto_async_request
*arq
;
638 struct dcp_sha_req_ctx
*rctx
;
640 struct ahash_request
*req
;
644 __set_current_state(TASK_INTERRUPTIBLE
);
646 mutex_lock(&sdcp
->mutex
[chan
]);
647 backlog
= crypto_get_backlog(&sdcp
->queue
[chan
]);
648 arq
= crypto_dequeue_request(&sdcp
->queue
[chan
]);
649 mutex_unlock(&sdcp
->mutex
[chan
]);
652 backlog
->complete(backlog
, -EINPROGRESS
);
655 req
= ahash_request_cast(arq
);
656 rctx
= ahash_request_ctx(req
);
658 ret
= dcp_sha_req_to_buf(arq
);
660 arq
->complete(arq
, ret
);
666 } while (!kthread_should_stop());
671 static int dcp_sha_init(struct ahash_request
*req
)
673 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
674 struct dcp_async_ctx
*actx
= crypto_ahash_ctx(tfm
);
676 struct hash_alg_common
*halg
= crypto_hash_alg_common(tfm
);
679 * Start hashing session. The code below only inits the
680 * hashing session context, nothing more.
682 memset(actx
, 0, sizeof(*actx
));
684 if (strcmp(halg
->base
.cra_name
, "sha1") == 0)
685 actx
->alg
= MXS_DCP_CONTROL1_HASH_SELECT_SHA1
;
687 actx
->alg
= MXS_DCP_CONTROL1_HASH_SELECT_SHA256
;
691 actx
->chan
= DCP_CHAN_HASH_SHA
;
693 mutex_init(&actx
->mutex
);
698 static int dcp_sha_update_fx(struct ahash_request
*req
, int fini
)
700 struct dcp
*sdcp
= global_sdcp
;
702 struct dcp_sha_req_ctx
*rctx
= ahash_request_ctx(req
);
703 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
704 struct dcp_async_ctx
*actx
= crypto_ahash_ctx(tfm
);
709 * Ignore requests that have no data in them and are not
710 * the trailing requests in the stream of requests.
712 if (!req
->nbytes
&& !fini
)
715 mutex_lock(&actx
->mutex
);
724 mutex_lock(&sdcp
->mutex
[actx
->chan
]);
725 ret
= crypto_enqueue_request(&sdcp
->queue
[actx
->chan
], &req
->base
);
726 mutex_unlock(&sdcp
->mutex
[actx
->chan
]);
728 wake_up_process(sdcp
->thread
[actx
->chan
]);
729 mutex_unlock(&actx
->mutex
);
734 static int dcp_sha_update(struct ahash_request
*req
)
736 return dcp_sha_update_fx(req
, 0);
739 static int dcp_sha_final(struct ahash_request
*req
)
741 ahash_request_set_crypt(req
, NULL
, req
->result
, 0);
743 return dcp_sha_update_fx(req
, 1);
746 static int dcp_sha_finup(struct ahash_request
*req
)
748 return dcp_sha_update_fx(req
, 1);
751 static int dcp_sha_digest(struct ahash_request
*req
)
755 ret
= dcp_sha_init(req
);
759 return dcp_sha_finup(req
);
762 static int dcp_sha_noimport(struct ahash_request
*req
, const void *in
)
767 static int dcp_sha_noexport(struct ahash_request
*req
, void *out
)
772 static int dcp_sha_cra_init(struct crypto_tfm
*tfm
)
774 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
775 sizeof(struct dcp_sha_req_ctx
));
779 static void dcp_sha_cra_exit(struct crypto_tfm
*tfm
)
783 /* AES 128 ECB and AES 128 CBC */
784 static struct crypto_alg dcp_aes_algs
[] = {
786 .cra_name
= "ecb(aes)",
787 .cra_driver_name
= "ecb-aes-dcp",
790 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
792 CRYPTO_ALG_NEED_FALLBACK
,
793 .cra_init
= mxs_dcp_aes_fallback_init
,
794 .cra_exit
= mxs_dcp_aes_fallback_exit
,
795 .cra_blocksize
= AES_BLOCK_SIZE
,
796 .cra_ctxsize
= sizeof(struct dcp_async_ctx
),
797 .cra_type
= &crypto_ablkcipher_type
,
798 .cra_module
= THIS_MODULE
,
801 .min_keysize
= AES_MIN_KEY_SIZE
,
802 .max_keysize
= AES_MAX_KEY_SIZE
,
803 .setkey
= mxs_dcp_aes_setkey
,
804 .encrypt
= mxs_dcp_aes_ecb_encrypt
,
805 .decrypt
= mxs_dcp_aes_ecb_decrypt
809 .cra_name
= "cbc(aes)",
810 .cra_driver_name
= "cbc-aes-dcp",
813 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
815 CRYPTO_ALG_NEED_FALLBACK
,
816 .cra_init
= mxs_dcp_aes_fallback_init
,
817 .cra_exit
= mxs_dcp_aes_fallback_exit
,
818 .cra_blocksize
= AES_BLOCK_SIZE
,
819 .cra_ctxsize
= sizeof(struct dcp_async_ctx
),
820 .cra_type
= &crypto_ablkcipher_type
,
821 .cra_module
= THIS_MODULE
,
824 .min_keysize
= AES_MIN_KEY_SIZE
,
825 .max_keysize
= AES_MAX_KEY_SIZE
,
826 .setkey
= mxs_dcp_aes_setkey
,
827 .encrypt
= mxs_dcp_aes_cbc_encrypt
,
828 .decrypt
= mxs_dcp_aes_cbc_decrypt
,
829 .ivsize
= AES_BLOCK_SIZE
,
836 static struct ahash_alg dcp_sha1_alg
= {
837 .init
= dcp_sha_init
,
838 .update
= dcp_sha_update
,
839 .final
= dcp_sha_final
,
840 .finup
= dcp_sha_finup
,
841 .digest
= dcp_sha_digest
,
842 .import
= dcp_sha_noimport
,
843 .export
= dcp_sha_noexport
,
845 .digestsize
= SHA1_DIGEST_SIZE
,
848 .cra_driver_name
= "sha1-dcp",
851 .cra_flags
= CRYPTO_ALG_ASYNC
,
852 .cra_blocksize
= SHA1_BLOCK_SIZE
,
853 .cra_ctxsize
= sizeof(struct dcp_async_ctx
),
854 .cra_module
= THIS_MODULE
,
855 .cra_init
= dcp_sha_cra_init
,
856 .cra_exit
= dcp_sha_cra_exit
,
862 static struct ahash_alg dcp_sha256_alg
= {
863 .init
= dcp_sha_init
,
864 .update
= dcp_sha_update
,
865 .final
= dcp_sha_final
,
866 .finup
= dcp_sha_finup
,
867 .digest
= dcp_sha_digest
,
868 .import
= dcp_sha_noimport
,
869 .export
= dcp_sha_noexport
,
871 .digestsize
= SHA256_DIGEST_SIZE
,
873 .cra_name
= "sha256",
874 .cra_driver_name
= "sha256-dcp",
877 .cra_flags
= CRYPTO_ALG_ASYNC
,
878 .cra_blocksize
= SHA256_BLOCK_SIZE
,
879 .cra_ctxsize
= sizeof(struct dcp_async_ctx
),
880 .cra_module
= THIS_MODULE
,
881 .cra_init
= dcp_sha_cra_init
,
882 .cra_exit
= dcp_sha_cra_exit
,
887 static irqreturn_t
mxs_dcp_irq(int irq
, void *context
)
889 struct dcp
*sdcp
= context
;
893 stat
= readl(sdcp
->base
+ MXS_DCP_STAT
);
894 stat
&= MXS_DCP_STAT_IRQ_MASK
;
898 /* Clear the interrupts. */
899 writel(stat
, sdcp
->base
+ MXS_DCP_STAT_CLR
);
901 /* Complete the DMA requests that finished. */
902 for (i
= 0; i
< DCP_MAX_CHANS
; i
++)
904 complete(&sdcp
->completion
[i
]);
909 static int mxs_dcp_probe(struct platform_device
*pdev
)
911 struct device
*dev
= &pdev
->dev
;
912 struct dcp
*sdcp
= NULL
;
915 struct resource
*iores
;
916 int dcp_vmi_irq
, dcp_irq
;
919 dev_err(dev
, "Only one DCP instance allowed!\n");
923 iores
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
924 dcp_vmi_irq
= platform_get_irq(pdev
, 0);
925 if (dcp_vmi_irq
< 0) {
926 dev_err(dev
, "Failed to get IRQ: (%d)!\n", dcp_vmi_irq
);
930 dcp_irq
= platform_get_irq(pdev
, 1);
932 dev_err(dev
, "Failed to get IRQ: (%d)!\n", dcp_irq
);
936 sdcp
= devm_kzalloc(dev
, sizeof(*sdcp
), GFP_KERNEL
);
941 sdcp
->base
= devm_ioremap_resource(dev
, iores
);
942 if (IS_ERR(sdcp
->base
))
943 return PTR_ERR(sdcp
->base
);
946 ret
= devm_request_irq(dev
, dcp_vmi_irq
, mxs_dcp_irq
, 0,
947 "dcp-vmi-irq", sdcp
);
949 dev_err(dev
, "Failed to claim DCP VMI IRQ!\n");
953 ret
= devm_request_irq(dev
, dcp_irq
, mxs_dcp_irq
, 0,
956 dev_err(dev
, "Failed to claim DCP IRQ!\n");
960 /* Allocate coherent helper block. */
961 sdcp
->coh
= devm_kzalloc(dev
, sizeof(*sdcp
->coh
) + DCP_ALIGNMENT
,
966 /* Re-align the structure so it fits the DCP constraints. */
967 sdcp
->coh
= PTR_ALIGN(sdcp
->coh
, DCP_ALIGNMENT
);
969 /* Restart the DCP block. */
970 ret
= stmp_reset_block(sdcp
->base
);
974 /* Initialize control register. */
975 writel(MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES
|
976 MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING
| 0xf,
977 sdcp
->base
+ MXS_DCP_CTRL
);
979 /* Enable all DCP DMA channels. */
980 writel(MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK
,
981 sdcp
->base
+ MXS_DCP_CHANNELCTRL
);
984 * We do not enable context switching. Give the context buffer a
985 * pointer to an illegal address so if context switching is
986 * inadvertantly enabled, the DCP will return an error instead of
987 * trashing good memory. The DCP DMA cannot access ROM, so any ROM
990 writel(0xffff0000, sdcp
->base
+ MXS_DCP_CONTEXT
);
991 for (i
= 0; i
< DCP_MAX_CHANS
; i
++)
992 writel(0xffffffff, sdcp
->base
+ MXS_DCP_CH_N_STAT_CLR(i
));
993 writel(0xffffffff, sdcp
->base
+ MXS_DCP_STAT_CLR
);
997 platform_set_drvdata(pdev
, sdcp
);
999 for (i
= 0; i
< DCP_MAX_CHANS
; i
++) {
1000 mutex_init(&sdcp
->mutex
[i
]);
1001 init_completion(&sdcp
->completion
[i
]);
1002 crypto_init_queue(&sdcp
->queue
[i
], 50);
1005 /* Create the SHA and AES handler threads. */
1006 sdcp
->thread
[DCP_CHAN_HASH_SHA
] = kthread_run(dcp_chan_thread_sha
,
1007 NULL
, "mxs_dcp_chan/sha");
1008 if (IS_ERR(sdcp
->thread
[DCP_CHAN_HASH_SHA
])) {
1009 dev_err(dev
, "Error starting SHA thread!\n");
1010 return PTR_ERR(sdcp
->thread
[DCP_CHAN_HASH_SHA
]);
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
;
1021 /* Register the various crypto algorithms. */
1022 sdcp
->caps
= readl(sdcp
->base
+ MXS_DCP_CAPABILITY1
);
1024 if (sdcp
->caps
& MXS_DCP_CAPABILITY1_AES128
) {
1025 ret
= crypto_register_algs(dcp_aes_algs
,
1026 ARRAY_SIZE(dcp_aes_algs
));
1028 /* Failed to register algorithm. */
1029 dev_err(dev
, "Failed to register AES crypto!\n");
1030 goto err_destroy_aes_thread
;
1034 if (sdcp
->caps
& MXS_DCP_CAPABILITY1_SHA1
) {
1035 ret
= crypto_register_ahash(&dcp_sha1_alg
);
1037 dev_err(dev
, "Failed to register %s hash!\n",
1038 dcp_sha1_alg
.halg
.base
.cra_name
);
1039 goto err_unregister_aes
;
1043 if (sdcp
->caps
& MXS_DCP_CAPABILITY1_SHA256
) {
1044 ret
= crypto_register_ahash(&dcp_sha256_alg
);
1046 dev_err(dev
, "Failed to register %s hash!\n",
1047 dcp_sha256_alg
.halg
.base
.cra_name
);
1048 goto err_unregister_sha1
;
1054 err_unregister_sha1
:
1055 if (sdcp
->caps
& MXS_DCP_CAPABILITY1_SHA1
)
1056 crypto_unregister_ahash(&dcp_sha1_alg
);
1059 if (sdcp
->caps
& MXS_DCP_CAPABILITY1_AES128
)
1060 crypto_unregister_algs(dcp_aes_algs
, ARRAY_SIZE(dcp_aes_algs
));
1062 err_destroy_aes_thread
:
1063 kthread_stop(sdcp
->thread
[DCP_CHAN_CRYPTO
]);
1065 err_destroy_sha_thread
:
1066 kthread_stop(sdcp
->thread
[DCP_CHAN_HASH_SHA
]);
1070 static int mxs_dcp_remove(struct platform_device
*pdev
)
1072 struct dcp
*sdcp
= platform_get_drvdata(pdev
);
1074 if (sdcp
->caps
& MXS_DCP_CAPABILITY1_SHA256
)
1075 crypto_unregister_ahash(&dcp_sha256_alg
);
1077 if (sdcp
->caps
& MXS_DCP_CAPABILITY1_SHA1
)
1078 crypto_unregister_ahash(&dcp_sha1_alg
);
1080 if (sdcp
->caps
& MXS_DCP_CAPABILITY1_AES128
)
1081 crypto_unregister_algs(dcp_aes_algs
, ARRAY_SIZE(dcp_aes_algs
));
1083 kthread_stop(sdcp
->thread
[DCP_CHAN_HASH_SHA
]);
1084 kthread_stop(sdcp
->thread
[DCP_CHAN_CRYPTO
]);
1086 platform_set_drvdata(pdev
, NULL
);
1093 static const struct of_device_id mxs_dcp_dt_ids
[] = {
1094 { .compatible
= "fsl,imx23-dcp", .data
= NULL
, },
1095 { .compatible
= "fsl,imx28-dcp", .data
= NULL
, },
1099 MODULE_DEVICE_TABLE(of
, mxs_dcp_dt_ids
);
1101 static struct platform_driver mxs_dcp_driver
= {
1102 .probe
= mxs_dcp_probe
,
1103 .remove
= mxs_dcp_remove
,
1106 .of_match_table
= mxs_dcp_dt_ids
,
1110 module_platform_driver(mxs_dcp_driver
);
1112 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
1113 MODULE_DESCRIPTION("Freescale MXS DCP Driver");
1114 MODULE_LICENSE("GPL");
1115 MODULE_ALIAS("platform:mxs-dcp");