blob: 14ec9e209ef80db7da1c55f0fdb29fa811884678 [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>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
12 *
13 */
14
Mark A. Greerebedbf72013-01-08 11:57:42 -070015#define OMAP_AES_DMA_PRIVATE
16
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080017#define pr_fmt(fmt) "%s: " fmt, __func__
18
19#include <linux/err.h>
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/errno.h>
23#include <linux/kernel.h>
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080024#include <linux/platform_device.h>
25#include <linux/scatterlist.h>
26#include <linux/dma-mapping.h>
Mark A. Greerebedbf72013-01-08 11:57:42 -070027#include <linux/dmaengine.h>
28#include <linux/omap-dma.h>
Mark A. Greer5946c4a2013-01-08 11:57:40 -070029#include <linux/pm_runtime.h>
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080030#include <linux/io.h>
31#include <linux/crypto.h>
32#include <linux/interrupt.h>
33#include <crypto/scatterwalk.h>
34#include <crypto/aes.h>
35
Mark A. Greerebedbf72013-01-08 11:57:42 -070036#define DST_MAXBURST 4
37#define DMA_MIN (DST_MAXBURST * sizeof(u32))
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080038
39/* OMAP TRM gives bitfields as start:end, where start is the higher bit
40 number. For example 7:0 */
41#define FLD_MASK(start, end) (((1 << ((start) - (end) + 1)) - 1) << (end))
42#define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
43
44#define AES_REG_KEY(x) (0x1C - ((x ^ 0x01) * 0x04))
45#define AES_REG_IV(x) (0x20 + ((x) * 0x04))
46
47#define AES_REG_CTRL 0x30
48#define AES_REG_CTRL_CTR_WIDTH (1 << 7)
49#define AES_REG_CTRL_CTR (1 << 6)
50#define AES_REG_CTRL_CBC (1 << 5)
51#define AES_REG_CTRL_KEY_SIZE (3 << 3)
52#define AES_REG_CTRL_DIRECTION (1 << 2)
53#define AES_REG_CTRL_INPUT_READY (1 << 1)
54#define AES_REG_CTRL_OUTPUT_READY (1 << 0)
55
56#define AES_REG_DATA 0x34
57#define AES_REG_DATA_N(x) (0x34 + ((x) * 0x04))
58
59#define AES_REG_REV 0x44
60#define AES_REG_REV_MAJOR 0xF0
61#define AES_REG_REV_MINOR 0x0F
62
63#define AES_REG_MASK 0x48
64#define AES_REG_MASK_SIDLE (1 << 6)
65#define AES_REG_MASK_START (1 << 5)
66#define AES_REG_MASK_DMA_OUT_EN (1 << 3)
67#define AES_REG_MASK_DMA_IN_EN (1 << 2)
68#define AES_REG_MASK_SOFTRESET (1 << 1)
69#define AES_REG_AUTOIDLE (1 << 0)
70
71#define AES_REG_SYSSTATUS 0x4C
72#define AES_REG_SYSSTATUS_RESETDONE (1 << 0)
73
74#define DEFAULT_TIMEOUT (5*HZ)
75
76#define FLAGS_MODE_MASK 0x000f
77#define FLAGS_ENCRYPT BIT(0)
78#define FLAGS_CBC BIT(1)
79#define FLAGS_GIV BIT(2)
80
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +020081#define FLAGS_INIT BIT(4)
82#define FLAGS_FAST BIT(5)
83#define FLAGS_BUSY BIT(6)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080084
85struct omap_aes_ctx {
86 struct omap_aes_dev *dd;
87
88 int keylen;
89 u32 key[AES_KEYSIZE_256 / sizeof(u32)];
90 unsigned long flags;
91};
92
93struct omap_aes_reqctx {
94 unsigned long mode;
95};
96
97#define OMAP_AES_QUEUE_LENGTH 1
98#define OMAP_AES_CACHE_SIZE 0
99
100struct omap_aes_dev {
101 struct list_head list;
102 unsigned long phys_base;
Dmitry Kasatkinefce41b2010-11-30 10:13:32 +0200103 void __iomem *io_base;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800104 struct omap_aes_ctx *ctx;
105 struct device *dev;
106 unsigned long flags;
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200107 int err;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800108
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200109 spinlock_t lock;
110 struct crypto_queue queue;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800111
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200112 struct tasklet_struct done_task;
113 struct tasklet_struct queue_task;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800114
115 struct ablkcipher_request *req;
116 size_t total;
117 struct scatterlist *in_sg;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700118#ifndef OMAP_AES_DMA_PRIVATE
119 struct scatterlist in_sgl;
120#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800121 size_t in_offset;
122 struct scatterlist *out_sg;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700123#ifndef OMAP_AES_DMA_PRIVATE
124 struct scatterlist out_sgl;
125#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800126 size_t out_offset;
127
128 size_t buflen;
129 void *buf_in;
130 size_t dma_size;
131 int dma_in;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700132#ifdef OMAP_AES_DMA_PRIVATE
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800133 int dma_lch_in;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700134#else
135 struct dma_chan *dma_lch_in;
136#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800137 dma_addr_t dma_addr_in;
138 void *buf_out;
139 int dma_out;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700140#ifdef OMAP_AES_DMA_PRIVATE
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800141 int dma_lch_out;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700142#else
143 struct dma_chan *dma_lch_out;
144#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800145 dma_addr_t dma_addr_out;
146};
147
148/* keep registered devices data here */
149static LIST_HEAD(dev_list);
150static DEFINE_SPINLOCK(list_lock);
151
152static inline u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset)
153{
154 return __raw_readl(dd->io_base + offset);
155}
156
157static inline void omap_aes_write(struct omap_aes_dev *dd, u32 offset,
158 u32 value)
159{
160 __raw_writel(value, dd->io_base + offset);
161}
162
163static inline void omap_aes_write_mask(struct omap_aes_dev *dd, u32 offset,
164 u32 value, u32 mask)
165{
166 u32 val;
167
168 val = omap_aes_read(dd, offset);
169 val &= ~mask;
170 val |= value;
171 omap_aes_write(dd, offset, val);
172}
173
174static void omap_aes_write_n(struct omap_aes_dev *dd, u32 offset,
175 u32 *value, int count)
176{
177 for (; count--; value++, offset += 4)
178 omap_aes_write(dd, offset, *value);
179}
180
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800181static int omap_aes_hw_init(struct omap_aes_dev *dd)
182{
Dmitry Kasatkin83ea7e02010-11-30 10:13:31 +0200183 /*
184 * clocks are enabled when request starts and disabled when finished.
185 * It may be long delays between requests.
186 * Device might go to off mode to save power.
187 */
Mark A. Greer5946c4a2013-01-08 11:57:40 -0700188 pm_runtime_get_sync(dd->dev);
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200189
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800190 if (!(dd->flags & FLAGS_INIT)) {
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200191 dd->flags |= FLAGS_INIT;
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200192 dd->err = 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800193 }
194
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200195 return 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800196}
197
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200198static int omap_aes_write_ctrl(struct omap_aes_dev *dd)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800199{
200 unsigned int key32;
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200201 int i, err;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800202 u32 val, mask;
203
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200204 err = omap_aes_hw_init(dd);
205 if (err)
206 return err;
207
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800208 val = 0;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700209#ifdef OMAP_AES_DMA_PRIVATE
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800210 if (dd->dma_lch_out >= 0)
211 val |= AES_REG_MASK_DMA_OUT_EN;
212 if (dd->dma_lch_in >= 0)
213 val |= AES_REG_MASK_DMA_IN_EN;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700214#else
215 if (dd->dma_lch_out != NULL)
216 val |= AES_REG_MASK_DMA_OUT_EN;
217 if (dd->dma_lch_in != NULL)
218 val |= AES_REG_MASK_DMA_IN_EN;
219#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800220
221 mask = AES_REG_MASK_DMA_IN_EN | AES_REG_MASK_DMA_OUT_EN;
222
223 omap_aes_write_mask(dd, AES_REG_MASK, val, mask);
224
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800225 key32 = dd->ctx->keylen / sizeof(u32);
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200226
227 /* it seems a key should always be set even if it has not changed */
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800228 for (i = 0; i < key32; i++) {
229 omap_aes_write(dd, AES_REG_KEY(i),
230 __le32_to_cpu(dd->ctx->key[i]));
231 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800232
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200233 if ((dd->flags & FLAGS_CBC) && dd->req->info)
234 omap_aes_write_n(dd, AES_REG_IV(0), dd->req->info, 4);
235
236 val = FLD_VAL(((dd->ctx->keylen >> 3) - 1), 4, 3);
237 if (dd->flags & FLAGS_CBC)
238 val |= AES_REG_CTRL_CBC;
239 if (dd->flags & FLAGS_ENCRYPT)
240 val |= AES_REG_CTRL_DIRECTION;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800241
242 mask = AES_REG_CTRL_CBC | AES_REG_CTRL_DIRECTION |
243 AES_REG_CTRL_KEY_SIZE;
244
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200245 omap_aes_write_mask(dd, AES_REG_CTRL, val, mask);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800246
Mark A. Greerebedbf72013-01-08 11:57:42 -0700247#ifdef OMAP_AES_DMA_PRIVATE
Dmitry Kasatkin83ea7e02010-11-30 10:13:31 +0200248 /* IN */
249 omap_set_dma_dest_params(dd->dma_lch_in, 0, OMAP_DMA_AMODE_CONSTANT,
250 dd->phys_base + AES_REG_DATA, 0, 4);
251
252 omap_set_dma_dest_burst_mode(dd->dma_lch_in, OMAP_DMA_DATA_BURST_4);
253 omap_set_dma_src_burst_mode(dd->dma_lch_in, OMAP_DMA_DATA_BURST_4);
254
255 /* OUT */
256 omap_set_dma_src_params(dd->dma_lch_out, 0, OMAP_DMA_AMODE_CONSTANT,
257 dd->phys_base + AES_REG_DATA, 0, 4);
258
259 omap_set_dma_src_burst_mode(dd->dma_lch_out, OMAP_DMA_DATA_BURST_4);
260 omap_set_dma_dest_burst_mode(dd->dma_lch_out, OMAP_DMA_DATA_BURST_4);
Mark A. Greerebedbf72013-01-08 11:57:42 -0700261#endif
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200262
263 return 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800264}
265
266static struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_ctx *ctx)
267{
268 struct omap_aes_dev *dd = NULL, *tmp;
269
270 spin_lock_bh(&list_lock);
271 if (!ctx->dd) {
272 list_for_each_entry(tmp, &dev_list, list) {
273 /* FIXME: take fist available aes core */
274 dd = tmp;
275 break;
276 }
277 ctx->dd = dd;
278 } else {
279 /* already found before */
280 dd = ctx->dd;
281 }
282 spin_unlock_bh(&list_lock);
283
284 return dd;
285}
286
Mark A. Greerebedbf72013-01-08 11:57:42 -0700287#ifdef OMAP_AES_DMA_PRIVATE
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800288static void omap_aes_dma_callback(int lch, u16 ch_status, void *data)
289{
290 struct omap_aes_dev *dd = data;
291
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200292 if (ch_status != OMAP_DMA_BLOCK_IRQ) {
293 pr_err("omap-aes DMA error status: 0x%hx\n", ch_status);
294 dd->err = -EIO;
295 dd->flags &= ~FLAGS_INIT; /* request to re-initialize */
296 } else if (lch == dd->dma_lch_in) {
297 return;
298 }
299
300 /* dma_lch_out - completed */
301 tasklet_schedule(&dd->done_task);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800302}
Mark A. Greerebedbf72013-01-08 11:57:42 -0700303#else
304static void omap_aes_dma_out_callback(void *data)
305{
306 struct omap_aes_dev *dd = data;
307
308 /* dma_lch_out - completed */
309 tasklet_schedule(&dd->done_task);
310}
311#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800312
313static int omap_aes_dma_init(struct omap_aes_dev *dd)
314{
315 int err = -ENOMEM;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700316#ifndef OMAP_AES_DMA_PRIVATE
317 dma_cap_mask_t mask;
318#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800319
Mark A. Greerebedbf72013-01-08 11:57:42 -0700320#ifdef OMAP_AES_DMA_PRIVATE
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800321 dd->dma_lch_out = -1;
322 dd->dma_lch_in = -1;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700323#else
324 dd->dma_lch_out = NULL;
325 dd->dma_lch_in = NULL;
326#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800327
328 dd->buf_in = (void *)__get_free_pages(GFP_KERNEL, OMAP_AES_CACHE_SIZE);
329 dd->buf_out = (void *)__get_free_pages(GFP_KERNEL, OMAP_AES_CACHE_SIZE);
330 dd->buflen = PAGE_SIZE << OMAP_AES_CACHE_SIZE;
331 dd->buflen &= ~(AES_BLOCK_SIZE - 1);
332
333 if (!dd->buf_in || !dd->buf_out) {
334 dev_err(dd->dev, "unable to alloc pages.\n");
335 goto err_alloc;
336 }
337
338 /* MAP here */
339 dd->dma_addr_in = dma_map_single(dd->dev, dd->buf_in, dd->buflen,
340 DMA_TO_DEVICE);
341 if (dma_mapping_error(dd->dev, dd->dma_addr_in)) {
342 dev_err(dd->dev, "dma %d bytes error\n", dd->buflen);
343 err = -EINVAL;
344 goto err_map_in;
345 }
346
347 dd->dma_addr_out = dma_map_single(dd->dev, dd->buf_out, dd->buflen,
348 DMA_FROM_DEVICE);
349 if (dma_mapping_error(dd->dev, dd->dma_addr_out)) {
350 dev_err(dd->dev, "dma %d bytes error\n", dd->buflen);
351 err = -EINVAL;
352 goto err_map_out;
353 }
354
Mark A. Greerebedbf72013-01-08 11:57:42 -0700355#ifdef OMAP_AES_DMA_PRIVATE
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800356 err = omap_request_dma(dd->dma_in, "omap-aes-rx",
357 omap_aes_dma_callback, dd, &dd->dma_lch_in);
358 if (err) {
359 dev_err(dd->dev, "Unable to request DMA channel\n");
360 goto err_dma_in;
361 }
362 err = omap_request_dma(dd->dma_out, "omap-aes-tx",
363 omap_aes_dma_callback, dd, &dd->dma_lch_out);
364 if (err) {
365 dev_err(dd->dev, "Unable to request DMA channel\n");
366 goto err_dma_out;
367 }
Mark A. Greerebedbf72013-01-08 11:57:42 -0700368#else
369 dma_cap_zero(mask);
370 dma_cap_set(DMA_SLAVE, mask);
371
372 dd->dma_lch_in = dma_request_channel(mask, omap_dma_filter_fn,
373 &dd->dma_in);
374 if (!dd->dma_lch_in) {
375 dev_err(dd->dev, "Unable to request in DMA channel\n");
376 goto err_dma_in;
377 }
378
379 dd->dma_lch_out = dma_request_channel(mask, omap_dma_filter_fn,
380 &dd->dma_out);
381 if (!dd->dma_lch_out) {
382 dev_err(dd->dev, "Unable to request out DMA channel\n");
383 goto err_dma_out;
384 }
385#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800386
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800387 return 0;
388
389err_dma_out:
Mark A. Greerebedbf72013-01-08 11:57:42 -0700390#ifdef OMAP_AES_DMA_PRIVATE
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800391 omap_free_dma(dd->dma_lch_in);
Mark A. Greerebedbf72013-01-08 11:57:42 -0700392#else
393 dma_release_channel(dd->dma_lch_in);
394#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800395err_dma_in:
396 dma_unmap_single(dd->dev, dd->dma_addr_out, dd->buflen,
397 DMA_FROM_DEVICE);
398err_map_out:
399 dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen, DMA_TO_DEVICE);
400err_map_in:
401 free_pages((unsigned long)dd->buf_out, OMAP_AES_CACHE_SIZE);
402 free_pages((unsigned long)dd->buf_in, OMAP_AES_CACHE_SIZE);
403err_alloc:
404 if (err)
405 pr_err("error: %d\n", err);
406 return err;
407}
408
409static void omap_aes_dma_cleanup(struct omap_aes_dev *dd)
410{
Mark A. Greerebedbf72013-01-08 11:57:42 -0700411#ifdef OMAP_AES_DMA_PRIVATE
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800412 omap_free_dma(dd->dma_lch_out);
413 omap_free_dma(dd->dma_lch_in);
Mark A. Greerebedbf72013-01-08 11:57:42 -0700414#else
415 dma_release_channel(dd->dma_lch_out);
416 dma_release_channel(dd->dma_lch_in);
417#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800418 dma_unmap_single(dd->dev, dd->dma_addr_out, dd->buflen,
419 DMA_FROM_DEVICE);
420 dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen, DMA_TO_DEVICE);
421 free_pages((unsigned long)dd->buf_out, OMAP_AES_CACHE_SIZE);
422 free_pages((unsigned long)dd->buf_in, OMAP_AES_CACHE_SIZE);
423}
424
425static void sg_copy_buf(void *buf, struct scatterlist *sg,
426 unsigned int start, unsigned int nbytes, int out)
427{
428 struct scatter_walk walk;
429
430 if (!nbytes)
431 return;
432
433 scatterwalk_start(&walk, sg);
434 scatterwalk_advance(&walk, start);
435 scatterwalk_copychunks(buf, &walk, nbytes, out);
436 scatterwalk_done(&walk, out, 0);
437}
438
439static int sg_copy(struct scatterlist **sg, size_t *offset, void *buf,
440 size_t buflen, size_t total, int out)
441{
442 unsigned int count, off = 0;
443
444 while (buflen && total) {
445 count = min((*sg)->length - *offset, total);
446 count = min(count, buflen);
447
448 if (!count)
449 return off;
450
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200451 /*
452 * buflen and total are AES_BLOCK_SIZE size aligned,
453 * so count should be also aligned
454 */
455
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800456 sg_copy_buf(buf + off, *sg, *offset, count, out);
457
458 off += count;
459 buflen -= count;
460 *offset += count;
461 total -= count;
462
463 if (*offset == (*sg)->length) {
464 *sg = sg_next(*sg);
465 if (*sg)
466 *offset = 0;
467 else
468 total = 0;
469 }
470 }
471
472 return off;
473}
474
Mark A. Greerebedbf72013-01-08 11:57:42 -0700475#ifdef OMAP_AES_DMA_PRIVATE
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800476static int omap_aes_crypt_dma(struct crypto_tfm *tfm, dma_addr_t dma_addr_in,
477 dma_addr_t dma_addr_out, int length)
Mark A. Greerebedbf72013-01-08 11:57:42 -0700478#else
479static int omap_aes_crypt_dma(struct crypto_tfm *tfm,
480 struct scatterlist *in_sg, struct scatterlist *out_sg)
481#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800482{
483 struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
484 struct omap_aes_dev *dd = ctx->dd;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700485#ifdef OMAP_AES_DMA_PRIVATE
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800486 int len32;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700487#else
488 struct dma_async_tx_descriptor *tx_in, *tx_out;
489 struct dma_slave_config cfg;
490 dma_addr_t dma_addr_in = sg_dma_address(in_sg);
491 int ret, length = sg_dma_len(in_sg);
492#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800493
494 pr_debug("len: %d\n", length);
495
496 dd->dma_size = length;
497
498 if (!(dd->flags & FLAGS_FAST))
499 dma_sync_single_for_device(dd->dev, dma_addr_in, length,
500 DMA_TO_DEVICE);
501
Mark A. Greerebedbf72013-01-08 11:57:42 -0700502#ifdef OMAP_AES_DMA_PRIVATE
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800503 len32 = DIV_ROUND_UP(length, sizeof(u32));
504
505 /* IN */
506 omap_set_dma_transfer_params(dd->dma_lch_in, OMAP_DMA_DATA_TYPE_S32,
507 len32, 1, OMAP_DMA_SYNC_PACKET, dd->dma_in,
508 OMAP_DMA_DST_SYNC);
509
510 omap_set_dma_src_params(dd->dma_lch_in, 0, OMAP_DMA_AMODE_POST_INC,
511 dma_addr_in, 0, 0);
512
513 /* OUT */
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800514 omap_set_dma_transfer_params(dd->dma_lch_out, OMAP_DMA_DATA_TYPE_S32,
515 len32, 1, OMAP_DMA_SYNC_PACKET,
516 dd->dma_out, OMAP_DMA_SRC_SYNC);
517
518 omap_set_dma_dest_params(dd->dma_lch_out, 0, OMAP_DMA_AMODE_POST_INC,
519 dma_addr_out, 0, 0);
520
521 omap_start_dma(dd->dma_lch_in);
522 omap_start_dma(dd->dma_lch_out);
Mark A. Greerebedbf72013-01-08 11:57:42 -0700523#else
524 memset(&cfg, 0, sizeof(cfg));
525
526 cfg.src_addr = dd->phys_base + AES_REG_DATA;
527 cfg.dst_addr = dd->phys_base + AES_REG_DATA;
528 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
529 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
530 cfg.src_maxburst = DST_MAXBURST;
531 cfg.dst_maxburst = DST_MAXBURST;
532
533 /* IN */
534 ret = dmaengine_slave_config(dd->dma_lch_in, &cfg);
535 if (ret) {
536 dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
537 ret);
538 return ret;
539 }
540
541 tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, 1,
542 DMA_MEM_TO_DEV,
543 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
544 if (!tx_in) {
545 dev_err(dd->dev, "IN prep_slave_sg() failed\n");
546 return -EINVAL;
547 }
548
549 /* No callback necessary */
550 tx_in->callback_param = dd;
551
552 /* OUT */
553 ret = dmaengine_slave_config(dd->dma_lch_out, &cfg);
554 if (ret) {
555 dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n",
556 ret);
557 return ret;
558 }
559
560 tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, 1,
561 DMA_DEV_TO_MEM,
562 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
563 if (!tx_out) {
564 dev_err(dd->dev, "OUT prep_slave_sg() failed\n");
565 return -EINVAL;
566 }
567
568 tx_out->callback = omap_aes_dma_out_callback;
569 tx_out->callback_param = dd;
570
571 dmaengine_submit(tx_in);
572 dmaengine_submit(tx_out);
573
574 dma_async_issue_pending(dd->dma_lch_in);
575 dma_async_issue_pending(dd->dma_lch_out);
576#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800577
Dmitry Kasatkin83ea7e02010-11-30 10:13:31 +0200578 /* start DMA or disable idle mode */
579 omap_aes_write_mask(dd, AES_REG_MASK, AES_REG_MASK_START,
580 AES_REG_MASK_START);
581
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800582 return 0;
583}
584
585static int omap_aes_crypt_dma_start(struct omap_aes_dev *dd)
586{
587 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(
588 crypto_ablkcipher_reqtfm(dd->req));
589 int err, fast = 0, in, out;
590 size_t count;
591 dma_addr_t addr_in, addr_out;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700592#ifndef OMAP_AES_DMA_PRIVATE
593 struct scatterlist *in_sg, *out_sg;
594 int len32;
595#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800596
597 pr_debug("total: %d\n", dd->total);
598
599 if (sg_is_last(dd->in_sg) && sg_is_last(dd->out_sg)) {
600 /* check for alignment */
601 in = IS_ALIGNED((u32)dd->in_sg->offset, sizeof(u32));
602 out = IS_ALIGNED((u32)dd->out_sg->offset, sizeof(u32));
603
604 fast = in && out;
605 }
606
607 if (fast) {
608 count = min(dd->total, sg_dma_len(dd->in_sg));
609 count = min(count, sg_dma_len(dd->out_sg));
610
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200611 if (count != dd->total) {
612 pr_err("request length != buffer length\n");
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800613 return -EINVAL;
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200614 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800615
616 pr_debug("fast\n");
617
618 err = dma_map_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
619 if (!err) {
620 dev_err(dd->dev, "dma_map_sg() error\n");
621 return -EINVAL;
622 }
623
624 err = dma_map_sg(dd->dev, dd->out_sg, 1, DMA_FROM_DEVICE);
625 if (!err) {
626 dev_err(dd->dev, "dma_map_sg() error\n");
627 dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
628 return -EINVAL;
629 }
630
631 addr_in = sg_dma_address(dd->in_sg);
632 addr_out = sg_dma_address(dd->out_sg);
633
Mark A. Greerebedbf72013-01-08 11:57:42 -0700634#ifndef OMAP_AES_DMA_PRIVATE
635 in_sg = dd->in_sg;
636 out_sg = dd->out_sg;
637#endif
638
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800639 dd->flags |= FLAGS_FAST;
640
641 } else {
642 /* use cache buffers */
643 count = sg_copy(&dd->in_sg, &dd->in_offset, dd->buf_in,
644 dd->buflen, dd->total, 0);
645
Mark A. Greerebedbf72013-01-08 11:57:42 -0700646#ifndef OMAP_AES_DMA_PRIVATE
647 len32 = DIV_ROUND_UP(count, DMA_MIN) * DMA_MIN;
648
649 /*
650 * The data going into the AES module has been copied
651 * to a local buffer and the data coming out will go
652 * into a local buffer so set up local SG entries for
653 * both.
654 */
655 sg_init_table(&dd->in_sgl, 1);
656 dd->in_sgl.offset = dd->in_offset;
657 sg_dma_len(&dd->in_sgl) = len32;
658 sg_dma_address(&dd->in_sgl) = dd->dma_addr_in;
659
660 sg_init_table(&dd->out_sgl, 1);
661 dd->out_sgl.offset = dd->out_offset;
662 sg_dma_len(&dd->out_sgl) = len32;
663 sg_dma_address(&dd->out_sgl) = dd->dma_addr_out;
664
665 in_sg = &dd->in_sgl;
666 out_sg = &dd->out_sgl;
667#endif
668
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800669 addr_in = dd->dma_addr_in;
670 addr_out = dd->dma_addr_out;
671
672 dd->flags &= ~FLAGS_FAST;
673
674 }
675
676 dd->total -= count;
677
Mark A. Greerebedbf72013-01-08 11:57:42 -0700678#ifdef OMAP_AES_DMA_PRIVATE
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800679 err = omap_aes_crypt_dma(tfm, addr_in, addr_out, count);
Mark A. Greerebedbf72013-01-08 11:57:42 -0700680#else
681 err = omap_aes_crypt_dma(tfm, in_sg, out_sg);
682#endif
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200683 if (err) {
684 dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
685 dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_TO_DEVICE);
686 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800687
688 return err;
689}
690
691static void omap_aes_finish_req(struct omap_aes_dev *dd, int err)
692{
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200693 struct ablkcipher_request *req = dd->req;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800694
695 pr_debug("err: %d\n", err);
696
Mark A. Greer5946c4a2013-01-08 11:57:40 -0700697 pm_runtime_put_sync(dd->dev);
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200698 dd->flags &= ~FLAGS_BUSY;
699
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200700 req->base.complete(&req->base, err);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800701}
702
703static int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd)
704{
705 int err = 0;
706 size_t count;
707
708 pr_debug("total: %d\n", dd->total);
709
710 omap_aes_write_mask(dd, AES_REG_MASK, 0, AES_REG_MASK_START);
711
Mark A. Greerebedbf72013-01-08 11:57:42 -0700712#ifdef OMAP_AES_DMA_PRIVATE
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800713 omap_stop_dma(dd->dma_lch_in);
714 omap_stop_dma(dd->dma_lch_out);
Mark A. Greerebedbf72013-01-08 11:57:42 -0700715#else
716 dmaengine_terminate_all(dd->dma_lch_in);
717 dmaengine_terminate_all(dd->dma_lch_out);
718#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800719
720 if (dd->flags & FLAGS_FAST) {
721 dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_FROM_DEVICE);
722 dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
723 } else {
724 dma_sync_single_for_device(dd->dev, dd->dma_addr_out,
725 dd->dma_size, DMA_FROM_DEVICE);
726
727 /* copy data */
728 count = sg_copy(&dd->out_sg, &dd->out_offset, dd->buf_out,
729 dd->buflen, dd->dma_size, 1);
730 if (count != dd->dma_size) {
731 err = -EINVAL;
732 pr_err("not all data converted: %u\n", count);
733 }
734 }
735
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800736 return err;
737}
738
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200739static int omap_aes_handle_queue(struct omap_aes_dev *dd,
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200740 struct ablkcipher_request *req)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800741{
742 struct crypto_async_request *async_req, *backlog;
743 struct omap_aes_ctx *ctx;
744 struct omap_aes_reqctx *rctx;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800745 unsigned long flags;
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200746 int err, ret = 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800747
748 spin_lock_irqsave(&dd->lock, flags);
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200749 if (req)
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200750 ret = ablkcipher_enqueue_request(&dd->queue, req);
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200751 if (dd->flags & FLAGS_BUSY) {
752 spin_unlock_irqrestore(&dd->lock, flags);
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200753 return ret;
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200754 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800755 backlog = crypto_get_backlog(&dd->queue);
756 async_req = crypto_dequeue_request(&dd->queue);
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200757 if (async_req)
758 dd->flags |= FLAGS_BUSY;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800759 spin_unlock_irqrestore(&dd->lock, flags);
760
761 if (!async_req)
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200762 return ret;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800763
764 if (backlog)
765 backlog->complete(backlog, -EINPROGRESS);
766
767 req = ablkcipher_request_cast(async_req);
768
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800769 /* assign new request to device */
770 dd->req = req;
771 dd->total = req->nbytes;
772 dd->in_offset = 0;
773 dd->in_sg = req->src;
774 dd->out_offset = 0;
775 dd->out_sg = req->dst;
776
777 rctx = ablkcipher_request_ctx(req);
778 ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
779 rctx->mode &= FLAGS_MODE_MASK;
780 dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
781
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200782 dd->ctx = ctx;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800783 ctx->dd = dd;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800784
Dmitry Kasatkin83ea7e02010-11-30 10:13:31 +0200785 err = omap_aes_write_ctrl(dd);
786 if (!err)
787 err = omap_aes_crypt_dma_start(dd);
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200788 if (err) {
789 /* aes_task will not finish it, so do it here */
790 omap_aes_finish_req(dd, err);
791 tasklet_schedule(&dd->queue_task);
792 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800793
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200794 return ret; /* return ret, which is enqueue return value */
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800795}
796
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200797static void omap_aes_done_task(unsigned long data)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800798{
799 struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200800 int err;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800801
802 pr_debug("enter\n");
803
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200804 err = omap_aes_crypt_dma_stop(dd);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800805
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200806 err = dd->err ? : err;
807
808 if (dd->total && !err) {
809 err = omap_aes_crypt_dma_start(dd);
810 if (!err)
811 return; /* DMA started. Not fininishing. */
812 }
813
814 omap_aes_finish_req(dd, err);
815 omap_aes_handle_queue(dd, NULL);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800816
817 pr_debug("exit\n");
818}
819
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200820static void omap_aes_queue_task(unsigned long data)
821{
822 struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
823
824 omap_aes_handle_queue(dd, NULL);
825}
826
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800827static int omap_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
828{
829 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
830 crypto_ablkcipher_reqtfm(req));
831 struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req);
832 struct omap_aes_dev *dd;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800833
834 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->nbytes,
835 !!(mode & FLAGS_ENCRYPT),
836 !!(mode & FLAGS_CBC));
837
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200838 if (!IS_ALIGNED(req->nbytes, AES_BLOCK_SIZE)) {
839 pr_err("request size is not exact amount of AES blocks\n");
840 return -EINVAL;
841 }
842
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800843 dd = omap_aes_find_dev(ctx);
844 if (!dd)
845 return -ENODEV;
846
847 rctx->mode = mode;
848
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200849 return omap_aes_handle_queue(dd, req);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800850}
851
852/* ********************** ALG API ************************************ */
853
854static int omap_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
855 unsigned int keylen)
856{
857 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
858
859 if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
860 keylen != AES_KEYSIZE_256)
861 return -EINVAL;
862
863 pr_debug("enter, keylen: %d\n", keylen);
864
865 memcpy(ctx->key, key, keylen);
866 ctx->keylen = keylen;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800867
868 return 0;
869}
870
871static int omap_aes_ecb_encrypt(struct ablkcipher_request *req)
872{
873 return omap_aes_crypt(req, FLAGS_ENCRYPT);
874}
875
876static int omap_aes_ecb_decrypt(struct ablkcipher_request *req)
877{
878 return omap_aes_crypt(req, 0);
879}
880
881static int omap_aes_cbc_encrypt(struct ablkcipher_request *req)
882{
883 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
884}
885
886static int omap_aes_cbc_decrypt(struct ablkcipher_request *req)
887{
888 return omap_aes_crypt(req, FLAGS_CBC);
889}
890
891static int omap_aes_cra_init(struct crypto_tfm *tfm)
892{
893 pr_debug("enter\n");
894
895 tfm->crt_ablkcipher.reqsize = sizeof(struct omap_aes_reqctx);
896
897 return 0;
898}
899
900static void omap_aes_cra_exit(struct crypto_tfm *tfm)
901{
902 pr_debug("enter\n");
903}
904
905/* ********************** ALGS ************************************ */
906
907static struct crypto_alg algs[] = {
908{
909 .cra_name = "ecb(aes)",
910 .cra_driver_name = "ecb-aes-omap",
911 .cra_priority = 100,
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +0100912 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
913 CRYPTO_ALG_KERN_DRIVER_ONLY |
914 CRYPTO_ALG_ASYNC,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800915 .cra_blocksize = AES_BLOCK_SIZE,
916 .cra_ctxsize = sizeof(struct omap_aes_ctx),
Dmitry Kasatkinefce41b2010-11-30 10:13:32 +0200917 .cra_alignmask = 0,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800918 .cra_type = &crypto_ablkcipher_type,
919 .cra_module = THIS_MODULE,
920 .cra_init = omap_aes_cra_init,
921 .cra_exit = omap_aes_cra_exit,
922 .cra_u.ablkcipher = {
923 .min_keysize = AES_MIN_KEY_SIZE,
924 .max_keysize = AES_MAX_KEY_SIZE,
925 .setkey = omap_aes_setkey,
926 .encrypt = omap_aes_ecb_encrypt,
927 .decrypt = omap_aes_ecb_decrypt,
928 }
929},
930{
931 .cra_name = "cbc(aes)",
932 .cra_driver_name = "cbc-aes-omap",
933 .cra_priority = 100,
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +0100934 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
935 CRYPTO_ALG_KERN_DRIVER_ONLY |
936 CRYPTO_ALG_ASYNC,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800937 .cra_blocksize = AES_BLOCK_SIZE,
938 .cra_ctxsize = sizeof(struct omap_aes_ctx),
Dmitry Kasatkinefce41b2010-11-30 10:13:32 +0200939 .cra_alignmask = 0,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800940 .cra_type = &crypto_ablkcipher_type,
941 .cra_module = THIS_MODULE,
942 .cra_init = omap_aes_cra_init,
943 .cra_exit = omap_aes_cra_exit,
944 .cra_u.ablkcipher = {
945 .min_keysize = AES_MIN_KEY_SIZE,
946 .max_keysize = AES_MAX_KEY_SIZE,
947 .ivsize = AES_BLOCK_SIZE,
948 .setkey = omap_aes_setkey,
949 .encrypt = omap_aes_cbc_encrypt,
950 .decrypt = omap_aes_cbc_decrypt,
951 }
952}
953};
954
955static int omap_aes_probe(struct platform_device *pdev)
956{
957 struct device *dev = &pdev->dev;
958 struct omap_aes_dev *dd;
959 struct resource *res;
960 int err = -ENOMEM, i, j;
961 u32 reg;
962
963 dd = kzalloc(sizeof(struct omap_aes_dev), GFP_KERNEL);
964 if (dd == NULL) {
965 dev_err(dev, "unable to alloc data struct.\n");
966 goto err_data;
967 }
968 dd->dev = dev;
969 platform_set_drvdata(pdev, dd);
970
971 spin_lock_init(&dd->lock);
972 crypto_init_queue(&dd->queue, OMAP_AES_QUEUE_LENGTH);
973
974 /* Get the base address */
975 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
976 if (!res) {
977 dev_err(dev, "invalid resource type\n");
978 err = -ENODEV;
979 goto err_res;
980 }
981 dd->phys_base = res->start;
982
983 /* Get the DMA */
984 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
985 if (!res)
986 dev_info(dev, "no DMA info\n");
987 else
988 dd->dma_out = res->start;
989
990 /* Get the DMA */
991 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
992 if (!res)
993 dev_info(dev, "no DMA info\n");
994 else
995 dd->dma_in = res->start;
996
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800997 dd->io_base = ioremap(dd->phys_base, SZ_4K);
998 if (!dd->io_base) {
999 dev_err(dev, "can't ioremap\n");
1000 err = -ENOMEM;
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001001 goto err_res;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001002 }
1003
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001004 pm_runtime_enable(dev);
1005 pm_runtime_get_sync(dev);
1006
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001007 reg = omap_aes_read(dd, AES_REG_REV);
1008 dev_info(dev, "OMAP AES hw accel rev: %u.%u\n",
1009 (reg & AES_REG_REV_MAJOR) >> 4, reg & AES_REG_REV_MINOR);
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001010
1011 pm_runtime_put_sync(dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001012
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +02001013 tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd);
1014 tasklet_init(&dd->queue_task, omap_aes_queue_task, (unsigned long)dd);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001015
1016 err = omap_aes_dma_init(dd);
1017 if (err)
1018 goto err_dma;
1019
1020 INIT_LIST_HEAD(&dd->list);
1021 spin_lock(&list_lock);
1022 list_add_tail(&dd->list, &dev_list);
1023 spin_unlock(&list_lock);
1024
1025 for (i = 0; i < ARRAY_SIZE(algs); i++) {
1026 pr_debug("i: %d\n", i);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001027 err = crypto_register_alg(&algs[i]);
1028 if (err)
1029 goto err_algs;
1030 }
1031
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001032 return 0;
1033err_algs:
1034 for (j = 0; j < i; j++)
1035 crypto_unregister_alg(&algs[j]);
1036 omap_aes_dma_cleanup(dd);
1037err_dma:
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +02001038 tasklet_kill(&dd->done_task);
1039 tasklet_kill(&dd->queue_task);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001040 iounmap(dd->io_base);
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001041 pm_runtime_disable(dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001042err_res:
1043 kfree(dd);
1044 dd = NULL;
1045err_data:
1046 dev_err(dev, "initialization failed.\n");
1047 return err;
1048}
1049
1050static int omap_aes_remove(struct platform_device *pdev)
1051{
1052 struct omap_aes_dev *dd = platform_get_drvdata(pdev);
1053 int i;
1054
1055 if (!dd)
1056 return -ENODEV;
1057
1058 spin_lock(&list_lock);
1059 list_del(&dd->list);
1060 spin_unlock(&list_lock);
1061
1062 for (i = 0; i < ARRAY_SIZE(algs); i++)
1063 crypto_unregister_alg(&algs[i]);
1064
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +02001065 tasklet_kill(&dd->done_task);
1066 tasklet_kill(&dd->queue_task);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001067 omap_aes_dma_cleanup(dd);
1068 iounmap(dd->io_base);
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001069 pm_runtime_disable(dd->dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001070 kfree(dd);
1071 dd = NULL;
1072
1073 return 0;
1074}
1075
Mark A. Greer0635fb32013-01-08 11:57:41 -07001076#ifdef CONFIG_PM_SLEEP
1077static int omap_aes_suspend(struct device *dev)
1078{
1079 pm_runtime_put_sync(dev);
1080 return 0;
1081}
1082
1083static int omap_aes_resume(struct device *dev)
1084{
1085 pm_runtime_get_sync(dev);
1086 return 0;
1087}
1088#endif
1089
1090static const struct dev_pm_ops omap_aes_pm_ops = {
1091 SET_SYSTEM_SLEEP_PM_OPS(omap_aes_suspend, omap_aes_resume)
1092};
1093
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001094static struct platform_driver omap_aes_driver = {
1095 .probe = omap_aes_probe,
1096 .remove = omap_aes_remove,
1097 .driver = {
1098 .name = "omap-aes",
1099 .owner = THIS_MODULE,
Mark A. Greer0635fb32013-01-08 11:57:41 -07001100 .pm = &omap_aes_pm_ops,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001101 },
1102};
1103
1104static int __init omap_aes_mod_init(void)
1105{
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001106 return platform_driver_register(&omap_aes_driver);
1107}
1108
1109static void __exit omap_aes_mod_exit(void)
1110{
1111 platform_driver_unregister(&omap_aes_driver);
1112}
1113
1114module_init(omap_aes_mod_init);
1115module_exit(omap_aes_mod_exit);
1116
1117MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
1118MODULE_LICENSE("GPL v2");
1119MODULE_AUTHOR("Dmitry Kasatkin");
1120