cqhci.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /* Copyright (c) 2015, The Linux Foundation. All rights reserved.
  3. */
  4. #ifndef LINUX_MMC_CQHCI_H
  5. #define LINUX_MMC_CQHCI_H
  6. #include <linux/compiler.h>
  7. #include <linux/bitfield.h>
  8. #include <linux/bitops.h>
  9. #include <linux/spinlock_types.h>
  10. #include <linux/types.h>
  11. #include <linux/completion.h>
  12. #include <linux/wait.h>
  13. #include <linux/irqreturn.h>
  14. #include <asm/io.h>
  15. /* registers */
  16. /* version */
  17. #define CQHCI_VER 0x00
  18. #define CQHCI_VER_MAJOR(x) (((x) & GENMASK(11, 8)) >> 8)
  19. #define CQHCI_VER_MINOR1(x) (((x) & GENMASK(7, 4)) >> 4)
  20. #define CQHCI_VER_MINOR2(x) ((x) & GENMASK(3, 0))
  21. /* capabilities */
  22. #define CQHCI_CAP 0x04
  23. #define CQHCI_CAP_CS 0x10000000 /* Crypto Support */
  24. #define CQHCI_CAP_ITCFMUL GENMASK(15, 12)
  25. #define CQHCI_ITCFMUL(x) FIELD_GET(CQHCI_CAP_ITCFMUL, (x))
  26. /* configuration */
  27. #define CQHCI_CFG 0x08
  28. #define CQHCI_DCMD 0x00001000
  29. #define CQHCI_TASK_DESC_SZ 0x00000100
  30. #define CQHCI_CRYPTO_GENERAL_ENABLE 0x00000002
  31. #define CQHCI_ENABLE 0x00000001
  32. /* control */
  33. #define CQHCI_CTL 0x0C
  34. #define CQHCI_CLEAR_ALL_TASKS 0x00000100
  35. #define CQHCI_HALT 0x00000001
  36. /* interrupt status */
  37. #define CQHCI_IS 0x10
  38. #define CQHCI_IS_HAC BIT(0)
  39. #define CQHCI_IS_TCC BIT(1)
  40. #define CQHCI_IS_RED BIT(2)
  41. #define CQHCI_IS_TCL BIT(3)
  42. #define CQHCI_IS_GCE BIT(4) /* General Crypto Error */
  43. #define CQHCI_IS_ICCE BIT(5) /* Invalid Crypto Config Error */
  44. #define CQHCI_IS_MASK (CQHCI_IS_TCC | CQHCI_IS_RED | \
  45. CQHCI_IS_GCE | CQHCI_IS_ICCE)
  46. /* interrupt status enable */
  47. #define CQHCI_ISTE 0x14
  48. /* interrupt signal enable */
  49. #define CQHCI_ISGE 0x18
  50. /* interrupt coalescing */
  51. #define CQHCI_IC 0x1C
  52. #define CQHCI_IC_ENABLE BIT(31)
  53. #define CQHCI_IC_RESET BIT(16)
  54. #define CQHCI_IC_ICCTHWEN BIT(15)
  55. #define CQHCI_IC_ICCTH(x) (((x) & 0x1F) << 8)
  56. #define CQHCI_IC_ICTOVALWEN BIT(7)
  57. #define CQHCI_IC_ICTOVAL(x) ((x) & 0x7F)
  58. /* task list base address */
  59. #define CQHCI_TDLBA 0x20
  60. /* task list base address upper */
  61. #define CQHCI_TDLBAU 0x24
  62. /* door-bell */
  63. #define CQHCI_TDBR 0x28
  64. /* task completion notification */
  65. #define CQHCI_TCN 0x2C
  66. /* device queue status */
  67. #define CQHCI_DQS 0x30
  68. /* device pending tasks */
  69. #define CQHCI_DPT 0x34
  70. /* task clear */
  71. #define CQHCI_TCLR 0x38
  72. /* task descriptor processing error */
  73. #define CQHCI_TDPE 0x3c
  74. /* send status config 1 */
  75. #define CQHCI_SSC1 0x40
  76. #define CQHCI_SSC1_CBC_MASK GENMASK(19, 16)
  77. #define CQHCI_SSC1_CIT_MASK GENMASK(15, 0)
  78. /* send status config 2 */
  79. #define CQHCI_SSC2 0x44
  80. /* response for dcmd */
  81. #define CQHCI_CRDCT 0x48
  82. /* response mode error mask */
  83. #define CQHCI_RMEM 0x50
  84. /* task error info */
  85. #define CQHCI_TERRI 0x54
  86. #define CQHCI_TERRI_C_INDEX(x) ((x) & GENMASK(5, 0))
  87. #define CQHCI_TERRI_C_TASK(x) (((x) & GENMASK(12, 8)) >> 8)
  88. #define CQHCI_TERRI_C_VALID(x) ((x) & BIT(15))
  89. #define CQHCI_TERRI_D_INDEX(x) (((x) & GENMASK(21, 16)) >> 16)
  90. #define CQHCI_TERRI_D_TASK(x) (((x) & GENMASK(28, 24)) >> 24)
  91. #define CQHCI_TERRI_D_VALID(x) ((x) & BIT(31))
  92. /* command response index */
  93. #define CQHCI_CRI 0x58
  94. /* command response argument */
  95. #define CQHCI_CRA 0x5C
  96. /* crypto capabilities */
  97. #define CQHCI_CCAP 0x100
  98. #define CQHCI_CRYPTOCAP 0x104
  99. #define CQHCI_INT_ALL 0xF
  100. #define CQHCI_IC_DEFAULT_ICCTH 31
  101. #define CQHCI_IC_DEFAULT_ICTOVAL 1
  102. /* attribute fields */
  103. #define CQHCI_VALID(x) (((x) & 1) << 0)
  104. #define CQHCI_END(x) (((x) & 1) << 1)
  105. #define CQHCI_INT(x) (((x) & 1) << 2)
  106. #define CQHCI_ACT(x) (((x) & 0x7) << 3)
  107. /* data command task descriptor fields */
  108. #define CQHCI_FORCED_PROG(x) (((x) & 1) << 6)
  109. #define CQHCI_CONTEXT(x) (((x) & 0xF) << 7)
  110. #define CQHCI_DATA_TAG(x) (((x) & 1) << 11)
  111. #define CQHCI_DATA_DIR(x) (((x) & 1) << 12)
  112. #define CQHCI_PRIORITY(x) (((x) & 1) << 13)
  113. #define CQHCI_QBAR(x) (((x) & 1) << 14)
  114. #define CQHCI_REL_WRITE(x) (((x) & 1) << 15)
  115. #define CQHCI_BLK_COUNT(x) (((x) & 0xFFFF) << 16)
  116. #define CQHCI_BLK_ADDR(x) (((x) & 0xFFFFFFFF) << 32)
  117. /* direct command task descriptor fields */
  118. #define CQHCI_CMD_INDEX(x) (((x) & 0x3F) << 16)
  119. #define CQHCI_CMD_TIMING(x) (((x) & 1) << 22)
  120. #define CQHCI_RESP_TYPE(x) (((x) & 0x3) << 23)
  121. /* crypto task descriptor fields (for bits 64-127 of task descriptor) */
  122. #define CQHCI_CRYPTO_ENABLE_BIT (1ULL << 47)
  123. #define CQHCI_CRYPTO_KEYSLOT(x) ((u64)(x) << 32)
  124. /* transfer descriptor fields */
  125. #define CQHCI_DAT_LENGTH(x) (((x) & 0xFFFF) << 16)
  126. #define CQHCI_DAT_ADDR_LO(x) (((x) & 0xFFFFFFFF) << 32)
  127. #define CQHCI_DAT_ADDR_HI(x) (((x) & 0xFFFFFFFF) << 0)
  128. /* CCAP - Crypto Capability 100h */
  129. union cqhci_crypto_capabilities {
  130. __le32 reg_val;
  131. struct {
  132. u8 num_crypto_cap;
  133. u8 config_count;
  134. u8 reserved;
  135. u8 config_array_ptr;
  136. };
  137. };
  138. enum cqhci_crypto_key_size {
  139. CQHCI_CRYPTO_KEY_SIZE_INVALID = 0,
  140. CQHCI_CRYPTO_KEY_SIZE_128 = 1,
  141. CQHCI_CRYPTO_KEY_SIZE_192 = 2,
  142. CQHCI_CRYPTO_KEY_SIZE_256 = 3,
  143. CQHCI_CRYPTO_KEY_SIZE_512 = 4,
  144. };
  145. enum cqhci_crypto_alg {
  146. CQHCI_CRYPTO_ALG_AES_XTS = 0,
  147. CQHCI_CRYPTO_ALG_BITLOCKER_AES_CBC = 1,
  148. CQHCI_CRYPTO_ALG_AES_ECB = 2,
  149. CQHCI_CRYPTO_ALG_ESSIV_AES_CBC = 3,
  150. };
  151. /* x-CRYPTOCAP - Crypto Capability X */
  152. union cqhci_crypto_cap_entry {
  153. __le32 reg_val;
  154. struct {
  155. u8 algorithm_id;
  156. u8 sdus_mask; /* Supported data unit size mask */
  157. u8 key_size;
  158. u8 reserved;
  159. };
  160. };
  161. #define CQHCI_CRYPTO_CONFIGURATION_ENABLE (1 << 7)
  162. #define CQHCI_CRYPTO_KEY_MAX_SIZE 64
  163. /* x-CRYPTOCFG - Crypto Configuration X */
  164. union cqhci_crypto_cfg_entry {
  165. __le32 reg_val[32];
  166. struct {
  167. u8 crypto_key[CQHCI_CRYPTO_KEY_MAX_SIZE];
  168. u8 data_unit_size;
  169. u8 crypto_cap_idx;
  170. u8 reserved_1;
  171. u8 config_enable;
  172. u8 reserved_multi_host;
  173. u8 reserved_2;
  174. u8 vsb[2];
  175. u8 reserved_3[56];
  176. };
  177. };
  178. struct cqhci_host_ops;
  179. struct mmc_host;
  180. struct mmc_request;
  181. struct cqhci_slot;
  182. struct cqhci_host {
  183. const struct cqhci_host_ops *ops;
  184. void __iomem *mmio;
  185. struct mmc_host *mmc;
  186. spinlock_t lock;
  187. /* relative card address of device */
  188. unsigned int rca;
  189. /* 64 bit DMA */
  190. bool dma64;
  191. int num_slots;
  192. int qcnt;
  193. u32 dcmd_slot;
  194. u32 caps;
  195. #define CQHCI_TASK_DESC_SZ_128 0x1
  196. u32 quirks;
  197. #define CQHCI_QUIRK_SHORT_TXFR_DESC_SZ 0x1
  198. bool enabled;
  199. bool halted;
  200. bool init_done;
  201. bool activated;
  202. bool waiting_for_idle;
  203. bool recovery_halt;
  204. size_t desc_size;
  205. size_t data_size;
  206. u8 *desc_base;
  207. /* total descriptor size */
  208. u8 slot_sz;
  209. /* 64/128 bit depends on CQHCI_CFG */
  210. u8 task_desc_len;
  211. /* 64 bit on 32-bit arch, 128 bit on 64-bit */
  212. u8 link_desc_len;
  213. u8 *trans_desc_base;
  214. /* same length as transfer descriptor */
  215. u8 trans_desc_len;
  216. dma_addr_t desc_dma_base;
  217. dma_addr_t trans_desc_dma_base;
  218. struct completion halt_comp;
  219. wait_queue_head_t wait_queue;
  220. struct cqhci_slot *slot;
  221. #ifdef CONFIG_MMC_CRYPTO
  222. union cqhci_crypto_capabilities crypto_capabilities;
  223. union cqhci_crypto_cap_entry *crypto_cap_array;
  224. u32 crypto_cfg_register;
  225. #endif
  226. };
  227. struct cqhci_host_ops {
  228. void (*dumpregs)(struct mmc_host *mmc);
  229. void (*write_l)(struct cqhci_host *host, u32 val, int reg);
  230. u32 (*read_l)(struct cqhci_host *host, int reg);
  231. void (*enable)(struct mmc_host *mmc);
  232. void (*disable)(struct mmc_host *mmc, bool recovery);
  233. void (*update_dcmd_desc)(struct mmc_host *mmc, struct mmc_request *mrq,
  234. u64 *data);
  235. void (*pre_enable)(struct mmc_host *mmc);
  236. void (*post_disable)(struct mmc_host *mmc);
  237. void (*set_tran_desc)(struct cqhci_host *cq_host, u8 **desc,
  238. dma_addr_t addr, int len, bool end, bool dma64);
  239. #ifdef CONFIG_MMC_CRYPTO
  240. bool uses_custom_crypto_profile;
  241. #endif
  242. };
  243. static inline void cqhci_writel(struct cqhci_host *host, u32 val, int reg)
  244. {
  245. if (unlikely(host->ops->write_l))
  246. host->ops->write_l(host, val, reg);
  247. else
  248. writel_relaxed(val, host->mmio + reg);
  249. }
  250. static inline u32 cqhci_readl(struct cqhci_host *host, int reg)
  251. {
  252. if (unlikely(host->ops->read_l))
  253. return host->ops->read_l(host, reg);
  254. else
  255. return readl_relaxed(host->mmio + reg);
  256. }
  257. struct platform_device;
  258. irqreturn_t cqhci_irq(struct mmc_host *mmc, u32 intmask, int cmd_error,
  259. int data_error);
  260. int cqhci_init(struct cqhci_host *cq_host, struct mmc_host *mmc, bool dma64);
  261. struct cqhci_host *cqhci_pltfm_init(struct platform_device *pdev);
  262. int cqhci_deactivate(struct mmc_host *mmc);
  263. void cqhci_set_tran_desc(u8 *desc, dma_addr_t addr, int len, bool end, bool dma64);
  264. static inline int cqhci_suspend(struct mmc_host *mmc)
  265. {
  266. return cqhci_deactivate(mmc);
  267. }
  268. int cqhci_resume(struct mmc_host *mmc);
  269. #endif