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_INIT BIT(4)
78 #define FLAGS_FAST BIT(5)
79 #define FLAGS_BUSY BIT(6)
82 struct omap_aes_dev
*dd
;
85 u32 key
[AES_KEYSIZE_256
/ sizeof(u32
)];
89 struct omap_aes_reqctx
{
93 #define OMAP_AES_QUEUE_LENGTH 1
94 #define OMAP_AES_CACHE_SIZE 0
97 struct list_head list
;
98 unsigned long phys_base
;
99 void __iomem
*io_base
;
101 struct omap_aes_ctx
*ctx
;
107 struct crypto_queue queue
;
109 struct tasklet_struct done_task
;
110 struct tasklet_struct queue_task
;
112 struct ablkcipher_request
*req
;
114 struct scatterlist
*in_sg
;
116 struct scatterlist
*out_sg
;
124 dma_addr_t dma_addr_in
;
128 dma_addr_t dma_addr_out
;
131 /* keep registered devices data here */
132 static LIST_HEAD(dev_list
);
133 static DEFINE_SPINLOCK(list_lock
);
135 static inline u32
omap_aes_read(struct omap_aes_dev
*dd
, u32 offset
)
137 return __raw_readl(dd
->io_base
+ offset
);
140 static inline void omap_aes_write(struct omap_aes_dev
*dd
, u32 offset
,
143 __raw_writel(value
, dd
->io_base
+ offset
);
146 static inline void omap_aes_write_mask(struct omap_aes_dev
*dd
, u32 offset
,
151 val
= omap_aes_read(dd
, offset
);
154 omap_aes_write(dd
, offset
, val
);
157 static void omap_aes_write_n(struct omap_aes_dev
*dd
, u32 offset
,
158 u32
*value
, int count
)
160 for (; count
--; value
++, offset
+= 4)
161 omap_aes_write(dd
, offset
, *value
);
164 static int omap_aes_wait(struct omap_aes_dev
*dd
, u32 offset
, u32 bit
)
166 unsigned long timeout
= jiffies
+ DEFAULT_TIMEOUT
;
168 while (!(omap_aes_read(dd
, offset
) & bit
)) {
169 if (time_is_before_jiffies(timeout
)) {
170 dev_err(dd
->dev
, "omap-aes timeout\n");
177 static int omap_aes_hw_init(struct omap_aes_dev
*dd
)
180 * clocks are enabled when request starts and disabled when finished.
181 * It may be long delays between requests.
182 * Device might go to off mode to save power.
184 clk_enable(dd
->iclk
);
186 if (!(dd
->flags
& FLAGS_INIT
)) {
187 /* is it necessary to reset before every operation? */
188 omap_aes_write_mask(dd
, AES_REG_MASK
, AES_REG_MASK_SOFTRESET
,
189 AES_REG_MASK_SOFTRESET
);
191 * prevent OCP bus error (SRESP) in case an access to the module
192 * is performed while the module is coming out of soft reset
194 __asm__
__volatile__("nop");
195 __asm__
__volatile__("nop");
197 if (omap_aes_wait(dd
, AES_REG_SYSSTATUS
,
198 AES_REG_SYSSTATUS_RESETDONE
))
201 dd
->flags
|= FLAGS_INIT
;
208 static int omap_aes_write_ctrl(struct omap_aes_dev
*dd
)
214 err
= omap_aes_hw_init(dd
);
219 if (dd
->dma_lch_out
>= 0)
220 val
|= AES_REG_MASK_DMA_OUT_EN
;
221 if (dd
->dma_lch_in
>= 0)
222 val
|= AES_REG_MASK_DMA_IN_EN
;
224 mask
= AES_REG_MASK_DMA_IN_EN
| AES_REG_MASK_DMA_OUT_EN
;
226 omap_aes_write_mask(dd
, AES_REG_MASK
, val
, mask
);
228 key32
= dd
->ctx
->keylen
/ sizeof(u32
);
230 /* it seems a key should always be set even if it has not changed */
231 for (i
= 0; i
< key32
; i
++) {
232 omap_aes_write(dd
, AES_REG_KEY(i
),
233 __le32_to_cpu(dd
->ctx
->key
[i
]));
236 if ((dd
->flags
& FLAGS_CBC
) && dd
->req
->info
)
237 omap_aes_write_n(dd
, AES_REG_IV(0), dd
->req
->info
, 4);
239 val
= FLD_VAL(((dd
->ctx
->keylen
>> 3) - 1), 4, 3);
240 if (dd
->flags
& FLAGS_CBC
)
241 val
|= AES_REG_CTRL_CBC
;
242 if (dd
->flags
& FLAGS_ENCRYPT
)
243 val
|= AES_REG_CTRL_DIRECTION
;
245 mask
= AES_REG_CTRL_CBC
| AES_REG_CTRL_DIRECTION
|
246 AES_REG_CTRL_KEY_SIZE
;
248 omap_aes_write_mask(dd
, AES_REG_CTRL
, val
, mask
);
251 omap_set_dma_dest_params(dd
->dma_lch_in
, 0, OMAP_DMA_AMODE_CONSTANT
,
252 dd
->phys_base
+ AES_REG_DATA
, 0, 4);
254 omap_set_dma_dest_burst_mode(dd
->dma_lch_in
, OMAP_DMA_DATA_BURST_4
);
255 omap_set_dma_src_burst_mode(dd
->dma_lch_in
, OMAP_DMA_DATA_BURST_4
);
258 omap_set_dma_src_params(dd
->dma_lch_out
, 0, OMAP_DMA_AMODE_CONSTANT
,
259 dd
->phys_base
+ AES_REG_DATA
, 0, 4);
261 omap_set_dma_src_burst_mode(dd
->dma_lch_out
, OMAP_DMA_DATA_BURST_4
);
262 omap_set_dma_dest_burst_mode(dd
->dma_lch_out
, OMAP_DMA_DATA_BURST_4
);
267 static struct omap_aes_dev
*omap_aes_find_dev(struct omap_aes_ctx
*ctx
)
269 struct omap_aes_dev
*dd
= NULL
, *tmp
;
271 spin_lock_bh(&list_lock
);
273 list_for_each_entry(tmp
, &dev_list
, list
) {
274 /* FIXME: take fist available aes core */
280 /* already found before */
283 spin_unlock_bh(&list_lock
);
288 static void omap_aes_dma_callback(int lch
, u16 ch_status
, void *data
)
290 struct omap_aes_dev
*dd
= data
;
292 if (ch_status
!= OMAP_DMA_BLOCK_IRQ
) {
293 pr_err("omap-aes DMA error status: 0x%hx\n", ch_status
);
295 dd
->flags
&= ~FLAGS_INIT
; /* request to re-initialize */
296 } else if (lch
== dd
->dma_lch_in
) {
300 /* dma_lch_out - completed */
301 tasklet_schedule(&dd
->done_task
);
304 static int omap_aes_dma_init(struct omap_aes_dev
*dd
)
308 dd
->dma_lch_out
= -1;
311 dd
->buf_in
= (void *)__get_free_pages(GFP_KERNEL
, OMAP_AES_CACHE_SIZE
);
312 dd
->buf_out
= (void *)__get_free_pages(GFP_KERNEL
, OMAP_AES_CACHE_SIZE
);
313 dd
->buflen
= PAGE_SIZE
<< OMAP_AES_CACHE_SIZE
;
314 dd
->buflen
&= ~(AES_BLOCK_SIZE
- 1);
316 if (!dd
->buf_in
|| !dd
->buf_out
) {
317 dev_err(dd
->dev
, "unable to alloc pages.\n");
322 dd
->dma_addr_in
= dma_map_single(dd
->dev
, dd
->buf_in
, dd
->buflen
,
324 if (dma_mapping_error(dd
->dev
, dd
->dma_addr_in
)) {
325 dev_err(dd
->dev
, "dma %d bytes error\n", dd
->buflen
);
330 dd
->dma_addr_out
= dma_map_single(dd
->dev
, dd
->buf_out
, dd
->buflen
,
332 if (dma_mapping_error(dd
->dev
, dd
->dma_addr_out
)) {
333 dev_err(dd
->dev
, "dma %d bytes error\n", dd
->buflen
);
338 err
= omap_request_dma(dd
->dma_in
, "omap-aes-rx",
339 omap_aes_dma_callback
, dd
, &dd
->dma_lch_in
);
341 dev_err(dd
->dev
, "Unable to request DMA channel\n");
344 err
= omap_request_dma(dd
->dma_out
, "omap-aes-tx",
345 omap_aes_dma_callback
, dd
, &dd
->dma_lch_out
);
347 dev_err(dd
->dev
, "Unable to request DMA channel\n");
354 omap_free_dma(dd
->dma_lch_in
);
356 dma_unmap_single(dd
->dev
, dd
->dma_addr_out
, dd
->buflen
,
359 dma_unmap_single(dd
->dev
, dd
->dma_addr_in
, dd
->buflen
, DMA_TO_DEVICE
);
361 free_pages((unsigned long)dd
->buf_out
, OMAP_AES_CACHE_SIZE
);
362 free_pages((unsigned long)dd
->buf_in
, OMAP_AES_CACHE_SIZE
);
365 pr_err("error: %d\n", err
);
369 static void omap_aes_dma_cleanup(struct omap_aes_dev
*dd
)
371 omap_free_dma(dd
->dma_lch_out
);
372 omap_free_dma(dd
->dma_lch_in
);
373 dma_unmap_single(dd
->dev
, dd
->dma_addr_out
, dd
->buflen
,
375 dma_unmap_single(dd
->dev
, dd
->dma_addr_in
, dd
->buflen
, DMA_TO_DEVICE
);
376 free_pages((unsigned long)dd
->buf_out
, OMAP_AES_CACHE_SIZE
);
377 free_pages((unsigned long)dd
->buf_in
, OMAP_AES_CACHE_SIZE
);
380 static void sg_copy_buf(void *buf
, struct scatterlist
*sg
,
381 unsigned int start
, unsigned int nbytes
, int out
)
383 struct scatter_walk walk
;
388 scatterwalk_start(&walk
, sg
);
389 scatterwalk_advance(&walk
, start
);
390 scatterwalk_copychunks(buf
, &walk
, nbytes
, out
);
391 scatterwalk_done(&walk
, out
, 0);
394 static int sg_copy(struct scatterlist
**sg
, size_t *offset
, void *buf
,
395 size_t buflen
, size_t total
, int out
)
397 unsigned int count
, off
= 0;
399 while (buflen
&& total
) {
400 count
= min((*sg
)->length
- *offset
, total
);
401 count
= min(count
, buflen
);
407 * buflen and total are AES_BLOCK_SIZE size aligned,
408 * so count should be also aligned
411 sg_copy_buf(buf
+ off
, *sg
, *offset
, count
, out
);
418 if (*offset
== (*sg
)->length
) {
430 static int omap_aes_crypt_dma(struct crypto_tfm
*tfm
, dma_addr_t dma_addr_in
,
431 dma_addr_t dma_addr_out
, int length
)
433 struct omap_aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
434 struct omap_aes_dev
*dd
= ctx
->dd
;
437 pr_debug("len: %d\n", length
);
439 dd
->dma_size
= length
;
441 if (!(dd
->flags
& FLAGS_FAST
))
442 dma_sync_single_for_device(dd
->dev
, dma_addr_in
, length
,
445 len32
= DIV_ROUND_UP(length
, sizeof(u32
));
448 omap_set_dma_transfer_params(dd
->dma_lch_in
, OMAP_DMA_DATA_TYPE_S32
,
449 len32
, 1, OMAP_DMA_SYNC_PACKET
, dd
->dma_in
,
452 omap_set_dma_src_params(dd
->dma_lch_in
, 0, OMAP_DMA_AMODE_POST_INC
,
456 omap_set_dma_transfer_params(dd
->dma_lch_out
, OMAP_DMA_DATA_TYPE_S32
,
457 len32
, 1, OMAP_DMA_SYNC_PACKET
,
458 dd
->dma_out
, OMAP_DMA_SRC_SYNC
);
460 omap_set_dma_dest_params(dd
->dma_lch_out
, 0, OMAP_DMA_AMODE_POST_INC
,
463 omap_start_dma(dd
->dma_lch_in
);
464 omap_start_dma(dd
->dma_lch_out
);
466 /* start DMA or disable idle mode */
467 omap_aes_write_mask(dd
, AES_REG_MASK
, AES_REG_MASK_START
,
473 static int omap_aes_crypt_dma_start(struct omap_aes_dev
*dd
)
475 struct crypto_tfm
*tfm
= crypto_ablkcipher_tfm(
476 crypto_ablkcipher_reqtfm(dd
->req
));
477 int err
, fast
= 0, in
, out
;
479 dma_addr_t addr_in
, addr_out
;
481 pr_debug("total: %d\n", dd
->total
);
483 if (sg_is_last(dd
->in_sg
) && sg_is_last(dd
->out_sg
)) {
484 /* check for alignment */
485 in
= IS_ALIGNED((u32
)dd
->in_sg
->offset
, sizeof(u32
));
486 out
= IS_ALIGNED((u32
)dd
->out_sg
->offset
, sizeof(u32
));
492 count
= min(dd
->total
, sg_dma_len(dd
->in_sg
));
493 count
= min(count
, sg_dma_len(dd
->out_sg
));
495 if (count
!= dd
->total
) {
496 pr_err("request length != buffer length\n");
502 err
= dma_map_sg(dd
->dev
, dd
->in_sg
, 1, DMA_TO_DEVICE
);
504 dev_err(dd
->dev
, "dma_map_sg() error\n");
508 err
= dma_map_sg(dd
->dev
, dd
->out_sg
, 1, DMA_FROM_DEVICE
);
510 dev_err(dd
->dev
, "dma_map_sg() error\n");
511 dma_unmap_sg(dd
->dev
, dd
->in_sg
, 1, DMA_TO_DEVICE
);
515 addr_in
= sg_dma_address(dd
->in_sg
);
516 addr_out
= sg_dma_address(dd
->out_sg
);
518 dd
->flags
|= FLAGS_FAST
;
521 /* use cache buffers */
522 count
= sg_copy(&dd
->in_sg
, &dd
->in_offset
, dd
->buf_in
,
523 dd
->buflen
, dd
->total
, 0);
525 addr_in
= dd
->dma_addr_in
;
526 addr_out
= dd
->dma_addr_out
;
528 dd
->flags
&= ~FLAGS_FAST
;
534 err
= omap_aes_crypt_dma(tfm
, addr_in
, addr_out
, count
);
536 dma_unmap_sg(dd
->dev
, dd
->in_sg
, 1, DMA_TO_DEVICE
);
537 dma_unmap_sg(dd
->dev
, dd
->out_sg
, 1, DMA_TO_DEVICE
);
543 static void omap_aes_finish_req(struct omap_aes_dev
*dd
, int err
)
545 struct ablkcipher_request
*req
= dd
->req
;
547 pr_debug("err: %d\n", err
);
549 clk_disable(dd
->iclk
);
550 dd
->flags
&= ~FLAGS_BUSY
;
552 req
->base
.complete(&req
->base
, err
);
555 static int omap_aes_crypt_dma_stop(struct omap_aes_dev
*dd
)
560 pr_debug("total: %d\n", dd
->total
);
562 omap_aes_write_mask(dd
, AES_REG_MASK
, 0, AES_REG_MASK_START
);
564 omap_stop_dma(dd
->dma_lch_in
);
565 omap_stop_dma(dd
->dma_lch_out
);
567 if (dd
->flags
& FLAGS_FAST
) {
568 dma_unmap_sg(dd
->dev
, dd
->out_sg
, 1, DMA_FROM_DEVICE
);
569 dma_unmap_sg(dd
->dev
, dd
->in_sg
, 1, DMA_TO_DEVICE
);
571 dma_sync_single_for_device(dd
->dev
, dd
->dma_addr_out
,
572 dd
->dma_size
, DMA_FROM_DEVICE
);
575 count
= sg_copy(&dd
->out_sg
, &dd
->out_offset
, dd
->buf_out
,
576 dd
->buflen
, dd
->dma_size
, 1);
577 if (count
!= dd
->dma_size
) {
579 pr_err("not all data converted: %u\n", count
);
586 static int omap_aes_handle_queue(struct omap_aes_dev
*dd
,
587 struct ablkcipher_request
*req
)
589 struct crypto_async_request
*async_req
, *backlog
;
590 struct omap_aes_ctx
*ctx
;
591 struct omap_aes_reqctx
*rctx
;
595 spin_lock_irqsave(&dd
->lock
, flags
);
597 ret
= ablkcipher_enqueue_request(&dd
->queue
, req
);
598 if (dd
->flags
& FLAGS_BUSY
) {
599 spin_unlock_irqrestore(&dd
->lock
, flags
);
602 backlog
= crypto_get_backlog(&dd
->queue
);
603 async_req
= crypto_dequeue_request(&dd
->queue
);
605 dd
->flags
|= FLAGS_BUSY
;
606 spin_unlock_irqrestore(&dd
->lock
, flags
);
612 backlog
->complete(backlog
, -EINPROGRESS
);
614 req
= ablkcipher_request_cast(async_req
);
616 /* assign new request to device */
618 dd
->total
= req
->nbytes
;
620 dd
->in_sg
= req
->src
;
622 dd
->out_sg
= req
->dst
;
624 rctx
= ablkcipher_request_ctx(req
);
625 ctx
= crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req
));
626 rctx
->mode
&= FLAGS_MODE_MASK
;
627 dd
->flags
= (dd
->flags
& ~FLAGS_MODE_MASK
) | rctx
->mode
;
632 err
= omap_aes_write_ctrl(dd
);
634 err
= omap_aes_crypt_dma_start(dd
);
636 /* aes_task will not finish it, so do it here */
637 omap_aes_finish_req(dd
, err
);
638 tasklet_schedule(&dd
->queue_task
);
641 return ret
; /* return ret, which is enqueue return value */
644 static void omap_aes_done_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
= dd
->err
? : err
;
655 if (dd
->total
&& !err
) {
656 err
= omap_aes_crypt_dma_start(dd
);
658 return; /* DMA started. Not fininishing. */
661 omap_aes_finish_req(dd
, err
);
662 omap_aes_handle_queue(dd
, NULL
);
667 static void omap_aes_queue_task(unsigned long data
)
669 struct omap_aes_dev
*dd
= (struct omap_aes_dev
*)data
;
671 omap_aes_handle_queue(dd
, NULL
);
674 static int omap_aes_crypt(struct ablkcipher_request
*req
, unsigned long mode
)
676 struct omap_aes_ctx
*ctx
= crypto_ablkcipher_ctx(
677 crypto_ablkcipher_reqtfm(req
));
678 struct omap_aes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
679 struct omap_aes_dev
*dd
;
681 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req
->nbytes
,
682 !!(mode
& FLAGS_ENCRYPT
),
683 !!(mode
& FLAGS_CBC
));
685 if (!IS_ALIGNED(req
->nbytes
, AES_BLOCK_SIZE
)) {
686 pr_err("request size is not exact amount of AES blocks\n");
690 dd
= omap_aes_find_dev(ctx
);
696 return omap_aes_handle_queue(dd
, req
);
699 /* ********************** ALG API ************************************ */
701 static int omap_aes_setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
704 struct omap_aes_ctx
*ctx
= crypto_ablkcipher_ctx(tfm
);
706 if (keylen
!= AES_KEYSIZE_128
&& keylen
!= AES_KEYSIZE_192
&&
707 keylen
!= AES_KEYSIZE_256
)
710 pr_debug("enter, keylen: %d\n", keylen
);
712 memcpy(ctx
->key
, key
, keylen
);
713 ctx
->keylen
= keylen
;
718 static int omap_aes_ecb_encrypt(struct ablkcipher_request
*req
)
720 return omap_aes_crypt(req
, FLAGS_ENCRYPT
);
723 static int omap_aes_ecb_decrypt(struct ablkcipher_request
*req
)
725 return omap_aes_crypt(req
, 0);
728 static int omap_aes_cbc_encrypt(struct ablkcipher_request
*req
)
730 return omap_aes_crypt(req
, FLAGS_ENCRYPT
| FLAGS_CBC
);
733 static int omap_aes_cbc_decrypt(struct ablkcipher_request
*req
)
735 return omap_aes_crypt(req
, FLAGS_CBC
);
738 static int omap_aes_cra_init(struct crypto_tfm
*tfm
)
742 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct omap_aes_reqctx
);
747 static void omap_aes_cra_exit(struct crypto_tfm
*tfm
)
752 /* ********************** ALGS ************************************ */
754 static struct crypto_alg algs
[] = {
756 .cra_name
= "ecb(aes)",
757 .cra_driver_name
= "ecb-aes-omap",
759 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
760 CRYPTO_ALG_KERN_DRIVER_ONLY
|
762 .cra_blocksize
= AES_BLOCK_SIZE
,
763 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
765 .cra_type
= &crypto_ablkcipher_type
,
766 .cra_module
= THIS_MODULE
,
767 .cra_init
= omap_aes_cra_init
,
768 .cra_exit
= omap_aes_cra_exit
,
769 .cra_u
.ablkcipher
= {
770 .min_keysize
= AES_MIN_KEY_SIZE
,
771 .max_keysize
= AES_MAX_KEY_SIZE
,
772 .setkey
= omap_aes_setkey
,
773 .encrypt
= omap_aes_ecb_encrypt
,
774 .decrypt
= omap_aes_ecb_decrypt
,
778 .cra_name
= "cbc(aes)",
779 .cra_driver_name
= "cbc-aes-omap",
781 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
782 CRYPTO_ALG_KERN_DRIVER_ONLY
|
784 .cra_blocksize
= AES_BLOCK_SIZE
,
785 .cra_ctxsize
= sizeof(struct omap_aes_ctx
),
787 .cra_type
= &crypto_ablkcipher_type
,
788 .cra_module
= THIS_MODULE
,
789 .cra_init
= omap_aes_cra_init
,
790 .cra_exit
= omap_aes_cra_exit
,
791 .cra_u
.ablkcipher
= {
792 .min_keysize
= AES_MIN_KEY_SIZE
,
793 .max_keysize
= AES_MAX_KEY_SIZE
,
794 .ivsize
= AES_BLOCK_SIZE
,
795 .setkey
= omap_aes_setkey
,
796 .encrypt
= omap_aes_cbc_encrypt
,
797 .decrypt
= omap_aes_cbc_decrypt
,
802 static int omap_aes_probe(struct platform_device
*pdev
)
804 struct device
*dev
= &pdev
->dev
;
805 struct omap_aes_dev
*dd
;
806 struct resource
*res
;
807 int err
= -ENOMEM
, i
, j
;
810 dd
= kzalloc(sizeof(struct omap_aes_dev
), GFP_KERNEL
);
812 dev_err(dev
, "unable to alloc data struct.\n");
816 platform_set_drvdata(pdev
, dd
);
818 spin_lock_init(&dd
->lock
);
819 crypto_init_queue(&dd
->queue
, OMAP_AES_QUEUE_LENGTH
);
821 /* Get the base address */
822 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
824 dev_err(dev
, "invalid resource type\n");
828 dd
->phys_base
= res
->start
;
831 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
833 dev_info(dev
, "no DMA info\n");
835 dd
->dma_out
= res
->start
;
838 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
840 dev_info(dev
, "no DMA info\n");
842 dd
->dma_in
= res
->start
;
844 /* Initializing the clock */
845 dd
->iclk
= clk_get(dev
, "ick");
846 if (IS_ERR(dd
->iclk
)) {
847 dev_err(dev
, "clock intialization failed.\n");
848 err
= PTR_ERR(dd
->iclk
);
852 dd
->io_base
= ioremap(dd
->phys_base
, SZ_4K
);
854 dev_err(dev
, "can't ioremap\n");
859 clk_enable(dd
->iclk
);
860 reg
= omap_aes_read(dd
, AES_REG_REV
);
861 dev_info(dev
, "OMAP AES hw accel rev: %u.%u\n",
862 (reg
& AES_REG_REV_MAJOR
) >> 4, reg
& AES_REG_REV_MINOR
);
863 clk_disable(dd
->iclk
);
865 tasklet_init(&dd
->done_task
, omap_aes_done_task
, (unsigned long)dd
);
866 tasklet_init(&dd
->queue_task
, omap_aes_queue_task
, (unsigned long)dd
);
868 err
= omap_aes_dma_init(dd
);
872 INIT_LIST_HEAD(&dd
->list
);
873 spin_lock(&list_lock
);
874 list_add_tail(&dd
->list
, &dev_list
);
875 spin_unlock(&list_lock
);
877 for (i
= 0; i
< ARRAY_SIZE(algs
); i
++) {
878 pr_debug("i: %d\n", i
);
879 INIT_LIST_HEAD(&algs
[i
].cra_list
);
880 err
= crypto_register_alg(&algs
[i
]);
885 pr_info("probe() done\n");
889 for (j
= 0; j
< i
; j
++)
890 crypto_unregister_alg(&algs
[j
]);
891 omap_aes_dma_cleanup(dd
);
893 tasklet_kill(&dd
->done_task
);
894 tasklet_kill(&dd
->queue_task
);
895 iounmap(dd
->io_base
);
902 dev_err(dev
, "initialization failed.\n");
906 static int omap_aes_remove(struct platform_device
*pdev
)
908 struct omap_aes_dev
*dd
= platform_get_drvdata(pdev
);
914 spin_lock(&list_lock
);
916 spin_unlock(&list_lock
);
918 for (i
= 0; i
< ARRAY_SIZE(algs
); i
++)
919 crypto_unregister_alg(&algs
[i
]);
921 tasklet_kill(&dd
->done_task
);
922 tasklet_kill(&dd
->queue_task
);
923 omap_aes_dma_cleanup(dd
);
924 iounmap(dd
->io_base
);
932 static struct platform_driver omap_aes_driver
= {
933 .probe
= omap_aes_probe
,
934 .remove
= omap_aes_remove
,
937 .owner
= THIS_MODULE
,
941 static int __init
omap_aes_mod_init(void)
943 pr_info("loading %s driver\n", "omap-aes");
945 if (!cpu_class_is_omap2() || omap_type() != OMAP2_DEVICE_TYPE_SEC
) {
946 pr_err("Unsupported cpu\n");
950 return platform_driver_register(&omap_aes_driver
);
953 static void __exit
omap_aes_mod_exit(void)
955 platform_driver_unregister(&omap_aes_driver
);
958 module_init(omap_aes_mod_init
);
959 module_exit(omap_aes_mod_exit
);
961 MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
962 MODULE_LICENSE("GPL v2");
963 MODULE_AUTHOR("Dmitry Kasatkin");