omap-sham.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Support for OMAP SHA1/MD5 HW acceleration.
  5. *
  6. * Copyright (c) 2010 Nokia Corporation
  7. * Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.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 old omap-sha1-md5.c driver.
  14. */
  15. #define pr_fmt(fmt) "%s: " fmt, __func__
  16. #include <linux/err.h>
  17. #include <linux/device.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/errno.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/kernel.h>
  23. #include <linux/clk.h>
  24. #include <linux/irq.h>
  25. #include <linux/io.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/scatterlist.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/delay.h>
  30. #include <linux/crypto.h>
  31. #include <linux/cryptohash.h>
  32. #include <crypto/scatterwalk.h>
  33. #include <crypto/algapi.h>
  34. #include <crypto/sha.h>
  35. #include <crypto/hash.h>
  36. #include <crypto/internal/hash.h>
  37. #include <linux/omap-dma.h>
  38. #ifdef CONFIG_ARCH_OMAP1
  39. #include <mach/irqs.h>
  40. #endif
  41. #define SHA_REG_DIGEST(x) (0x00 + ((x) * 0x04))
  42. #define SHA_REG_DIN(x) (0x1C + ((x) * 0x04))
  43. #define SHA1_MD5_BLOCK_SIZE SHA1_BLOCK_SIZE
  44. #define MD5_DIGEST_SIZE 16
  45. #define SHA_REG_DIGCNT 0x14
  46. #define SHA_REG_CTRL 0x18
  47. #define SHA_REG_CTRL_LENGTH (0xFFFFFFFF << 5)
  48. #define SHA_REG_CTRL_CLOSE_HASH (1 << 4)
  49. #define SHA_REG_CTRL_ALGO_CONST (1 << 3)
  50. #define SHA_REG_CTRL_ALGO (1 << 2)
  51. #define SHA_REG_CTRL_INPUT_READY (1 << 1)
  52. #define SHA_REG_CTRL_OUTPUT_READY (1 << 0)
  53. #define SHA_REG_REV 0x5C
  54. #define SHA_REG_REV_MAJOR 0xF0
  55. #define SHA_REG_REV_MINOR 0x0F
  56. #define SHA_REG_MASK 0x60
  57. #define SHA_REG_MASK_DMA_EN (1 << 3)
  58. #define SHA_REG_MASK_IT_EN (1 << 2)
  59. #define SHA_REG_MASK_SOFTRESET (1 << 1)
  60. #define SHA_REG_AUTOIDLE (1 << 0)
  61. #define SHA_REG_SYSSTATUS 0x64
  62. #define SHA_REG_SYSSTATUS_RESETDONE (1 << 0)
  63. #define DEFAULT_TIMEOUT_INTERVAL HZ
  64. /* mostly device flags */
  65. #define FLAGS_BUSY 0
  66. #define FLAGS_FINAL 1
  67. #define FLAGS_DMA_ACTIVE 2
  68. #define FLAGS_OUTPUT_READY 3
  69. #define FLAGS_INIT 4
  70. #define FLAGS_CPU 5
  71. #define FLAGS_DMA_READY 6
  72. /* context flags */
  73. #define FLAGS_FINUP 16
  74. #define FLAGS_SG 17
  75. #define FLAGS_SHA1 18
  76. #define FLAGS_HMAC 19
  77. #define FLAGS_ERROR 20
  78. #define OP_UPDATE 1
  79. #define OP_FINAL 2
  80. #define OMAP_ALIGN_MASK (sizeof(u32)-1)
  81. #define OMAP_ALIGNED __attribute__((aligned(sizeof(u32))))
  82. #define BUFLEN PAGE_SIZE
  83. struct omap_sham_dev;
  84. struct omap_sham_reqctx {
  85. struct omap_sham_dev *dd;
  86. unsigned long flags;
  87. unsigned long op;
  88. u8 digest[SHA1_DIGEST_SIZE] OMAP_ALIGNED;
  89. size_t digcnt;
  90. size_t bufcnt;
  91. size_t buflen;
  92. dma_addr_t dma_addr;
  93. /* walk state */
  94. struct scatterlist *sg;
  95. unsigned int offset; /* offset in current sg */
  96. unsigned int total; /* total request */
  97. u8 buffer[0] OMAP_ALIGNED;
  98. };
  99. struct omap_sham_hmac_ctx {
  100. struct crypto_shash *shash;
  101. u8 ipad[SHA1_MD5_BLOCK_SIZE];
  102. u8 opad[SHA1_MD5_BLOCK_SIZE];
  103. };
  104. struct omap_sham_ctx {
  105. struct omap_sham_dev *dd;
  106. unsigned long flags;
  107. /* fallback stuff */
  108. struct crypto_shash *fallback;
  109. struct omap_sham_hmac_ctx base[0];
  110. };
  111. #define OMAP_SHAM_QUEUE_LENGTH 1
  112. struct omap_sham_dev {
  113. struct list_head list;
  114. unsigned long phys_base;
  115. struct device *dev;
  116. void __iomem *io_base;
  117. int irq;
  118. struct clk *iclk;
  119. spinlock_t lock;
  120. int err;
  121. int dma;
  122. int dma_lch;
  123. struct tasklet_struct done_task;
  124. unsigned long flags;
  125. struct crypto_queue queue;
  126. struct ahash_request *req;
  127. };
  128. struct omap_sham_drv {
  129. struct list_head dev_list;
  130. spinlock_t lock;
  131. unsigned long flags;
  132. };
  133. static struct omap_sham_drv sham = {
  134. .dev_list = LIST_HEAD_INIT(sham.dev_list),
  135. .lock = __SPIN_LOCK_UNLOCKED(sham.lock),
  136. };
  137. static inline u32 omap_sham_read(struct omap_sham_dev *dd, u32 offset)
  138. {
  139. return __raw_readl(dd->io_base + offset);
  140. }
  141. static inline void omap_sham_write(struct omap_sham_dev *dd,
  142. u32 offset, u32 value)
  143. {
  144. __raw_writel(value, dd->io_base + offset);
  145. }
  146. static inline void omap_sham_write_mask(struct omap_sham_dev *dd, u32 address,
  147. u32 value, u32 mask)
  148. {
  149. u32 val;
  150. val = omap_sham_read(dd, address);
  151. val &= ~mask;
  152. val |= value;
  153. omap_sham_write(dd, address, val);
  154. }
  155. static inline int omap_sham_wait(struct omap_sham_dev *dd, u32 offset, u32 bit)
  156. {
  157. unsigned long timeout = jiffies + DEFAULT_TIMEOUT_INTERVAL;
  158. while (!(omap_sham_read(dd, offset) & bit)) {
  159. if (time_is_before_jiffies(timeout))
  160. return -ETIMEDOUT;
  161. }
  162. return 0;
  163. }
  164. static void omap_sham_copy_hash(struct ahash_request *req, int out)
  165. {
  166. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  167. u32 *hash = (u32 *)ctx->digest;
  168. int i;
  169. /* MD5 is almost unused. So copy sha1 size to reduce code */
  170. for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++) {
  171. if (out)
  172. hash[i] = omap_sham_read(ctx->dd,
  173. SHA_REG_DIGEST(i));
  174. else
  175. omap_sham_write(ctx->dd,
  176. SHA_REG_DIGEST(i), hash[i]);
  177. }
  178. }
  179. static void omap_sham_copy_ready_hash(struct ahash_request *req)
  180. {
  181. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  182. u32 *in = (u32 *)ctx->digest;
  183. u32 *hash = (u32 *)req->result;
  184. int i;
  185. if (!hash)
  186. return;
  187. if (likely(ctx->flags & BIT(FLAGS_SHA1))) {
  188. /* SHA1 results are in big endian */
  189. for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++)
  190. hash[i] = be32_to_cpu(in[i]);
  191. } else {
  192. /* MD5 results are in little endian */
  193. for (i = 0; i < MD5_DIGEST_SIZE / sizeof(u32); i++)
  194. hash[i] = le32_to_cpu(in[i]);
  195. }
  196. }
  197. static int omap_sham_hw_init(struct omap_sham_dev *dd)
  198. {
  199. clk_enable(dd->iclk);
  200. if (!test_bit(FLAGS_INIT, &dd->flags)) {
  201. omap_sham_write_mask(dd, SHA_REG_MASK,
  202. SHA_REG_MASK_SOFTRESET, SHA_REG_MASK_SOFTRESET);
  203. if (omap_sham_wait(dd, SHA_REG_SYSSTATUS,
  204. SHA_REG_SYSSTATUS_RESETDONE))
  205. return -ETIMEDOUT;
  206. set_bit(FLAGS_INIT, &dd->flags);
  207. dd->err = 0;
  208. }
  209. return 0;
  210. }
  211. static void omap_sham_write_ctrl(struct omap_sham_dev *dd, size_t length,
  212. int final, int dma)
  213. {
  214. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  215. u32 val = length << 5, mask;
  216. if (likely(ctx->digcnt))
  217. omap_sham_write(dd, SHA_REG_DIGCNT, ctx->digcnt);
  218. omap_sham_write_mask(dd, SHA_REG_MASK,
  219. SHA_REG_MASK_IT_EN | (dma ? SHA_REG_MASK_DMA_EN : 0),
  220. SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN);
  221. /*
  222. * Setting ALGO_CONST only for the first iteration
  223. * and CLOSE_HASH only for the last one.
  224. */
  225. if (ctx->flags & BIT(FLAGS_SHA1))
  226. val |= SHA_REG_CTRL_ALGO;
  227. if (!ctx->digcnt)
  228. val |= SHA_REG_CTRL_ALGO_CONST;
  229. if (final)
  230. val |= SHA_REG_CTRL_CLOSE_HASH;
  231. mask = SHA_REG_CTRL_ALGO_CONST | SHA_REG_CTRL_CLOSE_HASH |
  232. SHA_REG_CTRL_ALGO | SHA_REG_CTRL_LENGTH;
  233. omap_sham_write_mask(dd, SHA_REG_CTRL, val, mask);
  234. }
  235. static int omap_sham_xmit_cpu(struct omap_sham_dev *dd, const u8 *buf,
  236. size_t length, int final)
  237. {
  238. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  239. int count, len32;
  240. const u32 *buffer = (const u32 *)buf;
  241. dev_dbg(dd->dev, "xmit_cpu: digcnt: %d, length: %d, final: %d\n",
  242. ctx->digcnt, length, final);
  243. omap_sham_write_ctrl(dd, length, final, 0);
  244. /* should be non-zero before next lines to disable clocks later */
  245. ctx->digcnt += length;
  246. if (omap_sham_wait(dd, SHA_REG_CTRL, SHA_REG_CTRL_INPUT_READY))
  247. return -ETIMEDOUT;
  248. if (final)
  249. set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
  250. set_bit(FLAGS_CPU, &dd->flags);
  251. len32 = DIV_ROUND_UP(length, sizeof(u32));
  252. for (count = 0; count < len32; count++)
  253. omap_sham_write(dd, SHA_REG_DIN(count), buffer[count]);
  254. return -EINPROGRESS;
  255. }
  256. static int omap_sham_xmit_dma(struct omap_sham_dev *dd, dma_addr_t dma_addr,
  257. size_t length, int final)
  258. {
  259. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  260. int len32;
  261. dev_dbg(dd->dev, "xmit_dma: digcnt: %d, length: %d, final: %d\n",
  262. ctx->digcnt, length, final);
  263. len32 = DIV_ROUND_UP(length, sizeof(u32));
  264. omap_set_dma_transfer_params(dd->dma_lch, OMAP_DMA_DATA_TYPE_S32, len32,
  265. 1, OMAP_DMA_SYNC_PACKET, dd->dma,
  266. OMAP_DMA_DST_SYNC_PREFETCH);
  267. omap_set_dma_src_params(dd->dma_lch, 0, OMAP_DMA_AMODE_POST_INC,
  268. dma_addr, 0, 0);
  269. omap_sham_write_ctrl(dd, length, final, 1);
  270. ctx->digcnt += length;
  271. if (final)
  272. set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
  273. set_bit(FLAGS_DMA_ACTIVE, &dd->flags);
  274. omap_start_dma(dd->dma_lch);
  275. return -EINPROGRESS;
  276. }
  277. static size_t omap_sham_append_buffer(struct omap_sham_reqctx *ctx,
  278. const u8 *data, size_t length)
  279. {
  280. size_t count = min(length, ctx->buflen - ctx->bufcnt);
  281. count = min(count, ctx->total);
  282. if (count <= 0)
  283. return 0;
  284. memcpy(ctx->buffer + ctx->bufcnt, data, count);
  285. ctx->bufcnt += count;
  286. return count;
  287. }
  288. static size_t omap_sham_append_sg(struct omap_sham_reqctx *ctx)
  289. {
  290. size_t count;
  291. while (ctx->sg) {
  292. count = omap_sham_append_buffer(ctx,
  293. sg_virt(ctx->sg) + ctx->offset,
  294. ctx->sg->length - ctx->offset);
  295. if (!count)
  296. break;
  297. ctx->offset += count;
  298. ctx->total -= count;
  299. if (ctx->offset == ctx->sg->length) {
  300. ctx->sg = sg_next(ctx->sg);
  301. if (ctx->sg)
  302. ctx->offset = 0;
  303. else
  304. ctx->total = 0;
  305. }
  306. }
  307. return 0;
  308. }
  309. static int omap_sham_xmit_dma_map(struct omap_sham_dev *dd,
  310. struct omap_sham_reqctx *ctx,
  311. size_t length, int final)
  312. {
  313. ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer, ctx->buflen,
  314. DMA_TO_DEVICE);
  315. if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
  316. dev_err(dd->dev, "dma %u bytes error\n", ctx->buflen);
  317. return -EINVAL;
  318. }
  319. ctx->flags &= ~BIT(FLAGS_SG);
  320. /* next call does not fail... so no unmap in the case of error */
  321. return omap_sham_xmit_dma(dd, ctx->dma_addr, length, final);
  322. }
  323. static int omap_sham_update_dma_slow(struct omap_sham_dev *dd)
  324. {
  325. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  326. unsigned int final;
  327. size_t count;
  328. omap_sham_append_sg(ctx);
  329. final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total;
  330. dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: %d, final: %d\n",
  331. ctx->bufcnt, ctx->digcnt, final);
  332. if (final || (ctx->bufcnt == ctx->buflen && ctx->total)) {
  333. count = ctx->bufcnt;
  334. ctx->bufcnt = 0;
  335. return omap_sham_xmit_dma_map(dd, ctx, count, final);
  336. }
  337. return 0;
  338. }
  339. /* Start address alignment */
  340. #define SG_AA(sg) (IS_ALIGNED(sg->offset, sizeof(u32)))
  341. /* SHA1 block size alignment */
  342. #define SG_SA(sg) (IS_ALIGNED(sg->length, SHA1_MD5_BLOCK_SIZE))
  343. static int omap_sham_update_dma_start(struct omap_sham_dev *dd)
  344. {
  345. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  346. unsigned int length, final, tail;
  347. struct scatterlist *sg;
  348. if (!ctx->total)
  349. return 0;
  350. if (ctx->bufcnt || ctx->offset)
  351. return omap_sham_update_dma_slow(dd);
  352. dev_dbg(dd->dev, "fast: digcnt: %d, bufcnt: %u, total: %u\n",
  353. ctx->digcnt, ctx->bufcnt, ctx->total);
  354. sg = ctx->sg;
  355. if (!SG_AA(sg))
  356. return omap_sham_update_dma_slow(dd);
  357. if (!sg_is_last(sg) && !SG_SA(sg))
  358. /* size is not SHA1_BLOCK_SIZE aligned */
  359. return omap_sham_update_dma_slow(dd);
  360. length = min(ctx->total, sg->length);
  361. if (sg_is_last(sg)) {
  362. if (!(ctx->flags & BIT(FLAGS_FINUP))) {
  363. /* not last sg must be SHA1_MD5_BLOCK_SIZE aligned */
  364. tail = length & (SHA1_MD5_BLOCK_SIZE - 1);
  365. /* without finup() we need one block to close hash */
  366. if (!tail)
  367. tail = SHA1_MD5_BLOCK_SIZE;
  368. length -= tail;
  369. }
  370. }
  371. if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
  372. dev_err(dd->dev, "dma_map_sg error\n");
  373. return -EINVAL;
  374. }
  375. ctx->flags |= BIT(FLAGS_SG);
  376. ctx->total -= length;
  377. ctx->offset = length; /* offset where to start slow */
  378. final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total;
  379. /* next call does not fail... so no unmap in the case of error */
  380. return omap_sham_xmit_dma(dd, sg_dma_address(ctx->sg), length, final);
  381. }
  382. static int omap_sham_update_cpu(struct omap_sham_dev *dd)
  383. {
  384. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  385. int bufcnt;
  386. omap_sham_append_sg(ctx);
  387. bufcnt = ctx->bufcnt;
  388. ctx->bufcnt = 0;
  389. return omap_sham_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
  390. }
  391. static int omap_sham_update_dma_stop(struct omap_sham_dev *dd)
  392. {
  393. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  394. omap_stop_dma(dd->dma_lch);
  395. if (ctx->flags & BIT(FLAGS_SG)) {
  396. dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
  397. if (ctx->sg->length == ctx->offset) {
  398. ctx->sg = sg_next(ctx->sg);
  399. if (ctx->sg)
  400. ctx->offset = 0;
  401. }
  402. } else {
  403. dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen,
  404. DMA_TO_DEVICE);
  405. }
  406. return 0;
  407. }
  408. static int omap_sham_init(struct ahash_request *req)
  409. {
  410. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  411. struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
  412. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  413. struct omap_sham_dev *dd = NULL, *tmp;
  414. spin_lock_bh(&sham.lock);
  415. if (!tctx->dd) {
  416. list_for_each_entry(tmp, &sham.dev_list, list) {
  417. dd = tmp;
  418. break;
  419. }
  420. tctx->dd = dd;
  421. } else {
  422. dd = tctx->dd;
  423. }
  424. spin_unlock_bh(&sham.lock);
  425. ctx->dd = dd;
  426. ctx->flags = 0;
  427. dev_dbg(dd->dev, "init: digest size: %d\n",
  428. crypto_ahash_digestsize(tfm));
  429. if (crypto_ahash_digestsize(tfm) == SHA1_DIGEST_SIZE)
  430. ctx->flags |= BIT(FLAGS_SHA1);
  431. ctx->bufcnt = 0;
  432. ctx->digcnt = 0;
  433. ctx->buflen = BUFLEN;
  434. if (tctx->flags & BIT(FLAGS_HMAC)) {
  435. struct omap_sham_hmac_ctx *bctx = tctx->base;
  436. memcpy(ctx->buffer, bctx->ipad, SHA1_MD5_BLOCK_SIZE);
  437. ctx->bufcnt = SHA1_MD5_BLOCK_SIZE;
  438. ctx->flags |= BIT(FLAGS_HMAC);
  439. }
  440. return 0;
  441. }
  442. static int omap_sham_update_req(struct omap_sham_dev *dd)
  443. {
  444. struct ahash_request *req = dd->req;
  445. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  446. int err;
  447. dev_dbg(dd->dev, "update_req: total: %u, digcnt: %d, finup: %d\n",
  448. ctx->total, ctx->digcnt, (ctx->flags & BIT(FLAGS_FINUP)) != 0);
  449. if (ctx->flags & BIT(FLAGS_CPU))
  450. err = omap_sham_update_cpu(dd);
  451. else
  452. err = omap_sham_update_dma_start(dd);
  453. /* wait for dma completion before can take more data */
  454. dev_dbg(dd->dev, "update: err: %d, digcnt: %d\n", err, ctx->digcnt);
  455. return err;
  456. }
  457. static int omap_sham_final_req(struct omap_sham_dev *dd)
  458. {
  459. struct ahash_request *req = dd->req;
  460. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  461. int err = 0, use_dma = 1;
  462. if (ctx->bufcnt <= 64)
  463. /* faster to handle last block with cpu */
  464. use_dma = 0;
  465. if (use_dma)
  466. err = omap_sham_xmit_dma_map(dd, ctx, ctx->bufcnt, 1);
  467. else
  468. err = omap_sham_xmit_cpu(dd, ctx->buffer, ctx->bufcnt, 1);
  469. ctx->bufcnt = 0;
  470. dev_dbg(dd->dev, "final_req: err: %d\n", err);
  471. return err;
  472. }
  473. static int omap_sham_finish_hmac(struct ahash_request *req)
  474. {
  475. struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
  476. struct omap_sham_hmac_ctx *bctx = tctx->base;
  477. int bs = crypto_shash_blocksize(bctx->shash);
  478. int ds = crypto_shash_digestsize(bctx->shash);
  479. struct {
  480. struct shash_desc shash;
  481. char ctx[crypto_shash_descsize(bctx->shash)];
  482. } desc;
  483. desc.shash.tfm = bctx->shash;
  484. desc.shash.flags = 0; /* not CRYPTO_TFM_REQ_MAY_SLEEP */
  485. return crypto_shash_init(&desc.shash) ?:
  486. crypto_shash_update(&desc.shash, bctx->opad, bs) ?:
  487. crypto_shash_finup(&desc.shash, req->result, ds, req->result);
  488. }
  489. static int omap_sham_finish(struct ahash_request *req)
  490. {
  491. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  492. struct omap_sham_dev *dd = ctx->dd;
  493. int err = 0;
  494. if (ctx->digcnt) {
  495. omap_sham_copy_ready_hash(req);
  496. if (ctx->flags & BIT(FLAGS_HMAC))
  497. err = omap_sham_finish_hmac(req);
  498. }
  499. dev_dbg(dd->dev, "digcnt: %d, bufcnt: %d\n", ctx->digcnt, ctx->bufcnt);
  500. return err;
  501. }
  502. static void omap_sham_finish_req(struct ahash_request *req, int err)
  503. {
  504. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  505. struct omap_sham_dev *dd = ctx->dd;
  506. if (!err) {
  507. omap_sham_copy_hash(req, 1);
  508. if (test_bit(FLAGS_FINAL, &dd->flags))
  509. err = omap_sham_finish(req);
  510. } else {
  511. ctx->flags |= BIT(FLAGS_ERROR);
  512. }
  513. /* atomic operation is not needed here */
  514. dd->flags &= ~(BIT(FLAGS_BUSY) | BIT(FLAGS_FINAL) | BIT(FLAGS_CPU) |
  515. BIT(FLAGS_DMA_READY) | BIT(FLAGS_OUTPUT_READY));
  516. clk_disable(dd->iclk);
  517. if (req->base.complete)
  518. req->base.complete(&req->base, err);
  519. /* handle new request */
  520. tasklet_schedule(&dd->done_task);
  521. }
  522. static int omap_sham_handle_queue(struct omap_sham_dev *dd,
  523. struct ahash_request *req)
  524. {
  525. struct crypto_async_request *async_req, *backlog;
  526. struct omap_sham_reqctx *ctx;
  527. unsigned long flags;
  528. int err = 0, ret = 0;
  529. spin_lock_irqsave(&dd->lock, flags);
  530. if (req)
  531. ret = ahash_enqueue_request(&dd->queue, req);
  532. if (test_bit(FLAGS_BUSY, &dd->flags)) {
  533. spin_unlock_irqrestore(&dd->lock, flags);
  534. return ret;
  535. }
  536. backlog = crypto_get_backlog(&dd->queue);
  537. async_req = crypto_dequeue_request(&dd->queue);
  538. if (async_req)
  539. set_bit(FLAGS_BUSY, &dd->flags);
  540. spin_unlock_irqrestore(&dd->lock, flags);
  541. if (!async_req)
  542. return ret;
  543. if (backlog)
  544. backlog->complete(backlog, -EINPROGRESS);
  545. req = ahash_request_cast(async_req);
  546. dd->req = req;
  547. ctx = ahash_request_ctx(req);
  548. dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
  549. ctx->op, req->nbytes);
  550. err = omap_sham_hw_init(dd);
  551. if (err)
  552. goto err1;
  553. omap_set_dma_dest_params(dd->dma_lch, 0,
  554. OMAP_DMA_AMODE_CONSTANT,
  555. dd->phys_base + SHA_REG_DIN(0), 0, 16);
  556. omap_set_dma_dest_burst_mode(dd->dma_lch,
  557. OMAP_DMA_DATA_BURST_16);
  558. omap_set_dma_src_burst_mode(dd->dma_lch,
  559. OMAP_DMA_DATA_BURST_4);
  560. if (ctx->digcnt)
  561. /* request has changed - restore hash */
  562. omap_sham_copy_hash(req, 0);
  563. if (ctx->op == OP_UPDATE) {
  564. err = omap_sham_update_req(dd);
  565. if (err != -EINPROGRESS && (ctx->flags & BIT(FLAGS_FINUP)))
  566. /* no final() after finup() */
  567. err = omap_sham_final_req(dd);
  568. } else if (ctx->op == OP_FINAL) {
  569. err = omap_sham_final_req(dd);
  570. }
  571. err1:
  572. if (err != -EINPROGRESS)
  573. /* done_task will not finish it, so do it here */
  574. omap_sham_finish_req(req, err);
  575. dev_dbg(dd->dev, "exit, err: %d\n", err);
  576. return ret;
  577. }
  578. static int omap_sham_enqueue(struct ahash_request *req, unsigned int op)
  579. {
  580. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  581. struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
  582. struct omap_sham_dev *dd = tctx->dd;
  583. ctx->op = op;
  584. return omap_sham_handle_queue(dd, req);
  585. }
  586. static int omap_sham_update(struct ahash_request *req)
  587. {
  588. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  589. if (!req->nbytes)
  590. return 0;
  591. ctx->total = req->nbytes;
  592. ctx->sg = req->src;
  593. ctx->offset = 0;
  594. if (ctx->flags & BIT(FLAGS_FINUP)) {
  595. if ((ctx->digcnt + ctx->bufcnt + ctx->total) < 9) {
  596. /*
  597. * OMAP HW accel works only with buffers >= 9
  598. * will switch to bypass in final()
  599. * final has the same request and data
  600. */
  601. omap_sham_append_sg(ctx);
  602. return 0;
  603. } else if (ctx->bufcnt + ctx->total <= SHA1_MD5_BLOCK_SIZE) {
  604. /*
  605. * faster to use CPU for short transfers
  606. */
  607. ctx->flags |= BIT(FLAGS_CPU);
  608. }
  609. } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
  610. omap_sham_append_sg(ctx);
  611. return 0;
  612. }
  613. return omap_sham_enqueue(req, OP_UPDATE);
  614. }
  615. static int omap_sham_shash_digest(struct crypto_shash *shash, u32 flags,
  616. const u8 *data, unsigned int len, u8 *out)
  617. {
  618. struct {
  619. struct shash_desc shash;
  620. char ctx[crypto_shash_descsize(shash)];
  621. } desc;
  622. desc.shash.tfm = shash;
  623. desc.shash.flags = flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  624. return crypto_shash_digest(&desc.shash, data, len, out);
  625. }
  626. static int omap_sham_final_shash(struct ahash_request *req)
  627. {
  628. struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
  629. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  630. return omap_sham_shash_digest(tctx->fallback, req->base.flags,
  631. ctx->buffer, ctx->bufcnt, req->result);
  632. }
  633. static int omap_sham_final(struct ahash_request *req)
  634. {
  635. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  636. ctx->flags |= BIT(FLAGS_FINUP);
  637. if (ctx->flags & BIT(FLAGS_ERROR))
  638. return 0; /* uncompleted hash is not needed */
  639. /* OMAP HW accel works only with buffers >= 9 */
  640. /* HMAC is always >= 9 because ipad == block size */
  641. if ((ctx->digcnt + ctx->bufcnt) < 9)
  642. return omap_sham_final_shash(req);
  643. else if (ctx->bufcnt)
  644. return omap_sham_enqueue(req, OP_FINAL);
  645. /* copy ready hash (+ finalize hmac) */
  646. return omap_sham_finish(req);
  647. }
  648. static int omap_sham_finup(struct ahash_request *req)
  649. {
  650. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  651. int err1, err2;
  652. ctx->flags |= BIT(FLAGS_FINUP);
  653. err1 = omap_sham_update(req);
  654. if (err1 == -EINPROGRESS || err1 == -EBUSY)
  655. return err1;
  656. /*
  657. * final() has to be always called to cleanup resources
  658. * even if udpate() failed, except EINPROGRESS
  659. */
  660. err2 = omap_sham_final(req);
  661. return err1 ?: err2;
  662. }
  663. static int omap_sham_digest(struct ahash_request *req)
  664. {
  665. return omap_sham_init(req) ?: omap_sham_finup(req);
  666. }
  667. static int omap_sham_setkey(struct crypto_ahash *tfm, const u8 *key,
  668. unsigned int keylen)
  669. {
  670. struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
  671. struct omap_sham_hmac_ctx *bctx = tctx->base;
  672. int bs = crypto_shash_blocksize(bctx->shash);
  673. int ds = crypto_shash_digestsize(bctx->shash);
  674. int err, i;
  675. err = crypto_shash_setkey(tctx->fallback, key, keylen);
  676. if (err)
  677. return err;
  678. if (keylen > bs) {
  679. err = omap_sham_shash_digest(bctx->shash,
  680. crypto_shash_get_flags(bctx->shash),
  681. key, keylen, bctx->ipad);
  682. if (err)
  683. return err;
  684. keylen = ds;
  685. } else {
  686. memcpy(bctx->ipad, key, keylen);
  687. }
  688. memset(bctx->ipad + keylen, 0, bs - keylen);
  689. memcpy(bctx->opad, bctx->ipad, bs);
  690. for (i = 0; i < bs; i++) {
  691. bctx->ipad[i] ^= 0x36;
  692. bctx->opad[i] ^= 0x5c;
  693. }
  694. return err;
  695. }
  696. static int omap_sham_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base)
  697. {
  698. struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
  699. const char *alg_name = crypto_tfm_alg_name(tfm);
  700. /* Allocate a fallback and abort if it failed. */
  701. tctx->fallback = crypto_alloc_shash(alg_name, 0,
  702. CRYPTO_ALG_NEED_FALLBACK);
  703. if (IS_ERR(tctx->fallback)) {
  704. pr_err("omap-sham: fallback driver '%s' "
  705. "could not be loaded.\n", alg_name);
  706. return PTR_ERR(tctx->fallback);
  707. }
  708. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  709. sizeof(struct omap_sham_reqctx) + BUFLEN);
  710. if (alg_base) {
  711. struct omap_sham_hmac_ctx *bctx = tctx->base;
  712. tctx->flags |= BIT(FLAGS_HMAC);
  713. bctx->shash = crypto_alloc_shash(alg_base, 0,
  714. CRYPTO_ALG_NEED_FALLBACK);
  715. if (IS_ERR(bctx->shash)) {
  716. pr_err("omap-sham: base driver '%s' "
  717. "could not be loaded.\n", alg_base);
  718. crypto_free_shash(tctx->fallback);
  719. return PTR_ERR(bctx->shash);
  720. }
  721. }
  722. return 0;
  723. }
  724. static int omap_sham_cra_init(struct crypto_tfm *tfm)
  725. {
  726. return omap_sham_cra_init_alg(tfm, NULL);
  727. }
  728. static int omap_sham_cra_sha1_init(struct crypto_tfm *tfm)
  729. {
  730. return omap_sham_cra_init_alg(tfm, "sha1");
  731. }
  732. static int omap_sham_cra_md5_init(struct crypto_tfm *tfm)
  733. {
  734. return omap_sham_cra_init_alg(tfm, "md5");
  735. }
  736. static void omap_sham_cra_exit(struct crypto_tfm *tfm)
  737. {
  738. struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
  739. crypto_free_shash(tctx->fallback);
  740. tctx->fallback = NULL;
  741. if (tctx->flags & BIT(FLAGS_HMAC)) {
  742. struct omap_sham_hmac_ctx *bctx = tctx->base;
  743. crypto_free_shash(bctx->shash);
  744. }
  745. }
  746. static struct ahash_alg algs[] = {
  747. {
  748. .init = omap_sham_init,
  749. .update = omap_sham_update,
  750. .final = omap_sham_final,
  751. .finup = omap_sham_finup,
  752. .digest = omap_sham_digest,
  753. .halg.digestsize = SHA1_DIGEST_SIZE,
  754. .halg.base = {
  755. .cra_name = "sha1",
  756. .cra_driver_name = "omap-sha1",
  757. .cra_priority = 100,
  758. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  759. CRYPTO_ALG_KERN_DRIVER_ONLY |
  760. CRYPTO_ALG_ASYNC |
  761. CRYPTO_ALG_NEED_FALLBACK,
  762. .cra_blocksize = SHA1_BLOCK_SIZE,
  763. .cra_ctxsize = sizeof(struct omap_sham_ctx),
  764. .cra_alignmask = 0,
  765. .cra_module = THIS_MODULE,
  766. .cra_init = omap_sham_cra_init,
  767. .cra_exit = omap_sham_cra_exit,
  768. }
  769. },
  770. {
  771. .init = omap_sham_init,
  772. .update = omap_sham_update,
  773. .final = omap_sham_final,
  774. .finup = omap_sham_finup,
  775. .digest = omap_sham_digest,
  776. .halg.digestsize = MD5_DIGEST_SIZE,
  777. .halg.base = {
  778. .cra_name = "md5",
  779. .cra_driver_name = "omap-md5",
  780. .cra_priority = 100,
  781. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  782. CRYPTO_ALG_KERN_DRIVER_ONLY |
  783. CRYPTO_ALG_ASYNC |
  784. CRYPTO_ALG_NEED_FALLBACK,
  785. .cra_blocksize = SHA1_BLOCK_SIZE,
  786. .cra_ctxsize = sizeof(struct omap_sham_ctx),
  787. .cra_alignmask = OMAP_ALIGN_MASK,
  788. .cra_module = THIS_MODULE,
  789. .cra_init = omap_sham_cra_init,
  790. .cra_exit = omap_sham_cra_exit,
  791. }
  792. },
  793. {
  794. .init = omap_sham_init,
  795. .update = omap_sham_update,
  796. .final = omap_sham_final,
  797. .finup = omap_sham_finup,
  798. .digest = omap_sham_digest,
  799. .setkey = omap_sham_setkey,
  800. .halg.digestsize = SHA1_DIGEST_SIZE,
  801. .halg.base = {
  802. .cra_name = "hmac(sha1)",
  803. .cra_driver_name = "omap-hmac-sha1",
  804. .cra_priority = 100,
  805. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  806. CRYPTO_ALG_KERN_DRIVER_ONLY |
  807. CRYPTO_ALG_ASYNC |
  808. CRYPTO_ALG_NEED_FALLBACK,
  809. .cra_blocksize = SHA1_BLOCK_SIZE,
  810. .cra_ctxsize = sizeof(struct omap_sham_ctx) +
  811. sizeof(struct omap_sham_hmac_ctx),
  812. .cra_alignmask = OMAP_ALIGN_MASK,
  813. .cra_module = THIS_MODULE,
  814. .cra_init = omap_sham_cra_sha1_init,
  815. .cra_exit = omap_sham_cra_exit,
  816. }
  817. },
  818. {
  819. .init = omap_sham_init,
  820. .update = omap_sham_update,
  821. .final = omap_sham_final,
  822. .finup = omap_sham_finup,
  823. .digest = omap_sham_digest,
  824. .setkey = omap_sham_setkey,
  825. .halg.digestsize = MD5_DIGEST_SIZE,
  826. .halg.base = {
  827. .cra_name = "hmac(md5)",
  828. .cra_driver_name = "omap-hmac-md5",
  829. .cra_priority = 100,
  830. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  831. CRYPTO_ALG_KERN_DRIVER_ONLY |
  832. CRYPTO_ALG_ASYNC |
  833. CRYPTO_ALG_NEED_FALLBACK,
  834. .cra_blocksize = SHA1_BLOCK_SIZE,
  835. .cra_ctxsize = sizeof(struct omap_sham_ctx) +
  836. sizeof(struct omap_sham_hmac_ctx),
  837. .cra_alignmask = OMAP_ALIGN_MASK,
  838. .cra_module = THIS_MODULE,
  839. .cra_init = omap_sham_cra_md5_init,
  840. .cra_exit = omap_sham_cra_exit,
  841. }
  842. }
  843. };
  844. static void omap_sham_done_task(unsigned long data)
  845. {
  846. struct omap_sham_dev *dd = (struct omap_sham_dev *)data;
  847. int err = 0;
  848. if (!test_bit(FLAGS_BUSY, &dd->flags)) {
  849. omap_sham_handle_queue(dd, NULL);
  850. return;
  851. }
  852. if (test_bit(FLAGS_CPU, &dd->flags)) {
  853. if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags))
  854. goto finish;
  855. } else if (test_bit(FLAGS_DMA_READY, &dd->flags)) {
  856. if (test_and_clear_bit(FLAGS_DMA_ACTIVE, &dd->flags)) {
  857. omap_sham_update_dma_stop(dd);
  858. if (dd->err) {
  859. err = dd->err;
  860. goto finish;
  861. }
  862. }
  863. if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) {
  864. /* hash or semi-hash ready */
  865. clear_bit(FLAGS_DMA_READY, &dd->flags);
  866. err = omap_sham_update_dma_start(dd);
  867. if (err != -EINPROGRESS)
  868. goto finish;
  869. }
  870. }
  871. return;
  872. finish:
  873. dev_dbg(dd->dev, "update done: err: %d\n", err);
  874. /* finish curent request */
  875. omap_sham_finish_req(dd->req, err);
  876. }
  877. static irqreturn_t omap_sham_irq(int irq, void *dev_id)
  878. {
  879. struct omap_sham_dev *dd = dev_id;
  880. if (unlikely(test_bit(FLAGS_FINAL, &dd->flags)))
  881. /* final -> allow device to go to power-saving mode */
  882. omap_sham_write_mask(dd, SHA_REG_CTRL, 0, SHA_REG_CTRL_LENGTH);
  883. omap_sham_write_mask(dd, SHA_REG_CTRL, SHA_REG_CTRL_OUTPUT_READY,
  884. SHA_REG_CTRL_OUTPUT_READY);
  885. omap_sham_read(dd, SHA_REG_CTRL);
  886. if (!test_bit(FLAGS_BUSY, &dd->flags)) {
  887. dev_warn(dd->dev, "Interrupt when no active requests.\n");
  888. return IRQ_HANDLED;
  889. }
  890. set_bit(FLAGS_OUTPUT_READY, &dd->flags);
  891. tasklet_schedule(&dd->done_task);
  892. return IRQ_HANDLED;
  893. }
  894. static void omap_sham_dma_callback(int lch, u16 ch_status, void *data)
  895. {
  896. struct omap_sham_dev *dd = data;
  897. if (ch_status != OMAP_DMA_BLOCK_IRQ) {
  898. pr_err("omap-sham DMA error status: 0x%hx\n", ch_status);
  899. dd->err = -EIO;
  900. clear_bit(FLAGS_INIT, &dd->flags);/* request to re-initialize */
  901. }
  902. set_bit(FLAGS_DMA_READY, &dd->flags);
  903. tasklet_schedule(&dd->done_task);
  904. }
  905. static int omap_sham_dma_init(struct omap_sham_dev *dd)
  906. {
  907. int err;
  908. dd->dma_lch = -1;
  909. err = omap_request_dma(dd->dma, dev_name(dd->dev),
  910. omap_sham_dma_callback, dd, &dd->dma_lch);
  911. if (err) {
  912. dev_err(dd->dev, "Unable to request DMA channel\n");
  913. return err;
  914. }
  915. return 0;
  916. }
  917. static void omap_sham_dma_cleanup(struct omap_sham_dev *dd)
  918. {
  919. if (dd->dma_lch >= 0) {
  920. omap_free_dma(dd->dma_lch);
  921. dd->dma_lch = -1;
  922. }
  923. }
  924. static int omap_sham_probe(struct platform_device *pdev)
  925. {
  926. struct omap_sham_dev *dd;
  927. struct device *dev = &pdev->dev;
  928. struct resource *res;
  929. int err, i, j;
  930. dd = kzalloc(sizeof(struct omap_sham_dev), GFP_KERNEL);
  931. if (dd == NULL) {
  932. dev_err(dev, "unable to alloc data struct.\n");
  933. err = -ENOMEM;
  934. goto data_err;
  935. }
  936. dd->dev = dev;
  937. platform_set_drvdata(pdev, dd);
  938. INIT_LIST_HEAD(&dd->list);
  939. spin_lock_init(&dd->lock);
  940. tasklet_init(&dd->done_task, omap_sham_done_task, (unsigned long)dd);
  941. crypto_init_queue(&dd->queue, OMAP_SHAM_QUEUE_LENGTH);
  942. dd->irq = -1;
  943. /* Get the base address */
  944. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  945. if (!res) {
  946. dev_err(dev, "no MEM resource info\n");
  947. err = -ENODEV;
  948. goto res_err;
  949. }
  950. dd->phys_base = res->start;
  951. /* Get the DMA */
  952. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  953. if (!res) {
  954. dev_err(dev, "no DMA resource info\n");
  955. err = -ENODEV;
  956. goto res_err;
  957. }
  958. dd->dma = res->start;
  959. /* Get the IRQ */
  960. dd->irq = platform_get_irq(pdev, 0);
  961. if (dd->irq < 0) {
  962. dev_err(dev, "no IRQ resource info\n");
  963. err = dd->irq;
  964. goto res_err;
  965. }
  966. err = request_irq(dd->irq, omap_sham_irq,
  967. IRQF_TRIGGER_LOW, dev_name(dev), dd);
  968. if (err) {
  969. dev_err(dev, "unable to request irq.\n");
  970. goto res_err;
  971. }
  972. err = omap_sham_dma_init(dd);
  973. if (err)
  974. goto dma_err;
  975. /* Initializing the clock */
  976. dd->iclk = clk_get(dev, "ick");
  977. if (IS_ERR(dd->iclk)) {
  978. dev_err(dev, "clock intialization failed.\n");
  979. err = PTR_ERR(dd->iclk);
  980. goto clk_err;
  981. }
  982. dd->io_base = ioremap(dd->phys_base, SZ_4K);
  983. if (!dd->io_base) {
  984. dev_err(dev, "can't ioremap\n");
  985. err = -ENOMEM;
  986. goto io_err;
  987. }
  988. clk_enable(dd->iclk);
  989. dev_info(dev, "hw accel on OMAP rev %u.%u\n",
  990. (omap_sham_read(dd, SHA_REG_REV) & SHA_REG_REV_MAJOR) >> 4,
  991. omap_sham_read(dd, SHA_REG_REV) & SHA_REG_REV_MINOR);
  992. clk_disable(dd->iclk);
  993. spin_lock(&sham.lock);
  994. list_add_tail(&dd->list, &sham.dev_list);
  995. spin_unlock(&sham.lock);
  996. for (i = 0; i < ARRAY_SIZE(algs); i++) {
  997. err = crypto_register_ahash(&algs[i]);
  998. if (err)
  999. goto err_algs;
  1000. }
  1001. return 0;
  1002. err_algs:
  1003. for (j = 0; j < i; j++)
  1004. crypto_unregister_ahash(&algs[j]);
  1005. iounmap(dd->io_base);
  1006. io_err:
  1007. clk_put(dd->iclk);
  1008. clk_err:
  1009. omap_sham_dma_cleanup(dd);
  1010. dma_err:
  1011. if (dd->irq >= 0)
  1012. free_irq(dd->irq, dd);
  1013. res_err:
  1014. kfree(dd);
  1015. dd = NULL;
  1016. data_err:
  1017. dev_err(dev, "initialization failed.\n");
  1018. return err;
  1019. }
  1020. static int omap_sham_remove(struct platform_device *pdev)
  1021. {
  1022. static struct omap_sham_dev *dd;
  1023. int i;
  1024. dd = platform_get_drvdata(pdev);
  1025. if (!dd)
  1026. return -ENODEV;
  1027. spin_lock(&sham.lock);
  1028. list_del(&dd->list);
  1029. spin_unlock(&sham.lock);
  1030. for (i = 0; i < ARRAY_SIZE(algs); i++)
  1031. crypto_unregister_ahash(&algs[i]);
  1032. tasklet_kill(&dd->done_task);
  1033. iounmap(dd->io_base);
  1034. clk_put(dd->iclk);
  1035. omap_sham_dma_cleanup(dd);
  1036. if (dd->irq >= 0)
  1037. free_irq(dd->irq, dd);
  1038. kfree(dd);
  1039. dd = NULL;
  1040. return 0;
  1041. }
  1042. static struct platform_driver omap_sham_driver = {
  1043. .probe = omap_sham_probe,
  1044. .remove = omap_sham_remove,
  1045. .driver = {
  1046. .name = "omap-sham",
  1047. .owner = THIS_MODULE,
  1048. },
  1049. };
  1050. static int __init omap_sham_mod_init(void)
  1051. {
  1052. pr_info("loading %s driver\n", "omap-sham");
  1053. return platform_driver_register(&omap_sham_driver);
  1054. }
  1055. static void __exit omap_sham_mod_exit(void)
  1056. {
  1057. platform_driver_unregister(&omap_sham_driver);
  1058. }
  1059. module_init(omap_sham_mod_init);
  1060. module_exit(omap_sham_mod_exit);
  1061. MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
  1062. MODULE_LICENSE("GPL v2");
  1063. MODULE_AUTHOR("Dmitry Kasatkin");