4 * Support for OMAP AES HW acceleration.
6 * Copyright (c) 2010 Nokia Corporation
7 * Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
15 #define pr_fmt(fmt) "%s: " fmt, __func__
17 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/errno.h>
21 #include <linux/kernel.h>
22 #include <linux/clk.h>
23 #include <linux/platform_device.h>
24 #include <linux/scatterlist.h>
25 #include <linux/dma-mapping.h>
27 #include <linux/crypto.h>
28 #include <linux/interrupt.h>
29 #include <crypto/scatterwalk.h>
30 #include <crypto/aes.h>
35 /* OMAP TRM gives bitfields as start:end, where start is the higher bit
36 number. For example 7:0 */
37 #define FLD_MASK(start, end) (((1 << ((start) - (end) + 1)) - 1) << (end))
38 #define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
40 #define AES_REG_KEY(x) (0x1C - ((x ^ 0x01) * 0x04))
41 #define AES_REG_IV(x) (0x20 + ((x) * 0x04))
43 #define AES_REG_CTRL 0x30
44 #define AES_REG_CTRL_CTR_WIDTH (1 << 7)
45 #define AES_REG_CTRL_CTR (1 << 6)
46 #define AES_REG_CTRL_CBC (1 << 5)
47 #define AES_REG_CTRL_KEY_SIZE (3 << 3)
48 #define AES_REG_CTRL_DIRECTION (1 << 2)
49 #define AES_REG_CTRL_INPUT_READY (1 << 1)
50 #define AES_REG_CTRL_OUTPUT_READY (1 << 0)
52 #define AES_REG_DATA 0x34
53 #define AES_REG_DATA_N(x) (0x34 + ((x) * 0x04))
55 #define AES_REG_REV 0x44
56 #define AES_REG_REV_MAJOR 0xF0
57 #define AES_REG_REV_MINOR 0x0F
59 #define AES_REG_MASK 0x48
60 #define AES_REG_MASK_SIDLE (1 << 6)
61 #define AES_REG_MASK_START (1 << 5)
62 #define AES_REG_MASK_DMA_OUT_EN (1 << 3)
63 #define AES_REG_MASK_DMA_IN_EN (1 << 2)
64 #define AES_REG_MASK_SOFTRESET (1 << 1)
65 #define AES_REG_AUTOIDLE (1 << 0)
67 #define AES_REG_SYSSTATUS 0x4C
68 #define AES_REG_SYSSTATUS_RESETDONE (1 << 0)
70 #define DEFAULT_TIMEOUT (5*HZ)
72 #define FLAGS_MODE_MASK 0x000f
73 #define FLAGS_ENCRYPT BIT(0)
74 #define FLAGS_CBC BIT(1)
75 #define FLAGS_GIV BIT(2)
77 #define FLAGS_NEW_KEY BIT(4)
78 #define FLAGS_NEW_IV BIT(5)
79 #define FLAGS_INIT BIT(6)
80 #define FLAGS_FAST BIT(7)
84 struct omap_aes_dev
*dd
;
87 u32 key
[AES_KEYSIZE_256
/ sizeof(u32
)];
91 struct omap_aes_reqctx
{
95 #define OMAP_AES_QUEUE_LENGTH 1
96 #define OMAP_AES_CACHE_SIZE 0
99 struct list_head list
;
100 unsigned long phys_base
;
101 void __iomem
*io_base
;
103 struct omap_aes_ctx
*ctx
;
111 struct crypto_queue queue
;
113 struct tasklet_struct task
;
115 struct ablkcipher_request
*req
;
117 struct scatterlist
*in_sg
;
119 struct scatterlist
*out_sg
;
127 dma_addr_t dma_addr_in
;
131 dma_addr_t dma_addr_out
;
134 /* keep registered devices data here */
135 static LIST_HEAD(dev_list
);
136 static DEFINE_SPINLOCK(list_lock
);
138 static inline u32
omap_aes_read(struct omap_aes_dev
*dd
, u32 offset
)
140 return __raw_readl(dd
->io_base
+ offset
);
143 static inline void omap_aes_write(struct omap_aes_dev
*dd
, u32 offset
,
146 __raw_writel(value
, dd
->io_base
+ offset
);
149 static inline void omap_aes_write_mask(struct omap_aes_dev
*dd
, u32 offset
,
154 val
= omap_aes_read(dd
, offset
);
157 omap_aes_write(dd
, offset
, val
);
160 static void omap_aes_write_n(struct omap_aes_dev
*dd
, u32 offset
,
161 u32
*value
, int count
)
163 for (; count
--; value
++, offset
+= 4)
164 omap_aes_write(dd
, offset
, *value
);
167 static int omap_aes_wait(struct omap_aes_dev
*dd
, u32 offset
, u32 bit
)
169 unsigned long timeout
= jiffies
+ DEFAULT_TIMEOUT
;
171 while (!(omap_aes_read(dd
, offset
) & bit
)) {
172 if (time_is_before_jiffies(timeout
)) {
173 dev_err(dd
->dev
, "omap-aes timeout\n");
180 static int omap_aes_hw_init(struct omap_aes_dev
*dd
)
184 clk_enable(dd
->iclk
);
185 if (!(dd
->flags
& FLAGS_INIT
)) {
186 /* is it necessary to reset before every operation? */
187 omap_aes_write_mask(dd
, AES_REG_MASK
, AES_REG_MASK_SOFTRESET
,
188 AES_REG_MASK_SOFTRESET
);
190 * prevent OCP bus error (SRESP) in case an access to the module
191 * is performed while the module is coming out of soft reset
193 __asm__
__volatile__("nop");
194 __asm__
__volatile__("nop");
196 err
= omap_aes_wait(dd
, AES_REG_SYSSTATUS
,
197 AES_REG_SYSSTATUS_RESETDONE
);
199 dd
->flags
|= FLAGS_INIT
;
205 static void omap_aes_hw_cleanup(struct omap_aes_dev
*dd
)
207 clk_disable(dd
->iclk
);
210 static void omap_aes_write_ctrl(struct omap_aes_dev
*dd
)
216 val
= FLD_VAL(((dd
->ctx
->keylen
>> 3) - 1), 4, 3);
217 if (dd
->flags
& FLAGS_CBC
)
218 val
|= AES_REG_CTRL_CBC
;
219 if (dd
->flags
& FLAGS_ENCRYPT
)
220 val
|= AES_REG_CTRL_DIRECTION
;
222 if (dd
->ctrl
== val
&& !(dd
->flags
& FLAGS_NEW_IV
) &&
223 !(dd
->ctx
->flags
& FLAGS_NEW_KEY
))
226 /* only need to write control registers for new settings */
231 if (dd
->dma_lch_out
>= 0)
232 val
|= AES_REG_MASK_DMA_OUT_EN
;
233 if (dd
->dma_lch_in
>= 0)
234 val
|= AES_REG_MASK_DMA_IN_EN
;
236 mask
= AES_REG_MASK_DMA_IN_EN
| AES_REG_MASK_DMA_OUT_EN
;
238 omap_aes_write_mask(dd
, AES_REG_MASK
, val
, mask
);
240 pr_debug("Set key\n");
241 key32
= dd
->ctx
->keylen
/ sizeof(u32
);
243 for (i
= 0; i
< key32
; i
++) {
244 omap_aes_write(dd
, AES_REG_KEY(i
),
245 __le32_to_cpu(dd
->ctx
->key
[i
]));
247 dd
->ctx
->flags
&= ~FLAGS_NEW_KEY
;
249 if (dd
->flags
& FLAGS_NEW_IV
) {
250 pr_debug("Set IV\n");
251 omap_aes_write_n(dd
, AES_REG_IV(0), dd
->iv
, 4);
252 dd
->flags
&= ~FLAGS_NEW_IV
;
255 mask
= AES_REG_CTRL_CBC
| AES_REG_CTRL_DIRECTION
|
256 AES_REG_CTRL_KEY_SIZE
;
258 omap_aes_write_mask(dd
, AES_REG_CTRL
, dd
->ctrl
, mask
);
261 /* start DMA or disable idle mode */
262 omap_aes_write_mask(dd
, AES_REG_MASK
, AES_REG_MASK_START
,
266 static struct omap_aes_dev
*omap_aes_find_dev(struct omap_aes_ctx
*ctx
)
268 struct omap_aes_dev
*dd
= NULL
, *tmp
;
270 spin_lock_bh(&list_lock
);
272 list_for_each_entry(tmp
, &dev_list
, list
) {
273 /* FIXME: take fist available aes core */
279 /* already found before */
282 spin_unlock_bh(&list_lock
);
287 static void omap_aes_dma_callback(int lch
, u16 ch_status
, void *data
)
289 struct omap_aes_dev
*dd
= data
;
291 if (lch
== dd
->dma_lch_out
)
292 tasklet_schedule(&dd
->task
);
295 static int omap_aes_dma_init(struct omap_aes_dev
*dd
)
299 dd
->dma_lch_out
= -1;
302 dd
->buf_in
= (void *)__get_free_pages(GFP_KERNEL
, OMAP_AES_CACHE_SIZE
);
303 dd
->buf_out
= (void *)__get_free_pages(GFP_KERNEL
, OMAP_AES_CACHE_SIZE
);
304 dd
->buflen
= PAGE_SIZE
<< OMAP_AES_CACHE_SIZE
;
305 dd
->buflen
&= ~(AES_BLOCK_SIZE
- 1);
307 if (!dd
->buf_in
|| !dd
->buf_out
) {
308 dev_err(dd
->dev
, "unable to alloc pages.\n");
313 dd
->dma_addr_in
= dma_map_single(dd
->dev
, dd
->buf_in
, dd
->buflen
,
315 if (dma_mapping_error(dd
->dev
, dd
->dma_addr_in
)) {
316 dev_err(dd
->dev
, "dma %d bytes error\n", dd
->buflen
);
321 dd
->dma_addr_out
= dma_map_single(dd
->dev
, dd
->buf_out
, dd
->buflen
,
323 if (dma_mapping_error(dd
->dev
, dd
->dma_addr_out
)) {
324 dev_err(dd
->dev
, "dma %d bytes error\n", dd
->buflen
);
329 err
= omap_request_dma(dd
->dma_in
, "omap-aes-rx",
330 omap_aes_dma_callback
, dd
, &dd
->dma_lch_in
);
332 dev_err(dd
->dev
, "Unable to request DMA channel\n");
335 err
= omap_request_dma(dd
->dma_out
, "omap-aes-tx",
336 omap_aes_dma_callback
, dd
, &dd
->dma_lch_out
);
338 dev_err(dd
->dev
, "Unable to request DMA channel\n");
342 omap_set_dma_dest_params(dd
->dma_lch_in
, 0, OMAP_DMA_AMODE_CONSTANT
,
343 dd
->phys_base
+ AES_REG_DATA
, 0, 4);
345 omap_set_dma_dest_burst_mode(dd
->dma_lch_in
, OMAP_DMA_DATA_BURST_4
);
346 omap_set_dma_src_burst_mode(dd
->dma_lch_in
, OMAP_DMA_DATA_BURST_4
);
348 omap_set_dma_src_params(dd
->dma_lch_out
, 0, OMAP_DMA_AMODE_CONSTANT
,
349 dd
->phys_base
+ AES_REG_DATA
, 0, 4);
351 omap_set_dma_src_burst_mode(dd
->dma_lch_out
, OMAP_DMA_DATA_BURST_4
);
352 omap_set_dma_dest_burst_mode(dd
->dma_lch_out
, OMAP_DMA_DATA_BURST_4
);
357 omap_free_dma(dd
->dma_lch_in
);
359 dma_unmap_single(dd
->dev
, dd
->dma_addr_out
, dd
->buflen
,
362 dma_unmap_single(dd
->dev
, dd
->dma_addr_in
, dd
->buflen
, DMA_TO_DEVICE
);
364 free_pages((unsigned long)dd
->buf_out
, OMAP_AES_CACHE_SIZE
);
365 free_pages((unsigned long)dd
->buf_in
, OMAP_AES_CACHE_SIZE
);
368 pr_err("error: %d\n", err
);
372 static void omap_aes_dma_cleanup(struct omap_aes_dev
*dd
)
374 omap_free_dma(dd
->dma_lch_out
);
375 omap_free_dma(dd
->dma_lch_in
);
376 dma_unmap_single(dd
->dev
, dd
->dma_addr_out
, dd
->buflen
,
378 dma_unmap_single(dd
->dev
, dd
->dma_addr_in
, dd
->buflen
, DMA_TO_DEVICE
);
379 free_pages((unsigned long)dd
->buf_out
, OMAP_AES_CACHE_SIZE
);
380 free_pages((unsigned long)dd
->buf_in
, OMAP_AES_CACHE_SIZE
);
383 static void sg_copy_buf(void *buf
, struct scatterlist
*sg
,
384 unsigned int start
, unsigned int nbytes
, int out
)
386 struct scatter_walk walk
;
391 scatterwalk_start(&walk
, sg
);
392 scatterwalk_advance(&walk
, start
);
393 scatterwalk_copychunks(buf
, &walk
, nbytes
, out
);
394 scatterwalk_done(&walk
, out
, 0);
397 static int sg_copy(struct scatterlist
**sg
, size_t *offset
, void *buf
,
398 size_t buflen
, size_t total
, int out
)
400 unsigned int count
, off
= 0;
402 while (buflen
&& total
) {
403 count
= min((*sg
)->length
- *offset
, total
);
404 count
= min(count
, buflen
);
409 sg_copy_buf(buf
+ off
, *sg
, *offset
, count
, out
);
416 if (*offset
== (*sg
)->length
) {
428 static int omap_aes_crypt_dma(struct crypto_tfm
*tfm
, dma_addr_t dma_addr_in
,
429 dma_addr_t dma_addr_out
, int length
)
431 struct omap_aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
432 struct omap_aes_dev
*dd
= ctx
->dd
;
435 pr_debug("len: %d\n", length
);
437 dd
->dma_size
= length
;
439 if (!(dd
->flags
& FLAGS_FAST
))
440 dma_sync_single_for_device(dd
->dev
, dma_addr_in
, length
,
443 len32
= DIV_ROUND_UP(length
, sizeof(u32
));
446 omap_set_dma_transfer_params(dd
->dma_lch_in
, OMAP_DMA_DATA_TYPE_S32
,
447 len32
, 1, OMAP_DMA_SYNC_PACKET
, dd
->dma_in
,
450 omap_set_dma_src_params(dd
->dma_lch_in
, 0, OMAP_DMA_AMODE_POST_INC
,
454 omap_set_dma_transfer_params(dd
->dma_lch_out
, OMAP_DMA_DATA_TYPE_S32
,
455 len32
, 1, OMAP_DMA_SYNC_PACKET
,
456 dd
->dma_out
, OMAP_DMA_SRC_SYNC
);
458 omap_set_dma_dest_params(dd
->dma_lch_out
, 0, OMAP_DMA_AMODE_POST_INC
,
461 omap_start_dma(dd
->dma_lch_in
);
462 omap_start_dma(dd
->dma_lch_out
);
464 omap_aes_write_ctrl(dd
);
469 static int omap_aes_crypt_dma_start(struct omap_aes_dev
*dd
)
471 struct crypto_tfm
*tfm
= crypto_ablkcipher_tfm(
472 crypto_ablkcipher_reqtfm(dd
->req
));
473 int err
, fast
= 0, in
, out
;
475 dma_addr_t addr_in
, addr_out
;
477 pr_debug("total: %d\n", dd
->total
);
479 if (sg_is_last(dd
->in_sg
) && sg_is_last(dd
->out_sg
)) {
480 /* check for alignment */
481 in
= IS_ALIGNED((u32
)dd
->in_sg
->offset
, sizeof(u32
));
482 out
= IS_ALIGNED((u32
)dd
->out_sg
->offset
, sizeof(u32
));
488 count
= min(dd
->total
, sg_dma_len(dd
->in_sg
));
489 count
= min(count
, sg_dma_len(dd
->out_sg
));
491 if (count
!= dd
->total
)
496 err
= dma_map_sg(dd
->dev
, dd
->in_sg
, 1, DMA_TO_DEVICE
);
498 dev_err(dd
->dev
, "dma_map_sg() error\n");
502 err
= dma_map_sg(dd
->dev
, dd
->out_sg
, 1, DMA_FROM_DEVICE
);
504 dev_err(dd
->dev
, "dma_map_sg() error\n");
505 dma_unmap_sg(dd
->dev
, dd
->in_sg
, 1, DMA_TO_DEVICE
);
509 addr_in
= sg_dma_address(dd
->in_sg
);
510 addr_out
= sg_dma_address(dd
->out_sg
);
512 dd
->flags
|= FLAGS_FAST
;
515 /* use cache buffers */
516 count
= sg_copy(&dd
->in_sg
, &dd
->in_offset
, dd
->buf_in
,
517 dd
->buflen
, dd
->total
, 0);
519 addr_in
= dd
->dma_addr_in
;
520 addr_out
= dd
->dma_addr_out
;
522 dd
->flags
&= ~FLAGS_FAST
;
528 err
= omap_aes_hw_init(dd
);
530 err
= omap_aes_crypt_dma(tfm
, addr_in
, addr_out
, count
);
535 static void omap_aes_finish_req(struct omap_aes_dev
*dd
, int err
)
537 struct omap_aes_ctx
*ctx
;
539 pr_debug("err: %d\n", err
);
541 ctx
= crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(dd
->req
));
544 dd
->req
->base
.complete(&dd
->req
->base
, err
);
547 static int omap_aes_crypt_dma_stop(struct omap_aes_dev
*dd
)
552 pr_debug("total: %d\n", dd
->total
);
554 omap_aes_write_mask(dd
, AES_REG_MASK
, 0, AES_REG_MASK_START
);
556 omap_aes_hw_cleanup(dd
);
558 omap_stop_dma(dd
->dma_lch_in
);
559 omap_stop_dma(dd
->dma_lch_out
);
561 if (dd
->flags
& FLAGS_FAST
) {
562 dma_unmap_sg(dd
->dev
, dd
->out_sg
, 1, DMA_FROM_DEVICE
);
563 dma_unmap_sg(dd
->dev
, dd
->in_sg
, 1, DMA_TO_DEVICE
);
565 dma_sync_single_for_device(dd
->dev
, dd
->dma_addr_out
,
566 dd
->dma_size
, DMA_FROM_DEVICE
);
569 count
= sg_copy(&dd
->out_sg
, &dd
->out_offset
, dd
->buf_out
,
570 dd
->buflen
, dd
->dma_size
, 1);
571 if (count
!= dd
->dma_size
) {
573 pr_err("not all data converted: %u\n", count
);
577 if (err
|| !dd
->total
)
578 omap_aes_finish_req(dd
, err
);
583 static int omap_aes_handle_req(struct omap_aes_dev
*dd
)
585 struct crypto_async_request
*async_req
, *backlog
;
586 struct omap_aes_ctx
*ctx
;
587 struct omap_aes_reqctx
*rctx
;
588 struct ablkcipher_request
*req
;
594 spin_lock_irqsave(&dd
->lock
, flags
);
595 backlog
= crypto_get_backlog(&dd
->queue
);
596 async_req
= crypto_dequeue_request(&dd
->queue
);
598 clear_bit(FLAGS_BUSY
, &dd
->flags
);
599 spin_unlock_irqrestore(&dd
->lock
, flags
);
605 backlog
->complete(backlog
, -EINPROGRESS
);
607 req
= ablkcipher_request_cast(async_req
);
609 pr_debug("get new req\n");
611 /* assign new request to device */
613 dd
->total
= req
->nbytes
;
615 dd
->in_sg
= req
->src
;
617 dd
->out_sg
= req
->dst
;
619 rctx
= ablkcipher_request_ctx(req
);
620 ctx
= crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req
));
621 rctx
->mode
&= FLAGS_MODE_MASK
;
622 dd
->flags
= (dd
->flags
& ~FLAGS_MODE_MASK
) | rctx
->mode
;
625 if ((dd
->flags
& FLAGS_CBC
) && dd
->iv
)
626 dd
->flags
|= FLAGS_NEW_IV
;
628 dd
->flags
&= ~FLAGS_NEW_IV
;
631 if (dd
->ctx
!= ctx
) {
632 /* assign new context to device */
634 ctx
->flags
|= FLAGS_NEW_KEY
;
637 if (!IS_ALIGNED(req
->nbytes
, AES_BLOCK_SIZE
))
638 pr_err("request size is not exact amount of AES blocks\n");
641 return omap_aes_crypt_dma_start(dd
);
644 static void omap_aes_task(unsigned long data
)
646 struct omap_aes_dev
*dd
= (struct omap_aes_dev
*)data
;
651 err
= omap_aes_crypt_dma_stop(dd
);
653 err
= omap_aes_handle_req(dd
);
658 static int omap_aes_crypt(struct ablkcipher_request
*req
, unsigned long mode
)
660 struct omap_aes_ctx
*ctx
= crypto_ablkcipher_ctx(
661 crypto_ablkcipher_reqtfm(req
));
662 struct omap_aes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
663 struct omap_aes_dev
*dd
;
667 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req
->nbytes
,
668 !!(mode
& FLAGS_ENCRYPT
),
669 !!(mode
& FLAGS_CBC
));
671 dd
= omap_aes_find_dev(ctx
);
677 spin_lock_irqsave(&dd
->lock
, flags
);
678 err
= ablkcipher_enqueue_request(&dd
->queue
, req
);
679 spin_unlock_irqrestore(&dd
->lock
, flags
);
681 if (!test_and_set_bit(FLAGS_BUSY
, &dd
->flags
))
682 omap_aes_handle_req(dd
);
689 /* ********************** ALG API ************************************ */
691 static int omap_aes_setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
694 struct omap_aes_ctx
*ctx
= crypto_ablkcipher_ctx(tfm
);
696 if (keylen
!= AES_KEYSIZE_128
&& keylen
!= AES_KEYSIZE_192
&&
697 keylen
!= AES_KEYSIZE_256
)
700 pr_debug("enter, keylen: %d\n", keylen
);
702 memcpy(ctx
->key
, key
, keylen
);
703 ctx
->keylen
= keylen
;
704 ctx
->flags
|= FLAGS_NEW_KEY
;
709 static int omap_aes_ecb_encrypt(struct ablkcipher_request
*req
)
711 return omap_aes_crypt(req
, FLAGS_ENCRYPT
);
714 static int omap_aes_ecb_decrypt(struct ablkcipher_request
*req
)
716 return omap_aes_crypt(req
, 0);
719 static int omap_aes_cbc_encrypt(struct ablkcipher_request
*req
)
721 return omap_aes_crypt(req
, FLAGS_ENCRYPT
| FLAGS_CBC
);
724 static int omap_aes_cbc_decrypt(struct ablkcipher_request
*req
)
726 return omap_aes_crypt(req
, FLAGS_CBC
);
729 static int omap_aes_cra_init(struct crypto_tfm
*tfm
)
733 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct omap_aes_reqctx
);
738 static void omap_aes_cra_exit(struct crypto_tfm
*tfm
)
743 /* ********************** ALGS ************************************ */
745 static struct crypto_alg algs
[] = {
747 .cra_name
= "ecb(aes)",
748 .cra_driver_name
= "ecb-aes-omap",
750 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
751 .cra_blocksize
= AES_BLOCK_SIZE
,
752 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
754 .cra_type
= &crypto_ablkcipher_type
,
755 .cra_module
= THIS_MODULE
,
756 .cra_init
= omap_aes_cra_init
,
757 .cra_exit
= omap_aes_cra_exit
,
758 .cra_u
.ablkcipher
= {
759 .min_keysize
= AES_MIN_KEY_SIZE
,
760 .max_keysize
= AES_MAX_KEY_SIZE
,
761 .setkey
= omap_aes_setkey
,
762 .encrypt
= omap_aes_ecb_encrypt
,
763 .decrypt
= omap_aes_ecb_decrypt
,
767 .cra_name
= "cbc(aes)",
768 .cra_driver_name
= "cbc-aes-omap",
770 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
771 .cra_blocksize
= AES_BLOCK_SIZE
,
772 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
774 .cra_type
= &crypto_ablkcipher_type
,
775 .cra_module
= THIS_MODULE
,
776 .cra_init
= omap_aes_cra_init
,
777 .cra_exit
= omap_aes_cra_exit
,
778 .cra_u
.ablkcipher
= {
779 .min_keysize
= AES_MIN_KEY_SIZE
,
780 .max_keysize
= AES_MAX_KEY_SIZE
,
781 .ivsize
= AES_BLOCK_SIZE
,
782 .setkey
= omap_aes_setkey
,
783 .encrypt
= omap_aes_cbc_encrypt
,
784 .decrypt
= omap_aes_cbc_decrypt
,
789 static int omap_aes_probe(struct platform_device
*pdev
)
791 struct device
*dev
= &pdev
->dev
;
792 struct omap_aes_dev
*dd
;
793 struct resource
*res
;
794 int err
= -ENOMEM
, i
, j
;
797 dd
= kzalloc(sizeof(struct omap_aes_dev
), GFP_KERNEL
);
799 dev_err(dev
, "unable to alloc data struct.\n");
803 platform_set_drvdata(pdev
, dd
);
805 spin_lock_init(&dd
->lock
);
806 crypto_init_queue(&dd
->queue
, OMAP_AES_QUEUE_LENGTH
);
808 /* Get the base address */
809 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
811 dev_err(dev
, "invalid resource type\n");
815 dd
->phys_base
= res
->start
;
818 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
820 dev_info(dev
, "no DMA info\n");
822 dd
->dma_out
= res
->start
;
825 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
827 dev_info(dev
, "no DMA info\n");
829 dd
->dma_in
= res
->start
;
831 /* Initializing the clock */
832 dd
->iclk
= clk_get(dev
, "ick");
834 dev_err(dev
, "clock intialization failed.\n");
839 dd
->io_base
= ioremap(dd
->phys_base
, SZ_4K
);
841 dev_err(dev
, "can't ioremap\n");
846 clk_enable(dd
->iclk
);
847 reg
= omap_aes_read(dd
, AES_REG_REV
);
848 dev_info(dev
, "OMAP AES hw accel rev: %u.%u\n",
849 (reg
& AES_REG_REV_MAJOR
) >> 4, reg
& AES_REG_REV_MINOR
);
850 clk_disable(dd
->iclk
);
852 tasklet_init(&dd
->task
, omap_aes_task
, (unsigned long)dd
);
854 err
= omap_aes_dma_init(dd
);
858 INIT_LIST_HEAD(&dd
->list
);
859 spin_lock(&list_lock
);
860 list_add_tail(&dd
->list
, &dev_list
);
861 spin_unlock(&list_lock
);
863 for (i
= 0; i
< ARRAY_SIZE(algs
); i
++) {
864 pr_debug("i: %d\n", i
);
865 INIT_LIST_HEAD(&algs
[i
].cra_list
);
866 err
= crypto_register_alg(&algs
[i
]);
871 pr_info("probe() done\n");
875 for (j
= 0; j
< i
; j
++)
876 crypto_unregister_alg(&algs
[j
]);
877 omap_aes_dma_cleanup(dd
);
879 tasklet_kill(&dd
->task
);
880 iounmap(dd
->io_base
);
887 dev_err(dev
, "initialization failed.\n");
891 static int omap_aes_remove(struct platform_device
*pdev
)
893 struct omap_aes_dev
*dd
= platform_get_drvdata(pdev
);
899 spin_lock(&list_lock
);
901 spin_unlock(&list_lock
);
903 for (i
= 0; i
< ARRAY_SIZE(algs
); i
++)
904 crypto_unregister_alg(&algs
[i
]);
906 tasklet_kill(&dd
->task
);
907 omap_aes_dma_cleanup(dd
);
908 iounmap(dd
->io_base
);
916 static struct platform_driver omap_aes_driver
= {
917 .probe
= omap_aes_probe
,
918 .remove
= omap_aes_remove
,
921 .owner
= THIS_MODULE
,
925 static int __init
omap_aes_mod_init(void)
927 pr_info("loading %s driver\n", "omap-aes");
929 if (!cpu_class_is_omap2() || omap_type() != OMAP2_DEVICE_TYPE_SEC
) {
930 pr_err("Unsupported cpu\n");
934 return platform_driver_register(&omap_aes_driver
);
937 static void __exit
omap_aes_mod_exit(void)
939 platform_driver_unregister(&omap_aes_driver
);
942 module_init(omap_aes_mod_init
);
943 module_exit(omap_aes_mod_exit
);
945 MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
946 MODULE_LICENSE("GPL v2");
947 MODULE_AUTHOR("Dmitry Kasatkin");