1 // SPDX-License-Identifier: GPL-2.0
5 * Support for ATMEL DES/TDES HW acceleration.
7 * Copyright (c) 2012 Eukréa Electromatique - ATMEL
8 * Author: Nicolas Royer <nicolas@eukrea.com>
10 * Some ideas are from omap-aes.c drivers.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/err.h>
18 #include <linux/clk.h>
20 #include <linux/hw_random.h>
21 #include <linux/platform_device.h>
23 #include <linux/device.h>
24 #include <linux/dmaengine.h>
25 #include <linux/init.h>
26 #include <linux/errno.h>
27 #include <linux/interrupt.h>
28 #include <linux/irq.h>
29 #include <linux/scatterlist.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/of_device.h>
32 #include <linux/delay.h>
33 #include <linux/crypto.h>
34 #include <crypto/scatterwalk.h>
35 #include <crypto/algapi.h>
36 #include <crypto/internal/des.h>
37 #include <crypto/internal/skcipher.h>
38 #include "atmel-tdes-regs.h"
40 #define ATMEL_TDES_PRIORITY 300
43 /* Reserve bits [17:16], [13:12], [2:0] for AES Mode Register */
44 #define TDES_FLAGS_ENCRYPT TDES_MR_CYPHER_ENC
45 #define TDES_FLAGS_OPMODE_MASK (TDES_MR_OPMOD_MASK | TDES_MR_CFBS_MASK)
46 #define TDES_FLAGS_ECB TDES_MR_OPMOD_ECB
47 #define TDES_FLAGS_CBC TDES_MR_OPMOD_CBC
48 #define TDES_FLAGS_OFB TDES_MR_OPMOD_OFB
49 #define TDES_FLAGS_CFB64 (TDES_MR_OPMOD_CFB | TDES_MR_CFBS_64b)
50 #define TDES_FLAGS_CFB32 (TDES_MR_OPMOD_CFB | TDES_MR_CFBS_32b)
51 #define TDES_FLAGS_CFB16 (TDES_MR_OPMOD_CFB | TDES_MR_CFBS_16b)
52 #define TDES_FLAGS_CFB8 (TDES_MR_OPMOD_CFB | TDES_MR_CFBS_8b)
54 #define TDES_FLAGS_MODE_MASK (TDES_FLAGS_OPMODE_MASK | TDES_FLAGS_ENCRYPT)
56 #define TDES_FLAGS_INIT BIT(3)
57 #define TDES_FLAGS_FAST BIT(4)
58 #define TDES_FLAGS_BUSY BIT(5)
59 #define TDES_FLAGS_DMA BIT(6)
61 #define ATMEL_TDES_QUEUE_LENGTH 50
63 #define CFB8_BLOCK_SIZE 1
64 #define CFB16_BLOCK_SIZE 2
65 #define CFB32_BLOCK_SIZE 4
67 struct atmel_tdes_caps {
72 struct atmel_tdes_dev;
74 struct atmel_tdes_ctx {
75 struct atmel_tdes_dev *dd;
78 u32 key[DES3_EDE_KEY_SIZE / sizeof(u32)];
84 struct atmel_tdes_reqctx {
86 u8 lastc[DES_BLOCK_SIZE];
89 struct atmel_tdes_dma {
90 struct dma_chan *chan;
91 struct dma_slave_config dma_conf;
94 struct atmel_tdes_dev {
95 struct list_head list;
96 unsigned long phys_base;
97 void __iomem *io_base;
99 struct atmel_tdes_ctx *ctx;
107 struct crypto_queue queue;
109 struct tasklet_struct done_task;
110 struct tasklet_struct queue_task;
112 struct skcipher_request *req;
115 struct scatterlist *in_sg;
116 unsigned int nb_in_sg;
118 struct scatterlist *out_sg;
119 unsigned int nb_out_sg;
127 dma_addr_t dma_addr_in;
128 struct atmel_tdes_dma dma_lch_in;
132 dma_addr_t dma_addr_out;
133 struct atmel_tdes_dma dma_lch_out;
135 struct atmel_tdes_caps caps;
140 struct atmel_tdes_drv {
141 struct list_head dev_list;
145 static struct atmel_tdes_drv atmel_tdes = {
146 .dev_list = LIST_HEAD_INIT(atmel_tdes.dev_list),
147 .lock = __SPIN_LOCK_UNLOCKED(atmel_tdes.lock),
150 static int atmel_tdes_sg_copy(struct scatterlist **sg, size_t *offset,
151 void *buf, size_t buflen, size_t total, int out)
153 size_t count, off = 0;
155 while (buflen && total) {
156 count = min((*sg)->length - *offset, total);
157 count = min(count, buflen);
162 scatterwalk_map_and_copy(buf + off, *sg, *offset, count, out);
169 if (*offset == (*sg)->length) {
181 static inline u32 atmel_tdes_read(struct atmel_tdes_dev *dd, u32 offset)
183 return readl_relaxed(dd->io_base + offset);
186 static inline void atmel_tdes_write(struct atmel_tdes_dev *dd,
187 u32 offset, u32 value)
189 writel_relaxed(value, dd->io_base + offset);
192 static void atmel_tdes_write_n(struct atmel_tdes_dev *dd, u32 offset,
193 const u32 *value, int count)
195 for (; count--; value++, offset += 4)
196 atmel_tdes_write(dd, offset, *value);
199 static struct atmel_tdes_dev *atmel_tdes_dev_alloc(void)
201 struct atmel_tdes_dev *tdes_dd;
203 spin_lock_bh(&atmel_tdes.lock);
204 /* One TDES IP per SoC. */
205 tdes_dd = list_first_entry_or_null(&atmel_tdes.dev_list,
206 struct atmel_tdes_dev, list);
207 spin_unlock_bh(&atmel_tdes.lock);
211 static int atmel_tdes_hw_init(struct atmel_tdes_dev *dd)
215 err = clk_prepare_enable(dd->iclk);
219 if (!(dd->flags & TDES_FLAGS_INIT)) {
220 atmel_tdes_write(dd, TDES_CR, TDES_CR_SWRST);
221 dd->flags |= TDES_FLAGS_INIT;
227 static inline unsigned int atmel_tdes_get_version(struct atmel_tdes_dev *dd)
229 return atmel_tdes_read(dd, TDES_HW_VERSION) & 0x00000fff;
232 static int atmel_tdes_hw_version_init(struct atmel_tdes_dev *dd)
236 err = atmel_tdes_hw_init(dd);
240 dd->hw_version = atmel_tdes_get_version(dd);
243 "version: 0x%x\n", dd->hw_version);
245 clk_disable_unprepare(dd->iclk);
250 static void atmel_tdes_dma_callback(void *data)
252 struct atmel_tdes_dev *dd = data;
254 /* dma_lch_out - completed */
255 tasklet_schedule(&dd->done_task);
258 static int atmel_tdes_write_ctrl(struct atmel_tdes_dev *dd)
261 u32 valmr = TDES_MR_SMOD_PDC;
263 err = atmel_tdes_hw_init(dd);
268 if (!dd->caps.has_dma)
269 atmel_tdes_write(dd, TDES_PTCR,
270 TDES_PTCR_TXTDIS | TDES_PTCR_RXTDIS);
272 /* MR register must be set before IV registers */
273 if (dd->ctx->keylen > (DES_KEY_SIZE << 1)) {
274 valmr |= TDES_MR_KEYMOD_3KEY;
275 valmr |= TDES_MR_TDESMOD_TDES;
276 } else if (dd->ctx->keylen > DES_KEY_SIZE) {
277 valmr |= TDES_MR_KEYMOD_2KEY;
278 valmr |= TDES_MR_TDESMOD_TDES;
280 valmr |= TDES_MR_TDESMOD_DES;
283 valmr |= dd->flags & TDES_FLAGS_MODE_MASK;
285 atmel_tdes_write(dd, TDES_MR, valmr);
287 atmel_tdes_write_n(dd, TDES_KEY1W1R, dd->ctx->key,
288 dd->ctx->keylen >> 2);
290 if (dd->req->iv && (valmr & TDES_MR_OPMOD_MASK) != TDES_MR_OPMOD_ECB)
291 atmel_tdes_write_n(dd, TDES_IV1R, (void *)dd->req->iv, 2);
296 static int atmel_tdes_crypt_pdc_stop(struct atmel_tdes_dev *dd)
301 atmel_tdes_write(dd, TDES_PTCR, TDES_PTCR_TXTDIS|TDES_PTCR_RXTDIS);
303 if (dd->flags & TDES_FLAGS_FAST) {
304 dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_FROM_DEVICE);
305 dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
307 dma_sync_single_for_device(dd->dev, dd->dma_addr_out,
308 dd->dma_size, DMA_FROM_DEVICE);
311 count = atmel_tdes_sg_copy(&dd->out_sg, &dd->out_offset,
312 dd->buf_out, dd->buflen, dd->dma_size, 1);
313 if (count != dd->dma_size) {
315 dev_dbg(dd->dev, "not all data converted: %zu\n", count);
322 static int atmel_tdes_buff_init(struct atmel_tdes_dev *dd)
326 dd->buf_in = (void *)__get_free_pages(GFP_KERNEL, 0);
327 dd->buf_out = (void *)__get_free_pages(GFP_KERNEL, 0);
328 dd->buflen = PAGE_SIZE;
329 dd->buflen &= ~(DES_BLOCK_SIZE - 1);
331 if (!dd->buf_in || !dd->buf_out) {
332 dev_dbg(dd->dev, "unable to alloc pages.\n");
337 dd->dma_addr_in = dma_map_single(dd->dev, dd->buf_in,
338 dd->buflen, DMA_TO_DEVICE);
339 err = dma_mapping_error(dd->dev, dd->dma_addr_in);
341 dev_dbg(dd->dev, "dma %zd bytes error\n", dd->buflen);
345 dd->dma_addr_out = dma_map_single(dd->dev, dd->buf_out,
346 dd->buflen, DMA_FROM_DEVICE);
347 err = dma_mapping_error(dd->dev, dd->dma_addr_out);
349 dev_dbg(dd->dev, "dma %zd bytes error\n", dd->buflen);
356 dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen,
360 free_page((unsigned long)dd->buf_out);
361 free_page((unsigned long)dd->buf_in);
365 static void atmel_tdes_buff_cleanup(struct atmel_tdes_dev *dd)
367 dma_unmap_single(dd->dev, dd->dma_addr_out, dd->buflen,
369 dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen,
371 free_page((unsigned long)dd->buf_out);
372 free_page((unsigned long)dd->buf_in);
375 static int atmel_tdes_crypt_pdc(struct atmel_tdes_dev *dd,
376 dma_addr_t dma_addr_in,
377 dma_addr_t dma_addr_out, int length)
379 struct atmel_tdes_reqctx *rctx = skcipher_request_ctx(dd->req);
382 dd->dma_size = length;
384 if (!(dd->flags & TDES_FLAGS_FAST)) {
385 dma_sync_single_for_device(dd->dev, dma_addr_in, length,
389 switch (rctx->mode & TDES_FLAGS_OPMODE_MASK) {
390 case TDES_FLAGS_CFB8:
391 len32 = DIV_ROUND_UP(length, sizeof(u8));
394 case TDES_FLAGS_CFB16:
395 len32 = DIV_ROUND_UP(length, sizeof(u16));
399 len32 = DIV_ROUND_UP(length, sizeof(u32));
403 atmel_tdes_write(dd, TDES_PTCR, TDES_PTCR_TXTDIS|TDES_PTCR_RXTDIS);
404 atmel_tdes_write(dd, TDES_TPR, dma_addr_in);
405 atmel_tdes_write(dd, TDES_TCR, len32);
406 atmel_tdes_write(dd, TDES_RPR, dma_addr_out);
407 atmel_tdes_write(dd, TDES_RCR, len32);
409 /* Enable Interrupt */
410 atmel_tdes_write(dd, TDES_IER, TDES_INT_ENDRX);
412 /* Start DMA transfer */
413 atmel_tdes_write(dd, TDES_PTCR, TDES_PTCR_TXTEN | TDES_PTCR_RXTEN);
418 static int atmel_tdes_crypt_dma(struct atmel_tdes_dev *dd,
419 dma_addr_t dma_addr_in,
420 dma_addr_t dma_addr_out, int length)
422 struct atmel_tdes_reqctx *rctx = skcipher_request_ctx(dd->req);
423 struct scatterlist sg[2];
424 struct dma_async_tx_descriptor *in_desc, *out_desc;
425 enum dma_slave_buswidth addr_width;
427 dd->dma_size = length;
429 if (!(dd->flags & TDES_FLAGS_FAST)) {
430 dma_sync_single_for_device(dd->dev, dma_addr_in, length,
434 switch (rctx->mode & TDES_FLAGS_OPMODE_MASK) {
435 case TDES_FLAGS_CFB8:
436 addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
439 case TDES_FLAGS_CFB16:
440 addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
444 addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
448 dd->dma_lch_in.dma_conf.dst_addr_width = addr_width;
449 dd->dma_lch_out.dma_conf.src_addr_width = addr_width;
451 dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
452 dmaengine_slave_config(dd->dma_lch_out.chan, &dd->dma_lch_out.dma_conf);
454 dd->flags |= TDES_FLAGS_DMA;
456 sg_init_table(&sg[0], 1);
457 sg_dma_address(&sg[0]) = dma_addr_in;
458 sg_dma_len(&sg[0]) = length;
460 sg_init_table(&sg[1], 1);
461 sg_dma_address(&sg[1]) = dma_addr_out;
462 sg_dma_len(&sg[1]) = length;
464 in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, &sg[0],
466 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
470 out_desc = dmaengine_prep_slave_sg(dd->dma_lch_out.chan, &sg[1],
472 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
476 out_desc->callback = atmel_tdes_dma_callback;
477 out_desc->callback_param = dd;
479 dmaengine_submit(out_desc);
480 dma_async_issue_pending(dd->dma_lch_out.chan);
482 dmaengine_submit(in_desc);
483 dma_async_issue_pending(dd->dma_lch_in.chan);
488 static int atmel_tdes_crypt_start(struct atmel_tdes_dev *dd)
490 int err, fast = 0, in, out;
492 dma_addr_t addr_in, addr_out;
494 if ((!dd->in_offset) && (!dd->out_offset)) {
495 /* check for alignment */
496 in = IS_ALIGNED((u32)dd->in_sg->offset, sizeof(u32)) &&
497 IS_ALIGNED(dd->in_sg->length, dd->ctx->block_size);
498 out = IS_ALIGNED((u32)dd->out_sg->offset, sizeof(u32)) &&
499 IS_ALIGNED(dd->out_sg->length, dd->ctx->block_size);
502 if (sg_dma_len(dd->in_sg) != sg_dma_len(dd->out_sg))
508 count = min_t(size_t, dd->total, sg_dma_len(dd->in_sg));
509 count = min_t(size_t, count, sg_dma_len(dd->out_sg));
511 err = dma_map_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
513 dev_dbg(dd->dev, "dma_map_sg() error\n");
517 err = dma_map_sg(dd->dev, dd->out_sg, 1,
520 dev_dbg(dd->dev, "dma_map_sg() error\n");
521 dma_unmap_sg(dd->dev, dd->in_sg, 1,
526 addr_in = sg_dma_address(dd->in_sg);
527 addr_out = sg_dma_address(dd->out_sg);
529 dd->flags |= TDES_FLAGS_FAST;
532 /* use cache buffers */
533 count = atmel_tdes_sg_copy(&dd->in_sg, &dd->in_offset,
534 dd->buf_in, dd->buflen, dd->total, 0);
536 addr_in = dd->dma_addr_in;
537 addr_out = dd->dma_addr_out;
539 dd->flags &= ~TDES_FLAGS_FAST;
544 if (dd->caps.has_dma)
545 err = atmel_tdes_crypt_dma(dd, addr_in, addr_out, count);
547 err = atmel_tdes_crypt_pdc(dd, addr_in, addr_out, count);
549 if (err && (dd->flags & TDES_FLAGS_FAST)) {
550 dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
551 dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_TO_DEVICE);
558 atmel_tdes_set_iv_as_last_ciphertext_block(struct atmel_tdes_dev *dd)
560 struct skcipher_request *req = dd->req;
561 struct atmel_tdes_reqctx *rctx = skcipher_request_ctx(req);
562 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
563 unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
565 if (req->cryptlen < ivsize)
568 if (rctx->mode & TDES_FLAGS_ENCRYPT) {
569 scatterwalk_map_and_copy(req->iv, req->dst,
570 req->cryptlen - ivsize, ivsize, 0);
572 if (req->src == req->dst)
573 memcpy(req->iv, rctx->lastc, ivsize);
575 scatterwalk_map_and_copy(req->iv, req->src,
576 req->cryptlen - ivsize,
581 static void atmel_tdes_finish_req(struct atmel_tdes_dev *dd, int err)
583 struct skcipher_request *req = dd->req;
584 struct atmel_tdes_reqctx *rctx = skcipher_request_ctx(req);
586 clk_disable_unprepare(dd->iclk);
588 dd->flags &= ~TDES_FLAGS_BUSY;
590 if (!err && (rctx->mode & TDES_FLAGS_OPMODE_MASK) != TDES_FLAGS_ECB)
591 atmel_tdes_set_iv_as_last_ciphertext_block(dd);
593 req->base.complete(&req->base, err);
596 static int atmel_tdes_handle_queue(struct atmel_tdes_dev *dd,
597 struct skcipher_request *req)
599 struct crypto_async_request *async_req, *backlog;
600 struct atmel_tdes_ctx *ctx;
601 struct atmel_tdes_reqctx *rctx;
605 spin_lock_irqsave(&dd->lock, flags);
607 ret = crypto_enqueue_request(&dd->queue, &req->base);
608 if (dd->flags & TDES_FLAGS_BUSY) {
609 spin_unlock_irqrestore(&dd->lock, flags);
612 backlog = crypto_get_backlog(&dd->queue);
613 async_req = crypto_dequeue_request(&dd->queue);
615 dd->flags |= TDES_FLAGS_BUSY;
616 spin_unlock_irqrestore(&dd->lock, flags);
622 backlog->complete(backlog, -EINPROGRESS);
624 req = skcipher_request_cast(async_req);
626 /* assign new request to device */
628 dd->total = req->cryptlen;
630 dd->in_sg = req->src;
632 dd->out_sg = req->dst;
634 rctx = skcipher_request_ctx(req);
635 ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
636 rctx->mode &= TDES_FLAGS_MODE_MASK;
637 dd->flags = (dd->flags & ~TDES_FLAGS_MODE_MASK) | rctx->mode;
640 err = atmel_tdes_write_ctrl(dd);
642 err = atmel_tdes_crypt_start(dd);
644 /* des_task will not finish it, so do it here */
645 atmel_tdes_finish_req(dd, err);
646 tasklet_schedule(&dd->queue_task);
652 static int atmel_tdes_crypt_dma_stop(struct atmel_tdes_dev *dd)
657 if (dd->flags & TDES_FLAGS_DMA) {
659 if (dd->flags & TDES_FLAGS_FAST) {
660 dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_FROM_DEVICE);
661 dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
663 dma_sync_single_for_device(dd->dev, dd->dma_addr_out,
664 dd->dma_size, DMA_FROM_DEVICE);
667 count = atmel_tdes_sg_copy(&dd->out_sg, &dd->out_offset,
668 dd->buf_out, dd->buflen, dd->dma_size, 1);
669 if (count != dd->dma_size) {
671 dev_dbg(dd->dev, "not all data converted: %zu\n", count);
678 static int atmel_tdes_crypt(struct skcipher_request *req, unsigned long mode)
680 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
681 struct atmel_tdes_ctx *ctx = crypto_skcipher_ctx(skcipher);
682 struct atmel_tdes_reqctx *rctx = skcipher_request_ctx(req);
683 struct device *dev = ctx->dd->dev;
688 switch (mode & TDES_FLAGS_OPMODE_MASK) {
689 case TDES_FLAGS_CFB8:
690 if (!IS_ALIGNED(req->cryptlen, CFB8_BLOCK_SIZE)) {
691 dev_dbg(dev, "request size is not exact amount of CFB8 blocks\n");
694 ctx->block_size = CFB8_BLOCK_SIZE;
697 case TDES_FLAGS_CFB16:
698 if (!IS_ALIGNED(req->cryptlen, CFB16_BLOCK_SIZE)) {
699 dev_dbg(dev, "request size is not exact amount of CFB16 blocks\n");
702 ctx->block_size = CFB16_BLOCK_SIZE;
705 case TDES_FLAGS_CFB32:
706 if (!IS_ALIGNED(req->cryptlen, CFB32_BLOCK_SIZE)) {
707 dev_dbg(dev, "request size is not exact amount of CFB32 blocks\n");
710 ctx->block_size = CFB32_BLOCK_SIZE;
714 if (!IS_ALIGNED(req->cryptlen, DES_BLOCK_SIZE)) {
715 dev_dbg(dev, "request size is not exact amount of DES blocks\n");
718 ctx->block_size = DES_BLOCK_SIZE;
724 if ((mode & TDES_FLAGS_OPMODE_MASK) != TDES_FLAGS_ECB &&
725 !(mode & TDES_FLAGS_ENCRYPT) && req->src == req->dst) {
726 unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
728 if (req->cryptlen >= ivsize)
729 scatterwalk_map_and_copy(rctx->lastc, req->src,
730 req->cryptlen - ivsize,
734 return atmel_tdes_handle_queue(ctx->dd, req);
737 static int atmel_tdes_dma_init(struct atmel_tdes_dev *dd)
741 /* Try to grab 2 DMA channels */
742 dd->dma_lch_in.chan = dma_request_chan(dd->dev, "tx");
743 if (IS_ERR(dd->dma_lch_in.chan)) {
744 ret = PTR_ERR(dd->dma_lch_in.chan);
748 dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
750 dd->dma_lch_in.dma_conf.src_maxburst = 1;
751 dd->dma_lch_in.dma_conf.src_addr_width =
752 DMA_SLAVE_BUSWIDTH_4_BYTES;
753 dd->dma_lch_in.dma_conf.dst_maxburst = 1;
754 dd->dma_lch_in.dma_conf.dst_addr_width =
755 DMA_SLAVE_BUSWIDTH_4_BYTES;
756 dd->dma_lch_in.dma_conf.device_fc = false;
758 dd->dma_lch_out.chan = dma_request_chan(dd->dev, "rx");
759 if (IS_ERR(dd->dma_lch_out.chan)) {
760 ret = PTR_ERR(dd->dma_lch_out.chan);
764 dd->dma_lch_out.dma_conf.src_addr = dd->phys_base +
766 dd->dma_lch_out.dma_conf.src_maxburst = 1;
767 dd->dma_lch_out.dma_conf.src_addr_width =
768 DMA_SLAVE_BUSWIDTH_4_BYTES;
769 dd->dma_lch_out.dma_conf.dst_maxburst = 1;
770 dd->dma_lch_out.dma_conf.dst_addr_width =
771 DMA_SLAVE_BUSWIDTH_4_BYTES;
772 dd->dma_lch_out.dma_conf.device_fc = false;
777 dma_release_channel(dd->dma_lch_in.chan);
779 dev_err(dd->dev, "no DMA channel available\n");
783 static void atmel_tdes_dma_cleanup(struct atmel_tdes_dev *dd)
785 dma_release_channel(dd->dma_lch_in.chan);
786 dma_release_channel(dd->dma_lch_out.chan);
789 static int atmel_des_setkey(struct crypto_skcipher *tfm, const u8 *key,
792 struct atmel_tdes_ctx *ctx = crypto_skcipher_ctx(tfm);
795 err = verify_skcipher_des_key(tfm, key);
799 memcpy(ctx->key, key, keylen);
800 ctx->keylen = keylen;
805 static int atmel_tdes_setkey(struct crypto_skcipher *tfm, const u8 *key,
808 struct atmel_tdes_ctx *ctx = crypto_skcipher_ctx(tfm);
811 err = verify_skcipher_des3_key(tfm, key);
815 memcpy(ctx->key, key, keylen);
816 ctx->keylen = keylen;
821 static int atmel_tdes_ecb_encrypt(struct skcipher_request *req)
823 return atmel_tdes_crypt(req, TDES_FLAGS_ECB | TDES_FLAGS_ENCRYPT);
826 static int atmel_tdes_ecb_decrypt(struct skcipher_request *req)
828 return atmel_tdes_crypt(req, TDES_FLAGS_ECB);
831 static int atmel_tdes_cbc_encrypt(struct skcipher_request *req)
833 return atmel_tdes_crypt(req, TDES_FLAGS_CBC | TDES_FLAGS_ENCRYPT);
836 static int atmel_tdes_cbc_decrypt(struct skcipher_request *req)
838 return atmel_tdes_crypt(req, TDES_FLAGS_CBC);
840 static int atmel_tdes_cfb_encrypt(struct skcipher_request *req)
842 return atmel_tdes_crypt(req, TDES_FLAGS_CFB64 | TDES_FLAGS_ENCRYPT);
845 static int atmel_tdes_cfb_decrypt(struct skcipher_request *req)
847 return atmel_tdes_crypt(req, TDES_FLAGS_CFB64);
850 static int atmel_tdes_cfb8_encrypt(struct skcipher_request *req)
852 return atmel_tdes_crypt(req, TDES_FLAGS_CFB8 | TDES_FLAGS_ENCRYPT);
855 static int atmel_tdes_cfb8_decrypt(struct skcipher_request *req)
857 return atmel_tdes_crypt(req, TDES_FLAGS_CFB8);
860 static int atmel_tdes_cfb16_encrypt(struct skcipher_request *req)
862 return atmel_tdes_crypt(req, TDES_FLAGS_CFB16 | TDES_FLAGS_ENCRYPT);
865 static int atmel_tdes_cfb16_decrypt(struct skcipher_request *req)
867 return atmel_tdes_crypt(req, TDES_FLAGS_CFB16);
870 static int atmel_tdes_cfb32_encrypt(struct skcipher_request *req)
872 return atmel_tdes_crypt(req, TDES_FLAGS_CFB32 | TDES_FLAGS_ENCRYPT);
875 static int atmel_tdes_cfb32_decrypt(struct skcipher_request *req)
877 return atmel_tdes_crypt(req, TDES_FLAGS_CFB32);
880 static int atmel_tdes_ofb_encrypt(struct skcipher_request *req)
882 return atmel_tdes_crypt(req, TDES_FLAGS_OFB | TDES_FLAGS_ENCRYPT);
885 static int atmel_tdes_ofb_decrypt(struct skcipher_request *req)
887 return atmel_tdes_crypt(req, TDES_FLAGS_OFB);
890 static int atmel_tdes_init_tfm(struct crypto_skcipher *tfm)
892 struct atmel_tdes_ctx *ctx = crypto_skcipher_ctx(tfm);
894 ctx->dd = atmel_tdes_dev_alloc();
898 crypto_skcipher_set_reqsize(tfm, sizeof(struct atmel_tdes_reqctx));
903 static void atmel_tdes_skcipher_alg_init(struct skcipher_alg *alg)
905 alg->base.cra_priority = ATMEL_TDES_PRIORITY;
906 alg->base.cra_flags = CRYPTO_ALG_ASYNC;
907 alg->base.cra_ctxsize = sizeof(struct atmel_tdes_ctx);
908 alg->base.cra_module = THIS_MODULE;
910 alg->init = atmel_tdes_init_tfm;
913 static struct skcipher_alg tdes_algs[] = {
915 .base.cra_name = "ecb(des)",
916 .base.cra_driver_name = "atmel-ecb-des",
917 .base.cra_blocksize = DES_BLOCK_SIZE,
918 .base.cra_alignmask = 0x7,
920 .min_keysize = DES_KEY_SIZE,
921 .max_keysize = DES_KEY_SIZE,
922 .setkey = atmel_des_setkey,
923 .encrypt = atmel_tdes_ecb_encrypt,
924 .decrypt = atmel_tdes_ecb_decrypt,
927 .base.cra_name = "cbc(des)",
928 .base.cra_driver_name = "atmel-cbc-des",
929 .base.cra_blocksize = DES_BLOCK_SIZE,
930 .base.cra_alignmask = 0x7,
932 .min_keysize = DES_KEY_SIZE,
933 .max_keysize = DES_KEY_SIZE,
934 .ivsize = DES_BLOCK_SIZE,
935 .setkey = atmel_des_setkey,
936 .encrypt = atmel_tdes_cbc_encrypt,
937 .decrypt = atmel_tdes_cbc_decrypt,
940 .base.cra_name = "cfb(des)",
941 .base.cra_driver_name = "atmel-cfb-des",
942 .base.cra_blocksize = DES_BLOCK_SIZE,
943 .base.cra_alignmask = 0x7,
945 .min_keysize = DES_KEY_SIZE,
946 .max_keysize = DES_KEY_SIZE,
947 .ivsize = DES_BLOCK_SIZE,
948 .setkey = atmel_des_setkey,
949 .encrypt = atmel_tdes_cfb_encrypt,
950 .decrypt = atmel_tdes_cfb_decrypt,
953 .base.cra_name = "cfb8(des)",
954 .base.cra_driver_name = "atmel-cfb8-des",
955 .base.cra_blocksize = CFB8_BLOCK_SIZE,
956 .base.cra_alignmask = 0,
958 .min_keysize = DES_KEY_SIZE,
959 .max_keysize = DES_KEY_SIZE,
960 .ivsize = DES_BLOCK_SIZE,
961 .setkey = atmel_des_setkey,
962 .encrypt = atmel_tdes_cfb8_encrypt,
963 .decrypt = atmel_tdes_cfb8_decrypt,
966 .base.cra_name = "cfb16(des)",
967 .base.cra_driver_name = "atmel-cfb16-des",
968 .base.cra_blocksize = CFB16_BLOCK_SIZE,
969 .base.cra_alignmask = 0x1,
971 .min_keysize = DES_KEY_SIZE,
972 .max_keysize = DES_KEY_SIZE,
973 .ivsize = DES_BLOCK_SIZE,
974 .setkey = atmel_des_setkey,
975 .encrypt = atmel_tdes_cfb16_encrypt,
976 .decrypt = atmel_tdes_cfb16_decrypt,
979 .base.cra_name = "cfb32(des)",
980 .base.cra_driver_name = "atmel-cfb32-des",
981 .base.cra_blocksize = CFB32_BLOCK_SIZE,
982 .base.cra_alignmask = 0x3,
984 .min_keysize = DES_KEY_SIZE,
985 .max_keysize = DES_KEY_SIZE,
986 .ivsize = DES_BLOCK_SIZE,
987 .setkey = atmel_des_setkey,
988 .encrypt = atmel_tdes_cfb32_encrypt,
989 .decrypt = atmel_tdes_cfb32_decrypt,
992 .base.cra_name = "ofb(des)",
993 .base.cra_driver_name = "atmel-ofb-des",
994 .base.cra_blocksize = 1,
995 .base.cra_alignmask = 0x7,
997 .min_keysize = DES_KEY_SIZE,
998 .max_keysize = DES_KEY_SIZE,
999 .ivsize = DES_BLOCK_SIZE,
1000 .setkey = atmel_des_setkey,
1001 .encrypt = atmel_tdes_ofb_encrypt,
1002 .decrypt = atmel_tdes_ofb_decrypt,
1005 .base.cra_name = "ecb(des3_ede)",
1006 .base.cra_driver_name = "atmel-ecb-tdes",
1007 .base.cra_blocksize = DES_BLOCK_SIZE,
1008 .base.cra_alignmask = 0x7,
1010 .min_keysize = DES3_EDE_KEY_SIZE,
1011 .max_keysize = DES3_EDE_KEY_SIZE,
1012 .setkey = atmel_tdes_setkey,
1013 .encrypt = atmel_tdes_ecb_encrypt,
1014 .decrypt = atmel_tdes_ecb_decrypt,
1017 .base.cra_name = "cbc(des3_ede)",
1018 .base.cra_driver_name = "atmel-cbc-tdes",
1019 .base.cra_blocksize = DES_BLOCK_SIZE,
1020 .base.cra_alignmask = 0x7,
1022 .min_keysize = DES3_EDE_KEY_SIZE,
1023 .max_keysize = DES3_EDE_KEY_SIZE,
1024 .setkey = atmel_tdes_setkey,
1025 .encrypt = atmel_tdes_cbc_encrypt,
1026 .decrypt = atmel_tdes_cbc_decrypt,
1027 .ivsize = DES_BLOCK_SIZE,
1030 .base.cra_name = "ofb(des3_ede)",
1031 .base.cra_driver_name = "atmel-ofb-tdes",
1032 .base.cra_blocksize = DES_BLOCK_SIZE,
1033 .base.cra_alignmask = 0x7,
1035 .min_keysize = DES3_EDE_KEY_SIZE,
1036 .max_keysize = DES3_EDE_KEY_SIZE,
1037 .setkey = atmel_tdes_setkey,
1038 .encrypt = atmel_tdes_ofb_encrypt,
1039 .decrypt = atmel_tdes_ofb_decrypt,
1040 .ivsize = DES_BLOCK_SIZE,
1044 static void atmel_tdes_queue_task(unsigned long data)
1046 struct atmel_tdes_dev *dd = (struct atmel_tdes_dev *)data;
1048 atmel_tdes_handle_queue(dd, NULL);
1051 static void atmel_tdes_done_task(unsigned long data)
1053 struct atmel_tdes_dev *dd = (struct atmel_tdes_dev *) data;
1056 if (!(dd->flags & TDES_FLAGS_DMA))
1057 err = atmel_tdes_crypt_pdc_stop(dd);
1059 err = atmel_tdes_crypt_dma_stop(dd);
1061 if (dd->total && !err) {
1062 if (dd->flags & TDES_FLAGS_FAST) {
1063 dd->in_sg = sg_next(dd->in_sg);
1064 dd->out_sg = sg_next(dd->out_sg);
1065 if (!dd->in_sg || !dd->out_sg)
1069 err = atmel_tdes_crypt_start(dd);
1071 return; /* DMA started. Not fininishing. */
1074 atmel_tdes_finish_req(dd, err);
1075 atmel_tdes_handle_queue(dd, NULL);
1078 static irqreturn_t atmel_tdes_irq(int irq, void *dev_id)
1080 struct atmel_tdes_dev *tdes_dd = dev_id;
1083 reg = atmel_tdes_read(tdes_dd, TDES_ISR);
1084 if (reg & atmel_tdes_read(tdes_dd, TDES_IMR)) {
1085 atmel_tdes_write(tdes_dd, TDES_IDR, reg);
1086 if (TDES_FLAGS_BUSY & tdes_dd->flags)
1087 tasklet_schedule(&tdes_dd->done_task);
1089 dev_warn(tdes_dd->dev, "TDES interrupt when no active requests.\n");
1096 static void atmel_tdes_unregister_algs(struct atmel_tdes_dev *dd)
1100 for (i = 0; i < ARRAY_SIZE(tdes_algs); i++)
1101 crypto_unregister_skcipher(&tdes_algs[i]);
1104 static int atmel_tdes_register_algs(struct atmel_tdes_dev *dd)
1108 for (i = 0; i < ARRAY_SIZE(tdes_algs); i++) {
1109 atmel_tdes_skcipher_alg_init(&tdes_algs[i]);
1111 err = crypto_register_skcipher(&tdes_algs[i]);
1119 for (j = 0; j < i; j++)
1120 crypto_unregister_skcipher(&tdes_algs[j]);
1125 static void atmel_tdes_get_cap(struct atmel_tdes_dev *dd)
1128 dd->caps.has_dma = 0;
1129 dd->caps.has_cfb_3keys = 0;
1131 /* keep only major version number */
1132 switch (dd->hw_version & 0xf00) {
1135 dd->caps.has_dma = 1;
1136 dd->caps.has_cfb_3keys = 1;
1142 "Unmanaged tdes version, set minimum capabilities\n");
1147 #if defined(CONFIG_OF)
1148 static const struct of_device_id atmel_tdes_dt_ids[] = {
1149 { .compatible = "atmel,at91sam9g46-tdes" },
1152 MODULE_DEVICE_TABLE(of, atmel_tdes_dt_ids);
1155 static int atmel_tdes_probe(struct platform_device *pdev)
1157 struct atmel_tdes_dev *tdes_dd;
1158 struct device *dev = &pdev->dev;
1159 struct resource *tdes_res;
1162 tdes_dd = devm_kmalloc(&pdev->dev, sizeof(*tdes_dd), GFP_KERNEL);
1168 platform_set_drvdata(pdev, tdes_dd);
1170 INIT_LIST_HEAD(&tdes_dd->list);
1171 spin_lock_init(&tdes_dd->lock);
1173 tasklet_init(&tdes_dd->done_task, atmel_tdes_done_task,
1174 (unsigned long)tdes_dd);
1175 tasklet_init(&tdes_dd->queue_task, atmel_tdes_queue_task,
1176 (unsigned long)tdes_dd);
1178 crypto_init_queue(&tdes_dd->queue, ATMEL_TDES_QUEUE_LENGTH);
1180 /* Get the base address */
1181 tdes_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1183 dev_err(dev, "no MEM resource info\n");
1185 goto err_tasklet_kill;
1187 tdes_dd->phys_base = tdes_res->start;
1190 tdes_dd->irq = platform_get_irq(pdev, 0);
1191 if (tdes_dd->irq < 0) {
1193 goto err_tasklet_kill;
1196 err = devm_request_irq(&pdev->dev, tdes_dd->irq, atmel_tdes_irq,
1197 IRQF_SHARED, "atmel-tdes", tdes_dd);
1199 dev_err(dev, "unable to request tdes irq.\n");
1200 goto err_tasklet_kill;
1203 /* Initializing the clock */
1204 tdes_dd->iclk = devm_clk_get(&pdev->dev, "tdes_clk");
1205 if (IS_ERR(tdes_dd->iclk)) {
1206 dev_err(dev, "clock initialization failed.\n");
1207 err = PTR_ERR(tdes_dd->iclk);
1208 goto err_tasklet_kill;
1211 tdes_dd->io_base = devm_ioremap_resource(&pdev->dev, tdes_res);
1212 if (IS_ERR(tdes_dd->io_base)) {
1213 err = PTR_ERR(tdes_dd->io_base);
1214 goto err_tasklet_kill;
1217 err = atmel_tdes_hw_version_init(tdes_dd);
1219 goto err_tasklet_kill;
1221 atmel_tdes_get_cap(tdes_dd);
1223 err = atmel_tdes_buff_init(tdes_dd);
1225 goto err_tasklet_kill;
1227 if (tdes_dd->caps.has_dma) {
1228 err = atmel_tdes_dma_init(tdes_dd);
1230 goto err_buff_cleanup;
1232 dev_info(dev, "using %s, %s for DMA transfers\n",
1233 dma_chan_name(tdes_dd->dma_lch_in.chan),
1234 dma_chan_name(tdes_dd->dma_lch_out.chan));
1237 spin_lock(&atmel_tdes.lock);
1238 list_add_tail(&tdes_dd->list, &atmel_tdes.dev_list);
1239 spin_unlock(&atmel_tdes.lock);
1241 err = atmel_tdes_register_algs(tdes_dd);
1245 dev_info(dev, "Atmel DES/TDES\n");
1250 spin_lock(&atmel_tdes.lock);
1251 list_del(&tdes_dd->list);
1252 spin_unlock(&atmel_tdes.lock);
1253 if (tdes_dd->caps.has_dma)
1254 atmel_tdes_dma_cleanup(tdes_dd);
1256 atmel_tdes_buff_cleanup(tdes_dd);
1258 tasklet_kill(&tdes_dd->done_task);
1259 tasklet_kill(&tdes_dd->queue_task);
1264 static int atmel_tdes_remove(struct platform_device *pdev)
1266 struct atmel_tdes_dev *tdes_dd = platform_get_drvdata(pdev);
1268 spin_lock(&atmel_tdes.lock);
1269 list_del(&tdes_dd->list);
1270 spin_unlock(&atmel_tdes.lock);
1272 atmel_tdes_unregister_algs(tdes_dd);
1274 tasklet_kill(&tdes_dd->done_task);
1275 tasklet_kill(&tdes_dd->queue_task);
1277 if (tdes_dd->caps.has_dma)
1278 atmel_tdes_dma_cleanup(tdes_dd);
1280 atmel_tdes_buff_cleanup(tdes_dd);
1285 static struct platform_driver atmel_tdes_driver = {
1286 .probe = atmel_tdes_probe,
1287 .remove = atmel_tdes_remove,
1289 .name = "atmel_tdes",
1290 .of_match_table = of_match_ptr(atmel_tdes_dt_ids),
1294 module_platform_driver(atmel_tdes_driver);
1296 MODULE_DESCRIPTION("Atmel DES/TDES hw acceleration support.");
1297 MODULE_LICENSE("GPL v2");
1298 MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");