sdhci.c 110 KB

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