atmel-sha.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Support for ATMEL SHA1/SHA256 HW acceleration.
  5. *
  6. * Copyright (c) 2012 Eukréa Electromatique - ATMEL
  7. * Author: Nicolas Royer <nicolas@eukrea.com>
  8. *
  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.
  12. *
  13. * Some ideas are from omap-sham.c drivers.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/err.h>
  19. #include <linux/clk.h>
  20. #include <linux/io.h>
  21. #include <linux/hw_random.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/device.h>
  24. #include <linux/init.h>
  25. #include <linux/errno.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/irq.h>
  28. #include <linux/scatterlist.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/of_device.h>
  31. #include <linux/delay.h>
  32. #include <linux/crypto.h>
  33. #include <linux/cryptohash.h>
  34. #include <crypto/scatterwalk.h>
  35. #include <crypto/algapi.h>
  36. #include <crypto/sha.h>
  37. #include <crypto/hash.h>
  38. #include <crypto/internal/hash.h>
  39. #include <linux/platform_data/crypto-atmel.h>
  40. #include "atmel-sha-regs.h"
  41. /* SHA flags */
  42. #define SHA_FLAGS_BUSY BIT(0)
  43. #define SHA_FLAGS_FINAL BIT(1)
  44. #define SHA_FLAGS_DMA_ACTIVE BIT(2)
  45. #define SHA_FLAGS_OUTPUT_READY BIT(3)
  46. #define SHA_FLAGS_INIT BIT(4)
  47. #define SHA_FLAGS_CPU BIT(5)
  48. #define SHA_FLAGS_DMA_READY BIT(6)
  49. #define SHA_FLAGS_FINUP BIT(16)
  50. #define SHA_FLAGS_SG BIT(17)
  51. #define SHA_FLAGS_SHA1 BIT(18)
  52. #define SHA_FLAGS_SHA224 BIT(19)
  53. #define SHA_FLAGS_SHA256 BIT(20)
  54. #define SHA_FLAGS_SHA384 BIT(21)
  55. #define SHA_FLAGS_SHA512 BIT(22)
  56. #define SHA_FLAGS_ERROR BIT(23)
  57. #define SHA_FLAGS_PAD BIT(24)
  58. #define SHA_OP_UPDATE 1
  59. #define SHA_OP_FINAL 2
  60. #define SHA_BUFFER_LEN PAGE_SIZE
  61. #define ATMEL_SHA_DMA_THRESHOLD 56
  62. struct atmel_sha_caps {
  63. bool has_dma;
  64. bool has_dualbuff;
  65. bool has_sha224;
  66. bool has_sha_384_512;
  67. };
  68. struct atmel_sha_dev;
  69. struct atmel_sha_reqctx {
  70. struct atmel_sha_dev *dd;
  71. unsigned long flags;
  72. unsigned long op;
  73. u8 digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32));
  74. u64 digcnt[2];
  75. size_t bufcnt;
  76. size_t buflen;
  77. dma_addr_t dma_addr;
  78. /* walk state */
  79. struct scatterlist *sg;
  80. unsigned int offset; /* offset in current sg */
  81. unsigned int total; /* total request */
  82. size_t block_size;
  83. u8 buffer[0] __aligned(sizeof(u32));
  84. };
  85. struct atmel_sha_ctx {
  86. struct atmel_sha_dev *dd;
  87. unsigned long flags;
  88. };
  89. #define ATMEL_SHA_QUEUE_LENGTH 50
  90. struct atmel_sha_dma {
  91. struct dma_chan *chan;
  92. struct dma_slave_config dma_conf;
  93. };
  94. struct atmel_sha_dev {
  95. struct list_head list;
  96. unsigned long phys_base;
  97. struct device *dev;
  98. struct clk *iclk;
  99. int irq;
  100. void __iomem *io_base;
  101. spinlock_t lock;
  102. int err;
  103. struct tasklet_struct done_task;
  104. unsigned long flags;
  105. struct crypto_queue queue;
  106. struct ahash_request *req;
  107. struct atmel_sha_dma dma_lch_in;
  108. struct atmel_sha_caps caps;
  109. u32 hw_version;
  110. };
  111. struct atmel_sha_drv {
  112. struct list_head dev_list;
  113. spinlock_t lock;
  114. };
  115. static struct atmel_sha_drv atmel_sha = {
  116. .dev_list = LIST_HEAD_INIT(atmel_sha.dev_list),
  117. .lock = __SPIN_LOCK_UNLOCKED(atmel_sha.lock),
  118. };
  119. static inline u32 atmel_sha_read(struct atmel_sha_dev *dd, u32 offset)
  120. {
  121. return readl_relaxed(dd->io_base + offset);
  122. }
  123. static inline void atmel_sha_write(struct atmel_sha_dev *dd,
  124. u32 offset, u32 value)
  125. {
  126. writel_relaxed(value, dd->io_base + offset);
  127. }
  128. static size_t atmel_sha_append_sg(struct atmel_sha_reqctx *ctx)
  129. {
  130. size_t count;
  131. while ((ctx->bufcnt < ctx->buflen) && ctx->total) {
  132. count = min(ctx->sg->length - ctx->offset, ctx->total);
  133. count = min(count, ctx->buflen - ctx->bufcnt);
  134. if (count <= 0) {
  135. /*
  136. * Check if count <= 0 because the buffer is full or
  137. * because the sg length is 0. In the latest case,
  138. * check if there is another sg in the list, a 0 length
  139. * sg doesn't necessarily mean the end of the sg list.
  140. */
  141. if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) {
  142. ctx->sg = sg_next(ctx->sg);
  143. continue;
  144. } else {
  145. break;
  146. }
  147. }
  148. scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg,
  149. ctx->offset, count, 0);
  150. ctx->bufcnt += count;
  151. ctx->offset += count;
  152. ctx->total -= count;
  153. if (ctx->offset == ctx->sg->length) {
  154. ctx->sg = sg_next(ctx->sg);
  155. if (ctx->sg)
  156. ctx->offset = 0;
  157. else
  158. ctx->total = 0;
  159. }
  160. }
  161. return 0;
  162. }
  163. /*
  164. * The purpose of this padding is to ensure that the padded message is a
  165. * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
  166. * The bit "1" is appended at the end of the message followed by
  167. * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
  168. * 128 bits block (SHA384/SHA512) equals to the message length in bits
  169. * is appended.
  170. *
  171. * For SHA1/SHA224/SHA256, padlen is calculated as followed:
  172. * - if message length < 56 bytes then padlen = 56 - message length
  173. * - else padlen = 64 + 56 - message length
  174. *
  175. * For SHA384/SHA512, padlen is calculated as followed:
  176. * - if message length < 112 bytes then padlen = 112 - message length
  177. * - else padlen = 128 + 112 - message length
  178. */
  179. static void atmel_sha_fill_padding(struct atmel_sha_reqctx *ctx, int length)
  180. {
  181. unsigned int index, padlen;
  182. u64 bits[2];
  183. u64 size[2];
  184. size[0] = ctx->digcnt[0];
  185. size[1] = ctx->digcnt[1];
  186. size[0] += ctx->bufcnt;
  187. if (size[0] < ctx->bufcnt)
  188. size[1]++;
  189. size[0] += length;
  190. if (size[0] < length)
  191. size[1]++;
  192. bits[1] = cpu_to_be64(size[0] << 3);
  193. bits[0] = cpu_to_be64(size[1] << 3 | size[0] >> 61);
  194. if (ctx->flags & (SHA_FLAGS_SHA384 | SHA_FLAGS_SHA512)) {
  195. index = ctx->bufcnt & 0x7f;
  196. padlen = (index < 112) ? (112 - index) : ((128+112) - index);
  197. *(ctx->buffer + ctx->bufcnt) = 0x80;
  198. memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
  199. memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16);
  200. ctx->bufcnt += padlen + 16;
  201. ctx->flags |= SHA_FLAGS_PAD;
  202. } else {
  203. index = ctx->bufcnt & 0x3f;
  204. padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  205. *(ctx->buffer + ctx->bufcnt) = 0x80;
  206. memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
  207. memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8);
  208. ctx->bufcnt += padlen + 8;
  209. ctx->flags |= SHA_FLAGS_PAD;
  210. }
  211. }
  212. static int atmel_sha_init(struct ahash_request *req)
  213. {
  214. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  215. struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
  216. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  217. struct atmel_sha_dev *dd = NULL;
  218. struct atmel_sha_dev *tmp;
  219. spin_lock_bh(&atmel_sha.lock);
  220. if (!tctx->dd) {
  221. list_for_each_entry(tmp, &atmel_sha.dev_list, list) {
  222. dd = tmp;
  223. break;
  224. }
  225. tctx->dd = dd;
  226. } else {
  227. dd = tctx->dd;
  228. }
  229. spin_unlock_bh(&atmel_sha.lock);
  230. ctx->dd = dd;
  231. ctx->flags = 0;
  232. dev_dbg(dd->dev, "init: digest size: %d\n",
  233. crypto_ahash_digestsize(tfm));
  234. switch (crypto_ahash_digestsize(tfm)) {
  235. case SHA1_DIGEST_SIZE:
  236. ctx->flags |= SHA_FLAGS_SHA1;
  237. ctx->block_size = SHA1_BLOCK_SIZE;
  238. break;
  239. case SHA224_DIGEST_SIZE:
  240. ctx->flags |= SHA_FLAGS_SHA224;
  241. ctx->block_size = SHA224_BLOCK_SIZE;
  242. break;
  243. case SHA256_DIGEST_SIZE:
  244. ctx->flags |= SHA_FLAGS_SHA256;
  245. ctx->block_size = SHA256_BLOCK_SIZE;
  246. break;
  247. case SHA384_DIGEST_SIZE:
  248. ctx->flags |= SHA_FLAGS_SHA384;
  249. ctx->block_size = SHA384_BLOCK_SIZE;
  250. break;
  251. case SHA512_DIGEST_SIZE:
  252. ctx->flags |= SHA_FLAGS_SHA512;
  253. ctx->block_size = SHA512_BLOCK_SIZE;
  254. break;
  255. default:
  256. return -EINVAL;
  257. break;
  258. }
  259. ctx->bufcnt = 0;
  260. ctx->digcnt[0] = 0;
  261. ctx->digcnt[1] = 0;
  262. ctx->buflen = SHA_BUFFER_LEN;
  263. return 0;
  264. }
  265. static void atmel_sha_write_ctrl(struct atmel_sha_dev *dd, int dma)
  266. {
  267. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  268. u32 valcr = 0, valmr = SHA_MR_MODE_AUTO;
  269. if (likely(dma)) {
  270. if (!dd->caps.has_dma)
  271. atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE);
  272. valmr = SHA_MR_MODE_PDC;
  273. if (dd->caps.has_dualbuff)
  274. valmr |= SHA_MR_DUALBUFF;
  275. } else {
  276. atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  277. }
  278. if (ctx->flags & SHA_FLAGS_SHA1)
  279. valmr |= SHA_MR_ALGO_SHA1;
  280. else if (ctx->flags & SHA_FLAGS_SHA224)
  281. valmr |= SHA_MR_ALGO_SHA224;
  282. else if (ctx->flags & SHA_FLAGS_SHA256)
  283. valmr |= SHA_MR_ALGO_SHA256;
  284. else if (ctx->flags & SHA_FLAGS_SHA384)
  285. valmr |= SHA_MR_ALGO_SHA384;
  286. else if (ctx->flags & SHA_FLAGS_SHA512)
  287. valmr |= SHA_MR_ALGO_SHA512;
  288. /* Setting CR_FIRST only for the first iteration */
  289. if (!(ctx->digcnt[0] || ctx->digcnt[1]))
  290. valcr = SHA_CR_FIRST;
  291. atmel_sha_write(dd, SHA_CR, valcr);
  292. atmel_sha_write(dd, SHA_MR, valmr);
  293. }
  294. static int atmel_sha_xmit_cpu(struct atmel_sha_dev *dd, const u8 *buf,
  295. size_t length, int final)
  296. {
  297. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  298. int count, len32;
  299. const u32 *buffer = (const u32 *)buf;
  300. dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
  301. ctx->digcnt[1], ctx->digcnt[0], length, final);
  302. atmel_sha_write_ctrl(dd, 0);
  303. /* should be non-zero before next lines to disable clocks later */
  304. ctx->digcnt[0] += length;
  305. if (ctx->digcnt[0] < length)
  306. ctx->digcnt[1]++;
  307. if (final)
  308. dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
  309. len32 = DIV_ROUND_UP(length, sizeof(u32));
  310. dd->flags |= SHA_FLAGS_CPU;
  311. for (count = 0; count < len32; count++)
  312. atmel_sha_write(dd, SHA_REG_DIN(count), buffer[count]);
  313. return -EINPROGRESS;
  314. }
  315. static int atmel_sha_xmit_pdc(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
  316. size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
  317. {
  318. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  319. int len32;
  320. dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
  321. ctx->digcnt[1], ctx->digcnt[0], length1, final);
  322. len32 = DIV_ROUND_UP(length1, sizeof(u32));
  323. atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS);
  324. atmel_sha_write(dd, SHA_TPR, dma_addr1);
  325. atmel_sha_write(dd, SHA_TCR, len32);
  326. len32 = DIV_ROUND_UP(length2, sizeof(u32));
  327. atmel_sha_write(dd, SHA_TNPR, dma_addr2);
  328. atmel_sha_write(dd, SHA_TNCR, len32);
  329. atmel_sha_write_ctrl(dd, 1);
  330. /* should be non-zero before next lines to disable clocks later */
  331. ctx->digcnt[0] += length1;
  332. if (ctx->digcnt[0] < length1)
  333. ctx->digcnt[1]++;
  334. if (final)
  335. dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
  336. dd->flags |= SHA_FLAGS_DMA_ACTIVE;
  337. /* Start DMA transfer */
  338. atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTEN);
  339. return -EINPROGRESS;
  340. }
  341. static void atmel_sha_dma_callback(void *data)
  342. {
  343. struct atmel_sha_dev *dd = data;
  344. /* dma_lch_in - completed - wait DATRDY */
  345. atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  346. }
  347. static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
  348. size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
  349. {
  350. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  351. struct dma_async_tx_descriptor *in_desc;
  352. struct scatterlist sg[2];
  353. dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
  354. ctx->digcnt[1], ctx->digcnt[0], length1, final);
  355. dd->dma_lch_in.dma_conf.src_maxburst = 16;
  356. dd->dma_lch_in.dma_conf.dst_maxburst = 16;
  357. dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
  358. if (length2) {
  359. sg_init_table(sg, 2);
  360. sg_dma_address(&sg[0]) = dma_addr1;
  361. sg_dma_len(&sg[0]) = length1;
  362. sg_dma_address(&sg[1]) = dma_addr2;
  363. sg_dma_len(&sg[1]) = length2;
  364. in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2,
  365. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  366. } else {
  367. sg_init_table(sg, 1);
  368. sg_dma_address(&sg[0]) = dma_addr1;
  369. sg_dma_len(&sg[0]) = length1;
  370. in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1,
  371. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  372. }
  373. if (!in_desc)
  374. return -EINVAL;
  375. in_desc->callback = atmel_sha_dma_callback;
  376. in_desc->callback_param = dd;
  377. atmel_sha_write_ctrl(dd, 1);
  378. /* should be non-zero before next lines to disable clocks later */
  379. ctx->digcnt[0] += length1;
  380. if (ctx->digcnt[0] < length1)
  381. ctx->digcnt[1]++;
  382. if (final)
  383. dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
  384. dd->flags |= SHA_FLAGS_DMA_ACTIVE;
  385. /* Start DMA transfer */
  386. dmaengine_submit(in_desc);
  387. dma_async_issue_pending(dd->dma_lch_in.chan);
  388. return -EINPROGRESS;
  389. }
  390. static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
  391. size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
  392. {
  393. if (dd->caps.has_dma)
  394. return atmel_sha_xmit_dma(dd, dma_addr1, length1,
  395. dma_addr2, length2, final);
  396. else
  397. return atmel_sha_xmit_pdc(dd, dma_addr1, length1,
  398. dma_addr2, length2, final);
  399. }
  400. static int atmel_sha_update_cpu(struct atmel_sha_dev *dd)
  401. {
  402. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  403. int bufcnt;
  404. atmel_sha_append_sg(ctx);
  405. atmel_sha_fill_padding(ctx, 0);
  406. bufcnt = ctx->bufcnt;
  407. ctx->bufcnt = 0;
  408. return atmel_sha_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
  409. }
  410. static int atmel_sha_xmit_dma_map(struct atmel_sha_dev *dd,
  411. struct atmel_sha_reqctx *ctx,
  412. size_t length, int final)
  413. {
  414. ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
  415. ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
  416. if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
  417. dev_err(dd->dev, "dma %u bytes error\n", ctx->buflen +
  418. ctx->block_size);
  419. return -EINVAL;
  420. }
  421. ctx->flags &= ~SHA_FLAGS_SG;
  422. /* next call does not fail... so no unmap in the case of error */
  423. return atmel_sha_xmit_start(dd, ctx->dma_addr, length, 0, 0, final);
  424. }
  425. static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd)
  426. {
  427. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  428. unsigned int final;
  429. size_t count;
  430. atmel_sha_append_sg(ctx);
  431. final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
  432. dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: 0x%llx 0x%llx, final: %d\n",
  433. ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final);
  434. if (final)
  435. atmel_sha_fill_padding(ctx, 0);
  436. if (final || (ctx->bufcnt == ctx->buflen)) {
  437. count = ctx->bufcnt;
  438. ctx->bufcnt = 0;
  439. return atmel_sha_xmit_dma_map(dd, ctx, count, final);
  440. }
  441. return 0;
  442. }
  443. static int atmel_sha_update_dma_start(struct atmel_sha_dev *dd)
  444. {
  445. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  446. unsigned int length, final, tail;
  447. struct scatterlist *sg;
  448. unsigned int count;
  449. if (!ctx->total)
  450. return 0;
  451. if (ctx->bufcnt || ctx->offset)
  452. return atmel_sha_update_dma_slow(dd);
  453. dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %u, total: %u\n",
  454. ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total);
  455. sg = ctx->sg;
  456. if (!IS_ALIGNED(sg->offset, sizeof(u32)))
  457. return atmel_sha_update_dma_slow(dd);
  458. if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size))
  459. /* size is not ctx->block_size aligned */
  460. return atmel_sha_update_dma_slow(dd);
  461. length = min(ctx->total, sg->length);
  462. if (sg_is_last(sg)) {
  463. if (!(ctx->flags & SHA_FLAGS_FINUP)) {
  464. /* not last sg must be ctx->block_size aligned */
  465. tail = length & (ctx->block_size - 1);
  466. length -= tail;
  467. }
  468. }
  469. ctx->total -= length;
  470. ctx->offset = length; /* offset where to start slow */
  471. final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
  472. /* Add padding */
  473. if (final) {
  474. tail = length & (ctx->block_size - 1);
  475. length -= tail;
  476. ctx->total += tail;
  477. ctx->offset = length; /* offset where to start slow */
  478. sg = ctx->sg;
  479. atmel_sha_append_sg(ctx);
  480. atmel_sha_fill_padding(ctx, length);
  481. ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
  482. ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
  483. if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
  484. dev_err(dd->dev, "dma %u bytes error\n",
  485. ctx->buflen + ctx->block_size);
  486. return -EINVAL;
  487. }
  488. if (length == 0) {
  489. ctx->flags &= ~SHA_FLAGS_SG;
  490. count = ctx->bufcnt;
  491. ctx->bufcnt = 0;
  492. return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0,
  493. 0, final);
  494. } else {
  495. ctx->sg = sg;
  496. if (!dma_map_sg(dd->dev, ctx->sg, 1,
  497. DMA_TO_DEVICE)) {
  498. dev_err(dd->dev, "dma_map_sg error\n");
  499. return -EINVAL;
  500. }
  501. ctx->flags |= SHA_FLAGS_SG;
  502. count = ctx->bufcnt;
  503. ctx->bufcnt = 0;
  504. return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg),
  505. length, ctx->dma_addr, count, final);
  506. }
  507. }
  508. if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
  509. dev_err(dd->dev, "dma_map_sg error\n");
  510. return -EINVAL;
  511. }
  512. ctx->flags |= SHA_FLAGS_SG;
  513. /* next call does not fail... so no unmap in the case of error */
  514. return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), length, 0,
  515. 0, final);
  516. }
  517. static int atmel_sha_update_dma_stop(struct atmel_sha_dev *dd)
  518. {
  519. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  520. if (ctx->flags & SHA_FLAGS_SG) {
  521. dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
  522. if (ctx->sg->length == ctx->offset) {
  523. ctx->sg = sg_next(ctx->sg);
  524. if (ctx->sg)
  525. ctx->offset = 0;
  526. }
  527. if (ctx->flags & SHA_FLAGS_PAD) {
  528. dma_unmap_single(dd->dev, ctx->dma_addr,
  529. ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
  530. }
  531. } else {
  532. dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen +
  533. ctx->block_size, DMA_TO_DEVICE);
  534. }
  535. return 0;
  536. }
  537. static int atmel_sha_update_req(struct atmel_sha_dev *dd)
  538. {
  539. struct ahash_request *req = dd->req;
  540. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  541. int err;
  542. dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n",
  543. ctx->total, ctx->digcnt[1], ctx->digcnt[0]);
  544. if (ctx->flags & SHA_FLAGS_CPU)
  545. err = atmel_sha_update_cpu(dd);
  546. else
  547. err = atmel_sha_update_dma_start(dd);
  548. /* wait for dma completion before can take more data */
  549. dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx\n",
  550. err, ctx->digcnt[1], ctx->digcnt[0]);
  551. return err;
  552. }
  553. static int atmel_sha_final_req(struct atmel_sha_dev *dd)
  554. {
  555. struct ahash_request *req = dd->req;
  556. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  557. int err = 0;
  558. int count;
  559. if (ctx->bufcnt >= ATMEL_SHA_DMA_THRESHOLD) {
  560. atmel_sha_fill_padding(ctx, 0);
  561. count = ctx->bufcnt;
  562. ctx->bufcnt = 0;
  563. err = atmel_sha_xmit_dma_map(dd, ctx, count, 1);
  564. }
  565. /* faster to handle last block with cpu */
  566. else {
  567. atmel_sha_fill_padding(ctx, 0);
  568. count = ctx->bufcnt;
  569. ctx->bufcnt = 0;
  570. err = atmel_sha_xmit_cpu(dd, ctx->buffer, count, 1);
  571. }
  572. dev_dbg(dd->dev, "final_req: err: %d\n", err);
  573. return err;
  574. }
  575. static void atmel_sha_copy_hash(struct ahash_request *req)
  576. {
  577. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  578. u32 *hash = (u32 *)ctx->digest;
  579. int i;
  580. if (ctx->flags & SHA_FLAGS_SHA1)
  581. for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++)
  582. hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
  583. else if (ctx->flags & SHA_FLAGS_SHA224)
  584. for (i = 0; i < SHA224_DIGEST_SIZE / sizeof(u32); i++)
  585. hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
  586. else if (ctx->flags & SHA_FLAGS_SHA256)
  587. for (i = 0; i < SHA256_DIGEST_SIZE / sizeof(u32); i++)
  588. hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
  589. else if (ctx->flags & SHA_FLAGS_SHA384)
  590. for (i = 0; i < SHA384_DIGEST_SIZE / sizeof(u32); i++)
  591. hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
  592. else
  593. for (i = 0; i < SHA512_DIGEST_SIZE / sizeof(u32); i++)
  594. hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
  595. }
  596. static void atmel_sha_copy_ready_hash(struct ahash_request *req)
  597. {
  598. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  599. if (!req->result)
  600. return;
  601. if (ctx->flags & SHA_FLAGS_SHA1)
  602. memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE);
  603. else if (ctx->flags & SHA_FLAGS_SHA224)
  604. memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE);
  605. else if (ctx->flags & SHA_FLAGS_SHA256)
  606. memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE);
  607. else if (ctx->flags & SHA_FLAGS_SHA384)
  608. memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE);
  609. else
  610. memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE);
  611. }
  612. static int atmel_sha_finish(struct ahash_request *req)
  613. {
  614. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  615. struct atmel_sha_dev *dd = ctx->dd;
  616. int err = 0;
  617. if (ctx->digcnt[0] || ctx->digcnt[1])
  618. atmel_sha_copy_ready_hash(req);
  619. dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %d\n", ctx->digcnt[1],
  620. ctx->digcnt[0], ctx->bufcnt);
  621. return err;
  622. }
  623. static void atmel_sha_finish_req(struct ahash_request *req, int err)
  624. {
  625. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  626. struct atmel_sha_dev *dd = ctx->dd;
  627. if (!err) {
  628. atmel_sha_copy_hash(req);
  629. if (SHA_FLAGS_FINAL & dd->flags)
  630. err = atmel_sha_finish(req);
  631. } else {
  632. ctx->flags |= SHA_FLAGS_ERROR;
  633. }
  634. /* atomic operation is not needed here */
  635. dd->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL | SHA_FLAGS_CPU |
  636. SHA_FLAGS_DMA_READY | SHA_FLAGS_OUTPUT_READY);
  637. clk_disable(dd->iclk);
  638. if (req->base.complete)
  639. req->base.complete(&req->base, err);
  640. /* handle new request */
  641. tasklet_schedule(&dd->done_task);
  642. }
  643. static int atmel_sha_hw_init(struct atmel_sha_dev *dd)
  644. {
  645. int err;
  646. err = clk_enable(dd->iclk);
  647. if (err)
  648. return err;
  649. if (!(SHA_FLAGS_INIT & dd->flags)) {
  650. atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST);
  651. dd->flags |= SHA_FLAGS_INIT;
  652. dd->err = 0;
  653. }
  654. return 0;
  655. }
  656. static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd)
  657. {
  658. return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff;
  659. }
  660. static void atmel_sha_hw_version_init(struct atmel_sha_dev *dd)
  661. {
  662. atmel_sha_hw_init(dd);
  663. dd->hw_version = atmel_sha_get_version(dd);
  664. dev_info(dd->dev,
  665. "version: 0x%x\n", dd->hw_version);
  666. clk_disable(dd->iclk);
  667. }
  668. static int atmel_sha_handle_queue(struct atmel_sha_dev *dd,
  669. struct ahash_request *req)
  670. {
  671. struct crypto_async_request *async_req, *backlog;
  672. struct atmel_sha_reqctx *ctx;
  673. unsigned long flags;
  674. int err = 0, ret = 0;
  675. spin_lock_irqsave(&dd->lock, flags);
  676. if (req)
  677. ret = ahash_enqueue_request(&dd->queue, req);
  678. if (SHA_FLAGS_BUSY & dd->flags) {
  679. spin_unlock_irqrestore(&dd->lock, flags);
  680. return ret;
  681. }
  682. backlog = crypto_get_backlog(&dd->queue);
  683. async_req = crypto_dequeue_request(&dd->queue);
  684. if (async_req)
  685. dd->flags |= SHA_FLAGS_BUSY;
  686. spin_unlock_irqrestore(&dd->lock, flags);
  687. if (!async_req)
  688. return ret;
  689. if (backlog)
  690. backlog->complete(backlog, -EINPROGRESS);
  691. req = ahash_request_cast(async_req);
  692. dd->req = req;
  693. ctx = ahash_request_ctx(req);
  694. dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
  695. ctx->op, req->nbytes);
  696. err = atmel_sha_hw_init(dd);
  697. if (err)
  698. goto err1;
  699. if (ctx->op == SHA_OP_UPDATE) {
  700. err = atmel_sha_update_req(dd);
  701. if (err != -EINPROGRESS && (ctx->flags & SHA_FLAGS_FINUP))
  702. /* no final() after finup() */
  703. err = atmel_sha_final_req(dd);
  704. } else if (ctx->op == SHA_OP_FINAL) {
  705. err = atmel_sha_final_req(dd);
  706. }
  707. err1:
  708. if (err != -EINPROGRESS)
  709. /* done_task will not finish it, so do it here */
  710. atmel_sha_finish_req(req, err);
  711. dev_dbg(dd->dev, "exit, err: %d\n", err);
  712. return ret;
  713. }
  714. static int atmel_sha_enqueue(struct ahash_request *req, unsigned int op)
  715. {
  716. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  717. struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
  718. struct atmel_sha_dev *dd = tctx->dd;
  719. ctx->op = op;
  720. return atmel_sha_handle_queue(dd, req);
  721. }
  722. static int atmel_sha_update(struct ahash_request *req)
  723. {
  724. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  725. if (!req->nbytes)
  726. return 0;
  727. ctx->total = req->nbytes;
  728. ctx->sg = req->src;
  729. ctx->offset = 0;
  730. if (ctx->flags & SHA_FLAGS_FINUP) {
  731. if (ctx->bufcnt + ctx->total < ATMEL_SHA_DMA_THRESHOLD)
  732. /* faster to use CPU for short transfers */
  733. ctx->flags |= SHA_FLAGS_CPU;
  734. } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
  735. atmel_sha_append_sg(ctx);
  736. return 0;
  737. }
  738. return atmel_sha_enqueue(req, SHA_OP_UPDATE);
  739. }
  740. static int atmel_sha_final(struct ahash_request *req)
  741. {
  742. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  743. struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
  744. struct atmel_sha_dev *dd = tctx->dd;
  745. int err = 0;
  746. ctx->flags |= SHA_FLAGS_FINUP;
  747. if (ctx->flags & SHA_FLAGS_ERROR)
  748. return 0; /* uncompleted hash is not needed */
  749. if (ctx->bufcnt) {
  750. return atmel_sha_enqueue(req, SHA_OP_FINAL);
  751. } else if (!(ctx->flags & SHA_FLAGS_PAD)) { /* add padding */
  752. err = atmel_sha_hw_init(dd);
  753. if (err)
  754. goto err1;
  755. dd->flags |= SHA_FLAGS_BUSY;
  756. err = atmel_sha_final_req(dd);
  757. } else {
  758. /* copy ready hash (+ finalize hmac) */
  759. return atmel_sha_finish(req);
  760. }
  761. err1:
  762. if (err != -EINPROGRESS)
  763. /* done_task will not finish it, so do it here */
  764. atmel_sha_finish_req(req, err);
  765. return err;
  766. }
  767. static int atmel_sha_finup(struct ahash_request *req)
  768. {
  769. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  770. int err1, err2;
  771. ctx->flags |= SHA_FLAGS_FINUP;
  772. err1 = atmel_sha_update(req);
  773. if (err1 == -EINPROGRESS ||
  774. (err1 == -EBUSY && (ahash_request_flags(req) &
  775. CRYPTO_TFM_REQ_MAY_BACKLOG)))
  776. return err1;
  777. /*
  778. * final() has to be always called to cleanup resources
  779. * even if udpate() failed, except EINPROGRESS
  780. */
  781. err2 = atmel_sha_final(req);
  782. return err1 ?: err2;
  783. }
  784. static int atmel_sha_digest(struct ahash_request *req)
  785. {
  786. return atmel_sha_init(req) ?: atmel_sha_finup(req);
  787. }
  788. static int atmel_sha_cra_init(struct crypto_tfm *tfm)
  789. {
  790. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  791. sizeof(struct atmel_sha_reqctx) +
  792. SHA_BUFFER_LEN + SHA512_BLOCK_SIZE);
  793. return 0;
  794. }
  795. static struct ahash_alg sha_1_256_algs[] = {
  796. {
  797. .init = atmel_sha_init,
  798. .update = atmel_sha_update,
  799. .final = atmel_sha_final,
  800. .finup = atmel_sha_finup,
  801. .digest = atmel_sha_digest,
  802. .halg = {
  803. .digestsize = SHA1_DIGEST_SIZE,
  804. .base = {
  805. .cra_name = "sha1",
  806. .cra_driver_name = "atmel-sha1",
  807. .cra_priority = 100,
  808. .cra_flags = CRYPTO_ALG_ASYNC,
  809. .cra_blocksize = SHA1_BLOCK_SIZE,
  810. .cra_ctxsize = sizeof(struct atmel_sha_ctx),
  811. .cra_alignmask = 0,
  812. .cra_module = THIS_MODULE,
  813. .cra_init = atmel_sha_cra_init,
  814. }
  815. }
  816. },
  817. {
  818. .init = atmel_sha_init,
  819. .update = atmel_sha_update,
  820. .final = atmel_sha_final,
  821. .finup = atmel_sha_finup,
  822. .digest = atmel_sha_digest,
  823. .halg = {
  824. .digestsize = SHA256_DIGEST_SIZE,
  825. .base = {
  826. .cra_name = "sha256",
  827. .cra_driver_name = "atmel-sha256",
  828. .cra_priority = 100,
  829. .cra_flags = CRYPTO_ALG_ASYNC,
  830. .cra_blocksize = SHA256_BLOCK_SIZE,
  831. .cra_ctxsize = sizeof(struct atmel_sha_ctx),
  832. .cra_alignmask = 0,
  833. .cra_module = THIS_MODULE,
  834. .cra_init = atmel_sha_cra_init,
  835. }
  836. }
  837. },
  838. };
  839. static struct ahash_alg sha_224_alg = {
  840. .init = atmel_sha_init,
  841. .update = atmel_sha_update,
  842. .final = atmel_sha_final,
  843. .finup = atmel_sha_finup,
  844. .digest = atmel_sha_digest,
  845. .halg = {
  846. .digestsize = SHA224_DIGEST_SIZE,
  847. .base = {
  848. .cra_name = "sha224",
  849. .cra_driver_name = "atmel-sha224",
  850. .cra_priority = 100,
  851. .cra_flags = CRYPTO_ALG_ASYNC,
  852. .cra_blocksize = SHA224_BLOCK_SIZE,
  853. .cra_ctxsize = sizeof(struct atmel_sha_ctx),
  854. .cra_alignmask = 0,
  855. .cra_module = THIS_MODULE,
  856. .cra_init = atmel_sha_cra_init,
  857. }
  858. }
  859. };
  860. static struct ahash_alg sha_384_512_algs[] = {
  861. {
  862. .init = atmel_sha_init,
  863. .update = atmel_sha_update,
  864. .final = atmel_sha_final,
  865. .finup = atmel_sha_finup,
  866. .digest = atmel_sha_digest,
  867. .halg = {
  868. .digestsize = SHA384_DIGEST_SIZE,
  869. .base = {
  870. .cra_name = "sha384",
  871. .cra_driver_name = "atmel-sha384",
  872. .cra_priority = 100,
  873. .cra_flags = CRYPTO_ALG_ASYNC,
  874. .cra_blocksize = SHA384_BLOCK_SIZE,
  875. .cra_ctxsize = sizeof(struct atmel_sha_ctx),
  876. .cra_alignmask = 0x3,
  877. .cra_module = THIS_MODULE,
  878. .cra_init = atmel_sha_cra_init,
  879. }
  880. }
  881. },
  882. {
  883. .init = atmel_sha_init,
  884. .update = atmel_sha_update,
  885. .final = atmel_sha_final,
  886. .finup = atmel_sha_finup,
  887. .digest = atmel_sha_digest,
  888. .halg = {
  889. .digestsize = SHA512_DIGEST_SIZE,
  890. .base = {
  891. .cra_name = "sha512",
  892. .cra_driver_name = "atmel-sha512",
  893. .cra_priority = 100,
  894. .cra_flags = CRYPTO_ALG_ASYNC,
  895. .cra_blocksize = SHA512_BLOCK_SIZE,
  896. .cra_ctxsize = sizeof(struct atmel_sha_ctx),
  897. .cra_alignmask = 0x3,
  898. .cra_module = THIS_MODULE,
  899. .cra_init = atmel_sha_cra_init,
  900. }
  901. }
  902. },
  903. };
  904. static void atmel_sha_done_task(unsigned long data)
  905. {
  906. struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
  907. int err = 0;
  908. if (!(SHA_FLAGS_BUSY & dd->flags)) {
  909. atmel_sha_handle_queue(dd, NULL);
  910. return;
  911. }
  912. if (SHA_FLAGS_CPU & dd->flags) {
  913. if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
  914. dd->flags &= ~SHA_FLAGS_OUTPUT_READY;
  915. goto finish;
  916. }
  917. } else if (SHA_FLAGS_DMA_READY & dd->flags) {
  918. if (SHA_FLAGS_DMA_ACTIVE & dd->flags) {
  919. dd->flags &= ~SHA_FLAGS_DMA_ACTIVE;
  920. atmel_sha_update_dma_stop(dd);
  921. if (dd->err) {
  922. err = dd->err;
  923. goto finish;
  924. }
  925. }
  926. if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
  927. /* hash or semi-hash ready */
  928. dd->flags &= ~(SHA_FLAGS_DMA_READY |
  929. SHA_FLAGS_OUTPUT_READY);
  930. err = atmel_sha_update_dma_start(dd);
  931. if (err != -EINPROGRESS)
  932. goto finish;
  933. }
  934. }
  935. return;
  936. finish:
  937. /* finish curent request */
  938. atmel_sha_finish_req(dd->req, err);
  939. }
  940. static irqreturn_t atmel_sha_irq(int irq, void *dev_id)
  941. {
  942. struct atmel_sha_dev *sha_dd = dev_id;
  943. u32 reg;
  944. reg = atmel_sha_read(sha_dd, SHA_ISR);
  945. if (reg & atmel_sha_read(sha_dd, SHA_IMR)) {
  946. atmel_sha_write(sha_dd, SHA_IDR, reg);
  947. if (SHA_FLAGS_BUSY & sha_dd->flags) {
  948. sha_dd->flags |= SHA_FLAGS_OUTPUT_READY;
  949. if (!(SHA_FLAGS_CPU & sha_dd->flags))
  950. sha_dd->flags |= SHA_FLAGS_DMA_READY;
  951. tasklet_schedule(&sha_dd->done_task);
  952. } else {
  953. dev_warn(sha_dd->dev, "SHA interrupt when no active requests.\n");
  954. }
  955. return IRQ_HANDLED;
  956. }
  957. return IRQ_NONE;
  958. }
  959. static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd)
  960. {
  961. int i;
  962. for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++)
  963. crypto_unregister_ahash(&sha_1_256_algs[i]);
  964. if (dd->caps.has_sha224)
  965. crypto_unregister_ahash(&sha_224_alg);
  966. if (dd->caps.has_sha_384_512) {
  967. for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++)
  968. crypto_unregister_ahash(&sha_384_512_algs[i]);
  969. }
  970. }
  971. static int atmel_sha_register_algs(struct atmel_sha_dev *dd)
  972. {
  973. int err, i, j;
  974. for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) {
  975. err = crypto_register_ahash(&sha_1_256_algs[i]);
  976. if (err)
  977. goto err_sha_1_256_algs;
  978. }
  979. if (dd->caps.has_sha224) {
  980. err = crypto_register_ahash(&sha_224_alg);
  981. if (err)
  982. goto err_sha_224_algs;
  983. }
  984. if (dd->caps.has_sha_384_512) {
  985. for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) {
  986. err = crypto_register_ahash(&sha_384_512_algs[i]);
  987. if (err)
  988. goto err_sha_384_512_algs;
  989. }
  990. }
  991. return 0;
  992. err_sha_384_512_algs:
  993. for (j = 0; j < i; j++)
  994. crypto_unregister_ahash(&sha_384_512_algs[j]);
  995. crypto_unregister_ahash(&sha_224_alg);
  996. err_sha_224_algs:
  997. i = ARRAY_SIZE(sha_1_256_algs);
  998. err_sha_1_256_algs:
  999. for (j = 0; j < i; j++)
  1000. crypto_unregister_ahash(&sha_1_256_algs[j]);
  1001. return err;
  1002. }
  1003. static bool atmel_sha_filter(struct dma_chan *chan, void *slave)
  1004. {
  1005. struct at_dma_slave *sl = slave;
  1006. if (sl && sl->dma_dev == chan->device->dev) {
  1007. chan->private = sl;
  1008. return true;
  1009. } else {
  1010. return false;
  1011. }
  1012. }
  1013. static int atmel_sha_dma_init(struct atmel_sha_dev *dd,
  1014. struct crypto_platform_data *pdata)
  1015. {
  1016. int err = -ENOMEM;
  1017. dma_cap_mask_t mask_in;
  1018. /* Try to grab DMA channel */
  1019. dma_cap_zero(mask_in);
  1020. dma_cap_set(DMA_SLAVE, mask_in);
  1021. dd->dma_lch_in.chan = dma_request_slave_channel_compat(mask_in,
  1022. atmel_sha_filter, &pdata->dma_slave->rxdata, dd->dev, "tx");
  1023. if (!dd->dma_lch_in.chan) {
  1024. dev_warn(dd->dev, "no DMA channel available\n");
  1025. return err;
  1026. }
  1027. dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV;
  1028. dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
  1029. SHA_REG_DIN(0);
  1030. dd->dma_lch_in.dma_conf.src_maxburst = 1;
  1031. dd->dma_lch_in.dma_conf.src_addr_width =
  1032. DMA_SLAVE_BUSWIDTH_4_BYTES;
  1033. dd->dma_lch_in.dma_conf.dst_maxburst = 1;
  1034. dd->dma_lch_in.dma_conf.dst_addr_width =
  1035. DMA_SLAVE_BUSWIDTH_4_BYTES;
  1036. dd->dma_lch_in.dma_conf.device_fc = false;
  1037. return 0;
  1038. }
  1039. static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd)
  1040. {
  1041. dma_release_channel(dd->dma_lch_in.chan);
  1042. }
  1043. static void atmel_sha_get_cap(struct atmel_sha_dev *dd)
  1044. {
  1045. dd->caps.has_dma = 0;
  1046. dd->caps.has_dualbuff = 0;
  1047. dd->caps.has_sha224 = 0;
  1048. dd->caps.has_sha_384_512 = 0;
  1049. /* keep only major version number */
  1050. switch (dd->hw_version & 0xff0) {
  1051. case 0x420:
  1052. dd->caps.has_dma = 1;
  1053. dd->caps.has_dualbuff = 1;
  1054. dd->caps.has_sha224 = 1;
  1055. dd->caps.has_sha_384_512 = 1;
  1056. break;
  1057. case 0x410:
  1058. dd->caps.has_dma = 1;
  1059. dd->caps.has_dualbuff = 1;
  1060. dd->caps.has_sha224 = 1;
  1061. dd->caps.has_sha_384_512 = 1;
  1062. break;
  1063. case 0x400:
  1064. dd->caps.has_dma = 1;
  1065. dd->caps.has_dualbuff = 1;
  1066. dd->caps.has_sha224 = 1;
  1067. break;
  1068. case 0x320:
  1069. break;
  1070. default:
  1071. dev_warn(dd->dev,
  1072. "Unmanaged sha version, set minimum capabilities\n");
  1073. break;
  1074. }
  1075. }
  1076. #if defined(CONFIG_OF)
  1077. static const struct of_device_id atmel_sha_dt_ids[] = {
  1078. { .compatible = "atmel,at91sam9g46-sha" },
  1079. { /* sentinel */ }
  1080. };
  1081. MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids);
  1082. static struct crypto_platform_data *atmel_sha_of_init(struct platform_device *pdev)
  1083. {
  1084. struct device_node *np = pdev->dev.of_node;
  1085. struct crypto_platform_data *pdata;
  1086. if (!np) {
  1087. dev_err(&pdev->dev, "device node not found\n");
  1088. return ERR_PTR(-EINVAL);
  1089. }
  1090. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1091. if (!pdata) {
  1092. dev_err(&pdev->dev, "could not allocate memory for pdata\n");
  1093. return ERR_PTR(-ENOMEM);
  1094. }
  1095. pdata->dma_slave = devm_kzalloc(&pdev->dev,
  1096. sizeof(*(pdata->dma_slave)),
  1097. GFP_KERNEL);
  1098. if (!pdata->dma_slave) {
  1099. dev_err(&pdev->dev, "could not allocate memory for dma_slave\n");
  1100. return ERR_PTR(-ENOMEM);
  1101. }
  1102. return pdata;
  1103. }
  1104. #else /* CONFIG_OF */
  1105. static inline struct crypto_platform_data *atmel_sha_of_init(struct platform_device *dev)
  1106. {
  1107. return ERR_PTR(-EINVAL);
  1108. }
  1109. #endif
  1110. static int atmel_sha_probe(struct platform_device *pdev)
  1111. {
  1112. struct atmel_sha_dev *sha_dd;
  1113. struct crypto_platform_data *pdata;
  1114. struct device *dev = &pdev->dev;
  1115. struct resource *sha_res;
  1116. int err;
  1117. sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL);
  1118. if (sha_dd == NULL) {
  1119. dev_err(dev, "unable to alloc data struct.\n");
  1120. err = -ENOMEM;
  1121. goto sha_dd_err;
  1122. }
  1123. sha_dd->dev = dev;
  1124. platform_set_drvdata(pdev, sha_dd);
  1125. INIT_LIST_HEAD(&sha_dd->list);
  1126. spin_lock_init(&sha_dd->lock);
  1127. tasklet_init(&sha_dd->done_task, atmel_sha_done_task,
  1128. (unsigned long)sha_dd);
  1129. crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH);
  1130. sha_dd->irq = -1;
  1131. /* Get the base address */
  1132. sha_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1133. if (!sha_res) {
  1134. dev_err(dev, "no MEM resource info\n");
  1135. err = -ENODEV;
  1136. goto res_err;
  1137. }
  1138. sha_dd->phys_base = sha_res->start;
  1139. /* Get the IRQ */
  1140. sha_dd->irq = platform_get_irq(pdev, 0);
  1141. if (sha_dd->irq < 0) {
  1142. dev_err(dev, "no IRQ resource info\n");
  1143. err = sha_dd->irq;
  1144. goto res_err;
  1145. }
  1146. err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq,
  1147. IRQF_SHARED, "atmel-sha", sha_dd);
  1148. if (err) {
  1149. dev_err(dev, "unable to request sha irq.\n");
  1150. goto res_err;
  1151. }
  1152. /* Initializing the clock */
  1153. sha_dd->iclk = devm_clk_get(&pdev->dev, "sha_clk");
  1154. if (IS_ERR(sha_dd->iclk)) {
  1155. dev_err(dev, "clock initialization failed.\n");
  1156. err = PTR_ERR(sha_dd->iclk);
  1157. goto res_err;
  1158. }
  1159. sha_dd->io_base = devm_ioremap_resource(&pdev->dev, sha_res);
  1160. if (IS_ERR(sha_dd->io_base)) {
  1161. dev_err(dev, "can't ioremap\n");
  1162. err = PTR_ERR(sha_dd->io_base);
  1163. goto res_err;
  1164. }
  1165. err = clk_prepare(sha_dd->iclk);
  1166. if (err)
  1167. goto res_err;
  1168. atmel_sha_hw_version_init(sha_dd);
  1169. atmel_sha_get_cap(sha_dd);
  1170. if (sha_dd->caps.has_dma) {
  1171. pdata = pdev->dev.platform_data;
  1172. if (!pdata) {
  1173. pdata = atmel_sha_of_init(pdev);
  1174. if (IS_ERR(pdata)) {
  1175. dev_err(&pdev->dev, "platform data not available\n");
  1176. err = PTR_ERR(pdata);
  1177. goto iclk_unprepare;
  1178. }
  1179. }
  1180. if (!pdata->dma_slave) {
  1181. err = -ENXIO;
  1182. goto iclk_unprepare;
  1183. }
  1184. err = atmel_sha_dma_init(sha_dd, pdata);
  1185. if (err)
  1186. goto err_sha_dma;
  1187. dev_info(dev, "using %s for DMA transfers\n",
  1188. dma_chan_name(sha_dd->dma_lch_in.chan));
  1189. }
  1190. spin_lock(&atmel_sha.lock);
  1191. list_add_tail(&sha_dd->list, &atmel_sha.dev_list);
  1192. spin_unlock(&atmel_sha.lock);
  1193. err = atmel_sha_register_algs(sha_dd);
  1194. if (err)
  1195. goto err_algs;
  1196. dev_info(dev, "Atmel SHA1/SHA256%s%s\n",
  1197. sha_dd->caps.has_sha224 ? "/SHA224" : "",
  1198. sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : "");
  1199. return 0;
  1200. err_algs:
  1201. spin_lock(&atmel_sha.lock);
  1202. list_del(&sha_dd->list);
  1203. spin_unlock(&atmel_sha.lock);
  1204. if (sha_dd->caps.has_dma)
  1205. atmel_sha_dma_cleanup(sha_dd);
  1206. err_sha_dma:
  1207. iclk_unprepare:
  1208. clk_unprepare(sha_dd->iclk);
  1209. res_err:
  1210. tasklet_kill(&sha_dd->done_task);
  1211. sha_dd_err:
  1212. dev_err(dev, "initialization failed.\n");
  1213. return err;
  1214. }
  1215. static int atmel_sha_remove(struct platform_device *pdev)
  1216. {
  1217. static struct atmel_sha_dev *sha_dd;
  1218. sha_dd = platform_get_drvdata(pdev);
  1219. if (!sha_dd)
  1220. return -ENODEV;
  1221. spin_lock(&atmel_sha.lock);
  1222. list_del(&sha_dd->list);
  1223. spin_unlock(&atmel_sha.lock);
  1224. atmel_sha_unregister_algs(sha_dd);
  1225. tasklet_kill(&sha_dd->done_task);
  1226. if (sha_dd->caps.has_dma)
  1227. atmel_sha_dma_cleanup(sha_dd);
  1228. clk_unprepare(sha_dd->iclk);
  1229. return 0;
  1230. }
  1231. static struct platform_driver atmel_sha_driver = {
  1232. .probe = atmel_sha_probe,
  1233. .remove = atmel_sha_remove,
  1234. .driver = {
  1235. .name = "atmel_sha",
  1236. .of_match_table = of_match_ptr(atmel_sha_dt_ids),
  1237. },
  1238. };
  1239. module_platform_driver(atmel_sha_driver);
  1240. MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
  1241. MODULE_LICENSE("GPL v2");
  1242. MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");