blob: b29a4e5bc4046008c0738ed299693e2403b2639e [file] [log] [blame]
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001/*
2 * Cryptographic API.
3 *
4 * Support for ATMEL SHA1/SHA256 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-sham.c drivers.
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 Royerebc82ef2012-07-01 19:19:46 +020027#include <linux/init.h>
28#include <linux/errno.h>
29#include <linux/interrupt.h>
Nicolas Royerebc82ef2012-07-01 19:19:46 +020030#include <linux/irq.h>
Nicolas Royerebc82ef2012-07-01 19:19:46 +020031#include <linux/scatterlist.h>
32#include <linux/dma-mapping.h>
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +020033#include <linux/of_device.h>
Nicolas Royerebc82ef2012-07-01 19:19:46 +020034#include <linux/delay.h>
35#include <linux/crypto.h>
36#include <linux/cryptohash.h>
37#include <crypto/scatterwalk.h>
38#include <crypto/algapi.h>
39#include <crypto/sha.h>
40#include <crypto/hash.h>
41#include <crypto/internal/hash.h>
Nicolas Royerd4905b32013-02-20 17:10:26 +010042#include <linux/platform_data/crypto-atmel.h>
Nicolas Royerebc82ef2012-07-01 19:19:46 +020043#include "atmel-sha-regs.h"
44
45/* SHA flags */
46#define SHA_FLAGS_BUSY BIT(0)
47#define SHA_FLAGS_FINAL BIT(1)
48#define SHA_FLAGS_DMA_ACTIVE BIT(2)
49#define SHA_FLAGS_OUTPUT_READY BIT(3)
50#define SHA_FLAGS_INIT BIT(4)
51#define SHA_FLAGS_CPU BIT(5)
52#define SHA_FLAGS_DMA_READY BIT(6)
53
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +010054/* bits[10:8] are reserved. */
55#define SHA_FLAGS_ALGO_MASK SHA_MR_ALGO_MASK
56#define SHA_FLAGS_SHA1 SHA_MR_ALGO_SHA1
57#define SHA_FLAGS_SHA256 SHA_MR_ALGO_SHA256
58#define SHA_FLAGS_SHA384 SHA_MR_ALGO_SHA384
59#define SHA_FLAGS_SHA512 SHA_MR_ALGO_SHA512
60#define SHA_FLAGS_SHA224 SHA_MR_ALGO_SHA224
61
Nicolas Royerebc82ef2012-07-01 19:19:46 +020062#define SHA_FLAGS_FINUP BIT(16)
63#define SHA_FLAGS_SG BIT(17)
Nicolas Royerd4905b32013-02-20 17:10:26 +010064#define SHA_FLAGS_ERROR BIT(23)
65#define SHA_FLAGS_PAD BIT(24)
Cyrille Pitchen7cee3502016-01-15 15:49:34 +010066#define SHA_FLAGS_RESTORE BIT(25)
Nicolas Royerebc82ef2012-07-01 19:19:46 +020067
68#define SHA_OP_UPDATE 1
69#define SHA_OP_FINAL 2
70
Cyrille Pitchencc831d32016-01-29 17:04:02 +010071#define SHA_BUFFER_LEN (PAGE_SIZE / 16)
Nicolas Royerebc82ef2012-07-01 19:19:46 +020072
73#define ATMEL_SHA_DMA_THRESHOLD 56
74
Nicolas Royerd4905b32013-02-20 17:10:26 +010075struct atmel_sha_caps {
76 bool has_dma;
77 bool has_dualbuff;
78 bool has_sha224;
79 bool has_sha_384_512;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +010080 bool has_uihv;
Nicolas Royerd4905b32013-02-20 17:10:26 +010081};
Nicolas Royerebc82ef2012-07-01 19:19:46 +020082
83struct atmel_sha_dev;
84
Cyrille Pitchencc831d32016-01-29 17:04:02 +010085/*
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +010086 * .statesize = sizeof(struct atmel_sha_reqctx) must be <= PAGE_SIZE / 8 as
Cyrille Pitchencc831d32016-01-29 17:04:02 +010087 * tested by the ahash_prepare_alg() function.
88 */
Nicolas Royerebc82ef2012-07-01 19:19:46 +020089struct atmel_sha_reqctx {
90 struct atmel_sha_dev *dd;
91 unsigned long flags;
92 unsigned long op;
93
Nicolas Royerd4905b32013-02-20 17:10:26 +010094 u8 digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32));
95 u64 digcnt[2];
Nicolas Royerebc82ef2012-07-01 19:19:46 +020096 size_t bufcnt;
97 size_t buflen;
98 dma_addr_t dma_addr;
99
100 /* walk state */
101 struct scatterlist *sg;
102 unsigned int offset; /* offset in current sg */
103 unsigned int total; /* total request */
104
Nicolas Royerd4905b32013-02-20 17:10:26 +0100105 size_t block_size;
106
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +0100107 u8 buffer[SHA_BUFFER_LEN + SHA512_BLOCK_SIZE] __aligned(sizeof(u32));
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200108};
109
Cyrille Pitchena29af932017-01-26 17:07:47 +0100110typedef int (*atmel_sha_fn_t)(struct atmel_sha_dev *);
111
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200112struct atmel_sha_ctx {
113 struct atmel_sha_dev *dd;
Cyrille Pitchena29af932017-01-26 17:07:47 +0100114 atmel_sha_fn_t start;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200115
116 unsigned long flags;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200117};
118
Nicolas Royerd4905b32013-02-20 17:10:26 +0100119#define ATMEL_SHA_QUEUE_LENGTH 50
120
121struct atmel_sha_dma {
122 struct dma_chan *chan;
123 struct dma_slave_config dma_conf;
124};
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200125
126struct atmel_sha_dev {
127 struct list_head list;
128 unsigned long phys_base;
129 struct device *dev;
130 struct clk *iclk;
131 int irq;
132 void __iomem *io_base;
133
134 spinlock_t lock;
135 int err;
136 struct tasklet_struct done_task;
Cyrille Pitchenf56809c2016-01-15 15:49:32 +0100137 struct tasklet_struct queue_task;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200138
139 unsigned long flags;
140 struct crypto_queue queue;
141 struct ahash_request *req;
Cyrille Pitchena29af932017-01-26 17:07:47 +0100142 bool is_async;
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +0100143 atmel_sha_fn_t resume;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100144
145 struct atmel_sha_dma dma_lch_in;
146
147 struct atmel_sha_caps caps;
148
149 u32 hw_version;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200150};
151
152struct atmel_sha_drv {
153 struct list_head dev_list;
154 spinlock_t lock;
155};
156
157static struct atmel_sha_drv atmel_sha = {
158 .dev_list = LIST_HEAD_INIT(atmel_sha.dev_list),
159 .lock = __SPIN_LOCK_UNLOCKED(atmel_sha.lock),
160};
161
162static inline u32 atmel_sha_read(struct atmel_sha_dev *dd, u32 offset)
163{
164 return readl_relaxed(dd->io_base + offset);
165}
166
167static inline void atmel_sha_write(struct atmel_sha_dev *dd,
168 u32 offset, u32 value)
169{
170 writel_relaxed(value, dd->io_base + offset);
171}
172
Cyrille Pitchena29af932017-01-26 17:07:47 +0100173static inline int atmel_sha_complete(struct atmel_sha_dev *dd, int err)
174{
175 struct ahash_request *req = dd->req;
176
177 dd->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL | SHA_FLAGS_CPU |
178 SHA_FLAGS_DMA_READY | SHA_FLAGS_OUTPUT_READY);
179
180 clk_disable(dd->iclk);
181
182 if (dd->is_async && req->base.complete)
183 req->base.complete(&req->base, err);
184
185 /* handle new request */
186 tasklet_schedule(&dd->queue_task);
187
188 return err;
189}
190
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200191static size_t atmel_sha_append_sg(struct atmel_sha_reqctx *ctx)
192{
193 size_t count;
194
195 while ((ctx->bufcnt < ctx->buflen) && ctx->total) {
196 count = min(ctx->sg->length - ctx->offset, ctx->total);
197 count = min(count, ctx->buflen - ctx->bufcnt);
198
Leilei Zhao803eeae2015-04-07 17:45:05 +0800199 if (count <= 0) {
200 /*
201 * Check if count <= 0 because the buffer is full or
202 * because the sg length is 0. In the latest case,
203 * check if there is another sg in the list, a 0 length
204 * sg doesn't necessarily mean the end of the sg list.
205 */
206 if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) {
207 ctx->sg = sg_next(ctx->sg);
208 continue;
209 } else {
210 break;
211 }
212 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200213
214 scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg,
215 ctx->offset, count, 0);
216
217 ctx->bufcnt += count;
218 ctx->offset += count;
219 ctx->total -= count;
220
221 if (ctx->offset == ctx->sg->length) {
222 ctx->sg = sg_next(ctx->sg);
223 if (ctx->sg)
224 ctx->offset = 0;
225 else
226 ctx->total = 0;
227 }
228 }
229
230 return 0;
231}
232
233/*
Nicolas Royerd4905b32013-02-20 17:10:26 +0100234 * The purpose of this padding is to ensure that the padded message is a
235 * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
236 * The bit "1" is appended at the end of the message followed by
237 * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
238 * 128 bits block (SHA384/SHA512) equals to the message length in bits
239 * is appended.
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200240 *
Nicolas Royerd4905b32013-02-20 17:10:26 +0100241 * For SHA1/SHA224/SHA256, padlen is calculated as followed:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200242 * - if message length < 56 bytes then padlen = 56 - message length
243 * - else padlen = 64 + 56 - message length
Nicolas Royerd4905b32013-02-20 17:10:26 +0100244 *
245 * For SHA384/SHA512, padlen is calculated as followed:
246 * - if message length < 112 bytes then padlen = 112 - message length
247 * - else padlen = 128 + 112 - message length
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200248 */
249static void atmel_sha_fill_padding(struct atmel_sha_reqctx *ctx, int length)
250{
251 unsigned int index, padlen;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100252 u64 bits[2];
253 u64 size[2];
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200254
Nicolas Royerd4905b32013-02-20 17:10:26 +0100255 size[0] = ctx->digcnt[0];
256 size[1] = ctx->digcnt[1];
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200257
Nicolas Royerd4905b32013-02-20 17:10:26 +0100258 size[0] += ctx->bufcnt;
259 if (size[0] < ctx->bufcnt)
260 size[1]++;
261
262 size[0] += length;
263 if (size[0] < length)
264 size[1]++;
265
266 bits[1] = cpu_to_be64(size[0] << 3);
267 bits[0] = cpu_to_be64(size[1] << 3 | size[0] >> 61);
268
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100269 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
270 case SHA_FLAGS_SHA384:
271 case SHA_FLAGS_SHA512:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100272 index = ctx->bufcnt & 0x7f;
273 padlen = (index < 112) ? (112 - index) : ((128+112) - index);
274 *(ctx->buffer + ctx->bufcnt) = 0x80;
275 memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
276 memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16);
277 ctx->bufcnt += padlen + 16;
278 ctx->flags |= SHA_FLAGS_PAD;
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100279 break;
280
281 default:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100282 index = ctx->bufcnt & 0x3f;
283 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
284 *(ctx->buffer + ctx->bufcnt) = 0x80;
285 memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
286 memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8);
287 ctx->bufcnt += padlen + 8;
288 ctx->flags |= SHA_FLAGS_PAD;
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100289 break;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100290 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200291}
292
Cyrille Pitchen8340c7f2017-01-26 17:07:46 +0100293static struct atmel_sha_dev *atmel_sha_find_dev(struct atmel_sha_ctx *tctx)
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200294{
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200295 struct atmel_sha_dev *dd = NULL;
296 struct atmel_sha_dev *tmp;
297
298 spin_lock_bh(&atmel_sha.lock);
299 if (!tctx->dd) {
300 list_for_each_entry(tmp, &atmel_sha.dev_list, list) {
301 dd = tmp;
302 break;
303 }
304 tctx->dd = dd;
305 } else {
306 dd = tctx->dd;
307 }
308
309 spin_unlock_bh(&atmel_sha.lock);
310
Cyrille Pitchen8340c7f2017-01-26 17:07:46 +0100311 return dd;
312}
313
314static int atmel_sha_init(struct ahash_request *req)
315{
316 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
317 struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
318 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
319 struct atmel_sha_dev *dd = atmel_sha_find_dev(tctx);
320
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200321 ctx->dd = dd;
322
323 ctx->flags = 0;
324
325 dev_dbg(dd->dev, "init: digest size: %d\n",
326 crypto_ahash_digestsize(tfm));
327
Nicolas Royerd4905b32013-02-20 17:10:26 +0100328 switch (crypto_ahash_digestsize(tfm)) {
329 case SHA1_DIGEST_SIZE:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200330 ctx->flags |= SHA_FLAGS_SHA1;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100331 ctx->block_size = SHA1_BLOCK_SIZE;
332 break;
333 case SHA224_DIGEST_SIZE:
334 ctx->flags |= SHA_FLAGS_SHA224;
335 ctx->block_size = SHA224_BLOCK_SIZE;
336 break;
337 case SHA256_DIGEST_SIZE:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200338 ctx->flags |= SHA_FLAGS_SHA256;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100339 ctx->block_size = SHA256_BLOCK_SIZE;
340 break;
341 case SHA384_DIGEST_SIZE:
342 ctx->flags |= SHA_FLAGS_SHA384;
343 ctx->block_size = SHA384_BLOCK_SIZE;
344 break;
345 case SHA512_DIGEST_SIZE:
346 ctx->flags |= SHA_FLAGS_SHA512;
347 ctx->block_size = SHA512_BLOCK_SIZE;
348 break;
349 default:
350 return -EINVAL;
351 break;
352 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200353
354 ctx->bufcnt = 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100355 ctx->digcnt[0] = 0;
356 ctx->digcnt[1] = 0;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200357 ctx->buflen = SHA_BUFFER_LEN;
358
359 return 0;
360}
361
362static void atmel_sha_write_ctrl(struct atmel_sha_dev *dd, int dma)
363{
364 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100365 u32 valmr = SHA_MR_MODE_AUTO;
366 unsigned int i, hashsize = 0;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200367
368 if (likely(dma)) {
Nicolas Royerd4905b32013-02-20 17:10:26 +0100369 if (!dd->caps.has_dma)
370 atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200371 valmr = SHA_MR_MODE_PDC;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100372 if (dd->caps.has_dualbuff)
373 valmr |= SHA_MR_DUALBUFF;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200374 } else {
375 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
376 }
377
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100378 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
379 case SHA_FLAGS_SHA1:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100380 valmr |= SHA_MR_ALGO_SHA1;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100381 hashsize = SHA1_DIGEST_SIZE;
382 break;
383
384 case SHA_FLAGS_SHA224:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100385 valmr |= SHA_MR_ALGO_SHA224;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100386 hashsize = SHA256_DIGEST_SIZE;
387 break;
388
389 case SHA_FLAGS_SHA256:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200390 valmr |= SHA_MR_ALGO_SHA256;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100391 hashsize = SHA256_DIGEST_SIZE;
392 break;
393
394 case SHA_FLAGS_SHA384:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100395 valmr |= SHA_MR_ALGO_SHA384;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100396 hashsize = SHA512_DIGEST_SIZE;
397 break;
398
399 case SHA_FLAGS_SHA512:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100400 valmr |= SHA_MR_ALGO_SHA512;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100401 hashsize = SHA512_DIGEST_SIZE;
402 break;
403
404 default:
405 break;
406 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200407
408 /* Setting CR_FIRST only for the first iteration */
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100409 if (!(ctx->digcnt[0] || ctx->digcnt[1])) {
410 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
411 } else if (dd->caps.has_uihv && (ctx->flags & SHA_FLAGS_RESTORE)) {
412 const u32 *hash = (const u32 *)ctx->digest;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200413
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100414 /*
415 * Restore the hardware context: update the User Initialize
416 * Hash Value (UIHV) with the value saved when the latest
417 * 'update' operation completed on this very same crypto
418 * request.
419 */
420 ctx->flags &= ~SHA_FLAGS_RESTORE;
421 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
422 for (i = 0; i < hashsize / sizeof(u32); ++i)
423 atmel_sha_write(dd, SHA_REG_DIN(i), hash[i]);
424 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
425 valmr |= SHA_MR_UIHV;
426 }
427 /*
428 * WARNING: If the UIHV feature is not available, the hardware CANNOT
429 * process concurrent requests: the internal registers used to store
430 * the hash/digest are still set to the partial digest output values
431 * computed during the latest round.
432 */
433
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200434 atmel_sha_write(dd, SHA_MR, valmr);
435}
436
437static int atmel_sha_xmit_cpu(struct atmel_sha_dev *dd, const u8 *buf,
438 size_t length, int final)
439{
440 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
441 int count, len32;
442 const u32 *buffer = (const u32 *)buf;
443
Nicolas Royerd4905b32013-02-20 17:10:26 +0100444 dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
445 ctx->digcnt[1], ctx->digcnt[0], length, final);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200446
447 atmel_sha_write_ctrl(dd, 0);
448
449 /* should be non-zero before next lines to disable clocks later */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100450 ctx->digcnt[0] += length;
451 if (ctx->digcnt[0] < length)
452 ctx->digcnt[1]++;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200453
454 if (final)
455 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
456
457 len32 = DIV_ROUND_UP(length, sizeof(u32));
458
459 dd->flags |= SHA_FLAGS_CPU;
460
461 for (count = 0; count < len32; count++)
462 atmel_sha_write(dd, SHA_REG_DIN(count), buffer[count]);
463
464 return -EINPROGRESS;
465}
466
467static int atmel_sha_xmit_pdc(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
468 size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
469{
470 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
471 int len32;
472
Nicolas Royerd4905b32013-02-20 17:10:26 +0100473 dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
474 ctx->digcnt[1], ctx->digcnt[0], length1, final);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200475
476 len32 = DIV_ROUND_UP(length1, sizeof(u32));
477 atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS);
478 atmel_sha_write(dd, SHA_TPR, dma_addr1);
479 atmel_sha_write(dd, SHA_TCR, len32);
480
481 len32 = DIV_ROUND_UP(length2, sizeof(u32));
482 atmel_sha_write(dd, SHA_TNPR, dma_addr2);
483 atmel_sha_write(dd, SHA_TNCR, len32);
484
485 atmel_sha_write_ctrl(dd, 1);
486
487 /* should be non-zero before next lines to disable clocks later */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100488 ctx->digcnt[0] += length1;
489 if (ctx->digcnt[0] < length1)
490 ctx->digcnt[1]++;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200491
492 if (final)
493 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
494
495 dd->flags |= SHA_FLAGS_DMA_ACTIVE;
496
497 /* Start DMA transfer */
498 atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTEN);
499
500 return -EINPROGRESS;
501}
502
Nicolas Royerd4905b32013-02-20 17:10:26 +0100503static void atmel_sha_dma_callback(void *data)
504{
505 struct atmel_sha_dev *dd = data;
506
Cyrille Pitchena29af932017-01-26 17:07:47 +0100507 dd->is_async = true;
508
Nicolas Royerd4905b32013-02-20 17:10:26 +0100509 /* dma_lch_in - completed - wait DATRDY */
510 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
511}
512
513static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
514 size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
515{
516 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
517 struct dma_async_tx_descriptor *in_desc;
518 struct scatterlist sg[2];
519
520 dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
521 ctx->digcnt[1], ctx->digcnt[0], length1, final);
522
Leilei Zhao3f1992c2015-04-07 17:45:07 +0800523 dd->dma_lch_in.dma_conf.src_maxburst = 16;
524 dd->dma_lch_in.dma_conf.dst_maxburst = 16;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100525
526 dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
527
528 if (length2) {
529 sg_init_table(sg, 2);
530 sg_dma_address(&sg[0]) = dma_addr1;
531 sg_dma_len(&sg[0]) = length1;
532 sg_dma_address(&sg[1]) = dma_addr2;
533 sg_dma_len(&sg[1]) = length2;
534 in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2,
535 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
536 } else {
537 sg_init_table(sg, 1);
538 sg_dma_address(&sg[0]) = dma_addr1;
539 sg_dma_len(&sg[0]) = length1;
540 in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1,
541 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
542 }
543 if (!in_desc)
Cyrille Pitchena29af932017-01-26 17:07:47 +0100544 atmel_sha_complete(dd, -EINVAL);
Nicolas Royerd4905b32013-02-20 17:10:26 +0100545
546 in_desc->callback = atmel_sha_dma_callback;
547 in_desc->callback_param = dd;
548
549 atmel_sha_write_ctrl(dd, 1);
550
551 /* should be non-zero before next lines to disable clocks later */
552 ctx->digcnt[0] += length1;
553 if (ctx->digcnt[0] < length1)
554 ctx->digcnt[1]++;
555
556 if (final)
557 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
558
559 dd->flags |= SHA_FLAGS_DMA_ACTIVE;
560
561 /* Start DMA transfer */
562 dmaengine_submit(in_desc);
563 dma_async_issue_pending(dd->dma_lch_in.chan);
564
565 return -EINPROGRESS;
566}
567
568static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
569 size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
570{
571 if (dd->caps.has_dma)
572 return atmel_sha_xmit_dma(dd, dma_addr1, length1,
573 dma_addr2, length2, final);
574 else
575 return atmel_sha_xmit_pdc(dd, dma_addr1, length1,
576 dma_addr2, length2, final);
577}
578
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200579static int atmel_sha_update_cpu(struct atmel_sha_dev *dd)
580{
581 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
582 int bufcnt;
583
584 atmel_sha_append_sg(ctx);
585 atmel_sha_fill_padding(ctx, 0);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200586 bufcnt = ctx->bufcnt;
587 ctx->bufcnt = 0;
588
589 return atmel_sha_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
590}
591
592static int atmel_sha_xmit_dma_map(struct atmel_sha_dev *dd,
593 struct atmel_sha_reqctx *ctx,
594 size_t length, int final)
595{
596 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
Nicolas Royerd4905b32013-02-20 17:10:26 +0100597 ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200598 if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
599 dev_err(dd->dev, "dma %u bytes error\n", ctx->buflen +
Nicolas Royerd4905b32013-02-20 17:10:26 +0100600 ctx->block_size);
Cyrille Pitchena29af932017-01-26 17:07:47 +0100601 atmel_sha_complete(dd, -EINVAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200602 }
603
604 ctx->flags &= ~SHA_FLAGS_SG;
605
606 /* next call does not fail... so no unmap in the case of error */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100607 return atmel_sha_xmit_start(dd, ctx->dma_addr, length, 0, 0, final);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200608}
609
610static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd)
611{
612 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
613 unsigned int final;
614 size_t count;
615
616 atmel_sha_append_sg(ctx);
617
618 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
619
Nicolas Royerd4905b32013-02-20 17:10:26 +0100620 dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: 0x%llx 0x%llx, final: %d\n",
621 ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200622
623 if (final)
624 atmel_sha_fill_padding(ctx, 0);
625
Ludovic Desroches00992862015-04-07 17:45:04 +0800626 if (final || (ctx->bufcnt == ctx->buflen)) {
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200627 count = ctx->bufcnt;
628 ctx->bufcnt = 0;
629 return atmel_sha_xmit_dma_map(dd, ctx, count, final);
630 }
631
632 return 0;
633}
634
635static int atmel_sha_update_dma_start(struct atmel_sha_dev *dd)
636{
637 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
638 unsigned int length, final, tail;
639 struct scatterlist *sg;
640 unsigned int count;
641
642 if (!ctx->total)
643 return 0;
644
645 if (ctx->bufcnt || ctx->offset)
646 return atmel_sha_update_dma_slow(dd);
647
Nicolas Royerd4905b32013-02-20 17:10:26 +0100648 dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %u, total: %u\n",
649 ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200650
651 sg = ctx->sg;
652
653 if (!IS_ALIGNED(sg->offset, sizeof(u32)))
654 return atmel_sha_update_dma_slow(dd);
655
Nicolas Royerd4905b32013-02-20 17:10:26 +0100656 if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size))
657 /* size is not ctx->block_size aligned */
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200658 return atmel_sha_update_dma_slow(dd);
659
660 length = min(ctx->total, sg->length);
661
662 if (sg_is_last(sg)) {
663 if (!(ctx->flags & SHA_FLAGS_FINUP)) {
Nicolas Royerd4905b32013-02-20 17:10:26 +0100664 /* not last sg must be ctx->block_size aligned */
665 tail = length & (ctx->block_size - 1);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200666 length -= tail;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200667 }
668 }
669
670 ctx->total -= length;
671 ctx->offset = length; /* offset where to start slow */
672
673 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
674
675 /* Add padding */
676 if (final) {
Nicolas Royerd4905b32013-02-20 17:10:26 +0100677 tail = length & (ctx->block_size - 1);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200678 length -= tail;
679 ctx->total += tail;
680 ctx->offset = length; /* offset where to start slow */
681
682 sg = ctx->sg;
683 atmel_sha_append_sg(ctx);
684
685 atmel_sha_fill_padding(ctx, length);
686
687 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
Nicolas Royerd4905b32013-02-20 17:10:26 +0100688 ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200689 if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
690 dev_err(dd->dev, "dma %u bytes error\n",
Nicolas Royerd4905b32013-02-20 17:10:26 +0100691 ctx->buflen + ctx->block_size);
Cyrille Pitchena29af932017-01-26 17:07:47 +0100692 atmel_sha_complete(dd, -EINVAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200693 }
694
695 if (length == 0) {
696 ctx->flags &= ~SHA_FLAGS_SG;
697 count = ctx->bufcnt;
698 ctx->bufcnt = 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100699 return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0,
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200700 0, final);
701 } else {
702 ctx->sg = sg;
703 if (!dma_map_sg(dd->dev, ctx->sg, 1,
704 DMA_TO_DEVICE)) {
705 dev_err(dd->dev, "dma_map_sg error\n");
Cyrille Pitchena29af932017-01-26 17:07:47 +0100706 atmel_sha_complete(dd, -EINVAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200707 }
708
709 ctx->flags |= SHA_FLAGS_SG;
710
711 count = ctx->bufcnt;
712 ctx->bufcnt = 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100713 return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg),
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200714 length, ctx->dma_addr, count, final);
715 }
716 }
717
718 if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
719 dev_err(dd->dev, "dma_map_sg error\n");
Cyrille Pitchena29af932017-01-26 17:07:47 +0100720 atmel_sha_complete(dd, -EINVAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200721 }
722
723 ctx->flags |= SHA_FLAGS_SG;
724
725 /* next call does not fail... so no unmap in the case of error */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100726 return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), length, 0,
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200727 0, final);
728}
729
730static int atmel_sha_update_dma_stop(struct atmel_sha_dev *dd)
731{
732 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
733
734 if (ctx->flags & SHA_FLAGS_SG) {
735 dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
736 if (ctx->sg->length == ctx->offset) {
737 ctx->sg = sg_next(ctx->sg);
738 if (ctx->sg)
739 ctx->offset = 0;
740 }
Nicolas Royerd4905b32013-02-20 17:10:26 +0100741 if (ctx->flags & SHA_FLAGS_PAD) {
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200742 dma_unmap_single(dd->dev, ctx->dma_addr,
Nicolas Royerd4905b32013-02-20 17:10:26 +0100743 ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
744 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200745 } else {
746 dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen +
Nicolas Royerd4905b32013-02-20 17:10:26 +0100747 ctx->block_size, DMA_TO_DEVICE);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200748 }
749
750 return 0;
751}
752
753static int atmel_sha_update_req(struct atmel_sha_dev *dd)
754{
755 struct ahash_request *req = dd->req;
756 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
757 int err;
758
Nicolas Royerd4905b32013-02-20 17:10:26 +0100759 dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n",
760 ctx->total, ctx->digcnt[1], ctx->digcnt[0]);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200761
762 if (ctx->flags & SHA_FLAGS_CPU)
763 err = atmel_sha_update_cpu(dd);
764 else
765 err = atmel_sha_update_dma_start(dd);
766
767 /* wait for dma completion before can take more data */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100768 dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx\n",
769 err, ctx->digcnt[1], ctx->digcnt[0]);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200770
771 return err;
772}
773
774static int atmel_sha_final_req(struct atmel_sha_dev *dd)
775{
776 struct ahash_request *req = dd->req;
777 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
778 int err = 0;
779 int count;
780
781 if (ctx->bufcnt >= ATMEL_SHA_DMA_THRESHOLD) {
782 atmel_sha_fill_padding(ctx, 0);
783 count = ctx->bufcnt;
784 ctx->bufcnt = 0;
785 err = atmel_sha_xmit_dma_map(dd, ctx, count, 1);
786 }
787 /* faster to handle last block with cpu */
788 else {
789 atmel_sha_fill_padding(ctx, 0);
790 count = ctx->bufcnt;
791 ctx->bufcnt = 0;
792 err = atmel_sha_xmit_cpu(dd, ctx->buffer, count, 1);
793 }
794
795 dev_dbg(dd->dev, "final_req: err: %d\n", err);
796
797 return err;
798}
799
800static void atmel_sha_copy_hash(struct ahash_request *req)
801{
802 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
803 u32 *hash = (u32 *)ctx->digest;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100804 unsigned int i, hashsize;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200805
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100806 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
807 case SHA_FLAGS_SHA1:
808 hashsize = SHA1_DIGEST_SIZE;
809 break;
810
811 case SHA_FLAGS_SHA224:
812 case SHA_FLAGS_SHA256:
813 hashsize = SHA256_DIGEST_SIZE;
814 break;
815
816 case SHA_FLAGS_SHA384:
817 case SHA_FLAGS_SHA512:
818 hashsize = SHA512_DIGEST_SIZE;
819 break;
820
821 default:
822 /* Should not happen... */
823 return;
824 }
825
826 for (i = 0; i < hashsize / sizeof(u32); ++i)
827 hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
828 ctx->flags |= SHA_FLAGS_RESTORE;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200829}
830
831static void atmel_sha_copy_ready_hash(struct ahash_request *req)
832{
833 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
834
835 if (!req->result)
836 return;
837
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100838 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
839 default:
840 case SHA_FLAGS_SHA1:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200841 memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100842 break;
843
844 case SHA_FLAGS_SHA224:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100845 memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100846 break;
847
848 case SHA_FLAGS_SHA256:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200849 memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100850 break;
851
852 case SHA_FLAGS_SHA384:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100853 memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100854 break;
855
856 case SHA_FLAGS_SHA512:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100857 memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100858 break;
859 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200860}
861
862static int atmel_sha_finish(struct ahash_request *req)
863{
864 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
865 struct atmel_sha_dev *dd = ctx->dd;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200866
Nicolas Royerd4905b32013-02-20 17:10:26 +0100867 if (ctx->digcnt[0] || ctx->digcnt[1])
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200868 atmel_sha_copy_ready_hash(req);
869
Nicolas Royerd4905b32013-02-20 17:10:26 +0100870 dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %d\n", ctx->digcnt[1],
871 ctx->digcnt[0], ctx->bufcnt);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200872
Rahul Pathak871b88a2015-12-14 08:44:19 +0000873 return 0;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200874}
875
876static void atmel_sha_finish_req(struct ahash_request *req, int err)
877{
878 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
879 struct atmel_sha_dev *dd = ctx->dd;
880
881 if (!err) {
882 atmel_sha_copy_hash(req);
883 if (SHA_FLAGS_FINAL & dd->flags)
884 err = atmel_sha_finish(req);
885 } else {
886 ctx->flags |= SHA_FLAGS_ERROR;
887 }
888
889 /* atomic operation is not needed here */
Cyrille Pitchena29af932017-01-26 17:07:47 +0100890 (void)atmel_sha_complete(dd, err);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200891}
892
893static int atmel_sha_hw_init(struct atmel_sha_dev *dd)
894{
LABBE Corentin9d83d292015-10-02 14:12:58 +0200895 int err;
896
Cyrille Pitchenc0330422016-02-05 13:45:13 +0100897 err = clk_enable(dd->iclk);
LABBE Corentin9d83d292015-10-02 14:12:58 +0200898 if (err)
899 return err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200900
Nicolas Royerd4905b32013-02-20 17:10:26 +0100901 if (!(SHA_FLAGS_INIT & dd->flags)) {
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200902 atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200903 dd->flags |= SHA_FLAGS_INIT;
904 dd->err = 0;
905 }
906
907 return 0;
908}
909
Nicolas Royerd4905b32013-02-20 17:10:26 +0100910static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd)
911{
912 return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff;
913}
914
915static void atmel_sha_hw_version_init(struct atmel_sha_dev *dd)
916{
917 atmel_sha_hw_init(dd);
918
919 dd->hw_version = atmel_sha_get_version(dd);
920
921 dev_info(dd->dev,
922 "version: 0x%x\n", dd->hw_version);
923
Cyrille Pitchenc0330422016-02-05 13:45:13 +0100924 clk_disable(dd->iclk);
Nicolas Royerd4905b32013-02-20 17:10:26 +0100925}
926
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200927static int atmel_sha_handle_queue(struct atmel_sha_dev *dd,
928 struct ahash_request *req)
929{
930 struct crypto_async_request *async_req, *backlog;
Cyrille Pitchena29af932017-01-26 17:07:47 +0100931 struct atmel_sha_ctx *ctx;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200932 unsigned long flags;
Cyrille Pitchena29af932017-01-26 17:07:47 +0100933 bool start_async;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200934 int err = 0, ret = 0;
935
936 spin_lock_irqsave(&dd->lock, flags);
937 if (req)
938 ret = ahash_enqueue_request(&dd->queue, req);
939
940 if (SHA_FLAGS_BUSY & dd->flags) {
941 spin_unlock_irqrestore(&dd->lock, flags);
942 return ret;
943 }
944
945 backlog = crypto_get_backlog(&dd->queue);
946 async_req = crypto_dequeue_request(&dd->queue);
947 if (async_req)
948 dd->flags |= SHA_FLAGS_BUSY;
949
950 spin_unlock_irqrestore(&dd->lock, flags);
951
952 if (!async_req)
953 return ret;
954
955 if (backlog)
956 backlog->complete(backlog, -EINPROGRESS);
957
Cyrille Pitchena29af932017-01-26 17:07:47 +0100958 ctx = crypto_tfm_ctx(async_req->tfm);
959
960 dd->req = ahash_request_cast(async_req);
961 start_async = (dd->req != req);
962 dd->is_async = start_async;
963
964 /* WARNING: ctx->start() MAY change dd->is_async. */
965 err = ctx->start(dd);
966 return (start_async) ? ret : err;
967}
968
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +0100969static int atmel_sha_done(struct atmel_sha_dev *dd);
970
Cyrille Pitchena29af932017-01-26 17:07:47 +0100971static int atmel_sha_start(struct atmel_sha_dev *dd)
972{
973 struct ahash_request *req = dd->req;
974 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
975 int err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200976
977 dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
978 ctx->op, req->nbytes);
979
980 err = atmel_sha_hw_init(dd);
981
982 if (err)
983 goto err1;
984
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +0100985 dd->resume = atmel_sha_done;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200986 if (ctx->op == SHA_OP_UPDATE) {
987 err = atmel_sha_update_req(dd);
Nicolas Royerd4905b32013-02-20 17:10:26 +0100988 if (err != -EINPROGRESS && (ctx->flags & SHA_FLAGS_FINUP))
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200989 /* no final() after finup() */
990 err = atmel_sha_final_req(dd);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200991 } else if (ctx->op == SHA_OP_FINAL) {
992 err = atmel_sha_final_req(dd);
993 }
994
995err1:
996 if (err != -EINPROGRESS)
997 /* done_task will not finish it, so do it here */
998 atmel_sha_finish_req(req, err);
999
1000 dev_dbg(dd->dev, "exit, err: %d\n", err);
1001
Cyrille Pitchena29af932017-01-26 17:07:47 +01001002 return err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001003}
1004
1005static int atmel_sha_enqueue(struct ahash_request *req, unsigned int op)
1006{
1007 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1008 struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1009 struct atmel_sha_dev *dd = tctx->dd;
1010
1011 ctx->op = op;
1012
1013 return atmel_sha_handle_queue(dd, req);
1014}
1015
1016static int atmel_sha_update(struct ahash_request *req)
1017{
1018 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1019
1020 if (!req->nbytes)
1021 return 0;
1022
1023 ctx->total = req->nbytes;
1024 ctx->sg = req->src;
1025 ctx->offset = 0;
1026
1027 if (ctx->flags & SHA_FLAGS_FINUP) {
1028 if (ctx->bufcnt + ctx->total < ATMEL_SHA_DMA_THRESHOLD)
1029 /* faster to use CPU for short transfers */
1030 ctx->flags |= SHA_FLAGS_CPU;
1031 } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
1032 atmel_sha_append_sg(ctx);
1033 return 0;
1034 }
1035 return atmel_sha_enqueue(req, SHA_OP_UPDATE);
1036}
1037
1038static int atmel_sha_final(struct ahash_request *req)
1039{
1040 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001041
1042 ctx->flags |= SHA_FLAGS_FINUP;
1043
1044 if (ctx->flags & SHA_FLAGS_ERROR)
1045 return 0; /* uncompleted hash is not needed */
1046
Cyrille Pitchenad841122016-02-08 16:26:49 +01001047 if (ctx->flags & SHA_FLAGS_PAD)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001048 /* copy ready hash (+ finalize hmac) */
1049 return atmel_sha_finish(req);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001050
Cyrille Pitchenad841122016-02-08 16:26:49 +01001051 return atmel_sha_enqueue(req, SHA_OP_FINAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001052}
1053
1054static int atmel_sha_finup(struct ahash_request *req)
1055{
1056 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1057 int err1, err2;
1058
1059 ctx->flags |= SHA_FLAGS_FINUP;
1060
1061 err1 = atmel_sha_update(req);
1062 if (err1 == -EINPROGRESS || err1 == -EBUSY)
1063 return err1;
1064
1065 /*
1066 * final() has to be always called to cleanup resources
1067 * even if udpate() failed, except EINPROGRESS
1068 */
1069 err2 = atmel_sha_final(req);
1070
1071 return err1 ?: err2;
1072}
1073
1074static int atmel_sha_digest(struct ahash_request *req)
1075{
1076 return atmel_sha_init(req) ?: atmel_sha_finup(req);
1077}
1078
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001079
1080static int atmel_sha_export(struct ahash_request *req, void *out)
1081{
1082 const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001083
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001084 memcpy(out, ctx, sizeof(*ctx));
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001085 return 0;
1086}
1087
1088static int atmel_sha_import(struct ahash_request *req, const void *in)
1089{
1090 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001091
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001092 memcpy(ctx, in, sizeof(*ctx));
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001093 return 0;
1094}
1095
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001096static int atmel_sha_cra_init(struct crypto_tfm *tfm)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001097{
Cyrille Pitchena29af932017-01-26 17:07:47 +01001098 struct atmel_sha_ctx *ctx = crypto_tfm_ctx(tfm);
1099
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001100 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001101 sizeof(struct atmel_sha_reqctx));
Cyrille Pitchena29af932017-01-26 17:07:47 +01001102 ctx->start = atmel_sha_start;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001103
1104 return 0;
1105}
1106
Nicolas Royerd4905b32013-02-20 17:10:26 +01001107static struct ahash_alg sha_1_256_algs[] = {
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001108{
1109 .init = atmel_sha_init,
1110 .update = atmel_sha_update,
1111 .final = atmel_sha_final,
1112 .finup = atmel_sha_finup,
1113 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001114 .export = atmel_sha_export,
1115 .import = atmel_sha_import,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001116 .halg = {
1117 .digestsize = SHA1_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001118 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001119 .base = {
1120 .cra_name = "sha1",
1121 .cra_driver_name = "atmel-sha1",
1122 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001123 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001124 .cra_blocksize = SHA1_BLOCK_SIZE,
1125 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1126 .cra_alignmask = 0,
1127 .cra_module = THIS_MODULE,
1128 .cra_init = atmel_sha_cra_init,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001129 }
1130 }
1131},
1132{
1133 .init = atmel_sha_init,
1134 .update = atmel_sha_update,
1135 .final = atmel_sha_final,
1136 .finup = atmel_sha_finup,
1137 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001138 .export = atmel_sha_export,
1139 .import = atmel_sha_import,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001140 .halg = {
1141 .digestsize = SHA256_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001142 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001143 .base = {
1144 .cra_name = "sha256",
1145 .cra_driver_name = "atmel-sha256",
1146 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001147 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001148 .cra_blocksize = SHA256_BLOCK_SIZE,
1149 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1150 .cra_alignmask = 0,
1151 .cra_module = THIS_MODULE,
1152 .cra_init = atmel_sha_cra_init,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001153 }
1154 }
1155},
1156};
1157
Nicolas Royerd4905b32013-02-20 17:10:26 +01001158static struct ahash_alg sha_224_alg = {
1159 .init = atmel_sha_init,
1160 .update = atmel_sha_update,
1161 .final = atmel_sha_final,
1162 .finup = atmel_sha_finup,
1163 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001164 .export = atmel_sha_export,
1165 .import = atmel_sha_import,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001166 .halg = {
1167 .digestsize = SHA224_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001168 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerd4905b32013-02-20 17:10:26 +01001169 .base = {
1170 .cra_name = "sha224",
1171 .cra_driver_name = "atmel-sha224",
1172 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001173 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001174 .cra_blocksize = SHA224_BLOCK_SIZE,
1175 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1176 .cra_alignmask = 0,
1177 .cra_module = THIS_MODULE,
1178 .cra_init = atmel_sha_cra_init,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001179 }
1180 }
1181};
1182
1183static struct ahash_alg sha_384_512_algs[] = {
1184{
1185 .init = atmel_sha_init,
1186 .update = atmel_sha_update,
1187 .final = atmel_sha_final,
1188 .finup = atmel_sha_finup,
1189 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001190 .export = atmel_sha_export,
1191 .import = atmel_sha_import,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001192 .halg = {
1193 .digestsize = SHA384_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001194 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerd4905b32013-02-20 17:10:26 +01001195 .base = {
1196 .cra_name = "sha384",
1197 .cra_driver_name = "atmel-sha384",
1198 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001199 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001200 .cra_blocksize = SHA384_BLOCK_SIZE,
1201 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1202 .cra_alignmask = 0x3,
1203 .cra_module = THIS_MODULE,
1204 .cra_init = atmel_sha_cra_init,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001205 }
1206 }
1207},
1208{
1209 .init = atmel_sha_init,
1210 .update = atmel_sha_update,
1211 .final = atmel_sha_final,
1212 .finup = atmel_sha_finup,
1213 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001214 .export = atmel_sha_export,
1215 .import = atmel_sha_import,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001216 .halg = {
1217 .digestsize = SHA512_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001218 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerd4905b32013-02-20 17:10:26 +01001219 .base = {
1220 .cra_name = "sha512",
1221 .cra_driver_name = "atmel-sha512",
1222 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001223 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001224 .cra_blocksize = SHA512_BLOCK_SIZE,
1225 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1226 .cra_alignmask = 0x3,
1227 .cra_module = THIS_MODULE,
1228 .cra_init = atmel_sha_cra_init,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001229 }
1230 }
1231},
1232};
1233
Cyrille Pitchenf56809c2016-01-15 15:49:32 +01001234static void atmel_sha_queue_task(unsigned long data)
1235{
1236 struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
1237
1238 atmel_sha_handle_queue(dd, NULL);
1239}
1240
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +01001241static int atmel_sha_done(struct atmel_sha_dev *dd)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001242{
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001243 int err = 0;
1244
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001245 if (SHA_FLAGS_CPU & dd->flags) {
1246 if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
1247 dd->flags &= ~SHA_FLAGS_OUTPUT_READY;
1248 goto finish;
1249 }
1250 } else if (SHA_FLAGS_DMA_READY & dd->flags) {
1251 if (SHA_FLAGS_DMA_ACTIVE & dd->flags) {
1252 dd->flags &= ~SHA_FLAGS_DMA_ACTIVE;
1253 atmel_sha_update_dma_stop(dd);
1254 if (dd->err) {
1255 err = dd->err;
1256 goto finish;
1257 }
1258 }
1259 if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
1260 /* hash or semi-hash ready */
1261 dd->flags &= ~(SHA_FLAGS_DMA_READY |
1262 SHA_FLAGS_OUTPUT_READY);
1263 err = atmel_sha_update_dma_start(dd);
1264 if (err != -EINPROGRESS)
1265 goto finish;
1266 }
1267 }
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +01001268 return err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001269
1270finish:
1271 /* finish curent request */
1272 atmel_sha_finish_req(dd->req, err);
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +01001273
1274 return err;
1275}
1276
1277static void atmel_sha_done_task(unsigned long data)
1278{
1279 struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
1280
1281 dd->is_async = true;
1282 (void)dd->resume(dd);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001283}
1284
1285static irqreturn_t atmel_sha_irq(int irq, void *dev_id)
1286{
1287 struct atmel_sha_dev *sha_dd = dev_id;
1288 u32 reg;
1289
1290 reg = atmel_sha_read(sha_dd, SHA_ISR);
1291 if (reg & atmel_sha_read(sha_dd, SHA_IMR)) {
1292 atmel_sha_write(sha_dd, SHA_IDR, reg);
1293 if (SHA_FLAGS_BUSY & sha_dd->flags) {
1294 sha_dd->flags |= SHA_FLAGS_OUTPUT_READY;
1295 if (!(SHA_FLAGS_CPU & sha_dd->flags))
1296 sha_dd->flags |= SHA_FLAGS_DMA_READY;
1297 tasklet_schedule(&sha_dd->done_task);
1298 } else {
1299 dev_warn(sha_dd->dev, "SHA interrupt when no active requests.\n");
1300 }
1301 return IRQ_HANDLED;
1302 }
1303
1304 return IRQ_NONE;
1305}
1306
1307static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd)
1308{
1309 int i;
1310
Nicolas Royerd4905b32013-02-20 17:10:26 +01001311 for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++)
1312 crypto_unregister_ahash(&sha_1_256_algs[i]);
1313
1314 if (dd->caps.has_sha224)
1315 crypto_unregister_ahash(&sha_224_alg);
1316
1317 if (dd->caps.has_sha_384_512) {
1318 for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++)
1319 crypto_unregister_ahash(&sha_384_512_algs[i]);
1320 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001321}
1322
1323static int atmel_sha_register_algs(struct atmel_sha_dev *dd)
1324{
1325 int err, i, j;
1326
Nicolas Royerd4905b32013-02-20 17:10:26 +01001327 for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) {
1328 err = crypto_register_ahash(&sha_1_256_algs[i]);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001329 if (err)
Nicolas Royerd4905b32013-02-20 17:10:26 +01001330 goto err_sha_1_256_algs;
1331 }
1332
1333 if (dd->caps.has_sha224) {
1334 err = crypto_register_ahash(&sha_224_alg);
1335 if (err)
1336 goto err_sha_224_algs;
1337 }
1338
1339 if (dd->caps.has_sha_384_512) {
1340 for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) {
1341 err = crypto_register_ahash(&sha_384_512_algs[i]);
1342 if (err)
1343 goto err_sha_384_512_algs;
1344 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001345 }
1346
1347 return 0;
1348
Nicolas Royerd4905b32013-02-20 17:10:26 +01001349err_sha_384_512_algs:
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001350 for (j = 0; j < i; j++)
Nicolas Royerd4905b32013-02-20 17:10:26 +01001351 crypto_unregister_ahash(&sha_384_512_algs[j]);
1352 crypto_unregister_ahash(&sha_224_alg);
1353err_sha_224_algs:
1354 i = ARRAY_SIZE(sha_1_256_algs);
1355err_sha_1_256_algs:
1356 for (j = 0; j < i; j++)
1357 crypto_unregister_ahash(&sha_1_256_algs[j]);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001358
1359 return err;
1360}
1361
Nicolas Royerd4905b32013-02-20 17:10:26 +01001362static bool atmel_sha_filter(struct dma_chan *chan, void *slave)
1363{
1364 struct at_dma_slave *sl = slave;
1365
1366 if (sl && sl->dma_dev == chan->device->dev) {
1367 chan->private = sl;
1368 return true;
1369 } else {
1370 return false;
1371 }
1372}
1373
1374static int atmel_sha_dma_init(struct atmel_sha_dev *dd,
1375 struct crypto_platform_data *pdata)
1376{
1377 int err = -ENOMEM;
1378 dma_cap_mask_t mask_in;
1379
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001380 /* Try to grab DMA channel */
1381 dma_cap_zero(mask_in);
1382 dma_cap_set(DMA_SLAVE, mask_in);
Nicolas Royerd4905b32013-02-20 17:10:26 +01001383
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001384 dd->dma_lch_in.chan = dma_request_slave_channel_compat(mask_in,
1385 atmel_sha_filter, &pdata->dma_slave->rxdata, dd->dev, "tx");
1386 if (!dd->dma_lch_in.chan) {
1387 dev_warn(dd->dev, "no DMA channel available\n");
1388 return err;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001389 }
1390
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001391 dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV;
1392 dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
1393 SHA_REG_DIN(0);
1394 dd->dma_lch_in.dma_conf.src_maxburst = 1;
1395 dd->dma_lch_in.dma_conf.src_addr_width =
1396 DMA_SLAVE_BUSWIDTH_4_BYTES;
1397 dd->dma_lch_in.dma_conf.dst_maxburst = 1;
1398 dd->dma_lch_in.dma_conf.dst_addr_width =
1399 DMA_SLAVE_BUSWIDTH_4_BYTES;
1400 dd->dma_lch_in.dma_conf.device_fc = false;
1401
1402 return 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001403}
1404
1405static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd)
1406{
1407 dma_release_channel(dd->dma_lch_in.chan);
1408}
1409
1410static void atmel_sha_get_cap(struct atmel_sha_dev *dd)
1411{
1412
1413 dd->caps.has_dma = 0;
1414 dd->caps.has_dualbuff = 0;
1415 dd->caps.has_sha224 = 0;
1416 dd->caps.has_sha_384_512 = 0;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +01001417 dd->caps.has_uihv = 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001418
1419 /* keep only major version number */
1420 switch (dd->hw_version & 0xff0) {
Cyrille Pitchen507c5cc2016-01-15 15:49:33 +01001421 case 0x510:
1422 dd->caps.has_dma = 1;
1423 dd->caps.has_dualbuff = 1;
1424 dd->caps.has_sha224 = 1;
1425 dd->caps.has_sha_384_512 = 1;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +01001426 dd->caps.has_uihv = 1;
Cyrille Pitchen507c5cc2016-01-15 15:49:33 +01001427 break;
Leilei Zhao141824d2015-04-07 17:45:03 +08001428 case 0x420:
1429 dd->caps.has_dma = 1;
1430 dd->caps.has_dualbuff = 1;
1431 dd->caps.has_sha224 = 1;
1432 dd->caps.has_sha_384_512 = 1;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +01001433 dd->caps.has_uihv = 1;
Leilei Zhao141824d2015-04-07 17:45:03 +08001434 break;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001435 case 0x410:
1436 dd->caps.has_dma = 1;
1437 dd->caps.has_dualbuff = 1;
1438 dd->caps.has_sha224 = 1;
1439 dd->caps.has_sha_384_512 = 1;
1440 break;
1441 case 0x400:
1442 dd->caps.has_dma = 1;
1443 dd->caps.has_dualbuff = 1;
1444 dd->caps.has_sha224 = 1;
1445 break;
1446 case 0x320:
1447 break;
1448 default:
1449 dev_warn(dd->dev,
1450 "Unmanaged sha version, set minimum capabilities\n");
1451 break;
1452 }
1453}
1454
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001455#if defined(CONFIG_OF)
1456static const struct of_device_id atmel_sha_dt_ids[] = {
1457 { .compatible = "atmel,at91sam9g46-sha" },
1458 { /* sentinel */ }
1459};
1460
1461MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids);
1462
1463static struct crypto_platform_data *atmel_sha_of_init(struct platform_device *pdev)
1464{
1465 struct device_node *np = pdev->dev.of_node;
1466 struct crypto_platform_data *pdata;
1467
1468 if (!np) {
1469 dev_err(&pdev->dev, "device node not found\n");
1470 return ERR_PTR(-EINVAL);
1471 }
1472
1473 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1474 if (!pdata) {
1475 dev_err(&pdev->dev, "could not allocate memory for pdata\n");
1476 return ERR_PTR(-ENOMEM);
1477 }
1478
1479 pdata->dma_slave = devm_kzalloc(&pdev->dev,
1480 sizeof(*(pdata->dma_slave)),
1481 GFP_KERNEL);
1482 if (!pdata->dma_slave) {
1483 dev_err(&pdev->dev, "could not allocate memory for dma_slave\n");
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001484 return ERR_PTR(-ENOMEM);
1485 }
1486
1487 return pdata;
1488}
1489#else /* CONFIG_OF */
1490static inline struct crypto_platform_data *atmel_sha_of_init(struct platform_device *dev)
1491{
1492 return ERR_PTR(-EINVAL);
1493}
1494#endif
1495
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08001496static int atmel_sha_probe(struct platform_device *pdev)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001497{
1498 struct atmel_sha_dev *sha_dd;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001499 struct crypto_platform_data *pdata;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001500 struct device *dev = &pdev->dev;
1501 struct resource *sha_res;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001502 int err;
1503
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001504 sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001505 if (sha_dd == NULL) {
1506 dev_err(dev, "unable to alloc data struct.\n");
1507 err = -ENOMEM;
1508 goto sha_dd_err;
1509 }
1510
1511 sha_dd->dev = dev;
1512
1513 platform_set_drvdata(pdev, sha_dd);
1514
1515 INIT_LIST_HEAD(&sha_dd->list);
Leilei Zhao62728e82015-04-07 17:45:06 +08001516 spin_lock_init(&sha_dd->lock);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001517
1518 tasklet_init(&sha_dd->done_task, atmel_sha_done_task,
1519 (unsigned long)sha_dd);
Cyrille Pitchenf56809c2016-01-15 15:49:32 +01001520 tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task,
1521 (unsigned long)sha_dd);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001522
1523 crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH);
1524
1525 sha_dd->irq = -1;
1526
1527 /* Get the base address */
1528 sha_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1529 if (!sha_res) {
1530 dev_err(dev, "no MEM resource info\n");
1531 err = -ENODEV;
1532 goto res_err;
1533 }
1534 sha_dd->phys_base = sha_res->start;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001535
1536 /* Get the IRQ */
1537 sha_dd->irq = platform_get_irq(pdev, 0);
1538 if (sha_dd->irq < 0) {
1539 dev_err(dev, "no IRQ resource info\n");
1540 err = sha_dd->irq;
1541 goto res_err;
1542 }
1543
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001544 err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq,
1545 IRQF_SHARED, "atmel-sha", sha_dd);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001546 if (err) {
1547 dev_err(dev, "unable to request sha irq.\n");
1548 goto res_err;
1549 }
1550
1551 /* Initializing the clock */
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001552 sha_dd->iclk = devm_clk_get(&pdev->dev, "sha_clk");
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001553 if (IS_ERR(sha_dd->iclk)) {
Colin Ian Kingbe208352015-02-28 20:40:10 +00001554 dev_err(dev, "clock initialization failed.\n");
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001555 err = PTR_ERR(sha_dd->iclk);
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001556 goto res_err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001557 }
1558
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001559 sha_dd->io_base = devm_ioremap_resource(&pdev->dev, sha_res);
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02001560 if (IS_ERR(sha_dd->io_base)) {
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001561 dev_err(dev, "can't ioremap\n");
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02001562 err = PTR_ERR(sha_dd->io_base);
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001563 goto res_err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001564 }
1565
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001566 err = clk_prepare(sha_dd->iclk);
1567 if (err)
1568 goto res_err;
1569
Nicolas Royerd4905b32013-02-20 17:10:26 +01001570 atmel_sha_hw_version_init(sha_dd);
1571
1572 atmel_sha_get_cap(sha_dd);
1573
1574 if (sha_dd->caps.has_dma) {
1575 pdata = pdev->dev.platform_data;
1576 if (!pdata) {
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001577 pdata = atmel_sha_of_init(pdev);
1578 if (IS_ERR(pdata)) {
1579 dev_err(&pdev->dev, "platform data not available\n");
1580 err = PTR_ERR(pdata);
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001581 goto iclk_unprepare;
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001582 }
1583 }
1584 if (!pdata->dma_slave) {
Nicolas Royerd4905b32013-02-20 17:10:26 +01001585 err = -ENXIO;
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001586 goto iclk_unprepare;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001587 }
1588 err = atmel_sha_dma_init(sha_dd, pdata);
1589 if (err)
1590 goto err_sha_dma;
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001591
1592 dev_info(dev, "using %s for DMA transfers\n",
1593 dma_chan_name(sha_dd->dma_lch_in.chan));
Nicolas Royerd4905b32013-02-20 17:10:26 +01001594 }
1595
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001596 spin_lock(&atmel_sha.lock);
1597 list_add_tail(&sha_dd->list, &atmel_sha.dev_list);
1598 spin_unlock(&atmel_sha.lock);
1599
1600 err = atmel_sha_register_algs(sha_dd);
1601 if (err)
1602 goto err_algs;
1603
Nicolas Ferre1ca5b7d2013-10-15 16:37:44 +02001604 dev_info(dev, "Atmel SHA1/SHA256%s%s\n",
1605 sha_dd->caps.has_sha224 ? "/SHA224" : "",
1606 sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : "");
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001607
1608 return 0;
1609
1610err_algs:
1611 spin_lock(&atmel_sha.lock);
1612 list_del(&sha_dd->list);
1613 spin_unlock(&atmel_sha.lock);
Nicolas Royerd4905b32013-02-20 17:10:26 +01001614 if (sha_dd->caps.has_dma)
1615 atmel_sha_dma_cleanup(sha_dd);
1616err_sha_dma:
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001617iclk_unprepare:
1618 clk_unprepare(sha_dd->iclk);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001619res_err:
Cyrille Pitchenf56809c2016-01-15 15:49:32 +01001620 tasklet_kill(&sha_dd->queue_task);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001621 tasklet_kill(&sha_dd->done_task);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001622sha_dd_err:
1623 dev_err(dev, "initialization failed.\n");
1624
1625 return err;
1626}
1627
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08001628static int atmel_sha_remove(struct platform_device *pdev)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001629{
1630 static struct atmel_sha_dev *sha_dd;
1631
1632 sha_dd = platform_get_drvdata(pdev);
1633 if (!sha_dd)
1634 return -ENODEV;
1635 spin_lock(&atmel_sha.lock);
1636 list_del(&sha_dd->list);
1637 spin_unlock(&atmel_sha.lock);
1638
1639 atmel_sha_unregister_algs(sha_dd);
1640
Cyrille Pitchenf56809c2016-01-15 15:49:32 +01001641 tasklet_kill(&sha_dd->queue_task);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001642 tasklet_kill(&sha_dd->done_task);
1643
Nicolas Royerd4905b32013-02-20 17:10:26 +01001644 if (sha_dd->caps.has_dma)
1645 atmel_sha_dma_cleanup(sha_dd);
1646
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001647 clk_unprepare(sha_dd->iclk);
1648
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001649 return 0;
1650}
1651
1652static struct platform_driver atmel_sha_driver = {
1653 .probe = atmel_sha_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08001654 .remove = atmel_sha_remove,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001655 .driver = {
1656 .name = "atmel_sha",
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001657 .of_match_table = of_match_ptr(atmel_sha_dt_ids),
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001658 },
1659};
1660
1661module_platform_driver(atmel_sha_driver);
1662
Nicolas Royerd4905b32013-02-20 17:10:26 +01001663MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001664MODULE_LICENSE("GPL v2");
1665MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");