Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1 | /* |
| 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. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 8 | * Copyright (c) 2011 Texas Instruments Incorporated |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 9 | * |
| 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 Fernandes | 016af9b | 2013-08-18 00:56:11 -0500 | [diff] [blame] | 16 | #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 Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 19 | |
| 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 Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/scatterlist.h> |
| 27 | #include <linux/dma-mapping.h> |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 28 | #include <linux/dmaengine.h> |
| 29 | #include <linux/omap-dma.h> |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 30 | #include <linux/pm_runtime.h> |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 31 | #include <linux/of.h> |
| 32 | #include <linux/of_device.h> |
| 33 | #include <linux/of_address.h> |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 34 | #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. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 40 | #define DST_MAXBURST 4 |
| 41 | #define DMA_MIN (DST_MAXBURST * sizeof(u32)) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 42 | |
| 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. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 48 | #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 Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 51 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 52 | #define AES_REG_CTRL(dd) ((dd)->pdata->ctrl_ofs) |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 53 | #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 Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 58 | #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. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 65 | #define AES_REG_DATA_N(dd, x) ((dd)->pdata->data_ofs + ((x) * 0x04)) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 66 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 67 | #define AES_REG_REV(dd) ((dd)->pdata->rev_ofs) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 68 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 69 | #define AES_REG_MASK(dd) ((dd)->pdata->mask_ofs) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 70 | #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. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 77 | #define AES_REG_LENGTH_N(x) (0x54 + ((x) * 0x04)) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 78 | |
| 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. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 85 | #define FLAGS_CTR BIT(3) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 86 | |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 87 | #define FLAGS_INIT BIT(4) |
| 88 | #define FLAGS_FAST BIT(5) |
| 89 | #define FLAGS_BUSY BIT(6) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 90 | |
| 91 | struct 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 | |
| 99 | struct 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. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 106 | struct omap_aes_algs_info { |
| 107 | struct crypto_alg *algs_list; |
| 108 | unsigned int size; |
| 109 | unsigned int registered; |
| 110 | }; |
| 111 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 112 | struct omap_aes_pdata { |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 113 | struct omap_aes_algs_info *algs_info; |
| 114 | unsigned int algs_info_size; |
| 115 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 116 | 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 Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 135 | struct omap_aes_dev { |
| 136 | struct list_head list; |
| 137 | unsigned long phys_base; |
Dmitry Kasatkin | efce41b | 2010-11-30 10:13:32 +0200 | [diff] [blame] | 138 | void __iomem *io_base; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 139 | struct omap_aes_ctx *ctx; |
| 140 | struct device *dev; |
| 141 | unsigned long flags; |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 142 | int err; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 143 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 144 | spinlock_t lock; |
| 145 | struct crypto_queue queue; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 146 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 147 | struct tasklet_struct done_task; |
| 148 | struct tasklet_struct queue_task; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 149 | |
| 150 | struct ablkcipher_request *req; |
| 151 | size_t total; |
| 152 | struct scatterlist *in_sg; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 153 | struct scatterlist in_sgl; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 154 | size_t in_offset; |
| 155 | struct scatterlist *out_sg; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 156 | struct scatterlist out_sgl; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 157 | size_t out_offset; |
| 158 | |
| 159 | size_t buflen; |
| 160 | void *buf_in; |
| 161 | size_t dma_size; |
| 162 | int dma_in; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 163 | struct dma_chan *dma_lch_in; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 164 | dma_addr_t dma_addr_in; |
| 165 | void *buf_out; |
| 166 | int dma_out; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 167 | struct dma_chan *dma_lch_out; |
Joel Fernandes | e77c756 | 2013-08-17 21:42:24 -0500 | [diff] [blame] | 168 | int in_sg_len; |
| 169 | int out_sg_len; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 170 | dma_addr_t dma_addr_out; |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 171 | |
| 172 | const struct omap_aes_pdata *pdata; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | /* keep registered devices data here */ |
| 176 | static LIST_HEAD(dev_list); |
| 177 | static DEFINE_SPINLOCK(list_lock); |
| 178 | |
Joel Fernandes | 016af9b | 2013-08-18 00:56:11 -0500 | [diff] [blame] | 179 | #ifdef DEBUG |
| 180 | #define omap_aes_read(dd, offset) \ |
| 181 | ({ \ |
| 182 | int _read_ret; \ |
| 183 | _read_ret = __raw_readl(dd->io_base + offset); \ |
| 184 | pr_debug("omap_aes_read(" #offset "=%#x)= %#x\n", \ |
| 185 | offset, _read_ret); \ |
| 186 | _read_ret; \ |
| 187 | }) |
| 188 | #else |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 189 | static inline u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset) |
| 190 | { |
| 191 | return __raw_readl(dd->io_base + offset); |
| 192 | } |
Joel Fernandes | 016af9b | 2013-08-18 00:56:11 -0500 | [diff] [blame] | 193 | #endif |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 194 | |
Joel Fernandes | 016af9b | 2013-08-18 00:56:11 -0500 | [diff] [blame] | 195 | #ifdef DEBUG |
| 196 | #define omap_aes_write(dd, offset, value) \ |
| 197 | do { \ |
| 198 | pr_debug("omap_aes_write(" #offset "=%#x) value=%#x\n", \ |
| 199 | offset, value); \ |
| 200 | __raw_writel(value, dd->io_base + offset); \ |
| 201 | } while (0) |
| 202 | #else |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 203 | static inline void omap_aes_write(struct omap_aes_dev *dd, u32 offset, |
| 204 | u32 value) |
| 205 | { |
| 206 | __raw_writel(value, dd->io_base + offset); |
| 207 | } |
Joel Fernandes | 016af9b | 2013-08-18 00:56:11 -0500 | [diff] [blame] | 208 | #endif |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 209 | |
| 210 | static inline void omap_aes_write_mask(struct omap_aes_dev *dd, u32 offset, |
| 211 | u32 value, u32 mask) |
| 212 | { |
| 213 | u32 val; |
| 214 | |
| 215 | val = omap_aes_read(dd, offset); |
| 216 | val &= ~mask; |
| 217 | val |= value; |
| 218 | omap_aes_write(dd, offset, val); |
| 219 | } |
| 220 | |
| 221 | static void omap_aes_write_n(struct omap_aes_dev *dd, u32 offset, |
| 222 | u32 *value, int count) |
| 223 | { |
| 224 | for (; count--; value++, offset += 4) |
| 225 | omap_aes_write(dd, offset, *value); |
| 226 | } |
| 227 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 228 | static int omap_aes_hw_init(struct omap_aes_dev *dd) |
| 229 | { |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 230 | if (!(dd->flags & FLAGS_INIT)) { |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 231 | dd->flags |= FLAGS_INIT; |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 232 | dd->err = 0; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 233 | } |
| 234 | |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 235 | return 0; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 236 | } |
| 237 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 238 | static int omap_aes_write_ctrl(struct omap_aes_dev *dd) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 239 | { |
| 240 | unsigned int key32; |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 241 | int i, err; |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 242 | u32 val, mask = 0; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 243 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 244 | err = omap_aes_hw_init(dd); |
| 245 | if (err) |
| 246 | return err; |
| 247 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 248 | key32 = dd->ctx->keylen / sizeof(u32); |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 249 | |
| 250 | /* it seems a key should always be set even if it has not changed */ |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 251 | for (i = 0; i < key32; i++) { |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 252 | omap_aes_write(dd, AES_REG_KEY(dd, i), |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 253 | __le32_to_cpu(dd->ctx->key[i])); |
| 254 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 255 | |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 256 | if ((dd->flags & (FLAGS_CBC | FLAGS_CTR)) && dd->req->info) |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 257 | omap_aes_write_n(dd, AES_REG_IV(dd, 0), dd->req->info, 4); |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 258 | |
| 259 | val = FLD_VAL(((dd->ctx->keylen >> 3) - 1), 4, 3); |
| 260 | if (dd->flags & FLAGS_CBC) |
| 261 | val |= AES_REG_CTRL_CBC; |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 262 | if (dd->flags & FLAGS_CTR) { |
| 263 | val |= AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_32; |
| 264 | mask = AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_MASK; |
| 265 | } |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 266 | if (dd->flags & FLAGS_ENCRYPT) |
| 267 | val |= AES_REG_CTRL_DIRECTION; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 268 | |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 269 | mask |= AES_REG_CTRL_CBC | AES_REG_CTRL_DIRECTION | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 270 | AES_REG_CTRL_KEY_SIZE; |
| 271 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 272 | omap_aes_write_mask(dd, AES_REG_CTRL(dd), val, mask); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 273 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 274 | return 0; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 275 | } |
| 276 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 277 | static void omap_aes_dma_trigger_omap2(struct omap_aes_dev *dd, int length) |
| 278 | { |
| 279 | u32 mask, val; |
| 280 | |
| 281 | val = dd->pdata->dma_start; |
| 282 | |
| 283 | if (dd->dma_lch_out != NULL) |
| 284 | val |= dd->pdata->dma_enable_out; |
| 285 | if (dd->dma_lch_in != NULL) |
| 286 | val |= dd->pdata->dma_enable_in; |
| 287 | |
| 288 | mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in | |
| 289 | dd->pdata->dma_start; |
| 290 | |
| 291 | omap_aes_write_mask(dd, AES_REG_MASK(dd), val, mask); |
| 292 | |
| 293 | } |
| 294 | |
| 295 | static void omap_aes_dma_trigger_omap4(struct omap_aes_dev *dd, int length) |
| 296 | { |
| 297 | omap_aes_write(dd, AES_REG_LENGTH_N(0), length); |
| 298 | omap_aes_write(dd, AES_REG_LENGTH_N(1), 0); |
| 299 | |
| 300 | omap_aes_dma_trigger_omap2(dd, length); |
| 301 | } |
| 302 | |
| 303 | static void omap_aes_dma_stop(struct omap_aes_dev *dd) |
| 304 | { |
| 305 | u32 mask; |
| 306 | |
| 307 | mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in | |
| 308 | dd->pdata->dma_start; |
| 309 | |
| 310 | omap_aes_write_mask(dd, AES_REG_MASK(dd), 0, mask); |
| 311 | } |
| 312 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 313 | static struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_ctx *ctx) |
| 314 | { |
| 315 | struct omap_aes_dev *dd = NULL, *tmp; |
| 316 | |
| 317 | spin_lock_bh(&list_lock); |
| 318 | if (!ctx->dd) { |
| 319 | list_for_each_entry(tmp, &dev_list, list) { |
| 320 | /* FIXME: take fist available aes core */ |
| 321 | dd = tmp; |
| 322 | break; |
| 323 | } |
| 324 | ctx->dd = dd; |
| 325 | } else { |
| 326 | /* already found before */ |
| 327 | dd = ctx->dd; |
| 328 | } |
| 329 | spin_unlock_bh(&list_lock); |
| 330 | |
| 331 | return dd; |
| 332 | } |
| 333 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 334 | static void omap_aes_dma_out_callback(void *data) |
| 335 | { |
| 336 | struct omap_aes_dev *dd = data; |
| 337 | |
| 338 | /* dma_lch_out - completed */ |
| 339 | tasklet_schedule(&dd->done_task); |
| 340 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 341 | |
| 342 | static int omap_aes_dma_init(struct omap_aes_dev *dd) |
| 343 | { |
| 344 | int err = -ENOMEM; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 345 | dma_cap_mask_t mask; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 346 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 347 | dd->dma_lch_out = NULL; |
| 348 | dd->dma_lch_in = NULL; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 349 | |
| 350 | dd->buf_in = (void *)__get_free_pages(GFP_KERNEL, OMAP_AES_CACHE_SIZE); |
| 351 | dd->buf_out = (void *)__get_free_pages(GFP_KERNEL, OMAP_AES_CACHE_SIZE); |
| 352 | dd->buflen = PAGE_SIZE << OMAP_AES_CACHE_SIZE; |
| 353 | dd->buflen &= ~(AES_BLOCK_SIZE - 1); |
| 354 | |
| 355 | if (!dd->buf_in || !dd->buf_out) { |
| 356 | dev_err(dd->dev, "unable to alloc pages.\n"); |
| 357 | goto err_alloc; |
| 358 | } |
| 359 | |
| 360 | /* MAP here */ |
| 361 | dd->dma_addr_in = dma_map_single(dd->dev, dd->buf_in, dd->buflen, |
| 362 | DMA_TO_DEVICE); |
| 363 | if (dma_mapping_error(dd->dev, dd->dma_addr_in)) { |
| 364 | dev_err(dd->dev, "dma %d bytes error\n", dd->buflen); |
| 365 | err = -EINVAL; |
| 366 | goto err_map_in; |
| 367 | } |
| 368 | |
| 369 | dd->dma_addr_out = dma_map_single(dd->dev, dd->buf_out, dd->buflen, |
| 370 | DMA_FROM_DEVICE); |
| 371 | if (dma_mapping_error(dd->dev, dd->dma_addr_out)) { |
| 372 | dev_err(dd->dev, "dma %d bytes error\n", dd->buflen); |
| 373 | err = -EINVAL; |
| 374 | goto err_map_out; |
| 375 | } |
| 376 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 377 | dma_cap_zero(mask); |
| 378 | dma_cap_set(DMA_SLAVE, mask); |
| 379 | |
Mark A. Greer | b4b87a9 | 2013-01-08 11:57:45 -0700 | [diff] [blame] | 380 | dd->dma_lch_in = dma_request_slave_channel_compat(mask, |
| 381 | omap_dma_filter_fn, |
| 382 | &dd->dma_in, |
| 383 | dd->dev, "rx"); |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 384 | if (!dd->dma_lch_in) { |
| 385 | dev_err(dd->dev, "Unable to request in DMA channel\n"); |
| 386 | goto err_dma_in; |
| 387 | } |
| 388 | |
Mark A. Greer | b4b87a9 | 2013-01-08 11:57:45 -0700 | [diff] [blame] | 389 | dd->dma_lch_out = dma_request_slave_channel_compat(mask, |
| 390 | omap_dma_filter_fn, |
| 391 | &dd->dma_out, |
| 392 | dd->dev, "tx"); |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 393 | if (!dd->dma_lch_out) { |
| 394 | dev_err(dd->dev, "Unable to request out DMA channel\n"); |
| 395 | goto err_dma_out; |
| 396 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 397 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 398 | return 0; |
| 399 | |
| 400 | err_dma_out: |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 401 | dma_release_channel(dd->dma_lch_in); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 402 | err_dma_in: |
| 403 | dma_unmap_single(dd->dev, dd->dma_addr_out, dd->buflen, |
| 404 | DMA_FROM_DEVICE); |
| 405 | err_map_out: |
| 406 | dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen, DMA_TO_DEVICE); |
| 407 | err_map_in: |
| 408 | free_pages((unsigned long)dd->buf_out, OMAP_AES_CACHE_SIZE); |
| 409 | free_pages((unsigned long)dd->buf_in, OMAP_AES_CACHE_SIZE); |
| 410 | err_alloc: |
| 411 | if (err) |
| 412 | pr_err("error: %d\n", err); |
| 413 | return err; |
| 414 | } |
| 415 | |
| 416 | static void omap_aes_dma_cleanup(struct omap_aes_dev *dd) |
| 417 | { |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 418 | dma_release_channel(dd->dma_lch_out); |
| 419 | dma_release_channel(dd->dma_lch_in); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 420 | dma_unmap_single(dd->dev, dd->dma_addr_out, dd->buflen, |
| 421 | DMA_FROM_DEVICE); |
| 422 | dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen, DMA_TO_DEVICE); |
| 423 | free_pages((unsigned long)dd->buf_out, OMAP_AES_CACHE_SIZE); |
| 424 | free_pages((unsigned long)dd->buf_in, OMAP_AES_CACHE_SIZE); |
| 425 | } |
| 426 | |
| 427 | static void sg_copy_buf(void *buf, struct scatterlist *sg, |
| 428 | unsigned int start, unsigned int nbytes, int out) |
| 429 | { |
| 430 | struct scatter_walk walk; |
| 431 | |
| 432 | if (!nbytes) |
| 433 | return; |
| 434 | |
| 435 | scatterwalk_start(&walk, sg); |
| 436 | scatterwalk_advance(&walk, start); |
| 437 | scatterwalk_copychunks(buf, &walk, nbytes, out); |
| 438 | scatterwalk_done(&walk, out, 0); |
| 439 | } |
| 440 | |
| 441 | static int sg_copy(struct scatterlist **sg, size_t *offset, void *buf, |
| 442 | size_t buflen, size_t total, int out) |
| 443 | { |
| 444 | unsigned int count, off = 0; |
| 445 | |
| 446 | while (buflen && total) { |
| 447 | count = min((*sg)->length - *offset, total); |
| 448 | count = min(count, buflen); |
| 449 | |
| 450 | if (!count) |
| 451 | return off; |
| 452 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 453 | /* |
| 454 | * buflen and total are AES_BLOCK_SIZE size aligned, |
| 455 | * so count should be also aligned |
| 456 | */ |
| 457 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 458 | sg_copy_buf(buf + off, *sg, *offset, count, out); |
| 459 | |
| 460 | off += count; |
| 461 | buflen -= count; |
| 462 | *offset += count; |
| 463 | total -= count; |
| 464 | |
| 465 | if (*offset == (*sg)->length) { |
| 466 | *sg = sg_next(*sg); |
| 467 | if (*sg) |
| 468 | *offset = 0; |
| 469 | else |
| 470 | total = 0; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | return off; |
| 475 | } |
| 476 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 477 | static int omap_aes_crypt_dma(struct crypto_tfm *tfm, |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame^] | 478 | struct scatterlist *in_sg, struct scatterlist *out_sg, |
| 479 | int in_sg_len, int out_sg_len) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 480 | { |
| 481 | struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
| 482 | struct omap_aes_dev *dd = ctx->dd; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 483 | struct dma_async_tx_descriptor *tx_in, *tx_out; |
| 484 | struct dma_slave_config cfg; |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame^] | 485 | int ret; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 486 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 487 | memset(&cfg, 0, sizeof(cfg)); |
| 488 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 489 | cfg.src_addr = dd->phys_base + AES_REG_DATA_N(dd, 0); |
| 490 | cfg.dst_addr = dd->phys_base + AES_REG_DATA_N(dd, 0); |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 491 | cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 492 | cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 493 | cfg.src_maxburst = DST_MAXBURST; |
| 494 | cfg.dst_maxburst = DST_MAXBURST; |
| 495 | |
| 496 | /* IN */ |
| 497 | ret = dmaengine_slave_config(dd->dma_lch_in, &cfg); |
| 498 | if (ret) { |
| 499 | dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n", |
| 500 | ret); |
| 501 | return ret; |
| 502 | } |
| 503 | |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame^] | 504 | tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, in_sg_len, |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 505 | DMA_MEM_TO_DEV, |
| 506 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 507 | if (!tx_in) { |
| 508 | dev_err(dd->dev, "IN prep_slave_sg() failed\n"); |
| 509 | return -EINVAL; |
| 510 | } |
| 511 | |
| 512 | /* No callback necessary */ |
| 513 | tx_in->callback_param = dd; |
| 514 | |
| 515 | /* OUT */ |
| 516 | ret = dmaengine_slave_config(dd->dma_lch_out, &cfg); |
| 517 | if (ret) { |
| 518 | dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n", |
| 519 | ret); |
| 520 | return ret; |
| 521 | } |
| 522 | |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame^] | 523 | tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, out_sg_len, |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 524 | DMA_DEV_TO_MEM, |
| 525 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 526 | if (!tx_out) { |
| 527 | dev_err(dd->dev, "OUT prep_slave_sg() failed\n"); |
| 528 | return -EINVAL; |
| 529 | } |
| 530 | |
| 531 | tx_out->callback = omap_aes_dma_out_callback; |
| 532 | tx_out->callback_param = dd; |
| 533 | |
| 534 | dmaengine_submit(tx_in); |
| 535 | dmaengine_submit(tx_out); |
| 536 | |
| 537 | dma_async_issue_pending(dd->dma_lch_in); |
| 538 | dma_async_issue_pending(dd->dma_lch_out); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 539 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 540 | /* start DMA */ |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame^] | 541 | dd->pdata->trigger(dd, dd->total); |
Dmitry Kasatkin | 83ea7e0 | 2010-11-30 10:13:31 +0200 | [diff] [blame] | 542 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | static int omap_aes_crypt_dma_start(struct omap_aes_dev *dd) |
| 547 | { |
| 548 | struct crypto_tfm *tfm = crypto_ablkcipher_tfm( |
| 549 | crypto_ablkcipher_reqtfm(dd->req)); |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame^] | 550 | int err; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 551 | |
| 552 | pr_debug("total: %d\n", dd->total); |
| 553 | |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame^] | 554 | err = dma_map_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE); |
| 555 | if (!err) { |
| 556 | dev_err(dd->dev, "dma_map_sg() error\n"); |
| 557 | return -EINVAL; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 558 | } |
| 559 | |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame^] | 560 | err = dma_map_sg(dd->dev, dd->out_sg, dd->out_sg_len, DMA_FROM_DEVICE); |
| 561 | if (!err) { |
| 562 | dev_err(dd->dev, "dma_map_sg() error\n"); |
| 563 | return -EINVAL; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 564 | } |
| 565 | |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame^] | 566 | err = omap_aes_crypt_dma(tfm, dd->in_sg, dd->out_sg, dd->in_sg_len, |
| 567 | dd->out_sg_len); |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 568 | if (err) { |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame^] | 569 | dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE); |
| 570 | dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len, |
| 571 | DMA_FROM_DEVICE); |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 572 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 573 | |
| 574 | return err; |
| 575 | } |
| 576 | |
| 577 | static void omap_aes_finish_req(struct omap_aes_dev *dd, int err) |
| 578 | { |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 579 | struct ablkcipher_request *req = dd->req; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 580 | |
| 581 | pr_debug("err: %d\n", err); |
| 582 | |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 583 | dd->flags &= ~FLAGS_BUSY; |
| 584 | |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 585 | req->base.complete(&req->base, err); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | static int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd) |
| 589 | { |
| 590 | int err = 0; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 591 | |
| 592 | pr_debug("total: %d\n", dd->total); |
| 593 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 594 | omap_aes_dma_stop(dd); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 595 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 596 | dmaengine_terminate_all(dd->dma_lch_in); |
| 597 | dmaengine_terminate_all(dd->dma_lch_out); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 598 | |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame^] | 599 | dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE); |
| 600 | dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len, DMA_FROM_DEVICE); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 601 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 602 | return err; |
| 603 | } |
| 604 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 605 | static int omap_aes_handle_queue(struct omap_aes_dev *dd, |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 606 | struct ablkcipher_request *req) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 607 | { |
| 608 | struct crypto_async_request *async_req, *backlog; |
| 609 | struct omap_aes_ctx *ctx; |
| 610 | struct omap_aes_reqctx *rctx; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 611 | unsigned long flags; |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 612 | int err, ret = 0; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 613 | |
| 614 | spin_lock_irqsave(&dd->lock, flags); |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 615 | if (req) |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 616 | ret = ablkcipher_enqueue_request(&dd->queue, req); |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 617 | if (dd->flags & FLAGS_BUSY) { |
| 618 | spin_unlock_irqrestore(&dd->lock, flags); |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 619 | return ret; |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 620 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 621 | backlog = crypto_get_backlog(&dd->queue); |
| 622 | async_req = crypto_dequeue_request(&dd->queue); |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 623 | if (async_req) |
| 624 | dd->flags |= FLAGS_BUSY; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 625 | spin_unlock_irqrestore(&dd->lock, flags); |
| 626 | |
| 627 | if (!async_req) |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 628 | return ret; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 629 | |
| 630 | if (backlog) |
| 631 | backlog->complete(backlog, -EINPROGRESS); |
| 632 | |
| 633 | req = ablkcipher_request_cast(async_req); |
| 634 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 635 | /* assign new request to device */ |
| 636 | dd->req = req; |
| 637 | dd->total = req->nbytes; |
| 638 | dd->in_offset = 0; |
| 639 | dd->in_sg = req->src; |
| 640 | dd->out_offset = 0; |
| 641 | dd->out_sg = req->dst; |
| 642 | |
Joel Fernandes | e77c756 | 2013-08-17 21:42:24 -0500 | [diff] [blame] | 643 | dd->in_sg_len = scatterwalk_bytes_sglen(dd->in_sg, dd->total); |
| 644 | dd->out_sg_len = scatterwalk_bytes_sglen(dd->out_sg, dd->total); |
| 645 | BUG_ON(dd->in_sg_len < 0 || dd->out_sg_len < 0); |
| 646 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 647 | rctx = ablkcipher_request_ctx(req); |
| 648 | ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req)); |
| 649 | rctx->mode &= FLAGS_MODE_MASK; |
| 650 | dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode; |
| 651 | |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 652 | dd->ctx = ctx; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 653 | ctx->dd = dd; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 654 | |
Dmitry Kasatkin | 83ea7e0 | 2010-11-30 10:13:31 +0200 | [diff] [blame] | 655 | err = omap_aes_write_ctrl(dd); |
| 656 | if (!err) |
| 657 | err = omap_aes_crypt_dma_start(dd); |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 658 | if (err) { |
| 659 | /* aes_task will not finish it, so do it here */ |
| 660 | omap_aes_finish_req(dd, err); |
| 661 | tasklet_schedule(&dd->queue_task); |
| 662 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 663 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 664 | return ret; /* return ret, which is enqueue return value */ |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 665 | } |
| 666 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 667 | static void omap_aes_done_task(unsigned long data) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 668 | { |
| 669 | struct omap_aes_dev *dd = (struct omap_aes_dev *)data; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 670 | |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame^] | 671 | pr_debug("enter done_task\n"); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 672 | |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame^] | 673 | omap_aes_crypt_dma_stop(dd); |
| 674 | omap_aes_finish_req(dd, 0); |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 675 | omap_aes_handle_queue(dd, NULL); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 676 | |
| 677 | pr_debug("exit\n"); |
| 678 | } |
| 679 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 680 | static void omap_aes_queue_task(unsigned long data) |
| 681 | { |
| 682 | struct omap_aes_dev *dd = (struct omap_aes_dev *)data; |
| 683 | |
| 684 | omap_aes_handle_queue(dd, NULL); |
| 685 | } |
| 686 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 687 | static int omap_aes_crypt(struct ablkcipher_request *req, unsigned long mode) |
| 688 | { |
| 689 | struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx( |
| 690 | crypto_ablkcipher_reqtfm(req)); |
| 691 | struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req); |
| 692 | struct omap_aes_dev *dd; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 693 | |
| 694 | pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->nbytes, |
| 695 | !!(mode & FLAGS_ENCRYPT), |
| 696 | !!(mode & FLAGS_CBC)); |
| 697 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 698 | if (!IS_ALIGNED(req->nbytes, AES_BLOCK_SIZE)) { |
| 699 | pr_err("request size is not exact amount of AES blocks\n"); |
| 700 | return -EINVAL; |
| 701 | } |
| 702 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 703 | dd = omap_aes_find_dev(ctx); |
| 704 | if (!dd) |
| 705 | return -ENODEV; |
| 706 | |
| 707 | rctx->mode = mode; |
| 708 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 709 | return omap_aes_handle_queue(dd, req); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /* ********************** ALG API ************************************ */ |
| 713 | |
| 714 | static int omap_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key, |
| 715 | unsigned int keylen) |
| 716 | { |
| 717 | struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm); |
| 718 | |
| 719 | if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && |
| 720 | keylen != AES_KEYSIZE_256) |
| 721 | return -EINVAL; |
| 722 | |
| 723 | pr_debug("enter, keylen: %d\n", keylen); |
| 724 | |
| 725 | memcpy(ctx->key, key, keylen); |
| 726 | ctx->keylen = keylen; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | static int omap_aes_ecb_encrypt(struct ablkcipher_request *req) |
| 732 | { |
| 733 | return omap_aes_crypt(req, FLAGS_ENCRYPT); |
| 734 | } |
| 735 | |
| 736 | static int omap_aes_ecb_decrypt(struct ablkcipher_request *req) |
| 737 | { |
| 738 | return omap_aes_crypt(req, 0); |
| 739 | } |
| 740 | |
| 741 | static int omap_aes_cbc_encrypt(struct ablkcipher_request *req) |
| 742 | { |
| 743 | return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC); |
| 744 | } |
| 745 | |
| 746 | static int omap_aes_cbc_decrypt(struct ablkcipher_request *req) |
| 747 | { |
| 748 | return omap_aes_crypt(req, FLAGS_CBC); |
| 749 | } |
| 750 | |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 751 | static int omap_aes_ctr_encrypt(struct ablkcipher_request *req) |
| 752 | { |
| 753 | return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CTR); |
| 754 | } |
| 755 | |
| 756 | static int omap_aes_ctr_decrypt(struct ablkcipher_request *req) |
| 757 | { |
| 758 | return omap_aes_crypt(req, FLAGS_CTR); |
| 759 | } |
| 760 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 761 | static int omap_aes_cra_init(struct crypto_tfm *tfm) |
| 762 | { |
Joel A Fernandes | a3485e6 | 2013-05-28 19:02:55 -0500 | [diff] [blame] | 763 | struct omap_aes_dev *dd = NULL; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 764 | |
Joel A Fernandes | a3485e6 | 2013-05-28 19:02:55 -0500 | [diff] [blame] | 765 | /* Find AES device, currently picks the first device */ |
| 766 | spin_lock_bh(&list_lock); |
| 767 | list_for_each_entry(dd, &dev_list, list) { |
| 768 | break; |
| 769 | } |
| 770 | spin_unlock_bh(&list_lock); |
| 771 | |
| 772 | pm_runtime_get_sync(dd->dev); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 773 | tfm->crt_ablkcipher.reqsize = sizeof(struct omap_aes_reqctx); |
| 774 | |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | static void omap_aes_cra_exit(struct crypto_tfm *tfm) |
| 779 | { |
Joel A Fernandes | a3485e6 | 2013-05-28 19:02:55 -0500 | [diff] [blame] | 780 | struct omap_aes_dev *dd = NULL; |
| 781 | |
| 782 | /* Find AES device, currently picks the first device */ |
| 783 | spin_lock_bh(&list_lock); |
| 784 | list_for_each_entry(dd, &dev_list, list) { |
| 785 | break; |
| 786 | } |
| 787 | spin_unlock_bh(&list_lock); |
| 788 | |
| 789 | pm_runtime_put_sync(dd->dev); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /* ********************** ALGS ************************************ */ |
| 793 | |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 794 | static struct crypto_alg algs_ecb_cbc[] = { |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 795 | { |
| 796 | .cra_name = "ecb(aes)", |
| 797 | .cra_driver_name = "ecb-aes-omap", |
| 798 | .cra_priority = 100, |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 799 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
| 800 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
| 801 | CRYPTO_ALG_ASYNC, |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 802 | .cra_blocksize = AES_BLOCK_SIZE, |
| 803 | .cra_ctxsize = sizeof(struct omap_aes_ctx), |
Dmitry Kasatkin | efce41b | 2010-11-30 10:13:32 +0200 | [diff] [blame] | 804 | .cra_alignmask = 0, |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 805 | .cra_type = &crypto_ablkcipher_type, |
| 806 | .cra_module = THIS_MODULE, |
| 807 | .cra_init = omap_aes_cra_init, |
| 808 | .cra_exit = omap_aes_cra_exit, |
| 809 | .cra_u.ablkcipher = { |
| 810 | .min_keysize = AES_MIN_KEY_SIZE, |
| 811 | .max_keysize = AES_MAX_KEY_SIZE, |
| 812 | .setkey = omap_aes_setkey, |
| 813 | .encrypt = omap_aes_ecb_encrypt, |
| 814 | .decrypt = omap_aes_ecb_decrypt, |
| 815 | } |
| 816 | }, |
| 817 | { |
| 818 | .cra_name = "cbc(aes)", |
| 819 | .cra_driver_name = "cbc-aes-omap", |
| 820 | .cra_priority = 100, |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 821 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
| 822 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
| 823 | CRYPTO_ALG_ASYNC, |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 824 | .cra_blocksize = AES_BLOCK_SIZE, |
| 825 | .cra_ctxsize = sizeof(struct omap_aes_ctx), |
Dmitry Kasatkin | efce41b | 2010-11-30 10:13:32 +0200 | [diff] [blame] | 826 | .cra_alignmask = 0, |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 827 | .cra_type = &crypto_ablkcipher_type, |
| 828 | .cra_module = THIS_MODULE, |
| 829 | .cra_init = omap_aes_cra_init, |
| 830 | .cra_exit = omap_aes_cra_exit, |
| 831 | .cra_u.ablkcipher = { |
| 832 | .min_keysize = AES_MIN_KEY_SIZE, |
| 833 | .max_keysize = AES_MAX_KEY_SIZE, |
| 834 | .ivsize = AES_BLOCK_SIZE, |
| 835 | .setkey = omap_aes_setkey, |
| 836 | .encrypt = omap_aes_cbc_encrypt, |
| 837 | .decrypt = omap_aes_cbc_decrypt, |
| 838 | } |
| 839 | } |
| 840 | }; |
| 841 | |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 842 | static struct crypto_alg algs_ctr[] = { |
| 843 | { |
| 844 | .cra_name = "ctr(aes)", |
| 845 | .cra_driver_name = "ctr-aes-omap", |
| 846 | .cra_priority = 100, |
| 847 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
| 848 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
| 849 | CRYPTO_ALG_ASYNC, |
| 850 | .cra_blocksize = AES_BLOCK_SIZE, |
| 851 | .cra_ctxsize = sizeof(struct omap_aes_ctx), |
| 852 | .cra_alignmask = 0, |
| 853 | .cra_type = &crypto_ablkcipher_type, |
| 854 | .cra_module = THIS_MODULE, |
| 855 | .cra_init = omap_aes_cra_init, |
| 856 | .cra_exit = omap_aes_cra_exit, |
| 857 | .cra_u.ablkcipher = { |
| 858 | .min_keysize = AES_MIN_KEY_SIZE, |
| 859 | .max_keysize = AES_MAX_KEY_SIZE, |
| 860 | .geniv = "eseqiv", |
| 861 | .ivsize = AES_BLOCK_SIZE, |
| 862 | .setkey = omap_aes_setkey, |
| 863 | .encrypt = omap_aes_ctr_encrypt, |
| 864 | .decrypt = omap_aes_ctr_decrypt, |
| 865 | } |
| 866 | } , |
| 867 | }; |
| 868 | |
| 869 | static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc[] = { |
| 870 | { |
| 871 | .algs_list = algs_ecb_cbc, |
| 872 | .size = ARRAY_SIZE(algs_ecb_cbc), |
| 873 | }, |
| 874 | }; |
| 875 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 876 | static const struct omap_aes_pdata omap_aes_pdata_omap2 = { |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 877 | .algs_info = omap_aes_algs_info_ecb_cbc, |
| 878 | .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc), |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 879 | .trigger = omap_aes_dma_trigger_omap2, |
| 880 | .key_ofs = 0x1c, |
| 881 | .iv_ofs = 0x20, |
| 882 | .ctrl_ofs = 0x30, |
| 883 | .data_ofs = 0x34, |
| 884 | .rev_ofs = 0x44, |
| 885 | .mask_ofs = 0x48, |
| 886 | .dma_enable_in = BIT(2), |
| 887 | .dma_enable_out = BIT(3), |
| 888 | .dma_start = BIT(5), |
| 889 | .major_mask = 0xf0, |
| 890 | .major_shift = 4, |
| 891 | .minor_mask = 0x0f, |
| 892 | .minor_shift = 0, |
| 893 | }; |
| 894 | |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 895 | #ifdef CONFIG_OF |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 896 | static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr[] = { |
| 897 | { |
| 898 | .algs_list = algs_ecb_cbc, |
| 899 | .size = ARRAY_SIZE(algs_ecb_cbc), |
| 900 | }, |
| 901 | { |
| 902 | .algs_list = algs_ctr, |
| 903 | .size = ARRAY_SIZE(algs_ctr), |
| 904 | }, |
| 905 | }; |
| 906 | |
| 907 | static const struct omap_aes_pdata omap_aes_pdata_omap3 = { |
| 908 | .algs_info = omap_aes_algs_info_ecb_cbc_ctr, |
| 909 | .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr), |
| 910 | .trigger = omap_aes_dma_trigger_omap2, |
| 911 | .key_ofs = 0x1c, |
| 912 | .iv_ofs = 0x20, |
| 913 | .ctrl_ofs = 0x30, |
| 914 | .data_ofs = 0x34, |
| 915 | .rev_ofs = 0x44, |
| 916 | .mask_ofs = 0x48, |
| 917 | .dma_enable_in = BIT(2), |
| 918 | .dma_enable_out = BIT(3), |
| 919 | .dma_start = BIT(5), |
| 920 | .major_mask = 0xf0, |
| 921 | .major_shift = 4, |
| 922 | .minor_mask = 0x0f, |
| 923 | .minor_shift = 0, |
| 924 | }; |
| 925 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 926 | static const struct omap_aes_pdata omap_aes_pdata_omap4 = { |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 927 | .algs_info = omap_aes_algs_info_ecb_cbc_ctr, |
| 928 | .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr), |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 929 | .trigger = omap_aes_dma_trigger_omap4, |
| 930 | .key_ofs = 0x3c, |
| 931 | .iv_ofs = 0x40, |
| 932 | .ctrl_ofs = 0x50, |
| 933 | .data_ofs = 0x60, |
| 934 | .rev_ofs = 0x80, |
| 935 | .mask_ofs = 0x84, |
| 936 | .dma_enable_in = BIT(5), |
| 937 | .dma_enable_out = BIT(6), |
| 938 | .major_mask = 0x0700, |
| 939 | .major_shift = 8, |
| 940 | .minor_mask = 0x003f, |
| 941 | .minor_shift = 0, |
| 942 | }; |
| 943 | |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 944 | static const struct of_device_id omap_aes_of_match[] = { |
| 945 | { |
| 946 | .compatible = "ti,omap2-aes", |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 947 | .data = &omap_aes_pdata_omap2, |
| 948 | }, |
| 949 | { |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 950 | .compatible = "ti,omap3-aes", |
| 951 | .data = &omap_aes_pdata_omap3, |
| 952 | }, |
| 953 | { |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 954 | .compatible = "ti,omap4-aes", |
| 955 | .data = &omap_aes_pdata_omap4, |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 956 | }, |
| 957 | {}, |
| 958 | }; |
| 959 | MODULE_DEVICE_TABLE(of, omap_aes_of_match); |
| 960 | |
| 961 | static int omap_aes_get_res_of(struct omap_aes_dev *dd, |
| 962 | struct device *dev, struct resource *res) |
| 963 | { |
| 964 | struct device_node *node = dev->of_node; |
| 965 | const struct of_device_id *match; |
| 966 | int err = 0; |
| 967 | |
| 968 | match = of_match_device(of_match_ptr(omap_aes_of_match), dev); |
| 969 | if (!match) { |
| 970 | dev_err(dev, "no compatible OF match\n"); |
| 971 | err = -EINVAL; |
| 972 | goto err; |
| 973 | } |
| 974 | |
| 975 | err = of_address_to_resource(node, 0, res); |
| 976 | if (err < 0) { |
| 977 | dev_err(dev, "can't translate OF node address\n"); |
| 978 | err = -EINVAL; |
| 979 | goto err; |
| 980 | } |
| 981 | |
| 982 | dd->dma_out = -1; /* Dummy value that's unused */ |
| 983 | dd->dma_in = -1; /* Dummy value that's unused */ |
| 984 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 985 | dd->pdata = match->data; |
| 986 | |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 987 | err: |
| 988 | return err; |
| 989 | } |
| 990 | #else |
| 991 | static const struct of_device_id omap_aes_of_match[] = { |
| 992 | {}, |
| 993 | }; |
| 994 | |
| 995 | static int omap_aes_get_res_of(struct omap_aes_dev *dd, |
| 996 | struct device *dev, struct resource *res) |
| 997 | { |
| 998 | return -EINVAL; |
| 999 | } |
| 1000 | #endif |
| 1001 | |
| 1002 | static int omap_aes_get_res_pdev(struct omap_aes_dev *dd, |
| 1003 | struct platform_device *pdev, struct resource *res) |
| 1004 | { |
| 1005 | struct device *dev = &pdev->dev; |
| 1006 | struct resource *r; |
| 1007 | int err = 0; |
| 1008 | |
| 1009 | /* Get the base address */ |
| 1010 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1011 | if (!r) { |
| 1012 | dev_err(dev, "no MEM resource info\n"); |
| 1013 | err = -ENODEV; |
| 1014 | goto err; |
| 1015 | } |
| 1016 | memcpy(res, r, sizeof(*res)); |
| 1017 | |
| 1018 | /* Get the DMA out channel */ |
| 1019 | r = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 1020 | if (!r) { |
| 1021 | dev_err(dev, "no DMA out resource info\n"); |
| 1022 | err = -ENODEV; |
| 1023 | goto err; |
| 1024 | } |
| 1025 | dd->dma_out = r->start; |
| 1026 | |
| 1027 | /* Get the DMA in channel */ |
| 1028 | r = platform_get_resource(pdev, IORESOURCE_DMA, 1); |
| 1029 | if (!r) { |
| 1030 | dev_err(dev, "no DMA in resource info\n"); |
| 1031 | err = -ENODEV; |
| 1032 | goto err; |
| 1033 | } |
| 1034 | dd->dma_in = r->start; |
| 1035 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 1036 | /* Only OMAP2/3 can be non-DT */ |
| 1037 | dd->pdata = &omap_aes_pdata_omap2; |
| 1038 | |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 1039 | err: |
| 1040 | return err; |
| 1041 | } |
| 1042 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1043 | static int omap_aes_probe(struct platform_device *pdev) |
| 1044 | { |
| 1045 | struct device *dev = &pdev->dev; |
| 1046 | struct omap_aes_dev *dd; |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 1047 | struct crypto_alg *algp; |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 1048 | struct resource res; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1049 | int err = -ENOMEM, i, j; |
| 1050 | u32 reg; |
| 1051 | |
| 1052 | dd = kzalloc(sizeof(struct omap_aes_dev), GFP_KERNEL); |
| 1053 | if (dd == NULL) { |
| 1054 | dev_err(dev, "unable to alloc data struct.\n"); |
| 1055 | goto err_data; |
| 1056 | } |
| 1057 | dd->dev = dev; |
| 1058 | platform_set_drvdata(pdev, dd); |
| 1059 | |
| 1060 | spin_lock_init(&dd->lock); |
| 1061 | crypto_init_queue(&dd->queue, OMAP_AES_QUEUE_LENGTH); |
| 1062 | |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 1063 | err = (dev->of_node) ? omap_aes_get_res_of(dd, dev, &res) : |
| 1064 | omap_aes_get_res_pdev(dd, pdev, &res); |
| 1065 | if (err) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1066 | goto err_res; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1067 | |
Laurent Navet | 3086228 | 2013-05-02 14:00:38 +0200 | [diff] [blame] | 1068 | dd->io_base = devm_ioremap_resource(dev, &res); |
| 1069 | if (IS_ERR(dd->io_base)) { |
| 1070 | err = PTR_ERR(dd->io_base); |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 1071 | goto err_res; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1072 | } |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 1073 | dd->phys_base = res.start; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1074 | |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 1075 | pm_runtime_enable(dev); |
| 1076 | pm_runtime_get_sync(dev); |
| 1077 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 1078 | omap_aes_dma_stop(dd); |
| 1079 | |
| 1080 | reg = omap_aes_read(dd, AES_REG_REV(dd)); |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 1081 | |
| 1082 | pm_runtime_put_sync(dev); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1083 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 1084 | dev_info(dev, "OMAP AES hw accel rev: %u.%u\n", |
| 1085 | (reg & dd->pdata->major_mask) >> dd->pdata->major_shift, |
| 1086 | (reg & dd->pdata->minor_mask) >> dd->pdata->minor_shift); |
| 1087 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 1088 | tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd); |
| 1089 | tasklet_init(&dd->queue_task, omap_aes_queue_task, (unsigned long)dd); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1090 | |
| 1091 | err = omap_aes_dma_init(dd); |
| 1092 | if (err) |
| 1093 | goto err_dma; |
| 1094 | |
| 1095 | INIT_LIST_HEAD(&dd->list); |
| 1096 | spin_lock(&list_lock); |
| 1097 | list_add_tail(&dd->list, &dev_list); |
| 1098 | spin_unlock(&list_lock); |
| 1099 | |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 1100 | for (i = 0; i < dd->pdata->algs_info_size; i++) { |
| 1101 | for (j = 0; j < dd->pdata->algs_info[i].size; j++) { |
| 1102 | algp = &dd->pdata->algs_info[i].algs_list[j]; |
| 1103 | |
| 1104 | pr_debug("reg alg: %s\n", algp->cra_name); |
| 1105 | INIT_LIST_HEAD(&algp->cra_list); |
| 1106 | |
| 1107 | err = crypto_register_alg(algp); |
| 1108 | if (err) |
| 1109 | goto err_algs; |
| 1110 | |
| 1111 | dd->pdata->algs_info[i].registered++; |
| 1112 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1113 | } |
| 1114 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1115 | return 0; |
| 1116 | err_algs: |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 1117 | for (i = dd->pdata->algs_info_size - 1; i >= 0; i--) |
| 1118 | for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--) |
| 1119 | crypto_unregister_alg( |
| 1120 | &dd->pdata->algs_info[i].algs_list[j]); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1121 | omap_aes_dma_cleanup(dd); |
| 1122 | err_dma: |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 1123 | tasklet_kill(&dd->done_task); |
| 1124 | tasklet_kill(&dd->queue_task); |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 1125 | pm_runtime_disable(dev); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1126 | err_res: |
| 1127 | kfree(dd); |
| 1128 | dd = NULL; |
| 1129 | err_data: |
| 1130 | dev_err(dev, "initialization failed.\n"); |
| 1131 | return err; |
| 1132 | } |
| 1133 | |
| 1134 | static int omap_aes_remove(struct platform_device *pdev) |
| 1135 | { |
| 1136 | struct omap_aes_dev *dd = platform_get_drvdata(pdev); |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 1137 | int i, j; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1138 | |
| 1139 | if (!dd) |
| 1140 | return -ENODEV; |
| 1141 | |
| 1142 | spin_lock(&list_lock); |
| 1143 | list_del(&dd->list); |
| 1144 | spin_unlock(&list_lock); |
| 1145 | |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 1146 | for (i = dd->pdata->algs_info_size - 1; i >= 0; i--) |
| 1147 | for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--) |
| 1148 | crypto_unregister_alg( |
| 1149 | &dd->pdata->algs_info[i].algs_list[j]); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1150 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 1151 | tasklet_kill(&dd->done_task); |
| 1152 | tasklet_kill(&dd->queue_task); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1153 | omap_aes_dma_cleanup(dd); |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 1154 | pm_runtime_disable(dd->dev); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1155 | kfree(dd); |
| 1156 | dd = NULL; |
| 1157 | |
| 1158 | return 0; |
| 1159 | } |
| 1160 | |
Mark A. Greer | 0635fb3 | 2013-01-08 11:57:41 -0700 | [diff] [blame] | 1161 | #ifdef CONFIG_PM_SLEEP |
| 1162 | static int omap_aes_suspend(struct device *dev) |
| 1163 | { |
| 1164 | pm_runtime_put_sync(dev); |
| 1165 | return 0; |
| 1166 | } |
| 1167 | |
| 1168 | static int omap_aes_resume(struct device *dev) |
| 1169 | { |
| 1170 | pm_runtime_get_sync(dev); |
| 1171 | return 0; |
| 1172 | } |
| 1173 | #endif |
| 1174 | |
| 1175 | static const struct dev_pm_ops omap_aes_pm_ops = { |
| 1176 | SET_SYSTEM_SLEEP_PM_OPS(omap_aes_suspend, omap_aes_resume) |
| 1177 | }; |
| 1178 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1179 | static struct platform_driver omap_aes_driver = { |
| 1180 | .probe = omap_aes_probe, |
| 1181 | .remove = omap_aes_remove, |
| 1182 | .driver = { |
| 1183 | .name = "omap-aes", |
| 1184 | .owner = THIS_MODULE, |
Mark A. Greer | 0635fb3 | 2013-01-08 11:57:41 -0700 | [diff] [blame] | 1185 | .pm = &omap_aes_pm_ops, |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 1186 | .of_match_table = omap_aes_of_match, |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1187 | }, |
| 1188 | }; |
| 1189 | |
Sachin Kamat | 94e51df | 2013-03-04 15:09:42 +0530 | [diff] [blame] | 1190 | module_platform_driver(omap_aes_driver); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1191 | |
| 1192 | MODULE_DESCRIPTION("OMAP AES hw acceleration support."); |
| 1193 | MODULE_LICENSE("GPL v2"); |
| 1194 | MODULE_AUTHOR("Dmitry Kasatkin"); |
| 1195 | |