sdhci.c 98 KB

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