sdhci.c 85 KB

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