sdhci.c 78 KB

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