sdhci.c 48 KB

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