2 * Support for OMAP DES and Triple DES HW acceleration.
4 * Copyright (c) 2013 Texas Instruments Incorporated
5 * Author: Joel Fernandes <joelf@ti.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as published
9 * by the Free Software Foundation.
13 #define pr_fmt(fmt) "%s: " fmt, __func__
16 #define prn(num) printk(#num "=%d\n", num)
17 #define prx(num) printk(#num "=%x\n", num)
19 #define prn(num) do { } while (0)
20 #define prx(num) do { } while (0)
23 #include <linux/err.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/platform_device.h>
29 #include <linux/scatterlist.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/dmaengine.h>
32 #include <linux/pm_runtime.h>
34 #include <linux/of_device.h>
35 #include <linux/of_address.h>
37 #include <linux/crypto.h>
38 #include <linux/interrupt.h>
39 #include <crypto/scatterwalk.h>
40 #include <crypto/des.h>
41 #include <crypto/algapi.h>
42 #include <crypto/engine.h>
44 #define DST_MAXBURST 2
46 #define DES_BLOCK_WORDS (DES_BLOCK_SIZE >> 2)
48 #define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset)
50 #define DES_REG_KEY(dd, x) ((dd)->pdata->key_ofs - \
53 #define DES_REG_IV(dd, x) ((dd)->pdata->iv_ofs + ((x) * 0x04))
55 #define DES_REG_CTRL(dd) ((dd)->pdata->ctrl_ofs)
56 #define DES_REG_CTRL_CBC BIT(4)
57 #define DES_REG_CTRL_TDES BIT(3)
58 #define DES_REG_CTRL_DIRECTION BIT(2)
59 #define DES_REG_CTRL_INPUT_READY BIT(1)
60 #define DES_REG_CTRL_OUTPUT_READY BIT(0)
62 #define DES_REG_DATA_N(dd, x) ((dd)->pdata->data_ofs + ((x) * 0x04))
64 #define DES_REG_REV(dd) ((dd)->pdata->rev_ofs)
66 #define DES_REG_MASK(dd) ((dd)->pdata->mask_ofs)
68 #define DES_REG_LENGTH_N(x) (0x24 + ((x) * 0x04))
70 #define DES_REG_IRQ_STATUS(dd) ((dd)->pdata->irq_status_ofs)
71 #define DES_REG_IRQ_ENABLE(dd) ((dd)->pdata->irq_enable_ofs)
72 #define DES_REG_IRQ_DATA_IN BIT(1)
73 #define DES_REG_IRQ_DATA_OUT BIT(2)
75 #define FLAGS_MODE_MASK 0x000f
76 #define FLAGS_ENCRYPT BIT(0)
77 #define FLAGS_CBC BIT(1)
78 #define FLAGS_INIT BIT(4)
79 #define FLAGS_BUSY BIT(6)
82 struct omap_des_dev
*dd
;
85 u32 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 crypto_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 ablkcipher_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
;
156 struct scatter_walk in_walk
;
157 struct scatter_walk out_walk
;
158 struct dma_chan
*dma_lch_in
;
159 struct dma_chan
*dma_lch_out
;
163 const struct omap_des_pdata
*pdata
;
166 /* keep registered devices data here */
167 static LIST_HEAD(dev_list
);
168 static DEFINE_SPINLOCK(list_lock
);
171 #define omap_des_read(dd, offset) \
174 _read_ret = __raw_readl(dd->io_base + offset); \
175 pr_err("omap_des_read(" #offset "=%#x)= %#x\n", \
176 offset, _read_ret); \
180 static inline u32
omap_des_read(struct omap_des_dev
*dd
, u32 offset
)
182 return __raw_readl(dd
->io_base
+ offset
);
187 #define omap_des_write(dd, offset, value) \
189 pr_err("omap_des_write(" #offset "=%#x) value=%#x\n", \
191 __raw_writel(value, dd->io_base + offset); \
194 static inline void omap_des_write(struct omap_des_dev
*dd
, u32 offset
,
197 __raw_writel(value
, dd
->io_base
+ offset
);
201 static inline void omap_des_write_mask(struct omap_des_dev
*dd
, u32 offset
,
206 val
= omap_des_read(dd
, offset
);
209 omap_des_write(dd
, offset
, val
);
212 static void omap_des_write_n(struct omap_des_dev
*dd
, u32 offset
,
213 u32
*value
, int count
)
215 for (; count
--; value
++, offset
+= 4)
216 omap_des_write(dd
, offset
, *value
);
219 static int omap_des_hw_init(struct omap_des_dev
*dd
)
224 * clocks are enabled when request starts and disabled when finished.
225 * It may be long delays between requests.
226 * Device might go to off mode to save power.
228 err
= pm_runtime_get_sync(dd
->dev
);
230 pm_runtime_put_noidle(dd
->dev
);
231 dev_err(dd
->dev
, "%s: failed to get_sync(%d)\n", __func__
, err
);
235 if (!(dd
->flags
& FLAGS_INIT
)) {
236 dd
->flags
|= FLAGS_INIT
;
243 static int omap_des_write_ctrl(struct omap_des_dev
*dd
)
247 u32 val
= 0, mask
= 0;
249 err
= omap_des_hw_init(dd
);
253 key32
= dd
->ctx
->keylen
/ sizeof(u32
);
255 /* it seems a key should always be set even if it has not changed */
256 for (i
= 0; i
< key32
; i
++) {
257 omap_des_write(dd
, DES_REG_KEY(dd
, i
),
258 __le32_to_cpu(dd
->ctx
->key
[i
]));
261 if ((dd
->flags
& FLAGS_CBC
) && dd
->req
->info
)
262 omap_des_write_n(dd
, DES_REG_IV(dd
, 0), dd
->req
->info
, 2);
264 if (dd
->flags
& FLAGS_CBC
)
265 val
|= DES_REG_CTRL_CBC
;
266 if (dd
->flags
& FLAGS_ENCRYPT
)
267 val
|= DES_REG_CTRL_DIRECTION
;
269 val
|= DES_REG_CTRL_TDES
;
271 mask
|= DES_REG_CTRL_CBC
| DES_REG_CTRL_DIRECTION
| DES_REG_CTRL_TDES
;
273 omap_des_write_mask(dd
, DES_REG_CTRL(dd
), val
, mask
);
278 static void omap_des_dma_trigger_omap4(struct omap_des_dev
*dd
, int length
)
282 omap_des_write(dd
, DES_REG_LENGTH_N(0), length
);
284 val
= dd
->pdata
->dma_start
;
286 if (dd
->dma_lch_out
!= NULL
)
287 val
|= dd
->pdata
->dma_enable_out
;
288 if (dd
->dma_lch_in
!= NULL
)
289 val
|= dd
->pdata
->dma_enable_in
;
291 mask
= dd
->pdata
->dma_enable_out
| dd
->pdata
->dma_enable_in
|
292 dd
->pdata
->dma_start
;
294 omap_des_write_mask(dd
, DES_REG_MASK(dd
), val
, mask
);
297 static void omap_des_dma_stop(struct omap_des_dev
*dd
)
301 mask
= dd
->pdata
->dma_enable_out
| dd
->pdata
->dma_enable_in
|
302 dd
->pdata
->dma_start
;
304 omap_des_write_mask(dd
, DES_REG_MASK(dd
), 0, mask
);
307 static struct omap_des_dev
*omap_des_find_dev(struct omap_des_ctx
*ctx
)
309 struct omap_des_dev
*dd
= NULL
, *tmp
;
311 spin_lock_bh(&list_lock
);
313 list_for_each_entry(tmp
, &dev_list
, list
) {
314 /* FIXME: take fist available des core */
320 /* already found before */
323 spin_unlock_bh(&list_lock
);
328 static void omap_des_dma_out_callback(void *data
)
330 struct omap_des_dev
*dd
= data
;
332 /* dma_lch_out - completed */
333 tasklet_schedule(&dd
->done_task
);
336 static int omap_des_dma_init(struct omap_des_dev
*dd
)
340 dd
->dma_lch_out
= NULL
;
341 dd
->dma_lch_in
= NULL
;
343 dd
->dma_lch_in
= dma_request_chan(dd
->dev
, "rx");
344 if (IS_ERR(dd
->dma_lch_in
)) {
345 dev_err(dd
->dev
, "Unable to request in DMA channel\n");
346 return PTR_ERR(dd
->dma_lch_in
);
349 dd
->dma_lch_out
= dma_request_chan(dd
->dev
, "tx");
350 if (IS_ERR(dd
->dma_lch_out
)) {
351 dev_err(dd
->dev
, "Unable to request out DMA channel\n");
352 err
= PTR_ERR(dd
->dma_lch_out
);
359 dma_release_channel(dd
->dma_lch_in
);
364 static void omap_des_dma_cleanup(struct omap_des_dev
*dd
)
369 dma_release_channel(dd
->dma_lch_out
);
370 dma_release_channel(dd
->dma_lch_in
);
373 static void sg_copy_buf(void *buf
, struct scatterlist
*sg
,
374 unsigned int start
, unsigned int nbytes
, int out
)
376 struct scatter_walk walk
;
381 scatterwalk_start(&walk
, sg
);
382 scatterwalk_advance(&walk
, start
);
383 scatterwalk_copychunks(buf
, &walk
, nbytes
, out
);
384 scatterwalk_done(&walk
, out
, 0);
387 static int omap_des_crypt_dma(struct crypto_tfm
*tfm
,
388 struct scatterlist
*in_sg
, struct scatterlist
*out_sg
,
389 int in_sg_len
, int out_sg_len
)
391 struct omap_des_ctx
*ctx
= crypto_tfm_ctx(tfm
);
392 struct omap_des_dev
*dd
= ctx
->dd
;
393 struct dma_async_tx_descriptor
*tx_in
, *tx_out
;
394 struct dma_slave_config cfg
;
398 scatterwalk_start(&dd
->in_walk
, dd
->in_sg
);
399 scatterwalk_start(&dd
->out_walk
, dd
->out_sg
);
401 /* Enable DATAIN interrupt and let it take
403 omap_des_write(dd
, DES_REG_IRQ_ENABLE(dd
), 0x2);
407 dma_sync_sg_for_device(dd
->dev
, dd
->in_sg
, in_sg_len
, DMA_TO_DEVICE
);
409 memset(&cfg
, 0, sizeof(cfg
));
411 cfg
.src_addr
= dd
->phys_base
+ DES_REG_DATA_N(dd
, 0);
412 cfg
.dst_addr
= dd
->phys_base
+ DES_REG_DATA_N(dd
, 0);
413 cfg
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
414 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
415 cfg
.src_maxburst
= DST_MAXBURST
;
416 cfg
.dst_maxburst
= DST_MAXBURST
;
419 ret
= dmaengine_slave_config(dd
->dma_lch_in
, &cfg
);
421 dev_err(dd
->dev
, "can't configure IN dmaengine slave: %d\n",
426 tx_in
= dmaengine_prep_slave_sg(dd
->dma_lch_in
, in_sg
, in_sg_len
,
428 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
430 dev_err(dd
->dev
, "IN prep_slave_sg() failed\n");
434 /* No callback necessary */
435 tx_in
->callback_param
= dd
;
438 ret
= dmaengine_slave_config(dd
->dma_lch_out
, &cfg
);
440 dev_err(dd
->dev
, "can't configure OUT dmaengine slave: %d\n",
445 tx_out
= dmaengine_prep_slave_sg(dd
->dma_lch_out
, out_sg
, out_sg_len
,
447 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
449 dev_err(dd
->dev
, "OUT prep_slave_sg() failed\n");
453 tx_out
->callback
= omap_des_dma_out_callback
;
454 tx_out
->callback_param
= dd
;
456 dmaengine_submit(tx_in
);
457 dmaengine_submit(tx_out
);
459 dma_async_issue_pending(dd
->dma_lch_in
);
460 dma_async_issue_pending(dd
->dma_lch_out
);
463 dd
->pdata
->trigger(dd
, dd
->total
);
468 static int omap_des_crypt_dma_start(struct omap_des_dev
*dd
)
470 struct crypto_tfm
*tfm
= crypto_ablkcipher_tfm(
471 crypto_ablkcipher_reqtfm(dd
->req
));
474 pr_debug("total: %d\n", dd
->total
);
477 err
= dma_map_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
,
480 dev_err(dd
->dev
, "dma_map_sg() error\n");
484 err
= dma_map_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
487 dev_err(dd
->dev
, "dma_map_sg() error\n");
492 err
= omap_des_crypt_dma(tfm
, dd
->in_sg
, dd
->out_sg
, dd
->in_sg_len
,
494 if (err
&& !dd
->pio_only
) {
495 dma_unmap_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
, DMA_TO_DEVICE
);
496 dma_unmap_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
503 static void omap_des_finish_req(struct omap_des_dev
*dd
, int err
)
505 struct ablkcipher_request
*req
= dd
->req
;
507 pr_debug("err: %d\n", err
);
509 pm_runtime_put(dd
->dev
);
510 crypto_finalize_cipher_request(dd
->engine
, req
, err
);
513 static int omap_des_crypt_dma_stop(struct omap_des_dev
*dd
)
515 pr_debug("total: %d\n", dd
->total
);
517 omap_des_dma_stop(dd
);
519 dmaengine_terminate_all(dd
->dma_lch_in
);
520 dmaengine_terminate_all(dd
->dma_lch_out
);
525 static int omap_des_copy_needed(struct scatterlist
*sg
)
528 if (!IS_ALIGNED(sg
->offset
, 4))
530 if (!IS_ALIGNED(sg
->length
, DES_BLOCK_SIZE
))
537 static int omap_des_copy_sgs(struct omap_des_dev
*dd
)
539 void *buf_in
, *buf_out
;
542 pages
= dd
->total
>> PAGE_SHIFT
;
544 if (dd
->total
& (PAGE_SIZE
-1))
549 buf_in
= (void *)__get_free_pages(GFP_ATOMIC
, pages
);
550 buf_out
= (void *)__get_free_pages(GFP_ATOMIC
, pages
);
552 if (!buf_in
|| !buf_out
) {
553 pr_err("Couldn't allocated pages for unaligned cases.\n");
557 dd
->orig_out
= dd
->out_sg
;
559 sg_copy_buf(buf_in
, dd
->in_sg
, 0, dd
->total
, 0);
561 sg_init_table(&dd
->in_sgl
, 1);
562 sg_set_buf(&dd
->in_sgl
, buf_in
, dd
->total
);
563 dd
->in_sg
= &dd
->in_sgl
;
566 sg_init_table(&dd
->out_sgl
, 1);
567 sg_set_buf(&dd
->out_sgl
, buf_out
, dd
->total
);
568 dd
->out_sg
= &dd
->out_sgl
;
574 static int omap_des_handle_queue(struct omap_des_dev
*dd
,
575 struct ablkcipher_request
*req
)
578 return crypto_transfer_cipher_request_to_engine(dd
->engine
, req
);
583 static int omap_des_prepare_req(struct crypto_engine
*engine
,
584 struct ablkcipher_request
*req
)
586 struct omap_des_ctx
*ctx
= crypto_ablkcipher_ctx(
587 crypto_ablkcipher_reqtfm(req
));
588 struct omap_des_dev
*dd
= omap_des_find_dev(ctx
);
589 struct omap_des_reqctx
*rctx
;
594 /* assign new request to device */
596 dd
->total
= req
->nbytes
;
597 dd
->total_save
= req
->nbytes
;
598 dd
->in_sg
= req
->src
;
599 dd
->out_sg
= req
->dst
;
601 dd
->in_sg_len
= sg_nents_for_len(dd
->in_sg
, dd
->total
);
602 if (dd
->in_sg_len
< 0)
603 return dd
->in_sg_len
;
605 dd
->out_sg_len
= sg_nents_for_len(dd
->out_sg
, dd
->total
);
606 if (dd
->out_sg_len
< 0)
607 return dd
->out_sg_len
;
609 if (omap_des_copy_needed(dd
->in_sg
) ||
610 omap_des_copy_needed(dd
->out_sg
)) {
611 if (omap_des_copy_sgs(dd
))
612 pr_err("Failed to copy SGs for unaligned cases\n");
618 rctx
= ablkcipher_request_ctx(req
);
619 ctx
= crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req
));
620 rctx
->mode
&= FLAGS_MODE_MASK
;
621 dd
->flags
= (dd
->flags
& ~FLAGS_MODE_MASK
) | rctx
->mode
;
626 return omap_des_write_ctrl(dd
);
629 static int omap_des_crypt_req(struct crypto_engine
*engine
,
630 struct ablkcipher_request
*req
)
632 struct omap_des_ctx
*ctx
= crypto_ablkcipher_ctx(
633 crypto_ablkcipher_reqtfm(req
));
634 struct omap_des_dev
*dd
= omap_des_find_dev(ctx
);
639 return omap_des_crypt_dma_start(dd
);
642 static void omap_des_done_task(unsigned long data
)
644 struct omap_des_dev
*dd
= (struct omap_des_dev
*)data
;
645 void *buf_in
, *buf_out
;
648 pr_debug("enter done_task\n");
651 dma_sync_sg_for_device(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
653 dma_unmap_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
, DMA_TO_DEVICE
);
654 dma_unmap_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
656 omap_des_crypt_dma_stop(dd
);
659 if (dd
->sgs_copied
) {
660 buf_in
= sg_virt(&dd
->in_sgl
);
661 buf_out
= sg_virt(&dd
->out_sgl
);
663 sg_copy_buf(buf_out
, dd
->orig_out
, 0, dd
->total_save
, 1);
665 pages
= get_order(dd
->total_save
);
666 free_pages((unsigned long)buf_in
, pages
);
667 free_pages((unsigned long)buf_out
, pages
);
670 omap_des_finish_req(dd
, 0);
675 static int omap_des_crypt(struct ablkcipher_request
*req
, unsigned long mode
)
677 struct omap_des_ctx
*ctx
= crypto_ablkcipher_ctx(
678 crypto_ablkcipher_reqtfm(req
));
679 struct omap_des_reqctx
*rctx
= ablkcipher_request_ctx(req
);
680 struct omap_des_dev
*dd
;
682 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req
->nbytes
,
683 !!(mode
& FLAGS_ENCRYPT
),
684 !!(mode
& FLAGS_CBC
));
686 if (!IS_ALIGNED(req
->nbytes
, DES_BLOCK_SIZE
)) {
687 pr_err("request size is not exact amount of DES blocks\n");
691 dd
= omap_des_find_dev(ctx
);
697 return omap_des_handle_queue(dd
, req
);
700 /* ********************** ALG API ************************************ */
702 static int omap_des_setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
705 struct omap_des_ctx
*ctx
= crypto_ablkcipher_ctx(tfm
);
707 if (keylen
!= DES_KEY_SIZE
&& keylen
!= (3*DES_KEY_SIZE
))
710 pr_debug("enter, keylen: %d\n", keylen
);
712 memcpy(ctx
->key
, key
, keylen
);
713 ctx
->keylen
= keylen
;
718 static int omap_des_ecb_encrypt(struct ablkcipher_request
*req
)
720 return omap_des_crypt(req
, FLAGS_ENCRYPT
);
723 static int omap_des_ecb_decrypt(struct ablkcipher_request
*req
)
725 return omap_des_crypt(req
, 0);
728 static int omap_des_cbc_encrypt(struct ablkcipher_request
*req
)
730 return omap_des_crypt(req
, FLAGS_ENCRYPT
| FLAGS_CBC
);
733 static int omap_des_cbc_decrypt(struct ablkcipher_request
*req
)
735 return omap_des_crypt(req
, FLAGS_CBC
);
738 static int omap_des_cra_init(struct crypto_tfm
*tfm
)
742 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct omap_des_reqctx
);
747 static void omap_des_cra_exit(struct crypto_tfm
*tfm
)
752 /* ********************** ALGS ************************************ */
754 static struct crypto_alg algs_ecb_cbc
[] = {
756 .cra_name
= "ecb(des)",
757 .cra_driver_name
= "ecb-des-omap",
759 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
760 CRYPTO_ALG_KERN_DRIVER_ONLY
|
762 .cra_blocksize
= DES_BLOCK_SIZE
,
763 .cra_ctxsize
= sizeof(struct omap_des_ctx
),
765 .cra_type
= &crypto_ablkcipher_type
,
766 .cra_module
= THIS_MODULE
,
767 .cra_init
= omap_des_cra_init
,
768 .cra_exit
= omap_des_cra_exit
,
769 .cra_u
.ablkcipher
= {
770 .min_keysize
= DES_KEY_SIZE
,
771 .max_keysize
= DES_KEY_SIZE
,
772 .setkey
= omap_des_setkey
,
773 .encrypt
= omap_des_ecb_encrypt
,
774 .decrypt
= omap_des_ecb_decrypt
,
778 .cra_name
= "cbc(des)",
779 .cra_driver_name
= "cbc-des-omap",
781 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
782 CRYPTO_ALG_KERN_DRIVER_ONLY
|
784 .cra_blocksize
= DES_BLOCK_SIZE
,
785 .cra_ctxsize
= sizeof(struct omap_des_ctx
),
787 .cra_type
= &crypto_ablkcipher_type
,
788 .cra_module
= THIS_MODULE
,
789 .cra_init
= omap_des_cra_init
,
790 .cra_exit
= omap_des_cra_exit
,
791 .cra_u
.ablkcipher
= {
792 .min_keysize
= DES_KEY_SIZE
,
793 .max_keysize
= DES_KEY_SIZE
,
794 .ivsize
= DES_BLOCK_SIZE
,
795 .setkey
= omap_des_setkey
,
796 .encrypt
= omap_des_cbc_encrypt
,
797 .decrypt
= omap_des_cbc_decrypt
,
801 .cra_name
= "ecb(des3_ede)",
802 .cra_driver_name
= "ecb-des3-omap",
804 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
805 CRYPTO_ALG_KERN_DRIVER_ONLY
|
807 .cra_blocksize
= DES_BLOCK_SIZE
,
808 .cra_ctxsize
= sizeof(struct omap_des_ctx
),
810 .cra_type
= &crypto_ablkcipher_type
,
811 .cra_module
= THIS_MODULE
,
812 .cra_init
= omap_des_cra_init
,
813 .cra_exit
= omap_des_cra_exit
,
814 .cra_u
.ablkcipher
= {
815 .min_keysize
= 3*DES_KEY_SIZE
,
816 .max_keysize
= 3*DES_KEY_SIZE
,
817 .setkey
= omap_des_setkey
,
818 .encrypt
= omap_des_ecb_encrypt
,
819 .decrypt
= omap_des_ecb_decrypt
,
823 .cra_name
= "cbc(des3_ede)",
824 .cra_driver_name
= "cbc-des3-omap",
826 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
827 CRYPTO_ALG_KERN_DRIVER_ONLY
|
829 .cra_blocksize
= DES_BLOCK_SIZE
,
830 .cra_ctxsize
= sizeof(struct omap_des_ctx
),
832 .cra_type
= &crypto_ablkcipher_type
,
833 .cra_module
= THIS_MODULE
,
834 .cra_init
= omap_des_cra_init
,
835 .cra_exit
= omap_des_cra_exit
,
836 .cra_u
.ablkcipher
= {
837 .min_keysize
= 3*DES_KEY_SIZE
,
838 .max_keysize
= 3*DES_KEY_SIZE
,
839 .ivsize
= DES_BLOCK_SIZE
,
840 .setkey
= omap_des_setkey
,
841 .encrypt
= omap_des_cbc_encrypt
,
842 .decrypt
= omap_des_cbc_decrypt
,
847 static struct omap_des_algs_info omap_des_algs_info_ecb_cbc
[] = {
849 .algs_list
= algs_ecb_cbc
,
850 .size
= ARRAY_SIZE(algs_ecb_cbc
),
855 static const struct omap_des_pdata omap_des_pdata_omap4
= {
856 .algs_info
= omap_des_algs_info_ecb_cbc
,
857 .algs_info_size
= ARRAY_SIZE(omap_des_algs_info_ecb_cbc
),
858 .trigger
= omap_des_dma_trigger_omap4
,
865 .irq_status_ofs
= 0x3c,
866 .irq_enable_ofs
= 0x40,
867 .dma_enable_in
= BIT(5),
868 .dma_enable_out
= BIT(6),
869 .major_mask
= 0x0700,
871 .minor_mask
= 0x003f,
875 static irqreturn_t
omap_des_irq(int irq
, void *dev_id
)
877 struct omap_des_dev
*dd
= dev_id
;
881 status
= omap_des_read(dd
, DES_REG_IRQ_STATUS(dd
));
882 if (status
& DES_REG_IRQ_DATA_IN
) {
883 omap_des_write(dd
, DES_REG_IRQ_ENABLE(dd
), 0x0);
887 BUG_ON(_calc_walked(in
) > dd
->in_sg
->length
);
889 src
= sg_virt(dd
->in_sg
) + _calc_walked(in
);
891 for (i
= 0; i
< DES_BLOCK_WORDS
; i
++) {
892 omap_des_write(dd
, DES_REG_DATA_N(dd
, i
), *src
);
894 scatterwalk_advance(&dd
->in_walk
, 4);
895 if (dd
->in_sg
->length
== _calc_walked(in
)) {
896 dd
->in_sg
= sg_next(dd
->in_sg
);
898 scatterwalk_start(&dd
->in_walk
,
900 src
= sg_virt(dd
->in_sg
) +
908 /* Clear IRQ status */
909 status
&= ~DES_REG_IRQ_DATA_IN
;
910 omap_des_write(dd
, DES_REG_IRQ_STATUS(dd
), status
);
912 /* Enable DATA_OUT interrupt */
913 omap_des_write(dd
, DES_REG_IRQ_ENABLE(dd
), 0x4);
915 } else if (status
& DES_REG_IRQ_DATA_OUT
) {
916 omap_des_write(dd
, DES_REG_IRQ_ENABLE(dd
), 0x0);
920 BUG_ON(_calc_walked(out
) > dd
->out_sg
->length
);
922 dst
= sg_virt(dd
->out_sg
) + _calc_walked(out
);
924 for (i
= 0; i
< DES_BLOCK_WORDS
; i
++) {
925 *dst
= omap_des_read(dd
, DES_REG_DATA_N(dd
, i
));
926 scatterwalk_advance(&dd
->out_walk
, 4);
927 if (dd
->out_sg
->length
== _calc_walked(out
)) {
928 dd
->out_sg
= sg_next(dd
->out_sg
);
930 scatterwalk_start(&dd
->out_walk
,
932 dst
= sg_virt(dd
->out_sg
) +
940 BUG_ON(dd
->total
< DES_BLOCK_SIZE
);
942 dd
->total
-= DES_BLOCK_SIZE
;
944 /* Clear IRQ status */
945 status
&= ~DES_REG_IRQ_DATA_OUT
;
946 omap_des_write(dd
, DES_REG_IRQ_STATUS(dd
), status
);
949 /* All bytes read! */
950 tasklet_schedule(&dd
->done_task
);
952 /* Enable DATA_IN interrupt for next block */
953 omap_des_write(dd
, DES_REG_IRQ_ENABLE(dd
), 0x2);
959 static const struct of_device_id omap_des_of_match
[] = {
961 .compatible
= "ti,omap4-des",
962 .data
= &omap_des_pdata_omap4
,
966 MODULE_DEVICE_TABLE(of
, omap_des_of_match
);
968 static int omap_des_get_of(struct omap_des_dev
*dd
,
969 struct platform_device
*pdev
)
971 const struct of_device_id
*match
;
973 match
= of_match_device(of_match_ptr(omap_des_of_match
), &pdev
->dev
);
975 dev_err(&pdev
->dev
, "no compatible OF match\n");
979 dd
->pdata
= match
->data
;
984 static int omap_des_get_of(struct omap_des_dev
*dd
,
991 static int omap_des_get_pdev(struct omap_des_dev
*dd
,
992 struct platform_device
*pdev
)
994 /* non-DT devices get pdata from pdev */
995 dd
->pdata
= pdev
->dev
.platform_data
;
1000 static int omap_des_probe(struct platform_device
*pdev
)
1002 struct device
*dev
= &pdev
->dev
;
1003 struct omap_des_dev
*dd
;
1004 struct crypto_alg
*algp
;
1005 struct resource
*res
;
1006 int err
= -ENOMEM
, i
, j
, irq
= -1;
1009 dd
= devm_kzalloc(dev
, sizeof(struct omap_des_dev
), GFP_KERNEL
);
1011 dev_err(dev
, "unable to alloc data struct.\n");
1015 platform_set_drvdata(pdev
, dd
);
1017 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1019 dev_err(dev
, "no MEM resource info\n");
1023 err
= (dev
->of_node
) ? omap_des_get_of(dd
, pdev
) :
1024 omap_des_get_pdev(dd
, pdev
);
1028 dd
->io_base
= devm_ioremap_resource(dev
, res
);
1029 if (IS_ERR(dd
->io_base
)) {
1030 err
= PTR_ERR(dd
->io_base
);
1033 dd
->phys_base
= res
->start
;
1035 pm_runtime_enable(dev
);
1036 pm_runtime_irq_safe(dev
);
1037 err
= pm_runtime_get_sync(dev
);
1039 pm_runtime_put_noidle(dev
);
1040 dev_err(dd
->dev
, "%s: failed to get_sync(%d)\n", __func__
, err
);
1044 omap_des_dma_stop(dd
);
1046 reg
= omap_des_read(dd
, DES_REG_REV(dd
));
1048 pm_runtime_put_sync(dev
);
1050 dev_info(dev
, "OMAP DES hw accel rev: %u.%u\n",
1051 (reg
& dd
->pdata
->major_mask
) >> dd
->pdata
->major_shift
,
1052 (reg
& dd
->pdata
->minor_mask
) >> dd
->pdata
->minor_shift
);
1054 tasklet_init(&dd
->done_task
, omap_des_done_task
, (unsigned long)dd
);
1056 err
= omap_des_dma_init(dd
);
1057 if (err
== -EPROBE_DEFER
) {
1059 } else if (err
&& DES_REG_IRQ_STATUS(dd
) && DES_REG_IRQ_ENABLE(dd
)) {
1062 irq
= platform_get_irq(pdev
, 0);
1064 dev_err(dev
, "can't get IRQ resource\n");
1068 err
= devm_request_irq(dev
, irq
, omap_des_irq
, 0,
1071 dev_err(dev
, "Unable to grab omap-des IRQ\n");
1077 INIT_LIST_HEAD(&dd
->list
);
1078 spin_lock(&list_lock
);
1079 list_add_tail(&dd
->list
, &dev_list
);
1080 spin_unlock(&list_lock
);
1082 /* Initialize des crypto engine */
1083 dd
->engine
= crypto_engine_alloc_init(dev
, 1);
1089 dd
->engine
->prepare_cipher_request
= omap_des_prepare_req
;
1090 dd
->engine
->cipher_one_request
= omap_des_crypt_req
;
1091 err
= crypto_engine_start(dd
->engine
);
1095 for (i
= 0; i
< dd
->pdata
->algs_info_size
; i
++) {
1096 for (j
= 0; j
< dd
->pdata
->algs_info
[i
].size
; j
++) {
1097 algp
= &dd
->pdata
->algs_info
[i
].algs_list
[j
];
1099 pr_debug("reg alg: %s\n", algp
->cra_name
);
1100 INIT_LIST_HEAD(&algp
->cra_list
);
1102 err
= crypto_register_alg(algp
);
1106 dd
->pdata
->algs_info
[i
].registered
++;
1113 for (i
= dd
->pdata
->algs_info_size
- 1; i
>= 0; i
--)
1114 for (j
= dd
->pdata
->algs_info
[i
].registered
- 1; j
>= 0; j
--)
1115 crypto_unregister_alg(
1116 &dd
->pdata
->algs_info
[i
].algs_list
[j
]);
1120 crypto_engine_exit(dd
->engine
);
1122 omap_des_dma_cleanup(dd
);
1124 tasklet_kill(&dd
->done_task
);
1126 pm_runtime_disable(dev
);
1130 dev_err(dev
, "initialization failed.\n");
1134 static int omap_des_remove(struct platform_device
*pdev
)
1136 struct omap_des_dev
*dd
= platform_get_drvdata(pdev
);
1142 spin_lock(&list_lock
);
1143 list_del(&dd
->list
);
1144 spin_unlock(&list_lock
);
1146 for (i
= dd
->pdata
->algs_info_size
- 1; i
>= 0; i
--)
1147 for (j
= dd
->pdata
->algs_info
[i
].registered
- 1; j
>= 0; j
--)
1148 crypto_unregister_alg(
1149 &dd
->pdata
->algs_info
[i
].algs_list
[j
]);
1151 tasklet_kill(&dd
->done_task
);
1152 omap_des_dma_cleanup(dd
);
1153 pm_runtime_disable(dd
->dev
);
1159 #ifdef CONFIG_PM_SLEEP
1160 static int omap_des_suspend(struct device
*dev
)
1162 pm_runtime_put_sync(dev
);
1166 static int omap_des_resume(struct device
*dev
)
1170 err
= pm_runtime_get_sync(dev
);
1172 pm_runtime_put_noidle(dev
);
1173 dev_err(dev
, "%s: failed to get_sync(%d)\n", __func__
, err
);
1180 static SIMPLE_DEV_PM_OPS(omap_des_pm_ops
, omap_des_suspend
, omap_des_resume
);
1182 static struct platform_driver omap_des_driver
= {
1183 .probe
= omap_des_probe
,
1184 .remove
= omap_des_remove
,
1187 .pm
= &omap_des_pm_ops
,
1188 .of_match_table
= of_match_ptr(omap_des_of_match
),
1192 module_platform_driver(omap_des_driver
);
1194 MODULE_DESCRIPTION("OMAP DES hw acceleration support.");
1195 MODULE_LICENSE("GPL v2");
1196 MODULE_AUTHOR("Joel Fernandes <joelf@ti.com>");