sdhci.c 45 KB

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