cryp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /**
  2. * Copyright (C) ST-Ericsson SA 2010
  3. * Author: Shujuan Chen <shujuan.chen@stericsson.com> for ST-Ericsson.
  4. * Author: Jonas Linde <jonas.linde@stericsson.com> for ST-Ericsson.
  5. * Author: Niklas Hernaeus <niklas.hernaeus@stericsson.com> for ST-Ericsson.
  6. * Author: Joakim Bech <joakim.xx.bech@stericsson.com> for ST-Ericsson.
  7. * Author: Berne Hebark <berne.herbark@stericsson.com> for ST-Ericsson.
  8. * License terms: GNU General Public License (GPL) version 2
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include "cryp_p.h"
  14. #include "cryp.h"
  15. /**
  16. * cryp_wait_until_done - wait until the device logic is not busy
  17. */
  18. void cryp_wait_until_done(struct cryp_device_data *device_data)
  19. {
  20. while (cryp_is_logic_busy(device_data))
  21. cpu_relax();
  22. }
  23. /**
  24. * cryp_check - This routine checks Peripheral and PCell Id
  25. * @device_data: Pointer to the device data struct for base address.
  26. */
  27. int cryp_check(struct cryp_device_data *device_data)
  28. {
  29. int peripheralid2 = 0;
  30. if (NULL == device_data)
  31. return -EINVAL;
  32. peripheralid2 = readl_relaxed(&device_data->base->periphId2);
  33. if (peripheralid2 != CRYP_PERIPHERAL_ID2_DB8500)
  34. return -EPERM;
  35. /* Check Peripheral and Pcell Id Register for CRYP */
  36. if ((CRYP_PERIPHERAL_ID0 ==
  37. readl_relaxed(&device_data->base->periphId0))
  38. && (CRYP_PERIPHERAL_ID1 ==
  39. readl_relaxed(&device_data->base->periphId1))
  40. && (CRYP_PERIPHERAL_ID3 ==
  41. readl_relaxed(&device_data->base->periphId3))
  42. && (CRYP_PCELL_ID0 ==
  43. readl_relaxed(&device_data->base->pcellId0))
  44. && (CRYP_PCELL_ID1 ==
  45. readl_relaxed(&device_data->base->pcellId1))
  46. && (CRYP_PCELL_ID2 ==
  47. readl_relaxed(&device_data->base->pcellId2))
  48. && (CRYP_PCELL_ID3 ==
  49. readl_relaxed(&device_data->base->pcellId3))) {
  50. return 0;
  51. }
  52. return -EPERM;
  53. }
  54. /**
  55. * cryp_activity - This routine enables/disable the cryptography function.
  56. * @device_data: Pointer to the device data struct for base address.
  57. * @cryp_crypen: Enable/Disable functionality
  58. */
  59. void cryp_activity(struct cryp_device_data *device_data,
  60. enum cryp_crypen cryp_crypen)
  61. {
  62. CRYP_PUT_BITS(&device_data->base->cr,
  63. cryp_crypen,
  64. CRYP_CR_CRYPEN_POS,
  65. CRYP_CR_CRYPEN_MASK);
  66. }
  67. /**
  68. * cryp_flush_inoutfifo - Resets both the input and the output FIFOs
  69. * @device_data: Pointer to the device data struct for base address.
  70. */
  71. void cryp_flush_inoutfifo(struct cryp_device_data *device_data)
  72. {
  73. /*
  74. * We always need to disble the hardware before trying to flush the
  75. * FIFO. This is something that isn't written in the design
  76. * specification, but we have been informed by the hardware designers
  77. * that this must be done.
  78. */
  79. cryp_activity(device_data, CRYP_CRYPEN_DISABLE);
  80. cryp_wait_until_done(device_data);
  81. CRYP_SET_BITS(&device_data->base->cr, CRYP_CR_FFLUSH_MASK);
  82. /*
  83. * CRYP_SR_INFIFO_READY_MASK is the expected value on the status
  84. * register when starting a new calculation, which means Input FIFO is
  85. * not full and input FIFO is empty.
  86. */
  87. while (readl_relaxed(&device_data->base->sr) !=
  88. CRYP_SR_INFIFO_READY_MASK)
  89. cpu_relax();
  90. }
  91. /**
  92. * cryp_set_configuration - This routine set the cr CRYP IP
  93. * @device_data: Pointer to the device data struct for base address.
  94. * @cryp_config: Pointer to the configuration parameter
  95. * @control_register: The control register to be written later on.
  96. */
  97. int cryp_set_configuration(struct cryp_device_data *device_data,
  98. struct cryp_config *cryp_config,
  99. u32 *control_register)
  100. {
  101. u32 cr_for_kse;
  102. if (NULL == device_data || NULL == cryp_config)
  103. return -EINVAL;
  104. *control_register |= (cryp_config->keysize << CRYP_CR_KEYSIZE_POS);
  105. /* Prepare key for decryption in AES_ECB and AES_CBC mode. */
  106. if ((CRYP_ALGORITHM_DECRYPT == cryp_config->algodir) &&
  107. ((CRYP_ALGO_AES_ECB == cryp_config->algomode) ||
  108. (CRYP_ALGO_AES_CBC == cryp_config->algomode))) {
  109. cr_for_kse = *control_register;
  110. /*
  111. * This seems a bit odd, but it is indeed needed to set this to
  112. * encrypt even though it is a decryption that we are doing. It
  113. * also mentioned in the design spec that you need to do this.
  114. * After the keyprepartion for decrypting is done you should set
  115. * algodir back to decryption, which is done outside this if
  116. * statement.
  117. *
  118. * According to design specification we should set mode ECB
  119. * during key preparation even though we might be running CBC
  120. * when enter this function.
  121. *
  122. * Writing to KSE_ENABLED will drop CRYPEN when key preparation
  123. * is done. Therefore we need to set CRYPEN again outside this
  124. * if statement when running decryption.
  125. */
  126. cr_for_kse |= ((CRYP_ALGORITHM_ENCRYPT << CRYP_CR_ALGODIR_POS) |
  127. (CRYP_ALGO_AES_ECB << CRYP_CR_ALGOMODE_POS) |
  128. (CRYP_CRYPEN_ENABLE << CRYP_CR_CRYPEN_POS) |
  129. (KSE_ENABLED << CRYP_CR_KSE_POS));
  130. writel_relaxed(cr_for_kse, &device_data->base->cr);
  131. cryp_wait_until_done(device_data);
  132. }
  133. *control_register |=
  134. ((cryp_config->algomode << CRYP_CR_ALGOMODE_POS) |
  135. (cryp_config->algodir << CRYP_CR_ALGODIR_POS));
  136. return 0;
  137. }
  138. /**
  139. * cryp_configure_protection - set the protection bits in the CRYP logic.
  140. * @device_data: Pointer to the device data struct for base address.
  141. * @p_protect_config: Pointer to the protection mode and
  142. * secure mode configuration
  143. */
  144. int cryp_configure_protection(struct cryp_device_data *device_data,
  145. struct cryp_protection_config *p_protect_config)
  146. {
  147. if (NULL == p_protect_config)
  148. return -EINVAL;
  149. CRYP_WRITE_BIT(&device_data->base->cr,
  150. (u32) p_protect_config->secure_access,
  151. CRYP_CR_SECURE_MASK);
  152. CRYP_PUT_BITS(&device_data->base->cr,
  153. p_protect_config->privilege_access,
  154. CRYP_CR_PRLG_POS,
  155. CRYP_CR_PRLG_MASK);
  156. return 0;
  157. }
  158. /**
  159. * cryp_is_logic_busy - returns the busy status of the CRYP logic
  160. * @device_data: Pointer to the device data struct for base address.
  161. */
  162. int cryp_is_logic_busy(struct cryp_device_data *device_data)
  163. {
  164. return CRYP_TEST_BITS(&device_data->base->sr,
  165. CRYP_SR_BUSY_MASK);
  166. }
  167. /**
  168. * cryp_configure_for_dma - configures the CRYP IP for DMA operation
  169. * @device_data: Pointer to the device data struct for base address.
  170. * @dma_req: Specifies the DMA request type value.
  171. */
  172. void cryp_configure_for_dma(struct cryp_device_data *device_data,
  173. enum cryp_dma_req_type dma_req)
  174. {
  175. CRYP_SET_BITS(&device_data->base->dmacr,
  176. (u32) dma_req);
  177. }
  178. /**
  179. * cryp_configure_key_values - configures the key values for CRYP operations
  180. * @device_data: Pointer to the device data struct for base address.
  181. * @key_reg_index: Key value index register
  182. * @key_value: The key value struct
  183. */
  184. int cryp_configure_key_values(struct cryp_device_data *device_data,
  185. enum cryp_key_reg_index key_reg_index,
  186. struct cryp_key_value key_value)
  187. {
  188. while (cryp_is_logic_busy(device_data))
  189. cpu_relax();
  190. switch (key_reg_index) {
  191. case CRYP_KEY_REG_1:
  192. writel_relaxed(key_value.key_value_left,
  193. &device_data->base->key_1_l);
  194. writel_relaxed(key_value.key_value_right,
  195. &device_data->base->key_1_r);
  196. break;
  197. case CRYP_KEY_REG_2:
  198. writel_relaxed(key_value.key_value_left,
  199. &device_data->base->key_2_l);
  200. writel_relaxed(key_value.key_value_right,
  201. &device_data->base->key_2_r);
  202. break;
  203. case CRYP_KEY_REG_3:
  204. writel_relaxed(key_value.key_value_left,
  205. &device_data->base->key_3_l);
  206. writel_relaxed(key_value.key_value_right,
  207. &device_data->base->key_3_r);
  208. break;
  209. case CRYP_KEY_REG_4:
  210. writel_relaxed(key_value.key_value_left,
  211. &device_data->base->key_4_l);
  212. writel_relaxed(key_value.key_value_right,
  213. &device_data->base->key_4_r);
  214. break;
  215. default:
  216. return -EINVAL;
  217. }
  218. return 0;
  219. }
  220. /**
  221. * cryp_configure_init_vector - configures the initialization vector register
  222. * @device_data: Pointer to the device data struct for base address.
  223. * @init_vector_index: Specifies the index of the init vector.
  224. * @init_vector_value: Specifies the value for the init vector.
  225. */
  226. int cryp_configure_init_vector(struct cryp_device_data *device_data,
  227. enum cryp_init_vector_index
  228. init_vector_index,
  229. struct cryp_init_vector_value
  230. init_vector_value)
  231. {
  232. while (cryp_is_logic_busy(device_data))
  233. cpu_relax();
  234. switch (init_vector_index) {
  235. case CRYP_INIT_VECTOR_INDEX_0:
  236. writel_relaxed(init_vector_value.init_value_left,
  237. &device_data->base->init_vect_0_l);
  238. writel_relaxed(init_vector_value.init_value_right,
  239. &device_data->base->init_vect_0_r);
  240. break;
  241. case CRYP_INIT_VECTOR_INDEX_1:
  242. writel_relaxed(init_vector_value.init_value_left,
  243. &device_data->base->init_vect_1_l);
  244. writel_relaxed(init_vector_value.init_value_right,
  245. &device_data->base->init_vect_1_r);
  246. break;
  247. default:
  248. return -EINVAL;
  249. }
  250. return 0;
  251. }
  252. /**
  253. * cryp_save_device_context - Store hardware registers and
  254. * other device context parameter
  255. * @device_data: Pointer to the device data struct for base address.
  256. * @ctx: Crypto device context
  257. */
  258. void cryp_save_device_context(struct cryp_device_data *device_data,
  259. struct cryp_device_context *ctx,
  260. int cryp_mode)
  261. {
  262. enum cryp_algo_mode algomode;
  263. struct cryp_register __iomem *src_reg = device_data->base;
  264. struct cryp_config *config =
  265. (struct cryp_config *)device_data->current_ctx;
  266. /*
  267. * Always start by disable the hardware and wait for it to finish the
  268. * ongoing calculations before trying to reprogram it.
  269. */
  270. cryp_activity(device_data, CRYP_CRYPEN_DISABLE);
  271. cryp_wait_until_done(device_data);
  272. if (cryp_mode == CRYP_MODE_DMA)
  273. cryp_configure_for_dma(device_data, CRYP_DMA_DISABLE_BOTH);
  274. if (CRYP_TEST_BITS(&src_reg->sr, CRYP_SR_IFEM_MASK) == 0)
  275. ctx->din = readl_relaxed(&src_reg->din);
  276. ctx->cr = readl_relaxed(&src_reg->cr) & CRYP_CR_CONTEXT_SAVE_MASK;
  277. switch (config->keysize) {
  278. case CRYP_KEY_SIZE_256:
  279. ctx->key_4_l = readl_relaxed(&src_reg->key_4_l);
  280. ctx->key_4_r = readl_relaxed(&src_reg->key_4_r);
  281. case CRYP_KEY_SIZE_192:
  282. ctx->key_3_l = readl_relaxed(&src_reg->key_3_l);
  283. ctx->key_3_r = readl_relaxed(&src_reg->key_3_r);
  284. case CRYP_KEY_SIZE_128:
  285. ctx->key_2_l = readl_relaxed(&src_reg->key_2_l);
  286. ctx->key_2_r = readl_relaxed(&src_reg->key_2_r);
  287. default:
  288. ctx->key_1_l = readl_relaxed(&src_reg->key_1_l);
  289. ctx->key_1_r = readl_relaxed(&src_reg->key_1_r);
  290. }
  291. /* Save IV for CBC mode for both AES and DES. */
  292. algomode = ((ctx->cr & CRYP_CR_ALGOMODE_MASK) >> CRYP_CR_ALGOMODE_POS);
  293. if (algomode == CRYP_ALGO_TDES_CBC ||
  294. algomode == CRYP_ALGO_DES_CBC ||
  295. algomode == CRYP_ALGO_AES_CBC) {
  296. ctx->init_vect_0_l = readl_relaxed(&src_reg->init_vect_0_l);
  297. ctx->init_vect_0_r = readl_relaxed(&src_reg->init_vect_0_r);
  298. ctx->init_vect_1_l = readl_relaxed(&src_reg->init_vect_1_l);
  299. ctx->init_vect_1_r = readl_relaxed(&src_reg->init_vect_1_r);
  300. }
  301. }
  302. /**
  303. * cryp_restore_device_context - Restore hardware registers and
  304. * other device context parameter
  305. * @device_data: Pointer to the device data struct for base address.
  306. * @ctx: Crypto device context
  307. */
  308. void cryp_restore_device_context(struct cryp_device_data *device_data,
  309. struct cryp_device_context *ctx)
  310. {
  311. struct cryp_register __iomem *reg = device_data->base;
  312. struct cryp_config *config =
  313. (struct cryp_config *)device_data->current_ctx;
  314. /*
  315. * Fall through for all items in switch statement. DES is captured in
  316. * the default.
  317. */
  318. switch (config->keysize) {
  319. case CRYP_KEY_SIZE_256:
  320. writel_relaxed(ctx->key_4_l, &reg->key_4_l);
  321. writel_relaxed(ctx->key_4_r, &reg->key_4_r);
  322. case CRYP_KEY_SIZE_192:
  323. writel_relaxed(ctx->key_3_l, &reg->key_3_l);
  324. writel_relaxed(ctx->key_3_r, &reg->key_3_r);
  325. case CRYP_KEY_SIZE_128:
  326. writel_relaxed(ctx->key_2_l, &reg->key_2_l);
  327. writel_relaxed(ctx->key_2_r, &reg->key_2_r);
  328. default:
  329. writel_relaxed(ctx->key_1_l, &reg->key_1_l);
  330. writel_relaxed(ctx->key_1_r, &reg->key_1_r);
  331. }
  332. /* Restore IV for CBC mode for AES and DES. */
  333. if (config->algomode == CRYP_ALGO_TDES_CBC ||
  334. config->algomode == CRYP_ALGO_DES_CBC ||
  335. config->algomode == CRYP_ALGO_AES_CBC) {
  336. writel_relaxed(ctx->init_vect_0_l, &reg->init_vect_0_l);
  337. writel_relaxed(ctx->init_vect_0_r, &reg->init_vect_0_r);
  338. writel_relaxed(ctx->init_vect_1_l, &reg->init_vect_1_l);
  339. writel_relaxed(ctx->init_vect_1_r, &reg->init_vect_1_r);
  340. }
  341. }