1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support for OMAP DES and Triple DES HW acceleration.
5 * Copyright (c) 2013 Texas Instruments Incorporated
6 * Author: Joel Fernandes <joelf@ti.com>
9 #define pr_fmt(fmt) "%s: " fmt, __func__
12 #define prn(num) printk(#num "=%d\n", num)
13 #define prx(num) printk(#num "=%x\n", num)
15 #define prn(num) do { } while (0)
16 #define prx(num) do { } while (0)
19 #include <crypto/engine.h>
20 #include <crypto/internal/des.h>
21 #include <crypto/internal/skcipher.h>
22 #include <crypto/scatterwalk.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/dmaengine.h>
25 #include <linux/err.h>
26 #include <linux/init.h>
27 #include <linux/interrupt.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
32 #include <linux/platform_device.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/scatterlist.h>
35 #include <linux/string.h>
37 #include "omap-crypto.h"
39 #define DST_MAXBURST 2
41 #define DES_BLOCK_WORDS (DES_BLOCK_SIZE >> 2)
43 #define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset)
45 #define DES_REG_KEY(dd, x) ((dd)->pdata->key_ofs - \
48 #define DES_REG_IV(dd, x) ((dd)->pdata->iv_ofs + ((x) * 0x04))
50 #define DES_REG_CTRL(dd) ((dd)->pdata->ctrl_ofs)
51 #define DES_REG_CTRL_CBC BIT(4)
52 #define DES_REG_CTRL_TDES BIT(3)
53 #define DES_REG_CTRL_DIRECTION BIT(2)
54 #define DES_REG_CTRL_INPUT_READY BIT(1)
55 #define DES_REG_CTRL_OUTPUT_READY BIT(0)
57 #define DES_REG_DATA_N(dd, x) ((dd)->pdata->data_ofs + ((x) * 0x04))
59 #define DES_REG_REV(dd) ((dd)->pdata->rev_ofs)
61 #define DES_REG_MASK(dd) ((dd)->pdata->mask_ofs)
63 #define DES_REG_LENGTH_N(x) (0x24 + ((x) * 0x04))
65 #define DES_REG_IRQ_STATUS(dd) ((dd)->pdata->irq_status_ofs)
66 #define DES_REG_IRQ_ENABLE(dd) ((dd)->pdata->irq_enable_ofs)
67 #define DES_REG_IRQ_DATA_IN BIT(1)
68 #define DES_REG_IRQ_DATA_OUT BIT(2)
70 #define FLAGS_MODE_MASK 0x000f
71 #define FLAGS_ENCRYPT BIT(0)
72 #define FLAGS_CBC BIT(1)
73 #define FLAGS_INIT BIT(4)
74 #define FLAGS_BUSY BIT(6)
76 #define DEFAULT_AUTOSUSPEND_DELAY 1000
78 #define FLAGS_IN_DATA_ST_SHIFT 8
79 #define FLAGS_OUT_DATA_ST_SHIFT 10
82 struct omap_des_dev
*dd
;
85 __le32 key
[(3 * DES_KEY_SIZE
) / sizeof(u32
)];
89 struct omap_des_reqctx
{
93 #define OMAP_DES_QUEUE_LENGTH 1
94 #define OMAP_DES_CACHE_SIZE 0
96 struct omap_des_algs_info
{
97 struct skcipher_engine_alg
*algs_list
;
99 unsigned int registered
;
102 struct omap_des_pdata
{
103 struct omap_des_algs_info
*algs_info
;
104 unsigned int algs_info_size
;
106 void (*trigger
)(struct omap_des_dev
*dd
, int length
);
127 struct omap_des_dev
{
128 struct list_head list
;
129 unsigned long phys_base
;
130 void __iomem
*io_base
;
131 struct omap_des_ctx
*ctx
;
136 struct tasklet_struct done_task
;
138 struct skcipher_request
*req
;
139 struct crypto_engine
*engine
;
141 * total is used by PIO mode for book keeping so introduce
142 * variable total_save as need it to calc page_order
147 struct scatterlist
*in_sg
;
148 struct scatterlist
*out_sg
;
150 /* Buffers for copying for unaligned cases */
151 struct scatterlist in_sgl
;
152 struct scatterlist out_sgl
;
153 struct scatterlist
*orig_out
;
155 struct scatter_walk in_walk
;
156 struct scatter_walk out_walk
;
157 struct dma_chan
*dma_lch_in
;
158 struct dma_chan
*dma_lch_out
;
162 const struct omap_des_pdata
*pdata
;
165 /* keep registered devices data here */
166 static LIST_HEAD(dev_list
);
167 static DEFINE_SPINLOCK(list_lock
);
170 #define omap_des_read(dd, offset) \
173 _read_ret = __raw_readl(dd->io_base + offset); \
174 pr_err("omap_des_read(" #offset "=%#x)= %#x\n", \
175 offset, _read_ret); \
179 static inline u32
omap_des_read(struct omap_des_dev
*dd
, u32 offset
)
181 return __raw_readl(dd
->io_base
+ offset
);
186 #define omap_des_write(dd, offset, value) \
188 pr_err("omap_des_write(" #offset "=%#x) value=%#x\n", \
190 __raw_writel(value, dd->io_base + offset); \
193 static inline void omap_des_write(struct omap_des_dev
*dd
, u32 offset
,
196 __raw_writel(value
, dd
->io_base
+ offset
);
200 static inline void omap_des_write_mask(struct omap_des_dev
*dd
, u32 offset
,
205 val
= omap_des_read(dd
, offset
);
208 omap_des_write(dd
, offset
, val
);
211 static void omap_des_write_n(struct omap_des_dev
*dd
, u32 offset
,
212 u32
*value
, int count
)
214 for (; count
--; value
++, offset
+= 4)
215 omap_des_write(dd
, offset
, *value
);
218 static int omap_des_hw_init(struct omap_des_dev
*dd
)
223 * clocks are enabled when request starts and disabled when finished.
224 * It may be long delays between requests.
225 * Device might go to off mode to save power.
227 err
= pm_runtime_resume_and_get(dd
->dev
);
229 dev_err(dd
->dev
, "%s: failed to get_sync(%d)\n", __func__
, err
);
233 if (!(dd
->flags
& FLAGS_INIT
)) {
234 dd
->flags
|= FLAGS_INIT
;
241 static int omap_des_write_ctrl(struct omap_des_dev
*dd
)
245 u32 val
= 0, mask
= 0;
247 err
= omap_des_hw_init(dd
);
251 key32
= dd
->ctx
->keylen
/ sizeof(u32
);
253 /* it seems a key should always be set even if it has not changed */
254 for (i
= 0; i
< key32
; i
++) {
255 omap_des_write(dd
, DES_REG_KEY(dd
, i
),
256 __le32_to_cpu(dd
->ctx
->key
[i
]));
259 if ((dd
->flags
& FLAGS_CBC
) && dd
->req
->iv
)
260 omap_des_write_n(dd
, DES_REG_IV(dd
, 0), (void *)dd
->req
->iv
, 2);
262 if (dd
->flags
& FLAGS_CBC
)
263 val
|= DES_REG_CTRL_CBC
;
264 if (dd
->flags
& FLAGS_ENCRYPT
)
265 val
|= DES_REG_CTRL_DIRECTION
;
267 val
|= DES_REG_CTRL_TDES
;
269 mask
|= DES_REG_CTRL_CBC
| DES_REG_CTRL_DIRECTION
| DES_REG_CTRL_TDES
;
271 omap_des_write_mask(dd
, DES_REG_CTRL(dd
), val
, mask
);
276 static void omap_des_dma_trigger_omap4(struct omap_des_dev
*dd
, int length
)
280 omap_des_write(dd
, DES_REG_LENGTH_N(0), length
);
282 val
= dd
->pdata
->dma_start
;
284 if (dd
->dma_lch_out
!= NULL
)
285 val
|= dd
->pdata
->dma_enable_out
;
286 if (dd
->dma_lch_in
!= NULL
)
287 val
|= dd
->pdata
->dma_enable_in
;
289 mask
= dd
->pdata
->dma_enable_out
| dd
->pdata
->dma_enable_in
|
290 dd
->pdata
->dma_start
;
292 omap_des_write_mask(dd
, DES_REG_MASK(dd
), val
, mask
);
295 static void omap_des_dma_stop(struct omap_des_dev
*dd
)
299 mask
= dd
->pdata
->dma_enable_out
| dd
->pdata
->dma_enable_in
|
300 dd
->pdata
->dma_start
;
302 omap_des_write_mask(dd
, DES_REG_MASK(dd
), 0, mask
);
305 static struct omap_des_dev
*omap_des_find_dev(struct omap_des_ctx
*ctx
)
307 struct omap_des_dev
*dd
= NULL
, *tmp
;
309 spin_lock_bh(&list_lock
);
311 list_for_each_entry(tmp
, &dev_list
, list
) {
312 /* FIXME: take fist available des core */
318 /* already found before */
321 spin_unlock_bh(&list_lock
);
326 static void omap_des_dma_out_callback(void *data
)
328 struct omap_des_dev
*dd
= data
;
330 /* dma_lch_out - completed */
331 tasklet_schedule(&dd
->done_task
);
334 static int omap_des_dma_init(struct omap_des_dev
*dd
)
338 dd
->dma_lch_out
= NULL
;
339 dd
->dma_lch_in
= NULL
;
341 dd
->dma_lch_in
= dma_request_chan(dd
->dev
, "rx");
342 if (IS_ERR(dd
->dma_lch_in
)) {
343 dev_err(dd
->dev
, "Unable to request in DMA channel\n");
344 return PTR_ERR(dd
->dma_lch_in
);
347 dd
->dma_lch_out
= dma_request_chan(dd
->dev
, "tx");
348 if (IS_ERR(dd
->dma_lch_out
)) {
349 dev_err(dd
->dev
, "Unable to request out DMA channel\n");
350 err
= PTR_ERR(dd
->dma_lch_out
);
357 dma_release_channel(dd
->dma_lch_in
);
362 static void omap_des_dma_cleanup(struct omap_des_dev
*dd
)
367 dma_release_channel(dd
->dma_lch_out
);
368 dma_release_channel(dd
->dma_lch_in
);
371 static int omap_des_crypt_dma(struct crypto_tfm
*tfm
,
372 struct scatterlist
*in_sg
, struct scatterlist
*out_sg
,
373 int in_sg_len
, int out_sg_len
)
375 struct omap_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
376 struct omap_des_dev
*dd
= ctx
->dd
;
377 struct dma_async_tx_descriptor
*tx_in
, *tx_out
;
378 struct dma_slave_config cfg
;
382 scatterwalk_start(&dd
->in_walk
, dd
->in_sg
);
383 scatterwalk_start(&dd
->out_walk
, dd
->out_sg
);
385 /* Enable DATAIN interrupt and let it take
387 omap_des_write(dd
, DES_REG_IRQ_ENABLE(dd
), 0x2);
391 dma_sync_sg_for_device(dd
->dev
, dd
->in_sg
, in_sg_len
, DMA_TO_DEVICE
);
393 memset(&cfg
, 0, sizeof(cfg
));
395 cfg
.src_addr
= dd
->phys_base
+ DES_REG_DATA_N(dd
, 0);
396 cfg
.dst_addr
= dd
->phys_base
+ DES_REG_DATA_N(dd
, 0);
397 cfg
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
398 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
399 cfg
.src_maxburst
= DST_MAXBURST
;
400 cfg
.dst_maxburst
= DST_MAXBURST
;
403 ret
= dmaengine_slave_config(dd
->dma_lch_in
, &cfg
);
405 dev_err(dd
->dev
, "can't configure IN dmaengine slave: %d\n",
410 tx_in
= dmaengine_prep_slave_sg(dd
->dma_lch_in
, in_sg
, in_sg_len
,
412 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
414 dev_err(dd
->dev
, "IN prep_slave_sg() failed\n");
418 /* No callback necessary */
419 tx_in
->callback_param
= dd
;
422 ret
= dmaengine_slave_config(dd
->dma_lch_out
, &cfg
);
424 dev_err(dd
->dev
, "can't configure OUT dmaengine slave: %d\n",
429 tx_out
= dmaengine_prep_slave_sg(dd
->dma_lch_out
, out_sg
, out_sg_len
,
431 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
433 dev_err(dd
->dev
, "OUT prep_slave_sg() failed\n");
437 tx_out
->callback
= omap_des_dma_out_callback
;
438 tx_out
->callback_param
= dd
;
440 dmaengine_submit(tx_in
);
441 dmaengine_submit(tx_out
);
443 dma_async_issue_pending(dd
->dma_lch_in
);
444 dma_async_issue_pending(dd
->dma_lch_out
);
447 dd
->pdata
->trigger(dd
, dd
->total
);
452 static int omap_des_crypt_dma_start(struct omap_des_dev
*dd
)
454 struct crypto_tfm
*tfm
= crypto_skcipher_tfm(
455 crypto_skcipher_reqtfm(dd
->req
));
458 pr_debug("total: %zd\n", dd
->total
);
461 err
= dma_map_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
,
464 dev_err(dd
->dev
, "dma_map_sg() error\n");
468 err
= dma_map_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
471 dev_err(dd
->dev
, "dma_map_sg() error\n");
476 err
= omap_des_crypt_dma(tfm
, dd
->in_sg
, dd
->out_sg
, dd
->in_sg_len
,
478 if (err
&& !dd
->pio_only
) {
479 dma_unmap_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
, DMA_TO_DEVICE
);
480 dma_unmap_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
487 static void omap_des_finish_req(struct omap_des_dev
*dd
, int err
)
489 struct skcipher_request
*req
= dd
->req
;
491 pr_debug("err: %d\n", err
);
493 crypto_finalize_skcipher_request(dd
->engine
, req
, err
);
495 pm_runtime_mark_last_busy(dd
->dev
);
496 pm_runtime_put_autosuspend(dd
->dev
);
499 static int omap_des_crypt_dma_stop(struct omap_des_dev
*dd
)
501 pr_debug("total: %zd\n", dd
->total
);
503 omap_des_dma_stop(dd
);
505 dmaengine_terminate_all(dd
->dma_lch_in
);
506 dmaengine_terminate_all(dd
->dma_lch_out
);
511 static int omap_des_handle_queue(struct omap_des_dev
*dd
,
512 struct skcipher_request
*req
)
515 return crypto_transfer_skcipher_request_to_engine(dd
->engine
, req
);
520 static int omap_des_prepare_req(struct skcipher_request
*req
,
521 struct omap_des_dev
*dd
)
523 struct omap_des_ctx
*ctx
= crypto_skcipher_ctx(
524 crypto_skcipher_reqtfm(req
));
525 struct omap_des_reqctx
*rctx
;
529 /* assign new request to device */
531 dd
->total
= req
->cryptlen
;
532 dd
->total_save
= req
->cryptlen
;
533 dd
->in_sg
= req
->src
;
534 dd
->out_sg
= req
->dst
;
535 dd
->orig_out
= req
->dst
;
537 flags
= OMAP_CRYPTO_COPY_DATA
;
538 if (req
->src
== req
->dst
)
539 flags
|= OMAP_CRYPTO_FORCE_COPY
;
541 ret
= omap_crypto_align_sg(&dd
->in_sg
, dd
->total
, DES_BLOCK_SIZE
,
543 FLAGS_IN_DATA_ST_SHIFT
, &dd
->flags
);
547 ret
= omap_crypto_align_sg(&dd
->out_sg
, dd
->total
, DES_BLOCK_SIZE
,
549 FLAGS_OUT_DATA_ST_SHIFT
, &dd
->flags
);
553 dd
->in_sg_len
= sg_nents_for_len(dd
->in_sg
, dd
->total
);
554 if (dd
->in_sg_len
< 0)
555 return dd
->in_sg_len
;
557 dd
->out_sg_len
= sg_nents_for_len(dd
->out_sg
, dd
->total
);
558 if (dd
->out_sg_len
< 0)
559 return dd
->out_sg_len
;
561 rctx
= skcipher_request_ctx(req
);
562 ctx
= crypto_skcipher_ctx(crypto_skcipher_reqtfm(req
));
563 rctx
->mode
&= FLAGS_MODE_MASK
;
564 dd
->flags
= (dd
->flags
& ~FLAGS_MODE_MASK
) | rctx
->mode
;
569 return omap_des_write_ctrl(dd
);
572 static int omap_des_crypt_req(struct crypto_engine
*engine
,
575 struct skcipher_request
*req
= container_of(areq
, struct skcipher_request
, base
);
576 struct omap_des_ctx
*ctx
= crypto_skcipher_ctx(
577 crypto_skcipher_reqtfm(req
));
578 struct omap_des_dev
*dd
= omap_des_find_dev(ctx
);
583 return omap_des_prepare_req(req
, dd
) ?:
584 omap_des_crypt_dma_start(dd
);
587 static void omap_des_done_task(unsigned long data
)
589 struct omap_des_dev
*dd
= (struct omap_des_dev
*)data
;
592 pr_debug("enter done_task\n");
595 dma_sync_sg_for_device(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
597 dma_unmap_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
, DMA_TO_DEVICE
);
598 dma_unmap_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
600 omap_des_crypt_dma_stop(dd
);
603 omap_crypto_cleanup(&dd
->in_sgl
, NULL
, 0, dd
->total_save
,
604 FLAGS_IN_DATA_ST_SHIFT
, dd
->flags
);
606 omap_crypto_cleanup(&dd
->out_sgl
, dd
->orig_out
, 0, dd
->total_save
,
607 FLAGS_OUT_DATA_ST_SHIFT
, dd
->flags
);
609 if ((dd
->flags
& FLAGS_CBC
) && dd
->req
->iv
)
610 for (i
= 0; i
< 2; i
++)
611 ((u32
*)dd
->req
->iv
)[i
] =
612 omap_des_read(dd
, DES_REG_IV(dd
, i
));
614 omap_des_finish_req(dd
, 0);
619 static int omap_des_crypt(struct skcipher_request
*req
, unsigned long mode
)
621 struct omap_des_ctx
*ctx
= crypto_skcipher_ctx(
622 crypto_skcipher_reqtfm(req
));
623 struct omap_des_reqctx
*rctx
= skcipher_request_ctx(req
);
624 struct omap_des_dev
*dd
;
626 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req
->cryptlen
,
627 !!(mode
& FLAGS_ENCRYPT
),
628 !!(mode
& FLAGS_CBC
));
633 if (!IS_ALIGNED(req
->cryptlen
, DES_BLOCK_SIZE
))
636 dd
= omap_des_find_dev(ctx
);
642 return omap_des_handle_queue(dd
, req
);
645 /* ********************** ALG API ************************************ */
647 static int omap_des_setkey(struct crypto_skcipher
*cipher
, const u8
*key
,
650 struct omap_des_ctx
*ctx
= crypto_skcipher_ctx(cipher
);
653 pr_debug("enter, keylen: %d\n", keylen
);
655 err
= verify_skcipher_des_key(cipher
, key
);
659 memcpy(ctx
->key
, key
, keylen
);
660 ctx
->keylen
= keylen
;
665 static int omap_des3_setkey(struct crypto_skcipher
*cipher
, const u8
*key
,
668 struct omap_des_ctx
*ctx
= crypto_skcipher_ctx(cipher
);
671 pr_debug("enter, keylen: %d\n", keylen
);
673 err
= verify_skcipher_des3_key(cipher
, key
);
677 memcpy(ctx
->key
, key
, keylen
);
678 ctx
->keylen
= keylen
;
683 static int omap_des_ecb_encrypt(struct skcipher_request
*req
)
685 return omap_des_crypt(req
, FLAGS_ENCRYPT
);
688 static int omap_des_ecb_decrypt(struct skcipher_request
*req
)
690 return omap_des_crypt(req
, 0);
693 static int omap_des_cbc_encrypt(struct skcipher_request
*req
)
695 return omap_des_crypt(req
, FLAGS_ENCRYPT
| FLAGS_CBC
);
698 static int omap_des_cbc_decrypt(struct skcipher_request
*req
)
700 return omap_des_crypt(req
, FLAGS_CBC
);
703 static int omap_des_init_tfm(struct crypto_skcipher
*tfm
)
707 crypto_skcipher_set_reqsize(tfm
, sizeof(struct omap_des_reqctx
));
712 /* ********************** ALGS ************************************ */
714 static struct skcipher_engine_alg algs_ecb_cbc
[] = {
717 .base
.cra_name
= "ecb(des)",
718 .base
.cra_driver_name
= "ecb-des-omap",
719 .base
.cra_priority
= 300,
720 .base
.cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
|
722 .base
.cra_blocksize
= DES_BLOCK_SIZE
,
723 .base
.cra_ctxsize
= sizeof(struct omap_des_ctx
),
724 .base
.cra_module
= THIS_MODULE
,
726 .min_keysize
= DES_KEY_SIZE
,
727 .max_keysize
= DES_KEY_SIZE
,
728 .setkey
= omap_des_setkey
,
729 .encrypt
= omap_des_ecb_encrypt
,
730 .decrypt
= omap_des_ecb_decrypt
,
731 .init
= omap_des_init_tfm
,
733 .op
.do_one_request
= omap_des_crypt_req
,
737 .base
.cra_name
= "cbc(des)",
738 .base
.cra_driver_name
= "cbc-des-omap",
739 .base
.cra_priority
= 300,
740 .base
.cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
|
742 .base
.cra_blocksize
= DES_BLOCK_SIZE
,
743 .base
.cra_ctxsize
= sizeof(struct omap_des_ctx
),
744 .base
.cra_module
= THIS_MODULE
,
746 .min_keysize
= DES_KEY_SIZE
,
747 .max_keysize
= DES_KEY_SIZE
,
748 .ivsize
= DES_BLOCK_SIZE
,
749 .setkey
= omap_des_setkey
,
750 .encrypt
= omap_des_cbc_encrypt
,
751 .decrypt
= omap_des_cbc_decrypt
,
752 .init
= omap_des_init_tfm
,
754 .op
.do_one_request
= omap_des_crypt_req
,
758 .base
.cra_name
= "ecb(des3_ede)",
759 .base
.cra_driver_name
= "ecb-des3-omap",
760 .base
.cra_priority
= 300,
761 .base
.cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
|
763 .base
.cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
764 .base
.cra_ctxsize
= sizeof(struct omap_des_ctx
),
765 .base
.cra_module
= THIS_MODULE
,
767 .min_keysize
= DES3_EDE_KEY_SIZE
,
768 .max_keysize
= DES3_EDE_KEY_SIZE
,
769 .setkey
= omap_des3_setkey
,
770 .encrypt
= omap_des_ecb_encrypt
,
771 .decrypt
= omap_des_ecb_decrypt
,
772 .init
= omap_des_init_tfm
,
774 .op
.do_one_request
= omap_des_crypt_req
,
778 .base
.cra_name
= "cbc(des3_ede)",
779 .base
.cra_driver_name
= "cbc-des3-omap",
780 .base
.cra_priority
= 300,
781 .base
.cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
|
783 .base
.cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
784 .base
.cra_ctxsize
= sizeof(struct omap_des_ctx
),
785 .base
.cra_module
= THIS_MODULE
,
787 .min_keysize
= DES3_EDE_KEY_SIZE
,
788 .max_keysize
= DES3_EDE_KEY_SIZE
,
789 .ivsize
= DES3_EDE_BLOCK_SIZE
,
790 .setkey
= omap_des3_setkey
,
791 .encrypt
= omap_des_cbc_encrypt
,
792 .decrypt
= omap_des_cbc_decrypt
,
793 .init
= omap_des_init_tfm
,
795 .op
.do_one_request
= omap_des_crypt_req
,
799 static struct omap_des_algs_info omap_des_algs_info_ecb_cbc
[] = {
801 .algs_list
= algs_ecb_cbc
,
802 .size
= ARRAY_SIZE(algs_ecb_cbc
),
807 static const struct omap_des_pdata omap_des_pdata_omap4
= {
808 .algs_info
= omap_des_algs_info_ecb_cbc
,
809 .algs_info_size
= ARRAY_SIZE(omap_des_algs_info_ecb_cbc
),
810 .trigger
= omap_des_dma_trigger_omap4
,
817 .irq_status_ofs
= 0x3c,
818 .irq_enable_ofs
= 0x40,
819 .dma_enable_in
= BIT(5),
820 .dma_enable_out
= BIT(6),
821 .major_mask
= 0x0700,
823 .minor_mask
= 0x003f,
827 static irqreturn_t
omap_des_irq(int irq
, void *dev_id
)
829 struct omap_des_dev
*dd
= dev_id
;
833 status
= omap_des_read(dd
, DES_REG_IRQ_STATUS(dd
));
834 if (status
& DES_REG_IRQ_DATA_IN
) {
835 omap_des_write(dd
, DES_REG_IRQ_ENABLE(dd
), 0x0);
839 BUG_ON(_calc_walked(in
) > dd
->in_sg
->length
);
841 src
= sg_virt(dd
->in_sg
) + _calc_walked(in
);
843 for (i
= 0; i
< DES_BLOCK_WORDS
; i
++) {
844 omap_des_write(dd
, DES_REG_DATA_N(dd
, i
), *src
);
846 scatterwalk_advance(&dd
->in_walk
, 4);
847 if (dd
->in_sg
->length
== _calc_walked(in
)) {
848 dd
->in_sg
= sg_next(dd
->in_sg
);
850 scatterwalk_start(&dd
->in_walk
,
852 src
= sg_virt(dd
->in_sg
) +
860 /* Clear IRQ status */
861 status
&= ~DES_REG_IRQ_DATA_IN
;
862 omap_des_write(dd
, DES_REG_IRQ_STATUS(dd
), status
);
864 /* Enable DATA_OUT interrupt */
865 omap_des_write(dd
, DES_REG_IRQ_ENABLE(dd
), 0x4);
867 } else if (status
& DES_REG_IRQ_DATA_OUT
) {
868 omap_des_write(dd
, DES_REG_IRQ_ENABLE(dd
), 0x0);
872 BUG_ON(_calc_walked(out
) > dd
->out_sg
->length
);
874 dst
= sg_virt(dd
->out_sg
) + _calc_walked(out
);
876 for (i
= 0; i
< DES_BLOCK_WORDS
; i
++) {
877 *dst
= omap_des_read(dd
, DES_REG_DATA_N(dd
, i
));
878 scatterwalk_advance(&dd
->out_walk
, 4);
879 if (dd
->out_sg
->length
== _calc_walked(out
)) {
880 dd
->out_sg
= sg_next(dd
->out_sg
);
882 scatterwalk_start(&dd
->out_walk
,
884 dst
= sg_virt(dd
->out_sg
) +
892 BUG_ON(dd
->total
< DES_BLOCK_SIZE
);
894 dd
->total
-= DES_BLOCK_SIZE
;
896 /* Clear IRQ status */
897 status
&= ~DES_REG_IRQ_DATA_OUT
;
898 omap_des_write(dd
, DES_REG_IRQ_STATUS(dd
), status
);
901 /* All bytes read! */
902 tasklet_schedule(&dd
->done_task
);
904 /* Enable DATA_IN interrupt for next block */
905 omap_des_write(dd
, DES_REG_IRQ_ENABLE(dd
), 0x2);
911 static const struct of_device_id omap_des_of_match
[] = {
913 .compatible
= "ti,omap4-des",
914 .data
= &omap_des_pdata_omap4
,
918 MODULE_DEVICE_TABLE(of
, omap_des_of_match
);
920 static int omap_des_get_of(struct omap_des_dev
*dd
,
921 struct platform_device
*pdev
)
924 dd
->pdata
= of_device_get_match_data(&pdev
->dev
);
926 dev_err(&pdev
->dev
, "no compatible OF match\n");
933 static int omap_des_get_of(struct omap_des_dev
*dd
,
940 static int omap_des_get_pdev(struct omap_des_dev
*dd
,
941 struct platform_device
*pdev
)
943 /* non-DT devices get pdata from pdev */
944 dd
->pdata
= pdev
->dev
.platform_data
;
949 static int omap_des_probe(struct platform_device
*pdev
)
951 struct device
*dev
= &pdev
->dev
;
952 struct omap_des_dev
*dd
;
953 struct skcipher_engine_alg
*algp
;
954 struct resource
*res
;
955 int err
= -ENOMEM
, i
, j
, irq
= -1;
958 dd
= devm_kzalloc(dev
, sizeof(struct omap_des_dev
), GFP_KERNEL
);
960 dev_err(dev
, "unable to alloc data struct.\n");
964 platform_set_drvdata(pdev
, dd
);
966 err
= (dev
->of_node
) ? omap_des_get_of(dd
, pdev
) :
967 omap_des_get_pdev(dd
, pdev
);
971 dd
->io_base
= devm_platform_get_and_ioremap_resource(pdev
, 0, &res
);
972 if (IS_ERR(dd
->io_base
)) {
973 err
= PTR_ERR(dd
->io_base
);
976 dd
->phys_base
= res
->start
;
978 pm_runtime_use_autosuspend(dev
);
979 pm_runtime_set_autosuspend_delay(dev
, DEFAULT_AUTOSUSPEND_DELAY
);
981 pm_runtime_enable(dev
);
982 err
= pm_runtime_resume_and_get(dev
);
984 dev_err(dd
->dev
, "%s: failed to get_sync(%d)\n", __func__
, err
);
988 omap_des_dma_stop(dd
);
990 reg
= omap_des_read(dd
, DES_REG_REV(dd
));
992 pm_runtime_put_sync(dev
);
994 dev_info(dev
, "OMAP DES hw accel rev: %u.%u\n",
995 (reg
& dd
->pdata
->major_mask
) >> dd
->pdata
->major_shift
,
996 (reg
& dd
->pdata
->minor_mask
) >> dd
->pdata
->minor_shift
);
998 tasklet_init(&dd
->done_task
, omap_des_done_task
, (unsigned long)dd
);
1000 err
= omap_des_dma_init(dd
);
1001 if (err
== -EPROBE_DEFER
) {
1003 } else if (err
&& DES_REG_IRQ_STATUS(dd
) && DES_REG_IRQ_ENABLE(dd
)) {
1006 irq
= platform_get_irq(pdev
, 0);
1012 err
= devm_request_irq(dev
, irq
, omap_des_irq
, 0,
1015 dev_err(dev
, "Unable to grab omap-des IRQ\n");
1021 INIT_LIST_HEAD(&dd
->list
);
1022 spin_lock_bh(&list_lock
);
1023 list_add_tail(&dd
->list
, &dev_list
);
1024 spin_unlock_bh(&list_lock
);
1026 /* Initialize des crypto engine */
1027 dd
->engine
= crypto_engine_alloc_init(dev
, 1);
1033 err
= crypto_engine_start(dd
->engine
);
1037 for (i
= 0; i
< dd
->pdata
->algs_info_size
; i
++) {
1038 for (j
= 0; j
< dd
->pdata
->algs_info
[i
].size
; j
++) {
1039 algp
= &dd
->pdata
->algs_info
[i
].algs_list
[j
];
1041 pr_debug("reg alg: %s\n", algp
->base
.base
.cra_name
);
1043 err
= crypto_engine_register_skcipher(algp
);
1047 dd
->pdata
->algs_info
[i
].registered
++;
1054 for (i
= dd
->pdata
->algs_info_size
- 1; i
>= 0; i
--)
1055 for (j
= dd
->pdata
->algs_info
[i
].registered
- 1; j
>= 0; j
--)
1056 crypto_engine_unregister_skcipher(
1057 &dd
->pdata
->algs_info
[i
].algs_list
[j
]);
1061 crypto_engine_exit(dd
->engine
);
1063 omap_des_dma_cleanup(dd
);
1065 tasklet_kill(&dd
->done_task
);
1067 pm_runtime_disable(dev
);
1071 dev_err(dev
, "initialization failed.\n");
1075 static void omap_des_remove(struct platform_device
*pdev
)
1077 struct omap_des_dev
*dd
= platform_get_drvdata(pdev
);
1080 spin_lock_bh(&list_lock
);
1081 list_del(&dd
->list
);
1082 spin_unlock_bh(&list_lock
);
1084 for (i
= dd
->pdata
->algs_info_size
- 1; i
>= 0; i
--)
1085 for (j
= dd
->pdata
->algs_info
[i
].registered
- 1; j
>= 0; j
--)
1086 crypto_engine_unregister_skcipher(
1087 &dd
->pdata
->algs_info
[i
].algs_list
[j
]);
1089 tasklet_kill(&dd
->done_task
);
1090 omap_des_dma_cleanup(dd
);
1091 pm_runtime_disable(dd
->dev
);
1094 #ifdef CONFIG_PM_SLEEP
1095 static int omap_des_suspend(struct device
*dev
)
1097 pm_runtime_put_sync(dev
);
1101 static int omap_des_resume(struct device
*dev
)
1105 err
= pm_runtime_resume_and_get(dev
);
1107 dev_err(dev
, "%s: failed to get_sync(%d)\n", __func__
, err
);
1114 static SIMPLE_DEV_PM_OPS(omap_des_pm_ops
, omap_des_suspend
, omap_des_resume
);
1116 static struct platform_driver omap_des_driver
= {
1117 .probe
= omap_des_probe
,
1118 .remove
= omap_des_remove
,
1121 .pm
= &omap_des_pm_ops
,
1122 .of_match_table
= of_match_ptr(omap_des_of_match
),
1126 module_platform_driver(omap_des_driver
);
1128 MODULE_DESCRIPTION("OMAP DES hw acceleration support.");
1129 MODULE_LICENSE("GPL v2");
1130 MODULE_AUTHOR("Joel Fernandes <joelf@ti.com>");