omap-sham.c 31 KB

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