sdhci.c 43 KB

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