12 #include <linux/device.h>
13 #include <linux/module.h>
15 #include <linux/errno.h>
17 #include <linux/kernel.h>
37 #define CRC_CCRYPTO_QUEUE_LENGTH 5
39 #define DRIVER_NAME "bfin-hmac-crc"
40 #define CHKSUM_DIGEST_SIZE 4
41 #define CHKSUM_BLOCK_SIZE 1
43 #define CRC_MAX_DMA_DESC 100
45 #define CRC_CRYPTO_STATE_UPDATE 1
46 #define CRC_CRYPTO_STATE_FINALUPDATE 2
47 #define CRC_CRYPTO_STATE_FINISH 3
57 volatile struct crc_register *
regs;
70 static struct bfin_crypto_crc_list {
111 sg = scatterwalk_sg_next(sg);
135 crc->regs->datacntrld = 0;
136 crc->regs->control = MODE_CALC_CRC << OPMODE_OFFSET;
137 crc->regs->curresult =
key;
140 crc->regs->status = CMPERRI | DCNTEXPI;
141 crc->regs->intrenset = CMPERRI | DCNTEXPI;
155 spin_lock_bh(&crc_list.lock);
160 spin_unlock_bh(&crc_list.lock);
163 dev_dbg(crc->
dev,
"init: requested sg list is too big > %d\n",
179 crypto_ahash_digestsize(tfm));
181 return bfin_crypto_crc_init_hw(crc, crc_ctx->
key);
189 unsigned long dma_config;
192 unsigned int mid_dma_count = 0;
218 mid_dma_count = dma_count % 4;
221 if (dma_addr % 4 == 0) {
225 }
else if (dma_addr % 2 == 0) {
235 crc->
sg_cpu[
i].cfg = dma_config;
236 crc->
sg_cpu[
i].x_count = dma_count;
237 crc->
sg_cpu[
i].x_modify = dma_mod;
238 dev_dbg(crc->
dev,
"%d: crc_dma: start_addr:0x%lx, "
239 "cfg:0x%lx, x_count:0x%lx, x_modify:0x%lx\n",
240 i, crc->
sg_cpu[i].start_addr,
250 (
void *)(dma_addr + (dma_count << 2)),
256 crc->
sg_cpu[
i].cfg = dma_config;
259 dev_dbg(crc->
dev,
"%d: crc_dma: start_addr:0x%lx, "
260 "cfg:0x%lx, x_count:0x%lx, x_modify:0x%lx\n",
261 i, crc->
sg_cpu[i].start_addr,
274 crc->
sg_cpu[
i].cfg = dma_config;
277 dev_dbg(crc->
dev,
"%d: crc_dma: start_addr:0x%lx, "
278 "cfg:0x%lx, x_count:0x%lx, x_modify:0x%lx\n",
279 i, crc->
sg_cpu[i].start_addr,
289 (
unsigned int)crc->
sg_cpu +
295 set_dma_curr_desc_addr(crc->
dma_ch, (
unsigned long *)crc->
sg_dma);
296 set_dma_x_count(crc->
dma_ch, 0);
297 set_dma_x_modify(crc->
dma_ch, 0);
299 set_dma_config(crc->
dma_ch, dma_config);
310 unsigned int nextlen;
315 ret = ahash_enqueue_request(&crc->
queue, req);
317 spin_unlock_irqrestore(&crc->
lock, flags);
320 backlog = crypto_get_backlog(&crc->
queue);
324 spin_unlock_irqrestore(&crc->
lock, flags);
332 req = ahash_request_cast(async_req);
334 ctx = ahash_request_ctx(req);
339 dev_dbg(crc->
dev,
"handling new req, flag=%u, nbytes: %d\n",
375 scatterwalk_sg_chain(ctx->
bufsl, nsg,
391 for (i = nsg - 1; i >= 0; i--) {
392 sg = sg_get(ctx->
sg, nsg, i);
414 bfin_crypto_crc_config_dma(crc);
417 crc->
regs->control |= BLKEN;
434 return bfin_crypto_crc_handle_queue(ctx->
crc, req);
447 return bfin_crypto_crc_handle_queue(ctx->
crc, req);
461 return bfin_crypto_crc_handle_queue(ctx->
crc, req);
468 ret = bfin_crypto_crc_init(req);
472 return bfin_crypto_crc_finup(req);
491 static int bfin_crypto_crc_cra_init(
struct crypto_tfm *tfm)
496 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
502 static void bfin_crypto_crc_cra_exit(
struct crypto_tfm *tfm)
507 .init = bfin_crypto_crc_init,
508 .update = bfin_crypto_crc_update,
509 .final = bfin_crypto_crc_final,
510 .finup = bfin_crypto_crc_finup,
511 .digest = bfin_crypto_crc_digest,
512 .setkey = bfin_crypto_crc_setkey,
515 .cra_name =
"hmac(crc32)",
524 .cra_init = bfin_crypto_crc_cra_init,
525 .cra_exit = bfin_crypto_crc_cra_exit,
529 static void bfin_crypto_crc_done_task(
unsigned long data)
533 bfin_crypto_crc_handle_queue(crc,
NULL);
540 if (crc->
regs->status & DCNTEXP) {
541 crc->
regs->status = DCNTEXP;
547 crc->
regs->control &= ~BLKEN;
550 if (crc->
req->base.complete)
551 crc->
req->base.complete(&crc->
req->base, 0);
571 while ((crc->
regs->control & BLKEN) && --i)
580 # define bfin_crypto_crc_suspend NULL
583 #define bfin_crypto_crc_resume NULL
599 dev_err(&pdev->
dev,
"fail to malloc bfin_crypto_crc\n");
605 INIT_LIST_HEAD(&crc->
list);
612 dev_err(&pdev->
dev,
"Cannot get IORESOURCE_MEM\n");
614 goto out_error_free_mem;
621 goto out_error_free_mem;
626 dev_err(&pdev->
dev,
"No CRC DCNTEXP IRQ specified\n");
628 goto out_error_unmap;
633 dev_err(&pdev->
dev,
"Unable to request blackfin crc irq\n");
634 goto out_error_unmap;
639 dev_err(&pdev->
dev,
"No CRC DMA channel specified\n");
643 crc->dma_ch = res->
start;
647 dev_err(&pdev->
dev,
"Unable to attach Blackfin CRC DMA channel\n");
652 if (crc->sg_cpu ==
NULL) {
662 crc->regs->control = 0;
664 crc->regs->poly = crc->poly = (
u32)pdev->
dev.platform_data;
667 while (!(crc->regs->status & LUTDONE) && (--timeout) > 0)
673 spin_lock(&crc_list.lock);
674 list_add(&crc->list, &crc_list.dev_list);
675 spin_unlock(&crc_list.lock);
677 platform_set_drvdata(pdev, crc);
681 spin_lock(&crc_list.lock);
683 spin_unlock(&crc_list.lock);
684 dev_err(&pdev->
dev,
"Cann't register crypto ahash device\n");
717 spin_lock(&crc_list.lock);
719 spin_unlock(&crc_list.lock);
733 .probe = bfin_crypto_crc_probe,
749 static int __init bfin_crypto_crc_mod_init(
void)
753 pr_info(
"Blackfin hardware CRC crypto driver\n");
755 INIT_LIST_HEAD(&crc_list.dev_list);
770 static void __exit bfin_crypto_crc_mod_exit(
void)