bfin_crc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Support Blackfin CRC HW acceleration.
  5. *
  6. * Copyright 2012 Analog Devices Inc.
  7. *
  8. * Licensed under the GPL-2.
  9. */
  10. #include <linux/err.h>
  11. #include <linux/device.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/errno.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/irq.h>
  18. #include <linux/io.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/delay.h>
  23. #include <linux/crypto.h>
  24. #include <linux/cryptohash.h>
  25. #include <crypto/scatterwalk.h>
  26. #include <crypto/algapi.h>
  27. #include <crypto/hash.h>
  28. #include <crypto/internal/hash.h>
  29. #include <asm/unaligned.h>
  30. #include <asm/dma.h>
  31. #include <asm/portmux.h>
  32. #include <asm/io.h>
  33. #include "bfin_crc.h"
  34. #define CRC_CCRYPTO_QUEUE_LENGTH 5
  35. #define DRIVER_NAME "bfin-hmac-crc"
  36. #define CHKSUM_DIGEST_SIZE 4
  37. #define CHKSUM_BLOCK_SIZE 1
  38. #define CRC_MAX_DMA_DESC 100
  39. #define CRC_CRYPTO_STATE_UPDATE 1
  40. #define CRC_CRYPTO_STATE_FINALUPDATE 2
  41. #define CRC_CRYPTO_STATE_FINISH 3
  42. struct bfin_crypto_crc {
  43. struct list_head list;
  44. struct device *dev;
  45. spinlock_t lock;
  46. int irq;
  47. int dma_ch;
  48. u32 poly;
  49. struct crc_register *regs;
  50. struct ahash_request *req; /* current request in operation */
  51. struct dma_desc_array *sg_cpu; /* virt addr of sg dma descriptors */
  52. dma_addr_t sg_dma; /* phy addr of sg dma descriptors */
  53. u8 *sg_mid_buf;
  54. dma_addr_t sg_mid_dma; /* phy addr of sg mid buffer */
  55. struct tasklet_struct done_task;
  56. struct crypto_queue queue; /* waiting requests */
  57. u8 busy:1; /* crc device in operation flag */
  58. };
  59. static struct bfin_crypto_crc_list {
  60. struct list_head dev_list;
  61. spinlock_t lock;
  62. } crc_list;
  63. struct bfin_crypto_crc_reqctx {
  64. struct bfin_crypto_crc *crc;
  65. unsigned int total; /* total request bytes */
  66. size_t sg_buflen; /* bytes for this update */
  67. unsigned int sg_nents;
  68. struct scatterlist *sg; /* sg list head for this update*/
  69. struct scatterlist bufsl[2]; /* chained sg list */
  70. size_t bufnext_len;
  71. size_t buflast_len;
  72. u8 bufnext[CHKSUM_DIGEST_SIZE]; /* extra bytes for next udpate */
  73. u8 buflast[CHKSUM_DIGEST_SIZE]; /* extra bytes from last udpate */
  74. u8 flag;
  75. };
  76. struct bfin_crypto_crc_ctx {
  77. struct bfin_crypto_crc *crc;
  78. u32 key;
  79. };
  80. /*
  81. * get element in scatter list by given index
  82. */
  83. static struct scatterlist *sg_get(struct scatterlist *sg_list, unsigned int nents,
  84. unsigned int index)
  85. {
  86. struct scatterlist *sg = NULL;
  87. int i;
  88. for_each_sg(sg_list, sg, nents, i)
  89. if (i == index)
  90. break;
  91. return sg;
  92. }
  93. static int bfin_crypto_crc_init_hw(struct bfin_crypto_crc *crc, u32 key)
  94. {
  95. writel(0, &crc->regs->datacntrld);
  96. writel(MODE_CALC_CRC << OPMODE_OFFSET, &crc->regs->control);
  97. writel(key, &crc->regs->curresult);
  98. /* setup CRC interrupts */
  99. writel(CMPERRI | DCNTEXPI, &crc->regs->status);
  100. writel(CMPERRI | DCNTEXPI, &crc->regs->intrenset);
  101. return 0;
  102. }
  103. static int bfin_crypto_crc_init(struct ahash_request *req)
  104. {
  105. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  106. struct bfin_crypto_crc_ctx *crc_ctx = crypto_ahash_ctx(tfm);
  107. struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(req);
  108. struct bfin_crypto_crc *crc;
  109. dev_dbg(ctx->crc->dev, "crc_init\n");
  110. spin_lock_bh(&crc_list.lock);
  111. list_for_each_entry(crc, &crc_list.dev_list, list) {
  112. crc_ctx->crc = crc;
  113. break;
  114. }
  115. spin_unlock_bh(&crc_list.lock);
  116. if (sg_nents(req->src) > CRC_MAX_DMA_DESC) {
  117. dev_dbg(ctx->crc->dev, "init: requested sg list is too big > %d\n",
  118. CRC_MAX_DMA_DESC);
  119. return -EINVAL;
  120. }
  121. ctx->crc = crc;
  122. ctx->bufnext_len = 0;
  123. ctx->buflast_len = 0;
  124. ctx->sg_buflen = 0;
  125. ctx->total = 0;
  126. ctx->flag = 0;
  127. /* init crc results */
  128. put_unaligned_le32(crc_ctx->key, req->result);
  129. dev_dbg(ctx->crc->dev, "init: digest size: %d\n",
  130. crypto_ahash_digestsize(tfm));
  131. return bfin_crypto_crc_init_hw(crc, crc_ctx->key);
  132. }
  133. static void bfin_crypto_crc_config_dma(struct bfin_crypto_crc *crc)
  134. {
  135. struct scatterlist *sg;
  136. struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(crc->req);
  137. int i = 0, j = 0;
  138. unsigned long dma_config;
  139. unsigned int dma_count;
  140. unsigned int dma_addr;
  141. unsigned int mid_dma_count = 0;
  142. int dma_mod;
  143. dma_map_sg(crc->dev, ctx->sg, ctx->sg_nents, DMA_TO_DEVICE);
  144. for_each_sg(ctx->sg, sg, ctx->sg_nents, j) {
  145. dma_addr = sg_dma_address(sg);
  146. /* deduce extra bytes in last sg */
  147. if (sg_is_last(sg))
  148. dma_count = sg_dma_len(sg) - ctx->bufnext_len;
  149. else
  150. dma_count = sg_dma_len(sg);
  151. if (mid_dma_count) {
  152. /* Append last middle dma buffer to 4 bytes with first
  153. bytes in current sg buffer. Move addr of current
  154. sg and deduce the length of current sg.
  155. */
  156. memcpy(crc->sg_mid_buf +(i << 2) + mid_dma_count,
  157. sg_virt(sg),
  158. CHKSUM_DIGEST_SIZE - mid_dma_count);
  159. dma_addr += CHKSUM_DIGEST_SIZE - mid_dma_count;
  160. dma_count -= CHKSUM_DIGEST_SIZE - mid_dma_count;
  161. dma_config = DMAFLOW_ARRAY | RESTART | NDSIZE_3 |
  162. DMAEN | PSIZE_32 | WDSIZE_32;
  163. /* setup new dma descriptor for next middle dma */
  164. crc->sg_cpu[i].start_addr = crc->sg_mid_dma + (i << 2);
  165. crc->sg_cpu[i].cfg = dma_config;
  166. crc->sg_cpu[i].x_count = 1;
  167. crc->sg_cpu[i].x_modify = CHKSUM_DIGEST_SIZE;
  168. dev_dbg(crc->dev, "%d: crc_dma: start_addr:0x%lx, "
  169. "cfg:0x%lx, x_count:0x%lx, x_modify:0x%lx\n",
  170. i, crc->sg_cpu[i].start_addr,
  171. crc->sg_cpu[i].cfg, crc->sg_cpu[i].x_count,
  172. crc->sg_cpu[i].x_modify);
  173. i++;
  174. }
  175. dma_config = DMAFLOW_ARRAY | RESTART | NDSIZE_3 | DMAEN | PSIZE_32;
  176. /* chop current sg dma len to multiple of 32 bits */
  177. mid_dma_count = dma_count % 4;
  178. dma_count &= ~0x3;
  179. if (dma_addr % 4 == 0) {
  180. dma_config |= WDSIZE_32;
  181. dma_count >>= 2;
  182. dma_mod = 4;
  183. } else if (dma_addr % 2 == 0) {
  184. dma_config |= WDSIZE_16;
  185. dma_count >>= 1;
  186. dma_mod = 2;
  187. } else {
  188. dma_config |= WDSIZE_8;
  189. dma_mod = 1;
  190. }
  191. crc->sg_cpu[i].start_addr = dma_addr;
  192. crc->sg_cpu[i].cfg = dma_config;
  193. crc->sg_cpu[i].x_count = dma_count;
  194. crc->sg_cpu[i].x_modify = dma_mod;
  195. dev_dbg(crc->dev, "%d: crc_dma: start_addr:0x%lx, "
  196. "cfg:0x%lx, x_count:0x%lx, x_modify:0x%lx\n",
  197. i, crc->sg_cpu[i].start_addr,
  198. crc->sg_cpu[i].cfg, crc->sg_cpu[i].x_count,
  199. crc->sg_cpu[i].x_modify);
  200. i++;
  201. if (mid_dma_count) {
  202. /* copy extra bytes to next middle dma buffer */
  203. memcpy(crc->sg_mid_buf + (i << 2),
  204. (u8*)sg_virt(sg) + (dma_count << 2),
  205. mid_dma_count);
  206. }
  207. }
  208. dma_config = DMAFLOW_ARRAY | RESTART | NDSIZE_3 | DMAEN | PSIZE_32 | WDSIZE_32;
  209. /* For final update req, append the buffer for next update as well*/
  210. if (ctx->bufnext_len && (ctx->flag == CRC_CRYPTO_STATE_FINALUPDATE ||
  211. ctx->flag == CRC_CRYPTO_STATE_FINISH)) {
  212. crc->sg_cpu[i].start_addr = dma_map_single(crc->dev, ctx->bufnext,
  213. CHKSUM_DIGEST_SIZE, DMA_TO_DEVICE);
  214. crc->sg_cpu[i].cfg = dma_config;
  215. crc->sg_cpu[i].x_count = 1;
  216. crc->sg_cpu[i].x_modify = CHKSUM_DIGEST_SIZE;
  217. dev_dbg(crc->dev, "%d: crc_dma: start_addr:0x%lx, "
  218. "cfg:0x%lx, x_count:0x%lx, x_modify:0x%lx\n",
  219. i, crc->sg_cpu[i].start_addr,
  220. crc->sg_cpu[i].cfg, crc->sg_cpu[i].x_count,
  221. crc->sg_cpu[i].x_modify);
  222. i++;
  223. }
  224. if (i == 0)
  225. return;
  226. /* Set the last descriptor to stop mode */
  227. crc->sg_cpu[i - 1].cfg &= ~(DMAFLOW | NDSIZE);
  228. crc->sg_cpu[i - 1].cfg |= DI_EN;
  229. set_dma_curr_desc_addr(crc->dma_ch, (unsigned long *)crc->sg_dma);
  230. set_dma_x_count(crc->dma_ch, 0);
  231. set_dma_x_modify(crc->dma_ch, 0);
  232. set_dma_config(crc->dma_ch, dma_config);
  233. }
  234. static int bfin_crypto_crc_handle_queue(struct bfin_crypto_crc *crc,
  235. struct ahash_request *req)
  236. {
  237. struct crypto_async_request *async_req, *backlog;
  238. struct bfin_crypto_crc_reqctx *ctx;
  239. struct scatterlist *sg;
  240. int ret = 0;
  241. int nsg, i, j;
  242. unsigned int nextlen;
  243. unsigned long flags;
  244. u32 reg;
  245. spin_lock_irqsave(&crc->lock, flags);
  246. if (req)
  247. ret = ahash_enqueue_request(&crc->queue, req);
  248. if (crc->busy) {
  249. spin_unlock_irqrestore(&crc->lock, flags);
  250. return ret;
  251. }
  252. backlog = crypto_get_backlog(&crc->queue);
  253. async_req = crypto_dequeue_request(&crc->queue);
  254. if (async_req)
  255. crc->busy = 1;
  256. spin_unlock_irqrestore(&crc->lock, flags);
  257. if (!async_req)
  258. return ret;
  259. if (backlog)
  260. backlog->complete(backlog, -EINPROGRESS);
  261. req = ahash_request_cast(async_req);
  262. crc->req = req;
  263. ctx = ahash_request_ctx(req);
  264. ctx->sg = NULL;
  265. ctx->sg_buflen = 0;
  266. ctx->sg_nents = 0;
  267. dev_dbg(crc->dev, "handling new req, flag=%u, nbytes: %d\n",
  268. ctx->flag, req->nbytes);
  269. if (ctx->flag == CRC_CRYPTO_STATE_FINISH) {
  270. if (ctx->bufnext_len == 0) {
  271. crc->busy = 0;
  272. return 0;
  273. }
  274. /* Pack last crc update buffer to 32bit */
  275. memset(ctx->bufnext + ctx->bufnext_len, 0,
  276. CHKSUM_DIGEST_SIZE - ctx->bufnext_len);
  277. } else {
  278. /* Pack small data which is less than 32bit to buffer for next update. */
  279. if (ctx->bufnext_len + req->nbytes < CHKSUM_DIGEST_SIZE) {
  280. memcpy(ctx->bufnext + ctx->bufnext_len,
  281. sg_virt(req->src), req->nbytes);
  282. ctx->bufnext_len += req->nbytes;
  283. if (ctx->flag == CRC_CRYPTO_STATE_FINALUPDATE &&
  284. ctx->bufnext_len) {
  285. goto finish_update;
  286. } else {
  287. crc->busy = 0;
  288. return 0;
  289. }
  290. }
  291. if (ctx->bufnext_len) {
  292. /* Chain in extra bytes of last update */
  293. ctx->buflast_len = ctx->bufnext_len;
  294. memcpy(ctx->buflast, ctx->bufnext, ctx->buflast_len);
  295. nsg = ctx->sg_buflen ? 2 : 1;
  296. sg_init_table(ctx->bufsl, nsg);
  297. sg_set_buf(ctx->bufsl, ctx->buflast, ctx->buflast_len);
  298. if (nsg > 1)
  299. sg_chain(ctx->bufsl, nsg, req->src);
  300. ctx->sg = ctx->bufsl;
  301. } else
  302. ctx->sg = req->src;
  303. /* Chop crc buffer size to multiple of 32 bit */
  304. nsg = sg_nents(ctx->sg);
  305. ctx->sg_nents = nsg;
  306. ctx->sg_buflen = ctx->buflast_len + req->nbytes;
  307. ctx->bufnext_len = ctx->sg_buflen % 4;
  308. ctx->sg_buflen &= ~0x3;
  309. if (ctx->bufnext_len) {
  310. /* copy extra bytes to buffer for next update */
  311. memset(ctx->bufnext, 0, CHKSUM_DIGEST_SIZE);
  312. nextlen = ctx->bufnext_len;
  313. for (i = nsg - 1; i >= 0; i--) {
  314. sg = sg_get(ctx->sg, nsg, i);
  315. j = min(nextlen, sg_dma_len(sg));
  316. memcpy(ctx->bufnext + nextlen - j,
  317. sg_virt(sg) + sg_dma_len(sg) - j, j);
  318. if (j == sg_dma_len(sg))
  319. ctx->sg_nents--;
  320. nextlen -= j;
  321. if (nextlen == 0)
  322. break;
  323. }
  324. }
  325. }
  326. finish_update:
  327. if (ctx->bufnext_len && (ctx->flag == CRC_CRYPTO_STATE_FINALUPDATE ||
  328. ctx->flag == CRC_CRYPTO_STATE_FINISH))
  329. ctx->sg_buflen += CHKSUM_DIGEST_SIZE;
  330. /* set CRC data count before start DMA */
  331. writel(ctx->sg_buflen >> 2, &crc->regs->datacnt);
  332. /* setup and enable CRC DMA */
  333. bfin_crypto_crc_config_dma(crc);
  334. /* finally kick off CRC operation */
  335. reg = readl(&crc->regs->control);
  336. writel(reg | BLKEN, &crc->regs->control);
  337. return -EINPROGRESS;
  338. }
  339. static int bfin_crypto_crc_update(struct ahash_request *req)
  340. {
  341. struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(req);
  342. if (!req->nbytes)
  343. return 0;
  344. dev_dbg(ctx->crc->dev, "crc_update\n");
  345. ctx->total += req->nbytes;
  346. ctx->flag = CRC_CRYPTO_STATE_UPDATE;
  347. return bfin_crypto_crc_handle_queue(ctx->crc, req);
  348. }
  349. static int bfin_crypto_crc_final(struct ahash_request *req)
  350. {
  351. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  352. struct bfin_crypto_crc_ctx *crc_ctx = crypto_ahash_ctx(tfm);
  353. struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(req);
  354. dev_dbg(ctx->crc->dev, "crc_final\n");
  355. ctx->flag = CRC_CRYPTO_STATE_FINISH;
  356. crc_ctx->key = 0;
  357. return bfin_crypto_crc_handle_queue(ctx->crc, req);
  358. }
  359. static int bfin_crypto_crc_finup(struct ahash_request *req)
  360. {
  361. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  362. struct bfin_crypto_crc_ctx *crc_ctx = crypto_ahash_ctx(tfm);
  363. struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(req);
  364. dev_dbg(ctx->crc->dev, "crc_finishupdate\n");
  365. ctx->total += req->nbytes;
  366. ctx->flag = CRC_CRYPTO_STATE_FINALUPDATE;
  367. crc_ctx->key = 0;
  368. return bfin_crypto_crc_handle_queue(ctx->crc, req);
  369. }
  370. static int bfin_crypto_crc_digest(struct ahash_request *req)
  371. {
  372. int ret;
  373. ret = bfin_crypto_crc_init(req);
  374. if (ret)
  375. return ret;
  376. return bfin_crypto_crc_finup(req);
  377. }
  378. static int bfin_crypto_crc_setkey(struct crypto_ahash *tfm, const u8 *key,
  379. unsigned int keylen)
  380. {
  381. struct bfin_crypto_crc_ctx *crc_ctx = crypto_ahash_ctx(tfm);
  382. dev_dbg(crc_ctx->crc->dev, "crc_setkey\n");
  383. if (keylen != CHKSUM_DIGEST_SIZE) {
  384. crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  385. return -EINVAL;
  386. }
  387. crc_ctx->key = get_unaligned_le32(key);
  388. return 0;
  389. }
  390. static int bfin_crypto_crc_cra_init(struct crypto_tfm *tfm)
  391. {
  392. struct bfin_crypto_crc_ctx *crc_ctx = crypto_tfm_ctx(tfm);
  393. crc_ctx->key = 0;
  394. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  395. sizeof(struct bfin_crypto_crc_reqctx));
  396. return 0;
  397. }
  398. static void bfin_crypto_crc_cra_exit(struct crypto_tfm *tfm)
  399. {
  400. }
  401. static struct ahash_alg algs = {
  402. .init = bfin_crypto_crc_init,
  403. .update = bfin_crypto_crc_update,
  404. .final = bfin_crypto_crc_final,
  405. .finup = bfin_crypto_crc_finup,
  406. .digest = bfin_crypto_crc_digest,
  407. .setkey = bfin_crypto_crc_setkey,
  408. .halg.digestsize = CHKSUM_DIGEST_SIZE,
  409. .halg.base = {
  410. .cra_name = "hmac(crc32)",
  411. .cra_driver_name = DRIVER_NAME,
  412. .cra_priority = 100,
  413. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  414. CRYPTO_ALG_ASYNC,
  415. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  416. .cra_ctxsize = sizeof(struct bfin_crypto_crc_ctx),
  417. .cra_alignmask = 3,
  418. .cra_module = THIS_MODULE,
  419. .cra_init = bfin_crypto_crc_cra_init,
  420. .cra_exit = bfin_crypto_crc_cra_exit,
  421. }
  422. };
  423. static void bfin_crypto_crc_done_task(unsigned long data)
  424. {
  425. struct bfin_crypto_crc *crc = (struct bfin_crypto_crc *)data;
  426. bfin_crypto_crc_handle_queue(crc, NULL);
  427. }
  428. static irqreturn_t bfin_crypto_crc_handler(int irq, void *dev_id)
  429. {
  430. struct bfin_crypto_crc *crc = dev_id;
  431. u32 reg;
  432. if (readl(&crc->regs->status) & DCNTEXP) {
  433. writel(DCNTEXP, &crc->regs->status);
  434. /* prepare results */
  435. put_unaligned_le32(readl(&crc->regs->result),
  436. crc->req->result);
  437. reg = readl(&crc->regs->control);
  438. writel(reg & ~BLKEN, &crc->regs->control);
  439. crc->busy = 0;
  440. if (crc->req->base.complete)
  441. crc->req->base.complete(&crc->req->base, 0);
  442. tasklet_schedule(&crc->done_task);
  443. return IRQ_HANDLED;
  444. } else
  445. return IRQ_NONE;
  446. }
  447. #ifdef CONFIG_PM
  448. /**
  449. * bfin_crypto_crc_suspend - suspend crc device
  450. * @pdev: device being suspended
  451. * @state: requested suspend state
  452. */
  453. static int bfin_crypto_crc_suspend(struct platform_device *pdev, pm_message_t state)
  454. {
  455. struct bfin_crypto_crc *crc = platform_get_drvdata(pdev);
  456. int i = 100000;
  457. while ((readl(&crc->regs->control) & BLKEN) && --i)
  458. cpu_relax();
  459. if (i == 0)
  460. return -EBUSY;
  461. return 0;
  462. }
  463. #else
  464. # define bfin_crypto_crc_suspend NULL
  465. #endif
  466. #define bfin_crypto_crc_resume NULL
  467. /**
  468. * bfin_crypto_crc_probe - Initialize module
  469. *
  470. */
  471. static int bfin_crypto_crc_probe(struct platform_device *pdev)
  472. {
  473. struct device *dev = &pdev->dev;
  474. struct resource *res;
  475. struct bfin_crypto_crc *crc;
  476. unsigned int timeout = 100000;
  477. int ret;
  478. crc = devm_kzalloc(dev, sizeof(*crc), GFP_KERNEL);
  479. if (!crc) {
  480. dev_err(&pdev->dev, "fail to malloc bfin_crypto_crc\n");
  481. return -ENOMEM;
  482. }
  483. crc->dev = dev;
  484. INIT_LIST_HEAD(&crc->list);
  485. spin_lock_init(&crc->lock);
  486. tasklet_init(&crc->done_task, bfin_crypto_crc_done_task, (unsigned long)crc);
  487. crypto_init_queue(&crc->queue, CRC_CCRYPTO_QUEUE_LENGTH);
  488. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  489. if (res == NULL) {
  490. dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
  491. return -ENOENT;
  492. }
  493. crc->regs = devm_ioremap_resource(dev, res);
  494. if (IS_ERR((void *)crc->regs)) {
  495. dev_err(&pdev->dev, "Cannot map CRC IO\n");
  496. return PTR_ERR((void *)crc->regs);
  497. }
  498. crc->irq = platform_get_irq(pdev, 0);
  499. if (crc->irq < 0) {
  500. dev_err(&pdev->dev, "No CRC DCNTEXP IRQ specified\n");
  501. return -ENOENT;
  502. }
  503. ret = devm_request_irq(dev, crc->irq, bfin_crypto_crc_handler,
  504. IRQF_SHARED, dev_name(dev), crc);
  505. if (ret) {
  506. dev_err(&pdev->dev, "Unable to request blackfin crc irq\n");
  507. return ret;
  508. }
  509. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  510. if (res == NULL) {
  511. dev_err(&pdev->dev, "No CRC DMA channel specified\n");
  512. return -ENOENT;
  513. }
  514. crc->dma_ch = res->start;
  515. ret = request_dma(crc->dma_ch, dev_name(dev));
  516. if (ret) {
  517. dev_err(&pdev->dev, "Unable to attach Blackfin CRC DMA channel\n");
  518. return ret;
  519. }
  520. crc->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &crc->sg_dma, GFP_KERNEL);
  521. if (crc->sg_cpu == NULL) {
  522. ret = -ENOMEM;
  523. goto out_error_dma;
  524. }
  525. /*
  526. * need at most CRC_MAX_DMA_DESC sg + CRC_MAX_DMA_DESC middle +
  527. * 1 last + 1 next dma descriptors
  528. */
  529. crc->sg_mid_buf = (u8 *)(crc->sg_cpu + ((CRC_MAX_DMA_DESC + 1) << 1));
  530. crc->sg_mid_dma = crc->sg_dma + sizeof(struct dma_desc_array)
  531. * ((CRC_MAX_DMA_DESC + 1) << 1);
  532. writel(0, &crc->regs->control);
  533. crc->poly = (u32)pdev->dev.platform_data;
  534. writel(crc->poly, &crc->regs->poly);
  535. while (!(readl(&crc->regs->status) & LUTDONE) && (--timeout) > 0)
  536. cpu_relax();
  537. if (timeout == 0)
  538. dev_info(&pdev->dev, "init crc poly timeout\n");
  539. platform_set_drvdata(pdev, crc);
  540. spin_lock(&crc_list.lock);
  541. list_add(&crc->list, &crc_list.dev_list);
  542. spin_unlock(&crc_list.lock);
  543. if (list_is_singular(&crc_list.dev_list)) {
  544. ret = crypto_register_ahash(&algs);
  545. if (ret) {
  546. dev_err(&pdev->dev,
  547. "Can't register crypto ahash device\n");
  548. goto out_error_dma;
  549. }
  550. }
  551. dev_info(&pdev->dev, "initialized\n");
  552. return 0;
  553. out_error_dma:
  554. if (crc->sg_cpu)
  555. dma_free_coherent(&pdev->dev, PAGE_SIZE, crc->sg_cpu, crc->sg_dma);
  556. free_dma(crc->dma_ch);
  557. return ret;
  558. }
  559. /**
  560. * bfin_crypto_crc_remove - Initialize module
  561. *
  562. */
  563. static int bfin_crypto_crc_remove(struct platform_device *pdev)
  564. {
  565. struct bfin_crypto_crc *crc = platform_get_drvdata(pdev);
  566. if (!crc)
  567. return -ENODEV;
  568. spin_lock(&crc_list.lock);
  569. list_del(&crc->list);
  570. spin_unlock(&crc_list.lock);
  571. crypto_unregister_ahash(&algs);
  572. tasklet_kill(&crc->done_task);
  573. free_dma(crc->dma_ch);
  574. return 0;
  575. }
  576. static struct platform_driver bfin_crypto_crc_driver = {
  577. .probe = bfin_crypto_crc_probe,
  578. .remove = bfin_crypto_crc_remove,
  579. .suspend = bfin_crypto_crc_suspend,
  580. .resume = bfin_crypto_crc_resume,
  581. .driver = {
  582. .name = DRIVER_NAME,
  583. },
  584. };
  585. /**
  586. * bfin_crypto_crc_mod_init - Initialize module
  587. *
  588. * Checks the module params and registers the platform driver.
  589. * Real work is in the platform probe function.
  590. */
  591. static int __init bfin_crypto_crc_mod_init(void)
  592. {
  593. int ret;
  594. pr_info("Blackfin hardware CRC crypto driver\n");
  595. INIT_LIST_HEAD(&crc_list.dev_list);
  596. spin_lock_init(&crc_list.lock);
  597. ret = platform_driver_register(&bfin_crypto_crc_driver);
  598. if (ret) {
  599. pr_err("unable to register driver\n");
  600. return ret;
  601. }
  602. return 0;
  603. }
  604. /**
  605. * bfin_crypto_crc_mod_exit - Deinitialize module
  606. */
  607. static void __exit bfin_crypto_crc_mod_exit(void)
  608. {
  609. platform_driver_unregister(&bfin_crypto_crc_driver);
  610. }
  611. module_init(bfin_crypto_crc_mod_init);
  612. module_exit(bfin_crypto_crc_mod_exit);
  613. MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
  614. MODULE_DESCRIPTION("Blackfin CRC hardware crypto driver");
  615. MODULE_LICENSE("GPL");