sdhci.c 84 KB

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