blob: a4fc60b670996f12a309f6ced3d790574372f87e [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)
Cyrille Pitcheneec12f62017-01-26 17:07:52 +010067#define SHA_FLAGS_IDATAR0 BIT(26)
68#define SHA_FLAGS_WAIT_DATARDY BIT(27)
Nicolas Royerebc82ef2012-07-01 19:19:46 +020069
70#define SHA_OP_UPDATE 1
71#define SHA_OP_FINAL 2
72
Cyrille Pitchencc831d32016-01-29 17:04:02 +010073#define SHA_BUFFER_LEN (PAGE_SIZE / 16)
Nicolas Royerebc82ef2012-07-01 19:19:46 +020074
75#define ATMEL_SHA_DMA_THRESHOLD 56
76
Nicolas Royerd4905b32013-02-20 17:10:26 +010077struct atmel_sha_caps {
78 bool has_dma;
79 bool has_dualbuff;
80 bool has_sha224;
81 bool has_sha_384_512;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +010082 bool has_uihv;
Nicolas Royerd4905b32013-02-20 17:10:26 +010083};
Nicolas Royerebc82ef2012-07-01 19:19:46 +020084
85struct atmel_sha_dev;
86
Cyrille Pitchencc831d32016-01-29 17:04:02 +010087/*
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +010088 * .statesize = sizeof(struct atmel_sha_reqctx) must be <= PAGE_SIZE / 8 as
Cyrille Pitchencc831d32016-01-29 17:04:02 +010089 * tested by the ahash_prepare_alg() function.
90 */
Nicolas Royerebc82ef2012-07-01 19:19:46 +020091struct atmel_sha_reqctx {
92 struct atmel_sha_dev *dd;
93 unsigned long flags;
94 unsigned long op;
95
Nicolas Royerd4905b32013-02-20 17:10:26 +010096 u8 digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32));
97 u64 digcnt[2];
Nicolas Royerebc82ef2012-07-01 19:19:46 +020098 size_t bufcnt;
99 size_t buflen;
100 dma_addr_t dma_addr;
101
102 /* walk state */
103 struct scatterlist *sg;
104 unsigned int offset; /* offset in current sg */
105 unsigned int total; /* total request */
106
Nicolas Royerd4905b32013-02-20 17:10:26 +0100107 size_t block_size;
108
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +0100109 u8 buffer[SHA_BUFFER_LEN + SHA512_BLOCK_SIZE] __aligned(sizeof(u32));
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200110};
111
Cyrille Pitchena29af932017-01-26 17:07:47 +0100112typedef int (*atmel_sha_fn_t)(struct atmel_sha_dev *);
113
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200114struct atmel_sha_ctx {
115 struct atmel_sha_dev *dd;
Cyrille Pitchena29af932017-01-26 17:07:47 +0100116 atmel_sha_fn_t start;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200117
118 unsigned long flags;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200119};
120
Nicolas Royerd4905b32013-02-20 17:10:26 +0100121#define ATMEL_SHA_QUEUE_LENGTH 50
122
123struct atmel_sha_dma {
124 struct dma_chan *chan;
125 struct dma_slave_config dma_conf;
Cyrille Pitchen69303cf2017-01-26 17:07:53 +0100126 struct scatterlist *sg;
127 int nents;
128 unsigned int last_sg_length;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100129};
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200130
131struct atmel_sha_dev {
132 struct list_head list;
133 unsigned long phys_base;
134 struct device *dev;
135 struct clk *iclk;
136 int irq;
137 void __iomem *io_base;
138
139 spinlock_t lock;
140 int err;
141 struct tasklet_struct done_task;
Cyrille Pitchenf56809c2016-01-15 15:49:32 +0100142 struct tasklet_struct queue_task;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200143
144 unsigned long flags;
145 struct crypto_queue queue;
146 struct ahash_request *req;
Cyrille Pitchena29af932017-01-26 17:07:47 +0100147 bool is_async;
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +0100148 atmel_sha_fn_t resume;
Cyrille Pitcheneec12f62017-01-26 17:07:52 +0100149 atmel_sha_fn_t cpu_transfer_complete;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100150
151 struct atmel_sha_dma dma_lch_in;
152
153 struct atmel_sha_caps caps;
154
155 u32 hw_version;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200156};
157
158struct atmel_sha_drv {
159 struct list_head dev_list;
160 spinlock_t lock;
161};
162
163static struct atmel_sha_drv atmel_sha = {
164 .dev_list = LIST_HEAD_INIT(atmel_sha.dev_list),
165 .lock = __SPIN_LOCK_UNLOCKED(atmel_sha.lock),
166};
167
168static inline u32 atmel_sha_read(struct atmel_sha_dev *dd, u32 offset)
169{
170 return readl_relaxed(dd->io_base + offset);
171}
172
173static inline void atmel_sha_write(struct atmel_sha_dev *dd,
174 u32 offset, u32 value)
175{
176 writel_relaxed(value, dd->io_base + offset);
177}
178
Cyrille Pitchena29af932017-01-26 17:07:47 +0100179static inline int atmel_sha_complete(struct atmel_sha_dev *dd, int err)
180{
181 struct ahash_request *req = dd->req;
182
183 dd->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL | SHA_FLAGS_CPU |
184 SHA_FLAGS_DMA_READY | SHA_FLAGS_OUTPUT_READY);
185
186 clk_disable(dd->iclk);
187
188 if (dd->is_async && req->base.complete)
189 req->base.complete(&req->base, err);
190
191 /* handle new request */
192 tasklet_schedule(&dd->queue_task);
193
194 return err;
195}
196
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200197static size_t atmel_sha_append_sg(struct atmel_sha_reqctx *ctx)
198{
199 size_t count;
200
201 while ((ctx->bufcnt < ctx->buflen) && ctx->total) {
202 count = min(ctx->sg->length - ctx->offset, ctx->total);
203 count = min(count, ctx->buflen - ctx->bufcnt);
204
Leilei Zhao803eeae2015-04-07 17:45:05 +0800205 if (count <= 0) {
206 /*
207 * Check if count <= 0 because the buffer is full or
208 * because the sg length is 0. In the latest case,
209 * check if there is another sg in the list, a 0 length
210 * sg doesn't necessarily mean the end of the sg list.
211 */
212 if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) {
213 ctx->sg = sg_next(ctx->sg);
214 continue;
215 } else {
216 break;
217 }
218 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200219
220 scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg,
221 ctx->offset, count, 0);
222
223 ctx->bufcnt += count;
224 ctx->offset += count;
225 ctx->total -= count;
226
227 if (ctx->offset == ctx->sg->length) {
228 ctx->sg = sg_next(ctx->sg);
229 if (ctx->sg)
230 ctx->offset = 0;
231 else
232 ctx->total = 0;
233 }
234 }
235
236 return 0;
237}
238
239/*
Nicolas Royerd4905b32013-02-20 17:10:26 +0100240 * The purpose of this padding is to ensure that the padded message is a
241 * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
242 * The bit "1" is appended at the end of the message followed by
243 * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
244 * 128 bits block (SHA384/SHA512) equals to the message length in bits
245 * is appended.
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200246 *
Nicolas Royerd4905b32013-02-20 17:10:26 +0100247 * For SHA1/SHA224/SHA256, padlen is calculated as followed:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200248 * - if message length < 56 bytes then padlen = 56 - message length
249 * - else padlen = 64 + 56 - message length
Nicolas Royerd4905b32013-02-20 17:10:26 +0100250 *
251 * For SHA384/SHA512, padlen is calculated as followed:
252 * - if message length < 112 bytes then padlen = 112 - message length
253 * - else padlen = 128 + 112 - message length
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200254 */
255static void atmel_sha_fill_padding(struct atmel_sha_reqctx *ctx, int length)
256{
257 unsigned int index, padlen;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100258 u64 bits[2];
259 u64 size[2];
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200260
Nicolas Royerd4905b32013-02-20 17:10:26 +0100261 size[0] = ctx->digcnt[0];
262 size[1] = ctx->digcnt[1];
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200263
Nicolas Royerd4905b32013-02-20 17:10:26 +0100264 size[0] += ctx->bufcnt;
265 if (size[0] < ctx->bufcnt)
266 size[1]++;
267
268 size[0] += length;
269 if (size[0] < length)
270 size[1]++;
271
272 bits[1] = cpu_to_be64(size[0] << 3);
273 bits[0] = cpu_to_be64(size[1] << 3 | size[0] >> 61);
274
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100275 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
276 case SHA_FLAGS_SHA384:
277 case SHA_FLAGS_SHA512:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100278 index = ctx->bufcnt & 0x7f;
279 padlen = (index < 112) ? (112 - index) : ((128+112) - index);
280 *(ctx->buffer + ctx->bufcnt) = 0x80;
281 memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
282 memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16);
283 ctx->bufcnt += padlen + 16;
284 ctx->flags |= SHA_FLAGS_PAD;
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100285 break;
286
287 default:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100288 index = ctx->bufcnt & 0x3f;
289 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
290 *(ctx->buffer + ctx->bufcnt) = 0x80;
291 memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
292 memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8);
293 ctx->bufcnt += padlen + 8;
294 ctx->flags |= SHA_FLAGS_PAD;
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100295 break;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100296 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200297}
298
Cyrille Pitchen8340c7f2017-01-26 17:07:46 +0100299static struct atmel_sha_dev *atmel_sha_find_dev(struct atmel_sha_ctx *tctx)
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200300{
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200301 struct atmel_sha_dev *dd = NULL;
302 struct atmel_sha_dev *tmp;
303
304 spin_lock_bh(&atmel_sha.lock);
305 if (!tctx->dd) {
306 list_for_each_entry(tmp, &atmel_sha.dev_list, list) {
307 dd = tmp;
308 break;
309 }
310 tctx->dd = dd;
311 } else {
312 dd = tctx->dd;
313 }
314
315 spin_unlock_bh(&atmel_sha.lock);
316
Cyrille Pitchen8340c7f2017-01-26 17:07:46 +0100317 return dd;
318}
319
320static int atmel_sha_init(struct ahash_request *req)
321{
322 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
323 struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
324 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
325 struct atmel_sha_dev *dd = atmel_sha_find_dev(tctx);
326
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200327 ctx->dd = dd;
328
329 ctx->flags = 0;
330
331 dev_dbg(dd->dev, "init: digest size: %d\n",
332 crypto_ahash_digestsize(tfm));
333
Nicolas Royerd4905b32013-02-20 17:10:26 +0100334 switch (crypto_ahash_digestsize(tfm)) {
335 case SHA1_DIGEST_SIZE:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200336 ctx->flags |= SHA_FLAGS_SHA1;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100337 ctx->block_size = SHA1_BLOCK_SIZE;
338 break;
339 case SHA224_DIGEST_SIZE:
340 ctx->flags |= SHA_FLAGS_SHA224;
341 ctx->block_size = SHA224_BLOCK_SIZE;
342 break;
343 case SHA256_DIGEST_SIZE:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200344 ctx->flags |= SHA_FLAGS_SHA256;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100345 ctx->block_size = SHA256_BLOCK_SIZE;
346 break;
347 case SHA384_DIGEST_SIZE:
348 ctx->flags |= SHA_FLAGS_SHA384;
349 ctx->block_size = SHA384_BLOCK_SIZE;
350 break;
351 case SHA512_DIGEST_SIZE:
352 ctx->flags |= SHA_FLAGS_SHA512;
353 ctx->block_size = SHA512_BLOCK_SIZE;
354 break;
355 default:
356 return -EINVAL;
357 break;
358 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200359
360 ctx->bufcnt = 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100361 ctx->digcnt[0] = 0;
362 ctx->digcnt[1] = 0;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200363 ctx->buflen = SHA_BUFFER_LEN;
364
365 return 0;
366}
367
368static void atmel_sha_write_ctrl(struct atmel_sha_dev *dd, int dma)
369{
370 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100371 u32 valmr = SHA_MR_MODE_AUTO;
372 unsigned int i, hashsize = 0;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200373
374 if (likely(dma)) {
Nicolas Royerd4905b32013-02-20 17:10:26 +0100375 if (!dd->caps.has_dma)
376 atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200377 valmr = SHA_MR_MODE_PDC;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100378 if (dd->caps.has_dualbuff)
379 valmr |= SHA_MR_DUALBUFF;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200380 } else {
381 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
382 }
383
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100384 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
385 case SHA_FLAGS_SHA1:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100386 valmr |= SHA_MR_ALGO_SHA1;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100387 hashsize = SHA1_DIGEST_SIZE;
388 break;
389
390 case SHA_FLAGS_SHA224:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100391 valmr |= SHA_MR_ALGO_SHA224;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100392 hashsize = SHA256_DIGEST_SIZE;
393 break;
394
395 case SHA_FLAGS_SHA256:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200396 valmr |= SHA_MR_ALGO_SHA256;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100397 hashsize = SHA256_DIGEST_SIZE;
398 break;
399
400 case SHA_FLAGS_SHA384:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100401 valmr |= SHA_MR_ALGO_SHA384;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100402 hashsize = SHA512_DIGEST_SIZE;
403 break;
404
405 case SHA_FLAGS_SHA512:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100406 valmr |= SHA_MR_ALGO_SHA512;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100407 hashsize = SHA512_DIGEST_SIZE;
408 break;
409
410 default:
411 break;
412 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200413
414 /* Setting CR_FIRST only for the first iteration */
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100415 if (!(ctx->digcnt[0] || ctx->digcnt[1])) {
416 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
417 } else if (dd->caps.has_uihv && (ctx->flags & SHA_FLAGS_RESTORE)) {
418 const u32 *hash = (const u32 *)ctx->digest;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200419
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100420 /*
421 * Restore the hardware context: update the User Initialize
422 * Hash Value (UIHV) with the value saved when the latest
423 * 'update' operation completed on this very same crypto
424 * request.
425 */
426 ctx->flags &= ~SHA_FLAGS_RESTORE;
427 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
428 for (i = 0; i < hashsize / sizeof(u32); ++i)
429 atmel_sha_write(dd, SHA_REG_DIN(i), hash[i]);
430 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
431 valmr |= SHA_MR_UIHV;
432 }
433 /*
434 * WARNING: If the UIHV feature is not available, the hardware CANNOT
435 * process concurrent requests: the internal registers used to store
436 * the hash/digest are still set to the partial digest output values
437 * computed during the latest round.
438 */
439
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200440 atmel_sha_write(dd, SHA_MR, valmr);
441}
442
Cyrille Pitchen9064ed92017-01-26 17:07:50 +0100443static inline int atmel_sha_wait_for_data_ready(struct atmel_sha_dev *dd,
444 atmel_sha_fn_t resume)
445{
446 u32 isr = atmel_sha_read(dd, SHA_ISR);
447
448 if (unlikely(isr & SHA_INT_DATARDY))
449 return resume(dd);
450
451 dd->resume = resume;
452 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
453 return -EINPROGRESS;
454}
455
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200456static int atmel_sha_xmit_cpu(struct atmel_sha_dev *dd, const u8 *buf,
457 size_t length, int final)
458{
459 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
460 int count, len32;
461 const u32 *buffer = (const u32 *)buf;
462
Nicolas Royerd4905b32013-02-20 17:10:26 +0100463 dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
464 ctx->digcnt[1], ctx->digcnt[0], length, final);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200465
466 atmel_sha_write_ctrl(dd, 0);
467
468 /* should be non-zero before next lines to disable clocks later */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100469 ctx->digcnt[0] += length;
470 if (ctx->digcnt[0] < length)
471 ctx->digcnt[1]++;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200472
473 if (final)
474 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
475
476 len32 = DIV_ROUND_UP(length, sizeof(u32));
477
478 dd->flags |= SHA_FLAGS_CPU;
479
480 for (count = 0; count < len32; count++)
481 atmel_sha_write(dd, SHA_REG_DIN(count), buffer[count]);
482
483 return -EINPROGRESS;
484}
485
486static int atmel_sha_xmit_pdc(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
487 size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
488{
489 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
490 int len32;
491
Nicolas Royerd4905b32013-02-20 17:10:26 +0100492 dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
493 ctx->digcnt[1], ctx->digcnt[0], length1, final);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200494
495 len32 = DIV_ROUND_UP(length1, sizeof(u32));
496 atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS);
497 atmel_sha_write(dd, SHA_TPR, dma_addr1);
498 atmel_sha_write(dd, SHA_TCR, len32);
499
500 len32 = DIV_ROUND_UP(length2, sizeof(u32));
501 atmel_sha_write(dd, SHA_TNPR, dma_addr2);
502 atmel_sha_write(dd, SHA_TNCR, len32);
503
504 atmel_sha_write_ctrl(dd, 1);
505
506 /* should be non-zero before next lines to disable clocks later */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100507 ctx->digcnt[0] += length1;
508 if (ctx->digcnt[0] < length1)
509 ctx->digcnt[1]++;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200510
511 if (final)
512 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
513
514 dd->flags |= SHA_FLAGS_DMA_ACTIVE;
515
516 /* Start DMA transfer */
517 atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTEN);
518
519 return -EINPROGRESS;
520}
521
Nicolas Royerd4905b32013-02-20 17:10:26 +0100522static void atmel_sha_dma_callback(void *data)
523{
524 struct atmel_sha_dev *dd = data;
525
Cyrille Pitchena29af932017-01-26 17:07:47 +0100526 dd->is_async = true;
527
Nicolas Royerd4905b32013-02-20 17:10:26 +0100528 /* dma_lch_in - completed - wait DATRDY */
529 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
530}
531
532static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
533 size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
534{
535 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
536 struct dma_async_tx_descriptor *in_desc;
537 struct scatterlist sg[2];
538
539 dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
540 ctx->digcnt[1], ctx->digcnt[0], length1, final);
541
Leilei Zhao3f1992c2015-04-07 17:45:07 +0800542 dd->dma_lch_in.dma_conf.src_maxburst = 16;
543 dd->dma_lch_in.dma_conf.dst_maxburst = 16;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100544
545 dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
546
547 if (length2) {
548 sg_init_table(sg, 2);
549 sg_dma_address(&sg[0]) = dma_addr1;
550 sg_dma_len(&sg[0]) = length1;
551 sg_dma_address(&sg[1]) = dma_addr2;
552 sg_dma_len(&sg[1]) = length2;
553 in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2,
554 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
555 } else {
556 sg_init_table(sg, 1);
557 sg_dma_address(&sg[0]) = dma_addr1;
558 sg_dma_len(&sg[0]) = length1;
559 in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1,
560 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
561 }
562 if (!in_desc)
Cyrille Pitchena29af932017-01-26 17:07:47 +0100563 atmel_sha_complete(dd, -EINVAL);
Nicolas Royerd4905b32013-02-20 17:10:26 +0100564
565 in_desc->callback = atmel_sha_dma_callback;
566 in_desc->callback_param = dd;
567
568 atmel_sha_write_ctrl(dd, 1);
569
570 /* should be non-zero before next lines to disable clocks later */
571 ctx->digcnt[0] += length1;
572 if (ctx->digcnt[0] < length1)
573 ctx->digcnt[1]++;
574
575 if (final)
576 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
577
578 dd->flags |= SHA_FLAGS_DMA_ACTIVE;
579
580 /* Start DMA transfer */
581 dmaengine_submit(in_desc);
582 dma_async_issue_pending(dd->dma_lch_in.chan);
583
584 return -EINPROGRESS;
585}
586
587static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
588 size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
589{
590 if (dd->caps.has_dma)
591 return atmel_sha_xmit_dma(dd, dma_addr1, length1,
592 dma_addr2, length2, final);
593 else
594 return atmel_sha_xmit_pdc(dd, dma_addr1, length1,
595 dma_addr2, length2, final);
596}
597
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200598static int atmel_sha_update_cpu(struct atmel_sha_dev *dd)
599{
600 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
601 int bufcnt;
602
603 atmel_sha_append_sg(ctx);
604 atmel_sha_fill_padding(ctx, 0);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200605 bufcnt = ctx->bufcnt;
606 ctx->bufcnt = 0;
607
608 return atmel_sha_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
609}
610
611static int atmel_sha_xmit_dma_map(struct atmel_sha_dev *dd,
612 struct atmel_sha_reqctx *ctx,
613 size_t length, int final)
614{
615 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
Nicolas Royerd4905b32013-02-20 17:10:26 +0100616 ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200617 if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
618 dev_err(dd->dev, "dma %u bytes error\n", ctx->buflen +
Nicolas Royerd4905b32013-02-20 17:10:26 +0100619 ctx->block_size);
Cyrille Pitchena29af932017-01-26 17:07:47 +0100620 atmel_sha_complete(dd, -EINVAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200621 }
622
623 ctx->flags &= ~SHA_FLAGS_SG;
624
625 /* next call does not fail... so no unmap in the case of error */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100626 return atmel_sha_xmit_start(dd, ctx->dma_addr, length, 0, 0, final);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200627}
628
629static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd)
630{
631 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
632 unsigned int final;
633 size_t count;
634
635 atmel_sha_append_sg(ctx);
636
637 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
638
Nicolas Royerd4905b32013-02-20 17:10:26 +0100639 dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: 0x%llx 0x%llx, final: %d\n",
640 ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200641
642 if (final)
643 atmel_sha_fill_padding(ctx, 0);
644
Ludovic Desroches00992862015-04-07 17:45:04 +0800645 if (final || (ctx->bufcnt == ctx->buflen)) {
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200646 count = ctx->bufcnt;
647 ctx->bufcnt = 0;
648 return atmel_sha_xmit_dma_map(dd, ctx, count, final);
649 }
650
651 return 0;
652}
653
654static int atmel_sha_update_dma_start(struct atmel_sha_dev *dd)
655{
656 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
657 unsigned int length, final, tail;
658 struct scatterlist *sg;
659 unsigned int count;
660
661 if (!ctx->total)
662 return 0;
663
664 if (ctx->bufcnt || ctx->offset)
665 return atmel_sha_update_dma_slow(dd);
666
Nicolas Royerd4905b32013-02-20 17:10:26 +0100667 dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %u, total: %u\n",
668 ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200669
670 sg = ctx->sg;
671
672 if (!IS_ALIGNED(sg->offset, sizeof(u32)))
673 return atmel_sha_update_dma_slow(dd);
674
Nicolas Royerd4905b32013-02-20 17:10:26 +0100675 if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size))
676 /* size is not ctx->block_size aligned */
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200677 return atmel_sha_update_dma_slow(dd);
678
679 length = min(ctx->total, sg->length);
680
681 if (sg_is_last(sg)) {
682 if (!(ctx->flags & SHA_FLAGS_FINUP)) {
Nicolas Royerd4905b32013-02-20 17:10:26 +0100683 /* not last sg must be ctx->block_size aligned */
684 tail = length & (ctx->block_size - 1);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200685 length -= tail;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200686 }
687 }
688
689 ctx->total -= length;
690 ctx->offset = length; /* offset where to start slow */
691
692 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
693
694 /* Add padding */
695 if (final) {
Nicolas Royerd4905b32013-02-20 17:10:26 +0100696 tail = length & (ctx->block_size - 1);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200697 length -= tail;
698 ctx->total += tail;
699 ctx->offset = length; /* offset where to start slow */
700
701 sg = ctx->sg;
702 atmel_sha_append_sg(ctx);
703
704 atmel_sha_fill_padding(ctx, length);
705
706 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
Nicolas Royerd4905b32013-02-20 17:10:26 +0100707 ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200708 if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
709 dev_err(dd->dev, "dma %u bytes error\n",
Nicolas Royerd4905b32013-02-20 17:10:26 +0100710 ctx->buflen + ctx->block_size);
Cyrille Pitchena29af932017-01-26 17:07:47 +0100711 atmel_sha_complete(dd, -EINVAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200712 }
713
714 if (length == 0) {
715 ctx->flags &= ~SHA_FLAGS_SG;
716 count = ctx->bufcnt;
717 ctx->bufcnt = 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100718 return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0,
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200719 0, final);
720 } else {
721 ctx->sg = sg;
722 if (!dma_map_sg(dd->dev, ctx->sg, 1,
723 DMA_TO_DEVICE)) {
724 dev_err(dd->dev, "dma_map_sg error\n");
Cyrille Pitchena29af932017-01-26 17:07:47 +0100725 atmel_sha_complete(dd, -EINVAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200726 }
727
728 ctx->flags |= SHA_FLAGS_SG;
729
730 count = ctx->bufcnt;
731 ctx->bufcnt = 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100732 return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg),
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200733 length, ctx->dma_addr, count, final);
734 }
735 }
736
737 if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
738 dev_err(dd->dev, "dma_map_sg error\n");
Cyrille Pitchena29af932017-01-26 17:07:47 +0100739 atmel_sha_complete(dd, -EINVAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200740 }
741
742 ctx->flags |= SHA_FLAGS_SG;
743
744 /* next call does not fail... so no unmap in the case of error */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100745 return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), length, 0,
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200746 0, final);
747}
748
749static int atmel_sha_update_dma_stop(struct atmel_sha_dev *dd)
750{
751 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
752
753 if (ctx->flags & SHA_FLAGS_SG) {
754 dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
755 if (ctx->sg->length == ctx->offset) {
756 ctx->sg = sg_next(ctx->sg);
757 if (ctx->sg)
758 ctx->offset = 0;
759 }
Nicolas Royerd4905b32013-02-20 17:10:26 +0100760 if (ctx->flags & SHA_FLAGS_PAD) {
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200761 dma_unmap_single(dd->dev, ctx->dma_addr,
Nicolas Royerd4905b32013-02-20 17:10:26 +0100762 ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
763 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200764 } else {
765 dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen +
Nicolas Royerd4905b32013-02-20 17:10:26 +0100766 ctx->block_size, DMA_TO_DEVICE);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200767 }
768
769 return 0;
770}
771
772static int atmel_sha_update_req(struct atmel_sha_dev *dd)
773{
774 struct ahash_request *req = dd->req;
775 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
776 int err;
777
Nicolas Royerd4905b32013-02-20 17:10:26 +0100778 dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n",
779 ctx->total, ctx->digcnt[1], ctx->digcnt[0]);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200780
781 if (ctx->flags & SHA_FLAGS_CPU)
782 err = atmel_sha_update_cpu(dd);
783 else
784 err = atmel_sha_update_dma_start(dd);
785
786 /* wait for dma completion before can take more data */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100787 dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx\n",
788 err, ctx->digcnt[1], ctx->digcnt[0]);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200789
790 return err;
791}
792
793static int atmel_sha_final_req(struct atmel_sha_dev *dd)
794{
795 struct ahash_request *req = dd->req;
796 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
797 int err = 0;
798 int count;
799
800 if (ctx->bufcnt >= ATMEL_SHA_DMA_THRESHOLD) {
801 atmel_sha_fill_padding(ctx, 0);
802 count = ctx->bufcnt;
803 ctx->bufcnt = 0;
804 err = atmel_sha_xmit_dma_map(dd, ctx, count, 1);
805 }
806 /* faster to handle last block with cpu */
807 else {
808 atmel_sha_fill_padding(ctx, 0);
809 count = ctx->bufcnt;
810 ctx->bufcnt = 0;
811 err = atmel_sha_xmit_cpu(dd, ctx->buffer, count, 1);
812 }
813
814 dev_dbg(dd->dev, "final_req: err: %d\n", err);
815
816 return err;
817}
818
819static void atmel_sha_copy_hash(struct ahash_request *req)
820{
821 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
822 u32 *hash = (u32 *)ctx->digest;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100823 unsigned int i, hashsize;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200824
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100825 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
826 case SHA_FLAGS_SHA1:
827 hashsize = SHA1_DIGEST_SIZE;
828 break;
829
830 case SHA_FLAGS_SHA224:
831 case SHA_FLAGS_SHA256:
832 hashsize = SHA256_DIGEST_SIZE;
833 break;
834
835 case SHA_FLAGS_SHA384:
836 case SHA_FLAGS_SHA512:
837 hashsize = SHA512_DIGEST_SIZE;
838 break;
839
840 default:
841 /* Should not happen... */
842 return;
843 }
844
845 for (i = 0; i < hashsize / sizeof(u32); ++i)
846 hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
847 ctx->flags |= SHA_FLAGS_RESTORE;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200848}
849
850static void atmel_sha_copy_ready_hash(struct ahash_request *req)
851{
852 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
853
854 if (!req->result)
855 return;
856
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100857 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
858 default:
859 case SHA_FLAGS_SHA1:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200860 memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100861 break;
862
863 case SHA_FLAGS_SHA224:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100864 memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100865 break;
866
867 case SHA_FLAGS_SHA256:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200868 memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100869 break;
870
871 case SHA_FLAGS_SHA384:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100872 memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100873 break;
874
875 case SHA_FLAGS_SHA512:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100876 memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100877 break;
878 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200879}
880
881static int atmel_sha_finish(struct ahash_request *req)
882{
883 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
884 struct atmel_sha_dev *dd = ctx->dd;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200885
Nicolas Royerd4905b32013-02-20 17:10:26 +0100886 if (ctx->digcnt[0] || ctx->digcnt[1])
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200887 atmel_sha_copy_ready_hash(req);
888
Nicolas Royerd4905b32013-02-20 17:10:26 +0100889 dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %d\n", ctx->digcnt[1],
890 ctx->digcnt[0], ctx->bufcnt);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200891
Rahul Pathak871b88a2015-12-14 08:44:19 +0000892 return 0;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200893}
894
895static void atmel_sha_finish_req(struct ahash_request *req, int err)
896{
897 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
898 struct atmel_sha_dev *dd = ctx->dd;
899
900 if (!err) {
901 atmel_sha_copy_hash(req);
902 if (SHA_FLAGS_FINAL & dd->flags)
903 err = atmel_sha_finish(req);
904 } else {
905 ctx->flags |= SHA_FLAGS_ERROR;
906 }
907
908 /* atomic operation is not needed here */
Cyrille Pitchena29af932017-01-26 17:07:47 +0100909 (void)atmel_sha_complete(dd, err);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200910}
911
912static int atmel_sha_hw_init(struct atmel_sha_dev *dd)
913{
LABBE Corentin9d83d292015-10-02 14:12:58 +0200914 int err;
915
Cyrille Pitchenc0330422016-02-05 13:45:13 +0100916 err = clk_enable(dd->iclk);
LABBE Corentin9d83d292015-10-02 14:12:58 +0200917 if (err)
918 return err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200919
Nicolas Royerd4905b32013-02-20 17:10:26 +0100920 if (!(SHA_FLAGS_INIT & dd->flags)) {
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200921 atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200922 dd->flags |= SHA_FLAGS_INIT;
923 dd->err = 0;
924 }
925
926 return 0;
927}
928
Nicolas Royerd4905b32013-02-20 17:10:26 +0100929static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd)
930{
931 return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff;
932}
933
934static void atmel_sha_hw_version_init(struct atmel_sha_dev *dd)
935{
936 atmel_sha_hw_init(dd);
937
938 dd->hw_version = atmel_sha_get_version(dd);
939
940 dev_info(dd->dev,
941 "version: 0x%x\n", dd->hw_version);
942
Cyrille Pitchenc0330422016-02-05 13:45:13 +0100943 clk_disable(dd->iclk);
Nicolas Royerd4905b32013-02-20 17:10:26 +0100944}
945
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200946static int atmel_sha_handle_queue(struct atmel_sha_dev *dd,
947 struct ahash_request *req)
948{
949 struct crypto_async_request *async_req, *backlog;
Cyrille Pitchena29af932017-01-26 17:07:47 +0100950 struct atmel_sha_ctx *ctx;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200951 unsigned long flags;
Cyrille Pitchena29af932017-01-26 17:07:47 +0100952 bool start_async;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200953 int err = 0, ret = 0;
954
955 spin_lock_irqsave(&dd->lock, flags);
956 if (req)
957 ret = ahash_enqueue_request(&dd->queue, req);
958
959 if (SHA_FLAGS_BUSY & dd->flags) {
960 spin_unlock_irqrestore(&dd->lock, flags);
961 return ret;
962 }
963
964 backlog = crypto_get_backlog(&dd->queue);
965 async_req = crypto_dequeue_request(&dd->queue);
966 if (async_req)
967 dd->flags |= SHA_FLAGS_BUSY;
968
969 spin_unlock_irqrestore(&dd->lock, flags);
970
971 if (!async_req)
972 return ret;
973
974 if (backlog)
975 backlog->complete(backlog, -EINPROGRESS);
976
Cyrille Pitchena29af932017-01-26 17:07:47 +0100977 ctx = crypto_tfm_ctx(async_req->tfm);
978
979 dd->req = ahash_request_cast(async_req);
980 start_async = (dd->req != req);
981 dd->is_async = start_async;
982
983 /* WARNING: ctx->start() MAY change dd->is_async. */
984 err = ctx->start(dd);
985 return (start_async) ? ret : err;
986}
987
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +0100988static int atmel_sha_done(struct atmel_sha_dev *dd);
989
Cyrille Pitchena29af932017-01-26 17:07:47 +0100990static int atmel_sha_start(struct atmel_sha_dev *dd)
991{
992 struct ahash_request *req = dd->req;
993 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
994 int err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200995
996 dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
997 ctx->op, req->nbytes);
998
999 err = atmel_sha_hw_init(dd);
1000
1001 if (err)
1002 goto err1;
1003
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +01001004 dd->resume = atmel_sha_done;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001005 if (ctx->op == SHA_OP_UPDATE) {
1006 err = atmel_sha_update_req(dd);
Nicolas Royerd4905b32013-02-20 17:10:26 +01001007 if (err != -EINPROGRESS && (ctx->flags & SHA_FLAGS_FINUP))
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001008 /* no final() after finup() */
1009 err = atmel_sha_final_req(dd);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001010 } else if (ctx->op == SHA_OP_FINAL) {
1011 err = atmel_sha_final_req(dd);
1012 }
1013
1014err1:
1015 if (err != -EINPROGRESS)
1016 /* done_task will not finish it, so do it here */
1017 atmel_sha_finish_req(req, err);
1018
1019 dev_dbg(dd->dev, "exit, err: %d\n", err);
1020
Cyrille Pitchena29af932017-01-26 17:07:47 +01001021 return err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001022}
1023
1024static int atmel_sha_enqueue(struct ahash_request *req, unsigned int op)
1025{
1026 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1027 struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1028 struct atmel_sha_dev *dd = tctx->dd;
1029
1030 ctx->op = op;
1031
1032 return atmel_sha_handle_queue(dd, req);
1033}
1034
1035static int atmel_sha_update(struct ahash_request *req)
1036{
1037 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1038
1039 if (!req->nbytes)
1040 return 0;
1041
1042 ctx->total = req->nbytes;
1043 ctx->sg = req->src;
1044 ctx->offset = 0;
1045
1046 if (ctx->flags & SHA_FLAGS_FINUP) {
1047 if (ctx->bufcnt + ctx->total < ATMEL_SHA_DMA_THRESHOLD)
1048 /* faster to use CPU for short transfers */
1049 ctx->flags |= SHA_FLAGS_CPU;
1050 } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
1051 atmel_sha_append_sg(ctx);
1052 return 0;
1053 }
1054 return atmel_sha_enqueue(req, SHA_OP_UPDATE);
1055}
1056
1057static int atmel_sha_final(struct ahash_request *req)
1058{
1059 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001060
1061 ctx->flags |= SHA_FLAGS_FINUP;
1062
1063 if (ctx->flags & SHA_FLAGS_ERROR)
1064 return 0; /* uncompleted hash is not needed */
1065
Cyrille Pitchenad841122016-02-08 16:26:49 +01001066 if (ctx->flags & SHA_FLAGS_PAD)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001067 /* copy ready hash (+ finalize hmac) */
1068 return atmel_sha_finish(req);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001069
Cyrille Pitchenad841122016-02-08 16:26:49 +01001070 return atmel_sha_enqueue(req, SHA_OP_FINAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001071}
1072
1073static int atmel_sha_finup(struct ahash_request *req)
1074{
1075 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1076 int err1, err2;
1077
1078 ctx->flags |= SHA_FLAGS_FINUP;
1079
1080 err1 = atmel_sha_update(req);
1081 if (err1 == -EINPROGRESS || err1 == -EBUSY)
1082 return err1;
1083
1084 /*
1085 * final() has to be always called to cleanup resources
1086 * even if udpate() failed, except EINPROGRESS
1087 */
1088 err2 = atmel_sha_final(req);
1089
1090 return err1 ?: err2;
1091}
1092
1093static int atmel_sha_digest(struct ahash_request *req)
1094{
1095 return atmel_sha_init(req) ?: atmel_sha_finup(req);
1096}
1097
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001098
1099static int atmel_sha_export(struct ahash_request *req, void *out)
1100{
1101 const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001102
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001103 memcpy(out, ctx, sizeof(*ctx));
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001104 return 0;
1105}
1106
1107static int atmel_sha_import(struct ahash_request *req, const void *in)
1108{
1109 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001110
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001111 memcpy(ctx, in, sizeof(*ctx));
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001112 return 0;
1113}
1114
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001115static int atmel_sha_cra_init(struct crypto_tfm *tfm)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001116{
Cyrille Pitchena29af932017-01-26 17:07:47 +01001117 struct atmel_sha_ctx *ctx = crypto_tfm_ctx(tfm);
1118
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001119 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001120 sizeof(struct atmel_sha_reqctx));
Cyrille Pitchena29af932017-01-26 17:07:47 +01001121 ctx->start = atmel_sha_start;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001122
1123 return 0;
1124}
1125
Nicolas Royerd4905b32013-02-20 17:10:26 +01001126static struct ahash_alg sha_1_256_algs[] = {
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001127{
1128 .init = atmel_sha_init,
1129 .update = atmel_sha_update,
1130 .final = atmel_sha_final,
1131 .finup = atmel_sha_finup,
1132 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001133 .export = atmel_sha_export,
1134 .import = atmel_sha_import,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001135 .halg = {
1136 .digestsize = SHA1_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001137 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001138 .base = {
1139 .cra_name = "sha1",
1140 .cra_driver_name = "atmel-sha1",
1141 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001142 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001143 .cra_blocksize = SHA1_BLOCK_SIZE,
1144 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1145 .cra_alignmask = 0,
1146 .cra_module = THIS_MODULE,
1147 .cra_init = atmel_sha_cra_init,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001148 }
1149 }
1150},
1151{
1152 .init = atmel_sha_init,
1153 .update = atmel_sha_update,
1154 .final = atmel_sha_final,
1155 .finup = atmel_sha_finup,
1156 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001157 .export = atmel_sha_export,
1158 .import = atmel_sha_import,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001159 .halg = {
1160 .digestsize = SHA256_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001161 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001162 .base = {
1163 .cra_name = "sha256",
1164 .cra_driver_name = "atmel-sha256",
1165 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001166 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001167 .cra_blocksize = SHA256_BLOCK_SIZE,
1168 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1169 .cra_alignmask = 0,
1170 .cra_module = THIS_MODULE,
1171 .cra_init = atmel_sha_cra_init,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001172 }
1173 }
1174},
1175};
1176
Nicolas Royerd4905b32013-02-20 17:10:26 +01001177static struct ahash_alg sha_224_alg = {
1178 .init = atmel_sha_init,
1179 .update = atmel_sha_update,
1180 .final = atmel_sha_final,
1181 .finup = atmel_sha_finup,
1182 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001183 .export = atmel_sha_export,
1184 .import = atmel_sha_import,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001185 .halg = {
1186 .digestsize = SHA224_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001187 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerd4905b32013-02-20 17:10:26 +01001188 .base = {
1189 .cra_name = "sha224",
1190 .cra_driver_name = "atmel-sha224",
1191 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001192 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001193 .cra_blocksize = SHA224_BLOCK_SIZE,
1194 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1195 .cra_alignmask = 0,
1196 .cra_module = THIS_MODULE,
1197 .cra_init = atmel_sha_cra_init,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001198 }
1199 }
1200};
1201
1202static struct ahash_alg sha_384_512_algs[] = {
1203{
1204 .init = atmel_sha_init,
1205 .update = atmel_sha_update,
1206 .final = atmel_sha_final,
1207 .finup = atmel_sha_finup,
1208 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001209 .export = atmel_sha_export,
1210 .import = atmel_sha_import,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001211 .halg = {
1212 .digestsize = SHA384_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001213 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerd4905b32013-02-20 17:10:26 +01001214 .base = {
1215 .cra_name = "sha384",
1216 .cra_driver_name = "atmel-sha384",
1217 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001218 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001219 .cra_blocksize = SHA384_BLOCK_SIZE,
1220 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1221 .cra_alignmask = 0x3,
1222 .cra_module = THIS_MODULE,
1223 .cra_init = atmel_sha_cra_init,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001224 }
1225 }
1226},
1227{
1228 .init = atmel_sha_init,
1229 .update = atmel_sha_update,
1230 .final = atmel_sha_final,
1231 .finup = atmel_sha_finup,
1232 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001233 .export = atmel_sha_export,
1234 .import = atmel_sha_import,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001235 .halg = {
1236 .digestsize = SHA512_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001237 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerd4905b32013-02-20 17:10:26 +01001238 .base = {
1239 .cra_name = "sha512",
1240 .cra_driver_name = "atmel-sha512",
1241 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001242 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001243 .cra_blocksize = SHA512_BLOCK_SIZE,
1244 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1245 .cra_alignmask = 0x3,
1246 .cra_module = THIS_MODULE,
1247 .cra_init = atmel_sha_cra_init,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001248 }
1249 }
1250},
1251};
1252
Cyrille Pitchenf56809c2016-01-15 15:49:32 +01001253static void atmel_sha_queue_task(unsigned long data)
1254{
1255 struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
1256
1257 atmel_sha_handle_queue(dd, NULL);
1258}
1259
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +01001260static int atmel_sha_done(struct atmel_sha_dev *dd)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001261{
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001262 int err = 0;
1263
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001264 if (SHA_FLAGS_CPU & dd->flags) {
1265 if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
1266 dd->flags &= ~SHA_FLAGS_OUTPUT_READY;
1267 goto finish;
1268 }
1269 } else if (SHA_FLAGS_DMA_READY & dd->flags) {
1270 if (SHA_FLAGS_DMA_ACTIVE & dd->flags) {
1271 dd->flags &= ~SHA_FLAGS_DMA_ACTIVE;
1272 atmel_sha_update_dma_stop(dd);
1273 if (dd->err) {
1274 err = dd->err;
1275 goto finish;
1276 }
1277 }
1278 if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
1279 /* hash or semi-hash ready */
1280 dd->flags &= ~(SHA_FLAGS_DMA_READY |
1281 SHA_FLAGS_OUTPUT_READY);
1282 err = atmel_sha_update_dma_start(dd);
1283 if (err != -EINPROGRESS)
1284 goto finish;
1285 }
1286 }
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +01001287 return err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001288
1289finish:
1290 /* finish curent request */
1291 atmel_sha_finish_req(dd->req, err);
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +01001292
1293 return err;
1294}
1295
1296static void atmel_sha_done_task(unsigned long data)
1297{
1298 struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
1299
1300 dd->is_async = true;
1301 (void)dd->resume(dd);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001302}
1303
1304static irqreturn_t atmel_sha_irq(int irq, void *dev_id)
1305{
1306 struct atmel_sha_dev *sha_dd = dev_id;
1307 u32 reg;
1308
1309 reg = atmel_sha_read(sha_dd, SHA_ISR);
1310 if (reg & atmel_sha_read(sha_dd, SHA_IMR)) {
1311 atmel_sha_write(sha_dd, SHA_IDR, reg);
1312 if (SHA_FLAGS_BUSY & sha_dd->flags) {
1313 sha_dd->flags |= SHA_FLAGS_OUTPUT_READY;
1314 if (!(SHA_FLAGS_CPU & sha_dd->flags))
1315 sha_dd->flags |= SHA_FLAGS_DMA_READY;
1316 tasklet_schedule(&sha_dd->done_task);
1317 } else {
1318 dev_warn(sha_dd->dev, "SHA interrupt when no active requests.\n");
1319 }
1320 return IRQ_HANDLED;
1321 }
1322
1323 return IRQ_NONE;
1324}
1325
Cyrille Pitcheneec12f62017-01-26 17:07:52 +01001326
Cyrille Pitchen69303cf2017-01-26 17:07:53 +01001327/* DMA transfer functions */
1328
1329static bool atmel_sha_dma_check_aligned(struct atmel_sha_dev *dd,
1330 struct scatterlist *sg,
1331 size_t len)
1332{
1333 struct atmel_sha_dma *dma = &dd->dma_lch_in;
1334 struct ahash_request *req = dd->req;
1335 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1336 size_t bs = ctx->block_size;
1337 int nents;
1338
1339 for (nents = 0; sg; sg = sg_next(sg), ++nents) {
1340 if (!IS_ALIGNED(sg->offset, sizeof(u32)))
1341 return false;
1342
1343 /*
1344 * This is the last sg, the only one that is allowed to
1345 * have an unaligned length.
1346 */
1347 if (len <= sg->length) {
1348 dma->nents = nents + 1;
1349 dma->last_sg_length = sg->length;
1350 sg->length = ALIGN(len, sizeof(u32));
1351 return true;
1352 }
1353
1354 /* All other sg lengths MUST be aligned to the block size. */
1355 if (!IS_ALIGNED(sg->length, bs))
1356 return false;
1357
1358 len -= sg->length;
1359 }
1360
1361 return false;
1362}
1363
1364static void atmel_sha_dma_callback2(void *data)
1365{
1366 struct atmel_sha_dev *dd = data;
1367 struct atmel_sha_dma *dma = &dd->dma_lch_in;
1368 struct scatterlist *sg;
1369 int nents;
1370
1371 dmaengine_terminate_all(dma->chan);
1372 dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
1373
1374 sg = dma->sg;
1375 for (nents = 0; nents < dma->nents - 1; ++nents)
1376 sg = sg_next(sg);
1377 sg->length = dma->last_sg_length;
1378
1379 dd->is_async = true;
1380 (void)atmel_sha_wait_for_data_ready(dd, dd->resume);
1381}
1382
1383static int atmel_sha_dma_start(struct atmel_sha_dev *dd,
1384 struct scatterlist *src,
1385 size_t len,
1386 atmel_sha_fn_t resume)
1387{
1388 struct atmel_sha_dma *dma = &dd->dma_lch_in;
1389 struct dma_slave_config *config = &dma->dma_conf;
1390 struct dma_chan *chan = dma->chan;
1391 struct dma_async_tx_descriptor *desc;
1392 dma_cookie_t cookie;
1393 unsigned int sg_len;
1394 int err;
1395
1396 dd->resume = resume;
1397
1398 /*
1399 * dma->nents has already been initialized by
1400 * atmel_sha_dma_check_aligned().
1401 */
1402 dma->sg = src;
1403 sg_len = dma_map_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
1404 if (!sg_len) {
1405 err = -ENOMEM;
1406 goto exit;
1407 }
1408
1409 config->src_maxburst = 16;
1410 config->dst_maxburst = 16;
1411 err = dmaengine_slave_config(chan, config);
1412 if (err)
1413 goto unmap_sg;
1414
1415 desc = dmaengine_prep_slave_sg(chan, dma->sg, sg_len, DMA_MEM_TO_DEV,
1416 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1417 if (!desc) {
1418 err = -ENOMEM;
1419 goto unmap_sg;
1420 }
1421
1422 desc->callback = atmel_sha_dma_callback2;
1423 desc->callback_param = dd;
1424 cookie = dmaengine_submit(desc);
1425 err = dma_submit_error(cookie);
1426 if (err)
1427 goto unmap_sg;
1428
1429 dma_async_issue_pending(chan);
1430
1431 return -EINPROGRESS;
1432
1433unmap_sg:
1434 dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
1435exit:
1436 return atmel_sha_complete(dd, err);
1437}
1438
1439
Cyrille Pitcheneec12f62017-01-26 17:07:52 +01001440/* CPU transfer functions */
1441
1442static int atmel_sha_cpu_transfer(struct atmel_sha_dev *dd)
1443{
1444 struct ahash_request *req = dd->req;
1445 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1446 const u32 *words = (const u32 *)ctx->buffer;
1447 size_t i, num_words;
1448 u32 isr, din, din_inc;
1449
1450 din_inc = (ctx->flags & SHA_FLAGS_IDATAR0) ? 0 : 1;
1451 for (;;) {
1452 /* Write data into the Input Data Registers. */
1453 num_words = DIV_ROUND_UP(ctx->bufcnt, sizeof(u32));
1454 for (i = 0, din = 0; i < num_words; ++i, din += din_inc)
1455 atmel_sha_write(dd, SHA_REG_DIN(din), words[i]);
1456
1457 ctx->offset += ctx->bufcnt;
1458 ctx->total -= ctx->bufcnt;
1459
1460 if (!ctx->total)
1461 break;
1462
1463 /*
1464 * Prepare next block:
1465 * Fill ctx->buffer now with the next data to be written into
1466 * IDATARx: it gives time for the SHA hardware to process
1467 * the current data so the SHA_INT_DATARDY flag might be set
1468 * in SHA_ISR when polling this register at the beginning of
1469 * the next loop.
1470 */
1471 ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
1472 scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
1473 ctx->offset, ctx->bufcnt, 0);
1474
1475 /* Wait for hardware to be ready again. */
1476 isr = atmel_sha_read(dd, SHA_ISR);
1477 if (!(isr & SHA_INT_DATARDY)) {
1478 /* Not ready yet. */
1479 dd->resume = atmel_sha_cpu_transfer;
1480 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
1481 return -EINPROGRESS;
1482 }
1483 }
1484
1485 if (unlikely(!(ctx->flags & SHA_FLAGS_WAIT_DATARDY)))
1486 return dd->cpu_transfer_complete(dd);
1487
1488 return atmel_sha_wait_for_data_ready(dd, dd->cpu_transfer_complete);
1489}
1490
1491static int atmel_sha_cpu_start(struct atmel_sha_dev *dd,
1492 struct scatterlist *sg,
1493 unsigned int len,
1494 bool idatar0_only,
1495 bool wait_data_ready,
1496 atmel_sha_fn_t resume)
1497{
1498 struct ahash_request *req = dd->req;
1499 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1500
1501 if (!len)
1502 return resume(dd);
1503
1504 ctx->flags &= ~(SHA_FLAGS_IDATAR0 | SHA_FLAGS_WAIT_DATARDY);
1505
1506 if (idatar0_only)
1507 ctx->flags |= SHA_FLAGS_IDATAR0;
1508
1509 if (wait_data_ready)
1510 ctx->flags |= SHA_FLAGS_WAIT_DATARDY;
1511
1512 ctx->sg = sg;
1513 ctx->total = len;
1514 ctx->offset = 0;
1515
1516 /* Prepare the first block to be written. */
1517 ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
1518 scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
1519 ctx->offset, ctx->bufcnt, 0);
1520
1521 dd->cpu_transfer_complete = resume;
1522 return atmel_sha_cpu_transfer(dd);
1523}
1524
1525
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001526static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd)
1527{
1528 int i;
1529
Nicolas Royerd4905b32013-02-20 17:10:26 +01001530 for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++)
1531 crypto_unregister_ahash(&sha_1_256_algs[i]);
1532
1533 if (dd->caps.has_sha224)
1534 crypto_unregister_ahash(&sha_224_alg);
1535
1536 if (dd->caps.has_sha_384_512) {
1537 for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++)
1538 crypto_unregister_ahash(&sha_384_512_algs[i]);
1539 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001540}
1541
1542static int atmel_sha_register_algs(struct atmel_sha_dev *dd)
1543{
1544 int err, i, j;
1545
Nicolas Royerd4905b32013-02-20 17:10:26 +01001546 for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) {
1547 err = crypto_register_ahash(&sha_1_256_algs[i]);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001548 if (err)
Nicolas Royerd4905b32013-02-20 17:10:26 +01001549 goto err_sha_1_256_algs;
1550 }
1551
1552 if (dd->caps.has_sha224) {
1553 err = crypto_register_ahash(&sha_224_alg);
1554 if (err)
1555 goto err_sha_224_algs;
1556 }
1557
1558 if (dd->caps.has_sha_384_512) {
1559 for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) {
1560 err = crypto_register_ahash(&sha_384_512_algs[i]);
1561 if (err)
1562 goto err_sha_384_512_algs;
1563 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001564 }
1565
1566 return 0;
1567
Nicolas Royerd4905b32013-02-20 17:10:26 +01001568err_sha_384_512_algs:
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001569 for (j = 0; j < i; j++)
Nicolas Royerd4905b32013-02-20 17:10:26 +01001570 crypto_unregister_ahash(&sha_384_512_algs[j]);
1571 crypto_unregister_ahash(&sha_224_alg);
1572err_sha_224_algs:
1573 i = ARRAY_SIZE(sha_1_256_algs);
1574err_sha_1_256_algs:
1575 for (j = 0; j < i; j++)
1576 crypto_unregister_ahash(&sha_1_256_algs[j]);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001577
1578 return err;
1579}
1580
Nicolas Royerd4905b32013-02-20 17:10:26 +01001581static bool atmel_sha_filter(struct dma_chan *chan, void *slave)
1582{
1583 struct at_dma_slave *sl = slave;
1584
1585 if (sl && sl->dma_dev == chan->device->dev) {
1586 chan->private = sl;
1587 return true;
1588 } else {
1589 return false;
1590 }
1591}
1592
1593static int atmel_sha_dma_init(struct atmel_sha_dev *dd,
1594 struct crypto_platform_data *pdata)
1595{
1596 int err = -ENOMEM;
1597 dma_cap_mask_t mask_in;
1598
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001599 /* Try to grab DMA channel */
1600 dma_cap_zero(mask_in);
1601 dma_cap_set(DMA_SLAVE, mask_in);
Nicolas Royerd4905b32013-02-20 17:10:26 +01001602
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001603 dd->dma_lch_in.chan = dma_request_slave_channel_compat(mask_in,
1604 atmel_sha_filter, &pdata->dma_slave->rxdata, dd->dev, "tx");
1605 if (!dd->dma_lch_in.chan) {
1606 dev_warn(dd->dev, "no DMA channel available\n");
1607 return err;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001608 }
1609
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001610 dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV;
1611 dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
1612 SHA_REG_DIN(0);
1613 dd->dma_lch_in.dma_conf.src_maxburst = 1;
1614 dd->dma_lch_in.dma_conf.src_addr_width =
1615 DMA_SLAVE_BUSWIDTH_4_BYTES;
1616 dd->dma_lch_in.dma_conf.dst_maxburst = 1;
1617 dd->dma_lch_in.dma_conf.dst_addr_width =
1618 DMA_SLAVE_BUSWIDTH_4_BYTES;
1619 dd->dma_lch_in.dma_conf.device_fc = false;
1620
1621 return 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001622}
1623
1624static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd)
1625{
1626 dma_release_channel(dd->dma_lch_in.chan);
1627}
1628
1629static void atmel_sha_get_cap(struct atmel_sha_dev *dd)
1630{
1631
1632 dd->caps.has_dma = 0;
1633 dd->caps.has_dualbuff = 0;
1634 dd->caps.has_sha224 = 0;
1635 dd->caps.has_sha_384_512 = 0;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +01001636 dd->caps.has_uihv = 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001637
1638 /* keep only major version number */
1639 switch (dd->hw_version & 0xff0) {
Cyrille Pitchen507c5cc2016-01-15 15:49:33 +01001640 case 0x510:
1641 dd->caps.has_dma = 1;
1642 dd->caps.has_dualbuff = 1;
1643 dd->caps.has_sha224 = 1;
1644 dd->caps.has_sha_384_512 = 1;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +01001645 dd->caps.has_uihv = 1;
Cyrille Pitchen507c5cc2016-01-15 15:49:33 +01001646 break;
Leilei Zhao141824d2015-04-07 17:45:03 +08001647 case 0x420:
1648 dd->caps.has_dma = 1;
1649 dd->caps.has_dualbuff = 1;
1650 dd->caps.has_sha224 = 1;
1651 dd->caps.has_sha_384_512 = 1;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +01001652 dd->caps.has_uihv = 1;
Leilei Zhao141824d2015-04-07 17:45:03 +08001653 break;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001654 case 0x410:
1655 dd->caps.has_dma = 1;
1656 dd->caps.has_dualbuff = 1;
1657 dd->caps.has_sha224 = 1;
1658 dd->caps.has_sha_384_512 = 1;
1659 break;
1660 case 0x400:
1661 dd->caps.has_dma = 1;
1662 dd->caps.has_dualbuff = 1;
1663 dd->caps.has_sha224 = 1;
1664 break;
1665 case 0x320:
1666 break;
1667 default:
1668 dev_warn(dd->dev,
1669 "Unmanaged sha version, set minimum capabilities\n");
1670 break;
1671 }
1672}
1673
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001674#if defined(CONFIG_OF)
1675static const struct of_device_id atmel_sha_dt_ids[] = {
1676 { .compatible = "atmel,at91sam9g46-sha" },
1677 { /* sentinel */ }
1678};
1679
1680MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids);
1681
1682static struct crypto_platform_data *atmel_sha_of_init(struct platform_device *pdev)
1683{
1684 struct device_node *np = pdev->dev.of_node;
1685 struct crypto_platform_data *pdata;
1686
1687 if (!np) {
1688 dev_err(&pdev->dev, "device node not found\n");
1689 return ERR_PTR(-EINVAL);
1690 }
1691
1692 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1693 if (!pdata) {
1694 dev_err(&pdev->dev, "could not allocate memory for pdata\n");
1695 return ERR_PTR(-ENOMEM);
1696 }
1697
1698 pdata->dma_slave = devm_kzalloc(&pdev->dev,
1699 sizeof(*(pdata->dma_slave)),
1700 GFP_KERNEL);
1701 if (!pdata->dma_slave) {
1702 dev_err(&pdev->dev, "could not allocate memory for dma_slave\n");
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001703 return ERR_PTR(-ENOMEM);
1704 }
1705
1706 return pdata;
1707}
1708#else /* CONFIG_OF */
1709static inline struct crypto_platform_data *atmel_sha_of_init(struct platform_device *dev)
1710{
1711 return ERR_PTR(-EINVAL);
1712}
1713#endif
1714
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08001715static int atmel_sha_probe(struct platform_device *pdev)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001716{
1717 struct atmel_sha_dev *sha_dd;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001718 struct crypto_platform_data *pdata;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001719 struct device *dev = &pdev->dev;
1720 struct resource *sha_res;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001721 int err;
1722
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001723 sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001724 if (sha_dd == NULL) {
1725 dev_err(dev, "unable to alloc data struct.\n");
1726 err = -ENOMEM;
1727 goto sha_dd_err;
1728 }
1729
1730 sha_dd->dev = dev;
1731
1732 platform_set_drvdata(pdev, sha_dd);
1733
1734 INIT_LIST_HEAD(&sha_dd->list);
Leilei Zhao62728e82015-04-07 17:45:06 +08001735 spin_lock_init(&sha_dd->lock);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001736
1737 tasklet_init(&sha_dd->done_task, atmel_sha_done_task,
1738 (unsigned long)sha_dd);
Cyrille Pitchenf56809c2016-01-15 15:49:32 +01001739 tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task,
1740 (unsigned long)sha_dd);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001741
1742 crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH);
1743
1744 sha_dd->irq = -1;
1745
1746 /* Get the base address */
1747 sha_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1748 if (!sha_res) {
1749 dev_err(dev, "no MEM resource info\n");
1750 err = -ENODEV;
1751 goto res_err;
1752 }
1753 sha_dd->phys_base = sha_res->start;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001754
1755 /* Get the IRQ */
1756 sha_dd->irq = platform_get_irq(pdev, 0);
1757 if (sha_dd->irq < 0) {
1758 dev_err(dev, "no IRQ resource info\n");
1759 err = sha_dd->irq;
1760 goto res_err;
1761 }
1762
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001763 err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq,
1764 IRQF_SHARED, "atmel-sha", sha_dd);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001765 if (err) {
1766 dev_err(dev, "unable to request sha irq.\n");
1767 goto res_err;
1768 }
1769
1770 /* Initializing the clock */
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001771 sha_dd->iclk = devm_clk_get(&pdev->dev, "sha_clk");
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001772 if (IS_ERR(sha_dd->iclk)) {
Colin Ian Kingbe208352015-02-28 20:40:10 +00001773 dev_err(dev, "clock initialization failed.\n");
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001774 err = PTR_ERR(sha_dd->iclk);
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001775 goto res_err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001776 }
1777
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001778 sha_dd->io_base = devm_ioremap_resource(&pdev->dev, sha_res);
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02001779 if (IS_ERR(sha_dd->io_base)) {
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001780 dev_err(dev, "can't ioremap\n");
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02001781 err = PTR_ERR(sha_dd->io_base);
LABBE Corentinb0e8b342015-10-12 19:47:03 +02001782 goto res_err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001783 }
1784
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001785 err = clk_prepare(sha_dd->iclk);
1786 if (err)
1787 goto res_err;
1788
Nicolas Royerd4905b32013-02-20 17:10:26 +01001789 atmel_sha_hw_version_init(sha_dd);
1790
1791 atmel_sha_get_cap(sha_dd);
1792
1793 if (sha_dd->caps.has_dma) {
1794 pdata = pdev->dev.platform_data;
1795 if (!pdata) {
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001796 pdata = atmel_sha_of_init(pdev);
1797 if (IS_ERR(pdata)) {
1798 dev_err(&pdev->dev, "platform data not available\n");
1799 err = PTR_ERR(pdata);
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001800 goto iclk_unprepare;
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001801 }
1802 }
1803 if (!pdata->dma_slave) {
Nicolas Royerd4905b32013-02-20 17:10:26 +01001804 err = -ENXIO;
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001805 goto iclk_unprepare;
Nicolas Royerd4905b32013-02-20 17:10:26 +01001806 }
1807 err = atmel_sha_dma_init(sha_dd, pdata);
1808 if (err)
1809 goto err_sha_dma;
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001810
1811 dev_info(dev, "using %s for DMA transfers\n",
1812 dma_chan_name(sha_dd->dma_lch_in.chan));
Nicolas Royerd4905b32013-02-20 17:10:26 +01001813 }
1814
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001815 spin_lock(&atmel_sha.lock);
1816 list_add_tail(&sha_dd->list, &atmel_sha.dev_list);
1817 spin_unlock(&atmel_sha.lock);
1818
1819 err = atmel_sha_register_algs(sha_dd);
1820 if (err)
1821 goto err_algs;
1822
Nicolas Ferre1ca5b7d2013-10-15 16:37:44 +02001823 dev_info(dev, "Atmel SHA1/SHA256%s%s\n",
1824 sha_dd->caps.has_sha224 ? "/SHA224" : "",
1825 sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : "");
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001826
1827 return 0;
1828
1829err_algs:
1830 spin_lock(&atmel_sha.lock);
1831 list_del(&sha_dd->list);
1832 spin_unlock(&atmel_sha.lock);
Nicolas Royerd4905b32013-02-20 17:10:26 +01001833 if (sha_dd->caps.has_dma)
1834 atmel_sha_dma_cleanup(sha_dd);
1835err_sha_dma:
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001836iclk_unprepare:
1837 clk_unprepare(sha_dd->iclk);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001838res_err:
Cyrille Pitchenf56809c2016-01-15 15:49:32 +01001839 tasklet_kill(&sha_dd->queue_task);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001840 tasklet_kill(&sha_dd->done_task);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001841sha_dd_err:
1842 dev_err(dev, "initialization failed.\n");
1843
1844 return err;
1845}
1846
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08001847static int atmel_sha_remove(struct platform_device *pdev)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001848{
1849 static struct atmel_sha_dev *sha_dd;
1850
1851 sha_dd = platform_get_drvdata(pdev);
1852 if (!sha_dd)
1853 return -ENODEV;
1854 spin_lock(&atmel_sha.lock);
1855 list_del(&sha_dd->list);
1856 spin_unlock(&atmel_sha.lock);
1857
1858 atmel_sha_unregister_algs(sha_dd);
1859
Cyrille Pitchenf56809c2016-01-15 15:49:32 +01001860 tasklet_kill(&sha_dd->queue_task);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001861 tasklet_kill(&sha_dd->done_task);
1862
Nicolas Royerd4905b32013-02-20 17:10:26 +01001863 if (sha_dd->caps.has_dma)
1864 atmel_sha_dma_cleanup(sha_dd);
1865
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001866 clk_unprepare(sha_dd->iclk);
1867
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001868 return 0;
1869}
1870
1871static struct platform_driver atmel_sha_driver = {
1872 .probe = atmel_sha_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08001873 .remove = atmel_sha_remove,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001874 .driver = {
1875 .name = "atmel_sha",
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02001876 .of_match_table = of_match_ptr(atmel_sha_dt_ids),
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001877 },
1878};
1879
1880module_platform_driver(atmel_sha_driver);
1881
Nicolas Royerd4905b32013-02-20 17:10:26 +01001882MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001883MODULE_LICENSE("GPL v2");
1884MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");