omap-sham.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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_ASYNC |
  759. CRYPTO_ALG_NEED_FALLBACK,
  760. .cra_blocksize = SHA1_BLOCK_SIZE,
  761. .cra_ctxsize = sizeof(struct omap_sham_ctx),
  762. .cra_alignmask = 0,
  763. .cra_module = THIS_MODULE,
  764. .cra_init = omap_sham_cra_init,
  765. .cra_exit = omap_sham_cra_exit,
  766. }
  767. },
  768. {
  769. .init = omap_sham_init,
  770. .update = omap_sham_update,
  771. .final = omap_sham_final,
  772. .finup = omap_sham_finup,
  773. .digest = omap_sham_digest,
  774. .halg.digestsize = MD5_DIGEST_SIZE,
  775. .halg.base = {
  776. .cra_name = "md5",
  777. .cra_driver_name = "omap-md5",
  778. .cra_priority = 100,
  779. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  780. CRYPTO_ALG_ASYNC |
  781. CRYPTO_ALG_NEED_FALLBACK,
  782. .cra_blocksize = SHA1_BLOCK_SIZE,
  783. .cra_ctxsize = sizeof(struct omap_sham_ctx),
  784. .cra_alignmask = OMAP_ALIGN_MASK,
  785. .cra_module = THIS_MODULE,
  786. .cra_init = omap_sham_cra_init,
  787. .cra_exit = omap_sham_cra_exit,
  788. }
  789. },
  790. {
  791. .init = omap_sham_init,
  792. .update = omap_sham_update,
  793. .final = omap_sham_final,
  794. .finup = omap_sham_finup,
  795. .digest = omap_sham_digest,
  796. .setkey = omap_sham_setkey,
  797. .halg.digestsize = SHA1_DIGEST_SIZE,
  798. .halg.base = {
  799. .cra_name = "hmac(sha1)",
  800. .cra_driver_name = "omap-hmac-sha1",
  801. .cra_priority = 100,
  802. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  803. CRYPTO_ALG_ASYNC |
  804. CRYPTO_ALG_NEED_FALLBACK,
  805. .cra_blocksize = SHA1_BLOCK_SIZE,
  806. .cra_ctxsize = sizeof(struct omap_sham_ctx) +
  807. sizeof(struct omap_sham_hmac_ctx),
  808. .cra_alignmask = OMAP_ALIGN_MASK,
  809. .cra_module = THIS_MODULE,
  810. .cra_init = omap_sham_cra_sha1_init,
  811. .cra_exit = omap_sham_cra_exit,
  812. }
  813. },
  814. {
  815. .init = omap_sham_init,
  816. .update = omap_sham_update,
  817. .final = omap_sham_final,
  818. .finup = omap_sham_finup,
  819. .digest = omap_sham_digest,
  820. .setkey = omap_sham_setkey,
  821. .halg.digestsize = MD5_DIGEST_SIZE,
  822. .halg.base = {
  823. .cra_name = "hmac(md5)",
  824. .cra_driver_name = "omap-hmac-md5",
  825. .cra_priority = 100,
  826. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  827. CRYPTO_ALG_ASYNC |
  828. CRYPTO_ALG_NEED_FALLBACK,
  829. .cra_blocksize = SHA1_BLOCK_SIZE,
  830. .cra_ctxsize = sizeof(struct omap_sham_ctx) +
  831. sizeof(struct omap_sham_hmac_ctx),
  832. .cra_alignmask = OMAP_ALIGN_MASK,
  833. .cra_module = THIS_MODULE,
  834. .cra_init = omap_sham_cra_md5_init,
  835. .cra_exit = omap_sham_cra_exit,
  836. }
  837. }
  838. };
  839. static void omap_sham_done_task(unsigned long data)
  840. {
  841. struct omap_sham_dev *dd = (struct omap_sham_dev *)data;
  842. int err = 0;
  843. if (!test_bit(FLAGS_BUSY, &dd->flags)) {
  844. omap_sham_handle_queue(dd, NULL);
  845. return;
  846. }
  847. if (test_bit(FLAGS_CPU, &dd->flags)) {
  848. if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags))
  849. goto finish;
  850. } else if (test_bit(FLAGS_DMA_READY, &dd->flags)) {
  851. if (test_and_clear_bit(FLAGS_DMA_ACTIVE, &dd->flags)) {
  852. omap_sham_update_dma_stop(dd);
  853. if (dd->err) {
  854. err = dd->err;
  855. goto finish;
  856. }
  857. }
  858. if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) {
  859. /* hash or semi-hash ready */
  860. clear_bit(FLAGS_DMA_READY, &dd->flags);
  861. err = omap_sham_update_dma_start(dd);
  862. if (err != -EINPROGRESS)
  863. goto finish;
  864. }
  865. }
  866. return;
  867. finish:
  868. dev_dbg(dd->dev, "update done: err: %d\n", err);
  869. /* finish curent request */
  870. omap_sham_finish_req(dd->req, err);
  871. }
  872. static irqreturn_t omap_sham_irq(int irq, void *dev_id)
  873. {
  874. struct omap_sham_dev *dd = dev_id;
  875. if (unlikely(test_bit(FLAGS_FINAL, &dd->flags)))
  876. /* final -> allow device to go to power-saving mode */
  877. omap_sham_write_mask(dd, SHA_REG_CTRL, 0, SHA_REG_CTRL_LENGTH);
  878. omap_sham_write_mask(dd, SHA_REG_CTRL, SHA_REG_CTRL_OUTPUT_READY,
  879. SHA_REG_CTRL_OUTPUT_READY);
  880. omap_sham_read(dd, SHA_REG_CTRL);
  881. if (!test_bit(FLAGS_BUSY, &dd->flags)) {
  882. dev_warn(dd->dev, "Interrupt when no active requests.\n");
  883. return IRQ_HANDLED;
  884. }
  885. set_bit(FLAGS_OUTPUT_READY, &dd->flags);
  886. tasklet_schedule(&dd->done_task);
  887. return IRQ_HANDLED;
  888. }
  889. static void omap_sham_dma_callback(int lch, u16 ch_status, void *data)
  890. {
  891. struct omap_sham_dev *dd = data;
  892. if (ch_status != OMAP_DMA_BLOCK_IRQ) {
  893. pr_err("omap-sham DMA error status: 0x%hx\n", ch_status);
  894. dd->err = -EIO;
  895. clear_bit(FLAGS_INIT, &dd->flags);/* request to re-initialize */
  896. }
  897. set_bit(FLAGS_DMA_READY, &dd->flags);
  898. tasklet_schedule(&dd->done_task);
  899. }
  900. static int omap_sham_dma_init(struct omap_sham_dev *dd)
  901. {
  902. int err;
  903. dd->dma_lch = -1;
  904. err = omap_request_dma(dd->dma, dev_name(dd->dev),
  905. omap_sham_dma_callback, dd, &dd->dma_lch);
  906. if (err) {
  907. dev_err(dd->dev, "Unable to request DMA channel\n");
  908. return err;
  909. }
  910. return 0;
  911. }
  912. static void omap_sham_dma_cleanup(struct omap_sham_dev *dd)
  913. {
  914. if (dd->dma_lch >= 0) {
  915. omap_free_dma(dd->dma_lch);
  916. dd->dma_lch = -1;
  917. }
  918. }
  919. static int __devinit omap_sham_probe(struct platform_device *pdev)
  920. {
  921. struct omap_sham_dev *dd;
  922. struct device *dev = &pdev->dev;
  923. struct resource *res;
  924. int err, i, j;
  925. dd = kzalloc(sizeof(struct omap_sham_dev), GFP_KERNEL);
  926. if (dd == NULL) {
  927. dev_err(dev, "unable to alloc data struct.\n");
  928. err = -ENOMEM;
  929. goto data_err;
  930. }
  931. dd->dev = dev;
  932. platform_set_drvdata(pdev, dd);
  933. INIT_LIST_HEAD(&dd->list);
  934. spin_lock_init(&dd->lock);
  935. tasklet_init(&dd->done_task, omap_sham_done_task, (unsigned long)dd);
  936. crypto_init_queue(&dd->queue, OMAP_SHAM_QUEUE_LENGTH);
  937. dd->irq = -1;
  938. /* Get the base address */
  939. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  940. if (!res) {
  941. dev_err(dev, "no MEM resource info\n");
  942. err = -ENODEV;
  943. goto res_err;
  944. }
  945. dd->phys_base = res->start;
  946. /* Get the DMA */
  947. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  948. if (!res) {
  949. dev_err(dev, "no DMA resource info\n");
  950. err = -ENODEV;
  951. goto res_err;
  952. }
  953. dd->dma = res->start;
  954. /* Get the IRQ */
  955. dd->irq = platform_get_irq(pdev, 0);
  956. if (dd->irq < 0) {
  957. dev_err(dev, "no IRQ resource info\n");
  958. err = dd->irq;
  959. goto res_err;
  960. }
  961. err = request_irq(dd->irq, omap_sham_irq,
  962. IRQF_TRIGGER_LOW, dev_name(dev), dd);
  963. if (err) {
  964. dev_err(dev, "unable to request irq.\n");
  965. goto res_err;
  966. }
  967. err = omap_sham_dma_init(dd);
  968. if (err)
  969. goto dma_err;
  970. /* Initializing the clock */
  971. dd->iclk = clk_get(dev, "ick");
  972. if (IS_ERR(dd->iclk)) {
  973. dev_err(dev, "clock intialization failed.\n");
  974. err = PTR_ERR(dd->iclk);
  975. goto clk_err;
  976. }
  977. dd->io_base = ioremap(dd->phys_base, SZ_4K);
  978. if (!dd->io_base) {
  979. dev_err(dev, "can't ioremap\n");
  980. err = -ENOMEM;
  981. goto io_err;
  982. }
  983. clk_enable(dd->iclk);
  984. dev_info(dev, "hw accel on OMAP rev %u.%u\n",
  985. (omap_sham_read(dd, SHA_REG_REV) & SHA_REG_REV_MAJOR) >> 4,
  986. omap_sham_read(dd, SHA_REG_REV) & SHA_REG_REV_MINOR);
  987. clk_disable(dd->iclk);
  988. spin_lock(&sham.lock);
  989. list_add_tail(&dd->list, &sham.dev_list);
  990. spin_unlock(&sham.lock);
  991. for (i = 0; i < ARRAY_SIZE(algs); i++) {
  992. err = crypto_register_ahash(&algs[i]);
  993. if (err)
  994. goto err_algs;
  995. }
  996. return 0;
  997. err_algs:
  998. for (j = 0; j < i; j++)
  999. crypto_unregister_ahash(&algs[j]);
  1000. iounmap(dd->io_base);
  1001. io_err:
  1002. clk_put(dd->iclk);
  1003. clk_err:
  1004. omap_sham_dma_cleanup(dd);
  1005. dma_err:
  1006. if (dd->irq >= 0)
  1007. free_irq(dd->irq, dd);
  1008. res_err:
  1009. kfree(dd);
  1010. dd = NULL;
  1011. data_err:
  1012. dev_err(dev, "initialization failed.\n");
  1013. return err;
  1014. }
  1015. static int __devexit omap_sham_remove(struct platform_device *pdev)
  1016. {
  1017. static struct omap_sham_dev *dd;
  1018. int i;
  1019. dd = platform_get_drvdata(pdev);
  1020. if (!dd)
  1021. return -ENODEV;
  1022. spin_lock(&sham.lock);
  1023. list_del(&dd->list);
  1024. spin_unlock(&sham.lock);
  1025. for (i = 0; i < ARRAY_SIZE(algs); i++)
  1026. crypto_unregister_ahash(&algs[i]);
  1027. tasklet_kill(&dd->done_task);
  1028. iounmap(dd->io_base);
  1029. clk_put(dd->iclk);
  1030. omap_sham_dma_cleanup(dd);
  1031. if (dd->irq >= 0)
  1032. free_irq(dd->irq, dd);
  1033. kfree(dd);
  1034. dd = NULL;
  1035. return 0;
  1036. }
  1037. static struct platform_driver omap_sham_driver = {
  1038. .probe = omap_sham_probe,
  1039. .remove = omap_sham_remove,
  1040. .driver = {
  1041. .name = "omap-sham",
  1042. .owner = THIS_MODULE,
  1043. },
  1044. };
  1045. static int __init omap_sham_mod_init(void)
  1046. {
  1047. pr_info("loading %s driver\n", "omap-sham");
  1048. if (!cpu_class_is_omap2() ||
  1049. (omap_type() != OMAP2_DEVICE_TYPE_SEC &&
  1050. omap_type() != OMAP2_DEVICE_TYPE_EMU)) {
  1051. pr_err("Unsupported cpu\n");
  1052. return -ENODEV;
  1053. }
  1054. return platform_driver_register(&omap_sham_driver);
  1055. }
  1056. static void __exit omap_sham_mod_exit(void)
  1057. {
  1058. platform_driver_unregister(&omap_sham_driver);
  1059. }
  1060. module_init(omap_sham_mod_init);
  1061. module_exit(omap_sham_mod_exit);
  1062. MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
  1063. MODULE_LICENSE("GPL v2");
  1064. MODULE_AUTHOR("Dmitry Kasatkin");