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/engine.h>
39 #include <crypto/internal/skcipher.h>
40 #include <crypto/internal/aead.h>
42 #include "omap-crypto.h"
45 /* keep registered devices data here */
46 static LIST_HEAD(dev_list
);
47 static DEFINE_SPINLOCK(list_lock
);
50 #define omap_aes_read(dd, offset) \
53 _read_ret = __raw_readl(dd->io_base + offset); \
54 pr_debug("omap_aes_read(" #offset "=%#x)= %#x\n", \
59 inline u32
omap_aes_read(struct omap_aes_dev
*dd
, u32 offset
)
61 return __raw_readl(dd
->io_base
+ offset
);
66 #define omap_aes_write(dd, offset, value) \
68 pr_debug("omap_aes_write(" #offset "=%#x) value=%#x\n", \
70 __raw_writel(value, dd->io_base + offset); \
73 inline void omap_aes_write(struct omap_aes_dev
*dd
, u32 offset
,
76 __raw_writel(value
, dd
->io_base
+ offset
);
80 static inline void omap_aes_write_mask(struct omap_aes_dev
*dd
, u32 offset
,
85 val
= omap_aes_read(dd
, offset
);
88 omap_aes_write(dd
, offset
, val
);
91 static void omap_aes_write_n(struct omap_aes_dev
*dd
, u32 offset
,
92 u32
*value
, int count
)
94 for (; count
--; value
++, offset
+= 4)
95 omap_aes_write(dd
, offset
, *value
);
98 static int omap_aes_hw_init(struct omap_aes_dev
*dd
)
102 if (!(dd
->flags
& FLAGS_INIT
)) {
103 dd
->flags
|= FLAGS_INIT
;
107 err
= pm_runtime_get_sync(dd
->dev
);
109 dev_err(dd
->dev
, "failed to get sync: %d\n", err
);
116 void omap_aes_clear_copy_flags(struct omap_aes_dev
*dd
)
118 dd
->flags
&= ~(OMAP_CRYPTO_COPY_MASK
<< FLAGS_IN_DATA_ST_SHIFT
);
119 dd
->flags
&= ~(OMAP_CRYPTO_COPY_MASK
<< FLAGS_OUT_DATA_ST_SHIFT
);
120 dd
->flags
&= ~(OMAP_CRYPTO_COPY_MASK
<< FLAGS_ASSOC_DATA_ST_SHIFT
);
123 int omap_aes_write_ctrl(struct omap_aes_dev
*dd
)
125 struct omap_aes_reqctx
*rctx
;
130 err
= omap_aes_hw_init(dd
);
134 key32
= dd
->ctx
->keylen
/ sizeof(u32
);
136 /* RESET the key as previous HASH keys should not get affected*/
137 if (dd
->flags
& FLAGS_GCM
)
138 for (i
= 0; i
< 0x40; i
= i
+ 4)
139 omap_aes_write(dd
, i
, 0x0);
141 for (i
= 0; i
< key32
; i
++) {
142 omap_aes_write(dd
, AES_REG_KEY(dd
, i
),
143 __le32_to_cpu(dd
->ctx
->key
[i
]));
146 if ((dd
->flags
& (FLAGS_CBC
| FLAGS_CTR
)) && dd
->req
->info
)
147 omap_aes_write_n(dd
, AES_REG_IV(dd
, 0), dd
->req
->info
, 4);
149 if ((dd
->flags
& (FLAGS_GCM
)) && dd
->aead_req
->iv
) {
150 rctx
= aead_request_ctx(dd
->aead_req
);
151 omap_aes_write_n(dd
, AES_REG_IV(dd
, 0), (u32
*)rctx
->iv
, 4);
154 val
= FLD_VAL(((dd
->ctx
->keylen
>> 3) - 1), 4, 3);
155 if (dd
->flags
& FLAGS_CBC
)
156 val
|= AES_REG_CTRL_CBC
;
158 if (dd
->flags
& (FLAGS_CTR
| FLAGS_GCM
))
159 val
|= AES_REG_CTRL_CTR
| AES_REG_CTRL_CTR_WIDTH_128
;
161 if (dd
->flags
& FLAGS_GCM
)
162 val
|= AES_REG_CTRL_GCM
;
164 if (dd
->flags
& FLAGS_ENCRYPT
)
165 val
|= AES_REG_CTRL_DIRECTION
;
167 omap_aes_write_mask(dd
, AES_REG_CTRL(dd
), val
, AES_REG_CTRL_MASK
);
172 static void omap_aes_dma_trigger_omap2(struct omap_aes_dev
*dd
, int length
)
176 val
= dd
->pdata
->dma_start
;
178 if (dd
->dma_lch_out
!= NULL
)
179 val
|= dd
->pdata
->dma_enable_out
;
180 if (dd
->dma_lch_in
!= NULL
)
181 val
|= dd
->pdata
->dma_enable_in
;
183 mask
= dd
->pdata
->dma_enable_out
| dd
->pdata
->dma_enable_in
|
184 dd
->pdata
->dma_start
;
186 omap_aes_write_mask(dd
, AES_REG_MASK(dd
), val
, mask
);
190 static void omap_aes_dma_trigger_omap4(struct omap_aes_dev
*dd
, int length
)
192 omap_aes_write(dd
, AES_REG_LENGTH_N(0), length
);
193 omap_aes_write(dd
, AES_REG_LENGTH_N(1), 0);
194 if (dd
->flags
& FLAGS_GCM
)
195 omap_aes_write(dd
, AES_REG_A_LEN
, dd
->assoc_len
);
197 omap_aes_dma_trigger_omap2(dd
, length
);
200 static void omap_aes_dma_stop(struct omap_aes_dev
*dd
)
204 mask
= dd
->pdata
->dma_enable_out
| dd
->pdata
->dma_enable_in
|
205 dd
->pdata
->dma_start
;
207 omap_aes_write_mask(dd
, AES_REG_MASK(dd
), 0, mask
);
210 struct omap_aes_dev
*omap_aes_find_dev(struct omap_aes_reqctx
*rctx
)
212 struct omap_aes_dev
*dd
;
214 spin_lock_bh(&list_lock
);
215 dd
= list_first_entry(&dev_list
, struct omap_aes_dev
, list
);
216 list_move_tail(&dd
->list
, &dev_list
);
218 spin_unlock_bh(&list_lock
);
223 static void omap_aes_dma_out_callback(void *data
)
225 struct omap_aes_dev
*dd
= data
;
227 /* dma_lch_out - completed */
228 tasklet_schedule(&dd
->done_task
);
231 static int omap_aes_dma_init(struct omap_aes_dev
*dd
)
235 dd
->dma_lch_out
= NULL
;
236 dd
->dma_lch_in
= NULL
;
238 dd
->dma_lch_in
= dma_request_chan(dd
->dev
, "rx");
239 if (IS_ERR(dd
->dma_lch_in
)) {
240 dev_err(dd
->dev
, "Unable to request in DMA channel\n");
241 return PTR_ERR(dd
->dma_lch_in
);
244 dd
->dma_lch_out
= dma_request_chan(dd
->dev
, "tx");
245 if (IS_ERR(dd
->dma_lch_out
)) {
246 dev_err(dd
->dev
, "Unable to request out DMA channel\n");
247 err
= PTR_ERR(dd
->dma_lch_out
);
254 dma_release_channel(dd
->dma_lch_in
);
259 static void omap_aes_dma_cleanup(struct omap_aes_dev
*dd
)
264 dma_release_channel(dd
->dma_lch_out
);
265 dma_release_channel(dd
->dma_lch_in
);
268 static int omap_aes_crypt_dma(struct omap_aes_dev
*dd
,
269 struct scatterlist
*in_sg
,
270 struct scatterlist
*out_sg
,
271 int in_sg_len
, int out_sg_len
)
273 struct dma_async_tx_descriptor
*tx_in
, *tx_out
;
274 struct dma_slave_config cfg
;
278 scatterwalk_start(&dd
->in_walk
, dd
->in_sg
);
279 scatterwalk_start(&dd
->out_walk
, dd
->out_sg
);
281 /* Enable DATAIN interrupt and let it take
283 omap_aes_write(dd
, AES_REG_IRQ_ENABLE(dd
), 0x2);
287 dma_sync_sg_for_device(dd
->dev
, dd
->in_sg
, in_sg_len
, DMA_TO_DEVICE
);
289 memset(&cfg
, 0, sizeof(cfg
));
291 cfg
.src_addr
= dd
->phys_base
+ AES_REG_DATA_N(dd
, 0);
292 cfg
.dst_addr
= dd
->phys_base
+ AES_REG_DATA_N(dd
, 0);
293 cfg
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
294 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
295 cfg
.src_maxburst
= DST_MAXBURST
;
296 cfg
.dst_maxburst
= DST_MAXBURST
;
299 ret
= dmaengine_slave_config(dd
->dma_lch_in
, &cfg
);
301 dev_err(dd
->dev
, "can't configure IN dmaengine slave: %d\n",
306 tx_in
= dmaengine_prep_slave_sg(dd
->dma_lch_in
, in_sg
, in_sg_len
,
308 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
310 dev_err(dd
->dev
, "IN prep_slave_sg() failed\n");
314 /* No callback necessary */
315 tx_in
->callback_param
= dd
;
318 ret
= dmaengine_slave_config(dd
->dma_lch_out
, &cfg
);
320 dev_err(dd
->dev
, "can't configure OUT dmaengine slave: %d\n",
325 tx_out
= dmaengine_prep_slave_sg(dd
->dma_lch_out
, out_sg
, out_sg_len
,
327 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
329 dev_err(dd
->dev
, "OUT prep_slave_sg() failed\n");
333 if (dd
->flags
& FLAGS_GCM
)
334 tx_out
->callback
= omap_aes_gcm_dma_out_callback
;
336 tx_out
->callback
= omap_aes_dma_out_callback
;
337 tx_out
->callback_param
= dd
;
339 dmaengine_submit(tx_in
);
340 dmaengine_submit(tx_out
);
342 dma_async_issue_pending(dd
->dma_lch_in
);
343 dma_async_issue_pending(dd
->dma_lch_out
);
346 dd
->pdata
->trigger(dd
, dd
->total
);
351 int omap_aes_crypt_dma_start(struct omap_aes_dev
*dd
)
355 pr_debug("total: %d\n", dd
->total
);
358 err
= dma_map_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
,
361 dev_err(dd
->dev
, "dma_map_sg() error\n");
365 err
= dma_map_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
368 dev_err(dd
->dev
, "dma_map_sg() error\n");
373 err
= omap_aes_crypt_dma(dd
, dd
->in_sg
, dd
->out_sg
, dd
->in_sg_len
,
375 if (err
&& !dd
->pio_only
) {
376 dma_unmap_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
, DMA_TO_DEVICE
);
377 dma_unmap_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
384 static void omap_aes_finish_req(struct omap_aes_dev
*dd
, int err
)
386 struct ablkcipher_request
*req
= dd
->req
;
388 pr_debug("err: %d\n", err
);
390 crypto_finalize_cipher_request(dd
->engine
, req
, err
);
392 pm_runtime_mark_last_busy(dd
->dev
);
393 pm_runtime_put_autosuspend(dd
->dev
);
396 int omap_aes_crypt_dma_stop(struct omap_aes_dev
*dd
)
398 pr_debug("total: %d\n", dd
->total
);
400 omap_aes_dma_stop(dd
);
406 static int omap_aes_handle_queue(struct omap_aes_dev
*dd
,
407 struct ablkcipher_request
*req
)
410 return crypto_transfer_cipher_request_to_engine(dd
->engine
, req
);
415 static int omap_aes_prepare_req(struct crypto_engine
*engine
,
416 struct ablkcipher_request
*req
)
418 struct omap_aes_ctx
*ctx
= crypto_ablkcipher_ctx(
419 crypto_ablkcipher_reqtfm(req
));
420 struct omap_aes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
421 struct omap_aes_dev
*dd
= rctx
->dd
;
428 /* assign new request to device */
430 dd
->total
= req
->nbytes
;
431 dd
->total_save
= req
->nbytes
;
432 dd
->in_sg
= req
->src
;
433 dd
->out_sg
= req
->dst
;
434 dd
->orig_out
= req
->dst
;
436 flags
= OMAP_CRYPTO_COPY_DATA
;
437 if (req
->src
== req
->dst
)
438 flags
|= OMAP_CRYPTO_FORCE_COPY
;
440 ret
= omap_crypto_align_sg(&dd
->in_sg
, dd
->total
, AES_BLOCK_SIZE
,
442 FLAGS_IN_DATA_ST_SHIFT
, &dd
->flags
);
446 ret
= omap_crypto_align_sg(&dd
->out_sg
, dd
->total
, AES_BLOCK_SIZE
,
448 FLAGS_OUT_DATA_ST_SHIFT
, &dd
->flags
);
452 dd
->in_sg_len
= sg_nents_for_len(dd
->in_sg
, dd
->total
);
453 if (dd
->in_sg_len
< 0)
454 return dd
->in_sg_len
;
456 dd
->out_sg_len
= sg_nents_for_len(dd
->out_sg
, dd
->total
);
457 if (dd
->out_sg_len
< 0)
458 return dd
->out_sg_len
;
460 rctx
->mode
&= FLAGS_MODE_MASK
;
461 dd
->flags
= (dd
->flags
& ~FLAGS_MODE_MASK
) | rctx
->mode
;
466 return omap_aes_write_ctrl(dd
);
469 static int omap_aes_crypt_req(struct crypto_engine
*engine
,
470 struct ablkcipher_request
*req
)
472 struct omap_aes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
473 struct omap_aes_dev
*dd
= rctx
->dd
;
478 return omap_aes_crypt_dma_start(dd
);
481 static void omap_aes_done_task(unsigned long data
)
483 struct omap_aes_dev
*dd
= (struct omap_aes_dev
*)data
;
485 pr_debug("enter done_task\n");
488 dma_sync_sg_for_device(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
490 dma_unmap_sg(dd
->dev
, dd
->in_sg
, dd
->in_sg_len
, DMA_TO_DEVICE
);
491 dma_unmap_sg(dd
->dev
, dd
->out_sg
, dd
->out_sg_len
,
493 omap_aes_crypt_dma_stop(dd
);
496 omap_crypto_cleanup(dd
->in_sgl
, NULL
, 0, dd
->total_save
,
497 FLAGS_IN_DATA_ST_SHIFT
, dd
->flags
);
499 omap_crypto_cleanup(&dd
->out_sgl
, dd
->orig_out
, 0, dd
->total_save
,
500 FLAGS_OUT_DATA_ST_SHIFT
, dd
->flags
);
502 omap_aes_finish_req(dd
, 0);
507 static int omap_aes_crypt(struct ablkcipher_request
*req
, unsigned long mode
)
509 struct omap_aes_ctx
*ctx
= crypto_ablkcipher_ctx(
510 crypto_ablkcipher_reqtfm(req
));
511 struct omap_aes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
512 struct omap_aes_dev
*dd
;
515 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req
->nbytes
,
516 !!(mode
& FLAGS_ENCRYPT
),
517 !!(mode
& FLAGS_CBC
));
519 if (req
->nbytes
< 200) {
520 SKCIPHER_REQUEST_ON_STACK(subreq
, ctx
->fallback
);
522 skcipher_request_set_tfm(subreq
, ctx
->fallback
);
523 skcipher_request_set_callback(subreq
, req
->base
.flags
, NULL
,
525 skcipher_request_set_crypt(subreq
, req
->src
, req
->dst
,
526 req
->nbytes
, req
->info
);
528 if (mode
& FLAGS_ENCRYPT
)
529 ret
= crypto_skcipher_encrypt(subreq
);
531 ret
= crypto_skcipher_decrypt(subreq
);
533 skcipher_request_zero(subreq
);
536 dd
= omap_aes_find_dev(rctx
);
542 return omap_aes_handle_queue(dd
, req
);
545 /* ********************** ALG API ************************************ */
547 static int omap_aes_setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
550 struct omap_aes_ctx
*ctx
= crypto_ablkcipher_ctx(tfm
);
553 if (keylen
!= AES_KEYSIZE_128
&& keylen
!= AES_KEYSIZE_192
&&
554 keylen
!= AES_KEYSIZE_256
)
557 pr_debug("enter, keylen: %d\n", keylen
);
559 memcpy(ctx
->key
, key
, keylen
);
560 ctx
->keylen
= keylen
;
562 crypto_skcipher_clear_flags(ctx
->fallback
, CRYPTO_TFM_REQ_MASK
);
563 crypto_skcipher_set_flags(ctx
->fallback
, tfm
->base
.crt_flags
&
564 CRYPTO_TFM_REQ_MASK
);
566 ret
= crypto_skcipher_setkey(ctx
->fallback
, key
, keylen
);
573 static int omap_aes_ecb_encrypt(struct ablkcipher_request
*req
)
575 return omap_aes_crypt(req
, FLAGS_ENCRYPT
);
578 static int omap_aes_ecb_decrypt(struct ablkcipher_request
*req
)
580 return omap_aes_crypt(req
, 0);
583 static int omap_aes_cbc_encrypt(struct ablkcipher_request
*req
)
585 return omap_aes_crypt(req
, FLAGS_ENCRYPT
| FLAGS_CBC
);
588 static int omap_aes_cbc_decrypt(struct ablkcipher_request
*req
)
590 return omap_aes_crypt(req
, FLAGS_CBC
);
593 static int omap_aes_ctr_encrypt(struct ablkcipher_request
*req
)
595 return omap_aes_crypt(req
, FLAGS_ENCRYPT
| FLAGS_CTR
);
598 static int omap_aes_ctr_decrypt(struct ablkcipher_request
*req
)
600 return omap_aes_crypt(req
, FLAGS_CTR
);
603 static int omap_aes_cra_init(struct crypto_tfm
*tfm
)
605 const char *name
= crypto_tfm_alg_name(tfm
);
606 const u32 flags
= CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
;
607 struct omap_aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
608 struct crypto_skcipher
*blk
;
610 blk
= crypto_alloc_skcipher(name
, 0, flags
);
616 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct omap_aes_reqctx
);
621 static int omap_aes_gcm_cra_init(struct crypto_aead
*tfm
)
623 struct omap_aes_dev
*dd
= NULL
;
624 struct omap_aes_ctx
*ctx
= crypto_aead_ctx(tfm
);
627 /* Find AES device, currently picks the first device */
628 spin_lock_bh(&list_lock
);
629 list_for_each_entry(dd
, &dev_list
, list
) {
632 spin_unlock_bh(&list_lock
);
634 err
= pm_runtime_get_sync(dd
->dev
);
636 dev_err(dd
->dev
, "%s: failed to get_sync(%d)\n",
641 tfm
->reqsize
= sizeof(struct omap_aes_reqctx
);
642 ctx
->ctr
= crypto_alloc_skcipher("ecb(aes)", 0, 0);
643 if (IS_ERR(ctx
->ctr
)) {
644 pr_warn("could not load aes driver for encrypting IV\n");
645 return PTR_ERR(ctx
->ctr
);
651 static void omap_aes_cra_exit(struct crypto_tfm
*tfm
)
653 struct omap_aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
656 crypto_free_skcipher(ctx
->fallback
);
658 ctx
->fallback
= NULL
;
661 static void omap_aes_gcm_cra_exit(struct crypto_aead
*tfm
)
663 struct omap_aes_ctx
*ctx
= crypto_aead_ctx(tfm
);
665 omap_aes_cra_exit(crypto_aead_tfm(tfm
));
668 crypto_free_skcipher(ctx
->ctr
);
671 /* ********************** ALGS ************************************ */
673 static struct crypto_alg algs_ecb_cbc
[] = {
675 .cra_name
= "ecb(aes)",
676 .cra_driver_name
= "ecb-aes-omap",
678 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
679 CRYPTO_ALG_KERN_DRIVER_ONLY
|
680 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
,
681 .cra_blocksize
= AES_BLOCK_SIZE
,
682 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
684 .cra_type
= &crypto_ablkcipher_type
,
685 .cra_module
= THIS_MODULE
,
686 .cra_init
= omap_aes_cra_init
,
687 .cra_exit
= omap_aes_cra_exit
,
688 .cra_u
.ablkcipher
= {
689 .min_keysize
= AES_MIN_KEY_SIZE
,
690 .max_keysize
= AES_MAX_KEY_SIZE
,
691 .setkey
= omap_aes_setkey
,
692 .encrypt
= omap_aes_ecb_encrypt
,
693 .decrypt
= omap_aes_ecb_decrypt
,
697 .cra_name
= "cbc(aes)",
698 .cra_driver_name
= "cbc-aes-omap",
700 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
701 CRYPTO_ALG_KERN_DRIVER_ONLY
|
702 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
,
703 .cra_blocksize
= AES_BLOCK_SIZE
,
704 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
706 .cra_type
= &crypto_ablkcipher_type
,
707 .cra_module
= THIS_MODULE
,
708 .cra_init
= omap_aes_cra_init
,
709 .cra_exit
= omap_aes_cra_exit
,
710 .cra_u
.ablkcipher
= {
711 .min_keysize
= AES_MIN_KEY_SIZE
,
712 .max_keysize
= AES_MAX_KEY_SIZE
,
713 .ivsize
= AES_BLOCK_SIZE
,
714 .setkey
= omap_aes_setkey
,
715 .encrypt
= omap_aes_cbc_encrypt
,
716 .decrypt
= omap_aes_cbc_decrypt
,
721 static struct crypto_alg algs_ctr
[] = {
723 .cra_name
= "ctr(aes)",
724 .cra_driver_name
= "ctr-aes-omap",
726 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
727 CRYPTO_ALG_KERN_DRIVER_ONLY
|
728 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
,
729 .cra_blocksize
= AES_BLOCK_SIZE
,
730 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
732 .cra_type
= &crypto_ablkcipher_type
,
733 .cra_module
= THIS_MODULE
,
734 .cra_init
= omap_aes_cra_init
,
735 .cra_exit
= omap_aes_cra_exit
,
736 .cra_u
.ablkcipher
= {
737 .min_keysize
= AES_MIN_KEY_SIZE
,
738 .max_keysize
= AES_MAX_KEY_SIZE
,
740 .ivsize
= AES_BLOCK_SIZE
,
741 .setkey
= omap_aes_setkey
,
742 .encrypt
= omap_aes_ctr_encrypt
,
743 .decrypt
= omap_aes_ctr_decrypt
,
748 static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc
[] = {
750 .algs_list
= algs_ecb_cbc
,
751 .size
= ARRAY_SIZE(algs_ecb_cbc
),
755 static struct aead_alg algs_aead_gcm
[] = {
758 .cra_name
= "gcm(aes)",
759 .cra_driver_name
= "gcm-aes-omap",
761 .cra_flags
= CRYPTO_ALG_ASYNC
|
762 CRYPTO_ALG_KERN_DRIVER_ONLY
,
764 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
765 .cra_alignmask
= 0xf,
766 .cra_module
= THIS_MODULE
,
768 .init
= omap_aes_gcm_cra_init
,
769 .exit
= omap_aes_gcm_cra_exit
,
771 .maxauthsize
= AES_BLOCK_SIZE
,
772 .setkey
= omap_aes_gcm_setkey
,
773 .encrypt
= omap_aes_gcm_encrypt
,
774 .decrypt
= omap_aes_gcm_decrypt
,
778 .cra_name
= "rfc4106(gcm(aes))",
779 .cra_driver_name
= "rfc4106-gcm-aes-omap",
781 .cra_flags
= CRYPTO_ALG_ASYNC
|
782 CRYPTO_ALG_KERN_DRIVER_ONLY
,
784 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
785 .cra_alignmask
= 0xf,
786 .cra_module
= THIS_MODULE
,
788 .init
= omap_aes_gcm_cra_init
,
789 .exit
= omap_aes_gcm_cra_exit
,
790 .maxauthsize
= AES_BLOCK_SIZE
,
792 .setkey
= omap_aes_4106gcm_setkey
,
793 .encrypt
= omap_aes_4106gcm_encrypt
,
794 .decrypt
= omap_aes_4106gcm_decrypt
,
798 static struct omap_aes_aead_algs omap_aes_aead_info
= {
799 .algs_list
= algs_aead_gcm
,
800 .size
= ARRAY_SIZE(algs_aead_gcm
),
803 static const struct omap_aes_pdata omap_aes_pdata_omap2
= {
804 .algs_info
= omap_aes_algs_info_ecb_cbc
,
805 .algs_info_size
= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc
),
806 .trigger
= omap_aes_dma_trigger_omap2
,
813 .dma_enable_in
= BIT(2),
814 .dma_enable_out
= BIT(3),
823 static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr
[] = {
825 .algs_list
= algs_ecb_cbc
,
826 .size
= ARRAY_SIZE(algs_ecb_cbc
),
829 .algs_list
= algs_ctr
,
830 .size
= ARRAY_SIZE(algs_ctr
),
834 static const struct omap_aes_pdata omap_aes_pdata_omap3
= {
835 .algs_info
= omap_aes_algs_info_ecb_cbc_ctr
,
836 .algs_info_size
= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr
),
837 .trigger
= omap_aes_dma_trigger_omap2
,
844 .dma_enable_in
= BIT(2),
845 .dma_enable_out
= BIT(3),
853 static const struct omap_aes_pdata omap_aes_pdata_omap4
= {
854 .algs_info
= omap_aes_algs_info_ecb_cbc_ctr
,
855 .algs_info_size
= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr
),
856 .aead_algs_info
= &omap_aes_aead_info
,
857 .trigger
= omap_aes_dma_trigger_omap4
,
864 .irq_status_ofs
= 0x8c,
865 .irq_enable_ofs
= 0x90,
866 .dma_enable_in
= BIT(5),
867 .dma_enable_out
= BIT(6),
868 .major_mask
= 0x0700,
870 .minor_mask
= 0x003f,
874 static irqreturn_t
omap_aes_irq(int irq
, void *dev_id
)
876 struct omap_aes_dev
*dd
= dev_id
;
880 status
= omap_aes_read(dd
, AES_REG_IRQ_STATUS(dd
));
881 if (status
& AES_REG_IRQ_DATA_IN
) {
882 omap_aes_write(dd
, AES_REG_IRQ_ENABLE(dd
), 0x0);
886 BUG_ON(_calc_walked(in
) > dd
->in_sg
->length
);
888 src
= sg_virt(dd
->in_sg
) + _calc_walked(in
);
890 for (i
= 0; i
< AES_BLOCK_WORDS
; i
++) {
891 omap_aes_write(dd
, AES_REG_DATA_N(dd
, i
), *src
);
893 scatterwalk_advance(&dd
->in_walk
, 4);
894 if (dd
->in_sg
->length
== _calc_walked(in
)) {
895 dd
->in_sg
= sg_next(dd
->in_sg
);
897 scatterwalk_start(&dd
->in_walk
,
899 src
= sg_virt(dd
->in_sg
) +
907 /* Clear IRQ status */
908 status
&= ~AES_REG_IRQ_DATA_IN
;
909 omap_aes_write(dd
, AES_REG_IRQ_STATUS(dd
), status
);
911 /* Enable DATA_OUT interrupt */
912 omap_aes_write(dd
, AES_REG_IRQ_ENABLE(dd
), 0x4);
914 } else if (status
& AES_REG_IRQ_DATA_OUT
) {
915 omap_aes_write(dd
, AES_REG_IRQ_ENABLE(dd
), 0x0);
919 BUG_ON(_calc_walked(out
) > dd
->out_sg
->length
);
921 dst
= sg_virt(dd
->out_sg
) + _calc_walked(out
);
923 for (i
= 0; i
< AES_BLOCK_WORDS
; i
++) {
924 *dst
= omap_aes_read(dd
, AES_REG_DATA_N(dd
, i
));
925 scatterwalk_advance(&dd
->out_walk
, 4);
926 if (dd
->out_sg
->length
== _calc_walked(out
)) {
927 dd
->out_sg
= sg_next(dd
->out_sg
);
929 scatterwalk_start(&dd
->out_walk
,
931 dst
= sg_virt(dd
->out_sg
) +
939 dd
->total
-= min_t(size_t, AES_BLOCK_SIZE
, dd
->total
);
941 /* Clear IRQ status */
942 status
&= ~AES_REG_IRQ_DATA_OUT
;
943 omap_aes_write(dd
, AES_REG_IRQ_STATUS(dd
), status
);
946 /* All bytes read! */
947 tasklet_schedule(&dd
->done_task
);
949 /* Enable DATA_IN interrupt for next block */
950 omap_aes_write(dd
, AES_REG_IRQ_ENABLE(dd
), 0x2);
956 static const struct of_device_id omap_aes_of_match
[] = {
958 .compatible
= "ti,omap2-aes",
959 .data
= &omap_aes_pdata_omap2
,
962 .compatible
= "ti,omap3-aes",
963 .data
= &omap_aes_pdata_omap3
,
966 .compatible
= "ti,omap4-aes",
967 .data
= &omap_aes_pdata_omap4
,
971 MODULE_DEVICE_TABLE(of
, omap_aes_of_match
);
973 static int omap_aes_get_res_of(struct omap_aes_dev
*dd
,
974 struct device
*dev
, struct resource
*res
)
976 struct device_node
*node
= dev
->of_node
;
977 const struct of_device_id
*match
;
980 match
= of_match_device(of_match_ptr(omap_aes_of_match
), 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");
994 dd
->pdata
= match
->data
;
1000 static const struct of_device_id omap_aes_of_match
[] = {
1004 static int omap_aes_get_res_of(struct omap_aes_dev
*dd
,
1005 struct device
*dev
, struct resource
*res
)
1011 static int omap_aes_get_res_pdev(struct omap_aes_dev
*dd
,
1012 struct platform_device
*pdev
, struct resource
*res
)
1014 struct device
*dev
= &pdev
->dev
;
1018 /* Get the base address */
1019 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1021 dev_err(dev
, "no MEM resource info\n");
1025 memcpy(res
, r
, sizeof(*res
));
1027 /* Only OMAP2/3 can be non-DT */
1028 dd
->pdata
= &omap_aes_pdata_omap2
;
1034 static int omap_aes_probe(struct platform_device
*pdev
)
1036 struct device
*dev
= &pdev
->dev
;
1037 struct omap_aes_dev
*dd
;
1038 struct crypto_alg
*algp
;
1039 struct aead_alg
*aalg
;
1040 struct resource res
;
1041 int err
= -ENOMEM
, i
, j
, irq
= -1;
1044 dd
= devm_kzalloc(dev
, sizeof(struct omap_aes_dev
), GFP_KERNEL
);
1046 dev_err(dev
, "unable to alloc data struct.\n");
1050 platform_set_drvdata(pdev
, dd
);
1052 aead_init_queue(&dd
->aead_queue
, OMAP_AES_QUEUE_LENGTH
);
1054 err
= (dev
->of_node
) ? omap_aes_get_res_of(dd
, dev
, &res
) :
1055 omap_aes_get_res_pdev(dd
, pdev
, &res
);
1059 dd
->io_base
= devm_ioremap_resource(dev
, &res
);
1060 if (IS_ERR(dd
->io_base
)) {
1061 err
= PTR_ERR(dd
->io_base
);
1064 dd
->phys_base
= res
.start
;
1066 pm_runtime_use_autosuspend(dev
);
1067 pm_runtime_set_autosuspend_delay(dev
, DEFAULT_AUTOSUSPEND_DELAY
);
1069 pm_runtime_enable(dev
);
1070 err
= pm_runtime_get_sync(dev
);
1072 dev_err(dev
, "%s: failed to get_sync(%d)\n",
1077 omap_aes_dma_stop(dd
);
1079 reg
= omap_aes_read(dd
, AES_REG_REV(dd
));
1081 pm_runtime_put_sync(dev
);
1083 dev_info(dev
, "OMAP AES hw accel rev: %u.%u\n",
1084 (reg
& dd
->pdata
->major_mask
) >> dd
->pdata
->major_shift
,
1085 (reg
& dd
->pdata
->minor_mask
) >> dd
->pdata
->minor_shift
);
1087 tasklet_init(&dd
->done_task
, omap_aes_done_task
, (unsigned long)dd
);
1089 err
= omap_aes_dma_init(dd
);
1090 if (err
== -EPROBE_DEFER
) {
1092 } else if (err
&& AES_REG_IRQ_STATUS(dd
) && AES_REG_IRQ_ENABLE(dd
)) {
1095 irq
= platform_get_irq(pdev
, 0);
1097 dev_err(dev
, "can't get IRQ resource\n");
1102 err
= devm_request_irq(dev
, irq
, omap_aes_irq
, 0,
1105 dev_err(dev
, "Unable to grab omap-aes IRQ\n");
1110 spin_lock_init(&dd
->lock
);
1112 INIT_LIST_HEAD(&dd
->list
);
1113 spin_lock(&list_lock
);
1114 list_add_tail(&dd
->list
, &dev_list
);
1115 spin_unlock(&list_lock
);
1117 /* Initialize crypto engine */
1118 dd
->engine
= crypto_engine_alloc_init(dev
, 1);
1124 dd
->engine
->prepare_cipher_request
= omap_aes_prepare_req
;
1125 dd
->engine
->cipher_one_request
= omap_aes_crypt_req
;
1126 err
= crypto_engine_start(dd
->engine
);
1130 for (i
= 0; i
< dd
->pdata
->algs_info_size
; i
++) {
1131 if (!dd
->pdata
->algs_info
[i
].registered
) {
1132 for (j
= 0; j
< dd
->pdata
->algs_info
[i
].size
; j
++) {
1133 algp
= &dd
->pdata
->algs_info
[i
].algs_list
[j
];
1135 pr_debug("reg alg: %s\n", algp
->cra_name
);
1136 INIT_LIST_HEAD(&algp
->cra_list
);
1138 err
= crypto_register_alg(algp
);
1142 dd
->pdata
->algs_info
[i
].registered
++;
1147 if (dd
->pdata
->aead_algs_info
&&
1148 !dd
->pdata
->aead_algs_info
->registered
) {
1149 for (i
= 0; i
< dd
->pdata
->aead_algs_info
->size
; i
++) {
1150 aalg
= &dd
->pdata
->aead_algs_info
->algs_list
[i
];
1153 pr_debug("reg alg: %s\n", algp
->cra_name
);
1154 INIT_LIST_HEAD(&algp
->cra_list
);
1156 err
= crypto_register_aead(aalg
);
1160 dd
->pdata
->aead_algs_info
->registered
++;
1166 for (i
= dd
->pdata
->aead_algs_info
->registered
- 1; i
>= 0; i
--) {
1167 aalg
= &dd
->pdata
->aead_algs_info
->algs_list
[i
];
1168 crypto_unregister_aead(aalg
);
1171 for (i
= dd
->pdata
->algs_info_size
- 1; i
>= 0; i
--)
1172 for (j
= dd
->pdata
->algs_info
[i
].registered
- 1; j
>= 0; j
--)
1173 crypto_unregister_alg(
1174 &dd
->pdata
->algs_info
[i
].algs_list
[j
]);
1178 crypto_engine_exit(dd
->engine
);
1180 omap_aes_dma_cleanup(dd
);
1182 tasklet_kill(&dd
->done_task
);
1183 pm_runtime_disable(dev
);
1187 dev_err(dev
, "initialization failed.\n");
1191 static int omap_aes_remove(struct platform_device
*pdev
)
1193 struct omap_aes_dev
*dd
= platform_get_drvdata(pdev
);
1194 struct aead_alg
*aalg
;
1200 spin_lock(&list_lock
);
1201 list_del(&dd
->list
);
1202 spin_unlock(&list_lock
);
1204 for (i
= dd
->pdata
->algs_info_size
- 1; i
>= 0; i
--)
1205 for (j
= dd
->pdata
->algs_info
[i
].registered
- 1; j
>= 0; j
--)
1206 crypto_unregister_alg(
1207 &dd
->pdata
->algs_info
[i
].algs_list
[j
]);
1209 for (i
= dd
->pdata
->aead_algs_info
->size
- 1; i
>= 0; i
--) {
1210 aalg
= &dd
->pdata
->aead_algs_info
->algs_list
[i
];
1211 crypto_unregister_aead(aalg
);
1214 crypto_engine_exit(dd
->engine
);
1216 tasklet_kill(&dd
->done_task
);
1217 omap_aes_dma_cleanup(dd
);
1218 pm_runtime_disable(dd
->dev
);
1224 #ifdef CONFIG_PM_SLEEP
1225 static int omap_aes_suspend(struct device
*dev
)
1227 pm_runtime_put_sync(dev
);
1231 static int omap_aes_resume(struct device
*dev
)
1233 pm_runtime_get_sync(dev
);
1238 static SIMPLE_DEV_PM_OPS(omap_aes_pm_ops
, omap_aes_suspend
, omap_aes_resume
);
1240 static struct platform_driver omap_aes_driver
= {
1241 .probe
= omap_aes_probe
,
1242 .remove
= omap_aes_remove
,
1245 .pm
= &omap_aes_pm_ops
,
1246 .of_match_table
= omap_aes_of_match
,
1250 module_platform_driver(omap_aes_driver
);
1252 MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
1253 MODULE_LICENSE("GPL v2");
1254 MODULE_AUTHOR("Dmitry Kasatkin");