s5p-sss.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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/clk.h>
  14. #include <linux/crypto.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/err.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/scatterlist.h>
  26. #include <crypto/ctr.h>
  27. #include <crypto/aes.h>
  28. #include <crypto/algapi.h>
  29. #include <crypto/scatterwalk.h>
  30. #define _SBF(s, v) ((v) << (s))
  31. /* Feed control registers */
  32. #define SSS_REG_FCINTSTAT 0x0000
  33. #define SSS_FCINTSTAT_BRDMAINT BIT(3)
  34. #define SSS_FCINTSTAT_BTDMAINT BIT(2)
  35. #define SSS_FCINTSTAT_HRDMAINT BIT(1)
  36. #define SSS_FCINTSTAT_PKDMAINT BIT(0)
  37. #define SSS_REG_FCINTENSET 0x0004
  38. #define SSS_FCINTENSET_BRDMAINTENSET BIT(3)
  39. #define SSS_FCINTENSET_BTDMAINTENSET BIT(2)
  40. #define SSS_FCINTENSET_HRDMAINTENSET BIT(1)
  41. #define SSS_FCINTENSET_PKDMAINTENSET BIT(0)
  42. #define SSS_REG_FCINTENCLR 0x0008
  43. #define SSS_FCINTENCLR_BRDMAINTENCLR BIT(3)
  44. #define SSS_FCINTENCLR_BTDMAINTENCLR BIT(2)
  45. #define SSS_FCINTENCLR_HRDMAINTENCLR BIT(1)
  46. #define SSS_FCINTENCLR_PKDMAINTENCLR BIT(0)
  47. #define SSS_REG_FCINTPEND 0x000C
  48. #define SSS_FCINTPEND_BRDMAINTP BIT(3)
  49. #define SSS_FCINTPEND_BTDMAINTP BIT(2)
  50. #define SSS_FCINTPEND_HRDMAINTP BIT(1)
  51. #define SSS_FCINTPEND_PKDMAINTP BIT(0)
  52. #define SSS_REG_FCFIFOSTAT 0x0010
  53. #define SSS_FCFIFOSTAT_BRFIFOFUL BIT(7)
  54. #define SSS_FCFIFOSTAT_BRFIFOEMP BIT(6)
  55. #define SSS_FCFIFOSTAT_BTFIFOFUL BIT(5)
  56. #define SSS_FCFIFOSTAT_BTFIFOEMP BIT(4)
  57. #define SSS_FCFIFOSTAT_HRFIFOFUL BIT(3)
  58. #define SSS_FCFIFOSTAT_HRFIFOEMP BIT(2)
  59. #define SSS_FCFIFOSTAT_PKFIFOFUL BIT(1)
  60. #define SSS_FCFIFOSTAT_PKFIFOEMP BIT(0)
  61. #define SSS_REG_FCFIFOCTRL 0x0014
  62. #define SSS_FCFIFOCTRL_DESSEL BIT(2)
  63. #define SSS_HASHIN_INDEPENDENT _SBF(0, 0x00)
  64. #define SSS_HASHIN_CIPHER_INPUT _SBF(0, 0x01)
  65. #define SSS_HASHIN_CIPHER_OUTPUT _SBF(0, 0x02)
  66. #define SSS_REG_FCBRDMAS 0x0020
  67. #define SSS_REG_FCBRDMAL 0x0024
  68. #define SSS_REG_FCBRDMAC 0x0028
  69. #define SSS_FCBRDMAC_BYTESWAP BIT(1)
  70. #define SSS_FCBRDMAC_FLUSH BIT(0)
  71. #define SSS_REG_FCBTDMAS 0x0030
  72. #define SSS_REG_FCBTDMAL 0x0034
  73. #define SSS_REG_FCBTDMAC 0x0038
  74. #define SSS_FCBTDMAC_BYTESWAP BIT(1)
  75. #define SSS_FCBTDMAC_FLUSH BIT(0)
  76. #define SSS_REG_FCHRDMAS 0x0040
  77. #define SSS_REG_FCHRDMAL 0x0044
  78. #define SSS_REG_FCHRDMAC 0x0048
  79. #define SSS_FCHRDMAC_BYTESWAP BIT(1)
  80. #define SSS_FCHRDMAC_FLUSH BIT(0)
  81. #define SSS_REG_FCPKDMAS 0x0050
  82. #define SSS_REG_FCPKDMAL 0x0054
  83. #define SSS_REG_FCPKDMAC 0x0058
  84. #define SSS_FCPKDMAC_BYTESWAP BIT(3)
  85. #define SSS_FCPKDMAC_DESCEND BIT(2)
  86. #define SSS_FCPKDMAC_TRANSMIT BIT(1)
  87. #define SSS_FCPKDMAC_FLUSH BIT(0)
  88. #define SSS_REG_FCPKDMAO 0x005C
  89. /* AES registers */
  90. #define SSS_REG_AES_CONTROL 0x00
  91. #define SSS_AES_BYTESWAP_DI BIT(11)
  92. #define SSS_AES_BYTESWAP_DO BIT(10)
  93. #define SSS_AES_BYTESWAP_IV BIT(9)
  94. #define SSS_AES_BYTESWAP_CNT BIT(8)
  95. #define SSS_AES_BYTESWAP_KEY BIT(7)
  96. #define SSS_AES_KEY_CHANGE_MODE BIT(6)
  97. #define SSS_AES_KEY_SIZE_128 _SBF(4, 0x00)
  98. #define SSS_AES_KEY_SIZE_192 _SBF(4, 0x01)
  99. #define SSS_AES_KEY_SIZE_256 _SBF(4, 0x02)
  100. #define SSS_AES_FIFO_MODE BIT(3)
  101. #define SSS_AES_CHAIN_MODE_ECB _SBF(1, 0x00)
  102. #define SSS_AES_CHAIN_MODE_CBC _SBF(1, 0x01)
  103. #define SSS_AES_CHAIN_MODE_CTR _SBF(1, 0x02)
  104. #define SSS_AES_MODE_DECRYPT BIT(0)
  105. #define SSS_REG_AES_STATUS 0x04
  106. #define SSS_AES_BUSY BIT(2)
  107. #define SSS_AES_INPUT_READY BIT(1)
  108. #define SSS_AES_OUTPUT_READY BIT(0)
  109. #define SSS_REG_AES_IN_DATA(s) (0x10 + (s << 2))
  110. #define SSS_REG_AES_OUT_DATA(s) (0x20 + (s << 2))
  111. #define SSS_REG_AES_IV_DATA(s) (0x30 + (s << 2))
  112. #define SSS_REG_AES_CNT_DATA(s) (0x40 + (s << 2))
  113. #define SSS_REG_AES_KEY_DATA(s) (0x80 + (s << 2))
  114. #define SSS_REG(dev, reg) ((dev)->ioaddr + (SSS_REG_##reg))
  115. #define SSS_READ(dev, reg) __raw_readl(SSS_REG(dev, reg))
  116. #define SSS_WRITE(dev, reg, val) __raw_writel((val), SSS_REG(dev, reg))
  117. #define SSS_AES_REG(dev, reg) ((dev)->aes_ioaddr + SSS_REG_##reg)
  118. #define SSS_AES_WRITE(dev, reg, val) __raw_writel((val), \
  119. SSS_AES_REG(dev, reg))
  120. /* HW engine modes */
  121. #define FLAGS_AES_DECRYPT BIT(0)
  122. #define FLAGS_AES_MODE_MASK _SBF(1, 0x03)
  123. #define FLAGS_AES_CBC _SBF(1, 0x01)
  124. #define FLAGS_AES_CTR _SBF(1, 0x02)
  125. #define AES_KEY_LEN 16
  126. #define CRYPTO_QUEUE_LEN 1
  127. /**
  128. * struct samsung_aes_variant - platform specific SSS driver data
  129. * @aes_offset: AES register offset from SSS module's base.
  130. *
  131. * Specifies platform specific configuration of SSS module.
  132. * Note: A structure for driver specific platform data is used for future
  133. * expansion of its usage.
  134. */
  135. struct samsung_aes_variant {
  136. unsigned int aes_offset;
  137. };
  138. struct s5p_aes_reqctx {
  139. unsigned long mode;
  140. };
  141. struct s5p_aes_ctx {
  142. struct s5p_aes_dev *dev;
  143. uint8_t aes_key[AES_MAX_KEY_SIZE];
  144. uint8_t nonce[CTR_RFC3686_NONCE_SIZE];
  145. int keylen;
  146. };
  147. struct s5p_aes_dev {
  148. struct device *dev;
  149. struct clk *clk;
  150. void __iomem *ioaddr;
  151. void __iomem *aes_ioaddr;
  152. int irq_fc;
  153. struct ablkcipher_request *req;
  154. struct s5p_aes_ctx *ctx;
  155. struct scatterlist *sg_src;
  156. struct scatterlist *sg_dst;
  157. /* In case of unaligned access: */
  158. struct scatterlist *sg_src_cpy;
  159. struct scatterlist *sg_dst_cpy;
  160. struct tasklet_struct tasklet;
  161. struct crypto_queue queue;
  162. bool busy;
  163. spinlock_t lock;
  164. struct samsung_aes_variant *variant;
  165. };
  166. static struct s5p_aes_dev *s5p_dev;
  167. static const struct samsung_aes_variant s5p_aes_data = {
  168. .aes_offset = 0x4000,
  169. };
  170. static const struct samsung_aes_variant exynos_aes_data = {
  171. .aes_offset = 0x200,
  172. };
  173. static const struct of_device_id s5p_sss_dt_match[] = {
  174. {
  175. .compatible = "samsung,s5pv210-secss",
  176. .data = &s5p_aes_data,
  177. },
  178. {
  179. .compatible = "samsung,exynos4210-secss",
  180. .data = &exynos_aes_data,
  181. },
  182. { },
  183. };
  184. MODULE_DEVICE_TABLE(of, s5p_sss_dt_match);
  185. static inline struct samsung_aes_variant *find_s5p_sss_version
  186. (struct platform_device *pdev)
  187. {
  188. if (IS_ENABLED(CONFIG_OF) && (pdev->dev.of_node)) {
  189. const struct of_device_id *match;
  190. match = of_match_node(s5p_sss_dt_match,
  191. pdev->dev.of_node);
  192. return (struct samsung_aes_variant *)match->data;
  193. }
  194. return (struct samsung_aes_variant *)
  195. platform_get_device_id(pdev)->driver_data;
  196. }
  197. static void s5p_set_dma_indata(struct s5p_aes_dev *dev, struct scatterlist *sg)
  198. {
  199. SSS_WRITE(dev, FCBRDMAS, sg_dma_address(sg));
  200. SSS_WRITE(dev, FCBRDMAL, sg_dma_len(sg));
  201. }
  202. static void s5p_set_dma_outdata(struct s5p_aes_dev *dev, struct scatterlist *sg)
  203. {
  204. SSS_WRITE(dev, FCBTDMAS, sg_dma_address(sg));
  205. SSS_WRITE(dev, FCBTDMAL, sg_dma_len(sg));
  206. }
  207. static void s5p_free_sg_cpy(struct s5p_aes_dev *dev, struct scatterlist **sg)
  208. {
  209. int len;
  210. if (!*sg)
  211. return;
  212. len = ALIGN(dev->req->nbytes, AES_BLOCK_SIZE);
  213. free_pages((unsigned long)sg_virt(*sg), get_order(len));
  214. kfree(*sg);
  215. *sg = NULL;
  216. }
  217. static void s5p_sg_copy_buf(void *buf, struct scatterlist *sg,
  218. unsigned int nbytes, int out)
  219. {
  220. struct scatter_walk walk;
  221. if (!nbytes)
  222. return;
  223. scatterwalk_start(&walk, sg);
  224. scatterwalk_copychunks(buf, &walk, nbytes, out);
  225. scatterwalk_done(&walk, out, 0);
  226. }
  227. static void s5p_aes_complete(struct s5p_aes_dev *dev, int err)
  228. {
  229. if (dev->sg_dst_cpy) {
  230. dev_dbg(dev->dev,
  231. "Copying %d bytes of output data back to original place\n",
  232. dev->req->nbytes);
  233. s5p_sg_copy_buf(sg_virt(dev->sg_dst_cpy), dev->req->dst,
  234. dev->req->nbytes, 1);
  235. }
  236. s5p_free_sg_cpy(dev, &dev->sg_src_cpy);
  237. s5p_free_sg_cpy(dev, &dev->sg_dst_cpy);
  238. /* holding a lock outside */
  239. dev->req->base.complete(&dev->req->base, err);
  240. dev->busy = false;
  241. }
  242. static void s5p_unset_outdata(struct s5p_aes_dev *dev)
  243. {
  244. dma_unmap_sg(dev->dev, dev->sg_dst, 1, DMA_FROM_DEVICE);
  245. }
  246. static void s5p_unset_indata(struct s5p_aes_dev *dev)
  247. {
  248. dma_unmap_sg(dev->dev, dev->sg_src, 1, DMA_TO_DEVICE);
  249. }
  250. static int s5p_make_sg_cpy(struct s5p_aes_dev *dev, struct scatterlist *src,
  251. struct scatterlist **dst)
  252. {
  253. void *pages;
  254. int len;
  255. *dst = kmalloc(sizeof(**dst), GFP_ATOMIC);
  256. if (!*dst)
  257. return -ENOMEM;
  258. len = ALIGN(dev->req->nbytes, AES_BLOCK_SIZE);
  259. pages = (void *)__get_free_pages(GFP_ATOMIC, get_order(len));
  260. if (!pages) {
  261. kfree(*dst);
  262. *dst = NULL;
  263. return -ENOMEM;
  264. }
  265. s5p_sg_copy_buf(pages, src, dev->req->nbytes, 0);
  266. sg_init_table(*dst, 1);
  267. sg_set_buf(*dst, pages, len);
  268. return 0;
  269. }
  270. static int s5p_set_outdata(struct s5p_aes_dev *dev, struct scatterlist *sg)
  271. {
  272. int err;
  273. if (!sg->length) {
  274. err = -EINVAL;
  275. goto exit;
  276. }
  277. err = dma_map_sg(dev->dev, sg, 1, DMA_FROM_DEVICE);
  278. if (!err) {
  279. err = -ENOMEM;
  280. goto exit;
  281. }
  282. dev->sg_dst = sg;
  283. err = 0;
  284. exit:
  285. return err;
  286. }
  287. static int s5p_set_indata(struct s5p_aes_dev *dev, struct scatterlist *sg)
  288. {
  289. int err;
  290. if (!sg->length) {
  291. err = -EINVAL;
  292. goto exit;
  293. }
  294. err = dma_map_sg(dev->dev, sg, 1, DMA_TO_DEVICE);
  295. if (!err) {
  296. err = -ENOMEM;
  297. goto exit;
  298. }
  299. dev->sg_src = sg;
  300. err = 0;
  301. exit:
  302. return err;
  303. }
  304. /*
  305. * Returns true if new transmitting (output) data is ready and its
  306. * address+length have to be written to device (by calling
  307. * s5p_set_dma_outdata()). False otherwise.
  308. */
  309. static bool s5p_aes_tx(struct s5p_aes_dev *dev)
  310. {
  311. int err = 0;
  312. bool ret = false;
  313. s5p_unset_outdata(dev);
  314. if (!sg_is_last(dev->sg_dst)) {
  315. err = s5p_set_outdata(dev, sg_next(dev->sg_dst));
  316. if (err)
  317. s5p_aes_complete(dev, err);
  318. else
  319. ret = true;
  320. } else {
  321. s5p_aes_complete(dev, err);
  322. dev->busy = true;
  323. tasklet_schedule(&dev->tasklet);
  324. }
  325. return ret;
  326. }
  327. /*
  328. * Returns true if new receiving (input) data is ready and its
  329. * address+length have to be written to device (by calling
  330. * s5p_set_dma_indata()). False otherwise.
  331. */
  332. static bool s5p_aes_rx(struct s5p_aes_dev *dev)
  333. {
  334. int err;
  335. bool ret = false;
  336. s5p_unset_indata(dev);
  337. if (!sg_is_last(dev->sg_src)) {
  338. err = s5p_set_indata(dev, sg_next(dev->sg_src));
  339. if (err)
  340. s5p_aes_complete(dev, err);
  341. else
  342. ret = true;
  343. }
  344. return ret;
  345. }
  346. static irqreturn_t s5p_aes_interrupt(int irq, void *dev_id)
  347. {
  348. struct platform_device *pdev = dev_id;
  349. struct s5p_aes_dev *dev = platform_get_drvdata(pdev);
  350. bool set_dma_tx = false;
  351. bool set_dma_rx = false;
  352. unsigned long flags;
  353. uint32_t status;
  354. spin_lock_irqsave(&dev->lock, flags);
  355. status = SSS_READ(dev, FCINTSTAT);
  356. if (status & SSS_FCINTSTAT_BRDMAINT)
  357. set_dma_rx = s5p_aes_rx(dev);
  358. if (status & SSS_FCINTSTAT_BTDMAINT)
  359. set_dma_tx = s5p_aes_tx(dev);
  360. SSS_WRITE(dev, FCINTPEND, status);
  361. /*
  362. * Writing length of DMA block (either receiving or transmitting)
  363. * will start the operation immediately, so this should be done
  364. * at the end (even after clearing pending interrupts to not miss the
  365. * interrupt).
  366. */
  367. if (set_dma_tx)
  368. s5p_set_dma_outdata(dev, dev->sg_dst);
  369. if (set_dma_rx)
  370. s5p_set_dma_indata(dev, dev->sg_src);
  371. spin_unlock_irqrestore(&dev->lock, flags);
  372. return IRQ_HANDLED;
  373. }
  374. static void s5p_set_aes(struct s5p_aes_dev *dev,
  375. uint8_t *key, uint8_t *iv, unsigned int keylen)
  376. {
  377. void __iomem *keystart;
  378. if (iv)
  379. memcpy_toio(dev->aes_ioaddr + SSS_REG_AES_IV_DATA(0), iv, 0x10);
  380. if (keylen == AES_KEYSIZE_256)
  381. keystart = dev->aes_ioaddr + SSS_REG_AES_KEY_DATA(0);
  382. else if (keylen == AES_KEYSIZE_192)
  383. keystart = dev->aes_ioaddr + SSS_REG_AES_KEY_DATA(2);
  384. else
  385. keystart = dev->aes_ioaddr + SSS_REG_AES_KEY_DATA(4);
  386. memcpy_toio(keystart, key, keylen);
  387. }
  388. static bool s5p_is_sg_aligned(struct scatterlist *sg)
  389. {
  390. while (sg) {
  391. if (!IS_ALIGNED(sg->length, AES_BLOCK_SIZE))
  392. return false;
  393. sg = sg_next(sg);
  394. }
  395. return true;
  396. }
  397. static int s5p_set_indata_start(struct s5p_aes_dev *dev,
  398. struct ablkcipher_request *req)
  399. {
  400. struct scatterlist *sg;
  401. int err;
  402. dev->sg_src_cpy = NULL;
  403. sg = req->src;
  404. if (!s5p_is_sg_aligned(sg)) {
  405. dev_dbg(dev->dev,
  406. "At least one unaligned source scatter list, making a copy\n");
  407. err = s5p_make_sg_cpy(dev, sg, &dev->sg_src_cpy);
  408. if (err)
  409. return err;
  410. sg = dev->sg_src_cpy;
  411. }
  412. err = s5p_set_indata(dev, sg);
  413. if (err) {
  414. s5p_free_sg_cpy(dev, &dev->sg_src_cpy);
  415. return err;
  416. }
  417. return 0;
  418. }
  419. static int s5p_set_outdata_start(struct s5p_aes_dev *dev,
  420. struct ablkcipher_request *req)
  421. {
  422. struct scatterlist *sg;
  423. int err;
  424. dev->sg_dst_cpy = NULL;
  425. sg = req->dst;
  426. if (!s5p_is_sg_aligned(sg)) {
  427. dev_dbg(dev->dev,
  428. "At least one unaligned dest scatter list, making a copy\n");
  429. err = s5p_make_sg_cpy(dev, sg, &dev->sg_dst_cpy);
  430. if (err)
  431. return err;
  432. sg = dev->sg_dst_cpy;
  433. }
  434. err = s5p_set_outdata(dev, sg);
  435. if (err) {
  436. s5p_free_sg_cpy(dev, &dev->sg_dst_cpy);
  437. return err;
  438. }
  439. return 0;
  440. }
  441. static void s5p_aes_crypt_start(struct s5p_aes_dev *dev, unsigned long mode)
  442. {
  443. struct ablkcipher_request *req = dev->req;
  444. uint32_t aes_control;
  445. unsigned long flags;
  446. int err;
  447. u8 *iv;
  448. aes_control = SSS_AES_KEY_CHANGE_MODE;
  449. if (mode & FLAGS_AES_DECRYPT)
  450. aes_control |= SSS_AES_MODE_DECRYPT;
  451. if ((mode & FLAGS_AES_MODE_MASK) == FLAGS_AES_CBC) {
  452. aes_control |= SSS_AES_CHAIN_MODE_CBC;
  453. iv = req->info;
  454. } else if ((mode & FLAGS_AES_MODE_MASK) == FLAGS_AES_CTR) {
  455. aes_control |= SSS_AES_CHAIN_MODE_CTR;
  456. iv = req->info;
  457. } else {
  458. iv = NULL; /* AES_ECB */
  459. }
  460. if (dev->ctx->keylen == AES_KEYSIZE_192)
  461. aes_control |= SSS_AES_KEY_SIZE_192;
  462. else if (dev->ctx->keylen == AES_KEYSIZE_256)
  463. aes_control |= SSS_AES_KEY_SIZE_256;
  464. aes_control |= SSS_AES_FIFO_MODE;
  465. /* as a variant it is possible to use byte swapping on DMA side */
  466. aes_control |= SSS_AES_BYTESWAP_DI
  467. | SSS_AES_BYTESWAP_DO
  468. | SSS_AES_BYTESWAP_IV
  469. | SSS_AES_BYTESWAP_KEY
  470. | SSS_AES_BYTESWAP_CNT;
  471. spin_lock_irqsave(&dev->lock, flags);
  472. SSS_WRITE(dev, FCINTENCLR,
  473. SSS_FCINTENCLR_BTDMAINTENCLR | SSS_FCINTENCLR_BRDMAINTENCLR);
  474. SSS_WRITE(dev, FCFIFOCTRL, 0x00);
  475. err = s5p_set_indata_start(dev, req);
  476. if (err)
  477. goto indata_error;
  478. err = s5p_set_outdata_start(dev, req);
  479. if (err)
  480. goto outdata_error;
  481. SSS_AES_WRITE(dev, AES_CONTROL, aes_control);
  482. s5p_set_aes(dev, dev->ctx->aes_key, iv, dev->ctx->keylen);
  483. s5p_set_dma_indata(dev, dev->sg_src);
  484. s5p_set_dma_outdata(dev, dev->sg_dst);
  485. SSS_WRITE(dev, FCINTENSET,
  486. SSS_FCINTENSET_BTDMAINTENSET | SSS_FCINTENSET_BRDMAINTENSET);
  487. spin_unlock_irqrestore(&dev->lock, flags);
  488. return;
  489. outdata_error:
  490. s5p_unset_indata(dev);
  491. indata_error:
  492. s5p_aes_complete(dev, err);
  493. spin_unlock_irqrestore(&dev->lock, flags);
  494. }
  495. static void s5p_tasklet_cb(unsigned long data)
  496. {
  497. struct s5p_aes_dev *dev = (struct s5p_aes_dev *)data;
  498. struct crypto_async_request *async_req, *backlog;
  499. struct s5p_aes_reqctx *reqctx;
  500. unsigned long flags;
  501. spin_lock_irqsave(&dev->lock, flags);
  502. backlog = crypto_get_backlog(&dev->queue);
  503. async_req = crypto_dequeue_request(&dev->queue);
  504. if (!async_req) {
  505. dev->busy = false;
  506. spin_unlock_irqrestore(&dev->lock, flags);
  507. return;
  508. }
  509. spin_unlock_irqrestore(&dev->lock, flags);
  510. if (backlog)
  511. backlog->complete(backlog, -EINPROGRESS);
  512. dev->req = ablkcipher_request_cast(async_req);
  513. dev->ctx = crypto_tfm_ctx(dev->req->base.tfm);
  514. reqctx = ablkcipher_request_ctx(dev->req);
  515. s5p_aes_crypt_start(dev, reqctx->mode);
  516. }
  517. static int s5p_aes_handle_req(struct s5p_aes_dev *dev,
  518. struct ablkcipher_request *req)
  519. {
  520. unsigned long flags;
  521. int err;
  522. spin_lock_irqsave(&dev->lock, flags);
  523. err = ablkcipher_enqueue_request(&dev->queue, req);
  524. if (dev->busy) {
  525. spin_unlock_irqrestore(&dev->lock, flags);
  526. goto exit;
  527. }
  528. dev->busy = true;
  529. spin_unlock_irqrestore(&dev->lock, flags);
  530. tasklet_schedule(&dev->tasklet);
  531. exit:
  532. return err;
  533. }
  534. static int s5p_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
  535. {
  536. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  537. struct s5p_aes_reqctx *reqctx = ablkcipher_request_ctx(req);
  538. struct s5p_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  539. struct s5p_aes_dev *dev = ctx->dev;
  540. if (!IS_ALIGNED(req->nbytes, AES_BLOCK_SIZE)) {
  541. dev_err(dev->dev, "request size is not exact amount of AES blocks\n");
  542. return -EINVAL;
  543. }
  544. reqctx->mode = mode;
  545. return s5p_aes_handle_req(dev, req);
  546. }
  547. static int s5p_aes_setkey(struct crypto_ablkcipher *cipher,
  548. const uint8_t *key, unsigned int keylen)
  549. {
  550. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  551. struct s5p_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  552. if (keylen != AES_KEYSIZE_128 &&
  553. keylen != AES_KEYSIZE_192 &&
  554. keylen != AES_KEYSIZE_256)
  555. return -EINVAL;
  556. memcpy(ctx->aes_key, key, keylen);
  557. ctx->keylen = keylen;
  558. return 0;
  559. }
  560. static int s5p_aes_ecb_encrypt(struct ablkcipher_request *req)
  561. {
  562. return s5p_aes_crypt(req, 0);
  563. }
  564. static int s5p_aes_ecb_decrypt(struct ablkcipher_request *req)
  565. {
  566. return s5p_aes_crypt(req, FLAGS_AES_DECRYPT);
  567. }
  568. static int s5p_aes_cbc_encrypt(struct ablkcipher_request *req)
  569. {
  570. return s5p_aes_crypt(req, FLAGS_AES_CBC);
  571. }
  572. static int s5p_aes_cbc_decrypt(struct ablkcipher_request *req)
  573. {
  574. return s5p_aes_crypt(req, FLAGS_AES_DECRYPT | FLAGS_AES_CBC);
  575. }
  576. static int s5p_aes_cra_init(struct crypto_tfm *tfm)
  577. {
  578. struct s5p_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  579. ctx->dev = s5p_dev;
  580. tfm->crt_ablkcipher.reqsize = sizeof(struct s5p_aes_reqctx);
  581. return 0;
  582. }
  583. static struct crypto_alg algs[] = {
  584. {
  585. .cra_name = "ecb(aes)",
  586. .cra_driver_name = "ecb-aes-s5p",
  587. .cra_priority = 100,
  588. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  589. CRYPTO_ALG_ASYNC |
  590. CRYPTO_ALG_KERN_DRIVER_ONLY,
  591. .cra_blocksize = AES_BLOCK_SIZE,
  592. .cra_ctxsize = sizeof(struct s5p_aes_ctx),
  593. .cra_alignmask = 0x0f,
  594. .cra_type = &crypto_ablkcipher_type,
  595. .cra_module = THIS_MODULE,
  596. .cra_init = s5p_aes_cra_init,
  597. .cra_u.ablkcipher = {
  598. .min_keysize = AES_MIN_KEY_SIZE,
  599. .max_keysize = AES_MAX_KEY_SIZE,
  600. .setkey = s5p_aes_setkey,
  601. .encrypt = s5p_aes_ecb_encrypt,
  602. .decrypt = s5p_aes_ecb_decrypt,
  603. }
  604. },
  605. {
  606. .cra_name = "cbc(aes)",
  607. .cra_driver_name = "cbc-aes-s5p",
  608. .cra_priority = 100,
  609. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  610. CRYPTO_ALG_ASYNC |
  611. CRYPTO_ALG_KERN_DRIVER_ONLY,
  612. .cra_blocksize = AES_BLOCK_SIZE,
  613. .cra_ctxsize = sizeof(struct s5p_aes_ctx),
  614. .cra_alignmask = 0x0f,
  615. .cra_type = &crypto_ablkcipher_type,
  616. .cra_module = THIS_MODULE,
  617. .cra_init = s5p_aes_cra_init,
  618. .cra_u.ablkcipher = {
  619. .min_keysize = AES_MIN_KEY_SIZE,
  620. .max_keysize = AES_MAX_KEY_SIZE,
  621. .ivsize = AES_BLOCK_SIZE,
  622. .setkey = s5p_aes_setkey,
  623. .encrypt = s5p_aes_cbc_encrypt,
  624. .decrypt = s5p_aes_cbc_decrypt,
  625. }
  626. },
  627. };
  628. static int s5p_aes_probe(struct platform_device *pdev)
  629. {
  630. struct device *dev = &pdev->dev;
  631. int i, j, err = -ENODEV;
  632. struct samsung_aes_variant *variant;
  633. struct s5p_aes_dev *pdata;
  634. struct resource *res;
  635. if (s5p_dev)
  636. return -EEXIST;
  637. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  638. if (!pdata)
  639. return -ENOMEM;
  640. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  641. pdata->ioaddr = devm_ioremap_resource(&pdev->dev, res);
  642. if (IS_ERR(pdata->ioaddr))
  643. return PTR_ERR(pdata->ioaddr);
  644. variant = find_s5p_sss_version(pdev);
  645. pdata->clk = devm_clk_get(dev, "secss");
  646. if (IS_ERR(pdata->clk)) {
  647. dev_err(dev, "failed to find secss clock source\n");
  648. return -ENOENT;
  649. }
  650. err = clk_prepare_enable(pdata->clk);
  651. if (err < 0) {
  652. dev_err(dev, "Enabling SSS clk failed, err %d\n", err);
  653. return err;
  654. }
  655. spin_lock_init(&pdata->lock);
  656. pdata->aes_ioaddr = pdata->ioaddr + variant->aes_offset;
  657. pdata->irq_fc = platform_get_irq(pdev, 0);
  658. if (pdata->irq_fc < 0) {
  659. err = pdata->irq_fc;
  660. dev_warn(dev, "feed control interrupt is not available.\n");
  661. goto err_irq;
  662. }
  663. err = devm_request_threaded_irq(dev, pdata->irq_fc, NULL,
  664. s5p_aes_interrupt, IRQF_ONESHOT,
  665. pdev->name, pdev);
  666. if (err < 0) {
  667. dev_warn(dev, "feed control interrupt is not available.\n");
  668. goto err_irq;
  669. }
  670. pdata->busy = false;
  671. pdata->variant = variant;
  672. pdata->dev = dev;
  673. platform_set_drvdata(pdev, pdata);
  674. s5p_dev = pdata;
  675. tasklet_init(&pdata->tasklet, s5p_tasklet_cb, (unsigned long)pdata);
  676. crypto_init_queue(&pdata->queue, CRYPTO_QUEUE_LEN);
  677. for (i = 0; i < ARRAY_SIZE(algs); i++) {
  678. err = crypto_register_alg(&algs[i]);
  679. if (err)
  680. goto err_algs;
  681. }
  682. dev_info(dev, "s5p-sss driver registered\n");
  683. return 0;
  684. err_algs:
  685. dev_err(dev, "can't register '%s': %d\n", algs[i].cra_name, err);
  686. for (j = 0; j < i; j++)
  687. crypto_unregister_alg(&algs[j]);
  688. tasklet_kill(&pdata->tasklet);
  689. err_irq:
  690. clk_disable_unprepare(pdata->clk);
  691. s5p_dev = NULL;
  692. return err;
  693. }
  694. static int s5p_aes_remove(struct platform_device *pdev)
  695. {
  696. struct s5p_aes_dev *pdata = platform_get_drvdata(pdev);
  697. int i;
  698. if (!pdata)
  699. return -ENODEV;
  700. for (i = 0; i < ARRAY_SIZE(algs); i++)
  701. crypto_unregister_alg(&algs[i]);
  702. tasklet_kill(&pdata->tasklet);
  703. clk_disable_unprepare(pdata->clk);
  704. s5p_dev = NULL;
  705. return 0;
  706. }
  707. static struct platform_driver s5p_aes_crypto = {
  708. .probe = s5p_aes_probe,
  709. .remove = s5p_aes_remove,
  710. .driver = {
  711. .name = "s5p-secss",
  712. .of_match_table = s5p_sss_dt_match,
  713. },
  714. };
  715. module_platform_driver(s5p_aes_crypto);
  716. MODULE_DESCRIPTION("S5PV210 AES hw acceleration support.");
  717. MODULE_LICENSE("GPL v2");
  718. MODULE_AUTHOR("Vladimir Zapolskiy <vzapolskiy@gmail.com>");