sdhci.c 59 KB

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