sdhci.c 87 KB

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