sdhci.c 90 KB

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