sdhci.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362
  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_send_command(struct sdhci_host *, struct mmc_command *);
  42. static void sdhci_finish_command(struct sdhci_host *);
  43. static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
  44. static void sdhci_tuning_timer(unsigned long data);
  45. static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
  46. #ifdef CONFIG_PM_RUNTIME
  47. static int sdhci_runtime_pm_get(struct sdhci_host *host);
  48. static int sdhci_runtime_pm_put(struct sdhci_host *host);
  49. static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
  50. static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
  51. #else
  52. static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
  53. {
  54. return 0;
  55. }
  56. static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
  57. {
  58. return 0;
  59. }
  60. static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
  61. {
  62. }
  63. static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
  64. {
  65. }
  66. #endif
  67. static void sdhci_dumpregs(struct sdhci_host *host)
  68. {
  69. pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
  70. mmc_hostname(host->mmc));
  71. pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
  72. sdhci_readl(host, SDHCI_DMA_ADDRESS),
  73. sdhci_readw(host, SDHCI_HOST_VERSION));
  74. pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
  75. sdhci_readw(host, SDHCI_BLOCK_SIZE),
  76. sdhci_readw(host, SDHCI_BLOCK_COUNT));
  77. pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
  78. sdhci_readl(host, SDHCI_ARGUMENT),
  79. sdhci_readw(host, SDHCI_TRANSFER_MODE));
  80. pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
  81. sdhci_readl(host, SDHCI_PRESENT_STATE),
  82. sdhci_readb(host, SDHCI_HOST_CONTROL));
  83. pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
  84. sdhci_readb(host, SDHCI_POWER_CONTROL),
  85. sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
  86. pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
  87. sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
  88. sdhci_readw(host, SDHCI_CLOCK_CONTROL));
  89. pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
  90. sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
  91. sdhci_readl(host, SDHCI_INT_STATUS));
  92. pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
  93. sdhci_readl(host, SDHCI_INT_ENABLE),
  94. sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
  95. pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
  96. sdhci_readw(host, SDHCI_ACMD12_ERR),
  97. sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
  98. pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
  99. sdhci_readl(host, SDHCI_CAPABILITIES),
  100. sdhci_readl(host, SDHCI_CAPABILITIES_1));
  101. pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
  102. sdhci_readw(host, SDHCI_COMMAND),
  103. sdhci_readl(host, SDHCI_MAX_CURRENT));
  104. pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
  105. sdhci_readw(host, SDHCI_HOST_CONTROL2));
  106. if (host->flags & SDHCI_USE_ADMA)
  107. pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
  108. readl(host->ioaddr + SDHCI_ADMA_ERROR),
  109. readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
  110. pr_debug(DRIVER_NAME ": ===========================================\n");
  111. }
  112. /*****************************************************************************\
  113. * *
  114. * Low level functions *
  115. * *
  116. \*****************************************************************************/
  117. static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
  118. {
  119. u32 ier;
  120. ier = sdhci_readl(host, SDHCI_INT_ENABLE);
  121. ier &= ~clear;
  122. ier |= set;
  123. sdhci_writel(host, ier, SDHCI_INT_ENABLE);
  124. sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
  125. }
  126. static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
  127. {
  128. sdhci_clear_set_irqs(host, 0, irqs);
  129. }
  130. static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
  131. {
  132. sdhci_clear_set_irqs(host, irqs, 0);
  133. }
  134. static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
  135. {
  136. u32 present, irqs;
  137. if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
  138. (host->mmc->caps & MMC_CAP_NONREMOVABLE))
  139. return;
  140. present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  141. SDHCI_CARD_PRESENT;
  142. irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
  143. if (enable)
  144. sdhci_unmask_irqs(host, irqs);
  145. else
  146. sdhci_mask_irqs(host, irqs);
  147. }
  148. static void sdhci_enable_card_detection(struct sdhci_host *host)
  149. {
  150. sdhci_set_card_detection(host, true);
  151. }
  152. static void sdhci_disable_card_detection(struct sdhci_host *host)
  153. {
  154. sdhci_set_card_detection(host, false);
  155. }
  156. static void sdhci_reset(struct sdhci_host *host, u8 mask)
  157. {
  158. unsigned long timeout;
  159. u32 uninitialized_var(ier);
  160. if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
  161. if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
  162. SDHCI_CARD_PRESENT))
  163. return;
  164. }
  165. if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
  166. ier = sdhci_readl(host, SDHCI_INT_ENABLE);
  167. if (host->ops->platform_reset_enter)
  168. host->ops->platform_reset_enter(host, mask);
  169. sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
  170. if (mask & SDHCI_RESET_ALL) {
  171. host->clock = 0;
  172. /* Reset-all turns off SD Bus Power */
  173. if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
  174. sdhci_runtime_pm_bus_off(host);
  175. }
  176. /* Wait max 100 ms */
  177. timeout = 100;
  178. /* hw clears the bit when it's done */
  179. while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
  180. if (timeout == 0) {
  181. pr_err("%s: Reset 0x%x never completed.\n",
  182. mmc_hostname(host->mmc), (int)mask);
  183. sdhci_dumpregs(host);
  184. return;
  185. }
  186. timeout--;
  187. mdelay(1);
  188. }
  189. if (host->ops->platform_reset_exit)
  190. host->ops->platform_reset_exit(host, mask);
  191. if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
  192. sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
  193. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  194. if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
  195. host->ops->enable_dma(host);
  196. }
  197. }
  198. static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
  199. static void sdhci_init(struct sdhci_host *host, int soft)
  200. {
  201. if (soft)
  202. sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
  203. else
  204. sdhci_reset(host, SDHCI_RESET_ALL);
  205. sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
  206. SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
  207. SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
  208. SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
  209. SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
  210. if (soft) {
  211. /* force clock reconfiguration */
  212. host->clock = 0;
  213. sdhci_set_ios(host->mmc, &host->mmc->ios);
  214. }
  215. }
  216. static void sdhci_reinit(struct sdhci_host *host)
  217. {
  218. sdhci_init(host, 0);
  219. /*
  220. * Retuning stuffs are affected by different cards inserted and only
  221. * applicable to UHS-I cards. So reset these fields to their initial
  222. * value when card is removed.
  223. */
  224. if (host->flags & SDHCI_USING_RETUNING_TIMER) {
  225. host->flags &= ~SDHCI_USING_RETUNING_TIMER;
  226. del_timer_sync(&host->tuning_timer);
  227. host->flags &= ~SDHCI_NEEDS_RETUNING;
  228. host->mmc->max_blk_count =
  229. (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
  230. }
  231. sdhci_enable_card_detection(host);
  232. }
  233. static void sdhci_activate_led(struct sdhci_host *host)
  234. {
  235. u8 ctrl;
  236. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  237. ctrl |= SDHCI_CTRL_LED;
  238. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  239. }
  240. static void sdhci_deactivate_led(struct sdhci_host *host)
  241. {
  242. u8 ctrl;
  243. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  244. ctrl &= ~SDHCI_CTRL_LED;
  245. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  246. }
  247. #ifdef SDHCI_USE_LEDS_CLASS
  248. static void sdhci_led_control(struct led_classdev *led,
  249. enum led_brightness brightness)
  250. {
  251. struct sdhci_host *host = container_of(led, struct sdhci_host, led);
  252. unsigned long flags;
  253. spin_lock_irqsave(&host->lock, flags);
  254. if (host->runtime_suspended)
  255. goto out;
  256. if (brightness == LED_OFF)
  257. sdhci_deactivate_led(host);
  258. else
  259. sdhci_activate_led(host);
  260. out:
  261. spin_unlock_irqrestore(&host->lock, flags);
  262. }
  263. #endif
  264. /*****************************************************************************\
  265. * *
  266. * Core functions *
  267. * *
  268. \*****************************************************************************/
  269. static void sdhci_read_block_pio(struct sdhci_host *host)
  270. {
  271. unsigned long flags;
  272. size_t blksize, len, chunk;
  273. u32 uninitialized_var(scratch);
  274. u8 *buf;
  275. DBG("PIO reading\n");
  276. blksize = host->data->blksz;
  277. chunk = 0;
  278. local_irq_save(flags);
  279. while (blksize) {
  280. if (!sg_miter_next(&host->sg_miter))
  281. BUG();
  282. len = min(host->sg_miter.length, blksize);
  283. blksize -= len;
  284. host->sg_miter.consumed = len;
  285. buf = host->sg_miter.addr;
  286. while (len) {
  287. if (chunk == 0) {
  288. scratch = sdhci_readl(host, SDHCI_BUFFER);
  289. chunk = 4;
  290. }
  291. *buf = scratch & 0xFF;
  292. buf++;
  293. scratch >>= 8;
  294. chunk--;
  295. len--;
  296. }
  297. }
  298. sg_miter_stop(&host->sg_miter);
  299. local_irq_restore(flags);
  300. }
  301. static void sdhci_write_block_pio(struct sdhci_host *host)
  302. {
  303. unsigned long flags;
  304. size_t blksize, len, chunk;
  305. u32 scratch;
  306. u8 *buf;
  307. DBG("PIO writing\n");
  308. blksize = host->data->blksz;
  309. chunk = 0;
  310. scratch = 0;
  311. local_irq_save(flags);
  312. while (blksize) {
  313. if (!sg_miter_next(&host->sg_miter))
  314. BUG();
  315. len = min(host->sg_miter.length, blksize);
  316. blksize -= len;
  317. host->sg_miter.consumed = len;
  318. buf = host->sg_miter.addr;
  319. while (len) {
  320. scratch |= (u32)*buf << (chunk * 8);
  321. buf++;
  322. chunk++;
  323. len--;
  324. if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
  325. sdhci_writel(host, scratch, SDHCI_BUFFER);
  326. chunk = 0;
  327. scratch = 0;
  328. }
  329. }
  330. }
  331. sg_miter_stop(&host->sg_miter);
  332. local_irq_restore(flags);
  333. }
  334. static void sdhci_transfer_pio(struct sdhci_host *host)
  335. {
  336. u32 mask;
  337. BUG_ON(!host->data);
  338. if (host->blocks == 0)
  339. return;
  340. if (host->data->flags & MMC_DATA_READ)
  341. mask = SDHCI_DATA_AVAILABLE;
  342. else
  343. mask = SDHCI_SPACE_AVAILABLE;
  344. /*
  345. * Some controllers (JMicron JMB38x) mess up the buffer bits
  346. * for transfers < 4 bytes. As long as it is just one block,
  347. * we can ignore the bits.
  348. */
  349. if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
  350. (host->data->blocks == 1))
  351. mask = ~0;
  352. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  353. if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
  354. udelay(100);
  355. if (host->data->flags & MMC_DATA_READ)
  356. sdhci_read_block_pio(host);
  357. else
  358. sdhci_write_block_pio(host);
  359. host->blocks--;
  360. if (host->blocks == 0)
  361. break;
  362. }
  363. DBG("PIO transfer complete.\n");
  364. }
  365. static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
  366. {
  367. local_irq_save(*flags);
  368. return kmap_atomic(sg_page(sg)) + sg->offset;
  369. }
  370. static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
  371. {
  372. kunmap_atomic(buffer);
  373. local_irq_restore(*flags);
  374. }
  375. static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
  376. {
  377. __le32 *dataddr = (__le32 __force *)(desc + 4);
  378. __le16 *cmdlen = (__le16 __force *)desc;
  379. /* SDHCI specification says ADMA descriptors should be 4 byte
  380. * aligned, so using 16 or 32bit operations should be safe. */
  381. cmdlen[0] = cpu_to_le16(cmd);
  382. cmdlen[1] = cpu_to_le16(len);
  383. dataddr[0] = cpu_to_le32(addr);
  384. }
  385. static int sdhci_adma_table_pre(struct sdhci_host *host,
  386. struct mmc_data *data)
  387. {
  388. int direction;
  389. u8 *desc;
  390. u8 *align;
  391. dma_addr_t addr;
  392. dma_addr_t align_addr;
  393. int len, offset;
  394. struct scatterlist *sg;
  395. int i;
  396. char *buffer;
  397. unsigned long flags;
  398. /*
  399. * The spec does not specify endianness of descriptor table.
  400. * We currently guess that it is LE.
  401. */
  402. if (data->flags & MMC_DATA_READ)
  403. direction = DMA_FROM_DEVICE;
  404. else
  405. direction = DMA_TO_DEVICE;
  406. /*
  407. * The ADMA descriptor table is mapped further down as we
  408. * need to fill it with data first.
  409. */
  410. host->align_addr = dma_map_single(mmc_dev(host->mmc),
  411. host->align_buffer, 128 * 4, direction);
  412. if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
  413. goto fail;
  414. BUG_ON(host->align_addr & 0x3);
  415. host->sg_count = dma_map_sg(mmc_dev(host->mmc),
  416. data->sg, data->sg_len, direction);
  417. if (host->sg_count == 0)
  418. goto unmap_align;
  419. desc = host->adma_desc;
  420. align = host->align_buffer;
  421. align_addr = host->align_addr;
  422. for_each_sg(data->sg, sg, host->sg_count, i) {
  423. addr = sg_dma_address(sg);
  424. len = sg_dma_len(sg);
  425. /*
  426. * The SDHCI specification states that ADMA
  427. * addresses must be 32-bit aligned. If they
  428. * aren't, then we use a bounce buffer for
  429. * the (up to three) bytes that screw up the
  430. * alignment.
  431. */
  432. offset = (4 - (addr & 0x3)) & 0x3;
  433. if (offset) {
  434. if (data->flags & MMC_DATA_WRITE) {
  435. buffer = sdhci_kmap_atomic(sg, &flags);
  436. WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
  437. memcpy(align, buffer, offset);
  438. sdhci_kunmap_atomic(buffer, &flags);
  439. }
  440. /* tran, valid */
  441. sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
  442. BUG_ON(offset > 65536);
  443. align += 4;
  444. align_addr += 4;
  445. desc += 8;
  446. addr += offset;
  447. len -= offset;
  448. }
  449. BUG_ON(len > 65536);
  450. /* tran, valid */
  451. sdhci_set_adma_desc(desc, addr, len, 0x21);
  452. desc += 8;
  453. /*
  454. * If this triggers then we have a calculation bug
  455. * somewhere. :/
  456. */
  457. WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
  458. }
  459. if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
  460. /*
  461. * Mark the last descriptor as the terminating descriptor
  462. */
  463. if (desc != host->adma_desc) {
  464. desc -= 8;
  465. desc[0] |= 0x2; /* end */
  466. }
  467. } else {
  468. /*
  469. * Add a terminating entry.
  470. */
  471. /* nop, end, valid */
  472. sdhci_set_adma_desc(desc, 0, 0, 0x3);
  473. }
  474. /*
  475. * Resync align buffer as we might have changed it.
  476. */
  477. if (data->flags & MMC_DATA_WRITE) {
  478. dma_sync_single_for_device(mmc_dev(host->mmc),
  479. host->align_addr, 128 * 4, direction);
  480. }
  481. host->adma_addr = dma_map_single(mmc_dev(host->mmc),
  482. host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
  483. if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
  484. goto unmap_entries;
  485. BUG_ON(host->adma_addr & 0x3);
  486. return 0;
  487. unmap_entries:
  488. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  489. data->sg_len, direction);
  490. unmap_align:
  491. dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
  492. 128 * 4, direction);
  493. fail:
  494. return -EINVAL;
  495. }
  496. static void sdhci_adma_table_post(struct sdhci_host *host,
  497. struct mmc_data *data)
  498. {
  499. int direction;
  500. struct scatterlist *sg;
  501. int i, size;
  502. u8 *align;
  503. char *buffer;
  504. unsigned long flags;
  505. if (data->flags & MMC_DATA_READ)
  506. direction = DMA_FROM_DEVICE;
  507. else
  508. direction = DMA_TO_DEVICE;
  509. dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
  510. (128 * 2 + 1) * 4, DMA_TO_DEVICE);
  511. dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
  512. 128 * 4, direction);
  513. if (data->flags & MMC_DATA_READ) {
  514. dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
  515. data->sg_len, direction);
  516. align = host->align_buffer;
  517. for_each_sg(data->sg, sg, host->sg_count, i) {
  518. if (sg_dma_address(sg) & 0x3) {
  519. size = 4 - (sg_dma_address(sg) & 0x3);
  520. buffer = sdhci_kmap_atomic(sg, &flags);
  521. WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
  522. memcpy(buffer, align, size);
  523. sdhci_kunmap_atomic(buffer, &flags);
  524. align += 4;
  525. }
  526. }
  527. }
  528. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  529. data->sg_len, direction);
  530. }
  531. static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
  532. {
  533. u8 count;
  534. struct mmc_data *data = cmd->data;
  535. unsigned target_timeout, current_timeout;
  536. /*
  537. * If the host controller provides us with an incorrect timeout
  538. * value, just skip the check and use 0xE. The hardware may take
  539. * longer to time out, but that's much better than having a too-short
  540. * timeout value.
  541. */
  542. if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
  543. return 0xE;
  544. /* Unspecified timeout, assume max */
  545. if (!data && !cmd->cmd_timeout_ms)
  546. return 0xE;
  547. /* timeout in us */
  548. if (!data)
  549. target_timeout = cmd->cmd_timeout_ms * 1000;
  550. else {
  551. target_timeout = data->timeout_ns / 1000;
  552. if (host->clock)
  553. target_timeout += data->timeout_clks / host->clock;
  554. }
  555. /*
  556. * Figure out needed cycles.
  557. * We do this in steps in order to fit inside a 32 bit int.
  558. * The first step is the minimum timeout, which will have a
  559. * minimum resolution of 6 bits:
  560. * (1) 2^13*1000 > 2^22,
  561. * (2) host->timeout_clk < 2^16
  562. * =>
  563. * (1) / (2) > 2^6
  564. */
  565. count = 0;
  566. current_timeout = (1 << 13) * 1000 / host->timeout_clk;
  567. while (current_timeout < target_timeout) {
  568. count++;
  569. current_timeout <<= 1;
  570. if (count >= 0xF)
  571. break;
  572. }
  573. if (count >= 0xF) {
  574. DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
  575. mmc_hostname(host->mmc), count, cmd->opcode);
  576. count = 0xE;
  577. }
  578. return count;
  579. }
  580. static void sdhci_set_transfer_irqs(struct sdhci_host *host)
  581. {
  582. u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  583. u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  584. if (host->flags & SDHCI_REQ_USE_DMA)
  585. sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
  586. else
  587. sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
  588. }
  589. static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
  590. {
  591. u8 count;
  592. u8 ctrl;
  593. struct mmc_data *data = cmd->data;
  594. int ret;
  595. WARN_ON(host->data);
  596. if (data || (cmd->flags & MMC_RSP_BUSY)) {
  597. count = sdhci_calc_timeout(host, cmd);
  598. sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
  599. }
  600. if (!data)
  601. return;
  602. /* Sanity checks */
  603. BUG_ON(data->blksz * data->blocks > 524288);
  604. BUG_ON(data->blksz > host->mmc->max_blk_size);
  605. BUG_ON(data->blocks > 65535);
  606. host->data = data;
  607. host->data_early = 0;
  608. host->data->bytes_xfered = 0;
  609. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
  610. host->flags |= SDHCI_REQ_USE_DMA;
  611. /*
  612. * FIXME: This doesn't account for merging when mapping the
  613. * scatterlist.
  614. */
  615. if (host->flags & SDHCI_REQ_USE_DMA) {
  616. int broken, i;
  617. struct scatterlist *sg;
  618. broken = 0;
  619. if (host->flags & SDHCI_USE_ADMA) {
  620. if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
  621. broken = 1;
  622. } else {
  623. if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
  624. broken = 1;
  625. }
  626. if (unlikely(broken)) {
  627. for_each_sg(data->sg, sg, data->sg_len, i) {
  628. if (sg->length & 0x3) {
  629. DBG("Reverting to PIO because of "
  630. "transfer size (%d)\n",
  631. sg->length);
  632. host->flags &= ~SDHCI_REQ_USE_DMA;
  633. break;
  634. }
  635. }
  636. }
  637. }
  638. /*
  639. * The assumption here being that alignment is the same after
  640. * translation to device address space.
  641. */
  642. if (host->flags & SDHCI_REQ_USE_DMA) {
  643. int broken, i;
  644. struct scatterlist *sg;
  645. broken = 0;
  646. if (host->flags & SDHCI_USE_ADMA) {
  647. /*
  648. * As we use 3 byte chunks to work around
  649. * alignment problems, we need to check this
  650. * quirk.
  651. */
  652. if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
  653. broken = 1;
  654. } else {
  655. if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
  656. broken = 1;
  657. }
  658. if (unlikely(broken)) {
  659. for_each_sg(data->sg, sg, data->sg_len, i) {
  660. if (sg->offset & 0x3) {
  661. DBG("Reverting to PIO because of "
  662. "bad alignment\n");
  663. host->flags &= ~SDHCI_REQ_USE_DMA;
  664. break;
  665. }
  666. }
  667. }
  668. }
  669. if (host->flags & SDHCI_REQ_USE_DMA) {
  670. if (host->flags & SDHCI_USE_ADMA) {
  671. ret = sdhci_adma_table_pre(host, data);
  672. if (ret) {
  673. /*
  674. * This only happens when someone fed
  675. * us an invalid request.
  676. */
  677. WARN_ON(1);
  678. host->flags &= ~SDHCI_REQ_USE_DMA;
  679. } else {
  680. sdhci_writel(host, host->adma_addr,
  681. SDHCI_ADMA_ADDRESS);
  682. }
  683. } else {
  684. int sg_cnt;
  685. sg_cnt = dma_map_sg(mmc_dev(host->mmc),
  686. data->sg, data->sg_len,
  687. (data->flags & MMC_DATA_READ) ?
  688. DMA_FROM_DEVICE :
  689. DMA_TO_DEVICE);
  690. if (sg_cnt == 0) {
  691. /*
  692. * This only happens when someone fed
  693. * us an invalid request.
  694. */
  695. WARN_ON(1);
  696. host->flags &= ~SDHCI_REQ_USE_DMA;
  697. } else {
  698. WARN_ON(sg_cnt != 1);
  699. sdhci_writel(host, sg_dma_address(data->sg),
  700. SDHCI_DMA_ADDRESS);
  701. }
  702. }
  703. }
  704. /*
  705. * Always adjust the DMA selection as some controllers
  706. * (e.g. JMicron) can't do PIO properly when the selection
  707. * is ADMA.
  708. */
  709. if (host->version >= SDHCI_SPEC_200) {
  710. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  711. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  712. if ((host->flags & SDHCI_REQ_USE_DMA) &&
  713. (host->flags & SDHCI_USE_ADMA))
  714. ctrl |= SDHCI_CTRL_ADMA32;
  715. else
  716. ctrl |= SDHCI_CTRL_SDMA;
  717. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  718. }
  719. if (!(host->flags & SDHCI_REQ_USE_DMA)) {
  720. int flags;
  721. flags = SG_MITER_ATOMIC;
  722. if (host->data->flags & MMC_DATA_READ)
  723. flags |= SG_MITER_TO_SG;
  724. else
  725. flags |= SG_MITER_FROM_SG;
  726. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  727. host->blocks = data->blocks;
  728. }
  729. sdhci_set_transfer_irqs(host);
  730. /* Set the DMA boundary value and block size */
  731. sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
  732. data->blksz), SDHCI_BLOCK_SIZE);
  733. sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  734. }
  735. static void sdhci_set_transfer_mode(struct sdhci_host *host,
  736. struct mmc_command *cmd)
  737. {
  738. u16 mode;
  739. struct mmc_data *data = cmd->data;
  740. if (data == NULL)
  741. return;
  742. WARN_ON(!host->data);
  743. mode = SDHCI_TRNS_BLK_CNT_EN;
  744. if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
  745. mode |= SDHCI_TRNS_MULTI;
  746. /*
  747. * If we are sending CMD23, CMD12 never gets sent
  748. * on successful completion (so no Auto-CMD12).
  749. */
  750. if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
  751. mode |= SDHCI_TRNS_AUTO_CMD12;
  752. else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
  753. mode |= SDHCI_TRNS_AUTO_CMD23;
  754. sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
  755. }
  756. }
  757. if (data->flags & MMC_DATA_READ)
  758. mode |= SDHCI_TRNS_READ;
  759. if (host->flags & SDHCI_REQ_USE_DMA)
  760. mode |= SDHCI_TRNS_DMA;
  761. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  762. }
  763. static void sdhci_finish_data(struct sdhci_host *host)
  764. {
  765. struct mmc_data *data;
  766. BUG_ON(!host->data);
  767. data = host->data;
  768. host->data = NULL;
  769. if (host->flags & SDHCI_REQ_USE_DMA) {
  770. if (host->flags & SDHCI_USE_ADMA)
  771. sdhci_adma_table_post(host, data);
  772. else {
  773. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  774. data->sg_len, (data->flags & MMC_DATA_READ) ?
  775. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  776. }
  777. }
  778. /*
  779. * The specification states that the block count register must
  780. * be updated, but it does not specify at what point in the
  781. * data flow. That makes the register entirely useless to read
  782. * back so we have to assume that nothing made it to the card
  783. * in the event of an error.
  784. */
  785. if (data->error)
  786. data->bytes_xfered = 0;
  787. else
  788. data->bytes_xfered = data->blksz * data->blocks;
  789. /*
  790. * Need to send CMD12 if -
  791. * a) open-ended multiblock transfer (no CMD23)
  792. * b) error in multiblock transfer
  793. */
  794. if (data->stop &&
  795. (data->error ||
  796. !host->mrq->sbc)) {
  797. /*
  798. * The controller needs a reset of internal state machines
  799. * upon error conditions.
  800. */
  801. if (data->error) {
  802. sdhci_reset(host, SDHCI_RESET_CMD);
  803. sdhci_reset(host, SDHCI_RESET_DATA);
  804. }
  805. sdhci_send_command(host, data->stop);
  806. } else
  807. tasklet_schedule(&host->finish_tasklet);
  808. }
  809. static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
  810. {
  811. int flags;
  812. u32 mask;
  813. unsigned long timeout;
  814. WARN_ON(host->cmd);
  815. /* Wait max 10 ms */
  816. timeout = 10;
  817. mask = SDHCI_CMD_INHIBIT;
  818. if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  819. mask |= SDHCI_DATA_INHIBIT;
  820. /* We shouldn't wait for data inihibit for stop commands, even
  821. though they might use busy signaling */
  822. if (host->mrq->data && (cmd == host->mrq->data->stop))
  823. mask &= ~SDHCI_DATA_INHIBIT;
  824. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  825. if (timeout == 0) {
  826. pr_err("%s: Controller never released "
  827. "inhibit bit(s).\n", mmc_hostname(host->mmc));
  828. sdhci_dumpregs(host);
  829. cmd->error = -EIO;
  830. tasklet_schedule(&host->finish_tasklet);
  831. return;
  832. }
  833. timeout--;
  834. mdelay(1);
  835. }
  836. mod_timer(&host->timer, jiffies + 10 * HZ);
  837. host->cmd = cmd;
  838. sdhci_prepare_data(host, cmd);
  839. sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
  840. sdhci_set_transfer_mode(host, cmd);
  841. if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  842. pr_err("%s: Unsupported response type!\n",
  843. mmc_hostname(host->mmc));
  844. cmd->error = -EINVAL;
  845. tasklet_schedule(&host->finish_tasklet);
  846. return;
  847. }
  848. if (!(cmd->flags & MMC_RSP_PRESENT))
  849. flags = SDHCI_CMD_RESP_NONE;
  850. else if (cmd->flags & MMC_RSP_136)
  851. flags = SDHCI_CMD_RESP_LONG;
  852. else if (cmd->flags & MMC_RSP_BUSY)
  853. flags = SDHCI_CMD_RESP_SHORT_BUSY;
  854. else
  855. flags = SDHCI_CMD_RESP_SHORT;
  856. if (cmd->flags & MMC_RSP_CRC)
  857. flags |= SDHCI_CMD_CRC;
  858. if (cmd->flags & MMC_RSP_OPCODE)
  859. flags |= SDHCI_CMD_INDEX;
  860. /* CMD19 is special in that the Data Present Select should be set */
  861. if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
  862. cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
  863. flags |= SDHCI_CMD_DATA;
  864. sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
  865. }
  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. sdhci_enable_preset_value(host, false);
  1191. sdhci_set_clock(host, ios->clock);
  1192. if (ios->power_mode == MMC_POWER_OFF)
  1193. vdd_bit = sdhci_set_power(host, -1);
  1194. else
  1195. vdd_bit = sdhci_set_power(host, ios->vdd);
  1196. if (host->vmmc && vdd_bit != -1) {
  1197. spin_unlock_irqrestore(&host->lock, flags);
  1198. mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
  1199. spin_lock_irqsave(&host->lock, flags);
  1200. }
  1201. if (host->ops->platform_send_init_74_clocks)
  1202. host->ops->platform_send_init_74_clocks(host, ios->power_mode);
  1203. /*
  1204. * If your platform has 8-bit width support but is not a v3 controller,
  1205. * or if it requires special setup code, you should implement that in
  1206. * platform_bus_width().
  1207. */
  1208. if (host->ops->platform_bus_width) {
  1209. host->ops->platform_bus_width(host, ios->bus_width);
  1210. } else {
  1211. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  1212. if (ios->bus_width == MMC_BUS_WIDTH_8) {
  1213. ctrl &= ~SDHCI_CTRL_4BITBUS;
  1214. if (host->version >= SDHCI_SPEC_300)
  1215. ctrl |= SDHCI_CTRL_8BITBUS;
  1216. } else {
  1217. if (host->version >= SDHCI_SPEC_300)
  1218. ctrl &= ~SDHCI_CTRL_8BITBUS;
  1219. if (ios->bus_width == MMC_BUS_WIDTH_4)
  1220. ctrl |= SDHCI_CTRL_4BITBUS;
  1221. else
  1222. ctrl &= ~SDHCI_CTRL_4BITBUS;
  1223. }
  1224. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1225. }
  1226. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  1227. if ((ios->timing == MMC_TIMING_SD_HS ||
  1228. ios->timing == MMC_TIMING_MMC_HS)
  1229. && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
  1230. ctrl |= SDHCI_CTRL_HISPD;
  1231. else
  1232. ctrl &= ~SDHCI_CTRL_HISPD;
  1233. if (host->version >= SDHCI_SPEC_300) {
  1234. u16 clk, ctrl_2;
  1235. /* In case of UHS-I modes, set High Speed Enable */
  1236. if ((ios->timing == MMC_TIMING_MMC_HS200) ||
  1237. (ios->timing == MMC_TIMING_UHS_SDR50) ||
  1238. (ios->timing == MMC_TIMING_UHS_SDR104) ||
  1239. (ios->timing == MMC_TIMING_UHS_DDR50) ||
  1240. (ios->timing == MMC_TIMING_UHS_SDR25))
  1241. ctrl |= SDHCI_CTRL_HISPD;
  1242. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1243. if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
  1244. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1245. /*
  1246. * We only need to set Driver Strength if the
  1247. * preset value enable is not set.
  1248. */
  1249. ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
  1250. if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
  1251. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
  1252. else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
  1253. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
  1254. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  1255. } else {
  1256. /*
  1257. * According to SDHC Spec v3.00, if the Preset Value
  1258. * Enable in the Host Control 2 register is set, we
  1259. * need to reset SD Clock Enable before changing High
  1260. * Speed Enable to avoid generating clock gliches.
  1261. */
  1262. /* Reset SD Clock Enable */
  1263. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1264. clk &= ~SDHCI_CLOCK_CARD_EN;
  1265. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1266. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1267. /* Re-enable SD Clock */
  1268. sdhci_update_clock(host);
  1269. }
  1270. /* Reset SD Clock Enable */
  1271. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1272. clk &= ~SDHCI_CLOCK_CARD_EN;
  1273. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1274. if (host->ops->set_uhs_signaling)
  1275. host->ops->set_uhs_signaling(host, ios->timing);
  1276. else {
  1277. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1278. /* Select Bus Speed Mode for host */
  1279. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  1280. if (ios->timing == MMC_TIMING_MMC_HS200)
  1281. ctrl_2 |= SDHCI_CTRL_HS_SDR200;
  1282. else if (ios->timing == MMC_TIMING_UHS_SDR12)
  1283. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  1284. else if (ios->timing == MMC_TIMING_UHS_SDR25)
  1285. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  1286. else if (ios->timing == MMC_TIMING_UHS_SDR50)
  1287. ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
  1288. else if (ios->timing == MMC_TIMING_UHS_SDR104)
  1289. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  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_HS200_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. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1556. /*
  1557. * As per the Host Controller spec v3.00, tuning command
  1558. * generates Buffer Read Ready interrupt, so enable that.
  1559. *
  1560. * Note: The spec clearly says that when tuning sequence
  1561. * is being performed, the controller does not generate
  1562. * interrupts other than Buffer Read Ready interrupt. But
  1563. * to make sure we don't hit a controller bug, we _only_
  1564. * enable Buffer Read Ready interrupt here.
  1565. */
  1566. ier = sdhci_readl(host, SDHCI_INT_ENABLE);
  1567. sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
  1568. /*
  1569. * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
  1570. * of loops reaches 40 times or a timeout of 150ms occurs.
  1571. */
  1572. timeout = 150;
  1573. do {
  1574. struct mmc_command cmd = {0};
  1575. struct mmc_request mrq = {NULL};
  1576. if (!tuning_loop_counter && !timeout)
  1577. break;
  1578. cmd.opcode = opcode;
  1579. cmd.arg = 0;
  1580. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  1581. cmd.retries = 0;
  1582. cmd.data = NULL;
  1583. cmd.error = 0;
  1584. mrq.cmd = &cmd;
  1585. host->mrq = &mrq;
  1586. /*
  1587. * In response to CMD19, the card sends 64 bytes of tuning
  1588. * block to the Host Controller. So we set the block size
  1589. * to 64 here.
  1590. */
  1591. if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
  1592. if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
  1593. sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
  1594. SDHCI_BLOCK_SIZE);
  1595. else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
  1596. sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
  1597. SDHCI_BLOCK_SIZE);
  1598. } else {
  1599. sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
  1600. SDHCI_BLOCK_SIZE);
  1601. }
  1602. /*
  1603. * The tuning block is sent by the card to the host controller.
  1604. * So we set the TRNS_READ bit in the Transfer Mode register.
  1605. * This also takes care of setting DMA Enable and Multi Block
  1606. * Select in the same register to 0.
  1607. */
  1608. sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
  1609. sdhci_send_command(host, &cmd);
  1610. host->cmd = NULL;
  1611. host->mrq = NULL;
  1612. spin_unlock(&host->lock);
  1613. enable_irq(host->irq);
  1614. /* Wait for Buffer Read Ready interrupt */
  1615. wait_event_interruptible_timeout(host->buf_ready_int,
  1616. (host->tuning_done == 1),
  1617. msecs_to_jiffies(50));
  1618. disable_irq(host->irq);
  1619. spin_lock(&host->lock);
  1620. if (!host->tuning_done) {
  1621. pr_info(DRIVER_NAME ": Timeout waiting for "
  1622. "Buffer Read Ready interrupt during tuning "
  1623. "procedure, falling back to fixed sampling "
  1624. "clock\n");
  1625. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1626. ctrl &= ~SDHCI_CTRL_TUNED_CLK;
  1627. ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
  1628. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1629. err = -EIO;
  1630. goto out;
  1631. }
  1632. host->tuning_done = 0;
  1633. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1634. tuning_loop_counter--;
  1635. timeout--;
  1636. mdelay(1);
  1637. } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
  1638. /*
  1639. * The Host Driver has exhausted the maximum number of loops allowed,
  1640. * so use fixed sampling frequency.
  1641. */
  1642. if (!tuning_loop_counter || !timeout) {
  1643. ctrl &= ~SDHCI_CTRL_TUNED_CLK;
  1644. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1645. } else {
  1646. if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
  1647. pr_info(DRIVER_NAME ": Tuning procedure"
  1648. " failed, falling back to fixed sampling"
  1649. " clock\n");
  1650. err = -EIO;
  1651. }
  1652. }
  1653. out:
  1654. /*
  1655. * If this is the very first time we are here, we start the retuning
  1656. * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
  1657. * flag won't be set, we check this condition before actually starting
  1658. * the timer.
  1659. */
  1660. if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
  1661. (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
  1662. host->flags |= SDHCI_USING_RETUNING_TIMER;
  1663. mod_timer(&host->tuning_timer, jiffies +
  1664. host->tuning_count * HZ);
  1665. /* Tuning mode 1 limits the maximum data length to 4MB */
  1666. mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
  1667. } else {
  1668. host->flags &= ~SDHCI_NEEDS_RETUNING;
  1669. /* Reload the new initial value for timer */
  1670. if (host->tuning_mode == SDHCI_TUNING_MODE_1)
  1671. mod_timer(&host->tuning_timer, jiffies +
  1672. host->tuning_count * HZ);
  1673. }
  1674. /*
  1675. * In case tuning fails, host controllers which support re-tuning can
  1676. * try tuning again at a later time, when the re-tuning timer expires.
  1677. * So for these controllers, we return 0. Since there might be other
  1678. * controllers who do not have this capability, we return error for
  1679. * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
  1680. * a retuning timer to do the retuning for the card.
  1681. */
  1682. if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
  1683. err = 0;
  1684. sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
  1685. spin_unlock(&host->lock);
  1686. enable_irq(host->irq);
  1687. sdhci_runtime_pm_put(host);
  1688. return err;
  1689. }
  1690. static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
  1691. {
  1692. u16 ctrl;
  1693. /* Host Controller v3.00 defines preset value registers */
  1694. if (host->version < SDHCI_SPEC_300)
  1695. return;
  1696. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1697. /*
  1698. * We only enable or disable Preset Value if they are not already
  1699. * enabled or disabled respectively. Otherwise, we bail out.
  1700. */
  1701. if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
  1702. ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
  1703. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1704. host->flags |= SDHCI_PV_ENABLED;
  1705. } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
  1706. ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
  1707. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1708. host->flags &= ~SDHCI_PV_ENABLED;
  1709. }
  1710. }
  1711. static void sdhci_card_event(struct mmc_host *mmc)
  1712. {
  1713. struct sdhci_host *host = mmc_priv(mmc);
  1714. unsigned long flags;
  1715. spin_lock_irqsave(&host->lock, flags);
  1716. /* Check host->mrq first in case we are runtime suspended */
  1717. if (host->mrq && !sdhci_do_get_cd(host)) {
  1718. pr_err("%s: Card removed during transfer!\n",
  1719. mmc_hostname(host->mmc));
  1720. pr_err("%s: Resetting controller.\n",
  1721. mmc_hostname(host->mmc));
  1722. sdhci_reset(host, SDHCI_RESET_CMD);
  1723. sdhci_reset(host, SDHCI_RESET_DATA);
  1724. host->mrq->cmd->error = -ENOMEDIUM;
  1725. tasklet_schedule(&host->finish_tasklet);
  1726. }
  1727. spin_unlock_irqrestore(&host->lock, flags);
  1728. }
  1729. static const struct mmc_host_ops sdhci_ops = {
  1730. .request = sdhci_request,
  1731. .set_ios = sdhci_set_ios,
  1732. .get_cd = sdhci_get_cd,
  1733. .get_ro = sdhci_get_ro,
  1734. .hw_reset = sdhci_hw_reset,
  1735. .enable_sdio_irq = sdhci_enable_sdio_irq,
  1736. .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
  1737. .execute_tuning = sdhci_execute_tuning,
  1738. .card_event = sdhci_card_event,
  1739. .card_busy = sdhci_card_busy,
  1740. };
  1741. /*****************************************************************************\
  1742. * *
  1743. * Tasklets *
  1744. * *
  1745. \*****************************************************************************/
  1746. static void sdhci_tasklet_card(unsigned long param)
  1747. {
  1748. struct sdhci_host *host = (struct sdhci_host*)param;
  1749. sdhci_card_event(host->mmc);
  1750. mmc_detect_change(host->mmc, msecs_to_jiffies(200));
  1751. }
  1752. static void sdhci_tasklet_finish(unsigned long param)
  1753. {
  1754. struct sdhci_host *host;
  1755. unsigned long flags;
  1756. struct mmc_request *mrq;
  1757. host = (struct sdhci_host*)param;
  1758. spin_lock_irqsave(&host->lock, flags);
  1759. /*
  1760. * If this tasklet gets rescheduled while running, it will
  1761. * be run again afterwards but without any active request.
  1762. */
  1763. if (!host->mrq) {
  1764. spin_unlock_irqrestore(&host->lock, flags);
  1765. return;
  1766. }
  1767. del_timer(&host->timer);
  1768. mrq = host->mrq;
  1769. /*
  1770. * The controller needs a reset of internal state machines
  1771. * upon error conditions.
  1772. */
  1773. if (!(host->flags & SDHCI_DEVICE_DEAD) &&
  1774. ((mrq->cmd && mrq->cmd->error) ||
  1775. (mrq->data && (mrq->data->error ||
  1776. (mrq->data->stop && mrq->data->stop->error))) ||
  1777. (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
  1778. /* Some controllers need this kick or reset won't work here */
  1779. if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
  1780. /* This is to force an update */
  1781. sdhci_update_clock(host);
  1782. /* Spec says we should do both at the same time, but Ricoh
  1783. controllers do not like that. */
  1784. sdhci_reset(host, SDHCI_RESET_CMD);
  1785. sdhci_reset(host, SDHCI_RESET_DATA);
  1786. }
  1787. host->mrq = NULL;
  1788. host->cmd = NULL;
  1789. host->data = NULL;
  1790. #ifndef SDHCI_USE_LEDS_CLASS
  1791. sdhci_deactivate_led(host);
  1792. #endif
  1793. mmiowb();
  1794. spin_unlock_irqrestore(&host->lock, flags);
  1795. mmc_request_done(host->mmc, mrq);
  1796. sdhci_runtime_pm_put(host);
  1797. }
  1798. static void sdhci_timeout_timer(unsigned long data)
  1799. {
  1800. struct sdhci_host *host;
  1801. unsigned long flags;
  1802. host = (struct sdhci_host*)data;
  1803. spin_lock_irqsave(&host->lock, flags);
  1804. if (host->mrq) {
  1805. pr_err("%s: Timeout waiting for hardware "
  1806. "interrupt.\n", mmc_hostname(host->mmc));
  1807. sdhci_dumpregs(host);
  1808. if (host->data) {
  1809. host->data->error = -ETIMEDOUT;
  1810. sdhci_finish_data(host);
  1811. } else {
  1812. if (host->cmd)
  1813. host->cmd->error = -ETIMEDOUT;
  1814. else
  1815. host->mrq->cmd->error = -ETIMEDOUT;
  1816. tasklet_schedule(&host->finish_tasklet);
  1817. }
  1818. }
  1819. mmiowb();
  1820. spin_unlock_irqrestore(&host->lock, flags);
  1821. }
  1822. static void sdhci_tuning_timer(unsigned long data)
  1823. {
  1824. struct sdhci_host *host;
  1825. unsigned long flags;
  1826. host = (struct sdhci_host *)data;
  1827. spin_lock_irqsave(&host->lock, flags);
  1828. host->flags |= SDHCI_NEEDS_RETUNING;
  1829. spin_unlock_irqrestore(&host->lock, flags);
  1830. }
  1831. /*****************************************************************************\
  1832. * *
  1833. * Interrupt handling *
  1834. * *
  1835. \*****************************************************************************/
  1836. static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
  1837. {
  1838. BUG_ON(intmask == 0);
  1839. if (!host->cmd) {
  1840. pr_err("%s: Got command interrupt 0x%08x even "
  1841. "though no command operation was in progress.\n",
  1842. mmc_hostname(host->mmc), (unsigned)intmask);
  1843. sdhci_dumpregs(host);
  1844. return;
  1845. }
  1846. if (intmask & SDHCI_INT_TIMEOUT)
  1847. host->cmd->error = -ETIMEDOUT;
  1848. else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
  1849. SDHCI_INT_INDEX))
  1850. host->cmd->error = -EILSEQ;
  1851. if (host->cmd->error) {
  1852. tasklet_schedule(&host->finish_tasklet);
  1853. return;
  1854. }
  1855. /*
  1856. * The host can send and interrupt when the busy state has
  1857. * ended, allowing us to wait without wasting CPU cycles.
  1858. * Unfortunately this is overloaded on the "data complete"
  1859. * interrupt, so we need to take some care when handling
  1860. * it.
  1861. *
  1862. * Note: The 1.0 specification is a bit ambiguous about this
  1863. * feature so there might be some problems with older
  1864. * controllers.
  1865. */
  1866. if (host->cmd->flags & MMC_RSP_BUSY) {
  1867. if (host->cmd->data)
  1868. DBG("Cannot wait for busy signal when also "
  1869. "doing a data transfer");
  1870. else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
  1871. return;
  1872. /* The controller does not support the end-of-busy IRQ,
  1873. * fall through and take the SDHCI_INT_RESPONSE */
  1874. }
  1875. if (intmask & SDHCI_INT_RESPONSE)
  1876. sdhci_finish_command(host);
  1877. }
  1878. #ifdef CONFIG_MMC_DEBUG
  1879. static void sdhci_show_adma_error(struct sdhci_host *host)
  1880. {
  1881. const char *name = mmc_hostname(host->mmc);
  1882. u8 *desc = host->adma_desc;
  1883. __le32 *dma;
  1884. __le16 *len;
  1885. u8 attr;
  1886. sdhci_dumpregs(host);
  1887. while (true) {
  1888. dma = (__le32 *)(desc + 4);
  1889. len = (__le16 *)(desc + 2);
  1890. attr = *desc;
  1891. DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
  1892. name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
  1893. desc += 8;
  1894. if (attr & 2)
  1895. break;
  1896. }
  1897. }
  1898. #else
  1899. static void sdhci_show_adma_error(struct sdhci_host *host) { }
  1900. #endif
  1901. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  1902. {
  1903. u32 command;
  1904. BUG_ON(intmask == 0);
  1905. /* CMD19 generates _only_ Buffer Read Ready interrupt */
  1906. if (intmask & SDHCI_INT_DATA_AVAIL) {
  1907. command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
  1908. if (command == MMC_SEND_TUNING_BLOCK ||
  1909. command == MMC_SEND_TUNING_BLOCK_HS200) {
  1910. host->tuning_done = 1;
  1911. wake_up(&host->buf_ready_int);
  1912. return;
  1913. }
  1914. }
  1915. if (!host->data) {
  1916. /*
  1917. * The "data complete" interrupt is also used to
  1918. * indicate that a busy state has ended. See comment
  1919. * above in sdhci_cmd_irq().
  1920. */
  1921. if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
  1922. if (intmask & SDHCI_INT_DATA_END) {
  1923. sdhci_finish_command(host);
  1924. return;
  1925. }
  1926. }
  1927. pr_err("%s: Got data interrupt 0x%08x even "
  1928. "though no data operation was in progress.\n",
  1929. mmc_hostname(host->mmc), (unsigned)intmask);
  1930. sdhci_dumpregs(host);
  1931. return;
  1932. }
  1933. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  1934. host->data->error = -ETIMEDOUT;
  1935. else if (intmask & SDHCI_INT_DATA_END_BIT)
  1936. host->data->error = -EILSEQ;
  1937. else if ((intmask & SDHCI_INT_DATA_CRC) &&
  1938. SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
  1939. != MMC_BUS_TEST_R)
  1940. host->data->error = -EILSEQ;
  1941. else if (intmask & SDHCI_INT_ADMA_ERROR) {
  1942. pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
  1943. sdhci_show_adma_error(host);
  1944. host->data->error = -EIO;
  1945. if (host->ops->adma_workaround)
  1946. host->ops->adma_workaround(host, intmask);
  1947. }
  1948. if (host->data->error)
  1949. sdhci_finish_data(host);
  1950. else {
  1951. if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  1952. sdhci_transfer_pio(host);
  1953. /*
  1954. * We currently don't do anything fancy with DMA
  1955. * boundaries, but as we can't disable the feature
  1956. * we need to at least restart the transfer.
  1957. *
  1958. * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
  1959. * should return a valid address to continue from, but as
  1960. * some controllers are faulty, don't trust them.
  1961. */
  1962. if (intmask & SDHCI_INT_DMA_END) {
  1963. u32 dmastart, dmanow;
  1964. dmastart = sg_dma_address(host->data->sg);
  1965. dmanow = dmastart + host->data->bytes_xfered;
  1966. /*
  1967. * Force update to the next DMA block boundary.
  1968. */
  1969. dmanow = (dmanow &
  1970. ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
  1971. SDHCI_DEFAULT_BOUNDARY_SIZE;
  1972. host->data->bytes_xfered = dmanow - dmastart;
  1973. DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
  1974. " next 0x%08x\n",
  1975. mmc_hostname(host->mmc), dmastart,
  1976. host->data->bytes_xfered, dmanow);
  1977. sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
  1978. }
  1979. if (intmask & SDHCI_INT_DATA_END) {
  1980. if (host->cmd) {
  1981. /*
  1982. * Data managed to finish before the
  1983. * command completed. Make sure we do
  1984. * things in the proper order.
  1985. */
  1986. host->data_early = 1;
  1987. } else {
  1988. sdhci_finish_data(host);
  1989. }
  1990. }
  1991. }
  1992. }
  1993. static irqreturn_t sdhci_irq(int irq, void *dev_id)
  1994. {
  1995. irqreturn_t result;
  1996. struct sdhci_host *host = dev_id;
  1997. u32 intmask, unexpected = 0;
  1998. int cardint = 0, max_loops = 16;
  1999. spin_lock(&host->lock);
  2000. if (host->runtime_suspended) {
  2001. spin_unlock(&host->lock);
  2002. pr_warning("%s: got irq while runtime suspended\n",
  2003. mmc_hostname(host->mmc));
  2004. return IRQ_HANDLED;
  2005. }
  2006. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  2007. if (!intmask || intmask == 0xffffffff) {
  2008. result = IRQ_NONE;
  2009. goto out;
  2010. }
  2011. again:
  2012. DBG("*** %s got interrupt: 0x%08x\n",
  2013. mmc_hostname(host->mmc), intmask);
  2014. if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  2015. u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  2016. SDHCI_CARD_PRESENT;
  2017. /*
  2018. * There is a observation on i.mx esdhc. INSERT bit will be
  2019. * immediately set again when it gets cleared, if a card is
  2020. * inserted. We have to mask the irq to prevent interrupt
  2021. * storm which will freeze the system. And the REMOVE gets
  2022. * the same situation.
  2023. *
  2024. * More testing are needed here to ensure it works for other
  2025. * platforms though.
  2026. */
  2027. sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
  2028. SDHCI_INT_CARD_REMOVE);
  2029. sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
  2030. SDHCI_INT_CARD_INSERT);
  2031. sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
  2032. SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
  2033. intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
  2034. tasklet_schedule(&host->card_tasklet);
  2035. }
  2036. if (intmask & SDHCI_INT_CMD_MASK) {
  2037. sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
  2038. SDHCI_INT_STATUS);
  2039. sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  2040. }
  2041. if (intmask & SDHCI_INT_DATA_MASK) {
  2042. sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
  2043. SDHCI_INT_STATUS);
  2044. sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  2045. }
  2046. intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
  2047. intmask &= ~SDHCI_INT_ERROR;
  2048. if (intmask & SDHCI_INT_BUS_POWER) {
  2049. pr_err("%s: Card is consuming too much power!\n",
  2050. mmc_hostname(host->mmc));
  2051. sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
  2052. }
  2053. intmask &= ~SDHCI_INT_BUS_POWER;
  2054. if (intmask & SDHCI_INT_CARD_INT)
  2055. cardint = 1;
  2056. intmask &= ~SDHCI_INT_CARD_INT;
  2057. if (intmask) {
  2058. unexpected |= intmask;
  2059. sdhci_writel(host, intmask, SDHCI_INT_STATUS);
  2060. }
  2061. result = IRQ_HANDLED;
  2062. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  2063. if (intmask && --max_loops)
  2064. goto again;
  2065. out:
  2066. spin_unlock(&host->lock);
  2067. if (unexpected) {
  2068. pr_err("%s: Unexpected interrupt 0x%08x.\n",
  2069. mmc_hostname(host->mmc), unexpected);
  2070. sdhci_dumpregs(host);
  2071. }
  2072. /*
  2073. * We have to delay this as it calls back into the driver.
  2074. */
  2075. if (cardint)
  2076. mmc_signal_sdio_irq(host->mmc);
  2077. return result;
  2078. }
  2079. /*****************************************************************************\
  2080. * *
  2081. * Suspend/resume *
  2082. * *
  2083. \*****************************************************************************/
  2084. #ifdef CONFIG_PM
  2085. void sdhci_enable_irq_wakeups(struct sdhci_host *host)
  2086. {
  2087. u8 val;
  2088. u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
  2089. | SDHCI_WAKE_ON_INT;
  2090. val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
  2091. val |= mask ;
  2092. /* Avoid fake wake up */
  2093. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  2094. val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
  2095. sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
  2096. }
  2097. EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
  2098. void sdhci_disable_irq_wakeups(struct sdhci_host *host)
  2099. {
  2100. u8 val;
  2101. u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
  2102. | SDHCI_WAKE_ON_INT;
  2103. val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
  2104. val &= ~mask;
  2105. sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
  2106. }
  2107. EXPORT_SYMBOL_GPL(sdhci_disable_irq_wakeups);
  2108. int sdhci_suspend_host(struct sdhci_host *host)
  2109. {
  2110. int ret;
  2111. if (host->ops->platform_suspend)
  2112. host->ops->platform_suspend(host);
  2113. sdhci_disable_card_detection(host);
  2114. /* Disable tuning since we are suspending */
  2115. if (host->flags & SDHCI_USING_RETUNING_TIMER) {
  2116. del_timer_sync(&host->tuning_timer);
  2117. host->flags &= ~SDHCI_NEEDS_RETUNING;
  2118. }
  2119. ret = mmc_suspend_host(host->mmc);
  2120. if (ret) {
  2121. if (host->flags & SDHCI_USING_RETUNING_TIMER) {
  2122. host->flags |= SDHCI_NEEDS_RETUNING;
  2123. mod_timer(&host->tuning_timer, jiffies +
  2124. host->tuning_count * HZ);
  2125. }
  2126. sdhci_enable_card_detection(host);
  2127. return ret;
  2128. }
  2129. if (!device_may_wakeup(mmc_dev(host->mmc))) {
  2130. sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
  2131. free_irq(host->irq, host);
  2132. } else {
  2133. sdhci_enable_irq_wakeups(host);
  2134. enable_irq_wake(host->irq);
  2135. }
  2136. return ret;
  2137. }
  2138. EXPORT_SYMBOL_GPL(sdhci_suspend_host);
  2139. int sdhci_resume_host(struct sdhci_host *host)
  2140. {
  2141. int ret;
  2142. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2143. if (host->ops->enable_dma)
  2144. host->ops->enable_dma(host);
  2145. }
  2146. if (!device_may_wakeup(mmc_dev(host->mmc))) {
  2147. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  2148. mmc_hostname(host->mmc), host);
  2149. if (ret)
  2150. return ret;
  2151. } else {
  2152. sdhci_disable_irq_wakeups(host);
  2153. disable_irq_wake(host->irq);
  2154. }
  2155. if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
  2156. (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
  2157. /* Card keeps power but host controller does not */
  2158. sdhci_init(host, 0);
  2159. host->pwr = 0;
  2160. host->clock = 0;
  2161. sdhci_do_set_ios(host, &host->mmc->ios);
  2162. } else {
  2163. sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
  2164. mmiowb();
  2165. }
  2166. ret = mmc_resume_host(host->mmc);
  2167. sdhci_enable_card_detection(host);
  2168. if (host->ops->platform_resume)
  2169. host->ops->platform_resume(host);
  2170. /* Set the re-tuning expiration flag */
  2171. if (host->flags & SDHCI_USING_RETUNING_TIMER)
  2172. host->flags |= SDHCI_NEEDS_RETUNING;
  2173. return ret;
  2174. }
  2175. EXPORT_SYMBOL_GPL(sdhci_resume_host);
  2176. #endif /* CONFIG_PM */
  2177. #ifdef CONFIG_PM_RUNTIME
  2178. static int sdhci_runtime_pm_get(struct sdhci_host *host)
  2179. {
  2180. return pm_runtime_get_sync(host->mmc->parent);
  2181. }
  2182. static int sdhci_runtime_pm_put(struct sdhci_host *host)
  2183. {
  2184. pm_runtime_mark_last_busy(host->mmc->parent);
  2185. return pm_runtime_put_autosuspend(host->mmc->parent);
  2186. }
  2187. static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
  2188. {
  2189. if (host->runtime_suspended || host->bus_on)
  2190. return;
  2191. host->bus_on = true;
  2192. pm_runtime_get_noresume(host->mmc->parent);
  2193. }
  2194. static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
  2195. {
  2196. if (host->runtime_suspended || !host->bus_on)
  2197. return;
  2198. host->bus_on = false;
  2199. pm_runtime_put_noidle(host->mmc->parent);
  2200. }
  2201. int sdhci_runtime_suspend_host(struct sdhci_host *host)
  2202. {
  2203. unsigned long flags;
  2204. int ret = 0;
  2205. /* Disable tuning since we are suspending */
  2206. if (host->flags & SDHCI_USING_RETUNING_TIMER) {
  2207. del_timer_sync(&host->tuning_timer);
  2208. host->flags &= ~SDHCI_NEEDS_RETUNING;
  2209. }
  2210. spin_lock_irqsave(&host->lock, flags);
  2211. sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
  2212. spin_unlock_irqrestore(&host->lock, flags);
  2213. synchronize_irq(host->irq);
  2214. spin_lock_irqsave(&host->lock, flags);
  2215. host->runtime_suspended = true;
  2216. spin_unlock_irqrestore(&host->lock, flags);
  2217. return ret;
  2218. }
  2219. EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
  2220. int sdhci_runtime_resume_host(struct sdhci_host *host)
  2221. {
  2222. unsigned long flags;
  2223. int ret = 0, host_flags = host->flags;
  2224. if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2225. if (host->ops->enable_dma)
  2226. host->ops->enable_dma(host);
  2227. }
  2228. sdhci_init(host, 0);
  2229. /* Force clock and power re-program */
  2230. host->pwr = 0;
  2231. host->clock = 0;
  2232. sdhci_do_set_ios(host, &host->mmc->ios);
  2233. sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
  2234. if ((host_flags & SDHCI_PV_ENABLED) &&
  2235. !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
  2236. spin_lock_irqsave(&host->lock, flags);
  2237. sdhci_enable_preset_value(host, true);
  2238. spin_unlock_irqrestore(&host->lock, flags);
  2239. }
  2240. /* Set the re-tuning expiration flag */
  2241. if (host->flags & SDHCI_USING_RETUNING_TIMER)
  2242. host->flags |= SDHCI_NEEDS_RETUNING;
  2243. spin_lock_irqsave(&host->lock, flags);
  2244. host->runtime_suspended = false;
  2245. /* Enable SDIO IRQ */
  2246. if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
  2247. sdhci_enable_sdio_irq_nolock(host, true);
  2248. /* Enable Card Detection */
  2249. sdhci_enable_card_detection(host);
  2250. spin_unlock_irqrestore(&host->lock, flags);
  2251. return ret;
  2252. }
  2253. EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
  2254. #endif
  2255. /*****************************************************************************\
  2256. * *
  2257. * Device allocation/registration *
  2258. * *
  2259. \*****************************************************************************/
  2260. struct sdhci_host *sdhci_alloc_host(struct device *dev,
  2261. size_t priv_size)
  2262. {
  2263. struct mmc_host *mmc;
  2264. struct sdhci_host *host;
  2265. WARN_ON(dev == NULL);
  2266. mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
  2267. if (!mmc)
  2268. return ERR_PTR(-ENOMEM);
  2269. host = mmc_priv(mmc);
  2270. host->mmc = mmc;
  2271. return host;
  2272. }
  2273. EXPORT_SYMBOL_GPL(sdhci_alloc_host);
  2274. int sdhci_add_host(struct sdhci_host *host)
  2275. {
  2276. struct mmc_host *mmc;
  2277. u32 caps[2] = {0, 0};
  2278. u32 max_current_caps;
  2279. unsigned int ocr_avail;
  2280. int ret;
  2281. WARN_ON(host == NULL);
  2282. if (host == NULL)
  2283. return -EINVAL;
  2284. mmc = host->mmc;
  2285. if (debug_quirks)
  2286. host->quirks = debug_quirks;
  2287. if (debug_quirks2)
  2288. host->quirks2 = debug_quirks2;
  2289. sdhci_reset(host, SDHCI_RESET_ALL);
  2290. host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
  2291. host->version = (host->version & SDHCI_SPEC_VER_MASK)
  2292. >> SDHCI_SPEC_VER_SHIFT;
  2293. if (host->version > SDHCI_SPEC_300) {
  2294. pr_err("%s: Unknown controller version (%d). "
  2295. "You may experience problems.\n", mmc_hostname(mmc),
  2296. host->version);
  2297. }
  2298. caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
  2299. sdhci_readl(host, SDHCI_CAPABILITIES);
  2300. if (host->version >= SDHCI_SPEC_300)
  2301. caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
  2302. host->caps1 :
  2303. sdhci_readl(host, SDHCI_CAPABILITIES_1);
  2304. if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
  2305. host->flags |= SDHCI_USE_SDMA;
  2306. else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
  2307. DBG("Controller doesn't have SDMA capability\n");
  2308. else
  2309. host->flags |= SDHCI_USE_SDMA;
  2310. if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
  2311. (host->flags & SDHCI_USE_SDMA)) {
  2312. DBG("Disabling DMA as it is marked broken\n");
  2313. host->flags &= ~SDHCI_USE_SDMA;
  2314. }
  2315. if ((host->version >= SDHCI_SPEC_200) &&
  2316. (caps[0] & SDHCI_CAN_DO_ADMA2))
  2317. host->flags |= SDHCI_USE_ADMA;
  2318. if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
  2319. (host->flags & SDHCI_USE_ADMA)) {
  2320. DBG("Disabling ADMA as it is marked broken\n");
  2321. host->flags &= ~SDHCI_USE_ADMA;
  2322. }
  2323. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2324. if (host->ops->enable_dma) {
  2325. if (host->ops->enable_dma(host)) {
  2326. pr_warning("%s: No suitable DMA "
  2327. "available. Falling back to PIO.\n",
  2328. mmc_hostname(mmc));
  2329. host->flags &=
  2330. ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
  2331. }
  2332. }
  2333. }
  2334. if (host->flags & SDHCI_USE_ADMA) {
  2335. /*
  2336. * We need to allocate descriptors for all sg entries
  2337. * (128) and potentially one alignment transfer for
  2338. * each of those entries.
  2339. */
  2340. host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
  2341. host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
  2342. if (!host->adma_desc || !host->align_buffer) {
  2343. kfree(host->adma_desc);
  2344. kfree(host->align_buffer);
  2345. pr_warning("%s: Unable to allocate ADMA "
  2346. "buffers. Falling back to standard DMA.\n",
  2347. mmc_hostname(mmc));
  2348. host->flags &= ~SDHCI_USE_ADMA;
  2349. }
  2350. }
  2351. /*
  2352. * If we use DMA, then it's up to the caller to set the DMA
  2353. * mask, but PIO does not need the hw shim so we set a new
  2354. * mask here in that case.
  2355. */
  2356. if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
  2357. host->dma_mask = DMA_BIT_MASK(64);
  2358. mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
  2359. }
  2360. if (host->version >= SDHCI_SPEC_300)
  2361. host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
  2362. >> SDHCI_CLOCK_BASE_SHIFT;
  2363. else
  2364. host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
  2365. >> SDHCI_CLOCK_BASE_SHIFT;
  2366. host->max_clk *= 1000000;
  2367. if (host->max_clk == 0 || host->quirks &
  2368. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
  2369. if (!host->ops->get_max_clock) {
  2370. pr_err("%s: Hardware doesn't specify base clock "
  2371. "frequency.\n", mmc_hostname(mmc));
  2372. return -ENODEV;
  2373. }
  2374. host->max_clk = host->ops->get_max_clock(host);
  2375. }
  2376. /*
  2377. * In case of Host Controller v3.00, find out whether clock
  2378. * multiplier is supported.
  2379. */
  2380. host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
  2381. SDHCI_CLOCK_MUL_SHIFT;
  2382. /*
  2383. * In case the value in Clock Multiplier is 0, then programmable
  2384. * clock mode is not supported, otherwise the actual clock
  2385. * multiplier is one more than the value of Clock Multiplier
  2386. * in the Capabilities Register.
  2387. */
  2388. if (host->clk_mul)
  2389. host->clk_mul += 1;
  2390. /*
  2391. * Set host parameters.
  2392. */
  2393. mmc->ops = &sdhci_ops;
  2394. mmc->f_max = host->max_clk;
  2395. if (host->ops->get_min_clock)
  2396. mmc->f_min = host->ops->get_min_clock(host);
  2397. else if (host->version >= SDHCI_SPEC_300) {
  2398. if (host->clk_mul) {
  2399. mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
  2400. mmc->f_max = host->max_clk * host->clk_mul;
  2401. } else
  2402. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
  2403. } else
  2404. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
  2405. host->timeout_clk =
  2406. (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
  2407. if (host->timeout_clk == 0) {
  2408. if (host->ops->get_timeout_clock) {
  2409. host->timeout_clk = host->ops->get_timeout_clock(host);
  2410. } else if (!(host->quirks &
  2411. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
  2412. pr_err("%s: Hardware doesn't specify timeout clock "
  2413. "frequency.\n", mmc_hostname(mmc));
  2414. return -ENODEV;
  2415. }
  2416. }
  2417. if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
  2418. host->timeout_clk *= 1000;
  2419. if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
  2420. host->timeout_clk = mmc->f_max / 1000;
  2421. mmc->max_discard_to = (1 << 27) / host->timeout_clk;
  2422. mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
  2423. if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
  2424. host->flags |= SDHCI_AUTO_CMD12;
  2425. /* Auto-CMD23 stuff only works in ADMA or PIO. */
  2426. if ((host->version >= SDHCI_SPEC_300) &&
  2427. ((host->flags & SDHCI_USE_ADMA) ||
  2428. !(host->flags & SDHCI_USE_SDMA))) {
  2429. host->flags |= SDHCI_AUTO_CMD23;
  2430. DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
  2431. } else {
  2432. DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
  2433. }
  2434. /*
  2435. * A controller may support 8-bit width, but the board itself
  2436. * might not have the pins brought out. Boards that support
  2437. * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
  2438. * their platform code before calling sdhci_add_host(), and we
  2439. * won't assume 8-bit width for hosts without that CAP.
  2440. */
  2441. if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
  2442. mmc->caps |= MMC_CAP_4_BIT_DATA;
  2443. if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
  2444. mmc->caps &= ~MMC_CAP_CMD23;
  2445. if (caps[0] & SDHCI_CAN_DO_HISPD)
  2446. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  2447. if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
  2448. !(host->mmc->caps & MMC_CAP_NONREMOVABLE))
  2449. mmc->caps |= MMC_CAP_NEEDS_POLL;
  2450. /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
  2451. host->vqmmc = regulator_get(mmc_dev(mmc), "vqmmc");
  2452. if (IS_ERR_OR_NULL(host->vqmmc)) {
  2453. if (PTR_ERR(host->vqmmc) < 0) {
  2454. pr_info("%s: no vqmmc regulator found\n",
  2455. mmc_hostname(mmc));
  2456. host->vqmmc = NULL;
  2457. }
  2458. } else {
  2459. ret = regulator_enable(host->vqmmc);
  2460. if (!regulator_is_supported_voltage(host->vqmmc, 1700000,
  2461. 1950000))
  2462. caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
  2463. SDHCI_SUPPORT_SDR50 |
  2464. SDHCI_SUPPORT_DDR50);
  2465. if (ret) {
  2466. pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
  2467. mmc_hostname(mmc), ret);
  2468. host->vqmmc = NULL;
  2469. }
  2470. }
  2471. if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
  2472. caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
  2473. SDHCI_SUPPORT_DDR50);
  2474. /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
  2475. if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
  2476. SDHCI_SUPPORT_DDR50))
  2477. mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
  2478. /* SDR104 supports also implies SDR50 support */
  2479. if (caps[1] & SDHCI_SUPPORT_SDR104)
  2480. mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
  2481. else if (caps[1] & SDHCI_SUPPORT_SDR50)
  2482. mmc->caps |= MMC_CAP_UHS_SDR50;
  2483. if (caps[1] & SDHCI_SUPPORT_DDR50)
  2484. mmc->caps |= MMC_CAP_UHS_DDR50;
  2485. /* Does the host need tuning for SDR50? */
  2486. if (caps[1] & SDHCI_USE_SDR50_TUNING)
  2487. host->flags |= SDHCI_SDR50_NEEDS_TUNING;
  2488. /* Does the host need tuning for HS200? */
  2489. if (mmc->caps2 & MMC_CAP2_HS200)
  2490. host->flags |= SDHCI_HS200_NEEDS_TUNING;
  2491. /* Driver Type(s) (A, C, D) supported by the host */
  2492. if (caps[1] & SDHCI_DRIVER_TYPE_A)
  2493. mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
  2494. if (caps[1] & SDHCI_DRIVER_TYPE_C)
  2495. mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
  2496. if (caps[1] & SDHCI_DRIVER_TYPE_D)
  2497. mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
  2498. /* Initial value for re-tuning timer count */
  2499. host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
  2500. SDHCI_RETUNING_TIMER_COUNT_SHIFT;
  2501. /*
  2502. * In case Re-tuning Timer is not disabled, the actual value of
  2503. * re-tuning timer will be 2 ^ (n - 1).
  2504. */
  2505. if (host->tuning_count)
  2506. host->tuning_count = 1 << (host->tuning_count - 1);
  2507. /* Re-tuning mode supported by the Host Controller */
  2508. host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
  2509. SDHCI_RETUNING_MODE_SHIFT;
  2510. ocr_avail = 0;
  2511. host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
  2512. if (IS_ERR_OR_NULL(host->vmmc)) {
  2513. if (PTR_ERR(host->vmmc) < 0) {
  2514. pr_info("%s: no vmmc regulator found\n",
  2515. mmc_hostname(mmc));
  2516. host->vmmc = NULL;
  2517. }
  2518. }
  2519. #ifdef CONFIG_REGULATOR
  2520. /*
  2521. * Voltage range check makes sense only if regulator reports
  2522. * any voltage value.
  2523. */
  2524. if (host->vmmc && regulator_get_voltage(host->vmmc) > 0) {
  2525. ret = regulator_is_supported_voltage(host->vmmc, 2700000,
  2526. 3600000);
  2527. if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_330)))
  2528. caps[0] &= ~SDHCI_CAN_VDD_330;
  2529. if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_300)))
  2530. caps[0] &= ~SDHCI_CAN_VDD_300;
  2531. ret = regulator_is_supported_voltage(host->vmmc, 1700000,
  2532. 1950000);
  2533. if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_180)))
  2534. caps[0] &= ~SDHCI_CAN_VDD_180;
  2535. }
  2536. #endif /* CONFIG_REGULATOR */
  2537. /*
  2538. * According to SD Host Controller spec v3.00, if the Host System
  2539. * can afford more than 150mA, Host Driver should set XPC to 1. Also
  2540. * the value is meaningful only if Voltage Support in the Capabilities
  2541. * register is set. The actual current value is 4 times the register
  2542. * value.
  2543. */
  2544. max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
  2545. if (!max_current_caps && host->vmmc) {
  2546. u32 curr = regulator_get_current_limit(host->vmmc);
  2547. if (curr > 0) {
  2548. /* convert to SDHCI_MAX_CURRENT format */
  2549. curr = curr/1000; /* convert to mA */
  2550. curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
  2551. curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
  2552. max_current_caps =
  2553. (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
  2554. (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
  2555. (curr << SDHCI_MAX_CURRENT_180_SHIFT);
  2556. }
  2557. }
  2558. if (caps[0] & SDHCI_CAN_VDD_330) {
  2559. ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
  2560. mmc->max_current_330 = ((max_current_caps &
  2561. SDHCI_MAX_CURRENT_330_MASK) >>
  2562. SDHCI_MAX_CURRENT_330_SHIFT) *
  2563. SDHCI_MAX_CURRENT_MULTIPLIER;
  2564. }
  2565. if (caps[0] & SDHCI_CAN_VDD_300) {
  2566. ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
  2567. mmc->max_current_300 = ((max_current_caps &
  2568. SDHCI_MAX_CURRENT_300_MASK) >>
  2569. SDHCI_MAX_CURRENT_300_SHIFT) *
  2570. SDHCI_MAX_CURRENT_MULTIPLIER;
  2571. }
  2572. if (caps[0] & SDHCI_CAN_VDD_180) {
  2573. ocr_avail |= MMC_VDD_165_195;
  2574. mmc->max_current_180 = ((max_current_caps &
  2575. SDHCI_MAX_CURRENT_180_MASK) >>
  2576. SDHCI_MAX_CURRENT_180_SHIFT) *
  2577. SDHCI_MAX_CURRENT_MULTIPLIER;
  2578. }
  2579. mmc->ocr_avail = ocr_avail;
  2580. mmc->ocr_avail_sdio = ocr_avail;
  2581. if (host->ocr_avail_sdio)
  2582. mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
  2583. mmc->ocr_avail_sd = ocr_avail;
  2584. if (host->ocr_avail_sd)
  2585. mmc->ocr_avail_sd &= host->ocr_avail_sd;
  2586. else /* normal SD controllers don't support 1.8V */
  2587. mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
  2588. mmc->ocr_avail_mmc = ocr_avail;
  2589. if (host->ocr_avail_mmc)
  2590. mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
  2591. if (mmc->ocr_avail == 0) {
  2592. pr_err("%s: Hardware doesn't report any "
  2593. "support voltages.\n", mmc_hostname(mmc));
  2594. return -ENODEV;
  2595. }
  2596. spin_lock_init(&host->lock);
  2597. /*
  2598. * Maximum number of segments. Depends on if the hardware
  2599. * can do scatter/gather or not.
  2600. */
  2601. if (host->flags & SDHCI_USE_ADMA)
  2602. mmc->max_segs = 128;
  2603. else if (host->flags & SDHCI_USE_SDMA)
  2604. mmc->max_segs = 1;
  2605. else /* PIO */
  2606. mmc->max_segs = 128;
  2607. /*
  2608. * Maximum number of sectors in one transfer. Limited by DMA boundary
  2609. * size (512KiB).
  2610. */
  2611. mmc->max_req_size = 524288;
  2612. /*
  2613. * Maximum segment size. Could be one segment with the maximum number
  2614. * of bytes. When doing hardware scatter/gather, each entry cannot
  2615. * be larger than 64 KiB though.
  2616. */
  2617. if (host->flags & SDHCI_USE_ADMA) {
  2618. if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
  2619. mmc->max_seg_size = 65535;
  2620. else
  2621. mmc->max_seg_size = 65536;
  2622. } else {
  2623. mmc->max_seg_size = mmc->max_req_size;
  2624. }
  2625. /*
  2626. * Maximum block size. This varies from controller to controller and
  2627. * is specified in the capabilities register.
  2628. */
  2629. if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
  2630. mmc->max_blk_size = 2;
  2631. } else {
  2632. mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
  2633. SDHCI_MAX_BLOCK_SHIFT;
  2634. if (mmc->max_blk_size >= 3) {
  2635. pr_warning("%s: Invalid maximum block size, "
  2636. "assuming 512 bytes\n", mmc_hostname(mmc));
  2637. mmc->max_blk_size = 0;
  2638. }
  2639. }
  2640. mmc->max_blk_size = 512 << mmc->max_blk_size;
  2641. /*
  2642. * Maximum block count.
  2643. */
  2644. mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
  2645. /*
  2646. * Init tasklets.
  2647. */
  2648. tasklet_init(&host->card_tasklet,
  2649. sdhci_tasklet_card, (unsigned long)host);
  2650. tasklet_init(&host->finish_tasklet,
  2651. sdhci_tasklet_finish, (unsigned long)host);
  2652. setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
  2653. if (host->version >= SDHCI_SPEC_300) {
  2654. init_waitqueue_head(&host->buf_ready_int);
  2655. /* Initialize re-tuning timer */
  2656. init_timer(&host->tuning_timer);
  2657. host->tuning_timer.data = (unsigned long)host;
  2658. host->tuning_timer.function = sdhci_tuning_timer;
  2659. }
  2660. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  2661. mmc_hostname(mmc), host);
  2662. if (ret) {
  2663. pr_err("%s: Failed to request IRQ %d: %d\n",
  2664. mmc_hostname(mmc), host->irq, ret);
  2665. goto untasklet;
  2666. }
  2667. sdhci_init(host, 0);
  2668. #ifdef CONFIG_MMC_DEBUG
  2669. sdhci_dumpregs(host);
  2670. #endif
  2671. #ifdef SDHCI_USE_LEDS_CLASS
  2672. snprintf(host->led_name, sizeof(host->led_name),
  2673. "%s::", mmc_hostname(mmc));
  2674. host->led.name = host->led_name;
  2675. host->led.brightness = LED_OFF;
  2676. host->led.default_trigger = mmc_hostname(mmc);
  2677. host->led.brightness_set = sdhci_led_control;
  2678. ret = led_classdev_register(mmc_dev(mmc), &host->led);
  2679. if (ret) {
  2680. pr_err("%s: Failed to register LED device: %d\n",
  2681. mmc_hostname(mmc), ret);
  2682. goto reset;
  2683. }
  2684. #endif
  2685. mmiowb();
  2686. mmc_add_host(mmc);
  2687. pr_info("%s: SDHCI controller on %s [%s] using %s\n",
  2688. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  2689. (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
  2690. (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
  2691. sdhci_enable_card_detection(host);
  2692. return 0;
  2693. #ifdef SDHCI_USE_LEDS_CLASS
  2694. reset:
  2695. sdhci_reset(host, SDHCI_RESET_ALL);
  2696. sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
  2697. free_irq(host->irq, host);
  2698. #endif
  2699. untasklet:
  2700. tasklet_kill(&host->card_tasklet);
  2701. tasklet_kill(&host->finish_tasklet);
  2702. return ret;
  2703. }
  2704. EXPORT_SYMBOL_GPL(sdhci_add_host);
  2705. void sdhci_remove_host(struct sdhci_host *host, int dead)
  2706. {
  2707. unsigned long flags;
  2708. if (dead) {
  2709. spin_lock_irqsave(&host->lock, flags);
  2710. host->flags |= SDHCI_DEVICE_DEAD;
  2711. if (host->mrq) {
  2712. pr_err("%s: Controller removed during "
  2713. " transfer!\n", mmc_hostname(host->mmc));
  2714. host->mrq->cmd->error = -ENOMEDIUM;
  2715. tasklet_schedule(&host->finish_tasklet);
  2716. }
  2717. spin_unlock_irqrestore(&host->lock, flags);
  2718. }
  2719. sdhci_disable_card_detection(host);
  2720. mmc_remove_host(host->mmc);
  2721. #ifdef SDHCI_USE_LEDS_CLASS
  2722. led_classdev_unregister(&host->led);
  2723. #endif
  2724. if (!dead)
  2725. sdhci_reset(host, SDHCI_RESET_ALL);
  2726. sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
  2727. free_irq(host->irq, host);
  2728. del_timer_sync(&host->timer);
  2729. tasklet_kill(&host->card_tasklet);
  2730. tasklet_kill(&host->finish_tasklet);
  2731. if (host->vmmc) {
  2732. regulator_disable(host->vmmc);
  2733. regulator_put(host->vmmc);
  2734. }
  2735. if (host->vqmmc) {
  2736. regulator_disable(host->vqmmc);
  2737. regulator_put(host->vqmmc);
  2738. }
  2739. kfree(host->adma_desc);
  2740. kfree(host->align_buffer);
  2741. host->adma_desc = NULL;
  2742. host->align_buffer = NULL;
  2743. }
  2744. EXPORT_SYMBOL_GPL(sdhci_remove_host);
  2745. void sdhci_free_host(struct sdhci_host *host)
  2746. {
  2747. mmc_free_host(host->mmc);
  2748. }
  2749. EXPORT_SYMBOL_GPL(sdhci_free_host);
  2750. /*****************************************************************************\
  2751. * *
  2752. * Driver init/exit *
  2753. * *
  2754. \*****************************************************************************/
  2755. static int __init sdhci_drv_init(void)
  2756. {
  2757. pr_info(DRIVER_NAME
  2758. ": Secure Digital Host Controller Interface driver\n");
  2759. pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  2760. return 0;
  2761. }
  2762. static void __exit sdhci_drv_exit(void)
  2763. {
  2764. }
  2765. module_init(sdhci_drv_init);
  2766. module_exit(sdhci_drv_exit);
  2767. module_param(debug_quirks, uint, 0444);
  2768. module_param(debug_quirks2, uint, 0444);
  2769. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  2770. MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
  2771. MODULE_LICENSE("GPL");
  2772. MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
  2773. MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");