dev_sdhci.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-08-16 zhujiale first version
  9. */
  10. #include <rtthread.h>
  11. #define DBG_TAG "SDHCI"
  12. #ifdef RT_SDIO_DEBUG
  13. #define DBG_LVL DBG_LOG
  14. #else
  15. #define DBG_LVL DBG_INFO
  16. #endif /* RT_SDIO_DEBUG */
  17. #include <rtdbg.h>
  18. #include <cpuport.h>
  19. #include <mm_aspace.h>
  20. #include "dev_sdio_dm.h"
  21. static unsigned int debug_quirks = 0;
  22. static unsigned int debug_quirks2;
  23. /********************************************************* */
  24. /* cmd */
  25. /********************************************************* */
  26. void rt_sdhci_read_reg_debug(struct rt_sdhci_host *host)
  27. {
  28. rt_kprintf("0x00 addddddddddddd = %x\n", rt_sdhci_readl(host, 0x00));
  29. rt_kprintf("0x04 EMMC_BLOCKSIZE = %x\n", rt_sdhci_readw(host, 0x04));
  30. rt_kprintf("0x06 EMMC_BLOCKCOUNT = %x\n", rt_sdhci_readw(host, 0x06));
  31. rt_kprintf("0x08 RT_SDHCI_ARGUMENT = %x\n", rt_sdhci_readl(host, 0x08));
  32. rt_kprintf("0x0c EMMC_XFER_MODE = %x\n", rt_sdhci_readw(host, 0x0c));
  33. rt_kprintf("0x0e RT_SDHCI_COMMAND = %x\n", rt_sdhci_readw(host, 0x0e));
  34. rt_kprintf("0x24 RT_SDHCI_PRESENT_STATE = %x\n", rt_sdhci_readl(host, 0x24));
  35. rt_kprintf("0x28 RT_SDHCI_HOST_CONTROL = %x\n", rt_sdhci_readb(host, 0x28));
  36. rt_kprintf("0x29 RT_SDHCI_POWER_CONTROL = %x\n", rt_sdhci_readb(host, 0x29));
  37. rt_kprintf("0x2a EMMC_BGAP_CTRL = %x\n", rt_sdhci_readb(host, 0x2a));
  38. rt_kprintf("0x2c EMMC_CLK_CTRL = %x\n", rt_sdhci_readw(host, 0x2c));
  39. rt_kprintf("0x2e EMMC_TOUT_CTRL = %x\n", rt_sdhci_readb(host, 0x2e));
  40. rt_kprintf("0x2f EMMC_SW_RST = %x\n", rt_sdhci_readb(host, 0x2f));
  41. rt_kprintf("0x30 RT_SDHCI_INT_STATUS = %x\n", rt_sdhci_readw(host, 0x30));
  42. rt_kprintf("0x32 RT_SDHCI_ERR_INT_STATUS = %x\n", rt_sdhci_readw(host, 0x32));
  43. rt_kprintf("0x34 RT_SDHCI_INT_ENABLE = %x\n", rt_sdhci_readw(host, 0x34));
  44. rt_kprintf("0x36 EMMC ERROR INT STATEN = %x\n", rt_sdhci_readw(host, 0x36));
  45. rt_kprintf("0x38 EMMC NORMAL INT SIGNAL EN = %x\n", rt_sdhci_readw(host, 0x38));
  46. rt_kprintf("0x3a EMMC ERROR INT SIGNAL EN = %x\n", rt_sdhci_readw(host, 0x3a));
  47. rt_kprintf("0x3c EMMC_AUTO_CMD_STAT = %x\n", rt_sdhci_readw(host, 0x3c));
  48. rt_kprintf("0x3e EMMC_HOST_CTRL2 = %x\n", rt_sdhci_readw(host, 0x3e));
  49. rt_kprintf("0x40 EMMC_CAPABILITIES1 = %x\n", rt_sdhci_readl(host, 0x40));
  50. rt_kprintf("0x44 EMMC_CAPABILITIES2 = %x\n", rt_sdhci_readl(host, 0x44));
  51. rt_kprintf("0x52 EMMC_FORC_ERR_INT_STAT = %x\n", rt_sdhci_readw(host, 0x52));
  52. rt_kprintf("0x54 EMMC_ADMA_ERR_STAT = %x\n", rt_sdhci_readb(host, 0x54));
  53. rt_kprintf("0x58 EMMC_ADMA_SA = %x\n", rt_sdhci_readl(host, 0x58));
  54. rt_kprintf("0x66 EMMC_PRESET_SDR12 = %x\n", rt_sdhci_readw(host, 0x66));
  55. rt_kprintf("0x68 EMMC_PRESET_SDR25 = %x\n", rt_sdhci_readw(host, 0x68));
  56. rt_kprintf("0x6a EMMC_PRESET_SDR50 = %x\n", rt_sdhci_readw(host, 0x6a));
  57. rt_kprintf("0x6c EMMC_PRESET_SDR104 = %x\n", rt_sdhci_readw(host, 0x6c));
  58. rt_kprintf("0x6e EMMC_PRESET_DDR50 = %x\n", rt_sdhci_readw(host, 0x6e));
  59. rt_kprintf("0x78 EMMC_ADMA_ID = %x\n", rt_sdhci_readl(host, 0x78));
  60. rt_kprintf("0xfe EMMC_HOST_CNTRL_VERS = %x\n", rt_sdhci_readw(host, 0xfe));
  61. }
  62. rt_inline rt_bool_t sdhci_has_requests(struct rt_sdhci_host *host)
  63. {
  64. return host->cmd || host->data_cmd;
  65. }
  66. rt_inline rt_bool_t sdhci_auto_cmd23(struct rt_sdhci_host *host,
  67. struct rt_mmcsd_req *mrq)
  68. {
  69. return mrq->sbc && (host->flags & RT_SDHCI_AUTO_CMD23);
  70. }
  71. rt_inline rt_bool_t sdhci_auto_cmd12(struct rt_sdhci_host *host,
  72. struct rt_mmcsd_req *mrq)
  73. {
  74. return !mrq->sbc && (host->flags & RT_SDHCI_AUTO_CMD12) && !mrq->cap_cmd_during_tfr;
  75. }
  76. rt_inline rt_bool_t sdhci_manual_cmd23(struct rt_sdhci_host *host,
  77. struct rt_mmcsd_req *mrq)
  78. {
  79. return mrq->sbc && !(host->flags & RT_SDHCI_AUTO_CMD23);
  80. }
  81. rt_inline rt_bool_t sdhci_data_line_cmd(struct rt_mmcsd_cmd *cmd)
  82. {
  83. return cmd->data || cmd->flags & MMC_RSP_BUSY;
  84. }
  85. static void sdhci_do_enable_v4_mode(struct rt_sdhci_host *host)
  86. {
  87. rt_uint16_t ctrl2;
  88. ctrl2 = rt_sdhci_readw(host, RT_SDHCI_HOST_CONTROL2);
  89. if (ctrl2 & RT_SDHCI_CTRL_V4_MODE)
  90. {
  91. return;
  92. }
  93. ctrl2 |= RT_SDHCI_CTRL_V4_MODE;
  94. rt_sdhci_writew(host, ctrl2, RT_SDHCI_HOST_CONTROL2);
  95. }
  96. void rt_sdhci_enable_v4_mode(struct rt_sdhci_host *host)
  97. {
  98. host->v4_mode = RT_TRUE;
  99. sdhci_do_enable_v4_mode(host);
  100. }
  101. void rt_sdhci_data_irq_timeout(struct rt_sdhci_host *host, rt_bool_t enable)
  102. {
  103. if (enable)
  104. {
  105. host->ier |= RT_SDHCI_INT_DATA_TIMEOUT;
  106. }
  107. else
  108. {
  109. host->ier &= ~RT_SDHCI_INT_DATA_TIMEOUT;
  110. }
  111. rt_sdhci_writel(host, host->ier, RT_SDHCI_INT_ENABLE);
  112. rt_sdhci_writel(host, host->ier, RT_SDHCI_SIGNAL_ENABLE);
  113. }
  114. void rt_sdhci_set_uhs(struct rt_sdhci_host *host, unsigned timing)
  115. {
  116. rt_uint16_t ctrl_2;
  117. ctrl_2 = rt_sdhci_readw(host, RT_SDHCI_HOST_CONTROL2);
  118. ctrl_2 &= ~RT_SDHCI_CTRL_UHS_MASK;
  119. if ((timing == MMC_TIMING_MMC_HS200) || (timing == MMC_TIMING_UHS_SDR104))
  120. {
  121. ctrl_2 |= RT_SDHCI_CTRL_UHS_SDR104;
  122. }
  123. else if (timing == MMC_TIMING_UHS_SDR12)
  124. {
  125. ctrl_2 |= RT_SDHCI_CTRL_UHS_SDR12;
  126. }
  127. else if (timing == MMC_TIMING_UHS_SDR25)
  128. {
  129. ctrl_2 |= RT_SDHCI_CTRL_UHS_SDR25;
  130. }
  131. else if (timing == MMC_TIMING_UHS_SDR50)
  132. {
  133. ctrl_2 |= RT_SDHCI_CTRL_UHS_SDR50;
  134. }
  135. else if ((timing == MMC_TIMING_UHS_DDR50) || (timing == MMC_TIMING_MMC_DDR52))
  136. {
  137. ctrl_2 |= RT_SDHCI_CTRL_UHS_DDR50;
  138. }
  139. else if (timing == MMC_TIMING_MMC_HS400)
  140. {
  141. ctrl_2 |= RT_SDHCI_CTRL_HS400; /* Non-standard */
  142. }
  143. rt_sdhci_writew(host, ctrl_2, RT_SDHCI_HOST_CONTROL2);
  144. }
  145. void rt_sdhci_set_bus_width(struct rt_sdhci_host *host, int width)
  146. {
  147. rt_uint8_t ctrl;
  148. ctrl = rt_sdhci_readb(host, RT_SDHCI_HOST_CONTROL);
  149. if (width == MMC_BUS_WIDTH_8)
  150. {
  151. ctrl &= ~RT_SDHCI_CTRL_4BITBUS;
  152. ctrl |= RT_SDHCI_CTRL_8BITBUS;
  153. }
  154. else
  155. {
  156. if (host->mmc->caps & MMC_CAP_8_BIT_DATA)
  157. {
  158. ctrl &= ~RT_SDHCI_CTRL_8BITBUS;
  159. }
  160. if (width == MMC_BUS_WIDTH_4)
  161. {
  162. ctrl |= RT_SDHCI_CTRL_4BITBUS;
  163. }
  164. else
  165. {
  166. ctrl &= ~RT_SDHCI_CTRL_4BITBUS;
  167. }
  168. }
  169. rt_sdhci_writeb(host, ctrl, RT_SDHCI_HOST_CONTROL);
  170. }
  171. rt_inline rt_bool_t sdhci_can_64bit_dma(struct rt_sdhci_host *host)
  172. {
  173. if (host->version >= RT_SDHCI_SPEC_410 && host->v4_mode)
  174. {
  175. return host->caps & RT_SDHCI_CAN_64BIT_V4;
  176. }
  177. return host->caps & RT_SDHCI_CAN_64BIT;
  178. }
  179. void rt_sdhci_cleanup_host(struct rt_sdhci_host *host)
  180. {
  181. struct rt_mmc_host *mmc = host->mmc;
  182. if (host->sdhci_core_to_disable_vqmmc)
  183. {
  184. rt_regulator_disable(mmc->rthost.supply.vqmmc);
  185. }
  186. }
  187. static void sdhci_set_default_irqs(struct rt_sdhci_host *host)
  188. {
  189. host->ier = RT_SDHCI_INT_BUS_POWER | RT_SDHCI_INT_DATA_END_BIT |
  190. RT_SDHCI_INT_DATA_CRC | RT_SDHCI_INT_DATA_TIMEOUT |
  191. RT_SDHCI_INT_INDEX | RT_SDHCI_INT_END_BIT | RT_SDHCI_INT_CRC |
  192. RT_SDHCI_INT_TIMEOUT | RT_SDHCI_INT_DATA_END | RT_SDHCI_INT_RESPONSE;
  193. if (host->tuning_mode == RT_SDHCI_TUNING_MODE_2 ||
  194. host->tuning_mode == RT_SDHCI_TUNING_MODE_3)
  195. {
  196. host->ier |= RT_SDHCI_INT_RETUNE;
  197. }
  198. rt_sdhci_writel(host, host->ier, RT_SDHCI_INT_ENABLE);
  199. rt_sdhci_writel(host, host->ier, RT_SDHCI_SIGNAL_ENABLE);
  200. }
  201. rt_inline void sdhci_auto_cmd_select(struct rt_sdhci_host *host,
  202. struct rt_mmcsd_cmd *cmd,
  203. rt_uint16_t *mode)
  204. {
  205. rt_uint16_t ctrl2;
  206. rt_bool_t use_cmd23 = sdhci_auto_cmd23(host, cmd->mrq);
  207. rt_bool_t use_cmd12 = sdhci_auto_cmd12(host, cmd->mrq) && (cmd->cmd_code != SD_IO_RW_EXTENDED);
  208. if (host->version >= RT_SDHCI_SPEC_410 && host->v4_mode && (use_cmd12 || use_cmd23))
  209. {
  210. *mode |= RT_SDHCI_TRNS_AUTO_SEL;
  211. ctrl2 = rt_sdhci_readw(host, RT_SDHCI_HOST_CONTROL2);
  212. if (use_cmd23)
  213. {
  214. ctrl2 |= RT_SDHCI_CMD23_ENABLE;
  215. }
  216. else
  217. {
  218. ctrl2 &= ~RT_SDHCI_CMD23_ENABLE;
  219. }
  220. rt_sdhci_writew(host, ctrl2, RT_SDHCI_HOST_CONTROL2);
  221. return;
  222. }
  223. if (use_cmd12)
  224. {
  225. *mode |= RT_SDHCI_TRNS_AUTO_CMD12;
  226. }
  227. else if (use_cmd23)
  228. {
  229. *mode |= RT_SDHCI_TRNS_AUTO_CMD23;
  230. }
  231. }
  232. static rt_bool_t sdhci_present_error(struct rt_sdhci_host *host,
  233. struct rt_mmcsd_cmd *cmd, rt_bool_t present)
  234. {
  235. if (!present || host->flags & RT_SDHCI_DEVICE_DEAD)
  236. {
  237. cmd->err = -ENOMEDIUM;
  238. return RT_TRUE;
  239. }
  240. return RT_FALSE;
  241. }
  242. static rt_uint16_t sdhci_get_preset_value(struct rt_sdhci_host *host)
  243. {
  244. rt_uint16_t preset = 0;
  245. switch (host->timing)
  246. {
  247. case MMC_TIMING_MMC_HS:
  248. case MMC_TIMING_SD_HS:
  249. preset = rt_sdhci_readw(host, RT_SDHCI_PRESET_FOR_HIGH_SPEED);
  250. break;
  251. case MMC_TIMING_UHS_SDR12:
  252. preset = rt_sdhci_readw(host, RT_SDHCI_PRESET_FOR_SDR12);
  253. break;
  254. case MMC_TIMING_UHS_SDR25:
  255. preset = rt_sdhci_readw(host, RT_SDHCI_PRESET_FOR_SDR25);
  256. break;
  257. case MMC_TIMING_UHS_SDR50:
  258. preset = rt_sdhci_readw(host, RT_SDHCI_PRESET_FOR_SDR50);
  259. break;
  260. case MMC_TIMING_UHS_SDR104:
  261. case MMC_TIMING_MMC_HS200:
  262. preset = rt_sdhci_readw(host, RT_SDHCI_PRESET_FOR_SDR104);
  263. break;
  264. case MMC_TIMING_UHS_DDR50:
  265. case MMC_TIMING_MMC_DDR52:
  266. preset = rt_sdhci_readw(host, RT_SDHCI_PRESET_FOR_DDR50);
  267. break;
  268. case MMC_TIMING_MMC_HS400:
  269. preset = rt_sdhci_readw(host, RT_SDHCI_PRESET_FOR_HS400);
  270. break;
  271. default:
  272. preset = rt_sdhci_readw(host, RT_SDHCI_PRESET_FOR_SDR12);
  273. break;
  274. }
  275. return preset;
  276. }
  277. static void sdhci_set_card_detection(struct rt_sdhci_host *host, rt_bool_t enable)
  278. {
  279. rt_uint32_t present;
  280. if ((host->quirks & RT_SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
  281. !mmc_card_is_removable(host->mmc))
  282. {
  283. return;
  284. }
  285. if (enable)
  286. {
  287. present = rt_sdhci_readl(host, RT_SDHCI_PRESENT_STATE) & RT_SDHCI_CARD_PRESENT;
  288. host->ier |= present ? RT_SDHCI_INT_CARD_REMOVE : RT_SDHCI_INT_CARD_INSERT;
  289. }
  290. else
  291. {
  292. host->ier &= ~(RT_SDHCI_INT_CARD_REMOVE | RT_SDHCI_INT_CARD_INSERT);
  293. }
  294. rt_sdhci_writel(host, host->ier, RT_SDHCI_INT_ENABLE);
  295. rt_sdhci_writel(host, host->ier, RT_SDHCI_SIGNAL_ENABLE);
  296. }
  297. static void sdhci_enable_card_detection(struct rt_sdhci_host *host)
  298. {
  299. sdhci_set_card_detection(host, RT_TRUE);
  300. }
  301. /********************************************************* */
  302. /* reset */
  303. /********************************************************* */
  304. enum sdhci_reset_reason
  305. {
  306. RT_SDHCI_RESET_FOR_INIT,
  307. RT_SDHCI_RESET_FOR_REQUEST_ERROR,
  308. RT_SDHCI_RESET_FOR_REQUEST_ERROR_DATA_ONLY,
  309. RT_SDHCI_RESET_FOR_TUNING_ABORT,
  310. RT_SDHCI_RESET_FOR_CARD_REMOVED,
  311. RT_SDHCI_RESET_FOR_CQE_RECOVERY,
  312. };
  313. static rt_bool_t sdhci_needs_reset(struct rt_sdhci_host *host, struct rt_mmcsd_req *mrq)
  314. {
  315. return (!(host->flags & RT_SDHCI_DEVICE_DEAD) &&
  316. ((mrq->cmd && mrq->cmd->err) || (mrq->sbc && mrq->sbc->err) ||
  317. (mrq->data && mrq->data->stop && mrq->data->stop->err) ||
  318. (host->quirks & RT_SDHCI_QUIRK_RESET_AFTER_REQUEST)));
  319. }
  320. static rt_bool_t sdhci_do_reset(struct rt_sdhci_host *host, rt_uint8_t mask)
  321. {
  322. if (host->quirks & RT_SDHCI_QUIRK_NO_CARD_NO_RESET)
  323. {
  324. struct rt_mmc_host *mmc = host->mmc;
  325. if (!mmc->ops->get_cd(mmc))
  326. {
  327. return RT_FALSE;
  328. }
  329. }
  330. if (host->ops->reset)
  331. {
  332. host->ops->reset(host, mask);
  333. }
  334. return RT_TRUE;
  335. }
  336. static void sdhci_reset_for_reason(struct rt_sdhci_host *host,
  337. enum sdhci_reset_reason reason)
  338. {
  339. if (host->quirks2 & RT_SDHCI_QUIRK2_ISSUE_CMD_DAT_RESET_TOGETHER)
  340. {
  341. sdhci_do_reset(host, RT_SDHCI_RESET_CMD | RT_SDHCI_RESET_DATA);
  342. return;
  343. }
  344. switch (reason)
  345. {
  346. case RT_SDHCI_RESET_FOR_INIT:
  347. sdhci_do_reset(host, RT_SDHCI_RESET_CMD | RT_SDHCI_RESET_DATA);
  348. break;
  349. case RT_SDHCI_RESET_FOR_REQUEST_ERROR:
  350. case RT_SDHCI_RESET_FOR_TUNING_ABORT:
  351. case RT_SDHCI_RESET_FOR_CARD_REMOVED:
  352. case RT_SDHCI_RESET_FOR_CQE_RECOVERY:
  353. sdhci_do_reset(host, RT_SDHCI_RESET_CMD);
  354. sdhci_do_reset(host, RT_SDHCI_RESET_DATA);
  355. break;
  356. case RT_SDHCI_RESET_FOR_REQUEST_ERROR_DATA_ONLY:
  357. sdhci_do_reset(host, RT_SDHCI_RESET_DATA);
  358. break;
  359. }
  360. }
  361. #define sdhci_reset_for(h, r) sdhci_reset_for_reason((h), RT_SDHCI_RESET_FOR_##r)
  362. static void sdhci_reset_for_all(struct rt_sdhci_host *host)
  363. {
  364. if (sdhci_do_reset(host, RT_SDHCI_RESET_ALL))
  365. {
  366. if (host->flags & (RT_SDHCI_USE_SDMA))
  367. {
  368. if (host->ops->enable_dma)
  369. {
  370. host->ops->enable_dma(host);
  371. }
  372. }
  373. host->preset_enabled = RT_FALSE;
  374. }
  375. }
  376. static void sdhci_runtime_pm_bus_on(struct rt_sdhci_host *host)
  377. {
  378. if (host->bus_on)
  379. {
  380. return;
  381. }
  382. host->bus_on = RT_TRUE;
  383. }
  384. static void sdhci_runtime_pm_bus_off(struct rt_sdhci_host *host)
  385. {
  386. if (!host->bus_on)
  387. {
  388. return;
  389. }
  390. host->bus_on = RT_FALSE;
  391. }
  392. void rt_sdhci_reset(struct rt_sdhci_host *host, rt_uint8_t mask)
  393. {
  394. rt_ssize_t timeout;
  395. rt_sdhci_writeb(host, mask, RT_SDHCI_SOFTWARE_RESET);
  396. if (mask & RT_SDHCI_RESET_ALL)
  397. {
  398. host->clock = 0;
  399. if (host->quirks2 & RT_SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
  400. {
  401. sdhci_runtime_pm_bus_off(host);
  402. }
  403. }
  404. timeout = rt_tick_from_millisecond(150);
  405. while (RT_TRUE)
  406. {
  407. timeout = timeout - rt_tick_get();
  408. if (!(rt_sdhci_readb(host, RT_SDHCI_SOFTWARE_RESET) & mask))
  409. {
  410. break;
  411. }
  412. if (timeout < 0)
  413. {
  414. rt_kprintf("%s: Reset 0x%x never completed.\n",
  415. mmc_hostname(host->mmc), (int)mask);
  416. rt_sdhci_read_reg_debug(host);
  417. return;
  418. }
  419. rt_hw_us_delay(10);
  420. }
  421. }
  422. /********************************************************* */
  423. /* data */
  424. /********************************************************* */
  425. static rt_ubase_t sdhci_sdma_address(struct rt_sdhci_host *host)
  426. {
  427. return (rt_ubase_t)rt_kmem_v2p(host->data->buf);
  428. }
  429. static void sdhci_set_adma_addr(struct rt_sdhci_host *host, rt_uint32_t addr)
  430. {
  431. rt_sdhci_writel(host, rt_lower_32_bits(addr), RT_SDHCI_ADMA_ADDRESS);
  432. if (host->flags & RT_SDHCI_USE_64_BIT_DMA)
  433. {
  434. rt_sdhci_writel(host, rt_upper_32_bits(addr), RT_SDHCI_ADMA_ADDRESS_HI);
  435. }
  436. }
  437. static void sdhci_set_sdma_addr(struct rt_sdhci_host *host, rt_uint32_t addr)
  438. {
  439. if (host->v4_mode)
  440. {
  441. sdhci_set_adma_addr(host, addr);
  442. }
  443. else
  444. {
  445. rt_sdhci_writel(host, addr, RT_SDHCI_DMA_ADDRESS);
  446. }
  447. }
  448. static void sdhci_config_dma(struct rt_sdhci_host *host)
  449. {
  450. rt_uint8_t ctrl;
  451. rt_uint16_t ctrl2;
  452. if (host->version < RT_SDHCI_SPEC_200)
  453. {
  454. return;
  455. }
  456. ctrl = rt_sdhci_readb(host, RT_SDHCI_HOST_CONTROL);
  457. ctrl &= ~RT_SDHCI_CTRL_DMA_MASK;
  458. if (!(host->flags & RT_SDHCI_REQ_USE_DMA))
  459. {
  460. goto _out;
  461. }
  462. /* Note if DMA Select is zero then SDMA is selected */
  463. if (host->flags & RT_SDHCI_USE_64_BIT_DMA)
  464. {
  465. if (host->v4_mode)
  466. {
  467. ctrl2 = rt_sdhci_readw(host, RT_SDHCI_HOST_CONTROL2);
  468. ctrl2 |= RT_SDHCI_CTRL_64BIT_ADDR;
  469. rt_sdhci_writew(host, ctrl2, RT_SDHCI_HOST_CONTROL2);
  470. }
  471. }
  472. _out:
  473. rt_sdhci_writeb(host, ctrl, RT_SDHCI_HOST_CONTROL);
  474. }
  475. rt_inline void sdhci_set_block_info(struct rt_sdhci_host *host,
  476. struct rt_mmcsd_data *data)
  477. {
  478. int boundary;
  479. rt_size_t total_size = data->blks * data->blksize;
  480. if (total_size <= 512)
  481. {
  482. boundary = 0; /* 4k bytes*/
  483. }
  484. else if (total_size <= 1024)
  485. {
  486. boundary = 1; /* 8 KB*/
  487. }
  488. else if (total_size <= 2048)
  489. {
  490. boundary = 2; /* 16 KB*/
  491. }
  492. else if (total_size <= 4096)
  493. {
  494. boundary = 3; /* 32 KB*/
  495. }
  496. else if (total_size <= 8192)
  497. {
  498. boundary = 4; /* 64 KB*/
  499. }
  500. else if (total_size <= 16384)
  501. {
  502. boundary = 5; /* 128 KB*/
  503. }
  504. else if (total_size <= 32768)
  505. {
  506. boundary = 6; /* 256 KB*/
  507. }
  508. else
  509. {
  510. boundary = 7; /* 512 KB*/
  511. }
  512. rt_sdhci_writew(host, RT_SDHCI_MAKE_BLKSZ(boundary, data->blksize),
  513. RT_SDHCI_BLOCK_SIZE);
  514. if (host->version >= RT_SDHCI_SPEC_410 &&
  515. host->v4_mode && (host->quirks2 & RT_SDHCI_QUIRK2_USE_32BIT_BLK_CNT))
  516. {
  517. if (rt_sdhci_readw(host, RT_SDHCI_BLOCK_COUNT))
  518. {
  519. rt_sdhci_writew(host, 0, RT_SDHCI_BLOCK_COUNT);
  520. }
  521. rt_sdhci_writew(host, data->blks, RT_SDHCI_32BIT_BLK_CNT);
  522. }
  523. else
  524. {
  525. rt_sdhci_writew(host, data->blks, RT_SDHCI_BLOCK_COUNT);
  526. }
  527. }
  528. static void sdhci_set_transfer_irqs(struct rt_sdhci_host *host)
  529. {
  530. rt_uint32_t dma_irqs = RT_SDHCI_INT_DMA_END;
  531. rt_uint32_t pio_irqs = RT_SDHCI_INT_DATA_AVAIL | RT_SDHCI_INT_SPACE_AVAIL;
  532. if (host->flags & RT_SDHCI_REQ_USE_DMA)
  533. {
  534. host->ier = (host->ier & ~pio_irqs) | dma_irqs;
  535. }
  536. else
  537. {
  538. host->ier = (host->ier & ~dma_irqs) | pio_irqs;
  539. }
  540. if (host->flags & (RT_SDHCI_AUTO_CMD23 | RT_SDHCI_AUTO_CMD12))
  541. {
  542. host->ier |= RT_SDHCI_INT_AUTO_CMD_ERR;
  543. }
  544. else
  545. {
  546. host->ier &= ~RT_SDHCI_INT_AUTO_CMD_ERR;
  547. }
  548. rt_sdhci_writel(host, host->ier, RT_SDHCI_INT_ENABLE);
  549. rt_sdhci_writel(host, host->ier, RT_SDHCI_SIGNAL_ENABLE);
  550. }
  551. static void sdhci_prepare_data(struct rt_sdhci_host *host, struct rt_mmcsd_cmd *cmd)
  552. {
  553. struct rt_mmcsd_data *data = cmd->data;
  554. RT_ASSERT(!(data->blksize * data->blks > 524288));
  555. RT_ASSERT(!(data->blksize > host->mmc->max_blk_size));
  556. RT_ASSERT(!(data->blks > 65535));
  557. host->data = data;
  558. host->data_early = 0;
  559. host->data->bytes_xfered = 0;
  560. if (host->flags & RT_SDHCI_USE_SDMA)
  561. {
  562. unsigned int length_mask, offset_mask;
  563. host->flags |= RT_SDHCI_REQ_USE_DMA;
  564. length_mask = 0;
  565. offset_mask = 0;
  566. if (host->quirks & RT_SDHCI_QUIRK_32BIT_DMA_SIZE)
  567. {
  568. length_mask = 3;
  569. }
  570. if (host->quirks & RT_SDHCI_QUIRK_32BIT_DMA_ADDR)
  571. {
  572. offset_mask = 3;
  573. }
  574. if ((data->blks * data->blksize) & length_mask)
  575. {
  576. host->flags &= ~RT_SDHCI_REQ_USE_DMA;
  577. }
  578. else if ((rt_ubase_t)rt_kmem_v2p(data->buf) & offset_mask)
  579. {
  580. host->flags &= ~RT_SDHCI_REQ_USE_DMA;
  581. }
  582. }
  583. sdhci_config_dma(host);
  584. if (host->flags & RT_SDHCI_REQ_USE_DMA)
  585. {
  586. if (mmc_get_dma_dir(data) == DMA_FROM_DEVICE)
  587. {
  588. rt_hw_cpu_dcache_ops(RT_HW_CACHE_INVALIDATE, data->buf, data->blks * data->blksize);
  589. }
  590. else
  591. {
  592. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, data->buf, data->blks * data->blksize);
  593. }
  594. sdhci_set_sdma_addr(host, sdhci_sdma_address(host));
  595. }
  596. if (!(host->flags & RT_SDHCI_REQ_USE_DMA))
  597. {
  598. host->blocks = data->blks;
  599. }
  600. sdhci_set_transfer_irqs(host);
  601. sdhci_set_block_info(host, data);
  602. }
  603. static void sdhci_set_mrq_done(struct rt_sdhci_host *host, struct rt_mmcsd_req *mrq)
  604. {
  605. int i;
  606. for (i = 0; i < RT_SDHCI_MAX_MRQS; ++i)
  607. {
  608. if (host->mrqs_done[i] == mrq)
  609. {
  610. RT_ASSERT(0);
  611. return;
  612. }
  613. }
  614. for (i = 0; i < RT_SDHCI_MAX_MRQS; ++i)
  615. {
  616. if (!host->mrqs_done[i])
  617. {
  618. host->mrqs_done[i] = mrq;
  619. break;
  620. }
  621. }
  622. RT_ASSERT(!(i >= RT_SDHCI_MAX_MRQS));
  623. }
  624. rt_inline rt_bool_t sdhci_defer_done(struct rt_sdhci_host *host,
  625. struct rt_mmcsd_req *mrq)
  626. {
  627. struct rt_mmcsd_data *data = mrq->data;
  628. return host->pending_reset || host->always_defer_done ||
  629. ((host->flags & RT_SDHCI_REQ_USE_DMA) && data &&
  630. data->host_cookie == RT_SDHCI_COOKIE_MAPPED);
  631. }
  632. /********************************************************* */
  633. /* pio */
  634. /********************************************************* */
  635. static void rt_sdhci_read_block_pio(struct rt_sdhci_host *host, void **buf)
  636. {
  637. rt_size_t len;
  638. rt_uint32_t scratch;
  639. rt_uint32_t blksize = host->data->blksize;
  640. while (blksize)
  641. {
  642. len = rt_min_t(rt_size_t, 4U, blksize);
  643. scratch = rt_sdhci_readl(host, RT_SDHCI_BUFFER);
  644. rt_memcpy(*buf, &scratch, len);
  645. *buf += len;
  646. blksize -= len;
  647. }
  648. }
  649. static void rt_sdhci_write_block_pio(struct rt_sdhci_host *host,void **buf)
  650. {
  651. rt_uint32_t scratch;
  652. rt_size_t blksize, len;
  653. LOG_D("PIO writing");
  654. blksize = host->data->blksize;
  655. scratch = 0;
  656. while (blksize)
  657. {
  658. len = rt_min_t(rt_size_t, 4U, blksize);
  659. rt_memcpy(&scratch, *buf, len);
  660. *buf += len;
  661. blksize -= len;
  662. rt_sdhci_writel(host, scratch, RT_SDHCI_BUFFER);
  663. }
  664. }
  665. static void sdhci_transfer_pio(struct rt_sdhci_host *host)
  666. {
  667. void *buf;
  668. rt_uint32_t mask;
  669. if (host->blocks == 0)
  670. {
  671. return;
  672. }
  673. if (host->data->flags & DATA_DIR_READ)
  674. {
  675. mask = RT_SDHCI_DATA_AVAILABLE;
  676. }
  677. else
  678. {
  679. mask = RT_SDHCI_SPACE_AVAILABLE;
  680. }
  681. if ((host->quirks & RT_SDHCI_QUIRK_BROKEN_SMALL_PIO) && host->data->blks == 1)
  682. {
  683. mask = ~0;
  684. }
  685. buf = (void *)host->data->buf;
  686. while (rt_sdhci_readl(host, RT_SDHCI_PRESENT_STATE) & mask)
  687. {
  688. if (host->quirks & RT_SDHCI_QUIRK_PIO_NEEDS_DELAY)
  689. {
  690. rt_hw_us_delay(100);
  691. }
  692. if (host->data->flags & DATA_DIR_READ)
  693. {
  694. rt_sdhci_read_block_pio(host,&buf);
  695. }
  696. else
  697. {
  698. rt_sdhci_write_block_pio(host,&buf);
  699. }
  700. --host->data->blks;
  701. if (host->data->blks == 0)
  702. {
  703. break;
  704. }
  705. }
  706. }
  707. /********************************************************* */
  708. /* config */
  709. /********************************************************* */
  710. static rt_bool_t sdhci_timing_has_preset(unsigned char timing)
  711. {
  712. switch (timing)
  713. {
  714. case MMC_TIMING_UHS_SDR12:
  715. case MMC_TIMING_UHS_SDR25:
  716. case MMC_TIMING_UHS_SDR50:
  717. case MMC_TIMING_UHS_SDR104:
  718. case MMC_TIMING_UHS_DDR50:
  719. case MMC_TIMING_MMC_DDR52:
  720. return RT_TRUE;
  721. }
  722. return RT_FALSE;
  723. }
  724. static rt_bool_t sdhci_preset_needed(struct rt_sdhci_host *host, unsigned char timing)
  725. {
  726. return !(host->quirks2 & RT_SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
  727. sdhci_timing_has_preset(timing);
  728. }
  729. static rt_bool_t sdhci_presetable_values_change(struct rt_sdhci_host *host, struct rt_mmcsd_io_cfg *ios)
  730. {
  731. return !host->preset_enabled &&
  732. (sdhci_preset_needed(host, ios->timing) || host->drv_type != ios->drv_type);
  733. }
  734. static void sdhci_preset_value_enable(struct rt_sdhci_host *host, rt_bool_t enable)
  735. {
  736. if (host->version < RT_SDHCI_SPEC_300)
  737. {
  738. return;
  739. }
  740. if (host->preset_enabled != enable)
  741. {
  742. rt_uint16_t ctrl = rt_sdhci_readw(host, RT_SDHCI_HOST_CONTROL2);
  743. if (enable)
  744. {
  745. ctrl |= RT_SDHCI_CTRL_PRESET_VAL_ENABLE;
  746. }
  747. else
  748. {
  749. ctrl &= ~RT_SDHCI_CTRL_PRESET_VAL_ENABLE;
  750. }
  751. rt_sdhci_writew(host, ctrl, RT_SDHCI_HOST_CONTROL2);
  752. if (enable)
  753. {
  754. host->flags |= RT_SDHCI_PV_ENABLED;
  755. }
  756. else
  757. {
  758. host->flags &= ~RT_SDHCI_PV_ENABLED;
  759. }
  760. host->preset_enabled = enable;
  761. }
  762. }
  763. static void sdhci_set_power_reg(struct rt_sdhci_host *host, unsigned char mode,
  764. unsigned short vdd)
  765. {
  766. struct rt_mmc_host *mmc = host->mmc;
  767. sdio_regulator_set_ocr(&mmc->rthost, mmc->rthost.supply.vmmc, vdd);
  768. if (mode != MMC_POWER_OFF)
  769. {
  770. rt_sdhci_writeb(host, RT_SDHCI_POWER_ON, RT_SDHCI_POWER_CONTROL);
  771. }
  772. else
  773. {
  774. rt_sdhci_writeb(host, 0, RT_SDHCI_POWER_CONTROL);
  775. }
  776. }
  777. void rt_sdhci_set_power_and_bus_voltage(struct rt_sdhci_host *host, unsigned char mode,
  778. unsigned short vdd)
  779. {
  780. if (!rt_is_err_or_null(host->mmc->rthost.supply.vmmc))
  781. {
  782. struct rt_mmc_host *mmc = host->mmc;
  783. sdio_regulator_set_ocr(&mmc->rthost, mmc->rthost.supply.vmmc, vdd);
  784. }
  785. rt_sdhci_set_power_with_noreg(host, mode, vdd);
  786. }
  787. void rt_sdhci_set_power_with_noreg(struct rt_sdhci_host *host, unsigned char mode,
  788. unsigned short vdd)
  789. {
  790. rt_uint8_t pwr = 0;
  791. if (mode != MMC_POWER_OFF)
  792. {
  793. switch (1 << vdd)
  794. {
  795. case MMC_VDD_165_195:
  796. case MMC_VDD_20_21:
  797. pwr = RT_SDHCI_POWER_180;
  798. break;
  799. case MMC_VDD_29_30:
  800. case MMC_VDD_30_31:
  801. pwr = RT_SDHCI_POWER_300;
  802. break;
  803. case MMC_VDD_32_33:
  804. case MMC_VDD_33_34:
  805. case MMC_VDD_34_35:
  806. case MMC_VDD_35_36:
  807. pwr = RT_SDHCI_POWER_330;
  808. break;
  809. default:
  810. break;
  811. }
  812. }
  813. if (host->pwr == pwr)
  814. {
  815. return;
  816. }
  817. host->pwr = pwr;
  818. if (pwr == 0)
  819. {
  820. rt_sdhci_writeb(host, 0, RT_SDHCI_POWER_CONTROL);
  821. if (host->quirks2 & RT_SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
  822. {
  823. sdhci_runtime_pm_bus_off(host);
  824. }
  825. }
  826. else
  827. {
  828. if (!(host->quirks & RT_SDHCI_QUIRK_SINGLE_POWER_WRITE))
  829. {
  830. rt_sdhci_writeb(host, 0, RT_SDHCI_POWER_CONTROL);
  831. }
  832. if (host->quirks & RT_SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
  833. {
  834. rt_sdhci_writeb(host, pwr, RT_SDHCI_POWER_CONTROL);
  835. }
  836. pwr |= RT_SDHCI_POWER_ON;
  837. rt_sdhci_writeb(host, pwr, RT_SDHCI_POWER_CONTROL);
  838. if (host->quirks2 & RT_SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
  839. {
  840. sdhci_runtime_pm_bus_on(host);
  841. }
  842. if (host->quirks & RT_SDHCI_QUIRK_DELAY_AFTER_POWER)
  843. {
  844. rt_thread_mdelay(10);
  845. }
  846. }
  847. }
  848. void rt_sdhci_set_power(struct rt_sdhci_host *host, unsigned char mode,
  849. unsigned short vdd)
  850. {
  851. if (!host->mmc->rthost.supply.vmmc)
  852. {
  853. rt_sdhci_set_power_with_noreg(host, mode, vdd);
  854. }
  855. else
  856. {
  857. sdhci_set_power_reg(host, mode, vdd);
  858. }
  859. }
  860. int rt_sdhci_start_signal_voltage_switch(struct rt_mmc_host *mmc,
  861. struct rt_mmcsd_io_cfg *ios)
  862. {
  863. int ret;
  864. rt_uint16_t ctrl;
  865. struct rt_sdhci_host *host = rt_mmc_priv(mmc);
  866. if (host->version < RT_SDHCI_SPEC_300)
  867. {
  868. return 0;
  869. }
  870. ctrl = rt_sdhci_readw(host, RT_SDHCI_HOST_CONTROL2);
  871. switch (ios->signal_voltage)
  872. {
  873. case MMC_SIGNAL_VOLTAGE_330:
  874. if (!(host->flags & RT_SDHCI_SIGNALING_330))
  875. {
  876. return -EINVAL;
  877. }
  878. ctrl &= ~RT_SDHCI_CTRL_VDD_180;
  879. rt_sdhci_writew(host, ctrl, RT_SDHCI_HOST_CONTROL2);
  880. if (mmc->rthost.supply.vqmmc)
  881. {
  882. ret = sdio_regulator_set_vqmmc(&mmc->rthost, ios);
  883. if (ret < 0)
  884. {
  885. return -EIO;
  886. }
  887. }
  888. rt_thread_mdelay(5);
  889. ctrl = rt_sdhci_readw(host, RT_SDHCI_HOST_CONTROL2);
  890. if (!(ctrl & RT_SDHCI_CTRL_VDD_180))
  891. {
  892. return 0;
  893. }
  894. return -EAGAIN;
  895. case MMC_SIGNAL_VOLTAGE_180:
  896. if (!(host->flags & RT_SDHCI_SIGNALING_180))
  897. {
  898. return -EINVAL;
  899. }
  900. if (mmc->rthost.supply.vqmmc)
  901. {
  902. ret = sdio_regulator_set_vqmmc(&mmc->rthost, ios);
  903. if (ret < 0)
  904. {
  905. LOG_D("%s: Switching to 1.8V signalling voltage failed",
  906. mmc_hostname(mmc));
  907. return -EIO;
  908. }
  909. }
  910. ctrl |= RT_SDHCI_CTRL_VDD_180;
  911. rt_sdhci_writew(host, ctrl, RT_SDHCI_HOST_CONTROL2);
  912. if (host->ops->voltage_switch)
  913. {
  914. host->ops->voltage_switch(host);
  915. }
  916. ctrl = rt_sdhci_readw(host, RT_SDHCI_HOST_CONTROL2);
  917. if (ctrl & RT_SDHCI_CTRL_VDD_180)
  918. {
  919. return 0;
  920. }
  921. LOG_D("%s: 1.8V regulator output did not become stable",
  922. mmc_hostname(mmc));
  923. return -EAGAIN;
  924. case MMC_SIGNAL_VOLTAGE_120:
  925. if (!(host->flags & RT_SDHCI_SIGNALING_120))
  926. {
  927. return -EINVAL;
  928. }
  929. if (mmc->rthost.supply.vqmmc)
  930. {
  931. ret = sdio_regulator_set_vqmmc(&mmc->rthost, ios);
  932. if (ret < 0)
  933. {
  934. LOG_D("%s: Switching to 1.2V signalling voltage failed",
  935. mmc_hostname(mmc));
  936. return -EIO;
  937. }
  938. }
  939. return 0;
  940. default:
  941. return 0;
  942. }
  943. }
  944. static int sdhci_get_cd(struct rt_mmc_host *mmc)
  945. {
  946. int gpio_cd = rt_mmc_gpio_get_cd(mmc);
  947. struct rt_sdhci_host *host = rt_mmc_priv(mmc);
  948. if (host->flags & RT_SDHCI_DEVICE_DEAD)
  949. {
  950. return 0;
  951. }
  952. if (!mmc_card_is_removable(mmc))
  953. {
  954. return 1;
  955. }
  956. if (gpio_cd >= 0)
  957. {
  958. return !!gpio_cd;
  959. }
  960. if (host->quirks & RT_SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  961. {
  962. return 1;
  963. }
  964. return !!(rt_sdhci_readl(host, RT_SDHCI_PRESENT_STATE) & RT_SDHCI_CARD_PRESENT);
  965. }
  966. static int sdhci_check_ro(struct rt_sdhci_host *host)
  967. {
  968. int is_readonly;
  969. rt_base_t flags;
  970. flags = rt_spin_lock_irqsave(&host->lock);
  971. if (host->flags & RT_SDHCI_DEVICE_DEAD)
  972. {
  973. is_readonly = 0;
  974. }
  975. else if (host->ops->get_ro)
  976. {
  977. is_readonly = host->ops->get_ro(host);
  978. }
  979. else if (rt_mmc_can_gpio_ro(host->mmc))
  980. {
  981. is_readonly = rt_mmc_gpio_get_ro(host->mmc);
  982. }
  983. else
  984. {
  985. is_readonly = !(rt_sdhci_readl(host, RT_SDHCI_PRESENT_STATE) & RT_SDHCI_WRITE_PROTECT);
  986. }
  987. rt_spin_unlock_irqrestore(&host->lock, flags);
  988. return host->quirks & RT_SDHCI_QUIRK_INVERTED_WRITE_PROTECT ? !is_readonly : is_readonly;
  989. }
  990. #define SAMPLE_COUNT 5
  991. static int rt_sdhci_ro_get(struct rt_mmc_host *mmc)
  992. {
  993. int ro_count;
  994. struct rt_sdhci_host *host = rt_mmc_priv(mmc);
  995. if (!(host->quirks & RT_SDHCI_QUIRK_UNSTABLE_RO_DETECT))
  996. {
  997. return sdhci_check_ro(host);
  998. }
  999. ro_count = 0;
  1000. for (int i = 0; i < SAMPLE_COUNT; i++)
  1001. {
  1002. if (sdhci_check_ro(host))
  1003. {
  1004. if (++ro_count > SAMPLE_COUNT / 2)
  1005. {
  1006. return 1;
  1007. }
  1008. }
  1009. rt_thread_mdelay(30);
  1010. }
  1011. return 0;
  1012. }
  1013. static void rt_sdhci_enable_io_irq_nolock(struct rt_sdhci_host *host, int enable)
  1014. {
  1015. if (!(host->flags & RT_SDHCI_DEVICE_DEAD))
  1016. {
  1017. if (enable)
  1018. {
  1019. host->ier |= RT_SDHCI_INT_CARD_INT;
  1020. }
  1021. else
  1022. {
  1023. host->ier &= ~RT_SDHCI_INT_CARD_INT;
  1024. }
  1025. rt_sdhci_writel(host, host->ier, RT_SDHCI_INT_ENABLE);
  1026. rt_sdhci_writel(host, host->ier, RT_SDHCI_SIGNAL_ENABLE);
  1027. }
  1028. }
  1029. static void sdhci_ack_sdio_irq(struct rt_mmc_host *mmc)
  1030. {
  1031. rt_base_t flags;
  1032. struct rt_sdhci_host *host = rt_mmc_priv(mmc);
  1033. flags = rt_spin_lock_irqsave(&host->lock);
  1034. rt_sdhci_enable_io_irq_nolock(host, RT_TRUE);
  1035. rt_spin_unlock_irqrestore(&host->lock, flags);
  1036. }
  1037. static void sdhci_del_timer(struct rt_sdhci_host *host, struct rt_mmcsd_req *mrq)
  1038. {
  1039. if (sdhci_data_line_cmd(mrq->cmd))
  1040. {
  1041. rt_timer_stop(&host->data_timer);
  1042. }
  1043. else
  1044. {
  1045. rt_timer_stop(&host->timer);
  1046. }
  1047. }
  1048. static unsigned int sdhci_target_timeout(struct rt_sdhci_host *host,
  1049. struct rt_mmcsd_cmd *cmd,
  1050. struct rt_mmcsd_data *data)
  1051. {
  1052. unsigned int target_timeout;
  1053. if (!data)
  1054. {
  1055. target_timeout = cmd->busy_timeout * 1000;
  1056. }
  1057. else
  1058. {
  1059. target_timeout = RT_DIV_ROUND_UP(data->timeout_ns, 1000);
  1060. if (host->clock && data->timeout_clks)
  1061. {
  1062. rt_uint32_t val = 1000000ULL * data->timeout_clks;
  1063. if (rt_do_div(val, host->clock))
  1064. {
  1065. target_timeout++;
  1066. }
  1067. target_timeout += val;
  1068. }
  1069. }
  1070. return target_timeout;
  1071. }
  1072. static rt_uint8_t sdhci_calc_timeout(struct rt_sdhci_host *host,
  1073. struct rt_mmcsd_cmd *cmd,
  1074. rt_bool_t *too_big)
  1075. {
  1076. rt_uint8_t count;
  1077. struct rt_mmcsd_data *data;
  1078. unsigned target_timeout, current_timeout;
  1079. *too_big = RT_FALSE;
  1080. if (host->quirks & RT_SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
  1081. {
  1082. return host->max_timeout_count;
  1083. }
  1084. if (!cmd)
  1085. {
  1086. return host->max_timeout_count;
  1087. }
  1088. data = cmd->data;
  1089. if (!data && !cmd->busy_timeout)
  1090. {
  1091. return host->max_timeout_count;
  1092. }
  1093. target_timeout = sdhci_target_timeout(host, cmd, data);
  1094. count = 0;
  1095. current_timeout = (1 << 13) * 1000 / host->timeout_clk;
  1096. while (current_timeout < target_timeout)
  1097. {
  1098. ++count;
  1099. current_timeout <<= 1;
  1100. if (count > host->max_timeout_count)
  1101. {
  1102. if (!(host->quirks2 & RT_SDHCI_QUIRK2_DISABLE_HW_TIMEOUT))
  1103. {
  1104. LOG_D("Too large timeout 0x%x requested for CMD%d!",
  1105. count, cmd->cmd_code);
  1106. }
  1107. count = host->max_timeout_count;
  1108. *too_big = RT_TRUE;
  1109. break;
  1110. }
  1111. }
  1112. return count;
  1113. }
  1114. static void sdhci_calc_sw_timeout(struct rt_sdhci_host *host,
  1115. struct rt_mmcsd_cmd *cmd)
  1116. {
  1117. unsigned char bus_width;
  1118. unsigned int blksz, freq;
  1119. rt_uint64_t target_timeout, transfer_time;
  1120. struct rt_mmc_host *mmc = host->mmc;
  1121. struct rt_mmcsd_data *data = cmd->data;
  1122. struct rt_mmcsd_io_cfg *ios = &mmc->ios;
  1123. bus_width = 1 << ios->bus_width;
  1124. target_timeout = sdhci_target_timeout(host, cmd, data);
  1125. target_timeout *= 1000L;
  1126. if (data)
  1127. {
  1128. blksz = data->blksize;
  1129. freq = mmc->actual_clock ?: host->clock;
  1130. transfer_time = (rt_uint64_t)blksz * 1000000000L * (8 / bus_width);
  1131. rt_do_div(transfer_time, freq);
  1132. transfer_time = transfer_time * 2;
  1133. host->data_timeout = data->blks * target_timeout + transfer_time;
  1134. }
  1135. else
  1136. {
  1137. host->data_timeout = target_timeout;
  1138. }
  1139. if (host->data_timeout)
  1140. {
  1141. host->data_timeout += MMC_CMD_TRANSFER_TIME;
  1142. }
  1143. }
  1144. void rt_sdhci_timeout_set(struct rt_sdhci_host *host, struct rt_mmcsd_cmd *cmd)
  1145. {
  1146. rt_bool_t too_big = RT_FALSE;
  1147. rt_uint8_t count = sdhci_calc_timeout(host, cmd, &too_big);
  1148. if (too_big && host->quirks2 & RT_SDHCI_QUIRK2_DISABLE_HW_TIMEOUT)
  1149. {
  1150. sdhci_calc_sw_timeout(host, cmd);
  1151. rt_sdhci_data_irq_timeout(host, RT_FALSE);
  1152. }
  1153. else if (!(host->ier & RT_SDHCI_INT_DATA_TIMEOUT))
  1154. {
  1155. rt_sdhci_data_irq_timeout(host, RT_FALSE);
  1156. }
  1157. rt_sdhci_writeb(host, count, RT_SDHCI_TIMEOUT_CONTROL);
  1158. }
  1159. static void sdhci_set_timeout(struct rt_sdhci_host *host, struct rt_mmcsd_cmd *cmd)
  1160. {
  1161. if (host->ops->set_timeout)
  1162. {
  1163. host->ops->set_timeout(host, cmd);
  1164. }
  1165. else
  1166. {
  1167. rt_sdhci_timeout_set(host, cmd);
  1168. }
  1169. }
  1170. static void sdhci_start_timer(struct rt_sdhci_host *host, struct rt_mmcsd_req *mrq,
  1171. unsigned long timeout)
  1172. {
  1173. if (sdhci_data_line_cmd(mrq->cmd))
  1174. {
  1175. rt_tick_t tick = rt_tick_get();
  1176. if (timeout < tick)
  1177. {
  1178. timeout = tick;
  1179. }
  1180. tick = timeout - tick;
  1181. rt_timer_stop(&host->data_timer);
  1182. rt_timer_control(&host->data_timer, RT_TIMER_CTRL_SET_TIME, &tick);
  1183. rt_timer_start(&host->data_timer);
  1184. }
  1185. else
  1186. {
  1187. rt_tick_t tick = rt_tick_get();
  1188. if (timeout < tick)
  1189. {
  1190. timeout = tick;
  1191. }
  1192. tick = timeout - tick;
  1193. rt_timer_stop(&host->timer);
  1194. rt_timer_control(&host->timer, RT_TIMER_CTRL_SET_TIME, &tick);
  1195. rt_timer_start(&host->timer);
  1196. }
  1197. }
  1198. static void __sdhci_finish_mrq(struct rt_sdhci_host *host, struct rt_mmcsd_req *mrq)
  1199. {
  1200. if (host->cmd && host->cmd->mrq == mrq)
  1201. {
  1202. host->cmd = RT_NULL;
  1203. }
  1204. if (host->data_cmd && host->data_cmd->mrq == mrq)
  1205. {
  1206. host->data_cmd = RT_NULL;
  1207. }
  1208. if (host->deferred_cmd && host->deferred_cmd->mrq == mrq)
  1209. {
  1210. host->deferred_cmd = RT_NULL;
  1211. }
  1212. if (host->data && host->data->mrq == mrq)
  1213. {
  1214. host->data = RT_NULL;
  1215. }
  1216. if (sdhci_needs_reset(host, mrq))
  1217. {
  1218. host->pending_reset = RT_TRUE;
  1219. }
  1220. sdhci_set_mrq_done(host, mrq);
  1221. sdhci_del_timer(host, mrq);
  1222. }
  1223. static void sdhci_finish_mrq(struct rt_sdhci_host *host, struct rt_mmcsd_req *mrq)
  1224. {
  1225. __sdhci_finish_mrq(host, mrq);
  1226. rt_workqueue_submit_work(host->complete_wq, &host->complete_work, 0);
  1227. }
  1228. static void sdhci_error_out_mrqs(struct rt_sdhci_host *host, int err)
  1229. {
  1230. if (host->data_cmd)
  1231. {
  1232. host->data_cmd->err = err;
  1233. sdhci_finish_mrq(host, host->data_cmd->mrq);
  1234. }
  1235. if (host->cmd)
  1236. {
  1237. host->cmd->err = err;
  1238. sdhci_finish_mrq(host, host->cmd->mrq);
  1239. }
  1240. }
  1241. static void sdhci_card_event(struct rt_mmc_host *mmc)
  1242. {
  1243. int present;
  1244. rt_uint32_t flags;
  1245. struct rt_sdhci_host *host = rt_mmc_priv(mmc);
  1246. if (host->ops->card_event)
  1247. {
  1248. host->ops->card_event(host);
  1249. }
  1250. present = mmc->ops->get_cd(mmc);
  1251. flags = rt_spin_lock_irqsave(&host->lock);
  1252. if (sdhci_has_requests(host) && !present)
  1253. {
  1254. rt_kprintf("%s: Card removed during transfer!\n", mmc_hostname(mmc));
  1255. rt_kprintf("%s: Resetting controller.\n", mmc_hostname(mmc));
  1256. sdhci_do_reset(host, RT_SDHCI_RESET_CMD);
  1257. sdhci_do_reset(host, RT_SDHCI_RESET_DATA);
  1258. sdhci_error_out_mrqs(host, -ENOMEDIUM);
  1259. }
  1260. rt_spin_unlock_irqrestore(&host->lock, flags);
  1261. }
  1262. static int sdhci_card_busy(struct rt_mmc_host *mmc)
  1263. {
  1264. rt_uint32_t present_state;
  1265. struct rt_sdhci_host *host = rt_mmc_priv(mmc);
  1266. present_state = rt_sdhci_readl(host, RT_SDHCI_PRESENT_STATE);
  1267. return !(present_state & RT_SDHCI_DATA_0_LVL_MASK);
  1268. }
  1269. static int sdhci_prepare_hs400_tuning(struct rt_mmc_host *mmc, struct rt_mmcsd_io_cfg *ios)
  1270. {
  1271. rt_uint32_t flags;
  1272. struct rt_sdhci_host *host = rt_mmc_priv(mmc);
  1273. flags = rt_spin_lock_irqsave(&host->lock);
  1274. host->flags |= RT_SDHCI_HS400_TUNING;
  1275. rt_spin_unlock_irqrestore(&host->lock, flags);
  1276. return 0;
  1277. }
  1278. static void sdhci_set_transfer_mode(struct rt_sdhci_host *host,
  1279. struct rt_mmcsd_cmd *cmd)
  1280. {
  1281. rt_uint16_t mode = 0;
  1282. struct rt_mmcsd_data *data = cmd->data;
  1283. if (!data)
  1284. {
  1285. if (host->quirks2 & RT_SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD)
  1286. {
  1287. if (!mmc_op_tuning(cmd->cmd_code))
  1288. {
  1289. rt_sdhci_writew(host, 0x0, RT_SDHCI_TRANSFER_MODE);
  1290. }
  1291. }
  1292. else
  1293. {
  1294. mode = rt_sdhci_readw(host, RT_SDHCI_TRANSFER_MODE);
  1295. rt_sdhci_writew(host, mode & ~(RT_SDHCI_TRNS_AUTO_CMD12 | RT_SDHCI_TRNS_AUTO_CMD23),
  1296. RT_SDHCI_TRANSFER_MODE);
  1297. }
  1298. return;
  1299. }
  1300. if (!(host->quirks2 & RT_SDHCI_QUIRK2_SUPPORT_SINGLE))
  1301. {
  1302. mode = RT_SDHCI_TRNS_BLK_CNT_EN;
  1303. }
  1304. if (mmc_op_multi(cmd->cmd_code) || data->blks > 1)
  1305. {
  1306. mode = RT_SDHCI_TRNS_BLK_CNT_EN | RT_SDHCI_TRNS_MULTI;
  1307. sdhci_auto_cmd_select(host, cmd, &mode);
  1308. if (sdhci_auto_cmd23(host, cmd->mrq))
  1309. {
  1310. rt_sdhci_writel(host, cmd->mrq->sbc->arg, RT_SDHCI_ARGUMENT2);
  1311. }
  1312. }
  1313. if (data->flags & DATA_DIR_READ)
  1314. {
  1315. mode |= RT_SDHCI_TRNS_READ;
  1316. }
  1317. if (host->flags & RT_SDHCI_REQ_USE_DMA)
  1318. {
  1319. mode |= RT_SDHCI_TRNS_DMA;
  1320. }
  1321. rt_sdhci_writew(host, mode, RT_SDHCI_TRANSFER_MODE);
  1322. }
  1323. static rt_bool_t sdhci_send_command(struct rt_sdhci_host *host, struct rt_mmcsd_cmd *cmd)
  1324. {
  1325. int flags;
  1326. rt_uint32_t mask;
  1327. unsigned long timeout;
  1328. cmd->err = 0;
  1329. if ((host->quirks2 & RT_SDHCI_QUIRK2_STOP_WITH_TC) &&
  1330. cmd->cmd_code == MMC_STOP_TRANSMISSION)
  1331. {
  1332. cmd->flags |= MMC_RSP_BUSY;
  1333. }
  1334. mask = RT_SDHCI_CMD_INHIBIT;
  1335. if (sdhci_data_line_cmd(cmd))
  1336. {
  1337. mask |= RT_SDHCI_DATA_INHIBIT;
  1338. }
  1339. if (cmd->mrq->data && (cmd == cmd->mrq->data->stop))
  1340. {
  1341. mask &= ~RT_SDHCI_DATA_INHIBIT;
  1342. }
  1343. if (rt_sdhci_readl(host, RT_SDHCI_PRESENT_STATE) & mask)
  1344. {
  1345. return RT_FALSE;
  1346. }
  1347. host->cmd = cmd;
  1348. host->data_timeout = 0;
  1349. if (sdhci_data_line_cmd(cmd))
  1350. {
  1351. host->data_cmd = cmd;
  1352. sdhci_set_timeout(host, cmd);
  1353. }
  1354. if (cmd->data)
  1355. {
  1356. sdhci_prepare_data(host, cmd);
  1357. }
  1358. rt_sdhci_writel(host, cmd->arg, RT_SDHCI_ARGUMENT);
  1359. sdhci_set_transfer_mode(host, cmd);
  1360. if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY))
  1361. {
  1362. cmd->flags &= ~MMC_RSP_BUSY;
  1363. }
  1364. if (!(cmd->flags & MMC_RSP_PRESENT))
  1365. {
  1366. flags = RT_SDHCI_CMD_RESP_NONE;
  1367. }
  1368. else if (cmd->flags & MMC_RSP_136)
  1369. {
  1370. flags = RT_SDHCI_CMD_RESP_LONG;
  1371. }
  1372. else if (cmd->flags & MMC_RSP_BUSY)
  1373. {
  1374. flags = RT_SDHCI_CMD_RESP_SHORT_BUSY;
  1375. }
  1376. else
  1377. {
  1378. flags = RT_SDHCI_CMD_RESP_SHORT;
  1379. }
  1380. if (cmd->flags & MMC_RSP_CRC)
  1381. {
  1382. flags |= RT_SDHCI_CMD_CRC;
  1383. }
  1384. if (cmd->flags & MMC_RSP_OPCODE)
  1385. {
  1386. flags |= RT_SDHCI_CMD_INDEX;
  1387. }
  1388. if (cmd->data || mmc_op_tuning(cmd->cmd_code))
  1389. {
  1390. flags |= RT_SDHCI_CMD_DATA;
  1391. }
  1392. timeout = rt_tick_get();
  1393. if (host->data_timeout)
  1394. {
  1395. timeout += rt_tick_from_millisecond(host->data_timeout * 1000);
  1396. }
  1397. else if (!cmd->data && (int)cmd->busy_timeout > 9000)
  1398. {
  1399. timeout += (RT_DIV_ROUND_UP(cmd->busy_timeout, 1000) + 1) * RT_TICK_PER_SECOND;
  1400. }
  1401. else
  1402. {
  1403. timeout += 10 * RT_TICK_PER_SECOND;
  1404. }
  1405. sdhci_start_timer(host, cmd->mrq, timeout);
  1406. rt_sdhci_writew(host, RT_SDHCI_MAKE_CMD(cmd->cmd_code, flags), RT_SDHCI_COMMAND);
  1407. return RT_TRUE;
  1408. }
  1409. /********************************************************* */
  1410. /* dma */
  1411. /********************************************************* */
  1412. static void __sdhci_finish_data(struct rt_sdhci_host *host, rt_bool_t sw_data_timeout)
  1413. {
  1414. struct rt_mmcsd_data *data = host->data;
  1415. struct rt_mmcsd_cmd *data_cmd = host->data_cmd;
  1416. host->data = RT_NULL;
  1417. host->data_cmd = RT_NULL;
  1418. if (data->err)
  1419. {
  1420. if (!host->cmd || host->cmd == data_cmd)
  1421. {
  1422. sdhci_reset_for(host, REQUEST_ERROR);
  1423. }
  1424. else
  1425. {
  1426. sdhci_reset_for(host, REQUEST_ERROR_DATA_ONLY);
  1427. }
  1428. }
  1429. if (data->err)
  1430. {
  1431. data->bytes_xfered = 0;
  1432. }
  1433. else
  1434. {
  1435. data->bytes_xfered = data->blksize * data->blks;
  1436. }
  1437. if (data->stop && ((!data->mrq->sbc && !sdhci_auto_cmd12(host, data->mrq)) || data->err))
  1438. {
  1439. if (data->mrq->cap_cmd_during_tfr)
  1440. {
  1441. __sdhci_finish_mrq(host, data->mrq);
  1442. }
  1443. else
  1444. {
  1445. host->cmd = RT_NULL;
  1446. if (!sdhci_send_command(host, data->stop))
  1447. {
  1448. if (sw_data_timeout)
  1449. {
  1450. data->stop->err = -EIO;
  1451. __sdhci_finish_mrq(host, data->mrq);
  1452. }
  1453. else
  1454. {
  1455. host->deferred_cmd = data->stop;
  1456. }
  1457. }
  1458. }
  1459. }
  1460. else
  1461. {
  1462. __sdhci_finish_mrq(host, data->mrq);
  1463. }
  1464. }
  1465. static void sdhci_finish_data(struct rt_sdhci_host *host)
  1466. {
  1467. __sdhci_finish_data(host, RT_FALSE);
  1468. }
  1469. /********************************************************* */
  1470. /* irq */
  1471. /********************************************************* */
  1472. static void sdhci_data_irq(struct rt_sdhci_host *host, rt_uint32_t intmask)
  1473. {
  1474. rt_uint32_t command;
  1475. if (intmask & RT_SDHCI_INT_DATA_AVAIL && !host->data)
  1476. {
  1477. command = RT_SDHCI_GET_CMD(rt_sdhci_readw(host, RT_SDHCI_COMMAND));
  1478. if (command == MMC_SEND_TUNING_BLOCK || command == MMC_SEND_TUNING_BLOCK_HS200)
  1479. {
  1480. host->tuning_done = 1;
  1481. rt_wqueue_wakeup(&host->buf_ready_int, 0);
  1482. return;
  1483. }
  1484. }
  1485. if (!host->data)
  1486. {
  1487. struct rt_mmcsd_cmd *data_cmd = host->data_cmd;
  1488. if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY))
  1489. {
  1490. if (intmask & RT_SDHCI_INT_DATA_TIMEOUT)
  1491. {
  1492. host->data_cmd = RT_NULL;
  1493. data_cmd->err = -ETIMEDOUT;
  1494. __sdhci_finish_mrq(host, data_cmd->mrq);
  1495. return;
  1496. }
  1497. if (intmask & RT_SDHCI_INT_DATA_END)
  1498. {
  1499. host->data_cmd = RT_NULL;
  1500. if (host->cmd == data_cmd)
  1501. {
  1502. return;
  1503. }
  1504. __sdhci_finish_mrq(host, data_cmd->mrq);
  1505. return;
  1506. }
  1507. }
  1508. if (host->pending_reset)
  1509. {
  1510. return;
  1511. }
  1512. rt_kprintf("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
  1513. mmc_hostname(host->mmc), (unsigned)intmask);
  1514. rt_sdhci_read_reg_debug(host);
  1515. return;
  1516. }
  1517. if (intmask & RT_SDHCI_INT_DATA_TIMEOUT)
  1518. {
  1519. host->data->err = -ETIMEDOUT;
  1520. }
  1521. else if (intmask & RT_SDHCI_INT_DATA_END_BIT)
  1522. {
  1523. host->data->err = -EILSEQ;
  1524. }
  1525. else if ((intmask & RT_SDHCI_INT_DATA_CRC) &&
  1526. RT_SDHCI_GET_CMD(rt_sdhci_readw(host, RT_SDHCI_COMMAND)) != MMC_BUS_TEST_R)
  1527. {
  1528. host->data->err = -EILSEQ;
  1529. }
  1530. if (host->data->err)
  1531. {
  1532. sdhci_finish_data(host);
  1533. }
  1534. else
  1535. {
  1536. if (intmask & (RT_SDHCI_INT_DATA_AVAIL | RT_SDHCI_INT_SPACE_AVAIL))
  1537. {
  1538. sdhci_transfer_pio(host);
  1539. }
  1540. if (intmask & RT_SDHCI_INT_DMA_END)
  1541. {
  1542. rt_uint32_t dmastart, dmanow;
  1543. dmastart = sdhci_sdma_address(host);
  1544. dmanow = dmastart + host->data->bytes_xfered;
  1545. dmanow = (dmanow & ~((rt_uint32_t)RT_SDHCI_DEFAULT_BOUNDARY_SIZE - 1));
  1546. dmanow += RT_SDHCI_DEFAULT_BOUNDARY_SIZE;
  1547. host->data->bytes_xfered = dmanow - dmastart;
  1548. LOG_D("DMA base %p, transferred 0x%06x bytes, next %p",
  1549. &dmastart, host->data->bytes_xfered, &dmanow);
  1550. sdhci_set_sdma_addr(host, dmanow);
  1551. }
  1552. if (intmask & RT_SDHCI_INT_DATA_END)
  1553. {
  1554. struct rt_mmcsd_data *data = host->data;
  1555. if (data->buf)
  1556. {
  1557. if (mmc_get_dma_dir(data) == DMA_FROM_DEVICE)
  1558. {
  1559. rt_hw_cpu_dcache_ops(RT_HW_CACHE_INVALIDATE, data->buf,
  1560. data->blks * data->blksize);
  1561. }
  1562. else
  1563. {
  1564. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, data->buf,
  1565. data->blks * data->blksize);
  1566. }
  1567. }
  1568. if (host->cmd == host->data_cmd)
  1569. {
  1570. host->data_early = 1;
  1571. }
  1572. else
  1573. {
  1574. sdhci_finish_data(host);
  1575. }
  1576. }
  1577. }
  1578. }
  1579. static void rt_sdhci_read_rsp_136(struct rt_sdhci_host *host, struct rt_mmcsd_cmd *cmd)
  1580. {
  1581. int reg;
  1582. for (int i = 0; i < 4; ++i)
  1583. {
  1584. reg = RT_SDHCI_RESPONSE + (3 - i) * 4;
  1585. cmd->resp[i] = rt_sdhci_readl(host, reg);
  1586. }
  1587. if (host->quirks2 & RT_SDHCI_QUIRK2_RSP_136_HAS_CRC)
  1588. {
  1589. return;
  1590. }
  1591. for (int i = 0; i < 4; ++i)
  1592. {
  1593. cmd->resp[i] <<= 8;
  1594. if (i != 3)
  1595. {
  1596. cmd->resp[i] |= cmd->resp[i + 1] >> 24;
  1597. }
  1598. }
  1599. }
  1600. static void sdhci_command_end(struct rt_sdhci_host *host)
  1601. {
  1602. struct rt_mmcsd_cmd *cmd = host->cmd;
  1603. host->cmd = RT_NULL;
  1604. if (cmd->flags & MMC_RSP_PRESENT)
  1605. {
  1606. if (cmd->flags & MMC_RSP_136)
  1607. {
  1608. rt_sdhci_read_rsp_136(host, cmd);
  1609. }
  1610. else
  1611. {
  1612. cmd->resp[0] = rt_sdhci_readl(host, RT_SDHCI_RESPONSE);
  1613. }
  1614. }
  1615. if (cmd->flags & MMC_RSP_BUSY)
  1616. {
  1617. if (cmd->data)
  1618. {
  1619. LOG_D("Cannot wait for busy signal when also doing a data transfer");
  1620. }
  1621. else if (!(host->quirks & RT_SDHCI_QUIRK_NO_BUSY_IRQ) && cmd == host->data_cmd)
  1622. {
  1623. return;
  1624. }
  1625. }
  1626. if (cmd == cmd->mrq->sbc)
  1627. {
  1628. if (!sdhci_send_command(host, cmd->mrq->cmd))
  1629. {
  1630. host->deferred_cmd = cmd->mrq->cmd;
  1631. }
  1632. }
  1633. else
  1634. {
  1635. if (host->data && host->data_early)
  1636. {
  1637. sdhci_finish_data(host);
  1638. }
  1639. if (!cmd->data)
  1640. {
  1641. __sdhci_finish_mrq(host, cmd->mrq);
  1642. }
  1643. }
  1644. }
  1645. static void sdhci_cmd_irq(struct rt_sdhci_host *host,
  1646. rt_uint32_t intmask, rt_uint32_t *intmask_p)
  1647. {
  1648. if (intmask & RT_SDHCI_INT_AUTO_CMD_ERR && host->data_cmd)
  1649. {
  1650. int data_err_bit;
  1651. rt_uint16_t auto_cmd_status;
  1652. struct rt_mmcsd_req *mrq = host->data_cmd->mrq;
  1653. auto_cmd_status = rt_sdhci_readw(host, RT_SDHCI_AUTO_CMD_STATUS);
  1654. data_err_bit = (auto_cmd_status & RT_SDHCI_AUTO_CMD_TIMEOUT) ?
  1655. RT_SDHCI_INT_DATA_TIMEOUT : RT_SDHCI_INT_DATA_CRC;
  1656. if (!mrq->sbc && (host->flags & RT_SDHCI_AUTO_CMD12))
  1657. {
  1658. *intmask_p |= data_err_bit;
  1659. return;
  1660. }
  1661. }
  1662. if (!host->cmd)
  1663. {
  1664. if (host->pending_reset)
  1665. {
  1666. return;
  1667. }
  1668. rt_kprintf("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
  1669. mmc_hostname(host->mmc), (unsigned)intmask);
  1670. rt_sdhci_read_reg_debug(host);
  1671. return;
  1672. }
  1673. if (intmask & (RT_SDHCI_INT_TIMEOUT | RT_SDHCI_INT_CRC | RT_SDHCI_INT_END_BIT | RT_SDHCI_INT_INDEX))
  1674. {
  1675. if (intmask & RT_SDHCI_INT_TIMEOUT)
  1676. {
  1677. host->cmd->err = -ETIMEDOUT;
  1678. }
  1679. else
  1680. {
  1681. host->cmd->err = -EILSEQ;
  1682. }
  1683. /* Treat data command CRC error the same as data CRC error */
  1684. if (host->cmd->data && (intmask & (RT_SDHCI_INT_CRC | RT_SDHCI_INT_TIMEOUT)) == RT_SDHCI_INT_CRC)
  1685. {
  1686. host->cmd = NULL;
  1687. *intmask_p |= RT_SDHCI_INT_DATA_CRC;
  1688. return;
  1689. }
  1690. __sdhci_finish_mrq(host, host->cmd->mrq);
  1691. return;
  1692. }
  1693. if (intmask & RT_SDHCI_INT_AUTO_CMD_ERR)
  1694. {
  1695. int err;
  1696. rt_uint16_t auto_cmd_status;
  1697. struct rt_mmcsd_req *mrq = host->cmd->mrq;
  1698. auto_cmd_status = rt_sdhci_readw(host, RT_SDHCI_AUTO_CMD_STATUS);
  1699. err = (auto_cmd_status & RT_SDHCI_AUTO_CMD_TIMEOUT) ? -ETIMEDOUT : -EILSEQ;
  1700. if (mrq->sbc && (host->flags & RT_SDHCI_AUTO_CMD23))
  1701. {
  1702. mrq->sbc->err = err;
  1703. __sdhci_finish_mrq(host, mrq);
  1704. return;
  1705. }
  1706. }
  1707. if (intmask & RT_SDHCI_INT_RESPONSE)
  1708. {
  1709. sdhci_command_end(host);
  1710. }
  1711. }
  1712. static void sdhci_irq(int irq, void *dev_id)
  1713. {
  1714. #define IRQ_NONE 0
  1715. #define IRQ_WAIT 1
  1716. #define IRQ_DONE 2
  1717. int max_loops = 16, result = IRQ_NONE;
  1718. rt_uint32_t intmask, mask, unexpected = 0;
  1719. struct rt_sdhci_host *host = dev_id;
  1720. struct rt_mmcsd_req *mrqs_done[RT_SDHCI_MAX_MRQS] = { 0 };
  1721. rt_spin_lock(&host->lock);
  1722. if (host->runtime_suspended)
  1723. {
  1724. rt_spin_unlock(&host->lock);
  1725. return;
  1726. }
  1727. intmask = rt_sdhci_readl(host, RT_SDHCI_INT_STATUS);
  1728. if (!intmask || intmask == 0xffffffff)
  1729. {
  1730. result = IRQ_NONE;
  1731. goto _out;
  1732. }
  1733. do {
  1734. LOG_D("IRQ status 0x%08x", intmask);
  1735. if (host->ops->irq)
  1736. {
  1737. intmask = host->ops->irq(host, intmask);
  1738. if (!intmask)
  1739. {
  1740. goto _cont;
  1741. }
  1742. }
  1743. /* Clear selected interrupts. */
  1744. mask = intmask & (RT_SDHCI_INT_CMD_MASK | RT_SDHCI_INT_DATA_MASK | RT_SDHCI_INT_BUS_POWER);
  1745. rt_sdhci_writel(host, mask, RT_SDHCI_INT_STATUS);
  1746. if (intmask & (RT_SDHCI_INT_CARD_INSERT | RT_SDHCI_INT_CARD_REMOVE))
  1747. {
  1748. rt_uint32_t present = rt_sdhci_readl(host, RT_SDHCI_PRESENT_STATE) & RT_SDHCI_CARD_PRESENT;
  1749. host->ier &= ~(RT_SDHCI_INT_CARD_INSERT | RT_SDHCI_INT_CARD_REMOVE);
  1750. host->ier |= present ? RT_SDHCI_INT_CARD_REMOVE : RT_SDHCI_INT_CARD_INSERT;
  1751. rt_sdhci_writel(host, host->ier, RT_SDHCI_INT_ENABLE);
  1752. rt_sdhci_writel(host, host->ier, RT_SDHCI_SIGNAL_ENABLE);
  1753. rt_sdhci_writel(host, intmask & (RT_SDHCI_INT_CARD_INSERT | RT_SDHCI_INT_CARD_REMOVE),
  1754. RT_SDHCI_INT_STATUS);
  1755. host->thread_isr |= intmask & (RT_SDHCI_INT_CARD_INSERT | RT_SDHCI_INT_CARD_REMOVE);
  1756. result = IRQ_WAIT;
  1757. }
  1758. if (intmask & RT_SDHCI_INT_CMD_MASK)
  1759. {
  1760. sdhci_cmd_irq(host, intmask & RT_SDHCI_INT_CMD_MASK, &intmask);
  1761. }
  1762. if (intmask & RT_SDHCI_INT_DATA_MASK)
  1763. {
  1764. sdhci_data_irq(host, intmask & RT_SDHCI_INT_DATA_MASK);
  1765. }
  1766. if (intmask & RT_SDHCI_INT_BUS_POWER)
  1767. {
  1768. rt_kprintf("%s: Card is consuming too much power!\n",
  1769. mmc_hostname(host->mmc));
  1770. }
  1771. intmask &= ~(RT_SDHCI_INT_CARD_INSERT | RT_SDHCI_INT_CARD_REMOVE |
  1772. RT_SDHCI_INT_CMD_MASK | RT_SDHCI_INT_DATA_MASK |
  1773. RT_SDHCI_INT_ERROR | RT_SDHCI_INT_BUS_POWER |
  1774. RT_SDHCI_INT_RETUNE | RT_SDHCI_INT_CARD_INT);
  1775. if (intmask)
  1776. {
  1777. unexpected |= intmask;
  1778. rt_sdhci_writel(host, intmask, RT_SDHCI_INT_STATUS);
  1779. }
  1780. _cont:
  1781. if (result == IRQ_NONE)
  1782. {
  1783. result = IRQ_WAIT;
  1784. }
  1785. intmask = rt_sdhci_readl(host, RT_SDHCI_INT_STATUS);
  1786. } while (intmask && --max_loops);
  1787. for (int i = 0; i < RT_SDHCI_MAX_MRQS; ++i)
  1788. {
  1789. struct rt_mmcsd_req *mrq = host->mrqs_done[i];
  1790. if (!mrq)
  1791. {
  1792. continue;
  1793. }
  1794. if (sdhci_defer_done(host, mrq))
  1795. {
  1796. result = IRQ_WAIT;
  1797. }
  1798. else
  1799. {
  1800. mrqs_done[i] = mrq;
  1801. host->mrqs_done[i] = RT_NULL;
  1802. }
  1803. }
  1804. _out:
  1805. if (host->deferred_cmd)
  1806. {
  1807. result = IRQ_WAIT;
  1808. }
  1809. rt_spin_unlock(&host->lock);
  1810. for (int i = 0; i < RT_SDHCI_MAX_MRQS; ++i)
  1811. {
  1812. if (!mrqs_done[i])
  1813. {
  1814. continue;
  1815. }
  1816. if (host->ops->request_done)
  1817. {
  1818. host->ops->request_done(host, mrqs_done[i]);
  1819. }
  1820. else
  1821. {
  1822. rt_mmc_request_done(host->mmc, mrqs_done[i]);
  1823. }
  1824. }
  1825. if (unexpected)
  1826. {
  1827. rt_kprintf("%s: Unexpected interrupt 0x%08x.\n",
  1828. mmc_hostname(host->mmc), unexpected);
  1829. rt_sdhci_read_reg_debug(host);
  1830. }
  1831. if (result == IRQ_WAIT)
  1832. {
  1833. rt_workqueue_submit_work(host->irq_wq, &host->irq_work, 0);
  1834. }
  1835. }
  1836. static rt_bool_t sdhci_send_command_retry(struct rt_sdhci_host *host,
  1837. struct rt_mmcsd_cmd *cmd,
  1838. unsigned long flags)
  1839. {
  1840. int timeout;
  1841. rt_bool_t present;
  1842. struct rt_mmcsd_cmd *deferred_cmd = host->deferred_cmd;
  1843. timeout = 10;
  1844. while (!sdhci_send_command(host, cmd))
  1845. {
  1846. if (!timeout--)
  1847. {
  1848. rt_kprintf("%s: Controller never released inhibit bit(s).\n",
  1849. mmc_hostname(host->mmc));
  1850. rt_sdhci_read_reg_debug(host);
  1851. cmd->err = -EIO;
  1852. return RT_FALSE;
  1853. }
  1854. rt_spin_unlock_irqrestore(&host->lock, flags);
  1855. rt_thread_mdelay(1);
  1856. present = host->mmc->ops->get_cd(host->mmc);
  1857. flags = rt_spin_lock_irqsave(&host->lock);
  1858. if (cmd == deferred_cmd && cmd != host->deferred_cmd)
  1859. {
  1860. return RT_TRUE;
  1861. }
  1862. if (sdhci_present_error(host, cmd, present))
  1863. {
  1864. return RT_FALSE;
  1865. }
  1866. }
  1867. if (cmd == host->deferred_cmd)
  1868. {
  1869. host->deferred_cmd = RT_NULL;
  1870. }
  1871. return RT_TRUE;
  1872. }
  1873. static rt_bool_t rt_sdhci_start_request_done(struct rt_sdhci_host *host)
  1874. {
  1875. int i;
  1876. rt_base_t flags;
  1877. struct rt_mmcsd_req *mrq;
  1878. flags = rt_spin_lock_irqsave(&host->lock);
  1879. for (i = 0; i < RT_SDHCI_MAX_MRQS; ++i)
  1880. {
  1881. mrq = host->mrqs_done[i];
  1882. if (mrq)
  1883. {
  1884. break;
  1885. }
  1886. }
  1887. if (!mrq)
  1888. {
  1889. rt_spin_unlock_irqrestore(&host->lock, flags);
  1890. return RT_TRUE;
  1891. }
  1892. if (sdhci_needs_reset(host, mrq))
  1893. {
  1894. if (host->cmd || host->data_cmd)
  1895. {
  1896. rt_spin_unlock_irqrestore(&host->lock, flags);
  1897. return RT_TRUE;
  1898. }
  1899. /* Some controllers need this kick or reset won't work here */
  1900. if (host->quirks & RT_SDHCI_QUIRK_CLOCK_BEFORE_RESET)
  1901. {
  1902. /* This is to force an update */
  1903. host->ops->set_clock(host, host->clock);
  1904. }
  1905. sdhci_reset_for(host, REQUEST_ERROR);
  1906. host->pending_reset = RT_FALSE;
  1907. }
  1908. if (host->flags & RT_SDHCI_REQ_USE_DMA)
  1909. {
  1910. struct rt_mmcsd_data *data = mrq->data;
  1911. if (data && data->host_cookie == RT_SDHCI_COOKIE_MAPPED)
  1912. {
  1913. if (host->bounce_buffer)
  1914. {
  1915. /* On reads, copy the bounced data into the sglist */
  1916. if (mmc_get_dma_dir(data) == DMA_FROM_DEVICE)
  1917. {
  1918. unsigned int length = data->bytes_xfered;
  1919. if (length > host->bounce_buffer_size)
  1920. {
  1921. LOG_E("%s: bounce buffer is %u bytes but DMA claims to have transferred %u bytes",
  1922. mmc_hostname(host->mmc),
  1923. host->bounce_buffer_size,
  1924. data->bytes_xfered);
  1925. /* Cap it down and continue */
  1926. length = host->bounce_buffer_size;
  1927. }
  1928. rt_hw_cpu_dcache_ops(RT_HW_CACHE_INVALIDATE, data->buf,
  1929. data->blks * data->blksize);
  1930. }
  1931. else
  1932. {
  1933. /* No copying, just switch ownership */
  1934. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, data->buf,
  1935. data->blks * data->blksize);
  1936. }
  1937. }
  1938. data->host_cookie = RT_SDHCI_COOKIE_UNMAPPED;
  1939. }
  1940. else
  1941. {
  1942. if (mmc_get_dma_dir(data) == DMA_FROM_DEVICE)
  1943. {
  1944. rt_hw_cpu_dcache_ops(RT_HW_CACHE_INVALIDATE, data->buf,
  1945. data->blks * data->blksize);
  1946. }
  1947. else
  1948. {
  1949. /* No copying, just switch ownership */
  1950. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, data->buf,
  1951. data->blks * data->blksize);
  1952. }
  1953. }
  1954. }
  1955. host->mrqs_done[i] = RT_NULL;
  1956. rt_spin_unlock_irqrestore(&host->lock, flags);
  1957. if (host->ops->request_done)
  1958. {
  1959. host->ops->request_done(host, mrq);
  1960. }
  1961. else
  1962. {
  1963. rt_mmc_request_done(host->mmc, mrq);
  1964. }
  1965. return RT_FALSE;
  1966. }
  1967. static void sdhci_thread_irq(struct rt_work *work, void *work_data)
  1968. {
  1969. rt_base_t flags;
  1970. rt_uint32_t isr;
  1971. struct rt_mmcsd_cmd *cmd;
  1972. struct rt_sdhci_host *host = work_data;
  1973. while (!rt_sdhci_start_request_done(host))
  1974. {
  1975. rt_hw_cpu_relax();
  1976. }
  1977. flags = rt_spin_lock_irqsave(&host->lock);
  1978. isr = host->thread_isr;
  1979. host->thread_isr = 0;
  1980. cmd = host->deferred_cmd;
  1981. if (cmd && !sdhci_send_command_retry(host, cmd, flags))
  1982. {
  1983. sdhci_finish_mrq(host, cmd->mrq);
  1984. }
  1985. rt_spin_unlock_irqrestore(&host->lock, flags);
  1986. if (isr & (RT_SDHCI_INT_CARD_INSERT | RT_SDHCI_INT_CARD_REMOVE))
  1987. {
  1988. struct rt_mmc_host *mmc = host->mmc;
  1989. mmc->ops->card_event(mmc);
  1990. }
  1991. }
  1992. void rt_sdhci_enable_io_irq(struct rt_mmc_host *mmc, int enable)
  1993. {
  1994. struct rt_sdhci_host *host = rt_mmc_priv(mmc);
  1995. rt_uint32_t flags;
  1996. flags = rt_spin_lock_irqsave(&host->lock);
  1997. rt_sdhci_enable_io_irq_nolock(host, enable);
  1998. rt_spin_unlock_irqrestore(&host->lock, flags);
  1999. }
  2000. /********************************************************* */
  2001. /* request */
  2002. /********************************************************* */
  2003. void rt_sdhci_start_request(struct rt_mmc_host *mmc, struct rt_mmcsd_req *mrq)
  2004. {
  2005. rt_base_t flags;
  2006. rt_bool_t present;
  2007. struct rt_mmcsd_cmd *cmd;
  2008. struct rt_sdhci_host *host = rt_mmc_priv(mmc);
  2009. /* Firstly check card presence */
  2010. present = mmc->ops->get_cd(mmc);
  2011. flags = rt_spin_lock_irqsave(&host->lock);
  2012. if (sdhci_present_error(host, mrq->cmd, present))
  2013. {
  2014. goto _out_finish;
  2015. }
  2016. cmd = sdhci_manual_cmd23(host, mrq) ? mrq->sbc : mrq->cmd;
  2017. if (!sdhci_send_command_retry(host, cmd, flags))
  2018. {
  2019. goto _out_finish;
  2020. }
  2021. rt_spin_unlock_irqrestore(&host->lock, flags);
  2022. return;
  2023. _out_finish:
  2024. sdhci_finish_mrq(host, mrq);
  2025. rt_spin_unlock_irqrestore(&host->lock, flags);
  2026. }
  2027. static void sdhci_complete_work(struct rt_work *work, void *work_data)
  2028. {
  2029. struct rt_sdhci_host *host = work_data;
  2030. while (!rt_sdhci_start_request_done(host))
  2031. {
  2032. rt_hw_cpu_relax();
  2033. }
  2034. }
  2035. /********************************************************* */
  2036. /* timer */
  2037. /********************************************************* */
  2038. static void sdhci_timeout_timer(void *parameter)
  2039. {
  2040. rt_base_t flags;
  2041. struct rt_sdhci_host *host = parameter;
  2042. flags = rt_spin_lock_irqsave(&host->lock);
  2043. if (host->cmd && !sdhci_data_line_cmd(host->cmd))
  2044. {
  2045. rt_kprintf("%s: Timeout waiting for hardware cmd interrupt.\n",
  2046. mmc_hostname(host->mmc));
  2047. rt_sdhci_read_reg_debug(host);
  2048. host->cmd->err = -ETIMEDOUT;
  2049. sdhci_finish_mrq(host, host->cmd->mrq);
  2050. }
  2051. rt_spin_unlock_irqrestore(&host->lock, flags);
  2052. }
  2053. static void sdhci_timeout_data_timer(void *parameter)
  2054. {
  2055. rt_base_t flags;
  2056. struct rt_sdhci_host *host = parameter;
  2057. flags = rt_spin_lock_irqsave(&host->lock);
  2058. if (host->data || host->data_cmd || (host->cmd && sdhci_data_line_cmd(host->cmd)))
  2059. {
  2060. rt_kprintf("%s: Timeout waiting for hardware interrupt.\n",
  2061. mmc_hostname(host->mmc));
  2062. rt_sdhci_read_reg_debug(host);
  2063. if (host->data)
  2064. {
  2065. host->data->err = -ETIMEDOUT;
  2066. __sdhci_finish_data(host, RT_TRUE);
  2067. rt_workqueue_submit_work(host->complete_wq, &host->complete_work, 0);
  2068. }
  2069. else if (host->data_cmd)
  2070. {
  2071. host->data_cmd->err = -ETIMEDOUT;
  2072. sdhci_finish_mrq(host, host->data_cmd->mrq);
  2073. }
  2074. else
  2075. {
  2076. host->cmd->err = -ETIMEDOUT;
  2077. sdhci_finish_mrq(host, host->cmd->mrq);
  2078. }
  2079. }
  2080. rt_spin_unlock_irqrestore(&host->lock, flags);
  2081. }
  2082. /********************************************************* */
  2083. /* tuning */
  2084. /********************************************************* */
  2085. int rt_sdhci_execute_tuning(struct rt_mmc_host *mmc, rt_uint32_t opcode)
  2086. {
  2087. int err = 0;
  2088. rt_bool_t hs400_tuning;
  2089. unsigned int tuning_count = 0;
  2090. struct rt_sdhci_host *host = rt_mmc_priv(mmc);
  2091. hs400_tuning = host->flags & RT_SDHCI_HS400_TUNING;
  2092. if (host->tuning_mode == RT_SDHCI_TUNING_MODE_1)
  2093. {
  2094. tuning_count = host->tuning_count;
  2095. }
  2096. switch (host->timing)
  2097. {
  2098. /* HS400 tuning is done in HS200 mode */
  2099. case MMC_TIMING_MMC_HS400:
  2100. err = -EINVAL;
  2101. goto _out;
  2102. case MMC_TIMING_MMC_HS200:
  2103. if (hs400_tuning)
  2104. {
  2105. tuning_count = 0;
  2106. }
  2107. break;
  2108. case MMC_TIMING_UHS_SDR104:
  2109. case MMC_TIMING_UHS_DDR50:
  2110. break;
  2111. case MMC_TIMING_UHS_SDR50:
  2112. if (host->flags & RT_SDHCI_SDR50_NEEDS_TUNING)
  2113. {
  2114. break;
  2115. }
  2116. /* Fallthrough */
  2117. default:
  2118. goto _out;
  2119. }
  2120. if (host->ops->platform_execute_tuning)
  2121. {
  2122. err = host->ops->platform_execute_tuning(host, opcode);
  2123. goto _out;
  2124. }
  2125. mmc->retune_period = tuning_count;
  2126. if (host->tuning_delay < 0)
  2127. {
  2128. host->tuning_delay = opcode == MMC_SEND_TUNING_BLOCK;
  2129. }
  2130. rt_sdhci_start_tuning(host);
  2131. host->tuning_err = __sdhci_execute_tuning(host, opcode);
  2132. rt_sdhci_end_tuning(host);
  2133. _out:
  2134. host->flags &= ~RT_SDHCI_HS400_TUNING;
  2135. return err;
  2136. }
  2137. int __sdhci_execute_tuning(struct rt_sdhci_host *host, rt_uint32_t opcode)
  2138. {
  2139. for (int i = 0; i < host->tuning_loop_count; ++i)
  2140. {
  2141. rt_uint16_t ctrl;
  2142. rt_sdhci_send_tuning(host, opcode);
  2143. if (!host->tuning_done)
  2144. {
  2145. rt_sdhci_abort_tuning(host, opcode);
  2146. return -ETIMEDOUT;
  2147. }
  2148. if (host->tuning_delay > 0)
  2149. {
  2150. rt_thread_mdelay(host->tuning_delay);
  2151. }
  2152. ctrl = rt_sdhci_readw(host, RT_SDHCI_HOST_CONTROL2);
  2153. if (!(ctrl & RT_SDHCI_CTRL_EXEC_TUNING))
  2154. {
  2155. if (ctrl & RT_SDHCI_CTRL_TUNED_CLK)
  2156. {
  2157. return 0; /* Success! */
  2158. }
  2159. break;
  2160. }
  2161. }
  2162. LOG_D("%s: Tuning failed, falling back to fixed sampling clock",
  2163. mmc_hostname(host->mmc));
  2164. rt_sdhci_reset_tuning(host);
  2165. return -EAGAIN;
  2166. }
  2167. void rt_sdhci_start_tuning(struct rt_sdhci_host *host)
  2168. {
  2169. rt_uint16_t ctrl;
  2170. ctrl = rt_sdhci_readw(host, RT_SDHCI_HOST_CONTROL2);
  2171. ctrl |= RT_SDHCI_CTRL_EXEC_TUNING;
  2172. if (host->quirks2 & RT_SDHCI_QUIRK2_TUNING_WORK_AROUND)
  2173. {
  2174. ctrl |= RT_SDHCI_CTRL_TUNED_CLK;
  2175. }
  2176. rt_sdhci_writew(host, ctrl, RT_SDHCI_HOST_CONTROL2);
  2177. rt_sdhci_writel(host, RT_SDHCI_INT_DATA_AVAIL, RT_SDHCI_INT_ENABLE);
  2178. rt_sdhci_writel(host, RT_SDHCI_INT_DATA_AVAIL, RT_SDHCI_SIGNAL_ENABLE);
  2179. }
  2180. void rt_sdhci_end_tuning(struct rt_sdhci_host *host)
  2181. {
  2182. rt_sdhci_writel(host, host->ier, RT_SDHCI_INT_ENABLE);
  2183. rt_sdhci_writel(host, host->ier, RT_SDHCI_SIGNAL_ENABLE);
  2184. }
  2185. void rt_sdhci_abort_tuning(struct rt_sdhci_host *host, rt_uint32_t opcode)
  2186. {
  2187. rt_sdhci_reset_tuning(host);
  2188. sdhci_reset_for(host, TUNING_ABORT);
  2189. rt_sdhci_end_tuning(host);
  2190. }
  2191. void rt_sdhci_send_tuning(struct rt_sdhci_host *host, rt_uint32_t opcode)
  2192. {
  2193. unsigned long flags;
  2194. rt_uint32_t b = host->sdma_boundary;
  2195. struct rt_mmcsd_cmd cmd = {};
  2196. struct rt_mmcsd_req mrq = {};
  2197. struct rt_mmc_host *mmc = host->mmc;
  2198. flags = rt_spin_lock_irqsave(&host->lock);
  2199. cmd.cmd_code = opcode;
  2200. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  2201. cmd.mrq = &mrq;
  2202. mrq.cmd = &cmd;
  2203. if (cmd.cmd_code == MMC_SEND_TUNING_BLOCK_HS200 && mmc->ios.bus_width == MMC_BUS_WIDTH_8)
  2204. {
  2205. rt_sdhci_writew(host, RT_SDHCI_MAKE_BLKSZ(b, 128), RT_SDHCI_BLOCK_SIZE);
  2206. }
  2207. else
  2208. {
  2209. rt_sdhci_writew(host, RT_SDHCI_MAKE_BLKSZ(b, 64), RT_SDHCI_BLOCK_SIZE);
  2210. }
  2211. rt_sdhci_writew(host, RT_SDHCI_TRNS_READ, RT_SDHCI_TRANSFER_MODE);
  2212. if (!sdhci_send_command_retry(host, &cmd, flags))
  2213. {
  2214. rt_spin_unlock_irqrestore(&host->lock, flags);
  2215. host->tuning_done = 0;
  2216. return;
  2217. }
  2218. host->cmd = RT_NULL;
  2219. sdhci_del_timer(host, &mrq);
  2220. host->tuning_done = 0;
  2221. rt_spin_unlock_irqrestore(&host->lock, flags);
  2222. }
  2223. void rt_sdhci_reset_tuning(struct rt_sdhci_host *host)
  2224. {
  2225. rt_uint16_t ctrl;
  2226. ctrl = rt_sdhci_readw(host, RT_SDHCI_HOST_CONTROL2);
  2227. ctrl &= ~RT_SDHCI_CTRL_TUNED_CLK;
  2228. ctrl &= ~RT_SDHCI_CTRL_EXEC_TUNING;
  2229. rt_sdhci_writew(host, ctrl, RT_SDHCI_HOST_CONTROL2);
  2230. }
  2231. /********************************************************* */
  2232. /* error */
  2233. /********************************************************* */
  2234. static const struct rt_mmc_host_ops rt_sdhci_ops =
  2235. {
  2236. .request = rt_sdhci_start_request,
  2237. .set_ios = rt_sdhci_ios_set,
  2238. .get_cd = sdhci_get_cd,
  2239. .get_ro = rt_sdhci_ro_get,
  2240. .enable_sdio_irq = rt_sdhci_enable_io_irq,
  2241. .ack_sdio_irq = sdhci_ack_sdio_irq,
  2242. .start_signal_voltage_switch = rt_sdhci_start_signal_voltage_switch,
  2243. .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
  2244. .execute_tuning = rt_sdhci_execute_tuning,
  2245. .card_event = sdhci_card_event,
  2246. .card_busy = sdhci_card_busy,
  2247. };
  2248. void rt_sdhci_uninit_host(struct rt_sdhci_host *host, rt_bool_t dead)
  2249. {
  2250. unsigned long flags;
  2251. struct rt_mmc_host *mmc = host->mmc;
  2252. if (dead)
  2253. {
  2254. flags = rt_spin_lock_irqsave(&host->lock);
  2255. host->flags |= RT_SDHCI_DEVICE_DEAD;
  2256. if (sdhci_has_requests(host))
  2257. {
  2258. rt_kprintf("%s: Controller removed during transfer!\n",
  2259. mmc_hostname(mmc));
  2260. sdhci_error_out_mrqs(host, -ENOMEDIUM);
  2261. }
  2262. rt_spin_unlock_irqrestore(&host->lock, flags);
  2263. }
  2264. sdhci_set_card_detection(host, RT_FALSE);
  2265. rt_mmc_remove_host(mmc);
  2266. if (!dead)
  2267. {
  2268. sdhci_reset_for_all(host);
  2269. }
  2270. rt_sdhci_writel(host, 0, RT_SDHCI_INT_ENABLE);
  2271. rt_sdhci_writel(host, 0, RT_SDHCI_SIGNAL_ENABLE);
  2272. rt_timer_delete(&host->timer);
  2273. rt_timer_delete(&host->data_timer);
  2274. rt_workqueue_destroy(host->complete_wq);
  2275. if (host->sdhci_core_to_disable_vqmmc)
  2276. {
  2277. rt_regulator_disable(mmc->rthost.supply.vqmmc);
  2278. }
  2279. }
  2280. rt_uint16_t rt_sdhci_clk_set(struct rt_sdhci_host *host, unsigned int clock,
  2281. unsigned int *actual_clock)
  2282. {
  2283. rt_uint16_t clk = 0;
  2284. rt_bool_t switch_base_clk = RT_FALSE;
  2285. int div = 0, real_div = 0, clk_mul = 1;
  2286. if (host->version >= RT_SDHCI_SPEC_300)
  2287. {
  2288. if (host->preset_enabled)
  2289. {
  2290. rt_uint16_t pre_val;
  2291. clk = rt_sdhci_readw(host, RT_SDHCI_CLOCK_CONTROL);
  2292. pre_val = sdhci_get_preset_value(host);
  2293. div = RT_FIELD_GET(RT_SDHCI_PRESET_SDCLK_FREQ_MASK, pre_val);
  2294. if (host->clk_mul && (pre_val & RT_SDHCI_PRESET_CLKGEN_SEL))
  2295. {
  2296. clk = RT_SDHCI_PROG_CLOCK_MODE;
  2297. real_div = div + 1;
  2298. clk_mul = host->clk_mul;
  2299. }
  2300. else
  2301. {
  2302. real_div = rt_max_t(int, 1, div << 1);
  2303. }
  2304. goto _clock_set;
  2305. }
  2306. if (host->clk_mul)
  2307. {
  2308. for (div = 1; div <= 1024; div++)
  2309. {
  2310. if ((host->max_clk * host->clk_mul / div) <= clock)
  2311. {
  2312. break;
  2313. }
  2314. }
  2315. if ((host->max_clk * host->clk_mul / div) <= clock)
  2316. {
  2317. clk = RT_SDHCI_PROG_CLOCK_MODE;
  2318. real_div = div;
  2319. clk_mul = host->clk_mul;
  2320. div--;
  2321. }
  2322. else
  2323. {
  2324. switch_base_clk = RT_TRUE;
  2325. }
  2326. }
  2327. if (!host->clk_mul || switch_base_clk)
  2328. {
  2329. if (host->max_clk <= clock)
  2330. {
  2331. div = 1;
  2332. }
  2333. else
  2334. {
  2335. for (div = 2; div < RT_SDHCI_MAX_DIV_SPEC_300; div += 2)
  2336. {
  2337. if ((host->max_clk / div) <= clock)
  2338. {
  2339. break;
  2340. }
  2341. }
  2342. }
  2343. real_div = div;
  2344. div >>= 1;
  2345. if ((host->quirks2 & RT_SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
  2346. && !div && host->max_clk <= 25000000)
  2347. {
  2348. div = 1;
  2349. }
  2350. }
  2351. }
  2352. else
  2353. {
  2354. for (div = 1; div < RT_SDHCI_MAX_DIV_SPEC_200; div *= 2)
  2355. {
  2356. if ((host->max_clk / div) <= clock)
  2357. {
  2358. break;
  2359. }
  2360. }
  2361. real_div = div;
  2362. div >>= 1;
  2363. }
  2364. _clock_set:
  2365. if (real_div)
  2366. {
  2367. *actual_clock = (host->max_clk * clk_mul) / real_div;
  2368. }
  2369. clk |= (div & RT_SDHCI_DIV_MASK) << RT_SDHCI_DIVIDER_SHIFT;
  2370. clk |= ((div & RT_SDHCI_DIV_HI_MASK) >> RT_SDHCI_DIV_MASK_LEN)
  2371. << RT_SDHCI_DIVIDER_HI_SHIFT;
  2372. return clk;
  2373. }
  2374. void rt_sdhci_clk_enable(struct rt_sdhci_host *host, rt_uint16_t clk)
  2375. {
  2376. long timeout;
  2377. clk |= RT_SDHCI_CLOCK_INT_EN;
  2378. rt_sdhci_writew(host, clk, RT_SDHCI_CLOCK_CONTROL);
  2379. timeout = rt_tick_from_millisecond(150);
  2380. while (RT_TRUE)
  2381. {
  2382. timeout = timeout - rt_tick_get();
  2383. clk = rt_sdhci_readw(host, RT_SDHCI_CLOCK_CONTROL);
  2384. if (clk & RT_SDHCI_CLOCK_INT_STABLE)
  2385. {
  2386. break;
  2387. }
  2388. if (timeout < 0)
  2389. {
  2390. rt_kprintf("%s: Internal clock never stabilised.\n",
  2391. mmc_hostname(host->mmc));
  2392. rt_sdhci_read_reg_debug(host);
  2393. return;
  2394. }
  2395. rt_hw_us_delay(10);
  2396. }
  2397. if (host->version >= RT_SDHCI_SPEC_410 && host->v4_mode)
  2398. {
  2399. clk |= RT_SDHCI_CLOCK_PLL_EN;
  2400. clk &= ~RT_SDHCI_CLOCK_INT_STABLE;
  2401. rt_sdhci_writew(host, clk, RT_SDHCI_CLOCK_CONTROL);
  2402. timeout = rt_tick_from_millisecond(150);
  2403. while (RT_TRUE)
  2404. {
  2405. timeout = timeout - rt_tick_get();
  2406. clk = rt_sdhci_readw(host, RT_SDHCI_CLOCK_CONTROL);
  2407. if (clk & RT_SDHCI_CLOCK_INT_STABLE)
  2408. {
  2409. break;
  2410. }
  2411. if (timeout < 0)
  2412. {
  2413. rt_kprintf("%s: PLL clock never stabilised.\n",
  2414. mmc_hostname(host->mmc));
  2415. rt_sdhci_read_reg_debug(host);
  2416. return;
  2417. }
  2418. rt_hw_us_delay(10);
  2419. }
  2420. }
  2421. clk |= RT_SDHCI_CLOCK_CARD_EN;
  2422. rt_sdhci_writew(host, clk, RT_SDHCI_CLOCK_CONTROL);
  2423. }
  2424. void rt_sdhci_set_clock(struct rt_sdhci_host *host, unsigned int clock)
  2425. {
  2426. rt_uint16_t clk;
  2427. host->mmc->actual_clock = 0;
  2428. rt_sdhci_writew(host, 0, RT_SDHCI_CLOCK_CONTROL);
  2429. if (clock == 0)
  2430. {
  2431. return;
  2432. }
  2433. clk = rt_sdhci_clk_set(host, clock, &host->mmc->actual_clock);
  2434. rt_sdhci_clk_enable(host, clk);
  2435. }
  2436. void rt_sdhci_read_caps(struct rt_sdhci_host *host, const rt_uint16_t *ver,
  2437. const rt_uint32_t *caps, const rt_uint32_t *caps1)
  2438. {
  2439. rt_uint16_t v;
  2440. rt_uint64_t dt_caps_mask = 0, dt_caps = 0;
  2441. if (host->read_caps)
  2442. {
  2443. return;
  2444. }
  2445. host->read_caps = RT_TRUE;
  2446. if (debug_quirks)
  2447. {
  2448. host->quirks = debug_quirks;
  2449. }
  2450. if (debug_quirks2)
  2451. {
  2452. host->quirks2 = debug_quirks2;
  2453. }
  2454. sdhci_reset_for_all(host);
  2455. #ifdef RT_USING_OFW
  2456. rt_ofw_prop_read_u64(mmc_dev(host->mmc)->ofw_node,
  2457. "sdhci-caps-mask", &dt_caps_mask);
  2458. rt_ofw_prop_read_u64(mmc_dev(host->mmc)->ofw_node,
  2459. "sdhci-caps", &dt_caps);
  2460. #endif
  2461. v = ver ? *ver : rt_sdhci_readw(host, RT_SDHCI_HOST_VERSION);
  2462. host->version = (v & RT_SDHCI_SPEC_VER_MASK) >> RT_SDHCI_SPEC_VER_SHIFT;
  2463. if (caps)
  2464. {
  2465. host->caps = *caps;
  2466. }
  2467. else
  2468. {
  2469. host->caps = rt_sdhci_readl(host, RT_SDHCI_CAPABILITIES);
  2470. host->caps &= ~rt_lower_32_bits(dt_caps_mask);
  2471. host->caps |= rt_lower_32_bits(dt_caps);
  2472. }
  2473. if (host->version < RT_SDHCI_SPEC_300)
  2474. {
  2475. return;
  2476. }
  2477. if (caps1)
  2478. {
  2479. host->caps1 = *caps1;
  2480. }
  2481. else
  2482. {
  2483. host->caps1 = rt_sdhci_readl(host, RT_SDHCI_CAPABILITIES_1);
  2484. host->caps1 &= ~rt_upper_32_bits(dt_caps_mask);
  2485. host->caps1 |= rt_upper_32_bits(dt_caps);
  2486. }
  2487. }
  2488. struct rt_sdhci_host *rt_sdhci_alloc_host(struct rt_device *dev,
  2489. rt_size_t priv_size)
  2490. {
  2491. struct rt_mmc_host *mmc;
  2492. struct rt_sdhci_host *host;
  2493. mmc = rt_mmc_alloc_host(sizeof(struct rt_sdhci_host) + priv_size, dev);
  2494. if (!mmc)
  2495. {
  2496. return RT_NULL;
  2497. }
  2498. host = rt_mmc_priv(mmc);
  2499. host->mmc = mmc;
  2500. host->mmc_host_ops = rt_sdhci_ops;
  2501. mmc->ops = &host->mmc_host_ops;
  2502. host->flags = RT_SDHCI_SIGNALING_330;
  2503. host->cqe_ier = RT_SDHCI_CQE_INT_MASK;
  2504. host->cqe_err_ier = RT_SDHCI_CQE_INT_ERR_MASK;
  2505. host->tuning_delay = -1;
  2506. host->tuning_loop_count = RT_SDHCI_MAX_TUNING_LOOP;
  2507. host->sdma_boundary = RT_SDHCI_DEFAULT_BOUNDARY_ARG;
  2508. host->max_timeout_count = 0xe;
  2509. return host;
  2510. }
  2511. int rt_sdhci_setup_host(struct rt_sdhci_host *host)
  2512. {
  2513. int ret = 0;
  2514. rt_bool_t enable_vqmmc = RT_FALSE;
  2515. rt_size_t max_current_caps, max_clk;
  2516. unsigned int ocr_avail, override_timeout_clk;
  2517. struct rt_mmc_host *mmc;
  2518. RT_ASSERT(host != RT_NULL);
  2519. mmc = host->mmc;
  2520. if (!mmc->rthost.supply.vqmmc)
  2521. {
  2522. ret = sdio_regulator_get_supply(mmc->parent,&mmc->rthost);
  2523. if (ret)
  2524. {
  2525. return ret;
  2526. }
  2527. enable_vqmmc = RT_TRUE;
  2528. }
  2529. LOG_D("Version: 0x%08x | Present: 0x%08x",
  2530. rt_sdhci_readw(host, RT_SDHCI_HOST_VERSION),
  2531. rt_sdhci_readl(host, RT_SDHCI_PRESENT_STATE));
  2532. LOG_D("Caps: 0x%08x | Caps_1: 0x%08x",
  2533. rt_sdhci_readl(host, RT_SDHCI_CAPABILITIES),
  2534. rt_sdhci_readl(host, RT_SDHCI_CAPABILITIES_1));
  2535. rt_sdhci_read_caps(host,RT_NULL,RT_NULL,RT_NULL);
  2536. override_timeout_clk = host->timeout_clk;
  2537. if (host->version > RT_SDHCI_SPEC_420)
  2538. {
  2539. rt_kprintf("%s: Unknown controller version (%d). You may experience problems.\n",
  2540. mmc_hostname(mmc), host->version);
  2541. }
  2542. if (host->quirks & RT_SDHCI_QUIRK_FORCE_DMA)
  2543. {
  2544. host->flags |= RT_SDHCI_USE_SDMA;
  2545. }
  2546. else if (!(host->caps & RT_SDHCI_CAN_DO_SDMA))
  2547. {
  2548. LOG_D("Controller doesn't have SDMA capability");
  2549. }
  2550. else
  2551. {
  2552. host->flags |= RT_SDHCI_USE_SDMA;
  2553. }
  2554. if ((host->quirks & RT_SDHCI_QUIRK_BROKEN_DMA) && (host->flags & RT_SDHCI_USE_SDMA))
  2555. {
  2556. LOG_D("Disabling DMA as it is marked broken");
  2557. host->flags &= ~RT_SDHCI_USE_SDMA;
  2558. }
  2559. if (sdhci_can_64bit_dma(host))
  2560. {
  2561. host->flags |= RT_SDHCI_USE_64_BIT_DMA;
  2562. }
  2563. if (host->flags & RT_SDHCI_USE_SDMA)
  2564. {
  2565. if (host->ops->set_dma_mask)
  2566. {
  2567. ret = host->ops->set_dma_mask(host);
  2568. }
  2569. if (!ret && host->ops->enable_dma)
  2570. {
  2571. ret = host->ops->enable_dma(host);
  2572. }
  2573. if (ret)
  2574. {
  2575. rt_kprintf("%s: No suitable DMA available - falling back to PIO\n",
  2576. mmc_hostname(mmc));
  2577. host->flags &= ~RT_SDHCI_USE_SDMA;
  2578. ret = 0;
  2579. }
  2580. }
  2581. if ((host->flags & RT_SDHCI_USE_64_BIT_DMA) && !host->v4_mode)
  2582. {
  2583. host->flags &= ~RT_SDHCI_USE_SDMA;
  2584. }
  2585. if (!(host->flags & RT_SDHCI_USE_SDMA))
  2586. {
  2587. host->dma_mask = RT_DMA_ADDR_MASK(64);
  2588. }
  2589. if (host->version >= RT_SDHCI_SPEC_300)
  2590. {
  2591. host->max_clk = RT_FIELD_GET(RT_SDHCI_CLOCK_V3_BASE_MASK, host->caps);
  2592. }
  2593. else
  2594. {
  2595. host->max_clk = RT_FIELD_GET(RT_SDHCI_CLOCK_BASE_MASK, host->caps);
  2596. }
  2597. host->max_clk *= 1000000;
  2598. if (host->max_clk == 0 || host->quirks & RT_SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN)
  2599. {
  2600. if (!host->ops->get_max_clock)
  2601. {
  2602. rt_kprintf("%s: Hardware doesn't specify base clock frequency. %p \n",
  2603. mmc_hostname(mmc), host->ops->get_max_clock);
  2604. ret = -ENODEV;
  2605. goto _undma;
  2606. }
  2607. host->max_clk = host->ops->get_max_clock(host);
  2608. }
  2609. host->clk_mul = RT_FIELD_GET(RT_SDHCI_CLOCK_MUL_MASK, host->caps1);
  2610. if (host->clk_mul)
  2611. {
  2612. host->clk_mul += 1;
  2613. }
  2614. max_clk = host->max_clk;
  2615. if (host->ops->get_min_clock)
  2616. {
  2617. mmc->f_min = host->ops->get_min_clock(host);
  2618. }
  2619. else if (host->version >= RT_SDHCI_SPEC_300)
  2620. {
  2621. if (host->clk_mul)
  2622. {
  2623. max_clk = host->max_clk * host->clk_mul;
  2624. }
  2625. mmc->f_min = host->max_clk / RT_SDHCI_MAX_DIV_SPEC_300;
  2626. }
  2627. else
  2628. {
  2629. mmc->f_min = host->max_clk / RT_SDHCI_MAX_DIV_SPEC_200;
  2630. }
  2631. if (!mmc->f_max || mmc->f_max > max_clk)
  2632. {
  2633. mmc->f_max = max_clk;
  2634. }
  2635. if (!(host->quirks & RT_SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK))
  2636. {
  2637. host->timeout_clk = RT_FIELD_GET(RT_SDHCI_TIMEOUT_CLK_MASK, host->caps);
  2638. if (host->caps & RT_SDHCI_TIMEOUT_CLK_UNIT)
  2639. {
  2640. host->timeout_clk *= 1000;
  2641. }
  2642. if (host->timeout_clk == 0)
  2643. {
  2644. if (!host->ops->get_timeout_clock)
  2645. {
  2646. rt_kprintf("%s: Hardware doesn't specify timeout clock frequency.\n",
  2647. mmc_hostname(mmc));
  2648. ret = -ENODEV;
  2649. goto _undma;
  2650. }
  2651. host->timeout_clk = RT_DIV_ROUND_UP(host->ops->get_timeout_clock(host), 1000);
  2652. }
  2653. if (override_timeout_clk)
  2654. {
  2655. host->timeout_clk = override_timeout_clk;
  2656. }
  2657. mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
  2658. host->ops->get_max_timeout_count(host) : 1 << 27;
  2659. mmc->max_busy_timeout /= host->timeout_clk;
  2660. }
  2661. if (host->quirks2 & RT_SDHCI_QUIRK2_DISABLE_HW_TIMEOUT && !host->ops->get_max_timeout_count)
  2662. {
  2663. mmc->max_busy_timeout = 0;
  2664. }
  2665. mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_CMD23;
  2666. mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
  2667. if (host->quirks & RT_SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
  2668. {
  2669. host->flags |= RT_SDHCI_AUTO_CMD12;
  2670. }
  2671. if ((host->version >= RT_SDHCI_SPEC_300) &&
  2672. (!(host->flags & RT_SDHCI_USE_SDMA) || host->v4_mode) &&
  2673. !(host->quirks2 & RT_SDHCI_QUIRK2_ACMD23_BROKEN))
  2674. {
  2675. host->flags |= RT_SDHCI_AUTO_CMD23;
  2676. LOG_D("Auto-CMD23 available");
  2677. }
  2678. else
  2679. {
  2680. LOG_D("Auto-CMD23 unavailable");
  2681. }
  2682. if (!(host->quirks & RT_SDHCI_QUIRK_FORCE_1_BIT_DATA))
  2683. {
  2684. mmc->caps |= MMC_CAP_4_BIT_DATA;
  2685. }
  2686. if (host->quirks2 & RT_SDHCI_QUIRK2_HOST_NO_CMD23)
  2687. {
  2688. mmc->caps &= ~MMC_CAP_CMD23;
  2689. }
  2690. if (host->caps & RT_SDHCI_CAN_DO_HISPD)
  2691. {
  2692. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  2693. }
  2694. if ((host->quirks & RT_SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
  2695. mmc_card_is_removable(mmc) && rt_mmc_gpio_get_cd(mmc) < 0)
  2696. {
  2697. mmc->caps |= MMC_CAP_NEEDS_POLL;
  2698. }
  2699. if (mmc->rthost.supply.vqmmc)
  2700. {
  2701. if (enable_vqmmc)
  2702. {
  2703. rt_regulator_enable(mmc->rthost.supply.vqmmc);
  2704. host->sdhci_core_to_disable_vqmmc = !ret;
  2705. }
  2706. if (!rt_regulator_is_supported_voltage(mmc->rthost.supply.vqmmc, 1700000, 1950000))
  2707. {
  2708. host->caps1 &= ~(RT_SDHCI_SUPPORT_SDR104 | RT_SDHCI_SUPPORT_SDR50 | RT_SDHCI_SUPPORT_DDR50);
  2709. }
  2710. if (!rt_regulator_is_supported_voltage(mmc->rthost.supply.vqmmc, 2700000, 3600000))
  2711. {
  2712. host->flags &= ~RT_SDHCI_SIGNALING_330;
  2713. }
  2714. if (ret)
  2715. {
  2716. rt_kprintf("%s: Failed to enable vqmmc regulator: %d\n",
  2717. mmc_hostname(mmc), ret);
  2718. mmc->rthost.supply.vqmmc = (void *)-EINVAL;
  2719. }
  2720. }
  2721. if (host->quirks2 & RT_SDHCI_QUIRK2_NO_1_8_V)
  2722. {
  2723. host->caps1 &= ~(RT_SDHCI_SUPPORT_SDR104 | RT_SDHCI_SUPPORT_SDR50 | RT_SDHCI_SUPPORT_DDR50);
  2724. mmc->caps2 &= ~(MMC_CAP2_HSX00_1_8V | MMC_CAP2_HS400_ES);
  2725. mmc->caps &= ~(MMC_CAP_1_8V_DDR | MMC_CAP_UHS);
  2726. }
  2727. if (host->caps1 & (RT_SDHCI_SUPPORT_SDR104 | RT_SDHCI_SUPPORT_SDR50 | RT_SDHCI_SUPPORT_DDR50))
  2728. {
  2729. mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
  2730. }
  2731. if (host->caps1 & RT_SDHCI_SUPPORT_SDR104)
  2732. {
  2733. mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
  2734. if (!(host->quirks2 & RT_SDHCI_QUIRK2_BROKEN_HS200))
  2735. {
  2736. mmc->caps2 |= MMC_CAP2_HS200;
  2737. }
  2738. }
  2739. else if (host->caps1 & RT_SDHCI_SUPPORT_SDR50)
  2740. {
  2741. mmc->caps |= MMC_CAP_UHS_SDR50;
  2742. }
  2743. if ((host->quirks2 & RT_SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400) &&
  2744. (host->caps1 & RT_SDHCI_SUPPORT_HS400))
  2745. {
  2746. mmc->caps2 |= MMC_CAP2_HS400;
  2747. }
  2748. if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) && (!mmc->rthost.supply.vqmmc ||
  2749. !rt_regulator_is_supported_voltage(mmc->rthost.supply.vqmmc, 1100000, 1300000)))
  2750. {
  2751. mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
  2752. }
  2753. if ((host->caps1 & RT_SDHCI_SUPPORT_DDR50) &&
  2754. !(host->quirks2 & RT_SDHCI_QUIRK2_BROKEN_DDR50))
  2755. {
  2756. mmc->caps |= MMC_CAP_UHS_DDR50;
  2757. }
  2758. if (host->caps1 & RT_SDHCI_USE_SDR50_TUNING)
  2759. {
  2760. host->flags |= RT_SDHCI_SDR50_NEEDS_TUNING;
  2761. }
  2762. if (host->caps1 & RT_SDHCI_DRIVER_TYPE_A)
  2763. {
  2764. mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
  2765. }
  2766. if (host->caps1 & RT_SDHCI_DRIVER_TYPE_C)
  2767. {
  2768. mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
  2769. }
  2770. if (host->caps1 & RT_SDHCI_DRIVER_TYPE_D)
  2771. {
  2772. mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
  2773. }
  2774. host->tuning_count = RT_FIELD_GET(RT_SDHCI_RETUNING_TIMER_COUNT_MASK, host->caps1);
  2775. if (host->tuning_count)
  2776. {
  2777. host->tuning_count = 1 << (host->tuning_count - 1);
  2778. }
  2779. /* Re-tuning mode supported by the Host Controller */
  2780. host->tuning_mode = RT_FIELD_GET(RT_SDHCI_RETUNING_MODE_MASK, host->caps1);
  2781. ocr_avail = 0;
  2782. max_current_caps = rt_sdhci_readl(host, RT_SDHCI_MAX_CURRENT);
  2783. if (!max_current_caps && mmc->rthost.supply.vmmc)
  2784. {
  2785. int curr = rt_regulator_get_voltage(mmc->rthost.supply.vmmc);
  2786. if (curr > 0)
  2787. {
  2788. curr = curr / 1000; /* convert to mA */
  2789. curr = curr / RT_SDHCI_MAX_CURRENT_MULTIPLIER;
  2790. curr = rt_min_t(rt_uint32_t, curr, RT_SDHCI_MAX_CURRENT_LIMIT);
  2791. max_current_caps = RT_FIELD_PREP(RT_SDHCI_MAX_CURRENT_330_MASK, curr) |
  2792. RT_FIELD_PREP(RT_SDHCI_MAX_CURRENT_300_MASK, curr) |
  2793. RT_FIELD_PREP(RT_SDHCI_MAX_CURRENT_180_MASK, curr);
  2794. }
  2795. }
  2796. if (host->caps & RT_SDHCI_CAN_VDD_330)
  2797. {
  2798. ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
  2799. mmc->max_current_330 = RT_FIELD_GET(RT_SDHCI_MAX_CURRENT_330_MASK, max_current_caps);
  2800. mmc->max_current_330 *= RT_SDHCI_MAX_CURRENT_MULTIPLIER;
  2801. }
  2802. if (host->caps & RT_SDHCI_CAN_VDD_300)
  2803. {
  2804. ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
  2805. mmc->max_current_300 = RT_FIELD_GET(RT_SDHCI_MAX_CURRENT_300_MASK, max_current_caps);
  2806. mmc->max_current_300 *= RT_SDHCI_MAX_CURRENT_MULTIPLIER;
  2807. }
  2808. if (host->caps & RT_SDHCI_CAN_VDD_180)
  2809. {
  2810. ocr_avail |= MMC_VDD_165_195;
  2811. mmc->max_current_180 = RT_FIELD_GET(RT_SDHCI_MAX_CURRENT_180_MASK, max_current_caps);
  2812. mmc->max_current_180 *= RT_SDHCI_MAX_CURRENT_MULTIPLIER;
  2813. }
  2814. if (host->ocr_mask)
  2815. {
  2816. ocr_avail = host->ocr_mask;
  2817. }
  2818. if (mmc->ocr_avail)
  2819. {
  2820. ocr_avail = mmc->ocr_avail;
  2821. }
  2822. mmc->ocr_avail = ocr_avail;
  2823. mmc->ocr_avail_sdio = ocr_avail;
  2824. if (host->ocr_avail_sdio)
  2825. {
  2826. mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
  2827. }
  2828. mmc->ocr_avail_sd = ocr_avail;
  2829. if (host->ocr_avail_sd)
  2830. {
  2831. mmc->ocr_avail_sd &= host->ocr_avail_sd;
  2832. }
  2833. else
  2834. {
  2835. mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
  2836. }
  2837. mmc->ocr_avail_mmc = ocr_avail;
  2838. if (host->ocr_avail_mmc)
  2839. {
  2840. mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
  2841. }
  2842. if (mmc->ocr_avail == 0)
  2843. {
  2844. rt_kprintf("%s: Hardware doesn't report any support voltages.\n",
  2845. mmc_hostname(mmc));
  2846. ret = -ENODEV;
  2847. goto _unreg;
  2848. }
  2849. if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | MMC_CAP_UHS_SDR50 |
  2850. MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
  2851. (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
  2852. {
  2853. host->flags |= RT_SDHCI_SIGNALING_180;
  2854. }
  2855. if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
  2856. {
  2857. host->flags |= RT_SDHCI_SIGNALING_120;
  2858. }
  2859. rt_spin_lock_init(&host->lock);
  2860. mmc->max_req_size = 524288;
  2861. if (host->flags & RT_SDHCI_USE_SDMA)
  2862. {
  2863. mmc->max_segs = 1;
  2864. }
  2865. else
  2866. {
  2867. /* PIO */
  2868. mmc->max_segs = RT_SDHCI_MAX_SEGS;
  2869. }
  2870. mmc->max_seg_size = mmc->max_req_size;
  2871. if (host->quirks & RT_SDHCI_QUIRK_FORCE_BLK_SZ_2048)
  2872. {
  2873. mmc->max_blk_size = 2;
  2874. }
  2875. else
  2876. {
  2877. mmc->max_blk_size = (host->caps & RT_SDHCI_MAX_BLOCK_MASK) >> RT_SDHCI_MAX_BLOCK_SHIFT;
  2878. if (mmc->max_blk_size >= 3)
  2879. {
  2880. rt_kprintf("%s: Invalid maximum block size, assuming 512 bytes\n",
  2881. mmc_hostname(mmc));
  2882. mmc->max_blk_size = 0;
  2883. }
  2884. }
  2885. mmc->max_blk_size = 512 << mmc->max_blk_size;
  2886. /*
  2887. * Maximum block count.
  2888. */
  2889. mmc->max_blk_count = (host->quirks & RT_SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
  2890. return 0;
  2891. _unreg:
  2892. if (host->sdhci_core_to_disable_vqmmc)
  2893. {
  2894. rt_regulator_disable(mmc->rthost.supply.vqmmc);
  2895. }
  2896. _undma:
  2897. return ret;
  2898. }
  2899. static void sdhci_init(struct rt_sdhci_host *host, int soft)
  2900. {
  2901. rt_base_t flags;
  2902. struct rt_mmc_host *mmc = host->mmc;
  2903. if (soft)
  2904. {
  2905. sdhci_do_reset(host, RT_SDHCI_RESET_CMD | RT_SDHCI_RESET_DATA);
  2906. }
  2907. else
  2908. {
  2909. sdhci_do_reset(host, RT_SDHCI_RESET_ALL);
  2910. }
  2911. flags = rt_spin_lock_irqsave(&host->lock);
  2912. sdhci_set_default_irqs(host);
  2913. rt_spin_unlock_irqrestore(&host->lock, flags);
  2914. host->cqe_on = RT_FALSE;
  2915. if (soft)
  2916. {
  2917. /* force clock reconfiguration */
  2918. host->clock = 0;
  2919. host->reinit_uhs = RT_TRUE;
  2920. mmc->ops->set_ios(mmc, &mmc->ios);
  2921. }
  2922. }
  2923. static void sdhci_reinit(struct rt_sdhci_host *host)
  2924. {
  2925. rt_uint32_t cd = host->ier & (RT_SDHCI_INT_CARD_REMOVE | RT_SDHCI_INT_CARD_INSERT);
  2926. sdhci_init(host, 0);
  2927. sdhci_enable_card_detection(host);
  2928. if (cd != (host->ier & (RT_SDHCI_INT_CARD_REMOVE | RT_SDHCI_INT_CARD_INSERT)))
  2929. {
  2930. rt_mmc_detect_change(host->mmc, rt_tick_from_millisecond(200));
  2931. }
  2932. }
  2933. int rt_sdhci_init_host(struct rt_sdhci_host *host)
  2934. {
  2935. int ret, len;
  2936. char dev_name[32];
  2937. struct rt_mmc_host *mmc = host->mmc;
  2938. len = sdio_host_set_name(&mmc->rthost, dev_name);
  2939. if ((mmc->caps2 & MMC_CAP2_CQE) && (host->quirks & RT_SDHCI_QUIRK_BROKEN_CQE))
  2940. {
  2941. mmc->caps2 &= ~MMC_CAP2_CQE;
  2942. }
  2943. host->complete_wq = rt_workqueue_create(dev_name, RT_SYSTEM_WORKQUEUE_STACKSIZE, 20);
  2944. if (!host->complete_wq)
  2945. {
  2946. return -ENOMEM;
  2947. }
  2948. rt_work_init(&host->complete_work, sdhci_complete_work, host);
  2949. rt_sprintf(&dev_name[len], "-timer");
  2950. rt_timer_init(&host->timer, dev_name,
  2951. sdhci_timeout_timer, host, 0, RT_TIMER_FLAG_SOFT_TIMER);
  2952. rt_sprintf(&dev_name[len], "-data-timer");
  2953. rt_timer_init(&host->data_timer, dev_name,
  2954. sdhci_timeout_data_timer, host, 0, RT_TIMER_FLAG_SOFT_TIMER);
  2955. rt_wqueue_init(&host->buf_ready_int);
  2956. sdhci_init(host, 0);
  2957. rt_sprintf(&dev_name[len], "-irq");
  2958. host->irq_wq = rt_workqueue_create(dev_name, RT_SYSTEM_WORKQUEUE_STACKSIZE, 1);
  2959. rt_work_init(&host->irq_work, sdhci_thread_irq, host);
  2960. rt_hw_interrupt_install(host->irq, sdhci_irq, host, mmc_hostname(mmc));
  2961. rt_pic_irq_unmask(host->irq);
  2962. ret = rt_mmc_add_host(mmc);
  2963. if (ret)
  2964. {
  2965. goto _unirq;
  2966. }
  2967. LOG_D("%s: RT_SDHCI controller on %s [%s] using %s",
  2968. mmc_hostname(mmc), host->hw_name, mmc_dev(mmc)->parent.name,
  2969. (host->flags & RT_SDHCI_USE_SDMA) ? "DMA" : "PIO");
  2970. sdhci_enable_card_detection(host);
  2971. return 0;
  2972. _unirq:
  2973. sdhci_reset_for_all(host);
  2974. rt_sdhci_writel(host, 0, RT_SDHCI_INT_ENABLE);
  2975. rt_sdhci_writel(host, 0, RT_SDHCI_SIGNAL_ENABLE);
  2976. return ret;
  2977. }
  2978. int rt_sdhci_set_and_add_host(struct rt_sdhci_host *host)
  2979. {
  2980. int ret;
  2981. ret = rt_sdhci_setup_host(host);
  2982. if (ret)
  2983. {
  2984. return ret;
  2985. }
  2986. ret = rt_sdhci_init_host(host);
  2987. if (ret)
  2988. {
  2989. goto _cleanup;
  2990. }
  2991. return 0;
  2992. _cleanup:
  2993. rt_sdhci_cleanup_host(host);
  2994. return ret;
  2995. }
  2996. void rt_sdhci_ios_set(struct rt_mmc_host *mmc, struct rt_mmcsd_io_cfg *ios)
  2997. {
  2998. rt_uint8_t ctrl;
  2999. rt_bool_t reinit_uhs, turning_on_clk = RT_FALSE;
  3000. struct rt_sdhci_host *host = rt_mmc_priv(mmc);
  3001. reinit_uhs = host->reinit_uhs;
  3002. host->reinit_uhs = RT_FALSE;
  3003. if (ios->power_mode == MMC_POWER_UNDEFINED)
  3004. {
  3005. return;
  3006. }
  3007. if (host->flags & RT_SDHCI_DEVICE_DEAD)
  3008. {
  3009. if (mmc->rthost.supply.vmmc && ios->power_mode == MMC_POWER_OFF)
  3010. {
  3011. sdio_regulator_set_ocr(&mmc->rthost, mmc->rthost.supply.vmmc, 0);
  3012. }
  3013. return;
  3014. }
  3015. if (ios->power_mode == MMC_POWER_OFF)
  3016. {
  3017. rt_sdhci_writel(host, 0, RT_SDHCI_SIGNAL_ENABLE);
  3018. sdhci_reinit(host);
  3019. }
  3020. if (host->version >= RT_SDHCI_SPEC_300 &&
  3021. (ios->power_mode == MMC_POWER_UP) &&
  3022. !(host->quirks2 & RT_SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
  3023. {
  3024. sdhci_preset_value_enable(host, RT_FALSE);
  3025. }
  3026. if (!ios->clock || ios->clock != host->clock)
  3027. {
  3028. turning_on_clk = ios->clock && !host->clock;
  3029. host->ops->set_clock(host, ios->clock);
  3030. host->clock = ios->clock;
  3031. if (host->quirks & RT_SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK && host->clock)
  3032. {
  3033. host->timeout_clk = mmc->actual_clock ? mmc->actual_clock / 1000 : host->clock / 1000;
  3034. mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
  3035. host->ops->get_max_timeout_count(host) : 1 << 27;
  3036. mmc->max_busy_timeout /= host->timeout_clk;
  3037. }
  3038. }
  3039. if (host->ops->set_power)
  3040. {
  3041. host->ops->set_power(host, ios->power_mode, ios->vdd);
  3042. }
  3043. else
  3044. {
  3045. rt_sdhci_set_power(host, ios->power_mode, ios->vdd);
  3046. }
  3047. host->ops->set_bus_width(host, ios->bus_width);
  3048. if (!reinit_uhs && turning_on_clk &&
  3049. host->timing == ios->timing && host->version >= RT_SDHCI_SPEC_300 &&
  3050. !sdhci_presetable_values_change(host, ios))
  3051. {
  3052. return;
  3053. }
  3054. ctrl = rt_sdhci_readb(host, RT_SDHCI_HOST_CONTROL);
  3055. if (!(host->quirks & RT_SDHCI_QUIRK_NO_HISPD_BIT))
  3056. {
  3057. if (ios->timing == MMC_TIMING_SD_HS ||
  3058. ios->timing == MMC_TIMING_MMC_HS ||
  3059. ios->timing == MMC_TIMING_MMC_HS400 ||
  3060. ios->timing == MMC_TIMING_MMC_HS200 ||
  3061. ios->timing == MMC_TIMING_MMC_DDR52 ||
  3062. ios->timing == MMC_TIMING_UHS_SDR50 ||
  3063. ios->timing == MMC_TIMING_UHS_SDR104 ||
  3064. ios->timing == MMC_TIMING_UHS_DDR50 ||
  3065. ios->timing == MMC_TIMING_UHS_SDR25)
  3066. {
  3067. ctrl |= RT_SDHCI_CTRL_HISPD;
  3068. }
  3069. else
  3070. {
  3071. ctrl &= ~RT_SDHCI_CTRL_HISPD;
  3072. }
  3073. }
  3074. if (host->version >= RT_SDHCI_SPEC_300)
  3075. {
  3076. rt_uint16_t clk, ctrl_2;
  3077. clk = rt_sdhci_readw(host, RT_SDHCI_CLOCK_CONTROL);
  3078. if (clk & RT_SDHCI_CLOCK_CARD_EN)
  3079. {
  3080. clk &= ~RT_SDHCI_CLOCK_CARD_EN;
  3081. rt_sdhci_writew(host, clk, RT_SDHCI_CLOCK_CONTROL);
  3082. }
  3083. rt_sdhci_writeb(host, ctrl, RT_SDHCI_HOST_CONTROL);
  3084. if (!host->preset_enabled)
  3085. {
  3086. ctrl_2 = rt_sdhci_readw(host, RT_SDHCI_HOST_CONTROL2);
  3087. ctrl_2 &= ~RT_SDHCI_CTRL_DRV_TYPE_MASK;
  3088. if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
  3089. {
  3090. ctrl_2 |= RT_SDHCI_CTRL_DRV_TYPE_A;
  3091. }
  3092. else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
  3093. {
  3094. ctrl_2 |= RT_SDHCI_CTRL_DRV_TYPE_B;
  3095. }
  3096. else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
  3097. {
  3098. ctrl_2 |= RT_SDHCI_CTRL_DRV_TYPE_C;
  3099. }
  3100. else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
  3101. {
  3102. ctrl_2 |= RT_SDHCI_CTRL_DRV_TYPE_D;
  3103. }
  3104. else
  3105. {
  3106. LOG_D("%s: invalid driver type, default to driver type B",
  3107. mmc_hostname(mmc));
  3108. ctrl_2 |= RT_SDHCI_CTRL_DRV_TYPE_B;
  3109. }
  3110. rt_sdhci_writew(host, ctrl_2, RT_SDHCI_HOST_CONTROL2);
  3111. host->drv_type = ios->drv_type;
  3112. }
  3113. host->ops->set_uhs_signaling(host, ios->timing);
  3114. host->timing = ios->timing;
  3115. if (sdhci_preset_needed(host, ios->timing))
  3116. {
  3117. rt_uint16_t preset;
  3118. sdhci_preset_value_enable(host, RT_TRUE);
  3119. preset = sdhci_get_preset_value(host);
  3120. ios->drv_type = RT_FIELD_GET(RT_SDHCI_PRESET_DRV_MASK, preset);
  3121. host->drv_type = ios->drv_type;
  3122. }
  3123. host->ops->set_clock(host, host->clock);
  3124. }
  3125. else
  3126. {
  3127. rt_sdhci_writeb(host, ctrl, RT_SDHCI_HOST_CONTROL);
  3128. }
  3129. }
  3130. void rt_sdhci_free_host(struct rt_sdhci_host *host)
  3131. {
  3132. rt_sdhci_cleanup_host(host);
  3133. rt_free(host);
  3134. }