cqhci.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. /* send status config 2 */
  78. #define CQHCI_SSC2 0x44
  79. /* response for dcmd */
  80. #define CQHCI_CRDCT 0x48
  81. /* response mode error mask */
  82. #define CQHCI_RMEM 0x50
  83. /* task error info */
  84. #define CQHCI_TERRI 0x54
  85. #define CQHCI_TERRI_C_INDEX(x) ((x) & GENMASK(5, 0))
  86. #define CQHCI_TERRI_C_TASK(x) (((x) & GENMASK(12, 8)) >> 8)
  87. #define CQHCI_TERRI_C_VALID(x) ((x) & BIT(15))
  88. #define CQHCI_TERRI_D_INDEX(x) (((x) & GENMASK(21, 16)) >> 16)
  89. #define CQHCI_TERRI_D_TASK(x) (((x) & GENMASK(28, 24)) >> 24)
  90. #define CQHCI_TERRI_D_VALID(x) ((x) & BIT(31))
  91. /* command response index */
  92. #define CQHCI_CRI 0x58
  93. /* command response argument */
  94. #define CQHCI_CRA 0x5C
  95. /* crypto capabilities */
  96. #define CQHCI_CCAP 0x100
  97. #define CQHCI_CRYPTOCAP 0x104
  98. #define CQHCI_INT_ALL 0xF
  99. #define CQHCI_IC_DEFAULT_ICCTH 31
  100. #define CQHCI_IC_DEFAULT_ICTOVAL 1
  101. /* attribute fields */
  102. #define CQHCI_VALID(x) (((x) & 1) << 0)
  103. #define CQHCI_END(x) (((x) & 1) << 1)
  104. #define CQHCI_INT(x) (((x) & 1) << 2)
  105. #define CQHCI_ACT(x) (((x) & 0x7) << 3)
  106. /* data command task descriptor fields */
  107. #define CQHCI_FORCED_PROG(x) (((x) & 1) << 6)
  108. #define CQHCI_CONTEXT(x) (((x) & 0xF) << 7)
  109. #define CQHCI_DATA_TAG(x) (((x) & 1) << 11)
  110. #define CQHCI_DATA_DIR(x) (((x) & 1) << 12)
  111. #define CQHCI_PRIORITY(x) (((x) & 1) << 13)
  112. #define CQHCI_QBAR(x) (((x) & 1) << 14)
  113. #define CQHCI_REL_WRITE(x) (((x) & 1) << 15)
  114. #define CQHCI_BLK_COUNT(x) (((x) & 0xFFFF) << 16)
  115. #define CQHCI_BLK_ADDR(x) (((x) & 0xFFFFFFFF) << 32)
  116. /* direct command task descriptor fields */
  117. #define CQHCI_CMD_INDEX(x) (((x) & 0x3F) << 16)
  118. #define CQHCI_CMD_TIMING(x) (((x) & 1) << 22)
  119. #define CQHCI_RESP_TYPE(x) (((x) & 0x3) << 23)
  120. /* crypto task descriptor fields (for bits 64-127 of task descriptor) */
  121. #define CQHCI_CRYPTO_ENABLE_BIT (1ULL << 47)
  122. #define CQHCI_CRYPTO_KEYSLOT(x) ((u64)(x) << 32)
  123. /* transfer descriptor fields */
  124. #define CQHCI_DAT_LENGTH(x) (((x) & 0xFFFF) << 16)
  125. #define CQHCI_DAT_ADDR_LO(x) (((x) & 0xFFFFFFFF) << 32)
  126. #define CQHCI_DAT_ADDR_HI(x) (((x) & 0xFFFFFFFF) << 0)
  127. /* CCAP - Crypto Capability 100h */
  128. union cqhci_crypto_capabilities {
  129. __le32 reg_val;
  130. struct {
  131. u8 num_crypto_cap;
  132. u8 config_count;
  133. u8 reserved;
  134. u8 config_array_ptr;
  135. };
  136. };
  137. enum cqhci_crypto_key_size {
  138. CQHCI_CRYPTO_KEY_SIZE_INVALID = 0,
  139. CQHCI_CRYPTO_KEY_SIZE_128 = 1,
  140. CQHCI_CRYPTO_KEY_SIZE_192 = 2,
  141. CQHCI_CRYPTO_KEY_SIZE_256 = 3,
  142. CQHCI_CRYPTO_KEY_SIZE_512 = 4,
  143. };
  144. enum cqhci_crypto_alg {
  145. CQHCI_CRYPTO_ALG_AES_XTS = 0,
  146. CQHCI_CRYPTO_ALG_BITLOCKER_AES_CBC = 1,
  147. CQHCI_CRYPTO_ALG_AES_ECB = 2,
  148. CQHCI_CRYPTO_ALG_ESSIV_AES_CBC = 3,
  149. };
  150. /* x-CRYPTOCAP - Crypto Capability X */
  151. union cqhci_crypto_cap_entry {
  152. __le32 reg_val;
  153. struct {
  154. u8 algorithm_id;
  155. u8 sdus_mask; /* Supported data unit size mask */
  156. u8 key_size;
  157. u8 reserved;
  158. };
  159. };
  160. #define CQHCI_CRYPTO_CONFIGURATION_ENABLE (1 << 7)
  161. #define CQHCI_CRYPTO_KEY_MAX_SIZE 64
  162. /* x-CRYPTOCFG - Crypto Configuration X */
  163. union cqhci_crypto_cfg_entry {
  164. __le32 reg_val[32];
  165. struct {
  166. u8 crypto_key[CQHCI_CRYPTO_KEY_MAX_SIZE];
  167. u8 data_unit_size;
  168. u8 crypto_cap_idx;
  169. u8 reserved_1;
  170. u8 config_enable;
  171. u8 reserved_multi_host;
  172. u8 reserved_2;
  173. u8 vsb[2];
  174. u8 reserved_3[56];
  175. };
  176. };
  177. struct cqhci_host_ops;
  178. struct mmc_host;
  179. struct mmc_request;
  180. struct cqhci_slot;
  181. struct cqhci_host {
  182. const struct cqhci_host_ops *ops;
  183. void __iomem *mmio;
  184. struct mmc_host *mmc;
  185. spinlock_t lock;
  186. /* relative card address of device */
  187. unsigned int rca;
  188. /* 64 bit DMA */
  189. bool dma64;
  190. int num_slots;
  191. int qcnt;
  192. u32 dcmd_slot;
  193. u32 caps;
  194. #define CQHCI_TASK_DESC_SZ_128 0x1
  195. u32 quirks;
  196. #define CQHCI_QUIRK_SHORT_TXFR_DESC_SZ 0x1
  197. bool enabled;
  198. bool halted;
  199. bool init_done;
  200. bool activated;
  201. bool waiting_for_idle;
  202. bool recovery_halt;
  203. size_t desc_size;
  204. size_t data_size;
  205. u8 *desc_base;
  206. /* total descriptor size */
  207. u8 slot_sz;
  208. /* 64/128 bit depends on CQHCI_CFG */
  209. u8 task_desc_len;
  210. /* 64 bit on 32-bit arch, 128 bit on 64-bit */
  211. u8 link_desc_len;
  212. u8 *trans_desc_base;
  213. /* same length as transfer descriptor */
  214. u8 trans_desc_len;
  215. dma_addr_t desc_dma_base;
  216. dma_addr_t trans_desc_dma_base;
  217. struct completion halt_comp;
  218. wait_queue_head_t wait_queue;
  219. struct cqhci_slot *slot;
  220. #ifdef CONFIG_MMC_CRYPTO
  221. union cqhci_crypto_capabilities crypto_capabilities;
  222. union cqhci_crypto_cap_entry *crypto_cap_array;
  223. u32 crypto_cfg_register;
  224. #endif
  225. };
  226. struct cqhci_host_ops {
  227. void (*dumpregs)(struct mmc_host *mmc);
  228. void (*write_l)(struct cqhci_host *host, u32 val, int reg);
  229. u32 (*read_l)(struct cqhci_host *host, int reg);
  230. void (*enable)(struct mmc_host *mmc);
  231. void (*disable)(struct mmc_host *mmc, bool recovery);
  232. void (*update_dcmd_desc)(struct mmc_host *mmc, struct mmc_request *mrq,
  233. u64 *data);
  234. void (*pre_enable)(struct mmc_host *mmc);
  235. void (*post_disable)(struct mmc_host *mmc);
  236. #ifdef CONFIG_MMC_CRYPTO
  237. int (*program_key)(struct cqhci_host *cq_host,
  238. const union cqhci_crypto_cfg_entry *cfg, int slot);
  239. #endif
  240. void (*set_tran_desc)(struct cqhci_host *cq_host, u8 **desc,
  241. dma_addr_t addr, int len, bool end, bool dma64);
  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