sdhci.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-08-16 zhujiale first version
  9. */
  10. #ifndef __RT_SDHCI_HW_H
  11. #define __RT_SDHCI_HW_H
  12. #include "sdhci_host.h"
  13. #include "sdhci_misc.h"
  14. #include "sdhci-platform.h"
  15. #include <drivers/mmcsd_cmd.h>
  16. #include <drivers/dev_mmcsd_core.h>
  17. #include <drivers/mmcsd_host.h>
  18. #include <rtdevice.h>
  19. #define lower_32_bits(n) ((rt_uint32_t)((n) & 0xffffffff))
  20. #define upper_32_bits(n) ((rt_uint32_t)(((n) >> 16) >> 16))
  21. #define MAX_TUNING_LOOP 40
  22. /*
  23. * Controller registers
  24. */
  25. #define RT_SDHCI_DMA_ADDRESS 0x00
  26. #define RT_SDHCI_ARGUMENT2 RT_SDHCI_DMA_ADDRESS
  27. #define RT_SDHCI_32BIT_BLK_CNT RT_SDHCI_DMA_ADDRESS
  28. #define RT_SDHCI_BLOCK_SIZE 0x04
  29. #define RT_SDHCI_MAKE_BLKSZ(dma, blksz) (((dma & 0x7) << 12) | (blksz & 0xFFF))
  30. #define RT_SDHCI_BLOCK_COUNT 0x06
  31. #define RT_SDHCI_ARGUMENT 0x08
  32. #define RT_SDHCI_TRANSFER_MODE 0x0C
  33. #define RT_SDHCI_TRNS_DMA 0x01
  34. #define RT_SDHCI_TRNS_BLK_CNT_EN 0x02
  35. #define RT_SDHCI_TRNS_AUTO_CMD12 0x04
  36. #define RT_SDHCI_TRNS_AUTO_CMD23 0x08
  37. #define RT_SDHCI_TRNS_AUTO_SEL 0x0C
  38. #define RT_SDHCI_TRNS_READ 0x10
  39. #define RT_SDHCI_TRNS_MULTI 0x20
  40. #define RT_SDHCI_COMMAND 0x0E
  41. #define RT_SDHCI_CMD_RESP_MASK 0x03
  42. #define RT_SDHCI_CMD_CRC 0x08
  43. #define RT_SDHCI_CMD_INDEX 0x10
  44. #define RT_SDHCI_CMD_DATA 0x20
  45. #define RT_SDHCI_CMD_ABORTCMD 0xC0
  46. #define RT_SDHCI_CMD_RESP_NONE 0x00
  47. #define RT_SDHCI_CMD_RESP_LONG 0x01
  48. #define RT_SDHCI_CMD_RESP_SHORT 0x02
  49. #define RT_SDHCI_CMD_RESP_SHORT_BUSY 0x03
  50. #define RT_SDHCI_MAKE_CMD(c, f) (((c & 0xff) << 8) | (f & 0xff))
  51. #define RT_SDHCI_GET_CMD(c) ((c >> 8) & 0x3f)
  52. #define RT_SDHCI_RESPONSE 0x10
  53. #define RT_SDHCI_BUFFER 0x20
  54. #define RT_SDHCI_PRESENT_STATE 0x24
  55. #define RT_SDHCI_CMD_INHIBIT 0x00000001
  56. #define RT_SDHCI_DATA_INHIBIT 0x00000002
  57. #define RT_SDHCI_DOING_WRITE 0x00000100
  58. #define RT_SDHCI_DOING_READ 0x00000200
  59. #define RT_SDHCI_SPACE_AVAILABLE 0x00000400
  60. #define RT_SDHCI_DATA_AVAILABLE 0x00000800
  61. #define RT_SDHCI_CARD_PRESENT 0x00010000
  62. #define RT_SDHCI_CARD_PRES_SHIFT 16
  63. #define RT_SDHCI_CD_STABLE 0x00020000
  64. #define RT_SDHCI_CD_LVL 0x00040000
  65. #define RT_SDHCI_CD_LVL_SHIFT 18
  66. #define RT_SDHCI_WRITE_PROTECT 0x00080000
  67. #define RT_SDHCI_DATA_LVL_MASK 0x00F00000
  68. #define RT_SDHCI_DATA_LVL_SHIFT 20
  69. #define RT_SDHCI_DATA_0_LVL_MASK 0x00100000
  70. #define RT_SDHCI_CMD_LVL 0x01000000
  71. #define RT_SDHCI_HOST_CONTROL 0x28
  72. #define RT_SDHCI_CTRL_LED 0x01
  73. #define RT_SDHCI_CTRL_4BITBUS 0x02
  74. #define RT_SDHCI_CTRL_HISPD 0x04
  75. #define RT_SDHCI_CTRL_DMA_MASK 0x18
  76. #define RT_SDHCI_CTRL_SDMA 0x00
  77. #define RT_SDHCI_CTRL_ADMA1 0x08
  78. #define RT_SDHCI_CTRL_ADMA32 0x10
  79. #define RT_SDHCI_CTRL_ADMA64 0x18
  80. #define RT_SDHCI_CTRL_ADMA3 0x18
  81. #define RT_SDHCI_CTRL_8BITBUS 0x20
  82. #define RT_SDHCI_CTRL_CDTEST_INS 0x40
  83. #define RT_SDHCI_CTRL_CDTEST_EN 0x80
  84. #define RT_SDHCI_POWER_CONTROL 0x29
  85. #define RT_SDHCI_POWER_ON 0x01
  86. #define RT_SDHCI_POWER_180 0x0A
  87. #define RT_SDHCI_POWER_300 0x0C
  88. #define RT_SDHCI_POWER_330 0x0E
  89. /*
  90. * VDD2 - UHS2 or PCIe/NVMe
  91. * VDD2 power on/off and voltage select
  92. */
  93. #define RT_SDHCI_VDD2_POWER_ON 0x10
  94. #define RT_SDHCI_VDD2_POWER_120 0x80
  95. #define RT_SDHCI_VDD2_POWER_180 0xA0
  96. #define RT_SDHCI_BLOCK_GAP_CONTROL 0x2A
  97. #define RT_SDHCI_WAKE_UP_CONTROL 0x2B
  98. #define RT_SDHCI_WAKE_ON_INT 0x01
  99. #define RT_SDHCI_WAKE_ON_INSERT 0x02
  100. #define RT_SDHCI_WAKE_ON_REMOVE 0x04
  101. #define RT_SDHCI_CLOCK_CONTROL 0x2C
  102. #define RT_SDHCI_DIVIDER_SHIFT 8
  103. #define RT_SDHCI_DIVIDER_HI_SHIFT 6
  104. #define RT_SDHCI_DIV_MASK 0xFF
  105. #define RT_SDHCI_DIV_MASK_LEN 8
  106. #define RT_SDHCI_DIV_HI_MASK 0x300
  107. #define RT_SDHCI_PROG_CLOCK_MODE 0x0020
  108. #define RT_SDHCI_CLOCK_CARD_EN 0x0004
  109. #define RT_SDHCI_CLOCK_PLL_EN 0x0008
  110. #define RT_SDHCI_CLOCK_INT_STABLE 0x0002
  111. #define RT_SDHCI_CLOCK_INT_EN 0x0001
  112. #define RT_SDHCI_TIMEOUT_CONTROL 0x2E
  113. #define RT_SDHCI_SOFTWARE_RESET 0x2F
  114. #define RT_SDHCI_RESET_ALL 0x01
  115. #define RT_SDHCI_RESET_CMD 0x02
  116. #define RT_SDHCI_RESET_DATA 0x04
  117. #define RT_SDHCI_INT_STATUS 0x30
  118. #define RT_SDHCI_INT_ENABLE 0x34
  119. #define RT_SDHCI_SIGNAL_ENABLE 0x38
  120. #define RT_SDHCI_INT_RESPONSE 0x00000001
  121. #define RT_SDHCI_INT_DATA_END 0x00000002
  122. #define RT_SDHCI_INT_BLK_GAP 0x00000004
  123. #define RT_SDHCI_INT_DMA_END 0x00000008
  124. #define RT_SDHCI_INT_SPACE_AVAIL 0x00000010
  125. #define RT_SDHCI_INT_DATA_AVAIL 0x00000020
  126. #define RT_SDHCI_INT_CARD_INSERT 0x00000040
  127. #define RT_SDHCI_INT_CARD_REMOVE 0x00000080
  128. #define RT_SDHCI_INT_CARD_INT 0x00000100
  129. #define RT_SDHCI_INT_RETUNE 0x00001000
  130. #define RT_SDHCI_INT_CQE 0x00004000
  131. #define RT_SDHCI_INT_ERROR 0x00008000
  132. #define RT_SDHCI_INT_TIMEOUT 0x00010000
  133. #define RT_SDHCI_INT_CRC 0x00020000
  134. #define RT_SDHCI_INT_END_BIT 0x00040000
  135. #define RT_SDHCI_INT_INDEX 0x00080000
  136. #define RT_SDHCI_INT_DATA_TIMEOUT 0x00100000
  137. #define RT_SDHCI_INT_DATA_CRC 0x00200000
  138. #define RT_SDHCI_INT_DATA_END_BIT 0x00400000
  139. #define RT_SDHCI_INT_BUS_POWER 0x00800000
  140. #define RT_SDHCI_INT_AUTO_CMD_ERR 0x01000000
  141. #define RT_SDHCI_INT_ADMA_ERROR 0x02000000
  142. #define RT_SDHCI_INT_NORMAL_MASK 0x00007FFF
  143. #define RT_SDHCI_INT_ERROR_MASK 0xFFFF8000
  144. #define RT_SDHCI_INT_CMD_MASK (RT_SDHCI_INT_RESPONSE | RT_SDHCI_INT_TIMEOUT | RT_SDHCI_INT_CRC | RT_SDHCI_INT_END_BIT | RT_SDHCI_INT_INDEX | RT_SDHCI_INT_AUTO_CMD_ERR)
  145. #define RT_SDHCI_INT_DATA_MASK (RT_SDHCI_INT_DATA_END | RT_SDHCI_INT_DMA_END | RT_SDHCI_INT_DATA_AVAIL | RT_SDHCI_INT_SPACE_AVAIL | RT_SDHCI_INT_DATA_TIMEOUT | RT_SDHCI_INT_DATA_CRC | RT_SDHCI_INT_DATA_END_BIT | RT_SDHCI_INT_ADMA_ERROR | RT_SDHCI_INT_BLK_GAP)
  146. #define RT_SDHCI_INT_ALL_MASK ((unsigned int)-1)
  147. #define RT_SDHCI_CQE_INT_ERR_MASK ( \
  148. RT_SDHCI_INT_ADMA_ERROR | RT_SDHCI_INT_BUS_POWER | RT_SDHCI_INT_DATA_END_BIT | RT_SDHCI_INT_DATA_CRC | RT_SDHCI_INT_DATA_TIMEOUT | RT_SDHCI_INT_INDEX | RT_SDHCI_INT_END_BIT | RT_SDHCI_INT_CRC | RT_SDHCI_INT_TIMEOUT)
  149. #define RT_SDHCI_CQE_INT_MASK (RT_SDHCI_CQE_INT_ERR_MASK | RT_SDHCI_INT_CQE)
  150. #define RT_SDHCI_AUTO_CMD_STATUS 0x3C
  151. #define RT_SDHCI_AUTO_CMD_TIMEOUT 0x00000002
  152. #define RT_SDHCI_AUTO_CMD_CRC 0x00000004
  153. #define RT_SDHCI_AUTO_CMD_END_BIT 0x00000008
  154. #define RT_SDHCI_AUTO_CMD_INDEX 0x00000010
  155. #define RT_SDHCI_HOST_CONTROL2 0x3E
  156. #define RT_SDHCI_CTRL_UHS_MASK 0x0007
  157. #define RT_SDHCI_CTRL_UHS_SDR12 0x0000
  158. #define RT_SDHCI_CTRL_UHS_SDR25 0x0001
  159. #define RT_SDHCI_CTRL_UHS_SDR50 0x0002
  160. #define RT_SDHCI_CTRL_UHS_SDR104 0x0003
  161. #define RT_SDHCI_CTRL_UHS_DDR50 0x0004
  162. #define RT_SDHCI_CTRL_HS400 0x0005 /* Non-standard */
  163. #define RT_SDHCI_CTRL_VDD_180 0x0008
  164. #define RT_SDHCI_CTRL_DRV_TYPE_MASK 0x0030
  165. #define RT_SDHCI_CTRL_DRV_TYPE_B 0x0000
  166. #define RT_SDHCI_CTRL_DRV_TYPE_A 0x0010
  167. #define RT_SDHCI_CTRL_DRV_TYPE_C 0x0020
  168. #define RT_SDHCI_CTRL_DRV_TYPE_D 0x0030
  169. #define RT_SDHCI_CTRL_EXEC_TUNING 0x0040
  170. #define RT_SDHCI_CTRL_TUNED_CLK 0x0080
  171. #define RT_SDHCI_CMD23_ENABLE 0x0800
  172. #define RT_SDHCI_CTRL_V4_MODE 0x1000
  173. #define RT_SDHCI_CTRL_64BIT_ADDR 0x2000
  174. #define RT_SDHCI_CTRL_PRESET_VAL_ENABLE 0x8000
  175. #define RT_SDHCI_CAPABILITIES 0x40
  176. #define RT_SDHCI_TIMEOUT_CLK_MASK RT_GENMASK(5, 0)
  177. #define RT_SDHCI_TIMEOUT_CLK_SHIFT 0
  178. #define RT_SDHCI_TIMEOUT_CLK_UNIT 0x00000080
  179. #define RT_SDHCI_CLOCK_BASE_MASK RT_GENMASK(13, 8)
  180. #define RT_SDHCI_CLOCK_BASE_SHIFT 8
  181. #define RT_SDHCI_CLOCK_V3_BASE_MASK RT_GENMASK(15, 8)
  182. #define RT_SDHCI_MAX_BLOCK_MASK 0x00030000
  183. #define RT_SDHCI_MAX_BLOCK_SHIFT 16
  184. #define RT_SDHCI_CAN_DO_8BIT 0x00040000
  185. #define RT_SDHCI_CAN_DO_ADMA2 0x00080000
  186. #define RT_SDHCI_CAN_DO_ADMA1 0x00100000
  187. #define RT_SDHCI_CAN_DO_HISPD 0x00200000
  188. #define RT_SDHCI_CAN_DO_SDMA 0x00400000
  189. #define RT_SDHCI_CAN_DO_SUSPEND 0x00800000
  190. #define RT_SDHCI_CAN_VDD_330 0x01000000
  191. #define RT_SDHCI_CAN_VDD_300 0x02000000
  192. #define RT_SDHCI_CAN_VDD_180 0x04000000
  193. #define RT_SDHCI_CAN_64BIT_V4 0x08000000
  194. #define RT_SDHCI_CAN_64BIT 0x10000000
  195. #define RT_SDHCI_CAPABILITIES_1 0x44
  196. #define RT_SDHCI_SUPPORT_SDR50 0x00000001
  197. #define RT_SDHCI_SUPPORT_SDR104 0x00000002
  198. #define RT_SDHCI_SUPPORT_DDR50 0x00000004
  199. #define RT_SDHCI_DRIVER_TYPE_A 0x00000010
  200. #define RT_SDHCI_DRIVER_TYPE_C 0x00000020
  201. #define RT_SDHCI_DRIVER_TYPE_D 0x00000040
  202. #define RT_SDHCI_RETUNING_TIMER_COUNT_MASK RT_GENMASK(11, 8)
  203. #define RT_SDHCI_USE_SDR50_TUNING 0x00002000
  204. #define RT_SDHCI_RETUNING_MODE_MASK RT_GENMASK(15, 14)
  205. #define RT_SDHCI_CLOCK_MUL_MASK RT_GENMASK(23, 16)
  206. #define RT_SDHCI_CAN_DO_ADMA3 0x08000000
  207. #define RT_SDHCI_SUPPORT_HS400 0x80000000 /* Non-standard */
  208. #define RT_SDHCI_MAX_CURRENT 0x48
  209. #define RT_SDHCI_MAX_CURRENT_LIMIT RT_GENMASK(7, 0)
  210. #define RT_SDHCI_MAX_CURRENT_330_MASK RT_GENMASK(7, 0)
  211. #define RT_SDHCI_MAX_CURRENT_300_MASK RT_GENMASK(15, 8)
  212. #define RT_SDHCI_MAX_CURRENT_180_MASK RT_GENMASK(23, 16)
  213. #define RT_SDHCI_MAX_CURRENT_MULTIPLIER 4
  214. /* 4C-4F reserved for more max current */
  215. #define RT_SDHCI_SET_ACMD12_ERROR 0x50
  216. #define RT_SDHCI_SET_INT_ERROR 0x52
  217. #define RT_SDHCI_ADMA_ERROR 0x54
  218. /* 55-57 reserved */
  219. #define RT_SDHCI_ADMA_ADDRESS 0x58
  220. #define RT_SDHCI_ADMA_ADDRESS_HI 0x5C
  221. /* 60-FB reserved */
  222. #define RT_SDHCI_PRESET_FOR_HIGH_SPEED 0x64
  223. #define RT_SDHCI_PRESET_FOR_SDR12 0x66
  224. #define RT_SDHCI_PRESET_FOR_SDR25 0x68
  225. #define RT_SDHCI_PRESET_FOR_SDR50 0x6A
  226. #define RT_SDHCI_PRESET_FOR_SDR104 0x6C
  227. #define RT_SDHCI_PRESET_FOR_DDR50 0x6E
  228. #define RT_SDHCI_PRESET_FOR_HS400 0x74 /* Non-standard */
  229. #define RT_SDHCI_PRESET_DRV_MASK RT_GENMASK(15, 14)
  230. #define BIT(nr) ((1) << (nr))
  231. #define RT_SDHCI_PRESET_CLKGEN_SEL BIT(10)
  232. #define RT_SDHCI_PRESET_SDCLK_FREQ_MASK RT_GENMASK(9, 0)
  233. #define RT_SDHCI_SLOT_INT_STATUS 0xFC
  234. #define RT_SDHCI_HOST_VERSION 0xFE
  235. #define RT_SDHCI_VENDOR_VER_MASK 0xFF00
  236. #define RT_SDHCI_VENDOR_VER_SHIFT 8
  237. #define RT_SDHCI_SPEC_VER_MASK 0x00FF
  238. #define RT_SDHCI_SPEC_VER_SHIFT 0
  239. #define RT_SDHCI_SPEC_100 0
  240. #define RT_SDHCI_SPEC_200 1
  241. #define RT_SDHCI_SPEC_300 2
  242. #define RT_SDHCI_SPEC_400 3
  243. #define RT_SDHCI_SPEC_410 4
  244. #define RT_SDHCI_SPEC_420 5
  245. /*
  246. * End of controller registers.
  247. */
  248. #define RT_SDHCI_MAX_DIV_SPEC_200 256
  249. #define RT_SDHCI_MAX_DIV_SPEC_300 2046
  250. /*
  251. * Host SDMA buffer boundary. Valid values from 4K to 512K in powers of 2.
  252. */
  253. #define RT_SDHCI_DEFAULT_BOUNDARY_SIZE (512 * 1024)
  254. #define ilog2(v) __rt_ffs(v)
  255. #define RT_SDHCI_DEFAULT_BOUNDARY_ARG (ilog2(RT_SDHCI_DEFAULT_BOUNDARY_SIZE) - 12)
  256. #define RT_SDHCI_MAX_SEGS 128
  257. /* Allow for a command request and a data request at the same time */
  258. #define RT_SDHCI_MAX_MRQS 2
  259. #define MMC_CMD_TRANSFER_TIME (10 * 1000000L) /* max 10 ms */
  260. enum rt_sdhci_cookie
  261. {
  262. COOKIE_UNMAPPED,
  263. COOKIE_PRE_MAPPED, /* mapped by sdhci_pre_req() */
  264. COOKIE_MAPPED, /* mapped by sdhci_prepare_data() */
  265. };
  266. struct rt_sdhci_host
  267. {
  268. const char *hw_name; /* Hardware bus name */
  269. unsigned int quirks; /* Deviations from spec. */
  270. void *data_buf;
  271. /* Controller doesn't honor resets unless we touch the clock register */
  272. #define RT_SDHCI_QUIRK_CLOCK_BEFORE_RESET (1 << 0)
  273. /* Controller has bad caps bits, but really supports DMA */
  274. #define RT_SDHCI_QUIRK_FORCE_DMA (1 << 1)
  275. /* Controller doesn't like to be reset when there is no card inserted. */
  276. #define RT_SDHCI_QUIRK_NO_CARD_NO_RESET (1 << 2)
  277. /* Controller doesn't like clearing the power reg before a change */
  278. #define RT_SDHCI_QUIRK_SINGLE_POWER_WRITE (1 << 3)
  279. /* Controller has an unusable DMA engine */
  280. #define RT_SDHCI_QUIRK_BROKEN_DMA (1 << 5)
  281. /* Controller has an unusable ADMA engine */
  282. #define RT_SDHCI_QUIRK_BROKEN_ADMA (1 << 6)
  283. /* Controller can only DMA from 32-bit aligned addresses */
  284. #define RT_SDHCI_QUIRK_32BIT_DMA_ADDR (1 << 7)
  285. /* Controller can only DMA chunk sizes that are a multiple of 32 bits */
  286. #define RT_SDHCI_QUIRK_32BIT_DMA_SIZE (1 << 8)
  287. /* Controller can only ADMA chunks that are a multiple of 32 bits */
  288. #define RT_SDHCI_QUIRK_32BIT_ADMA_SIZE (1 << 9)
  289. /* Controller needs to be reset after each request to stay stable */
  290. #define RT_SDHCI_QUIRK_RESET_AFTER_REQUEST (1 << 10)
  291. /* Controller needs voltage and power writes to happen separately */
  292. #define RT_SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER (1 << 11)
  293. /* Controller provides an incorrect timeout value for transfers */
  294. #define RT_SDHCI_QUIRK_BROKEN_TIMEOUT_VAL (1 << 12)
  295. /* Controller has an issue with buffer bits for small transfers */
  296. #define RT_SDHCI_QUIRK_BROKEN_SMALL_PIO (1 << 13)
  297. /* Controller does not provide transfer-complete interrupt when not busy */
  298. #define RT_SDHCI_QUIRK_NO_BUSY_IRQ (1 << 14)
  299. /* Controller has unreliable card detection */
  300. #define RT_SDHCI_QUIRK_BROKEN_CARD_DETECTION (1 << 15)
  301. /* Controller reports inverted write-protect state */
  302. #define RT_SDHCI_QUIRK_INVERTED_WRITE_PROTECT (1 << 16)
  303. /* Controller has unusable command queue engine */
  304. #define RT_SDHCI_QUIRK_BROKEN_CQE (1 << 17)
  305. /* Controller does not like fast PIO transfers */
  306. #define RT_SDHCI_QUIRK_PIO_NEEDS_DELAY (1 << 18)
  307. /* Controller does not have a LED */
  308. #define RT_SDHCI_QUIRK_NO_LED (1 << 19)
  309. /* Controller has to be forced to use block size of 2048 bytes */
  310. #define RT_SDHCI_QUIRK_FORCE_BLK_SZ_2048 (1 << 20)
  311. /* Controller cannot do multi-block transfers */
  312. #define RT_SDHCI_QUIRK_NO_MULTIBLOCK (1 << 21)
  313. /* Controller can only handle 1-bit data transfers */
  314. #define RT_SDHCI_QUIRK_FORCE_1_BIT_DATA (1 << 22)
  315. /* Controller needs 10ms delay between applying power and clock */
  316. #define RT_SDHCI_QUIRK_DELAY_AFTER_POWER (1 << 23)
  317. /* Controller uses SDCLK instead of TMCLK for data timeouts */
  318. #define RT_SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK (1 << 24)
  319. /* Controller reports wrong base clock capability */
  320. #define RT_SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN (1 << 25)
  321. /* Controller cannot support End Attribute in NOP ADMA descriptor */
  322. #define RT_SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC (1 << 26)
  323. /* Controller uses Auto CMD12 command to stop the transfer */
  324. #define RT_SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12 (1 << 28)
  325. /* Controller doesn't have HISPD bit field in HI-SPEED SD card */
  326. #define RT_SDHCI_QUIRK_NO_HISPD_BIT (1 << 29)
  327. /* Controller treats ADMA descriptors with length 0000h incorrectly */
  328. #define RT_SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC (1 << 30)
  329. /* The read-only detection via RT_SDHCI_PRESENT_STATE register is unstable */
  330. #define RT_SDHCI_QUIRK_UNSTABLE_RO_DETECT (1 << 31)
  331. unsigned int quirks2; /* More deviations from spec. */
  332. #define RT_SDHCI_QUIRK2_HOST_OFF_CARD_ON (1 << 0)
  333. #define RT_SDHCI_QUIRK2_HOST_NO_CMD23 (1 << 1)
  334. /* The system physically doesn't support 1.8v, even if the host does */
  335. #define RT_SDHCI_QUIRK2_NO_1_8_V (1 << 2)
  336. #define RT_SDHCI_QUIRK2_PRESET_VALUE_BROKEN (1 << 3)
  337. #define RT_SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON (1 << 4)
  338. /* Controller has a non-standard host control register */
  339. #define RT_SDHCI_QUIRK2_BROKEN_HOST_CONTROL (1 << 5)
  340. /* Controller does not support HS200 */
  341. #define RT_SDHCI_QUIRK2_BROKEN_HS200 (1 << 6)
  342. /* Controller does not support DDR50 */
  343. #define RT_SDHCI_QUIRK2_BROKEN_DDR50 (1 << 7)
  344. /* Stop command (CMD12) can set Transfer Complete when not using MMC_RSP_BUSY */
  345. #define RT_SDHCI_QUIRK2_STOP_WITH_TC (1 << 8)
  346. /* Controller does not support 64-bit DMA */
  347. #define RT_SDHCI_QUIRK2_BROKEN_64_BIT_DMA (1 << 9)
  348. /* need clear transfer mode register before send cmd */
  349. #define RT_SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD (1 << 10)
  350. /* Capability register bit-63 indicates HS400 support */
  351. #define RT_SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 (1 << 11)
  352. /* forced tuned clock */
  353. #define RT_SDHCI_QUIRK2_TUNING_WORK_AROUND (1 << 12)
  354. /* disable the block count for single block transactions */
  355. #define RT_SDHCI_QUIRK2_SUPPORT_SINGLE (1 << 13)
  356. /* Controller broken with using ACMD23 */
  357. #define RT_SDHCI_QUIRK2_ACMD23_BROKEN (1 << 14)
  358. /* Broken Clock divider zero in controller */
  359. #define RT_SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN (1 << 15)
  360. /* Controller has CRC in 136 bit Command Response */
  361. #define RT_SDHCI_QUIRK2_RSP_136_HAS_CRC (1 << 16)
  362. #define RT_SDHCI_QUIRK2_DISABLE_HW_TIMEOUT (1 << 17)
  363. #define RT_SDHCI_QUIRK2_USE_32BIT_BLK_CNT (1 << 18)
  364. /* Issue CMD and DATA reset together */
  365. #define RT_SDHCI_QUIRK2_ISSUE_CMD_DAT_RESET_TOGETHER (1 << 19)
  366. int irq; /* Device IRQ */
  367. void *ioaddr; /* Mapped address */
  368. char *bounce_buffer; /* For packing SDMA reads/writes */
  369. rt_uint64_t bounce_addr;
  370. unsigned int bounce_buffer_size;
  371. const struct rt_sdhci_ops *ops; /* Low level hw interface */
  372. /* Internal data */
  373. struct rt_mmc_host *mmc; /* MMC structure */
  374. struct mmc_host_ops mmc_host_ops; /* MMC host ops */
  375. rt_uint64_t dma_mask; /* custom DMA mask */
  376. rt_spinlock_t lock;
  377. int flags; /* Host attributes */
  378. #define RT_SDHCI_USE_SDMA (1 << 0) /* Host is SDMA capable */
  379. #define RT_SDHCI_USE_ADMA (1 << 1) /* Host is ADMA capable */
  380. #define RT_SDHCI_REQ_USE_DMA (1 << 2) /* Use DMA for this req. */
  381. #define RT_SDHCI_DEVICE_DEAD (1 << 3) /* Device unresponsive */
  382. #define RT_SDHCI_SDR50_NEEDS_TUNING (1 << 4) /* SDR50 needs tuning */
  383. #define RT_SDHCI_AUTO_CMD12 (1 << 6) /* Auto CMD12 support */
  384. #define RT_SDHCI_AUTO_CMD23 (1 << 7) /* Auto CMD23 support */
  385. #define RT_SDHCI_PV_ENABLED (1 << 8) /* Preset value enabled */
  386. #define RT_SDHCI_USE_64_BIT_DMA (1 << 12) /* Use 64-bit DMA */
  387. #define RT_SDHCI_HS400_TUNING (1 << 13) /* Tuning for HS400 */
  388. #define RT_SDHCI_SIGNALING_330 (1 << 14) /* Host is capable of 3.3V signaling */
  389. #define RT_SDHCI_SIGNALING_180 (1 << 15) /* Host is capable of 1.8V signaling */
  390. #define RT_SDHCI_SIGNALING_120 (1 << 16) /* Host is capable of 1.2V signaling */
  391. unsigned int version; /* RT_SDHCI spec. version */
  392. unsigned int max_clk; /* Max possible freq (MHz) */
  393. unsigned int timeout_clk; /* Timeout freq (KHz) */
  394. rt_uint8_t max_timeout_count; /* Vendor specific max timeout count */
  395. unsigned int clk_mul; /* Clock Muliplier value */
  396. unsigned int clock; /* Current clock (MHz) */
  397. rt_uint8_t pwr; /* Current voltage */
  398. rt_uint8_t drv_type; /* Current UHS-I driver type */
  399. rt_bool_t reinit_uhs; /* Force UHS-related re-initialization */
  400. rt_bool_t runtime_suspended; /* Host is runtime suspended */
  401. rt_bool_t bus_on; /* Bus power prevents runtime suspend */
  402. rt_bool_t preset_enabled; /* Preset is enabled */
  403. rt_bool_t pending_reset; /* Cmd/data reset is pending */
  404. rt_bool_t irq_wake_enabled; /* IRQ wakeup is enabled */
  405. rt_bool_t v4_mode; /* Host Version 4 Enable */
  406. rt_bool_t always_defer_done; /* Always defer to complete requests */
  407. struct rt_mmcsd_req *mrqs_done[RT_SDHCI_MAX_MRQS]; /* Requests done */
  408. struct rt_mmcsd_cmd *cmd; /* Current command */
  409. struct rt_mmcsd_cmd *data_cmd; /* Current data command */
  410. struct rt_mmcsd_cmd *deferred_cmd; /* Deferred command */
  411. struct rt_mmcsd_data *data; /* Current data request */
  412. unsigned int data_early : 1; /* Data finished before cmd */
  413. unsigned int blocks; /* remaining PIO blocks */
  414. size_t align_buffer_sz; /* Bounce buffer size */
  415. rt_uint64_t align_addr; /* Mapped bounce buffer */
  416. struct rt_workqueue *complete_wq; /* Request completion wq */
  417. struct rt_work complete_work; /* Request completion work */
  418. struct rt_workqueue *irq_wq;
  419. struct rt_work irq_work;
  420. struct rt_timer timer; /* Timer for timeouts */
  421. struct rt_timer data_timer; /* Timer for data timeouts */
  422. rt_uint32_t caps; /* CAPABILITY_0 */
  423. rt_uint32_t caps1; /* CAPABILITY_1 */
  424. rt_bool_t read_caps; /* Capability flags have been read */
  425. rt_bool_t sdhci_core_to_disable_vqmmc; /* sdhci core can disable vqmmc */
  426. unsigned int ocr_avail_sdio; /* OCR bit masks */
  427. unsigned int ocr_avail_sd;
  428. unsigned int ocr_avail_mmc;
  429. rt_uint32_t ocr_mask; /* available voltages */
  430. unsigned timing; /* Current timing */
  431. rt_uint32_t thread_isr;
  432. /* cached registers */
  433. rt_uint32_t ier;
  434. rt_bool_t cqe_on; /* CQE is operating */
  435. rt_uint32_t cqe_ier; /* CQE interrupt mask */
  436. rt_uint32_t cqe_err_ier; /* CQE error interrupt mask */
  437. rt_wqueue_t buf_ready_int; /* Waitqueue for Buffer Read Ready interrupt */
  438. unsigned int tuning_done; /* Condition flag set when CMD19 succeeds */
  439. unsigned int tuning_count; /* Timer count for re-tuning */
  440. unsigned int tuning_mode; /* Re-tuning mode supported by host */
  441. unsigned int tuning_err; /* Error code for re-tuning */
  442. #define RT_SDHCI_TUNING_MODE_1 0
  443. #define RT_SDHCI_TUNING_MODE_2 1
  444. #define RT_SDHCI_TUNING_MODE_3 2
  445. /* Delay (ms) between tuning commands */
  446. int tuning_delay;
  447. int tuning_loop_count;
  448. /* Host SDMA buffer boundary. */
  449. rt_uint32_t sdma_boundary;
  450. rt_uint64_t data_timeout;
  451. unsigned long private[];
  452. };
  453. static inline rt_uint8_t u8_read(const volatile void *addr)
  454. {
  455. return *(const volatile rt_uint8_t *)addr;
  456. }
  457. static inline rt_uint16_t u16_read(const volatile void *addr)
  458. {
  459. return *(const volatile rt_uint16_t *)addr;
  460. }
  461. static inline rt_uint32_t u32_read(const volatile void *addr)
  462. {
  463. return *(const volatile rt_uint32_t *)addr;
  464. }
  465. static inline void u8_write(rt_uint8_t value, volatile void *addr)
  466. {
  467. *(volatile rt_uint8_t *)addr = value;
  468. }
  469. static inline void u16_write(rt_uint16_t value, volatile void *addr)
  470. {
  471. *(volatile rt_uint16_t *)addr = value;
  472. }
  473. static inline void u32_write(rt_uint32_t value, volatile void *addr)
  474. {
  475. *(volatile rt_uint32_t *)addr = value;
  476. }
  477. #define readb(c) u8_read(c)
  478. #define readw(c) u16_read(c)
  479. #define readl(c) u32_read(c)
  480. #define readsb(p, d, l) ({ __raw_readsb(p,d,l); __iormb(); })
  481. #define readsw(p, d, l) ({ __raw_readsw(p,d,l); __iormb(); })
  482. #define readsl(p, d, l) ({ __raw_readsl(p,d,l); __iormb(); })
  483. #define writeb(v, c) u8_write(v, c)
  484. #define writew(v, c) u16_write(v, c)
  485. #define writel(v, c) u32_write(v, c)
  486. #define writesb(p, d, l) ({ __iowmb(); __raw_writesb(p,d,l); })
  487. #define writesw(p, d, l) ({ __iowmb(); __raw_writesw(p,d,l); })
  488. #define writesl(p, d, l) ({ __iowmb(); __raw_writesl(p,d,l); })
  489. static inline void rt_sdhci_writel(struct rt_sdhci_host *host, rt_uint32_t val, int reg)
  490. {
  491. writel(val, host->ioaddr + reg);
  492. }
  493. static inline void rt_sdhci_writew(struct rt_sdhci_host *host, rt_uint16_t val, int reg)
  494. {
  495. writew(val, host->ioaddr + reg);
  496. }
  497. static inline void rt_sdhci_writeb(struct rt_sdhci_host *host, rt_uint8_t val, int reg)
  498. {
  499. writeb(val, host->ioaddr + reg);
  500. }
  501. static inline rt_uint32_t rt_sdhci_readl(struct rt_sdhci_host *host, int reg)
  502. {
  503. return readl(host->ioaddr + reg);
  504. }
  505. static inline rt_uint16_t rt_sdhci_readw(struct rt_sdhci_host *host, int reg)
  506. {
  507. return readw(host->ioaddr + reg);
  508. }
  509. static inline rt_uint8_t rt_sdhci_readb(struct rt_sdhci_host *host, int reg)
  510. {
  511. return readb(host->ioaddr + reg);
  512. }
  513. struct rt_sdhci_ops
  514. {
  515. void (*set_clock)(struct rt_sdhci_host *host, unsigned int clock);
  516. void (*set_power)(struct rt_sdhci_host *host, unsigned char mode,
  517. unsigned short vdd);
  518. rt_uint32_t (*irq)(struct rt_sdhci_host *host, rt_uint32_t intmask);
  519. int (*set_dma_mask)(struct rt_sdhci_host *host);
  520. int (*enable_dma)(struct rt_sdhci_host *host);
  521. unsigned int (*get_max_clock)(struct rt_sdhci_host *host);
  522. unsigned int (*get_min_clock)(struct rt_sdhci_host *host);
  523. unsigned int (*get_timeout_clock)(struct rt_sdhci_host *host);
  524. unsigned int (*get_max_timeout_count)(struct rt_sdhci_host *host);
  525. void (*set_timeout)(struct rt_sdhci_host *host,
  526. struct rt_mmcsd_cmd *cmd);
  527. void (*set_bus_width)(struct rt_sdhci_host *host, int width);
  528. unsigned int (*get_ro)(struct rt_sdhci_host *host);
  529. void (*reset)(struct rt_sdhci_host *host, rt_uint8_t mask);
  530. int (*platform_execute_tuning)(struct rt_sdhci_host *host, rt_uint32_t opcode);
  531. void (*set_uhs_signaling)(struct rt_sdhci_host *host, unsigned int uhs);
  532. void (*hw_reset)(struct rt_sdhci_host *host);
  533. void (*card_event)(struct rt_sdhci_host *host);
  534. void (*voltage_switch)(struct rt_sdhci_host *host);
  535. void (*request_done)(struct rt_sdhci_host *host,
  536. struct rt_mmcsd_req *mrq);
  537. };
  538. struct rt_sdhci_host *rt_sdhci_alloc_host(struct rt_device *dev, size_t priv_size);
  539. void rt_sdhci_free_host(struct rt_sdhci_host *host);
  540. static inline void *sdhci_priv(struct rt_sdhci_host *host)
  541. {
  542. return host->private;
  543. }
  544. void rt_sdhci_read_caps(struct rt_sdhci_host *host, const rt_uint16_t *ver,
  545. const rt_uint32_t *caps, const rt_uint32_t *caps1);
  546. int rt_sdhci_setup_host(struct rt_sdhci_host *host);
  547. void rt_sdhci_cleanup_host(struct rt_sdhci_host *host);
  548. int rt_sdhci_set_and_add_host(struct rt_sdhci_host *host);
  549. int rt_sdhci_init_host(struct rt_sdhci_host *host);
  550. void rt_sdhci_uninit_host(struct rt_sdhci_host *host, int dead);
  551. rt_uint16_t rt_sdhci_clk_set(struct rt_sdhci_host *host, unsigned int clock,
  552. unsigned int *actual_clock);
  553. void rt_sdhci_set_clock(struct rt_sdhci_host *host, unsigned int clock);
  554. void rt_sdhci_clk_enable(struct rt_sdhci_host *host, rt_uint16_t clk);
  555. void rt_sdhci_set_power(struct rt_sdhci_host *host, unsigned char mode,unsigned short vdd);
  556. void rt_read_reg(struct rt_sdhci_host* host);
  557. void rt_sdhci_set_power_with_noreg(struct rt_sdhci_host *host, unsigned char mode,
  558. unsigned short vdd);
  559. void rt_sdhci_start_request(struct rt_mmc_host *mmc, struct rt_mmcsd_req *mrq);
  560. int rt_sdhci_start_request_atomic(struct rt_mmc_host *mmc, struct rt_mmcsd_req *mrq);
  561. void rt_sdhci_set_bus_width(struct rt_sdhci_host *host, int width);
  562. void rt_sdhci_reset(struct rt_sdhci_host *host, rt_uint8_t mask);
  563. void rt_sdhci_set_uhs(struct rt_sdhci_host *host, unsigned timing);
  564. int rt_sdhci_execute_tuning(struct rt_mmc_host *mmc, rt_uint32_t opcode);
  565. int __sdhci_execute_tuning(struct rt_sdhci_host *host, rt_uint32_t opcode);
  566. void rt_sdhci_ios_set(struct rt_mmc_host *mmc, struct rt_mmcsd_io_cfg *ios);
  567. int rt_sdhci_start_signal_voltage_switch(struct rt_mmc_host *mmc,
  568. struct rt_mmcsd_io_cfg *ios);
  569. void rt_sdhci_enable_io_irq(struct rt_mmc_host *mmc, int enable);
  570. void rt_sdhci_start_tuning(struct rt_sdhci_host *host);
  571. void rt_sdhci_end_tuning(struct rt_sdhci_host *host);
  572. void rt_sdhci_reset_tuning(struct rt_sdhci_host *host);
  573. void rt_sdhci_send_tuning(struct rt_sdhci_host *host, rt_uint32_t opcode);
  574. void rt_sdhci_abort_tuning(struct rt_sdhci_host *host, rt_uint32_t opcode);
  575. void rt_sdhci_data_irq_timeout(struct rt_sdhci_host *host, rt_bool_t enable);
  576. void rt_sdhci_timeout_set(struct rt_sdhci_host *host, struct rt_mmcsd_cmd *cmd);
  577. void rt_read_reg_debug(struct rt_sdhci_host* host);
  578. #endif /* __RT_SDHCI_HW_H */