mtk-platform.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Driver for EIP97 cryptographic accelerator.
  3. *
  4. * Copyright (c) 2016 Ryder Lee <ryder.lee@mediatek.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #ifndef __MTK_PLATFORM_H_
  12. #define __MTK_PLATFORM_H_
  13. #include <crypto/algapi.h>
  14. #include <crypto/internal/aead.h>
  15. #include <crypto/internal/hash.h>
  16. #include <crypto/scatterwalk.h>
  17. #include <crypto/skcipher.h>
  18. #include <linux/crypto.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/scatterlist.h>
  22. #include "mtk-regs.h"
  23. #define MTK_RDR_PROC_THRESH BIT(0)
  24. #define MTK_RDR_PROC_MODE BIT(23)
  25. #define MTK_CNT_RST BIT(31)
  26. #define MTK_IRQ_RDR0 BIT(1)
  27. #define MTK_IRQ_RDR1 BIT(3)
  28. #define MTK_IRQ_RDR2 BIT(5)
  29. #define MTK_IRQ_RDR3 BIT(7)
  30. #define SIZE_IN_WORDS(x) ((x) >> 2)
  31. /**
  32. * Ring 0/1 are used by AES encrypt and decrypt.
  33. * Ring 2/3 are used by SHA.
  34. */
  35. enum {
  36. MTK_RING0,
  37. MTK_RING1,
  38. MTK_RING2,
  39. MTK_RING3,
  40. MTK_RING_MAX
  41. };
  42. #define MTK_REC_NUM (MTK_RING_MAX / 2)
  43. #define MTK_IRQ_NUM 5
  44. /**
  45. * struct mtk_desc - DMA descriptor
  46. * @hdr: the descriptor control header
  47. * @buf: DMA address of input buffer segment
  48. * @ct: DMA address of command token that control operation flow
  49. * @ct_hdr: the command token control header
  50. * @tag: the user-defined field
  51. * @tfm: DMA address of transform state
  52. * @bound: align descriptors offset boundary
  53. *
  54. * Structure passed to the crypto engine to describe where source
  55. * data needs to be fetched and how it needs to be processed.
  56. */
  57. struct mtk_desc {
  58. __le32 hdr;
  59. __le32 buf;
  60. __le32 ct;
  61. __le32 ct_hdr;
  62. __le32 tag;
  63. __le32 tfm;
  64. __le32 bound[2];
  65. };
  66. #define MTK_DESC_NUM 512
  67. #define MTK_DESC_OFF SIZE_IN_WORDS(sizeof(struct mtk_desc))
  68. #define MTK_DESC_SZ (MTK_DESC_OFF - 2)
  69. #define MTK_DESC_RING_SZ ((sizeof(struct mtk_desc) * MTK_DESC_NUM))
  70. #define MTK_DESC_CNT(x) ((MTK_DESC_OFF * (x)) << 2)
  71. #define MTK_DESC_LAST cpu_to_le32(BIT(22))
  72. #define MTK_DESC_FIRST cpu_to_le32(BIT(23))
  73. #define MTK_DESC_BUF_LEN(x) cpu_to_le32(x)
  74. #define MTK_DESC_CT_LEN(x) cpu_to_le32((x) << 24)
  75. /**
  76. * struct mtk_ring - Descriptor ring
  77. * @cmd_base: pointer to command descriptor ring base
  78. * @cmd_next: pointer to the next command descriptor
  79. * @cmd_dma: DMA address of command descriptor ring
  80. * @res_base: pointer to result descriptor ring base
  81. * @res_next: pointer to the next result descriptor
  82. * @res_dma: DMA address of result descriptor ring
  83. *
  84. * A descriptor ring is a circular buffer that is used to manage
  85. * one or more descriptors. There are two type of descriptor rings;
  86. * the command descriptor ring and result descriptor ring.
  87. */
  88. struct mtk_ring {
  89. struct mtk_desc *cmd_base;
  90. struct mtk_desc *cmd_next;
  91. dma_addr_t cmd_dma;
  92. struct mtk_desc *res_base;
  93. struct mtk_desc *res_next;
  94. dma_addr_t res_dma;
  95. };
  96. /**
  97. * struct mtk_aes_dma - Structure that holds sg list info
  98. * @sg: pointer to scatter-gather list
  99. * @nents: number of entries in the sg list
  100. * @remainder: remainder of sg list
  101. * @sg_len: number of entries in the sg mapped list
  102. */
  103. struct mtk_aes_dma {
  104. struct scatterlist *sg;
  105. int nents;
  106. u32 remainder;
  107. u32 sg_len;
  108. };
  109. struct mtk_aes_base_ctx;
  110. struct mtk_aes_rec;
  111. struct mtk_cryp;
  112. typedef int (*mtk_aes_fn)(struct mtk_cryp *cryp, struct mtk_aes_rec *aes);
  113. /**
  114. * struct mtk_aes_rec - AES operation record
  115. * @cryp: pointer to Cryptographic device
  116. * @queue: crypto request queue
  117. * @areq: pointer to async request
  118. * @task: the tasklet is use in AES interrupt
  119. * @ctx: pointer to current context
  120. * @src: the structure that holds source sg list info
  121. * @dst: the structure that holds destination sg list info
  122. * @aligned_sg: the scatter list is use to alignment
  123. * @real_dst: pointer to the destination sg list
  124. * @resume: pointer to resume function
  125. * @total: request buffer length
  126. * @buf: pointer to page buffer
  127. * @id: the current use of ring
  128. * @flags: it's describing AES operation state
  129. * @lock: the async queue lock
  130. *
  131. * Structure used to record AES execution state.
  132. */
  133. struct mtk_aes_rec {
  134. struct mtk_cryp *cryp;
  135. struct crypto_queue queue;
  136. struct crypto_async_request *areq;
  137. struct tasklet_struct task;
  138. struct mtk_aes_base_ctx *ctx;
  139. struct mtk_aes_dma src;
  140. struct mtk_aes_dma dst;
  141. struct scatterlist aligned_sg;
  142. struct scatterlist *real_dst;
  143. mtk_aes_fn resume;
  144. size_t total;
  145. void *buf;
  146. u8 id;
  147. unsigned long flags;
  148. /* queue lock */
  149. spinlock_t lock;
  150. };
  151. /**
  152. * struct mtk_sha_rec - SHA operation record
  153. * @cryp: pointer to Cryptographic device
  154. * @queue: crypto request queue
  155. * @req: pointer to ahash request
  156. * @task: the tasklet is use in SHA interrupt
  157. * @id: the current use of ring
  158. * @flags: it's describing SHA operation state
  159. * @lock: the async queue lock
  160. *
  161. * Structure used to record SHA execution state.
  162. */
  163. struct mtk_sha_rec {
  164. struct mtk_cryp *cryp;
  165. struct crypto_queue queue;
  166. struct ahash_request *req;
  167. struct tasklet_struct task;
  168. u8 id;
  169. unsigned long flags;
  170. /* queue lock */
  171. spinlock_t lock;
  172. };
  173. /**
  174. * struct mtk_cryp - Cryptographic device
  175. * @base: pointer to mapped register I/O base
  176. * @dev: pointer to device
  177. * @clk_ethif: pointer to ethif clock
  178. * @clk_cryp: pointer to crypto clock
  179. * @irq: global system and rings IRQ
  180. * @ring: pointer to descriptor rings
  181. * @aes: pointer to operation record of AES
  182. * @sha: pointer to operation record of SHA
  183. * @aes_list: device list of AES
  184. * @sha_list: device list of SHA
  185. * @rec: it's used to select SHA record for tfm
  186. *
  187. * Structure storing cryptographic device information.
  188. */
  189. struct mtk_cryp {
  190. void __iomem *base;
  191. struct device *dev;
  192. struct clk *clk_ethif;
  193. struct clk *clk_cryp;
  194. int irq[MTK_IRQ_NUM];
  195. struct mtk_ring *ring[MTK_RING_MAX];
  196. struct mtk_aes_rec *aes[MTK_REC_NUM];
  197. struct mtk_sha_rec *sha[MTK_REC_NUM];
  198. struct list_head aes_list;
  199. struct list_head sha_list;
  200. bool rec;
  201. };
  202. int mtk_cipher_alg_register(struct mtk_cryp *cryp);
  203. void mtk_cipher_alg_release(struct mtk_cryp *cryp);
  204. int mtk_hash_alg_register(struct mtk_cryp *cryp);
  205. void mtk_hash_alg_release(struct mtk_cryp *cryp);
  206. #endif /* __MTK_PLATFORM_H_ */