sdhci.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171
  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_start_signal_voltage_switch(struct sdhci_host *host,
  1318. struct mmc_ios *ios)
  1319. {
  1320. u8 pwr;
  1321. u16 clk, ctrl;
  1322. u32 present_state;
  1323. /*
  1324. * Signal Voltage Switching is only applicable for Host Controllers
  1325. * v3.00 and above.
  1326. */
  1327. if (host->version < SDHCI_SPEC_300)
  1328. return 0;
  1329. /*
  1330. * We first check whether the request is to set signalling voltage
  1331. * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
  1332. */
  1333. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1334. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
  1335. /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
  1336. ctrl &= ~SDHCI_CTRL_VDD_180;
  1337. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1338. /* Wait for 5ms */
  1339. usleep_range(5000, 5500);
  1340. /* 3.3V regulator output should be stable within 5 ms */
  1341. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1342. if (!(ctrl & SDHCI_CTRL_VDD_180))
  1343. return 0;
  1344. else {
  1345. pr_info(DRIVER_NAME ": Switching to 3.3V "
  1346. "signalling voltage failed\n");
  1347. return -EIO;
  1348. }
  1349. } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
  1350. (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
  1351. /* Stop SDCLK */
  1352. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1353. clk &= ~SDHCI_CLOCK_CARD_EN;
  1354. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1355. /* Check whether DAT[3:0] is 0000 */
  1356. present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
  1357. if (!((present_state & SDHCI_DATA_LVL_MASK) >>
  1358. SDHCI_DATA_LVL_SHIFT)) {
  1359. /*
  1360. * Enable 1.8V Signal Enable in the Host Control2
  1361. * register
  1362. */
  1363. ctrl |= SDHCI_CTRL_VDD_180;
  1364. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1365. /* Wait for 5ms */
  1366. usleep_range(5000, 5500);
  1367. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1368. if (ctrl & SDHCI_CTRL_VDD_180) {
  1369. /* Provide SDCLK again and wait for 1ms*/
  1370. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1371. clk |= SDHCI_CLOCK_CARD_EN;
  1372. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1373. usleep_range(1000, 1500);
  1374. /*
  1375. * If DAT[3:0] level is 1111b, then the card
  1376. * was successfully switched to 1.8V signaling.
  1377. */
  1378. present_state = sdhci_readl(host,
  1379. SDHCI_PRESENT_STATE);
  1380. if ((present_state & SDHCI_DATA_LVL_MASK) ==
  1381. SDHCI_DATA_LVL_MASK)
  1382. return 0;
  1383. }
  1384. }
  1385. /*
  1386. * If we are here, that means the switch to 1.8V signaling
  1387. * failed. We power cycle the card, and retry initialization
  1388. * sequence by setting S18R to 0.
  1389. */
  1390. pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
  1391. pwr &= ~SDHCI_POWER_ON;
  1392. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  1393. if (host->vmmc)
  1394. regulator_disable(host->vmmc);
  1395. /* Wait for 1ms as per the spec */
  1396. usleep_range(1000, 1500);
  1397. pwr |= SDHCI_POWER_ON;
  1398. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  1399. if (host->vmmc)
  1400. regulator_enable(host->vmmc);
  1401. pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
  1402. "voltage failed, retrying with S18R set to 0\n");
  1403. return -EAGAIN;
  1404. } else
  1405. /* No signal voltage switch required */
  1406. return 0;
  1407. }
  1408. static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
  1409. struct mmc_ios *ios)
  1410. {
  1411. struct sdhci_host *host = mmc_priv(mmc);
  1412. int err;
  1413. if (host->version < SDHCI_SPEC_300)
  1414. return 0;
  1415. sdhci_runtime_pm_get(host);
  1416. err = sdhci_do_start_signal_voltage_switch(host, ios);
  1417. sdhci_runtime_pm_put(host);
  1418. return err;
  1419. }
  1420. static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
  1421. {
  1422. struct sdhci_host *host;
  1423. u16 ctrl;
  1424. u32 ier;
  1425. int tuning_loop_counter = MAX_TUNING_LOOP;
  1426. unsigned long timeout;
  1427. int err = 0;
  1428. bool requires_tuning_nonuhs = false;
  1429. host = mmc_priv(mmc);
  1430. sdhci_runtime_pm_get(host);
  1431. disable_irq(host->irq);
  1432. spin_lock(&host->lock);
  1433. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1434. /*
  1435. * The Host Controller needs tuning only in case of SDR104 mode
  1436. * and for SDR50 mode when Use Tuning for SDR50 is set in the
  1437. * Capabilities register.
  1438. * If the Host Controller supports the HS200 mode then the
  1439. * tuning function has to be executed.
  1440. */
  1441. if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
  1442. (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
  1443. host->flags & SDHCI_HS200_NEEDS_TUNING))
  1444. requires_tuning_nonuhs = true;
  1445. if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
  1446. requires_tuning_nonuhs)
  1447. ctrl |= SDHCI_CTRL_EXEC_TUNING;
  1448. else {
  1449. spin_unlock(&host->lock);
  1450. enable_irq(host->irq);
  1451. sdhci_runtime_pm_put(host);
  1452. return 0;
  1453. }
  1454. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1455. /*
  1456. * As per the Host Controller spec v3.00, tuning command
  1457. * generates Buffer Read Ready interrupt, so enable that.
  1458. *
  1459. * Note: The spec clearly says that when tuning sequence
  1460. * is being performed, the controller does not generate
  1461. * interrupts other than Buffer Read Ready interrupt. But
  1462. * to make sure we don't hit a controller bug, we _only_
  1463. * enable Buffer Read Ready interrupt here.
  1464. */
  1465. ier = sdhci_readl(host, SDHCI_INT_ENABLE);
  1466. sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
  1467. /*
  1468. * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
  1469. * of loops reaches 40 times or a timeout of 150ms occurs.
  1470. */
  1471. timeout = 150;
  1472. do {
  1473. struct mmc_command cmd = {0};
  1474. struct mmc_request mrq = {NULL};
  1475. if (!tuning_loop_counter && !timeout)
  1476. break;
  1477. cmd.opcode = opcode;
  1478. cmd.arg = 0;
  1479. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  1480. cmd.retries = 0;
  1481. cmd.data = NULL;
  1482. cmd.error = 0;
  1483. mrq.cmd = &cmd;
  1484. host->mrq = &mrq;
  1485. /*
  1486. * In response to CMD19, the card sends 64 bytes of tuning
  1487. * block to the Host Controller. So we set the block size
  1488. * to 64 here.
  1489. */
  1490. if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
  1491. if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
  1492. sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
  1493. SDHCI_BLOCK_SIZE);
  1494. else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
  1495. sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
  1496. SDHCI_BLOCK_SIZE);
  1497. } else {
  1498. sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
  1499. SDHCI_BLOCK_SIZE);
  1500. }
  1501. /*
  1502. * The tuning block is sent by the card to the host controller.
  1503. * So we set the TRNS_READ bit in the Transfer Mode register.
  1504. * This also takes care of setting DMA Enable and Multi Block
  1505. * Select in the same register to 0.
  1506. */
  1507. sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
  1508. sdhci_send_command(host, &cmd);
  1509. host->cmd = NULL;
  1510. host->mrq = NULL;
  1511. spin_unlock(&host->lock);
  1512. enable_irq(host->irq);
  1513. /* Wait for Buffer Read Ready interrupt */
  1514. wait_event_interruptible_timeout(host->buf_ready_int,
  1515. (host->tuning_done == 1),
  1516. msecs_to_jiffies(50));
  1517. disable_irq(host->irq);
  1518. spin_lock(&host->lock);
  1519. if (!host->tuning_done) {
  1520. pr_info(DRIVER_NAME ": Timeout waiting for "
  1521. "Buffer Read Ready interrupt during tuning "
  1522. "procedure, falling back to fixed sampling "
  1523. "clock\n");
  1524. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1525. ctrl &= ~SDHCI_CTRL_TUNED_CLK;
  1526. ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
  1527. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1528. err = -EIO;
  1529. goto out;
  1530. }
  1531. host->tuning_done = 0;
  1532. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1533. tuning_loop_counter--;
  1534. timeout--;
  1535. mdelay(1);
  1536. } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
  1537. /*
  1538. * The Host Driver has exhausted the maximum number of loops allowed,
  1539. * so use fixed sampling frequency.
  1540. */
  1541. if (!tuning_loop_counter || !timeout) {
  1542. ctrl &= ~SDHCI_CTRL_TUNED_CLK;
  1543. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1544. } else {
  1545. if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
  1546. pr_info(DRIVER_NAME ": Tuning procedure"
  1547. " failed, falling back to fixed sampling"
  1548. " clock\n");
  1549. err = -EIO;
  1550. }
  1551. }
  1552. out:
  1553. /*
  1554. * If this is the very first time we are here, we start the retuning
  1555. * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
  1556. * flag won't be set, we check this condition before actually starting
  1557. * the timer.
  1558. */
  1559. if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
  1560. (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
  1561. host->flags |= SDHCI_USING_RETUNING_TIMER;
  1562. mod_timer(&host->tuning_timer, jiffies +
  1563. host->tuning_count * HZ);
  1564. /* Tuning mode 1 limits the maximum data length to 4MB */
  1565. mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
  1566. } else {
  1567. host->flags &= ~SDHCI_NEEDS_RETUNING;
  1568. /* Reload the new initial value for timer */
  1569. if (host->tuning_mode == SDHCI_TUNING_MODE_1)
  1570. mod_timer(&host->tuning_timer, jiffies +
  1571. host->tuning_count * HZ);
  1572. }
  1573. /*
  1574. * In case tuning fails, host controllers which support re-tuning can
  1575. * try tuning again at a later time, when the re-tuning timer expires.
  1576. * So for these controllers, we return 0. Since there might be other
  1577. * controllers who do not have this capability, we return error for
  1578. * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
  1579. * a retuning timer to do the retuning for the card.
  1580. */
  1581. if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
  1582. err = 0;
  1583. sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
  1584. spin_unlock(&host->lock);
  1585. enable_irq(host->irq);
  1586. sdhci_runtime_pm_put(host);
  1587. return err;
  1588. }
  1589. static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
  1590. {
  1591. u16 ctrl;
  1592. unsigned long flags;
  1593. /* Host Controller v3.00 defines preset value registers */
  1594. if (host->version < SDHCI_SPEC_300)
  1595. return;
  1596. spin_lock_irqsave(&host->lock, flags);
  1597. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1598. /*
  1599. * We only enable or disable Preset Value if they are not already
  1600. * enabled or disabled respectively. Otherwise, we bail out.
  1601. */
  1602. if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
  1603. ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
  1604. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1605. host->flags |= SDHCI_PV_ENABLED;
  1606. } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
  1607. ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
  1608. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1609. host->flags &= ~SDHCI_PV_ENABLED;
  1610. }
  1611. spin_unlock_irqrestore(&host->lock, flags);
  1612. }
  1613. static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
  1614. {
  1615. struct sdhci_host *host = mmc_priv(mmc);
  1616. sdhci_runtime_pm_get(host);
  1617. sdhci_do_enable_preset_value(host, enable);
  1618. sdhci_runtime_pm_put(host);
  1619. }
  1620. static const struct mmc_host_ops sdhci_ops = {
  1621. .request = sdhci_request,
  1622. .set_ios = sdhci_set_ios,
  1623. .get_ro = sdhci_get_ro,
  1624. .hw_reset = sdhci_hw_reset,
  1625. .enable_sdio_irq = sdhci_enable_sdio_irq,
  1626. .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
  1627. .execute_tuning = sdhci_execute_tuning,
  1628. .enable_preset_value = sdhci_enable_preset_value,
  1629. };
  1630. /*****************************************************************************\
  1631. * *
  1632. * Tasklets *
  1633. * *
  1634. \*****************************************************************************/
  1635. static void sdhci_tasklet_card(unsigned long param)
  1636. {
  1637. struct sdhci_host *host;
  1638. unsigned long flags;
  1639. host = (struct sdhci_host*)param;
  1640. spin_lock_irqsave(&host->lock, flags);
  1641. /* Check host->mrq first in case we are runtime suspended */
  1642. if (host->mrq &&
  1643. !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
  1644. pr_err("%s: Card removed during transfer!\n",
  1645. mmc_hostname(host->mmc));
  1646. pr_err("%s: Resetting controller.\n",
  1647. mmc_hostname(host->mmc));
  1648. sdhci_reset(host, SDHCI_RESET_CMD);
  1649. sdhci_reset(host, SDHCI_RESET_DATA);
  1650. host->mrq->cmd->error = -ENOMEDIUM;
  1651. tasklet_schedule(&host->finish_tasklet);
  1652. }
  1653. spin_unlock_irqrestore(&host->lock, flags);
  1654. mmc_detect_change(host->mmc, msecs_to_jiffies(200));
  1655. }
  1656. static void sdhci_tasklet_finish(unsigned long param)
  1657. {
  1658. struct sdhci_host *host;
  1659. unsigned long flags;
  1660. struct mmc_request *mrq;
  1661. host = (struct sdhci_host*)param;
  1662. spin_lock_irqsave(&host->lock, flags);
  1663. /*
  1664. * If this tasklet gets rescheduled while running, it will
  1665. * be run again afterwards but without any active request.
  1666. */
  1667. if (!host->mrq) {
  1668. spin_unlock_irqrestore(&host->lock, flags);
  1669. return;
  1670. }
  1671. del_timer(&host->timer);
  1672. mrq = host->mrq;
  1673. /*
  1674. * The controller needs a reset of internal state machines
  1675. * upon error conditions.
  1676. */
  1677. if (!(host->flags & SDHCI_DEVICE_DEAD) &&
  1678. ((mrq->cmd && mrq->cmd->error) ||
  1679. (mrq->data && (mrq->data->error ||
  1680. (mrq->data->stop && mrq->data->stop->error))) ||
  1681. (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
  1682. /* Some controllers need this kick or reset won't work here */
  1683. if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
  1684. unsigned int clock;
  1685. /* This is to force an update */
  1686. clock = host->clock;
  1687. host->clock = 0;
  1688. sdhci_set_clock(host, clock);
  1689. }
  1690. /* Spec says we should do both at the same time, but Ricoh
  1691. controllers do not like that. */
  1692. sdhci_reset(host, SDHCI_RESET_CMD);
  1693. sdhci_reset(host, SDHCI_RESET_DATA);
  1694. }
  1695. host->mrq = NULL;
  1696. host->cmd = NULL;
  1697. host->data = NULL;
  1698. #ifndef SDHCI_USE_LEDS_CLASS
  1699. sdhci_deactivate_led(host);
  1700. #endif
  1701. mmiowb();
  1702. spin_unlock_irqrestore(&host->lock, flags);
  1703. mmc_request_done(host->mmc, mrq);
  1704. sdhci_runtime_pm_put(host);
  1705. }
  1706. static void sdhci_timeout_timer(unsigned long data)
  1707. {
  1708. struct sdhci_host *host;
  1709. unsigned long flags;
  1710. host = (struct sdhci_host*)data;
  1711. spin_lock_irqsave(&host->lock, flags);
  1712. if (host->mrq) {
  1713. pr_err("%s: Timeout waiting for hardware "
  1714. "interrupt.\n", mmc_hostname(host->mmc));
  1715. sdhci_dumpregs(host);
  1716. if (host->data) {
  1717. host->data->error = -ETIMEDOUT;
  1718. sdhci_finish_data(host);
  1719. } else {
  1720. if (host->cmd)
  1721. host->cmd->error = -ETIMEDOUT;
  1722. else
  1723. host->mrq->cmd->error = -ETIMEDOUT;
  1724. tasklet_schedule(&host->finish_tasklet);
  1725. }
  1726. }
  1727. mmiowb();
  1728. spin_unlock_irqrestore(&host->lock, flags);
  1729. }
  1730. static void sdhci_tuning_timer(unsigned long data)
  1731. {
  1732. struct sdhci_host *host;
  1733. unsigned long flags;
  1734. host = (struct sdhci_host *)data;
  1735. spin_lock_irqsave(&host->lock, flags);
  1736. host->flags |= SDHCI_NEEDS_RETUNING;
  1737. spin_unlock_irqrestore(&host->lock, flags);
  1738. }
  1739. /*****************************************************************************\
  1740. * *
  1741. * Interrupt handling *
  1742. * *
  1743. \*****************************************************************************/
  1744. static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
  1745. {
  1746. BUG_ON(intmask == 0);
  1747. if (!host->cmd) {
  1748. pr_err("%s: Got command interrupt 0x%08x even "
  1749. "though no command operation was in progress.\n",
  1750. mmc_hostname(host->mmc), (unsigned)intmask);
  1751. sdhci_dumpregs(host);
  1752. return;
  1753. }
  1754. if (intmask & SDHCI_INT_TIMEOUT)
  1755. host->cmd->error = -ETIMEDOUT;
  1756. else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
  1757. SDHCI_INT_INDEX))
  1758. host->cmd->error = -EILSEQ;
  1759. if (host->cmd->error) {
  1760. tasklet_schedule(&host->finish_tasklet);
  1761. return;
  1762. }
  1763. /*
  1764. * The host can send and interrupt when the busy state has
  1765. * ended, allowing us to wait without wasting CPU cycles.
  1766. * Unfortunately this is overloaded on the "data complete"
  1767. * interrupt, so we need to take some care when handling
  1768. * it.
  1769. *
  1770. * Note: The 1.0 specification is a bit ambiguous about this
  1771. * feature so there might be some problems with older
  1772. * controllers.
  1773. */
  1774. if (host->cmd->flags & MMC_RSP_BUSY) {
  1775. if (host->cmd->data)
  1776. DBG("Cannot wait for busy signal when also "
  1777. "doing a data transfer");
  1778. else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
  1779. return;
  1780. /* The controller does not support the end-of-busy IRQ,
  1781. * fall through and take the SDHCI_INT_RESPONSE */
  1782. }
  1783. if (intmask & SDHCI_INT_RESPONSE)
  1784. sdhci_finish_command(host);
  1785. }
  1786. #ifdef CONFIG_MMC_DEBUG
  1787. static void sdhci_show_adma_error(struct sdhci_host *host)
  1788. {
  1789. const char *name = mmc_hostname(host->mmc);
  1790. u8 *desc = host->adma_desc;
  1791. __le32 *dma;
  1792. __le16 *len;
  1793. u8 attr;
  1794. sdhci_dumpregs(host);
  1795. while (true) {
  1796. dma = (__le32 *)(desc + 4);
  1797. len = (__le16 *)(desc + 2);
  1798. attr = *desc;
  1799. DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
  1800. name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
  1801. desc += 8;
  1802. if (attr & 2)
  1803. break;
  1804. }
  1805. }
  1806. #else
  1807. static void sdhci_show_adma_error(struct sdhci_host *host) { }
  1808. #endif
  1809. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  1810. {
  1811. u32 command;
  1812. BUG_ON(intmask == 0);
  1813. /* CMD19 generates _only_ Buffer Read Ready interrupt */
  1814. if (intmask & SDHCI_INT_DATA_AVAIL) {
  1815. command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
  1816. if (command == MMC_SEND_TUNING_BLOCK ||
  1817. command == MMC_SEND_TUNING_BLOCK_HS200) {
  1818. host->tuning_done = 1;
  1819. wake_up(&host->buf_ready_int);
  1820. return;
  1821. }
  1822. }
  1823. if (!host->data) {
  1824. /*
  1825. * The "data complete" interrupt is also used to
  1826. * indicate that a busy state has ended. See comment
  1827. * above in sdhci_cmd_irq().
  1828. */
  1829. if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
  1830. if (intmask & SDHCI_INT_DATA_END) {
  1831. sdhci_finish_command(host);
  1832. return;
  1833. }
  1834. }
  1835. pr_err("%s: Got data interrupt 0x%08x even "
  1836. "though no data operation was in progress.\n",
  1837. mmc_hostname(host->mmc), (unsigned)intmask);
  1838. sdhci_dumpregs(host);
  1839. return;
  1840. }
  1841. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  1842. host->data->error = -ETIMEDOUT;
  1843. else if (intmask & SDHCI_INT_DATA_END_BIT)
  1844. host->data->error = -EILSEQ;
  1845. else if ((intmask & SDHCI_INT_DATA_CRC) &&
  1846. SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
  1847. != MMC_BUS_TEST_R)
  1848. host->data->error = -EILSEQ;
  1849. else if (intmask & SDHCI_INT_ADMA_ERROR) {
  1850. pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
  1851. sdhci_show_adma_error(host);
  1852. host->data->error = -EIO;
  1853. }
  1854. if (host->data->error)
  1855. sdhci_finish_data(host);
  1856. else {
  1857. if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  1858. sdhci_transfer_pio(host);
  1859. /*
  1860. * We currently don't do anything fancy with DMA
  1861. * boundaries, but as we can't disable the feature
  1862. * we need to at least restart the transfer.
  1863. *
  1864. * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
  1865. * should return a valid address to continue from, but as
  1866. * some controllers are faulty, don't trust them.
  1867. */
  1868. if (intmask & SDHCI_INT_DMA_END) {
  1869. u32 dmastart, dmanow;
  1870. dmastart = sg_dma_address(host->data->sg);
  1871. dmanow = dmastart + host->data->bytes_xfered;
  1872. /*
  1873. * Force update to the next DMA block boundary.
  1874. */
  1875. dmanow = (dmanow &
  1876. ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
  1877. SDHCI_DEFAULT_BOUNDARY_SIZE;
  1878. host->data->bytes_xfered = dmanow - dmastart;
  1879. DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
  1880. " next 0x%08x\n",
  1881. mmc_hostname(host->mmc), dmastart,
  1882. host->data->bytes_xfered, dmanow);
  1883. sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
  1884. }
  1885. if (intmask & SDHCI_INT_DATA_END) {
  1886. if (host->cmd) {
  1887. /*
  1888. * Data managed to finish before the
  1889. * command completed. Make sure we do
  1890. * things in the proper order.
  1891. */
  1892. host->data_early = 1;
  1893. } else {
  1894. sdhci_finish_data(host);
  1895. }
  1896. }
  1897. }
  1898. }
  1899. static irqreturn_t sdhci_irq(int irq, void *dev_id)
  1900. {
  1901. irqreturn_t result;
  1902. struct sdhci_host *host = dev_id;
  1903. u32 intmask, unexpected = 0;
  1904. int cardint = 0, max_loops = 16;
  1905. spin_lock(&host->lock);
  1906. if (host->runtime_suspended) {
  1907. spin_unlock(&host->lock);
  1908. pr_warning("%s: got irq while runtime suspended\n",
  1909. mmc_hostname(host->mmc));
  1910. return IRQ_HANDLED;
  1911. }
  1912. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  1913. if (!intmask || intmask == 0xffffffff) {
  1914. result = IRQ_NONE;
  1915. goto out;
  1916. }
  1917. again:
  1918. DBG("*** %s got interrupt: 0x%08x\n",
  1919. mmc_hostname(host->mmc), intmask);
  1920. if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  1921. u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  1922. SDHCI_CARD_PRESENT;
  1923. /*
  1924. * There is a observation on i.mx esdhc. INSERT bit will be
  1925. * immediately set again when it gets cleared, if a card is
  1926. * inserted. We have to mask the irq to prevent interrupt
  1927. * storm which will freeze the system. And the REMOVE gets
  1928. * the same situation.
  1929. *
  1930. * More testing are needed here to ensure it works for other
  1931. * platforms though.
  1932. */
  1933. sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
  1934. SDHCI_INT_CARD_REMOVE);
  1935. sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
  1936. SDHCI_INT_CARD_INSERT);
  1937. sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
  1938. SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
  1939. intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
  1940. tasklet_schedule(&host->card_tasklet);
  1941. }
  1942. if (intmask & SDHCI_INT_CMD_MASK) {
  1943. sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
  1944. SDHCI_INT_STATUS);
  1945. sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  1946. }
  1947. if (intmask & SDHCI_INT_DATA_MASK) {
  1948. sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
  1949. SDHCI_INT_STATUS);
  1950. sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  1951. }
  1952. intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
  1953. intmask &= ~SDHCI_INT_ERROR;
  1954. if (intmask & SDHCI_INT_BUS_POWER) {
  1955. pr_err("%s: Card is consuming too much power!\n",
  1956. mmc_hostname(host->mmc));
  1957. sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
  1958. }
  1959. intmask &= ~SDHCI_INT_BUS_POWER;
  1960. if (intmask & SDHCI_INT_CARD_INT)
  1961. cardint = 1;
  1962. intmask &= ~SDHCI_INT_CARD_INT;
  1963. if (intmask) {
  1964. unexpected |= intmask;
  1965. sdhci_writel(host, intmask, SDHCI_INT_STATUS);
  1966. }
  1967. result = IRQ_HANDLED;
  1968. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  1969. if (intmask && --max_loops)
  1970. goto again;
  1971. out:
  1972. spin_unlock(&host->lock);
  1973. if (unexpected) {
  1974. pr_err("%s: Unexpected interrupt 0x%08x.\n",
  1975. mmc_hostname(host->mmc), unexpected);
  1976. sdhci_dumpregs(host);
  1977. }
  1978. /*
  1979. * We have to delay this as it calls back into the driver.
  1980. */
  1981. if (cardint)
  1982. mmc_signal_sdio_irq(host->mmc);
  1983. return result;
  1984. }
  1985. /*****************************************************************************\
  1986. * *
  1987. * Suspend/resume *
  1988. * *
  1989. \*****************************************************************************/
  1990. #ifdef CONFIG_PM
  1991. int sdhci_suspend_host(struct sdhci_host *host)
  1992. {
  1993. int ret;
  1994. if (host->ops->platform_suspend)
  1995. host->ops->platform_suspend(host);
  1996. sdhci_disable_card_detection(host);
  1997. /* Disable tuning since we are suspending */
  1998. if (host->flags & SDHCI_USING_RETUNING_TIMER) {
  1999. del_timer_sync(&host->tuning_timer);
  2000. host->flags &= ~SDHCI_NEEDS_RETUNING;
  2001. }
  2002. ret = mmc_suspend_host(host->mmc);
  2003. if (ret) {
  2004. if (host->flags & SDHCI_USING_RETUNING_TIMER) {
  2005. host->flags |= SDHCI_NEEDS_RETUNING;
  2006. mod_timer(&host->tuning_timer, jiffies +
  2007. host->tuning_count * HZ);
  2008. }
  2009. sdhci_enable_card_detection(host);
  2010. return ret;
  2011. }
  2012. free_irq(host->irq, host);
  2013. return ret;
  2014. }
  2015. EXPORT_SYMBOL_GPL(sdhci_suspend_host);
  2016. int sdhci_resume_host(struct sdhci_host *host)
  2017. {
  2018. int ret;
  2019. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2020. if (host->ops->enable_dma)
  2021. host->ops->enable_dma(host);
  2022. }
  2023. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  2024. mmc_hostname(host->mmc), host);
  2025. if (ret)
  2026. return ret;
  2027. if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
  2028. (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
  2029. /* Card keeps power but host controller does not */
  2030. sdhci_init(host, 0);
  2031. host->pwr = 0;
  2032. host->clock = 0;
  2033. sdhci_do_set_ios(host, &host->mmc->ios);
  2034. } else {
  2035. sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
  2036. mmiowb();
  2037. }
  2038. ret = mmc_resume_host(host->mmc);
  2039. sdhci_enable_card_detection(host);
  2040. if (host->ops->platform_resume)
  2041. host->ops->platform_resume(host);
  2042. /* Set the re-tuning expiration flag */
  2043. if (host->flags & SDHCI_USING_RETUNING_TIMER)
  2044. host->flags |= SDHCI_NEEDS_RETUNING;
  2045. return ret;
  2046. }
  2047. EXPORT_SYMBOL_GPL(sdhci_resume_host);
  2048. void sdhci_enable_irq_wakeups(struct sdhci_host *host)
  2049. {
  2050. u8 val;
  2051. val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
  2052. val |= SDHCI_WAKE_ON_INT;
  2053. sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
  2054. }
  2055. EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
  2056. #endif /* CONFIG_PM */
  2057. #ifdef CONFIG_PM_RUNTIME
  2058. static int sdhci_runtime_pm_get(struct sdhci_host *host)
  2059. {
  2060. return pm_runtime_get_sync(host->mmc->parent);
  2061. }
  2062. static int sdhci_runtime_pm_put(struct sdhci_host *host)
  2063. {
  2064. pm_runtime_mark_last_busy(host->mmc->parent);
  2065. return pm_runtime_put_autosuspend(host->mmc->parent);
  2066. }
  2067. int sdhci_runtime_suspend_host(struct sdhci_host *host)
  2068. {
  2069. unsigned long flags;
  2070. int ret = 0;
  2071. /* Disable tuning since we are suspending */
  2072. if (host->flags & SDHCI_USING_RETUNING_TIMER) {
  2073. del_timer_sync(&host->tuning_timer);
  2074. host->flags &= ~SDHCI_NEEDS_RETUNING;
  2075. }
  2076. spin_lock_irqsave(&host->lock, flags);
  2077. sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
  2078. spin_unlock_irqrestore(&host->lock, flags);
  2079. synchronize_irq(host->irq);
  2080. spin_lock_irqsave(&host->lock, flags);
  2081. host->runtime_suspended = true;
  2082. spin_unlock_irqrestore(&host->lock, flags);
  2083. return ret;
  2084. }
  2085. EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
  2086. int sdhci_runtime_resume_host(struct sdhci_host *host)
  2087. {
  2088. unsigned long flags;
  2089. int ret = 0, host_flags = host->flags;
  2090. if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2091. if (host->ops->enable_dma)
  2092. host->ops->enable_dma(host);
  2093. }
  2094. sdhci_init(host, 0);
  2095. /* Force clock and power re-program */
  2096. host->pwr = 0;
  2097. host->clock = 0;
  2098. sdhci_do_set_ios(host, &host->mmc->ios);
  2099. sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
  2100. if (host_flags & SDHCI_PV_ENABLED)
  2101. sdhci_do_enable_preset_value(host, true);
  2102. /* Set the re-tuning expiration flag */
  2103. if (host->flags & SDHCI_USING_RETUNING_TIMER)
  2104. host->flags |= SDHCI_NEEDS_RETUNING;
  2105. spin_lock_irqsave(&host->lock, flags);
  2106. host->runtime_suspended = false;
  2107. /* Enable SDIO IRQ */
  2108. if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
  2109. sdhci_enable_sdio_irq_nolock(host, true);
  2110. /* Enable Card Detection */
  2111. sdhci_enable_card_detection(host);
  2112. spin_unlock_irqrestore(&host->lock, flags);
  2113. return ret;
  2114. }
  2115. EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
  2116. #endif
  2117. /*****************************************************************************\
  2118. * *
  2119. * Device allocation/registration *
  2120. * *
  2121. \*****************************************************************************/
  2122. struct sdhci_host *sdhci_alloc_host(struct device *dev,
  2123. size_t priv_size)
  2124. {
  2125. struct mmc_host *mmc;
  2126. struct sdhci_host *host;
  2127. WARN_ON(dev == NULL);
  2128. mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
  2129. if (!mmc)
  2130. return ERR_PTR(-ENOMEM);
  2131. host = mmc_priv(mmc);
  2132. host->mmc = mmc;
  2133. return host;
  2134. }
  2135. EXPORT_SYMBOL_GPL(sdhci_alloc_host);
  2136. int sdhci_add_host(struct sdhci_host *host)
  2137. {
  2138. struct mmc_host *mmc;
  2139. u32 caps[2] = {0, 0};
  2140. u32 max_current_caps;
  2141. unsigned int ocr_avail;
  2142. int ret;
  2143. WARN_ON(host == NULL);
  2144. if (host == NULL)
  2145. return -EINVAL;
  2146. mmc = host->mmc;
  2147. if (debug_quirks)
  2148. host->quirks = debug_quirks;
  2149. if (debug_quirks2)
  2150. host->quirks2 = debug_quirks2;
  2151. sdhci_reset(host, SDHCI_RESET_ALL);
  2152. host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
  2153. host->version = (host->version & SDHCI_SPEC_VER_MASK)
  2154. >> SDHCI_SPEC_VER_SHIFT;
  2155. if (host->version > SDHCI_SPEC_300) {
  2156. pr_err("%s: Unknown controller version (%d). "
  2157. "You may experience problems.\n", mmc_hostname(mmc),
  2158. host->version);
  2159. }
  2160. caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
  2161. sdhci_readl(host, SDHCI_CAPABILITIES);
  2162. if (host->version >= SDHCI_SPEC_300)
  2163. caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
  2164. host->caps1 :
  2165. sdhci_readl(host, SDHCI_CAPABILITIES_1);
  2166. if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
  2167. host->flags |= SDHCI_USE_SDMA;
  2168. else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
  2169. DBG("Controller doesn't have SDMA capability\n");
  2170. else
  2171. host->flags |= SDHCI_USE_SDMA;
  2172. if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
  2173. (host->flags & SDHCI_USE_SDMA)) {
  2174. DBG("Disabling DMA as it is marked broken\n");
  2175. host->flags &= ~SDHCI_USE_SDMA;
  2176. }
  2177. if ((host->version >= SDHCI_SPEC_200) &&
  2178. (caps[0] & SDHCI_CAN_DO_ADMA2))
  2179. host->flags |= SDHCI_USE_ADMA;
  2180. if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
  2181. (host->flags & SDHCI_USE_ADMA)) {
  2182. DBG("Disabling ADMA as it is marked broken\n");
  2183. host->flags &= ~SDHCI_USE_ADMA;
  2184. }
  2185. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2186. if (host->ops->enable_dma) {
  2187. if (host->ops->enable_dma(host)) {
  2188. pr_warning("%s: No suitable DMA "
  2189. "available. Falling back to PIO.\n",
  2190. mmc_hostname(mmc));
  2191. host->flags &=
  2192. ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
  2193. }
  2194. }
  2195. }
  2196. if (host->flags & SDHCI_USE_ADMA) {
  2197. /*
  2198. * We need to allocate descriptors for all sg entries
  2199. * (128) and potentially one alignment transfer for
  2200. * each of those entries.
  2201. */
  2202. host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
  2203. host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
  2204. if (!host->adma_desc || !host->align_buffer) {
  2205. kfree(host->adma_desc);
  2206. kfree(host->align_buffer);
  2207. pr_warning("%s: Unable to allocate ADMA "
  2208. "buffers. Falling back to standard DMA.\n",
  2209. mmc_hostname(mmc));
  2210. host->flags &= ~SDHCI_USE_ADMA;
  2211. }
  2212. }
  2213. /*
  2214. * If we use DMA, then it's up to the caller to set the DMA
  2215. * mask, but PIO does not need the hw shim so we set a new
  2216. * mask here in that case.
  2217. */
  2218. if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
  2219. host->dma_mask = DMA_BIT_MASK(64);
  2220. mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
  2221. }
  2222. if (host->version >= SDHCI_SPEC_300)
  2223. host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
  2224. >> SDHCI_CLOCK_BASE_SHIFT;
  2225. else
  2226. host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
  2227. >> SDHCI_CLOCK_BASE_SHIFT;
  2228. host->max_clk *= 1000000;
  2229. if (host->max_clk == 0 || host->quirks &
  2230. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
  2231. if (!host->ops->get_max_clock) {
  2232. pr_err("%s: Hardware doesn't specify base clock "
  2233. "frequency.\n", mmc_hostname(mmc));
  2234. return -ENODEV;
  2235. }
  2236. host->max_clk = host->ops->get_max_clock(host);
  2237. }
  2238. /*
  2239. * In case of Host Controller v3.00, find out whether clock
  2240. * multiplier is supported.
  2241. */
  2242. host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
  2243. SDHCI_CLOCK_MUL_SHIFT;
  2244. /*
  2245. * In case the value in Clock Multiplier is 0, then programmable
  2246. * clock mode is not supported, otherwise the actual clock
  2247. * multiplier is one more than the value of Clock Multiplier
  2248. * in the Capabilities Register.
  2249. */
  2250. if (host->clk_mul)
  2251. host->clk_mul += 1;
  2252. /*
  2253. * Set host parameters.
  2254. */
  2255. mmc->ops = &sdhci_ops;
  2256. mmc->f_max = host->max_clk;
  2257. if (host->ops->get_min_clock)
  2258. mmc->f_min = host->ops->get_min_clock(host);
  2259. else if (host->version >= SDHCI_SPEC_300) {
  2260. if (host->clk_mul) {
  2261. mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
  2262. mmc->f_max = host->max_clk * host->clk_mul;
  2263. } else
  2264. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
  2265. } else
  2266. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
  2267. host->timeout_clk =
  2268. (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
  2269. if (host->timeout_clk == 0) {
  2270. if (host->ops->get_timeout_clock) {
  2271. host->timeout_clk = host->ops->get_timeout_clock(host);
  2272. } else if (!(host->quirks &
  2273. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
  2274. pr_err("%s: Hardware doesn't specify timeout clock "
  2275. "frequency.\n", mmc_hostname(mmc));
  2276. return -ENODEV;
  2277. }
  2278. }
  2279. if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
  2280. host->timeout_clk *= 1000;
  2281. if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
  2282. host->timeout_clk = mmc->f_max / 1000;
  2283. mmc->max_discard_to = (1 << 27) / host->timeout_clk;
  2284. mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
  2285. if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
  2286. host->flags |= SDHCI_AUTO_CMD12;
  2287. /* Auto-CMD23 stuff only works in ADMA or PIO. */
  2288. if ((host->version >= SDHCI_SPEC_300) &&
  2289. ((host->flags & SDHCI_USE_ADMA) ||
  2290. !(host->flags & SDHCI_USE_SDMA))) {
  2291. host->flags |= SDHCI_AUTO_CMD23;
  2292. DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
  2293. } else {
  2294. DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
  2295. }
  2296. /*
  2297. * A controller may support 8-bit width, but the board itself
  2298. * might not have the pins brought out. Boards that support
  2299. * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
  2300. * their platform code before calling sdhci_add_host(), and we
  2301. * won't assume 8-bit width for hosts without that CAP.
  2302. */
  2303. if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
  2304. mmc->caps |= MMC_CAP_4_BIT_DATA;
  2305. if (caps[0] & SDHCI_CAN_DO_HISPD)
  2306. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  2307. if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
  2308. !(host->mmc->caps & MMC_CAP_NONREMOVABLE))
  2309. mmc->caps |= MMC_CAP_NEEDS_POLL;
  2310. /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
  2311. if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
  2312. SDHCI_SUPPORT_DDR50))
  2313. mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
  2314. /* SDR104 supports also implies SDR50 support */
  2315. if (caps[1] & SDHCI_SUPPORT_SDR104)
  2316. mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
  2317. else if (caps[1] & SDHCI_SUPPORT_SDR50)
  2318. mmc->caps |= MMC_CAP_UHS_SDR50;
  2319. if (caps[1] & SDHCI_SUPPORT_DDR50)
  2320. mmc->caps |= MMC_CAP_UHS_DDR50;
  2321. /* Does the host need tuning for SDR50? */
  2322. if (caps[1] & SDHCI_USE_SDR50_TUNING)
  2323. host->flags |= SDHCI_SDR50_NEEDS_TUNING;
  2324. /* Does the host need tuning for HS200? */
  2325. if (mmc->caps2 & MMC_CAP2_HS200)
  2326. host->flags |= SDHCI_HS200_NEEDS_TUNING;
  2327. /* Driver Type(s) (A, C, D) supported by the host */
  2328. if (caps[1] & SDHCI_DRIVER_TYPE_A)
  2329. mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
  2330. if (caps[1] & SDHCI_DRIVER_TYPE_C)
  2331. mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
  2332. if (caps[1] & SDHCI_DRIVER_TYPE_D)
  2333. mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
  2334. /*
  2335. * If Power Off Notify capability is enabled by the host,
  2336. * set notify to short power off notify timeout value.
  2337. */
  2338. if (mmc->caps2 & MMC_CAP2_POWEROFF_NOTIFY)
  2339. mmc->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
  2340. else
  2341. mmc->power_notify_type = MMC_HOST_PW_NOTIFY_NONE;
  2342. /* Initial value for re-tuning timer count */
  2343. host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
  2344. SDHCI_RETUNING_TIMER_COUNT_SHIFT;
  2345. /*
  2346. * In case Re-tuning Timer is not disabled, the actual value of
  2347. * re-tuning timer will be 2 ^ (n - 1).
  2348. */
  2349. if (host->tuning_count)
  2350. host->tuning_count = 1 << (host->tuning_count - 1);
  2351. /* Re-tuning mode supported by the Host Controller */
  2352. host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
  2353. SDHCI_RETUNING_MODE_SHIFT;
  2354. ocr_avail = 0;
  2355. host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
  2356. if (IS_ERR(host->vmmc)) {
  2357. pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
  2358. host->vmmc = NULL;
  2359. }
  2360. #ifdef CONFIG_REGULATOR
  2361. if (host->vmmc) {
  2362. ret = regulator_is_supported_voltage(host->vmmc, 3300000,
  2363. 3300000);
  2364. if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_330)))
  2365. caps[0] &= ~SDHCI_CAN_VDD_330;
  2366. ret = regulator_is_supported_voltage(host->vmmc, 3000000,
  2367. 3000000);
  2368. if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_300)))
  2369. caps[0] &= ~SDHCI_CAN_VDD_300;
  2370. ret = regulator_is_supported_voltage(host->vmmc, 1800000,
  2371. 1800000);
  2372. if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_180)))
  2373. caps[0] &= ~SDHCI_CAN_VDD_180;
  2374. }
  2375. #endif /* CONFIG_REGULATOR */
  2376. /*
  2377. * According to SD Host Controller spec v3.00, if the Host System
  2378. * can afford more than 150mA, Host Driver should set XPC to 1. Also
  2379. * the value is meaningful only if Voltage Support in the Capabilities
  2380. * register is set. The actual current value is 4 times the register
  2381. * value.
  2382. */
  2383. max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
  2384. if (!max_current_caps && host->vmmc) {
  2385. u32 curr = regulator_get_current_limit(host->vmmc);
  2386. if (curr > 0) {
  2387. /* convert to SDHCI_MAX_CURRENT format */
  2388. curr = curr/1000; /* convert to mA */
  2389. curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
  2390. curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
  2391. max_current_caps =
  2392. (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
  2393. (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
  2394. (curr << SDHCI_MAX_CURRENT_180_SHIFT);
  2395. }
  2396. }
  2397. if (caps[0] & SDHCI_CAN_VDD_330) {
  2398. ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
  2399. mmc->max_current_330 = ((max_current_caps &
  2400. SDHCI_MAX_CURRENT_330_MASK) >>
  2401. SDHCI_MAX_CURRENT_330_SHIFT) *
  2402. SDHCI_MAX_CURRENT_MULTIPLIER;
  2403. }
  2404. if (caps[0] & SDHCI_CAN_VDD_300) {
  2405. ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
  2406. mmc->max_current_300 = ((max_current_caps &
  2407. SDHCI_MAX_CURRENT_300_MASK) >>
  2408. SDHCI_MAX_CURRENT_300_SHIFT) *
  2409. SDHCI_MAX_CURRENT_MULTIPLIER;
  2410. }
  2411. if (caps[0] & SDHCI_CAN_VDD_180) {
  2412. ocr_avail |= MMC_VDD_165_195;
  2413. mmc->max_current_180 = ((max_current_caps &
  2414. SDHCI_MAX_CURRENT_180_MASK) >>
  2415. SDHCI_MAX_CURRENT_180_SHIFT) *
  2416. SDHCI_MAX_CURRENT_MULTIPLIER;
  2417. }
  2418. mmc->ocr_avail = ocr_avail;
  2419. mmc->ocr_avail_sdio = ocr_avail;
  2420. if (host->ocr_avail_sdio)
  2421. mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
  2422. mmc->ocr_avail_sd = ocr_avail;
  2423. if (host->ocr_avail_sd)
  2424. mmc->ocr_avail_sd &= host->ocr_avail_sd;
  2425. else /* normal SD controllers don't support 1.8V */
  2426. mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
  2427. mmc->ocr_avail_mmc = ocr_avail;
  2428. if (host->ocr_avail_mmc)
  2429. mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
  2430. if (mmc->ocr_avail == 0) {
  2431. pr_err("%s: Hardware doesn't report any "
  2432. "support voltages.\n", mmc_hostname(mmc));
  2433. return -ENODEV;
  2434. }
  2435. spin_lock_init(&host->lock);
  2436. /*
  2437. * Maximum number of segments. Depends on if the hardware
  2438. * can do scatter/gather or not.
  2439. */
  2440. if (host->flags & SDHCI_USE_ADMA)
  2441. mmc->max_segs = 128;
  2442. else if (host->flags & SDHCI_USE_SDMA)
  2443. mmc->max_segs = 1;
  2444. else /* PIO */
  2445. mmc->max_segs = 128;
  2446. /*
  2447. * Maximum number of sectors in one transfer. Limited by DMA boundary
  2448. * size (512KiB).
  2449. */
  2450. mmc->max_req_size = 524288;
  2451. /*
  2452. * Maximum segment size. Could be one segment with the maximum number
  2453. * of bytes. When doing hardware scatter/gather, each entry cannot
  2454. * be larger than 64 KiB though.
  2455. */
  2456. if (host->flags & SDHCI_USE_ADMA) {
  2457. if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
  2458. mmc->max_seg_size = 65535;
  2459. else
  2460. mmc->max_seg_size = 65536;
  2461. } else {
  2462. mmc->max_seg_size = mmc->max_req_size;
  2463. }
  2464. /*
  2465. * Maximum block size. This varies from controller to controller and
  2466. * is specified in the capabilities register.
  2467. */
  2468. if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
  2469. mmc->max_blk_size = 2;
  2470. } else {
  2471. mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
  2472. SDHCI_MAX_BLOCK_SHIFT;
  2473. if (mmc->max_blk_size >= 3) {
  2474. pr_warning("%s: Invalid maximum block size, "
  2475. "assuming 512 bytes\n", mmc_hostname(mmc));
  2476. mmc->max_blk_size = 0;
  2477. }
  2478. }
  2479. mmc->max_blk_size = 512 << mmc->max_blk_size;
  2480. /*
  2481. * Maximum block count.
  2482. */
  2483. mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
  2484. /*
  2485. * Init tasklets.
  2486. */
  2487. tasklet_init(&host->card_tasklet,
  2488. sdhci_tasklet_card, (unsigned long)host);
  2489. tasklet_init(&host->finish_tasklet,
  2490. sdhci_tasklet_finish, (unsigned long)host);
  2491. setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
  2492. if (host->version >= SDHCI_SPEC_300) {
  2493. init_waitqueue_head(&host->buf_ready_int);
  2494. /* Initialize re-tuning timer */
  2495. init_timer(&host->tuning_timer);
  2496. host->tuning_timer.data = (unsigned long)host;
  2497. host->tuning_timer.function = sdhci_tuning_timer;
  2498. }
  2499. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  2500. mmc_hostname(mmc), host);
  2501. if (ret) {
  2502. pr_err("%s: Failed to request IRQ %d: %d\n",
  2503. mmc_hostname(mmc), host->irq, ret);
  2504. goto untasklet;
  2505. }
  2506. sdhci_init(host, 0);
  2507. #ifdef CONFIG_MMC_DEBUG
  2508. sdhci_dumpregs(host);
  2509. #endif
  2510. #ifdef SDHCI_USE_LEDS_CLASS
  2511. snprintf(host->led_name, sizeof(host->led_name),
  2512. "%s::", mmc_hostname(mmc));
  2513. host->led.name = host->led_name;
  2514. host->led.brightness = LED_OFF;
  2515. host->led.default_trigger = mmc_hostname(mmc);
  2516. host->led.brightness_set = sdhci_led_control;
  2517. ret = led_classdev_register(mmc_dev(mmc), &host->led);
  2518. if (ret) {
  2519. pr_err("%s: Failed to register LED device: %d\n",
  2520. mmc_hostname(mmc), ret);
  2521. goto reset;
  2522. }
  2523. #endif
  2524. mmiowb();
  2525. mmc_add_host(mmc);
  2526. pr_info("%s: SDHCI controller on %s [%s] using %s\n",
  2527. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  2528. (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
  2529. (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
  2530. sdhci_enable_card_detection(host);
  2531. return 0;
  2532. #ifdef SDHCI_USE_LEDS_CLASS
  2533. reset:
  2534. sdhci_reset(host, SDHCI_RESET_ALL);
  2535. free_irq(host->irq, host);
  2536. #endif
  2537. untasklet:
  2538. tasklet_kill(&host->card_tasklet);
  2539. tasklet_kill(&host->finish_tasklet);
  2540. return ret;
  2541. }
  2542. EXPORT_SYMBOL_GPL(sdhci_add_host);
  2543. void sdhci_remove_host(struct sdhci_host *host, int dead)
  2544. {
  2545. unsigned long flags;
  2546. if (dead) {
  2547. spin_lock_irqsave(&host->lock, flags);
  2548. host->flags |= SDHCI_DEVICE_DEAD;
  2549. if (host->mrq) {
  2550. pr_err("%s: Controller removed during "
  2551. " transfer!\n", mmc_hostname(host->mmc));
  2552. host->mrq->cmd->error = -ENOMEDIUM;
  2553. tasklet_schedule(&host->finish_tasklet);
  2554. }
  2555. spin_unlock_irqrestore(&host->lock, flags);
  2556. }
  2557. sdhci_disable_card_detection(host);
  2558. mmc_remove_host(host->mmc);
  2559. #ifdef SDHCI_USE_LEDS_CLASS
  2560. led_classdev_unregister(&host->led);
  2561. #endif
  2562. if (!dead)
  2563. sdhci_reset(host, SDHCI_RESET_ALL);
  2564. free_irq(host->irq, host);
  2565. del_timer_sync(&host->timer);
  2566. tasklet_kill(&host->card_tasklet);
  2567. tasklet_kill(&host->finish_tasklet);
  2568. if (host->vmmc)
  2569. regulator_put(host->vmmc);
  2570. kfree(host->adma_desc);
  2571. kfree(host->align_buffer);
  2572. host->adma_desc = NULL;
  2573. host->align_buffer = NULL;
  2574. }
  2575. EXPORT_SYMBOL_GPL(sdhci_remove_host);
  2576. void sdhci_free_host(struct sdhci_host *host)
  2577. {
  2578. mmc_free_host(host->mmc);
  2579. }
  2580. EXPORT_SYMBOL_GPL(sdhci_free_host);
  2581. /*****************************************************************************\
  2582. * *
  2583. * Driver init/exit *
  2584. * *
  2585. \*****************************************************************************/
  2586. static int __init sdhci_drv_init(void)
  2587. {
  2588. pr_info(DRIVER_NAME
  2589. ": Secure Digital Host Controller Interface driver\n");
  2590. pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  2591. return 0;
  2592. }
  2593. static void __exit sdhci_drv_exit(void)
  2594. {
  2595. }
  2596. module_init(sdhci_drv_init);
  2597. module_exit(sdhci_drv_exit);
  2598. module_param(debug_quirks, uint, 0444);
  2599. module_param(debug_quirks2, uint, 0444);
  2600. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  2601. MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
  2602. MODULE_LICENSE("GPL");
  2603. MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
  2604. MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");