sdhci.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992
  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. sg_miter_start(&host->sg_miter,
  628. data->sg, data->sg_len, SG_MITER_ATOMIC);
  629. host->blocks = data->blocks;
  630. }
  631. sdhci_set_transfer_irqs(host);
  632. /* We do not handle DMA boundaries, so set it to max (512 KiB) */
  633. sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE);
  634. sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  635. }
  636. static void sdhci_set_transfer_mode(struct sdhci_host *host,
  637. struct mmc_data *data)
  638. {
  639. u16 mode;
  640. if (data == NULL)
  641. return;
  642. WARN_ON(!host->data);
  643. mode = SDHCI_TRNS_BLK_CNT_EN;
  644. if (data->blocks > 1)
  645. mode |= SDHCI_TRNS_MULTI;
  646. if (data->flags & MMC_DATA_READ)
  647. mode |= SDHCI_TRNS_READ;
  648. if (host->flags & SDHCI_REQ_USE_DMA)
  649. mode |= SDHCI_TRNS_DMA;
  650. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  651. }
  652. static void sdhci_finish_data(struct sdhci_host *host)
  653. {
  654. struct mmc_data *data;
  655. BUG_ON(!host->data);
  656. data = host->data;
  657. host->data = NULL;
  658. if (host->flags & SDHCI_REQ_USE_DMA) {
  659. if (host->flags & SDHCI_USE_ADMA)
  660. sdhci_adma_table_post(host, data);
  661. else {
  662. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  663. data->sg_len, (data->flags & MMC_DATA_READ) ?
  664. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  665. }
  666. }
  667. /*
  668. * The specification states that the block count register must
  669. * be updated, but it does not specify at what point in the
  670. * data flow. That makes the register entirely useless to read
  671. * back so we have to assume that nothing made it to the card
  672. * in the event of an error.
  673. */
  674. if (data->error)
  675. data->bytes_xfered = 0;
  676. else
  677. data->bytes_xfered = data->blksz * data->blocks;
  678. if (data->stop) {
  679. /*
  680. * The controller needs a reset of internal state machines
  681. * upon error conditions.
  682. */
  683. if (data->error) {
  684. sdhci_reset(host, SDHCI_RESET_CMD);
  685. sdhci_reset(host, SDHCI_RESET_DATA);
  686. }
  687. sdhci_send_command(host, data->stop);
  688. } else
  689. tasklet_schedule(&host->finish_tasklet);
  690. }
  691. static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
  692. {
  693. int flags;
  694. u32 mask;
  695. unsigned long timeout;
  696. WARN_ON(host->cmd);
  697. /* Wait max 10 ms */
  698. timeout = 10;
  699. mask = SDHCI_CMD_INHIBIT;
  700. if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  701. mask |= SDHCI_DATA_INHIBIT;
  702. /* We shouldn't wait for data inihibit for stop commands, even
  703. though they might use busy signaling */
  704. if (host->mrq->data && (cmd == host->mrq->data->stop))
  705. mask &= ~SDHCI_DATA_INHIBIT;
  706. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  707. if (timeout == 0) {
  708. printk(KERN_ERR "%s: Controller never released "
  709. "inhibit bit(s).\n", mmc_hostname(host->mmc));
  710. sdhci_dumpregs(host);
  711. cmd->error = -EIO;
  712. tasklet_schedule(&host->finish_tasklet);
  713. return;
  714. }
  715. timeout--;
  716. mdelay(1);
  717. }
  718. mod_timer(&host->timer, jiffies + 10 * HZ);
  719. host->cmd = cmd;
  720. sdhci_prepare_data(host, cmd->data);
  721. sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
  722. sdhci_set_transfer_mode(host, cmd->data);
  723. if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  724. printk(KERN_ERR "%s: Unsupported response type!\n",
  725. mmc_hostname(host->mmc));
  726. cmd->error = -EINVAL;
  727. tasklet_schedule(&host->finish_tasklet);
  728. return;
  729. }
  730. if (!(cmd->flags & MMC_RSP_PRESENT))
  731. flags = SDHCI_CMD_RESP_NONE;
  732. else if (cmd->flags & MMC_RSP_136)
  733. flags = SDHCI_CMD_RESP_LONG;
  734. else if (cmd->flags & MMC_RSP_BUSY)
  735. flags = SDHCI_CMD_RESP_SHORT_BUSY;
  736. else
  737. flags = SDHCI_CMD_RESP_SHORT;
  738. if (cmd->flags & MMC_RSP_CRC)
  739. flags |= SDHCI_CMD_CRC;
  740. if (cmd->flags & MMC_RSP_OPCODE)
  741. flags |= SDHCI_CMD_INDEX;
  742. if (cmd->data)
  743. flags |= SDHCI_CMD_DATA;
  744. sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
  745. }
  746. static void sdhci_finish_command(struct sdhci_host *host)
  747. {
  748. int i;
  749. BUG_ON(host->cmd == NULL);
  750. if (host->cmd->flags & MMC_RSP_PRESENT) {
  751. if (host->cmd->flags & MMC_RSP_136) {
  752. /* CRC is stripped so we need to do some shifting. */
  753. for (i = 0;i < 4;i++) {
  754. host->cmd->resp[i] = sdhci_readl(host,
  755. SDHCI_RESPONSE + (3-i)*4) << 8;
  756. if (i != 3)
  757. host->cmd->resp[i] |=
  758. sdhci_readb(host,
  759. SDHCI_RESPONSE + (3-i)*4-1);
  760. }
  761. } else {
  762. host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
  763. }
  764. }
  765. host->cmd->error = 0;
  766. if (host->data && host->data_early)
  767. sdhci_finish_data(host);
  768. if (!host->cmd->data)
  769. tasklet_schedule(&host->finish_tasklet);
  770. host->cmd = NULL;
  771. }
  772. static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
  773. {
  774. int div;
  775. u16 clk;
  776. unsigned long timeout;
  777. if (clock == host->clock)
  778. return;
  779. if (host->ops->set_clock) {
  780. host->ops->set_clock(host, clock);
  781. if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
  782. return;
  783. }
  784. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  785. if (clock == 0)
  786. goto out;
  787. for (div = 1;div < 256;div *= 2) {
  788. if ((host->max_clk / div) <= clock)
  789. break;
  790. }
  791. div >>= 1;
  792. clk = div << SDHCI_DIVIDER_SHIFT;
  793. clk |= SDHCI_CLOCK_INT_EN;
  794. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  795. /* Wait max 10 ms */
  796. timeout = 10;
  797. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  798. & SDHCI_CLOCK_INT_STABLE)) {
  799. if (timeout == 0) {
  800. printk(KERN_ERR "%s: Internal clock never "
  801. "stabilised.\n", mmc_hostname(host->mmc));
  802. sdhci_dumpregs(host);
  803. return;
  804. }
  805. timeout--;
  806. mdelay(1);
  807. }
  808. clk |= SDHCI_CLOCK_CARD_EN;
  809. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  810. out:
  811. host->clock = clock;
  812. }
  813. static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
  814. {
  815. u8 pwr;
  816. if (power == (unsigned short)-1)
  817. pwr = 0;
  818. else {
  819. switch (1 << power) {
  820. case MMC_VDD_165_195:
  821. pwr = SDHCI_POWER_180;
  822. break;
  823. case MMC_VDD_29_30:
  824. case MMC_VDD_30_31:
  825. pwr = SDHCI_POWER_300;
  826. break;
  827. case MMC_VDD_32_33:
  828. case MMC_VDD_33_34:
  829. pwr = SDHCI_POWER_330;
  830. break;
  831. default:
  832. BUG();
  833. }
  834. }
  835. if (host->pwr == pwr)
  836. return;
  837. host->pwr = pwr;
  838. if (pwr == 0) {
  839. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  840. return;
  841. }
  842. /*
  843. * Spec says that we should clear the power reg before setting
  844. * a new value. Some controllers don't seem to like this though.
  845. */
  846. if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
  847. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  848. /*
  849. * At least the Marvell CaFe chip gets confused if we set the voltage
  850. * and set turn on power at the same time, so set the voltage first.
  851. */
  852. if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
  853. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  854. pwr |= SDHCI_POWER_ON;
  855. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  856. /*
  857. * Some controllers need an extra 10ms delay of 10ms before they
  858. * can apply clock after applying power
  859. */
  860. if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
  861. mdelay(10);
  862. }
  863. /*****************************************************************************\
  864. * *
  865. * MMC callbacks *
  866. * *
  867. \*****************************************************************************/
  868. static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  869. {
  870. struct sdhci_host *host;
  871. bool present;
  872. unsigned long flags;
  873. host = mmc_priv(mmc);
  874. spin_lock_irqsave(&host->lock, flags);
  875. WARN_ON(host->mrq != NULL);
  876. #ifndef SDHCI_USE_LEDS_CLASS
  877. sdhci_activate_led(host);
  878. #endif
  879. host->mrq = mrq;
  880. /* If polling, assume that the card is always present. */
  881. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  882. present = true;
  883. else
  884. present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  885. SDHCI_CARD_PRESENT;
  886. if (!present || host->flags & SDHCI_DEVICE_DEAD) {
  887. host->mrq->cmd->error = -ENOMEDIUM;
  888. tasklet_schedule(&host->finish_tasklet);
  889. } else
  890. sdhci_send_command(host, mrq->cmd);
  891. mmiowb();
  892. spin_unlock_irqrestore(&host->lock, flags);
  893. }
  894. static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  895. {
  896. struct sdhci_host *host;
  897. unsigned long flags;
  898. u8 ctrl;
  899. host = mmc_priv(mmc);
  900. spin_lock_irqsave(&host->lock, flags);
  901. if (host->flags & SDHCI_DEVICE_DEAD)
  902. goto out;
  903. /*
  904. * Reset the chip on each power off.
  905. * Should clear out any weird states.
  906. */
  907. if (ios->power_mode == MMC_POWER_OFF) {
  908. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  909. sdhci_reinit(host);
  910. }
  911. sdhci_set_clock(host, ios->clock);
  912. if (ios->power_mode == MMC_POWER_OFF)
  913. sdhci_set_power(host, -1);
  914. else
  915. sdhci_set_power(host, ios->vdd);
  916. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  917. if (ios->bus_width == MMC_BUS_WIDTH_4)
  918. ctrl |= SDHCI_CTRL_4BITBUS;
  919. else
  920. ctrl &= ~SDHCI_CTRL_4BITBUS;
  921. if (ios->timing == MMC_TIMING_SD_HS)
  922. ctrl |= SDHCI_CTRL_HISPD;
  923. else
  924. ctrl &= ~SDHCI_CTRL_HISPD;
  925. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  926. /*
  927. * Some (ENE) controllers go apeshit on some ios operation,
  928. * signalling timeout and CRC errors even on CMD0. Resetting
  929. * it on each ios seems to solve the problem.
  930. */
  931. if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
  932. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  933. out:
  934. mmiowb();
  935. spin_unlock_irqrestore(&host->lock, flags);
  936. }
  937. static int sdhci_get_ro(struct mmc_host *mmc)
  938. {
  939. struct sdhci_host *host;
  940. unsigned long flags;
  941. int present;
  942. host = mmc_priv(mmc);
  943. spin_lock_irqsave(&host->lock, flags);
  944. if (host->flags & SDHCI_DEVICE_DEAD)
  945. present = 0;
  946. else
  947. present = sdhci_readl(host, SDHCI_PRESENT_STATE);
  948. spin_unlock_irqrestore(&host->lock, flags);
  949. if (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT)
  950. return !!(present & SDHCI_WRITE_PROTECT);
  951. return !(present & SDHCI_WRITE_PROTECT);
  952. }
  953. static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  954. {
  955. struct sdhci_host *host;
  956. unsigned long flags;
  957. host = mmc_priv(mmc);
  958. spin_lock_irqsave(&host->lock, flags);
  959. if (host->flags & SDHCI_DEVICE_DEAD)
  960. goto out;
  961. if (enable)
  962. sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
  963. else
  964. sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
  965. out:
  966. mmiowb();
  967. spin_unlock_irqrestore(&host->lock, flags);
  968. }
  969. static const struct mmc_host_ops sdhci_ops = {
  970. .request = sdhci_request,
  971. .set_ios = sdhci_set_ios,
  972. .get_ro = sdhci_get_ro,
  973. .enable_sdio_irq = sdhci_enable_sdio_irq,
  974. };
  975. /*****************************************************************************\
  976. * *
  977. * Tasklets *
  978. * *
  979. \*****************************************************************************/
  980. static void sdhci_tasklet_card(unsigned long param)
  981. {
  982. struct sdhci_host *host;
  983. unsigned long flags;
  984. host = (struct sdhci_host*)param;
  985. spin_lock_irqsave(&host->lock, flags);
  986. if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
  987. if (host->mrq) {
  988. printk(KERN_ERR "%s: Card removed during transfer!\n",
  989. mmc_hostname(host->mmc));
  990. printk(KERN_ERR "%s: Resetting controller.\n",
  991. mmc_hostname(host->mmc));
  992. sdhci_reset(host, SDHCI_RESET_CMD);
  993. sdhci_reset(host, SDHCI_RESET_DATA);
  994. host->mrq->cmd->error = -ENOMEDIUM;
  995. tasklet_schedule(&host->finish_tasklet);
  996. }
  997. }
  998. spin_unlock_irqrestore(&host->lock, flags);
  999. mmc_detect_change(host->mmc, msecs_to_jiffies(200));
  1000. }
  1001. static void sdhci_tasklet_finish(unsigned long param)
  1002. {
  1003. struct sdhci_host *host;
  1004. unsigned long flags;
  1005. struct mmc_request *mrq;
  1006. host = (struct sdhci_host*)param;
  1007. spin_lock_irqsave(&host->lock, flags);
  1008. del_timer(&host->timer);
  1009. mrq = host->mrq;
  1010. /*
  1011. * The controller needs a reset of internal state machines
  1012. * upon error conditions.
  1013. */
  1014. if (!(host->flags & SDHCI_DEVICE_DEAD) &&
  1015. (mrq->cmd->error ||
  1016. (mrq->data && (mrq->data->error ||
  1017. (mrq->data->stop && mrq->data->stop->error))) ||
  1018. (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
  1019. /* Some controllers need this kick or reset won't work here */
  1020. if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
  1021. unsigned int clock;
  1022. /* This is to force an update */
  1023. clock = host->clock;
  1024. host->clock = 0;
  1025. sdhci_set_clock(host, clock);
  1026. }
  1027. /* Spec says we should do both at the same time, but Ricoh
  1028. controllers do not like that. */
  1029. sdhci_reset(host, SDHCI_RESET_CMD);
  1030. sdhci_reset(host, SDHCI_RESET_DATA);
  1031. }
  1032. host->mrq = NULL;
  1033. host->cmd = NULL;
  1034. host->data = NULL;
  1035. #ifndef SDHCI_USE_LEDS_CLASS
  1036. sdhci_deactivate_led(host);
  1037. #endif
  1038. mmiowb();
  1039. spin_unlock_irqrestore(&host->lock, flags);
  1040. mmc_request_done(host->mmc, mrq);
  1041. }
  1042. static void sdhci_timeout_timer(unsigned long data)
  1043. {
  1044. struct sdhci_host *host;
  1045. unsigned long flags;
  1046. host = (struct sdhci_host*)data;
  1047. spin_lock_irqsave(&host->lock, flags);
  1048. if (host->mrq) {
  1049. printk(KERN_ERR "%s: Timeout waiting for hardware "
  1050. "interrupt.\n", mmc_hostname(host->mmc));
  1051. sdhci_dumpregs(host);
  1052. if (host->data) {
  1053. host->data->error = -ETIMEDOUT;
  1054. sdhci_finish_data(host);
  1055. } else {
  1056. if (host->cmd)
  1057. host->cmd->error = -ETIMEDOUT;
  1058. else
  1059. host->mrq->cmd->error = -ETIMEDOUT;
  1060. tasklet_schedule(&host->finish_tasklet);
  1061. }
  1062. }
  1063. mmiowb();
  1064. spin_unlock_irqrestore(&host->lock, flags);
  1065. }
  1066. /*****************************************************************************\
  1067. * *
  1068. * Interrupt handling *
  1069. * *
  1070. \*****************************************************************************/
  1071. static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
  1072. {
  1073. BUG_ON(intmask == 0);
  1074. if (!host->cmd) {
  1075. printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
  1076. "though no command operation was in progress.\n",
  1077. mmc_hostname(host->mmc), (unsigned)intmask);
  1078. sdhci_dumpregs(host);
  1079. return;
  1080. }
  1081. if (intmask & SDHCI_INT_TIMEOUT)
  1082. host->cmd->error = -ETIMEDOUT;
  1083. else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
  1084. SDHCI_INT_INDEX))
  1085. host->cmd->error = -EILSEQ;
  1086. if (host->cmd->error) {
  1087. tasklet_schedule(&host->finish_tasklet);
  1088. return;
  1089. }
  1090. /*
  1091. * The host can send and interrupt when the busy state has
  1092. * ended, allowing us to wait without wasting CPU cycles.
  1093. * Unfortunately this is overloaded on the "data complete"
  1094. * interrupt, so we need to take some care when handling
  1095. * it.
  1096. *
  1097. * Note: The 1.0 specification is a bit ambiguous about this
  1098. * feature so there might be some problems with older
  1099. * controllers.
  1100. */
  1101. if (host->cmd->flags & MMC_RSP_BUSY) {
  1102. if (host->cmd->data)
  1103. DBG("Cannot wait for busy signal when also "
  1104. "doing a data transfer");
  1105. else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
  1106. return;
  1107. /* The controller does not support the end-of-busy IRQ,
  1108. * fall through and take the SDHCI_INT_RESPONSE */
  1109. }
  1110. if (intmask & SDHCI_INT_RESPONSE)
  1111. sdhci_finish_command(host);
  1112. }
  1113. #ifdef DEBUG
  1114. static void sdhci_show_adma_error(struct sdhci_host *host)
  1115. {
  1116. const char *name = mmc_hostname(host->mmc);
  1117. u8 *desc = host->adma_desc;
  1118. __le32 *dma;
  1119. __le16 *len;
  1120. u8 attr;
  1121. sdhci_dumpregs(host);
  1122. while (true) {
  1123. dma = (__le32 *)(desc + 4);
  1124. len = (__le16 *)(desc + 2);
  1125. attr = *desc;
  1126. DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
  1127. name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
  1128. desc += 8;
  1129. if (attr & 2)
  1130. break;
  1131. }
  1132. }
  1133. #else
  1134. static void sdhci_show_adma_error(struct sdhci_host *host) { }
  1135. #endif
  1136. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  1137. {
  1138. BUG_ON(intmask == 0);
  1139. if (!host->data) {
  1140. /*
  1141. * The "data complete" interrupt is also used to
  1142. * indicate that a busy state has ended. See comment
  1143. * above in sdhci_cmd_irq().
  1144. */
  1145. if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
  1146. if (intmask & SDHCI_INT_DATA_END) {
  1147. sdhci_finish_command(host);
  1148. return;
  1149. }
  1150. }
  1151. printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
  1152. "though no data operation was in progress.\n",
  1153. mmc_hostname(host->mmc), (unsigned)intmask);
  1154. sdhci_dumpregs(host);
  1155. return;
  1156. }
  1157. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  1158. host->data->error = -ETIMEDOUT;
  1159. else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
  1160. host->data->error = -EILSEQ;
  1161. else if (intmask & SDHCI_INT_ADMA_ERROR) {
  1162. printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
  1163. sdhci_show_adma_error(host);
  1164. host->data->error = -EIO;
  1165. }
  1166. if (host->data->error)
  1167. sdhci_finish_data(host);
  1168. else {
  1169. if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  1170. sdhci_transfer_pio(host);
  1171. /*
  1172. * We currently don't do anything fancy with DMA
  1173. * boundaries, but as we can't disable the feature
  1174. * we need to at least restart the transfer.
  1175. */
  1176. if (intmask & SDHCI_INT_DMA_END)
  1177. sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
  1178. SDHCI_DMA_ADDRESS);
  1179. if (intmask & SDHCI_INT_DATA_END) {
  1180. if (host->cmd) {
  1181. /*
  1182. * Data managed to finish before the
  1183. * command completed. Make sure we do
  1184. * things in the proper order.
  1185. */
  1186. host->data_early = 1;
  1187. } else {
  1188. sdhci_finish_data(host);
  1189. }
  1190. }
  1191. }
  1192. }
  1193. static irqreturn_t sdhci_irq(int irq, void *dev_id)
  1194. {
  1195. irqreturn_t result;
  1196. struct sdhci_host* host = dev_id;
  1197. u32 intmask;
  1198. int cardint = 0;
  1199. spin_lock(&host->lock);
  1200. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  1201. if (!intmask || intmask == 0xffffffff) {
  1202. result = IRQ_NONE;
  1203. goto out;
  1204. }
  1205. DBG("*** %s got interrupt: 0x%08x\n",
  1206. mmc_hostname(host->mmc), intmask);
  1207. if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  1208. sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
  1209. SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
  1210. tasklet_schedule(&host->card_tasklet);
  1211. }
  1212. intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
  1213. if (intmask & SDHCI_INT_CMD_MASK) {
  1214. sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
  1215. SDHCI_INT_STATUS);
  1216. sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  1217. }
  1218. if (intmask & SDHCI_INT_DATA_MASK) {
  1219. sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
  1220. SDHCI_INT_STATUS);
  1221. sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  1222. }
  1223. intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
  1224. intmask &= ~SDHCI_INT_ERROR;
  1225. if (intmask & SDHCI_INT_BUS_POWER) {
  1226. printk(KERN_ERR "%s: Card is consuming too much power!\n",
  1227. mmc_hostname(host->mmc));
  1228. sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
  1229. }
  1230. intmask &= ~SDHCI_INT_BUS_POWER;
  1231. if (intmask & SDHCI_INT_CARD_INT)
  1232. cardint = 1;
  1233. intmask &= ~SDHCI_INT_CARD_INT;
  1234. if (intmask) {
  1235. printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
  1236. mmc_hostname(host->mmc), intmask);
  1237. sdhci_dumpregs(host);
  1238. sdhci_writel(host, intmask, SDHCI_INT_STATUS);
  1239. }
  1240. result = IRQ_HANDLED;
  1241. mmiowb();
  1242. out:
  1243. spin_unlock(&host->lock);
  1244. /*
  1245. * We have to delay this as it calls back into the driver.
  1246. */
  1247. if (cardint)
  1248. mmc_signal_sdio_irq(host->mmc);
  1249. return result;
  1250. }
  1251. /*****************************************************************************\
  1252. * *
  1253. * Suspend/resume *
  1254. * *
  1255. \*****************************************************************************/
  1256. #ifdef CONFIG_PM
  1257. int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
  1258. {
  1259. int ret;
  1260. sdhci_disable_card_detection(host);
  1261. ret = mmc_suspend_host(host->mmc, state);
  1262. if (ret)
  1263. return ret;
  1264. free_irq(host->irq, host);
  1265. return 0;
  1266. }
  1267. EXPORT_SYMBOL_GPL(sdhci_suspend_host);
  1268. int sdhci_resume_host(struct sdhci_host *host)
  1269. {
  1270. int ret;
  1271. if (host->flags & SDHCI_USE_DMA) {
  1272. if (host->ops->enable_dma)
  1273. host->ops->enable_dma(host);
  1274. }
  1275. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  1276. mmc_hostname(host->mmc), host);
  1277. if (ret)
  1278. return ret;
  1279. sdhci_init(host);
  1280. mmiowb();
  1281. ret = mmc_resume_host(host->mmc);
  1282. if (ret)
  1283. return ret;
  1284. sdhci_enable_card_detection(host);
  1285. return 0;
  1286. }
  1287. EXPORT_SYMBOL_GPL(sdhci_resume_host);
  1288. #endif /* CONFIG_PM */
  1289. /*****************************************************************************\
  1290. * *
  1291. * Device allocation/registration *
  1292. * *
  1293. \*****************************************************************************/
  1294. struct sdhci_host *sdhci_alloc_host(struct device *dev,
  1295. size_t priv_size)
  1296. {
  1297. struct mmc_host *mmc;
  1298. struct sdhci_host *host;
  1299. WARN_ON(dev == NULL);
  1300. mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
  1301. if (!mmc)
  1302. return ERR_PTR(-ENOMEM);
  1303. host = mmc_priv(mmc);
  1304. host->mmc = mmc;
  1305. return host;
  1306. }
  1307. EXPORT_SYMBOL_GPL(sdhci_alloc_host);
  1308. int sdhci_add_host(struct sdhci_host *host)
  1309. {
  1310. struct mmc_host *mmc;
  1311. unsigned int caps;
  1312. int ret;
  1313. WARN_ON(host == NULL);
  1314. if (host == NULL)
  1315. return -EINVAL;
  1316. mmc = host->mmc;
  1317. if (debug_quirks)
  1318. host->quirks = debug_quirks;
  1319. sdhci_reset(host, SDHCI_RESET_ALL);
  1320. host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
  1321. host->version = (host->version & SDHCI_SPEC_VER_MASK)
  1322. >> SDHCI_SPEC_VER_SHIFT;
  1323. if (host->version > SDHCI_SPEC_200) {
  1324. printk(KERN_ERR "%s: Unknown controller version (%d). "
  1325. "You may experience problems.\n", mmc_hostname(mmc),
  1326. host->version);
  1327. }
  1328. caps = sdhci_readl(host, SDHCI_CAPABILITIES);
  1329. if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
  1330. host->flags |= SDHCI_USE_DMA;
  1331. else if (!(caps & SDHCI_CAN_DO_DMA))
  1332. DBG("Controller doesn't have DMA capability\n");
  1333. else
  1334. host->flags |= SDHCI_USE_DMA;
  1335. if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
  1336. (host->flags & SDHCI_USE_DMA)) {
  1337. DBG("Disabling DMA as it is marked broken\n");
  1338. host->flags &= ~SDHCI_USE_DMA;
  1339. }
  1340. if (host->flags & SDHCI_USE_DMA) {
  1341. if ((host->version >= SDHCI_SPEC_200) &&
  1342. (caps & SDHCI_CAN_DO_ADMA2))
  1343. host->flags |= SDHCI_USE_ADMA;
  1344. }
  1345. if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
  1346. (host->flags & SDHCI_USE_ADMA)) {
  1347. DBG("Disabling ADMA as it is marked broken\n");
  1348. host->flags &= ~SDHCI_USE_ADMA;
  1349. }
  1350. if (host->flags & SDHCI_USE_DMA) {
  1351. if (host->ops->enable_dma) {
  1352. if (host->ops->enable_dma(host)) {
  1353. printk(KERN_WARNING "%s: No suitable DMA "
  1354. "available. Falling back to PIO.\n",
  1355. mmc_hostname(mmc));
  1356. host->flags &= ~(SDHCI_USE_DMA | SDHCI_USE_ADMA);
  1357. }
  1358. }
  1359. }
  1360. if (host->flags & SDHCI_USE_ADMA) {
  1361. /*
  1362. * We need to allocate descriptors for all sg entries
  1363. * (128) and potentially one alignment transfer for
  1364. * each of those entries.
  1365. */
  1366. host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
  1367. host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
  1368. if (!host->adma_desc || !host->align_buffer) {
  1369. kfree(host->adma_desc);
  1370. kfree(host->align_buffer);
  1371. printk(KERN_WARNING "%s: Unable to allocate ADMA "
  1372. "buffers. Falling back to standard DMA.\n",
  1373. mmc_hostname(mmc));
  1374. host->flags &= ~SDHCI_USE_ADMA;
  1375. }
  1376. }
  1377. /*
  1378. * If we use DMA, then it's up to the caller to set the DMA
  1379. * mask, but PIO does not need the hw shim so we set a new
  1380. * mask here in that case.
  1381. */
  1382. if (!(host->flags & SDHCI_USE_DMA)) {
  1383. host->dma_mask = DMA_BIT_MASK(64);
  1384. mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
  1385. }
  1386. host->max_clk =
  1387. (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
  1388. host->max_clk *= 1000000;
  1389. if (host->max_clk == 0) {
  1390. if (!host->ops->get_max_clock) {
  1391. printk(KERN_ERR
  1392. "%s: Hardware doesn't specify base clock "
  1393. "frequency.\n", mmc_hostname(mmc));
  1394. return -ENODEV;
  1395. }
  1396. host->max_clk = host->ops->get_max_clock(host);
  1397. }
  1398. host->timeout_clk =
  1399. (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
  1400. if (host->timeout_clk == 0) {
  1401. if (!host->ops->get_timeout_clock) {
  1402. printk(KERN_ERR
  1403. "%s: Hardware doesn't specify timeout clock "
  1404. "frequency.\n", mmc_hostname(mmc));
  1405. return -ENODEV;
  1406. }
  1407. host->timeout_clk = host->ops->get_timeout_clock(host);
  1408. }
  1409. if (caps & SDHCI_TIMEOUT_CLK_UNIT)
  1410. host->timeout_clk *= 1000;
  1411. /*
  1412. * Set host parameters.
  1413. */
  1414. mmc->ops = &sdhci_ops;
  1415. mmc->f_min = host->max_clk / 256;
  1416. mmc->f_max = host->max_clk;
  1417. mmc->caps = MMC_CAP_SDIO_IRQ;
  1418. if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
  1419. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1420. if (caps & SDHCI_CAN_DO_HISPD)
  1421. mmc->caps |= MMC_CAP_SD_HIGHSPEED;
  1422. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  1423. mmc->caps |= MMC_CAP_NEEDS_POLL;
  1424. mmc->ocr_avail = 0;
  1425. if (caps & SDHCI_CAN_VDD_330)
  1426. mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
  1427. if (caps & SDHCI_CAN_VDD_300)
  1428. mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
  1429. if (caps & SDHCI_CAN_VDD_180)
  1430. mmc->ocr_avail |= MMC_VDD_165_195;
  1431. if (mmc->ocr_avail == 0) {
  1432. printk(KERN_ERR "%s: Hardware doesn't report any "
  1433. "support voltages.\n", mmc_hostname(mmc));
  1434. return -ENODEV;
  1435. }
  1436. spin_lock_init(&host->lock);
  1437. /*
  1438. * Maximum number of segments. Depends on if the hardware
  1439. * can do scatter/gather or not.
  1440. */
  1441. if (host->flags & SDHCI_USE_ADMA)
  1442. mmc->max_hw_segs = 128;
  1443. else if (host->flags & SDHCI_USE_DMA)
  1444. mmc->max_hw_segs = 1;
  1445. else /* PIO */
  1446. mmc->max_hw_segs = 128;
  1447. mmc->max_phys_segs = 128;
  1448. /*
  1449. * Maximum number of sectors in one transfer. Limited by DMA boundary
  1450. * size (512KiB).
  1451. */
  1452. mmc->max_req_size = 524288;
  1453. /*
  1454. * Maximum segment size. Could be one segment with the maximum number
  1455. * of bytes. When doing hardware scatter/gather, each entry cannot
  1456. * be larger than 64 KiB though.
  1457. */
  1458. if (host->flags & SDHCI_USE_ADMA)
  1459. mmc->max_seg_size = 65536;
  1460. else
  1461. mmc->max_seg_size = mmc->max_req_size;
  1462. /*
  1463. * Maximum block size. This varies from controller to controller and
  1464. * is specified in the capabilities register.
  1465. */
  1466. if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
  1467. mmc->max_blk_size = 2;
  1468. } else {
  1469. mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
  1470. SDHCI_MAX_BLOCK_SHIFT;
  1471. if (mmc->max_blk_size >= 3) {
  1472. printk(KERN_WARNING "%s: Invalid maximum block size, "
  1473. "assuming 512 bytes\n", mmc_hostname(mmc));
  1474. mmc->max_blk_size = 0;
  1475. }
  1476. }
  1477. mmc->max_blk_size = 512 << mmc->max_blk_size;
  1478. /*
  1479. * Maximum block count.
  1480. */
  1481. mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
  1482. /*
  1483. * Init tasklets.
  1484. */
  1485. tasklet_init(&host->card_tasklet,
  1486. sdhci_tasklet_card, (unsigned long)host);
  1487. tasklet_init(&host->finish_tasklet,
  1488. sdhci_tasklet_finish, (unsigned long)host);
  1489. setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
  1490. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  1491. mmc_hostname(mmc), host);
  1492. if (ret)
  1493. goto untasklet;
  1494. sdhci_init(host);
  1495. #ifdef CONFIG_MMC_DEBUG
  1496. sdhci_dumpregs(host);
  1497. #endif
  1498. #ifdef SDHCI_USE_LEDS_CLASS
  1499. snprintf(host->led_name, sizeof(host->led_name),
  1500. "%s::", mmc_hostname(mmc));
  1501. host->led.name = host->led_name;
  1502. host->led.brightness = LED_OFF;
  1503. host->led.default_trigger = mmc_hostname(mmc);
  1504. host->led.brightness_set = sdhci_led_control;
  1505. ret = led_classdev_register(mmc_dev(mmc), &host->led);
  1506. if (ret)
  1507. goto reset;
  1508. #endif
  1509. mmiowb();
  1510. mmc_add_host(mmc);
  1511. printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s%s\n",
  1512. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  1513. (host->flags & SDHCI_USE_ADMA)?"A":"",
  1514. (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
  1515. sdhci_enable_card_detection(host);
  1516. return 0;
  1517. #ifdef SDHCI_USE_LEDS_CLASS
  1518. reset:
  1519. sdhci_reset(host, SDHCI_RESET_ALL);
  1520. free_irq(host->irq, host);
  1521. #endif
  1522. untasklet:
  1523. tasklet_kill(&host->card_tasklet);
  1524. tasklet_kill(&host->finish_tasklet);
  1525. return ret;
  1526. }
  1527. EXPORT_SYMBOL_GPL(sdhci_add_host);
  1528. void sdhci_remove_host(struct sdhci_host *host, int dead)
  1529. {
  1530. unsigned long flags;
  1531. if (dead) {
  1532. spin_lock_irqsave(&host->lock, flags);
  1533. host->flags |= SDHCI_DEVICE_DEAD;
  1534. if (host->mrq) {
  1535. printk(KERN_ERR "%s: Controller removed during "
  1536. " transfer!\n", mmc_hostname(host->mmc));
  1537. host->mrq->cmd->error = -ENOMEDIUM;
  1538. tasklet_schedule(&host->finish_tasklet);
  1539. }
  1540. spin_unlock_irqrestore(&host->lock, flags);
  1541. }
  1542. sdhci_disable_card_detection(host);
  1543. mmc_remove_host(host->mmc);
  1544. #ifdef SDHCI_USE_LEDS_CLASS
  1545. led_classdev_unregister(&host->led);
  1546. #endif
  1547. if (!dead)
  1548. sdhci_reset(host, SDHCI_RESET_ALL);
  1549. free_irq(host->irq, host);
  1550. del_timer_sync(&host->timer);
  1551. tasklet_kill(&host->card_tasklet);
  1552. tasklet_kill(&host->finish_tasklet);
  1553. kfree(host->adma_desc);
  1554. kfree(host->align_buffer);
  1555. host->adma_desc = NULL;
  1556. host->align_buffer = NULL;
  1557. }
  1558. EXPORT_SYMBOL_GPL(sdhci_remove_host);
  1559. void sdhci_free_host(struct sdhci_host *host)
  1560. {
  1561. mmc_free_host(host->mmc);
  1562. }
  1563. EXPORT_SYMBOL_GPL(sdhci_free_host);
  1564. /*****************************************************************************\
  1565. * *
  1566. * Driver init/exit *
  1567. * *
  1568. \*****************************************************************************/
  1569. static int __init sdhci_drv_init(void)
  1570. {
  1571. printk(KERN_INFO DRIVER_NAME
  1572. ": Secure Digital Host Controller Interface driver\n");
  1573. printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  1574. return 0;
  1575. }
  1576. static void __exit sdhci_drv_exit(void)
  1577. {
  1578. }
  1579. module_init(sdhci_drv_init);
  1580. module_exit(sdhci_drv_exit);
  1581. module_param(debug_quirks, uint, 0444);
  1582. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  1583. MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
  1584. MODULE_LICENSE("GPL");
  1585. MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");