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/crypto.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/kthread.h>
20 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/stmp_device.h>
25 #include <crypto/aes.h>
26 #include <crypto/sha.h>
27 #include <crypto/internal/hash.h>
29 #define DCP_MAX_CHANS 4
30 #define DCP_BUF_SZ PAGE_SIZE
32 /* DCP DMA descriptor. */
34 uint32_t next_cmd_addr
;
44 /* Coherent aligned block for bounce buffering. */
45 struct dcp_coherent_block
{
46 uint8_t aes_in_buf
[DCP_BUF_SZ
];
47 uint8_t aes_out_buf
[DCP_BUF_SZ
];
48 uint8_t sha_in_buf
[DCP_BUF_SZ
];
50 uint8_t aes_key
[2 * AES_KEYSIZE_128
];
51 uint8_t sha_digest
[SHA256_DIGEST_SIZE
];
53 struct dcp_dma_desc desc
[DCP_MAX_CHANS
];
62 struct dcp_coherent_block
*coh
;
64 struct completion completion
[DCP_MAX_CHANS
];
65 struct mutex mutex
[DCP_MAX_CHANS
];
66 struct task_struct
*thread
[DCP_MAX_CHANS
];
67 struct crypto_queue queue
[DCP_MAX_CHANS
];
71 DCP_CHAN_HASH_SHA
= 0,
75 struct dcp_async_ctx
{
80 /* SHA Hash-specific context */
85 /* Crypto-specific context */
88 struct crypto_ablkcipher
*fallback
;
90 uint8_t key
[AES_KEYSIZE_128
];
93 struct dcp_sha_req_ctx
{
99 * There can even be only one instance of the MXS DCP due to the
100 * design of Linux Crypto API.
102 static struct dcp
*global_sdcp
;
103 static DEFINE_MUTEX(global_mutex
);
105 /* DCP register layout. */
106 #define MXS_DCP_CTRL 0x00
107 #define MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES (1 << 23)
108 #define MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING (1 << 22)
110 #define MXS_DCP_STAT 0x10
111 #define MXS_DCP_STAT_CLR 0x18
112 #define MXS_DCP_STAT_IRQ_MASK 0xf
114 #define MXS_DCP_CHANNELCTRL 0x20
115 #define MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK 0xff
117 #define MXS_DCP_CAPABILITY1 0x40
118 #define MXS_DCP_CAPABILITY1_SHA256 (4 << 16)
119 #define MXS_DCP_CAPABILITY1_SHA1 (1 << 16)
120 #define MXS_DCP_CAPABILITY1_AES128 (1 << 0)
122 #define MXS_DCP_CONTEXT 0x50
124 #define MXS_DCP_CH_N_CMDPTR(n) (0x100 + ((n) * 0x40))
126 #define MXS_DCP_CH_N_SEMA(n) (0x110 + ((n) * 0x40))
128 #define MXS_DCP_CH_N_STAT(n) (0x120 + ((n) * 0x40))
129 #define MXS_DCP_CH_N_STAT_CLR(n) (0x128 + ((n) * 0x40))
131 /* DMA descriptor bits. */
132 #define MXS_DCP_CONTROL0_HASH_TERM (1 << 13)
133 #define MXS_DCP_CONTROL0_HASH_INIT (1 << 12)
134 #define MXS_DCP_CONTROL0_PAYLOAD_KEY (1 << 11)
135 #define MXS_DCP_CONTROL0_CIPHER_ENCRYPT (1 << 8)
136 #define MXS_DCP_CONTROL0_CIPHER_INIT (1 << 9)
137 #define MXS_DCP_CONTROL0_ENABLE_HASH (1 << 6)
138 #define MXS_DCP_CONTROL0_ENABLE_CIPHER (1 << 5)
139 #define MXS_DCP_CONTROL0_DECR_SEMAPHORE (1 << 1)
140 #define MXS_DCP_CONTROL0_INTERRUPT (1 << 0)
142 #define MXS_DCP_CONTROL1_HASH_SELECT_SHA256 (2 << 16)
143 #define MXS_DCP_CONTROL1_HASH_SELECT_SHA1 (0 << 16)
144 #define MXS_DCP_CONTROL1_CIPHER_MODE_CBC (1 << 4)
145 #define MXS_DCP_CONTROL1_CIPHER_MODE_ECB (0 << 4)
146 #define MXS_DCP_CONTROL1_CIPHER_SELECT_AES128 (0 << 0)
148 static int mxs_dcp_start_dma(struct dcp_async_ctx
*actx
)
150 struct dcp
*sdcp
= global_sdcp
;
151 const int chan
= actx
->chan
;
154 struct dcp_dma_desc
*desc
= &sdcp
->coh
->desc
[actx
->chan
];
156 dma_addr_t desc_phys
= dma_map_single(sdcp
->dev
, desc
, sizeof(*desc
),
159 reinit_completion(&sdcp
->completion
[chan
]);
161 /* Clear status register. */
162 writel(0xffffffff, sdcp
->base
+ MXS_DCP_CH_N_STAT_CLR(chan
));
164 /* Load the DMA descriptor. */
165 writel(desc_phys
, sdcp
->base
+ MXS_DCP_CH_N_CMDPTR(chan
));
167 /* Increment the semaphore to start the DMA transfer. */
168 writel(1, sdcp
->base
+ MXS_DCP_CH_N_SEMA(chan
));
170 ret
= wait_for_completion_timeout(&sdcp
->completion
[chan
],
171 msecs_to_jiffies(1000));
173 dev_err(sdcp
->dev
, "Channel %i timeout (DCP_STAT=0x%08x)\n",
174 chan
, readl(sdcp
->base
+ MXS_DCP_STAT
));
178 stat
= readl(sdcp
->base
+ MXS_DCP_CH_N_STAT(chan
));
180 dev_err(sdcp
->dev
, "Channel %i error (CH_STAT=0x%08x)\n",
185 dma_unmap_single(sdcp
->dev
, desc_phys
, sizeof(*desc
), DMA_TO_DEVICE
);
191 * Encryption (AES128)
193 static int mxs_dcp_run_aes(struct dcp_async_ctx
*actx
, int init
)
195 struct dcp
*sdcp
= global_sdcp
;
196 struct dcp_dma_desc
*desc
= &sdcp
->coh
->desc
[actx
->chan
];
199 dma_addr_t key_phys
= dma_map_single(sdcp
->dev
, sdcp
->coh
->aes_key
,
202 dma_addr_t src_phys
= dma_map_single(sdcp
->dev
, sdcp
->coh
->aes_in_buf
,
203 DCP_BUF_SZ
, DMA_TO_DEVICE
);
204 dma_addr_t dst_phys
= dma_map_single(sdcp
->dev
, sdcp
->coh
->aes_out_buf
,
205 DCP_BUF_SZ
, DMA_FROM_DEVICE
);
207 /* Fill in the DMA descriptor. */
208 desc
->control0
= MXS_DCP_CONTROL0_DECR_SEMAPHORE
|
209 MXS_DCP_CONTROL0_INTERRUPT
|
210 MXS_DCP_CONTROL0_ENABLE_CIPHER
;
212 /* Payload contains the key. */
213 desc
->control0
|= MXS_DCP_CONTROL0_PAYLOAD_KEY
;
216 desc
->control0
|= MXS_DCP_CONTROL0_CIPHER_ENCRYPT
;
218 desc
->control0
|= MXS_DCP_CONTROL0_CIPHER_INIT
;
220 desc
->control1
= MXS_DCP_CONTROL1_CIPHER_SELECT_AES128
;
223 desc
->control1
|= MXS_DCP_CONTROL1_CIPHER_MODE_ECB
;
225 desc
->control1
|= MXS_DCP_CONTROL1_CIPHER_MODE_CBC
;
227 desc
->next_cmd_addr
= 0;
228 desc
->source
= src_phys
;
229 desc
->destination
= dst_phys
;
230 desc
->size
= actx
->fill
;
231 desc
->payload
= key_phys
;
234 ret
= mxs_dcp_start_dma(actx
);
236 dma_unmap_single(sdcp
->dev
, key_phys
, 2 * AES_KEYSIZE_128
,
238 dma_unmap_single(sdcp
->dev
, src_phys
, DCP_BUF_SZ
, DMA_TO_DEVICE
);
239 dma_unmap_single(sdcp
->dev
, dst_phys
, DCP_BUF_SZ
, DMA_FROM_DEVICE
);
244 static int mxs_dcp_aes_block_crypt(struct crypto_async_request
*arq
)
246 struct dcp
*sdcp
= global_sdcp
;
248 struct ablkcipher_request
*req
= ablkcipher_request_cast(arq
);
249 struct dcp_async_ctx
*actx
= crypto_tfm_ctx(arq
->tfm
);
251 struct scatterlist
*dst
= req
->dst
;
252 struct scatterlist
*src
= req
->src
;
253 const int nents
= sg_nents(req
->src
);
255 const int out_off
= DCP_BUF_SZ
;
256 uint8_t *in_buf
= sdcp
->coh
->aes_in_buf
;
257 uint8_t *out_buf
= sdcp
->coh
->aes_out_buf
;
259 uint8_t *out_tmp
, *src_buf
, *dst_buf
= NULL
;
260 uint32_t dst_off
= 0;
262 uint8_t *key
= sdcp
->coh
->aes_key
;
266 unsigned int i
, len
, clen
, rem
= 0;
271 /* Copy the key from the temporary location. */
272 memcpy(key
, actx
->key
, actx
->key_len
);
275 /* Copy the CBC IV just past the key. */
276 memcpy(key
+ AES_KEYSIZE_128
, req
->info
, AES_KEYSIZE_128
);
277 /* CBC needs the INIT set. */
280 memset(key
+ AES_KEYSIZE_128
, 0, AES_KEYSIZE_128
);
283 for_each_sg(req
->src
, src
, nents
, i
) {
284 src_buf
= sg_virt(src
);
285 len
= sg_dma_len(src
);
288 if (actx
->fill
+ len
> out_off
)
289 clen
= out_off
- actx
->fill
;
293 memcpy(in_buf
+ actx
->fill
, src_buf
, clen
);
299 * If we filled the buffer or this is the last SG,
302 if (actx
->fill
== out_off
|| sg_is_last(src
)) {
303 ret
= mxs_dcp_run_aes(actx
, init
);
309 while (dst
&& actx
->fill
) {
311 dst_buf
= sg_virt(dst
);
314 rem
= min(sg_dma_len(dst
) - dst_off
,
317 memcpy(dst_buf
+ dst_off
, out_tmp
, rem
);
322 if (dst_off
== sg_dma_len(dst
)) {
336 static int dcp_chan_thread_aes(void *data
)
338 struct dcp
*sdcp
= global_sdcp
;
339 const int chan
= DCP_CHAN_CRYPTO
;
341 struct crypto_async_request
*backlog
;
342 struct crypto_async_request
*arq
;
347 __set_current_state(TASK_INTERRUPTIBLE
);
349 mutex_lock(&sdcp
->mutex
[chan
]);
350 backlog
= crypto_get_backlog(&sdcp
->queue
[chan
]);
351 arq
= crypto_dequeue_request(&sdcp
->queue
[chan
]);
352 mutex_unlock(&sdcp
->mutex
[chan
]);
355 backlog
->complete(backlog
, -EINPROGRESS
);
358 ret
= mxs_dcp_aes_block_crypt(arq
);
359 arq
->complete(arq
, ret
);
364 } while (!kthread_should_stop());
369 static int mxs_dcp_block_fallback(struct ablkcipher_request
*req
, int enc
)
371 struct crypto_tfm
*tfm
=
372 crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req
));
373 struct dcp_async_ctx
*ctx
= crypto_ablkcipher_ctx(
374 crypto_ablkcipher_reqtfm(req
));
377 ablkcipher_request_set_tfm(req
, ctx
->fallback
);
380 ret
= crypto_ablkcipher_encrypt(req
);
382 ret
= crypto_ablkcipher_decrypt(req
);
384 ablkcipher_request_set_tfm(req
, __crypto_ablkcipher_cast(tfm
));
389 static int mxs_dcp_aes_enqueue(struct ablkcipher_request
*req
, int enc
, int ecb
)
391 struct dcp
*sdcp
= global_sdcp
;
392 struct crypto_async_request
*arq
= &req
->base
;
393 struct dcp_async_ctx
*actx
= crypto_tfm_ctx(arq
->tfm
);
396 if (unlikely(actx
->key_len
!= AES_KEYSIZE_128
))
397 return mxs_dcp_block_fallback(req
, enc
);
401 actx
->chan
= DCP_CHAN_CRYPTO
;
403 mutex_lock(&sdcp
->mutex
[actx
->chan
]);
404 ret
= crypto_enqueue_request(&sdcp
->queue
[actx
->chan
], &req
->base
);
405 mutex_unlock(&sdcp
->mutex
[actx
->chan
]);
407 wake_up_process(sdcp
->thread
[actx
->chan
]);
412 static int mxs_dcp_aes_ecb_decrypt(struct ablkcipher_request
*req
)
414 return mxs_dcp_aes_enqueue(req
, 0, 1);
417 static int mxs_dcp_aes_ecb_encrypt(struct ablkcipher_request
*req
)
419 return mxs_dcp_aes_enqueue(req
, 1, 1);
422 static int mxs_dcp_aes_cbc_decrypt(struct ablkcipher_request
*req
)
424 return mxs_dcp_aes_enqueue(req
, 0, 0);
427 static int mxs_dcp_aes_cbc_encrypt(struct ablkcipher_request
*req
)
429 return mxs_dcp_aes_enqueue(req
, 1, 0);
432 static int mxs_dcp_aes_setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
435 struct dcp_async_ctx
*actx
= crypto_ablkcipher_ctx(tfm
);
439 * AES 128 is supposed by the hardware, store key into temporary
440 * buffer and exit. We must use the temporary buffer here, since
441 * there can still be an operation in progress.
444 if (len
== AES_KEYSIZE_128
) {
445 memcpy(actx
->key
, key
, len
);
449 /* Check if the key size is supported by kernel at all. */
450 if (len
!= AES_KEYSIZE_192
&& len
!= AES_KEYSIZE_256
) {
451 tfm
->base
.crt_flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
456 * If the requested AES key size is not supported by the hardware,
457 * but is supported by in-kernel software implementation, we use
460 actx
->fallback
->base
.crt_flags
&= ~CRYPTO_TFM_REQ_MASK
;
461 actx
->fallback
->base
.crt_flags
|=
462 tfm
->base
.crt_flags
& CRYPTO_TFM_REQ_MASK
;
464 ret
= crypto_ablkcipher_setkey(actx
->fallback
, key
, len
);
468 tfm
->base
.crt_flags
&= ~CRYPTO_TFM_RES_MASK
;
469 tfm
->base
.crt_flags
|=
470 actx
->fallback
->base
.crt_flags
& CRYPTO_TFM_RES_MASK
;
475 static int mxs_dcp_aes_fallback_init(struct crypto_tfm
*tfm
)
477 const char *name
= tfm
->__crt_alg
->cra_name
;
478 const uint32_t flags
= CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
;
479 struct dcp_async_ctx
*actx
= crypto_tfm_ctx(tfm
);
480 struct crypto_ablkcipher
*blk
;
482 blk
= crypto_alloc_ablkcipher(name
, 0, flags
);
486 actx
->fallback
= blk
;
487 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct dcp_async_ctx
);
491 static void mxs_dcp_aes_fallback_exit(struct crypto_tfm
*tfm
)
493 struct dcp_async_ctx
*actx
= crypto_tfm_ctx(tfm
);
495 crypto_free_ablkcipher(actx
->fallback
);
496 actx
->fallback
= NULL
;
500 * Hashing (SHA1/SHA256)
502 static int mxs_dcp_run_sha(struct ahash_request
*req
)
504 struct dcp
*sdcp
= global_sdcp
;
507 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
508 struct dcp_async_ctx
*actx
= crypto_ahash_ctx(tfm
);
509 struct dcp_sha_req_ctx
*rctx
= ahash_request_ctx(req
);
511 struct dcp_dma_desc
*desc
= &sdcp
->coh
->desc
[actx
->chan
];
512 dma_addr_t digest_phys
= dma_map_single(sdcp
->dev
,
513 sdcp
->coh
->sha_digest
,
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 desc
->control0
|= MXS_DCP_CONTROL0_HASH_TERM
;
538 desc
->payload
= digest_phys
;
541 ret
= mxs_dcp_start_dma(actx
);
543 dma_unmap_single(sdcp
->dev
, digest_phys
, SHA256_DIGEST_SIZE
,
545 dma_unmap_single(sdcp
->dev
, buf_phys
, DCP_BUF_SZ
, DMA_TO_DEVICE
);
550 static int dcp_sha_req_to_buf(struct crypto_async_request
*arq
)
552 struct dcp
*sdcp
= global_sdcp
;
554 struct ahash_request
*req
= ahash_request_cast(arq
);
555 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
556 struct dcp_async_ctx
*actx
= crypto_ahash_ctx(tfm
);
557 struct dcp_sha_req_ctx
*rctx
= ahash_request_ctx(req
);
558 struct hash_alg_common
*halg
= crypto_hash_alg_common(tfm
);
559 const int nents
= sg_nents(req
->src
);
561 uint8_t *digest
= sdcp
->coh
->sha_digest
;
562 uint8_t *in_buf
= sdcp
->coh
->sha_in_buf
;
566 struct scatterlist
*src
;
568 unsigned int i
, len
, clen
;
571 int fin
= rctx
->fini
;
575 for_each_sg(req
->src
, src
, nents
, i
) {
576 src_buf
= sg_virt(src
);
577 len
= sg_dma_len(src
);
580 if (actx
->fill
+ len
> DCP_BUF_SZ
)
581 clen
= DCP_BUF_SZ
- actx
->fill
;
585 memcpy(in_buf
+ actx
->fill
, src_buf
, clen
);
591 * If we filled the buffer and still have some
592 * more data, submit the buffer.
594 if (len
&& actx
->fill
== DCP_BUF_SZ
) {
595 ret
= mxs_dcp_run_sha(req
);
607 /* Submit whatever is left. */
608 ret
= mxs_dcp_run_sha(req
);
609 if (ret
|| !req
->result
)
613 /* For some reason, the result is flipped. */
614 for (i
= 0; i
< halg
->digestsize
; i
++)
615 req
->result
[i
] = digest
[halg
->digestsize
- i
- 1];
621 static int dcp_chan_thread_sha(void *data
)
623 struct dcp
*sdcp
= global_sdcp
;
624 const int chan
= DCP_CHAN_HASH_SHA
;
626 struct crypto_async_request
*backlog
;
627 struct crypto_async_request
*arq
;
629 struct dcp_sha_req_ctx
*rctx
;
631 struct ahash_request
*req
;
635 __set_current_state(TASK_INTERRUPTIBLE
);
637 mutex_lock(&sdcp
->mutex
[chan
]);
638 backlog
= crypto_get_backlog(&sdcp
->queue
[chan
]);
639 arq
= crypto_dequeue_request(&sdcp
->queue
[chan
]);
640 mutex_unlock(&sdcp
->mutex
[chan
]);
643 backlog
->complete(backlog
, -EINPROGRESS
);
646 req
= ahash_request_cast(arq
);
647 rctx
= ahash_request_ctx(req
);
649 ret
= dcp_sha_req_to_buf(arq
);
651 arq
->complete(arq
, ret
);
657 } while (!kthread_should_stop());
662 static int dcp_sha_init(struct ahash_request
*req
)
664 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
665 struct dcp_async_ctx
*actx
= crypto_ahash_ctx(tfm
);
667 struct hash_alg_common
*halg
= crypto_hash_alg_common(tfm
);
670 * Start hashing session. The code below only inits the
671 * hashing session context, nothing more.
673 memset(actx
, 0, sizeof(*actx
));
675 if (strcmp(halg
->base
.cra_name
, "sha1") == 0)
676 actx
->alg
= MXS_DCP_CONTROL1_HASH_SELECT_SHA1
;
678 actx
->alg
= MXS_DCP_CONTROL1_HASH_SELECT_SHA256
;
682 actx
->chan
= DCP_CHAN_HASH_SHA
;
684 mutex_init(&actx
->mutex
);
689 static int dcp_sha_update_fx(struct ahash_request
*req
, int fini
)
691 struct dcp
*sdcp
= global_sdcp
;
693 struct dcp_sha_req_ctx
*rctx
= ahash_request_ctx(req
);
694 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
695 struct dcp_async_ctx
*actx
= crypto_ahash_ctx(tfm
);
700 * Ignore requests that have no data in them and are not
701 * the trailing requests in the stream of requests.
703 if (!req
->nbytes
&& !fini
)
706 mutex_lock(&actx
->mutex
);
715 mutex_lock(&sdcp
->mutex
[actx
->chan
]);
716 ret
= crypto_enqueue_request(&sdcp
->queue
[actx
->chan
], &req
->base
);
717 mutex_unlock(&sdcp
->mutex
[actx
->chan
]);
719 wake_up_process(sdcp
->thread
[actx
->chan
]);
720 mutex_unlock(&actx
->mutex
);
725 static int dcp_sha_update(struct ahash_request
*req
)
727 return dcp_sha_update_fx(req
, 0);
730 static int dcp_sha_final(struct ahash_request
*req
)
732 ahash_request_set_crypt(req
, NULL
, req
->result
, 0);
734 return dcp_sha_update_fx(req
, 1);
737 static int dcp_sha_finup(struct ahash_request
*req
)
739 return dcp_sha_update_fx(req
, 1);
742 static int dcp_sha_digest(struct ahash_request
*req
)
746 ret
= dcp_sha_init(req
);
750 return dcp_sha_finup(req
);
753 static int dcp_sha_cra_init(struct crypto_tfm
*tfm
)
755 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
756 sizeof(struct dcp_sha_req_ctx
));
760 static void dcp_sha_cra_exit(struct crypto_tfm
*tfm
)
764 /* AES 128 ECB and AES 128 CBC */
765 static struct crypto_alg dcp_aes_algs
[] = {
767 .cra_name
= "ecb(aes)",
768 .cra_driver_name
= "ecb-aes-dcp",
771 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
773 CRYPTO_ALG_NEED_FALLBACK
,
774 .cra_init
= mxs_dcp_aes_fallback_init
,
775 .cra_exit
= mxs_dcp_aes_fallback_exit
,
776 .cra_blocksize
= AES_BLOCK_SIZE
,
777 .cra_ctxsize
= sizeof(struct dcp_async_ctx
),
778 .cra_type
= &crypto_ablkcipher_type
,
779 .cra_module
= THIS_MODULE
,
782 .min_keysize
= AES_MIN_KEY_SIZE
,
783 .max_keysize
= AES_MAX_KEY_SIZE
,
784 .setkey
= mxs_dcp_aes_setkey
,
785 .encrypt
= mxs_dcp_aes_ecb_encrypt
,
786 .decrypt
= mxs_dcp_aes_ecb_decrypt
790 .cra_name
= "cbc(aes)",
791 .cra_driver_name
= "cbc-aes-dcp",
794 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
796 CRYPTO_ALG_NEED_FALLBACK
,
797 .cra_init
= mxs_dcp_aes_fallback_init
,
798 .cra_exit
= mxs_dcp_aes_fallback_exit
,
799 .cra_blocksize
= AES_BLOCK_SIZE
,
800 .cra_ctxsize
= sizeof(struct dcp_async_ctx
),
801 .cra_type
= &crypto_ablkcipher_type
,
802 .cra_module
= THIS_MODULE
,
805 .min_keysize
= AES_MIN_KEY_SIZE
,
806 .max_keysize
= AES_MAX_KEY_SIZE
,
807 .setkey
= mxs_dcp_aes_setkey
,
808 .encrypt
= mxs_dcp_aes_cbc_encrypt
,
809 .decrypt
= mxs_dcp_aes_cbc_decrypt
,
810 .ivsize
= AES_BLOCK_SIZE
,
817 static struct ahash_alg dcp_sha1_alg
= {
818 .init
= dcp_sha_init
,
819 .update
= dcp_sha_update
,
820 .final
= dcp_sha_final
,
821 .finup
= dcp_sha_finup
,
822 .digest
= dcp_sha_digest
,
824 .digestsize
= SHA1_DIGEST_SIZE
,
827 .cra_driver_name
= "sha1-dcp",
830 .cra_flags
= CRYPTO_ALG_ASYNC
,
831 .cra_blocksize
= SHA1_BLOCK_SIZE
,
832 .cra_ctxsize
= sizeof(struct dcp_async_ctx
),
833 .cra_module
= THIS_MODULE
,
834 .cra_init
= dcp_sha_cra_init
,
835 .cra_exit
= dcp_sha_cra_exit
,
841 static struct ahash_alg dcp_sha256_alg
= {
842 .init
= dcp_sha_init
,
843 .update
= dcp_sha_update
,
844 .final
= dcp_sha_final
,
845 .finup
= dcp_sha_finup
,
846 .digest
= dcp_sha_digest
,
848 .digestsize
= SHA256_DIGEST_SIZE
,
850 .cra_name
= "sha256",
851 .cra_driver_name
= "sha256-dcp",
854 .cra_flags
= CRYPTO_ALG_ASYNC
,
855 .cra_blocksize
= SHA256_BLOCK_SIZE
,
856 .cra_ctxsize
= sizeof(struct dcp_async_ctx
),
857 .cra_module
= THIS_MODULE
,
858 .cra_init
= dcp_sha_cra_init
,
859 .cra_exit
= dcp_sha_cra_exit
,
864 static irqreturn_t
mxs_dcp_irq(int irq
, void *context
)
866 struct dcp
*sdcp
= context
;
870 stat
= readl(sdcp
->base
+ MXS_DCP_STAT
);
871 stat
&= MXS_DCP_STAT_IRQ_MASK
;
875 /* Clear the interrupts. */
876 writel(stat
, sdcp
->base
+ MXS_DCP_STAT_CLR
);
878 /* Complete the DMA requests that finished. */
879 for (i
= 0; i
< DCP_MAX_CHANS
; i
++)
881 complete(&sdcp
->completion
[i
]);
886 static int mxs_dcp_probe(struct platform_device
*pdev
)
888 struct device
*dev
= &pdev
->dev
;
889 struct dcp
*sdcp
= NULL
;
892 struct resource
*iores
;
893 int dcp_vmi_irq
, dcp_irq
;
895 mutex_lock(&global_mutex
);
897 dev_err(dev
, "Only one DCP instance allowed!\n");
902 iores
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
903 dcp_vmi_irq
= platform_get_irq(pdev
, 0);
904 dcp_irq
= platform_get_irq(pdev
, 1);
905 if (dcp_vmi_irq
< 0 || dcp_irq
< 0) {
910 sdcp
= devm_kzalloc(dev
, sizeof(*sdcp
), GFP_KERNEL
);
917 sdcp
->base
= devm_ioremap_resource(dev
, iores
);
918 if (IS_ERR(sdcp
->base
)) {
919 ret
= PTR_ERR(sdcp
->base
);
923 ret
= devm_request_irq(dev
, dcp_vmi_irq
, mxs_dcp_irq
, 0,
924 "dcp-vmi-irq", sdcp
);
926 dev_err(dev
, "Failed to claim DCP VMI IRQ!\n");
930 ret
= devm_request_irq(dev
, dcp_irq
, mxs_dcp_irq
, 0,
933 dev_err(dev
, "Failed to claim DCP IRQ!\n");
937 /* Allocate coherent helper block. */
938 sdcp
->coh
= kzalloc(sizeof(struct dcp_coherent_block
), GFP_KERNEL
);
940 dev_err(dev
, "Error allocating coherent block\n");
945 /* Restart the DCP block. */
946 stmp_reset_block(sdcp
->base
);
948 /* Initialize control register. */
949 writel(MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES
|
950 MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING
| 0xf,
951 sdcp
->base
+ MXS_DCP_CTRL
);
953 /* Enable all DCP DMA channels. */
954 writel(MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK
,
955 sdcp
->base
+ MXS_DCP_CHANNELCTRL
);
958 * We do not enable context switching. Give the context buffer a
959 * pointer to an illegal address so if context switching is
960 * inadvertantly enabled, the DCP will return an error instead of
961 * trashing good memory. The DCP DMA cannot access ROM, so any ROM
964 writel(0xffff0000, sdcp
->base
+ MXS_DCP_CONTEXT
);
965 for (i
= 0; i
< DCP_MAX_CHANS
; i
++)
966 writel(0xffffffff, sdcp
->base
+ MXS_DCP_CH_N_STAT_CLR(i
));
967 writel(0xffffffff, sdcp
->base
+ MXS_DCP_STAT_CLR
);
971 platform_set_drvdata(pdev
, sdcp
);
973 for (i
= 0; i
< DCP_MAX_CHANS
; i
++) {
974 mutex_init(&sdcp
->mutex
[i
]);
975 init_completion(&sdcp
->completion
[i
]);
976 crypto_init_queue(&sdcp
->queue
[i
], 50);
979 /* Create the SHA and AES handler threads. */
980 sdcp
->thread
[DCP_CHAN_HASH_SHA
] = kthread_run(dcp_chan_thread_sha
,
981 NULL
, "mxs_dcp_chan/sha");
982 if (IS_ERR(sdcp
->thread
[DCP_CHAN_HASH_SHA
])) {
983 dev_err(dev
, "Error starting SHA thread!\n");
984 ret
= PTR_ERR(sdcp
->thread
[DCP_CHAN_HASH_SHA
]);
985 goto err_free_coherent
;
988 sdcp
->thread
[DCP_CHAN_CRYPTO
] = kthread_run(dcp_chan_thread_aes
,
989 NULL
, "mxs_dcp_chan/aes");
990 if (IS_ERR(sdcp
->thread
[DCP_CHAN_CRYPTO
])) {
991 dev_err(dev
, "Error starting SHA thread!\n");
992 ret
= PTR_ERR(sdcp
->thread
[DCP_CHAN_CRYPTO
]);
993 goto err_destroy_sha_thread
;
996 /* Register the various crypto algorithms. */
997 sdcp
->caps
= readl(sdcp
->base
+ MXS_DCP_CAPABILITY1
);
999 if (sdcp
->caps
& MXS_DCP_CAPABILITY1_AES128
) {
1000 ret
= crypto_register_algs(dcp_aes_algs
,
1001 ARRAY_SIZE(dcp_aes_algs
));
1003 /* Failed to register algorithm. */
1004 dev_err(dev
, "Failed to register AES crypto!\n");
1005 goto err_destroy_aes_thread
;
1009 if (sdcp
->caps
& MXS_DCP_CAPABILITY1_SHA1
) {
1010 ret
= crypto_register_ahash(&dcp_sha1_alg
);
1012 dev_err(dev
, "Failed to register %s hash!\n",
1013 dcp_sha1_alg
.halg
.base
.cra_name
);
1014 goto err_unregister_aes
;
1018 if (sdcp
->caps
& MXS_DCP_CAPABILITY1_SHA256
) {
1019 ret
= crypto_register_ahash(&dcp_sha256_alg
);
1021 dev_err(dev
, "Failed to register %s hash!\n",
1022 dcp_sha256_alg
.halg
.base
.cra_name
);
1023 goto err_unregister_sha1
;
1029 err_unregister_sha1
:
1030 if (sdcp
->caps
& MXS_DCP_CAPABILITY1_SHA1
)
1031 crypto_unregister_ahash(&dcp_sha1_alg
);
1034 if (sdcp
->caps
& MXS_DCP_CAPABILITY1_AES128
)
1035 crypto_unregister_algs(dcp_aes_algs
, ARRAY_SIZE(dcp_aes_algs
));
1037 err_destroy_aes_thread
:
1038 kthread_stop(sdcp
->thread
[DCP_CHAN_CRYPTO
]);
1040 err_destroy_sha_thread
:
1041 kthread_stop(sdcp
->thread
[DCP_CHAN_HASH_SHA
]);
1046 mutex_unlock(&global_mutex
);
1050 static int mxs_dcp_remove(struct platform_device
*pdev
)
1052 struct dcp
*sdcp
= platform_get_drvdata(pdev
);
1056 if (sdcp
->caps
& MXS_DCP_CAPABILITY1_SHA256
)
1057 crypto_unregister_ahash(&dcp_sha256_alg
);
1059 if (sdcp
->caps
& MXS_DCP_CAPABILITY1_SHA1
)
1060 crypto_unregister_ahash(&dcp_sha1_alg
);
1062 if (sdcp
->caps
& MXS_DCP_CAPABILITY1_AES128
)
1063 crypto_unregister_algs(dcp_aes_algs
, ARRAY_SIZE(dcp_aes_algs
));
1065 kthread_stop(sdcp
->thread
[DCP_CHAN_HASH_SHA
]);
1066 kthread_stop(sdcp
->thread
[DCP_CHAN_CRYPTO
]);
1068 platform_set_drvdata(pdev
, NULL
);
1070 mutex_lock(&global_mutex
);
1072 mutex_unlock(&global_mutex
);
1077 static const struct of_device_id mxs_dcp_dt_ids
[] = {
1078 { .compatible
= "fsl,imx23-dcp", .data
= NULL
, },
1079 { .compatible
= "fsl,imx28-dcp", .data
= NULL
, },
1083 MODULE_DEVICE_TABLE(of
, mxs_dcp_dt_ids
);
1085 static struct platform_driver mxs_dcp_driver
= {
1086 .probe
= mxs_dcp_probe
,
1087 .remove
= mxs_dcp_remove
,
1090 .owner
= THIS_MODULE
,
1091 .of_match_table
= mxs_dcp_dt_ids
,
1095 module_platform_driver(mxs_dcp_driver
);
1097 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
1098 MODULE_DESCRIPTION("Freescale MXS DCP Driver");
1099 MODULE_LICENSE("GPL");
1100 MODULE_ALIAS("platform:mxs-dcp");