2 * Copyright (c) 2014 Imagination Technologies
3 * Authors: Will Thomas, James Hartley
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation.
9 * Interface structure taken from omap-sham driver
12 #include <linux/clk.h>
13 #include <linux/dmaengine.h>
14 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/scatterlist.h>
22 #include <crypto/internal/hash.h>
23 #include <crypto/md5.h>
24 #include <crypto/sha.h>
27 #define CR_RESET_SET 1
28 #define CR_RESET_UNSET 0
30 #define CR_MESSAGE_LENGTH_H 0x4
31 #define CR_MESSAGE_LENGTH_L 0x8
33 #define CR_CONTROL 0xc
34 #define CR_CONTROL_BYTE_ORDER_3210 0
35 #define CR_CONTROL_BYTE_ORDER_0123 1
36 #define CR_CONTROL_BYTE_ORDER_2310 2
37 #define CR_CONTROL_BYTE_ORDER_1032 3
38 #define CR_CONTROL_BYTE_ORDER_SHIFT 8
39 #define CR_CONTROL_ALGO_MD5 0
40 #define CR_CONTROL_ALGO_SHA1 1
41 #define CR_CONTROL_ALGO_SHA224 2
42 #define CR_CONTROL_ALGO_SHA256 3
44 #define CR_INTSTAT 0x10
45 #define CR_INTENAB 0x14
46 #define CR_INTCLEAR 0x18
47 #define CR_INT_RESULTS_AVAILABLE BIT(0)
48 #define CR_INT_NEW_RESULTS_SET BIT(1)
49 #define CR_INT_RESULT_READ_ERR BIT(2)
50 #define CR_INT_MESSAGE_WRITE_ERROR BIT(3)
51 #define CR_INT_STATUS BIT(8)
53 #define CR_RESULT_QUEUE 0x1c
55 #define CR_CORE_REV 0x50
56 #define CR_CORE_DES1 0x60
57 #define CR_CORE_DES2 0x70
59 #define DRIVER_FLAGS_BUSY BIT(0)
60 #define DRIVER_FLAGS_FINAL BIT(1)
61 #define DRIVER_FLAGS_DMA_ACTIVE BIT(2)
62 #define DRIVER_FLAGS_OUTPUT_READY BIT(3)
63 #define DRIVER_FLAGS_INIT BIT(4)
64 #define DRIVER_FLAGS_CPU BIT(5)
65 #define DRIVER_FLAGS_DMA_READY BIT(6)
66 #define DRIVER_FLAGS_ERROR BIT(7)
67 #define DRIVER_FLAGS_SG BIT(8)
68 #define DRIVER_FLAGS_SHA1 BIT(18)
69 #define DRIVER_FLAGS_SHA224 BIT(19)
70 #define DRIVER_FLAGS_SHA256 BIT(20)
71 #define DRIVER_FLAGS_MD5 BIT(21)
73 #define IMG_HASH_QUEUE_LENGTH 20
74 #define IMG_HASH_DMA_THRESHOLD 64
76 #ifdef __LITTLE_ENDIAN
77 #define IMG_HASH_BYTE_ORDER CR_CONTROL_BYTE_ORDER_3210
79 #define IMG_HASH_BYTE_ORDER CR_CONTROL_BYTE_ORDER_0123
84 struct img_hash_request_ctx {
85 struct img_hash_dev *hdev;
86 u8 digest[SHA256_DIGEST_SIZE] __aligned(sizeof(u32));
94 struct scatterlist *sgfirst;
96 struct scatterlist *sg;
105 struct ahash_request fallback_req;
107 /* Zero length buffer must remain last member of struct */
108 u8 buffer[0] __aligned(sizeof(u32));
111 struct img_hash_ctx {
112 struct img_hash_dev *hdev;
114 struct crypto_ahash *fallback;
117 struct img_hash_dev {
118 struct list_head list;
120 struct clk *hash_clk;
122 void __iomem *io_base;
124 phys_addr_t bus_addr;
125 void __iomem *cpu_addr;
129 struct tasklet_struct done_task;
130 struct tasklet_struct dma_task;
133 struct crypto_queue queue;
134 struct ahash_request *req;
136 struct dma_chan *dma_lch;
139 struct img_hash_drv {
140 struct list_head dev_list;
144 static struct img_hash_drv img_hash = {
145 .dev_list = LIST_HEAD_INIT(img_hash.dev_list),
146 .lock = __SPIN_LOCK_UNLOCKED(img_hash.lock),
149 static inline u32 img_hash_read(struct img_hash_dev *hdev, u32 offset)
151 return readl_relaxed(hdev->io_base + offset);
154 static inline void img_hash_write(struct img_hash_dev *hdev,
155 u32 offset, u32 value)
157 writel_relaxed(value, hdev->io_base + offset);
160 static inline u32 img_hash_read_result_queue(struct img_hash_dev *hdev)
162 return be32_to_cpu(img_hash_read(hdev, CR_RESULT_QUEUE));
165 static void img_hash_start(struct img_hash_dev *hdev, bool dma)
167 struct img_hash_request_ctx *ctx = ahash_request_ctx(hdev->req);
168 u32 cr = IMG_HASH_BYTE_ORDER << CR_CONTROL_BYTE_ORDER_SHIFT;
170 if (ctx->flags & DRIVER_FLAGS_MD5)
171 cr |= CR_CONTROL_ALGO_MD5;
172 else if (ctx->flags & DRIVER_FLAGS_SHA1)
173 cr |= CR_CONTROL_ALGO_SHA1;
174 else if (ctx->flags & DRIVER_FLAGS_SHA224)
175 cr |= CR_CONTROL_ALGO_SHA224;
176 else if (ctx->flags & DRIVER_FLAGS_SHA256)
177 cr |= CR_CONTROL_ALGO_SHA256;
178 dev_dbg(hdev->dev, "Starting hash process\n");
179 img_hash_write(hdev, CR_CONTROL, cr);
182 * The hardware block requires two cycles between writing the control
183 * register and writing the first word of data in non DMA mode, to
184 * ensure the first data write is not grouped in burst with the control
185 * register write a read is issued to 'flush' the bus.
188 img_hash_read(hdev, CR_CONTROL);
191 static int img_hash_xmit_cpu(struct img_hash_dev *hdev, const u8 *buf,
192 size_t length, int final)
195 const u32 *buffer = (const u32 *)buf;
197 dev_dbg(hdev->dev, "xmit_cpu: length: %zu bytes\n", length);
200 hdev->flags |= DRIVER_FLAGS_FINAL;
202 len32 = DIV_ROUND_UP(length, sizeof(u32));
204 for (count = 0; count < len32; count++)
205 writel_relaxed(buffer[count], hdev->cpu_addr);
210 static void img_hash_dma_callback(void *data)
212 struct img_hash_dev *hdev = (struct img_hash_dev *)data;
213 struct img_hash_request_ctx *ctx = ahash_request_ctx(hdev->req);
216 img_hash_xmit_cpu(hdev, ctx->buffer, ctx->bufcnt, 0);
220 tasklet_schedule(&hdev->dma_task);
223 static int img_hash_xmit_dma(struct img_hash_dev *hdev, struct scatterlist *sg)
225 struct dma_async_tx_descriptor *desc;
226 struct img_hash_request_ctx *ctx = ahash_request_ctx(hdev->req);
228 ctx->dma_ct = dma_map_sg(hdev->dev, sg, 1, DMA_MEM_TO_DEV);
229 if (ctx->dma_ct == 0) {
230 dev_err(hdev->dev, "Invalid DMA sg\n");
235 desc = dmaengine_prep_slave_sg(hdev->dma_lch,
239 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
241 dev_err(hdev->dev, "Null DMA descriptor\n");
243 dma_unmap_sg(hdev->dev, sg, 1, DMA_MEM_TO_DEV);
246 desc->callback = img_hash_dma_callback;
247 desc->callback_param = hdev;
248 dmaengine_submit(desc);
249 dma_async_issue_pending(hdev->dma_lch);
254 static int img_hash_write_via_cpu(struct img_hash_dev *hdev)
256 struct img_hash_request_ctx *ctx = ahash_request_ctx(hdev->req);
258 ctx->bufcnt = sg_copy_to_buffer(hdev->req->src, sg_nents(ctx->sg),
259 ctx->buffer, hdev->req->nbytes);
261 ctx->total = hdev->req->nbytes;
264 hdev->flags |= (DRIVER_FLAGS_CPU | DRIVER_FLAGS_FINAL);
266 img_hash_start(hdev, false);
268 return img_hash_xmit_cpu(hdev, ctx->buffer, ctx->total, 1);
271 static int img_hash_finish(struct ahash_request *req)
273 struct img_hash_request_ctx *ctx = ahash_request_ctx(req);
278 memcpy(req->result, ctx->digest, ctx->digsize);
283 static void img_hash_copy_hash(struct ahash_request *req)
285 struct img_hash_request_ctx *ctx = ahash_request_ctx(req);
286 u32 *hash = (u32 *)ctx->digest;
289 for (i = (ctx->digsize / sizeof(u32)) - 1; i >= 0; i--)
290 hash[i] = img_hash_read_result_queue(ctx->hdev);
293 static void img_hash_finish_req(struct ahash_request *req, int err)
295 struct img_hash_request_ctx *ctx = ahash_request_ctx(req);
296 struct img_hash_dev *hdev = ctx->hdev;
299 img_hash_copy_hash(req);
300 if (DRIVER_FLAGS_FINAL & hdev->flags)
301 err = img_hash_finish(req);
303 dev_warn(hdev->dev, "Hash failed with error %d\n", err);
304 ctx->flags |= DRIVER_FLAGS_ERROR;
307 hdev->flags &= ~(DRIVER_FLAGS_DMA_READY | DRIVER_FLAGS_OUTPUT_READY |
308 DRIVER_FLAGS_CPU | DRIVER_FLAGS_BUSY | DRIVER_FLAGS_FINAL);
310 if (req->base.complete)
311 req->base.complete(&req->base, err);
314 static int img_hash_write_via_dma(struct img_hash_dev *hdev)
316 struct img_hash_request_ctx *ctx = ahash_request_ctx(hdev->req);
318 img_hash_start(hdev, true);
320 dev_dbg(hdev->dev, "xmit dma size: %d\n", ctx->total);
323 hdev->flags |= DRIVER_FLAGS_FINAL;
325 hdev->flags |= DRIVER_FLAGS_DMA_ACTIVE | DRIVER_FLAGS_FINAL;
327 tasklet_schedule(&hdev->dma_task);
332 static int img_hash_dma_init(struct img_hash_dev *hdev)
334 struct dma_slave_config dma_conf;
337 hdev->dma_lch = dma_request_slave_channel(hdev->dev, "tx");
338 if (!hdev->dma_lch) {
339 dev_err(hdev->dev, "Couldn't acquire a slave DMA channel.\n");
342 dma_conf.direction = DMA_MEM_TO_DEV;
343 dma_conf.dst_addr = hdev->bus_addr;
344 dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
345 dma_conf.dst_maxburst = 16;
346 dma_conf.device_fc = false;
348 err = dmaengine_slave_config(hdev->dma_lch, &dma_conf);
350 dev_err(hdev->dev, "Couldn't configure DMA slave.\n");
351 dma_release_channel(hdev->dma_lch);
358 static void img_hash_dma_task(unsigned long d)
360 struct img_hash_dev *hdev = (struct img_hash_dev *)d;
361 struct img_hash_request_ctx *ctx = ahash_request_ctx(hdev->req);
363 size_t nbytes, bleft, wsend, len, tbc;
364 struct scatterlist tsg;
366 if (!hdev->req || !ctx->sg)
369 addr = sg_virt(ctx->sg);
370 nbytes = ctx->sg->length - ctx->offset;
373 * The hash accelerator does not support a data valid mask. This means
374 * that if each dma (i.e. per page) is not a multiple of 4 bytes, the
375 * padding bytes in the last word written by that dma would erroneously
376 * be included in the hash. To avoid this we round down the transfer,
377 * and add the excess to the start of the next dma. It does not matter
378 * that the final dma may not be a multiple of 4 bytes as the hashing
379 * block is programmed to accept the correct number of bytes.
383 wsend = (nbytes / 4);
386 sg_init_one(&tsg, addr + ctx->offset, wsend * 4);
387 if (img_hash_xmit_dma(hdev, &tsg)) {
388 dev_err(hdev->dev, "DMA failed, falling back to CPU");
389 ctx->flags |= DRIVER_FLAGS_CPU;
391 img_hash_xmit_cpu(hdev, addr + ctx->offset,
393 ctx->sent += wsend * 4;
396 ctx->sent += wsend * 4;
401 ctx->bufcnt = sg_pcopy_to_buffer(ctx->sgfirst, ctx->nents,
402 ctx->buffer, bleft, ctx->sent);
404 ctx->sg = sg_next(ctx->sg);
405 while (ctx->sg && (ctx->bufcnt < 4)) {
406 len = ctx->sg->length;
407 if (likely(len > (4 - ctx->bufcnt)))
408 len = 4 - ctx->bufcnt;
409 tbc = sg_pcopy_to_buffer(ctx->sgfirst, ctx->nents,
410 ctx->buffer + ctx->bufcnt, len,
411 ctx->sent + ctx->bufcnt);
413 if (tbc >= ctx->sg->length) {
414 ctx->sg = sg_next(ctx->sg);
419 ctx->sent += ctx->bufcnt;
423 img_hash_dma_callback(hdev);
426 ctx->sg = sg_next(ctx->sg);
430 static int img_hash_write_via_dma_stop(struct img_hash_dev *hdev)
432 struct img_hash_request_ctx *ctx = ahash_request_ctx(hdev->req);
434 if (ctx->flags & DRIVER_FLAGS_SG)
435 dma_unmap_sg(hdev->dev, ctx->sg, ctx->dma_ct, DMA_TO_DEVICE);
440 static int img_hash_process_data(struct img_hash_dev *hdev)
442 struct ahash_request *req = hdev->req;
443 struct img_hash_request_ctx *ctx = ahash_request_ctx(req);
448 if (req->nbytes >= IMG_HASH_DMA_THRESHOLD) {
449 dev_dbg(hdev->dev, "process data request(%d bytes) using DMA\n",
451 err = img_hash_write_via_dma(hdev);
453 dev_dbg(hdev->dev, "process data request(%d bytes) using CPU\n",
455 err = img_hash_write_via_cpu(hdev);
460 static int img_hash_hw_init(struct img_hash_dev *hdev)
462 unsigned long long nbits;
465 img_hash_write(hdev, CR_RESET, CR_RESET_SET);
466 img_hash_write(hdev, CR_RESET, CR_RESET_UNSET);
467 img_hash_write(hdev, CR_INTENAB, CR_INT_NEW_RESULTS_SET);
469 nbits = (u64)hdev->req->nbytes << 3;
472 img_hash_write(hdev, CR_MESSAGE_LENGTH_H, u);
473 img_hash_write(hdev, CR_MESSAGE_LENGTH_L, l);
475 if (!(DRIVER_FLAGS_INIT & hdev->flags)) {
476 hdev->flags |= DRIVER_FLAGS_INIT;
479 dev_dbg(hdev->dev, "hw initialized, nbits: %llx\n", nbits);
483 static int img_hash_init(struct ahash_request *req)
485 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
486 struct img_hash_request_ctx *rctx = ahash_request_ctx(req);
487 struct img_hash_ctx *ctx = crypto_ahash_ctx(tfm);
489 ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback);
490 rctx->fallback_req.base.flags = req->base.flags
491 & CRYPTO_TFM_REQ_MAY_SLEEP;
493 return crypto_ahash_init(&rctx->fallback_req);
496 static int img_hash_handle_queue(struct img_hash_dev *hdev,
497 struct ahash_request *req)
499 struct crypto_async_request *async_req, *backlog;
500 struct img_hash_request_ctx *ctx;
502 int err = 0, res = 0;
504 spin_lock_irqsave(&hdev->lock, flags);
507 res = ahash_enqueue_request(&hdev->queue, req);
509 if (DRIVER_FLAGS_BUSY & hdev->flags) {
510 spin_unlock_irqrestore(&hdev->lock, flags);
514 backlog = crypto_get_backlog(&hdev->queue);
515 async_req = crypto_dequeue_request(&hdev->queue);
517 hdev->flags |= DRIVER_FLAGS_BUSY;
519 spin_unlock_irqrestore(&hdev->lock, flags);
525 backlog->complete(backlog, -EINPROGRESS);
527 req = ahash_request_cast(async_req);
530 ctx = ahash_request_ctx(req);
532 dev_info(hdev->dev, "processing req, op: %lu, bytes: %d\n",
533 ctx->op, req->nbytes);
535 err = img_hash_hw_init(hdev);
538 err = img_hash_process_data(hdev);
540 if (err != -EINPROGRESS) {
541 /* done_task will not finish so do it here */
542 img_hash_finish_req(req, err);
547 static int img_hash_update(struct ahash_request *req)
549 struct img_hash_request_ctx *rctx = ahash_request_ctx(req);
550 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
551 struct img_hash_ctx *ctx = crypto_ahash_ctx(tfm);
553 ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback);
554 rctx->fallback_req.base.flags = req->base.flags
555 & CRYPTO_TFM_REQ_MAY_SLEEP;
556 rctx->fallback_req.nbytes = req->nbytes;
557 rctx->fallback_req.src = req->src;
559 return crypto_ahash_update(&rctx->fallback_req);
562 static int img_hash_final(struct ahash_request *req)
564 struct img_hash_request_ctx *rctx = ahash_request_ctx(req);
565 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
566 struct img_hash_ctx *ctx = crypto_ahash_ctx(tfm);
568 ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback);
569 rctx->fallback_req.base.flags = req->base.flags
570 & CRYPTO_TFM_REQ_MAY_SLEEP;
571 rctx->fallback_req.result = req->result;
573 return crypto_ahash_final(&rctx->fallback_req);
576 static int img_hash_finup(struct ahash_request *req)
578 struct img_hash_request_ctx *rctx = ahash_request_ctx(req);
579 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
580 struct img_hash_ctx *ctx = crypto_ahash_ctx(tfm);
582 ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback);
583 rctx->fallback_req.base.flags = req->base.flags
584 & CRYPTO_TFM_REQ_MAY_SLEEP;
585 rctx->fallback_req.nbytes = req->nbytes;
586 rctx->fallback_req.src = req->src;
587 rctx->fallback_req.result = req->result;
589 return crypto_ahash_finup(&rctx->fallback_req);
592 static int img_hash_digest(struct ahash_request *req)
594 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
595 struct img_hash_ctx *tctx = crypto_ahash_ctx(tfm);
596 struct img_hash_request_ctx *ctx = ahash_request_ctx(req);
597 struct img_hash_dev *hdev = NULL;
598 struct img_hash_dev *tmp;
601 spin_lock(&img_hash.lock);
603 list_for_each_entry(tmp, &img_hash.dev_list, list) {
613 spin_unlock(&img_hash.lock);
616 ctx->digsize = crypto_ahash_digestsize(tfm);
618 switch (ctx->digsize) {
619 case SHA1_DIGEST_SIZE:
620 ctx->flags |= DRIVER_FLAGS_SHA1;
622 case SHA256_DIGEST_SIZE:
623 ctx->flags |= DRIVER_FLAGS_SHA256;
625 case SHA224_DIGEST_SIZE:
626 ctx->flags |= DRIVER_FLAGS_SHA224;
628 case MD5_DIGEST_SIZE:
629 ctx->flags |= DRIVER_FLAGS_MD5;
638 ctx->total = req->nbytes;
640 ctx->sgfirst = req->src;
641 ctx->nents = sg_nents(ctx->sg);
643 err = img_hash_handle_queue(tctx->hdev, req);
648 static int img_hash_cra_init(struct crypto_tfm *tfm)
650 struct img_hash_ctx *ctx = crypto_tfm_ctx(tfm);
651 const char *alg_name = crypto_tfm_alg_name(tfm);
654 ctx->fallback = crypto_alloc_ahash(alg_name, 0,
655 CRYPTO_ALG_NEED_FALLBACK);
656 if (IS_ERR(ctx->fallback)) {
657 pr_err("img_hash: Could not load fallback driver.\n");
658 err = PTR_ERR(ctx->fallback);
661 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
662 sizeof(struct img_hash_request_ctx) +
663 IMG_HASH_DMA_THRESHOLD);
671 static void img_hash_cra_exit(struct crypto_tfm *tfm)
673 struct img_hash_ctx *tctx = crypto_tfm_ctx(tfm);
675 crypto_free_ahash(tctx->fallback);
678 static irqreturn_t img_irq_handler(int irq, void *dev_id)
680 struct img_hash_dev *hdev = dev_id;
683 reg = img_hash_read(hdev, CR_INTSTAT);
684 img_hash_write(hdev, CR_INTCLEAR, reg);
686 if (reg & CR_INT_NEW_RESULTS_SET) {
687 dev_dbg(hdev->dev, "IRQ CR_INT_NEW_RESULTS_SET\n");
688 if (DRIVER_FLAGS_BUSY & hdev->flags) {
689 hdev->flags |= DRIVER_FLAGS_OUTPUT_READY;
690 if (!(DRIVER_FLAGS_CPU & hdev->flags))
691 hdev->flags |= DRIVER_FLAGS_DMA_READY;
692 tasklet_schedule(&hdev->done_task);
695 "HASH interrupt when no active requests.\n");
697 } else if (reg & CR_INT_RESULTS_AVAILABLE) {
699 "IRQ triggered before the hash had completed\n");
700 } else if (reg & CR_INT_RESULT_READ_ERR) {
702 "Attempt to read from an empty result queue\n");
703 } else if (reg & CR_INT_MESSAGE_WRITE_ERROR) {
705 "Data written before the hardware was configured\n");
710 static struct ahash_alg img_algs[] = {
712 .init = img_hash_init,
713 .update = img_hash_update,
714 .final = img_hash_final,
715 .finup = img_hash_finup,
716 .digest = img_hash_digest,
718 .digestsize = MD5_DIGEST_SIZE,
721 .cra_driver_name = "img-md5",
725 CRYPTO_ALG_NEED_FALLBACK,
726 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
727 .cra_ctxsize = sizeof(struct img_hash_ctx),
728 .cra_init = img_hash_cra_init,
729 .cra_exit = img_hash_cra_exit,
730 .cra_module = THIS_MODULE,
735 .init = img_hash_init,
736 .update = img_hash_update,
737 .final = img_hash_final,
738 .finup = img_hash_finup,
739 .digest = img_hash_digest,
741 .digestsize = SHA1_DIGEST_SIZE,
744 .cra_driver_name = "img-sha1",
748 CRYPTO_ALG_NEED_FALLBACK,
749 .cra_blocksize = SHA1_BLOCK_SIZE,
750 .cra_ctxsize = sizeof(struct img_hash_ctx),
751 .cra_init = img_hash_cra_init,
752 .cra_exit = img_hash_cra_exit,
753 .cra_module = THIS_MODULE,
758 .init = img_hash_init,
759 .update = img_hash_update,
760 .final = img_hash_final,
761 .finup = img_hash_finup,
762 .digest = img_hash_digest,
764 .digestsize = SHA224_DIGEST_SIZE,
766 .cra_name = "sha224",
767 .cra_driver_name = "img-sha224",
771 CRYPTO_ALG_NEED_FALLBACK,
772 .cra_blocksize = SHA224_BLOCK_SIZE,
773 .cra_ctxsize = sizeof(struct img_hash_ctx),
774 .cra_init = img_hash_cra_init,
775 .cra_exit = img_hash_cra_exit,
776 .cra_module = THIS_MODULE,
781 .init = img_hash_init,
782 .update = img_hash_update,
783 .final = img_hash_final,
784 .finup = img_hash_finup,
785 .digest = img_hash_digest,
787 .digestsize = SHA256_DIGEST_SIZE,
789 .cra_name = "sha256",
790 .cra_driver_name = "img-sha256",
794 CRYPTO_ALG_NEED_FALLBACK,
795 .cra_blocksize = SHA256_BLOCK_SIZE,
796 .cra_ctxsize = sizeof(struct img_hash_ctx),
797 .cra_init = img_hash_cra_init,
798 .cra_exit = img_hash_cra_exit,
799 .cra_module = THIS_MODULE,
805 static int img_register_algs(struct img_hash_dev *hdev)
809 for (i = 0; i < ARRAY_SIZE(img_algs); i++) {
810 err = crypto_register_ahash(&img_algs[i]);
818 crypto_unregister_ahash(&img_algs[i]);
823 static int img_unregister_algs(struct img_hash_dev *hdev)
827 for (i = 0; i < ARRAY_SIZE(img_algs); i++)
828 crypto_unregister_ahash(&img_algs[i]);
832 static void img_hash_done_task(unsigned long data)
834 struct img_hash_dev *hdev = (struct img_hash_dev *)data;
837 if (hdev->err == -EINVAL) {
842 if (!(DRIVER_FLAGS_BUSY & hdev->flags)) {
843 img_hash_handle_queue(hdev, NULL);
847 if (DRIVER_FLAGS_CPU & hdev->flags) {
848 if (DRIVER_FLAGS_OUTPUT_READY & hdev->flags) {
849 hdev->flags &= ~DRIVER_FLAGS_OUTPUT_READY;
852 } else if (DRIVER_FLAGS_DMA_READY & hdev->flags) {
853 if (DRIVER_FLAGS_DMA_ACTIVE & hdev->flags) {
854 hdev->flags &= ~DRIVER_FLAGS_DMA_ACTIVE;
855 img_hash_write_via_dma_stop(hdev);
861 if (DRIVER_FLAGS_OUTPUT_READY & hdev->flags) {
862 hdev->flags &= ~(DRIVER_FLAGS_DMA_READY |
863 DRIVER_FLAGS_OUTPUT_READY);
870 img_hash_finish_req(hdev->req, err);
873 static const struct of_device_id img_hash_match[] = {
874 { .compatible = "img,hash-accelerator" },
877 MODULE_DEVICE_TABLE(of, img_hash_match);
879 static int img_hash_probe(struct platform_device *pdev)
881 struct img_hash_dev *hdev;
882 struct device *dev = &pdev->dev;
883 struct resource *hash_res;
887 hdev = devm_kzalloc(dev, sizeof(*hdev), GFP_KERNEL);
891 spin_lock_init(&hdev->lock);
895 platform_set_drvdata(pdev, hdev);
897 INIT_LIST_HEAD(&hdev->list);
899 tasklet_init(&hdev->done_task, img_hash_done_task, (unsigned long)hdev);
900 tasklet_init(&hdev->dma_task, img_hash_dma_task, (unsigned long)hdev);
902 crypto_init_queue(&hdev->queue, IMG_HASH_QUEUE_LENGTH);
905 hash_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
907 hdev->io_base = devm_ioremap_resource(dev, hash_res);
908 if (IS_ERR(hdev->io_base)) {
909 err = PTR_ERR(hdev->io_base);
910 dev_err(dev, "can't ioremap, returned %d\n", err);
915 /* Write port (DMA or CPU) */
916 hash_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
917 hdev->cpu_addr = devm_ioremap_resource(dev, hash_res);
918 if (IS_ERR(hdev->cpu_addr)) {
919 dev_err(dev, "can't ioremap write port\n");
920 err = PTR_ERR(hdev->cpu_addr);
923 hdev->bus_addr = hash_res->start;
925 irq = platform_get_irq(pdev, 0);
927 dev_err(dev, "no IRQ resource info\n");
932 err = devm_request_irq(dev, irq, img_irq_handler, 0,
933 dev_name(dev), hdev);
935 dev_err(dev, "unable to request irq\n");
938 dev_dbg(dev, "using IRQ channel %d\n", irq);
940 hdev->hash_clk = devm_clk_get(&pdev->dev, "hash");
941 if (IS_ERR(hdev->hash_clk)) {
942 dev_err(dev, "clock initialization failed.\n");
943 err = PTR_ERR(hdev->hash_clk);
947 hdev->sys_clk = devm_clk_get(&pdev->dev, "sys");
948 if (IS_ERR(hdev->sys_clk)) {
949 dev_err(dev, "clock initialization failed.\n");
950 err = PTR_ERR(hdev->sys_clk);
954 err = clk_prepare_enable(hdev->hash_clk);
958 err = clk_prepare_enable(hdev->sys_clk);
962 err = img_hash_dma_init(hdev);
966 dev_dbg(dev, "using %s for DMA transfers\n",
967 dma_chan_name(hdev->dma_lch));
969 spin_lock(&img_hash.lock);
970 list_add_tail(&hdev->list, &img_hash.dev_list);
971 spin_unlock(&img_hash.lock);
973 err = img_register_algs(hdev);
976 dev_dbg(dev, "Img MD5/SHA1/SHA224/SHA256 Hardware accelerator initialized\n");
981 spin_lock(&img_hash.lock);
982 list_del(&hdev->list);
983 spin_unlock(&img_hash.lock);
984 dma_release_channel(hdev->dma_lch);
986 clk_disable_unprepare(hdev->sys_clk);
988 clk_disable_unprepare(hdev->hash_clk);
990 tasklet_kill(&hdev->done_task);
991 tasklet_kill(&hdev->dma_task);
996 static int img_hash_remove(struct platform_device *pdev)
998 static struct img_hash_dev *hdev;
1000 hdev = platform_get_drvdata(pdev);
1001 spin_lock(&img_hash.lock);
1002 list_del(&hdev->list);
1003 spin_unlock(&img_hash.lock);
1005 img_unregister_algs(hdev);
1007 tasklet_kill(&hdev->done_task);
1008 tasklet_kill(&hdev->dma_task);
1010 dma_release_channel(hdev->dma_lch);
1012 clk_disable_unprepare(hdev->hash_clk);
1013 clk_disable_unprepare(hdev->sys_clk);
1018 static struct platform_driver img_hash_driver = {
1019 .probe = img_hash_probe,
1020 .remove = img_hash_remove,
1022 .name = "img-hash-accelerator",
1023 .of_match_table = of_match_ptr(img_hash_match),
1026 module_platform_driver(img_hash_driver);
1028 MODULE_LICENSE("GPL v2");
1029 MODULE_DESCRIPTION("Imgtec SHA1/224/256 & MD5 hw accelerator driver");
1030 MODULE_AUTHOR("Will Thomas.");
1031 MODULE_AUTHOR("James Hartley <james.hartley@imgtec.com>");