mxc-scc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /*
  2. * Copyright (C) 2016 Pengutronix, Steffen Trumtrar <kernel@pengutronix.de>
  3. *
  4. * The driver is based on information gathered from
  5. * drivers/mxc/security/mxc_scc.c which can be found in
  6. * the Freescale linux-2.6-imx.git in the imx_2.6.35_maintain branch.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/crypto.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/irq.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/mutex.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/platform_device.h>
  29. #include <crypto/algapi.h>
  30. #include <crypto/des.h>
  31. /* Secure Memory (SCM) registers */
  32. #define SCC_SCM_RED_START 0x0000
  33. #define SCC_SCM_BLACK_START 0x0004
  34. #define SCC_SCM_LENGTH 0x0008
  35. #define SCC_SCM_CTRL 0x000C
  36. #define SCC_SCM_STATUS 0x0010
  37. #define SCC_SCM_ERROR_STATUS 0x0014
  38. #define SCC_SCM_INTR_CTRL 0x0018
  39. #define SCC_SCM_CFG 0x001C
  40. #define SCC_SCM_INIT_VECTOR_0 0x0020
  41. #define SCC_SCM_INIT_VECTOR_1 0x0024
  42. #define SCC_SCM_RED_MEMORY 0x0400
  43. #define SCC_SCM_BLACK_MEMORY 0x0800
  44. /* Security Monitor (SMN) Registers */
  45. #define SCC_SMN_STATUS 0x1000
  46. #define SCC_SMN_COMMAND 0x1004
  47. #define SCC_SMN_SEQ_START 0x1008
  48. #define SCC_SMN_SEQ_END 0x100C
  49. #define SCC_SMN_SEQ_CHECK 0x1010
  50. #define SCC_SMN_BIT_COUNT 0x1014
  51. #define SCC_SMN_BITBANK_INC_SIZE 0x1018
  52. #define SCC_SMN_BITBANK_DECREMENT 0x101C
  53. #define SCC_SMN_COMPARE_SIZE 0x1020
  54. #define SCC_SMN_PLAINTEXT_CHECK 0x1024
  55. #define SCC_SMN_CIPHERTEXT_CHECK 0x1028
  56. #define SCC_SMN_TIMER_IV 0x102C
  57. #define SCC_SMN_TIMER_CONTROL 0x1030
  58. #define SCC_SMN_DEBUG_DETECT_STAT 0x1034
  59. #define SCC_SMN_TIMER 0x1038
  60. #define SCC_SCM_CTRL_START_CIPHER BIT(2)
  61. #define SCC_SCM_CTRL_CBC_MODE BIT(1)
  62. #define SCC_SCM_CTRL_DECRYPT_MODE BIT(0)
  63. #define SCC_SCM_STATUS_LEN_ERR BIT(12)
  64. #define SCC_SCM_STATUS_SMN_UNBLOCKED BIT(11)
  65. #define SCC_SCM_STATUS_CIPHERING_DONE BIT(10)
  66. #define SCC_SCM_STATUS_ZEROIZING_DONE BIT(9)
  67. #define SCC_SCM_STATUS_INTR_STATUS BIT(8)
  68. #define SCC_SCM_STATUS_SEC_KEY BIT(7)
  69. #define SCC_SCM_STATUS_INTERNAL_ERR BIT(6)
  70. #define SCC_SCM_STATUS_BAD_SEC_KEY BIT(5)
  71. #define SCC_SCM_STATUS_ZEROIZE_FAIL BIT(4)
  72. #define SCC_SCM_STATUS_SMN_BLOCKED BIT(3)
  73. #define SCC_SCM_STATUS_CIPHERING BIT(2)
  74. #define SCC_SCM_STATUS_ZEROIZING BIT(1)
  75. #define SCC_SCM_STATUS_BUSY BIT(0)
  76. #define SCC_SMN_STATUS_STATE_MASK 0x0000001F
  77. #define SCC_SMN_STATE_START 0x0
  78. /* The SMN is zeroizing its RAM during reset */
  79. #define SCC_SMN_STATE_ZEROIZE_RAM 0x5
  80. /* SMN has passed internal checks */
  81. #define SCC_SMN_STATE_HEALTH_CHECK 0x6
  82. /* Fatal Security Violation. SMN is locked, SCM is inoperative. */
  83. #define SCC_SMN_STATE_FAIL 0x9
  84. /* SCC is in secure state. SCM is using secret key. */
  85. #define SCC_SMN_STATE_SECURE 0xA
  86. /* SCC is not secure. SCM is using default key. */
  87. #define SCC_SMN_STATE_NON_SECURE 0xC
  88. #define SCC_SCM_INTR_CTRL_ZEROIZE_MEM BIT(2)
  89. #define SCC_SCM_INTR_CTRL_CLR_INTR BIT(1)
  90. #define SCC_SCM_INTR_CTRL_MASK_INTR BIT(0)
  91. /* Size, in blocks, of Red memory. */
  92. #define SCC_SCM_CFG_BLACK_SIZE_MASK 0x07fe0000
  93. #define SCC_SCM_CFG_BLACK_SIZE_SHIFT 17
  94. /* Size, in blocks, of Black memory. */
  95. #define SCC_SCM_CFG_RED_SIZE_MASK 0x0001ff80
  96. #define SCC_SCM_CFG_RED_SIZE_SHIFT 7
  97. /* Number of bytes per block. */
  98. #define SCC_SCM_CFG_BLOCK_SIZE_MASK 0x0000007f
  99. #define SCC_SMN_COMMAND_TAMPER_LOCK BIT(4)
  100. #define SCC_SMN_COMMAND_CLR_INTR BIT(3)
  101. #define SCC_SMN_COMMAND_CLR_BIT_BANK BIT(2)
  102. #define SCC_SMN_COMMAND_EN_INTR BIT(1)
  103. #define SCC_SMN_COMMAND_SET_SOFTWARE_ALARM BIT(0)
  104. #define SCC_KEY_SLOTS 20
  105. #define SCC_MAX_KEY_SIZE 32
  106. #define SCC_KEY_SLOT_SIZE 32
  107. #define SCC_CRC_CCITT_START 0xFFFF
  108. /*
  109. * Offset into each RAM of the base of the area which is not
  110. * used for Stored Keys.
  111. */
  112. #define SCC_NON_RESERVED_OFFSET (SCC_KEY_SLOTS * SCC_KEY_SLOT_SIZE)
  113. /* Fixed padding for appending to plaintext to fill out a block */
  114. static char scc_block_padding[8] = { 0x80, 0, 0, 0, 0, 0, 0, 0 };
  115. enum mxc_scc_state {
  116. SCC_STATE_OK,
  117. SCC_STATE_UNIMPLEMENTED,
  118. SCC_STATE_FAILED
  119. };
  120. struct mxc_scc {
  121. struct device *dev;
  122. void __iomem *base;
  123. struct clk *clk;
  124. bool hw_busy;
  125. spinlock_t lock;
  126. struct crypto_queue queue;
  127. struct crypto_async_request *req;
  128. int block_size_bytes;
  129. int black_ram_size_blocks;
  130. int memory_size_bytes;
  131. int bytes_remaining;
  132. void __iomem *red_memory;
  133. void __iomem *black_memory;
  134. };
  135. struct mxc_scc_ctx {
  136. struct mxc_scc *scc;
  137. struct scatterlist *sg_src;
  138. size_t src_nents;
  139. struct scatterlist *sg_dst;
  140. size_t dst_nents;
  141. unsigned int offset;
  142. unsigned int size;
  143. unsigned int ctrl;
  144. };
  145. struct mxc_scc_crypto_tmpl {
  146. struct mxc_scc *scc;
  147. struct crypto_alg alg;
  148. };
  149. static int mxc_scc_get_data(struct mxc_scc_ctx *ctx,
  150. struct crypto_async_request *req)
  151. {
  152. struct ablkcipher_request *ablkreq = ablkcipher_request_cast(req);
  153. struct mxc_scc *scc = ctx->scc;
  154. size_t len;
  155. void __iomem *from;
  156. if (ctx->ctrl & SCC_SCM_CTRL_DECRYPT_MODE)
  157. from = scc->red_memory;
  158. else
  159. from = scc->black_memory;
  160. dev_dbg(scc->dev, "pcopy: from 0x%p %zu bytes\n", from,
  161. ctx->dst_nents * 8);
  162. len = sg_pcopy_from_buffer(ablkreq->dst, ctx->dst_nents,
  163. from, ctx->size, ctx->offset);
  164. if (!len) {
  165. dev_err(scc->dev, "pcopy err from 0x%p (len=%zu)\n", from, len);
  166. return -EINVAL;
  167. }
  168. #ifdef DEBUG
  169. print_hex_dump(KERN_ERR,
  170. "red memory@"__stringify(__LINE__)": ",
  171. DUMP_PREFIX_ADDRESS, 16, 4,
  172. scc->red_memory, ctx->size, 1);
  173. print_hex_dump(KERN_ERR,
  174. "black memory@"__stringify(__LINE__)": ",
  175. DUMP_PREFIX_ADDRESS, 16, 4,
  176. scc->black_memory, ctx->size, 1);
  177. #endif
  178. ctx->offset += len;
  179. if (ctx->offset < ablkreq->nbytes)
  180. return -EINPROGRESS;
  181. return 0;
  182. }
  183. static int mxc_scc_ablkcipher_req_init(struct ablkcipher_request *req,
  184. struct mxc_scc_ctx *ctx)
  185. {
  186. struct mxc_scc *scc = ctx->scc;
  187. int nents;
  188. nents = sg_nents_for_len(req->src, req->nbytes);
  189. if (nents < 0) {
  190. dev_err(scc->dev, "Invalid number of src SC");
  191. return nents;
  192. }
  193. ctx->src_nents = nents;
  194. nents = sg_nents_for_len(req->dst, req->nbytes);
  195. if (nents < 0) {
  196. dev_err(scc->dev, "Invalid number of dst SC");
  197. return nents;
  198. }
  199. ctx->dst_nents = nents;
  200. ctx->size = 0;
  201. ctx->offset = 0;
  202. return 0;
  203. }
  204. static int mxc_scc_ablkcipher_req_complete(struct crypto_async_request *req,
  205. struct mxc_scc_ctx *ctx,
  206. int result)
  207. {
  208. struct ablkcipher_request *ablkreq = ablkcipher_request_cast(req);
  209. struct mxc_scc *scc = ctx->scc;
  210. scc->req = NULL;
  211. scc->bytes_remaining = scc->memory_size_bytes;
  212. if (ctx->ctrl & SCC_SCM_CTRL_CBC_MODE)
  213. memcpy(ablkreq->info, scc->base + SCC_SCM_INIT_VECTOR_0,
  214. scc->block_size_bytes);
  215. req->complete(req, result);
  216. scc->hw_busy = false;
  217. return 0;
  218. }
  219. static int mxc_scc_put_data(struct mxc_scc_ctx *ctx,
  220. struct ablkcipher_request *req)
  221. {
  222. u8 padding_buffer[sizeof(u16) + sizeof(scc_block_padding)];
  223. size_t len = min_t(size_t, req->nbytes - ctx->offset,
  224. ctx->scc->bytes_remaining);
  225. unsigned int padding_byte_count = 0;
  226. struct mxc_scc *scc = ctx->scc;
  227. void __iomem *to;
  228. if (ctx->ctrl & SCC_SCM_CTRL_DECRYPT_MODE)
  229. to = scc->black_memory;
  230. else
  231. to = scc->red_memory;
  232. if (ctx->ctrl & SCC_SCM_CTRL_CBC_MODE && req->info)
  233. memcpy(scc->base + SCC_SCM_INIT_VECTOR_0, req->info,
  234. scc->block_size_bytes);
  235. len = sg_pcopy_to_buffer(req->src, ctx->src_nents,
  236. to, len, ctx->offset);
  237. if (!len) {
  238. dev_err(scc->dev, "pcopy err to 0x%p (len=%zu)\n", to, len);
  239. return -EINVAL;
  240. }
  241. ctx->size = len;
  242. #ifdef DEBUG
  243. dev_dbg(scc->dev, "copied %d bytes to 0x%p\n", len, to);
  244. print_hex_dump(KERN_ERR,
  245. "init vector0@"__stringify(__LINE__)": ",
  246. DUMP_PREFIX_ADDRESS, 16, 4,
  247. scc->base + SCC_SCM_INIT_VECTOR_0, scc->block_size_bytes,
  248. 1);
  249. print_hex_dump(KERN_ERR,
  250. "red memory@"__stringify(__LINE__)": ",
  251. DUMP_PREFIX_ADDRESS, 16, 4,
  252. scc->red_memory, ctx->size, 1);
  253. print_hex_dump(KERN_ERR,
  254. "black memory@"__stringify(__LINE__)": ",
  255. DUMP_PREFIX_ADDRESS, 16, 4,
  256. scc->black_memory, ctx->size, 1);
  257. #endif
  258. scc->bytes_remaining -= len;
  259. padding_byte_count = len % scc->block_size_bytes;
  260. if (padding_byte_count) {
  261. memcpy(padding_buffer, scc_block_padding, padding_byte_count);
  262. memcpy(to + len, padding_buffer, padding_byte_count);
  263. ctx->size += padding_byte_count;
  264. }
  265. #ifdef DEBUG
  266. print_hex_dump(KERN_ERR,
  267. "data to encrypt@"__stringify(__LINE__)": ",
  268. DUMP_PREFIX_ADDRESS, 16, 4,
  269. to, ctx->size, 1);
  270. #endif
  271. return 0;
  272. }
  273. static void mxc_scc_ablkcipher_next(struct mxc_scc_ctx *ctx,
  274. struct crypto_async_request *req)
  275. {
  276. struct ablkcipher_request *ablkreq = ablkcipher_request_cast(req);
  277. struct mxc_scc *scc = ctx->scc;
  278. int err;
  279. dev_dbg(scc->dev, "dispatch request (nbytes=%d, src=%p, dst=%p)\n",
  280. ablkreq->nbytes, ablkreq->src, ablkreq->dst);
  281. writel(0, scc->base + SCC_SCM_ERROR_STATUS);
  282. err = mxc_scc_put_data(ctx, ablkreq);
  283. if (err) {
  284. mxc_scc_ablkcipher_req_complete(req, ctx, err);
  285. return;
  286. }
  287. dev_dbg(scc->dev, "Start encryption (0x%x/0x%x)\n",
  288. readl(scc->base + SCC_SCM_RED_START),
  289. readl(scc->base + SCC_SCM_BLACK_START));
  290. /* clear interrupt control registers */
  291. writel(SCC_SCM_INTR_CTRL_CLR_INTR,
  292. scc->base + SCC_SCM_INTR_CTRL);
  293. writel((ctx->size / ctx->scc->block_size_bytes) - 1,
  294. scc->base + SCC_SCM_LENGTH);
  295. dev_dbg(scc->dev, "Process %d block(s) in 0x%p\n",
  296. ctx->size / ctx->scc->block_size_bytes,
  297. (ctx->ctrl & SCC_SCM_CTRL_DECRYPT_MODE) ? scc->black_memory :
  298. scc->red_memory);
  299. writel(ctx->ctrl, scc->base + SCC_SCM_CTRL);
  300. }
  301. static irqreturn_t mxc_scc_int(int irq, void *priv)
  302. {
  303. struct crypto_async_request *req;
  304. struct mxc_scc_ctx *ctx;
  305. struct mxc_scc *scc = priv;
  306. int status;
  307. int ret;
  308. status = readl(scc->base + SCC_SCM_STATUS);
  309. /* clear interrupt control registers */
  310. writel(SCC_SCM_INTR_CTRL_CLR_INTR, scc->base + SCC_SCM_INTR_CTRL);
  311. if (status & SCC_SCM_STATUS_BUSY)
  312. return IRQ_NONE;
  313. req = scc->req;
  314. if (req) {
  315. ctx = crypto_tfm_ctx(req->tfm);
  316. ret = mxc_scc_get_data(ctx, req);
  317. if (ret != -EINPROGRESS)
  318. mxc_scc_ablkcipher_req_complete(req, ctx, ret);
  319. else
  320. mxc_scc_ablkcipher_next(ctx, req);
  321. }
  322. return IRQ_HANDLED;
  323. }
  324. static int mxc_scc_cra_init(struct crypto_tfm *tfm)
  325. {
  326. struct mxc_scc_ctx *ctx = crypto_tfm_ctx(tfm);
  327. struct crypto_alg *alg = tfm->__crt_alg;
  328. struct mxc_scc_crypto_tmpl *algt;
  329. algt = container_of(alg, struct mxc_scc_crypto_tmpl, alg);
  330. ctx->scc = algt->scc;
  331. return 0;
  332. }
  333. static void mxc_scc_dequeue_req_unlocked(struct mxc_scc_ctx *ctx)
  334. {
  335. struct crypto_async_request *req, *backlog;
  336. if (ctx->scc->hw_busy)
  337. return;
  338. spin_lock_bh(&ctx->scc->lock);
  339. backlog = crypto_get_backlog(&ctx->scc->queue);
  340. req = crypto_dequeue_request(&ctx->scc->queue);
  341. ctx->scc->req = req;
  342. ctx->scc->hw_busy = true;
  343. spin_unlock_bh(&ctx->scc->lock);
  344. if (!req)
  345. return;
  346. if (backlog)
  347. backlog->complete(backlog, -EINPROGRESS);
  348. mxc_scc_ablkcipher_next(ctx, req);
  349. }
  350. static int mxc_scc_queue_req(struct mxc_scc_ctx *ctx,
  351. struct crypto_async_request *req)
  352. {
  353. int ret;
  354. spin_lock_bh(&ctx->scc->lock);
  355. ret = crypto_enqueue_request(&ctx->scc->queue, req);
  356. spin_unlock_bh(&ctx->scc->lock);
  357. if (ret != -EINPROGRESS)
  358. return ret;
  359. mxc_scc_dequeue_req_unlocked(ctx);
  360. return -EINPROGRESS;
  361. }
  362. static int mxc_scc_des3_op(struct mxc_scc_ctx *ctx,
  363. struct ablkcipher_request *req)
  364. {
  365. int err;
  366. err = mxc_scc_ablkcipher_req_init(req, ctx);
  367. if (err)
  368. return err;
  369. return mxc_scc_queue_req(ctx, &req->base);
  370. }
  371. static int mxc_scc_ecb_des_encrypt(struct ablkcipher_request *req)
  372. {
  373. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(req);
  374. struct mxc_scc_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  375. ctx->ctrl = SCC_SCM_CTRL_START_CIPHER;
  376. return mxc_scc_des3_op(ctx, req);
  377. }
  378. static int mxc_scc_ecb_des_decrypt(struct ablkcipher_request *req)
  379. {
  380. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(req);
  381. struct mxc_scc_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  382. ctx->ctrl = SCC_SCM_CTRL_START_CIPHER;
  383. ctx->ctrl |= SCC_SCM_CTRL_DECRYPT_MODE;
  384. return mxc_scc_des3_op(ctx, req);
  385. }
  386. static int mxc_scc_cbc_des_encrypt(struct ablkcipher_request *req)
  387. {
  388. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(req);
  389. struct mxc_scc_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  390. ctx->ctrl = SCC_SCM_CTRL_START_CIPHER;
  391. ctx->ctrl |= SCC_SCM_CTRL_CBC_MODE;
  392. return mxc_scc_des3_op(ctx, req);
  393. }
  394. static int mxc_scc_cbc_des_decrypt(struct ablkcipher_request *req)
  395. {
  396. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(req);
  397. struct mxc_scc_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  398. ctx->ctrl = SCC_SCM_CTRL_START_CIPHER;
  399. ctx->ctrl |= SCC_SCM_CTRL_CBC_MODE;
  400. ctx->ctrl |= SCC_SCM_CTRL_DECRYPT_MODE;
  401. return mxc_scc_des3_op(ctx, req);
  402. }
  403. static void mxc_scc_hw_init(struct mxc_scc *scc)
  404. {
  405. int offset;
  406. offset = SCC_NON_RESERVED_OFFSET / scc->block_size_bytes;
  407. /* Fill the RED_START register */
  408. writel(offset, scc->base + SCC_SCM_RED_START);
  409. /* Fill the BLACK_START register */
  410. writel(offset, scc->base + SCC_SCM_BLACK_START);
  411. scc->red_memory = scc->base + SCC_SCM_RED_MEMORY +
  412. SCC_NON_RESERVED_OFFSET;
  413. scc->black_memory = scc->base + SCC_SCM_BLACK_MEMORY +
  414. SCC_NON_RESERVED_OFFSET;
  415. scc->bytes_remaining = scc->memory_size_bytes;
  416. }
  417. static int mxc_scc_get_config(struct mxc_scc *scc)
  418. {
  419. int config;
  420. config = readl(scc->base + SCC_SCM_CFG);
  421. scc->block_size_bytes = config & SCC_SCM_CFG_BLOCK_SIZE_MASK;
  422. scc->black_ram_size_blocks = config & SCC_SCM_CFG_BLACK_SIZE_MASK;
  423. scc->memory_size_bytes = (scc->block_size_bytes *
  424. scc->black_ram_size_blocks) -
  425. SCC_NON_RESERVED_OFFSET;
  426. return 0;
  427. }
  428. static enum mxc_scc_state mxc_scc_get_state(struct mxc_scc *scc)
  429. {
  430. enum mxc_scc_state state;
  431. int status;
  432. status = readl(scc->base + SCC_SMN_STATUS) &
  433. SCC_SMN_STATUS_STATE_MASK;
  434. /* If in Health Check, try to bringup to secure state */
  435. if (status & SCC_SMN_STATE_HEALTH_CHECK) {
  436. /*
  437. * Write a simple algorithm to the Algorithm Sequence
  438. * Checker (ASC)
  439. */
  440. writel(0xaaaa, scc->base + SCC_SMN_SEQ_START);
  441. writel(0x5555, scc->base + SCC_SMN_SEQ_END);
  442. writel(0x5555, scc->base + SCC_SMN_SEQ_CHECK);
  443. status = readl(scc->base + SCC_SMN_STATUS) &
  444. SCC_SMN_STATUS_STATE_MASK;
  445. }
  446. switch (status) {
  447. case SCC_SMN_STATE_NON_SECURE:
  448. case SCC_SMN_STATE_SECURE:
  449. state = SCC_STATE_OK;
  450. break;
  451. case SCC_SMN_STATE_FAIL:
  452. state = SCC_STATE_FAILED;
  453. break;
  454. default:
  455. state = SCC_STATE_UNIMPLEMENTED;
  456. break;
  457. }
  458. return state;
  459. }
  460. static struct mxc_scc_crypto_tmpl scc_ecb_des = {
  461. .alg = {
  462. .cra_name = "ecb(des3_ede)",
  463. .cra_driver_name = "ecb-des3-scc",
  464. .cra_priority = 300,
  465. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER,
  466. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  467. .cra_ctxsize = sizeof(struct mxc_scc_ctx),
  468. .cra_alignmask = 0,
  469. .cra_type = &crypto_ablkcipher_type,
  470. .cra_module = THIS_MODULE,
  471. .cra_init = mxc_scc_cra_init,
  472. .cra_u.ablkcipher = {
  473. .min_keysize = DES3_EDE_KEY_SIZE,
  474. .max_keysize = DES3_EDE_KEY_SIZE,
  475. .encrypt = mxc_scc_ecb_des_encrypt,
  476. .decrypt = mxc_scc_ecb_des_decrypt,
  477. }
  478. }
  479. };
  480. static struct mxc_scc_crypto_tmpl scc_cbc_des = {
  481. .alg = {
  482. .cra_name = "cbc(des3_ede)",
  483. .cra_driver_name = "cbc-des3-scc",
  484. .cra_priority = 300,
  485. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER,
  486. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  487. .cra_ctxsize = sizeof(struct mxc_scc_ctx),
  488. .cra_alignmask = 0,
  489. .cra_type = &crypto_ablkcipher_type,
  490. .cra_module = THIS_MODULE,
  491. .cra_init = mxc_scc_cra_init,
  492. .cra_u.ablkcipher = {
  493. .min_keysize = DES3_EDE_KEY_SIZE,
  494. .max_keysize = DES3_EDE_KEY_SIZE,
  495. .encrypt = mxc_scc_cbc_des_encrypt,
  496. .decrypt = mxc_scc_cbc_des_decrypt,
  497. }
  498. }
  499. };
  500. static struct mxc_scc_crypto_tmpl *scc_crypto_algs[] = {
  501. &scc_ecb_des,
  502. &scc_cbc_des,
  503. };
  504. static int mxc_scc_crypto_register(struct mxc_scc *scc)
  505. {
  506. int i;
  507. int err = 0;
  508. for (i = 0; i < ARRAY_SIZE(scc_crypto_algs); i++) {
  509. scc_crypto_algs[i]->scc = scc;
  510. err = crypto_register_alg(&scc_crypto_algs[i]->alg);
  511. if (err)
  512. goto err_out;
  513. }
  514. return 0;
  515. err_out:
  516. while (--i >= 0)
  517. crypto_unregister_alg(&scc_crypto_algs[i]->alg);
  518. return err;
  519. }
  520. static void mxc_scc_crypto_unregister(void)
  521. {
  522. unsigned int i;
  523. for (i = 0; i < ARRAY_SIZE(scc_crypto_algs); i++)
  524. crypto_unregister_alg(&scc_crypto_algs[i]->alg);
  525. }
  526. static int mxc_scc_probe(struct platform_device *pdev)
  527. {
  528. struct device *dev = &pdev->dev;
  529. struct resource *res;
  530. struct mxc_scc *scc;
  531. enum mxc_scc_state state;
  532. int irq;
  533. int ret;
  534. int i;
  535. scc = devm_kzalloc(dev, sizeof(*scc), GFP_KERNEL);
  536. if (!scc)
  537. return -ENOMEM;
  538. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  539. scc->base = devm_ioremap_resource(dev, res);
  540. if (IS_ERR(scc->base))
  541. return PTR_ERR(scc->base);
  542. scc->clk = devm_clk_get(&pdev->dev, "ipg");
  543. if (IS_ERR(scc->clk)) {
  544. dev_err(dev, "Could not get ipg clock\n");
  545. return PTR_ERR(scc->clk);
  546. }
  547. ret = clk_prepare_enable(scc->clk);
  548. if (ret)
  549. return ret;
  550. /* clear error status register */
  551. writel(0x0, scc->base + SCC_SCM_ERROR_STATUS);
  552. /* clear interrupt control registers */
  553. writel(SCC_SCM_INTR_CTRL_CLR_INTR |
  554. SCC_SCM_INTR_CTRL_MASK_INTR,
  555. scc->base + SCC_SCM_INTR_CTRL);
  556. writel(SCC_SMN_COMMAND_CLR_INTR |
  557. SCC_SMN_COMMAND_EN_INTR,
  558. scc->base + SCC_SMN_COMMAND);
  559. scc->dev = dev;
  560. platform_set_drvdata(pdev, scc);
  561. ret = mxc_scc_get_config(scc);
  562. if (ret)
  563. goto err_out;
  564. state = mxc_scc_get_state(scc);
  565. if (state != SCC_STATE_OK) {
  566. dev_err(dev, "SCC in unusable state %d\n", state);
  567. ret = -EINVAL;
  568. goto err_out;
  569. }
  570. mxc_scc_hw_init(scc);
  571. spin_lock_init(&scc->lock);
  572. /* FIXME: calculate queue from RAM slots */
  573. crypto_init_queue(&scc->queue, 50);
  574. for (i = 0; i < 2; i++) {
  575. irq = platform_get_irq(pdev, i);
  576. if (irq < 0) {
  577. dev_err(dev, "failed to get irq resource: %d\n", irq);
  578. ret = irq;
  579. goto err_out;
  580. }
  581. ret = devm_request_threaded_irq(dev, irq, NULL, mxc_scc_int,
  582. IRQF_ONESHOT, dev_name(dev), scc);
  583. if (ret)
  584. goto err_out;
  585. }
  586. ret = mxc_scc_crypto_register(scc);
  587. if (ret) {
  588. dev_err(dev, "could not register algorithms");
  589. goto err_out;
  590. }
  591. dev_info(dev, "registered successfully.\n");
  592. return 0;
  593. err_out:
  594. clk_disable_unprepare(scc->clk);
  595. return ret;
  596. }
  597. static int mxc_scc_remove(struct platform_device *pdev)
  598. {
  599. struct mxc_scc *scc = platform_get_drvdata(pdev);
  600. mxc_scc_crypto_unregister();
  601. clk_disable_unprepare(scc->clk);
  602. return 0;
  603. }
  604. static const struct of_device_id mxc_scc_dt_ids[] = {
  605. { .compatible = "fsl,imx25-scc", .data = NULL, },
  606. { /* sentinel */ }
  607. };
  608. MODULE_DEVICE_TABLE(of, mxc_scc_dt_ids);
  609. static struct platform_driver mxc_scc_driver = {
  610. .probe = mxc_scc_probe,
  611. .remove = mxc_scc_remove,
  612. .driver = {
  613. .name = "mxc-scc",
  614. .of_match_table = mxc_scc_dt_ids,
  615. },
  616. };
  617. module_platform_driver(mxc_scc_driver);
  618. MODULE_AUTHOR("Steffen Trumtrar <kernel@pengutronix.de>");
  619. MODULE_DESCRIPTION("Freescale i.MX25 SCC Crypto driver");
  620. MODULE_LICENSE("GPL v2");