s5p-sss.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Support for Samsung S5PV210 HW acceleration.
  5. *
  6. * Copyright (C) 2011 NetUP Inc. All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as published
  10. * by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/errno.h>
  18. #include <linux/kernel.h>
  19. #include <linux/clk.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/io.h>
  24. #include <linux/crypto.h>
  25. #include <linux/interrupt.h>
  26. #include <crypto/algapi.h>
  27. #include <crypto/aes.h>
  28. #include <crypto/ctr.h>
  29. #include <plat/cpu.h>
  30. #include <plat/dma.h>
  31. #define _SBF(s, v) ((v) << (s))
  32. #define _BIT(b) _SBF(b, 1)
  33. /* Feed control registers */
  34. #define SSS_REG_FCINTSTAT 0x0000
  35. #define SSS_FCINTSTAT_BRDMAINT _BIT(3)
  36. #define SSS_FCINTSTAT_BTDMAINT _BIT(2)
  37. #define SSS_FCINTSTAT_HRDMAINT _BIT(1)
  38. #define SSS_FCINTSTAT_PKDMAINT _BIT(0)
  39. #define SSS_REG_FCINTENSET 0x0004
  40. #define SSS_FCINTENSET_BRDMAINTENSET _BIT(3)
  41. #define SSS_FCINTENSET_BTDMAINTENSET _BIT(2)
  42. #define SSS_FCINTENSET_HRDMAINTENSET _BIT(1)
  43. #define SSS_FCINTENSET_PKDMAINTENSET _BIT(0)
  44. #define SSS_REG_FCINTENCLR 0x0008
  45. #define SSS_FCINTENCLR_BRDMAINTENCLR _BIT(3)
  46. #define SSS_FCINTENCLR_BTDMAINTENCLR _BIT(2)
  47. #define SSS_FCINTENCLR_HRDMAINTENCLR _BIT(1)
  48. #define SSS_FCINTENCLR_PKDMAINTENCLR _BIT(0)
  49. #define SSS_REG_FCINTPEND 0x000C
  50. #define SSS_FCINTPEND_BRDMAINTP _BIT(3)
  51. #define SSS_FCINTPEND_BTDMAINTP _BIT(2)
  52. #define SSS_FCINTPEND_HRDMAINTP _BIT(1)
  53. #define SSS_FCINTPEND_PKDMAINTP _BIT(0)
  54. #define SSS_REG_FCFIFOSTAT 0x0010
  55. #define SSS_FCFIFOSTAT_BRFIFOFUL _BIT(7)
  56. #define SSS_FCFIFOSTAT_BRFIFOEMP _BIT(6)
  57. #define SSS_FCFIFOSTAT_BTFIFOFUL _BIT(5)
  58. #define SSS_FCFIFOSTAT_BTFIFOEMP _BIT(4)
  59. #define SSS_FCFIFOSTAT_HRFIFOFUL _BIT(3)
  60. #define SSS_FCFIFOSTAT_HRFIFOEMP _BIT(2)
  61. #define SSS_FCFIFOSTAT_PKFIFOFUL _BIT(1)
  62. #define SSS_FCFIFOSTAT_PKFIFOEMP _BIT(0)
  63. #define SSS_REG_FCFIFOCTRL 0x0014
  64. #define SSS_FCFIFOCTRL_DESSEL _BIT(2)
  65. #define SSS_HASHIN_INDEPENDENT _SBF(0, 0x00)
  66. #define SSS_HASHIN_CIPHER_INPUT _SBF(0, 0x01)
  67. #define SSS_HASHIN_CIPHER_OUTPUT _SBF(0, 0x02)
  68. #define SSS_REG_FCBRDMAS 0x0020
  69. #define SSS_REG_FCBRDMAL 0x0024
  70. #define SSS_REG_FCBRDMAC 0x0028
  71. #define SSS_FCBRDMAC_BYTESWAP _BIT(1)
  72. #define SSS_FCBRDMAC_FLUSH _BIT(0)
  73. #define SSS_REG_FCBTDMAS 0x0030
  74. #define SSS_REG_FCBTDMAL 0x0034
  75. #define SSS_REG_FCBTDMAC 0x0038
  76. #define SSS_FCBTDMAC_BYTESWAP _BIT(1)
  77. #define SSS_FCBTDMAC_FLUSH _BIT(0)
  78. #define SSS_REG_FCHRDMAS 0x0040
  79. #define SSS_REG_FCHRDMAL 0x0044
  80. #define SSS_REG_FCHRDMAC 0x0048
  81. #define SSS_FCHRDMAC_BYTESWAP _BIT(1)
  82. #define SSS_FCHRDMAC_FLUSH _BIT(0)
  83. #define SSS_REG_FCPKDMAS 0x0050
  84. #define SSS_REG_FCPKDMAL 0x0054
  85. #define SSS_REG_FCPKDMAC 0x0058
  86. #define SSS_FCPKDMAC_BYTESWAP _BIT(3)
  87. #define SSS_FCPKDMAC_DESCEND _BIT(2)
  88. #define SSS_FCPKDMAC_TRANSMIT _BIT(1)
  89. #define SSS_FCPKDMAC_FLUSH _BIT(0)
  90. #define SSS_REG_FCPKDMAO 0x005C
  91. /* AES registers */
  92. #define SSS_REG_AES_CONTROL 0x4000
  93. #define SSS_AES_BYTESWAP_DI _BIT(11)
  94. #define SSS_AES_BYTESWAP_DO _BIT(10)
  95. #define SSS_AES_BYTESWAP_IV _BIT(9)
  96. #define SSS_AES_BYTESWAP_CNT _BIT(8)
  97. #define SSS_AES_BYTESWAP_KEY _BIT(7)
  98. #define SSS_AES_KEY_CHANGE_MODE _BIT(6)
  99. #define SSS_AES_KEY_SIZE_128 _SBF(4, 0x00)
  100. #define SSS_AES_KEY_SIZE_192 _SBF(4, 0x01)
  101. #define SSS_AES_KEY_SIZE_256 _SBF(4, 0x02)
  102. #define SSS_AES_FIFO_MODE _BIT(3)
  103. #define SSS_AES_CHAIN_MODE_ECB _SBF(1, 0x00)
  104. #define SSS_AES_CHAIN_MODE_CBC _SBF(1, 0x01)
  105. #define SSS_AES_CHAIN_MODE_CTR _SBF(1, 0x02)
  106. #define SSS_AES_MODE_DECRYPT _BIT(0)
  107. #define SSS_REG_AES_STATUS 0x4004
  108. #define SSS_AES_BUSY _BIT(2)
  109. #define SSS_AES_INPUT_READY _BIT(1)
  110. #define SSS_AES_OUTPUT_READY _BIT(0)
  111. #define SSS_REG_AES_IN_DATA(s) (0x4010 + (s << 2))
  112. #define SSS_REG_AES_OUT_DATA(s) (0x4020 + (s << 2))
  113. #define SSS_REG_AES_IV_DATA(s) (0x4030 + (s << 2))
  114. #define SSS_REG_AES_CNT_DATA(s) (0x4040 + (s << 2))
  115. #define SSS_REG_AES_KEY_DATA(s) (0x4080 + (s << 2))
  116. #define SSS_REG(dev, reg) ((dev)->ioaddr + (SSS_REG_##reg))
  117. #define SSS_READ(dev, reg) __raw_readl(SSS_REG(dev, reg))
  118. #define SSS_WRITE(dev, reg, val) __raw_writel((val), SSS_REG(dev, reg))
  119. /* HW engine modes */
  120. #define FLAGS_AES_DECRYPT _BIT(0)
  121. #define FLAGS_AES_MODE_MASK _SBF(1, 0x03)
  122. #define FLAGS_AES_CBC _SBF(1, 0x01)
  123. #define FLAGS_AES_CTR _SBF(1, 0x02)
  124. #define AES_KEY_LEN 16
  125. #define CRYPTO_QUEUE_LEN 1
  126. struct s5p_aes_reqctx {
  127. unsigned long mode;
  128. };
  129. struct s5p_aes_ctx {
  130. struct s5p_aes_dev *dev;
  131. uint8_t aes_key[AES_MAX_KEY_SIZE];
  132. uint8_t nonce[CTR_RFC3686_NONCE_SIZE];
  133. int keylen;
  134. };
  135. struct s5p_aes_dev {
  136. struct device *dev;
  137. struct clk *clk;
  138. void __iomem *ioaddr;
  139. int irq_hash;
  140. int irq_fc;
  141. struct ablkcipher_request *req;
  142. struct s5p_aes_ctx *ctx;
  143. struct scatterlist *sg_src;
  144. struct scatterlist *sg_dst;
  145. struct tasklet_struct tasklet;
  146. struct crypto_queue queue;
  147. bool busy;
  148. spinlock_t lock;
  149. };
  150. static struct s5p_aes_dev *s5p_dev;
  151. static void s5p_set_dma_indata(struct s5p_aes_dev *dev, struct scatterlist *sg)
  152. {
  153. SSS_WRITE(dev, FCBRDMAS, sg_dma_address(sg));
  154. SSS_WRITE(dev, FCBRDMAL, sg_dma_len(sg));
  155. }
  156. static void s5p_set_dma_outdata(struct s5p_aes_dev *dev, struct scatterlist *sg)
  157. {
  158. SSS_WRITE(dev, FCBTDMAS, sg_dma_address(sg));
  159. SSS_WRITE(dev, FCBTDMAL, sg_dma_len(sg));
  160. }
  161. static void s5p_aes_complete(struct s5p_aes_dev *dev, int err)
  162. {
  163. /* holding a lock outside */
  164. dev->req->base.complete(&dev->req->base, err);
  165. dev->busy = false;
  166. }
  167. static void s5p_unset_outdata(struct s5p_aes_dev *dev)
  168. {
  169. dma_unmap_sg(dev->dev, dev->sg_dst, 1, DMA_FROM_DEVICE);
  170. }
  171. static void s5p_unset_indata(struct s5p_aes_dev *dev)
  172. {
  173. dma_unmap_sg(dev->dev, dev->sg_src, 1, DMA_TO_DEVICE);
  174. }
  175. static int s5p_set_outdata(struct s5p_aes_dev *dev, struct scatterlist *sg)
  176. {
  177. int err;
  178. if (!IS_ALIGNED(sg_dma_len(sg), AES_BLOCK_SIZE)) {
  179. err = -EINVAL;
  180. goto exit;
  181. }
  182. if (!sg_dma_len(sg)) {
  183. err = -EINVAL;
  184. goto exit;
  185. }
  186. err = dma_map_sg(dev->dev, sg, 1, DMA_FROM_DEVICE);
  187. if (!err) {
  188. err = -ENOMEM;
  189. goto exit;
  190. }
  191. dev->sg_dst = sg;
  192. err = 0;
  193. exit:
  194. return err;
  195. }
  196. static int s5p_set_indata(struct s5p_aes_dev *dev, struct scatterlist *sg)
  197. {
  198. int err;
  199. if (!IS_ALIGNED(sg_dma_len(sg), AES_BLOCK_SIZE)) {
  200. err = -EINVAL;
  201. goto exit;
  202. }
  203. if (!sg_dma_len(sg)) {
  204. err = -EINVAL;
  205. goto exit;
  206. }
  207. err = dma_map_sg(dev->dev, sg, 1, DMA_TO_DEVICE);
  208. if (!err) {
  209. err = -ENOMEM;
  210. goto exit;
  211. }
  212. dev->sg_src = sg;
  213. err = 0;
  214. exit:
  215. return err;
  216. }
  217. static void s5p_aes_tx(struct s5p_aes_dev *dev)
  218. {
  219. int err = 0;
  220. s5p_unset_outdata(dev);
  221. if (!sg_is_last(dev->sg_dst)) {
  222. err = s5p_set_outdata(dev, sg_next(dev->sg_dst));
  223. if (err) {
  224. s5p_aes_complete(dev, err);
  225. return;
  226. }
  227. s5p_set_dma_outdata(dev, dev->sg_dst);
  228. } else
  229. s5p_aes_complete(dev, err);
  230. }
  231. static void s5p_aes_rx(struct s5p_aes_dev *dev)
  232. {
  233. int err;
  234. s5p_unset_indata(dev);
  235. if (!sg_is_last(dev->sg_src)) {
  236. err = s5p_set_indata(dev, sg_next(dev->sg_src));
  237. if (err) {
  238. s5p_aes_complete(dev, err);
  239. return;
  240. }
  241. s5p_set_dma_indata(dev, dev->sg_src);
  242. }
  243. }
  244. static irqreturn_t s5p_aes_interrupt(int irq, void *dev_id)
  245. {
  246. struct platform_device *pdev = dev_id;
  247. struct s5p_aes_dev *dev = platform_get_drvdata(pdev);
  248. uint32_t status;
  249. unsigned long flags;
  250. spin_lock_irqsave(&dev->lock, flags);
  251. if (irq == dev->irq_fc) {
  252. status = SSS_READ(dev, FCINTSTAT);
  253. if (status & SSS_FCINTSTAT_BRDMAINT)
  254. s5p_aes_rx(dev);
  255. if (status & SSS_FCINTSTAT_BTDMAINT)
  256. s5p_aes_tx(dev);
  257. SSS_WRITE(dev, FCINTPEND, status);
  258. }
  259. spin_unlock_irqrestore(&dev->lock, flags);
  260. return IRQ_HANDLED;
  261. }
  262. static void s5p_set_aes(struct s5p_aes_dev *dev,
  263. uint8_t *key, uint8_t *iv, unsigned int keylen)
  264. {
  265. void __iomem *keystart;
  266. memcpy(dev->ioaddr + SSS_REG_AES_IV_DATA(0), iv, 0x10);
  267. if (keylen == AES_KEYSIZE_256)
  268. keystart = dev->ioaddr + SSS_REG_AES_KEY_DATA(0);
  269. else if (keylen == AES_KEYSIZE_192)
  270. keystart = dev->ioaddr + SSS_REG_AES_KEY_DATA(2);
  271. else
  272. keystart = dev->ioaddr + SSS_REG_AES_KEY_DATA(4);
  273. memcpy(keystart, key, keylen);
  274. }
  275. static void s5p_aes_crypt_start(struct s5p_aes_dev *dev, unsigned long mode)
  276. {
  277. struct ablkcipher_request *req = dev->req;
  278. uint32_t aes_control;
  279. int err;
  280. unsigned long flags;
  281. aes_control = SSS_AES_KEY_CHANGE_MODE;
  282. if (mode & FLAGS_AES_DECRYPT)
  283. aes_control |= SSS_AES_MODE_DECRYPT;
  284. if ((mode & FLAGS_AES_MODE_MASK) == FLAGS_AES_CBC)
  285. aes_control |= SSS_AES_CHAIN_MODE_CBC;
  286. else if ((mode & FLAGS_AES_MODE_MASK) == FLAGS_AES_CTR)
  287. aes_control |= SSS_AES_CHAIN_MODE_CTR;
  288. if (dev->ctx->keylen == AES_KEYSIZE_192)
  289. aes_control |= SSS_AES_KEY_SIZE_192;
  290. else if (dev->ctx->keylen == AES_KEYSIZE_256)
  291. aes_control |= SSS_AES_KEY_SIZE_256;
  292. aes_control |= SSS_AES_FIFO_MODE;
  293. /* as a variant it is possible to use byte swapping on DMA side */
  294. aes_control |= SSS_AES_BYTESWAP_DI
  295. | SSS_AES_BYTESWAP_DO
  296. | SSS_AES_BYTESWAP_IV
  297. | SSS_AES_BYTESWAP_KEY
  298. | SSS_AES_BYTESWAP_CNT;
  299. spin_lock_irqsave(&dev->lock, flags);
  300. SSS_WRITE(dev, FCINTENCLR,
  301. SSS_FCINTENCLR_BTDMAINTENCLR | SSS_FCINTENCLR_BRDMAINTENCLR);
  302. SSS_WRITE(dev, FCFIFOCTRL, 0x00);
  303. err = s5p_set_indata(dev, req->src);
  304. if (err)
  305. goto indata_error;
  306. err = s5p_set_outdata(dev, req->dst);
  307. if (err)
  308. goto outdata_error;
  309. SSS_WRITE(dev, AES_CONTROL, aes_control);
  310. s5p_set_aes(dev, dev->ctx->aes_key, req->info, dev->ctx->keylen);
  311. s5p_set_dma_indata(dev, req->src);
  312. s5p_set_dma_outdata(dev, req->dst);
  313. SSS_WRITE(dev, FCINTENSET,
  314. SSS_FCINTENSET_BTDMAINTENSET | SSS_FCINTENSET_BRDMAINTENSET);
  315. spin_unlock_irqrestore(&dev->lock, flags);
  316. return;
  317. outdata_error:
  318. s5p_unset_indata(dev);
  319. indata_error:
  320. s5p_aes_complete(dev, err);
  321. spin_unlock_irqrestore(&dev->lock, flags);
  322. }
  323. static void s5p_tasklet_cb(unsigned long data)
  324. {
  325. struct s5p_aes_dev *dev = (struct s5p_aes_dev *)data;
  326. struct crypto_async_request *async_req, *backlog;
  327. struct s5p_aes_reqctx *reqctx;
  328. unsigned long flags;
  329. spin_lock_irqsave(&dev->lock, flags);
  330. backlog = crypto_get_backlog(&dev->queue);
  331. async_req = crypto_dequeue_request(&dev->queue);
  332. spin_unlock_irqrestore(&dev->lock, flags);
  333. if (!async_req)
  334. return;
  335. if (backlog)
  336. backlog->complete(backlog, -EINPROGRESS);
  337. dev->req = ablkcipher_request_cast(async_req);
  338. dev->ctx = crypto_tfm_ctx(dev->req->base.tfm);
  339. reqctx = ablkcipher_request_ctx(dev->req);
  340. s5p_aes_crypt_start(dev, reqctx->mode);
  341. }
  342. static int s5p_aes_handle_req(struct s5p_aes_dev *dev,
  343. struct ablkcipher_request *req)
  344. {
  345. unsigned long flags;
  346. int err;
  347. spin_lock_irqsave(&dev->lock, flags);
  348. if (dev->busy) {
  349. err = -EAGAIN;
  350. spin_unlock_irqrestore(&dev->lock, flags);
  351. goto exit;
  352. }
  353. dev->busy = true;
  354. err = ablkcipher_enqueue_request(&dev->queue, req);
  355. spin_unlock_irqrestore(&dev->lock, flags);
  356. tasklet_schedule(&dev->tasklet);
  357. exit:
  358. return err;
  359. }
  360. static int s5p_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
  361. {
  362. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  363. struct s5p_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  364. struct s5p_aes_reqctx *reqctx = ablkcipher_request_ctx(req);
  365. struct s5p_aes_dev *dev = ctx->dev;
  366. if (!IS_ALIGNED(req->nbytes, AES_BLOCK_SIZE)) {
  367. pr_err("request size is not exact amount of AES blocks\n");
  368. return -EINVAL;
  369. }
  370. reqctx->mode = mode;
  371. return s5p_aes_handle_req(dev, req);
  372. }
  373. static int s5p_aes_setkey(struct crypto_ablkcipher *cipher,
  374. const uint8_t *key, unsigned int keylen)
  375. {
  376. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  377. struct s5p_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  378. if (keylen != AES_KEYSIZE_128 &&
  379. keylen != AES_KEYSIZE_192 &&
  380. keylen != AES_KEYSIZE_256)
  381. return -EINVAL;
  382. memcpy(ctx->aes_key, key, keylen);
  383. ctx->keylen = keylen;
  384. return 0;
  385. }
  386. static int s5p_aes_ecb_encrypt(struct ablkcipher_request *req)
  387. {
  388. return s5p_aes_crypt(req, 0);
  389. }
  390. static int s5p_aes_ecb_decrypt(struct ablkcipher_request *req)
  391. {
  392. return s5p_aes_crypt(req, FLAGS_AES_DECRYPT);
  393. }
  394. static int s5p_aes_cbc_encrypt(struct ablkcipher_request *req)
  395. {
  396. return s5p_aes_crypt(req, FLAGS_AES_CBC);
  397. }
  398. static int s5p_aes_cbc_decrypt(struct ablkcipher_request *req)
  399. {
  400. return s5p_aes_crypt(req, FLAGS_AES_DECRYPT | FLAGS_AES_CBC);
  401. }
  402. static int s5p_aes_cra_init(struct crypto_tfm *tfm)
  403. {
  404. struct s5p_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  405. ctx->dev = s5p_dev;
  406. tfm->crt_ablkcipher.reqsize = sizeof(struct s5p_aes_reqctx);
  407. return 0;
  408. }
  409. static struct crypto_alg algs[] = {
  410. {
  411. .cra_name = "ecb(aes)",
  412. .cra_driver_name = "ecb-aes-s5p",
  413. .cra_priority = 100,
  414. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  415. CRYPTO_ALG_ASYNC,
  416. .cra_blocksize = AES_BLOCK_SIZE,
  417. .cra_ctxsize = sizeof(struct s5p_aes_ctx),
  418. .cra_alignmask = 0x0f,
  419. .cra_type = &crypto_ablkcipher_type,
  420. .cra_module = THIS_MODULE,
  421. .cra_init = s5p_aes_cra_init,
  422. .cra_u.ablkcipher = {
  423. .min_keysize = AES_MIN_KEY_SIZE,
  424. .max_keysize = AES_MAX_KEY_SIZE,
  425. .setkey = s5p_aes_setkey,
  426. .encrypt = s5p_aes_ecb_encrypt,
  427. .decrypt = s5p_aes_ecb_decrypt,
  428. }
  429. },
  430. {
  431. .cra_name = "cbc(aes)",
  432. .cra_driver_name = "cbc-aes-s5p",
  433. .cra_priority = 100,
  434. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  435. CRYPTO_ALG_ASYNC,
  436. .cra_blocksize = AES_BLOCK_SIZE,
  437. .cra_ctxsize = sizeof(struct s5p_aes_ctx),
  438. .cra_alignmask = 0x0f,
  439. .cra_type = &crypto_ablkcipher_type,
  440. .cra_module = THIS_MODULE,
  441. .cra_init = s5p_aes_cra_init,
  442. .cra_u.ablkcipher = {
  443. .min_keysize = AES_MIN_KEY_SIZE,
  444. .max_keysize = AES_MAX_KEY_SIZE,
  445. .ivsize = AES_BLOCK_SIZE,
  446. .setkey = s5p_aes_setkey,
  447. .encrypt = s5p_aes_cbc_encrypt,
  448. .decrypt = s5p_aes_cbc_decrypt,
  449. }
  450. },
  451. };
  452. static int s5p_aes_probe(struct platform_device *pdev)
  453. {
  454. int i, j, err = -ENODEV;
  455. struct s5p_aes_dev *pdata;
  456. struct device *dev = &pdev->dev;
  457. struct resource *res;
  458. if (s5p_dev)
  459. return -EEXIST;
  460. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  461. if (!res)
  462. return -ENODEV;
  463. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  464. if (!pdata)
  465. return -ENOMEM;
  466. if (!devm_request_mem_region(dev, res->start,
  467. resource_size(res), pdev->name))
  468. return -EBUSY;
  469. pdata->clk = clk_get(dev, "secss");
  470. if (IS_ERR(pdata->clk)) {
  471. dev_err(dev, "failed to find secss clock source\n");
  472. return -ENOENT;
  473. }
  474. clk_enable(pdata->clk);
  475. spin_lock_init(&pdata->lock);
  476. pdata->ioaddr = devm_ioremap(dev, res->start,
  477. resource_size(res));
  478. pdata->irq_hash = platform_get_irq_byname(pdev, "hash");
  479. if (pdata->irq_hash < 0) {
  480. err = pdata->irq_hash;
  481. dev_warn(dev, "hash interrupt is not available.\n");
  482. goto err_irq;
  483. }
  484. err = devm_request_irq(dev, pdata->irq_hash, s5p_aes_interrupt,
  485. IRQF_SHARED, pdev->name, pdev);
  486. if (err < 0) {
  487. dev_warn(dev, "hash interrupt is not available.\n");
  488. goto err_irq;
  489. }
  490. pdata->irq_fc = platform_get_irq_byname(pdev, "feed control");
  491. if (pdata->irq_fc < 0) {
  492. err = pdata->irq_fc;
  493. dev_warn(dev, "feed control interrupt is not available.\n");
  494. goto err_irq;
  495. }
  496. err = devm_request_irq(dev, pdata->irq_fc, s5p_aes_interrupt,
  497. IRQF_SHARED, pdev->name, pdev);
  498. if (err < 0) {
  499. dev_warn(dev, "feed control interrupt is not available.\n");
  500. goto err_irq;
  501. }
  502. pdata->dev = dev;
  503. platform_set_drvdata(pdev, pdata);
  504. s5p_dev = pdata;
  505. tasklet_init(&pdata->tasklet, s5p_tasklet_cb, (unsigned long)pdata);
  506. crypto_init_queue(&pdata->queue, CRYPTO_QUEUE_LEN);
  507. for (i = 0; i < ARRAY_SIZE(algs); i++) {
  508. INIT_LIST_HEAD(&algs[i].cra_list);
  509. err = crypto_register_alg(&algs[i]);
  510. if (err)
  511. goto err_algs;
  512. }
  513. pr_info("s5p-sss driver registered\n");
  514. return 0;
  515. err_algs:
  516. dev_err(dev, "can't register '%s': %d\n", algs[i].cra_name, err);
  517. for (j = 0; j < i; j++)
  518. crypto_unregister_alg(&algs[j]);
  519. tasklet_kill(&pdata->tasklet);
  520. err_irq:
  521. clk_disable(pdata->clk);
  522. clk_put(pdata->clk);
  523. s5p_dev = NULL;
  524. platform_set_drvdata(pdev, NULL);
  525. return err;
  526. }
  527. static int s5p_aes_remove(struct platform_device *pdev)
  528. {
  529. struct s5p_aes_dev *pdata = platform_get_drvdata(pdev);
  530. int i;
  531. if (!pdata)
  532. return -ENODEV;
  533. for (i = 0; i < ARRAY_SIZE(algs); i++)
  534. crypto_unregister_alg(&algs[i]);
  535. tasklet_kill(&pdata->tasklet);
  536. clk_disable(pdata->clk);
  537. clk_put(pdata->clk);
  538. s5p_dev = NULL;
  539. platform_set_drvdata(pdev, NULL);
  540. return 0;
  541. }
  542. static struct platform_driver s5p_aes_crypto = {
  543. .probe = s5p_aes_probe,
  544. .remove = s5p_aes_remove,
  545. .driver = {
  546. .owner = THIS_MODULE,
  547. .name = "s5p-secss",
  548. },
  549. };
  550. static int __init s5p_aes_mod_init(void)
  551. {
  552. return platform_driver_register(&s5p_aes_crypto);
  553. }
  554. static void __exit s5p_aes_mod_exit(void)
  555. {
  556. platform_driver_unregister(&s5p_aes_crypto);
  557. }
  558. module_init(s5p_aes_mod_init);
  559. module_exit(s5p_aes_mod_exit);
  560. MODULE_DESCRIPTION("S5PV210 AES hw acceleration support.");
  561. MODULE_LICENSE("GPL v2");
  562. MODULE_AUTHOR("Vladimir Zapolskiy <vzapolskiy@gmail.com>");