sdhci.c 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349
  1. /*
  2. * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
  3. *
  4. * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * Thanks to the following companies for their support:
  12. *
  13. * - JMicron (hardware and technical support)
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/ktime.h>
  17. #include <linux/highmem.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/slab.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/sizes.h>
  24. #include <linux/swiotlb.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/of.h>
  28. #include <linux/leds.h>
  29. #include <linux/mmc/mmc.h>
  30. #include <linux/mmc/host.h>
  31. #include <linux/mmc/card.h>
  32. #include <linux/mmc/sdio.h>
  33. #include <linux/mmc/slot-gpio.h>
  34. #include "sdhci.h"
  35. #define DRIVER_NAME "sdhci"
  36. #define DBG(f, x...) \
  37. pr_debug("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
  38. #define SDHCI_DUMP(f, x...) \
  39. pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
  40. #define MAX_TUNING_LOOP 40
  41. static unsigned int debug_quirks = 0;
  42. static unsigned int debug_quirks2;
  43. static void sdhci_finish_data(struct sdhci_host *);
  44. static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
  45. void sdhci_dumpregs(struct sdhci_host *host)
  46. {
  47. SDHCI_DUMP("============ SDHCI REGISTER DUMP ===========\n");
  48. SDHCI_DUMP("Sys addr: 0x%08x | Version: 0x%08x\n",
  49. sdhci_readl(host, SDHCI_DMA_ADDRESS),
  50. sdhci_readw(host, SDHCI_HOST_VERSION));
  51. SDHCI_DUMP("Blk size: 0x%08x | Blk cnt: 0x%08x\n",
  52. sdhci_readw(host, SDHCI_BLOCK_SIZE),
  53. sdhci_readw(host, SDHCI_BLOCK_COUNT));
  54. SDHCI_DUMP("Argument: 0x%08x | Trn mode: 0x%08x\n",
  55. sdhci_readl(host, SDHCI_ARGUMENT),
  56. sdhci_readw(host, SDHCI_TRANSFER_MODE));
  57. SDHCI_DUMP("Present: 0x%08x | Host ctl: 0x%08x\n",
  58. sdhci_readl(host, SDHCI_PRESENT_STATE),
  59. sdhci_readb(host, SDHCI_HOST_CONTROL));
  60. SDHCI_DUMP("Power: 0x%08x | Blk gap: 0x%08x\n",
  61. sdhci_readb(host, SDHCI_POWER_CONTROL),
  62. sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
  63. SDHCI_DUMP("Wake-up: 0x%08x | Clock: 0x%08x\n",
  64. sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
  65. sdhci_readw(host, SDHCI_CLOCK_CONTROL));
  66. SDHCI_DUMP("Timeout: 0x%08x | Int stat: 0x%08x\n",
  67. sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
  68. sdhci_readl(host, SDHCI_INT_STATUS));
  69. SDHCI_DUMP("Int enab: 0x%08x | Sig enab: 0x%08x\n",
  70. sdhci_readl(host, SDHCI_INT_ENABLE),
  71. sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
  72. SDHCI_DUMP("AC12 err: 0x%08x | Slot int: 0x%08x\n",
  73. sdhci_readw(host, SDHCI_ACMD12_ERR),
  74. sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
  75. SDHCI_DUMP("Caps: 0x%08x | Caps_1: 0x%08x\n",
  76. sdhci_readl(host, SDHCI_CAPABILITIES),
  77. sdhci_readl(host, SDHCI_CAPABILITIES_1));
  78. SDHCI_DUMP("Cmd: 0x%08x | Max curr: 0x%08x\n",
  79. sdhci_readw(host, SDHCI_COMMAND),
  80. sdhci_readl(host, SDHCI_MAX_CURRENT));
  81. SDHCI_DUMP("Resp[0]: 0x%08x | Resp[1]: 0x%08x\n",
  82. sdhci_readl(host, SDHCI_RESPONSE),
  83. sdhci_readl(host, SDHCI_RESPONSE + 4));
  84. SDHCI_DUMP("Resp[2]: 0x%08x | Resp[3]: 0x%08x\n",
  85. sdhci_readl(host, SDHCI_RESPONSE + 8),
  86. sdhci_readl(host, SDHCI_RESPONSE + 12));
  87. SDHCI_DUMP("Host ctl2: 0x%08x\n",
  88. sdhci_readw(host, SDHCI_HOST_CONTROL2));
  89. if (host->flags & SDHCI_USE_ADMA) {
  90. if (host->flags & SDHCI_USE_64_BIT_DMA) {
  91. SDHCI_DUMP("ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
  92. sdhci_readl(host, SDHCI_ADMA_ERROR),
  93. sdhci_readl(host, SDHCI_ADMA_ADDRESS_HI),
  94. sdhci_readl(host, SDHCI_ADMA_ADDRESS));
  95. } else {
  96. SDHCI_DUMP("ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
  97. sdhci_readl(host, SDHCI_ADMA_ERROR),
  98. sdhci_readl(host, SDHCI_ADMA_ADDRESS));
  99. }
  100. }
  101. SDHCI_DUMP("============================================\n");
  102. }
  103. EXPORT_SYMBOL_GPL(sdhci_dumpregs);
  104. /*****************************************************************************\
  105. * *
  106. * Low level functions *
  107. * *
  108. \*****************************************************************************/
  109. static void sdhci_do_enable_v4_mode(struct sdhci_host *host)
  110. {
  111. u16 ctrl2;
  112. ctrl2 = sdhci_readb(host, SDHCI_HOST_CONTROL2);
  113. if (ctrl2 & SDHCI_CTRL_V4_MODE)
  114. return;
  115. ctrl2 |= SDHCI_CTRL_V4_MODE;
  116. sdhci_writeb(host, ctrl2, SDHCI_HOST_CONTROL);
  117. }
  118. /*
  119. * This can be called before sdhci_add_host() by Vendor's host controller
  120. * driver to enable v4 mode if supported.
  121. */
  122. void sdhci_enable_v4_mode(struct sdhci_host *host)
  123. {
  124. host->v4_mode = true;
  125. sdhci_do_enable_v4_mode(host);
  126. }
  127. EXPORT_SYMBOL_GPL(sdhci_enable_v4_mode);
  128. static inline bool sdhci_data_line_cmd(struct mmc_command *cmd)
  129. {
  130. return cmd->data || cmd->flags & MMC_RSP_BUSY;
  131. }
  132. static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
  133. {
  134. u32 present;
  135. if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
  136. !mmc_card_is_removable(host->mmc))
  137. return;
  138. if (enable) {
  139. present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  140. SDHCI_CARD_PRESENT;
  141. host->ier |= present ? SDHCI_INT_CARD_REMOVE :
  142. SDHCI_INT_CARD_INSERT;
  143. } else {
  144. host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
  145. }
  146. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  147. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  148. }
  149. static void sdhci_enable_card_detection(struct sdhci_host *host)
  150. {
  151. sdhci_set_card_detection(host, true);
  152. }
  153. static void sdhci_disable_card_detection(struct sdhci_host *host)
  154. {
  155. sdhci_set_card_detection(host, false);
  156. }
  157. static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
  158. {
  159. if (host->bus_on)
  160. return;
  161. host->bus_on = true;
  162. pm_runtime_get_noresume(host->mmc->parent);
  163. }
  164. static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
  165. {
  166. if (!host->bus_on)
  167. return;
  168. host->bus_on = false;
  169. pm_runtime_put_noidle(host->mmc->parent);
  170. }
  171. void sdhci_reset(struct sdhci_host *host, u8 mask)
  172. {
  173. ktime_t timeout;
  174. sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
  175. if (mask & SDHCI_RESET_ALL) {
  176. host->clock = 0;
  177. /* Reset-all turns off SD Bus Power */
  178. if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
  179. sdhci_runtime_pm_bus_off(host);
  180. }
  181. /* Wait max 100 ms */
  182. timeout = ktime_add_ms(ktime_get(), 100);
  183. /* hw clears the bit when it's done */
  184. while (1) {
  185. bool timedout = ktime_after(ktime_get(), timeout);
  186. if (!(sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask))
  187. break;
  188. if (timedout) {
  189. pr_err("%s: Reset 0x%x never completed.\n",
  190. mmc_hostname(host->mmc), (int)mask);
  191. sdhci_dumpregs(host);
  192. return;
  193. }
  194. udelay(10);
  195. }
  196. }
  197. EXPORT_SYMBOL_GPL(sdhci_reset);
  198. static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
  199. {
  200. if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
  201. struct mmc_host *mmc = host->mmc;
  202. if (!mmc->ops->get_cd(mmc))
  203. return;
  204. }
  205. host->ops->reset(host, mask);
  206. if (mask & SDHCI_RESET_ALL) {
  207. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  208. if (host->ops->enable_dma)
  209. host->ops->enable_dma(host);
  210. }
  211. /* Resetting the controller clears many */
  212. host->preset_enabled = false;
  213. }
  214. }
  215. static void sdhci_set_default_irqs(struct sdhci_host *host)
  216. {
  217. host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
  218. SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
  219. SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
  220. SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
  221. SDHCI_INT_RESPONSE;
  222. if (host->tuning_mode == SDHCI_TUNING_MODE_2 ||
  223. host->tuning_mode == SDHCI_TUNING_MODE_3)
  224. host->ier |= SDHCI_INT_RETUNE;
  225. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  226. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  227. }
  228. static void sdhci_config_dma(struct sdhci_host *host)
  229. {
  230. u8 ctrl;
  231. u16 ctrl2;
  232. if (host->version < SDHCI_SPEC_200)
  233. return;
  234. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  235. /*
  236. * Always adjust the DMA selection as some controllers
  237. * (e.g. JMicron) can't do PIO properly when the selection
  238. * is ADMA.
  239. */
  240. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  241. if (!(host->flags & SDHCI_REQ_USE_DMA))
  242. goto out;
  243. /* Note if DMA Select is zero then SDMA is selected */
  244. if (host->flags & SDHCI_USE_ADMA)
  245. ctrl |= SDHCI_CTRL_ADMA32;
  246. if (host->flags & SDHCI_USE_64_BIT_DMA) {
  247. /*
  248. * If v4 mode, all supported DMA can be 64-bit addressing if
  249. * controller supports 64-bit system address, otherwise only
  250. * ADMA can support 64-bit addressing.
  251. */
  252. if (host->v4_mode) {
  253. ctrl2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  254. ctrl2 |= SDHCI_CTRL_64BIT_ADDR;
  255. sdhci_writew(host, ctrl2, SDHCI_HOST_CONTROL2);
  256. } else if (host->flags & SDHCI_USE_ADMA) {
  257. /*
  258. * Don't need to undo SDHCI_CTRL_ADMA32 in order to
  259. * set SDHCI_CTRL_ADMA64.
  260. */
  261. ctrl |= SDHCI_CTRL_ADMA64;
  262. }
  263. }
  264. out:
  265. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  266. }
  267. static void sdhci_init(struct sdhci_host *host, int soft)
  268. {
  269. struct mmc_host *mmc = host->mmc;
  270. if (soft)
  271. sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  272. else
  273. sdhci_do_reset(host, SDHCI_RESET_ALL);
  274. if (host->v4_mode)
  275. sdhci_do_enable_v4_mode(host);
  276. sdhci_set_default_irqs(host);
  277. host->cqe_on = false;
  278. if (soft) {
  279. /* force clock reconfiguration */
  280. host->clock = 0;
  281. mmc->ops->set_ios(mmc, &mmc->ios);
  282. }
  283. }
  284. static void sdhci_reinit(struct sdhci_host *host)
  285. {
  286. sdhci_init(host, 0);
  287. sdhci_enable_card_detection(host);
  288. }
  289. static void __sdhci_led_activate(struct sdhci_host *host)
  290. {
  291. u8 ctrl;
  292. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  293. ctrl |= SDHCI_CTRL_LED;
  294. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  295. }
  296. static void __sdhci_led_deactivate(struct sdhci_host *host)
  297. {
  298. u8 ctrl;
  299. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  300. ctrl &= ~SDHCI_CTRL_LED;
  301. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  302. }
  303. #if IS_REACHABLE(CONFIG_LEDS_CLASS)
  304. static void sdhci_led_control(struct led_classdev *led,
  305. enum led_brightness brightness)
  306. {
  307. struct sdhci_host *host = container_of(led, struct sdhci_host, led);
  308. unsigned long flags;
  309. spin_lock_irqsave(&host->lock, flags);
  310. if (host->runtime_suspended)
  311. goto out;
  312. if (brightness == LED_OFF)
  313. __sdhci_led_deactivate(host);
  314. else
  315. __sdhci_led_activate(host);
  316. out:
  317. spin_unlock_irqrestore(&host->lock, flags);
  318. }
  319. static int sdhci_led_register(struct sdhci_host *host)
  320. {
  321. struct mmc_host *mmc = host->mmc;
  322. snprintf(host->led_name, sizeof(host->led_name),
  323. "%s::", mmc_hostname(mmc));
  324. host->led.name = host->led_name;
  325. host->led.brightness = LED_OFF;
  326. host->led.default_trigger = mmc_hostname(mmc);
  327. host->led.brightness_set = sdhci_led_control;
  328. return led_classdev_register(mmc_dev(mmc), &host->led);
  329. }
  330. static void sdhci_led_unregister(struct sdhci_host *host)
  331. {
  332. led_classdev_unregister(&host->led);
  333. }
  334. static inline void sdhci_led_activate(struct sdhci_host *host)
  335. {
  336. }
  337. static inline void sdhci_led_deactivate(struct sdhci_host *host)
  338. {
  339. }
  340. #else
  341. static inline int sdhci_led_register(struct sdhci_host *host)
  342. {
  343. return 0;
  344. }
  345. static inline void sdhci_led_unregister(struct sdhci_host *host)
  346. {
  347. }
  348. static inline void sdhci_led_activate(struct sdhci_host *host)
  349. {
  350. __sdhci_led_activate(host);
  351. }
  352. static inline void sdhci_led_deactivate(struct sdhci_host *host)
  353. {
  354. __sdhci_led_deactivate(host);
  355. }
  356. #endif
  357. /*****************************************************************************\
  358. * *
  359. * Core functions *
  360. * *
  361. \*****************************************************************************/
  362. static void sdhci_read_block_pio(struct sdhci_host *host)
  363. {
  364. unsigned long flags;
  365. size_t blksize, len, chunk;
  366. u32 uninitialized_var(scratch);
  367. u8 *buf;
  368. DBG("PIO reading\n");
  369. blksize = host->data->blksz;
  370. chunk = 0;
  371. local_irq_save(flags);
  372. while (blksize) {
  373. BUG_ON(!sg_miter_next(&host->sg_miter));
  374. len = min(host->sg_miter.length, blksize);
  375. blksize -= len;
  376. host->sg_miter.consumed = len;
  377. buf = host->sg_miter.addr;
  378. while (len) {
  379. if (chunk == 0) {
  380. scratch = sdhci_readl(host, SDHCI_BUFFER);
  381. chunk = 4;
  382. }
  383. *buf = scratch & 0xFF;
  384. buf++;
  385. scratch >>= 8;
  386. chunk--;
  387. len--;
  388. }
  389. }
  390. sg_miter_stop(&host->sg_miter);
  391. local_irq_restore(flags);
  392. }
  393. static void sdhci_write_block_pio(struct sdhci_host *host)
  394. {
  395. unsigned long flags;
  396. size_t blksize, len, chunk;
  397. u32 scratch;
  398. u8 *buf;
  399. DBG("PIO writing\n");
  400. blksize = host->data->blksz;
  401. chunk = 0;
  402. scratch = 0;
  403. local_irq_save(flags);
  404. while (blksize) {
  405. BUG_ON(!sg_miter_next(&host->sg_miter));
  406. len = min(host->sg_miter.length, blksize);
  407. blksize -= len;
  408. host->sg_miter.consumed = len;
  409. buf = host->sg_miter.addr;
  410. while (len) {
  411. scratch |= (u32)*buf << (chunk * 8);
  412. buf++;
  413. chunk++;
  414. len--;
  415. if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
  416. sdhci_writel(host, scratch, SDHCI_BUFFER);
  417. chunk = 0;
  418. scratch = 0;
  419. }
  420. }
  421. }
  422. sg_miter_stop(&host->sg_miter);
  423. local_irq_restore(flags);
  424. }
  425. static void sdhci_transfer_pio(struct sdhci_host *host)
  426. {
  427. u32 mask;
  428. if (host->blocks == 0)
  429. return;
  430. if (host->data->flags & MMC_DATA_READ)
  431. mask = SDHCI_DATA_AVAILABLE;
  432. else
  433. mask = SDHCI_SPACE_AVAILABLE;
  434. /*
  435. * Some controllers (JMicron JMB38x) mess up the buffer bits
  436. * for transfers < 4 bytes. As long as it is just one block,
  437. * we can ignore the bits.
  438. */
  439. if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
  440. (host->data->blocks == 1))
  441. mask = ~0;
  442. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  443. if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
  444. udelay(100);
  445. if (host->data->flags & MMC_DATA_READ)
  446. sdhci_read_block_pio(host);
  447. else
  448. sdhci_write_block_pio(host);
  449. host->blocks--;
  450. if (host->blocks == 0)
  451. break;
  452. }
  453. DBG("PIO transfer complete.\n");
  454. }
  455. static int sdhci_pre_dma_transfer(struct sdhci_host *host,
  456. struct mmc_data *data, int cookie)
  457. {
  458. int sg_count;
  459. /*
  460. * If the data buffers are already mapped, return the previous
  461. * dma_map_sg() result.
  462. */
  463. if (data->host_cookie == COOKIE_PRE_MAPPED)
  464. return data->sg_count;
  465. /* Bounce write requests to the bounce buffer */
  466. if (host->bounce_buffer) {
  467. unsigned int length = data->blksz * data->blocks;
  468. if (length > host->bounce_buffer_size) {
  469. pr_err("%s: asked for transfer of %u bytes exceeds bounce buffer %u bytes\n",
  470. mmc_hostname(host->mmc), length,
  471. host->bounce_buffer_size);
  472. return -EIO;
  473. }
  474. if (mmc_get_dma_dir(data) == DMA_TO_DEVICE) {
  475. /* Copy the data to the bounce buffer */
  476. sg_copy_to_buffer(data->sg, data->sg_len,
  477. host->bounce_buffer,
  478. length);
  479. }
  480. /* Switch ownership to the DMA */
  481. dma_sync_single_for_device(host->mmc->parent,
  482. host->bounce_addr,
  483. host->bounce_buffer_size,
  484. mmc_get_dma_dir(data));
  485. /* Just a dummy value */
  486. sg_count = 1;
  487. } else {
  488. /* Just access the data directly from memory */
  489. sg_count = dma_map_sg(mmc_dev(host->mmc),
  490. data->sg, data->sg_len,
  491. mmc_get_dma_dir(data));
  492. }
  493. if (sg_count == 0)
  494. return -ENOSPC;
  495. data->sg_count = sg_count;
  496. data->host_cookie = cookie;
  497. return sg_count;
  498. }
  499. static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
  500. {
  501. local_irq_save(*flags);
  502. return kmap_atomic(sg_page(sg)) + sg->offset;
  503. }
  504. static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
  505. {
  506. kunmap_atomic(buffer);
  507. local_irq_restore(*flags);
  508. }
  509. void sdhci_adma_write_desc(struct sdhci_host *host, void **desc,
  510. dma_addr_t addr, int len, unsigned int cmd)
  511. {
  512. struct sdhci_adma2_64_desc *dma_desc = *desc;
  513. /* 32-bit and 64-bit descriptors have these members in same position */
  514. dma_desc->cmd = cpu_to_le16(cmd);
  515. dma_desc->len = cpu_to_le16(len);
  516. dma_desc->addr_lo = cpu_to_le32((u32)addr);
  517. if (host->flags & SDHCI_USE_64_BIT_DMA)
  518. dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
  519. *desc += host->desc_sz;
  520. }
  521. EXPORT_SYMBOL_GPL(sdhci_adma_write_desc);
  522. static inline void __sdhci_adma_write_desc(struct sdhci_host *host,
  523. void **desc, dma_addr_t addr,
  524. int len, unsigned int cmd)
  525. {
  526. if (host->ops->adma_write_desc)
  527. host->ops->adma_write_desc(host, desc, addr, len, cmd);
  528. else
  529. sdhci_adma_write_desc(host, desc, addr, len, cmd);
  530. }
  531. static void sdhci_adma_mark_end(void *desc)
  532. {
  533. struct sdhci_adma2_64_desc *dma_desc = desc;
  534. /* 32-bit and 64-bit descriptors have 'cmd' in same position */
  535. dma_desc->cmd |= cpu_to_le16(ADMA2_END);
  536. }
  537. static void sdhci_adma_table_pre(struct sdhci_host *host,
  538. struct mmc_data *data, int sg_count)
  539. {
  540. struct scatterlist *sg;
  541. unsigned long flags;
  542. dma_addr_t addr, align_addr;
  543. void *desc, *align;
  544. char *buffer;
  545. int len, offset, i;
  546. /*
  547. * The spec does not specify endianness of descriptor table.
  548. * We currently guess that it is LE.
  549. */
  550. host->sg_count = sg_count;
  551. desc = host->adma_table;
  552. align = host->align_buffer;
  553. align_addr = host->align_addr;
  554. for_each_sg(data->sg, sg, host->sg_count, i) {
  555. addr = sg_dma_address(sg);
  556. len = sg_dma_len(sg);
  557. /*
  558. * The SDHCI specification states that ADMA addresses must
  559. * be 32-bit aligned. If they aren't, then we use a bounce
  560. * buffer for the (up to three) bytes that screw up the
  561. * alignment.
  562. */
  563. offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
  564. SDHCI_ADMA2_MASK;
  565. if (offset) {
  566. if (data->flags & MMC_DATA_WRITE) {
  567. buffer = sdhci_kmap_atomic(sg, &flags);
  568. memcpy(align, buffer, offset);
  569. sdhci_kunmap_atomic(buffer, &flags);
  570. }
  571. /* tran, valid */
  572. __sdhci_adma_write_desc(host, &desc, align_addr,
  573. offset, ADMA2_TRAN_VALID);
  574. BUG_ON(offset > 65536);
  575. align += SDHCI_ADMA2_ALIGN;
  576. align_addr += SDHCI_ADMA2_ALIGN;
  577. addr += offset;
  578. len -= offset;
  579. }
  580. BUG_ON(len > 65536);
  581. /* tran, valid */
  582. if (len)
  583. __sdhci_adma_write_desc(host, &desc, addr, len,
  584. ADMA2_TRAN_VALID);
  585. /*
  586. * If this triggers then we have a calculation bug
  587. * somewhere. :/
  588. */
  589. WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
  590. }
  591. if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
  592. /* Mark the last descriptor as the terminating descriptor */
  593. if (desc != host->adma_table) {
  594. desc -= host->desc_sz;
  595. sdhci_adma_mark_end(desc);
  596. }
  597. } else {
  598. /* Add a terminating entry - nop, end, valid */
  599. __sdhci_adma_write_desc(host, &desc, 0, 0, ADMA2_NOP_END_VALID);
  600. }
  601. }
  602. static void sdhci_adma_table_post(struct sdhci_host *host,
  603. struct mmc_data *data)
  604. {
  605. struct scatterlist *sg;
  606. int i, size;
  607. void *align;
  608. char *buffer;
  609. unsigned long flags;
  610. if (data->flags & MMC_DATA_READ) {
  611. bool has_unaligned = false;
  612. /* Do a quick scan of the SG list for any unaligned mappings */
  613. for_each_sg(data->sg, sg, host->sg_count, i)
  614. if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
  615. has_unaligned = true;
  616. break;
  617. }
  618. if (has_unaligned) {
  619. dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
  620. data->sg_len, DMA_FROM_DEVICE);
  621. align = host->align_buffer;
  622. for_each_sg(data->sg, sg, host->sg_count, i) {
  623. if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
  624. size = SDHCI_ADMA2_ALIGN -
  625. (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
  626. buffer = sdhci_kmap_atomic(sg, &flags);
  627. memcpy(buffer, align, size);
  628. sdhci_kunmap_atomic(buffer, &flags);
  629. align += SDHCI_ADMA2_ALIGN;
  630. }
  631. }
  632. }
  633. }
  634. }
  635. static dma_addr_t sdhci_sdma_address(struct sdhci_host *host)
  636. {
  637. if (host->bounce_buffer)
  638. return host->bounce_addr;
  639. else
  640. return sg_dma_address(host->data->sg);
  641. }
  642. static void sdhci_set_sdma_addr(struct sdhci_host *host, dma_addr_t addr)
  643. {
  644. if (host->v4_mode) {
  645. sdhci_writel(host, addr, SDHCI_ADMA_ADDRESS);
  646. if (host->flags & SDHCI_USE_64_BIT_DMA)
  647. sdhci_writel(host, (u64)addr >> 32, SDHCI_ADMA_ADDRESS_HI);
  648. } else {
  649. sdhci_writel(host, addr, SDHCI_DMA_ADDRESS);
  650. }
  651. }
  652. static unsigned int sdhci_target_timeout(struct sdhci_host *host,
  653. struct mmc_command *cmd,
  654. struct mmc_data *data)
  655. {
  656. unsigned int target_timeout;
  657. /* timeout in us */
  658. if (!data) {
  659. target_timeout = cmd->busy_timeout * 1000;
  660. } else {
  661. target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
  662. if (host->clock && data->timeout_clks) {
  663. unsigned long long val;
  664. /*
  665. * data->timeout_clks is in units of clock cycles.
  666. * host->clock is in Hz. target_timeout is in us.
  667. * Hence, us = 1000000 * cycles / Hz. Round up.
  668. */
  669. val = 1000000ULL * data->timeout_clks;
  670. if (do_div(val, host->clock))
  671. target_timeout++;
  672. target_timeout += val;
  673. }
  674. }
  675. return target_timeout;
  676. }
  677. static void sdhci_calc_sw_timeout(struct sdhci_host *host,
  678. struct mmc_command *cmd)
  679. {
  680. struct mmc_data *data = cmd->data;
  681. struct mmc_host *mmc = host->mmc;
  682. struct mmc_ios *ios = &mmc->ios;
  683. unsigned char bus_width = 1 << ios->bus_width;
  684. unsigned int blksz;
  685. unsigned int freq;
  686. u64 target_timeout;
  687. u64 transfer_time;
  688. target_timeout = sdhci_target_timeout(host, cmd, data);
  689. target_timeout *= NSEC_PER_USEC;
  690. if (data) {
  691. blksz = data->blksz;
  692. freq = host->mmc->actual_clock ? : host->clock;
  693. transfer_time = (u64)blksz * NSEC_PER_SEC * (8 / bus_width);
  694. do_div(transfer_time, freq);
  695. /* multiply by '2' to account for any unknowns */
  696. transfer_time = transfer_time * 2;
  697. /* calculate timeout for the entire data */
  698. host->data_timeout = data->blocks * target_timeout +
  699. transfer_time;
  700. } else {
  701. host->data_timeout = target_timeout;
  702. }
  703. if (host->data_timeout)
  704. host->data_timeout += MMC_CMD_TRANSFER_TIME;
  705. }
  706. static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd,
  707. bool *too_big)
  708. {
  709. u8 count;
  710. struct mmc_data *data = cmd->data;
  711. unsigned target_timeout, current_timeout;
  712. *too_big = true;
  713. /*
  714. * If the host controller provides us with an incorrect timeout
  715. * value, just skip the check and use 0xE. The hardware may take
  716. * longer to time out, but that's much better than having a too-short
  717. * timeout value.
  718. */
  719. if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
  720. return 0xE;
  721. /* Unspecified timeout, assume max */
  722. if (!data && !cmd->busy_timeout)
  723. return 0xE;
  724. /* timeout in us */
  725. target_timeout = sdhci_target_timeout(host, cmd, data);
  726. /*
  727. * Figure out needed cycles.
  728. * We do this in steps in order to fit inside a 32 bit int.
  729. * The first step is the minimum timeout, which will have a
  730. * minimum resolution of 6 bits:
  731. * (1) 2^13*1000 > 2^22,
  732. * (2) host->timeout_clk < 2^16
  733. * =>
  734. * (1) / (2) > 2^6
  735. */
  736. count = 0;
  737. current_timeout = (1 << 13) * 1000 / host->timeout_clk;
  738. while (current_timeout < target_timeout) {
  739. count++;
  740. current_timeout <<= 1;
  741. if (count >= 0xF)
  742. break;
  743. }
  744. if (count >= 0xF) {
  745. if (!(host->quirks2 & SDHCI_QUIRK2_DISABLE_HW_TIMEOUT))
  746. DBG("Too large timeout 0x%x requested for CMD%d!\n",
  747. count, cmd->opcode);
  748. count = 0xE;
  749. } else {
  750. *too_big = false;
  751. }
  752. return count;
  753. }
  754. static void sdhci_set_transfer_irqs(struct sdhci_host *host)
  755. {
  756. u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  757. u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  758. if (host->flags & SDHCI_REQ_USE_DMA)
  759. host->ier = (host->ier & ~pio_irqs) | dma_irqs;
  760. else
  761. host->ier = (host->ier & ~dma_irqs) | pio_irqs;
  762. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  763. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  764. }
  765. static void sdhci_set_data_timeout_irq(struct sdhci_host *host, bool enable)
  766. {
  767. if (enable)
  768. host->ier |= SDHCI_INT_DATA_TIMEOUT;
  769. else
  770. host->ier &= ~SDHCI_INT_DATA_TIMEOUT;
  771. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  772. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  773. }
  774. static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
  775. {
  776. u8 count;
  777. if (host->ops->set_timeout) {
  778. host->ops->set_timeout(host, cmd);
  779. } else {
  780. bool too_big = false;
  781. count = sdhci_calc_timeout(host, cmd, &too_big);
  782. if (too_big &&
  783. host->quirks2 & SDHCI_QUIRK2_DISABLE_HW_TIMEOUT) {
  784. sdhci_calc_sw_timeout(host, cmd);
  785. sdhci_set_data_timeout_irq(host, false);
  786. } else if (!(host->ier & SDHCI_INT_DATA_TIMEOUT)) {
  787. sdhci_set_data_timeout_irq(host, true);
  788. }
  789. sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
  790. }
  791. }
  792. static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
  793. {
  794. struct mmc_data *data = cmd->data;
  795. host->data_timeout = 0;
  796. if (sdhci_data_line_cmd(cmd))
  797. sdhci_set_timeout(host, cmd);
  798. if (!data)
  799. return;
  800. WARN_ON(host->data);
  801. /* Sanity checks */
  802. BUG_ON(data->blksz * data->blocks > 524288);
  803. BUG_ON(data->blksz > host->mmc->max_blk_size);
  804. BUG_ON(data->blocks > 65535);
  805. host->data = data;
  806. host->data_early = 0;
  807. host->data->bytes_xfered = 0;
  808. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  809. struct scatterlist *sg;
  810. unsigned int length_mask, offset_mask;
  811. int i;
  812. host->flags |= SDHCI_REQ_USE_DMA;
  813. /*
  814. * FIXME: This doesn't account for merging when mapping the
  815. * scatterlist.
  816. *
  817. * The assumption here being that alignment and lengths are
  818. * the same after DMA mapping to device address space.
  819. */
  820. length_mask = 0;
  821. offset_mask = 0;
  822. if (host->flags & SDHCI_USE_ADMA) {
  823. if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
  824. length_mask = 3;
  825. /*
  826. * As we use up to 3 byte chunks to work
  827. * around alignment problems, we need to
  828. * check the offset as well.
  829. */
  830. offset_mask = 3;
  831. }
  832. } else {
  833. if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
  834. length_mask = 3;
  835. if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
  836. offset_mask = 3;
  837. }
  838. if (unlikely(length_mask | offset_mask)) {
  839. for_each_sg(data->sg, sg, data->sg_len, i) {
  840. if (sg->length & length_mask) {
  841. DBG("Reverting to PIO because of transfer size (%d)\n",
  842. sg->length);
  843. host->flags &= ~SDHCI_REQ_USE_DMA;
  844. break;
  845. }
  846. if (sg->offset & offset_mask) {
  847. DBG("Reverting to PIO because of bad alignment\n");
  848. host->flags &= ~SDHCI_REQ_USE_DMA;
  849. break;
  850. }
  851. }
  852. }
  853. }
  854. if (host->flags & SDHCI_REQ_USE_DMA) {
  855. int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
  856. if (sg_cnt <= 0) {
  857. /*
  858. * This only happens when someone fed
  859. * us an invalid request.
  860. */
  861. WARN_ON(1);
  862. host->flags &= ~SDHCI_REQ_USE_DMA;
  863. } else if (host->flags & SDHCI_USE_ADMA) {
  864. sdhci_adma_table_pre(host, data, sg_cnt);
  865. sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
  866. if (host->flags & SDHCI_USE_64_BIT_DMA)
  867. sdhci_writel(host,
  868. (u64)host->adma_addr >> 32,
  869. SDHCI_ADMA_ADDRESS_HI);
  870. } else {
  871. WARN_ON(sg_cnt != 1);
  872. sdhci_set_sdma_addr(host, sdhci_sdma_address(host));
  873. }
  874. }
  875. sdhci_config_dma(host);
  876. if (!(host->flags & SDHCI_REQ_USE_DMA)) {
  877. int flags;
  878. flags = SG_MITER_ATOMIC;
  879. if (host->data->flags & MMC_DATA_READ)
  880. flags |= SG_MITER_TO_SG;
  881. else
  882. flags |= SG_MITER_FROM_SG;
  883. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  884. host->blocks = data->blocks;
  885. }
  886. sdhci_set_transfer_irqs(host);
  887. /* Set the DMA boundary value and block size */
  888. sdhci_writew(host, SDHCI_MAKE_BLKSZ(host->sdma_boundary, data->blksz),
  889. SDHCI_BLOCK_SIZE);
  890. /*
  891. * For Version 4.10 onwards, if v4 mode is enabled, 32-bit Block Count
  892. * can be supported, in that case 16-bit block count register must be 0.
  893. */
  894. if (host->version >= SDHCI_SPEC_410 && host->v4_mode &&
  895. (host->quirks2 & SDHCI_QUIRK2_USE_32BIT_BLK_CNT)) {
  896. if (sdhci_readw(host, SDHCI_BLOCK_COUNT))
  897. sdhci_writew(host, 0, SDHCI_BLOCK_COUNT);
  898. sdhci_writew(host, data->blocks, SDHCI_32BIT_BLK_CNT);
  899. } else {
  900. sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  901. }
  902. }
  903. static inline bool sdhci_auto_cmd12(struct sdhci_host *host,
  904. struct mmc_request *mrq)
  905. {
  906. return !mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
  907. !mrq->cap_cmd_during_tfr;
  908. }
  909. static inline void sdhci_auto_cmd_select(struct sdhci_host *host,
  910. struct mmc_command *cmd,
  911. u16 *mode)
  912. {
  913. bool use_cmd12 = sdhci_auto_cmd12(host, cmd->mrq) &&
  914. (cmd->opcode != SD_IO_RW_EXTENDED);
  915. bool use_cmd23 = cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23);
  916. u16 ctrl2;
  917. /*
  918. * In case of Version 4.10 or later, use of 'Auto CMD Auto
  919. * Select' is recommended rather than use of 'Auto CMD12
  920. * Enable' or 'Auto CMD23 Enable'.
  921. */
  922. if (host->version >= SDHCI_SPEC_410 && (use_cmd12 || use_cmd23)) {
  923. *mode |= SDHCI_TRNS_AUTO_SEL;
  924. ctrl2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  925. if (use_cmd23)
  926. ctrl2 |= SDHCI_CMD23_ENABLE;
  927. else
  928. ctrl2 &= ~SDHCI_CMD23_ENABLE;
  929. sdhci_writew(host, ctrl2, SDHCI_HOST_CONTROL2);
  930. return;
  931. }
  932. /*
  933. * If we are sending CMD23, CMD12 never gets sent
  934. * on successful completion (so no Auto-CMD12).
  935. */
  936. if (use_cmd12)
  937. *mode |= SDHCI_TRNS_AUTO_CMD12;
  938. else if (use_cmd23)
  939. *mode |= SDHCI_TRNS_AUTO_CMD23;
  940. }
  941. static void sdhci_set_transfer_mode(struct sdhci_host *host,
  942. struct mmc_command *cmd)
  943. {
  944. u16 mode = 0;
  945. struct mmc_data *data = cmd->data;
  946. if (data == NULL) {
  947. if (host->quirks2 &
  948. SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
  949. /* must not clear SDHCI_TRANSFER_MODE when tuning */
  950. if (cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200)
  951. sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
  952. } else {
  953. /* clear Auto CMD settings for no data CMDs */
  954. mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
  955. sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
  956. SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
  957. }
  958. return;
  959. }
  960. WARN_ON(!host->data);
  961. if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
  962. mode = SDHCI_TRNS_BLK_CNT_EN;
  963. if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
  964. mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
  965. sdhci_auto_cmd_select(host, cmd, &mode);
  966. if (cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23))
  967. sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2);
  968. }
  969. if (data->flags & MMC_DATA_READ)
  970. mode |= SDHCI_TRNS_READ;
  971. if (host->flags & SDHCI_REQ_USE_DMA)
  972. mode |= SDHCI_TRNS_DMA;
  973. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  974. }
  975. static bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq)
  976. {
  977. return (!(host->flags & SDHCI_DEVICE_DEAD) &&
  978. ((mrq->cmd && mrq->cmd->error) ||
  979. (mrq->sbc && mrq->sbc->error) ||
  980. (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
  981. (mrq->data->stop && mrq->data->stop->error))) ||
  982. (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)));
  983. }
  984. static void __sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
  985. {
  986. int i;
  987. for (i = 0; i < SDHCI_MAX_MRQS; i++) {
  988. if (host->mrqs_done[i] == mrq) {
  989. WARN_ON(1);
  990. return;
  991. }
  992. }
  993. for (i = 0; i < SDHCI_MAX_MRQS; i++) {
  994. if (!host->mrqs_done[i]) {
  995. host->mrqs_done[i] = mrq;
  996. break;
  997. }
  998. }
  999. WARN_ON(i >= SDHCI_MAX_MRQS);
  1000. tasklet_schedule(&host->finish_tasklet);
  1001. }
  1002. static void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
  1003. {
  1004. if (host->cmd && host->cmd->mrq == mrq)
  1005. host->cmd = NULL;
  1006. if (host->data_cmd && host->data_cmd->mrq == mrq)
  1007. host->data_cmd = NULL;
  1008. if (host->data && host->data->mrq == mrq)
  1009. host->data = NULL;
  1010. if (sdhci_needs_reset(host, mrq))
  1011. host->pending_reset = true;
  1012. __sdhci_finish_mrq(host, mrq);
  1013. }
  1014. static void sdhci_finish_data(struct sdhci_host *host)
  1015. {
  1016. struct mmc_command *data_cmd = host->data_cmd;
  1017. struct mmc_data *data = host->data;
  1018. host->data = NULL;
  1019. host->data_cmd = NULL;
  1020. if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
  1021. (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
  1022. sdhci_adma_table_post(host, data);
  1023. /*
  1024. * The specification states that the block count register must
  1025. * be updated, but it does not specify at what point in the
  1026. * data flow. That makes the register entirely useless to read
  1027. * back so we have to assume that nothing made it to the card
  1028. * in the event of an error.
  1029. */
  1030. if (data->error)
  1031. data->bytes_xfered = 0;
  1032. else
  1033. data->bytes_xfered = data->blksz * data->blocks;
  1034. /*
  1035. * Need to send CMD12 if -
  1036. * a) open-ended multiblock transfer (no CMD23)
  1037. * b) error in multiblock transfer
  1038. */
  1039. if (data->stop &&
  1040. (data->error ||
  1041. !data->mrq->sbc)) {
  1042. /*
  1043. * The controller needs a reset of internal state machines
  1044. * upon error conditions.
  1045. */
  1046. if (data->error) {
  1047. if (!host->cmd || host->cmd == data_cmd)
  1048. sdhci_do_reset(host, SDHCI_RESET_CMD);
  1049. sdhci_do_reset(host, SDHCI_RESET_DATA);
  1050. }
  1051. /*
  1052. * 'cap_cmd_during_tfr' request must not use the command line
  1053. * after mmc_command_done() has been called. It is upper layer's
  1054. * responsibility to send the stop command if required.
  1055. */
  1056. if (data->mrq->cap_cmd_during_tfr) {
  1057. sdhci_finish_mrq(host, data->mrq);
  1058. } else {
  1059. /* Avoid triggering warning in sdhci_send_command() */
  1060. host->cmd = NULL;
  1061. sdhci_send_command(host, data->stop);
  1062. }
  1063. } else {
  1064. sdhci_finish_mrq(host, data->mrq);
  1065. }
  1066. }
  1067. static void sdhci_mod_timer(struct sdhci_host *host, struct mmc_request *mrq,
  1068. unsigned long timeout)
  1069. {
  1070. if (sdhci_data_line_cmd(mrq->cmd))
  1071. mod_timer(&host->data_timer, timeout);
  1072. else
  1073. mod_timer(&host->timer, timeout);
  1074. }
  1075. static void sdhci_del_timer(struct sdhci_host *host, struct mmc_request *mrq)
  1076. {
  1077. if (sdhci_data_line_cmd(mrq->cmd))
  1078. del_timer(&host->data_timer);
  1079. else
  1080. del_timer(&host->timer);
  1081. }
  1082. void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
  1083. {
  1084. int flags;
  1085. u32 mask;
  1086. unsigned long timeout;
  1087. WARN_ON(host->cmd);
  1088. /* Initially, a command has no error */
  1089. cmd->error = 0;
  1090. if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
  1091. cmd->opcode == MMC_STOP_TRANSMISSION)
  1092. cmd->flags |= MMC_RSP_BUSY;
  1093. /* Wait max 10 ms */
  1094. timeout = 10;
  1095. mask = SDHCI_CMD_INHIBIT;
  1096. if (sdhci_data_line_cmd(cmd))
  1097. mask |= SDHCI_DATA_INHIBIT;
  1098. /* We shouldn't wait for data inihibit for stop commands, even
  1099. though they might use busy signaling */
  1100. if (cmd->mrq->data && (cmd == cmd->mrq->data->stop))
  1101. mask &= ~SDHCI_DATA_INHIBIT;
  1102. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  1103. if (timeout == 0) {
  1104. pr_err("%s: Controller never released inhibit bit(s).\n",
  1105. mmc_hostname(host->mmc));
  1106. sdhci_dumpregs(host);
  1107. cmd->error = -EIO;
  1108. sdhci_finish_mrq(host, cmd->mrq);
  1109. return;
  1110. }
  1111. timeout--;
  1112. mdelay(1);
  1113. }
  1114. host->cmd = cmd;
  1115. if (sdhci_data_line_cmd(cmd)) {
  1116. WARN_ON(host->data_cmd);
  1117. host->data_cmd = cmd;
  1118. }
  1119. sdhci_prepare_data(host, cmd);
  1120. sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
  1121. sdhci_set_transfer_mode(host, cmd);
  1122. if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  1123. pr_err("%s: Unsupported response type!\n",
  1124. mmc_hostname(host->mmc));
  1125. cmd->error = -EINVAL;
  1126. sdhci_finish_mrq(host, cmd->mrq);
  1127. return;
  1128. }
  1129. if (!(cmd->flags & MMC_RSP_PRESENT))
  1130. flags = SDHCI_CMD_RESP_NONE;
  1131. else if (cmd->flags & MMC_RSP_136)
  1132. flags = SDHCI_CMD_RESP_LONG;
  1133. else if (cmd->flags & MMC_RSP_BUSY)
  1134. flags = SDHCI_CMD_RESP_SHORT_BUSY;
  1135. else
  1136. flags = SDHCI_CMD_RESP_SHORT;
  1137. if (cmd->flags & MMC_RSP_CRC)
  1138. flags |= SDHCI_CMD_CRC;
  1139. if (cmd->flags & MMC_RSP_OPCODE)
  1140. flags |= SDHCI_CMD_INDEX;
  1141. /* CMD19 is special in that the Data Present Select should be set */
  1142. if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
  1143. cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
  1144. flags |= SDHCI_CMD_DATA;
  1145. timeout = jiffies;
  1146. if (host->data_timeout)
  1147. timeout += nsecs_to_jiffies(host->data_timeout);
  1148. else if (!cmd->data && cmd->busy_timeout > 9000)
  1149. timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
  1150. else
  1151. timeout += 10 * HZ;
  1152. sdhci_mod_timer(host, cmd->mrq, timeout);
  1153. sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
  1154. }
  1155. EXPORT_SYMBOL_GPL(sdhci_send_command);
  1156. static void sdhci_read_rsp_136(struct sdhci_host *host, struct mmc_command *cmd)
  1157. {
  1158. int i, reg;
  1159. for (i = 0; i < 4; i++) {
  1160. reg = SDHCI_RESPONSE + (3 - i) * 4;
  1161. cmd->resp[i] = sdhci_readl(host, reg);
  1162. }
  1163. if (host->quirks2 & SDHCI_QUIRK2_RSP_136_HAS_CRC)
  1164. return;
  1165. /* CRC is stripped so we need to do some shifting */
  1166. for (i = 0; i < 4; i++) {
  1167. cmd->resp[i] <<= 8;
  1168. if (i != 3)
  1169. cmd->resp[i] |= cmd->resp[i + 1] >> 24;
  1170. }
  1171. }
  1172. static void sdhci_finish_command(struct sdhci_host *host)
  1173. {
  1174. struct mmc_command *cmd = host->cmd;
  1175. host->cmd = NULL;
  1176. if (cmd->flags & MMC_RSP_PRESENT) {
  1177. if (cmd->flags & MMC_RSP_136) {
  1178. sdhci_read_rsp_136(host, cmd);
  1179. } else {
  1180. cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
  1181. }
  1182. }
  1183. if (cmd->mrq->cap_cmd_during_tfr && cmd == cmd->mrq->cmd)
  1184. mmc_command_done(host->mmc, cmd->mrq);
  1185. /*
  1186. * The host can send and interrupt when the busy state has
  1187. * ended, allowing us to wait without wasting CPU cycles.
  1188. * The busy signal uses DAT0 so this is similar to waiting
  1189. * for data to complete.
  1190. *
  1191. * Note: The 1.0 specification is a bit ambiguous about this
  1192. * feature so there might be some problems with older
  1193. * controllers.
  1194. */
  1195. if (cmd->flags & MMC_RSP_BUSY) {
  1196. if (cmd->data) {
  1197. DBG("Cannot wait for busy signal when also doing a data transfer");
  1198. } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
  1199. cmd == host->data_cmd) {
  1200. /* Command complete before busy is ended */
  1201. return;
  1202. }
  1203. }
  1204. /* Finished CMD23, now send actual command. */
  1205. if (cmd == cmd->mrq->sbc) {
  1206. sdhci_send_command(host, cmd->mrq->cmd);
  1207. } else {
  1208. /* Processed actual command. */
  1209. if (host->data && host->data_early)
  1210. sdhci_finish_data(host);
  1211. if (!cmd->data)
  1212. sdhci_finish_mrq(host, cmd->mrq);
  1213. }
  1214. }
  1215. static u16 sdhci_get_preset_value(struct sdhci_host *host)
  1216. {
  1217. u16 preset = 0;
  1218. switch (host->timing) {
  1219. case MMC_TIMING_UHS_SDR12:
  1220. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
  1221. break;
  1222. case MMC_TIMING_UHS_SDR25:
  1223. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
  1224. break;
  1225. case MMC_TIMING_UHS_SDR50:
  1226. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
  1227. break;
  1228. case MMC_TIMING_UHS_SDR104:
  1229. case MMC_TIMING_MMC_HS200:
  1230. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
  1231. break;
  1232. case MMC_TIMING_UHS_DDR50:
  1233. case MMC_TIMING_MMC_DDR52:
  1234. preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
  1235. break;
  1236. case MMC_TIMING_MMC_HS400:
  1237. preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
  1238. break;
  1239. default:
  1240. pr_warn("%s: Invalid UHS-I mode selected\n",
  1241. mmc_hostname(host->mmc));
  1242. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
  1243. break;
  1244. }
  1245. return preset;
  1246. }
  1247. u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
  1248. unsigned int *actual_clock)
  1249. {
  1250. int div = 0; /* Initialized for compiler warning */
  1251. int real_div = div, clk_mul = 1;
  1252. u16 clk = 0;
  1253. bool switch_base_clk = false;
  1254. if (host->version >= SDHCI_SPEC_300) {
  1255. if (host->preset_enabled) {
  1256. u16 pre_val;
  1257. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1258. pre_val = sdhci_get_preset_value(host);
  1259. div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
  1260. >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
  1261. if (host->clk_mul &&
  1262. (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
  1263. clk = SDHCI_PROG_CLOCK_MODE;
  1264. real_div = div + 1;
  1265. clk_mul = host->clk_mul;
  1266. } else {
  1267. real_div = max_t(int, 1, div << 1);
  1268. }
  1269. goto clock_set;
  1270. }
  1271. /*
  1272. * Check if the Host Controller supports Programmable Clock
  1273. * Mode.
  1274. */
  1275. if (host->clk_mul) {
  1276. for (div = 1; div <= 1024; div++) {
  1277. if ((host->max_clk * host->clk_mul / div)
  1278. <= clock)
  1279. break;
  1280. }
  1281. if ((host->max_clk * host->clk_mul / div) <= clock) {
  1282. /*
  1283. * Set Programmable Clock Mode in the Clock
  1284. * Control register.
  1285. */
  1286. clk = SDHCI_PROG_CLOCK_MODE;
  1287. real_div = div;
  1288. clk_mul = host->clk_mul;
  1289. div--;
  1290. } else {
  1291. /*
  1292. * Divisor can be too small to reach clock
  1293. * speed requirement. Then use the base clock.
  1294. */
  1295. switch_base_clk = true;
  1296. }
  1297. }
  1298. if (!host->clk_mul || switch_base_clk) {
  1299. /* Version 3.00 divisors must be a multiple of 2. */
  1300. if (host->max_clk <= clock)
  1301. div = 1;
  1302. else {
  1303. for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
  1304. div += 2) {
  1305. if ((host->max_clk / div) <= clock)
  1306. break;
  1307. }
  1308. }
  1309. real_div = div;
  1310. div >>= 1;
  1311. if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
  1312. && !div && host->max_clk <= 25000000)
  1313. div = 1;
  1314. }
  1315. } else {
  1316. /* Version 2.00 divisors must be a power of 2. */
  1317. for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
  1318. if ((host->max_clk / div) <= clock)
  1319. break;
  1320. }
  1321. real_div = div;
  1322. div >>= 1;
  1323. }
  1324. clock_set:
  1325. if (real_div)
  1326. *actual_clock = (host->max_clk * clk_mul) / real_div;
  1327. clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
  1328. clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
  1329. << SDHCI_DIVIDER_HI_SHIFT;
  1330. return clk;
  1331. }
  1332. EXPORT_SYMBOL_GPL(sdhci_calc_clk);
  1333. void sdhci_enable_clk(struct sdhci_host *host, u16 clk)
  1334. {
  1335. ktime_t timeout;
  1336. clk |= SDHCI_CLOCK_INT_EN;
  1337. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1338. /* Wait max 20 ms */
  1339. timeout = ktime_add_ms(ktime_get(), 20);
  1340. while (1) {
  1341. bool timedout = ktime_after(ktime_get(), timeout);
  1342. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1343. if (clk & SDHCI_CLOCK_INT_STABLE)
  1344. break;
  1345. if (timedout) {
  1346. pr_err("%s: Internal clock never stabilised.\n",
  1347. mmc_hostname(host->mmc));
  1348. sdhci_dumpregs(host);
  1349. return;
  1350. }
  1351. udelay(10);
  1352. }
  1353. clk |= SDHCI_CLOCK_CARD_EN;
  1354. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1355. }
  1356. EXPORT_SYMBOL_GPL(sdhci_enable_clk);
  1357. void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
  1358. {
  1359. u16 clk;
  1360. host->mmc->actual_clock = 0;
  1361. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  1362. if (clock == 0)
  1363. return;
  1364. clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
  1365. sdhci_enable_clk(host, clk);
  1366. }
  1367. EXPORT_SYMBOL_GPL(sdhci_set_clock);
  1368. static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
  1369. unsigned short vdd)
  1370. {
  1371. struct mmc_host *mmc = host->mmc;
  1372. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
  1373. if (mode != MMC_POWER_OFF)
  1374. sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
  1375. else
  1376. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  1377. }
  1378. void sdhci_set_power_noreg(struct sdhci_host *host, unsigned char mode,
  1379. unsigned short vdd)
  1380. {
  1381. u8 pwr = 0;
  1382. if (mode != MMC_POWER_OFF) {
  1383. switch (1 << vdd) {
  1384. case MMC_VDD_165_195:
  1385. /*
  1386. * Without a regulator, SDHCI does not support 2.0v
  1387. * so we only get here if the driver deliberately
  1388. * added the 2.0v range to ocr_avail. Map it to 1.8v
  1389. * for the purpose of turning on the power.
  1390. */
  1391. case MMC_VDD_20_21:
  1392. pwr = SDHCI_POWER_180;
  1393. break;
  1394. case MMC_VDD_29_30:
  1395. case MMC_VDD_30_31:
  1396. pwr = SDHCI_POWER_300;
  1397. break;
  1398. case MMC_VDD_32_33:
  1399. case MMC_VDD_33_34:
  1400. pwr = SDHCI_POWER_330;
  1401. break;
  1402. default:
  1403. WARN(1, "%s: Invalid vdd %#x\n",
  1404. mmc_hostname(host->mmc), vdd);
  1405. break;
  1406. }
  1407. }
  1408. if (host->pwr == pwr)
  1409. return;
  1410. host->pwr = pwr;
  1411. if (pwr == 0) {
  1412. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  1413. if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
  1414. sdhci_runtime_pm_bus_off(host);
  1415. } else {
  1416. /*
  1417. * Spec says that we should clear the power reg before setting
  1418. * a new value. Some controllers don't seem to like this though.
  1419. */
  1420. if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
  1421. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  1422. /*
  1423. * At least the Marvell CaFe chip gets confused if we set the
  1424. * voltage and set turn on power at the same time, so set the
  1425. * voltage first.
  1426. */
  1427. if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
  1428. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  1429. pwr |= SDHCI_POWER_ON;
  1430. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  1431. if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
  1432. sdhci_runtime_pm_bus_on(host);
  1433. /*
  1434. * Some controllers need an extra 10ms delay of 10ms before
  1435. * they can apply clock after applying power
  1436. */
  1437. if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
  1438. mdelay(10);
  1439. }
  1440. }
  1441. EXPORT_SYMBOL_GPL(sdhci_set_power_noreg);
  1442. void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
  1443. unsigned short vdd)
  1444. {
  1445. if (IS_ERR(host->mmc->supply.vmmc))
  1446. sdhci_set_power_noreg(host, mode, vdd);
  1447. else
  1448. sdhci_set_power_reg(host, mode, vdd);
  1449. }
  1450. EXPORT_SYMBOL_GPL(sdhci_set_power);
  1451. /*****************************************************************************\
  1452. * *
  1453. * MMC callbacks *
  1454. * *
  1455. \*****************************************************************************/
  1456. void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  1457. {
  1458. struct sdhci_host *host;
  1459. int present;
  1460. unsigned long flags;
  1461. host = mmc_priv(mmc);
  1462. /* Firstly check card presence */
  1463. present = mmc->ops->get_cd(mmc);
  1464. spin_lock_irqsave(&host->lock, flags);
  1465. sdhci_led_activate(host);
  1466. /*
  1467. * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
  1468. * requests if Auto-CMD12 is enabled.
  1469. */
  1470. if (sdhci_auto_cmd12(host, mrq)) {
  1471. if (mrq->stop) {
  1472. mrq->data->stop = NULL;
  1473. mrq->stop = NULL;
  1474. }
  1475. }
  1476. if (!present || host->flags & SDHCI_DEVICE_DEAD) {
  1477. mrq->cmd->error = -ENOMEDIUM;
  1478. sdhci_finish_mrq(host, mrq);
  1479. } else {
  1480. if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
  1481. sdhci_send_command(host, mrq->sbc);
  1482. else
  1483. sdhci_send_command(host, mrq->cmd);
  1484. }
  1485. mmiowb();
  1486. spin_unlock_irqrestore(&host->lock, flags);
  1487. }
  1488. EXPORT_SYMBOL_GPL(sdhci_request);
  1489. void sdhci_set_bus_width(struct sdhci_host *host, int width)
  1490. {
  1491. u8 ctrl;
  1492. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  1493. if (width == MMC_BUS_WIDTH_8) {
  1494. ctrl &= ~SDHCI_CTRL_4BITBUS;
  1495. ctrl |= SDHCI_CTRL_8BITBUS;
  1496. } else {
  1497. if (host->mmc->caps & MMC_CAP_8_BIT_DATA)
  1498. ctrl &= ~SDHCI_CTRL_8BITBUS;
  1499. if (width == MMC_BUS_WIDTH_4)
  1500. ctrl |= SDHCI_CTRL_4BITBUS;
  1501. else
  1502. ctrl &= ~SDHCI_CTRL_4BITBUS;
  1503. }
  1504. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1505. }
  1506. EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
  1507. void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
  1508. {
  1509. u16 ctrl_2;
  1510. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1511. /* Select Bus Speed Mode for host */
  1512. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  1513. if ((timing == MMC_TIMING_MMC_HS200) ||
  1514. (timing == MMC_TIMING_UHS_SDR104))
  1515. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  1516. else if (timing == MMC_TIMING_UHS_SDR12)
  1517. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  1518. else if (timing == MMC_TIMING_UHS_SDR25)
  1519. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  1520. else if (timing == MMC_TIMING_UHS_SDR50)
  1521. ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
  1522. else if ((timing == MMC_TIMING_UHS_DDR50) ||
  1523. (timing == MMC_TIMING_MMC_DDR52))
  1524. ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
  1525. else if (timing == MMC_TIMING_MMC_HS400)
  1526. ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
  1527. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  1528. }
  1529. EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
  1530. void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1531. {
  1532. struct sdhci_host *host = mmc_priv(mmc);
  1533. u8 ctrl;
  1534. if (ios->power_mode == MMC_POWER_UNDEFINED)
  1535. return;
  1536. if (host->flags & SDHCI_DEVICE_DEAD) {
  1537. if (!IS_ERR(mmc->supply.vmmc) &&
  1538. ios->power_mode == MMC_POWER_OFF)
  1539. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  1540. return;
  1541. }
  1542. /*
  1543. * Reset the chip on each power off.
  1544. * Should clear out any weird states.
  1545. */
  1546. if (ios->power_mode == MMC_POWER_OFF) {
  1547. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  1548. sdhci_reinit(host);
  1549. }
  1550. if (host->version >= SDHCI_SPEC_300 &&
  1551. (ios->power_mode == MMC_POWER_UP) &&
  1552. !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
  1553. sdhci_enable_preset_value(host, false);
  1554. if (!ios->clock || ios->clock != host->clock) {
  1555. host->ops->set_clock(host, ios->clock);
  1556. host->clock = ios->clock;
  1557. if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
  1558. host->clock) {
  1559. host->timeout_clk = host->mmc->actual_clock ?
  1560. host->mmc->actual_clock / 1000 :
  1561. host->clock / 1000;
  1562. host->mmc->max_busy_timeout =
  1563. host->ops->get_max_timeout_count ?
  1564. host->ops->get_max_timeout_count(host) :
  1565. 1 << 27;
  1566. host->mmc->max_busy_timeout /= host->timeout_clk;
  1567. }
  1568. }
  1569. if (host->ops->set_power)
  1570. host->ops->set_power(host, ios->power_mode, ios->vdd);
  1571. else
  1572. sdhci_set_power(host, ios->power_mode, ios->vdd);
  1573. if (host->ops->platform_send_init_74_clocks)
  1574. host->ops->platform_send_init_74_clocks(host, ios->power_mode);
  1575. host->ops->set_bus_width(host, ios->bus_width);
  1576. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  1577. if (!(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT)) {
  1578. if (ios->timing == MMC_TIMING_SD_HS ||
  1579. ios->timing == MMC_TIMING_MMC_HS ||
  1580. ios->timing == MMC_TIMING_MMC_HS400 ||
  1581. ios->timing == MMC_TIMING_MMC_HS200 ||
  1582. ios->timing == MMC_TIMING_MMC_DDR52 ||
  1583. ios->timing == MMC_TIMING_UHS_SDR50 ||
  1584. ios->timing == MMC_TIMING_UHS_SDR104 ||
  1585. ios->timing == MMC_TIMING_UHS_DDR50 ||
  1586. ios->timing == MMC_TIMING_UHS_SDR25)
  1587. ctrl |= SDHCI_CTRL_HISPD;
  1588. else
  1589. ctrl &= ~SDHCI_CTRL_HISPD;
  1590. }
  1591. if (host->version >= SDHCI_SPEC_300) {
  1592. u16 clk, ctrl_2;
  1593. if (!host->preset_enabled) {
  1594. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1595. /*
  1596. * We only need to set Driver Strength if the
  1597. * preset value enable is not set.
  1598. */
  1599. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1600. ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
  1601. if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
  1602. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
  1603. else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
  1604. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
  1605. else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
  1606. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
  1607. else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
  1608. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
  1609. else {
  1610. pr_warn("%s: invalid driver type, default to driver type B\n",
  1611. mmc_hostname(mmc));
  1612. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
  1613. }
  1614. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  1615. } else {
  1616. /*
  1617. * According to SDHC Spec v3.00, if the Preset Value
  1618. * Enable in the Host Control 2 register is set, we
  1619. * need to reset SD Clock Enable before changing High
  1620. * Speed Enable to avoid generating clock gliches.
  1621. */
  1622. /* Reset SD Clock Enable */
  1623. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1624. clk &= ~SDHCI_CLOCK_CARD_EN;
  1625. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1626. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1627. /* Re-enable SD Clock */
  1628. host->ops->set_clock(host, host->clock);
  1629. }
  1630. /* Reset SD Clock Enable */
  1631. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1632. clk &= ~SDHCI_CLOCK_CARD_EN;
  1633. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1634. host->ops->set_uhs_signaling(host, ios->timing);
  1635. host->timing = ios->timing;
  1636. if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
  1637. ((ios->timing == MMC_TIMING_UHS_SDR12) ||
  1638. (ios->timing == MMC_TIMING_UHS_SDR25) ||
  1639. (ios->timing == MMC_TIMING_UHS_SDR50) ||
  1640. (ios->timing == MMC_TIMING_UHS_SDR104) ||
  1641. (ios->timing == MMC_TIMING_UHS_DDR50) ||
  1642. (ios->timing == MMC_TIMING_MMC_DDR52))) {
  1643. u16 preset;
  1644. sdhci_enable_preset_value(host, true);
  1645. preset = sdhci_get_preset_value(host);
  1646. ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
  1647. >> SDHCI_PRESET_DRV_SHIFT;
  1648. }
  1649. /* Re-enable SD Clock */
  1650. host->ops->set_clock(host, host->clock);
  1651. } else
  1652. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1653. /*
  1654. * Some (ENE) controllers go apeshit on some ios operation,
  1655. * signalling timeout and CRC errors even on CMD0. Resetting
  1656. * it on each ios seems to solve the problem.
  1657. */
  1658. if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
  1659. sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  1660. mmiowb();
  1661. }
  1662. EXPORT_SYMBOL_GPL(sdhci_set_ios);
  1663. static int sdhci_get_cd(struct mmc_host *mmc)
  1664. {
  1665. struct sdhci_host *host = mmc_priv(mmc);
  1666. int gpio_cd = mmc_gpio_get_cd(mmc);
  1667. if (host->flags & SDHCI_DEVICE_DEAD)
  1668. return 0;
  1669. /* If nonremovable, assume that the card is always present. */
  1670. if (!mmc_card_is_removable(host->mmc))
  1671. return 1;
  1672. /*
  1673. * Try slot gpio detect, if defined it take precedence
  1674. * over build in controller functionality
  1675. */
  1676. if (gpio_cd >= 0)
  1677. return !!gpio_cd;
  1678. /* If polling, assume that the card is always present. */
  1679. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  1680. return 1;
  1681. /* Host native card detect */
  1682. return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
  1683. }
  1684. static int sdhci_check_ro(struct sdhci_host *host)
  1685. {
  1686. unsigned long flags;
  1687. int is_readonly;
  1688. spin_lock_irqsave(&host->lock, flags);
  1689. if (host->flags & SDHCI_DEVICE_DEAD)
  1690. is_readonly = 0;
  1691. else if (host->ops->get_ro)
  1692. is_readonly = host->ops->get_ro(host);
  1693. else
  1694. is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
  1695. & SDHCI_WRITE_PROTECT);
  1696. spin_unlock_irqrestore(&host->lock, flags);
  1697. /* This quirk needs to be replaced by a callback-function later */
  1698. return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
  1699. !is_readonly : is_readonly;
  1700. }
  1701. #define SAMPLE_COUNT 5
  1702. static int sdhci_get_ro(struct mmc_host *mmc)
  1703. {
  1704. struct sdhci_host *host = mmc_priv(mmc);
  1705. int i, ro_count;
  1706. if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
  1707. return sdhci_check_ro(host);
  1708. ro_count = 0;
  1709. for (i = 0; i < SAMPLE_COUNT; i++) {
  1710. if (sdhci_check_ro(host)) {
  1711. if (++ro_count > SAMPLE_COUNT / 2)
  1712. return 1;
  1713. }
  1714. msleep(30);
  1715. }
  1716. return 0;
  1717. }
  1718. static void sdhci_hw_reset(struct mmc_host *mmc)
  1719. {
  1720. struct sdhci_host *host = mmc_priv(mmc);
  1721. if (host->ops && host->ops->hw_reset)
  1722. host->ops->hw_reset(host);
  1723. }
  1724. static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
  1725. {
  1726. if (!(host->flags & SDHCI_DEVICE_DEAD)) {
  1727. if (enable)
  1728. host->ier |= SDHCI_INT_CARD_INT;
  1729. else
  1730. host->ier &= ~SDHCI_INT_CARD_INT;
  1731. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  1732. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  1733. mmiowb();
  1734. }
  1735. }
  1736. void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  1737. {
  1738. struct sdhci_host *host = mmc_priv(mmc);
  1739. unsigned long flags;
  1740. if (enable)
  1741. pm_runtime_get_noresume(host->mmc->parent);
  1742. spin_lock_irqsave(&host->lock, flags);
  1743. if (enable)
  1744. host->flags |= SDHCI_SDIO_IRQ_ENABLED;
  1745. else
  1746. host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
  1747. sdhci_enable_sdio_irq_nolock(host, enable);
  1748. spin_unlock_irqrestore(&host->lock, flags);
  1749. if (!enable)
  1750. pm_runtime_put_noidle(host->mmc->parent);
  1751. }
  1752. EXPORT_SYMBOL_GPL(sdhci_enable_sdio_irq);
  1753. int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
  1754. struct mmc_ios *ios)
  1755. {
  1756. struct sdhci_host *host = mmc_priv(mmc);
  1757. u16 ctrl;
  1758. int ret;
  1759. /*
  1760. * Signal Voltage Switching is only applicable for Host Controllers
  1761. * v3.00 and above.
  1762. */
  1763. if (host->version < SDHCI_SPEC_300)
  1764. return 0;
  1765. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1766. switch (ios->signal_voltage) {
  1767. case MMC_SIGNAL_VOLTAGE_330:
  1768. if (!(host->flags & SDHCI_SIGNALING_330))
  1769. return -EINVAL;
  1770. /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
  1771. ctrl &= ~SDHCI_CTRL_VDD_180;
  1772. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1773. if (!IS_ERR(mmc->supply.vqmmc)) {
  1774. ret = mmc_regulator_set_vqmmc(mmc, ios);
  1775. if (ret) {
  1776. pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
  1777. mmc_hostname(mmc));
  1778. return -EIO;
  1779. }
  1780. }
  1781. /* Wait for 5ms */
  1782. usleep_range(5000, 5500);
  1783. /* 3.3V regulator output should be stable within 5 ms */
  1784. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1785. if (!(ctrl & SDHCI_CTRL_VDD_180))
  1786. return 0;
  1787. pr_warn("%s: 3.3V regulator output did not became stable\n",
  1788. mmc_hostname(mmc));
  1789. return -EAGAIN;
  1790. case MMC_SIGNAL_VOLTAGE_180:
  1791. if (!(host->flags & SDHCI_SIGNALING_180))
  1792. return -EINVAL;
  1793. if (!IS_ERR(mmc->supply.vqmmc)) {
  1794. ret = mmc_regulator_set_vqmmc(mmc, ios);
  1795. if (ret) {
  1796. pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
  1797. mmc_hostname(mmc));
  1798. return -EIO;
  1799. }
  1800. }
  1801. /*
  1802. * Enable 1.8V Signal Enable in the Host Control2
  1803. * register
  1804. */
  1805. ctrl |= SDHCI_CTRL_VDD_180;
  1806. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1807. /* Some controller need to do more when switching */
  1808. if (host->ops->voltage_switch)
  1809. host->ops->voltage_switch(host);
  1810. /* 1.8V regulator output should be stable within 5 ms */
  1811. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1812. if (ctrl & SDHCI_CTRL_VDD_180)
  1813. return 0;
  1814. pr_warn("%s: 1.8V regulator output did not became stable\n",
  1815. mmc_hostname(mmc));
  1816. return -EAGAIN;
  1817. case MMC_SIGNAL_VOLTAGE_120:
  1818. if (!(host->flags & SDHCI_SIGNALING_120))
  1819. return -EINVAL;
  1820. if (!IS_ERR(mmc->supply.vqmmc)) {
  1821. ret = mmc_regulator_set_vqmmc(mmc, ios);
  1822. if (ret) {
  1823. pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
  1824. mmc_hostname(mmc));
  1825. return -EIO;
  1826. }
  1827. }
  1828. return 0;
  1829. default:
  1830. /* No signal voltage switch required */
  1831. return 0;
  1832. }
  1833. }
  1834. EXPORT_SYMBOL_GPL(sdhci_start_signal_voltage_switch);
  1835. static int sdhci_card_busy(struct mmc_host *mmc)
  1836. {
  1837. struct sdhci_host *host = mmc_priv(mmc);
  1838. u32 present_state;
  1839. /* Check whether DAT[0] is 0 */
  1840. present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
  1841. return !(present_state & SDHCI_DATA_0_LVL_MASK);
  1842. }
  1843. static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
  1844. {
  1845. struct sdhci_host *host = mmc_priv(mmc);
  1846. unsigned long flags;
  1847. spin_lock_irqsave(&host->lock, flags);
  1848. host->flags |= SDHCI_HS400_TUNING;
  1849. spin_unlock_irqrestore(&host->lock, flags);
  1850. return 0;
  1851. }
  1852. void sdhci_start_tuning(struct sdhci_host *host)
  1853. {
  1854. u16 ctrl;
  1855. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1856. ctrl |= SDHCI_CTRL_EXEC_TUNING;
  1857. if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
  1858. ctrl |= SDHCI_CTRL_TUNED_CLK;
  1859. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1860. /*
  1861. * As per the Host Controller spec v3.00, tuning command
  1862. * generates Buffer Read Ready interrupt, so enable that.
  1863. *
  1864. * Note: The spec clearly says that when tuning sequence
  1865. * is being performed, the controller does not generate
  1866. * interrupts other than Buffer Read Ready interrupt. But
  1867. * to make sure we don't hit a controller bug, we _only_
  1868. * enable Buffer Read Ready interrupt here.
  1869. */
  1870. sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
  1871. sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
  1872. }
  1873. EXPORT_SYMBOL_GPL(sdhci_start_tuning);
  1874. void sdhci_end_tuning(struct sdhci_host *host)
  1875. {
  1876. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  1877. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  1878. }
  1879. EXPORT_SYMBOL_GPL(sdhci_end_tuning);
  1880. void sdhci_reset_tuning(struct sdhci_host *host)
  1881. {
  1882. u16 ctrl;
  1883. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1884. ctrl &= ~SDHCI_CTRL_TUNED_CLK;
  1885. ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
  1886. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1887. }
  1888. EXPORT_SYMBOL_GPL(sdhci_reset_tuning);
  1889. static void sdhci_abort_tuning(struct sdhci_host *host, u32 opcode)
  1890. {
  1891. sdhci_reset_tuning(host);
  1892. sdhci_do_reset(host, SDHCI_RESET_CMD);
  1893. sdhci_do_reset(host, SDHCI_RESET_DATA);
  1894. sdhci_end_tuning(host);
  1895. mmc_abort_tuning(host->mmc, opcode);
  1896. }
  1897. /*
  1898. * We use sdhci_send_tuning() because mmc_send_tuning() is not a good fit. SDHCI
  1899. * tuning command does not have a data payload (or rather the hardware does it
  1900. * automatically) so mmc_send_tuning() will return -EIO. Also the tuning command
  1901. * interrupt setup is different to other commands and there is no timeout
  1902. * interrupt so special handling is needed.
  1903. */
  1904. void sdhci_send_tuning(struct sdhci_host *host, u32 opcode)
  1905. {
  1906. struct mmc_host *mmc = host->mmc;
  1907. struct mmc_command cmd = {};
  1908. struct mmc_request mrq = {};
  1909. unsigned long flags;
  1910. u32 b = host->sdma_boundary;
  1911. spin_lock_irqsave(&host->lock, flags);
  1912. cmd.opcode = opcode;
  1913. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  1914. cmd.mrq = &mrq;
  1915. mrq.cmd = &cmd;
  1916. /*
  1917. * In response to CMD19, the card sends 64 bytes of tuning
  1918. * block to the Host Controller. So we set the block size
  1919. * to 64 here.
  1920. */
  1921. if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
  1922. mmc->ios.bus_width == MMC_BUS_WIDTH_8)
  1923. sdhci_writew(host, SDHCI_MAKE_BLKSZ(b, 128), SDHCI_BLOCK_SIZE);
  1924. else
  1925. sdhci_writew(host, SDHCI_MAKE_BLKSZ(b, 64), SDHCI_BLOCK_SIZE);
  1926. /*
  1927. * The tuning block is sent by the card to the host controller.
  1928. * So we set the TRNS_READ bit in the Transfer Mode register.
  1929. * This also takes care of setting DMA Enable and Multi Block
  1930. * Select in the same register to 0.
  1931. */
  1932. sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
  1933. sdhci_send_command(host, &cmd);
  1934. host->cmd = NULL;
  1935. sdhci_del_timer(host, &mrq);
  1936. host->tuning_done = 0;
  1937. mmiowb();
  1938. spin_unlock_irqrestore(&host->lock, flags);
  1939. /* Wait for Buffer Read Ready interrupt */
  1940. wait_event_timeout(host->buf_ready_int, (host->tuning_done == 1),
  1941. msecs_to_jiffies(50));
  1942. }
  1943. EXPORT_SYMBOL_GPL(sdhci_send_tuning);
  1944. static int __sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
  1945. {
  1946. int i;
  1947. /*
  1948. * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
  1949. * of loops reaches 40 times.
  1950. */
  1951. for (i = 0; i < MAX_TUNING_LOOP; i++) {
  1952. u16 ctrl;
  1953. sdhci_send_tuning(host, opcode);
  1954. if (!host->tuning_done) {
  1955. pr_info("%s: Tuning timeout, falling back to fixed sampling clock\n",
  1956. mmc_hostname(host->mmc));
  1957. sdhci_abort_tuning(host, opcode);
  1958. return -ETIMEDOUT;
  1959. }
  1960. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1961. if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
  1962. if (ctrl & SDHCI_CTRL_TUNED_CLK)
  1963. return 0; /* Success! */
  1964. break;
  1965. }
  1966. /* Spec does not require a delay between tuning cycles */
  1967. if (host->tuning_delay > 0)
  1968. mdelay(host->tuning_delay);
  1969. }
  1970. pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
  1971. mmc_hostname(host->mmc));
  1972. sdhci_reset_tuning(host);
  1973. return -EAGAIN;
  1974. }
  1975. int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
  1976. {
  1977. struct sdhci_host *host = mmc_priv(mmc);
  1978. int err = 0;
  1979. unsigned int tuning_count = 0;
  1980. bool hs400_tuning;
  1981. hs400_tuning = host->flags & SDHCI_HS400_TUNING;
  1982. if (host->tuning_mode == SDHCI_TUNING_MODE_1)
  1983. tuning_count = host->tuning_count;
  1984. /*
  1985. * The Host Controller needs tuning in case of SDR104 and DDR50
  1986. * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
  1987. * the Capabilities register.
  1988. * If the Host Controller supports the HS200 mode then the
  1989. * tuning function has to be executed.
  1990. */
  1991. switch (host->timing) {
  1992. /* HS400 tuning is done in HS200 mode */
  1993. case MMC_TIMING_MMC_HS400:
  1994. err = -EINVAL;
  1995. goto out;
  1996. case MMC_TIMING_MMC_HS200:
  1997. /*
  1998. * Periodic re-tuning for HS400 is not expected to be needed, so
  1999. * disable it here.
  2000. */
  2001. if (hs400_tuning)
  2002. tuning_count = 0;
  2003. break;
  2004. case MMC_TIMING_UHS_SDR104:
  2005. case MMC_TIMING_UHS_DDR50:
  2006. break;
  2007. case MMC_TIMING_UHS_SDR50:
  2008. if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
  2009. break;
  2010. /* FALLTHROUGH */
  2011. default:
  2012. goto out;
  2013. }
  2014. if (host->ops->platform_execute_tuning) {
  2015. err = host->ops->platform_execute_tuning(host, opcode);
  2016. goto out;
  2017. }
  2018. host->mmc->retune_period = tuning_count;
  2019. if (host->tuning_delay < 0)
  2020. host->tuning_delay = opcode == MMC_SEND_TUNING_BLOCK;
  2021. sdhci_start_tuning(host);
  2022. host->tuning_err = __sdhci_execute_tuning(host, opcode);
  2023. sdhci_end_tuning(host);
  2024. out:
  2025. host->flags &= ~SDHCI_HS400_TUNING;
  2026. return err;
  2027. }
  2028. EXPORT_SYMBOL_GPL(sdhci_execute_tuning);
  2029. static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
  2030. {
  2031. /* Host Controller v3.00 defines preset value registers */
  2032. if (host->version < SDHCI_SPEC_300)
  2033. return;
  2034. /*
  2035. * We only enable or disable Preset Value if they are not already
  2036. * enabled or disabled respectively. Otherwise, we bail out.
  2037. */
  2038. if (host->preset_enabled != enable) {
  2039. u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  2040. if (enable)
  2041. ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
  2042. else
  2043. ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
  2044. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  2045. if (enable)
  2046. host->flags |= SDHCI_PV_ENABLED;
  2047. else
  2048. host->flags &= ~SDHCI_PV_ENABLED;
  2049. host->preset_enabled = enable;
  2050. }
  2051. }
  2052. static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
  2053. int err)
  2054. {
  2055. struct sdhci_host *host = mmc_priv(mmc);
  2056. struct mmc_data *data = mrq->data;
  2057. if (data->host_cookie != COOKIE_UNMAPPED)
  2058. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  2059. mmc_get_dma_dir(data));
  2060. data->host_cookie = COOKIE_UNMAPPED;
  2061. }
  2062. static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
  2063. {
  2064. struct sdhci_host *host = mmc_priv(mmc);
  2065. mrq->data->host_cookie = COOKIE_UNMAPPED;
  2066. /*
  2067. * No pre-mapping in the pre hook if we're using the bounce buffer,
  2068. * for that we would need two bounce buffers since one buffer is
  2069. * in flight when this is getting called.
  2070. */
  2071. if (host->flags & SDHCI_REQ_USE_DMA && !host->bounce_buffer)
  2072. sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
  2073. }
  2074. static inline bool sdhci_has_requests(struct sdhci_host *host)
  2075. {
  2076. return host->cmd || host->data_cmd;
  2077. }
  2078. static void sdhci_error_out_mrqs(struct sdhci_host *host, int err)
  2079. {
  2080. if (host->data_cmd) {
  2081. host->data_cmd->error = err;
  2082. sdhci_finish_mrq(host, host->data_cmd->mrq);
  2083. }
  2084. if (host->cmd) {
  2085. host->cmd->error = err;
  2086. sdhci_finish_mrq(host, host->cmd->mrq);
  2087. }
  2088. }
  2089. static void sdhci_card_event(struct mmc_host *mmc)
  2090. {
  2091. struct sdhci_host *host = mmc_priv(mmc);
  2092. unsigned long flags;
  2093. int present;
  2094. /* First check if client has provided their own card event */
  2095. if (host->ops->card_event)
  2096. host->ops->card_event(host);
  2097. present = mmc->ops->get_cd(mmc);
  2098. spin_lock_irqsave(&host->lock, flags);
  2099. /* Check sdhci_has_requests() first in case we are runtime suspended */
  2100. if (sdhci_has_requests(host) && !present) {
  2101. pr_err("%s: Card removed during transfer!\n",
  2102. mmc_hostname(host->mmc));
  2103. pr_err("%s: Resetting controller.\n",
  2104. mmc_hostname(host->mmc));
  2105. sdhci_do_reset(host, SDHCI_RESET_CMD);
  2106. sdhci_do_reset(host, SDHCI_RESET_DATA);
  2107. sdhci_error_out_mrqs(host, -ENOMEDIUM);
  2108. }
  2109. spin_unlock_irqrestore(&host->lock, flags);
  2110. }
  2111. static const struct mmc_host_ops sdhci_ops = {
  2112. .request = sdhci_request,
  2113. .post_req = sdhci_post_req,
  2114. .pre_req = sdhci_pre_req,
  2115. .set_ios = sdhci_set_ios,
  2116. .get_cd = sdhci_get_cd,
  2117. .get_ro = sdhci_get_ro,
  2118. .hw_reset = sdhci_hw_reset,
  2119. .enable_sdio_irq = sdhci_enable_sdio_irq,
  2120. .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
  2121. .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
  2122. .execute_tuning = sdhci_execute_tuning,
  2123. .card_event = sdhci_card_event,
  2124. .card_busy = sdhci_card_busy,
  2125. };
  2126. /*****************************************************************************\
  2127. * *
  2128. * Tasklets *
  2129. * *
  2130. \*****************************************************************************/
  2131. static bool sdhci_request_done(struct sdhci_host *host)
  2132. {
  2133. unsigned long flags;
  2134. struct mmc_request *mrq;
  2135. int i;
  2136. spin_lock_irqsave(&host->lock, flags);
  2137. for (i = 0; i < SDHCI_MAX_MRQS; i++) {
  2138. mrq = host->mrqs_done[i];
  2139. if (mrq)
  2140. break;
  2141. }
  2142. if (!mrq) {
  2143. spin_unlock_irqrestore(&host->lock, flags);
  2144. return true;
  2145. }
  2146. sdhci_del_timer(host, mrq);
  2147. /*
  2148. * Always unmap the data buffers if they were mapped by
  2149. * sdhci_prepare_data() whenever we finish with a request.
  2150. * This avoids leaking DMA mappings on error.
  2151. */
  2152. if (host->flags & SDHCI_REQ_USE_DMA) {
  2153. struct mmc_data *data = mrq->data;
  2154. if (data && data->host_cookie == COOKIE_MAPPED) {
  2155. if (host->bounce_buffer) {
  2156. /*
  2157. * On reads, copy the bounced data into the
  2158. * sglist
  2159. */
  2160. if (mmc_get_dma_dir(data) == DMA_FROM_DEVICE) {
  2161. unsigned int length = data->bytes_xfered;
  2162. if (length > host->bounce_buffer_size) {
  2163. pr_err("%s: bounce buffer is %u bytes but DMA claims to have transferred %u bytes\n",
  2164. mmc_hostname(host->mmc),
  2165. host->bounce_buffer_size,
  2166. data->bytes_xfered);
  2167. /* Cap it down and continue */
  2168. length = host->bounce_buffer_size;
  2169. }
  2170. dma_sync_single_for_cpu(
  2171. host->mmc->parent,
  2172. host->bounce_addr,
  2173. host->bounce_buffer_size,
  2174. DMA_FROM_DEVICE);
  2175. sg_copy_from_buffer(data->sg,
  2176. data->sg_len,
  2177. host->bounce_buffer,
  2178. length);
  2179. } else {
  2180. /* No copying, just switch ownership */
  2181. dma_sync_single_for_cpu(
  2182. host->mmc->parent,
  2183. host->bounce_addr,
  2184. host->bounce_buffer_size,
  2185. mmc_get_dma_dir(data));
  2186. }
  2187. } else {
  2188. /* Unmap the raw data */
  2189. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  2190. data->sg_len,
  2191. mmc_get_dma_dir(data));
  2192. }
  2193. data->host_cookie = COOKIE_UNMAPPED;
  2194. }
  2195. }
  2196. /*
  2197. * The controller needs a reset of internal state machines
  2198. * upon error conditions.
  2199. */
  2200. if (sdhci_needs_reset(host, mrq)) {
  2201. /*
  2202. * Do not finish until command and data lines are available for
  2203. * reset. Note there can only be one other mrq, so it cannot
  2204. * also be in mrqs_done, otherwise host->cmd and host->data_cmd
  2205. * would both be null.
  2206. */
  2207. if (host->cmd || host->data_cmd) {
  2208. spin_unlock_irqrestore(&host->lock, flags);
  2209. return true;
  2210. }
  2211. /* Some controllers need this kick or reset won't work here */
  2212. if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
  2213. /* This is to force an update */
  2214. host->ops->set_clock(host, host->clock);
  2215. /* Spec says we should do both at the same time, but Ricoh
  2216. controllers do not like that. */
  2217. sdhci_do_reset(host, SDHCI_RESET_CMD);
  2218. sdhci_do_reset(host, SDHCI_RESET_DATA);
  2219. host->pending_reset = false;
  2220. }
  2221. if (!sdhci_has_requests(host))
  2222. sdhci_led_deactivate(host);
  2223. host->mrqs_done[i] = NULL;
  2224. mmiowb();
  2225. spin_unlock_irqrestore(&host->lock, flags);
  2226. mmc_request_done(host->mmc, mrq);
  2227. return false;
  2228. }
  2229. static void sdhci_tasklet_finish(unsigned long param)
  2230. {
  2231. struct sdhci_host *host = (struct sdhci_host *)param;
  2232. while (!sdhci_request_done(host))
  2233. ;
  2234. }
  2235. static void sdhci_timeout_timer(struct timer_list *t)
  2236. {
  2237. struct sdhci_host *host;
  2238. unsigned long flags;
  2239. host = from_timer(host, t, timer);
  2240. spin_lock_irqsave(&host->lock, flags);
  2241. if (host->cmd && !sdhci_data_line_cmd(host->cmd)) {
  2242. pr_err("%s: Timeout waiting for hardware cmd interrupt.\n",
  2243. mmc_hostname(host->mmc));
  2244. sdhci_dumpregs(host);
  2245. host->cmd->error = -ETIMEDOUT;
  2246. sdhci_finish_mrq(host, host->cmd->mrq);
  2247. }
  2248. mmiowb();
  2249. spin_unlock_irqrestore(&host->lock, flags);
  2250. }
  2251. static void sdhci_timeout_data_timer(struct timer_list *t)
  2252. {
  2253. struct sdhci_host *host;
  2254. unsigned long flags;
  2255. host = from_timer(host, t, data_timer);
  2256. spin_lock_irqsave(&host->lock, flags);
  2257. if (host->data || host->data_cmd ||
  2258. (host->cmd && sdhci_data_line_cmd(host->cmd))) {
  2259. pr_err("%s: Timeout waiting for hardware interrupt.\n",
  2260. mmc_hostname(host->mmc));
  2261. sdhci_dumpregs(host);
  2262. if (host->data) {
  2263. host->data->error = -ETIMEDOUT;
  2264. sdhci_finish_data(host);
  2265. } else if (host->data_cmd) {
  2266. host->data_cmd->error = -ETIMEDOUT;
  2267. sdhci_finish_mrq(host, host->data_cmd->mrq);
  2268. } else {
  2269. host->cmd->error = -ETIMEDOUT;
  2270. sdhci_finish_mrq(host, host->cmd->mrq);
  2271. }
  2272. }
  2273. mmiowb();
  2274. spin_unlock_irqrestore(&host->lock, flags);
  2275. }
  2276. /*****************************************************************************\
  2277. * *
  2278. * Interrupt handling *
  2279. * *
  2280. \*****************************************************************************/
  2281. static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
  2282. {
  2283. if (!host->cmd) {
  2284. /*
  2285. * SDHCI recovers from errors by resetting the cmd and data
  2286. * circuits. Until that is done, there very well might be more
  2287. * interrupts, so ignore them in that case.
  2288. */
  2289. if (host->pending_reset)
  2290. return;
  2291. pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
  2292. mmc_hostname(host->mmc), (unsigned)intmask);
  2293. sdhci_dumpregs(host);
  2294. return;
  2295. }
  2296. if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
  2297. SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
  2298. if (intmask & SDHCI_INT_TIMEOUT)
  2299. host->cmd->error = -ETIMEDOUT;
  2300. else
  2301. host->cmd->error = -EILSEQ;
  2302. /*
  2303. * If this command initiates a data phase and a response
  2304. * CRC error is signalled, the card can start transferring
  2305. * data - the card may have received the command without
  2306. * error. We must not terminate the mmc_request early.
  2307. *
  2308. * If the card did not receive the command or returned an
  2309. * error which prevented it sending data, the data phase
  2310. * will time out.
  2311. */
  2312. if (host->cmd->data &&
  2313. (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
  2314. SDHCI_INT_CRC) {
  2315. host->cmd = NULL;
  2316. return;
  2317. }
  2318. sdhci_finish_mrq(host, host->cmd->mrq);
  2319. return;
  2320. }
  2321. if (intmask & SDHCI_INT_RESPONSE)
  2322. sdhci_finish_command(host);
  2323. }
  2324. static void sdhci_adma_show_error(struct sdhci_host *host)
  2325. {
  2326. void *desc = host->adma_table;
  2327. sdhci_dumpregs(host);
  2328. while (true) {
  2329. struct sdhci_adma2_64_desc *dma_desc = desc;
  2330. if (host->flags & SDHCI_USE_64_BIT_DMA)
  2331. DBG("%p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
  2332. desc, le32_to_cpu(dma_desc->addr_hi),
  2333. le32_to_cpu(dma_desc->addr_lo),
  2334. le16_to_cpu(dma_desc->len),
  2335. le16_to_cpu(dma_desc->cmd));
  2336. else
  2337. DBG("%p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
  2338. desc, le32_to_cpu(dma_desc->addr_lo),
  2339. le16_to_cpu(dma_desc->len),
  2340. le16_to_cpu(dma_desc->cmd));
  2341. desc += host->desc_sz;
  2342. if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
  2343. break;
  2344. }
  2345. }
  2346. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  2347. {
  2348. u32 command;
  2349. /* CMD19 generates _only_ Buffer Read Ready interrupt */
  2350. if (intmask & SDHCI_INT_DATA_AVAIL) {
  2351. command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
  2352. if (command == MMC_SEND_TUNING_BLOCK ||
  2353. command == MMC_SEND_TUNING_BLOCK_HS200) {
  2354. host->tuning_done = 1;
  2355. wake_up(&host->buf_ready_int);
  2356. return;
  2357. }
  2358. }
  2359. if (!host->data) {
  2360. struct mmc_command *data_cmd = host->data_cmd;
  2361. /*
  2362. * The "data complete" interrupt is also used to
  2363. * indicate that a busy state has ended. See comment
  2364. * above in sdhci_cmd_irq().
  2365. */
  2366. if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) {
  2367. if (intmask & SDHCI_INT_DATA_TIMEOUT) {
  2368. host->data_cmd = NULL;
  2369. data_cmd->error = -ETIMEDOUT;
  2370. sdhci_finish_mrq(host, data_cmd->mrq);
  2371. return;
  2372. }
  2373. if (intmask & SDHCI_INT_DATA_END) {
  2374. host->data_cmd = NULL;
  2375. /*
  2376. * Some cards handle busy-end interrupt
  2377. * before the command completed, so make
  2378. * sure we do things in the proper order.
  2379. */
  2380. if (host->cmd == data_cmd)
  2381. return;
  2382. sdhci_finish_mrq(host, data_cmd->mrq);
  2383. return;
  2384. }
  2385. }
  2386. /*
  2387. * SDHCI recovers from errors by resetting the cmd and data
  2388. * circuits. Until that is done, there very well might be more
  2389. * interrupts, so ignore them in that case.
  2390. */
  2391. if (host->pending_reset)
  2392. return;
  2393. pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
  2394. mmc_hostname(host->mmc), (unsigned)intmask);
  2395. sdhci_dumpregs(host);
  2396. return;
  2397. }
  2398. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  2399. host->data->error = -ETIMEDOUT;
  2400. else if (intmask & SDHCI_INT_DATA_END_BIT)
  2401. host->data->error = -EILSEQ;
  2402. else if ((intmask & SDHCI_INT_DATA_CRC) &&
  2403. SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
  2404. != MMC_BUS_TEST_R)
  2405. host->data->error = -EILSEQ;
  2406. else if (intmask & SDHCI_INT_ADMA_ERROR) {
  2407. pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
  2408. sdhci_adma_show_error(host);
  2409. host->data->error = -EIO;
  2410. if (host->ops->adma_workaround)
  2411. host->ops->adma_workaround(host, intmask);
  2412. }
  2413. if (host->data->error)
  2414. sdhci_finish_data(host);
  2415. else {
  2416. if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  2417. sdhci_transfer_pio(host);
  2418. /*
  2419. * We currently don't do anything fancy with DMA
  2420. * boundaries, but as we can't disable the feature
  2421. * we need to at least restart the transfer.
  2422. *
  2423. * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
  2424. * should return a valid address to continue from, but as
  2425. * some controllers are faulty, don't trust them.
  2426. */
  2427. if (intmask & SDHCI_INT_DMA_END) {
  2428. dma_addr_t dmastart, dmanow;
  2429. dmastart = sdhci_sdma_address(host);
  2430. dmanow = dmastart + host->data->bytes_xfered;
  2431. /*
  2432. * Force update to the next DMA block boundary.
  2433. */
  2434. dmanow = (dmanow &
  2435. ~((dma_addr_t)SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
  2436. SDHCI_DEFAULT_BOUNDARY_SIZE;
  2437. host->data->bytes_xfered = dmanow - dmastart;
  2438. DBG("DMA base %pad, transferred 0x%06x bytes, next %pad\n",
  2439. &dmastart, host->data->bytes_xfered, &dmanow);
  2440. sdhci_set_sdma_addr(host, dmanow);
  2441. }
  2442. if (intmask & SDHCI_INT_DATA_END) {
  2443. if (host->cmd == host->data_cmd) {
  2444. /*
  2445. * Data managed to finish before the
  2446. * command completed. Make sure we do
  2447. * things in the proper order.
  2448. */
  2449. host->data_early = 1;
  2450. } else {
  2451. sdhci_finish_data(host);
  2452. }
  2453. }
  2454. }
  2455. }
  2456. static irqreturn_t sdhci_irq(int irq, void *dev_id)
  2457. {
  2458. irqreturn_t result = IRQ_NONE;
  2459. struct sdhci_host *host = dev_id;
  2460. u32 intmask, mask, unexpected = 0;
  2461. int max_loops = 16;
  2462. spin_lock(&host->lock);
  2463. if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
  2464. spin_unlock(&host->lock);
  2465. return IRQ_NONE;
  2466. }
  2467. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  2468. if (!intmask || intmask == 0xffffffff) {
  2469. result = IRQ_NONE;
  2470. goto out;
  2471. }
  2472. do {
  2473. DBG("IRQ status 0x%08x\n", intmask);
  2474. if (host->ops->irq) {
  2475. intmask = host->ops->irq(host, intmask);
  2476. if (!intmask)
  2477. goto cont;
  2478. }
  2479. /* Clear selected interrupts. */
  2480. mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  2481. SDHCI_INT_BUS_POWER);
  2482. sdhci_writel(host, mask, SDHCI_INT_STATUS);
  2483. if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  2484. u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  2485. SDHCI_CARD_PRESENT;
  2486. /*
  2487. * There is a observation on i.mx esdhc. INSERT
  2488. * bit will be immediately set again when it gets
  2489. * cleared, if a card is inserted. We have to mask
  2490. * the irq to prevent interrupt storm which will
  2491. * freeze the system. And the REMOVE gets the
  2492. * same situation.
  2493. *
  2494. * More testing are needed here to ensure it works
  2495. * for other platforms though.
  2496. */
  2497. host->ier &= ~(SDHCI_INT_CARD_INSERT |
  2498. SDHCI_INT_CARD_REMOVE);
  2499. host->ier |= present ? SDHCI_INT_CARD_REMOVE :
  2500. SDHCI_INT_CARD_INSERT;
  2501. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  2502. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  2503. sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
  2504. SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
  2505. host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
  2506. SDHCI_INT_CARD_REMOVE);
  2507. result = IRQ_WAKE_THREAD;
  2508. }
  2509. if (intmask & SDHCI_INT_CMD_MASK)
  2510. sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  2511. if (intmask & SDHCI_INT_DATA_MASK)
  2512. sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  2513. if (intmask & SDHCI_INT_BUS_POWER)
  2514. pr_err("%s: Card is consuming too much power!\n",
  2515. mmc_hostname(host->mmc));
  2516. if (intmask & SDHCI_INT_RETUNE)
  2517. mmc_retune_needed(host->mmc);
  2518. if ((intmask & SDHCI_INT_CARD_INT) &&
  2519. (host->ier & SDHCI_INT_CARD_INT)) {
  2520. sdhci_enable_sdio_irq_nolock(host, false);
  2521. host->thread_isr |= SDHCI_INT_CARD_INT;
  2522. result = IRQ_WAKE_THREAD;
  2523. }
  2524. intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
  2525. SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  2526. SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
  2527. SDHCI_INT_RETUNE | SDHCI_INT_CARD_INT);
  2528. if (intmask) {
  2529. unexpected |= intmask;
  2530. sdhci_writel(host, intmask, SDHCI_INT_STATUS);
  2531. }
  2532. cont:
  2533. if (result == IRQ_NONE)
  2534. result = IRQ_HANDLED;
  2535. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  2536. } while (intmask && --max_loops);
  2537. out:
  2538. spin_unlock(&host->lock);
  2539. if (unexpected) {
  2540. pr_err("%s: Unexpected interrupt 0x%08x.\n",
  2541. mmc_hostname(host->mmc), unexpected);
  2542. sdhci_dumpregs(host);
  2543. }
  2544. return result;
  2545. }
  2546. static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
  2547. {
  2548. struct sdhci_host *host = dev_id;
  2549. unsigned long flags;
  2550. u32 isr;
  2551. spin_lock_irqsave(&host->lock, flags);
  2552. isr = host->thread_isr;
  2553. host->thread_isr = 0;
  2554. spin_unlock_irqrestore(&host->lock, flags);
  2555. if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  2556. struct mmc_host *mmc = host->mmc;
  2557. mmc->ops->card_event(mmc);
  2558. mmc_detect_change(mmc, msecs_to_jiffies(200));
  2559. }
  2560. if (isr & SDHCI_INT_CARD_INT) {
  2561. sdio_run_irqs(host->mmc);
  2562. spin_lock_irqsave(&host->lock, flags);
  2563. if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
  2564. sdhci_enable_sdio_irq_nolock(host, true);
  2565. spin_unlock_irqrestore(&host->lock, flags);
  2566. }
  2567. return isr ? IRQ_HANDLED : IRQ_NONE;
  2568. }
  2569. /*****************************************************************************\
  2570. * *
  2571. * Suspend/resume *
  2572. * *
  2573. \*****************************************************************************/
  2574. #ifdef CONFIG_PM
  2575. static bool sdhci_cd_irq_can_wakeup(struct sdhci_host *host)
  2576. {
  2577. return mmc_card_is_removable(host->mmc) &&
  2578. !(host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
  2579. !mmc_can_gpio_cd(host->mmc);
  2580. }
  2581. /*
  2582. * To enable wakeup events, the corresponding events have to be enabled in
  2583. * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
  2584. * Table' in the SD Host Controller Standard Specification.
  2585. * It is useless to restore SDHCI_INT_ENABLE state in
  2586. * sdhci_disable_irq_wakeups() since it will be set by
  2587. * sdhci_enable_card_detection() or sdhci_init().
  2588. */
  2589. static bool sdhci_enable_irq_wakeups(struct sdhci_host *host)
  2590. {
  2591. u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE |
  2592. SDHCI_WAKE_ON_INT;
  2593. u32 irq_val = 0;
  2594. u8 wake_val = 0;
  2595. u8 val;
  2596. if (sdhci_cd_irq_can_wakeup(host)) {
  2597. wake_val |= SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE;
  2598. irq_val |= SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE;
  2599. }
  2600. if (mmc_card_wake_sdio_irq(host->mmc)) {
  2601. wake_val |= SDHCI_WAKE_ON_INT;
  2602. irq_val |= SDHCI_INT_CARD_INT;
  2603. }
  2604. if (!irq_val)
  2605. return false;
  2606. val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
  2607. val &= ~mask;
  2608. val |= wake_val;
  2609. sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
  2610. sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
  2611. host->irq_wake_enabled = !enable_irq_wake(host->irq);
  2612. return host->irq_wake_enabled;
  2613. }
  2614. static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
  2615. {
  2616. u8 val;
  2617. u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
  2618. | SDHCI_WAKE_ON_INT;
  2619. val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
  2620. val &= ~mask;
  2621. sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
  2622. disable_irq_wake(host->irq);
  2623. host->irq_wake_enabled = false;
  2624. }
  2625. int sdhci_suspend_host(struct sdhci_host *host)
  2626. {
  2627. sdhci_disable_card_detection(host);
  2628. mmc_retune_timer_stop(host->mmc);
  2629. if (!device_may_wakeup(mmc_dev(host->mmc)) ||
  2630. !sdhci_enable_irq_wakeups(host)) {
  2631. host->ier = 0;
  2632. sdhci_writel(host, 0, SDHCI_INT_ENABLE);
  2633. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  2634. free_irq(host->irq, host);
  2635. }
  2636. return 0;
  2637. }
  2638. EXPORT_SYMBOL_GPL(sdhci_suspend_host);
  2639. int sdhci_resume_host(struct sdhci_host *host)
  2640. {
  2641. struct mmc_host *mmc = host->mmc;
  2642. int ret = 0;
  2643. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2644. if (host->ops->enable_dma)
  2645. host->ops->enable_dma(host);
  2646. }
  2647. if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
  2648. (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
  2649. /* Card keeps power but host controller does not */
  2650. sdhci_init(host, 0);
  2651. host->pwr = 0;
  2652. host->clock = 0;
  2653. mmc->ops->set_ios(mmc, &mmc->ios);
  2654. } else {
  2655. sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
  2656. mmiowb();
  2657. }
  2658. if (host->irq_wake_enabled) {
  2659. sdhci_disable_irq_wakeups(host);
  2660. } else {
  2661. ret = request_threaded_irq(host->irq, sdhci_irq,
  2662. sdhci_thread_irq, IRQF_SHARED,
  2663. mmc_hostname(host->mmc), host);
  2664. if (ret)
  2665. return ret;
  2666. }
  2667. sdhci_enable_card_detection(host);
  2668. return ret;
  2669. }
  2670. EXPORT_SYMBOL_GPL(sdhci_resume_host);
  2671. int sdhci_runtime_suspend_host(struct sdhci_host *host)
  2672. {
  2673. unsigned long flags;
  2674. mmc_retune_timer_stop(host->mmc);
  2675. spin_lock_irqsave(&host->lock, flags);
  2676. host->ier &= SDHCI_INT_CARD_INT;
  2677. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  2678. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  2679. spin_unlock_irqrestore(&host->lock, flags);
  2680. synchronize_hardirq(host->irq);
  2681. spin_lock_irqsave(&host->lock, flags);
  2682. host->runtime_suspended = true;
  2683. spin_unlock_irqrestore(&host->lock, flags);
  2684. return 0;
  2685. }
  2686. EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
  2687. int sdhci_runtime_resume_host(struct sdhci_host *host)
  2688. {
  2689. struct mmc_host *mmc = host->mmc;
  2690. unsigned long flags;
  2691. int host_flags = host->flags;
  2692. if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2693. if (host->ops->enable_dma)
  2694. host->ops->enable_dma(host);
  2695. }
  2696. sdhci_init(host, 0);
  2697. if (mmc->ios.power_mode != MMC_POWER_UNDEFINED &&
  2698. mmc->ios.power_mode != MMC_POWER_OFF) {
  2699. /* Force clock and power re-program */
  2700. host->pwr = 0;
  2701. host->clock = 0;
  2702. mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
  2703. mmc->ops->set_ios(mmc, &mmc->ios);
  2704. if ((host_flags & SDHCI_PV_ENABLED) &&
  2705. !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
  2706. spin_lock_irqsave(&host->lock, flags);
  2707. sdhci_enable_preset_value(host, true);
  2708. spin_unlock_irqrestore(&host->lock, flags);
  2709. }
  2710. if ((mmc->caps2 & MMC_CAP2_HS400_ES) &&
  2711. mmc->ops->hs400_enhanced_strobe)
  2712. mmc->ops->hs400_enhanced_strobe(mmc, &mmc->ios);
  2713. }
  2714. spin_lock_irqsave(&host->lock, flags);
  2715. host->runtime_suspended = false;
  2716. /* Enable SDIO IRQ */
  2717. if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
  2718. sdhci_enable_sdio_irq_nolock(host, true);
  2719. /* Enable Card Detection */
  2720. sdhci_enable_card_detection(host);
  2721. spin_unlock_irqrestore(&host->lock, flags);
  2722. return 0;
  2723. }
  2724. EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
  2725. #endif /* CONFIG_PM */
  2726. /*****************************************************************************\
  2727. * *
  2728. * Command Queue Engine (CQE) helpers *
  2729. * *
  2730. \*****************************************************************************/
  2731. void sdhci_cqe_enable(struct mmc_host *mmc)
  2732. {
  2733. struct sdhci_host *host = mmc_priv(mmc);
  2734. unsigned long flags;
  2735. u8 ctrl;
  2736. spin_lock_irqsave(&host->lock, flags);
  2737. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  2738. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  2739. if (host->flags & SDHCI_USE_64_BIT_DMA)
  2740. ctrl |= SDHCI_CTRL_ADMA64;
  2741. else
  2742. ctrl |= SDHCI_CTRL_ADMA32;
  2743. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  2744. sdhci_writew(host, SDHCI_MAKE_BLKSZ(host->sdma_boundary, 512),
  2745. SDHCI_BLOCK_SIZE);
  2746. /* Set maximum timeout */
  2747. sdhci_writeb(host, 0xE, SDHCI_TIMEOUT_CONTROL);
  2748. host->ier = host->cqe_ier;
  2749. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  2750. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  2751. host->cqe_on = true;
  2752. pr_debug("%s: sdhci: CQE on, IRQ mask %#x, IRQ status %#x\n",
  2753. mmc_hostname(mmc), host->ier,
  2754. sdhci_readl(host, SDHCI_INT_STATUS));
  2755. mmiowb();
  2756. spin_unlock_irqrestore(&host->lock, flags);
  2757. }
  2758. EXPORT_SYMBOL_GPL(sdhci_cqe_enable);
  2759. void sdhci_cqe_disable(struct mmc_host *mmc, bool recovery)
  2760. {
  2761. struct sdhci_host *host = mmc_priv(mmc);
  2762. unsigned long flags;
  2763. spin_lock_irqsave(&host->lock, flags);
  2764. sdhci_set_default_irqs(host);
  2765. host->cqe_on = false;
  2766. if (recovery) {
  2767. sdhci_do_reset(host, SDHCI_RESET_CMD);
  2768. sdhci_do_reset(host, SDHCI_RESET_DATA);
  2769. }
  2770. pr_debug("%s: sdhci: CQE off, IRQ mask %#x, IRQ status %#x\n",
  2771. mmc_hostname(mmc), host->ier,
  2772. sdhci_readl(host, SDHCI_INT_STATUS));
  2773. mmiowb();
  2774. spin_unlock_irqrestore(&host->lock, flags);
  2775. }
  2776. EXPORT_SYMBOL_GPL(sdhci_cqe_disable);
  2777. bool sdhci_cqe_irq(struct sdhci_host *host, u32 intmask, int *cmd_error,
  2778. int *data_error)
  2779. {
  2780. u32 mask;
  2781. if (!host->cqe_on)
  2782. return false;
  2783. if (intmask & (SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC))
  2784. *cmd_error = -EILSEQ;
  2785. else if (intmask & SDHCI_INT_TIMEOUT)
  2786. *cmd_error = -ETIMEDOUT;
  2787. else
  2788. *cmd_error = 0;
  2789. if (intmask & (SDHCI_INT_DATA_END_BIT | SDHCI_INT_DATA_CRC))
  2790. *data_error = -EILSEQ;
  2791. else if (intmask & SDHCI_INT_DATA_TIMEOUT)
  2792. *data_error = -ETIMEDOUT;
  2793. else if (intmask & SDHCI_INT_ADMA_ERROR)
  2794. *data_error = -EIO;
  2795. else
  2796. *data_error = 0;
  2797. /* Clear selected interrupts. */
  2798. mask = intmask & host->cqe_ier;
  2799. sdhci_writel(host, mask, SDHCI_INT_STATUS);
  2800. if (intmask & SDHCI_INT_BUS_POWER)
  2801. pr_err("%s: Card is consuming too much power!\n",
  2802. mmc_hostname(host->mmc));
  2803. intmask &= ~(host->cqe_ier | SDHCI_INT_ERROR);
  2804. if (intmask) {
  2805. sdhci_writel(host, intmask, SDHCI_INT_STATUS);
  2806. pr_err("%s: CQE: Unexpected interrupt 0x%08x.\n",
  2807. mmc_hostname(host->mmc), intmask);
  2808. sdhci_dumpregs(host);
  2809. }
  2810. return true;
  2811. }
  2812. EXPORT_SYMBOL_GPL(sdhci_cqe_irq);
  2813. /*****************************************************************************\
  2814. * *
  2815. * Device allocation/registration *
  2816. * *
  2817. \*****************************************************************************/
  2818. struct sdhci_host *sdhci_alloc_host(struct device *dev,
  2819. size_t priv_size)
  2820. {
  2821. struct mmc_host *mmc;
  2822. struct sdhci_host *host;
  2823. WARN_ON(dev == NULL);
  2824. mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
  2825. if (!mmc)
  2826. return ERR_PTR(-ENOMEM);
  2827. host = mmc_priv(mmc);
  2828. host->mmc = mmc;
  2829. host->mmc_host_ops = sdhci_ops;
  2830. mmc->ops = &host->mmc_host_ops;
  2831. host->flags = SDHCI_SIGNALING_330;
  2832. host->cqe_ier = SDHCI_CQE_INT_MASK;
  2833. host->cqe_err_ier = SDHCI_CQE_INT_ERR_MASK;
  2834. host->tuning_delay = -1;
  2835. host->sdma_boundary = SDHCI_DEFAULT_BOUNDARY_ARG;
  2836. /*
  2837. * The DMA table descriptor count is calculated as the maximum
  2838. * number of segments times 2, to allow for an alignment
  2839. * descriptor for each segment, plus 1 for a nop end descriptor.
  2840. */
  2841. host->adma_table_cnt = SDHCI_MAX_SEGS * 2 + 1;
  2842. return host;
  2843. }
  2844. EXPORT_SYMBOL_GPL(sdhci_alloc_host);
  2845. static int sdhci_set_dma_mask(struct sdhci_host *host)
  2846. {
  2847. struct mmc_host *mmc = host->mmc;
  2848. struct device *dev = mmc_dev(mmc);
  2849. int ret = -EINVAL;
  2850. if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
  2851. host->flags &= ~SDHCI_USE_64_BIT_DMA;
  2852. /* Try 64-bit mask if hardware is capable of it */
  2853. if (host->flags & SDHCI_USE_64_BIT_DMA) {
  2854. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
  2855. if (ret) {
  2856. pr_warn("%s: Failed to set 64-bit DMA mask.\n",
  2857. mmc_hostname(mmc));
  2858. host->flags &= ~SDHCI_USE_64_BIT_DMA;
  2859. }
  2860. }
  2861. /* 32-bit mask as default & fallback */
  2862. if (ret) {
  2863. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  2864. if (ret)
  2865. pr_warn("%s: Failed to set 32-bit DMA mask.\n",
  2866. mmc_hostname(mmc));
  2867. }
  2868. return ret;
  2869. }
  2870. void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
  2871. {
  2872. u16 v;
  2873. u64 dt_caps_mask = 0;
  2874. u64 dt_caps = 0;
  2875. if (host->read_caps)
  2876. return;
  2877. host->read_caps = true;
  2878. if (debug_quirks)
  2879. host->quirks = debug_quirks;
  2880. if (debug_quirks2)
  2881. host->quirks2 = debug_quirks2;
  2882. sdhci_do_reset(host, SDHCI_RESET_ALL);
  2883. if (host->v4_mode)
  2884. sdhci_do_enable_v4_mode(host);
  2885. of_property_read_u64(mmc_dev(host->mmc)->of_node,
  2886. "sdhci-caps-mask", &dt_caps_mask);
  2887. of_property_read_u64(mmc_dev(host->mmc)->of_node,
  2888. "sdhci-caps", &dt_caps);
  2889. v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
  2890. host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
  2891. if (host->quirks & SDHCI_QUIRK_MISSING_CAPS)
  2892. return;
  2893. if (caps) {
  2894. host->caps = *caps;
  2895. } else {
  2896. host->caps = sdhci_readl(host, SDHCI_CAPABILITIES);
  2897. host->caps &= ~lower_32_bits(dt_caps_mask);
  2898. host->caps |= lower_32_bits(dt_caps);
  2899. }
  2900. if (host->version < SDHCI_SPEC_300)
  2901. return;
  2902. if (caps1) {
  2903. host->caps1 = *caps1;
  2904. } else {
  2905. host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
  2906. host->caps1 &= ~upper_32_bits(dt_caps_mask);
  2907. host->caps1 |= upper_32_bits(dt_caps);
  2908. }
  2909. }
  2910. EXPORT_SYMBOL_GPL(__sdhci_read_caps);
  2911. static int sdhci_allocate_bounce_buffer(struct sdhci_host *host)
  2912. {
  2913. struct mmc_host *mmc = host->mmc;
  2914. unsigned int max_blocks;
  2915. unsigned int bounce_size;
  2916. int ret;
  2917. /*
  2918. * Cap the bounce buffer at 64KB. Using a bigger bounce buffer
  2919. * has diminishing returns, this is probably because SD/MMC
  2920. * cards are usually optimized to handle this size of requests.
  2921. */
  2922. bounce_size = SZ_64K;
  2923. /*
  2924. * Adjust downwards to maximum request size if this is less
  2925. * than our segment size, else hammer down the maximum
  2926. * request size to the maximum buffer size.
  2927. */
  2928. if (mmc->max_req_size < bounce_size)
  2929. bounce_size = mmc->max_req_size;
  2930. max_blocks = bounce_size / 512;
  2931. /*
  2932. * When we just support one segment, we can get significant
  2933. * speedups by the help of a bounce buffer to group scattered
  2934. * reads/writes together.
  2935. */
  2936. host->bounce_buffer = devm_kmalloc(mmc->parent,
  2937. bounce_size,
  2938. GFP_KERNEL);
  2939. if (!host->bounce_buffer) {
  2940. pr_err("%s: failed to allocate %u bytes for bounce buffer, falling back to single segments\n",
  2941. mmc_hostname(mmc),
  2942. bounce_size);
  2943. /*
  2944. * Exiting with zero here makes sure we proceed with
  2945. * mmc->max_segs == 1.
  2946. */
  2947. return 0;
  2948. }
  2949. host->bounce_addr = dma_map_single(mmc->parent,
  2950. host->bounce_buffer,
  2951. bounce_size,
  2952. DMA_BIDIRECTIONAL);
  2953. ret = dma_mapping_error(mmc->parent, host->bounce_addr);
  2954. if (ret)
  2955. /* Again fall back to max_segs == 1 */
  2956. return 0;
  2957. host->bounce_buffer_size = bounce_size;
  2958. /* Lie about this since we're bouncing */
  2959. mmc->max_segs = max_blocks;
  2960. mmc->max_seg_size = bounce_size;
  2961. mmc->max_req_size = bounce_size;
  2962. pr_info("%s bounce up to %u segments into one, max segment size %u bytes\n",
  2963. mmc_hostname(mmc), max_blocks, bounce_size);
  2964. return 0;
  2965. }
  2966. static inline bool sdhci_can_64bit_dma(struct sdhci_host *host)
  2967. {
  2968. /*
  2969. * According to SD Host Controller spec v4.10, bit[27] added from
  2970. * version 4.10 in Capabilities Register is used as 64-bit System
  2971. * Address support for V4 mode.
  2972. */
  2973. if (host->version >= SDHCI_SPEC_410 && host->v4_mode)
  2974. return host->caps & SDHCI_CAN_64BIT_V4;
  2975. return host->caps & SDHCI_CAN_64BIT;
  2976. }
  2977. int sdhci_setup_host(struct sdhci_host *host)
  2978. {
  2979. struct mmc_host *mmc;
  2980. u32 max_current_caps;
  2981. unsigned int ocr_avail;
  2982. unsigned int override_timeout_clk;
  2983. u32 max_clk;
  2984. int ret;
  2985. WARN_ON(host == NULL);
  2986. if (host == NULL)
  2987. return -EINVAL;
  2988. mmc = host->mmc;
  2989. /*
  2990. * If there are external regulators, get them. Note this must be done
  2991. * early before resetting the host and reading the capabilities so that
  2992. * the host can take the appropriate action if regulators are not
  2993. * available.
  2994. */
  2995. ret = mmc_regulator_get_supply(mmc);
  2996. if (ret)
  2997. return ret;
  2998. DBG("Version: 0x%08x | Present: 0x%08x\n",
  2999. sdhci_readw(host, SDHCI_HOST_VERSION),
  3000. sdhci_readl(host, SDHCI_PRESENT_STATE));
  3001. DBG("Caps: 0x%08x | Caps_1: 0x%08x\n",
  3002. sdhci_readl(host, SDHCI_CAPABILITIES),
  3003. sdhci_readl(host, SDHCI_CAPABILITIES_1));
  3004. sdhci_read_caps(host);
  3005. override_timeout_clk = host->timeout_clk;
  3006. if (host->version > SDHCI_SPEC_420) {
  3007. pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
  3008. mmc_hostname(mmc), host->version);
  3009. }
  3010. if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
  3011. host->flags |= SDHCI_USE_SDMA;
  3012. else if (!(host->caps & SDHCI_CAN_DO_SDMA))
  3013. DBG("Controller doesn't have SDMA capability\n");
  3014. else
  3015. host->flags |= SDHCI_USE_SDMA;
  3016. if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
  3017. (host->flags & SDHCI_USE_SDMA)) {
  3018. DBG("Disabling DMA as it is marked broken\n");
  3019. host->flags &= ~SDHCI_USE_SDMA;
  3020. }
  3021. if ((host->version >= SDHCI_SPEC_200) &&
  3022. (host->caps & SDHCI_CAN_DO_ADMA2))
  3023. host->flags |= SDHCI_USE_ADMA;
  3024. if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
  3025. (host->flags & SDHCI_USE_ADMA)) {
  3026. DBG("Disabling ADMA as it is marked broken\n");
  3027. host->flags &= ~SDHCI_USE_ADMA;
  3028. }
  3029. /*
  3030. * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
  3031. * and *must* do 64-bit DMA. A driver has the opportunity to change
  3032. * that during the first call to ->enable_dma(). Similarly
  3033. * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
  3034. * implement.
  3035. */
  3036. if (sdhci_can_64bit_dma(host))
  3037. host->flags |= SDHCI_USE_64_BIT_DMA;
  3038. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  3039. ret = sdhci_set_dma_mask(host);
  3040. if (!ret && host->ops->enable_dma)
  3041. ret = host->ops->enable_dma(host);
  3042. if (ret) {
  3043. pr_warn("%s: No suitable DMA available - falling back to PIO\n",
  3044. mmc_hostname(mmc));
  3045. host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
  3046. ret = 0;
  3047. }
  3048. }
  3049. /* SDMA does not support 64-bit DMA if v4 mode not set */
  3050. if ((host->flags & SDHCI_USE_64_BIT_DMA) && !host->v4_mode)
  3051. host->flags &= ~SDHCI_USE_SDMA;
  3052. if (host->flags & SDHCI_USE_ADMA) {
  3053. dma_addr_t dma;
  3054. void *buf;
  3055. if (host->flags & SDHCI_USE_64_BIT_DMA) {
  3056. host->adma_table_sz = host->adma_table_cnt *
  3057. SDHCI_ADMA2_64_DESC_SZ(host);
  3058. host->desc_sz = SDHCI_ADMA2_64_DESC_SZ(host);
  3059. } else {
  3060. host->adma_table_sz = host->adma_table_cnt *
  3061. SDHCI_ADMA2_32_DESC_SZ;
  3062. host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
  3063. }
  3064. host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
  3065. /*
  3066. * Use zalloc to zero the reserved high 32-bits of 128-bit
  3067. * descriptors so that they never need to be written.
  3068. */
  3069. buf = dma_zalloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
  3070. host->adma_table_sz, &dma, GFP_KERNEL);
  3071. if (!buf) {
  3072. pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
  3073. mmc_hostname(mmc));
  3074. host->flags &= ~SDHCI_USE_ADMA;
  3075. } else if ((dma + host->align_buffer_sz) &
  3076. (SDHCI_ADMA2_DESC_ALIGN - 1)) {
  3077. pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
  3078. mmc_hostname(mmc));
  3079. host->flags &= ~SDHCI_USE_ADMA;
  3080. dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
  3081. host->adma_table_sz, buf, dma);
  3082. } else {
  3083. host->align_buffer = buf;
  3084. host->align_addr = dma;
  3085. host->adma_table = buf + host->align_buffer_sz;
  3086. host->adma_addr = dma + host->align_buffer_sz;
  3087. }
  3088. }
  3089. /*
  3090. * If we use DMA, then it's up to the caller to set the DMA
  3091. * mask, but PIO does not need the hw shim so we set a new
  3092. * mask here in that case.
  3093. */
  3094. if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
  3095. host->dma_mask = DMA_BIT_MASK(64);
  3096. mmc_dev(mmc)->dma_mask = &host->dma_mask;
  3097. }
  3098. if (host->version >= SDHCI_SPEC_300)
  3099. host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK)
  3100. >> SDHCI_CLOCK_BASE_SHIFT;
  3101. else
  3102. host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK)
  3103. >> SDHCI_CLOCK_BASE_SHIFT;
  3104. host->max_clk *= 1000000;
  3105. if (host->max_clk == 0 || host->quirks &
  3106. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
  3107. if (!host->ops->get_max_clock) {
  3108. pr_err("%s: Hardware doesn't specify base clock frequency.\n",
  3109. mmc_hostname(mmc));
  3110. ret = -ENODEV;
  3111. goto undma;
  3112. }
  3113. host->max_clk = host->ops->get_max_clock(host);
  3114. }
  3115. /*
  3116. * In case of Host Controller v3.00, find out whether clock
  3117. * multiplier is supported.
  3118. */
  3119. host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >>
  3120. SDHCI_CLOCK_MUL_SHIFT;
  3121. /*
  3122. * In case the value in Clock Multiplier is 0, then programmable
  3123. * clock mode is not supported, otherwise the actual clock
  3124. * multiplier is one more than the value of Clock Multiplier
  3125. * in the Capabilities Register.
  3126. */
  3127. if (host->clk_mul)
  3128. host->clk_mul += 1;
  3129. /*
  3130. * Set host parameters.
  3131. */
  3132. max_clk = host->max_clk;
  3133. if (host->ops->get_min_clock)
  3134. mmc->f_min = host->ops->get_min_clock(host);
  3135. else if (host->version >= SDHCI_SPEC_300) {
  3136. if (host->clk_mul) {
  3137. mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
  3138. max_clk = host->max_clk * host->clk_mul;
  3139. } else
  3140. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
  3141. } else
  3142. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
  3143. if (!mmc->f_max || mmc->f_max > max_clk)
  3144. mmc->f_max = max_clk;
  3145. if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
  3146. host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >>
  3147. SDHCI_TIMEOUT_CLK_SHIFT;
  3148. if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
  3149. host->timeout_clk *= 1000;
  3150. if (host->timeout_clk == 0) {
  3151. if (!host->ops->get_timeout_clock) {
  3152. pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
  3153. mmc_hostname(mmc));
  3154. ret = -ENODEV;
  3155. goto undma;
  3156. }
  3157. host->timeout_clk =
  3158. DIV_ROUND_UP(host->ops->get_timeout_clock(host),
  3159. 1000);
  3160. }
  3161. if (override_timeout_clk)
  3162. host->timeout_clk = override_timeout_clk;
  3163. mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
  3164. host->ops->get_max_timeout_count(host) : 1 << 27;
  3165. mmc->max_busy_timeout /= host->timeout_clk;
  3166. }
  3167. if (host->quirks2 & SDHCI_QUIRK2_DISABLE_HW_TIMEOUT &&
  3168. !host->ops->get_max_timeout_count)
  3169. mmc->max_busy_timeout = 0;
  3170. mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
  3171. mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
  3172. if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
  3173. host->flags |= SDHCI_AUTO_CMD12;
  3174. /*
  3175. * For v3 mode, Auto-CMD23 stuff only works in ADMA or PIO.
  3176. * For v4 mode, SDMA may use Auto-CMD23 as well.
  3177. */
  3178. if ((host->version >= SDHCI_SPEC_300) &&
  3179. ((host->flags & SDHCI_USE_ADMA) ||
  3180. !(host->flags & SDHCI_USE_SDMA) || host->v4_mode) &&
  3181. !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
  3182. host->flags |= SDHCI_AUTO_CMD23;
  3183. DBG("Auto-CMD23 available\n");
  3184. } else {
  3185. DBG("Auto-CMD23 unavailable\n");
  3186. }
  3187. /*
  3188. * A controller may support 8-bit width, but the board itself
  3189. * might not have the pins brought out. Boards that support
  3190. * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
  3191. * their platform code before calling sdhci_add_host(), and we
  3192. * won't assume 8-bit width for hosts without that CAP.
  3193. */
  3194. if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
  3195. mmc->caps |= MMC_CAP_4_BIT_DATA;
  3196. if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
  3197. mmc->caps &= ~MMC_CAP_CMD23;
  3198. if (host->caps & SDHCI_CAN_DO_HISPD)
  3199. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  3200. if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
  3201. mmc_card_is_removable(mmc) &&
  3202. mmc_gpio_get_cd(host->mmc) < 0)
  3203. mmc->caps |= MMC_CAP_NEEDS_POLL;
  3204. if (!IS_ERR(mmc->supply.vqmmc)) {
  3205. ret = regulator_enable(mmc->supply.vqmmc);
  3206. /* If vqmmc provides no 1.8V signalling, then there's no UHS */
  3207. if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
  3208. 1950000))
  3209. host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
  3210. SDHCI_SUPPORT_SDR50 |
  3211. SDHCI_SUPPORT_DDR50);
  3212. /* In eMMC case vqmmc might be a fixed 1.8V regulator */
  3213. if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 2700000,
  3214. 3600000))
  3215. host->flags &= ~SDHCI_SIGNALING_330;
  3216. if (ret) {
  3217. pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
  3218. mmc_hostname(mmc), ret);
  3219. mmc->supply.vqmmc = ERR_PTR(-EINVAL);
  3220. }
  3221. }
  3222. if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
  3223. host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
  3224. SDHCI_SUPPORT_DDR50);
  3225. /*
  3226. * The SDHCI controller in a SoC might support HS200/HS400
  3227. * (indicated using mmc-hs200-1_8v/mmc-hs400-1_8v dt property),
  3228. * but if the board is modeled such that the IO lines are not
  3229. * connected to 1.8v then HS200/HS400 cannot be supported.
  3230. * Disable HS200/HS400 if the board does not have 1.8v connected
  3231. * to the IO lines. (Applicable for other modes in 1.8v)
  3232. */
  3233. mmc->caps2 &= ~(MMC_CAP2_HSX00_1_8V | MMC_CAP2_HS400_ES);
  3234. mmc->caps &= ~(MMC_CAP_1_8V_DDR | MMC_CAP_UHS);
  3235. }
  3236. /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
  3237. if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
  3238. SDHCI_SUPPORT_DDR50))
  3239. mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
  3240. /* SDR104 supports also implies SDR50 support */
  3241. if (host->caps1 & SDHCI_SUPPORT_SDR104) {
  3242. mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
  3243. /* SD3.0: SDR104 is supported so (for eMMC) the caps2
  3244. * field can be promoted to support HS200.
  3245. */
  3246. if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
  3247. mmc->caps2 |= MMC_CAP2_HS200;
  3248. } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
  3249. mmc->caps |= MMC_CAP_UHS_SDR50;
  3250. }
  3251. if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
  3252. (host->caps1 & SDHCI_SUPPORT_HS400))
  3253. mmc->caps2 |= MMC_CAP2_HS400;
  3254. if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
  3255. (IS_ERR(mmc->supply.vqmmc) ||
  3256. !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
  3257. 1300000)))
  3258. mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
  3259. if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
  3260. !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
  3261. mmc->caps |= MMC_CAP_UHS_DDR50;
  3262. /* Does the host need tuning for SDR50? */
  3263. if (host->caps1 & SDHCI_USE_SDR50_TUNING)
  3264. host->flags |= SDHCI_SDR50_NEEDS_TUNING;
  3265. /* Driver Type(s) (A, C, D) supported by the host */
  3266. if (host->caps1 & SDHCI_DRIVER_TYPE_A)
  3267. mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
  3268. if (host->caps1 & SDHCI_DRIVER_TYPE_C)
  3269. mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
  3270. if (host->caps1 & SDHCI_DRIVER_TYPE_D)
  3271. mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
  3272. /* Initial value for re-tuning timer count */
  3273. host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
  3274. SDHCI_RETUNING_TIMER_COUNT_SHIFT;
  3275. /*
  3276. * In case Re-tuning Timer is not disabled, the actual value of
  3277. * re-tuning timer will be 2 ^ (n - 1).
  3278. */
  3279. if (host->tuning_count)
  3280. host->tuning_count = 1 << (host->tuning_count - 1);
  3281. /* Re-tuning mode supported by the Host Controller */
  3282. host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >>
  3283. SDHCI_RETUNING_MODE_SHIFT;
  3284. ocr_avail = 0;
  3285. /*
  3286. * According to SD Host Controller spec v3.00, if the Host System
  3287. * can afford more than 150mA, Host Driver should set XPC to 1. Also
  3288. * the value is meaningful only if Voltage Support in the Capabilities
  3289. * register is set. The actual current value is 4 times the register
  3290. * value.
  3291. */
  3292. max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
  3293. if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
  3294. int curr = regulator_get_current_limit(mmc->supply.vmmc);
  3295. if (curr > 0) {
  3296. /* convert to SDHCI_MAX_CURRENT format */
  3297. curr = curr/1000; /* convert to mA */
  3298. curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
  3299. curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
  3300. max_current_caps =
  3301. (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
  3302. (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
  3303. (curr << SDHCI_MAX_CURRENT_180_SHIFT);
  3304. }
  3305. }
  3306. if (host->caps & SDHCI_CAN_VDD_330) {
  3307. ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
  3308. mmc->max_current_330 = ((max_current_caps &
  3309. SDHCI_MAX_CURRENT_330_MASK) >>
  3310. SDHCI_MAX_CURRENT_330_SHIFT) *
  3311. SDHCI_MAX_CURRENT_MULTIPLIER;
  3312. }
  3313. if (host->caps & SDHCI_CAN_VDD_300) {
  3314. ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
  3315. mmc->max_current_300 = ((max_current_caps &
  3316. SDHCI_MAX_CURRENT_300_MASK) >>
  3317. SDHCI_MAX_CURRENT_300_SHIFT) *
  3318. SDHCI_MAX_CURRENT_MULTIPLIER;
  3319. }
  3320. if (host->caps & SDHCI_CAN_VDD_180) {
  3321. ocr_avail |= MMC_VDD_165_195;
  3322. mmc->max_current_180 = ((max_current_caps &
  3323. SDHCI_MAX_CURRENT_180_MASK) >>
  3324. SDHCI_MAX_CURRENT_180_SHIFT) *
  3325. SDHCI_MAX_CURRENT_MULTIPLIER;
  3326. }
  3327. /* If OCR set by host, use it instead. */
  3328. if (host->ocr_mask)
  3329. ocr_avail = host->ocr_mask;
  3330. /* If OCR set by external regulators, give it highest prio. */
  3331. if (mmc->ocr_avail)
  3332. ocr_avail = mmc->ocr_avail;
  3333. mmc->ocr_avail = ocr_avail;
  3334. mmc->ocr_avail_sdio = ocr_avail;
  3335. if (host->ocr_avail_sdio)
  3336. mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
  3337. mmc->ocr_avail_sd = ocr_avail;
  3338. if (host->ocr_avail_sd)
  3339. mmc->ocr_avail_sd &= host->ocr_avail_sd;
  3340. else /* normal SD controllers don't support 1.8V */
  3341. mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
  3342. mmc->ocr_avail_mmc = ocr_avail;
  3343. if (host->ocr_avail_mmc)
  3344. mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
  3345. if (mmc->ocr_avail == 0) {
  3346. pr_err("%s: Hardware doesn't report any support voltages.\n",
  3347. mmc_hostname(mmc));
  3348. ret = -ENODEV;
  3349. goto unreg;
  3350. }
  3351. if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
  3352. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
  3353. MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
  3354. (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
  3355. host->flags |= SDHCI_SIGNALING_180;
  3356. if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
  3357. host->flags |= SDHCI_SIGNALING_120;
  3358. spin_lock_init(&host->lock);
  3359. /*
  3360. * Maximum number of sectors in one transfer. Limited by SDMA boundary
  3361. * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
  3362. * is less anyway.
  3363. */
  3364. mmc->max_req_size = 524288;
  3365. /*
  3366. * Maximum number of segments. Depends on if the hardware
  3367. * can do scatter/gather or not.
  3368. */
  3369. if (host->flags & SDHCI_USE_ADMA) {
  3370. mmc->max_segs = SDHCI_MAX_SEGS;
  3371. } else if (host->flags & SDHCI_USE_SDMA) {
  3372. mmc->max_segs = 1;
  3373. if (swiotlb_max_segment()) {
  3374. unsigned int max_req_size = (1 << IO_TLB_SHIFT) *
  3375. IO_TLB_SEGSIZE;
  3376. mmc->max_req_size = min(mmc->max_req_size,
  3377. max_req_size);
  3378. }
  3379. } else { /* PIO */
  3380. mmc->max_segs = SDHCI_MAX_SEGS;
  3381. }
  3382. /*
  3383. * Maximum segment size. Could be one segment with the maximum number
  3384. * of bytes. When doing hardware scatter/gather, each entry cannot
  3385. * be larger than 64 KiB though.
  3386. */
  3387. if (host->flags & SDHCI_USE_ADMA) {
  3388. if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
  3389. mmc->max_seg_size = 65535;
  3390. else
  3391. mmc->max_seg_size = 65536;
  3392. } else {
  3393. mmc->max_seg_size = mmc->max_req_size;
  3394. }
  3395. /*
  3396. * Maximum block size. This varies from controller to controller and
  3397. * is specified in the capabilities register.
  3398. */
  3399. if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
  3400. mmc->max_blk_size = 2;
  3401. } else {
  3402. mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
  3403. SDHCI_MAX_BLOCK_SHIFT;
  3404. if (mmc->max_blk_size >= 3) {
  3405. pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
  3406. mmc_hostname(mmc));
  3407. mmc->max_blk_size = 0;
  3408. }
  3409. }
  3410. mmc->max_blk_size = 512 << mmc->max_blk_size;
  3411. /*
  3412. * Maximum block count.
  3413. */
  3414. mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
  3415. if (mmc->max_segs == 1) {
  3416. /* This may alter mmc->*_blk_* parameters */
  3417. ret = sdhci_allocate_bounce_buffer(host);
  3418. if (ret)
  3419. return ret;
  3420. }
  3421. return 0;
  3422. unreg:
  3423. if (!IS_ERR(mmc->supply.vqmmc))
  3424. regulator_disable(mmc->supply.vqmmc);
  3425. undma:
  3426. if (host->align_buffer)
  3427. dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
  3428. host->adma_table_sz, host->align_buffer,
  3429. host->align_addr);
  3430. host->adma_table = NULL;
  3431. host->align_buffer = NULL;
  3432. return ret;
  3433. }
  3434. EXPORT_SYMBOL_GPL(sdhci_setup_host);
  3435. void sdhci_cleanup_host(struct sdhci_host *host)
  3436. {
  3437. struct mmc_host *mmc = host->mmc;
  3438. if (!IS_ERR(mmc->supply.vqmmc))
  3439. regulator_disable(mmc->supply.vqmmc);
  3440. if (host->align_buffer)
  3441. dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
  3442. host->adma_table_sz, host->align_buffer,
  3443. host->align_addr);
  3444. host->adma_table = NULL;
  3445. host->align_buffer = NULL;
  3446. }
  3447. EXPORT_SYMBOL_GPL(sdhci_cleanup_host);
  3448. int __sdhci_add_host(struct sdhci_host *host)
  3449. {
  3450. struct mmc_host *mmc = host->mmc;
  3451. int ret;
  3452. /*
  3453. * Init tasklets.
  3454. */
  3455. tasklet_init(&host->finish_tasklet,
  3456. sdhci_tasklet_finish, (unsigned long)host);
  3457. timer_setup(&host->timer, sdhci_timeout_timer, 0);
  3458. timer_setup(&host->data_timer, sdhci_timeout_data_timer, 0);
  3459. init_waitqueue_head(&host->buf_ready_int);
  3460. sdhci_init(host, 0);
  3461. ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
  3462. IRQF_SHARED, mmc_hostname(mmc), host);
  3463. if (ret) {
  3464. pr_err("%s: Failed to request IRQ %d: %d\n",
  3465. mmc_hostname(mmc), host->irq, ret);
  3466. goto untasklet;
  3467. }
  3468. ret = sdhci_led_register(host);
  3469. if (ret) {
  3470. pr_err("%s: Failed to register LED device: %d\n",
  3471. mmc_hostname(mmc), ret);
  3472. goto unirq;
  3473. }
  3474. mmiowb();
  3475. ret = mmc_add_host(mmc);
  3476. if (ret)
  3477. goto unled;
  3478. pr_info("%s: SDHCI controller on %s [%s] using %s\n",
  3479. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  3480. (host->flags & SDHCI_USE_ADMA) ?
  3481. (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
  3482. (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
  3483. sdhci_enable_card_detection(host);
  3484. return 0;
  3485. unled:
  3486. sdhci_led_unregister(host);
  3487. unirq:
  3488. sdhci_do_reset(host, SDHCI_RESET_ALL);
  3489. sdhci_writel(host, 0, SDHCI_INT_ENABLE);
  3490. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  3491. free_irq(host->irq, host);
  3492. untasklet:
  3493. tasklet_kill(&host->finish_tasklet);
  3494. return ret;
  3495. }
  3496. EXPORT_SYMBOL_GPL(__sdhci_add_host);
  3497. int sdhci_add_host(struct sdhci_host *host)
  3498. {
  3499. int ret;
  3500. ret = sdhci_setup_host(host);
  3501. if (ret)
  3502. return ret;
  3503. ret = __sdhci_add_host(host);
  3504. if (ret)
  3505. goto cleanup;
  3506. return 0;
  3507. cleanup:
  3508. sdhci_cleanup_host(host);
  3509. return ret;
  3510. }
  3511. EXPORT_SYMBOL_GPL(sdhci_add_host);
  3512. void sdhci_remove_host(struct sdhci_host *host, int dead)
  3513. {
  3514. struct mmc_host *mmc = host->mmc;
  3515. unsigned long flags;
  3516. if (dead) {
  3517. spin_lock_irqsave(&host->lock, flags);
  3518. host->flags |= SDHCI_DEVICE_DEAD;
  3519. if (sdhci_has_requests(host)) {
  3520. pr_err("%s: Controller removed during "
  3521. " transfer!\n", mmc_hostname(mmc));
  3522. sdhci_error_out_mrqs(host, -ENOMEDIUM);
  3523. }
  3524. spin_unlock_irqrestore(&host->lock, flags);
  3525. }
  3526. sdhci_disable_card_detection(host);
  3527. mmc_remove_host(mmc);
  3528. sdhci_led_unregister(host);
  3529. if (!dead)
  3530. sdhci_do_reset(host, SDHCI_RESET_ALL);
  3531. sdhci_writel(host, 0, SDHCI_INT_ENABLE);
  3532. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  3533. free_irq(host->irq, host);
  3534. del_timer_sync(&host->timer);
  3535. del_timer_sync(&host->data_timer);
  3536. tasklet_kill(&host->finish_tasklet);
  3537. if (!IS_ERR(mmc->supply.vqmmc))
  3538. regulator_disable(mmc->supply.vqmmc);
  3539. if (host->align_buffer)
  3540. dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
  3541. host->adma_table_sz, host->align_buffer,
  3542. host->align_addr);
  3543. host->adma_table = NULL;
  3544. host->align_buffer = NULL;
  3545. }
  3546. EXPORT_SYMBOL_GPL(sdhci_remove_host);
  3547. void sdhci_free_host(struct sdhci_host *host)
  3548. {
  3549. mmc_free_host(host->mmc);
  3550. }
  3551. EXPORT_SYMBOL_GPL(sdhci_free_host);
  3552. /*****************************************************************************\
  3553. * *
  3554. * Driver init/exit *
  3555. * *
  3556. \*****************************************************************************/
  3557. static int __init sdhci_drv_init(void)
  3558. {
  3559. pr_info(DRIVER_NAME
  3560. ": Secure Digital Host Controller Interface driver\n");
  3561. pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  3562. return 0;
  3563. }
  3564. static void __exit sdhci_drv_exit(void)
  3565. {
  3566. }
  3567. module_init(sdhci_drv_init);
  3568. module_exit(sdhci_drv_exit);
  3569. module_param(debug_quirks, uint, 0444);
  3570. module_param(debug_quirks2, uint, 0444);
  3571. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  3572. MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
  3573. MODULE_LICENSE("GPL");
  3574. MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
  3575. MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");