Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * Support for ATMEL AES HW acceleration. |
| 5 | * |
| 6 | * Copyright (c) 2012 Eukréa Electromatique - ATMEL |
| 7 | * Author: Nicolas Royer <nicolas@eukrea.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as published |
| 11 | * by the Free Software Foundation. |
| 12 | * |
| 13 | * Some ideas are from omap-aes.c driver. |
| 14 | */ |
| 15 | |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/clk.h> |
| 22 | #include <linux/io.h> |
| 23 | #include <linux/hw_random.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | |
| 26 | #include <linux/device.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 27 | #include <linux/init.h> |
| 28 | #include <linux/errno.h> |
| 29 | #include <linux/interrupt.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 30 | #include <linux/irq.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 31 | #include <linux/scatterlist.h> |
| 32 | #include <linux/dma-mapping.h> |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 33 | #include <linux/of_device.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 34 | #include <linux/delay.h> |
| 35 | #include <linux/crypto.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 36 | #include <crypto/scatterwalk.h> |
| 37 | #include <crypto/algapi.h> |
| 38 | #include <crypto/aes.h> |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 39 | #include <linux/platform_data/crypto-atmel.h> |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 40 | #include <dt-bindings/dma/at91.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 41 | #include "atmel-aes-regs.h" |
| 42 | |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 43 | #define ATMEL_AES_PRIORITY 300 |
| 44 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 45 | #define CFB8_BLOCK_SIZE 1 |
| 46 | #define CFB16_BLOCK_SIZE 2 |
| 47 | #define CFB32_BLOCK_SIZE 4 |
| 48 | #define CFB64_BLOCK_SIZE 8 |
| 49 | |
| 50 | /* AES flags */ |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 51 | /* Reserve bits [18:16] [14:12] [0] for mode (same as for AES_MR) */ |
| 52 | #define AES_FLAGS_ENCRYPT AES_MR_CYPHER_ENC |
| 53 | #define AES_FLAGS_OPMODE_MASK (AES_MR_OPMOD_MASK | AES_MR_CFBS_MASK) |
| 54 | #define AES_FLAGS_ECB AES_MR_OPMOD_ECB |
| 55 | #define AES_FLAGS_CBC AES_MR_OPMOD_CBC |
| 56 | #define AES_FLAGS_OFB AES_MR_OPMOD_OFB |
| 57 | #define AES_FLAGS_CFB128 (AES_MR_OPMOD_CFB | AES_MR_CFBS_128b) |
| 58 | #define AES_FLAGS_CFB64 (AES_MR_OPMOD_CFB | AES_MR_CFBS_64b) |
| 59 | #define AES_FLAGS_CFB32 (AES_MR_OPMOD_CFB | AES_MR_CFBS_32b) |
| 60 | #define AES_FLAGS_CFB16 (AES_MR_OPMOD_CFB | AES_MR_CFBS_16b) |
| 61 | #define AES_FLAGS_CFB8 (AES_MR_OPMOD_CFB | AES_MR_CFBS_8b) |
| 62 | #define AES_FLAGS_CTR AES_MR_OPMOD_CTR |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 63 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 64 | #define AES_FLAGS_MODE_MASK (AES_FLAGS_OPMODE_MASK | \ |
| 65 | AES_FLAGS_ENCRYPT) |
| 66 | |
| 67 | #define AES_FLAGS_INIT BIT(2) |
| 68 | #define AES_FLAGS_BUSY BIT(3) |
| 69 | #define AES_FLAGS_DMA BIT(4) |
| 70 | #define AES_FLAGS_FAST BIT(5) |
| 71 | |
| 72 | #define AES_FLAGS_PERSISTENT (AES_FLAGS_INIT | AES_FLAGS_BUSY) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 73 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 74 | #define ATMEL_AES_QUEUE_LENGTH 50 |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 75 | |
| 76 | #define ATMEL_AES_DMA_THRESHOLD 16 |
| 77 | |
| 78 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 79 | struct atmel_aes_caps { |
| 80 | bool has_dualbuff; |
| 81 | bool has_cfb64; |
| 82 | u32 max_burst_size; |
| 83 | }; |
| 84 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 85 | struct atmel_aes_dev; |
| 86 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 87 | |
| 88 | typedef int (*atmel_aes_fn_t)(struct atmel_aes_dev *); |
| 89 | |
| 90 | |
| 91 | struct atmel_aes_base_ctx { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 92 | struct atmel_aes_dev *dd; |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 93 | atmel_aes_fn_t start; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 94 | |
| 95 | int keylen; |
| 96 | u32 key[AES_KEYSIZE_256 / sizeof(u32)]; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 97 | |
| 98 | u16 block_size; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 101 | struct atmel_aes_ctx { |
| 102 | struct atmel_aes_base_ctx base; |
| 103 | }; |
| 104 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 105 | struct atmel_aes_reqctx { |
| 106 | unsigned long mode; |
| 107 | }; |
| 108 | |
| 109 | struct atmel_aes_dma { |
| 110 | struct dma_chan *chan; |
| 111 | struct dma_slave_config dma_conf; |
| 112 | }; |
| 113 | |
| 114 | struct atmel_aes_dev { |
| 115 | struct list_head list; |
| 116 | unsigned long phys_base; |
| 117 | void __iomem *io_base; |
| 118 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 119 | struct crypto_async_request *areq; |
| 120 | struct atmel_aes_base_ctx *ctx; |
| 121 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 122 | struct device *dev; |
| 123 | struct clk *iclk; |
| 124 | int irq; |
| 125 | |
| 126 | unsigned long flags; |
| 127 | int err; |
| 128 | |
| 129 | spinlock_t lock; |
| 130 | struct crypto_queue queue; |
| 131 | |
| 132 | struct tasklet_struct done_task; |
| 133 | struct tasklet_struct queue_task; |
| 134 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 135 | size_t total; |
| 136 | |
| 137 | struct scatterlist *in_sg; |
| 138 | unsigned int nb_in_sg; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 139 | size_t in_offset; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 140 | struct scatterlist *out_sg; |
| 141 | unsigned int nb_out_sg; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 142 | size_t out_offset; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 143 | |
| 144 | size_t bufcnt; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 145 | size_t buflen; |
| 146 | size_t dma_size; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 147 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 148 | void *buf_in; |
| 149 | int dma_in; |
| 150 | dma_addr_t dma_addr_in; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 151 | struct atmel_aes_dma dma_lch_in; |
| 152 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 153 | void *buf_out; |
| 154 | int dma_out; |
| 155 | dma_addr_t dma_addr_out; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 156 | struct atmel_aes_dma dma_lch_out; |
| 157 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 158 | struct atmel_aes_caps caps; |
| 159 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 160 | u32 hw_version; |
| 161 | }; |
| 162 | |
| 163 | struct atmel_aes_drv { |
| 164 | struct list_head dev_list; |
| 165 | spinlock_t lock; |
| 166 | }; |
| 167 | |
| 168 | static struct atmel_aes_drv atmel_aes = { |
| 169 | .dev_list = LIST_HEAD_INIT(atmel_aes.dev_list), |
| 170 | .lock = __SPIN_LOCK_UNLOCKED(atmel_aes.lock), |
| 171 | }; |
| 172 | |
| 173 | static int atmel_aes_sg_length(struct ablkcipher_request *req, |
| 174 | struct scatterlist *sg) |
| 175 | { |
| 176 | unsigned int total = req->nbytes; |
| 177 | int sg_nb; |
| 178 | unsigned int len; |
| 179 | struct scatterlist *sg_list; |
| 180 | |
| 181 | sg_nb = 0; |
| 182 | sg_list = sg; |
| 183 | total = req->nbytes; |
| 184 | |
| 185 | while (total) { |
| 186 | len = min(sg_list->length, total); |
| 187 | |
| 188 | sg_nb++; |
| 189 | total -= len; |
| 190 | |
| 191 | sg_list = sg_next(sg_list); |
| 192 | if (!sg_list) |
| 193 | total = 0; |
| 194 | } |
| 195 | |
| 196 | return sg_nb; |
| 197 | } |
| 198 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 199 | static int atmel_aes_sg_copy(struct scatterlist **sg, size_t *offset, |
| 200 | void *buf, size_t buflen, size_t total, int out) |
| 201 | { |
Arnd Bergmann | 20ecae7 | 2015-11-17 10:22:06 +0100 | [diff] [blame] | 202 | size_t count, off = 0; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 203 | |
| 204 | while (buflen && total) { |
| 205 | count = min((*sg)->length - *offset, total); |
| 206 | count = min(count, buflen); |
| 207 | |
| 208 | if (!count) |
| 209 | return off; |
| 210 | |
| 211 | scatterwalk_map_and_copy(buf + off, *sg, *offset, count, out); |
| 212 | |
| 213 | off += count; |
| 214 | buflen -= count; |
| 215 | *offset += count; |
| 216 | total -= count; |
| 217 | |
| 218 | if (*offset == (*sg)->length) { |
| 219 | *sg = sg_next(*sg); |
| 220 | if (*sg) |
| 221 | *offset = 0; |
| 222 | else |
| 223 | total = 0; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return off; |
| 228 | } |
| 229 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 230 | static inline u32 atmel_aes_read(struct atmel_aes_dev *dd, u32 offset) |
| 231 | { |
| 232 | return readl_relaxed(dd->io_base + offset); |
| 233 | } |
| 234 | |
| 235 | static inline void atmel_aes_write(struct atmel_aes_dev *dd, |
| 236 | u32 offset, u32 value) |
| 237 | { |
| 238 | writel_relaxed(value, dd->io_base + offset); |
| 239 | } |
| 240 | |
| 241 | static void atmel_aes_read_n(struct atmel_aes_dev *dd, u32 offset, |
| 242 | u32 *value, int count) |
| 243 | { |
| 244 | for (; count--; value++, offset += 4) |
| 245 | *value = atmel_aes_read(dd, offset); |
| 246 | } |
| 247 | |
| 248 | static void atmel_aes_write_n(struct atmel_aes_dev *dd, u32 offset, |
Cyrille Pitchen | c0b28d8 | 2015-12-17 17:48:33 +0100 | [diff] [blame] | 249 | const u32 *value, int count) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 250 | { |
| 251 | for (; count--; value++, offset += 4) |
| 252 | atmel_aes_write(dd, offset, *value); |
| 253 | } |
| 254 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 255 | static struct atmel_aes_dev *atmel_aes_find_dev(struct atmel_aes_base_ctx *ctx) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 256 | { |
| 257 | struct atmel_aes_dev *aes_dd = NULL; |
| 258 | struct atmel_aes_dev *tmp; |
| 259 | |
| 260 | spin_lock_bh(&atmel_aes.lock); |
| 261 | if (!ctx->dd) { |
| 262 | list_for_each_entry(tmp, &atmel_aes.dev_list, list) { |
| 263 | aes_dd = tmp; |
| 264 | break; |
| 265 | } |
| 266 | ctx->dd = aes_dd; |
| 267 | } else { |
| 268 | aes_dd = ctx->dd; |
| 269 | } |
| 270 | |
| 271 | spin_unlock_bh(&atmel_aes.lock); |
| 272 | |
| 273 | return aes_dd; |
| 274 | } |
| 275 | |
| 276 | static int atmel_aes_hw_init(struct atmel_aes_dev *dd) |
| 277 | { |
LABBE Corentin | 9d83d29 | 2015-10-02 14:12:58 +0200 | [diff] [blame] | 278 | int err; |
| 279 | |
| 280 | err = clk_prepare_enable(dd->iclk); |
| 281 | if (err) |
| 282 | return err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 283 | |
| 284 | if (!(dd->flags & AES_FLAGS_INIT)) { |
| 285 | atmel_aes_write(dd, AES_CR, AES_CR_SWRST); |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 286 | atmel_aes_write(dd, AES_MR, 0xE << AES_MR_CKEY_OFFSET); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 287 | dd->flags |= AES_FLAGS_INIT; |
| 288 | dd->err = 0; |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 294 | static inline unsigned int atmel_aes_get_version(struct atmel_aes_dev *dd) |
| 295 | { |
| 296 | return atmel_aes_read(dd, AES_HW_VERSION) & 0x00000fff; |
| 297 | } |
| 298 | |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 299 | static int atmel_aes_hw_version_init(struct atmel_aes_dev *dd) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 300 | { |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 301 | int err; |
| 302 | |
| 303 | err = atmel_aes_hw_init(dd); |
| 304 | if (err) |
| 305 | return err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 306 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 307 | dd->hw_version = atmel_aes_get_version(dd); |
| 308 | |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 309 | dev_info(dd->dev, "version: 0x%x\n", dd->hw_version); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 310 | |
| 311 | clk_disable_unprepare(dd->iclk); |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 312 | return 0; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 315 | static inline void atmel_aes_set_mode(struct atmel_aes_dev *dd, |
| 316 | const struct atmel_aes_reqctx *rctx) |
| 317 | { |
| 318 | /* Clear all but persistent flags and set request flags. */ |
| 319 | dd->flags = (dd->flags & AES_FLAGS_PERSISTENT) | rctx->mode; |
| 320 | } |
| 321 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 322 | static void atmel_aes_finish_req(struct atmel_aes_dev *dd, int err) |
| 323 | { |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 324 | struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 325 | |
| 326 | clk_disable_unprepare(dd->iclk); |
| 327 | dd->flags &= ~AES_FLAGS_BUSY; |
| 328 | |
| 329 | req->base.complete(&req->base, err); |
| 330 | } |
| 331 | |
| 332 | static void atmel_aes_dma_callback(void *data) |
| 333 | { |
| 334 | struct atmel_aes_dev *dd = data; |
| 335 | |
| 336 | /* dma_lch_out - completed */ |
| 337 | tasklet_schedule(&dd->done_task); |
| 338 | } |
| 339 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 340 | static int atmel_aes_crypt_dma(struct atmel_aes_dev *dd, |
| 341 | dma_addr_t dma_addr_in, dma_addr_t dma_addr_out, int length) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 342 | { |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 343 | struct scatterlist sg[2]; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 344 | struct dma_async_tx_descriptor *in_desc, *out_desc; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 345 | enum dma_slave_buswidth addr_width; |
| 346 | u32 maxburst; |
| 347 | |
| 348 | switch (dd->ctx->block_size) { |
| 349 | case CFB8_BLOCK_SIZE: |
| 350 | addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 351 | maxburst = 1; |
| 352 | break; |
| 353 | |
| 354 | case CFB16_BLOCK_SIZE: |
| 355 | addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 356 | maxburst = 1; |
| 357 | break; |
| 358 | |
| 359 | case CFB32_BLOCK_SIZE: |
| 360 | case CFB64_BLOCK_SIZE: |
| 361 | addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 362 | maxburst = 1; |
| 363 | break; |
| 364 | |
| 365 | case AES_BLOCK_SIZE: |
| 366 | addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 367 | maxburst = dd->caps.max_burst_size; |
| 368 | break; |
| 369 | |
| 370 | default: |
| 371 | return -EINVAL; |
| 372 | } |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 373 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 374 | dd->dma_size = length; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 375 | |
Leilei Zhao | 289b262 | 2015-04-07 17:45:10 +0800 | [diff] [blame] | 376 | dma_sync_single_for_device(dd->dev, dma_addr_in, length, |
| 377 | DMA_TO_DEVICE); |
| 378 | dma_sync_single_for_device(dd->dev, dma_addr_out, length, |
| 379 | DMA_FROM_DEVICE); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 380 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 381 | dd->dma_lch_in.dma_conf.dst_addr_width = addr_width; |
| 382 | dd->dma_lch_in.dma_conf.src_maxburst = maxburst; |
| 383 | dd->dma_lch_in.dma_conf.dst_maxburst = maxburst; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 384 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 385 | dd->dma_lch_out.dma_conf.src_addr_width = addr_width; |
| 386 | dd->dma_lch_out.dma_conf.src_maxburst = maxburst; |
| 387 | dd->dma_lch_out.dma_conf.dst_maxburst = maxburst; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 388 | |
| 389 | dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf); |
| 390 | dmaengine_slave_config(dd->dma_lch_out.chan, &dd->dma_lch_out.dma_conf); |
| 391 | |
| 392 | dd->flags |= AES_FLAGS_DMA; |
| 393 | |
| 394 | sg_init_table(&sg[0], 1); |
| 395 | sg_dma_address(&sg[0]) = dma_addr_in; |
| 396 | sg_dma_len(&sg[0]) = length; |
| 397 | |
| 398 | sg_init_table(&sg[1], 1); |
| 399 | sg_dma_address(&sg[1]) = dma_addr_out; |
| 400 | sg_dma_len(&sg[1]) = length; |
| 401 | |
| 402 | in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, &sg[0], |
| 403 | 1, DMA_MEM_TO_DEV, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 404 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 405 | if (!in_desc) |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 406 | return -EINVAL; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 407 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 408 | out_desc = dmaengine_prep_slave_sg(dd->dma_lch_out.chan, &sg[1], |
| 409 | 1, DMA_DEV_TO_MEM, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 410 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 411 | if (!out_desc) |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 412 | return -EINVAL; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 413 | |
| 414 | out_desc->callback = atmel_aes_dma_callback; |
| 415 | out_desc->callback_param = dd; |
| 416 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 417 | dmaengine_submit(out_desc); |
| 418 | dma_async_issue_pending(dd->dma_lch_out.chan); |
| 419 | |
| 420 | dmaengine_submit(in_desc); |
| 421 | dma_async_issue_pending(dd->dma_lch_in.chan); |
| 422 | |
| 423 | return 0; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | static int atmel_aes_crypt_cpu_start(struct atmel_aes_dev *dd) |
| 427 | { |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 428 | struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq); |
| 429 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 430 | dd->flags &= ~AES_FLAGS_DMA; |
| 431 | |
Leilei Zhao | 289b262 | 2015-04-07 17:45:10 +0800 | [diff] [blame] | 432 | dma_sync_single_for_cpu(dd->dev, dd->dma_addr_in, |
| 433 | dd->dma_size, DMA_TO_DEVICE); |
| 434 | dma_sync_single_for_cpu(dd->dev, dd->dma_addr_out, |
| 435 | dd->dma_size, DMA_FROM_DEVICE); |
| 436 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 437 | /* use cache buffers */ |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 438 | dd->nb_in_sg = atmel_aes_sg_length(req, dd->in_sg); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 439 | if (!dd->nb_in_sg) |
| 440 | return -EINVAL; |
| 441 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 442 | dd->nb_out_sg = atmel_aes_sg_length(req, dd->out_sg); |
Julia Lawall | 7b5c253c | 2013-01-21 14:02:51 +0100 | [diff] [blame] | 443 | if (!dd->nb_out_sg) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 444 | return -EINVAL; |
| 445 | |
| 446 | dd->bufcnt = sg_copy_to_buffer(dd->in_sg, dd->nb_in_sg, |
| 447 | dd->buf_in, dd->total); |
| 448 | |
| 449 | if (!dd->bufcnt) |
| 450 | return -EINVAL; |
| 451 | |
| 452 | dd->total -= dd->bufcnt; |
| 453 | |
| 454 | atmel_aes_write(dd, AES_IER, AES_INT_DATARDY); |
| 455 | atmel_aes_write_n(dd, AES_IDATAR(0), (u32 *) dd->buf_in, |
| 456 | dd->bufcnt >> 2); |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | static int atmel_aes_crypt_dma_start(struct atmel_aes_dev *dd) |
| 462 | { |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 463 | int err, fast = 0, in, out; |
| 464 | size_t count; |
| 465 | dma_addr_t addr_in, addr_out; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 466 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 467 | if ((!dd->in_offset) && (!dd->out_offset)) { |
| 468 | /* check for alignment */ |
| 469 | in = IS_ALIGNED((u32)dd->in_sg->offset, sizeof(u32)) && |
| 470 | IS_ALIGNED(dd->in_sg->length, dd->ctx->block_size); |
| 471 | out = IS_ALIGNED((u32)dd->out_sg->offset, sizeof(u32)) && |
| 472 | IS_ALIGNED(dd->out_sg->length, dd->ctx->block_size); |
| 473 | fast = in && out; |
| 474 | |
| 475 | if (sg_dma_len(dd->in_sg) != sg_dma_len(dd->out_sg)) |
| 476 | fast = 0; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 479 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 480 | if (fast) { |
Arnd Bergmann | 20ecae7 | 2015-11-17 10:22:06 +0100 | [diff] [blame] | 481 | count = min_t(size_t, dd->total, sg_dma_len(dd->in_sg)); |
| 482 | count = min_t(size_t, count, sg_dma_len(dd->out_sg)); |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 483 | |
| 484 | err = dma_map_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE); |
| 485 | if (!err) { |
| 486 | dev_err(dd->dev, "dma_map_sg() error\n"); |
| 487 | return -EINVAL; |
| 488 | } |
| 489 | |
| 490 | err = dma_map_sg(dd->dev, dd->out_sg, 1, |
| 491 | DMA_FROM_DEVICE); |
| 492 | if (!err) { |
| 493 | dev_err(dd->dev, "dma_map_sg() error\n"); |
| 494 | dma_unmap_sg(dd->dev, dd->in_sg, 1, |
| 495 | DMA_TO_DEVICE); |
| 496 | return -EINVAL; |
| 497 | } |
| 498 | |
| 499 | addr_in = sg_dma_address(dd->in_sg); |
| 500 | addr_out = sg_dma_address(dd->out_sg); |
| 501 | |
| 502 | dd->flags |= AES_FLAGS_FAST; |
| 503 | |
| 504 | } else { |
Leilei Zhao | 289b262 | 2015-04-07 17:45:10 +0800 | [diff] [blame] | 505 | dma_sync_single_for_cpu(dd->dev, dd->dma_addr_in, |
| 506 | dd->dma_size, DMA_TO_DEVICE); |
| 507 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 508 | /* use cache buffers */ |
| 509 | count = atmel_aes_sg_copy(&dd->in_sg, &dd->in_offset, |
| 510 | dd->buf_in, dd->buflen, dd->total, 0); |
| 511 | |
| 512 | addr_in = dd->dma_addr_in; |
| 513 | addr_out = dd->dma_addr_out; |
| 514 | |
| 515 | dd->flags &= ~AES_FLAGS_FAST; |
| 516 | } |
| 517 | |
| 518 | dd->total -= count; |
| 519 | |
| 520 | err = atmel_aes_crypt_dma(dd, addr_in, addr_out, count); |
| 521 | |
| 522 | if (err && (dd->flags & AES_FLAGS_FAST)) { |
| 523 | dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE); |
| 524 | dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_TO_DEVICE); |
| 525 | } |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 526 | |
| 527 | return err; |
| 528 | } |
| 529 | |
Cyrille Pitchen | cdfab4a | 2015-12-17 17:48:38 +0100 | [diff] [blame] | 530 | static void atmel_aes_write_ctrl(struct atmel_aes_dev *dd, bool use_dma, |
| 531 | const u32 *iv) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 532 | { |
Cyrille Pitchen | 794595d | 2015-12-17 17:48:40 +0100 | [diff] [blame] | 533 | u32 valmr = 0; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 534 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 535 | /* MR register must be set before IV registers */ |
| 536 | if (dd->ctx->keylen == AES_KEYSIZE_128) |
| 537 | valmr |= AES_MR_KEYSIZE_128; |
| 538 | else if (dd->ctx->keylen == AES_KEYSIZE_192) |
| 539 | valmr |= AES_MR_KEYSIZE_192; |
| 540 | else |
| 541 | valmr |= AES_MR_KEYSIZE_256; |
| 542 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 543 | valmr |= dd->flags & AES_FLAGS_MODE_MASK; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 544 | |
Cyrille Pitchen | cdfab4a | 2015-12-17 17:48:38 +0100 | [diff] [blame] | 545 | if (use_dma) { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 546 | valmr |= AES_MR_SMOD_IDATAR0; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 547 | if (dd->caps.has_dualbuff) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 548 | valmr |= AES_MR_DUALBUFF; |
| 549 | } else { |
| 550 | valmr |= AES_MR_SMOD_AUTO; |
| 551 | } |
| 552 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 553 | atmel_aes_write(dd, AES_MR, valmr); |
| 554 | |
| 555 | atmel_aes_write_n(dd, AES_KEYWR(0), dd->ctx->key, |
| 556 | dd->ctx->keylen >> 2); |
| 557 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 558 | if (iv && (valmr & AES_MR_OPMOD_MASK) != AES_MR_OPMOD_ECB) |
Cyrille Pitchen | cdfab4a | 2015-12-17 17:48:38 +0100 | [diff] [blame] | 559 | atmel_aes_write_n(dd, AES_IVR(0), iv, 4); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | static int atmel_aes_handle_queue(struct atmel_aes_dev *dd, |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 563 | struct crypto_async_request *new_areq) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 564 | { |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 565 | struct crypto_async_request *areq, *backlog; |
| 566 | struct atmel_aes_base_ctx *ctx; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 567 | unsigned long flags; |
| 568 | int err, ret = 0; |
| 569 | |
| 570 | spin_lock_irqsave(&dd->lock, flags); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 571 | if (new_areq) |
| 572 | ret = crypto_enqueue_request(&dd->queue, new_areq); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 573 | if (dd->flags & AES_FLAGS_BUSY) { |
| 574 | spin_unlock_irqrestore(&dd->lock, flags); |
| 575 | return ret; |
| 576 | } |
| 577 | backlog = crypto_get_backlog(&dd->queue); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 578 | areq = crypto_dequeue_request(&dd->queue); |
| 579 | if (areq) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 580 | dd->flags |= AES_FLAGS_BUSY; |
| 581 | spin_unlock_irqrestore(&dd->lock, flags); |
| 582 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 583 | if (!areq) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 584 | return ret; |
| 585 | |
| 586 | if (backlog) |
| 587 | backlog->complete(backlog, -EINPROGRESS); |
| 588 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 589 | ctx = crypto_tfm_ctx(areq->tfm); |
| 590 | |
| 591 | dd->areq = areq; |
| 592 | dd->ctx = ctx; |
| 593 | |
| 594 | err = ctx->start(dd); |
| 595 | return (areq != new_areq) ? ret : err; |
| 596 | } |
| 597 | |
| 598 | static int atmel_aes_start(struct atmel_aes_dev *dd) |
| 599 | { |
| 600 | struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq); |
| 601 | struct atmel_aes_reqctx *rctx; |
| 602 | bool use_dma; |
| 603 | int err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 604 | |
| 605 | /* assign new request to device */ |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 606 | dd->total = req->nbytes; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 607 | dd->in_offset = 0; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 608 | dd->in_sg = req->src; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 609 | dd->out_offset = 0; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 610 | dd->out_sg = req->dst; |
| 611 | |
| 612 | rctx = ablkcipher_request_ctx(req); |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 613 | atmel_aes_set_mode(dd, rctx); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 614 | |
Cyrille Pitchen | cdfab4a | 2015-12-17 17:48:38 +0100 | [diff] [blame] | 615 | err = atmel_aes_hw_init(dd); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 616 | if (!err) { |
Cyrille Pitchen | cdfab4a | 2015-12-17 17:48:38 +0100 | [diff] [blame] | 617 | use_dma = (dd->total > ATMEL_AES_DMA_THRESHOLD); |
| 618 | atmel_aes_write_ctrl(dd, use_dma, req->info); |
| 619 | if (use_dma) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 620 | err = atmel_aes_crypt_dma_start(dd); |
| 621 | else |
| 622 | err = atmel_aes_crypt_cpu_start(dd); |
| 623 | } |
| 624 | if (err) { |
| 625 | /* aes_task will not finish it, so do it here */ |
| 626 | atmel_aes_finish_req(dd, err); |
| 627 | tasklet_schedule(&dd->queue_task); |
| 628 | } |
| 629 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 630 | return -EINPROGRESS; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | static int atmel_aes_crypt_dma_stop(struct atmel_aes_dev *dd) |
| 634 | { |
| 635 | int err = -EINVAL; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 636 | size_t count; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 637 | |
| 638 | if (dd->flags & AES_FLAGS_DMA) { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 639 | err = 0; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 640 | if (dd->flags & AES_FLAGS_FAST) { |
| 641 | dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_FROM_DEVICE); |
| 642 | dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE); |
| 643 | } else { |
Leilei Zhao | 9cd2232 | 2015-04-07 17:45:11 +0800 | [diff] [blame] | 644 | dma_sync_single_for_cpu(dd->dev, dd->dma_addr_out, |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 645 | dd->dma_size, DMA_FROM_DEVICE); |
| 646 | |
| 647 | /* copy data */ |
| 648 | count = atmel_aes_sg_copy(&dd->out_sg, &dd->out_offset, |
| 649 | dd->buf_out, dd->buflen, dd->dma_size, 1); |
| 650 | if (count != dd->dma_size) { |
| 651 | err = -EINVAL; |
Arnd Bergmann | 20ecae7 | 2015-11-17 10:22:06 +0100 | [diff] [blame] | 652 | pr_err("not all data converted: %zu\n", count); |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 653 | } |
| 654 | } |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | return err; |
| 658 | } |
| 659 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 660 | |
| 661 | static int atmel_aes_buff_init(struct atmel_aes_dev *dd) |
| 662 | { |
| 663 | int err = -ENOMEM; |
| 664 | |
| 665 | dd->buf_in = (void *)__get_free_pages(GFP_KERNEL, 0); |
| 666 | dd->buf_out = (void *)__get_free_pages(GFP_KERNEL, 0); |
| 667 | dd->buflen = PAGE_SIZE; |
| 668 | dd->buflen &= ~(AES_BLOCK_SIZE - 1); |
| 669 | |
| 670 | if (!dd->buf_in || !dd->buf_out) { |
| 671 | dev_err(dd->dev, "unable to alloc pages.\n"); |
| 672 | goto err_alloc; |
| 673 | } |
| 674 | |
| 675 | /* MAP here */ |
| 676 | dd->dma_addr_in = dma_map_single(dd->dev, dd->buf_in, |
| 677 | dd->buflen, DMA_TO_DEVICE); |
| 678 | if (dma_mapping_error(dd->dev, dd->dma_addr_in)) { |
Arnd Bergmann | 20ecae7 | 2015-11-17 10:22:06 +0100 | [diff] [blame] | 679 | dev_err(dd->dev, "dma %zd bytes error\n", dd->buflen); |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 680 | err = -EINVAL; |
| 681 | goto err_map_in; |
| 682 | } |
| 683 | |
| 684 | dd->dma_addr_out = dma_map_single(dd->dev, dd->buf_out, |
| 685 | dd->buflen, DMA_FROM_DEVICE); |
| 686 | if (dma_mapping_error(dd->dev, dd->dma_addr_out)) { |
Arnd Bergmann | 20ecae7 | 2015-11-17 10:22:06 +0100 | [diff] [blame] | 687 | dev_err(dd->dev, "dma %zd bytes error\n", dd->buflen); |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 688 | err = -EINVAL; |
| 689 | goto err_map_out; |
| 690 | } |
| 691 | |
| 692 | return 0; |
| 693 | |
| 694 | err_map_out: |
| 695 | dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen, |
| 696 | DMA_TO_DEVICE); |
| 697 | err_map_in: |
Christophe Jaillet | 088f628 | 2015-01-20 08:15:52 +0100 | [diff] [blame] | 698 | err_alloc: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 699 | free_page((unsigned long)dd->buf_out); |
| 700 | free_page((unsigned long)dd->buf_in); |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 701 | if (err) |
| 702 | pr_err("error: %d\n", err); |
| 703 | return err; |
| 704 | } |
| 705 | |
| 706 | static void atmel_aes_buff_cleanup(struct atmel_aes_dev *dd) |
| 707 | { |
| 708 | dma_unmap_single(dd->dev, dd->dma_addr_out, dd->buflen, |
| 709 | DMA_FROM_DEVICE); |
| 710 | dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen, |
| 711 | DMA_TO_DEVICE); |
| 712 | free_page((unsigned long)dd->buf_out); |
| 713 | free_page((unsigned long)dd->buf_in); |
| 714 | } |
| 715 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 716 | static int atmel_aes_crypt(struct ablkcipher_request *req, unsigned long mode) |
| 717 | { |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 718 | struct atmel_aes_base_ctx *ctx = crypto_ablkcipher_ctx( |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 719 | crypto_ablkcipher_reqtfm(req)); |
| 720 | struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req); |
| 721 | struct atmel_aes_dev *dd; |
| 722 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 723 | switch (mode & AES_FLAGS_OPMODE_MASK) { |
| 724 | case AES_FLAGS_CFB8: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 725 | ctx->block_size = CFB8_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 726 | break; |
| 727 | |
| 728 | case AES_FLAGS_CFB16: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 729 | ctx->block_size = CFB16_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 730 | break; |
| 731 | |
| 732 | case AES_FLAGS_CFB32: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 733 | ctx->block_size = CFB32_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 734 | break; |
| 735 | |
| 736 | case AES_FLAGS_CFB64: |
Leilei Zhao | 9f84951 | 2014-04-22 15:23:24 +0800 | [diff] [blame] | 737 | ctx->block_size = CFB64_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 738 | break; |
| 739 | |
| 740 | default: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 741 | ctx->block_size = AES_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 742 | break; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | dd = atmel_aes_find_dev(ctx); |
| 746 | if (!dd) |
| 747 | return -ENODEV; |
| 748 | |
| 749 | rctx->mode = mode; |
| 750 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 751 | return atmel_aes_handle_queue(dd, &req->base); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | static bool atmel_aes_filter(struct dma_chan *chan, void *slave) |
| 755 | { |
| 756 | struct at_dma_slave *sl = slave; |
| 757 | |
| 758 | if (sl && sl->dma_dev == chan->device->dev) { |
| 759 | chan->private = sl; |
| 760 | return true; |
| 761 | } else { |
| 762 | return false; |
| 763 | } |
| 764 | } |
| 765 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 766 | static int atmel_aes_dma_init(struct atmel_aes_dev *dd, |
| 767 | struct crypto_platform_data *pdata) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 768 | { |
| 769 | int err = -ENOMEM; |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 770 | dma_cap_mask_t mask; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 771 | |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 772 | dma_cap_zero(mask); |
| 773 | dma_cap_set(DMA_SLAVE, mask); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 774 | |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 775 | /* Try to grab 2 DMA channels */ |
| 776 | dd->dma_lch_in.chan = dma_request_slave_channel_compat(mask, |
| 777 | atmel_aes_filter, &pdata->dma_slave->rxdata, dd->dev, "tx"); |
| 778 | if (!dd->dma_lch_in.chan) |
| 779 | goto err_dma_in; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 780 | |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 781 | dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV; |
| 782 | dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base + |
| 783 | AES_IDATAR(0); |
| 784 | dd->dma_lch_in.dma_conf.src_maxburst = dd->caps.max_burst_size; |
| 785 | dd->dma_lch_in.dma_conf.src_addr_width = |
| 786 | DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 787 | dd->dma_lch_in.dma_conf.dst_maxburst = dd->caps.max_burst_size; |
| 788 | dd->dma_lch_in.dma_conf.dst_addr_width = |
| 789 | DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 790 | dd->dma_lch_in.dma_conf.device_fc = false; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 791 | |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 792 | dd->dma_lch_out.chan = dma_request_slave_channel_compat(mask, |
| 793 | atmel_aes_filter, &pdata->dma_slave->txdata, dd->dev, "rx"); |
| 794 | if (!dd->dma_lch_out.chan) |
| 795 | goto err_dma_out; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 796 | |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 797 | dd->dma_lch_out.dma_conf.direction = DMA_DEV_TO_MEM; |
| 798 | dd->dma_lch_out.dma_conf.src_addr = dd->phys_base + |
| 799 | AES_ODATAR(0); |
| 800 | dd->dma_lch_out.dma_conf.src_maxburst = dd->caps.max_burst_size; |
| 801 | dd->dma_lch_out.dma_conf.src_addr_width = |
| 802 | DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 803 | dd->dma_lch_out.dma_conf.dst_maxburst = dd->caps.max_burst_size; |
| 804 | dd->dma_lch_out.dma_conf.dst_addr_width = |
| 805 | DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 806 | dd->dma_lch_out.dma_conf.device_fc = false; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 807 | |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 808 | return 0; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 809 | |
| 810 | err_dma_out: |
| 811 | dma_release_channel(dd->dma_lch_in.chan); |
| 812 | err_dma_in: |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 813 | dev_warn(dd->dev, "no DMA channel available\n"); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 814 | return err; |
| 815 | } |
| 816 | |
| 817 | static void atmel_aes_dma_cleanup(struct atmel_aes_dev *dd) |
| 818 | { |
| 819 | dma_release_channel(dd->dma_lch_in.chan); |
| 820 | dma_release_channel(dd->dma_lch_out.chan); |
| 821 | } |
| 822 | |
| 823 | static int atmel_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key, |
| 824 | unsigned int keylen) |
| 825 | { |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 826 | struct atmel_aes_base_ctx *ctx = crypto_ablkcipher_ctx(tfm); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 827 | |
| 828 | if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && |
| 829 | keylen != AES_KEYSIZE_256) { |
| 830 | crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 831 | return -EINVAL; |
| 832 | } |
| 833 | |
| 834 | memcpy(ctx->key, key, keylen); |
| 835 | ctx->keylen = keylen; |
| 836 | |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | static int atmel_aes_ecb_encrypt(struct ablkcipher_request *req) |
| 841 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 842 | return atmel_aes_crypt(req, AES_FLAGS_ECB | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | static int atmel_aes_ecb_decrypt(struct ablkcipher_request *req) |
| 846 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 847 | return atmel_aes_crypt(req, AES_FLAGS_ECB); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | static int atmel_aes_cbc_encrypt(struct ablkcipher_request *req) |
| 851 | { |
| 852 | return atmel_aes_crypt(req, |
| 853 | AES_FLAGS_ENCRYPT | AES_FLAGS_CBC); |
| 854 | } |
| 855 | |
| 856 | static int atmel_aes_cbc_decrypt(struct ablkcipher_request *req) |
| 857 | { |
| 858 | return atmel_aes_crypt(req, |
| 859 | AES_FLAGS_CBC); |
| 860 | } |
| 861 | |
| 862 | static int atmel_aes_ofb_encrypt(struct ablkcipher_request *req) |
| 863 | { |
| 864 | return atmel_aes_crypt(req, |
| 865 | AES_FLAGS_ENCRYPT | AES_FLAGS_OFB); |
| 866 | } |
| 867 | |
| 868 | static int atmel_aes_ofb_decrypt(struct ablkcipher_request *req) |
| 869 | { |
| 870 | return atmel_aes_crypt(req, |
| 871 | AES_FLAGS_OFB); |
| 872 | } |
| 873 | |
| 874 | static int atmel_aes_cfb_encrypt(struct ablkcipher_request *req) |
| 875 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 876 | return atmel_aes_crypt(req, AES_FLAGS_CFB128 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | static int atmel_aes_cfb_decrypt(struct ablkcipher_request *req) |
| 880 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 881 | return atmel_aes_crypt(req, AES_FLAGS_CFB128); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | static int atmel_aes_cfb64_encrypt(struct ablkcipher_request *req) |
| 885 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 886 | return atmel_aes_crypt(req, AES_FLAGS_CFB64 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | static int atmel_aes_cfb64_decrypt(struct ablkcipher_request *req) |
| 890 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 891 | return atmel_aes_crypt(req, AES_FLAGS_CFB64); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | static int atmel_aes_cfb32_encrypt(struct ablkcipher_request *req) |
| 895 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 896 | return atmel_aes_crypt(req, AES_FLAGS_CFB32 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | static int atmel_aes_cfb32_decrypt(struct ablkcipher_request *req) |
| 900 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 901 | return atmel_aes_crypt(req, AES_FLAGS_CFB32); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | static int atmel_aes_cfb16_encrypt(struct ablkcipher_request *req) |
| 905 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 906 | return atmel_aes_crypt(req, AES_FLAGS_CFB16 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | static int atmel_aes_cfb16_decrypt(struct ablkcipher_request *req) |
| 910 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 911 | return atmel_aes_crypt(req, AES_FLAGS_CFB16); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | static int atmel_aes_cfb8_encrypt(struct ablkcipher_request *req) |
| 915 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 916 | return atmel_aes_crypt(req, AES_FLAGS_CFB8 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | static int atmel_aes_cfb8_decrypt(struct ablkcipher_request *req) |
| 920 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame^] | 921 | return atmel_aes_crypt(req, AES_FLAGS_CFB8); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | static int atmel_aes_ctr_encrypt(struct ablkcipher_request *req) |
| 925 | { |
| 926 | return atmel_aes_crypt(req, |
| 927 | AES_FLAGS_ENCRYPT | AES_FLAGS_CTR); |
| 928 | } |
| 929 | |
| 930 | static int atmel_aes_ctr_decrypt(struct ablkcipher_request *req) |
| 931 | { |
| 932 | return atmel_aes_crypt(req, |
| 933 | AES_FLAGS_CTR); |
| 934 | } |
| 935 | |
| 936 | static int atmel_aes_cra_init(struct crypto_tfm *tfm) |
| 937 | { |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 938 | struct atmel_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
| 939 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 940 | tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 941 | ctx->base.start = atmel_aes_start; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 942 | |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | static void atmel_aes_cra_exit(struct crypto_tfm *tfm) |
| 947 | { |
| 948 | } |
| 949 | |
| 950 | static struct crypto_alg aes_algs[] = { |
| 951 | { |
| 952 | .cra_name = "ecb(aes)", |
| 953 | .cra_driver_name = "atmel-ecb-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 954 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 955 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 956 | .cra_blocksize = AES_BLOCK_SIZE, |
| 957 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 958 | .cra_alignmask = 0xf, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 959 | .cra_type = &crypto_ablkcipher_type, |
| 960 | .cra_module = THIS_MODULE, |
| 961 | .cra_init = atmel_aes_cra_init, |
| 962 | .cra_exit = atmel_aes_cra_exit, |
| 963 | .cra_u.ablkcipher = { |
| 964 | .min_keysize = AES_MIN_KEY_SIZE, |
| 965 | .max_keysize = AES_MAX_KEY_SIZE, |
| 966 | .setkey = atmel_aes_setkey, |
| 967 | .encrypt = atmel_aes_ecb_encrypt, |
| 968 | .decrypt = atmel_aes_ecb_decrypt, |
| 969 | } |
| 970 | }, |
| 971 | { |
| 972 | .cra_name = "cbc(aes)", |
| 973 | .cra_driver_name = "atmel-cbc-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 974 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 975 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 976 | .cra_blocksize = AES_BLOCK_SIZE, |
| 977 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 978 | .cra_alignmask = 0xf, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 979 | .cra_type = &crypto_ablkcipher_type, |
| 980 | .cra_module = THIS_MODULE, |
| 981 | .cra_init = atmel_aes_cra_init, |
| 982 | .cra_exit = atmel_aes_cra_exit, |
| 983 | .cra_u.ablkcipher = { |
| 984 | .min_keysize = AES_MIN_KEY_SIZE, |
| 985 | .max_keysize = AES_MAX_KEY_SIZE, |
| 986 | .ivsize = AES_BLOCK_SIZE, |
| 987 | .setkey = atmel_aes_setkey, |
| 988 | .encrypt = atmel_aes_cbc_encrypt, |
| 989 | .decrypt = atmel_aes_cbc_decrypt, |
| 990 | } |
| 991 | }, |
| 992 | { |
| 993 | .cra_name = "ofb(aes)", |
| 994 | .cra_driver_name = "atmel-ofb-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 995 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 996 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 997 | .cra_blocksize = AES_BLOCK_SIZE, |
| 998 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 999 | .cra_alignmask = 0xf, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1000 | .cra_type = &crypto_ablkcipher_type, |
| 1001 | .cra_module = THIS_MODULE, |
| 1002 | .cra_init = atmel_aes_cra_init, |
| 1003 | .cra_exit = atmel_aes_cra_exit, |
| 1004 | .cra_u.ablkcipher = { |
| 1005 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1006 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1007 | .ivsize = AES_BLOCK_SIZE, |
| 1008 | .setkey = atmel_aes_setkey, |
| 1009 | .encrypt = atmel_aes_ofb_encrypt, |
| 1010 | .decrypt = atmel_aes_ofb_decrypt, |
| 1011 | } |
| 1012 | }, |
| 1013 | { |
| 1014 | .cra_name = "cfb(aes)", |
| 1015 | .cra_driver_name = "atmel-cfb-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1016 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1017 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1018 | .cra_blocksize = AES_BLOCK_SIZE, |
| 1019 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1020 | .cra_alignmask = 0xf, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1021 | .cra_type = &crypto_ablkcipher_type, |
| 1022 | .cra_module = THIS_MODULE, |
| 1023 | .cra_init = atmel_aes_cra_init, |
| 1024 | .cra_exit = atmel_aes_cra_exit, |
| 1025 | .cra_u.ablkcipher = { |
| 1026 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1027 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1028 | .ivsize = AES_BLOCK_SIZE, |
| 1029 | .setkey = atmel_aes_setkey, |
| 1030 | .encrypt = atmel_aes_cfb_encrypt, |
| 1031 | .decrypt = atmel_aes_cfb_decrypt, |
| 1032 | } |
| 1033 | }, |
| 1034 | { |
| 1035 | .cra_name = "cfb32(aes)", |
| 1036 | .cra_driver_name = "atmel-cfb32-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1037 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1038 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1039 | .cra_blocksize = CFB32_BLOCK_SIZE, |
| 1040 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1041 | .cra_alignmask = 0x3, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1042 | .cra_type = &crypto_ablkcipher_type, |
| 1043 | .cra_module = THIS_MODULE, |
| 1044 | .cra_init = atmel_aes_cra_init, |
| 1045 | .cra_exit = atmel_aes_cra_exit, |
| 1046 | .cra_u.ablkcipher = { |
| 1047 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1048 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1049 | .ivsize = AES_BLOCK_SIZE, |
| 1050 | .setkey = atmel_aes_setkey, |
| 1051 | .encrypt = atmel_aes_cfb32_encrypt, |
| 1052 | .decrypt = atmel_aes_cfb32_decrypt, |
| 1053 | } |
| 1054 | }, |
| 1055 | { |
| 1056 | .cra_name = "cfb16(aes)", |
| 1057 | .cra_driver_name = "atmel-cfb16-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1058 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1059 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1060 | .cra_blocksize = CFB16_BLOCK_SIZE, |
| 1061 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1062 | .cra_alignmask = 0x1, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1063 | .cra_type = &crypto_ablkcipher_type, |
| 1064 | .cra_module = THIS_MODULE, |
| 1065 | .cra_init = atmel_aes_cra_init, |
| 1066 | .cra_exit = atmel_aes_cra_exit, |
| 1067 | .cra_u.ablkcipher = { |
| 1068 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1069 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1070 | .ivsize = AES_BLOCK_SIZE, |
| 1071 | .setkey = atmel_aes_setkey, |
| 1072 | .encrypt = atmel_aes_cfb16_encrypt, |
| 1073 | .decrypt = atmel_aes_cfb16_decrypt, |
| 1074 | } |
| 1075 | }, |
| 1076 | { |
| 1077 | .cra_name = "cfb8(aes)", |
| 1078 | .cra_driver_name = "atmel-cfb8-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1079 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1080 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
Leilei Zhao | e5d8c96 | 2014-04-22 15:23:23 +0800 | [diff] [blame] | 1081 | .cra_blocksize = CFB8_BLOCK_SIZE, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1082 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
| 1083 | .cra_alignmask = 0x0, |
| 1084 | .cra_type = &crypto_ablkcipher_type, |
| 1085 | .cra_module = THIS_MODULE, |
| 1086 | .cra_init = atmel_aes_cra_init, |
| 1087 | .cra_exit = atmel_aes_cra_exit, |
| 1088 | .cra_u.ablkcipher = { |
| 1089 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1090 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1091 | .ivsize = AES_BLOCK_SIZE, |
| 1092 | .setkey = atmel_aes_setkey, |
| 1093 | .encrypt = atmel_aes_cfb8_encrypt, |
| 1094 | .decrypt = atmel_aes_cfb8_decrypt, |
| 1095 | } |
| 1096 | }, |
| 1097 | { |
| 1098 | .cra_name = "ctr(aes)", |
| 1099 | .cra_driver_name = "atmel-ctr-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1100 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1101 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1102 | .cra_blocksize = AES_BLOCK_SIZE, |
| 1103 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1104 | .cra_alignmask = 0xf, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1105 | .cra_type = &crypto_ablkcipher_type, |
| 1106 | .cra_module = THIS_MODULE, |
| 1107 | .cra_init = atmel_aes_cra_init, |
| 1108 | .cra_exit = atmel_aes_cra_exit, |
| 1109 | .cra_u.ablkcipher = { |
| 1110 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1111 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1112 | .ivsize = AES_BLOCK_SIZE, |
| 1113 | .setkey = atmel_aes_setkey, |
| 1114 | .encrypt = atmel_aes_ctr_encrypt, |
| 1115 | .decrypt = atmel_aes_ctr_decrypt, |
| 1116 | } |
| 1117 | }, |
| 1118 | }; |
| 1119 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1120 | static struct crypto_alg aes_cfb64_alg = { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1121 | .cra_name = "cfb64(aes)", |
| 1122 | .cra_driver_name = "atmel-cfb64-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1123 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1124 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1125 | .cra_blocksize = CFB64_BLOCK_SIZE, |
| 1126 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1127 | .cra_alignmask = 0x7, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1128 | .cra_type = &crypto_ablkcipher_type, |
| 1129 | .cra_module = THIS_MODULE, |
| 1130 | .cra_init = atmel_aes_cra_init, |
| 1131 | .cra_exit = atmel_aes_cra_exit, |
| 1132 | .cra_u.ablkcipher = { |
| 1133 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1134 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1135 | .ivsize = AES_BLOCK_SIZE, |
| 1136 | .setkey = atmel_aes_setkey, |
| 1137 | .encrypt = atmel_aes_cfb64_encrypt, |
| 1138 | .decrypt = atmel_aes_cfb64_decrypt, |
| 1139 | } |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1140 | }; |
| 1141 | |
| 1142 | static void atmel_aes_queue_task(unsigned long data) |
| 1143 | { |
| 1144 | struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data; |
| 1145 | |
| 1146 | atmel_aes_handle_queue(dd, NULL); |
| 1147 | } |
| 1148 | |
| 1149 | static void atmel_aes_done_task(unsigned long data) |
| 1150 | { |
| 1151 | struct atmel_aes_dev *dd = (struct atmel_aes_dev *) data; |
| 1152 | int err; |
| 1153 | |
| 1154 | if (!(dd->flags & AES_FLAGS_DMA)) { |
| 1155 | atmel_aes_read_n(dd, AES_ODATAR(0), (u32 *) dd->buf_out, |
| 1156 | dd->bufcnt >> 2); |
| 1157 | |
| 1158 | if (sg_copy_from_buffer(dd->out_sg, dd->nb_out_sg, |
| 1159 | dd->buf_out, dd->bufcnt)) |
| 1160 | err = 0; |
| 1161 | else |
| 1162 | err = -EINVAL; |
| 1163 | |
| 1164 | goto cpu_end; |
| 1165 | } |
| 1166 | |
| 1167 | err = atmel_aes_crypt_dma_stop(dd); |
| 1168 | |
| 1169 | err = dd->err ? : err; |
| 1170 | |
| 1171 | if (dd->total && !err) { |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1172 | if (dd->flags & AES_FLAGS_FAST) { |
| 1173 | dd->in_sg = sg_next(dd->in_sg); |
| 1174 | dd->out_sg = sg_next(dd->out_sg); |
| 1175 | if (!dd->in_sg || !dd->out_sg) |
| 1176 | err = -EINVAL; |
| 1177 | } |
| 1178 | if (!err) |
| 1179 | err = atmel_aes_crypt_dma_start(dd); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1180 | if (!err) |
| 1181 | return; /* DMA started. Not fininishing. */ |
| 1182 | } |
| 1183 | |
| 1184 | cpu_end: |
| 1185 | atmel_aes_finish_req(dd, err); |
| 1186 | atmel_aes_handle_queue(dd, NULL); |
| 1187 | } |
| 1188 | |
| 1189 | static irqreturn_t atmel_aes_irq(int irq, void *dev_id) |
| 1190 | { |
| 1191 | struct atmel_aes_dev *aes_dd = dev_id; |
| 1192 | u32 reg; |
| 1193 | |
| 1194 | reg = atmel_aes_read(aes_dd, AES_ISR); |
| 1195 | if (reg & atmel_aes_read(aes_dd, AES_IMR)) { |
| 1196 | atmel_aes_write(aes_dd, AES_IDR, reg); |
| 1197 | if (AES_FLAGS_BUSY & aes_dd->flags) |
| 1198 | tasklet_schedule(&aes_dd->done_task); |
| 1199 | else |
| 1200 | dev_warn(aes_dd->dev, "AES interrupt when no active requests.\n"); |
| 1201 | return IRQ_HANDLED; |
| 1202 | } |
| 1203 | |
| 1204 | return IRQ_NONE; |
| 1205 | } |
| 1206 | |
| 1207 | static void atmel_aes_unregister_algs(struct atmel_aes_dev *dd) |
| 1208 | { |
| 1209 | int i; |
| 1210 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1211 | if (dd->caps.has_cfb64) |
| 1212 | crypto_unregister_alg(&aes_cfb64_alg); |
Cyrille Pitchen | 924a8bc | 2015-12-17 17:48:35 +0100 | [diff] [blame] | 1213 | |
| 1214 | for (i = 0; i < ARRAY_SIZE(aes_algs); i++) |
| 1215 | crypto_unregister_alg(&aes_algs[i]); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | static int atmel_aes_register_algs(struct atmel_aes_dev *dd) |
| 1219 | { |
| 1220 | int err, i, j; |
| 1221 | |
| 1222 | for (i = 0; i < ARRAY_SIZE(aes_algs); i++) { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1223 | err = crypto_register_alg(&aes_algs[i]); |
| 1224 | if (err) |
| 1225 | goto err_aes_algs; |
| 1226 | } |
| 1227 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1228 | if (dd->caps.has_cfb64) { |
| 1229 | err = crypto_register_alg(&aes_cfb64_alg); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1230 | if (err) |
| 1231 | goto err_aes_cfb64_alg; |
| 1232 | } |
| 1233 | |
| 1234 | return 0; |
| 1235 | |
| 1236 | err_aes_cfb64_alg: |
| 1237 | i = ARRAY_SIZE(aes_algs); |
| 1238 | err_aes_algs: |
| 1239 | for (j = 0; j < i; j++) |
| 1240 | crypto_unregister_alg(&aes_algs[j]); |
| 1241 | |
| 1242 | return err; |
| 1243 | } |
| 1244 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1245 | static void atmel_aes_get_cap(struct atmel_aes_dev *dd) |
| 1246 | { |
| 1247 | dd->caps.has_dualbuff = 0; |
| 1248 | dd->caps.has_cfb64 = 0; |
| 1249 | dd->caps.max_burst_size = 1; |
| 1250 | |
| 1251 | /* keep only major version number */ |
| 1252 | switch (dd->hw_version & 0xff0) { |
Leilei Zhao | 973e209 | 2015-12-17 17:48:32 +0100 | [diff] [blame] | 1253 | case 0x500: |
| 1254 | dd->caps.has_dualbuff = 1; |
| 1255 | dd->caps.has_cfb64 = 1; |
| 1256 | dd->caps.max_burst_size = 4; |
| 1257 | break; |
Leilei Zhao | cf1f0d1 | 2015-04-07 17:45:02 +0800 | [diff] [blame] | 1258 | case 0x200: |
| 1259 | dd->caps.has_dualbuff = 1; |
| 1260 | dd->caps.has_cfb64 = 1; |
| 1261 | dd->caps.max_burst_size = 4; |
| 1262 | break; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1263 | case 0x130: |
| 1264 | dd->caps.has_dualbuff = 1; |
| 1265 | dd->caps.has_cfb64 = 1; |
| 1266 | dd->caps.max_burst_size = 4; |
| 1267 | break; |
| 1268 | case 0x120: |
| 1269 | break; |
| 1270 | default: |
| 1271 | dev_warn(dd->dev, |
| 1272 | "Unmanaged aes version, set minimum capabilities\n"); |
| 1273 | break; |
| 1274 | } |
| 1275 | } |
| 1276 | |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 1277 | #if defined(CONFIG_OF) |
| 1278 | static const struct of_device_id atmel_aes_dt_ids[] = { |
| 1279 | { .compatible = "atmel,at91sam9g46-aes" }, |
| 1280 | { /* sentinel */ } |
| 1281 | }; |
| 1282 | MODULE_DEVICE_TABLE(of, atmel_aes_dt_ids); |
| 1283 | |
| 1284 | static struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev) |
| 1285 | { |
| 1286 | struct device_node *np = pdev->dev.of_node; |
| 1287 | struct crypto_platform_data *pdata; |
| 1288 | |
| 1289 | if (!np) { |
| 1290 | dev_err(&pdev->dev, "device node not found\n"); |
| 1291 | return ERR_PTR(-EINVAL); |
| 1292 | } |
| 1293 | |
| 1294 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 1295 | if (!pdata) { |
| 1296 | dev_err(&pdev->dev, "could not allocate memory for pdata\n"); |
| 1297 | return ERR_PTR(-ENOMEM); |
| 1298 | } |
| 1299 | |
| 1300 | pdata->dma_slave = devm_kzalloc(&pdev->dev, |
| 1301 | sizeof(*(pdata->dma_slave)), |
| 1302 | GFP_KERNEL); |
| 1303 | if (!pdata->dma_slave) { |
| 1304 | dev_err(&pdev->dev, "could not allocate memory for dma_slave\n"); |
| 1305 | devm_kfree(&pdev->dev, pdata); |
| 1306 | return ERR_PTR(-ENOMEM); |
| 1307 | } |
| 1308 | |
| 1309 | return pdata; |
| 1310 | } |
| 1311 | #else |
| 1312 | static inline struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev) |
| 1313 | { |
| 1314 | return ERR_PTR(-EINVAL); |
| 1315 | } |
| 1316 | #endif |
| 1317 | |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 1318 | static int atmel_aes_probe(struct platform_device *pdev) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1319 | { |
| 1320 | struct atmel_aes_dev *aes_dd; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1321 | struct crypto_platform_data *pdata; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1322 | struct device *dev = &pdev->dev; |
| 1323 | struct resource *aes_res; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1324 | int err; |
| 1325 | |
| 1326 | pdata = pdev->dev.platform_data; |
| 1327 | if (!pdata) { |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 1328 | pdata = atmel_aes_of_init(pdev); |
| 1329 | if (IS_ERR(pdata)) { |
| 1330 | err = PTR_ERR(pdata); |
| 1331 | goto aes_dd_err; |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | if (!pdata->dma_slave) { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1336 | err = -ENXIO; |
| 1337 | goto aes_dd_err; |
| 1338 | } |
| 1339 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1340 | aes_dd = devm_kzalloc(&pdev->dev, sizeof(*aes_dd), GFP_KERNEL); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1341 | if (aes_dd == NULL) { |
| 1342 | dev_err(dev, "unable to alloc data struct.\n"); |
| 1343 | err = -ENOMEM; |
| 1344 | goto aes_dd_err; |
| 1345 | } |
| 1346 | |
| 1347 | aes_dd->dev = dev; |
| 1348 | |
| 1349 | platform_set_drvdata(pdev, aes_dd); |
| 1350 | |
| 1351 | INIT_LIST_HEAD(&aes_dd->list); |
Leilei Zhao | 8a10eb8 | 2015-04-07 17:45:09 +0800 | [diff] [blame] | 1352 | spin_lock_init(&aes_dd->lock); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1353 | |
| 1354 | tasklet_init(&aes_dd->done_task, atmel_aes_done_task, |
| 1355 | (unsigned long)aes_dd); |
| 1356 | tasklet_init(&aes_dd->queue_task, atmel_aes_queue_task, |
| 1357 | (unsigned long)aes_dd); |
| 1358 | |
| 1359 | crypto_init_queue(&aes_dd->queue, ATMEL_AES_QUEUE_LENGTH); |
| 1360 | |
| 1361 | aes_dd->irq = -1; |
| 1362 | |
| 1363 | /* Get the base address */ |
| 1364 | aes_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1365 | if (!aes_res) { |
| 1366 | dev_err(dev, "no MEM resource info\n"); |
| 1367 | err = -ENODEV; |
| 1368 | goto res_err; |
| 1369 | } |
| 1370 | aes_dd->phys_base = aes_res->start; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1371 | |
| 1372 | /* Get the IRQ */ |
| 1373 | aes_dd->irq = platform_get_irq(pdev, 0); |
| 1374 | if (aes_dd->irq < 0) { |
| 1375 | dev_err(dev, "no IRQ resource info\n"); |
| 1376 | err = aes_dd->irq; |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1377 | goto res_err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1378 | } |
| 1379 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1380 | err = devm_request_irq(&pdev->dev, aes_dd->irq, atmel_aes_irq, |
| 1381 | IRQF_SHARED, "atmel-aes", aes_dd); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1382 | if (err) { |
| 1383 | dev_err(dev, "unable to request aes irq.\n"); |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1384 | goto res_err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | /* Initializing the clock */ |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1388 | aes_dd->iclk = devm_clk_get(&pdev->dev, "aes_clk"); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1389 | if (IS_ERR(aes_dd->iclk)) { |
Colin Ian King | be20835 | 2015-02-28 20:40:10 +0000 | [diff] [blame] | 1390 | dev_err(dev, "clock initialization failed.\n"); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1391 | err = PTR_ERR(aes_dd->iclk); |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1392 | goto res_err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1393 | } |
| 1394 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1395 | aes_dd->io_base = devm_ioremap_resource(&pdev->dev, aes_res); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1396 | if (!aes_dd->io_base) { |
| 1397 | dev_err(dev, "can't ioremap\n"); |
| 1398 | err = -ENOMEM; |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1399 | goto res_err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1400 | } |
| 1401 | |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 1402 | err = atmel_aes_hw_version_init(aes_dd); |
| 1403 | if (err) |
| 1404 | goto res_err; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1405 | |
| 1406 | atmel_aes_get_cap(aes_dd); |
| 1407 | |
| 1408 | err = atmel_aes_buff_init(aes_dd); |
| 1409 | if (err) |
| 1410 | goto err_aes_buff; |
| 1411 | |
| 1412 | err = atmel_aes_dma_init(aes_dd, pdata); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1413 | if (err) |
| 1414 | goto err_aes_dma; |
| 1415 | |
| 1416 | spin_lock(&atmel_aes.lock); |
| 1417 | list_add_tail(&aes_dd->list, &atmel_aes.dev_list); |
| 1418 | spin_unlock(&atmel_aes.lock); |
| 1419 | |
| 1420 | err = atmel_aes_register_algs(aes_dd); |
| 1421 | if (err) |
| 1422 | goto err_algs; |
| 1423 | |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 1424 | dev_info(dev, "Atmel AES - Using %s, %s for DMA transfers\n", |
| 1425 | dma_chan_name(aes_dd->dma_lch_in.chan), |
| 1426 | dma_chan_name(aes_dd->dma_lch_out.chan)); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1427 | |
| 1428 | return 0; |
| 1429 | |
| 1430 | err_algs: |
| 1431 | spin_lock(&atmel_aes.lock); |
| 1432 | list_del(&aes_dd->list); |
| 1433 | spin_unlock(&atmel_aes.lock); |
| 1434 | atmel_aes_dma_cleanup(aes_dd); |
| 1435 | err_aes_dma: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1436 | atmel_aes_buff_cleanup(aes_dd); |
| 1437 | err_aes_buff: |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1438 | res_err: |
| 1439 | tasklet_kill(&aes_dd->done_task); |
| 1440 | tasklet_kill(&aes_dd->queue_task); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1441 | aes_dd_err: |
| 1442 | dev_err(dev, "initialization failed.\n"); |
| 1443 | |
| 1444 | return err; |
| 1445 | } |
| 1446 | |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 1447 | static int atmel_aes_remove(struct platform_device *pdev) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1448 | { |
| 1449 | static struct atmel_aes_dev *aes_dd; |
| 1450 | |
| 1451 | aes_dd = platform_get_drvdata(pdev); |
| 1452 | if (!aes_dd) |
| 1453 | return -ENODEV; |
| 1454 | spin_lock(&atmel_aes.lock); |
| 1455 | list_del(&aes_dd->list); |
| 1456 | spin_unlock(&atmel_aes.lock); |
| 1457 | |
| 1458 | atmel_aes_unregister_algs(aes_dd); |
| 1459 | |
| 1460 | tasklet_kill(&aes_dd->done_task); |
| 1461 | tasklet_kill(&aes_dd->queue_task); |
| 1462 | |
| 1463 | atmel_aes_dma_cleanup(aes_dd); |
| 1464 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1465 | return 0; |
| 1466 | } |
| 1467 | |
| 1468 | static struct platform_driver atmel_aes_driver = { |
| 1469 | .probe = atmel_aes_probe, |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 1470 | .remove = atmel_aes_remove, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1471 | .driver = { |
| 1472 | .name = "atmel_aes", |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 1473 | .of_match_table = of_match_ptr(atmel_aes_dt_ids), |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1474 | }, |
| 1475 | }; |
| 1476 | |
| 1477 | module_platform_driver(atmel_aes_driver); |
| 1478 | |
| 1479 | MODULE_DESCRIPTION("Atmel AES hw acceleration support."); |
| 1480 | MODULE_LICENSE("GPL v2"); |
| 1481 | MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique"); |