sdhci.c 82 KB

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