cryp.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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: Joakim Bech <joakim.xx.bech@stericsson.com> for ST-Ericsson.
  6. * Author: Berne Hebark <berne.herbark@stericsson.com> for ST-Ericsson.
  7. * Author: Niklas Hernaeus <niklas.hernaeus@stericsson.com> for ST-Ericsson.
  8. * License terms: GNU General Public License (GPL) version 2
  9. */
  10. #ifndef _CRYP_H_
  11. #define _CRYP_H_
  12. #include <linux/completion.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/klist.h>
  15. #include <linux/mutex.h>
  16. #define DEV_DBG_NAME "crypX crypX:"
  17. /* CRYP enable/disable */
  18. enum cryp_crypen {
  19. CRYP_CRYPEN_DISABLE = 0,
  20. CRYP_CRYPEN_ENABLE = 1
  21. };
  22. /* CRYP Start Computation enable/disable */
  23. enum cryp_start {
  24. CRYP_START_DISABLE = 0,
  25. CRYP_START_ENABLE = 1
  26. };
  27. /* CRYP Init Signal enable/disable */
  28. enum cryp_init {
  29. CRYP_INIT_DISABLE = 0,
  30. CRYP_INIT_ENABLE = 1
  31. };
  32. /* Cryp State enable/disable */
  33. enum cryp_state {
  34. CRYP_STATE_DISABLE = 0,
  35. CRYP_STATE_ENABLE = 1
  36. };
  37. /* Key preparation bit enable */
  38. enum cryp_key_prep {
  39. KSE_DISABLED = 0,
  40. KSE_ENABLED = 1
  41. };
  42. /* Key size for AES */
  43. #define CRYP_KEY_SIZE_128 (0)
  44. #define CRYP_KEY_SIZE_192 (1)
  45. #define CRYP_KEY_SIZE_256 (2)
  46. /* AES modes */
  47. enum cryp_algo_mode {
  48. CRYP_ALGO_TDES_ECB,
  49. CRYP_ALGO_TDES_CBC,
  50. CRYP_ALGO_DES_ECB,
  51. CRYP_ALGO_DES_CBC,
  52. CRYP_ALGO_AES_ECB,
  53. CRYP_ALGO_AES_CBC,
  54. CRYP_ALGO_AES_CTR,
  55. CRYP_ALGO_AES_XTS
  56. };
  57. /* Cryp Encryption or Decryption */
  58. enum cryp_algorithm_dir {
  59. CRYP_ALGORITHM_ENCRYPT,
  60. CRYP_ALGORITHM_DECRYPT
  61. };
  62. /* Hardware access method */
  63. enum cryp_mode {
  64. CRYP_MODE_POLLING,
  65. CRYP_MODE_INTERRUPT,
  66. CRYP_MODE_DMA
  67. };
  68. /**
  69. * struct cryp_config -
  70. * @keysize: Key size for AES
  71. * @algomode: AES modes
  72. * @algodir: Cryp Encryption or Decryption
  73. *
  74. * CRYP configuration structure to be passed to set configuration
  75. */
  76. struct cryp_config {
  77. int keysize;
  78. enum cryp_algo_mode algomode;
  79. enum cryp_algorithm_dir algodir;
  80. };
  81. /**
  82. * struct cryp_protection_config -
  83. * @privilege_access: Privileged cryp state enable/disable
  84. * @secure_access: Secure cryp state enable/disable
  85. *
  86. * Protection configuration structure for setting privilage access
  87. */
  88. struct cryp_protection_config {
  89. enum cryp_state privilege_access;
  90. enum cryp_state secure_access;
  91. };
  92. /* Cryp status */
  93. enum cryp_status_id {
  94. CRYP_STATUS_BUSY = 0x10,
  95. CRYP_STATUS_OUTPUT_FIFO_FULL = 0x08,
  96. CRYP_STATUS_OUTPUT_FIFO_NOT_EMPTY = 0x04,
  97. CRYP_STATUS_INPUT_FIFO_NOT_FULL = 0x02,
  98. CRYP_STATUS_INPUT_FIFO_EMPTY = 0x01
  99. };
  100. /* Cryp DMA interface */
  101. #define CRYP_DMA_TX_FIFO 0x08
  102. #define CRYP_DMA_RX_FIFO 0x10
  103. enum cryp_dma_req_type {
  104. CRYP_DMA_DISABLE_BOTH,
  105. CRYP_DMA_ENABLE_IN_DATA,
  106. CRYP_DMA_ENABLE_OUT_DATA,
  107. CRYP_DMA_ENABLE_BOTH_DIRECTIONS
  108. };
  109. enum cryp_dma_channel {
  110. CRYP_DMA_RX = 0,
  111. CRYP_DMA_TX
  112. };
  113. /* Key registers */
  114. enum cryp_key_reg_index {
  115. CRYP_KEY_REG_1,
  116. CRYP_KEY_REG_2,
  117. CRYP_KEY_REG_3,
  118. CRYP_KEY_REG_4
  119. };
  120. /* Key register left and right */
  121. struct cryp_key_value {
  122. u32 key_value_left;
  123. u32 key_value_right;
  124. };
  125. /* Cryp Initialization structure */
  126. enum cryp_init_vector_index {
  127. CRYP_INIT_VECTOR_INDEX_0,
  128. CRYP_INIT_VECTOR_INDEX_1
  129. };
  130. /* struct cryp_init_vector_value -
  131. * @init_value_left
  132. * @init_value_right
  133. * */
  134. struct cryp_init_vector_value {
  135. u32 init_value_left;
  136. u32 init_value_right;
  137. };
  138. /**
  139. * struct cryp_device_context - structure for a cryp context.
  140. * @cr: control register
  141. * @dmacr: DMA control register
  142. * @imsc: Interrupt mask set/clear register
  143. * @key_1_l: Key 1l register
  144. * @key_1_r: Key 1r register
  145. * @key_2_l: Key 2l register
  146. * @key_2_r: Key 2r register
  147. * @key_3_l: Key 3l register
  148. * @key_3_r: Key 3r register
  149. * @key_4_l: Key 4l register
  150. * @key_4_r: Key 4r register
  151. * @init_vect_0_l: Initialization vector 0l register
  152. * @init_vect_0_r: Initialization vector 0r register
  153. * @init_vect_1_l: Initialization vector 1l register
  154. * @init_vect_1_r: Initialization vector 0r register
  155. * @din: Data in register
  156. * @dout: Data out register
  157. *
  158. * CRYP power management specifc structure.
  159. */
  160. struct cryp_device_context {
  161. u32 cr;
  162. u32 dmacr;
  163. u32 imsc;
  164. u32 key_1_l;
  165. u32 key_1_r;
  166. u32 key_2_l;
  167. u32 key_2_r;
  168. u32 key_3_l;
  169. u32 key_3_r;
  170. u32 key_4_l;
  171. u32 key_4_r;
  172. u32 init_vect_0_l;
  173. u32 init_vect_0_r;
  174. u32 init_vect_1_l;
  175. u32 init_vect_1_r;
  176. u32 din;
  177. u32 dout;
  178. };
  179. struct cryp_dma {
  180. dma_cap_mask_t mask;
  181. struct completion cryp_dma_complete;
  182. struct dma_chan *chan_cryp2mem;
  183. struct dma_chan *chan_mem2cryp;
  184. struct stedma40_chan_cfg *cfg_cryp2mem;
  185. struct stedma40_chan_cfg *cfg_mem2cryp;
  186. int sg_src_len;
  187. int sg_dst_len;
  188. struct scatterlist *sg_src;
  189. struct scatterlist *sg_dst;
  190. int nents_src;
  191. int nents_dst;
  192. };
  193. /**
  194. * struct cryp_device_data - structure for a cryp device.
  195. * @base: Pointer to virtual base address of the cryp device.
  196. * @phybase: Pointer to physical memory location of the cryp device.
  197. * @dev: Pointer to the devices dev structure.
  198. * @clk: Pointer to the device's clock control.
  199. * @pwr_regulator: Pointer to the device's power control.
  200. * @power_status: Current status of the power.
  201. * @ctx_lock: Lock for current_ctx.
  202. * @current_ctx: Pointer to the currently allocated context.
  203. * @list_node: For inclusion into a klist.
  204. * @dma: The dma structure holding channel configuration.
  205. * @power_state: TRUE = power state on, FALSE = power state off.
  206. * @power_state_spinlock: Spinlock for power_state.
  207. * @restore_dev_ctx: TRUE = saved ctx, FALSE = no saved ctx.
  208. */
  209. struct cryp_device_data {
  210. struct cryp_register __iomem *base;
  211. phys_addr_t phybase;
  212. struct device *dev;
  213. struct clk *clk;
  214. struct regulator *pwr_regulator;
  215. int power_status;
  216. struct spinlock ctx_lock;
  217. struct cryp_ctx *current_ctx;
  218. struct klist_node list_node;
  219. struct cryp_dma dma;
  220. bool power_state;
  221. struct spinlock power_state_spinlock;
  222. bool restore_dev_ctx;
  223. };
  224. void cryp_wait_until_done(struct cryp_device_data *device_data);
  225. /* Initialization functions */
  226. int cryp_check(struct cryp_device_data *device_data);
  227. void cryp_activity(struct cryp_device_data *device_data,
  228. enum cryp_crypen cryp_crypen);
  229. void cryp_flush_inoutfifo(struct cryp_device_data *device_data);
  230. int cryp_set_configuration(struct cryp_device_data *device_data,
  231. struct cryp_config *cryp_config,
  232. u32 *control_register);
  233. void cryp_configure_for_dma(struct cryp_device_data *device_data,
  234. enum cryp_dma_req_type dma_req);
  235. int cryp_configure_key_values(struct cryp_device_data *device_data,
  236. enum cryp_key_reg_index key_reg_index,
  237. struct cryp_key_value key_value);
  238. int cryp_configure_init_vector(struct cryp_device_data *device_data,
  239. enum cryp_init_vector_index
  240. init_vector_index,
  241. struct cryp_init_vector_value
  242. init_vector_value);
  243. int cryp_configure_protection(struct cryp_device_data *device_data,
  244. struct cryp_protection_config *p_protect_config);
  245. /* Power management funtions */
  246. void cryp_save_device_context(struct cryp_device_data *device_data,
  247. struct cryp_device_context *ctx,
  248. int cryp_mode);
  249. void cryp_restore_device_context(struct cryp_device_data *device_data,
  250. struct cryp_device_context *ctx);
  251. /* Data transfer and status bits. */
  252. int cryp_is_logic_busy(struct cryp_device_data *device_data);
  253. int cryp_get_status(struct cryp_device_data *device_data);
  254. /**
  255. * cryp_write_indata - This routine writes 32 bit data into the data input
  256. * register of the cryptography IP.
  257. * @device_data: Pointer to the device data struct for base address.
  258. * @write_data: Data to write.
  259. */
  260. int cryp_write_indata(struct cryp_device_data *device_data, u32 write_data);
  261. /**
  262. * cryp_read_outdata - This routine reads the data from the data output
  263. * register of the CRYP logic
  264. * @device_data: Pointer to the device data struct for base address.
  265. * @read_data: Read the data from the output FIFO.
  266. */
  267. int cryp_read_outdata(struct cryp_device_data *device_data, u32 *read_data);
  268. #endif /* _CRYP_H_ */