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 <linux/err.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/errno.h>
23 #include <linux/kernel.h>
24 #include <linux/platform_device.h>
25 #include <linux/scatterlist.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/dmaengine.h>
28 #include <linux/pm_runtime.h>
30 #include <linux/of_device.h>
31 #include <linux/of_address.h>
33 #include <linux/crypto.h>
34 #include <linux/interrupt.h>
35 #include <crypto/scatterwalk.h>
36 #include <crypto/internal/des.h>
37 #include <crypto/internal/skcipher.h>
38 #include <crypto/algapi.h>
39 #include <crypto/engine.h>
41 #include "omap-crypto.h"
43 #define DST_MAXBURST 2
45 #define DES_BLOCK_WORDS (DES_BLOCK_SIZE >> 2)
47 #define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset)
49 #define DES_REG_KEY(dd, x) ((dd)->pdata->key_ofs - \
52 #define DES_REG_IV(dd, x) ((dd)->pdata->iv_ofs + ((x) * 0x04))
54 #define DES_REG_CTRL(dd) ((dd)->pdata->ctrl_ofs)
55 #define DES_REG_CTRL_CBC BIT(4)
56 #define DES_REG_CTRL_TDES BIT(3)
57 #define DES_REG_CTRL_DIRECTION BIT(2)
58 #define DES_REG_CTRL_INPUT_READY BIT(1)
59 #define DES_REG_CTRL_OUTPUT_READY BIT(0)
61 #define DES_REG_DATA_N(dd, x) ((dd)->pdata->data_ofs + ((x) * 0x04))
63 #define DES_REG_REV(dd) ((dd)->pdata->rev_ofs)
65 #define DES_REG_MASK(dd) ((dd)->pdata->mask_ofs)
67 #define DES_REG_LENGTH_N(x) (0x24 + ((x) * 0x04))
69 #define DES_REG_IRQ_STATUS(dd) ((dd)->pdata->irq_status_ofs)
70 #define DES_REG_IRQ_ENABLE(dd) ((dd)->pdata->irq_enable_ofs)
71 #define DES_REG_IRQ_DATA_IN BIT(1)
72 #define DES_REG_IRQ_DATA_OUT BIT(2)
74 #define FLAGS_MODE_MASK 0x000f
75 #define FLAGS_ENCRYPT BIT(0)
76 #define FLAGS_CBC BIT(1)
77 #define FLAGS_INIT BIT(4)
78 #define FLAGS_BUSY BIT(6)
80 #define DEFAULT_AUTOSUSPEND_DELAY 1000
82 #define FLAGS_IN_DATA_ST_SHIFT 8
83 #define FLAGS_OUT_DATA_ST_SHIFT 10
86 struct crypto_engine_ctx enginectx
;
87 struct omap_des_dev
*dd
;
90 __le32 key
[(3 * DES_KEY_SIZE
) / sizeof(u32
)];
94 struct omap_des_reqctx
{
98 #define OMAP_DES_QUEUE_LENGTH 1
99 #define OMAP_DES_CACHE_SIZE 0
101 struct omap_des_algs_info
{
102 struct skcipher_alg
*algs_list
;
104 unsigned int registered
;
107 struct omap_des_pdata
{
108 struct omap_des_algs_info
*algs_info
;
109 unsigned int algs_info_size
;
111 void (*trigger
)(struct omap_des_dev
*dd
, int length
);
132 struct omap_des_dev
{
133 struct list_head list
;
134 unsigned long phys_base
;
135 void __iomem
*io_base
;
136 struct omap_des_ctx
*ctx
;
141 struct tasklet_struct done_task
;
143 struct skcipher_request
*req
;
144 struct crypto_engine
*engine
;
146 * total is used by PIO mode for book keeping so introduce
147 * variable total_save as need it to calc page_order
152 struct scatterlist
*in_sg
;
153 struct scatterlist
*out_sg
;
155 /* Buffers for copying for unaligned cases */
156 struct scatterlist in_sgl
;
157 struct scatterlist out_sgl
;
158 struct scatterlist
*orig_out
;
160 struct scatter_walk in_walk
;
161 struct scatter_walk out_walk
;
162 struct dma_chan
*dma_lch_in
;
163 struct dma_chan
*dma_lch_out
;
167 const struct omap_des_pdata
*pdata
;
170 /* keep registered devices data here */
171 static LIST_HEAD(dev_list
);
172 static DEFINE_SPINLOCK(list_lock
);
175 #define omap_des_read(dd, offset) \
178 _read_ret = __raw_readl(dd->io_base + offset); \
179 pr_err("omap_des_read(" #offset "=%#x)= %#x\n", \
180 offset, _read_ret); \
184 static inline u32
omap_des_read(struct omap_des_dev
*dd
, u32 offset
)
186 return __raw_readl(dd
->io_base
+ offset
);
191 #define omap_des_write(dd, offset, value) \
193 pr_err("omap_des_write(" #offset "=%#x) value=%#x\n", \
195 __raw_writel(value, dd->io_base + offset); \
198 static inline void omap_des_write(struct omap_des_dev
*dd
, u32 offset
,
201 __raw_writel(value
, dd
->io_base
+ offset
);
205 static inline void omap_des_write_mask(struct omap_des_dev
*dd
, u32 offset
,
210 val
= omap_des_read(dd
, offset
);
213 omap_des_write(dd
, offset
, val
);
216 static void omap_des_write_n(struct omap_des_dev
*dd
, u32 offset
,
217 u32
*value
, int count
)
219 for (; count
--; value
++, offset
+= 4)
220 omap_des_write(dd
, offset
, *value
);
223 static int omap_des_hw_init(struct omap_des_dev
*dd
)
228 * clocks are enabled when request starts and disabled when finished.
229 * It may be long delays between requests.
230 * Device might go to off mode to save power.
232 err
= pm_runtime_get_sync(dd
->dev
);
234 pm_runtime_put_noidle(dd
->dev
);
235 dev_err(dd
->dev
, "%s: failed to get_sync(%d)\n", __func__
, err
);
239 if (!(dd
->flags
& FLAGS_INIT
)) {
240 dd
->flags
|= FLAGS_INIT
;
247 static int omap_des_write_ctrl(struct omap_des_dev
*dd
)
251 u32 val
= 0, mask
= 0;
253 err
= omap_des_hw_init(dd
);
257 key32
= dd
->ctx
->keylen
/ sizeof(u32
);
259 /* it seems a key should always be set even if it has not changed */
260 for (i
= 0; i
< key32
; i
++) {
261 omap_des_write(dd
, DES_REG_KEY(dd
, i
),
262 __le32_to_cpu(dd
->ctx
->key
[i
]));
265 if ((dd
->flags
& FLAGS_CBC
) && dd
->req
->iv
)
266 omap_des_write_n(dd
, DES_REG_IV(dd
, 0), (void *)dd
->req
->iv
, 2);
268 if (dd
->flags
& FLAGS_CBC
)
269 val
|= DES_REG_CTRL_CBC
;
270 if (dd
->flags
& FLAGS_ENCRYPT
)
271 val
|= DES_REG_CTRL_DIRECTION
;
273 val
|= DES_REG_CTRL_TDES
;
275 mask
|= DES_REG_CTRL_CBC
| DES_REG_CTRL_DIRECTION
| DES_REG_CTRL_TDES
;
277 omap_des_write_mask(dd
, DES_REG_CTRL(dd
), val
, mask
);
282 static void omap_des_dma_trigger_omap4(struct omap_des_dev
*dd
, int length
)
286 omap_des_write(dd
, DES_REG_LENGTH_N(0), length
);
288 val
= dd
->pdata
->dma_start
;
290 if (dd
->dma_lch_out
!= NULL
)
291 val
|= dd
->pdata
->dma_enable_out
;
292 if (dd
->dma_lch_in
!= NULL
)
293 val
|= dd
->pdata
->dma_enable_in
;
295 mask
= dd
->pdata
->dma_enable_out
| dd
->pdata
->dma_enable_in
|
296 dd
->pdata
->dma_start
;
298 omap_des_write_mask(dd
, DES_REG_MASK(dd
), val
, mask
);
301 static void omap_des_dma_stop(struct omap_des_dev
*dd
)
305 mask
= dd
->pdata
->dma_enable_out
| dd
->pdata
->dma_enable_in
|
306 dd
->pdata
->dma_start
;
308 omap_des_write_mask(dd
, DES_REG_MASK(dd
), 0, mask
);
311 static struct omap_des_dev
*omap_des_find_dev(struct omap_des_ctx
*ctx
)
313 struct omap_des_dev
*dd
= NULL
, *tmp
;
315 spin_lock_bh(&list_lock
);
317 list_for_each_entry(tmp
, &dev_list
, list
) {
318 /* FIXME: take fist available des core */
324 /* already found before */
327 spin_unlock_bh(&list_lock
);
332 static void omap_des_dma_out_callback(void *data
)
334 struct omap_des_dev
*dd
= data
;
336 /* dma_lch_out - completed */
337 tasklet_schedule(&dd
->done_task
);
340 static int omap_des_dma_init(struct omap_des_dev
*dd
)
344 dd
->dma_lch_out
= NULL
;
345 dd
->dma_lch_in
= NULL
;
347 dd
->dma_lch_in
= dma_request_chan(dd
->dev
, "rx");
348 if (IS_ERR(dd
->dma_lch_in
)) {
349 dev_err(dd
->dev
, "Unable to request in DMA channel\n");
350 return PTR_ERR(dd
->dma_lch_in
);
353 dd
->dma_lch_out
= dma_request_chan(dd
->dev
, "tx");
354 if (IS_ERR(dd
->dma_lch_out
)) {
355 dev_err(dd
->dev
, "Unable to request out DMA channel\n");
356 err
= PTR_ERR(dd
->dma_lch_out
);
363 dma_release_channel(dd
->dma_lch_in
);
368 static void omap_des_dma_cleanup(struct omap_des_dev
*dd
)
373 dma_release_channel(dd
->dma_lch_out
);
374 dma_release_channel(dd
->dma_lch_in
);
377 static int omap_des_crypt_dma(struct crypto_tfm
*tfm
,
378 struct scatterlist
*in_sg
, struct scatterlist
*out_sg
,
379 int in_sg_len
, int out_sg_len
)
381 struct omap_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
382 struct omap_des_dev
*dd
= ctx
->dd
;
383 struct dma_async_tx_descriptor
*tx_in
, *tx_out
;
384 struct dma_slave_config cfg
;
388 scatterwalk_start(&dd
->in_walk
, dd
->in_sg
);
389 scatterwalk_start(&dd
->out_walk
, dd
->out_sg
);
391 /* Enable DATAIN interrupt and let it take
393 omap_des_write(dd
, DES_REG_IRQ_ENABLE(dd
), 0x2);
397 dma_sync_sg_for_device(dd
->dev
, dd
->in_sg
, in_sg_len
, DMA_TO_DEVICE
);
399 memset(&cfg
, 0, sizeof(cfg
));
401 cfg
.src_addr
= dd
->phys_base
+ DES_REG_DATA_N(dd
, 0);
402 cfg
.dst_addr
= dd
->phys_base
+ DES_REG_DATA_N(dd
, 0);
403 cfg
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
404 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
405 cfg
.src_maxburst
= DST_MAXBURST
;
406 cfg
.dst_maxburst
= DST_MAXBURST
;
409 ret
= dmaengine_slave_config(dd
->dma_lch_in
, &cfg
);
411 dev_err(dd
->dev
, "can't configure IN dmaengine slave: %d\n",
416 tx_in
= dmaengine_prep_slave_sg(dd
->dma_lch_in
, in_sg
, in_sg_len
,
418 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
420 dev_err(dd
->dev
, "IN prep_slave_sg() failed\n");
424 /* No callback necessary */
425 tx_in
->callback_param
= dd
;
428 ret
= dmaengine_slave_config(dd
->dma_lch_out
, &cfg
);
430 dev_err(dd
->dev
, "can't configure OUT dmaengine slave: %d\n",
435 tx_out
= dmaengine_prep_slave_sg(dd
->dma_lch_out
, out_sg
, out_sg_len
,
437 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
439 dev_err(dd
->dev
, "OUT prep_slave_sg() failed\n");
443 tx_out
->callback
= omap_des_dma_out_callback
;
444 tx_out
->callback_param
= dd
;
446 dmaengine_submit(tx_in
);
447 dmaengine_submit(tx_out
);
449 dma_async_issue_pending(dd
->dma_lch_in
);
450 dma_async_issue_pending(dd
->dma_lch_out
);
453 dd
->pdata
->trigger(dd
, dd
->total
);
458 static int omap_des_crypt_dma_start(struct omap_des_dev
*dd
)
460 struct crypto_tfm
*tfm
= crypto_skcipher_tfm(
461 crypto_skcipher_reqtfm(dd
->req
));
464 pr_debug("total: %zd\n", dd
->total
);
467 err
= dma_map_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
,
470 dev_err(dd
->dev
, "dma_map_sg() error\n");
474 err
= dma_map_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
477 dev_err(dd
->dev
, "dma_map_sg() error\n");
482 err
= omap_des_crypt_dma(tfm
, dd
->in_sg
, dd
->out_sg
, dd
->in_sg_len
,
484 if (err
&& !dd
->pio_only
) {
485 dma_unmap_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
, DMA_TO_DEVICE
);
486 dma_unmap_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
493 static void omap_des_finish_req(struct omap_des_dev
*dd
, int err
)
495 struct skcipher_request
*req
= dd
->req
;
497 pr_debug("err: %d\n", err
);
499 crypto_finalize_skcipher_request(dd
->engine
, req
, err
);
501 pm_runtime_mark_last_busy(dd
->dev
);
502 pm_runtime_put_autosuspend(dd
->dev
);
505 static int omap_des_crypt_dma_stop(struct omap_des_dev
*dd
)
507 pr_debug("total: %zd\n", dd
->total
);
509 omap_des_dma_stop(dd
);
511 dmaengine_terminate_all(dd
->dma_lch_in
);
512 dmaengine_terminate_all(dd
->dma_lch_out
);
517 static int omap_des_handle_queue(struct omap_des_dev
*dd
,
518 struct skcipher_request
*req
)
521 return crypto_transfer_skcipher_request_to_engine(dd
->engine
, req
);
526 static int omap_des_prepare_req(struct crypto_engine
*engine
,
529 struct skcipher_request
*req
= container_of(areq
, struct skcipher_request
, base
);
530 struct omap_des_ctx
*ctx
= crypto_skcipher_ctx(
531 crypto_skcipher_reqtfm(req
));
532 struct omap_des_dev
*dd
= omap_des_find_dev(ctx
);
533 struct omap_des_reqctx
*rctx
;
540 /* assign new request to device */
542 dd
->total
= req
->cryptlen
;
543 dd
->total_save
= req
->cryptlen
;
544 dd
->in_sg
= req
->src
;
545 dd
->out_sg
= req
->dst
;
546 dd
->orig_out
= req
->dst
;
548 flags
= OMAP_CRYPTO_COPY_DATA
;
549 if (req
->src
== req
->dst
)
550 flags
|= OMAP_CRYPTO_FORCE_COPY
;
552 ret
= omap_crypto_align_sg(&dd
->in_sg
, dd
->total
, DES_BLOCK_SIZE
,
554 FLAGS_IN_DATA_ST_SHIFT
, &dd
->flags
);
558 ret
= omap_crypto_align_sg(&dd
->out_sg
, dd
->total
, DES_BLOCK_SIZE
,
560 FLAGS_OUT_DATA_ST_SHIFT
, &dd
->flags
);
564 dd
->in_sg_len
= sg_nents_for_len(dd
->in_sg
, dd
->total
);
565 if (dd
->in_sg_len
< 0)
566 return dd
->in_sg_len
;
568 dd
->out_sg_len
= sg_nents_for_len(dd
->out_sg
, dd
->total
);
569 if (dd
->out_sg_len
< 0)
570 return dd
->out_sg_len
;
572 rctx
= skcipher_request_ctx(req
);
573 ctx
= crypto_skcipher_ctx(crypto_skcipher_reqtfm(req
));
574 rctx
->mode
&= FLAGS_MODE_MASK
;
575 dd
->flags
= (dd
->flags
& ~FLAGS_MODE_MASK
) | rctx
->mode
;
580 return omap_des_write_ctrl(dd
);
583 static int omap_des_crypt_req(struct crypto_engine
*engine
,
586 struct skcipher_request
*req
= container_of(areq
, struct skcipher_request
, base
);
587 struct omap_des_ctx
*ctx
= crypto_skcipher_ctx(
588 crypto_skcipher_reqtfm(req
));
589 struct omap_des_dev
*dd
= omap_des_find_dev(ctx
);
594 return omap_des_crypt_dma_start(dd
);
597 static void omap_des_done_task(unsigned long data
)
599 struct omap_des_dev
*dd
= (struct omap_des_dev
*)data
;
602 pr_debug("enter done_task\n");
605 dma_sync_sg_for_device(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
607 dma_unmap_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
, DMA_TO_DEVICE
);
608 dma_unmap_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
610 omap_des_crypt_dma_stop(dd
);
613 omap_crypto_cleanup(&dd
->in_sgl
, NULL
, 0, dd
->total_save
,
614 FLAGS_IN_DATA_ST_SHIFT
, dd
->flags
);
616 omap_crypto_cleanup(&dd
->out_sgl
, dd
->orig_out
, 0, dd
->total_save
,
617 FLAGS_OUT_DATA_ST_SHIFT
, dd
->flags
);
619 if ((dd
->flags
& FLAGS_CBC
) && dd
->req
->iv
)
620 for (i
= 0; i
< 2; i
++)
621 ((u32
*)dd
->req
->iv
)[i
] =
622 omap_des_read(dd
, DES_REG_IV(dd
, i
));
624 omap_des_finish_req(dd
, 0);
629 static int omap_des_crypt(struct skcipher_request
*req
, unsigned long mode
)
631 struct omap_des_ctx
*ctx
= crypto_skcipher_ctx(
632 crypto_skcipher_reqtfm(req
));
633 struct omap_des_reqctx
*rctx
= skcipher_request_ctx(req
);
634 struct omap_des_dev
*dd
;
636 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req
->cryptlen
,
637 !!(mode
& FLAGS_ENCRYPT
),
638 !!(mode
& FLAGS_CBC
));
643 if (!IS_ALIGNED(req
->cryptlen
, DES_BLOCK_SIZE
))
646 dd
= omap_des_find_dev(ctx
);
652 return omap_des_handle_queue(dd
, req
);
655 /* ********************** ALG API ************************************ */
657 static int omap_des_setkey(struct crypto_skcipher
*cipher
, const u8
*key
,
660 struct omap_des_ctx
*ctx
= crypto_skcipher_ctx(cipher
);
663 pr_debug("enter, keylen: %d\n", keylen
);
665 err
= verify_skcipher_des_key(cipher
, key
);
669 memcpy(ctx
->key
, key
, keylen
);
670 ctx
->keylen
= keylen
;
675 static int omap_des3_setkey(struct crypto_skcipher
*cipher
, const u8
*key
,
678 struct omap_des_ctx
*ctx
= crypto_skcipher_ctx(cipher
);
681 pr_debug("enter, keylen: %d\n", keylen
);
683 err
= verify_skcipher_des3_key(cipher
, key
);
687 memcpy(ctx
->key
, key
, keylen
);
688 ctx
->keylen
= keylen
;
693 static int omap_des_ecb_encrypt(struct skcipher_request
*req
)
695 return omap_des_crypt(req
, FLAGS_ENCRYPT
);
698 static int omap_des_ecb_decrypt(struct skcipher_request
*req
)
700 return omap_des_crypt(req
, 0);
703 static int omap_des_cbc_encrypt(struct skcipher_request
*req
)
705 return omap_des_crypt(req
, FLAGS_ENCRYPT
| FLAGS_CBC
);
708 static int omap_des_cbc_decrypt(struct skcipher_request
*req
)
710 return omap_des_crypt(req
, FLAGS_CBC
);
713 static int omap_des_prepare_req(struct crypto_engine
*engine
,
715 static int omap_des_crypt_req(struct crypto_engine
*engine
,
718 static int omap_des_init_tfm(struct crypto_skcipher
*tfm
)
720 struct omap_des_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
724 crypto_skcipher_set_reqsize(tfm
, sizeof(struct omap_des_reqctx
));
726 ctx
->enginectx
.op
.prepare_request
= omap_des_prepare_req
;
727 ctx
->enginectx
.op
.unprepare_request
= NULL
;
728 ctx
->enginectx
.op
.do_one_request
= omap_des_crypt_req
;
733 /* ********************** ALGS ************************************ */
735 static struct skcipher_alg algs_ecb_cbc
[] = {
737 .base
.cra_name
= "ecb(des)",
738 .base
.cra_driver_name
= "ecb-des-omap",
739 .base
.cra_priority
= 100,
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 .setkey
= omap_des_setkey
,
749 .encrypt
= omap_des_ecb_encrypt
,
750 .decrypt
= omap_des_ecb_decrypt
,
751 .init
= omap_des_init_tfm
,
754 .base
.cra_name
= "cbc(des)",
755 .base
.cra_driver_name
= "cbc-des-omap",
756 .base
.cra_priority
= 100,
757 .base
.cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
|
759 .base
.cra_blocksize
= DES_BLOCK_SIZE
,
760 .base
.cra_ctxsize
= sizeof(struct omap_des_ctx
),
761 .base
.cra_module
= THIS_MODULE
,
763 .min_keysize
= DES_KEY_SIZE
,
764 .max_keysize
= DES_KEY_SIZE
,
765 .ivsize
= DES_BLOCK_SIZE
,
766 .setkey
= omap_des_setkey
,
767 .encrypt
= omap_des_cbc_encrypt
,
768 .decrypt
= omap_des_cbc_decrypt
,
769 .init
= omap_des_init_tfm
,
772 .base
.cra_name
= "ecb(des3_ede)",
773 .base
.cra_driver_name
= "ecb-des3-omap",
774 .base
.cra_priority
= 100,
775 .base
.cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
|
777 .base
.cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
778 .base
.cra_ctxsize
= sizeof(struct omap_des_ctx
),
779 .base
.cra_module
= THIS_MODULE
,
781 .min_keysize
= DES3_EDE_KEY_SIZE
,
782 .max_keysize
= DES3_EDE_KEY_SIZE
,
783 .setkey
= omap_des3_setkey
,
784 .encrypt
= omap_des_ecb_encrypt
,
785 .decrypt
= omap_des_ecb_decrypt
,
786 .init
= omap_des_init_tfm
,
789 .base
.cra_name
= "cbc(des3_ede)",
790 .base
.cra_driver_name
= "cbc-des3-omap",
791 .base
.cra_priority
= 100,
792 .base
.cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
|
794 .base
.cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
795 .base
.cra_ctxsize
= sizeof(struct omap_des_ctx
),
796 .base
.cra_module
= THIS_MODULE
,
798 .min_keysize
= DES3_EDE_KEY_SIZE
,
799 .max_keysize
= DES3_EDE_KEY_SIZE
,
800 .ivsize
= DES3_EDE_BLOCK_SIZE
,
801 .setkey
= omap_des3_setkey
,
802 .encrypt
= omap_des_cbc_encrypt
,
803 .decrypt
= omap_des_cbc_decrypt
,
804 .init
= omap_des_init_tfm
,
808 static struct omap_des_algs_info omap_des_algs_info_ecb_cbc
[] = {
810 .algs_list
= algs_ecb_cbc
,
811 .size
= ARRAY_SIZE(algs_ecb_cbc
),
816 static const struct omap_des_pdata omap_des_pdata_omap4
= {
817 .algs_info
= omap_des_algs_info_ecb_cbc
,
818 .algs_info_size
= ARRAY_SIZE(omap_des_algs_info_ecb_cbc
),
819 .trigger
= omap_des_dma_trigger_omap4
,
826 .irq_status_ofs
= 0x3c,
827 .irq_enable_ofs
= 0x40,
828 .dma_enable_in
= BIT(5),
829 .dma_enable_out
= BIT(6),
830 .major_mask
= 0x0700,
832 .minor_mask
= 0x003f,
836 static irqreturn_t
omap_des_irq(int irq
, void *dev_id
)
838 struct omap_des_dev
*dd
= dev_id
;
842 status
= omap_des_read(dd
, DES_REG_IRQ_STATUS(dd
));
843 if (status
& DES_REG_IRQ_DATA_IN
) {
844 omap_des_write(dd
, DES_REG_IRQ_ENABLE(dd
), 0x0);
848 BUG_ON(_calc_walked(in
) > dd
->in_sg
->length
);
850 src
= sg_virt(dd
->in_sg
) + _calc_walked(in
);
852 for (i
= 0; i
< DES_BLOCK_WORDS
; i
++) {
853 omap_des_write(dd
, DES_REG_DATA_N(dd
, i
), *src
);
855 scatterwalk_advance(&dd
->in_walk
, 4);
856 if (dd
->in_sg
->length
== _calc_walked(in
)) {
857 dd
->in_sg
= sg_next(dd
->in_sg
);
859 scatterwalk_start(&dd
->in_walk
,
861 src
= sg_virt(dd
->in_sg
) +
869 /* Clear IRQ status */
870 status
&= ~DES_REG_IRQ_DATA_IN
;
871 omap_des_write(dd
, DES_REG_IRQ_STATUS(dd
), status
);
873 /* Enable DATA_OUT interrupt */
874 omap_des_write(dd
, DES_REG_IRQ_ENABLE(dd
), 0x4);
876 } else if (status
& DES_REG_IRQ_DATA_OUT
) {
877 omap_des_write(dd
, DES_REG_IRQ_ENABLE(dd
), 0x0);
881 BUG_ON(_calc_walked(out
) > dd
->out_sg
->length
);
883 dst
= sg_virt(dd
->out_sg
) + _calc_walked(out
);
885 for (i
= 0; i
< DES_BLOCK_WORDS
; i
++) {
886 *dst
= omap_des_read(dd
, DES_REG_DATA_N(dd
, i
));
887 scatterwalk_advance(&dd
->out_walk
, 4);
888 if (dd
->out_sg
->length
== _calc_walked(out
)) {
889 dd
->out_sg
= sg_next(dd
->out_sg
);
891 scatterwalk_start(&dd
->out_walk
,
893 dst
= sg_virt(dd
->out_sg
) +
901 BUG_ON(dd
->total
< DES_BLOCK_SIZE
);
903 dd
->total
-= DES_BLOCK_SIZE
;
905 /* Clear IRQ status */
906 status
&= ~DES_REG_IRQ_DATA_OUT
;
907 omap_des_write(dd
, DES_REG_IRQ_STATUS(dd
), status
);
910 /* All bytes read! */
911 tasklet_schedule(&dd
->done_task
);
913 /* Enable DATA_IN interrupt for next block */
914 omap_des_write(dd
, DES_REG_IRQ_ENABLE(dd
), 0x2);
920 static const struct of_device_id omap_des_of_match
[] = {
922 .compatible
= "ti,omap4-des",
923 .data
= &omap_des_pdata_omap4
,
927 MODULE_DEVICE_TABLE(of
, omap_des_of_match
);
929 static int omap_des_get_of(struct omap_des_dev
*dd
,
930 struct platform_device
*pdev
)
933 dd
->pdata
= of_device_get_match_data(&pdev
->dev
);
935 dev_err(&pdev
->dev
, "no compatible OF match\n");
942 static int omap_des_get_of(struct omap_des_dev
*dd
,
949 static int omap_des_get_pdev(struct omap_des_dev
*dd
,
950 struct platform_device
*pdev
)
952 /* non-DT devices get pdata from pdev */
953 dd
->pdata
= pdev
->dev
.platform_data
;
958 static int omap_des_probe(struct platform_device
*pdev
)
960 struct device
*dev
= &pdev
->dev
;
961 struct omap_des_dev
*dd
;
962 struct skcipher_alg
*algp
;
963 struct resource
*res
;
964 int err
= -ENOMEM
, i
, j
, irq
= -1;
967 dd
= devm_kzalloc(dev
, sizeof(struct omap_des_dev
), GFP_KERNEL
);
969 dev_err(dev
, "unable to alloc data struct.\n");
973 platform_set_drvdata(pdev
, dd
);
975 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
977 dev_err(dev
, "no MEM resource info\n");
981 err
= (dev
->of_node
) ? omap_des_get_of(dd
, pdev
) :
982 omap_des_get_pdev(dd
, pdev
);
986 dd
->io_base
= devm_ioremap_resource(dev
, res
);
987 if (IS_ERR(dd
->io_base
)) {
988 err
= PTR_ERR(dd
->io_base
);
991 dd
->phys_base
= res
->start
;
993 pm_runtime_use_autosuspend(dev
);
994 pm_runtime_set_autosuspend_delay(dev
, DEFAULT_AUTOSUSPEND_DELAY
);
996 pm_runtime_enable(dev
);
997 err
= pm_runtime_get_sync(dev
);
999 pm_runtime_put_noidle(dev
);
1000 dev_err(dd
->dev
, "%s: failed to get_sync(%d)\n", __func__
, err
);
1004 omap_des_dma_stop(dd
);
1006 reg
= omap_des_read(dd
, DES_REG_REV(dd
));
1008 pm_runtime_put_sync(dev
);
1010 dev_info(dev
, "OMAP DES hw accel rev: %u.%u\n",
1011 (reg
& dd
->pdata
->major_mask
) >> dd
->pdata
->major_shift
,
1012 (reg
& dd
->pdata
->minor_mask
) >> dd
->pdata
->minor_shift
);
1014 tasklet_init(&dd
->done_task
, omap_des_done_task
, (unsigned long)dd
);
1016 err
= omap_des_dma_init(dd
);
1017 if (err
== -EPROBE_DEFER
) {
1019 } else if (err
&& DES_REG_IRQ_STATUS(dd
) && DES_REG_IRQ_ENABLE(dd
)) {
1022 irq
= platform_get_irq(pdev
, 0);
1028 err
= devm_request_irq(dev
, irq
, omap_des_irq
, 0,
1031 dev_err(dev
, "Unable to grab omap-des IRQ\n");
1037 INIT_LIST_HEAD(&dd
->list
);
1038 spin_lock(&list_lock
);
1039 list_add_tail(&dd
->list
, &dev_list
);
1040 spin_unlock(&list_lock
);
1042 /* Initialize des crypto engine */
1043 dd
->engine
= crypto_engine_alloc_init(dev
, 1);
1049 err
= crypto_engine_start(dd
->engine
);
1053 for (i
= 0; i
< dd
->pdata
->algs_info_size
; i
++) {
1054 for (j
= 0; j
< dd
->pdata
->algs_info
[i
].size
; j
++) {
1055 algp
= &dd
->pdata
->algs_info
[i
].algs_list
[j
];
1057 pr_debug("reg alg: %s\n", algp
->base
.cra_name
);
1059 err
= crypto_register_skcipher(algp
);
1063 dd
->pdata
->algs_info
[i
].registered
++;
1070 for (i
= dd
->pdata
->algs_info_size
- 1; i
>= 0; i
--)
1071 for (j
= dd
->pdata
->algs_info
[i
].registered
- 1; j
>= 0; j
--)
1072 crypto_unregister_skcipher(
1073 &dd
->pdata
->algs_info
[i
].algs_list
[j
]);
1077 crypto_engine_exit(dd
->engine
);
1079 omap_des_dma_cleanup(dd
);
1081 tasklet_kill(&dd
->done_task
);
1083 pm_runtime_disable(dev
);
1087 dev_err(dev
, "initialization failed.\n");
1091 static int omap_des_remove(struct platform_device
*pdev
)
1093 struct omap_des_dev
*dd
= platform_get_drvdata(pdev
);
1099 spin_lock(&list_lock
);
1100 list_del(&dd
->list
);
1101 spin_unlock(&list_lock
);
1103 for (i
= dd
->pdata
->algs_info_size
- 1; i
>= 0; i
--)
1104 for (j
= dd
->pdata
->algs_info
[i
].registered
- 1; j
>= 0; j
--)
1105 crypto_unregister_skcipher(
1106 &dd
->pdata
->algs_info
[i
].algs_list
[j
]);
1108 tasklet_kill(&dd
->done_task
);
1109 omap_des_dma_cleanup(dd
);
1110 pm_runtime_disable(dd
->dev
);
1116 #ifdef CONFIG_PM_SLEEP
1117 static int omap_des_suspend(struct device
*dev
)
1119 pm_runtime_put_sync(dev
);
1123 static int omap_des_resume(struct device
*dev
)
1127 err
= pm_runtime_get_sync(dev
);
1129 pm_runtime_put_noidle(dev
);
1130 dev_err(dev
, "%s: failed to get_sync(%d)\n", __func__
, err
);
1137 static SIMPLE_DEV_PM_OPS(omap_des_pm_ops
, omap_des_suspend
, omap_des_resume
);
1139 static struct platform_driver omap_des_driver
= {
1140 .probe
= omap_des_probe
,
1141 .remove
= omap_des_remove
,
1144 .pm
= &omap_des_pm_ops
,
1145 .of_match_table
= of_match_ptr(omap_des_of_match
),
1149 module_platform_driver(omap_des_driver
);
1151 MODULE_DESCRIPTION("OMAP DES hw acceleration support.");
1152 MODULE_LICENSE("GPL v2");
1153 MODULE_AUTHOR("Joel Fernandes <joelf@ti.com>");