sdhci.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891
  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. #define MAX_TUNING_LOOP 40
  34. static unsigned int debug_quirks = 0;
  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 int sdhci_execute_tuning(struct mmc_host *mmc);
  39. static void sdhci_tuning_timer(unsigned long data);
  40. static void sdhci_dumpregs(struct sdhci_host *host)
  41. {
  42. printk(KERN_DEBUG DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
  43. mmc_hostname(host->mmc));
  44. printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
  45. sdhci_readl(host, SDHCI_DMA_ADDRESS),
  46. sdhci_readw(host, SDHCI_HOST_VERSION));
  47. printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
  48. sdhci_readw(host, SDHCI_BLOCK_SIZE),
  49. sdhci_readw(host, SDHCI_BLOCK_COUNT));
  50. printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
  51. sdhci_readl(host, SDHCI_ARGUMENT),
  52. sdhci_readw(host, SDHCI_TRANSFER_MODE));
  53. printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
  54. sdhci_readl(host, SDHCI_PRESENT_STATE),
  55. sdhci_readb(host, SDHCI_HOST_CONTROL));
  56. printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
  57. sdhci_readb(host, SDHCI_POWER_CONTROL),
  58. sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
  59. printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
  60. sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
  61. sdhci_readw(host, SDHCI_CLOCK_CONTROL));
  62. printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
  63. sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
  64. sdhci_readl(host, SDHCI_INT_STATUS));
  65. printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
  66. sdhci_readl(host, SDHCI_INT_ENABLE),
  67. sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
  68. printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
  69. sdhci_readw(host, SDHCI_ACMD12_ERR),
  70. sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
  71. printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
  72. sdhci_readl(host, SDHCI_CAPABILITIES),
  73. sdhci_readl(host, SDHCI_CAPABILITIES_1));
  74. printk(KERN_DEBUG DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
  75. sdhci_readw(host, SDHCI_COMMAND),
  76. sdhci_readl(host, SDHCI_MAX_CURRENT));
  77. printk(KERN_DEBUG DRIVER_NAME ": Host ctl2: 0x%08x\n",
  78. sdhci_readw(host, SDHCI_HOST_CONTROL2));
  79. if (host->flags & SDHCI_USE_ADMA)
  80. printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
  81. readl(host->ioaddr + SDHCI_ADMA_ERROR),
  82. readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
  83. printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
  84. }
  85. /*****************************************************************************\
  86. * *
  87. * Low level functions *
  88. * *
  89. \*****************************************************************************/
  90. static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
  91. {
  92. u32 ier;
  93. ier = sdhci_readl(host, SDHCI_INT_ENABLE);
  94. ier &= ~clear;
  95. ier |= set;
  96. sdhci_writel(host, ier, SDHCI_INT_ENABLE);
  97. sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
  98. }
  99. static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
  100. {
  101. sdhci_clear_set_irqs(host, 0, irqs);
  102. }
  103. static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
  104. {
  105. sdhci_clear_set_irqs(host, irqs, 0);
  106. }
  107. static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
  108. {
  109. u32 present, irqs;
  110. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  111. return;
  112. present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  113. SDHCI_CARD_PRESENT;
  114. irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
  115. if (enable)
  116. sdhci_unmask_irqs(host, irqs);
  117. else
  118. sdhci_mask_irqs(host, irqs);
  119. }
  120. static void sdhci_enable_card_detection(struct sdhci_host *host)
  121. {
  122. sdhci_set_card_detection(host, true);
  123. }
  124. static void sdhci_disable_card_detection(struct sdhci_host *host)
  125. {
  126. sdhci_set_card_detection(host, false);
  127. }
  128. static void sdhci_reset(struct sdhci_host *host, u8 mask)
  129. {
  130. unsigned long timeout;
  131. u32 uninitialized_var(ier);
  132. if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
  133. if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
  134. SDHCI_CARD_PRESENT))
  135. return;
  136. }
  137. if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
  138. ier = sdhci_readl(host, SDHCI_INT_ENABLE);
  139. if (host->ops->platform_reset_enter)
  140. host->ops->platform_reset_enter(host, mask);
  141. sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
  142. if (mask & SDHCI_RESET_ALL)
  143. host->clock = 0;
  144. /* Wait max 100 ms */
  145. timeout = 100;
  146. /* hw clears the bit when it's done */
  147. while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
  148. if (timeout == 0) {
  149. printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
  150. mmc_hostname(host->mmc), (int)mask);
  151. sdhci_dumpregs(host);
  152. return;
  153. }
  154. timeout--;
  155. mdelay(1);
  156. }
  157. if (host->ops->platform_reset_exit)
  158. host->ops->platform_reset_exit(host, mask);
  159. if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
  160. sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
  161. }
  162. static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
  163. static void sdhci_init(struct sdhci_host *host, int soft)
  164. {
  165. if (soft)
  166. sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
  167. else
  168. sdhci_reset(host, SDHCI_RESET_ALL);
  169. sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
  170. SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
  171. SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
  172. SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
  173. SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
  174. if (soft) {
  175. /* force clock reconfiguration */
  176. host->clock = 0;
  177. sdhci_set_ios(host->mmc, &host->mmc->ios);
  178. }
  179. }
  180. static void sdhci_reinit(struct sdhci_host *host)
  181. {
  182. sdhci_init(host, 0);
  183. sdhci_enable_card_detection(host);
  184. }
  185. static void sdhci_activate_led(struct sdhci_host *host)
  186. {
  187. u8 ctrl;
  188. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  189. ctrl |= SDHCI_CTRL_LED;
  190. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  191. }
  192. static void sdhci_deactivate_led(struct sdhci_host *host)
  193. {
  194. u8 ctrl;
  195. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  196. ctrl &= ~SDHCI_CTRL_LED;
  197. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  198. }
  199. #ifdef SDHCI_USE_LEDS_CLASS
  200. static void sdhci_led_control(struct led_classdev *led,
  201. enum led_brightness brightness)
  202. {
  203. struct sdhci_host *host = container_of(led, struct sdhci_host, led);
  204. unsigned long flags;
  205. spin_lock_irqsave(&host->lock, flags);
  206. if (brightness == LED_OFF)
  207. sdhci_deactivate_led(host);
  208. else
  209. sdhci_activate_led(host);
  210. spin_unlock_irqrestore(&host->lock, flags);
  211. }
  212. #endif
  213. /*****************************************************************************\
  214. * *
  215. * Core functions *
  216. * *
  217. \*****************************************************************************/
  218. static void sdhci_read_block_pio(struct sdhci_host *host)
  219. {
  220. unsigned long flags;
  221. size_t blksize, len, chunk;
  222. u32 uninitialized_var(scratch);
  223. u8 *buf;
  224. DBG("PIO reading\n");
  225. blksize = host->data->blksz;
  226. chunk = 0;
  227. local_irq_save(flags);
  228. while (blksize) {
  229. if (!sg_miter_next(&host->sg_miter))
  230. BUG();
  231. len = min(host->sg_miter.length, blksize);
  232. blksize -= len;
  233. host->sg_miter.consumed = len;
  234. buf = host->sg_miter.addr;
  235. while (len) {
  236. if (chunk == 0) {
  237. scratch = sdhci_readl(host, SDHCI_BUFFER);
  238. chunk = 4;
  239. }
  240. *buf = scratch & 0xFF;
  241. buf++;
  242. scratch >>= 8;
  243. chunk--;
  244. len--;
  245. }
  246. }
  247. sg_miter_stop(&host->sg_miter);
  248. local_irq_restore(flags);
  249. }
  250. static void sdhci_write_block_pio(struct sdhci_host *host)
  251. {
  252. unsigned long flags;
  253. size_t blksize, len, chunk;
  254. u32 scratch;
  255. u8 *buf;
  256. DBG("PIO writing\n");
  257. blksize = host->data->blksz;
  258. chunk = 0;
  259. scratch = 0;
  260. local_irq_save(flags);
  261. while (blksize) {
  262. if (!sg_miter_next(&host->sg_miter))
  263. BUG();
  264. len = min(host->sg_miter.length, blksize);
  265. blksize -= len;
  266. host->sg_miter.consumed = len;
  267. buf = host->sg_miter.addr;
  268. while (len) {
  269. scratch |= (u32)*buf << (chunk * 8);
  270. buf++;
  271. chunk++;
  272. len--;
  273. if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
  274. sdhci_writel(host, scratch, SDHCI_BUFFER);
  275. chunk = 0;
  276. scratch = 0;
  277. }
  278. }
  279. }
  280. sg_miter_stop(&host->sg_miter);
  281. local_irq_restore(flags);
  282. }
  283. static void sdhci_transfer_pio(struct sdhci_host *host)
  284. {
  285. u32 mask;
  286. BUG_ON(!host->data);
  287. if (host->blocks == 0)
  288. return;
  289. if (host->data->flags & MMC_DATA_READ)
  290. mask = SDHCI_DATA_AVAILABLE;
  291. else
  292. mask = SDHCI_SPACE_AVAILABLE;
  293. /*
  294. * Some controllers (JMicron JMB38x) mess up the buffer bits
  295. * for transfers < 4 bytes. As long as it is just one block,
  296. * we can ignore the bits.
  297. */
  298. if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
  299. (host->data->blocks == 1))
  300. mask = ~0;
  301. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  302. if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
  303. udelay(100);
  304. if (host->data->flags & MMC_DATA_READ)
  305. sdhci_read_block_pio(host);
  306. else
  307. sdhci_write_block_pio(host);
  308. host->blocks--;
  309. if (host->blocks == 0)
  310. break;
  311. }
  312. DBG("PIO transfer complete.\n");
  313. }
  314. static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
  315. {
  316. local_irq_save(*flags);
  317. return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
  318. }
  319. static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
  320. {
  321. kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
  322. local_irq_restore(*flags);
  323. }
  324. static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
  325. {
  326. __le32 *dataddr = (__le32 __force *)(desc + 4);
  327. __le16 *cmdlen = (__le16 __force *)desc;
  328. /* SDHCI specification says ADMA descriptors should be 4 byte
  329. * aligned, so using 16 or 32bit operations should be safe. */
  330. cmdlen[0] = cpu_to_le16(cmd);
  331. cmdlen[1] = cpu_to_le16(len);
  332. dataddr[0] = cpu_to_le32(addr);
  333. }
  334. static int sdhci_adma_table_pre(struct sdhci_host *host,
  335. struct mmc_data *data)
  336. {
  337. int direction;
  338. u8 *desc;
  339. u8 *align;
  340. dma_addr_t addr;
  341. dma_addr_t align_addr;
  342. int len, offset;
  343. struct scatterlist *sg;
  344. int i;
  345. char *buffer;
  346. unsigned long flags;
  347. /*
  348. * The spec does not specify endianness of descriptor table.
  349. * We currently guess that it is LE.
  350. */
  351. if (data->flags & MMC_DATA_READ)
  352. direction = DMA_FROM_DEVICE;
  353. else
  354. direction = DMA_TO_DEVICE;
  355. /*
  356. * The ADMA descriptor table is mapped further down as we
  357. * need to fill it with data first.
  358. */
  359. host->align_addr = dma_map_single(mmc_dev(host->mmc),
  360. host->align_buffer, 128 * 4, direction);
  361. if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
  362. goto fail;
  363. BUG_ON(host->align_addr & 0x3);
  364. host->sg_count = dma_map_sg(mmc_dev(host->mmc),
  365. data->sg, data->sg_len, direction);
  366. if (host->sg_count == 0)
  367. goto unmap_align;
  368. desc = host->adma_desc;
  369. align = host->align_buffer;
  370. align_addr = host->align_addr;
  371. for_each_sg(data->sg, sg, host->sg_count, i) {
  372. addr = sg_dma_address(sg);
  373. len = sg_dma_len(sg);
  374. /*
  375. * The SDHCI specification states that ADMA
  376. * addresses must be 32-bit aligned. If they
  377. * aren't, then we use a bounce buffer for
  378. * the (up to three) bytes that screw up the
  379. * alignment.
  380. */
  381. offset = (4 - (addr & 0x3)) & 0x3;
  382. if (offset) {
  383. if (data->flags & MMC_DATA_WRITE) {
  384. buffer = sdhci_kmap_atomic(sg, &flags);
  385. WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
  386. memcpy(align, buffer, offset);
  387. sdhci_kunmap_atomic(buffer, &flags);
  388. }
  389. /* tran, valid */
  390. sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
  391. BUG_ON(offset > 65536);
  392. align += 4;
  393. align_addr += 4;
  394. desc += 8;
  395. addr += offset;
  396. len -= offset;
  397. }
  398. BUG_ON(len > 65536);
  399. /* tran, valid */
  400. sdhci_set_adma_desc(desc, addr, len, 0x21);
  401. desc += 8;
  402. /*
  403. * If this triggers then we have a calculation bug
  404. * somewhere. :/
  405. */
  406. WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
  407. }
  408. if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
  409. /*
  410. * Mark the last descriptor as the terminating descriptor
  411. */
  412. if (desc != host->adma_desc) {
  413. desc -= 8;
  414. desc[0] |= 0x2; /* end */
  415. }
  416. } else {
  417. /*
  418. * Add a terminating entry.
  419. */
  420. /* nop, end, valid */
  421. sdhci_set_adma_desc(desc, 0, 0, 0x3);
  422. }
  423. /*
  424. * Resync align buffer as we might have changed it.
  425. */
  426. if (data->flags & MMC_DATA_WRITE) {
  427. dma_sync_single_for_device(mmc_dev(host->mmc),
  428. host->align_addr, 128 * 4, direction);
  429. }
  430. host->adma_addr = dma_map_single(mmc_dev(host->mmc),
  431. host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
  432. if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
  433. goto unmap_entries;
  434. BUG_ON(host->adma_addr & 0x3);
  435. return 0;
  436. unmap_entries:
  437. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  438. data->sg_len, direction);
  439. unmap_align:
  440. dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
  441. 128 * 4, direction);
  442. fail:
  443. return -EINVAL;
  444. }
  445. static void sdhci_adma_table_post(struct sdhci_host *host,
  446. struct mmc_data *data)
  447. {
  448. int direction;
  449. struct scatterlist *sg;
  450. int i, size;
  451. u8 *align;
  452. char *buffer;
  453. unsigned long flags;
  454. if (data->flags & MMC_DATA_READ)
  455. direction = DMA_FROM_DEVICE;
  456. else
  457. direction = DMA_TO_DEVICE;
  458. dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
  459. (128 * 2 + 1) * 4, DMA_TO_DEVICE);
  460. dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
  461. 128 * 4, direction);
  462. if (data->flags & MMC_DATA_READ) {
  463. dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
  464. data->sg_len, direction);
  465. align = host->align_buffer;
  466. for_each_sg(data->sg, sg, host->sg_count, i) {
  467. if (sg_dma_address(sg) & 0x3) {
  468. size = 4 - (sg_dma_address(sg) & 0x3);
  469. buffer = sdhci_kmap_atomic(sg, &flags);
  470. WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
  471. memcpy(buffer, align, size);
  472. sdhci_kunmap_atomic(buffer, &flags);
  473. align += 4;
  474. }
  475. }
  476. }
  477. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  478. data->sg_len, direction);
  479. }
  480. static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
  481. {
  482. u8 count;
  483. struct mmc_data *data = cmd->data;
  484. unsigned target_timeout, current_timeout;
  485. /*
  486. * If the host controller provides us with an incorrect timeout
  487. * value, just skip the check and use 0xE. The hardware may take
  488. * longer to time out, but that's much better than having a too-short
  489. * timeout value.
  490. */
  491. if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
  492. return 0xE;
  493. /* Unspecified timeout, assume max */
  494. if (!data && !cmd->cmd_timeout_ms)
  495. return 0xE;
  496. /* timeout in us */
  497. if (!data)
  498. target_timeout = cmd->cmd_timeout_ms * 1000;
  499. else
  500. target_timeout = data->timeout_ns / 1000 +
  501. data->timeout_clks / host->clock;
  502. if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
  503. host->timeout_clk = host->clock / 1000;
  504. /*
  505. * Figure out needed cycles.
  506. * We do this in steps in order to fit inside a 32 bit int.
  507. * The first step is the minimum timeout, which will have a
  508. * minimum resolution of 6 bits:
  509. * (1) 2^13*1000 > 2^22,
  510. * (2) host->timeout_clk < 2^16
  511. * =>
  512. * (1) / (2) > 2^6
  513. */
  514. BUG_ON(!host->timeout_clk);
  515. count = 0;
  516. current_timeout = (1 << 13) * 1000 / host->timeout_clk;
  517. while (current_timeout < target_timeout) {
  518. count++;
  519. current_timeout <<= 1;
  520. if (count >= 0xF)
  521. break;
  522. }
  523. if (count >= 0xF) {
  524. printk(KERN_WARNING "%s: Too large timeout requested for CMD%d!\n",
  525. mmc_hostname(host->mmc), cmd->opcode);
  526. count = 0xE;
  527. }
  528. return count;
  529. }
  530. static void sdhci_set_transfer_irqs(struct sdhci_host *host)
  531. {
  532. u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  533. u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  534. if (host->flags & SDHCI_REQ_USE_DMA)
  535. sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
  536. else
  537. sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
  538. }
  539. static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
  540. {
  541. u8 count;
  542. u8 ctrl;
  543. struct mmc_data *data = cmd->data;
  544. int ret;
  545. WARN_ON(host->data);
  546. if (data || (cmd->flags & MMC_RSP_BUSY)) {
  547. count = sdhci_calc_timeout(host, cmd);
  548. sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
  549. }
  550. if (!data)
  551. return;
  552. /* Sanity checks */
  553. BUG_ON(data->blksz * data->blocks > 524288);
  554. BUG_ON(data->blksz > host->mmc->max_blk_size);
  555. BUG_ON(data->blocks > 65535);
  556. host->data = data;
  557. host->data_early = 0;
  558. host->data->bytes_xfered = 0;
  559. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
  560. host->flags |= SDHCI_REQ_USE_DMA;
  561. /*
  562. * FIXME: This doesn't account for merging when mapping the
  563. * scatterlist.
  564. */
  565. if (host->flags & SDHCI_REQ_USE_DMA) {
  566. int broken, i;
  567. struct scatterlist *sg;
  568. broken = 0;
  569. if (host->flags & SDHCI_USE_ADMA) {
  570. if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
  571. broken = 1;
  572. } else {
  573. if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
  574. broken = 1;
  575. }
  576. if (unlikely(broken)) {
  577. for_each_sg(data->sg, sg, data->sg_len, i) {
  578. if (sg->length & 0x3) {
  579. DBG("Reverting to PIO because of "
  580. "transfer size (%d)\n",
  581. sg->length);
  582. host->flags &= ~SDHCI_REQ_USE_DMA;
  583. break;
  584. }
  585. }
  586. }
  587. }
  588. /*
  589. * The assumption here being that alignment is the same after
  590. * translation to device address space.
  591. */
  592. if (host->flags & SDHCI_REQ_USE_DMA) {
  593. int broken, i;
  594. struct scatterlist *sg;
  595. broken = 0;
  596. if (host->flags & SDHCI_USE_ADMA) {
  597. /*
  598. * As we use 3 byte chunks to work around
  599. * alignment problems, we need to check this
  600. * quirk.
  601. */
  602. if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
  603. broken = 1;
  604. } else {
  605. if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
  606. broken = 1;
  607. }
  608. if (unlikely(broken)) {
  609. for_each_sg(data->sg, sg, data->sg_len, i) {
  610. if (sg->offset & 0x3) {
  611. DBG("Reverting to PIO because of "
  612. "bad alignment\n");
  613. host->flags &= ~SDHCI_REQ_USE_DMA;
  614. break;
  615. }
  616. }
  617. }
  618. }
  619. if (host->flags & SDHCI_REQ_USE_DMA) {
  620. if (host->flags & SDHCI_USE_ADMA) {
  621. ret = sdhci_adma_table_pre(host, data);
  622. if (ret) {
  623. /*
  624. * This only happens when someone fed
  625. * us an invalid request.
  626. */
  627. WARN_ON(1);
  628. host->flags &= ~SDHCI_REQ_USE_DMA;
  629. } else {
  630. sdhci_writel(host, host->adma_addr,
  631. SDHCI_ADMA_ADDRESS);
  632. }
  633. } else {
  634. int sg_cnt;
  635. sg_cnt = dma_map_sg(mmc_dev(host->mmc),
  636. data->sg, data->sg_len,
  637. (data->flags & MMC_DATA_READ) ?
  638. DMA_FROM_DEVICE :
  639. DMA_TO_DEVICE);
  640. if (sg_cnt == 0) {
  641. /*
  642. * This only happens when someone fed
  643. * us an invalid request.
  644. */
  645. WARN_ON(1);
  646. host->flags &= ~SDHCI_REQ_USE_DMA;
  647. } else {
  648. WARN_ON(sg_cnt != 1);
  649. sdhci_writel(host, sg_dma_address(data->sg),
  650. SDHCI_DMA_ADDRESS);
  651. }
  652. }
  653. }
  654. /*
  655. * Always adjust the DMA selection as some controllers
  656. * (e.g. JMicron) can't do PIO properly when the selection
  657. * is ADMA.
  658. */
  659. if (host->version >= SDHCI_SPEC_200) {
  660. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  661. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  662. if ((host->flags & SDHCI_REQ_USE_DMA) &&
  663. (host->flags & SDHCI_USE_ADMA))
  664. ctrl |= SDHCI_CTRL_ADMA32;
  665. else
  666. ctrl |= SDHCI_CTRL_SDMA;
  667. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  668. }
  669. if (!(host->flags & SDHCI_REQ_USE_DMA)) {
  670. int flags;
  671. flags = SG_MITER_ATOMIC;
  672. if (host->data->flags & MMC_DATA_READ)
  673. flags |= SG_MITER_TO_SG;
  674. else
  675. flags |= SG_MITER_FROM_SG;
  676. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  677. host->blocks = data->blocks;
  678. }
  679. sdhci_set_transfer_irqs(host);
  680. /* Set the DMA boundary value and block size */
  681. sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
  682. data->blksz), SDHCI_BLOCK_SIZE);
  683. sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  684. }
  685. static void sdhci_set_transfer_mode(struct sdhci_host *host,
  686. struct mmc_command *cmd)
  687. {
  688. u16 mode;
  689. struct mmc_data *data = cmd->data;
  690. if (data == NULL)
  691. return;
  692. WARN_ON(!host->data);
  693. mode = SDHCI_TRNS_BLK_CNT_EN;
  694. if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
  695. mode |= SDHCI_TRNS_MULTI;
  696. /*
  697. * If we are sending CMD23, CMD12 never gets sent
  698. * on successful completion (so no Auto-CMD12).
  699. */
  700. if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
  701. mode |= SDHCI_TRNS_AUTO_CMD12;
  702. else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
  703. mode |= SDHCI_TRNS_AUTO_CMD23;
  704. sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
  705. }
  706. }
  707. if (data->flags & MMC_DATA_READ)
  708. mode |= SDHCI_TRNS_READ;
  709. if (host->flags & SDHCI_REQ_USE_DMA)
  710. mode |= SDHCI_TRNS_DMA;
  711. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  712. }
  713. static void sdhci_finish_data(struct sdhci_host *host)
  714. {
  715. struct mmc_data *data;
  716. BUG_ON(!host->data);
  717. data = host->data;
  718. host->data = NULL;
  719. if (host->flags & SDHCI_REQ_USE_DMA) {
  720. if (host->flags & SDHCI_USE_ADMA)
  721. sdhci_adma_table_post(host, data);
  722. else {
  723. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  724. data->sg_len, (data->flags & MMC_DATA_READ) ?
  725. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  726. }
  727. }
  728. /*
  729. * The specification states that the block count register must
  730. * be updated, but it does not specify at what point in the
  731. * data flow. That makes the register entirely useless to read
  732. * back so we have to assume that nothing made it to the card
  733. * in the event of an error.
  734. */
  735. if (data->error)
  736. data->bytes_xfered = 0;
  737. else
  738. data->bytes_xfered = data->blksz * data->blocks;
  739. /*
  740. * Need to send CMD12 if -
  741. * a) open-ended multiblock transfer (no CMD23)
  742. * b) error in multiblock transfer
  743. */
  744. if (data->stop &&
  745. (data->error ||
  746. !host->mrq->sbc)) {
  747. /*
  748. * The controller needs a reset of internal state machines
  749. * upon error conditions.
  750. */
  751. if (data->error) {
  752. sdhci_reset(host, SDHCI_RESET_CMD);
  753. sdhci_reset(host, SDHCI_RESET_DATA);
  754. }
  755. sdhci_send_command(host, data->stop);
  756. } else
  757. tasklet_schedule(&host->finish_tasklet);
  758. }
  759. static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
  760. {
  761. int flags;
  762. u32 mask;
  763. unsigned long timeout;
  764. WARN_ON(host->cmd);
  765. /* Wait max 10 ms */
  766. timeout = 10;
  767. mask = SDHCI_CMD_INHIBIT;
  768. if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  769. mask |= SDHCI_DATA_INHIBIT;
  770. /* We shouldn't wait for data inihibit for stop commands, even
  771. though they might use busy signaling */
  772. if (host->mrq->data && (cmd == host->mrq->data->stop))
  773. mask &= ~SDHCI_DATA_INHIBIT;
  774. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  775. if (timeout == 0) {
  776. printk(KERN_ERR "%s: Controller never released "
  777. "inhibit bit(s).\n", mmc_hostname(host->mmc));
  778. sdhci_dumpregs(host);
  779. cmd->error = -EIO;
  780. tasklet_schedule(&host->finish_tasklet);
  781. return;
  782. }
  783. timeout--;
  784. mdelay(1);
  785. }
  786. mod_timer(&host->timer, jiffies + 10 * HZ);
  787. host->cmd = cmd;
  788. sdhci_prepare_data(host, cmd);
  789. sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
  790. sdhci_set_transfer_mode(host, cmd);
  791. if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  792. printk(KERN_ERR "%s: Unsupported response type!\n",
  793. mmc_hostname(host->mmc));
  794. cmd->error = -EINVAL;
  795. tasklet_schedule(&host->finish_tasklet);
  796. return;
  797. }
  798. if (!(cmd->flags & MMC_RSP_PRESENT))
  799. flags = SDHCI_CMD_RESP_NONE;
  800. else if (cmd->flags & MMC_RSP_136)
  801. flags = SDHCI_CMD_RESP_LONG;
  802. else if (cmd->flags & MMC_RSP_BUSY)
  803. flags = SDHCI_CMD_RESP_SHORT_BUSY;
  804. else
  805. flags = SDHCI_CMD_RESP_SHORT;
  806. if (cmd->flags & MMC_RSP_CRC)
  807. flags |= SDHCI_CMD_CRC;
  808. if (cmd->flags & MMC_RSP_OPCODE)
  809. flags |= SDHCI_CMD_INDEX;
  810. /* CMD19 is special in that the Data Present Select should be set */
  811. if (cmd->data || (cmd->opcode == MMC_SEND_TUNING_BLOCK))
  812. flags |= SDHCI_CMD_DATA;
  813. sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
  814. }
  815. static void sdhci_finish_command(struct sdhci_host *host)
  816. {
  817. int i;
  818. BUG_ON(host->cmd == NULL);
  819. if (host->cmd->flags & MMC_RSP_PRESENT) {
  820. if (host->cmd->flags & MMC_RSP_136) {
  821. /* CRC is stripped so we need to do some shifting. */
  822. for (i = 0;i < 4;i++) {
  823. host->cmd->resp[i] = sdhci_readl(host,
  824. SDHCI_RESPONSE + (3-i)*4) << 8;
  825. if (i != 3)
  826. host->cmd->resp[i] |=
  827. sdhci_readb(host,
  828. SDHCI_RESPONSE + (3-i)*4-1);
  829. }
  830. } else {
  831. host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
  832. }
  833. }
  834. host->cmd->error = 0;
  835. /* Finished CMD23, now send actual command. */
  836. if (host->cmd == host->mrq->sbc) {
  837. host->cmd = NULL;
  838. sdhci_send_command(host, host->mrq->cmd);
  839. } else {
  840. /* Processed actual command. */
  841. if (host->data && host->data_early)
  842. sdhci_finish_data(host);
  843. if (!host->cmd->data)
  844. tasklet_schedule(&host->finish_tasklet);
  845. host->cmd = NULL;
  846. }
  847. }
  848. static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
  849. {
  850. int div = 0; /* Initialized for compiler warning */
  851. u16 clk = 0;
  852. unsigned long timeout;
  853. if (clock == host->clock)
  854. return;
  855. if (host->ops->set_clock) {
  856. host->ops->set_clock(host, clock);
  857. if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
  858. return;
  859. }
  860. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  861. if (clock == 0)
  862. goto out;
  863. if (host->version >= SDHCI_SPEC_300) {
  864. /*
  865. * Check if the Host Controller supports Programmable Clock
  866. * Mode.
  867. */
  868. if (host->clk_mul) {
  869. u16 ctrl;
  870. /*
  871. * We need to figure out whether the Host Driver needs
  872. * to select Programmable Clock Mode, or the value can
  873. * be set automatically by the Host Controller based on
  874. * the Preset Value registers.
  875. */
  876. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  877. if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
  878. for (div = 1; div <= 1024; div++) {
  879. if (((host->max_clk * host->clk_mul) /
  880. div) <= clock)
  881. break;
  882. }
  883. /*
  884. * Set Programmable Clock Mode in the Clock
  885. * Control register.
  886. */
  887. clk = SDHCI_PROG_CLOCK_MODE;
  888. div--;
  889. }
  890. } else {
  891. /* Version 3.00 divisors must be a multiple of 2. */
  892. if (host->max_clk <= clock)
  893. div = 1;
  894. else {
  895. for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
  896. div += 2) {
  897. if ((host->max_clk / div) <= clock)
  898. break;
  899. }
  900. }
  901. div >>= 1;
  902. }
  903. } else {
  904. /* Version 2.00 divisors must be a power of 2. */
  905. for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
  906. if ((host->max_clk / div) <= clock)
  907. break;
  908. }
  909. div >>= 1;
  910. }
  911. clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
  912. clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
  913. << SDHCI_DIVIDER_HI_SHIFT;
  914. clk |= SDHCI_CLOCK_INT_EN;
  915. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  916. /* Wait max 20 ms */
  917. timeout = 20;
  918. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  919. & SDHCI_CLOCK_INT_STABLE)) {
  920. if (timeout == 0) {
  921. printk(KERN_ERR "%s: Internal clock never "
  922. "stabilised.\n", mmc_hostname(host->mmc));
  923. sdhci_dumpregs(host);
  924. return;
  925. }
  926. timeout--;
  927. mdelay(1);
  928. }
  929. clk |= SDHCI_CLOCK_CARD_EN;
  930. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  931. out:
  932. host->clock = clock;
  933. }
  934. static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
  935. {
  936. u8 pwr = 0;
  937. if (power != (unsigned short)-1) {
  938. switch (1 << power) {
  939. case MMC_VDD_165_195:
  940. pwr = SDHCI_POWER_180;
  941. break;
  942. case MMC_VDD_29_30:
  943. case MMC_VDD_30_31:
  944. pwr = SDHCI_POWER_300;
  945. break;
  946. case MMC_VDD_32_33:
  947. case MMC_VDD_33_34:
  948. pwr = SDHCI_POWER_330;
  949. break;
  950. default:
  951. BUG();
  952. }
  953. }
  954. if (host->pwr == pwr)
  955. return;
  956. host->pwr = pwr;
  957. if (pwr == 0) {
  958. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  959. return;
  960. }
  961. /*
  962. * Spec says that we should clear the power reg before setting
  963. * a new value. Some controllers don't seem to like this though.
  964. */
  965. if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
  966. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  967. /*
  968. * At least the Marvell CaFe chip gets confused if we set the voltage
  969. * and set turn on power at the same time, so set the voltage first.
  970. */
  971. if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
  972. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  973. pwr |= SDHCI_POWER_ON;
  974. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  975. /*
  976. * Some controllers need an extra 10ms delay of 10ms before they
  977. * can apply clock after applying power
  978. */
  979. if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
  980. mdelay(10);
  981. }
  982. /*****************************************************************************\
  983. * *
  984. * MMC callbacks *
  985. * *
  986. \*****************************************************************************/
  987. static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  988. {
  989. struct sdhci_host *host;
  990. bool present;
  991. unsigned long flags;
  992. host = mmc_priv(mmc);
  993. spin_lock_irqsave(&host->lock, flags);
  994. WARN_ON(host->mrq != NULL);
  995. #ifndef SDHCI_USE_LEDS_CLASS
  996. sdhci_activate_led(host);
  997. #endif
  998. /*
  999. * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
  1000. * requests if Auto-CMD12 is enabled.
  1001. */
  1002. if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
  1003. if (mrq->stop) {
  1004. mrq->data->stop = NULL;
  1005. mrq->stop = NULL;
  1006. }
  1007. }
  1008. host->mrq = mrq;
  1009. /* If polling, assume that the card is always present. */
  1010. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  1011. present = true;
  1012. else
  1013. present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  1014. SDHCI_CARD_PRESENT;
  1015. if (!present || host->flags & SDHCI_DEVICE_DEAD) {
  1016. host->mrq->cmd->error = -ENOMEDIUM;
  1017. tasklet_schedule(&host->finish_tasklet);
  1018. } else {
  1019. u32 present_state;
  1020. present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
  1021. /*
  1022. * Check if the re-tuning timer has already expired and there
  1023. * is no on-going data transfer. If so, we need to execute
  1024. * tuning procedure before sending command.
  1025. */
  1026. if ((host->flags & SDHCI_NEEDS_RETUNING) &&
  1027. !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
  1028. spin_unlock_irqrestore(&host->lock, flags);
  1029. sdhci_execute_tuning(mmc);
  1030. spin_lock_irqsave(&host->lock, flags);
  1031. /* Restore original mmc_request structure */
  1032. host->mrq = mrq;
  1033. }
  1034. if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
  1035. sdhci_send_command(host, mrq->sbc);
  1036. else
  1037. sdhci_send_command(host, mrq->cmd);
  1038. }
  1039. mmiowb();
  1040. spin_unlock_irqrestore(&host->lock, flags);
  1041. }
  1042. static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1043. {
  1044. struct sdhci_host *host;
  1045. unsigned long flags;
  1046. u8 ctrl;
  1047. host = mmc_priv(mmc);
  1048. spin_lock_irqsave(&host->lock, flags);
  1049. if (host->flags & SDHCI_DEVICE_DEAD)
  1050. goto out;
  1051. /*
  1052. * Reset the chip on each power off.
  1053. * Should clear out any weird states.
  1054. */
  1055. if (ios->power_mode == MMC_POWER_OFF) {
  1056. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  1057. sdhci_reinit(host);
  1058. }
  1059. sdhci_set_clock(host, ios->clock);
  1060. if (ios->power_mode == MMC_POWER_OFF)
  1061. sdhci_set_power(host, -1);
  1062. else
  1063. sdhci_set_power(host, ios->vdd);
  1064. if (host->ops->platform_send_init_74_clocks)
  1065. host->ops->platform_send_init_74_clocks(host, ios->power_mode);
  1066. /*
  1067. * If your platform has 8-bit width support but is not a v3 controller,
  1068. * or if it requires special setup code, you should implement that in
  1069. * platform_8bit_width().
  1070. */
  1071. if (host->ops->platform_8bit_width)
  1072. host->ops->platform_8bit_width(host, ios->bus_width);
  1073. else {
  1074. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  1075. if (ios->bus_width == MMC_BUS_WIDTH_8) {
  1076. ctrl &= ~SDHCI_CTRL_4BITBUS;
  1077. if (host->version >= SDHCI_SPEC_300)
  1078. ctrl |= SDHCI_CTRL_8BITBUS;
  1079. } else {
  1080. if (host->version >= SDHCI_SPEC_300)
  1081. ctrl &= ~SDHCI_CTRL_8BITBUS;
  1082. if (ios->bus_width == MMC_BUS_WIDTH_4)
  1083. ctrl |= SDHCI_CTRL_4BITBUS;
  1084. else
  1085. ctrl &= ~SDHCI_CTRL_4BITBUS;
  1086. }
  1087. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1088. }
  1089. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  1090. if ((ios->timing == MMC_TIMING_SD_HS ||
  1091. ios->timing == MMC_TIMING_MMC_HS)
  1092. && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
  1093. ctrl |= SDHCI_CTRL_HISPD;
  1094. else
  1095. ctrl &= ~SDHCI_CTRL_HISPD;
  1096. if (host->version >= SDHCI_SPEC_300) {
  1097. u16 clk, ctrl_2;
  1098. unsigned int clock;
  1099. /* In case of UHS-I modes, set High Speed Enable */
  1100. if ((ios->timing == MMC_TIMING_UHS_SDR50) ||
  1101. (ios->timing == MMC_TIMING_UHS_SDR104) ||
  1102. (ios->timing == MMC_TIMING_UHS_DDR50) ||
  1103. (ios->timing == MMC_TIMING_UHS_SDR25) ||
  1104. (ios->timing == MMC_TIMING_UHS_SDR12))
  1105. ctrl |= SDHCI_CTRL_HISPD;
  1106. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1107. if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
  1108. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1109. /*
  1110. * We only need to set Driver Strength if the
  1111. * preset value enable is not set.
  1112. */
  1113. ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
  1114. if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
  1115. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
  1116. else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
  1117. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
  1118. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  1119. } else {
  1120. /*
  1121. * According to SDHC Spec v3.00, if the Preset Value
  1122. * Enable in the Host Control 2 register is set, we
  1123. * need to reset SD Clock Enable before changing High
  1124. * Speed Enable to avoid generating clock gliches.
  1125. */
  1126. /* Reset SD Clock Enable */
  1127. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1128. clk &= ~SDHCI_CLOCK_CARD_EN;
  1129. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1130. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1131. /* Re-enable SD Clock */
  1132. clock = host->clock;
  1133. host->clock = 0;
  1134. sdhci_set_clock(host, clock);
  1135. }
  1136. /* Reset SD Clock Enable */
  1137. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1138. clk &= ~SDHCI_CLOCK_CARD_EN;
  1139. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1140. if (host->ops->set_uhs_signaling)
  1141. host->ops->set_uhs_signaling(host, ios->timing);
  1142. else {
  1143. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1144. /* Select Bus Speed Mode for host */
  1145. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  1146. if (ios->timing == MMC_TIMING_UHS_SDR12)
  1147. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  1148. else if (ios->timing == MMC_TIMING_UHS_SDR25)
  1149. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  1150. else if (ios->timing == MMC_TIMING_UHS_SDR50)
  1151. ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
  1152. else if (ios->timing == MMC_TIMING_UHS_SDR104)
  1153. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  1154. else if (ios->timing == MMC_TIMING_UHS_DDR50)
  1155. ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
  1156. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  1157. }
  1158. /* Re-enable SD Clock */
  1159. clock = host->clock;
  1160. host->clock = 0;
  1161. sdhci_set_clock(host, clock);
  1162. } else
  1163. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1164. /*
  1165. * Some (ENE) controllers go apeshit on some ios operation,
  1166. * signalling timeout and CRC errors even on CMD0. Resetting
  1167. * it on each ios seems to solve the problem.
  1168. */
  1169. if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
  1170. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  1171. out:
  1172. mmiowb();
  1173. spin_unlock_irqrestore(&host->lock, flags);
  1174. }
  1175. static int check_ro(struct sdhci_host *host)
  1176. {
  1177. unsigned long flags;
  1178. int is_readonly;
  1179. spin_lock_irqsave(&host->lock, flags);
  1180. if (host->flags & SDHCI_DEVICE_DEAD)
  1181. is_readonly = 0;
  1182. else if (host->ops->get_ro)
  1183. is_readonly = host->ops->get_ro(host);
  1184. else
  1185. is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
  1186. & SDHCI_WRITE_PROTECT);
  1187. spin_unlock_irqrestore(&host->lock, flags);
  1188. /* This quirk needs to be replaced by a callback-function later */
  1189. return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
  1190. !is_readonly : is_readonly;
  1191. }
  1192. #define SAMPLE_COUNT 5
  1193. static int sdhci_get_ro(struct mmc_host *mmc)
  1194. {
  1195. struct sdhci_host *host;
  1196. int i, ro_count;
  1197. host = mmc_priv(mmc);
  1198. if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
  1199. return check_ro(host);
  1200. ro_count = 0;
  1201. for (i = 0; i < SAMPLE_COUNT; i++) {
  1202. if (check_ro(host)) {
  1203. if (++ro_count > SAMPLE_COUNT / 2)
  1204. return 1;
  1205. }
  1206. msleep(30);
  1207. }
  1208. return 0;
  1209. }
  1210. static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  1211. {
  1212. struct sdhci_host *host;
  1213. unsigned long flags;
  1214. host = mmc_priv(mmc);
  1215. spin_lock_irqsave(&host->lock, flags);
  1216. if (host->flags & SDHCI_DEVICE_DEAD)
  1217. goto out;
  1218. if (enable)
  1219. sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
  1220. else
  1221. sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
  1222. out:
  1223. mmiowb();
  1224. spin_unlock_irqrestore(&host->lock, flags);
  1225. }
  1226. static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
  1227. struct mmc_ios *ios)
  1228. {
  1229. struct sdhci_host *host;
  1230. u8 pwr;
  1231. u16 clk, ctrl;
  1232. u32 present_state;
  1233. host = mmc_priv(mmc);
  1234. /*
  1235. * Signal Voltage Switching is only applicable for Host Controllers
  1236. * v3.00 and above.
  1237. */
  1238. if (host->version < SDHCI_SPEC_300)
  1239. return 0;
  1240. /*
  1241. * We first check whether the request is to set signalling voltage
  1242. * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
  1243. */
  1244. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1245. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
  1246. /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
  1247. ctrl &= ~SDHCI_CTRL_VDD_180;
  1248. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1249. /* Wait for 5ms */
  1250. usleep_range(5000, 5500);
  1251. /* 3.3V regulator output should be stable within 5 ms */
  1252. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1253. if (!(ctrl & SDHCI_CTRL_VDD_180))
  1254. return 0;
  1255. else {
  1256. printk(KERN_INFO DRIVER_NAME ": Switching to 3.3V "
  1257. "signalling voltage failed\n");
  1258. return -EIO;
  1259. }
  1260. } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
  1261. (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
  1262. /* Stop SDCLK */
  1263. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1264. clk &= ~SDHCI_CLOCK_CARD_EN;
  1265. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1266. /* Check whether DAT[3:0] is 0000 */
  1267. present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
  1268. if (!((present_state & SDHCI_DATA_LVL_MASK) >>
  1269. SDHCI_DATA_LVL_SHIFT)) {
  1270. /*
  1271. * Enable 1.8V Signal Enable in the Host Control2
  1272. * register
  1273. */
  1274. ctrl |= SDHCI_CTRL_VDD_180;
  1275. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1276. /* Wait for 5ms */
  1277. usleep_range(5000, 5500);
  1278. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1279. if (ctrl & SDHCI_CTRL_VDD_180) {
  1280. /* Provide SDCLK again and wait for 1ms*/
  1281. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1282. clk |= SDHCI_CLOCK_CARD_EN;
  1283. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1284. usleep_range(1000, 1500);
  1285. /*
  1286. * If DAT[3:0] level is 1111b, then the card
  1287. * was successfully switched to 1.8V signaling.
  1288. */
  1289. present_state = sdhci_readl(host,
  1290. SDHCI_PRESENT_STATE);
  1291. if ((present_state & SDHCI_DATA_LVL_MASK) ==
  1292. SDHCI_DATA_LVL_MASK)
  1293. return 0;
  1294. }
  1295. }
  1296. /*
  1297. * If we are here, that means the switch to 1.8V signaling
  1298. * failed. We power cycle the card, and retry initialization
  1299. * sequence by setting S18R to 0.
  1300. */
  1301. pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
  1302. pwr &= ~SDHCI_POWER_ON;
  1303. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  1304. /* Wait for 1ms as per the spec */
  1305. usleep_range(1000, 1500);
  1306. pwr |= SDHCI_POWER_ON;
  1307. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  1308. printk(KERN_INFO DRIVER_NAME ": Switching to 1.8V signalling "
  1309. "voltage failed, retrying with S18R set to 0\n");
  1310. return -EAGAIN;
  1311. } else
  1312. /* No signal voltage switch required */
  1313. return 0;
  1314. }
  1315. static int sdhci_execute_tuning(struct mmc_host *mmc)
  1316. {
  1317. struct sdhci_host *host;
  1318. u16 ctrl;
  1319. u32 ier;
  1320. int tuning_loop_counter = MAX_TUNING_LOOP;
  1321. unsigned long timeout;
  1322. int err = 0;
  1323. host = mmc_priv(mmc);
  1324. disable_irq(host->irq);
  1325. spin_lock(&host->lock);
  1326. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1327. /*
  1328. * Host Controller needs tuning only in case of SDR104 mode
  1329. * and for SDR50 mode when Use Tuning for SDR50 is set in
  1330. * Capabilities register.
  1331. */
  1332. if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
  1333. (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
  1334. (host->flags & SDHCI_SDR50_NEEDS_TUNING)))
  1335. ctrl |= SDHCI_CTRL_EXEC_TUNING;
  1336. else {
  1337. spin_unlock(&host->lock);
  1338. enable_irq(host->irq);
  1339. return 0;
  1340. }
  1341. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1342. /*
  1343. * As per the Host Controller spec v3.00, tuning command
  1344. * generates Buffer Read Ready interrupt, so enable that.
  1345. *
  1346. * Note: The spec clearly says that when tuning sequence
  1347. * is being performed, the controller does not generate
  1348. * interrupts other than Buffer Read Ready interrupt. But
  1349. * to make sure we don't hit a controller bug, we _only_
  1350. * enable Buffer Read Ready interrupt here.
  1351. */
  1352. ier = sdhci_readl(host, SDHCI_INT_ENABLE);
  1353. sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
  1354. /*
  1355. * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
  1356. * of loops reaches 40 times or a timeout of 150ms occurs.
  1357. */
  1358. timeout = 150;
  1359. do {
  1360. struct mmc_command cmd = {0};
  1361. struct mmc_request mrq = {0};
  1362. if (!tuning_loop_counter && !timeout)
  1363. break;
  1364. cmd.opcode = MMC_SEND_TUNING_BLOCK;
  1365. cmd.arg = 0;
  1366. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  1367. cmd.retries = 0;
  1368. cmd.data = NULL;
  1369. cmd.error = 0;
  1370. mrq.cmd = &cmd;
  1371. host->mrq = &mrq;
  1372. /*
  1373. * In response to CMD19, the card sends 64 bytes of tuning
  1374. * block to the Host Controller. So we set the block size
  1375. * to 64 here.
  1376. */
  1377. sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
  1378. /*
  1379. * The tuning block is sent by the card to the host controller.
  1380. * So we set the TRNS_READ bit in the Transfer Mode register.
  1381. * This also takes care of setting DMA Enable and Multi Block
  1382. * Select in the same register to 0.
  1383. */
  1384. sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
  1385. sdhci_send_command(host, &cmd);
  1386. host->cmd = NULL;
  1387. host->mrq = NULL;
  1388. spin_unlock(&host->lock);
  1389. enable_irq(host->irq);
  1390. /* Wait for Buffer Read Ready interrupt */
  1391. wait_event_interruptible_timeout(host->buf_ready_int,
  1392. (host->tuning_done == 1),
  1393. msecs_to_jiffies(50));
  1394. disable_irq(host->irq);
  1395. spin_lock(&host->lock);
  1396. if (!host->tuning_done) {
  1397. printk(KERN_INFO DRIVER_NAME ": Timeout waiting for "
  1398. "Buffer Read Ready interrupt during tuning "
  1399. "procedure, falling back to fixed sampling "
  1400. "clock\n");
  1401. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1402. ctrl &= ~SDHCI_CTRL_TUNED_CLK;
  1403. ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
  1404. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1405. err = -EIO;
  1406. goto out;
  1407. }
  1408. host->tuning_done = 0;
  1409. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1410. tuning_loop_counter--;
  1411. timeout--;
  1412. mdelay(1);
  1413. } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
  1414. /*
  1415. * The Host Driver has exhausted the maximum number of loops allowed,
  1416. * so use fixed sampling frequency.
  1417. */
  1418. if (!tuning_loop_counter || !timeout) {
  1419. ctrl &= ~SDHCI_CTRL_TUNED_CLK;
  1420. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1421. } else {
  1422. if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
  1423. printk(KERN_INFO DRIVER_NAME ": Tuning procedure"
  1424. " failed, falling back to fixed sampling"
  1425. " clock\n");
  1426. err = -EIO;
  1427. }
  1428. }
  1429. out:
  1430. /*
  1431. * If this is the very first time we are here, we start the retuning
  1432. * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
  1433. * flag won't be set, we check this condition before actually starting
  1434. * the timer.
  1435. */
  1436. if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
  1437. (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
  1438. mod_timer(&host->tuning_timer, jiffies +
  1439. host->tuning_count * HZ);
  1440. /* Tuning mode 1 limits the maximum data length to 4MB */
  1441. mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
  1442. } else {
  1443. host->flags &= ~SDHCI_NEEDS_RETUNING;
  1444. /* Reload the new initial value for timer */
  1445. if (host->tuning_mode == SDHCI_TUNING_MODE_1)
  1446. mod_timer(&host->tuning_timer, jiffies +
  1447. host->tuning_count * HZ);
  1448. }
  1449. /*
  1450. * In case tuning fails, host controllers which support re-tuning can
  1451. * try tuning again at a later time, when the re-tuning timer expires.
  1452. * So for these controllers, we return 0. Since there might be other
  1453. * controllers who do not have this capability, we return error for
  1454. * them.
  1455. */
  1456. if (err && host->tuning_count &&
  1457. host->tuning_mode == SDHCI_TUNING_MODE_1)
  1458. err = 0;
  1459. sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
  1460. spin_unlock(&host->lock);
  1461. enable_irq(host->irq);
  1462. return err;
  1463. }
  1464. static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
  1465. {
  1466. struct sdhci_host *host;
  1467. u16 ctrl;
  1468. unsigned long flags;
  1469. host = mmc_priv(mmc);
  1470. /* Host Controller v3.00 defines preset value registers */
  1471. if (host->version < SDHCI_SPEC_300)
  1472. return;
  1473. spin_lock_irqsave(&host->lock, flags);
  1474. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1475. /*
  1476. * We only enable or disable Preset Value if they are not already
  1477. * enabled or disabled respectively. Otherwise, we bail out.
  1478. */
  1479. if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
  1480. ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
  1481. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1482. } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
  1483. ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
  1484. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1485. }
  1486. spin_unlock_irqrestore(&host->lock, flags);
  1487. }
  1488. static const struct mmc_host_ops sdhci_ops = {
  1489. .request = sdhci_request,
  1490. .set_ios = sdhci_set_ios,
  1491. .get_ro = sdhci_get_ro,
  1492. .enable_sdio_irq = sdhci_enable_sdio_irq,
  1493. .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
  1494. .execute_tuning = sdhci_execute_tuning,
  1495. .enable_preset_value = sdhci_enable_preset_value,
  1496. };
  1497. /*****************************************************************************\
  1498. * *
  1499. * Tasklets *
  1500. * *
  1501. \*****************************************************************************/
  1502. static void sdhci_tasklet_card(unsigned long param)
  1503. {
  1504. struct sdhci_host *host;
  1505. unsigned long flags;
  1506. host = (struct sdhci_host*)param;
  1507. spin_lock_irqsave(&host->lock, flags);
  1508. if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
  1509. if (host->mrq) {
  1510. printk(KERN_ERR "%s: Card removed during transfer!\n",
  1511. mmc_hostname(host->mmc));
  1512. printk(KERN_ERR "%s: Resetting controller.\n",
  1513. mmc_hostname(host->mmc));
  1514. sdhci_reset(host, SDHCI_RESET_CMD);
  1515. sdhci_reset(host, SDHCI_RESET_DATA);
  1516. host->mrq->cmd->error = -ENOMEDIUM;
  1517. tasklet_schedule(&host->finish_tasklet);
  1518. }
  1519. }
  1520. spin_unlock_irqrestore(&host->lock, flags);
  1521. mmc_detect_change(host->mmc, msecs_to_jiffies(200));
  1522. }
  1523. static void sdhci_tasklet_finish(unsigned long param)
  1524. {
  1525. struct sdhci_host *host;
  1526. unsigned long flags;
  1527. struct mmc_request *mrq;
  1528. host = (struct sdhci_host*)param;
  1529. /*
  1530. * If this tasklet gets rescheduled while running, it will
  1531. * be run again afterwards but without any active request.
  1532. */
  1533. if (!host->mrq)
  1534. return;
  1535. spin_lock_irqsave(&host->lock, flags);
  1536. del_timer(&host->timer);
  1537. if (host->version >= SDHCI_SPEC_300)
  1538. del_timer(&host->tuning_timer);
  1539. mrq = host->mrq;
  1540. /*
  1541. * The controller needs a reset of internal state machines
  1542. * upon error conditions.
  1543. */
  1544. if (!(host->flags & SDHCI_DEVICE_DEAD) &&
  1545. ((mrq->cmd && mrq->cmd->error) ||
  1546. (mrq->data && (mrq->data->error ||
  1547. (mrq->data->stop && mrq->data->stop->error))) ||
  1548. (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
  1549. /* Some controllers need this kick or reset won't work here */
  1550. if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
  1551. unsigned int clock;
  1552. /* This is to force an update */
  1553. clock = host->clock;
  1554. host->clock = 0;
  1555. sdhci_set_clock(host, clock);
  1556. }
  1557. /* Spec says we should do both at the same time, but Ricoh
  1558. controllers do not like that. */
  1559. sdhci_reset(host, SDHCI_RESET_CMD);
  1560. sdhci_reset(host, SDHCI_RESET_DATA);
  1561. }
  1562. host->mrq = NULL;
  1563. host->cmd = NULL;
  1564. host->data = NULL;
  1565. #ifndef SDHCI_USE_LEDS_CLASS
  1566. sdhci_deactivate_led(host);
  1567. #endif
  1568. mmiowb();
  1569. spin_unlock_irqrestore(&host->lock, flags);
  1570. mmc_request_done(host->mmc, mrq);
  1571. }
  1572. static void sdhci_timeout_timer(unsigned long data)
  1573. {
  1574. struct sdhci_host *host;
  1575. unsigned long flags;
  1576. host = (struct sdhci_host*)data;
  1577. spin_lock_irqsave(&host->lock, flags);
  1578. if (host->mrq) {
  1579. printk(KERN_ERR "%s: Timeout waiting for hardware "
  1580. "interrupt.\n", mmc_hostname(host->mmc));
  1581. sdhci_dumpregs(host);
  1582. if (host->data) {
  1583. host->data->error = -ETIMEDOUT;
  1584. sdhci_finish_data(host);
  1585. } else {
  1586. if (host->cmd)
  1587. host->cmd->error = -ETIMEDOUT;
  1588. else
  1589. host->mrq->cmd->error = -ETIMEDOUT;
  1590. tasklet_schedule(&host->finish_tasklet);
  1591. }
  1592. }
  1593. mmiowb();
  1594. spin_unlock_irqrestore(&host->lock, flags);
  1595. }
  1596. static void sdhci_tuning_timer(unsigned long data)
  1597. {
  1598. struct sdhci_host *host;
  1599. unsigned long flags;
  1600. host = (struct sdhci_host *)data;
  1601. spin_lock_irqsave(&host->lock, flags);
  1602. host->flags |= SDHCI_NEEDS_RETUNING;
  1603. spin_unlock_irqrestore(&host->lock, flags);
  1604. }
  1605. /*****************************************************************************\
  1606. * *
  1607. * Interrupt handling *
  1608. * *
  1609. \*****************************************************************************/
  1610. static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
  1611. {
  1612. BUG_ON(intmask == 0);
  1613. if (!host->cmd) {
  1614. printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
  1615. "though no command operation was in progress.\n",
  1616. mmc_hostname(host->mmc), (unsigned)intmask);
  1617. sdhci_dumpregs(host);
  1618. return;
  1619. }
  1620. if (intmask & SDHCI_INT_TIMEOUT)
  1621. host->cmd->error = -ETIMEDOUT;
  1622. else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
  1623. SDHCI_INT_INDEX))
  1624. host->cmd->error = -EILSEQ;
  1625. if (host->cmd->error) {
  1626. tasklet_schedule(&host->finish_tasklet);
  1627. return;
  1628. }
  1629. /*
  1630. * The host can send and interrupt when the busy state has
  1631. * ended, allowing us to wait without wasting CPU cycles.
  1632. * Unfortunately this is overloaded on the "data complete"
  1633. * interrupt, so we need to take some care when handling
  1634. * it.
  1635. *
  1636. * Note: The 1.0 specification is a bit ambiguous about this
  1637. * feature so there might be some problems with older
  1638. * controllers.
  1639. */
  1640. if (host->cmd->flags & MMC_RSP_BUSY) {
  1641. if (host->cmd->data)
  1642. DBG("Cannot wait for busy signal when also "
  1643. "doing a data transfer");
  1644. else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
  1645. return;
  1646. /* The controller does not support the end-of-busy IRQ,
  1647. * fall through and take the SDHCI_INT_RESPONSE */
  1648. }
  1649. if (intmask & SDHCI_INT_RESPONSE)
  1650. sdhci_finish_command(host);
  1651. }
  1652. #ifdef CONFIG_MMC_DEBUG
  1653. static void sdhci_show_adma_error(struct sdhci_host *host)
  1654. {
  1655. const char *name = mmc_hostname(host->mmc);
  1656. u8 *desc = host->adma_desc;
  1657. __le32 *dma;
  1658. __le16 *len;
  1659. u8 attr;
  1660. sdhci_dumpregs(host);
  1661. while (true) {
  1662. dma = (__le32 *)(desc + 4);
  1663. len = (__le16 *)(desc + 2);
  1664. attr = *desc;
  1665. DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
  1666. name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
  1667. desc += 8;
  1668. if (attr & 2)
  1669. break;
  1670. }
  1671. }
  1672. #else
  1673. static void sdhci_show_adma_error(struct sdhci_host *host) { }
  1674. #endif
  1675. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  1676. {
  1677. BUG_ON(intmask == 0);
  1678. /* CMD19 generates _only_ Buffer Read Ready interrupt */
  1679. if (intmask & SDHCI_INT_DATA_AVAIL) {
  1680. if (SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) ==
  1681. MMC_SEND_TUNING_BLOCK) {
  1682. host->tuning_done = 1;
  1683. wake_up(&host->buf_ready_int);
  1684. return;
  1685. }
  1686. }
  1687. if (!host->data) {
  1688. /*
  1689. * The "data complete" interrupt is also used to
  1690. * indicate that a busy state has ended. See comment
  1691. * above in sdhci_cmd_irq().
  1692. */
  1693. if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
  1694. if (intmask & SDHCI_INT_DATA_END) {
  1695. sdhci_finish_command(host);
  1696. return;
  1697. }
  1698. }
  1699. printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
  1700. "though no data operation was in progress.\n",
  1701. mmc_hostname(host->mmc), (unsigned)intmask);
  1702. sdhci_dumpregs(host);
  1703. return;
  1704. }
  1705. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  1706. host->data->error = -ETIMEDOUT;
  1707. else if (intmask & SDHCI_INT_DATA_END_BIT)
  1708. host->data->error = -EILSEQ;
  1709. else if ((intmask & SDHCI_INT_DATA_CRC) &&
  1710. SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
  1711. != MMC_BUS_TEST_R)
  1712. host->data->error = -EILSEQ;
  1713. else if (intmask & SDHCI_INT_ADMA_ERROR) {
  1714. printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
  1715. sdhci_show_adma_error(host);
  1716. host->data->error = -EIO;
  1717. }
  1718. if (host->data->error)
  1719. sdhci_finish_data(host);
  1720. else {
  1721. if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  1722. sdhci_transfer_pio(host);
  1723. /*
  1724. * We currently don't do anything fancy with DMA
  1725. * boundaries, but as we can't disable the feature
  1726. * we need to at least restart the transfer.
  1727. *
  1728. * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
  1729. * should return a valid address to continue from, but as
  1730. * some controllers are faulty, don't trust them.
  1731. */
  1732. if (intmask & SDHCI_INT_DMA_END) {
  1733. u32 dmastart, dmanow;
  1734. dmastart = sg_dma_address(host->data->sg);
  1735. dmanow = dmastart + host->data->bytes_xfered;
  1736. /*
  1737. * Force update to the next DMA block boundary.
  1738. */
  1739. dmanow = (dmanow &
  1740. ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
  1741. SDHCI_DEFAULT_BOUNDARY_SIZE;
  1742. host->data->bytes_xfered = dmanow - dmastart;
  1743. DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
  1744. " next 0x%08x\n",
  1745. mmc_hostname(host->mmc), dmastart,
  1746. host->data->bytes_xfered, dmanow);
  1747. sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
  1748. }
  1749. if (intmask & SDHCI_INT_DATA_END) {
  1750. if (host->cmd) {
  1751. /*
  1752. * Data managed to finish before the
  1753. * command completed. Make sure we do
  1754. * things in the proper order.
  1755. */
  1756. host->data_early = 1;
  1757. } else {
  1758. sdhci_finish_data(host);
  1759. }
  1760. }
  1761. }
  1762. }
  1763. static irqreturn_t sdhci_irq(int irq, void *dev_id)
  1764. {
  1765. irqreturn_t result;
  1766. struct sdhci_host* host = dev_id;
  1767. u32 intmask;
  1768. int cardint = 0;
  1769. spin_lock(&host->lock);
  1770. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  1771. if (!intmask || intmask == 0xffffffff) {
  1772. result = IRQ_NONE;
  1773. goto out;
  1774. }
  1775. DBG("*** %s got interrupt: 0x%08x\n",
  1776. mmc_hostname(host->mmc), intmask);
  1777. if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  1778. u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  1779. SDHCI_CARD_PRESENT;
  1780. /*
  1781. * There is a observation on i.mx esdhc. INSERT bit will be
  1782. * immediately set again when it gets cleared, if a card is
  1783. * inserted. We have to mask the irq to prevent interrupt
  1784. * storm which will freeze the system. And the REMOVE gets
  1785. * the same situation.
  1786. *
  1787. * More testing are needed here to ensure it works for other
  1788. * platforms though.
  1789. */
  1790. sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
  1791. SDHCI_INT_CARD_REMOVE);
  1792. sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
  1793. SDHCI_INT_CARD_INSERT);
  1794. sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
  1795. SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
  1796. intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
  1797. tasklet_schedule(&host->card_tasklet);
  1798. }
  1799. if (intmask & SDHCI_INT_CMD_MASK) {
  1800. sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
  1801. SDHCI_INT_STATUS);
  1802. sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  1803. }
  1804. if (intmask & SDHCI_INT_DATA_MASK) {
  1805. sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
  1806. SDHCI_INT_STATUS);
  1807. sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  1808. }
  1809. intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
  1810. intmask &= ~SDHCI_INT_ERROR;
  1811. if (intmask & SDHCI_INT_BUS_POWER) {
  1812. printk(KERN_ERR "%s: Card is consuming too much power!\n",
  1813. mmc_hostname(host->mmc));
  1814. sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
  1815. }
  1816. intmask &= ~SDHCI_INT_BUS_POWER;
  1817. if (intmask & SDHCI_INT_CARD_INT)
  1818. cardint = 1;
  1819. intmask &= ~SDHCI_INT_CARD_INT;
  1820. if (intmask) {
  1821. printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
  1822. mmc_hostname(host->mmc), intmask);
  1823. sdhci_dumpregs(host);
  1824. sdhci_writel(host, intmask, SDHCI_INT_STATUS);
  1825. }
  1826. result = IRQ_HANDLED;
  1827. mmiowb();
  1828. out:
  1829. spin_unlock(&host->lock);
  1830. /*
  1831. * We have to delay this as it calls back into the driver.
  1832. */
  1833. if (cardint)
  1834. mmc_signal_sdio_irq(host->mmc);
  1835. return result;
  1836. }
  1837. /*****************************************************************************\
  1838. * *
  1839. * Suspend/resume *
  1840. * *
  1841. \*****************************************************************************/
  1842. #ifdef CONFIG_PM
  1843. int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
  1844. {
  1845. int ret;
  1846. sdhci_disable_card_detection(host);
  1847. /* Disable tuning since we are suspending */
  1848. if (host->version >= SDHCI_SPEC_300 && host->tuning_count &&
  1849. host->tuning_mode == SDHCI_TUNING_MODE_1) {
  1850. host->flags &= ~SDHCI_NEEDS_RETUNING;
  1851. mod_timer(&host->tuning_timer, jiffies +
  1852. host->tuning_count * HZ);
  1853. }
  1854. ret = mmc_suspend_host(host->mmc);
  1855. if (ret)
  1856. return ret;
  1857. free_irq(host->irq, host);
  1858. if (host->vmmc)
  1859. ret = regulator_disable(host->vmmc);
  1860. return ret;
  1861. }
  1862. EXPORT_SYMBOL_GPL(sdhci_suspend_host);
  1863. int sdhci_resume_host(struct sdhci_host *host)
  1864. {
  1865. int ret;
  1866. if (host->vmmc) {
  1867. int ret = regulator_enable(host->vmmc);
  1868. if (ret)
  1869. return ret;
  1870. }
  1871. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  1872. if (host->ops->enable_dma)
  1873. host->ops->enable_dma(host);
  1874. }
  1875. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  1876. mmc_hostname(host->mmc), host);
  1877. if (ret)
  1878. return ret;
  1879. sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
  1880. mmiowb();
  1881. ret = mmc_resume_host(host->mmc);
  1882. sdhci_enable_card_detection(host);
  1883. /* Set the re-tuning expiration flag */
  1884. if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
  1885. (host->tuning_mode == SDHCI_TUNING_MODE_1))
  1886. host->flags |= SDHCI_NEEDS_RETUNING;
  1887. return ret;
  1888. }
  1889. EXPORT_SYMBOL_GPL(sdhci_resume_host);
  1890. void sdhci_enable_irq_wakeups(struct sdhci_host *host)
  1891. {
  1892. u8 val;
  1893. val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
  1894. val |= SDHCI_WAKE_ON_INT;
  1895. sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
  1896. }
  1897. EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
  1898. #endif /* CONFIG_PM */
  1899. /*****************************************************************************\
  1900. * *
  1901. * Device allocation/registration *
  1902. * *
  1903. \*****************************************************************************/
  1904. struct sdhci_host *sdhci_alloc_host(struct device *dev,
  1905. size_t priv_size)
  1906. {
  1907. struct mmc_host *mmc;
  1908. struct sdhci_host *host;
  1909. WARN_ON(dev == NULL);
  1910. mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
  1911. if (!mmc)
  1912. return ERR_PTR(-ENOMEM);
  1913. host = mmc_priv(mmc);
  1914. host->mmc = mmc;
  1915. return host;
  1916. }
  1917. EXPORT_SYMBOL_GPL(sdhci_alloc_host);
  1918. int sdhci_add_host(struct sdhci_host *host)
  1919. {
  1920. struct mmc_host *mmc;
  1921. u32 caps[2];
  1922. u32 max_current_caps;
  1923. unsigned int ocr_avail;
  1924. int ret;
  1925. WARN_ON(host == NULL);
  1926. if (host == NULL)
  1927. return -EINVAL;
  1928. mmc = host->mmc;
  1929. if (debug_quirks)
  1930. host->quirks = debug_quirks;
  1931. sdhci_reset(host, SDHCI_RESET_ALL);
  1932. host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
  1933. host->version = (host->version & SDHCI_SPEC_VER_MASK)
  1934. >> SDHCI_SPEC_VER_SHIFT;
  1935. if (host->version > SDHCI_SPEC_300) {
  1936. printk(KERN_ERR "%s: Unknown controller version (%d). "
  1937. "You may experience problems.\n", mmc_hostname(mmc),
  1938. host->version);
  1939. }
  1940. caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
  1941. sdhci_readl(host, SDHCI_CAPABILITIES);
  1942. caps[1] = (host->version >= SDHCI_SPEC_300) ?
  1943. sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
  1944. if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
  1945. host->flags |= SDHCI_USE_SDMA;
  1946. else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
  1947. DBG("Controller doesn't have SDMA capability\n");
  1948. else
  1949. host->flags |= SDHCI_USE_SDMA;
  1950. if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
  1951. (host->flags & SDHCI_USE_SDMA)) {
  1952. DBG("Disabling DMA as it is marked broken\n");
  1953. host->flags &= ~SDHCI_USE_SDMA;
  1954. }
  1955. if ((host->version >= SDHCI_SPEC_200) &&
  1956. (caps[0] & SDHCI_CAN_DO_ADMA2))
  1957. host->flags |= SDHCI_USE_ADMA;
  1958. if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
  1959. (host->flags & SDHCI_USE_ADMA)) {
  1960. DBG("Disabling ADMA as it is marked broken\n");
  1961. host->flags &= ~SDHCI_USE_ADMA;
  1962. }
  1963. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  1964. if (host->ops->enable_dma) {
  1965. if (host->ops->enable_dma(host)) {
  1966. printk(KERN_WARNING "%s: No suitable DMA "
  1967. "available. Falling back to PIO.\n",
  1968. mmc_hostname(mmc));
  1969. host->flags &=
  1970. ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
  1971. }
  1972. }
  1973. }
  1974. if (host->flags & SDHCI_USE_ADMA) {
  1975. /*
  1976. * We need to allocate descriptors for all sg entries
  1977. * (128) and potentially one alignment transfer for
  1978. * each of those entries.
  1979. */
  1980. host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
  1981. host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
  1982. if (!host->adma_desc || !host->align_buffer) {
  1983. kfree(host->adma_desc);
  1984. kfree(host->align_buffer);
  1985. printk(KERN_WARNING "%s: Unable to allocate ADMA "
  1986. "buffers. Falling back to standard DMA.\n",
  1987. mmc_hostname(mmc));
  1988. host->flags &= ~SDHCI_USE_ADMA;
  1989. }
  1990. }
  1991. /*
  1992. * If we use DMA, then it's up to the caller to set the DMA
  1993. * mask, but PIO does not need the hw shim so we set a new
  1994. * mask here in that case.
  1995. */
  1996. if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
  1997. host->dma_mask = DMA_BIT_MASK(64);
  1998. mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
  1999. }
  2000. if (host->version >= SDHCI_SPEC_300)
  2001. host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
  2002. >> SDHCI_CLOCK_BASE_SHIFT;
  2003. else
  2004. host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
  2005. >> SDHCI_CLOCK_BASE_SHIFT;
  2006. host->max_clk *= 1000000;
  2007. if (host->max_clk == 0 || host->quirks &
  2008. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
  2009. if (!host->ops->get_max_clock) {
  2010. printk(KERN_ERR
  2011. "%s: Hardware doesn't specify base clock "
  2012. "frequency.\n", mmc_hostname(mmc));
  2013. return -ENODEV;
  2014. }
  2015. host->max_clk = host->ops->get_max_clock(host);
  2016. }
  2017. host->timeout_clk =
  2018. (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
  2019. if (host->timeout_clk == 0) {
  2020. if (host->ops->get_timeout_clock) {
  2021. host->timeout_clk = host->ops->get_timeout_clock(host);
  2022. } else if (!(host->quirks &
  2023. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
  2024. printk(KERN_ERR
  2025. "%s: Hardware doesn't specify timeout clock "
  2026. "frequency.\n", mmc_hostname(mmc));
  2027. return -ENODEV;
  2028. }
  2029. }
  2030. if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
  2031. host->timeout_clk *= 1000;
  2032. /*
  2033. * In case of Host Controller v3.00, find out whether clock
  2034. * multiplier is supported.
  2035. */
  2036. host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
  2037. SDHCI_CLOCK_MUL_SHIFT;
  2038. /*
  2039. * In case the value in Clock Multiplier is 0, then programmable
  2040. * clock mode is not supported, otherwise the actual clock
  2041. * multiplier is one more than the value of Clock Multiplier
  2042. * in the Capabilities Register.
  2043. */
  2044. if (host->clk_mul)
  2045. host->clk_mul += 1;
  2046. /*
  2047. * Set host parameters.
  2048. */
  2049. mmc->ops = &sdhci_ops;
  2050. mmc->f_max = host->max_clk;
  2051. if (host->ops->get_min_clock)
  2052. mmc->f_min = host->ops->get_min_clock(host);
  2053. else if (host->version >= SDHCI_SPEC_300) {
  2054. if (host->clk_mul) {
  2055. mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
  2056. mmc->f_max = host->max_clk * host->clk_mul;
  2057. } else
  2058. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
  2059. } else
  2060. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
  2061. if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
  2062. mmc->max_discard_to = (1 << 27) / (mmc->f_max / 1000);
  2063. else
  2064. mmc->max_discard_to = (1 << 27) / host->timeout_clk;
  2065. mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
  2066. if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
  2067. host->flags |= SDHCI_AUTO_CMD12;
  2068. /* Auto-CMD23 stuff only works in ADMA or PIO. */
  2069. if ((host->version >= SDHCI_SPEC_300) &&
  2070. ((host->flags & SDHCI_USE_ADMA) ||
  2071. !(host->flags & SDHCI_USE_SDMA))) {
  2072. host->flags |= SDHCI_AUTO_CMD23;
  2073. DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
  2074. } else {
  2075. DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
  2076. }
  2077. /*
  2078. * A controller may support 8-bit width, but the board itself
  2079. * might not have the pins brought out. Boards that support
  2080. * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
  2081. * their platform code before calling sdhci_add_host(), and we
  2082. * won't assume 8-bit width for hosts without that CAP.
  2083. */
  2084. if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
  2085. mmc->caps |= MMC_CAP_4_BIT_DATA;
  2086. if (caps[0] & SDHCI_CAN_DO_HISPD)
  2087. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  2088. if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
  2089. mmc_card_is_removable(mmc))
  2090. mmc->caps |= MMC_CAP_NEEDS_POLL;
  2091. /* UHS-I mode(s) supported by the host controller. */
  2092. if (host->version >= SDHCI_SPEC_300)
  2093. mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
  2094. /* SDR104 supports also implies SDR50 support */
  2095. if (caps[1] & SDHCI_SUPPORT_SDR104)
  2096. mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
  2097. else if (caps[1] & SDHCI_SUPPORT_SDR50)
  2098. mmc->caps |= MMC_CAP_UHS_SDR50;
  2099. if (caps[1] & SDHCI_SUPPORT_DDR50)
  2100. mmc->caps |= MMC_CAP_UHS_DDR50;
  2101. /* Does the host needs tuning for SDR50? */
  2102. if (caps[1] & SDHCI_USE_SDR50_TUNING)
  2103. host->flags |= SDHCI_SDR50_NEEDS_TUNING;
  2104. /* Driver Type(s) (A, C, D) supported by the host */
  2105. if (caps[1] & SDHCI_DRIVER_TYPE_A)
  2106. mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
  2107. if (caps[1] & SDHCI_DRIVER_TYPE_C)
  2108. mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
  2109. if (caps[1] & SDHCI_DRIVER_TYPE_D)
  2110. mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
  2111. /* Initial value for re-tuning timer count */
  2112. host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
  2113. SDHCI_RETUNING_TIMER_COUNT_SHIFT;
  2114. /*
  2115. * In case Re-tuning Timer is not disabled, the actual value of
  2116. * re-tuning timer will be 2 ^ (n - 1).
  2117. */
  2118. if (host->tuning_count)
  2119. host->tuning_count = 1 << (host->tuning_count - 1);
  2120. /* Re-tuning mode supported by the Host Controller */
  2121. host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
  2122. SDHCI_RETUNING_MODE_SHIFT;
  2123. ocr_avail = 0;
  2124. /*
  2125. * According to SD Host Controller spec v3.00, if the Host System
  2126. * can afford more than 150mA, Host Driver should set XPC to 1. Also
  2127. * the value is meaningful only if Voltage Support in the Capabilities
  2128. * register is set. The actual current value is 4 times the register
  2129. * value.
  2130. */
  2131. max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
  2132. if (caps[0] & SDHCI_CAN_VDD_330) {
  2133. int max_current_330;
  2134. ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
  2135. max_current_330 = ((max_current_caps &
  2136. SDHCI_MAX_CURRENT_330_MASK) >>
  2137. SDHCI_MAX_CURRENT_330_SHIFT) *
  2138. SDHCI_MAX_CURRENT_MULTIPLIER;
  2139. if (max_current_330 > 150)
  2140. mmc->caps |= MMC_CAP_SET_XPC_330;
  2141. }
  2142. if (caps[0] & SDHCI_CAN_VDD_300) {
  2143. int max_current_300;
  2144. ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
  2145. max_current_300 = ((max_current_caps &
  2146. SDHCI_MAX_CURRENT_300_MASK) >>
  2147. SDHCI_MAX_CURRENT_300_SHIFT) *
  2148. SDHCI_MAX_CURRENT_MULTIPLIER;
  2149. if (max_current_300 > 150)
  2150. mmc->caps |= MMC_CAP_SET_XPC_300;
  2151. }
  2152. if (caps[0] & SDHCI_CAN_VDD_180) {
  2153. int max_current_180;
  2154. ocr_avail |= MMC_VDD_165_195;
  2155. max_current_180 = ((max_current_caps &
  2156. SDHCI_MAX_CURRENT_180_MASK) >>
  2157. SDHCI_MAX_CURRENT_180_SHIFT) *
  2158. SDHCI_MAX_CURRENT_MULTIPLIER;
  2159. if (max_current_180 > 150)
  2160. mmc->caps |= MMC_CAP_SET_XPC_180;
  2161. /* Maximum current capabilities of the host at 1.8V */
  2162. if (max_current_180 >= 800)
  2163. mmc->caps |= MMC_CAP_MAX_CURRENT_800;
  2164. else if (max_current_180 >= 600)
  2165. mmc->caps |= MMC_CAP_MAX_CURRENT_600;
  2166. else if (max_current_180 >= 400)
  2167. mmc->caps |= MMC_CAP_MAX_CURRENT_400;
  2168. else
  2169. mmc->caps |= MMC_CAP_MAX_CURRENT_200;
  2170. }
  2171. mmc->ocr_avail = ocr_avail;
  2172. mmc->ocr_avail_sdio = ocr_avail;
  2173. if (host->ocr_avail_sdio)
  2174. mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
  2175. mmc->ocr_avail_sd = ocr_avail;
  2176. if (host->ocr_avail_sd)
  2177. mmc->ocr_avail_sd &= host->ocr_avail_sd;
  2178. else /* normal SD controllers don't support 1.8V */
  2179. mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
  2180. mmc->ocr_avail_mmc = ocr_avail;
  2181. if (host->ocr_avail_mmc)
  2182. mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
  2183. if (mmc->ocr_avail == 0) {
  2184. printk(KERN_ERR "%s: Hardware doesn't report any "
  2185. "support voltages.\n", mmc_hostname(mmc));
  2186. return -ENODEV;
  2187. }
  2188. spin_lock_init(&host->lock);
  2189. /*
  2190. * Maximum number of segments. Depends on if the hardware
  2191. * can do scatter/gather or not.
  2192. */
  2193. if (host->flags & SDHCI_USE_ADMA)
  2194. mmc->max_segs = 128;
  2195. else if (host->flags & SDHCI_USE_SDMA)
  2196. mmc->max_segs = 1;
  2197. else /* PIO */
  2198. mmc->max_segs = 128;
  2199. /*
  2200. * Maximum number of sectors in one transfer. Limited by DMA boundary
  2201. * size (512KiB).
  2202. */
  2203. mmc->max_req_size = 524288;
  2204. /*
  2205. * Maximum segment size. Could be one segment with the maximum number
  2206. * of bytes. When doing hardware scatter/gather, each entry cannot
  2207. * be larger than 64 KiB though.
  2208. */
  2209. if (host->flags & SDHCI_USE_ADMA) {
  2210. if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
  2211. mmc->max_seg_size = 65535;
  2212. else
  2213. mmc->max_seg_size = 65536;
  2214. } else {
  2215. mmc->max_seg_size = mmc->max_req_size;
  2216. }
  2217. /*
  2218. * Maximum block size. This varies from controller to controller and
  2219. * is specified in the capabilities register.
  2220. */
  2221. if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
  2222. mmc->max_blk_size = 2;
  2223. } else {
  2224. mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
  2225. SDHCI_MAX_BLOCK_SHIFT;
  2226. if (mmc->max_blk_size >= 3) {
  2227. printk(KERN_WARNING "%s: Invalid maximum block size, "
  2228. "assuming 512 bytes\n", mmc_hostname(mmc));
  2229. mmc->max_blk_size = 0;
  2230. }
  2231. }
  2232. mmc->max_blk_size = 512 << mmc->max_blk_size;
  2233. /*
  2234. * Maximum block count.
  2235. */
  2236. mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
  2237. /*
  2238. * Init tasklets.
  2239. */
  2240. tasklet_init(&host->card_tasklet,
  2241. sdhci_tasklet_card, (unsigned long)host);
  2242. tasklet_init(&host->finish_tasklet,
  2243. sdhci_tasklet_finish, (unsigned long)host);
  2244. setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
  2245. if (host->version >= SDHCI_SPEC_300) {
  2246. init_waitqueue_head(&host->buf_ready_int);
  2247. /* Initialize re-tuning timer */
  2248. init_timer(&host->tuning_timer);
  2249. host->tuning_timer.data = (unsigned long)host;
  2250. host->tuning_timer.function = sdhci_tuning_timer;
  2251. }
  2252. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  2253. mmc_hostname(mmc), host);
  2254. if (ret)
  2255. goto untasklet;
  2256. host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
  2257. if (IS_ERR(host->vmmc)) {
  2258. printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
  2259. host->vmmc = NULL;
  2260. } else {
  2261. regulator_enable(host->vmmc);
  2262. }
  2263. sdhci_init(host, 0);
  2264. #ifdef CONFIG_MMC_DEBUG
  2265. sdhci_dumpregs(host);
  2266. #endif
  2267. #ifdef SDHCI_USE_LEDS_CLASS
  2268. snprintf(host->led_name, sizeof(host->led_name),
  2269. "%s::", mmc_hostname(mmc));
  2270. host->led.name = host->led_name;
  2271. host->led.brightness = LED_OFF;
  2272. host->led.default_trigger = mmc_hostname(mmc);
  2273. host->led.brightness_set = sdhci_led_control;
  2274. ret = led_classdev_register(mmc_dev(mmc), &host->led);
  2275. if (ret)
  2276. goto reset;
  2277. #endif
  2278. mmiowb();
  2279. mmc_add_host(mmc);
  2280. printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
  2281. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  2282. (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
  2283. (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
  2284. sdhci_enable_card_detection(host);
  2285. return 0;
  2286. #ifdef SDHCI_USE_LEDS_CLASS
  2287. reset:
  2288. sdhci_reset(host, SDHCI_RESET_ALL);
  2289. free_irq(host->irq, host);
  2290. #endif
  2291. untasklet:
  2292. tasklet_kill(&host->card_tasklet);
  2293. tasklet_kill(&host->finish_tasklet);
  2294. return ret;
  2295. }
  2296. EXPORT_SYMBOL_GPL(sdhci_add_host);
  2297. void sdhci_remove_host(struct sdhci_host *host, int dead)
  2298. {
  2299. unsigned long flags;
  2300. if (dead) {
  2301. spin_lock_irqsave(&host->lock, flags);
  2302. host->flags |= SDHCI_DEVICE_DEAD;
  2303. if (host->mrq) {
  2304. printk(KERN_ERR "%s: Controller removed during "
  2305. " transfer!\n", mmc_hostname(host->mmc));
  2306. host->mrq->cmd->error = -ENOMEDIUM;
  2307. tasklet_schedule(&host->finish_tasklet);
  2308. }
  2309. spin_unlock_irqrestore(&host->lock, flags);
  2310. }
  2311. sdhci_disable_card_detection(host);
  2312. mmc_remove_host(host->mmc);
  2313. #ifdef SDHCI_USE_LEDS_CLASS
  2314. led_classdev_unregister(&host->led);
  2315. #endif
  2316. if (!dead)
  2317. sdhci_reset(host, SDHCI_RESET_ALL);
  2318. free_irq(host->irq, host);
  2319. del_timer_sync(&host->timer);
  2320. if (host->version >= SDHCI_SPEC_300)
  2321. del_timer_sync(&host->tuning_timer);
  2322. tasklet_kill(&host->card_tasklet);
  2323. tasklet_kill(&host->finish_tasklet);
  2324. if (host->vmmc) {
  2325. regulator_disable(host->vmmc);
  2326. regulator_put(host->vmmc);
  2327. }
  2328. kfree(host->adma_desc);
  2329. kfree(host->align_buffer);
  2330. host->adma_desc = NULL;
  2331. host->align_buffer = NULL;
  2332. }
  2333. EXPORT_SYMBOL_GPL(sdhci_remove_host);
  2334. void sdhci_free_host(struct sdhci_host *host)
  2335. {
  2336. mmc_free_host(host->mmc);
  2337. }
  2338. EXPORT_SYMBOL_GPL(sdhci_free_host);
  2339. /*****************************************************************************\
  2340. * *
  2341. * Driver init/exit *
  2342. * *
  2343. \*****************************************************************************/
  2344. static int __init sdhci_drv_init(void)
  2345. {
  2346. printk(KERN_INFO DRIVER_NAME
  2347. ": Secure Digital Host Controller Interface driver\n");
  2348. printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  2349. return 0;
  2350. }
  2351. static void __exit sdhci_drv_exit(void)
  2352. {
  2353. }
  2354. module_init(sdhci_drv_init);
  2355. module_exit(sdhci_drv_exit);
  2356. module_param(debug_quirks, uint, 0444);
  2357. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  2358. MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
  2359. MODULE_LICENSE("GPL");
  2360. MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");