sdhci.c 48 KB

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