sdhci.c 78 KB

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