4 * Support for OMAP AES HW acceleration.
6 * Copyright (c) 2010 Nokia Corporation
7 * Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
8 * Copyright (c) 2011 Texas Instruments Incorporated
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
16 #define pr_fmt(fmt) "%20s: " fmt, __func__
17 #define prn(num) pr_debug(#num "=%d\n", num)
18 #define prx(num) pr_debug(#num "=%x\n", num)
20 #include <linux/err.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/errno.h>
24 #include <linux/kernel.h>
25 #include <linux/platform_device.h>
26 #include <linux/scatterlist.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/dmaengine.h>
29 #include <linux/pm_runtime.h>
31 #include <linux/of_device.h>
32 #include <linux/of_address.h>
34 #include <linux/crypto.h>
35 #include <linux/interrupt.h>
36 #include <crypto/scatterwalk.h>
37 #include <crypto/aes.h>
38 #include <crypto/gcm.h>
39 #include <crypto/engine.h>
40 #include <crypto/internal/skcipher.h>
41 #include <crypto/internal/aead.h>
43 #include "omap-crypto.h"
46 /* keep registered devices data here */
47 static LIST_HEAD(dev_list
);
48 static DEFINE_SPINLOCK(list_lock
);
51 #define omap_aes_read(dd, offset) \
54 _read_ret = __raw_readl(dd->io_base + offset); \
55 pr_debug("omap_aes_read(" #offset "=%#x)= %#x\n", \
60 inline u32
omap_aes_read(struct omap_aes_dev
*dd
, u32 offset
)
62 return __raw_readl(dd
->io_base
+ offset
);
67 #define omap_aes_write(dd, offset, value) \
69 pr_debug("omap_aes_write(" #offset "=%#x) value=%#x\n", \
71 __raw_writel(value, dd->io_base + offset); \
74 inline void omap_aes_write(struct omap_aes_dev
*dd
, u32 offset
,
77 __raw_writel(value
, dd
->io_base
+ offset
);
81 static inline void omap_aes_write_mask(struct omap_aes_dev
*dd
, u32 offset
,
86 val
= omap_aes_read(dd
, offset
);
89 omap_aes_write(dd
, offset
, val
);
92 static void omap_aes_write_n(struct omap_aes_dev
*dd
, u32 offset
,
93 u32
*value
, int count
)
95 for (; count
--; value
++, offset
+= 4)
96 omap_aes_write(dd
, offset
, *value
);
99 static int omap_aes_hw_init(struct omap_aes_dev
*dd
)
103 if (!(dd
->flags
& FLAGS_INIT
)) {
104 dd
->flags
|= FLAGS_INIT
;
108 err
= pm_runtime_get_sync(dd
->dev
);
110 dev_err(dd
->dev
, "failed to get sync: %d\n", err
);
117 void omap_aes_clear_copy_flags(struct omap_aes_dev
*dd
)
119 dd
->flags
&= ~(OMAP_CRYPTO_COPY_MASK
<< FLAGS_IN_DATA_ST_SHIFT
);
120 dd
->flags
&= ~(OMAP_CRYPTO_COPY_MASK
<< FLAGS_OUT_DATA_ST_SHIFT
);
121 dd
->flags
&= ~(OMAP_CRYPTO_COPY_MASK
<< FLAGS_ASSOC_DATA_ST_SHIFT
);
124 int omap_aes_write_ctrl(struct omap_aes_dev
*dd
)
126 struct omap_aes_reqctx
*rctx
;
131 err
= omap_aes_hw_init(dd
);
135 key32
= dd
->ctx
->keylen
/ sizeof(u32
);
137 /* RESET the key as previous HASH keys should not get affected*/
138 if (dd
->flags
& FLAGS_GCM
)
139 for (i
= 0; i
< 0x40; i
= i
+ 4)
140 omap_aes_write(dd
, i
, 0x0);
142 for (i
= 0; i
< key32
; i
++) {
143 omap_aes_write(dd
, AES_REG_KEY(dd
, i
),
144 __le32_to_cpu(dd
->ctx
->key
[i
]));
147 if ((dd
->flags
& (FLAGS_CBC
| FLAGS_CTR
)) && dd
->req
->info
)
148 omap_aes_write_n(dd
, AES_REG_IV(dd
, 0), dd
->req
->info
, 4);
150 if ((dd
->flags
& (FLAGS_GCM
)) && dd
->aead_req
->iv
) {
151 rctx
= aead_request_ctx(dd
->aead_req
);
152 omap_aes_write_n(dd
, AES_REG_IV(dd
, 0), (u32
*)rctx
->iv
, 4);
155 val
= FLD_VAL(((dd
->ctx
->keylen
>> 3) - 1), 4, 3);
156 if (dd
->flags
& FLAGS_CBC
)
157 val
|= AES_REG_CTRL_CBC
;
159 if (dd
->flags
& (FLAGS_CTR
| FLAGS_GCM
))
160 val
|= AES_REG_CTRL_CTR
| AES_REG_CTRL_CTR_WIDTH_128
;
162 if (dd
->flags
& FLAGS_GCM
)
163 val
|= AES_REG_CTRL_GCM
;
165 if (dd
->flags
& FLAGS_ENCRYPT
)
166 val
|= AES_REG_CTRL_DIRECTION
;
168 omap_aes_write_mask(dd
, AES_REG_CTRL(dd
), val
, AES_REG_CTRL_MASK
);
173 static void omap_aes_dma_trigger_omap2(struct omap_aes_dev
*dd
, int length
)
177 val
= dd
->pdata
->dma_start
;
179 if (dd
->dma_lch_out
!= NULL
)
180 val
|= dd
->pdata
->dma_enable_out
;
181 if (dd
->dma_lch_in
!= NULL
)
182 val
|= dd
->pdata
->dma_enable_in
;
184 mask
= dd
->pdata
->dma_enable_out
| dd
->pdata
->dma_enable_in
|
185 dd
->pdata
->dma_start
;
187 omap_aes_write_mask(dd
, AES_REG_MASK(dd
), val
, mask
);
191 static void omap_aes_dma_trigger_omap4(struct omap_aes_dev
*dd
, int length
)
193 omap_aes_write(dd
, AES_REG_LENGTH_N(0), length
);
194 omap_aes_write(dd
, AES_REG_LENGTH_N(1), 0);
195 if (dd
->flags
& FLAGS_GCM
)
196 omap_aes_write(dd
, AES_REG_A_LEN
, dd
->assoc_len
);
198 omap_aes_dma_trigger_omap2(dd
, length
);
201 static void omap_aes_dma_stop(struct omap_aes_dev
*dd
)
205 mask
= dd
->pdata
->dma_enable_out
| dd
->pdata
->dma_enable_in
|
206 dd
->pdata
->dma_start
;
208 omap_aes_write_mask(dd
, AES_REG_MASK(dd
), 0, mask
);
211 struct omap_aes_dev
*omap_aes_find_dev(struct omap_aes_reqctx
*rctx
)
213 struct omap_aes_dev
*dd
;
215 spin_lock_bh(&list_lock
);
216 dd
= list_first_entry(&dev_list
, struct omap_aes_dev
, list
);
217 list_move_tail(&dd
->list
, &dev_list
);
219 spin_unlock_bh(&list_lock
);
224 static void omap_aes_dma_out_callback(void *data
)
226 struct omap_aes_dev
*dd
= data
;
228 /* dma_lch_out - completed */
229 tasklet_schedule(&dd
->done_task
);
232 static int omap_aes_dma_init(struct omap_aes_dev
*dd
)
236 dd
->dma_lch_out
= NULL
;
237 dd
->dma_lch_in
= NULL
;
239 dd
->dma_lch_in
= dma_request_chan(dd
->dev
, "rx");
240 if (IS_ERR(dd
->dma_lch_in
)) {
241 dev_err(dd
->dev
, "Unable to request in DMA channel\n");
242 return PTR_ERR(dd
->dma_lch_in
);
245 dd
->dma_lch_out
= dma_request_chan(dd
->dev
, "tx");
246 if (IS_ERR(dd
->dma_lch_out
)) {
247 dev_err(dd
->dev
, "Unable to request out DMA channel\n");
248 err
= PTR_ERR(dd
->dma_lch_out
);
255 dma_release_channel(dd
->dma_lch_in
);
260 static void omap_aes_dma_cleanup(struct omap_aes_dev
*dd
)
265 dma_release_channel(dd
->dma_lch_out
);
266 dma_release_channel(dd
->dma_lch_in
);
269 static int omap_aes_crypt_dma(struct omap_aes_dev
*dd
,
270 struct scatterlist
*in_sg
,
271 struct scatterlist
*out_sg
,
272 int in_sg_len
, int out_sg_len
)
274 struct dma_async_tx_descriptor
*tx_in
, *tx_out
;
275 struct dma_slave_config cfg
;
279 scatterwalk_start(&dd
->in_walk
, dd
->in_sg
);
280 scatterwalk_start(&dd
->out_walk
, dd
->out_sg
);
282 /* Enable DATAIN interrupt and let it take
284 omap_aes_write(dd
, AES_REG_IRQ_ENABLE(dd
), 0x2);
288 dma_sync_sg_for_device(dd
->dev
, dd
->in_sg
, in_sg_len
, DMA_TO_DEVICE
);
290 memset(&cfg
, 0, sizeof(cfg
));
292 cfg
.src_addr
= dd
->phys_base
+ AES_REG_DATA_N(dd
, 0);
293 cfg
.dst_addr
= dd
->phys_base
+ AES_REG_DATA_N(dd
, 0);
294 cfg
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
295 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
296 cfg
.src_maxburst
= DST_MAXBURST
;
297 cfg
.dst_maxburst
= DST_MAXBURST
;
300 ret
= dmaengine_slave_config(dd
->dma_lch_in
, &cfg
);
302 dev_err(dd
->dev
, "can't configure IN dmaengine slave: %d\n",
307 tx_in
= dmaengine_prep_slave_sg(dd
->dma_lch_in
, in_sg
, in_sg_len
,
309 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
311 dev_err(dd
->dev
, "IN prep_slave_sg() failed\n");
315 /* No callback necessary */
316 tx_in
->callback_param
= dd
;
319 ret
= dmaengine_slave_config(dd
->dma_lch_out
, &cfg
);
321 dev_err(dd
->dev
, "can't configure OUT dmaengine slave: %d\n",
326 tx_out
= dmaengine_prep_slave_sg(dd
->dma_lch_out
, out_sg
, out_sg_len
,
328 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
330 dev_err(dd
->dev
, "OUT prep_slave_sg() failed\n");
334 if (dd
->flags
& FLAGS_GCM
)
335 tx_out
->callback
= omap_aes_gcm_dma_out_callback
;
337 tx_out
->callback
= omap_aes_dma_out_callback
;
338 tx_out
->callback_param
= dd
;
340 dmaengine_submit(tx_in
);
341 dmaengine_submit(tx_out
);
343 dma_async_issue_pending(dd
->dma_lch_in
);
344 dma_async_issue_pending(dd
->dma_lch_out
);
347 dd
->pdata
->trigger(dd
, dd
->total
);
352 int omap_aes_crypt_dma_start(struct omap_aes_dev
*dd
)
356 pr_debug("total: %d\n", dd
->total
);
359 err
= dma_map_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
,
362 dev_err(dd
->dev
, "dma_map_sg() error\n");
366 err
= dma_map_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
369 dev_err(dd
->dev
, "dma_map_sg() error\n");
374 err
= omap_aes_crypt_dma(dd
, dd
->in_sg
, dd
->out_sg
, dd
->in_sg_len
,
376 if (err
&& !dd
->pio_only
) {
377 dma_unmap_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
, DMA_TO_DEVICE
);
378 dma_unmap_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
385 static void omap_aes_finish_req(struct omap_aes_dev
*dd
, int err
)
387 struct ablkcipher_request
*req
= dd
->req
;
389 pr_debug("err: %d\n", err
);
391 crypto_finalize_cipher_request(dd
->engine
, req
, err
);
393 pm_runtime_mark_last_busy(dd
->dev
);
394 pm_runtime_put_autosuspend(dd
->dev
);
397 int omap_aes_crypt_dma_stop(struct omap_aes_dev
*dd
)
399 pr_debug("total: %d\n", dd
->total
);
401 omap_aes_dma_stop(dd
);
407 static int omap_aes_handle_queue(struct omap_aes_dev
*dd
,
408 struct ablkcipher_request
*req
)
411 return crypto_transfer_cipher_request_to_engine(dd
->engine
, req
);
416 static int omap_aes_prepare_req(struct crypto_engine
*engine
,
417 struct ablkcipher_request
*req
)
419 struct omap_aes_ctx
*ctx
= crypto_ablkcipher_ctx(
420 crypto_ablkcipher_reqtfm(req
));
421 struct omap_aes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
422 struct omap_aes_dev
*dd
= rctx
->dd
;
429 /* assign new request to device */
431 dd
->total
= req
->nbytes
;
432 dd
->total_save
= req
->nbytes
;
433 dd
->in_sg
= req
->src
;
434 dd
->out_sg
= req
->dst
;
435 dd
->orig_out
= req
->dst
;
437 flags
= OMAP_CRYPTO_COPY_DATA
;
438 if (req
->src
== req
->dst
)
439 flags
|= OMAP_CRYPTO_FORCE_COPY
;
441 ret
= omap_crypto_align_sg(&dd
->in_sg
, dd
->total
, AES_BLOCK_SIZE
,
443 FLAGS_IN_DATA_ST_SHIFT
, &dd
->flags
);
447 ret
= omap_crypto_align_sg(&dd
->out_sg
, dd
->total
, AES_BLOCK_SIZE
,
449 FLAGS_OUT_DATA_ST_SHIFT
, &dd
->flags
);
453 dd
->in_sg_len
= sg_nents_for_len(dd
->in_sg
, dd
->total
);
454 if (dd
->in_sg_len
< 0)
455 return dd
->in_sg_len
;
457 dd
->out_sg_len
= sg_nents_for_len(dd
->out_sg
, dd
->total
);
458 if (dd
->out_sg_len
< 0)
459 return dd
->out_sg_len
;
461 rctx
->mode
&= FLAGS_MODE_MASK
;
462 dd
->flags
= (dd
->flags
& ~FLAGS_MODE_MASK
) | rctx
->mode
;
467 return omap_aes_write_ctrl(dd
);
470 static int omap_aes_crypt_req(struct crypto_engine
*engine
,
471 struct ablkcipher_request
*req
)
473 struct omap_aes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
474 struct omap_aes_dev
*dd
= rctx
->dd
;
479 return omap_aes_crypt_dma_start(dd
);
482 static void omap_aes_done_task(unsigned long data
)
484 struct omap_aes_dev
*dd
= (struct omap_aes_dev
*)data
;
486 pr_debug("enter done_task\n");
489 dma_sync_sg_for_device(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
491 dma_unmap_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
, DMA_TO_DEVICE
);
492 dma_unmap_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
494 omap_aes_crypt_dma_stop(dd
);
497 omap_crypto_cleanup(dd
->in_sgl
, NULL
, 0, dd
->total_save
,
498 FLAGS_IN_DATA_ST_SHIFT
, dd
->flags
);
500 omap_crypto_cleanup(&dd
->out_sgl
, dd
->orig_out
, 0, dd
->total_save
,
501 FLAGS_OUT_DATA_ST_SHIFT
, dd
->flags
);
503 omap_aes_finish_req(dd
, 0);
508 static int omap_aes_crypt(struct ablkcipher_request
*req
, unsigned long mode
)
510 struct omap_aes_ctx
*ctx
= crypto_ablkcipher_ctx(
511 crypto_ablkcipher_reqtfm(req
));
512 struct omap_aes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
513 struct omap_aes_dev
*dd
;
516 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req
->nbytes
,
517 !!(mode
& FLAGS_ENCRYPT
),
518 !!(mode
& FLAGS_CBC
));
520 if (req
->nbytes
< 200) {
521 SKCIPHER_REQUEST_ON_STACK(subreq
, ctx
->fallback
);
523 skcipher_request_set_tfm(subreq
, ctx
->fallback
);
524 skcipher_request_set_callback(subreq
, req
->base
.flags
, NULL
,
526 skcipher_request_set_crypt(subreq
, req
->src
, req
->dst
,
527 req
->nbytes
, req
->info
);
529 if (mode
& FLAGS_ENCRYPT
)
530 ret
= crypto_skcipher_encrypt(subreq
);
532 ret
= crypto_skcipher_decrypt(subreq
);
534 skcipher_request_zero(subreq
);
537 dd
= omap_aes_find_dev(rctx
);
543 return omap_aes_handle_queue(dd
, req
);
546 /* ********************** ALG API ************************************ */
548 static int omap_aes_setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
551 struct omap_aes_ctx
*ctx
= crypto_ablkcipher_ctx(tfm
);
554 if (keylen
!= AES_KEYSIZE_128
&& keylen
!= AES_KEYSIZE_192
&&
555 keylen
!= AES_KEYSIZE_256
)
558 pr_debug("enter, keylen: %d\n", keylen
);
560 memcpy(ctx
->key
, key
, keylen
);
561 ctx
->keylen
= keylen
;
563 crypto_skcipher_clear_flags(ctx
->fallback
, CRYPTO_TFM_REQ_MASK
);
564 crypto_skcipher_set_flags(ctx
->fallback
, tfm
->base
.crt_flags
&
565 CRYPTO_TFM_REQ_MASK
);
567 ret
= crypto_skcipher_setkey(ctx
->fallback
, key
, keylen
);
574 static int omap_aes_ecb_encrypt(struct ablkcipher_request
*req
)
576 return omap_aes_crypt(req
, FLAGS_ENCRYPT
);
579 static int omap_aes_ecb_decrypt(struct ablkcipher_request
*req
)
581 return omap_aes_crypt(req
, 0);
584 static int omap_aes_cbc_encrypt(struct ablkcipher_request
*req
)
586 return omap_aes_crypt(req
, FLAGS_ENCRYPT
| FLAGS_CBC
);
589 static int omap_aes_cbc_decrypt(struct ablkcipher_request
*req
)
591 return omap_aes_crypt(req
, FLAGS_CBC
);
594 static int omap_aes_ctr_encrypt(struct ablkcipher_request
*req
)
596 return omap_aes_crypt(req
, FLAGS_ENCRYPT
| FLAGS_CTR
);
599 static int omap_aes_ctr_decrypt(struct ablkcipher_request
*req
)
601 return omap_aes_crypt(req
, FLAGS_CTR
);
604 static int omap_aes_cra_init(struct crypto_tfm
*tfm
)
606 const char *name
= crypto_tfm_alg_name(tfm
);
607 const u32 flags
= CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
;
608 struct omap_aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
609 struct crypto_skcipher
*blk
;
611 blk
= crypto_alloc_skcipher(name
, 0, flags
);
617 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct omap_aes_reqctx
);
622 static int omap_aes_gcm_cra_init(struct crypto_aead
*tfm
)
624 struct omap_aes_dev
*dd
= NULL
;
625 struct omap_aes_ctx
*ctx
= crypto_aead_ctx(tfm
);
628 /* Find AES device, currently picks the first device */
629 spin_lock_bh(&list_lock
);
630 list_for_each_entry(dd
, &dev_list
, list
) {
633 spin_unlock_bh(&list_lock
);
635 err
= pm_runtime_get_sync(dd
->dev
);
637 dev_err(dd
->dev
, "%s: failed to get_sync(%d)\n",
642 tfm
->reqsize
= sizeof(struct omap_aes_reqctx
);
643 ctx
->ctr
= crypto_alloc_skcipher("ecb(aes)", 0, 0);
644 if (IS_ERR(ctx
->ctr
)) {
645 pr_warn("could not load aes driver for encrypting IV\n");
646 return PTR_ERR(ctx
->ctr
);
652 static void omap_aes_cra_exit(struct crypto_tfm
*tfm
)
654 struct omap_aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
657 crypto_free_skcipher(ctx
->fallback
);
659 ctx
->fallback
= NULL
;
662 static void omap_aes_gcm_cra_exit(struct crypto_aead
*tfm
)
664 struct omap_aes_ctx
*ctx
= crypto_aead_ctx(tfm
);
666 omap_aes_cra_exit(crypto_aead_tfm(tfm
));
669 crypto_free_skcipher(ctx
->ctr
);
672 /* ********************** ALGS ************************************ */
674 static struct crypto_alg algs_ecb_cbc
[] = {
676 .cra_name
= "ecb(aes)",
677 .cra_driver_name
= "ecb-aes-omap",
679 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
680 CRYPTO_ALG_KERN_DRIVER_ONLY
|
681 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
,
682 .cra_blocksize
= AES_BLOCK_SIZE
,
683 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
685 .cra_type
= &crypto_ablkcipher_type
,
686 .cra_module
= THIS_MODULE
,
687 .cra_init
= omap_aes_cra_init
,
688 .cra_exit
= omap_aes_cra_exit
,
689 .cra_u
.ablkcipher
= {
690 .min_keysize
= AES_MIN_KEY_SIZE
,
691 .max_keysize
= AES_MAX_KEY_SIZE
,
692 .setkey
= omap_aes_setkey
,
693 .encrypt
= omap_aes_ecb_encrypt
,
694 .decrypt
= omap_aes_ecb_decrypt
,
698 .cra_name
= "cbc(aes)",
699 .cra_driver_name
= "cbc-aes-omap",
701 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
702 CRYPTO_ALG_KERN_DRIVER_ONLY
|
703 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
,
704 .cra_blocksize
= AES_BLOCK_SIZE
,
705 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
707 .cra_type
= &crypto_ablkcipher_type
,
708 .cra_module
= THIS_MODULE
,
709 .cra_init
= omap_aes_cra_init
,
710 .cra_exit
= omap_aes_cra_exit
,
711 .cra_u
.ablkcipher
= {
712 .min_keysize
= AES_MIN_KEY_SIZE
,
713 .max_keysize
= AES_MAX_KEY_SIZE
,
714 .ivsize
= AES_BLOCK_SIZE
,
715 .setkey
= omap_aes_setkey
,
716 .encrypt
= omap_aes_cbc_encrypt
,
717 .decrypt
= omap_aes_cbc_decrypt
,
722 static struct crypto_alg algs_ctr
[] = {
724 .cra_name
= "ctr(aes)",
725 .cra_driver_name
= "ctr-aes-omap",
727 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
728 CRYPTO_ALG_KERN_DRIVER_ONLY
|
729 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
,
730 .cra_blocksize
= AES_BLOCK_SIZE
,
731 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
733 .cra_type
= &crypto_ablkcipher_type
,
734 .cra_module
= THIS_MODULE
,
735 .cra_init
= omap_aes_cra_init
,
736 .cra_exit
= omap_aes_cra_exit
,
737 .cra_u
.ablkcipher
= {
738 .min_keysize
= AES_MIN_KEY_SIZE
,
739 .max_keysize
= AES_MAX_KEY_SIZE
,
741 .ivsize
= AES_BLOCK_SIZE
,
742 .setkey
= omap_aes_setkey
,
743 .encrypt
= omap_aes_ctr_encrypt
,
744 .decrypt
= omap_aes_ctr_decrypt
,
749 static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc
[] = {
751 .algs_list
= algs_ecb_cbc
,
752 .size
= ARRAY_SIZE(algs_ecb_cbc
),
756 static struct aead_alg algs_aead_gcm
[] = {
759 .cra_name
= "gcm(aes)",
760 .cra_driver_name
= "gcm-aes-omap",
762 .cra_flags
= CRYPTO_ALG_ASYNC
|
763 CRYPTO_ALG_KERN_DRIVER_ONLY
,
765 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
766 .cra_alignmask
= 0xf,
767 .cra_module
= THIS_MODULE
,
769 .init
= omap_aes_gcm_cra_init
,
770 .exit
= omap_aes_gcm_cra_exit
,
771 .ivsize
= GCM_AES_IV_SIZE
,
772 .maxauthsize
= AES_BLOCK_SIZE
,
773 .setkey
= omap_aes_gcm_setkey
,
774 .encrypt
= omap_aes_gcm_encrypt
,
775 .decrypt
= omap_aes_gcm_decrypt
,
779 .cra_name
= "rfc4106(gcm(aes))",
780 .cra_driver_name
= "rfc4106-gcm-aes-omap",
782 .cra_flags
= CRYPTO_ALG_ASYNC
|
783 CRYPTO_ALG_KERN_DRIVER_ONLY
,
785 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
786 .cra_alignmask
= 0xf,
787 .cra_module
= THIS_MODULE
,
789 .init
= omap_aes_gcm_cra_init
,
790 .exit
= omap_aes_gcm_cra_exit
,
791 .maxauthsize
= AES_BLOCK_SIZE
,
792 .ivsize
= GCM_RFC4106_IV_SIZE
,
793 .setkey
= omap_aes_4106gcm_setkey
,
794 .encrypt
= omap_aes_4106gcm_encrypt
,
795 .decrypt
= omap_aes_4106gcm_decrypt
,
799 static struct omap_aes_aead_algs omap_aes_aead_info
= {
800 .algs_list
= algs_aead_gcm
,
801 .size
= ARRAY_SIZE(algs_aead_gcm
),
804 static const struct omap_aes_pdata omap_aes_pdata_omap2
= {
805 .algs_info
= omap_aes_algs_info_ecb_cbc
,
806 .algs_info_size
= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc
),
807 .trigger
= omap_aes_dma_trigger_omap2
,
814 .dma_enable_in
= BIT(2),
815 .dma_enable_out
= BIT(3),
824 static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr
[] = {
826 .algs_list
= algs_ecb_cbc
,
827 .size
= ARRAY_SIZE(algs_ecb_cbc
),
830 .algs_list
= algs_ctr
,
831 .size
= ARRAY_SIZE(algs_ctr
),
835 static const struct omap_aes_pdata omap_aes_pdata_omap3
= {
836 .algs_info
= omap_aes_algs_info_ecb_cbc_ctr
,
837 .algs_info_size
= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr
),
838 .trigger
= omap_aes_dma_trigger_omap2
,
845 .dma_enable_in
= BIT(2),
846 .dma_enable_out
= BIT(3),
854 static const struct omap_aes_pdata omap_aes_pdata_omap4
= {
855 .algs_info
= omap_aes_algs_info_ecb_cbc_ctr
,
856 .algs_info_size
= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr
),
857 .aead_algs_info
= &omap_aes_aead_info
,
858 .trigger
= omap_aes_dma_trigger_omap4
,
865 .irq_status_ofs
= 0x8c,
866 .irq_enable_ofs
= 0x90,
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_aes_irq(int irq
, void *dev_id
)
877 struct omap_aes_dev
*dd
= dev_id
;
881 status
= omap_aes_read(dd
, AES_REG_IRQ_STATUS(dd
));
882 if (status
& AES_REG_IRQ_DATA_IN
) {
883 omap_aes_write(dd
, AES_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
< AES_BLOCK_WORDS
; i
++) {
892 omap_aes_write(dd
, AES_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
&= ~AES_REG_IRQ_DATA_IN
;
910 omap_aes_write(dd
, AES_REG_IRQ_STATUS(dd
), status
);
912 /* Enable DATA_OUT interrupt */
913 omap_aes_write(dd
, AES_REG_IRQ_ENABLE(dd
), 0x4);
915 } else if (status
& AES_REG_IRQ_DATA_OUT
) {
916 omap_aes_write(dd
, AES_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
< AES_BLOCK_WORDS
; i
++) {
925 *dst
= omap_aes_read(dd
, AES_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 dd
->total
-= min_t(size_t, AES_BLOCK_SIZE
, dd
->total
);
942 /* Clear IRQ status */
943 status
&= ~AES_REG_IRQ_DATA_OUT
;
944 omap_aes_write(dd
, AES_REG_IRQ_STATUS(dd
), status
);
947 /* All bytes read! */
948 tasklet_schedule(&dd
->done_task
);
950 /* Enable DATA_IN interrupt for next block */
951 omap_aes_write(dd
, AES_REG_IRQ_ENABLE(dd
), 0x2);
957 static const struct of_device_id omap_aes_of_match
[] = {
959 .compatible
= "ti,omap2-aes",
960 .data
= &omap_aes_pdata_omap2
,
963 .compatible
= "ti,omap3-aes",
964 .data
= &omap_aes_pdata_omap3
,
967 .compatible
= "ti,omap4-aes",
968 .data
= &omap_aes_pdata_omap4
,
972 MODULE_DEVICE_TABLE(of
, omap_aes_of_match
);
974 static int omap_aes_get_res_of(struct omap_aes_dev
*dd
,
975 struct device
*dev
, struct resource
*res
)
977 struct device_node
*node
= dev
->of_node
;
980 dd
->pdata
= of_device_get_match_data(dev
);
982 dev_err(dev
, "no compatible OF match\n");
987 err
= of_address_to_resource(node
, 0, res
);
989 dev_err(dev
, "can't translate OF node address\n");
998 static const struct of_device_id omap_aes_of_match
[] = {
1002 static int omap_aes_get_res_of(struct omap_aes_dev
*dd
,
1003 struct device
*dev
, struct resource
*res
)
1009 static int omap_aes_get_res_pdev(struct omap_aes_dev
*dd
,
1010 struct platform_device
*pdev
, struct resource
*res
)
1012 struct device
*dev
= &pdev
->dev
;
1016 /* Get the base address */
1017 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1019 dev_err(dev
, "no MEM resource info\n");
1023 memcpy(res
, r
, sizeof(*res
));
1025 /* Only OMAP2/3 can be non-DT */
1026 dd
->pdata
= &omap_aes_pdata_omap2
;
1032 static int omap_aes_probe(struct platform_device
*pdev
)
1034 struct device
*dev
= &pdev
->dev
;
1035 struct omap_aes_dev
*dd
;
1036 struct crypto_alg
*algp
;
1037 struct aead_alg
*aalg
;
1038 struct resource res
;
1039 int err
= -ENOMEM
, i
, j
, irq
= -1;
1042 dd
= devm_kzalloc(dev
, sizeof(struct omap_aes_dev
), GFP_KERNEL
);
1044 dev_err(dev
, "unable to alloc data struct.\n");
1048 platform_set_drvdata(pdev
, dd
);
1050 aead_init_queue(&dd
->aead_queue
, OMAP_AES_QUEUE_LENGTH
);
1052 err
= (dev
->of_node
) ? omap_aes_get_res_of(dd
, dev
, &res
) :
1053 omap_aes_get_res_pdev(dd
, pdev
, &res
);
1057 dd
->io_base
= devm_ioremap_resource(dev
, &res
);
1058 if (IS_ERR(dd
->io_base
)) {
1059 err
= PTR_ERR(dd
->io_base
);
1062 dd
->phys_base
= res
.start
;
1064 pm_runtime_use_autosuspend(dev
);
1065 pm_runtime_set_autosuspend_delay(dev
, DEFAULT_AUTOSUSPEND_DELAY
);
1067 pm_runtime_enable(dev
);
1068 err
= pm_runtime_get_sync(dev
);
1070 dev_err(dev
, "%s: failed to get_sync(%d)\n",
1075 omap_aes_dma_stop(dd
);
1077 reg
= omap_aes_read(dd
, AES_REG_REV(dd
));
1079 pm_runtime_put_sync(dev
);
1081 dev_info(dev
, "OMAP AES hw accel rev: %u.%u\n",
1082 (reg
& dd
->pdata
->major_mask
) >> dd
->pdata
->major_shift
,
1083 (reg
& dd
->pdata
->minor_mask
) >> dd
->pdata
->minor_shift
);
1085 tasklet_init(&dd
->done_task
, omap_aes_done_task
, (unsigned long)dd
);
1087 err
= omap_aes_dma_init(dd
);
1088 if (err
== -EPROBE_DEFER
) {
1090 } else if (err
&& AES_REG_IRQ_STATUS(dd
) && AES_REG_IRQ_ENABLE(dd
)) {
1093 irq
= platform_get_irq(pdev
, 0);
1095 dev_err(dev
, "can't get IRQ resource\n");
1100 err
= devm_request_irq(dev
, irq
, omap_aes_irq
, 0,
1103 dev_err(dev
, "Unable to grab omap-aes IRQ\n");
1108 spin_lock_init(&dd
->lock
);
1110 INIT_LIST_HEAD(&dd
->list
);
1111 spin_lock(&list_lock
);
1112 list_add_tail(&dd
->list
, &dev_list
);
1113 spin_unlock(&list_lock
);
1115 /* Initialize crypto engine */
1116 dd
->engine
= crypto_engine_alloc_init(dev
, 1);
1122 dd
->engine
->prepare_cipher_request
= omap_aes_prepare_req
;
1123 dd
->engine
->cipher_one_request
= omap_aes_crypt_req
;
1124 err
= crypto_engine_start(dd
->engine
);
1128 for (i
= 0; i
< dd
->pdata
->algs_info_size
; i
++) {
1129 if (!dd
->pdata
->algs_info
[i
].registered
) {
1130 for (j
= 0; j
< dd
->pdata
->algs_info
[i
].size
; j
++) {
1131 algp
= &dd
->pdata
->algs_info
[i
].algs_list
[j
];
1133 pr_debug("reg alg: %s\n", algp
->cra_name
);
1134 INIT_LIST_HEAD(&algp
->cra_list
);
1136 err
= crypto_register_alg(algp
);
1140 dd
->pdata
->algs_info
[i
].registered
++;
1145 if (dd
->pdata
->aead_algs_info
&&
1146 !dd
->pdata
->aead_algs_info
->registered
) {
1147 for (i
= 0; i
< dd
->pdata
->aead_algs_info
->size
; i
++) {
1148 aalg
= &dd
->pdata
->aead_algs_info
->algs_list
[i
];
1151 pr_debug("reg alg: %s\n", algp
->cra_name
);
1152 INIT_LIST_HEAD(&algp
->cra_list
);
1154 err
= crypto_register_aead(aalg
);
1158 dd
->pdata
->aead_algs_info
->registered
++;
1164 for (i
= dd
->pdata
->aead_algs_info
->registered
- 1; i
>= 0; i
--) {
1165 aalg
= &dd
->pdata
->aead_algs_info
->algs_list
[i
];
1166 crypto_unregister_aead(aalg
);
1169 for (i
= dd
->pdata
->algs_info_size
- 1; i
>= 0; i
--)
1170 for (j
= dd
->pdata
->algs_info
[i
].registered
- 1; j
>= 0; j
--)
1171 crypto_unregister_alg(
1172 &dd
->pdata
->algs_info
[i
].algs_list
[j
]);
1176 crypto_engine_exit(dd
->engine
);
1178 omap_aes_dma_cleanup(dd
);
1180 tasklet_kill(&dd
->done_task
);
1181 pm_runtime_disable(dev
);
1185 dev_err(dev
, "initialization failed.\n");
1189 static int omap_aes_remove(struct platform_device
*pdev
)
1191 struct omap_aes_dev
*dd
= platform_get_drvdata(pdev
);
1192 struct aead_alg
*aalg
;
1198 spin_lock(&list_lock
);
1199 list_del(&dd
->list
);
1200 spin_unlock(&list_lock
);
1202 for (i
= dd
->pdata
->algs_info_size
- 1; i
>= 0; i
--)
1203 for (j
= dd
->pdata
->algs_info
[i
].registered
- 1; j
>= 0; j
--)
1204 crypto_unregister_alg(
1205 &dd
->pdata
->algs_info
[i
].algs_list
[j
]);
1207 for (i
= dd
->pdata
->aead_algs_info
->size
- 1; i
>= 0; i
--) {
1208 aalg
= &dd
->pdata
->aead_algs_info
->algs_list
[i
];
1209 crypto_unregister_aead(aalg
);
1212 crypto_engine_exit(dd
->engine
);
1214 tasklet_kill(&dd
->done_task
);
1215 omap_aes_dma_cleanup(dd
);
1216 pm_runtime_disable(dd
->dev
);
1222 #ifdef CONFIG_PM_SLEEP
1223 static int omap_aes_suspend(struct device
*dev
)
1225 pm_runtime_put_sync(dev
);
1229 static int omap_aes_resume(struct device
*dev
)
1231 pm_runtime_get_sync(dev
);
1236 static SIMPLE_DEV_PM_OPS(omap_aes_pm_ops
, omap_aes_suspend
, omap_aes_resume
);
1238 static struct platform_driver omap_aes_driver
= {
1239 .probe
= omap_aes_probe
,
1240 .remove
= omap_aes_remove
,
1243 .pm
= &omap_aes_pm_ops
,
1244 .of_match_table
= omap_aes_of_match
,
1248 module_platform_driver(omap_aes_driver
);
1250 MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
1251 MODULE_LICENSE("GPL v2");
1252 MODULE_AUTHOR("Dmitry Kasatkin");