sdhci.c 52 KB

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