sdhci.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  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. /*****************************************************************************\
  858. * *
  859. * MMC callbacks *
  860. * *
  861. \*****************************************************************************/
  862. static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  863. {
  864. struct sdhci_host *host;
  865. bool present;
  866. unsigned long flags;
  867. host = mmc_priv(mmc);
  868. spin_lock_irqsave(&host->lock, flags);
  869. WARN_ON(host->mrq != NULL);
  870. #ifndef SDHCI_USE_LEDS_CLASS
  871. sdhci_activate_led(host);
  872. #endif
  873. host->mrq = mrq;
  874. /* If polling, assume that the card is always present. */
  875. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  876. present = true;
  877. else
  878. present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  879. SDHCI_CARD_PRESENT;
  880. if (!present || host->flags & SDHCI_DEVICE_DEAD) {
  881. host->mrq->cmd->error = -ENOMEDIUM;
  882. tasklet_schedule(&host->finish_tasklet);
  883. } else
  884. sdhci_send_command(host, mrq->cmd);
  885. mmiowb();
  886. spin_unlock_irqrestore(&host->lock, flags);
  887. }
  888. static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  889. {
  890. struct sdhci_host *host;
  891. unsigned long flags;
  892. u8 ctrl;
  893. host = mmc_priv(mmc);
  894. spin_lock_irqsave(&host->lock, flags);
  895. if (host->flags & SDHCI_DEVICE_DEAD)
  896. goto out;
  897. /*
  898. * Reset the chip on each power off.
  899. * Should clear out any weird states.
  900. */
  901. if (ios->power_mode == MMC_POWER_OFF) {
  902. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  903. sdhci_reinit(host);
  904. }
  905. sdhci_set_clock(host, ios->clock);
  906. if (ios->power_mode == MMC_POWER_OFF)
  907. sdhci_set_power(host, -1);
  908. else
  909. sdhci_set_power(host, ios->vdd);
  910. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  911. if (ios->bus_width == MMC_BUS_WIDTH_4)
  912. ctrl |= SDHCI_CTRL_4BITBUS;
  913. else
  914. ctrl &= ~SDHCI_CTRL_4BITBUS;
  915. if (ios->timing == MMC_TIMING_SD_HS)
  916. ctrl |= SDHCI_CTRL_HISPD;
  917. else
  918. ctrl &= ~SDHCI_CTRL_HISPD;
  919. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  920. /*
  921. * Some (ENE) controllers go apeshit on some ios operation,
  922. * signalling timeout and CRC errors even on CMD0. Resetting
  923. * it on each ios seems to solve the problem.
  924. */
  925. if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
  926. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  927. out:
  928. mmiowb();
  929. spin_unlock_irqrestore(&host->lock, flags);
  930. }
  931. static int sdhci_get_ro(struct mmc_host *mmc)
  932. {
  933. struct sdhci_host *host;
  934. unsigned long flags;
  935. int present;
  936. host = mmc_priv(mmc);
  937. spin_lock_irqsave(&host->lock, flags);
  938. if (host->flags & SDHCI_DEVICE_DEAD)
  939. present = 0;
  940. else
  941. present = sdhci_readl(host, SDHCI_PRESENT_STATE);
  942. spin_unlock_irqrestore(&host->lock, flags);
  943. if (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT)
  944. return !!(present & SDHCI_WRITE_PROTECT);
  945. return !(present & SDHCI_WRITE_PROTECT);
  946. }
  947. static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  948. {
  949. struct sdhci_host *host;
  950. unsigned long flags;
  951. host = mmc_priv(mmc);
  952. spin_lock_irqsave(&host->lock, flags);
  953. if (host->flags & SDHCI_DEVICE_DEAD)
  954. goto out;
  955. if (enable)
  956. sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
  957. else
  958. sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
  959. out:
  960. mmiowb();
  961. spin_unlock_irqrestore(&host->lock, flags);
  962. }
  963. static const struct mmc_host_ops sdhci_ops = {
  964. .request = sdhci_request,
  965. .set_ios = sdhci_set_ios,
  966. .get_ro = sdhci_get_ro,
  967. .enable_sdio_irq = sdhci_enable_sdio_irq,
  968. };
  969. /*****************************************************************************\
  970. * *
  971. * Tasklets *
  972. * *
  973. \*****************************************************************************/
  974. static void sdhci_tasklet_card(unsigned long param)
  975. {
  976. struct sdhci_host *host;
  977. unsigned long flags;
  978. host = (struct sdhci_host*)param;
  979. spin_lock_irqsave(&host->lock, flags);
  980. if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
  981. if (host->mrq) {
  982. printk(KERN_ERR "%s: Card removed during transfer!\n",
  983. mmc_hostname(host->mmc));
  984. printk(KERN_ERR "%s: Resetting controller.\n",
  985. mmc_hostname(host->mmc));
  986. sdhci_reset(host, SDHCI_RESET_CMD);
  987. sdhci_reset(host, SDHCI_RESET_DATA);
  988. host->mrq->cmd->error = -ENOMEDIUM;
  989. tasklet_schedule(&host->finish_tasklet);
  990. }
  991. }
  992. spin_unlock_irqrestore(&host->lock, flags);
  993. mmc_detect_change(host->mmc, msecs_to_jiffies(200));
  994. }
  995. static void sdhci_tasklet_finish(unsigned long param)
  996. {
  997. struct sdhci_host *host;
  998. unsigned long flags;
  999. struct mmc_request *mrq;
  1000. host = (struct sdhci_host*)param;
  1001. spin_lock_irqsave(&host->lock, flags);
  1002. del_timer(&host->timer);
  1003. mrq = host->mrq;
  1004. /*
  1005. * The controller needs a reset of internal state machines
  1006. * upon error conditions.
  1007. */
  1008. if (!(host->flags & SDHCI_DEVICE_DEAD) &&
  1009. (mrq->cmd->error ||
  1010. (mrq->data && (mrq->data->error ||
  1011. (mrq->data->stop && mrq->data->stop->error))) ||
  1012. (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
  1013. /* Some controllers need this kick or reset won't work here */
  1014. if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
  1015. unsigned int clock;
  1016. /* This is to force an update */
  1017. clock = host->clock;
  1018. host->clock = 0;
  1019. sdhci_set_clock(host, clock);
  1020. }
  1021. /* Spec says we should do both at the same time, but Ricoh
  1022. controllers do not like that. */
  1023. sdhci_reset(host, SDHCI_RESET_CMD);
  1024. sdhci_reset(host, SDHCI_RESET_DATA);
  1025. }
  1026. host->mrq = NULL;
  1027. host->cmd = NULL;
  1028. host->data = NULL;
  1029. #ifndef SDHCI_USE_LEDS_CLASS
  1030. sdhci_deactivate_led(host);
  1031. #endif
  1032. mmiowb();
  1033. spin_unlock_irqrestore(&host->lock, flags);
  1034. mmc_request_done(host->mmc, mrq);
  1035. }
  1036. static void sdhci_timeout_timer(unsigned long data)
  1037. {
  1038. struct sdhci_host *host;
  1039. unsigned long flags;
  1040. host = (struct sdhci_host*)data;
  1041. spin_lock_irqsave(&host->lock, flags);
  1042. if (host->mrq) {
  1043. printk(KERN_ERR "%s: Timeout waiting for hardware "
  1044. "interrupt.\n", mmc_hostname(host->mmc));
  1045. sdhci_dumpregs(host);
  1046. if (host->data) {
  1047. host->data->error = -ETIMEDOUT;
  1048. sdhci_finish_data(host);
  1049. } else {
  1050. if (host->cmd)
  1051. host->cmd->error = -ETIMEDOUT;
  1052. else
  1053. host->mrq->cmd->error = -ETIMEDOUT;
  1054. tasklet_schedule(&host->finish_tasklet);
  1055. }
  1056. }
  1057. mmiowb();
  1058. spin_unlock_irqrestore(&host->lock, flags);
  1059. }
  1060. /*****************************************************************************\
  1061. * *
  1062. * Interrupt handling *
  1063. * *
  1064. \*****************************************************************************/
  1065. static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
  1066. {
  1067. BUG_ON(intmask == 0);
  1068. if (!host->cmd) {
  1069. printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
  1070. "though no command operation was in progress.\n",
  1071. mmc_hostname(host->mmc), (unsigned)intmask);
  1072. sdhci_dumpregs(host);
  1073. return;
  1074. }
  1075. if (intmask & SDHCI_INT_TIMEOUT)
  1076. host->cmd->error = -ETIMEDOUT;
  1077. else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
  1078. SDHCI_INT_INDEX))
  1079. host->cmd->error = -EILSEQ;
  1080. if (host->cmd->error) {
  1081. tasklet_schedule(&host->finish_tasklet);
  1082. return;
  1083. }
  1084. /*
  1085. * The host can send and interrupt when the busy state has
  1086. * ended, allowing us to wait without wasting CPU cycles.
  1087. * Unfortunately this is overloaded on the "data complete"
  1088. * interrupt, so we need to take some care when handling
  1089. * it.
  1090. *
  1091. * Note: The 1.0 specification is a bit ambiguous about this
  1092. * feature so there might be some problems with older
  1093. * controllers.
  1094. */
  1095. if (host->cmd->flags & MMC_RSP_BUSY) {
  1096. if (host->cmd->data)
  1097. DBG("Cannot wait for busy signal when also "
  1098. "doing a data transfer");
  1099. else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
  1100. return;
  1101. /* The controller does not support the end-of-busy IRQ,
  1102. * fall through and take the SDHCI_INT_RESPONSE */
  1103. }
  1104. if (intmask & SDHCI_INT_RESPONSE)
  1105. sdhci_finish_command(host);
  1106. }
  1107. #ifdef DEBUG
  1108. static void sdhci_show_adma_error(struct sdhci_host *host)
  1109. {
  1110. const char *name = mmc_hostname(host->mmc);
  1111. u8 *desc = host->adma_desc;
  1112. __le32 *dma;
  1113. __le16 *len;
  1114. u8 attr;
  1115. sdhci_dumpregs(host);
  1116. while (true) {
  1117. dma = (__le32 *)(desc + 4);
  1118. len = (__le16 *)(desc + 2);
  1119. attr = *desc;
  1120. DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
  1121. name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
  1122. desc += 8;
  1123. if (attr & 2)
  1124. break;
  1125. }
  1126. }
  1127. #else
  1128. static void sdhci_show_adma_error(struct sdhci_host *host) { }
  1129. #endif
  1130. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  1131. {
  1132. BUG_ON(intmask == 0);
  1133. if (!host->data) {
  1134. /*
  1135. * The "data complete" interrupt is also used to
  1136. * indicate that a busy state has ended. See comment
  1137. * above in sdhci_cmd_irq().
  1138. */
  1139. if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
  1140. if (intmask & SDHCI_INT_DATA_END) {
  1141. sdhci_finish_command(host);
  1142. return;
  1143. }
  1144. }
  1145. printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
  1146. "though no data operation was in progress.\n",
  1147. mmc_hostname(host->mmc), (unsigned)intmask);
  1148. sdhci_dumpregs(host);
  1149. return;
  1150. }
  1151. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  1152. host->data->error = -ETIMEDOUT;
  1153. else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
  1154. host->data->error = -EILSEQ;
  1155. else if (intmask & SDHCI_INT_ADMA_ERROR) {
  1156. printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
  1157. sdhci_show_adma_error(host);
  1158. host->data->error = -EIO;
  1159. }
  1160. if (host->data->error)
  1161. sdhci_finish_data(host);
  1162. else {
  1163. if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  1164. sdhci_transfer_pio(host);
  1165. /*
  1166. * We currently don't do anything fancy with DMA
  1167. * boundaries, but as we can't disable the feature
  1168. * we need to at least restart the transfer.
  1169. */
  1170. if (intmask & SDHCI_INT_DMA_END)
  1171. sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
  1172. SDHCI_DMA_ADDRESS);
  1173. if (intmask & SDHCI_INT_DATA_END) {
  1174. if (host->cmd) {
  1175. /*
  1176. * Data managed to finish before the
  1177. * command completed. Make sure we do
  1178. * things in the proper order.
  1179. */
  1180. host->data_early = 1;
  1181. } else {
  1182. sdhci_finish_data(host);
  1183. }
  1184. }
  1185. }
  1186. }
  1187. static irqreturn_t sdhci_irq(int irq, void *dev_id)
  1188. {
  1189. irqreturn_t result;
  1190. struct sdhci_host* host = dev_id;
  1191. u32 intmask;
  1192. int cardint = 0;
  1193. spin_lock(&host->lock);
  1194. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  1195. if (!intmask || intmask == 0xffffffff) {
  1196. result = IRQ_NONE;
  1197. goto out;
  1198. }
  1199. DBG("*** %s got interrupt: 0x%08x\n",
  1200. mmc_hostname(host->mmc), intmask);
  1201. if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  1202. sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
  1203. SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
  1204. tasklet_schedule(&host->card_tasklet);
  1205. }
  1206. intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
  1207. if (intmask & SDHCI_INT_CMD_MASK) {
  1208. sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
  1209. SDHCI_INT_STATUS);
  1210. sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  1211. }
  1212. if (intmask & SDHCI_INT_DATA_MASK) {
  1213. sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
  1214. SDHCI_INT_STATUS);
  1215. sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  1216. }
  1217. intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
  1218. intmask &= ~SDHCI_INT_ERROR;
  1219. if (intmask & SDHCI_INT_BUS_POWER) {
  1220. printk(KERN_ERR "%s: Card is consuming too much power!\n",
  1221. mmc_hostname(host->mmc));
  1222. sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
  1223. }
  1224. intmask &= ~SDHCI_INT_BUS_POWER;
  1225. if (intmask & SDHCI_INT_CARD_INT)
  1226. cardint = 1;
  1227. intmask &= ~SDHCI_INT_CARD_INT;
  1228. if (intmask) {
  1229. printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
  1230. mmc_hostname(host->mmc), intmask);
  1231. sdhci_dumpregs(host);
  1232. sdhci_writel(host, intmask, SDHCI_INT_STATUS);
  1233. }
  1234. result = IRQ_HANDLED;
  1235. mmiowb();
  1236. out:
  1237. spin_unlock(&host->lock);
  1238. /*
  1239. * We have to delay this as it calls back into the driver.
  1240. */
  1241. if (cardint)
  1242. mmc_signal_sdio_irq(host->mmc);
  1243. return result;
  1244. }
  1245. /*****************************************************************************\
  1246. * *
  1247. * Suspend/resume *
  1248. * *
  1249. \*****************************************************************************/
  1250. #ifdef CONFIG_PM
  1251. int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
  1252. {
  1253. int ret;
  1254. sdhci_disable_card_detection(host);
  1255. ret = mmc_suspend_host(host->mmc, state);
  1256. if (ret)
  1257. return ret;
  1258. free_irq(host->irq, host);
  1259. return 0;
  1260. }
  1261. EXPORT_SYMBOL_GPL(sdhci_suspend_host);
  1262. int sdhci_resume_host(struct sdhci_host *host)
  1263. {
  1264. int ret;
  1265. if (host->flags & SDHCI_USE_DMA) {
  1266. if (host->ops->enable_dma)
  1267. host->ops->enable_dma(host);
  1268. }
  1269. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  1270. mmc_hostname(host->mmc), host);
  1271. if (ret)
  1272. return ret;
  1273. sdhci_init(host);
  1274. mmiowb();
  1275. ret = mmc_resume_host(host->mmc);
  1276. if (ret)
  1277. return ret;
  1278. sdhci_enable_card_detection(host);
  1279. return 0;
  1280. }
  1281. EXPORT_SYMBOL_GPL(sdhci_resume_host);
  1282. #endif /* CONFIG_PM */
  1283. /*****************************************************************************\
  1284. * *
  1285. * Device allocation/registration *
  1286. * *
  1287. \*****************************************************************************/
  1288. struct sdhci_host *sdhci_alloc_host(struct device *dev,
  1289. size_t priv_size)
  1290. {
  1291. struct mmc_host *mmc;
  1292. struct sdhci_host *host;
  1293. WARN_ON(dev == NULL);
  1294. mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
  1295. if (!mmc)
  1296. return ERR_PTR(-ENOMEM);
  1297. host = mmc_priv(mmc);
  1298. host->mmc = mmc;
  1299. return host;
  1300. }
  1301. EXPORT_SYMBOL_GPL(sdhci_alloc_host);
  1302. int sdhci_add_host(struct sdhci_host *host)
  1303. {
  1304. struct mmc_host *mmc;
  1305. unsigned int caps;
  1306. int ret;
  1307. WARN_ON(host == NULL);
  1308. if (host == NULL)
  1309. return -EINVAL;
  1310. mmc = host->mmc;
  1311. if (debug_quirks)
  1312. host->quirks = debug_quirks;
  1313. sdhci_reset(host, SDHCI_RESET_ALL);
  1314. host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
  1315. host->version = (host->version & SDHCI_SPEC_VER_MASK)
  1316. >> SDHCI_SPEC_VER_SHIFT;
  1317. if (host->version > SDHCI_SPEC_200) {
  1318. printk(KERN_ERR "%s: Unknown controller version (%d). "
  1319. "You may experience problems.\n", mmc_hostname(mmc),
  1320. host->version);
  1321. }
  1322. caps = sdhci_readl(host, SDHCI_CAPABILITIES);
  1323. if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
  1324. host->flags |= SDHCI_USE_DMA;
  1325. else if (!(caps & SDHCI_CAN_DO_DMA))
  1326. DBG("Controller doesn't have DMA capability\n");
  1327. else
  1328. host->flags |= SDHCI_USE_DMA;
  1329. if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
  1330. (host->flags & SDHCI_USE_DMA)) {
  1331. DBG("Disabling DMA as it is marked broken\n");
  1332. host->flags &= ~SDHCI_USE_DMA;
  1333. }
  1334. if (host->flags & SDHCI_USE_DMA) {
  1335. if ((host->version >= SDHCI_SPEC_200) &&
  1336. (caps & SDHCI_CAN_DO_ADMA2))
  1337. host->flags |= SDHCI_USE_ADMA;
  1338. }
  1339. if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
  1340. (host->flags & SDHCI_USE_ADMA)) {
  1341. DBG("Disabling ADMA as it is marked broken\n");
  1342. host->flags &= ~SDHCI_USE_ADMA;
  1343. }
  1344. if (host->flags & SDHCI_USE_DMA) {
  1345. if (host->ops->enable_dma) {
  1346. if (host->ops->enable_dma(host)) {
  1347. printk(KERN_WARNING "%s: No suitable DMA "
  1348. "available. Falling back to PIO.\n",
  1349. mmc_hostname(mmc));
  1350. host->flags &= ~(SDHCI_USE_DMA | SDHCI_USE_ADMA);
  1351. }
  1352. }
  1353. }
  1354. if (host->flags & SDHCI_USE_ADMA) {
  1355. /*
  1356. * We need to allocate descriptors for all sg entries
  1357. * (128) and potentially one alignment transfer for
  1358. * each of those entries.
  1359. */
  1360. host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
  1361. host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
  1362. if (!host->adma_desc || !host->align_buffer) {
  1363. kfree(host->adma_desc);
  1364. kfree(host->align_buffer);
  1365. printk(KERN_WARNING "%s: Unable to allocate ADMA "
  1366. "buffers. Falling back to standard DMA.\n",
  1367. mmc_hostname(mmc));
  1368. host->flags &= ~SDHCI_USE_ADMA;
  1369. }
  1370. }
  1371. /*
  1372. * If we use DMA, then it's up to the caller to set the DMA
  1373. * mask, but PIO does not need the hw shim so we set a new
  1374. * mask here in that case.
  1375. */
  1376. if (!(host->flags & SDHCI_USE_DMA)) {
  1377. host->dma_mask = DMA_BIT_MASK(64);
  1378. mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
  1379. }
  1380. host->max_clk =
  1381. (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
  1382. host->max_clk *= 1000000;
  1383. if (host->max_clk == 0) {
  1384. if (!host->ops->get_max_clock) {
  1385. printk(KERN_ERR
  1386. "%s: Hardware doesn't specify base clock "
  1387. "frequency.\n", mmc_hostname(mmc));
  1388. return -ENODEV;
  1389. }
  1390. host->max_clk = host->ops->get_max_clock(host);
  1391. }
  1392. host->timeout_clk =
  1393. (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
  1394. if (host->timeout_clk == 0) {
  1395. if (!host->ops->get_timeout_clock) {
  1396. printk(KERN_ERR
  1397. "%s: Hardware doesn't specify timeout clock "
  1398. "frequency.\n", mmc_hostname(mmc));
  1399. return -ENODEV;
  1400. }
  1401. host->timeout_clk = host->ops->get_timeout_clock(host);
  1402. }
  1403. if (caps & SDHCI_TIMEOUT_CLK_UNIT)
  1404. host->timeout_clk *= 1000;
  1405. /*
  1406. * Set host parameters.
  1407. */
  1408. mmc->ops = &sdhci_ops;
  1409. mmc->f_min = host->max_clk / 256;
  1410. mmc->f_max = host->max_clk;
  1411. mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
  1412. if (caps & SDHCI_CAN_DO_HISPD)
  1413. mmc->caps |= MMC_CAP_SD_HIGHSPEED;
  1414. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  1415. mmc->caps |= MMC_CAP_NEEDS_POLL;
  1416. mmc->ocr_avail = 0;
  1417. if (caps & SDHCI_CAN_VDD_330)
  1418. mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
  1419. if (caps & SDHCI_CAN_VDD_300)
  1420. mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
  1421. if (caps & SDHCI_CAN_VDD_180)
  1422. mmc->ocr_avail |= MMC_VDD_165_195;
  1423. if (mmc->ocr_avail == 0) {
  1424. printk(KERN_ERR "%s: Hardware doesn't report any "
  1425. "support voltages.\n", mmc_hostname(mmc));
  1426. return -ENODEV;
  1427. }
  1428. spin_lock_init(&host->lock);
  1429. /*
  1430. * Maximum number of segments. Depends on if the hardware
  1431. * can do scatter/gather or not.
  1432. */
  1433. if (host->flags & SDHCI_USE_ADMA)
  1434. mmc->max_hw_segs = 128;
  1435. else if (host->flags & SDHCI_USE_DMA)
  1436. mmc->max_hw_segs = 1;
  1437. else /* PIO */
  1438. mmc->max_hw_segs = 128;
  1439. mmc->max_phys_segs = 128;
  1440. /*
  1441. * Maximum number of sectors in one transfer. Limited by DMA boundary
  1442. * size (512KiB).
  1443. */
  1444. mmc->max_req_size = 524288;
  1445. /*
  1446. * Maximum segment size. Could be one segment with the maximum number
  1447. * of bytes. When doing hardware scatter/gather, each entry cannot
  1448. * be larger than 64 KiB though.
  1449. */
  1450. if (host->flags & SDHCI_USE_ADMA)
  1451. mmc->max_seg_size = 65536;
  1452. else
  1453. mmc->max_seg_size = mmc->max_req_size;
  1454. /*
  1455. * Maximum block size. This varies from controller to controller and
  1456. * is specified in the capabilities register.
  1457. */
  1458. if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
  1459. mmc->max_blk_size = 2;
  1460. } else {
  1461. mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
  1462. SDHCI_MAX_BLOCK_SHIFT;
  1463. if (mmc->max_blk_size >= 3) {
  1464. printk(KERN_WARNING "%s: Invalid maximum block size, "
  1465. "assuming 512 bytes\n", mmc_hostname(mmc));
  1466. mmc->max_blk_size = 0;
  1467. }
  1468. }
  1469. mmc->max_blk_size = 512 << mmc->max_blk_size;
  1470. /*
  1471. * Maximum block count.
  1472. */
  1473. mmc->max_blk_count = 65535;
  1474. /*
  1475. * Init tasklets.
  1476. */
  1477. tasklet_init(&host->card_tasklet,
  1478. sdhci_tasklet_card, (unsigned long)host);
  1479. tasklet_init(&host->finish_tasklet,
  1480. sdhci_tasklet_finish, (unsigned long)host);
  1481. setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
  1482. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  1483. mmc_hostname(mmc), host);
  1484. if (ret)
  1485. goto untasklet;
  1486. sdhci_init(host);
  1487. #ifdef CONFIG_MMC_DEBUG
  1488. sdhci_dumpregs(host);
  1489. #endif
  1490. #ifdef SDHCI_USE_LEDS_CLASS
  1491. snprintf(host->led_name, sizeof(host->led_name),
  1492. "%s::", mmc_hostname(mmc));
  1493. host->led.name = host->led_name;
  1494. host->led.brightness = LED_OFF;
  1495. host->led.default_trigger = mmc_hostname(mmc);
  1496. host->led.brightness_set = sdhci_led_control;
  1497. ret = led_classdev_register(mmc_dev(mmc), &host->led);
  1498. if (ret)
  1499. goto reset;
  1500. #endif
  1501. mmiowb();
  1502. mmc_add_host(mmc);
  1503. printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s%s\n",
  1504. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  1505. (host->flags & SDHCI_USE_ADMA)?"A":"",
  1506. (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
  1507. sdhci_enable_card_detection(host);
  1508. return 0;
  1509. #ifdef SDHCI_USE_LEDS_CLASS
  1510. reset:
  1511. sdhci_reset(host, SDHCI_RESET_ALL);
  1512. free_irq(host->irq, host);
  1513. #endif
  1514. untasklet:
  1515. tasklet_kill(&host->card_tasklet);
  1516. tasklet_kill(&host->finish_tasklet);
  1517. return ret;
  1518. }
  1519. EXPORT_SYMBOL_GPL(sdhci_add_host);
  1520. void sdhci_remove_host(struct sdhci_host *host, int dead)
  1521. {
  1522. unsigned long flags;
  1523. if (dead) {
  1524. spin_lock_irqsave(&host->lock, flags);
  1525. host->flags |= SDHCI_DEVICE_DEAD;
  1526. if (host->mrq) {
  1527. printk(KERN_ERR "%s: Controller removed during "
  1528. " transfer!\n", mmc_hostname(host->mmc));
  1529. host->mrq->cmd->error = -ENOMEDIUM;
  1530. tasklet_schedule(&host->finish_tasklet);
  1531. }
  1532. spin_unlock_irqrestore(&host->lock, flags);
  1533. }
  1534. sdhci_disable_card_detection(host);
  1535. mmc_remove_host(host->mmc);
  1536. #ifdef SDHCI_USE_LEDS_CLASS
  1537. led_classdev_unregister(&host->led);
  1538. #endif
  1539. if (!dead)
  1540. sdhci_reset(host, SDHCI_RESET_ALL);
  1541. free_irq(host->irq, host);
  1542. del_timer_sync(&host->timer);
  1543. tasklet_kill(&host->card_tasklet);
  1544. tasklet_kill(&host->finish_tasklet);
  1545. kfree(host->adma_desc);
  1546. kfree(host->align_buffer);
  1547. host->adma_desc = NULL;
  1548. host->align_buffer = NULL;
  1549. }
  1550. EXPORT_SYMBOL_GPL(sdhci_remove_host);
  1551. void sdhci_free_host(struct sdhci_host *host)
  1552. {
  1553. mmc_free_host(host->mmc);
  1554. }
  1555. EXPORT_SYMBOL_GPL(sdhci_free_host);
  1556. /*****************************************************************************\
  1557. * *
  1558. * Driver init/exit *
  1559. * *
  1560. \*****************************************************************************/
  1561. static int __init sdhci_drv_init(void)
  1562. {
  1563. printk(KERN_INFO DRIVER_NAME
  1564. ": Secure Digital Host Controller Interface driver\n");
  1565. printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  1566. return 0;
  1567. }
  1568. static void __exit sdhci_drv_exit(void)
  1569. {
  1570. }
  1571. module_init(sdhci_drv_init);
  1572. module_exit(sdhci_drv_exit);
  1573. module_param(debug_quirks, uint, 0444);
  1574. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  1575. MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
  1576. MODULE_LICENSE("GPL");
  1577. MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");