dmaengine: imx-sdma: Let the core do the device node validation
[linux/fpc-iii.git] / drivers / crypto / atmel-tdes.c
blobfa76620281e803aae994f7a28241a3e8505e94e5
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Cryptographic API.
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>
19 #include <linux/io.h>
20 #include <linux/hw_random.h>
21 #include <linux/platform_device.h>
23 #include <linux/device.h>
24 #include <linux/init.h>
25 #include <linux/errno.h>
26 #include <linux/interrupt.h>
27 #include <linux/irq.h>
28 #include <linux/scatterlist.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/of_device.h>
31 #include <linux/delay.h>
32 #include <linux/crypto.h>
33 #include <linux/cryptohash.h>
34 #include <crypto/scatterwalk.h>
35 #include <crypto/algapi.h>
36 #include <crypto/des.h>
37 #include <crypto/hash.h>
38 #include <crypto/internal/hash.h>
39 #include <linux/platform_data/crypto-atmel.h>
40 #include "atmel-tdes-regs.h"
42 /* TDES flags */
43 #define TDES_FLAGS_MODE_MASK 0x00ff
44 #define TDES_FLAGS_ENCRYPT BIT(0)
45 #define TDES_FLAGS_CBC BIT(1)
46 #define TDES_FLAGS_CFB BIT(2)
47 #define TDES_FLAGS_CFB8 BIT(3)
48 #define TDES_FLAGS_CFB16 BIT(4)
49 #define TDES_FLAGS_CFB32 BIT(5)
50 #define TDES_FLAGS_CFB64 BIT(6)
51 #define TDES_FLAGS_OFB BIT(7)
53 #define TDES_FLAGS_INIT BIT(16)
54 #define TDES_FLAGS_FAST BIT(17)
55 #define TDES_FLAGS_BUSY BIT(18)
56 #define TDES_FLAGS_DMA BIT(19)
58 #define ATMEL_TDES_QUEUE_LENGTH 50
60 #define CFB8_BLOCK_SIZE 1
61 #define CFB16_BLOCK_SIZE 2
62 #define CFB32_BLOCK_SIZE 4
64 struct atmel_tdes_caps {
65 bool has_dma;
66 u32 has_cfb_3keys;
69 struct atmel_tdes_dev;
71 struct atmel_tdes_ctx {
72 struct atmel_tdes_dev *dd;
74 int keylen;
75 u32 key[3*DES_KEY_SIZE / sizeof(u32)];
76 unsigned long flags;
78 u16 block_size;
81 struct atmel_tdes_reqctx {
82 unsigned long mode;
85 struct atmel_tdes_dma {
86 struct dma_chan *chan;
87 struct dma_slave_config dma_conf;
90 struct atmel_tdes_dev {
91 struct list_head list;
92 unsigned long phys_base;
93 void __iomem *io_base;
95 struct atmel_tdes_ctx *ctx;
96 struct device *dev;
97 struct clk *iclk;
98 int irq;
100 unsigned long flags;
101 int err;
103 spinlock_t lock;
104 struct crypto_queue queue;
106 struct tasklet_struct done_task;
107 struct tasklet_struct queue_task;
109 struct ablkcipher_request *req;
110 size_t total;
112 struct scatterlist *in_sg;
113 unsigned int nb_in_sg;
114 size_t in_offset;
115 struct scatterlist *out_sg;
116 unsigned int nb_out_sg;
117 size_t out_offset;
119 size_t buflen;
120 size_t dma_size;
122 void *buf_in;
123 int dma_in;
124 dma_addr_t dma_addr_in;
125 struct atmel_tdes_dma dma_lch_in;
127 void *buf_out;
128 int dma_out;
129 dma_addr_t dma_addr_out;
130 struct atmel_tdes_dma dma_lch_out;
132 struct atmel_tdes_caps caps;
134 u32 hw_version;
137 struct atmel_tdes_drv {
138 struct list_head dev_list;
139 spinlock_t lock;
142 static struct atmel_tdes_drv atmel_tdes = {
143 .dev_list = LIST_HEAD_INIT(atmel_tdes.dev_list),
144 .lock = __SPIN_LOCK_UNLOCKED(atmel_tdes.lock),
147 static int atmel_tdes_sg_copy(struct scatterlist **sg, size_t *offset,
148 void *buf, size_t buflen, size_t total, int out)
150 size_t count, off = 0;
152 while (buflen && total) {
153 count = min((*sg)->length - *offset, total);
154 count = min(count, buflen);
156 if (!count)
157 return off;
159 scatterwalk_map_and_copy(buf + off, *sg, *offset, count, out);
161 off += count;
162 buflen -= count;
163 *offset += count;
164 total -= count;
166 if (*offset == (*sg)->length) {
167 *sg = sg_next(*sg);
168 if (*sg)
169 *offset = 0;
170 else
171 total = 0;
175 return off;
178 static inline u32 atmel_tdes_read(struct atmel_tdes_dev *dd, u32 offset)
180 return readl_relaxed(dd->io_base + offset);
183 static inline void atmel_tdes_write(struct atmel_tdes_dev *dd,
184 u32 offset, u32 value)
186 writel_relaxed(value, dd->io_base + offset);
189 static void atmel_tdes_write_n(struct atmel_tdes_dev *dd, u32 offset,
190 u32 *value, int count)
192 for (; count--; value++, offset += 4)
193 atmel_tdes_write(dd, offset, *value);
196 static struct atmel_tdes_dev *atmel_tdes_find_dev(struct atmel_tdes_ctx *ctx)
198 struct atmel_tdes_dev *tdes_dd = NULL;
199 struct atmel_tdes_dev *tmp;
201 spin_lock_bh(&atmel_tdes.lock);
202 if (!ctx->dd) {
203 list_for_each_entry(tmp, &atmel_tdes.dev_list, list) {
204 tdes_dd = tmp;
205 break;
207 ctx->dd = tdes_dd;
208 } else {
209 tdes_dd = ctx->dd;
211 spin_unlock_bh(&atmel_tdes.lock);
213 return tdes_dd;
216 static int atmel_tdes_hw_init(struct atmel_tdes_dev *dd)
218 int err;
220 err = clk_prepare_enable(dd->iclk);
221 if (err)
222 return err;
224 if (!(dd->flags & TDES_FLAGS_INIT)) {
225 atmel_tdes_write(dd, TDES_CR, TDES_CR_SWRST);
226 dd->flags |= TDES_FLAGS_INIT;
227 dd->err = 0;
230 return 0;
233 static inline unsigned int atmel_tdes_get_version(struct atmel_tdes_dev *dd)
235 return atmel_tdes_read(dd, TDES_HW_VERSION) & 0x00000fff;
238 static void atmel_tdes_hw_version_init(struct atmel_tdes_dev *dd)
240 atmel_tdes_hw_init(dd);
242 dd->hw_version = atmel_tdes_get_version(dd);
244 dev_info(dd->dev,
245 "version: 0x%x\n", dd->hw_version);
247 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)
260 int err;
261 u32 valcr = 0, valmr = TDES_MR_SMOD_PDC;
263 err = atmel_tdes_hw_init(dd);
265 if (err)
266 return err;
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;
279 } else {
280 valmr |= TDES_MR_TDESMOD_DES;
283 if (dd->flags & TDES_FLAGS_CBC) {
284 valmr |= TDES_MR_OPMOD_CBC;
285 } else if (dd->flags & TDES_FLAGS_CFB) {
286 valmr |= TDES_MR_OPMOD_CFB;
288 if (dd->flags & TDES_FLAGS_CFB8)
289 valmr |= TDES_MR_CFBS_8b;
290 else if (dd->flags & TDES_FLAGS_CFB16)
291 valmr |= TDES_MR_CFBS_16b;
292 else if (dd->flags & TDES_FLAGS_CFB32)
293 valmr |= TDES_MR_CFBS_32b;
294 else if (dd->flags & TDES_FLAGS_CFB64)
295 valmr |= TDES_MR_CFBS_64b;
296 } else if (dd->flags & TDES_FLAGS_OFB) {
297 valmr |= TDES_MR_OPMOD_OFB;
300 if ((dd->flags & TDES_FLAGS_ENCRYPT) || (dd->flags & TDES_FLAGS_OFB))
301 valmr |= TDES_MR_CYPHER_ENC;
303 atmel_tdes_write(dd, TDES_CR, valcr);
304 atmel_tdes_write(dd, TDES_MR, valmr);
306 atmel_tdes_write_n(dd, TDES_KEY1W1R, dd->ctx->key,
307 dd->ctx->keylen >> 2);
309 if (((dd->flags & TDES_FLAGS_CBC) || (dd->flags & TDES_FLAGS_CFB) ||
310 (dd->flags & TDES_FLAGS_OFB)) && dd->req->info) {
311 atmel_tdes_write_n(dd, TDES_IV1R, dd->req->info, 2);
314 return 0;
317 static int atmel_tdes_crypt_pdc_stop(struct atmel_tdes_dev *dd)
319 int err = 0;
320 size_t count;
322 atmel_tdes_write(dd, TDES_PTCR, TDES_PTCR_TXTDIS|TDES_PTCR_RXTDIS);
324 if (dd->flags & TDES_FLAGS_FAST) {
325 dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_FROM_DEVICE);
326 dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
327 } else {
328 dma_sync_single_for_device(dd->dev, dd->dma_addr_out,
329 dd->dma_size, DMA_FROM_DEVICE);
331 /* copy data */
332 count = atmel_tdes_sg_copy(&dd->out_sg, &dd->out_offset,
333 dd->buf_out, dd->buflen, dd->dma_size, 1);
334 if (count != dd->dma_size) {
335 err = -EINVAL;
336 pr_err("not all data converted: %zu\n", count);
340 return err;
343 static int atmel_tdes_buff_init(struct atmel_tdes_dev *dd)
345 int err = -ENOMEM;
347 dd->buf_in = (void *)__get_free_pages(GFP_KERNEL, 0);
348 dd->buf_out = (void *)__get_free_pages(GFP_KERNEL, 0);
349 dd->buflen = PAGE_SIZE;
350 dd->buflen &= ~(DES_BLOCK_SIZE - 1);
352 if (!dd->buf_in || !dd->buf_out) {
353 dev_err(dd->dev, "unable to alloc pages.\n");
354 goto err_alloc;
357 /* MAP here */
358 dd->dma_addr_in = dma_map_single(dd->dev, dd->buf_in,
359 dd->buflen, DMA_TO_DEVICE);
360 if (dma_mapping_error(dd->dev, dd->dma_addr_in)) {
361 dev_err(dd->dev, "dma %zd bytes error\n", dd->buflen);
362 err = -EINVAL;
363 goto err_map_in;
366 dd->dma_addr_out = dma_map_single(dd->dev, dd->buf_out,
367 dd->buflen, DMA_FROM_DEVICE);
368 if (dma_mapping_error(dd->dev, dd->dma_addr_out)) {
369 dev_err(dd->dev, "dma %zd bytes error\n", dd->buflen);
370 err = -EINVAL;
371 goto err_map_out;
374 return 0;
376 err_map_out:
377 dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen,
378 DMA_TO_DEVICE);
379 err_map_in:
380 err_alloc:
381 free_page((unsigned long)dd->buf_out);
382 free_page((unsigned long)dd->buf_in);
383 if (err)
384 pr_err("error: %d\n", err);
385 return err;
388 static void atmel_tdes_buff_cleanup(struct atmel_tdes_dev *dd)
390 dma_unmap_single(dd->dev, dd->dma_addr_out, dd->buflen,
391 DMA_FROM_DEVICE);
392 dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen,
393 DMA_TO_DEVICE);
394 free_page((unsigned long)dd->buf_out);
395 free_page((unsigned long)dd->buf_in);
398 static int atmel_tdes_crypt_pdc(struct crypto_tfm *tfm, dma_addr_t dma_addr_in,
399 dma_addr_t dma_addr_out, int length)
401 struct atmel_tdes_ctx *ctx = crypto_tfm_ctx(tfm);
402 struct atmel_tdes_dev *dd = ctx->dd;
403 int len32;
405 dd->dma_size = length;
407 if (!(dd->flags & TDES_FLAGS_FAST)) {
408 dma_sync_single_for_device(dd->dev, dma_addr_in, length,
409 DMA_TO_DEVICE);
412 if ((dd->flags & TDES_FLAGS_CFB) && (dd->flags & TDES_FLAGS_CFB8))
413 len32 = DIV_ROUND_UP(length, sizeof(u8));
414 else if ((dd->flags & TDES_FLAGS_CFB) && (dd->flags & TDES_FLAGS_CFB16))
415 len32 = DIV_ROUND_UP(length, sizeof(u16));
416 else
417 len32 = DIV_ROUND_UP(length, sizeof(u32));
419 atmel_tdes_write(dd, TDES_PTCR, TDES_PTCR_TXTDIS|TDES_PTCR_RXTDIS);
420 atmel_tdes_write(dd, TDES_TPR, dma_addr_in);
421 atmel_tdes_write(dd, TDES_TCR, len32);
422 atmel_tdes_write(dd, TDES_RPR, dma_addr_out);
423 atmel_tdes_write(dd, TDES_RCR, len32);
425 /* Enable Interrupt */
426 atmel_tdes_write(dd, TDES_IER, TDES_INT_ENDRX);
428 /* Start DMA transfer */
429 atmel_tdes_write(dd, TDES_PTCR, TDES_PTCR_TXTEN | TDES_PTCR_RXTEN);
431 return 0;
434 static int atmel_tdes_crypt_dma(struct crypto_tfm *tfm, dma_addr_t dma_addr_in,
435 dma_addr_t dma_addr_out, int length)
437 struct atmel_tdes_ctx *ctx = crypto_tfm_ctx(tfm);
438 struct atmel_tdes_dev *dd = ctx->dd;
439 struct scatterlist sg[2];
440 struct dma_async_tx_descriptor *in_desc, *out_desc;
442 dd->dma_size = length;
444 if (!(dd->flags & TDES_FLAGS_FAST)) {
445 dma_sync_single_for_device(dd->dev, dma_addr_in, length,
446 DMA_TO_DEVICE);
449 if (dd->flags & TDES_FLAGS_CFB8) {
450 dd->dma_lch_in.dma_conf.dst_addr_width =
451 DMA_SLAVE_BUSWIDTH_1_BYTE;
452 dd->dma_lch_out.dma_conf.src_addr_width =
453 DMA_SLAVE_BUSWIDTH_1_BYTE;
454 } else if (dd->flags & TDES_FLAGS_CFB16) {
455 dd->dma_lch_in.dma_conf.dst_addr_width =
456 DMA_SLAVE_BUSWIDTH_2_BYTES;
457 dd->dma_lch_out.dma_conf.src_addr_width =
458 DMA_SLAVE_BUSWIDTH_2_BYTES;
459 } else {
460 dd->dma_lch_in.dma_conf.dst_addr_width =
461 DMA_SLAVE_BUSWIDTH_4_BYTES;
462 dd->dma_lch_out.dma_conf.src_addr_width =
463 DMA_SLAVE_BUSWIDTH_4_BYTES;
466 dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
467 dmaengine_slave_config(dd->dma_lch_out.chan, &dd->dma_lch_out.dma_conf);
469 dd->flags |= TDES_FLAGS_DMA;
471 sg_init_table(&sg[0], 1);
472 sg_dma_address(&sg[0]) = dma_addr_in;
473 sg_dma_len(&sg[0]) = length;
475 sg_init_table(&sg[1], 1);
476 sg_dma_address(&sg[1]) = dma_addr_out;
477 sg_dma_len(&sg[1]) = length;
479 in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, &sg[0],
480 1, DMA_MEM_TO_DEV,
481 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
482 if (!in_desc)
483 return -EINVAL;
485 out_desc = dmaengine_prep_slave_sg(dd->dma_lch_out.chan, &sg[1],
486 1, DMA_DEV_TO_MEM,
487 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
488 if (!out_desc)
489 return -EINVAL;
491 out_desc->callback = atmel_tdes_dma_callback;
492 out_desc->callback_param = dd;
494 dmaengine_submit(out_desc);
495 dma_async_issue_pending(dd->dma_lch_out.chan);
497 dmaengine_submit(in_desc);
498 dma_async_issue_pending(dd->dma_lch_in.chan);
500 return 0;
503 static int atmel_tdes_crypt_start(struct atmel_tdes_dev *dd)
505 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(
506 crypto_ablkcipher_reqtfm(dd->req));
507 int err, fast = 0, in, out;
508 size_t count;
509 dma_addr_t addr_in, addr_out;
511 if ((!dd->in_offset) && (!dd->out_offset)) {
512 /* check for alignment */
513 in = IS_ALIGNED((u32)dd->in_sg->offset, sizeof(u32)) &&
514 IS_ALIGNED(dd->in_sg->length, dd->ctx->block_size);
515 out = IS_ALIGNED((u32)dd->out_sg->offset, sizeof(u32)) &&
516 IS_ALIGNED(dd->out_sg->length, dd->ctx->block_size);
517 fast = in && out;
519 if (sg_dma_len(dd->in_sg) != sg_dma_len(dd->out_sg))
520 fast = 0;
524 if (fast) {
525 count = min_t(size_t, dd->total, sg_dma_len(dd->in_sg));
526 count = min_t(size_t, count, sg_dma_len(dd->out_sg));
528 err = dma_map_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
529 if (!err) {
530 dev_err(dd->dev, "dma_map_sg() error\n");
531 return -EINVAL;
534 err = dma_map_sg(dd->dev, dd->out_sg, 1,
535 DMA_FROM_DEVICE);
536 if (!err) {
537 dev_err(dd->dev, "dma_map_sg() error\n");
538 dma_unmap_sg(dd->dev, dd->in_sg, 1,
539 DMA_TO_DEVICE);
540 return -EINVAL;
543 addr_in = sg_dma_address(dd->in_sg);
544 addr_out = sg_dma_address(dd->out_sg);
546 dd->flags |= TDES_FLAGS_FAST;
548 } else {
549 /* use cache buffers */
550 count = atmel_tdes_sg_copy(&dd->in_sg, &dd->in_offset,
551 dd->buf_in, dd->buflen, dd->total, 0);
553 addr_in = dd->dma_addr_in;
554 addr_out = dd->dma_addr_out;
556 dd->flags &= ~TDES_FLAGS_FAST;
559 dd->total -= count;
561 if (dd->caps.has_dma)
562 err = atmel_tdes_crypt_dma(tfm, addr_in, addr_out, count);
563 else
564 err = atmel_tdes_crypt_pdc(tfm, addr_in, addr_out, count);
566 if (err && (dd->flags & TDES_FLAGS_FAST)) {
567 dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
568 dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_TO_DEVICE);
571 return err;
574 static void atmel_tdes_finish_req(struct atmel_tdes_dev *dd, int err)
576 struct ablkcipher_request *req = dd->req;
578 clk_disable_unprepare(dd->iclk);
580 dd->flags &= ~TDES_FLAGS_BUSY;
582 req->base.complete(&req->base, err);
585 static int atmel_tdes_handle_queue(struct atmel_tdes_dev *dd,
586 struct ablkcipher_request *req)
588 struct crypto_async_request *async_req, *backlog;
589 struct atmel_tdes_ctx *ctx;
590 struct atmel_tdes_reqctx *rctx;
591 unsigned long flags;
592 int err, ret = 0;
594 spin_lock_irqsave(&dd->lock, flags);
595 if (req)
596 ret = ablkcipher_enqueue_request(&dd->queue, req);
597 if (dd->flags & TDES_FLAGS_BUSY) {
598 spin_unlock_irqrestore(&dd->lock, flags);
599 return ret;
601 backlog = crypto_get_backlog(&dd->queue);
602 async_req = crypto_dequeue_request(&dd->queue);
603 if (async_req)
604 dd->flags |= TDES_FLAGS_BUSY;
605 spin_unlock_irqrestore(&dd->lock, flags);
607 if (!async_req)
608 return ret;
610 if (backlog)
611 backlog->complete(backlog, -EINPROGRESS);
613 req = ablkcipher_request_cast(async_req);
615 /* assign new request to device */
616 dd->req = req;
617 dd->total = req->nbytes;
618 dd->in_offset = 0;
619 dd->in_sg = req->src;
620 dd->out_offset = 0;
621 dd->out_sg = req->dst;
623 rctx = ablkcipher_request_ctx(req);
624 ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
625 rctx->mode &= TDES_FLAGS_MODE_MASK;
626 dd->flags = (dd->flags & ~TDES_FLAGS_MODE_MASK) | rctx->mode;
627 dd->ctx = ctx;
628 ctx->dd = dd;
630 err = atmel_tdes_write_ctrl(dd);
631 if (!err)
632 err = atmel_tdes_crypt_start(dd);
633 if (err) {
634 /* des_task will not finish it, so do it here */
635 atmel_tdes_finish_req(dd, err);
636 tasklet_schedule(&dd->queue_task);
639 return ret;
642 static int atmel_tdes_crypt_dma_stop(struct atmel_tdes_dev *dd)
644 int err = -EINVAL;
645 size_t count;
647 if (dd->flags & TDES_FLAGS_DMA) {
648 err = 0;
649 if (dd->flags & TDES_FLAGS_FAST) {
650 dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_FROM_DEVICE);
651 dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
652 } else {
653 dma_sync_single_for_device(dd->dev, dd->dma_addr_out,
654 dd->dma_size, DMA_FROM_DEVICE);
656 /* copy data */
657 count = atmel_tdes_sg_copy(&dd->out_sg, &dd->out_offset,
658 dd->buf_out, dd->buflen, dd->dma_size, 1);
659 if (count != dd->dma_size) {
660 err = -EINVAL;
661 pr_err("not all data converted: %zu\n", count);
665 return err;
668 static int atmel_tdes_crypt(struct ablkcipher_request *req, unsigned long mode)
670 struct atmel_tdes_ctx *ctx = crypto_ablkcipher_ctx(
671 crypto_ablkcipher_reqtfm(req));
672 struct atmel_tdes_reqctx *rctx = ablkcipher_request_ctx(req);
674 if (mode & TDES_FLAGS_CFB8) {
675 if (!IS_ALIGNED(req->nbytes, CFB8_BLOCK_SIZE)) {
676 pr_err("request size is not exact amount of CFB8 blocks\n");
677 return -EINVAL;
679 ctx->block_size = CFB8_BLOCK_SIZE;
680 } else if (mode & TDES_FLAGS_CFB16) {
681 if (!IS_ALIGNED(req->nbytes, CFB16_BLOCK_SIZE)) {
682 pr_err("request size is not exact amount of CFB16 blocks\n");
683 return -EINVAL;
685 ctx->block_size = CFB16_BLOCK_SIZE;
686 } else if (mode & TDES_FLAGS_CFB32) {
687 if (!IS_ALIGNED(req->nbytes, CFB32_BLOCK_SIZE)) {
688 pr_err("request size is not exact amount of CFB32 blocks\n");
689 return -EINVAL;
691 ctx->block_size = CFB32_BLOCK_SIZE;
692 } else {
693 if (!IS_ALIGNED(req->nbytes, DES_BLOCK_SIZE)) {
694 pr_err("request size is not exact amount of DES blocks\n");
695 return -EINVAL;
697 ctx->block_size = DES_BLOCK_SIZE;
700 rctx->mode = mode;
702 return atmel_tdes_handle_queue(ctx->dd, req);
705 static bool atmel_tdes_filter(struct dma_chan *chan, void *slave)
707 struct at_dma_slave *sl = slave;
709 if (sl && sl->dma_dev == chan->device->dev) {
710 chan->private = sl;
711 return true;
712 } else {
713 return false;
717 static int atmel_tdes_dma_init(struct atmel_tdes_dev *dd,
718 struct crypto_platform_data *pdata)
720 dma_cap_mask_t mask;
722 dma_cap_zero(mask);
723 dma_cap_set(DMA_SLAVE, mask);
725 /* Try to grab 2 DMA channels */
726 dd->dma_lch_in.chan = dma_request_slave_channel_compat(mask,
727 atmel_tdes_filter, &pdata->dma_slave->rxdata, dd->dev, "tx");
728 if (!dd->dma_lch_in.chan)
729 goto err_dma_in;
731 dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV;
732 dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
733 TDES_IDATA1R;
734 dd->dma_lch_in.dma_conf.src_maxburst = 1;
735 dd->dma_lch_in.dma_conf.src_addr_width =
736 DMA_SLAVE_BUSWIDTH_4_BYTES;
737 dd->dma_lch_in.dma_conf.dst_maxburst = 1;
738 dd->dma_lch_in.dma_conf.dst_addr_width =
739 DMA_SLAVE_BUSWIDTH_4_BYTES;
740 dd->dma_lch_in.dma_conf.device_fc = false;
742 dd->dma_lch_out.chan = dma_request_slave_channel_compat(mask,
743 atmel_tdes_filter, &pdata->dma_slave->txdata, dd->dev, "rx");
744 if (!dd->dma_lch_out.chan)
745 goto err_dma_out;
747 dd->dma_lch_out.dma_conf.direction = DMA_DEV_TO_MEM;
748 dd->dma_lch_out.dma_conf.src_addr = dd->phys_base +
749 TDES_ODATA1R;
750 dd->dma_lch_out.dma_conf.src_maxburst = 1;
751 dd->dma_lch_out.dma_conf.src_addr_width =
752 DMA_SLAVE_BUSWIDTH_4_BYTES;
753 dd->dma_lch_out.dma_conf.dst_maxburst = 1;
754 dd->dma_lch_out.dma_conf.dst_addr_width =
755 DMA_SLAVE_BUSWIDTH_4_BYTES;
756 dd->dma_lch_out.dma_conf.device_fc = false;
758 return 0;
760 err_dma_out:
761 dma_release_channel(dd->dma_lch_in.chan);
762 err_dma_in:
763 dev_warn(dd->dev, "no DMA channel available\n");
764 return -ENODEV;
767 static void atmel_tdes_dma_cleanup(struct atmel_tdes_dev *dd)
769 dma_release_channel(dd->dma_lch_in.chan);
770 dma_release_channel(dd->dma_lch_out.chan);
773 static int atmel_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
774 unsigned int keylen)
776 u32 tmp[DES_EXPKEY_WORDS];
777 int err;
778 struct crypto_tfm *ctfm = crypto_ablkcipher_tfm(tfm);
780 struct atmel_tdes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
782 if (keylen != DES_KEY_SIZE) {
783 crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
784 return -EINVAL;
787 err = des_ekey(tmp, key);
788 if (err == 0 && (ctfm->crt_flags & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)) {
789 ctfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
790 return -EINVAL;
793 memcpy(ctx->key, key, keylen);
794 ctx->keylen = keylen;
796 return 0;
799 static int atmel_tdes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
800 unsigned int keylen)
802 struct atmel_tdes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
803 u32 flags;
804 int err;
806 flags = crypto_ablkcipher_get_flags(tfm);
807 err = __des3_verify_key(&flags, key);
808 if (unlikely(err)) {
809 crypto_ablkcipher_set_flags(tfm, flags);
810 return err;
813 memcpy(ctx->key, key, keylen);
814 ctx->keylen = keylen;
816 return 0;
819 static int atmel_tdes_ecb_encrypt(struct ablkcipher_request *req)
821 return atmel_tdes_crypt(req, TDES_FLAGS_ENCRYPT);
824 static int atmel_tdes_ecb_decrypt(struct ablkcipher_request *req)
826 return atmel_tdes_crypt(req, 0);
829 static int atmel_tdes_cbc_encrypt(struct ablkcipher_request *req)
831 return atmel_tdes_crypt(req, TDES_FLAGS_ENCRYPT | TDES_FLAGS_CBC);
834 static int atmel_tdes_cbc_decrypt(struct ablkcipher_request *req)
836 return atmel_tdes_crypt(req, TDES_FLAGS_CBC);
838 static int atmel_tdes_cfb_encrypt(struct ablkcipher_request *req)
840 return atmel_tdes_crypt(req, TDES_FLAGS_ENCRYPT | TDES_FLAGS_CFB);
843 static int atmel_tdes_cfb_decrypt(struct ablkcipher_request *req)
845 return atmel_tdes_crypt(req, TDES_FLAGS_CFB);
848 static int atmel_tdes_cfb8_encrypt(struct ablkcipher_request *req)
850 return atmel_tdes_crypt(req, TDES_FLAGS_ENCRYPT | TDES_FLAGS_CFB |
851 TDES_FLAGS_CFB8);
854 static int atmel_tdes_cfb8_decrypt(struct ablkcipher_request *req)
856 return atmel_tdes_crypt(req, TDES_FLAGS_CFB | TDES_FLAGS_CFB8);
859 static int atmel_tdes_cfb16_encrypt(struct ablkcipher_request *req)
861 return atmel_tdes_crypt(req, TDES_FLAGS_ENCRYPT | TDES_FLAGS_CFB |
862 TDES_FLAGS_CFB16);
865 static int atmel_tdes_cfb16_decrypt(struct ablkcipher_request *req)
867 return atmel_tdes_crypt(req, TDES_FLAGS_CFB | TDES_FLAGS_CFB16);
870 static int atmel_tdes_cfb32_encrypt(struct ablkcipher_request *req)
872 return atmel_tdes_crypt(req, TDES_FLAGS_ENCRYPT | TDES_FLAGS_CFB |
873 TDES_FLAGS_CFB32);
876 static int atmel_tdes_cfb32_decrypt(struct ablkcipher_request *req)
878 return atmel_tdes_crypt(req, TDES_FLAGS_CFB | TDES_FLAGS_CFB32);
881 static int atmel_tdes_ofb_encrypt(struct ablkcipher_request *req)
883 return atmel_tdes_crypt(req, TDES_FLAGS_ENCRYPT | TDES_FLAGS_OFB);
886 static int atmel_tdes_ofb_decrypt(struct ablkcipher_request *req)
888 return atmel_tdes_crypt(req, TDES_FLAGS_OFB);
891 static int atmel_tdes_cra_init(struct crypto_tfm *tfm)
893 struct atmel_tdes_ctx *ctx = crypto_tfm_ctx(tfm);
894 struct atmel_tdes_dev *dd;
896 tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_tdes_reqctx);
898 dd = atmel_tdes_find_dev(ctx);
899 if (!dd)
900 return -ENODEV;
902 return 0;
905 static struct crypto_alg tdes_algs[] = {
907 .cra_name = "ecb(des)",
908 .cra_driver_name = "atmel-ecb-des",
909 .cra_priority = 100,
910 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
911 .cra_blocksize = DES_BLOCK_SIZE,
912 .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
913 .cra_alignmask = 0x7,
914 .cra_type = &crypto_ablkcipher_type,
915 .cra_module = THIS_MODULE,
916 .cra_init = atmel_tdes_cra_init,
917 .cra_u.ablkcipher = {
918 .min_keysize = DES_KEY_SIZE,
919 .max_keysize = DES_KEY_SIZE,
920 .setkey = atmel_des_setkey,
921 .encrypt = atmel_tdes_ecb_encrypt,
922 .decrypt = atmel_tdes_ecb_decrypt,
926 .cra_name = "cbc(des)",
927 .cra_driver_name = "atmel-cbc-des",
928 .cra_priority = 100,
929 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
930 .cra_blocksize = DES_BLOCK_SIZE,
931 .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
932 .cra_alignmask = 0x7,
933 .cra_type = &crypto_ablkcipher_type,
934 .cra_module = THIS_MODULE,
935 .cra_init = atmel_tdes_cra_init,
936 .cra_u.ablkcipher = {
937 .min_keysize = DES_KEY_SIZE,
938 .max_keysize = DES_KEY_SIZE,
939 .ivsize = DES_BLOCK_SIZE,
940 .setkey = atmel_des_setkey,
941 .encrypt = atmel_tdes_cbc_encrypt,
942 .decrypt = atmel_tdes_cbc_decrypt,
946 .cra_name = "cfb(des)",
947 .cra_driver_name = "atmel-cfb-des",
948 .cra_priority = 100,
949 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
950 .cra_blocksize = DES_BLOCK_SIZE,
951 .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
952 .cra_alignmask = 0x7,
953 .cra_type = &crypto_ablkcipher_type,
954 .cra_module = THIS_MODULE,
955 .cra_init = atmel_tdes_cra_init,
956 .cra_u.ablkcipher = {
957 .min_keysize = DES_KEY_SIZE,
958 .max_keysize = DES_KEY_SIZE,
959 .ivsize = DES_BLOCK_SIZE,
960 .setkey = atmel_des_setkey,
961 .encrypt = atmel_tdes_cfb_encrypt,
962 .decrypt = atmel_tdes_cfb_decrypt,
966 .cra_name = "cfb8(des)",
967 .cra_driver_name = "atmel-cfb8-des",
968 .cra_priority = 100,
969 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
970 .cra_blocksize = CFB8_BLOCK_SIZE,
971 .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
972 .cra_alignmask = 0,
973 .cra_type = &crypto_ablkcipher_type,
974 .cra_module = THIS_MODULE,
975 .cra_init = atmel_tdes_cra_init,
976 .cra_u.ablkcipher = {
977 .min_keysize = DES_KEY_SIZE,
978 .max_keysize = DES_KEY_SIZE,
979 .ivsize = DES_BLOCK_SIZE,
980 .setkey = atmel_des_setkey,
981 .encrypt = atmel_tdes_cfb8_encrypt,
982 .decrypt = atmel_tdes_cfb8_decrypt,
986 .cra_name = "cfb16(des)",
987 .cra_driver_name = "atmel-cfb16-des",
988 .cra_priority = 100,
989 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
990 .cra_blocksize = CFB16_BLOCK_SIZE,
991 .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
992 .cra_alignmask = 0x1,
993 .cra_type = &crypto_ablkcipher_type,
994 .cra_module = THIS_MODULE,
995 .cra_init = atmel_tdes_cra_init,
996 .cra_u.ablkcipher = {
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_cfb16_encrypt,
1002 .decrypt = atmel_tdes_cfb16_decrypt,
1006 .cra_name = "cfb32(des)",
1007 .cra_driver_name = "atmel-cfb32-des",
1008 .cra_priority = 100,
1009 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1010 .cra_blocksize = CFB32_BLOCK_SIZE,
1011 .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
1012 .cra_alignmask = 0x3,
1013 .cra_type = &crypto_ablkcipher_type,
1014 .cra_module = THIS_MODULE,
1015 .cra_init = atmel_tdes_cra_init,
1016 .cra_u.ablkcipher = {
1017 .min_keysize = DES_KEY_SIZE,
1018 .max_keysize = DES_KEY_SIZE,
1019 .ivsize = DES_BLOCK_SIZE,
1020 .setkey = atmel_des_setkey,
1021 .encrypt = atmel_tdes_cfb32_encrypt,
1022 .decrypt = atmel_tdes_cfb32_decrypt,
1026 .cra_name = "ofb(des)",
1027 .cra_driver_name = "atmel-ofb-des",
1028 .cra_priority = 100,
1029 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1030 .cra_blocksize = DES_BLOCK_SIZE,
1031 .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
1032 .cra_alignmask = 0x7,
1033 .cra_type = &crypto_ablkcipher_type,
1034 .cra_module = THIS_MODULE,
1035 .cra_init = atmel_tdes_cra_init,
1036 .cra_u.ablkcipher = {
1037 .min_keysize = DES_KEY_SIZE,
1038 .max_keysize = DES_KEY_SIZE,
1039 .ivsize = DES_BLOCK_SIZE,
1040 .setkey = atmel_des_setkey,
1041 .encrypt = atmel_tdes_ofb_encrypt,
1042 .decrypt = atmel_tdes_ofb_decrypt,
1046 .cra_name = "ecb(des3_ede)",
1047 .cra_driver_name = "atmel-ecb-tdes",
1048 .cra_priority = 100,
1049 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1050 .cra_blocksize = DES_BLOCK_SIZE,
1051 .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
1052 .cra_alignmask = 0x7,
1053 .cra_type = &crypto_ablkcipher_type,
1054 .cra_module = THIS_MODULE,
1055 .cra_init = atmel_tdes_cra_init,
1056 .cra_u.ablkcipher = {
1057 .min_keysize = 3 * DES_KEY_SIZE,
1058 .max_keysize = 3 * DES_KEY_SIZE,
1059 .setkey = atmel_tdes_setkey,
1060 .encrypt = atmel_tdes_ecb_encrypt,
1061 .decrypt = atmel_tdes_ecb_decrypt,
1065 .cra_name = "cbc(des3_ede)",
1066 .cra_driver_name = "atmel-cbc-tdes",
1067 .cra_priority = 100,
1068 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1069 .cra_blocksize = DES_BLOCK_SIZE,
1070 .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
1071 .cra_alignmask = 0x7,
1072 .cra_type = &crypto_ablkcipher_type,
1073 .cra_module = THIS_MODULE,
1074 .cra_init = atmel_tdes_cra_init,
1075 .cra_u.ablkcipher = {
1076 .min_keysize = 3*DES_KEY_SIZE,
1077 .max_keysize = 3*DES_KEY_SIZE,
1078 .ivsize = DES_BLOCK_SIZE,
1079 .setkey = atmel_tdes_setkey,
1080 .encrypt = atmel_tdes_cbc_encrypt,
1081 .decrypt = atmel_tdes_cbc_decrypt,
1085 .cra_name = "ofb(des3_ede)",
1086 .cra_driver_name = "atmel-ofb-tdes",
1087 .cra_priority = 100,
1088 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1089 .cra_blocksize = DES_BLOCK_SIZE,
1090 .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
1091 .cra_alignmask = 0x7,
1092 .cra_type = &crypto_ablkcipher_type,
1093 .cra_module = THIS_MODULE,
1094 .cra_init = atmel_tdes_cra_init,
1095 .cra_u.ablkcipher = {
1096 .min_keysize = 3*DES_KEY_SIZE,
1097 .max_keysize = 3*DES_KEY_SIZE,
1098 .ivsize = DES_BLOCK_SIZE,
1099 .setkey = atmel_tdes_setkey,
1100 .encrypt = atmel_tdes_ofb_encrypt,
1101 .decrypt = atmel_tdes_ofb_decrypt,
1106 static void atmel_tdes_queue_task(unsigned long data)
1108 struct atmel_tdes_dev *dd = (struct atmel_tdes_dev *)data;
1110 atmel_tdes_handle_queue(dd, NULL);
1113 static void atmel_tdes_done_task(unsigned long data)
1115 struct atmel_tdes_dev *dd = (struct atmel_tdes_dev *) data;
1116 int err;
1118 if (!(dd->flags & TDES_FLAGS_DMA))
1119 err = atmel_tdes_crypt_pdc_stop(dd);
1120 else
1121 err = atmel_tdes_crypt_dma_stop(dd);
1123 err = dd->err ? : err;
1125 if (dd->total && !err) {
1126 if (dd->flags & TDES_FLAGS_FAST) {
1127 dd->in_sg = sg_next(dd->in_sg);
1128 dd->out_sg = sg_next(dd->out_sg);
1129 if (!dd->in_sg || !dd->out_sg)
1130 err = -EINVAL;
1132 if (!err)
1133 err = atmel_tdes_crypt_start(dd);
1134 if (!err)
1135 return; /* DMA started. Not fininishing. */
1138 atmel_tdes_finish_req(dd, err);
1139 atmel_tdes_handle_queue(dd, NULL);
1142 static irqreturn_t atmel_tdes_irq(int irq, void *dev_id)
1144 struct atmel_tdes_dev *tdes_dd = dev_id;
1145 u32 reg;
1147 reg = atmel_tdes_read(tdes_dd, TDES_ISR);
1148 if (reg & atmel_tdes_read(tdes_dd, TDES_IMR)) {
1149 atmel_tdes_write(tdes_dd, TDES_IDR, reg);
1150 if (TDES_FLAGS_BUSY & tdes_dd->flags)
1151 tasklet_schedule(&tdes_dd->done_task);
1152 else
1153 dev_warn(tdes_dd->dev, "TDES interrupt when no active requests.\n");
1154 return IRQ_HANDLED;
1157 return IRQ_NONE;
1160 static void atmel_tdes_unregister_algs(struct atmel_tdes_dev *dd)
1162 int i;
1164 for (i = 0; i < ARRAY_SIZE(tdes_algs); i++)
1165 crypto_unregister_alg(&tdes_algs[i]);
1168 static int atmel_tdes_register_algs(struct atmel_tdes_dev *dd)
1170 int err, i, j;
1172 for (i = 0; i < ARRAY_SIZE(tdes_algs); i++) {
1173 err = crypto_register_alg(&tdes_algs[i]);
1174 if (err)
1175 goto err_tdes_algs;
1178 return 0;
1180 err_tdes_algs:
1181 for (j = 0; j < i; j++)
1182 crypto_unregister_alg(&tdes_algs[j]);
1184 return err;
1187 static void atmel_tdes_get_cap(struct atmel_tdes_dev *dd)
1190 dd->caps.has_dma = 0;
1191 dd->caps.has_cfb_3keys = 0;
1193 /* keep only major version number */
1194 switch (dd->hw_version & 0xf00) {
1195 case 0x700:
1196 dd->caps.has_dma = 1;
1197 dd->caps.has_cfb_3keys = 1;
1198 break;
1199 case 0x600:
1200 break;
1201 default:
1202 dev_warn(dd->dev,
1203 "Unmanaged tdes version, set minimum capabilities\n");
1204 break;
1208 #if defined(CONFIG_OF)
1209 static const struct of_device_id atmel_tdes_dt_ids[] = {
1210 { .compatible = "atmel,at91sam9g46-tdes" },
1211 { /* sentinel */ }
1213 MODULE_DEVICE_TABLE(of, atmel_tdes_dt_ids);
1215 static struct crypto_platform_data *atmel_tdes_of_init(struct platform_device *pdev)
1217 struct device_node *np = pdev->dev.of_node;
1218 struct crypto_platform_data *pdata;
1220 if (!np) {
1221 dev_err(&pdev->dev, "device node not found\n");
1222 return ERR_PTR(-EINVAL);
1225 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1226 if (!pdata)
1227 return ERR_PTR(-ENOMEM);
1229 pdata->dma_slave = devm_kzalloc(&pdev->dev,
1230 sizeof(*(pdata->dma_slave)),
1231 GFP_KERNEL);
1232 if (!pdata->dma_slave)
1233 return ERR_PTR(-ENOMEM);
1235 return pdata;
1237 #else /* CONFIG_OF */
1238 static inline struct crypto_platform_data *atmel_tdes_of_init(struct platform_device *pdev)
1240 return ERR_PTR(-EINVAL);
1242 #endif
1244 static int atmel_tdes_probe(struct platform_device *pdev)
1246 struct atmel_tdes_dev *tdes_dd;
1247 struct crypto_platform_data *pdata;
1248 struct device *dev = &pdev->dev;
1249 struct resource *tdes_res;
1250 int err;
1252 tdes_dd = devm_kmalloc(&pdev->dev, sizeof(*tdes_dd), GFP_KERNEL);
1253 if (tdes_dd == NULL) {
1254 err = -ENOMEM;
1255 goto tdes_dd_err;
1258 tdes_dd->dev = dev;
1260 platform_set_drvdata(pdev, tdes_dd);
1262 INIT_LIST_HEAD(&tdes_dd->list);
1263 spin_lock_init(&tdes_dd->lock);
1265 tasklet_init(&tdes_dd->done_task, atmel_tdes_done_task,
1266 (unsigned long)tdes_dd);
1267 tasklet_init(&tdes_dd->queue_task, atmel_tdes_queue_task,
1268 (unsigned long)tdes_dd);
1270 crypto_init_queue(&tdes_dd->queue, ATMEL_TDES_QUEUE_LENGTH);
1272 /* Get the base address */
1273 tdes_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1274 if (!tdes_res) {
1275 dev_err(dev, "no MEM resource info\n");
1276 err = -ENODEV;
1277 goto res_err;
1279 tdes_dd->phys_base = tdes_res->start;
1281 /* Get the IRQ */
1282 tdes_dd->irq = platform_get_irq(pdev, 0);
1283 if (tdes_dd->irq < 0) {
1284 dev_err(dev, "no IRQ resource info\n");
1285 err = tdes_dd->irq;
1286 goto res_err;
1289 err = devm_request_irq(&pdev->dev, tdes_dd->irq, atmel_tdes_irq,
1290 IRQF_SHARED, "atmel-tdes", tdes_dd);
1291 if (err) {
1292 dev_err(dev, "unable to request tdes irq.\n");
1293 goto res_err;
1296 /* Initializing the clock */
1297 tdes_dd->iclk = devm_clk_get(&pdev->dev, "tdes_clk");
1298 if (IS_ERR(tdes_dd->iclk)) {
1299 dev_err(dev, "clock initialization failed.\n");
1300 err = PTR_ERR(tdes_dd->iclk);
1301 goto res_err;
1304 tdes_dd->io_base = devm_ioremap_resource(&pdev->dev, tdes_res);
1305 if (IS_ERR(tdes_dd->io_base)) {
1306 dev_err(dev, "can't ioremap\n");
1307 err = PTR_ERR(tdes_dd->io_base);
1308 goto res_err;
1311 atmel_tdes_hw_version_init(tdes_dd);
1313 atmel_tdes_get_cap(tdes_dd);
1315 err = atmel_tdes_buff_init(tdes_dd);
1316 if (err)
1317 goto err_tdes_buff;
1319 if (tdes_dd->caps.has_dma) {
1320 pdata = pdev->dev.platform_data;
1321 if (!pdata) {
1322 pdata = atmel_tdes_of_init(pdev);
1323 if (IS_ERR(pdata)) {
1324 dev_err(&pdev->dev, "platform data not available\n");
1325 err = PTR_ERR(pdata);
1326 goto err_pdata;
1329 if (!pdata->dma_slave) {
1330 err = -ENXIO;
1331 goto err_pdata;
1333 err = atmel_tdes_dma_init(tdes_dd, pdata);
1334 if (err)
1335 goto err_tdes_dma;
1337 dev_info(dev, "using %s, %s for DMA transfers\n",
1338 dma_chan_name(tdes_dd->dma_lch_in.chan),
1339 dma_chan_name(tdes_dd->dma_lch_out.chan));
1342 spin_lock(&atmel_tdes.lock);
1343 list_add_tail(&tdes_dd->list, &atmel_tdes.dev_list);
1344 spin_unlock(&atmel_tdes.lock);
1346 err = atmel_tdes_register_algs(tdes_dd);
1347 if (err)
1348 goto err_algs;
1350 dev_info(dev, "Atmel DES/TDES\n");
1352 return 0;
1354 err_algs:
1355 spin_lock(&atmel_tdes.lock);
1356 list_del(&tdes_dd->list);
1357 spin_unlock(&atmel_tdes.lock);
1358 if (tdes_dd->caps.has_dma)
1359 atmel_tdes_dma_cleanup(tdes_dd);
1360 err_tdes_dma:
1361 err_pdata:
1362 atmel_tdes_buff_cleanup(tdes_dd);
1363 err_tdes_buff:
1364 res_err:
1365 tasklet_kill(&tdes_dd->done_task);
1366 tasklet_kill(&tdes_dd->queue_task);
1367 tdes_dd_err:
1368 dev_err(dev, "initialization failed.\n");
1370 return err;
1373 static int atmel_tdes_remove(struct platform_device *pdev)
1375 struct atmel_tdes_dev *tdes_dd;
1377 tdes_dd = platform_get_drvdata(pdev);
1378 if (!tdes_dd)
1379 return -ENODEV;
1380 spin_lock(&atmel_tdes.lock);
1381 list_del(&tdes_dd->list);
1382 spin_unlock(&atmel_tdes.lock);
1384 atmel_tdes_unregister_algs(tdes_dd);
1386 tasklet_kill(&tdes_dd->done_task);
1387 tasklet_kill(&tdes_dd->queue_task);
1389 if (tdes_dd->caps.has_dma)
1390 atmel_tdes_dma_cleanup(tdes_dd);
1392 atmel_tdes_buff_cleanup(tdes_dd);
1394 return 0;
1397 static struct platform_driver atmel_tdes_driver = {
1398 .probe = atmel_tdes_probe,
1399 .remove = atmel_tdes_remove,
1400 .driver = {
1401 .name = "atmel_tdes",
1402 .of_match_table = of_match_ptr(atmel_tdes_dt_ids),
1406 module_platform_driver(atmel_tdes_driver);
1408 MODULE_DESCRIPTION("Atmel DES/TDES hw acceleration support.");
1409 MODULE_LICENSE("GPL v2");
1410 MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");