17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
26 #include <linux/device.h>
28 #include <linux/errno.h>
38 #include <crypto/aes.h>
44 #define CFB8_BLOCK_SIZE 1
45 #define CFB16_BLOCK_SIZE 2
46 #define CFB32_BLOCK_SIZE 4
47 #define CFB64_BLOCK_SIZE 8
50 #define AES_FLAGS_MODE_MASK 0x01ff
51 #define AES_FLAGS_ENCRYPT BIT(0)
52 #define AES_FLAGS_CBC BIT(1)
53 #define AES_FLAGS_CFB BIT(2)
54 #define AES_FLAGS_CFB8 BIT(3)
55 #define AES_FLAGS_CFB16 BIT(4)
56 #define AES_FLAGS_CFB32 BIT(5)
57 #define AES_FLAGS_CFB64 BIT(6)
58 #define AES_FLAGS_OFB BIT(7)
59 #define AES_FLAGS_CTR BIT(8)
61 #define AES_FLAGS_INIT BIT(16)
62 #define AES_FLAGS_DMA BIT(17)
63 #define AES_FLAGS_BUSY BIT(18)
65 #define AES_FLAGS_DUALBUFF BIT(24)
67 #define ATMEL_AES_QUEUE_LENGTH 1
68 #define ATMEL_AES_CACHE_SIZE 0
70 #define ATMEL_AES_DMA_THRESHOLD 16
145 unsigned int total = req->
nbytes;
176 writel_relaxed(value, dd->
io_base + offset);
182 for (; count--; value++, offset += 4)
183 *value = atmel_aes_read(dd, offset);
187 u32 *value,
int count)
189 for (; count--; value++, offset += 4)
190 atmel_aes_write(dd, offset, *value);
193 static void atmel_aes_dualbuff_test(
struct atmel_aes_dev *dd)
206 spin_lock_bh(&atmel_aes.
lock);
217 spin_unlock_bh(&atmel_aes.
lock);
224 clk_prepare_enable(dd->
iclk);
228 atmel_aes_dualbuff_test(dd);
236 static void atmel_aes_hw_version_init(
struct atmel_aes_dev *dd)
238 atmel_aes_hw_init(dd);
242 clk_disable_unprepare(dd->
iclk);
249 clk_disable_unprepare(dd->
iclk);
252 req->
base.complete(&req->
base, err);
255 static void atmel_aes_dma_callback(
void *
data)
266 int nb_dma_sg_in, nb_dma_sg_out;
302 out_desc->
callback = atmel_aes_dma_callback;
307 dmaengine_submit(out_desc);
310 dmaengine_submit(in_desc);
325 static int atmel_aes_crypt_cpu_start(
struct atmel_aes_dev *dd)
339 dd->buf_in, dd->
total);
353 static int atmel_aes_crypt_dma_start(
struct atmel_aes_dev *dd)
378 err = atmel_aes_crypt_dma(dd);
386 u32 valcr = 0, valmr = 0;
388 err = atmel_aes_hw_init(dd);
432 atmel_aes_write(dd,
AES_CR, valcr);
433 atmel_aes_write(dd,
AES_MR, valmr);
436 dd->
ctx->keylen >> 2);
441 atmel_aes_write_n(dd,
AES_IVR(0), dd->
req->info, 4);
458 ret = ablkcipher_enqueue_request(&dd->
queue, req);
460 spin_unlock_irqrestore(&dd->
lock, flags);
463 backlog = crypto_get_backlog(&dd->
queue);
467 spin_unlock_irqrestore(&dd->
lock, flags);
475 req = ablkcipher_request_cast(async_req);
483 rctx = ablkcipher_request_ctx(req);
484 ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
490 err = atmel_aes_write_ctrl(dd);
493 err = atmel_aes_crypt_dma_start(dd);
495 err = atmel_aes_crypt_cpu_start(dd);
499 atmel_aes_finish_req(dd, err);
506 static int atmel_aes_crypt_dma_stop(
struct atmel_aes_dev *dd)
524 crypto_ablkcipher_reqtfm(req));
529 pr_err(
"request size is not exact amount of AES blocks\n");
533 dd = atmel_aes_find_dev(ctx);
539 return atmel_aes_handle_queue(dd, req);
560 pdata = dd->
dev->platform_data;
562 if (pdata && pdata->
dma_slave->txdata.dma_dev &&
570 atmel_aes_filter, &pdata->
dma_slave->rxdata);
584 atmel_aes_filter, &pdata->
dma_slave->txdata);
631 return atmel_aes_crypt(req,
637 return atmel_aes_crypt(req,
643 return atmel_aes_crypt(req,
649 return atmel_aes_crypt(req,
655 return atmel_aes_crypt(req,
661 return atmel_aes_crypt(req,
667 return atmel_aes_crypt(req,
673 return atmel_aes_crypt(req,
679 return atmel_aes_crypt(req,
685 return atmel_aes_crypt(req,
691 return atmel_aes_crypt(req,
697 return atmel_aes_crypt(req,
703 return atmel_aes_crypt(req,
709 return atmel_aes_crypt(req,
715 return atmel_aes_crypt(req,
721 return atmel_aes_crypt(req,
727 return atmel_aes_crypt(req,
733 return atmel_aes_crypt(req,
737 static int atmel_aes_cra_init(
struct crypto_tfm *tfm)
744 static void atmel_aes_cra_exit(
struct crypto_tfm *tfm)
750 .cra_name =
"ecb(aes)",
751 .cra_driver_name =
"atmel-ecb-aes",
756 .cra_alignmask = 0x0,
759 .cra_init = atmel_aes_cra_init,
760 .cra_exit = atmel_aes_cra_exit,
761 .cra_u.ablkcipher = {
764 .setkey = atmel_aes_setkey,
765 .encrypt = atmel_aes_ecb_encrypt,
766 .decrypt = atmel_aes_ecb_decrypt,
770 .cra_name =
"cbc(aes)",
771 .cra_driver_name =
"atmel-cbc-aes",
776 .cra_alignmask = 0x0,
779 .cra_init = atmel_aes_cra_init,
780 .cra_exit = atmel_aes_cra_exit,
781 .cra_u.ablkcipher = {
785 .setkey = atmel_aes_setkey,
786 .encrypt = atmel_aes_cbc_encrypt,
787 .decrypt = atmel_aes_cbc_decrypt,
791 .cra_name =
"ofb(aes)",
792 .cra_driver_name =
"atmel-ofb-aes",
797 .cra_alignmask = 0x0,
800 .cra_init = atmel_aes_cra_init,
801 .cra_exit = atmel_aes_cra_exit,
802 .cra_u.ablkcipher = {
806 .setkey = atmel_aes_setkey,
807 .encrypt = atmel_aes_ofb_encrypt,
808 .decrypt = atmel_aes_ofb_decrypt,
812 .cra_name =
"cfb(aes)",
813 .cra_driver_name =
"atmel-cfb-aes",
818 .cra_alignmask = 0x0,
821 .cra_init = atmel_aes_cra_init,
822 .cra_exit = atmel_aes_cra_exit,
823 .cra_u.ablkcipher = {
827 .setkey = atmel_aes_setkey,
828 .encrypt = atmel_aes_cfb_encrypt,
829 .decrypt = atmel_aes_cfb_decrypt,
833 .cra_name =
"cfb32(aes)",
834 .cra_driver_name =
"atmel-cfb32-aes",
839 .cra_alignmask = 0x0,
842 .cra_init = atmel_aes_cra_init,
843 .cra_exit = atmel_aes_cra_exit,
844 .cra_u.ablkcipher = {
848 .setkey = atmel_aes_setkey,
849 .encrypt = atmel_aes_cfb32_encrypt,
850 .decrypt = atmel_aes_cfb32_decrypt,
854 .cra_name =
"cfb16(aes)",
855 .cra_driver_name =
"atmel-cfb16-aes",
860 .cra_alignmask = 0x0,
863 .cra_init = atmel_aes_cra_init,
864 .cra_exit = atmel_aes_cra_exit,
865 .cra_u.ablkcipher = {
869 .setkey = atmel_aes_setkey,
870 .encrypt = atmel_aes_cfb16_encrypt,
871 .decrypt = atmel_aes_cfb16_decrypt,
875 .cra_name =
"cfb8(aes)",
876 .cra_driver_name =
"atmel-cfb8-aes",
881 .cra_alignmask = 0x0,
884 .cra_init = atmel_aes_cra_init,
885 .cra_exit = atmel_aes_cra_exit,
886 .cra_u.ablkcipher = {
890 .setkey = atmel_aes_setkey,
891 .encrypt = atmel_aes_cfb8_encrypt,
892 .decrypt = atmel_aes_cfb8_decrypt,
896 .cra_name =
"ctr(aes)",
897 .cra_driver_name =
"atmel-ctr-aes",
902 .cra_alignmask = 0x0,
905 .cra_init = atmel_aes_cra_init,
906 .cra_exit = atmel_aes_cra_exit,
907 .cra_u.ablkcipher = {
911 .setkey = atmel_aes_setkey,
912 .encrypt = atmel_aes_ctr_encrypt,
913 .decrypt = atmel_aes_ctr_decrypt,
920 .cra_name =
"cfb64(aes)",
921 .cra_driver_name =
"atmel-cfb64-aes",
926 .cra_alignmask = 0x0,
929 .cra_init = atmel_aes_cra_init,
930 .cra_exit = atmel_aes_cra_exit,
931 .cra_u.ablkcipher = {
935 .setkey = atmel_aes_setkey,
936 .encrypt = atmel_aes_cfb64_encrypt,
937 .decrypt = atmel_aes_cfb64_decrypt,
942 static void atmel_aes_queue_task(
unsigned long data)
946 atmel_aes_handle_queue(dd,
NULL);
949 static void atmel_aes_done_task(
unsigned long data)
967 err = atmel_aes_crypt_dma_stop(dd);
971 if (dd->
total && !err) {
972 err = atmel_aes_crypt_dma_start(dd);
978 atmel_aes_finish_req(dd, err);
979 atmel_aes_handle_queue(dd,
NULL);
987 reg = atmel_aes_read(aes_dd,
AES_ISR);
988 if (reg & atmel_aes_read(aes_dd,
AES_IMR)) {
989 atmel_aes_write(aes_dd,
AES_IDR, reg);
993 dev_warn(aes_dd->
dev,
"AES interrupt when no active requests.\n");
1000 static void atmel_aes_unregister_algs(
struct atmel_aes_dev *dd)
1010 static int atmel_aes_register_algs(
struct atmel_aes_dev *dd)
1020 atmel_aes_hw_version_init(dd);
1025 goto err_aes_cfb64_alg;
1033 for (j = 0; j <
i; j++)
1045 unsigned long aes_phys_size;
1048 pdata = pdev->
dev.platform_data;
1055 if (aes_dd ==
NULL) {
1056 dev_err(dev,
"unable to alloc data struct.\n");
1063 platform_set_drvdata(pdev, aes_dd);
1065 INIT_LIST_HEAD(&aes_dd->
list);
1068 (
unsigned long)aes_dd);
1070 (
unsigned long)aes_dd);
1079 dev_err(dev,
"no MEM resource info\n");
1084 aes_phys_size = resource_size(aes_res);
1088 if (aes_dd->
irq < 0) {
1089 dev_err(dev,
"no IRQ resource info\n");
1097 dev_err(dev,
"unable to request aes irq.\n");
1103 if (IS_ERR(aes_dd->
iclk)) {
1104 dev_err(dev,
"clock intialization failed.\n");
1105 err = PTR_ERR(aes_dd->
iclk);
1111 dev_err(dev,
"can't ioremap\n");
1116 err = atmel_aes_dma_init(aes_dd);
1120 spin_lock(&atmel_aes.
lock);
1122 spin_unlock(&atmel_aes.
lock);
1124 err = atmel_aes_register_algs(aes_dd);
1133 spin_lock(&atmel_aes.
lock);
1135 spin_unlock(&atmel_aes.
lock);
1136 atmel_aes_dma_cleanup(aes_dd);
1150 dev_err(dev,
"initialization failed.\n");
1159 aes_dd = platform_get_drvdata(pdev);
1162 spin_lock(&atmel_aes.
lock);
1164 spin_unlock(&atmel_aes.
lock);
1166 atmel_aes_unregister_algs(aes_dd);
1171 atmel_aes_dma_cleanup(aes_dd);
1177 if (aes_dd->
irq > 0)
1187 .probe = atmel_aes_probe,
1190 .name =
"atmel_aes",