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>
28 #include <crypto/scatterwalk.h>
30 #define DCP_MAX_CHANS 4
31 #define DCP_BUF_SZ PAGE_SIZE
32 #define DCP_SHA_PAY_SZ 64
34 #define DCP_ALIGNMENT 64
37 * Null hashes to align with hw behavior on imx6sl and ull
38 * these are flipped for consistency with hw output
40 static const uint8_t sha1_null_hash[] =
41 "\x09\x07\xd8\xaf\x90\x18\x60\x95\xef\xbf"
42 "\x55\x32\x0d\x4b\x6b\x5e\xee\xa3\x39\xda";
44 static const uint8_t sha256_null_hash[] =
45 "\x55\xb8\x52\x78\x1b\x99\x95\xa4"
46 "\x4c\x93\x9b\x64\xe4\x41\xae\x27"
47 "\x24\xb9\x6f\x99\xc8\xf4\xfb\x9a"
48 "\x14\x1c\xfc\x98\x42\xc4\xb0\xe3";
50 /* DCP DMA descriptor. */
52 uint32_t next_cmd_addr;
62 /* Coherent aligned block for bounce buffering. */
63 struct dcp_coherent_block {
64 uint8_t aes_in_buf[DCP_BUF_SZ];
65 uint8_t aes_out_buf[DCP_BUF_SZ];
66 uint8_t sha_in_buf[DCP_BUF_SZ];
67 uint8_t sha_out_buf[DCP_SHA_PAY_SZ];
69 uint8_t aes_key[2 * AES_KEYSIZE_128];
71 struct dcp_dma_desc desc[DCP_MAX_CHANS];
80 struct dcp_coherent_block *coh;
82 struct completion completion[DCP_MAX_CHANS];
83 spinlock_t lock[DCP_MAX_CHANS];
84 struct task_struct *thread[DCP_MAX_CHANS];
85 struct crypto_queue queue[DCP_MAX_CHANS];
89 DCP_CHAN_HASH_SHA = 0,
93 struct dcp_async_ctx {
98 /* SHA Hash-specific context */
103 /* Crypto-specific context */
104 struct crypto_ablkcipher *fallback;
105 unsigned int key_len;
106 uint8_t key[AES_KEYSIZE_128];
109 struct dcp_aes_req_ctx {
114 struct dcp_sha_req_ctx {
120 * There can even be only one instance of the MXS DCP due to the
121 * design of Linux Crypto API.
123 static struct dcp *global_sdcp;
125 /* DCP register layout. */
126 #define MXS_DCP_CTRL 0x00
127 #define MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES (1 << 23)
128 #define MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING (1 << 22)
130 #define MXS_DCP_STAT 0x10
131 #define MXS_DCP_STAT_CLR 0x18
132 #define MXS_DCP_STAT_IRQ_MASK 0xf
134 #define MXS_DCP_CHANNELCTRL 0x20
135 #define MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK 0xff
137 #define MXS_DCP_CAPABILITY1 0x40
138 #define MXS_DCP_CAPABILITY1_SHA256 (4 << 16)
139 #define MXS_DCP_CAPABILITY1_SHA1 (1 << 16)
140 #define MXS_DCP_CAPABILITY1_AES128 (1 << 0)
142 #define MXS_DCP_CONTEXT 0x50
144 #define MXS_DCP_CH_N_CMDPTR(n) (0x100 + ((n) * 0x40))
146 #define MXS_DCP_CH_N_SEMA(n) (0x110 + ((n) * 0x40))
148 #define MXS_DCP_CH_N_STAT(n) (0x120 + ((n) * 0x40))
149 #define MXS_DCP_CH_N_STAT_CLR(n) (0x128 + ((n) * 0x40))
151 /* DMA descriptor bits. */
152 #define MXS_DCP_CONTROL0_HASH_TERM (1 << 13)
153 #define MXS_DCP_CONTROL0_HASH_INIT (1 << 12)
154 #define MXS_DCP_CONTROL0_PAYLOAD_KEY (1 << 11)
155 #define MXS_DCP_CONTROL0_CIPHER_ENCRYPT (1 << 8)
156 #define MXS_DCP_CONTROL0_CIPHER_INIT (1 << 9)
157 #define MXS_DCP_CONTROL0_ENABLE_HASH (1 << 6)
158 #define MXS_DCP_CONTROL0_ENABLE_CIPHER (1 << 5)
159 #define MXS_DCP_CONTROL0_DECR_SEMAPHORE (1 << 1)
160 #define MXS_DCP_CONTROL0_INTERRUPT (1 << 0)
162 #define MXS_DCP_CONTROL1_HASH_SELECT_SHA256 (2 << 16)
163 #define MXS_DCP_CONTROL1_HASH_SELECT_SHA1 (0 << 16)
164 #define MXS_DCP_CONTROL1_CIPHER_MODE_CBC (1 << 4)
165 #define MXS_DCP_CONTROL1_CIPHER_MODE_ECB (0 << 4)
166 #define MXS_DCP_CONTROL1_CIPHER_SELECT_AES128 (0 << 0)
168 static int mxs_dcp_start_dma(struct dcp_async_ctx *actx)
171 struct dcp *sdcp = global_sdcp;
172 const int chan = actx->chan;
175 struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
176 dma_addr_t desc_phys = dma_map_single(sdcp->dev, desc, sizeof(*desc),
179 dma_err = dma_mapping_error(sdcp->dev, desc_phys);
183 reinit_completion(&sdcp->completion[chan]);
185 /* Clear status register. */
186 writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(chan));
188 /* Load the DMA descriptor. */
189 writel(desc_phys, sdcp->base + MXS_DCP_CH_N_CMDPTR(chan));
191 /* Increment the semaphore to start the DMA transfer. */
192 writel(1, sdcp->base + MXS_DCP_CH_N_SEMA(chan));
194 ret = wait_for_completion_timeout(&sdcp->completion[chan],
195 msecs_to_jiffies(1000));
197 dev_err(sdcp->dev, "Channel %i timeout (DCP_STAT=0x%08x)\n",
198 chan, readl(sdcp->base + MXS_DCP_STAT));
202 stat = readl(sdcp->base + MXS_DCP_CH_N_STAT(chan));
204 dev_err(sdcp->dev, "Channel %i error (CH_STAT=0x%08x)\n",
209 dma_unmap_single(sdcp->dev, desc_phys, sizeof(*desc), DMA_TO_DEVICE);
215 * Encryption (AES128)
217 static int mxs_dcp_run_aes(struct dcp_async_ctx *actx,
218 struct ablkcipher_request *req, int init)
220 dma_addr_t key_phys, src_phys, dst_phys;
221 struct dcp *sdcp = global_sdcp;
222 struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
223 struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
226 key_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_key,
227 2 * AES_KEYSIZE_128, DMA_TO_DEVICE);
228 ret = dma_mapping_error(sdcp->dev, key_phys);
232 src_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_in_buf,
233 DCP_BUF_SZ, DMA_TO_DEVICE);
234 ret = dma_mapping_error(sdcp->dev, src_phys);
238 dst_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_out_buf,
239 DCP_BUF_SZ, DMA_FROM_DEVICE);
240 ret = dma_mapping_error(sdcp->dev, dst_phys);
244 if (actx->fill % AES_BLOCK_SIZE) {
245 dev_err(sdcp->dev, "Invalid block size!\n");
250 /* Fill in the DMA descriptor. */
251 desc->control0 = MXS_DCP_CONTROL0_DECR_SEMAPHORE |
252 MXS_DCP_CONTROL0_INTERRUPT |
253 MXS_DCP_CONTROL0_ENABLE_CIPHER;
255 /* Payload contains the key. */
256 desc->control0 |= MXS_DCP_CONTROL0_PAYLOAD_KEY;
259 desc->control0 |= MXS_DCP_CONTROL0_CIPHER_ENCRYPT;
261 desc->control0 |= MXS_DCP_CONTROL0_CIPHER_INIT;
263 desc->control1 = MXS_DCP_CONTROL1_CIPHER_SELECT_AES128;
266 desc->control1 |= MXS_DCP_CONTROL1_CIPHER_MODE_ECB;
268 desc->control1 |= MXS_DCP_CONTROL1_CIPHER_MODE_CBC;
270 desc->next_cmd_addr = 0;
271 desc->source = src_phys;
272 desc->destination = dst_phys;
273 desc->size = actx->fill;
274 desc->payload = key_phys;
277 ret = mxs_dcp_start_dma(actx);
280 dma_unmap_single(sdcp->dev, dst_phys, DCP_BUF_SZ, DMA_FROM_DEVICE);
282 dma_unmap_single(sdcp->dev, src_phys, DCP_BUF_SZ, DMA_TO_DEVICE);
284 dma_unmap_single(sdcp->dev, key_phys, 2 * AES_KEYSIZE_128,
290 static int mxs_dcp_aes_block_crypt(struct crypto_async_request *arq)
292 struct dcp *sdcp = global_sdcp;
294 struct ablkcipher_request *req = ablkcipher_request_cast(arq);
295 struct dcp_async_ctx *actx = crypto_tfm_ctx(arq->tfm);
296 struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
298 struct scatterlist *dst = req->dst;
299 struct scatterlist *src = req->src;
300 int dst_nents = sg_nents(dst);
302 const int out_off = DCP_BUF_SZ;
303 uint8_t *in_buf = sdcp->coh->aes_in_buf;
304 uint8_t *out_buf = sdcp->coh->aes_out_buf;
306 uint32_t dst_off = 0;
307 uint8_t *src_buf = NULL;
308 uint32_t last_out_len = 0;
310 uint8_t *key = sdcp->coh->aes_key;
313 unsigned int i, len, clen, tlen = 0;
315 bool limit_hit = false;
319 /* Copy the key from the temporary location. */
320 memcpy(key, actx->key, actx->key_len);
323 /* Copy the CBC IV just past the key. */
324 memcpy(key + AES_KEYSIZE_128, req->info, AES_KEYSIZE_128);
325 /* CBC needs the INIT set. */
328 memset(key + AES_KEYSIZE_128, 0, AES_KEYSIZE_128);
331 for_each_sg(req->src, src, sg_nents(src), i) {
332 src_buf = sg_virt(src);
333 len = sg_dma_len(src);
335 limit_hit = tlen > req->nbytes;
338 len = req->nbytes - (tlen - len);
341 if (actx->fill + len > out_off)
342 clen = out_off - actx->fill;
346 memcpy(in_buf + actx->fill, src_buf, clen);
352 * If we filled the buffer or this is the last SG,
355 if (actx->fill == out_off || sg_is_last(src) ||
357 ret = mxs_dcp_run_aes(actx, req, init);
362 sg_pcopy_from_buffer(dst, dst_nents, out_buf,
363 actx->fill, dst_off);
364 dst_off += actx->fill;
365 last_out_len = actx->fill;
374 /* Copy the IV for CBC for chaining */
377 memcpy(req->info, out_buf+(last_out_len-AES_BLOCK_SIZE),
380 memcpy(req->info, in_buf+(last_out_len-AES_BLOCK_SIZE),
387 static int dcp_chan_thread_aes(void *data)
389 struct dcp *sdcp = global_sdcp;
390 const int chan = DCP_CHAN_CRYPTO;
392 struct crypto_async_request *backlog;
393 struct crypto_async_request *arq;
397 while (!kthread_should_stop()) {
398 set_current_state(TASK_INTERRUPTIBLE);
400 spin_lock(&sdcp->lock[chan]);
401 backlog = crypto_get_backlog(&sdcp->queue[chan]);
402 arq = crypto_dequeue_request(&sdcp->queue[chan]);
403 spin_unlock(&sdcp->lock[chan]);
405 if (!backlog && !arq) {
410 set_current_state(TASK_RUNNING);
413 backlog->complete(backlog, -EINPROGRESS);
416 ret = mxs_dcp_aes_block_crypt(arq);
417 arq->complete(arq, ret);
424 static int mxs_dcp_block_fallback(struct ablkcipher_request *req, int enc)
426 struct crypto_tfm *tfm =
427 crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req));
428 struct dcp_async_ctx *ctx = crypto_ablkcipher_ctx(
429 crypto_ablkcipher_reqtfm(req));
432 ablkcipher_request_set_tfm(req, ctx->fallback);
435 ret = crypto_ablkcipher_encrypt(req);
437 ret = crypto_ablkcipher_decrypt(req);
439 ablkcipher_request_set_tfm(req, __crypto_ablkcipher_cast(tfm));
444 static int mxs_dcp_aes_enqueue(struct ablkcipher_request *req, int enc, int ecb)
446 struct dcp *sdcp = global_sdcp;
447 struct crypto_async_request *arq = &req->base;
448 struct dcp_async_ctx *actx = crypto_tfm_ctx(arq->tfm);
449 struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
452 if (unlikely(actx->key_len != AES_KEYSIZE_128))
453 return mxs_dcp_block_fallback(req, enc);
457 actx->chan = DCP_CHAN_CRYPTO;
459 spin_lock(&sdcp->lock[actx->chan]);
460 ret = crypto_enqueue_request(&sdcp->queue[actx->chan], &req->base);
461 spin_unlock(&sdcp->lock[actx->chan]);
463 wake_up_process(sdcp->thread[actx->chan]);
468 static int mxs_dcp_aes_ecb_decrypt(struct ablkcipher_request *req)
470 return mxs_dcp_aes_enqueue(req, 0, 1);
473 static int mxs_dcp_aes_ecb_encrypt(struct ablkcipher_request *req)
475 return mxs_dcp_aes_enqueue(req, 1, 1);
478 static int mxs_dcp_aes_cbc_decrypt(struct ablkcipher_request *req)
480 return mxs_dcp_aes_enqueue(req, 0, 0);
483 static int mxs_dcp_aes_cbc_encrypt(struct ablkcipher_request *req)
485 return mxs_dcp_aes_enqueue(req, 1, 0);
488 static int mxs_dcp_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
491 struct dcp_async_ctx *actx = crypto_ablkcipher_ctx(tfm);
495 * AES 128 is supposed by the hardware, store key into temporary
496 * buffer and exit. We must use the temporary buffer here, since
497 * there can still be an operation in progress.
500 if (len == AES_KEYSIZE_128) {
501 memcpy(actx->key, key, len);
505 /* Check if the key size is supported by kernel at all. */
506 if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256) {
507 tfm->base.crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
512 * If the requested AES key size is not supported by the hardware,
513 * but is supported by in-kernel software implementation, we use
516 actx->fallback->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
517 actx->fallback->base.crt_flags |=
518 tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK;
520 ret = crypto_ablkcipher_setkey(actx->fallback, key, len);
524 tfm->base.crt_flags &= ~CRYPTO_TFM_RES_MASK;
525 tfm->base.crt_flags |=
526 actx->fallback->base.crt_flags & CRYPTO_TFM_RES_MASK;
531 static int mxs_dcp_aes_fallback_init(struct crypto_tfm *tfm)
533 const char *name = crypto_tfm_alg_name(tfm);
534 const uint32_t flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK;
535 struct dcp_async_ctx *actx = crypto_tfm_ctx(tfm);
536 struct crypto_ablkcipher *blk;
538 blk = crypto_alloc_ablkcipher(name, 0, flags);
542 actx->fallback = blk;
543 tfm->crt_ablkcipher.reqsize = sizeof(struct dcp_aes_req_ctx);
547 static void mxs_dcp_aes_fallback_exit(struct crypto_tfm *tfm)
549 struct dcp_async_ctx *actx = crypto_tfm_ctx(tfm);
551 crypto_free_ablkcipher(actx->fallback);
552 actx->fallback = NULL;
556 * Hashing (SHA1/SHA256)
558 static int mxs_dcp_run_sha(struct ahash_request *req)
560 struct dcp *sdcp = global_sdcp;
563 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
564 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
565 struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
566 struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
568 dma_addr_t digest_phys = 0;
569 dma_addr_t buf_phys = dma_map_single(sdcp->dev, sdcp->coh->sha_in_buf,
570 DCP_BUF_SZ, DMA_TO_DEVICE);
572 ret = dma_mapping_error(sdcp->dev, buf_phys);
576 /* Fill in the DMA descriptor. */
577 desc->control0 = MXS_DCP_CONTROL0_DECR_SEMAPHORE |
578 MXS_DCP_CONTROL0_INTERRUPT |
579 MXS_DCP_CONTROL0_ENABLE_HASH;
581 desc->control0 |= MXS_DCP_CONTROL0_HASH_INIT;
583 desc->control1 = actx->alg;
584 desc->next_cmd_addr = 0;
585 desc->source = buf_phys;
586 desc->destination = 0;
587 desc->size = actx->fill;
592 * Align driver with hw behavior when generating null hashes
594 if (rctx->init && rctx->fini && desc->size == 0) {
595 struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
596 const uint8_t *sha_buf =
597 (actx->alg == MXS_DCP_CONTROL1_HASH_SELECT_SHA1) ?
598 sha1_null_hash : sha256_null_hash;
599 memcpy(sdcp->coh->sha_out_buf, sha_buf, halg->digestsize);
604 /* Set HASH_TERM bit for last transfer block. */
606 digest_phys = dma_map_single(sdcp->dev, sdcp->coh->sha_out_buf,
607 DCP_SHA_PAY_SZ, DMA_FROM_DEVICE);
608 ret = dma_mapping_error(sdcp->dev, digest_phys);
612 desc->control0 |= MXS_DCP_CONTROL0_HASH_TERM;
613 desc->payload = digest_phys;
616 ret = mxs_dcp_start_dma(actx);
619 dma_unmap_single(sdcp->dev, digest_phys, DCP_SHA_PAY_SZ,
623 dma_unmap_single(sdcp->dev, buf_phys, DCP_BUF_SZ, DMA_TO_DEVICE);
628 static int dcp_sha_req_to_buf(struct crypto_async_request *arq)
630 struct dcp *sdcp = global_sdcp;
632 struct ahash_request *req = ahash_request_cast(arq);
633 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
634 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
635 struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
636 struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
638 uint8_t *in_buf = sdcp->coh->sha_in_buf;
639 uint8_t *out_buf = sdcp->coh->sha_out_buf;
641 struct scatterlist *src;
643 unsigned int i, len, clen, oft = 0;
646 int fin = rctx->fini;
654 if (actx->fill + len > DCP_BUF_SZ)
655 clen = DCP_BUF_SZ - actx->fill;
659 scatterwalk_map_and_copy(in_buf + actx->fill, src, oft, clen,
667 * If we filled the buffer and still have some
668 * more data, submit the buffer.
670 if (len && actx->fill == DCP_BUF_SZ) {
671 ret = mxs_dcp_run_sha(req);
682 /* Submit whatever is left. */
686 ret = mxs_dcp_run_sha(req);
692 /* For some reason the result is flipped */
693 for (i = 0; i < halg->digestsize; i++)
694 req->result[i] = out_buf[halg->digestsize - i - 1];
700 static int dcp_chan_thread_sha(void *data)
702 struct dcp *sdcp = global_sdcp;
703 const int chan = DCP_CHAN_HASH_SHA;
705 struct crypto_async_request *backlog;
706 struct crypto_async_request *arq;
708 struct dcp_sha_req_ctx *rctx;
710 struct ahash_request *req;
713 while (!kthread_should_stop()) {
714 set_current_state(TASK_INTERRUPTIBLE);
716 spin_lock(&sdcp->lock[chan]);
717 backlog = crypto_get_backlog(&sdcp->queue[chan]);
718 arq = crypto_dequeue_request(&sdcp->queue[chan]);
719 spin_unlock(&sdcp->lock[chan]);
721 if (!backlog && !arq) {
726 set_current_state(TASK_RUNNING);
729 backlog->complete(backlog, -EINPROGRESS);
732 req = ahash_request_cast(arq);
733 rctx = ahash_request_ctx(req);
735 ret = dcp_sha_req_to_buf(arq);
737 arq->complete(arq, ret);
744 static int dcp_sha_init(struct ahash_request *req)
746 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
747 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
749 struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
752 * Start hashing session. The code below only inits the
753 * hashing session context, nothing more.
755 memset(actx, 0, sizeof(*actx));
757 if (strcmp(halg->base.cra_name, "sha1") == 0)
758 actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA1;
760 actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA256;
764 actx->chan = DCP_CHAN_HASH_SHA;
766 mutex_init(&actx->mutex);
771 static int dcp_sha_update_fx(struct ahash_request *req, int fini)
773 struct dcp *sdcp = global_sdcp;
775 struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
776 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
777 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
782 * Ignore requests that have no data in them and are not
783 * the trailing requests in the stream of requests.
785 if (!req->nbytes && !fini)
788 mutex_lock(&actx->mutex);
797 spin_lock(&sdcp->lock[actx->chan]);
798 ret = crypto_enqueue_request(&sdcp->queue[actx->chan], &req->base);
799 spin_unlock(&sdcp->lock[actx->chan]);
801 wake_up_process(sdcp->thread[actx->chan]);
802 mutex_unlock(&actx->mutex);
807 static int dcp_sha_update(struct ahash_request *req)
809 return dcp_sha_update_fx(req, 0);
812 static int dcp_sha_final(struct ahash_request *req)
814 ahash_request_set_crypt(req, NULL, req->result, 0);
816 return dcp_sha_update_fx(req, 1);
819 static int dcp_sha_finup(struct ahash_request *req)
821 return dcp_sha_update_fx(req, 1);
824 static int dcp_sha_digest(struct ahash_request *req)
828 ret = dcp_sha_init(req);
832 return dcp_sha_finup(req);
835 static int dcp_sha_cra_init(struct crypto_tfm *tfm)
837 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
838 sizeof(struct dcp_sha_req_ctx));
842 static void dcp_sha_cra_exit(struct crypto_tfm *tfm)
846 /* AES 128 ECB and AES 128 CBC */
847 static struct crypto_alg dcp_aes_algs[] = {
849 .cra_name = "ecb(aes)",
850 .cra_driver_name = "ecb-aes-dcp",
853 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
855 CRYPTO_ALG_NEED_FALLBACK,
856 .cra_init = mxs_dcp_aes_fallback_init,
857 .cra_exit = mxs_dcp_aes_fallback_exit,
858 .cra_blocksize = AES_BLOCK_SIZE,
859 .cra_ctxsize = sizeof(struct dcp_async_ctx),
860 .cra_type = &crypto_ablkcipher_type,
861 .cra_module = THIS_MODULE,
864 .min_keysize = AES_MIN_KEY_SIZE,
865 .max_keysize = AES_MAX_KEY_SIZE,
866 .setkey = mxs_dcp_aes_setkey,
867 .encrypt = mxs_dcp_aes_ecb_encrypt,
868 .decrypt = mxs_dcp_aes_ecb_decrypt
872 .cra_name = "cbc(aes)",
873 .cra_driver_name = "cbc-aes-dcp",
876 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
878 CRYPTO_ALG_NEED_FALLBACK,
879 .cra_init = mxs_dcp_aes_fallback_init,
880 .cra_exit = mxs_dcp_aes_fallback_exit,
881 .cra_blocksize = AES_BLOCK_SIZE,
882 .cra_ctxsize = sizeof(struct dcp_async_ctx),
883 .cra_type = &crypto_ablkcipher_type,
884 .cra_module = THIS_MODULE,
887 .min_keysize = AES_MIN_KEY_SIZE,
888 .max_keysize = AES_MAX_KEY_SIZE,
889 .setkey = mxs_dcp_aes_setkey,
890 .encrypt = mxs_dcp_aes_cbc_encrypt,
891 .decrypt = mxs_dcp_aes_cbc_decrypt,
892 .ivsize = AES_BLOCK_SIZE,
899 static struct ahash_alg dcp_sha1_alg = {
900 .init = dcp_sha_init,
901 .update = dcp_sha_update,
902 .final = dcp_sha_final,
903 .finup = dcp_sha_finup,
904 .digest = dcp_sha_digest,
906 .digestsize = SHA1_DIGEST_SIZE,
909 .cra_driver_name = "sha1-dcp",
912 .cra_flags = CRYPTO_ALG_ASYNC,
913 .cra_blocksize = SHA1_BLOCK_SIZE,
914 .cra_ctxsize = sizeof(struct dcp_async_ctx),
915 .cra_module = THIS_MODULE,
916 .cra_init = dcp_sha_cra_init,
917 .cra_exit = dcp_sha_cra_exit,
923 static struct ahash_alg dcp_sha256_alg = {
924 .init = dcp_sha_init,
925 .update = dcp_sha_update,
926 .final = dcp_sha_final,
927 .finup = dcp_sha_finup,
928 .digest = dcp_sha_digest,
930 .digestsize = SHA256_DIGEST_SIZE,
932 .cra_name = "sha256",
933 .cra_driver_name = "sha256-dcp",
936 .cra_flags = CRYPTO_ALG_ASYNC,
937 .cra_blocksize = SHA256_BLOCK_SIZE,
938 .cra_ctxsize = sizeof(struct dcp_async_ctx),
939 .cra_module = THIS_MODULE,
940 .cra_init = dcp_sha_cra_init,
941 .cra_exit = dcp_sha_cra_exit,
946 static irqreturn_t mxs_dcp_irq(int irq, void *context)
948 struct dcp *sdcp = context;
952 stat = readl(sdcp->base + MXS_DCP_STAT);
953 stat &= MXS_DCP_STAT_IRQ_MASK;
957 /* Clear the interrupts. */
958 writel(stat, sdcp->base + MXS_DCP_STAT_CLR);
960 /* Complete the DMA requests that finished. */
961 for (i = 0; i < DCP_MAX_CHANS; i++)
963 complete(&sdcp->completion[i]);
968 static int mxs_dcp_probe(struct platform_device *pdev)
970 struct device *dev = &pdev->dev;
971 struct dcp *sdcp = NULL;
974 struct resource *iores;
975 int dcp_vmi_irq, dcp_irq;
978 dev_err(dev, "Only one DCP instance allowed!\n");
982 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
983 dcp_vmi_irq = platform_get_irq(pdev, 0);
987 dcp_irq = platform_get_irq(pdev, 1);
991 sdcp = devm_kzalloc(dev, sizeof(*sdcp), GFP_KERNEL);
996 sdcp->base = devm_ioremap_resource(dev, iores);
997 if (IS_ERR(sdcp->base))
998 return PTR_ERR(sdcp->base);
1001 ret = devm_request_irq(dev, dcp_vmi_irq, mxs_dcp_irq, 0,
1002 "dcp-vmi-irq", sdcp);
1004 dev_err(dev, "Failed to claim DCP VMI IRQ!\n");
1008 ret = devm_request_irq(dev, dcp_irq, mxs_dcp_irq, 0,
1011 dev_err(dev, "Failed to claim DCP IRQ!\n");
1015 /* Allocate coherent helper block. */
1016 sdcp->coh = devm_kzalloc(dev, sizeof(*sdcp->coh) + DCP_ALIGNMENT,
1021 /* Re-align the structure so it fits the DCP constraints. */
1022 sdcp->coh = PTR_ALIGN(sdcp->coh, DCP_ALIGNMENT);
1024 /* Restart the DCP block. */
1025 ret = stmp_reset_block(sdcp->base);
1029 /* Initialize control register. */
1030 writel(MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES |
1031 MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING | 0xf,
1032 sdcp->base + MXS_DCP_CTRL);
1034 /* Enable all DCP DMA channels. */
1035 writel(MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK,
1036 sdcp->base + MXS_DCP_CHANNELCTRL);
1039 * We do not enable context switching. Give the context buffer a
1040 * pointer to an illegal address so if context switching is
1041 * inadvertantly enabled, the DCP will return an error instead of
1042 * trashing good memory. The DCP DMA cannot access ROM, so any ROM
1045 writel(0xffff0000, sdcp->base + MXS_DCP_CONTEXT);
1046 for (i = 0; i < DCP_MAX_CHANS; i++)
1047 writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(i));
1048 writel(0xffffffff, sdcp->base + MXS_DCP_STAT_CLR);
1052 platform_set_drvdata(pdev, sdcp);
1054 for (i = 0; i < DCP_MAX_CHANS; i++) {
1055 spin_lock_init(&sdcp->lock[i]);
1056 init_completion(&sdcp->completion[i]);
1057 crypto_init_queue(&sdcp->queue[i], 50);
1060 /* Create the SHA and AES handler threads. */
1061 sdcp->thread[DCP_CHAN_HASH_SHA] = kthread_run(dcp_chan_thread_sha,
1062 NULL, "mxs_dcp_chan/sha");
1063 if (IS_ERR(sdcp->thread[DCP_CHAN_HASH_SHA])) {
1064 dev_err(dev, "Error starting SHA thread!\n");
1065 return PTR_ERR(sdcp->thread[DCP_CHAN_HASH_SHA]);
1068 sdcp->thread[DCP_CHAN_CRYPTO] = kthread_run(dcp_chan_thread_aes,
1069 NULL, "mxs_dcp_chan/aes");
1070 if (IS_ERR(sdcp->thread[DCP_CHAN_CRYPTO])) {
1071 dev_err(dev, "Error starting SHA thread!\n");
1072 ret = PTR_ERR(sdcp->thread[DCP_CHAN_CRYPTO]);
1073 goto err_destroy_sha_thread;
1076 /* Register the various crypto algorithms. */
1077 sdcp->caps = readl(sdcp->base + MXS_DCP_CAPABILITY1);
1079 if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128) {
1080 ret = crypto_register_algs(dcp_aes_algs,
1081 ARRAY_SIZE(dcp_aes_algs));
1083 /* Failed to register algorithm. */
1084 dev_err(dev, "Failed to register AES crypto!\n");
1085 goto err_destroy_aes_thread;
1089 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1) {
1090 ret = crypto_register_ahash(&dcp_sha1_alg);
1092 dev_err(dev, "Failed to register %s hash!\n",
1093 dcp_sha1_alg.halg.base.cra_name);
1094 goto err_unregister_aes;
1098 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256) {
1099 ret = crypto_register_ahash(&dcp_sha256_alg);
1101 dev_err(dev, "Failed to register %s hash!\n",
1102 dcp_sha256_alg.halg.base.cra_name);
1103 goto err_unregister_sha1;
1109 err_unregister_sha1:
1110 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1)
1111 crypto_unregister_ahash(&dcp_sha1_alg);
1114 if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128)
1115 crypto_unregister_algs(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs));
1117 err_destroy_aes_thread:
1118 kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]);
1120 err_destroy_sha_thread:
1121 kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]);
1125 static int mxs_dcp_remove(struct platform_device *pdev)
1127 struct dcp *sdcp = platform_get_drvdata(pdev);
1129 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256)
1130 crypto_unregister_ahash(&dcp_sha256_alg);
1132 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1)
1133 crypto_unregister_ahash(&dcp_sha1_alg);
1135 if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128)
1136 crypto_unregister_algs(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs));
1138 kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]);
1139 kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]);
1141 platform_set_drvdata(pdev, NULL);
1148 static const struct of_device_id mxs_dcp_dt_ids[] = {
1149 { .compatible = "fsl,imx23-dcp", .data = NULL, },
1150 { .compatible = "fsl,imx28-dcp", .data = NULL, },
1154 MODULE_DEVICE_TABLE(of, mxs_dcp_dt_ids);
1156 static struct platform_driver mxs_dcp_driver = {
1157 .probe = mxs_dcp_probe,
1158 .remove = mxs_dcp_remove,
1161 .of_match_table = mxs_dcp_dt_ids,
1165 module_platform_driver(mxs_dcp_driver);
1167 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
1168 MODULE_DESCRIPTION("Freescale MXS DCP Driver");
1169 MODULE_LICENSE("GPL");
1170 MODULE_ALIAS("platform:mxs-dcp");