usdhc.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867
  1. /*
  2. * Copyright (c) 2019 NXP
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #define DT_DRV_COMPAT nxp_imx_usdhc
  7. #include <sys/__assert.h>
  8. #include <drivers/disk.h>
  9. #include <drivers/gpio.h>
  10. #include <sys/byteorder.h>
  11. #include <soc.h>
  12. #include <drivers/clock_control.h>
  13. #include "sdmmc_sdhc.h"
  14. #include <logging/log.h>
  15. LOG_MODULE_REGISTER(usdhc, CONFIG_SDMMC_LOG_LEVEL);
  16. enum usdhc_cmd_type {
  17. USDHC_CMD_TYPE_NORMAL = 0U,
  18. /*!< Normal command */
  19. USDHC_CMD_TYPE_SUSPEND = 1U,
  20. /*!< Suspend command */
  21. USDHC_CMD_TYPE_RESUME = 2U,
  22. /*!< Resume command */
  23. USDHC_CMD_TYPE_ABORT = 3U,
  24. /*!< Abort command */
  25. USDHC_CMD_TYPE_EMPTY = 4U,
  26. /*!< Empty command */
  27. };
  28. enum usdhc_status_flag {
  29. USDHC_CMD_INHIBIT_FLAG =
  30. USDHC_PRES_STATE_CIHB_MASK,
  31. /*!< Command inhibit */
  32. USDHC_DATA_INHIBIT_FLAG =
  33. USDHC_PRES_STATE_CDIHB_MASK,
  34. /*!< Data inhibit */
  35. USDHC_DATA_LINE_ACTIVE_FLAG =
  36. USDHC_PRES_STATE_DLA_MASK,
  37. /*!< Data line active */
  38. USDHC_SD_CLK_STATUS_FLAG =
  39. USDHC_PRES_STATE_SDSTB_MASK,
  40. /*!< SD bus clock stable */
  41. USDHC_WRITE_ACTIVE_FLAG =
  42. USDHC_PRES_STATE_WTA_MASK,
  43. /*!< Write transfer active */
  44. USDHC_READ_ACTIVE_FLAG =
  45. USDHC_PRES_STATE_RTA_MASK,
  46. /*!< Read transfer active */
  47. USDHC_BUF_WRITE_ENABLE_FLAG =
  48. USDHC_PRES_STATE_BWEN_MASK,
  49. /*!< Buffer write enable */
  50. USDHC_BUF_READ_ENABLE_FLAG =
  51. USDHC_PRES_STATE_BREN_MASK,
  52. /*!< Buffer read enable */
  53. USDHC_RETUNING_REQ_FLAG =
  54. USDHC_PRES_STATE_RTR_MASK,
  55. /*!< re-tuning request flag ,only used for SDR104 mode */
  56. USDHC_DELAY_SETTING_DONE_FLAG =
  57. USDHC_PRES_STATE_TSCD_MASK,
  58. /*!< delay setting finished flag */
  59. USDHC_CARD_INSERTED_FLAG =
  60. USDHC_PRES_STATE_CINST_MASK,
  61. /*!< Card inserted */
  62. USDHC_CMD_LINE_LEVEL_FLAG =
  63. USDHC_PRES_STATE_CLSL_MASK,
  64. /*!< Command line signal level */
  65. USDHC_DATA0_LINE_LEVEL_FLAG =
  66. 1U << USDHC_PRES_STATE_DLSL_SHIFT,
  67. /*!< Data0 line signal level */
  68. USDHC_DATA1_LINE_LEVEL_FLAG =
  69. 1U << (USDHC_PRES_STATE_DLSL_SHIFT + 1U),
  70. /*!< Data1 line signal level */
  71. USDHC_DATA2_LINE_LEVEL_FLAG =
  72. 1U << (USDHC_PRES_STATE_DLSL_SHIFT + 2U),
  73. /*!< Data2 line signal level */
  74. USDHC_DATA3_LINE_LEVEL_FLAG =
  75. 1U << (USDHC_PRES_STATE_DLSL_SHIFT + 3U),
  76. /*!< Data3 line signal level */
  77. USDHC_DATA4_LINE_LEVEL_FLAG =
  78. 1U << (USDHC_PRES_STATE_DLSL_SHIFT + 4U),
  79. /*!< Data4 line signal level */
  80. USDHC_DATA5_LINE_LEVEL_FLAG =
  81. 1U << (USDHC_PRES_STATE_DLSL_SHIFT + 5U),
  82. /*!< Data5 line signal level */
  83. USDHC_DATA6_LINE_LEVEL_FLAG =
  84. 1U << (USDHC_PRES_STATE_DLSL_SHIFT + 6U),
  85. /*!< Data6 line signal level */
  86. USDHC_DATA7_LINE_LEVEL_FLAG =
  87. (int)(1U << (USDHC_PRES_STATE_DLSL_SHIFT + 7U)),
  88. /*!< Data7 line signal level */
  89. };
  90. enum usdhc_transfer_flag {
  91. USDHC_ENABLE_DMA_FLAG =
  92. USDHC_MIX_CTRL_DMAEN_MASK,
  93. /*!< Enable DMA */
  94. USDHC_CMD_TYPE_SUSPEND_FLAG =
  95. (USDHC_CMD_XFR_TYP_CMDTYP(1U)),
  96. /*!< Suspend command */
  97. USDHC_CMD_TYPE_RESUME_FLAG =
  98. (USDHC_CMD_XFR_TYP_CMDTYP(2U)),
  99. /*!< Resume command */
  100. USDHC_CMD_TYPE_ABORT_FLAG =
  101. (USDHC_CMD_XFR_TYP_CMDTYP(3U)),
  102. /*!< Abort command */
  103. USDHC_BLOCK_COUNT_FLAG =
  104. USDHC_MIX_CTRL_BCEN_MASK,
  105. /*!< Enable block count */
  106. USDHC_AUTO_CMD12_FLAG =
  107. USDHC_MIX_CTRL_AC12EN_MASK,
  108. /*!< Enable auto CMD12 */
  109. USDHC_DATA_READ_FLAG =
  110. USDHC_MIX_CTRL_DTDSEL_MASK,
  111. /*!< Enable data read */
  112. USDHC_MULTIPLE_BLOCK_FLAG =
  113. USDHC_MIX_CTRL_MSBSEL_MASK,
  114. /*!< Multiple block data read/write */
  115. USDHC_AUTO_CMD23FLAG =
  116. USDHC_MIX_CTRL_AC23EN_MASK,
  117. /*!< Enable auto CMD23 */
  118. USDHC_RSP_LEN_136_FLAG =
  119. USDHC_CMD_XFR_TYP_RSPTYP(1U),
  120. /*!< 136 bit response length */
  121. USDHC_RSP_LEN_48_FLAG =
  122. USDHC_CMD_XFR_TYP_RSPTYP(2U),
  123. /*!< 48 bit response length */
  124. USDHC_RSP_LEN_48_BUSY_FLAG =
  125. USDHC_CMD_XFR_TYP_RSPTYP(3U),
  126. /*!< 48 bit response length with busy status */
  127. USDHC_CRC_CHECK_FLAG =
  128. USDHC_CMD_XFR_TYP_CCCEN_MASK,
  129. /*!< Enable CRC check */
  130. USDHC_IDX_CHECK_FLAG =
  131. USDHC_CMD_XFR_TYP_CICEN_MASK,
  132. /*!< Enable index check */
  133. USDHC_DATA_PRESENT_FLAG =
  134. USDHC_CMD_XFR_TYP_DPSEL_MASK,
  135. /*!< Data present flag */
  136. };
  137. enum usdhc_int_status_flag {
  138. USDHC_INT_CMD_DONE_FLAG =
  139. USDHC_INT_STATUS_CC_MASK,
  140. /*!< Command complete */
  141. USDHC_INT_DATA_DONE_FLAG =
  142. USDHC_INT_STATUS_TC_MASK,
  143. /*!< Data complete */
  144. USDHC_INT_BLK_GAP_EVENT_FLAG =
  145. USDHC_INT_STATUS_BGE_MASK,
  146. /*!< Block gap event */
  147. USDHC_INT_DMA_DONE_FLAG =
  148. USDHC_INT_STATUS_DINT_MASK,
  149. /*!< DMA interrupt */
  150. USDHC_INT_BUF_WRITE_READY_FLAG =
  151. USDHC_INT_STATUS_BWR_MASK,
  152. /*!< Buffer write ready */
  153. USDHC_INT_BUF_READ_READY_FLAG =
  154. USDHC_INT_STATUS_BRR_MASK,
  155. /*!< Buffer read ready */
  156. USDHC_INT_CARD_INSERTED_FLAG =
  157. USDHC_INT_STATUS_CINS_MASK,
  158. /*!< Card inserted */
  159. USDHC_INT_CARD_REMOVED_FLAG =
  160. USDHC_INT_STATUS_CRM_MASK,
  161. /*!< Card removed */
  162. USDHC_INT_CARD_INTERRUPT_FLAG =
  163. USDHC_INT_STATUS_CINT_MASK,
  164. /*!< Card interrupt */
  165. USDHC_INT_RE_TUNING_EVENT_FLAG =
  166. USDHC_INT_STATUS_RTE_MASK,
  167. /*!< Re-Tuning event,only for SD3.0 SDR104 mode */
  168. USDHC_INT_TUNING_PASS_FLAG =
  169. USDHC_INT_STATUS_TP_MASK,
  170. /*!< SDR104 mode tuning pass flag */
  171. USDHC_INT_TUNING_ERR_FLAG =
  172. USDHC_INT_STATUS_TNE_MASK,
  173. /*!< SDR104 tuning error flag */
  174. USDHC_INT_CMD_TIMEOUT_FLAG =
  175. USDHC_INT_STATUS_CTOE_MASK,
  176. /*!< Command timeout error */
  177. USDHC_INT_CMD_CRC_ERR_FLAG =
  178. USDHC_INT_STATUS_CCE_MASK,
  179. /*!< Command CRC error */
  180. USDHC_INT_CMD_ENDBIT_ERR_FLAG =
  181. USDHC_INT_STATUS_CEBE_MASK,
  182. /*!< Command end bit error */
  183. USDHC_INT_CMD_IDX_ERR_FLAG =
  184. USDHC_INT_STATUS_CIE_MASK,
  185. /*!< Command index error */
  186. USDHC_INT_DATA_TIMEOUT_FLAG =
  187. USDHC_INT_STATUS_DTOE_MASK,
  188. /*!< Data timeout error */
  189. USDHC_INT_DATA_CRC_ERR_FLAG =
  190. USDHC_INT_STATUS_DCE_MASK,
  191. /*!< Data CRC error */
  192. USDHC_INT_DATA_ENDBIT_ERR_FLAG =
  193. USDHC_INT_STATUS_DEBE_MASK,
  194. /*!< Data end bit error */
  195. USDHC_INT_AUTO_CMD12_ERR_FLAG =
  196. USDHC_INT_STATUS_AC12E_MASK,
  197. /*!< Auto CMD12 error */
  198. USDHC_INT_DMA_ERR_FLAG =
  199. USDHC_INT_STATUS_DMAE_MASK,
  200. /*!< DMA error */
  201. USDHC_INT_CMD_ERR_FLAG =
  202. (USDHC_INT_CMD_TIMEOUT_FLAG |
  203. USDHC_INT_CMD_CRC_ERR_FLAG |
  204. USDHC_INT_CMD_ENDBIT_ERR_FLAG |
  205. USDHC_INT_CMD_IDX_ERR_FLAG),
  206. /*!< Command error */
  207. USDHC_INT_DATA_ERR_FLAG =
  208. (USDHC_INT_DATA_TIMEOUT_FLAG |
  209. USDHC_INT_DATA_CRC_ERR_FLAG |
  210. USDHC_INT_DATA_ENDBIT_ERR_FLAG |
  211. USDHC_INT_AUTO_CMD12_ERR_FLAG),
  212. /*!< Data error */
  213. USDHC_INT_ERR_FLAG =
  214. (USDHC_INT_CMD_ERR_FLAG |
  215. USDHC_INT_DATA_ERR_FLAG |
  216. USDHC_INT_DMA_ERR_FLAG),
  217. /*!< All error */
  218. USDHC_INT_DATA_FLAG =
  219. (USDHC_INT_DATA_DONE_FLAG |
  220. USDHC_INT_DMA_DONE_FLAG |
  221. USDHC_INT_BUF_WRITE_READY_FLAG |
  222. USDHC_INT_BUF_READ_READY_FLAG |
  223. USDHC_INT_DATA_ERR_FLAG |
  224. USDHC_INT_DMA_ERR_FLAG),
  225. /*!< Data interrupts */
  226. USDHC_INT_CMD_FLAG =
  227. (USDHC_INT_CMD_DONE_FLAG |
  228. USDHC_INT_CMD_ERR_FLAG),
  229. /*!< Command interrupts */
  230. USDHC_INT_CARD_DETECT_FLAG =
  231. (USDHC_INT_CARD_INSERTED_FLAG |
  232. USDHC_INT_CARD_REMOVED_FLAG),
  233. /*!< Card detection interrupts */
  234. USDHC_INT_SDR104_TUNING_FLAG =
  235. (USDHC_INT_RE_TUNING_EVENT_FLAG |
  236. USDHC_INT_TUNING_PASS_FLAG |
  237. USDHC_INT_TUNING_ERR_FLAG),
  238. USDHC_INT_ALL_FLAGS =
  239. (USDHC_INT_BLK_GAP_EVENT_FLAG |
  240. USDHC_INT_CARD_INTERRUPT_FLAG |
  241. USDHC_INT_CMD_FLAG |
  242. USDHC_INT_DATA_FLAG |
  243. USDHC_INT_ERR_FLAG |
  244. USDHC_INT_SDR104_TUNING_FLAG),
  245. /*!< All flags mask */
  246. };
  247. enum usdhc_data_bus_width {
  248. USDHC_DATA_BUS_WIDTH_1BIT = 0U,
  249. /*!< 1-bit mode */
  250. USDHC_DATA_BUS_WIDTH_4BIT = 1U,
  251. /*!< 4-bit mode */
  252. USDHC_DATA_BUS_WIDTH_8BIT = 2U,
  253. /*!< 8-bit mode */
  254. };
  255. #define USDHC_MAX_BLOCK_COUNT \
  256. (USDHC_BLK_ATT_BLKCNT_MASK >> \
  257. USDHC_BLK_ATT_BLKCNT_SHIFT)
  258. struct usdhc_cmd {
  259. uint32_t index; /*cmd idx*/
  260. uint32_t argument; /*cmd arg*/
  261. enum usdhc_cmd_type cmd_type;
  262. enum sdhc_rsp_type rsp_type;
  263. uint32_t response[4U];
  264. uint32_t rsp_err_flags;
  265. uint32_t flags;
  266. };
  267. struct usdhc_data {
  268. bool cmd12;
  269. /* Enable auto CMD12 */
  270. bool cmd23;
  271. /* Enable auto CMD23 */
  272. bool ignore_err;
  273. /* Enable to ignore error event
  274. * to read/write all the data
  275. */
  276. bool data_enable;
  277. uint8_t data_type;
  278. /* this is used to distinguish
  279. * the normal/tuning/boot data
  280. */
  281. uint32_t block_size;
  282. /* Block size
  283. */
  284. uint32_t block_count;
  285. /* Block count
  286. */
  287. uint32_t *rx_data;
  288. /* Buffer to save data read
  289. */
  290. const uint32_t *tx_data;
  291. /* Data buffer to write
  292. */
  293. };
  294. enum usdhc_dma_mode {
  295. USDHC_DMA_SIMPLE = 0U,
  296. /* external DMA
  297. */
  298. USDHC_DMA_ADMA1 = 1U,
  299. /* ADMA1 is selected
  300. */
  301. USDHC_DMA_ADMA2 = 2U,
  302. /* ADMA2 is selected
  303. */
  304. USDHC_EXT_DMA = 3U,
  305. /* external dma mode select
  306. */
  307. };
  308. enum usdhc_burst_len {
  309. USDHC_INCR_BURST_LEN = 0x01U,
  310. /* enable burst len for INCR
  311. */
  312. USDHC_INCR4816_BURST_LEN = 0x02U,
  313. /* enable burst len for INCR4/INCR8/INCR16
  314. */
  315. USDHC_INCR4816_BURST_LEN_WRAP = 0x04U,
  316. /* enable burst len for INCR4/8/16 WRAP
  317. */
  318. };
  319. struct usdhc_adma_config {
  320. enum usdhc_dma_mode dma_mode;
  321. /* DMA mode
  322. */
  323. enum usdhc_burst_len burst_len;
  324. /* burst len config
  325. */
  326. uint32_t *adma_table;
  327. /* ADMA table address,
  328. * can't be null if transfer way is ADMA1/ADMA2
  329. */
  330. uint32_t adma_table_words;
  331. /* ADMA table length united as words,
  332. * can't be 0 if transfer way is ADMA1/ADMA2
  333. */
  334. };
  335. struct usdhc_context {
  336. bool cmd_only;
  337. struct usdhc_cmd cmd;
  338. struct usdhc_data data;
  339. struct usdhc_adma_config dma_cfg;
  340. };
  341. enum usdhc_endian_mode {
  342. USDHC_BIG_ENDIAN = 0U,
  343. /* Big endian mode
  344. */
  345. USDHC_HALF_WORD_BIG_ENDIAN = 1U,
  346. /* Half word big endian mode
  347. */
  348. USDHC_LITTLE_ENDIAN = 2U,
  349. /* Little endian mode
  350. */
  351. };
  352. struct usdhc_config {
  353. USDHC_Type *base;
  354. const struct device *clock_dev;
  355. clock_control_subsys_t clock_subsys;
  356. uint8_t nusdhc;
  357. char *pwr_name;
  358. uint8_t pwr_pin;
  359. gpio_dt_flags_t pwr_flags;
  360. char *detect_name;
  361. uint8_t detect_pin;
  362. gpio_dt_flags_t detect_flags;
  363. bool no_1_8_v;
  364. uint32_t data_timeout;
  365. /* Data timeout value
  366. */
  367. enum usdhc_endian_mode endian;
  368. /* Endian mode
  369. */
  370. uint8_t read_watermark;
  371. /* Watermark level for DMA read operation.
  372. * Available range is 1 ~ 128.
  373. */
  374. uint8_t write_watermark;
  375. /* Watermark level for DMA write operation.
  376. * Available range is 1 ~ 128.
  377. */
  378. uint8_t read_burst_len;
  379. /* Read burst len
  380. */
  381. uint8_t write_burst_len;
  382. /* Write burst len
  383. */
  384. };
  385. struct usdhc_capability {
  386. uint32_t max_blk_len;
  387. uint32_t max_blk_cnt;
  388. uint32_t host_flags;
  389. };
  390. enum host_detect_type {
  391. SD_DETECT_GPIO_CD,
  392. /* sd card detect by CD pin through GPIO
  393. */
  394. SD_DETECT_HOST_CD,
  395. /* sd card detect by CD pin through host
  396. */
  397. SD_DETECT_HOST_DATA3,
  398. /* sd card detect by DAT3 pin through host
  399. */
  400. };
  401. struct usdhc_client_info {
  402. uint32_t busclk_hz;
  403. uint32_t relative_addr;
  404. uint32_t version;
  405. uint32_t card_flags;
  406. uint32_t raw_cid[4U];
  407. uint32_t raw_csd[4U];
  408. uint32_t raw_scr[2U];
  409. uint32_t raw_ocr;
  410. struct sd_cid cid;
  411. struct sd_csd csd;
  412. struct sd_scr scr;
  413. uint32_t sd_block_count;
  414. uint32_t sd_block_size;
  415. enum sd_timing_mode sd_timing;
  416. enum sd_driver_strength driver_strength;
  417. enum sd_max_current max_current;
  418. enum sd_voltage voltage;
  419. };
  420. struct usdhc_priv {
  421. bool host_ready;
  422. uint8_t status;
  423. const struct device *pwr_gpio;
  424. const struct device *detect_gpio;
  425. struct gpio_callback detect_cb;
  426. enum host_detect_type detect_type;
  427. bool inserted;
  428. uint32_t src_clk_hz;
  429. const struct usdhc_config *config;
  430. struct usdhc_capability host_capability;
  431. struct usdhc_client_info card_info;
  432. struct usdhc_context op_context;
  433. };
  434. enum usdhc_xfer_data_type {
  435. USDHC_XFER_NORMAL = 0U,
  436. /* transfer normal read/write data
  437. */
  438. USDHC_XFER_TUNING = 1U,
  439. /* transfer tuning data
  440. */
  441. USDHC_XFER_BOOT = 2U,
  442. /* transfer boot data
  443. */
  444. USDHC_XFER_BOOT_CONTINUOUS = 3U,
  445. /* transfer boot data continuous
  446. */
  447. };
  448. #define USDHC_ADMA1_ADDRESS_ALIGN (4096U)
  449. #define USDHC_ADMA1_LENGTH_ALIGN (4096U)
  450. #define USDHC_ADMA2_ADDRESS_ALIGN (4U)
  451. #define USDHC_ADMA2_LENGTH_ALIGN (4U)
  452. #define USDHC_ADMA2_DESCRIPTOR_LENGTH_SHIFT (16U)
  453. #define USDHC_ADMA2_DESCRIPTOR_LENGTH_MASK (0xFFFFU)
  454. #define USDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY \
  455. (USDHC_ADMA2_DESCRIPTOR_LENGTH_MASK - 3U)
  456. #define SWAP_WORD_BYTE_SEQUENCE(x) (__REV(x))
  457. #define SWAP_HALF_WROD_BYTE_SEQUENCE(x) (__REV16(x))
  458. #define SDMMCHOST_NOT_SUPPORT 0U
  459. #define CARD_BUS_FREQ_50MHZ (0U)
  460. #define CARD_BUS_FREQ_100MHZ0 (1U)
  461. #define CARD_BUS_FREQ_100MHZ1 (2U)
  462. #define CARD_BUS_FREQ_200MHZ (3U)
  463. #define CARD_BUS_STRENGTH_0 (0U)
  464. #define CARD_BUS_STRENGTH_1 (1U)
  465. #define CARD_BUS_STRENGTH_2 (2U)
  466. #define CARD_BUS_STRENGTH_3 (3U)
  467. #define CARD_BUS_STRENGTH_4 (4U)
  468. #define CARD_BUS_STRENGTH_5 (5U)
  469. #define CARD_BUS_STRENGTH_6 (6U)
  470. #define CARD_BUS_STRENGTH_7 (7U)
  471. enum usdhc_adma_flag {
  472. USDHC_ADMA_SINGLE_FLAG = 0U,
  473. USDHC_ADMA_MUTI_FLAG = 1U,
  474. };
  475. enum usdhc_adma2_descriptor_flag {
  476. USDHC_ADMA2_VALID_FLAG = (1U << 0U),
  477. /* Valid flag
  478. */
  479. USDHC_ADMA2_END_FLAG = (1U << 1U),
  480. /* End flag
  481. */
  482. USDHC_ADMA2_INT_FLAG = (1U << 2U),
  483. /* Interrupt flag
  484. */
  485. USDHC_ADMA2_ACTIVITY1_FLAG = (1U << 4U),
  486. /* Activity 1 mask
  487. */
  488. USDHC_ADMA2_ACTIVITY2_FLAG = (1U << 5U),
  489. /* Activity 2 mask
  490. */
  491. USDHC_ADMA2_NOP_FLAG =
  492. (USDHC_ADMA2_VALID_FLAG),
  493. /* No operation
  494. */
  495. USDHC_ADMA2_RESERVED_FLAG =
  496. (USDHC_ADMA2_ACTIVITY1_FLAG |
  497. USDHC_ADMA2_VALID_FLAG),
  498. /* Reserved
  499. */
  500. USDHC_ADMA2_XFER_FLAG =
  501. (USDHC_ADMA2_ACTIVITY2_FLAG |
  502. USDHC_ADMA2_VALID_FLAG),
  503. /* Transfer type
  504. */
  505. USDHC_ADMA2_LINK_FLAG =
  506. (USDHC_ADMA2_ACTIVITY1_FLAG |
  507. USDHC_ADMA2_ACTIVITY2_FLAG |
  508. USDHC_ADMA2_VALID_FLAG),
  509. /* Link type
  510. */
  511. };
  512. struct usdhc_adma2_descriptor {
  513. uint32_t attribute;
  514. /*!< The control and status field */
  515. const uint32_t *address;
  516. /*!< The address field */
  517. };
  518. enum usdhc_card_flag {
  519. USDHC_HIGH_CAPACITY_FLAG =
  520. (1U << 1U),
  521. /* Support high capacity
  522. */
  523. USDHC_4BIT_WIDTH_FLAG =
  524. (1U << 2U),
  525. /* Support 4-bit data width
  526. */
  527. USDHC_SDHC_FLAG =
  528. (1U << 3U),
  529. /* Card is SDHC
  530. */
  531. USDHC_SDXC_FLAG =
  532. (1U << 4U),
  533. /* Card is SDXC
  534. */
  535. USDHC_VOL_1_8V_FLAG =
  536. (1U << 5U),
  537. /* card support 1.8v voltage
  538. */
  539. USDHC_SET_BLK_CNT_CMD23_FLAG =
  540. (1U << 6U),
  541. /* card support cmd23 flag
  542. */
  543. USDHC_SPEED_CLASS_CONTROL_CMD_FLAG =
  544. (1U << 7U),
  545. /* card support speed class control flag
  546. */
  547. };
  548. enum usdhc_capability_flag {
  549. USDHC_SUPPORT_ADMA_FLAG =
  550. USDHC_HOST_CTRL_CAP_ADMAS_MASK,
  551. /*!< Support ADMA */
  552. USDHC_SUPPORT_HIGHSPEED_FLAG =
  553. USDHC_HOST_CTRL_CAP_HSS_MASK,
  554. /*!< Support high-speed */
  555. USDHC_SUPPORT_DMA_FLAG =
  556. USDHC_HOST_CTRL_CAP_DMAS_MASK,
  557. /*!< Support DMA */
  558. USDHC_SUPPORT_SUSPEND_RESUME_FLAG =
  559. USDHC_HOST_CTRL_CAP_SRS_MASK,
  560. /*!< Support suspend/resume */
  561. USDHC_SUPPORT_V330_FLAG =
  562. USDHC_HOST_CTRL_CAP_VS33_MASK,
  563. /*!< Support voltage 3.3V */
  564. USDHC_SUPPORT_V300_FLAG =
  565. USDHC_HOST_CTRL_CAP_VS30_MASK,
  566. /*!< Support voltage 3.0V */
  567. USDHC_SUPPORT_V180_FLAG =
  568. USDHC_HOST_CTRL_CAP_VS18_MASK,
  569. /*!< Support voltage 1.8V */
  570. /* Put additional two flags in
  571. * HTCAPBLT_MBL's position.
  572. */
  573. USDHC_SUPPORT_4BIT_FLAG =
  574. (USDHC_HOST_CTRL_CAP_MBL_SHIFT << 0U),
  575. /*!< Support 4 bit mode */
  576. USDHC_SUPPORT_8BIT_FLAG =
  577. (USDHC_HOST_CTRL_CAP_MBL_SHIFT << 1U),
  578. /*!< Support 8 bit mode */
  579. /* sd version 3.0 new feature */
  580. USDHC_SUPPORT_DDR50_FLAG =
  581. USDHC_HOST_CTRL_CAP_DDR50_SUPPORT_MASK,
  582. /*!< support DDR50 mode */
  583. #if defined(FSL_FEATURE_USDHC_HAS_SDR104_MODE) &&\
  584. (!FSL_FEATURE_USDHC_HAS_SDR104_MODE)
  585. USDHC_SUPPORT_SDR104_FLAG = 0,
  586. /*!< not support SDR104 mode */
  587. #else
  588. USDHC_SUPPORT_SDR104_FLAG =
  589. USDHC_HOST_CTRL_CAP_SDR104_SUPPORT_MASK,
  590. /*!< support SDR104 mode */
  591. #endif
  592. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) &&\
  593. (!FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  594. USDHC_SUPPORT_SDR50_FLAG = 0U,
  595. /*!< not support SDR50 mode */
  596. #else
  597. USDHC_SUPPORT_SDR50_FLAG =
  598. USDHC_HOST_CTRL_CAP_SDR50_SUPPORT_MASK,
  599. /*!< support SDR50 mode */
  600. #endif
  601. };
  602. #define NXP_SDMMC_MAX_VOLTAGE_RETRIES (1000U)
  603. #define CARD_DATA0_STATUS_MASK USDHC_DATA0_LINE_LEVEL_FLAG
  604. #define CARD_DATA1_STATUS_MASK USDHC_DATA1_LINE_LEVEL_FLAG
  605. #define CARD_DATA2_STATUS_MASK USDHC_DATA2_LINE_LEVEL_FLAG
  606. #define CARD_DATA3_STATUS_MASK USDHC_DATA3_LINE_LEVEL_FLAG
  607. #define CARD_DATA0_NOT_BUSY USDHC_DATA0_LINE_LEVEL_FLAG
  608. #define SDHC_STANDARD_TUNING_START (10U)
  609. /*!< standard tuning start point */
  610. #define SDHC_TUINIG_STEP (2U)
  611. /*!< standard tuning step */
  612. #define SDHC_RETUNING_TIMER_COUNT (0U)
  613. /*!< Re-tuning timer */
  614. #define USDHC_MAX_DVS \
  615. ((USDHC_SYS_CTRL_DVS_MASK >> \
  616. USDHC_SYS_CTRL_DVS_SHIFT) + 1U)
  617. #define USDHC_MAX_CLKFS \
  618. ((USDHC_SYS_CTRL_SDCLKFS_MASK >> \
  619. USDHC_SYS_CTRL_SDCLKFS_SHIFT) + 1U)
  620. #define USDHC_PREV_DVS(x) ((x) -= 1U)
  621. #define USDHC_PREV_CLKFS(x, y) ((x) >>= (y))
  622. #define SDMMCHOST_SUPPORT_SDR104_FREQ SD_CLOCK_208MHZ
  623. #define USDHC_ADMA_TABLE_WORDS (8U)
  624. #define USDHC_ADMA2_ADDR_ALIGN (4U)
  625. #define USDHC_READ_BURST_LEN (8U)
  626. #define USDHC_WRITE_BURST_LEN (8U)
  627. #define USDHC_DATA_TIMEOUT (0xFU)
  628. #define USDHC_READ_WATERMARK_LEVEL (0x80U)
  629. #define USDHC_WRITE_WATERMARK_LEVEL (0x80U)
  630. enum usdhc_reset {
  631. USDHC_RESET_ALL =
  632. USDHC_SYS_CTRL_RSTA_MASK,
  633. /*!< Reset all except card detection */
  634. USDHC_RESET_CMD =
  635. USDHC_SYS_CTRL_RSTC_MASK,
  636. /*!< Reset command line */
  637. USDHC_RESET_DATA =
  638. USDHC_SYS_CTRL_RSTD_MASK,
  639. /*!< Reset data line */
  640. #if defined(FSL_FEATURE_USDHC_HAS_SDR50_MODE) &&\
  641. (!FSL_FEATURE_USDHC_HAS_SDR50_MODE)
  642. USDHC_RESET_TUNING = 0U,
  643. /*!< no reset tuning circuit bit */
  644. #else
  645. USDHC_RESET_TUNING = USDHC_SYS_CTRL_RSTT_MASK,
  646. /*!< reset tuning circuit */
  647. #endif
  648. USDHC_RESETS_All =
  649. (USDHC_RESET_ALL |
  650. USDHC_RESET_CMD |
  651. USDHC_RESET_DATA |
  652. USDHC_RESET_TUNING),
  653. /*!< All reset types */
  654. };
  655. static void usdhc_millsec_delay(unsigned int cycles_to_wait)
  656. {
  657. unsigned int start = sys_clock_cycle_get_32();
  658. while (sys_clock_cycle_get_32() - start < (cycles_to_wait * 1000))
  659. ;
  660. }
  661. uint32_t g_usdhc_boot_dummy __aligned(64);
  662. uint32_t g_usdhc_rx_dummy[2048] __aligned(64);
  663. static int usdhc_adma2_descriptor_cfg(
  664. uint32_t *adma_table, uint32_t adma_table_words,
  665. const uint32_t *data_addr, uint32_t data_size, uint32_t flags)
  666. {
  667. uint32_t min_entries, start_entry = 0U;
  668. uint32_t max_entries = (adma_table_words * sizeof(uint32_t)) /
  669. sizeof(struct usdhc_adma2_descriptor);
  670. struct usdhc_adma2_descriptor *adma2_addr =
  671. (struct usdhc_adma2_descriptor *)(adma_table);
  672. uint32_t i, dma_buf_len = 0U;
  673. if ((uint32_t)data_addr % USDHC_ADMA2_ADDRESS_ALIGN) {
  674. return -EIO;
  675. }
  676. /* Add non aligned access support.
  677. */
  678. if (data_size % sizeof(uint32_t)) {
  679. /* make the data length as word-aligned */
  680. data_size += sizeof(uint32_t) - (data_size % sizeof(uint32_t));
  681. }
  682. /* Check if ADMA descriptor's number is enough. */
  683. if (!(data_size % USDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY)) {
  684. min_entries = data_size /
  685. USDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY;
  686. } else {
  687. min_entries = ((data_size /
  688. USDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY) + 1U);
  689. }
  690. /* calcucate the start entry for multiple descriptor mode,
  691. * ADMA engine is not stop, so update the descriptor
  692. * data address and data size is enough
  693. */
  694. if (flags == USDHC_ADMA_MUTI_FLAG) {
  695. for (i = 0U; i < max_entries; i++) {
  696. if (!(adma2_addr[i].attribute & USDHC_ADMA2_VALID_FLAG))
  697. break;
  698. }
  699. start_entry = i;
  700. /* add one entry for dummy entry */
  701. min_entries += 1U;
  702. }
  703. if ((min_entries + start_entry) > max_entries) {
  704. return -EIO;
  705. }
  706. for (i = start_entry; i < (min_entries + start_entry); i++) {
  707. if (data_size > USDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY) {
  708. dma_buf_len =
  709. USDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY;
  710. } else {
  711. dma_buf_len = (data_size == 0U ? sizeof(uint32_t) :
  712. data_size);
  713. /* adma don't support 0 data length transfer
  714. * descriptor
  715. */
  716. }
  717. /* Each descriptor for ADMA2 is 64-bit in length */
  718. adma2_addr[i].address = (data_size == 0U) ?
  719. &g_usdhc_boot_dummy : data_addr;
  720. adma2_addr[i].attribute = (dma_buf_len <<
  721. USDHC_ADMA2_DESCRIPTOR_LENGTH_SHIFT);
  722. adma2_addr[i].attribute |=
  723. (data_size == 0U) ? 0U :
  724. (USDHC_ADMA2_XFER_FLAG | USDHC_ADMA2_INT_FLAG);
  725. data_addr += (dma_buf_len / sizeof(uint32_t));
  726. if (data_size != 0U)
  727. data_size -= dma_buf_len;
  728. }
  729. /* add a dummy valid ADMA descriptor for multiple descriptor mode,
  730. * this is useful when transfer boot data, the ADMA
  731. * engine will not stop at block gap
  732. */
  733. if (flags == USDHC_ADMA_MUTI_FLAG) {
  734. adma2_addr[start_entry + 1U].attribute |= USDHC_ADMA2_XFER_FLAG;
  735. } else {
  736. adma2_addr[i - 1U].attribute |= USDHC_ADMA2_END_FLAG;
  737. /* set the end bit */
  738. }
  739. return 0;
  740. }
  741. static int usdhc_Internal_dma_cfg(struct usdhc_priv *priv,
  742. struct usdhc_adma_config *dma_cfg,
  743. const uint32_t *data_addr)
  744. {
  745. USDHC_Type *base = priv->config->base;
  746. bool cmd23 = priv->op_context.data.cmd23;
  747. if (dma_cfg->dma_mode == USDHC_DMA_SIMPLE) {
  748. /* check DMA data buffer address align or not */
  749. if (((uint32_t)data_addr % USDHC_ADMA2_ADDRESS_ALIGN) != 0U) {
  750. return -EIO;
  751. }
  752. /* in simple DMA mode if use auto CMD23,
  753. * address should load to ADMA addr,
  754. * and block count should load to DS_ADDR
  755. */
  756. if (cmd23)
  757. base->ADMA_SYS_ADDR = (uint32_t)data_addr;
  758. else
  759. base->DS_ADDR = (uint32_t)data_addr;
  760. } else {
  761. /* When use ADMA, disable simple DMA */
  762. base->DS_ADDR = 0U;
  763. base->ADMA_SYS_ADDR = (uint32_t)(dma_cfg->adma_table);
  764. }
  765. /* select DMA mode and config the burst length */
  766. base->PROT_CTRL &= ~(USDHC_PROT_CTRL_DMASEL_MASK |
  767. USDHC_PROT_CTRL_BURST_LEN_EN_MASK);
  768. base->PROT_CTRL |= USDHC_PROT_CTRL_DMASEL(dma_cfg->dma_mode) |
  769. USDHC_PROT_CTRL_BURST_LEN_EN(dma_cfg->burst_len);
  770. /* enable DMA */
  771. base->MIX_CTRL |= USDHC_MIX_CTRL_DMAEN_MASK;
  772. return 0;
  773. }
  774. static int usdhc_adma_table_cfg(struct usdhc_priv *priv, uint32_t flags)
  775. {
  776. int error = -EIO;
  777. struct usdhc_data *data = &priv->op_context.data;
  778. struct usdhc_adma_config *dma_cfg = &priv->op_context.dma_cfg;
  779. uint32_t boot_dummy_off = data->data_type == USDHC_XFER_BOOT_CONTINUOUS ?
  780. sizeof(uint32_t) : 0U;
  781. const uint32_t *data_addr = (const uint32_t *)((uint32_t)((!data->rx_data) ?
  782. data->tx_data : data->rx_data) + boot_dummy_off);
  783. uint32_t data_size = data->block_size * data->block_count - boot_dummy_off;
  784. switch (dma_cfg->dma_mode) {
  785. case USDHC_DMA_SIMPLE:
  786. error = 0;
  787. break;
  788. case USDHC_DMA_ADMA1:
  789. error = -EINVAL;
  790. break;
  791. case USDHC_DMA_ADMA2:
  792. error = usdhc_adma2_descriptor_cfg(dma_cfg->adma_table,
  793. dma_cfg->adma_table_words, data_addr, data_size, flags);
  794. break;
  795. default:
  796. return -EINVAL;
  797. }
  798. /* for internal dma, internal DMA configurations should not update
  799. * the configurations when continuous transfer the
  800. * boot data, only the DMA descriptor need update
  801. */
  802. if ((!error) && (data->data_type != USDHC_XFER_BOOT_CONTINUOUS)) {
  803. error = usdhc_Internal_dma_cfg(priv, dma_cfg, data_addr);
  804. }
  805. return error;
  806. }
  807. static int usdhc_data_xfer_cfg(struct usdhc_priv *priv,
  808. bool en_dma)
  809. {
  810. USDHC_Type *base = priv->config->base;
  811. uint32_t mix_ctrl = base->MIX_CTRL;
  812. struct usdhc_data *data = NULL;
  813. uint32_t *flag = &priv->op_context.cmd.flags;
  814. if (!priv->op_context.cmd_only)
  815. data = &priv->op_context.data;
  816. if (data != NULL) {
  817. if (data->data_type == USDHC_XFER_BOOT_CONTINUOUS) {
  818. /* clear stop at block gap request */
  819. base->PROT_CTRL &= ~USDHC_PROT_CTRL_SABGREQ_MASK;
  820. /* continuous transfer data */
  821. base->PROT_CTRL |= USDHC_PROT_CTRL_CREQ_MASK;
  822. return 0;
  823. }
  824. /* check data inhibit flag */
  825. if (base->PRES_STATE & USDHC_DATA_INHIBIT_FLAG)
  826. return -EBUSY;
  827. /* check transfer block count */
  828. if ((data->block_count > USDHC_MAX_BLOCK_COUNT) ||
  829. (!data->tx_data && !data->rx_data))
  830. return -EINVAL;
  831. /* config mix parameter */
  832. mix_ctrl &= ~(USDHC_MIX_CTRL_MSBSEL_MASK |
  833. USDHC_MIX_CTRL_BCEN_MASK |
  834. USDHC_MIX_CTRL_DTDSEL_MASK |
  835. USDHC_MIX_CTRL_AC12EN_MASK);
  836. if (data->rx_data) {
  837. mix_ctrl |= USDHC_MIX_CTRL_DTDSEL_MASK;
  838. }
  839. if (data->block_count > 1U) {
  840. mix_ctrl |= USDHC_MIX_CTRL_MSBSEL_MASK |
  841. USDHC_MIX_CTRL_BCEN_MASK;
  842. /* auto command 12 */
  843. if (data->cmd12) {
  844. mix_ctrl |= USDHC_MIX_CTRL_AC12EN_MASK;
  845. }
  846. }
  847. /* auto command 23, auto send set block count cmd before
  848. * multiple read/write
  849. */
  850. if ((data->cmd23)) {
  851. mix_ctrl |= USDHC_MIX_CTRL_AC23EN_MASK;
  852. base->VEND_SPEC2 |=
  853. USDHC_VEND_SPEC2_ACMD23_ARGU2_EN_MASK;
  854. /* config the block count to DS_ADDR */
  855. base->DS_ADDR = data->block_count;
  856. } else {
  857. mix_ctrl &= ~USDHC_MIX_CTRL_AC23EN_MASK;
  858. base->VEND_SPEC2 &=
  859. (~USDHC_VEND_SPEC2_ACMD23_ARGU2_EN_MASK);
  860. }
  861. if (data->data_type != USDHC_XFER_BOOT) {
  862. /* config data block size/block count */
  863. base->BLK_ATT =
  864. ((base->BLK_ATT & ~(USDHC_BLK_ATT_BLKSIZE_MASK |
  865. USDHC_BLK_ATT_BLKCNT_MASK)) |
  866. (USDHC_BLK_ATT_BLKSIZE(data->block_size) |
  867. USDHC_BLK_ATT_BLKCNT(data->block_count)));
  868. } else {
  869. mix_ctrl |= USDHC_MIX_CTRL_MSBSEL_MASK |
  870. USDHC_MIX_CTRL_BCEN_MASK;
  871. base->PROT_CTRL |=
  872. USDHC_PROT_CTRL_RD_DONE_NO_8CLK_MASK;
  873. }
  874. /* data present flag */
  875. *flag |= USDHC_DATA_PRESENT_FLAG;
  876. /* Disable useless interrupt */
  877. if (en_dma) {
  878. base->INT_SIGNAL_EN &=
  879. ~(USDHC_INT_BUF_WRITE_READY_FLAG |
  880. USDHC_INT_BUF_READ_READY_FLAG |
  881. USDHC_INT_DMA_DONE_FLAG);
  882. base->INT_STATUS_EN &=
  883. ~(USDHC_INT_BUF_WRITE_READY_FLAG |
  884. USDHC_INT_BUF_READ_READY_FLAG |
  885. USDHC_INT_DMA_DONE_FLAG);
  886. } else {
  887. base->INT_SIGNAL_EN |=
  888. USDHC_INT_BUF_WRITE_READY_FLAG |
  889. USDHC_INT_BUF_READ_READY_FLAG;
  890. base->INT_STATUS_EN |=
  891. USDHC_INT_BUF_WRITE_READY_FLAG |
  892. USDHC_INT_BUF_READ_READY_FLAG;
  893. }
  894. } else {
  895. /* clear data flags */
  896. mix_ctrl &= ~(USDHC_MIX_CTRL_MSBSEL_MASK |
  897. USDHC_MIX_CTRL_BCEN_MASK |
  898. USDHC_MIX_CTRL_DTDSEL_MASK |
  899. USDHC_MIX_CTRL_AC12EN_MASK |
  900. USDHC_MIX_CTRL_AC23EN_MASK);
  901. if (base->PRES_STATE & USDHC_CMD_INHIBIT_FLAG)
  902. return -EBUSY;
  903. }
  904. /* config the mix parameter */
  905. base->MIX_CTRL = mix_ctrl;
  906. return 0;
  907. }
  908. static void usdhc_send_cmd(USDHC_Type *base, struct usdhc_cmd *command)
  909. {
  910. uint32_t xfer_type = base->CMD_XFR_TYP;
  911. uint32_t flags = command->flags;
  912. if (!(base->PRES_STATE & USDHC_CMD_INHIBIT_FLAG)
  913. && (command->cmd_type != USDHC_CMD_TYPE_EMPTY)) {
  914. /* Define the flag corresponding to each response type. */
  915. switch (command->rsp_type) {
  916. case SDHC_RSP_TYPE_NONE:
  917. break;
  918. case SDHC_RSP_TYPE_R1: /* Response 1 */
  919. case SDHC_RSP_TYPE_R5: /* Response 5 */
  920. case SDHC_RSP_TYPE_R6: /* Response 6 */
  921. case SDHC_RSP_TYPE_R7: /* Response 7 */
  922. flags |= (USDHC_RSP_LEN_48_FLAG |
  923. USDHC_CRC_CHECK_FLAG |
  924. USDHC_IDX_CHECK_FLAG);
  925. break;
  926. case SDHC_RSP_TYPE_R1b: /* Response 1 with busy */
  927. case SDHC_RSP_TYPE_R5b: /* Response 5 with busy */
  928. flags |= (USDHC_RSP_LEN_48_BUSY_FLAG |
  929. USDHC_CRC_CHECK_FLAG |
  930. USDHC_IDX_CHECK_FLAG);
  931. break;
  932. case SDHC_RSP_TYPE_R2: /* Response 2 */
  933. flags |= (USDHC_RSP_LEN_136_FLAG |
  934. USDHC_CRC_CHECK_FLAG);
  935. break;
  936. case SDHC_RSP_TYPE_R3: /* Response 3 */
  937. case SDHC_RSP_TYPE_R4: /* Response 4 */
  938. flags |= (USDHC_RSP_LEN_48_FLAG);
  939. break;
  940. default:
  941. break;
  942. }
  943. if (command->cmd_type == USDHC_CMD_TYPE_ABORT)
  944. flags |= USDHC_CMD_TYPE_ABORT_FLAG;
  945. /* config cmd index */
  946. xfer_type &= ~(USDHC_CMD_XFR_TYP_CMDINX_MASK |
  947. USDHC_CMD_XFR_TYP_CMDTYP_MASK |
  948. USDHC_CMD_XFR_TYP_CICEN_MASK |
  949. USDHC_CMD_XFR_TYP_CCCEN_MASK |
  950. USDHC_CMD_XFR_TYP_RSPTYP_MASK |
  951. USDHC_CMD_XFR_TYP_DPSEL_MASK);
  952. xfer_type |=
  953. (((command->index << USDHC_CMD_XFR_TYP_CMDINX_SHIFT) &
  954. USDHC_CMD_XFR_TYP_CMDINX_MASK) |
  955. ((flags) & (USDHC_CMD_XFR_TYP_CMDTYP_MASK |
  956. USDHC_CMD_XFR_TYP_CICEN_MASK |
  957. USDHC_CMD_XFR_TYP_CCCEN_MASK |
  958. USDHC_CMD_XFR_TYP_RSPTYP_MASK |
  959. USDHC_CMD_XFR_TYP_DPSEL_MASK)));
  960. /* config the command xfertype and argument */
  961. base->CMD_ARG = command->argument;
  962. base->CMD_XFR_TYP = xfer_type;
  963. }
  964. if (command->cmd_type == USDHC_CMD_TYPE_EMPTY) {
  965. /* disable CMD done interrupt for empty command */
  966. base->INT_SIGNAL_EN &= ~USDHC_INT_SIGNAL_EN_CCIEN_MASK;
  967. }
  968. }
  969. static int usdhc_cmd_rsp(struct usdhc_priv *priv)
  970. {
  971. uint32_t i;
  972. USDHC_Type *base = priv->config->base;
  973. struct usdhc_cmd *cmd = &priv->op_context.cmd;
  974. if (cmd->rsp_type != SDHC_RSP_TYPE_NONE) {
  975. cmd->response[0U] = base->CMD_RSP0;
  976. if (cmd->rsp_type == SDHC_RSP_TYPE_R2) {
  977. cmd->response[1U] = base->CMD_RSP1;
  978. cmd->response[2U] = base->CMD_RSP2;
  979. cmd->response[3U] = base->CMD_RSP3;
  980. i = 4U;
  981. /* R3-R2-R1-R0(lowest 8 bit is invalid bit)
  982. * has the same format
  983. * as R2 format in SD specification document
  984. * after removed internal CRC7 and end bit.
  985. */
  986. do {
  987. cmd->response[i - 1U] <<= 8U;
  988. if (i > 1U) {
  989. cmd->response[i - 1U] |=
  990. ((cmd->response[i - 2U] &
  991. 0xFF000000U) >> 24U);
  992. }
  993. i--;
  994. } while (i);
  995. }
  996. }
  997. /* check response error flag */
  998. if ((cmd->rsp_err_flags) &&
  999. ((cmd->rsp_type == SDHC_RSP_TYPE_R1) ||
  1000. (cmd->rsp_type == SDHC_RSP_TYPE_R1b) ||
  1001. (cmd->rsp_type == SDHC_RSP_TYPE_R6) ||
  1002. (cmd->rsp_type == SDHC_RSP_TYPE_R5))) {
  1003. if (((cmd->rsp_err_flags) & (cmd->response[0U])))
  1004. return -EIO;
  1005. }
  1006. return 0;
  1007. }
  1008. static int usdhc_wait_cmd_done(struct usdhc_priv *priv,
  1009. bool poll_cmd)
  1010. {
  1011. int error = 0;
  1012. uint32_t int_status = 0U;
  1013. USDHC_Type *base = priv->config->base;
  1014. /* check if need polling command done or not */
  1015. if (poll_cmd) {
  1016. /* Wait command complete or USDHC encounters error. */
  1017. while (!(int_status & (USDHC_INT_CMD_DONE_FLAG |
  1018. USDHC_INT_CMD_ERR_FLAG))) {
  1019. int_status = base->INT_STATUS;
  1020. }
  1021. if ((int_status & USDHC_INT_TUNING_ERR_FLAG) ||
  1022. (int_status & USDHC_INT_CMD_ERR_FLAG)) {
  1023. error = -EIO;
  1024. }
  1025. /* Receive response when command completes successfully. */
  1026. if (!error) {
  1027. error = usdhc_cmd_rsp(priv);
  1028. } else {
  1029. LOG_ERR("CMD%d Polling ERROR",
  1030. priv->op_context.cmd.index);
  1031. }
  1032. base->INT_STATUS = (USDHC_INT_CMD_DONE_FLAG |
  1033. USDHC_INT_CMD_ERR_FLAG |
  1034. USDHC_INT_TUNING_ERR_FLAG);
  1035. }
  1036. return error;
  1037. }
  1038. static inline void usdhc_write_data(USDHC_Type *base, uint32_t data)
  1039. {
  1040. base->DATA_BUFF_ACC_PORT = data;
  1041. }
  1042. static inline uint32_t usdhc_read_data(USDHC_Type *base)
  1043. {
  1044. return base->DATA_BUFF_ACC_PORT;
  1045. }
  1046. static uint32_t usdhc_read_data_port(struct usdhc_priv *priv,
  1047. uint32_t xfered_words)
  1048. {
  1049. USDHC_Type *base = priv->config->base;
  1050. struct usdhc_data *data = &priv->op_context.data;
  1051. uint32_t i, total_words, remaing_words;
  1052. /* The words can be read at this time. */
  1053. uint32_t watermark = ((base->WTMK_LVL & USDHC_WTMK_LVL_RD_WML_MASK) >>
  1054. USDHC_WTMK_LVL_RD_WML_SHIFT);
  1055. /* If DMA is enable, do not need to polling data port */
  1056. if (!(base->MIX_CTRL & USDHC_MIX_CTRL_DMAEN_MASK)) {
  1057. /*Add non aligned access support.*/
  1058. if (data->block_size % sizeof(uint32_t)) {
  1059. data->block_size +=
  1060. sizeof(uint32_t) -
  1061. (data->block_size % sizeof(uint32_t));
  1062. /* make the block size as word-aligned */
  1063. }
  1064. total_words = ((data->block_count * data->block_size) /
  1065. sizeof(uint32_t));
  1066. if (watermark >= total_words) {
  1067. remaing_words = total_words;
  1068. } else if ((watermark < total_words) &&
  1069. ((total_words - xfered_words) >= watermark)) {
  1070. remaing_words = watermark;
  1071. } else {
  1072. remaing_words = (total_words - xfered_words);
  1073. }
  1074. i = 0U;
  1075. while (i < remaing_words) {
  1076. data->rx_data[xfered_words++] = usdhc_read_data(base);
  1077. i++;
  1078. }
  1079. }
  1080. return xfered_words;
  1081. }
  1082. static int usdhc_read_data_port_sync(struct usdhc_priv *priv)
  1083. {
  1084. USDHC_Type *base = priv->config->base;
  1085. struct usdhc_data *data = &priv->op_context.data;
  1086. uint32_t total_words;
  1087. uint32_t xfered_words = 0U, int_status = 0U;
  1088. int error = 0;
  1089. if (data->block_size % sizeof(uint32_t)) {
  1090. data->block_size +=
  1091. sizeof(uint32_t) -
  1092. (data->block_size % sizeof(uint32_t));
  1093. }
  1094. total_words =
  1095. ((data->block_count * data->block_size) /
  1096. sizeof(uint32_t));
  1097. while ((!error) && (xfered_words < total_words)) {
  1098. while (!(int_status & (USDHC_INT_BUF_READ_READY_FLAG |
  1099. USDHC_INT_DATA_ERR_FLAG |
  1100. USDHC_INT_TUNING_ERR_FLAG)))
  1101. int_status = base->INT_STATUS;
  1102. /* during std tuning process, software do not need to read data,
  1103. * but wait BRR is enough
  1104. */
  1105. if ((data->data_type == USDHC_XFER_TUNING) &&
  1106. (int_status & USDHC_INT_BUF_READ_READY_FLAG)) {
  1107. base->INT_STATUS = USDHC_INT_BUF_READ_READY_FLAG |
  1108. USDHC_INT_TUNING_PASS_FLAG;
  1109. return 0;
  1110. } else if ((int_status & USDHC_INT_TUNING_ERR_FLAG)) {
  1111. base->INT_STATUS = USDHC_INT_TUNING_ERR_FLAG;
  1112. /* if tuning error occur ,return directly */
  1113. error = -EIO;
  1114. } else if ((int_status & USDHC_INT_DATA_ERR_FLAG)) {
  1115. if (!(data->ignore_err))
  1116. error = -EIO;
  1117. /* clear data error flag */
  1118. base->INT_STATUS = USDHC_INT_DATA_ERR_FLAG;
  1119. }
  1120. if (!error) {
  1121. xfered_words = usdhc_read_data_port(priv, xfered_words);
  1122. /* clear buffer read ready */
  1123. base->INT_STATUS = USDHC_INT_BUF_READ_READY_FLAG;
  1124. int_status = 0U;
  1125. }
  1126. }
  1127. /* Clear data complete flag after the last read operation. */
  1128. base->INT_STATUS = USDHC_INT_DATA_DONE_FLAG;
  1129. return error;
  1130. }
  1131. static uint32_t usdhc_write_data_port(struct usdhc_priv *priv,
  1132. uint32_t xfered_words)
  1133. {
  1134. USDHC_Type *base = priv->config->base;
  1135. struct usdhc_data *data = &priv->op_context.data;
  1136. uint32_t i, total_words, remaing_words;
  1137. /* Words can be wrote at this time. */
  1138. uint32_t watermark = ((base->WTMK_LVL & USDHC_WTMK_LVL_WR_WML_MASK) >>
  1139. USDHC_WTMK_LVL_WR_WML_SHIFT);
  1140. /* If DMA is enable, do not need to polling data port */
  1141. if (!(base->MIX_CTRL & USDHC_MIX_CTRL_DMAEN_MASK)) {
  1142. if (data->block_size % sizeof(uint32_t)) {
  1143. data->block_size +=
  1144. sizeof(uint32_t) -
  1145. (data->block_size % sizeof(uint32_t));
  1146. }
  1147. total_words =
  1148. ((data->block_count * data->block_size) /
  1149. sizeof(uint32_t));
  1150. if (watermark >= total_words) {
  1151. remaing_words = total_words;
  1152. } else if ((watermark < total_words) &&
  1153. ((total_words - xfered_words) >= watermark)) {
  1154. remaing_words = watermark;
  1155. } else {
  1156. remaing_words = (total_words - xfered_words);
  1157. }
  1158. i = 0U;
  1159. while (i < remaing_words) {
  1160. usdhc_write_data(base, data->tx_data[xfered_words++]);
  1161. i++;
  1162. }
  1163. }
  1164. return xfered_words;
  1165. }
  1166. static status_t usdhc_write_data_port_sync(struct usdhc_priv *priv)
  1167. {
  1168. USDHC_Type *base = priv->config->base;
  1169. struct usdhc_data *data = &priv->op_context.data;
  1170. uint32_t total_words;
  1171. uint32_t xfered_words = 0U, int_status = 0U;
  1172. int error = 0;
  1173. if (data->block_size % sizeof(uint32_t)) {
  1174. data->block_size +=
  1175. sizeof(uint32_t) - (data->block_size % sizeof(uint32_t));
  1176. }
  1177. total_words = (data->block_count * data->block_size) / sizeof(uint32_t);
  1178. while ((!error) && (xfered_words < total_words)) {
  1179. while (!(int_status & (USDHC_INT_BUF_WRITE_READY_FLAG |
  1180. USDHC_INT_DATA_ERR_FLAG |
  1181. USDHC_INT_TUNING_ERR_FLAG))) {
  1182. int_status = base->INT_STATUS;
  1183. }
  1184. if (int_status & USDHC_INT_TUNING_ERR_FLAG) {
  1185. base->INT_STATUS = USDHC_INT_TUNING_ERR_FLAG;
  1186. /* if tuning error occur ,return directly */
  1187. return -EIO;
  1188. } else if (int_status & USDHC_INT_DATA_ERR_FLAG) {
  1189. if (!(data->ignore_err))
  1190. error = -EIO;
  1191. /* clear data error flag */
  1192. base->INT_STATUS = USDHC_INT_DATA_ERR_FLAG;
  1193. }
  1194. if (!error) {
  1195. xfered_words = usdhc_write_data_port(priv,
  1196. xfered_words);
  1197. /* clear buffer write ready */
  1198. base->INT_STATUS = USDHC_INT_BUF_WRITE_READY_FLAG;
  1199. int_status = 0U;
  1200. }
  1201. }
  1202. /* Wait write data complete or data transfer error
  1203. * after the last writing operation.
  1204. */
  1205. while (!(int_status & (USDHC_INT_DATA_DONE_FLAG |
  1206. USDHC_INT_DATA_ERR_FLAG))) {
  1207. int_status = base->INT_STATUS;
  1208. }
  1209. if (int_status & USDHC_INT_DATA_ERR_FLAG) {
  1210. if (!(data->ignore_err))
  1211. error = -EIO;
  1212. }
  1213. base->INT_STATUS = USDHC_INT_DATA_DONE_FLAG |
  1214. USDHC_INT_DATA_ERR_FLAG;
  1215. return error;
  1216. }
  1217. static int usdhc_data_sync_xfer(struct usdhc_priv *priv, bool en_dma)
  1218. {
  1219. int error = 0;
  1220. uint32_t int_status = 0U;
  1221. USDHC_Type *base = priv->config->base;
  1222. struct usdhc_data *data = &priv->op_context.data;
  1223. if (en_dma) {
  1224. /* Wait data complete or USDHC encounters error. */
  1225. while (!((int_status &
  1226. (USDHC_INT_DATA_DONE_FLAG | USDHC_INT_DATA_ERR_FLAG |
  1227. USDHC_INT_CMD_ERR_FLAG | USDHC_INT_TUNING_ERR_FLAG)))) {
  1228. int_status = base->INT_STATUS;
  1229. }
  1230. if (int_status & USDHC_INT_TUNING_ERR_FLAG) {
  1231. error = -EIO;
  1232. } else if ((int_status & (USDHC_INT_DATA_ERR_FLAG |
  1233. USDHC_INT_DMA_ERR_FLAG))) {
  1234. if ((!(data->ignore_err)) ||
  1235. (int_status &
  1236. USDHC_INT_DATA_TIMEOUT_FLAG)) {
  1237. error = -EIO;
  1238. }
  1239. }
  1240. /* load dummy data */
  1241. if ((data->data_type == USDHC_XFER_BOOT_CONTINUOUS) && (!error))
  1242. *(data->rx_data) = g_usdhc_boot_dummy;
  1243. base->INT_STATUS = (USDHC_INT_DATA_DONE_FLAG |
  1244. USDHC_INT_DATA_ERR_FLAG |
  1245. USDHC_INT_DMA_ERR_FLAG |
  1246. USDHC_INT_TUNING_PASS_FLAG |
  1247. USDHC_INT_TUNING_ERR_FLAG);
  1248. } else {
  1249. if (data->rx_data) {
  1250. error = usdhc_read_data_port_sync(priv);
  1251. } else {
  1252. error = usdhc_write_data_port_sync(priv);
  1253. }
  1254. }
  1255. return error;
  1256. }
  1257. static int usdhc_xfer(struct usdhc_priv *priv)
  1258. {
  1259. int error = -EIO;
  1260. struct usdhc_data *data = NULL;
  1261. bool en_dma = true, execute_tuning;
  1262. USDHC_Type *base = priv->config->base;
  1263. if (!priv->op_context.cmd_only) {
  1264. data = &priv->op_context.data;
  1265. if (data->data_type == USDHC_XFER_TUNING)
  1266. execute_tuning = true;
  1267. else
  1268. execute_tuning = false;
  1269. } else {
  1270. execute_tuning = false;
  1271. }
  1272. /*check re-tuning request*/
  1273. if ((base->INT_STATUS & USDHC_INT_RE_TUNING_EVENT_FLAG)) {
  1274. base->INT_STATUS = USDHC_INT_RE_TUNING_EVENT_FLAG;
  1275. return -EAGAIN;
  1276. }
  1277. /* Update ADMA descriptor table according to different DMA mode
  1278. * (no DMA, ADMA1, ADMA2).
  1279. */
  1280. if (data && (!execute_tuning) && priv->op_context.dma_cfg.adma_table)
  1281. error = usdhc_adma_table_cfg(priv,
  1282. (data->data_type & USDHC_XFER_BOOT) ?
  1283. USDHC_ADMA_MUTI_FLAG : USDHC_ADMA_SINGLE_FLAG);
  1284. /* if the DMA descriptor configure fail or not needed , disable it */
  1285. if (error) {
  1286. en_dma = false;
  1287. /* disable DMA, using polling mode in this situation */
  1288. base->MIX_CTRL &= ~USDHC_MIX_CTRL_DMAEN_MASK;
  1289. base->PROT_CTRL &= ~USDHC_PROT_CTRL_DMASEL_MASK;
  1290. }
  1291. /* config the data transfer parameter */
  1292. error = usdhc_data_xfer_cfg(priv, en_dma);
  1293. if (error)
  1294. return error;
  1295. /* send command first */
  1296. usdhc_send_cmd(base, &priv->op_context.cmd);
  1297. /* wait command done */
  1298. error = usdhc_wait_cmd_done(priv, (data == NULL) ||
  1299. (data->data_type == USDHC_XFER_NORMAL));
  1300. /* wait transfer data finish */
  1301. if (data && (!error)) {
  1302. return usdhc_data_sync_xfer(priv, en_dma);
  1303. }
  1304. return error;
  1305. }
  1306. static inline void usdhc_select_1_8_vol(USDHC_Type *base, bool en_1_8_v)
  1307. {
  1308. if (en_1_8_v)
  1309. base->VEND_SPEC |= USDHC_VEND_SPEC_VSELECT_MASK;
  1310. else
  1311. base->VEND_SPEC &= ~USDHC_VEND_SPEC_VSELECT_MASK;
  1312. }
  1313. static inline void usdhc_force_clk_on(USDHC_Type *base, bool on)
  1314. {
  1315. if (on)
  1316. base->VEND_SPEC |= USDHC_VEND_SPEC_FRC_SDCLK_ON_MASK;
  1317. else
  1318. base->VEND_SPEC &= ~USDHC_VEND_SPEC_FRC_SDCLK_ON_MASK;
  1319. }
  1320. static void usdhc_tuning(USDHC_Type *base, uint32_t start, uint32_t step, bool enable)
  1321. {
  1322. uint32_t tuning_ctrl = 0U;
  1323. if (enable) {
  1324. /* feedback clock */
  1325. base->MIX_CTRL |= USDHC_MIX_CTRL_FBCLK_SEL_MASK;
  1326. /* config tuning start and step */
  1327. tuning_ctrl = base->TUNING_CTRL;
  1328. tuning_ctrl &= ~(USDHC_TUNING_CTRL_TUNING_START_TAP_MASK |
  1329. USDHC_TUNING_CTRL_TUNING_STEP_MASK);
  1330. tuning_ctrl |= (USDHC_TUNING_CTRL_TUNING_START_TAP(start) |
  1331. USDHC_TUNING_CTRL_TUNING_STEP(step) |
  1332. USDHC_TUNING_CTRL_STD_TUNING_EN_MASK);
  1333. base->TUNING_CTRL = tuning_ctrl;
  1334. /* excute tuning */
  1335. base->AUTOCMD12_ERR_STATUS |=
  1336. (USDHC_AUTOCMD12_ERR_STATUS_EXECUTE_TUNING_MASK |
  1337. USDHC_AUTOCMD12_ERR_STATUS_SMP_CLK_SEL_MASK);
  1338. } else {
  1339. /* disable the standard tuning */
  1340. base->TUNING_CTRL &= ~USDHC_TUNING_CTRL_STD_TUNING_EN_MASK;
  1341. /* clear excute tuning */
  1342. base->AUTOCMD12_ERR_STATUS &=
  1343. ~(USDHC_AUTOCMD12_ERR_STATUS_EXECUTE_TUNING_MASK |
  1344. USDHC_AUTOCMD12_ERR_STATUS_SMP_CLK_SEL_MASK);
  1345. }
  1346. }
  1347. int usdhc_adjust_tuning_timing(USDHC_Type *base, uint32_t delay)
  1348. {
  1349. uint32_t clk_tune_ctrl = 0U;
  1350. clk_tune_ctrl = base->CLK_TUNE_CTRL_STATUS;
  1351. clk_tune_ctrl &= ~USDHC_CLK_TUNE_CTRL_STATUS_DLY_CELL_SET_PRE_MASK;
  1352. clk_tune_ctrl |= USDHC_CLK_TUNE_CTRL_STATUS_DLY_CELL_SET_PRE(delay);
  1353. /* load the delay setting */
  1354. base->CLK_TUNE_CTRL_STATUS = clk_tune_ctrl;
  1355. /* check delat setting error */
  1356. if (base->CLK_TUNE_CTRL_STATUS &
  1357. (USDHC_CLK_TUNE_CTRL_STATUS_PRE_ERR_MASK |
  1358. USDHC_CLK_TUNE_CTRL_STATUS_NXT_ERR_MASK))
  1359. return -EIO;
  1360. return 0;
  1361. }
  1362. static inline void usdhc_set_retuning_timer(USDHC_Type *base, uint32_t counter)
  1363. {
  1364. base->HOST_CTRL_CAP &= ~USDHC_HOST_CTRL_CAP_TIME_COUNT_RETUNING_MASK;
  1365. base->HOST_CTRL_CAP |= USDHC_HOST_CTRL_CAP_TIME_COUNT_RETUNING(counter);
  1366. }
  1367. static inline void usdhc_set_bus_width(USDHC_Type *base,
  1368. enum usdhc_data_bus_width width)
  1369. {
  1370. base->PROT_CTRL = ((base->PROT_CTRL & ~USDHC_PROT_CTRL_DTW_MASK) |
  1371. USDHC_PROT_CTRL_DTW(width));
  1372. }
  1373. static int usdhc_execute_tuning(struct usdhc_priv *priv)
  1374. {
  1375. bool tuning_err = true;
  1376. int ret;
  1377. USDHC_Type *base = priv->config->base;
  1378. /* enable the standard tuning */
  1379. usdhc_tuning(base, SDHC_STANDARD_TUNING_START, SDHC_TUINIG_STEP, true);
  1380. while (true) {
  1381. /* send tuning block */
  1382. ret = usdhc_xfer(priv);
  1383. if (ret) {
  1384. return ret;
  1385. }
  1386. usdhc_millsec_delay(10);
  1387. /*wait excute tuning bit clear*/
  1388. if ((base->AUTOCMD12_ERR_STATUS &
  1389. USDHC_AUTOCMD12_ERR_STATUS_EXECUTE_TUNING_MASK)) {
  1390. continue;
  1391. }
  1392. /* if tuning error , re-tuning again */
  1393. if ((base->CLK_TUNE_CTRL_STATUS &
  1394. (USDHC_CLK_TUNE_CTRL_STATUS_NXT_ERR_MASK |
  1395. USDHC_CLK_TUNE_CTRL_STATUS_PRE_ERR_MASK)) &&
  1396. tuning_err) {
  1397. tuning_err = false;
  1398. /* enable the standard tuning */
  1399. usdhc_tuning(base, SDHC_STANDARD_TUNING_START,
  1400. SDHC_TUINIG_STEP, true);
  1401. usdhc_adjust_tuning_timing(base,
  1402. SDHC_STANDARD_TUNING_START);
  1403. } else {
  1404. break;
  1405. }
  1406. }
  1407. /* delay to wait the host controller stable */
  1408. usdhc_millsec_delay(1000);
  1409. /* check tuning result*/
  1410. if (!(base->AUTOCMD12_ERR_STATUS &
  1411. USDHC_AUTOCMD12_ERR_STATUS_SMP_CLK_SEL_MASK)) {
  1412. return -EIO;
  1413. }
  1414. usdhc_set_retuning_timer(base, SDHC_RETUNING_TIMER_COUNT);
  1415. return 0;
  1416. }
  1417. static int usdhc_vol_switch(struct usdhc_priv *priv)
  1418. {
  1419. USDHC_Type *base = priv->config->base;
  1420. int retry = 0xffff;
  1421. while (base->PRES_STATE &
  1422. (CARD_DATA1_STATUS_MASK | CARD_DATA2_STATUS_MASK |
  1423. CARD_DATA3_STATUS_MASK | CARD_DATA0_NOT_BUSY)) {
  1424. retry--;
  1425. if (retry <= 0) {
  1426. return -EACCES;
  1427. }
  1428. }
  1429. /* host switch to 1.8V */
  1430. usdhc_select_1_8_vol(base, true);
  1431. usdhc_millsec_delay(20000U);
  1432. /*enable force clock on*/
  1433. usdhc_force_clk_on(base, true);
  1434. /* dealy 1ms,not exactly correct when use while */
  1435. usdhc_millsec_delay(20000U);
  1436. /*disable force clock on*/
  1437. usdhc_force_clk_on(base, false);
  1438. /* check data line and cmd line status */
  1439. retry = 0xffff;
  1440. while (!(base->PRES_STATE &
  1441. (CARD_DATA1_STATUS_MASK | CARD_DATA2_STATUS_MASK |
  1442. CARD_DATA3_STATUS_MASK | CARD_DATA0_NOT_BUSY))) {
  1443. retry--;
  1444. if (retry <= 0) {
  1445. return -EBUSY;
  1446. }
  1447. }
  1448. return 0;
  1449. }
  1450. static inline void usdhc_op_ctx_init(struct usdhc_priv *priv,
  1451. bool cmd_only, uint8_t cmd_idx, uint32_t arg, enum sdhc_rsp_type rsp_type)
  1452. {
  1453. struct usdhc_cmd *cmd = &priv->op_context.cmd;
  1454. struct usdhc_data *data = &priv->op_context.data;
  1455. priv->op_context.cmd_only = cmd_only;
  1456. memset((char *)cmd, 0, sizeof(struct usdhc_cmd));
  1457. memset((char *)data, 0, sizeof(struct usdhc_data));
  1458. cmd->index = cmd_idx;
  1459. cmd->argument = arg;
  1460. cmd->rsp_type = rsp_type;
  1461. }
  1462. static int usdhc_select_fun(struct usdhc_priv *priv,
  1463. uint32_t group, uint32_t function)
  1464. {
  1465. const struct usdhc_config *config = priv->config;
  1466. uint32_t *fun_status;
  1467. uint16_t fun_grp_info[6U] = {0};
  1468. uint32_t current_fun_status = 0U, arg;
  1469. struct usdhc_cmd *cmd = &priv->op_context.cmd;
  1470. struct usdhc_data *data = &priv->op_context.data;
  1471. int ret;
  1472. /* check if card support CMD6 */
  1473. if ((priv->card_info.version <= SD_SPEC_VER1_0) ||
  1474. (!(priv->card_info.csd.cmd_class & SD_CMD_CLASS_SWITCH))) {
  1475. return -EINVAL;
  1476. }
  1477. /* Check if card support high speed mode. */
  1478. arg = (SD_SWITCH_CHECK << 31U | 0x00FFFFFFU);
  1479. arg &= ~((uint32_t)(0xFU) << (group * 4U));
  1480. arg |= (function << (group * 4U));
  1481. usdhc_op_ctx_init(priv, 0, SDHC_SWITCH, arg, SDHC_RSP_TYPE_R1);
  1482. data->block_size = 64U;
  1483. data->block_count = 1U;
  1484. data->rx_data = &g_usdhc_rx_dummy[0];
  1485. ret = usdhc_xfer(priv);
  1486. if (ret || (cmd->response[0U] & SDHC_R1ERR_All_FLAG))
  1487. return -EIO;
  1488. fun_status = data->rx_data;
  1489. /* Switch function status byte sequence
  1490. * from card is big endian(MSB first).
  1491. */
  1492. switch (config->endian) {
  1493. case USDHC_LITTLE_ENDIAN:
  1494. fun_status[0U] = SWAP_WORD_BYTE_SEQUENCE(fun_status[0U]);
  1495. fun_status[1U] = SWAP_WORD_BYTE_SEQUENCE(fun_status[1U]);
  1496. fun_status[2U] = SWAP_WORD_BYTE_SEQUENCE(fun_status[2U]);
  1497. fun_status[3U] = SWAP_WORD_BYTE_SEQUENCE(fun_status[3U]);
  1498. fun_status[4U] = SWAP_WORD_BYTE_SEQUENCE(fun_status[4U]);
  1499. break;
  1500. case USDHC_BIG_ENDIAN:
  1501. break;
  1502. case USDHC_HALF_WORD_BIG_ENDIAN:
  1503. fun_status[0U] = SWAP_HALF_WROD_BYTE_SEQUENCE(fun_status[0U]);
  1504. fun_status[1U] = SWAP_HALF_WROD_BYTE_SEQUENCE(fun_status[1U]);
  1505. fun_status[2U] = SWAP_HALF_WROD_BYTE_SEQUENCE(fun_status[2U]);
  1506. fun_status[3U] = SWAP_HALF_WROD_BYTE_SEQUENCE(fun_status[3U]);
  1507. fun_status[4U] = SWAP_HALF_WROD_BYTE_SEQUENCE(fun_status[4U]);
  1508. break;
  1509. default:
  1510. return -ENOTSUP;
  1511. }
  1512. fun_grp_info[5U] = (uint16_t)fun_status[0U];
  1513. fun_grp_info[4U] = (uint16_t)(fun_status[1U] >> 16U);
  1514. fun_grp_info[3U] = (uint16_t)(fun_status[1U]);
  1515. fun_grp_info[2U] = (uint16_t)(fun_status[2U] >> 16U);
  1516. fun_grp_info[1U] = (uint16_t)(fun_status[2U]);
  1517. fun_grp_info[0U] = (uint16_t)(fun_status[3U] >> 16U);
  1518. current_fun_status = ((fun_status[3U] & 0xFFU) << 8U) |
  1519. (fun_status[4U] >> 24U);
  1520. /* check if function is support */
  1521. if (((fun_grp_info[group] & (1 << function)) == 0U) ||
  1522. ((current_fun_status >>
  1523. (group * 4U)) & 0xFU) != function) {
  1524. return -ENOTSUP;
  1525. }
  1526. /* Switch to high speed mode. */
  1527. usdhc_op_ctx_init(priv, 0, SDHC_SWITCH, arg, SDHC_RSP_TYPE_R1);
  1528. data->block_size = 64U;
  1529. data->block_count = 1U;
  1530. data->rx_data = &g_usdhc_rx_dummy[0];
  1531. cmd->argument = (SD_SWITCH_SET << 31U | 0x00FFFFFFU);
  1532. cmd->argument &= ~((uint32_t)(0xFU) << (group * 4U));
  1533. cmd->argument |= (function << (group * 4U));
  1534. ret = usdhc_xfer(priv);
  1535. if (ret || (cmd->response[0U] & SDHC_R1ERR_All_FLAG))
  1536. return -EIO;
  1537. /* Switch function status byte sequence
  1538. * from card is big endian(MSB first).
  1539. */
  1540. switch (config->endian) {
  1541. case USDHC_LITTLE_ENDIAN:
  1542. fun_status[3U] = SWAP_WORD_BYTE_SEQUENCE(fun_status[3U]);
  1543. fun_status[4U] = SWAP_WORD_BYTE_SEQUENCE(fun_status[4U]);
  1544. break;
  1545. case USDHC_BIG_ENDIAN:
  1546. break;
  1547. case USDHC_HALF_WORD_BIG_ENDIAN:
  1548. fun_status[3U] = SWAP_HALF_WROD_BYTE_SEQUENCE(fun_status[3U]);
  1549. fun_status[4U] = SWAP_HALF_WROD_BYTE_SEQUENCE(fun_status[4U]);
  1550. break;
  1551. default:
  1552. return -ENOTSUP;
  1553. }
  1554. /* According to the "switch function status[bits 511~0]" return
  1555. * by switch command in mode "set function":
  1556. * -check if group 1 is successfully changed to function 1 by checking
  1557. * if bits 379~376 equal value 1;
  1558. */
  1559. current_fun_status = ((fun_status[3U] & 0xFFU) << 8U) |
  1560. (fun_status[4U] >> 24U);
  1561. if (((current_fun_status >>
  1562. (group * 4U)) & 0xFU) != function) {
  1563. return -EINVAL;
  1564. }
  1565. return 0;
  1566. }
  1567. uint32_t usdhc_set_sd_clk(USDHC_Type *base, uint32_t src_clk_hz, uint32_t sd_clk_hz)
  1568. {
  1569. uint32_t total_div = 0U;
  1570. uint32_t divisor = 0U;
  1571. uint32_t prescaler = 0U;
  1572. uint32_t sysctl = 0U;
  1573. uint32_t nearest_freq = 0U;
  1574. __ASSERT_NO_MSG(src_clk_hz != 0U);
  1575. __ASSERT_NO_MSG((sd_clk_hz != 0U) && (sd_clk_hz <= src_clk_hz));
  1576. /* calculate total divisor first */
  1577. total_div = src_clk_hz / sd_clk_hz;
  1578. if (total_div > (USDHC_MAX_CLKFS * USDHC_MAX_DVS)) {
  1579. return 0U;
  1580. }
  1581. if (total_div) {
  1582. /* calculate the divisor (src_clk_hz / divisor) <= sd_clk_hz */
  1583. if ((src_clk_hz / total_div) > sd_clk_hz)
  1584. total_div++;
  1585. /* divide the total divisor to div and prescaler */
  1586. if (total_div > USDHC_MAX_DVS) {
  1587. prescaler = total_div / USDHC_MAX_DVS;
  1588. /* prescaler must be a value which equal 2^n and
  1589. * smaller than SDHC_MAX_CLKFS
  1590. */
  1591. while (((USDHC_MAX_CLKFS % prescaler) != 0U) ||
  1592. (prescaler == 1U))
  1593. prescaler++;
  1594. /* calculate the divisor */
  1595. divisor = total_div / prescaler;
  1596. /* fine tuning the divisor until
  1597. * divisor * prescaler >= total_div
  1598. */
  1599. while ((divisor * prescaler) < total_div) {
  1600. divisor++;
  1601. if (divisor > USDHC_MAX_DVS) {
  1602. if ((prescaler <<= 1U) >
  1603. USDHC_MAX_CLKFS) {
  1604. return 0;
  1605. }
  1606. divisor = total_div / prescaler;
  1607. }
  1608. }
  1609. } else {
  1610. /* in this situation , divsior and SDCLKFS
  1611. * can generate same clock
  1612. * use SDCLKFS
  1613. */
  1614. if (((total_div % 2U) != 0U) & (total_div != 1U)) {
  1615. divisor = total_div;
  1616. prescaler = 1U;
  1617. } else {
  1618. divisor = 1U;
  1619. prescaler = total_div;
  1620. }
  1621. }
  1622. nearest_freq = src_clk_hz / (divisor == 0U ? 1U : divisor) /
  1623. prescaler;
  1624. } else {
  1625. /* in this condition , src_clk_hz = busClock_Hz, */
  1626. /* in DDR mode , set SDCLKFS to 0, divisor = 0, actually the
  1627. * totoal divider = 2U
  1628. */
  1629. divisor = 0U;
  1630. prescaler = 0U;
  1631. nearest_freq = src_clk_hz;
  1632. }
  1633. /* calculate the value write to register */
  1634. if (divisor != 0U) {
  1635. USDHC_PREV_DVS(divisor);
  1636. }
  1637. /* calculate the value write to register */
  1638. if (prescaler != 0U) {
  1639. USDHC_PREV_CLKFS(prescaler, 1U);
  1640. }
  1641. /* Set the SD clock frequency divisor, SD clock frequency select,
  1642. * data timeout counter value.
  1643. */
  1644. sysctl = base->SYS_CTRL;
  1645. sysctl &= ~(USDHC_SYS_CTRL_DVS_MASK |
  1646. USDHC_SYS_CTRL_SDCLKFS_MASK);
  1647. sysctl |= (USDHC_SYS_CTRL_DVS(divisor) |
  1648. USDHC_SYS_CTRL_SDCLKFS(prescaler));
  1649. base->SYS_CTRL = sysctl;
  1650. /* Wait until the SD clock is stable. */
  1651. while (!(base->PRES_STATE & USDHC_PRES_STATE_SDSTB_MASK)) {
  1652. ;
  1653. }
  1654. return nearest_freq;
  1655. }
  1656. static void usdhc_enable_ddr_mode(USDHC_Type *base,
  1657. bool enable, uint32_t nibble_pos)
  1658. {
  1659. uint32_t prescaler = (base->SYS_CTRL & USDHC_SYS_CTRL_SDCLKFS_MASK) >>
  1660. USDHC_SYS_CTRL_SDCLKFS_SHIFT;
  1661. if (enable) {
  1662. base->MIX_CTRL &= ~USDHC_MIX_CTRL_NIBBLE_POS_MASK;
  1663. base->MIX_CTRL |= (USDHC_MIX_CTRL_DDR_EN_MASK |
  1664. USDHC_MIX_CTRL_NIBBLE_POS(nibble_pos));
  1665. prescaler >>= 1U;
  1666. } else {
  1667. base->MIX_CTRL &= ~USDHC_MIX_CTRL_DDR_EN_MASK;
  1668. if (prescaler == 0U) {
  1669. prescaler += 1U;
  1670. } else {
  1671. prescaler <<= 1U;
  1672. }
  1673. }
  1674. base->SYS_CTRL = (base->SYS_CTRL & (~USDHC_SYS_CTRL_SDCLKFS_MASK)) |
  1675. USDHC_SYS_CTRL_SDCLKFS(prescaler);
  1676. }
  1677. static int usdhc_select_bus_timing(struct usdhc_priv *priv)
  1678. {
  1679. const struct usdhc_config *config = priv->config;
  1680. int error = -EIO;
  1681. if (priv->card_info.voltage != SD_VOL_1_8_V) {
  1682. /* Switch the card to high speed mode */
  1683. if (priv->host_capability.host_flags &
  1684. USDHC_SUPPORT_HIGHSPEED_FLAG) {
  1685. /* group 1, function 1 ->high speed mode*/
  1686. error = usdhc_select_fun(priv, SD_GRP_TIMING_MODE,
  1687. SD_TIMING_SDR25_HIGH_SPEED_MODE);
  1688. /* If the result isn't "switching to
  1689. * high speed mode(50MHZ)
  1690. * successfully or card doesn't support
  1691. * high speed
  1692. * mode". Return failed status.
  1693. */
  1694. if (!error) {
  1695. priv->card_info.sd_timing =
  1696. SD_TIMING_SDR25_HIGH_SPEED_MODE;
  1697. priv->card_info.busclk_hz =
  1698. usdhc_set_sd_clk(config->base,
  1699. priv->src_clk_hz,
  1700. SD_CLOCK_50MHZ);
  1701. } else if (error == -ENOTSUP) {
  1702. /* if not support high speed,
  1703. * keep the card work at default mode
  1704. */
  1705. return 0;
  1706. }
  1707. } else {
  1708. /* if not support high speed,
  1709. * keep the card work at default mode
  1710. */
  1711. return 0;
  1712. }
  1713. } else if ((USDHC_SUPPORT_SDR104_FLAG !=
  1714. SDMMCHOST_NOT_SUPPORT) ||
  1715. (USDHC_SUPPORT_SDR50_FLAG != SDMMCHOST_NOT_SUPPORT) ||
  1716. (USDHC_SUPPORT_DDR50_FLAG != SDMMCHOST_NOT_SUPPORT)) {
  1717. /* card is in UHS_I mode */
  1718. switch (priv->card_info.sd_timing) {
  1719. /* if not select timing mode,
  1720. * sdmmc will handle it automatically
  1721. */
  1722. case SD_TIMING_SDR12_DFT_MODE:
  1723. case SD_TIMING_SDR104_MODE:
  1724. error = usdhc_select_fun(priv, SD_GRP_TIMING_MODE,
  1725. SD_TIMING_SDR104_MODE);
  1726. if (!error) {
  1727. priv->card_info.sd_timing =
  1728. SD_TIMING_SDR104_MODE;
  1729. priv->card_info.busclk_hz =
  1730. usdhc_set_sd_clk(config->base,
  1731. priv->src_clk_hz,
  1732. SDMMCHOST_SUPPORT_SDR104_FREQ);
  1733. break;
  1734. }
  1735. case SD_TIMING_DDR50_MODE:
  1736. error = usdhc_select_fun(priv, SD_GRP_TIMING_MODE,
  1737. SD_TIMING_DDR50_MODE);
  1738. if (!error) {
  1739. priv->card_info.sd_timing =
  1740. SD_TIMING_DDR50_MODE;
  1741. priv->card_info.busclk_hz =
  1742. usdhc_set_sd_clk(
  1743. config->base,
  1744. priv->src_clk_hz,
  1745. SD_CLOCK_50MHZ);
  1746. usdhc_enable_ddr_mode(config->base, true, 0U);
  1747. }
  1748. break;
  1749. case SD_TIMING_SDR50_MODE:
  1750. error = usdhc_select_fun(priv,
  1751. SD_GRP_TIMING_MODE,
  1752. SD_TIMING_SDR50_MODE);
  1753. if (!error) {
  1754. priv->card_info.sd_timing =
  1755. SD_TIMING_SDR50_MODE;
  1756. priv->card_info.busclk_hz =
  1757. usdhc_set_sd_clk(
  1758. config->base,
  1759. priv->src_clk_hz,
  1760. SD_CLOCK_100MHZ);
  1761. }
  1762. break;
  1763. case SD_TIMING_SDR25_HIGH_SPEED_MODE:
  1764. error = usdhc_select_fun(priv, SD_GRP_TIMING_MODE,
  1765. SD_TIMING_SDR25_HIGH_SPEED_MODE);
  1766. if (!error) {
  1767. priv->card_info.sd_timing =
  1768. SD_TIMING_SDR25_HIGH_SPEED_MODE;
  1769. priv->card_info.busclk_hz =
  1770. usdhc_set_sd_clk(
  1771. config->base,
  1772. priv->src_clk_hz,
  1773. SD_CLOCK_50MHZ);
  1774. }
  1775. break;
  1776. default:
  1777. break;
  1778. }
  1779. }
  1780. /* SDR50 and SDR104 mode need tuning */
  1781. if ((priv->card_info.sd_timing == SD_TIMING_SDR50_MODE) ||
  1782. (priv->card_info.sd_timing == SD_TIMING_SDR104_MODE)) {
  1783. struct usdhc_cmd *cmd = &priv->op_context.cmd;
  1784. struct usdhc_data *data = &priv->op_context.data;
  1785. /* config IO strength in IOMUX*/
  1786. if (priv->card_info.sd_timing == SD_TIMING_SDR50_MODE) {
  1787. imxrt_usdhc_pinmux(config->nusdhc, false,
  1788. CARD_BUS_FREQ_100MHZ1,
  1789. CARD_BUS_STRENGTH_7);
  1790. } else {
  1791. imxrt_usdhc_pinmux(config->nusdhc, false,
  1792. CARD_BUS_FREQ_200MHZ,
  1793. CARD_BUS_STRENGTH_7);
  1794. }
  1795. /* execute tuning */
  1796. priv->op_context.cmd_only = 0;
  1797. memset((char *)cmd, 0, sizeof(struct usdhc_cmd));
  1798. memset((char *)data, 0, sizeof(struct usdhc_data));
  1799. cmd->index = SDHC_SEND_TUNING_BLOCK;
  1800. cmd->rsp_type = SDHC_RSP_TYPE_R1;
  1801. data->block_size = 64;
  1802. data->block_count = 1U;
  1803. data->rx_data = &g_usdhc_rx_dummy[0];
  1804. data->data_type = USDHC_XFER_TUNING;
  1805. error = usdhc_execute_tuning(priv);
  1806. if (error)
  1807. return error;
  1808. } else {
  1809. /* set default IO strength to 4 to cover card adapter driver
  1810. * strength difference
  1811. */
  1812. imxrt_usdhc_pinmux(config->nusdhc, false,
  1813. CARD_BUS_FREQ_100MHZ1,
  1814. CARD_BUS_STRENGTH_4);
  1815. }
  1816. return error;
  1817. }
  1818. static int usdhc_write_sector(void *bus_data, const uint8_t *buf, uint32_t sector,
  1819. uint32_t count)
  1820. {
  1821. struct usdhc_priv *priv = bus_data;
  1822. struct usdhc_cmd *cmd = &priv->op_context.cmd;
  1823. struct usdhc_data *data = &priv->op_context.data;
  1824. memset((char *)cmd, 0, sizeof(struct usdhc_cmd));
  1825. memset((char *)data, 0, sizeof(struct usdhc_data));
  1826. priv->op_context.cmd_only = 0;
  1827. cmd->index = SDHC_WRITE_MULTIPLE_BLOCK;
  1828. data->block_size = priv->card_info.sd_block_size;
  1829. data->block_count = count;
  1830. data->tx_data = (const uint32_t *)buf;
  1831. data->cmd12 = true;
  1832. if (data->block_count == 1U) {
  1833. cmd->index = SDHC_WRITE_BLOCK;
  1834. }
  1835. cmd->argument = sector;
  1836. if (!(priv->card_info.card_flags & SDHC_HIGH_CAPACITY_FLAG)) {
  1837. cmd->argument *= priv->card_info.sd_block_size;
  1838. }
  1839. cmd->rsp_type = SDHC_RSP_TYPE_R1;
  1840. cmd->rsp_err_flags = SDHC_R1ERR_All_FLAG;
  1841. return usdhc_xfer(priv);
  1842. }
  1843. static int usdhc_read_sector(void *bus_data, uint8_t *buf, uint32_t sector,
  1844. uint32_t count)
  1845. {
  1846. struct usdhc_priv *priv = bus_data;
  1847. struct usdhc_cmd *cmd = &priv->op_context.cmd;
  1848. struct usdhc_data *data = &priv->op_context.data;
  1849. memset((char *)cmd, 0, sizeof(struct usdhc_cmd));
  1850. memset((char *)data, 0, sizeof(struct usdhc_data));
  1851. priv->op_context.cmd_only = 0;
  1852. cmd->index = SDHC_READ_MULTIPLE_BLOCK;
  1853. data->block_size = priv->card_info.sd_block_size;
  1854. data->block_count = count;
  1855. data->rx_data = (uint32_t *)buf;
  1856. data->cmd12 = true;
  1857. if (data->block_count == 1U) {
  1858. cmd->index = SDHC_READ_SINGLE_BLOCK;
  1859. }
  1860. cmd->argument = sector;
  1861. if (!(priv->card_info.card_flags & SDHC_HIGH_CAPACITY_FLAG)) {
  1862. cmd->argument *= priv->card_info.sd_block_size;
  1863. }
  1864. cmd->rsp_type = SDHC_RSP_TYPE_R1;
  1865. cmd->rsp_err_flags = SDHC_R1ERR_All_FLAG;
  1866. return usdhc_xfer(priv);
  1867. }
  1868. static bool usdhc_set_sd_active(USDHC_Type *base)
  1869. {
  1870. uint32_t timeout = 0xffff;
  1871. base->SYS_CTRL |= USDHC_SYS_CTRL_INITA_MASK;
  1872. /* Delay some time to wait card become active state. */
  1873. while ((base->SYS_CTRL & USDHC_SYS_CTRL_INITA_MASK) ==
  1874. USDHC_SYS_CTRL_INITA_MASK) {
  1875. if (!timeout) {
  1876. break;
  1877. }
  1878. timeout--;
  1879. }
  1880. return ((!timeout) ? false : true);
  1881. }
  1882. static void usdhc_get_host_capability(USDHC_Type *base,
  1883. struct usdhc_capability *capability)
  1884. {
  1885. uint32_t host_cap;
  1886. uint32_t max_blk_len;
  1887. host_cap = base->HOST_CTRL_CAP;
  1888. /* Get the capability of USDHC. */
  1889. max_blk_len = ((host_cap & USDHC_HOST_CTRL_CAP_MBL_MASK) >>
  1890. USDHC_HOST_CTRL_CAP_MBL_SHIFT);
  1891. capability->max_blk_len = (512U << max_blk_len);
  1892. /* Other attributes not in HTCAPBLT register. */
  1893. capability->max_blk_cnt = USDHC_MAX_BLOCK_COUNT;
  1894. capability->host_flags = (host_cap & (USDHC_SUPPORT_ADMA_FLAG |
  1895. USDHC_SUPPORT_HIGHSPEED_FLAG | USDHC_SUPPORT_DMA_FLAG |
  1896. USDHC_SUPPORT_SUSPEND_RESUME_FLAG | USDHC_SUPPORT_V330_FLAG));
  1897. capability->host_flags |= (host_cap & USDHC_SUPPORT_V300_FLAG);
  1898. capability->host_flags |= (host_cap & USDHC_SUPPORT_V180_FLAG);
  1899. capability->host_flags |=
  1900. (host_cap & (USDHC_SUPPORT_DDR50_FLAG |
  1901. USDHC_SUPPORT_SDR104_FLAG |
  1902. USDHC_SUPPORT_SDR50_FLAG));
  1903. /* USDHC support 4/8 bit data bus width. */
  1904. capability->host_flags |= (USDHC_SUPPORT_4BIT_FLAG |
  1905. USDHC_SUPPORT_8BIT_FLAG);
  1906. }
  1907. static bool usdhc_hw_reset(USDHC_Type *base, uint32_t mask, uint32_t timeout)
  1908. {
  1909. base->SYS_CTRL |= (mask & (USDHC_SYS_CTRL_RSTA_MASK |
  1910. USDHC_SYS_CTRL_RSTC_MASK | USDHC_SYS_CTRL_RSTD_MASK));
  1911. /* Delay some time to wait reset success. */
  1912. while ((base->SYS_CTRL & mask)) {
  1913. if (!timeout) {
  1914. break;
  1915. }
  1916. timeout--;
  1917. }
  1918. return ((!timeout) ? false : true);
  1919. }
  1920. static void usdhc_host_hw_init(USDHC_Type *base,
  1921. const struct usdhc_config *config)
  1922. {
  1923. uint32_t proctl, sysctl, wml;
  1924. uint32_t int_mask;
  1925. __ASSERT_NO_MSG(config);
  1926. __ASSERT_NO_MSG((config->write_watermark >= 1U) &&
  1927. (config->write_watermark <= 128U));
  1928. __ASSERT_NO_MSG((config->read_watermark >= 1U) &&
  1929. (config->read_watermark <= 128U));
  1930. __ASSERT_NO_MSG(config->write_burst_len <= 16U);
  1931. /* Reset USDHC. */
  1932. usdhc_hw_reset(base, USDHC_RESET_ALL, 100U);
  1933. proctl = base->PROT_CTRL;
  1934. wml = base->WTMK_LVL;
  1935. sysctl = base->SYS_CTRL;
  1936. proctl &= ~(USDHC_PROT_CTRL_EMODE_MASK | USDHC_PROT_CTRL_DMASEL_MASK);
  1937. /* Endian mode*/
  1938. proctl |= USDHC_PROT_CTRL_EMODE(config->endian);
  1939. /* Watermark level */
  1940. wml &= ~(USDHC_WTMK_LVL_RD_WML_MASK |
  1941. USDHC_WTMK_LVL_WR_WML_MASK |
  1942. USDHC_WTMK_LVL_RD_BRST_LEN_MASK |
  1943. USDHC_WTMK_LVL_WR_BRST_LEN_MASK);
  1944. wml |= (USDHC_WTMK_LVL_RD_WML(config->read_watermark) |
  1945. USDHC_WTMK_LVL_WR_WML(config->write_watermark) |
  1946. USDHC_WTMK_LVL_RD_BRST_LEN(config->read_burst_len) |
  1947. USDHC_WTMK_LVL_WR_BRST_LEN(config->write_burst_len));
  1948. /* config the data timeout value */
  1949. sysctl &= ~USDHC_SYS_CTRL_DTOCV_MASK;
  1950. sysctl |= USDHC_SYS_CTRL_DTOCV(config->data_timeout);
  1951. base->SYS_CTRL = sysctl;
  1952. base->WTMK_LVL = wml;
  1953. base->PROT_CTRL = proctl;
  1954. /* disable internal DMA and DDR mode */
  1955. base->MIX_CTRL &= ~(USDHC_MIX_CTRL_DMAEN_MASK |
  1956. USDHC_MIX_CTRL_DDR_EN_MASK);
  1957. int_mask = (USDHC_INT_CMD_FLAG | USDHC_INT_CARD_DETECT_FLAG |
  1958. USDHC_INT_DATA_FLAG | USDHC_INT_SDR104_TUNING_FLAG |
  1959. USDHC_INT_BLK_GAP_EVENT_FLAG);
  1960. base->INT_STATUS_EN |= int_mask;
  1961. }
  1962. static void usdhc_cd_gpio_cb(const struct device *dev,
  1963. struct gpio_callback *cb, uint32_t pins)
  1964. {
  1965. struct usdhc_priv *priv =
  1966. CONTAINER_OF(cb, struct usdhc_priv, detect_cb);
  1967. const struct usdhc_config *config = priv->config;
  1968. gpio_pin_interrupt_configure(dev, config->detect_pin, GPIO_INT_DISABLE);
  1969. }
  1970. static int usdhc_cd_gpio_init(const struct device *detect_gpio,
  1971. uint32_t pin, gpio_dt_flags_t flags,
  1972. struct gpio_callback *callback)
  1973. {
  1974. int ret;
  1975. ret = gpio_pin_configure(detect_gpio, pin, GPIO_INPUT | flags);
  1976. if (ret)
  1977. return ret;
  1978. gpio_init_callback(callback, usdhc_cd_gpio_cb, BIT(pin));
  1979. return gpio_add_callback(detect_gpio, callback);
  1980. }
  1981. static void usdhc_host_reset(struct usdhc_priv *priv)
  1982. {
  1983. USDHC_Type *base = priv->config->base;
  1984. usdhc_select_1_8_vol(base, false);
  1985. usdhc_enable_ddr_mode(base, false, 0);
  1986. usdhc_tuning(base, SDHC_STANDARD_TUNING_START, SDHC_TUINIG_STEP, false);
  1987. #if FSL_FEATURE_USDHC_HAS_HS400_MODE
  1988. /* Disable HS400 mode */
  1989. /* Disable DLL */
  1990. #endif
  1991. }
  1992. static int usdhc_app_host_cmd(struct usdhc_priv *priv, int retry,
  1993. uint32_t arg, uint8_t app_cmd, uint32_t app_arg, enum sdhc_rsp_type rsp_type,
  1994. enum sdhc_rsp_type app_rsp_type, bool app_cmd_only)
  1995. {
  1996. struct usdhc_cmd *cmd = &priv->op_context.cmd;
  1997. int ret;
  1998. APP_CMD_XFER_AGAIN:
  1999. priv->op_context.cmd_only = 1;
  2000. cmd->index = SDHC_APP_CMD;
  2001. cmd->argument = arg;
  2002. cmd->rsp_type = rsp_type;
  2003. ret = usdhc_xfer(priv);
  2004. retry--;
  2005. if (ret && retry > 0) {
  2006. goto APP_CMD_XFER_AGAIN;
  2007. }
  2008. priv->op_context.cmd_only = app_cmd_only;
  2009. cmd->index = app_cmd;
  2010. cmd->argument = app_arg;
  2011. cmd->rsp_type = app_rsp_type;
  2012. ret = usdhc_xfer(priv);
  2013. if (ret && retry > 0) {
  2014. goto APP_CMD_XFER_AGAIN;
  2015. }
  2016. return ret;
  2017. }
  2018. static int usdhc_sd_init(struct usdhc_priv *priv)
  2019. {
  2020. const struct usdhc_config *config = priv->config;
  2021. USDHC_Type *base = config->base;
  2022. uint32_t app_cmd_41_arg = 0U;
  2023. int ret, retry;
  2024. struct usdhc_cmd *cmd = &priv->op_context.cmd;
  2025. struct usdhc_data *data = &priv->op_context.data;
  2026. if (!priv->host_ready) {
  2027. return -ENODEV;
  2028. }
  2029. /* reset variables */
  2030. priv->card_info.card_flags = 0U;
  2031. /* set DATA bus width 1bit at beginning*/
  2032. usdhc_set_bus_width(base, USDHC_DATA_BUS_WIDTH_1BIT);
  2033. /*set card freq to 400KHZ at begging*/
  2034. priv->card_info.busclk_hz =
  2035. usdhc_set_sd_clk(base, priv->src_clk_hz,
  2036. SDMMC_CLOCK_400KHZ);
  2037. /* send card active */
  2038. ret = usdhc_set_sd_active(base);
  2039. if (ret == false) {
  2040. return -EIO;
  2041. }
  2042. /* Get host capability. */
  2043. usdhc_get_host_capability(base, &priv->host_capability);
  2044. /* card go idle */
  2045. usdhc_op_ctx_init(priv, 1, SDHC_GO_IDLE_STATE, 0, SDHC_RSP_TYPE_NONE);
  2046. ret = usdhc_xfer(priv);
  2047. if (ret) {
  2048. return ret;
  2049. }
  2050. if (USDHC_SUPPORT_V330_FLAG != SDMMCHOST_NOT_SUPPORT) {
  2051. app_cmd_41_arg |= (SD_OCR_VDD32_33FLAG | SD_OCR_VDD33_34FLAG);
  2052. priv->card_info.voltage = SD_VOL_3_3_V;
  2053. } else if (USDHC_SUPPORT_V300_FLAG != SDMMCHOST_NOT_SUPPORT) {
  2054. app_cmd_41_arg |= SD_OCR_VDD29_30FLAG;
  2055. priv->card_info.voltage = SD_VOL_3_3_V;
  2056. }
  2057. /* allow user select the work voltage, if not select,
  2058. * sdmmc will handle it automatically
  2059. */
  2060. if (priv->config->no_1_8_v == false) {
  2061. if (USDHC_SUPPORT_V180_FLAG != SDMMCHOST_NOT_SUPPORT) {
  2062. app_cmd_41_arg |= SD_OCR_SWITCH_18_REQ_FLAG;
  2063. }
  2064. }
  2065. /* Check card's supported interface condition. */
  2066. usdhc_op_ctx_init(priv, 1, SDHC_SEND_IF_COND,
  2067. SDHC_VHS_3V3 | SDHC_CHECK, SDHC_RSP_TYPE_R7);
  2068. retry = 10;
  2069. while (retry) {
  2070. ret = usdhc_xfer(priv);
  2071. if (!ret) {
  2072. if ((cmd->response[0U] & 0xFFU) != SDHC_CHECK) {
  2073. ret = -ENOTSUP;
  2074. } else {
  2075. break;
  2076. }
  2077. }
  2078. retry--;
  2079. }
  2080. if (!ret) {
  2081. /* SDHC or SDXC card */
  2082. app_cmd_41_arg |= SD_OCR_HOST_CAP_FLAG;
  2083. priv->card_info.card_flags |= USDHC_SDHC_FLAG;
  2084. } else {
  2085. /* SDSC card */
  2086. LOG_ERR("USDHC SDSC not implemented yet!");
  2087. return -ENOTSUP;
  2088. }
  2089. /* Set card interface condition according to SDHC capability and
  2090. * card's supported interface condition.
  2091. */
  2092. APP_SEND_OP_COND_AGAIN:
  2093. usdhc_op_ctx_init(priv, 1, 0, 0, SDHC_RSP_TYPE_NONE);
  2094. ret = usdhc_app_host_cmd(priv, NXP_SDMMC_MAX_VOLTAGE_RETRIES, 0,
  2095. SDHC_APP_SEND_OP_COND, app_cmd_41_arg,
  2096. SDHC_RSP_TYPE_R1, SDHC_RSP_TYPE_R3, 1);
  2097. if (ret) {
  2098. LOG_ERR("APP Condition CMD failed:%d", ret);
  2099. return ret;
  2100. }
  2101. if (cmd->response[0U] & SD_OCR_PWR_BUSY_FLAG) {
  2102. /* high capacity check */
  2103. if (cmd->response[0U] & SD_OCR_CARD_CAP_FLAG) {
  2104. priv->card_info.card_flags |= SDHC_HIGH_CAPACITY_FLAG;
  2105. }
  2106. if (priv->config->no_1_8_v == false) {
  2107. /* 1.8V support */
  2108. if (cmd->response[0U] & SD_OCR_SWITCH_18_ACCEPT_FLAG) {
  2109. priv->card_info.card_flags |= SDHC_1800MV_FLAG;
  2110. }
  2111. }
  2112. priv->card_info.raw_ocr = cmd->response[0U];
  2113. } else {
  2114. goto APP_SEND_OP_COND_AGAIN;
  2115. }
  2116. /* check if card support 1.8V */
  2117. if ((priv->card_info.card_flags & USDHC_VOL_1_8V_FLAG)) {
  2118. usdhc_op_ctx_init(priv, 1, SDHC_VOL_SWITCH,
  2119. 0, SDHC_RSP_TYPE_R1);
  2120. ret = usdhc_xfer(priv);
  2121. if (!ret) {
  2122. ret = usdhc_vol_switch(priv);
  2123. }
  2124. if (ret) {
  2125. LOG_ERR("Voltage switch failed: %d", ret);
  2126. return ret;
  2127. }
  2128. priv->card_info.voltage = SD_VOL_1_8_V;
  2129. }
  2130. /* Initialize card if the card is SD card. */
  2131. usdhc_op_ctx_init(priv, 1, SDHC_ALL_SEND_CID, 0, SDHC_RSP_TYPE_R2);
  2132. ret = usdhc_xfer(priv);
  2133. if (!ret) {
  2134. memcpy(priv->card_info.raw_cid, cmd->response,
  2135. sizeof(priv->card_info.raw_cid));
  2136. sdhc_decode_cid(&priv->card_info.cid,
  2137. priv->card_info.raw_cid);
  2138. } else {
  2139. LOG_ERR("All send CID CMD failed: %d", ret);
  2140. return ret;
  2141. }
  2142. usdhc_op_ctx_init(priv, 1, SDHC_SEND_RELATIVE_ADDR,
  2143. 0, SDHC_RSP_TYPE_R6);
  2144. ret = usdhc_xfer(priv);
  2145. if (!ret) {
  2146. priv->card_info.relative_addr = (cmd->response[0U] >> 16U);
  2147. } else {
  2148. LOG_ERR("Send relative address CMD failed: %d", ret);
  2149. return ret;
  2150. }
  2151. usdhc_op_ctx_init(priv, 1, SDHC_SEND_CSD,
  2152. (priv->card_info.relative_addr << 16U), SDHC_RSP_TYPE_R2);
  2153. ret = usdhc_xfer(priv);
  2154. if (!ret) {
  2155. memcpy(priv->card_info.raw_csd, cmd->response,
  2156. sizeof(priv->card_info.raw_csd));
  2157. sdhc_decode_csd(&priv->card_info.csd, priv->card_info.raw_csd,
  2158. &priv->card_info.sd_block_count,
  2159. &priv->card_info.sd_block_size);
  2160. } else {
  2161. LOG_ERR("Send CSD CMD failed: %d", ret);
  2162. return ret;
  2163. }
  2164. usdhc_op_ctx_init(priv, 1, SDHC_SELECT_CARD,
  2165. priv->card_info.relative_addr << 16U,
  2166. SDHC_RSP_TYPE_R1);
  2167. ret = usdhc_xfer(priv);
  2168. if (ret || (cmd->response[0U] & SDHC_R1ERR_All_FLAG)) {
  2169. LOG_ERR("Select card CMD failed: %d", ret);
  2170. return -EIO;
  2171. }
  2172. usdhc_op_ctx_init(priv, 0, 0, 0, SDHC_RSP_TYPE_NONE);
  2173. data->block_size = 8;
  2174. data->block_count = 1;
  2175. data->rx_data = &priv->card_info.raw_scr[0];
  2176. ret = usdhc_app_host_cmd(priv, 1, (priv->card_info.relative_addr << 16),
  2177. SDHC_APP_SEND_SCR, 0,
  2178. SDHC_RSP_TYPE_R1, SDHC_RSP_TYPE_R1, 0);
  2179. if (ret) {
  2180. LOG_ERR("Send SCR following APP CMD failed: %d", ret);
  2181. return ret;
  2182. }
  2183. switch (config->endian) {
  2184. case USDHC_LITTLE_ENDIAN:
  2185. priv->card_info.raw_scr[0] =
  2186. SWAP_WORD_BYTE_SEQUENCE(priv->card_info.raw_scr[0]);
  2187. priv->card_info.raw_scr[1] =
  2188. SWAP_WORD_BYTE_SEQUENCE(priv->card_info.raw_scr[1]);
  2189. break;
  2190. case USDHC_BIG_ENDIAN:
  2191. break;
  2192. case USDHC_HALF_WORD_BIG_ENDIAN:
  2193. priv->card_info.raw_scr[0U] =
  2194. SWAP_HALF_WROD_BYTE_SEQUENCE(
  2195. priv->card_info.raw_scr[0U]);
  2196. priv->card_info.raw_scr[1U] =
  2197. SWAP_HALF_WROD_BYTE_SEQUENCE(
  2198. priv->card_info.raw_scr[1U]);
  2199. break;
  2200. default:
  2201. return -EINVAL;
  2202. }
  2203. sdhc_decode_scr(&priv->card_info.scr, priv->card_info.raw_scr,
  2204. &priv->card_info.version);
  2205. if (priv->card_info.scr.sd_width & 0x4U) {
  2206. priv->card_info.card_flags |=
  2207. USDHC_4BIT_WIDTH_FLAG;
  2208. }
  2209. /* speed class control cmd */
  2210. if (priv->card_info.scr.cmd_support & 0x01U) {
  2211. priv->card_info.card_flags |=
  2212. USDHC_SPEED_CLASS_CONTROL_CMD_FLAG;
  2213. }
  2214. /* set block count cmd */
  2215. if (priv->card_info.scr.cmd_support & 0x02U) {
  2216. priv->card_info.card_flags |=
  2217. USDHC_SET_BLK_CNT_CMD23_FLAG;
  2218. }
  2219. /* Set to max frequency in non-high speed mode. */
  2220. priv->card_info.busclk_hz = usdhc_set_sd_clk(base,
  2221. priv->src_clk_hz, SD_CLOCK_25MHZ);
  2222. /* Set to 4-bit data bus mode. */
  2223. if ((priv->host_capability.host_flags & USDHC_SUPPORT_4BIT_FLAG) &&
  2224. (priv->card_info.card_flags & USDHC_4BIT_WIDTH_FLAG)) {
  2225. usdhc_op_ctx_init(priv, 1, 0, 0, SDHC_RSP_TYPE_NONE);
  2226. ret = usdhc_app_host_cmd(priv, 1,
  2227. (priv->card_info.relative_addr << 16),
  2228. SDHC_APP_SET_BUS_WIDTH, 2,
  2229. SDHC_RSP_TYPE_R1, SDHC_RSP_TYPE_R1, 1);
  2230. if (ret) {
  2231. LOG_ERR("Set bus width failed: %d", ret);
  2232. return ret;
  2233. }
  2234. usdhc_set_bus_width(base, USDHC_DATA_BUS_WIDTH_4BIT);
  2235. }
  2236. if (priv->card_info.version >= SD_SPEC_VER3_0) {
  2237. /* set sd card driver strength */
  2238. ret = usdhc_select_fun(priv, SD_GRP_DRIVER_STRENGTH_MODE,
  2239. priv->card_info.driver_strength);
  2240. if (ret) {
  2241. LOG_ERR("Set SD driver strength failed: %d", ret);
  2242. return ret;
  2243. }
  2244. /* set sd card current limit */
  2245. ret = usdhc_select_fun(priv, SD_GRP_CURRENT_LIMIT_MODE,
  2246. priv->card_info.max_current);
  2247. if (ret) {
  2248. LOG_ERR("Set SD current limit failed: %d", ret);
  2249. return ret;
  2250. }
  2251. }
  2252. /* set block size */
  2253. usdhc_op_ctx_init(priv, 1, SDHC_SET_BLOCK_SIZE,
  2254. priv->card_info.sd_block_size, SDHC_RSP_TYPE_R1);
  2255. ret = usdhc_xfer(priv);
  2256. if (ret || cmd->response[0U] & SDHC_R1ERR_All_FLAG) {
  2257. LOG_ERR("Set block size failed: %d", ret);
  2258. return -EIO;
  2259. }
  2260. if (priv->card_info.version > SD_SPEC_VER1_0) {
  2261. /* select bus timing */
  2262. ret = usdhc_select_bus_timing(priv);
  2263. if (ret) {
  2264. LOG_ERR("Select bus timing failed: %d", ret);
  2265. return ret;
  2266. }
  2267. }
  2268. retry = 10;
  2269. ret = -EIO;
  2270. while (ret && retry >= 0) {
  2271. ret = usdhc_read_sector(priv, (uint8_t *)g_usdhc_rx_dummy, 0, 1);
  2272. if (!ret) {
  2273. break;
  2274. }
  2275. retry--;
  2276. }
  2277. if (ret) {
  2278. LOG_ERR("USDHC bus device initalization failed!");
  2279. }
  2280. return ret;
  2281. }
  2282. static K_MUTEX_DEFINE(z_usdhc_init_lock);
  2283. static int usdhc_board_access_init(struct usdhc_priv *priv)
  2284. {
  2285. const struct usdhc_config *config = priv->config;
  2286. int ret = 0;
  2287. uint32_t gpio_level;
  2288. USDHC_Type *base = config->base;
  2289. if (config->pwr_name) {
  2290. priv->pwr_gpio = device_get_binding(config->pwr_name);
  2291. if (!priv->pwr_gpio) {
  2292. return -ENODEV;
  2293. }
  2294. }
  2295. if (config->detect_name) {
  2296. priv->detect_type = SD_DETECT_GPIO_CD;
  2297. priv->detect_gpio = device_get_binding(config->detect_name);
  2298. if (!priv->detect_gpio) {
  2299. return -ENODEV;
  2300. }
  2301. }
  2302. if (priv->pwr_gpio) {
  2303. ret = gpio_pin_configure(priv->pwr_gpio,
  2304. config->pwr_pin,
  2305. GPIO_OUTPUT_ACTIVE |
  2306. config->pwr_flags);
  2307. if (ret) {
  2308. return ret;
  2309. }
  2310. /* 100ms delay to make sure SD card is stable,
  2311. * maybe could be shorter
  2312. */
  2313. k_busy_wait(100000);
  2314. }
  2315. if (!priv->detect_gpio) {
  2316. LOG_INF("USDHC detection other than GPIO");
  2317. /* DATA3 does not monitor card insertion */
  2318. base->PROT_CTRL &= ~USDHC_PROT_CTRL_D3CD_MASK;
  2319. if ((base->PRES_STATE & USDHC_PRES_STATE_CINST_MASK) != 0) {
  2320. priv->inserted = true;
  2321. } else {
  2322. priv->inserted = false;
  2323. return -ENODEV;
  2324. }
  2325. } else {
  2326. ret = usdhc_cd_gpio_init(priv->detect_gpio,
  2327. config->detect_pin,
  2328. config->detect_flags,
  2329. &priv->detect_cb);
  2330. if (ret) {
  2331. return ret;
  2332. }
  2333. ret = gpio_pin_get(priv->detect_gpio, config->detect_pin);
  2334. if (ret < 0) {
  2335. return ret;
  2336. }
  2337. gpio_level = ret;
  2338. if (gpio_level == 0) {
  2339. priv->inserted = false;
  2340. LOG_ERR("NO SD inserted!");
  2341. return -ENODEV;
  2342. }
  2343. priv->inserted = true;
  2344. LOG_INF("SD inserted!");
  2345. }
  2346. return 0;
  2347. }
  2348. static int usdhc_access_init(const struct device *dev)
  2349. {
  2350. const struct usdhc_config *config = dev->config;
  2351. struct usdhc_priv *priv = dev->data;
  2352. int ret;
  2353. (void)k_mutex_lock(&z_usdhc_init_lock, K_FOREVER);
  2354. memset((char *)priv, 0, sizeof(struct usdhc_priv));
  2355. priv->config = config;
  2356. if (!config->base) {
  2357. k_mutex_unlock(&z_usdhc_init_lock);
  2358. return -ENODEV;
  2359. }
  2360. if (clock_control_get_rate(config->clock_dev,
  2361. config->clock_subsys,
  2362. &priv->src_clk_hz)) {
  2363. return -EINVAL;
  2364. }
  2365. ret = usdhc_board_access_init(priv);
  2366. if (ret) {
  2367. k_mutex_unlock(&z_usdhc_init_lock);
  2368. return ret;
  2369. }
  2370. priv->op_context.dma_cfg.dma_mode = USDHC_DMA_ADMA2;
  2371. priv->op_context.dma_cfg.burst_len = USDHC_INCR_BURST_LEN;
  2372. /*No DMA used for this Version*/
  2373. priv->op_context.dma_cfg.adma_table = 0;
  2374. priv->op_context.dma_cfg.adma_table_words = USDHC_ADMA_TABLE_WORDS;
  2375. usdhc_host_hw_init(config->base, config);
  2376. priv->host_ready = 1;
  2377. usdhc_host_reset(priv);
  2378. ret = usdhc_sd_init(priv);
  2379. k_mutex_unlock(&z_usdhc_init_lock);
  2380. return ret;
  2381. }
  2382. static int disk_usdhc_access_status(struct disk_info *disk)
  2383. {
  2384. const struct device *dev = disk->dev;
  2385. struct usdhc_priv *priv = dev->data;
  2386. return priv->status;
  2387. }
  2388. static int disk_usdhc_access_read(struct disk_info *disk, uint8_t *buf,
  2389. uint32_t sector, uint32_t count)
  2390. {
  2391. const struct device *dev = disk->dev;
  2392. struct usdhc_priv *priv = dev->data;
  2393. LOG_DBG("sector=%u count=%u", sector, count);
  2394. return usdhc_read_sector(priv, buf, sector, count);
  2395. }
  2396. static int disk_usdhc_access_write(struct disk_info *disk, const uint8_t *buf,
  2397. uint32_t sector, uint32_t count)
  2398. {
  2399. const struct device *dev = disk->dev;
  2400. struct usdhc_priv *priv = dev->data;
  2401. LOG_DBG("sector=%u count=%u", sector, count);
  2402. return usdhc_write_sector(priv, buf, sector, count);
  2403. }
  2404. static int disk_usdhc_access_ioctl(struct disk_info *disk, uint8_t cmd, void *buf)
  2405. {
  2406. const struct device *dev = disk->dev;
  2407. struct usdhc_priv *priv = dev->data;
  2408. int err;
  2409. err = sdhc_map_disk_status(priv->status);
  2410. if (err != 0) {
  2411. return err;
  2412. }
  2413. switch (cmd) {
  2414. case DISK_IOCTL_CTRL_SYNC:
  2415. break;
  2416. case DISK_IOCTL_GET_SECTOR_COUNT:
  2417. *(uint32_t *)buf = priv->card_info.sd_block_count;
  2418. break;
  2419. case DISK_IOCTL_GET_SECTOR_SIZE:
  2420. *(uint32_t *)buf = priv->card_info.sd_block_size;
  2421. break;
  2422. case DISK_IOCTL_GET_ERASE_BLOCK_SZ:
  2423. *(uint32_t *)buf = priv->card_info.sd_block_size;
  2424. break;
  2425. default:
  2426. return -EINVAL;
  2427. }
  2428. return 0;
  2429. }
  2430. static int disk_usdhc_access_init(struct disk_info *disk)
  2431. {
  2432. const struct device *dev = disk->dev;
  2433. struct usdhc_priv *priv = dev->data;
  2434. if (priv->status == DISK_STATUS_OK) {
  2435. /* Called twice, don't re-init. */
  2436. return 0;
  2437. }
  2438. return usdhc_access_init(dev);
  2439. }
  2440. static const struct disk_operations usdhc_disk_ops = {
  2441. .init = disk_usdhc_access_init,
  2442. .status = disk_usdhc_access_status,
  2443. .read = disk_usdhc_access_read,
  2444. .write = disk_usdhc_access_write,
  2445. .ioctl = disk_usdhc_access_ioctl,
  2446. };
  2447. static struct disk_info usdhc_disk = {
  2448. .name = CONFIG_SDMMC_VOLUME_NAME,
  2449. .ops = &usdhc_disk_ops,
  2450. };
  2451. static int disk_usdhc_init(const struct device *dev)
  2452. {
  2453. struct usdhc_priv *priv = dev->data;
  2454. priv->status = DISK_STATUS_UNINIT;
  2455. usdhc_disk.dev = dev;
  2456. return disk_access_register(&usdhc_disk);
  2457. }
  2458. #define DISK_ACCESS_USDHC_INIT_NONE(n)
  2459. #define DISK_ACCESS_USDHC_INIT_PWR_PROPS(n) \
  2460. .pwr_name = DT_INST_GPIO_LABEL(n, pwr_gpios), \
  2461. .pwr_pin = DT_INST_GPIO_PIN(n, pwr_gpios), \
  2462. .pwr_flags = DT_INST_GPIO_FLAGS(n, pwr_gpios),
  2463. #define DISK_ACCESS_USDHC_INIT_CD_PROPS(n) \
  2464. .detect_name = DT_INST_GPIO_LABEL(n, cd_gpios), \
  2465. .detect_pin = DT_INST_GPIO_PIN(n, cd_gpios), \
  2466. .detect_flags = DT_INST_GPIO_FLAGS(n, cd_gpios),
  2467. #define DISK_ACCESS_USDHC_INIT_PWR(n) \
  2468. COND_CODE_1(DT_INST_NODE_HAS_PROP(n, pwr_gpios), \
  2469. (DISK_ACCESS_USDHC_INIT_PWR_PROPS(n)), \
  2470. (DISK_ACCESS_USDHC_INIT_NONE(n)))
  2471. #define DISK_ACCESS_USDHC_INIT_CD(n) \
  2472. COND_CODE_1(DT_INST_NODE_HAS_PROP(n, cd_gpios), \
  2473. (DISK_ACCESS_USDHC_INIT_CD_PROPS(n)), \
  2474. (DISK_ACCESS_USDHC_INIT_NONE(n)))
  2475. #define DISK_ACCESS_USDHC_INIT(n) \
  2476. static const struct usdhc_config usdhc_config_##n = { \
  2477. .base = (USDHC_Type *) DT_INST_REG_ADDR(n), \
  2478. .clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \
  2479. .clock_subsys = \
  2480. (clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name), \
  2481. .nusdhc = n, \
  2482. DISK_ACCESS_USDHC_INIT_PWR(n) \
  2483. DISK_ACCESS_USDHC_INIT_CD(n) \
  2484. .no_1_8_v = DT_INST_PROP(n, no_1_8_v), \
  2485. .data_timeout = USDHC_DATA_TIMEOUT, \
  2486. .endian = USDHC_LITTLE_ENDIAN, \
  2487. .read_watermark = USDHC_READ_WATERMARK_LEVEL, \
  2488. .write_watermark = USDHC_WRITE_WATERMARK_LEVEL, \
  2489. .read_burst_len = USDHC_READ_BURST_LEN, \
  2490. .write_burst_len = USDHC_WRITE_BURST_LEN, \
  2491. }; \
  2492. \
  2493. static struct usdhc_priv usdhc_priv_##n; \
  2494. \
  2495. DEVICE_DT_INST_DEFINE(n, \
  2496. &disk_usdhc_init, \
  2497. NULL, \
  2498. &usdhc_priv_##n, \
  2499. &usdhc_config_##n, \
  2500. POST_KERNEL, \
  2501. CONFIG_SDMMC_INIT_PRIORITY, \
  2502. NULL);
  2503. DT_INST_FOREACH_STATUS_OKAY(DISK_ACCESS_USDHC_INIT)