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 unsigned int 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
)
221 clk_prepare_enable(dd
->iclk
);
223 if (!(dd
->flags
& TDES_FLAGS_INIT
)) {
224 atmel_tdes_write(dd
, TDES_CR
, TDES_CR_SWRST
);
225 dd
->flags
|= TDES_FLAGS_INIT
;
232 static inline unsigned int atmel_tdes_get_version(struct atmel_tdes_dev
*dd
)
234 return atmel_tdes_read(dd
, TDES_HW_VERSION
) & 0x00000fff;
237 static void atmel_tdes_hw_version_init(struct atmel_tdes_dev
*dd
)
239 atmel_tdes_hw_init(dd
);
241 dd
->hw_version
= atmel_tdes_get_version(dd
);
244 "version: 0x%x\n", dd
->hw_version
);
246 clk_disable_unprepare(dd
->iclk
);
249 static void atmel_tdes_dma_callback(void *data
)
251 struct atmel_tdes_dev
*dd
= data
;
253 /* dma_lch_out - completed */
254 tasklet_schedule(&dd
->done_task
);
257 static int atmel_tdes_write_ctrl(struct atmel_tdes_dev
*dd
)
260 u32 valcr
= 0, valmr
= TDES_MR_SMOD_PDC
;
262 err
= atmel_tdes_hw_init(dd
);
267 if (!dd
->caps
.has_dma
)
268 atmel_tdes_write(dd
, TDES_PTCR
,
269 TDES_PTCR_TXTDIS
| TDES_PTCR_RXTDIS
);
271 /* MR register must be set before IV registers */
272 if (dd
->ctx
->keylen
> (DES_KEY_SIZE
<< 1)) {
273 valmr
|= TDES_MR_KEYMOD_3KEY
;
274 valmr
|= TDES_MR_TDESMOD_TDES
;
275 } else if (dd
->ctx
->keylen
> DES_KEY_SIZE
) {
276 valmr
|= TDES_MR_KEYMOD_2KEY
;
277 valmr
|= TDES_MR_TDESMOD_TDES
;
279 valmr
|= TDES_MR_TDESMOD_DES
;
282 if (dd
->flags
& TDES_FLAGS_CBC
) {
283 valmr
|= TDES_MR_OPMOD_CBC
;
284 } else if (dd
->flags
& TDES_FLAGS_CFB
) {
285 valmr
|= TDES_MR_OPMOD_CFB
;
287 if (dd
->flags
& TDES_FLAGS_CFB8
)
288 valmr
|= TDES_MR_CFBS_8b
;
289 else if (dd
->flags
& TDES_FLAGS_CFB16
)
290 valmr
|= TDES_MR_CFBS_16b
;
291 else if (dd
->flags
& TDES_FLAGS_CFB32
)
292 valmr
|= TDES_MR_CFBS_32b
;
293 else if (dd
->flags
& TDES_FLAGS_CFB64
)
294 valmr
|= TDES_MR_CFBS_64b
;
295 } else if (dd
->flags
& TDES_FLAGS_OFB
) {
296 valmr
|= TDES_MR_OPMOD_OFB
;
299 if ((dd
->flags
& TDES_FLAGS_ENCRYPT
) || (dd
->flags
& TDES_FLAGS_OFB
))
300 valmr
|= TDES_MR_CYPHER_ENC
;
302 atmel_tdes_write(dd
, TDES_CR
, valcr
);
303 atmel_tdes_write(dd
, TDES_MR
, valmr
);
305 atmel_tdes_write_n(dd
, TDES_KEY1W1R
, dd
->ctx
->key
,
306 dd
->ctx
->keylen
>> 2);
308 if (((dd
->flags
& TDES_FLAGS_CBC
) || (dd
->flags
& TDES_FLAGS_CFB
) ||
309 (dd
->flags
& TDES_FLAGS_OFB
)) && dd
->req
->info
) {
310 atmel_tdes_write_n(dd
, TDES_IV1R
, dd
->req
->info
, 2);
316 static int atmel_tdes_crypt_pdc_stop(struct atmel_tdes_dev
*dd
)
321 atmel_tdes_write(dd
, TDES_PTCR
, TDES_PTCR_TXTDIS
|TDES_PTCR_RXTDIS
);
323 if (dd
->flags
& TDES_FLAGS_FAST
) {
324 dma_unmap_sg(dd
->dev
, dd
->out_sg
, 1, DMA_FROM_DEVICE
);
325 dma_unmap_sg(dd
->dev
, dd
->in_sg
, 1, DMA_TO_DEVICE
);
327 dma_sync_single_for_device(dd
->dev
, dd
->dma_addr_out
,
328 dd
->dma_size
, DMA_FROM_DEVICE
);
331 count
= atmel_tdes_sg_copy(&dd
->out_sg
, &dd
->out_offset
,
332 dd
->buf_out
, dd
->buflen
, dd
->dma_size
, 1);
333 if (count
!= dd
->dma_size
) {
335 pr_err("not all data converted: %u\n", count
);
342 static int atmel_tdes_buff_init(struct atmel_tdes_dev
*dd
)
346 dd
->buf_in
= (void *)__get_free_pages(GFP_KERNEL
, 0);
347 dd
->buf_out
= (void *)__get_free_pages(GFP_KERNEL
, 0);
348 dd
->buflen
= PAGE_SIZE
;
349 dd
->buflen
&= ~(DES_BLOCK_SIZE
- 1);
351 if (!dd
->buf_in
|| !dd
->buf_out
) {
352 dev_err(dd
->dev
, "unable to alloc pages.\n");
357 dd
->dma_addr_in
= dma_map_single(dd
->dev
, dd
->buf_in
,
358 dd
->buflen
, DMA_TO_DEVICE
);
359 if (dma_mapping_error(dd
->dev
, dd
->dma_addr_in
)) {
360 dev_err(dd
->dev
, "dma %d bytes error\n", dd
->buflen
);
365 dd
->dma_addr_out
= dma_map_single(dd
->dev
, dd
->buf_out
,
366 dd
->buflen
, DMA_FROM_DEVICE
);
367 if (dma_mapping_error(dd
->dev
, dd
->dma_addr_out
)) {
368 dev_err(dd
->dev
, "dma %d bytes error\n", dd
->buflen
);
376 dma_unmap_single(dd
->dev
, dd
->dma_addr_in
, dd
->buflen
,
379 free_page((unsigned long)dd
->buf_out
);
380 free_page((unsigned long)dd
->buf_in
);
383 pr_err("error: %d\n", err
);
387 static void atmel_tdes_buff_cleanup(struct atmel_tdes_dev
*dd
)
389 dma_unmap_single(dd
->dev
, dd
->dma_addr_out
, dd
->buflen
,
391 dma_unmap_single(dd
->dev
, dd
->dma_addr_in
, dd
->buflen
,
393 free_page((unsigned long)dd
->buf_out
);
394 free_page((unsigned long)dd
->buf_in
);
397 static int atmel_tdes_crypt_pdc(struct crypto_tfm
*tfm
, dma_addr_t dma_addr_in
,
398 dma_addr_t dma_addr_out
, int length
)
400 struct atmel_tdes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
401 struct atmel_tdes_dev
*dd
= ctx
->dd
;
404 dd
->dma_size
= length
;
406 if (!(dd
->flags
& TDES_FLAGS_FAST
)) {
407 dma_sync_single_for_device(dd
->dev
, dma_addr_in
, length
,
411 if ((dd
->flags
& TDES_FLAGS_CFB
) && (dd
->flags
& TDES_FLAGS_CFB8
))
412 len32
= DIV_ROUND_UP(length
, sizeof(u8
));
413 else if ((dd
->flags
& TDES_FLAGS_CFB
) && (dd
->flags
& TDES_FLAGS_CFB16
))
414 len32
= DIV_ROUND_UP(length
, sizeof(u16
));
416 len32
= DIV_ROUND_UP(length
, sizeof(u32
));
418 atmel_tdes_write(dd
, TDES_PTCR
, TDES_PTCR_TXTDIS
|TDES_PTCR_RXTDIS
);
419 atmel_tdes_write(dd
, TDES_TPR
, dma_addr_in
);
420 atmel_tdes_write(dd
, TDES_TCR
, len32
);
421 atmel_tdes_write(dd
, TDES_RPR
, dma_addr_out
);
422 atmel_tdes_write(dd
, TDES_RCR
, len32
);
424 /* Enable Interrupt */
425 atmel_tdes_write(dd
, TDES_IER
, TDES_INT_ENDRX
);
427 /* Start DMA transfer */
428 atmel_tdes_write(dd
, TDES_PTCR
, TDES_PTCR_TXTEN
| TDES_PTCR_RXTEN
);
433 static int atmel_tdes_crypt_dma(struct crypto_tfm
*tfm
, dma_addr_t dma_addr_in
,
434 dma_addr_t dma_addr_out
, int length
)
436 struct atmel_tdes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
437 struct atmel_tdes_dev
*dd
= ctx
->dd
;
438 struct scatterlist sg
[2];
439 struct dma_async_tx_descriptor
*in_desc
, *out_desc
;
441 dd
->dma_size
= length
;
443 if (!(dd
->flags
& TDES_FLAGS_FAST
)) {
444 dma_sync_single_for_device(dd
->dev
, dma_addr_in
, length
,
448 if (dd
->flags
& TDES_FLAGS_CFB8
) {
449 dd
->dma_lch_in
.dma_conf
.dst_addr_width
=
450 DMA_SLAVE_BUSWIDTH_1_BYTE
;
451 dd
->dma_lch_out
.dma_conf
.src_addr_width
=
452 DMA_SLAVE_BUSWIDTH_1_BYTE
;
453 } else if (dd
->flags
& TDES_FLAGS_CFB16
) {
454 dd
->dma_lch_in
.dma_conf
.dst_addr_width
=
455 DMA_SLAVE_BUSWIDTH_2_BYTES
;
456 dd
->dma_lch_out
.dma_conf
.src_addr_width
=
457 DMA_SLAVE_BUSWIDTH_2_BYTES
;
459 dd
->dma_lch_in
.dma_conf
.dst_addr_width
=
460 DMA_SLAVE_BUSWIDTH_4_BYTES
;
461 dd
->dma_lch_out
.dma_conf
.src_addr_width
=
462 DMA_SLAVE_BUSWIDTH_4_BYTES
;
465 dmaengine_slave_config(dd
->dma_lch_in
.chan
, &dd
->dma_lch_in
.dma_conf
);
466 dmaengine_slave_config(dd
->dma_lch_out
.chan
, &dd
->dma_lch_out
.dma_conf
);
468 dd
->flags
|= TDES_FLAGS_DMA
;
470 sg_init_table(&sg
[0], 1);
471 sg_dma_address(&sg
[0]) = dma_addr_in
;
472 sg_dma_len(&sg
[0]) = length
;
474 sg_init_table(&sg
[1], 1);
475 sg_dma_address(&sg
[1]) = dma_addr_out
;
476 sg_dma_len(&sg
[1]) = length
;
478 in_desc
= dmaengine_prep_slave_sg(dd
->dma_lch_in
.chan
, &sg
[0],
480 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
484 out_desc
= dmaengine_prep_slave_sg(dd
->dma_lch_out
.chan
, &sg
[1],
486 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
490 out_desc
->callback
= atmel_tdes_dma_callback
;
491 out_desc
->callback_param
= dd
;
493 dmaengine_submit(out_desc
);
494 dma_async_issue_pending(dd
->dma_lch_out
.chan
);
496 dmaengine_submit(in_desc
);
497 dma_async_issue_pending(dd
->dma_lch_in
.chan
);
502 static int atmel_tdes_crypt_start(struct atmel_tdes_dev
*dd
)
504 struct crypto_tfm
*tfm
= crypto_ablkcipher_tfm(
505 crypto_ablkcipher_reqtfm(dd
->req
));
506 int err
, fast
= 0, in
, out
;
508 dma_addr_t addr_in
, addr_out
;
510 if ((!dd
->in_offset
) && (!dd
->out_offset
)) {
511 /* check for alignment */
512 in
= IS_ALIGNED((u32
)dd
->in_sg
->offset
, sizeof(u32
)) &&
513 IS_ALIGNED(dd
->in_sg
->length
, dd
->ctx
->block_size
);
514 out
= IS_ALIGNED((u32
)dd
->out_sg
->offset
, sizeof(u32
)) &&
515 IS_ALIGNED(dd
->out_sg
->length
, dd
->ctx
->block_size
);
518 if (sg_dma_len(dd
->in_sg
) != sg_dma_len(dd
->out_sg
))
524 count
= min(dd
->total
, sg_dma_len(dd
->in_sg
));
525 count
= min(count
, sg_dma_len(dd
->out_sg
));
527 err
= dma_map_sg(dd
->dev
, dd
->in_sg
, 1, DMA_TO_DEVICE
);
529 dev_err(dd
->dev
, "dma_map_sg() error\n");
533 err
= dma_map_sg(dd
->dev
, dd
->out_sg
, 1,
536 dev_err(dd
->dev
, "dma_map_sg() error\n");
537 dma_unmap_sg(dd
->dev
, dd
->in_sg
, 1,
542 addr_in
= sg_dma_address(dd
->in_sg
);
543 addr_out
= sg_dma_address(dd
->out_sg
);
545 dd
->flags
|= TDES_FLAGS_FAST
;
548 /* use cache buffers */
549 count
= atmel_tdes_sg_copy(&dd
->in_sg
, &dd
->in_offset
,
550 dd
->buf_in
, dd
->buflen
, dd
->total
, 0);
552 addr_in
= dd
->dma_addr_in
;
553 addr_out
= dd
->dma_addr_out
;
555 dd
->flags
&= ~TDES_FLAGS_FAST
;
560 if (dd
->caps
.has_dma
)
561 err
= atmel_tdes_crypt_dma(tfm
, addr_in
, addr_out
, count
);
563 err
= atmel_tdes_crypt_pdc(tfm
, addr_in
, addr_out
, count
);
565 if (err
&& (dd
->flags
& TDES_FLAGS_FAST
)) {
566 dma_unmap_sg(dd
->dev
, dd
->in_sg
, 1, DMA_TO_DEVICE
);
567 dma_unmap_sg(dd
->dev
, dd
->out_sg
, 1, DMA_TO_DEVICE
);
573 static void atmel_tdes_finish_req(struct atmel_tdes_dev
*dd
, int err
)
575 struct ablkcipher_request
*req
= dd
->req
;
577 clk_disable_unprepare(dd
->iclk
);
579 dd
->flags
&= ~TDES_FLAGS_BUSY
;
581 req
->base
.complete(&req
->base
, err
);
584 static int atmel_tdes_handle_queue(struct atmel_tdes_dev
*dd
,
585 struct ablkcipher_request
*req
)
587 struct crypto_async_request
*async_req
, *backlog
;
588 struct atmel_tdes_ctx
*ctx
;
589 struct atmel_tdes_reqctx
*rctx
;
593 spin_lock_irqsave(&dd
->lock
, flags
);
595 ret
= ablkcipher_enqueue_request(&dd
->queue
, req
);
596 if (dd
->flags
& TDES_FLAGS_BUSY
) {
597 spin_unlock_irqrestore(&dd
->lock
, flags
);
600 backlog
= crypto_get_backlog(&dd
->queue
);
601 async_req
= crypto_dequeue_request(&dd
->queue
);
603 dd
->flags
|= TDES_FLAGS_BUSY
;
604 spin_unlock_irqrestore(&dd
->lock
, flags
);
610 backlog
->complete(backlog
, -EINPROGRESS
);
612 req
= ablkcipher_request_cast(async_req
);
614 /* assign new request to device */
616 dd
->total
= req
->nbytes
;
618 dd
->in_sg
= req
->src
;
620 dd
->out_sg
= req
->dst
;
622 rctx
= ablkcipher_request_ctx(req
);
623 ctx
= crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req
));
624 rctx
->mode
&= TDES_FLAGS_MODE_MASK
;
625 dd
->flags
= (dd
->flags
& ~TDES_FLAGS_MODE_MASK
) | rctx
->mode
;
629 err
= atmel_tdes_write_ctrl(dd
);
631 err
= atmel_tdes_crypt_start(dd
);
633 /* des_task will not finish it, so do it here */
634 atmel_tdes_finish_req(dd
, err
);
635 tasklet_schedule(&dd
->queue_task
);
641 static int atmel_tdes_crypt_dma_stop(struct atmel_tdes_dev
*dd
)
646 if (dd
->flags
& TDES_FLAGS_DMA
) {
648 if (dd
->flags
& TDES_FLAGS_FAST
) {
649 dma_unmap_sg(dd
->dev
, dd
->out_sg
, 1, DMA_FROM_DEVICE
);
650 dma_unmap_sg(dd
->dev
, dd
->in_sg
, 1, DMA_TO_DEVICE
);
652 dma_sync_single_for_device(dd
->dev
, dd
->dma_addr_out
,
653 dd
->dma_size
, DMA_FROM_DEVICE
);
656 count
= atmel_tdes_sg_copy(&dd
->out_sg
, &dd
->out_offset
,
657 dd
->buf_out
, dd
->buflen
, dd
->dma_size
, 1);
658 if (count
!= dd
->dma_size
) {
660 pr_err("not all data converted: %u\n", count
);
667 static int atmel_tdes_crypt(struct ablkcipher_request
*req
, unsigned long mode
)
669 struct atmel_tdes_ctx
*ctx
= crypto_ablkcipher_ctx(
670 crypto_ablkcipher_reqtfm(req
));
671 struct atmel_tdes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
673 if (mode
& TDES_FLAGS_CFB8
) {
674 if (!IS_ALIGNED(req
->nbytes
, CFB8_BLOCK_SIZE
)) {
675 pr_err("request size is not exact amount of CFB8 blocks\n");
678 ctx
->block_size
= CFB8_BLOCK_SIZE
;
679 } else if (mode
& TDES_FLAGS_CFB16
) {
680 if (!IS_ALIGNED(req
->nbytes
, CFB16_BLOCK_SIZE
)) {
681 pr_err("request size is not exact amount of CFB16 blocks\n");
684 ctx
->block_size
= CFB16_BLOCK_SIZE
;
685 } else if (mode
& TDES_FLAGS_CFB32
) {
686 if (!IS_ALIGNED(req
->nbytes
, CFB32_BLOCK_SIZE
)) {
687 pr_err("request size is not exact amount of CFB32 blocks\n");
690 ctx
->block_size
= CFB32_BLOCK_SIZE
;
692 if (!IS_ALIGNED(req
->nbytes
, DES_BLOCK_SIZE
)) {
693 pr_err("request size is not exact amount of DES blocks\n");
696 ctx
->block_size
= DES_BLOCK_SIZE
;
701 return atmel_tdes_handle_queue(ctx
->dd
, req
);
704 static bool atmel_tdes_filter(struct dma_chan
*chan
, void *slave
)
706 struct at_dma_slave
*sl
= slave
;
708 if (sl
&& sl
->dma_dev
== chan
->device
->dev
) {
716 static int atmel_tdes_dma_init(struct atmel_tdes_dev
*dd
,
717 struct crypto_platform_data
*pdata
)
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
)
731 dd
->dma_lch_in
.dma_conf
.direction
= DMA_MEM_TO_DEV
;
732 dd
->dma_lch_in
.dma_conf
.dst_addr
= dd
->phys_base
+
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
)
747 dd
->dma_lch_out
.dma_conf
.direction
= DMA_DEV_TO_MEM
;
748 dd
->dma_lch_out
.dma_conf
.src_addr
= dd
->phys_base
+
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;
761 dma_release_channel(dd
->dma_lch_in
.chan
);
763 dev_warn(dd
->dev
, "no DMA channel available\n");
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
,
776 u32 tmp
[DES_EXPKEY_WORDS
];
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
);
787 err
= des_ekey(tmp
, key
);
788 if (err
== 0 && (ctfm
->crt_flags
& CRYPTO_TFM_REQ_WEAK_KEY
)) {
789 ctfm
->crt_flags
|= CRYPTO_TFM_RES_WEAK_KEY
;
793 memcpy(ctx
->key
, key
, keylen
);
794 ctx
->keylen
= keylen
;
799 static int atmel_tdes_setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
802 struct atmel_tdes_ctx
*ctx
= crypto_ablkcipher_ctx(tfm
);
803 const char *alg_name
;
805 alg_name
= crypto_tfm_alg_name(crypto_ablkcipher_tfm(tfm
));
808 * HW bug in cfb 3-keys mode.
810 if (!ctx
->dd
->caps
.has_cfb_3keys
&& strstr(alg_name
, "cfb")
811 && (keylen
!= 2*DES_KEY_SIZE
)) {
812 crypto_ablkcipher_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
814 } else if ((keylen
!= 2*DES_KEY_SIZE
) && (keylen
!= 3*DES_KEY_SIZE
)) {
815 crypto_ablkcipher_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
819 memcpy(ctx
->key
, key
, keylen
);
820 ctx
->keylen
= keylen
;
825 static int atmel_tdes_ecb_encrypt(struct ablkcipher_request
*req
)
827 return atmel_tdes_crypt(req
, TDES_FLAGS_ENCRYPT
);
830 static int atmel_tdes_ecb_decrypt(struct ablkcipher_request
*req
)
832 return atmel_tdes_crypt(req
, 0);
835 static int atmel_tdes_cbc_encrypt(struct ablkcipher_request
*req
)
837 return atmel_tdes_crypt(req
, TDES_FLAGS_ENCRYPT
| TDES_FLAGS_CBC
);
840 static int atmel_tdes_cbc_decrypt(struct ablkcipher_request
*req
)
842 return atmel_tdes_crypt(req
, TDES_FLAGS_CBC
);
844 static int atmel_tdes_cfb_encrypt(struct ablkcipher_request
*req
)
846 return atmel_tdes_crypt(req
, TDES_FLAGS_ENCRYPT
| TDES_FLAGS_CFB
);
849 static int atmel_tdes_cfb_decrypt(struct ablkcipher_request
*req
)
851 return atmel_tdes_crypt(req
, TDES_FLAGS_CFB
);
854 static int atmel_tdes_cfb8_encrypt(struct ablkcipher_request
*req
)
856 return atmel_tdes_crypt(req
, TDES_FLAGS_ENCRYPT
| TDES_FLAGS_CFB
|
860 static int atmel_tdes_cfb8_decrypt(struct ablkcipher_request
*req
)
862 return atmel_tdes_crypt(req
, TDES_FLAGS_CFB
| TDES_FLAGS_CFB8
);
865 static int atmel_tdes_cfb16_encrypt(struct ablkcipher_request
*req
)
867 return atmel_tdes_crypt(req
, TDES_FLAGS_ENCRYPT
| TDES_FLAGS_CFB
|
871 static int atmel_tdes_cfb16_decrypt(struct ablkcipher_request
*req
)
873 return atmel_tdes_crypt(req
, TDES_FLAGS_CFB
| TDES_FLAGS_CFB16
);
876 static int atmel_tdes_cfb32_encrypt(struct ablkcipher_request
*req
)
878 return atmel_tdes_crypt(req
, TDES_FLAGS_ENCRYPT
| TDES_FLAGS_CFB
|
882 static int atmel_tdes_cfb32_decrypt(struct ablkcipher_request
*req
)
884 return atmel_tdes_crypt(req
, TDES_FLAGS_CFB
| TDES_FLAGS_CFB32
);
887 static int atmel_tdes_ofb_encrypt(struct ablkcipher_request
*req
)
889 return atmel_tdes_crypt(req
, TDES_FLAGS_ENCRYPT
| TDES_FLAGS_OFB
);
892 static int atmel_tdes_ofb_decrypt(struct ablkcipher_request
*req
)
894 return atmel_tdes_crypt(req
, TDES_FLAGS_OFB
);
897 static int atmel_tdes_cra_init(struct crypto_tfm
*tfm
)
899 struct atmel_tdes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
900 struct atmel_tdes_dev
*dd
;
902 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct atmel_tdes_reqctx
);
904 dd
= atmel_tdes_find_dev(ctx
);
911 static void atmel_tdes_cra_exit(struct crypto_tfm
*tfm
)
915 static struct crypto_alg tdes_algs
[] = {
917 .cra_name
= "ecb(des)",
918 .cra_driver_name
= "atmel-ecb-des",
920 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
921 .cra_blocksize
= DES_BLOCK_SIZE
,
922 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
923 .cra_alignmask
= 0x7,
924 .cra_type
= &crypto_ablkcipher_type
,
925 .cra_module
= THIS_MODULE
,
926 .cra_init
= atmel_tdes_cra_init
,
927 .cra_exit
= atmel_tdes_cra_exit
,
928 .cra_u
.ablkcipher
= {
929 .min_keysize
= DES_KEY_SIZE
,
930 .max_keysize
= DES_KEY_SIZE
,
931 .setkey
= atmel_des_setkey
,
932 .encrypt
= atmel_tdes_ecb_encrypt
,
933 .decrypt
= atmel_tdes_ecb_decrypt
,
937 .cra_name
= "cbc(des)",
938 .cra_driver_name
= "atmel-cbc-des",
940 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
941 .cra_blocksize
= DES_BLOCK_SIZE
,
942 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
943 .cra_alignmask
= 0x7,
944 .cra_type
= &crypto_ablkcipher_type
,
945 .cra_module
= THIS_MODULE
,
946 .cra_init
= atmel_tdes_cra_init
,
947 .cra_exit
= atmel_tdes_cra_exit
,
948 .cra_u
.ablkcipher
= {
949 .min_keysize
= DES_KEY_SIZE
,
950 .max_keysize
= DES_KEY_SIZE
,
951 .ivsize
= DES_BLOCK_SIZE
,
952 .setkey
= atmel_des_setkey
,
953 .encrypt
= atmel_tdes_cbc_encrypt
,
954 .decrypt
= atmel_tdes_cbc_decrypt
,
958 .cra_name
= "cfb(des)",
959 .cra_driver_name
= "atmel-cfb-des",
961 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
962 .cra_blocksize
= DES_BLOCK_SIZE
,
963 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
964 .cra_alignmask
= 0x7,
965 .cra_type
= &crypto_ablkcipher_type
,
966 .cra_module
= THIS_MODULE
,
967 .cra_init
= atmel_tdes_cra_init
,
968 .cra_exit
= atmel_tdes_cra_exit
,
969 .cra_u
.ablkcipher
= {
970 .min_keysize
= DES_KEY_SIZE
,
971 .max_keysize
= DES_KEY_SIZE
,
972 .ivsize
= DES_BLOCK_SIZE
,
973 .setkey
= atmel_des_setkey
,
974 .encrypt
= atmel_tdes_cfb_encrypt
,
975 .decrypt
= atmel_tdes_cfb_decrypt
,
979 .cra_name
= "cfb8(des)",
980 .cra_driver_name
= "atmel-cfb8-des",
982 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
983 .cra_blocksize
= CFB8_BLOCK_SIZE
,
984 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
986 .cra_type
= &crypto_ablkcipher_type
,
987 .cra_module
= THIS_MODULE
,
988 .cra_init
= atmel_tdes_cra_init
,
989 .cra_exit
= atmel_tdes_cra_exit
,
990 .cra_u
.ablkcipher
= {
991 .min_keysize
= DES_KEY_SIZE
,
992 .max_keysize
= DES_KEY_SIZE
,
993 .ivsize
= DES_BLOCK_SIZE
,
994 .setkey
= atmel_des_setkey
,
995 .encrypt
= atmel_tdes_cfb8_encrypt
,
996 .decrypt
= atmel_tdes_cfb8_decrypt
,
1000 .cra_name
= "cfb16(des)",
1001 .cra_driver_name
= "atmel-cfb16-des",
1002 .cra_priority
= 100,
1003 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1004 .cra_blocksize
= CFB16_BLOCK_SIZE
,
1005 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1006 .cra_alignmask
= 0x1,
1007 .cra_type
= &crypto_ablkcipher_type
,
1008 .cra_module
= THIS_MODULE
,
1009 .cra_init
= atmel_tdes_cra_init
,
1010 .cra_exit
= atmel_tdes_cra_exit
,
1011 .cra_u
.ablkcipher
= {
1012 .min_keysize
= DES_KEY_SIZE
,
1013 .max_keysize
= DES_KEY_SIZE
,
1014 .ivsize
= DES_BLOCK_SIZE
,
1015 .setkey
= atmel_des_setkey
,
1016 .encrypt
= atmel_tdes_cfb16_encrypt
,
1017 .decrypt
= atmel_tdes_cfb16_decrypt
,
1021 .cra_name
= "cfb32(des)",
1022 .cra_driver_name
= "atmel-cfb32-des",
1023 .cra_priority
= 100,
1024 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1025 .cra_blocksize
= CFB32_BLOCK_SIZE
,
1026 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1027 .cra_alignmask
= 0x3,
1028 .cra_type
= &crypto_ablkcipher_type
,
1029 .cra_module
= THIS_MODULE
,
1030 .cra_init
= atmel_tdes_cra_init
,
1031 .cra_exit
= atmel_tdes_cra_exit
,
1032 .cra_u
.ablkcipher
= {
1033 .min_keysize
= DES_KEY_SIZE
,
1034 .max_keysize
= DES_KEY_SIZE
,
1035 .ivsize
= DES_BLOCK_SIZE
,
1036 .setkey
= atmel_des_setkey
,
1037 .encrypt
= atmel_tdes_cfb32_encrypt
,
1038 .decrypt
= atmel_tdes_cfb32_decrypt
,
1042 .cra_name
= "ofb(des)",
1043 .cra_driver_name
= "atmel-ofb-des",
1044 .cra_priority
= 100,
1045 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1046 .cra_blocksize
= DES_BLOCK_SIZE
,
1047 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1048 .cra_alignmask
= 0x7,
1049 .cra_type
= &crypto_ablkcipher_type
,
1050 .cra_module
= THIS_MODULE
,
1051 .cra_init
= atmel_tdes_cra_init
,
1052 .cra_exit
= atmel_tdes_cra_exit
,
1053 .cra_u
.ablkcipher
= {
1054 .min_keysize
= DES_KEY_SIZE
,
1055 .max_keysize
= DES_KEY_SIZE
,
1056 .ivsize
= DES_BLOCK_SIZE
,
1057 .setkey
= atmel_des_setkey
,
1058 .encrypt
= atmel_tdes_ofb_encrypt
,
1059 .decrypt
= atmel_tdes_ofb_decrypt
,
1063 .cra_name
= "ecb(des3_ede)",
1064 .cra_driver_name
= "atmel-ecb-tdes",
1065 .cra_priority
= 100,
1066 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1067 .cra_blocksize
= DES_BLOCK_SIZE
,
1068 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1069 .cra_alignmask
= 0x7,
1070 .cra_type
= &crypto_ablkcipher_type
,
1071 .cra_module
= THIS_MODULE
,
1072 .cra_init
= atmel_tdes_cra_init
,
1073 .cra_exit
= atmel_tdes_cra_exit
,
1074 .cra_u
.ablkcipher
= {
1075 .min_keysize
= 2 * DES_KEY_SIZE
,
1076 .max_keysize
= 3 * DES_KEY_SIZE
,
1077 .setkey
= atmel_tdes_setkey
,
1078 .encrypt
= atmel_tdes_ecb_encrypt
,
1079 .decrypt
= atmel_tdes_ecb_decrypt
,
1083 .cra_name
= "cbc(des3_ede)",
1084 .cra_driver_name
= "atmel-cbc-tdes",
1085 .cra_priority
= 100,
1086 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1087 .cra_blocksize
= DES_BLOCK_SIZE
,
1088 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1089 .cra_alignmask
= 0x7,
1090 .cra_type
= &crypto_ablkcipher_type
,
1091 .cra_module
= THIS_MODULE
,
1092 .cra_init
= atmel_tdes_cra_init
,
1093 .cra_exit
= atmel_tdes_cra_exit
,
1094 .cra_u
.ablkcipher
= {
1095 .min_keysize
= 2*DES_KEY_SIZE
,
1096 .max_keysize
= 3*DES_KEY_SIZE
,
1097 .ivsize
= DES_BLOCK_SIZE
,
1098 .setkey
= atmel_tdes_setkey
,
1099 .encrypt
= atmel_tdes_cbc_encrypt
,
1100 .decrypt
= atmel_tdes_cbc_decrypt
,
1104 .cra_name
= "cfb(des3_ede)",
1105 .cra_driver_name
= "atmel-cfb-tdes",
1106 .cra_priority
= 100,
1107 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1108 .cra_blocksize
= DES_BLOCK_SIZE
,
1109 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1110 .cra_alignmask
= 0x7,
1111 .cra_type
= &crypto_ablkcipher_type
,
1112 .cra_module
= THIS_MODULE
,
1113 .cra_init
= atmel_tdes_cra_init
,
1114 .cra_exit
= atmel_tdes_cra_exit
,
1115 .cra_u
.ablkcipher
= {
1116 .min_keysize
= 2*DES_KEY_SIZE
,
1117 .max_keysize
= 2*DES_KEY_SIZE
,
1118 .ivsize
= DES_BLOCK_SIZE
,
1119 .setkey
= atmel_tdes_setkey
,
1120 .encrypt
= atmel_tdes_cfb_encrypt
,
1121 .decrypt
= atmel_tdes_cfb_decrypt
,
1125 .cra_name
= "cfb8(des3_ede)",
1126 .cra_driver_name
= "atmel-cfb8-tdes",
1127 .cra_priority
= 100,
1128 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1129 .cra_blocksize
= CFB8_BLOCK_SIZE
,
1130 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1132 .cra_type
= &crypto_ablkcipher_type
,
1133 .cra_module
= THIS_MODULE
,
1134 .cra_init
= atmel_tdes_cra_init
,
1135 .cra_exit
= atmel_tdes_cra_exit
,
1136 .cra_u
.ablkcipher
= {
1137 .min_keysize
= 2*DES_KEY_SIZE
,
1138 .max_keysize
= 2*DES_KEY_SIZE
,
1139 .ivsize
= DES_BLOCK_SIZE
,
1140 .setkey
= atmel_tdes_setkey
,
1141 .encrypt
= atmel_tdes_cfb8_encrypt
,
1142 .decrypt
= atmel_tdes_cfb8_decrypt
,
1146 .cra_name
= "cfb16(des3_ede)",
1147 .cra_driver_name
= "atmel-cfb16-tdes",
1148 .cra_priority
= 100,
1149 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1150 .cra_blocksize
= CFB16_BLOCK_SIZE
,
1151 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1152 .cra_alignmask
= 0x1,
1153 .cra_type
= &crypto_ablkcipher_type
,
1154 .cra_module
= THIS_MODULE
,
1155 .cra_init
= atmel_tdes_cra_init
,
1156 .cra_exit
= atmel_tdes_cra_exit
,
1157 .cra_u
.ablkcipher
= {
1158 .min_keysize
= 2*DES_KEY_SIZE
,
1159 .max_keysize
= 2*DES_KEY_SIZE
,
1160 .ivsize
= DES_BLOCK_SIZE
,
1161 .setkey
= atmel_tdes_setkey
,
1162 .encrypt
= atmel_tdes_cfb16_encrypt
,
1163 .decrypt
= atmel_tdes_cfb16_decrypt
,
1167 .cra_name
= "cfb32(des3_ede)",
1168 .cra_driver_name
= "atmel-cfb32-tdes",
1169 .cra_priority
= 100,
1170 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1171 .cra_blocksize
= CFB32_BLOCK_SIZE
,
1172 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1173 .cra_alignmask
= 0x3,
1174 .cra_type
= &crypto_ablkcipher_type
,
1175 .cra_module
= THIS_MODULE
,
1176 .cra_init
= atmel_tdes_cra_init
,
1177 .cra_exit
= atmel_tdes_cra_exit
,
1178 .cra_u
.ablkcipher
= {
1179 .min_keysize
= 2*DES_KEY_SIZE
,
1180 .max_keysize
= 2*DES_KEY_SIZE
,
1181 .ivsize
= DES_BLOCK_SIZE
,
1182 .setkey
= atmel_tdes_setkey
,
1183 .encrypt
= atmel_tdes_cfb32_encrypt
,
1184 .decrypt
= atmel_tdes_cfb32_decrypt
,
1188 .cra_name
= "ofb(des3_ede)",
1189 .cra_driver_name
= "atmel-ofb-tdes",
1190 .cra_priority
= 100,
1191 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1192 .cra_blocksize
= DES_BLOCK_SIZE
,
1193 .cra_ctxsize
= sizeof(struct atmel_tdes_ctx
),
1194 .cra_alignmask
= 0x7,
1195 .cra_type
= &crypto_ablkcipher_type
,
1196 .cra_module
= THIS_MODULE
,
1197 .cra_init
= atmel_tdes_cra_init
,
1198 .cra_exit
= atmel_tdes_cra_exit
,
1199 .cra_u
.ablkcipher
= {
1200 .min_keysize
= 2*DES_KEY_SIZE
,
1201 .max_keysize
= 3*DES_KEY_SIZE
,
1202 .ivsize
= DES_BLOCK_SIZE
,
1203 .setkey
= atmel_tdes_setkey
,
1204 .encrypt
= atmel_tdes_ofb_encrypt
,
1205 .decrypt
= atmel_tdes_ofb_decrypt
,
1210 static void atmel_tdes_queue_task(unsigned long data
)
1212 struct atmel_tdes_dev
*dd
= (struct atmel_tdes_dev
*)data
;
1214 atmel_tdes_handle_queue(dd
, NULL
);
1217 static void atmel_tdes_done_task(unsigned long data
)
1219 struct atmel_tdes_dev
*dd
= (struct atmel_tdes_dev
*) data
;
1222 if (!(dd
->flags
& TDES_FLAGS_DMA
))
1223 err
= atmel_tdes_crypt_pdc_stop(dd
);
1225 err
= atmel_tdes_crypt_dma_stop(dd
);
1227 err
= dd
->err
? : err
;
1229 if (dd
->total
&& !err
) {
1230 if (dd
->flags
& TDES_FLAGS_FAST
) {
1231 dd
->in_sg
= sg_next(dd
->in_sg
);
1232 dd
->out_sg
= sg_next(dd
->out_sg
);
1233 if (!dd
->in_sg
|| !dd
->out_sg
)
1237 err
= atmel_tdes_crypt_start(dd
);
1239 return; /* DMA started. Not fininishing. */
1242 atmel_tdes_finish_req(dd
, err
);
1243 atmel_tdes_handle_queue(dd
, NULL
);
1246 static irqreturn_t
atmel_tdes_irq(int irq
, void *dev_id
)
1248 struct atmel_tdes_dev
*tdes_dd
= dev_id
;
1251 reg
= atmel_tdes_read(tdes_dd
, TDES_ISR
);
1252 if (reg
& atmel_tdes_read(tdes_dd
, TDES_IMR
)) {
1253 atmel_tdes_write(tdes_dd
, TDES_IDR
, reg
);
1254 if (TDES_FLAGS_BUSY
& tdes_dd
->flags
)
1255 tasklet_schedule(&tdes_dd
->done_task
);
1257 dev_warn(tdes_dd
->dev
, "TDES interrupt when no active requests.\n");
1264 static void atmel_tdes_unregister_algs(struct atmel_tdes_dev
*dd
)
1268 for (i
= 0; i
< ARRAY_SIZE(tdes_algs
); i
++)
1269 crypto_unregister_alg(&tdes_algs
[i
]);
1272 static int atmel_tdes_register_algs(struct atmel_tdes_dev
*dd
)
1276 for (i
= 0; i
< ARRAY_SIZE(tdes_algs
); i
++) {
1277 err
= crypto_register_alg(&tdes_algs
[i
]);
1285 for (j
= 0; j
< i
; j
++)
1286 crypto_unregister_alg(&tdes_algs
[j
]);
1291 static void atmel_tdes_get_cap(struct atmel_tdes_dev
*dd
)
1294 dd
->caps
.has_dma
= 0;
1295 dd
->caps
.has_cfb_3keys
= 0;
1297 /* keep only major version number */
1298 switch (dd
->hw_version
& 0xf00) {
1300 dd
->caps
.has_dma
= 1;
1301 dd
->caps
.has_cfb_3keys
= 1;
1307 "Unmanaged tdes version, set minimum capabilities\n");
1312 #if defined(CONFIG_OF)
1313 static const struct of_device_id atmel_tdes_dt_ids
[] = {
1314 { .compatible
= "atmel,at91sam9g46-tdes" },
1317 MODULE_DEVICE_TABLE(of
, atmel_tdes_dt_ids
);
1319 static struct crypto_platform_data
*atmel_tdes_of_init(struct platform_device
*pdev
)
1321 struct device_node
*np
= pdev
->dev
.of_node
;
1322 struct crypto_platform_data
*pdata
;
1325 dev_err(&pdev
->dev
, "device node not found\n");
1326 return ERR_PTR(-EINVAL
);
1329 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
1331 dev_err(&pdev
->dev
, "could not allocate memory for pdata\n");
1332 return ERR_PTR(-ENOMEM
);
1335 pdata
->dma_slave
= devm_kzalloc(&pdev
->dev
,
1336 sizeof(*(pdata
->dma_slave
)),
1338 if (!pdata
->dma_slave
) {
1339 dev_err(&pdev
->dev
, "could not allocate memory for dma_slave\n");
1340 devm_kfree(&pdev
->dev
, pdata
);
1341 return ERR_PTR(-ENOMEM
);
1346 #else /* CONFIG_OF */
1347 static inline struct crypto_platform_data
*atmel_tdes_of_init(struct platform_device
*pdev
)
1349 return ERR_PTR(-EINVAL
);
1353 static int atmel_tdes_probe(struct platform_device
*pdev
)
1355 struct atmel_tdes_dev
*tdes_dd
;
1356 struct crypto_platform_data
*pdata
;
1357 struct device
*dev
= &pdev
->dev
;
1358 struct resource
*tdes_res
;
1359 unsigned long tdes_phys_size
;
1362 tdes_dd
= kzalloc(sizeof(struct atmel_tdes_dev
), GFP_KERNEL
);
1363 if (tdes_dd
== NULL
) {
1364 dev_err(dev
, "unable to alloc data struct.\n");
1371 platform_set_drvdata(pdev
, tdes_dd
);
1373 INIT_LIST_HEAD(&tdes_dd
->list
);
1375 tasklet_init(&tdes_dd
->done_task
, atmel_tdes_done_task
,
1376 (unsigned long)tdes_dd
);
1377 tasklet_init(&tdes_dd
->queue_task
, atmel_tdes_queue_task
,
1378 (unsigned long)tdes_dd
);
1380 crypto_init_queue(&tdes_dd
->queue
, ATMEL_TDES_QUEUE_LENGTH
);
1384 /* Get the base address */
1385 tdes_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1387 dev_err(dev
, "no MEM resource info\n");
1391 tdes_dd
->phys_base
= tdes_res
->start
;
1392 tdes_phys_size
= resource_size(tdes_res
);
1395 tdes_dd
->irq
= platform_get_irq(pdev
, 0);
1396 if (tdes_dd
->irq
< 0) {
1397 dev_err(dev
, "no IRQ resource info\n");
1402 err
= request_irq(tdes_dd
->irq
, atmel_tdes_irq
, IRQF_SHARED
,
1403 "atmel-tdes", tdes_dd
);
1405 dev_err(dev
, "unable to request tdes irq.\n");
1409 /* Initializing the clock */
1410 tdes_dd
->iclk
= clk_get(&pdev
->dev
, "tdes_clk");
1411 if (IS_ERR(tdes_dd
->iclk
)) {
1412 dev_err(dev
, "clock intialization failed.\n");
1413 err
= PTR_ERR(tdes_dd
->iclk
);
1417 tdes_dd
->io_base
= ioremap(tdes_dd
->phys_base
, tdes_phys_size
);
1418 if (!tdes_dd
->io_base
) {
1419 dev_err(dev
, "can't ioremap\n");
1424 atmel_tdes_hw_version_init(tdes_dd
);
1426 atmel_tdes_get_cap(tdes_dd
);
1428 err
= atmel_tdes_buff_init(tdes_dd
);
1432 if (tdes_dd
->caps
.has_dma
) {
1433 pdata
= pdev
->dev
.platform_data
;
1435 pdata
= atmel_tdes_of_init(pdev
);
1436 if (IS_ERR(pdata
)) {
1437 dev_err(&pdev
->dev
, "platform data not available\n");
1438 err
= PTR_ERR(pdata
);
1442 if (!pdata
->dma_slave
) {
1446 err
= atmel_tdes_dma_init(tdes_dd
, pdata
);
1450 dev_info(dev
, "using %s, %s for DMA transfers\n",
1451 dma_chan_name(tdes_dd
->dma_lch_in
.chan
),
1452 dma_chan_name(tdes_dd
->dma_lch_out
.chan
));
1455 spin_lock(&atmel_tdes
.lock
);
1456 list_add_tail(&tdes_dd
->list
, &atmel_tdes
.dev_list
);
1457 spin_unlock(&atmel_tdes
.lock
);
1459 err
= atmel_tdes_register_algs(tdes_dd
);
1463 dev_info(dev
, "Atmel DES/TDES\n");
1468 spin_lock(&atmel_tdes
.lock
);
1469 list_del(&tdes_dd
->list
);
1470 spin_unlock(&atmel_tdes
.lock
);
1471 if (tdes_dd
->caps
.has_dma
)
1472 atmel_tdes_dma_cleanup(tdes_dd
);
1475 atmel_tdes_buff_cleanup(tdes_dd
);
1477 iounmap(tdes_dd
->io_base
);
1479 clk_put(tdes_dd
->iclk
);
1481 free_irq(tdes_dd
->irq
, tdes_dd
);
1484 tasklet_kill(&tdes_dd
->done_task
);
1485 tasklet_kill(&tdes_dd
->queue_task
);
1489 dev_err(dev
, "initialization failed.\n");
1494 static int atmel_tdes_remove(struct platform_device
*pdev
)
1496 static struct atmel_tdes_dev
*tdes_dd
;
1498 tdes_dd
= platform_get_drvdata(pdev
);
1501 spin_lock(&atmel_tdes
.lock
);
1502 list_del(&tdes_dd
->list
);
1503 spin_unlock(&atmel_tdes
.lock
);
1505 atmel_tdes_unregister_algs(tdes_dd
);
1507 tasklet_kill(&tdes_dd
->done_task
);
1508 tasklet_kill(&tdes_dd
->queue_task
);
1510 if (tdes_dd
->caps
.has_dma
)
1511 atmel_tdes_dma_cleanup(tdes_dd
);
1513 atmel_tdes_buff_cleanup(tdes_dd
);
1515 iounmap(tdes_dd
->io_base
);
1517 clk_put(tdes_dd
->iclk
);
1519 if (tdes_dd
->irq
>= 0)
1520 free_irq(tdes_dd
->irq
, tdes_dd
);
1528 static struct platform_driver atmel_tdes_driver
= {
1529 .probe
= atmel_tdes_probe
,
1530 .remove
= atmel_tdes_remove
,
1532 .name
= "atmel_tdes",
1533 .owner
= THIS_MODULE
,
1534 .of_match_table
= of_match_ptr(atmel_tdes_dt_ids
),
1538 module_platform_driver(atmel_tdes_driver
);
1540 MODULE_DESCRIPTION("Atmel DES/TDES hw acceleration support.");
1541 MODULE_LICENSE("GPL v2");
1542 MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");