sdhci.c 73 KB

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