sdhci.c 102 KB

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