sdhci.c 47 KB

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