xref: /openbmc/linux/drivers/crypto/omap-aes.c (revision 5be4d4c9)
1537559a5SDmitry Kasatkin /*
2537559a5SDmitry Kasatkin  * Cryptographic API.
3537559a5SDmitry Kasatkin  *
4537559a5SDmitry Kasatkin  * Support for OMAP AES HW acceleration.
5537559a5SDmitry Kasatkin  *
6537559a5SDmitry Kasatkin  * Copyright (c) 2010 Nokia Corporation
7537559a5SDmitry Kasatkin  * Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
80d35583aSMark A. Greer  * Copyright (c) 2011 Texas Instruments Incorporated
9537559a5SDmitry Kasatkin  *
10537559a5SDmitry Kasatkin  * This program is free software; you can redistribute it and/or modify
11537559a5SDmitry Kasatkin  * it under the terms of the GNU General Public License version 2 as published
12537559a5SDmitry Kasatkin  * by the Free Software Foundation.
13537559a5SDmitry Kasatkin  *
14537559a5SDmitry Kasatkin  */
15537559a5SDmitry Kasatkin 
16016af9b5SJoel Fernandes #define pr_fmt(fmt) "%20s: " fmt, __func__
17016af9b5SJoel Fernandes #define prn(num) pr_debug(#num "=%d\n", num)
18016af9b5SJoel Fernandes #define prx(num) pr_debug(#num "=%x\n", num)
19537559a5SDmitry Kasatkin 
20537559a5SDmitry Kasatkin #include <linux/err.h>
21537559a5SDmitry Kasatkin #include <linux/module.h>
22537559a5SDmitry Kasatkin #include <linux/init.h>
23537559a5SDmitry Kasatkin #include <linux/errno.h>
24537559a5SDmitry Kasatkin #include <linux/kernel.h>
25537559a5SDmitry Kasatkin #include <linux/platform_device.h>
26537559a5SDmitry Kasatkin #include <linux/scatterlist.h>
27537559a5SDmitry Kasatkin #include <linux/dma-mapping.h>
28ebedbf79SMark A. Greer #include <linux/dmaengine.h>
29ebedbf79SMark A. Greer #include <linux/omap-dma.h>
305946c4a5SMark A. Greer #include <linux/pm_runtime.h>
31bc69d124SMark A. Greer #include <linux/of.h>
32bc69d124SMark A. Greer #include <linux/of_device.h>
33bc69d124SMark A. Greer #include <linux/of_address.h>
34537559a5SDmitry Kasatkin #include <linux/io.h>
35537559a5SDmitry Kasatkin #include <linux/crypto.h>
36537559a5SDmitry Kasatkin #include <linux/interrupt.h>
37537559a5SDmitry Kasatkin #include <crypto/scatterwalk.h>
38537559a5SDmitry Kasatkin #include <crypto/aes.h>
39537559a5SDmitry Kasatkin 
40ebedbf79SMark A. Greer #define DST_MAXBURST			4
41ebedbf79SMark A. Greer #define DMA_MIN				(DST_MAXBURST * sizeof(u32))
42537559a5SDmitry Kasatkin 
431bf95ccaSJoel Fernandes #define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset)
441bf95ccaSJoel Fernandes 
45537559a5SDmitry Kasatkin /* OMAP TRM gives bitfields as start:end, where start is the higher bit
46537559a5SDmitry Kasatkin    number. For example 7:0 */
47537559a5SDmitry Kasatkin #define FLD_MASK(start, end)	(((1 << ((start) - (end) + 1)) - 1) << (end))
48537559a5SDmitry Kasatkin #define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
49537559a5SDmitry Kasatkin 
500d35583aSMark A. Greer #define AES_REG_KEY(dd, x)		((dd)->pdata->key_ofs - \
510d35583aSMark A. Greer 						((x ^ 0x01) * 0x04))
520d35583aSMark A. Greer #define AES_REG_IV(dd, x)		((dd)->pdata->iv_ofs + ((x) * 0x04))
53537559a5SDmitry Kasatkin 
540d35583aSMark A. Greer #define AES_REG_CTRL(dd)		((dd)->pdata->ctrl_ofs)
55f9fb69e7SMark A. Greer #define AES_REG_CTRL_CTR_WIDTH_MASK	(3 << 7)
56f9fb69e7SMark A. Greer #define AES_REG_CTRL_CTR_WIDTH_32		(0 << 7)
57f9fb69e7SMark A. Greer #define AES_REG_CTRL_CTR_WIDTH_64		(1 << 7)
58f9fb69e7SMark A. Greer #define AES_REG_CTRL_CTR_WIDTH_96		(2 << 7)
59f9fb69e7SMark A. Greer #define AES_REG_CTRL_CTR_WIDTH_128		(3 << 7)
60537559a5SDmitry Kasatkin #define AES_REG_CTRL_CTR		(1 << 6)
61537559a5SDmitry Kasatkin #define AES_REG_CTRL_CBC		(1 << 5)
62537559a5SDmitry Kasatkin #define AES_REG_CTRL_KEY_SIZE		(3 << 3)
63537559a5SDmitry Kasatkin #define AES_REG_CTRL_DIRECTION		(1 << 2)
64537559a5SDmitry Kasatkin #define AES_REG_CTRL_INPUT_READY	(1 << 1)
65537559a5SDmitry Kasatkin #define AES_REG_CTRL_OUTPUT_READY	(1 << 0)
66537559a5SDmitry Kasatkin 
670d35583aSMark A. Greer #define AES_REG_DATA_N(dd, x)		((dd)->pdata->data_ofs + ((x) * 0x04))
68537559a5SDmitry Kasatkin 
690d35583aSMark A. Greer #define AES_REG_REV(dd)			((dd)->pdata->rev_ofs)
70537559a5SDmitry Kasatkin 
710d35583aSMark A. Greer #define AES_REG_MASK(dd)		((dd)->pdata->mask_ofs)
72537559a5SDmitry Kasatkin #define AES_REG_MASK_SIDLE		(1 << 6)
73537559a5SDmitry Kasatkin #define AES_REG_MASK_START		(1 << 5)
74537559a5SDmitry Kasatkin #define AES_REG_MASK_DMA_OUT_EN		(1 << 3)
75537559a5SDmitry Kasatkin #define AES_REG_MASK_DMA_IN_EN		(1 << 2)
76537559a5SDmitry Kasatkin #define AES_REG_MASK_SOFTRESET		(1 << 1)
77537559a5SDmitry Kasatkin #define AES_REG_AUTOIDLE		(1 << 0)
78537559a5SDmitry Kasatkin 
790d35583aSMark A. Greer #define AES_REG_LENGTH_N(x)		(0x54 + ((x) * 0x04))
80537559a5SDmitry Kasatkin 
8167216756SJoel Fernandes #define AES_REG_IRQ_STATUS(dd)         ((dd)->pdata->irq_status_ofs)
8267216756SJoel Fernandes #define AES_REG_IRQ_ENABLE(dd)         ((dd)->pdata->irq_enable_ofs)
8367216756SJoel Fernandes #define AES_REG_IRQ_DATA_IN            BIT(1)
8467216756SJoel Fernandes #define AES_REG_IRQ_DATA_OUT           BIT(2)
85537559a5SDmitry Kasatkin #define DEFAULT_TIMEOUT		(5*HZ)
86537559a5SDmitry Kasatkin 
87537559a5SDmitry Kasatkin #define FLAGS_MODE_MASK		0x000f
88537559a5SDmitry Kasatkin #define FLAGS_ENCRYPT		BIT(0)
89537559a5SDmitry Kasatkin #define FLAGS_CBC		BIT(1)
90537559a5SDmitry Kasatkin #define FLAGS_GIV		BIT(2)
91f9fb69e7SMark A. Greer #define FLAGS_CTR		BIT(3)
92537559a5SDmitry Kasatkin 
9367a730ceSDmitry Kasatkin #define FLAGS_INIT		BIT(4)
9467a730ceSDmitry Kasatkin #define FLAGS_FAST		BIT(5)
9567a730ceSDmitry Kasatkin #define FLAGS_BUSY		BIT(6)
96537559a5SDmitry Kasatkin 
971bf95ccaSJoel Fernandes #define AES_BLOCK_WORDS		(AES_BLOCK_SIZE >> 2)
981bf95ccaSJoel Fernandes 
99537559a5SDmitry Kasatkin struct omap_aes_ctx {
100537559a5SDmitry Kasatkin 	struct omap_aes_dev *dd;
101537559a5SDmitry Kasatkin 
102537559a5SDmitry Kasatkin 	int		keylen;
103537559a5SDmitry Kasatkin 	u32		key[AES_KEYSIZE_256 / sizeof(u32)];
104537559a5SDmitry Kasatkin 	unsigned long	flags;
105537559a5SDmitry Kasatkin };
106537559a5SDmitry Kasatkin 
107537559a5SDmitry Kasatkin struct omap_aes_reqctx {
108537559a5SDmitry Kasatkin 	unsigned long mode;
109537559a5SDmitry Kasatkin };
110537559a5SDmitry Kasatkin 
111537559a5SDmitry Kasatkin #define OMAP_AES_QUEUE_LENGTH	1
112537559a5SDmitry Kasatkin #define OMAP_AES_CACHE_SIZE	0
113537559a5SDmitry Kasatkin 
114f9fb69e7SMark A. Greer struct omap_aes_algs_info {
115f9fb69e7SMark A. Greer 	struct crypto_alg	*algs_list;
116f9fb69e7SMark A. Greer 	unsigned int		size;
117f9fb69e7SMark A. Greer 	unsigned int		registered;
118f9fb69e7SMark A. Greer };
119f9fb69e7SMark A. Greer 
1200d35583aSMark A. Greer struct omap_aes_pdata {
121f9fb69e7SMark A. Greer 	struct omap_aes_algs_info	*algs_info;
122f9fb69e7SMark A. Greer 	unsigned int	algs_info_size;
123f9fb69e7SMark A. Greer 
1240d35583aSMark A. Greer 	void		(*trigger)(struct omap_aes_dev *dd, int length);
1250d35583aSMark A. Greer 
1260d35583aSMark A. Greer 	u32		key_ofs;
1270d35583aSMark A. Greer 	u32		iv_ofs;
1280d35583aSMark A. Greer 	u32		ctrl_ofs;
1290d35583aSMark A. Greer 	u32		data_ofs;
1300d35583aSMark A. Greer 	u32		rev_ofs;
1310d35583aSMark A. Greer 	u32		mask_ofs;
13267216756SJoel Fernandes 	u32             irq_enable_ofs;
13367216756SJoel Fernandes 	u32             irq_status_ofs;
1340d35583aSMark A. Greer 
1350d35583aSMark A. Greer 	u32		dma_enable_in;
1360d35583aSMark A. Greer 	u32		dma_enable_out;
1370d35583aSMark A. Greer 	u32		dma_start;
1380d35583aSMark A. Greer 
1390d35583aSMark A. Greer 	u32		major_mask;
1400d35583aSMark A. Greer 	u32		major_shift;
1410d35583aSMark A. Greer 	u32		minor_mask;
1420d35583aSMark A. Greer 	u32		minor_shift;
1430d35583aSMark A. Greer };
1440d35583aSMark A. Greer 
145537559a5SDmitry Kasatkin struct omap_aes_dev {
146537559a5SDmitry Kasatkin 	struct list_head	list;
147537559a5SDmitry Kasatkin 	unsigned long		phys_base;
148537559a5SDmitry Kasatkin 	void __iomem		*io_base;
149537559a5SDmitry Kasatkin 	struct omap_aes_ctx	*ctx;
150537559a5SDmitry Kasatkin 	struct device		*dev;
151537559a5SDmitry Kasatkin 	unsigned long		flags;
15221fe9767SDmitry Kasatkin 	int			err;
153537559a5SDmitry Kasatkin 
154537559a5SDmitry Kasatkin 	spinlock_t		lock;
155537559a5SDmitry Kasatkin 	struct crypto_queue	queue;
156537559a5SDmitry Kasatkin 
15721fe9767SDmitry Kasatkin 	struct tasklet_struct	done_task;
15821fe9767SDmitry Kasatkin 	struct tasklet_struct	queue_task;
159537559a5SDmitry Kasatkin 
160537559a5SDmitry Kasatkin 	struct ablkcipher_request	*req;
1616242332fSJoel Fernandes 
1626242332fSJoel Fernandes 	/*
1636242332fSJoel Fernandes 	 * total is used by PIO mode for book keeping so introduce
1646242332fSJoel Fernandes 	 * variable total_save as need it to calc page_order
1656242332fSJoel Fernandes 	 */
166537559a5SDmitry Kasatkin 	size_t				total;
1676242332fSJoel Fernandes 	size_t				total_save;
1686242332fSJoel Fernandes 
169537559a5SDmitry Kasatkin 	struct scatterlist		*in_sg;
170537559a5SDmitry Kasatkin 	struct scatterlist		*out_sg;
1716242332fSJoel Fernandes 
1726242332fSJoel Fernandes 	/* Buffers for copying for unaligned cases */
1736242332fSJoel Fernandes 	struct scatterlist		in_sgl;
1746242332fSJoel Fernandes 	struct scatterlist		out_sgl;
1756242332fSJoel Fernandes 	struct scatterlist		*orig_out;
1766242332fSJoel Fernandes 	int				sgs_copied;
1776242332fSJoel Fernandes 
1781bf95ccaSJoel Fernandes 	struct scatter_walk		in_walk;
1791bf95ccaSJoel Fernandes 	struct scatter_walk		out_walk;
180537559a5SDmitry Kasatkin 	int			dma_in;
181ebedbf79SMark A. Greer 	struct dma_chan		*dma_lch_in;
182537559a5SDmitry Kasatkin 	int			dma_out;
183ebedbf79SMark A. Greer 	struct dma_chan		*dma_lch_out;
184e77c756eSJoel Fernandes 	int			in_sg_len;
185e77c756eSJoel Fernandes 	int			out_sg_len;
18698837abcSJoel Fernandes 	int			pio_only;
1870d35583aSMark A. Greer 	const struct omap_aes_pdata	*pdata;
188537559a5SDmitry Kasatkin };
189537559a5SDmitry Kasatkin 
190537559a5SDmitry Kasatkin /* keep registered devices data here */
191537559a5SDmitry Kasatkin static LIST_HEAD(dev_list);
192537559a5SDmitry Kasatkin static DEFINE_SPINLOCK(list_lock);
193537559a5SDmitry Kasatkin 
194016af9b5SJoel Fernandes #ifdef DEBUG
195016af9b5SJoel Fernandes #define omap_aes_read(dd, offset)				\
196016af9b5SJoel Fernandes ({								\
197016af9b5SJoel Fernandes 	int _read_ret;						\
198016af9b5SJoel Fernandes 	_read_ret = __raw_readl(dd->io_base + offset);		\
199016af9b5SJoel Fernandes 	pr_debug("omap_aes_read(" #offset "=%#x)= %#x\n",	\
200016af9b5SJoel Fernandes 		 offset, _read_ret);				\
201016af9b5SJoel Fernandes 	_read_ret;						\
202016af9b5SJoel Fernandes })
203016af9b5SJoel Fernandes #else
204537559a5SDmitry Kasatkin static inline u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset)
205537559a5SDmitry Kasatkin {
206537559a5SDmitry Kasatkin 	return __raw_readl(dd->io_base + offset);
207537559a5SDmitry Kasatkin }
208016af9b5SJoel Fernandes #endif
209537559a5SDmitry Kasatkin 
210016af9b5SJoel Fernandes #ifdef DEBUG
211016af9b5SJoel Fernandes #define omap_aes_write(dd, offset, value)				\
212016af9b5SJoel Fernandes 	do {								\
213016af9b5SJoel Fernandes 		pr_debug("omap_aes_write(" #offset "=%#x) value=%#x\n",	\
214016af9b5SJoel Fernandes 			 offset, value);				\
215016af9b5SJoel Fernandes 		__raw_writel(value, dd->io_base + offset);		\
216016af9b5SJoel Fernandes 	} while (0)
217016af9b5SJoel Fernandes #else
218537559a5SDmitry Kasatkin static inline void omap_aes_write(struct omap_aes_dev *dd, u32 offset,
219537559a5SDmitry Kasatkin 				  u32 value)
220537559a5SDmitry Kasatkin {
221537559a5SDmitry Kasatkin 	__raw_writel(value, dd->io_base + offset);
222537559a5SDmitry Kasatkin }
223016af9b5SJoel Fernandes #endif
224537559a5SDmitry Kasatkin 
225537559a5SDmitry Kasatkin static inline void omap_aes_write_mask(struct omap_aes_dev *dd, u32 offset,
226537559a5SDmitry Kasatkin 					u32 value, u32 mask)
227537559a5SDmitry Kasatkin {
228537559a5SDmitry Kasatkin 	u32 val;
229537559a5SDmitry Kasatkin 
230537559a5SDmitry Kasatkin 	val = omap_aes_read(dd, offset);
231537559a5SDmitry Kasatkin 	val &= ~mask;
232537559a5SDmitry Kasatkin 	val |= value;
233537559a5SDmitry Kasatkin 	omap_aes_write(dd, offset, val);
234537559a5SDmitry Kasatkin }
235537559a5SDmitry Kasatkin 
236537559a5SDmitry Kasatkin static void omap_aes_write_n(struct omap_aes_dev *dd, u32 offset,
237537559a5SDmitry Kasatkin 					u32 *value, int count)
238537559a5SDmitry Kasatkin {
239537559a5SDmitry Kasatkin 	for (; count--; value++, offset += 4)
240537559a5SDmitry Kasatkin 		omap_aes_write(dd, offset, *value);
241537559a5SDmitry Kasatkin }
242537559a5SDmitry Kasatkin 
243537559a5SDmitry Kasatkin static int omap_aes_hw_init(struct omap_aes_dev *dd)
244537559a5SDmitry Kasatkin {
245537559a5SDmitry Kasatkin 	if (!(dd->flags & FLAGS_INIT)) {
246537559a5SDmitry Kasatkin 		dd->flags |= FLAGS_INIT;
24721fe9767SDmitry Kasatkin 		dd->err = 0;
248537559a5SDmitry Kasatkin 	}
249537559a5SDmitry Kasatkin 
250eeb2b202SDmitry Kasatkin 	return 0;
251537559a5SDmitry Kasatkin }
252537559a5SDmitry Kasatkin 
25321fe9767SDmitry Kasatkin static int omap_aes_write_ctrl(struct omap_aes_dev *dd)
254537559a5SDmitry Kasatkin {
255537559a5SDmitry Kasatkin 	unsigned int key32;
25667a730ceSDmitry Kasatkin 	int i, err;
257f9fb69e7SMark A. Greer 	u32 val, mask = 0;
258537559a5SDmitry Kasatkin 
25921fe9767SDmitry Kasatkin 	err = omap_aes_hw_init(dd);
26021fe9767SDmitry Kasatkin 	if (err)
26121fe9767SDmitry Kasatkin 		return err;
26221fe9767SDmitry Kasatkin 
263537559a5SDmitry Kasatkin 	key32 = dd->ctx->keylen / sizeof(u32);
26467a730ceSDmitry Kasatkin 
26567a730ceSDmitry Kasatkin 	/* it seems a key should always be set even if it has not changed */
266537559a5SDmitry Kasatkin 	for (i = 0; i < key32; i++) {
2670d35583aSMark A. Greer 		omap_aes_write(dd, AES_REG_KEY(dd, i),
268537559a5SDmitry Kasatkin 			__le32_to_cpu(dd->ctx->key[i]));
269537559a5SDmitry Kasatkin 	}
270537559a5SDmitry Kasatkin 
271f9fb69e7SMark A. Greer 	if ((dd->flags & (FLAGS_CBC | FLAGS_CTR)) && dd->req->info)
2720d35583aSMark A. Greer 		omap_aes_write_n(dd, AES_REG_IV(dd, 0), dd->req->info, 4);
27367a730ceSDmitry Kasatkin 
27467a730ceSDmitry Kasatkin 	val = FLD_VAL(((dd->ctx->keylen >> 3) - 1), 4, 3);
27567a730ceSDmitry Kasatkin 	if (dd->flags & FLAGS_CBC)
27667a730ceSDmitry Kasatkin 		val |= AES_REG_CTRL_CBC;
277f9fb69e7SMark A. Greer 	if (dd->flags & FLAGS_CTR) {
2788ed49c76SJoel Fernandes 		val |= AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_128;
279f9fb69e7SMark A. Greer 		mask = AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_MASK;
280f9fb69e7SMark A. Greer 	}
28167a730ceSDmitry Kasatkin 	if (dd->flags & FLAGS_ENCRYPT)
28267a730ceSDmitry Kasatkin 		val |= AES_REG_CTRL_DIRECTION;
283537559a5SDmitry Kasatkin 
284f9fb69e7SMark A. Greer 	mask |= AES_REG_CTRL_CBC | AES_REG_CTRL_DIRECTION |
285537559a5SDmitry Kasatkin 			AES_REG_CTRL_KEY_SIZE;
286537559a5SDmitry Kasatkin 
2870d35583aSMark A. Greer 	omap_aes_write_mask(dd, AES_REG_CTRL(dd), val, mask);
288537559a5SDmitry Kasatkin 
28921fe9767SDmitry Kasatkin 	return 0;
290537559a5SDmitry Kasatkin }
291537559a5SDmitry Kasatkin 
2920d35583aSMark A. Greer static void omap_aes_dma_trigger_omap2(struct omap_aes_dev *dd, int length)
2930d35583aSMark A. Greer {
2940d35583aSMark A. Greer 	u32 mask, val;
2950d35583aSMark A. Greer 
2960d35583aSMark A. Greer 	val = dd->pdata->dma_start;
2970d35583aSMark A. Greer 
2980d35583aSMark A. Greer 	if (dd->dma_lch_out != NULL)
2990d35583aSMark A. Greer 		val |= dd->pdata->dma_enable_out;
3000d35583aSMark A. Greer 	if (dd->dma_lch_in != NULL)
3010d35583aSMark A. Greer 		val |= dd->pdata->dma_enable_in;
3020d35583aSMark A. Greer 
3030d35583aSMark A. Greer 	mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
3040d35583aSMark A. Greer 	       dd->pdata->dma_start;
3050d35583aSMark A. Greer 
3060d35583aSMark A. Greer 	omap_aes_write_mask(dd, AES_REG_MASK(dd), val, mask);
3070d35583aSMark A. Greer 
3080d35583aSMark A. Greer }
3090d35583aSMark A. Greer 
3100d35583aSMark A. Greer static void omap_aes_dma_trigger_omap4(struct omap_aes_dev *dd, int length)
3110d35583aSMark A. Greer {
3120d35583aSMark A. Greer 	omap_aes_write(dd, AES_REG_LENGTH_N(0), length);
3130d35583aSMark A. Greer 	omap_aes_write(dd, AES_REG_LENGTH_N(1), 0);
3140d35583aSMark A. Greer 
3150d35583aSMark A. Greer 	omap_aes_dma_trigger_omap2(dd, length);
3160d35583aSMark A. Greer }
3170d35583aSMark A. Greer 
3180d35583aSMark A. Greer static void omap_aes_dma_stop(struct omap_aes_dev *dd)
3190d35583aSMark A. Greer {
3200d35583aSMark A. Greer 	u32 mask;
3210d35583aSMark A. Greer 
3220d35583aSMark A. Greer 	mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
3230d35583aSMark A. Greer 	       dd->pdata->dma_start;
3240d35583aSMark A. Greer 
3250d35583aSMark A. Greer 	omap_aes_write_mask(dd, AES_REG_MASK(dd), 0, mask);
3260d35583aSMark A. Greer }
3270d35583aSMark A. Greer 
328537559a5SDmitry Kasatkin static struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_ctx *ctx)
329537559a5SDmitry Kasatkin {
330537559a5SDmitry Kasatkin 	struct omap_aes_dev *dd = NULL, *tmp;
331537559a5SDmitry Kasatkin 
332537559a5SDmitry Kasatkin 	spin_lock_bh(&list_lock);
333537559a5SDmitry Kasatkin 	if (!ctx->dd) {
334537559a5SDmitry Kasatkin 		list_for_each_entry(tmp, &dev_list, list) {
335537559a5SDmitry Kasatkin 			/* FIXME: take fist available aes core */
336537559a5SDmitry Kasatkin 			dd = tmp;
337537559a5SDmitry Kasatkin 			break;
338537559a5SDmitry Kasatkin 		}
339537559a5SDmitry Kasatkin 		ctx->dd = dd;
340537559a5SDmitry Kasatkin 	} else {
341537559a5SDmitry Kasatkin 		/* already found before */
342537559a5SDmitry Kasatkin 		dd = ctx->dd;
343537559a5SDmitry Kasatkin 	}
344537559a5SDmitry Kasatkin 	spin_unlock_bh(&list_lock);
345537559a5SDmitry Kasatkin 
346537559a5SDmitry Kasatkin 	return dd;
347537559a5SDmitry Kasatkin }
348537559a5SDmitry Kasatkin 
349ebedbf79SMark A. Greer static void omap_aes_dma_out_callback(void *data)
350ebedbf79SMark A. Greer {
351ebedbf79SMark A. Greer 	struct omap_aes_dev *dd = data;
352ebedbf79SMark A. Greer 
353ebedbf79SMark A. Greer 	/* dma_lch_out - completed */
354ebedbf79SMark A. Greer 	tasklet_schedule(&dd->done_task);
355ebedbf79SMark A. Greer }
356537559a5SDmitry Kasatkin 
357537559a5SDmitry Kasatkin static int omap_aes_dma_init(struct omap_aes_dev *dd)
358537559a5SDmitry Kasatkin {
359537559a5SDmitry Kasatkin 	int err = -ENOMEM;
360ebedbf79SMark A. Greer 	dma_cap_mask_t mask;
361537559a5SDmitry Kasatkin 
362ebedbf79SMark A. Greer 	dd->dma_lch_out = NULL;
363ebedbf79SMark A. Greer 	dd->dma_lch_in = NULL;
364537559a5SDmitry Kasatkin 
365ebedbf79SMark A. Greer 	dma_cap_zero(mask);
366ebedbf79SMark A. Greer 	dma_cap_set(DMA_SLAVE, mask);
367ebedbf79SMark A. Greer 
368b4b87a93SMark A. Greer 	dd->dma_lch_in = dma_request_slave_channel_compat(mask,
369b4b87a93SMark A. Greer 							  omap_dma_filter_fn,
370b4b87a93SMark A. Greer 							  &dd->dma_in,
371b4b87a93SMark A. Greer 							  dd->dev, "rx");
372ebedbf79SMark A. Greer 	if (!dd->dma_lch_in) {
373ebedbf79SMark A. Greer 		dev_err(dd->dev, "Unable to request in DMA channel\n");
374ebedbf79SMark A. Greer 		goto err_dma_in;
375ebedbf79SMark A. Greer 	}
376ebedbf79SMark A. Greer 
377b4b87a93SMark A. Greer 	dd->dma_lch_out = dma_request_slave_channel_compat(mask,
378b4b87a93SMark A. Greer 							   omap_dma_filter_fn,
379b4b87a93SMark A. Greer 							   &dd->dma_out,
380b4b87a93SMark A. Greer 							   dd->dev, "tx");
381ebedbf79SMark A. Greer 	if (!dd->dma_lch_out) {
382ebedbf79SMark A. Greer 		dev_err(dd->dev, "Unable to request out DMA channel\n");
383ebedbf79SMark A. Greer 		goto err_dma_out;
384ebedbf79SMark A. Greer 	}
385537559a5SDmitry Kasatkin 
386537559a5SDmitry Kasatkin 	return 0;
387537559a5SDmitry Kasatkin 
388537559a5SDmitry Kasatkin err_dma_out:
389ebedbf79SMark A. Greer 	dma_release_channel(dd->dma_lch_in);
390537559a5SDmitry Kasatkin err_dma_in:
391537559a5SDmitry Kasatkin 	if (err)
392537559a5SDmitry Kasatkin 		pr_err("error: %d\n", err);
393537559a5SDmitry Kasatkin 	return err;
394537559a5SDmitry Kasatkin }
395537559a5SDmitry Kasatkin 
396537559a5SDmitry Kasatkin static void omap_aes_dma_cleanup(struct omap_aes_dev *dd)
397537559a5SDmitry Kasatkin {
398ebedbf79SMark A. Greer 	dma_release_channel(dd->dma_lch_out);
399ebedbf79SMark A. Greer 	dma_release_channel(dd->dma_lch_in);
400537559a5SDmitry Kasatkin }
401537559a5SDmitry Kasatkin 
402537559a5SDmitry Kasatkin static void sg_copy_buf(void *buf, struct scatterlist *sg,
403537559a5SDmitry Kasatkin 			      unsigned int start, unsigned int nbytes, int out)
404537559a5SDmitry Kasatkin {
405537559a5SDmitry Kasatkin 	struct scatter_walk walk;
406537559a5SDmitry Kasatkin 
407537559a5SDmitry Kasatkin 	if (!nbytes)
408537559a5SDmitry Kasatkin 		return;
409537559a5SDmitry Kasatkin 
410537559a5SDmitry Kasatkin 	scatterwalk_start(&walk, sg);
411537559a5SDmitry Kasatkin 	scatterwalk_advance(&walk, start);
412537559a5SDmitry Kasatkin 	scatterwalk_copychunks(buf, &walk, nbytes, out);
413537559a5SDmitry Kasatkin 	scatterwalk_done(&walk, out, 0);
414537559a5SDmitry Kasatkin }
415537559a5SDmitry Kasatkin 
416ebedbf79SMark A. Greer static int omap_aes_crypt_dma(struct crypto_tfm *tfm,
4174b645c94SJoel Fernandes 		struct scatterlist *in_sg, struct scatterlist *out_sg,
4184b645c94SJoel Fernandes 		int in_sg_len, int out_sg_len)
419537559a5SDmitry Kasatkin {
420537559a5SDmitry Kasatkin 	struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
421537559a5SDmitry Kasatkin 	struct omap_aes_dev *dd = ctx->dd;
422ebedbf79SMark A. Greer 	struct dma_async_tx_descriptor *tx_in, *tx_out;
423ebedbf79SMark A. Greer 	struct dma_slave_config cfg;
4244b645c94SJoel Fernandes 	int ret;
425537559a5SDmitry Kasatkin 
42698837abcSJoel Fernandes 	if (dd->pio_only) {
42798837abcSJoel Fernandes 		scatterwalk_start(&dd->in_walk, dd->in_sg);
42898837abcSJoel Fernandes 		scatterwalk_start(&dd->out_walk, dd->out_sg);
42998837abcSJoel Fernandes 
43098837abcSJoel Fernandes 		/* Enable DATAIN interrupt and let it take
43198837abcSJoel Fernandes 		   care of the rest */
43298837abcSJoel Fernandes 		omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
43398837abcSJoel Fernandes 		return 0;
43498837abcSJoel Fernandes 	}
43598837abcSJoel Fernandes 
4360a641712SJoel Fernandes 	dma_sync_sg_for_device(dd->dev, dd->in_sg, in_sg_len, DMA_TO_DEVICE);
4370a641712SJoel Fernandes 
438ebedbf79SMark A. Greer 	memset(&cfg, 0, sizeof(cfg));
439ebedbf79SMark A. Greer 
4400d35583aSMark A. Greer 	cfg.src_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
4410d35583aSMark A. Greer 	cfg.dst_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
442ebedbf79SMark A. Greer 	cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
443ebedbf79SMark A. Greer 	cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
444ebedbf79SMark A. Greer 	cfg.src_maxburst = DST_MAXBURST;
445ebedbf79SMark A. Greer 	cfg.dst_maxburst = DST_MAXBURST;
446ebedbf79SMark A. Greer 
447ebedbf79SMark A. Greer 	/* IN */
448ebedbf79SMark A. Greer 	ret = dmaengine_slave_config(dd->dma_lch_in, &cfg);
449ebedbf79SMark A. Greer 	if (ret) {
450ebedbf79SMark A. Greer 		dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
451ebedbf79SMark A. Greer 			ret);
452ebedbf79SMark A. Greer 		return ret;
453ebedbf79SMark A. Greer 	}
454ebedbf79SMark A. Greer 
4554b645c94SJoel Fernandes 	tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, in_sg_len,
456ebedbf79SMark A. Greer 					DMA_MEM_TO_DEV,
457ebedbf79SMark A. Greer 					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
458ebedbf79SMark A. Greer 	if (!tx_in) {
459ebedbf79SMark A. Greer 		dev_err(dd->dev, "IN prep_slave_sg() failed\n");
460ebedbf79SMark A. Greer 		return -EINVAL;
461ebedbf79SMark A. Greer 	}
462ebedbf79SMark A. Greer 
463ebedbf79SMark A. Greer 	/* No callback necessary */
464ebedbf79SMark A. Greer 	tx_in->callback_param = dd;
465ebedbf79SMark A. Greer 
466ebedbf79SMark A. Greer 	/* OUT */
467ebedbf79SMark A. Greer 	ret = dmaengine_slave_config(dd->dma_lch_out, &cfg);
468ebedbf79SMark A. Greer 	if (ret) {
469ebedbf79SMark A. Greer 		dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n",
470ebedbf79SMark A. Greer 			ret);
471ebedbf79SMark A. Greer 		return ret;
472ebedbf79SMark A. Greer 	}
473ebedbf79SMark A. Greer 
4744b645c94SJoel Fernandes 	tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, out_sg_len,
475ebedbf79SMark A. Greer 					DMA_DEV_TO_MEM,
476ebedbf79SMark A. Greer 					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
477ebedbf79SMark A. Greer 	if (!tx_out) {
478ebedbf79SMark A. Greer 		dev_err(dd->dev, "OUT prep_slave_sg() failed\n");
479ebedbf79SMark A. Greer 		return -EINVAL;
480ebedbf79SMark A. Greer 	}
481ebedbf79SMark A. Greer 
482ebedbf79SMark A. Greer 	tx_out->callback = omap_aes_dma_out_callback;
483ebedbf79SMark A. Greer 	tx_out->callback_param = dd;
484ebedbf79SMark A. Greer 
485ebedbf79SMark A. Greer 	dmaengine_submit(tx_in);
486ebedbf79SMark A. Greer 	dmaengine_submit(tx_out);
487ebedbf79SMark A. Greer 
488ebedbf79SMark A. Greer 	dma_async_issue_pending(dd->dma_lch_in);
489ebedbf79SMark A. Greer 	dma_async_issue_pending(dd->dma_lch_out);
490537559a5SDmitry Kasatkin 
4910d35583aSMark A. Greer 	/* start DMA */
4924b645c94SJoel Fernandes 	dd->pdata->trigger(dd, dd->total);
49383ea7e0fSDmitry Kasatkin 
494537559a5SDmitry Kasatkin 	return 0;
495537559a5SDmitry Kasatkin }
496537559a5SDmitry Kasatkin 
497537559a5SDmitry Kasatkin static int omap_aes_crypt_dma_start(struct omap_aes_dev *dd)
498537559a5SDmitry Kasatkin {
499537559a5SDmitry Kasatkin 	struct crypto_tfm *tfm = crypto_ablkcipher_tfm(
500537559a5SDmitry Kasatkin 					crypto_ablkcipher_reqtfm(dd->req));
5014b645c94SJoel Fernandes 	int err;
502537559a5SDmitry Kasatkin 
503537559a5SDmitry Kasatkin 	pr_debug("total: %d\n", dd->total);
504537559a5SDmitry Kasatkin 
50598837abcSJoel Fernandes 	if (!dd->pio_only) {
50698837abcSJoel Fernandes 		err = dma_map_sg(dd->dev, dd->in_sg, dd->in_sg_len,
50798837abcSJoel Fernandes 				 DMA_TO_DEVICE);
508537559a5SDmitry Kasatkin 		if (!err) {
509537559a5SDmitry Kasatkin 			dev_err(dd->dev, "dma_map_sg() error\n");
510537559a5SDmitry Kasatkin 			return -EINVAL;
511537559a5SDmitry Kasatkin 		}
512537559a5SDmitry Kasatkin 
51398837abcSJoel Fernandes 		err = dma_map_sg(dd->dev, dd->out_sg, dd->out_sg_len,
51498837abcSJoel Fernandes 				 DMA_FROM_DEVICE);
515537559a5SDmitry Kasatkin 		if (!err) {
516537559a5SDmitry Kasatkin 			dev_err(dd->dev, "dma_map_sg() error\n");
517537559a5SDmitry Kasatkin 			return -EINVAL;
518537559a5SDmitry Kasatkin 		}
51998837abcSJoel Fernandes 	}
520537559a5SDmitry Kasatkin 
5214b645c94SJoel Fernandes 	err = omap_aes_crypt_dma(tfm, dd->in_sg, dd->out_sg, dd->in_sg_len,
5224b645c94SJoel Fernandes 				 dd->out_sg_len);
52398837abcSJoel Fernandes 	if (err && !dd->pio_only) {
5244b645c94SJoel Fernandes 		dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
5254b645c94SJoel Fernandes 		dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
5264b645c94SJoel Fernandes 			     DMA_FROM_DEVICE);
52721fe9767SDmitry Kasatkin 	}
528537559a5SDmitry Kasatkin 
529537559a5SDmitry Kasatkin 	return err;
530537559a5SDmitry Kasatkin }
531537559a5SDmitry Kasatkin 
532537559a5SDmitry Kasatkin static void omap_aes_finish_req(struct omap_aes_dev *dd, int err)
533537559a5SDmitry Kasatkin {
53421fe9767SDmitry Kasatkin 	struct ablkcipher_request *req = dd->req;
535537559a5SDmitry Kasatkin 
536537559a5SDmitry Kasatkin 	pr_debug("err: %d\n", err);
537537559a5SDmitry Kasatkin 
538eeb2b202SDmitry Kasatkin 	dd->flags &= ~FLAGS_BUSY;
539eeb2b202SDmitry Kasatkin 
54021fe9767SDmitry Kasatkin 	req->base.complete(&req->base, err);
541537559a5SDmitry Kasatkin }
542537559a5SDmitry Kasatkin 
543537559a5SDmitry Kasatkin static int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd)
544537559a5SDmitry Kasatkin {
545537559a5SDmitry Kasatkin 	int err = 0;
546537559a5SDmitry Kasatkin 
547537559a5SDmitry Kasatkin 	pr_debug("total: %d\n", dd->total);
548537559a5SDmitry Kasatkin 
5490d35583aSMark A. Greer 	omap_aes_dma_stop(dd);
550537559a5SDmitry Kasatkin 
551ebedbf79SMark A. Greer 	dmaengine_terminate_all(dd->dma_lch_in);
552ebedbf79SMark A. Greer 	dmaengine_terminate_all(dd->dma_lch_out);
553537559a5SDmitry Kasatkin 
554537559a5SDmitry Kasatkin 	return err;
555537559a5SDmitry Kasatkin }
556537559a5SDmitry Kasatkin 
557034568e8SSachin Kamat static int omap_aes_check_aligned(struct scatterlist *sg)
5586242332fSJoel Fernandes {
5596242332fSJoel Fernandes 	while (sg) {
5606242332fSJoel Fernandes 		if (!IS_ALIGNED(sg->offset, 4))
5616242332fSJoel Fernandes 			return -1;
5626242332fSJoel Fernandes 		if (!IS_ALIGNED(sg->length, AES_BLOCK_SIZE))
5636242332fSJoel Fernandes 			return -1;
5646242332fSJoel Fernandes 		sg = sg_next(sg);
5656242332fSJoel Fernandes 	}
5666242332fSJoel Fernandes 	return 0;
5676242332fSJoel Fernandes }
5686242332fSJoel Fernandes 
569034568e8SSachin Kamat static int omap_aes_copy_sgs(struct omap_aes_dev *dd)
5706242332fSJoel Fernandes {
5716242332fSJoel Fernandes 	void *buf_in, *buf_out;
5726242332fSJoel Fernandes 	int pages;
5736242332fSJoel Fernandes 
5746242332fSJoel Fernandes 	pages = get_order(dd->total);
5756242332fSJoel Fernandes 
5766242332fSJoel Fernandes 	buf_in = (void *)__get_free_pages(GFP_ATOMIC, pages);
5776242332fSJoel Fernandes 	buf_out = (void *)__get_free_pages(GFP_ATOMIC, pages);
5786242332fSJoel Fernandes 
5796242332fSJoel Fernandes 	if (!buf_in || !buf_out) {
5806242332fSJoel Fernandes 		pr_err("Couldn't allocated pages for unaligned cases.\n");
5816242332fSJoel Fernandes 		return -1;
5826242332fSJoel Fernandes 	}
5836242332fSJoel Fernandes 
5846242332fSJoel Fernandes 	dd->orig_out = dd->out_sg;
5856242332fSJoel Fernandes 
5866242332fSJoel Fernandes 	sg_copy_buf(buf_in, dd->in_sg, 0, dd->total, 0);
5876242332fSJoel Fernandes 
5886242332fSJoel Fernandes 	sg_init_table(&dd->in_sgl, 1);
5896242332fSJoel Fernandes 	sg_set_buf(&dd->in_sgl, buf_in, dd->total);
5906242332fSJoel Fernandes 	dd->in_sg = &dd->in_sgl;
5916242332fSJoel Fernandes 
5926242332fSJoel Fernandes 	sg_init_table(&dd->out_sgl, 1);
5936242332fSJoel Fernandes 	sg_set_buf(&dd->out_sgl, buf_out, dd->total);
5946242332fSJoel Fernandes 	dd->out_sg = &dd->out_sgl;
5956242332fSJoel Fernandes 
5966242332fSJoel Fernandes 	return 0;
5976242332fSJoel Fernandes }
5986242332fSJoel Fernandes 
59921fe9767SDmitry Kasatkin static int omap_aes_handle_queue(struct omap_aes_dev *dd,
600eeb2b202SDmitry Kasatkin 			       struct ablkcipher_request *req)
601537559a5SDmitry Kasatkin {
602537559a5SDmitry Kasatkin 	struct crypto_async_request *async_req, *backlog;
603537559a5SDmitry Kasatkin 	struct omap_aes_ctx *ctx;
604537559a5SDmitry Kasatkin 	struct omap_aes_reqctx *rctx;
605537559a5SDmitry Kasatkin 	unsigned long flags;
60621fe9767SDmitry Kasatkin 	int err, ret = 0;
607537559a5SDmitry Kasatkin 
608537559a5SDmitry Kasatkin 	spin_lock_irqsave(&dd->lock, flags);
609eeb2b202SDmitry Kasatkin 	if (req)
61021fe9767SDmitry Kasatkin 		ret = ablkcipher_enqueue_request(&dd->queue, req);
611eeb2b202SDmitry Kasatkin 	if (dd->flags & FLAGS_BUSY) {
612eeb2b202SDmitry Kasatkin 		spin_unlock_irqrestore(&dd->lock, flags);
61321fe9767SDmitry Kasatkin 		return ret;
614eeb2b202SDmitry Kasatkin 	}
615537559a5SDmitry Kasatkin 	backlog = crypto_get_backlog(&dd->queue);
616537559a5SDmitry Kasatkin 	async_req = crypto_dequeue_request(&dd->queue);
617eeb2b202SDmitry Kasatkin 	if (async_req)
618eeb2b202SDmitry Kasatkin 		dd->flags |= FLAGS_BUSY;
619537559a5SDmitry Kasatkin 	spin_unlock_irqrestore(&dd->lock, flags);
620537559a5SDmitry Kasatkin 
621537559a5SDmitry Kasatkin 	if (!async_req)
62221fe9767SDmitry Kasatkin 		return ret;
623537559a5SDmitry Kasatkin 
624537559a5SDmitry Kasatkin 	if (backlog)
625537559a5SDmitry Kasatkin 		backlog->complete(backlog, -EINPROGRESS);
626537559a5SDmitry Kasatkin 
627537559a5SDmitry Kasatkin 	req = ablkcipher_request_cast(async_req);
628537559a5SDmitry Kasatkin 
629537559a5SDmitry Kasatkin 	/* assign new request to device */
630537559a5SDmitry Kasatkin 	dd->req = req;
631537559a5SDmitry Kasatkin 	dd->total = req->nbytes;
6326242332fSJoel Fernandes 	dd->total_save = req->nbytes;
633537559a5SDmitry Kasatkin 	dd->in_sg = req->src;
634537559a5SDmitry Kasatkin 	dd->out_sg = req->dst;
635537559a5SDmitry Kasatkin 
6366242332fSJoel Fernandes 	if (omap_aes_check_aligned(dd->in_sg) ||
6376242332fSJoel Fernandes 	    omap_aes_check_aligned(dd->out_sg)) {
6386242332fSJoel Fernandes 		if (omap_aes_copy_sgs(dd))
6396242332fSJoel Fernandes 			pr_err("Failed to copy SGs for unaligned cases\n");
6406242332fSJoel Fernandes 		dd->sgs_copied = 1;
6416242332fSJoel Fernandes 	} else {
6426242332fSJoel Fernandes 		dd->sgs_copied = 0;
6436242332fSJoel Fernandes 	}
6446242332fSJoel Fernandes 
645e77c756eSJoel Fernandes 	dd->in_sg_len = scatterwalk_bytes_sglen(dd->in_sg, dd->total);
646e77c756eSJoel Fernandes 	dd->out_sg_len = scatterwalk_bytes_sglen(dd->out_sg, dd->total);
647e77c756eSJoel Fernandes 	BUG_ON(dd->in_sg_len < 0 || dd->out_sg_len < 0);
648e77c756eSJoel Fernandes 
649537559a5SDmitry Kasatkin 	rctx = ablkcipher_request_ctx(req);
650537559a5SDmitry Kasatkin 	ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
651537559a5SDmitry Kasatkin 	rctx->mode &= FLAGS_MODE_MASK;
652537559a5SDmitry Kasatkin 	dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
653537559a5SDmitry Kasatkin 
654537559a5SDmitry Kasatkin 	dd->ctx = ctx;
65567a730ceSDmitry Kasatkin 	ctx->dd = dd;
656537559a5SDmitry Kasatkin 
65783ea7e0fSDmitry Kasatkin 	err = omap_aes_write_ctrl(dd);
65883ea7e0fSDmitry Kasatkin 	if (!err)
65921fe9767SDmitry Kasatkin 		err = omap_aes_crypt_dma_start(dd);
66021fe9767SDmitry Kasatkin 	if (err) {
66121fe9767SDmitry Kasatkin 		/* aes_task will not finish it, so do it here */
66221fe9767SDmitry Kasatkin 		omap_aes_finish_req(dd, err);
66321fe9767SDmitry Kasatkin 		tasklet_schedule(&dd->queue_task);
664537559a5SDmitry Kasatkin 	}
665537559a5SDmitry Kasatkin 
66621fe9767SDmitry Kasatkin 	return ret; /* return ret, which is enqueue return value */
66721fe9767SDmitry Kasatkin }
66821fe9767SDmitry Kasatkin 
66921fe9767SDmitry Kasatkin static void omap_aes_done_task(unsigned long data)
670537559a5SDmitry Kasatkin {
671537559a5SDmitry Kasatkin 	struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
6726242332fSJoel Fernandes 	void *buf_in, *buf_out;
6736242332fSJoel Fernandes 	int pages;
674537559a5SDmitry Kasatkin 
6754b645c94SJoel Fernandes 	pr_debug("enter done_task\n");
676537559a5SDmitry Kasatkin 
67798837abcSJoel Fernandes 	if (!dd->pio_only) {
67898837abcSJoel Fernandes 		dma_sync_sg_for_device(dd->dev, dd->out_sg, dd->out_sg_len,
67998837abcSJoel Fernandes 				       DMA_FROM_DEVICE);
6806242332fSJoel Fernandes 		dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
6816242332fSJoel Fernandes 		dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
6826242332fSJoel Fernandes 			     DMA_FROM_DEVICE);
6834b645c94SJoel Fernandes 		omap_aes_crypt_dma_stop(dd);
68498837abcSJoel Fernandes 	}
6856242332fSJoel Fernandes 
6866242332fSJoel Fernandes 	if (dd->sgs_copied) {
6876242332fSJoel Fernandes 		buf_in = sg_virt(&dd->in_sgl);
6886242332fSJoel Fernandes 		buf_out = sg_virt(&dd->out_sgl);
6896242332fSJoel Fernandes 
6906242332fSJoel Fernandes 		sg_copy_buf(buf_out, dd->orig_out, 0, dd->total_save, 1);
6916242332fSJoel Fernandes 
6926242332fSJoel Fernandes 		pages = get_order(dd->total_save);
6936242332fSJoel Fernandes 		free_pages((unsigned long)buf_in, pages);
6946242332fSJoel Fernandes 		free_pages((unsigned long)buf_out, pages);
6956242332fSJoel Fernandes 	}
6966242332fSJoel Fernandes 
6974b645c94SJoel Fernandes 	omap_aes_finish_req(dd, 0);
69821fe9767SDmitry Kasatkin 	omap_aes_handle_queue(dd, NULL);
699537559a5SDmitry Kasatkin 
700537559a5SDmitry Kasatkin 	pr_debug("exit\n");
701537559a5SDmitry Kasatkin }
702537559a5SDmitry Kasatkin 
70321fe9767SDmitry Kasatkin static void omap_aes_queue_task(unsigned long data)
70421fe9767SDmitry Kasatkin {
70521fe9767SDmitry Kasatkin 	struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
70621fe9767SDmitry Kasatkin 
70721fe9767SDmitry Kasatkin 	omap_aes_handle_queue(dd, NULL);
70821fe9767SDmitry Kasatkin }
70921fe9767SDmitry Kasatkin 
710537559a5SDmitry Kasatkin static int omap_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
711537559a5SDmitry Kasatkin {
712537559a5SDmitry Kasatkin 	struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
713537559a5SDmitry Kasatkin 			crypto_ablkcipher_reqtfm(req));
714537559a5SDmitry Kasatkin 	struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req);
715537559a5SDmitry Kasatkin 	struct omap_aes_dev *dd;
716537559a5SDmitry Kasatkin 
717537559a5SDmitry Kasatkin 	pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->nbytes,
718537559a5SDmitry Kasatkin 		  !!(mode & FLAGS_ENCRYPT),
719537559a5SDmitry Kasatkin 		  !!(mode & FLAGS_CBC));
720537559a5SDmitry Kasatkin 
72121fe9767SDmitry Kasatkin 	if (!IS_ALIGNED(req->nbytes, AES_BLOCK_SIZE)) {
72221fe9767SDmitry Kasatkin 		pr_err("request size is not exact amount of AES blocks\n");
72321fe9767SDmitry Kasatkin 		return -EINVAL;
72421fe9767SDmitry Kasatkin 	}
72521fe9767SDmitry Kasatkin 
726537559a5SDmitry Kasatkin 	dd = omap_aes_find_dev(ctx);
727537559a5SDmitry Kasatkin 	if (!dd)
728537559a5SDmitry Kasatkin 		return -ENODEV;
729537559a5SDmitry Kasatkin 
730537559a5SDmitry Kasatkin 	rctx->mode = mode;
731537559a5SDmitry Kasatkin 
73221fe9767SDmitry Kasatkin 	return omap_aes_handle_queue(dd, req);
733537559a5SDmitry Kasatkin }
734537559a5SDmitry Kasatkin 
735537559a5SDmitry Kasatkin /* ********************** ALG API ************************************ */
736537559a5SDmitry Kasatkin 
737537559a5SDmitry Kasatkin static int omap_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
738537559a5SDmitry Kasatkin 			   unsigned int keylen)
739537559a5SDmitry Kasatkin {
740537559a5SDmitry Kasatkin 	struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
741537559a5SDmitry Kasatkin 
742537559a5SDmitry Kasatkin 	if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
743537559a5SDmitry Kasatkin 		   keylen != AES_KEYSIZE_256)
744537559a5SDmitry Kasatkin 		return -EINVAL;
745537559a5SDmitry Kasatkin 
746537559a5SDmitry Kasatkin 	pr_debug("enter, keylen: %d\n", keylen);
747537559a5SDmitry Kasatkin 
748537559a5SDmitry Kasatkin 	memcpy(ctx->key, key, keylen);
749537559a5SDmitry Kasatkin 	ctx->keylen = keylen;
750537559a5SDmitry Kasatkin 
751537559a5SDmitry Kasatkin 	return 0;
752537559a5SDmitry Kasatkin }
753537559a5SDmitry Kasatkin 
754537559a5SDmitry Kasatkin static int omap_aes_ecb_encrypt(struct ablkcipher_request *req)
755537559a5SDmitry Kasatkin {
756537559a5SDmitry Kasatkin 	return omap_aes_crypt(req, FLAGS_ENCRYPT);
757537559a5SDmitry Kasatkin }
758537559a5SDmitry Kasatkin 
759537559a5SDmitry Kasatkin static int omap_aes_ecb_decrypt(struct ablkcipher_request *req)
760537559a5SDmitry Kasatkin {
761537559a5SDmitry Kasatkin 	return omap_aes_crypt(req, 0);
762537559a5SDmitry Kasatkin }
763537559a5SDmitry Kasatkin 
764537559a5SDmitry Kasatkin static int omap_aes_cbc_encrypt(struct ablkcipher_request *req)
765537559a5SDmitry Kasatkin {
766537559a5SDmitry Kasatkin 	return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
767537559a5SDmitry Kasatkin }
768537559a5SDmitry Kasatkin 
769537559a5SDmitry Kasatkin static int omap_aes_cbc_decrypt(struct ablkcipher_request *req)
770537559a5SDmitry Kasatkin {
771537559a5SDmitry Kasatkin 	return omap_aes_crypt(req, FLAGS_CBC);
772537559a5SDmitry Kasatkin }
773537559a5SDmitry Kasatkin 
774f9fb69e7SMark A. Greer static int omap_aes_ctr_encrypt(struct ablkcipher_request *req)
775f9fb69e7SMark A. Greer {
776f9fb69e7SMark A. Greer 	return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CTR);
777f9fb69e7SMark A. Greer }
778f9fb69e7SMark A. Greer 
779f9fb69e7SMark A. Greer static int omap_aes_ctr_decrypt(struct ablkcipher_request *req)
780f9fb69e7SMark A. Greer {
781f9fb69e7SMark A. Greer 	return omap_aes_crypt(req, FLAGS_CTR);
782f9fb69e7SMark A. Greer }
783f9fb69e7SMark A. Greer 
784537559a5SDmitry Kasatkin static int omap_aes_cra_init(struct crypto_tfm *tfm)
785537559a5SDmitry Kasatkin {
786a3485e68SJoel A Fernandes 	struct omap_aes_dev *dd = NULL;
787f7b2b5ddSNishanth Menon 	int err;
788537559a5SDmitry Kasatkin 
789a3485e68SJoel A Fernandes 	/* Find AES device, currently picks the first device */
790a3485e68SJoel A Fernandes 	spin_lock_bh(&list_lock);
791a3485e68SJoel A Fernandes 	list_for_each_entry(dd, &dev_list, list) {
792a3485e68SJoel A Fernandes 		break;
793a3485e68SJoel A Fernandes 	}
794a3485e68SJoel A Fernandes 	spin_unlock_bh(&list_lock);
795a3485e68SJoel A Fernandes 
796f7b2b5ddSNishanth Menon 	err = pm_runtime_get_sync(dd->dev);
797f7b2b5ddSNishanth Menon 	if (err < 0) {
798f7b2b5ddSNishanth Menon 		dev_err(dd->dev, "%s: failed to get_sync(%d)\n",
799f7b2b5ddSNishanth Menon 			__func__, err);
800f7b2b5ddSNishanth Menon 		return err;
801f7b2b5ddSNishanth Menon 	}
802f7b2b5ddSNishanth Menon 
803537559a5SDmitry Kasatkin 	tfm->crt_ablkcipher.reqsize = sizeof(struct omap_aes_reqctx);
804537559a5SDmitry Kasatkin 
805537559a5SDmitry Kasatkin 	return 0;
806537559a5SDmitry Kasatkin }
807537559a5SDmitry Kasatkin 
808537559a5SDmitry Kasatkin static void omap_aes_cra_exit(struct crypto_tfm *tfm)
809537559a5SDmitry Kasatkin {
810a3485e68SJoel A Fernandes 	struct omap_aes_dev *dd = NULL;
811a3485e68SJoel A Fernandes 
812a3485e68SJoel A Fernandes 	/* Find AES device, currently picks the first device */
813a3485e68SJoel A Fernandes 	spin_lock_bh(&list_lock);
814a3485e68SJoel A Fernandes 	list_for_each_entry(dd, &dev_list, list) {
815a3485e68SJoel A Fernandes 		break;
816a3485e68SJoel A Fernandes 	}
817a3485e68SJoel A Fernandes 	spin_unlock_bh(&list_lock);
818a3485e68SJoel A Fernandes 
819a3485e68SJoel A Fernandes 	pm_runtime_put_sync(dd->dev);
820537559a5SDmitry Kasatkin }
821537559a5SDmitry Kasatkin 
822537559a5SDmitry Kasatkin /* ********************** ALGS ************************************ */
823537559a5SDmitry Kasatkin 
824f9fb69e7SMark A. Greer static struct crypto_alg algs_ecb_cbc[] = {
825537559a5SDmitry Kasatkin {
826537559a5SDmitry Kasatkin 	.cra_name		= "ecb(aes)",
827537559a5SDmitry Kasatkin 	.cra_driver_name	= "ecb-aes-omap",
828537559a5SDmitry Kasatkin 	.cra_priority		= 100,
829d912bb76SNikos Mavrogiannopoulos 	.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER |
830d912bb76SNikos Mavrogiannopoulos 				  CRYPTO_ALG_KERN_DRIVER_ONLY |
831d912bb76SNikos Mavrogiannopoulos 				  CRYPTO_ALG_ASYNC,
832537559a5SDmitry Kasatkin 	.cra_blocksize		= AES_BLOCK_SIZE,
833537559a5SDmitry Kasatkin 	.cra_ctxsize		= sizeof(struct omap_aes_ctx),
834537559a5SDmitry Kasatkin 	.cra_alignmask		= 0,
835537559a5SDmitry Kasatkin 	.cra_type		= &crypto_ablkcipher_type,
836537559a5SDmitry Kasatkin 	.cra_module		= THIS_MODULE,
837537559a5SDmitry Kasatkin 	.cra_init		= omap_aes_cra_init,
838537559a5SDmitry Kasatkin 	.cra_exit		= omap_aes_cra_exit,
839537559a5SDmitry Kasatkin 	.cra_u.ablkcipher = {
840537559a5SDmitry Kasatkin 		.min_keysize	= AES_MIN_KEY_SIZE,
841537559a5SDmitry Kasatkin 		.max_keysize	= AES_MAX_KEY_SIZE,
842537559a5SDmitry Kasatkin 		.setkey		= omap_aes_setkey,
843537559a5SDmitry Kasatkin 		.encrypt	= omap_aes_ecb_encrypt,
844537559a5SDmitry Kasatkin 		.decrypt	= omap_aes_ecb_decrypt,
845537559a5SDmitry Kasatkin 	}
846537559a5SDmitry Kasatkin },
847537559a5SDmitry Kasatkin {
848537559a5SDmitry Kasatkin 	.cra_name		= "cbc(aes)",
849537559a5SDmitry Kasatkin 	.cra_driver_name	= "cbc-aes-omap",
850537559a5SDmitry Kasatkin 	.cra_priority		= 100,
851d912bb76SNikos Mavrogiannopoulos 	.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER |
852d912bb76SNikos Mavrogiannopoulos 				  CRYPTO_ALG_KERN_DRIVER_ONLY |
853d912bb76SNikos Mavrogiannopoulos 				  CRYPTO_ALG_ASYNC,
854537559a5SDmitry Kasatkin 	.cra_blocksize		= AES_BLOCK_SIZE,
855537559a5SDmitry Kasatkin 	.cra_ctxsize		= sizeof(struct omap_aes_ctx),
856537559a5SDmitry Kasatkin 	.cra_alignmask		= 0,
857537559a5SDmitry Kasatkin 	.cra_type		= &crypto_ablkcipher_type,
858537559a5SDmitry Kasatkin 	.cra_module		= THIS_MODULE,
859537559a5SDmitry Kasatkin 	.cra_init		= omap_aes_cra_init,
860537559a5SDmitry Kasatkin 	.cra_exit		= omap_aes_cra_exit,
861537559a5SDmitry Kasatkin 	.cra_u.ablkcipher = {
862537559a5SDmitry Kasatkin 		.min_keysize	= AES_MIN_KEY_SIZE,
863537559a5SDmitry Kasatkin 		.max_keysize	= AES_MAX_KEY_SIZE,
864537559a5SDmitry Kasatkin 		.ivsize		= AES_BLOCK_SIZE,
865537559a5SDmitry Kasatkin 		.setkey		= omap_aes_setkey,
866537559a5SDmitry Kasatkin 		.encrypt	= omap_aes_cbc_encrypt,
867537559a5SDmitry Kasatkin 		.decrypt	= omap_aes_cbc_decrypt,
868537559a5SDmitry Kasatkin 	}
869537559a5SDmitry Kasatkin }
870537559a5SDmitry Kasatkin };
871537559a5SDmitry Kasatkin 
872f9fb69e7SMark A. Greer static struct crypto_alg algs_ctr[] = {
873f9fb69e7SMark A. Greer {
874f9fb69e7SMark A. Greer 	.cra_name		= "ctr(aes)",
875f9fb69e7SMark A. Greer 	.cra_driver_name	= "ctr-aes-omap",
876f9fb69e7SMark A. Greer 	.cra_priority		= 100,
877f9fb69e7SMark A. Greer 	.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER |
878f9fb69e7SMark A. Greer 				  CRYPTO_ALG_KERN_DRIVER_ONLY |
879f9fb69e7SMark A. Greer 				  CRYPTO_ALG_ASYNC,
880f9fb69e7SMark A. Greer 	.cra_blocksize		= AES_BLOCK_SIZE,
881f9fb69e7SMark A. Greer 	.cra_ctxsize		= sizeof(struct omap_aes_ctx),
882f9fb69e7SMark A. Greer 	.cra_alignmask		= 0,
883f9fb69e7SMark A. Greer 	.cra_type		= &crypto_ablkcipher_type,
884f9fb69e7SMark A. Greer 	.cra_module		= THIS_MODULE,
885f9fb69e7SMark A. Greer 	.cra_init		= omap_aes_cra_init,
886f9fb69e7SMark A. Greer 	.cra_exit		= omap_aes_cra_exit,
887f9fb69e7SMark A. Greer 	.cra_u.ablkcipher = {
888f9fb69e7SMark A. Greer 		.min_keysize	= AES_MIN_KEY_SIZE,
889f9fb69e7SMark A. Greer 		.max_keysize	= AES_MAX_KEY_SIZE,
890f9fb69e7SMark A. Greer 		.geniv		= "eseqiv",
891f9fb69e7SMark A. Greer 		.ivsize		= AES_BLOCK_SIZE,
892f9fb69e7SMark A. Greer 		.setkey		= omap_aes_setkey,
893f9fb69e7SMark A. Greer 		.encrypt	= omap_aes_ctr_encrypt,
894f9fb69e7SMark A. Greer 		.decrypt	= omap_aes_ctr_decrypt,
895f9fb69e7SMark A. Greer 	}
896f9fb69e7SMark A. Greer } ,
897f9fb69e7SMark A. Greer };
898f9fb69e7SMark A. Greer 
899f9fb69e7SMark A. Greer static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc[] = {
900f9fb69e7SMark A. Greer 	{
901f9fb69e7SMark A. Greer 		.algs_list	= algs_ecb_cbc,
902f9fb69e7SMark A. Greer 		.size		= ARRAY_SIZE(algs_ecb_cbc),
903f9fb69e7SMark A. Greer 	},
904f9fb69e7SMark A. Greer };
905f9fb69e7SMark A. Greer 
9060d35583aSMark A. Greer static const struct omap_aes_pdata omap_aes_pdata_omap2 = {
907f9fb69e7SMark A. Greer 	.algs_info	= omap_aes_algs_info_ecb_cbc,
908f9fb69e7SMark A. Greer 	.algs_info_size	= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc),
9090d35583aSMark A. Greer 	.trigger	= omap_aes_dma_trigger_omap2,
9100d35583aSMark A. Greer 	.key_ofs	= 0x1c,
9110d35583aSMark A. Greer 	.iv_ofs		= 0x20,
9120d35583aSMark A. Greer 	.ctrl_ofs	= 0x30,
9130d35583aSMark A. Greer 	.data_ofs	= 0x34,
9140d35583aSMark A. Greer 	.rev_ofs	= 0x44,
9150d35583aSMark A. Greer 	.mask_ofs	= 0x48,
9160d35583aSMark A. Greer 	.dma_enable_in	= BIT(2),
9170d35583aSMark A. Greer 	.dma_enable_out	= BIT(3),
9180d35583aSMark A. Greer 	.dma_start	= BIT(5),
9190d35583aSMark A. Greer 	.major_mask	= 0xf0,
9200d35583aSMark A. Greer 	.major_shift	= 4,
9210d35583aSMark A. Greer 	.minor_mask	= 0x0f,
9220d35583aSMark A. Greer 	.minor_shift	= 0,
9230d35583aSMark A. Greer };
9240d35583aSMark A. Greer 
925bc69d124SMark A. Greer #ifdef CONFIG_OF
926f9fb69e7SMark A. Greer static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr[] = {
927f9fb69e7SMark A. Greer 	{
928f9fb69e7SMark A. Greer 		.algs_list	= algs_ecb_cbc,
929f9fb69e7SMark A. Greer 		.size		= ARRAY_SIZE(algs_ecb_cbc),
930f9fb69e7SMark A. Greer 	},
931f9fb69e7SMark A. Greer 	{
932f9fb69e7SMark A. Greer 		.algs_list	= algs_ctr,
933f9fb69e7SMark A. Greer 		.size		= ARRAY_SIZE(algs_ctr),
934f9fb69e7SMark A. Greer 	},
935f9fb69e7SMark A. Greer };
936f9fb69e7SMark A. Greer 
937f9fb69e7SMark A. Greer static const struct omap_aes_pdata omap_aes_pdata_omap3 = {
938f9fb69e7SMark A. Greer 	.algs_info	= omap_aes_algs_info_ecb_cbc_ctr,
939f9fb69e7SMark A. Greer 	.algs_info_size	= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
940f9fb69e7SMark A. Greer 	.trigger	= omap_aes_dma_trigger_omap2,
941f9fb69e7SMark A. Greer 	.key_ofs	= 0x1c,
942f9fb69e7SMark A. Greer 	.iv_ofs		= 0x20,
943f9fb69e7SMark A. Greer 	.ctrl_ofs	= 0x30,
944f9fb69e7SMark A. Greer 	.data_ofs	= 0x34,
945f9fb69e7SMark A. Greer 	.rev_ofs	= 0x44,
946f9fb69e7SMark A. Greer 	.mask_ofs	= 0x48,
947f9fb69e7SMark A. Greer 	.dma_enable_in	= BIT(2),
948f9fb69e7SMark A. Greer 	.dma_enable_out	= BIT(3),
949f9fb69e7SMark A. Greer 	.dma_start	= BIT(5),
950f9fb69e7SMark A. Greer 	.major_mask	= 0xf0,
951f9fb69e7SMark A. Greer 	.major_shift	= 4,
952f9fb69e7SMark A. Greer 	.minor_mask	= 0x0f,
953f9fb69e7SMark A. Greer 	.minor_shift	= 0,
954f9fb69e7SMark A. Greer };
955f9fb69e7SMark A. Greer 
9560d35583aSMark A. Greer static const struct omap_aes_pdata omap_aes_pdata_omap4 = {
957f9fb69e7SMark A. Greer 	.algs_info	= omap_aes_algs_info_ecb_cbc_ctr,
958f9fb69e7SMark A. Greer 	.algs_info_size	= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
9590d35583aSMark A. Greer 	.trigger	= omap_aes_dma_trigger_omap4,
9600d35583aSMark A. Greer 	.key_ofs	= 0x3c,
9610d35583aSMark A. Greer 	.iv_ofs		= 0x40,
9620d35583aSMark A. Greer 	.ctrl_ofs	= 0x50,
9630d35583aSMark A. Greer 	.data_ofs	= 0x60,
9640d35583aSMark A. Greer 	.rev_ofs	= 0x80,
9650d35583aSMark A. Greer 	.mask_ofs	= 0x84,
96667216756SJoel Fernandes 	.irq_status_ofs = 0x8c,
96767216756SJoel Fernandes 	.irq_enable_ofs = 0x90,
9680d35583aSMark A. Greer 	.dma_enable_in	= BIT(5),
9690d35583aSMark A. Greer 	.dma_enable_out	= BIT(6),
9700d35583aSMark A. Greer 	.major_mask	= 0x0700,
9710d35583aSMark A. Greer 	.major_shift	= 8,
9720d35583aSMark A. Greer 	.minor_mask	= 0x003f,
9730d35583aSMark A. Greer 	.minor_shift	= 0,
9740d35583aSMark A. Greer };
9750d35583aSMark A. Greer 
9761bf95ccaSJoel Fernandes static irqreturn_t omap_aes_irq(int irq, void *dev_id)
9771bf95ccaSJoel Fernandes {
9781bf95ccaSJoel Fernandes 	struct omap_aes_dev *dd = dev_id;
9791bf95ccaSJoel Fernandes 	u32 status, i;
9801bf95ccaSJoel Fernandes 	u32 *src, *dst;
9811bf95ccaSJoel Fernandes 
9821bf95ccaSJoel Fernandes 	status = omap_aes_read(dd, AES_REG_IRQ_STATUS(dd));
9831bf95ccaSJoel Fernandes 	if (status & AES_REG_IRQ_DATA_IN) {
9841bf95ccaSJoel Fernandes 		omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
9851bf95ccaSJoel Fernandes 
9861bf95ccaSJoel Fernandes 		BUG_ON(!dd->in_sg);
9871bf95ccaSJoel Fernandes 
9881bf95ccaSJoel Fernandes 		BUG_ON(_calc_walked(in) > dd->in_sg->length);
9891bf95ccaSJoel Fernandes 
9901bf95ccaSJoel Fernandes 		src = sg_virt(dd->in_sg) + _calc_walked(in);
9911bf95ccaSJoel Fernandes 
9921bf95ccaSJoel Fernandes 		for (i = 0; i < AES_BLOCK_WORDS; i++) {
9931bf95ccaSJoel Fernandes 			omap_aes_write(dd, AES_REG_DATA_N(dd, i), *src);
9941bf95ccaSJoel Fernandes 
9951bf95ccaSJoel Fernandes 			scatterwalk_advance(&dd->in_walk, 4);
9961bf95ccaSJoel Fernandes 			if (dd->in_sg->length == _calc_walked(in)) {
9975be4d4c9SCristian Stoica 				dd->in_sg = sg_next(dd->in_sg);
9981bf95ccaSJoel Fernandes 				if (dd->in_sg) {
9991bf95ccaSJoel Fernandes 					scatterwalk_start(&dd->in_walk,
10001bf95ccaSJoel Fernandes 							  dd->in_sg);
10011bf95ccaSJoel Fernandes 					src = sg_virt(dd->in_sg) +
10021bf95ccaSJoel Fernandes 					      _calc_walked(in);
10031bf95ccaSJoel Fernandes 				}
10041bf95ccaSJoel Fernandes 			} else {
10051bf95ccaSJoel Fernandes 				src++;
10061bf95ccaSJoel Fernandes 			}
10071bf95ccaSJoel Fernandes 		}
10081bf95ccaSJoel Fernandes 
10091bf95ccaSJoel Fernandes 		/* Clear IRQ status */
10101bf95ccaSJoel Fernandes 		status &= ~AES_REG_IRQ_DATA_IN;
10111bf95ccaSJoel Fernandes 		omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
10121bf95ccaSJoel Fernandes 
10131bf95ccaSJoel Fernandes 		/* Enable DATA_OUT interrupt */
10141bf95ccaSJoel Fernandes 		omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x4);
10151bf95ccaSJoel Fernandes 
10161bf95ccaSJoel Fernandes 	} else if (status & AES_REG_IRQ_DATA_OUT) {
10171bf95ccaSJoel Fernandes 		omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
10181bf95ccaSJoel Fernandes 
10191bf95ccaSJoel Fernandes 		BUG_ON(!dd->out_sg);
10201bf95ccaSJoel Fernandes 
10211bf95ccaSJoel Fernandes 		BUG_ON(_calc_walked(out) > dd->out_sg->length);
10221bf95ccaSJoel Fernandes 
10231bf95ccaSJoel Fernandes 		dst = sg_virt(dd->out_sg) + _calc_walked(out);
10241bf95ccaSJoel Fernandes 
10251bf95ccaSJoel Fernandes 		for (i = 0; i < AES_BLOCK_WORDS; i++) {
10261bf95ccaSJoel Fernandes 			*dst = omap_aes_read(dd, AES_REG_DATA_N(dd, i));
10271bf95ccaSJoel Fernandes 			scatterwalk_advance(&dd->out_walk, 4);
10281bf95ccaSJoel Fernandes 			if (dd->out_sg->length == _calc_walked(out)) {
10295be4d4c9SCristian Stoica 				dd->out_sg = sg_next(dd->out_sg);
10301bf95ccaSJoel Fernandes 				if (dd->out_sg) {
10311bf95ccaSJoel Fernandes 					scatterwalk_start(&dd->out_walk,
10321bf95ccaSJoel Fernandes 							  dd->out_sg);
10331bf95ccaSJoel Fernandes 					dst = sg_virt(dd->out_sg) +
10341bf95ccaSJoel Fernandes 					      _calc_walked(out);
10351bf95ccaSJoel Fernandes 				}
10361bf95ccaSJoel Fernandes 			} else {
10371bf95ccaSJoel Fernandes 				dst++;
10381bf95ccaSJoel Fernandes 			}
10391bf95ccaSJoel Fernandes 		}
10401bf95ccaSJoel Fernandes 
10411bf95ccaSJoel Fernandes 		dd->total -= AES_BLOCK_SIZE;
10421bf95ccaSJoel Fernandes 
10431bf95ccaSJoel Fernandes 		BUG_ON(dd->total < 0);
10441bf95ccaSJoel Fernandes 
10451bf95ccaSJoel Fernandes 		/* Clear IRQ status */
10461bf95ccaSJoel Fernandes 		status &= ~AES_REG_IRQ_DATA_OUT;
10471bf95ccaSJoel Fernandes 		omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
10481bf95ccaSJoel Fernandes 
10491bf95ccaSJoel Fernandes 		if (!dd->total)
10501bf95ccaSJoel Fernandes 			/* All bytes read! */
10511bf95ccaSJoel Fernandes 			tasklet_schedule(&dd->done_task);
10521bf95ccaSJoel Fernandes 		else
10531bf95ccaSJoel Fernandes 			/* Enable DATA_IN interrupt for next block */
10541bf95ccaSJoel Fernandes 			omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
10551bf95ccaSJoel Fernandes 	}
10561bf95ccaSJoel Fernandes 
10571bf95ccaSJoel Fernandes 	return IRQ_HANDLED;
10581bf95ccaSJoel Fernandes }
10591bf95ccaSJoel Fernandes 
1060bc69d124SMark A. Greer static const struct of_device_id omap_aes_of_match[] = {
1061bc69d124SMark A. Greer 	{
1062bc69d124SMark A. Greer 		.compatible	= "ti,omap2-aes",
10630d35583aSMark A. Greer 		.data		= &omap_aes_pdata_omap2,
10640d35583aSMark A. Greer 	},
10650d35583aSMark A. Greer 	{
1066f9fb69e7SMark A. Greer 		.compatible	= "ti,omap3-aes",
1067f9fb69e7SMark A. Greer 		.data		= &omap_aes_pdata_omap3,
1068f9fb69e7SMark A. Greer 	},
1069f9fb69e7SMark A. Greer 	{
10700d35583aSMark A. Greer 		.compatible	= "ti,omap4-aes",
10710d35583aSMark A. Greer 		.data		= &omap_aes_pdata_omap4,
1072bc69d124SMark A. Greer 	},
1073bc69d124SMark A. Greer 	{},
1074bc69d124SMark A. Greer };
1075bc69d124SMark A. Greer MODULE_DEVICE_TABLE(of, omap_aes_of_match);
1076bc69d124SMark A. Greer 
1077bc69d124SMark A. Greer static int omap_aes_get_res_of(struct omap_aes_dev *dd,
1078bc69d124SMark A. Greer 		struct device *dev, struct resource *res)
1079bc69d124SMark A. Greer {
1080bc69d124SMark A. Greer 	struct device_node *node = dev->of_node;
1081bc69d124SMark A. Greer 	const struct of_device_id *match;
1082bc69d124SMark A. Greer 	int err = 0;
1083bc69d124SMark A. Greer 
1084bc69d124SMark A. Greer 	match = of_match_device(of_match_ptr(omap_aes_of_match), dev);
1085bc69d124SMark A. Greer 	if (!match) {
1086bc69d124SMark A. Greer 		dev_err(dev, "no compatible OF match\n");
1087bc69d124SMark A. Greer 		err = -EINVAL;
1088bc69d124SMark A. Greer 		goto err;
1089bc69d124SMark A. Greer 	}
1090bc69d124SMark A. Greer 
1091bc69d124SMark A. Greer 	err = of_address_to_resource(node, 0, res);
1092bc69d124SMark A. Greer 	if (err < 0) {
1093bc69d124SMark A. Greer 		dev_err(dev, "can't translate OF node address\n");
1094bc69d124SMark A. Greer 		err = -EINVAL;
1095bc69d124SMark A. Greer 		goto err;
1096bc69d124SMark A. Greer 	}
1097bc69d124SMark A. Greer 
1098bc69d124SMark A. Greer 	dd->dma_out = -1; /* Dummy value that's unused */
1099bc69d124SMark A. Greer 	dd->dma_in = -1; /* Dummy value that's unused */
1100bc69d124SMark A. Greer 
11010d35583aSMark A. Greer 	dd->pdata = match->data;
11020d35583aSMark A. Greer 
1103bc69d124SMark A. Greer err:
1104bc69d124SMark A. Greer 	return err;
1105bc69d124SMark A. Greer }
1106bc69d124SMark A. Greer #else
1107bc69d124SMark A. Greer static const struct of_device_id omap_aes_of_match[] = {
1108bc69d124SMark A. Greer 	{},
1109bc69d124SMark A. Greer };
1110bc69d124SMark A. Greer 
1111bc69d124SMark A. Greer static int omap_aes_get_res_of(struct omap_aes_dev *dd,
1112bc69d124SMark A. Greer 		struct device *dev, struct resource *res)
1113bc69d124SMark A. Greer {
1114bc69d124SMark A. Greer 	return -EINVAL;
1115bc69d124SMark A. Greer }
1116bc69d124SMark A. Greer #endif
1117bc69d124SMark A. Greer 
1118bc69d124SMark A. Greer static int omap_aes_get_res_pdev(struct omap_aes_dev *dd,
1119bc69d124SMark A. Greer 		struct platform_device *pdev, struct resource *res)
1120bc69d124SMark A. Greer {
1121bc69d124SMark A. Greer 	struct device *dev = &pdev->dev;
1122bc69d124SMark A. Greer 	struct resource *r;
1123bc69d124SMark A. Greer 	int err = 0;
1124bc69d124SMark A. Greer 
1125bc69d124SMark A. Greer 	/* Get the base address */
1126bc69d124SMark A. Greer 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1127bc69d124SMark A. Greer 	if (!r) {
1128bc69d124SMark A. Greer 		dev_err(dev, "no MEM resource info\n");
1129bc69d124SMark A. Greer 		err = -ENODEV;
1130bc69d124SMark A. Greer 		goto err;
1131bc69d124SMark A. Greer 	}
1132bc69d124SMark A. Greer 	memcpy(res, r, sizeof(*res));
1133bc69d124SMark A. Greer 
1134bc69d124SMark A. Greer 	/* Get the DMA out channel */
1135bc69d124SMark A. Greer 	r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1136bc69d124SMark A. Greer 	if (!r) {
1137bc69d124SMark A. Greer 		dev_err(dev, "no DMA out resource info\n");
1138bc69d124SMark A. Greer 		err = -ENODEV;
1139bc69d124SMark A. Greer 		goto err;
1140bc69d124SMark A. Greer 	}
1141bc69d124SMark A. Greer 	dd->dma_out = r->start;
1142bc69d124SMark A. Greer 
1143bc69d124SMark A. Greer 	/* Get the DMA in channel */
1144bc69d124SMark A. Greer 	r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1145bc69d124SMark A. Greer 	if (!r) {
1146bc69d124SMark A. Greer 		dev_err(dev, "no DMA in resource info\n");
1147bc69d124SMark A. Greer 		err = -ENODEV;
1148bc69d124SMark A. Greer 		goto err;
1149bc69d124SMark A. Greer 	}
1150bc69d124SMark A. Greer 	dd->dma_in = r->start;
1151bc69d124SMark A. Greer 
11520d35583aSMark A. Greer 	/* Only OMAP2/3 can be non-DT */
11530d35583aSMark A. Greer 	dd->pdata = &omap_aes_pdata_omap2;
11540d35583aSMark A. Greer 
1155bc69d124SMark A. Greer err:
1156bc69d124SMark A. Greer 	return err;
1157bc69d124SMark A. Greer }
1158bc69d124SMark A. Greer 
1159537559a5SDmitry Kasatkin static int omap_aes_probe(struct platform_device *pdev)
1160537559a5SDmitry Kasatkin {
1161537559a5SDmitry Kasatkin 	struct device *dev = &pdev->dev;
1162537559a5SDmitry Kasatkin 	struct omap_aes_dev *dd;
1163f9fb69e7SMark A. Greer 	struct crypto_alg *algp;
1164bc69d124SMark A. Greer 	struct resource res;
11651801ad94SJoel Fernandes 	int err = -ENOMEM, i, j, irq = -1;
1166537559a5SDmitry Kasatkin 	u32 reg;
1167537559a5SDmitry Kasatkin 
116805007c10SJoel Fernandes 	dd = devm_kzalloc(dev, sizeof(struct omap_aes_dev), GFP_KERNEL);
1169537559a5SDmitry Kasatkin 	if (dd == NULL) {
1170537559a5SDmitry Kasatkin 		dev_err(dev, "unable to alloc data struct.\n");
1171537559a5SDmitry Kasatkin 		goto err_data;
1172537559a5SDmitry Kasatkin 	}
1173537559a5SDmitry Kasatkin 	dd->dev = dev;
1174537559a5SDmitry Kasatkin 	platform_set_drvdata(pdev, dd);
1175537559a5SDmitry Kasatkin 
1176537559a5SDmitry Kasatkin 	spin_lock_init(&dd->lock);
1177537559a5SDmitry Kasatkin 	crypto_init_queue(&dd->queue, OMAP_AES_QUEUE_LENGTH);
1178537559a5SDmitry Kasatkin 
1179bc69d124SMark A. Greer 	err = (dev->of_node) ? omap_aes_get_res_of(dd, dev, &res) :
1180bc69d124SMark A. Greer 			       omap_aes_get_res_pdev(dd, pdev, &res);
1181bc69d124SMark A. Greer 	if (err)
1182537559a5SDmitry Kasatkin 		goto err_res;
1183537559a5SDmitry Kasatkin 
118430862281SLaurent Navet 	dd->io_base = devm_ioremap_resource(dev, &res);
118530862281SLaurent Navet 	if (IS_ERR(dd->io_base)) {
118630862281SLaurent Navet 		err = PTR_ERR(dd->io_base);
11875946c4a5SMark A. Greer 		goto err_res;
1188537559a5SDmitry Kasatkin 	}
1189bc69d124SMark A. Greer 	dd->phys_base = res.start;
1190537559a5SDmitry Kasatkin 
11915946c4a5SMark A. Greer 	pm_runtime_enable(dev);
1192f7b2b5ddSNishanth Menon 	err = pm_runtime_get_sync(dev);
1193f7b2b5ddSNishanth Menon 	if (err < 0) {
1194f7b2b5ddSNishanth Menon 		dev_err(dev, "%s: failed to get_sync(%d)\n",
1195f7b2b5ddSNishanth Menon 			__func__, err);
1196f7b2b5ddSNishanth Menon 		goto err_res;
1197f7b2b5ddSNishanth Menon 	}
11985946c4a5SMark A. Greer 
11990d35583aSMark A. Greer 	omap_aes_dma_stop(dd);
12000d35583aSMark A. Greer 
12010d35583aSMark A. Greer 	reg = omap_aes_read(dd, AES_REG_REV(dd));
12025946c4a5SMark A. Greer 
12035946c4a5SMark A. Greer 	pm_runtime_put_sync(dev);
1204537559a5SDmitry Kasatkin 
12050d35583aSMark A. Greer 	dev_info(dev, "OMAP AES hw accel rev: %u.%u\n",
12060d35583aSMark A. Greer 		 (reg & dd->pdata->major_mask) >> dd->pdata->major_shift,
12070d35583aSMark A. Greer 		 (reg & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
12080d35583aSMark A. Greer 
120921fe9767SDmitry Kasatkin 	tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd);
121021fe9767SDmitry Kasatkin 	tasklet_init(&dd->queue_task, omap_aes_queue_task, (unsigned long)dd);
1211537559a5SDmitry Kasatkin 
1212537559a5SDmitry Kasatkin 	err = omap_aes_dma_init(dd);
12131801ad94SJoel Fernandes 	if (err && AES_REG_IRQ_STATUS(dd) && AES_REG_IRQ_ENABLE(dd)) {
12141801ad94SJoel Fernandes 		dd->pio_only = 1;
12151801ad94SJoel Fernandes 
12161801ad94SJoel Fernandes 		irq = platform_get_irq(pdev, 0);
12171801ad94SJoel Fernandes 		if (irq < 0) {
12181801ad94SJoel Fernandes 			dev_err(dev, "can't get IRQ resource\n");
12191801ad94SJoel Fernandes 			goto err_irq;
12201801ad94SJoel Fernandes 		}
12211801ad94SJoel Fernandes 
1222bce2a228SJoel Fernandes 		err = devm_request_irq(dev, irq, omap_aes_irq, 0,
12231801ad94SJoel Fernandes 				dev_name(dev), dd);
12241801ad94SJoel Fernandes 		if (err) {
12251801ad94SJoel Fernandes 			dev_err(dev, "Unable to grab omap-aes IRQ\n");
12261801ad94SJoel Fernandes 			goto err_irq;
12271801ad94SJoel Fernandes 		}
12281801ad94SJoel Fernandes 	}
12291801ad94SJoel Fernandes 
1230537559a5SDmitry Kasatkin 
1231537559a5SDmitry Kasatkin 	INIT_LIST_HEAD(&dd->list);
1232537559a5SDmitry Kasatkin 	spin_lock(&list_lock);
1233537559a5SDmitry Kasatkin 	list_add_tail(&dd->list, &dev_list);
1234537559a5SDmitry Kasatkin 	spin_unlock(&list_lock);
1235537559a5SDmitry Kasatkin 
1236f9fb69e7SMark A. Greer 	for (i = 0; i < dd->pdata->algs_info_size; i++) {
1237f9fb69e7SMark A. Greer 		for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
1238f9fb69e7SMark A. Greer 			algp = &dd->pdata->algs_info[i].algs_list[j];
1239f9fb69e7SMark A. Greer 
1240f9fb69e7SMark A. Greer 			pr_debug("reg alg: %s\n", algp->cra_name);
1241f9fb69e7SMark A. Greer 			INIT_LIST_HEAD(&algp->cra_list);
1242f9fb69e7SMark A. Greer 
1243f9fb69e7SMark A. Greer 			err = crypto_register_alg(algp);
1244537559a5SDmitry Kasatkin 			if (err)
1245537559a5SDmitry Kasatkin 				goto err_algs;
1246f9fb69e7SMark A. Greer 
1247f9fb69e7SMark A. Greer 			dd->pdata->algs_info[i].registered++;
1248f9fb69e7SMark A. Greer 		}
1249537559a5SDmitry Kasatkin 	}
1250537559a5SDmitry Kasatkin 
1251537559a5SDmitry Kasatkin 	return 0;
1252537559a5SDmitry Kasatkin err_algs:
1253f9fb69e7SMark A. Greer 	for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1254f9fb69e7SMark A. Greer 		for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1255f9fb69e7SMark A. Greer 			crypto_unregister_alg(
1256f9fb69e7SMark A. Greer 					&dd->pdata->algs_info[i].algs_list[j]);
1257bce2a228SJoel Fernandes 	if (!dd->pio_only)
1258537559a5SDmitry Kasatkin 		omap_aes_dma_cleanup(dd);
12591801ad94SJoel Fernandes err_irq:
126021fe9767SDmitry Kasatkin 	tasklet_kill(&dd->done_task);
126121fe9767SDmitry Kasatkin 	tasklet_kill(&dd->queue_task);
12625946c4a5SMark A. Greer 	pm_runtime_disable(dev);
1263537559a5SDmitry Kasatkin err_res:
1264537559a5SDmitry Kasatkin 	dd = NULL;
1265537559a5SDmitry Kasatkin err_data:
1266537559a5SDmitry Kasatkin 	dev_err(dev, "initialization failed.\n");
1267537559a5SDmitry Kasatkin 	return err;
1268537559a5SDmitry Kasatkin }
1269537559a5SDmitry Kasatkin 
1270537559a5SDmitry Kasatkin static int omap_aes_remove(struct platform_device *pdev)
1271537559a5SDmitry Kasatkin {
1272537559a5SDmitry Kasatkin 	struct omap_aes_dev *dd = platform_get_drvdata(pdev);
1273f9fb69e7SMark A. Greer 	int i, j;
1274537559a5SDmitry Kasatkin 
1275537559a5SDmitry Kasatkin 	if (!dd)
1276537559a5SDmitry Kasatkin 		return -ENODEV;
1277537559a5SDmitry Kasatkin 
1278537559a5SDmitry Kasatkin 	spin_lock(&list_lock);
1279537559a5SDmitry Kasatkin 	list_del(&dd->list);
1280537559a5SDmitry Kasatkin 	spin_unlock(&list_lock);
1281537559a5SDmitry Kasatkin 
1282f9fb69e7SMark A. Greer 	for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1283f9fb69e7SMark A. Greer 		for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1284f9fb69e7SMark A. Greer 			crypto_unregister_alg(
1285f9fb69e7SMark A. Greer 					&dd->pdata->algs_info[i].algs_list[j]);
1286537559a5SDmitry Kasatkin 
128721fe9767SDmitry Kasatkin 	tasklet_kill(&dd->done_task);
128821fe9767SDmitry Kasatkin 	tasklet_kill(&dd->queue_task);
1289537559a5SDmitry Kasatkin 	omap_aes_dma_cleanup(dd);
12905946c4a5SMark A. Greer 	pm_runtime_disable(dd->dev);
1291537559a5SDmitry Kasatkin 	dd = NULL;
1292537559a5SDmitry Kasatkin 
1293537559a5SDmitry Kasatkin 	return 0;
1294537559a5SDmitry Kasatkin }
1295537559a5SDmitry Kasatkin 
12960635fb3aSMark A. Greer #ifdef CONFIG_PM_SLEEP
12970635fb3aSMark A. Greer static int omap_aes_suspend(struct device *dev)
12980635fb3aSMark A. Greer {
12990635fb3aSMark A. Greer 	pm_runtime_put_sync(dev);
13000635fb3aSMark A. Greer 	return 0;
13010635fb3aSMark A. Greer }
13020635fb3aSMark A. Greer 
13030635fb3aSMark A. Greer static int omap_aes_resume(struct device *dev)
13040635fb3aSMark A. Greer {
13050635fb3aSMark A. Greer 	pm_runtime_get_sync(dev);
13060635fb3aSMark A. Greer 	return 0;
13070635fb3aSMark A. Greer }
13080635fb3aSMark A. Greer #endif
13090635fb3aSMark A. Greer 
1310ea7b2843SJingoo Han static SIMPLE_DEV_PM_OPS(omap_aes_pm_ops, omap_aes_suspend, omap_aes_resume);
13110635fb3aSMark A. Greer 
1312537559a5SDmitry Kasatkin static struct platform_driver omap_aes_driver = {
1313537559a5SDmitry Kasatkin 	.probe	= omap_aes_probe,
1314537559a5SDmitry Kasatkin 	.remove	= omap_aes_remove,
1315537559a5SDmitry Kasatkin 	.driver	= {
1316537559a5SDmitry Kasatkin 		.name	= "omap-aes",
13170635fb3aSMark A. Greer 		.pm	= &omap_aes_pm_ops,
1318bc69d124SMark A. Greer 		.of_match_table	= omap_aes_of_match,
1319537559a5SDmitry Kasatkin 	},
1320537559a5SDmitry Kasatkin };
1321537559a5SDmitry Kasatkin 
132294e51df9SSachin Kamat module_platform_driver(omap_aes_driver);
1323537559a5SDmitry Kasatkin 
1324537559a5SDmitry Kasatkin MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
1325537559a5SDmitry Kasatkin MODULE_LICENSE("GPL v2");
1326537559a5SDmitry Kasatkin MODULE_AUTHOR("Dmitry Kasatkin");
1327537559a5SDmitry Kasatkin 
1328