sdhci.c 43 KB

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