jh7110-cryp.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __STARFIVE_STR_H__
  3. #define __STARFIVE_STR_H__
  4. #include <crypto/aes.h>
  5. #include <crypto/hash.h>
  6. #include <crypto/scatterwalk.h>
  7. #include <crypto/sha2.h>
  8. #include <crypto/sm3.h>
  9. #include <linux/delay.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/interrupt.h>
  13. #define STARFIVE_ALG_CR_OFFSET 0x0
  14. #define STARFIVE_ALG_FIFO_OFFSET 0x4
  15. #define STARFIVE_IE_MASK_OFFSET 0x8
  16. #define STARFIVE_IE_FLAG_OFFSET 0xc
  17. #define STARFIVE_DMA_IN_LEN_OFFSET 0x10
  18. #define STARFIVE_DMA_OUT_LEN_OFFSET 0x14
  19. #define STARFIVE_IE_MASK_AES_DONE 0x1
  20. #define STARFIVE_IE_MASK_HASH_DONE 0x4
  21. #define STARFIVE_IE_MASK_PKA_DONE 0x8
  22. #define STARFIVE_IE_FLAG_AES_DONE 0x1
  23. #define STARFIVE_IE_FLAG_HASH_DONE 0x4
  24. #define STARFIVE_IE_FLAG_PKA_DONE 0x8
  25. #define STARFIVE_MSG_BUFFER_SIZE SZ_16K
  26. #define MAX_KEY_SIZE SHA512_BLOCK_SIZE
  27. #define STARFIVE_AES_IV_LEN AES_BLOCK_SIZE
  28. #define STARFIVE_AES_CTR_LEN AES_BLOCK_SIZE
  29. #define STARFIVE_RSA_MAX_KEYSZ 256
  30. union starfive_aes_csr {
  31. u32 v;
  32. struct {
  33. u32 cmode :1;
  34. #define STARFIVE_AES_KEYMODE_128 0x0
  35. #define STARFIVE_AES_KEYMODE_192 0x1
  36. #define STARFIVE_AES_KEYMODE_256 0x2
  37. u32 keymode :2;
  38. #define STARFIVE_AES_BUSY BIT(3)
  39. u32 busy :1;
  40. u32 done :1;
  41. #define STARFIVE_AES_KEY_DONE BIT(5)
  42. u32 krdy :1;
  43. u32 aesrst :1;
  44. u32 ie :1;
  45. #define STARFIVE_AES_CCM_START BIT(8)
  46. u32 ccm_start :1;
  47. #define STARFIVE_AES_MODE_ECB 0x0
  48. #define STARFIVE_AES_MODE_CBC 0x1
  49. #define STARFIVE_AES_MODE_CTR 0x4
  50. #define STARFIVE_AES_MODE_CCM 0x5
  51. #define STARFIVE_AES_MODE_GCM 0x6
  52. u32 mode :3;
  53. #define STARFIVE_AES_GCM_START BIT(12)
  54. u32 gcm_start :1;
  55. #define STARFIVE_AES_GCM_DONE BIT(13)
  56. u32 gcm_done :1;
  57. u32 delay_aes :1;
  58. u32 vaes_start :1;
  59. u32 rsvd_0 :8;
  60. #define STARFIVE_AES_MODE_XFB_1 0x0
  61. #define STARFIVE_AES_MODE_XFB_128 0x5
  62. u32 stmode :3;
  63. u32 rsvd_1 :5;
  64. };
  65. };
  66. union starfive_hash_csr {
  67. u32 v;
  68. struct {
  69. u32 start :1;
  70. u32 reset :1;
  71. u32 ie :1;
  72. u32 firstb :1;
  73. #define STARFIVE_HASH_SM3 0x0
  74. #define STARFIVE_HASH_SHA224 0x3
  75. #define STARFIVE_HASH_SHA256 0x4
  76. #define STARFIVE_HASH_SHA384 0x5
  77. #define STARFIVE_HASH_SHA512 0x6
  78. #define STARFIVE_HASH_MODE_MASK 0x7
  79. u32 mode :3;
  80. u32 rsvd_1 :1;
  81. u32 final :1;
  82. u32 rsvd_2 :2;
  83. #define STARFIVE_HASH_HMAC_FLAGS 0x800
  84. u32 hmac :1;
  85. u32 rsvd_3 :1;
  86. #define STARFIVE_HASH_KEY_DONE BIT(13)
  87. u32 key_done :1;
  88. u32 key_flag :1;
  89. #define STARFIVE_HASH_HMAC_DONE BIT(15)
  90. u32 hmac_done :1;
  91. #define STARFIVE_HASH_BUSY BIT(16)
  92. u32 busy :1;
  93. u32 hashdone :1;
  94. u32 rsvd_4 :14;
  95. };
  96. };
  97. union starfive_pka_cacr {
  98. u32 v;
  99. struct {
  100. u32 start :1;
  101. u32 reset :1;
  102. u32 ie :1;
  103. u32 rsvd_0 :1;
  104. u32 fifo_mode :1;
  105. u32 not_r2 :1;
  106. u32 ecc_sub :1;
  107. u32 pre_expf :1;
  108. u32 cmd :4;
  109. u32 rsvd_1 :1;
  110. u32 ctrl_dummy :1;
  111. u32 ctrl_false :1;
  112. u32 cln_done :1;
  113. u32 opsize :6;
  114. u32 rsvd_2 :2;
  115. u32 exposize :6;
  116. u32 rsvd_3 :1;
  117. u32 bigendian :1;
  118. };
  119. };
  120. union starfive_pka_casr {
  121. u32 v;
  122. struct {
  123. #define STARFIVE_PKA_DONE BIT(0)
  124. u32 done :1;
  125. u32 rsvd_0 :31;
  126. };
  127. };
  128. struct starfive_rsa_key {
  129. u8 *n;
  130. u8 *e;
  131. u8 *d;
  132. int e_bitlen;
  133. int d_bitlen;
  134. int bitlen;
  135. size_t key_sz;
  136. };
  137. union starfive_alg_cr {
  138. u32 v;
  139. struct {
  140. u32 start :1;
  141. u32 aes_dma_en :1;
  142. u32 rsvd_0 :1;
  143. u32 hash_dma_en :1;
  144. u32 alg_done :1;
  145. u32 rsvd_1 :3;
  146. u32 clear :1;
  147. u32 rsvd_2 :23;
  148. };
  149. };
  150. struct starfive_cryp_ctx {
  151. struct starfive_cryp_dev *cryp;
  152. struct starfive_cryp_request_ctx *rctx;
  153. unsigned int hash_mode;
  154. u8 key[MAX_KEY_SIZE];
  155. int keylen;
  156. bool is_hmac;
  157. struct starfive_rsa_key rsa_key;
  158. struct crypto_akcipher *akcipher_fbk;
  159. struct crypto_ahash *ahash_fbk;
  160. struct crypto_aead *aead_fbk;
  161. struct crypto_skcipher *skcipher_fbk;
  162. };
  163. struct starfive_cryp_dev {
  164. struct list_head list;
  165. struct device *dev;
  166. struct clk *hclk;
  167. struct clk *ahb;
  168. struct reset_control *rst;
  169. void __iomem *base;
  170. phys_addr_t phys_base;
  171. u32 dma_maxburst;
  172. struct dma_chan *tx;
  173. struct dma_chan *rx;
  174. struct dma_slave_config cfg_in;
  175. struct dma_slave_config cfg_out;
  176. struct crypto_engine *engine;
  177. struct completion dma_done;
  178. size_t assoclen;
  179. size_t total_in;
  180. size_t total_out;
  181. u32 tag_in[4];
  182. u32 tag_out[4];
  183. unsigned int authsize;
  184. unsigned long flags;
  185. int err;
  186. bool side_chan;
  187. union starfive_alg_cr alg_cr;
  188. union {
  189. struct ahash_request *hreq;
  190. struct aead_request *areq;
  191. struct skcipher_request *sreq;
  192. } req;
  193. };
  194. struct starfive_cryp_request_ctx {
  195. union {
  196. union starfive_hash_csr hash;
  197. union starfive_pka_cacr pka;
  198. union starfive_aes_csr aes;
  199. } csr;
  200. struct scatterlist *in_sg;
  201. struct scatterlist *out_sg;
  202. size_t total;
  203. unsigned int blksize;
  204. unsigned int digsize;
  205. unsigned long in_sg_len;
  206. unsigned char *adata;
  207. u8 rsa_data[STARFIVE_RSA_MAX_KEYSZ] __aligned(sizeof(u32));
  208. /* Must be last as it ends in a flexible-array member. */
  209. struct ahash_request ahash_fbk_req;
  210. };
  211. struct starfive_cryp_dev *starfive_cryp_find_dev(struct starfive_cryp_ctx *ctx);
  212. int starfive_hash_register_algs(void);
  213. void starfive_hash_unregister_algs(void);
  214. int starfive_rsa_register_algs(void);
  215. void starfive_rsa_unregister_algs(void);
  216. int starfive_aes_register_algs(void);
  217. void starfive_aes_unregister_algs(void);
  218. #endif