drv_sdio.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * File : drv_sdio.h
  3. * Copyright (c) 2006-2018, RT-Thread Development Team
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2019-07-29 zdzn first version
  10. */
  11. #ifndef __DRV_SDIO_H__
  12. #define __DRV_SDIO_H__
  13. #include <rtthread.h>
  14. #define MMC0_BASE_ADDR 0x20300000
  15. struct raspi_mmc
  16. {
  17. volatile rt_uint32_t arg2_reg; /* (0x000) */
  18. volatile rt_uint32_t blksizecnt_reg; /* (0x004) */
  19. volatile rt_uint32_t arg1_reg; /* (0x008) */
  20. volatile rt_uint32_t cmdtm_reg; /* (0x00C) */
  21. volatile rt_uint32_t resp0_reg; /* (0x010) */
  22. volatile rt_uint32_t resp1_reg; /* (0x014) */
  23. volatile rt_uint32_t resp2_reg; /* (0x018) */
  24. volatile rt_uint32_t resp3_reg; /* (0x01C) */
  25. volatile rt_uint32_t data_reg; /* (0x020) */
  26. volatile rt_uint32_t status_reg; /* (0x024) */
  27. volatile rt_uint32_t control0_reg; /* (0x028) */
  28. volatile rt_uint32_t control1_reg; /* (0x02C) */
  29. volatile rt_uint32_t interrupt_reg; /* (0x030) */
  30. volatile rt_uint32_t irpt_mask_reg; /* (0x034) */
  31. volatile rt_uint32_t irpt_en_reg; /* (0x038) */
  32. volatile rt_uint32_t control2_reg; /* (0x03C) */
  33. volatile rt_uint32_t reserved1[4]; /* (0x040) */
  34. volatile rt_uint32_t force_irpt_reg; /* (0x050) */
  35. volatile rt_uint32_t reserved2[7]; /* (0x054) */
  36. volatile rt_uint32_t boot_timeout_reg; /* (0x070) */
  37. volatile rt_uint32_t deg_sel_reg; /* (0x074) */
  38. volatile rt_uint32_t reserved3[2]; /* (0x078) */
  39. volatile rt_uint32_t exrdfifo_cfg_reg; /* (0x080) */
  40. volatile rt_uint32_t exrdfifo_cn_reg; /* (0x084) */
  41. volatile rt_uint32_t tune_step_reg; /* (0x088) */
  42. volatile rt_uint32_t tune_step_std_reg; /* (0x08C) */
  43. volatile rt_uint32_t tune_step_ddr_reg; /* (0x090) */
  44. volatile rt_uint32_t reserved4[23]; /* (0x094) */
  45. volatile rt_uint32_t spi_int_reg; /* (0x0f0) */
  46. volatile rt_uint32_t reserved5[2]; /* (0x0f4) */
  47. volatile rt_uint32_t slotisr_ver_reg; /* (0x0fC) */
  48. };
  49. typedef struct raspi_mmc *raspi_mmc_t;
  50. #define MMC0 ((tina_mmc_t)MMC0_BASE_ADDR)
  51. #define BIT(x) (1<<(x))
  52. /* Struct for Intrrrupt Information */
  53. #define SDXC_CmdDone BIT(0)
  54. #define SDXC_DataDone BIT(1)
  55. #define SDXC_BlockGap BIT(2)
  56. #define SDXC_WriteRdy BIT(4)
  57. #define SDXC_ReadRdy BIT(5)
  58. #define SDXC_Card BIT(8)
  59. #define SDXC_Retune BIT(12)
  60. #define SDXC_BootAck BIT(13)
  61. #define SDXC_EndBoot BIT(14)
  62. #define SDXC_Err BIT(15)
  63. #define SDXC_CTOErr BIT(16)
  64. #define SDXC_CCRCErr BIT(17)
  65. #define SDXC_CENDErr BIT(18)
  66. #define SDXC_CBADErr BIT(19)
  67. #define SDXC_DTOErr BIT(20)
  68. #define SDXC_DCRCErr BIT(21)
  69. #define SDXC_DENDErr BIT(22)
  70. #define SDXC_ACMDErr BIT(24)
  71. /*
  72. SD CMD reg
  73. REG[0-5] : Cmd ID
  74. REG[6] : Has response
  75. REG[7] : Long response
  76. REG[8] : Check response CRC
  77. REG[9] : Has data
  78. REG[10] : Write
  79. REG[11] : Steam mode
  80. REG[12] : Auto stop
  81. REG[13] : Wait previous over
  82. REG[14] : About cmd
  83. REG[15] : Send initialization
  84. REG[21] : Update clock
  85. REG[31] : Load cmd
  86. */
  87. #define SDXC_BLKCNT_EN BIT(1)
  88. #define SDXC_AUTO_CMD12_EN BIT(2)
  89. #define SDXC_AUTO_CMD23_EN BIT(3)
  90. #define SDXC_DAT_DIR BIT(4) //from card to host
  91. #define SDXC_MULTI_BLOCK BIT(5)
  92. #define SDXC_CMD_RSPNS_136 BIT(16)
  93. #define SDXC_CMD_RSPNS_48 BIT(17)
  94. #define SDXC_CMD_RSPNS_48busy BIT(16)|BIT(17)
  95. #define SDXC_CHECK_CRC_CMD BIT(19)
  96. #define SDXC_CMD_IXCHK_EN BIT(20)
  97. #define SDXC_CMD_ISDATA BIT(21)
  98. #define SDXC_CMD_SUSPEND BIT(22)
  99. #define SDXC_CMD_RESUME BIT(23)
  100. #define SDXC_CMD_ABORT BIT(23)|BIT(22)
  101. //#define SDXC_CHECK_CRC_CMD BIT(19)
  102. //
  103. //#define SDXC_RESPONSE_CMD BIT(6)
  104. //#define SDXC_LONG_RESPONSE_CMD BIT(7)
  105. //#define SDXC_CHECK_CRC_CMD BIT(8)
  106. //#define SDXC_HAS_DATA_CMD BIT(9)
  107. //#define SDXC_WRITE_CMD BIT(10)
  108. //#define SDXC_STEAM_CMD BIT(11)
  109. //#define SDXC_AUTO_STOP_CMD BIT(12)
  110. //#define SDXC_WAIT_OVER_CMD BIT(13)
  111. //#define SDXC_ABOUT_CMD BIT(14)
  112. //#define SDXC_SEND_INIT_CMD BIT(15)
  113. //#define SDXC_UPDATE_CLOCK_CMD BIT(21)
  114. //#define SDXC_LOAD_CMD BIT(31)
  115. /*
  116. SD status reg
  117. REG[0] : FIFO_RX_LEVEL
  118. REG[1] : FIFO_TX_LEVEL
  119. REG[2] : FIFO_EMPTY
  120. REG[3] : FIFO_FULL
  121. REG[4-7] : FSM_STA
  122. REG[8] : CARD_PRESENT
  123. REG[9] : CARD_BUSY
  124. REG[10] : FSM_BUSY
  125. REG[11-16]: RESP_IDX
  126. REG[17-21]: FIFO_LEVEL
  127. REG[31] : DMA_REQ
  128. */
  129. #define SDXC_CMD_INHIBIT BIT(0)
  130. #define SDXC_DAT_INHIBIT BIT(1)
  131. #define SDXC_DAT_ACTIVE BIT(2)
  132. #define SDXC_WRITE_TRANSFER BIT(8)
  133. #define SDXC_READ_TRANSFER BIT(9)
  134. //
  135. //
  136. //#define SDXC_FIFO_RX_LEVEL BIT(0)
  137. //#define SDXC_FIFO_TX_LEVEL BIT(1)
  138. //#define SDXC_FIFO_EMPTY BIT(2)
  139. //#define SDXC_FIFO_FULL BIT(3)
  140. //#define SDXC_CARD_PRESENT BIT(8)
  141. //#define SDXC_CARD_BUSY BIT(9)
  142. //#define SDXC_FSM_BUSY BIT(10)
  143. //#define SDXC_DMA_REQ BIT(31)
  144. struct mmc_des_v4p1
  145. {
  146. rt_uint32_t : 1,
  147. dic : 1, /* disable interrupt on completion */
  148. last_des : 1, /* 1-this data buffer is the last buffer */
  149. first_des : 1, /* 1-data buffer is the first buffer,0-data buffer contained in the next descriptor is 1st buffer */
  150. des_chain : 1, /* 1-the 2nd address in the descriptor is the next descriptor address */
  151. end_of_ring : 1, /* 1-last descriptor flag when using dual data buffer in descriptor */
  152. : 24,
  153. card_err_sum : 1, /* transfer error flag */
  154. own : 1; /* des owner:1-idma owns it, 0-host owns it */
  155. #define SDXC_DES_NUM_SHIFT 12 /* smhc2!! */
  156. #define SDXC_DES_BUFFER_MAX_LEN (1 << SDXC_DES_NUM_SHIFT)
  157. rt_uint32_t data_buf1_sz : 16,
  158. data_buf2_sz : 16;
  159. rt_uint32_t buf_addr_ptr1;
  160. rt_uint32_t buf_addr_ptr2;
  161. };
  162. struct sdhci_cmd_t
  163. {
  164. rt_uint32_t cmdidx;
  165. rt_uint32_t cmdarg;
  166. //const char* name;
  167. // rt_uint32_t code;
  168. rt_uint32_t resptype;
  169. // rt_uint8_t rca;
  170. // rt_uint32_t delay;
  171. rt_uint32_t response[4];
  172. };
  173. struct sdhci_data_t
  174. {
  175. rt_uint8_t * buf;
  176. rt_uint32_t flag;
  177. rt_uint32_t blksz;
  178. rt_uint32_t blkcnt;
  179. };
  180. struct sdhci_t
  181. {
  182. char * name;
  183. rt_uint32_t voltages;
  184. rt_uint32_t width;
  185. rt_uint32_t clock;
  186. rt_err_t removeable;
  187. void * sdcard;
  188. rt_err_t (*detect)(struct sdhci_t * sdhci);
  189. rt_err_t (*setwidth)(struct sdhci_t * sdhci, rt_uint32_t width);
  190. rt_err_t (*setclock)(struct sdhci_t * sdhci, rt_uint32_t clock);
  191. rt_err_t (*transfer)(struct sdhci_t * sdhci, struct sdhci_cmd_t * cmd, struct sdhci_data_t * dat);
  192. void * priv;
  193. };
  194. struct sdhci_pdata_t
  195. {
  196. rt_uint32_t virt;
  197. };
  198. // EMMC command flags
  199. #define CMD_TYPE_NORMAL 0x00000000
  200. #define CMD_TYPE_SUSPEND 0x00400000
  201. #define CMD_TYPE_RESUME 0x00800000
  202. #define CMD_TYPE_ABORT 0x00c00000
  203. #define CMD_IS_DATA 0x00200000
  204. #define CMD_IXCHK_EN 0x00100000
  205. #define CMD_CRCCHK_EN 0x00080000
  206. #define CMD_RSPNS_NO 0x00000000
  207. #define CMD_RSPNS_136 0x00010000
  208. #define CMD_RSPNS_48 0x00020000
  209. #define CMD_RSPNS_48B 0x00030000
  210. #define TM_MULTI_BLOCK 0x00000020
  211. #define TM_DAT_DIR_HC 0x00000000
  212. #define TM_DAT_DIR_CH 0x00000010
  213. #define TM_AUTO_CMD23 0x00000008
  214. #define TM_AUTO_CMD12 0x00000004
  215. #define TM_BLKCNT_EN 0x00000002
  216. #define TM_MULTI_DATA (CMD_IS_DATA|TM_MULTI_BLOCK|TM_BLKCNT_EN)
  217. // Response types.
  218. // Note that on the PI, the index and CRC are dropped, leaving 32 bits in RESP0.
  219. #define RESP_NO 0 // No response
  220. //#define RESP_R1 1 // 48 RESP0 contains card status
  221. #define RESP_R1b 11 // 48 RESP0 contains card status, data line indicates busy
  222. #define RESP_R2I 2 // 136 RESP0..3 contains 128 bit CID shifted down by 8 bits as no CRC
  223. #define RESP_R2S 12 // 136 RESP0..3 contains 128 bit CSD shifted down by 8 bits as no CRC
  224. //#define RESP_R3 3 // 48 RESP0 contains OCR register
  225. //#define RESP_R6 6 // 48 RESP0 contains RCA and status bits 23,22,19,12:0
  226. //#define RESP_R7 7 // 48 RESP0 contains voltage acceptance and check pattern
  227. #define RCA_NO 1
  228. #define RCA_YES 2
  229. // INTERRUPT register settings
  230. #define INT_AUTO_ERROR 0x01000000
  231. #define INT_DATA_END_ERR 0x00400000
  232. #define INT_DATA_CRC_ERR 0x00200000
  233. #define INT_DATA_TIMEOUT 0x00100000
  234. #define INT_INDEX_ERROR 0x00080000
  235. #define INT_END_ERROR 0x00040000
  236. #define INT_CRC_ERROR 0x00020000
  237. #define INT_CMD_TIMEOUT 0x00010000
  238. #define INT_ERR 0x00008000
  239. #define INT_ENDBOOT 0x00004000
  240. #define INT_BOOTACK 0x00002000
  241. #define INT_RETUNE 0x00001000
  242. #define INT_CARD 0x00000100
  243. #define INT_READ_RDY 0x00000020
  244. #define INT_WRITE_RDY 0x00000010
  245. #define INT_BLOCK_GAP 0x00000004
  246. #define INT_DATA_DONE 0x00000002
  247. #define INT_CMD_DONE 0x00000001
  248. #define INT_ERROR_MASK (INT_CRC_ERROR|INT_END_ERROR|INT_INDEX_ERROR| \
  249. INT_DATA_TIMEOUT|INT_DATA_CRC_ERR|INT_DATA_END_ERR| \
  250. INT_ERR|INT_AUTO_ERROR)
  251. #define INT_ALL_MASK (INT_CMD_DONE|INT_DATA_DONE|INT_READ_RDY|INT_WRITE_RDY|INT_ERROR_MASK)
  252. #define EMMC_ARG2 (0x00)
  253. #define EMMC_BLKSIZECNT (0x04)
  254. #define EMMC_ARG1 (0x08)
  255. #define EMMC_CMDTM (0x0c)
  256. #define EMMC_RESP0 (0x10)
  257. #define EMMC_RESP1 (0x14)
  258. #define EMMC_RESP2 (0x18)
  259. #define EMMC_RESP3 (0x1c)
  260. #define EMMC_DATA (0x20)
  261. #define EMMC_STATUS (0x24)
  262. #define EMMC_CONTROL0 (0x28)
  263. #define EMMC_CONTROL1 (0x2c)
  264. #define EMMC_INTERRUPT (0x30)
  265. #define EMMC_IRPT_MASK (0x34)
  266. #define EMMC_IRPT_EN (0x38)
  267. #define EMMC_CONTROL2 (0x3c)
  268. #define EMMC_BOOT_TIMEOUT (0x70)
  269. #define EMMC_EXRDFIFO_EN (0x84)
  270. #define EMMC_SPI_INT_SPT (0xf0)
  271. #define EMMC_SLOTISR_VER (0xfc)
  272. // CONTROL register settings
  273. #define C0_SPI_MODE_EN 0x00100000
  274. #define C0_HCTL_HS_EN 0x00000004
  275. #define C0_HCTL_DWITDH 0x00000002
  276. #define C1_SRST_DATA 0x04000000
  277. #define C1_SRST_CMD 0x02000000
  278. #define C1_SRST_HC 0x01000000
  279. #define C1_TOUNIT_DIS 0x000f0000
  280. #define C1_TOUNIT_MAX 0x000e0000
  281. #define C1_CLK_GENSEL 0x00000020
  282. #define C1_CLK_EN 0x00000004
  283. #define C1_CLK_STABLE 0x00000002
  284. #define C1_CLK_INTLEN 0x00000001
  285. #define FREQ_SETUP 400000 // 400 Khz
  286. #define FREQ_NORMAL 25000000 // 25 Mhz
  287. // SLOTISR_VER values
  288. #define HOST_SPEC_NUM 0x00ff0000
  289. #define HOST_SPEC_NUM_SHIFT 16
  290. #define HOST_SPEC_V3 2
  291. #define HOST_SPEC_V2 1
  292. #define HOST_SPEC_V1 0
  293. // STATUS register settings
  294. #define SR_DAT_LEVEL1 0x1e000000
  295. #define SR_CMD_LEVEL 0x01000000
  296. #define SR_DAT_LEVEL0 0x00f00000
  297. #define SR_DAT3 0x00800000
  298. #define SR_DAT2 0x00400000
  299. #define SR_DAT1 0x00200000
  300. #define SR_DAT0 0x00100000
  301. #define SR_WRITE_PROT 0x00080000 // From SDHC spec v2, BCM says reserved
  302. #define SR_READ_AVAILABLE 0x00000800 // ???? undocumented
  303. #define SR_WRITE_AVAILABLE 0x00000400 // ???? undocumented
  304. #define SR_READ_TRANSFER 0x00000200
  305. #define SR_WRITE_TRANSFER 0x00000100
  306. #define SR_DAT_ACTIVE 0x00000004
  307. #define SR_DAT_INHIBIT 0x00000002
  308. #define SR_CMD_INHIBIT 0x00000001
  309. #endif