sdhci.c 44 KB

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