sdhci.c 87 KB

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