sdhci.c 82 KB

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