cryp.c 12 KB

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