sdhci.c 131 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
  4. *
  5. * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
  6. *
  7. * Thanks to the following companies for their support:
  8. *
  9. * - JMicron (hardware and technical support)
  10. */
  11. #include <linux/bitfield.h>
  12. #include <linux/delay.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/ktime.h>
  15. #include <linux/highmem.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/slab.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/sizes.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/of.h>
  25. #include <linux/bug.h>
  26. #include <linux/leds.h>
  27. #include <linux/mmc/mmc.h>
  28. #include <linux/mmc/host.h>
  29. #include <linux/mmc/card.h>
  30. #include <linux/mmc/sdio.h>
  31. #include <linux/mmc/slot-gpio.h>
  32. #include "sdhci.h"
  33. #define DRIVER_NAME "sdhci"
  34. #define DBG(f, x...) \
  35. pr_debug("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
  36. #define SDHCI_DUMP(f, x...) \
  37. pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
  38. #define MAX_TUNING_LOOP 40
  39. static unsigned int debug_quirks = 0;
  40. static unsigned int debug_quirks2;
  41. static bool sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd);
  42. void sdhci_dumpregs(struct sdhci_host *host)
  43. {
  44. SDHCI_DUMP("============ SDHCI REGISTER DUMP ===========\n");
  45. SDHCI_DUMP("Sys addr: 0x%08x | Version: 0x%08x\n",
  46. sdhci_readl(host, SDHCI_DMA_ADDRESS),
  47. sdhci_readw(host, SDHCI_HOST_VERSION));
  48. SDHCI_DUMP("Blk size: 0x%08x | Blk cnt: 0x%08x\n",
  49. sdhci_readw(host, SDHCI_BLOCK_SIZE),
  50. sdhci_readw(host, SDHCI_BLOCK_COUNT));
  51. SDHCI_DUMP("Argument: 0x%08x | Trn mode: 0x%08x\n",
  52. sdhci_readl(host, SDHCI_ARGUMENT),
  53. sdhci_readw(host, SDHCI_TRANSFER_MODE));
  54. SDHCI_DUMP("Present: 0x%08x | Host ctl: 0x%08x\n",
  55. sdhci_readl(host, SDHCI_PRESENT_STATE),
  56. sdhci_readb(host, SDHCI_HOST_CONTROL));
  57. SDHCI_DUMP("Power: 0x%08x | Blk gap: 0x%08x\n",
  58. sdhci_readb(host, SDHCI_POWER_CONTROL),
  59. sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
  60. SDHCI_DUMP("Wake-up: 0x%08x | Clock: 0x%08x\n",
  61. sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
  62. sdhci_readw(host, SDHCI_CLOCK_CONTROL));
  63. SDHCI_DUMP("Timeout: 0x%08x | Int stat: 0x%08x\n",
  64. sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
  65. sdhci_readl(host, SDHCI_INT_STATUS));
  66. SDHCI_DUMP("Int enab: 0x%08x | Sig enab: 0x%08x\n",
  67. sdhci_readl(host, SDHCI_INT_ENABLE),
  68. sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
  69. SDHCI_DUMP("ACmd stat: 0x%08x | Slot int: 0x%08x\n",
  70. sdhci_readw(host, SDHCI_AUTO_CMD_STATUS),
  71. sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
  72. SDHCI_DUMP("Caps: 0x%08x | Caps_1: 0x%08x\n",
  73. sdhci_readl(host, SDHCI_CAPABILITIES),
  74. sdhci_readl(host, SDHCI_CAPABILITIES_1));
  75. SDHCI_DUMP("Cmd: 0x%08x | Max curr: 0x%08x\n",
  76. sdhci_readw(host, SDHCI_COMMAND),
  77. sdhci_readl(host, SDHCI_MAX_CURRENT));
  78. SDHCI_DUMP("Resp[0]: 0x%08x | Resp[1]: 0x%08x\n",
  79. sdhci_readl(host, SDHCI_RESPONSE),
  80. sdhci_readl(host, SDHCI_RESPONSE + 4));
  81. SDHCI_DUMP("Resp[2]: 0x%08x | Resp[3]: 0x%08x\n",
  82. sdhci_readl(host, SDHCI_RESPONSE + 8),
  83. sdhci_readl(host, SDHCI_RESPONSE + 12));
  84. SDHCI_DUMP("Host ctl2: 0x%08x\n",
  85. sdhci_readw(host, SDHCI_HOST_CONTROL2));
  86. if (host->flags & SDHCI_USE_ADMA) {
  87. if (host->flags & SDHCI_USE_64_BIT_DMA) {
  88. SDHCI_DUMP("ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
  89. sdhci_readl(host, SDHCI_ADMA_ERROR),
  90. sdhci_readl(host, SDHCI_ADMA_ADDRESS_HI),
  91. sdhci_readl(host, SDHCI_ADMA_ADDRESS));
  92. } else {
  93. SDHCI_DUMP("ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
  94. sdhci_readl(host, SDHCI_ADMA_ERROR),
  95. sdhci_readl(host, SDHCI_ADMA_ADDRESS));
  96. }
  97. }
  98. if (host->ops->dump_uhs2_regs)
  99. host->ops->dump_uhs2_regs(host);
  100. if (host->ops->dump_vendor_regs)
  101. host->ops->dump_vendor_regs(host);
  102. SDHCI_DUMP("============================================\n");
  103. }
  104. EXPORT_SYMBOL_GPL(sdhci_dumpregs);
  105. /*****************************************************************************\
  106. * *
  107. * Low level functions *
  108. * *
  109. \*****************************************************************************/
  110. static void sdhci_do_enable_v4_mode(struct sdhci_host *host)
  111. {
  112. u16 ctrl2;
  113. ctrl2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  114. if (ctrl2 & SDHCI_CTRL_V4_MODE)
  115. return;
  116. ctrl2 |= SDHCI_CTRL_V4_MODE;
  117. sdhci_writew(host, ctrl2, SDHCI_HOST_CONTROL2);
  118. }
  119. /*
  120. * This can be called before sdhci_add_host() by Vendor's host controller
  121. * driver to enable v4 mode if supported.
  122. */
  123. void sdhci_enable_v4_mode(struct sdhci_host *host)
  124. {
  125. host->v4_mode = true;
  126. sdhci_do_enable_v4_mode(host);
  127. }
  128. EXPORT_SYMBOL_GPL(sdhci_enable_v4_mode);
  129. bool sdhci_data_line_cmd(struct mmc_command *cmd)
  130. {
  131. return cmd->data || cmd->flags & MMC_RSP_BUSY;
  132. }
  133. EXPORT_SYMBOL_GPL(sdhci_data_line_cmd);
  134. static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
  135. {
  136. u32 present;
  137. if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
  138. !mmc_card_is_removable(host->mmc) || mmc_host_can_gpio_cd(host->mmc))
  139. return;
  140. if (enable) {
  141. present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  142. SDHCI_CARD_PRESENT;
  143. host->ier |= present ? SDHCI_INT_CARD_REMOVE :
  144. SDHCI_INT_CARD_INSERT;
  145. } else {
  146. host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
  147. }
  148. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  149. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  150. }
  151. static void sdhci_enable_card_detection(struct sdhci_host *host)
  152. {
  153. sdhci_set_card_detection(host, true);
  154. }
  155. static void sdhci_disable_card_detection(struct sdhci_host *host)
  156. {
  157. sdhci_set_card_detection(host, false);
  158. }
  159. static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
  160. {
  161. if (host->bus_on)
  162. return;
  163. host->bus_on = true;
  164. pm_runtime_get_noresume(mmc_dev(host->mmc));
  165. }
  166. static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
  167. {
  168. if (!host->bus_on)
  169. return;
  170. host->bus_on = false;
  171. pm_runtime_put_noidle(mmc_dev(host->mmc));
  172. }
  173. void sdhci_reset(struct sdhci_host *host, u8 mask)
  174. {
  175. ktime_t timeout;
  176. sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
  177. if (mask & SDHCI_RESET_ALL) {
  178. host->clock = 0;
  179. /* Reset-all turns off SD Bus Power */
  180. if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
  181. sdhci_runtime_pm_bus_off(host);
  182. }
  183. /* Wait max 100 ms */
  184. timeout = ktime_add_ms(ktime_get(), 100);
  185. /* hw clears the bit when it's done */
  186. while (1) {
  187. bool timedout = ktime_after(ktime_get(), timeout);
  188. if (!(sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask))
  189. break;
  190. if (timedout) {
  191. pr_err("%s: Reset 0x%x never completed.\n",
  192. mmc_hostname(host->mmc), (int)mask);
  193. sdhci_err_stats_inc(host, CTRL_TIMEOUT);
  194. sdhci_dumpregs(host);
  195. return;
  196. }
  197. udelay(10);
  198. }
  199. }
  200. EXPORT_SYMBOL_GPL(sdhci_reset);
  201. bool sdhci_do_reset(struct sdhci_host *host, u8 mask)
  202. {
  203. if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
  204. struct mmc_host *mmc = host->mmc;
  205. if (!mmc->ops->get_cd(mmc))
  206. return false;
  207. }
  208. host->ops->reset(host, mask);
  209. return true;
  210. }
  211. EXPORT_SYMBOL_GPL(sdhci_do_reset);
  212. static void sdhci_reset_for_all(struct sdhci_host *host)
  213. {
  214. if (sdhci_do_reset(host, SDHCI_RESET_ALL)) {
  215. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  216. if (host->ops->enable_dma)
  217. host->ops->enable_dma(host);
  218. }
  219. /* Resetting the controller clears many */
  220. host->preset_enabled = false;
  221. }
  222. }
  223. enum sdhci_reset_reason {
  224. SDHCI_RESET_FOR_INIT,
  225. SDHCI_RESET_FOR_REQUEST_ERROR,
  226. SDHCI_RESET_FOR_REQUEST_ERROR_DATA_ONLY,
  227. SDHCI_RESET_FOR_TUNING_ABORT,
  228. SDHCI_RESET_FOR_CARD_REMOVED,
  229. SDHCI_RESET_FOR_CQE_RECOVERY,
  230. };
  231. static void sdhci_reset_for_reason(struct sdhci_host *host, enum sdhci_reset_reason reason)
  232. {
  233. if (host->quirks2 & SDHCI_QUIRK2_ISSUE_CMD_DAT_RESET_TOGETHER) {
  234. sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  235. return;
  236. }
  237. switch (reason) {
  238. case SDHCI_RESET_FOR_INIT:
  239. sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  240. break;
  241. case SDHCI_RESET_FOR_REQUEST_ERROR:
  242. case SDHCI_RESET_FOR_TUNING_ABORT:
  243. case SDHCI_RESET_FOR_CARD_REMOVED:
  244. case SDHCI_RESET_FOR_CQE_RECOVERY:
  245. sdhci_do_reset(host, SDHCI_RESET_CMD);
  246. sdhci_do_reset(host, SDHCI_RESET_DATA);
  247. break;
  248. case SDHCI_RESET_FOR_REQUEST_ERROR_DATA_ONLY:
  249. sdhci_do_reset(host, SDHCI_RESET_DATA);
  250. break;
  251. }
  252. }
  253. #define sdhci_reset_for(h, r) sdhci_reset_for_reason((h), SDHCI_RESET_FOR_##r)
  254. static void sdhci_set_default_irqs(struct sdhci_host *host)
  255. {
  256. host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
  257. SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
  258. SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
  259. SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
  260. SDHCI_INT_RESPONSE;
  261. if (host->tuning_mode == SDHCI_TUNING_MODE_2 ||
  262. host->tuning_mode == SDHCI_TUNING_MODE_3)
  263. host->ier |= SDHCI_INT_RETUNE;
  264. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  265. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  266. }
  267. static void sdhci_config_dma(struct sdhci_host *host)
  268. {
  269. u8 ctrl;
  270. u16 ctrl2;
  271. if (host->version < SDHCI_SPEC_200)
  272. return;
  273. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  274. /*
  275. * Always adjust the DMA selection as some controllers
  276. * (e.g. JMicron) can't do PIO properly when the selection
  277. * is ADMA.
  278. */
  279. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  280. if (!(host->flags & SDHCI_REQ_USE_DMA))
  281. goto out;
  282. /* Note if DMA Select is zero then SDMA is selected */
  283. if (host->flags & SDHCI_USE_ADMA)
  284. ctrl |= SDHCI_CTRL_ADMA32;
  285. if (host->flags & SDHCI_USE_64_BIT_DMA) {
  286. /*
  287. * If v4 mode, all supported DMA can be 64-bit addressing if
  288. * controller supports 64-bit system address, otherwise only
  289. * ADMA can support 64-bit addressing.
  290. */
  291. if (host->v4_mode) {
  292. ctrl2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  293. ctrl2 |= SDHCI_CTRL_64BIT_ADDR;
  294. sdhci_writew(host, ctrl2, SDHCI_HOST_CONTROL2);
  295. } else if (host->flags & SDHCI_USE_ADMA) {
  296. /*
  297. * Don't need to undo SDHCI_CTRL_ADMA32 in order to
  298. * set SDHCI_CTRL_ADMA64.
  299. */
  300. ctrl |= SDHCI_CTRL_ADMA64;
  301. }
  302. }
  303. out:
  304. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  305. }
  306. static void sdhci_init(struct sdhci_host *host, int soft)
  307. {
  308. struct mmc_host *mmc = host->mmc;
  309. unsigned long flags;
  310. if (soft)
  311. sdhci_reset_for(host, INIT);
  312. else
  313. sdhci_reset_for_all(host);
  314. if (host->v4_mode)
  315. sdhci_do_enable_v4_mode(host);
  316. spin_lock_irqsave(&host->lock, flags);
  317. sdhci_set_default_irqs(host);
  318. spin_unlock_irqrestore(&host->lock, flags);
  319. host->cqe_on = false;
  320. if (soft) {
  321. /* force clock reconfiguration */
  322. host->clock = 0;
  323. host->reinit_uhs = true;
  324. mmc->ops->set_ios(mmc, &mmc->ios);
  325. }
  326. }
  327. static void sdhci_reinit(struct sdhci_host *host)
  328. {
  329. u32 cd = host->ier & (SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
  330. sdhci_init(host, 0);
  331. sdhci_enable_card_detection(host);
  332. /*
  333. * A change to the card detect bits indicates a change in present state,
  334. * refer sdhci_set_card_detection(). A card detect interrupt might have
  335. * been missed while the host controller was being reset, so trigger a
  336. * rescan to check.
  337. */
  338. if (cd != (host->ier & (SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT)))
  339. mmc_detect_change(host->mmc, msecs_to_jiffies(200));
  340. }
  341. static void __sdhci_led_activate(struct sdhci_host *host)
  342. {
  343. u8 ctrl;
  344. if (host->quirks & SDHCI_QUIRK_NO_LED)
  345. return;
  346. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  347. ctrl |= SDHCI_CTRL_LED;
  348. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  349. }
  350. static void __sdhci_led_deactivate(struct sdhci_host *host)
  351. {
  352. u8 ctrl;
  353. if (host->quirks & SDHCI_QUIRK_NO_LED)
  354. return;
  355. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  356. ctrl &= ~SDHCI_CTRL_LED;
  357. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  358. }
  359. #if IS_REACHABLE(CONFIG_LEDS_CLASS)
  360. static void sdhci_led_control(struct led_classdev *led,
  361. enum led_brightness brightness)
  362. {
  363. struct sdhci_host *host = container_of(led, struct sdhci_host, led);
  364. unsigned long flags;
  365. spin_lock_irqsave(&host->lock, flags);
  366. if (host->runtime_suspended)
  367. goto out;
  368. if (brightness == LED_OFF)
  369. __sdhci_led_deactivate(host);
  370. else
  371. __sdhci_led_activate(host);
  372. out:
  373. spin_unlock_irqrestore(&host->lock, flags);
  374. }
  375. static int sdhci_led_register(struct sdhci_host *host)
  376. {
  377. struct mmc_host *mmc = host->mmc;
  378. if (host->quirks & SDHCI_QUIRK_NO_LED)
  379. return 0;
  380. snprintf(host->led_name, sizeof(host->led_name),
  381. "%s::", mmc_hostname(mmc));
  382. host->led.name = host->led_name;
  383. host->led.brightness = LED_OFF;
  384. host->led.default_trigger = mmc_hostname(mmc);
  385. host->led.brightness_set = sdhci_led_control;
  386. return led_classdev_register(mmc_dev(mmc), &host->led);
  387. }
  388. static void sdhci_led_unregister(struct sdhci_host *host)
  389. {
  390. if (host->quirks & SDHCI_QUIRK_NO_LED)
  391. return;
  392. led_classdev_unregister(&host->led);
  393. }
  394. static inline void sdhci_led_activate(struct sdhci_host *host)
  395. {
  396. }
  397. static inline void sdhci_led_deactivate(struct sdhci_host *host)
  398. {
  399. }
  400. #else
  401. static inline int sdhci_led_register(struct sdhci_host *host)
  402. {
  403. return 0;
  404. }
  405. static inline void sdhci_led_unregister(struct sdhci_host *host)
  406. {
  407. }
  408. static inline void sdhci_led_activate(struct sdhci_host *host)
  409. {
  410. __sdhci_led_activate(host);
  411. }
  412. static inline void sdhci_led_deactivate(struct sdhci_host *host)
  413. {
  414. __sdhci_led_deactivate(host);
  415. }
  416. #endif
  417. void sdhci_mod_timer(struct sdhci_host *host, struct mmc_request *mrq,
  418. unsigned long timeout)
  419. {
  420. if (sdhci_data_line_cmd(mrq->cmd))
  421. mod_timer(&host->data_timer, timeout);
  422. else
  423. mod_timer(&host->timer, timeout);
  424. }
  425. EXPORT_SYMBOL_GPL(sdhci_mod_timer);
  426. static void sdhci_del_timer(struct sdhci_host *host, struct mmc_request *mrq)
  427. {
  428. if (sdhci_data_line_cmd(mrq->cmd))
  429. timer_delete(&host->data_timer);
  430. else
  431. timer_delete(&host->timer);
  432. }
  433. static inline bool sdhci_has_requests(struct sdhci_host *host)
  434. {
  435. return host->cmd || host->data_cmd;
  436. }
  437. /*****************************************************************************\
  438. * *
  439. * Core functions *
  440. * *
  441. \*****************************************************************************/
  442. static void sdhci_read_block_pio(struct sdhci_host *host)
  443. {
  444. size_t blksize, len, chunk;
  445. u32 scratch;
  446. u8 *buf;
  447. DBG("PIO reading\n");
  448. blksize = host->data->blksz;
  449. chunk = 0;
  450. while (blksize) {
  451. BUG_ON(!sg_miter_next(&host->sg_miter));
  452. len = min(host->sg_miter.length, blksize);
  453. blksize -= len;
  454. host->sg_miter.consumed = len;
  455. buf = host->sg_miter.addr;
  456. while (len) {
  457. if (chunk == 0) {
  458. scratch = sdhci_readl(host, SDHCI_BUFFER);
  459. chunk = 4;
  460. }
  461. *buf = scratch & 0xFF;
  462. buf++;
  463. scratch >>= 8;
  464. chunk--;
  465. len--;
  466. }
  467. }
  468. sg_miter_stop(&host->sg_miter);
  469. }
  470. static void sdhci_write_block_pio(struct sdhci_host *host)
  471. {
  472. size_t blksize, len, chunk;
  473. u32 scratch;
  474. u8 *buf;
  475. DBG("PIO writing\n");
  476. blksize = host->data->blksz;
  477. chunk = 0;
  478. scratch = 0;
  479. while (blksize) {
  480. BUG_ON(!sg_miter_next(&host->sg_miter));
  481. len = min(host->sg_miter.length, blksize);
  482. blksize -= len;
  483. host->sg_miter.consumed = len;
  484. buf = host->sg_miter.addr;
  485. while (len) {
  486. scratch |= (u32)*buf << (chunk * 8);
  487. buf++;
  488. chunk++;
  489. len--;
  490. if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
  491. sdhci_writel(host, scratch, SDHCI_BUFFER);
  492. chunk = 0;
  493. scratch = 0;
  494. }
  495. }
  496. }
  497. sg_miter_stop(&host->sg_miter);
  498. }
  499. static void sdhci_transfer_pio(struct sdhci_host *host)
  500. {
  501. u32 mask;
  502. if (host->blocks == 0)
  503. return;
  504. if (host->data->flags & MMC_DATA_READ)
  505. mask = SDHCI_DATA_AVAILABLE;
  506. else
  507. mask = SDHCI_SPACE_AVAILABLE;
  508. /*
  509. * Some controllers (JMicron JMB38x) mess up the buffer bits
  510. * for transfers < 4 bytes. As long as it is just one block,
  511. * we can ignore the bits.
  512. */
  513. if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
  514. (host->data->blocks == 1))
  515. mask = ~0;
  516. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  517. if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
  518. udelay(100);
  519. if (host->data->flags & MMC_DATA_READ)
  520. sdhci_read_block_pio(host);
  521. else
  522. sdhci_write_block_pio(host);
  523. host->blocks--;
  524. if (host->blocks == 0)
  525. break;
  526. }
  527. DBG("PIO transfer complete.\n");
  528. }
  529. static int sdhci_pre_dma_transfer(struct sdhci_host *host,
  530. struct mmc_data *data, int cookie)
  531. {
  532. int sg_count;
  533. /*
  534. * If the data buffers are already mapped, return the previous
  535. * dma_map_sg() result.
  536. */
  537. if (data->host_cookie == COOKIE_PRE_MAPPED)
  538. return data->sg_count;
  539. /* Bounce write requests to the bounce buffer */
  540. if (host->bounce_buffer) {
  541. unsigned int length = data->blksz * data->blocks;
  542. if (length > host->bounce_buffer_size) {
  543. pr_err("%s: asked for transfer of %u bytes exceeds bounce buffer %u bytes\n",
  544. mmc_hostname(host->mmc), length,
  545. host->bounce_buffer_size);
  546. return -EIO;
  547. }
  548. if (mmc_get_dma_dir(data) == DMA_TO_DEVICE) {
  549. /* Copy the data to the bounce buffer */
  550. if (host->ops->copy_to_bounce_buffer) {
  551. host->ops->copy_to_bounce_buffer(host,
  552. data, length);
  553. } else {
  554. sg_copy_to_buffer(data->sg, data->sg_len,
  555. host->bounce_buffer, length);
  556. }
  557. }
  558. /* Switch ownership to the DMA */
  559. dma_sync_single_for_device(mmc_dev(host->mmc),
  560. host->bounce_addr,
  561. host->bounce_buffer_size,
  562. mmc_get_dma_dir(data));
  563. /* Just a dummy value */
  564. sg_count = 1;
  565. } else {
  566. /* Just access the data directly from memory */
  567. sg_count = dma_map_sg(mmc_dev(host->mmc),
  568. data->sg, data->sg_len,
  569. mmc_get_dma_dir(data));
  570. }
  571. if (sg_count == 0)
  572. return -ENOSPC;
  573. data->sg_count = sg_count;
  574. data->host_cookie = cookie;
  575. return sg_count;
  576. }
  577. static char *sdhci_kmap_atomic(struct scatterlist *sg)
  578. {
  579. return kmap_local_page(sg_page(sg)) + sg->offset;
  580. }
  581. static void sdhci_kunmap_atomic(void *buffer)
  582. {
  583. kunmap_local(buffer);
  584. }
  585. void sdhci_adma_write_desc(struct sdhci_host *host, void **desc,
  586. dma_addr_t addr, int len, unsigned int cmd)
  587. {
  588. struct sdhci_adma2_64_desc *dma_desc = *desc;
  589. /* 32-bit and 64-bit descriptors have these members in same position */
  590. dma_desc->cmd = cpu_to_le16(cmd);
  591. dma_desc->len = cpu_to_le16(len);
  592. dma_desc->addr_lo = cpu_to_le32(lower_32_bits(addr));
  593. if (host->flags & SDHCI_USE_64_BIT_DMA)
  594. dma_desc->addr_hi = cpu_to_le32(upper_32_bits(addr));
  595. *desc += host->desc_sz;
  596. }
  597. EXPORT_SYMBOL_GPL(sdhci_adma_write_desc);
  598. static inline void __sdhci_adma_write_desc(struct sdhci_host *host,
  599. void **desc, dma_addr_t addr,
  600. int len, unsigned int cmd)
  601. {
  602. if (host->ops->adma_write_desc)
  603. host->ops->adma_write_desc(host, desc, addr, len, cmd);
  604. else
  605. sdhci_adma_write_desc(host, desc, addr, len, cmd);
  606. }
  607. static void sdhci_adma_mark_end(void *desc)
  608. {
  609. struct sdhci_adma2_64_desc *dma_desc = desc;
  610. /* 32-bit and 64-bit descriptors have 'cmd' in same position */
  611. dma_desc->cmd |= cpu_to_le16(ADMA2_END);
  612. }
  613. static void sdhci_adma_table_pre(struct sdhci_host *host,
  614. struct mmc_data *data, int sg_count)
  615. {
  616. struct scatterlist *sg;
  617. dma_addr_t addr, align_addr;
  618. void *desc, *align;
  619. char *buffer;
  620. int len, offset, i;
  621. /*
  622. * The spec does not specify endianness of descriptor table.
  623. * We currently guess that it is LE.
  624. */
  625. host->sg_count = sg_count;
  626. desc = host->adma_table;
  627. align = host->align_buffer;
  628. align_addr = host->align_addr;
  629. for_each_sg(data->sg, sg, host->sg_count, i) {
  630. addr = sg_dma_address(sg);
  631. len = sg_dma_len(sg);
  632. /*
  633. * The SDHCI specification states that ADMA addresses must
  634. * be 32-bit aligned. If they aren't, then we use a bounce
  635. * buffer for the (up to three) bytes that screw up the
  636. * alignment.
  637. */
  638. offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
  639. SDHCI_ADMA2_MASK;
  640. if (offset) {
  641. if (data->flags & MMC_DATA_WRITE) {
  642. buffer = sdhci_kmap_atomic(sg);
  643. memcpy(align, buffer, offset);
  644. sdhci_kunmap_atomic(buffer);
  645. }
  646. /* tran, valid */
  647. __sdhci_adma_write_desc(host, &desc, align_addr,
  648. offset, ADMA2_TRAN_VALID);
  649. BUG_ON(offset > 65536);
  650. align += SDHCI_ADMA2_ALIGN;
  651. align_addr += SDHCI_ADMA2_ALIGN;
  652. addr += offset;
  653. len -= offset;
  654. }
  655. /*
  656. * The block layer forces a minimum segment size of PAGE_SIZE,
  657. * so 'len' can be too big here if PAGE_SIZE >= 64KiB. Write
  658. * multiple descriptors, noting that the ADMA table is sized
  659. * for 4KiB chunks anyway, so it will be big enough.
  660. */
  661. while (len > host->max_adma) {
  662. int n = 32 * 1024; /* 32KiB*/
  663. __sdhci_adma_write_desc(host, &desc, addr, n, ADMA2_TRAN_VALID);
  664. addr += n;
  665. len -= n;
  666. }
  667. /* tran, valid */
  668. if (len)
  669. __sdhci_adma_write_desc(host, &desc, addr, len,
  670. ADMA2_TRAN_VALID);
  671. /*
  672. * If this triggers then we have a calculation bug
  673. * somewhere. :/
  674. */
  675. WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
  676. }
  677. if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
  678. /* Mark the last descriptor as the terminating descriptor */
  679. if (desc != host->adma_table) {
  680. desc -= host->desc_sz;
  681. sdhci_adma_mark_end(desc);
  682. }
  683. } else {
  684. /* Add a terminating entry - nop, end, valid */
  685. __sdhci_adma_write_desc(host, &desc, 0, 0, ADMA2_NOP_END_VALID);
  686. }
  687. }
  688. static void sdhci_adma_table_post(struct sdhci_host *host,
  689. struct mmc_data *data)
  690. {
  691. struct scatterlist *sg;
  692. int i, size;
  693. void *align;
  694. char *buffer;
  695. if (data->flags & MMC_DATA_READ) {
  696. bool has_unaligned = false;
  697. /* Do a quick scan of the SG list for any unaligned mappings */
  698. for_each_sg(data->sg, sg, host->sg_count, i)
  699. if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
  700. has_unaligned = true;
  701. break;
  702. }
  703. if (has_unaligned) {
  704. dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
  705. data->sg_len, DMA_FROM_DEVICE);
  706. align = host->align_buffer;
  707. for_each_sg(data->sg, sg, host->sg_count, i) {
  708. if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
  709. size = SDHCI_ADMA2_ALIGN -
  710. (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
  711. buffer = sdhci_kmap_atomic(sg);
  712. memcpy(buffer, align, size);
  713. sdhci_kunmap_atomic(buffer);
  714. align += SDHCI_ADMA2_ALIGN;
  715. }
  716. }
  717. }
  718. }
  719. }
  720. static void sdhci_set_adma_addr(struct sdhci_host *host, dma_addr_t addr)
  721. {
  722. sdhci_writel(host, lower_32_bits(addr), SDHCI_ADMA_ADDRESS);
  723. if (host->flags & SDHCI_USE_64_BIT_DMA)
  724. sdhci_writel(host, upper_32_bits(addr), SDHCI_ADMA_ADDRESS_HI);
  725. }
  726. static dma_addr_t sdhci_sdma_address(struct sdhci_host *host)
  727. {
  728. if (host->bounce_buffer)
  729. return host->bounce_addr;
  730. else
  731. return sg_dma_address(host->data->sg);
  732. }
  733. static void sdhci_set_sdma_addr(struct sdhci_host *host, dma_addr_t addr)
  734. {
  735. if (host->v4_mode)
  736. sdhci_set_adma_addr(host, addr);
  737. else
  738. sdhci_writel(host, addr, SDHCI_DMA_ADDRESS);
  739. }
  740. static unsigned int sdhci_target_timeout(struct sdhci_host *host,
  741. struct mmc_command *cmd,
  742. struct mmc_data *data)
  743. {
  744. unsigned int target_timeout;
  745. /* timeout in us */
  746. if (!data) {
  747. target_timeout = cmd->busy_timeout * 1000;
  748. } else {
  749. target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
  750. if (host->clock && data->timeout_clks) {
  751. unsigned long long val;
  752. /*
  753. * data->timeout_clks is in units of clock cycles.
  754. * host->clock is in Hz. target_timeout is in us.
  755. * Hence, us = 1000000 * cycles / Hz. Round up.
  756. */
  757. val = 1000000ULL * data->timeout_clks;
  758. if (do_div(val, host->clock))
  759. target_timeout++;
  760. target_timeout += val;
  761. }
  762. }
  763. return target_timeout;
  764. }
  765. static void sdhci_calc_sw_timeout(struct sdhci_host *host,
  766. struct mmc_command *cmd)
  767. {
  768. struct mmc_data *data = cmd->data;
  769. struct mmc_host *mmc = host->mmc;
  770. struct mmc_ios *ios = &mmc->ios;
  771. unsigned char bus_width = 1 << ios->bus_width;
  772. unsigned int blksz;
  773. unsigned int freq;
  774. u64 target_timeout;
  775. u64 transfer_time;
  776. target_timeout = sdhci_target_timeout(host, cmd, data);
  777. target_timeout *= NSEC_PER_USEC;
  778. if (data) {
  779. blksz = data->blksz;
  780. freq = mmc->actual_clock ? : host->clock;
  781. transfer_time = (u64)blksz * NSEC_PER_SEC * (8 / bus_width);
  782. do_div(transfer_time, freq);
  783. /* multiply by '2' to account for any unknowns */
  784. transfer_time = transfer_time * 2;
  785. /* calculate timeout for the entire data */
  786. host->data_timeout = data->blocks * target_timeout +
  787. transfer_time;
  788. } else {
  789. host->data_timeout = target_timeout;
  790. }
  791. if (host->data_timeout)
  792. host->data_timeout += MMC_CMD_TRANSFER_TIME;
  793. }
  794. static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd,
  795. bool *too_big)
  796. {
  797. u8 count;
  798. struct mmc_data *data;
  799. unsigned target_timeout, current_timeout;
  800. *too_big = false;
  801. /*
  802. * If the host controller provides us with an incorrect timeout
  803. * value, just skip the check and use the maximum. The hardware may take
  804. * longer to time out, but that's much better than having a too-short
  805. * timeout value.
  806. */
  807. if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
  808. return host->max_timeout_count;
  809. /* Unspecified command, assume max */
  810. if (cmd == NULL)
  811. return host->max_timeout_count;
  812. data = cmd->data;
  813. /* Unspecified timeout, assume max */
  814. if (!data && !cmd->busy_timeout)
  815. return host->max_timeout_count;
  816. /* timeout in us */
  817. target_timeout = sdhci_target_timeout(host, cmd, data);
  818. /*
  819. * Figure out needed cycles.
  820. * We do this in steps in order to fit inside a 32 bit int.
  821. * The first step is the minimum timeout, which will have a
  822. * minimum resolution of 6 bits:
  823. * (1) 2^13*1000 > 2^22,
  824. * (2) host->timeout_clk < 2^16
  825. * =>
  826. * (1) / (2) > 2^6
  827. */
  828. count = 0;
  829. current_timeout = (1 << 13) * 1000 / host->timeout_clk;
  830. while (current_timeout < target_timeout) {
  831. count++;
  832. current_timeout <<= 1;
  833. if (count > host->max_timeout_count) {
  834. if (!(host->quirks2 & SDHCI_QUIRK2_DISABLE_HW_TIMEOUT))
  835. DBG("Too large timeout 0x%x requested for CMD%d!\n",
  836. count, cmd->opcode);
  837. count = host->max_timeout_count;
  838. *too_big = true;
  839. break;
  840. }
  841. }
  842. return count;
  843. }
  844. static void sdhci_set_transfer_irqs(struct sdhci_host *host)
  845. {
  846. u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  847. u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  848. if (host->flags & SDHCI_REQ_USE_DMA)
  849. host->ier = (host->ier & ~pio_irqs) | dma_irqs;
  850. else
  851. host->ier = (host->ier & ~dma_irqs) | pio_irqs;
  852. if (host->flags & (SDHCI_AUTO_CMD23 | SDHCI_AUTO_CMD12))
  853. host->ier |= SDHCI_INT_AUTO_CMD_ERR;
  854. else
  855. host->ier &= ~SDHCI_INT_AUTO_CMD_ERR;
  856. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  857. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  858. }
  859. void sdhci_set_data_timeout_irq(struct sdhci_host *host, bool enable)
  860. {
  861. if (enable)
  862. host->ier |= SDHCI_INT_DATA_TIMEOUT;
  863. else
  864. host->ier &= ~SDHCI_INT_DATA_TIMEOUT;
  865. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  866. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  867. }
  868. EXPORT_SYMBOL_GPL(sdhci_set_data_timeout_irq);
  869. void __sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
  870. {
  871. bool too_big = false;
  872. u8 count = sdhci_calc_timeout(host, cmd, &too_big);
  873. if (too_big &&
  874. host->quirks2 & SDHCI_QUIRK2_DISABLE_HW_TIMEOUT) {
  875. sdhci_calc_sw_timeout(host, cmd);
  876. sdhci_set_data_timeout_irq(host, false);
  877. } else if (!(host->ier & SDHCI_INT_DATA_TIMEOUT)) {
  878. sdhci_set_data_timeout_irq(host, true);
  879. }
  880. sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
  881. }
  882. EXPORT_SYMBOL_GPL(__sdhci_set_timeout);
  883. static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
  884. {
  885. if (host->ops->set_timeout)
  886. host->ops->set_timeout(host, cmd);
  887. else
  888. __sdhci_set_timeout(host, cmd);
  889. }
  890. void sdhci_initialize_data(struct sdhci_host *host, struct mmc_data *data)
  891. {
  892. WARN_ON(host->data);
  893. /* Sanity checks */
  894. BUG_ON(data->blksz * data->blocks > 524288);
  895. BUG_ON(data->blksz > host->mmc->max_blk_size);
  896. BUG_ON(data->blocks > 65535);
  897. host->data = data;
  898. host->data_early = 0;
  899. host->data->bytes_xfered = 0;
  900. }
  901. EXPORT_SYMBOL_GPL(sdhci_initialize_data);
  902. static inline void sdhci_set_block_info(struct sdhci_host *host,
  903. struct mmc_data *data)
  904. {
  905. /* Set the DMA boundary value and block size */
  906. sdhci_writew(host,
  907. SDHCI_MAKE_BLKSZ(host->sdma_boundary, data->blksz),
  908. SDHCI_BLOCK_SIZE);
  909. /*
  910. * For Version 4.10 onwards, if v4 mode is enabled, 32-bit Block Count
  911. * can be supported, in that case 16-bit block count register must be 0.
  912. */
  913. if (host->version >= SDHCI_SPEC_410 && host->v4_mode &&
  914. (host->quirks2 & SDHCI_QUIRK2_USE_32BIT_BLK_CNT)) {
  915. if (sdhci_readw(host, SDHCI_BLOCK_COUNT))
  916. sdhci_writew(host, 0, SDHCI_BLOCK_COUNT);
  917. sdhci_writew(host, data->blocks, SDHCI_32BIT_BLK_CNT);
  918. } else {
  919. sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  920. }
  921. }
  922. void sdhci_prepare_dma(struct sdhci_host *host, struct mmc_data *data)
  923. {
  924. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  925. struct scatterlist *sg;
  926. unsigned int length_mask, offset_mask;
  927. int i;
  928. host->flags |= SDHCI_REQ_USE_DMA;
  929. /*
  930. * FIXME: This doesn't account for merging when mapping the
  931. * scatterlist.
  932. *
  933. * The assumption here being that alignment and lengths are
  934. * the same after DMA mapping to device address space.
  935. */
  936. length_mask = 0;
  937. offset_mask = 0;
  938. if (host->flags & SDHCI_USE_ADMA) {
  939. if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
  940. length_mask = 3;
  941. /*
  942. * As we use up to 3 byte chunks to work
  943. * around alignment problems, we need to
  944. * check the offset as well.
  945. */
  946. offset_mask = 3;
  947. }
  948. } else {
  949. if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
  950. length_mask = 3;
  951. if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
  952. offset_mask = 3;
  953. }
  954. if (unlikely(length_mask | offset_mask)) {
  955. for_each_sg(data->sg, sg, data->sg_len, i) {
  956. if (sg->length & length_mask) {
  957. DBG("Reverting to PIO because of transfer size (%d)\n",
  958. sg->length);
  959. host->flags &= ~SDHCI_REQ_USE_DMA;
  960. break;
  961. }
  962. if (sg->offset & offset_mask) {
  963. DBG("Reverting to PIO because of bad alignment\n");
  964. host->flags &= ~SDHCI_REQ_USE_DMA;
  965. break;
  966. }
  967. }
  968. }
  969. }
  970. sdhci_config_dma(host);
  971. if (host->flags & SDHCI_REQ_USE_DMA) {
  972. int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
  973. if (sg_cnt <= 0) {
  974. /*
  975. * This only happens when someone fed
  976. * us an invalid request.
  977. */
  978. WARN_ON(1);
  979. host->flags &= ~SDHCI_REQ_USE_DMA;
  980. } else if (host->flags & SDHCI_USE_ADMA) {
  981. sdhci_adma_table_pre(host, data, sg_cnt);
  982. sdhci_set_adma_addr(host, host->adma_addr);
  983. } else {
  984. WARN_ON(sg_cnt != 1);
  985. sdhci_set_sdma_addr(host, sdhci_sdma_address(host));
  986. }
  987. }
  988. if (!(host->flags & SDHCI_REQ_USE_DMA)) {
  989. int flags;
  990. flags = SG_MITER_ATOMIC;
  991. if (host->data->flags & MMC_DATA_READ)
  992. flags |= SG_MITER_TO_SG;
  993. else
  994. flags |= SG_MITER_FROM_SG;
  995. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  996. host->blocks = data->blocks;
  997. }
  998. sdhci_set_transfer_irqs(host);
  999. }
  1000. EXPORT_SYMBOL_GPL(sdhci_prepare_dma);
  1001. static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
  1002. {
  1003. struct mmc_data *data = cmd->data;
  1004. sdhci_initialize_data(host, data);
  1005. sdhci_prepare_dma(host, data);
  1006. sdhci_set_block_info(host, data);
  1007. }
  1008. #if IS_ENABLED(CONFIG_MMC_SDHCI_EXTERNAL_DMA)
  1009. static int sdhci_external_dma_init(struct sdhci_host *host)
  1010. {
  1011. int ret = 0;
  1012. struct mmc_host *mmc = host->mmc;
  1013. host->tx_chan = dma_request_chan(mmc_dev(mmc), "tx");
  1014. if (IS_ERR(host->tx_chan)) {
  1015. ret = PTR_ERR(host->tx_chan);
  1016. if (ret != -EPROBE_DEFER)
  1017. pr_warn("Failed to request TX DMA channel.\n");
  1018. host->tx_chan = NULL;
  1019. return ret;
  1020. }
  1021. host->rx_chan = dma_request_chan(mmc_dev(mmc), "rx");
  1022. if (IS_ERR(host->rx_chan)) {
  1023. if (host->tx_chan) {
  1024. dma_release_channel(host->tx_chan);
  1025. host->tx_chan = NULL;
  1026. }
  1027. ret = PTR_ERR(host->rx_chan);
  1028. if (ret != -EPROBE_DEFER)
  1029. pr_warn("Failed to request RX DMA channel.\n");
  1030. host->rx_chan = NULL;
  1031. }
  1032. return ret;
  1033. }
  1034. static struct dma_chan *sdhci_external_dma_channel(struct sdhci_host *host,
  1035. struct mmc_data *data)
  1036. {
  1037. return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
  1038. }
  1039. static int sdhci_external_dma_setup(struct sdhci_host *host,
  1040. struct mmc_command *cmd)
  1041. {
  1042. int ret, i;
  1043. enum dma_transfer_direction dir;
  1044. struct dma_async_tx_descriptor *desc;
  1045. struct mmc_data *data = cmd->data;
  1046. struct dma_chan *chan;
  1047. struct dma_slave_config cfg;
  1048. dma_cookie_t cookie;
  1049. int sg_cnt;
  1050. if (!host->mapbase)
  1051. return -EINVAL;
  1052. memset(&cfg, 0, sizeof(cfg));
  1053. cfg.src_addr = host->mapbase + SDHCI_BUFFER;
  1054. cfg.dst_addr = host->mapbase + SDHCI_BUFFER;
  1055. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1056. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1057. cfg.src_maxburst = data->blksz / 4;
  1058. cfg.dst_maxburst = data->blksz / 4;
  1059. /* Sanity check: all the SG entries must be aligned by block size. */
  1060. for (i = 0; i < data->sg_len; i++) {
  1061. if ((data->sg + i)->length % data->blksz)
  1062. return -EINVAL;
  1063. }
  1064. chan = sdhci_external_dma_channel(host, data);
  1065. ret = dmaengine_slave_config(chan, &cfg);
  1066. if (ret)
  1067. return ret;
  1068. sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
  1069. if (sg_cnt <= 0)
  1070. return -EINVAL;
  1071. dir = data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
  1072. desc = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len, dir,
  1073. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1074. if (!desc)
  1075. return -EINVAL;
  1076. desc->callback = NULL;
  1077. desc->callback_param = NULL;
  1078. cookie = dmaengine_submit(desc);
  1079. if (dma_submit_error(cookie))
  1080. ret = cookie;
  1081. return ret;
  1082. }
  1083. static void sdhci_external_dma_release(struct sdhci_host *host)
  1084. {
  1085. if (host->tx_chan) {
  1086. dma_release_channel(host->tx_chan);
  1087. host->tx_chan = NULL;
  1088. }
  1089. if (host->rx_chan) {
  1090. dma_release_channel(host->rx_chan);
  1091. host->rx_chan = NULL;
  1092. }
  1093. sdhci_switch_external_dma(host, false);
  1094. }
  1095. static void __sdhci_external_dma_prepare_data(struct sdhci_host *host,
  1096. struct mmc_command *cmd)
  1097. {
  1098. struct mmc_data *data = cmd->data;
  1099. sdhci_initialize_data(host, data);
  1100. host->flags |= SDHCI_REQ_USE_DMA;
  1101. sdhci_set_transfer_irqs(host);
  1102. sdhci_set_block_info(host, data);
  1103. }
  1104. static void sdhci_external_dma_prepare_data(struct sdhci_host *host,
  1105. struct mmc_command *cmd)
  1106. {
  1107. if (!sdhci_external_dma_setup(host, cmd)) {
  1108. __sdhci_external_dma_prepare_data(host, cmd);
  1109. } else {
  1110. sdhci_external_dma_release(host);
  1111. pr_err("%s: Cannot use external DMA, switch to the DMA/PIO which standard SDHCI provides.\n",
  1112. mmc_hostname(host->mmc));
  1113. sdhci_prepare_data(host, cmd);
  1114. }
  1115. }
  1116. static void sdhci_external_dma_pre_transfer(struct sdhci_host *host,
  1117. struct mmc_command *cmd)
  1118. {
  1119. struct dma_chan *chan;
  1120. if (!cmd->data)
  1121. return;
  1122. chan = sdhci_external_dma_channel(host, cmd->data);
  1123. if (chan)
  1124. dma_async_issue_pending(chan);
  1125. }
  1126. #else
  1127. static inline int sdhci_external_dma_init(struct sdhci_host *host)
  1128. {
  1129. return -EOPNOTSUPP;
  1130. }
  1131. static inline void sdhci_external_dma_release(struct sdhci_host *host)
  1132. {
  1133. }
  1134. static inline void sdhci_external_dma_prepare_data(struct sdhci_host *host,
  1135. struct mmc_command *cmd)
  1136. {
  1137. /* This should never happen */
  1138. WARN_ON_ONCE(1);
  1139. }
  1140. static inline void sdhci_external_dma_pre_transfer(struct sdhci_host *host,
  1141. struct mmc_command *cmd)
  1142. {
  1143. }
  1144. static inline struct dma_chan *sdhci_external_dma_channel(struct sdhci_host *host,
  1145. struct mmc_data *data)
  1146. {
  1147. return NULL;
  1148. }
  1149. #endif
  1150. void sdhci_switch_external_dma(struct sdhci_host *host, bool en)
  1151. {
  1152. host->use_external_dma = en;
  1153. }
  1154. EXPORT_SYMBOL_GPL(sdhci_switch_external_dma);
  1155. static inline bool sdhci_auto_cmd12(struct sdhci_host *host,
  1156. struct mmc_request *mrq)
  1157. {
  1158. return !mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
  1159. !mrq->cap_cmd_during_tfr;
  1160. }
  1161. static inline bool sdhci_auto_cmd23(struct sdhci_host *host,
  1162. struct mmc_request *mrq)
  1163. {
  1164. return mrq->sbc && (host->flags & SDHCI_AUTO_CMD23);
  1165. }
  1166. static inline bool sdhci_manual_cmd23(struct sdhci_host *host,
  1167. struct mmc_request *mrq)
  1168. {
  1169. return mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23);
  1170. }
  1171. static inline void sdhci_auto_cmd_select(struct sdhci_host *host,
  1172. struct mmc_command *cmd,
  1173. u16 *mode)
  1174. {
  1175. bool use_cmd12 = sdhci_auto_cmd12(host, cmd->mrq) &&
  1176. (cmd->opcode != SD_IO_RW_EXTENDED);
  1177. bool use_cmd23 = sdhci_auto_cmd23(host, cmd->mrq);
  1178. u16 ctrl2;
  1179. /*
  1180. * In case of Version 4.10 or later, use of 'Auto CMD Auto
  1181. * Select' is recommended rather than use of 'Auto CMD12
  1182. * Enable' or 'Auto CMD23 Enable'. We require Version 4 Mode
  1183. * here because some controllers (e.g sdhci-of-dwmshc) expect it.
  1184. */
  1185. if (host->version >= SDHCI_SPEC_410 && host->v4_mode &&
  1186. (use_cmd12 || use_cmd23)) {
  1187. *mode |= SDHCI_TRNS_AUTO_SEL;
  1188. ctrl2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1189. if (use_cmd23)
  1190. ctrl2 |= SDHCI_CMD23_ENABLE;
  1191. else
  1192. ctrl2 &= ~SDHCI_CMD23_ENABLE;
  1193. sdhci_writew(host, ctrl2, SDHCI_HOST_CONTROL2);
  1194. return;
  1195. }
  1196. /*
  1197. * If we are sending CMD23, CMD12 never gets sent
  1198. * on successful completion (so no Auto-CMD12).
  1199. */
  1200. if (use_cmd12)
  1201. *mode |= SDHCI_TRNS_AUTO_CMD12;
  1202. else if (use_cmd23)
  1203. *mode |= SDHCI_TRNS_AUTO_CMD23;
  1204. }
  1205. static void sdhci_set_transfer_mode(struct sdhci_host *host,
  1206. struct mmc_command *cmd)
  1207. {
  1208. u16 mode = 0;
  1209. struct mmc_data *data = cmd->data;
  1210. if (data == NULL) {
  1211. if (host->quirks2 &
  1212. SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
  1213. /* must not clear SDHCI_TRANSFER_MODE when tuning */
  1214. if (!mmc_op_tuning(cmd->opcode))
  1215. sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
  1216. } else {
  1217. /* clear Auto CMD settings for no data CMDs */
  1218. mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
  1219. sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
  1220. SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
  1221. }
  1222. return;
  1223. }
  1224. WARN_ON(!host->data);
  1225. if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
  1226. mode = SDHCI_TRNS_BLK_CNT_EN;
  1227. if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
  1228. mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
  1229. sdhci_auto_cmd_select(host, cmd, &mode);
  1230. if (sdhci_auto_cmd23(host, cmd->mrq))
  1231. sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2);
  1232. }
  1233. if (data->flags & MMC_DATA_READ)
  1234. mode |= SDHCI_TRNS_READ;
  1235. if (host->flags & SDHCI_REQ_USE_DMA)
  1236. mode |= SDHCI_TRNS_DMA;
  1237. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  1238. }
  1239. bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq)
  1240. {
  1241. return (!(host->flags & SDHCI_DEVICE_DEAD) &&
  1242. ((mrq->cmd && mrq->cmd->error) ||
  1243. (mrq->sbc && mrq->sbc->error) ||
  1244. (mrq->data && mrq->data->stop && mrq->data->stop->error) ||
  1245. (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)));
  1246. }
  1247. EXPORT_SYMBOL_GPL(sdhci_needs_reset);
  1248. static void sdhci_set_mrq_done(struct sdhci_host *host, struct mmc_request *mrq)
  1249. {
  1250. int i;
  1251. for (i = 0; i < SDHCI_MAX_MRQS; i++) {
  1252. if (host->mrqs_done[i] == mrq) {
  1253. WARN_ON(1);
  1254. return;
  1255. }
  1256. }
  1257. for (i = 0; i < SDHCI_MAX_MRQS; i++) {
  1258. if (!host->mrqs_done[i]) {
  1259. host->mrqs_done[i] = mrq;
  1260. break;
  1261. }
  1262. }
  1263. WARN_ON(i >= SDHCI_MAX_MRQS);
  1264. }
  1265. void __sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
  1266. {
  1267. if (host->cmd && host->cmd->mrq == mrq)
  1268. host->cmd = NULL;
  1269. if (host->data_cmd && host->data_cmd->mrq == mrq)
  1270. host->data_cmd = NULL;
  1271. if (host->deferred_cmd && host->deferred_cmd->mrq == mrq)
  1272. host->deferred_cmd = NULL;
  1273. if (host->data && host->data->mrq == mrq)
  1274. host->data = NULL;
  1275. if (sdhci_needs_reset(host, mrq))
  1276. host->pending_reset = true;
  1277. sdhci_set_mrq_done(host, mrq);
  1278. sdhci_del_timer(host, mrq);
  1279. if (!sdhci_has_requests(host))
  1280. sdhci_led_deactivate(host);
  1281. }
  1282. EXPORT_SYMBOL_GPL(__sdhci_finish_mrq);
  1283. void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
  1284. {
  1285. __sdhci_finish_mrq(host, mrq);
  1286. queue_work(host->complete_wq, &host->complete_work);
  1287. }
  1288. EXPORT_SYMBOL_GPL(sdhci_finish_mrq);
  1289. void __sdhci_finish_data_common(struct sdhci_host *host, bool defer_reset)
  1290. {
  1291. struct mmc_command *data_cmd = host->data_cmd;
  1292. struct mmc_data *data = host->data;
  1293. host->data = NULL;
  1294. host->data_cmd = NULL;
  1295. /*
  1296. * The controller needs a reset of internal state machines upon error
  1297. * conditions.
  1298. */
  1299. if (data->error) {
  1300. if (defer_reset)
  1301. host->pending_reset = true;
  1302. else if (!host->cmd || host->cmd == data_cmd)
  1303. sdhci_reset_for(host, REQUEST_ERROR);
  1304. else
  1305. sdhci_reset_for(host, REQUEST_ERROR_DATA_ONLY);
  1306. }
  1307. if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
  1308. (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
  1309. sdhci_adma_table_post(host, data);
  1310. /*
  1311. * The specification states that the block count register must
  1312. * be updated, but it does not specify at what point in the
  1313. * data flow. That makes the register entirely useless to read
  1314. * back so we have to assume that nothing made it to the card
  1315. * in the event of an error.
  1316. */
  1317. if (data->error)
  1318. data->bytes_xfered = 0;
  1319. else
  1320. data->bytes_xfered = data->blksz * data->blocks;
  1321. }
  1322. EXPORT_SYMBOL_GPL(__sdhci_finish_data_common);
  1323. static void __sdhci_finish_data(struct sdhci_host *host, bool sw_data_timeout)
  1324. {
  1325. struct mmc_data *data = host->data;
  1326. __sdhci_finish_data_common(host, false);
  1327. /*
  1328. * Need to send CMD12 if -
  1329. * a) open-ended multiblock transfer not using auto CMD12 (no CMD23)
  1330. * b) error in multiblock transfer
  1331. */
  1332. if (data->stop &&
  1333. ((!data->mrq->sbc && !sdhci_auto_cmd12(host, data->mrq)) ||
  1334. data->error)) {
  1335. /*
  1336. * 'cap_cmd_during_tfr' request must not use the command line
  1337. * after mmc_command_done() has been called. It is upper layer's
  1338. * responsibility to send the stop command if required.
  1339. */
  1340. if (data->mrq->cap_cmd_during_tfr) {
  1341. __sdhci_finish_mrq(host, data->mrq);
  1342. } else {
  1343. /* Avoid triggering warning in sdhci_send_command() */
  1344. host->cmd = NULL;
  1345. if (!sdhci_send_command(host, data->stop)) {
  1346. if (sw_data_timeout) {
  1347. /*
  1348. * This is anyway a sw data timeout, so
  1349. * give up now.
  1350. */
  1351. data->stop->error = -EIO;
  1352. __sdhci_finish_mrq(host, data->mrq);
  1353. } else {
  1354. WARN_ON(host->deferred_cmd);
  1355. host->deferred_cmd = data->stop;
  1356. }
  1357. }
  1358. }
  1359. } else {
  1360. __sdhci_finish_mrq(host, data->mrq);
  1361. }
  1362. }
  1363. static void sdhci_finish_data(struct sdhci_host *host)
  1364. {
  1365. __sdhci_finish_data(host, false);
  1366. }
  1367. static bool sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
  1368. {
  1369. int flags;
  1370. u32 mask;
  1371. unsigned long timeout;
  1372. WARN_ON(host->cmd);
  1373. /* Initially, a command has no error */
  1374. cmd->error = 0;
  1375. if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
  1376. cmd->opcode == MMC_STOP_TRANSMISSION)
  1377. cmd->flags |= MMC_RSP_BUSY;
  1378. mask = SDHCI_CMD_INHIBIT;
  1379. if (sdhci_data_line_cmd(cmd))
  1380. mask |= SDHCI_DATA_INHIBIT;
  1381. /* We shouldn't wait for data inihibit for stop commands, even
  1382. though they might use busy signaling */
  1383. if (cmd->mrq->data && (cmd == cmd->mrq->data->stop))
  1384. mask &= ~SDHCI_DATA_INHIBIT;
  1385. if (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask)
  1386. return false;
  1387. host->cmd = cmd;
  1388. host->data_timeout = 0;
  1389. if (sdhci_data_line_cmd(cmd)) {
  1390. WARN_ON(host->data_cmd);
  1391. host->data_cmd = cmd;
  1392. sdhci_set_timeout(host, cmd);
  1393. }
  1394. if (cmd->data) {
  1395. if (host->use_external_dma)
  1396. sdhci_external_dma_prepare_data(host, cmd);
  1397. else
  1398. sdhci_prepare_data(host, cmd);
  1399. }
  1400. sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
  1401. sdhci_set_transfer_mode(host, cmd);
  1402. if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  1403. WARN_ONCE(1, "Unsupported response type!\n");
  1404. /*
  1405. * This does not happen in practice because 136-bit response
  1406. * commands never have busy waiting, so rather than complicate
  1407. * the error path, just remove busy waiting and continue.
  1408. */
  1409. cmd->flags &= ~MMC_RSP_BUSY;
  1410. }
  1411. if (!(cmd->flags & MMC_RSP_PRESENT))
  1412. flags = SDHCI_CMD_RESP_NONE;
  1413. else if (cmd->flags & MMC_RSP_136)
  1414. flags = SDHCI_CMD_RESP_LONG;
  1415. else if (cmd->flags & MMC_RSP_BUSY)
  1416. flags = SDHCI_CMD_RESP_SHORT_BUSY;
  1417. else
  1418. flags = SDHCI_CMD_RESP_SHORT;
  1419. if (cmd->flags & MMC_RSP_CRC)
  1420. flags |= SDHCI_CMD_CRC;
  1421. if (cmd->flags & MMC_RSP_OPCODE)
  1422. flags |= SDHCI_CMD_INDEX;
  1423. /* CMD19 is special in that the Data Present Select should be set */
  1424. if (cmd->data || mmc_op_tuning(cmd->opcode))
  1425. flags |= SDHCI_CMD_DATA;
  1426. timeout = jiffies;
  1427. if (host->data_timeout)
  1428. timeout += nsecs_to_jiffies(host->data_timeout);
  1429. else if (!cmd->data && cmd->busy_timeout > 9000)
  1430. timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
  1431. else
  1432. timeout += 10 * HZ;
  1433. sdhci_mod_timer(host, cmd->mrq, timeout);
  1434. if (host->use_external_dma)
  1435. sdhci_external_dma_pre_transfer(host, cmd);
  1436. sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
  1437. return true;
  1438. }
  1439. bool sdhci_present_error(struct sdhci_host *host,
  1440. struct mmc_command *cmd, bool present)
  1441. {
  1442. if (!present || host->flags & SDHCI_DEVICE_DEAD) {
  1443. cmd->error = -ENOMEDIUM;
  1444. return true;
  1445. }
  1446. return false;
  1447. }
  1448. EXPORT_SYMBOL_GPL(sdhci_present_error);
  1449. static bool sdhci_send_command_retry(struct sdhci_host *host,
  1450. struct mmc_command *cmd,
  1451. unsigned long flags)
  1452. __releases(host->lock)
  1453. __acquires(host->lock)
  1454. {
  1455. struct mmc_command *deferred_cmd = host->deferred_cmd;
  1456. int timeout = 10; /* Approx. 10 ms */
  1457. bool present;
  1458. while (!sdhci_send_command(host, cmd)) {
  1459. if (!timeout--) {
  1460. pr_err("%s: Controller never released inhibit bit(s).\n",
  1461. mmc_hostname(host->mmc));
  1462. sdhci_err_stats_inc(host, CTRL_TIMEOUT);
  1463. sdhci_dumpregs(host);
  1464. cmd->error = -EIO;
  1465. return false;
  1466. }
  1467. spin_unlock_irqrestore(&host->lock, flags);
  1468. usleep_range(1000, 1250);
  1469. present = host->mmc->ops->get_cd(host->mmc);
  1470. spin_lock_irqsave(&host->lock, flags);
  1471. /* A deferred command might disappear, handle that */
  1472. if (cmd == deferred_cmd && cmd != host->deferred_cmd)
  1473. return true;
  1474. if (sdhci_present_error(host, cmd, present))
  1475. return false;
  1476. }
  1477. if (cmd == host->deferred_cmd)
  1478. host->deferred_cmd = NULL;
  1479. return true;
  1480. }
  1481. static void sdhci_read_rsp_136(struct sdhci_host *host, struct mmc_command *cmd)
  1482. {
  1483. int i, reg;
  1484. for (i = 0; i < 4; i++) {
  1485. reg = SDHCI_RESPONSE + (3 - i) * 4;
  1486. cmd->resp[i] = sdhci_readl(host, reg);
  1487. }
  1488. if (host->quirks2 & SDHCI_QUIRK2_RSP_136_HAS_CRC)
  1489. return;
  1490. /* CRC is stripped so we need to do some shifting */
  1491. for (i = 0; i < 4; i++) {
  1492. cmd->resp[i] <<= 8;
  1493. if (i != 3)
  1494. cmd->resp[i] |= cmd->resp[i + 1] >> 24;
  1495. }
  1496. }
  1497. static void sdhci_finish_command(struct sdhci_host *host)
  1498. {
  1499. struct mmc_command *cmd = host->cmd;
  1500. host->cmd = NULL;
  1501. if (cmd->flags & MMC_RSP_PRESENT) {
  1502. if (cmd->flags & MMC_RSP_136) {
  1503. sdhci_read_rsp_136(host, cmd);
  1504. } else {
  1505. cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
  1506. }
  1507. }
  1508. if (cmd->mrq->cap_cmd_during_tfr && cmd == cmd->mrq->cmd)
  1509. mmc_command_done(host->mmc, cmd->mrq);
  1510. /*
  1511. * The host can send and interrupt when the busy state has
  1512. * ended, allowing us to wait without wasting CPU cycles.
  1513. * The busy signal uses DAT0 so this is similar to waiting
  1514. * for data to complete.
  1515. *
  1516. * Note: The 1.0 specification is a bit ambiguous about this
  1517. * feature so there might be some problems with older
  1518. * controllers.
  1519. */
  1520. if (cmd->flags & MMC_RSP_BUSY) {
  1521. if (cmd->data) {
  1522. DBG("Cannot wait for busy signal when also doing a data transfer");
  1523. } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
  1524. cmd == host->data_cmd) {
  1525. /* Command complete before busy is ended */
  1526. return;
  1527. }
  1528. }
  1529. /* Finished CMD23, now send actual command. */
  1530. if (cmd == cmd->mrq->sbc) {
  1531. if (!sdhci_send_command(host, cmd->mrq->cmd)) {
  1532. WARN_ON(host->deferred_cmd);
  1533. host->deferred_cmd = cmd->mrq->cmd;
  1534. }
  1535. } else {
  1536. /* Processed actual command. */
  1537. if (host->data && host->data_early)
  1538. sdhci_finish_data(host);
  1539. if (!cmd->data)
  1540. __sdhci_finish_mrq(host, cmd->mrq);
  1541. }
  1542. }
  1543. static u16 sdhci_get_preset_value(struct sdhci_host *host)
  1544. {
  1545. u16 preset = 0;
  1546. switch (host->timing) {
  1547. case MMC_TIMING_MMC_HS:
  1548. case MMC_TIMING_SD_HS:
  1549. preset = sdhci_readw(host, SDHCI_PRESET_FOR_HIGH_SPEED);
  1550. break;
  1551. case MMC_TIMING_UHS_SDR12:
  1552. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
  1553. break;
  1554. case MMC_TIMING_UHS_SDR25:
  1555. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
  1556. break;
  1557. case MMC_TIMING_UHS_SDR50:
  1558. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
  1559. break;
  1560. case MMC_TIMING_UHS_SDR104:
  1561. case MMC_TIMING_MMC_HS200:
  1562. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
  1563. break;
  1564. case MMC_TIMING_UHS_DDR50:
  1565. case MMC_TIMING_MMC_DDR52:
  1566. preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
  1567. break;
  1568. case MMC_TIMING_MMC_HS400:
  1569. preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
  1570. break;
  1571. case MMC_TIMING_UHS2_SPEED_A:
  1572. case MMC_TIMING_UHS2_SPEED_A_HD:
  1573. case MMC_TIMING_UHS2_SPEED_B:
  1574. case MMC_TIMING_UHS2_SPEED_B_HD:
  1575. preset = sdhci_readw(host, SDHCI_PRESET_FOR_UHS2);
  1576. break;
  1577. default:
  1578. pr_warn("%s: Invalid UHS-I mode selected\n",
  1579. mmc_hostname(host->mmc));
  1580. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
  1581. break;
  1582. }
  1583. return preset;
  1584. }
  1585. u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
  1586. unsigned int *actual_clock)
  1587. {
  1588. int div = 0; /* Initialized for compiler warning */
  1589. int real_div = div, clk_mul = 1;
  1590. u16 clk = 0;
  1591. bool switch_base_clk = false;
  1592. if (host->version >= SDHCI_SPEC_300) {
  1593. if (host->preset_enabled) {
  1594. u16 pre_val;
  1595. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1596. pre_val = sdhci_get_preset_value(host);
  1597. div = FIELD_GET(SDHCI_PRESET_SDCLK_FREQ_MASK, pre_val);
  1598. if (host->clk_mul &&
  1599. (pre_val & SDHCI_PRESET_CLKGEN_SEL)) {
  1600. clk = SDHCI_PROG_CLOCK_MODE;
  1601. real_div = div + 1;
  1602. clk_mul = host->clk_mul;
  1603. } else {
  1604. real_div = max_t(int, 1, div << 1);
  1605. }
  1606. goto clock_set;
  1607. }
  1608. /*
  1609. * Check if the Host Controller supports Programmable Clock
  1610. * Mode.
  1611. */
  1612. if (host->clk_mul) {
  1613. for (div = 1; div <= 1024; div++) {
  1614. if ((host->max_clk * host->clk_mul / div)
  1615. <= clock)
  1616. break;
  1617. }
  1618. if ((host->max_clk * host->clk_mul / div) <= clock) {
  1619. /*
  1620. * Set Programmable Clock Mode in the Clock
  1621. * Control register.
  1622. */
  1623. clk = SDHCI_PROG_CLOCK_MODE;
  1624. real_div = div;
  1625. clk_mul = host->clk_mul;
  1626. div--;
  1627. } else {
  1628. /*
  1629. * Divisor can be too small to reach clock
  1630. * speed requirement. Then use the base clock.
  1631. */
  1632. switch_base_clk = true;
  1633. }
  1634. }
  1635. if (!host->clk_mul || switch_base_clk) {
  1636. /* Version 3.00 divisors must be a multiple of 2. */
  1637. if (host->max_clk <= clock)
  1638. div = 1;
  1639. else {
  1640. for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
  1641. div += 2) {
  1642. if ((host->max_clk / div) <= clock)
  1643. break;
  1644. }
  1645. }
  1646. real_div = div;
  1647. div >>= 1;
  1648. if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
  1649. && !div && host->max_clk <= 25000000)
  1650. div = 1;
  1651. }
  1652. } else {
  1653. /* Version 2.00 divisors must be a power of 2. */
  1654. for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
  1655. if ((host->max_clk / div) <= clock)
  1656. break;
  1657. }
  1658. real_div = div;
  1659. div >>= 1;
  1660. }
  1661. clock_set:
  1662. if (real_div)
  1663. *actual_clock = (host->max_clk * clk_mul) / real_div;
  1664. clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
  1665. clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
  1666. << SDHCI_DIVIDER_HI_SHIFT;
  1667. return clk;
  1668. }
  1669. EXPORT_SYMBOL_GPL(sdhci_calc_clk);
  1670. void sdhci_enable_clk(struct sdhci_host *host, u16 clk)
  1671. {
  1672. ktime_t timeout;
  1673. clk |= SDHCI_CLOCK_INT_EN;
  1674. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1675. /* Wait max 150 ms */
  1676. timeout = ktime_add_ms(ktime_get(), 150);
  1677. while (1) {
  1678. bool timedout = ktime_after(ktime_get(), timeout);
  1679. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1680. if (clk & SDHCI_CLOCK_INT_STABLE)
  1681. break;
  1682. if (timedout) {
  1683. pr_err("%s: Internal clock never stabilised.\n",
  1684. mmc_hostname(host->mmc));
  1685. sdhci_err_stats_inc(host, CTRL_TIMEOUT);
  1686. sdhci_dumpregs(host);
  1687. return;
  1688. }
  1689. udelay(10);
  1690. }
  1691. if (host->version >= SDHCI_SPEC_410 && host->v4_mode) {
  1692. clk |= SDHCI_CLOCK_PLL_EN;
  1693. clk &= ~SDHCI_CLOCK_INT_STABLE;
  1694. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1695. /* Wait max 150 ms */
  1696. timeout = ktime_add_ms(ktime_get(), 150);
  1697. while (1) {
  1698. bool timedout = ktime_after(ktime_get(), timeout);
  1699. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1700. if (clk & SDHCI_CLOCK_INT_STABLE)
  1701. break;
  1702. if (timedout) {
  1703. pr_err("%s: PLL clock never stabilised.\n",
  1704. mmc_hostname(host->mmc));
  1705. sdhci_err_stats_inc(host, CTRL_TIMEOUT);
  1706. sdhci_dumpregs(host);
  1707. return;
  1708. }
  1709. udelay(10);
  1710. }
  1711. }
  1712. clk |= SDHCI_CLOCK_CARD_EN;
  1713. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1714. }
  1715. EXPORT_SYMBOL_GPL(sdhci_enable_clk);
  1716. void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
  1717. {
  1718. u16 clk;
  1719. host->mmc->actual_clock = 0;
  1720. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  1721. if (clock == 0)
  1722. return;
  1723. clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
  1724. sdhci_enable_clk(host, clk);
  1725. }
  1726. EXPORT_SYMBOL_GPL(sdhci_set_clock);
  1727. static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
  1728. unsigned short vdd)
  1729. {
  1730. struct mmc_host *mmc = host->mmc;
  1731. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
  1732. if (mode != MMC_POWER_OFF)
  1733. sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
  1734. else
  1735. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  1736. }
  1737. unsigned short sdhci_get_vdd_value(unsigned short vdd)
  1738. {
  1739. switch (1 << vdd) {
  1740. case MMC_VDD_165_195:
  1741. /*
  1742. * Without a regulator, SDHCI does not support 2.0v
  1743. * so we only get here if the driver deliberately
  1744. * added the 2.0v range to ocr_avail. Map it to 1.8v
  1745. * for the purpose of turning on the power.
  1746. */
  1747. case MMC_VDD_20_21:
  1748. return SDHCI_POWER_180;
  1749. case MMC_VDD_29_30:
  1750. case MMC_VDD_30_31:
  1751. return SDHCI_POWER_300;
  1752. case MMC_VDD_32_33:
  1753. case MMC_VDD_33_34:
  1754. /*
  1755. * 3.4V ~ 3.6V are valid only for those platforms where it's
  1756. * known that the voltage range is supported by hardware.
  1757. */
  1758. case MMC_VDD_34_35:
  1759. case MMC_VDD_35_36:
  1760. return SDHCI_POWER_330;
  1761. default:
  1762. return 0;
  1763. }
  1764. }
  1765. EXPORT_SYMBOL_GPL(sdhci_get_vdd_value);
  1766. void sdhci_set_power_noreg(struct sdhci_host *host, unsigned char mode,
  1767. unsigned short vdd)
  1768. {
  1769. u8 pwr = 0;
  1770. if (mode != MMC_POWER_OFF) {
  1771. pwr = sdhci_get_vdd_value(vdd);
  1772. if (!pwr) {
  1773. WARN(1, "%s: Invalid vdd %#x\n",
  1774. mmc_hostname(host->mmc), vdd);
  1775. }
  1776. }
  1777. if (host->pwr == pwr)
  1778. return;
  1779. host->pwr = pwr;
  1780. if (pwr == 0) {
  1781. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  1782. if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
  1783. sdhci_runtime_pm_bus_off(host);
  1784. } else {
  1785. /*
  1786. * Spec says that we should clear the power reg before setting
  1787. * a new value. Some controllers don't seem to like this though.
  1788. */
  1789. if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
  1790. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  1791. /*
  1792. * At least the Marvell CaFe chip gets confused if we set the
  1793. * voltage and set turn on power at the same time, so set the
  1794. * voltage first.
  1795. */
  1796. if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
  1797. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  1798. pwr |= SDHCI_POWER_ON;
  1799. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  1800. if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
  1801. sdhci_runtime_pm_bus_on(host);
  1802. /*
  1803. * Some controllers need an extra 10ms delay of 10ms before
  1804. * they can apply clock after applying power
  1805. */
  1806. if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
  1807. mdelay(10);
  1808. }
  1809. }
  1810. EXPORT_SYMBOL_GPL(sdhci_set_power_noreg);
  1811. void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
  1812. unsigned short vdd)
  1813. {
  1814. if (IS_ERR(host->mmc->supply.vmmc))
  1815. sdhci_set_power_noreg(host, mode, vdd);
  1816. else
  1817. sdhci_set_power_reg(host, mode, vdd);
  1818. }
  1819. EXPORT_SYMBOL_GPL(sdhci_set_power);
  1820. /*
  1821. * Some controllers need to configure a valid bus voltage on their power
  1822. * register regardless of whether an external regulator is taking care of power
  1823. * supply. This helper function takes care of it if set as the controller's
  1824. * sdhci_ops.set_power callback.
  1825. */
  1826. void sdhci_set_power_and_bus_voltage(struct sdhci_host *host,
  1827. unsigned char mode,
  1828. unsigned short vdd)
  1829. {
  1830. if (!IS_ERR(host->mmc->supply.vmmc)) {
  1831. struct mmc_host *mmc = host->mmc;
  1832. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
  1833. }
  1834. sdhci_set_power_noreg(host, mode, vdd);
  1835. }
  1836. EXPORT_SYMBOL_GPL(sdhci_set_power_and_bus_voltage);
  1837. /*****************************************************************************\
  1838. * *
  1839. * MMC callbacks *
  1840. * *
  1841. \*****************************************************************************/
  1842. void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  1843. {
  1844. struct sdhci_host *host = mmc_priv(mmc);
  1845. struct mmc_command *cmd;
  1846. unsigned long flags;
  1847. bool present;
  1848. /* Firstly check card presence */
  1849. present = mmc->ops->get_cd(mmc);
  1850. spin_lock_irqsave(&host->lock, flags);
  1851. sdhci_led_activate(host);
  1852. if (sdhci_present_error(host, mrq->cmd, present))
  1853. goto out_finish;
  1854. cmd = sdhci_manual_cmd23(host, mrq) ? mrq->sbc : mrq->cmd;
  1855. if (!sdhci_send_command_retry(host, cmd, flags))
  1856. goto out_finish;
  1857. spin_unlock_irqrestore(&host->lock, flags);
  1858. return;
  1859. out_finish:
  1860. sdhci_finish_mrq(host, mrq);
  1861. spin_unlock_irqrestore(&host->lock, flags);
  1862. }
  1863. EXPORT_SYMBOL_GPL(sdhci_request);
  1864. int sdhci_request_atomic(struct mmc_host *mmc, struct mmc_request *mrq)
  1865. {
  1866. struct sdhci_host *host = mmc_priv(mmc);
  1867. struct mmc_command *cmd;
  1868. unsigned long flags;
  1869. int ret = 0;
  1870. spin_lock_irqsave(&host->lock, flags);
  1871. if (sdhci_present_error(host, mrq->cmd, true)) {
  1872. sdhci_finish_mrq(host, mrq);
  1873. goto out_finish;
  1874. }
  1875. cmd = sdhci_manual_cmd23(host, mrq) ? mrq->sbc : mrq->cmd;
  1876. /*
  1877. * The HSQ may send a command in interrupt context without polling
  1878. * the busy signaling, which means we should return BUSY if controller
  1879. * has not released inhibit bits to allow HSQ trying to send request
  1880. * again in non-atomic context. So we should not finish this request
  1881. * here.
  1882. */
  1883. if (!sdhci_send_command(host, cmd))
  1884. ret = -EBUSY;
  1885. else
  1886. sdhci_led_activate(host);
  1887. out_finish:
  1888. spin_unlock_irqrestore(&host->lock, flags);
  1889. return ret;
  1890. }
  1891. EXPORT_SYMBOL_GPL(sdhci_request_atomic);
  1892. void sdhci_set_bus_width(struct sdhci_host *host, int width)
  1893. {
  1894. u8 ctrl;
  1895. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  1896. if (width == MMC_BUS_WIDTH_8) {
  1897. ctrl &= ~SDHCI_CTRL_4BITBUS;
  1898. ctrl |= SDHCI_CTRL_8BITBUS;
  1899. } else {
  1900. if (host->mmc->caps & MMC_CAP_8_BIT_DATA)
  1901. ctrl &= ~SDHCI_CTRL_8BITBUS;
  1902. if (width == MMC_BUS_WIDTH_4)
  1903. ctrl |= SDHCI_CTRL_4BITBUS;
  1904. else
  1905. ctrl &= ~SDHCI_CTRL_4BITBUS;
  1906. }
  1907. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1908. }
  1909. EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
  1910. void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
  1911. {
  1912. u16 ctrl_2;
  1913. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1914. /* Select Bus Speed Mode for host */
  1915. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  1916. if ((timing == MMC_TIMING_MMC_HS200) ||
  1917. (timing == MMC_TIMING_UHS_SDR104))
  1918. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  1919. else if (timing == MMC_TIMING_UHS_SDR12)
  1920. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  1921. else if (timing == MMC_TIMING_UHS_SDR25)
  1922. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  1923. else if (timing == MMC_TIMING_UHS_SDR50)
  1924. ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
  1925. else if ((timing == MMC_TIMING_UHS_DDR50) ||
  1926. (timing == MMC_TIMING_MMC_DDR52))
  1927. ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
  1928. else if (timing == MMC_TIMING_MMC_HS400)
  1929. ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
  1930. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  1931. }
  1932. EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
  1933. static bool sdhci_timing_has_preset(unsigned char timing)
  1934. {
  1935. switch (timing) {
  1936. case MMC_TIMING_UHS_SDR12:
  1937. case MMC_TIMING_UHS_SDR25:
  1938. case MMC_TIMING_UHS_SDR50:
  1939. case MMC_TIMING_UHS_SDR104:
  1940. case MMC_TIMING_UHS_DDR50:
  1941. case MMC_TIMING_MMC_DDR52:
  1942. return true;
  1943. }
  1944. return false;
  1945. }
  1946. static bool sdhci_preset_needed(struct sdhci_host *host, unsigned char timing)
  1947. {
  1948. return !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
  1949. sdhci_timing_has_preset(timing);
  1950. }
  1951. static bool sdhci_presetable_values_change(struct sdhci_host *host, struct mmc_ios *ios)
  1952. {
  1953. /*
  1954. * Preset Values are: Driver Strength, Clock Generator and SDCLK/RCLK
  1955. * Frequency. Check if preset values need to be enabled, or the Driver
  1956. * Strength needs updating. Note, clock changes are handled separately.
  1957. */
  1958. return !host->preset_enabled &&
  1959. (sdhci_preset_needed(host, ios->timing) || host->drv_type != ios->drv_type);
  1960. }
  1961. void sdhci_set_ios_common(struct mmc_host *mmc, struct mmc_ios *ios)
  1962. {
  1963. struct sdhci_host *host = mmc_priv(mmc);
  1964. /*
  1965. * Reset the chip on each power off.
  1966. * Should clear out any weird states.
  1967. */
  1968. if (ios->power_mode == MMC_POWER_OFF) {
  1969. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  1970. sdhci_reinit(host);
  1971. }
  1972. if (host->version >= SDHCI_SPEC_300 &&
  1973. (ios->power_mode == MMC_POWER_UP) &&
  1974. !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
  1975. sdhci_enable_preset_value(host, false);
  1976. }
  1977. EXPORT_SYMBOL_GPL(sdhci_set_ios_common);
  1978. void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1979. {
  1980. struct sdhci_host *host = mmc_priv(mmc);
  1981. bool reinit_uhs = host->reinit_uhs;
  1982. bool turning_on_clk;
  1983. u8 ctrl;
  1984. host->reinit_uhs = false;
  1985. if (ios->power_mode == MMC_POWER_UNDEFINED)
  1986. return;
  1987. if (host->flags & SDHCI_DEVICE_DEAD) {
  1988. if (!IS_ERR(mmc->supply.vmmc) &&
  1989. ios->power_mode == MMC_POWER_OFF)
  1990. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  1991. return;
  1992. }
  1993. turning_on_clk = ios->clock != host->clock && ios->clock && !host->clock;
  1994. sdhci_set_ios_common(mmc, ios);
  1995. if (!ios->clock || ios->clock != host->clock) {
  1996. host->ops->set_clock(host, ios->clock);
  1997. host->clock = ios->clock;
  1998. if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
  1999. host->clock) {
  2000. host->timeout_clk = mmc->actual_clock ?
  2001. mmc->actual_clock / 1000 :
  2002. host->clock / 1000;
  2003. mmc->max_busy_timeout =
  2004. host->ops->get_max_timeout_count ?
  2005. host->ops->get_max_timeout_count(host) :
  2006. 1 << 27;
  2007. mmc->max_busy_timeout /= host->timeout_clk;
  2008. }
  2009. }
  2010. if (host->ops->set_power)
  2011. host->ops->set_power(host, ios->power_mode, ios->vdd);
  2012. else
  2013. sdhci_set_power(host, ios->power_mode, ios->vdd);
  2014. if (host->ops->platform_send_init_74_clocks)
  2015. host->ops->platform_send_init_74_clocks(host, ios->power_mode);
  2016. host->ops->set_bus_width(host, ios->bus_width);
  2017. /*
  2018. * Special case to avoid multiple clock changes during voltage
  2019. * switching.
  2020. */
  2021. if (!reinit_uhs &&
  2022. turning_on_clk &&
  2023. host->timing == ios->timing &&
  2024. host->version >= SDHCI_SPEC_300 &&
  2025. !sdhci_presetable_values_change(host, ios))
  2026. return;
  2027. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  2028. if (!(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT)) {
  2029. if (ios->timing == MMC_TIMING_SD_HS ||
  2030. ios->timing == MMC_TIMING_MMC_HS ||
  2031. ios->timing == MMC_TIMING_MMC_HS400 ||
  2032. ios->timing == MMC_TIMING_MMC_HS200 ||
  2033. ios->timing == MMC_TIMING_MMC_DDR52 ||
  2034. ios->timing == MMC_TIMING_UHS_SDR50 ||
  2035. ios->timing == MMC_TIMING_UHS_SDR104 ||
  2036. ios->timing == MMC_TIMING_UHS_DDR50 ||
  2037. ios->timing == MMC_TIMING_UHS_SDR25)
  2038. ctrl |= SDHCI_CTRL_HISPD;
  2039. else
  2040. ctrl &= ~SDHCI_CTRL_HISPD;
  2041. }
  2042. if (host->version >= SDHCI_SPEC_300) {
  2043. u16 clk, ctrl_2;
  2044. /*
  2045. * According to SDHCI Spec v3.00, if the Preset Value
  2046. * Enable in the Host Control 2 register is set, we
  2047. * need to reset SD Clock Enable before changing High
  2048. * Speed Enable to avoid generating clock glitches.
  2049. */
  2050. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  2051. if (clk & SDHCI_CLOCK_CARD_EN) {
  2052. clk &= ~SDHCI_CLOCK_CARD_EN;
  2053. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  2054. }
  2055. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  2056. if (!host->preset_enabled) {
  2057. /*
  2058. * We only need to set Driver Strength if the
  2059. * preset value enable is not set.
  2060. */
  2061. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  2062. ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
  2063. if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
  2064. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
  2065. else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
  2066. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
  2067. else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
  2068. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
  2069. else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
  2070. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
  2071. else {
  2072. pr_warn("%s: invalid driver type, default to driver type B\n",
  2073. mmc_hostname(mmc));
  2074. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
  2075. }
  2076. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  2077. host->drv_type = ios->drv_type;
  2078. }
  2079. host->ops->set_uhs_signaling(host, ios->timing);
  2080. host->timing = ios->timing;
  2081. if (sdhci_preset_needed(host, ios->timing)) {
  2082. u16 preset;
  2083. sdhci_enable_preset_value(host, true);
  2084. preset = sdhci_get_preset_value(host);
  2085. ios->drv_type = FIELD_GET(SDHCI_PRESET_DRV_MASK,
  2086. preset);
  2087. host->drv_type = ios->drv_type;
  2088. }
  2089. /* Re-enable SD Clock */
  2090. host->ops->set_clock(host, host->clock);
  2091. } else
  2092. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  2093. }
  2094. EXPORT_SYMBOL_GPL(sdhci_set_ios);
  2095. static int sdhci_get_cd(struct mmc_host *mmc)
  2096. {
  2097. struct sdhci_host *host = mmc_priv(mmc);
  2098. int gpio_cd = mmc_gpio_get_cd(mmc);
  2099. if (host->flags & SDHCI_DEVICE_DEAD)
  2100. return 0;
  2101. /* If nonremovable, assume that the card is always present. */
  2102. if (!mmc_card_is_removable(mmc))
  2103. return 1;
  2104. /*
  2105. * Try slot gpio detect, if defined it take precedence
  2106. * over build in controller functionality
  2107. */
  2108. if (gpio_cd >= 0)
  2109. return !!gpio_cd;
  2110. /* If polling, assume that the card is always present. */
  2111. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  2112. return 1;
  2113. /* Host native card detect */
  2114. return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
  2115. }
  2116. int sdhci_get_cd_nogpio(struct mmc_host *mmc)
  2117. {
  2118. struct sdhci_host *host = mmc_priv(mmc);
  2119. unsigned long flags;
  2120. int ret = 0;
  2121. spin_lock_irqsave(&host->lock, flags);
  2122. if (host->flags & SDHCI_DEVICE_DEAD)
  2123. goto out;
  2124. ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
  2125. out:
  2126. spin_unlock_irqrestore(&host->lock, flags);
  2127. return ret;
  2128. }
  2129. EXPORT_SYMBOL_GPL(sdhci_get_cd_nogpio);
  2130. int sdhci_get_ro(struct mmc_host *mmc)
  2131. {
  2132. struct sdhci_host *host = mmc_priv(mmc);
  2133. bool allow_invert = false;
  2134. int is_readonly;
  2135. if (host->flags & SDHCI_DEVICE_DEAD) {
  2136. is_readonly = 0;
  2137. } else if (host->ops->get_ro) {
  2138. is_readonly = host->ops->get_ro(host);
  2139. } else if (mmc_host_can_gpio_ro(mmc)) {
  2140. is_readonly = mmc_gpio_get_ro(mmc);
  2141. /* Do not invert twice */
  2142. allow_invert = !(mmc->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
  2143. } else {
  2144. is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
  2145. & SDHCI_WRITE_PROTECT);
  2146. allow_invert = true;
  2147. }
  2148. if (is_readonly >= 0 &&
  2149. allow_invert &&
  2150. (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT))
  2151. is_readonly = !is_readonly;
  2152. return is_readonly;
  2153. }
  2154. EXPORT_SYMBOL_GPL(sdhci_get_ro);
  2155. static void sdhci_hw_reset(struct mmc_host *mmc)
  2156. {
  2157. struct sdhci_host *host = mmc_priv(mmc);
  2158. if (host->ops && host->ops->hw_reset)
  2159. host->ops->hw_reset(host);
  2160. }
  2161. static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
  2162. {
  2163. if (!(host->flags & SDHCI_DEVICE_DEAD)) {
  2164. if (enable)
  2165. host->ier |= SDHCI_INT_CARD_INT;
  2166. else
  2167. host->ier &= ~SDHCI_INT_CARD_INT;
  2168. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  2169. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  2170. }
  2171. }
  2172. void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  2173. {
  2174. struct sdhci_host *host = mmc_priv(mmc);
  2175. unsigned long flags;
  2176. if (enable)
  2177. pm_runtime_get_noresume(mmc_dev(mmc));
  2178. spin_lock_irqsave(&host->lock, flags);
  2179. sdhci_enable_sdio_irq_nolock(host, enable);
  2180. spin_unlock_irqrestore(&host->lock, flags);
  2181. if (!enable)
  2182. pm_runtime_put_noidle(mmc_dev(mmc));
  2183. }
  2184. EXPORT_SYMBOL_GPL(sdhci_enable_sdio_irq);
  2185. static void sdhci_ack_sdio_irq(struct mmc_host *mmc)
  2186. {
  2187. struct sdhci_host *host = mmc_priv(mmc);
  2188. unsigned long flags;
  2189. spin_lock_irqsave(&host->lock, flags);
  2190. sdhci_enable_sdio_irq_nolock(host, true);
  2191. spin_unlock_irqrestore(&host->lock, flags);
  2192. }
  2193. int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
  2194. struct mmc_ios *ios)
  2195. {
  2196. struct sdhci_host *host = mmc_priv(mmc);
  2197. u16 ctrl;
  2198. int ret;
  2199. /*
  2200. * Signal Voltage Switching is only applicable for Host Controllers
  2201. * v3.00 and above.
  2202. */
  2203. if (host->version < SDHCI_SPEC_300)
  2204. return 0;
  2205. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  2206. switch (ios->signal_voltage) {
  2207. case MMC_SIGNAL_VOLTAGE_330:
  2208. if (!(host->flags & SDHCI_SIGNALING_330))
  2209. return -EINVAL;
  2210. /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
  2211. ctrl &= ~SDHCI_CTRL_VDD_180;
  2212. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  2213. if (!IS_ERR(mmc->supply.vqmmc)) {
  2214. ret = mmc_regulator_set_vqmmc(mmc, ios);
  2215. if (ret < 0) {
  2216. pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
  2217. mmc_hostname(mmc));
  2218. return -EIO;
  2219. }
  2220. }
  2221. /* Wait for 5ms */
  2222. usleep_range(5000, 5500);
  2223. /* 3.3V regulator output should be stable within 5 ms */
  2224. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  2225. if (!(ctrl & SDHCI_CTRL_VDD_180))
  2226. return 0;
  2227. pr_warn("%s: 3.3V regulator output did not become stable\n",
  2228. mmc_hostname(mmc));
  2229. return -EAGAIN;
  2230. case MMC_SIGNAL_VOLTAGE_180:
  2231. if (!(host->flags & SDHCI_SIGNALING_180))
  2232. return -EINVAL;
  2233. if (!IS_ERR(mmc->supply.vqmmc)) {
  2234. ret = mmc_regulator_set_vqmmc(mmc, ios);
  2235. if (ret < 0) {
  2236. pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
  2237. mmc_hostname(mmc));
  2238. return -EIO;
  2239. }
  2240. }
  2241. /*
  2242. * Enable 1.8V Signal Enable in the Host Control2
  2243. * register
  2244. */
  2245. ctrl |= SDHCI_CTRL_VDD_180;
  2246. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  2247. /* Some controller need to do more when switching */
  2248. if (host->ops->voltage_switch)
  2249. host->ops->voltage_switch(host);
  2250. /* 1.8V regulator output should be stable within 5 ms */
  2251. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  2252. if (ctrl & SDHCI_CTRL_VDD_180)
  2253. return 0;
  2254. pr_warn("%s: 1.8V regulator output did not become stable\n",
  2255. mmc_hostname(mmc));
  2256. return -EAGAIN;
  2257. case MMC_SIGNAL_VOLTAGE_120:
  2258. if (!(host->flags & SDHCI_SIGNALING_120))
  2259. return -EINVAL;
  2260. if (!IS_ERR(mmc->supply.vqmmc)) {
  2261. ret = mmc_regulator_set_vqmmc(mmc, ios);
  2262. if (ret < 0) {
  2263. pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
  2264. mmc_hostname(mmc));
  2265. return -EIO;
  2266. }
  2267. }
  2268. return 0;
  2269. default:
  2270. /* No signal voltage switch required */
  2271. return 0;
  2272. }
  2273. }
  2274. EXPORT_SYMBOL_GPL(sdhci_start_signal_voltage_switch);
  2275. static int sdhci_card_busy(struct mmc_host *mmc)
  2276. {
  2277. struct sdhci_host *host = mmc_priv(mmc);
  2278. u32 present_state;
  2279. /* Check whether DAT[0] is 0 */
  2280. present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
  2281. return !(present_state & SDHCI_DATA_0_LVL_MASK);
  2282. }
  2283. static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
  2284. {
  2285. struct sdhci_host *host = mmc_priv(mmc);
  2286. unsigned long flags;
  2287. spin_lock_irqsave(&host->lock, flags);
  2288. host->flags |= SDHCI_HS400_TUNING;
  2289. spin_unlock_irqrestore(&host->lock, flags);
  2290. return 0;
  2291. }
  2292. void sdhci_start_tuning(struct sdhci_host *host)
  2293. {
  2294. u16 ctrl;
  2295. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  2296. ctrl |= SDHCI_CTRL_EXEC_TUNING;
  2297. if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
  2298. ctrl |= SDHCI_CTRL_TUNED_CLK;
  2299. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  2300. /*
  2301. * As per the Host Controller spec v3.00, tuning command
  2302. * generates Buffer Read Ready interrupt, so enable that.
  2303. *
  2304. * Note: The spec clearly says that when tuning sequence
  2305. * is being performed, the controller does not generate
  2306. * interrupts other than Buffer Read Ready interrupt. But
  2307. * to make sure we don't hit a controller bug, we _only_
  2308. * enable Buffer Read Ready interrupt here.
  2309. */
  2310. sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
  2311. sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
  2312. }
  2313. EXPORT_SYMBOL_GPL(sdhci_start_tuning);
  2314. void sdhci_end_tuning(struct sdhci_host *host)
  2315. {
  2316. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  2317. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  2318. }
  2319. EXPORT_SYMBOL_GPL(sdhci_end_tuning);
  2320. void sdhci_reset_tuning(struct sdhci_host *host)
  2321. {
  2322. u16 ctrl;
  2323. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  2324. ctrl &= ~SDHCI_CTRL_TUNED_CLK;
  2325. ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
  2326. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  2327. }
  2328. EXPORT_SYMBOL_GPL(sdhci_reset_tuning);
  2329. void sdhci_abort_tuning(struct sdhci_host *host, u32 opcode)
  2330. {
  2331. sdhci_reset_tuning(host);
  2332. sdhci_reset_for(host, TUNING_ABORT);
  2333. sdhci_end_tuning(host);
  2334. mmc_send_abort_tuning(host->mmc, opcode);
  2335. }
  2336. EXPORT_SYMBOL_GPL(sdhci_abort_tuning);
  2337. /*
  2338. * We use sdhci_send_tuning() because mmc_send_tuning() is not a good fit. SDHCI
  2339. * tuning command does not have a data payload (or rather the hardware does it
  2340. * automatically) so mmc_send_tuning() will return -EIO. Also the tuning command
  2341. * interrupt setup is different to other commands and there is no timeout
  2342. * interrupt so special handling is needed.
  2343. */
  2344. void sdhci_send_tuning(struct sdhci_host *host, u32 opcode)
  2345. {
  2346. struct mmc_host *mmc = host->mmc;
  2347. struct mmc_command cmd = {};
  2348. struct mmc_request mrq = {};
  2349. unsigned long flags;
  2350. u32 b = host->sdma_boundary;
  2351. spin_lock_irqsave(&host->lock, flags);
  2352. cmd.opcode = opcode;
  2353. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  2354. cmd.mrq = &mrq;
  2355. mrq.cmd = &cmd;
  2356. /*
  2357. * In response to CMD19, the card sends 64 bytes of tuning
  2358. * block to the Host Controller. So we set the block size
  2359. * to 64 here.
  2360. */
  2361. if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
  2362. mmc->ios.bus_width == MMC_BUS_WIDTH_8)
  2363. sdhci_writew(host, SDHCI_MAKE_BLKSZ(b, 128), SDHCI_BLOCK_SIZE);
  2364. else
  2365. sdhci_writew(host, SDHCI_MAKE_BLKSZ(b, 64), SDHCI_BLOCK_SIZE);
  2366. /*
  2367. * The tuning block is sent by the card to the host controller.
  2368. * So we set the TRNS_READ bit in the Transfer Mode register.
  2369. * This also takes care of setting DMA Enable and Multi Block
  2370. * Select in the same register to 0.
  2371. */
  2372. sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
  2373. if (!sdhci_send_command_retry(host, &cmd, flags)) {
  2374. spin_unlock_irqrestore(&host->lock, flags);
  2375. host->tuning_done = 0;
  2376. return;
  2377. }
  2378. host->cmd = NULL;
  2379. sdhci_del_timer(host, &mrq);
  2380. host->tuning_done = 0;
  2381. spin_unlock_irqrestore(&host->lock, flags);
  2382. /* Wait for Buffer Read Ready interrupt */
  2383. wait_event_timeout(host->buf_ready_int, (host->tuning_done == 1),
  2384. msecs_to_jiffies(50));
  2385. }
  2386. EXPORT_SYMBOL_GPL(sdhci_send_tuning);
  2387. int __sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
  2388. {
  2389. int i;
  2390. /*
  2391. * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
  2392. * of loops reaches tuning loop count.
  2393. */
  2394. for (i = 0; i < host->tuning_loop_count; i++) {
  2395. u16 ctrl;
  2396. sdhci_send_tuning(host, opcode);
  2397. if (!host->tuning_done) {
  2398. pr_debug("%s: Tuning timeout, falling back to fixed sampling clock\n",
  2399. mmc_hostname(host->mmc));
  2400. sdhci_abort_tuning(host, opcode);
  2401. return -ETIMEDOUT;
  2402. }
  2403. /* Spec does not require a delay between tuning cycles */
  2404. if (host->tuning_delay > 0)
  2405. mdelay(host->tuning_delay);
  2406. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  2407. if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
  2408. if (ctrl & SDHCI_CTRL_TUNED_CLK)
  2409. return 0; /* Success! */
  2410. break;
  2411. }
  2412. }
  2413. pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
  2414. mmc_hostname(host->mmc));
  2415. sdhci_reset_tuning(host);
  2416. return -EAGAIN;
  2417. }
  2418. EXPORT_SYMBOL_GPL(__sdhci_execute_tuning);
  2419. int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
  2420. {
  2421. struct sdhci_host *host = mmc_priv(mmc);
  2422. int err = 0;
  2423. unsigned int tuning_count = 0;
  2424. bool hs400_tuning;
  2425. hs400_tuning = host->flags & SDHCI_HS400_TUNING;
  2426. if (host->tuning_mode == SDHCI_TUNING_MODE_1)
  2427. tuning_count = host->tuning_count;
  2428. /*
  2429. * The Host Controller needs tuning in case of SDR104 and DDR50
  2430. * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
  2431. * the Capabilities register.
  2432. * If the Host Controller supports the HS200 mode then the
  2433. * tuning function has to be executed.
  2434. */
  2435. switch (host->timing) {
  2436. /* HS400 tuning is done in HS200 mode */
  2437. case MMC_TIMING_MMC_HS400:
  2438. err = -EINVAL;
  2439. goto out;
  2440. case MMC_TIMING_MMC_HS200:
  2441. /*
  2442. * Periodic re-tuning for HS400 is not expected to be needed, so
  2443. * disable it here.
  2444. */
  2445. if (hs400_tuning)
  2446. tuning_count = 0;
  2447. break;
  2448. case MMC_TIMING_UHS_SDR104:
  2449. case MMC_TIMING_UHS_DDR50:
  2450. break;
  2451. case MMC_TIMING_UHS_SDR50:
  2452. if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
  2453. break;
  2454. fallthrough;
  2455. default:
  2456. goto out;
  2457. }
  2458. if (host->ops->platform_execute_tuning) {
  2459. err = host->ops->platform_execute_tuning(host, opcode);
  2460. goto out;
  2461. }
  2462. mmc->retune_period = tuning_count;
  2463. if (host->tuning_delay < 0)
  2464. host->tuning_delay = opcode == MMC_SEND_TUNING_BLOCK;
  2465. sdhci_start_tuning(host);
  2466. host->tuning_err = __sdhci_execute_tuning(host, opcode);
  2467. sdhci_end_tuning(host);
  2468. out:
  2469. host->flags &= ~SDHCI_HS400_TUNING;
  2470. return err;
  2471. }
  2472. EXPORT_SYMBOL_GPL(sdhci_execute_tuning);
  2473. void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
  2474. {
  2475. /* Host Controller v3.00 defines preset value registers */
  2476. if (host->version < SDHCI_SPEC_300)
  2477. return;
  2478. /*
  2479. * We only enable or disable Preset Value if they are not already
  2480. * enabled or disabled respectively. Otherwise, we bail out.
  2481. */
  2482. if (host->preset_enabled != enable) {
  2483. u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  2484. if (enable)
  2485. ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
  2486. else
  2487. ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
  2488. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  2489. if (enable)
  2490. host->flags |= SDHCI_PV_ENABLED;
  2491. else
  2492. host->flags &= ~SDHCI_PV_ENABLED;
  2493. host->preset_enabled = enable;
  2494. }
  2495. }
  2496. EXPORT_SYMBOL_GPL(sdhci_enable_preset_value);
  2497. static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
  2498. int err)
  2499. {
  2500. struct mmc_data *data = mrq->data;
  2501. if (data->host_cookie != COOKIE_UNMAPPED)
  2502. dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len,
  2503. mmc_get_dma_dir(data));
  2504. data->host_cookie = COOKIE_UNMAPPED;
  2505. }
  2506. static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
  2507. {
  2508. struct sdhci_host *host = mmc_priv(mmc);
  2509. mrq->data->host_cookie = COOKIE_UNMAPPED;
  2510. /*
  2511. * No pre-mapping in the pre hook if we're using the bounce buffer,
  2512. * for that we would need two bounce buffers since one buffer is
  2513. * in flight when this is getting called.
  2514. */
  2515. if (host->flags & SDHCI_REQ_USE_DMA && !host->bounce_buffer)
  2516. sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
  2517. }
  2518. static void sdhci_error_out_mrqs(struct sdhci_host *host, int err)
  2519. {
  2520. if (host->data_cmd) {
  2521. host->data_cmd->error = err;
  2522. sdhci_finish_mrq(host, host->data_cmd->mrq);
  2523. }
  2524. if (host->cmd) {
  2525. host->cmd->error = err;
  2526. sdhci_finish_mrq(host, host->cmd->mrq);
  2527. }
  2528. }
  2529. static void sdhci_card_event(struct mmc_host *mmc)
  2530. {
  2531. struct sdhci_host *host = mmc_priv(mmc);
  2532. unsigned long flags;
  2533. int present;
  2534. /* First check if client has provided their own card event */
  2535. if (host->ops->card_event)
  2536. host->ops->card_event(host);
  2537. present = mmc->ops->get_cd(mmc);
  2538. spin_lock_irqsave(&host->lock, flags);
  2539. /* Check sdhci_has_requests() first in case we are runtime suspended */
  2540. if (sdhci_has_requests(host) && !present) {
  2541. pr_err("%s: Card removed during transfer!\n",
  2542. mmc_hostname(mmc));
  2543. pr_err("%s: Resetting controller.\n",
  2544. mmc_hostname(mmc));
  2545. sdhci_reset_for(host, CARD_REMOVED);
  2546. sdhci_error_out_mrqs(host, -ENOMEDIUM);
  2547. }
  2548. spin_unlock_irqrestore(&host->lock, flags);
  2549. }
  2550. static const struct mmc_host_ops sdhci_ops = {
  2551. .request = sdhci_request,
  2552. .post_req = sdhci_post_req,
  2553. .pre_req = sdhci_pre_req,
  2554. .set_ios = sdhci_set_ios,
  2555. .get_cd = sdhci_get_cd,
  2556. .get_ro = sdhci_get_ro,
  2557. .card_hw_reset = sdhci_hw_reset,
  2558. .enable_sdio_irq = sdhci_enable_sdio_irq,
  2559. .ack_sdio_irq = sdhci_ack_sdio_irq,
  2560. .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
  2561. .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
  2562. .execute_tuning = sdhci_execute_tuning,
  2563. .card_event = sdhci_card_event,
  2564. .card_busy = sdhci_card_busy,
  2565. };
  2566. /*****************************************************************************\
  2567. * *
  2568. * Request done *
  2569. * *
  2570. \*****************************************************************************/
  2571. void sdhci_request_done_dma(struct sdhci_host *host, struct mmc_request *mrq)
  2572. {
  2573. struct mmc_data *data = mrq->data;
  2574. if (data && data->host_cookie == COOKIE_MAPPED) {
  2575. if (host->bounce_buffer) {
  2576. /*
  2577. * On reads, copy the bounced data into the
  2578. * sglist
  2579. */
  2580. if (mmc_get_dma_dir(data) == DMA_FROM_DEVICE) {
  2581. unsigned int length = data->bytes_xfered;
  2582. if (length > host->bounce_buffer_size) {
  2583. pr_err("%s: bounce buffer is %u bytes but DMA claims to have transferred %u bytes\n",
  2584. mmc_hostname(host->mmc),
  2585. host->bounce_buffer_size,
  2586. data->bytes_xfered);
  2587. /* Cap it down and continue */
  2588. length = host->bounce_buffer_size;
  2589. }
  2590. dma_sync_single_for_cpu(mmc_dev(host->mmc),
  2591. host->bounce_addr,
  2592. host->bounce_buffer_size,
  2593. DMA_FROM_DEVICE);
  2594. sg_copy_from_buffer(data->sg,
  2595. data->sg_len,
  2596. host->bounce_buffer,
  2597. length);
  2598. } else {
  2599. /* No copying, just switch ownership */
  2600. dma_sync_single_for_cpu(mmc_dev(host->mmc),
  2601. host->bounce_addr,
  2602. host->bounce_buffer_size,
  2603. mmc_get_dma_dir(data));
  2604. }
  2605. } else {
  2606. /* Unmap the raw data */
  2607. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  2608. data->sg_len,
  2609. mmc_get_dma_dir(data));
  2610. }
  2611. data->host_cookie = COOKIE_UNMAPPED;
  2612. }
  2613. }
  2614. EXPORT_SYMBOL_GPL(sdhci_request_done_dma);
  2615. static bool sdhci_request_done(struct sdhci_host *host)
  2616. {
  2617. unsigned long flags;
  2618. struct mmc_request *mrq;
  2619. int i;
  2620. spin_lock_irqsave(&host->lock, flags);
  2621. for (i = 0; i < SDHCI_MAX_MRQS; i++) {
  2622. mrq = host->mrqs_done[i];
  2623. if (mrq)
  2624. break;
  2625. }
  2626. if (!mrq) {
  2627. spin_unlock_irqrestore(&host->lock, flags);
  2628. return true;
  2629. }
  2630. /*
  2631. * The controller needs a reset of internal state machines
  2632. * upon error conditions.
  2633. */
  2634. if (sdhci_needs_reset(host, mrq)) {
  2635. /*
  2636. * Do not finish until command and data lines are available for
  2637. * reset. Note there can only be one other mrq, so it cannot
  2638. * also be in mrqs_done, otherwise host->cmd and host->data_cmd
  2639. * would both be null.
  2640. */
  2641. if (host->cmd || host->data_cmd) {
  2642. spin_unlock_irqrestore(&host->lock, flags);
  2643. return true;
  2644. }
  2645. /* Some controllers need this kick or reset won't work here */
  2646. if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
  2647. /* This is to force an update */
  2648. host->ops->set_clock(host, host->clock);
  2649. sdhci_reset_for(host, REQUEST_ERROR);
  2650. host->pending_reset = false;
  2651. }
  2652. /*
  2653. * Always unmap the data buffers if they were mapped by
  2654. * sdhci_prepare_data() whenever we finish with a request.
  2655. * This avoids leaking DMA mappings on error.
  2656. */
  2657. if (host->flags & SDHCI_REQ_USE_DMA) {
  2658. struct mmc_data *data = mrq->data;
  2659. if (host->use_external_dma && data &&
  2660. (mrq->cmd->error || data->error)) {
  2661. struct dma_chan *chan = sdhci_external_dma_channel(host, data);
  2662. host->mrqs_done[i] = NULL;
  2663. spin_unlock_irqrestore(&host->lock, flags);
  2664. dmaengine_terminate_sync(chan);
  2665. spin_lock_irqsave(&host->lock, flags);
  2666. sdhci_set_mrq_done(host, mrq);
  2667. }
  2668. sdhci_request_done_dma(host, mrq);
  2669. }
  2670. host->mrqs_done[i] = NULL;
  2671. spin_unlock_irqrestore(&host->lock, flags);
  2672. if (host->ops->request_done)
  2673. host->ops->request_done(host, mrq);
  2674. else
  2675. mmc_request_done(host->mmc, mrq);
  2676. return false;
  2677. }
  2678. void sdhci_complete_work(struct work_struct *work)
  2679. {
  2680. struct sdhci_host *host = container_of(work, struct sdhci_host,
  2681. complete_work);
  2682. while (!sdhci_request_done(host))
  2683. ;
  2684. }
  2685. EXPORT_SYMBOL_GPL(sdhci_complete_work);
  2686. static void sdhci_timeout_timer(struct timer_list *t)
  2687. {
  2688. struct sdhci_host *host;
  2689. unsigned long flags;
  2690. host = timer_container_of(host, t, timer);
  2691. spin_lock_irqsave(&host->lock, flags);
  2692. if (host->cmd && !sdhci_data_line_cmd(host->cmd)) {
  2693. pr_err("%s: Timeout waiting for hardware cmd interrupt.\n",
  2694. mmc_hostname(host->mmc));
  2695. sdhci_err_stats_inc(host, REQ_TIMEOUT);
  2696. sdhci_dumpregs(host);
  2697. host->cmd->error = -ETIMEDOUT;
  2698. sdhci_finish_mrq(host, host->cmd->mrq);
  2699. }
  2700. spin_unlock_irqrestore(&host->lock, flags);
  2701. }
  2702. static void sdhci_timeout_data_timer(struct timer_list *t)
  2703. {
  2704. struct sdhci_host *host;
  2705. unsigned long flags;
  2706. host = timer_container_of(host, t, data_timer);
  2707. spin_lock_irqsave(&host->lock, flags);
  2708. if (host->data || host->data_cmd ||
  2709. (host->cmd && sdhci_data_line_cmd(host->cmd))) {
  2710. pr_err("%s: Timeout waiting for hardware interrupt.\n",
  2711. mmc_hostname(host->mmc));
  2712. sdhci_err_stats_inc(host, REQ_TIMEOUT);
  2713. sdhci_dumpregs(host);
  2714. if (host->data) {
  2715. host->data->error = -ETIMEDOUT;
  2716. __sdhci_finish_data(host, true);
  2717. queue_work(host->complete_wq, &host->complete_work);
  2718. } else if (host->data_cmd) {
  2719. host->data_cmd->error = -ETIMEDOUT;
  2720. sdhci_finish_mrq(host, host->data_cmd->mrq);
  2721. } else {
  2722. host->cmd->error = -ETIMEDOUT;
  2723. sdhci_finish_mrq(host, host->cmd->mrq);
  2724. }
  2725. }
  2726. spin_unlock_irqrestore(&host->lock, flags);
  2727. }
  2728. /*****************************************************************************\
  2729. * *
  2730. * Interrupt handling *
  2731. * *
  2732. \*****************************************************************************/
  2733. static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *intmask_p)
  2734. {
  2735. /* Handle auto-CMD12 error */
  2736. if (intmask & SDHCI_INT_AUTO_CMD_ERR && host->data_cmd) {
  2737. struct mmc_request *mrq = host->data_cmd->mrq;
  2738. u16 auto_cmd_status = sdhci_readw(host, SDHCI_AUTO_CMD_STATUS);
  2739. int data_err_bit = (auto_cmd_status & SDHCI_AUTO_CMD_TIMEOUT) ?
  2740. SDHCI_INT_DATA_TIMEOUT :
  2741. SDHCI_INT_DATA_CRC;
  2742. /* Treat auto-CMD12 error the same as data error */
  2743. if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
  2744. *intmask_p |= data_err_bit;
  2745. return;
  2746. }
  2747. }
  2748. if (!host->cmd) {
  2749. /*
  2750. * SDHCI recovers from errors by resetting the cmd and data
  2751. * circuits. Until that is done, there very well might be more
  2752. * interrupts, so ignore them in that case.
  2753. */
  2754. if (host->pending_reset)
  2755. return;
  2756. pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
  2757. mmc_hostname(host->mmc), (unsigned)intmask);
  2758. sdhci_err_stats_inc(host, UNEXPECTED_IRQ);
  2759. sdhci_dumpregs(host);
  2760. return;
  2761. }
  2762. if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
  2763. SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
  2764. if (intmask & SDHCI_INT_TIMEOUT) {
  2765. host->cmd->error = -ETIMEDOUT;
  2766. sdhci_err_stats_inc(host, CMD_TIMEOUT);
  2767. } else {
  2768. host->cmd->error = -EILSEQ;
  2769. if (!mmc_op_tuning(host->cmd->opcode))
  2770. sdhci_err_stats_inc(host, CMD_CRC);
  2771. }
  2772. /* Treat data command CRC error the same as data CRC error */
  2773. if (host->cmd->data &&
  2774. (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
  2775. SDHCI_INT_CRC) {
  2776. host->cmd = NULL;
  2777. *intmask_p |= SDHCI_INT_DATA_CRC;
  2778. return;
  2779. }
  2780. __sdhci_finish_mrq(host, host->cmd->mrq);
  2781. return;
  2782. }
  2783. /* Handle auto-CMD23 error */
  2784. if (intmask & SDHCI_INT_AUTO_CMD_ERR) {
  2785. struct mmc_request *mrq = host->cmd->mrq;
  2786. u16 auto_cmd_status = sdhci_readw(host, SDHCI_AUTO_CMD_STATUS);
  2787. int err = (auto_cmd_status & SDHCI_AUTO_CMD_TIMEOUT) ?
  2788. -ETIMEDOUT :
  2789. -EILSEQ;
  2790. sdhci_err_stats_inc(host, AUTO_CMD);
  2791. if (sdhci_auto_cmd23(host, mrq)) {
  2792. mrq->sbc->error = err;
  2793. __sdhci_finish_mrq(host, mrq);
  2794. return;
  2795. }
  2796. }
  2797. if (intmask & SDHCI_INT_RESPONSE)
  2798. sdhci_finish_command(host);
  2799. }
  2800. static void sdhci_adma_show_error(struct sdhci_host *host)
  2801. {
  2802. void *desc = host->adma_table;
  2803. dma_addr_t dma = host->adma_addr;
  2804. sdhci_dumpregs(host);
  2805. while (true) {
  2806. struct sdhci_adma2_64_desc *dma_desc = desc;
  2807. if (host->flags & SDHCI_USE_64_BIT_DMA)
  2808. SDHCI_DUMP("%08llx: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
  2809. (unsigned long long)dma,
  2810. le32_to_cpu(dma_desc->addr_hi),
  2811. le32_to_cpu(dma_desc->addr_lo),
  2812. le16_to_cpu(dma_desc->len),
  2813. le16_to_cpu(dma_desc->cmd));
  2814. else
  2815. SDHCI_DUMP("%08llx: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
  2816. (unsigned long long)dma,
  2817. le32_to_cpu(dma_desc->addr_lo),
  2818. le16_to_cpu(dma_desc->len),
  2819. le16_to_cpu(dma_desc->cmd));
  2820. desc += host->desc_sz;
  2821. dma += host->desc_sz;
  2822. if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
  2823. break;
  2824. }
  2825. }
  2826. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  2827. {
  2828. /*
  2829. * CMD19 generates _only_ Buffer Read Ready interrupt if
  2830. * use sdhci_send_tuning.
  2831. * Need to exclude this case: PIO mode and use mmc_send_tuning,
  2832. * If not, sdhci_transfer_pio will never be called, make the
  2833. * SDHCI_INT_DATA_AVAIL always there, stuck in irq storm.
  2834. */
  2835. if (intmask & SDHCI_INT_DATA_AVAIL && !host->data) {
  2836. if (mmc_op_tuning(SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)))) {
  2837. host->tuning_done = 1;
  2838. wake_up(&host->buf_ready_int);
  2839. return;
  2840. }
  2841. }
  2842. if (!host->data) {
  2843. struct mmc_command *data_cmd = host->data_cmd;
  2844. /*
  2845. * The "data complete" interrupt is also used to
  2846. * indicate that a busy state has ended. See comment
  2847. * above in sdhci_cmd_irq().
  2848. */
  2849. if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) {
  2850. if (intmask & SDHCI_INT_DATA_TIMEOUT) {
  2851. host->data_cmd = NULL;
  2852. data_cmd->error = -ETIMEDOUT;
  2853. sdhci_err_stats_inc(host, CMD_TIMEOUT);
  2854. __sdhci_finish_mrq(host, data_cmd->mrq);
  2855. return;
  2856. }
  2857. if (intmask & SDHCI_INT_DATA_END) {
  2858. host->data_cmd = NULL;
  2859. /*
  2860. * Some cards handle busy-end interrupt
  2861. * before the command completed, so make
  2862. * sure we do things in the proper order.
  2863. */
  2864. if (host->cmd == data_cmd)
  2865. return;
  2866. __sdhci_finish_mrq(host, data_cmd->mrq);
  2867. return;
  2868. }
  2869. }
  2870. /*
  2871. * SDHCI recovers from errors by resetting the cmd and data
  2872. * circuits. Until that is done, there very well might be more
  2873. * interrupts, so ignore them in that case.
  2874. */
  2875. if (host->pending_reset)
  2876. return;
  2877. pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
  2878. mmc_hostname(host->mmc), (unsigned)intmask);
  2879. sdhci_err_stats_inc(host, UNEXPECTED_IRQ);
  2880. sdhci_dumpregs(host);
  2881. return;
  2882. }
  2883. if (intmask & SDHCI_INT_DATA_TIMEOUT) {
  2884. host->data->error = -ETIMEDOUT;
  2885. sdhci_err_stats_inc(host, DAT_TIMEOUT);
  2886. } else if (intmask & SDHCI_INT_DATA_END_BIT) {
  2887. host->data->error = -EILSEQ;
  2888. if (!mmc_op_tuning(SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))))
  2889. sdhci_err_stats_inc(host, DAT_CRC);
  2890. } else if ((intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_TUNING_ERROR)) &&
  2891. SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
  2892. != MMC_BUS_TEST_R) {
  2893. host->data->error = -EILSEQ;
  2894. if (!mmc_op_tuning(SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))))
  2895. sdhci_err_stats_inc(host, DAT_CRC);
  2896. if (intmask & SDHCI_INT_TUNING_ERROR) {
  2897. u16 ctrl2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  2898. ctrl2 &= ~SDHCI_CTRL_TUNED_CLK;
  2899. sdhci_writew(host, ctrl2, SDHCI_HOST_CONTROL2);
  2900. }
  2901. } else if (intmask & SDHCI_INT_ADMA_ERROR) {
  2902. pr_err("%s: ADMA error: 0x%08x\n", mmc_hostname(host->mmc),
  2903. intmask);
  2904. sdhci_adma_show_error(host);
  2905. sdhci_err_stats_inc(host, ADMA);
  2906. host->data->error = -EIO;
  2907. if (host->ops->adma_workaround)
  2908. host->ops->adma_workaround(host, intmask);
  2909. }
  2910. if (host->data->error)
  2911. sdhci_finish_data(host);
  2912. else {
  2913. if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  2914. sdhci_transfer_pio(host);
  2915. /*
  2916. * We currently don't do anything fancy with DMA
  2917. * boundaries, but as we can't disable the feature
  2918. * we need to at least restart the transfer.
  2919. *
  2920. * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
  2921. * should return a valid address to continue from, but as
  2922. * some controllers are faulty, don't trust them.
  2923. */
  2924. if (intmask & SDHCI_INT_DMA_END) {
  2925. dma_addr_t dmastart, dmanow;
  2926. dmastart = sdhci_sdma_address(host);
  2927. dmanow = dmastart + host->data->bytes_xfered;
  2928. /*
  2929. * Force update to the next DMA block boundary.
  2930. */
  2931. dmanow = (dmanow &
  2932. ~((dma_addr_t)SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
  2933. SDHCI_DEFAULT_BOUNDARY_SIZE;
  2934. host->data->bytes_xfered = dmanow - dmastart;
  2935. DBG("DMA base %pad, transferred 0x%06x bytes, next %pad\n",
  2936. &dmastart, host->data->bytes_xfered, &dmanow);
  2937. sdhci_set_sdma_addr(host, dmanow);
  2938. }
  2939. if (intmask & SDHCI_INT_DATA_END) {
  2940. if (host->cmd == host->data_cmd) {
  2941. /*
  2942. * Data managed to finish before the
  2943. * command completed. Make sure we do
  2944. * things in the proper order.
  2945. */
  2946. host->data_early = 1;
  2947. } else {
  2948. sdhci_finish_data(host);
  2949. }
  2950. }
  2951. }
  2952. }
  2953. static inline bool sdhci_defer_done(struct sdhci_host *host,
  2954. struct mmc_request *mrq)
  2955. {
  2956. struct mmc_data *data = mrq->data;
  2957. return host->pending_reset || host->always_defer_done ||
  2958. ((host->flags & SDHCI_REQ_USE_DMA) && data &&
  2959. data->host_cookie == COOKIE_MAPPED);
  2960. }
  2961. static irqreturn_t sdhci_irq(int irq, void *dev_id)
  2962. {
  2963. struct mmc_request *mrqs_done[SDHCI_MAX_MRQS] = {0};
  2964. irqreturn_t result = IRQ_NONE;
  2965. struct sdhci_host *host = dev_id;
  2966. u32 intmask, mask, unexpected = 0;
  2967. int max_loops = 16;
  2968. int i;
  2969. spin_lock(&host->lock);
  2970. if (host->runtime_suspended) {
  2971. spin_unlock(&host->lock);
  2972. return IRQ_NONE;
  2973. }
  2974. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  2975. if (!intmask || intmask == 0xffffffff) {
  2976. result = IRQ_NONE;
  2977. goto out;
  2978. }
  2979. do {
  2980. DBG("IRQ status 0x%08x\n", intmask);
  2981. if (host->ops->irq) {
  2982. intmask = host->ops->irq(host, intmask);
  2983. if (!intmask)
  2984. goto cont;
  2985. }
  2986. /* Clear selected interrupts. */
  2987. mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  2988. SDHCI_INT_BUS_POWER);
  2989. sdhci_writel(host, mask, SDHCI_INT_STATUS);
  2990. if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  2991. u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  2992. SDHCI_CARD_PRESENT;
  2993. /*
  2994. * There is a observation on i.mx esdhc. INSERT
  2995. * bit will be immediately set again when it gets
  2996. * cleared, if a card is inserted. We have to mask
  2997. * the irq to prevent interrupt storm which will
  2998. * freeze the system. And the REMOVE gets the
  2999. * same situation.
  3000. *
  3001. * More testing are needed here to ensure it works
  3002. * for other platforms though.
  3003. */
  3004. host->ier &= ~(SDHCI_INT_CARD_INSERT |
  3005. SDHCI_INT_CARD_REMOVE);
  3006. host->ier |= present ? SDHCI_INT_CARD_REMOVE :
  3007. SDHCI_INT_CARD_INSERT;
  3008. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  3009. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  3010. sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
  3011. SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
  3012. host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
  3013. SDHCI_INT_CARD_REMOVE);
  3014. result = IRQ_WAKE_THREAD;
  3015. }
  3016. if (intmask & SDHCI_INT_CMD_MASK)
  3017. sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK, &intmask);
  3018. if (intmask & SDHCI_INT_DATA_MASK)
  3019. sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  3020. if (intmask & SDHCI_INT_BUS_POWER)
  3021. pr_err("%s: Card is consuming too much power!\n",
  3022. mmc_hostname(host->mmc));
  3023. if (intmask & SDHCI_INT_RETUNE)
  3024. mmc_retune_needed(host->mmc);
  3025. if ((intmask & SDHCI_INT_CARD_INT) &&
  3026. (host->ier & SDHCI_INT_CARD_INT)) {
  3027. sdhci_enable_sdio_irq_nolock(host, false);
  3028. sdio_signal_irq(host->mmc);
  3029. }
  3030. intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
  3031. SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  3032. SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
  3033. SDHCI_INT_RETUNE | SDHCI_INT_CARD_INT);
  3034. if (intmask) {
  3035. unexpected |= intmask;
  3036. sdhci_writel(host, intmask, SDHCI_INT_STATUS);
  3037. }
  3038. cont:
  3039. if (result == IRQ_NONE)
  3040. result = IRQ_HANDLED;
  3041. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  3042. } while (intmask && --max_loops);
  3043. /* Determine if mrqs can be completed immediately */
  3044. for (i = 0; i < SDHCI_MAX_MRQS; i++) {
  3045. struct mmc_request *mrq = host->mrqs_done[i];
  3046. if (!mrq)
  3047. continue;
  3048. if (sdhci_defer_done(host, mrq)) {
  3049. result = IRQ_WAKE_THREAD;
  3050. } else {
  3051. mrqs_done[i] = mrq;
  3052. host->mrqs_done[i] = NULL;
  3053. }
  3054. }
  3055. out:
  3056. if (host->deferred_cmd)
  3057. result = IRQ_WAKE_THREAD;
  3058. spin_unlock(&host->lock);
  3059. /* Process mrqs ready for immediate completion */
  3060. for (i = 0; i < SDHCI_MAX_MRQS; i++) {
  3061. if (!mrqs_done[i])
  3062. continue;
  3063. if (host->ops->request_done)
  3064. host->ops->request_done(host, mrqs_done[i]);
  3065. else
  3066. mmc_request_done(host->mmc, mrqs_done[i]);
  3067. }
  3068. if (unexpected) {
  3069. pr_err("%s: Unexpected interrupt 0x%08x.\n",
  3070. mmc_hostname(host->mmc), unexpected);
  3071. sdhci_err_stats_inc(host, UNEXPECTED_IRQ);
  3072. sdhci_dumpregs(host);
  3073. }
  3074. return result;
  3075. }
  3076. irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
  3077. {
  3078. struct sdhci_host *host = dev_id;
  3079. struct mmc_command *cmd;
  3080. unsigned long flags;
  3081. u32 isr;
  3082. while (!sdhci_request_done(host))
  3083. ;
  3084. spin_lock_irqsave(&host->lock, flags);
  3085. isr = host->thread_isr;
  3086. host->thread_isr = 0;
  3087. cmd = host->deferred_cmd;
  3088. if (cmd && !sdhci_send_command_retry(host, cmd, flags))
  3089. sdhci_finish_mrq(host, cmd->mrq);
  3090. spin_unlock_irqrestore(&host->lock, flags);
  3091. if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  3092. struct mmc_host *mmc = host->mmc;
  3093. mmc->ops->card_event(mmc);
  3094. mmc_detect_change(mmc, msecs_to_jiffies(200));
  3095. }
  3096. return IRQ_HANDLED;
  3097. }
  3098. EXPORT_SYMBOL_GPL(sdhci_thread_irq);
  3099. /*****************************************************************************\
  3100. * *
  3101. * Suspend/resume *
  3102. * *
  3103. \*****************************************************************************/
  3104. #ifdef CONFIG_PM
  3105. static bool sdhci_cd_irq_can_wakeup(struct sdhci_host *host)
  3106. {
  3107. return mmc_card_is_removable(host->mmc) &&
  3108. !(host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
  3109. !mmc_host_can_gpio_cd(host->mmc);
  3110. }
  3111. /*
  3112. * To enable wakeup events, the corresponding events have to be enabled in
  3113. * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
  3114. * Table' in the SD Host Controller Standard Specification.
  3115. * It is useless to restore SDHCI_INT_ENABLE state in
  3116. * sdhci_disable_irq_wakeups() since it will be set by
  3117. * sdhci_enable_card_detection() or sdhci_init().
  3118. */
  3119. bool sdhci_enable_irq_wakeups(struct sdhci_host *host)
  3120. {
  3121. u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE |
  3122. SDHCI_WAKE_ON_INT;
  3123. u32 irq_val = 0;
  3124. u8 wake_val = 0;
  3125. u8 val;
  3126. if (sdhci_cd_irq_can_wakeup(host)) {
  3127. wake_val |= SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE;
  3128. irq_val |= SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE;
  3129. }
  3130. if (mmc_card_wake_sdio_irq(host->mmc)) {
  3131. wake_val |= SDHCI_WAKE_ON_INT;
  3132. irq_val |= SDHCI_INT_CARD_INT;
  3133. }
  3134. if (!irq_val)
  3135. return false;
  3136. val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
  3137. val &= ~mask;
  3138. val |= wake_val;
  3139. sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
  3140. sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
  3141. host->irq_wake_enabled = !enable_irq_wake(host->irq);
  3142. return host->irq_wake_enabled;
  3143. }
  3144. EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
  3145. void sdhci_disable_irq_wakeups(struct sdhci_host *host)
  3146. {
  3147. u8 val;
  3148. u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
  3149. | SDHCI_WAKE_ON_INT;
  3150. val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
  3151. val &= ~mask;
  3152. sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
  3153. disable_irq_wake(host->irq);
  3154. host->irq_wake_enabled = false;
  3155. }
  3156. EXPORT_SYMBOL_GPL(sdhci_disable_irq_wakeups);
  3157. int sdhci_suspend_host(struct sdhci_host *host)
  3158. {
  3159. sdhci_disable_card_detection(host);
  3160. mmc_retune_timer_stop(host->mmc);
  3161. if (!device_may_wakeup(mmc_dev(host->mmc)) ||
  3162. !sdhci_enable_irq_wakeups(host)) {
  3163. host->ier = 0;
  3164. sdhci_writel(host, 0, SDHCI_INT_ENABLE);
  3165. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  3166. free_irq(host->irq, host);
  3167. }
  3168. return 0;
  3169. }
  3170. EXPORT_SYMBOL_GPL(sdhci_suspend_host);
  3171. int sdhci_resume_host(struct sdhci_host *host)
  3172. {
  3173. struct mmc_host *mmc = host->mmc;
  3174. int ret = 0;
  3175. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  3176. if (host->ops->enable_dma)
  3177. host->ops->enable_dma(host);
  3178. }
  3179. if ((mmc->pm_flags & MMC_PM_KEEP_POWER) &&
  3180. (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
  3181. /* Card keeps power but host controller does not */
  3182. sdhci_init(host, 0);
  3183. host->pwr = 0;
  3184. host->clock = 0;
  3185. host->reinit_uhs = true;
  3186. mmc->ops->set_ios(mmc, &mmc->ios);
  3187. } else {
  3188. sdhci_init(host, (mmc->pm_flags & MMC_PM_KEEP_POWER));
  3189. }
  3190. if (host->irq_wake_enabled) {
  3191. sdhci_disable_irq_wakeups(host);
  3192. } else {
  3193. ret = request_threaded_irq(host->irq, sdhci_irq,
  3194. sdhci_thread_irq, IRQF_SHARED,
  3195. mmc_hostname(mmc), host);
  3196. if (ret)
  3197. return ret;
  3198. }
  3199. sdhci_enable_card_detection(host);
  3200. return ret;
  3201. }
  3202. EXPORT_SYMBOL_GPL(sdhci_resume_host);
  3203. void sdhci_runtime_suspend_host(struct sdhci_host *host)
  3204. {
  3205. unsigned long flags;
  3206. mmc_retune_timer_stop(host->mmc);
  3207. spin_lock_irqsave(&host->lock, flags);
  3208. host->ier &= SDHCI_INT_CARD_INT;
  3209. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  3210. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  3211. spin_unlock_irqrestore(&host->lock, flags);
  3212. synchronize_hardirq(host->irq);
  3213. spin_lock_irqsave(&host->lock, flags);
  3214. host->runtime_suspended = true;
  3215. spin_unlock_irqrestore(&host->lock, flags);
  3216. }
  3217. EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
  3218. void sdhci_runtime_resume_host(struct sdhci_host *host, int soft_reset)
  3219. {
  3220. struct mmc_host *mmc = host->mmc;
  3221. unsigned long flags;
  3222. int host_flags = host->flags;
  3223. if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  3224. if (host->ops->enable_dma)
  3225. host->ops->enable_dma(host);
  3226. }
  3227. sdhci_init(host, soft_reset);
  3228. if (mmc->ios.power_mode != MMC_POWER_UNDEFINED &&
  3229. mmc->ios.power_mode != MMC_POWER_OFF) {
  3230. /* Force clock and power re-program */
  3231. host->pwr = 0;
  3232. host->clock = 0;
  3233. host->reinit_uhs = true;
  3234. mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
  3235. mmc->ops->set_ios(mmc, &mmc->ios);
  3236. if ((host_flags & SDHCI_PV_ENABLED) &&
  3237. !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
  3238. spin_lock_irqsave(&host->lock, flags);
  3239. sdhci_enable_preset_value(host, true);
  3240. spin_unlock_irqrestore(&host->lock, flags);
  3241. }
  3242. if ((mmc->caps2 & MMC_CAP2_HS400_ES) &&
  3243. mmc->ops->hs400_enhanced_strobe)
  3244. mmc->ops->hs400_enhanced_strobe(mmc, &mmc->ios);
  3245. }
  3246. spin_lock_irqsave(&host->lock, flags);
  3247. host->runtime_suspended = false;
  3248. /* Enable SDIO IRQ */
  3249. if (sdio_irq_claimed(mmc))
  3250. sdhci_enable_sdio_irq_nolock(host, true);
  3251. /* Enable Card Detection */
  3252. sdhci_enable_card_detection(host);
  3253. spin_unlock_irqrestore(&host->lock, flags);
  3254. }
  3255. EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
  3256. #endif /* CONFIG_PM */
  3257. /*****************************************************************************\
  3258. * *
  3259. * Command Queue Engine (CQE) helpers *
  3260. * *
  3261. \*****************************************************************************/
  3262. void sdhci_cqe_enable(struct mmc_host *mmc)
  3263. {
  3264. struct sdhci_host *host = mmc_priv(mmc);
  3265. unsigned long flags;
  3266. u8 ctrl;
  3267. spin_lock_irqsave(&host->lock, flags);
  3268. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  3269. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  3270. /*
  3271. * Host from V4.10 supports ADMA3 DMA type.
  3272. * ADMA3 performs integrated descriptor which is more suitable
  3273. * for cmd queuing to fetch both command and transfer descriptors.
  3274. */
  3275. if (host->v4_mode && (host->caps1 & SDHCI_CAN_DO_ADMA3))
  3276. ctrl |= SDHCI_CTRL_ADMA3;
  3277. else if (host->flags & SDHCI_USE_64_BIT_DMA)
  3278. ctrl |= SDHCI_CTRL_ADMA64;
  3279. else
  3280. ctrl |= SDHCI_CTRL_ADMA32;
  3281. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  3282. sdhci_writew(host, SDHCI_MAKE_BLKSZ(host->sdma_boundary, 512),
  3283. SDHCI_BLOCK_SIZE);
  3284. /* Set maximum timeout */
  3285. sdhci_set_timeout(host, NULL);
  3286. host->ier = host->cqe_ier;
  3287. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  3288. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  3289. host->cqe_on = true;
  3290. pr_debug("%s: sdhci: CQE on, IRQ mask %#x, IRQ status %#x\n",
  3291. mmc_hostname(mmc), host->ier,
  3292. sdhci_readl(host, SDHCI_INT_STATUS));
  3293. spin_unlock_irqrestore(&host->lock, flags);
  3294. }
  3295. EXPORT_SYMBOL_GPL(sdhci_cqe_enable);
  3296. void sdhci_cqe_disable(struct mmc_host *mmc, bool recovery)
  3297. {
  3298. struct sdhci_host *host = mmc_priv(mmc);
  3299. unsigned long flags;
  3300. spin_lock_irqsave(&host->lock, flags);
  3301. sdhci_set_default_irqs(host);
  3302. host->cqe_on = false;
  3303. if (recovery)
  3304. sdhci_reset_for(host, CQE_RECOVERY);
  3305. pr_debug("%s: sdhci: CQE off, IRQ mask %#x, IRQ status %#x\n",
  3306. mmc_hostname(mmc), host->ier,
  3307. sdhci_readl(host, SDHCI_INT_STATUS));
  3308. spin_unlock_irqrestore(&host->lock, flags);
  3309. }
  3310. EXPORT_SYMBOL_GPL(sdhci_cqe_disable);
  3311. bool sdhci_cqe_irq(struct sdhci_host *host, u32 intmask, int *cmd_error,
  3312. int *data_error)
  3313. {
  3314. u32 mask;
  3315. if (!host->cqe_on)
  3316. return false;
  3317. if (intmask & (SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC)) {
  3318. *cmd_error = -EILSEQ;
  3319. if (!mmc_op_tuning(SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))))
  3320. sdhci_err_stats_inc(host, CMD_CRC);
  3321. } else if (intmask & SDHCI_INT_TIMEOUT) {
  3322. *cmd_error = -ETIMEDOUT;
  3323. sdhci_err_stats_inc(host, CMD_TIMEOUT);
  3324. } else
  3325. *cmd_error = 0;
  3326. if (intmask & (SDHCI_INT_DATA_END_BIT | SDHCI_INT_DATA_CRC | SDHCI_INT_TUNING_ERROR)) {
  3327. *data_error = -EILSEQ;
  3328. if (!mmc_op_tuning(SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))))
  3329. sdhci_err_stats_inc(host, DAT_CRC);
  3330. } else if (intmask & SDHCI_INT_DATA_TIMEOUT) {
  3331. *data_error = -ETIMEDOUT;
  3332. sdhci_err_stats_inc(host, DAT_TIMEOUT);
  3333. } else if (intmask & SDHCI_INT_ADMA_ERROR) {
  3334. *data_error = -EIO;
  3335. sdhci_err_stats_inc(host, ADMA);
  3336. } else
  3337. *data_error = 0;
  3338. /* Clear selected interrupts. */
  3339. mask = intmask & host->cqe_ier;
  3340. sdhci_writel(host, mask, SDHCI_INT_STATUS);
  3341. if (intmask & SDHCI_INT_BUS_POWER)
  3342. pr_err("%s: Card is consuming too much power!\n",
  3343. mmc_hostname(host->mmc));
  3344. intmask &= ~(host->cqe_ier | SDHCI_INT_ERROR);
  3345. if (intmask) {
  3346. sdhci_writel(host, intmask, SDHCI_INT_STATUS);
  3347. pr_err("%s: CQE: Unexpected interrupt 0x%08x.\n",
  3348. mmc_hostname(host->mmc), intmask);
  3349. sdhci_err_stats_inc(host, UNEXPECTED_IRQ);
  3350. sdhci_dumpregs(host);
  3351. }
  3352. return true;
  3353. }
  3354. EXPORT_SYMBOL_GPL(sdhci_cqe_irq);
  3355. /*****************************************************************************\
  3356. * *
  3357. * Device allocation/registration *
  3358. * *
  3359. \*****************************************************************************/
  3360. struct sdhci_host *sdhci_alloc_host(struct device *dev,
  3361. size_t priv_size)
  3362. {
  3363. struct mmc_host *mmc;
  3364. struct sdhci_host *host;
  3365. WARN_ON(dev == NULL);
  3366. mmc = devm_mmc_alloc_host(dev, sizeof(struct sdhci_host) + priv_size);
  3367. if (!mmc)
  3368. return ERR_PTR(-ENOMEM);
  3369. host = mmc_priv(mmc);
  3370. host->mmc = mmc;
  3371. host->mmc_host_ops = sdhci_ops;
  3372. mmc->ops = &host->mmc_host_ops;
  3373. host->flags = SDHCI_SIGNALING_330;
  3374. host->cqe_ier = SDHCI_CQE_INT_MASK;
  3375. host->cqe_err_ier = SDHCI_CQE_INT_ERR_MASK;
  3376. host->tuning_delay = -1;
  3377. host->tuning_loop_count = MAX_TUNING_LOOP;
  3378. host->sdma_boundary = SDHCI_DEFAULT_BOUNDARY_ARG;
  3379. /*
  3380. * The DMA table descriptor count is calculated as the maximum
  3381. * number of segments times 2, to allow for an alignment
  3382. * descriptor for each segment, plus 1 for a nop end descriptor.
  3383. */
  3384. host->adma_table_cnt = SDHCI_MAX_SEGS * 2 + 1;
  3385. host->max_adma = 65536;
  3386. host->max_timeout_count = 0xE;
  3387. host->complete_work_fn = sdhci_complete_work;
  3388. host->thread_irq_fn = sdhci_thread_irq;
  3389. return host;
  3390. }
  3391. EXPORT_SYMBOL_GPL(sdhci_alloc_host);
  3392. static int sdhci_set_dma_mask(struct sdhci_host *host)
  3393. {
  3394. struct mmc_host *mmc = host->mmc;
  3395. struct device *dev = mmc_dev(mmc);
  3396. int ret = -EINVAL;
  3397. if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
  3398. host->flags &= ~SDHCI_USE_64_BIT_DMA;
  3399. /* Try 64-bit mask if hardware is capable of it */
  3400. if (host->flags & SDHCI_USE_64_BIT_DMA) {
  3401. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
  3402. if (ret) {
  3403. pr_warn("%s: Failed to set 64-bit DMA mask.\n",
  3404. mmc_hostname(mmc));
  3405. host->flags &= ~SDHCI_USE_64_BIT_DMA;
  3406. }
  3407. }
  3408. /* 32-bit mask as default & fallback */
  3409. if (ret) {
  3410. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  3411. if (ret)
  3412. pr_warn("%s: Failed to set 32-bit DMA mask.\n",
  3413. mmc_hostname(mmc));
  3414. }
  3415. return ret;
  3416. }
  3417. void __sdhci_read_caps(struct sdhci_host *host, const u16 *ver,
  3418. const u32 *caps, const u32 *caps1)
  3419. {
  3420. u16 v;
  3421. u64 dt_caps_mask = 0;
  3422. u64 dt_caps = 0;
  3423. if (host->read_caps)
  3424. return;
  3425. host->read_caps = true;
  3426. if (debug_quirks)
  3427. host->quirks = debug_quirks;
  3428. if (debug_quirks2)
  3429. host->quirks2 = debug_quirks2;
  3430. sdhci_reset_for_all(host);
  3431. if (host->v4_mode)
  3432. sdhci_do_enable_v4_mode(host);
  3433. device_property_read_u64(mmc_dev(host->mmc),
  3434. "sdhci-caps-mask", &dt_caps_mask);
  3435. device_property_read_u64(mmc_dev(host->mmc),
  3436. "sdhci-caps", &dt_caps);
  3437. v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
  3438. host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
  3439. if (caps) {
  3440. host->caps = *caps;
  3441. } else {
  3442. host->caps = sdhci_readl(host, SDHCI_CAPABILITIES);
  3443. host->caps &= ~lower_32_bits(dt_caps_mask);
  3444. host->caps |= lower_32_bits(dt_caps);
  3445. }
  3446. if (host->version < SDHCI_SPEC_300)
  3447. return;
  3448. if (caps1) {
  3449. host->caps1 = *caps1;
  3450. } else {
  3451. host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
  3452. host->caps1 &= ~upper_32_bits(dt_caps_mask);
  3453. host->caps1 |= upper_32_bits(dt_caps);
  3454. }
  3455. }
  3456. EXPORT_SYMBOL_GPL(__sdhci_read_caps);
  3457. static void sdhci_allocate_bounce_buffer(struct sdhci_host *host)
  3458. {
  3459. struct mmc_host *mmc = host->mmc;
  3460. unsigned int max_blocks;
  3461. unsigned int bounce_size;
  3462. int ret;
  3463. /*
  3464. * Cap the bounce buffer at 64KB. Using a bigger bounce buffer
  3465. * has diminishing returns, this is probably because SD/MMC
  3466. * cards are usually optimized to handle this size of requests.
  3467. */
  3468. bounce_size = SZ_64K;
  3469. /*
  3470. * Adjust downwards to maximum request size if this is less
  3471. * than our segment size, else hammer down the maximum
  3472. * request size to the maximum buffer size.
  3473. */
  3474. if (mmc->max_req_size < bounce_size)
  3475. bounce_size = mmc->max_req_size;
  3476. max_blocks = bounce_size / 512;
  3477. /*
  3478. * When we just support one segment, we can get significant
  3479. * speedups by the help of a bounce buffer to group scattered
  3480. * reads/writes together.
  3481. */
  3482. host->bounce_buffer = devm_kmalloc(mmc_dev(mmc),
  3483. bounce_size,
  3484. GFP_KERNEL);
  3485. if (!host->bounce_buffer) {
  3486. pr_err("%s: failed to allocate %u bytes for bounce buffer, falling back to single segments\n",
  3487. mmc_hostname(mmc),
  3488. bounce_size);
  3489. /*
  3490. * Exiting with zero here makes sure we proceed with
  3491. * mmc->max_segs == 1.
  3492. */
  3493. return;
  3494. }
  3495. host->bounce_addr = dma_map_single(mmc_dev(mmc),
  3496. host->bounce_buffer,
  3497. bounce_size,
  3498. DMA_BIDIRECTIONAL);
  3499. ret = dma_mapping_error(mmc_dev(mmc), host->bounce_addr);
  3500. if (ret) {
  3501. devm_kfree(mmc_dev(mmc), host->bounce_buffer);
  3502. host->bounce_buffer = NULL;
  3503. /* Again fall back to max_segs == 1 */
  3504. return;
  3505. }
  3506. host->bounce_buffer_size = bounce_size;
  3507. /* Lie about this since we're bouncing */
  3508. mmc->max_segs = max_blocks;
  3509. mmc->max_seg_size = bounce_size;
  3510. mmc->max_req_size = bounce_size;
  3511. pr_info("%s bounce up to %u segments into one, max segment size %u bytes\n",
  3512. mmc_hostname(mmc), max_blocks, bounce_size);
  3513. }
  3514. static inline bool sdhci_can_64bit_dma(struct sdhci_host *host)
  3515. {
  3516. /*
  3517. * According to SD Host Controller spec v4.10, bit[27] added from
  3518. * version 4.10 in Capabilities Register is used as 64-bit System
  3519. * Address support for V4 mode.
  3520. */
  3521. if (host->version >= SDHCI_SPEC_410 && host->v4_mode)
  3522. return host->caps & SDHCI_CAN_64BIT_V4;
  3523. return host->caps & SDHCI_CAN_64BIT;
  3524. }
  3525. int sdhci_setup_host(struct sdhci_host *host)
  3526. {
  3527. struct mmc_host *mmc;
  3528. u32 max_current_caps;
  3529. unsigned int ocr_avail;
  3530. unsigned int override_timeout_clk;
  3531. u32 max_clk;
  3532. int ret = 0;
  3533. bool enable_vqmmc = false;
  3534. WARN_ON(host == NULL);
  3535. if (host == NULL)
  3536. return -EINVAL;
  3537. mmc = host->mmc;
  3538. /*
  3539. * If there are external regulators, get them. Note this must be done
  3540. * early before resetting the host and reading the capabilities so that
  3541. * the host can take the appropriate action if regulators are not
  3542. * available.
  3543. */
  3544. if (!mmc->supply.vqmmc) {
  3545. ret = mmc_regulator_get_supply(mmc);
  3546. if (ret)
  3547. return ret;
  3548. enable_vqmmc = true;
  3549. }
  3550. DBG("Version: 0x%08x | Present: 0x%08x\n",
  3551. sdhci_readw(host, SDHCI_HOST_VERSION),
  3552. sdhci_readl(host, SDHCI_PRESENT_STATE));
  3553. DBG("Caps: 0x%08x | Caps_1: 0x%08x\n",
  3554. sdhci_readl(host, SDHCI_CAPABILITIES),
  3555. sdhci_readl(host, SDHCI_CAPABILITIES_1));
  3556. sdhci_read_caps(host);
  3557. override_timeout_clk = host->timeout_clk;
  3558. if (host->version > SDHCI_SPEC_420) {
  3559. pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
  3560. mmc_hostname(mmc), host->version);
  3561. }
  3562. if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
  3563. host->flags |= SDHCI_USE_SDMA;
  3564. else if (!(host->caps & SDHCI_CAN_DO_SDMA))
  3565. DBG("Controller doesn't have SDMA capability\n");
  3566. else
  3567. host->flags |= SDHCI_USE_SDMA;
  3568. if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
  3569. (host->flags & SDHCI_USE_SDMA)) {
  3570. DBG("Disabling DMA as it is marked broken\n");
  3571. host->flags &= ~SDHCI_USE_SDMA;
  3572. }
  3573. if ((host->version >= SDHCI_SPEC_200) &&
  3574. (host->caps & SDHCI_CAN_DO_ADMA2))
  3575. host->flags |= SDHCI_USE_ADMA;
  3576. if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
  3577. (host->flags & SDHCI_USE_ADMA)) {
  3578. DBG("Disabling ADMA as it is marked broken\n");
  3579. host->flags &= ~SDHCI_USE_ADMA;
  3580. }
  3581. if (sdhci_can_64bit_dma(host))
  3582. host->flags |= SDHCI_USE_64_BIT_DMA;
  3583. if (host->use_external_dma) {
  3584. ret = sdhci_external_dma_init(host);
  3585. if (ret == -EPROBE_DEFER)
  3586. goto unreg;
  3587. /*
  3588. * Fall back to use the DMA/PIO integrated in standard SDHCI
  3589. * instead of external DMA devices.
  3590. */
  3591. else if (ret)
  3592. sdhci_switch_external_dma(host, false);
  3593. /* Disable internal DMA sources */
  3594. else
  3595. host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
  3596. }
  3597. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  3598. if (host->ops->set_dma_mask)
  3599. ret = host->ops->set_dma_mask(host);
  3600. else
  3601. ret = sdhci_set_dma_mask(host);
  3602. if (!ret && host->ops->enable_dma)
  3603. ret = host->ops->enable_dma(host);
  3604. if (ret) {
  3605. pr_warn("%s: No suitable DMA available - falling back to PIO\n",
  3606. mmc_hostname(mmc));
  3607. host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
  3608. ret = 0;
  3609. }
  3610. }
  3611. /* SDMA does not support 64-bit DMA if v4 mode not set */
  3612. if ((host->flags & SDHCI_USE_64_BIT_DMA) && !host->v4_mode)
  3613. host->flags &= ~SDHCI_USE_SDMA;
  3614. if (host->flags & SDHCI_USE_ADMA) {
  3615. dma_addr_t dma;
  3616. void *buf;
  3617. if (!(host->flags & SDHCI_USE_64_BIT_DMA))
  3618. host->alloc_desc_sz = SDHCI_ADMA2_32_DESC_SZ;
  3619. else if (!host->alloc_desc_sz)
  3620. host->alloc_desc_sz = SDHCI_ADMA2_64_DESC_SZ(host);
  3621. host->desc_sz = host->alloc_desc_sz;
  3622. host->adma_table_sz = host->adma_table_cnt * host->desc_sz;
  3623. host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
  3624. /*
  3625. * Use zalloc to zero the reserved high 32-bits of 128-bit
  3626. * descriptors so that they never need to be written.
  3627. */
  3628. buf = dma_alloc_coherent(mmc_dev(mmc),
  3629. host->align_buffer_sz + host->adma_table_sz,
  3630. &dma, GFP_KERNEL);
  3631. if (!buf) {
  3632. pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
  3633. mmc_hostname(mmc));
  3634. host->flags &= ~SDHCI_USE_ADMA;
  3635. } else if ((dma + host->align_buffer_sz) &
  3636. (SDHCI_ADMA2_DESC_ALIGN - 1)) {
  3637. pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
  3638. mmc_hostname(mmc));
  3639. host->flags &= ~SDHCI_USE_ADMA;
  3640. dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
  3641. host->adma_table_sz, buf, dma);
  3642. } else {
  3643. host->align_buffer = buf;
  3644. host->align_addr = dma;
  3645. host->adma_table = buf + host->align_buffer_sz;
  3646. host->adma_addr = dma + host->align_buffer_sz;
  3647. }
  3648. }
  3649. /*
  3650. * If we use DMA, then it's up to the caller to set the DMA
  3651. * mask, but PIO does not need the hw shim so we set a new
  3652. * mask here in that case.
  3653. */
  3654. if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
  3655. host->dma_mask = DMA_BIT_MASK(64);
  3656. mmc_dev(mmc)->dma_mask = &host->dma_mask;
  3657. }
  3658. if (host->version >= SDHCI_SPEC_300)
  3659. host->max_clk = FIELD_GET(SDHCI_CLOCK_V3_BASE_MASK, host->caps);
  3660. else
  3661. host->max_clk = FIELD_GET(SDHCI_CLOCK_BASE_MASK, host->caps);
  3662. host->max_clk *= 1000000;
  3663. if (host->max_clk == 0 || host->quirks &
  3664. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
  3665. if (!host->ops->get_max_clock) {
  3666. pr_err("%s: Hardware doesn't specify base clock frequency.\n",
  3667. mmc_hostname(mmc));
  3668. ret = -ENODEV;
  3669. goto undma;
  3670. }
  3671. host->max_clk = host->ops->get_max_clock(host);
  3672. }
  3673. /*
  3674. * In case of Host Controller v3.00, find out whether clock
  3675. * multiplier is supported.
  3676. */
  3677. host->clk_mul = FIELD_GET(SDHCI_CLOCK_MUL_MASK, host->caps1);
  3678. /*
  3679. * In case the value in Clock Multiplier is 0, then programmable
  3680. * clock mode is not supported, otherwise the actual clock
  3681. * multiplier is one more than the value of Clock Multiplier
  3682. * in the Capabilities Register.
  3683. */
  3684. if (host->clk_mul)
  3685. host->clk_mul += 1;
  3686. /*
  3687. * Set host parameters.
  3688. */
  3689. max_clk = host->max_clk;
  3690. if (host->ops->get_min_clock)
  3691. mmc->f_min = host->ops->get_min_clock(host);
  3692. else if (host->version >= SDHCI_SPEC_300) {
  3693. if (host->clk_mul)
  3694. max_clk = host->max_clk * host->clk_mul;
  3695. /*
  3696. * Divided Clock Mode minimum clock rate is always less than
  3697. * Programmable Clock Mode minimum clock rate.
  3698. */
  3699. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
  3700. } else
  3701. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
  3702. if (!mmc->f_max || mmc->f_max > max_clk)
  3703. mmc->f_max = max_clk;
  3704. if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
  3705. host->timeout_clk = FIELD_GET(SDHCI_TIMEOUT_CLK_MASK, host->caps);
  3706. if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
  3707. host->timeout_clk *= 1000;
  3708. if (host->timeout_clk == 0) {
  3709. if (!host->ops->get_timeout_clock) {
  3710. pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
  3711. mmc_hostname(mmc));
  3712. ret = -ENODEV;
  3713. goto undma;
  3714. }
  3715. host->timeout_clk =
  3716. DIV_ROUND_UP(host->ops->get_timeout_clock(host),
  3717. 1000);
  3718. }
  3719. if (override_timeout_clk)
  3720. host->timeout_clk = override_timeout_clk;
  3721. mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
  3722. host->ops->get_max_timeout_count(host) : 1 << 27;
  3723. mmc->max_busy_timeout /= host->timeout_clk;
  3724. }
  3725. if (host->quirks2 & SDHCI_QUIRK2_DISABLE_HW_TIMEOUT &&
  3726. !host->ops->get_max_timeout_count)
  3727. mmc->max_busy_timeout = 0;
  3728. mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_CMD23;
  3729. mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
  3730. if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
  3731. host->flags |= SDHCI_AUTO_CMD12;
  3732. /*
  3733. * For v3 mode, Auto-CMD23 stuff only works in ADMA or PIO.
  3734. * For v4 mode, SDMA may use Auto-CMD23 as well.
  3735. */
  3736. if ((host->version >= SDHCI_SPEC_300) &&
  3737. ((host->flags & SDHCI_USE_ADMA) ||
  3738. !(host->flags & SDHCI_USE_SDMA) || host->v4_mode) &&
  3739. !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
  3740. host->flags |= SDHCI_AUTO_CMD23;
  3741. DBG("Auto-CMD23 available\n");
  3742. } else {
  3743. DBG("Auto-CMD23 unavailable\n");
  3744. }
  3745. /*
  3746. * A controller may support 8-bit width, but the board itself
  3747. * might not have the pins brought out. Boards that support
  3748. * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
  3749. * their platform code before calling sdhci_add_host(), and we
  3750. * won't assume 8-bit width for hosts without that CAP.
  3751. */
  3752. if (host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA) {
  3753. host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_DDR50);
  3754. if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400)
  3755. host->caps1 &= ~SDHCI_SUPPORT_HS400;
  3756. mmc->caps2 &= ~(MMC_CAP2_HS200 | MMC_CAP2_HS400 | MMC_CAP2_HS400_ES);
  3757. mmc->caps &= ~(MMC_CAP_DDR | MMC_CAP_UHS);
  3758. } else {
  3759. mmc->caps |= MMC_CAP_4_BIT_DATA;
  3760. }
  3761. if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
  3762. mmc->caps &= ~MMC_CAP_CMD23;
  3763. if (host->caps & SDHCI_CAN_DO_HISPD)
  3764. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  3765. if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
  3766. mmc_card_is_removable(mmc) &&
  3767. mmc_gpio_get_cd(mmc) < 0)
  3768. mmc->caps |= MMC_CAP_NEEDS_POLL;
  3769. if (!IS_ERR(mmc->supply.vqmmc)) {
  3770. if (enable_vqmmc) {
  3771. ret = regulator_enable(mmc->supply.vqmmc);
  3772. host->sdhci_core_to_disable_vqmmc = !ret;
  3773. }
  3774. /* If vqmmc provides no 1.8V signalling, then there's no UHS */
  3775. if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
  3776. 1950000))
  3777. host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
  3778. SDHCI_SUPPORT_SDR50 |
  3779. SDHCI_SUPPORT_DDR50);
  3780. /* In eMMC case vqmmc might be a fixed 1.8V regulator */
  3781. if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 2700000,
  3782. 3600000))
  3783. host->flags &= ~SDHCI_SIGNALING_330;
  3784. if (ret) {
  3785. pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
  3786. mmc_hostname(mmc), ret);
  3787. mmc->supply.vqmmc = ERR_PTR(-EINVAL);
  3788. }
  3789. }
  3790. if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
  3791. host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
  3792. SDHCI_SUPPORT_DDR50);
  3793. /*
  3794. * The SDHCI controller in a SoC might support HS200/HS400
  3795. * (indicated using mmc-hs200-1_8v/mmc-hs400-1_8v dt property),
  3796. * but if the board is modeled such that the IO lines are not
  3797. * connected to 1.8v then HS200/HS400 cannot be supported.
  3798. * Disable HS200/HS400 if the board does not have 1.8v connected
  3799. * to the IO lines. (Applicable for other modes in 1.8v)
  3800. */
  3801. mmc->caps2 &= ~(MMC_CAP2_HSX00_1_8V | MMC_CAP2_HS400_ES);
  3802. mmc->caps &= ~(MMC_CAP_1_8V_DDR | MMC_CAP_UHS);
  3803. }
  3804. /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
  3805. if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
  3806. SDHCI_SUPPORT_DDR50))
  3807. mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
  3808. /* SDR104 supports also implies SDR50 support */
  3809. if (host->caps1 & SDHCI_SUPPORT_SDR104) {
  3810. mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
  3811. /* SD3.0: SDR104 is supported so (for eMMC) the caps2
  3812. * field can be promoted to support HS200.
  3813. */
  3814. if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
  3815. mmc->caps2 |= MMC_CAP2_HS200;
  3816. } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
  3817. mmc->caps |= MMC_CAP_UHS_SDR50;
  3818. }
  3819. if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
  3820. (host->caps1 & SDHCI_SUPPORT_HS400))
  3821. mmc->caps2 |= MMC_CAP2_HS400;
  3822. if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
  3823. (IS_ERR(mmc->supply.vqmmc) ||
  3824. !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
  3825. 1300000)))
  3826. mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
  3827. if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
  3828. !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
  3829. mmc->caps |= MMC_CAP_UHS_DDR50;
  3830. /* Does the host need tuning for SDR50? */
  3831. if (host->caps1 & SDHCI_USE_SDR50_TUNING)
  3832. host->flags |= SDHCI_SDR50_NEEDS_TUNING;
  3833. /* Driver Type(s) (A, C, D) supported by the host */
  3834. if (host->caps1 & SDHCI_DRIVER_TYPE_A)
  3835. mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
  3836. if (host->caps1 & SDHCI_DRIVER_TYPE_C)
  3837. mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
  3838. if (host->caps1 & SDHCI_DRIVER_TYPE_D)
  3839. mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
  3840. /* Initial value for re-tuning timer count */
  3841. host->tuning_count = FIELD_GET(SDHCI_RETUNING_TIMER_COUNT_MASK,
  3842. host->caps1);
  3843. /*
  3844. * In case Re-tuning Timer is not disabled, the actual value of
  3845. * re-tuning timer will be 2 ^ (n - 1).
  3846. */
  3847. if (host->tuning_count)
  3848. host->tuning_count = 1 << (host->tuning_count - 1);
  3849. /* Re-tuning mode supported by the Host Controller */
  3850. host->tuning_mode = FIELD_GET(SDHCI_RETUNING_MODE_MASK, host->caps1);
  3851. ocr_avail = 0;
  3852. /*
  3853. * According to SD Host Controller spec v3.00, if the Host System
  3854. * can afford more than 150mA, Host Driver should set XPC to 1. Also
  3855. * the value is meaningful only if Voltage Support in the Capabilities
  3856. * register is set. The actual current value is 4 times the register
  3857. * value.
  3858. */
  3859. max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
  3860. if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
  3861. int curr = regulator_get_current_limit(mmc->supply.vmmc);
  3862. if (curr > 0) {
  3863. /* convert to SDHCI_MAX_CURRENT format */
  3864. curr = curr/1000; /* convert to mA */
  3865. curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
  3866. curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
  3867. max_current_caps =
  3868. FIELD_PREP(SDHCI_MAX_CURRENT_330_MASK, curr) |
  3869. FIELD_PREP(SDHCI_MAX_CURRENT_300_MASK, curr) |
  3870. FIELD_PREP(SDHCI_MAX_CURRENT_180_MASK, curr);
  3871. }
  3872. }
  3873. if (host->caps & SDHCI_CAN_VDD_330) {
  3874. ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
  3875. mmc->max_current_330 = FIELD_GET(SDHCI_MAX_CURRENT_330_MASK,
  3876. max_current_caps) *
  3877. SDHCI_MAX_CURRENT_MULTIPLIER;
  3878. }
  3879. if (host->caps & SDHCI_CAN_VDD_300) {
  3880. ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
  3881. mmc->max_current_300 = FIELD_GET(SDHCI_MAX_CURRENT_300_MASK,
  3882. max_current_caps) *
  3883. SDHCI_MAX_CURRENT_MULTIPLIER;
  3884. }
  3885. if (host->caps & SDHCI_CAN_VDD_180) {
  3886. ocr_avail |= MMC_VDD_165_195;
  3887. mmc->max_current_180 = FIELD_GET(SDHCI_MAX_CURRENT_180_MASK,
  3888. max_current_caps) *
  3889. SDHCI_MAX_CURRENT_MULTIPLIER;
  3890. }
  3891. /* If OCR set by host, use it instead. */
  3892. if (host->ocr_mask)
  3893. ocr_avail = host->ocr_mask;
  3894. /* If OCR set by external regulators, give it highest prio. */
  3895. if (mmc->ocr_avail)
  3896. ocr_avail = mmc->ocr_avail;
  3897. mmc->ocr_avail = ocr_avail;
  3898. mmc->ocr_avail_sdio = ocr_avail;
  3899. if (host->ocr_avail_sdio)
  3900. mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
  3901. mmc->ocr_avail_sd = ocr_avail;
  3902. if (host->ocr_avail_sd)
  3903. mmc->ocr_avail_sd &= host->ocr_avail_sd;
  3904. else /* normal SD controllers don't support 1.8V */
  3905. mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
  3906. mmc->ocr_avail_mmc = ocr_avail;
  3907. if (host->ocr_avail_mmc)
  3908. mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
  3909. if (mmc->ocr_avail == 0) {
  3910. pr_err("%s: Hardware doesn't report any support voltages.\n",
  3911. mmc_hostname(mmc));
  3912. ret = -ENODEV;
  3913. goto unreg;
  3914. }
  3915. if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
  3916. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
  3917. MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
  3918. (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
  3919. host->flags |= SDHCI_SIGNALING_180;
  3920. if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
  3921. host->flags |= SDHCI_SIGNALING_120;
  3922. spin_lock_init(&host->lock);
  3923. /*
  3924. * Maximum number of sectors in one transfer. Limited by SDMA boundary
  3925. * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
  3926. * is less anyway.
  3927. */
  3928. mmc->max_req_size = 524288;
  3929. /*
  3930. * Maximum number of segments. Depends on if the hardware
  3931. * can do scatter/gather or not.
  3932. */
  3933. if (host->flags & SDHCI_USE_ADMA) {
  3934. mmc->max_segs = SDHCI_MAX_SEGS;
  3935. } else if (host->flags & SDHCI_USE_SDMA) {
  3936. mmc->max_segs = 1;
  3937. mmc->max_req_size = min_t(size_t, mmc->max_req_size,
  3938. dma_max_mapping_size(mmc_dev(mmc)));
  3939. } else { /* PIO */
  3940. mmc->max_segs = SDHCI_MAX_SEGS;
  3941. }
  3942. /*
  3943. * Maximum segment size. Could be one segment with the maximum number
  3944. * of bytes. When doing hardware scatter/gather, each entry cannot
  3945. * be larger than 64 KiB though.
  3946. */
  3947. if (host->flags & SDHCI_USE_ADMA) {
  3948. if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC) {
  3949. host->max_adma = 65532; /* 32-bit alignment */
  3950. mmc->max_seg_size = 65535;
  3951. /*
  3952. * sdhci_adma_table_pre() expects to define 1 DMA
  3953. * descriptor per segment, so the maximum segment size
  3954. * is set accordingly. SDHCI allows up to 64KiB per DMA
  3955. * descriptor (16-bit field), but some controllers do
  3956. * not support "zero means 65536" reducing the maximum
  3957. * for them to 65535. That is a problem if PAGE_SIZE is
  3958. * 64KiB because the block layer does not support
  3959. * max_seg_size < PAGE_SIZE, however
  3960. * sdhci_adma_table_pre() has a workaround to handle
  3961. * that case, and split the descriptor. Refer also
  3962. * comment in sdhci_adma_table_pre().
  3963. */
  3964. if (mmc->max_seg_size < PAGE_SIZE)
  3965. mmc->max_seg_size = PAGE_SIZE;
  3966. } else {
  3967. mmc->max_seg_size = 65536;
  3968. }
  3969. } else {
  3970. mmc->max_seg_size = mmc->max_req_size;
  3971. }
  3972. /*
  3973. * Maximum block size. This varies from controller to controller and
  3974. * is specified in the capabilities register.
  3975. */
  3976. if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
  3977. mmc->max_blk_size = 2;
  3978. } else {
  3979. mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
  3980. SDHCI_MAX_BLOCK_SHIFT;
  3981. if (mmc->max_blk_size >= 3) {
  3982. pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
  3983. mmc_hostname(mmc));
  3984. mmc->max_blk_size = 0;
  3985. }
  3986. }
  3987. mmc->max_blk_size = 512 << mmc->max_blk_size;
  3988. /*
  3989. * Maximum block count.
  3990. */
  3991. mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
  3992. if (mmc->max_segs == 1)
  3993. /* This may alter mmc->*_blk_* parameters */
  3994. sdhci_allocate_bounce_buffer(host);
  3995. return 0;
  3996. unreg:
  3997. if (host->sdhci_core_to_disable_vqmmc)
  3998. regulator_disable(mmc->supply.vqmmc);
  3999. undma:
  4000. if (host->align_buffer)
  4001. dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
  4002. host->adma_table_sz, host->align_buffer,
  4003. host->align_addr);
  4004. host->adma_table = NULL;
  4005. host->align_buffer = NULL;
  4006. return ret;
  4007. }
  4008. EXPORT_SYMBOL_GPL(sdhci_setup_host);
  4009. void sdhci_cleanup_host(struct sdhci_host *host)
  4010. {
  4011. struct mmc_host *mmc = host->mmc;
  4012. if (host->sdhci_core_to_disable_vqmmc)
  4013. regulator_disable(mmc->supply.vqmmc);
  4014. if (host->align_buffer)
  4015. dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
  4016. host->adma_table_sz, host->align_buffer,
  4017. host->align_addr);
  4018. if (host->use_external_dma)
  4019. sdhci_external_dma_release(host);
  4020. host->adma_table = NULL;
  4021. host->align_buffer = NULL;
  4022. }
  4023. EXPORT_SYMBOL_GPL(sdhci_cleanup_host);
  4024. int __sdhci_add_host(struct sdhci_host *host)
  4025. {
  4026. unsigned int flags = WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_HIGHPRI;
  4027. struct mmc_host *mmc = host->mmc;
  4028. int ret;
  4029. if ((mmc->caps2 & MMC_CAP2_CQE) &&
  4030. (host->quirks & SDHCI_QUIRK_BROKEN_CQE)) {
  4031. mmc->caps2 &= ~MMC_CAP2_CQE;
  4032. mmc->cqe_ops = NULL;
  4033. }
  4034. host->complete_wq = alloc_workqueue("sdhci", flags, 0);
  4035. if (!host->complete_wq)
  4036. return -ENOMEM;
  4037. INIT_WORK(&host->complete_work, host->complete_work_fn);
  4038. timer_setup(&host->timer, sdhci_timeout_timer, 0);
  4039. timer_setup(&host->data_timer, sdhci_timeout_data_timer, 0);
  4040. init_waitqueue_head(&host->buf_ready_int);
  4041. sdhci_init(host, 0);
  4042. ret = request_threaded_irq(host->irq, sdhci_irq, host->thread_irq_fn,
  4043. IRQF_SHARED, mmc_hostname(mmc), host);
  4044. if (ret) {
  4045. pr_err("%s: Failed to request IRQ %d: %d\n",
  4046. mmc_hostname(mmc), host->irq, ret);
  4047. goto unwq;
  4048. }
  4049. ret = sdhci_led_register(host);
  4050. if (ret) {
  4051. pr_err("%s: Failed to register LED device: %d\n",
  4052. mmc_hostname(mmc), ret);
  4053. goto unirq;
  4054. }
  4055. ret = mmc_add_host(mmc);
  4056. if (ret)
  4057. goto unled;
  4058. pr_info("%s: SDHCI controller on %s [%s] using %s\n",
  4059. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  4060. host->use_external_dma ? "External DMA" :
  4061. (host->flags & SDHCI_USE_ADMA) ?
  4062. (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
  4063. (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
  4064. sdhci_enable_card_detection(host);
  4065. return 0;
  4066. unled:
  4067. sdhci_led_unregister(host);
  4068. unirq:
  4069. sdhci_reset_for_all(host);
  4070. sdhci_writel(host, 0, SDHCI_INT_ENABLE);
  4071. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  4072. free_irq(host->irq, host);
  4073. unwq:
  4074. destroy_workqueue(host->complete_wq);
  4075. return ret;
  4076. }
  4077. EXPORT_SYMBOL_GPL(__sdhci_add_host);
  4078. int sdhci_add_host(struct sdhci_host *host)
  4079. {
  4080. int ret;
  4081. ret = sdhci_setup_host(host);
  4082. if (ret)
  4083. return ret;
  4084. ret = __sdhci_add_host(host);
  4085. if (ret)
  4086. goto cleanup;
  4087. return 0;
  4088. cleanup:
  4089. sdhci_cleanup_host(host);
  4090. return ret;
  4091. }
  4092. EXPORT_SYMBOL_GPL(sdhci_add_host);
  4093. void sdhci_remove_host(struct sdhci_host *host, int dead)
  4094. {
  4095. struct mmc_host *mmc = host->mmc;
  4096. unsigned long flags;
  4097. if (dead) {
  4098. spin_lock_irqsave(&host->lock, flags);
  4099. host->flags |= SDHCI_DEVICE_DEAD;
  4100. if (sdhci_has_requests(host)) {
  4101. pr_err("%s: Controller removed during "
  4102. " transfer!\n", mmc_hostname(mmc));
  4103. sdhci_error_out_mrqs(host, -ENOMEDIUM);
  4104. }
  4105. spin_unlock_irqrestore(&host->lock, flags);
  4106. }
  4107. sdhci_disable_card_detection(host);
  4108. mmc_remove_host(mmc);
  4109. sdhci_led_unregister(host);
  4110. if (!dead)
  4111. sdhci_reset_for_all(host);
  4112. sdhci_writel(host, 0, SDHCI_INT_ENABLE);
  4113. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  4114. free_irq(host->irq, host);
  4115. timer_delete_sync(&host->timer);
  4116. timer_delete_sync(&host->data_timer);
  4117. destroy_workqueue(host->complete_wq);
  4118. if (host->sdhci_core_to_disable_vqmmc)
  4119. regulator_disable(mmc->supply.vqmmc);
  4120. if (host->align_buffer)
  4121. dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
  4122. host->adma_table_sz, host->align_buffer,
  4123. host->align_addr);
  4124. if (host->use_external_dma)
  4125. sdhci_external_dma_release(host);
  4126. host->adma_table = NULL;
  4127. host->align_buffer = NULL;
  4128. }
  4129. EXPORT_SYMBOL_GPL(sdhci_remove_host);
  4130. /*****************************************************************************\
  4131. * *
  4132. * Driver init/exit *
  4133. * *
  4134. \*****************************************************************************/
  4135. static int __init sdhci_drv_init(void)
  4136. {
  4137. pr_info(DRIVER_NAME
  4138. ": Secure Digital Host Controller Interface driver\n");
  4139. pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  4140. return 0;
  4141. }
  4142. static void __exit sdhci_drv_exit(void)
  4143. {
  4144. }
  4145. module_init(sdhci_drv_init);
  4146. module_exit(sdhci_drv_exit);
  4147. module_param(debug_quirks, uint, 0444);
  4148. module_param(debug_quirks2, uint, 0444);
  4149. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  4150. MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
  4151. MODULE_LICENSE("GPL");
  4152. MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
  4153. MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");