blob: 81f0e848b8d1a080cde16ee43b44b75c5f34ccf7 [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
79#define DEFAULT_TIMEOUT (5*HZ)
80
81#define FLAGS_MODE_MASK 0x000f
82#define FLAGS_ENCRYPT BIT(0)
83#define FLAGS_CBC BIT(1)
84#define FLAGS_GIV BIT(2)
Mark A. Greerf9fb69e2013-01-08 11:57:47 -070085#define FLAGS_CTR BIT(3)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080086
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +020087#define FLAGS_INIT BIT(4)
88#define FLAGS_FAST BIT(5)
89#define FLAGS_BUSY BIT(6)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080090
91struct omap_aes_ctx {
92 struct omap_aes_dev *dd;
93
94 int keylen;
95 u32 key[AES_KEYSIZE_256 / sizeof(u32)];
96 unsigned long flags;
97};
98
99struct omap_aes_reqctx {
100 unsigned long mode;
101};
102
103#define OMAP_AES_QUEUE_LENGTH 1
104#define OMAP_AES_CACHE_SIZE 0
105
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700106struct omap_aes_algs_info {
107 struct crypto_alg *algs_list;
108 unsigned int size;
109 unsigned int registered;
110};
111
Mark A. Greer0d355832013-01-08 11:57:46 -0700112struct omap_aes_pdata {
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700113 struct omap_aes_algs_info *algs_info;
114 unsigned int algs_info_size;
115
Mark A. Greer0d355832013-01-08 11:57:46 -0700116 void (*trigger)(struct omap_aes_dev *dd, int length);
117
118 u32 key_ofs;
119 u32 iv_ofs;
120 u32 ctrl_ofs;
121 u32 data_ofs;
122 u32 rev_ofs;
123 u32 mask_ofs;
124
125 u32 dma_enable_in;
126 u32 dma_enable_out;
127 u32 dma_start;
128
129 u32 major_mask;
130 u32 major_shift;
131 u32 minor_mask;
132 u32 minor_shift;
133};
134
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800135struct omap_aes_dev {
136 struct list_head list;
137 unsigned long phys_base;
Dmitry Kasatkinefce41b2010-11-30 10:13:32 +0200138 void __iomem *io_base;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800139 struct omap_aes_ctx *ctx;
140 struct device *dev;
141 unsigned long flags;
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200142 int err;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800143
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200144 spinlock_t lock;
145 struct crypto_queue queue;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800146
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200147 struct tasklet_struct done_task;
148 struct tasklet_struct queue_task;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800149
150 struct ablkcipher_request *req;
151 size_t total;
152 struct scatterlist *in_sg;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800153 struct scatterlist *out_sg;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800154 int dma_in;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700155 struct dma_chan *dma_lch_in;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800156 int dma_out;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700157 struct dma_chan *dma_lch_out;
Joel Fernandese77c7562013-08-17 21:42:24 -0500158 int in_sg_len;
159 int out_sg_len;
Mark A. Greer0d355832013-01-08 11:57:46 -0700160 const struct omap_aes_pdata *pdata;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800161};
162
163/* keep registered devices data here */
164static LIST_HEAD(dev_list);
165static DEFINE_SPINLOCK(list_lock);
166
Joel Fernandes016af9b2013-08-18 00:56:11 -0500167#ifdef DEBUG
168#define omap_aes_read(dd, offset) \
169({ \
170 int _read_ret; \
171 _read_ret = __raw_readl(dd->io_base + offset); \
172 pr_debug("omap_aes_read(" #offset "=%#x)= %#x\n", \
173 offset, _read_ret); \
174 _read_ret; \
175})
176#else
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800177static inline u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset)
178{
179 return __raw_readl(dd->io_base + offset);
180}
Joel Fernandes016af9b2013-08-18 00:56:11 -0500181#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800182
Joel Fernandes016af9b2013-08-18 00:56:11 -0500183#ifdef DEBUG
184#define omap_aes_write(dd, offset, value) \
185 do { \
186 pr_debug("omap_aes_write(" #offset "=%#x) value=%#x\n", \
187 offset, value); \
188 __raw_writel(value, dd->io_base + offset); \
189 } while (0)
190#else
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800191static inline void omap_aes_write(struct omap_aes_dev *dd, u32 offset,
192 u32 value)
193{
194 __raw_writel(value, dd->io_base + offset);
195}
Joel Fernandes016af9b2013-08-18 00:56:11 -0500196#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800197
198static inline void omap_aes_write_mask(struct omap_aes_dev *dd, u32 offset,
199 u32 value, u32 mask)
200{
201 u32 val;
202
203 val = omap_aes_read(dd, offset);
204 val &= ~mask;
205 val |= value;
206 omap_aes_write(dd, offset, val);
207}
208
209static void omap_aes_write_n(struct omap_aes_dev *dd, u32 offset,
210 u32 *value, int count)
211{
212 for (; count--; value++, offset += 4)
213 omap_aes_write(dd, offset, *value);
214}
215
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800216static int omap_aes_hw_init(struct omap_aes_dev *dd)
217{
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800218 if (!(dd->flags & FLAGS_INIT)) {
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200219 dd->flags |= FLAGS_INIT;
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200220 dd->err = 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800221 }
222
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200223 return 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800224}
225
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200226static int omap_aes_write_ctrl(struct omap_aes_dev *dd)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800227{
228 unsigned int key32;
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200229 int i, err;
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700230 u32 val, mask = 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800231
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200232 err = omap_aes_hw_init(dd);
233 if (err)
234 return err;
235
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800236 key32 = dd->ctx->keylen / sizeof(u32);
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200237
238 /* it seems a key should always be set even if it has not changed */
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800239 for (i = 0; i < key32; i++) {
Mark A. Greer0d355832013-01-08 11:57:46 -0700240 omap_aes_write(dd, AES_REG_KEY(dd, i),
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800241 __le32_to_cpu(dd->ctx->key[i]));
242 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800243
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700244 if ((dd->flags & (FLAGS_CBC | FLAGS_CTR)) && dd->req->info)
Mark A. Greer0d355832013-01-08 11:57:46 -0700245 omap_aes_write_n(dd, AES_REG_IV(dd, 0), dd->req->info, 4);
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200246
247 val = FLD_VAL(((dd->ctx->keylen >> 3) - 1), 4, 3);
248 if (dd->flags & FLAGS_CBC)
249 val |= AES_REG_CTRL_CBC;
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700250 if (dd->flags & FLAGS_CTR) {
251 val |= AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_32;
252 mask = AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_MASK;
253 }
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200254 if (dd->flags & FLAGS_ENCRYPT)
255 val |= AES_REG_CTRL_DIRECTION;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800256
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700257 mask |= AES_REG_CTRL_CBC | AES_REG_CTRL_DIRECTION |
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800258 AES_REG_CTRL_KEY_SIZE;
259
Mark A. Greer0d355832013-01-08 11:57:46 -0700260 omap_aes_write_mask(dd, AES_REG_CTRL(dd), val, mask);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800261
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200262 return 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800263}
264
Mark A. Greer0d355832013-01-08 11:57:46 -0700265static void omap_aes_dma_trigger_omap2(struct omap_aes_dev *dd, int length)
266{
267 u32 mask, val;
268
269 val = dd->pdata->dma_start;
270
271 if (dd->dma_lch_out != NULL)
272 val |= dd->pdata->dma_enable_out;
273 if (dd->dma_lch_in != NULL)
274 val |= dd->pdata->dma_enable_in;
275
276 mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
277 dd->pdata->dma_start;
278
279 omap_aes_write_mask(dd, AES_REG_MASK(dd), val, mask);
280
281}
282
283static void omap_aes_dma_trigger_omap4(struct omap_aes_dev *dd, int length)
284{
285 omap_aes_write(dd, AES_REG_LENGTH_N(0), length);
286 omap_aes_write(dd, AES_REG_LENGTH_N(1), 0);
287
288 omap_aes_dma_trigger_omap2(dd, length);
289}
290
291static void omap_aes_dma_stop(struct omap_aes_dev *dd)
292{
293 u32 mask;
294
295 mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
296 dd->pdata->dma_start;
297
298 omap_aes_write_mask(dd, AES_REG_MASK(dd), 0, mask);
299}
300
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800301static struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_ctx *ctx)
302{
303 struct omap_aes_dev *dd = NULL, *tmp;
304
305 spin_lock_bh(&list_lock);
306 if (!ctx->dd) {
307 list_for_each_entry(tmp, &dev_list, list) {
308 /* FIXME: take fist available aes core */
309 dd = tmp;
310 break;
311 }
312 ctx->dd = dd;
313 } else {
314 /* already found before */
315 dd = ctx->dd;
316 }
317 spin_unlock_bh(&list_lock);
318
319 return dd;
320}
321
Mark A. Greerebedbf72013-01-08 11:57:42 -0700322static void omap_aes_dma_out_callback(void *data)
323{
324 struct omap_aes_dev *dd = data;
325
326 /* dma_lch_out - completed */
327 tasklet_schedule(&dd->done_task);
328}
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800329
330static int omap_aes_dma_init(struct omap_aes_dev *dd)
331{
332 int err = -ENOMEM;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700333 dma_cap_mask_t mask;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800334
Mark A. Greerebedbf72013-01-08 11:57:42 -0700335 dd->dma_lch_out = NULL;
336 dd->dma_lch_in = NULL;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800337
Mark A. Greerebedbf72013-01-08 11:57:42 -0700338 dma_cap_zero(mask);
339 dma_cap_set(DMA_SLAVE, mask);
340
Mark A. Greerb4b87a92013-01-08 11:57:45 -0700341 dd->dma_lch_in = dma_request_slave_channel_compat(mask,
342 omap_dma_filter_fn,
343 &dd->dma_in,
344 dd->dev, "rx");
Mark A. Greerebedbf72013-01-08 11:57:42 -0700345 if (!dd->dma_lch_in) {
346 dev_err(dd->dev, "Unable to request in DMA channel\n");
347 goto err_dma_in;
348 }
349
Mark A. Greerb4b87a92013-01-08 11:57:45 -0700350 dd->dma_lch_out = dma_request_slave_channel_compat(mask,
351 omap_dma_filter_fn,
352 &dd->dma_out,
353 dd->dev, "tx");
Mark A. Greerebedbf72013-01-08 11:57:42 -0700354 if (!dd->dma_lch_out) {
355 dev_err(dd->dev, "Unable to request out DMA channel\n");
356 goto err_dma_out;
357 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800358
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800359 return 0;
360
361err_dma_out:
Mark A. Greerebedbf72013-01-08 11:57:42 -0700362 dma_release_channel(dd->dma_lch_in);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800363err_dma_in:
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800364 if (err)
365 pr_err("error: %d\n", err);
366 return err;
367}
368
369static void omap_aes_dma_cleanup(struct omap_aes_dev *dd)
370{
Mark A. Greerebedbf72013-01-08 11:57:42 -0700371 dma_release_channel(dd->dma_lch_out);
372 dma_release_channel(dd->dma_lch_in);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800373}
374
375static void sg_copy_buf(void *buf, struct scatterlist *sg,
376 unsigned int start, unsigned int nbytes, int out)
377{
378 struct scatter_walk walk;
379
380 if (!nbytes)
381 return;
382
383 scatterwalk_start(&walk, sg);
384 scatterwalk_advance(&walk, start);
385 scatterwalk_copychunks(buf, &walk, nbytes, out);
386 scatterwalk_done(&walk, out, 0);
387}
388
Mark A. Greerebedbf72013-01-08 11:57:42 -0700389static int omap_aes_crypt_dma(struct crypto_tfm *tfm,
Joel Fernandes4b645c92013-08-17 21:42:25 -0500390 struct scatterlist *in_sg, struct scatterlist *out_sg,
391 int in_sg_len, int out_sg_len)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800392{
393 struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
394 struct omap_aes_dev *dd = ctx->dd;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700395 struct dma_async_tx_descriptor *tx_in, *tx_out;
396 struct dma_slave_config cfg;
Joel Fernandes4b645c92013-08-17 21:42:25 -0500397 int ret;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800398
Joel Fernandes0a641712013-08-17 21:42:26 -0500399 dma_sync_sg_for_device(dd->dev, dd->in_sg, in_sg_len, DMA_TO_DEVICE);
400
Mark A. Greerebedbf72013-01-08 11:57:42 -0700401 memset(&cfg, 0, sizeof(cfg));
402
Mark A. Greer0d355832013-01-08 11:57:46 -0700403 cfg.src_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
404 cfg.dst_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
Mark A. Greerebedbf72013-01-08 11:57:42 -0700405 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
406 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
407 cfg.src_maxburst = DST_MAXBURST;
408 cfg.dst_maxburst = DST_MAXBURST;
409
410 /* IN */
411 ret = dmaengine_slave_config(dd->dma_lch_in, &cfg);
412 if (ret) {
413 dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
414 ret);
415 return ret;
416 }
417
Joel Fernandes4b645c92013-08-17 21:42:25 -0500418 tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, in_sg_len,
Mark A. Greerebedbf72013-01-08 11:57:42 -0700419 DMA_MEM_TO_DEV,
420 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
421 if (!tx_in) {
422 dev_err(dd->dev, "IN prep_slave_sg() failed\n");
423 return -EINVAL;
424 }
425
426 /* No callback necessary */
427 tx_in->callback_param = dd;
428
429 /* OUT */
430 ret = dmaengine_slave_config(dd->dma_lch_out, &cfg);
431 if (ret) {
432 dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n",
433 ret);
434 return ret;
435 }
436
Joel Fernandes4b645c92013-08-17 21:42:25 -0500437 tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, out_sg_len,
Mark A. Greerebedbf72013-01-08 11:57:42 -0700438 DMA_DEV_TO_MEM,
439 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
440 if (!tx_out) {
441 dev_err(dd->dev, "OUT prep_slave_sg() failed\n");
442 return -EINVAL;
443 }
444
445 tx_out->callback = omap_aes_dma_out_callback;
446 tx_out->callback_param = dd;
447
448 dmaengine_submit(tx_in);
449 dmaengine_submit(tx_out);
450
451 dma_async_issue_pending(dd->dma_lch_in);
452 dma_async_issue_pending(dd->dma_lch_out);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800453
Mark A. Greer0d355832013-01-08 11:57:46 -0700454 /* start DMA */
Joel Fernandes4b645c92013-08-17 21:42:25 -0500455 dd->pdata->trigger(dd, dd->total);
Dmitry Kasatkin83ea7e02010-11-30 10:13:31 +0200456
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800457 return 0;
458}
459
460static int omap_aes_crypt_dma_start(struct omap_aes_dev *dd)
461{
462 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(
463 crypto_ablkcipher_reqtfm(dd->req));
Joel Fernandes4b645c92013-08-17 21:42:25 -0500464 int err;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800465
466 pr_debug("total: %d\n", dd->total);
467
Joel Fernandes4b645c92013-08-17 21:42:25 -0500468 err = dma_map_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
469 if (!err) {
470 dev_err(dd->dev, "dma_map_sg() error\n");
471 return -EINVAL;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800472 }
473
Joel Fernandes4b645c92013-08-17 21:42:25 -0500474 err = dma_map_sg(dd->dev, dd->out_sg, dd->out_sg_len, DMA_FROM_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 = omap_aes_crypt_dma(tfm, dd->in_sg, dd->out_sg, dd->in_sg_len,
481 dd->out_sg_len);
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200482 if (err) {
Joel Fernandes4b645c92013-08-17 21:42:25 -0500483 dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
484 dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
485 DMA_FROM_DEVICE);
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200486 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800487
488 return err;
489}
490
491static void omap_aes_finish_req(struct omap_aes_dev *dd, int err)
492{
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200493 struct ablkcipher_request *req = dd->req;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800494
495 pr_debug("err: %d\n", err);
496
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200497 dd->flags &= ~FLAGS_BUSY;
498
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200499 req->base.complete(&req->base, err);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800500}
501
502static int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd)
503{
504 int err = 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800505
506 pr_debug("total: %d\n", dd->total);
507
Mark A. Greer0d355832013-01-08 11:57:46 -0700508 omap_aes_dma_stop(dd);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800509
Mark A. Greerebedbf72013-01-08 11:57:42 -0700510 dmaengine_terminate_all(dd->dma_lch_in);
511 dmaengine_terminate_all(dd->dma_lch_out);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800512
Joel Fernandes4b645c92013-08-17 21:42:25 -0500513 dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
514 dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len, DMA_FROM_DEVICE);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800515
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800516 return err;
517}
518
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200519static int omap_aes_handle_queue(struct omap_aes_dev *dd,
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200520 struct ablkcipher_request *req)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800521{
522 struct crypto_async_request *async_req, *backlog;
523 struct omap_aes_ctx *ctx;
524 struct omap_aes_reqctx *rctx;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800525 unsigned long flags;
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200526 int err, ret = 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800527
528 spin_lock_irqsave(&dd->lock, flags);
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200529 if (req)
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200530 ret = ablkcipher_enqueue_request(&dd->queue, req);
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200531 if (dd->flags & FLAGS_BUSY) {
532 spin_unlock_irqrestore(&dd->lock, flags);
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200533 return ret;
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200534 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800535 backlog = crypto_get_backlog(&dd->queue);
536 async_req = crypto_dequeue_request(&dd->queue);
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200537 if (async_req)
538 dd->flags |= FLAGS_BUSY;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800539 spin_unlock_irqrestore(&dd->lock, flags);
540
541 if (!async_req)
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200542 return ret;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800543
544 if (backlog)
545 backlog->complete(backlog, -EINPROGRESS);
546
547 req = ablkcipher_request_cast(async_req);
548
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800549 /* assign new request to device */
550 dd->req = req;
551 dd->total = req->nbytes;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800552 dd->in_sg = req->src;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800553 dd->out_sg = req->dst;
554
Joel Fernandese77c7562013-08-17 21:42:24 -0500555 dd->in_sg_len = scatterwalk_bytes_sglen(dd->in_sg, dd->total);
556 dd->out_sg_len = scatterwalk_bytes_sglen(dd->out_sg, dd->total);
557 BUG_ON(dd->in_sg_len < 0 || dd->out_sg_len < 0);
558
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800559 rctx = ablkcipher_request_ctx(req);
560 ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
561 rctx->mode &= FLAGS_MODE_MASK;
562 dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
563
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200564 dd->ctx = ctx;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800565 ctx->dd = dd;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800566
Dmitry Kasatkin83ea7e02010-11-30 10:13:31 +0200567 err = omap_aes_write_ctrl(dd);
568 if (!err)
569 err = omap_aes_crypt_dma_start(dd);
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200570 if (err) {
571 /* aes_task will not finish it, so do it here */
572 omap_aes_finish_req(dd, err);
573 tasklet_schedule(&dd->queue_task);
574 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800575
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200576 return ret; /* return ret, which is enqueue return value */
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800577}
578
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200579static void omap_aes_done_task(unsigned long data)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800580{
581 struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800582
Joel Fernandes4b645c92013-08-17 21:42:25 -0500583 pr_debug("enter done_task\n");
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800584
Joel Fernandes0a641712013-08-17 21:42:26 -0500585 dma_sync_sg_for_cpu(dd->dev, dd->in_sg, dd->in_sg_len, DMA_FROM_DEVICE);
586
Joel Fernandes4b645c92013-08-17 21:42:25 -0500587 omap_aes_crypt_dma_stop(dd);
588 omap_aes_finish_req(dd, 0);
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200589 omap_aes_handle_queue(dd, NULL);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800590
591 pr_debug("exit\n");
592}
593
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200594static void omap_aes_queue_task(unsigned long data)
595{
596 struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
597
598 omap_aes_handle_queue(dd, NULL);
599}
600
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800601static int omap_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
602{
603 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
604 crypto_ablkcipher_reqtfm(req));
605 struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req);
606 struct omap_aes_dev *dd;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800607
608 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->nbytes,
609 !!(mode & FLAGS_ENCRYPT),
610 !!(mode & FLAGS_CBC));
611
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200612 if (!IS_ALIGNED(req->nbytes, AES_BLOCK_SIZE)) {
613 pr_err("request size is not exact amount of AES blocks\n");
614 return -EINVAL;
615 }
616
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800617 dd = omap_aes_find_dev(ctx);
618 if (!dd)
619 return -ENODEV;
620
621 rctx->mode = mode;
622
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200623 return omap_aes_handle_queue(dd, req);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800624}
625
626/* ********************** ALG API ************************************ */
627
628static int omap_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
629 unsigned int keylen)
630{
631 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
632
633 if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
634 keylen != AES_KEYSIZE_256)
635 return -EINVAL;
636
637 pr_debug("enter, keylen: %d\n", keylen);
638
639 memcpy(ctx->key, key, keylen);
640 ctx->keylen = keylen;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800641
642 return 0;
643}
644
645static int omap_aes_ecb_encrypt(struct ablkcipher_request *req)
646{
647 return omap_aes_crypt(req, FLAGS_ENCRYPT);
648}
649
650static int omap_aes_ecb_decrypt(struct ablkcipher_request *req)
651{
652 return omap_aes_crypt(req, 0);
653}
654
655static int omap_aes_cbc_encrypt(struct ablkcipher_request *req)
656{
657 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
658}
659
660static int omap_aes_cbc_decrypt(struct ablkcipher_request *req)
661{
662 return omap_aes_crypt(req, FLAGS_CBC);
663}
664
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700665static int omap_aes_ctr_encrypt(struct ablkcipher_request *req)
666{
667 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CTR);
668}
669
670static int omap_aes_ctr_decrypt(struct ablkcipher_request *req)
671{
672 return omap_aes_crypt(req, FLAGS_CTR);
673}
674
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800675static int omap_aes_cra_init(struct crypto_tfm *tfm)
676{
Joel A Fernandesa3485e62013-05-28 19:02:55 -0500677 struct omap_aes_dev *dd = NULL;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800678
Joel A Fernandesa3485e62013-05-28 19:02:55 -0500679 /* Find AES device, currently picks the first device */
680 spin_lock_bh(&list_lock);
681 list_for_each_entry(dd, &dev_list, list) {
682 break;
683 }
684 spin_unlock_bh(&list_lock);
685
686 pm_runtime_get_sync(dd->dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800687 tfm->crt_ablkcipher.reqsize = sizeof(struct omap_aes_reqctx);
688
689 return 0;
690}
691
692static void omap_aes_cra_exit(struct crypto_tfm *tfm)
693{
Joel A Fernandesa3485e62013-05-28 19:02:55 -0500694 struct omap_aes_dev *dd = NULL;
695
696 /* Find AES device, currently picks the first device */
697 spin_lock_bh(&list_lock);
698 list_for_each_entry(dd, &dev_list, list) {
699 break;
700 }
701 spin_unlock_bh(&list_lock);
702
703 pm_runtime_put_sync(dd->dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800704}
705
706/* ********************** ALGS ************************************ */
707
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700708static struct crypto_alg algs_ecb_cbc[] = {
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800709{
710 .cra_name = "ecb(aes)",
711 .cra_driver_name = "ecb-aes-omap",
712 .cra_priority = 100,
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +0100713 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
714 CRYPTO_ALG_KERN_DRIVER_ONLY |
715 CRYPTO_ALG_ASYNC,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800716 .cra_blocksize = AES_BLOCK_SIZE,
717 .cra_ctxsize = sizeof(struct omap_aes_ctx),
Dmitry Kasatkinefce41b2010-11-30 10:13:32 +0200718 .cra_alignmask = 0,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800719 .cra_type = &crypto_ablkcipher_type,
720 .cra_module = THIS_MODULE,
721 .cra_init = omap_aes_cra_init,
722 .cra_exit = omap_aes_cra_exit,
723 .cra_u.ablkcipher = {
724 .min_keysize = AES_MIN_KEY_SIZE,
725 .max_keysize = AES_MAX_KEY_SIZE,
726 .setkey = omap_aes_setkey,
727 .encrypt = omap_aes_ecb_encrypt,
728 .decrypt = omap_aes_ecb_decrypt,
729 }
730},
731{
732 .cra_name = "cbc(aes)",
733 .cra_driver_name = "cbc-aes-omap",
734 .cra_priority = 100,
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +0100735 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
736 CRYPTO_ALG_KERN_DRIVER_ONLY |
737 CRYPTO_ALG_ASYNC,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800738 .cra_blocksize = AES_BLOCK_SIZE,
739 .cra_ctxsize = sizeof(struct omap_aes_ctx),
Dmitry Kasatkinefce41b2010-11-30 10:13:32 +0200740 .cra_alignmask = 0,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800741 .cra_type = &crypto_ablkcipher_type,
742 .cra_module = THIS_MODULE,
743 .cra_init = omap_aes_cra_init,
744 .cra_exit = omap_aes_cra_exit,
745 .cra_u.ablkcipher = {
746 .min_keysize = AES_MIN_KEY_SIZE,
747 .max_keysize = AES_MAX_KEY_SIZE,
748 .ivsize = AES_BLOCK_SIZE,
749 .setkey = omap_aes_setkey,
750 .encrypt = omap_aes_cbc_encrypt,
751 .decrypt = omap_aes_cbc_decrypt,
752 }
753}
754};
755
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700756static struct crypto_alg algs_ctr[] = {
757{
758 .cra_name = "ctr(aes)",
759 .cra_driver_name = "ctr-aes-omap",
760 .cra_priority = 100,
761 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
762 CRYPTO_ALG_KERN_DRIVER_ONLY |
763 CRYPTO_ALG_ASYNC,
764 .cra_blocksize = AES_BLOCK_SIZE,
765 .cra_ctxsize = sizeof(struct omap_aes_ctx),
766 .cra_alignmask = 0,
767 .cra_type = &crypto_ablkcipher_type,
768 .cra_module = THIS_MODULE,
769 .cra_init = omap_aes_cra_init,
770 .cra_exit = omap_aes_cra_exit,
771 .cra_u.ablkcipher = {
772 .min_keysize = AES_MIN_KEY_SIZE,
773 .max_keysize = AES_MAX_KEY_SIZE,
774 .geniv = "eseqiv",
775 .ivsize = AES_BLOCK_SIZE,
776 .setkey = omap_aes_setkey,
777 .encrypt = omap_aes_ctr_encrypt,
778 .decrypt = omap_aes_ctr_decrypt,
779 }
780} ,
781};
782
783static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc[] = {
784 {
785 .algs_list = algs_ecb_cbc,
786 .size = ARRAY_SIZE(algs_ecb_cbc),
787 },
788};
789
Mark A. Greer0d355832013-01-08 11:57:46 -0700790static const struct omap_aes_pdata omap_aes_pdata_omap2 = {
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700791 .algs_info = omap_aes_algs_info_ecb_cbc,
792 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc),
Mark A. Greer0d355832013-01-08 11:57:46 -0700793 .trigger = omap_aes_dma_trigger_omap2,
794 .key_ofs = 0x1c,
795 .iv_ofs = 0x20,
796 .ctrl_ofs = 0x30,
797 .data_ofs = 0x34,
798 .rev_ofs = 0x44,
799 .mask_ofs = 0x48,
800 .dma_enable_in = BIT(2),
801 .dma_enable_out = BIT(3),
802 .dma_start = BIT(5),
803 .major_mask = 0xf0,
804 .major_shift = 4,
805 .minor_mask = 0x0f,
806 .minor_shift = 0,
807};
808
Mark A. Greerbc69d122013-01-08 11:57:44 -0700809#ifdef CONFIG_OF
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700810static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr[] = {
811 {
812 .algs_list = algs_ecb_cbc,
813 .size = ARRAY_SIZE(algs_ecb_cbc),
814 },
815 {
816 .algs_list = algs_ctr,
817 .size = ARRAY_SIZE(algs_ctr),
818 },
819};
820
821static const struct omap_aes_pdata omap_aes_pdata_omap3 = {
822 .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
823 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
824 .trigger = omap_aes_dma_trigger_omap2,
825 .key_ofs = 0x1c,
826 .iv_ofs = 0x20,
827 .ctrl_ofs = 0x30,
828 .data_ofs = 0x34,
829 .rev_ofs = 0x44,
830 .mask_ofs = 0x48,
831 .dma_enable_in = BIT(2),
832 .dma_enable_out = BIT(3),
833 .dma_start = BIT(5),
834 .major_mask = 0xf0,
835 .major_shift = 4,
836 .minor_mask = 0x0f,
837 .minor_shift = 0,
838};
839
Mark A. Greer0d355832013-01-08 11:57:46 -0700840static const struct omap_aes_pdata omap_aes_pdata_omap4 = {
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700841 .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
842 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
Mark A. Greer0d355832013-01-08 11:57:46 -0700843 .trigger = omap_aes_dma_trigger_omap4,
844 .key_ofs = 0x3c,
845 .iv_ofs = 0x40,
846 .ctrl_ofs = 0x50,
847 .data_ofs = 0x60,
848 .rev_ofs = 0x80,
849 .mask_ofs = 0x84,
850 .dma_enable_in = BIT(5),
851 .dma_enable_out = BIT(6),
852 .major_mask = 0x0700,
853 .major_shift = 8,
854 .minor_mask = 0x003f,
855 .minor_shift = 0,
856};
857
Mark A. Greerbc69d122013-01-08 11:57:44 -0700858static const struct of_device_id omap_aes_of_match[] = {
859 {
860 .compatible = "ti,omap2-aes",
Mark A. Greer0d355832013-01-08 11:57:46 -0700861 .data = &omap_aes_pdata_omap2,
862 },
863 {
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700864 .compatible = "ti,omap3-aes",
865 .data = &omap_aes_pdata_omap3,
866 },
867 {
Mark A. Greer0d355832013-01-08 11:57:46 -0700868 .compatible = "ti,omap4-aes",
869 .data = &omap_aes_pdata_omap4,
Mark A. Greerbc69d122013-01-08 11:57:44 -0700870 },
871 {},
872};
873MODULE_DEVICE_TABLE(of, omap_aes_of_match);
874
875static int omap_aes_get_res_of(struct omap_aes_dev *dd,
876 struct device *dev, struct resource *res)
877{
878 struct device_node *node = dev->of_node;
879 const struct of_device_id *match;
880 int err = 0;
881
882 match = of_match_device(of_match_ptr(omap_aes_of_match), dev);
883 if (!match) {
884 dev_err(dev, "no compatible OF match\n");
885 err = -EINVAL;
886 goto err;
887 }
888
889 err = of_address_to_resource(node, 0, res);
890 if (err < 0) {
891 dev_err(dev, "can't translate OF node address\n");
892 err = -EINVAL;
893 goto err;
894 }
895
896 dd->dma_out = -1; /* Dummy value that's unused */
897 dd->dma_in = -1; /* Dummy value that's unused */
898
Mark A. Greer0d355832013-01-08 11:57:46 -0700899 dd->pdata = match->data;
900
Mark A. Greerbc69d122013-01-08 11:57:44 -0700901err:
902 return err;
903}
904#else
905static const struct of_device_id omap_aes_of_match[] = {
906 {},
907};
908
909static int omap_aes_get_res_of(struct omap_aes_dev *dd,
910 struct device *dev, struct resource *res)
911{
912 return -EINVAL;
913}
914#endif
915
916static int omap_aes_get_res_pdev(struct omap_aes_dev *dd,
917 struct platform_device *pdev, struct resource *res)
918{
919 struct device *dev = &pdev->dev;
920 struct resource *r;
921 int err = 0;
922
923 /* Get the base address */
924 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
925 if (!r) {
926 dev_err(dev, "no MEM resource info\n");
927 err = -ENODEV;
928 goto err;
929 }
930 memcpy(res, r, sizeof(*res));
931
932 /* Get the DMA out channel */
933 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
934 if (!r) {
935 dev_err(dev, "no DMA out resource info\n");
936 err = -ENODEV;
937 goto err;
938 }
939 dd->dma_out = r->start;
940
941 /* Get the DMA in channel */
942 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
943 if (!r) {
944 dev_err(dev, "no DMA in resource info\n");
945 err = -ENODEV;
946 goto err;
947 }
948 dd->dma_in = r->start;
949
Mark A. Greer0d355832013-01-08 11:57:46 -0700950 /* Only OMAP2/3 can be non-DT */
951 dd->pdata = &omap_aes_pdata_omap2;
952
Mark A. Greerbc69d122013-01-08 11:57:44 -0700953err:
954 return err;
955}
956
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800957static int omap_aes_probe(struct platform_device *pdev)
958{
959 struct device *dev = &pdev->dev;
960 struct omap_aes_dev *dd;
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700961 struct crypto_alg *algp;
Mark A. Greerbc69d122013-01-08 11:57:44 -0700962 struct resource res;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800963 int err = -ENOMEM, i, j;
964 u32 reg;
965
966 dd = kzalloc(sizeof(struct omap_aes_dev), GFP_KERNEL);
967 if (dd == NULL) {
968 dev_err(dev, "unable to alloc data struct.\n");
969 goto err_data;
970 }
971 dd->dev = dev;
972 platform_set_drvdata(pdev, dd);
973
974 spin_lock_init(&dd->lock);
975 crypto_init_queue(&dd->queue, OMAP_AES_QUEUE_LENGTH);
976
Mark A. Greerbc69d122013-01-08 11:57:44 -0700977 err = (dev->of_node) ? omap_aes_get_res_of(dd, dev, &res) :
978 omap_aes_get_res_pdev(dd, pdev, &res);
979 if (err)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800980 goto err_res;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800981
Laurent Navet30862282013-05-02 14:00:38 +0200982 dd->io_base = devm_ioremap_resource(dev, &res);
983 if (IS_ERR(dd->io_base)) {
984 err = PTR_ERR(dd->io_base);
Mark A. Greer5946c4a2013-01-08 11:57:40 -0700985 goto err_res;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800986 }
Mark A. Greerbc69d122013-01-08 11:57:44 -0700987 dd->phys_base = res.start;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800988
Mark A. Greer5946c4a2013-01-08 11:57:40 -0700989 pm_runtime_enable(dev);
990 pm_runtime_get_sync(dev);
991
Mark A. Greer0d355832013-01-08 11:57:46 -0700992 omap_aes_dma_stop(dd);
993
994 reg = omap_aes_read(dd, AES_REG_REV(dd));
Mark A. Greer5946c4a2013-01-08 11:57:40 -0700995
996 pm_runtime_put_sync(dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800997
Mark A. Greer0d355832013-01-08 11:57:46 -0700998 dev_info(dev, "OMAP AES hw accel rev: %u.%u\n",
999 (reg & dd->pdata->major_mask) >> dd->pdata->major_shift,
1000 (reg & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
1001
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +02001002 tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd);
1003 tasklet_init(&dd->queue_task, omap_aes_queue_task, (unsigned long)dd);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001004
1005 err = omap_aes_dma_init(dd);
1006 if (err)
1007 goto err_dma;
1008
1009 INIT_LIST_HEAD(&dd->list);
1010 spin_lock(&list_lock);
1011 list_add_tail(&dd->list, &dev_list);
1012 spin_unlock(&list_lock);
1013
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001014 for (i = 0; i < dd->pdata->algs_info_size; i++) {
1015 for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
1016 algp = &dd->pdata->algs_info[i].algs_list[j];
1017
1018 pr_debug("reg alg: %s\n", algp->cra_name);
1019 INIT_LIST_HEAD(&algp->cra_list);
1020
1021 err = crypto_register_alg(algp);
1022 if (err)
1023 goto err_algs;
1024
1025 dd->pdata->algs_info[i].registered++;
1026 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001027 }
1028
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001029 return 0;
1030err_algs:
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001031 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1032 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1033 crypto_unregister_alg(
1034 &dd->pdata->algs_info[i].algs_list[j]);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001035 omap_aes_dma_cleanup(dd);
1036err_dma:
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +02001037 tasklet_kill(&dd->done_task);
1038 tasklet_kill(&dd->queue_task);
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001039 pm_runtime_disable(dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001040err_res:
1041 kfree(dd);
1042 dd = NULL;
1043err_data:
1044 dev_err(dev, "initialization failed.\n");
1045 return err;
1046}
1047
1048static int omap_aes_remove(struct platform_device *pdev)
1049{
1050 struct omap_aes_dev *dd = platform_get_drvdata(pdev);
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001051 int i, j;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001052
1053 if (!dd)
1054 return -ENODEV;
1055
1056 spin_lock(&list_lock);
1057 list_del(&dd->list);
1058 spin_unlock(&list_lock);
1059
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001060 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1061 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1062 crypto_unregister_alg(
1063 &dd->pdata->algs_info[i].algs_list[j]);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001064
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);
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001068 pm_runtime_disable(dd->dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001069 kfree(dd);
1070 dd = NULL;
1071
1072 return 0;
1073}
1074
Mark A. Greer0635fb32013-01-08 11:57:41 -07001075#ifdef CONFIG_PM_SLEEP
1076static int omap_aes_suspend(struct device *dev)
1077{
1078 pm_runtime_put_sync(dev);
1079 return 0;
1080}
1081
1082static int omap_aes_resume(struct device *dev)
1083{
1084 pm_runtime_get_sync(dev);
1085 return 0;
1086}
1087#endif
1088
1089static const struct dev_pm_ops omap_aes_pm_ops = {
1090 SET_SYSTEM_SLEEP_PM_OPS(omap_aes_suspend, omap_aes_resume)
1091};
1092
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001093static struct platform_driver omap_aes_driver = {
1094 .probe = omap_aes_probe,
1095 .remove = omap_aes_remove,
1096 .driver = {
1097 .name = "omap-aes",
1098 .owner = THIS_MODULE,
Mark A. Greer0635fb32013-01-08 11:57:41 -07001099 .pm = &omap_aes_pm_ops,
Mark A. Greerbc69d122013-01-08 11:57:44 -07001100 .of_match_table = omap_aes_of_match,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001101 },
1102};
1103
Sachin Kamat94e51df2013-03-04 15:09:42 +05301104module_platform_driver(omap_aes_driver);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001105
1106MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
1107MODULE_LICENSE("GPL v2");
1108MODULE_AUTHOR("Dmitry Kasatkin");
1109