1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Freescale i.MX23/i.MX28 Data Co-Processor driver
5 * Copyright (C) 2013 Marek Vasut <marex@denx.de>
8 #include <linux/dma-mapping.h>
9 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <linux/kthread.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/stmp_device.h>
17 #include <linux/clk.h>
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>
26 #define DCP_MAX_CHANS 4
27 #define DCP_BUF_SZ PAGE_SIZE
28 #define DCP_SHA_PAY_SZ 64
30 #define DCP_ALIGNMENT 64
33 * Null hashes to align with hw behavior on imx6sl and ull
34 * these are flipped for consistency with hw output
36 static 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";
40 static 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";
46 /* DCP DMA descriptor. */
48 uint32_t next_cmd_addr;
58 /* Coherent aligned block for bounce buffering. */
59 struct 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];
65 uint8_t aes_key[2 * AES_KEYSIZE_128];
67 struct dcp_dma_desc desc[DCP_MAX_CHANS];
76 struct dcp_coherent_block *coh;
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];
86 DCP_CHAN_HASH_SHA = 0,
90 struct dcp_async_ctx {
95 /* SHA Hash-specific context */
100 /* Crypto-specific context */
101 struct crypto_skcipher *fallback;
102 unsigned int key_len;
103 uint8_t key[AES_KEYSIZE_128];
106 struct dcp_aes_req_ctx {
109 struct skcipher_request fallback_req; // keep at the end
112 struct dcp_sha_req_ctx {
117 struct dcp_export_state {
118 struct dcp_sha_req_ctx req_ctx;
119 struct dcp_async_ctx async_ctx;
123 * There can even be only one instance of the MXS DCP due to the
124 * design of Linux Crypto API.
126 static struct dcp *global_sdcp;
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)
133 #define MXS_DCP_STAT 0x10
134 #define MXS_DCP_STAT_CLR 0x18
135 #define MXS_DCP_STAT_IRQ_MASK 0xf
137 #define MXS_DCP_CHANNELCTRL 0x20
138 #define MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK 0xff
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)
145 #define MXS_DCP_CONTEXT 0x50
147 #define MXS_DCP_CH_N_CMDPTR(n) (0x100 + ((n) * 0x40))
149 #define MXS_DCP_CH_N_SEMA(n) (0x110 + ((n) * 0x40))
151 #define MXS_DCP_CH_N_STAT(n) (0x120 + ((n) * 0x40))
152 #define MXS_DCP_CH_N_STAT_CLR(n) (0x128 + ((n) * 0x40))
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)
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)
171 static int mxs_dcp_start_dma(struct dcp_async_ctx *actx)
174 struct dcp *sdcp = global_sdcp;
175 const int chan = actx->chan;
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),
182 dma_err = dma_mapping_error(sdcp->dev, desc_phys);
186 reinit_completion(&sdcp->completion[chan]);
188 /* Clear status register. */
189 writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(chan));
191 /* Load the DMA descriptor. */
192 writel(desc_phys, sdcp->base + MXS_DCP_CH_N_CMDPTR(chan));
194 /* Increment the semaphore to start the DMA transfer. */
195 writel(1, sdcp->base + MXS_DCP_CH_N_SEMA(chan));
197 ret = wait_for_completion_timeout(&sdcp->completion[chan],
198 msecs_to_jiffies(1000));
200 dev_err(sdcp->dev, "Channel %i timeout (DCP_STAT=0x%08x)\n",
201 chan, readl(sdcp->base + MXS_DCP_STAT));
205 stat = readl(sdcp->base + MXS_DCP_CH_N_STAT(chan));
207 dev_err(sdcp->dev, "Channel %i error (CH_STAT=0x%08x)\n",
212 dma_unmap_single(sdcp->dev, desc_phys, sizeof(*desc), DMA_TO_DEVICE);
218 * Encryption (AES128)
220 static int mxs_dcp_run_aes(struct dcp_async_ctx *actx,
221 struct skcipher_request *req, int init)
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);
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);
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);
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);
247 if (actx->fill % AES_BLOCK_SIZE) {
248 dev_err(sdcp->dev, "Invalid block size!\n");
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;
258 /* Payload contains the key. */
259 desc->control0 |= MXS_DCP_CONTROL0_PAYLOAD_KEY;
262 desc->control0 |= MXS_DCP_CONTROL0_CIPHER_ENCRYPT;
264 desc->control0 |= MXS_DCP_CONTROL0_CIPHER_INIT;
266 desc->control1 = MXS_DCP_CONTROL1_CIPHER_SELECT_AES128;
269 desc->control1 |= MXS_DCP_CONTROL1_CIPHER_MODE_ECB;
271 desc->control1 |= MXS_DCP_CONTROL1_CIPHER_MODE_CBC;
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;
280 ret = mxs_dcp_start_dma(actx);
283 dma_unmap_single(sdcp->dev, dst_phys, DCP_BUF_SZ, DMA_FROM_DEVICE);
285 dma_unmap_single(sdcp->dev, src_phys, DCP_BUF_SZ, DMA_TO_DEVICE);
287 dma_unmap_single(sdcp->dev, key_phys, 2 * AES_KEYSIZE_128,
293 static int mxs_dcp_aes_block_crypt(struct crypto_async_request *arq)
295 struct dcp *sdcp = global_sdcp;
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);
301 struct scatterlist *dst = req->dst;
302 struct scatterlist *src = req->src;
303 int dst_nents = sg_nents(dst);
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;
309 uint32_t dst_off = 0;
310 uint8_t *src_buf = NULL;
311 uint32_t last_out_len = 0;
313 uint8_t *key = sdcp->coh->aes_key;
316 unsigned int i, len, clen, tlen = 0;
318 bool limit_hit = false;
322 /* Copy the key from the temporary location. */
323 memcpy(key, actx->key, actx->key_len);
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. */
331 memset(key + AES_KEYSIZE_128, 0, AES_KEYSIZE_128);
334 for_each_sg(req->src, src, sg_nents(req->src), i) {
335 src_buf = sg_virt(src);
336 len = sg_dma_len(src);
338 limit_hit = tlen > req->cryptlen;
341 len = req->cryptlen - (tlen - len);
344 if (actx->fill + len > out_off)
345 clen = out_off - actx->fill;
349 memcpy(in_buf + actx->fill, src_buf, clen);
355 * If we filled the buffer or this is the last SG,
358 if (actx->fill == out_off || sg_is_last(src) ||
360 ret = mxs_dcp_run_aes(actx, req, init);
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;
377 /* Copy the IV for CBC for chaining */
380 memcpy(req->iv, out_buf+(last_out_len-AES_BLOCK_SIZE),
383 memcpy(req->iv, in_buf+(last_out_len-AES_BLOCK_SIZE),
390 static int dcp_chan_thread_aes(void *data)
392 struct dcp *sdcp = global_sdcp;
393 const int chan = DCP_CHAN_CRYPTO;
395 struct crypto_async_request *backlog;
396 struct crypto_async_request *arq;
400 while (!kthread_should_stop()) {
401 set_current_state(TASK_INTERRUPTIBLE);
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]);
408 if (!backlog && !arq) {
413 set_current_state(TASK_RUNNING);
416 crypto_request_complete(backlog, -EINPROGRESS);
419 ret = mxs_dcp_aes_block_crypt(arq);
420 crypto_request_complete(arq, ret);
427 static int mxs_dcp_block_fallback(struct skcipher_request *req, int enc)
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);
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);
441 ret = crypto_skcipher_encrypt(&rctx->fallback_req);
443 ret = crypto_skcipher_decrypt(&rctx->fallback_req);
448 static int mxs_dcp_aes_enqueue(struct skcipher_request *req, int enc, int ecb)
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);
456 if (unlikely(actx->key_len != AES_KEYSIZE_128))
457 return mxs_dcp_block_fallback(req, enc);
461 actx->chan = DCP_CHAN_CRYPTO;
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]);
467 wake_up_process(sdcp->thread[actx->chan]);
472 static int mxs_dcp_aes_ecb_decrypt(struct skcipher_request *req)
474 return mxs_dcp_aes_enqueue(req, 0, 1);
477 static int mxs_dcp_aes_ecb_encrypt(struct skcipher_request *req)
479 return mxs_dcp_aes_enqueue(req, 1, 1);
482 static int mxs_dcp_aes_cbc_decrypt(struct skcipher_request *req)
484 return mxs_dcp_aes_enqueue(req, 0, 0);
487 static int mxs_dcp_aes_cbc_encrypt(struct skcipher_request *req)
489 return mxs_dcp_aes_enqueue(req, 1, 0);
492 static int mxs_dcp_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
495 struct dcp_async_ctx *actx = crypto_skcipher_ctx(tfm);
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.
503 if (len == AES_KEYSIZE_128) {
504 memcpy(actx->key, key, len);
509 * If the requested AES key size is not supported by the hardware,
510 * but is supported by in-kernel software implementation, we use
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);
519 static int mxs_dcp_aes_fallback_init_tfm(struct crypto_skcipher *tfm)
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;
525 blk = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
529 actx->fallback = blk;
530 crypto_skcipher_set_reqsize(tfm, sizeof(struct dcp_aes_req_ctx) +
531 crypto_skcipher_reqsize(blk));
535 static void mxs_dcp_aes_fallback_exit_tfm(struct crypto_skcipher *tfm)
537 struct dcp_async_ctx *actx = crypto_skcipher_ctx(tfm);
539 crypto_free_skcipher(actx->fallback);
543 * Hashing (SHA1/SHA256)
545 static int mxs_dcp_run_sha(struct ahash_request *req)
547 struct dcp *sdcp = global_sdcp;
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];
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);
559 ret = dma_mapping_error(sdcp->dev, buf_phys);
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;
568 desc->control0 |= MXS_DCP_CONTROL0_HASH_INIT;
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;
579 * Align driver with hw behavior when generating null hashes
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);
591 /* Set HASH_TERM bit for last transfer block. */
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);
599 desc->control0 |= MXS_DCP_CONTROL0_HASH_TERM;
600 desc->payload = digest_phys;
603 ret = mxs_dcp_start_dma(actx);
606 dma_unmap_single(sdcp->dev, digest_phys, DCP_SHA_PAY_SZ,
610 dma_unmap_single(sdcp->dev, buf_phys, DCP_BUF_SZ, DMA_TO_DEVICE);
615 static int dcp_sha_req_to_buf(struct crypto_async_request *arq)
617 struct dcp *sdcp = global_sdcp;
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);
625 uint8_t *in_buf = sdcp->coh->sha_in_buf;
626 uint8_t *out_buf = sdcp->coh->sha_out_buf;
628 struct scatterlist *src;
630 unsigned int i, len, clen, oft = 0;
633 int fin = rctx->fini;
641 if (actx->fill + len > DCP_BUF_SZ)
642 clen = DCP_BUF_SZ - actx->fill;
646 scatterwalk_map_and_copy(in_buf + actx->fill, src, oft, clen,
654 * If we filled the buffer and still have some
655 * more data, submit the buffer.
657 if (len && actx->fill == DCP_BUF_SZ) {
658 ret = mxs_dcp_run_sha(req);
669 /* Submit whatever is left. */
673 ret = mxs_dcp_run_sha(req);
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];
687 static int dcp_chan_thread_sha(void *data)
689 struct dcp *sdcp = global_sdcp;
690 const int chan = DCP_CHAN_HASH_SHA;
692 struct crypto_async_request *backlog;
693 struct crypto_async_request *arq;
696 while (!kthread_should_stop()) {
697 set_current_state(TASK_INTERRUPTIBLE);
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]);
704 if (!backlog && !arq) {
709 set_current_state(TASK_RUNNING);
712 crypto_request_complete(backlog, -EINPROGRESS);
715 ret = dcp_sha_req_to_buf(arq);
716 crypto_request_complete(arq, ret);
723 static int dcp_sha_init(struct ahash_request *req)
725 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
726 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
728 struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
731 * Start hashing session. The code below only inits the
732 * hashing session context, nothing more.
734 memset(actx, 0, sizeof(*actx));
736 if (strcmp(halg->base.cra_name, "sha1") == 0)
737 actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA1;
739 actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA256;
743 actx->chan = DCP_CHAN_HASH_SHA;
745 mutex_init(&actx->mutex);
750 static int dcp_sha_update_fx(struct ahash_request *req, int fini)
752 struct dcp *sdcp = global_sdcp;
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);
761 * Ignore requests that have no data in them and are not
762 * the trailing requests in the stream of requests.
764 if (!req->nbytes && !fini)
767 mutex_lock(&actx->mutex);
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]);
780 wake_up_process(sdcp->thread[actx->chan]);
781 mutex_unlock(&actx->mutex);
786 static int dcp_sha_update(struct ahash_request *req)
788 return dcp_sha_update_fx(req, 0);
791 static int dcp_sha_final(struct ahash_request *req)
793 ahash_request_set_crypt(req, NULL, req->result, 0);
795 return dcp_sha_update_fx(req, 1);
798 static int dcp_sha_finup(struct ahash_request *req)
800 return dcp_sha_update_fx(req, 1);
803 static int dcp_sha_digest(struct ahash_request *req)
807 ret = dcp_sha_init(req);
811 return dcp_sha_finup(req);
814 static int dcp_sha_import(struct ahash_request *req, const void *in)
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;
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));
829 static int dcp_sha_export(struct ahash_request *req, void *out)
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;
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));
842 static int dcp_sha_cra_init(struct crypto_tfm *tfm)
844 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
845 sizeof(struct dcp_sha_req_ctx));
849 static void dcp_sha_cra_exit(struct crypto_tfm *tfm)
853 /* AES 128 ECB and AES 128 CBC */
854 static struct skcipher_alg dcp_aes_algs[] = {
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,
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,
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,
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,
896 static 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,
905 .digestsize = SHA1_DIGEST_SIZE,
906 .statesize = sizeof(struct dcp_export_state),
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,
929 .import = dcp_sha_import,
930 .export = dcp_sha_export,
932 .digestsize = SHA256_DIGEST_SIZE,
933 .statesize = sizeof(struct dcp_export_state),
935 .cra_name = "sha256",
936 .cra_driver_name = "sha256-dcp",
939 .cra_flags = CRYPTO_ALG_ASYNC,
940 .cra_blocksize = SHA256_BLOCK_SIZE,
941 .cra_ctxsize = sizeof(struct dcp_async_ctx),
942 .cra_module = THIS_MODULE,
943 .cra_init = dcp_sha_cra_init,
944 .cra_exit = dcp_sha_cra_exit,
949 static irqreturn_t mxs_dcp_irq(int irq, void *context)
951 struct dcp *sdcp = context;
955 stat = readl(sdcp->base + MXS_DCP_STAT);
956 stat &= MXS_DCP_STAT_IRQ_MASK;
960 /* Clear the interrupts. */
961 writel(stat, sdcp->base + MXS_DCP_STAT_CLR);
963 /* Complete the DMA requests that finished. */
964 for (i = 0; i < DCP_MAX_CHANS; i++)
966 complete(&sdcp->completion[i]);
971 static int mxs_dcp_probe(struct platform_device *pdev)
973 struct device *dev = &pdev->dev;
974 struct dcp *sdcp = NULL;
976 int dcp_vmi_irq, dcp_irq;
979 dev_err(dev, "Only one DCP instance allowed!\n");
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_platform_ioremap_resource(pdev, 0);
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 /* DCP clock is optional, only used on some SOCs */
1025 sdcp->dcp_clk = devm_clk_get_optional_enabled(dev, "dcp");
1026 if (IS_ERR(sdcp->dcp_clk))
1027 return PTR_ERR(sdcp->dcp_clk);
1029 /* Restart the DCP block. */
1030 ret = stmp_reset_block(sdcp->base);
1032 dev_err(dev, "Failed reset\n");
1036 /* Initialize control register. */
1037 writel(MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES |
1038 MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING | 0xf,
1039 sdcp->base + MXS_DCP_CTRL);
1041 /* Enable all DCP DMA channels. */
1042 writel(MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK,
1043 sdcp->base + MXS_DCP_CHANNELCTRL);
1046 * We do not enable context switching. Give the context buffer a
1047 * pointer to an illegal address so if context switching is
1048 * inadvertantly enabled, the DCP will return an error instead of
1049 * trashing good memory. The DCP DMA cannot access ROM, so any ROM
1052 writel(0xffff0000, sdcp->base + MXS_DCP_CONTEXT);
1053 for (i = 0; i < DCP_MAX_CHANS; i++)
1054 writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(i));
1055 writel(0xffffffff, sdcp->base + MXS_DCP_STAT_CLR);
1059 platform_set_drvdata(pdev, sdcp);
1061 for (i = 0; i < DCP_MAX_CHANS; i++) {
1062 spin_lock_init(&sdcp->lock[i]);
1063 init_completion(&sdcp->completion[i]);
1064 crypto_init_queue(&sdcp->queue[i], 50);
1067 /* Create the SHA and AES handler threads. */
1068 sdcp->thread[DCP_CHAN_HASH_SHA] = kthread_run(dcp_chan_thread_sha,
1069 NULL, "mxs_dcp_chan/sha");
1070 if (IS_ERR(sdcp->thread[DCP_CHAN_HASH_SHA])) {
1071 dev_err(dev, "Error starting SHA thread!\n");
1072 ret = PTR_ERR(sdcp->thread[DCP_CHAN_HASH_SHA]);
1076 sdcp->thread[DCP_CHAN_CRYPTO] = kthread_run(dcp_chan_thread_aes,
1077 NULL, "mxs_dcp_chan/aes");
1078 if (IS_ERR(sdcp->thread[DCP_CHAN_CRYPTO])) {
1079 dev_err(dev, "Error starting SHA thread!\n");
1080 ret = PTR_ERR(sdcp->thread[DCP_CHAN_CRYPTO]);
1081 goto err_destroy_sha_thread;
1084 /* Register the various crypto algorithms. */
1085 sdcp->caps = readl(sdcp->base + MXS_DCP_CAPABILITY1);
1087 if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128) {
1088 ret = crypto_register_skciphers(dcp_aes_algs,
1089 ARRAY_SIZE(dcp_aes_algs));
1091 /* Failed to register algorithm. */
1092 dev_err(dev, "Failed to register AES crypto!\n");
1093 goto err_destroy_aes_thread;
1097 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1) {
1098 ret = crypto_register_ahash(&dcp_sha1_alg);
1100 dev_err(dev, "Failed to register %s hash!\n",
1101 dcp_sha1_alg.halg.base.cra_name);
1102 goto err_unregister_aes;
1106 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256) {
1107 ret = crypto_register_ahash(&dcp_sha256_alg);
1109 dev_err(dev, "Failed to register %s hash!\n",
1110 dcp_sha256_alg.halg.base.cra_name);
1111 goto err_unregister_sha1;
1117 err_unregister_sha1:
1118 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1)
1119 crypto_unregister_ahash(&dcp_sha1_alg);
1122 if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128)
1123 crypto_unregister_skciphers(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs));
1125 err_destroy_aes_thread:
1126 kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]);
1128 err_destroy_sha_thread:
1129 kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]);
1134 static int mxs_dcp_remove(struct platform_device *pdev)
1136 struct dcp *sdcp = platform_get_drvdata(pdev);
1138 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256)
1139 crypto_unregister_ahash(&dcp_sha256_alg);
1141 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1)
1142 crypto_unregister_ahash(&dcp_sha1_alg);
1144 if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128)
1145 crypto_unregister_skciphers(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs));
1147 kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]);
1148 kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]);
1150 platform_set_drvdata(pdev, NULL);
1157 static const struct of_device_id mxs_dcp_dt_ids[] = {
1158 { .compatible = "fsl,imx23-dcp", .data = NULL, },
1159 { .compatible = "fsl,imx28-dcp", .data = NULL, },
1163 MODULE_DEVICE_TABLE(of, mxs_dcp_dt_ids);
1165 static struct platform_driver mxs_dcp_driver = {
1166 .probe = mxs_dcp_probe,
1167 .remove = mxs_dcp_remove,
1170 .of_match_table = mxs_dcp_dt_ids,
1174 module_platform_driver(mxs_dcp_driver);
1176 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
1177 MODULE_DESCRIPTION("Freescale MXS DCP Driver");
1178 MODULE_LICENSE("GPL");
1179 MODULE_ALIAS("platform:mxs-dcp");