hash_alg.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. * Author: Shujuan Chen (shujuan.chen@stericsson.com)
  4. * Author: Joakim Bech (joakim.xx.bech@stericsson.com)
  5. * Author: Berne Hebark (berne.hebark@stericsson.com))
  6. * License terms: GNU General Public License (GPL) version 2
  7. */
  8. #ifndef _HASH_ALG_H
  9. #define _HASH_ALG_H
  10. #include <linux/bitops.h>
  11. #define HASH_BLOCK_SIZE 64
  12. #define HASH_DMA_FIFO 4
  13. #define HASH_DMA_ALIGN_SIZE 4
  14. #define HASH_DMA_PERFORMANCE_MIN_SIZE 1024
  15. #define HASH_BYTES_PER_WORD 4
  16. /* Maximum value of the length's high word */
  17. #define HASH_HIGH_WORD_MAX_VAL 0xFFFFFFFFUL
  18. /* Power on Reset values HASH registers */
  19. #define HASH_RESET_CR_VALUE 0x0
  20. #define HASH_RESET_STR_VALUE 0x0
  21. /* Number of context swap registers */
  22. #define HASH_CSR_COUNT 52
  23. #define HASH_RESET_CSRX_REG_VALUE 0x0
  24. #define HASH_RESET_CSFULL_REG_VALUE 0x0
  25. #define HASH_RESET_CSDATAIN_REG_VALUE 0x0
  26. #define HASH_RESET_INDEX_VAL 0x0
  27. #define HASH_RESET_BIT_INDEX_VAL 0x0
  28. #define HASH_RESET_BUFFER_VAL 0x0
  29. #define HASH_RESET_LEN_HIGH_VAL 0x0
  30. #define HASH_RESET_LEN_LOW_VAL 0x0
  31. /* Control register bitfields */
  32. #define HASH_CR_RESUME_MASK 0x11FCF
  33. #define HASH_CR_SWITCHON_POS 31
  34. #define HASH_CR_SWITCHON_MASK BIT(31)
  35. #define HASH_CR_EMPTYMSG_POS 20
  36. #define HASH_CR_EMPTYMSG_MASK BIT(20)
  37. #define HASH_CR_DINF_POS 12
  38. #define HASH_CR_DINF_MASK BIT(12)
  39. #define HASH_CR_NBW_POS 8
  40. #define HASH_CR_NBW_MASK 0x00000F00UL
  41. #define HASH_CR_LKEY_POS 16
  42. #define HASH_CR_LKEY_MASK BIT(16)
  43. #define HASH_CR_ALGO_POS 7
  44. #define HASH_CR_ALGO_MASK BIT(7)
  45. #define HASH_CR_MODE_POS 6
  46. #define HASH_CR_MODE_MASK BIT(6)
  47. #define HASH_CR_DATAFORM_POS 4
  48. #define HASH_CR_DATAFORM_MASK (BIT(4) | BIT(5))
  49. #define HASH_CR_DMAE_POS 3
  50. #define HASH_CR_DMAE_MASK BIT(3)
  51. #define HASH_CR_INIT_POS 2
  52. #define HASH_CR_INIT_MASK BIT(2)
  53. #define HASH_CR_PRIVN_POS 1
  54. #define HASH_CR_PRIVN_MASK BIT(1)
  55. #define HASH_CR_SECN_POS 0
  56. #define HASH_CR_SECN_MASK BIT(0)
  57. /* Start register bitfields */
  58. #define HASH_STR_DCAL_POS 8
  59. #define HASH_STR_DCAL_MASK BIT(8)
  60. #define HASH_STR_DEFAULT 0x0
  61. #define HASH_STR_NBLW_POS 0
  62. #define HASH_STR_NBLW_MASK 0x0000001FUL
  63. #define HASH_NBLW_MAX_VAL 0x1F
  64. /* PrimeCell IDs */
  65. #define HASH_P_ID0 0xE0
  66. #define HASH_P_ID1 0x05
  67. #define HASH_P_ID2 0x38
  68. #define HASH_P_ID3 0x00
  69. #define HASH_CELL_ID0 0x0D
  70. #define HASH_CELL_ID1 0xF0
  71. #define HASH_CELL_ID2 0x05
  72. #define HASH_CELL_ID3 0xB1
  73. #define HASH_SET_BITS(reg_name, mask) \
  74. writel_relaxed((readl_relaxed(reg_name) | mask), reg_name)
  75. #define HASH_CLEAR_BITS(reg_name, mask) \
  76. writel_relaxed((readl_relaxed(reg_name) & ~mask), reg_name)
  77. #define HASH_PUT_BITS(reg, val, shift, mask) \
  78. writel_relaxed(((readl(reg) & ~(mask)) | \
  79. (((u32)val << shift) & (mask))), reg)
  80. #define HASH_SET_DIN(val, len) writesl(&device_data->base->din, (val), (len))
  81. #define HASH_INITIALIZE \
  82. HASH_PUT_BITS( \
  83. &device_data->base->cr, \
  84. 0x01, HASH_CR_INIT_POS, \
  85. HASH_CR_INIT_MASK)
  86. #define HASH_SET_DATA_FORMAT(data_format) \
  87. HASH_PUT_BITS( \
  88. &device_data->base->cr, \
  89. (u32) (data_format), HASH_CR_DATAFORM_POS, \
  90. HASH_CR_DATAFORM_MASK)
  91. #define HASH_SET_NBLW(val) \
  92. HASH_PUT_BITS( \
  93. &device_data->base->str, \
  94. (u32) (val), HASH_STR_NBLW_POS, \
  95. HASH_STR_NBLW_MASK)
  96. #define HASH_SET_DCAL \
  97. HASH_PUT_BITS( \
  98. &device_data->base->str, \
  99. 0x01, HASH_STR_DCAL_POS, \
  100. HASH_STR_DCAL_MASK)
  101. /* Hardware access method */
  102. enum hash_mode {
  103. HASH_MODE_CPU,
  104. HASH_MODE_DMA
  105. };
  106. /**
  107. * struct uint64 - Structure to handle 64 bits integers.
  108. * @high_word: Most significant bits.
  109. * @low_word: Least significant bits.
  110. *
  111. * Used to handle 64 bits integers.
  112. */
  113. struct uint64 {
  114. u32 high_word;
  115. u32 low_word;
  116. };
  117. /**
  118. * struct hash_register - Contains all registers in ux500 hash hardware.
  119. * @cr: HASH control register (0x000).
  120. * @din: HASH data input register (0x004).
  121. * @str: HASH start register (0x008).
  122. * @hx: HASH digest register 0..7 (0x00c-0x01C).
  123. * @padding0: Reserved (0x02C).
  124. * @itcr: Integration test control register (0x080).
  125. * @itip: Integration test input register (0x084).
  126. * @itop: Integration test output register (0x088).
  127. * @padding1: Reserved (0x08C).
  128. * @csfull: HASH context full register (0x0F8).
  129. * @csdatain: HASH context swap data input register (0x0FC).
  130. * @csrx: HASH context swap register 0..51 (0x100-0x1CC).
  131. * @padding2: Reserved (0x1D0).
  132. * @periphid0: HASH peripheral identification register 0 (0xFE0).
  133. * @periphid1: HASH peripheral identification register 1 (0xFE4).
  134. * @periphid2: HASH peripheral identification register 2 (0xFE8).
  135. * @periphid3: HASH peripheral identification register 3 (0xFEC).
  136. * @cellid0: HASH PCell identification register 0 (0xFF0).
  137. * @cellid1: HASH PCell identification register 1 (0xFF4).
  138. * @cellid2: HASH PCell identification register 2 (0xFF8).
  139. * @cellid3: HASH PCell identification register 3 (0xFFC).
  140. *
  141. * The device communicates to the HASH via 32-bit-wide control registers
  142. * accessible via the 32-bit width AMBA rev. 2.0 AHB Bus. Below is a structure
  143. * with the registers used.
  144. */
  145. struct hash_register {
  146. u32 cr;
  147. u32 din;
  148. u32 str;
  149. u32 hx[8];
  150. u32 padding0[(0x080 - 0x02C) / sizeof(u32)];
  151. u32 itcr;
  152. u32 itip;
  153. u32 itop;
  154. u32 padding1[(0x0F8 - 0x08C) / sizeof(u32)];
  155. u32 csfull;
  156. u32 csdatain;
  157. u32 csrx[HASH_CSR_COUNT];
  158. u32 padding2[(0xFE0 - 0x1D0) / sizeof(u32)];
  159. u32 periphid0;
  160. u32 periphid1;
  161. u32 periphid2;
  162. u32 periphid3;
  163. u32 cellid0;
  164. u32 cellid1;
  165. u32 cellid2;
  166. u32 cellid3;
  167. };
  168. /**
  169. * struct hash_state - Hash context state.
  170. * @temp_cr: Temporary HASH Control Register.
  171. * @str_reg: HASH Start Register.
  172. * @din_reg: HASH Data Input Register.
  173. * @csr[52]: HASH Context Swap Registers 0-39.
  174. * @csfull: HASH Context Swap Registers 40 ie Status flags.
  175. * @csdatain: HASH Context Swap Registers 41 ie Input data.
  176. * @buffer: Working buffer for messages going to the hardware.
  177. * @length: Length of the part of message hashed so far (floor(N/64) * 64).
  178. * @index: Valid number of bytes in buffer (N % 64).
  179. * @bit_index: Valid number of bits in buffer (N % 8).
  180. *
  181. * This structure is used between context switches, i.e. when ongoing jobs are
  182. * interupted with new jobs. When this happens we need to store intermediate
  183. * results in software.
  184. *
  185. * WARNING: "index" is the member of the structure, to be sure that "buffer"
  186. * is aligned on a 4-bytes boundary. This is highly implementation dependent
  187. * and MUST be checked whenever this code is ported on new platforms.
  188. */
  189. struct hash_state {
  190. u32 temp_cr;
  191. u32 str_reg;
  192. u32 din_reg;
  193. u32 csr[52];
  194. u32 csfull;
  195. u32 csdatain;
  196. u32 buffer[HASH_BLOCK_SIZE / sizeof(u32)];
  197. struct uint64 length;
  198. u8 index;
  199. u8 bit_index;
  200. };
  201. /**
  202. * enum hash_device_id - HASH device ID.
  203. * @HASH_DEVICE_ID_0: Hash hardware with ID 0
  204. * @HASH_DEVICE_ID_1: Hash hardware with ID 1
  205. */
  206. enum hash_device_id {
  207. HASH_DEVICE_ID_0 = 0,
  208. HASH_DEVICE_ID_1 = 1
  209. };
  210. /**
  211. * enum hash_data_format - HASH data format.
  212. * @HASH_DATA_32_BITS: 32 bits data format
  213. * @HASH_DATA_16_BITS: 16 bits data format
  214. * @HASH_DATA_8_BITS: 8 bits data format.
  215. * @HASH_DATA_1_BITS: 1 bit data format.
  216. */
  217. enum hash_data_format {
  218. HASH_DATA_32_BITS = 0x0,
  219. HASH_DATA_16_BITS = 0x1,
  220. HASH_DATA_8_BITS = 0x2,
  221. HASH_DATA_1_BIT = 0x3
  222. };
  223. /**
  224. * enum hash_algo - Enumeration for selecting between SHA1 or SHA2 algorithm.
  225. * @HASH_ALGO_SHA1: Indicates that SHA1 is used.
  226. * @HASH_ALGO_SHA2: Indicates that SHA2 (SHA256) is used.
  227. */
  228. enum hash_algo {
  229. HASH_ALGO_SHA1 = 0x0,
  230. HASH_ALGO_SHA256 = 0x1
  231. };
  232. /**
  233. * enum hash_op - Enumeration for selecting between HASH or HMAC mode.
  234. * @HASH_OPER_MODE_HASH: Indicates usage of normal HASH mode.
  235. * @HASH_OPER_MODE_HMAC: Indicates usage of HMAC.
  236. */
  237. enum hash_op {
  238. HASH_OPER_MODE_HASH = 0x0,
  239. HASH_OPER_MODE_HMAC = 0x1
  240. };
  241. /**
  242. * struct hash_config - Configuration data for the hardware.
  243. * @data_format: Format of data entered into the hash data in register.
  244. * @algorithm: Algorithm selection bit.
  245. * @oper_mode: Operating mode selection bit.
  246. */
  247. struct hash_config {
  248. int data_format;
  249. int algorithm;
  250. int oper_mode;
  251. };
  252. /**
  253. * struct hash_dma - Structure used for dma.
  254. * @mask: DMA capabilities bitmap mask.
  255. * @complete: Used to maintain state for a "completion".
  256. * @chan_mem2hash: DMA channel.
  257. * @cfg_mem2hash: DMA channel configuration.
  258. * @sg_len: Scatterlist length.
  259. * @sg: Scatterlist.
  260. * @nents: Number of sg entries.
  261. */
  262. struct hash_dma {
  263. dma_cap_mask_t mask;
  264. struct completion complete;
  265. struct dma_chan *chan_mem2hash;
  266. void *cfg_mem2hash;
  267. int sg_len;
  268. struct scatterlist *sg;
  269. int nents;
  270. };
  271. /**
  272. * struct hash_ctx - The context used for hash calculations.
  273. * @key: The key used in the operation.
  274. * @keylen: The length of the key.
  275. * @state: The state of the current calculations.
  276. * @config: The current configuration.
  277. * @digestsize: The size of current digest.
  278. * @device: Pointer to the device structure.
  279. */
  280. struct hash_ctx {
  281. u8 *key;
  282. u32 keylen;
  283. struct hash_config config;
  284. int digestsize;
  285. struct hash_device_data *device;
  286. };
  287. /**
  288. * struct hash_ctx - The request context used for hash calculations.
  289. * @state: The state of the current calculations.
  290. * @dma_mode: Used in special cases (workaround), e.g. need to change to
  291. * cpu mode, if not supported/working in dma mode.
  292. * @updated: Indicates if hardware is initialized for new operations.
  293. */
  294. struct hash_req_ctx {
  295. struct hash_state state;
  296. bool dma_mode;
  297. u8 updated;
  298. };
  299. /**
  300. * struct hash_device_data - structure for a hash device.
  301. * @base: Pointer to virtual base address of the hash device.
  302. * @phybase: Pointer to physical memory location of the hash device.
  303. * @list_node: For inclusion in klist.
  304. * @dev: Pointer to the device dev structure.
  305. * @ctx_lock: Spinlock for current_ctx.
  306. * @current_ctx: Pointer to the currently allocated context.
  307. * @power_state: TRUE = power state on, FALSE = power state off.
  308. * @power_state_lock: Spinlock for power_state.
  309. * @regulator: Pointer to the device's power control.
  310. * @clk: Pointer to the device's clock control.
  311. * @restore_dev_state: TRUE = saved state, FALSE = no saved state.
  312. * @dma: Structure used for dma.
  313. */
  314. struct hash_device_data {
  315. struct hash_register __iomem *base;
  316. phys_addr_t phybase;
  317. struct klist_node list_node;
  318. struct device *dev;
  319. struct spinlock ctx_lock;
  320. struct hash_ctx *current_ctx;
  321. bool power_state;
  322. struct spinlock power_state_lock;
  323. struct regulator *regulator;
  324. struct clk *clk;
  325. bool restore_dev_state;
  326. struct hash_state state; /* Used for saving and resuming state */
  327. struct hash_dma dma;
  328. };
  329. int hash_check_hw(struct hash_device_data *device_data);
  330. int hash_setconfiguration(struct hash_device_data *device_data,
  331. struct hash_config *config);
  332. void hash_begin(struct hash_device_data *device_data, struct hash_ctx *ctx);
  333. void hash_get_digest(struct hash_device_data *device_data,
  334. u8 *digest, int algorithm);
  335. int hash_hw_update(struct ahash_request *req);
  336. int hash_save_state(struct hash_device_data *device_data,
  337. struct hash_state *state);
  338. int hash_resume_state(struct hash_device_data *device_data,
  339. const struct hash_state *state);
  340. #endif