sdhci.c 80 KB

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