fsl_usdhc.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480
  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. #include "fsl_usdhc.h"
  9. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  10. #include "fsl_cache.h"
  11. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  12. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  13. #include "fsl_memory.h"
  14. #endif
  15. /*******************************************************************************
  16. * Definitions
  17. ******************************************************************************/
  18. /* Component ID definition, used by tools. */
  19. #ifndef FSL_COMPONENT_ID
  20. #define FSL_COMPONENT_ID "platform.drivers.usdhc"
  21. #endif
  22. /*! @brief Clock setting */
  23. /* Max SD clock divisor from base clock */
  24. #define USDHC_MAX_DVS ((USDHC_SYS_CTRL_DVS_MASK >> USDHC_SYS_CTRL_DVS_SHIFT) + 1U)
  25. #define USDHC_MAX_CLKFS ((USDHC_SYS_CTRL_SDCLKFS_MASK >> USDHC_SYS_CTRL_SDCLKFS_SHIFT) + 1U)
  26. #define USDHC_PREV_DVS(x) ((x) -= 1U)
  27. #define USDHC_PREV_CLKFS(x, y) ((x) >>= (y))
  28. /*! @brief USDHC ADMA table address align size */
  29. #define USDHC_ADMA_TABLE_ADDRESS_ALIGN (4U)
  30. /* Typedef for interrupt handler. */
  31. typedef void (*usdhc_isr_t)(USDHC_Type *base, usdhc_handle_t *handle);
  32. /*! @brief check flag avalibility */
  33. #define IS_USDHC_FLAG_SET(reg, flag) (((reg) & ((uint32_t)flag)) != 0UL)
  34. /*! @brief usdhc transfer flags */
  35. enum _usdhc_transfer_flags
  36. {
  37. kUSDHC_CommandOnly = 1U, /*!< transfer command only */
  38. kUSDHC_CommandAndTxData = 2U, /*!< transfer command and transmit data */
  39. kUSDHC_CommandAndRxData = 4U, /*!< transfer command and receive data */
  40. kUSDHC_DataWithAutoCmd12 = 8U, /*!< transfer data with auto cmd12 enabled */
  41. kUSDHC_DataWithAutoCmd23 = 16U, /*!< transfer data with auto cmd23 enabled */
  42. kUSDHC_BootData = 32U, /*!< transfer boot data */
  43. kUSDHC_BootDataContinuous = 64U, /*!< transfer boot data continuous */
  44. };
  45. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  46. #define USDHC_ADDR_CPU_2_DMA(addr) (MEMORY_ConvertMemoryMapAddress((addr), kMEMORY_Local2DMA))
  47. #else
  48. #define USDHC_ADDR_CPU_2_DMA(addr) (addr)
  49. #endif
  50. /*******************************************************************************
  51. * Prototypes
  52. ******************************************************************************/
  53. /*!
  54. * @brief Get the instance.
  55. *
  56. * @param base USDHC peripheral base address.
  57. * @return Instance number.
  58. */
  59. static uint32_t USDHC_GetInstance(USDHC_Type *base);
  60. /*!
  61. * @brief Start transfer according to current transfer state
  62. *
  63. * @param base USDHC peripheral base address.
  64. * @param transferFlags transfer flags, @ref _usdhc_transfer_flags.
  65. * @param blockSize block size.
  66. * @param blockCount block count.
  67. */
  68. static status_t USDHC_SetTransferConfig(USDHC_Type *base,
  69. uint32_t transferFlags,
  70. size_t blockSize,
  71. uint32_t blockCount);
  72. /*!
  73. * @brief Receive command response
  74. *
  75. * @param base USDHC peripheral base address.
  76. * @param command Command to be sent.
  77. */
  78. static status_t USDHC_ReceiveCommandResponse(USDHC_Type *base, usdhc_command_t *command);
  79. /*!
  80. * @brief Read DATAPORT when buffer enable bit is set.
  81. *
  82. * @param base USDHC peripheral base address.
  83. * @param data Data to be read.
  84. * @param transferredWords The number of data words have been transferred last time transaction.
  85. * @return The number of total data words have been transferred after this time transaction.
  86. */
  87. static uint32_t USDHC_ReadDataPort(USDHC_Type *base, usdhc_data_t *data, uint32_t transferredWords);
  88. /*!
  89. * @brief Read data by using DATAPORT polling way.
  90. *
  91. * @param base USDHC peripheral base address.
  92. * @param data Data to be read.
  93. * @retval kStatus_Fail Read DATAPORT failed.
  94. * @retval kStatus_Success Operate successfully.
  95. */
  96. static status_t USDHC_ReadByDataPortBlocking(USDHC_Type *base, usdhc_data_t *data);
  97. /*!
  98. * @brief Write DATAPORT when buffer enable bit is set.
  99. *
  100. * @param base USDHC peripheral base address.
  101. * @param data Data to be read.
  102. * @param transferredWords The number of data words have been transferred last time.
  103. * @return The number of total data words have been transferred after this time transaction.
  104. */
  105. static uint32_t USDHC_WriteDataPort(USDHC_Type *base, usdhc_data_t *data, uint32_t transferredWords);
  106. /*!
  107. * @brief Write data by using DATAPORT polling way.
  108. *
  109. * @param base USDHC peripheral base address.
  110. * @param data Data to be transferred.
  111. * @retval kStatus_Fail Write DATAPORT failed.
  112. * @retval kStatus_Success Operate successfully.
  113. */
  114. static status_t USDHC_WriteByDataPortBlocking(USDHC_Type *base, usdhc_data_t *data);
  115. /*!
  116. * @brief Transfer data by polling way.
  117. *
  118. * @param base USDHC peripheral base address.
  119. * @param data Data to be transferred.
  120. * @param use DMA flag.
  121. * @retval kStatus_Fail Transfer data failed.
  122. * @retval kStatus_InvalidArgument Argument is invalid.
  123. * @retval kStatus_Success Operate successfully.
  124. */
  125. static status_t USDHC_TransferDataBlocking(USDHC_Type *base, usdhc_data_t *data, bool enDMA);
  126. /*!
  127. * @brief wait command done
  128. *
  129. * @param base USDHC peripheral base address.
  130. * @param command configuration
  131. * @param pollingCmdDone polling command done flag
  132. */
  133. static status_t USDHC_WaitCommandDone(USDHC_Type *base, usdhc_command_t *command, bool pollingCmdDone);
  134. /*!
  135. * @brief Handle card detect interrupt.
  136. *
  137. * @param base USDHC peripheral base address.
  138. * @param handle USDHC handle.
  139. * @param interruptFlags Card detect related interrupt flags.
  140. */
  141. static void USDHC_TransferHandleCardDetect(USDHC_Type *base, usdhc_handle_t *handle, uint32_t interruptFlags);
  142. /*!
  143. * @brief Handle command interrupt.
  144. *
  145. * @param base USDHC peripheral base address.
  146. * @param handle USDHC handle.
  147. * @param interruptFlags Command related interrupt flags.
  148. */
  149. static void USDHC_TransferHandleCommand(USDHC_Type *base, usdhc_handle_t *handle, uint32_t interruptFlags);
  150. /*!
  151. * @brief Handle data interrupt.
  152. *
  153. * @param base USDHC peripheral base address.
  154. * @param handle USDHC handle.
  155. * @param interruptFlags Data related interrupt flags.
  156. */
  157. static void USDHC_TransferHandleData(USDHC_Type *base, usdhc_handle_t *handle, uint32_t interruptFlags);
  158. /*!
  159. * @brief Handle SDIO card interrupt signal.
  160. *
  161. * @param base USDHC peripheral base address.
  162. * @param handle USDHC handle.
  163. */
  164. static void USDHC_TransferHandleSdioInterrupt(USDHC_Type *base, usdhc_handle_t *handle);
  165. /*!
  166. * @brief Handle SDIO block gap event.
  167. *
  168. * @param base USDHC peripheral base address.
  169. * @param handle USDHC handle.
  170. */
  171. static void USDHC_TransferHandleBlockGap(USDHC_Type *base, usdhc_handle_t *handle);
  172. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  173. /*!
  174. * @brief Handle retuning
  175. *
  176. * @param base USDHC peripheral base address.
  177. * @param handle USDHC handle.
  178. * @param interrupt flags
  179. */
  180. static void USDHC_TransferHandleReTuning(USDHC_Type *base, usdhc_handle_t *handle, uint32_t interruptFlags);
  181. #endif
  182. /*******************************************************************************
  183. * Variables
  184. ******************************************************************************/
  185. /*! @brief USDHC base pointer array */
  186. static USDHC_Type *const s_usdhcBase[] = USDHC_BASE_PTRS;
  187. /*! @brief USDHC internal handle pointer array */
  188. static usdhc_handle_t *s_usdhcHandle[ARRAY_SIZE(s_usdhcBase)] = {0};
  189. /*! @brief USDHC IRQ name array */
  190. static const IRQn_Type s_usdhcIRQ[] = USDHC_IRQS;
  191. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  192. /*! @brief USDHC clock array name */
  193. static const clock_ip_name_t s_usdhcClock[] = USDHC_CLOCKS;
  194. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  195. #if (defined(FSL_FEATURE_USDHC_HAS_RESET) && FSL_FEATURE_USDHC_HAS_RESET)
  196. /*! @brief Pointers to USDHC resets for each instance. */
  197. static const reset_ip_name_t s_usdhcResets[] = USDHC_RSTS;
  198. #endif
  199. /* USDHC ISR for transactional APIs. */
  200. #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  201. static usdhc_isr_t s_usdhcIsr = (usdhc_isr_t)DefaultISR;
  202. #else
  203. static usdhc_isr_t s_usdhcIsr;
  204. #endif
  205. /*! @brief Dummy data buffer for mmc boot mode */
  206. AT_NONCACHEABLE_SECTION_ALIGN(static uint32_t s_usdhcBootDummy, USDHC_ADMA2_ADDRESS_ALIGN);
  207. /*******************************************************************************
  208. * Code
  209. ******************************************************************************/
  210. static uint32_t USDHC_GetInstance(USDHC_Type *base)
  211. {
  212. uint8_t instance = 0;
  213. while ((instance < ARRAY_SIZE(s_usdhcBase)) && (s_usdhcBase[instance] != base))
  214. {
  215. instance++;
  216. }
  217. assert(instance < ARRAY_SIZE(s_usdhcBase));
  218. return instance;
  219. }
  220. static status_t USDHC_SetTransferConfig(USDHC_Type *base, uint32_t transferFlags, size_t blockSize, uint32_t blockCount)
  221. {
  222. uint32_t mixCtrl = base->MIX_CTRL;
  223. if (((uint32_t)kUSDHC_CommandOnly & transferFlags) != 0U)
  224. {
  225. /* clear data flags */
  226. mixCtrl &= ~(USDHC_MIX_CTRL_MSBSEL_MASK | USDHC_MIX_CTRL_BCEN_MASK | USDHC_MIX_CTRL_DTDSEL_MASK |
  227. USDHC_MIX_CTRL_AC12EN_MASK | USDHC_MIX_CTRL_AC23EN_MASK);
  228. if (IS_USDHC_FLAG_SET(base->PRES_STATE, kUSDHC_CommandInhibitFlag))
  229. {
  230. return kStatus_USDHC_BusyTransferring;
  231. }
  232. }
  233. else
  234. {
  235. /* if transfer boot continous, only need set the CREQ bit, leave others as it is */
  236. if ((transferFlags & (uint32_t)kUSDHC_BootDataContinuous) != 0U)
  237. {
  238. /* clear stop at block gap request */
  239. base->PROT_CTRL &= ~USDHC_PROT_CTRL_SABGREQ_MASK;
  240. /* continous transfer data */
  241. base->PROT_CTRL |= USDHC_PROT_CTRL_CREQ_MASK;
  242. return kStatus_Success;
  243. }
  244. /* check data inhibit flag */
  245. if (IS_USDHC_FLAG_SET(base->PRES_STATE, kUSDHC_DataInhibitFlag))
  246. {
  247. return kStatus_USDHC_BusyTransferring;
  248. }
  249. /* check transfer block count */
  250. if ((blockCount > USDHC_MAX_BLOCK_COUNT))
  251. {
  252. return kStatus_InvalidArgument;
  253. }
  254. /* config mix parameter */
  255. mixCtrl &= ~(USDHC_MIX_CTRL_MSBSEL_MASK | USDHC_MIX_CTRL_BCEN_MASK | USDHC_MIX_CTRL_DTDSEL_MASK |
  256. USDHC_MIX_CTRL_AC12EN_MASK);
  257. if ((transferFlags & (uint32_t)kUSDHC_CommandAndRxData) != 0U)
  258. {
  259. mixCtrl |= USDHC_MIX_CTRL_DTDSEL_MASK;
  260. }
  261. if (blockCount > 1U)
  262. {
  263. mixCtrl |= USDHC_MIX_CTRL_MSBSEL_MASK | USDHC_MIX_CTRL_BCEN_MASK;
  264. /* auto command 12 */
  265. if ((transferFlags & (uint32_t)kUSDHC_DataWithAutoCmd12) != 0U)
  266. {
  267. mixCtrl |= USDHC_MIX_CTRL_AC12EN_MASK;
  268. }
  269. }
  270. /* auto command 23, auto send set block count cmd before multiple read/write */
  271. if ((transferFlags & (uint32_t)kUSDHC_DataWithAutoCmd23) != 0U)
  272. {
  273. mixCtrl |= USDHC_MIX_CTRL_AC23EN_MASK;
  274. base->VEND_SPEC2 |= USDHC_VEND_SPEC2_ACMD23_ARGU2_EN_MASK;
  275. /* config the block count to DS_ADDR */
  276. base->DS_ADDR = blockCount;
  277. }
  278. else
  279. {
  280. mixCtrl &= ~USDHC_MIX_CTRL_AC23EN_MASK;
  281. base->VEND_SPEC2 &= ~USDHC_VEND_SPEC2_ACMD23_ARGU2_EN_MASK;
  282. }
  283. /* if transfer boot data, leave the block count to USDHC_SetMmcBootConfig function */
  284. if ((transferFlags & (uint32_t)kUSDHC_BootData) == 0U)
  285. {
  286. /* config data block size/block count */
  287. base->BLK_ATT = ((base->BLK_ATT & ~(USDHC_BLK_ATT_BLKSIZE_MASK | USDHC_BLK_ATT_BLKCNT_MASK)) |
  288. (USDHC_BLK_ATT_BLKSIZE(blockSize) | USDHC_BLK_ATT_BLKCNT(blockCount)));
  289. }
  290. else
  291. {
  292. mixCtrl |= USDHC_MIX_CTRL_MSBSEL_MASK | USDHC_MIX_CTRL_BCEN_MASK;
  293. base->PROT_CTRL |= USDHC_PROT_CTRL_RD_DONE_NO_8CLK_MASK;
  294. }
  295. }
  296. /* config the mix parameter */
  297. base->MIX_CTRL = mixCtrl;
  298. return kStatus_Success;
  299. }
  300. void USDHC_SetDataConfig(USDHC_Type *base,
  301. usdhc_transfer_direction_t dataDirection,
  302. uint32_t blockCount,
  303. uint32_t blockSize)
  304. {
  305. assert(blockCount <= USDHC_MAX_BLOCK_COUNT);
  306. uint32_t mixCtrl = base->MIX_CTRL;
  307. /* block attribute configuration */
  308. base->BLK_ATT = ((base->BLK_ATT & ~(USDHC_BLK_ATT_BLKSIZE_MASK | USDHC_BLK_ATT_BLKCNT_MASK)) |
  309. (USDHC_BLK_ATT_BLKSIZE(blockSize) | USDHC_BLK_ATT_BLKCNT(blockCount)));
  310. /* config mix parameter */
  311. mixCtrl &= ~(USDHC_MIX_CTRL_MSBSEL_MASK | USDHC_MIX_CTRL_BCEN_MASK | USDHC_MIX_CTRL_DTDSEL_MASK);
  312. mixCtrl |= USDHC_MIX_CTRL_DTDSEL(dataDirection) | (blockCount > 1U ? USDHC_MIX_CTRL_MSBSEL_MASK : 0U);
  313. base->MIX_CTRL = mixCtrl;
  314. }
  315. static status_t USDHC_ReceiveCommandResponse(USDHC_Type *base, usdhc_command_t *command)
  316. {
  317. assert(command != NULL);
  318. uint32_t response0 = base->CMD_RSP0;
  319. uint32_t response1 = base->CMD_RSP1;
  320. uint32_t response2 = base->CMD_RSP2;
  321. if (command->responseType != kCARD_ResponseTypeNone)
  322. {
  323. command->response[0U] = response0;
  324. if (command->responseType == kCARD_ResponseTypeR2)
  325. {
  326. /* R3-R2-R1-R0(lowest 8 bit is invalid bit) has the same format as R2 format in SD specification document
  327. after removed internal CRC7 and end bit. */
  328. command->response[0U] <<= 8U;
  329. command->response[1U] = (response1 << 8U) | ((response0 & 0xFF000000U) >> 24U);
  330. command->response[2U] = (response2 << 8U) | ((response1 & 0xFF000000U) >> 24U);
  331. command->response[3U] = (base->CMD_RSP3 << 8U) | ((response2 & 0xFF000000U) >> 24U);
  332. }
  333. }
  334. /* check response error flag */
  335. if ((command->responseErrorFlags != 0U) &&
  336. ((command->responseType == kCARD_ResponseTypeR1) || (command->responseType == kCARD_ResponseTypeR1b) ||
  337. (command->responseType == kCARD_ResponseTypeR6) || (command->responseType == kCARD_ResponseTypeR5)))
  338. {
  339. if (((command->responseErrorFlags) & (command->response[0U])) != 0U)
  340. {
  341. return kStatus_USDHC_SendCommandFailed;
  342. }
  343. }
  344. return kStatus_Success;
  345. }
  346. static uint32_t USDHC_ReadDataPort(USDHC_Type *base, usdhc_data_t *data, uint32_t transferredWords)
  347. {
  348. uint32_t i;
  349. uint32_t totalWords;
  350. uint32_t wordsCanBeRead; /* The words can be read at this time. */
  351. uint32_t readWatermark = ((base->WTMK_LVL & USDHC_WTMK_LVL_RD_WML_MASK) >> USDHC_WTMK_LVL_RD_WML_SHIFT);
  352. /* If DMA is enable, do not need to polling data port */
  353. if ((base->MIX_CTRL & USDHC_MIX_CTRL_DMAEN_MASK) == 0U)
  354. {
  355. /*
  356. * Add non aligned access support ,user need make sure your buffer size is big
  357. * enough to hold the data,in other words,user need make sure the buffer size
  358. * is 4 byte aligned
  359. */
  360. if (data->blockSize % sizeof(uint32_t) != 0U)
  361. {
  362. data->blockSize +=
  363. sizeof(uint32_t) - (data->blockSize % sizeof(uint32_t)); /* make the block size as word-aligned */
  364. }
  365. totalWords = ((data->blockCount * data->blockSize) / sizeof(uint32_t));
  366. /* If watermark level is equal or bigger than totalWords, transfers totalWords data. */
  367. if (readWatermark >= totalWords)
  368. {
  369. wordsCanBeRead = totalWords;
  370. }
  371. /* If watermark level is less than totalWords and left words to be sent is equal or bigger than readWatermark,
  372. transfers watermark level words. */
  373. else if ((readWatermark < totalWords) && ((totalWords - transferredWords) >= readWatermark))
  374. {
  375. wordsCanBeRead = readWatermark;
  376. }
  377. /* If watermark level is less than totalWords and left words to be sent is less than readWatermark, transfers
  378. left
  379. words. */
  380. else
  381. {
  382. wordsCanBeRead = (totalWords - transferredWords);
  383. }
  384. i = 0U;
  385. while (i < wordsCanBeRead)
  386. {
  387. data->rxData[transferredWords++] = USDHC_ReadData(base);
  388. i++;
  389. }
  390. }
  391. return transferredWords;
  392. }
  393. static status_t USDHC_ReadByDataPortBlocking(USDHC_Type *base, usdhc_data_t *data)
  394. {
  395. uint32_t totalWords;
  396. uint32_t transferredWords = 0U, interruptStatus = 0U;
  397. status_t error = kStatus_Success;
  398. /*
  399. * Add non aligned access support ,user need make sure your buffer size is big
  400. * enough to hold the data,in other words,user need make sure the buffer size
  401. * is 4 byte aligned
  402. */
  403. if (data->blockSize % sizeof(uint32_t) != 0U)
  404. {
  405. data->blockSize +=
  406. sizeof(uint32_t) - (data->blockSize % sizeof(uint32_t)); /* make the block size as word-aligned */
  407. }
  408. totalWords = ((data->blockCount * data->blockSize) / sizeof(uint32_t));
  409. while ((error == kStatus_Success) && (transferredWords < totalWords))
  410. {
  411. while (
  412. !(IS_USDHC_FLAG_SET(interruptStatus, ((uint32_t)kUSDHC_BufferReadReadyFlag |
  413. (uint32_t)kUSDHC_DataErrorFlag | (uint32_t)kUSDHC_TuningErrorFlag))))
  414. {
  415. interruptStatus = USDHC_GetInterruptStatusFlags(base);
  416. }
  417. /* during std tuning process, software do not need to read data, but wait BRR is enough */
  418. if ((data->dataType == (uint32_t)kUSDHC_TransferDataTuning) &&
  419. (IS_USDHC_FLAG_SET(interruptStatus, kUSDHC_BufferReadReadyFlag)))
  420. {
  421. USDHC_ClearInterruptStatusFlags(base, kUSDHC_BufferReadReadyFlag);
  422. return kStatus_Success;
  423. }
  424. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  425. else if (IS_USDHC_FLAG_SET(interruptStatus, kUSDHC_TuningErrorFlag))
  426. {
  427. USDHC_ClearInterruptStatusFlags(base, kUSDHC_TuningErrorFlag);
  428. /* if tuning error occur ,return directly */
  429. error = kStatus_USDHC_TuningError;
  430. }
  431. #endif
  432. else if (IS_USDHC_FLAG_SET(interruptStatus, kUSDHC_DataErrorFlag))
  433. {
  434. if (!(data->enableIgnoreError))
  435. {
  436. error = kStatus_Fail;
  437. }
  438. /* clear data error flag */
  439. USDHC_ClearInterruptStatusFlags(base, kUSDHC_DataErrorFlag);
  440. }
  441. else
  442. {
  443. /* Intentional empty */
  444. }
  445. if (error == kStatus_Success)
  446. {
  447. transferredWords = USDHC_ReadDataPort(base, data, transferredWords);
  448. /* clear buffer read ready */
  449. USDHC_ClearInterruptStatusFlags(base, kUSDHC_BufferReadReadyFlag);
  450. interruptStatus = 0U;
  451. }
  452. }
  453. /* Clear data complete flag after the last read operation. */
  454. USDHC_ClearInterruptStatusFlags(base, kUSDHC_DataCompleteFlag);
  455. return error;
  456. }
  457. static uint32_t USDHC_WriteDataPort(USDHC_Type *base, usdhc_data_t *data, uint32_t transferredWords)
  458. {
  459. uint32_t i;
  460. uint32_t totalWords;
  461. uint32_t wordsCanBeWrote; /* Words can be wrote at this time. */
  462. uint32_t writeWatermark = ((base->WTMK_LVL & USDHC_WTMK_LVL_WR_WML_MASK) >> USDHC_WTMK_LVL_WR_WML_SHIFT);
  463. /* If DMA is enable, do not need to polling data port */
  464. if ((base->MIX_CTRL & USDHC_MIX_CTRL_DMAEN_MASK) == 0U)
  465. {
  466. /*
  467. * Add non aligned access support ,user need make sure your buffer size is big
  468. * enough to hold the data,in other words,user need make sure the buffer size
  469. * is 4 byte aligned
  470. */
  471. if (data->blockSize % sizeof(uint32_t) != 0U)
  472. {
  473. data->blockSize +=
  474. sizeof(uint32_t) - (data->blockSize % sizeof(uint32_t)); /* make the block size as word-aligned */
  475. }
  476. totalWords = ((data->blockCount * data->blockSize) / sizeof(uint32_t));
  477. /* If watermark level is equal or bigger than totalWords, transfers totalWords data.*/
  478. if (writeWatermark >= totalWords)
  479. {
  480. wordsCanBeWrote = totalWords;
  481. }
  482. /* If watermark level is less than totalWords and left words to be sent is equal or bigger than watermark,
  483. transfers watermark level words. */
  484. else if ((writeWatermark < totalWords) && ((totalWords - transferredWords) >= writeWatermark))
  485. {
  486. wordsCanBeWrote = writeWatermark;
  487. }
  488. /* If watermark level is less than totalWords and left words to be sent is less than watermark, transfers left
  489. words. */
  490. else
  491. {
  492. wordsCanBeWrote = (totalWords - transferredWords);
  493. }
  494. i = 0U;
  495. while (i < wordsCanBeWrote)
  496. {
  497. USDHC_WriteData(base, data->txData[transferredWords++]);
  498. i++;
  499. }
  500. }
  501. return transferredWords;
  502. }
  503. static status_t USDHC_WriteByDataPortBlocking(USDHC_Type *base, usdhc_data_t *data)
  504. {
  505. uint32_t totalWords;
  506. uint32_t transferredWords = 0U, interruptStatus = 0U;
  507. status_t error = kStatus_Success;
  508. /*
  509. * Add non aligned access support ,user need make sure your buffer size is big
  510. * enough to hold the data,in other words,user need make sure the buffer size
  511. * is 4 byte aligned
  512. */
  513. if (data->blockSize % sizeof(uint32_t) != 0U)
  514. {
  515. data->blockSize +=
  516. sizeof(uint32_t) - (data->blockSize % sizeof(uint32_t)); /* make the block size as word-aligned */
  517. }
  518. totalWords = (data->blockCount * data->blockSize) / sizeof(uint32_t);
  519. while ((error == kStatus_Success) && (transferredWords < totalWords))
  520. {
  521. while (!(IS_USDHC_FLAG_SET(interruptStatus, (uint32_t)kUSDHC_BufferWriteReadyFlag |
  522. (uint32_t)kUSDHC_DataErrorFlag |
  523. (uint32_t)kUSDHC_TuningErrorFlag)))
  524. {
  525. interruptStatus = USDHC_GetInterruptStatusFlags(base);
  526. }
  527. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  528. if (IS_USDHC_FLAG_SET(interruptStatus, kUSDHC_TuningErrorFlag))
  529. {
  530. USDHC_ClearInterruptStatusFlags(base, kUSDHC_TuningErrorFlag);
  531. /* if tuning error occur ,return directly */
  532. return kStatus_USDHC_TuningError;
  533. }
  534. else
  535. #endif
  536. if (IS_USDHC_FLAG_SET(interruptStatus, kUSDHC_DataErrorFlag))
  537. {
  538. if (!(data->enableIgnoreError))
  539. {
  540. error = kStatus_Fail;
  541. }
  542. /* clear data error flag */
  543. USDHC_ClearInterruptStatusFlags(base, kUSDHC_DataErrorFlag);
  544. }
  545. else
  546. {
  547. /* Intentional empty */
  548. }
  549. if (error == kStatus_Success)
  550. {
  551. transferredWords = USDHC_WriteDataPort(base, data, transferredWords);
  552. /* clear buffer write ready */
  553. USDHC_ClearInterruptStatusFlags(base, kUSDHC_BufferWriteReadyFlag);
  554. interruptStatus = 0U;
  555. }
  556. }
  557. /* Wait write data complete or data transfer error after the last writing operation. */
  558. while (!(IS_USDHC_FLAG_SET(interruptStatus, (uint32_t)kUSDHC_DataCompleteFlag | (uint32_t)kUSDHC_DataErrorFlag)))
  559. {
  560. interruptStatus = USDHC_GetInterruptStatusFlags(base);
  561. }
  562. if ((interruptStatus & (uint32_t)kUSDHC_DataErrorFlag) != 0UL)
  563. {
  564. if (!(data->enableIgnoreError))
  565. {
  566. error = kStatus_Fail;
  567. }
  568. }
  569. USDHC_ClearInterruptStatusFlags(base, ((uint32_t)kUSDHC_DataCompleteFlag | (uint32_t)kUSDHC_DataErrorFlag));
  570. return error;
  571. }
  572. /*!
  573. * brief send command function
  574. *
  575. * param base USDHC peripheral base address.
  576. * param command configuration
  577. */
  578. void USDHC_SendCommand(USDHC_Type *base, usdhc_command_t *command)
  579. {
  580. assert(NULL != command);
  581. uint32_t xferType = base->CMD_XFR_TYP, flags = command->flags;
  582. if (((base->PRES_STATE & (uint32_t)kUSDHC_CommandInhibitFlag) == 0U) && (command->type != kCARD_CommandTypeEmpty))
  583. {
  584. if ((command->responseType == kCARD_ResponseTypeR1) || (command->responseType == kCARD_ResponseTypeR5) ||
  585. (command->responseType == kCARD_ResponseTypeR6) || (command->responseType == kCARD_ResponseTypeR7))
  586. {
  587. flags |= ((uint32_t)kUSDHC_ResponseLength48Flag | (uint32_t)kUSDHC_EnableCrcCheckFlag |
  588. (uint32_t)kUSDHC_EnableIndexCheckFlag);
  589. }
  590. else if ((command->responseType == kCARD_ResponseTypeR1b) || (command->responseType == kCARD_ResponseTypeR5b))
  591. {
  592. flags |= ((uint32_t)kUSDHC_ResponseLength48BusyFlag | (uint32_t)kUSDHC_EnableCrcCheckFlag |
  593. (uint32_t)kUSDHC_EnableIndexCheckFlag);
  594. }
  595. else if (command->responseType == kCARD_ResponseTypeR2)
  596. {
  597. flags |= ((uint32_t)kUSDHC_ResponseLength136Flag | (uint32_t)kUSDHC_EnableCrcCheckFlag);
  598. }
  599. else if ((command->responseType == kCARD_ResponseTypeR3) || (command->responseType == kCARD_ResponseTypeR4))
  600. {
  601. flags |= ((uint32_t)kUSDHC_ResponseLength48Flag);
  602. }
  603. else
  604. {
  605. /* Intentional empty */
  606. }
  607. if (command->type == kCARD_CommandTypeAbort)
  608. {
  609. flags |= (uint32_t)kUSDHC_CommandTypeAbortFlag;
  610. }
  611. /* config cmd index */
  612. xferType &= ~(USDHC_CMD_XFR_TYP_CMDINX_MASK | USDHC_CMD_XFR_TYP_CMDTYP_MASK | USDHC_CMD_XFR_TYP_CICEN_MASK |
  613. USDHC_CMD_XFR_TYP_CCCEN_MASK | USDHC_CMD_XFR_TYP_RSPTYP_MASK | USDHC_CMD_XFR_TYP_DPSEL_MASK);
  614. xferType |=
  615. (((command->index << USDHC_CMD_XFR_TYP_CMDINX_SHIFT) & USDHC_CMD_XFR_TYP_CMDINX_MASK) |
  616. ((flags) & (USDHC_CMD_XFR_TYP_CMDTYP_MASK | USDHC_CMD_XFR_TYP_CICEN_MASK | USDHC_CMD_XFR_TYP_CCCEN_MASK |
  617. USDHC_CMD_XFR_TYP_RSPTYP_MASK | USDHC_CMD_XFR_TYP_DPSEL_MASK)));
  618. /* config the command xfertype and argument */
  619. base->CMD_ARG = command->argument;
  620. base->CMD_XFR_TYP = xferType;
  621. }
  622. if (command->type == kCARD_CommandTypeEmpty)
  623. {
  624. /* disable CMD done interrupt for empty command */
  625. base->INT_SIGNAL_EN &= ~USDHC_INT_SIGNAL_EN_CCIEN_MASK;
  626. }
  627. }
  628. static status_t USDHC_WaitCommandDone(USDHC_Type *base, usdhc_command_t *command, bool pollingCmdDone)
  629. {
  630. assert(NULL != command);
  631. status_t error = kStatus_Success;
  632. uint32_t interruptStatus = 0U;
  633. /* check if need polling command done or not */
  634. if (pollingCmdDone)
  635. {
  636. /* Wait command complete or USDHC encounters error. */
  637. while (!(IS_USDHC_FLAG_SET(interruptStatus, kUSDHC_CommandFlag)))
  638. {
  639. interruptStatus = USDHC_GetInterruptStatusFlags(base);
  640. }
  641. if ((interruptStatus & (uint32_t)kUSDHC_CommandErrorFlag) != 0UL)
  642. {
  643. error = kStatus_Fail;
  644. }
  645. /* Receive response when command completes successfully. */
  646. if (error == kStatus_Success)
  647. {
  648. error = USDHC_ReceiveCommandResponse(base, command);
  649. }
  650. USDHC_ClearInterruptStatusFlags(base, kUSDHC_CommandFlag);
  651. }
  652. return error;
  653. }
  654. static status_t USDHC_TransferDataBlocking(USDHC_Type *base, usdhc_data_t *data, bool enDMA)
  655. {
  656. status_t error = kStatus_Success;
  657. uint32_t interruptStatus = 0U;
  658. if (enDMA)
  659. {
  660. /* Wait data complete or USDHC encounters error. */
  661. while (!(IS_USDHC_FLAG_SET(interruptStatus, ((uint32_t)kUSDHC_DataDMAFlag | (uint32_t)kUSDHC_TuningErrorFlag))))
  662. {
  663. interruptStatus = USDHC_GetInterruptStatusFlags(base);
  664. }
  665. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  666. if (IS_USDHC_FLAG_SET(interruptStatus, kUSDHC_TuningErrorFlag))
  667. {
  668. error = kStatus_USDHC_TuningError;
  669. }
  670. else
  671. #endif
  672. if (IS_USDHC_FLAG_SET(interruptStatus, ((uint32_t)kUSDHC_DataErrorFlag | (uint32_t)kUSDHC_DmaErrorFlag)))
  673. {
  674. if ((!(data->enableIgnoreError)) || (IS_USDHC_FLAG_SET(interruptStatus, kUSDHC_DataTimeoutFlag)))
  675. {
  676. error = kStatus_USDHC_TransferDataFailed;
  677. }
  678. }
  679. else
  680. {
  681. /* Intentional empty */
  682. }
  683. /* load dummy data */
  684. if ((data->dataType == (uint32_t)kUSDHC_TransferDataBootcontinous) && (error == kStatus_Success))
  685. {
  686. *(data->rxData) = s_usdhcBootDummy;
  687. }
  688. USDHC_ClearInterruptStatusFlags(base, ((uint32_t)kUSDHC_DataDMAFlag | (uint32_t)kUSDHC_TuningErrorFlag));
  689. }
  690. else
  691. {
  692. if (data->rxData != NULL)
  693. {
  694. error = USDHC_ReadByDataPortBlocking(base, data);
  695. if (error != kStatus_Success)
  696. {
  697. return error;
  698. }
  699. }
  700. else
  701. {
  702. error = USDHC_WriteByDataPortBlocking(base, data);
  703. if (error != kStatus_Success)
  704. {
  705. return error;
  706. }
  707. }
  708. }
  709. return error;
  710. }
  711. /*!
  712. * brief USDHC module initialization function.
  713. *
  714. * Configures the USDHC according to the user configuration.
  715. *
  716. * Example:
  717. code
  718. usdhc_config_t config;
  719. config.cardDetectDat3 = false;
  720. config.endianMode = kUSDHC_EndianModeLittle;
  721. config.dmaMode = kUSDHC_DmaModeAdma2;
  722. config.readWatermarkLevel = 128U;
  723. config.writeWatermarkLevel = 128U;
  724. USDHC_Init(USDHC, &config);
  725. endcode
  726. *
  727. * param base USDHC peripheral base address.
  728. * param config USDHC configuration information.
  729. * retval kStatus_Success Operate successfully.
  730. */
  731. void USDHC_Init(USDHC_Type *base, const usdhc_config_t *config)
  732. {
  733. assert(config != NULL);
  734. assert((config->writeWatermarkLevel >= 1U) && (config->writeWatermarkLevel <= 128U));
  735. assert((config->readWatermarkLevel >= 1U) && (config->readWatermarkLevel <= 128U));
  736. #if !(defined(FSL_FEATURE_USDHC_HAS_NO_RW_BURST_LEN) && FSL_FEATURE_USDHC_HAS_NO_RW_BURST_LEN)
  737. assert(config->writeBurstLen <= 16U);
  738. #endif
  739. uint32_t proctl, sysctl, wml;
  740. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  741. /* Enable USDHC clock. */
  742. CLOCK_EnableClock(s_usdhcClock[USDHC_GetInstance(base)]);
  743. #endif
  744. #if (defined(FSL_FEATURE_USDHC_HAS_RESET) && FSL_FEATURE_USDHC_HAS_RESET)
  745. /* Reset the USDHC module */
  746. RESET_PeripheralReset(s_usdhcResets[USDHC_GetInstance(base)]);
  747. #endif
  748. /* Reset ALL USDHC. */
  749. base->SYS_CTRL |= USDHC_SYS_CTRL_RSTA_MASK | USDHC_SYS_CTRL_RSTC_MASK | USDHC_SYS_CTRL_RSTD_MASK;
  750. proctl = base->PROT_CTRL;
  751. wml = base->WTMK_LVL;
  752. sysctl = base->SYS_CTRL;
  753. proctl &= ~(USDHC_PROT_CTRL_EMODE_MASK | USDHC_PROT_CTRL_DMASEL_MASK);
  754. /* Endian mode*/
  755. proctl |= USDHC_PROT_CTRL_EMODE(config->endianMode);
  756. #if (defined(FSL_FEATURE_USDHC_HAS_NO_RW_BURST_LEN) && FSL_FEATURE_USDHC_HAS_NO_RW_BURST_LEN)
  757. /* Watermark level */
  758. wml &= ~(USDHC_WTMK_LVL_RD_WML_MASK | USDHC_WTMK_LVL_WR_WML_MASK);
  759. wml |= (USDHC_WTMK_LVL_RD_WML(config->readWatermarkLevel) | USDHC_WTMK_LVL_WR_WML(config->writeWatermarkLevel));
  760. #else
  761. /* Watermark level */
  762. wml &= ~(USDHC_WTMK_LVL_RD_WML_MASK | USDHC_WTMK_LVL_WR_WML_MASK | USDHC_WTMK_LVL_RD_BRST_LEN_MASK |
  763. USDHC_WTMK_LVL_WR_BRST_LEN_MASK);
  764. wml |= (USDHC_WTMK_LVL_RD_WML(config->readWatermarkLevel) | USDHC_WTMK_LVL_WR_WML(config->writeWatermarkLevel) |
  765. USDHC_WTMK_LVL_RD_BRST_LEN(config->readBurstLen) | USDHC_WTMK_LVL_WR_BRST_LEN(config->writeBurstLen));
  766. #endif
  767. /* config the data timeout value */
  768. sysctl &= ~USDHC_SYS_CTRL_DTOCV_MASK;
  769. sysctl |= USDHC_SYS_CTRL_DTOCV(config->dataTimeout);
  770. base->SYS_CTRL = sysctl;
  771. base->WTMK_LVL = wml;
  772. base->PROT_CTRL = proctl;
  773. #if FSL_FEATURE_USDHC_HAS_EXT_DMA
  774. /* disable external DMA */
  775. base->VEND_SPEC &= ~USDHC_VEND_SPEC_EXT_DMA_EN_MASK;
  776. #endif
  777. /* disable internal DMA and DDR mode */
  778. base->MIX_CTRL &= ~(USDHC_MIX_CTRL_DMAEN_MASK | USDHC_MIX_CTRL_DDR_EN_MASK);
  779. /* disable interrupt, enable all the interrupt status, clear status. */
  780. base->INT_STATUS_EN = kUSDHC_AllInterruptFlags;
  781. base->INT_SIGNAL_EN = 0UL;
  782. base->INT_STATUS = kUSDHC_AllInterruptFlags;
  783. }
  784. /*!
  785. * brief Deinitializes the USDHC.
  786. *
  787. * param base USDHC peripheral base address.
  788. */
  789. void USDHC_Deinit(USDHC_Type *base)
  790. {
  791. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  792. /* Disable clock. */
  793. CLOCK_DisableClock(s_usdhcClock[USDHC_GetInstance(base)]);
  794. #endif
  795. }
  796. /*!
  797. * brief Resets the USDHC.
  798. *
  799. * param base USDHC peripheral base address.
  800. * param mask The reset type mask(_usdhc_reset).
  801. * param timeout Timeout for reset.
  802. * retval true Reset successfully.
  803. * retval false Reset failed.
  804. */
  805. bool USDHC_Reset(USDHC_Type *base, uint32_t mask, uint32_t timeout)
  806. {
  807. base->SYS_CTRL |= (mask & (USDHC_SYS_CTRL_RSTA_MASK | USDHC_SYS_CTRL_RSTC_MASK | USDHC_SYS_CTRL_RSTD_MASK
  808. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  809. | USDHC_SYS_CTRL_RSTT_MASK
  810. #endif
  811. ));
  812. /* Delay some time to wait reset success. */
  813. while (IS_USDHC_FLAG_SET(base->SYS_CTRL, mask))
  814. {
  815. if (timeout == 0UL)
  816. {
  817. break;
  818. }
  819. timeout--;
  820. }
  821. return ((0UL == timeout) ? false : true);
  822. }
  823. /*!
  824. * brief Gets the capability information.
  825. *
  826. * param base USDHC peripheral base address.
  827. * param capability Structure to save capability information.
  828. */
  829. void USDHC_GetCapability(USDHC_Type *base, usdhc_capability_t *capability)
  830. {
  831. assert(capability != NULL);
  832. uint32_t htCapability;
  833. uint32_t maxBlockLength;
  834. htCapability = base->HOST_CTRL_CAP;
  835. /* Get the capability of USDHC. */
  836. maxBlockLength = ((htCapability & USDHC_HOST_CTRL_CAP_MBL_MASK) >> USDHC_HOST_CTRL_CAP_MBL_SHIFT);
  837. capability->maxBlockLength = (512UL << maxBlockLength);
  838. /* Other attributes not in HTCAPBLT register. */
  839. capability->maxBlockCount = USDHC_MAX_BLOCK_COUNT;
  840. capability->flags =
  841. (htCapability & (USDHC_HOST_CTRL_CAP_ADMAS_MASK | USDHC_HOST_CTRL_CAP_HSS_MASK | USDHC_HOST_CTRL_CAP_DMAS_MASK |
  842. USDHC_HOST_CTRL_CAP_SRS_MASK | USDHC_HOST_CTRL_CAP_VS33_MASK));
  843. capability->flags |= htCapability & USDHC_HOST_CTRL_CAP_VS30_MASK;
  844. capability->flags |= htCapability & USDHC_HOST_CTRL_CAP_VS18_MASK;
  845. capability->flags |= htCapability & USDHC_HOST_CTRL_CAP_DDR50_SUPPORT_MASK;
  846. #if defined(FSL_FEATURE_USDHC_HAS_SDR104_MODE) && FSL_FEATURE_USDHC_HAS_SDR104_MODE
  847. capability->flags |= USDHC_HOST_CTRL_CAP_SDR104_SUPPORT_MASK;
  848. #endif
  849. #if defined(FSL_FEATURE_USDHC_HAS_SDR104_MODE) && FSL_FEATURE_USDHC_HAS_SDR50_MODE
  850. capability->flags |= USDHC_HOST_CTRL_CAP_SDR50_SUPPORT_MASK;
  851. #endif
  852. /* USDHC support 4/8 bit data bus width. */
  853. capability->flags |= (USDHC_HOST_CTRL_CAP_MBL_SHIFT << 0UL) | (USDHC_HOST_CTRL_CAP_MBL_SHIFT << 1UL);
  854. }
  855. /*!
  856. * brief Sets the SD bus clock frequency.
  857. *
  858. * param base USDHC peripheral base address.
  859. * param srcClock_Hz USDHC source clock frequency united in Hz.
  860. * param busClock_Hz SD bus clock frequency united in Hz.
  861. *
  862. * return The nearest frequency of busClock_Hz configured to SD bus.
  863. */
  864. uint32_t USDHC_SetSdClock(USDHC_Type *base, uint32_t srcClock_Hz, uint32_t busClock_Hz)
  865. {
  866. assert(srcClock_Hz != 0U);
  867. assert(busClock_Hz != 0U);
  868. uint32_t totalDiv = 0UL;
  869. uint32_t divisor = 0UL;
  870. uint32_t prescaler = 0UL;
  871. uint32_t sysctl = 0UL;
  872. uint32_t nearestFrequency = 0UL;
  873. if (busClock_Hz > srcClock_Hz)
  874. {
  875. busClock_Hz = srcClock_Hz;
  876. }
  877. totalDiv = srcClock_Hz / busClock_Hz;
  878. /* calucate total divisor first */
  879. if (totalDiv > (USDHC_MAX_CLKFS * USDHC_MAX_DVS))
  880. {
  881. return 0UL;
  882. }
  883. if (totalDiv != 0UL)
  884. {
  885. /* calucate the divisor (srcClock_Hz / divisor) <= busClock_Hz */
  886. if ((srcClock_Hz / totalDiv) > busClock_Hz)
  887. {
  888. totalDiv++;
  889. }
  890. /* divide the total divisor to div and prescaler */
  891. if (totalDiv > USDHC_MAX_DVS)
  892. {
  893. prescaler = totalDiv / USDHC_MAX_DVS;
  894. /* prescaler must be a value which equal 2^n and smaller than SDHC_MAX_CLKFS */
  895. while (((USDHC_MAX_CLKFS % prescaler) != 0UL) || (prescaler == 1UL))
  896. {
  897. prescaler++;
  898. }
  899. /* calucate the divisor */
  900. divisor = totalDiv / prescaler;
  901. /* fine tuning the divisor until divisor * prescaler >= totalDiv */
  902. while ((divisor * prescaler) < totalDiv)
  903. {
  904. divisor++;
  905. if (divisor > USDHC_MAX_DVS)
  906. {
  907. prescaler <<= 1UL;
  908. if (prescaler > USDHC_MAX_CLKFS)
  909. {
  910. return 0UL;
  911. }
  912. divisor = totalDiv / prescaler;
  913. }
  914. }
  915. }
  916. else
  917. {
  918. /* in this situation , divsior and SDCLKFS can generate same clock
  919. use SDCLKFS*/
  920. if (((totalDiv % 2UL) != 0UL) && (totalDiv != 1UL))
  921. {
  922. divisor = totalDiv;
  923. prescaler = 1UL;
  924. }
  925. else
  926. {
  927. divisor = 1UL;
  928. prescaler = totalDiv;
  929. }
  930. }
  931. nearestFrequency = srcClock_Hz / (divisor == 0UL ? 1UL : divisor) / prescaler;
  932. }
  933. /* in this condition , srcClock_Hz = busClock_Hz, */
  934. else
  935. {
  936. /* in DDR mode , set SDCLKFS to 0, divisor = 0, actually the
  937. totoal divider = 2U */
  938. divisor = 0UL;
  939. prescaler = 0UL;
  940. nearestFrequency = srcClock_Hz;
  941. }
  942. /* calucate the value write to register */
  943. if (divisor != 0UL)
  944. {
  945. USDHC_PREV_DVS(divisor);
  946. }
  947. /* calucate the value write to register */
  948. if (prescaler != 0UL)
  949. {
  950. USDHC_PREV_CLKFS(prescaler, 1UL);
  951. }
  952. /* Set the SD clock frequency divisor, SD clock frequency select, data timeout counter value. */
  953. sysctl = base->SYS_CTRL;
  954. sysctl &= ~(USDHC_SYS_CTRL_DVS_MASK | USDHC_SYS_CTRL_SDCLKFS_MASK);
  955. sysctl |= (USDHC_SYS_CTRL_DVS(divisor) | USDHC_SYS_CTRL_SDCLKFS(prescaler));
  956. base->SYS_CTRL = sysctl;
  957. /* Wait until the SD clock is stable. */
  958. while (!IS_USDHC_FLAG_SET(base->PRES_STATE, USDHC_PRES_STATE_SDSTB_MASK))
  959. {
  960. }
  961. return nearestFrequency;
  962. }
  963. /*!
  964. * brief Sends 80 clocks to the card to set it to the active state.
  965. *
  966. * This function must be called each time the card is inserted to ensure that the card can receive the command
  967. * correctly.
  968. *
  969. * param base USDHC peripheral base address.
  970. * param timeout Timeout to initialize card.
  971. * retval true Set card active successfully.
  972. * retval false Set card active failed.
  973. */
  974. bool USDHC_SetCardActive(USDHC_Type *base, uint32_t timeout)
  975. {
  976. base->SYS_CTRL |= USDHC_SYS_CTRL_INITA_MASK;
  977. /* Delay some time to wait card become active state. */
  978. while (IS_USDHC_FLAG_SET(base->SYS_CTRL, USDHC_SYS_CTRL_INITA_MASK))
  979. {
  980. if (0UL == timeout)
  981. {
  982. break;
  983. }
  984. timeout--;
  985. }
  986. return ((0UL == timeout) ? false : true);
  987. }
  988. /*!
  989. * brief the enable/disable DDR mode
  990. *
  991. * param base USDHC peripheral base address.
  992. * param enable/disable flag
  993. * param nibble position
  994. */
  995. void USDHC_EnableDDRMode(USDHC_Type *base, bool enable, uint32_t nibblePos)
  996. {
  997. uint32_t prescaler = (base->SYS_CTRL & USDHC_SYS_CTRL_SDCLKFS_MASK) >> USDHC_SYS_CTRL_SDCLKFS_SHIFT;
  998. if (enable)
  999. {
  1000. base->MIX_CTRL &= ~USDHC_MIX_CTRL_NIBBLE_POS_MASK;
  1001. base->MIX_CTRL |= (USDHC_MIX_CTRL_DDR_EN_MASK | USDHC_MIX_CTRL_NIBBLE_POS(nibblePos));
  1002. prescaler >>= 1UL;
  1003. }
  1004. else
  1005. {
  1006. base->MIX_CTRL &= ~USDHC_MIX_CTRL_DDR_EN_MASK;
  1007. if (prescaler == 0UL)
  1008. {
  1009. prescaler += 1UL;
  1010. }
  1011. else
  1012. {
  1013. prescaler <<= 1UL;
  1014. }
  1015. }
  1016. base->SYS_CTRL = (base->SYS_CTRL & (~USDHC_SYS_CTRL_SDCLKFS_MASK)) | USDHC_SYS_CTRL_SDCLKFS(prescaler);
  1017. }
  1018. /*!
  1019. * brief Configures the MMC boot feature.
  1020. *
  1021. * Example:
  1022. code
  1023. usdhc_boot_config_t config;
  1024. config.ackTimeoutCount = 4;
  1025. config.bootMode = kUSDHC_BootModeNormal;
  1026. config.blockCount = 5;
  1027. config.enableBootAck = true;
  1028. config.enableBoot = true;
  1029. config.enableAutoStopAtBlockGap = true;
  1030. USDHC_SetMmcBootConfig(USDHC, &config);
  1031. endcode
  1032. *
  1033. * param base USDHC peripheral base address.
  1034. * param config The MMC boot configuration information.
  1035. */
  1036. void USDHC_SetMmcBootConfig(USDHC_Type *base, const usdhc_boot_config_t *config)
  1037. {
  1038. assert(config != NULL);
  1039. assert(config->ackTimeoutCount <= (USDHC_MMC_BOOT_DTOCV_ACK_MASK >> USDHC_MMC_BOOT_DTOCV_ACK_SHIFT));
  1040. assert(config->blockCount <= (USDHC_MMC_BOOT_BOOT_BLK_CNT_MASK >> USDHC_MMC_BOOT_BOOT_BLK_CNT_SHIFT));
  1041. uint32_t mmcboot = base->MMC_BOOT;
  1042. mmcboot &= ~(USDHC_MMC_BOOT_DTOCV_ACK_MASK | USDHC_MMC_BOOT_BOOT_MODE_MASK | USDHC_MMC_BOOT_BOOT_BLK_CNT_MASK);
  1043. mmcboot |= USDHC_MMC_BOOT_DTOCV_ACK(config->ackTimeoutCount) | USDHC_MMC_BOOT_BOOT_MODE(config->bootMode);
  1044. if (config->enableBootAck)
  1045. {
  1046. mmcboot |= USDHC_MMC_BOOT_BOOT_ACK_MASK;
  1047. }
  1048. if (config->enableAutoStopAtBlockGap)
  1049. {
  1050. mmcboot |=
  1051. USDHC_MMC_BOOT_AUTO_SABG_EN_MASK | USDHC_MMC_BOOT_BOOT_BLK_CNT(USDHC_MAX_BLOCK_COUNT - config->blockCount);
  1052. /* always set the block count to USDHC_MAX_BLOCK_COUNT to use auto stop at block gap feature */
  1053. base->BLK_ATT = ((base->BLK_ATT & ~(USDHC_BLK_ATT_BLKSIZE_MASK | USDHC_BLK_ATT_BLKCNT_MASK)) |
  1054. (USDHC_BLK_ATT_BLKSIZE(config->blockSize) | USDHC_BLK_ATT_BLKCNT(USDHC_MAX_BLOCK_COUNT)));
  1055. }
  1056. else
  1057. {
  1058. base->BLK_ATT = ((base->BLK_ATT & ~(USDHC_BLK_ATT_BLKSIZE_MASK | USDHC_BLK_ATT_BLKCNT_MASK)) |
  1059. (USDHC_BLK_ATT_BLKSIZE(config->blockSize) | USDHC_BLK_ATT_BLKCNT(config->blockCount)));
  1060. }
  1061. base->MMC_BOOT = mmcboot;
  1062. }
  1063. /*!
  1064. * brief Sets the ADMA1 descriptor table configuration.
  1065. *
  1066. * param admaTable Adma table address.
  1067. * param admaTableWords Adma table length.
  1068. * param dataBufferAddr Data buffer address.
  1069. * param dataBytes Data length.
  1070. * param flags ADAM descriptor flag, used to indicate to create multiple or single descriptor, please
  1071. * reference _usdhc_adma_flag.
  1072. * retval kStatus_OutOfRange ADMA descriptor table length isn't enough to describe data.
  1073. * retval kStatus_Success Operate successfully.
  1074. */
  1075. status_t USDHC_SetADMA1Descriptor(
  1076. uint32_t *admaTable, uint32_t admaTableWords, const uint32_t *dataBufferAddr, uint32_t dataBytes, uint32_t flags)
  1077. {
  1078. assert(NULL != admaTable);
  1079. assert(NULL != dataBufferAddr);
  1080. uint32_t miniEntries, startEntries = 0UL,
  1081. maxEntries = (admaTableWords * sizeof(uint32_t)) / sizeof(usdhc_adma1_descriptor_t);
  1082. usdhc_adma1_descriptor_t *adma1EntryAddress = (usdhc_adma1_descriptor_t *)(uint32_t)(admaTable);
  1083. uint32_t i, dmaBufferLen = 0UL;
  1084. const uint32_t *data = dataBufferAddr;
  1085. if (((uint32_t)data % USDHC_ADMA1_ADDRESS_ALIGN) != 0UL)
  1086. {
  1087. return kStatus_USDHC_DMADataAddrNotAlign;
  1088. }
  1089. if (flags == (uint32_t)kUSDHC_AdmaDescriptorMultipleFlag)
  1090. {
  1091. return kStatus_USDHC_NotSupport;
  1092. }
  1093. /*
  1094. * Add non aligned access support ,user need make sure your buffer size is big
  1095. * enough to hold the data,in other words,user need make sure the buffer size
  1096. * is 4 byte aligned
  1097. */
  1098. if (dataBytes % sizeof(uint32_t) != 0UL)
  1099. {
  1100. /* make the data length as word-aligned */
  1101. dataBytes += sizeof(uint32_t) - (dataBytes % sizeof(uint32_t));
  1102. }
  1103. /* Check if ADMA descriptor's number is enough. */
  1104. if ((dataBytes % USDHC_ADMA1_DESCRIPTOR_MAX_LENGTH_PER_ENTRY) == 0UL)
  1105. {
  1106. miniEntries = dataBytes / USDHC_ADMA1_DESCRIPTOR_MAX_LENGTH_PER_ENTRY;
  1107. }
  1108. else
  1109. {
  1110. miniEntries = ((dataBytes / USDHC_ADMA1_DESCRIPTOR_MAX_LENGTH_PER_ENTRY) + 1UL);
  1111. }
  1112. /* ADMA1 needs two descriptors to finish a transfer */
  1113. miniEntries <<= 1UL;
  1114. if (miniEntries + startEntries > maxEntries)
  1115. {
  1116. return kStatus_OutOfRange;
  1117. }
  1118. for (i = startEntries; i < (miniEntries + startEntries); i += 2UL)
  1119. {
  1120. if (dataBytes > USDHC_ADMA1_DESCRIPTOR_MAX_LENGTH_PER_ENTRY)
  1121. {
  1122. dmaBufferLen = USDHC_ADMA1_DESCRIPTOR_MAX_LENGTH_PER_ENTRY;
  1123. }
  1124. else
  1125. {
  1126. dmaBufferLen = dataBytes;
  1127. }
  1128. adma1EntryAddress[i] = (dmaBufferLen << USDHC_ADMA1_DESCRIPTOR_LENGTH_SHIFT);
  1129. adma1EntryAddress[i] |= (uint32_t)kUSDHC_Adma1DescriptorTypeSetLength;
  1130. adma1EntryAddress[i + 1UL] = (uint32_t)(data);
  1131. adma1EntryAddress[i + 1UL] |=
  1132. (uint32_t)kUSDHC_Adma1DescriptorTypeTransfer | (uint32_t)kUSDHC_Adma1DescriptorInterrupFlag;
  1133. data = (uint32_t *)((uint32_t)data + dmaBufferLen);
  1134. dataBytes -= dmaBufferLen;
  1135. }
  1136. /* the end of the descriptor */
  1137. adma1EntryAddress[i - 1UL] |= (uint32_t)kUSDHC_Adma1DescriptorEndFlag;
  1138. return kStatus_Success;
  1139. }
  1140. /*!
  1141. * brief Sets the ADMA2 descriptor table configuration.
  1142. *
  1143. * param admaTable Adma table address.
  1144. * param admaTableWords Adma table length.
  1145. * param dataBufferAddr Data buffer address.
  1146. * param dataBytes Data Data length.
  1147. * param flags ADAM descriptor flag, used to indicate to create multiple or single descriptor, please
  1148. * reference _usdhc_adma_flag.
  1149. * retval kStatus_OutOfRange ADMA descriptor table length isn't enough to describe data.
  1150. * retval kStatus_Success Operate successfully.
  1151. */
  1152. status_t USDHC_SetADMA2Descriptor(
  1153. uint32_t *admaTable, uint32_t admaTableWords, const uint32_t *dataBufferAddr, uint32_t dataBytes, uint32_t flags)
  1154. {
  1155. assert(NULL != admaTable);
  1156. assert(NULL != dataBufferAddr);
  1157. uint32_t miniEntries, startEntries = 0UL,
  1158. maxEntries = (admaTableWords * sizeof(uint32_t)) / sizeof(usdhc_adma2_descriptor_t);
  1159. usdhc_adma2_descriptor_t *adma2EntryAddress = (usdhc_adma2_descriptor_t *)(uint32_t)(admaTable);
  1160. uint32_t i, dmaBufferLen = 0UL;
  1161. const uint32_t *data = dataBufferAddr;
  1162. if (((uint32_t)data % USDHC_ADMA2_ADDRESS_ALIGN) != 0UL)
  1163. {
  1164. return kStatus_USDHC_DMADataAddrNotAlign;
  1165. }
  1166. /*
  1167. * Add non aligned access support ,user need make sure your buffer size is big
  1168. * enough to hold the data,in other words,user need make sure the buffer size
  1169. * is 4 byte aligned
  1170. */
  1171. if (dataBytes % sizeof(uint32_t) != 0UL)
  1172. {
  1173. /* make the data length as word-aligned */
  1174. dataBytes += sizeof(uint32_t) - (dataBytes % sizeof(uint32_t));
  1175. }
  1176. /* Check if ADMA descriptor's number is enough. */
  1177. if ((dataBytes % USDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY) == 0UL)
  1178. {
  1179. miniEntries = dataBytes / USDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY;
  1180. }
  1181. else
  1182. {
  1183. miniEntries = ((dataBytes / USDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY) + 1UL);
  1184. }
  1185. /* calucate the start entry for multiple descriptor mode, ADMA engine is not stop, so update the descriptor
  1186. data adress and data size is enough */
  1187. if (flags == (uint32_t)kUSDHC_AdmaDescriptorMultipleFlag)
  1188. {
  1189. for (i = 0UL; i < maxEntries; i++)
  1190. {
  1191. if ((adma2EntryAddress[i].attribute & (uint32_t)kUSDHC_Adma2DescriptorValidFlag) == 0UL)
  1192. {
  1193. break;
  1194. }
  1195. }
  1196. startEntries = i;
  1197. /* add one entry for dummy entry */
  1198. miniEntries += 1UL;
  1199. }
  1200. if ((miniEntries + startEntries) > maxEntries)
  1201. {
  1202. return kStatus_OutOfRange;
  1203. }
  1204. for (i = startEntries; i < (miniEntries + startEntries); i++)
  1205. {
  1206. if (dataBytes > USDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY)
  1207. {
  1208. dmaBufferLen = USDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY;
  1209. }
  1210. else
  1211. {
  1212. dmaBufferLen = (dataBytes == 0UL ? sizeof(uint32_t) :
  1213. dataBytes); /* adma don't support 0 data length transfer descriptor */
  1214. }
  1215. /* Each descriptor for ADMA2 is 64-bit in length */
  1216. adma2EntryAddress[i].address = (dataBytes == 0UL) ? &s_usdhcBootDummy : data;
  1217. adma2EntryAddress[i].attribute = (dmaBufferLen << USDHC_ADMA2_DESCRIPTOR_LENGTH_SHIFT);
  1218. adma2EntryAddress[i].attribute |=
  1219. (dataBytes == 0UL) ?
  1220. 0UL :
  1221. ((uint32_t)kUSDHC_Adma2DescriptorTypeTransfer | (uint32_t)kUSDHC_Adma2DescriptorInterruptFlag);
  1222. data = (uint32_t *)((uint32_t)data + dmaBufferLen);
  1223. if (dataBytes != 0UL)
  1224. {
  1225. dataBytes -= dmaBufferLen;
  1226. }
  1227. }
  1228. /* add a dummy valid ADMA descriptor for multiple descriptor mode, this is useful when transfer boot data, the ADMA
  1229. engine
  1230. will not stop at block gap */
  1231. if (flags == (uint32_t)kUSDHC_AdmaDescriptorMultipleFlag)
  1232. {
  1233. adma2EntryAddress[startEntries + 1UL].attribute |= (uint32_t)kUSDHC_Adma2DescriptorTypeTransfer;
  1234. }
  1235. else
  1236. {
  1237. /* set the end bit */
  1238. adma2EntryAddress[i - 1UL].attribute |= (uint32_t)kUSDHC_Adma2DescriptorEndFlag;
  1239. }
  1240. return kStatus_Success;
  1241. }
  1242. /*!
  1243. * brief Internal DMA configuration.
  1244. * This function is used to config the USDHC DMA related registers.
  1245. * param base USDHC peripheral base address.
  1246. * param adma configuration
  1247. * param dataAddr transfer data address, a simple DMA parameter, if ADMA is used, leave it to NULL.
  1248. * param enAutoCmd23 flag to indicate Auto CMD23 is enable or not, a simple DMA parameter,if ADMA is used, leave it to
  1249. * false.
  1250. * retval kStatus_OutOfRange ADMA descriptor table length isn't enough to describe data.
  1251. * retval kStatus_Success Operate successfully.
  1252. */
  1253. status_t USDHC_SetInternalDmaConfig(USDHC_Type *base,
  1254. usdhc_adma_config_t *dmaConfig,
  1255. const uint32_t *dataAddr,
  1256. bool enAutoCmd23)
  1257. {
  1258. assert(dmaConfig != NULL);
  1259. assert(dataAddr != NULL);
  1260. assert((NULL != dmaConfig->admaTable) &&
  1261. (((USDHC_ADMA_TABLE_ADDRESS_ALIGN - 1U) & (uint32_t)dmaConfig->admaTable) == 0UL));
  1262. #if FSL_FEATURE_USDHC_HAS_EXT_DMA
  1263. /* disable the external DMA if support */
  1264. base->VEND_SPEC &= ~USDHC_VEND_SPEC_EXT_DMA_EN_MASK;
  1265. #endif
  1266. if (dmaConfig->dmaMode == kUSDHC_DmaModeSimple)
  1267. {
  1268. /* check DMA data buffer address align or not */
  1269. if (((uint32_t)dataAddr % USDHC_ADMA2_ADDRESS_ALIGN) != 0UL)
  1270. {
  1271. return kStatus_USDHC_DMADataAddrNotAlign;
  1272. }
  1273. /* in simple DMA mode if use auto CMD23, address should load to ADMA addr,
  1274. and block count should load to DS_ADDR*/
  1275. if (enAutoCmd23)
  1276. {
  1277. base->ADMA_SYS_ADDR = USDHC_ADDR_CPU_2_DMA((uint32_t)dataAddr);
  1278. }
  1279. else
  1280. {
  1281. base->DS_ADDR = USDHC_ADDR_CPU_2_DMA((uint32_t)dataAddr);
  1282. }
  1283. }
  1284. else
  1285. {
  1286. /* When use ADMA, disable simple DMA */
  1287. base->DS_ADDR = 0UL;
  1288. base->ADMA_SYS_ADDR = USDHC_ADDR_CPU_2_DMA((uint32_t)(dmaConfig->admaTable));
  1289. }
  1290. #if (defined(FSL_FEATURE_USDHC_HAS_NO_RW_BURST_LEN) && FSL_FEATURE_USDHC_HAS_NO_RW_BURST_LEN)
  1291. /* select DMA mode and config the burst length */
  1292. base->PROT_CTRL &= ~(USDHC_PROT_CTRL_DMASEL_MASK);
  1293. base->PROT_CTRL |= USDHC_PROT_CTRL_DMASEL(dmaConfig->dmaMode);
  1294. #else
  1295. /* select DMA mode and config the burst length */
  1296. base->PROT_CTRL &= ~(USDHC_PROT_CTRL_DMASEL_MASK | USDHC_PROT_CTRL_BURST_LEN_EN_MASK);
  1297. base->PROT_CTRL |= USDHC_PROT_CTRL_DMASEL(dmaConfig->dmaMode) | USDHC_PROT_CTRL_BURST_LEN_EN(dmaConfig->burstLen);
  1298. #endif
  1299. /* enable DMA */
  1300. base->MIX_CTRL |= USDHC_MIX_CTRL_DMAEN_MASK;
  1301. return kStatus_Success;
  1302. }
  1303. /*!
  1304. * brief Sets the DMA descriptor table configuration.
  1305. * A high level DMA descriptor configuration function.
  1306. * param base USDHC peripheral base address.
  1307. * param adma configuration
  1308. * param data Data descriptor
  1309. * param flags ADAM descriptor flag, used to indicate to create multiple or single descriptor, please
  1310. * reference _usdhc_adma_flag
  1311. * retval kStatus_OutOfRange ADMA descriptor table length isn't enough to describe data.
  1312. * retval kStatus_Success Operate successfully.
  1313. */
  1314. status_t USDHC_SetAdmaTableConfig(USDHC_Type *base,
  1315. usdhc_adma_config_t *dmaConfig,
  1316. usdhc_data_t *dataConfig,
  1317. uint32_t flags)
  1318. {
  1319. assert(NULL != dmaConfig);
  1320. assert((NULL != dmaConfig->admaTable) &&
  1321. (((USDHC_ADMA_TABLE_ADDRESS_ALIGN - 1U) & (uint32_t)dmaConfig->admaTable) == 0UL));
  1322. assert(NULL != dataConfig);
  1323. status_t error = kStatus_Fail;
  1324. uint32_t bootDummyOffset =
  1325. dataConfig->dataType == (uint32_t)kUSDHC_TransferDataBootcontinous ? sizeof(uint32_t) : 0UL;
  1326. const uint32_t *data = (const uint32_t *)USDHC_ADDR_CPU_2_DMA((uint32_t)(
  1327. (uint32_t)((dataConfig->rxData == NULL) ? dataConfig->txData : dataConfig->rxData) + bootDummyOffset));
  1328. uint32_t blockSize = dataConfig->blockSize * dataConfig->blockCount - bootDummyOffset;
  1329. #if FSL_FEATURE_USDHC_HAS_EXT_DMA
  1330. if (dmaConfig->dmaMode == kUSDHC_ExternalDMA)
  1331. {
  1332. /* enable the external DMA */
  1333. base->VEND_SPEC |= USDHC_VEND_SPEC_EXT_DMA_EN_MASK;
  1334. }
  1335. else
  1336. #endif
  1337. if (dmaConfig->dmaMode == kUSDHC_DmaModeSimple)
  1338. {
  1339. error = kStatus_Success;
  1340. }
  1341. else if (dmaConfig->dmaMode == kUSDHC_DmaModeAdma1)
  1342. {
  1343. error = USDHC_SetADMA1Descriptor(dmaConfig->admaTable, dmaConfig->admaTableWords, data, blockSize, flags);
  1344. }
  1345. /* ADMA2 */
  1346. else
  1347. {
  1348. error = USDHC_SetADMA2Descriptor(dmaConfig->admaTable, dmaConfig->admaTableWords, data, blockSize, flags);
  1349. }
  1350. /* for internal dma, internal DMA configurations should not update the configurations when continous transfer the
  1351. * boot data, only the DMA descriptor need update */
  1352. if ((dmaConfig->dmaMode != kUSDHC_ExternalDMA) && (error == kStatus_Success) &&
  1353. (dataConfig->dataType != (uint32_t)kUSDHC_TransferDataBootcontinous))
  1354. {
  1355. error = USDHC_SetInternalDmaConfig(base, dmaConfig, data, dataConfig->enableAutoCommand23);
  1356. }
  1357. return error;
  1358. }
  1359. /*!
  1360. * brief Transfers the command/data using a blocking method.
  1361. *
  1362. * This function waits until the command response/data is received or the USDHC encounters an error by polling the
  1363. * status
  1364. * flag.
  1365. * The application must not call this API in multiple threads at the same time. Because of that this API doesn't support
  1366. * the re-entry mechanism.
  1367. *
  1368. * note There is no need to call the API 'USDHC_TransferCreateHandle' when calling this API.
  1369. *
  1370. * param base USDHC peripheral base address.
  1371. * param adma configuration
  1372. * param transfer Transfer content.
  1373. * retval kStatus_InvalidArgument Argument is invalid.
  1374. * retval kStatus_USDHC_PrepareAdmaDescriptorFailed Prepare ADMA descriptor failed.
  1375. * retval kStatus_USDHC_SendCommandFailed Send command failed.
  1376. * retval kStatus_USDHC_TransferDataFailed Transfer data failed.
  1377. * retval kStatus_Success Operate successfully.
  1378. */
  1379. status_t USDHC_TransferBlocking(USDHC_Type *base, usdhc_adma_config_t *dmaConfig, usdhc_transfer_t *transfer)
  1380. {
  1381. assert(transfer != NULL);
  1382. status_t error = kStatus_Fail;
  1383. usdhc_command_t *command = transfer->command;
  1384. usdhc_data_t *data = transfer->data;
  1385. bool enDMA = true;
  1386. bool executeTuning = ((data == NULL) ? false : data->dataType == (uint32_t)kUSDHC_TransferDataTuning);
  1387. uint32_t transferFlags = (uint32_t)kUSDHC_CommandOnly;
  1388. size_t blockSize = 0U;
  1389. size_t blockCount = 0U;
  1390. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  1391. /*check re-tuning request*/
  1392. if ((USDHC_GetInterruptStatusFlags(base) & (uint32_t)kUSDHC_ReTuningEventFlag) != 0UL)
  1393. {
  1394. USDHC_ClearInterruptStatusFlags(base, kUSDHC_ReTuningEventFlag);
  1395. return kStatus_USDHC_ReTuningRequest;
  1396. }
  1397. #endif
  1398. if (data != NULL)
  1399. {
  1400. /* Update ADMA descriptor table according to different DMA mode(no DMA, ADMA1, ADMA2).*/
  1401. if ((dmaConfig != NULL) && (!executeTuning))
  1402. {
  1403. error = USDHC_SetAdmaTableConfig(base, dmaConfig, data,
  1404. (uint32_t)(IS_USDHC_FLAG_SET(data->dataType, kUSDHC_TransferDataBoot) ?
  1405. kUSDHC_AdmaDescriptorMultipleFlag :
  1406. kUSDHC_AdmaDescriptorSingleFlag));
  1407. }
  1408. blockSize = data->blockSize;
  1409. blockCount = data->blockCount;
  1410. transferFlags = data->enableAutoCommand12 ? (uint32_t)kUSDHC_DataWithAutoCmd12 : 0U;
  1411. transferFlags |= data->enableAutoCommand23 ? (uint32_t)kUSDHC_DataWithAutoCmd23 : 0U;
  1412. transferFlags |= data->txData != NULL ? (uint32_t)kUSDHC_CommandAndTxData : (uint32_t)kUSDHC_CommandAndRxData;
  1413. transferFlags |= data->dataType == (uint8_t)kUSDHC_TransferDataBoot ? (uint32_t)kUSDHC_BootData : 0U;
  1414. transferFlags |=
  1415. data->dataType == (uint8_t)kUSDHC_TransferDataBootcontinous ? (uint32_t)kUSDHC_BootDataContinuous : 0U;
  1416. command->flags |= (uint32_t)kUSDHC_DataPresentFlag;
  1417. }
  1418. /* if the DMA desciptor configure fail or not needed , disable it */
  1419. if (error != kStatus_Success)
  1420. {
  1421. enDMA = false;
  1422. /* disable DMA, using polling mode in this situation */
  1423. USDHC_EnableInternalDMA(base, false);
  1424. }
  1425. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1426. else
  1427. {
  1428. if (data->txData != NULL)
  1429. {
  1430. /* clear the DCACHE */
  1431. DCACHE_CleanByRange((uint32_t)data->txData, (data->blockSize) * (data->blockCount));
  1432. }
  1433. else
  1434. {
  1435. /* clear the DCACHE */
  1436. DCACHE_CleanInvalidateByRange((uint32_t)data->rxData, (data->blockSize) * (data->blockCount));
  1437. }
  1438. }
  1439. #endif
  1440. /* config the data transfer parameter */
  1441. error = USDHC_SetTransferConfig(base, transferFlags, blockSize, blockCount);
  1442. if (error != kStatus_Success)
  1443. {
  1444. return error;
  1445. }
  1446. /* send command first */
  1447. USDHC_SendCommand(base, command);
  1448. /* wait command done */
  1449. error =
  1450. USDHC_WaitCommandDone(base, command, (data == NULL) || (data->dataType == (uint32_t)kUSDHC_TransferDataNormal));
  1451. if (kStatus_Success != error)
  1452. {
  1453. return kStatus_USDHC_SendCommandFailed;
  1454. }
  1455. /* wait transfer data finsih */
  1456. if (data != NULL)
  1457. {
  1458. error = USDHC_TransferDataBlocking(base, data, enDMA);
  1459. if (kStatus_Success != error)
  1460. {
  1461. return error;
  1462. }
  1463. }
  1464. return kStatus_Success;
  1465. }
  1466. #if (defined FSL_USDHC_ENABLE_SCATTER_GATHER_TRANSFER) && FSL_USDHC_ENABLE_SCATTER_GATHER_TRANSFER
  1467. static status_t USDHC_SetScatterGatherAdmaTableConfig(USDHC_Type *base,
  1468. usdhc_adma_config_t *dmaConfig,
  1469. usdhc_scatter_gather_data_t *dataConfig,
  1470. uint32_t *totalTransferSize)
  1471. {
  1472. assert(NULL != dmaConfig);
  1473. assert((NULL != dmaConfig->admaTable) &&
  1474. (((USDHC_ADMA_TABLE_ADDRESS_ALIGN - 1U) & (uint32_t)dmaConfig->admaTable) == 0UL));
  1475. assert(NULL != dataConfig);
  1476. status_t error = kStatus_Fail;
  1477. uint32_t *admaDesBuffer = dmaConfig->admaTable;
  1478. uint32_t admaDesLen = dmaConfig->admaTableWords;
  1479. usdhc_scatter_gather_data_list_t *sgDataList = &dataConfig->sgData;
  1480. uint32_t oneDescriptorMaxTransferSize = dmaConfig->dmaMode == kUSDHC_DmaModeAdma1 ?
  1481. USDHC_ADMA1_DESCRIPTOR_MAX_LENGTH_PER_ENTRY :
  1482. USDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY;
  1483. uint32_t miniEntries = 0U;
  1484. while (sgDataList != NULL)
  1485. {
  1486. if (dmaConfig->dmaMode == kUSDHC_DmaModeAdma1)
  1487. {
  1488. error = USDHC_SetADMA1Descriptor(admaDesBuffer, admaDesLen, sgDataList->dataAddr, sgDataList->dataSize, 0U);
  1489. }
  1490. /* ADMA2 */
  1491. else
  1492. {
  1493. error = USDHC_SetADMA2Descriptor(admaDesBuffer, admaDesLen, sgDataList->dataAddr, sgDataList->dataSize, 0U);
  1494. }
  1495. if (error != kStatus_Success)
  1496. {
  1497. return kStatus_USDHC_PrepareAdmaDescriptorFailed;
  1498. }
  1499. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1500. if (dataConfig->dataDirection == kUSDHC_TransferDirectionSend)
  1501. {
  1502. /* clear the DCACHE */
  1503. DCACHE_CleanByRange((uint32_t)sgDataList->dataAddr, sgDataList->dataSize);
  1504. }
  1505. else
  1506. {
  1507. /* clear the DCACHE */
  1508. DCACHE_CleanInvalidateByRange((uint32_t)sgDataList->dataAddr, sgDataList->dataSize);
  1509. }
  1510. #endif
  1511. *totalTransferSize += sgDataList->dataSize;
  1512. if (sgDataList->dataList != NULL)
  1513. {
  1514. if ((sgDataList->dataSize % oneDescriptorMaxTransferSize) == 0UL)
  1515. {
  1516. miniEntries = sgDataList->dataSize / oneDescriptorMaxTransferSize;
  1517. }
  1518. else
  1519. {
  1520. miniEntries = ((sgDataList->dataSize / oneDescriptorMaxTransferSize) + 1UL);
  1521. }
  1522. if (dmaConfig->dmaMode == kUSDHC_DmaModeAdma1)
  1523. {
  1524. admaDesBuffer[miniEntries * 2U - 1U] &= ~kUSDHC_Adma1DescriptorEndFlag;
  1525. }
  1526. else
  1527. {
  1528. admaDesBuffer[miniEntries * 2U - 2U] &= ~kUSDHC_Adma2DescriptorEndFlag;
  1529. }
  1530. admaDesBuffer += miniEntries * 2U;
  1531. admaDesLen -= miniEntries * 2U;
  1532. }
  1533. sgDataList = sgDataList->dataList;
  1534. }
  1535. base->DS_ADDR = 0UL;
  1536. base->ADMA_SYS_ADDR = (uint32_t)(dmaConfig->admaTable);
  1537. /* select DMA mode and config the burst length */
  1538. base->PROT_CTRL &= ~(USDHC_PROT_CTRL_DMASEL_MASK);
  1539. base->PROT_CTRL |= USDHC_PROT_CTRL_DMASEL(dmaConfig->dmaMode);
  1540. /* enable DMA */
  1541. base->MIX_CTRL |= USDHC_MIX_CTRL_DMAEN_MASK;
  1542. return error;
  1543. }
  1544. /*!
  1545. * brief Transfers the command/scatter gather data using an interrupt and an asynchronous method.
  1546. *
  1547. * This function sends a command and data and returns immediately. It doesn't wait for the transfer to complete or
  1548. * to encounter an error. The application must not call this API in multiple threads at the same time. Because of that
  1549. * this API doesn't support the re-entry mechanism.
  1550. * This function is target for the application would like to have scatter gather buffer to be transferred within one
  1551. * read/write request, non scatter gather buffer is support by this function also.
  1552. *
  1553. * note Call API @ref USDHC_TransferCreateHandle when calling this API.
  1554. *
  1555. * param base USDHC peripheral base address.
  1556. * param handle USDHC handle.
  1557. * param dmaConfig adma configurations, must be not NULL, since the function is target for ADMA only.
  1558. * param transfer scatter gather transfer content.
  1559. *
  1560. * retval #kStatus_InvalidArgument Argument is invalid.
  1561. * retval #kStatus_USDHC_BusyTransferring Busy transferring.
  1562. * retval #kStatus_USDHC_PrepareAdmaDescriptorFailed Prepare ADMA descriptor failed.
  1563. * retval #kStatus_Success Operate successfully.
  1564. */
  1565. status_t USDHC_TransferScatterGatherADMANonBlocking(USDHC_Type *base,
  1566. usdhc_handle_t *handle,
  1567. usdhc_adma_config_t *dmaConfig,
  1568. usdhc_scatter_gather_transfer_t *transfer)
  1569. {
  1570. assert(handle != NULL);
  1571. assert(transfer != NULL);
  1572. assert(dmaConfig != NULL);
  1573. status_t error = kStatus_Fail;
  1574. usdhc_command_t *command = transfer->command;
  1575. uint32_t totalTransferSize = 0U;
  1576. uint32_t transferFlags = kUSDHC_CommandOnly;
  1577. size_t blockSize = 0U;
  1578. size_t blockCount = 0U;
  1579. usdhc_scatter_gather_data_t *scatterGatherData = transfer->data;
  1580. bool enDMA = false;
  1581. /* check data inhibit flag */
  1582. if (IS_USDHC_FLAG_SET(base->PRES_STATE, kUSDHC_CommandInhibitFlag))
  1583. {
  1584. return kStatus_USDHC_BusyTransferring;
  1585. }
  1586. handle->command = command;
  1587. handle->data = scatterGatherData;
  1588. /* transferredWords will only be updated in ISR when transfer way is DATAPORT. */
  1589. handle->transferredWords = 0UL;
  1590. /* Update ADMA descriptor table according to different DMA mode(ADMA1, ADMA2).*/
  1591. if (scatterGatherData != NULL)
  1592. {
  1593. if (scatterGatherData->sgData.dataAddr == NULL)
  1594. {
  1595. return kStatus_InvalidArgument;
  1596. }
  1597. if (scatterGatherData->dataType != (uint32_t)kUSDHC_TransferDataTuning)
  1598. {
  1599. if (USDHC_SetScatterGatherAdmaTableConfig(base, dmaConfig, transfer->data, &totalTransferSize) !=
  1600. kStatus_Success)
  1601. {
  1602. return kStatus_USDHC_PrepareAdmaDescriptorFailed;
  1603. }
  1604. enDMA = true;
  1605. }
  1606. blockSize = scatterGatherData->blockSize;
  1607. blockCount = totalTransferSize / scatterGatherData->blockSize;
  1608. transferFlags = scatterGatherData->enableAutoCommand12 ? kUSDHC_DataWithAutoCmd12 : 0U;
  1609. transferFlags |= scatterGatherData->enableAutoCommand23 ? kUSDHC_DataWithAutoCmd23 : 0U;
  1610. transferFlags |= scatterGatherData->dataDirection == kUSDHC_TransferDirectionSend ? kUSDHC_CommandAndTxData :
  1611. kUSDHC_CommandAndRxData;
  1612. command->flags |= kUSDHC_DataPresentFlag;
  1613. }
  1614. error = USDHC_SetTransferConfig(base, transferFlags, blockSize, blockCount);
  1615. if (error != kStatus_Success)
  1616. {
  1617. return error;
  1618. }
  1619. /* enable interrupt per transfer request */
  1620. if (scatterGatherData != NULL)
  1621. {
  1622. USDHC_ClearInterruptStatusFlags(
  1623. base, (uint32_t)(enDMA == false ? kUSDHC_DataFlag : kUSDHC_DataDMAFlag | kUSDHC_DmaCompleteFlag) |
  1624. (uint32_t)kUSDHC_CommandFlag);
  1625. USDHC_EnableInterruptSignal(
  1626. base, (uint32_t)(enDMA == false ? kUSDHC_DataFlag : kUSDHC_DataDMAFlag | kUSDHC_DmaCompleteFlag) |
  1627. (uint32_t)kUSDHC_CommandFlag);
  1628. }
  1629. else
  1630. {
  1631. USDHC_ClearInterruptStatusFlags(base, kUSDHC_CommandFlag);
  1632. USDHC_EnableInterruptSignal(base, kUSDHC_CommandFlag);
  1633. }
  1634. /* send command first */
  1635. USDHC_SendCommand(base, command);
  1636. return kStatus_Success;
  1637. }
  1638. #else
  1639. /*!
  1640. * brief Transfers the command/data using an interrupt and an asynchronous method.
  1641. *
  1642. * This function sends a command and data and returns immediately. It doesn't wait the transfer complete or encounter an
  1643. * error.
  1644. * The application must not call this API in multiple threads at the same time. Because of that this API doesn't support
  1645. * the re-entry mechanism.
  1646. *
  1647. * note Call the API 'USDHC_TransferCreateHandle' when calling this API.
  1648. *
  1649. * param base USDHC peripheral base address.
  1650. * param handle USDHC handle.
  1651. * param adma configuration.
  1652. * param transfer Transfer content.
  1653. * retval kStatus_InvalidArgument Argument is invalid.
  1654. * retval kStatus_USDHC_BusyTransferring Busy transferring.
  1655. * retval kStatus_USDHC_PrepareAdmaDescriptorFailed Prepare ADMA descriptor failed.
  1656. * retval kStatus_Success Operate successfully.
  1657. */
  1658. status_t USDHC_TransferNonBlocking(USDHC_Type *base,
  1659. usdhc_handle_t *handle,
  1660. usdhc_adma_config_t *dmaConfig,
  1661. usdhc_transfer_t *transfer)
  1662. {
  1663. assert(handle != NULL);
  1664. assert(transfer != NULL);
  1665. status_t error = kStatus_Fail;
  1666. usdhc_command_t *command = transfer->command;
  1667. usdhc_data_t *data = transfer->data;
  1668. bool executeTuning = ((data == NULL) ? false : data->dataType == (uint32_t)kUSDHC_TransferDataTuning);
  1669. bool enDMA = true;
  1670. uint32_t transferFlags = (uint32_t)kUSDHC_CommandOnly;
  1671. size_t blockSize = 0U;
  1672. size_t blockCount = 0U;
  1673. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  1674. /*check re-tuning request*/
  1675. if ((USDHC_GetInterruptStatusFlags(base) & ((uint32_t)kUSDHC_ReTuningEventFlag)) != 0UL)
  1676. {
  1677. USDHC_ClearInterruptStatusFlags(base, kUSDHC_ReTuningEventFlag);
  1678. return kStatus_USDHC_ReTuningRequest;
  1679. }
  1680. #endif
  1681. /* Save command and data into handle before transferring. */
  1682. handle->command = command;
  1683. handle->data = data;
  1684. /* transferredWords will only be updated in ISR when transfer way is DATAPORT. */
  1685. handle->transferredWords = 0UL;
  1686. if (data != NULL)
  1687. {
  1688. /* Update ADMA descriptor table according to different DMA mode(no DMA, ADMA1, ADMA2).*/
  1689. if ((dmaConfig != NULL) && (!executeTuning))
  1690. {
  1691. error = USDHC_SetAdmaTableConfig(
  1692. base, dmaConfig, data,
  1693. (uint32_t)(IS_USDHC_FLAG_SET(data->dataType, (uint32_t)kUSDHC_TransferDataBoot) ?
  1694. kUSDHC_AdmaDescriptorMultipleFlag :
  1695. kUSDHC_AdmaDescriptorSingleFlag));
  1696. }
  1697. blockSize = data->blockSize;
  1698. blockCount = data->blockCount;
  1699. transferFlags = data->enableAutoCommand12 ? (uint32_t)kUSDHC_DataWithAutoCmd12 : 0U;
  1700. transferFlags |= data->enableAutoCommand23 ? (uint32_t)kUSDHC_DataWithAutoCmd23 : 0U;
  1701. transferFlags |= data->txData != NULL ? (uint32_t)kUSDHC_CommandAndTxData : (uint32_t)kUSDHC_CommandAndRxData;
  1702. transferFlags |= data->dataType == (uint8_t)kUSDHC_TransferDataBoot ? (uint32_t)kUSDHC_BootData : 0U;
  1703. transferFlags |=
  1704. data->dataType == (uint8_t)kUSDHC_TransferDataBootcontinous ? (uint32_t)kUSDHC_BootDataContinuous : 0U;
  1705. command->flags |= (uint32_t)kUSDHC_DataPresentFlag;
  1706. }
  1707. /* if the DMA desciptor configure fail or not needed , disable it */
  1708. if (error != kStatus_Success)
  1709. {
  1710. /* disable DMA, using polling mode in this situation */
  1711. USDHC_EnableInternalDMA(base, false);
  1712. enDMA = false;
  1713. }
  1714. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  1715. else
  1716. {
  1717. if (data->txData != NULL)
  1718. {
  1719. /* clear the DCACHE */
  1720. DCACHE_CleanByRange((uint32_t)data->txData, (data->blockSize) * (data->blockCount));
  1721. }
  1722. else
  1723. {
  1724. /* clear the DCACHE */
  1725. DCACHE_CleanInvalidateByRange((uint32_t)data->rxData, (data->blockSize) * (data->blockCount));
  1726. }
  1727. }
  1728. #endif
  1729. /* config the data transfer parameter */
  1730. error = USDHC_SetTransferConfig(base, transferFlags, blockSize, blockCount);
  1731. if (error != kStatus_Success)
  1732. {
  1733. return error;
  1734. }
  1735. /* enable interrupt per transfer request */
  1736. if (handle->data != NULL)
  1737. {
  1738. USDHC_ClearInterruptStatusFlags(
  1739. base, (uint32_t)(enDMA == false ? kUSDHC_DataFlag : kUSDHC_DataDMAFlag) | (uint32_t)kUSDHC_CommandFlag |
  1740. (uint32_t)(data->dataType == (uint8_t)kUSDHC_TransferDataBootcontinous ?
  1741. (uint32_t)kUSDHC_DmaCompleteFlag :
  1742. 0U));
  1743. USDHC_EnableInterruptSignal(base, (uint32_t)(enDMA == false ? kUSDHC_DataFlag : kUSDHC_DataDMAFlag) |
  1744. (uint32_t)kUSDHC_CommandFlag |
  1745. (uint32_t)(data->dataType == (uint8_t)kUSDHC_TransferDataBootcontinous ?
  1746. (uint32_t)kUSDHC_DmaCompleteFlag :
  1747. 0U));
  1748. }
  1749. else
  1750. {
  1751. USDHC_ClearInterruptStatusFlags(base, kUSDHC_CommandFlag);
  1752. USDHC_EnableInterruptSignal(base, kUSDHC_CommandFlag);
  1753. }
  1754. /* send command first */
  1755. USDHC_SendCommand(base, command);
  1756. return kStatus_Success;
  1757. }
  1758. #endif
  1759. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  1760. /*!
  1761. * brief manual tuning trigger or abort
  1762. * User should handle the tuning cmd and find the boundary of the delay
  1763. * then calucate a average value which will be config to the CLK_TUNE_CTRL_STATUS
  1764. * This function should called before USDHC_AdjustDelayforSDR104 function
  1765. * param base USDHC peripheral base address.
  1766. * param tuning enable flag
  1767. */
  1768. void USDHC_EnableManualTuning(USDHC_Type *base, bool enable)
  1769. {
  1770. if (enable)
  1771. {
  1772. /* make sure std_tun_en bit is clear */
  1773. base->TUNING_CTRL &= ~USDHC_TUNING_CTRL_STD_TUNING_EN_MASK;
  1774. /* disable auto tuning here */
  1775. base->MIX_CTRL &= ~USDHC_MIX_CTRL_AUTO_TUNE_EN_MASK;
  1776. /* execute tuning for SDR104 mode */
  1777. base->MIX_CTRL |= USDHC_MIX_CTRL_EXE_TUNE_MASK | USDHC_MIX_CTRL_SMP_CLK_SEL_MASK;
  1778. }
  1779. else
  1780. { /* abort the tuning */
  1781. base->MIX_CTRL &= ~USDHC_MIX_CTRL_EXE_TUNE_MASK;
  1782. }
  1783. }
  1784. /*!
  1785. * brief the SDR104 mode delay setting adjust
  1786. * This function should called after USDHC_ManualTuningForSDR104
  1787. * param base USDHC peripheral base address.
  1788. * param delay setting configuration
  1789. * retval kStatus_Fail config the delay setting fail
  1790. * retval kStatus_Success config the delay setting success
  1791. */
  1792. status_t USDHC_AdjustDelayForManualTuning(USDHC_Type *base, uint32_t delay)
  1793. {
  1794. uint32_t clkTuneCtrl = 0UL;
  1795. clkTuneCtrl = base->CLK_TUNE_CTRL_STATUS;
  1796. clkTuneCtrl &= ~USDHC_CLK_TUNE_CTRL_STATUS_DLY_CELL_SET_PRE_MASK;
  1797. clkTuneCtrl |= USDHC_CLK_TUNE_CTRL_STATUS_DLY_CELL_SET_PRE(delay);
  1798. /* load the delay setting */
  1799. base->CLK_TUNE_CTRL_STATUS = clkTuneCtrl;
  1800. /* check delat setting error */
  1801. if (IS_USDHC_FLAG_SET(base->CLK_TUNE_CTRL_STATUS,
  1802. USDHC_CLK_TUNE_CTRL_STATUS_PRE_ERR_MASK | USDHC_CLK_TUNE_CTRL_STATUS_NXT_ERR_MASK))
  1803. {
  1804. return kStatus_Fail;
  1805. }
  1806. return kStatus_Success;
  1807. }
  1808. /*!
  1809. * brief The tuning delay cell setting.
  1810. *
  1811. * param base USDHC peripheral base address.
  1812. * param preDelay Set the number of delay cells on the feedback clock between the feedback clock and CLK_PRE.
  1813. * param outDelay Set the number of delay cells on the feedback clock between CLK_PRE and CLK_OUT.
  1814. * param postDelay Set the number of delay cells on the feedback clock between CLK_OUT and CLK_POST.
  1815. * retval kStatus_Fail config the delay setting fail
  1816. * retval kStatus_Success config the delay setting success
  1817. */
  1818. status_t USDHC_SetTuningDelay(USDHC_Type *base, uint32_t preDelay, uint32_t outDelay, uint32_t postDelay)
  1819. {
  1820. assert(preDelay <=
  1821. (USDHC_CLK_TUNE_CTRL_STATUS_DLY_CELL_SET_PRE_MASK >> USDHC_CLK_TUNE_CTRL_STATUS_DLY_CELL_SET_PRE_SHIFT));
  1822. assert(outDelay <=
  1823. (USDHC_CLK_TUNE_CTRL_STATUS_DLY_CELL_SET_OUT_MASK >> USDHC_CLK_TUNE_CTRL_STATUS_DLY_CELL_SET_OUT_SHIFT));
  1824. assert(postDelay <=
  1825. (USDHC_CLK_TUNE_CTRL_STATUS_DLY_CELL_SET_POST_MASK >> USDHC_CLK_TUNE_CTRL_STATUS_DLY_CELL_SET_POST_SHIFT));
  1826. uint32_t clkTuneCtrl = 0UL;
  1827. clkTuneCtrl = base->CLK_TUNE_CTRL_STATUS;
  1828. clkTuneCtrl &=
  1829. ~(USDHC_CLK_TUNE_CTRL_STATUS_DLY_CELL_SET_PRE_MASK | USDHC_CLK_TUNE_CTRL_STATUS_DLY_CELL_SET_OUT_MASK |
  1830. USDHC_CLK_TUNE_CTRL_STATUS_DLY_CELL_SET_POST_MASK);
  1831. clkTuneCtrl |= USDHC_CLK_TUNE_CTRL_STATUS_DLY_CELL_SET_PRE(preDelay) |
  1832. USDHC_CLK_TUNE_CTRL_STATUS_DLY_CELL_SET_OUT(outDelay) |
  1833. USDHC_CLK_TUNE_CTRL_STATUS_DLY_CELL_SET_POST(postDelay);
  1834. /* load the delay setting */
  1835. base->CLK_TUNE_CTRL_STATUS = clkTuneCtrl;
  1836. /* check delat setting error */
  1837. if (IS_USDHC_FLAG_SET(base->CLK_TUNE_CTRL_STATUS,
  1838. USDHC_CLK_TUNE_CTRL_STATUS_PRE_ERR_MASK | USDHC_CLK_TUNE_CTRL_STATUS_NXT_ERR_MASK))
  1839. {
  1840. return kStatus_Fail;
  1841. }
  1842. return kStatus_Success;
  1843. }
  1844. /*!
  1845. * brief the enable standard tuning function
  1846. * The standard tuning window and tuning counter use the default config
  1847. * tuning cmd is send by the software, user need to check the tuning result
  1848. * can be used for SDR50,SDR104,HS200 mode tuning
  1849. * param base USDHC peripheral base address.
  1850. * param tuning start tap
  1851. * param tuning step
  1852. * param enable/disable flag
  1853. */
  1854. void USDHC_EnableStandardTuning(USDHC_Type *base, uint32_t tuningStartTap, uint32_t step, bool enable)
  1855. {
  1856. uint32_t tuningCtrl = 0UL;
  1857. if (enable)
  1858. {
  1859. /* feedback clock */
  1860. base->MIX_CTRL |= USDHC_MIX_CTRL_FBCLK_SEL_MASK;
  1861. /* config tuning start and step */
  1862. tuningCtrl = base->TUNING_CTRL;
  1863. tuningCtrl &= ~(USDHC_TUNING_CTRL_TUNING_START_TAP_MASK | USDHC_TUNING_CTRL_TUNING_STEP_MASK);
  1864. tuningCtrl |= (USDHC_TUNING_CTRL_TUNING_START_TAP(tuningStartTap) | USDHC_TUNING_CTRL_TUNING_STEP(step) |
  1865. USDHC_TUNING_CTRL_STD_TUNING_EN_MASK);
  1866. base->TUNING_CTRL = tuningCtrl;
  1867. /* excute tuning */
  1868. base->AUTOCMD12_ERR_STATUS |=
  1869. (USDHC_AUTOCMD12_ERR_STATUS_EXECUTE_TUNING_MASK | USDHC_AUTOCMD12_ERR_STATUS_SMP_CLK_SEL_MASK);
  1870. }
  1871. else
  1872. {
  1873. /* disable the standard tuning */
  1874. base->TUNING_CTRL &= ~USDHC_TUNING_CTRL_STD_TUNING_EN_MASK;
  1875. /* clear excute tuning */
  1876. base->AUTOCMD12_ERR_STATUS &=
  1877. ~(USDHC_AUTOCMD12_ERR_STATUS_EXECUTE_TUNING_MASK | USDHC_AUTOCMD12_ERR_STATUS_SMP_CLK_SEL_MASK);
  1878. }
  1879. }
  1880. #if FSL_FEATURE_USDHC_HAS_HS400_MODE
  1881. /*!
  1882. * brief config the strobe DLL delay target and update interval
  1883. *
  1884. * param base USDHC peripheral base address.
  1885. * param delayTarget delay target
  1886. * param updateInterval update interval
  1887. */
  1888. void USDHC_ConfigStrobeDLL(USDHC_Type *base, uint32_t delayTarget, uint32_t updateInterval)
  1889. {
  1890. assert(delayTarget <= (USDHC_STROBE_DLL_CTRL_STROBE_DLL_CTRL_SLV_DLY_TARGET_MASK >>
  1891. USDHC_STROBE_DLL_CTRL_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT));
  1892. /* reset strobe dll firstly */
  1893. base->STROBE_DLL_CTRL |= USDHC_STROBE_DLL_CTRL_STROBE_DLL_CTRL_RESET_MASK;
  1894. /* clear reset and other register fields */
  1895. base->STROBE_DLL_CTRL = 0;
  1896. /* configure the DELAY target and update interval */
  1897. base->STROBE_DLL_CTRL |= USDHC_STROBE_DLL_CTRL_STROBE_DLL_CTRL_ENABLE_MASK |
  1898. USDHC_STROBE_DLL_CTRL_STROBE_DLL_CTRL_SLV_UPDATE_INT(updateInterval) |
  1899. USDHC_STROBE_DLL_CTRL_STROBE_DLL_CTRL_SLV_DLY_TARGET(delayTarget);
  1900. while (
  1901. (USDHC_GetStrobeDLLStatus(base) & (USDHC_STROBE_DLL_STATUS_STROBE_DLL_STS_SLV_LOCK_MASK |
  1902. USDHC_STROBE_DLL_STATUS_STROBE_DLL_STS_REF_LOCK_MASK)) !=
  1903. ((USDHC_STROBE_DLL_STATUS_STROBE_DLL_STS_SLV_LOCK_MASK | USDHC_STROBE_DLL_STATUS_STROBE_DLL_STS_REF_LOCK_MASK)))
  1904. {
  1905. }
  1906. }
  1907. #endif
  1908. /*!
  1909. * brief the auto tuning enbale for CMD/DATA line
  1910. *
  1911. * param base USDHC peripheral base address.
  1912. */
  1913. void USDHC_EnableAutoTuningForCmdAndData(USDHC_Type *base)
  1914. {
  1915. uint32_t busWidth = (base->PROT_CTRL & USDHC_PROT_CTRL_DTW_MASK) >> USDHC_PROT_CTRL_DTW_SHIFT;
  1916. base->VEND_SPEC2 |= USDHC_VEND_SPEC2_TUNING_CMD_EN_MASK;
  1917. /* 1bit data width */
  1918. if (busWidth == 0UL)
  1919. {
  1920. base->VEND_SPEC2 &= ~USDHC_VEND_SPEC2_TUNING_8bit_EN_MASK;
  1921. base->VEND_SPEC2 |= USDHC_VEND_SPEC2_TUNING_1bit_EN_MASK;
  1922. }
  1923. /* 4bit data width */
  1924. else if (busWidth == 1UL)
  1925. {
  1926. base->VEND_SPEC2 &= ~USDHC_VEND_SPEC2_TUNING_8bit_EN_MASK;
  1927. base->VEND_SPEC2 &= ~USDHC_VEND_SPEC2_TUNING_1bit_EN_MASK;
  1928. }
  1929. /* 8bit data width */
  1930. else
  1931. {
  1932. base->VEND_SPEC2 |= USDHC_VEND_SPEC2_TUNING_8bit_EN_MASK;
  1933. base->VEND_SPEC2 &= ~USDHC_VEND_SPEC2_TUNING_1bit_EN_MASK;
  1934. }
  1935. }
  1936. #endif /* FSL_FEATURE_USDHC_HAS_SDR50_MODE */
  1937. static void USDHC_TransferHandleCardDetect(USDHC_Type *base, usdhc_handle_t *handle, uint32_t interruptFlags)
  1938. {
  1939. if (IS_USDHC_FLAG_SET(interruptFlags, kUSDHC_CardInsertionFlag))
  1940. {
  1941. if (handle->callback.CardInserted != NULL)
  1942. {
  1943. handle->callback.CardInserted(base, handle->userData);
  1944. }
  1945. }
  1946. else
  1947. {
  1948. if (handle->callback.CardRemoved != NULL)
  1949. {
  1950. handle->callback.CardRemoved(base, handle->userData);
  1951. }
  1952. }
  1953. }
  1954. static void USDHC_TransferHandleCommand(USDHC_Type *base, usdhc_handle_t *handle, uint32_t interruptFlags)
  1955. {
  1956. assert(handle->command != NULL);
  1957. if (IS_USDHC_FLAG_SET(interruptFlags, kUSDHC_CommandErrorFlag))
  1958. {
  1959. if (handle->callback.TransferComplete != NULL)
  1960. {
  1961. handle->callback.TransferComplete(base, handle, kStatus_USDHC_SendCommandFailed, handle->userData);
  1962. }
  1963. }
  1964. else
  1965. {
  1966. /* Receive response */
  1967. if (kStatus_Success != USDHC_ReceiveCommandResponse(base, handle->command))
  1968. {
  1969. if (handle->callback.TransferComplete != NULL)
  1970. {
  1971. handle->callback.TransferComplete(base, handle, kStatus_USDHC_SendCommandFailed, handle->userData);
  1972. }
  1973. }
  1974. else
  1975. {
  1976. if (handle->callback.TransferComplete != NULL)
  1977. {
  1978. handle->callback.TransferComplete(base, handle, kStatus_USDHC_SendCommandSuccess, handle->userData);
  1979. }
  1980. }
  1981. }
  1982. /* disable interrupt signal and reset command pointer */
  1983. USDHC_DisableInterruptSignal(base, kUSDHC_CommandFlag);
  1984. handle->command = NULL;
  1985. }
  1986. #if (defined FSL_USDHC_ENABLE_SCATTER_GATHER_TRANSFER) && FSL_USDHC_ENABLE_SCATTER_GATHER_TRANSFER
  1987. static void USDHC_TransferHandleData(USDHC_Type *base, usdhc_handle_t *handle, uint32_t interruptFlags)
  1988. {
  1989. assert(handle->data != NULL);
  1990. status_t transferStatus = kStatus_USDHC_BusyTransferring;
  1991. if ((!(handle->data->enableIgnoreError)) &&
  1992. (IS_USDHC_FLAG_SET(interruptFlags, (uint32_t)kUSDHC_DataErrorFlag | (uint32_t)kUSDHC_DmaErrorFlag)))
  1993. {
  1994. transferStatus = kStatus_USDHC_TransferDataFailed;
  1995. }
  1996. else
  1997. {
  1998. if (IS_USDHC_FLAG_SET(interruptFlags, kUSDHC_BufferReadReadyFlag))
  1999. {
  2000. /* std tuning process only need to wait BRR */
  2001. if (handle->data->dataType == (uint32_t)kUSDHC_TransferDataTuning)
  2002. {
  2003. transferStatus = kStatus_USDHC_TransferDataComplete;
  2004. }
  2005. }
  2006. else
  2007. {
  2008. if (IS_USDHC_FLAG_SET(interruptFlags, kUSDHC_DmaCompleteFlag))
  2009. {
  2010. transferStatus = kStatus_USDHC_TransferDMAComplete;
  2011. }
  2012. if (IS_USDHC_FLAG_SET(interruptFlags, kUSDHC_DataCompleteFlag))
  2013. {
  2014. transferStatus = kStatus_USDHC_TransferDataComplete;
  2015. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  2016. if (handle->data->dataDirection == kUSDHC_TransferDirectionReceive)
  2017. {
  2018. usdhc_scatter_gather_data_list_t *sgDataList = &handle->data->sgData;
  2019. while (sgDataList != NULL)
  2020. {
  2021. DCACHE_InvalidateByRange((uint32_t)sgDataList->dataAddr, sgDataList->dataSize);
  2022. sgDataList = sgDataList->dataList;
  2023. }
  2024. }
  2025. #endif
  2026. }
  2027. }
  2028. }
  2029. if ((handle->callback.TransferComplete != NULL) && (transferStatus != kStatus_USDHC_BusyTransferring))
  2030. {
  2031. handle->callback.TransferComplete(base, handle, transferStatus, handle->userData);
  2032. USDHC_DisableInterruptSignal(
  2033. base, (uint32_t)kUSDHC_DataFlag | (uint32_t)kUSDHC_DataDMAFlag | (uint32_t)kUSDHC_DmaCompleteFlag);
  2034. handle->data = NULL;
  2035. }
  2036. }
  2037. #else
  2038. static void USDHC_TransferHandleData(USDHC_Type *base, usdhc_handle_t *handle, uint32_t interruptFlags)
  2039. {
  2040. assert(handle->data != NULL);
  2041. status_t transferStatus = kStatus_USDHC_BusyTransferring;
  2042. uint32_t transferredWords = handle->transferredWords;
  2043. if ((!(handle->data->enableIgnoreError)) &&
  2044. (IS_USDHC_FLAG_SET(interruptFlags, (uint32_t)kUSDHC_DataErrorFlag | (uint32_t)kUSDHC_DmaErrorFlag)))
  2045. {
  2046. transferStatus = kStatus_USDHC_TransferDataFailed;
  2047. }
  2048. else
  2049. {
  2050. if (IS_USDHC_FLAG_SET(interruptFlags, kUSDHC_BufferReadReadyFlag))
  2051. {
  2052. /* std tuning process only need to wait BRR */
  2053. if (handle->data->dataType == (uint32_t)kUSDHC_TransferDataTuning)
  2054. {
  2055. transferStatus = kStatus_USDHC_TransferDataComplete;
  2056. }
  2057. else
  2058. {
  2059. handle->transferredWords = USDHC_ReadDataPort(base, handle->data, transferredWords);
  2060. }
  2061. }
  2062. else if (IS_USDHC_FLAG_SET(interruptFlags, kUSDHC_BufferWriteReadyFlag))
  2063. {
  2064. handle->transferredWords = USDHC_WriteDataPort(base, handle->data, transferredWords);
  2065. }
  2066. else
  2067. {
  2068. if ((IS_USDHC_FLAG_SET(interruptFlags, kUSDHC_DmaCompleteFlag)) &&
  2069. (handle->data->dataType == (uint32_t)kUSDHC_TransferDataBootcontinous))
  2070. {
  2071. *(handle->data->rxData) = s_usdhcBootDummy;
  2072. }
  2073. if (IS_USDHC_FLAG_SET(interruptFlags, kUSDHC_DataCompleteFlag))
  2074. {
  2075. transferStatus = kStatus_USDHC_TransferDataComplete;
  2076. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  2077. if (handle->data->rxData != NULL)
  2078. {
  2079. DCACHE_InvalidateByRange((uint32_t)(handle->data->rxData),
  2080. (handle->data->blockSize) * (handle->data->blockCount));
  2081. }
  2082. #endif
  2083. }
  2084. }
  2085. }
  2086. if ((handle->callback.TransferComplete != NULL) && (transferStatus != kStatus_USDHC_BusyTransferring))
  2087. {
  2088. handle->callback.TransferComplete(base, handle, transferStatus, handle->userData);
  2089. USDHC_DisableInterruptSignal(base, (uint32_t)kUSDHC_DataFlag | (uint32_t)kUSDHC_DataDMAFlag);
  2090. handle->data = NULL;
  2091. }
  2092. }
  2093. #endif
  2094. static void USDHC_TransferHandleSdioInterrupt(USDHC_Type *base, usdhc_handle_t *handle)
  2095. {
  2096. if (handle->callback.SdioInterrupt != NULL)
  2097. {
  2098. handle->callback.SdioInterrupt(base, handle->userData);
  2099. }
  2100. }
  2101. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  2102. static void USDHC_TransferHandleReTuning(USDHC_Type *base, usdhc_handle_t *handle, uint32_t interruptFlags)
  2103. {
  2104. assert(handle->callback.ReTuning != NULL);
  2105. /* retuning request */
  2106. if (IS_USDHC_FLAG_SET(interruptFlags, kUSDHC_TuningErrorFlag))
  2107. {
  2108. handle->callback.ReTuning(base, handle->userData); /* retuning fail */
  2109. }
  2110. }
  2111. #endif
  2112. static void USDHC_TransferHandleBlockGap(USDHC_Type *base, usdhc_handle_t *handle)
  2113. {
  2114. if (handle->callback.BlockGap != NULL)
  2115. {
  2116. handle->callback.BlockGap(base, handle->userData);
  2117. }
  2118. }
  2119. /*!
  2120. * brief Creates the USDHC handle.
  2121. *
  2122. * param base USDHC peripheral base address.
  2123. * param handle USDHC handle pointer.
  2124. * param callback Structure pointer to contain all callback functions.
  2125. * param userData Callback function parameter.
  2126. */
  2127. void USDHC_TransferCreateHandle(USDHC_Type *base,
  2128. usdhc_handle_t *handle,
  2129. const usdhc_transfer_callback_t *callback,
  2130. void *userData)
  2131. {
  2132. assert(handle != NULL);
  2133. assert(callback != NULL);
  2134. /* Zero the handle. */
  2135. (void)memset(handle, 0, sizeof(*handle));
  2136. /* Set the callback. */
  2137. handle->callback.CardInserted = callback->CardInserted;
  2138. handle->callback.CardRemoved = callback->CardRemoved;
  2139. handle->callback.SdioInterrupt = callback->SdioInterrupt;
  2140. handle->callback.BlockGap = callback->BlockGap;
  2141. handle->callback.TransferComplete = callback->TransferComplete;
  2142. handle->callback.ReTuning = callback->ReTuning;
  2143. handle->userData = userData;
  2144. /* Save the handle in global variables to support the double weak mechanism. */
  2145. s_usdhcHandle[USDHC_GetInstance(base)] = handle;
  2146. /* save IRQ handler */
  2147. s_usdhcIsr = USDHC_TransferHandleIRQ;
  2148. (void)EnableIRQ(s_usdhcIRQ[USDHC_GetInstance(base)]);
  2149. }
  2150. /*!
  2151. * brief IRQ handler for the USDHC.
  2152. *
  2153. * This function deals with the IRQs on the given host controller.
  2154. *
  2155. * param base USDHC peripheral base address.
  2156. * param handle USDHC handle.
  2157. */
  2158. void USDHC_TransferHandleIRQ(USDHC_Type *base, usdhc_handle_t *handle)
  2159. {
  2160. assert(handle != NULL);
  2161. uint32_t interruptFlags;
  2162. interruptFlags = USDHC_GetEnabledInterruptStatusFlags(base);
  2163. if (IS_USDHC_FLAG_SET(interruptFlags, kUSDHC_CardDetectFlag))
  2164. {
  2165. USDHC_TransferHandleCardDetect(base, handle, interruptFlags);
  2166. }
  2167. if (IS_USDHC_FLAG_SET(interruptFlags, kUSDHC_CommandFlag))
  2168. {
  2169. USDHC_TransferHandleCommand(base, handle, interruptFlags);
  2170. }
  2171. if (IS_USDHC_FLAG_SET(interruptFlags, kUSDHC_DataFlag))
  2172. {
  2173. USDHC_TransferHandleData(base, handle, interruptFlags);
  2174. }
  2175. if (IS_USDHC_FLAG_SET(interruptFlags, kUSDHC_CardInterruptFlag))
  2176. {
  2177. USDHC_TransferHandleSdioInterrupt(base, handle);
  2178. }
  2179. if (IS_USDHC_FLAG_SET(interruptFlags, kUSDHC_BlockGapEventFlag))
  2180. {
  2181. USDHC_TransferHandleBlockGap(base, handle);
  2182. }
  2183. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) && (FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  2184. if (IS_USDHC_FLAG_SET(interruptFlags, kUSDHC_SDR104TuningFlag))
  2185. {
  2186. USDHC_TransferHandleReTuning(base, handle, interruptFlags);
  2187. }
  2188. #endif
  2189. USDHC_ClearInterruptStatusFlags(base, interruptFlags);
  2190. }
  2191. #ifdef USDHC0
  2192. void USDHC0_DriverIRQHandler(void);
  2193. void USDHC0_DriverIRQHandler(void)
  2194. {
  2195. s_usdhcIsr(s_usdhcBase[0U], s_usdhcHandle[0U]);
  2196. SDK_ISR_EXIT_BARRIER;
  2197. }
  2198. #endif
  2199. #ifdef USDHC1
  2200. void USDHC1_DriverIRQHandler(void);
  2201. void USDHC1_DriverIRQHandler(void)
  2202. {
  2203. s_usdhcIsr(s_usdhcBase[1U], s_usdhcHandle[1U]);
  2204. SDK_ISR_EXIT_BARRIER;
  2205. }
  2206. #endif
  2207. #ifdef USDHC2
  2208. void USDHC2_DriverIRQHandler(void);
  2209. void USDHC2_DriverIRQHandler(void)
  2210. {
  2211. s_usdhcIsr(s_usdhcBase[2U], s_usdhcHandle[2U]);
  2212. SDK_ISR_EXIT_BARRIER;
  2213. }
  2214. #endif