4 * Support for ATMEL SHA1/SHA256 HW acceleration.
6 * Copyright (c) 2012 Eukréa Electromatique - ATMEL
7 * Author: Nicolas Royer <nicolas@eukrea.com>
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.
13 * Some ideas are from omap-sham.c drivers.
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>
23 #include <linux/hw_random.h>
24 #include <linux/platform_device.h>
26 #include <linux/device.h>
27 #include <linux/init.h>
28 #include <linux/errno.h>
29 #include <linux/interrupt.h>
30 #include <linux/irq.h>
31 #include <linux/scatterlist.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/of_device.h>
34 #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>
42 #include <linux/platform_data/crypto-atmel.h>
43 #include "atmel-sha-regs.h"
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)
54 #define SHA_FLAGS_FINUP BIT(16)
55 #define SHA_FLAGS_SG BIT(17)
56 #define SHA_FLAGS_SHA1 BIT(18)
57 #define SHA_FLAGS_SHA224 BIT(19)
58 #define SHA_FLAGS_SHA256 BIT(20)
59 #define SHA_FLAGS_SHA384 BIT(21)
60 #define SHA_FLAGS_SHA512 BIT(22)
61 #define SHA_FLAGS_ERROR BIT(23)
62 #define SHA_FLAGS_PAD BIT(24)
64 #define SHA_OP_UPDATE 1
65 #define SHA_OP_FINAL 2
67 #define SHA_BUFFER_LEN PAGE_SIZE
69 #define ATMEL_SHA_DMA_THRESHOLD 56
71 struct atmel_sha_caps
{
80 struct atmel_sha_reqctx
{
81 struct atmel_sha_dev
*dd
;
85 u8 digest
[SHA512_DIGEST_SIZE
] __aligned(sizeof(u32
));
92 struct scatterlist
*sg
;
93 unsigned int offset
; /* offset in current sg */
94 unsigned int total
; /* total request */
98 u8 buffer
[0] __aligned(sizeof(u32
));
101 struct atmel_sha_ctx
{
102 struct atmel_sha_dev
*dd
;
107 #define ATMEL_SHA_QUEUE_LENGTH 50
109 struct atmel_sha_dma
{
110 struct dma_chan
*chan
;
111 struct dma_slave_config dma_conf
;
114 struct atmel_sha_dev
{
115 struct list_head list
;
116 unsigned long phys_base
;
120 void __iomem
*io_base
;
124 struct tasklet_struct done_task
;
127 struct crypto_queue queue
;
128 struct ahash_request
*req
;
130 struct atmel_sha_dma dma_lch_in
;
132 struct atmel_sha_caps caps
;
137 struct atmel_sha_drv
{
138 struct list_head dev_list
;
142 static struct atmel_sha_drv atmel_sha
= {
143 .dev_list
= LIST_HEAD_INIT(atmel_sha
.dev_list
),
144 .lock
= __SPIN_LOCK_UNLOCKED(atmel_sha
.lock
),
147 static inline u32
atmel_sha_read(struct atmel_sha_dev
*dd
, u32 offset
)
149 return readl_relaxed(dd
->io_base
+ offset
);
152 static inline void atmel_sha_write(struct atmel_sha_dev
*dd
,
153 u32 offset
, u32 value
)
155 writel_relaxed(value
, dd
->io_base
+ offset
);
158 static size_t atmel_sha_append_sg(struct atmel_sha_reqctx
*ctx
)
162 while ((ctx
->bufcnt
< ctx
->buflen
) && ctx
->total
) {
163 count
= min(ctx
->sg
->length
- ctx
->offset
, ctx
->total
);
164 count
= min(count
, ctx
->buflen
- ctx
->bufcnt
);
168 * Check if count <= 0 because the buffer is full or
169 * because the sg length is 0. In the latest case,
170 * check if there is another sg in the list, a 0 length
171 * sg doesn't necessarily mean the end of the sg list.
173 if ((ctx
->sg
->length
== 0) && !sg_is_last(ctx
->sg
)) {
174 ctx
->sg
= sg_next(ctx
->sg
);
181 scatterwalk_map_and_copy(ctx
->buffer
+ ctx
->bufcnt
, ctx
->sg
,
182 ctx
->offset
, count
, 0);
184 ctx
->bufcnt
+= count
;
185 ctx
->offset
+= count
;
188 if (ctx
->offset
== ctx
->sg
->length
) {
189 ctx
->sg
= sg_next(ctx
->sg
);
201 * The purpose of this padding is to ensure that the padded message is a
202 * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
203 * The bit "1" is appended at the end of the message followed by
204 * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
205 * 128 bits block (SHA384/SHA512) equals to the message length in bits
208 * For SHA1/SHA224/SHA256, padlen is calculated as followed:
209 * - if message length < 56 bytes then padlen = 56 - message length
210 * - else padlen = 64 + 56 - message length
212 * For SHA384/SHA512, padlen is calculated as followed:
213 * - if message length < 112 bytes then padlen = 112 - message length
214 * - else padlen = 128 + 112 - message length
216 static void atmel_sha_fill_padding(struct atmel_sha_reqctx
*ctx
, int length
)
218 unsigned int index
, padlen
;
222 size
[0] = ctx
->digcnt
[0];
223 size
[1] = ctx
->digcnt
[1];
225 size
[0] += ctx
->bufcnt
;
226 if (size
[0] < ctx
->bufcnt
)
230 if (size
[0] < length
)
233 bits
[1] = cpu_to_be64(size
[0] << 3);
234 bits
[0] = cpu_to_be64(size
[1] << 3 | size
[0] >> 61);
236 if (ctx
->flags
& (SHA_FLAGS_SHA384
| SHA_FLAGS_SHA512
)) {
237 index
= ctx
->bufcnt
& 0x7f;
238 padlen
= (index
< 112) ? (112 - index
) : ((128+112) - index
);
239 *(ctx
->buffer
+ ctx
->bufcnt
) = 0x80;
240 memset(ctx
->buffer
+ ctx
->bufcnt
+ 1, 0, padlen
-1);
241 memcpy(ctx
->buffer
+ ctx
->bufcnt
+ padlen
, bits
, 16);
242 ctx
->bufcnt
+= padlen
+ 16;
243 ctx
->flags
|= SHA_FLAGS_PAD
;
245 index
= ctx
->bufcnt
& 0x3f;
246 padlen
= (index
< 56) ? (56 - index
) : ((64+56) - index
);
247 *(ctx
->buffer
+ ctx
->bufcnt
) = 0x80;
248 memset(ctx
->buffer
+ ctx
->bufcnt
+ 1, 0, padlen
-1);
249 memcpy(ctx
->buffer
+ ctx
->bufcnt
+ padlen
, &bits
[1], 8);
250 ctx
->bufcnt
+= padlen
+ 8;
251 ctx
->flags
|= SHA_FLAGS_PAD
;
255 static int atmel_sha_init(struct ahash_request
*req
)
257 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
258 struct atmel_sha_ctx
*tctx
= crypto_ahash_ctx(tfm
);
259 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
260 struct atmel_sha_dev
*dd
= NULL
;
261 struct atmel_sha_dev
*tmp
;
263 spin_lock_bh(&atmel_sha
.lock
);
265 list_for_each_entry(tmp
, &atmel_sha
.dev_list
, list
) {
274 spin_unlock_bh(&atmel_sha
.lock
);
280 dev_dbg(dd
->dev
, "init: digest size: %d\n",
281 crypto_ahash_digestsize(tfm
));
283 switch (crypto_ahash_digestsize(tfm
)) {
284 case SHA1_DIGEST_SIZE
:
285 ctx
->flags
|= SHA_FLAGS_SHA1
;
286 ctx
->block_size
= SHA1_BLOCK_SIZE
;
288 case SHA224_DIGEST_SIZE
:
289 ctx
->flags
|= SHA_FLAGS_SHA224
;
290 ctx
->block_size
= SHA224_BLOCK_SIZE
;
292 case SHA256_DIGEST_SIZE
:
293 ctx
->flags
|= SHA_FLAGS_SHA256
;
294 ctx
->block_size
= SHA256_BLOCK_SIZE
;
296 case SHA384_DIGEST_SIZE
:
297 ctx
->flags
|= SHA_FLAGS_SHA384
;
298 ctx
->block_size
= SHA384_BLOCK_SIZE
;
300 case SHA512_DIGEST_SIZE
:
301 ctx
->flags
|= SHA_FLAGS_SHA512
;
302 ctx
->block_size
= SHA512_BLOCK_SIZE
;
312 ctx
->buflen
= SHA_BUFFER_LEN
;
317 static void atmel_sha_write_ctrl(struct atmel_sha_dev
*dd
, int dma
)
319 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
320 u32 valcr
= 0, valmr
= SHA_MR_MODE_AUTO
;
323 if (!dd
->caps
.has_dma
)
324 atmel_sha_write(dd
, SHA_IER
, SHA_INT_TXBUFE
);
325 valmr
= SHA_MR_MODE_PDC
;
326 if (dd
->caps
.has_dualbuff
)
327 valmr
|= SHA_MR_DUALBUFF
;
329 atmel_sha_write(dd
, SHA_IER
, SHA_INT_DATARDY
);
332 if (ctx
->flags
& SHA_FLAGS_SHA1
)
333 valmr
|= SHA_MR_ALGO_SHA1
;
334 else if (ctx
->flags
& SHA_FLAGS_SHA224
)
335 valmr
|= SHA_MR_ALGO_SHA224
;
336 else if (ctx
->flags
& SHA_FLAGS_SHA256
)
337 valmr
|= SHA_MR_ALGO_SHA256
;
338 else if (ctx
->flags
& SHA_FLAGS_SHA384
)
339 valmr
|= SHA_MR_ALGO_SHA384
;
340 else if (ctx
->flags
& SHA_FLAGS_SHA512
)
341 valmr
|= SHA_MR_ALGO_SHA512
;
343 /* Setting CR_FIRST only for the first iteration */
344 if (!(ctx
->digcnt
[0] || ctx
->digcnt
[1]))
345 valcr
= SHA_CR_FIRST
;
347 atmel_sha_write(dd
, SHA_CR
, valcr
);
348 atmel_sha_write(dd
, SHA_MR
, valmr
);
351 static int atmel_sha_xmit_cpu(struct atmel_sha_dev
*dd
, const u8
*buf
,
352 size_t length
, int final
)
354 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
356 const u32
*buffer
= (const u32
*)buf
;
358 dev_dbg(dd
->dev
, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
359 ctx
->digcnt
[1], ctx
->digcnt
[0], length
, final
);
361 atmel_sha_write_ctrl(dd
, 0);
363 /* should be non-zero before next lines to disable clocks later */
364 ctx
->digcnt
[0] += length
;
365 if (ctx
->digcnt
[0] < length
)
369 dd
->flags
|= SHA_FLAGS_FINAL
; /* catch last interrupt */
371 len32
= DIV_ROUND_UP(length
, sizeof(u32
));
373 dd
->flags
|= SHA_FLAGS_CPU
;
375 for (count
= 0; count
< len32
; count
++)
376 atmel_sha_write(dd
, SHA_REG_DIN(count
), buffer
[count
]);
381 static int atmel_sha_xmit_pdc(struct atmel_sha_dev
*dd
, dma_addr_t dma_addr1
,
382 size_t length1
, dma_addr_t dma_addr2
, size_t length2
, int final
)
384 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
387 dev_dbg(dd
->dev
, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
388 ctx
->digcnt
[1], ctx
->digcnt
[0], length1
, final
);
390 len32
= DIV_ROUND_UP(length1
, sizeof(u32
));
391 atmel_sha_write(dd
, SHA_PTCR
, SHA_PTCR_TXTDIS
);
392 atmel_sha_write(dd
, SHA_TPR
, dma_addr1
);
393 atmel_sha_write(dd
, SHA_TCR
, len32
);
395 len32
= DIV_ROUND_UP(length2
, sizeof(u32
));
396 atmel_sha_write(dd
, SHA_TNPR
, dma_addr2
);
397 atmel_sha_write(dd
, SHA_TNCR
, len32
);
399 atmel_sha_write_ctrl(dd
, 1);
401 /* should be non-zero before next lines to disable clocks later */
402 ctx
->digcnt
[0] += length1
;
403 if (ctx
->digcnt
[0] < length1
)
407 dd
->flags
|= SHA_FLAGS_FINAL
; /* catch last interrupt */
409 dd
->flags
|= SHA_FLAGS_DMA_ACTIVE
;
411 /* Start DMA transfer */
412 atmel_sha_write(dd
, SHA_PTCR
, SHA_PTCR_TXTEN
);
417 static void atmel_sha_dma_callback(void *data
)
419 struct atmel_sha_dev
*dd
= data
;
421 /* dma_lch_in - completed - wait DATRDY */
422 atmel_sha_write(dd
, SHA_IER
, SHA_INT_DATARDY
);
425 static int atmel_sha_xmit_dma(struct atmel_sha_dev
*dd
, dma_addr_t dma_addr1
,
426 size_t length1
, dma_addr_t dma_addr2
, size_t length2
, int final
)
428 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
429 struct dma_async_tx_descriptor
*in_desc
;
430 struct scatterlist sg
[2];
432 dev_dbg(dd
->dev
, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
433 ctx
->digcnt
[1], ctx
->digcnt
[0], length1
, final
);
435 dd
->dma_lch_in
.dma_conf
.src_maxburst
= 16;
436 dd
->dma_lch_in
.dma_conf
.dst_maxburst
= 16;
438 dmaengine_slave_config(dd
->dma_lch_in
.chan
, &dd
->dma_lch_in
.dma_conf
);
441 sg_init_table(sg
, 2);
442 sg_dma_address(&sg
[0]) = dma_addr1
;
443 sg_dma_len(&sg
[0]) = length1
;
444 sg_dma_address(&sg
[1]) = dma_addr2
;
445 sg_dma_len(&sg
[1]) = length2
;
446 in_desc
= dmaengine_prep_slave_sg(dd
->dma_lch_in
.chan
, sg
, 2,
447 DMA_MEM_TO_DEV
, DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
449 sg_init_table(sg
, 1);
450 sg_dma_address(&sg
[0]) = dma_addr1
;
451 sg_dma_len(&sg
[0]) = length1
;
452 in_desc
= dmaengine_prep_slave_sg(dd
->dma_lch_in
.chan
, sg
, 1,
453 DMA_MEM_TO_DEV
, DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
458 in_desc
->callback
= atmel_sha_dma_callback
;
459 in_desc
->callback_param
= dd
;
461 atmel_sha_write_ctrl(dd
, 1);
463 /* should be non-zero before next lines to disable clocks later */
464 ctx
->digcnt
[0] += length1
;
465 if (ctx
->digcnt
[0] < length1
)
469 dd
->flags
|= SHA_FLAGS_FINAL
; /* catch last interrupt */
471 dd
->flags
|= SHA_FLAGS_DMA_ACTIVE
;
473 /* Start DMA transfer */
474 dmaengine_submit(in_desc
);
475 dma_async_issue_pending(dd
->dma_lch_in
.chan
);
480 static int atmel_sha_xmit_start(struct atmel_sha_dev
*dd
, dma_addr_t dma_addr1
,
481 size_t length1
, dma_addr_t dma_addr2
, size_t length2
, int final
)
483 if (dd
->caps
.has_dma
)
484 return atmel_sha_xmit_dma(dd
, dma_addr1
, length1
,
485 dma_addr2
, length2
, final
);
487 return atmel_sha_xmit_pdc(dd
, dma_addr1
, length1
,
488 dma_addr2
, length2
, final
);
491 static int atmel_sha_update_cpu(struct atmel_sha_dev
*dd
)
493 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
496 atmel_sha_append_sg(ctx
);
497 atmel_sha_fill_padding(ctx
, 0);
498 bufcnt
= ctx
->bufcnt
;
501 return atmel_sha_xmit_cpu(dd
, ctx
->buffer
, bufcnt
, 1);
504 static int atmel_sha_xmit_dma_map(struct atmel_sha_dev
*dd
,
505 struct atmel_sha_reqctx
*ctx
,
506 size_t length
, int final
)
508 ctx
->dma_addr
= dma_map_single(dd
->dev
, ctx
->buffer
,
509 ctx
->buflen
+ ctx
->block_size
, DMA_TO_DEVICE
);
510 if (dma_mapping_error(dd
->dev
, ctx
->dma_addr
)) {
511 dev_err(dd
->dev
, "dma %u bytes error\n", ctx
->buflen
+
516 ctx
->flags
&= ~SHA_FLAGS_SG
;
518 /* next call does not fail... so no unmap in the case of error */
519 return atmel_sha_xmit_start(dd
, ctx
->dma_addr
, length
, 0, 0, final
);
522 static int atmel_sha_update_dma_slow(struct atmel_sha_dev
*dd
)
524 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
528 atmel_sha_append_sg(ctx
);
530 final
= (ctx
->flags
& SHA_FLAGS_FINUP
) && !ctx
->total
;
532 dev_dbg(dd
->dev
, "slow: bufcnt: %u, digcnt: 0x%llx 0x%llx, final: %d\n",
533 ctx
->bufcnt
, ctx
->digcnt
[1], ctx
->digcnt
[0], final
);
536 atmel_sha_fill_padding(ctx
, 0);
538 if (final
|| (ctx
->bufcnt
== ctx
->buflen
)) {
541 return atmel_sha_xmit_dma_map(dd
, ctx
, count
, final
);
547 static int atmel_sha_update_dma_start(struct atmel_sha_dev
*dd
)
549 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
550 unsigned int length
, final
, tail
;
551 struct scatterlist
*sg
;
557 if (ctx
->bufcnt
|| ctx
->offset
)
558 return atmel_sha_update_dma_slow(dd
);
560 dev_dbg(dd
->dev
, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %u, total: %u\n",
561 ctx
->digcnt
[1], ctx
->digcnt
[0], ctx
->bufcnt
, ctx
->total
);
565 if (!IS_ALIGNED(sg
->offset
, sizeof(u32
)))
566 return atmel_sha_update_dma_slow(dd
);
568 if (!sg_is_last(sg
) && !IS_ALIGNED(sg
->length
, ctx
->block_size
))
569 /* size is not ctx->block_size aligned */
570 return atmel_sha_update_dma_slow(dd
);
572 length
= min(ctx
->total
, sg
->length
);
574 if (sg_is_last(sg
)) {
575 if (!(ctx
->flags
& SHA_FLAGS_FINUP
)) {
576 /* not last sg must be ctx->block_size aligned */
577 tail
= length
& (ctx
->block_size
- 1);
582 ctx
->total
-= length
;
583 ctx
->offset
= length
; /* offset where to start slow */
585 final
= (ctx
->flags
& SHA_FLAGS_FINUP
) && !ctx
->total
;
589 tail
= length
& (ctx
->block_size
- 1);
592 ctx
->offset
= length
; /* offset where to start slow */
595 atmel_sha_append_sg(ctx
);
597 atmel_sha_fill_padding(ctx
, length
);
599 ctx
->dma_addr
= dma_map_single(dd
->dev
, ctx
->buffer
,
600 ctx
->buflen
+ ctx
->block_size
, DMA_TO_DEVICE
);
601 if (dma_mapping_error(dd
->dev
, ctx
->dma_addr
)) {
602 dev_err(dd
->dev
, "dma %u bytes error\n",
603 ctx
->buflen
+ ctx
->block_size
);
608 ctx
->flags
&= ~SHA_FLAGS_SG
;
611 return atmel_sha_xmit_start(dd
, ctx
->dma_addr
, count
, 0,
615 if (!dma_map_sg(dd
->dev
, ctx
->sg
, 1,
617 dev_err(dd
->dev
, "dma_map_sg error\n");
621 ctx
->flags
|= SHA_FLAGS_SG
;
625 return atmel_sha_xmit_start(dd
, sg_dma_address(ctx
->sg
),
626 length
, ctx
->dma_addr
, count
, final
);
630 if (!dma_map_sg(dd
->dev
, ctx
->sg
, 1, DMA_TO_DEVICE
)) {
631 dev_err(dd
->dev
, "dma_map_sg error\n");
635 ctx
->flags
|= SHA_FLAGS_SG
;
637 /* next call does not fail... so no unmap in the case of error */
638 return atmel_sha_xmit_start(dd
, sg_dma_address(ctx
->sg
), length
, 0,
642 static int atmel_sha_update_dma_stop(struct atmel_sha_dev
*dd
)
644 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
646 if (ctx
->flags
& SHA_FLAGS_SG
) {
647 dma_unmap_sg(dd
->dev
, ctx
->sg
, 1, DMA_TO_DEVICE
);
648 if (ctx
->sg
->length
== ctx
->offset
) {
649 ctx
->sg
= sg_next(ctx
->sg
);
653 if (ctx
->flags
& SHA_FLAGS_PAD
) {
654 dma_unmap_single(dd
->dev
, ctx
->dma_addr
,
655 ctx
->buflen
+ ctx
->block_size
, DMA_TO_DEVICE
);
658 dma_unmap_single(dd
->dev
, ctx
->dma_addr
, ctx
->buflen
+
659 ctx
->block_size
, DMA_TO_DEVICE
);
665 static int atmel_sha_update_req(struct atmel_sha_dev
*dd
)
667 struct ahash_request
*req
= dd
->req
;
668 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
671 dev_dbg(dd
->dev
, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n",
672 ctx
->total
, ctx
->digcnt
[1], ctx
->digcnt
[0]);
674 if (ctx
->flags
& SHA_FLAGS_CPU
)
675 err
= atmel_sha_update_cpu(dd
);
677 err
= atmel_sha_update_dma_start(dd
);
679 /* wait for dma completion before can take more data */
680 dev_dbg(dd
->dev
, "update: err: %d, digcnt: 0x%llx 0%llx\n",
681 err
, ctx
->digcnt
[1], ctx
->digcnt
[0]);
686 static int atmel_sha_final_req(struct atmel_sha_dev
*dd
)
688 struct ahash_request
*req
= dd
->req
;
689 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
693 if (ctx
->bufcnt
>= ATMEL_SHA_DMA_THRESHOLD
) {
694 atmel_sha_fill_padding(ctx
, 0);
697 err
= atmel_sha_xmit_dma_map(dd
, ctx
, count
, 1);
699 /* faster to handle last block with cpu */
701 atmel_sha_fill_padding(ctx
, 0);
704 err
= atmel_sha_xmit_cpu(dd
, ctx
->buffer
, count
, 1);
707 dev_dbg(dd
->dev
, "final_req: err: %d\n", err
);
712 static void atmel_sha_copy_hash(struct ahash_request
*req
)
714 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
715 u32
*hash
= (u32
*)ctx
->digest
;
718 if (ctx
->flags
& SHA_FLAGS_SHA1
)
719 for (i
= 0; i
< SHA1_DIGEST_SIZE
/ sizeof(u32
); i
++)
720 hash
[i
] = atmel_sha_read(ctx
->dd
, SHA_REG_DIGEST(i
));
721 else if (ctx
->flags
& SHA_FLAGS_SHA224
)
722 for (i
= 0; i
< SHA224_DIGEST_SIZE
/ sizeof(u32
); i
++)
723 hash
[i
] = atmel_sha_read(ctx
->dd
, SHA_REG_DIGEST(i
));
724 else if (ctx
->flags
& SHA_FLAGS_SHA256
)
725 for (i
= 0; i
< SHA256_DIGEST_SIZE
/ sizeof(u32
); i
++)
726 hash
[i
] = atmel_sha_read(ctx
->dd
, SHA_REG_DIGEST(i
));
727 else if (ctx
->flags
& SHA_FLAGS_SHA384
)
728 for (i
= 0; i
< SHA384_DIGEST_SIZE
/ sizeof(u32
); i
++)
729 hash
[i
] = atmel_sha_read(ctx
->dd
, SHA_REG_DIGEST(i
));
731 for (i
= 0; i
< SHA512_DIGEST_SIZE
/ sizeof(u32
); i
++)
732 hash
[i
] = atmel_sha_read(ctx
->dd
, SHA_REG_DIGEST(i
));
735 static void atmel_sha_copy_ready_hash(struct ahash_request
*req
)
737 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
742 if (ctx
->flags
& SHA_FLAGS_SHA1
)
743 memcpy(req
->result
, ctx
->digest
, SHA1_DIGEST_SIZE
);
744 else if (ctx
->flags
& SHA_FLAGS_SHA224
)
745 memcpy(req
->result
, ctx
->digest
, SHA224_DIGEST_SIZE
);
746 else if (ctx
->flags
& SHA_FLAGS_SHA256
)
747 memcpy(req
->result
, ctx
->digest
, SHA256_DIGEST_SIZE
);
748 else if (ctx
->flags
& SHA_FLAGS_SHA384
)
749 memcpy(req
->result
, ctx
->digest
, SHA384_DIGEST_SIZE
);
751 memcpy(req
->result
, ctx
->digest
, SHA512_DIGEST_SIZE
);
754 static int atmel_sha_finish(struct ahash_request
*req
)
756 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
757 struct atmel_sha_dev
*dd
= ctx
->dd
;
760 if (ctx
->digcnt
[0] || ctx
->digcnt
[1])
761 atmel_sha_copy_ready_hash(req
);
763 dev_dbg(dd
->dev
, "digcnt: 0x%llx 0x%llx, bufcnt: %d\n", ctx
->digcnt
[1],
764 ctx
->digcnt
[0], ctx
->bufcnt
);
769 static void atmel_sha_finish_req(struct ahash_request
*req
, int err
)
771 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
772 struct atmel_sha_dev
*dd
= ctx
->dd
;
775 atmel_sha_copy_hash(req
);
776 if (SHA_FLAGS_FINAL
& dd
->flags
)
777 err
= atmel_sha_finish(req
);
779 ctx
->flags
|= SHA_FLAGS_ERROR
;
782 /* atomic operation is not needed here */
783 dd
->flags
&= ~(SHA_FLAGS_BUSY
| SHA_FLAGS_FINAL
| SHA_FLAGS_CPU
|
784 SHA_FLAGS_DMA_READY
| SHA_FLAGS_OUTPUT_READY
);
786 clk_disable(dd
->iclk
);
788 if (req
->base
.complete
)
789 req
->base
.complete(&req
->base
, err
);
791 /* handle new request */
792 tasklet_schedule(&dd
->done_task
);
795 static int atmel_sha_hw_init(struct atmel_sha_dev
*dd
)
799 err
= clk_enable(dd
->iclk
);
803 if (!(SHA_FLAGS_INIT
& dd
->flags
)) {
804 atmel_sha_write(dd
, SHA_CR
, SHA_CR_SWRST
);
805 dd
->flags
|= SHA_FLAGS_INIT
;
812 static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev
*dd
)
814 return atmel_sha_read(dd
, SHA_HW_VERSION
) & 0x00000fff;
817 static void atmel_sha_hw_version_init(struct atmel_sha_dev
*dd
)
819 atmel_sha_hw_init(dd
);
821 dd
->hw_version
= atmel_sha_get_version(dd
);
824 "version: 0x%x\n", dd
->hw_version
);
826 clk_disable(dd
->iclk
);
829 static int atmel_sha_handle_queue(struct atmel_sha_dev
*dd
,
830 struct ahash_request
*req
)
832 struct crypto_async_request
*async_req
, *backlog
;
833 struct atmel_sha_reqctx
*ctx
;
835 int err
= 0, ret
= 0;
837 spin_lock_irqsave(&dd
->lock
, flags
);
839 ret
= ahash_enqueue_request(&dd
->queue
, req
);
841 if (SHA_FLAGS_BUSY
& dd
->flags
) {
842 spin_unlock_irqrestore(&dd
->lock
, flags
);
846 backlog
= crypto_get_backlog(&dd
->queue
);
847 async_req
= crypto_dequeue_request(&dd
->queue
);
849 dd
->flags
|= SHA_FLAGS_BUSY
;
851 spin_unlock_irqrestore(&dd
->lock
, flags
);
857 backlog
->complete(backlog
, -EINPROGRESS
);
859 req
= ahash_request_cast(async_req
);
861 ctx
= ahash_request_ctx(req
);
863 dev_dbg(dd
->dev
, "handling new req, op: %lu, nbytes: %d\n",
864 ctx
->op
, req
->nbytes
);
866 err
= atmel_sha_hw_init(dd
);
871 if (ctx
->op
== SHA_OP_UPDATE
) {
872 err
= atmel_sha_update_req(dd
);
873 if (err
!= -EINPROGRESS
&& (ctx
->flags
& SHA_FLAGS_FINUP
))
874 /* no final() after finup() */
875 err
= atmel_sha_final_req(dd
);
876 } else if (ctx
->op
== SHA_OP_FINAL
) {
877 err
= atmel_sha_final_req(dd
);
881 if (err
!= -EINPROGRESS
)
882 /* done_task will not finish it, so do it here */
883 atmel_sha_finish_req(req
, err
);
885 dev_dbg(dd
->dev
, "exit, err: %d\n", err
);
890 static int atmel_sha_enqueue(struct ahash_request
*req
, unsigned int op
)
892 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
893 struct atmel_sha_ctx
*tctx
= crypto_tfm_ctx(req
->base
.tfm
);
894 struct atmel_sha_dev
*dd
= tctx
->dd
;
898 return atmel_sha_handle_queue(dd
, req
);
901 static int atmel_sha_update(struct ahash_request
*req
)
903 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
908 ctx
->total
= req
->nbytes
;
912 if (ctx
->flags
& SHA_FLAGS_FINUP
) {
913 if (ctx
->bufcnt
+ ctx
->total
< ATMEL_SHA_DMA_THRESHOLD
)
914 /* faster to use CPU for short transfers */
915 ctx
->flags
|= SHA_FLAGS_CPU
;
916 } else if (ctx
->bufcnt
+ ctx
->total
< ctx
->buflen
) {
917 atmel_sha_append_sg(ctx
);
920 return atmel_sha_enqueue(req
, SHA_OP_UPDATE
);
923 static int atmel_sha_final(struct ahash_request
*req
)
925 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
926 struct atmel_sha_ctx
*tctx
= crypto_tfm_ctx(req
->base
.tfm
);
927 struct atmel_sha_dev
*dd
= tctx
->dd
;
931 ctx
->flags
|= SHA_FLAGS_FINUP
;
933 if (ctx
->flags
& SHA_FLAGS_ERROR
)
934 return 0; /* uncompleted hash is not needed */
937 return atmel_sha_enqueue(req
, SHA_OP_FINAL
);
938 } else if (!(ctx
->flags
& SHA_FLAGS_PAD
)) { /* add padding */
939 err
= atmel_sha_hw_init(dd
);
943 dd
->flags
|= SHA_FLAGS_BUSY
;
944 err
= atmel_sha_final_req(dd
);
946 /* copy ready hash (+ finalize hmac) */
947 return atmel_sha_finish(req
);
951 if (err
!= -EINPROGRESS
)
952 /* done_task will not finish it, so do it here */
953 atmel_sha_finish_req(req
, err
);
958 static int atmel_sha_finup(struct ahash_request
*req
)
960 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
963 ctx
->flags
|= SHA_FLAGS_FINUP
;
965 err1
= atmel_sha_update(req
);
966 if (err1
== -EINPROGRESS
||
967 (err1
== -EBUSY
&& (ahash_request_flags(req
) &
968 CRYPTO_TFM_REQ_MAY_BACKLOG
)))
972 * final() has to be always called to cleanup resources
973 * even if udpate() failed, except EINPROGRESS
975 err2
= atmel_sha_final(req
);
980 static int atmel_sha_digest(struct ahash_request
*req
)
982 return atmel_sha_init(req
) ?: atmel_sha_finup(req
);
985 static int atmel_sha_cra_init(struct crypto_tfm
*tfm
)
987 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
988 sizeof(struct atmel_sha_reqctx
) +
989 SHA_BUFFER_LEN
+ SHA512_BLOCK_SIZE
);
994 static struct ahash_alg sha_1_256_algs
[] = {
996 .init
= atmel_sha_init
,
997 .update
= atmel_sha_update
,
998 .final
= atmel_sha_final
,
999 .finup
= atmel_sha_finup
,
1000 .digest
= atmel_sha_digest
,
1002 .digestsize
= SHA1_DIGEST_SIZE
,
1005 .cra_driver_name
= "atmel-sha1",
1006 .cra_priority
= 100,
1007 .cra_flags
= CRYPTO_ALG_ASYNC
,
1008 .cra_blocksize
= SHA1_BLOCK_SIZE
,
1009 .cra_ctxsize
= sizeof(struct atmel_sha_ctx
),
1011 .cra_module
= THIS_MODULE
,
1012 .cra_init
= atmel_sha_cra_init
,
1017 .init
= atmel_sha_init
,
1018 .update
= atmel_sha_update
,
1019 .final
= atmel_sha_final
,
1020 .finup
= atmel_sha_finup
,
1021 .digest
= atmel_sha_digest
,
1023 .digestsize
= SHA256_DIGEST_SIZE
,
1025 .cra_name
= "sha256",
1026 .cra_driver_name
= "atmel-sha256",
1027 .cra_priority
= 100,
1028 .cra_flags
= CRYPTO_ALG_ASYNC
,
1029 .cra_blocksize
= SHA256_BLOCK_SIZE
,
1030 .cra_ctxsize
= sizeof(struct atmel_sha_ctx
),
1032 .cra_module
= THIS_MODULE
,
1033 .cra_init
= atmel_sha_cra_init
,
1039 static struct ahash_alg sha_224_alg
= {
1040 .init
= atmel_sha_init
,
1041 .update
= atmel_sha_update
,
1042 .final
= atmel_sha_final
,
1043 .finup
= atmel_sha_finup
,
1044 .digest
= atmel_sha_digest
,
1046 .digestsize
= SHA224_DIGEST_SIZE
,
1048 .cra_name
= "sha224",
1049 .cra_driver_name
= "atmel-sha224",
1050 .cra_priority
= 100,
1051 .cra_flags
= CRYPTO_ALG_ASYNC
,
1052 .cra_blocksize
= SHA224_BLOCK_SIZE
,
1053 .cra_ctxsize
= sizeof(struct atmel_sha_ctx
),
1055 .cra_module
= THIS_MODULE
,
1056 .cra_init
= atmel_sha_cra_init
,
1061 static struct ahash_alg sha_384_512_algs
[] = {
1063 .init
= atmel_sha_init
,
1064 .update
= atmel_sha_update
,
1065 .final
= atmel_sha_final
,
1066 .finup
= atmel_sha_finup
,
1067 .digest
= atmel_sha_digest
,
1069 .digestsize
= SHA384_DIGEST_SIZE
,
1071 .cra_name
= "sha384",
1072 .cra_driver_name
= "atmel-sha384",
1073 .cra_priority
= 100,
1074 .cra_flags
= CRYPTO_ALG_ASYNC
,
1075 .cra_blocksize
= SHA384_BLOCK_SIZE
,
1076 .cra_ctxsize
= sizeof(struct atmel_sha_ctx
),
1077 .cra_alignmask
= 0x3,
1078 .cra_module
= THIS_MODULE
,
1079 .cra_init
= atmel_sha_cra_init
,
1084 .init
= atmel_sha_init
,
1085 .update
= atmel_sha_update
,
1086 .final
= atmel_sha_final
,
1087 .finup
= atmel_sha_finup
,
1088 .digest
= atmel_sha_digest
,
1090 .digestsize
= SHA512_DIGEST_SIZE
,
1092 .cra_name
= "sha512",
1093 .cra_driver_name
= "atmel-sha512",
1094 .cra_priority
= 100,
1095 .cra_flags
= CRYPTO_ALG_ASYNC
,
1096 .cra_blocksize
= SHA512_BLOCK_SIZE
,
1097 .cra_ctxsize
= sizeof(struct atmel_sha_ctx
),
1098 .cra_alignmask
= 0x3,
1099 .cra_module
= THIS_MODULE
,
1100 .cra_init
= atmel_sha_cra_init
,
1106 static void atmel_sha_done_task(unsigned long data
)
1108 struct atmel_sha_dev
*dd
= (struct atmel_sha_dev
*)data
;
1111 if (!(SHA_FLAGS_BUSY
& dd
->flags
)) {
1112 atmel_sha_handle_queue(dd
, NULL
);
1116 if (SHA_FLAGS_CPU
& dd
->flags
) {
1117 if (SHA_FLAGS_OUTPUT_READY
& dd
->flags
) {
1118 dd
->flags
&= ~SHA_FLAGS_OUTPUT_READY
;
1121 } else if (SHA_FLAGS_DMA_READY
& dd
->flags
) {
1122 if (SHA_FLAGS_DMA_ACTIVE
& dd
->flags
) {
1123 dd
->flags
&= ~SHA_FLAGS_DMA_ACTIVE
;
1124 atmel_sha_update_dma_stop(dd
);
1130 if (SHA_FLAGS_OUTPUT_READY
& dd
->flags
) {
1131 /* hash or semi-hash ready */
1132 dd
->flags
&= ~(SHA_FLAGS_DMA_READY
|
1133 SHA_FLAGS_OUTPUT_READY
);
1134 err
= atmel_sha_update_dma_start(dd
);
1135 if (err
!= -EINPROGRESS
)
1142 /* finish curent request */
1143 atmel_sha_finish_req(dd
->req
, err
);
1146 static irqreturn_t
atmel_sha_irq(int irq
, void *dev_id
)
1148 struct atmel_sha_dev
*sha_dd
= dev_id
;
1151 reg
= atmel_sha_read(sha_dd
, SHA_ISR
);
1152 if (reg
& atmel_sha_read(sha_dd
, SHA_IMR
)) {
1153 atmel_sha_write(sha_dd
, SHA_IDR
, reg
);
1154 if (SHA_FLAGS_BUSY
& sha_dd
->flags
) {
1155 sha_dd
->flags
|= SHA_FLAGS_OUTPUT_READY
;
1156 if (!(SHA_FLAGS_CPU
& sha_dd
->flags
))
1157 sha_dd
->flags
|= SHA_FLAGS_DMA_READY
;
1158 tasklet_schedule(&sha_dd
->done_task
);
1160 dev_warn(sha_dd
->dev
, "SHA interrupt when no active requests.\n");
1168 static void atmel_sha_unregister_algs(struct atmel_sha_dev
*dd
)
1172 for (i
= 0; i
< ARRAY_SIZE(sha_1_256_algs
); i
++)
1173 crypto_unregister_ahash(&sha_1_256_algs
[i
]);
1175 if (dd
->caps
.has_sha224
)
1176 crypto_unregister_ahash(&sha_224_alg
);
1178 if (dd
->caps
.has_sha_384_512
) {
1179 for (i
= 0; i
< ARRAY_SIZE(sha_384_512_algs
); i
++)
1180 crypto_unregister_ahash(&sha_384_512_algs
[i
]);
1184 static int atmel_sha_register_algs(struct atmel_sha_dev
*dd
)
1188 for (i
= 0; i
< ARRAY_SIZE(sha_1_256_algs
); i
++) {
1189 err
= crypto_register_ahash(&sha_1_256_algs
[i
]);
1191 goto err_sha_1_256_algs
;
1194 if (dd
->caps
.has_sha224
) {
1195 err
= crypto_register_ahash(&sha_224_alg
);
1197 goto err_sha_224_algs
;
1200 if (dd
->caps
.has_sha_384_512
) {
1201 for (i
= 0; i
< ARRAY_SIZE(sha_384_512_algs
); i
++) {
1202 err
= crypto_register_ahash(&sha_384_512_algs
[i
]);
1204 goto err_sha_384_512_algs
;
1210 err_sha_384_512_algs
:
1211 for (j
= 0; j
< i
; j
++)
1212 crypto_unregister_ahash(&sha_384_512_algs
[j
]);
1213 crypto_unregister_ahash(&sha_224_alg
);
1215 i
= ARRAY_SIZE(sha_1_256_algs
);
1217 for (j
= 0; j
< i
; j
++)
1218 crypto_unregister_ahash(&sha_1_256_algs
[j
]);
1223 static bool atmel_sha_filter(struct dma_chan
*chan
, void *slave
)
1225 struct at_dma_slave
*sl
= slave
;
1227 if (sl
&& sl
->dma_dev
== chan
->device
->dev
) {
1235 static int atmel_sha_dma_init(struct atmel_sha_dev
*dd
,
1236 struct crypto_platform_data
*pdata
)
1239 dma_cap_mask_t mask_in
;
1241 /* Try to grab DMA channel */
1242 dma_cap_zero(mask_in
);
1243 dma_cap_set(DMA_SLAVE
, mask_in
);
1245 dd
->dma_lch_in
.chan
= dma_request_slave_channel_compat(mask_in
,
1246 atmel_sha_filter
, &pdata
->dma_slave
->rxdata
, dd
->dev
, "tx");
1247 if (!dd
->dma_lch_in
.chan
) {
1248 dev_warn(dd
->dev
, "no DMA channel available\n");
1252 dd
->dma_lch_in
.dma_conf
.direction
= DMA_MEM_TO_DEV
;
1253 dd
->dma_lch_in
.dma_conf
.dst_addr
= dd
->phys_base
+
1255 dd
->dma_lch_in
.dma_conf
.src_maxburst
= 1;
1256 dd
->dma_lch_in
.dma_conf
.src_addr_width
=
1257 DMA_SLAVE_BUSWIDTH_4_BYTES
;
1258 dd
->dma_lch_in
.dma_conf
.dst_maxburst
= 1;
1259 dd
->dma_lch_in
.dma_conf
.dst_addr_width
=
1260 DMA_SLAVE_BUSWIDTH_4_BYTES
;
1261 dd
->dma_lch_in
.dma_conf
.device_fc
= false;
1266 static void atmel_sha_dma_cleanup(struct atmel_sha_dev
*dd
)
1268 dma_release_channel(dd
->dma_lch_in
.chan
);
1271 static void atmel_sha_get_cap(struct atmel_sha_dev
*dd
)
1274 dd
->caps
.has_dma
= 0;
1275 dd
->caps
.has_dualbuff
= 0;
1276 dd
->caps
.has_sha224
= 0;
1277 dd
->caps
.has_sha_384_512
= 0;
1279 /* keep only major version number */
1280 switch (dd
->hw_version
& 0xff0) {
1282 dd
->caps
.has_dma
= 1;
1283 dd
->caps
.has_dualbuff
= 1;
1284 dd
->caps
.has_sha224
= 1;
1285 dd
->caps
.has_sha_384_512
= 1;
1288 dd
->caps
.has_dma
= 1;
1289 dd
->caps
.has_dualbuff
= 1;
1290 dd
->caps
.has_sha224
= 1;
1291 dd
->caps
.has_sha_384_512
= 1;
1294 dd
->caps
.has_dma
= 1;
1295 dd
->caps
.has_dualbuff
= 1;
1296 dd
->caps
.has_sha224
= 1;
1302 "Unmanaged sha version, set minimum capabilities\n");
1307 #if defined(CONFIG_OF)
1308 static const struct of_device_id atmel_sha_dt_ids
[] = {
1309 { .compatible
= "atmel,at91sam9g46-sha" },
1313 MODULE_DEVICE_TABLE(of
, atmel_sha_dt_ids
);
1315 static struct crypto_platform_data
*atmel_sha_of_init(struct platform_device
*pdev
)
1317 struct device_node
*np
= pdev
->dev
.of_node
;
1318 struct crypto_platform_data
*pdata
;
1321 dev_err(&pdev
->dev
, "device node not found\n");
1322 return ERR_PTR(-EINVAL
);
1325 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
1327 dev_err(&pdev
->dev
, "could not allocate memory for pdata\n");
1328 return ERR_PTR(-ENOMEM
);
1331 pdata
->dma_slave
= devm_kzalloc(&pdev
->dev
,
1332 sizeof(*(pdata
->dma_slave
)),
1334 if (!pdata
->dma_slave
) {
1335 dev_err(&pdev
->dev
, "could not allocate memory for dma_slave\n");
1336 return ERR_PTR(-ENOMEM
);
1341 #else /* CONFIG_OF */
1342 static inline struct crypto_platform_data
*atmel_sha_of_init(struct platform_device
*dev
)
1344 return ERR_PTR(-EINVAL
);
1348 static int atmel_sha_probe(struct platform_device
*pdev
)
1350 struct atmel_sha_dev
*sha_dd
;
1351 struct crypto_platform_data
*pdata
;
1352 struct device
*dev
= &pdev
->dev
;
1353 struct resource
*sha_res
;
1356 sha_dd
= devm_kzalloc(&pdev
->dev
, sizeof(*sha_dd
), GFP_KERNEL
);
1357 if (sha_dd
== NULL
) {
1358 dev_err(dev
, "unable to alloc data struct.\n");
1365 platform_set_drvdata(pdev
, sha_dd
);
1367 INIT_LIST_HEAD(&sha_dd
->list
);
1368 spin_lock_init(&sha_dd
->lock
);
1370 tasklet_init(&sha_dd
->done_task
, atmel_sha_done_task
,
1371 (unsigned long)sha_dd
);
1373 crypto_init_queue(&sha_dd
->queue
, ATMEL_SHA_QUEUE_LENGTH
);
1377 /* Get the base address */
1378 sha_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1380 dev_err(dev
, "no MEM resource info\n");
1384 sha_dd
->phys_base
= sha_res
->start
;
1387 sha_dd
->irq
= platform_get_irq(pdev
, 0);
1388 if (sha_dd
->irq
< 0) {
1389 dev_err(dev
, "no IRQ resource info\n");
1394 err
= devm_request_irq(&pdev
->dev
, sha_dd
->irq
, atmel_sha_irq
,
1395 IRQF_SHARED
, "atmel-sha", sha_dd
);
1397 dev_err(dev
, "unable to request sha irq.\n");
1401 /* Initializing the clock */
1402 sha_dd
->iclk
= devm_clk_get(&pdev
->dev
, "sha_clk");
1403 if (IS_ERR(sha_dd
->iclk
)) {
1404 dev_err(dev
, "clock initialization failed.\n");
1405 err
= PTR_ERR(sha_dd
->iclk
);
1409 sha_dd
->io_base
= devm_ioremap_resource(&pdev
->dev
, sha_res
);
1410 if (IS_ERR(sha_dd
->io_base
)) {
1411 dev_err(dev
, "can't ioremap\n");
1412 err
= PTR_ERR(sha_dd
->io_base
);
1416 err
= clk_prepare(sha_dd
->iclk
);
1420 atmel_sha_hw_version_init(sha_dd
);
1422 atmel_sha_get_cap(sha_dd
);
1424 if (sha_dd
->caps
.has_dma
) {
1425 pdata
= pdev
->dev
.platform_data
;
1427 pdata
= atmel_sha_of_init(pdev
);
1428 if (IS_ERR(pdata
)) {
1429 dev_err(&pdev
->dev
, "platform data not available\n");
1430 err
= PTR_ERR(pdata
);
1431 goto iclk_unprepare
;
1434 if (!pdata
->dma_slave
) {
1436 goto iclk_unprepare
;
1438 err
= atmel_sha_dma_init(sha_dd
, pdata
);
1442 dev_info(dev
, "using %s for DMA transfers\n",
1443 dma_chan_name(sha_dd
->dma_lch_in
.chan
));
1446 spin_lock(&atmel_sha
.lock
);
1447 list_add_tail(&sha_dd
->list
, &atmel_sha
.dev_list
);
1448 spin_unlock(&atmel_sha
.lock
);
1450 err
= atmel_sha_register_algs(sha_dd
);
1454 dev_info(dev
, "Atmel SHA1/SHA256%s%s\n",
1455 sha_dd
->caps
.has_sha224
? "/SHA224" : "",
1456 sha_dd
->caps
.has_sha_384_512
? "/SHA384/SHA512" : "");
1461 spin_lock(&atmel_sha
.lock
);
1462 list_del(&sha_dd
->list
);
1463 spin_unlock(&atmel_sha
.lock
);
1464 if (sha_dd
->caps
.has_dma
)
1465 atmel_sha_dma_cleanup(sha_dd
);
1468 clk_unprepare(sha_dd
->iclk
);
1470 tasklet_kill(&sha_dd
->done_task
);
1472 dev_err(dev
, "initialization failed.\n");
1477 static int atmel_sha_remove(struct platform_device
*pdev
)
1479 static struct atmel_sha_dev
*sha_dd
;
1481 sha_dd
= platform_get_drvdata(pdev
);
1484 spin_lock(&atmel_sha
.lock
);
1485 list_del(&sha_dd
->list
);
1486 spin_unlock(&atmel_sha
.lock
);
1488 atmel_sha_unregister_algs(sha_dd
);
1490 tasklet_kill(&sha_dd
->done_task
);
1492 if (sha_dd
->caps
.has_dma
)
1493 atmel_sha_dma_cleanup(sha_dd
);
1495 clk_unprepare(sha_dd
->iclk
);
1500 static struct platform_driver atmel_sha_driver
= {
1501 .probe
= atmel_sha_probe
,
1502 .remove
= atmel_sha_remove
,
1504 .name
= "atmel_sha",
1505 .of_match_table
= of_match_ptr(atmel_sha_dt_ids
),
1509 module_platform_driver(atmel_sha_driver
);
1511 MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
1512 MODULE_LICENSE("GPL v2");
1513 MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");