fsl_sdhc.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_sdhc.h"
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /*! @brief Clock setting */
  39. /* Max SD clock divisor from base clock */
  40. #define SDHC_MAX_DVS ((SDHC_SYSCTL_DVS_MASK >> SDHC_SYSCTL_DVS_SHIFT) + 1U)
  41. #define SDHC_PREV_DVS(x) ((x) -= 1U)
  42. #define SDHC_MAX_CLKFS ((SDHC_SYSCTL_SDCLKFS_MASK >> SDHC_SYSCTL_SDCLKFS_SHIFT) + 1U)
  43. #define SDHC_PREV_CLKFS(x) ((x) >>= 1U)
  44. /* Typedef for interrupt handler. */
  45. typedef void (*sdhc_isr_t)(SDHC_Type *base, sdhc_handle_t *handle);
  46. /*! @brief ADMA table configuration */
  47. typedef struct _sdhc_adma_table_config
  48. {
  49. uint32_t *admaTable; /*!< ADMA table address, can't be null if transfer way is ADMA1/ADMA2 */
  50. uint32_t admaTableWords; /*!< ADMA table length united as words, can't be 0 if transfer way is ADMA1/ADMA2 */
  51. } sdhc_adma_table_config_t;
  52. /*******************************************************************************
  53. * Prototypes
  54. ******************************************************************************/
  55. /*!
  56. * @brief Get the instance.
  57. *
  58. * @param base SDHC peripheral base address.
  59. * @return Instance number.
  60. */
  61. static uint32_t SDHC_GetInstance(SDHC_Type *base);
  62. /*!
  63. * @brief Set transfer interrupt.
  64. *
  65. * @param base SDHC peripheral base address.
  66. * @param usingInterruptSignal True to use IRQ signal.
  67. */
  68. static void SDHC_SetTransferInterrupt(SDHC_Type *base, bool usingInterruptSignal);
  69. /*!
  70. * @brief Start transfer according to current transfer state
  71. *
  72. * @param base SDHC peripheral base address.
  73. * @param command Command to be sent.
  74. * @param data Data to be transferred.
  75. * @param DMA mode selection
  76. */
  77. static void SDHC_StartTransfer(SDHC_Type *base, sdhc_command_t *command, sdhc_data_t *data, sdhc_dma_mode_t dmaMode);
  78. /*!
  79. * @brief Receive command response
  80. *
  81. * @param base SDHC peripheral base address.
  82. * @param command Command to be sent.
  83. */
  84. static status_t SDHC_ReceiveCommandResponse(SDHC_Type *base, sdhc_command_t *command);
  85. /*!
  86. * @brief Read DATAPORT when buffer enable bit is set.
  87. *
  88. * @param base SDHC peripheral base address.
  89. * @param data Data to be read.
  90. * @param transferredWords The number of data words have been transferred last time transaction.
  91. * @return The number of total data words have been transferred after this time transaction.
  92. */
  93. static uint32_t SDHC_ReadDataPort(SDHC_Type *base, sdhc_data_t *data, uint32_t transferredWords);
  94. /*!
  95. * @brief Read data by using DATAPORT polling way.
  96. *
  97. * @param base SDHC peripheral base address.
  98. * @param data Data to be read.
  99. * @retval kStatus_Fail Read DATAPORT failed.
  100. * @retval kStatus_Success Operate successfully.
  101. */
  102. static status_t SDHC_ReadByDataPortBlocking(SDHC_Type *base, sdhc_data_t *data);
  103. /*!
  104. * @brief Write DATAPORT when buffer enable bit is set.
  105. *
  106. * @param base SDHC peripheral base address.
  107. * @param data Data to be read.
  108. * @param transferredWords The number of data words have been transferred last time.
  109. * @return The number of total data words have been transferred after this time transaction.
  110. */
  111. static uint32_t SDHC_WriteDataPort(SDHC_Type *base, sdhc_data_t *data, uint32_t transferredWords);
  112. /*!
  113. * @brief Write data by using DATAPORT polling way.
  114. *
  115. * @param base SDHC peripheral base address.
  116. * @param data Data to be transferred.
  117. * @retval kStatus_Fail Write DATAPORT failed.
  118. * @retval kStatus_Success Operate successfully.
  119. */
  120. static status_t SDHC_WriteByDataPortBlocking(SDHC_Type *base, sdhc_data_t *data);
  121. /*!
  122. * @brief Send command by using polling way.
  123. *
  124. * @param base SDHC peripheral base address.
  125. * @param command Command to be sent.
  126. * @retval kStatus_Fail Send command failed.
  127. * @retval kStatus_Success Operate successfully.
  128. */
  129. static status_t SDHC_SendCommandBlocking(SDHC_Type *base, sdhc_command_t *command);
  130. /*!
  131. * @brief Transfer data by DATAPORT and polling way.
  132. *
  133. * @param base SDHC peripheral base address.
  134. * @param data Data to be transferred.
  135. * @retval kStatus_Fail Transfer data failed.
  136. * @retval kStatus_Success Operate successfully.
  137. */
  138. static status_t SDHC_TransferByDataPortBlocking(SDHC_Type *base, sdhc_data_t *data);
  139. /*!
  140. * @brief Transfer data by ADMA2 and polling way.
  141. *
  142. * @param base SDHC peripheral base address.
  143. * @param data Data to be transferred.
  144. * @retval kStatus_Fail Transfer data failed.
  145. * @retval kStatus_Success Operate successfully.
  146. */
  147. static status_t SDHC_TransferByAdma2Blocking(SDHC_Type *base, sdhc_data_t *data);
  148. /*!
  149. * @brief Transfer data by polling way.
  150. *
  151. * @param dmaMode DMA mode.
  152. * @param base SDHC peripheral base address.
  153. * @param data Data to be transferred.
  154. * @retval kStatus_Fail Transfer data failed.
  155. * @retval kStatus_InvalidArgument Argument is invalid.
  156. * @retval kStatus_Success Operate successfully.
  157. */
  158. static status_t SDHC_TransferDataBlocking(sdhc_dma_mode_t dmaMode, SDHC_Type *base, sdhc_data_t *data);
  159. /*!
  160. * @brief Handle card detect interrupt.
  161. *
  162. * @param base SDHC peripheral base address.
  163. * @param handle SDHC handle.
  164. * @param interruptFlags Card detect related interrupt flags.
  165. */
  166. static void SDHC_TransferHandleCardDetect(SDHC_Type *base, sdhc_handle_t *handle, uint32_t interruptFlags);
  167. /*!
  168. * @brief Handle command interrupt.
  169. *
  170. * @param base SDHC peripheral base address.
  171. * @param handle SDHC handle.
  172. * @param interruptFlags Command related interrupt flags.
  173. */
  174. static void SDHC_TransferHandleCommand(SDHC_Type *base, sdhc_handle_t *handle, uint32_t interruptFlags);
  175. /*!
  176. * @brief Handle data interrupt.
  177. *
  178. * @param base SDHC peripheral base address.
  179. * @param handle SDHC handle.
  180. * @param interruptFlags Data related interrupt flags.
  181. */
  182. static void SDHC_TransferHandleData(SDHC_Type *base, sdhc_handle_t *handle, uint32_t interruptFlags);
  183. /*!
  184. * @brief Handle SDIO card interrupt signal.
  185. *
  186. * @param base SDHC peripheral base address.
  187. * @param handle SDHC handle.
  188. */
  189. static void SDHC_TransferHandleSdioInterrupt(SDHC_Type *base, sdhc_handle_t *handle);
  190. /*!
  191. * @brief Handle SDIO block gap event.
  192. *
  193. * @param base SDHC peripheral base address.
  194. * @param handle SDHC handle.
  195. */
  196. static void SDHC_TransferHandleSdioBlockGap(SDHC_Type *base, sdhc_handle_t *handle);
  197. /*******************************************************************************
  198. * Variables
  199. ******************************************************************************/
  200. /*! @brief SDHC internal handle pointer array */
  201. static sdhc_handle_t *s_sdhcHandle[FSL_FEATURE_SOC_SDHC_COUNT];
  202. /*! @brief SDHC base pointer array */
  203. static SDHC_Type *const s_sdhcBase[] = SDHC_BASE_PTRS;
  204. /*! @brief SDHC IRQ name array */
  205. static const IRQn_Type s_sdhcIRQ[] = SDHC_IRQS;
  206. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  207. /*! @brief SDHC clock array name */
  208. static const clock_ip_name_t s_sdhcClock[] = SDHC_CLOCKS;
  209. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  210. /* SDHC ISR for transactional APIs. */
  211. static sdhc_isr_t s_sdhcIsr;
  212. /*******************************************************************************
  213. * Code
  214. ******************************************************************************/
  215. static uint32_t SDHC_GetInstance(SDHC_Type *base)
  216. {
  217. uint8_t instance = 0;
  218. while ((instance < ARRAY_SIZE(s_sdhcBase)) && (s_sdhcBase[instance] != base))
  219. {
  220. instance++;
  221. }
  222. assert(instance < ARRAY_SIZE(s_sdhcBase));
  223. return instance;
  224. }
  225. static void SDHC_SetTransferInterrupt(SDHC_Type *base, bool usingInterruptSignal)
  226. {
  227. uint32_t interruptEnabled; /* The Interrupt status flags to be enabled */
  228. bool cardDetectDat3 = (bool)(base->PROCTL & SDHC_PROCTL_D3CD_MASK);
  229. /* Disable all interrupts */
  230. SDHC_DisableInterruptStatus(base, (uint32_t)kSDHC_AllInterruptFlags);
  231. SDHC_DisableInterruptSignal(base, (uint32_t)kSDHC_AllInterruptFlags);
  232. DisableIRQ(s_sdhcIRQ[SDHC_GetInstance(base)]);
  233. interruptEnabled =
  234. (kSDHC_CommandIndexErrorFlag | kSDHC_CommandCrcErrorFlag | kSDHC_CommandEndBitErrorFlag |
  235. kSDHC_CommandTimeoutFlag | kSDHC_CommandCompleteFlag | kSDHC_DataTimeoutFlag | kSDHC_DataCrcErrorFlag |
  236. kSDHC_DataEndBitErrorFlag | kSDHC_DataCompleteFlag | kSDHC_AutoCommand12ErrorFlag | kSDHC_BufferReadReadyFlag |
  237. kSDHC_BufferWriteReadyFlag | kSDHC_DmaErrorFlag | kSDHC_DmaCompleteFlag);
  238. if (cardDetectDat3)
  239. {
  240. interruptEnabled |= (kSDHC_CardInsertionFlag | kSDHC_CardRemovalFlag);
  241. }
  242. SDHC_EnableInterruptStatus(base, interruptEnabled);
  243. if (usingInterruptSignal)
  244. {
  245. SDHC_EnableInterruptSignal(base, interruptEnabled);
  246. }
  247. }
  248. static void SDHC_StartTransfer(SDHC_Type *base, sdhc_command_t *command, sdhc_data_t *data, sdhc_dma_mode_t dmaMode)
  249. {
  250. uint32_t flags = 0U;
  251. sdhc_transfer_config_t sdhcTransferConfig = {0};
  252. /* Define the flag corresponding to each response type. */
  253. switch (command->responseType)
  254. {
  255. case kCARD_ResponseTypeNone:
  256. break;
  257. case kCARD_ResponseTypeR1: /* Response 1 */
  258. flags |= (kSDHC_ResponseLength48Flag | kSDHC_EnableCrcCheckFlag | kSDHC_EnableIndexCheckFlag);
  259. break;
  260. case kCARD_ResponseTypeR1b: /* Response 1 with busy */
  261. flags |= (kSDHC_ResponseLength48BusyFlag | kSDHC_EnableCrcCheckFlag | kSDHC_EnableIndexCheckFlag);
  262. break;
  263. case kCARD_ResponseTypeR2: /* Response 2 */
  264. flags |= (kSDHC_ResponseLength136Flag | kSDHC_EnableCrcCheckFlag);
  265. break;
  266. case kCARD_ResponseTypeR3: /* Response 3 */
  267. flags |= (kSDHC_ResponseLength48Flag);
  268. break;
  269. case kCARD_ResponseTypeR4: /* Response 4 */
  270. flags |= (kSDHC_ResponseLength48Flag);
  271. break;
  272. case kCARD_ResponseTypeR5: /* Response 5 */
  273. flags |= (kSDHC_ResponseLength48Flag | kSDHC_EnableCrcCheckFlag | kSDHC_EnableIndexCheckFlag);
  274. break;
  275. case kCARD_ResponseTypeR5b: /* Response 5 with busy */
  276. flags |= (kSDHC_ResponseLength48BusyFlag | kSDHC_EnableCrcCheckFlag | kSDHC_EnableIndexCheckFlag);
  277. break;
  278. case kCARD_ResponseTypeR6: /* Response 6 */
  279. flags |= (kSDHC_ResponseLength48Flag | kSDHC_EnableCrcCheckFlag | kSDHC_EnableIndexCheckFlag);
  280. break;
  281. case kCARD_ResponseTypeR7: /* Response 7 */
  282. flags |= (kSDHC_ResponseLength48Flag | kSDHC_EnableCrcCheckFlag | kSDHC_EnableIndexCheckFlag);
  283. break;
  284. default:
  285. break;
  286. }
  287. if (command->type == kCARD_CommandTypeAbort)
  288. {
  289. flags |= kSDHC_CommandTypeAbortFlag;
  290. }
  291. if (data)
  292. {
  293. flags |= kSDHC_DataPresentFlag;
  294. if (dmaMode != kSDHC_DmaModeNo)
  295. {
  296. flags |= kSDHC_EnableDmaFlag;
  297. }
  298. if (data->rxData)
  299. {
  300. flags |= kSDHC_DataReadFlag;
  301. }
  302. if (data->blockCount > 1U)
  303. {
  304. flags |= (kSDHC_MultipleBlockFlag | kSDHC_EnableBlockCountFlag);
  305. if (data->enableAutoCommand12)
  306. {
  307. /* Enable Auto command 12. */
  308. flags |= kSDHC_EnableAutoCommand12Flag;
  309. }
  310. }
  311. sdhcTransferConfig.dataBlockSize = data->blockSize;
  312. sdhcTransferConfig.dataBlockCount = data->blockCount;
  313. }
  314. else
  315. {
  316. sdhcTransferConfig.dataBlockSize = 0U;
  317. sdhcTransferConfig.dataBlockCount = 0U;
  318. }
  319. sdhcTransferConfig.commandArgument = command->argument;
  320. sdhcTransferConfig.commandIndex = command->index;
  321. sdhcTransferConfig.flags = flags;
  322. SDHC_SetTransferConfig(base, &sdhcTransferConfig);
  323. }
  324. static status_t SDHC_ReceiveCommandResponse(SDHC_Type *base, sdhc_command_t *command)
  325. {
  326. uint32_t i;
  327. if (command->responseType != kCARD_ResponseTypeNone)
  328. {
  329. command->response[0U] = SDHC_GetCommandResponse(base, 0U);
  330. if (command->responseType == kCARD_ResponseTypeR2)
  331. {
  332. command->response[1U] = SDHC_GetCommandResponse(base, 1U);
  333. command->response[2U] = SDHC_GetCommandResponse(base, 2U);
  334. command->response[3U] = SDHC_GetCommandResponse(base, 3U);
  335. i = 4U;
  336. /* R3-R2-R1-R0(lowest 8 bit is invalid bit) has the same format as R2 format in SD specification document
  337. after removed internal CRC7 and end bit. */
  338. do
  339. {
  340. command->response[i - 1U] <<= 8U;
  341. if (i > 1U)
  342. {
  343. command->response[i - 1U] |= ((command->response[i - 2U] & 0xFF000000U) >> 24U);
  344. }
  345. } while (i--);
  346. }
  347. }
  348. /* check response error flag */
  349. if ((command->responseErrorFlags != 0U) &&
  350. ((command->responseType == kCARD_ResponseTypeR1) || (command->responseType == kCARD_ResponseTypeR1b) ||
  351. (command->responseType == kCARD_ResponseTypeR6) || (command->responseType == kCARD_ResponseTypeR5)))
  352. {
  353. if (((command->responseErrorFlags) & (command->response[0U])) != 0U)
  354. {
  355. return kStatus_SDHC_SendCommandFailed;
  356. }
  357. }
  358. return kStatus_Success;
  359. }
  360. static uint32_t SDHC_ReadDataPort(SDHC_Type *base, sdhc_data_t *data, uint32_t transferredWords)
  361. {
  362. uint32_t i;
  363. uint32_t totalWords;
  364. uint32_t wordsCanBeRead; /* The words can be read at this time. */
  365. uint32_t readWatermark = ((base->WML & SDHC_WML_RDWML_MASK) >> SDHC_WML_RDWML_SHIFT);
  366. /*
  367. * Add non aligned access support ,user need make sure your buffer size is big
  368. * enough to hold the data,in other words,user need make sure the buffer size
  369. * is 4 byte aligned
  370. */
  371. if (data->blockSize % sizeof(uint32_t) != 0U)
  372. {
  373. data->blockSize +=
  374. sizeof(uint32_t) - (data->blockSize % sizeof(uint32_t)); /* make the block size as word-aligned */
  375. }
  376. totalWords = ((data->blockCount * data->blockSize) / sizeof(uint32_t));
  377. /* If watermark level is equal or bigger than totalWords, transfers totalWords data. */
  378. if (readWatermark >= totalWords)
  379. {
  380. wordsCanBeRead = totalWords;
  381. }
  382. /* If watermark level is less than totalWords and left words to be sent is equal or bigger than readWatermark,
  383. transfers watermark level words. */
  384. else if ((readWatermark < totalWords) && ((totalWords - transferredWords) >= readWatermark))
  385. {
  386. wordsCanBeRead = readWatermark;
  387. }
  388. /* If watermark level is less than totalWords and left words to be sent is less than readWatermark, transfers left
  389. words. */
  390. else
  391. {
  392. wordsCanBeRead = (totalWords - transferredWords);
  393. }
  394. i = 0U;
  395. while (i < wordsCanBeRead)
  396. {
  397. data->rxData[transferredWords++] = SDHC_ReadData(base);
  398. i++;
  399. }
  400. return transferredWords;
  401. }
  402. static status_t SDHC_ReadByDataPortBlocking(SDHC_Type *base, sdhc_data_t *data)
  403. {
  404. uint32_t totalWords;
  405. uint32_t transferredWords = 0U;
  406. status_t error = kStatus_Success;
  407. /*
  408. * Add non aligned access support ,user need make sure your buffer size is big
  409. * enough to hold the data,in other words,user need make sure the buffer size
  410. * is 4 byte aligned
  411. */
  412. if (data->blockSize % sizeof(uint32_t) != 0U)
  413. {
  414. data->blockSize +=
  415. sizeof(uint32_t) - (data->blockSize % sizeof(uint32_t)); /* make the block size as word-aligned */
  416. }
  417. totalWords = ((data->blockCount * data->blockSize) / sizeof(uint32_t));
  418. while ((error == kStatus_Success) && (transferredWords < totalWords))
  419. {
  420. while (!(SDHC_GetInterruptStatusFlags(base) & (kSDHC_BufferReadReadyFlag | kSDHC_DataErrorFlag)))
  421. {
  422. }
  423. if (SDHC_GetInterruptStatusFlags(base) & kSDHC_DataErrorFlag)
  424. {
  425. if (!(data->enableIgnoreError))
  426. {
  427. error = kStatus_Fail;
  428. }
  429. }
  430. if (error == kStatus_Success)
  431. {
  432. transferredWords = SDHC_ReadDataPort(base, data, transferredWords);
  433. }
  434. /* clear buffer ready and error */
  435. SDHC_ClearInterruptStatusFlags(base, kSDHC_BufferReadReadyFlag | kSDHC_DataErrorFlag);
  436. }
  437. /* Clear data complete flag after the last read operation. */
  438. SDHC_ClearInterruptStatusFlags(base, kSDHC_DataCompleteFlag | kSDHC_DataErrorFlag);
  439. return error;
  440. }
  441. static uint32_t SDHC_WriteDataPort(SDHC_Type *base, sdhc_data_t *data, uint32_t transferredWords)
  442. {
  443. uint32_t i;
  444. uint32_t totalWords;
  445. uint32_t wordsCanBeWrote; /* Words can be wrote at this time. */
  446. uint32_t writeWatermark = ((base->WML & SDHC_WML_WRWML_MASK) >> SDHC_WML_WRWML_SHIFT);
  447. /*
  448. * Add non aligned access support ,user need make sure your buffer size is big
  449. * enough to hold the data,in other words,user need make sure the buffer size
  450. * is 4 byte aligned
  451. */
  452. if (data->blockSize % sizeof(uint32_t) != 0U)
  453. {
  454. data->blockSize +=
  455. sizeof(uint32_t) - (data->blockSize % sizeof(uint32_t)); /* make the block size as word-aligned */
  456. }
  457. totalWords = ((data->blockCount * data->blockSize) / sizeof(uint32_t));
  458. /* If watermark level is equal or bigger than totalWords, transfers totalWords data.*/
  459. if (writeWatermark >= totalWords)
  460. {
  461. wordsCanBeWrote = totalWords;
  462. }
  463. /* If watermark level is less than totalWords and left words to be sent is equal or bigger than watermark,
  464. transfers watermark level words. */
  465. else if ((writeWatermark < totalWords) && ((totalWords - transferredWords) >= writeWatermark))
  466. {
  467. wordsCanBeWrote = writeWatermark;
  468. }
  469. /* If watermark level is less than totalWords and left words to be sent is less than watermark, transfers left
  470. words. */
  471. else
  472. {
  473. wordsCanBeWrote = (totalWords - transferredWords);
  474. }
  475. i = 0U;
  476. while (i < wordsCanBeWrote)
  477. {
  478. SDHC_WriteData(base, data->txData[transferredWords++]);
  479. i++;
  480. }
  481. return transferredWords;
  482. }
  483. static status_t SDHC_WriteByDataPortBlocking(SDHC_Type *base, sdhc_data_t *data)
  484. {
  485. uint32_t totalWords;
  486. uint32_t transferredWords = 0U;
  487. status_t error = kStatus_Success;
  488. /*
  489. * Add non aligned access support ,user need make sure your buffer size is big
  490. * enough to hold the data,in other words,user need make sure the buffer size
  491. * is 4 byte aligned
  492. */
  493. if (data->blockSize % sizeof(uint32_t) != 0U)
  494. {
  495. data->blockSize +=
  496. sizeof(uint32_t) - (data->blockSize % sizeof(uint32_t)); /* make the block size as word-aligned */
  497. }
  498. totalWords = (data->blockCount * data->blockSize) / sizeof(uint32_t);
  499. while ((error == kStatus_Success) && (transferredWords < totalWords))
  500. {
  501. while (!(SDHC_GetInterruptStatusFlags(base) & (kSDHC_BufferWriteReadyFlag | kSDHC_DataErrorFlag)))
  502. {
  503. }
  504. if (SDHC_GetInterruptStatusFlags(base) & kSDHC_DataErrorFlag)
  505. {
  506. if (!(data->enableIgnoreError))
  507. {
  508. error = kStatus_Fail;
  509. }
  510. }
  511. if (error == kStatus_Success)
  512. {
  513. transferredWords = SDHC_WriteDataPort(base, data, transferredWords);
  514. }
  515. /* Clear buffer enable flag to trigger transfer. Clear error flag when SDHC encounter error. */
  516. SDHC_ClearInterruptStatusFlags(base, (kSDHC_BufferWriteReadyFlag | kSDHC_DataErrorFlag));
  517. }
  518. /* Wait write data complete or data transfer error after the last writing operation. */
  519. while (!(SDHC_GetInterruptStatusFlags(base) & (kSDHC_DataCompleteFlag | kSDHC_DataErrorFlag)))
  520. {
  521. }
  522. if (SDHC_GetInterruptStatusFlags(base) & kSDHC_DataErrorFlag)
  523. {
  524. if (!(data->enableIgnoreError))
  525. {
  526. error = kStatus_Fail;
  527. }
  528. }
  529. SDHC_ClearInterruptStatusFlags(base, (kSDHC_DataCompleteFlag | kSDHC_DataErrorFlag));
  530. return error;
  531. }
  532. static status_t SDHC_SendCommandBlocking(SDHC_Type *base, sdhc_command_t *command)
  533. {
  534. status_t error = kStatus_Success;
  535. /* Wait command complete or SDHC encounters error. */
  536. while (!(SDHC_GetInterruptStatusFlags(base) & (kSDHC_CommandCompleteFlag | kSDHC_CommandErrorFlag)))
  537. {
  538. }
  539. if (SDHC_GetInterruptStatusFlags(base) & kSDHC_CommandErrorFlag)
  540. {
  541. error = kStatus_Fail;
  542. }
  543. /* Receive response when command completes successfully. */
  544. if (error == kStatus_Success)
  545. {
  546. error = SDHC_ReceiveCommandResponse(base, command);
  547. }
  548. SDHC_ClearInterruptStatusFlags(base, (kSDHC_CommandCompleteFlag | kSDHC_CommandErrorFlag));
  549. return error;
  550. }
  551. static status_t SDHC_TransferByDataPortBlocking(SDHC_Type *base, sdhc_data_t *data)
  552. {
  553. status_t error = kStatus_Success;
  554. if (data->rxData)
  555. {
  556. error = SDHC_ReadByDataPortBlocking(base, data);
  557. }
  558. else
  559. {
  560. error = SDHC_WriteByDataPortBlocking(base, data);
  561. }
  562. return error;
  563. }
  564. static status_t SDHC_TransferByAdma2Blocking(SDHC_Type *base, sdhc_data_t *data)
  565. {
  566. status_t error = kStatus_Success;
  567. /* Wait data complete or SDHC encounters error. */
  568. while (!(SDHC_GetInterruptStatusFlags(base) & (kSDHC_DataCompleteFlag | kSDHC_DataErrorFlag | kSDHC_DmaErrorFlag)))
  569. {
  570. }
  571. if (SDHC_GetInterruptStatusFlags(base) & (kSDHC_DataErrorFlag | kSDHC_DmaErrorFlag))
  572. {
  573. if (!(data->enableIgnoreError))
  574. {
  575. error = kStatus_Fail;
  576. }
  577. }
  578. SDHC_ClearInterruptStatusFlags(
  579. base, (kSDHC_DataCompleteFlag | kSDHC_DmaCompleteFlag | kSDHC_DataErrorFlag | kSDHC_DmaErrorFlag));
  580. return error;
  581. }
  582. #if defined FSL_SDHC_ENABLE_ADMA1
  583. #define SDHC_TransferByAdma1Blocking(base, data) SDHC_TransferByAdma2Blocking(base, data)
  584. #endif /* FSL_SDHC_ENABLE_ADMA1 */
  585. static status_t SDHC_TransferDataBlocking(sdhc_dma_mode_t dmaMode, SDHC_Type *base, sdhc_data_t *data)
  586. {
  587. status_t error = kStatus_Success;
  588. switch (dmaMode)
  589. {
  590. case kSDHC_DmaModeNo:
  591. error = SDHC_TransferByDataPortBlocking(base, data);
  592. break;
  593. #if defined FSL_SDHC_ENABLE_ADMA1
  594. case kSDHC_DmaModeAdma1:
  595. error = SDHC_TransferByAdma1Blocking(base, data);
  596. break;
  597. #endif /* FSL_SDHC_ENABLE_ADMA1 */
  598. case kSDHC_DmaModeAdma2:
  599. error = SDHC_TransferByAdma2Blocking(base, data);
  600. break;
  601. default:
  602. error = kStatus_InvalidArgument;
  603. break;
  604. }
  605. return error;
  606. }
  607. static void SDHC_TransferHandleCardDetect(SDHC_Type *base, sdhc_handle_t *handle, uint32_t interruptFlags)
  608. {
  609. if (interruptFlags & kSDHC_CardInsertionFlag)
  610. {
  611. if (handle->callback.CardInserted)
  612. {
  613. handle->callback.CardInserted(base, handle->userData);
  614. }
  615. }
  616. else
  617. {
  618. if (handle->callback.CardRemoved)
  619. {
  620. handle->callback.CardRemoved(base, handle->userData);
  621. }
  622. }
  623. }
  624. static void SDHC_TransferHandleCommand(SDHC_Type *base, sdhc_handle_t *handle, uint32_t interruptFlags)
  625. {
  626. assert(handle->command);
  627. if ((interruptFlags & kSDHC_CommandErrorFlag) && (!(handle->data)) && (handle->callback.TransferComplete))
  628. {
  629. handle->callback.TransferComplete(base, handle, kStatus_SDHC_SendCommandFailed, handle->userData);
  630. }
  631. else
  632. {
  633. /* Receive response */
  634. SDHC_ReceiveCommandResponse(base, handle->command);
  635. if ((!(handle->data)) && (handle->callback.TransferComplete))
  636. {
  637. handle->callback.TransferComplete(base, handle, kStatus_Success, handle->userData);
  638. }
  639. }
  640. }
  641. static void SDHC_TransferHandleData(SDHC_Type *base, sdhc_handle_t *handle, uint32_t interruptFlags)
  642. {
  643. assert(handle->data);
  644. if ((!(handle->data->enableIgnoreError)) && (interruptFlags & (kSDHC_DataErrorFlag | kSDHC_DmaErrorFlag)) &&
  645. (handle->callback.TransferComplete))
  646. {
  647. handle->callback.TransferComplete(base, handle, kStatus_SDHC_TransferDataFailed, handle->userData);
  648. }
  649. else
  650. {
  651. if (interruptFlags & kSDHC_BufferReadReadyFlag)
  652. {
  653. handle->transferredWords = SDHC_ReadDataPort(base, handle->data, handle->transferredWords);
  654. }
  655. else if (interruptFlags & kSDHC_BufferWriteReadyFlag)
  656. {
  657. handle->transferredWords = SDHC_WriteDataPort(base, handle->data, handle->transferredWords);
  658. }
  659. else
  660. {
  661. }
  662. if ((interruptFlags & kSDHC_DataCompleteFlag) && (handle->callback.TransferComplete))
  663. {
  664. handle->callback.TransferComplete(base, handle, kStatus_Success, handle->userData);
  665. }
  666. else
  667. {
  668. /* Do nothing when DMA complete flag is set. Wait until data complete flag is set. */
  669. }
  670. }
  671. }
  672. static void SDHC_TransferHandleSdioInterrupt(SDHC_Type *base, sdhc_handle_t *handle)
  673. {
  674. if (handle->callback.SdioInterrupt)
  675. {
  676. handle->callback.SdioInterrupt(base, handle->userData);
  677. }
  678. }
  679. static void SDHC_TransferHandleSdioBlockGap(SDHC_Type *base, sdhc_handle_t *handle)
  680. {
  681. if (handle->callback.SdioBlockGap)
  682. {
  683. handle->callback.SdioBlockGap(base, handle->userData);
  684. }
  685. }
  686. void SDHC_Init(SDHC_Type *base, const sdhc_config_t *config)
  687. {
  688. assert(config);
  689. #if !defined FSL_SDHC_ENABLE_ADMA1
  690. assert(config->dmaMode != kSDHC_DmaModeAdma1);
  691. #endif /* FSL_SDHC_ENABLE_ADMA1 */
  692. assert((config->writeWatermarkLevel >= 1U) && (config->writeWatermarkLevel <= 128U));
  693. assert((config->readWatermarkLevel >= 1U) && (config->readWatermarkLevel <= 128U));
  694. uint32_t proctl;
  695. uint32_t wml;
  696. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  697. /* Enable SDHC clock. */
  698. CLOCK_EnableClock(s_sdhcClock[SDHC_GetInstance(base)]);
  699. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  700. /* Reset SDHC. */
  701. SDHC_Reset(base, kSDHC_ResetAll, 100);
  702. proctl = base->PROCTL;
  703. wml = base->WML;
  704. proctl &= ~(SDHC_PROCTL_D3CD_MASK | SDHC_PROCTL_EMODE_MASK | SDHC_PROCTL_DMAS_MASK);
  705. /* Set DAT3 as card detection pin */
  706. if (config->cardDetectDat3)
  707. {
  708. proctl |= SDHC_PROCTL_D3CD_MASK;
  709. }
  710. /* Endian mode and DMA mode */
  711. proctl |= (SDHC_PROCTL_EMODE(config->endianMode) | SDHC_PROCTL_DMAS(config->dmaMode));
  712. /* Watermark level */
  713. wml &= ~(SDHC_WML_RDWML_MASK | SDHC_WML_WRWML_MASK);
  714. wml |= (SDHC_WML_RDWML(config->readWatermarkLevel) | SDHC_WML_WRWML(config->writeWatermarkLevel));
  715. base->WML = wml;
  716. base->PROCTL = proctl;
  717. /* Disable all clock auto gated off feature because of DAT0 line logic(card buffer full status) can't be updated
  718. correctly when clock auto gated off is enabled. */
  719. base->SYSCTL |= (SDHC_SYSCTL_PEREN_MASK | SDHC_SYSCTL_HCKEN_MASK | SDHC_SYSCTL_IPGEN_MASK);
  720. /* Enable interrupt status but doesn't enable interrupt signal. */
  721. SDHC_SetTransferInterrupt(base, false);
  722. }
  723. void SDHC_Deinit(SDHC_Type *base)
  724. {
  725. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  726. /* Disable clock. */
  727. CLOCK_DisableClock(s_sdhcClock[SDHC_GetInstance(base)]);
  728. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  729. }
  730. bool SDHC_Reset(SDHC_Type *base, uint32_t mask, uint32_t timeout)
  731. {
  732. base->SYSCTL |= (mask & (SDHC_SYSCTL_RSTA_MASK | SDHC_SYSCTL_RSTC_MASK | SDHC_SYSCTL_RSTD_MASK));
  733. /* Delay some time to wait reset success. */
  734. while ((base->SYSCTL & mask))
  735. {
  736. if (!timeout)
  737. {
  738. break;
  739. }
  740. timeout--;
  741. }
  742. return ((!timeout) ? false : true);
  743. }
  744. void SDHC_GetCapability(SDHC_Type *base, sdhc_capability_t *capability)
  745. {
  746. assert(capability);
  747. uint32_t htCapability;
  748. uint32_t hostVer;
  749. uint32_t maxBlockLength;
  750. hostVer = base->HOSTVER;
  751. htCapability = base->HTCAPBLT;
  752. /* Get the capability of SDHC. */
  753. capability->specVersion = ((hostVer & SDHC_HOSTVER_SVN_MASK) >> SDHC_HOSTVER_SVN_SHIFT);
  754. capability->vendorVersion = ((hostVer & SDHC_HOSTVER_VVN_MASK) >> SDHC_HOSTVER_VVN_SHIFT);
  755. maxBlockLength = ((htCapability & SDHC_HTCAPBLT_MBL_MASK) >> SDHC_HTCAPBLT_MBL_SHIFT);
  756. capability->maxBlockLength = (512U << maxBlockLength);
  757. /* Other attributes not in HTCAPBLT register. */
  758. capability->maxBlockCount = SDHC_MAX_BLOCK_COUNT;
  759. capability->flags = (htCapability & (kSDHC_SupportAdmaFlag | kSDHC_SupportHighSpeedFlag | kSDHC_SupportDmaFlag |
  760. kSDHC_SupportSuspendResumeFlag | kSDHC_SupportV330Flag));
  761. #if defined FSL_FEATURE_SDHC_HAS_V300_SUPPORT && FSL_FEATURE_SDHC_HAS_V300_SUPPORT
  762. capability->flags |= (htCapability & kSDHC_SupportV300Flag);
  763. #endif
  764. #if defined FSL_FEATURE_SDHC_HAS_V180_SUPPORT && FSL_FEATURE_SDHC_HAS_V180_SUPPORT
  765. capability->flags |= (htCapability & kSDHC_SupportV180Flag);
  766. #endif
  767. /* eSDHC on all kinetis boards will support 4/8 bit data bus width. */
  768. capability->flags |= (kSDHC_Support4BitFlag | kSDHC_Support8BitFlag);
  769. }
  770. uint32_t SDHC_SetSdClock(SDHC_Type *base, uint32_t srcClock_Hz, uint32_t busClock_Hz)
  771. {
  772. assert(srcClock_Hz != 0U);
  773. assert((busClock_Hz != 0U) && (busClock_Hz <= srcClock_Hz));
  774. uint32_t totalDiv = 0U;
  775. uint32_t divisor = 0U;
  776. uint32_t prescaler = 0U;
  777. uint32_t sysctl = 0U;
  778. uint32_t nearestFrequency = 0U;
  779. /* calucate total divisor first */
  780. totalDiv = srcClock_Hz / busClock_Hz;
  781. if (totalDiv != 0U)
  782. {
  783. /* calucate the divisor (srcClock_Hz / divisor) <= busClock_Hz */
  784. if ((srcClock_Hz / totalDiv) > busClock_Hz)
  785. {
  786. totalDiv++;
  787. }
  788. /* divide the total divisor to div and prescaler */
  789. if (totalDiv > SDHC_MAX_DVS)
  790. {
  791. prescaler = totalDiv / SDHC_MAX_DVS;
  792. /* prescaler must be a value which equal 2^n and smaller than SDHC_MAX_CLKFS */
  793. while (((SDHC_MAX_CLKFS % prescaler) != 0U) || (prescaler == 1U))
  794. {
  795. prescaler++;
  796. }
  797. /* calucate the divisor */
  798. divisor = totalDiv / prescaler;
  799. /* fine tuning the divisor until divisor * prescaler >= totalDiv */
  800. while ((divisor * prescaler) < totalDiv)
  801. {
  802. divisor++;
  803. }
  804. nearestFrequency = srcClock_Hz / divisor / prescaler;
  805. }
  806. else
  807. {
  808. divisor = totalDiv;
  809. prescaler = 0U;
  810. nearestFrequency = srcClock_Hz / divisor;
  811. }
  812. }
  813. /* in this condition , srcClock_Hz = busClock_Hz, */
  814. else
  815. {
  816. /* total divider = 1U */
  817. divisor = 0U;
  818. prescaler = 0U;
  819. nearestFrequency = srcClock_Hz;
  820. }
  821. /* calucate the value write to register */
  822. if (divisor != 0U)
  823. {
  824. SDHC_PREV_DVS(divisor);
  825. }
  826. /* calucate the value write to register */
  827. if (prescaler != 0U)
  828. {
  829. SDHC_PREV_CLKFS(prescaler);
  830. }
  831. /* Disable SD clock. It should be disabled before changing the SD clock frequency.*/
  832. base->SYSCTL &= ~SDHC_SYSCTL_SDCLKEN_MASK;
  833. /* Set the SD clock frequency divisor, SD clock frequency select, data timeout counter value. */
  834. sysctl = base->SYSCTL;
  835. sysctl &= ~(SDHC_SYSCTL_DVS_MASK | SDHC_SYSCTL_SDCLKFS_MASK | SDHC_SYSCTL_DTOCV_MASK);
  836. sysctl |= (SDHC_SYSCTL_DVS(divisor) | SDHC_SYSCTL_SDCLKFS(prescaler) | SDHC_SYSCTL_DTOCV(0xEU));
  837. base->SYSCTL = sysctl;
  838. /* Wait until the SD clock is stable. */
  839. while (!(base->PRSSTAT & SDHC_PRSSTAT_SDSTB_MASK))
  840. {
  841. }
  842. /* Enable the SD clock. */
  843. base->SYSCTL |= SDHC_SYSCTL_SDCLKEN_MASK;
  844. return nearestFrequency;
  845. }
  846. bool SDHC_SetCardActive(SDHC_Type *base, uint32_t timeout)
  847. {
  848. base->SYSCTL |= SDHC_SYSCTL_INITA_MASK;
  849. /* Delay some time to wait card become active state. */
  850. while (base->SYSCTL & SDHC_SYSCTL_INITA_MASK)
  851. {
  852. if (!timeout)
  853. {
  854. break;
  855. }
  856. timeout--;
  857. }
  858. return ((!timeout) ? false : true);
  859. }
  860. void SDHC_SetTransferConfig(SDHC_Type *base, const sdhc_transfer_config_t *config)
  861. {
  862. assert(config);
  863. assert(config->dataBlockSize <= (SDHC_BLKATTR_BLKSIZE_MASK >> SDHC_BLKATTR_BLKSIZE_SHIFT));
  864. assert(config->dataBlockCount <= (SDHC_BLKATTR_BLKCNT_MASK >> SDHC_BLKATTR_BLKCNT_SHIFT));
  865. base->BLKATTR = ((base->BLKATTR & ~(SDHC_BLKATTR_BLKSIZE_MASK | SDHC_BLKATTR_BLKCNT_MASK)) |
  866. (SDHC_BLKATTR_BLKSIZE(config->dataBlockSize) | SDHC_BLKATTR_BLKCNT(config->dataBlockCount)));
  867. base->CMDARG = config->commandArgument;
  868. base->XFERTYP = (((config->commandIndex << SDHC_XFERTYP_CMDINX_SHIFT) & SDHC_XFERTYP_CMDINX_MASK) |
  869. (config->flags & (SDHC_XFERTYP_DMAEN_MASK | SDHC_XFERTYP_MSBSEL_MASK | SDHC_XFERTYP_DPSEL_MASK |
  870. SDHC_XFERTYP_CMDTYP_MASK | SDHC_XFERTYP_BCEN_MASK | SDHC_XFERTYP_CICEN_MASK |
  871. SDHC_XFERTYP_CCCEN_MASK | SDHC_XFERTYP_RSPTYP_MASK | SDHC_XFERTYP_DTDSEL_MASK |
  872. SDHC_XFERTYP_AC12EN_MASK)));
  873. }
  874. void SDHC_EnableSdioControl(SDHC_Type *base, uint32_t mask, bool enable)
  875. {
  876. uint32_t proctl = base->PROCTL;
  877. uint32_t vendor = base->VENDOR;
  878. if (enable)
  879. {
  880. if (mask & kSDHC_StopAtBlockGapFlag)
  881. {
  882. proctl |= SDHC_PROCTL_SABGREQ_MASK;
  883. }
  884. if (mask & kSDHC_ReadWaitControlFlag)
  885. {
  886. proctl |= SDHC_PROCTL_RWCTL_MASK;
  887. }
  888. if (mask & kSDHC_InterruptAtBlockGapFlag)
  889. {
  890. proctl |= SDHC_PROCTL_IABG_MASK;
  891. }
  892. if (mask & kSDHC_ExactBlockNumberReadFlag)
  893. {
  894. vendor |= SDHC_VENDOR_EXBLKNU_MASK;
  895. }
  896. }
  897. else
  898. {
  899. if (mask & kSDHC_StopAtBlockGapFlag)
  900. {
  901. proctl &= ~SDHC_PROCTL_SABGREQ_MASK;
  902. }
  903. if (mask & kSDHC_ReadWaitControlFlag)
  904. {
  905. proctl &= ~SDHC_PROCTL_RWCTL_MASK;
  906. }
  907. if (mask & kSDHC_InterruptAtBlockGapFlag)
  908. {
  909. proctl &= ~SDHC_PROCTL_IABG_MASK;
  910. }
  911. if (mask & kSDHC_ExactBlockNumberReadFlag)
  912. {
  913. vendor &= ~SDHC_VENDOR_EXBLKNU_MASK;
  914. }
  915. }
  916. base->PROCTL = proctl;
  917. base->VENDOR = vendor;
  918. }
  919. void SDHC_SetMmcBootConfig(SDHC_Type *base, const sdhc_boot_config_t *config)
  920. {
  921. assert(config);
  922. assert(config->ackTimeoutCount <= (SDHC_MMCBOOT_DTOCVACK_MASK >> SDHC_MMCBOOT_DTOCVACK_SHIFT));
  923. assert(config->blockCount <= (SDHC_MMCBOOT_BOOTBLKCNT_MASK >> SDHC_MMCBOOT_BOOTBLKCNT_SHIFT));
  924. uint32_t mmcboot = 0U;
  925. mmcboot = (SDHC_MMCBOOT_DTOCVACK(config->ackTimeoutCount) | SDHC_MMCBOOT_BOOTMODE(config->bootMode) |
  926. SDHC_MMCBOOT_BOOTBLKCNT(config->blockCount));
  927. if (config->enableBootAck)
  928. {
  929. mmcboot |= SDHC_MMCBOOT_BOOTACK_MASK;
  930. }
  931. if (config->enableBoot)
  932. {
  933. mmcboot |= SDHC_MMCBOOT_BOOTEN_MASK;
  934. }
  935. if (config->enableAutoStopAtBlockGap)
  936. {
  937. mmcboot |= SDHC_MMCBOOT_AUTOSABGEN_MASK;
  938. }
  939. base->MMCBOOT = mmcboot;
  940. }
  941. status_t SDHC_SetAdmaTableConfig(SDHC_Type *base,
  942. sdhc_dma_mode_t dmaMode,
  943. uint32_t *table,
  944. uint32_t tableWords,
  945. const uint32_t *data,
  946. uint32_t dataBytes)
  947. {
  948. status_t error = kStatus_Success;
  949. const uint32_t *startAddress = data;
  950. uint32_t entries;
  951. uint32_t i;
  952. #if defined FSL_SDHC_ENABLE_ADMA1
  953. sdhc_adma1_descriptor_t *adma1EntryAddress;
  954. #endif
  955. sdhc_adma2_descriptor_t *adma2EntryAddress;
  956. if ((((!table) || (!tableWords)) && ((dmaMode == kSDHC_DmaModeAdma1) || (dmaMode == kSDHC_DmaModeAdma2))) ||
  957. (!data) || (!dataBytes)
  958. #if !defined FSL_SDHC_ENABLE_ADMA1
  959. || (dmaMode == kSDHC_DmaModeAdma1)
  960. #endif
  961. )
  962. {
  963. error = kStatus_InvalidArgument;
  964. }
  965. else if (((dmaMode == kSDHC_DmaModeAdma2) && (((uint32_t)startAddress % SDHC_ADMA2_LENGTH_ALIGN) != 0U))
  966. #if defined FSL_SDHC_ENABLE_ADMA1
  967. || ((dmaMode == kSDHC_DmaModeAdma1) && (((uint32_t)startAddress % SDHC_ADMA1_LENGTH_ALIGN) != 0U))
  968. #endif
  969. )
  970. {
  971. error = kStatus_SDHC_DMADataBufferAddrNotAlign;
  972. }
  973. else
  974. {
  975. switch (dmaMode)
  976. {
  977. case kSDHC_DmaModeNo:
  978. break;
  979. #if defined FSL_SDHC_ENABLE_ADMA1
  980. case kSDHC_DmaModeAdma1:
  981. /*
  982. * Add non aligned access support ,user need make sure your buffer size is big
  983. * enough to hold the data,in other words,user need make sure the buffer size
  984. * is 4 byte aligned
  985. */
  986. if (dataBytes % sizeof(uint32_t) != 0U)
  987. {
  988. dataBytes +=
  989. sizeof(uint32_t) - (dataBytes % sizeof(uint32_t)); /* make the data length as word-aligned */
  990. }
  991. /* Check if ADMA descriptor's number is enough. */
  992. entries = ((dataBytes / SDHC_ADMA1_DESCRIPTOR_MAX_LENGTH_PER_ENTRY) + 1U);
  993. /* ADMA1 needs two descriptors to finish a transfer */
  994. entries <<= 1U;
  995. if (entries > ((tableWords * sizeof(uint32_t)) / sizeof(sdhc_adma1_descriptor_t)))
  996. {
  997. error = kStatus_OutOfRange;
  998. }
  999. else
  1000. {
  1001. adma1EntryAddress = (sdhc_adma1_descriptor_t *)(table);
  1002. for (i = 0U; i < entries; i += 2U)
  1003. {
  1004. /* Each descriptor for ADMA1 is 32-bit in length */
  1005. if ((dataBytes - sizeof(uint32_t) * (startAddress - data)) <=
  1006. SDHC_ADMA1_DESCRIPTOR_MAX_LENGTH_PER_ENTRY)
  1007. {
  1008. /* The last piece of data, setting end flag in descriptor */
  1009. adma1EntryAddress[i] = ((uint32_t)(dataBytes - sizeof(uint32_t) * (startAddress - data))
  1010. << SDHC_ADMA1_DESCRIPTOR_LENGTH_SHIFT);
  1011. adma1EntryAddress[i] |= kSDHC_Adma1DescriptorTypeSetLength;
  1012. adma1EntryAddress[i + 1U] =
  1013. ((uint32_t)(startAddress) << SDHC_ADMA1_DESCRIPTOR_ADDRESS_SHIFT);
  1014. adma1EntryAddress[i + 1U] |=
  1015. (kSDHC_Adma1DescriptorTypeTransfer | kSDHC_Adma1DescriptorEndFlag);
  1016. }
  1017. else
  1018. {
  1019. adma1EntryAddress[i] = ((uint32_t)SDHC_ADMA1_DESCRIPTOR_MAX_LENGTH_PER_ENTRY
  1020. << SDHC_ADMA1_DESCRIPTOR_LENGTH_SHIFT);
  1021. adma1EntryAddress[i] |= kSDHC_Adma1DescriptorTypeSetLength;
  1022. adma1EntryAddress[i + 1U] =
  1023. ((uint32_t)(startAddress) << SDHC_ADMA1_DESCRIPTOR_ADDRESS_SHIFT);
  1024. adma1EntryAddress[i + 1U] |= kSDHC_Adma1DescriptorTypeTransfer;
  1025. startAddress += SDHC_ADMA1_DESCRIPTOR_MAX_LENGTH_PER_ENTRY / sizeof(uint32_t);
  1026. }
  1027. }
  1028. /* When use ADMA, disable simple DMA */
  1029. base->DSADDR = 0U;
  1030. base->ADSADDR = (uint32_t)table;
  1031. /* disable the buffer ready flag in DMA mode */
  1032. SDHC_DisableInterruptSignal(base, kSDHC_BufferReadReadyFlag | kSDHC_BufferWriteReadyFlag);
  1033. SDHC_DisableInterruptStatus(base, kSDHC_BufferReadReadyFlag | kSDHC_BufferWriteReadyFlag);
  1034. }
  1035. break;
  1036. #endif /* FSL_SDHC_ENABLE_ADMA1 */
  1037. case kSDHC_DmaModeAdma2:
  1038. /*
  1039. * Add non aligned access support ,user need make sure your buffer size is big
  1040. * enough to hold the data,in other words,user need make sure the buffer size
  1041. * is 4 byte aligned
  1042. */
  1043. if (dataBytes % sizeof(uint32_t) != 0U)
  1044. {
  1045. dataBytes +=
  1046. sizeof(uint32_t) - (dataBytes % sizeof(uint32_t)); /* make the data length as word-aligned */
  1047. }
  1048. /* Check if ADMA descriptor's number is enough. */
  1049. entries = ((dataBytes / SDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY) + 1U);
  1050. if (entries > ((tableWords * sizeof(uint32_t)) / sizeof(sdhc_adma2_descriptor_t)))
  1051. {
  1052. error = kStatus_OutOfRange;
  1053. }
  1054. else
  1055. {
  1056. adma2EntryAddress = (sdhc_adma2_descriptor_t *)(table);
  1057. for (i = 0U; i < entries; i++)
  1058. {
  1059. /* Each descriptor for ADMA2 is 64-bit in length */
  1060. if ((dataBytes - sizeof(uint32_t) * (startAddress - data)) <=
  1061. SDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY)
  1062. {
  1063. /* The last piece of data, setting end flag in descriptor */
  1064. adma2EntryAddress[i].address = startAddress;
  1065. adma2EntryAddress[i].attribute = ((dataBytes - sizeof(uint32_t) * (startAddress - data))
  1066. << SDHC_ADMA2_DESCRIPTOR_LENGTH_SHIFT);
  1067. adma2EntryAddress[i].attribute |=
  1068. (kSDHC_Adma2DescriptorTypeTransfer | kSDHC_Adma2DescriptorEndFlag);
  1069. }
  1070. else
  1071. {
  1072. adma2EntryAddress[i].address = startAddress;
  1073. adma2EntryAddress[i].attribute =
  1074. (((SDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY / sizeof(uint32_t)) * sizeof(uint32_t))
  1075. << SDHC_ADMA2_DESCRIPTOR_LENGTH_SHIFT);
  1076. adma2EntryAddress[i].attribute |= kSDHC_Adma2DescriptorTypeTransfer;
  1077. startAddress += (SDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY / sizeof(uint32_t));
  1078. }
  1079. }
  1080. /* When use ADMA, disable simple DMA */
  1081. base->DSADDR = 0U;
  1082. base->ADSADDR = (uint32_t)table;
  1083. /* disable the buffer read flag in DMA mode */
  1084. SDHC_DisableInterruptSignal(base, kSDHC_BufferReadReadyFlag | kSDHC_BufferWriteReadyFlag);
  1085. SDHC_DisableInterruptStatus(base, kSDHC_BufferReadReadyFlag | kSDHC_BufferWriteReadyFlag);
  1086. }
  1087. break;
  1088. default:
  1089. break;
  1090. }
  1091. }
  1092. return error;
  1093. }
  1094. status_t SDHC_TransferBlocking(SDHC_Type *base, uint32_t *admaTable, uint32_t admaTableWords, sdhc_transfer_t *transfer)
  1095. {
  1096. assert(transfer);
  1097. status_t error = kStatus_Success;
  1098. sdhc_dma_mode_t dmaMode = (sdhc_dma_mode_t)((base->PROCTL & SDHC_PROCTL_DMAS_MASK) >> SDHC_PROCTL_DMAS_SHIFT);
  1099. sdhc_command_t *command = transfer->command;
  1100. sdhc_data_t *data = transfer->data;
  1101. /* make sure the cmd/block count is valid */
  1102. if ((!command) || (data && (data->blockCount > SDHC_MAX_BLOCK_COUNT)))
  1103. {
  1104. return kStatus_InvalidArgument;
  1105. }
  1106. /* Wait until command/data bus out of busy status. */
  1107. while (SDHC_GetPresentStatusFlags(base) & kSDHC_CommandInhibitFlag)
  1108. {
  1109. }
  1110. while (data && (SDHC_GetPresentStatusFlags(base) & kSDHC_DataInhibitFlag))
  1111. {
  1112. }
  1113. /* Update ADMA descriptor table according to different DMA mode(no DMA, ADMA1, ADMA2).*/
  1114. if (data && (NULL != admaTable))
  1115. {
  1116. error =
  1117. SDHC_SetAdmaTableConfig(base, dmaMode, admaTable, admaTableWords,
  1118. (data->rxData ? data->rxData : data->txData), (data->blockCount * data->blockSize));
  1119. /* in this situation , we disable the DMA instead of polling transfer mode */
  1120. if (error == kStatus_SDHC_DMADataBufferAddrNotAlign)
  1121. {
  1122. dmaMode = kSDHC_DmaModeNo;
  1123. SDHC_EnableInterruptStatus(base, kSDHC_BufferReadReadyFlag | kSDHC_BufferWriteReadyFlag);
  1124. }
  1125. else if (error != kStatus_Success)
  1126. {
  1127. return error;
  1128. }
  1129. else
  1130. {
  1131. }
  1132. }
  1133. /* Send command and receive data. */
  1134. SDHC_StartTransfer(base, command, data, dmaMode);
  1135. if (kStatus_Success != SDHC_SendCommandBlocking(base, command))
  1136. {
  1137. return kStatus_SDHC_SendCommandFailed;
  1138. }
  1139. else if (data && (kStatus_Success != SDHC_TransferDataBlocking(dmaMode, base, data)))
  1140. {
  1141. return kStatus_SDHC_TransferDataFailed;
  1142. }
  1143. else
  1144. {
  1145. }
  1146. return kStatus_Success;
  1147. }
  1148. void SDHC_TransferCreateHandle(SDHC_Type *base,
  1149. sdhc_handle_t *handle,
  1150. const sdhc_transfer_callback_t *callback,
  1151. void *userData)
  1152. {
  1153. assert(handle);
  1154. assert(callback);
  1155. /* Zero the handle. */
  1156. memset(handle, 0, sizeof(*handle));
  1157. /* Set the callback. */
  1158. handle->callback.CardInserted = callback->CardInserted;
  1159. handle->callback.CardRemoved = callback->CardRemoved;
  1160. handle->callback.SdioInterrupt = callback->SdioInterrupt;
  1161. handle->callback.SdioBlockGap = callback->SdioBlockGap;
  1162. handle->callback.TransferComplete = callback->TransferComplete;
  1163. handle->userData = userData;
  1164. /* Save the handle in global variables to support the double weak mechanism. */
  1165. s_sdhcHandle[SDHC_GetInstance(base)] = handle;
  1166. /* Enable interrupt in NVIC. */
  1167. SDHC_SetTransferInterrupt(base, true);
  1168. /* save IRQ handler */
  1169. s_sdhcIsr = SDHC_TransferHandleIRQ;
  1170. EnableIRQ(s_sdhcIRQ[SDHC_GetInstance(base)]);
  1171. }
  1172. status_t SDHC_TransferNonBlocking(
  1173. SDHC_Type *base, sdhc_handle_t *handle, uint32_t *admaTable, uint32_t admaTableWords, sdhc_transfer_t *transfer)
  1174. {
  1175. assert(transfer);
  1176. sdhc_dma_mode_t dmaMode = (sdhc_dma_mode_t)((base->PROCTL & SDHC_PROCTL_DMAS_MASK) >> SDHC_PROCTL_DMAS_SHIFT);
  1177. status_t error = kStatus_Success;
  1178. sdhc_command_t *command = transfer->command;
  1179. sdhc_data_t *data = transfer->data;
  1180. /* make sure cmd/block count is valid */
  1181. if ((!command) || (data && (data->blockCount > SDHC_MAX_BLOCK_COUNT)))
  1182. {
  1183. return kStatus_InvalidArgument;
  1184. }
  1185. /* Wait until command/data bus out of busy status. */
  1186. if ((SDHC_GetPresentStatusFlags(base) & kSDHC_CommandInhibitFlag) ||
  1187. (data && (SDHC_GetPresentStatusFlags(base) & kSDHC_DataInhibitFlag)))
  1188. {
  1189. return kStatus_SDHC_BusyTransferring;
  1190. }
  1191. /* Update ADMA descriptor table according to different DMA mode(no DMA, ADMA1, ADMA2).*/
  1192. if (data && (NULL != admaTable))
  1193. {
  1194. error =
  1195. SDHC_SetAdmaTableConfig(base, dmaMode, admaTable, admaTableWords,
  1196. (data->rxData ? data->rxData : data->txData), (data->blockCount * data->blockSize));
  1197. /* in this situation , we disable the DMA instead of polling transfer mode */
  1198. if (error == kStatus_SDHC_DMADataBufferAddrNotAlign)
  1199. {
  1200. /* change to polling mode */
  1201. dmaMode = kSDHC_DmaModeNo;
  1202. SDHC_EnableInterruptSignal(base, kSDHC_BufferReadReadyFlag | kSDHC_BufferWriteReadyFlag);
  1203. SDHC_EnableInterruptStatus(base, kSDHC_BufferReadReadyFlag | kSDHC_BufferWriteReadyFlag);
  1204. }
  1205. else if (error != kStatus_Success)
  1206. {
  1207. return error;
  1208. }
  1209. else
  1210. {
  1211. }
  1212. }
  1213. /* Save command and data into handle before transferring. */
  1214. handle->command = command;
  1215. handle->data = data;
  1216. handle->interruptFlags = 0U;
  1217. /* transferredWords will only be updated in ISR when transfer way is DATAPORT. */
  1218. handle->transferredWords = 0U;
  1219. SDHC_StartTransfer(base, command, data, dmaMode);
  1220. return kStatus_Success;
  1221. }
  1222. void SDHC_TransferHandleIRQ(SDHC_Type *base, sdhc_handle_t *handle)
  1223. {
  1224. assert(handle);
  1225. uint32_t interruptFlags;
  1226. interruptFlags = SDHC_GetInterruptStatusFlags(base);
  1227. handle->interruptFlags = interruptFlags;
  1228. if (interruptFlags & kSDHC_CardDetectFlag)
  1229. {
  1230. SDHC_TransferHandleCardDetect(base, handle, (interruptFlags & kSDHC_CardDetectFlag));
  1231. }
  1232. if (interruptFlags & kSDHC_CommandFlag)
  1233. {
  1234. SDHC_TransferHandleCommand(base, handle, (interruptFlags & kSDHC_CommandFlag));
  1235. }
  1236. if (interruptFlags & kSDHC_DataFlag)
  1237. {
  1238. SDHC_TransferHandleData(base, handle, (interruptFlags & kSDHC_DataFlag));
  1239. }
  1240. if (interruptFlags & kSDHC_CardInterruptFlag)
  1241. {
  1242. SDHC_TransferHandleSdioInterrupt(base, handle);
  1243. }
  1244. if (interruptFlags & kSDHC_BlockGapEventFlag)
  1245. {
  1246. SDHC_TransferHandleSdioBlockGap(base, handle);
  1247. }
  1248. SDHC_ClearInterruptStatusFlags(base, interruptFlags);
  1249. }
  1250. #if defined(SDHC)
  1251. void SDHC_DriverIRQHandler(void)
  1252. {
  1253. assert(s_sdhcHandle[0]);
  1254. s_sdhcIsr(SDHC, s_sdhcHandle[0]);
  1255. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1256. exception return operation might vector to incorrect interrupt */
  1257. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1258. __DSB();
  1259. #endif
  1260. }
  1261. #endif