4 * Support for ATMEL DES/TDES HW acceleration.
6 * Copyright (c) 2012 Eukréa Electromatique - ATMEL
7 * Author: Nicolas Royer <nicolas@eukrea.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
13 * Some ideas are from omap-aes.c drivers.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/err.h>
21 #include <linux/clk.h>
23 #include <linux/hw_random.h>
24 #include <linux/platform_device.h>
26 #include <linux/device.h>
27 #include <linux/init.h>
28 #include <linux/errno.h>
29 #include <linux/interrupt.h>
30 #include <linux/irq.h>
31 #include <linux/scatterlist.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/of_device.h>
34 #include <linux/delay.h>
35 #include <linux/crypto.h>
36 #include <linux/cryptohash.h>
37 #include <crypto/scatterwalk.h>
38 #include <crypto/algapi.h>
39 #include <crypto/des.h>
40 #include <crypto/hash.h>
41 #include <crypto/internal/hash.h>
42 #include <linux/platform_data/crypto-atmel.h>
43 #include "atmel-tdes-regs.h"
46 #define TDES_FLAGS_MODE_MASK 0x00ff
47 #define TDES_FLAGS_ENCRYPT BIT(0)
48 #define TDES_FLAGS_CBC BIT(1)
49 #define TDES_FLAGS_CFB BIT(2)
50 #define TDES_FLAGS_CFB8 BIT(3)
51 #define TDES_FLAGS_CFB16 BIT(4)
52 #define TDES_FLAGS_CFB32 BIT(5)
53 #define TDES_FLAGS_CFB64 BIT(6)
54 #define TDES_FLAGS_OFB BIT(7)
56 #define TDES_FLAGS_INIT BIT(16)
57 #define TDES_FLAGS_FAST BIT(17)
58 #define TDES_FLAGS_BUSY BIT(18)
59 #define TDES_FLAGS_DMA BIT(19)
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
[3*DES_KEY_SIZE
/ sizeof(u32
)];
84 struct atmel_tdes_reqctx
{
88 struct atmel_tdes_dma
{
89 struct dma_chan
*chan
;
90 struct dma_slave_config dma_conf
;
93 struct atmel_tdes_dev
{
94 struct list_head list
;
95 unsigned long phys_base
;
96 void __iomem
*io_base
;
98 struct atmel_tdes_ctx
*ctx
;
107 struct crypto_queue queue
;
109 struct tasklet_struct done_task
;
110 struct tasklet_struct queue_task
;
112 struct ablkcipher_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 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_find_dev(struct atmel_tdes_ctx
*ctx
)
201 struct atmel_tdes_dev
*tdes_dd
= NULL
;
202 struct atmel_tdes_dev
*tmp
;
204 spin_lock_bh(&atmel_tdes
.lock
);
206 list_for_each_entry(tmp
, &atmel_tdes
.dev_list
, list
) {
214 spin_unlock_bh(&atmel_tdes
.lock
);
219 static int atmel_tdes_hw_init(struct atmel_tdes_dev
*dd
)
223 err
= clk_prepare_enable(dd
->iclk
);
227 if (!(dd
->flags
& TDES_FLAGS_INIT
)) {
228 atmel_tdes_write(dd
, TDES_CR
, TDES_CR_SWRST
);
229 dd
->flags
|= TDES_FLAGS_INIT
;
236 static inline unsigned int atmel_tdes_get_version(struct atmel_tdes_dev
*dd
)
238 return atmel_tdes_read(dd
, TDES_HW_VERSION
) & 0x00000fff;
241 static void atmel_tdes_hw_version_init(struct atmel_tdes_dev
*dd
)
243 atmel_tdes_hw_init(dd
);
245 dd
->hw_version
= atmel_tdes_get_version(dd
);
248 "version: 0x%x\n", dd
->hw_version
);
250 clk_disable_unprepare(dd
->iclk
);
253 static void atmel_tdes_dma_callback(void *data
)
255 struct atmel_tdes_dev
*dd
= data
;
257 /* dma_lch_out - completed */
258 tasklet_schedule(&dd
->done_task
);
261 static int atmel_tdes_write_ctrl(struct atmel_tdes_dev
*dd
)
264 u32 valcr
= 0, valmr
= TDES_MR_SMOD_PDC
;
266 err
= atmel_tdes_hw_init(dd
);
271 if (!dd
->caps
.has_dma
)
272 atmel_tdes_write(dd
, TDES_PTCR
,
273 TDES_PTCR_TXTDIS
| TDES_PTCR_RXTDIS
);
275 /* MR register must be set before IV registers */
276 if (dd
->ctx
->keylen
> (DES_KEY_SIZE
<< 1)) {
277 valmr
|= TDES_MR_KEYMOD_3KEY
;
278 valmr
|= TDES_MR_TDESMOD_TDES
;
279 } else if (dd
->ctx
->keylen
> DES_KEY_SIZE
) {
280 valmr
|= TDES_MR_KEYMOD_2KEY
;
281 valmr
|= TDES_MR_TDESMOD_TDES
;
283 valmr
|= TDES_MR_TDESMOD_DES
;
286 if (dd
->flags
& TDES_FLAGS_CBC
) {
287 valmr
|= TDES_MR_OPMOD_CBC
;
288 } else if (dd
->flags
& TDES_FLAGS_CFB
) {
289 valmr
|= TDES_MR_OPMOD_CFB
;
291 if (dd
->flags
& TDES_FLAGS_CFB8
)
292 valmr
|= TDES_MR_CFBS_8b
;
293 else if (dd
->flags
& TDES_FLAGS_CFB16
)
294 valmr
|= TDES_MR_CFBS_16b
;
295 else if (dd
->flags
& TDES_FLAGS_CFB32
)
296 valmr
|= TDES_MR_CFBS_32b
;
297 else if (dd
->flags
& TDES_FLAGS_CFB64
)
298 valmr
|= TDES_MR_CFBS_64b
;
299 } else if (dd
->flags
& TDES_FLAGS_OFB
) {
300 valmr
|= TDES_MR_OPMOD_OFB
;
303 if ((dd
->flags
& TDES_FLAGS_ENCRYPT
) || (dd
->flags
& TDES_FLAGS_OFB
))
304 valmr
|= TDES_MR_CYPHER_ENC
;
306 atmel_tdes_write(dd
, TDES_CR
, valcr
);
307 atmel_tdes_write(dd
, TDES_MR
, valmr
);
309 atmel_tdes_write_n(dd
, TDES_KEY1W1R
, dd
->ctx
->key
,
310 dd
->ctx
->keylen
>> 2);
312 if (((dd
->flags
& TDES_FLAGS_CBC
) || (dd
->flags
& TDES_FLAGS_CFB
) ||
313 (dd
->flags
& TDES_FLAGS_OFB
)) && dd
->req
->info
) {
314 atmel_tdes_write_n(dd
, TDES_IV1R
, dd
->req
->info
, 2);
320 static int atmel_tdes_crypt_pdc_stop(struct atmel_tdes_dev
*dd
)
325 atmel_tdes_write(dd
, TDES_PTCR
, TDES_PTCR_TXTDIS
|TDES_PTCR_RXTDIS
);
327 if (dd
->flags
& TDES_FLAGS_FAST
) {
328 dma_unmap_sg(dd
->dev
, dd
->out_sg
, 1, DMA_FROM_DEVICE
);
329 dma_unmap_sg(dd
->dev
, dd
->in_sg
, 1, DMA_TO_DEVICE
);
331 dma_sync_single_for_device(dd
->dev
, dd
->dma_addr_out
,
332 dd
->dma_size
, DMA_FROM_DEVICE
);
335 count
= atmel_tdes_sg_copy(&dd
->out_sg
, &dd
->out_offset
,
336 dd
->buf_out
, dd
->buflen
, dd
->dma_size
, 1);
337 if (count
!= dd
->dma_size
) {
339 pr_err("not all data converted: %zu\n", count
);
346 static int atmel_tdes_buff_init(struct atmel_tdes_dev
*dd
)
350 dd
->buf_in
= (void *)__get_free_pages(GFP_KERNEL
, 0);
351 dd
->buf_out
= (void *)__get_free_pages(GFP_KERNEL
, 0);
352 dd
->buflen
= PAGE_SIZE
;
353 dd
->buflen
&= ~(DES_BLOCK_SIZE
- 1);
355 if (!dd
->buf_in
|| !dd
->buf_out
) {
356 dev_err(dd
->dev
, "unable to alloc pages.\n");
361 dd
->dma_addr_in
= dma_map_single(dd
->dev
, dd
->buf_in
,
362 dd
->buflen
, DMA_TO_DEVICE
);
363 if (dma_mapping_error(dd
->dev
, dd
->dma_addr_in
)) {
364 dev_err(dd
->dev
, "dma %zd bytes error\n", dd
->buflen
);
369 dd
->dma_addr_out
= dma_map_single(dd
->dev
, dd
->buf_out
,
370 dd
->buflen
, DMA_FROM_DEVICE
);
371 if (dma_mapping_error(dd
->dev
, dd
->dma_addr_out
)) {
372 dev_err(dd
->dev
, "dma %zd bytes error\n", dd
->buflen
);
380 dma_unmap_single(dd
->dev
, dd
->dma_addr_in
, dd
->buflen
,
384 free_page((unsigned long)dd
->buf_out
);
385 free_page((unsigned long)dd
->buf_in
);
387 pr_err("error: %d\n", err
);
391 static void atmel_tdes_buff_cleanup(struct atmel_tdes_dev
*dd
)
393 dma_unmap_single(dd
->dev
, dd
->dma_addr_out
, dd
->buflen
,
395 dma_unmap_single(dd
->dev
, dd
->dma_addr_in
, dd
->buflen
,
397 free_page((unsigned long)dd
->buf_out
);
398 free_page((unsigned long)dd
->buf_in
);
401 static int atmel_tdes_crypt_pdc(struct crypto_tfm
*tfm
, dma_addr_t dma_addr_in
,
402 dma_addr_t dma_addr_out
, int length
)
404 struct atmel_tdes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
405 struct atmel_tdes_dev
*dd
= ctx
->dd
;
408 dd
->dma_size
= length
;
410 if (!(dd
->flags
& TDES_FLAGS_FAST
)) {
411 dma_sync_single_for_device(dd
->dev
, dma_addr_in
, length
,
415 if ((dd
->flags
& TDES_FLAGS_CFB
) && (dd
->flags
& TDES_FLAGS_CFB8
))
416 len32
= DIV_ROUND_UP(length
, sizeof(u8
));
417 else if ((dd
->flags
& TDES_FLAGS_CFB
) && (dd
->flags
& TDES_FLAGS_CFB16
))
418 len32
= DIV_ROUND_UP(length
, sizeof(u16
));
420 len32
= DIV_ROUND_UP(length
, sizeof(u32
));
422 atmel_tdes_write(dd
, TDES_PTCR
, TDES_PTCR_TXTDIS
|TDES_PTCR_RXTDIS
);
423 atmel_tdes_write(dd
, TDES_TPR
, dma_addr_in
);
424 atmel_tdes_write(dd
, TDES_TCR
, len32
);
425 atmel_tdes_write(dd
, TDES_RPR
, dma_addr_out
);
426 atmel_tdes_write(dd
, TDES_RCR
, len32
);
428 /* Enable Interrupt */
429 atmel_tdes_write(dd
, TDES_IER
, TDES_INT_ENDRX
);
431 /* Start DMA transfer */
432 atmel_tdes_write(dd
, TDES_PTCR
, TDES_PTCR_TXTEN
| TDES_PTCR_RXTEN
);
437 static int atmel_tdes_crypt_dma(struct crypto_tfm
*tfm
, dma_addr_t dma_addr_in
,
438 dma_addr_t dma_addr_out
, int length
)
440 struct atmel_tdes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
441 struct atmel_tdes_dev
*dd
= ctx
->dd
;
442 struct scatterlist sg
[2];
443 struct dma_async_tx_descriptor
*in_desc
, *out_desc
;
445 dd
->dma_size
= length
;
447 if (!(dd
->flags
& TDES_FLAGS_FAST
)) {
448 dma_sync_single_for_device(dd
->dev
, dma_addr_in
, length
,
452 if (dd
->flags
& TDES_FLAGS_CFB8
) {
453 dd
->dma_lch_in
.dma_conf
.dst_addr_width
=
454 DMA_SLAVE_BUSWIDTH_1_BYTE
;
455 dd
->dma_lch_out
.dma_conf
.src_addr_width
=
456 DMA_SLAVE_BUSWIDTH_1_BYTE
;
457 } else if (dd
->flags
& TDES_FLAGS_CFB16
) {
458 dd
->dma_lch_in
.dma_conf
.dst_addr_width
=
459 DMA_SLAVE_BUSWIDTH_2_BYTES
;
460 dd
->dma_lch_out
.dma_conf
.src_addr_width
=
461 DMA_SLAVE_BUSWIDTH_2_BYTES
;
463 dd
->dma_lch_in
.dma_conf
.dst_addr_width
=
464 DMA_SLAVE_BUSWIDTH_4_BYTES
;
465 dd
->dma_lch_out
.dma_conf
.src_addr_width
=
466 DMA_SLAVE_BUSWIDTH_4_BYTES
;
469 dmaengine_slave_config(dd
->dma_lch_in
.chan
, &dd
->dma_lch_in
.dma_conf
);
470 dmaengine_slave_config(dd
->dma_lch_out
.chan
, &dd
->dma_lch_out
.dma_conf
);
472 dd
->flags
|= TDES_FLAGS_DMA
;
474 sg_init_table(&sg
[0], 1);
475 sg_dma_address(&sg
[0]) = dma_addr_in
;
476 sg_dma_len(&sg
[0]) = length
;
478 sg_init_table(&sg
[1], 1);
479 sg_dma_address(&sg
[1]) = dma_addr_out
;
480 sg_dma_len(&sg
[1]) = length
;
482 in_desc
= dmaengine_prep_slave_sg(dd
->dma_lch_in
.chan
, &sg
[0],
484 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
488 out_desc
= dmaengine_prep_slave_sg(dd
->dma_lch_out
.chan
, &sg
[1],
490 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
494 out_desc
->callback
= atmel_tdes_dma_callback
;
495 out_desc
->callback_param
= dd
;
497 dmaengine_submit(out_desc
);
498 dma_async_issue_pending(dd
->dma_lch_out
.chan
);
500 dmaengine_submit(in_desc
);
501 dma_async_issue_pending(dd
->dma_lch_in
.chan
);
506 static int atmel_tdes_crypt_start(struct atmel_tdes_dev
*dd
)
508 struct crypto_tfm
*tfm
= crypto_ablkcipher_tfm(
509 crypto_ablkcipher_reqtfm(dd
->req
));
510 int err
, fast
= 0, in
, out
;
512 dma_addr_t addr_in
, addr_out
;
514 if ((!dd
->in_offset
) && (!dd
->out_offset
)) {
515 /* check for alignment */
516 in
= IS_ALIGNED((u32
)dd
->in_sg
->offset
, sizeof(u32
)) &&
517 IS_ALIGNED(dd
->in_sg
->length
, dd
->ctx
->block_size
);
518 out
= IS_ALIGNED((u32
)dd
->out_sg
->offset
, sizeof(u32
)) &&
519 IS_ALIGNED(dd
->out_sg
->length
, dd
->ctx
->block_size
);
522 if (sg_dma_len(dd
->in_sg
) != sg_dma_len(dd
->out_sg
))
528 count
= min_t(size_t, dd
->total
, sg_dma_len(dd
->in_sg
));
529 count
= min_t(size_t, count
, sg_dma_len(dd
->out_sg
));
531 err
= dma_map_sg(dd
->dev
, dd
->in_sg
, 1, DMA_TO_DEVICE
);
533 dev_err(dd
->dev
, "dma_map_sg() error\n");
537 err
= dma_map_sg(dd
->dev
, dd
->out_sg
, 1,
540 dev_err(dd
->dev
, "dma_map_sg() error\n");
541 dma_unmap_sg(dd
->dev
, dd
->in_sg
, 1,
546 addr_in
= sg_dma_address(dd
->in_sg
);
547 addr_out
= sg_dma_address(dd
->out_sg
);
549 dd
->flags
|= TDES_FLAGS_FAST
;
552 /* use cache buffers */
553 count
= atmel_tdes_sg_copy(&dd
->in_sg
, &dd
->in_offset
,
554 dd
->buf_in
, dd
->buflen
, dd
->total
, 0);
556 addr_in
= dd
->dma_addr_in
;
557 addr_out
= dd
->dma_addr_out
;
559 dd
->flags
&= ~TDES_FLAGS_FAST
;
564 if (dd
->caps
.has_dma
)
565 err
= atmel_tdes_crypt_dma(tfm
, addr_in
, addr_out
, count
);
567 err
= atmel_tdes_crypt_pdc(tfm
, addr_in
, addr_out
, count
);
569 if (err
&& (dd
->flags
& TDES_FLAGS_FAST
)) {
570 dma_unmap_sg(dd
->dev
, dd
->in_sg
, 1, DMA_TO_DEVICE
);
571 dma_unmap_sg(dd
->dev
, dd
->out_sg
, 1, DMA_TO_DEVICE
);
577 static void atmel_tdes_finish_req(struct atmel_tdes_dev
*dd
, int err
)
579 struct ablkcipher_request
*req
= dd
->req
;
581 clk_disable_unprepare(dd
->iclk
);
583 dd
->flags
&= ~TDES_FLAGS_BUSY
;
585 req
->base
.complete(&req
->base
, err
);
588 static int atmel_tdes_handle_queue(struct atmel_tdes_dev
*dd
,
589 struct ablkcipher_request
*req
)
591 struct crypto_async_request
*async_req
, *backlog
;
592 struct atmel_tdes_ctx
*ctx
;
593 struct atmel_tdes_reqctx
*rctx
;
597 spin_lock_irqsave(&dd
->lock
, flags
);
599 ret
= ablkcipher_enqueue_request(&dd
->queue
, req
);
600 if (dd
->flags
& TDES_FLAGS_BUSY
) {
601 spin_unlock_irqrestore(&dd
->lock
, flags
);
604 backlog
= crypto_get_backlog(&dd
->queue
);
605 async_req
= crypto_dequeue_request(&dd
->queue
);
607 dd
->flags
|= TDES_FLAGS_BUSY
;
608 spin_unlock_irqrestore(&dd
->lock
, flags
);
614 backlog
->complete(backlog
, -EINPROGRESS
);
616 req
= ablkcipher_request_cast(async_req
);
618 /* assign new request to device */
620 dd
->total
= req
->nbytes
;
622 dd
->in_sg
= req
->src
;
624 dd
->out_sg
= req
->dst
;
626 rctx
= ablkcipher_request_ctx(req
);
627 ctx
= crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req
));
628 rctx
->mode
&= TDES_FLAGS_MODE_MASK
;
629 dd
->flags
= (dd
->flags
& ~TDES_FLAGS_MODE_MASK
) | rctx
->mode
;
633 err
= atmel_tdes_write_ctrl(dd
);
635 err
= atmel_tdes_crypt_start(dd
);
637 /* des_task will not finish it, so do it here */
638 atmel_tdes_finish_req(dd
, err
);
639 tasklet_schedule(&dd
->queue_task
);
645 static int atmel_tdes_crypt_dma_stop(struct atmel_tdes_dev
*dd
)
650 if (dd
->flags
& TDES_FLAGS_DMA
) {
652 if (dd
->flags
& TDES_FLAGS_FAST
) {
653 dma_unmap_sg(dd
->dev
, dd
->out_sg
, 1, DMA_FROM_DEVICE
);
654 dma_unmap_sg(dd
->dev
, dd
->in_sg
, 1, DMA_TO_DEVICE
);
656 dma_sync_single_for_device(dd
->dev
, dd
->dma_addr_out
,
657 dd
->dma_size
, DMA_FROM_DEVICE
);
660 count
= atmel_tdes_sg_copy(&dd
->out_sg
, &dd
->out_offset
,
661 dd
->buf_out
, dd
->buflen
, dd
->dma_size
, 1);
662 if (count
!= dd
->dma_size
) {
664 pr_err("not all data converted: %zu\n", count
);
671 static int atmel_tdes_crypt(struct ablkcipher_request
*req
, unsigned long mode
)
673 struct atmel_tdes_ctx
*ctx
= crypto_ablkcipher_ctx(
674 crypto_ablkcipher_reqtfm(req
));
675 struct atmel_tdes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
677 if (mode
& TDES_FLAGS_CFB8
) {
678 if (!IS_ALIGNED(req
->nbytes
, CFB8_BLOCK_SIZE
)) {
679 pr_err("request size is not exact amount of CFB8 blocks\n");
682 ctx
->block_size
= CFB8_BLOCK_SIZE
;
683 } else if (mode
& TDES_FLAGS_CFB16
) {
684 if (!IS_ALIGNED(req
->nbytes
, CFB16_BLOCK_SIZE
)) {
685 pr_err("request size is not exact amount of CFB16 blocks\n");
688 ctx
->block_size
= CFB16_BLOCK_SIZE
;
689 } else if (mode
& TDES_FLAGS_CFB32
) {
690 if (!IS_ALIGNED(req
->nbytes
, CFB32_BLOCK_SIZE
)) {
691 pr_err("request size is not exact amount of CFB32 blocks\n");
694 ctx
->block_size
= CFB32_BLOCK_SIZE
;
696 if (!IS_ALIGNED(req
->nbytes
, DES_BLOCK_SIZE
)) {
697 pr_err("request size is not exact amount of DES blocks\n");
700 ctx
->block_size
= DES_BLOCK_SIZE
;
705 return atmel_tdes_handle_queue(ctx
->dd
, req
);
708 static bool atmel_tdes_filter(struct dma_chan
*chan
, void *slave
)
710 struct at_dma_slave
*sl
= slave
;
712 if (sl
&& sl
->dma_dev
== chan
->device
->dev
) {
720 static int atmel_tdes_dma_init(struct atmel_tdes_dev
*dd
,
721 struct crypto_platform_data
*pdata
)
726 dma_cap_set(DMA_SLAVE
, mask
);
728 /* Try to grab 2 DMA channels */
729 dd
->dma_lch_in
.chan
= dma_request_slave_channel_compat(mask
,
730 atmel_tdes_filter
, &pdata
->dma_slave
->rxdata
, dd
->dev
, "tx");
731 if (!dd
->dma_lch_in
.chan
)
734 dd
->dma_lch_in
.dma_conf
.direction
= DMA_MEM_TO_DEV
;
735 dd
->dma_lch_in
.dma_conf
.dst_addr
= dd
->phys_base
+
737 dd
->dma_lch_in
.dma_conf
.src_maxburst
= 1;
738 dd
->dma_lch_in
.dma_conf
.src_addr_width
=
739 DMA_SLAVE_BUSWIDTH_4_BYTES
;
740 dd
->dma_lch_in
.dma_conf
.dst_maxburst
= 1;
741 dd
->dma_lch_in
.dma_conf
.dst_addr_width
=
742 DMA_SLAVE_BUSWIDTH_4_BYTES
;
743 dd
->dma_lch_in
.dma_conf
.device_fc
= false;
745 dd
->dma_lch_out
.chan
= dma_request_slave_channel_compat(mask
,
746 atmel_tdes_filter
, &pdata
->dma_slave
->txdata
, dd
->dev
, "rx");
747 if (!dd
->dma_lch_out
.chan
)
750 dd
->dma_lch_out
.dma_conf
.direction
= DMA_DEV_TO_MEM
;
751 dd
->dma_lch_out
.dma_conf
.src_addr
= dd
->phys_base
+
753 dd
->dma_lch_out
.dma_conf
.src_maxburst
= 1;
754 dd
->dma_lch_out
.dma_conf
.src_addr_width
=
755 DMA_SLAVE_BUSWIDTH_4_BYTES
;
756 dd
->dma_lch_out
.dma_conf
.dst_maxburst
= 1;
757 dd
->dma_lch_out
.dma_conf
.dst_addr_width
=
758 DMA_SLAVE_BUSWIDTH_4_BYTES
;
759 dd
->dma_lch_out
.dma_conf
.device_fc
= false;
764 dma_release_channel(dd
->dma_lch_in
.chan
);
766 dev_warn(dd
->dev
, "no DMA channel available\n");
770 static void atmel_tdes_dma_cleanup(struct atmel_tdes_dev
*dd
)
772 dma_release_channel(dd
->dma_lch_in
.chan
);
773 dma_release_channel(dd
->dma_lch_out
.chan
);
776 static int atmel_des_setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
779 u32 tmp
[DES_EXPKEY_WORDS
];
781 struct crypto_tfm
*ctfm
= crypto_ablkcipher_tfm(tfm
);
783 struct atmel_tdes_ctx
*ctx
= crypto_ablkcipher_ctx(tfm
);
785 if (keylen
!= DES_KEY_SIZE
) {
786 crypto_ablkcipher_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
790 err
= des_ekey(tmp
, key
);
791 if (err
== 0 && (ctfm
->crt_flags
& CRYPTO_TFM_REQ_WEAK_KEY
)) {
792 ctfm
->crt_flags
|= CRYPTO_TFM_RES_WEAK_KEY
;
796 memcpy(ctx
->key
, key
, keylen
);
797 ctx
->keylen
= keylen
;
802 static int atmel_tdes_setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
805 struct atmel_tdes_ctx
*ctx
= crypto_ablkcipher_ctx(tfm
);
806 const char *alg_name
;
808 alg_name
= crypto_tfm_alg_name(crypto_ablkcipher_tfm(tfm
));
811 * HW bug in cfb 3-keys mode.
813 if (!ctx
->dd
->caps
.has_cfb_3keys
&& strstr(alg_name
, "cfb")
814 && (keylen
!= 2*DES_KEY_SIZE
)) {
815 crypto_ablkcipher_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
817 } else if ((keylen
!= 2*DES_KEY_SIZE
) && (keylen
!= 3*DES_KEY_SIZE
)) {
818 crypto_ablkcipher_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
822 memcpy(ctx
->key
, key
, keylen
);
823 ctx
->keylen
= keylen
;
828 static int atmel_tdes_ecb_encrypt(struct ablkcipher_request
*req
)
830 return atmel_tdes_crypt(req
, TDES_FLAGS_ENCRYPT
);
833 static int atmel_tdes_ecb_decrypt(struct ablkcipher_request
*req
)
835 return atmel_tdes_crypt(req
, 0);
838 static int atmel_tdes_cbc_encrypt(struct ablkcipher_request
*req
)
840 return atmel_tdes_crypt(req
, TDES_FLAGS_ENCRYPT
| TDES_FLAGS_CBC
);
843 static int atmel_tdes_cbc_decrypt(struct ablkcipher_request
*req
)
845 return atmel_tdes_crypt(req
, TDES_FLAGS_CBC
);
847 static int atmel_tdes_cfb_encrypt(struct ablkcipher_request
*req
)
849 return atmel_tdes_crypt(req
, TDES_FLAGS_ENCRYPT
| TDES_FLAGS_CFB
);
852 static int atmel_tdes_cfb_decrypt(struct ablkcipher_request
*req
)
854 return atmel_tdes_crypt(req
, TDES_FLAGS_CFB
);
857 static int atmel_tdes_cfb8_encrypt(struct ablkcipher_request
*req
)
859 return atmel_tdes_crypt(req
, TDES_FLAGS_ENCRYPT
| TDES_FLAGS_CFB
|
863 static int atmel_tdes_cfb8_decrypt(struct ablkcipher_request
*req
)
865 return atmel_tdes_crypt(req
, TDES_FLAGS_CFB
| TDES_FLAGS_CFB8
);
868 static int atmel_tdes_cfb16_encrypt(struct ablkcipher_request
*req
)
870 return atmel_tdes_crypt(req
, TDES_FLAGS_ENCRYPT
| TDES_FLAGS_CFB
|
874 static int atmel_tdes_cfb16_decrypt(struct ablkcipher_request
*req
)
876 return atmel_tdes_crypt(req
, TDES_FLAGS_CFB
| TDES_FLAGS_CFB16
);
879 static int atmel_tdes_cfb32_encrypt(struct ablkcipher_request
*req
)
881 return atmel_tdes_crypt(req
, TDES_FLAGS_ENCRYPT
| TDES_FLAGS_CFB
|
885 static int atmel_tdes_cfb32_decrypt(struct ablkcipher_request
*req
)
887 return atmel_tdes_crypt(req
, TDES_FLAGS_CFB
| TDES_FLAGS_CFB32
);
890 static int atmel_tdes_ofb_encrypt(struct ablkcipher_request
*req
)
892 return atmel_tdes_crypt(req
, TDES_FLAGS_ENCRYPT
| TDES_FLAGS_OFB
);
895 static int atmel_tdes_ofb_decrypt(struct ablkcipher_request
*req
)
897 return atmel_tdes_crypt(req
, TDES_FLAGS_OFB
);
900 static int atmel_tdes_cra_init(struct crypto_tfm
*tfm
)
902 struct atmel_tdes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
903 struct atmel_tdes_dev
*dd
;
905 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct atmel_tdes_reqctx
);
907 dd
= atmel_tdes_find_dev(ctx
);
914 static struct crypto_alg tdes_algs
[] = {
916 .cra_name
= "ecb(des)",
917 .cra_driver_name
= "atmel-ecb-des",
919 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
920 .cra_blocksize
= DES_BLOCK_SIZE
,
921 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
922 .cra_alignmask
= 0x7,
923 .cra_type
= &crypto_ablkcipher_type
,
924 .cra_module
= THIS_MODULE
,
925 .cra_init
= atmel_tdes_cra_init
,
926 .cra_u
.ablkcipher
= {
927 .min_keysize
= DES_KEY_SIZE
,
928 .max_keysize
= DES_KEY_SIZE
,
929 .setkey
= atmel_des_setkey
,
930 .encrypt
= atmel_tdes_ecb_encrypt
,
931 .decrypt
= atmel_tdes_ecb_decrypt
,
935 .cra_name
= "cbc(des)",
936 .cra_driver_name
= "atmel-cbc-des",
938 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
939 .cra_blocksize
= DES_BLOCK_SIZE
,
940 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
941 .cra_alignmask
= 0x7,
942 .cra_type
= &crypto_ablkcipher_type
,
943 .cra_module
= THIS_MODULE
,
944 .cra_init
= atmel_tdes_cra_init
,
945 .cra_u
.ablkcipher
= {
946 .min_keysize
= DES_KEY_SIZE
,
947 .max_keysize
= DES_KEY_SIZE
,
948 .ivsize
= DES_BLOCK_SIZE
,
949 .setkey
= atmel_des_setkey
,
950 .encrypt
= atmel_tdes_cbc_encrypt
,
951 .decrypt
= atmel_tdes_cbc_decrypt
,
955 .cra_name
= "cfb(des)",
956 .cra_driver_name
= "atmel-cfb-des",
958 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
959 .cra_blocksize
= DES_BLOCK_SIZE
,
960 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
961 .cra_alignmask
= 0x7,
962 .cra_type
= &crypto_ablkcipher_type
,
963 .cra_module
= THIS_MODULE
,
964 .cra_init
= atmel_tdes_cra_init
,
965 .cra_u
.ablkcipher
= {
966 .min_keysize
= DES_KEY_SIZE
,
967 .max_keysize
= DES_KEY_SIZE
,
968 .ivsize
= DES_BLOCK_SIZE
,
969 .setkey
= atmel_des_setkey
,
970 .encrypt
= atmel_tdes_cfb_encrypt
,
971 .decrypt
= atmel_tdes_cfb_decrypt
,
975 .cra_name
= "cfb8(des)",
976 .cra_driver_name
= "atmel-cfb8-des",
978 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
979 .cra_blocksize
= CFB8_BLOCK_SIZE
,
980 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
982 .cra_type
= &crypto_ablkcipher_type
,
983 .cra_module
= THIS_MODULE
,
984 .cra_init
= atmel_tdes_cra_init
,
985 .cra_u
.ablkcipher
= {
986 .min_keysize
= DES_KEY_SIZE
,
987 .max_keysize
= DES_KEY_SIZE
,
988 .ivsize
= DES_BLOCK_SIZE
,
989 .setkey
= atmel_des_setkey
,
990 .encrypt
= atmel_tdes_cfb8_encrypt
,
991 .decrypt
= atmel_tdes_cfb8_decrypt
,
995 .cra_name
= "cfb16(des)",
996 .cra_driver_name
= "atmel-cfb16-des",
998 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
999 .cra_blocksize
= CFB16_BLOCK_SIZE
,
1000 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1001 .cra_alignmask
= 0x1,
1002 .cra_type
= &crypto_ablkcipher_type
,
1003 .cra_module
= THIS_MODULE
,
1004 .cra_init
= atmel_tdes_cra_init
,
1005 .cra_u
.ablkcipher
= {
1006 .min_keysize
= DES_KEY_SIZE
,
1007 .max_keysize
= DES_KEY_SIZE
,
1008 .ivsize
= DES_BLOCK_SIZE
,
1009 .setkey
= atmel_des_setkey
,
1010 .encrypt
= atmel_tdes_cfb16_encrypt
,
1011 .decrypt
= atmel_tdes_cfb16_decrypt
,
1015 .cra_name
= "cfb32(des)",
1016 .cra_driver_name
= "atmel-cfb32-des",
1017 .cra_priority
= 100,
1018 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1019 .cra_blocksize
= CFB32_BLOCK_SIZE
,
1020 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1021 .cra_alignmask
= 0x3,
1022 .cra_type
= &crypto_ablkcipher_type
,
1023 .cra_module
= THIS_MODULE
,
1024 .cra_init
= atmel_tdes_cra_init
,
1025 .cra_u
.ablkcipher
= {
1026 .min_keysize
= DES_KEY_SIZE
,
1027 .max_keysize
= DES_KEY_SIZE
,
1028 .ivsize
= DES_BLOCK_SIZE
,
1029 .setkey
= atmel_des_setkey
,
1030 .encrypt
= atmel_tdes_cfb32_encrypt
,
1031 .decrypt
= atmel_tdes_cfb32_decrypt
,
1035 .cra_name
= "ofb(des)",
1036 .cra_driver_name
= "atmel-ofb-des",
1037 .cra_priority
= 100,
1038 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1039 .cra_blocksize
= DES_BLOCK_SIZE
,
1040 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1041 .cra_alignmask
= 0x7,
1042 .cra_type
= &crypto_ablkcipher_type
,
1043 .cra_module
= THIS_MODULE
,
1044 .cra_init
= atmel_tdes_cra_init
,
1045 .cra_u
.ablkcipher
= {
1046 .min_keysize
= DES_KEY_SIZE
,
1047 .max_keysize
= DES_KEY_SIZE
,
1048 .ivsize
= DES_BLOCK_SIZE
,
1049 .setkey
= atmel_des_setkey
,
1050 .encrypt
= atmel_tdes_ofb_encrypt
,
1051 .decrypt
= atmel_tdes_ofb_decrypt
,
1055 .cra_name
= "ecb(des3_ede)",
1056 .cra_driver_name
= "atmel-ecb-tdes",
1057 .cra_priority
= 100,
1058 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1059 .cra_blocksize
= DES_BLOCK_SIZE
,
1060 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1061 .cra_alignmask
= 0x7,
1062 .cra_type
= &crypto_ablkcipher_type
,
1063 .cra_module
= THIS_MODULE
,
1064 .cra_init
= atmel_tdes_cra_init
,
1065 .cra_u
.ablkcipher
= {
1066 .min_keysize
= 2 * DES_KEY_SIZE
,
1067 .max_keysize
= 3 * DES_KEY_SIZE
,
1068 .setkey
= atmel_tdes_setkey
,
1069 .encrypt
= atmel_tdes_ecb_encrypt
,
1070 .decrypt
= atmel_tdes_ecb_decrypt
,
1074 .cra_name
= "cbc(des3_ede)",
1075 .cra_driver_name
= "atmel-cbc-tdes",
1076 .cra_priority
= 100,
1077 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1078 .cra_blocksize
= DES_BLOCK_SIZE
,
1079 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1080 .cra_alignmask
= 0x7,
1081 .cra_type
= &crypto_ablkcipher_type
,
1082 .cra_module
= THIS_MODULE
,
1083 .cra_init
= atmel_tdes_cra_init
,
1084 .cra_u
.ablkcipher
= {
1085 .min_keysize
= 2*DES_KEY_SIZE
,
1086 .max_keysize
= 3*DES_KEY_SIZE
,
1087 .ivsize
= DES_BLOCK_SIZE
,
1088 .setkey
= atmel_tdes_setkey
,
1089 .encrypt
= atmel_tdes_cbc_encrypt
,
1090 .decrypt
= atmel_tdes_cbc_decrypt
,
1094 .cra_name
= "cfb(des3_ede)",
1095 .cra_driver_name
= "atmel-cfb-tdes",
1096 .cra_priority
= 100,
1097 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1098 .cra_blocksize
= DES_BLOCK_SIZE
,
1099 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1100 .cra_alignmask
= 0x7,
1101 .cra_type
= &crypto_ablkcipher_type
,
1102 .cra_module
= THIS_MODULE
,
1103 .cra_init
= atmel_tdes_cra_init
,
1104 .cra_u
.ablkcipher
= {
1105 .min_keysize
= 2*DES_KEY_SIZE
,
1106 .max_keysize
= 2*DES_KEY_SIZE
,
1107 .ivsize
= DES_BLOCK_SIZE
,
1108 .setkey
= atmel_tdes_setkey
,
1109 .encrypt
= atmel_tdes_cfb_encrypt
,
1110 .decrypt
= atmel_tdes_cfb_decrypt
,
1114 .cra_name
= "cfb8(des3_ede)",
1115 .cra_driver_name
= "atmel-cfb8-tdes",
1116 .cra_priority
= 100,
1117 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1118 .cra_blocksize
= CFB8_BLOCK_SIZE
,
1119 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1121 .cra_type
= &crypto_ablkcipher_type
,
1122 .cra_module
= THIS_MODULE
,
1123 .cra_init
= atmel_tdes_cra_init
,
1124 .cra_u
.ablkcipher
= {
1125 .min_keysize
= 2*DES_KEY_SIZE
,
1126 .max_keysize
= 2*DES_KEY_SIZE
,
1127 .ivsize
= DES_BLOCK_SIZE
,
1128 .setkey
= atmel_tdes_setkey
,
1129 .encrypt
= atmel_tdes_cfb8_encrypt
,
1130 .decrypt
= atmel_tdes_cfb8_decrypt
,
1134 .cra_name
= "cfb16(des3_ede)",
1135 .cra_driver_name
= "atmel-cfb16-tdes",
1136 .cra_priority
= 100,
1137 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1138 .cra_blocksize
= CFB16_BLOCK_SIZE
,
1139 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1140 .cra_alignmask
= 0x1,
1141 .cra_type
= &crypto_ablkcipher_type
,
1142 .cra_module
= THIS_MODULE
,
1143 .cra_init
= atmel_tdes_cra_init
,
1144 .cra_u
.ablkcipher
= {
1145 .min_keysize
= 2*DES_KEY_SIZE
,
1146 .max_keysize
= 2*DES_KEY_SIZE
,
1147 .ivsize
= DES_BLOCK_SIZE
,
1148 .setkey
= atmel_tdes_setkey
,
1149 .encrypt
= atmel_tdes_cfb16_encrypt
,
1150 .decrypt
= atmel_tdes_cfb16_decrypt
,
1154 .cra_name
= "cfb32(des3_ede)",
1155 .cra_driver_name
= "atmel-cfb32-tdes",
1156 .cra_priority
= 100,
1157 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1158 .cra_blocksize
= CFB32_BLOCK_SIZE
,
1159 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1160 .cra_alignmask
= 0x3,
1161 .cra_type
= &crypto_ablkcipher_type
,
1162 .cra_module
= THIS_MODULE
,
1163 .cra_init
= atmel_tdes_cra_init
,
1164 .cra_u
.ablkcipher
= {
1165 .min_keysize
= 2*DES_KEY_SIZE
,
1166 .max_keysize
= 2*DES_KEY_SIZE
,
1167 .ivsize
= DES_BLOCK_SIZE
,
1168 .setkey
= atmel_tdes_setkey
,
1169 .encrypt
= atmel_tdes_cfb32_encrypt
,
1170 .decrypt
= atmel_tdes_cfb32_decrypt
,
1174 .cra_name
= "ofb(des3_ede)",
1175 .cra_driver_name
= "atmel-ofb-tdes",
1176 .cra_priority
= 100,
1177 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1178 .cra_blocksize
= DES_BLOCK_SIZE
,
1179 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1180 .cra_alignmask
= 0x7,
1181 .cra_type
= &crypto_ablkcipher_type
,
1182 .cra_module
= THIS_MODULE
,
1183 .cra_init
= atmel_tdes_cra_init
,
1184 .cra_u
.ablkcipher
= {
1185 .min_keysize
= 2*DES_KEY_SIZE
,
1186 .max_keysize
= 3*DES_KEY_SIZE
,
1187 .ivsize
= DES_BLOCK_SIZE
,
1188 .setkey
= atmel_tdes_setkey
,
1189 .encrypt
= atmel_tdes_ofb_encrypt
,
1190 .decrypt
= atmel_tdes_ofb_decrypt
,
1195 static void atmel_tdes_queue_task(unsigned long data
)
1197 struct atmel_tdes_dev
*dd
= (struct atmel_tdes_dev
*)data
;
1199 atmel_tdes_handle_queue(dd
, NULL
);
1202 static void atmel_tdes_done_task(unsigned long data
)
1204 struct atmel_tdes_dev
*dd
= (struct atmel_tdes_dev
*) data
;
1207 if (!(dd
->flags
& TDES_FLAGS_DMA
))
1208 err
= atmel_tdes_crypt_pdc_stop(dd
);
1210 err
= atmel_tdes_crypt_dma_stop(dd
);
1212 err
= dd
->err
? : err
;
1214 if (dd
->total
&& !err
) {
1215 if (dd
->flags
& TDES_FLAGS_FAST
) {
1216 dd
->in_sg
= sg_next(dd
->in_sg
);
1217 dd
->out_sg
= sg_next(dd
->out_sg
);
1218 if (!dd
->in_sg
|| !dd
->out_sg
)
1222 err
= atmel_tdes_crypt_start(dd
);
1224 return; /* DMA started. Not fininishing. */
1227 atmel_tdes_finish_req(dd
, err
);
1228 atmel_tdes_handle_queue(dd
, NULL
);
1231 static irqreturn_t
atmel_tdes_irq(int irq
, void *dev_id
)
1233 struct atmel_tdes_dev
*tdes_dd
= dev_id
;
1236 reg
= atmel_tdes_read(tdes_dd
, TDES_ISR
);
1237 if (reg
& atmel_tdes_read(tdes_dd
, TDES_IMR
)) {
1238 atmel_tdes_write(tdes_dd
, TDES_IDR
, reg
);
1239 if (TDES_FLAGS_BUSY
& tdes_dd
->flags
)
1240 tasklet_schedule(&tdes_dd
->done_task
);
1242 dev_warn(tdes_dd
->dev
, "TDES interrupt when no active requests.\n");
1249 static void atmel_tdes_unregister_algs(struct atmel_tdes_dev
*dd
)
1253 for (i
= 0; i
< ARRAY_SIZE(tdes_algs
); i
++)
1254 crypto_unregister_alg(&tdes_algs
[i
]);
1257 static int atmel_tdes_register_algs(struct atmel_tdes_dev
*dd
)
1261 for (i
= 0; i
< ARRAY_SIZE(tdes_algs
); i
++) {
1262 err
= crypto_register_alg(&tdes_algs
[i
]);
1270 for (j
= 0; j
< i
; j
++)
1271 crypto_unregister_alg(&tdes_algs
[j
]);
1276 static void atmel_tdes_get_cap(struct atmel_tdes_dev
*dd
)
1279 dd
->caps
.has_dma
= 0;
1280 dd
->caps
.has_cfb_3keys
= 0;
1282 /* keep only major version number */
1283 switch (dd
->hw_version
& 0xf00) {
1285 dd
->caps
.has_dma
= 1;
1286 dd
->caps
.has_cfb_3keys
= 1;
1292 "Unmanaged tdes version, set minimum capabilities\n");
1297 #if defined(CONFIG_OF)
1298 static const struct of_device_id atmel_tdes_dt_ids
[] = {
1299 { .compatible
= "atmel,at91sam9g46-tdes" },
1302 MODULE_DEVICE_TABLE(of
, atmel_tdes_dt_ids
);
1304 static struct crypto_platform_data
*atmel_tdes_of_init(struct platform_device
*pdev
)
1306 struct device_node
*np
= pdev
->dev
.of_node
;
1307 struct crypto_platform_data
*pdata
;
1310 dev_err(&pdev
->dev
, "device node not found\n");
1311 return ERR_PTR(-EINVAL
);
1314 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
1316 return ERR_PTR(-ENOMEM
);
1318 pdata
->dma_slave
= devm_kzalloc(&pdev
->dev
,
1319 sizeof(*(pdata
->dma_slave
)),
1321 if (!pdata
->dma_slave
)
1322 return ERR_PTR(-ENOMEM
);
1326 #else /* CONFIG_OF */
1327 static inline struct crypto_platform_data
*atmel_tdes_of_init(struct platform_device
*pdev
)
1329 return ERR_PTR(-EINVAL
);
1333 static int atmel_tdes_probe(struct platform_device
*pdev
)
1335 struct atmel_tdes_dev
*tdes_dd
;
1336 struct crypto_platform_data
*pdata
;
1337 struct device
*dev
= &pdev
->dev
;
1338 struct resource
*tdes_res
;
1341 tdes_dd
= devm_kmalloc(&pdev
->dev
, sizeof(*tdes_dd
), GFP_KERNEL
);
1342 if (tdes_dd
== NULL
) {
1349 platform_set_drvdata(pdev
, tdes_dd
);
1351 INIT_LIST_HEAD(&tdes_dd
->list
);
1352 spin_lock_init(&tdes_dd
->lock
);
1354 tasklet_init(&tdes_dd
->done_task
, atmel_tdes_done_task
,
1355 (unsigned long)tdes_dd
);
1356 tasklet_init(&tdes_dd
->queue_task
, atmel_tdes_queue_task
,
1357 (unsigned long)tdes_dd
);
1359 crypto_init_queue(&tdes_dd
->queue
, ATMEL_TDES_QUEUE_LENGTH
);
1361 /* Get the base address */
1362 tdes_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1364 dev_err(dev
, "no MEM resource info\n");
1368 tdes_dd
->phys_base
= tdes_res
->start
;
1371 tdes_dd
->irq
= platform_get_irq(pdev
, 0);
1372 if (tdes_dd
->irq
< 0) {
1373 dev_err(dev
, "no IRQ resource info\n");
1378 err
= devm_request_irq(&pdev
->dev
, tdes_dd
->irq
, atmel_tdes_irq
,
1379 IRQF_SHARED
, "atmel-tdes", tdes_dd
);
1381 dev_err(dev
, "unable to request tdes irq.\n");
1385 /* Initializing the clock */
1386 tdes_dd
->iclk
= devm_clk_get(&pdev
->dev
, "tdes_clk");
1387 if (IS_ERR(tdes_dd
->iclk
)) {
1388 dev_err(dev
, "clock initialization failed.\n");
1389 err
= PTR_ERR(tdes_dd
->iclk
);
1393 tdes_dd
->io_base
= devm_ioremap_resource(&pdev
->dev
, tdes_res
);
1394 if (IS_ERR(tdes_dd
->io_base
)) {
1395 dev_err(dev
, "can't ioremap\n");
1396 err
= PTR_ERR(tdes_dd
->io_base
);
1400 atmel_tdes_hw_version_init(tdes_dd
);
1402 atmel_tdes_get_cap(tdes_dd
);
1404 err
= atmel_tdes_buff_init(tdes_dd
);
1408 if (tdes_dd
->caps
.has_dma
) {
1409 pdata
= pdev
->dev
.platform_data
;
1411 pdata
= atmel_tdes_of_init(pdev
);
1412 if (IS_ERR(pdata
)) {
1413 dev_err(&pdev
->dev
, "platform data not available\n");
1414 err
= PTR_ERR(pdata
);
1418 if (!pdata
->dma_slave
) {
1422 err
= atmel_tdes_dma_init(tdes_dd
, pdata
);
1426 dev_info(dev
, "using %s, %s for DMA transfers\n",
1427 dma_chan_name(tdes_dd
->dma_lch_in
.chan
),
1428 dma_chan_name(tdes_dd
->dma_lch_out
.chan
));
1431 spin_lock(&atmel_tdes
.lock
);
1432 list_add_tail(&tdes_dd
->list
, &atmel_tdes
.dev_list
);
1433 spin_unlock(&atmel_tdes
.lock
);
1435 err
= atmel_tdes_register_algs(tdes_dd
);
1439 dev_info(dev
, "Atmel DES/TDES\n");
1444 spin_lock(&atmel_tdes
.lock
);
1445 list_del(&tdes_dd
->list
);
1446 spin_unlock(&atmel_tdes
.lock
);
1447 if (tdes_dd
->caps
.has_dma
)
1448 atmel_tdes_dma_cleanup(tdes_dd
);
1451 atmel_tdes_buff_cleanup(tdes_dd
);
1454 tasklet_kill(&tdes_dd
->done_task
);
1455 tasklet_kill(&tdes_dd
->queue_task
);
1457 dev_err(dev
, "initialization failed.\n");
1462 static int atmel_tdes_remove(struct platform_device
*pdev
)
1464 struct atmel_tdes_dev
*tdes_dd
;
1466 tdes_dd
= platform_get_drvdata(pdev
);
1469 spin_lock(&atmel_tdes
.lock
);
1470 list_del(&tdes_dd
->list
);
1471 spin_unlock(&atmel_tdes
.lock
);
1473 atmel_tdes_unregister_algs(tdes_dd
);
1475 tasklet_kill(&tdes_dd
->done_task
);
1476 tasklet_kill(&tdes_dd
->queue_task
);
1478 if (tdes_dd
->caps
.has_dma
)
1479 atmel_tdes_dma_cleanup(tdes_dd
);
1481 atmel_tdes_buff_cleanup(tdes_dd
);
1486 static struct platform_driver atmel_tdes_driver
= {
1487 .probe
= atmel_tdes_probe
,
1488 .remove
= atmel_tdes_remove
,
1490 .name
= "atmel_tdes",
1491 .of_match_table
= of_match_ptr(atmel_tdes_dt_ids
),
1495 module_platform_driver(atmel_tdes_driver
);
1497 MODULE_DESCRIPTION("Atmel DES/TDES hw acceleration support.");
1498 MODULE_LICENSE("GPL v2");
1499 MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");