sdhci.c 72 KB

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