blob: be0d72cf4352f3a80510e79a587b27d4aca04f41 [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
Cyrille Pitchen9064ed92017-01-26 17:07:50 +0100437static inline int atmel_sha_wait_for_data_ready(struct atmel_sha_dev *dd,
438 atmel_sha_fn_t resume)
439{
440 u32 isr = atmel_sha_read(dd, SHA_ISR);
441
442 if (unlikely(isr & SHA_INT_DATARDY))
443 return resume(dd);
444
445 dd->resume = resume;
446 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
447 return -EINPROGRESS;
448}
449
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200450static int atmel_sha_xmit_cpu(struct atmel_sha_dev *dd, const u8 *buf,
451 size_t length, int final)
452{
453 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
454 int count, len32;
455 const u32 *buffer = (const u32 *)buf;
456
Nicolas Royerd4905b32013-02-20 17:10:26 +0100457 dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
458 ctx->digcnt[1], ctx->digcnt[0], length, final);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200459
460 atmel_sha_write_ctrl(dd, 0);
461
462 /* should be non-zero before next lines to disable clocks later */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100463 ctx->digcnt[0] += length;
464 if (ctx->digcnt[0] < length)
465 ctx->digcnt[1]++;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200466
467 if (final)
468 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
469
470 len32 = DIV_ROUND_UP(length, sizeof(u32));
471
472 dd->flags |= SHA_FLAGS_CPU;
473
474 for (count = 0; count < len32; count++)
475 atmel_sha_write(dd, SHA_REG_DIN(count), buffer[count]);
476
477 return -EINPROGRESS;
478}
479
480static int atmel_sha_xmit_pdc(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
481 size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
482{
483 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
484 int len32;
485
Nicolas Royerd4905b32013-02-20 17:10:26 +0100486 dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
487 ctx->digcnt[1], ctx->digcnt[0], length1, final);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200488
489 len32 = DIV_ROUND_UP(length1, sizeof(u32));
490 atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS);
491 atmel_sha_write(dd, SHA_TPR, dma_addr1);
492 atmel_sha_write(dd, SHA_TCR, len32);
493
494 len32 = DIV_ROUND_UP(length2, sizeof(u32));
495 atmel_sha_write(dd, SHA_TNPR, dma_addr2);
496 atmel_sha_write(dd, SHA_TNCR, len32);
497
498 atmel_sha_write_ctrl(dd, 1);
499
500 /* should be non-zero before next lines to disable clocks later */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100501 ctx->digcnt[0] += length1;
502 if (ctx->digcnt[0] < length1)
503 ctx->digcnt[1]++;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200504
505 if (final)
506 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
507
508 dd->flags |= SHA_FLAGS_DMA_ACTIVE;
509
510 /* Start DMA transfer */
511 atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTEN);
512
513 return -EINPROGRESS;
514}
515
Nicolas Royerd4905b32013-02-20 17:10:26 +0100516static void atmel_sha_dma_callback(void *data)
517{
518 struct atmel_sha_dev *dd = data;
519
Cyrille Pitchena29af932017-01-26 17:07:47 +0100520 dd->is_async = true;
521
Nicolas Royerd4905b32013-02-20 17:10:26 +0100522 /* dma_lch_in - completed - wait DATRDY */
523 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
524}
525
526static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
527 size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
528{
529 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
530 struct dma_async_tx_descriptor *in_desc;
531 struct scatterlist sg[2];
532
533 dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
534 ctx->digcnt[1], ctx->digcnt[0], length1, final);
535
Leilei Zhao3f1992c2015-04-07 17:45:07 +0800536 dd->dma_lch_in.dma_conf.src_maxburst = 16;
537 dd->dma_lch_in.dma_conf.dst_maxburst = 16;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100538
539 dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
540
541 if (length2) {
542 sg_init_table(sg, 2);
543 sg_dma_address(&sg[0]) = dma_addr1;
544 sg_dma_len(&sg[0]) = length1;
545 sg_dma_address(&sg[1]) = dma_addr2;
546 sg_dma_len(&sg[1]) = length2;
547 in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2,
548 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
549 } else {
550 sg_init_table(sg, 1);
551 sg_dma_address(&sg[0]) = dma_addr1;
552 sg_dma_len(&sg[0]) = length1;
553 in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1,
554 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
555 }
556 if (!in_desc)
Cyrille Pitchena29af932017-01-26 17:07:47 +0100557 atmel_sha_complete(dd, -EINVAL);
Nicolas Royerd4905b32013-02-20 17:10:26 +0100558
559 in_desc->callback = atmel_sha_dma_callback;
560 in_desc->callback_param = dd;
561
562 atmel_sha_write_ctrl(dd, 1);
563
564 /* should be non-zero before next lines to disable clocks later */
565 ctx->digcnt[0] += length1;
566 if (ctx->digcnt[0] < length1)
567 ctx->digcnt[1]++;
568
569 if (final)
570 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
571
572 dd->flags |= SHA_FLAGS_DMA_ACTIVE;
573
574 /* Start DMA transfer */
575 dmaengine_submit(in_desc);
576 dma_async_issue_pending(dd->dma_lch_in.chan);
577
578 return -EINPROGRESS;
579}
580
581static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
582 size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
583{
584 if (dd->caps.has_dma)
585 return atmel_sha_xmit_dma(dd, dma_addr1, length1,
586 dma_addr2, length2, final);
587 else
588 return atmel_sha_xmit_pdc(dd, dma_addr1, length1,
589 dma_addr2, length2, final);
590}
591
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200592static int atmel_sha_update_cpu(struct atmel_sha_dev *dd)
593{
594 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
595 int bufcnt;
596
597 atmel_sha_append_sg(ctx);
598 atmel_sha_fill_padding(ctx, 0);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200599 bufcnt = ctx->bufcnt;
600 ctx->bufcnt = 0;
601
602 return atmel_sha_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
603}
604
605static int atmel_sha_xmit_dma_map(struct atmel_sha_dev *dd,
606 struct atmel_sha_reqctx *ctx,
607 size_t length, int final)
608{
609 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
Nicolas Royerd4905b32013-02-20 17:10:26 +0100610 ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200611 if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
612 dev_err(dd->dev, "dma %u bytes error\n", ctx->buflen +
Nicolas Royerd4905b32013-02-20 17:10:26 +0100613 ctx->block_size);
Cyrille Pitchena29af932017-01-26 17:07:47 +0100614 atmel_sha_complete(dd, -EINVAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200615 }
616
617 ctx->flags &= ~SHA_FLAGS_SG;
618
619 /* next call does not fail... so no unmap in the case of error */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100620 return atmel_sha_xmit_start(dd, ctx->dma_addr, length, 0, 0, final);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200621}
622
623static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd)
624{
625 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
626 unsigned int final;
627 size_t count;
628
629 atmel_sha_append_sg(ctx);
630
631 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
632
Nicolas Royerd4905b32013-02-20 17:10:26 +0100633 dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: 0x%llx 0x%llx, final: %d\n",
634 ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200635
636 if (final)
637 atmel_sha_fill_padding(ctx, 0);
638
Ludovic Desroches00992862015-04-07 17:45:04 +0800639 if (final || (ctx->bufcnt == ctx->buflen)) {
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200640 count = ctx->bufcnt;
641 ctx->bufcnt = 0;
642 return atmel_sha_xmit_dma_map(dd, ctx, count, final);
643 }
644
645 return 0;
646}
647
648static int atmel_sha_update_dma_start(struct atmel_sha_dev *dd)
649{
650 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
651 unsigned int length, final, tail;
652 struct scatterlist *sg;
653 unsigned int count;
654
655 if (!ctx->total)
656 return 0;
657
658 if (ctx->bufcnt || ctx->offset)
659 return atmel_sha_update_dma_slow(dd);
660
Nicolas Royerd4905b32013-02-20 17:10:26 +0100661 dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %u, total: %u\n",
662 ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200663
664 sg = ctx->sg;
665
666 if (!IS_ALIGNED(sg->offset, sizeof(u32)))
667 return atmel_sha_update_dma_slow(dd);
668
Nicolas Royerd4905b32013-02-20 17:10:26 +0100669 if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size))
670 /* size is not ctx->block_size aligned */
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200671 return atmel_sha_update_dma_slow(dd);
672
673 length = min(ctx->total, sg->length);
674
675 if (sg_is_last(sg)) {
676 if (!(ctx->flags & SHA_FLAGS_FINUP)) {
Nicolas Royerd4905b32013-02-20 17:10:26 +0100677 /* not last sg must be ctx->block_size aligned */
678 tail = length & (ctx->block_size - 1);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200679 length -= tail;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200680 }
681 }
682
683 ctx->total -= length;
684 ctx->offset = length; /* offset where to start slow */
685
686 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
687
688 /* Add padding */
689 if (final) {
Nicolas Royerd4905b32013-02-20 17:10:26 +0100690 tail = length & (ctx->block_size - 1);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200691 length -= tail;
692 ctx->total += tail;
693 ctx->offset = length; /* offset where to start slow */
694
695 sg = ctx->sg;
696 atmel_sha_append_sg(ctx);
697
698 atmel_sha_fill_padding(ctx, length);
699
700 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
Nicolas Royerd4905b32013-02-20 17:10:26 +0100701 ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200702 if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
703 dev_err(dd->dev, "dma %u bytes error\n",
Nicolas Royerd4905b32013-02-20 17:10:26 +0100704 ctx->buflen + ctx->block_size);
Cyrille Pitchena29af932017-01-26 17:07:47 +0100705 atmel_sha_complete(dd, -EINVAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200706 }
707
708 if (length == 0) {
709 ctx->flags &= ~SHA_FLAGS_SG;
710 count = ctx->bufcnt;
711 ctx->bufcnt = 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100712 return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0,
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200713 0, final);
714 } else {
715 ctx->sg = sg;
716 if (!dma_map_sg(dd->dev, ctx->sg, 1,
717 DMA_TO_DEVICE)) {
718 dev_err(dd->dev, "dma_map_sg error\n");
Cyrille Pitchena29af932017-01-26 17:07:47 +0100719 atmel_sha_complete(dd, -EINVAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200720 }
721
722 ctx->flags |= SHA_FLAGS_SG;
723
724 count = ctx->bufcnt;
725 ctx->bufcnt = 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100726 return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg),
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200727 length, ctx->dma_addr, count, final);
728 }
729 }
730
731 if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
732 dev_err(dd->dev, "dma_map_sg error\n");
Cyrille Pitchena29af932017-01-26 17:07:47 +0100733 atmel_sha_complete(dd, -EINVAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200734 }
735
736 ctx->flags |= SHA_FLAGS_SG;
737
738 /* next call does not fail... so no unmap in the case of error */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100739 return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), length, 0,
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200740 0, final);
741}
742
743static int atmel_sha_update_dma_stop(struct atmel_sha_dev *dd)
744{
745 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
746
747 if (ctx->flags & SHA_FLAGS_SG) {
748 dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
749 if (ctx->sg->length == ctx->offset) {
750 ctx->sg = sg_next(ctx->sg);
751 if (ctx->sg)
752 ctx->offset = 0;
753 }
Nicolas Royerd4905b32013-02-20 17:10:26 +0100754 if (ctx->flags & SHA_FLAGS_PAD) {
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200755 dma_unmap_single(dd->dev, ctx->dma_addr,
Nicolas Royerd4905b32013-02-20 17:10:26 +0100756 ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
757 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200758 } else {
759 dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen +
Nicolas Royerd4905b32013-02-20 17:10:26 +0100760 ctx->block_size, DMA_TO_DEVICE);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200761 }
762
763 return 0;
764}
765
766static int atmel_sha_update_req(struct atmel_sha_dev *dd)
767{
768 struct ahash_request *req = dd->req;
769 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
770 int err;
771
Nicolas Royerd4905b32013-02-20 17:10:26 +0100772 dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n",
773 ctx->total, ctx->digcnt[1], ctx->digcnt[0]);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200774
775 if (ctx->flags & SHA_FLAGS_CPU)
776 err = atmel_sha_update_cpu(dd);
777 else
778 err = atmel_sha_update_dma_start(dd);
779
780 /* wait for dma completion before can take more data */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100781 dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx\n",
782 err, ctx->digcnt[1], ctx->digcnt[0]);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200783
784 return err;
785}
786
787static int atmel_sha_final_req(struct atmel_sha_dev *dd)
788{
789 struct ahash_request *req = dd->req;
790 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
791 int err = 0;
792 int count;
793
794 if (ctx->bufcnt >= ATMEL_SHA_DMA_THRESHOLD) {
795 atmel_sha_fill_padding(ctx, 0);
796 count = ctx->bufcnt;
797 ctx->bufcnt = 0;
798 err = atmel_sha_xmit_dma_map(dd, ctx, count, 1);
799 }
800 /* faster to handle last block with cpu */
801 else {
802 atmel_sha_fill_padding(ctx, 0);
803 count = ctx->bufcnt;
804 ctx->bufcnt = 0;
805 err = atmel_sha_xmit_cpu(dd, ctx->buffer, count, 1);
806 }
807
808 dev_dbg(dd->dev, "final_req: err: %d\n", err);
809
810 return err;
811}
812
813static void atmel_sha_copy_hash(struct ahash_request *req)
814{
815 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
816 u32 *hash = (u32 *)ctx->digest;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100817 unsigned int i, hashsize;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200818
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100819 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
820 case SHA_FLAGS_SHA1:
821 hashsize = SHA1_DIGEST_SIZE;
822 break;
823
824 case SHA_FLAGS_SHA224:
825 case SHA_FLAGS_SHA256:
826 hashsize = SHA256_DIGEST_SIZE;
827 break;
828
829 case SHA_FLAGS_SHA384:
830 case SHA_FLAGS_SHA512:
831 hashsize = SHA512_DIGEST_SIZE;
832 break;
833
834 default:
835 /* Should not happen... */
836 return;
837 }
838
839 for (i = 0; i < hashsize / sizeof(u32); ++i)
840 hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
841 ctx->flags |= SHA_FLAGS_RESTORE;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200842}
843
844static void atmel_sha_copy_ready_hash(struct ahash_request *req)
845{
846 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
847
848 if (!req->result)
849 return;
850
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100851 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
852 default:
853 case SHA_FLAGS_SHA1:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200854 memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100855 break;
856
857 case SHA_FLAGS_SHA224:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100858 memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100859 break;
860
861 case SHA_FLAGS_SHA256:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200862 memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100863 break;
864
865 case SHA_FLAGS_SHA384:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100866 memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100867 break;
868
869 case SHA_FLAGS_SHA512:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100870 memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100871 break;
872 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200873}
874
875static int atmel_sha_finish(struct ahash_request *req)
876{
877 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
878 struct atmel_sha_dev *dd = ctx->dd;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200879
Nicolas Royerd4905b32013-02-20 17:10:26 +0100880 if (ctx->digcnt[0] || ctx->digcnt[1])
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200881 atmel_sha_copy_ready_hash(req);
882
Nicolas Royerd4905b32013-02-20 17:10:26 +0100883 dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %d\n", ctx->digcnt[1],
884 ctx->digcnt[0], ctx->bufcnt);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200885
Rahul Pathak871b88a2015-12-14 08:44:19 +0000886 return 0;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200887}
888
889static void atmel_sha_finish_req(struct ahash_request *req, int err)
890{
891 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
892 struct atmel_sha_dev *dd = ctx->dd;
893
894 if (!err) {
895 atmel_sha_copy_hash(req);
896 if (SHA_FLAGS_FINAL & dd->flags)
897 err = atmel_sha_finish(req);
898 } else {
899 ctx->flags |= SHA_FLAGS_ERROR;
900 }
901
902 /* atomic operation is not needed here */
Cyrille Pitchena29af932017-01-26 17:07:47 +0100903 (void)atmel_sha_complete(dd, err);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200904}
905
906static int atmel_sha_hw_init(struct atmel_sha_dev *dd)
907{
LABBE Corentin9d83d292015-10-02 14:12:58 +0200908 int err;
909
Cyrille Pitchenc0330422016-02-05 13:45:13 +0100910 err = clk_enable(dd->iclk);
LABBE Corentin9d83d292015-10-02 14:12:58 +0200911 if (err)
912 return err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200913
Nicolas Royerd4905b32013-02-20 17:10:26 +0100914 if (!(SHA_FLAGS_INIT & dd->flags)) {
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200915 atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200916 dd->flags |= SHA_FLAGS_INIT;
917 dd->err = 0;
918 }
919
920 return 0;
921}
922
Nicolas Royerd4905b32013-02-20 17:10:26 +0100923static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd)
924{
925 return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff;
926}
927
928static void atmel_sha_hw_version_init(struct atmel_sha_dev *dd)
929{
930 atmel_sha_hw_init(dd);
931
932 dd->hw_version = atmel_sha_get_version(dd);
933
934 dev_info(dd->dev,
935 "version: 0x%x\n", dd->hw_version);
936
Cyrille Pitchenc0330422016-02-05 13:45:13 +0100937 clk_disable(dd->iclk);
Nicolas Royerd4905b32013-02-20 17:10:26 +0100938}
939
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200940static int atmel_sha_handle_queue(struct atmel_sha_dev *dd,
941 struct ahash_request *req)
942{
943 struct crypto_async_request *async_req, *backlog;
Cyrille Pitchena29af932017-01-26 17:07:47 +0100944 struct atmel_sha_ctx *ctx;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200945 unsigned long flags;
Cyrille Pitchena29af932017-01-26 17:07:47 +0100946 bool start_async;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200947 int err = 0, ret = 0;
948
949 spin_lock_irqsave(&dd->lock, flags);
950 if (req)
951 ret = ahash_enqueue_request(&dd->queue, req);
952
953 if (SHA_FLAGS_BUSY & dd->flags) {
954 spin_unlock_irqrestore(&dd->lock, flags);
955 return ret;
956 }
957
958 backlog = crypto_get_backlog(&dd->queue);
959 async_req = crypto_dequeue_request(&dd->queue);
960 if (async_req)
961 dd->flags |= SHA_FLAGS_BUSY;
962
963 spin_unlock_irqrestore(&dd->lock, flags);
964
965 if (!async_req)
966 return ret;
967
968 if (backlog)
969 backlog->complete(backlog, -EINPROGRESS);
970
Cyrille Pitchena29af932017-01-26 17:07:47 +0100971 ctx = crypto_tfm_ctx(async_req->tfm);
972
973 dd->req = ahash_request_cast(async_req);
974 start_async = (dd->req != req);
975 dd->is_async = start_async;
976
977 /* WARNING: ctx->start() MAY change dd->is_async. */
978 err = ctx->start(dd);
979 return (start_async) ? ret : err;
980}
981
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +0100982static int atmel_sha_done(struct atmel_sha_dev *dd);
983
Cyrille Pitchena29af932017-01-26 17:07:47 +0100984static int atmel_sha_start(struct atmel_sha_dev *dd)
985{
986 struct ahash_request *req = dd->req;
987 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
988 int err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200989
990 dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
991 ctx->op, req->nbytes);
992
993 err = atmel_sha_hw_init(dd);
994
995 if (err)
996 goto err1;
997
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +0100998 dd->resume = atmel_sha_done;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200999 if (ctx->op == SHA_OP_UPDATE) {
1000 err = atmel_sha_update_req(dd);
Nicolas Royerd4905b32013-02-20 17:10:26 +01001001 if (err != -EINPROGRESS && (ctx->flags & SHA_FLAGS_FINUP))
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001002 /* no final() after finup() */
1003 err = atmel_sha_final_req(dd);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001004 } else if (ctx->op == SHA_OP_FINAL) {
1005 err = atmel_sha_final_req(dd);
1006 }
1007
1008err1:
1009 if (err != -EINPROGRESS)
1010 /* done_task will not finish it, so do it here */
1011 atmel_sha_finish_req(req, err);
1012
1013 dev_dbg(dd->dev, "exit, err: %d\n", err);
1014
Cyrille Pitchena29af932017-01-26 17:07:47 +01001015 return err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001016}
1017
1018static int atmel_sha_enqueue(struct ahash_request *req, unsigned int op)
1019{
1020 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1021 struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1022 struct atmel_sha_dev *dd = tctx->dd;
1023
1024 ctx->op = op;
1025
1026 return atmel_sha_handle_queue(dd, req);
1027}
1028
1029static int atmel_sha_update(struct ahash_request *req)
1030{
1031 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1032
1033 if (!req->nbytes)
1034 return 0;
1035
1036 ctx->total = req->nbytes;
1037 ctx->sg = req->src;
1038 ctx->offset = 0;
1039
1040 if (ctx->flags & SHA_FLAGS_FINUP) {
1041 if (ctx->bufcnt + ctx->total < ATMEL_SHA_DMA_THRESHOLD)
1042 /* faster to use CPU for short transfers */
1043 ctx->flags |= SHA_FLAGS_CPU;
1044 } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
1045 atmel_sha_append_sg(ctx);
1046 return 0;
1047 }
1048 return atmel_sha_enqueue(req, SHA_OP_UPDATE);
1049}
1050
1051static int atmel_sha_final(struct ahash_request *req)
1052{
1053 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001054
1055 ctx->flags |= SHA_FLAGS_FINUP;
1056
1057 if (ctx->flags & SHA_FLAGS_ERROR)
1058 return 0; /* uncompleted hash is not needed */
1059
Cyrille Pitchenad841122016-02-08 16:26:49 +01001060 if (ctx->flags & SHA_FLAGS_PAD)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001061 /* copy ready hash (+ finalize hmac) */
1062 return atmel_sha_finish(req);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001063
Cyrille Pitchenad841122016-02-08 16:26:49 +01001064 return atmel_sha_enqueue(req, SHA_OP_FINAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001065}
1066
1067static int atmel_sha_finup(struct ahash_request *req)
1068{
1069 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1070 int err1, err2;
1071
1072 ctx->flags |= SHA_FLAGS_FINUP;
1073
1074 err1 = atmel_sha_update(req);
1075 if (err1 == -EINPROGRESS || err1 == -EBUSY)
1076 return err1;
1077
1078 /*
1079 * final() has to be always called to cleanup resources
1080 * even if udpate() failed, except EINPROGRESS
1081 */
1082 err2 = atmel_sha_final(req);
1083
1084 return err1 ?: err2;
1085}
1086
1087static int atmel_sha_digest(struct ahash_request *req)
1088{
1089 return atmel_sha_init(req) ?: atmel_sha_finup(req);
1090}
1091
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001092
1093static int atmel_sha_export(struct ahash_request *req, void *out)
1094{
1095 const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001096
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001097 memcpy(out, ctx, sizeof(*ctx));
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001098 return 0;
1099}
1100
1101static int atmel_sha_import(struct ahash_request *req, const void *in)
1102{
1103 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001104
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001105 memcpy(ctx, in, sizeof(*ctx));
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001106 return 0;
1107}
1108
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001109static int atmel_sha_cra_init(struct crypto_tfm *tfm)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001110{
Cyrille Pitchena29af932017-01-26 17:07:47 +01001111 struct atmel_sha_ctx *ctx = crypto_tfm_ctx(tfm);
1112
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001113 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001114 sizeof(struct atmel_sha_reqctx));
Cyrille Pitchena29af932017-01-26 17:07:47 +01001115 ctx->start = atmel_sha_start;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001116
1117 return 0;
1118}
1119
Nicolas Royerd4905b32013-02-20 17:10:26 +01001120static struct ahash_alg sha_1_256_algs[] = {
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001121{
1122 .init = atmel_sha_init,
1123 .update = atmel_sha_update,
1124 .final = atmel_sha_final,
1125 .finup = atmel_sha_finup,
1126 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001127 .export = atmel_sha_export,
1128 .import = atmel_sha_import,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001129 .halg = {
1130 .digestsize = SHA1_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001131 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001132 .base = {
1133 .cra_name = "sha1",
1134 .cra_driver_name = "atmel-sha1",
1135 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001136 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001137 .cra_blocksize = SHA1_BLOCK_SIZE,
1138 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1139 .cra_alignmask = 0,
1140 .cra_module = THIS_MODULE,
1141 .cra_init = atmel_sha_cra_init,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001142 }
1143 }
1144},
1145{
1146 .init = atmel_sha_init,
1147 .update = atmel_sha_update,
1148 .final = atmel_sha_final,
1149 .finup = atmel_sha_finup,
1150 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001151 .export = atmel_sha_export,
1152 .import = atmel_sha_import,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001153 .halg = {
1154 .digestsize = SHA256_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001155 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001156 .base = {
1157 .cra_name = "sha256",
1158 .cra_driver_name = "atmel-sha256",
1159 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001160 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001161 .cra_blocksize = SHA256_BLOCK_SIZE,
1162 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1163 .cra_alignmask = 0,
1164 .cra_module = THIS_MODULE,
1165 .cra_init = atmel_sha_cra_init,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001166 }
1167 }
1168},
1169};
1170
Nicolas Royerd4905b32013-02-20 17:10:26 +01001171static struct ahash_alg sha_224_alg = {
1172 .init = atmel_sha_init,
1173 .update = atmel_sha_update,
1174 .final = atmel_sha_final,
1175 .finup = atmel_sha_finup,
1176 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001177 .export = atmel_sha_export,
1178 .import = atmel_sha_import,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001179 .halg = {
1180 .digestsize = SHA224_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001181 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerd4905b32013-02-20 17:10:26 +01001182 .base = {
1183 .cra_name = "sha224",
1184 .cra_driver_name = "atmel-sha224",
1185 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001186 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001187 .cra_blocksize = SHA224_BLOCK_SIZE,
1188 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1189 .cra_alignmask = 0,
1190 .cra_module = THIS_MODULE,
1191 .cra_init = atmel_sha_cra_init,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001192 }
1193 }
1194};
1195
1196static struct ahash_alg sha_384_512_algs[] = {
1197{
1198 .init = atmel_sha_init,
1199 .update = atmel_sha_update,
1200 .final = atmel_sha_final,
1201 .finup = atmel_sha_finup,
1202 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001203 .export = atmel_sha_export,
1204 .import = atmel_sha_import,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001205 .halg = {
1206 .digestsize = SHA384_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001207 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerd4905b32013-02-20 17:10:26 +01001208 .base = {
1209 .cra_name = "sha384",
1210 .cra_driver_name = "atmel-sha384",
1211 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001212 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001213 .cra_blocksize = SHA384_BLOCK_SIZE,
1214 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1215 .cra_alignmask = 0x3,
1216 .cra_module = THIS_MODULE,
1217 .cra_init = atmel_sha_cra_init,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001218 }
1219 }
1220},
1221{
1222 .init = atmel_sha_init,
1223 .update = atmel_sha_update,
1224 .final = atmel_sha_final,
1225 .finup = atmel_sha_finup,
1226 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001227 .export = atmel_sha_export,
1228 .import = atmel_sha_import,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001229 .halg = {
1230 .digestsize = SHA512_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001231 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerd4905b32013-02-20 17:10:26 +01001232 .base = {
1233 .cra_name = "sha512",
1234 .cra_driver_name = "atmel-sha512",
1235 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001236 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001237 .cra_blocksize = SHA512_BLOCK_SIZE,
1238 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1239 .cra_alignmask = 0x3,
1240 .cra_module = THIS_MODULE,
1241 .cra_init = atmel_sha_cra_init,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001242 }
1243 }
1244},
1245};
1246
Cyrille Pitchenf56809c2016-01-15 15:49:32 +01001247static void atmel_sha_queue_task(unsigned long data)
1248{
1249 struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
1250
1251 atmel_sha_handle_queue(dd, NULL);
1252}
1253
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +01001254static int atmel_sha_done(struct atmel_sha_dev *dd)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001255{
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001256 int err = 0;
1257
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001258 if (SHA_FLAGS_CPU & dd->flags) {
1259 if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
1260 dd->flags &= ~SHA_FLAGS_OUTPUT_READY;
1261 goto finish;
1262 }
1263 } else if (SHA_FLAGS_DMA_READY & dd->flags) {
1264 if (SHA_FLAGS_DMA_ACTIVE & dd->flags) {
1265 dd->flags &= ~SHA_FLAGS_DMA_ACTIVE;
1266 atmel_sha_update_dma_stop(dd);
1267 if (dd->err) {
1268 err = dd->err;
1269 goto finish;
1270 }
1271 }
1272 if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
1273 /* hash or semi-hash ready */
1274 dd->flags &= ~(SHA_FLAGS_DMA_READY |
1275 SHA_FLAGS_OUTPUT_READY);
1276 err = atmel_sha_update_dma_start(dd);
1277 if (err != -EINPROGRESS)
1278 goto finish;
1279 }
1280 }
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +01001281 return err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001282
1283finish:
1284 /* finish curent request */
1285 atmel_sha_finish_req(dd->req, err);
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +01001286
1287 return err;
1288}
1289
1290static void atmel_sha_done_task(unsigned long data)
1291{
1292 struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
1293
1294 dd->is_async = true;
1295 (void)dd->resume(dd);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001296}
1297
1298static irqreturn_t atmel_sha_irq(int irq, void *dev_id)
1299{
1300 struct atmel_sha_dev *sha_dd = dev_id;
1301 u32 reg;
1302
1303 reg = atmel_sha_read(sha_dd, SHA_ISR);
1304 if (reg & atmel_sha_read(sha_dd, SHA_IMR)) {
1305 atmel_sha_write(sha_dd, SHA_IDR, reg);
1306 if (SHA_FLAGS_BUSY & sha_dd->flags) {
1307 sha_dd->flags |= SHA_FLAGS_OUTPUT_READY;
1308 if (!(SHA_FLAGS_CPU & sha_dd->flags))
1309 sha_dd->flags |= SHA_FLAGS_DMA_READY;
1310 tasklet_schedule(&sha_dd->done_task);
1311 } else {
1312 dev_warn(sha_dd->dev, "SHA interrupt when no active requests.\n");
1313 }
1314 return IRQ_HANDLED;
1315 }
1316
1317 return IRQ_NONE;
1318}
1319
1320static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd)
1321{
1322 int i;
1323
Nicolas Royerd4905b32013-02-20 17:10:26 +01001324 for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++)
1325 crypto_unregister_ahash(&sha_1_256_algs[i]);
1326
1327 if (dd->caps.has_sha224)
1328 crypto_unregister_ahash(&sha_224_alg);
1329
1330 if (dd->caps.has_sha_384_512) {
1331 for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++)
1332 crypto_unregister_ahash(&sha_384_512_algs[i]);
1333 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001334}
1335
1336static int atmel_sha_register_algs(struct atmel_sha_dev *dd)
1337{
1338 int err, i, j;
1339
Nicolas Royerd4905b32013-02-20 17:10:26 +01001340 for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) {
1341 err = crypto_register_ahash(&sha_1_256_algs[i]);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001342 if (err)
Nicolas Royerd4905b32013-02-20 17:10:26 +01001343 goto err_sha_1_256_algs;
1344 }
1345
1346 if (dd->caps.has_sha224) {
1347 err = crypto_register_ahash(&sha_224_alg);
1348 if (err)
1349 goto err_sha_224_algs;
1350 }
1351
1352 if (dd->caps.has_sha_384_512) {
1353 for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) {
1354 err = crypto_register_ahash(&sha_384_512_algs[i]);
1355 if (err)
1356 goto err_sha_384_512_algs;
1357 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001358 }
1359
1360 return 0;
1361
Nicolas Royerd4905b32013-02-20 17:10:26 +01001362err_sha_384_512_algs:
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001363 for (j = 0; j < i; j++)
Nicolas Royerd4905b32013-02-20 17:10:26 +01001364 crypto_unregister_ahash(&sha_384_512_algs[j]);
1365 crypto_unregister_ahash(&sha_224_alg);
1366err_sha_224_algs:
1367 i = ARRAY_SIZE(sha_1_256_algs);
1368err_sha_1_256_algs:
1369 for (j = 0; j < i; j++)
1370 crypto_unregister_ahash(&sha_1_256_algs[j]);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001371
1372 return err;
1373}
1374
Nicolas Royerd4905b32013-02-20 17:10:26 +01001375static bool atmel_sha_filter(struct dma_chan *chan, void *slave)
1376{
1377 struct at_dma_slave *sl = slave;
1378
1379 if (sl && sl->dma_dev == chan->device->dev) {
1380 chan->private = sl;
1381 return true;
1382 } else {
1383 return false;
1384 }
1385}
1386
1387static int atmel_sha_dma_init(struct atmel_sha_dev *dd,
1388 struct crypto_platform_data *pdata)
1389{
1390 int err = -ENOMEM;
1391 dma_cap_mask_t mask_in;
1392
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001393 /* Try to grab DMA channel */
1394 dma_cap_zero(mask_in);
1395 dma_cap_set(DMA_SLAVE, mask_in);
Nicolas Royerd4905b32013-02-20 17:10:26 +01001396
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001397 dd->dma_lch_in.chan = dma_request_slave_channel_compat(mask_in,
1398 atmel_sha_filter, &pdata->dma_slave->rxdata, dd->dev, "tx");
1399 if (!dd->dma_lch_in.chan) {
1400 dev_warn(dd->dev, "no DMA channel available\n");
1401 return err;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001402 }
1403
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001404 dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV;
1405 dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
1406 SHA_REG_DIN(0);
1407 dd->dma_lch_in.dma_conf.src_maxburst = 1;
1408 dd->dma_lch_in.dma_conf.src_addr_width =
1409 DMA_SLAVE_BUSWIDTH_4_BYTES;
1410 dd->dma_lch_in.dma_conf.dst_maxburst = 1;
1411 dd->dma_lch_in.dma_conf.dst_addr_width =
1412 DMA_SLAVE_BUSWIDTH_4_BYTES;
1413 dd->dma_lch_in.dma_conf.device_fc = false;
1414
1415 return 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001416}
1417
1418static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd)
1419{
1420 dma_release_channel(dd->dma_lch_in.chan);
1421}
1422
1423static void atmel_sha_get_cap(struct atmel_sha_dev *dd)
1424{
1425
1426 dd->caps.has_dma = 0;
1427 dd->caps.has_dualbuff = 0;
1428 dd->caps.has_sha224 = 0;
1429 dd->caps.has_sha_384_512 = 0;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +01001430 dd->caps.has_uihv = 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001431
1432 /* keep only major version number */
1433 switch (dd->hw_version & 0xff0) {
Cyrille Pitchen507c5cc2016-01-15 15:49:33 +01001434 case 0x510:
1435 dd->caps.has_dma = 1;
1436 dd->caps.has_dualbuff = 1;
1437 dd->caps.has_sha224 = 1;
1438 dd->caps.has_sha_384_512 = 1;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +01001439 dd->caps.has_uihv = 1;
Cyrille Pitchen507c5cc2016-01-15 15:49:33 +01001440 break;
Leilei Zhao141824d2015-04-07 17:45:03 +08001441 case 0x420:
1442 dd->caps.has_dma = 1;
1443 dd->caps.has_dualbuff = 1;
1444 dd->caps.has_sha224 = 1;
1445 dd->caps.has_sha_384_512 = 1;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +01001446 dd->caps.has_uihv = 1;
Leilei Zhao141824d2015-04-07 17:45:03 +08001447 break;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001448 case 0x410:
1449 dd->caps.has_dma = 1;
1450 dd->caps.has_dualbuff = 1;
1451 dd->caps.has_sha224 = 1;
1452 dd->caps.has_sha_384_512 = 1;
1453 break;
1454 case 0x400:
1455 dd->caps.has_dma = 1;
1456 dd->caps.has_dualbuff = 1;
1457 dd->caps.has_sha224 = 1;
1458 break;
1459 case 0x320:
1460 break;
1461 default:
1462 dev_warn(dd->dev,
1463 "Unmanaged sha version, set minimum capabilities\n");
1464 break;
1465 }
1466}
1467
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001468#if defined(CONFIG_OF)
1469static const struct of_device_id atmel_sha_dt_ids[] = {
1470 { .compatible = "atmel,at91sam9g46-sha" },
1471 { /* sentinel */ }
1472};
1473
1474MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids);
1475
1476static struct crypto_platform_data *atmel_sha_of_init(struct platform_device *pdev)
1477{
1478 struct device_node *np = pdev->dev.of_node;
1479 struct crypto_platform_data *pdata;
1480
1481 if (!np) {
1482 dev_err(&pdev->dev, "device node not found\n");
1483 return ERR_PTR(-EINVAL);
1484 }
1485
1486 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1487 if (!pdata) {
1488 dev_err(&pdev->dev, "could not allocate memory for pdata\n");
1489 return ERR_PTR(-ENOMEM);
1490 }
1491
1492 pdata->dma_slave = devm_kzalloc(&pdev->dev,
1493 sizeof(*(pdata->dma_slave)),
1494 GFP_KERNEL);
1495 if (!pdata->dma_slave) {
1496 dev_err(&pdev->dev, "could not allocate memory for dma_slave\n");
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001497 return ERR_PTR(-ENOMEM);
1498 }
1499
1500 return pdata;
1501}
1502#else /* CONFIG_OF */
1503static inline struct crypto_platform_data *atmel_sha_of_init(struct platform_device *dev)
1504{
1505 return ERR_PTR(-EINVAL);
1506}
1507#endif
1508
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08001509static int atmel_sha_probe(struct platform_device *pdev)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001510{
1511 struct atmel_sha_dev *sha_dd;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001512 struct crypto_platform_data *pdata;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001513 struct device *dev = &pdev->dev;
1514 struct resource *sha_res;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001515 int err;
1516
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001517 sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001518 if (sha_dd == NULL) {
1519 dev_err(dev, "unable to alloc data struct.\n");
1520 err = -ENOMEM;
1521 goto sha_dd_err;
1522 }
1523
1524 sha_dd->dev = dev;
1525
1526 platform_set_drvdata(pdev, sha_dd);
1527
1528 INIT_LIST_HEAD(&sha_dd->list);
Leilei Zhao62728e82015-04-07 17:45:06 +08001529 spin_lock_init(&sha_dd->lock);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001530
1531 tasklet_init(&sha_dd->done_task, atmel_sha_done_task,
1532 (unsigned long)sha_dd);
Cyrille Pitchenf56809c2016-01-15 15:49:32 +01001533 tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task,
1534 (unsigned long)sha_dd);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001535
1536 crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH);
1537
1538 sha_dd->irq = -1;
1539
1540 /* Get the base address */
1541 sha_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1542 if (!sha_res) {
1543 dev_err(dev, "no MEM resource info\n");
1544 err = -ENODEV;
1545 goto res_err;
1546 }
1547 sha_dd->phys_base = sha_res->start;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001548
1549 /* Get the IRQ */
1550 sha_dd->irq = platform_get_irq(pdev, 0);
1551 if (sha_dd->irq < 0) {
1552 dev_err(dev, "no IRQ resource info\n");
1553 err = sha_dd->irq;
1554 goto res_err;
1555 }
1556
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001557 err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq,
1558 IRQF_SHARED, "atmel-sha", sha_dd);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001559 if (err) {
1560 dev_err(dev, "unable to request sha irq.\n");
1561 goto res_err;
1562 }
1563
1564 /* Initializing the clock */
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001565 sha_dd->iclk = devm_clk_get(&pdev->dev, "sha_clk");
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001566 if (IS_ERR(sha_dd->iclk)) {
Colin Ian Kingbe208352015-02-28 20:40:10 +00001567 dev_err(dev, "clock initialization failed.\n");
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001568 err = PTR_ERR(sha_dd->iclk);
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001569 goto res_err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001570 }
1571
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001572 sha_dd->io_base = devm_ioremap_resource(&pdev->dev, sha_res);
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02001573 if (IS_ERR(sha_dd->io_base)) {
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001574 dev_err(dev, "can't ioremap\n");
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02001575 err = PTR_ERR(sha_dd->io_base);
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001576 goto res_err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001577 }
1578
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001579 err = clk_prepare(sha_dd->iclk);
1580 if (err)
1581 goto res_err;
1582
Nicolas Royerd4905b32013-02-20 17:10:26 +01001583 atmel_sha_hw_version_init(sha_dd);
1584
1585 atmel_sha_get_cap(sha_dd);
1586
1587 if (sha_dd->caps.has_dma) {
1588 pdata = pdev->dev.platform_data;
1589 if (!pdata) {
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001590 pdata = atmel_sha_of_init(pdev);
1591 if (IS_ERR(pdata)) {
1592 dev_err(&pdev->dev, "platform data not available\n");
1593 err = PTR_ERR(pdata);
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001594 goto iclk_unprepare;
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001595 }
1596 }
1597 if (!pdata->dma_slave) {
Nicolas Royerd4905b32013-02-20 17:10:26 +01001598 err = -ENXIO;
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001599 goto iclk_unprepare;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001600 }
1601 err = atmel_sha_dma_init(sha_dd, pdata);
1602 if (err)
1603 goto err_sha_dma;
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001604
1605 dev_info(dev, "using %s for DMA transfers\n",
1606 dma_chan_name(sha_dd->dma_lch_in.chan));
Nicolas Royerd4905b32013-02-20 17:10:26 +01001607 }
1608
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001609 spin_lock(&atmel_sha.lock);
1610 list_add_tail(&sha_dd->list, &atmel_sha.dev_list);
1611 spin_unlock(&atmel_sha.lock);
1612
1613 err = atmel_sha_register_algs(sha_dd);
1614 if (err)
1615 goto err_algs;
1616
Nicolas Ferre1ca5b7d2013-10-15 16:37:44 +02001617 dev_info(dev, "Atmel SHA1/SHA256%s%s\n",
1618 sha_dd->caps.has_sha224 ? "/SHA224" : "",
1619 sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : "");
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001620
1621 return 0;
1622
1623err_algs:
1624 spin_lock(&atmel_sha.lock);
1625 list_del(&sha_dd->list);
1626 spin_unlock(&atmel_sha.lock);
Nicolas Royerd4905b32013-02-20 17:10:26 +01001627 if (sha_dd->caps.has_dma)
1628 atmel_sha_dma_cleanup(sha_dd);
1629err_sha_dma:
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001630iclk_unprepare:
1631 clk_unprepare(sha_dd->iclk);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001632res_err:
Cyrille Pitchenf56809c2016-01-15 15:49:32 +01001633 tasklet_kill(&sha_dd->queue_task);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001634 tasklet_kill(&sha_dd->done_task);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001635sha_dd_err:
1636 dev_err(dev, "initialization failed.\n");
1637
1638 return err;
1639}
1640
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08001641static int atmel_sha_remove(struct platform_device *pdev)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001642{
1643 static struct atmel_sha_dev *sha_dd;
1644
1645 sha_dd = platform_get_drvdata(pdev);
1646 if (!sha_dd)
1647 return -ENODEV;
1648 spin_lock(&atmel_sha.lock);
1649 list_del(&sha_dd->list);
1650 spin_unlock(&atmel_sha.lock);
1651
1652 atmel_sha_unregister_algs(sha_dd);
1653
Cyrille Pitchenf56809c2016-01-15 15:49:32 +01001654 tasklet_kill(&sha_dd->queue_task);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001655 tasklet_kill(&sha_dd->done_task);
1656
Nicolas Royerd4905b32013-02-20 17:10:26 +01001657 if (sha_dd->caps.has_dma)
1658 atmel_sha_dma_cleanup(sha_dd);
1659
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001660 clk_unprepare(sha_dd->iclk);
1661
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001662 return 0;
1663}
1664
1665static struct platform_driver atmel_sha_driver = {
1666 .probe = atmel_sha_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08001667 .remove = atmel_sha_remove,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001668 .driver = {
1669 .name = "atmel_sha",
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001670 .of_match_table = of_match_ptr(atmel_sha_dt_ids),
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001671 },
1672};
1673
1674module_platform_driver(atmel_sha_driver);
1675
Nicolas Royerd4905b32013-02-20 17:10:26 +01001676MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001677MODULE_LICENSE("GPL v2");
1678MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");