fsl_usdhc.h 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2021 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _FSL_USDHC_H_
  9. #define _FSL_USDHC_H_
  10. #include "fsl_common.h"
  11. /*!
  12. * @addtogroup usdhc
  13. * @{
  14. */
  15. /******************************************************************************
  16. * Definitions.
  17. *****************************************************************************/
  18. /*! @name Driver version */
  19. /*@{*/
  20. /*! @brief Driver version 2.8.1. */
  21. #define FSL_USDHC_DRIVER_VERSION (MAKE_VERSION(2U, 8U, 1U))
  22. /*@}*/
  23. /*! @brief Maximum block count can be set one time */
  24. #define USDHC_MAX_BLOCK_COUNT (USDHC_BLK_ATT_BLKCNT_MASK >> USDHC_BLK_ATT_BLKCNT_SHIFT)
  25. /*! @brief USDHC scatter gather feature control macro */
  26. #ifndef FSL_USDHC_ENABLE_SCATTER_GATHER_TRANSFER
  27. #define FSL_USDHC_ENABLE_SCATTER_GATHER_TRANSFER 0U
  28. #endif
  29. /*! @brief Enum _usdhc_status. USDHC status. */
  30. enum
  31. {
  32. kStatus_USDHC_BusyTransferring = MAKE_STATUS(kStatusGroup_USDHC, 0U), /*!< Transfer is on-going. */
  33. kStatus_USDHC_PrepareAdmaDescriptorFailed = MAKE_STATUS(kStatusGroup_USDHC, 1U), /*!< Set DMA descriptor failed. */
  34. kStatus_USDHC_SendCommandFailed = MAKE_STATUS(kStatusGroup_USDHC, 2U), /*!< Send command failed. */
  35. kStatus_USDHC_TransferDataFailed = MAKE_STATUS(kStatusGroup_USDHC, 3U), /*!< Transfer data failed. */
  36. kStatus_USDHC_DMADataAddrNotAlign = MAKE_STATUS(kStatusGroup_USDHC, 4U), /*!< Data address not aligned. */
  37. kStatus_USDHC_ReTuningRequest = MAKE_STATUS(kStatusGroup_USDHC, 5U), /*!< Re-tuning request. */
  38. kStatus_USDHC_TuningError = MAKE_STATUS(kStatusGroup_USDHC, 6U), /*!< Tuning error. */
  39. kStatus_USDHC_NotSupport = MAKE_STATUS(kStatusGroup_USDHC, 7U), /*!< Not support. */
  40. kStatus_USDHC_TransferDataComplete = MAKE_STATUS(kStatusGroup_USDHC, 8U), /*!< Transfer data complete. */
  41. kStatus_USDHC_SendCommandSuccess = MAKE_STATUS(kStatusGroup_USDHC, 9U), /*!< Transfer command complete. */
  42. kStatus_USDHC_TransferDMAComplete = MAKE_STATUS(kStatusGroup_USDHC, 10U), /*!< Transfer DMA complete. */
  43. };
  44. /*! @brief Enum _usdhc_capability_flag. Host controller capabilities flag mask.
  45. * @anchor _usdhc_capability_flag
  46. */
  47. enum
  48. {
  49. kUSDHC_SupportAdmaFlag = USDHC_HOST_CTRL_CAP_ADMAS_MASK, /*!< Support ADMA. */
  50. kUSDHC_SupportHighSpeedFlag = USDHC_HOST_CTRL_CAP_HSS_MASK, /*!< Support high-speed. */
  51. kUSDHC_SupportDmaFlag = USDHC_HOST_CTRL_CAP_DMAS_MASK, /*!< Support DMA. */
  52. kUSDHC_SupportSuspendResumeFlag = USDHC_HOST_CTRL_CAP_SRS_MASK, /*!< Support suspend/resume. */
  53. kUSDHC_SupportV330Flag = USDHC_HOST_CTRL_CAP_VS33_MASK, /*!< Support voltage 3.3V. */
  54. kUSDHC_SupportV300Flag = USDHC_HOST_CTRL_CAP_VS30_MASK, /*!< Support voltage 3.0V. */
  55. kUSDHC_SupportV180Flag = USDHC_HOST_CTRL_CAP_VS18_MASK, /*!< Support voltage 1.8V. */
  56. kUSDHC_Support4BitFlag = (USDHC_HOST_CTRL_CAP_MBL_SHIFT << 0U),
  57. /*!< Flag in HTCAPBLT_MBL's position, supporting 4-bit mode. */
  58. kUSDHC_Support8BitFlag = (USDHC_HOST_CTRL_CAP_MBL_SHIFT << 1U),
  59. /*!< Flag in HTCAPBLT_MBL's position, supporting 8-bit mode. */
  60. kUSDHC_SupportDDR50Flag = USDHC_HOST_CTRL_CAP_DDR50_SUPPORT_MASK,
  61. /*!< SD version 3.0 new feature, supporting DDR50 mode. */
  62. #if defined(FSL_FEATURE_USDHC_HAS_SDR104_MODE) && (!FSL_FEATURE_USDHC_HAS_SDR104_MODE)
  63. kUSDHC_SupportSDR104Flag = 0, /*!< not support SDR104 mode */
  64. #else
  65. kUSDHC_SupportSDR104Flag = USDHC_HOST_CTRL_CAP_SDR104_SUPPORT_MASK, /*!< Support SDR104 mode. */
  66. #endif
  67. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (!FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  68. kUSDHC_SupportSDR50Flag = 0U, /*!< not support SDR50 mode */
  69. #else
  70. kUSDHC_SupportSDR50Flag = USDHC_HOST_CTRL_CAP_SDR50_SUPPORT_MASK, /*!< Support SDR50 mode. */
  71. #endif
  72. };
  73. /*! @brief Enum _usdhc_wakeup_event. Wakeup event mask.
  74. * @anchor _usdhc_wakeup_event
  75. */
  76. enum
  77. {
  78. kUSDHC_WakeupEventOnCardInt = USDHC_PROT_CTRL_WECINT_MASK, /*!< Wakeup on card interrupt. */
  79. kUSDHC_WakeupEventOnCardInsert = USDHC_PROT_CTRL_WECINS_MASK, /*!< Wakeup on card insertion. */
  80. kUSDHC_WakeupEventOnCardRemove = USDHC_PROT_CTRL_WECRM_MASK, /*!< Wakeup on card removal. */
  81. kUSDHC_WakeupEventsAll =
  82. (kUSDHC_WakeupEventOnCardInt | kUSDHC_WakeupEventOnCardInsert | kUSDHC_WakeupEventOnCardRemove),
  83. /*!< All wakeup events */
  84. };
  85. /*! @brief Enum _usdhc_reset. Reset type mask.
  86. * @anchor _usdhc_reset
  87. */
  88. enum
  89. {
  90. kUSDHC_ResetAll = USDHC_SYS_CTRL_RSTA_MASK, /*!< Reset all except card detection. */
  91. kUSDHC_ResetCommand = USDHC_SYS_CTRL_RSTC_MASK, /*!< Reset command line. */
  92. kUSDHC_ResetData = USDHC_SYS_CTRL_RSTD_MASK, /*!< Reset data line. */
  93. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (!FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  94. kUSDHC_ResetTuning = 0U, /*!< no reset tuning circuit bit */
  95. #else
  96. kUSDHC_ResetTuning = USDHC_SYS_CTRL_RSTT_MASK, /*!< Reset tuning circuit. */
  97. #endif
  98. kUSDHC_ResetsAll = (kUSDHC_ResetAll | kUSDHC_ResetCommand | kUSDHC_ResetData | kUSDHC_ResetTuning),
  99. /*!< All reset types */
  100. };
  101. /*! @brief Enum _usdhc_transfer_flag. Transfer flag mask. */
  102. enum
  103. {
  104. kUSDHC_EnableDmaFlag = USDHC_MIX_CTRL_DMAEN_MASK, /*!< Enable DMA. */
  105. kUSDHC_CommandTypeSuspendFlag = USDHC_CMD_XFR_TYP_CMDTYP(1U), /*!< Suspend command. */
  106. kUSDHC_CommandTypeResumeFlag = USDHC_CMD_XFR_TYP_CMDTYP(2U), /*!< Resume command. */
  107. kUSDHC_CommandTypeAbortFlag = USDHC_CMD_XFR_TYP_CMDTYP(3U), /*!< Abort command. */
  108. kUSDHC_EnableBlockCountFlag = USDHC_MIX_CTRL_BCEN_MASK, /*!< Enable block count. */
  109. kUSDHC_EnableAutoCommand12Flag = USDHC_MIX_CTRL_AC12EN_MASK, /*!< Enable auto CMD12. */
  110. kUSDHC_DataReadFlag = USDHC_MIX_CTRL_DTDSEL_MASK, /*!< Enable data read. */
  111. kUSDHC_MultipleBlockFlag = USDHC_MIX_CTRL_MSBSEL_MASK, /*!< Multiple block data read/write. */
  112. kUSDHC_EnableAutoCommand23Flag = USDHC_MIX_CTRL_AC23EN_MASK, /*!< Enable auto CMD23. */
  113. kUSDHC_ResponseLength136Flag = USDHC_CMD_XFR_TYP_RSPTYP(1U), /*!< 136-bit response length. */
  114. kUSDHC_ResponseLength48Flag = USDHC_CMD_XFR_TYP_RSPTYP(2U), /*!< 48-bit response length. */
  115. kUSDHC_ResponseLength48BusyFlag = USDHC_CMD_XFR_TYP_RSPTYP(3U), /*!< 48-bit response length with busy status. */
  116. kUSDHC_EnableCrcCheckFlag = USDHC_CMD_XFR_TYP_CCCEN_MASK, /*!< Enable CRC check. */
  117. kUSDHC_EnableIndexCheckFlag = USDHC_CMD_XFR_TYP_CICEN_MASK, /*!< Enable index check. */
  118. kUSDHC_DataPresentFlag = USDHC_CMD_XFR_TYP_DPSEL_MASK, /*!< Data present flag. */
  119. };
  120. /*! @brief Enum _usdhc_present_status_flag. Present status flag mask.
  121. * @anchor _usdhc_present_status_flag
  122. */
  123. enum
  124. {
  125. kUSDHC_CommandInhibitFlag = USDHC_PRES_STATE_CIHB_MASK, /*!< Command inhibit. */
  126. kUSDHC_DataInhibitFlag = USDHC_PRES_STATE_CDIHB_MASK, /*!< Data inhibit. */
  127. kUSDHC_DataLineActiveFlag = USDHC_PRES_STATE_DLA_MASK, /*!< Data line active. */
  128. kUSDHC_SdClockStableFlag = USDHC_PRES_STATE_SDSTB_MASK, /*!< SD bus clock stable. */
  129. kUSDHC_WriteTransferActiveFlag = USDHC_PRES_STATE_WTA_MASK, /*!< Write transfer active. */
  130. kUSDHC_ReadTransferActiveFlag = USDHC_PRES_STATE_RTA_MASK, /*!< Read transfer active. */
  131. kUSDHC_BufferWriteEnableFlag = USDHC_PRES_STATE_BWEN_MASK, /*!< Buffer write enable. */
  132. kUSDHC_BufferReadEnableFlag = USDHC_PRES_STATE_BREN_MASK, /*!< Buffer read enable. */
  133. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (!FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  134. kUSDHC_DelaySettingFinishedFlag = 0U, /*!< not support */
  135. kUSDHC_ReTuningRequestFlag = 0U, /*!< not support */
  136. #else
  137. kUSDHC_ReTuningRequestFlag = USDHC_PRES_STATE_RTR_MASK, /*!< Re-tuning request flag, only used for SDR104 mode. */
  138. kUSDHC_DelaySettingFinishedFlag = USDHC_PRES_STATE_TSCD_MASK, /*!< Delay setting finished flag. */
  139. #endif
  140. kUSDHC_CardInsertedFlag = USDHC_PRES_STATE_CINST_MASK, /*!< Card inserted. */
  141. kUSDHC_CommandLineLevelFlag = USDHC_PRES_STATE_CLSL_MASK, /*!< Command line signal level. */
  142. kUSDHC_Data0LineLevelFlag = 1U << USDHC_PRES_STATE_DLSL_SHIFT, /*!< Data0 line signal level. */
  143. kUSDHC_Data1LineLevelFlag = 1U << (USDHC_PRES_STATE_DLSL_SHIFT + 1U), /*!< Data1 line signal level. */
  144. kUSDHC_Data2LineLevelFlag = 1U << (USDHC_PRES_STATE_DLSL_SHIFT + 2U), /*!< Data2 line signal level. */
  145. kUSDHC_Data3LineLevelFlag = 1U << (USDHC_PRES_STATE_DLSL_SHIFT + 3U), /*!< Data3 line signal level. */
  146. kUSDHC_Data4LineLevelFlag = 1U << (USDHC_PRES_STATE_DLSL_SHIFT + 4U), /*!< Data4 line signal level. */
  147. kUSDHC_Data5LineLevelFlag = 1U << (USDHC_PRES_STATE_DLSL_SHIFT + 5U), /*!< Data5 line signal level. */
  148. kUSDHC_Data6LineLevelFlag = 1U << (USDHC_PRES_STATE_DLSL_SHIFT + 6U), /*!< Data6 line signal level. */
  149. kUSDHC_Data7LineLevelFlag = (int)(1U << (USDHC_PRES_STATE_DLSL_SHIFT + 7U)), /*!< Data7 line signal level. */
  150. };
  151. /*! @brief Enum _usdhc_interrupt_status_flag. Interrupt status flag mask.
  152. * @anchor _usdhc_interrupt_status_flag
  153. */
  154. enum
  155. {
  156. kUSDHC_CommandCompleteFlag = USDHC_INT_STATUS_CC_MASK, /*!< Command complete. */
  157. kUSDHC_DataCompleteFlag = USDHC_INT_STATUS_TC_MASK, /*!< Data complete. */
  158. kUSDHC_BlockGapEventFlag = USDHC_INT_STATUS_BGE_MASK, /*!< Block gap event. */
  159. kUSDHC_DmaCompleteFlag = USDHC_INT_STATUS_DINT_MASK, /*!< DMA interrupt. */
  160. kUSDHC_BufferWriteReadyFlag = USDHC_INT_STATUS_BWR_MASK, /*!< Buffer write ready. */
  161. kUSDHC_BufferReadReadyFlag = USDHC_INT_STATUS_BRR_MASK, /*!< Buffer read ready. */
  162. kUSDHC_CardInsertionFlag = USDHC_INT_STATUS_CINS_MASK, /*!< Card inserted. */
  163. kUSDHC_CardRemovalFlag = USDHC_INT_STATUS_CRM_MASK, /*!< Card removed. */
  164. kUSDHC_CardInterruptFlag = USDHC_INT_STATUS_CINT_MASK, /*!< Card interrupt. */
  165. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (!FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  166. kUSDHC_ReTuningEventFlag = 0U, /*!< Re-Tuning event, only for SD3.0 SDR104 mode. */
  167. kUSDHC_TuningPassFlag = 0U, /*!< SDR104 mode tuning pass flag. */
  168. kUSDHC_TuningErrorFlag = 0U, /*!< SDR104 tuning error flag. */
  169. #else
  170. kUSDHC_ReTuningEventFlag = USDHC_INT_STATUS_RTE_MASK, /*!< Re-Tuning event, only for SD3.0 SDR104 mode. */
  171. kUSDHC_TuningPassFlag = USDHC_INT_STATUS_TP_MASK, /*!< SDR104 mode tuning pass flag. */
  172. kUSDHC_TuningErrorFlag = USDHC_INT_STATUS_TNE_MASK, /*!< SDR104 tuning error flag. */
  173. #endif
  174. kUSDHC_CommandTimeoutFlag = USDHC_INT_STATUS_CTOE_MASK, /*!< Command timeout error. */
  175. kUSDHC_CommandCrcErrorFlag = USDHC_INT_STATUS_CCE_MASK, /*!< Command CRC error. */
  176. kUSDHC_CommandEndBitErrorFlag = USDHC_INT_STATUS_CEBE_MASK, /*!< Command end bit error. */
  177. kUSDHC_CommandIndexErrorFlag = USDHC_INT_STATUS_CIE_MASK, /*!< Command index error. */
  178. kUSDHC_DataTimeoutFlag = USDHC_INT_STATUS_DTOE_MASK, /*!< Data timeout error. */
  179. kUSDHC_DataCrcErrorFlag = USDHC_INT_STATUS_DCE_MASK, /*!< Data CRC error. */
  180. kUSDHC_DataEndBitErrorFlag = USDHC_INT_STATUS_DEBE_MASK, /*!< Data end bit error. */
  181. kUSDHC_AutoCommand12ErrorFlag = USDHC_INT_STATUS_AC12E_MASK, /*!< Auto CMD12 error. */
  182. kUSDHC_DmaErrorFlag = USDHC_INT_STATUS_DMAE_MASK, /*!< DMA error. */
  183. kUSDHC_CommandErrorFlag = (kUSDHC_CommandTimeoutFlag | kUSDHC_CommandCrcErrorFlag | kUSDHC_CommandEndBitErrorFlag |
  184. kUSDHC_CommandIndexErrorFlag), /*!< Command error */
  185. kUSDHC_DataErrorFlag = (kUSDHC_DataTimeoutFlag | kUSDHC_DataCrcErrorFlag | kUSDHC_DataEndBitErrorFlag |
  186. kUSDHC_AutoCommand12ErrorFlag), /*!< Data error */
  187. kUSDHC_ErrorFlag = (kUSDHC_CommandErrorFlag | kUSDHC_DataErrorFlag | kUSDHC_DmaErrorFlag), /*!< All error */
  188. kUSDHC_DataFlag = (kUSDHC_DataCompleteFlag | kUSDHC_BufferWriteReadyFlag | kUSDHC_BufferReadReadyFlag |
  189. kUSDHC_DataErrorFlag), /*!< Data interrupts */
  190. kUSDHC_DataDMAFlag = (kUSDHC_DataCompleteFlag | kUSDHC_DataErrorFlag | kUSDHC_DmaErrorFlag), /*!< Data interrupts */
  191. kUSDHC_CommandFlag = (kUSDHC_CommandErrorFlag | kUSDHC_CommandCompleteFlag), /*!< Command interrupts */
  192. kUSDHC_CardDetectFlag = (kUSDHC_CardInsertionFlag | kUSDHC_CardRemovalFlag), /*!< Card detection interrupts */
  193. kUSDHC_SDR104TuningFlag = (kUSDHC_TuningErrorFlag | kUSDHC_TuningPassFlag | kUSDHC_ReTuningEventFlag),
  194. /*!< SDR104 tuning flag. */
  195. kUSDHC_AllInterruptFlags =
  196. (kUSDHC_BlockGapEventFlag | kUSDHC_CardInterruptFlag | kUSDHC_CommandFlag | kUSDHC_DataFlag | kUSDHC_ErrorFlag |
  197. kUSDHC_SDR104TuningFlag | kUSDHC_DmaCompleteFlag), /*!< All flags mask */
  198. };
  199. /*! @brief Enum _usdhc_auto_command12_error_status_flag. Auto CMD12 error status flag mask.
  200. * @anchor _usdhc_auto_command12_error_status_flag
  201. */
  202. enum
  203. {
  204. kUSDHC_AutoCommand12NotExecutedFlag = USDHC_AUTOCMD12_ERR_STATUS_AC12NE_MASK, /*!< Not executed error. */
  205. kUSDHC_AutoCommand12TimeoutFlag = USDHC_AUTOCMD12_ERR_STATUS_AC12TOE_MASK, /*!< Timeout error. */
  206. kUSDHC_AutoCommand12EndBitErrorFlag = USDHC_AUTOCMD12_ERR_STATUS_AC12EBE_MASK, /*!< End bit error. */
  207. kUSDHC_AutoCommand12CrcErrorFlag = USDHC_AUTOCMD12_ERR_STATUS_AC12CE_MASK, /*!< CRC error. */
  208. kUSDHC_AutoCommand12IndexErrorFlag = USDHC_AUTOCMD12_ERR_STATUS_AC12IE_MASK, /*!< Index error. */
  209. kUSDHC_AutoCommand12NotIssuedFlag = USDHC_AUTOCMD12_ERR_STATUS_CNIBAC12E_MASK, /*!< Not issued error. */
  210. };
  211. /*! @brief Enum _usdhc_standard_tuning. Standard tuning flag. */
  212. enum
  213. {
  214. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (!FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  215. kUSDHC_ExecuteTuning = 0U, /*!< not support */
  216. kUSDHC_TuningSampleClockSel = 0U, /*!< not support */
  217. #else
  218. kUSDHC_ExecuteTuning = USDHC_AUTOCMD12_ERR_STATUS_EXECUTE_TUNING_MASK, /*!< Used to start tuning procedure. */
  219. kUSDHC_TuningSampleClockSel =
  220. USDHC_AUTOCMD12_ERR_STATUS_SMP_CLK_SEL_MASK, /*!< When <b>std_tuning_en</b> bit is set, this
  221. bit is used to select sampleing clock. */
  222. #endif
  223. };
  224. /*! @brief Enum _usdhc_adma_error_status_flag. ADMA error status flag mask.
  225. * @anchor _usdhc_adma_error_status_flag
  226. */
  227. enum
  228. {
  229. kUSDHC_AdmaLenghMismatchFlag = USDHC_ADMA_ERR_STATUS_ADMALME_MASK, /*!< Length mismatch error. */
  230. kUSDHC_AdmaDescriptorErrorFlag = USDHC_ADMA_ERR_STATUS_ADMADCE_MASK, /*!< Descriptor error. */
  231. };
  232. /*!
  233. * @brief Enum _usdhc_adma_error_state. ADMA error state.
  234. *
  235. * This state is the detail state when ADMA error has occurred.
  236. */
  237. enum
  238. {
  239. kUSDHC_AdmaErrorStateStopDma = 0x00U,
  240. /*!< Stop DMA, previous location set in the ADMA system address is errored address. */
  241. kUSDHC_AdmaErrorStateFetchDescriptor = 0x01U,
  242. /*!< Fetch descriptor, current location set in the ADMA system address is errored address. */
  243. kUSDHC_AdmaErrorStateChangeAddress = 0x02U, /*!< Change address, no DMA error has occurred. */
  244. kUSDHC_AdmaErrorStateTransferData = 0x03U,
  245. /*!< Transfer data, previous location set in the ADMA system address is errored address. */
  246. kUSDHC_AdmaErrorStateInvalidLength = 0x04U, /*!< Invalid length in ADMA descriptor. */
  247. kUSDHC_AdmaErrorStateInvalidDescriptor = 0x08U, /*!< Invalid descriptor fetched by ADMA. */
  248. kUSDHC_AdmaErrorState = kUSDHC_AdmaErrorStateInvalidLength | kUSDHC_AdmaErrorStateInvalidDescriptor |
  249. kUSDHC_AdmaErrorStateFetchDescriptor, /*!< ADMA error state */
  250. };
  251. /*! @brief Enum _usdhc_force_event. Force event bit position.
  252. * @anchor _usdhc_force_event
  253. */
  254. enum
  255. {
  256. kUSDHC_ForceEventAutoCommand12NotExecuted =
  257. USDHC_FORCE_EVENT_FEVTAC12NE_MASK, /*!< Auto CMD12 not executed error. */
  258. kUSDHC_ForceEventAutoCommand12Timeout = USDHC_FORCE_EVENT_FEVTAC12TOE_MASK, /*!< Auto CMD12 timeout error. */
  259. kUSDHC_ForceEventAutoCommand12CrcError = USDHC_FORCE_EVENT_FEVTAC12CE_MASK, /*!< Auto CMD12 CRC error. */
  260. kUSDHC_ForceEventEndBitError = USDHC_FORCE_EVENT_FEVTAC12EBE_MASK, /*!< Auto CMD12 end bit error. */
  261. kUSDHC_ForceEventAutoCommand12IndexError = USDHC_FORCE_EVENT_FEVTAC12IE_MASK, /*!< Auto CMD12 index error. */
  262. kUSDHC_ForceEventAutoCommand12NotIssued = USDHC_FORCE_EVENT_FEVTCNIBAC12E_MASK, /*!< Auto CMD12 not issued error. */
  263. kUSDHC_ForceEventCommandTimeout = USDHC_FORCE_EVENT_FEVTCTOE_MASK, /*!< Command timeout error. */
  264. kUSDHC_ForceEventCommandCrcError = USDHC_FORCE_EVENT_FEVTCCE_MASK, /*!< Command CRC error. */
  265. kUSDHC_ForceEventCommandEndBitError = USDHC_FORCE_EVENT_FEVTCEBE_MASK, /*!< Command end bit error. */
  266. kUSDHC_ForceEventCommandIndexError = USDHC_FORCE_EVENT_FEVTCIE_MASK, /*!< Command index error. */
  267. kUSDHC_ForceEventDataTimeout = USDHC_FORCE_EVENT_FEVTDTOE_MASK, /*!< Data timeout error. */
  268. kUSDHC_ForceEventDataCrcError = USDHC_FORCE_EVENT_FEVTDCE_MASK, /*!< Data CRC error. */
  269. kUSDHC_ForceEventDataEndBitError = USDHC_FORCE_EVENT_FEVTDEBE_MASK, /*!< Data end bit error. */
  270. kUSDHC_ForceEventAutoCommand12Error = USDHC_FORCE_EVENT_FEVTAC12E_MASK, /*!< Auto CMD12 error. */
  271. kUSDHC_ForceEventCardInt = (int)USDHC_FORCE_EVENT_FEVTCINT_MASK, /*!< Card interrupt. */
  272. kUSDHC_ForceEventDmaError = USDHC_FORCE_EVENT_FEVTDMAE_MASK, /*!< Dma error. */
  273. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (!FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  274. kUSDHC_ForceEventTuningError = 0U, /*!< not support */
  275. #else
  276. kUSDHC_ForceEventTuningError = USDHC_FORCE_EVENT_FEVTTNE_MASK, /*!< Tuning error. */
  277. #endif
  278. kUSDHC_ForceEventsAll =
  279. (int)(USDHC_FORCE_EVENT_FEVTAC12NE_MASK | USDHC_FORCE_EVENT_FEVTAC12TOE_MASK |
  280. USDHC_FORCE_EVENT_FEVTAC12CE_MASK | USDHC_FORCE_EVENT_FEVTAC12EBE_MASK |
  281. USDHC_FORCE_EVENT_FEVTAC12IE_MASK | USDHC_FORCE_EVENT_FEVTCNIBAC12E_MASK |
  282. USDHC_FORCE_EVENT_FEVTCTOE_MASK | USDHC_FORCE_EVENT_FEVTCCE_MASK | USDHC_FORCE_EVENT_FEVTCEBE_MASK |
  283. USDHC_FORCE_EVENT_FEVTCIE_MASK | USDHC_FORCE_EVENT_FEVTDTOE_MASK | USDHC_FORCE_EVENT_FEVTDCE_MASK |
  284. USDHC_FORCE_EVENT_FEVTDEBE_MASK | USDHC_FORCE_EVENT_FEVTAC12E_MASK | USDHC_FORCE_EVENT_FEVTCINT_MASK |
  285. USDHC_FORCE_EVENT_FEVTDMAE_MASK | kUSDHC_ForceEventTuningError), /*!< All force event flags mask. */
  286. };
  287. /*! @brief Data transfer direction. */
  288. typedef enum _usdhc_transfer_direction
  289. {
  290. kUSDHC_TransferDirectionReceive = 1U, /*!< USDHC transfer direction receive. */
  291. kUSDHC_TransferDirectionSend = 0U, /*!< USDHC transfer direction send. */
  292. } usdhc_transfer_direction_t;
  293. /*! @brief Data transfer width. */
  294. typedef enum _usdhc_data_bus_width
  295. {
  296. kUSDHC_DataBusWidth1Bit = 0U, /*!< 1-bit mode */
  297. kUSDHC_DataBusWidth4Bit = 1U, /*!< 4-bit mode */
  298. kUSDHC_DataBusWidth8Bit = 2U, /*!< 8-bit mode */
  299. } usdhc_data_bus_width_t;
  300. /*! @brief Endian mode */
  301. typedef enum _usdhc_endian_mode
  302. {
  303. kUSDHC_EndianModeBig = 0U, /*!< Big endian mode. */
  304. kUSDHC_EndianModeHalfWordBig = 1U, /*!< Half word big endian mode. */
  305. kUSDHC_EndianModeLittle = 2U, /*!< Little endian mode. */
  306. } usdhc_endian_mode_t;
  307. /*! @brief DMA mode */
  308. typedef enum _usdhc_dma_mode
  309. {
  310. kUSDHC_DmaModeSimple = 0U, /*!< External DMA. */
  311. kUSDHC_DmaModeAdma1 = 1U, /*!< ADMA1 is selected. */
  312. kUSDHC_DmaModeAdma2 = 2U, /*!< ADMA2 is selected. */
  313. kUSDHC_ExternalDMA = 3U, /*!< External DMA mode selected. */
  314. } usdhc_dma_mode_t;
  315. /*! @brief Enum _usdhc_sdio_control_flag. SDIO control flag mask.
  316. * @anchor _usdhc_sdio_control_flag
  317. */
  318. enum
  319. {
  320. kUSDHC_StopAtBlockGapFlag = USDHC_PROT_CTRL_SABGREQ_MASK, /*!< Stop at block gap. */
  321. kUSDHC_ReadWaitControlFlag = USDHC_PROT_CTRL_RWCTL_MASK, /*!< Read wait control. */
  322. kUSDHC_InterruptAtBlockGapFlag = USDHC_PROT_CTRL_IABG_MASK, /*!< Interrupt at block gap. */
  323. kUSDHC_ReadDoneNo8CLK = USDHC_PROT_CTRL_RD_DONE_NO_8CLK_MASK, /*!< Read done without 8 clk for block gap. */
  324. kUSDHC_ExactBlockNumberReadFlag = USDHC_PROT_CTRL_NON_EXACT_BLK_RD_MASK, /*!< Exact block number read. */
  325. };
  326. /*! @brief MMC card boot mode */
  327. typedef enum _usdhc_boot_mode
  328. {
  329. kUSDHC_BootModeNormal = 0U, /*!< Normal boot */
  330. kUSDHC_BootModeAlternative = 1U, /*!< Alternative boot */
  331. } usdhc_boot_mode_t;
  332. /*! @brief The command type */
  333. typedef enum _usdhc_card_command_type
  334. {
  335. kCARD_CommandTypeNormal = 0U, /*!< Normal command */
  336. kCARD_CommandTypeSuspend = 1U, /*!< Suspend command */
  337. kCARD_CommandTypeResume = 2U, /*!< Resume command */
  338. kCARD_CommandTypeAbort = 3U, /*!< Abort command */
  339. kCARD_CommandTypeEmpty = 4U, /*!< Empty command */
  340. } usdhc_card_command_type_t;
  341. /*!
  342. * @brief The command response type.
  343. *
  344. * Defines the command response type from card to host controller.
  345. */
  346. typedef enum _usdhc_card_response_type
  347. {
  348. kCARD_ResponseTypeNone = 0U, /*!< Response type: none */
  349. kCARD_ResponseTypeR1 = 1U, /*!< Response type: R1 */
  350. kCARD_ResponseTypeR1b = 2U, /*!< Response type: R1b */
  351. kCARD_ResponseTypeR2 = 3U, /*!< Response type: R2 */
  352. kCARD_ResponseTypeR3 = 4U, /*!< Response type: R3 */
  353. kCARD_ResponseTypeR4 = 5U, /*!< Response type: R4 */
  354. kCARD_ResponseTypeR5 = 6U, /*!< Response type: R5 */
  355. kCARD_ResponseTypeR5b = 7U, /*!< Response type: R5b */
  356. kCARD_ResponseTypeR6 = 8U, /*!< Response type: R6 */
  357. kCARD_ResponseTypeR7 = 9U, /*!< Response type: R7 */
  358. } usdhc_card_response_type_t;
  359. /*! @brief The alignment size for ADDRESS filed in ADMA1's descriptor. */
  360. #define USDHC_ADMA1_ADDRESS_ALIGN (4096U)
  361. /*! @brief The alignment size for LENGTH field in ADMA1's descriptor. */
  362. #define USDHC_ADMA1_LENGTH_ALIGN (4096U)
  363. /*! @brief The alignment size for ADDRESS field in ADMA2's descriptor. */
  364. #define USDHC_ADMA2_ADDRESS_ALIGN (4U)
  365. /*! @brief The alignment size for LENGTH filed in ADMA2's descriptor. */
  366. #define USDHC_ADMA2_LENGTH_ALIGN (4U)
  367. /* ADMA1 descriptor table:
  368. * |------------------------|---------|--------------------------|
  369. * | Address/page field |Reserved | Attribute |
  370. * |------------------------|---------|--------------------------|
  371. * |31 12|11 6|05 |04 |03|02 |01 |00 |
  372. * |------------------------|---------|----|----|--|---|---|-----|
  373. * | address or data length | 000000 |Act2|Act1| 0|Int|End|Valid|
  374. * |------------------------|---------|----|----|--|---|---|-----|
  375. *
  376. * ADMA2 action table:
  377. * |------|------|-----------------|-------|-------------|
  378. * | Act2 | Act1 | Comment | 31-28 | 27 - 12 |
  379. * |------|------|-----------------|---------------------|
  380. * | 0 | 0 | No op | Don't care |
  381. * |------|------|-----------------|-------|-------------|
  382. * | 0 | 1 | Set data length | 0000 | Data Length |
  383. * |------|------|-----------------|-------|-------------|
  384. * | 1 | 0 | Transfer data | Data address |
  385. * |------|------|-----------------|---------------------|
  386. * | 1 | 1 | Link descriptor | Descriptor address |
  387. * |------|------|-----------------|---------------------|
  388. */
  389. /****************************tables below are created only for Doxygen*********************************/
  390. /*! @brief The bit shift for ADDRESS filed in ADMA1's descriptor.
  391. * <table>
  392. * <caption>ADMA1 descriptor table</caption>
  393. * <tr><th>Address/page field <th>Reserved <th colspan="6">Attribute
  394. * <tr><td>31 12 <td>11 6 <td>05 <td>04 <td>03 <td>02 <td>01 <td>00
  395. * <tr><td>address or data length <td>000000 <td>Act2 <td>Act1 <td>0 <td>Int <td>End <td>Valid
  396. * </table>
  397. *
  398. * <table>
  399. * <caption>ADMA2 action</caption>
  400. * <tr><th>Act2 <th>Act1 <th>Comment <th>31-28 <th>27-12
  401. * <tr><td>0 <td>0 <td>No op <td colspan="2">Don't care
  402. * <tr><td>0 <td>1 <td>Set data length <td>0000 <td> Data Length
  403. * <tr><td>1 <td>0 <td>Transfer data <td colspan="2">Data address
  404. * <tr><td>1 <td>1 <td>Link descriptor <td colspan="2">Descriptor address
  405. * </table>
  406. */
  407. #define USDHC_ADMA1_DESCRIPTOR_ADDRESS_SHIFT (12U)
  408. /*! @brief The bit mask for ADDRESS field in ADMA1's descriptor. */
  409. #define USDHC_ADMA1_DESCRIPTOR_ADDRESS_MASK (0xFFFFFU)
  410. /*! @brief The bit shift for LENGTH filed in ADMA1's descriptor. */
  411. #define USDHC_ADMA1_DESCRIPTOR_LENGTH_SHIFT (12U)
  412. /*! @brief The mask for LENGTH field in ADMA1's descriptor. */
  413. #define USDHC_ADMA1_DESCRIPTOR_LENGTH_MASK (0xFFFFU)
  414. /*! @brief The maximum value of LENGTH filed in ADMA1's descriptor.
  415. * Since the max transfer size ADMA1 support is 65535 which is indivisible by
  416. * 4096, so to make sure a large data load transfer (>64KB) continuously (require the data
  417. * address be always align with 4096), software will set the maximum data length
  418. * for ADMA1 to (64 - 4)KB.
  419. */
  420. #define USDHC_ADMA1_DESCRIPTOR_MAX_LENGTH_PER_ENTRY (USDHC_ADMA1_DESCRIPTOR_LENGTH_MASK + 1U - 4096U)
  421. /*! @brief Enum _usdhc_adma1_descriptor_flag. The mask for the control/status field in ADMA1 descriptor. */
  422. enum
  423. {
  424. kUSDHC_Adma1DescriptorValidFlag = (1U << 0U), /*!< Valid flag. */
  425. kUSDHC_Adma1DescriptorEndFlag = (1U << 1U), /*!< End flag. */
  426. kUSDHC_Adma1DescriptorInterrupFlag = (1U << 2U), /*!< Interrupt flag. */
  427. kUSDHC_Adma1DescriptorActivity1Flag = (1U << 4U), /*!< Activity 1 flag. */
  428. kUSDHC_Adma1DescriptorActivity2Flag = (1U << 5U), /*!< Activity 2 flag. */
  429. kUSDHC_Adma1DescriptorTypeNop = (kUSDHC_Adma1DescriptorValidFlag), /*!< No operation. */
  430. kUSDHC_Adma1DescriptorTypeTransfer = (kUSDHC_Adma1DescriptorActivity2Flag | kUSDHC_Adma1DescriptorValidFlag),
  431. /*!< Transfer data. */
  432. kUSDHC_Adma1DescriptorTypeLink = (kUSDHC_Adma1DescriptorActivity1Flag | kUSDHC_Adma1DescriptorActivity2Flag |
  433. kUSDHC_Adma1DescriptorValidFlag), /*!< Link descriptor. */
  434. kUSDHC_Adma1DescriptorTypeSetLength = (kUSDHC_Adma1DescriptorActivity1Flag | kUSDHC_Adma1DescriptorValidFlag),
  435. /*!< Set data length. */
  436. };
  437. /* ADMA2 descriptor table:
  438. * |----------------|---------------|-------------|--------------------------|
  439. * | Address field | Length | Reserved | Attribute |
  440. * |----------------|---------------|-------------|--------------------------|
  441. * |63 32|31 16|15 06|05 |04 |03|02 |01 |00 |
  442. * |----------------|---------------|-------------|----|----|--|---|---|-----|
  443. * | 32-bit address | 16-bit length | 0000000000 |Act2|Act1| 0|Int|End|Valid|
  444. * |----------------|---------------|-------------|----|----|--|---|---|-----|
  445. *
  446. * ADMA2 action table:
  447. * | Act2 | Act1 | Comment | Operation |
  448. * |------|------|-----------------|-------------------------------------------------------------------|
  449. * | 0 | 0 | No op | Don't care |
  450. * |------|------|-----------------|-------------------------------------------------------------------|
  451. * | 0 | 1 | Reserved | Read this line and go to next one |
  452. * |------|------|-----------------|-------------------------------------------------------------------|
  453. * | 1 | 0 | Transfer data | Transfer data with address and length set in this descriptor line |
  454. * |------|------|-----------------|-------------------------------------------------------------------|
  455. * | 1 | 1 | Link descriptor | Link to another descriptor |
  456. * |------|------|-----------------|-------------------------------------------------------------------|
  457. */
  458. /**********************************tables below are created only for Doxygen***********************************/
  459. /*! @brief The bit shift for LENGTH field in ADMA2's descriptor.
  460. *
  461. * <table>
  462. * <caption>ADMA2 descriptor table</caption>
  463. * <tr><th>Address field <th>Length <th>Reserved <th colspan="6">Attribute
  464. * <tr><td>63 32 <td>31 16 <td>15 06 <td>05 <td>04 <td>03 <td>02 <td>01 <td>00
  465. * <tr><td>32-bit address <td>16-bit length <td>0000000000 <td>Act2 <td>Act1 <td>0 <td>Int <td>End <td>Valid
  466. *</table>
  467. *
  468. * <table>
  469. * <caption>ADMA2 action</caption>
  470. * <tr><th>Act2 <th>Act1 <th>Comment <th>Operation
  471. * <tr><td> 0 <td>0 <td>No op <td>Don't care
  472. * <tr><td> 0 <td>1 <td> Reserved <td> Read this line and go to next one
  473. * <tr><td> 1 <td>0 <td>Transfer data <td>Transfer data with address and length set in this descriptor line
  474. * <tr><td> 1 <td>1 <td>Link descriptor <td>Link to another descriptor
  475. * </table>
  476. */
  477. #define USDHC_ADMA2_DESCRIPTOR_LENGTH_SHIFT (16U)
  478. /*! @brief The bit mask for LENGTH field in ADMA2's descriptor. */
  479. #define USDHC_ADMA2_DESCRIPTOR_LENGTH_MASK (0xFFFFU)
  480. /*! @brief The maximum value of LENGTH field in ADMA2's descriptor. */
  481. #define USDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY (USDHC_ADMA2_DESCRIPTOR_LENGTH_MASK - 3U)
  482. /*! @brief Enum _usdhc_adma2_descriptor_flag. ADMA1 descriptor control and status mask. */
  483. enum
  484. {
  485. kUSDHC_Adma2DescriptorValidFlag = (1U << 0U), /*!< Valid flag. */
  486. kUSDHC_Adma2DescriptorEndFlag = (1U << 1U), /*!< End flag. */
  487. kUSDHC_Adma2DescriptorInterruptFlag = (1U << 2U), /*!< Interrupt flag. */
  488. kUSDHC_Adma2DescriptorActivity1Flag = (1U << 4U), /*!< Activity 1 mask. */
  489. kUSDHC_Adma2DescriptorActivity2Flag = (1U << 5U), /*!< Activity 2 mask. */
  490. kUSDHC_Adma2DescriptorTypeNop = (kUSDHC_Adma2DescriptorValidFlag), /*!< No operation. */
  491. kUSDHC_Adma2DescriptorTypeReserved = (kUSDHC_Adma2DescriptorActivity1Flag | kUSDHC_Adma2DescriptorValidFlag),
  492. /*!< Reserved. */
  493. kUSDHC_Adma2DescriptorTypeTransfer = (kUSDHC_Adma2DescriptorActivity2Flag | kUSDHC_Adma2DescriptorValidFlag),
  494. /*!< Transfer type. */
  495. kUSDHC_Adma2DescriptorTypeLink = (kUSDHC_Adma2DescriptorActivity1Flag | kUSDHC_Adma2DescriptorActivity2Flag |
  496. kUSDHC_Adma2DescriptorValidFlag), /*!< Link type. */
  497. };
  498. /*! @brief Enum _usdhc_adma_flag. ADMA descriptor configuration flag.
  499. * @anchor _usdhc_adma_flag
  500. */
  501. enum
  502. {
  503. kUSDHC_AdmaDescriptorSingleFlag = 0U,
  504. /*!< Try to finish the transfer in a single ADMA descriptor. If transfer size is bigger than one
  505. ADMA descriptor's ability, new another descriptor for data transfer. */
  506. kUSDHC_AdmaDescriptorMultipleFlag =
  507. 1U, /*!< Create multiple ADMA descriptors within the ADMA table, this is used for
  508. mmc boot mode specifically, which need
  509. to modify the ADMA descriptor on the fly, so the flag should be used
  510. combining with stop at block gap feature. */
  511. };
  512. /*! @brief DMA transfer burst len config. */
  513. typedef enum _usdhc_burst_len
  514. {
  515. kUSDHC_EnBurstLenForINCR = 0x01U, /*!< Enable burst len for INCR. */
  516. kUSDHC_EnBurstLenForINCR4816 = 0x02U, /*!< Enable burst len for INCR4/INCR8/INCR16. */
  517. kUSDHC_EnBurstLenForINCR4816WRAP = 0x04U, /*!< Enable burst len for INCR4/8/16 WRAP. */
  518. } usdhc_burst_len_t;
  519. /*! @brief Enum _usdhc_transfer_data_type. Tansfer data type definition. */
  520. enum
  521. {
  522. kUSDHC_TransferDataNormal = 0U, /*!< Transfer normal read/write data. */
  523. kUSDHC_TransferDataTuning = 1U, /*!< Transfer tuning data. */
  524. kUSDHC_TransferDataBoot = 2U, /*!< Transfer boot data. */
  525. kUSDHC_TransferDataBootcontinous = 3U, /*!< Transfer boot data continuously. */
  526. };
  527. /*! @brief Defines the ADMA1 descriptor structure. */
  528. typedef uint32_t usdhc_adma1_descriptor_t;
  529. /*! @brief Defines the ADMA2 descriptor structure. */
  530. typedef struct _usdhc_adma2_descriptor
  531. {
  532. uint32_t attribute; /*!< The control and status field. */
  533. const uint32_t *address; /*!< The address field. */
  534. } usdhc_adma2_descriptor_t;
  535. /*!
  536. * @brief USDHC capability information.
  537. *
  538. * Defines a structure to save the capability information of USDHC.
  539. */
  540. typedef struct _usdhc_capability
  541. {
  542. uint32_t sdVersion; /*!< Support SD card/sdio version. */
  543. uint32_t mmcVersion; /*!< Support EMMC card version. */
  544. uint32_t maxBlockLength; /*!< Maximum block length united as byte. */
  545. uint32_t maxBlockCount; /*!< Maximum block count can be set one time. */
  546. uint32_t flags; /*!< Capability flags to indicate the support information(@ref _usdhc_capability_flag). */
  547. } usdhc_capability_t;
  548. /*! @brief Data structure to configure the MMC boot feature. */
  549. typedef struct _usdhc_boot_config
  550. {
  551. uint32_t ackTimeoutCount; /*!< Timeout value for the boot ACK. The available range is 0 ~ 15. */
  552. usdhc_boot_mode_t bootMode; /*!< Boot mode selection. */
  553. uint32_t blockCount; /*!< Stop at block gap value of automatic mode. Available range is 0 ~ 65535. */
  554. size_t blockSize; /*!< Block size. */
  555. bool enableBootAck; /*!< Enable or disable boot ACK. */
  556. bool enableAutoStopAtBlockGap; /*!< Enable or disable auto stop at block gap function in boot period. */
  557. } usdhc_boot_config_t;
  558. /*! @brief Data structure to initialize the USDHC. */
  559. typedef struct _usdhc_config
  560. {
  561. uint32_t dataTimeout; /*!< Data timeout value. */
  562. usdhc_endian_mode_t endianMode; /*!< Endian mode. */
  563. uint8_t readWatermarkLevel; /*!< Watermark level for DMA read operation. Available range is 1 ~ 128. */
  564. uint8_t writeWatermarkLevel; /*!< Watermark level for DMA write operation. Available range is 1 ~ 128. */
  565. #if !(defined(FSL_FEATURE_USDHC_HAS_NO_RW_BURST_LEN) && FSL_FEATURE_USDHC_HAS_NO_RW_BURST_LEN)
  566. uint8_t readBurstLen; /*!< Read burst len. */
  567. uint8_t writeBurstLen; /*!< Write burst len. */
  568. #endif
  569. } usdhc_config_t;
  570. /*!
  571. * @brief Card command descriptor.
  572. *
  573. * Defines card command-related attribute.
  574. */
  575. typedef struct _usdhc_command
  576. {
  577. uint32_t index; /*!< Command index. */
  578. uint32_t argument; /*!< Command argument. */
  579. usdhc_card_command_type_t type; /*!< Command type. */
  580. usdhc_card_response_type_t responseType; /*!< Command response type. */
  581. uint32_t response[4U]; /*!< Response for this command. */
  582. uint32_t responseErrorFlags; /*!< Response error flag, which need to check
  583. the command reponse. */
  584. uint32_t flags; /*!< Cmd flags. */
  585. } usdhc_command_t;
  586. /*! @brief ADMA configuration. */
  587. typedef struct _usdhc_adma_config
  588. {
  589. usdhc_dma_mode_t dmaMode; /*!< DMA mode. */
  590. #if !(defined(FSL_FEATURE_USDHC_HAS_NO_RW_BURST_LEN) && FSL_FEATURE_USDHC_HAS_NO_RW_BURST_LEN)
  591. usdhc_burst_len_t burstLen; /*!< Burst len config. */
  592. #endif
  593. uint32_t *admaTable; /*!< ADMA table address, can't be null if transfer way is ADMA1/ADMA2. */
  594. uint32_t admaTableWords; /*!< ADMA table length united as words, can't be 0 if transfer way is ADMA1/ADMA2. */
  595. } usdhc_adma_config_t;
  596. /*!
  597. * @brief Card scatter gather data list.
  598. *
  599. * Allow application register uncontinuous data buffer for data transfer.
  600. */
  601. typedef struct _usdhc_scatter_gather_data_list
  602. {
  603. uint32_t *dataAddr;
  604. uint32_t dataSize;
  605. struct _usdhc_scatter_gather_data_list *dataList;
  606. } usdhc_scatter_gather_data_list_t;
  607. /*!
  608. * @brief Card scatter gather data descriptor.
  609. *
  610. * Defines a structure to contain data-related attribute. The 'enableIgnoreError' is used when upper card
  611. * driver wants to ignore the error event to read/write all the data and not to stop read/write immediately when an
  612. * error event happens. For example, bus testing procedure for MMC card.
  613. */
  614. typedef struct _usdhc_scatter_gather_data
  615. {
  616. bool enableAutoCommand12; /*!< Enable auto CMD12. */
  617. bool enableAutoCommand23; /*!< Enable auto CMD23. */
  618. bool enableIgnoreError; /*!< Enable to ignore error event to read/write all the data. */
  619. usdhc_transfer_direction_t dataDirection; /*!< data direction */
  620. uint8_t dataType; /*!< this is used to distinguish the normal/tuning/boot data. */
  621. size_t blockSize; /*!< Block size. */
  622. usdhc_scatter_gather_data_list_t sgData; /*!< scatter gather data */
  623. } usdhc_scatter_gather_data_t;
  624. /*! @brief usdhc scatter gather transfer. */
  625. typedef struct _usdhc_scatter_gather_transfer
  626. {
  627. usdhc_scatter_gather_data_t *data; /*!< Data to transfer. */
  628. usdhc_command_t *command; /*!< Command to send. */
  629. } usdhc_scatter_gather_transfer_t;
  630. /*!
  631. * @brief Card data descriptor.
  632. *
  633. * Defines a structure to contain data-related attribute. The 'enableIgnoreError' is used when upper card
  634. * driver wants to ignore the error event to read/write all the data and not to stop read/write immediately when an
  635. * error event happens. For example, bus testing procedure for MMC card.
  636. */
  637. typedef struct _usdhc_data
  638. {
  639. bool enableAutoCommand12; /*!< Enable auto CMD12. */
  640. bool enableAutoCommand23; /*!< Enable auto CMD23. */
  641. bool enableIgnoreError; /*!< Enable to ignore error event to read/write all the data. */
  642. uint8_t dataType; /*!< this is used to distinguish the normal/tuning/boot data. */
  643. size_t blockSize; /*!< Block size. */
  644. uint32_t blockCount; /*!< Block count. */
  645. uint32_t *rxData; /*!< Buffer to save data read. */
  646. const uint32_t *txData; /*!< Data buffer to write. */
  647. } usdhc_data_t;
  648. /*! @brief Transfer state. */
  649. typedef struct _usdhc_transfer
  650. {
  651. usdhc_data_t *data; /*!< Data to transfer. */
  652. usdhc_command_t *command; /*!< Command to send. */
  653. } usdhc_transfer_t;
  654. /*! @brief USDHC handle typedef. */
  655. typedef struct _usdhc_handle usdhc_handle_t;
  656. /*! @brief USDHC callback functions. */
  657. typedef struct _usdhc_transfer_callback
  658. {
  659. void (*CardInserted)(USDHC_Type *base,
  660. void *userData); /*!< Card inserted occurs when DAT3/CD pin is for card detect */
  661. void (*CardRemoved)(USDHC_Type *base, void *userData); /*!< Card removed occurs */
  662. void (*SdioInterrupt)(USDHC_Type *base, void *userData); /*!< SDIO card interrupt occurs */
  663. void (*BlockGap)(USDHC_Type *base, void *userData); /*!< stopped at block gap event */
  664. void (*TransferComplete)(USDHC_Type *base,
  665. usdhc_handle_t *handle,
  666. status_t status,
  667. void *userData); /*!< Transfer complete callback. */
  668. void (*ReTuning)(USDHC_Type *base, void *userData); /*!< Handle the re-tuning. */
  669. } usdhc_transfer_callback_t;
  670. /*!
  671. * @brief USDHC handle.
  672. *
  673. * Defines the structure to save the USDHC state information and callback function.
  674. *
  675. * @note All the fields except interruptFlags and transferredWords must be allocated by the user.
  676. */
  677. struct _usdhc_handle
  678. {
  679. #if (defined FSL_USDHC_ENABLE_SCATTER_GATHER_TRANSFER) && FSL_USDHC_ENABLE_SCATTER_GATHER_TRANSFER
  680. usdhc_scatter_gather_data_t *volatile data; /*!< scatter gather data pointer */
  681. #else
  682. usdhc_data_t *volatile data; /*!< Transfer parameter. Data to transfer. */
  683. #endif
  684. usdhc_command_t *volatile command; /*!< Transfer parameter. Command to send. */
  685. volatile uint32_t transferredWords; /*!< Transfer status. Words transferred by DATAPORT way. */
  686. usdhc_transfer_callback_t callback; /*!< Callback function. */
  687. void *userData; /*!< Parameter for transfer complete callback. */
  688. };
  689. /*! @brief USDHC transfer function. */
  690. typedef status_t (*usdhc_transfer_function_t)(USDHC_Type *base, usdhc_transfer_t *content);
  691. /*! @brief USDHC host descriptor. */
  692. typedef struct _usdhc_host
  693. {
  694. USDHC_Type *base; /*!< USDHC peripheral base address. */
  695. uint32_t sourceClock_Hz; /*!< USDHC source clock frequency united in Hz. */
  696. usdhc_config_t config; /*!< USDHC configuration. */
  697. usdhc_capability_t capability; /*!< USDHC capability information. */
  698. usdhc_transfer_function_t transfer; /*!< USDHC transfer function. */
  699. } usdhc_host_t;
  700. /*************************************************************************************************
  701. * API
  702. ************************************************************************************************/
  703. #if defined(__cplusplus)
  704. extern "C" {
  705. #endif
  706. /*!
  707. * @name Initialization and deinitialization
  708. * @{
  709. */
  710. /*!
  711. * @brief USDHC module initialization function.
  712. *
  713. * Configures the USDHC according to the user configuration.
  714. *
  715. * Example:
  716. @code
  717. usdhc_config_t config;
  718. config.cardDetectDat3 = false;
  719. config.endianMode = kUSDHC_EndianModeLittle;
  720. config.dmaMode = kUSDHC_DmaModeAdma2;
  721. config.readWatermarkLevel = 128U;
  722. config.writeWatermarkLevel = 128U;
  723. USDHC_Init(USDHC, &config);
  724. @endcode
  725. *
  726. * @param base USDHC peripheral base address.
  727. * @param config USDHC configuration information.
  728. * @retval #kStatus_Success Operate successfully.
  729. */
  730. void USDHC_Init(USDHC_Type *base, const usdhc_config_t *config);
  731. /*!
  732. * @brief Deinitializes the USDHC.
  733. *
  734. * @param base USDHC peripheral base address.
  735. */
  736. void USDHC_Deinit(USDHC_Type *base);
  737. /*!
  738. * @brief Resets the USDHC.
  739. *
  740. * @param base USDHC peripheral base address.
  741. * @param mask The reset type mask(@ref _usdhc_reset).
  742. * @param timeout Timeout for reset.
  743. * @retval true Reset successfully.
  744. * @retval false Reset failed.
  745. */
  746. bool USDHC_Reset(USDHC_Type *base, uint32_t mask, uint32_t timeout);
  747. /* @} */
  748. /*!
  749. * @name DMA Control
  750. * @{
  751. */
  752. /*!
  753. * @brief Sets the DMA descriptor table configuration.
  754. * A high level DMA descriptor configuration function.
  755. * @param base USDHC peripheral base address.
  756. * @param dmaConfig ADMA configuration
  757. * @param dataConfig Data descriptor
  758. * @param flags ADAM descriptor flag, used to indicate to create multiple or single descriptor, please
  759. * refer to enum @ref _usdhc_adma_flag.
  760. * @retval #kStatus_OutOfRange ADMA descriptor table length isn't enough to describe data.
  761. * @retval #kStatus_Success Operate successfully.
  762. */
  763. status_t USDHC_SetAdmaTableConfig(USDHC_Type *base,
  764. usdhc_adma_config_t *dmaConfig,
  765. usdhc_data_t *dataConfig,
  766. uint32_t flags);
  767. /*!
  768. * @brief Internal DMA configuration.
  769. * This function is used to config the USDHC DMA related registers.
  770. * @param base USDHC peripheral base address.
  771. * @param dmaConfig ADMA configuration.
  772. * @param dataAddr Transfer data address, a simple DMA parameter, if ADMA is used, leave it to NULL.
  773. * @param enAutoCmd23 Flag to indicate Auto CMD23 is enable or not, a simple DMA parameter, if ADMA is used, leave it
  774. * to false.
  775. * @retval #kStatus_OutOfRange ADMA descriptor table length isn't enough to describe data.
  776. * @retval #kStatus_Success Operate successfully.
  777. */
  778. status_t USDHC_SetInternalDmaConfig(USDHC_Type *base,
  779. usdhc_adma_config_t *dmaConfig,
  780. const uint32_t *dataAddr,
  781. bool enAutoCmd23);
  782. /*!
  783. * @brief Sets the ADMA2 descriptor table configuration.
  784. *
  785. * @param admaTable ADMA table address.
  786. * @param admaTableWords ADMA table length.
  787. * @param dataBufferAddr Data buffer address.
  788. * @param dataBytes Data Data length.
  789. * @param flags ADAM descriptor flag, used to indicate to create multiple or single descriptor, please
  790. * refer to enum @ref _usdhc_adma_flag.
  791. * @retval #kStatus_OutOfRange ADMA descriptor table length isn't enough to describe data.
  792. * @retval #kStatus_Success Operate successfully.
  793. */
  794. status_t USDHC_SetADMA2Descriptor(
  795. uint32_t *admaTable, uint32_t admaTableWords, const uint32_t *dataBufferAddr, uint32_t dataBytes, uint32_t flags);
  796. /*!
  797. * @brief Sets the ADMA1 descriptor table configuration.
  798. *
  799. * @param admaTable ADMA table address.
  800. * @param admaTableWords ADMA table length.
  801. * @param dataBufferAddr Data buffer address.
  802. * @param dataBytes Data length.
  803. * @param flags ADAM descriptor flag, used to indicate to create multiple or single descriptor, please
  804. * refer to enum @ref _usdhc_adma_flag.
  805. * @retval #kStatus_OutOfRange ADMA descriptor table length isn't enough to describe data.
  806. * @retval #kStatus_Success Operate successfully.
  807. */
  808. status_t USDHC_SetADMA1Descriptor(
  809. uint32_t *admaTable, uint32_t admaTableWords, const uint32_t *dataBufferAddr, uint32_t dataBytes, uint32_t flags);
  810. /*!
  811. * @brief Enables internal DMA.
  812. *
  813. * @param base USDHC peripheral base address.
  814. * @param enable enable or disable flag
  815. */
  816. static inline void USDHC_EnableInternalDMA(USDHC_Type *base, bool enable)
  817. {
  818. if (enable)
  819. {
  820. base->MIX_CTRL |= USDHC_MIX_CTRL_DMAEN_MASK;
  821. }
  822. else
  823. {
  824. base->MIX_CTRL &= ~USDHC_MIX_CTRL_DMAEN_MASK;
  825. base->PROT_CTRL &= ~USDHC_PROT_CTRL_DMASEL_MASK;
  826. }
  827. }
  828. /* @} */
  829. /*!
  830. * @name Interrupts
  831. * @{
  832. */
  833. /*!
  834. * @brief Enables the interrupt status.
  835. *
  836. * @param base USDHC peripheral base address.
  837. * @param mask Interrupt status flags mask(@ref _usdhc_interrupt_status_flag).
  838. */
  839. static inline void USDHC_EnableInterruptStatus(USDHC_Type *base, uint32_t mask)
  840. {
  841. base->INT_STATUS_EN |= mask;
  842. }
  843. /*!
  844. * @brief Disables the interrupt status.
  845. *
  846. * @param base USDHC peripheral base address.
  847. * @param mask The interrupt status flags mask(@ref _usdhc_interrupt_status_flag).
  848. */
  849. static inline void USDHC_DisableInterruptStatus(USDHC_Type *base, uint32_t mask)
  850. {
  851. base->INT_STATUS_EN &= ~mask;
  852. }
  853. /*!
  854. * @brief Enables the interrupt signal corresponding to the interrupt status flag.
  855. *
  856. * @param base USDHC peripheral base address.
  857. * @param mask The interrupt status flags mask(@ref _usdhc_interrupt_status_flag).
  858. */
  859. static inline void USDHC_EnableInterruptSignal(USDHC_Type *base, uint32_t mask)
  860. {
  861. base->INT_SIGNAL_EN |= mask;
  862. }
  863. /*!
  864. * @brief Disables the interrupt signal corresponding to the interrupt status flag.
  865. *
  866. * @param base USDHC peripheral base address.
  867. * @param mask The interrupt status flags mask(@ref _usdhc_interrupt_status_flag).
  868. */
  869. static inline void USDHC_DisableInterruptSignal(USDHC_Type *base, uint32_t mask)
  870. {
  871. base->INT_SIGNAL_EN &= ~mask;
  872. }
  873. /* @} */
  874. /*!
  875. * @name Status
  876. * @{
  877. */
  878. /*!
  879. * @brief Gets the enabled interrupt status.
  880. *
  881. * @param base USDHC peripheral base address.
  882. * @return Current interrupt status flags mask(@ref _usdhc_interrupt_status_flag).
  883. */
  884. static inline uint32_t USDHC_GetEnabledInterruptStatusFlags(USDHC_Type *base)
  885. {
  886. uint32_t intStatus = base->INT_STATUS;
  887. return intStatus & base->INT_SIGNAL_EN;
  888. }
  889. /*!
  890. * @brief Gets the current interrupt status.
  891. *
  892. * @param base USDHC peripheral base address.
  893. * @return Current interrupt status flags mask(@ref _usdhc_interrupt_status_flag).
  894. */
  895. static inline uint32_t USDHC_GetInterruptStatusFlags(USDHC_Type *base)
  896. {
  897. return base->INT_STATUS;
  898. }
  899. /*!
  900. * @brief Clears a specified interrupt status.
  901. * write 1 clears
  902. * @param base USDHC peripheral base address.
  903. * @param mask The interrupt status flags mask(@ref _usdhc_interrupt_status_flag).
  904. */
  905. static inline void USDHC_ClearInterruptStatusFlags(USDHC_Type *base, uint32_t mask)
  906. {
  907. base->INT_STATUS = mask;
  908. }
  909. /*!
  910. * @brief Gets the status of auto command 12 error.
  911. *
  912. * @param base USDHC peripheral base address.
  913. * @return Auto command 12 error status flags mask(@ref _usdhc_auto_command12_error_status_flag).
  914. */
  915. static inline uint32_t USDHC_GetAutoCommand12ErrorStatusFlags(USDHC_Type *base)
  916. {
  917. return base->AUTOCMD12_ERR_STATUS;
  918. }
  919. /*!
  920. * @brief Gets the status of the ADMA error.
  921. *
  922. * @param base USDHC peripheral base address.
  923. * @return ADMA error status flags mask(@ref _usdhc_adma_error_status_flag).
  924. */
  925. static inline uint32_t USDHC_GetAdmaErrorStatusFlags(USDHC_Type *base)
  926. {
  927. return base->ADMA_ERR_STATUS & 0xFUL;
  928. }
  929. /*!
  930. * @brief Gets a present status.
  931. *
  932. * This function gets the present USDHC's status except for an interrupt status and an error status.
  933. *
  934. * @param base USDHC peripheral base address.
  935. * @return Present USDHC's status flags mask(@ref _usdhc_present_status_flag).
  936. */
  937. static inline uint32_t USDHC_GetPresentStatusFlags(USDHC_Type *base)
  938. {
  939. return base->PRES_STATE;
  940. }
  941. /* @} */
  942. /*!
  943. * @name Bus Operations
  944. * @{
  945. */
  946. /*!
  947. * @brief Gets the capability information.
  948. *
  949. * @param base USDHC peripheral base address.
  950. * @param capability Structure to save capability information.
  951. */
  952. void USDHC_GetCapability(USDHC_Type *base, usdhc_capability_t *capability);
  953. /*!
  954. * @brief Forces the card clock on.
  955. *
  956. * @param base USDHC peripheral base address.
  957. * @param enable enable/disable flag
  958. */
  959. static inline void USDHC_ForceClockOn(USDHC_Type *base, bool enable)
  960. {
  961. if (enable)
  962. {
  963. base->VEND_SPEC |= USDHC_VEND_SPEC_FRC_SDCLK_ON_MASK;
  964. }
  965. else
  966. {
  967. base->VEND_SPEC &= ~USDHC_VEND_SPEC_FRC_SDCLK_ON_MASK;
  968. }
  969. }
  970. /*!
  971. * @brief Sets the SD bus clock frequency.
  972. *
  973. * @param base USDHC peripheral base address.
  974. * @param srcClock_Hz USDHC source clock frequency united in Hz.
  975. * @param busClock_Hz SD bus clock frequency united in Hz.
  976. *
  977. * @return The nearest frequency of busClock_Hz configured for SD bus.
  978. */
  979. uint32_t USDHC_SetSdClock(USDHC_Type *base, uint32_t srcClock_Hz, uint32_t busClock_Hz);
  980. /*!
  981. * @brief Sends 80 clocks to the card to set it to the active state.
  982. *
  983. * This function must be called each time the card is inserted to ensure that the card can receive the command
  984. * correctly.
  985. *
  986. * @param base USDHC peripheral base address.
  987. * @param timeout Timeout to initialize card.
  988. * @retval true Set card active successfully.
  989. * @retval false Set card active failed.
  990. */
  991. bool USDHC_SetCardActive(USDHC_Type *base, uint32_t timeout);
  992. /*!
  993. * @brief Triggers a hardware reset.
  994. *
  995. * @param base USDHC peripheral base address.
  996. * @param high 1 or 0 level
  997. */
  998. static inline void USDHC_AssertHardwareReset(USDHC_Type *base, bool high)
  999. {
  1000. if (high)
  1001. {
  1002. base->SYS_CTRL |= USDHC_SYS_CTRL_IPP_RST_N_MASK;
  1003. }
  1004. else
  1005. {
  1006. base->SYS_CTRL &= ~USDHC_SYS_CTRL_IPP_RST_N_MASK;
  1007. }
  1008. }
  1009. /*!
  1010. * @brief Sets the data transfer width.
  1011. *
  1012. * @param base USDHC peripheral base address.
  1013. * @param width Data transfer width.
  1014. */
  1015. static inline void USDHC_SetDataBusWidth(USDHC_Type *base, usdhc_data_bus_width_t width)
  1016. {
  1017. base->PROT_CTRL = ((base->PROT_CTRL & ~USDHC_PROT_CTRL_DTW_MASK) | USDHC_PROT_CTRL_DTW(width));
  1018. }
  1019. /*!
  1020. * @brief Fills the data port.
  1021. *
  1022. * This function is used to implement the data transfer by Data Port instead of DMA.
  1023. *
  1024. * @param base USDHC peripheral base address.
  1025. * @param data The data about to be sent.
  1026. */
  1027. static inline void USDHC_WriteData(USDHC_Type *base, uint32_t data)
  1028. {
  1029. base->DATA_BUFF_ACC_PORT = data;
  1030. }
  1031. /*!
  1032. * @brief Retrieves the data from the data port.
  1033. *
  1034. * This function is used to implement the data transfer by Data Port instead of DMA.
  1035. *
  1036. * @param base USDHC peripheral base address.
  1037. * @return The data has been read.
  1038. */
  1039. static inline uint32_t USDHC_ReadData(USDHC_Type *base)
  1040. {
  1041. return base->DATA_BUFF_ACC_PORT;
  1042. }
  1043. /*!
  1044. * @brief Sends command function.
  1045. *
  1046. * @param base USDHC peripheral base address.
  1047. * @param command configuration
  1048. */
  1049. void USDHC_SendCommand(USDHC_Type *base, usdhc_command_t *command);
  1050. /*!
  1051. * @brief Enables or disables a wakeup event in low-power mode.
  1052. *
  1053. * @param base USDHC peripheral base address.
  1054. * @param mask Wakeup events mask(@ref _usdhc_wakeup_event).
  1055. * @param enable True to enable, false to disable.
  1056. */
  1057. static inline void USDHC_EnableWakeupEvent(USDHC_Type *base, uint32_t mask, bool enable)
  1058. {
  1059. if (enable)
  1060. {
  1061. base->PROT_CTRL |= mask;
  1062. }
  1063. else
  1064. {
  1065. base->PROT_CTRL &= ~mask;
  1066. }
  1067. }
  1068. /*!
  1069. * @brief Detects card insert status.
  1070. *
  1071. * @param base USDHC peripheral base address.
  1072. * @param enable enable/disable flag
  1073. */
  1074. static inline void USDHC_CardDetectByData3(USDHC_Type *base, bool enable)
  1075. {
  1076. if (enable)
  1077. {
  1078. base->PROT_CTRL |= USDHC_PROT_CTRL_D3CD_MASK;
  1079. }
  1080. else
  1081. {
  1082. base->PROT_CTRL &= ~USDHC_PROT_CTRL_D3CD_MASK;
  1083. }
  1084. }
  1085. /*!
  1086. * @brief Detects card insert status.
  1087. *
  1088. * @param base USDHC peripheral base address.
  1089. */
  1090. static inline bool USDHC_DetectCardInsert(USDHC_Type *base)
  1091. {
  1092. return ((base->PRES_STATE & (uint32_t)kUSDHC_CardInsertedFlag) != 0UL) ? true : false;
  1093. }
  1094. /*!
  1095. * @brief Enables or disables the SDIO card control.
  1096. *
  1097. * @param base USDHC peripheral base address.
  1098. * @param mask SDIO card control flags mask(@ref _usdhc_sdio_control_flag).
  1099. * @param enable True to enable, false to disable.
  1100. */
  1101. static inline void USDHC_EnableSdioControl(USDHC_Type *base, uint32_t mask, bool enable)
  1102. {
  1103. if (enable)
  1104. {
  1105. base->PROT_CTRL |= mask;
  1106. }
  1107. else
  1108. {
  1109. base->PROT_CTRL &= ~mask;
  1110. }
  1111. }
  1112. /*!
  1113. * @brief Restarts a transaction which has stopped at the block GAP for the SDIO card.
  1114. *
  1115. * @param base USDHC peripheral base address.
  1116. */
  1117. static inline void USDHC_SetContinueRequest(USDHC_Type *base)
  1118. {
  1119. base->PROT_CTRL |= USDHC_PROT_CTRL_CREQ_MASK;
  1120. }
  1121. /*!
  1122. * @brief Request stop at block gap function.
  1123. *
  1124. * @param base USDHC peripheral base address.
  1125. * @param enable True to stop at block gap, false to normal transfer.
  1126. */
  1127. static inline void USDHC_RequestStopAtBlockGap(USDHC_Type *base, bool enable)
  1128. {
  1129. if (enable)
  1130. {
  1131. base->PROT_CTRL |= USDHC_PROT_CTRL_SABGREQ_MASK;
  1132. }
  1133. else
  1134. {
  1135. base->PROT_CTRL &= ~USDHC_PROT_CTRL_SABGREQ_MASK;
  1136. }
  1137. }
  1138. /*!
  1139. * @brief Configures the MMC boot feature.
  1140. *
  1141. * Example:
  1142. @code
  1143. usdhc_boot_config_t config;
  1144. config.ackTimeoutCount = 4;
  1145. config.bootMode = kUSDHC_BootModeNormal;
  1146. config.blockCount = 5;
  1147. config.enableBootAck = true;
  1148. config.enableBoot = true;
  1149. config.enableAutoStopAtBlockGap = true;
  1150. USDHC_SetMmcBootConfig(USDHC, &config);
  1151. @endcode
  1152. *
  1153. * @param base USDHC peripheral base address.
  1154. * @param config The MMC boot configuration information.
  1155. */
  1156. void USDHC_SetMmcBootConfig(USDHC_Type *base, const usdhc_boot_config_t *config);
  1157. /*!
  1158. * @brief Enables or disables the mmc boot mode.
  1159. *
  1160. * @param base USDHC peripheral base address.
  1161. * @param enable True to enable, false to disable.
  1162. */
  1163. static inline void USDHC_EnableMmcBoot(USDHC_Type *base, bool enable)
  1164. {
  1165. if (enable)
  1166. {
  1167. base->MMC_BOOT |= USDHC_MMC_BOOT_BOOT_EN_MASK;
  1168. }
  1169. else
  1170. {
  1171. base->MMC_BOOT &= ~USDHC_MMC_BOOT_BOOT_EN_MASK;
  1172. }
  1173. }
  1174. /*!
  1175. * @brief Forces generating events according to the given mask.
  1176. *
  1177. * @param base USDHC peripheral base address.
  1178. * @param mask The force events bit posistion (_usdhc_force_event).
  1179. */
  1180. static inline void USDHC_SetForceEvent(USDHC_Type *base, uint32_t mask)
  1181. {
  1182. base->FORCE_EVENT = mask;
  1183. }
  1184. /*!
  1185. * @brief Selects the USDHC output voltage.
  1186. *
  1187. * @param base USDHC peripheral base address.
  1188. * @param en18v True means 1.8V, false means 3.0V.
  1189. */
  1190. static inline void UDSHC_SelectVoltage(USDHC_Type *base, bool en18v)
  1191. {
  1192. if (en18v)
  1193. {
  1194. base->VEND_SPEC |= USDHC_VEND_SPEC_VSELECT_MASK;
  1195. }
  1196. else
  1197. {
  1198. base->VEND_SPEC &= ~USDHC_VEND_SPEC_VSELECT_MASK;
  1199. }
  1200. }
  1201. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  1202. /*!
  1203. * @brief Checks the SDR50 mode request tuning bit.
  1204. * When this bit set, application shall perform tuning for SDR50 mode.
  1205. * @param base USDHC peripheral base address.
  1206. */
  1207. static inline bool USDHC_RequestTuningForSDR50(USDHC_Type *base)
  1208. {
  1209. return ((base->HOST_CTRL_CAP & USDHC_HOST_CTRL_CAP_USE_TUNING_SDR50_MASK) != 0UL) ? true : false;
  1210. }
  1211. /*!
  1212. * @brief Checks the request re-tuning bit.
  1213. * When this bit is set, user should do manual tuning or standard tuning function.
  1214. * @param base USDHC peripheral base address.
  1215. */
  1216. static inline bool USDHC_RequestReTuning(USDHC_Type *base)
  1217. {
  1218. return ((base->PRES_STATE & USDHC_PRES_STATE_RTR_MASK) != 0UL) ? true : false;
  1219. }
  1220. /*!
  1221. * @brief The SDR104 mode auto tuning enable and disable.
  1222. * This function should be called after tuning function execute pass, auto tuning will handle
  1223. * by hardware.
  1224. * @param base USDHC peripheral base address.
  1225. * @param enable enable/disable flag
  1226. */
  1227. static inline void USDHC_EnableAutoTuning(USDHC_Type *base, bool enable)
  1228. {
  1229. if (enable)
  1230. {
  1231. base->MIX_CTRL |= USDHC_MIX_CTRL_AUTO_TUNE_EN_MASK;
  1232. }
  1233. else
  1234. {
  1235. base->MIX_CTRL &= ~USDHC_MIX_CTRL_AUTO_TUNE_EN_MASK;
  1236. }
  1237. }
  1238. #if !(defined(FSL_FEATURE_USDHC_REGISTER_HOST_CTRL_CAP_HAS_NO_RETUNING_TIME_COUNTER) && \
  1239. FSL_FEATURE_USDHC_REGISTER_HOST_CTRL_CAP_HAS_NO_RETUNING_TIME_COUNTER)
  1240. /*!
  1241. * @brief Configs the re-tuning timer for mode 1 and mode 3.
  1242. * This timer is used for standard tuning auto re-tuning,
  1243. * @param base USDHC peripheral base address.
  1244. * @param counter timer counter value
  1245. */
  1246. static inline void USDHC_SetRetuningTimer(USDHC_Type *base, uint32_t counter)
  1247. {
  1248. base->HOST_CTRL_CAP &= ~USDHC_HOST_CTRL_CAP_TIME_COUNT_RETUNING_MASK;
  1249. base->HOST_CTRL_CAP |= USDHC_HOST_CTRL_CAP_TIME_COUNT_RETUNING(counter);
  1250. }
  1251. #endif /* FSL_FEATURE_USDHC_REGISTER_HOST_CTRL_CAP_HAS_RETUNING_TIME_COUNTER */
  1252. /*!
  1253. * @brief The auto tuning enbale for CMD/DATA line.
  1254. *
  1255. * @param base USDHC peripheral base address.
  1256. */
  1257. void USDHC_EnableAutoTuningForCmdAndData(USDHC_Type *base);
  1258. /*!
  1259. * @brief Manual tuning trigger or abort.
  1260. * User should handle the tuning cmd and find the boundary of the delay
  1261. * then calucate a average value which will be configured to the <b>CLK_TUNE_CTRL_STATUS</b>
  1262. * This function should be called before function @ref USDHC_AdjustDelayForManualTuning.
  1263. * @param base USDHC peripheral base address.
  1264. * @param enable tuning enable flag
  1265. */
  1266. void USDHC_EnableManualTuning(USDHC_Type *base, bool enable);
  1267. /*!
  1268. * @brief Get the tuning delay cell setting.
  1269. *
  1270. * @param base USDHC peripheral base address.
  1271. * @retval CLK Tuning Control and Status register value.
  1272. */
  1273. static inline uint32_t USDHC_GetTuningDelayStatus(USDHC_Type *base)
  1274. {
  1275. return base->CLK_TUNE_CTRL_STATUS >> 16U;
  1276. }
  1277. /*!
  1278. * @brief The tuning delay cell setting.
  1279. *
  1280. * @param base USDHC peripheral base address.
  1281. * @param preDelay Set the number of delay cells on the feedback clock between the feedback clock and CLK_PRE.
  1282. * @param outDelay Set the number of delay cells on the feedback clock between CLK_PRE and CLK_OUT.
  1283. * @param postDelay Set the number of delay cells on the feedback clock between CLK_OUT and CLK_POST.
  1284. * @retval kStatus_Fail config the delay setting fail
  1285. * @retval kStatus_Success config the delay setting success
  1286. */
  1287. status_t USDHC_SetTuningDelay(USDHC_Type *base, uint32_t preDelay, uint32_t outDelay, uint32_t postDelay);
  1288. /*!
  1289. * @brief Adjusts delay for mannual tuning.
  1290. * @deprecated Do not use this function. It has been superceded by USDHC_SetTuingDelay
  1291. * @param base USDHC peripheral base address.
  1292. * @param delay setting configuration
  1293. * @retval #kStatus_Fail config the delay setting fail
  1294. * @retval #kStatus_Success config the delay setting success
  1295. */
  1296. status_t USDHC_AdjustDelayForManualTuning(USDHC_Type *base, uint32_t delay);
  1297. /*!
  1298. * @brief set tuning counter tuning.
  1299. * @param base USDHC peripheral base address.
  1300. * @param counter tuning counter
  1301. * @retval #kStatus_Fail config the delay setting fail
  1302. * @retval #kStatus_Success config the delay setting success
  1303. */
  1304. static inline void USDHC_SetStandardTuningCounter(USDHC_Type *base, uint8_t counter)
  1305. {
  1306. base->TUNING_CTRL =
  1307. (base->TUNING_CTRL & (~USDHC_TUNING_CTRL_TUNING_COUNTER_MASK)) | USDHC_TUNING_CTRL_TUNING_COUNTER(counter);
  1308. }
  1309. /*!
  1310. * @brief The enable standard tuning function.
  1311. * The standard tuning window and tuning counter using the default config
  1312. * tuning cmd is sent by the software, user need to check whether the tuning result
  1313. * can be used for SDR50, SDR104, and HS200 mode tuning.
  1314. * @param base USDHC peripheral base address.
  1315. * @param tuningStartTap start tap
  1316. * @param step tuning step
  1317. * @param enable enable/disable flag
  1318. */
  1319. void USDHC_EnableStandardTuning(USDHC_Type *base, uint32_t tuningStartTap, uint32_t step, bool enable);
  1320. /*!
  1321. * @brief Gets execute STD tuning status.
  1322. *
  1323. * @param base USDHC peripheral base address.
  1324. */
  1325. static inline uint32_t USDHC_GetExecuteStdTuningStatus(USDHC_Type *base)
  1326. {
  1327. return (base->AUTOCMD12_ERR_STATUS & USDHC_AUTOCMD12_ERR_STATUS_EXECUTE_TUNING_MASK);
  1328. }
  1329. /*!
  1330. * @brief Checks STD tuning result.
  1331. *
  1332. * @param base USDHC peripheral base address.
  1333. */
  1334. static inline uint32_t USDHC_CheckStdTuningResult(USDHC_Type *base)
  1335. {
  1336. return (base->AUTOCMD12_ERR_STATUS & USDHC_AUTOCMD12_ERR_STATUS_SMP_CLK_SEL_MASK);
  1337. }
  1338. /*!
  1339. * @brief Checks tuning error.
  1340. *
  1341. * @param base USDHC peripheral base address.
  1342. */
  1343. static inline uint32_t USDHC_CheckTuningError(USDHC_Type *base)
  1344. {
  1345. return (base->CLK_TUNE_CTRL_STATUS &
  1346. (USDHC_CLK_TUNE_CTRL_STATUS_NXT_ERR_MASK | USDHC_CLK_TUNE_CTRL_STATUS_PRE_ERR_MASK));
  1347. }
  1348. #endif
  1349. /*!
  1350. * @brief The enable/disable DDR mode.
  1351. *
  1352. * @param base USDHC peripheral base address.
  1353. * @param enable enable/disable flag
  1354. * @param nibblePos nibble position
  1355. */
  1356. void USDHC_EnableDDRMode(USDHC_Type *base, bool enable, uint32_t nibblePos);
  1357. #if FSL_FEATURE_USDHC_HAS_HS400_MODE
  1358. /*!
  1359. * @brief The enable/disable HS400 mode.
  1360. *
  1361. * @param base USDHC peripheral base address.
  1362. * @param enable enable/disable flag
  1363. */
  1364. static inline void USDHC_EnableHS400Mode(USDHC_Type *base, bool enable)
  1365. {
  1366. if (enable)
  1367. {
  1368. base->MIX_CTRL |= USDHC_MIX_CTRL_HS400_MODE_MASK;
  1369. }
  1370. else
  1371. {
  1372. base->MIX_CTRL &= ~USDHC_MIX_CTRL_HS400_MODE_MASK;
  1373. }
  1374. }
  1375. /*!
  1376. * @brief Resets the strobe DLL.
  1377. *
  1378. * @param base USDHC peripheral base address.
  1379. */
  1380. static inline void USDHC_ResetStrobeDLL(USDHC_Type *base)
  1381. {
  1382. base->STROBE_DLL_CTRL |= USDHC_STROBE_DLL_CTRL_STROBE_DLL_CTRL_RESET_MASK;
  1383. }
  1384. /*!
  1385. * @brief Enables/disables the strobe DLL.
  1386. *
  1387. * @param base USDHC peripheral base address.
  1388. * @param enable enable/disable flag
  1389. */
  1390. static inline void USDHC_EnableStrobeDLL(USDHC_Type *base, bool enable)
  1391. {
  1392. if (enable)
  1393. {
  1394. base->STROBE_DLL_CTRL |= USDHC_STROBE_DLL_CTRL_STROBE_DLL_CTRL_ENABLE_MASK;
  1395. }
  1396. else
  1397. {
  1398. base->STROBE_DLL_CTRL &= ~USDHC_STROBE_DLL_CTRL_STROBE_DLL_CTRL_ENABLE_MASK;
  1399. }
  1400. }
  1401. /*!
  1402. * @brief Configs the strobe DLL delay target and update interval.
  1403. *
  1404. * @param base USDHC peripheral base address.
  1405. * @param delayTarget delay target
  1406. * @param updateInterval update interval
  1407. */
  1408. void USDHC_ConfigStrobeDLL(USDHC_Type *base, uint32_t delayTarget, uint32_t updateInterval);
  1409. /*!
  1410. * @brief Enables manual override for slave delay chain using <b>STROBE_SLV_OVERRIDE_VAL</b>.
  1411. *
  1412. * @param base USDHC peripheral base address.
  1413. * @param delayTaps Valid delay taps range from 1 - 128 taps. A value of 0 selects tap 1, and a value of 0x7F selects
  1414. * tap 128.
  1415. */
  1416. static inline void USDHC_SetStrobeDllOverride(USDHC_Type *base, uint32_t delayTaps)
  1417. {
  1418. base->STROBE_DLL_CTRL &= (USDHC_STROBE_DLL_CTRL_STROBE_DLL_CTRL_ENABLE_MASK |
  1419. USDHC_STROBE_DLL_CTRL_STROBE_DLL_CTRL_SLV_OVERRIDE_VAL_MASK);
  1420. base->STROBE_DLL_CTRL |= USDHC_STROBE_DLL_CTRL_STROBE_DLL_CTRL_SLV_OVERRIDE_MASK |
  1421. USDHC_STROBE_DLL_CTRL_STROBE_DLL_CTRL_SLV_OVERRIDE_VAL(delayTaps);
  1422. }
  1423. /*!
  1424. * @brief Gets the strobe DLL status.
  1425. *
  1426. * @param base USDHC peripheral base address.
  1427. */
  1428. static inline uint32_t USDHC_GetStrobeDLLStatus(USDHC_Type *base)
  1429. {
  1430. return base->STROBE_DLL_STATUS;
  1431. }
  1432. #endif
  1433. /*!
  1434. * @brief USDHC data configuration.
  1435. *
  1436. * @param base USDHC peripheral base address.
  1437. * @param dataDirection Data direction, tx or rx.
  1438. * @param blockCount Data block count.
  1439. * @param blockSize Data block size.
  1440. *
  1441. */
  1442. void USDHC_SetDataConfig(USDHC_Type *base,
  1443. usdhc_transfer_direction_t dataDirection,
  1444. uint32_t blockCount,
  1445. uint32_t blockSize);
  1446. /* @} */
  1447. /*!
  1448. * @name Transactional functions
  1449. * @{
  1450. */
  1451. /*!
  1452. * @brief Creates the USDHC handle.
  1453. *
  1454. * @param base USDHC peripheral base address.
  1455. * @param handle USDHC handle pointer.
  1456. * @param callback Structure pointer to contain all callback functions.
  1457. * @param userData Callback function parameter.
  1458. */
  1459. void USDHC_TransferCreateHandle(USDHC_Type *base,
  1460. usdhc_handle_t *handle,
  1461. const usdhc_transfer_callback_t *callback,
  1462. void *userData);
  1463. #if (defined FSL_USDHC_ENABLE_SCATTER_GATHER_TRANSFER) && FSL_USDHC_ENABLE_SCATTER_GATHER_TRANSFER
  1464. /*!
  1465. * @brief Transfers the command/scatter gather data using an interrupt and an asynchronous method.
  1466. *
  1467. * This function sends a command and data and returns immediately. It doesn't wait for the transfer to complete or
  1468. * to encounter an error. The application must not call this API in multiple threads at the same time. Because of that
  1469. * this API doesn't support the re-entry mechanism.
  1470. * This function is target for the application would like to have scatter gather buffer to be transferred within one
  1471. * read/write request, non scatter gather buffer is support by this function also.
  1472. *
  1473. * @note Call API @ref USDHC_TransferCreateHandle when calling this API.
  1474. *
  1475. * @param base USDHC peripheral base address.
  1476. * @param handle USDHC handle.
  1477. * @param dmaConfig adma configurations, must be not NULL, since the function is target for ADMA only.
  1478. * @param transfer scatter gather transfer content.
  1479. *
  1480. * @retval #kStatus_InvalidArgument Argument is invalid.
  1481. * @retval #kStatus_USDHC_BusyTransferring Busy transferring.
  1482. * @retval #kStatus_USDHC_PrepareAdmaDescriptorFailed Prepare ADMA descriptor failed.
  1483. * @retval #kStatus_Success Operate successfully.
  1484. */
  1485. status_t USDHC_TransferScatterGatherADMANonBlocking(USDHC_Type *base,
  1486. usdhc_handle_t *handle,
  1487. usdhc_adma_config_t *dmaConfig,
  1488. usdhc_scatter_gather_transfer_t *transfer);
  1489. #else
  1490. /*!
  1491. * @brief Transfers the command/data using an interrupt and an asynchronous method.
  1492. *
  1493. * This function sends a command and data and returns immediately. It doesn't wait for the transfer to complete or
  1494. * to encounter an error. The application must not call this API in multiple threads at the same time. Because of that
  1495. * this API doesn't support the re-entry mechanism.
  1496. *
  1497. * @note Call API @ref USDHC_TransferCreateHandle when calling this API.
  1498. *
  1499. * @param base USDHC peripheral base address.
  1500. * @param handle USDHC handle.
  1501. * @param dmaConfig ADMA configuration.
  1502. * @param transfer Transfer content.
  1503. * @retval #kStatus_InvalidArgument Argument is invalid.
  1504. * @retval #kStatus_USDHC_BusyTransferring Busy transferring.
  1505. * @retval #kStatus_USDHC_PrepareAdmaDescriptorFailed Prepare ADMA descriptor failed.
  1506. * @retval #kStatus_Success Operate successfully.
  1507. */
  1508. status_t USDHC_TransferNonBlocking(USDHC_Type *base,
  1509. usdhc_handle_t *handle,
  1510. usdhc_adma_config_t *dmaConfig,
  1511. usdhc_transfer_t *transfer);
  1512. #endif
  1513. /*!
  1514. * @brief Transfers the command/data using a blocking method.
  1515. *
  1516. * This function waits until the command response/data is received or the USDHC encounters an error by polling the
  1517. * status flag. \n
  1518. * The application must not call this API in multiple threads at the same time. Because this API doesn't
  1519. * support the re-entry mechanism.
  1520. *
  1521. * @note There is no need to call API @ref USDHC_TransferCreateHandle when calling this API.
  1522. *
  1523. * @param base USDHC peripheral base address.
  1524. * @param dmaConfig adma configuration
  1525. * @param transfer Transfer content.
  1526. * @retval #kStatus_InvalidArgument Argument is invalid.
  1527. * @retval #kStatus_USDHC_PrepareAdmaDescriptorFailed Prepare ADMA descriptor failed.
  1528. * @retval #kStatus_USDHC_SendCommandFailed Send command failed.
  1529. * @retval #kStatus_USDHC_TransferDataFailed Transfer data failed.
  1530. * @retval #kStatus_Success Operate successfully.
  1531. */
  1532. status_t USDHC_TransferBlocking(USDHC_Type *base, usdhc_adma_config_t *dmaConfig, usdhc_transfer_t *transfer);
  1533. /*!
  1534. * @brief IRQ handler for the USDHC.
  1535. *
  1536. * This function deals with the IRQs on the given host controller.
  1537. *
  1538. * @param base USDHC peripheral base address.
  1539. * @param handle USDHC handle.
  1540. */
  1541. void USDHC_TransferHandleIRQ(USDHC_Type *base, usdhc_handle_t *handle);
  1542. /* @} */
  1543. #if defined(__cplusplus)
  1544. }
  1545. #endif
  1546. /*! @} */
  1547. #endif /* _FSL_USDHC_H_*/