sdhci.c 86 KB

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