sdhci.c 88 KB

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