sdhci.c 94 KB

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