cryp.c 12 KB

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