sdmmc_001.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * @brief SD/SDIO (MCI) registers and control functions
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2012
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licensor disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #ifndef __SDMMC_001_H_
  32. #define __SDMMC_001_H_
  33. #include "sys_config.h"
  34. #include "cmsis.h"
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /** @defgroup IP_SDMMC_001 IP: SDMMC register block and driver
  39. * @ingroup IP_Drivers
  40. * @{
  41. */
  42. /**
  43. * @brief SD/MMC & SDIO register block structure
  44. */
  45. typedef struct { /*!< SDMMC Structure */
  46. __IO uint32_t CTRL; /*!< Control Register */
  47. __IO uint32_t PWREN; /*!< Power Enable Register */
  48. __IO uint32_t CLKDIV; /*!< Clock Divider Register */
  49. __IO uint32_t CLKSRC; /*!< SD Clock Source Register */
  50. __IO uint32_t CLKENA; /*!< Clock Enable Register */
  51. __IO uint32_t TMOUT; /*!< Timeout Register */
  52. __IO uint32_t CTYPE; /*!< Card Type Register */
  53. __IO uint32_t BLKSIZ; /*!< Block Size Register */
  54. __IO uint32_t BYTCNT; /*!< Byte Count Register */
  55. __IO uint32_t INTMASK; /*!< Interrupt Mask Register */
  56. __IO uint32_t CMDARG; /*!< Command Argument Register */
  57. __IO uint32_t CMD; /*!< Command Register */
  58. __I uint32_t RESP0; /*!< Response Register 0 */
  59. __I uint32_t RESP1; /*!< Response Register 1 */
  60. __I uint32_t RESP2; /*!< Response Register 2 */
  61. __I uint32_t RESP3; /*!< Response Register 3 */
  62. __I uint32_t MINTSTS; /*!< Masked Interrupt Status Register */
  63. __IO uint32_t RINTSTS; /*!< Raw Interrupt Status Register */
  64. __I uint32_t STATUS; /*!< Status Register */
  65. __IO uint32_t FIFOTH; /*!< FIFO Threshold Watermark Register */
  66. __I uint32_t CDETECT; /*!< Card Detect Register */
  67. __I uint32_t WRTPRT; /*!< Write Protect Register */
  68. __IO uint32_t GPIO; /*!< General Purpose Input/Output Register */
  69. __I uint32_t TCBCNT; /*!< Transferred CIU Card Byte Count Register */
  70. __I uint32_t TBBCNT; /*!< Transferred Host to BIU-FIFO Byte Count Register */
  71. __IO uint32_t DEBNCE; /*!< Debounce Count Register */
  72. __IO uint32_t USRID; /*!< User ID Register */
  73. __I uint32_t VERID; /*!< Version ID Register */
  74. __I uint32_t RESERVED0;
  75. __IO uint32_t UHS_REG; /*!< UHS-1 Register */
  76. __IO uint32_t RST_N; /*!< Hardware Reset */
  77. __I uint32_t RESERVED1;
  78. __IO uint32_t BMOD; /*!< Bus Mode Register */
  79. __O uint32_t PLDMND; /*!< Poll Demand Register */
  80. __IO uint32_t DBADDR; /*!< Descriptor List Base Address Register */
  81. __IO uint32_t IDSTS; /*!< Internal DMAC Status Register */
  82. __IO uint32_t IDINTEN; /*!< Internal DMAC Interrupt Enable Register */
  83. __I uint32_t DSCADDR; /*!< Current Host Descriptor Address Register */
  84. __I uint32_t BUFADDR; /*!< Current Buffer Descriptor Address Register */
  85. } IP_SDMMC_001_Type;
  86. /** @brief SDIO DMA descriptor control (des0) register defines
  87. */
  88. #define MCI_DMADES0_OWN (1UL << 31) /*!< DMA owns descriptor bit */
  89. #define MCI_DMADES0_CES (1 << 30) /*!< Card Error Summary bit */
  90. #define MCI_DMADES0_ER (1 << 5) /*!< End of descriptopr ring bit */
  91. #define MCI_DMADES0_CH (1 << 4) /*!< Second address chained bit */
  92. #define MCI_DMADES0_FS (1 << 3) /*!< First descriptor bit */
  93. #define MCI_DMADES0_LD (1 << 2) /*!< Last descriptor bit */
  94. #define MCI_DMADES0_DIC (1 << 1) /*!< Disable interrupt on completion bit */
  95. /** @brief SDIO DMA descriptor size (des1) register defines
  96. */
  97. #define MCI_DMADES1_BS1(x) (x) /*!< Size of buffer 1 */
  98. #define MCI_DMADES1_BS2(x) ((x) << 13) /*!< Size of buffer 2 */
  99. #define MCI_DMADES1_MAXTR 4096 /*!< Max transfer size per buffer */
  100. /** @brief SDIO control register defines
  101. */
  102. #define MCI_CTRL_USE_INT_DMAC (1 << 25) /*!< Use internal DMA */
  103. #define MCI_CTRL_CARDV_MASK (0x7 << 16) /*!< SD_VOLT[2:0} pins output state mask */
  104. #define MCI_CTRL_CEATA_INT_EN (1 << 11) /*!< Enable CE-ATA interrupts */
  105. #define MCI_CTRL_SEND_AS_CCSD (1 << 10) /*!< Send auto-stop */
  106. #define MCI_CTRL_SEND_CCSD (1 << 9) /*!< Send CCSD */
  107. #define MCI_CTRL_ABRT_READ_DATA (1 << 8) /*!< Abort read data */
  108. #define MCI_CTRL_SEND_IRQ_RESP (1 << 7) /*!< Send auto-IRQ response */
  109. #define MCI_CTRL_READ_WAIT (1 << 6) /*!< Assert read-wait for SDIO */
  110. #define MCI_CTRL_INT_ENABLE (1 << 4) /*!< Global interrupt enable */
  111. #define MCI_CTRL_DMA_RESET (1 << 2) /*!< Reset internal DMA */
  112. #define MCI_CTRL_FIFO_RESET (1 << 1) /*!< Reset data FIFO pointers */
  113. #define MCI_CTRL_RESET (1 << 0) /*!< Reset controller */
  114. /** @brief SDIO Power Enable register defines
  115. */
  116. #define MCI_POWER_ENABLE 0x1 /*!< Enable slot power signal (SD_POW) */
  117. /** @brief SDIO Clock divider register defines
  118. */
  119. #define MCI_CLOCK_DIVIDER(dn, d2) ((d2) << ((dn) * 8)) /*!< Set cklock divider */
  120. /** @brief SDIO Clock source register defines
  121. */
  122. #define MCI_CLKSRC_CLKDIV0 0
  123. #define MCI_CLKSRC_CLKDIV1 1
  124. #define MCI_CLKSRC_CLKDIV2 2
  125. #define MCI_CLKSRC_CLKDIV3 3
  126. #define MCI_CLK_SOURCE(clksrc) (clksrc) /*!< Set cklock divider source */
  127. /** @brief SDIO Clock Enable register defines
  128. */
  129. #define MCI_CLKEN_LOW_PWR (1 << 16) /*!< Enable clock idle for slot */
  130. #define MCI_CLKEN_ENABLE (1 << 0) /*!< Enable slot clock */
  131. /** @brief SDIO time-out register defines
  132. */
  133. #define MCI_TMOUT_DATA(clks) ((clks) << 8) /*!< Data timeout clocks */
  134. #define MCI_TMOUT_DATA_MSK 0xFFFFFF00
  135. #define MCI_TMOUT_RESP(clks) ((clks) & 0xFF) /*!< Response timeout clocks */
  136. #define MCI_TMOUT_RESP_MSK 0xFF
  137. /** @brief SDIO card-type register defines
  138. */
  139. #define MCI_CTYPE_8BIT (1 << 16) /*!< Enable 4-bit mode */
  140. #define MCI_CTYPE_4BIT (1 << 0) /*!< Enable 8-bit mode */
  141. /** @brief SDIO Interrupt status & mask register defines
  142. */
  143. #define MCI_INT_SDIO (1 << 16) /*!< SDIO interrupt */
  144. #define MCI_INT_EBE (1 << 15) /*!< End-bit error */
  145. #define MCI_INT_ACD (1 << 14) /*!< Auto command done */
  146. #define MCI_INT_SBE (1 << 13) /*!< Start bit error */
  147. #define MCI_INT_HLE (1 << 12) /*!< Hardware locked error */
  148. #define MCI_INT_FRUN (1 << 11) /*!< FIFO overrun/underrun error */
  149. #define MCI_INT_HTO (1 << 10) /*!< Host data starvation error */
  150. #define MCI_INT_DTO (1 << 9) /*!< Data timeout error */
  151. #define MCI_INT_RTO (1 << 8) /*!< Response timeout error */
  152. #define MCI_INT_DCRC (1 << 7) /*!< Data CRC error */
  153. #define MCI_INT_RCRC (1 << 6) /*!< Response CRC error */
  154. #define MCI_INT_RXDR (1 << 5) /*!< RX data ready */
  155. #define MCI_INT_TXDR (1 << 4) /*!< TX data needed */
  156. #define MCI_INT_DATA_OVER (1 << 3) /*!< Data transfer over */
  157. #define MCI_INT_CMD_DONE (1 << 2) /*!< Command done */
  158. #define MCI_INT_RESP_ERR (1 << 1) /*!< Command response error */
  159. #define MCI_INT_CD (1 << 0) /*!< Card detect */
  160. /** @brief SDIO Command register defines
  161. */
  162. #define MCI_CMD_START (1UL << 31) /*!< Start command */
  163. #define MCI_CMD_VOLT_SWITCH (1 << 28) /*!< Voltage switch bit */
  164. #define MCI_CMD_BOOT_MODE (1 << 27) /*!< Boot mode */
  165. #define MCI_CMD_DISABLE_BOOT (1 << 26) /*!< Disable boot */
  166. #define MCI_CMD_EXPECT_BOOT_ACK (1 << 25) /*!< Expect boot ack */
  167. #define MCI_CMD_ENABLE_BOOT (1 << 24) /*!< Enable boot */
  168. #define MCI_CMD_CCS_EXP (1 << 23) /*!< CCS expected */
  169. #define MCI_CMD_CEATA_RD (1 << 22) /*!< CE-ATA read in progress */
  170. #define MCI_CMD_UPD_CLK (1 << 21) /*!< Update clock register only */
  171. #define MCI_CMD_INIT (1 << 15) /*!< Send init sequence */
  172. #define MCI_CMD_STOP (1 << 14) /*!< Stop/abort command */
  173. #define MCI_CMD_PRV_DAT_WAIT (1 << 13) /*!< Wait before send */
  174. #define MCI_CMD_SEND_STOP (1 << 12) /*!< Send auto-stop */
  175. #define MCI_CMD_STRM_MODE (1 << 11) /*!< Stream transfer mode */
  176. #define MCI_CMD_DAT_WR (1 << 10) /*!< Read(0)/Write(1) selection */
  177. #define MCI_CMD_DAT_EXP (1 << 9) /*!< Data expected */
  178. #define MCI_CMD_RESP_CRC (1 << 8) /*!< Check response CRC */
  179. #define MCI_CMD_RESP_LONG (1 << 7) /*!< Response length */
  180. #define MCI_CMD_RESP_EXP (1 << 6) /*!< Response expected */
  181. #define MCI_CMD_INDX(n) ((n) & 0x1F)
  182. /** @brief SDIO status register definess
  183. */
  184. #define MCI_STS_GET_FCNT(x) (((x) >> 17) & 0x1FF)
  185. /** @brief SDIO FIFO threshold defines
  186. */
  187. #define MCI_FIFOTH_TX_WM(x) ((x) & 0xFFF)
  188. #define MCI_FIFOTH_RX_WM(x) (((x) & 0xFFF) << 16)
  189. #define MCI_FIFOTH_DMA_MTS_1 (0UL << 28)
  190. #define MCI_FIFOTH_DMA_MTS_4 (1UL << 28)
  191. #define MCI_FIFOTH_DMA_MTS_8 (2UL << 28)
  192. #define MCI_FIFOTH_DMA_MTS_16 (3UL << 28)
  193. #define MCI_FIFOTH_DMA_MTS_32 (4UL << 28)
  194. #define MCI_FIFOTH_DMA_MTS_64 (5UL << 28)
  195. #define MCI_FIFOTH_DMA_MTS_128 (6UL << 28)
  196. #define MCI_FIFOTH_DMA_MTS_256 (7UL << 28)
  197. /** @brief Bus mode register defines
  198. */
  199. #define MCI_BMOD_PBL1 (0 << 8) /*!< Burst length = 1 */
  200. #define MCI_BMOD_PBL4 (1 << 8) /*!< Burst length = 4 */
  201. #define MCI_BMOD_PBL8 (2 << 8) /*!< Burst length = 8 */
  202. #define MCI_BMOD_PBL16 (3 << 8) /*!< Burst length = 16 */
  203. #define MCI_BMOD_PBL32 (4 << 8) /*!< Burst length = 32 */
  204. #define MCI_BMOD_PBL64 (5 << 8) /*!< Burst length = 64 */
  205. #define MCI_BMOD_PBL128 (6 << 8) /*!< Burst length = 128 */
  206. #define MCI_BMOD_PBL256 (7 << 8) /*!< Burst length = 256 */
  207. #define MCI_BMOD_DE (1 << 7) /*!< Enable internal DMAC */
  208. #define MCI_BMOD_DSL(len) ((len) << 2) /*!< Descriptor skip length */
  209. #define MCI_BMOD_FB (1 << 1) /*!< Fixed bursts */
  210. #define MCI_BMOD_SWR (1 << 0) /*!< Software reset of internal registers */
  211. /** @brief Commonly used definitions
  212. */
  213. #define SD_FIFO_SZ 32 /*!< Size of SDIO FIFOs (32-bit wide) */
  214. /** Function prototype for SD interface IRQ callback */
  215. typedef uint32_t (*MCI_IRQ_CB_FUNC_T)(uint32_t);
  216. /** Function prototype for SD detect and write protect status check */
  217. typedef int32_t (*PSCHECK_FUNC_T)(void);
  218. /** Function prototype for SD slot power enable or slot reset */
  219. typedef void (*PS_POWER_FUNC_T)(int32_t enable);
  220. /** @brief SDIO chained DMA descriptor
  221. */
  222. typedef struct {
  223. volatile uint32_t des0; /*!< Control and status */
  224. volatile uint32_t des1; /*!< Buffer size(s) */
  225. volatile uint32_t des2; /*!< Buffer address pointer 1 */
  226. volatile uint32_t des3; /*!< Buffer address pointer 2 */
  227. } pSDMMC_DMA_Type;
  228. /** @brief SDIO device type
  229. */
  230. typedef struct _sdif_device {
  231. // MCI_IRQ_CB_FUNC_T irq_cb;
  232. pSDMMC_DMA_Type mci_dma_dd[1 + (0x10000 / MCI_DMADES1_MAXTR)];
  233. // uint32_t sdio_clk_rate;
  234. // uint32_t sdif_slot_clk_rate;
  235. // int32_t clock_enabled;
  236. } sdif_device;
  237. /**
  238. * @brief Initializes the MCI card controller
  239. * @param pSDMMC Pointer to IP_SDMMC_001_Type structure
  240. * @return None
  241. */
  242. void IP_SDMMC_Init(IP_SDMMC_001_Type *pSDMMC);
  243. /**
  244. * @brief Close the MCI
  245. * @param pSDMMC : Pointer to IP_SDMMC_001_Type structure
  246. * @return None
  247. */
  248. void IP_SDMMC_DeInit(IP_SDMMC_001_Type *pSDMMC);
  249. /**
  250. * @brief Set block size for transfer
  251. * @param pSDMMC : Pointer to IP_SDMMC_001_Type structure
  252. * @param bytes : block size in bytes
  253. * @return None
  254. */
  255. void IP_SDMMC_SetBlkSize(IP_SDMMC_001_Type *pSDMMC, uint32_t bytes);
  256. /**
  257. * @brief Reset card in slot
  258. * @param pSDMMC : Pointer to IP_SDMMC_001_Type structure
  259. * @param reset : Sets SD_RST to passed state
  260. * @return None
  261. * Reset card in slot, must manually de-assert reset after assertion
  262. * (Uses SD_RST pin, set per reset parameter state)
  263. */
  264. void IP_SDMMC_Reset(IP_SDMMC_001_Type *pSDMMC, int32_t reset);
  265. /**
  266. * @brief Enable or disable slot power
  267. * @param pSDMMC : Pointer to IP_SDMMC_001_Type structure
  268. * @param enable : !0 to enable, or 0 to disable
  269. * @return None
  270. * Enable or disable slot power, !0 = enable slot power
  271. * (Uses SD_POW pin, set to high or low based on enable parameter state)
  272. */
  273. void IP_SDMMC_PowerOnOff(IP_SDMMC_001_Type *pSDMMC, int32_t enable);
  274. /**
  275. * @brief Detect if write protect is enabled
  276. * @param pSDMMC : Pointer to IP_SDMMC_001_Type structure
  277. * @return Returns 1 if card is write protected, otherwise 0
  278. * Detect if write protect is enabled
  279. * (uses SD_WP pin, returns 1 if card is write protected)
  280. */
  281. int32_t IP_SDMMC_CardWpOn(IP_SDMMC_001_Type *pSDMMC);
  282. /**
  283. * @brief Detect if an SD card is inserted
  284. * @param pSDMMC : Pointer to IP_SDMMC_001_Type structure
  285. * @return Returns 0 if a card is detected, otherwise 1
  286. * Detect if an SD card is inserted
  287. * (uses SD_CD pin, returns 0 on card detect)
  288. */
  289. int32_t IP_SDMMC_CardNDetect(IP_SDMMC_001_Type *pSDMMC);
  290. /**
  291. * @brief Function to send command to Card interface unit (CIU)
  292. * @param pSDMMC : Pointer to IP_SDMMC_001_Type structure
  293. * @param cmd : Command with all flags set
  294. * @param arg : Argument for the command
  295. * @return TRUE on times-out, otherwise FALSE
  296. */
  297. int32_t IP_SDMMC_SendCmd(IP_SDMMC_001_Type *pSDMMC, uint32_t cmd, uint32_t arg);
  298. /**
  299. * @brief Read the response from the last command
  300. * @param pSDMMC : Pointer to IP_SDMMC_001_Type structure
  301. * @param resp : Pointer to response array to fill
  302. * @return None
  303. */
  304. void IP_SDMMC_GetResponse(IP_SDMMC_001_Type *pSDMMC, uint32_t *resp);
  305. /**
  306. * @brief Sets the SD bus clock speed
  307. * @param pSDMMC : Pointer to IP_SDMMC_001_Type structure
  308. * @param clk_rate : Input clock rate into the IP block
  309. * @param speed : Desired clock speed to the card
  310. * @return None
  311. */
  312. void IP_SDMMC_SetClock(IP_SDMMC_001_Type *pSDMMC, uint32_t clk_rate, uint32_t speed);
  313. /**
  314. * @brief Function to set card type
  315. * @param pSDMMC : Pointer to IP_SDMMC_001_Type structure
  316. * @param ctype : card type
  317. * @return None
  318. */
  319. void IP_SDMMC_SetCardType(IP_SDMMC_001_Type *pSDMMC, uint32_t ctype);
  320. /**
  321. * @brief Function to clear interrupt & FIFOs
  322. * @param pSDMMC : Pointer to IP_SDMMC_001_Type structure
  323. * @return None
  324. */
  325. void IP_SDMMC_SetClearIntFifo(IP_SDMMC_001_Type *pSDMMC);
  326. /**
  327. * @brief Returns the raw SD interface interrupt status
  328. * @param pSDMMC : Pointer to IP_SDMMC_001_Type structure
  329. * @return Raw interrupt status of Or'ed values MCI_INT_*
  330. */
  331. uint32_t IP_SDMMC_GetRawIntStatus(IP_SDMMC_001_Type *pSDMMC);
  332. /**
  333. * @brief Sets the raw SD interface interrupt status
  334. * @param pSDMMC : Pointer to IP_SDMMC_001_Type structure
  335. * @param iVal : Raw interrupts to set, Or'ed values MCI_INT_*
  336. * @return None
  337. */
  338. void IP_SDMMC_SetRawIntStatus(IP_SDMMC_001_Type *pSDMMC, uint32_t iVal);
  339. /**
  340. * @brief Sets the SD interface interrupt mask
  341. * @param pSDMMC : Pointer to IP_SDMMC_001_Type structure
  342. * @param iVal : Interrupts to enable, Or'ed values MCI_INT_*
  343. * @return None
  344. */
  345. void IP_SDMMC_SetIntMask(IP_SDMMC_001_Type *pSDMMC, uint32_t iVal);
  346. /**
  347. * @brief Setup DMA descriptors
  348. * @param pSDMMC : Pointer to IP_SDMMC_001_Type structure
  349. * @param psdif_dev : SD interface device
  350. * @param addr : Address of buffer (source or destination)
  351. * @param size : size of buffer in bytes (64K max)
  352. * @return None
  353. */
  354. void IP_SDMMC_DmaSetup(IP_SDMMC_001_Type *pSDMMC, sdif_device *psdif_dev, uint32_t addr, uint32_t size);
  355. /* Sets the transfer block size */
  356. void IP_SDMMC_SetBlockSize(IP_SDMMC_001_Type *pSDMMC, uint32_t blk_size);
  357. /**
  358. * @}
  359. */
  360. #ifdef __cplusplus
  361. }
  362. #endif
  363. #endif /* __SDMMC_001_H_ */