sdhci.c 91 KB

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