bfin_crc.c 19 KB

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