sdhci.c 91 KB

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