blob: 68c44253137d18a9a1fe33643f5e30483b75793b [file] [log] [blame]
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001/*
2 * Cryptographic API.
3 *
4 * Support for OMAP AES HW acceleration.
5 *
6 * Copyright (c) 2010 Nokia Corporation
7 * Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
Mark A. Greer0d355832013-01-08 11:57:46 -07008 * Copyright (c) 2011 Texas Instruments Incorporated
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
13 *
14 */
15
Joel Fernandes016af9b2013-08-18 00:56:11 -050016#define pr_fmt(fmt) "%20s: " fmt, __func__
17#define prn(num) pr_debug(#num "=%d\n", num)
18#define prx(num) pr_debug(#num "=%x\n", num)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080019
20#include <linux/err.h>
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/errno.h>
24#include <linux/kernel.h>
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080025#include <linux/platform_device.h>
26#include <linux/scatterlist.h>
27#include <linux/dma-mapping.h>
Mark A. Greerebedbf72013-01-08 11:57:42 -070028#include <linux/dmaengine.h>
29#include <linux/omap-dma.h>
Mark A. Greer5946c4a2013-01-08 11:57:40 -070030#include <linux/pm_runtime.h>
Mark A. Greerbc69d122013-01-08 11:57:44 -070031#include <linux/of.h>
32#include <linux/of_device.h>
33#include <linux/of_address.h>
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080034#include <linux/io.h>
35#include <linux/crypto.h>
36#include <linux/interrupt.h>
37#include <crypto/scatterwalk.h>
38#include <crypto/aes.h>
39
Mark A. Greerebedbf72013-01-08 11:57:42 -070040#define DST_MAXBURST 4
41#define DMA_MIN (DST_MAXBURST * sizeof(u32))
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080042
43/* OMAP TRM gives bitfields as start:end, where start is the higher bit
44 number. For example 7:0 */
45#define FLD_MASK(start, end) (((1 << ((start) - (end) + 1)) - 1) << (end))
46#define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
47
Mark A. Greer0d355832013-01-08 11:57:46 -070048#define AES_REG_KEY(dd, x) ((dd)->pdata->key_ofs - \
49 ((x ^ 0x01) * 0x04))
50#define AES_REG_IV(dd, x) ((dd)->pdata->iv_ofs + ((x) * 0x04))
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080051
Mark A. Greer0d355832013-01-08 11:57:46 -070052#define AES_REG_CTRL(dd) ((dd)->pdata->ctrl_ofs)
Mark A. Greerf9fb69e2013-01-08 11:57:47 -070053#define AES_REG_CTRL_CTR_WIDTH_MASK (3 << 7)
54#define AES_REG_CTRL_CTR_WIDTH_32 (0 << 7)
55#define AES_REG_CTRL_CTR_WIDTH_64 (1 << 7)
56#define AES_REG_CTRL_CTR_WIDTH_96 (2 << 7)
57#define AES_REG_CTRL_CTR_WIDTH_128 (3 << 7)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080058#define AES_REG_CTRL_CTR (1 << 6)
59#define AES_REG_CTRL_CBC (1 << 5)
60#define AES_REG_CTRL_KEY_SIZE (3 << 3)
61#define AES_REG_CTRL_DIRECTION (1 << 2)
62#define AES_REG_CTRL_INPUT_READY (1 << 1)
63#define AES_REG_CTRL_OUTPUT_READY (1 << 0)
64
Mark A. Greer0d355832013-01-08 11:57:46 -070065#define AES_REG_DATA_N(dd, x) ((dd)->pdata->data_ofs + ((x) * 0x04))
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080066
Mark A. Greer0d355832013-01-08 11:57:46 -070067#define AES_REG_REV(dd) ((dd)->pdata->rev_ofs)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080068
Mark A. Greer0d355832013-01-08 11:57:46 -070069#define AES_REG_MASK(dd) ((dd)->pdata->mask_ofs)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080070#define AES_REG_MASK_SIDLE (1 << 6)
71#define AES_REG_MASK_START (1 << 5)
72#define AES_REG_MASK_DMA_OUT_EN (1 << 3)
73#define AES_REG_MASK_DMA_IN_EN (1 << 2)
74#define AES_REG_MASK_SOFTRESET (1 << 1)
75#define AES_REG_AUTOIDLE (1 << 0)
76
Mark A. Greer0d355832013-01-08 11:57:46 -070077#define AES_REG_LENGTH_N(x) (0x54 + ((x) * 0x04))
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080078
Joel Fernandes67216752013-08-17 21:42:28 -050079#define AES_REG_IRQ_STATUS(dd) ((dd)->pdata->irq_status_ofs)
80#define AES_REG_IRQ_ENABLE(dd) ((dd)->pdata->irq_enable_ofs)
81#define AES_REG_IRQ_DATA_IN BIT(1)
82#define AES_REG_IRQ_DATA_OUT BIT(2)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080083#define DEFAULT_TIMEOUT (5*HZ)
84
85#define FLAGS_MODE_MASK 0x000f
86#define FLAGS_ENCRYPT BIT(0)
87#define FLAGS_CBC BIT(1)
88#define FLAGS_GIV BIT(2)
Mark A. Greerf9fb69e2013-01-08 11:57:47 -070089#define FLAGS_CTR BIT(3)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080090
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +020091#define FLAGS_INIT BIT(4)
92#define FLAGS_FAST BIT(5)
93#define FLAGS_BUSY BIT(6)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080094
95struct omap_aes_ctx {
96 struct omap_aes_dev *dd;
97
98 int keylen;
99 u32 key[AES_KEYSIZE_256 / sizeof(u32)];
100 unsigned long flags;
101};
102
103struct omap_aes_reqctx {
104 unsigned long mode;
105};
106
107#define OMAP_AES_QUEUE_LENGTH 1
108#define OMAP_AES_CACHE_SIZE 0
109
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700110struct omap_aes_algs_info {
111 struct crypto_alg *algs_list;
112 unsigned int size;
113 unsigned int registered;
114};
115
Mark A. Greer0d355832013-01-08 11:57:46 -0700116struct omap_aes_pdata {
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700117 struct omap_aes_algs_info *algs_info;
118 unsigned int algs_info_size;
119
Mark A. Greer0d355832013-01-08 11:57:46 -0700120 void (*trigger)(struct omap_aes_dev *dd, int length);
121
122 u32 key_ofs;
123 u32 iv_ofs;
124 u32 ctrl_ofs;
125 u32 data_ofs;
126 u32 rev_ofs;
127 u32 mask_ofs;
Joel Fernandes67216752013-08-17 21:42:28 -0500128 u32 irq_enable_ofs;
129 u32 irq_status_ofs;
Mark A. Greer0d355832013-01-08 11:57:46 -0700130
131 u32 dma_enable_in;
132 u32 dma_enable_out;
133 u32 dma_start;
134
135 u32 major_mask;
136 u32 major_shift;
137 u32 minor_mask;
138 u32 minor_shift;
139};
140
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800141struct omap_aes_dev {
142 struct list_head list;
143 unsigned long phys_base;
Dmitry Kasatkinefce41b2010-11-30 10:13:32 +0200144 void __iomem *io_base;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800145 struct omap_aes_ctx *ctx;
146 struct device *dev;
147 unsigned long flags;
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200148 int err;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800149
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200150 spinlock_t lock;
151 struct crypto_queue queue;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800152
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200153 struct tasklet_struct done_task;
154 struct tasklet_struct queue_task;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800155
156 struct ablkcipher_request *req;
157 size_t total;
158 struct scatterlist *in_sg;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800159 struct scatterlist *out_sg;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800160 int dma_in;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700161 struct dma_chan *dma_lch_in;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800162 int dma_out;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700163 struct dma_chan *dma_lch_out;
Joel Fernandese77c7562013-08-17 21:42:24 -0500164 int in_sg_len;
165 int out_sg_len;
Mark A. Greer0d355832013-01-08 11:57:46 -0700166 const struct omap_aes_pdata *pdata;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800167};
168
169/* keep registered devices data here */
170static LIST_HEAD(dev_list);
171static DEFINE_SPINLOCK(list_lock);
172
Joel Fernandes016af9b2013-08-18 00:56:11 -0500173#ifdef DEBUG
174#define omap_aes_read(dd, offset) \
175({ \
176 int _read_ret; \
177 _read_ret = __raw_readl(dd->io_base + offset); \
178 pr_debug("omap_aes_read(" #offset "=%#x)= %#x\n", \
179 offset, _read_ret); \
180 _read_ret; \
181})
182#else
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800183static inline u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset)
184{
185 return __raw_readl(dd->io_base + offset);
186}
Joel Fernandes016af9b2013-08-18 00:56:11 -0500187#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800188
Joel Fernandes016af9b2013-08-18 00:56:11 -0500189#ifdef DEBUG
190#define omap_aes_write(dd, offset, value) \
191 do { \
192 pr_debug("omap_aes_write(" #offset "=%#x) value=%#x\n", \
193 offset, value); \
194 __raw_writel(value, dd->io_base + offset); \
195 } while (0)
196#else
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800197static inline void omap_aes_write(struct omap_aes_dev *dd, u32 offset,
198 u32 value)
199{
200 __raw_writel(value, dd->io_base + offset);
201}
Joel Fernandes016af9b2013-08-18 00:56:11 -0500202#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800203
204static inline void omap_aes_write_mask(struct omap_aes_dev *dd, u32 offset,
205 u32 value, u32 mask)
206{
207 u32 val;
208
209 val = omap_aes_read(dd, offset);
210 val &= ~mask;
211 val |= value;
212 omap_aes_write(dd, offset, val);
213}
214
215static void omap_aes_write_n(struct omap_aes_dev *dd, u32 offset,
216 u32 *value, int count)
217{
218 for (; count--; value++, offset += 4)
219 omap_aes_write(dd, offset, *value);
220}
221
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800222static int omap_aes_hw_init(struct omap_aes_dev *dd)
223{
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800224 if (!(dd->flags & FLAGS_INIT)) {
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200225 dd->flags |= FLAGS_INIT;
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200226 dd->err = 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800227 }
228
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200229 return 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800230}
231
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200232static int omap_aes_write_ctrl(struct omap_aes_dev *dd)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800233{
234 unsigned int key32;
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200235 int i, err;
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700236 u32 val, mask = 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800237
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200238 err = omap_aes_hw_init(dd);
239 if (err)
240 return err;
241
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800242 key32 = dd->ctx->keylen / sizeof(u32);
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200243
244 /* it seems a key should always be set even if it has not changed */
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800245 for (i = 0; i < key32; i++) {
Mark A. Greer0d355832013-01-08 11:57:46 -0700246 omap_aes_write(dd, AES_REG_KEY(dd, i),
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800247 __le32_to_cpu(dd->ctx->key[i]));
248 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800249
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700250 if ((dd->flags & (FLAGS_CBC | FLAGS_CTR)) && dd->req->info)
Mark A. Greer0d355832013-01-08 11:57:46 -0700251 omap_aes_write_n(dd, AES_REG_IV(dd, 0), dd->req->info, 4);
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200252
253 val = FLD_VAL(((dd->ctx->keylen >> 3) - 1), 4, 3);
254 if (dd->flags & FLAGS_CBC)
255 val |= AES_REG_CTRL_CBC;
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700256 if (dd->flags & FLAGS_CTR) {
257 val |= AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_32;
258 mask = AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_MASK;
259 }
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200260 if (dd->flags & FLAGS_ENCRYPT)
261 val |= AES_REG_CTRL_DIRECTION;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800262
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700263 mask |= AES_REG_CTRL_CBC | AES_REG_CTRL_DIRECTION |
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800264 AES_REG_CTRL_KEY_SIZE;
265
Mark A. Greer0d355832013-01-08 11:57:46 -0700266 omap_aes_write_mask(dd, AES_REG_CTRL(dd), val, mask);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800267
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200268 return 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800269}
270
Mark A. Greer0d355832013-01-08 11:57:46 -0700271static void omap_aes_dma_trigger_omap2(struct omap_aes_dev *dd, int length)
272{
273 u32 mask, val;
274
275 val = dd->pdata->dma_start;
276
277 if (dd->dma_lch_out != NULL)
278 val |= dd->pdata->dma_enable_out;
279 if (dd->dma_lch_in != NULL)
280 val |= dd->pdata->dma_enable_in;
281
282 mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
283 dd->pdata->dma_start;
284
285 omap_aes_write_mask(dd, AES_REG_MASK(dd), val, mask);
286
287}
288
289static void omap_aes_dma_trigger_omap4(struct omap_aes_dev *dd, int length)
290{
291 omap_aes_write(dd, AES_REG_LENGTH_N(0), length);
292 omap_aes_write(dd, AES_REG_LENGTH_N(1), 0);
293
294 omap_aes_dma_trigger_omap2(dd, length);
295}
296
297static void omap_aes_dma_stop(struct omap_aes_dev *dd)
298{
299 u32 mask;
300
301 mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
302 dd->pdata->dma_start;
303
304 omap_aes_write_mask(dd, AES_REG_MASK(dd), 0, mask);
305}
306
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800307static struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_ctx *ctx)
308{
309 struct omap_aes_dev *dd = NULL, *tmp;
310
311 spin_lock_bh(&list_lock);
312 if (!ctx->dd) {
313 list_for_each_entry(tmp, &dev_list, list) {
314 /* FIXME: take fist available aes core */
315 dd = tmp;
316 break;
317 }
318 ctx->dd = dd;
319 } else {
320 /* already found before */
321 dd = ctx->dd;
322 }
323 spin_unlock_bh(&list_lock);
324
325 return dd;
326}
327
Mark A. Greerebedbf72013-01-08 11:57:42 -0700328static void omap_aes_dma_out_callback(void *data)
329{
330 struct omap_aes_dev *dd = data;
331
332 /* dma_lch_out - completed */
333 tasklet_schedule(&dd->done_task);
334}
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800335
336static int omap_aes_dma_init(struct omap_aes_dev *dd)
337{
338 int err = -ENOMEM;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700339 dma_cap_mask_t mask;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800340
Mark A. Greerebedbf72013-01-08 11:57:42 -0700341 dd->dma_lch_out = NULL;
342 dd->dma_lch_in = NULL;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800343
Mark A. Greerebedbf72013-01-08 11:57:42 -0700344 dma_cap_zero(mask);
345 dma_cap_set(DMA_SLAVE, mask);
346
Mark A. Greerb4b87a92013-01-08 11:57:45 -0700347 dd->dma_lch_in = dma_request_slave_channel_compat(mask,
348 omap_dma_filter_fn,
349 &dd->dma_in,
350 dd->dev, "rx");
Mark A. Greerebedbf72013-01-08 11:57:42 -0700351 if (!dd->dma_lch_in) {
352 dev_err(dd->dev, "Unable to request in DMA channel\n");
353 goto err_dma_in;
354 }
355
Mark A. Greerb4b87a92013-01-08 11:57:45 -0700356 dd->dma_lch_out = dma_request_slave_channel_compat(mask,
357 omap_dma_filter_fn,
358 &dd->dma_out,
359 dd->dev, "tx");
Mark A. Greerebedbf72013-01-08 11:57:42 -0700360 if (!dd->dma_lch_out) {
361 dev_err(dd->dev, "Unable to request out DMA channel\n");
362 goto err_dma_out;
363 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800364
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800365 return 0;
366
367err_dma_out:
Mark A. Greerebedbf72013-01-08 11:57:42 -0700368 dma_release_channel(dd->dma_lch_in);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800369err_dma_in:
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800370 if (err)
371 pr_err("error: %d\n", err);
372 return err;
373}
374
375static void omap_aes_dma_cleanup(struct omap_aes_dev *dd)
376{
Mark A. Greerebedbf72013-01-08 11:57:42 -0700377 dma_release_channel(dd->dma_lch_out);
378 dma_release_channel(dd->dma_lch_in);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800379}
380
381static void sg_copy_buf(void *buf, struct scatterlist *sg,
382 unsigned int start, unsigned int nbytes, int out)
383{
384 struct scatter_walk walk;
385
386 if (!nbytes)
387 return;
388
389 scatterwalk_start(&walk, sg);
390 scatterwalk_advance(&walk, start);
391 scatterwalk_copychunks(buf, &walk, nbytes, out);
392 scatterwalk_done(&walk, out, 0);
393}
394
Mark A. Greerebedbf72013-01-08 11:57:42 -0700395static int omap_aes_crypt_dma(struct crypto_tfm *tfm,
Joel Fernandes4b645c92013-08-17 21:42:25 -0500396 struct scatterlist *in_sg, struct scatterlist *out_sg,
397 int in_sg_len, int out_sg_len)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800398{
399 struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
400 struct omap_aes_dev *dd = ctx->dd;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700401 struct dma_async_tx_descriptor *tx_in, *tx_out;
402 struct dma_slave_config cfg;
Joel Fernandes4b645c92013-08-17 21:42:25 -0500403 int ret;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800404
Joel Fernandes0a641712013-08-17 21:42:26 -0500405 dma_sync_sg_for_device(dd->dev, dd->in_sg, in_sg_len, DMA_TO_DEVICE);
406
Mark A. Greerebedbf72013-01-08 11:57:42 -0700407 memset(&cfg, 0, sizeof(cfg));
408
Mark A. Greer0d355832013-01-08 11:57:46 -0700409 cfg.src_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
410 cfg.dst_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
Mark A. Greerebedbf72013-01-08 11:57:42 -0700411 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
412 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
413 cfg.src_maxburst = DST_MAXBURST;
414 cfg.dst_maxburst = DST_MAXBURST;
415
416 /* IN */
417 ret = dmaengine_slave_config(dd->dma_lch_in, &cfg);
418 if (ret) {
419 dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
420 ret);
421 return ret;
422 }
423
Joel Fernandes4b645c92013-08-17 21:42:25 -0500424 tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, in_sg_len,
Mark A. Greerebedbf72013-01-08 11:57:42 -0700425 DMA_MEM_TO_DEV,
426 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
427 if (!tx_in) {
428 dev_err(dd->dev, "IN prep_slave_sg() failed\n");
429 return -EINVAL;
430 }
431
432 /* No callback necessary */
433 tx_in->callback_param = dd;
434
435 /* OUT */
436 ret = dmaengine_slave_config(dd->dma_lch_out, &cfg);
437 if (ret) {
438 dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n",
439 ret);
440 return ret;
441 }
442
Joel Fernandes4b645c92013-08-17 21:42:25 -0500443 tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, out_sg_len,
Mark A. Greerebedbf72013-01-08 11:57:42 -0700444 DMA_DEV_TO_MEM,
445 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
446 if (!tx_out) {
447 dev_err(dd->dev, "OUT prep_slave_sg() failed\n");
448 return -EINVAL;
449 }
450
451 tx_out->callback = omap_aes_dma_out_callback;
452 tx_out->callback_param = dd;
453
454 dmaengine_submit(tx_in);
455 dmaengine_submit(tx_out);
456
457 dma_async_issue_pending(dd->dma_lch_in);
458 dma_async_issue_pending(dd->dma_lch_out);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800459
Mark A. Greer0d355832013-01-08 11:57:46 -0700460 /* start DMA */
Joel Fernandes4b645c92013-08-17 21:42:25 -0500461 dd->pdata->trigger(dd, dd->total);
Dmitry Kasatkin83ea7e02010-11-30 10:13:31 +0200462
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800463 return 0;
464}
465
466static int omap_aes_crypt_dma_start(struct omap_aes_dev *dd)
467{
468 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(
469 crypto_ablkcipher_reqtfm(dd->req));
Joel Fernandes4b645c92013-08-17 21:42:25 -0500470 int err;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800471
472 pr_debug("total: %d\n", dd->total);
473
Joel Fernandes4b645c92013-08-17 21:42:25 -0500474 err = dma_map_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
475 if (!err) {
476 dev_err(dd->dev, "dma_map_sg() error\n");
477 return -EINVAL;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800478 }
479
Joel Fernandes4b645c92013-08-17 21:42:25 -0500480 err = dma_map_sg(dd->dev, dd->out_sg, dd->out_sg_len, DMA_FROM_DEVICE);
481 if (!err) {
482 dev_err(dd->dev, "dma_map_sg() error\n");
483 return -EINVAL;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800484 }
485
Joel Fernandes4b645c92013-08-17 21:42:25 -0500486 err = omap_aes_crypt_dma(tfm, dd->in_sg, dd->out_sg, dd->in_sg_len,
487 dd->out_sg_len);
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200488 if (err) {
Joel Fernandes4b645c92013-08-17 21:42:25 -0500489 dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
490 dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
491 DMA_FROM_DEVICE);
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200492 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800493
494 return err;
495}
496
497static void omap_aes_finish_req(struct omap_aes_dev *dd, int err)
498{
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200499 struct ablkcipher_request *req = dd->req;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800500
501 pr_debug("err: %d\n", err);
502
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200503 dd->flags &= ~FLAGS_BUSY;
504
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200505 req->base.complete(&req->base, err);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800506}
507
508static int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd)
509{
510 int err = 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800511
512 pr_debug("total: %d\n", dd->total);
513
Mark A. Greer0d355832013-01-08 11:57:46 -0700514 omap_aes_dma_stop(dd);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800515
Mark A. Greerebedbf72013-01-08 11:57:42 -0700516 dmaengine_terminate_all(dd->dma_lch_in);
517 dmaengine_terminate_all(dd->dma_lch_out);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800518
Joel Fernandes4b645c92013-08-17 21:42:25 -0500519 dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
520 dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len, DMA_FROM_DEVICE);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800521
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800522 return err;
523}
524
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200525static int omap_aes_handle_queue(struct omap_aes_dev *dd,
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200526 struct ablkcipher_request *req)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800527{
528 struct crypto_async_request *async_req, *backlog;
529 struct omap_aes_ctx *ctx;
530 struct omap_aes_reqctx *rctx;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800531 unsigned long flags;
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200532 int err, ret = 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800533
534 spin_lock_irqsave(&dd->lock, flags);
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200535 if (req)
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200536 ret = ablkcipher_enqueue_request(&dd->queue, req);
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200537 if (dd->flags & FLAGS_BUSY) {
538 spin_unlock_irqrestore(&dd->lock, flags);
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200539 return ret;
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200540 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800541 backlog = crypto_get_backlog(&dd->queue);
542 async_req = crypto_dequeue_request(&dd->queue);
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200543 if (async_req)
544 dd->flags |= FLAGS_BUSY;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800545 spin_unlock_irqrestore(&dd->lock, flags);
546
547 if (!async_req)
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200548 return ret;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800549
550 if (backlog)
551 backlog->complete(backlog, -EINPROGRESS);
552
553 req = ablkcipher_request_cast(async_req);
554
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800555 /* assign new request to device */
556 dd->req = req;
557 dd->total = req->nbytes;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800558 dd->in_sg = req->src;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800559 dd->out_sg = req->dst;
560
Joel Fernandese77c7562013-08-17 21:42:24 -0500561 dd->in_sg_len = scatterwalk_bytes_sglen(dd->in_sg, dd->total);
562 dd->out_sg_len = scatterwalk_bytes_sglen(dd->out_sg, dd->total);
563 BUG_ON(dd->in_sg_len < 0 || dd->out_sg_len < 0);
564
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800565 rctx = ablkcipher_request_ctx(req);
566 ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
567 rctx->mode &= FLAGS_MODE_MASK;
568 dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
569
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200570 dd->ctx = ctx;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800571 ctx->dd = dd;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800572
Dmitry Kasatkin83ea7e02010-11-30 10:13:31 +0200573 err = omap_aes_write_ctrl(dd);
574 if (!err)
575 err = omap_aes_crypt_dma_start(dd);
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200576 if (err) {
577 /* aes_task will not finish it, so do it here */
578 omap_aes_finish_req(dd, err);
579 tasklet_schedule(&dd->queue_task);
580 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800581
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200582 return ret; /* return ret, which is enqueue return value */
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800583}
584
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200585static void omap_aes_done_task(unsigned long data)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800586{
587 struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800588
Joel Fernandes4b645c92013-08-17 21:42:25 -0500589 pr_debug("enter done_task\n");
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800590
Joel Fernandes0a641712013-08-17 21:42:26 -0500591 dma_sync_sg_for_cpu(dd->dev, dd->in_sg, dd->in_sg_len, DMA_FROM_DEVICE);
592
Joel Fernandes4b645c92013-08-17 21:42:25 -0500593 omap_aes_crypt_dma_stop(dd);
594 omap_aes_finish_req(dd, 0);
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200595 omap_aes_handle_queue(dd, NULL);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800596
597 pr_debug("exit\n");
598}
599
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200600static void omap_aes_queue_task(unsigned long data)
601{
602 struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
603
604 omap_aes_handle_queue(dd, NULL);
605}
606
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800607static int omap_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
608{
609 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
610 crypto_ablkcipher_reqtfm(req));
611 struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req);
612 struct omap_aes_dev *dd;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800613
614 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->nbytes,
615 !!(mode & FLAGS_ENCRYPT),
616 !!(mode & FLAGS_CBC));
617
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200618 if (!IS_ALIGNED(req->nbytes, AES_BLOCK_SIZE)) {
619 pr_err("request size is not exact amount of AES blocks\n");
620 return -EINVAL;
621 }
622
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800623 dd = omap_aes_find_dev(ctx);
624 if (!dd)
625 return -ENODEV;
626
627 rctx->mode = mode;
628
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200629 return omap_aes_handle_queue(dd, req);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800630}
631
632/* ********************** ALG API ************************************ */
633
634static int omap_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
635 unsigned int keylen)
636{
637 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
638
639 if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
640 keylen != AES_KEYSIZE_256)
641 return -EINVAL;
642
643 pr_debug("enter, keylen: %d\n", keylen);
644
645 memcpy(ctx->key, key, keylen);
646 ctx->keylen = keylen;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800647
648 return 0;
649}
650
651static int omap_aes_ecb_encrypt(struct ablkcipher_request *req)
652{
653 return omap_aes_crypt(req, FLAGS_ENCRYPT);
654}
655
656static int omap_aes_ecb_decrypt(struct ablkcipher_request *req)
657{
658 return omap_aes_crypt(req, 0);
659}
660
661static int omap_aes_cbc_encrypt(struct ablkcipher_request *req)
662{
663 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
664}
665
666static int omap_aes_cbc_decrypt(struct ablkcipher_request *req)
667{
668 return omap_aes_crypt(req, FLAGS_CBC);
669}
670
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700671static int omap_aes_ctr_encrypt(struct ablkcipher_request *req)
672{
673 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CTR);
674}
675
676static int omap_aes_ctr_decrypt(struct ablkcipher_request *req)
677{
678 return omap_aes_crypt(req, FLAGS_CTR);
679}
680
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800681static int omap_aes_cra_init(struct crypto_tfm *tfm)
682{
Joel A Fernandesa3485e62013-05-28 19:02:55 -0500683 struct omap_aes_dev *dd = NULL;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800684
Joel A Fernandesa3485e62013-05-28 19:02:55 -0500685 /* Find AES device, currently picks the first device */
686 spin_lock_bh(&list_lock);
687 list_for_each_entry(dd, &dev_list, list) {
688 break;
689 }
690 spin_unlock_bh(&list_lock);
691
692 pm_runtime_get_sync(dd->dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800693 tfm->crt_ablkcipher.reqsize = sizeof(struct omap_aes_reqctx);
694
695 return 0;
696}
697
698static void omap_aes_cra_exit(struct crypto_tfm *tfm)
699{
Joel A Fernandesa3485e62013-05-28 19:02:55 -0500700 struct omap_aes_dev *dd = NULL;
701
702 /* Find AES device, currently picks the first device */
703 spin_lock_bh(&list_lock);
704 list_for_each_entry(dd, &dev_list, list) {
705 break;
706 }
707 spin_unlock_bh(&list_lock);
708
709 pm_runtime_put_sync(dd->dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800710}
711
712/* ********************** ALGS ************************************ */
713
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700714static struct crypto_alg algs_ecb_cbc[] = {
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800715{
716 .cra_name = "ecb(aes)",
717 .cra_driver_name = "ecb-aes-omap",
718 .cra_priority = 100,
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +0100719 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
720 CRYPTO_ALG_KERN_DRIVER_ONLY |
721 CRYPTO_ALG_ASYNC,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800722 .cra_blocksize = AES_BLOCK_SIZE,
723 .cra_ctxsize = sizeof(struct omap_aes_ctx),
Dmitry Kasatkinefce41b2010-11-30 10:13:32 +0200724 .cra_alignmask = 0,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800725 .cra_type = &crypto_ablkcipher_type,
726 .cra_module = THIS_MODULE,
727 .cra_init = omap_aes_cra_init,
728 .cra_exit = omap_aes_cra_exit,
729 .cra_u.ablkcipher = {
730 .min_keysize = AES_MIN_KEY_SIZE,
731 .max_keysize = AES_MAX_KEY_SIZE,
732 .setkey = omap_aes_setkey,
733 .encrypt = omap_aes_ecb_encrypt,
734 .decrypt = omap_aes_ecb_decrypt,
735 }
736},
737{
738 .cra_name = "cbc(aes)",
739 .cra_driver_name = "cbc-aes-omap",
740 .cra_priority = 100,
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +0100741 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
742 CRYPTO_ALG_KERN_DRIVER_ONLY |
743 CRYPTO_ALG_ASYNC,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800744 .cra_blocksize = AES_BLOCK_SIZE,
745 .cra_ctxsize = sizeof(struct omap_aes_ctx),
Dmitry Kasatkinefce41b2010-11-30 10:13:32 +0200746 .cra_alignmask = 0,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800747 .cra_type = &crypto_ablkcipher_type,
748 .cra_module = THIS_MODULE,
749 .cra_init = omap_aes_cra_init,
750 .cra_exit = omap_aes_cra_exit,
751 .cra_u.ablkcipher = {
752 .min_keysize = AES_MIN_KEY_SIZE,
753 .max_keysize = AES_MAX_KEY_SIZE,
754 .ivsize = AES_BLOCK_SIZE,
755 .setkey = omap_aes_setkey,
756 .encrypt = omap_aes_cbc_encrypt,
757 .decrypt = omap_aes_cbc_decrypt,
758 }
759}
760};
761
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700762static struct crypto_alg algs_ctr[] = {
763{
764 .cra_name = "ctr(aes)",
765 .cra_driver_name = "ctr-aes-omap",
766 .cra_priority = 100,
767 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
768 CRYPTO_ALG_KERN_DRIVER_ONLY |
769 CRYPTO_ALG_ASYNC,
770 .cra_blocksize = AES_BLOCK_SIZE,
771 .cra_ctxsize = sizeof(struct omap_aes_ctx),
772 .cra_alignmask = 0,
773 .cra_type = &crypto_ablkcipher_type,
774 .cra_module = THIS_MODULE,
775 .cra_init = omap_aes_cra_init,
776 .cra_exit = omap_aes_cra_exit,
777 .cra_u.ablkcipher = {
778 .min_keysize = AES_MIN_KEY_SIZE,
779 .max_keysize = AES_MAX_KEY_SIZE,
780 .geniv = "eseqiv",
781 .ivsize = AES_BLOCK_SIZE,
782 .setkey = omap_aes_setkey,
783 .encrypt = omap_aes_ctr_encrypt,
784 .decrypt = omap_aes_ctr_decrypt,
785 }
786} ,
787};
788
789static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc[] = {
790 {
791 .algs_list = algs_ecb_cbc,
792 .size = ARRAY_SIZE(algs_ecb_cbc),
793 },
794};
795
Mark A. Greer0d355832013-01-08 11:57:46 -0700796static const struct omap_aes_pdata omap_aes_pdata_omap2 = {
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700797 .algs_info = omap_aes_algs_info_ecb_cbc,
798 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc),
Mark A. Greer0d355832013-01-08 11:57:46 -0700799 .trigger = omap_aes_dma_trigger_omap2,
800 .key_ofs = 0x1c,
801 .iv_ofs = 0x20,
802 .ctrl_ofs = 0x30,
803 .data_ofs = 0x34,
804 .rev_ofs = 0x44,
805 .mask_ofs = 0x48,
806 .dma_enable_in = BIT(2),
807 .dma_enable_out = BIT(3),
808 .dma_start = BIT(5),
809 .major_mask = 0xf0,
810 .major_shift = 4,
811 .minor_mask = 0x0f,
812 .minor_shift = 0,
813};
814
Mark A. Greerbc69d122013-01-08 11:57:44 -0700815#ifdef CONFIG_OF
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700816static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr[] = {
817 {
818 .algs_list = algs_ecb_cbc,
819 .size = ARRAY_SIZE(algs_ecb_cbc),
820 },
821 {
822 .algs_list = algs_ctr,
823 .size = ARRAY_SIZE(algs_ctr),
824 },
825};
826
827static const struct omap_aes_pdata omap_aes_pdata_omap3 = {
828 .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
829 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
830 .trigger = omap_aes_dma_trigger_omap2,
831 .key_ofs = 0x1c,
832 .iv_ofs = 0x20,
833 .ctrl_ofs = 0x30,
834 .data_ofs = 0x34,
835 .rev_ofs = 0x44,
836 .mask_ofs = 0x48,
837 .dma_enable_in = BIT(2),
838 .dma_enable_out = BIT(3),
839 .dma_start = BIT(5),
840 .major_mask = 0xf0,
841 .major_shift = 4,
842 .minor_mask = 0x0f,
843 .minor_shift = 0,
844};
845
Mark A. Greer0d355832013-01-08 11:57:46 -0700846static const struct omap_aes_pdata omap_aes_pdata_omap4 = {
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700847 .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
848 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
Mark A. Greer0d355832013-01-08 11:57:46 -0700849 .trigger = omap_aes_dma_trigger_omap4,
850 .key_ofs = 0x3c,
851 .iv_ofs = 0x40,
852 .ctrl_ofs = 0x50,
853 .data_ofs = 0x60,
854 .rev_ofs = 0x80,
855 .mask_ofs = 0x84,
Joel Fernandes67216752013-08-17 21:42:28 -0500856 .irq_status_ofs = 0x8c,
857 .irq_enable_ofs = 0x90,
Mark A. Greer0d355832013-01-08 11:57:46 -0700858 .dma_enable_in = BIT(5),
859 .dma_enable_out = BIT(6),
860 .major_mask = 0x0700,
861 .major_shift = 8,
862 .minor_mask = 0x003f,
863 .minor_shift = 0,
864};
865
Mark A. Greerbc69d122013-01-08 11:57:44 -0700866static const struct of_device_id omap_aes_of_match[] = {
867 {
868 .compatible = "ti,omap2-aes",
Mark A. Greer0d355832013-01-08 11:57:46 -0700869 .data = &omap_aes_pdata_omap2,
870 },
871 {
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700872 .compatible = "ti,omap3-aes",
873 .data = &omap_aes_pdata_omap3,
874 },
875 {
Mark A. Greer0d355832013-01-08 11:57:46 -0700876 .compatible = "ti,omap4-aes",
877 .data = &omap_aes_pdata_omap4,
Mark A. Greerbc69d122013-01-08 11:57:44 -0700878 },
879 {},
880};
881MODULE_DEVICE_TABLE(of, omap_aes_of_match);
882
883static int omap_aes_get_res_of(struct omap_aes_dev *dd,
884 struct device *dev, struct resource *res)
885{
886 struct device_node *node = dev->of_node;
887 const struct of_device_id *match;
888 int err = 0;
889
890 match = of_match_device(of_match_ptr(omap_aes_of_match), dev);
891 if (!match) {
892 dev_err(dev, "no compatible OF match\n");
893 err = -EINVAL;
894 goto err;
895 }
896
897 err = of_address_to_resource(node, 0, res);
898 if (err < 0) {
899 dev_err(dev, "can't translate OF node address\n");
900 err = -EINVAL;
901 goto err;
902 }
903
904 dd->dma_out = -1; /* Dummy value that's unused */
905 dd->dma_in = -1; /* Dummy value that's unused */
906
Mark A. Greer0d355832013-01-08 11:57:46 -0700907 dd->pdata = match->data;
908
Mark A. Greerbc69d122013-01-08 11:57:44 -0700909err:
910 return err;
911}
912#else
913static const struct of_device_id omap_aes_of_match[] = {
914 {},
915};
916
917static int omap_aes_get_res_of(struct omap_aes_dev *dd,
918 struct device *dev, struct resource *res)
919{
920 return -EINVAL;
921}
922#endif
923
924static int omap_aes_get_res_pdev(struct omap_aes_dev *dd,
925 struct platform_device *pdev, struct resource *res)
926{
927 struct device *dev = &pdev->dev;
928 struct resource *r;
929 int err = 0;
930
931 /* Get the base address */
932 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
933 if (!r) {
934 dev_err(dev, "no MEM resource info\n");
935 err = -ENODEV;
936 goto err;
937 }
938 memcpy(res, r, sizeof(*res));
939
940 /* Get the DMA out channel */
941 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
942 if (!r) {
943 dev_err(dev, "no DMA out resource info\n");
944 err = -ENODEV;
945 goto err;
946 }
947 dd->dma_out = r->start;
948
949 /* Get the DMA in channel */
950 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
951 if (!r) {
952 dev_err(dev, "no DMA in resource info\n");
953 err = -ENODEV;
954 goto err;
955 }
956 dd->dma_in = r->start;
957
Mark A. Greer0d355832013-01-08 11:57:46 -0700958 /* Only OMAP2/3 can be non-DT */
959 dd->pdata = &omap_aes_pdata_omap2;
960
Mark A. Greerbc69d122013-01-08 11:57:44 -0700961err:
962 return err;
963}
964
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800965static int omap_aes_probe(struct platform_device *pdev)
966{
967 struct device *dev = &pdev->dev;
968 struct omap_aes_dev *dd;
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700969 struct crypto_alg *algp;
Mark A. Greerbc69d122013-01-08 11:57:44 -0700970 struct resource res;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800971 int err = -ENOMEM, i, j;
972 u32 reg;
973
974 dd = kzalloc(sizeof(struct omap_aes_dev), GFP_KERNEL);
975 if (dd == NULL) {
976 dev_err(dev, "unable to alloc data struct.\n");
977 goto err_data;
978 }
979 dd->dev = dev;
980 platform_set_drvdata(pdev, dd);
981
982 spin_lock_init(&dd->lock);
983 crypto_init_queue(&dd->queue, OMAP_AES_QUEUE_LENGTH);
984
Mark A. Greerbc69d122013-01-08 11:57:44 -0700985 err = (dev->of_node) ? omap_aes_get_res_of(dd, dev, &res) :
986 omap_aes_get_res_pdev(dd, pdev, &res);
987 if (err)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800988 goto err_res;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800989
Laurent Navet30862282013-05-02 14:00:38 +0200990 dd->io_base = devm_ioremap_resource(dev, &res);
991 if (IS_ERR(dd->io_base)) {
992 err = PTR_ERR(dd->io_base);
Mark A. Greer5946c4a2013-01-08 11:57:40 -0700993 goto err_res;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800994 }
Mark A. Greerbc69d122013-01-08 11:57:44 -0700995 dd->phys_base = res.start;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800996
Mark A. Greer5946c4a2013-01-08 11:57:40 -0700997 pm_runtime_enable(dev);
998 pm_runtime_get_sync(dev);
999
Mark A. Greer0d355832013-01-08 11:57:46 -07001000 omap_aes_dma_stop(dd);
1001
1002 reg = omap_aes_read(dd, AES_REG_REV(dd));
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001003
1004 pm_runtime_put_sync(dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001005
Mark A. Greer0d355832013-01-08 11:57:46 -07001006 dev_info(dev, "OMAP AES hw accel rev: %u.%u\n",
1007 (reg & dd->pdata->major_mask) >> dd->pdata->major_shift,
1008 (reg & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
1009
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +02001010 tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd);
1011 tasklet_init(&dd->queue_task, omap_aes_queue_task, (unsigned long)dd);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001012
1013 err = omap_aes_dma_init(dd);
1014 if (err)
1015 goto err_dma;
1016
1017 INIT_LIST_HEAD(&dd->list);
1018 spin_lock(&list_lock);
1019 list_add_tail(&dd->list, &dev_list);
1020 spin_unlock(&list_lock);
1021
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001022 for (i = 0; i < dd->pdata->algs_info_size; i++) {
1023 for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
1024 algp = &dd->pdata->algs_info[i].algs_list[j];
1025
1026 pr_debug("reg alg: %s\n", algp->cra_name);
1027 INIT_LIST_HEAD(&algp->cra_list);
1028
1029 err = crypto_register_alg(algp);
1030 if (err)
1031 goto err_algs;
1032
1033 dd->pdata->algs_info[i].registered++;
1034 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001035 }
1036
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001037 return 0;
1038err_algs:
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001039 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1040 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1041 crypto_unregister_alg(
1042 &dd->pdata->algs_info[i].algs_list[j]);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001043 omap_aes_dma_cleanup(dd);
1044err_dma:
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +02001045 tasklet_kill(&dd->done_task);
1046 tasklet_kill(&dd->queue_task);
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001047 pm_runtime_disable(dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001048err_res:
1049 kfree(dd);
1050 dd = NULL;
1051err_data:
1052 dev_err(dev, "initialization failed.\n");
1053 return err;
1054}
1055
1056static int omap_aes_remove(struct platform_device *pdev)
1057{
1058 struct omap_aes_dev *dd = platform_get_drvdata(pdev);
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001059 int i, j;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001060
1061 if (!dd)
1062 return -ENODEV;
1063
1064 spin_lock(&list_lock);
1065 list_del(&dd->list);
1066 spin_unlock(&list_lock);
1067
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001068 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1069 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1070 crypto_unregister_alg(
1071 &dd->pdata->algs_info[i].algs_list[j]);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001072
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +02001073 tasklet_kill(&dd->done_task);
1074 tasklet_kill(&dd->queue_task);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001075 omap_aes_dma_cleanup(dd);
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001076 pm_runtime_disable(dd->dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001077 kfree(dd);
1078 dd = NULL;
1079
1080 return 0;
1081}
1082
Mark A. Greer0635fb32013-01-08 11:57:41 -07001083#ifdef CONFIG_PM_SLEEP
1084static int omap_aes_suspend(struct device *dev)
1085{
1086 pm_runtime_put_sync(dev);
1087 return 0;
1088}
1089
1090static int omap_aes_resume(struct device *dev)
1091{
1092 pm_runtime_get_sync(dev);
1093 return 0;
1094}
1095#endif
1096
1097static const struct dev_pm_ops omap_aes_pm_ops = {
1098 SET_SYSTEM_SLEEP_PM_OPS(omap_aes_suspend, omap_aes_resume)
1099};
1100
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001101static struct platform_driver omap_aes_driver = {
1102 .probe = omap_aes_probe,
1103 .remove = omap_aes_remove,
1104 .driver = {
1105 .name = "omap-aes",
1106 .owner = THIS_MODULE,
Mark A. Greer0635fb32013-01-08 11:57:41 -07001107 .pm = &omap_aes_pm_ops,
Mark A. Greerbc69d122013-01-08 11:57:44 -07001108 .of_match_table = omap_aes_of_match,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001109 },
1110};
1111
Sachin Kamat94e51df2013-03-04 15:09:42 +05301112module_platform_driver(omap_aes_driver);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001113
1114MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
1115MODULE_LICENSE("GPL v2");
1116MODULE_AUTHOR("Dmitry Kasatkin");
1117