hal_sdhost.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532
  1. /**
  2. * @file hal_sdhost.c
  3. * @author ALLWINNERTECH IOT WLAN Team
  4. */
  5. /*
  6. * Copyright (C) 2017 ALLWINNERTECH TECHNOLOGY CO., LTD. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name of ALLWINNERTECH TECHNOLOGY CO., LTD. nor the names of
  18. * its contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. * LIMITED TO, )|hhst->sdio_irq_maskPROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include "_sdhost.h"
  34. #include "sunxi_hal_common.h"
  35. #include "stdint.h"
  36. //#include "io.h"
  37. //#include "hal_prcm.h"
  38. #include "hal_ccm.h"
  39. #include "hal_gpio.h"
  40. #include "hal_board.h"
  41. #include "hal_dev.h"
  42. #include "hal_clock.h"
  43. #include "sdmmc/sys/param.h"
  44. #include "hal_sdhost.h"
  45. #ifdef CONFIG_USE_SDIO
  46. #include "sdio.h"
  47. #endif
  48. #include "sdmmc.h"
  49. #include "_sd_define.h"
  50. #include "_core.h"
  51. #ifndef CONFIG_KERNEL_FREERTOS
  52. #include "osal/os/RT-Thread/os_util.h"
  53. #else
  54. #include "osal/os/FreeRTOS/os_util.h"
  55. #endif
  56. #include "interrupt.h"
  57. //#include "k_arch.h"
  58. extern uint32_t sdmmc_pinctrl_init(struct mmc_host *host);
  59. extern int mmc_gpiod_request_cd_irq(struct mmc_host* host);
  60. //#define __dsb__() dsb(0xf)
  61. #ifndef UINT_MAX
  62. #define UINT_MAX (~0U)
  63. #endif
  64. /*
  65. #define SDC_REQUEST_IRQ(n, hdl) HAL_NVIC_SetIRQHandler(n, hdl)
  66. #define SDC_SetPriority(n, l) HAL_NVIC_SetPriority(n, l)
  67. #define SDC_ENABLE_IRQ(n) HAL_NVIC_EnableIRQ(n)
  68. #define SDC_CONFIG_IRQ(n, hdl, l) HAL_NVIC_ConfigExtIRQ(n, hdl, l)
  69. #define SDC_DISABLE_IRQ(n) HAL_NVIC_DisableIRQ(n)
  70. #define SDC_CLEAR_IRQPINGD(n) HAL_NVIC_ClearPendingIRQ(n)
  71. #define SDC_IRQHandler NVIC_IRQHandler
  72. */
  73. #define SDC_REQUEST_IRQ(n, hdl,d) ({SDC_LOGD("%s,%d,sdc %d irq request \n",__func__, __LINE__, d->sdc_id);\
  74. request_irq(n, hdl, 0, "sdmmc",d);})
  75. #define SDC_FREE_IRQ(n, d) (free_irq(n, d))
  76. #define SDC_SetPriority(n, l) ({SDC_LOGN("Not imp %s,%d\n", __FUNCTION__,__LINE__);})
  77. #define SDC_ENABLE_IRQ(n) enable_irq(n)
  78. #define SDC_CONFIG_IRQ(n, hdl, l) ({SDC_LOGN("Not imp %s,%d\n", __FUNCTION__,__LINE__);})
  79. #define SDC_DISABLE_IRQ(n) disable_irq(n)
  80. //#define SDC_CLEAR_IRQPINGD(n) gic_clear_pending(n)
  81. #define SDC_CLEAR_IRQPINGD(n) ({SDC_LOGN("Not imp %s,%d\n", __FUNCTION__,__LINE__);})
  82. //#define SDC_CLEAR_IRQPINGD(n) free_irq(n)
  83. //#define SDC_IRQHandler interrupt_handler_t
  84. #define SDC_IRQHandler irq_handler_t *
  85. #define NUSE_STANDARD_INTERFACE 1
  86. #ifdef NUSE_STANDARD_INTERFACE
  87. #define SDC1_SUPPORT 1
  88. #define SDC_CCM_SDC0_SCLK_CTRL (SDC_CCM_BASE + 0x830)
  89. #define SDC_CCM_SDC1_SCLK_CTRL (SDC_CCM_BASE + 0x834)
  90. #define SDC_CCM_SDC_BUS_GATE_RESET (SDC_CCM_BASE + 0x84c)
  91. /*
  92. #define SDC0_CCM_BusForceReset() HAL_CCM_BusForcePeriphReset(CCM_BUS_PERIPH_BIT_SDC0)
  93. #define SDC0_CCM_BusReleaseRest() HAL_CCM_BusReleasePeriphReset(CCM_BUS_PERIPH_BIT_SDC0)
  94. #define SDC0_CCM_BusEnableClock() HAL_CCM_BusEnablePeriphClock(CCM_BUS_PERIPH_BIT_SDC0)
  95. #define SDC0_CCM_BusDisableClock() HAL_CCM_BusDisablePeriphClock(CCM_BUS_PERIPH_BIT_SDC0)
  96. #define SDC0_CCM_EnableMClock() HAL_CCM_SDC0_EnableMClock()
  97. #define SDC0_DisableMClock HAL_CCM_SDC0_DisableMClock
  98. #define SDC0_SetMClock HAL_CCM_SDC0_SetMClock
  99. */
  100. #define SDC0_CCM_BusForceReset() ({int v = readl(SDC_CCM_SDC_BUS_GATE_RESET); v &= ~(1<<16); writel(v,SDC_CCM_SDC_BUS_GATE_RESET);})
  101. #define SDC0_CCM_BusReleaseRest() ({int v = readl(SDC_CCM_SDC_BUS_GATE_RESET); v |= (1<<16); writel(v,SDC_CCM_SDC_BUS_GATE_RESET);})
  102. #define SDC0_CCM_BusEnableClock() ({int v = readl(SDC_CCM_SDC_BUS_GATE_RESET); v |= (1<<0); writel(v,SDC_CCM_SDC_BUS_GATE_RESET);})
  103. #define SDC0_CCM_BusDisableClock() ({int v = readl(SDC_CCM_SDC_BUS_GATE_RESET); v &= ~(1<<0); writel(v,SDC_CCM_SDC_BUS_GATE_RESET);})
  104. #define SDC0_CCM_EnableMClock() ({int v = readl(SDC_CCM_SDC0_SCLK_CTRL); v |= (1<<31); writel(v,SDC_CCM_SDC0_SCLK_CTRL);})
  105. #define SDC0_DisableMClock() ({int v = readl(SDC_CCM_SDC0_SCLK_CTRL); v &= ~(1<<31); writel(v,SDC_CCM_SDC0_SCLK_CTRL);})
  106. #define SDC1_CCM_BusForceReset() ({int v = readl(SDC_CCM_SDC_BUS_GATE_RESET); v &= ~(1<<17); writel(v,SDC_CCM_SDC_BUS_GATE_RESET);})
  107. #define SDC1_CCM_BusReleaseRest() ({int v = readl(SDC_CCM_SDC_BUS_GATE_RESET); v |= (1<<17); writel(v,SDC_CCM_SDC_BUS_GATE_RESET);})
  108. #define SDC1_CCM_BusEnableClock() ({int v = readl(SDC_CCM_SDC_BUS_GATE_RESET); v |= (1<<1); writel(v,SDC_CCM_SDC_BUS_GATE_RESET);})
  109. #define SDC1_CCM_BusDisableClock() ({int v = readl(SDC_CCM_SDC_BUS_GATE_RESET); v &= ~(1<<1); writel(v,SDC_CCM_SDC_BUS_GATE_RESET);})
  110. #define SDC1_CCM_EnableMClock() ({int v = readl(SDC_CCM_SDC1_SCLK_CTRL); v |= (1<<31); writel(v,SDC_CCM_SDC1_SCLK_CTRL);})
  111. #define SDC1_DisableMClock() ({int v = readl(SDC_CCM_SDC1_SCLK_CTRL); v &= ~(1<<31); writel(v,SDC_CCM_SDC1_SCLK_CTRL);})
  112. #define SDC1_SET_GPIO_MUX() ({int v = readl(SDC_GPIO_BASE + 0xd8);\
  113. SDC_LOGD("RAW SET SDC1 MUX\n");\
  114. v &= ~(0xffffff); v |=0x222222;\
  115. writel(v,SDC_GPIO_BASE + 0xd8);\
  116. SDC_LOGD("RAW SDC1 MUX %x\n", readl(SDC_GPIO_BASE + 0xd8));})
  117. #define SDC1_SET_GPIO_DRV() ({int v = readl(SDC_GPIO_BASE + 0xec);\
  118. SDC_LOGD("RAW SET SDC1 DRV\n");\
  119. v &= ~(0xfff);\
  120. v |=0x000; writel(v,SDC_GPIO_BASE + 0xec);\
  121. SDC_LOGD("RAW SDC1 DRV %x\n",readl(SDC_GPIO_BASE + 0xec));})
  122. #define SDC1_SET_GPIO_PULL() ({int v = readl(SDC_GPIO_BASE + 0xf4);\
  123. SDC_LOGD("RAW SET SDC1 PULL\n");\
  124. v &= ~(0xfff);\
  125. v |=0x555; writel(v,SDC_GPIO_BASE + 0xf4);\
  126. SDC_LOGD("RAW SDC1 PULL %x\n",readl(SDC_GPIO_BASE + 0xf4));})
  127. #define SDC_TIG_SET_GPIO_MUX() ({int v = readl(SDC_GPIO_BASE + 0x28);\
  128. SDC_LOGD("+RAW SET TIG SDC MUX %x\n", SDC_GPIO_BASE + 0x28);\
  129. v &= ~(0x0000F000); v |=0x00001000;\
  130. writel(v,SDC_GPIO_BASE + 0x28);\
  131. SDC_LOGD("-RAW SDC TIG MUX %x\n", readl(SDC_GPIO_BASE + 0x28));})
  132. #define SDC_TIG_SET_GPIO_DRV() ({int v = readl(SDC_GPIO_BASE + 0x38);\
  133. SDC_LOGD("+RAW SET SDC TIG DRV %x\n", SDC_GPIO_BASE + 0x38);\
  134. v &= ~(0x00C00000);\
  135. v |=0x00C00000; writel(v,SDC_GPIO_BASE + 0x38);\
  136. SDC_LOGD("-RAW SDC TRG DRV %x\n",readl(SDC_GPIO_BASE + 0x38));})
  137. #define SDC_TIG_SET_GPIO_PULL() ({int v = readl(SDC_GPIO_BASE + 0x40);\
  138. SDC_LOGD("+RAW SET SDC TRG PULL %x\n", SDC_GPIO_BASE + 0x40);\
  139. v &= ~(0x00C00000);\
  140. v |=0x00400000; writel(v,SDC_GPIO_BASE + 0x40);\
  141. SDC_LOGD("-RAW SDC1 PULL %x\n",readl(SDC_GPIO_BASE + 0x40));})
  142. #define SDC_TIG_SET_GPIO_DAT() ({int v = readl(SDC_GPIO_BASE + 0x34);\
  143. SDC_LOGD("+raw set sdc trg dat %x\n",SDC_GPIO_BASE + 0x34);\
  144. v &= ~(0x00000800);\
  145. v |=0x00000800; writel(v,SDC_GPIO_BASE + 0x34);\
  146. SDC_LOGD("-RAW SDC1 DAT %x\n",readl(SDC_GPIO_BASE + 0x34));})
  147. #define SDC_TIG_CLR_GPIO_DAT() ({int v = readl(SDC_GPIO_BASE + 0x34);\
  148. SDC_LOGD("RAW SET SDC TRG dat\n");\
  149. v &= ~(0x00000800);\
  150. v |=0x00000000; writel(v,SDC_GPIO_BASE + 0x34);\
  151. SDC_LOGD("RAW SDC1 dat %x\n",readl(SDC_GPIO_BASE + 0x34));})
  152. #define HAL_BoardIoctl(a,b,c) ({SDC_LOGN("NOT IMP %s,%d\n",__FUNCTION__, __LINE__);})
  153. #define HAL_GPIO_ReadPin(cd_gpios, data) ({hal_gpio_get_data(cd_gpios, (gpio_data_t *)data);\
  154. *(data);})
  155. #else
  156. #define SDC0_CCM_BusForceReset() HAL_CCM_BusForcePeriphReset(CCM_BUS_PERIPH_BIT_SDC0)
  157. #define SDC0_CCM_BusReleaseRest() HAL_CCM_BusReleasePeriphReset(CCM_BUS_PERIPH_BIT_SDC0)
  158. #define SDC0_CCM_BusEnableClock() HAL_CCM_BusEnablePeriphClock(CCM_BUS_PERIPH_BIT_SDC0)
  159. #define SDC0_CCM_BusDisableClock() HAL_CCM_BusDisablePeriphClock(CCM_BUS_PERIPH_BIT_SDC0)
  160. #define SDC0_CCM_EnableMClock() HAL_CCM_SDC0_EnableMClock()
  161. #define SDC0_DisableMClock HAL_CCM_SDC0_DisableMClock
  162. #define SDC0_SetMClock HAL_CCM_SDC0_SetMClock
  163. #define SDC1_SUPPORT 1
  164. #define SDC1_CCM_BusForceReset() HAL_CCM_BusForcePeriphReset(CCM_BUS_PERIPH_BIT_SDC1)
  165. #define SDC1_CCM_BusReleaseRest() HAL_CCM_BusReleasePeriphReset(CCM_BUS_PERIPH_BIT_SDC1)
  166. #define SDC1_CCM_BusEnableClock() HAL_CCM_BusEnablePeriphClock(CCM_BUS_PERIPH_BIT_SDC1)
  167. #define SDC1_CCM_BusDisableClock() HAL_CCM_BusDisablePeriphClock(CCM_BUS_PERIPH_BIT_SDC1)
  168. #define SDC1_CCM_EnableMClock() HAL_CCM_SDC1_EnableMClock()
  169. #define SDC1_DisableMClock HAL_CCM_SDC1_DisableMClock
  170. #define SDC1_SetMClock HAL_CCM_SDC1_SetMClock
  171. #endif /* NUSE_STANDARD_INTERFACE */
  172. #ifndef SDC1_SUPPORT
  173. #define SDC1_SUPPORT 0
  174. #endif
  175. #define MEMS_VA2PA(x) __va_to_pa(x)
  176. #if 0
  177. #define mci_readl(host, reg) \
  178. ({ uint32_t val;\
  179. SDC_LOGD("+reg add 0x%x\n", (uint32_t)(host)->reg_base + reg);\
  180. val = readl((uint32_t)(host)->reg_base + reg);\
  181. SDC_LOGD("%s,%d\n", __FUNCTION__, __LINE__);\
  182. SDC_LOGD("-reg val 0x%x\n", val);\
  183. val;})
  184. #define mci_writel(value, host, reg) \
  185. ({ uint32_t val;\
  186. SDC_LOGD("+reg add 0x%x write val 0x%x\n", (uint32_t)(host)->reg_base + reg, value);\
  187. writel((value), (uint32_t)(host)->reg_base + reg);\
  188. val = readl((uint32_t)(host)->reg_base + reg);\
  189. SDC_LOGD("%s,%d\n", __FUNCTION__, __LINE__);\
  190. SDC_LOGD("-reg read val 0x%x\n", val);})
  191. #define mci_readw(host, reg) \
  192. ({ uint32_t val;\
  193. SDC_LOGD("+reg add 0x%x\n", (uint32_t)(host)->reg_base + reg);\
  194. val = readw((uint32_t)(host)->reg_base + reg);\
  195. SDC_LOGD("%s,%d\n", __FUNCTION__, __LINE__);\
  196. SDC_LOGD("-reg val 0x%x\n", val);\
  197. val;})
  198. #define mci_writew(value, host, reg) \
  199. ({ uint32_t val;\
  200. SDC_LOGD("+reg add 0x%x write val 0x%x\n", (uint32_t)(host)->reg_base + reg, value);\
  201. writew((value), (uint32_t)(host)->reg_base + reg);\
  202. val = readl((uint32_t)(host)->reg_base + reg);\
  203. SDC_LOGD("%s,%d\n", __FUNCTION__, __LINE__);\
  204. SDC_LOGD("-reg read val 0x%x\n", val);})
  205. #define mci_readb(host, reg) \
  206. ({ uint32_t val;\
  207. SDC_LOGD("+reg add 0x%x\n", (uint32_t)(host)->reg_base + reg);\
  208. val = readb((uint32_t)(host)->reg_base + reg);\
  209. SDC_LOGD("%s,%d\n", __FUNCTION__, __LINE__);\
  210. SDC_LOGD("-reg val 0x%x\n", val);\
  211. val;})
  212. #define mci_writeb(value, host, reg) \
  213. ({ uint32_t val;\
  214. SDC_LOGD("+reg add 0x%x write val 0x%x\n", (uint32_t)(host)->reg_base + reg, value);\
  215. writeb((value), (uint32_t)(host)->reg_base + reg);\
  216. val = readl((uint32_t)(host)->reg_base + reg);\
  217. SDC_LOGD("%s,%d\n", __FUNCTION__, __LINE__);\
  218. SDC_LOGD("-reg read val 0x%x\n", val);})
  219. #else
  220. #define mci_readl(host, reg) \
  221. readl(HAL_PT_TO_U((host)->reg_base) + reg)
  222. #define mci_writel(value, host, reg) \
  223. writel((value), HAL_PT_TO_U((host)->reg_base) + reg)
  224. #define mci_writew(value, host, reg) \
  225. writew((value), HAL_PT_TO_U((host)->reg_base) + reg)
  226. #define mci_readw(host, reg) \
  227. readw(HAL_PT_TO_U((host)->reg_base) + reg)
  228. #define mci_readb(host, reg) \
  229. readb(HAL_PT_TO_U((host)->reg_base) + reg)
  230. #define mci_writeb(value, host, reg) \
  231. writeb((value),HAL_PT_TO_U((host)->reg_base) + reg)
  232. #endif
  233. #define SDC_BUSY_WAIT_LOOP (0xffffffff)
  234. int32_t __mci_check_bit_clear(struct mmc_host *host, uint32_t reg_offset, uint32_t bit_map);
  235. static struct mmc_host *_mci_host[SDC_NUM];
  236. /**
  237. * sdc timer timeout flags
  238. */
  239. static int __sdc_init_timeout_flags = 0;
  240. static uint64_t __mci_get_rate(uint64_t bytes, uint64_t time_us)
  241. {
  242. uint64_t ns;
  243. ns = time_us * 1000;
  244. bytes *= 1000000000;
  245. while (ns > UINT_MAX) {
  246. bytes >>= 1;
  247. ns >>= 1;
  248. }
  249. if (!ns)
  250. return 0;
  251. do_div(bytes, (uint32_t)ns);
  252. return bytes;
  253. }
  254. static void __mci_force_dump_host_info(struct mmc_host *host)
  255. {
  256. uint16_t debug_mask = host->debug_mask;
  257. host->debug_mask |= ROM_DUMP_MASK | ROM_ERR_MASK;
  258. SDC_LOGE("*force dump gpio reg*\n");
  259. sdc_hex_dump_word((void *)SDC_GPIO_BASE+0xb0,0xff);
  260. sdc_hex_dump_word((void *)SDC_GPIO_BASE+0x340,0xff);
  261. SDC_LOGE("*force dump ccmu reg*\n");
  262. sdc_hex_dump_word((void *)SDC_CCM_SDC0_SCLK_CTRL ,0xff);
  263. sdc_hex_dump_word((void *)SDC_CCM_BASE ,0xff);
  264. SDC_LOGE("*force dump smc reg*\n");
  265. sdc_hex_dump_word((void *)(SMC0_BASE + 0x1000UL * host->sdc_id) ,0x150);
  266. SDC_LOGE("force dump state wait:%x,int_sum %x, int_err %x\n",\
  267. (unsigned int)host->wait, (unsigned int)host->int_sum, (unsigned int)host->int_err);
  268. SDC_LOGE("force dump done dma_done:%x,transfer_done %x\n",\
  269. (unsigned int)host->dma_done, (unsigned int)host->trans_done);
  270. host->debug_mask = debug_mask;
  271. }
  272. static int32_t __mci_exit_host(struct mmc_host *host)
  273. {
  274. uint32_t rval;
  275. #ifdef CONFIG_SDC_SUPPORT_1V8
  276. host->voltage = SDC_WOLTAGE_OFF;
  277. #endif
  278. rval = mci_readl(host, SDXC_REG_GCTRL) | SDXC_HWReset;
  279. mci_writel(rval, host, SDXC_REG_GCTRL);
  280. __mci_check_bit_clear(host, SDXC_REG_GCTRL, SDXC_HWReset);
  281. return 0;
  282. }
  283. static __inline void __mci_sel_access_mode(struct mmc_host *host, uint32_t access_mode)
  284. {
  285. mci_writel((mci_readl(host, SDXC_REG_GCTRL) & (~SDXC_ACCESS_BY_AHB)) | access_mode,
  286. host, SDXC_REG_GCTRL);
  287. }
  288. static int32_t __mci_reset(struct mmc_host *host)
  289. {
  290. uint32_t value;
  291. uint32_t time = HAL_Ticks() + HAL_MSecsToTicks(1000) + 1;
  292. value = mci_readl(host, SDXC_REG_GCTRL)|SDXC_HWReset;
  293. mci_writel(value, host, SDXC_REG_GCTRL);
  294. while ((mci_readl(host, SDXC_REG_GCTRL) & SDXC_SoftReset) && (HAL_TimeBefore(HAL_Ticks(), time)));
  295. if (mci_readl(host, SDXC_REG_GCTRL) & SDXC_SoftReset) {
  296. SDC_LOGE("SDC reset time out\n");
  297. return -1;
  298. }
  299. SDC_LOGD("%s,%d SDC reset finish \n", __func__, __LINE__);
  300. return 0;
  301. }
  302. static int32_t __mci_program_clk(struct mmc_host *host)
  303. {
  304. uint32_t value;
  305. int32_t ret = 0;
  306. uint32_t time = HAL_Ticks() + HAL_MSecsToTicks(1000) + 1;
  307. /* disable command done interrupt */
  308. mci_writel((mci_readl(host, SDXC_REG_IMASK) & (~SDXC_CmdDone))|host->sdio_irq_mask,
  309. host, SDXC_REG_IMASK);
  310. mci_writel(mci_readl(host, SDXC_REG_CLKCR) | SDXC_Mask_Data0,\
  311. host, SDXC_REG_CLKCR);
  312. value = SDXC_Start | SDXC_UPCLKOnly | SDXC_WaitPreOver;
  313. mci_writel(value, host, SDXC_REG_CMDR);
  314. do {
  315. value = mci_readl(host, SDXC_REG_CMDR);
  316. } while ((value & SDXC_Start) && HAL_TimeBefore(HAL_Ticks(), time));
  317. if (mci_readl(host, SDXC_REG_CMDR) & SDXC_Start) {
  318. SDC_LOGE("%s,%d SDC change clock time out\n", __func__, __LINE__);
  319. __mci_force_dump_host_info(host);
  320. ret = -1;
  321. }
  322. /* clear command done flag */
  323. value = mci_readl(host, SDXC_REG_RINTR);
  324. mci_writel(value, host, SDXC_REG_RINTR);
  325. mci_writel(mci_readl(host, SDXC_REG_CLKCR) & ~SDXC_Mask_Data0,\
  326. host, SDXC_REG_CLKCR);
  327. /* enable command done interrupt */
  328. mci_writel((mci_readl(host, SDXC_REG_IMASK) | SDXC_CmdDone)|host->sdio_irq_mask,
  329. host, SDXC_REG_IMASK);
  330. return ret;
  331. }
  332. static void __mci_trans_by_ahb(struct mmc_host *host, struct mmc_data *data)
  333. {
  334. uint32_t i, j;
  335. uint32_t *buf_temp; /* Cortex-M3/4 can access data with unaligned address */
  336. uint32_t time;
  337. for (i = 0; i < data->sg_len; i++) {
  338. buf_temp = data->sg[i].buffer;
  339. if (data->flags & MMC_DATA_READ) {
  340. for (j = 0; j < (data->sg[i].len >> 2); j++) { /* sg[i].len should be multiply of 4 */
  341. time = HAL_Ticks() + HAL_MSecsToTicks(1000) + 1 ;
  342. while ((mci_readl(host, SDXC_REG_STAS) & SDXC_FIFOEmpty) &&
  343. (HAL_TimeBefore(HAL_Ticks(), time)) && host->present);
  344. if(mci_readl(host, SDXC_REG_STAS) & SDXC_FIFOEmpty){
  345. SDC_LOGE("read from fifo timeout1\n");
  346. return;
  347. }
  348. buf_temp[j] = mci_readl(host, SDXC_REG_FIFO);
  349. }
  350. if((data->sg[i].len) & 0x3) {
  351. time = HAL_Ticks() + HAL_MSecsToTicks(1000) + 1;
  352. while ((mci_readl(host, SDXC_REG_STAS) & SDXC_FIFOEmpty) &&
  353. (HAL_TimeBefore(HAL_Ticks(), time)) && host->present);
  354. if(mci_readl(host, SDXC_REG_STAS) & SDXC_FIFOEmpty){
  355. SDC_LOGE("read from fifo timeout2\n");
  356. return;
  357. }
  358. if(((data->sg[i].len) & 0x3) == 2){
  359. buf_temp[j] = (uint16_t)mci_readl(host, SDXC_REG_FIFO);
  360. }else if(((data->sg[i].len) & 0x3) == 1){
  361. buf_temp[j] = (uint8_t)mci_readl(host, SDXC_REG_FIFO);
  362. }else
  363. SDC_BUG_ON(1);
  364. }
  365. } else if (data->flags & MMC_DATA_WRITE) {
  366. for (j = 0; j < (data->sg[i].len >> 2); j++) { /* sg[i].len should be multiply of 4 */
  367. time = HAL_Ticks() + HAL_MSecsToTicks(1000) + 1;
  368. while ((mci_readl(host, SDXC_REG_STAS) & SDXC_FIFOFull) &&
  369. (HAL_TimeBefore(HAL_Ticks(), time)) && host->present);
  370. if(mci_readl(host, SDXC_REG_STAS) & SDXC_FIFOFull){
  371. SDC_LOGE("write to fifo timeout1\n");
  372. return;
  373. }
  374. mci_writel(buf_temp[j], host, SDXC_REG_FIFO);
  375. }
  376. if((data->sg[i].len) & 0x3) {
  377. time = HAL_Ticks() + HAL_MSecsToTicks(1000) + 1;
  378. while ((mci_readl(host, SDXC_REG_STAS) & SDXC_FIFOFull) &&
  379. (HAL_TimeBefore(HAL_Ticks(), time)) && host->present);
  380. if(mci_readl(host, SDXC_REG_STAS) & SDXC_FIFOFull){
  381. SDC_LOGE("write to fifo timeout2\n");
  382. return;
  383. }
  384. if(((data->sg[i].len) & 0x3) == 2){
  385. mci_writel((uint16_t)buf_temp[j], host, SDXC_REG_FIFO);
  386. }else if(((data->sg[i].len) & 0x3) == 1){
  387. mci_writel((uint8_t)buf_temp[j], host, SDXC_REG_FIFO);
  388. }else
  389. SDC_BUG_ON(1);
  390. }
  391. } else {
  392. SDC_LOGW("illigle data request\n");
  393. SDC_BUG_ON(1);
  394. return ;
  395. }
  396. }
  397. HAL_WMB();
  398. }
  399. static smc_idma_des *__mci_alloc_idma_des(struct mmc_host *host, struct mmc_data *data)
  400. {
  401. smc_idma_des *pdes = host->idma_des;
  402. uint32_t des_idx = 0;
  403. uint32_t buff_frag_num = 0;
  404. uint32_t remain;
  405. uint32_t i, j;
  406. uint32_t config;
  407. /* init IDMA Descriptor, two mode: 1-fixed skip length, 2-chain mode */
  408. for (i = 0; i < data->sg_len; i++) {
  409. buff_frag_num = data->sg[i].len >> SDXC_DES_NUM_SHIFT; /* num = len/8192 = len>>13 */
  410. remain = data->sg[i].len & (SDXC_DES_BUFFER_MAX_LEN - 1);
  411. if (remain) {
  412. buff_frag_num++;
  413. } else {
  414. remain = SDXC_DES_BUFFER_MAX_LEN;
  415. }
  416. for (j = 0; j < buff_frag_num; j++, des_idx++) {
  417. SDC_Memset((void *)&pdes[des_idx], 0, sizeof(smc_idma_des));
  418. config = SDXC_IDMAC_DES0_CH | SDXC_IDMAC_DES0_OWN | SDXC_IDMAC_DES0_DIC;
  419. if (buff_frag_num > 1 && j != buff_frag_num - 1) {
  420. pdes[des_idx].data_buf1_sz = SDXC_DES_BUFFER_MAX_LEN;
  421. } else {
  422. pdes[des_idx].data_buf1_sz = remain;
  423. }
  424. pdes[des_idx].buf_addr_ptr1 = SDXC_IDMAC_DES_ADDR((uint32_t)MEMS_VA2PA(HAL_PT_TO_U(data->sg[i].buffer)) + j * SDXC_DES_BUFFER_MAX_LEN);
  425. if (i == 0 && j == 0) {
  426. config |= SDXC_IDMAC_DES0_FD;
  427. }
  428. if ((i == data->sg_len - 1) && (j == buff_frag_num - 1)) {
  429. config &= ~SDXC_IDMAC_DES0_DIC;
  430. config |= SDXC_IDMAC_DES0_LD | SDXC_IDMAC_DES0_ER;
  431. pdes[des_idx].buf_addr_ptr2 = 0;
  432. } else {
  433. pdes[des_idx].buf_addr_ptr2 = SDXC_IDMAC_DES_ADDR((uint32_t)MEMS_VA2PA(HAL_PT_TO_U(&pdes[des_idx + 1])));
  434. }
  435. pdes[des_idx].config = config;
  436. SDC_LOGD("sg %lu, frag %lu, remain %lu, des[%lu](%p): [0]:%lx, [1]:%lx, [2]:%lx, [3]:%lx\n",
  437. HAL_PR_SZ_L(i), HAL_PR_SZ_L(j), HAL_PR_SZ_L(remain), HAL_PR_SZ_L(des_idx), &pdes[des_idx],
  438. HAL_PR_SZ_L(((uint32_t *)&pdes[des_idx])[0]), HAL_PR_SZ_L(((uint32_t *)&pdes[des_idx])[1]),
  439. HAL_PR_SZ_L(((uint32_t *)&pdes[des_idx])[2]), HAL_PR_SZ_L(((uint32_t *)&pdes[des_idx])[3]));
  440. }
  441. }
  442. HAL_FlushDcacheRegion((unsigned long)pdes, HAL_ALIGN(SDXC_MAX_DES_NUM * sizeof(smc_idma_des), OS_CACHE_ALIGN_BYTES));
  443. HAL_WMB();
  444. return pdes;
  445. }
  446. static smc_idma_des *__mci_prepare_dma(struct mmc_host *host, struct mmc_data *data)
  447. {
  448. uint32_t temp;
  449. smc_idma_des *pdes = NULL;
  450. /* creat descriptor list, two mode: 1-fixed skip length, 2-chain mode */
  451. pdes = __mci_alloc_idma_des(host, data);
  452. if (NULL == pdes) {
  453. SDC_LOGW("alloc IDMA descriptor failed\n");
  454. return NULL;
  455. }
  456. temp = mci_readl(host, SDXC_REG_GCTRL);
  457. temp |= SDXC_DMAEnb;
  458. mci_writel(temp, host, SDXC_REG_GCTRL);
  459. temp |= (SDXC_DMAReset | SDXC_FIFOReset);
  460. mci_writel(temp, host, SDXC_REG_GCTRL);
  461. __mci_check_bit_clear(host, SDXC_REG_GCTRL, (SDXC_DMAReset | SDXC_FIFOReset));
  462. mci_writel(SDXC_IDMACSoftRST, host, SDXC_REG_DMAC); /* reset IDMAC */
  463. __mci_check_bit_clear(host, SDXC_REG_DMAC, SDXC_IDMACSoftRST);
  464. temp = SDXC_IDMACFixBurst | SDXC_IDMACIDMAOn;
  465. mci_writel(temp, host, SDXC_REG_DMAC);
  466. /* enable IDMA interrupt, here not use */
  467. temp = mci_readl(host, SDXC_REG_IDIE);
  468. temp &= ~(SDXC_IDMACReceiveInt | SDXC_IDMACTransmitInt);
  469. if (data->flags & MMC_DATA_WRITE) {
  470. ;//temp |= SDXC_IDMACTransmitInt; /* disable dma int for less irqs */
  471. } else {
  472. temp |= SDXC_IDMACReceiveInt;
  473. }
  474. mci_writel(temp, host, SDXC_REG_IDIE);
  475. /* write descriptor address to register */
  476. mci_writel(SDXC_IDMAC_DES_ADDR((uint32_t)MEMS_VA2PA(HAL_PT_TO_U(pdes))), host, SDXC_REG_DLBA);
  477. /* write water level */
  478. mci_writel((BURST_SIZE << 28) | (SMC_RX_WLEVEL << 16) | SMC_TX_WLEVEL,
  479. host, SDXC_REG_FTRGL);
  480. return pdes;
  481. }
  482. static void __mci_free_idma_des(smc_idma_des *pdes)
  483. {
  484. pdes->config &= ~SDXC_IDMAC_DES0_OWN;
  485. }
  486. int32_t __mci_wait_access_done(struct mmc_host *host)
  487. {
  488. int32_t own_set = 0;
  489. uint32_t time = HAL_Ticks() + HAL_MSecsToTicks(1000) + 1;
  490. while (!(mci_readl(host, SDXC_REG_GCTRL) & SDXC_MemAccessDone) && (HAL_TimeBefore(HAL_Ticks(), time)));
  491. if (!(mci_readl(host, SDXC_REG_GCTRL) & SDXC_MemAccessDone)) {
  492. SDC_LOGE("wait memory access done timeout !!\n");
  493. }
  494. return own_set;
  495. }
  496. static int32_t __mci_request_done(struct mmc_host *host)
  497. {
  498. struct mmc_request *mrq = host->mrq;
  499. unsigned long iflags;
  500. uint32_t temp;
  501. int32_t ret = 0;
  502. iflags = HAL_EnterCriticalSection();
  503. if (host->wait != SDC_WAIT_FINALIZE) {
  504. HAL_ExitCriticalSection(iflags);
  505. SDC_LOGW("%s nothing finalize, wt %lx\n", __func__, HAL_PR_SZ_L(host->wait));
  506. return -1;
  507. }
  508. host->wait = SDC_WAIT_NONE;
  509. host->trans_done = 0;
  510. host->dma_done = 0;
  511. HAL_ExitCriticalSection(iflags);
  512. if (host->int_sum & SDXC_IntErrBit) {
  513. SDC_LOGE("SDC err, cmd %ld, %s%s%s%s%s%s%s%s%s%s\n",
  514. HAL_PR_SZ_L(host->smc_cmd & SDXC_CMD_OPCODE),
  515. host->int_sum & SDXC_RespErr ? " RE" : "",
  516. host->int_sum & SDXC_RespCRCErr ? " RCE" : "",
  517. host->int_sum & SDXC_DataCRCErr ? " DCE" : "",
  518. host->int_sum & SDXC_RespTimeout ? " RTO" : "",
  519. host->int_sum & SDXC_DataTimeout ? " DTO" : "",
  520. host->int_sum & SDXC_DataStarve ? " DS" : "",
  521. host->int_sum & SDXC_FIFORunErr ? " FRE" : "",
  522. host->int_sum & SDXC_HardWLocked ? " HL" : "",
  523. host->int_sum & SDXC_StartBitErr ? " SBE" : "",
  524. host->int_sum & SDXC_EndBitErr ? " EBE" : "");
  525. sdc_hex_dump_word((void *)(SDC_GPIO_BASE+0xB0),0xff);
  526. if (host->sdc_id == 0) {
  527. sdc_hex_dump_word((void *)SDC_CCM_SDC0_SCLK_CTRL ,0x200);
  528. sdc_hex_dump_word((void *)SMC0_BASE ,0x200);
  529. } else if (host->sdc_id == 1) {
  530. sdc_hex_dump_word((void *)SDC_CCM_SDC1_SCLK_CTRL ,0x200);
  531. sdc_hex_dump_word((void *)SMC1_BASE ,0x200);
  532. }
  533. ret = -1;
  534. goto out;
  535. }
  536. if (mrq->cmd->flags & MMC_RSP_136) {
  537. mrq->cmd->resp[0] = mci_readl(host, SDXC_REG_RESP3);
  538. mrq->cmd->resp[1] = mci_readl(host, SDXC_REG_RESP2);
  539. mrq->cmd->resp[2] = mci_readl(host, SDXC_REG_RESP1);
  540. mrq->cmd->resp[3] = mci_readl(host, SDXC_REG_RESP0);
  541. } else
  542. mrq->cmd->resp[0] = mci_readl(host, SDXC_REG_RESP0);
  543. out:
  544. if (mrq->data) {
  545. if (host->dma_hdle) {
  546. __mci_wait_access_done(host);
  547. mci_writel(0x337, host, SDXC_REG_IDST); /* clear interrupt flags */
  548. mci_writel(0, host, SDXC_REG_IDIE); /* disable idma interrupt */
  549. mci_writel(0, host, SDXC_REG_DMAC); /* idma off */
  550. temp = mci_readl(host, SDXC_REG_GCTRL);
  551. mci_writel(temp | SDXC_DMAReset, host, SDXC_REG_GCTRL);
  552. temp &= ~SDXC_DMAEnb;
  553. mci_writel(temp, host, SDXC_REG_GCTRL); /* disable IDMA */
  554. temp |= SDXC_FIFOReset;
  555. mci_writel(temp, host, SDXC_REG_GCTRL);
  556. __mci_free_idma_des((void *)host->dma_hdle);
  557. host->dma_hdle = NULL;
  558. }
  559. mci_writel(mci_readl(host, SDXC_REG_GCTRL) | SDXC_FIFOReset,
  560. host, SDXC_REG_GCTRL);
  561. }
  562. #ifdef CONFIG_DETECT_CARD
  563. if (host->param.cd_mode == CARD_DETECT_BY_D3){
  564. mci_writel((SDXC_CardInsert|SDXC_CardRemove)|host->sdio_irq_mask, host, SDXC_REG_IMASK);
  565. } else
  566. #endif
  567. {
  568. mci_writel(host->sdio_irq_mask, host, SDXC_REG_IMASK);
  569. }
  570. if (host->int_sum & (SDXC_RespErr | SDXC_HardWLocked | SDXC_RespTimeout)) {
  571. SDC_LOGE("sdc %d abnormal status: %s\n", __LINE__,
  572. host->int_sum & SDXC_HardWLocked ? "HardWLocked" : "RespErr");
  573. }
  574. mci_writel(0xffff, host, SDXC_REG_RINTR);
  575. SDC_LOGD("SDC done, resp %lx %lx %lx %lx\n", HAL_PR_SZ_L(mrq->cmd->resp[0]),
  576. HAL_PR_SZ_L(mrq->cmd->resp[1]), HAL_PR_SZ_L(mrq->cmd->resp[2]), HAL_PR_SZ_L(mrq->cmd->resp[3]));
  577. if (mrq->data && (host->int_sum & SDXC_IntErrBit)) {
  578. SDC_LOGW("found data error, need to send stop command !!\n");
  579. __mci_reset(host);
  580. __mci_program_clk(host);
  581. }
  582. host->int_err = 0;
  583. return ret;
  584. }
  585. #define mci_writel_irq(value, host, reg) \
  586. ({ uint32_t val;\
  587. printf("+irq reg add 0x%x write val 0x%x\n", (uint32_t)(host)->reg_base + reg, value);\
  588. writel((value), (uint32_t)(host)->reg_base + reg);\
  589. val = readl((uint32_t)(host)->reg_base + reg);\
  590. printf("%s,%d\n", __FUNCTION__, __LINE__);\
  591. printf("-irq reg read val 0x%x\n", (unsigned int)val);})
  592. //#ifdef CONFIG_SDIO_IRQ_SUPPORT
  593. __nonxip_text
  594. static void __mci_enable_sdio_irq(struct mmc_host *host, int enable)
  595. {
  596. uint32_t imask;
  597. uint32_t flags = HAL_EnterCriticalSection();
  598. imask = mci_readl(host, SDXC_REG_IMASK);
  599. if (enable){
  600. // mci_writel(SDXC_SDIOInt, host, SDXC_REG_RINTR);
  601. host->sdio_irq_mask = SDXC_SDIOInt;
  602. imask |= SDXC_SDIOInt;
  603. }else{
  604. host->sdio_irq_mask = 0;
  605. imask &= ~SDXC_SDIOInt;
  606. }
  607. mci_writel(imask, host, SDXC_REG_IMASK);
  608. HAL_ExitCriticalSection(flags);
  609. }
  610. __nonxip_text
  611. static inline void __mci_signal_sdio_irq(struct mmc_host *host)
  612. {
  613. // if (!host->card || !host->card->irq_handler) {
  614. if (!host->card) {
  615. SDC_IT_LOGE("BUG at __mci_signal_sdio_irq():%d\n", __LINE__);
  616. return;
  617. }
  618. #ifdef SD_PERF_TRACE_ON
  619. host->start_sdio_irq_times_ns = HAL_GetTimeNs();
  620. host->sdio_irq_count ++;
  621. #endif
  622. __mci_enable_sdio_irq(host, 0);
  623. host->sdio_irq_pending = true;
  624. //if (host.sdio_irq_thread)
  625. //xTaskResumeFromISR(host->sdio_irq_thread.handle);
  626. OS_SemaphoreRelease(&host->sdio_irq_signal);
  627. }
  628. void rom_HAL_SDC_Enable_Sdio_Irq(struct mmc_host *host, int enable)
  629. {
  630. uint32_t flags = 0;
  631. __mci_enable_sdio_irq(host,enable);
  632. }
  633. //#endif
  634. static void __mci_clk_prepare_enable(struct mmc_host *host)
  635. {
  636. if (host->sdc_id == 0) {
  637. SDC0_CCM_BusEnableClock(); /* clock enable */
  638. SDC0_CCM_BusReleaseRest(); /* reset and gating */
  639. SDC0_CCM_EnableMClock();
  640. #if SDC1_SUPPORT
  641. } else if (host->sdc_id == 1) {
  642. SDC1_CCM_BusEnableClock(); /* clock enable */
  643. SDC1_CCM_BusReleaseRest(); /* reset and gating */
  644. SDC1_CCM_EnableMClock();
  645. #endif
  646. }
  647. }
  648. static void __mci_clk_disable_unprepare(struct mmc_host *host)
  649. {
  650. if (host->sdc_id == 0) {
  651. SDC0_CCM_BusDisableClock();
  652. #if SDC1_SUPPORT
  653. } else if (host->sdc_id == 1) {
  654. SDC1_CCM_BusDisableClock();
  655. #endif
  656. }
  657. }
  658. static void __mci_hold_io(struct mmc_host* host)
  659. {
  660. #ifdef __CONFIG_ARCH_APP_CORE
  661. #ifndef NUSE_STANDARD_INTERFACE
  662. /* disable gpio to avoid power leakage */
  663. if (host->pin_ref) {
  664. HAL_BoardIoctl(HAL_BIR_PINMUX_DEINIT, HAL_MKDEV(HAL_DEV_MAJOR_SDC,
  665. host->sdc_id), SDCGPIO_BAS);
  666. host->pin_ref = 0;
  667. }
  668. #else
  669. SDC_LOGN("Not implement %s,%d\n", __FUNCTION__, __LINE__);
  670. #endif
  671. #endif
  672. }
  673. static void __mci_restore_io(struct mmc_host* host)
  674. {
  675. #ifdef __CONFIG_ARCH_APP_CORE
  676. #ifndef NUSE_STANDARD_INTERFACE
  677. if (host->pin_ref == 0) {
  678. HAL_BoardIoctl(HAL_BIR_PINMUX_INIT, HAL_MKDEV(HAL_DEV_MAJOR_SDC,
  679. host->sdc_id), SDCGPIO_BAS);
  680. host->pin_ref = 1;
  681. }
  682. #else
  683. SDC_LOGN("Not implement %s,%d\n", __FUNCTION__, __LINE__);
  684. #endif
  685. #
  686. #endif
  687. }
  688. __nonxip_text
  689. static irqreturn_t __mci_irq_handler(uint32_t sdc_id)
  690. {
  691. struct mmc_host *host = _mci_host[sdc_id];
  692. uint32_t sdio_int = 0;
  693. uint32_t raw_int;
  694. uint32_t msk_int;
  695. uint32_t idma_inte;
  696. uint32_t idma_int;
  697. #if (defined(__CONFIG_SECTION_ATTRIBUTE_NONXIP) && SDC_DEBUG)
  698. __nonxip_rodata static const char __s_func[] = "__mci_irq_handler";
  699. #endif
  700. //SDC_IT_LOGD("***%s,%d***\n",__FUNCTION__, __LINE__);
  701. if (!host) {
  702. SDC_IT_LOGE_RAW(ROM_ERR_MASK, "%s,%d no host exist!\n", __func__, __LINE__);
  703. return IRQ_NONE;
  704. }
  705. if (!host->present) {
  706. if (host->sdc_id == 0) {
  707. SDC0_CCM_BusEnableClock();
  708. #if SDC1_SUPPORT
  709. } else if (host->sdc_id == 1) {
  710. SDC1_CCM_BusEnableClock();
  711. #endif
  712. }
  713. }
  714. idma_int = mci_readl(host, SDXC_REG_IDST);
  715. idma_inte = mci_readl(host, SDXC_REG_IDIE);
  716. raw_int = mci_readl(host, SDXC_REG_RINTR);
  717. msk_int = mci_readl(host, SDXC_REG_MISTA);
  718. if (!msk_int && !idma_int) {
  719. SDC_IT_LOGE("sdc nop irq: ri:%08lx mi:%08lx ie:%08lx idi:%08lx\n",
  720. HAL_PR_SZ_L(raw_int), HAL_PR_SZ_L(msk_int), HAL_PR_SZ_L(idma_inte), HAL_PR_SZ_L(idma_int));
  721. return IRQ_NONE;
  722. }
  723. host->int_sum = raw_int;
  724. SDC_IT_LOGD("sdc %d ri:%02x(%02x), mi:%x, ie:%x, idi:%x\n", __LINE__,
  725. (int)raw_int, (int)host->int_sum,
  726. (int)msk_int, (int)idma_inte, (int)idma_int);
  727. (void)idma_inte;
  728. #ifdef CONFIG_SDIO_IRQ_SUPPORT
  729. if (msk_int & SDXC_SDIOInt) {
  730. sdio_int = 1;
  731. mci_writel(SDXC_SDIOInt, host, SDXC_REG_RINTR);
  732. goto sdio_out;
  733. }
  734. #endif
  735. #ifdef CONFIG_DETECT_CARD
  736. if (host->param.cd_mode == CARD_DETECT_BY_D3) {
  737. if (msk_int & SDXC_CardInsert) {
  738. SDC_IT_LOGN("Card Insert !!\n");
  739. host->present = 1;
  740. SDC_IT_ModTimer(&host->cd_timer, 10);
  741. } else if (msk_int & SDXC_CardRemove) {
  742. SDC_IT_LOGN("Card Remove !!\n");
  743. host->present = 0;
  744. SDC_IT_ModTimer(&host->cd_timer, 10);
  745. }
  746. }
  747. #endif
  748. if (host->wait == SDC_WAIT_NONE && !sdio_int) {
  749. SDC_IT_LOGE("%s nothing to complete, ri:%08lx, mi:%08lx\n",
  750. __s_func, HAL_PR_SZ_L(raw_int), HAL_PR_SZ_L(msk_int));
  751. goto irq_out;
  752. }
  753. if ((raw_int & SDXC_IntErrBit) || (idma_int & SDXC_IDMA_ERR)) {
  754. host->int_err = raw_int & SDXC_IntErrBit;
  755. host->wait = SDC_WAIT_FINALIZE;
  756. SDC_IT_LOGE("%s,%d raw_int:%lx err!\n", __s_func, __LINE__, HAL_PR_SZ_L(raw_int));
  757. goto irq_out;
  758. }
  759. if (raw_int & SDXC_HardWLocked) {
  760. SDC_IT_LOGE("command hardware lock\n");
  761. }
  762. if (idma_int & (SDXC_IDMACTransmitInt|SDXC_IDMACReceiveInt)) {
  763. host->dma_done = 1;
  764. mci_writel(idma_int, host, SDXC_REG_IDST);
  765. }
  766. if (msk_int & (SDXC_AutoCMDDone|SDXC_DataOver|SDXC_CmdDone|SDXC_VolChgDone))
  767. host->trans_done = 1;
  768. if ((host->trans_done && \
  769. (host->wait == SDC_WAIT_AUTOCMD_DONE || host->wait == SDC_WAIT_DATA_OVER
  770. || host->wait == SDC_WAIT_CMD_DONE || host->wait == SDC_WAIT_SWITCH1V8))
  771. || (host->trans_done && host->dma_done && (host->wait & SDC_WAIT_IDMA_DONE))) {
  772. host->wait = SDC_WAIT_FINALIZE;
  773. }
  774. irq_out:
  775. mci_writel(msk_int & (~SDXC_SDIOInt), host, SDXC_REG_RINTR);
  776. if (host->wait == SDC_WAIT_FINALIZE) {
  777. #ifdef CONFIG_DETECT_CARD
  778. if (host->param.cd_mode == CARD_DETECT_BY_D3){
  779. mci_writel(SDXC_CardInsert|SDXC_CardRemove|host->sdio_irq_mask,
  780. host, SDXC_REG_IMASK);
  781. } else
  782. #endif
  783. {
  784. mci_writel(host->sdio_irq_mask, host, SDXC_REG_IMASK);
  785. }
  786. HAL_WMB();
  787. if(!OS_IsISRContext()){
  788. SDC_BUG_ON(1);
  789. sys_abort();
  790. }
  791. SDC_SemPost(&host->lock);
  792. SDC_IT_LOGD("SDC irq post, trans:%d, dma:%d\n",
  793. (int)host->trans_done, (int)host->dma_done);
  794. }
  795. #ifdef CONFIG_SDIO_IRQ_SUPPORT
  796. sdio_out:
  797. if (sdio_int)
  798. __mci_signal_sdio_irq(host);
  799. #endif
  800. return IRQ_HANDLED;
  801. }
  802. __nonxip_text
  803. irqreturn_t SDC0_IRQHandler(int id, void *data)
  804. {
  805. return __mci_irq_handler(SDC0);
  806. }
  807. #if SDC1_SUPPORT
  808. __nonxip_text
  809. irqreturn_t SDC1_IRQHandler(int id, void *data) //static
  810. {
  811. return __mci_irq_handler(SDC1);
  812. }
  813. #endif
  814. /* clock config:
  815. * SYS_CRYSTAL: 26M
  816. * 400K, SCLK-26M, 26M/(2^3)/(7+1)/2 = 361K
  817. * SYS_CRYSTAL: 24M
  818. * 400K, SCLK-24M, 24M/(2^2)/(7+1)/2 = 375K
  819. *
  820. * 25M, SCLK-192M, 192M/(2^0)/(3+1)/2 = 24M
  821. * 50M, SCLK-192M, 192M/(2^0)/(1+1)/2 = 48M
  822. * 64M, SCLK-192M, 192M/(2^0)/(1+1)/2 = 48M
  823. */
  824. #define DEFINE_SYS_CRYSTAL HAL_GetHFClock()
  825. #define DEFINE_SYS_DEVCLK HAL_GetDevClock()
  826. static int32_t __mci_update_clock(struct mmc_host *host, uint32_t cclk)
  827. {
  828. uint32_t sclk;
  829. uint32_t div;
  830. uint32_t rval;
  831. uint32_t src = 0;
  832. uint32_t m, n;
  833. if (cclk > DEFINE_SYS_CRYSTAL/2) {
  834. #ifdef NUSE_STANDARD_INTERFACE
  835. src = 1;
  836. #else
  837. src = CCM_AHB_PERIPH_CLK_SRC_DEVCLK;
  838. #endif
  839. sclk = DEFINE_SYS_DEVCLK;
  840. } else {
  841. #ifdef NUSE_STANDARD_INTERFACE
  842. src = 0;
  843. #else
  844. src = CCM_AHB_PERIPH_CLK_SRC_HFCLK;
  845. #endif
  846. sclk = DEFINE_SYS_CRYSTAL;
  847. }
  848. cclk = cclk * 2; /* 2x MODE clock configure */
  849. div = (2 * sclk + cclk) / (2 * cclk);
  850. div = div == 0 ? 1 : div;
  851. if (div > 128) {
  852. n = 3;
  853. m = 16;
  854. SDC_LOGE("source clk is too high!\n");
  855. } else if (div > 64) {
  856. n = 3;
  857. m = div >> 3;
  858. } else if (div > 32) {
  859. n = 2;
  860. m = div >> 2;
  861. } else if (div > 16) {
  862. n = 1;
  863. m = div >> 1;
  864. } else {
  865. n = 0;
  866. m = div;
  867. }
  868. m = m - 1;
  869. #ifdef NUSE_STANDARD_INTERFACE
  870. /*
  871. if (src) {
  872. rval |= (2U << 20) | (1 << 8);
  873. SDC_LOGN("%s,%d check spec. bit20, 8!!\n", __func__, __LINE__);
  874. }
  875. */
  876. if (host->sdc_id == 0) {
  877. rval = readl(SDC_CCM_SDC0_SCLK_CTRL);
  878. rval &= ~(1U<<31);
  879. writel(rval,SDC_CCM_SDC0_SCLK_CTRL);
  880. HAL_WMB();
  881. rval = (src << 24) | (n << 8) | (m);
  882. writel(rval,SDC_CCM_SDC0_SCLK_CTRL);
  883. HAL_WMB();
  884. rval = readl(SDC_CCM_SDC0_SCLK_CTRL);
  885. rval |= (1U<<31);
  886. writel(rval,SDC_CCM_SDC0_SCLK_CTRL);
  887. HAL_WMB();
  888. SDC_LOGN("SDC clock=%ld kHz,src:%x, n:%d, m:%d\n",
  889. HAL_PR_SZ_L((src ? DEFINE_SYS_DEVCLK:DEFINE_SYS_CRYSTAL)/(1<<n)/(m+1)/2),
  890. (int)src, (int)n, (int)m);
  891. #if SDC1_SUPPORT
  892. } else if (host->sdc_id == 1) {
  893. rval = readl(SDC_CCM_SDC1_SCLK_CTRL);
  894. rval &= ~(1U<<31);
  895. writel(rval,SDC_CCM_SDC1_SCLK_CTRL);
  896. HAL_WMB();
  897. rval = (src << 24) | (n << 8) | (m);
  898. writel(rval,SDC_CCM_SDC1_SCLK_CTRL);
  899. HAL_WMB();
  900. rval = readl(SDC_CCM_SDC1_SCLK_CTRL);
  901. rval |= (1U<<31);
  902. writel(rval,SDC_CCM_SDC1_SCLK_CTRL);
  903. HAL_WMB();
  904. SDC_LOGN("SDC clock=%ld kHz,src:%x, n:%d, m:%d\n",
  905. HAL_PR_SZ_L((src?DEFINE_SYS_DEVCLK:DEFINE_SYS_CRYSTAL)/(1<<n)/(m+1)/2),
  906. (int)src, (int)n, (int)m);
  907. #endif
  908. }
  909. #else
  910. if (host->sdc_id == 0) {
  911. SDC0_DisableMClock();
  912. SDC0_SetMClock(src, n << CCM_PERIPH_CLK_DIV_N_SHIFT, m << CCM_PERIPH_CLK_DIV_M_SHIFT);
  913. SDC0_CCM_EnableMClock();
  914. #if SDC1_SUPPORT
  915. } else if (host->sdc_id == 1) {
  916. SDC1_DisableMClock();
  917. SDC1_SetMClock(src, n << CCM_PERIPH_CLK_DIV_N_SHIFT, m << CCM_PERIPH_CLK_DIV_M_SHIFT);
  918. SDC1_CCM_EnableMClock();
  919. #endif
  920. }
  921. SDC_LOGN("SDC source:%u MHz clock=%u kHz, src:%x, n:%d, m:%d\n", sclk/1000000,
  922. sclk/(1<<n)/(m+1)/2000, (int)src, (int)n, (int)m);
  923. #endif
  924. /* clear internal divider */
  925. rval = mci_readl(host, SDXC_REG_CLKCR) & (~0xff);
  926. mci_writel(rval, host, SDXC_REG_CLKCR);
  927. sdc_hex_dump_word((void *)SDC_CCM_BASE ,0x900);
  928. return cclk;
  929. }
  930. int32_t rom_HAL_SDC_Update_Clk(struct mmc_host *host, uint32_t clk)
  931. {
  932. uint32_t rval;
  933. if (!host || clk < 200000) { /* 200K */
  934. SDC_LOGE_RAW(ROM_ERR_MASK, "%s,%d err", __func__, __LINE__);
  935. return -1;
  936. }
  937. /* Disable Clock */
  938. rval = mci_readl(host, SDXC_REG_CLKCR) & (~SDXC_CardClkOn) & (~SDXC_LowPowerOn);
  939. mci_writel(rval, host, SDXC_REG_CLKCR);
  940. if (__mci_program_clk(host)) {
  941. SDC_LOGE("Clock Program Failed 0!!\n");
  942. return -1;
  943. }
  944. __mci_update_clock(host, clk);
  945. /* Re-enable Clock */
  946. rval = mci_readl(host, SDXC_REG_CLKCR) | (SDXC_CardClkOn);// | SDXC_LowPowerOn;
  947. mci_writel(rval, host, SDXC_REG_CLKCR);
  948. if (__mci_program_clk(host)) {
  949. SDC_LOGE("Clock Program Failed 1!!\n");
  950. return -1;
  951. }
  952. host->clk = clk;
  953. return 0;
  954. }
  955. static int32_t __mci_update_clk(struct mmc_host *host)
  956. {
  957. uint32_t rval;
  958. int32_t ret = 0;
  959. uint32_t time = HAL_Ticks() + HAL_MSecsToTicks(1000) + 1;
  960. mci_writel(mci_readl(host, SDXC_REG_CLKCR) | SDXC_Mask_Data0,\
  961. host, SDXC_REG_CLKCR);
  962. rval = SDXC_Start|SDXC_UPCLKOnly|SDXC_WaitPreOver;
  963. #ifdef CONFIG_SDC_SUPPORT_1V8
  964. if (host->voltage_switching)
  965. rval |= SDXC_VolSwitch;
  966. #endif
  967. mci_writel(rval, host, SDXC_REG_CMDR);
  968. do {
  969. rval = mci_readl(host, SDXC_REG_CMDR);
  970. } while ((rval & SDXC_Start) && HAL_TimeBefore(HAL_Ticks(), time));
  971. if (mci_readl(host, SDXC_REG_CMDR) & SDXC_Start) {
  972. SDC_LOGE("sdc update clock timeout, fatal error!!!\n");
  973. __mci_force_dump_host_info(host);
  974. ret = -1;
  975. }
  976. if (!ret)
  977. SDC_LOGD("sdc update clock ok\n");
  978. /* clear command done flag */
  979. rval = mci_readl(host, SDXC_REG_RINTR);
  980. mci_writel(rval, host, SDXC_REG_RINTR);
  981. mci_writel(mci_readl(host, SDXC_REG_CLKCR) & ~SDXC_Mask_Data0,\
  982. host, SDXC_REG_CLKCR);
  983. return ret;
  984. }
  985. int32_t rom_HAL_SDC_Clk_PWR_Opt(struct mmc_host *host, uint32_t oclk_en, uint32_t pwr_save)
  986. {
  987. uint32_t rval;
  988. if (!host->power_on)
  989. return 0;
  990. rval = mci_readl(host, SDXC_REG_CLKCR);
  991. rval &= ~(SDXC_CardClkOn | SDXC_LowPowerOn);
  992. if (oclk_en)
  993. rval |= SDXC_CardClkOn;
  994. if (pwr_save)
  995. rval |= SDXC_LowPowerOn;
  996. mci_writel(rval, host, SDXC_REG_CLKCR);
  997. __mci_update_clk(host);
  998. return 0;
  999. }
  1000. static void __mci_debounce_onoff(struct mmc_host *host, uint32_t onoff)
  1001. {
  1002. uint32_t rval = mci_readl(host, SDXC_REG_GCTRL);
  1003. rval &= ~SDXC_DebounceEnb;
  1004. if (onoff)
  1005. rval |= SDXC_DebounceEnb;
  1006. mci_writel(rval, host, SDXC_REG_GCTRL);
  1007. SDC_LOGD("%s,%d,%ld\n",__FUNCTION__,__LINE__, HAL_PR_SZ_L(onoff));
  1008. }
  1009. uint32_t HAL_SDC_Is_Busy(struct mmc_host *host)
  1010. {
  1011. return mci_readl(host, SDXC_REG_STAS) & SDXC_CardBusy;
  1012. }
  1013. static int32_t __mci_check_busy_over(struct mmc_host *host)
  1014. {
  1015. uint32_t i;
  1016. for(i = 0; i< SDC_BUSY_WAIT_LOOP; i++){
  1017. if(!(mci_readl(host, SDXC_REG_STAS) & SDXC_CardBusy)){
  1018. return 0;
  1019. }
  1020. if(i>(SDC_BUSY_WAIT_LOOP/8)) {
  1021. mmc_mdelay(10);
  1022. if(i%8)
  1023. SDC_LOGW("Waiting reg %x bitmap %x clear v %x,i %x\n",\
  1024. HAL_PR_SZ(HAL_PT_TO_U(host->reg_base)) + SDXC_REG_STAS,\
  1025. (unsigned int)SDXC_CardBusy,\
  1026. (unsigned int)mci_readl(host, SDXC_CardBusy),\
  1027. (unsigned int)i);
  1028. }
  1029. SDC_LOGD("Waiting reg %x bitmap %x clear v %x,i %x\n",\
  1030. HAL_PR_SZ(HAL_PT_TO_U(host->reg_base)) + SDXC_REG_STAS,\
  1031. (unsigned int)SDXC_CardBusy,\
  1032. (unsigned int)mci_readl(host, SDXC_CardBusy),\
  1033. (unsigned int)i);
  1034. }
  1035. SDC_LOGE("Wait busy timeout %x,%x\n",
  1036. (unsigned int)mci_readl(host, SDXC_REG_STAS), (unsigned int)i);
  1037. return -1;
  1038. }
  1039. int32_t __mci_check_bit_clear(struct mmc_host *host, uint32_t reg_offset, uint32_t bit_map)
  1040. {
  1041. uint32_t i;
  1042. for(i = 0; i< SDC_BUSY_WAIT_LOOP; i++){
  1043. if(!(mci_readl(host, reg_offset) & bit_map)){
  1044. SDC_LOGD("Wait reg %p bitmap %x clear v %lx,i %x ok\n",\
  1045. (host->reg_base) + reg_offset,\
  1046. (unsigned int)bit_map,\
  1047. HAL_PR_SZ_L(mci_readl(host, reg_offset)),\
  1048. (unsigned int)i);
  1049. return 0;
  1050. }
  1051. if(i>(SDC_BUSY_WAIT_LOOP/8)) {
  1052. mmc_mdelay(10);
  1053. if(i%8)
  1054. SDC_LOGW("Waiting reg %p bitmap %x clear v %lx,i %x\n",\
  1055. (host->reg_base) + reg_offset,\
  1056. (unsigned int)bit_map,\
  1057. HAL_PR_SZ_L(mci_readl(host, reg_offset)),\
  1058. (unsigned int)i);
  1059. }
  1060. }
  1061. SDC_LOGE("Wait reg %p bitmap %x clear timeout v %lx,i %x\n",\
  1062. (host->reg_base) + reg_offset,\
  1063. (unsigned int) bit_map,\
  1064. HAL_PR_SZ_L(mci_readl(host, reg_offset)),\
  1065. (unsigned int)i);
  1066. return -1;
  1067. }
  1068. void HAL_SDC_Set_BusWidth(struct mmc_host *host, uint32_t width)
  1069. {
  1070. if (!host) {
  1071. SDC_LOGE_RAW(ROM_ERR_MASK, "%s,%d err", __func__, __LINE__);
  1072. return ;
  1073. }
  1074. switch (width) {
  1075. case MMC_BUS_WIDTH_1:
  1076. mci_writel(SDXC_WIDTH1, host, SDXC_REG_WIDTH);
  1077. break;
  1078. case MMC_BUS_WIDTH_4:
  1079. mci_writel(SDXC_WIDTH4, host, SDXC_REG_WIDTH);
  1080. break;
  1081. #ifdef CONFIG_USE_MMC
  1082. case MMC_BUS_WIDTH_8:
  1083. mci_writel(SDXC_WIDTH8, host, SDXC_REG_WIDTH);
  1084. break;
  1085. #endif
  1086. default:
  1087. SDC_BUG_ON(1);
  1088. return ;
  1089. }
  1090. host->buswidth = width;
  1091. }
  1092. static void __mci_send_cmd(struct mmc_host *host, struct mmc_command *cmd)
  1093. {
  1094. uint32_t imask = SDXC_IntErrBit;
  1095. uint32_t cmd_val = SDXC_Start | (cmd->opcode & 0x3f);
  1096. unsigned long iflags;
  1097. uint32_t wait = SDC_WAIT_CMD_DONE;
  1098. struct mmc_data *data = cmd->data;
  1099. if (cmd->opcode == MMC_GO_IDLE_STATE) {
  1100. cmd_val |= SDXC_SendInitSeq;
  1101. imask |= SDXC_CmdDone;
  1102. }
  1103. #ifdef CONFIG_SDC_SUPPORT_1V8
  1104. if (cmd->opcode == SD_SWITCH_VOLTAGE) {
  1105. cmd_val |= SDXC_VolSwitch;
  1106. imask |= SDXC_VolChgDone;
  1107. host->voltage_switching = 1;
  1108. wait = SDC_WAIT_SWITCH1V8;
  1109. /* switch controller to high power mode */
  1110. HAL_SDC_Clk_PWR_Opt(host, 1, 0);
  1111. }
  1112. #endif
  1113. if (cmd->flags & MMC_RSP_PRESENT) { /* with response */
  1114. cmd_val |= SDXC_RspExp;
  1115. if (cmd->flags & MMC_RSP_136) /* long response */
  1116. cmd_val |= SDXC_LongRsp;
  1117. if (cmd->flags & MMC_RSP_CRC)
  1118. cmd_val |= SDXC_CheckRspCRC;
  1119. if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) { /* with data */
  1120. if (!data) {
  1121. SDC_LOGE("%s,%d no data exist!\n", __func__, __LINE__);
  1122. return ;
  1123. }
  1124. cmd_val |= SDXC_DataExp | SDXC_WaitPreOver;
  1125. wait = SDC_WAIT_DATA_OVER;
  1126. if (data->flags & MMC_DATA_STREAM) { /* sequence mode */
  1127. imask |= SDXC_AutoCMDDone;
  1128. cmd_val |= SDXC_Seqmod | SDXC_SendAutoStop;
  1129. wait = SDC_WAIT_AUTOCMD_DONE;
  1130. }
  1131. if (cmd->stop) {
  1132. imask |= SDXC_AutoCMDDone;
  1133. cmd_val |= SDXC_SendAutoStop;
  1134. wait = SDC_WAIT_AUTOCMD_DONE;
  1135. } else
  1136. imask |= SDXC_DataOver;
  1137. if (data->flags & MMC_DATA_WRITE) {
  1138. cmd_val |= SDXC_Write;
  1139. } else if (host->dma_hdle) {
  1140. wait |= SDC_WAIT_IDMA_DONE;
  1141. }
  1142. SDC_LOGD("blk_size:%lu, sg len:%lu\n",HAL_PR_SZ_L(data->blksz), HAL_PR_SZ_L(data->sg->len));
  1143. } else
  1144. imask |= SDXC_CmdDone;
  1145. } else
  1146. imask |= SDXC_CmdDone;
  1147. SDC_LOGD("sdc cmd:%ld(%lx), arg:%lx ie:%lx wt:%lx len:%lu\n",
  1148. HAL_PR_SZ_L(cmd_val & SDXC_CMD_OPCODE), HAL_PR_SZ_L(cmd_val), HAL_PR_SZ_L(cmd->arg), HAL_PR_SZ_L(imask), HAL_PR_SZ_L(wait),
  1149. HAL_PR_SZ_L(cmd->data ? cmd->data->blksz * cmd->data->blocks : 0));
  1150. iflags = HAL_EnterCriticalSection();
  1151. host->smc_cmd = cmd_val;
  1152. host->wait = wait;
  1153. mci_writel(imask | host->sdio_irq_mask, host, SDXC_REG_IMASK);
  1154. if (cmd_val & SDXC_SendAutoStop)
  1155. mci_writel(0, host, SDXC_REG_A12A);
  1156. else
  1157. mci_writel(0xffff, host, SDXC_REG_A12A);
  1158. mci_writel(cmd->arg, host, SDXC_REG_CARG);
  1159. HAL_WMB();
  1160. mci_writel(cmd_val, host, SDXC_REG_CMDR);
  1161. HAL_ExitCriticalSection(iflags);
  1162. if (data && NULL == host->dma_hdle) {
  1163. __mci_trans_by_ahb(host, data);
  1164. }
  1165. }
  1166. #ifdef SD_PERF_TRACE_ON
  1167. static void __mci_start_data_time_stats(struct mmc_host *host, struct mmc_request *mrq)
  1168. {
  1169. if((mrq->data) && (mmc_cmd_type(mrq->cmd) == MMC_CMD_ADTC)){
  1170. if( (mrq->data->blksz * mrq->data->blocks) > 4 )
  1171. host->start_times_us = HAL_GetTimeUs();
  1172. }
  1173. }
  1174. /*this func can not be called by irq_server_function;
  1175. *because there are using printf(only printf support u64).
  1176. *if you want to use it in irq_server_funciton,printf to printk
  1177. * */
  1178. static void __mci_end_data_time_stats(struct mmc_host *host, struct mmc_request *mrq)
  1179. {
  1180. if((mrq->data) && (mmc_cmd_type(mrq->cmd) == MMC_CMD_ADTC) \
  1181. && ((mrq->data->blksz * mrq->data->blocks) > 4)){
  1182. uint64_t time_use_us = (uint64_t)HAL_GetTimeUs() - host->start_times_us;
  1183. if((mrq->data->flags) & MMC_DATA_WRITE ) {
  1184. host->wbytes += mrq->data->blksz * mrq->data->blocks ;
  1185. host->wtimes_us += time_use_us;
  1186. host->wcount ++;
  1187. if(host->wcount && !(host->wcount % SD_PERF_TRACE_COUNT)) {
  1188. printf("Stats Total write:bytes %lld, time us %lu, count %lld, %lld KB\n",\
  1189. host->wbytes, host->wtimes_us, host->wcount,\
  1190. __mci_get_rate(host->wbytes, host->wtimes_us)/1024);
  1191. //host->wtimes_us = 0;
  1192. //host->wbytes = 0;
  1193. //host->wcount = 0;
  1194. }
  1195. } else if((mrq->data->flags) & MMC_DATA_READ) {
  1196. host->rbytes += mrq->data->blksz * mrq->data->blocks;
  1197. host->rtimes_us += time_use_us;
  1198. host->rcount++;
  1199. if( host->rcount &&!(host->rcount % SD_PERF_TRACE_COUNT)) {
  1200. printf("Stats Total read: byte %lld, time us %lu, count %lld, %lldKB\n",\
  1201. host->rbytes, host->rtimes_us, host->rcount,\
  1202. __mci_get_rate(host->rbytes, host->rtimes_us)/1024);
  1203. //host->rtimes_us = 0;
  1204. //host->rbytes = 0;
  1205. //host->rcount = 0;
  1206. }
  1207. }
  1208. }
  1209. }
  1210. #endif
  1211. int32_t do_rom_HAL_SDC_Request(struct mmc_host *host, struct mmc_request *mrq)
  1212. {
  1213. int32_t ret = 0;
  1214. struct mmc_command *cmd = mrq->cmd;
  1215. struct mmc_data *data = mrq->data;
  1216. #ifdef CONFIG_SDC_DMA_USED
  1217. uint32_t sdc_use_dma = host->dma_use;
  1218. #else
  1219. uint32_t sdc_use_dma = 0;
  1220. #endif
  1221. if (!host->present) {
  1222. SDC_LOGW("sdc:%p no medium present\n", host);
  1223. SDC_SemRest((&host->lock));
  1224. return -1;
  1225. }
  1226. #ifdef CONFIG_SD_PM
  1227. if (host->suspend) {
  1228. SDC_LOGE("sdc:%p has suspended!\n", host);
  1229. return -1;
  1230. }
  1231. #endif
  1232. #ifdef SD_PERF_TRACE_ON
  1233. __mci_start_data_time_stats(host, mrq);
  1234. #endif
  1235. __mci_check_busy_over(host);
  1236. __mci_check_bit_clear(host, SDXC_REG_GCTRL, SDXC_HWReset);
  1237. SDC_MutexLock(&host->thread_lock, SDC_THREAD_TIMEOUT);
  1238. /* disable debounce */
  1239. __mci_debounce_onoff(host, 0);
  1240. host->mrq = mrq;
  1241. if (data) {
  1242. uint32_t i;
  1243. uint32_t byte_cnt = 0;
  1244. struct scatterlist *sg = data->sg;
  1245. uint32_t sg_len = data->sg_len;
  1246. unsigned long addr = 0;
  1247. uint32_t len = 0;
  1248. for (i = 0; i < sg_len; i++) {
  1249. addr = HAL_PT_TO_U(sg[i].buffer);
  1250. len = sg[i].len;
  1251. byte_cnt += sg[i].len;
  1252. #ifdef CONFIG_SDC_DMA_USED
  1253. if ((addr & 0x03)) {
  1254. sdc_use_dma = 0;
  1255. SDC_LOGW("unalign addr %lx, len %ld wr %lx\n", HAL_PR_SZ_L(addr), HAL_PR_SZ_L(len), HAL_PR_SZ_L(data->flags & MMC_DATA_WRITE));
  1256. continue;
  1257. }
  1258. if((addr & (OS_CACHE_ALIGN_BYTES - 1))\
  1259. || (len & (OS_CACHE_ALIGN_BYTES - 1))) {
  1260. SDC_LOGD("unalign addr %lx, len %ld wr %lx\n", HAL_PR_SZ_L(addr), HAL_PR_SZ_L(len), HAL_PR_SZ_L(data->flags & MMC_DATA_WRITE));
  1261. }
  1262. HAL_FlushDcacheRegion((unsigned long)sg[i].buffer, HAL_ALIGN(sg[i].len, OS_CACHE_ALIGN_BYTES));
  1263. #endif
  1264. }
  1265. #ifdef CONFIG_SDC_DMA_USED
  1266. if (byte_cnt <= SDC_MAX_CPU_TRANS_LEN) {
  1267. sdc_use_dma = 0;
  1268. }
  1269. #endif
  1270. if (byte_cnt != (data->blksz * data->blocks)) {
  1271. SDC_LOGE("sg data len %ld not equel data's len %ld\n", HAL_PR_SZ_L(byte_cnt), HAL_PR_SZ_L(data->blksz * data->blocks));
  1272. }
  1273. mci_writel(data->blksz, host, SDXC_REG_BLKSZ);
  1274. mci_writel(byte_cnt, host, SDXC_REG_BCNTR);
  1275. if (sdc_use_dma && byte_cnt > SDC_MAX_CPU_TRANS_LEN) { /* transfer by idma */
  1276. __mci_sel_access_mode(host, SDXC_ACCESS_BY_DMA);
  1277. host->dma_hdle = __mci_prepare_dma(host, data);
  1278. if (NULL == host->dma_hdle) {
  1279. SDC_LOGW("SDC prepare DMA failed\n");
  1280. __mci_sel_access_mode(host, SDXC_ACCESS_BY_AHB);
  1281. }
  1282. } else {
  1283. /* switch data bus to ahb */
  1284. __mci_sel_access_mode(host, SDXC_ACCESS_BY_AHB);
  1285. }
  1286. }
  1287. __mci_send_cmd(host, cmd);
  1288. #if defined(CONFIG_SOC_SUN20IW1) && defined(CONFIG_OS_MELIS)
  1289. struct timespec64 time0 ={0}, time1={0};
  1290. do_gettimeofday(&time0);
  1291. ret = SDC_SemPend(&host->lock, SDC_DMA_TIMEOUT);
  1292. do_gettimeofday(&time1);
  1293. if ((ret != HAL_OK && host->present) && \
  1294. (((time1.tv_sec*1000+time1.tv_nsec/1000000) - (time0.tv_sec*1000+time0.tv_nsec/1000000)) < SDC_DMA_TIMEOUT))
  1295. {
  1296. //增加这里的原因是防止读写sdmmc的task被esKRNL_TimeDlyResume,出现这种情况,会导致信号量提前退出
  1297. //如果是正常的timeout则不会跑到这里面
  1298. SDC_LOGE("===SDC_SemPend abnormal===\n");
  1299. ret = SDC_SemPend(&host->lock, SDC_DMA_TIMEOUT);
  1300. }
  1301. #else
  1302. ret = SDC_SemPend(&host->lock, SDC_DMA_TIMEOUT);
  1303. #endif
  1304. if (ret != HAL_OK && host->present){
  1305. uint32_t sg_len = data->sg_len;
  1306. uint32_t i;
  1307. uint32_t rval = 0;
  1308. SDC_LOGE("sdc cmd:%ld, wait command done timeout !!\n",
  1309. HAL_PR_SZ_L(host->smc_cmd & SDXC_CMD_OPCODE));
  1310. host->debug_mask |= ROM_DUMP_MASK | ROM_ERR_MASK;
  1311. #if 1
  1312. __mci_force_dump_host_info(host);
  1313. if (data) {
  1314. SDC_LOGE("dump data blksz %ld,blocks %ld,flags %lx\n", HAL_PR_SZ_L(data->blksz), HAL_PR_SZ_L(data->blocks), HAL_PR_SZ_L(data->flags));
  1315. }
  1316. #endif
  1317. SDC_LOGE("%s,%d\n",__FUNCTION__,__LINE__);
  1318. for (i = 0; i < sg_len; i++) {
  1319. struct scatterlist *sg = data->sg;
  1320. uint32_t *buf_temp = (uint32_t *)sg[i].buffer;
  1321. #ifdef CONFIG_SDC_DMA_USED
  1322. if (HAL_PT_TO_U(sg[i].buffer) & 0x03) {
  1323. sdc_use_dma = 0;
  1324. SDC_LOGW("%s,%d\n",__FUNCTION__,__LINE__);
  1325. break;
  1326. }
  1327. SDC_LOGE("%s,%d\n",__FUNCTION__,__LINE__);
  1328. HAL_InvalidDcacheRegion((unsigned long )(sg[i].buffer), HAL_ALIGN(sg[i].len, OS_CACHE_ALIGN_BYTES));
  1329. SDC_LOGE("%s,%d\n",__FUNCTION__,__LINE__);
  1330. SDC_LOGE("buf addr %lx buffer0 %x buffer1 %x\n", HAL_PR_SZ_L(sg[i].buffer), (unsigned int)buf_temp[0],(unsigned int) buf_temp[1]);
  1331. #endif
  1332. }
  1333. SDC_LOGE("%s,%d\n",__FUNCTION__,__LINE__);
  1334. mci_writel(0xdeb, host, SDXC_REG_TMOUT);
  1335. SDC_LOGE("*force wr smc timeout reg %x*\n",(unsigned int)mci_readl(host, SDXC_REG_TMOUT));
  1336. #if 1
  1337. SDC_LOGE("*force dump sram*\n");
  1338. rval = readl(0x03000000+0x0048);
  1339. rval |= 1 << 4;
  1340. writel(rval, 0x03000000+0x0048);
  1341. sdc_hex_dump_word((void *)SMC1_BASE ,0x100);
  1342. #endif
  1343. SDC_LOGE("%s,%d\n",__FUNCTION__,__LINE__);
  1344. SDC_BUG_ON(1);
  1345. sys_abort();
  1346. /*sdc_hex_dump_word((void *)0x4002000 ,0x200);*/
  1347. host->debug_mask &= ~ROM_DUMP_MASK;
  1348. goto out;
  1349. }
  1350. ret = __mci_request_done(host);
  1351. if (ret) {
  1352. goto out;
  1353. }
  1354. out:
  1355. /* enable debounce */
  1356. __mci_debounce_onoff(host, 1);
  1357. if(data && (data->flags & MMC_DATA_READ )){
  1358. uint32_t i;
  1359. uint32_t byte_cnt = 0;
  1360. struct scatterlist *sg = data->sg;
  1361. uint32_t sg_len = data->sg_len;
  1362. for (i = 0; i < sg_len; i++) {
  1363. byte_cnt += sg[i].len;
  1364. #ifdef CONFIG_SDC_DMA_USED
  1365. if (HAL_PT_TO_U(sg[i].buffer) & 0x03) {
  1366. sdc_use_dma = 0;
  1367. break;
  1368. }
  1369. HAL_InvalidDcacheRegion((unsigned long)(sg[i].buffer), HAL_ALIGN(sg[i].len, OS_CACHE_ALIGN_BYTES));
  1370. #endif
  1371. }
  1372. }
  1373. SDC_MutexUnlock(&host->thread_lock);
  1374. #ifdef SD_PERF_TRACE_ON
  1375. __mci_end_data_time_stats(host, mrq);
  1376. #endif
  1377. return ret;
  1378. }
  1379. int32_t rom_HAL_SDC_Request(struct mmc_host *host, struct mmc_request *mrq)
  1380. {
  1381. int32_t ret = 0;
  1382. #ifdef CONFIG_SDC_DMA_USED
  1383. uint32_t sdc_use_dma_buf = 0;
  1384. struct scatterlist sg_dma = {0};
  1385. struct scatterlist *sg = NULL;
  1386. uint32_t sg_len = 0;
  1387. struct mmc_data *data;
  1388. uint32_t i = 0;
  1389. uint32_t byte_cnt = 0;
  1390. unsigned long addr = 0;
  1391. uint32_t len = 0;
  1392. if (mrq->data) {
  1393. data = mrq->data;
  1394. sg_len = data->sg_len;
  1395. sg = data->sg;
  1396. for (i = 0; i < sg_len; i++) {
  1397. addr = HAL_PT_TO_U(sg[i].buffer);
  1398. len = sg[i].len;
  1399. byte_cnt += sg[i].len;
  1400. if ((addr & 0x03)\
  1401. || (addr & (OS_CACHE_ALIGN_BYTES - 1))
  1402. || (len & (OS_CACHE_ALIGN_BYTES - 1))) {
  1403. sdc_use_dma_buf = 1;
  1404. SDC_LOGD("try to use dma debounce buff, addr %lx, len %ld\n", \
  1405. HAL_PR_SZ_L(addr), HAL_PR_SZ_L(byte_cnt));
  1406. }
  1407. }
  1408. if (sdc_use_dma_buf) {
  1409. if (byte_cnt > SDC_ALIGN_DMA_BUF_SIZE) {
  1410. SDC_LOGE("byte count %ld over dma debounce buf size %d\n", \
  1411. HAL_PR_SZ_L(byte_cnt), SDC_ALIGN_DMA_BUF_SIZE);
  1412. return -1;
  1413. } else if (byte_cnt < SDC_MAX_CPU_TRANS_LEN) {
  1414. SDC_LOGD("byte cnt %ld is too small not use dma align buf\n", \
  1415. HAL_PR_SZ_L(byte_cnt));
  1416. sdc_use_dma_buf = 0;
  1417. } else {
  1418. uint32_t total_len = 0;
  1419. if (data->flags & MMC_DATA_WRITE) {
  1420. for (i = 0; i < sg_len; i++) {
  1421. addr = HAL_PT_TO_U(sg[i].buffer);
  1422. len = sg[i].len;
  1423. HAL_Memcpy((char *)(host->align_dma_buf + total_len), (char *)addr, len);
  1424. total_len += len;
  1425. }
  1426. }
  1427. sg_dma.buffer = host->align_dma_buf;
  1428. sg_dma.len = byte_cnt;
  1429. data->sg = &sg_dma;
  1430. data->sg_len = 1;
  1431. SDC_LOGD("use dma debounce buff addr %lx, len %d, align_dma_buf %lx\n", \
  1432. HAL_PT_TO_U(sg[0].buffer),\
  1433. (unsigned int)byte_cnt,\
  1434. HAL_PT_TO_U(host->align_dma_buf));
  1435. }
  1436. }
  1437. }
  1438. #endif
  1439. ret = do_rom_HAL_SDC_Request(host, mrq);
  1440. #ifdef CONFIG_SDC_DMA_USED
  1441. if(!ret && data && sdc_use_dma_buf) {
  1442. if (data->flags & MMC_DATA_READ) {
  1443. uint32_t total_len = 0;
  1444. for (i = 0; i < sg_len; i++) {
  1445. addr = HAL_PT_TO_U(sg[i].buffer);
  1446. len = sg[i].len;
  1447. HAL_Memcpy((char *)addr, (char *)(host->align_dma_buf + total_len), len);
  1448. total_len += len;
  1449. }
  1450. SDC_LOGD("copy back to dst buffer%ld\n", HAL_PR_SZ_L(byte_cnt));
  1451. }
  1452. data->sg = sg;
  1453. data->sg_len = sg_len;
  1454. }
  1455. #endif
  1456. return ret;
  1457. }
  1458. #ifdef CONFIG_SDC_EXCLUSIVE_HOST
  1459. /**
  1460. * HAL_SDC_Claim_Host - exclusively claim a host
  1461. * @host: mmc host to claim
  1462. *
  1463. * Claim a host for a set of operations.
  1464. */
  1465. int32_t HAL_SDC_Claim_Host(struct mmc_host *host)
  1466. {
  1467. return (SDC_SemPend(&host->exclusive_lock, OS_WAIT_FOREVER) == OS_OK ? 0 : -1);
  1468. }
  1469. /**
  1470. * HAL_SDC_Release_Host - release a host
  1471. * @host: mmc host to release
  1472. *
  1473. * Release a MMC host, allowing others to claim the host
  1474. * for their operations.
  1475. */
  1476. void HAL_SDC_Release_Host(struct mmc_host *host)
  1477. {
  1478. SDC_SemPost(&host->exclusive_lock);
  1479. }
  1480. #endif
  1481. #ifdef CONFIG_SDC_READONLY_USED
  1482. int32_t HAL_SDC_Get_ReadOnly(struct mmc_host *host)
  1483. {
  1484. uint32_t wp_val;
  1485. GPIO_PinMuxParam *ro_gpio = &host->ro_gpio;
  1486. wp_val = (GPIO_PIN_HIGH == HAL_GPIO_ReadPin(ro_gpio->port, ro_gpio->pin)) ? 1 : 0;
  1487. SDC_LOGN("sdc fetch card wp pin status: %u\n", (unsigned int)wp_val);
  1488. if (!wp_val) {
  1489. host->read_only = 0;
  1490. return 0;
  1491. } else {
  1492. SDC_LOGN("Card is write-protected\n");
  1493. host->read_only = 1;
  1494. return 1;
  1495. }
  1496. return 0;
  1497. }
  1498. #endif
  1499. #ifdef CONFIG_DETECT_CARD
  1500. static irqreturn_t __mci_cd_irq(int unuse, void *arg);
  1501. static void __mci_enable_cd_pin_irq(struct mmc_host *host)
  1502. {
  1503. //SDC_LOGD("Not implement %s,%d\n", __FUNCTION__,__LINE__);
  1504. //SDC_ENABLE_IRQ(host->cd_irq);
  1505. hal_gpio_irq_enable(host->cd_irq);
  1506. }
  1507. static void __mci_disable_cd_pin_irq(struct mmc_host *host)
  1508. {
  1509. // HAL_GPIO_DisableIRQ(host->cd_port, host->cd_pin);
  1510. // SDC_LOGE("Not implement %s,%d\n", __FUNCTION__,__LINE__);
  1511. //SDC_DISABLE_IRQ(host->cd_irq);
  1512. hal_gpio_irq_disable(host->cd_irq);
  1513. }
  1514. static void __mci_voltage_stable_det(void *arg)
  1515. {
  1516. struct mmc_host *host = (struct mmc_host *)arg;
  1517. if (!host->present || host->wait_voltage_stable || !host->cd_delay) {
  1518. host->wait_voltage_stable = 0;
  1519. __mci_enable_cd_pin_irq(host);
  1520. host->param.cd_cb(host->present);
  1521. } else {
  1522. SDC_ModTimer(&host->cd_timer, host->cd_delay);
  1523. host->wait_voltage_stable = 1;
  1524. }
  1525. }
  1526. static void __mci_cd_timer(void *arg)
  1527. {
  1528. struct mmc_host *host = (struct mmc_host *)arg;
  1529. uint32_t gpio_val = 0;
  1530. uint32_t present;
  1531. gpio_val = (host->cd_pin_present_val == HAL_GPIO_ReadPin(host->cd_gpio_pin, &gpio_val)) ? 1 : 0;
  1532. //SDC_LOGE("Not implement %s,%d\n", __FUNCTION__,__LINE__);
  1533. if (gpio_val) {
  1534. present = 1;
  1535. } else {
  1536. present = 0;
  1537. }
  1538. SDC_LOGD("cd %lu, host present %lu, cur present %lu\n",
  1539. HAL_PR_SZ_L(gpio_val), HAL_PR_SZ_L(host->present), HAL_PR_SZ_L(present));
  1540. if (host->present ^ present || host->wait_voltage_stable) {
  1541. SDC_LOGD("sdc detect change, present %lu\n", HAL_PR_SZ_L(present));
  1542. host->present = present;
  1543. __mci_voltage_stable_det(host);
  1544. }
  1545. else {
  1546. __mci_enable_cd_pin_irq(host);
  1547. }
  1548. __sdc_init_timeout_flags = 1;
  1549. return ;
  1550. }
  1551. static void __mci_dat3_det(void *arg)
  1552. {
  1553. struct mmc_host *host = (struct mmc_host *)arg;
  1554. SDC_LOGD("***dat3 det***\n");
  1555. __mci_voltage_stable_det(host);
  1556. }
  1557. static irqreturn_t __mci_cd_irq(int unuse, void *arg)
  1558. {
  1559. struct mmc_host *host = (struct mmc_host *)arg;
  1560. SDC_LOGD("***in cd***\n");
  1561. if (!SDC_TimerIsActive(&host->cd_timer) || host->wait_voltage_stable) {
  1562. host->wait_voltage_stable = 0;
  1563. SDC_ModTimer(&host->cd_timer, 10);
  1564. __mci_disable_cd_pin_irq(host);
  1565. }
  1566. return (irqreturn_t)NULL;
  1567. }
  1568. #endif
  1569. int32_t rom_HAL_SDC_PowerOn(struct mmc_host *host)
  1570. {
  1571. uint32_t rval;
  1572. if (!host) {
  1573. SDC_LOGE_RAW(ROM_ERR_MASK, "%s,%d err", __func__, __LINE__);
  1574. return -1;
  1575. }
  1576. SDC_LOGD("MMC Driver init host\n");
  1577. __mci_restore_io(host);
  1578. __mci_clk_prepare_enable(host);
  1579. /* delay 1ms ? */
  1580. __mci_update_clock(host, 400000);
  1581. host->clk = 400000;
  1582. /* reset controller*/
  1583. rval = mci_readl(host, SDXC_REG_GCTRL)|SDXC_HWReset|SDXC_INTEnb|
  1584. SDXC_AccessDoneDirect;
  1585. mci_writel(rval, host, SDXC_REG_GCTRL);
  1586. __mci_check_bit_clear(host, SDXC_REG_GCTRL, SDXC_HWReset);
  1587. SDC_LOGD("%s,%d\n",__FUNCTION__,__LINE__);
  1588. mci_writel(0xffffffff, host, SDXC_REG_RINTR);
  1589. #ifdef CONFIG_DETECT_CARD
  1590. if (host->param.cd_mode == CARD_DETECT_BY_D3) {
  1591. mci_writel(SDXC_CardInsert|SDXC_CardRemove|host->sdio_irq_mask, host, SDXC_REG_IMASK);
  1592. } else
  1593. #endif
  1594. {
  1595. mci_writel(host->sdio_irq_mask, host, SDXC_REG_IMASK);
  1596. }
  1597. #define SDMC_DATA_TIMEOUT 0x0ffffffU
  1598. #define SDMC_RESP_TIMEOUT 0xffU
  1599. /* Set Data & Response Timeout Value */
  1600. mci_writel((SDMC_DATA_TIMEOUT<<8)|SDMC_RESP_TIMEOUT, host, SDXC_REG_TMOUT);
  1601. #undef SDMC_RESP_TIMEOUT
  1602. #undef SDMC_DATA_TIMEOUT
  1603. HAL_SDC_Set_BusWidth(host, MMC_BUS_WIDTH_1);
  1604. #ifdef SDC_DEBUG
  1605. mci_writel(0xdeb, host, SDXC_REG_DBGC);
  1606. mci_writel(0xceaa0000, host, SDXC_REG_FUNS);
  1607. #endif
  1608. #ifdef CONFIG_DETECT_CARD
  1609. if (host->param.cd_mode == CARD_DETECT_BY_D3){
  1610. rval |= SDXC_DebounceEnb;
  1611. }
  1612. mci_writel(rval, host, SDXC_REG_GCTRL);
  1613. #endif
  1614. if (host->sdc_id == 0) {
  1615. SDC_ENABLE_IRQ(SDC0_IRQn);
  1616. }
  1617. #if SDC1_SUPPORT
  1618. else if (host->sdc_id == 1)
  1619. SDC_ENABLE_IRQ(SDC1_IRQn);
  1620. #endif
  1621. host->power_on = 1;
  1622. return 0;
  1623. }
  1624. int32_t rom_HAL_SDC_PowerOff(struct mmc_host *host)
  1625. {
  1626. if (!host) {
  1627. SDC_LOGE_RAW(ROM_ERR_MASK, "%s,%d err", __func__, __LINE__);
  1628. return -1;
  1629. }
  1630. #ifdef CONFIG_DETECT_CARD
  1631. if (host->param.cd_mode != CARD_DETECT_BY_D3)
  1632. #endif
  1633. {
  1634. uint32_t rval;
  1635. if (host->sdc_id == 0)
  1636. SDC_DISABLE_IRQ(SDC0_IRQn);
  1637. #if SDC1_SUPPORT
  1638. else if (host->sdc_id == 1)
  1639. SDC_DISABLE_IRQ(SDC1_IRQn);
  1640. #endif
  1641. rval = mci_readl(host, SDXC_REG_GCTRL) | SDXC_HWReset;
  1642. mci_writel(rval, host, SDXC_REG_GCTRL);
  1643. __mci_check_bit_clear(host, SDXC_REG_GCTRL, SDXC_HWReset);
  1644. rval = mci_readl(host, SDXC_REG_MISTA);
  1645. mci_writel(rval & (~SDXC_SDIOInt), host, SDXC_REG_RINTR);
  1646. if (host->sdc_id == 0)
  1647. SDC_CLEAR_IRQPINGD(SDC0_IRQn);
  1648. #if SDC1_SUPPORT
  1649. else if (host->sdc_id == 1)
  1650. SDC_CLEAR_IRQPINGD(SDC1_IRQn);
  1651. #endif
  1652. __mci_clk_disable_unprepare(host);
  1653. __mci_hold_io(host);
  1654. host->power_on = 0;
  1655. }
  1656. return 0;
  1657. }
  1658. #ifdef CONFIG_SD_PM
  1659. static void __mci_regs_save(struct mmc_host *host)
  1660. {
  1661. struct __mci_ctrl_regs *bak_regs = &host->regs_back;
  1662. bak_regs->gctrl = mci_readl(host, SDXC_REG_GCTRL);
  1663. bak_regs->clkc = mci_readl(host, SDXC_REG_CLKCR);
  1664. bak_regs->timeout = mci_readl(host, SDXC_REG_TMOUT);
  1665. bak_regs->buswid = mci_readl(host, SDXC_REG_WIDTH);
  1666. bak_regs->waterlvl = mci_readl(host, SDXC_REG_FTRGL);
  1667. bak_regs->funcsel = mci_readl(host, SDXC_REG_FUNS);
  1668. bak_regs->idmacc = mci_readl(host, SDXC_REG_DMAC);
  1669. }
  1670. static void __mci_regs_restore(struct mmc_host *host)
  1671. {
  1672. struct __mci_ctrl_regs* bak_regs = &host->regs_back;
  1673. mci_writel(bak_regs->gctrl, host, SDXC_REG_GCTRL);
  1674. mci_writel(bak_regs->clkc, host, SDXC_REG_CLKCR);
  1675. mci_writel(bak_regs->timeout, host, SDXC_REG_TMOUT);
  1676. mci_writel(bak_regs->buswid, host, SDXC_REG_WIDTH);
  1677. mci_writel(bak_regs->waterlvl, host, SDXC_REG_FTRGL);
  1678. mci_writel(bak_regs->funcsel, host, SDXC_REG_FUNS);
  1679. #ifdef SDC_DEBUG
  1680. mci_writel(0xdeb, host, SDXC_REG_DBGC);
  1681. mci_writel(0xceaa0000, host, SDXC_REG_FUNS);
  1682. #endif
  1683. mci_writel(bak_regs->idmacc, host, SDXC_REG_DMAC);
  1684. }
  1685. static int __mci_suspend(struct soc_device *dev, enum suspend_state_t state)
  1686. {
  1687. int ret = 0;
  1688. struct mmc_host *host = dev->platform_data;
  1689. uint32_t _timeout = HAL_Ticks() + HAL_MSecsToTicks(SDC_DMA_TIMEOUT) + 1;
  1690. if (host->bus_ops && host->bus_ops->suspend)
  1691. ret = host->bus_ops->suspend(host);
  1692. if (ret) {
  1693. if (host->bus_ops->resume)
  1694. host->bus_ops->resume(host);
  1695. SDC_LOGE("%s bus id:%d faild\n", __func__, host->sdc_id);
  1696. goto err;
  1697. }
  1698. host->suspend = 1;
  1699. while ((host->wait != SDC_WAIT_NONE) && HAL_TimeBefore(HAL_Ticks(), _timeout)) {
  1700. }
  1701. if (HAL_TimeAfterEqual(HAL_Ticks(), _timeout) && (host->wait != SDC_WAIT_NONE)) {
  1702. ret = -1;
  1703. host->suspend = 0;
  1704. if (host->bus_ops && host->bus_ops->resume)
  1705. ret = host->bus_ops->resume(host);
  1706. SDC_LOGE("%s id:%d faild\n", __func__, host->sdc_id);
  1707. goto err;
  1708. }
  1709. __mci_regs_save(host);
  1710. /* gate clock for lower power */
  1711. if (host->sdc_id == 0) {
  1712. SDC0_DisableMClock();
  1713. SDC0_CCM_BusForceReset();
  1714. #if SDC1_SUPPORT
  1715. } else if (host->sdc_id == 1) {
  1716. SDC1_DisableMClock();
  1717. SDC1_CCM_BusForceReset();
  1718. #endif
  1719. }
  1720. __mci_clk_disable_unprepare(host);
  1721. #ifdef CONFIG_DETECT_CARD
  1722. if (host->param.cd_mode == CARD_DETECT_BY_D3) {
  1723. __mci_exit_host(host);
  1724. if (host->sdc_id == 0) {
  1725. SDC0_DisableMClock();
  1726. SDC0_CCM_BusForceReset();
  1727. #if SDC1_SUPPORT
  1728. } else if (host->sdc_id == 1) {
  1729. SDC1_DisableMClock();
  1730. SDC1_CCM_BusForceReset();
  1731. #endif
  1732. }
  1733. __mci_hold_io(host);
  1734. SDC_LOGD("sdc card_power_off ok\n");
  1735. //__mci_set_vddio(host, SDC_WOLTAGE_OFF);
  1736. //usleep_range(1000, 1500);
  1737. host->power_on = 0;
  1738. }
  1739. #endif
  1740. SDC_LOGD("%s id:%d ok\n", __func__, host->sdc_id);
  1741. err:
  1742. return ret;
  1743. }
  1744. static int __mci_resume(struct soc_device *dev, enum suspend_state_t state)
  1745. {
  1746. int ret = 0;
  1747. struct mmc_host *host = dev->platform_data;
  1748. uint32_t clk = host->clk;
  1749. __mci_restore_io(host);
  1750. if (host->sdc_id == 0) {
  1751. SDC0_CCM_BusForceReset();
  1752. #if SDC1_SUPPORT
  1753. } else if (host->sdc_id == 1) {
  1754. SDC1_CCM_BusForceReset();
  1755. #endif
  1756. }
  1757. __mci_clk_disable_unprepare(host);
  1758. mmc_udelay(35);
  1759. /* enable clock for resotre */
  1760. __mci_clk_prepare_enable(host);
  1761. mmc_udelay(50);
  1762. __mci_update_clock(host, clk);
  1763. mmc_udelay(100);
  1764. __mci_regs_restore(host);
  1765. #ifdef CONFIG_DETECT_CARD
  1766. #else
  1767. mci_writel(SDXC_CardInsert, host, SDXC_REG_RINTR);
  1768. #endif
  1769. __mci_update_clk(host);
  1770. /* register IRQ */
  1771. if (host->sdc_id == 0)
  1772. SDC_CONFIG_IRQ(SDC0_IRQn, &SDC0_IRQHandler, NVIC_PERIPH_PRIO_DEFAULT);
  1773. #if SDC1_SUPPORT
  1774. else if (host->sdc_id == 1)
  1775. SDC_CONFIG_IRQ(SDC1_IRQn, &SDC1_IRQHandler, NVIC_PERIPH_PRIO_DEFAULT);
  1776. #endif
  1777. mmc_udelay(100);
  1778. #ifdef CONFIG_DETECT_CARD
  1779. if (host->param.cd_mode == CARD_DETECT_BY_GPIO_IRQ)
  1780. __mci_cd_timer(host);
  1781. if (host->param.cd_mode == CARD_DETECT_BY_D3) {
  1782. uint32_t rval = 0;
  1783. //__mci_set_vddio(host, host->regulator_voltage);
  1784. //usleep_range(1000, 1500);
  1785. host->power_on = 1;
  1786. __mci_restore_io(host);
  1787. __mci_clk_prepare_enable(host);
  1788. mmc_mdelay(1);
  1789. rval = mci_readl(host, SDXC_REG_RINTR);
  1790. SDC_LOGD(">> REG_RINTR=0x%x\n", (unsigned int)rval);
  1791. (void)rval;
  1792. }
  1793. #endif
  1794. host->suspend = 0;
  1795. if (host->bus_ops && host->bus_ops->resume)
  1796. ret = host->bus_ops->resume(host);
  1797. SDC_LOGD("%s id:%d ok\n", __func__, host->sdc_id);
  1798. return ret;
  1799. }
  1800. static const struct soc_device_driver sdc_drv = {
  1801. .name = "sdc",
  1802. .suspend = __mci_suspend,
  1803. .resume = __mci_resume,
  1804. };
  1805. static struct soc_device sdc_dev[SDC_NUM] = {
  1806. { .name = "sdc0", .driver = &sdc_drv, },
  1807. { .name = "sdc1", .driver = &sdc_drv, }
  1808. };
  1809. #define SDC_DEV(id) (&sdc_dev[id])
  1810. #endif /* CONFIG_SD_PM */
  1811. int hal_sdc_init_timeout(void)
  1812. {
  1813. return __sdc_init_timeout_flags;
  1814. }
  1815. /**
  1816. * @brief Initializes the SDC peripheral.
  1817. * @param sdc_id:
  1818. * @arg sdc_id->SDC ID.
  1819. * @param param:
  1820. * @arg param->[in] The configuration information.
  1821. * @retval SDC handler.
  1822. */
  1823. struct mmc_host *hal_sdc_init(struct mmc_host *host)
  1824. {
  1825. __sdc_init_timeout_flags = 0;
  1826. #ifdef __CONFIG_ARCH_APP_CORE
  1827. const HAL_SDCGPIOCfg *sd_gpio_cfg = NULL;
  1828. const HAL_SDCGPIOCfg sd_gpio_cfg_entry;
  1829. sd_gpio_cfg = &sd_gpio_cfg_entry;
  1830. #endif
  1831. int ret = 0;
  1832. if (host->State != SDC_STATE_RESET) {
  1833. SDC_LOGW("%s----%d reinit sdc!\n", __func__,__LINE__);
  1834. return NULL;
  1835. }
  1836. if (host->sdc_id == 0) {
  1837. host->reg_base = (volatile void *)SMC0_BASE;
  1838. #if SDC1_SUPPORT
  1839. } else if (host->sdc_id == 1) {
  1840. host->reg_base = (volatile void *)SMC1_BASE;
  1841. #endif
  1842. } else {
  1843. SDC_LOGW("%s unsupport sdc id:%d!\n", __func__, host->sdc_id);
  1844. return NULL;
  1845. }
  1846. #ifdef __CONFIG_PLATFORM_FPGA
  1847. host->caps = MMC_CAP_MMC_HIGHSPEED | MMC_CAP_WAIT_WHILE_BUSY |
  1848. MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | MMC_CAP_UHS_SDR50;
  1849. if (HAL_GetDevClock() > 25000000) {
  1850. host->caps |= MMC_CAP_SD_HIGHSPEED;
  1851. }
  1852. #else
  1853. host->caps = MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED | MMC_CAP_WAIT_WHILE_BUSY | \
  1854. MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | MMC_CAP_UHS_SDR50|MMC_CAP_SDIO_IRQ;
  1855. if (host->param.low_speed)
  1856. host->caps &= ~(MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED);
  1857. #endif
  1858. #ifdef CONFIG_SD_PM
  1859. host->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
  1860. #endif
  1861. #ifdef __CONFIG_ARCH_APP_CORE
  1862. #ifdef NUSE_STANDARD_INTERFACE
  1863. if (host->sdc_id == 0) {
  1864. #ifndef SDC_FPGA
  1865. sdmmc_pinctrl_init(host);
  1866. #endif
  1867. host->caps |= MMC_CAP_4_BIT_DATA;
  1868. } else if (host->sdc_id == 1) {
  1869. sdmmc_pinctrl_init(host);
  1870. #if 0
  1871. SDC1_SET_GPIO_PULL();
  1872. SDC1_SET_GPIO_DRV();
  1873. SDC1_SET_GPIO_MUX();
  1874. #endif
  1875. host->caps &= ~MMC_CAP_4_BIT_DATA;
  1876. }
  1877. #else
  1878. HAL_BoardIoctl(HAL_BIR_GET_CFG, HAL_MKDEV(HAL_DEV_MAJOR_SDC, host->sdc_id),
  1879. (uint32_t)&sd_gpio_cfg);
  1880. /*test gpio_detect by gpio irq*/
  1881. sd_gpio_cfg->has_detect_gpio = 1;
  1882. if (!sd_gpio_cfg)
  1883. host->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
  1884. else if (sd_gpio_cfg->data_bits == 8)
  1885. host->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
  1886. else if (sd_gpio_cfg->data_bits == 4)
  1887. host->caps |= MMC_CAP_4_BIT_DATA;
  1888. #endif
  1889. #else
  1890. host->caps |= MMC_CAP_4_BIT_DATA;
  1891. #endif
  1892. __mci_restore_io(host);
  1893. host->clk = 400000;
  1894. /* register IRQ */
  1895. if (host->sdc_id == 0) {
  1896. ret = SDC_REQUEST_IRQ(SDC0_IRQn, SDC0_IRQHandler, host);
  1897. if (ret != 0) {
  1898. SDC_LOGE("%s,%d err:%d\n",__func__, __LINE__, ret);
  1899. return NULL;
  1900. }
  1901. SDC_SetPriority(SDC0_IRQn, NVIC_PERIPH_PRIO_DEFAULT);
  1902. #if SDC1_SUPPORT
  1903. } else if (host->sdc_id == 1) {
  1904. ret = SDC_REQUEST_IRQ(SDC1_IRQn, SDC1_IRQHandler, host);
  1905. if (ret != 0) {
  1906. SDC_LOGE("%s,%d err:%d\n",__func__, __LINE__, ret);
  1907. return NULL;
  1908. }
  1909. SDC_SetPriority(SDC1_IRQn, NVIC_PERIPH_PRIO_DEFAULT);
  1910. #endif
  1911. }
  1912. #ifdef CONFIG_DETECT_CARD
  1913. if (host->param.cd_mode == CARD_ALWAYS_PRESENT) {
  1914. host->present = 1;
  1915. } else if (host->param.cd_mode == CARD_DETECT_BY_GPIO_IRQ) {
  1916. #if 0
  1917. if (!sd_gpio_cfg || (!sd_gpio_cfg->has_detect_gpio)) {
  1918. SDC_LOGE("%s,%d cd_mode:%ld with no detect_gpio!\n",
  1919. __func__, __LINE__, host->param.cd_mode);
  1920. return NULL;
  1921. }
  1922. #endif
  1923. //host->cd_port = sd_gpio_cfg->detect_port;
  1924. //host->cd_pin = sd_gpio_cfg->detect_pin;
  1925. //host->cd_pin_present_val = sd_gpio_cfg->detect_pin_present_val;
  1926. #ifndef CONFIG_DRIVERS_SDC_CDPIN_PRESENT_VAL
  1927. #define CONFIG_DRIVERS_SDC_CDPIN_PRESENT_VAL 0
  1928. #endif
  1929. host->cd_pin_present_val = CONFIG_DRIVERS_SDC_CDPIN_PRESENT_VAL;
  1930. //HAL_BoardIoctl(HAL_BIR_PINMUX_INIT, HAL_MKDEV(HAL_DEV_MAJOR_SDC,
  1931. // host->sdc_id), SDCGPIO_DET);
  1932. SDC_LOGN("%s,%d no imp\n",__func__, __LINE__);
  1933. //host->cd_delay = sd_gpio_cfg->detect_delay;
  1934. host->cd_delay = 10; /* short delay */
  1935. SDC_InitTimer(&host->cd_timer, &__mci_cd_timer, host, 10);
  1936. //HAL_GPIO_SetDebounce(&Irq_param, (2U << 4) | 1); /* set debounce clock */
  1937. __mci_enable_cd_pin_irq(host);
  1938. SDC_LOGN("%s,%d no imp\n",__func__, __LINE__);
  1939. host->present = (host->cd_pin_present_val ==
  1940. HAL_GPIO_ReadPin(host->cd_gpio_pin, &host->present)) ? 1 : 0;
  1941. //host->present = 1;
  1942. } else if (host->param.cd_mode == CARD_DETECT_BY_D3) {
  1943. uint32_t rval;
  1944. //host->cd_delay = sd_gpio_cfg->detect_delay;
  1945. host->cd_delay = 100;
  1946. __mci_clk_prepare_enable(host);
  1947. mmc_mdelay(1);
  1948. host->present = 1;
  1949. rval = mci_readl(host, SDXC_REG_RINTR);
  1950. SDC_LOGD("sdc +> REG_RINTR=0x%x\n", (unsigned int)rval);
  1951. if ((rval & SDXC_CardRemove)) {
  1952. SDC_LOGD("sdc data[3] detect Card Remove\n");
  1953. host->present = 0;
  1954. }
  1955. SDC_InitTimer(&host->cd_timer, &__mci_dat3_det, host, 300);
  1956. }
  1957. #else
  1958. host->present = 1;
  1959. #endif
  1960. host->align_dma_buf = HAL_MallocAlign(SDC_ALIGN_DMA_BUF_SIZE);
  1961. host->idma_des = HAL_MallocAlign(HAL_ALIGN(SDXC_MAX_DES_NUM * sizeof(smc_idma_des), OS_CACHE_ALIGN_BYTES));
  1962. if (HAL_PT_TO_U(host->idma_des) & 0x07U) {
  1963. SDC_LOGE_RAW(ROM_ERR_MASK, "%s malloc not aligned by 8B\n", __func__);
  1964. return NULL;
  1965. }
  1966. SDC_LOGD("idma_des %lx\n",HAL_PR_SZ_L(host->idma_des));
  1967. host->max_blk_count = 8192;
  1968. host->max_blk_size = 4096;
  1969. host->max_req_size = host->max_blk_size * host->max_blk_count;
  1970. host->max_seg_size = host->max_req_size;
  1971. host->max_segs = 128;
  1972. host->ocr_avail = MMC_VDD_28_29 | MMC_VDD_29_30 | MMC_VDD_30_31 | MMC_VDD_31_32
  1973. | MMC_VDD_32_33 | MMC_VDD_33_34;
  1974. SDC_LOGN("SDC Host Capability:0x%x Ocr avail:0x%x\n", (unsigned int)host->caps, (unsigned int)host->ocr_avail);
  1975. /* init semaphore */
  1976. SDC_SemCreate(&host->lock, 0);
  1977. SDC_MutexCreate(&host->thread_lock);
  1978. #ifdef CONFIG_SDC_EXCLUSIVE_HOST
  1979. SDC_SemCreate(&host->exclusive_lock, 1);
  1980. #endif
  1981. #ifdef CONFIG_DETECT_CARD
  1982. if (host->param.cd_mode == CARD_DETECT_BY_D3 && host->present == 0) {
  1983. SDC_LOGD("SDC power init.\n");
  1984. HAL_SDC_PowerOn(host);
  1985. } else if (host->present == 0) {
  1986. /* if card is not present and the card detect mode is not CARD_DETECT_BY_D3,
  1987. * we shutdown io voltage to save power. */
  1988. SDC_LOGD("SDC no card detected, shutdown io voltage.\n");
  1989. __mci_hold_io(host);
  1990. //__mci_set_vddio(host, SDC_VOLTAGE_OFF);
  1991. }
  1992. #endif
  1993. #ifdef CONFIG_SD_PM
  1994. SDC_DEV(host->sdc_id)->platform_data = host;
  1995. pm_register_ops(SDC_DEV(host->sdc_id));
  1996. #endif
  1997. host->State = SDC_STATE_READY;
  1998. host->wait = SDC_WAIT_NONE;
  1999. #ifdef CONFIG_DETECT_CARD
  2000. //if (host->param.cd_mode != CARD_ALWAYS_PRESENT && host->present) {
  2001. if (host->param.cd_mode != CARD_ALWAYS_PRESENT) {
  2002. host->wait_voltage_stable = 1;
  2003. if (host->cd_delay == 0) {
  2004. SDC_ModTimer(&host->cd_timer, 10);
  2005. } else {
  2006. SDC_ModTimer(&host->cd_timer, host->cd_delay);
  2007. }
  2008. }
  2009. SDC_LOGN("SDC cd_mode:%d present_val:%d\n", (unsigned int)host->param.cd_mode,
  2010. (unsigned int)host->cd_pin_present_val);
  2011. #endif
  2012. SDC_LOGN("SDC id:%d dma_use:%d present:%d\n driver version %s init ok.\n", (unsigned int)host->sdc_id,
  2013. (unsigned int)host->dma_use, (unsigned int)host->present ,SDMMC_DRIVER_VERSION);
  2014. #ifdef SDC_FPGA
  2015. SDC_LOGN("SDC ***Warning: use FPGA config,Are you sure?***\n wmb,cache unsupport now\n");
  2016. #endif
  2017. sdc_hex_dump_word((const void *)(SDC_GPIO_BASE+0xb0),0x400);
  2018. sdc_hex_dump_word((const void *)0x4002000, 0x200);
  2019. return host;
  2020. }
  2021. /**
  2022. * @brief DeInitializes the SDC peripheral.
  2023. * @param sdc_id:
  2024. * @arg sdc_id-> SDC ID.
  2025. * @retval None.
  2026. */
  2027. int32_t hal_sdc_deinit(uint32_t sdc_id)
  2028. {
  2029. struct mmc_host *host = _mci_host[sdc_id];
  2030. if (!host) {
  2031. SDC_LOGE_RAW(ROM_ERR_MASK, "%s,%d err", __func__, __LINE__);
  2032. return -1;
  2033. }
  2034. #ifdef CONFIG_DETECT_CARD
  2035. host->param.cd_mode = 0;
  2036. #endif
  2037. #ifdef CONFIG_SD_PM
  2038. pm_unregister_ops(SDC_DEV(host->sdc_id));
  2039. #endif
  2040. __mci_exit_host(host);
  2041. if (host->sdc_id == 0) {
  2042. SDC0_DisableMClock();
  2043. SDC0_CCM_BusForceReset();
  2044. #if SDC1_SUPPORT
  2045. } else if (host->sdc_id == 1) {
  2046. SDC1_DisableMClock();
  2047. SDC1_CCM_BusForceReset();
  2048. #endif
  2049. }
  2050. #ifdef CONFIG_DETECT_CARD
  2051. HAL_BoardIoctl(HAL_BIR_PINMUX_DEINIT, HAL_MKDEV(HAL_DEV_MAJOR_SDC,
  2052. host->sdc_id), SDCGPIO_DET);
  2053. #endif
  2054. #ifdef CONFIG_SDC_EXCLUSIVE_HOST
  2055. SDC_SemDel(&host->exclusive_lock);
  2056. #endif
  2057. SDC_MutexDelete(&host->thread_lock);
  2058. SDC_SemDel(&host->lock);
  2059. HAL_FreeAlign(host->idma_des);
  2060. HAL_FreeAlign(host->align_dma_buf);
  2061. #ifdef CONFIG_DETECT_CARD
  2062. if ((host->param.cd_mode == CARD_DETECT_BY_GPIO_IRQ)||
  2063. (host->param.cd_mode == CARD_DETECT_BY_D3)) {
  2064. SDC_DelTimer(&host->cd_timer);
  2065. }
  2066. #endif
  2067. if (host->sdc_id == 0) {
  2068. SDC_FREE_IRQ(SDC0_IRQn, host);
  2069. #if SDC1_SUPPORT
  2070. } else if (host->sdc_id == 1) {
  2071. SDC_FREE_IRQ(SDC1_IRQn, host);
  2072. #endif
  2073. }
  2074. SDC_LOGD("sdc:%ld deinit ok.\n", HAL_PR_SZ_L(sdc_id));
  2075. return 0;
  2076. }
  2077. /**
  2078. * @brief malloc for sd host.
  2079. * @param sdc_id:
  2080. * @arg host ID.
  2081. * @retval 0 if success or other if failed.
  2082. */
  2083. struct mmc_host *hal_sdc_create(uint32_t sdc_id, SDC_InitTypeDef *param)
  2084. {
  2085. struct mmc_host *host = _mci_host[sdc_id];
  2086. int ret;
  2087. rt_kprintf("hal_sdc_create %d\n",sdc_id);
  2088. if (host) {
  2089. SDC_LOGE_RAW(ROM_ERR_MASK, "%s has already created!\n", __func__);
  2090. return host;
  2091. }
  2092. host = HAL_Malloc(sizeof(struct mmc_host));
  2093. if (host == NULL) {
  2094. SDC_LOGE_RAW(ROM_ERR_MASK, "%s malloc fail\n", __func__);
  2095. } else {
  2096. SDC_Memset(host, 0, sizeof(struct mmc_host));
  2097. host->sdc_id = sdc_id;
  2098. host->ref = 0;
  2099. #ifdef __CONFIG_ARCH_APP_CORE
  2100. memcpy(&host->param, param, sizeof(SDC_InitTypeDef));
  2101. host->debug_mask = param->debug_mask;
  2102. host->dma_use = param->dma_use;
  2103. #ifdef CONFIG_DETECT_CARD
  2104. if (param->cd_mode == CARD_DETECT_BY_GPIO_IRQ || param->cd_mode == CARD_DETECT_BY_D3) {
  2105. host->cd_gpio_isr = __mci_cd_irq;
  2106. ret = mmc_gpiod_request_cd_irq(host);
  2107. if (!param->cd_cb && ret < 0) {
  2108. HAL_Free(host);
  2109. SDC_LOGE("%s,%d cd_mode:%ld with no cb!\n",
  2110. __func__, __LINE__, HAL_PR_SZ_L(param->cd_mode));
  2111. return NULL;
  2112. }
  2113. }
  2114. #endif
  2115. #endif
  2116. _mci_host[sdc_id] = host;
  2117. SDC_LOGN("%s host:%p id:%d\n", __func__, host, host->sdc_id);
  2118. }
  2119. return host;
  2120. }
  2121. /**
  2122. * @brief free for sd host.
  2123. * @param host:
  2124. * @arg host ID.
  2125. * @param flg:
  2126. * @arg 0:normal delete, 1:unnormal delete, internal use.
  2127. * @retval 0 if success or other if failed.
  2128. */
  2129. int32_t hal_sdc_destroy(struct mmc_host *host)
  2130. {
  2131. if (host == NULL) {
  2132. SDC_LOGW_RAW(ROM_WRN_MASK, "%s host not exit!\n", __func__);
  2133. return -1;
  2134. }
  2135. if (host->ref != 0) {
  2136. SDC_LOGW("%s fail, ref:%d\n", __func__, host->ref);
  2137. return -1;
  2138. }
  2139. _mci_host[host->sdc_id] = NULL;
  2140. SDC_LOGD("%s host:%p id:%d\n", __func__, host, host->sdc_id);
  2141. HAL_Free(host);
  2142. return 0;
  2143. }
  2144. /**
  2145. * @brief get pointer of mmc_host.
  2146. * @param sdc_id:
  2147. * @arg host ID.
  2148. * @retval pointer of mmc_card if success or NULL if failed.
  2149. */
  2150. struct mmc_host *hal_sdc_open(uint32_t sdc_id)
  2151. {
  2152. struct mmc_host *host = _mci_host[sdc_id];
  2153. if (host == NULL || host->sdc_id != sdc_id) {
  2154. SDC_LOGW_RAW(ROM_WRN_MASK, "%s host not exit! id:%d,host:%x\n", __func__, (unsigned int)sdc_id, HAL_PR_SZ(HAL_PT_TO_U(host)));
  2155. return NULL;
  2156. }
  2157. host->ref++;
  2158. return host;
  2159. }
  2160. /**
  2161. * @brief close mmc_host.
  2162. * @param host_id:
  2163. * @arg host ID.
  2164. * @retval 0 if success or other if failed.
  2165. */
  2166. uint32_t hal_sdc_close(uint32_t sdc_id)
  2167. {
  2168. struct mmc_host *host = _mci_host[sdc_id];
  2169. if (host == NULL || host->sdc_id != sdc_id || host->ref < 1) {
  2170. SDC_LOGW_RAW(ROM_WRN_MASK, "%s fail! id:%ld\n", __func__, HAL_PR_SZ_L(sdc_id));
  2171. return -1;
  2172. }
  2173. host->ref--;
  2174. return 0;
  2175. }