sdhci.c 48 KB

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