sdhci.c 86 KB

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