msm_sdcc.c 189 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193
  1. /*
  2. * linux/drivers/mmc/host/msm_sdcc.c - Qualcomm MSM 7X00A SDCC Driver
  3. *
  4. * Copyright (C) 2007 Google Inc,
  5. * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
  6. * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * Based on mmci.c
  14. *
  15. * Author: San Mehat (san@android.com)
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/init.h>
  21. #include <linux/ioport.h>
  22. #include <linux/of.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/device.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/irq.h>
  27. #include <linux/delay.h>
  28. #include <linux/err.h>
  29. #include <linux/highmem.h>
  30. #include <linux/log2.h>
  31. #include <linux/mmc/host.h>
  32. #include <linux/mmc/card.h>
  33. #include <linux/mmc/mmc.h>
  34. #include <linux/mmc/sdio.h>
  35. #include <linux/clk.h>
  36. #include <linux/scatterlist.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/debugfs.h>
  40. #include <linux/io.h>
  41. #include <linux/memory.h>
  42. #include <linux/pm_runtime.h>
  43. #include <linux/wakelock.h>
  44. #include <linux/gpio.h>
  45. #include <linux/regulator/consumer.h>
  46. #include <linux/slab.h>
  47. #include <linux/pm_qos.h>
  48. #include <linux/iopoll.h>
  49. #include <asm/cacheflush.h>
  50. #include <asm/div64.h>
  51. #include <asm/sizes.h>
  52. #include <asm/mach/mmc.h>
  53. #include <mach/msm_iomap.h>
  54. #include <mach/clk.h>
  55. #include <mach/dma.h>
  56. #include <mach/sdio_al.h>
  57. #include <mach/mpm.h>
  58. #include <mach/msm_bus.h>
  59. #include "msm_sdcc.h"
  60. #include "msm_sdcc_dml.h"
  61. #define DRIVER_NAME "msm-sdcc"
  62. #define DBG(host, fmt, args...) \
  63. pr_debug("%s: %s: " fmt "\n", mmc_hostname(host->mmc), __func__ , args)
  64. #define IRQ_DEBUG 0
  65. #define SPS_SDCC_PRODUCER_PIPE_INDEX 1
  66. #define SPS_SDCC_CONSUMER_PIPE_INDEX 2
  67. #define SPS_CONS_PERIPHERAL 0
  68. #define SPS_PROD_PERIPHERAL 1
  69. /* Use SPS only if transfer size is more than this macro */
  70. #define SPS_MIN_XFER_SIZE MCI_FIFOSIZE
  71. #define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
  72. #define INVALID_TUNING_PHASE -1
  73. #if defined(CONFIG_DEBUG_FS)
  74. static void msmsdcc_dbg_createhost(struct msmsdcc_host *);
  75. static struct dentry *debugfs_dir;
  76. static int msmsdcc_dbg_init(void);
  77. #endif
  78. static int msmsdcc_prep_xfer(struct msmsdcc_host *host, struct mmc_data
  79. *data);
  80. static void msmsdcc_msm_bus_cancel_work_and_set_vote(struct msmsdcc_host *host,
  81. struct mmc_ios *ios);
  82. static void msmsdcc_msm_bus_queue_work(struct msmsdcc_host *host);
  83. static u64 dma_mask = DMA_BIT_MASK(32);
  84. static unsigned int msmsdcc_pwrsave = 1;
  85. static struct mmc_command dummy52cmd;
  86. static struct mmc_request dummy52mrq = {
  87. .cmd = &dummy52cmd,
  88. .data = NULL,
  89. .stop = NULL,
  90. };
  91. static struct mmc_command dummy52cmd = {
  92. .opcode = SD_IO_RW_DIRECT,
  93. .flags = MMC_RSP_PRESENT,
  94. .data = NULL,
  95. .mrq = &dummy52mrq,
  96. };
  97. /*
  98. * An array holding the Tuning pattern to compare with when
  99. * executing a tuning cycle.
  100. */
  101. static const u32 tuning_block_64[] = {
  102. 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
  103. 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
  104. 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
  105. 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
  106. };
  107. static const u32 tuning_block_128[] = {
  108. 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
  109. 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
  110. 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
  111. 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
  112. 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
  113. 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
  114. 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
  115. 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
  116. };
  117. static int disable_slots;
  118. module_param(disable_slots, int, 0);
  119. #if IRQ_DEBUG == 1
  120. static char *irq_status_bits[] = { "cmdcrcfail", "datcrcfail", "cmdtimeout",
  121. "dattimeout", "txunderrun", "rxoverrun",
  122. "cmdrespend", "cmdsent", "dataend", NULL,
  123. "datablkend", "cmdactive", "txactive",
  124. "rxactive", "txhalfempty", "rxhalffull",
  125. "txfifofull", "rxfifofull", "txfifoempty",
  126. "rxfifoempty", "txdataavlbl", "rxdataavlbl",
  127. "sdiointr", "progdone", "atacmdcompl",
  128. "sdiointrope", "ccstimeout", NULL, NULL,
  129. NULL, NULL, NULL };
  130. static void
  131. msmsdcc_print_status(struct msmsdcc_host *host, char *hdr, uint32_t status)
  132. {
  133. int i;
  134. pr_debug("%s-%s ", mmc_hostname(host->mmc), hdr);
  135. for (i = 0; i < 32; i++) {
  136. if (status & (1 << i))
  137. pr_debug("%s ", irq_status_bits[i]);
  138. }
  139. pr_debug("\n");
  140. }
  141. #endif
  142. static void
  143. msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd,
  144. u32 c);
  145. static inline void msmsdcc_sync_reg_wr(struct msmsdcc_host *host);
  146. static inline void msmsdcc_delay(struct msmsdcc_host *host);
  147. static void msmsdcc_dump_sdcc_state(struct msmsdcc_host *host);
  148. static void msmsdcc_sg_start(struct msmsdcc_host *host);
  149. static int msmsdcc_vreg_reset(struct msmsdcc_host *host);
  150. static int msmsdcc_runtime_resume(struct device *dev);
  151. static int msmsdcc_dt_get_array(struct device *dev, const char *prop_name,
  152. u32 **out_array, int *len, int size);
  153. static int msmsdcc_execute_tuning(struct mmc_host *mmc, u32 opcode);
  154. static bool msmsdcc_is_wait_for_auto_prog_done(struct msmsdcc_host *host,
  155. struct mmc_request *mrq);
  156. static bool msmsdcc_is_wait_for_prog_done(struct msmsdcc_host *host,
  157. struct mmc_request *mrq);
  158. static inline unsigned short msmsdcc_get_nr_sg(struct msmsdcc_host *host)
  159. {
  160. unsigned short ret = NR_SG;
  161. if (is_sps_mode(host)) {
  162. ret = SPS_MAX_DESCS;
  163. } else { /* DMA or PIO mode */
  164. if (NR_SG > MAX_NR_SG_DMA_PIO)
  165. ret = MAX_NR_SG_DMA_PIO;
  166. }
  167. return ret;
  168. }
  169. /* Prevent idle power collapse(pc) while operating in peripheral mode */
  170. static void msmsdcc_pm_qos_update_latency(struct msmsdcc_host *host, int vote)
  171. {
  172. if (!host->cpu_dma_latency)
  173. return;
  174. if (vote)
  175. pm_qos_update_request(&host->pm_qos_req_dma,
  176. host->cpu_dma_latency);
  177. else
  178. pm_qos_update_request(&host->pm_qos_req_dma,
  179. PM_QOS_DEFAULT_VALUE);
  180. }
  181. #ifdef CONFIG_MMC_MSM_SPS_SUPPORT
  182. static int msmsdcc_sps_reset_ep(struct msmsdcc_host *host,
  183. struct msmsdcc_sps_ep_conn_data *ep);
  184. static int msmsdcc_sps_restore_ep(struct msmsdcc_host *host,
  185. struct msmsdcc_sps_ep_conn_data *ep);
  186. #else
  187. static inline int msmsdcc_sps_init_ep_conn(struct msmsdcc_host *host,
  188. struct msmsdcc_sps_ep_conn_data *ep,
  189. bool is_producer) { return 0; }
  190. static inline void msmsdcc_sps_exit_ep_conn(struct msmsdcc_host *host,
  191. struct msmsdcc_sps_ep_conn_data *ep) { }
  192. static inline int msmsdcc_sps_reset_ep(struct msmsdcc_host *host,
  193. struct msmsdcc_sps_ep_conn_data *ep)
  194. {
  195. return 0;
  196. }
  197. static inline int msmsdcc_sps_restore_ep(struct msmsdcc_host *host,
  198. struct msmsdcc_sps_ep_conn_data *ep)
  199. {
  200. return 0;
  201. }
  202. static inline int msmsdcc_sps_init(struct msmsdcc_host *host) { return 0; }
  203. static inline void msmsdcc_sps_exit(struct msmsdcc_host *host) {}
  204. #endif /* CONFIG_MMC_MSM_SPS_SUPPORT */
  205. /**
  206. * Apply reset
  207. *
  208. * This function resets SPS BAM and DML cores.
  209. *
  210. * This function should be called to recover from error
  211. * conditions encountered during CMD/DATA tranfsers with card.
  212. *
  213. * @host - Pointer to driver's host structure
  214. *
  215. */
  216. static int msmsdcc_bam_dml_reset_and_restore(struct msmsdcc_host *host)
  217. {
  218. int rc;
  219. /* Reset all SDCC BAM pipes */
  220. rc = msmsdcc_sps_reset_ep(host, &host->sps.prod);
  221. if (rc) {
  222. pr_err("%s: msmsdcc_sps_reset_ep(prod) error=%d\n",
  223. mmc_hostname(host->mmc), rc);
  224. goto out;
  225. }
  226. rc = msmsdcc_sps_reset_ep(host, &host->sps.cons);
  227. if (rc) {
  228. pr_err("%s: msmsdcc_sps_reset_ep(cons) error=%d\n",
  229. mmc_hostname(host->mmc), rc);
  230. goto out;
  231. }
  232. /* Reset BAM */
  233. rc = sps_device_reset(host->sps.bam_handle);
  234. if (rc) {
  235. pr_err("%s: sps_device_reset error=%d\n",
  236. mmc_hostname(host->mmc), rc);
  237. goto out;
  238. }
  239. memset(host->sps.prod.config.desc.base, 0x00,
  240. host->sps.prod.config.desc.size);
  241. memset(host->sps.cons.config.desc.base, 0x00,
  242. host->sps.cons.config.desc.size);
  243. /* Restore all BAM pipes connections */
  244. rc = msmsdcc_sps_restore_ep(host, &host->sps.prod);
  245. if (rc) {
  246. pr_err("%s: msmsdcc_sps_restore_ep(prod) error=%d\n",
  247. mmc_hostname(host->mmc), rc);
  248. goto out;
  249. }
  250. rc = msmsdcc_sps_restore_ep(host, &host->sps.cons);
  251. if (rc) {
  252. pr_err("%s: msmsdcc_sps_restore_ep(cons) error=%d\n",
  253. mmc_hostname(host->mmc), rc);
  254. goto out;
  255. }
  256. /* Reset and init DML */
  257. rc = msmsdcc_dml_init(host);
  258. if (rc)
  259. pr_err("%s: msmsdcc_dml_init error=%d\n",
  260. mmc_hostname(host->mmc), rc);
  261. out:
  262. if (!rc)
  263. host->sps.reset_bam = false;
  264. return rc;
  265. }
  266. /**
  267. * Apply soft reset
  268. *
  269. * This function applies soft reset to SDCC core.
  270. *
  271. * This function should be called to recover from error
  272. * conditions encountered with CMD/DATA tranfsers with card.
  273. *
  274. * Soft reset should only be used with SDCC controller v4.
  275. *
  276. * @host - Pointer to driver's host structure
  277. *
  278. */
  279. static void msmsdcc_soft_reset(struct msmsdcc_host *host)
  280. {
  281. /*
  282. * Reset controller state machines without resetting
  283. * configuration registers (MCI_POWER, MCI_CLK, MCI_INT_MASKn).
  284. */
  285. if (is_sw_reset_save_config(host)) {
  286. ktime_t start;
  287. uint32_t dll_config = 0;
  288. if (is_sw_reset_save_config_broken(host))
  289. dll_config = readl_relaxed(host->base + MCI_DLL_CONFIG);
  290. writel_relaxed(readl_relaxed(host->base + MMCIPOWER)
  291. | MCI_SW_RST_CFG, host->base + MMCIPOWER);
  292. msmsdcc_sync_reg_wr(host);
  293. start = ktime_get();
  294. while (readl_relaxed(host->base + MMCIPOWER) & MCI_SW_RST_CFG) {
  295. /*
  296. * SW reset can take upto 10HCLK + 15MCLK cycles.
  297. * Calculating based on min clk rates (hclk = 27MHz,
  298. * mclk = 400KHz) it comes to ~40us. Let's poll for
  299. * max. 1ms for reset completion.
  300. */
  301. if (ktime_to_us(ktime_sub(ktime_get(), start)) > 1000) {
  302. pr_err("%s: %s failed\n",
  303. mmc_hostname(host->mmc), __func__);
  304. BUG();
  305. }
  306. }
  307. if (is_sw_reset_save_config_broken(host)) {
  308. writel_relaxed(dll_config, host->base + MCI_DLL_CONFIG);
  309. mb();
  310. }
  311. } else {
  312. writel_relaxed(0, host->base + MMCICOMMAND);
  313. msmsdcc_sync_reg_wr(host);
  314. writel_relaxed(0, host->base + MMCIDATACTRL);
  315. msmsdcc_sync_reg_wr(host);
  316. }
  317. }
  318. static void msmsdcc_hard_reset(struct msmsdcc_host *host)
  319. {
  320. int ret;
  321. /*
  322. * Reset SDCC controller to power on default state.
  323. * Don't issue a reset request to clock control block if
  324. * SDCC controller itself can support hard reset.
  325. */
  326. if (is_sw_hard_reset(host)) {
  327. u32 pwr;
  328. writel_relaxed(readl_relaxed(host->base + MMCIPOWER)
  329. | MCI_SW_RST, host->base + MMCIPOWER);
  330. msmsdcc_sync_reg_wr(host);
  331. /*
  332. * See comment in msmsdcc_soft_reset() on choosing 1ms
  333. * poll timeout.
  334. */
  335. ret = readl_poll_timeout_noirq(host->base + MMCIPOWER,
  336. pwr, !(pwr & MCI_SW_RST), 100, 10);
  337. if (ret) {
  338. pr_err("%s: %s failed (%d)\n",
  339. mmc_hostname(host->mmc), __func__, ret);
  340. BUG();
  341. }
  342. } else {
  343. ret = clk_reset(host->clk, CLK_RESET_ASSERT);
  344. if (ret)
  345. pr_err("%s: Clock assert failed at %u Hz" \
  346. " with err %d\n", mmc_hostname(host->mmc),
  347. host->clk_rate, ret);
  348. ret = clk_reset(host->clk, CLK_RESET_DEASSERT);
  349. if (ret)
  350. pr_err("%s: Clock deassert failed at %u Hz" \
  351. " with err %d\n", mmc_hostname(host->mmc),
  352. host->clk_rate, ret);
  353. mb();
  354. /* Give some delay for clock reset to propogate to controller */
  355. msmsdcc_delay(host);
  356. }
  357. }
  358. static void msmsdcc_reset_and_restore(struct msmsdcc_host *host)
  359. {
  360. if (is_soft_reset(host)) {
  361. msmsdcc_soft_reset(host);
  362. pr_debug("%s: Applied soft reset to Controller\n",
  363. mmc_hostname(host->mmc));
  364. } else {
  365. /* Give Clock reset (hard reset) to controller */
  366. u32 mci_clk = 0;
  367. u32 mci_mask0 = 0;
  368. u32 dll_config = 0;
  369. /* Save the controller state */
  370. mci_clk = readl_relaxed(host->base + MMCICLOCK);
  371. mci_mask0 = readl_relaxed(host->base + MMCIMASK0);
  372. host->pwr = readl_relaxed(host->base + MMCIPOWER);
  373. if (host->tuning_needed)
  374. dll_config = readl_relaxed(host->base + MCI_DLL_CONFIG);
  375. mb();
  376. msmsdcc_hard_reset(host);
  377. pr_debug("%s: Applied hard reset to controller\n",
  378. mmc_hostname(host->mmc));
  379. /* Restore the contoller state */
  380. writel_relaxed(host->pwr, host->base + MMCIPOWER);
  381. msmsdcc_sync_reg_wr(host);
  382. writel_relaxed(mci_clk, host->base + MMCICLOCK);
  383. msmsdcc_sync_reg_wr(host);
  384. writel_relaxed(mci_mask0, host->base + MMCIMASK0);
  385. if (host->tuning_needed)
  386. writel_relaxed(dll_config, host->base + MCI_DLL_CONFIG);
  387. mb(); /* no delay required after writing to MASK0 register */
  388. }
  389. if (is_sps_mode(host))
  390. /*
  391. * delay the SPS BAM reset in thread context as
  392. * sps_connect/sps_disconnect APIs can be called
  393. * only from non-atomic context.
  394. */
  395. host->sps.reset_bam = true;
  396. if (host->dummy_52_needed)
  397. host->dummy_52_needed = 0;
  398. }
  399. static void msmsdcc_reset_dpsm(struct msmsdcc_host *host)
  400. {
  401. struct mmc_request *mrq = host->curr.mrq;
  402. if (!mrq || !mrq->cmd || !mrq->data)
  403. goto out;
  404. /*
  405. * If we have not waited for the prog done for write transfer then
  406. * perform the DPSM reset without polling for TXACTIVE.
  407. * Otherwise, we poll here unnecessarily as TXACTIVE will not be
  408. * deasserted until DAT0 (Busy line) goes high.
  409. */
  410. if (mrq->data->flags & MMC_DATA_WRITE) {
  411. if (!msmsdcc_is_wait_for_prog_done(host, mrq)) {
  412. if (is_wait_for_tx_rx_active(host) &&
  413. !is_auto_prog_done(host))
  414. pr_warning("%s: %s: AUTO_PROG_DONE capability is must\n",
  415. mmc_hostname(host->mmc), __func__);
  416. goto no_polling;
  417. }
  418. }
  419. /* Make sure h/w (TX/RX) is inactive before resetting DPSM */
  420. if (is_wait_for_tx_rx_active(host)) {
  421. ktime_t start = ktime_get();
  422. while (readl_relaxed(host->base + MMCISTATUS) &
  423. (MCI_TXACTIVE | MCI_RXACTIVE)) {
  424. /*
  425. * TX/RX active bits may be asserted for 4HCLK + 4MCLK
  426. * cycles (~11us) after data transfer due to clock mux
  427. * switching delays. Let's poll for 1ms and panic if
  428. * still active.
  429. */
  430. if (ktime_to_us(ktime_sub(ktime_get(), start)) > 1000) {
  431. pr_err("%s: %s still active\n",
  432. mmc_hostname(host->mmc),
  433. readl_relaxed(host->base + MMCISTATUS)
  434. & MCI_TXACTIVE ? "TX" : "RX");
  435. msmsdcc_dump_sdcc_state(host);
  436. msmsdcc_reset_and_restore(host);
  437. goto out;
  438. }
  439. }
  440. }
  441. no_polling:
  442. writel_relaxed(0, host->base + MMCIDATACTRL);
  443. msmsdcc_sync_reg_wr(host); /* Allow the DPSM to be reset */
  444. out:
  445. return;
  446. }
  447. static int
  448. msmsdcc_request_end(struct msmsdcc_host *host, struct mmc_request *mrq)
  449. {
  450. int retval = 0;
  451. BUG_ON(host->curr.data);
  452. del_timer(&host->req_tout_timer);
  453. if (mrq->data)
  454. mrq->data->bytes_xfered = host->curr.data_xfered;
  455. msmsdcc_reset_dpsm(host);
  456. /* Clear current request information as current request has ended */
  457. memset(&host->curr, 0, sizeof(struct msmsdcc_curr_req));
  458. /*
  459. * Need to drop the host lock here; mmc_request_done may call
  460. * back into the driver...
  461. */
  462. spin_unlock(&host->lock);
  463. mmc_request_done(host->mmc, mrq);
  464. spin_lock(&host->lock);
  465. return retval;
  466. }
  467. static void
  468. msmsdcc_stop_data(struct msmsdcc_host *host)
  469. {
  470. host->curr.data = NULL;
  471. host->curr.got_dataend = 0;
  472. host->curr.wait_for_auto_prog_done = false;
  473. host->curr.got_auto_prog_done = false;
  474. }
  475. static inline uint32_t msmsdcc_fifo_addr(struct msmsdcc_host *host)
  476. {
  477. return host->core_memres->start + MMCIFIFO;
  478. }
  479. static inline unsigned int msmsdcc_get_min_sup_clk_rate(
  480. struct msmsdcc_host *host);
  481. static inline void msmsdcc_sync_reg_wr(struct msmsdcc_host *host)
  482. {
  483. mb();
  484. if (!is_wait_for_reg_write(host))
  485. udelay(host->reg_write_delay);
  486. else if (readl_relaxed(host->base + MCI_STATUS2) &
  487. MCI_MCLK_REG_WR_ACTIVE) {
  488. ktime_t start, diff;
  489. start = ktime_get();
  490. while (readl_relaxed(host->base + MCI_STATUS2) &
  491. MCI_MCLK_REG_WR_ACTIVE) {
  492. diff = ktime_sub(ktime_get(), start);
  493. /* poll for max. 1 ms */
  494. if (ktime_to_us(diff) > 1000) {
  495. pr_warning("%s: previous reg. write is"
  496. " still active\n",
  497. mmc_hostname(host->mmc));
  498. break;
  499. }
  500. }
  501. }
  502. }
  503. static inline void msmsdcc_delay(struct msmsdcc_host *host)
  504. {
  505. udelay(host->reg_write_delay);
  506. }
  507. static inline void
  508. msmsdcc_start_command_exec(struct msmsdcc_host *host, u32 arg, u32 c)
  509. {
  510. writel_relaxed(arg, host->base + MMCIARGUMENT);
  511. writel_relaxed(c, host->base + MMCICOMMAND);
  512. /*
  513. * As after sending the command, we don't write any of the
  514. * controller registers and just wait for the
  515. * CMD_RESPOND_END/CMD_SENT/Command failure notication
  516. * from Controller.
  517. */
  518. mb();
  519. }
  520. static void
  521. msmsdcc_dma_exec_func(struct msm_dmov_cmd *cmd)
  522. {
  523. struct msmsdcc_host *host = (struct msmsdcc_host *)cmd->user;
  524. writel_relaxed(host->cmd_timeout, host->base + MMCIDATATIMER);
  525. writel_relaxed((unsigned int)host->curr.xfer_size,
  526. host->base + MMCIDATALENGTH);
  527. writel_relaxed(host->cmd_datactrl, host->base + MMCIDATACTRL);
  528. msmsdcc_sync_reg_wr(host); /* Force delay prior to ADM or command */
  529. if (host->cmd_cmd) {
  530. msmsdcc_start_command_exec(host,
  531. (u32)host->cmd_cmd->arg, (u32)host->cmd_c);
  532. }
  533. }
  534. static void
  535. msmsdcc_dma_complete_tlet(unsigned long data)
  536. {
  537. struct msmsdcc_host *host = (struct msmsdcc_host *)data;
  538. unsigned long flags;
  539. struct mmc_request *mrq;
  540. spin_lock_irqsave(&host->lock, flags);
  541. mrq = host->curr.mrq;
  542. BUG_ON(!mrq);
  543. if (!(host->dma.result & DMOV_RSLT_VALID)) {
  544. pr_err("msmsdcc: Invalid DataMover result\n");
  545. goto out;
  546. }
  547. if (host->dma.result & DMOV_RSLT_DONE) {
  548. host->curr.data_xfered = host->curr.xfer_size;
  549. host->curr.xfer_remain -= host->curr.xfer_size;
  550. } else {
  551. /* Error or flush */
  552. if (host->dma.result & DMOV_RSLT_ERROR)
  553. pr_err("%s: DMA error (0x%.8x)\n",
  554. mmc_hostname(host->mmc), host->dma.result);
  555. if (host->dma.result & DMOV_RSLT_FLUSH)
  556. pr_err("%s: DMA channel flushed (0x%.8x)\n",
  557. mmc_hostname(host->mmc), host->dma.result);
  558. pr_err("Flush data: %.8x %.8x %.8x %.8x %.8x %.8x\n",
  559. host->dma.err.flush[0], host->dma.err.flush[1],
  560. host->dma.err.flush[2], host->dma.err.flush[3],
  561. host->dma.err.flush[4],
  562. host->dma.err.flush[5]);
  563. msmsdcc_reset_and_restore(host);
  564. if (!mrq->data->error)
  565. mrq->data->error = -EIO;
  566. }
  567. if (!mrq->data->host_cookie)
  568. dma_unmap_sg(mmc_dev(host->mmc), host->dma.sg,
  569. host->dma.num_ents, host->dma.dir);
  570. if (host->curr.user_pages) {
  571. struct scatterlist *sg = host->dma.sg;
  572. int i;
  573. for (i = 0; i < host->dma.num_ents; i++, sg++)
  574. flush_dcache_page(sg_page(sg));
  575. }
  576. host->dma.sg = NULL;
  577. host->dma.busy = 0;
  578. if ((host->curr.got_dataend && (!host->curr.wait_for_auto_prog_done ||
  579. (host->curr.wait_for_auto_prog_done &&
  580. host->curr.got_auto_prog_done))) || mrq->data->error) {
  581. /*
  582. * If we've already gotten our DATAEND / DATABLKEND
  583. * for this request, then complete it through here.
  584. */
  585. if (!mrq->data->error) {
  586. host->curr.data_xfered = host->curr.xfer_size;
  587. host->curr.xfer_remain -= host->curr.xfer_size;
  588. }
  589. if (host->dummy_52_needed) {
  590. mrq->data->bytes_xfered = host->curr.data_xfered;
  591. host->dummy_52_sent = 1;
  592. msmsdcc_start_command(host, &dummy52cmd,
  593. MCI_CPSM_PROGENA);
  594. goto out;
  595. }
  596. msmsdcc_stop_data(host);
  597. if (!mrq->data->stop || mrq->cmd->error ||
  598. (mrq->sbc && !mrq->data->error)) {
  599. mrq->data->bytes_xfered = host->curr.data_xfered;
  600. msmsdcc_reset_dpsm(host);
  601. del_timer(&host->req_tout_timer);
  602. /*
  603. * Clear current request information as current
  604. * request has ended
  605. */
  606. memset(&host->curr, 0, sizeof(struct msmsdcc_curr_req));
  607. spin_unlock_irqrestore(&host->lock, flags);
  608. mmc_request_done(host->mmc, mrq);
  609. return;
  610. } else if (mrq->data->stop && ((mrq->sbc && mrq->data->error)
  611. || !mrq->sbc)) {
  612. msmsdcc_start_command(host, mrq->data->stop, 0);
  613. }
  614. }
  615. out:
  616. spin_unlock_irqrestore(&host->lock, flags);
  617. return;
  618. }
  619. #ifdef CONFIG_MMC_MSM_SPS_SUPPORT
  620. /**
  621. * Callback notification from SPS driver
  622. *
  623. * This callback function gets triggered called from
  624. * SPS driver when requested SPS data transfer is
  625. * completed.
  626. *
  627. * SPS driver invokes this callback in BAM irq context so
  628. * SDCC driver schedule a tasklet for further processing
  629. * this callback notification at later point of time in
  630. * tasklet context and immediately returns control back
  631. * to SPS driver.
  632. *
  633. * @nofity - Pointer to sps event notify sturcture
  634. *
  635. */
  636. static void
  637. msmsdcc_sps_complete_cb(struct sps_event_notify *notify)
  638. {
  639. struct msmsdcc_host *host =
  640. (struct msmsdcc_host *)
  641. ((struct sps_event_notify *)notify)->user;
  642. host->sps.notify = *notify;
  643. pr_debug("%s: %s: sps ev_id=%d, addr=0x%x, size=0x%x, flags=0x%x\n",
  644. mmc_hostname(host->mmc), __func__, notify->event_id,
  645. notify->data.transfer.iovec.addr,
  646. notify->data.transfer.iovec.size,
  647. notify->data.transfer.iovec.flags);
  648. /* Schedule a tasklet for completing data transfer */
  649. tasklet_schedule(&host->sps.tlet);
  650. }
  651. /**
  652. * Tasklet handler for processing SPS callback event
  653. *
  654. * This function processing SPS event notification and
  655. * checks if the SPS transfer is completed or not and
  656. * then accordingly notifies status to MMC core layer.
  657. *
  658. * This function is called in tasklet context.
  659. *
  660. * @data - Pointer to sdcc driver data
  661. *
  662. */
  663. static void msmsdcc_sps_complete_tlet(unsigned long data)
  664. {
  665. unsigned long flags;
  666. int i, rc;
  667. u32 data_xfered = 0;
  668. struct mmc_request *mrq;
  669. struct sps_iovec iovec;
  670. struct sps_pipe *sps_pipe_handle;
  671. struct msmsdcc_host *host = (struct msmsdcc_host *)data;
  672. struct sps_event_notify *notify = &host->sps.notify;
  673. spin_lock_irqsave(&host->lock, flags);
  674. if (host->sps.dir == DMA_FROM_DEVICE)
  675. sps_pipe_handle = host->sps.prod.pipe_handle;
  676. else
  677. sps_pipe_handle = host->sps.cons.pipe_handle;
  678. mrq = host->curr.mrq;
  679. if (!mrq) {
  680. spin_unlock_irqrestore(&host->lock, flags);
  681. return;
  682. }
  683. pr_debug("%s: %s: sps event_id=%d\n",
  684. mmc_hostname(host->mmc), __func__,
  685. notify->event_id);
  686. /*
  687. * Got End of transfer event!!! Check if all of the data
  688. * has been transferred?
  689. */
  690. for (i = 0; i < host->sps.xfer_req_cnt; i++) {
  691. rc = sps_get_iovec(sps_pipe_handle, &iovec);
  692. if (rc) {
  693. pr_err("%s: %s: sps_get_iovec() failed rc=%d, i=%d",
  694. mmc_hostname(host->mmc), __func__, rc, i);
  695. break;
  696. }
  697. data_xfered += iovec.size;
  698. }
  699. if (data_xfered == host->curr.xfer_size) {
  700. host->curr.data_xfered = host->curr.xfer_size;
  701. host->curr.xfer_remain -= host->curr.xfer_size;
  702. pr_debug("%s: Data xfer success. data_xfered=0x%x",
  703. mmc_hostname(host->mmc),
  704. host->curr.xfer_size);
  705. } else {
  706. pr_err("%s: Data xfer failed. data_xfered=0x%x,"
  707. " xfer_size=%d", mmc_hostname(host->mmc),
  708. data_xfered, host->curr.xfer_size);
  709. msmsdcc_reset_and_restore(host);
  710. if (!mrq->data->error)
  711. mrq->data->error = -EIO;
  712. }
  713. /* Unmap sg buffers */
  714. if (!mrq->data->host_cookie)
  715. dma_unmap_sg(mmc_dev(host->mmc), host->sps.sg,
  716. host->sps.num_ents, host->sps.dir);
  717. host->sps.sg = NULL;
  718. host->sps.busy = 0;
  719. if ((host->curr.got_dataend && (!host->curr.wait_for_auto_prog_done ||
  720. (host->curr.wait_for_auto_prog_done &&
  721. host->curr.got_auto_prog_done))) || mrq->data->error) {
  722. /*
  723. * If we've already gotten our DATAEND / DATABLKEND
  724. * for this request, then complete it through here.
  725. */
  726. if (!mrq->data->error) {
  727. host->curr.data_xfered = host->curr.xfer_size;
  728. host->curr.xfer_remain -= host->curr.xfer_size;
  729. }
  730. if (host->dummy_52_needed) {
  731. mrq->data->bytes_xfered = host->curr.data_xfered;
  732. host->dummy_52_sent = 1;
  733. msmsdcc_start_command(host, &dummy52cmd,
  734. MCI_CPSM_PROGENA);
  735. spin_unlock_irqrestore(&host->lock, flags);
  736. return;
  737. }
  738. msmsdcc_stop_data(host);
  739. if (!mrq->data->stop || mrq->cmd->error ||
  740. (mrq->sbc && !mrq->data->error)) {
  741. mrq->data->bytes_xfered = host->curr.data_xfered;
  742. msmsdcc_reset_dpsm(host);
  743. del_timer(&host->req_tout_timer);
  744. /*
  745. * Clear current request information as current
  746. * request has ended
  747. */
  748. memset(&host->curr, 0, sizeof(struct msmsdcc_curr_req));
  749. spin_unlock_irqrestore(&host->lock, flags);
  750. mmc_request_done(host->mmc, mrq);
  751. return;
  752. } else if (mrq->data->stop && ((mrq->sbc && mrq->data->error)
  753. || !mrq->sbc)) {
  754. msmsdcc_start_command(host, mrq->data->stop, 0);
  755. }
  756. }
  757. spin_unlock_irqrestore(&host->lock, flags);
  758. }
  759. /**
  760. * Exit from current SPS data transfer
  761. *
  762. * This function exits from current SPS data transfer.
  763. *
  764. * This function should be called when error condition
  765. * is encountered during data transfer.
  766. *
  767. * @host - Pointer to sdcc host structure
  768. *
  769. */
  770. static void msmsdcc_sps_exit_curr_xfer(struct msmsdcc_host *host)
  771. {
  772. struct mmc_request *mrq;
  773. mrq = host->curr.mrq;
  774. BUG_ON(!mrq);
  775. msmsdcc_reset_and_restore(host);
  776. if (!mrq->data->error)
  777. mrq->data->error = -EIO;
  778. /* Unmap sg buffers */
  779. if (!mrq->data->host_cookie)
  780. dma_unmap_sg(mmc_dev(host->mmc), host->sps.sg,
  781. host->sps.num_ents, host->sps.dir);
  782. host->sps.sg = NULL;
  783. host->sps.busy = 0;
  784. if (host->curr.data)
  785. msmsdcc_stop_data(host);
  786. if (!mrq->data->stop || mrq->cmd->error ||
  787. (mrq->sbc && !mrq->data->error))
  788. msmsdcc_request_end(host, mrq);
  789. else if (mrq->data->stop && ((mrq->sbc && mrq->data->error)
  790. || !mrq->sbc))
  791. msmsdcc_start_command(host, mrq->data->stop, 0);
  792. }
  793. #else
  794. static inline void msmsdcc_sps_complete_cb(struct sps_event_notify *notify) { }
  795. static inline void msmsdcc_sps_complete_tlet(unsigned long data) { }
  796. static inline void msmsdcc_sps_exit_curr_xfer(struct msmsdcc_host *host) { }
  797. #endif /* CONFIG_MMC_MSM_SPS_SUPPORT */
  798. static int msmsdcc_enable_cdr_cm_sdc4_dll(struct msmsdcc_host *host);
  799. static void
  800. msmsdcc_dma_complete_func(struct msm_dmov_cmd *cmd,
  801. unsigned int result,
  802. struct msm_dmov_errdata *err)
  803. {
  804. struct msmsdcc_dma_data *dma_data =
  805. container_of(cmd, struct msmsdcc_dma_data, hdr);
  806. struct msmsdcc_host *host = dma_data->host;
  807. dma_data->result = result;
  808. if (err)
  809. memcpy(&dma_data->err, err, sizeof(struct msm_dmov_errdata));
  810. tasklet_schedule(&host->dma_tlet);
  811. }
  812. static bool msmsdcc_is_dma_possible(struct msmsdcc_host *host,
  813. struct mmc_data *data)
  814. {
  815. bool ret = true;
  816. u32 xfer_size = data->blksz * data->blocks;
  817. if (host->enforce_pio_mode) {
  818. ret = false;
  819. goto out;
  820. }
  821. if (is_sps_mode(host)) {
  822. /*
  823. * BAM Mode: Fall back on PIO if size is less
  824. * than or equal to SPS_MIN_XFER_SIZE bytes.
  825. */
  826. if (xfer_size <= SPS_MIN_XFER_SIZE)
  827. ret = false;
  828. } else if (is_dma_mode(host)) {
  829. /*
  830. * ADM Mode: Fall back on PIO if size is less than FIFO size
  831. * or not integer multiple of FIFO size
  832. */
  833. if (xfer_size % MCI_FIFOSIZE)
  834. ret = false;
  835. } else {
  836. /* PIO Mode */
  837. ret = false;
  838. }
  839. out:
  840. return ret;
  841. }
  842. static int msmsdcc_config_dma(struct msmsdcc_host *host, struct mmc_data *data)
  843. {
  844. struct msmsdcc_nc_dmadata *nc;
  845. dmov_box *box;
  846. uint32_t rows;
  847. int n;
  848. int i, err = 0, box_cmd_cnt = 0;
  849. struct scatterlist *sg = data->sg;
  850. unsigned int len, offset;
  851. if ((host->dma.channel == -1) || (host->dma.crci == -1))
  852. return -ENOENT;
  853. BUG_ON((host->pdev->id < 1) || (host->pdev->id > 5));
  854. host->dma.sg = data->sg;
  855. host->dma.num_ents = data->sg_len;
  856. /* Prevent memory corruption */
  857. BUG_ON(host->dma.num_ents > msmsdcc_get_nr_sg(host));
  858. nc = host->dma.nc;
  859. if (data->flags & MMC_DATA_READ)
  860. host->dma.dir = DMA_FROM_DEVICE;
  861. else
  862. host->dma.dir = DMA_TO_DEVICE;
  863. if (!data->host_cookie) {
  864. n = msmsdcc_prep_xfer(host, data);
  865. if (unlikely(n < 0)) {
  866. host->dma.sg = NULL;
  867. host->dma.num_ents = 0;
  868. return -ENOMEM;
  869. }
  870. }
  871. /* host->curr.user_pages = (data->flags & MMC_DATA_USERPAGE); */
  872. host->curr.user_pages = 0;
  873. box = &nc->cmd[0];
  874. for (i = 0; i < host->dma.num_ents; i++) {
  875. len = sg_dma_len(sg);
  876. offset = 0;
  877. do {
  878. /* Check if we can do DMA */
  879. if (!len || (box_cmd_cnt >= MMC_MAX_DMA_CMDS)) {
  880. err = -ENOTSUPP;
  881. goto unmap;
  882. }
  883. box->cmd = CMD_MODE_BOX;
  884. if (len >= MMC_MAX_DMA_BOX_LENGTH) {
  885. len = MMC_MAX_DMA_BOX_LENGTH;
  886. len -= len % data->blksz;
  887. }
  888. rows = (len % MCI_FIFOSIZE) ?
  889. (len / MCI_FIFOSIZE) + 1 :
  890. (len / MCI_FIFOSIZE);
  891. if (data->flags & MMC_DATA_READ) {
  892. box->src_row_addr = msmsdcc_fifo_addr(host);
  893. box->dst_row_addr = sg_dma_address(sg) + offset;
  894. box->src_dst_len = (MCI_FIFOSIZE << 16) |
  895. (MCI_FIFOSIZE);
  896. box->row_offset = MCI_FIFOSIZE;
  897. box->num_rows = rows * ((1 << 16) + 1);
  898. box->cmd |= CMD_SRC_CRCI(host->dma.crci);
  899. } else {
  900. box->src_row_addr = sg_dma_address(sg) + offset;
  901. box->dst_row_addr = msmsdcc_fifo_addr(host);
  902. box->src_dst_len = (MCI_FIFOSIZE << 16) |
  903. (MCI_FIFOSIZE);
  904. box->row_offset = (MCI_FIFOSIZE << 16);
  905. box->num_rows = rows * ((1 << 16) + 1);
  906. box->cmd |= CMD_DST_CRCI(host->dma.crci);
  907. }
  908. offset += len;
  909. len = sg_dma_len(sg) - offset;
  910. box++;
  911. box_cmd_cnt++;
  912. } while (len);
  913. sg++;
  914. }
  915. /* Mark last command */
  916. box--;
  917. box->cmd |= CMD_LC;
  918. /* location of command block must be 64 bit aligned */
  919. BUG_ON(host->dma.cmd_busaddr & 0x07);
  920. nc->cmdptr = (host->dma.cmd_busaddr >> 3) | CMD_PTR_LP;
  921. host->dma.hdr.cmdptr = DMOV_CMD_PTR_LIST |
  922. DMOV_CMD_ADDR(host->dma.cmdptr_busaddr);
  923. host->dma.hdr.complete_func = msmsdcc_dma_complete_func;
  924. /* Flush all data to memory before starting dma */
  925. mb();
  926. unmap:
  927. if (err) {
  928. if (!data->host_cookie)
  929. dma_unmap_sg(mmc_dev(host->mmc), host->dma.sg,
  930. host->dma.num_ents, host->dma.dir);
  931. pr_err("%s: cannot do DMA, fall back to PIO mode err=%d\n",
  932. mmc_hostname(host->mmc), err);
  933. }
  934. return err;
  935. }
  936. static int msmsdcc_prep_xfer(struct msmsdcc_host *host,
  937. struct mmc_data *data)
  938. {
  939. int rc = 0;
  940. unsigned int dir;
  941. /* Prevent memory corruption */
  942. BUG_ON(data->sg_len > msmsdcc_get_nr_sg(host));
  943. if (data->flags & MMC_DATA_READ)
  944. dir = DMA_FROM_DEVICE;
  945. else
  946. dir = DMA_TO_DEVICE;
  947. /* Make sg buffers DMA ready */
  948. rc = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  949. dir);
  950. if (unlikely(rc != data->sg_len)) {
  951. pr_err("%s: Unable to map in all sg elements, rc=%d\n",
  952. mmc_hostname(host->mmc), rc);
  953. rc = -ENOMEM;
  954. goto dma_map_err;
  955. }
  956. pr_debug("%s: %s: %s: sg_len=%d\n",
  957. mmc_hostname(host->mmc), __func__,
  958. dir == DMA_FROM_DEVICE ? "READ" : "WRITE",
  959. data->sg_len);
  960. goto out;
  961. dma_map_err:
  962. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  963. data->flags);
  964. out:
  965. return rc;
  966. }
  967. #ifdef CONFIG_MMC_MSM_SPS_SUPPORT
  968. /**
  969. * Submits data transfer request to SPS driver
  970. *
  971. * This function make sg (scatter gather) data buffers
  972. * DMA ready and then submits them to SPS driver for
  973. * transfer.
  974. *
  975. * @host - Pointer to sdcc host structure
  976. * @data - Pointer to mmc_data structure
  977. *
  978. * @return 0 if success else negative value
  979. */
  980. static int msmsdcc_sps_start_xfer(struct msmsdcc_host *host,
  981. struct mmc_data *data)
  982. {
  983. int rc = 0;
  984. u32 flags;
  985. int i;
  986. u32 addr, len, data_cnt;
  987. struct scatterlist *sg = data->sg;
  988. struct sps_pipe *sps_pipe_handle;
  989. host->sps.sg = data->sg;
  990. host->sps.num_ents = data->sg_len;
  991. host->sps.xfer_req_cnt = 0;
  992. if (data->flags & MMC_DATA_READ) {
  993. host->sps.dir = DMA_FROM_DEVICE;
  994. sps_pipe_handle = host->sps.prod.pipe_handle;
  995. } else {
  996. host->sps.dir = DMA_TO_DEVICE;
  997. sps_pipe_handle = host->sps.cons.pipe_handle;
  998. }
  999. if (!data->host_cookie) {
  1000. rc = msmsdcc_prep_xfer(host, data);
  1001. if (unlikely(rc < 0)) {
  1002. host->dma.sg = NULL;
  1003. host->dma.num_ents = 0;
  1004. goto out;
  1005. }
  1006. }
  1007. for (i = 0; i < data->sg_len; i++) {
  1008. /*
  1009. * Check if this is the last buffer to transfer?
  1010. * If yes then set the INT and EOT flags.
  1011. */
  1012. len = sg_dma_len(sg);
  1013. addr = sg_dma_address(sg);
  1014. flags = 0;
  1015. while (len > 0) {
  1016. if (len > SPS_MAX_DESC_SIZE) {
  1017. data_cnt = SPS_MAX_DESC_SIZE;
  1018. } else {
  1019. data_cnt = len;
  1020. if ((i == data->sg_len - 1) &&
  1021. (sps_pipe_handle ==
  1022. host->sps.cons.pipe_handle)) {
  1023. /*
  1024. * set EOT only for consumer pipe, for
  1025. * producer pipe h/w will set it.
  1026. */
  1027. flags = SPS_IOVEC_FLAG_INT |
  1028. SPS_IOVEC_FLAG_EOT;
  1029. }
  1030. }
  1031. rc = sps_transfer_one(sps_pipe_handle, addr,
  1032. data_cnt, host, flags);
  1033. if (rc) {
  1034. pr_err("%s: sps_transfer_one() error! rc=%d,"
  1035. " pipe=0x%x, sg=0x%x, sg_buf_no=%d\n",
  1036. mmc_hostname(host->mmc), rc,
  1037. (u32)sps_pipe_handle, (u32)sg, i);
  1038. goto dma_map_err;
  1039. }
  1040. addr += data_cnt;
  1041. len -= data_cnt;
  1042. host->sps.xfer_req_cnt++;
  1043. }
  1044. sg++;
  1045. }
  1046. goto out;
  1047. dma_map_err:
  1048. /* unmap sg buffers */
  1049. if (!data->host_cookie)
  1050. dma_unmap_sg(mmc_dev(host->mmc), host->sps.sg,
  1051. host->sps.num_ents, host->sps.dir);
  1052. out:
  1053. return rc;
  1054. }
  1055. #else
  1056. static int msmsdcc_sps_start_xfer(struct msmsdcc_host *host,
  1057. struct mmc_data *data) { return 0; }
  1058. #endif /* CONFIG_MMC_MSM_SPS_SUPPORT */
  1059. static void
  1060. msmsdcc_start_command_deferred(struct msmsdcc_host *host,
  1061. struct mmc_command *cmd, u32 *c)
  1062. {
  1063. DBG(host, "op %02x arg %08x flags %08x\n",
  1064. cmd->opcode, cmd->arg, cmd->flags);
  1065. *c |= (cmd->opcode | MCI_CPSM_ENABLE);
  1066. if (cmd->flags & MMC_RSP_PRESENT) {
  1067. if (cmd->flags & MMC_RSP_136)
  1068. *c |= MCI_CPSM_LONGRSP;
  1069. *c |= MCI_CPSM_RESPONSE;
  1070. }
  1071. if (/*interrupt*/0)
  1072. *c |= MCI_CPSM_INTERRUPT;
  1073. /* DAT_CMD bit should be set for all ADTC */
  1074. if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
  1075. *c |= MCI_CSPM_DATCMD;
  1076. /* Check if AUTO CMD19/CMD21 is required or not? */
  1077. if (host->tuning_needed && (cmd->mrq->data &&
  1078. (cmd->mrq->data->flags & MMC_DATA_READ)) &&
  1079. (host->en_auto_cmd19 || host->en_auto_cmd21)) {
  1080. /*
  1081. * For open ended block read operation (without CMD23),
  1082. * AUTO_CMD19/AUTO_CMD21 bit should be set while sending
  1083. * the READ command.
  1084. * For close ended block read operation (with CMD23),
  1085. * AUTO_CMD19/AUTO_CMD21 bit should be set while sending
  1086. * CMD23.
  1087. */
  1088. if ((cmd->opcode == MMC_SET_BLOCK_COUNT &&
  1089. host->curr.mrq->cmd->opcode ==
  1090. MMC_READ_MULTIPLE_BLOCK) ||
  1091. (!host->curr.mrq->sbc &&
  1092. (cmd->opcode == MMC_READ_SINGLE_BLOCK ||
  1093. cmd->opcode == MMC_READ_MULTIPLE_BLOCK ||
  1094. cmd->opcode == SD_IO_RW_EXTENDED))) {
  1095. msmsdcc_enable_cdr_cm_sdc4_dll(host);
  1096. if (host->en_auto_cmd19 &&
  1097. host->mmc->ios.timing == MMC_TIMING_UHS_SDR104)
  1098. *c |= MCI_CSPM_AUTO_CMD19;
  1099. else if (host->en_auto_cmd21 &&
  1100. host->mmc->ios.timing == MMC_TIMING_MMC_HS200)
  1101. *c |= MCI_CSPM_AUTO_CMD21;
  1102. }
  1103. }
  1104. if (cmd->mrq->data && (cmd->mrq->data->flags & MMC_DATA_READ))
  1105. writel_relaxed((readl_relaxed(host->base +
  1106. MCI_DLL_CONFIG) | MCI_CDR_EN),
  1107. host->base + MCI_DLL_CONFIG);
  1108. else
  1109. /* Clear CDR_EN bit for non read operations */
  1110. writel_relaxed((readl_relaxed(host->base +
  1111. MCI_DLL_CONFIG) & ~MCI_CDR_EN),
  1112. host->base + MCI_DLL_CONFIG);
  1113. if ((cmd->flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
  1114. *c |= MCI_CPSM_PROGENA;
  1115. host->prog_enable = 1;
  1116. }
  1117. if (cmd == cmd->mrq->stop)
  1118. *c |= MCI_CSPM_MCIABORT;
  1119. if (host->curr.cmd != NULL) {
  1120. pr_err("%s: Overlapping command requests\n",
  1121. mmc_hostname(host->mmc));
  1122. }
  1123. host->curr.cmd = cmd;
  1124. }
  1125. static void
  1126. msmsdcc_start_data(struct msmsdcc_host *host, struct mmc_data *data,
  1127. struct mmc_command *cmd, u32 c)
  1128. {
  1129. unsigned int datactrl = 0, timeout;
  1130. unsigned long long clks;
  1131. void __iomem *base = host->base;
  1132. unsigned int pio_irqmask = 0;
  1133. BUG_ON(!data->sg);
  1134. BUG_ON(!data->sg_len);
  1135. host->curr.data = data;
  1136. host->curr.xfer_size = data->blksz * data->blocks;
  1137. host->curr.xfer_remain = host->curr.xfer_size;
  1138. host->curr.data_xfered = 0;
  1139. host->curr.got_dataend = 0;
  1140. host->curr.got_auto_prog_done = false;
  1141. datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
  1142. if (host->curr.wait_for_auto_prog_done)
  1143. datactrl |= MCI_AUTO_PROG_DONE;
  1144. if (msmsdcc_is_dma_possible(host, data)) {
  1145. if (is_dma_mode(host) && !msmsdcc_config_dma(host, data)) {
  1146. datactrl |= MCI_DPSM_DMAENABLE;
  1147. } else if (is_sps_mode(host)) {
  1148. if (!msmsdcc_sps_start_xfer(host, data)) {
  1149. /* Now kick start DML transfer */
  1150. mb();
  1151. msmsdcc_dml_start_xfer(host, data);
  1152. datactrl |= MCI_DPSM_DMAENABLE;
  1153. host->sps.busy = 1;
  1154. }
  1155. }
  1156. }
  1157. /* Is data transfer in PIO mode required? */
  1158. if (!(datactrl & MCI_DPSM_DMAENABLE)) {
  1159. if (data->flags & MMC_DATA_READ) {
  1160. pio_irqmask = MCI_RXFIFOHALFFULLMASK;
  1161. if (host->curr.xfer_remain < MCI_FIFOSIZE)
  1162. pio_irqmask |= MCI_RXDATAAVLBLMASK;
  1163. } else
  1164. pio_irqmask = MCI_TXFIFOHALFEMPTYMASK |
  1165. MCI_TXFIFOEMPTYMASK;
  1166. msmsdcc_sg_start(host);
  1167. }
  1168. if (data->flags & MMC_DATA_READ)
  1169. datactrl |= (MCI_DPSM_DIRECTION | MCI_RX_DATA_PEND);
  1170. else if (host->curr.use_wr_data_pend)
  1171. datactrl |= MCI_DATA_PEND;
  1172. if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50)
  1173. clks = (unsigned long long)data->timeout_ns *
  1174. (host->clk_rate / 2);
  1175. else
  1176. clks = (unsigned long long)data->timeout_ns * host->clk_rate;
  1177. do_div(clks, 1000000000UL);
  1178. timeout = data->timeout_clks + (unsigned int)clks*2 ;
  1179. WARN(!timeout,
  1180. "%s: data timeout is zero. timeout_ns=0x%x, timeout_clks=0x%x\n",
  1181. mmc_hostname(host->mmc), data->timeout_ns, data->timeout_clks);
  1182. if (is_dma_mode(host) && (datactrl & MCI_DPSM_DMAENABLE)) {
  1183. /* Use ADM (Application Data Mover) HW for Data transfer */
  1184. /* Save parameters for the dma exec function */
  1185. host->cmd_timeout = timeout;
  1186. host->cmd_pio_irqmask = pio_irqmask;
  1187. host->cmd_datactrl = datactrl;
  1188. host->cmd_cmd = cmd;
  1189. host->dma.hdr.exec_func = msmsdcc_dma_exec_func;
  1190. host->dma.hdr.user = (void *)host;
  1191. host->dma.busy = 1;
  1192. if (cmd) {
  1193. msmsdcc_start_command_deferred(host, cmd, &c);
  1194. host->cmd_c = c;
  1195. }
  1196. writel_relaxed((readl_relaxed(host->base + MMCIMASK0) &
  1197. (~(MCI_IRQ_PIO))) | host->cmd_pio_irqmask,
  1198. host->base + MMCIMASK0);
  1199. mb();
  1200. msm_dmov_enqueue_cmd_ext(host->dma.channel, &host->dma.hdr);
  1201. } else {
  1202. /* SPS-BAM mode or PIO mode */
  1203. writel_relaxed(timeout, base + MMCIDATATIMER);
  1204. writel_relaxed(host->curr.xfer_size, base + MMCIDATALENGTH);
  1205. writel_relaxed((readl_relaxed(host->base + MMCIMASK0) &
  1206. (~(MCI_IRQ_PIO))) | pio_irqmask,
  1207. host->base + MMCIMASK0);
  1208. writel_relaxed(datactrl, base + MMCIDATACTRL);
  1209. if (cmd) {
  1210. /* Delay between data/command */
  1211. msmsdcc_sync_reg_wr(host);
  1212. /* Daisy-chain the command if requested */
  1213. msmsdcc_start_command(host, cmd, c);
  1214. } else {
  1215. /*
  1216. * We don't need delay after writing to DATA_CTRL
  1217. * register if we are not writing to CMD register
  1218. * immediately after this. As we already have delay
  1219. * before sending the command, we just need mb() here.
  1220. */
  1221. mb();
  1222. }
  1223. }
  1224. }
  1225. static void
  1226. msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd, u32 c)
  1227. {
  1228. msmsdcc_start_command_deferred(host, cmd, &c);
  1229. msmsdcc_start_command_exec(host, cmd->arg, c);
  1230. }
  1231. static void
  1232. msmsdcc_data_err(struct msmsdcc_host *host, struct mmc_data *data,
  1233. unsigned int status)
  1234. {
  1235. if ((status & MCI_DATACRCFAIL) || (status & MCI_DATATIMEOUT)) {
  1236. u32 opcode = data->mrq->cmd->opcode;
  1237. if (!((!host->tuning_in_progress && opcode == MMC_BUS_TEST_W)
  1238. || (opcode == MMC_BUS_TEST_R) ||
  1239. (host->tuning_in_progress &&
  1240. (opcode == MMC_SEND_TUNING_BLOCK_HS200 ||
  1241. opcode == MMC_SEND_TUNING_BLOCK)))) {
  1242. /* Execute full tuning in case of CRC/timeout errors */
  1243. host->saved_tuning_phase = INVALID_TUNING_PHASE;
  1244. if (status & MCI_DATACRCFAIL) {
  1245. pr_err("%s: Data CRC error\n",
  1246. mmc_hostname(host->mmc));
  1247. pr_err("%s: opcode 0x%.8x\n", __func__, opcode);
  1248. pr_err("%s: blksz %d, blocks %d\n", __func__,
  1249. data->blksz, data->blocks);
  1250. } else {
  1251. pr_err("%s: CMD%d: Data timeout. DAT0 => %d\n",
  1252. mmc_hostname(host->mmc), opcode,
  1253. (readl_relaxed(host->base
  1254. + MCI_TEST_INPUT) & 0x2) ? 1 : 0);
  1255. msmsdcc_dump_sdcc_state(host);
  1256. }
  1257. }
  1258. /*
  1259. * CRC is optional for the bus test commands, not all
  1260. * cards respond back with CRC. However controller
  1261. * waits for the CRC and times out. Hence ignore the
  1262. * data timeouts during the Bustest.
  1263. */
  1264. if (!((!host->tuning_in_progress && opcode == MMC_BUS_TEST_W)
  1265. || (opcode == MMC_BUS_TEST_R))) {
  1266. if (status & MCI_DATACRCFAIL)
  1267. data->error = -EILSEQ;
  1268. else
  1269. data->error = -ETIMEDOUT;
  1270. }
  1271. /* In case of DATA CRC/timeout error, execute tuning again */
  1272. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || defined(CONFIG_BCM4354)
  1273. if (host->tuning_needed&&!host->tuning_in_progress&&(host->pdev->id!=2))
  1274. #else
  1275. if (host->tuning_needed&&!host->tuning_in_progress)
  1276. #endif
  1277. host->tuning_done = false;
  1278. } else if (status & MCI_RXOVERRUN) {
  1279. pr_err("%s: RX overrun\n", mmc_hostname(host->mmc));
  1280. data->error = -EIO;
  1281. } else if (status & MCI_TXUNDERRUN) {
  1282. pr_err("%s: TX underrun\n", mmc_hostname(host->mmc));
  1283. data->error = -EIO;
  1284. } else {
  1285. pr_err("%s: Unknown error (0x%.8x)\n",
  1286. mmc_hostname(host->mmc), status);
  1287. data->error = -EIO;
  1288. }
  1289. /* Dummy CMD52 is not needed when CMD53 has errors */
  1290. if (host->dummy_52_needed)
  1291. host->dummy_52_needed = 0;
  1292. }
  1293. static int
  1294. msmsdcc_pio_read(struct msmsdcc_host *host, char *buffer, unsigned int remain)
  1295. {
  1296. void __iomem *base = host->base;
  1297. uint32_t *ptr = (uint32_t *) buffer;
  1298. int count = 0;
  1299. if (remain % 4)
  1300. remain = ((remain >> 2) + 1) << 2;
  1301. while (readl_relaxed(base + MMCISTATUS) & MCI_RXDATAAVLBL) {
  1302. *ptr = readl_relaxed(base + MMCIFIFO + (count % MCI_FIFOSIZE));
  1303. ptr++;
  1304. count += sizeof(uint32_t);
  1305. remain -= sizeof(uint32_t);
  1306. if (remain == 0)
  1307. break;
  1308. }
  1309. return count;
  1310. }
  1311. static int
  1312. msmsdcc_pio_write(struct msmsdcc_host *host, char *buffer,
  1313. unsigned int remain)
  1314. {
  1315. void __iomem *base = host->base;
  1316. char *ptr = buffer;
  1317. unsigned int maxcnt = MCI_FIFOHALFSIZE;
  1318. while (readl_relaxed(base + MMCISTATUS) &
  1319. (MCI_TXFIFOEMPTY | MCI_TXFIFOHALFEMPTY)) {
  1320. unsigned int count, sz;
  1321. count = min(remain, maxcnt);
  1322. sz = count % 4 ? (count >> 2) + 1 : (count >> 2);
  1323. writesl(base + MMCIFIFO, ptr, sz);
  1324. ptr += count;
  1325. remain -= count;
  1326. if (remain == 0)
  1327. break;
  1328. }
  1329. mb();
  1330. return ptr - buffer;
  1331. }
  1332. /*
  1333. * Copy up to a word (4 bytes) between a scatterlist
  1334. * and a temporary bounce buffer when the word lies across
  1335. * two pages. The temporary buffer can then be read to/
  1336. * written from the FIFO once.
  1337. */
  1338. static void _msmsdcc_sg_consume_word(struct msmsdcc_host *host)
  1339. {
  1340. struct msmsdcc_pio_data *pio = &host->pio;
  1341. unsigned int bytes_avail;
  1342. if (host->curr.data->flags & MMC_DATA_READ)
  1343. memcpy(pio->sg_miter.addr, pio->bounce_buf,
  1344. pio->bounce_buf_len);
  1345. else
  1346. memcpy(pio->bounce_buf, pio->sg_miter.addr,
  1347. pio->bounce_buf_len);
  1348. while (pio->bounce_buf_len != 4) {
  1349. if (!sg_miter_next(&pio->sg_miter))
  1350. break;
  1351. bytes_avail = min_t(unsigned int, pio->sg_miter.length,
  1352. 4 - pio->bounce_buf_len);
  1353. if (host->curr.data->flags & MMC_DATA_READ)
  1354. memcpy(pio->sg_miter.addr,
  1355. &pio->bounce_buf[pio->bounce_buf_len],
  1356. bytes_avail);
  1357. else
  1358. memcpy(&pio->bounce_buf[pio->bounce_buf_len],
  1359. pio->sg_miter.addr, bytes_avail);
  1360. pio->sg_miter.consumed = bytes_avail;
  1361. pio->bounce_buf_len += bytes_avail;
  1362. }
  1363. }
  1364. /*
  1365. * Use sg_miter_next to return as many 4-byte aligned
  1366. * chunks as possible, using a temporary 4 byte buffer
  1367. * for alignment if necessary
  1368. */
  1369. static int msmsdcc_sg_next(struct msmsdcc_host *host, char **buf, int *len)
  1370. {
  1371. struct msmsdcc_pio_data *pio = &host->pio;
  1372. unsigned int length, rlength;
  1373. char *buffer;
  1374. if (!sg_miter_next(&pio->sg_miter))
  1375. return 0;
  1376. buffer = pio->sg_miter.addr;
  1377. length = pio->sg_miter.length;
  1378. if (length < host->curr.xfer_remain) {
  1379. rlength = round_down(length, 4);
  1380. if (rlength) {
  1381. /*
  1382. * We have a 4-byte aligned chunk.
  1383. * The rounding will be reflected by
  1384. * a call to msmsdcc_sg_consumed
  1385. */
  1386. length = rlength;
  1387. goto sg_next_end;
  1388. }
  1389. /*
  1390. * We have a length less than 4 bytes. Check to
  1391. * see if more buffer is available, and combine
  1392. * to make 4 bytes if possible.
  1393. */
  1394. pio->bounce_buf_len = length;
  1395. memset(pio->bounce_buf, 0, 4);
  1396. /*
  1397. * On a read, get 4 bytes from FIFO, and distribute
  1398. * (4-bouce_buf_len) bytes into consecutive
  1399. * sgl buffers when msmsdcc_sg_consumed is called
  1400. */
  1401. if (host->curr.data->flags & MMC_DATA_READ) {
  1402. buffer = pio->bounce_buf;
  1403. length = 4;
  1404. goto sg_next_end;
  1405. } else {
  1406. _msmsdcc_sg_consume_word(host);
  1407. buffer = pio->bounce_buf;
  1408. length = pio->bounce_buf_len;
  1409. }
  1410. }
  1411. sg_next_end:
  1412. *buf = buffer;
  1413. *len = length;
  1414. return 1;
  1415. }
  1416. /*
  1417. * Update sg_miter.consumed based on how many bytes were
  1418. * consumed. If the bounce buffer was used to read from FIFO,
  1419. * redistribute into sgls.
  1420. */
  1421. static void msmsdcc_sg_consumed(struct msmsdcc_host *host,
  1422. unsigned int length)
  1423. {
  1424. struct msmsdcc_pio_data *pio = &host->pio;
  1425. if (host->curr.data->flags & MMC_DATA_READ) {
  1426. if (length > pio->sg_miter.consumed)
  1427. /*
  1428. * consumed 4 bytes, but sgl
  1429. * describes < 4 bytes
  1430. */
  1431. _msmsdcc_sg_consume_word(host);
  1432. else
  1433. pio->sg_miter.consumed = length;
  1434. } else
  1435. if (length < pio->sg_miter.consumed)
  1436. pio->sg_miter.consumed = length;
  1437. }
  1438. static void msmsdcc_sg_start(struct msmsdcc_host *host)
  1439. {
  1440. unsigned int sg_miter_flags = SG_MITER_ATOMIC;
  1441. host->pio.bounce_buf_len = 0;
  1442. if (host->curr.data->flags & MMC_DATA_READ)
  1443. sg_miter_flags |= SG_MITER_TO_SG;
  1444. else
  1445. sg_miter_flags |= SG_MITER_FROM_SG;
  1446. sg_miter_start(&host->pio.sg_miter, host->curr.data->sg,
  1447. host->curr.data->sg_len, sg_miter_flags);
  1448. }
  1449. static void msmsdcc_sg_stop(struct msmsdcc_host *host)
  1450. {
  1451. sg_miter_stop(&host->pio.sg_miter);
  1452. }
  1453. static inline void msmsdcc_clear_pio_irq_mask(struct msmsdcc_host *host)
  1454. {
  1455. writel_relaxed(readl_relaxed(host->base + MMCIMASK0) & ~MCI_IRQ_PIO,
  1456. host->base + MMCIMASK0);
  1457. mb();
  1458. }
  1459. static irqreturn_t
  1460. msmsdcc_pio_irq(int irq, void *dev_id)
  1461. {
  1462. struct msmsdcc_host *host = dev_id;
  1463. void __iomem *base = host->base;
  1464. uint32_t status;
  1465. unsigned long flags;
  1466. unsigned int remain;
  1467. char *buffer;
  1468. spin_lock(&host->lock);
  1469. if (!atomic_read(&host->clks_on) || !host->curr.data) {
  1470. spin_unlock(&host->lock);
  1471. return IRQ_NONE;
  1472. }
  1473. status = readl_relaxed(base + MMCISTATUS);
  1474. if (((readl_relaxed(host->base + MMCIMASK0) & status) &
  1475. (MCI_IRQ_PIO)) == 0) {
  1476. spin_unlock(&host->lock);
  1477. return IRQ_NONE;
  1478. }
  1479. #if IRQ_DEBUG
  1480. msmsdcc_print_status(host, "irq1-r", status);
  1481. #endif
  1482. local_irq_save(flags);
  1483. do {
  1484. unsigned int len;
  1485. if (!(status & (MCI_TXFIFOHALFEMPTY | MCI_TXFIFOEMPTY
  1486. | MCI_RXDATAAVLBL)))
  1487. break;
  1488. if (!msmsdcc_sg_next(host, &buffer, &remain))
  1489. break;
  1490. len = 0;
  1491. if (status & MCI_RXACTIVE)
  1492. len = msmsdcc_pio_read(host, buffer, remain);
  1493. if (status & MCI_TXACTIVE)
  1494. len = msmsdcc_pio_write(host, buffer, remain);
  1495. /* len might have aligned to 32bits above */
  1496. if (len > remain)
  1497. len = remain;
  1498. host->curr.xfer_remain -= len;
  1499. host->curr.data_xfered += len;
  1500. remain -= len;
  1501. msmsdcc_sg_consumed(host, len);
  1502. if (remain) /* Done with this page? */
  1503. break; /* Nope */
  1504. status = readl_relaxed(base + MMCISTATUS);
  1505. } while (1);
  1506. msmsdcc_sg_stop(host);
  1507. local_irq_restore(flags);
  1508. if (!host->curr.xfer_remain) {
  1509. msmsdcc_clear_pio_irq_mask(host);
  1510. goto out_unlock;
  1511. }
  1512. if (status & MCI_RXACTIVE && host->curr.xfer_remain < MCI_FIFOSIZE) {
  1513. writel_relaxed((readl_relaxed(host->base + MMCIMASK0) &
  1514. ~MCI_IRQ_PIO) | MCI_RXDATAAVLBLMASK,
  1515. host->base + MMCIMASK0);
  1516. mb();
  1517. }
  1518. out_unlock:
  1519. spin_unlock(&host->lock);
  1520. return IRQ_HANDLED;
  1521. }
  1522. static void
  1523. msmsdcc_request_start(struct msmsdcc_host *host, struct mmc_request *mrq);
  1524. static void msmsdcc_wait_for_rxdata(struct msmsdcc_host *host,
  1525. struct mmc_data *data)
  1526. {
  1527. u32 loop_cnt = 0;
  1528. /*
  1529. * For read commands with data less than fifo size, it is possible to
  1530. * get DATAEND first and RXDATA_AVAIL might be set later because of
  1531. * synchronization delay through the asynchronous RX FIFO. Thus, for
  1532. * such cases, even after DATAEND interrupt is received software
  1533. * should poll for RXDATA_AVAIL until the requested data is read out
  1534. * of FIFO. This change is needed to get around this abnormal but
  1535. * sometimes expected behavior of SDCC3 controller.
  1536. *
  1537. * We can expect RXDATAAVAIL bit to be set after 6HCLK clock cycles
  1538. * after the data is loaded into RX FIFO. This would amount to less
  1539. * than a microsecond and thus looping for 1000 times is good enough
  1540. * for that delay.
  1541. */
  1542. while (((int)host->curr.xfer_remain > 0) && (++loop_cnt < 1000)) {
  1543. if (readl_relaxed(host->base + MMCISTATUS) & MCI_RXDATAAVLBL) {
  1544. spin_unlock(&host->lock);
  1545. msmsdcc_pio_irq(1, host);
  1546. spin_lock(&host->lock);
  1547. }
  1548. }
  1549. if (loop_cnt == 1000) {
  1550. pr_info("%s: Timed out while polling for Rx Data\n",
  1551. mmc_hostname(host->mmc));
  1552. data->error = -ETIMEDOUT;
  1553. msmsdcc_reset_and_restore(host);
  1554. }
  1555. }
  1556. static void msmsdcc_do_cmdirq(struct msmsdcc_host *host, uint32_t status)
  1557. {
  1558. struct mmc_command *cmd = host->curr.cmd;
  1559. host->curr.cmd = NULL;
  1560. if (mmc_resp_type(cmd))
  1561. cmd->resp[0] = readl_relaxed(host->base + MMCIRESPONSE0);
  1562. /*
  1563. * Read rest of the response registers only if
  1564. * long response is expected for this command
  1565. */
  1566. if (mmc_resp_type(cmd) & MMC_RSP_136) {
  1567. cmd->resp[1] = readl_relaxed(host->base + MMCIRESPONSE1);
  1568. cmd->resp[2] = readl_relaxed(host->base + MMCIRESPONSE2);
  1569. cmd->resp[3] = readl_relaxed(host->base + MMCIRESPONSE3);
  1570. }
  1571. if (status & (MCI_CMDTIMEOUT | MCI_AUTOCMD19TIMEOUT)) {
  1572. pr_debug("%s: CMD%d: Command timeout\n",
  1573. mmc_hostname(host->mmc), cmd->opcode);
  1574. cmd->error = -ETIMEDOUT;
  1575. } else if ((status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) &&
  1576. !host->tuning_in_progress) {
  1577. pr_err("%s: CMD%d: Command CRC error\n",
  1578. mmc_hostname(host->mmc), cmd->opcode);
  1579. msmsdcc_dump_sdcc_state(host);
  1580. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || defined(CONFIG_BCM4339) || defined(CONFIG_BCM4354)
  1581. if( host->pdev->id == 2){
  1582. printk("%s: Skipped tuning.\n",mmc_hostname(host->mmc));
  1583. }
  1584. #else
  1585. /* Execute full tuning in case of CRC errors */
  1586. host->saved_tuning_phase = INVALID_TUNING_PHASE;
  1587. if (host->tuning_needed)
  1588. host->tuning_done = false;
  1589. #endif
  1590. cmd->error = -EILSEQ;
  1591. }
  1592. if (!cmd->error) {
  1593. if (cmd->cmd_timeout_ms > host->curr.req_tout_ms) {
  1594. host->curr.req_tout_ms = cmd->cmd_timeout_ms;
  1595. mod_timer(&host->req_tout_timer, (jiffies +
  1596. msecs_to_jiffies(host->curr.req_tout_ms)));
  1597. }
  1598. }
  1599. if (!cmd->data || cmd->error) {
  1600. if (host->curr.data && host->dma.sg &&
  1601. is_dma_mode(host))
  1602. msm_dmov_flush(host->dma.channel, 0);
  1603. else if (host->curr.data && host->sps.sg &&
  1604. is_sps_mode(host)) {
  1605. /* Stop current SPS transfer */
  1606. msmsdcc_sps_exit_curr_xfer(host);
  1607. }
  1608. else if (host->curr.data) { /* Non DMA */
  1609. msmsdcc_clear_pio_irq_mask(host);
  1610. msmsdcc_reset_and_restore(host);
  1611. msmsdcc_stop_data(host);
  1612. msmsdcc_request_end(host, cmd->mrq);
  1613. } else { /* host->data == NULL */
  1614. if (!cmd->error && host->prog_enable) {
  1615. if (status & MCI_PROGDONE) {
  1616. host->prog_enable = 0;
  1617. msmsdcc_request_end(host, cmd->mrq);
  1618. } else
  1619. host->curr.cmd = cmd;
  1620. } else {
  1621. host->prog_enable = 0;
  1622. host->curr.wait_for_auto_prog_done = false;
  1623. if (host->dummy_52_needed)
  1624. host->dummy_52_needed = 0;
  1625. if (cmd->data && cmd->error)
  1626. msmsdcc_reset_and_restore(host);
  1627. msmsdcc_request_end(host, cmd->mrq);
  1628. }
  1629. }
  1630. } else if (cmd->data) {
  1631. if (cmd == host->curr.mrq->sbc)
  1632. msmsdcc_start_command(host, host->curr.mrq->cmd, 0);
  1633. else if ((cmd->data->flags & MMC_DATA_WRITE) &&
  1634. !host->curr.use_wr_data_pend)
  1635. msmsdcc_start_data(host, cmd->data, NULL, 0);
  1636. }
  1637. }
  1638. static irqreturn_t
  1639. msmsdcc_irq(int irq, void *dev_id)
  1640. {
  1641. struct msmsdcc_host *host = dev_id;
  1642. struct mmc_host *mmc = host->mmc;
  1643. u32 status;
  1644. int ret = 0;
  1645. int timer = 0;
  1646. spin_lock(&host->lock);
  1647. do {
  1648. struct mmc_command *cmd;
  1649. struct mmc_data *data;
  1650. if (timer) {
  1651. timer = 0;
  1652. msmsdcc_delay(host);
  1653. }
  1654. if (!atomic_read(&host->clks_on)) {
  1655. pr_debug("%s: %s: SDIO async irq received\n",
  1656. mmc_hostname(host->mmc), __func__);
  1657. /*
  1658. * Only async interrupt can come when clocks are off,
  1659. * disable further interrupts and enable them when
  1660. * clocks are on.
  1661. */
  1662. if (!host->sdcc_irq_disabled) {
  1663. disable_irq_nosync(irq);
  1664. host->sdcc_irq_disabled = 1;
  1665. }
  1666. /*
  1667. * If mmc_card_wake_sdio_irq() is set, mmc core layer
  1668. * will take care of signaling sdio irq during
  1669. * mmc_sdio_resume().
  1670. */
  1671. if (host->sdcc_suspended &&
  1672. (host->plat->mpm_sdiowakeup_int ||
  1673. host->plat->sdiowakeup_irq)) {
  1674. /*
  1675. * This is a wakeup interrupt so hold wakelock
  1676. * until SDCC resume is handled.
  1677. */
  1678. wake_lock(&host->sdio_wlock);
  1679. } else {
  1680. if (!mmc->card || (mmc->card &&
  1681. !mmc_card_sdio(mmc->card))) {
  1682. pr_warning("%s: SDCC core interrupt received for non-SDIO cards when SDCC clocks are off\n",
  1683. mmc_hostname(mmc));
  1684. ret = 1;
  1685. break;
  1686. }
  1687. spin_unlock(&host->lock);
  1688. mmc_signal_sdio_irq(host->mmc);
  1689. spin_lock(&host->lock);
  1690. }
  1691. ret = 1;
  1692. break;
  1693. }
  1694. status = readl_relaxed(host->base + MMCISTATUS);
  1695. if (((readl_relaxed(host->base + MMCIMASK0) & status) &
  1696. (~(MCI_IRQ_PIO))) == 0)
  1697. break;
  1698. #if IRQ_DEBUG
  1699. msmsdcc_print_status(host, "irq0-r", status);
  1700. #endif
  1701. status &= readl_relaxed(host->base + MMCIMASK0);
  1702. writel_relaxed(status, host->base + MMCICLEAR);
  1703. /* Allow clear to take effect*/
  1704. if (host->clk_rate <=
  1705. msmsdcc_get_min_sup_clk_rate(host))
  1706. msmsdcc_sync_reg_wr(host);
  1707. #if IRQ_DEBUG
  1708. msmsdcc_print_status(host, "irq0-p", status);
  1709. #endif
  1710. if (status & MCI_SDIOINTROPE) {
  1711. if (!mmc->card || (mmc->card &&
  1712. !mmc_card_sdio(mmc->card))) {
  1713. pr_warning("%s: SDIO interrupt (SDIOINTROPE) received for non-SDIO card\n",
  1714. mmc_hostname(mmc));
  1715. ret = 1;
  1716. break;
  1717. }
  1718. if (host->sdcc_suspending)
  1719. wake_lock(&host->sdio_suspend_wlock);
  1720. spin_unlock(&host->lock);
  1721. mmc_signal_sdio_irq(host->mmc);
  1722. spin_lock(&host->lock);
  1723. }
  1724. data = host->curr.data;
  1725. if (host->dummy_52_sent) {
  1726. if (status & (MCI_PROGDONE | MCI_CMDCRCFAIL |
  1727. MCI_CMDTIMEOUT)) {
  1728. if (status & MCI_CMDTIMEOUT)
  1729. pr_debug("%s: dummy CMD52 timeout\n",
  1730. mmc_hostname(host->mmc));
  1731. if (status & MCI_CMDCRCFAIL)
  1732. pr_debug("%s: dummy CMD52 CRC failed\n",
  1733. mmc_hostname(host->mmc));
  1734. host->dummy_52_sent = 0;
  1735. host->dummy_52_needed = 0;
  1736. if (data) {
  1737. msmsdcc_stop_data(host);
  1738. msmsdcc_request_end(host, data->mrq);
  1739. }
  1740. WARN(!data, "No data cmd for dummy CMD52\n");
  1741. spin_unlock(&host->lock);
  1742. return IRQ_HANDLED;
  1743. }
  1744. break;
  1745. }
  1746. /*
  1747. * Check for proper command response
  1748. */
  1749. cmd = host->curr.cmd;
  1750. if ((status & (MCI_CMDSENT | MCI_CMDRESPEND | MCI_CMDCRCFAIL |
  1751. MCI_CMDTIMEOUT | MCI_PROGDONE |
  1752. MCI_AUTOCMD19TIMEOUT)) && host->curr.cmd) {
  1753. msmsdcc_do_cmdirq(host, status);
  1754. }
  1755. if (data) {
  1756. /* Check for data errors */
  1757. if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|
  1758. MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
  1759. msmsdcc_data_err(host, data, status);
  1760. host->curr.data_xfered = 0;
  1761. if (host->dma.sg && is_dma_mode(host))
  1762. msm_dmov_flush(host->dma.channel, 0);
  1763. else if (host->sps.sg && is_sps_mode(host)) {
  1764. /* Stop current SPS transfer */
  1765. msmsdcc_sps_exit_curr_xfer(host);
  1766. } else {
  1767. msmsdcc_clear_pio_irq_mask(host);
  1768. msmsdcc_reset_and_restore(host);
  1769. if (host->curr.data)
  1770. msmsdcc_stop_data(host);
  1771. if (!data->stop || (host->curr.mrq->sbc
  1772. && !data->error))
  1773. timer |=
  1774. msmsdcc_request_end(host,
  1775. data->mrq);
  1776. else if ((host->curr.mrq->sbc
  1777. && data->error) ||
  1778. !host->curr.mrq->sbc) {
  1779. msmsdcc_start_command(host,
  1780. data->stop,
  1781. 0);
  1782. timer = 1;
  1783. }
  1784. }
  1785. }
  1786. /* Check for prog done */
  1787. if (host->curr.wait_for_auto_prog_done &&
  1788. (status & MCI_PROGDONE))
  1789. host->curr.got_auto_prog_done = true;
  1790. /* Check for data done */
  1791. if (!host->curr.got_dataend && (status & MCI_DATAEND))
  1792. host->curr.got_dataend = 1;
  1793. if (host->curr.got_dataend &&
  1794. (!host->curr.wait_for_auto_prog_done ||
  1795. (host->curr.wait_for_auto_prog_done &&
  1796. host->curr.got_auto_prog_done))) {
  1797. /*
  1798. * If DMA is still in progress, we complete
  1799. * via the completion handler
  1800. */
  1801. if (!host->dma.busy && !host->sps.busy) {
  1802. /*
  1803. * There appears to be an issue in the
  1804. * controller where if you request a
  1805. * small block transfer (< fifo size),
  1806. * you may get your DATAEND/DATABLKEND
  1807. * irq without the PIO data irq.
  1808. *
  1809. * Check to see if theres still data
  1810. * to be read, and simulate a PIO irq.
  1811. */
  1812. if (data->flags & MMC_DATA_READ)
  1813. msmsdcc_wait_for_rxdata(host,
  1814. data);
  1815. if (!data->error) {
  1816. host->curr.data_xfered =
  1817. host->curr.xfer_size;
  1818. host->curr.xfer_remain -=
  1819. host->curr.xfer_size;
  1820. }
  1821. if (!host->dummy_52_needed) {
  1822. msmsdcc_stop_data(host);
  1823. if (!data->stop ||
  1824. (host->curr.mrq->sbc
  1825. && !data->error))
  1826. msmsdcc_request_end(
  1827. host,
  1828. data->mrq);
  1829. else if ((host->curr.mrq->sbc
  1830. && data->error) ||
  1831. !host->curr.mrq->sbc) {
  1832. msmsdcc_start_command(
  1833. host,
  1834. data->stop, 0);
  1835. timer = 1;
  1836. }
  1837. } else {
  1838. host->dummy_52_sent = 1;
  1839. msmsdcc_start_command(host,
  1840. &dummy52cmd,
  1841. MCI_CPSM_PROGENA);
  1842. }
  1843. }
  1844. }
  1845. }
  1846. ret = 1;
  1847. } while (status);
  1848. spin_unlock(&host->lock);
  1849. return IRQ_RETVAL(ret);
  1850. }
  1851. static void
  1852. msmsdcc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1853. bool is_first_request)
  1854. {
  1855. struct msmsdcc_host *host = mmc_priv(mmc);
  1856. struct mmc_data *data = mrq->data;
  1857. int rc = 0;
  1858. if (unlikely(!data)) {
  1859. pr_err("%s: %s cannot prepare null data\n", mmc_hostname(mmc),
  1860. __func__);
  1861. return;
  1862. }
  1863. if (unlikely(data->host_cookie)) {
  1864. /* Very wrong */
  1865. data->host_cookie = 0;
  1866. pr_err("%s: %s Request reposted for prepare\n",
  1867. mmc_hostname(mmc), __func__);
  1868. return;
  1869. }
  1870. if (!msmsdcc_is_dma_possible(host, data))
  1871. return;
  1872. rc = msmsdcc_prep_xfer(host, data);
  1873. if (unlikely(rc < 0)) {
  1874. data->host_cookie = 0;
  1875. return;
  1876. }
  1877. data->host_cookie = 1;
  1878. }
  1879. static void
  1880. msmsdcc_post_req(struct mmc_host *mmc, struct mmc_request *mrq, int err)
  1881. {
  1882. struct msmsdcc_host *host = mmc_priv(mmc);
  1883. unsigned int dir;
  1884. struct mmc_data *data = mrq->data;
  1885. if (unlikely(!data)) {
  1886. pr_err("%s: %s cannot cleanup null data\n", mmc_hostname(mmc),
  1887. __func__);
  1888. return;
  1889. }
  1890. if (data->flags & MMC_DATA_READ)
  1891. dir = DMA_FROM_DEVICE;
  1892. else
  1893. dir = DMA_TO_DEVICE;
  1894. if (data->host_cookie)
  1895. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  1896. data->sg_len, dir);
  1897. data->host_cookie = 0;
  1898. }
  1899. static void
  1900. msmsdcc_request_start(struct msmsdcc_host *host, struct mmc_request *mrq)
  1901. {
  1902. if (mrq->data) {
  1903. /* Queue/read data, daisy-chain command when data starts */
  1904. if ((mrq->data->flags & MMC_DATA_READ) ||
  1905. host->curr.use_wr_data_pend)
  1906. msmsdcc_start_data(host, mrq->data,
  1907. mrq->sbc ? mrq->sbc : mrq->cmd,
  1908. 0);
  1909. else
  1910. msmsdcc_start_command(host,
  1911. mrq->sbc ? mrq->sbc : mrq->cmd,
  1912. 0);
  1913. } else {
  1914. msmsdcc_start_command(host, mrq->cmd, 0);
  1915. }
  1916. }
  1917. /*
  1918. * This function returns true if AUTO_PROG_DONE feature of host is
  1919. * applicable for current request, returns "false" otherwise.
  1920. *
  1921. * NOTE: Caller should call this function only for data write operations.
  1922. */
  1923. static bool msmsdcc_is_wait_for_auto_prog_done(struct msmsdcc_host *host,
  1924. struct mmc_request *mrq)
  1925. {
  1926. /*
  1927. * Auto-prog done will be enabled for following cases:
  1928. * mrq->sbc | mrq->stop
  1929. * _____________|________________
  1930. * True | Don't care
  1931. * False | False (CMD24, ACMD25 use case)
  1932. */
  1933. if (is_auto_prog_done(host) && (mrq->sbc || !mrq->stop))
  1934. return true;
  1935. return false;
  1936. }
  1937. /*
  1938. * This function returns true if controller can wait for prog done
  1939. * for current request, returns "false" otherwise.
  1940. *
  1941. * NOTE: Caller should call this function only for data write operations.
  1942. */
  1943. static bool msmsdcc_is_wait_for_prog_done(struct msmsdcc_host *host,
  1944. struct mmc_request *mrq)
  1945. {
  1946. if (msmsdcc_is_wait_for_auto_prog_done(host, mrq) || mrq->stop)
  1947. return true;
  1948. return false;
  1949. }
  1950. static void
  1951. msmsdcc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  1952. {
  1953. struct msmsdcc_host *host = mmc_priv(mmc);
  1954. unsigned long flags;
  1955. unsigned int error = 0;
  1956. int retries = 5;
  1957. /*
  1958. * Get the SDIO AL client out of LPM.
  1959. */
  1960. WARN(host->dummy_52_sent, "Dummy CMD52 in progress\n");
  1961. if (host->plat->is_sdio_al_client)
  1962. msmsdcc_sdio_al_lpm(mmc, false);
  1963. /*
  1964. * Don't start the request if SDCC is not in proper state to handle it
  1965. * BAM state is checked below if applicable
  1966. */
  1967. if (!host->pwr || !atomic_read(&host->clks_on) ||
  1968. host->sdcc_irq_disabled) {
  1969. WARN(1, "%s: %s: SDCC is in bad state. don't process new request (CMD%d)\n",
  1970. mmc_hostname(host->mmc), __func__, mrq->cmd->opcode);
  1971. error = EIO;
  1972. goto bad_state;
  1973. }
  1974. /* check if sps bam needs to be reset */
  1975. if (is_sps_mode(host) && host->sps.reset_bam) {
  1976. while (retries) {
  1977. if (!msmsdcc_bam_dml_reset_and_restore(host))
  1978. break;
  1979. pr_err("%s: msmsdcc_bam_dml_reset_and_restore returned error. %d attempts left.\n",
  1980. mmc_hostname(host->mmc), --retries);
  1981. }
  1982. /* check if BAM reset succeeded or not */
  1983. if (host->sps.reset_bam) {
  1984. pr_err("%s: bam reset failed. Not processing the new request (CMD%d)\n",
  1985. mmc_hostname(host->mmc), mrq->cmd->opcode);
  1986. error = EAGAIN;
  1987. goto bad_state;
  1988. }
  1989. }
  1990. /*
  1991. * Check if DLL retuning is required? If yes, perform it here before
  1992. * starting new request.
  1993. */
  1994. if (host->tuning_needed && !host->tuning_in_progress &&
  1995. !host->tuning_done) {
  1996. pr_debug("%s: %s: execute_tuning for timing mode = %d\n",
  1997. mmc_hostname(mmc), __func__, host->mmc->ios.timing);
  1998. if (host->mmc->ios.timing == MMC_TIMING_UHS_SDR104)
  1999. msmsdcc_execute_tuning(mmc,
  2000. MMC_SEND_TUNING_BLOCK);
  2001. else if (host->mmc->ios.timing == MMC_TIMING_MMC_HS200)
  2002. msmsdcc_execute_tuning(mmc,
  2003. MMC_SEND_TUNING_BLOCK_HS200);
  2004. }
  2005. if (host->eject) {
  2006. error = ENOMEDIUM;
  2007. goto card_ejected;
  2008. }
  2009. WARN(host->curr.mrq, "%s: %s: New request (CMD%d) received while"
  2010. " other request (CMD%d) is in progress\n",
  2011. mmc_hostname(host->mmc), __func__,
  2012. mrq->cmd->opcode, host->curr.mrq->cmd->opcode);
  2013. spin_lock_irqsave(&host->lock, flags);
  2014. /*
  2015. * Set timeout value to 10 secs (or more in case of buggy cards)
  2016. */
  2017. if ((mmc->card) && (mmc->card->quirks & MMC_QUIRK_INAND_DATA_TIMEOUT))
  2018. host->curr.req_tout_ms = 20000;
  2019. else
  2020. host->curr.req_tout_ms = MSM_MMC_REQ_TIMEOUT;
  2021. /*
  2022. * Kick the software request timeout timer here with the timeout
  2023. * value identified above
  2024. */
  2025. mod_timer(&host->req_tout_timer,
  2026. (jiffies +
  2027. msecs_to_jiffies(host->curr.req_tout_ms)));
  2028. host->curr.mrq = mrq;
  2029. if (mrq->sbc) {
  2030. mrq->sbc->mrq = mrq;
  2031. mrq->sbc->data = mrq->data;
  2032. }
  2033. if (mrq->data && (mrq->data->flags & MMC_DATA_WRITE)) {
  2034. if (msmsdcc_is_wait_for_auto_prog_done(host, mrq)) {
  2035. host->curr.wait_for_auto_prog_done = true;
  2036. } else {
  2037. if ((mrq->cmd->opcode == SD_IO_RW_EXTENDED) ||
  2038. (mrq->cmd->opcode == 54))
  2039. host->dummy_52_needed = 1;
  2040. }
  2041. if ((mrq->cmd->opcode == MMC_WRITE_BLOCK) ||
  2042. (mrq->cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK) ||
  2043. ((mrq->cmd->opcode == SD_IO_RW_EXTENDED) &&
  2044. is_data_pend_for_cmd53(host)))
  2045. host->curr.use_wr_data_pend = true;
  2046. }
  2047. msmsdcc_request_start(host, mrq);
  2048. spin_unlock_irqrestore(&host->lock, flags);
  2049. return;
  2050. bad_state:
  2051. msmsdcc_dump_sdcc_state(host);
  2052. card_ejected:
  2053. mrq->cmd->error = -error;
  2054. if (mrq->data) {
  2055. mrq->data->error = -error;
  2056. mrq->data->bytes_xfered = 0;
  2057. }
  2058. mmc_request_done(mmc, mrq);
  2059. }
  2060. static inline int msmsdcc_vreg_set_voltage(struct msm_mmc_reg_data *vreg,
  2061. int min_uV, int max_uV)
  2062. {
  2063. int rc = 0;
  2064. if (vreg->set_voltage_sup) {
  2065. rc = regulator_set_voltage(vreg->reg, min_uV, max_uV);
  2066. if (rc) {
  2067. pr_err("%s: regulator_set_voltage(%s) failed."
  2068. " min_uV=%d, max_uV=%d, rc=%d\n",
  2069. __func__, vreg->name, min_uV, max_uV, rc);
  2070. }
  2071. }
  2072. return rc;
  2073. }
  2074. static inline int msmsdcc_vreg_get_voltage(struct msm_mmc_reg_data *vreg)
  2075. {
  2076. int rc = 0;
  2077. rc = regulator_get_voltage(vreg->reg);
  2078. if (rc < 0)
  2079. pr_err("%s: regulator_get_voltage(%s) failed. rc=%d\n",
  2080. __func__, vreg->name, rc);
  2081. return rc;
  2082. }
  2083. static inline int msmsdcc_vreg_set_optimum_mode(struct msm_mmc_reg_data *vreg,
  2084. int uA_load)
  2085. {
  2086. int rc = 0;
  2087. /* regulators that do not support regulator_set_voltage also
  2088. do not support regulator_set_optimum_mode */
  2089. if (vreg->set_voltage_sup) {
  2090. rc = regulator_set_optimum_mode(vreg->reg, uA_load);
  2091. if (rc < 0)
  2092. pr_err("%s: regulator_set_optimum_mode(reg=%s, "
  2093. "uA_load=%d) failed. rc=%d\n", __func__,
  2094. vreg->name, uA_load, rc);
  2095. else
  2096. /* regulator_set_optimum_mode() can return non zero
  2097. * value even for success case.
  2098. */
  2099. rc = 0;
  2100. }
  2101. return rc;
  2102. }
  2103. static inline int msmsdcc_vreg_init_reg(struct msm_mmc_reg_data *vreg,
  2104. struct device *dev)
  2105. {
  2106. int rc = 0;
  2107. /* check if regulator is already initialized? */
  2108. if (vreg->reg)
  2109. goto out;
  2110. /* Get the regulator handle */
  2111. vreg->reg = regulator_get(dev, vreg->name);
  2112. if (IS_ERR(vreg->reg)) {
  2113. rc = PTR_ERR(vreg->reg);
  2114. pr_err("%s: regulator_get(%s) failed. rc=%d\n",
  2115. __func__, vreg->name, rc);
  2116. goto out;
  2117. }
  2118. if (regulator_count_voltages(vreg->reg) > 0) {
  2119. vreg->set_voltage_sup = 1;
  2120. /* sanity check */
  2121. if (!vreg->high_vol_level || !vreg->hpm_uA) {
  2122. pr_err("%s: %s invalid constraints specified\n",
  2123. __func__, vreg->name);
  2124. rc = -EINVAL;
  2125. }
  2126. }
  2127. out:
  2128. return rc;
  2129. }
  2130. static inline void msmsdcc_vreg_deinit_reg(struct msm_mmc_reg_data *vreg)
  2131. {
  2132. if (vreg->reg)
  2133. regulator_put(vreg->reg);
  2134. }
  2135. /* This init function should be called only once for each SDCC slot */
  2136. static int msmsdcc_vreg_init(struct msmsdcc_host *host, bool is_init)
  2137. {
  2138. int rc = 0;
  2139. struct msm_mmc_slot_reg_data *curr_slot;
  2140. struct msm_mmc_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
  2141. struct device *dev = mmc_dev(host->mmc);
  2142. curr_slot = host->plat->vreg_data;
  2143. if (!curr_slot)
  2144. goto out;
  2145. curr_vdd_reg = curr_slot->vdd_data;
  2146. curr_vdd_io_reg = curr_slot->vdd_io_data;
  2147. if (is_init) {
  2148. /*
  2149. * Get the regulator handle from voltage regulator framework
  2150. * and then try to set the voltage level for the regulator
  2151. */
  2152. if (curr_vdd_reg) {
  2153. rc = msmsdcc_vreg_init_reg(curr_vdd_reg, dev);
  2154. if (rc)
  2155. goto out;
  2156. }
  2157. if (curr_vdd_io_reg) {
  2158. rc = msmsdcc_vreg_init_reg(curr_vdd_io_reg, dev);
  2159. if (rc)
  2160. goto vdd_reg_deinit;
  2161. }
  2162. rc = msmsdcc_vreg_reset(host);
  2163. if (rc)
  2164. pr_err("msmsdcc.%d vreg reset failed (%d)\n",
  2165. host->pdev->id, rc);
  2166. goto out;
  2167. } else {
  2168. /* Deregister all regulators from regulator framework */
  2169. goto vdd_io_reg_deinit;
  2170. }
  2171. vdd_io_reg_deinit:
  2172. if (curr_vdd_io_reg)
  2173. msmsdcc_vreg_deinit_reg(curr_vdd_io_reg);
  2174. vdd_reg_deinit:
  2175. if (curr_vdd_reg)
  2176. msmsdcc_vreg_deinit_reg(curr_vdd_reg);
  2177. out:
  2178. return rc;
  2179. }
  2180. static int msmsdcc_vreg_enable(struct msm_mmc_reg_data *vreg)
  2181. {
  2182. int rc = 0;
  2183. /* Put regulator in HPM (high power mode) */
  2184. rc = msmsdcc_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
  2185. if (rc < 0)
  2186. goto out;
  2187. if (!vreg->is_enabled) {
  2188. /* Set voltage level */
  2189. rc = msmsdcc_vreg_set_voltage(vreg, vreg->high_vol_level,
  2190. vreg->high_vol_level);
  2191. if (rc)
  2192. goto out;
  2193. rc = regulator_enable(vreg->reg);
  2194. if (rc) {
  2195. pr_err("%s: regulator_enable(%s) failed. rc=%d\n",
  2196. __func__, vreg->name, rc);
  2197. goto out;
  2198. }
  2199. vreg->is_enabled = true;
  2200. }
  2201. out:
  2202. return rc;
  2203. }
  2204. static int msmsdcc_vreg_disable(struct msm_mmc_reg_data *vreg, bool is_init)
  2205. {
  2206. int rc = 0;
  2207. /* Never disable regulator marked as always_on */
  2208. if (vreg->is_enabled && !vreg->always_on) {
  2209. rc = regulator_disable(vreg->reg);
  2210. if (rc) {
  2211. pr_err("%s: regulator_disable(%s) failed. rc=%d\n",
  2212. __func__, vreg->name, rc);
  2213. goto out;
  2214. }
  2215. vreg->is_enabled = false;
  2216. rc = msmsdcc_vreg_set_optimum_mode(vreg, 0);
  2217. if (rc < 0)
  2218. goto out;
  2219. /* Set min. voltage level to 0 */
  2220. rc = msmsdcc_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
  2221. if (rc)
  2222. goto out;
  2223. } else if (vreg->is_enabled && vreg->always_on) {
  2224. if (!is_init && vreg->lpm_sup) {
  2225. /* Put always_on regulator in LPM (low power mode) */
  2226. rc = msmsdcc_vreg_set_optimum_mode(vreg, vreg->lpm_uA);
  2227. if (rc < 0)
  2228. goto out;
  2229. } else if (is_init && vreg->reset_at_init) {
  2230. /**
  2231. * The regulator might not actually be disabled if it
  2232. * is shared and in use by other drivers.
  2233. */
  2234. rc = regulator_disable(vreg->reg);
  2235. if (rc) {
  2236. pr_err("%s: regulator_disable(%s) failed at " \
  2237. "bootup. rc=%d\n", __func__,
  2238. vreg->name, rc);
  2239. goto out;
  2240. }
  2241. vreg->is_enabled = false;
  2242. }
  2243. }
  2244. out:
  2245. return rc;
  2246. }
  2247. static int msmsdcc_setup_vreg(struct msmsdcc_host *host, bool enable,
  2248. bool is_init)
  2249. {
  2250. int rc = 0, i;
  2251. struct msm_mmc_slot_reg_data *curr_slot;
  2252. struct msm_mmc_reg_data *vreg_table[2];
  2253. curr_slot = host->plat->vreg_data;
  2254. if (!curr_slot) {
  2255. pr_debug("%s: vreg info unavailable, assuming the slot is powered by always on domain\n",
  2256. mmc_hostname(host->mmc));
  2257. goto out;
  2258. }
  2259. vreg_table[0] = curr_slot->vdd_data;
  2260. vreg_table[1] = curr_slot->vdd_io_data;
  2261. for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
  2262. if (vreg_table[i]) {
  2263. if (enable)
  2264. rc = msmsdcc_vreg_enable(vreg_table[i]);
  2265. else
  2266. rc = msmsdcc_vreg_disable(vreg_table[i],
  2267. is_init);
  2268. if (rc)
  2269. goto out;
  2270. }
  2271. }
  2272. out:
  2273. return rc;
  2274. }
  2275. /*
  2276. * Reset vreg by ensuring it is off during probe. A call
  2277. * to enable vreg is needed to balance disable vreg
  2278. */
  2279. static int msmsdcc_vreg_reset(struct msmsdcc_host *host)
  2280. {
  2281. int rc;
  2282. rc = msmsdcc_setup_vreg(host, 1, true);
  2283. if (rc)
  2284. return rc;
  2285. rc = msmsdcc_setup_vreg(host, 0, true);
  2286. return rc;
  2287. }
  2288. enum vdd_io_level {
  2289. /* set vdd_io_data->low_vol_level */
  2290. VDD_IO_LOW,
  2291. /* set vdd_io_data->high_vol_level */
  2292. VDD_IO_HIGH,
  2293. /*
  2294. * set whatever there in voltage_level (third argument) of
  2295. * msmsdcc_set_vdd_io_vol() function.
  2296. */
  2297. VDD_IO_SET_LEVEL,
  2298. };
  2299. /*
  2300. * This function returns the current VDD IO voltage level.
  2301. * Returns negative value if it fails to read the voltage level
  2302. * Returns 0 if regulator was disabled or if VDD_IO (and VDD)
  2303. * regulator were not defined for host.
  2304. */
  2305. static int msmsdcc_get_vdd_io_vol(struct msmsdcc_host *host)
  2306. {
  2307. int rc = 0;
  2308. if (host->plat->vreg_data) {
  2309. struct msm_mmc_reg_data *io_reg =
  2310. host->plat->vreg_data->vdd_io_data;
  2311. /*
  2312. * If vdd_io is not defined, then we can consider that
  2313. * IO voltage is same as VDD.
  2314. */
  2315. if (!io_reg)
  2316. io_reg = host->plat->vreg_data->vdd_data;
  2317. if (io_reg && io_reg->is_enabled)
  2318. rc = msmsdcc_vreg_get_voltage(io_reg);
  2319. }
  2320. return rc;
  2321. }
  2322. /*
  2323. * This function updates the IO pad power switch bit in MCI_CLK register
  2324. * based on currrent IO pad voltage level.
  2325. * NOTE: This function assumes that host lock was not taken by caller.
  2326. */
  2327. static void msmsdcc_update_io_pad_pwr_switch(struct msmsdcc_host *host)
  2328. {
  2329. int rc = 0;
  2330. unsigned long flags;
  2331. if (!is_io_pad_pwr_switch(host))
  2332. return;
  2333. rc = msmsdcc_get_vdd_io_vol(host);
  2334. spin_lock_irqsave(&host->lock, flags);
  2335. /*
  2336. * Dual voltage pad is the SDCC's (chipset) functionality and not all
  2337. * the SDCC instances support the dual voltage pads.
  2338. * For dual-voltage pad (1.8v/3.3v), SW should set IO_PAD_PWR_SWITCH
  2339. * bit before using the pads in 1.8V mode.
  2340. * For regular, not dual-voltage pads (including eMMC 1.2v/1.8v pads),
  2341. * IO_PAD_PWR_SWITCH bit is a don't care.
  2342. * But we don't have an option to know (by reading some SDCC register)
  2343. * that a particular SDCC instance supports dual voltage pads or not,
  2344. * so we simply set the IO_PAD_PWR_SWITCH bit for low voltage IO
  2345. * (1.8v/1.2v). For regular (not dual-voltage pads), this bit value
  2346. * is anyway ignored.
  2347. */
  2348. if (rc > 0 && rc < 2700000)
  2349. host->io_pad_pwr_switch = 1;
  2350. else
  2351. host->io_pad_pwr_switch = 0;
  2352. if (atomic_read(&host->clks_on)) {
  2353. if (host->io_pad_pwr_switch)
  2354. writel_relaxed((readl_relaxed(host->base + MMCICLOCK) |
  2355. IO_PAD_PWR_SWITCH),
  2356. host->base + MMCICLOCK);
  2357. else
  2358. writel_relaxed((readl_relaxed(host->base + MMCICLOCK) &
  2359. ~IO_PAD_PWR_SWITCH),
  2360. host->base + MMCICLOCK);
  2361. msmsdcc_sync_reg_wr(host);
  2362. }
  2363. spin_unlock_irqrestore(&host->lock, flags);
  2364. }
  2365. static int msmsdcc_set_vdd_io_vol(struct msmsdcc_host *host,
  2366. enum vdd_io_level level,
  2367. unsigned int voltage_level)
  2368. {
  2369. int rc = 0;
  2370. int set_level;
  2371. if (host->plat->vreg_data) {
  2372. struct msm_mmc_reg_data *vdd_io_reg =
  2373. host->plat->vreg_data->vdd_io_data;
  2374. if (vdd_io_reg && vdd_io_reg->is_enabled) {
  2375. switch (level) {
  2376. case VDD_IO_LOW:
  2377. set_level = vdd_io_reg->low_vol_level;
  2378. break;
  2379. case VDD_IO_HIGH:
  2380. set_level = vdd_io_reg->high_vol_level;
  2381. break;
  2382. case VDD_IO_SET_LEVEL:
  2383. set_level = voltage_level;
  2384. break;
  2385. default:
  2386. pr_err("%s: %s: invalid argument level = %d",
  2387. mmc_hostname(host->mmc), __func__,
  2388. level);
  2389. rc = -EINVAL;
  2390. goto out;
  2391. }
  2392. rc = msmsdcc_vreg_set_voltage(vdd_io_reg,
  2393. set_level, set_level);
  2394. }
  2395. }
  2396. out:
  2397. return rc;
  2398. }
  2399. static inline int msmsdcc_is_pwrsave(struct msmsdcc_host *host)
  2400. {
  2401. if (host->clk_rate > 400000 && msmsdcc_pwrsave)
  2402. return 1;
  2403. return 0;
  2404. }
  2405. /*
  2406. * Any function calling msmsdcc_setup_clocks must
  2407. * acquire clk_mutex. May sleep.
  2408. */
  2409. static int msmsdcc_setup_clocks(struct msmsdcc_host *host, bool enable)
  2410. {
  2411. int rc = 0;
  2412. if (enable && !atomic_read(&host->clks_on)) {
  2413. msmsdcc_msm_bus_cancel_work_and_set_vote(host, &host->mmc->ios);
  2414. if (!IS_ERR_OR_NULL(host->bus_clk)) {
  2415. rc = clk_prepare_enable(host->bus_clk);
  2416. if (rc) {
  2417. pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
  2418. mmc_hostname(host->mmc), __func__, rc);
  2419. goto remove_vote;
  2420. }
  2421. }
  2422. if (!IS_ERR(host->pclk)) {
  2423. rc = clk_prepare_enable(host->pclk);
  2424. if (rc) {
  2425. pr_err("%s: %s: failed to enable the pclk with error %d\n",
  2426. mmc_hostname(host->mmc), __func__, rc);
  2427. goto disable_bus;
  2428. }
  2429. }
  2430. rc = clk_prepare_enable(host->clk);
  2431. if (rc) {
  2432. pr_err("%s: %s: failed to enable the host-clk with error %d\n",
  2433. mmc_hostname(host->mmc), __func__, rc);
  2434. goto disable_pclk;
  2435. }
  2436. mb();
  2437. msmsdcc_delay(host);
  2438. atomic_set(&host->clks_on, 1);
  2439. } else if (!enable && atomic_read(&host->clks_on)) {
  2440. mb();
  2441. msmsdcc_delay(host);
  2442. clk_disable_unprepare(host->clk);
  2443. if (!IS_ERR(host->pclk))
  2444. clk_disable_unprepare(host->pclk);
  2445. if (!IS_ERR_OR_NULL(host->bus_clk))
  2446. clk_disable_unprepare(host->bus_clk);
  2447. /*
  2448. * If clock gating is enabled, then remove the vote
  2449. * immediately because clocks will be disabled only
  2450. * after MSM_MMC_CLK_GATE_DELAY and thus no additional
  2451. * delay is required to remove the bus vote.
  2452. */
  2453. if (host->mmc->clkgate_delay)
  2454. msmsdcc_msm_bus_cancel_work_and_set_vote(host, NULL);
  2455. else
  2456. msmsdcc_msm_bus_queue_work(host);
  2457. atomic_set(&host->clks_on, 0);
  2458. }
  2459. goto out;
  2460. disable_pclk:
  2461. if (!IS_ERR_OR_NULL(host->pclk))
  2462. clk_disable_unprepare(host->pclk);
  2463. disable_bus:
  2464. if (!IS_ERR_OR_NULL(host->bus_clk))
  2465. clk_disable_unprepare(host->bus_clk);
  2466. remove_vote:
  2467. msmsdcc_msm_bus_cancel_work_and_set_vote(host, NULL);
  2468. out:
  2469. return rc;
  2470. }
  2471. static inline unsigned int msmsdcc_get_sup_clk_rate(struct msmsdcc_host *host,
  2472. unsigned int req_clk)
  2473. {
  2474. unsigned int sel_clk = -1;
  2475. if (req_clk < msmsdcc_get_min_sup_clk_rate(host)) {
  2476. sel_clk = msmsdcc_get_min_sup_clk_rate(host);
  2477. goto out;
  2478. }
  2479. if (host->plat->sup_clk_table && host->plat->sup_clk_cnt) {
  2480. unsigned char cnt;
  2481. for (cnt = 0; cnt < host->plat->sup_clk_cnt; cnt++) {
  2482. if (host->plat->sup_clk_table[cnt] > req_clk)
  2483. break;
  2484. else if (host->plat->sup_clk_table[cnt] == req_clk) {
  2485. sel_clk = host->plat->sup_clk_table[cnt];
  2486. break;
  2487. } else
  2488. sel_clk = host->plat->sup_clk_table[cnt];
  2489. }
  2490. } else {
  2491. if ((req_clk < host->plat->msmsdcc_fmax) &&
  2492. (req_clk > host->plat->msmsdcc_fmid))
  2493. sel_clk = host->plat->msmsdcc_fmid;
  2494. else
  2495. sel_clk = req_clk;
  2496. }
  2497. out:
  2498. return sel_clk;
  2499. }
  2500. static inline unsigned int msmsdcc_get_min_sup_clk_rate(
  2501. struct msmsdcc_host *host)
  2502. {
  2503. if (host->plat->sup_clk_table && host->plat->sup_clk_cnt)
  2504. return host->plat->sup_clk_table[0];
  2505. else
  2506. return host->plat->msmsdcc_fmin;
  2507. }
  2508. static inline unsigned int msmsdcc_get_max_sup_clk_rate(
  2509. struct msmsdcc_host *host)
  2510. {
  2511. if (host->plat->sup_clk_table && host->plat->sup_clk_cnt)
  2512. return host->plat->sup_clk_table[host->plat->sup_clk_cnt - 1];
  2513. else
  2514. return host->plat->msmsdcc_fmax;
  2515. }
  2516. static int msmsdcc_setup_gpio(struct msmsdcc_host *host, bool enable)
  2517. {
  2518. struct msm_mmc_gpio_data *curr;
  2519. int i, rc = 0;
  2520. curr = host->plat->pin_data->gpio_data;
  2521. for (i = 0; i < curr->size; i++) {
  2522. if (!gpio_is_valid(curr->gpio[i].no)) {
  2523. rc = -EINVAL;
  2524. pr_err("%s: Invalid gpio = %d\n",
  2525. mmc_hostname(host->mmc), curr->gpio[i].no);
  2526. goto free_gpios;
  2527. }
  2528. if (enable) {
  2529. if (curr->gpio[i].is_always_on &&
  2530. curr->gpio[i].is_enabled)
  2531. continue;
  2532. rc = gpio_request(curr->gpio[i].no,
  2533. curr->gpio[i].name);
  2534. if (rc) {
  2535. pr_err("%s: gpio_request(%d, %s) failed %d\n",
  2536. mmc_hostname(host->mmc),
  2537. curr->gpio[i].no,
  2538. curr->gpio[i].name, rc);
  2539. goto free_gpios;
  2540. }
  2541. curr->gpio[i].is_enabled = true;
  2542. } else {
  2543. if (curr->gpio[i].is_always_on)
  2544. continue;
  2545. gpio_free(curr->gpio[i].no);
  2546. curr->gpio[i].is_enabled = false;
  2547. }
  2548. }
  2549. goto out;
  2550. free_gpios:
  2551. for (i--; i >= 0; i--) {
  2552. gpio_free(curr->gpio[i].no);
  2553. curr->gpio[i].is_enabled = false;
  2554. }
  2555. out:
  2556. return rc;
  2557. }
  2558. static int msmsdcc_setup_pad(struct msmsdcc_host *host, bool enable)
  2559. {
  2560. struct msm_mmc_pad_data *curr;
  2561. int i;
  2562. curr = host->plat->pin_data->pad_data;
  2563. for (i = 0; i < curr->drv->size; i++) {
  2564. if (enable)
  2565. msm_tlmm_set_hdrive(curr->drv->on[i].no,
  2566. curr->drv->on[i].val);
  2567. else
  2568. msm_tlmm_set_hdrive(curr->drv->off[i].no,
  2569. curr->drv->off[i].val);
  2570. }
  2571. for (i = 0; i < curr->pull->size; i++) {
  2572. if (enable)
  2573. msm_tlmm_set_pull(curr->pull->on[i].no,
  2574. curr->pull->on[i].val);
  2575. else
  2576. msm_tlmm_set_pull(curr->pull->off[i].no,
  2577. curr->pull->off[i].val);
  2578. }
  2579. return 0;
  2580. }
  2581. static u32 msmsdcc_setup_pins(struct msmsdcc_host *host, bool enable)
  2582. {
  2583. int rc = 0;
  2584. if (!host->plat->pin_data || host->plat->pin_data->cfg_sts == enable)
  2585. return 0;
  2586. if (host->plat->pin_data->is_gpio)
  2587. rc = msmsdcc_setup_gpio(host, enable);
  2588. else
  2589. rc = msmsdcc_setup_pad(host, enable);
  2590. if (!rc)
  2591. host->plat->pin_data->cfg_sts = enable;
  2592. return rc;
  2593. }
  2594. static int msmsdcc_cfg_mpm_sdiowakeup(struct msmsdcc_host *host,
  2595. unsigned mode)
  2596. {
  2597. int ret = 0;
  2598. unsigned int pin = host->plat->mpm_sdiowakeup_int;
  2599. if (!pin)
  2600. return 0;
  2601. switch (mode) {
  2602. case SDC_DAT1_DISABLE:
  2603. ret = msm_mpm_enable_pin(pin, 0);
  2604. break;
  2605. case SDC_DAT1_ENABLE:
  2606. ret = msm_mpm_set_pin_type(pin, IRQ_TYPE_LEVEL_LOW);
  2607. ret = msm_mpm_enable_pin(pin, 1);
  2608. break;
  2609. case SDC_DAT1_ENWAKE:
  2610. ret = msm_mpm_set_pin_wake(pin, 1);
  2611. break;
  2612. case SDC_DAT1_DISWAKE:
  2613. ret = msm_mpm_set_pin_wake(pin, 0);
  2614. break;
  2615. default:
  2616. ret = -EINVAL;
  2617. break;
  2618. }
  2619. return ret;
  2620. }
  2621. static u32 msmsdcc_setup_pwr(struct msmsdcc_host *host, struct mmc_ios *ios)
  2622. {
  2623. u32 pwr = 0;
  2624. int ret = 0;
  2625. struct mmc_host *mmc = host->mmc;
  2626. if (host->plat->translate_vdd && !host->sdio_gpio_lpm)
  2627. ret = host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
  2628. else if (!host->plat->translate_vdd && !host->sdio_gpio_lpm)
  2629. ret = msmsdcc_setup_vreg(host, !!ios->vdd, false);
  2630. if (ret) {
  2631. pr_err("%s: Failed to setup voltage regulators\n",
  2632. mmc_hostname(host->mmc));
  2633. goto out;
  2634. }
  2635. switch (ios->power_mode) {
  2636. case MMC_POWER_OFF:
  2637. pwr = MCI_PWR_OFF;
  2638. msmsdcc_cfg_mpm_sdiowakeup(host, SDC_DAT1_DISABLE);
  2639. /*
  2640. * If VDD IO rail is always on, set low voltage for VDD
  2641. * IO rail when slot is not in use (like when card is not
  2642. * present or during system suspend).
  2643. */
  2644. msmsdcc_set_vdd_io_vol(host, VDD_IO_LOW, 0);
  2645. msmsdcc_update_io_pad_pwr_switch(host);
  2646. msmsdcc_setup_pins(host, false);
  2647. /*
  2648. * Reset the mask to prevent hitting any pending interrupts
  2649. * after powering up the card again.
  2650. */
  2651. if (atomic_read(&host->clks_on)) {
  2652. writel_relaxed(0, host->base + MMCIMASK0);
  2653. mb();
  2654. }
  2655. break;
  2656. case MMC_POWER_UP:
  2657. /* writing PWR_UP bit is redundant */
  2658. pwr = MCI_PWR_UP;
  2659. msmsdcc_cfg_mpm_sdiowakeup(host, SDC_DAT1_ENABLE);
  2660. msmsdcc_set_vdd_io_vol(host, VDD_IO_HIGH, 0);
  2661. msmsdcc_update_io_pad_pwr_switch(host);
  2662. msmsdcc_setup_pins(host, true);
  2663. break;
  2664. case MMC_POWER_ON:
  2665. pwr = MCI_PWR_ON;
  2666. break;
  2667. }
  2668. out:
  2669. return pwr;
  2670. }
  2671. static void msmsdcc_enable_irq_wake(struct msmsdcc_host *host)
  2672. {
  2673. unsigned int wakeup_irq;
  2674. wakeup_irq = (host->plat->sdiowakeup_irq) ?
  2675. host->plat->sdiowakeup_irq :
  2676. host->core_irqres->start;
  2677. if (!host->irq_wake_enabled) {
  2678. enable_irq_wake(wakeup_irq);
  2679. host->irq_wake_enabled = true;
  2680. }
  2681. }
  2682. static void msmsdcc_disable_irq_wake(struct msmsdcc_host *host)
  2683. {
  2684. unsigned int wakeup_irq;
  2685. wakeup_irq = (host->plat->sdiowakeup_irq) ?
  2686. host->plat->sdiowakeup_irq :
  2687. host->core_irqres->start;
  2688. if (host->irq_wake_enabled) {
  2689. disable_irq_wake(wakeup_irq);
  2690. host->irq_wake_enabled = false;
  2691. }
  2692. }
  2693. /* Returns required bandwidth in Bytes per Sec */
  2694. static unsigned int msmsdcc_get_bw_required(struct msmsdcc_host *host,
  2695. struct mmc_ios *ios)
  2696. {
  2697. unsigned int bw;
  2698. bw = host->clk_rate;
  2699. /*
  2700. * For DDR mode, SDCC controller clock will be at
  2701. * the double rate than the actual clock that goes to card.
  2702. */
  2703. if (ios->bus_width == MMC_BUS_WIDTH_4)
  2704. bw /= 2;
  2705. else if (ios->bus_width == MMC_BUS_WIDTH_1)
  2706. bw /= 8;
  2707. return bw;
  2708. }
  2709. static int msmsdcc_msm_bus_get_vote_for_bw(struct msmsdcc_host *host,
  2710. unsigned int bw)
  2711. {
  2712. unsigned int *table = host->plat->msm_bus_voting_data->bw_vecs;
  2713. unsigned int size = host->plat->msm_bus_voting_data->bw_vecs_size;
  2714. int i;
  2715. if (host->msm_bus_vote.is_max_bw_needed && bw)
  2716. return host->msm_bus_vote.max_bw_vote;
  2717. for (i = 0; i < size; i++) {
  2718. if (bw <= table[i])
  2719. break;
  2720. }
  2721. if (i && (i == size))
  2722. i--;
  2723. return i;
  2724. }
  2725. static int msmsdcc_msm_bus_register(struct msmsdcc_host *host)
  2726. {
  2727. int rc = 0;
  2728. struct msm_bus_scale_pdata *use_cases;
  2729. if (host->pdev->dev.of_node) {
  2730. struct msm_mmc_bus_voting_data *data;
  2731. struct device *dev = &host->pdev->dev;
  2732. data = devm_kzalloc(dev,
  2733. sizeof(struct msm_mmc_bus_voting_data), GFP_KERNEL);
  2734. if (!data) {
  2735. dev_err(&host->pdev->dev,
  2736. "%s: failed to allocate memory\n", __func__);
  2737. rc = -ENOMEM;
  2738. goto out;
  2739. }
  2740. rc = msmsdcc_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
  2741. &data->bw_vecs, &data->bw_vecs_size, 0);
  2742. if (!rc) {
  2743. data->use_cases = msm_bus_cl_get_pdata(host->pdev);
  2744. host->plat->msm_bus_voting_data = data;
  2745. }
  2746. }
  2747. if (host->plat->msm_bus_voting_data &&
  2748. host->plat->msm_bus_voting_data->use_cases &&
  2749. host->plat->msm_bus_voting_data->bw_vecs &&
  2750. host->plat->msm_bus_voting_data->bw_vecs_size) {
  2751. use_cases = host->plat->msm_bus_voting_data->use_cases;
  2752. host->msm_bus_vote.client_handle =
  2753. msm_bus_scale_register_client(use_cases);
  2754. } else {
  2755. return 0;
  2756. }
  2757. if (!host->msm_bus_vote.client_handle) {
  2758. pr_err("%s: msm_bus_scale_register_client() failed\n",
  2759. mmc_hostname(host->mmc));
  2760. rc = -EFAULT;
  2761. } else {
  2762. /* cache the vote index for minimum and maximum bandwidth */
  2763. host->msm_bus_vote.min_bw_vote =
  2764. msmsdcc_msm_bus_get_vote_for_bw(host, 0);
  2765. host->msm_bus_vote.max_bw_vote =
  2766. msmsdcc_msm_bus_get_vote_for_bw(host, UINT_MAX);
  2767. }
  2768. out:
  2769. return rc;
  2770. }
  2771. static void msmsdcc_msm_bus_unregister(struct msmsdcc_host *host)
  2772. {
  2773. if (host->msm_bus_vote.client_handle)
  2774. msm_bus_scale_unregister_client(
  2775. host->msm_bus_vote.client_handle);
  2776. }
  2777. /*
  2778. * This function must be called with host lock acquired.
  2779. * Caller of this function should also ensure that msm bus client
  2780. * handle is not null.
  2781. */
  2782. static inline int msmsdcc_msm_bus_set_vote(struct msmsdcc_host *host,
  2783. int vote,
  2784. unsigned long flags)
  2785. {
  2786. int rc = 0;
  2787. if (vote != host->msm_bus_vote.curr_vote) {
  2788. spin_unlock_irqrestore(&host->lock, flags);
  2789. rc = msm_bus_scale_client_update_request(
  2790. host->msm_bus_vote.client_handle, vote);
  2791. if (rc)
  2792. pr_err("%s: msm_bus_scale_client_update_request() failed."
  2793. " bus_client_handle=0x%x, vote=%d, err=%d\n",
  2794. mmc_hostname(host->mmc),
  2795. host->msm_bus_vote.client_handle, vote, rc);
  2796. spin_lock_irqsave(&host->lock, flags);
  2797. if (!rc)
  2798. host->msm_bus_vote.curr_vote = vote;
  2799. }
  2800. return rc;
  2801. }
  2802. /*
  2803. * Internal work. Work to set 0 bandwidth for msm bus.
  2804. */
  2805. static void msmsdcc_msm_bus_work(struct work_struct *work)
  2806. {
  2807. struct msmsdcc_host *host = container_of(work,
  2808. struct msmsdcc_host,
  2809. msm_bus_vote.vote_work.work);
  2810. unsigned long flags;
  2811. if (!host->msm_bus_vote.client_handle)
  2812. return;
  2813. spin_lock_irqsave(&host->lock, flags);
  2814. /* don't vote for 0 bandwidth if any request is in progress */
  2815. if (!host->curr.mrq)
  2816. msmsdcc_msm_bus_set_vote(host,
  2817. host->msm_bus_vote.min_bw_vote, flags);
  2818. else
  2819. pr_warning("%s: %s: SDCC transfer in progress. skipping"
  2820. " bus voting to 0 bandwidth\n",
  2821. mmc_hostname(host->mmc), __func__);
  2822. spin_unlock_irqrestore(&host->lock, flags);
  2823. }
  2824. /*
  2825. * This function cancels any scheduled delayed work
  2826. * and sets the bus vote based on ios argument.
  2827. * If "ios" argument is NULL, bandwidth required is 0 else
  2828. * calculate the bandwidth based on ios parameters.
  2829. */
  2830. static void msmsdcc_msm_bus_cancel_work_and_set_vote(
  2831. struct msmsdcc_host *host,
  2832. struct mmc_ios *ios)
  2833. {
  2834. unsigned long flags;
  2835. unsigned int bw;
  2836. int vote;
  2837. if (!host->msm_bus_vote.client_handle)
  2838. return;
  2839. bw = ios ? msmsdcc_get_bw_required(host, ios) : 0;
  2840. cancel_delayed_work_sync(&host->msm_bus_vote.vote_work);
  2841. spin_lock_irqsave(&host->lock, flags);
  2842. vote = msmsdcc_msm_bus_get_vote_for_bw(host, bw);
  2843. msmsdcc_msm_bus_set_vote(host, vote, flags);
  2844. spin_unlock_irqrestore(&host->lock, flags);
  2845. }
  2846. /* This function queues a work which will set the bandwidth requiement to 0 */
  2847. static void msmsdcc_msm_bus_queue_work(struct msmsdcc_host *host)
  2848. {
  2849. unsigned long flags;
  2850. if (!host->msm_bus_vote.client_handle)
  2851. return;
  2852. spin_lock_irqsave(&host->lock, flags);
  2853. if (host->msm_bus_vote.min_bw_vote != host->msm_bus_vote.curr_vote)
  2854. queue_delayed_work(system_nrt_wq,
  2855. &host->msm_bus_vote.vote_work,
  2856. msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
  2857. spin_unlock_irqrestore(&host->lock, flags);
  2858. }
  2859. static void
  2860. msmsdcc_cfg_sdio_wakeup(struct msmsdcc_host *host, bool enable_wakeup_irq)
  2861. {
  2862. struct mmc_host *mmc = host->mmc;
  2863. /*
  2864. * SDIO_AL clients has different mechanism of handling LPM through
  2865. * sdio_al driver itself. The sdio wakeup interrupt is configured as
  2866. * part of that. Here, we are interested only in clients like WLAN.
  2867. */
  2868. if (!(mmc->card && mmc_card_sdio(mmc->card))
  2869. || host->plat->is_sdio_al_client)
  2870. goto out;
  2871. if (!host->sdcc_suspended) {
  2872. /*
  2873. * When MSM is not in power collapse and we
  2874. * are disabling clocks, enable bit 22 in MASK0
  2875. * to handle asynchronous SDIO interrupts.
  2876. */
  2877. if (enable_wakeup_irq) {
  2878. writel_relaxed(MCI_SDIOINTMASK, host->base + MMCIMASK0);
  2879. mb();
  2880. } else {
  2881. writel_relaxed(MCI_SDIOINTMASK, host->base + MMCICLEAR);
  2882. msmsdcc_sync_reg_wr(host);
  2883. }
  2884. goto out;
  2885. } else if (!mmc_card_wake_sdio_irq(mmc)) {
  2886. /*
  2887. * Wakeup MSM only if SDIO function drivers set
  2888. * MMC_PM_WAKE_SDIO_IRQ flag in their suspend call.
  2889. */
  2890. goto out;
  2891. }
  2892. if (enable_wakeup_irq) {
  2893. if (!host->plat->sdiowakeup_irq) {
  2894. /*
  2895. * When there is no gpio line that can be configured
  2896. * as wakeup interrupt handle it by configuring
  2897. * asynchronous sdio interrupts and DAT1 line.
  2898. */
  2899. writel_relaxed(MCI_SDIOINTMASK,
  2900. host->base + MMCIMASK0);
  2901. mb();
  2902. msmsdcc_cfg_mpm_sdiowakeup(host, SDC_DAT1_ENWAKE);
  2903. /* configure sdcc core interrupt as wakeup interrupt */
  2904. msmsdcc_enable_irq_wake(host);
  2905. } else {
  2906. /* Let gpio line handle wakeup interrupt */
  2907. writel_relaxed(0, host->base + MMCIMASK0);
  2908. mb();
  2909. if (host->sdio_wakeupirq_disabled) {
  2910. host->sdio_wakeupirq_disabled = 0;
  2911. /* configure gpio line as wakeup interrupt */
  2912. msmsdcc_enable_irq_wake(host);
  2913. enable_irq(host->plat->sdiowakeup_irq);
  2914. }
  2915. }
  2916. } else {
  2917. if (!host->plat->sdiowakeup_irq) {
  2918. /*
  2919. * We may not have cleared bit 22 in the interrupt
  2920. * handler as the clocks might be off at that time.
  2921. */
  2922. writel_relaxed(MCI_SDIOINTMASK, host->base + MMCICLEAR);
  2923. msmsdcc_sync_reg_wr(host);
  2924. msmsdcc_cfg_mpm_sdiowakeup(host, SDC_DAT1_DISWAKE);
  2925. msmsdcc_disable_irq_wake(host);
  2926. } else if (!host->sdio_wakeupirq_disabled) {
  2927. disable_irq_nosync(host->plat->sdiowakeup_irq);
  2928. msmsdcc_disable_irq_wake(host);
  2929. host->sdio_wakeupirq_disabled = 1;
  2930. }
  2931. }
  2932. out:
  2933. return;
  2934. }
  2935. static void
  2936. msmsdcc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  2937. {
  2938. struct msmsdcc_host *host = mmc_priv(mmc);
  2939. u32 clk = 0, pwr = 0;
  2940. int rc;
  2941. unsigned long flags;
  2942. unsigned int clock;
  2943. /*
  2944. * Disable SDCC core interrupt until set_ios is completed.
  2945. * This avoids any race conditions with interrupt raised
  2946. * when turning on/off the clocks. One possible
  2947. * scenario is SDIO operational interrupt while the clock
  2948. * is turned off.
  2949. * host->lock is being released intermittently below.
  2950. * Thus, prevent concurrent access to host.
  2951. */
  2952. mutex_lock(&host->clk_mutex);
  2953. DBG(host, "ios->clock = %u\n", ios->clock);
  2954. spin_lock_irqsave(&host->lock, flags);
  2955. if (!host->sdcc_irq_disabled) {
  2956. disable_irq_nosync(host->core_irqres->start);
  2957. host->sdcc_irq_disabled = 1;
  2958. }
  2959. spin_unlock_irqrestore(&host->lock, flags);
  2960. /* Make sure sdcc core irq is synchronized */
  2961. synchronize_irq(host->core_irqres->start);
  2962. pwr = msmsdcc_setup_pwr(host, ios);
  2963. spin_lock_irqsave(&host->lock, flags);
  2964. if (ios->clock) {
  2965. spin_unlock_irqrestore(&host->lock, flags);
  2966. rc = msmsdcc_setup_clocks(host, true);
  2967. if (rc)
  2968. goto out;
  2969. spin_lock_irqsave(&host->lock, flags);
  2970. writel_relaxed(host->mci_irqenable, host->base + MMCIMASK0);
  2971. mb();
  2972. msmsdcc_cfg_sdio_wakeup(host, false);
  2973. clock = msmsdcc_get_sup_clk_rate(host, ios->clock);
  2974. /*
  2975. * For DDR50 mode, controller needs clock rate to be
  2976. * double than what is required on the SD card CLK pin.
  2977. *
  2978. * Setting DDR timing mode in controller before setting the
  2979. * clock rate will make sure that card don't see the double
  2980. * clock rate even for very small duration. Some eMMC
  2981. * cards seems to lock up if they see clock frequency > 52MHz.
  2982. */
  2983. if (ios->timing == MMC_TIMING_UHS_DDR50) {
  2984. u32 clk;
  2985. clk = readl_relaxed(host->base + MMCICLOCK);
  2986. clk &= ~(0x7 << 14); /* clear SELECT_IN field */
  2987. clk |= (3 << 14); /* set DDR timing mode */
  2988. writel_relaxed(clk, host->base + MMCICLOCK);
  2989. msmsdcc_sync_reg_wr(host);
  2990. clock = msmsdcc_get_sup_clk_rate(host, ios->clock * 2);
  2991. }
  2992. if (clock != host->clk_rate) {
  2993. spin_unlock_irqrestore(&host->lock, flags);
  2994. rc = clk_set_rate(host->clk, clock);
  2995. spin_lock_irqsave(&host->lock, flags);
  2996. if (rc < 0)
  2997. pr_err("%s: failed to set clk rate %u\n",
  2998. mmc_hostname(mmc), clock);
  2999. host->clk_rate = clock;
  3000. host->reg_write_delay =
  3001. (1 + ((3 * USEC_PER_SEC) /
  3002. (host->clk_rate ? host->clk_rate :
  3003. msmsdcc_get_min_sup_clk_rate(host))));
  3004. spin_unlock_irqrestore(&host->lock, flags);
  3005. /*
  3006. * Update bus vote incase of frequency change due to
  3007. * clock scaling.
  3008. */
  3009. msmsdcc_msm_bus_cancel_work_and_set_vote(host,
  3010. &mmc->ios);
  3011. spin_lock_irqsave(&host->lock, flags);
  3012. }
  3013. /*
  3014. * give atleast 2 MCLK cycles delay for clocks
  3015. * and SDCC core to stabilize
  3016. */
  3017. mb();
  3018. msmsdcc_delay(host);
  3019. clk |= MCI_CLK_ENABLE;
  3020. }
  3021. if (ios->bus_width == MMC_BUS_WIDTH_8)
  3022. clk |= MCI_CLK_WIDEBUS_8;
  3023. else if (ios->bus_width == MMC_BUS_WIDTH_4)
  3024. clk |= MCI_CLK_WIDEBUS_4;
  3025. else
  3026. clk |= MCI_CLK_WIDEBUS_1;
  3027. if (msmsdcc_is_pwrsave(host) && mmc_host_may_gate_card(host->mmc->card))
  3028. clk |= MCI_CLK_PWRSAVE;
  3029. clk |= MCI_CLK_FLOWENA;
  3030. host->tuning_needed = 0;
  3031. /*
  3032. * Select the controller timing mode according
  3033. * to current bus speed mode
  3034. */
  3035. if (host->clk_rate > (100 * 1000 * 1000) &&
  3036. (ios->timing == MMC_TIMING_UHS_SDR104 ||
  3037. ios->timing == MMC_TIMING_MMC_HS200)) {
  3038. /* Card clock frequency must be > 100MHz to enable tuning */
  3039. clk |= (4 << 14);
  3040. host->tuning_needed = 1;
  3041. } else {
  3042. if (ios->timing == MMC_TIMING_UHS_DDR50)
  3043. clk |= (3 << 14);
  3044. else
  3045. clk |= (2 << 14); /* feedback clock */
  3046. host->tuning_done = false;
  3047. if (atomic_read(&host->clks_on)) {
  3048. /* Write 1 to DLL_RST bit of MCI_DLL_CONFIG register */
  3049. writel_relaxed((readl_relaxed(host->base +
  3050. MCI_DLL_CONFIG) | MCI_DLL_RST),
  3051. host->base + MCI_DLL_CONFIG);
  3052. /* Write 1 to DLL_PDN bit of MCI_DLL_CONFIG register */
  3053. writel_relaxed((readl_relaxed(host->base +
  3054. MCI_DLL_CONFIG) | MCI_DLL_PDN),
  3055. host->base + MCI_DLL_CONFIG);
  3056. }
  3057. }
  3058. /* Select free running MCLK as input clock of cm_dll_sdc4 */
  3059. clk |= (2 << 23);
  3060. if (host->io_pad_pwr_switch)
  3061. clk |= IO_PAD_PWR_SWITCH;
  3062. /* Don't write into registers if clocks are disabled */
  3063. if (atomic_read(&host->clks_on)) {
  3064. if (readl_relaxed(host->base + MMCICLOCK) != clk) {
  3065. writel_relaxed(clk, host->base + MMCICLOCK);
  3066. msmsdcc_sync_reg_wr(host);
  3067. }
  3068. if (readl_relaxed(host->base + MMCIPOWER) != pwr) {
  3069. host->pwr = pwr;
  3070. writel_relaxed(pwr, host->base + MMCIPOWER);
  3071. msmsdcc_sync_reg_wr(host);
  3072. }
  3073. }
  3074. if (!(clk & MCI_CLK_ENABLE) && atomic_read(&host->clks_on)) {
  3075. msmsdcc_cfg_sdio_wakeup(host, true);
  3076. spin_unlock_irqrestore(&host->lock, flags);
  3077. /*
  3078. * May get a wake-up interrupt the instant we disable the
  3079. * clocks. This would disable the wake-up interrupt.
  3080. */
  3081. msmsdcc_setup_clocks(host, false);
  3082. spin_lock_irqsave(&host->lock, flags);
  3083. }
  3084. if (host->tuning_in_progress)
  3085. WARN(!atomic_read(&host->clks_on),
  3086. "tuning_in_progress but SDCC clocks are OFF\n");
  3087. /* Let interrupts be disabled if the host is powered off */
  3088. if (ios->power_mode != MMC_POWER_OFF && host->sdcc_irq_disabled) {
  3089. enable_irq(host->core_irqres->start);
  3090. host->sdcc_irq_disabled = 0;
  3091. }
  3092. spin_unlock_irqrestore(&host->lock, flags);
  3093. out:
  3094. mutex_unlock(&host->clk_mutex);
  3095. }
  3096. int msmsdcc_set_pwrsave(struct mmc_host *mmc, int pwrsave)
  3097. {
  3098. struct msmsdcc_host *host = mmc_priv(mmc);
  3099. u32 clk;
  3100. clk = readl_relaxed(host->base + MMCICLOCK);
  3101. pr_debug("Changing to pwr_save=%d", pwrsave);
  3102. if (pwrsave && msmsdcc_is_pwrsave(host))
  3103. clk |= MCI_CLK_PWRSAVE;
  3104. else
  3105. clk &= ~MCI_CLK_PWRSAVE;
  3106. writel_relaxed(clk, host->base + MMCICLOCK);
  3107. msmsdcc_sync_reg_wr(host);
  3108. return 0;
  3109. }
  3110. static int msmsdcc_get_ro(struct mmc_host *mmc)
  3111. {
  3112. int status = -ENOSYS;
  3113. struct msmsdcc_host *host = mmc_priv(mmc);
  3114. if (host->plat->wpswitch) {
  3115. status = host->plat->wpswitch(mmc_dev(mmc));
  3116. } else if (gpio_is_valid(host->plat->wpswitch_gpio)) {
  3117. status = gpio_request(host->plat->wpswitch_gpio,
  3118. "SD_WP_Switch");
  3119. if (status) {
  3120. pr_err("%s: %s: Failed to request GPIO %d\n",
  3121. mmc_hostname(mmc), __func__,
  3122. host->plat->wpswitch_gpio);
  3123. } else {
  3124. status = gpio_direction_input(
  3125. host->plat->wpswitch_gpio);
  3126. if (!status) {
  3127. /*
  3128. * Wait for atleast 300ms as debounce
  3129. * time for GPIO input to stabilize.
  3130. */
  3131. msleep(300);
  3132. status = gpio_get_value_cansleep(
  3133. host->plat->wpswitch_gpio);
  3134. status ^= !host->plat->is_wpswitch_active_low;
  3135. }
  3136. gpio_free(host->plat->wpswitch_gpio);
  3137. }
  3138. }
  3139. if (status < 0)
  3140. status = -ENOSYS;
  3141. pr_debug("%s: Card read-only status %d\n", __func__, status);
  3142. return status;
  3143. }
  3144. static void msmsdcc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  3145. {
  3146. struct msmsdcc_host *host = mmc_priv(mmc);
  3147. unsigned long flags;
  3148. /*
  3149. * We may come here with clocks turned off in that case don't
  3150. * attempt to write into MASK0 register. While turning on the
  3151. * clocks mci_irqenable will be written to MASK0 register.
  3152. */
  3153. spin_lock_irqsave(&host->lock, flags);
  3154. if (enable) {
  3155. host->mci_irqenable |= MCI_SDIOINTOPERMASK;
  3156. if (atomic_read(&host->clks_on)) {
  3157. writel_relaxed(readl_relaxed(host->base + MMCIMASK0) |
  3158. MCI_SDIOINTOPERMASK, host->base + MMCIMASK0);
  3159. mb();
  3160. }
  3161. } else {
  3162. host->mci_irqenable &= ~MCI_SDIOINTOPERMASK;
  3163. if (atomic_read(&host->clks_on)) {
  3164. writel_relaxed(readl_relaxed(host->base + MMCIMASK0) &
  3165. ~MCI_SDIOINTOPERMASK, host->base + MMCIMASK0);
  3166. mb();
  3167. }
  3168. }
  3169. spin_unlock_irqrestore(&host->lock, flags);
  3170. }
  3171. #ifdef CONFIG_PM_RUNTIME
  3172. static void msmsdcc_print_rpm_info(struct msmsdcc_host *host)
  3173. {
  3174. struct device *dev = mmc_dev(host->mmc);
  3175. pr_err("%s: PM: sdcc_suspended=%d, pending_resume=%d, sdcc_suspending=%d\n",
  3176. mmc_hostname(host->mmc), host->sdcc_suspended,
  3177. host->pending_resume, host->sdcc_suspending);
  3178. pr_err("%s: RPM: runtime_status=%d, usage_count=%d,"
  3179. " is_suspended=%d, disable_depth=%d, runtime_error=%d,"
  3180. " request_pending=%d, request=%d\n",
  3181. mmc_hostname(host->mmc), dev->power.runtime_status,
  3182. atomic_read(&dev->power.usage_count),
  3183. dev->power.is_suspended, dev->power.disable_depth,
  3184. dev->power.runtime_error, dev->power.request_pending,
  3185. dev->power.request);
  3186. }
  3187. static int msmsdcc_enable(struct mmc_host *mmc)
  3188. {
  3189. int rc = 0;
  3190. struct device *dev = mmc->parent;
  3191. struct msmsdcc_host *host = mmc_priv(mmc);
  3192. msmsdcc_pm_qos_update_latency(host, 1);
  3193. if (mmc->card && mmc_card_sdio(mmc->card))
  3194. goto out;
  3195. if (host->sdcc_suspended && host->pending_resume) {
  3196. host->pending_resume = false;
  3197. pm_runtime_get_noresume(dev);
  3198. rc = msmsdcc_runtime_resume(dev);
  3199. goto skip_get_sync;
  3200. }
  3201. if (dev->power.runtime_status == RPM_SUSPENDING) {
  3202. if (mmc->suspend_task == current) {
  3203. pm_runtime_get_noresume(dev);
  3204. goto out;
  3205. }
  3206. } else if (dev->power.runtime_status == RPM_RESUMING) {
  3207. pm_runtime_get_noresume(dev);
  3208. goto out;
  3209. }
  3210. rc = pm_runtime_get_sync(dev);
  3211. skip_get_sync:
  3212. if (rc < 0) {
  3213. WARN(1, "%s: %s: failed with error %d\n", mmc_hostname(mmc),
  3214. __func__, rc);
  3215. msmsdcc_print_rpm_info(host);
  3216. return rc;
  3217. }
  3218. out:
  3219. return 0;
  3220. }
  3221. static int msmsdcc_disable(struct mmc_host *mmc)
  3222. {
  3223. int rc;
  3224. struct msmsdcc_host *host = mmc_priv(mmc);
  3225. msmsdcc_pm_qos_update_latency(host, 0);
  3226. if (mmc->card && mmc_card_sdio(mmc->card)) {
  3227. rc = 0;
  3228. goto out;
  3229. }
  3230. if (host->plat->disable_runtime_pm)
  3231. return -ENOTSUPP;
  3232. rc = pm_runtime_put_sync(mmc->parent);
  3233. if (rc < 0) {
  3234. WARN(1, "%s: %s: failed with error %d\n", mmc_hostname(mmc),
  3235. __func__, rc);
  3236. msmsdcc_print_rpm_info(host);
  3237. return rc;
  3238. }
  3239. out:
  3240. return rc;
  3241. }
  3242. #else
  3243. static void msmsdcc_print_rpm_info(struct msmsdcc_host *host) {}
  3244. static int msmsdcc_enable(struct mmc_host *mmc)
  3245. {
  3246. struct device *dev = mmc->parent;
  3247. struct msmsdcc_host *host = mmc_priv(mmc);
  3248. int rc = 0;
  3249. msmsdcc_pm_qos_update_latency(host, 1);
  3250. if (mmc->card && mmc_card_sdio(mmc->card)) {
  3251. rc = 0;
  3252. goto out;
  3253. }
  3254. if (host->sdcc_suspended && host->pending_resume) {
  3255. host->pending_resume = false;
  3256. rc = msmsdcc_runtime_resume(dev);
  3257. goto out;
  3258. }
  3259. mutex_lock(&host->clk_mutex);
  3260. rc = msmsdcc_setup_clocks(host, true);
  3261. mutex_unlock(&host->clk_mutex);
  3262. out:
  3263. if (rc < 0) {
  3264. pr_info("%s: %s: failed with error %d", mmc_hostname(mmc),
  3265. __func__, rc);
  3266. msmsdcc_pm_qos_update_latency(host, 0);
  3267. return rc;
  3268. }
  3269. return 0;
  3270. }
  3271. static int msmsdcc_disable(struct mmc_host *mmc)
  3272. {
  3273. struct msmsdcc_host *host = mmc_priv(mmc);
  3274. int rc = 0;
  3275. msmsdcc_pm_qos_update_latency(host, 0);
  3276. if (mmc->card && mmc_card_sdio(mmc->card))
  3277. goto out;
  3278. mutex_lock(&host->clk_mutex);
  3279. rc = msmsdcc_setup_clocks(host, false);
  3280. mutex_unlock(&host->clk_mutex);
  3281. if (rc) {
  3282. msmsdcc_pm_qos_update_latency(host, 1);
  3283. return rc;
  3284. }
  3285. out:
  3286. return rc;
  3287. }
  3288. #endif
  3289. static int msmsdcc_switch_io_voltage(struct mmc_host *mmc,
  3290. struct mmc_ios *ios)
  3291. {
  3292. struct msmsdcc_host *host = mmc_priv(mmc);
  3293. unsigned long flags;
  3294. bool prev_pwrsave, curr_pwrsave;
  3295. int rc = 0;
  3296. switch (ios->signal_voltage) {
  3297. case MMC_SIGNAL_VOLTAGE_330:
  3298. /* Set VDD IO to high voltage range (2.7v - 3.6v) */
  3299. rc = msmsdcc_set_vdd_io_vol(host, VDD_IO_HIGH, 0);
  3300. if (!rc)
  3301. msmsdcc_update_io_pad_pwr_switch(host);
  3302. goto out;
  3303. case MMC_SIGNAL_VOLTAGE_180:
  3304. break;
  3305. case MMC_SIGNAL_VOLTAGE_120:
  3306. /*
  3307. * For eMMC cards, VDD_IO voltage range must be changed
  3308. * only if it operates in HS200 SDR 1.2V mode or in
  3309. * DDR 1.2V mode.
  3310. */
  3311. rc = msmsdcc_set_vdd_io_vol(host, VDD_IO_SET_LEVEL, 1200000);
  3312. if (!rc)
  3313. msmsdcc_update_io_pad_pwr_switch(host);
  3314. goto out;
  3315. default:
  3316. /* invalid selection. don't do anything */
  3317. rc = -EINVAL;
  3318. goto out;
  3319. }
  3320. /*
  3321. * If we are here means voltage switch from high voltage to
  3322. * low voltage is required
  3323. */
  3324. spin_lock_irqsave(&host->lock, flags);
  3325. prev_pwrsave = !!(readl_relaxed(host->base + MMCICLOCK) &
  3326. MCI_CLK_PWRSAVE);
  3327. curr_pwrsave = prev_pwrsave;
  3328. /*
  3329. * Poll on MCIDATIN_3_0 and MCICMDIN bits of MCI_TEST_INPUT
  3330. * register until they become all zeros.
  3331. */
  3332. if (readl_relaxed(host->base + MCI_TEST_INPUT) & (0xF << 1)) {
  3333. rc = -EAGAIN;
  3334. pr_err("%s: %s: MCIDATIN_3_0 is still not all zeros",
  3335. mmc_hostname(mmc), __func__);
  3336. goto out_unlock;
  3337. }
  3338. /* Stop SD CLK output. */
  3339. if (!prev_pwrsave) {
  3340. writel_relaxed((readl_relaxed(host->base + MMCICLOCK) |
  3341. MCI_CLK_PWRSAVE), host->base + MMCICLOCK);
  3342. msmsdcc_sync_reg_wr(host);
  3343. curr_pwrsave = true;
  3344. }
  3345. spin_unlock_irqrestore(&host->lock, flags);
  3346. /*
  3347. * Switch VDD Io from high voltage range (2.7v - 3.6v) to
  3348. * low voltage range (1.7v - 1.95v).
  3349. */
  3350. rc = msmsdcc_set_vdd_io_vol(host, VDD_IO_LOW, 0);
  3351. if (rc)
  3352. goto out;
  3353. msmsdcc_update_io_pad_pwr_switch(host);
  3354. /* Wait 5 ms for the voltage regulater in the card to become stable. */
  3355. usleep_range(5000, 5500);
  3356. spin_lock_irqsave(&host->lock, flags);
  3357. /* Disable PWRSAVE would make sure that SD CLK is always running */
  3358. writel_relaxed((readl_relaxed(host->base + MMCICLOCK)
  3359. & ~MCI_CLK_PWRSAVE), host->base + MMCICLOCK);
  3360. msmsdcc_sync_reg_wr(host);
  3361. curr_pwrsave = false;
  3362. spin_unlock_irqrestore(&host->lock, flags);
  3363. /*
  3364. * If MCIDATIN_3_0 and MCICMDIN bits of MCI_TEST_INPUT register
  3365. * don't become all ones within 1 ms then a Voltage Switch
  3366. * sequence has failed and a power cycle to the card is required.
  3367. * Otherwise Voltage Switch sequence is completed successfully.
  3368. */
  3369. usleep_range(1000, 1500);
  3370. spin_lock_irqsave(&host->lock, flags);
  3371. if ((readl_relaxed(host->base + MCI_TEST_INPUT) & (0xF << 1))
  3372. != (0xF << 1)) {
  3373. pr_err("%s: %s: MCIDATIN_3_0 are still not all ones",
  3374. mmc_hostname(mmc), __func__);
  3375. rc = -EAGAIN;
  3376. goto out_unlock;
  3377. }
  3378. out_unlock:
  3379. /* Restore the correct PWRSAVE state */
  3380. if (prev_pwrsave ^ curr_pwrsave)
  3381. msmsdcc_set_pwrsave(mmc, prev_pwrsave);
  3382. spin_unlock_irqrestore(&host->lock, flags);
  3383. out:
  3384. return rc;
  3385. }
  3386. static inline void msmsdcc_cm_sdc4_dll_set_freq(struct msmsdcc_host *host)
  3387. {
  3388. u32 mclk_freq = 0;
  3389. /* Program the MCLK value to MCLK_FREQ bit field */
  3390. if (host->clk_rate <= 112000000)
  3391. mclk_freq = 0;
  3392. else if (host->clk_rate <= 125000000)
  3393. mclk_freq = 1;
  3394. else if (host->clk_rate <= 137000000)
  3395. mclk_freq = 2;
  3396. else if (host->clk_rate <= 150000000)
  3397. mclk_freq = 3;
  3398. else if (host->clk_rate <= 162000000)
  3399. mclk_freq = 4;
  3400. else if (host->clk_rate <= 175000000)
  3401. mclk_freq = 5;
  3402. else if (host->clk_rate <= 187000000)
  3403. mclk_freq = 6;
  3404. else if (host->clk_rate <= 200000000)
  3405. mclk_freq = 7;
  3406. writel_relaxed(((readl_relaxed(host->base + MCI_DLL_CONFIG)
  3407. & ~(7 << 24)) | (mclk_freq << 24)),
  3408. host->base + MCI_DLL_CONFIG);
  3409. }
  3410. /* Initialize the DLL (Programmable Delay Line ) */
  3411. static int msmsdcc_init_cm_sdc4_dll(struct msmsdcc_host *host)
  3412. {
  3413. int rc = 0;
  3414. unsigned long flags;
  3415. u32 wait_cnt;
  3416. bool prev_pwrsave, curr_pwrsave;
  3417. spin_lock_irqsave(&host->lock, flags);
  3418. prev_pwrsave = !!(readl_relaxed(host->base + MMCICLOCK) &
  3419. MCI_CLK_PWRSAVE);
  3420. curr_pwrsave = prev_pwrsave;
  3421. /*
  3422. * Make sure that clock is always enabled when DLL
  3423. * tuning is in progress. Keeping PWRSAVE ON may
  3424. * turn off the clock. So let's disable the PWRSAVE
  3425. * here and re-enable it once tuning is completed.
  3426. */
  3427. if (prev_pwrsave) {
  3428. writel_relaxed((readl_relaxed(host->base + MMCICLOCK)
  3429. & ~MCI_CLK_PWRSAVE), host->base + MMCICLOCK);
  3430. msmsdcc_sync_reg_wr(host);
  3431. curr_pwrsave = false;
  3432. }
  3433. /* Write 1 to DLL_RST bit of MCI_DLL_CONFIG register */
  3434. writel_relaxed((readl_relaxed(host->base + MCI_DLL_CONFIG)
  3435. | MCI_DLL_RST), host->base + MCI_DLL_CONFIG);
  3436. /* Write 1 to DLL_PDN bit of MCI_DLL_CONFIG register */
  3437. writel_relaxed((readl_relaxed(host->base + MCI_DLL_CONFIG)
  3438. | MCI_DLL_PDN), host->base + MCI_DLL_CONFIG);
  3439. msmsdcc_cm_sdc4_dll_set_freq(host);
  3440. /* Write 0 to DLL_RST bit of MCI_DLL_CONFIG register */
  3441. writel_relaxed((readl_relaxed(host->base + MCI_DLL_CONFIG)
  3442. & ~MCI_DLL_RST), host->base + MCI_DLL_CONFIG);
  3443. /* Write 0 to DLL_PDN bit of MCI_DLL_CONFIG register */
  3444. writel_relaxed((readl_relaxed(host->base + MCI_DLL_CONFIG)
  3445. & ~MCI_DLL_PDN), host->base + MCI_DLL_CONFIG);
  3446. /* Set DLL_EN bit to 1. */
  3447. writel_relaxed((readl_relaxed(host->base + MCI_DLL_CONFIG)
  3448. | MCI_DLL_EN), host->base + MCI_DLL_CONFIG);
  3449. /* Set CK_OUT_EN bit to 1. */
  3450. writel_relaxed((readl_relaxed(host->base + MCI_DLL_CONFIG)
  3451. | MCI_CK_OUT_EN), host->base + MCI_DLL_CONFIG);
  3452. wait_cnt = 50;
  3453. /* Wait until DLL_LOCK bit of MCI_DLL_STATUS register becomes '1' */
  3454. while (!(readl_relaxed(host->base + MCI_DLL_STATUS) & MCI_DLL_LOCK)) {
  3455. /* max. wait for 50us sec for LOCK bit to be set */
  3456. if (--wait_cnt == 0) {
  3457. pr_err("%s: %s: DLL failed to LOCK\n",
  3458. mmc_hostname(host->mmc), __func__);
  3459. rc = -ETIMEDOUT;
  3460. goto out;
  3461. }
  3462. /* wait for 1us before polling again */
  3463. udelay(1);
  3464. }
  3465. out:
  3466. /* Restore the correct PWRSAVE state */
  3467. if (prev_pwrsave ^ curr_pwrsave)
  3468. msmsdcc_set_pwrsave(host->mmc, prev_pwrsave);
  3469. spin_unlock_irqrestore(&host->lock, flags);
  3470. return rc;
  3471. }
  3472. static inline int msmsdcc_dll_poll_ck_out_en(struct msmsdcc_host *host,
  3473. u8 poll)
  3474. {
  3475. int rc = 0;
  3476. u32 wait_cnt = 50;
  3477. u8 ck_out_en = 0;
  3478. /* poll for MCI_CK_OUT_EN bit. max. poll time = 50us */
  3479. ck_out_en = !!(readl_relaxed(host->base + MCI_DLL_CONFIG) &
  3480. MCI_CK_OUT_EN);
  3481. while (ck_out_en != poll) {
  3482. if (--wait_cnt == 0) {
  3483. pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
  3484. mmc_hostname(host->mmc), __func__, poll);
  3485. rc = -ETIMEDOUT;
  3486. goto out;
  3487. }
  3488. udelay(1);
  3489. ck_out_en = !!(readl_relaxed(host->base + MCI_DLL_CONFIG) &
  3490. MCI_CK_OUT_EN);
  3491. }
  3492. out:
  3493. return rc;
  3494. }
  3495. /*
  3496. * Enable a CDR circuit in CM_SDC4_DLL block to enable automatic
  3497. * calibration sequence. This function should be called before
  3498. * enabling AUTO_CMD19 bit in MCI_CMD register for block read
  3499. * commands (CMD17/CMD18).
  3500. *
  3501. * This function gets called when host spinlock acquired.
  3502. */
  3503. static int msmsdcc_enable_cdr_cm_sdc4_dll(struct msmsdcc_host *host)
  3504. {
  3505. int rc = 0;
  3506. u32 config;
  3507. config = readl_relaxed(host->base + MCI_DLL_CONFIG);
  3508. config |= MCI_CDR_EN;
  3509. config &= ~(MCI_CDR_EXT_EN | MCI_CK_OUT_EN);
  3510. writel_relaxed(config, host->base + MCI_DLL_CONFIG);
  3511. /* Wait until CK_OUT_EN bit of MCI_DLL_CONFIG register becomes '0' */
  3512. rc = msmsdcc_dll_poll_ck_out_en(host, 0);
  3513. if (rc)
  3514. goto err_out;
  3515. /* Set CK_OUT_EN bit of MCI_DLL_CONFIG register to 1. */
  3516. writel_relaxed((readl_relaxed(host->base + MCI_DLL_CONFIG)
  3517. | MCI_CK_OUT_EN), host->base + MCI_DLL_CONFIG);
  3518. /* Wait until CK_OUT_EN bit of MCI_DLL_CONFIG register becomes '1' */
  3519. rc = msmsdcc_dll_poll_ck_out_en(host, 1);
  3520. if (rc)
  3521. goto err_out;
  3522. goto out;
  3523. err_out:
  3524. pr_err("%s: %s: Failed\n", mmc_hostname(host->mmc), __func__);
  3525. out:
  3526. return rc;
  3527. }
  3528. static int msmsdcc_config_cm_sdc4_dll_phase(struct msmsdcc_host *host,
  3529. u8 phase)
  3530. {
  3531. int rc = 0;
  3532. u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
  3533. 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
  3534. 0x8};
  3535. unsigned long flags;
  3536. u32 config;
  3537. spin_lock_irqsave(&host->lock, flags);
  3538. config = readl_relaxed(host->base + MCI_DLL_CONFIG);
  3539. config &= ~(MCI_CDR_EN | MCI_CK_OUT_EN);
  3540. config |= (MCI_CDR_EXT_EN | MCI_DLL_EN);
  3541. writel_relaxed(config, host->base + MCI_DLL_CONFIG);
  3542. /* Wait until CK_OUT_EN bit of MCI_DLL_CONFIG register becomes '0' */
  3543. rc = msmsdcc_dll_poll_ck_out_en(host, 0);
  3544. if (rc)
  3545. goto err_out;
  3546. /*
  3547. * Write the selected DLL clock output phase (0 ... 15)
  3548. * to CDR_SELEXT bit field of MCI_DLL_CONFIG register.
  3549. */
  3550. writel_relaxed(((readl_relaxed(host->base + MCI_DLL_CONFIG)
  3551. & ~(0xF << 20))
  3552. | (grey_coded_phase_table[phase] << 20)),
  3553. host->base + MCI_DLL_CONFIG);
  3554. /* Set CK_OUT_EN bit of MCI_DLL_CONFIG register to 1. */
  3555. writel_relaxed((readl_relaxed(host->base + MCI_DLL_CONFIG)
  3556. | MCI_CK_OUT_EN), host->base + MCI_DLL_CONFIG);
  3557. /* Wait until CK_OUT_EN bit of MCI_DLL_CONFIG register becomes '1' */
  3558. rc = msmsdcc_dll_poll_ck_out_en(host, 1);
  3559. if (rc)
  3560. goto err_out;
  3561. config = readl_relaxed(host->base + MCI_DLL_CONFIG);
  3562. config |= MCI_CDR_EN;
  3563. config &= ~MCI_CDR_EXT_EN;
  3564. writel_relaxed(config, host->base + MCI_DLL_CONFIG);
  3565. goto out;
  3566. err_out:
  3567. pr_err("%s: %s: Failed to set DLL phase: %d\n",
  3568. mmc_hostname(host->mmc), __func__, phase);
  3569. out:
  3570. spin_unlock_irqrestore(&host->lock, flags);
  3571. return rc;
  3572. }
  3573. /*
  3574. * Find out the greatest range of consecuitive selected
  3575. * DLL clock output phases that can be used as sampling
  3576. * setting for SD3.0 UHS-I card read operation (in SDR104
  3577. * timing mode) or for eMMC4.5 card read operation (in HS200
  3578. * timing mode).
  3579. * Select the 3/4 of the range and configure the DLL with the
  3580. * selected DLL clock output phase.
  3581. */
  3582. static int find_most_appropriate_phase(struct msmsdcc_host *host,
  3583. u8 *phase_table, u8 total_phases)
  3584. {
  3585. #define MAX_PHASES 16
  3586. int ret;
  3587. u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
  3588. u8 phases_per_row[MAX_PHASES] = {0};
  3589. int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
  3590. int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
  3591. bool phase_0_found = false, phase_15_found = false;
  3592. if (!total_phases || (total_phases > MAX_PHASES)) {
  3593. pr_err("%s: %s: invalid argument: total_phases=%d\n",
  3594. mmc_hostname(host->mmc), __func__, total_phases);
  3595. return -EINVAL;
  3596. }
  3597. for (cnt = 0; cnt < total_phases; cnt++) {
  3598. ranges[row_index][col_index] = phase_table[cnt];
  3599. phases_per_row[row_index] += 1;
  3600. col_index++;
  3601. if ((cnt + 1) == total_phases) {
  3602. continue;
  3603. /* check if next phase in phase_table is consecutive or not */
  3604. } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
  3605. row_index++;
  3606. col_index = 0;
  3607. }
  3608. }
  3609. if (row_index >= MAX_PHASES)
  3610. return -EINVAL;
  3611. /* Check if phase-0 is present in first valid window? */
  3612. if (!ranges[0][0]) {
  3613. phase_0_found = true;
  3614. phase_0_raw_index = 0;
  3615. /* Check if cycle exist between 2 valid windows */
  3616. for (cnt = 1; cnt <= row_index; cnt++) {
  3617. if (phases_per_row[cnt]) {
  3618. for (i = 0; i < phases_per_row[cnt]; i++) {
  3619. if (ranges[cnt][i] == 15) {
  3620. phase_15_found = true;
  3621. phase_15_raw_index = cnt;
  3622. break;
  3623. }
  3624. }
  3625. }
  3626. }
  3627. }
  3628. /* If 2 valid windows form cycle then merge them as single window */
  3629. if (phase_0_found && phase_15_found) {
  3630. /* number of phases in raw where phase 0 is present */
  3631. u8 phases_0 = phases_per_row[phase_0_raw_index];
  3632. /* number of phases in raw where phase 15 is present */
  3633. u8 phases_15 = phases_per_row[phase_15_raw_index];
  3634. if (phases_0 + phases_15 >= MAX_PHASES)
  3635. /*
  3636. * If there are more than 1 phase windows then total
  3637. * number of phases in both the windows should not be
  3638. * more than or equal to MAX_PHASES.
  3639. */
  3640. return -EINVAL;
  3641. /* Merge 2 cyclic windows */
  3642. i = phases_15;
  3643. for (cnt = 0; cnt < phases_0; cnt++) {
  3644. ranges[phase_15_raw_index][i] =
  3645. ranges[phase_0_raw_index][cnt];
  3646. if (++i >= MAX_PHASES)
  3647. break;
  3648. }
  3649. phases_per_row[phase_0_raw_index] = 0;
  3650. phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
  3651. }
  3652. for (cnt = 0; cnt <= row_index; cnt++) {
  3653. if (phases_per_row[cnt] > curr_max) {
  3654. curr_max = phases_per_row[cnt];
  3655. selected_row_index = cnt;
  3656. }
  3657. }
  3658. i = ((curr_max * 3) / 4);
  3659. if (i)
  3660. i--;
  3661. ret = (int)ranges[selected_row_index][i];
  3662. if (ret >= MAX_PHASES) {
  3663. ret = -EINVAL;
  3664. pr_err("%s: %s: invalid phase selected=%d\n",
  3665. mmc_hostname(host->mmc), __func__, ret);
  3666. }
  3667. return ret;
  3668. }
  3669. static int msmsdcc_execute_tuning(struct mmc_host *mmc, u32 opcode)
  3670. {
  3671. int rc = 0;
  3672. struct msmsdcc_host *host = mmc_priv(mmc);
  3673. unsigned long flags;
  3674. u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
  3675. const u32 *tuning_block_pattern = tuning_block_64;
  3676. int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
  3677. bool is_tuning_all_phases;
  3678. pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
  3679. /* Tuning is only required for SDR104 modes */
  3680. if (!host->tuning_needed) {
  3681. rc = 0;
  3682. goto exit;
  3683. }
  3684. spin_lock_irqsave(&host->lock, flags);
  3685. WARN(!host->pwr, "SDCC power is turned off\n");
  3686. WARN(!atomic_read(&host->clks_on), "SDCC clocks are turned off\n");
  3687. WARN(host->sdcc_irq_disabled, "SDCC IRQ is disabled\n");
  3688. host->tuning_in_progress = 1;
  3689. if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
  3690. (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
  3691. tuning_block_pattern = tuning_block_128;
  3692. size = sizeof(tuning_block_128);
  3693. }
  3694. spin_unlock_irqrestore(&host->lock, flags);
  3695. /* first of all reset the tuning block */
  3696. rc = msmsdcc_init_cm_sdc4_dll(host);
  3697. if (rc)
  3698. goto out;
  3699. data_buf = kmalloc(size, GFP_KERNEL);
  3700. if (!data_buf) {
  3701. rc = -ENOMEM;
  3702. goto out;
  3703. }
  3704. is_tuning_all_phases = !(host->mmc->card &&
  3705. (host->saved_tuning_phase != INVALID_TUNING_PHASE));
  3706. retry:
  3707. if (is_tuning_all_phases)
  3708. phase = 0; /* start from phase 0 during init */
  3709. else
  3710. phase = (u8)host->saved_tuning_phase;
  3711. do {
  3712. struct mmc_command cmd = {0};
  3713. struct mmc_data data = {0};
  3714. struct mmc_request mrq = {
  3715. .cmd = &cmd,
  3716. .data = &data
  3717. };
  3718. struct scatterlist sg;
  3719. /* set the phase in delay line hw block */
  3720. rc = msmsdcc_config_cm_sdc4_dll_phase(host, phase);
  3721. if (rc)
  3722. goto kfree;
  3723. cmd.opcode = opcode;
  3724. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  3725. data.blksz = size;
  3726. data.blocks = 1;
  3727. data.flags = MMC_DATA_READ;
  3728. data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
  3729. data.sg = &sg;
  3730. data.sg_len = 1;
  3731. sg_init_one(&sg, data_buf, size);
  3732. memset(data_buf, 0, size);
  3733. mmc_wait_for_req(mmc, &mrq);
  3734. if (!cmd.error && !data.error &&
  3735. !memcmp(data_buf, tuning_block_pattern, size)) {
  3736. /* tuning is successful at this tuning point */
  3737. if (!is_tuning_all_phases)
  3738. goto kfree;
  3739. tuned_phases[tuned_phase_cnt++] = phase;
  3740. pr_debug("%s: %s: found good phase = %d\n",
  3741. mmc_hostname(mmc), __func__, phase);
  3742. } else if (!is_tuning_all_phases) {
  3743. pr_debug("%s: tuning failed at saved phase (%d), retrying\n",
  3744. mmc_hostname(mmc), (u32)phase);
  3745. is_tuning_all_phases = true;
  3746. goto retry;
  3747. }
  3748. } while (++phase < 16);
  3749. if (tuned_phase_cnt) {
  3750. rc = find_most_appropriate_phase(host, tuned_phases,
  3751. tuned_phase_cnt);
  3752. if (rc < 0)
  3753. goto kfree;
  3754. else
  3755. phase = (u8)rc;
  3756. /*
  3757. * Finally set the selected phase in delay
  3758. * line hw block.
  3759. */
  3760. rc = msmsdcc_config_cm_sdc4_dll_phase(host, phase);
  3761. if (rc)
  3762. goto kfree;
  3763. else
  3764. host->saved_tuning_phase = phase;
  3765. pr_debug("%s: %s: finally setting the tuning phase to %d\n",
  3766. mmc_hostname(mmc), __func__, phase);
  3767. } else {
  3768. /* tuning failed */
  3769. pr_err("%s: %s: no tuning point found\n",
  3770. mmc_hostname(mmc), __func__);
  3771. msmsdcc_dump_sdcc_state(host);
  3772. rc = -EAGAIN;
  3773. }
  3774. kfree:
  3775. kfree(data_buf);
  3776. out:
  3777. spin_lock_irqsave(&host->lock, flags);
  3778. host->tuning_in_progress = 0;
  3779. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || defined(CONFIG_BCM4354)
  3780. if (!rc || host->pdev->id == 2)
  3781. #else
  3782. if (!rc)
  3783. #endif
  3784. {
  3785. printk("%s : set tuning_done ture.\n", __func__); //add debug
  3786. host->tuning_done = true;
  3787. }
  3788. spin_unlock_irqrestore(&host->lock, flags);
  3789. exit:
  3790. pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
  3791. return rc;
  3792. }
  3793. /**
  3794. * msmsdcc_stop_request - stops ongoing request
  3795. * @mmc: MMC host, running the request
  3796. *
  3797. * Stops currently running request synchronously. All relevant request
  3798. * information is cleared.
  3799. */
  3800. int msmsdcc_stop_request(struct mmc_host *mmc)
  3801. {
  3802. struct msmsdcc_host *host = mmc_priv(mmc);
  3803. struct mmc_request *mrq;
  3804. unsigned long flags;
  3805. int rc = 0;
  3806. spin_lock_irqsave(&host->lock, flags);
  3807. mrq = host->curr.mrq;
  3808. if (mrq) {
  3809. msmsdcc_reset_and_restore(host);
  3810. /*
  3811. * Note: We are just taking care of SPS. We may also
  3812. * need to think about ADM (and PIO?) later if required.
  3813. */
  3814. if (host->sps.sg && is_sps_mode(host)) {
  3815. if (!mrq->data->host_cookie)
  3816. dma_unmap_sg(mmc_dev(host->mmc), host->sps.sg,
  3817. host->sps.num_ents, host->sps.dir);
  3818. host->sps.sg = NULL;
  3819. host->sps.busy = 0;
  3820. }
  3821. /*
  3822. * Clear current request information as current
  3823. * request has ended
  3824. */
  3825. memset(&host->curr, 0, sizeof(struct msmsdcc_curr_req));
  3826. del_timer(&host->req_tout_timer);
  3827. } else {
  3828. rc = -EINVAL;
  3829. }
  3830. spin_unlock_irqrestore(&host->lock, flags);
  3831. return rc;
  3832. }
  3833. /**
  3834. * msmsdcc_get_xfer_remain - returns number of bytes passed on bus
  3835. * @mmc: MMC host, running the request
  3836. *
  3837. * Returns the number of bytes passed for SPS transfer. 0 - for non-SPS
  3838. * transfer.
  3839. */
  3840. unsigned int msmsdcc_get_xfer_remain(struct mmc_host *mmc)
  3841. {
  3842. struct msmsdcc_host *host = mmc_priv(mmc);
  3843. u32 data_cnt = 0;
  3844. /* Currently, we don't support to stop the non-SPS transfer */
  3845. if (host->sps.busy && atomic_read(&host->clks_on))
  3846. data_cnt = readl_relaxed(host->base + MMCIDATACNT);
  3847. return data_cnt;
  3848. }
  3849. static int msmsdcc_notify_load(struct mmc_host *mmc, enum mmc_load state)
  3850. {
  3851. int err = 0;
  3852. unsigned long rate;
  3853. struct msmsdcc_host *host = mmc_priv(mmc);
  3854. if (IS_ERR_OR_NULL(host->bus_clk))
  3855. goto out;
  3856. switch (state) {
  3857. case MMC_LOAD_HIGH:
  3858. rate = MSMSDCC_BUS_VOTE_MAX_RATE;
  3859. break;
  3860. case MMC_LOAD_LOW:
  3861. rate = MSMSDCC_BUS_VOTE_MIN_RATE;
  3862. break;
  3863. default:
  3864. err = -EINVAL;
  3865. goto out;
  3866. }
  3867. if (rate != host->bus_clk_rate) {
  3868. err = clk_set_rate(host->bus_clk, rate);
  3869. if (err)
  3870. pr_err("%s: %s: bus clk set rate %lu Hz err %d\n",
  3871. mmc_hostname(mmc), __func__, rate, err);
  3872. else
  3873. host->bus_clk_rate = rate;
  3874. }
  3875. out:
  3876. return err;
  3877. }
  3878. static const struct mmc_host_ops msmsdcc_ops = {
  3879. .enable = msmsdcc_enable,
  3880. .disable = msmsdcc_disable,
  3881. .pre_req = msmsdcc_pre_req,
  3882. .post_req = msmsdcc_post_req,
  3883. .request = msmsdcc_request,
  3884. .set_ios = msmsdcc_set_ios,
  3885. .get_ro = msmsdcc_get_ro,
  3886. .enable_sdio_irq = msmsdcc_enable_sdio_irq,
  3887. .start_signal_voltage_switch = msmsdcc_switch_io_voltage,
  3888. .execute_tuning = msmsdcc_execute_tuning,
  3889. .stop_request = msmsdcc_stop_request,
  3890. .get_xfer_remain = msmsdcc_get_xfer_remain,
  3891. .notify_load = msmsdcc_notify_load,
  3892. };
  3893. static void msmsdcc_enable_status_gpio(struct msmsdcc_host *host)
  3894. {
  3895. unsigned int gpio_no = host->plat->status_gpio;
  3896. int status;
  3897. if (!gpio_is_valid(gpio_no))
  3898. return;
  3899. status = gpio_request(gpio_no, "SD_HW_Detect");
  3900. if (status)
  3901. pr_err("%s: %s: gpio_request(%d) failed\n",
  3902. mmc_hostname(host->mmc), __func__, gpio_no);
  3903. }
  3904. static void msmsdcc_disable_status_gpio(struct msmsdcc_host *host)
  3905. {
  3906. if (gpio_is_valid(host->plat->status_gpio))
  3907. gpio_free(host->plat->status_gpio);
  3908. }
  3909. static unsigned int
  3910. msmsdcc_slot_status(struct msmsdcc_host *host)
  3911. {
  3912. int status;
  3913. status = gpio_get_value_cansleep(host->plat->status_gpio);
  3914. if (host->plat->is_status_gpio_active_low)
  3915. status = !status;
  3916. return status;
  3917. }
  3918. static void
  3919. msmsdcc_check_status(unsigned long data)
  3920. {
  3921. struct msmsdcc_host *host = (struct msmsdcc_host *)data;
  3922. unsigned int status;
  3923. if (host->plat->status || gpio_is_valid(host->plat->status_gpio)) {
  3924. if (host->plat->status)
  3925. status = host->plat->status(mmc_dev(host->mmc));
  3926. else
  3927. status = msmsdcc_slot_status(host);
  3928. host->eject = !status;
  3929. if (status ^ host->oldstat) {
  3930. if (host->plat->status)
  3931. pr_info("%s: Slot status change detected "
  3932. "(%d -> %d)\n",
  3933. mmc_hostname(host->mmc),
  3934. host->oldstat, status);
  3935. else if (host->plat->is_status_gpio_active_low)
  3936. pr_info("%s: Slot status change detected "
  3937. "(%d -> %d) and the card detect GPIO"
  3938. " is ACTIVE_LOW\n",
  3939. mmc_hostname(host->mmc),
  3940. host->oldstat, status);
  3941. else
  3942. pr_info("%s: Slot status change detected "
  3943. "(%d -> %d) and the card detect GPIO"
  3944. " is ACTIVE_HIGH\n",
  3945. mmc_hostname(host->mmc),
  3946. host->oldstat, status);
  3947. mmc_detect_change(host->mmc, 0);
  3948. }
  3949. host->oldstat = status;
  3950. } else {
  3951. mmc_detect_change(host->mmc, 0);
  3952. }
  3953. }
  3954. static irqreturn_t
  3955. msmsdcc_platform_status_irq(int irq, void *dev_id)
  3956. {
  3957. struct msmsdcc_host *host = dev_id;
  3958. pr_debug("%s: %d\n", __func__, irq);
  3959. msmsdcc_check_status((unsigned long) host);
  3960. return IRQ_HANDLED;
  3961. }
  3962. static irqreturn_t
  3963. msmsdcc_platform_sdiowakeup_irq(int irq, void *dev_id)
  3964. {
  3965. struct msmsdcc_host *host = dev_id;
  3966. pr_debug("%s: SDIO Wake up IRQ : %d\n", mmc_hostname(host->mmc), irq);
  3967. spin_lock(&host->lock);
  3968. if (!host->sdio_wakeupirq_disabled) {
  3969. disable_irq_nosync(irq);
  3970. if (host->sdcc_suspended) {
  3971. wake_lock(&host->sdio_wlock);
  3972. msmsdcc_disable_irq_wake(host);
  3973. }
  3974. host->sdio_wakeupirq_disabled = 1;
  3975. }
  3976. if (host->plat->is_sdio_al_client) {
  3977. wake_lock(&host->sdio_wlock);
  3978. spin_unlock(&host->lock);
  3979. mmc_signal_sdio_irq(host->mmc);
  3980. goto out_unlocked;
  3981. }
  3982. spin_unlock(&host->lock);
  3983. out_unlocked:
  3984. return IRQ_HANDLED;
  3985. }
  3986. static void
  3987. msmsdcc_status_notify_cb(int card_present, void *dev_id)
  3988. {
  3989. struct msmsdcc_host *host = dev_id;
  3990. pr_debug("%s: card_present %d\n", mmc_hostname(host->mmc),
  3991. card_present);
  3992. msmsdcc_check_status((unsigned long) host);
  3993. }
  3994. static int
  3995. msmsdcc_init_dma(struct msmsdcc_host *host)
  3996. {
  3997. memset(&host->dma, 0, sizeof(struct msmsdcc_dma_data));
  3998. host->dma.host = host;
  3999. host->dma.channel = -1;
  4000. host->dma.crci = -1;
  4001. if (!host->dmares)
  4002. return -ENODEV;
  4003. host->dma.nc = dma_alloc_coherent(NULL,
  4004. sizeof(struct msmsdcc_nc_dmadata),
  4005. &host->dma.nc_busaddr,
  4006. GFP_KERNEL);
  4007. if (host->dma.nc == NULL) {
  4008. pr_err("Unable to allocate DMA buffer\n");
  4009. return -ENOMEM;
  4010. }
  4011. memset(host->dma.nc, 0x00, sizeof(struct msmsdcc_nc_dmadata));
  4012. host->dma.cmd_busaddr = host->dma.nc_busaddr;
  4013. host->dma.cmdptr_busaddr = host->dma.nc_busaddr +
  4014. offsetof(struct msmsdcc_nc_dmadata, cmdptr);
  4015. host->dma.channel = host->dmares->start;
  4016. host->dma.crci = host->dma_crci_res->start;
  4017. return 0;
  4018. }
  4019. #ifdef CONFIG_MMC_MSM_SPS_SUPPORT
  4020. /**
  4021. * Allocate and Connect a SDCC peripheral's SPS endpoint
  4022. *
  4023. * This function allocates endpoint context and
  4024. * connect it with memory endpoint by calling
  4025. * appropriate SPS driver APIs.
  4026. *
  4027. * Also registers a SPS callback function with
  4028. * SPS driver
  4029. *
  4030. * This function should only be called once typically
  4031. * during driver probe.
  4032. *
  4033. * @host - Pointer to sdcc host structure
  4034. * @ep - Pointer to sps endpoint data structure
  4035. * @is_produce - 1 means Producer endpoint
  4036. * 0 means Consumer endpoint
  4037. *
  4038. * @return - 0 if successful else negative value.
  4039. *
  4040. */
  4041. static int msmsdcc_sps_init_ep_conn(struct msmsdcc_host *host,
  4042. struct msmsdcc_sps_ep_conn_data *ep,
  4043. bool is_producer)
  4044. {
  4045. int rc = 0;
  4046. struct sps_pipe *sps_pipe_handle;
  4047. struct sps_connect *sps_config = &ep->config;
  4048. struct sps_register_event *sps_event = &ep->event;
  4049. /* Allocate endpoint context */
  4050. sps_pipe_handle = sps_alloc_endpoint();
  4051. if (!sps_pipe_handle) {
  4052. pr_err("%s: sps_alloc_endpoint() failed!!! is_producer=%d",
  4053. mmc_hostname(host->mmc), is_producer);
  4054. rc = -ENOMEM;
  4055. goto out;
  4056. }
  4057. /* Get default connection configuration for an endpoint */
  4058. rc = sps_get_config(sps_pipe_handle, sps_config);
  4059. if (rc) {
  4060. pr_err("%s: sps_get_config() failed!!! pipe_handle=0x%x,"
  4061. " rc=%d", mmc_hostname(host->mmc),
  4062. (u32)sps_pipe_handle, rc);
  4063. goto get_config_err;
  4064. }
  4065. /* Modify the default connection configuration */
  4066. if (is_producer) {
  4067. /*
  4068. * For SDCC producer transfer, source should be
  4069. * SDCC peripheral where as destination should
  4070. * be system memory.
  4071. */
  4072. sps_config->source = host->sps.bam_handle;
  4073. sps_config->destination = SPS_DEV_HANDLE_MEM;
  4074. /* Producer pipe will handle this connection */
  4075. sps_config->mode = SPS_MODE_SRC;
  4076. sps_config->options =
  4077. SPS_O_AUTO_ENABLE | SPS_O_EOT | SPS_O_ACK_TRANSFERS;
  4078. } else {
  4079. /*
  4080. * For SDCC consumer transfer, source should be
  4081. * system memory where as destination should
  4082. * SDCC peripheral
  4083. */
  4084. sps_config->source = SPS_DEV_HANDLE_MEM;
  4085. sps_config->destination = host->sps.bam_handle;
  4086. sps_config->mode = SPS_MODE_DEST;
  4087. sps_config->options =
  4088. SPS_O_AUTO_ENABLE | SPS_O_EOT | SPS_O_ACK_TRANSFERS;
  4089. }
  4090. /* Producer pipe index */
  4091. sps_config->src_pipe_index = host->sps.src_pipe_index;
  4092. /* Consumer pipe index */
  4093. sps_config->dest_pipe_index = host->sps.dest_pipe_index;
  4094. /*
  4095. * This event thresold value is only significant for BAM-to-BAM
  4096. * transfer. It's ignored for BAM-to-System mode transfer.
  4097. */
  4098. sps_config->event_thresh = 0x10;
  4099. /* Allocate maximum descriptor fifo size */
  4100. sps_config->desc.size = SPS_MAX_DESC_FIFO_SIZE -
  4101. (SPS_MAX_DESC_FIFO_SIZE % SPS_MAX_DESC_LENGTH);
  4102. sps_config->desc.base = dma_alloc_coherent(mmc_dev(host->mmc),
  4103. sps_config->desc.size,
  4104. &sps_config->desc.phys_base,
  4105. GFP_KERNEL);
  4106. if (!sps_config->desc.base) {
  4107. rc = -ENOMEM;
  4108. pr_err("%s: dma_alloc_coherent() failed!!! Can't allocate buffer\n"
  4109. , mmc_hostname(host->mmc));
  4110. goto get_config_err;
  4111. }
  4112. memset(sps_config->desc.base, 0x00, sps_config->desc.size);
  4113. /* Establish connection between peripheral and memory endpoint */
  4114. rc = sps_connect(sps_pipe_handle, sps_config);
  4115. if (rc) {
  4116. pr_err("%s: sps_connect() failed!!! pipe_handle=0x%x,"
  4117. " rc=%d", mmc_hostname(host->mmc),
  4118. (u32)sps_pipe_handle, rc);
  4119. goto sps_connect_err;
  4120. }
  4121. sps_event->mode = SPS_TRIGGER_CALLBACK;
  4122. sps_event->options = SPS_O_EOT;
  4123. sps_event->callback = msmsdcc_sps_complete_cb;
  4124. sps_event->xfer_done = NULL;
  4125. sps_event->user = (void *)host;
  4126. /* Register callback event for EOT (End of transfer) event. */
  4127. rc = sps_register_event(sps_pipe_handle, sps_event);
  4128. if (rc) {
  4129. pr_err("%s: sps_connect() failed!!! pipe_handle=0x%x,"
  4130. " rc=%d", mmc_hostname(host->mmc),
  4131. (u32)sps_pipe_handle, rc);
  4132. goto reg_event_err;
  4133. }
  4134. /* Now save the sps pipe handle */
  4135. ep->pipe_handle = sps_pipe_handle;
  4136. pr_debug("%s: %s, success !!! %s: pipe_handle=0x%x,"\
  4137. " desc_fifo.phys_base=%pa\n", mmc_hostname(host->mmc),
  4138. __func__, is_producer ? "READ" : "WRITE",
  4139. (u32)sps_pipe_handle, &sps_config->desc.phys_base);
  4140. goto out;
  4141. reg_event_err:
  4142. sps_disconnect(sps_pipe_handle);
  4143. sps_connect_err:
  4144. dma_free_coherent(mmc_dev(host->mmc),
  4145. sps_config->desc.size,
  4146. sps_config->desc.base,
  4147. sps_config->desc.phys_base);
  4148. get_config_err:
  4149. sps_free_endpoint(sps_pipe_handle);
  4150. out:
  4151. return rc;
  4152. }
  4153. /**
  4154. * Disconnect and Deallocate a SDCC peripheral's SPS endpoint
  4155. *
  4156. * This function disconnect endpoint and deallocates
  4157. * endpoint context.
  4158. *
  4159. * This function should only be called once typically
  4160. * during driver remove.
  4161. *
  4162. * @host - Pointer to sdcc host structure
  4163. * @ep - Pointer to sps endpoint data structure
  4164. *
  4165. */
  4166. static void msmsdcc_sps_exit_ep_conn(struct msmsdcc_host *host,
  4167. struct msmsdcc_sps_ep_conn_data *ep)
  4168. {
  4169. struct sps_pipe *sps_pipe_handle = ep->pipe_handle;
  4170. struct sps_connect *sps_config = &ep->config;
  4171. struct sps_register_event *sps_event = &ep->event;
  4172. sps_event->xfer_done = NULL;
  4173. sps_event->callback = NULL;
  4174. sps_register_event(sps_pipe_handle, sps_event);
  4175. sps_disconnect(sps_pipe_handle);
  4176. dma_free_coherent(mmc_dev(host->mmc),
  4177. sps_config->desc.size,
  4178. sps_config->desc.base,
  4179. sps_config->desc.phys_base);
  4180. sps_free_endpoint(sps_pipe_handle);
  4181. }
  4182. /**
  4183. * Reset SDCC peripheral's SPS endpoint
  4184. *
  4185. * This function disconnects an endpoint.
  4186. *
  4187. * This function should be called for reseting
  4188. * SPS endpoint when data transfer error is
  4189. * encountered during data transfer. This
  4190. * can be considered as soft reset to endpoint.
  4191. *
  4192. * This function should only be called if
  4193. * msmsdcc_sps_init() is already called.
  4194. *
  4195. * @host - Pointer to sdcc host structure
  4196. * @ep - Pointer to sps endpoint data structure
  4197. *
  4198. * @return - 0 if successful else negative value.
  4199. */
  4200. static int msmsdcc_sps_reset_ep(struct msmsdcc_host *host,
  4201. struct msmsdcc_sps_ep_conn_data *ep)
  4202. {
  4203. int rc = 0;
  4204. struct sps_pipe *sps_pipe_handle = ep->pipe_handle;
  4205. rc = sps_disconnect(sps_pipe_handle);
  4206. if (rc) {
  4207. pr_err("%s: %s: sps_disconnect() failed!!! pipe_handle=0x%x,"
  4208. " rc=%d", mmc_hostname(host->mmc), __func__,
  4209. (u32)sps_pipe_handle, rc);
  4210. goto out;
  4211. }
  4212. out:
  4213. return rc;
  4214. }
  4215. /**
  4216. * Restore SDCC peripheral's SPS endpoint
  4217. *
  4218. * This function connects an endpoint.
  4219. *
  4220. * This function should be called for restoring
  4221. * SPS endpoint after data transfer error is
  4222. * encountered during data transfer. This
  4223. * can be considered as soft reset to endpoint.
  4224. *
  4225. * This function should only be called if
  4226. * msmsdcc_sps_reset_ep() is called before.
  4227. *
  4228. * @host - Pointer to sdcc host structure
  4229. * @ep - Pointer to sps endpoint data structure
  4230. *
  4231. * @return - 0 if successful else negative value.
  4232. */
  4233. static int msmsdcc_sps_restore_ep(struct msmsdcc_host *host,
  4234. struct msmsdcc_sps_ep_conn_data *ep)
  4235. {
  4236. int rc = 0;
  4237. struct sps_pipe *sps_pipe_handle = ep->pipe_handle;
  4238. struct sps_connect *sps_config = &ep->config;
  4239. struct sps_register_event *sps_event = &ep->event;
  4240. /* Establish connection between peripheral and memory endpoint */
  4241. rc = sps_connect(sps_pipe_handle, sps_config);
  4242. if (rc) {
  4243. pr_err("%s: %s: sps_connect() failed!!! pipe_handle=0x%x,"
  4244. " rc=%d", mmc_hostname(host->mmc), __func__,
  4245. (u32)sps_pipe_handle, rc);
  4246. goto out;
  4247. }
  4248. /* Register callback event for EOT (End of transfer) event. */
  4249. rc = sps_register_event(sps_pipe_handle, sps_event);
  4250. if (rc) {
  4251. pr_err("%s: %s: sps_register_event() failed!!!"
  4252. " pipe_handle=0x%x, rc=%d",
  4253. mmc_hostname(host->mmc), __func__,
  4254. (u32)sps_pipe_handle, rc);
  4255. goto reg_event_err;
  4256. }
  4257. goto out;
  4258. reg_event_err:
  4259. sps_disconnect(sps_pipe_handle);
  4260. out:
  4261. return rc;
  4262. }
  4263. /**
  4264. * Handle BAM device's global error condition
  4265. *
  4266. * This is an error handler for the SDCC bam device
  4267. *
  4268. * This function is registered as a callback with SPS-BAM
  4269. * driver and will called in case there are an errors for
  4270. * the SDCC BAM deivce. Any error conditions in the BAM
  4271. * device are global and will be result in this function
  4272. * being called once per device.
  4273. *
  4274. * This function will be called from the sps driver's
  4275. * interrupt context.
  4276. *
  4277. * @sps_cb_case - indicates what error it is
  4278. * @user - Pointer to sdcc host structure
  4279. */
  4280. static void
  4281. msmsdcc_sps_bam_global_irq_cb(enum sps_callback_case sps_cb_case, void *user)
  4282. {
  4283. struct msmsdcc_host *host = (struct msmsdcc_host *)user;
  4284. struct mmc_request *mrq;
  4285. unsigned long flags;
  4286. int32_t error = 0;
  4287. BUG_ON(!host);
  4288. BUG_ON(!is_sps_mode(host));
  4289. if (sps_cb_case == SPS_CALLBACK_BAM_ERROR_IRQ) {
  4290. /* Reset all endpoints along with resetting bam. */
  4291. host->sps.reset_bam = true;
  4292. pr_err("%s: BAM Global ERROR IRQ happened\n",
  4293. mmc_hostname(host->mmc));
  4294. error = EAGAIN;
  4295. } else if (sps_cb_case == SPS_CALLBACK_BAM_HRESP_ERR_IRQ) {
  4296. /**
  4297. * This means that there was an AHB access error and
  4298. * the address we are trying to read/write is something
  4299. * we dont have priviliges to do so.
  4300. */
  4301. pr_err("%s: BAM HRESP_ERR_IRQ happened\n",
  4302. mmc_hostname(host->mmc));
  4303. error = EACCES;
  4304. } else {
  4305. /**
  4306. * This should not have happened ideally. If this happens
  4307. * there is some seriously wrong.
  4308. */
  4309. pr_err("%s: BAM global IRQ callback received, type:%d\n",
  4310. mmc_hostname(host->mmc), (u32) sps_cb_case);
  4311. error = EIO;
  4312. }
  4313. spin_lock_irqsave(&host->lock, flags);
  4314. mrq = host->curr.mrq;
  4315. if (mrq && mrq->cmd) {
  4316. msmsdcc_dump_sdcc_state(host);
  4317. if (!mrq->cmd->error)
  4318. mrq->cmd->error = -error;
  4319. if (host->curr.data) {
  4320. if (mrq->data && !mrq->data->error)
  4321. mrq->data->error = -error;
  4322. host->curr.data_xfered = 0;
  4323. if (host->sps.sg && is_sps_mode(host)) {
  4324. /* Stop current SPS transfer */
  4325. msmsdcc_sps_exit_curr_xfer(host);
  4326. } else {
  4327. /* this condition should not have happened */
  4328. pr_err("%s: something is seriously wrong. "\
  4329. "Funtion: %s, line: %d\n",
  4330. mmc_hostname(host->mmc),
  4331. __func__, __LINE__);
  4332. }
  4333. } else {
  4334. /* this condition should not have happened */
  4335. pr_err("%s: something is seriously wrong. Funtion: "\
  4336. "%s, line: %d\n", mmc_hostname(host->mmc),
  4337. __func__, __LINE__);
  4338. }
  4339. }
  4340. spin_unlock_irqrestore(&host->lock, flags);
  4341. }
  4342. /**
  4343. * Initialize SPS HW connected with SDCC core
  4344. *
  4345. * This function register BAM HW resources with
  4346. * SPS driver and then initialize 2 SPS endpoints
  4347. *
  4348. * This function should only be called once typically
  4349. * during driver probe.
  4350. *
  4351. * @host - Pointer to sdcc host structure
  4352. *
  4353. * @return - 0 if successful else negative value.
  4354. *
  4355. */
  4356. static int msmsdcc_sps_init(struct msmsdcc_host *host)
  4357. {
  4358. int rc = 0;
  4359. struct sps_bam_props bam = {0};
  4360. host->bam_base = ioremap(host->bam_memres->start,
  4361. resource_size(host->bam_memres));
  4362. if (!host->bam_base) {
  4363. pr_err("%s: BAM ioremap() failed!!! resource: %pr\n",
  4364. mmc_hostname(host->mmc), host->bam_memres);
  4365. rc = -ENOMEM;
  4366. goto out;
  4367. }
  4368. bam.phys_addr = host->bam_memres->start;
  4369. bam.virt_addr = host->bam_base;
  4370. /*
  4371. * This event thresold value is only significant for BAM-to-BAM
  4372. * transfer. It's ignored for BAM-to-System mode transfer.
  4373. */
  4374. bam.event_threshold = 0x10; /* Pipe event threshold */
  4375. /*
  4376. * This threshold controls when the BAM publish
  4377. * the descriptor size on the sideband interface.
  4378. * SPS HW will be used for data transfer size even
  4379. * less than SDCC FIFO size. So let's set BAM summing
  4380. * thresold to SPS_MIN_XFER_SIZE bytes.
  4381. */
  4382. bam.summing_threshold = SPS_MIN_XFER_SIZE;
  4383. /* SPS driver wll handle the SDCC BAM IRQ */
  4384. bam.irq = host->bam_irqres->start;
  4385. bam.manage = SPS_BAM_MGR_LOCAL;
  4386. bam.callback = msmsdcc_sps_bam_global_irq_cb;
  4387. bam.user = (void *)host;
  4388. /* bam reset messages will be limited to 5 times */
  4389. bam.constrained_logging = true;
  4390. bam.logging_number = 5;
  4391. pr_info("%s: bam physical base=0x%x\n", mmc_hostname(host->mmc),
  4392. (u32)bam.phys_addr);
  4393. pr_info("%s: bam virtual base=0x%x\n", mmc_hostname(host->mmc),
  4394. (u32)bam.virt_addr);
  4395. /* Register SDCC Peripheral BAM device to SPS driver */
  4396. rc = sps_register_bam_device(&bam, &host->sps.bam_handle);
  4397. if (rc) {
  4398. pr_err("%s: sps_register_bam_device() failed!!! err=%d",
  4399. mmc_hostname(host->mmc), rc);
  4400. goto reg_bam_err;
  4401. }
  4402. pr_info("%s: BAM device registered. bam_handle=0x%x",
  4403. mmc_hostname(host->mmc), host->sps.bam_handle);
  4404. host->sps.src_pipe_index = SPS_SDCC_PRODUCER_PIPE_INDEX;
  4405. host->sps.dest_pipe_index = SPS_SDCC_CONSUMER_PIPE_INDEX;
  4406. rc = msmsdcc_sps_init_ep_conn(host, &host->sps.prod,
  4407. SPS_PROD_PERIPHERAL);
  4408. if (rc)
  4409. goto sps_reset_err;
  4410. rc = msmsdcc_sps_init_ep_conn(host, &host->sps.cons,
  4411. SPS_CONS_PERIPHERAL);
  4412. if (rc)
  4413. goto cons_conn_err;
  4414. pr_info("%s: Qualcomm MSM SDCC-BAM at %pr %pr\n",
  4415. mmc_hostname(host->mmc), host->bam_memres, host->bam_irqres);
  4416. goto out;
  4417. cons_conn_err:
  4418. msmsdcc_sps_exit_ep_conn(host, &host->sps.prod);
  4419. sps_reset_err:
  4420. sps_deregister_bam_device(host->sps.bam_handle);
  4421. reg_bam_err:
  4422. iounmap(host->bam_base);
  4423. out:
  4424. return rc;
  4425. }
  4426. /**
  4427. * De-initialize SPS HW connected with SDCC core
  4428. *
  4429. * This function deinitialize SPS endpoints and then
  4430. * deregisters BAM resources from SPS driver.
  4431. *
  4432. * This function should only be called once typically
  4433. * during driver remove.
  4434. *
  4435. * @host - Pointer to sdcc host structure
  4436. *
  4437. */
  4438. static void msmsdcc_sps_exit(struct msmsdcc_host *host)
  4439. {
  4440. msmsdcc_sps_exit_ep_conn(host, &host->sps.cons);
  4441. msmsdcc_sps_exit_ep_conn(host, &host->sps.prod);
  4442. sps_deregister_bam_device(host->sps.bam_handle);
  4443. iounmap(host->bam_base);
  4444. }
  4445. #endif /* CONFIG_MMC_MSM_SPS_SUPPORT */
  4446. static ssize_t
  4447. show_polling(struct device *dev, struct device_attribute *attr, char *buf)
  4448. {
  4449. struct mmc_host *mmc = dev_get_drvdata(dev);
  4450. struct msmsdcc_host *host = mmc_priv(mmc);
  4451. int poll;
  4452. unsigned long flags;
  4453. spin_lock_irqsave(&host->lock, flags);
  4454. poll = !!(mmc->caps & MMC_CAP_NEEDS_POLL);
  4455. spin_unlock_irqrestore(&host->lock, flags);
  4456. return snprintf(buf, PAGE_SIZE, "%d\n", poll);
  4457. }
  4458. static ssize_t
  4459. store_polling(struct device *dev, struct device_attribute *attr,
  4460. const char *buf, size_t count)
  4461. {
  4462. struct mmc_host *mmc = dev_get_drvdata(dev);
  4463. struct msmsdcc_host *host = mmc_priv(mmc);
  4464. int value;
  4465. unsigned long flags;
  4466. sscanf(buf, "%d", &value);
  4467. spin_lock_irqsave(&host->lock, flags);
  4468. if (value) {
  4469. mmc->caps |= MMC_CAP_NEEDS_POLL;
  4470. mmc_detect_change(host->mmc, 0);
  4471. } else {
  4472. mmc->caps &= ~MMC_CAP_NEEDS_POLL;
  4473. }
  4474. spin_unlock_irqrestore(&host->lock, flags);
  4475. return count;
  4476. }
  4477. static ssize_t
  4478. show_sdcc_to_mem_max_bus_bw(struct device *dev, struct device_attribute *attr,
  4479. char *buf)
  4480. {
  4481. struct mmc_host *mmc = dev_get_drvdata(dev);
  4482. struct msmsdcc_host *host = mmc_priv(mmc);
  4483. return snprintf(buf, PAGE_SIZE, "%u\n",
  4484. host->msm_bus_vote.is_max_bw_needed);
  4485. }
  4486. static ssize_t
  4487. store_sdcc_to_mem_max_bus_bw(struct device *dev, struct device_attribute *attr,
  4488. const char *buf, size_t count)
  4489. {
  4490. struct mmc_host *mmc = dev_get_drvdata(dev);
  4491. struct msmsdcc_host *host = mmc_priv(mmc);
  4492. uint32_t value;
  4493. unsigned long flags;
  4494. if (!kstrtou32(buf, 0, &value)) {
  4495. spin_lock_irqsave(&host->lock, flags);
  4496. host->msm_bus_vote.is_max_bw_needed = !!value;
  4497. spin_unlock_irqrestore(&host->lock, flags);
  4498. }
  4499. return count;
  4500. }
  4501. static ssize_t
  4502. show_idle_timeout(struct device *dev, struct device_attribute *attr,
  4503. char *buf)
  4504. {
  4505. struct mmc_host *mmc = dev_get_drvdata(dev);
  4506. struct msmsdcc_host *host = mmc_priv(mmc);
  4507. return snprintf(buf, PAGE_SIZE, "%u (Min 5 sec)\n",
  4508. host->idle_tout / 1000);
  4509. }
  4510. static ssize_t
  4511. store_idle_timeout(struct device *dev, struct device_attribute *attr,
  4512. const char *buf, size_t count)
  4513. {
  4514. struct mmc_host *mmc = dev_get_drvdata(dev);
  4515. struct msmsdcc_host *host = mmc_priv(mmc);
  4516. unsigned int long flags;
  4517. int timeout; /* in secs */
  4518. if (!kstrtou32(buf, 0, &timeout)
  4519. && (timeout > MSM_MMC_DEFAULT_IDLE_TIMEOUT / 1000)) {
  4520. spin_lock_irqsave(&host->lock, flags);
  4521. host->idle_tout = timeout * 1000;
  4522. spin_unlock_irqrestore(&host->lock, flags);
  4523. }
  4524. return count;
  4525. }
  4526. static inline void set_auto_cmd_setting(struct device *dev,
  4527. const char *buf,
  4528. bool is_cmd19)
  4529. {
  4530. struct mmc_host *mmc = dev_get_drvdata(dev);
  4531. struct msmsdcc_host *host = mmc_priv(mmc);
  4532. unsigned int long flags;
  4533. int temp;
  4534. if (!kstrtou32(buf, 0, &temp)) {
  4535. spin_lock_irqsave(&host->lock, flags);
  4536. if (is_cmd19)
  4537. host->en_auto_cmd19 = !!temp;
  4538. else
  4539. host->en_auto_cmd21 = !!temp;
  4540. spin_unlock_irqrestore(&host->lock, flags);
  4541. }
  4542. }
  4543. static ssize_t
  4544. show_enable_auto_cmd19(struct device *dev, struct device_attribute *attr,
  4545. char *buf)
  4546. {
  4547. struct mmc_host *mmc = dev_get_drvdata(dev);
  4548. struct msmsdcc_host *host = mmc_priv(mmc);
  4549. return snprintf(buf, PAGE_SIZE, "%d\n", host->en_auto_cmd19);
  4550. }
  4551. static ssize_t
  4552. store_enable_auto_cmd19(struct device *dev, struct device_attribute *attr,
  4553. const char *buf, size_t count)
  4554. {
  4555. set_auto_cmd_setting(dev, buf, true);
  4556. return count;
  4557. }
  4558. static ssize_t
  4559. show_enable_auto_cmd21(struct device *dev, struct device_attribute *attr,
  4560. char *buf)
  4561. {
  4562. struct mmc_host *mmc = dev_get_drvdata(dev);
  4563. struct msmsdcc_host *host = mmc_priv(mmc);
  4564. return snprintf(buf, PAGE_SIZE, "%d\n", host->en_auto_cmd21);
  4565. }
  4566. static ssize_t
  4567. store_enable_auto_cmd21(struct device *dev, struct device_attribute *attr,
  4568. const char *buf, size_t count)
  4569. {
  4570. set_auto_cmd_setting(dev, buf, false);
  4571. return count;
  4572. }
  4573. static void msmsdcc_print_regs(const char *name, void __iomem *base,
  4574. resource_size_t phys_base,
  4575. unsigned int no_of_regs)
  4576. {
  4577. unsigned int i;
  4578. if (!base)
  4579. return;
  4580. pr_err("===== %s: Register Dumps @phys_base=%pa, @virt_base=0x%x"\
  4581. " =====\n", name, &phys_base, (u32)base);
  4582. for (i = 0; i < no_of_regs; i = i + 4) {
  4583. pr_err("Reg=0x%.2x: 0x%.8x, 0x%.8x, 0x%.8x, 0x%.8x\n", i*4,
  4584. (u32)readl_relaxed(base + i*4),
  4585. (u32)readl_relaxed(base + ((i+1)*4)),
  4586. (u32)readl_relaxed(base + ((i+2)*4)),
  4587. (u32)readl_relaxed(base + ((i+3)*4)));
  4588. }
  4589. }
  4590. /*
  4591. * This function prints the testbus debug output for all the
  4592. * available SDCC controller test bus.
  4593. *
  4594. * Note: This function should only be called if the SDCC is clocked.
  4595. */
  4596. static void msmsdcc_print_testbus_info(struct msmsdcc_host *host)
  4597. {
  4598. int testbus_num;
  4599. if (!is_testbus_debug(host))
  4600. return;
  4601. pr_err("== SDCC Test Bus Debug ==");
  4602. for (testbus_num = 0; testbus_num < MAX_TESTBUS; testbus_num++) {
  4603. writel_relaxed(((testbus_num & MCI_TESTBUS_SEL_MASK)
  4604. | MCI_TESTBUS_ENA),
  4605. host->base + MCI_TESTBUS_CONFIG);
  4606. pr_err("TestBus(%d) = 0x%.8x\n", testbus_num,
  4607. (u32)readl_relaxed(host->base + MCI_SDCC_DEBUG_REG));
  4608. }
  4609. /* Disable the test bus output */
  4610. writel_relaxed(~MCI_TESTBUS_ENA, host->base + MCI_TESTBUS_CONFIG);
  4611. }
  4612. static void msmsdcc_dump_sdcc_state(struct msmsdcc_host *host)
  4613. {
  4614. /* Dump current state of SDCC clocks, power and irq */
  4615. pr_err("%s: SDCC PWR is %s\n", mmc_hostname(host->mmc),
  4616. (host->pwr ? "ON" : "OFF"));
  4617. pr_err("%s: SDCC clks are %s, MCLK rate=%d\n",
  4618. mmc_hostname(host->mmc),
  4619. (atomic_read(&host->clks_on) ? "ON" : "OFF"),
  4620. (u32)clk_get_rate(host->clk));
  4621. pr_err("%s: SDCC irq is %s\n", mmc_hostname(host->mmc),
  4622. (host->sdcc_irq_disabled ? "disabled" : "enabled"));
  4623. /* Now dump SDCC registers. Don't print FIFO registers */
  4624. if (atomic_read(&host->clks_on)) {
  4625. msmsdcc_print_regs("SDCC-CORE", host->base,
  4626. host->core_memres->start, 28);
  4627. pr_err("%s: MCI_TEST_INPUT = 0x%.8x\n",
  4628. mmc_hostname(host->mmc),
  4629. readl_relaxed(host->base + MCI_TEST_INPUT));
  4630. msmsdcc_print_testbus_info(host);
  4631. }
  4632. if (host->curr.data) {
  4633. if (!msmsdcc_is_dma_possible(host, host->curr.data))
  4634. pr_err("%s: PIO mode\n", mmc_hostname(host->mmc));
  4635. else if (is_dma_mode(host))
  4636. pr_err("%s: ADM mode: busy=%d, chnl=%d, crci=%d\n",
  4637. mmc_hostname(host->mmc), host->dma.busy,
  4638. host->dma.channel, host->dma.crci);
  4639. else if (is_sps_mode(host)) {
  4640. if (host->sps.busy && atomic_read(&host->clks_on))
  4641. msmsdcc_print_regs("SDCC-DML", host->dml_base,
  4642. host->dml_memres->start,
  4643. 16);
  4644. pr_err("%s: SPS mode: busy=%d\n",
  4645. mmc_hostname(host->mmc), host->sps.busy);
  4646. }
  4647. pr_err("%s: xfer_size=%d, data_xfered=%d, xfer_remain=%d\n",
  4648. mmc_hostname(host->mmc), host->curr.xfer_size,
  4649. host->curr.data_xfered, host->curr.xfer_remain);
  4650. }
  4651. if (host->sps.reset_bam)
  4652. pr_err("%s: SPS BAM reset failed: sps reset_bam=%d\n",
  4653. mmc_hostname(host->mmc), host->sps.reset_bam);
  4654. pr_err("%s: got_dataend=%d, prog_enable=%d,"
  4655. " wait_for_auto_prog_done=%d, got_auto_prog_done=%d,"
  4656. " req_tout_ms=%d\n", mmc_hostname(host->mmc),
  4657. host->curr.got_dataend, host->prog_enable,
  4658. host->curr.wait_for_auto_prog_done,
  4659. host->curr.got_auto_prog_done, host->curr.req_tout_ms);
  4660. msmsdcc_print_rpm_info(host);
  4661. }
  4662. static void msmsdcc_req_tout_timer_hdlr(unsigned long data)
  4663. {
  4664. struct msmsdcc_host *host = (struct msmsdcc_host *)data;
  4665. struct mmc_request *mrq;
  4666. unsigned long flags;
  4667. spin_lock_irqsave(&host->lock, flags);
  4668. if (host->dummy_52_sent) {
  4669. pr_info("%s: %s: dummy CMD52 timeout\n",
  4670. mmc_hostname(host->mmc), __func__);
  4671. host->dummy_52_sent = 0;
  4672. }
  4673. mrq = host->curr.mrq;
  4674. if (mrq && mrq->cmd) {
  4675. if (!mrq->cmd->ignore_timeout) {
  4676. pr_info("%s: CMD%d: Request timeout\n",
  4677. mmc_hostname(host->mmc), mrq->cmd->opcode);
  4678. msmsdcc_dump_sdcc_state(host);
  4679. }
  4680. if (!mrq->cmd->error)
  4681. mrq->cmd->error = -ETIMEDOUT;
  4682. host->dummy_52_needed = 0;
  4683. if (host->curr.data) {
  4684. if (mrq->data && !mrq->data->error)
  4685. mrq->data->error = -ETIMEDOUT;
  4686. host->curr.data_xfered = 0;
  4687. if (host->dma.sg && is_dma_mode(host)) {
  4688. msm_dmov_flush(host->dma.channel, 0);
  4689. } else if (host->sps.sg && is_sps_mode(host)) {
  4690. /* Stop current SPS transfer */
  4691. msmsdcc_sps_exit_curr_xfer(host);
  4692. } else {
  4693. msmsdcc_clear_pio_irq_mask(host);
  4694. msmsdcc_reset_and_restore(host);
  4695. msmsdcc_stop_data(host);
  4696. if (mrq->data && mrq->data->stop)
  4697. msmsdcc_start_command(host,
  4698. mrq->data->stop, 0);
  4699. else
  4700. msmsdcc_request_end(host, mrq);
  4701. }
  4702. } else {
  4703. host->prog_enable = 0;
  4704. host->curr.wait_for_auto_prog_done = false;
  4705. msmsdcc_reset_and_restore(host);
  4706. msmsdcc_request_end(host, mrq);
  4707. }
  4708. }
  4709. spin_unlock_irqrestore(&host->lock, flags);
  4710. }
  4711. /*
  4712. * msmsdcc_dt_get_array - Wrapper fn to read an array of 32 bit integers
  4713. *
  4714. * @dev: device node from which the property value is to be read.
  4715. * @prop_name: name of the property to be searched.
  4716. * @out_array: filled array returned to caller
  4717. * @len: filled array size returned to caller
  4718. * @size: expected size of the array
  4719. *
  4720. * If expected "size" doesn't match with "len" an error is returned. If
  4721. * expected size is zero, the length of actual array is returned provided
  4722. * return value is zero.
  4723. *
  4724. * RETURNS:
  4725. * zero on success, negative error if failed.
  4726. */
  4727. static int msmsdcc_dt_get_array(struct device *dev, const char *prop_name,
  4728. u32 **out_array, int *len, int size)
  4729. {
  4730. int ret = 0;
  4731. u32 *array = NULL;
  4732. struct device_node *np = dev->of_node;
  4733. if (of_get_property(np, prop_name, len)) {
  4734. size_t sz;
  4735. sz = *len = *len / sizeof(*array);
  4736. if (sz > 0 && !(size > 0 && (sz != size))) {
  4737. array = devm_kzalloc(dev, sz * sizeof(*array),
  4738. GFP_KERNEL);
  4739. if (!array) {
  4740. dev_err(dev, "%s: no memory\n", prop_name);
  4741. ret = -ENOMEM;
  4742. goto out;
  4743. }
  4744. ret = of_property_read_u32_array(np, prop_name,
  4745. array, sz);
  4746. if (ret < 0) {
  4747. dev_err(dev, "%s: error reading array %d\n",
  4748. prop_name, ret);
  4749. goto out;
  4750. }
  4751. } else {
  4752. dev_err(dev, "%s invalid size\n", prop_name);
  4753. ret = -EINVAL;
  4754. goto out;
  4755. }
  4756. } else {
  4757. dev_err(dev, "%s not specified\n", prop_name);
  4758. ret = -EINVAL;
  4759. goto out;
  4760. }
  4761. *out_array = array;
  4762. out:
  4763. if (ret)
  4764. *len = 0;
  4765. return ret;
  4766. }
  4767. static int msmsdcc_dt_get_pad_pull_info(struct device *dev, int id,
  4768. struct msm_mmc_pad_pull_data **pad_pull_data)
  4769. {
  4770. int ret = 0, base = 0, len, i;
  4771. u32 *tmp;
  4772. struct msm_mmc_pad_pull_data *pull_data;
  4773. struct msm_mmc_pad_pull *pull;
  4774. switch (id) {
  4775. case 1:
  4776. base = TLMM_PULL_SDC1_CLK;
  4777. break;
  4778. case 2:
  4779. base = TLMM_PULL_SDC2_CLK;
  4780. break;
  4781. case 3:
  4782. base = TLMM_PULL_SDC3_CLK;
  4783. break;
  4784. case 4:
  4785. base = TLMM_PULL_SDC4_CLK;
  4786. break;
  4787. default:
  4788. dev_err(dev, "%s: Invalid slot id\n", __func__);
  4789. ret = -EINVAL;
  4790. goto err;
  4791. }
  4792. pull_data = devm_kzalloc(dev, sizeof(struct msm_mmc_pad_pull_data),
  4793. GFP_KERNEL);
  4794. if (!pull_data) {
  4795. dev_err(dev, "No memory msm_mmc_pad_pull_data\n");
  4796. ret = -ENOMEM;
  4797. goto err;
  4798. }
  4799. pull_data->size = 3; /* array size for clk, cmd, data */
  4800. /* Allocate on, off configs for clk, cmd, data */
  4801. pull = devm_kzalloc(dev, 2 * pull_data->size *\
  4802. sizeof(struct msm_mmc_pad_pull), GFP_KERNEL);
  4803. if (!pull) {
  4804. dev_err(dev, "No memory for msm_mmc_pad_pull\n");
  4805. ret = -ENOMEM;
  4806. goto err;
  4807. }
  4808. pull_data->on = pull;
  4809. pull_data->off = pull + pull_data->size;
  4810. ret = msmsdcc_dt_get_array(dev, "qcom,pad-pull-on",
  4811. &tmp, &len, pull_data->size);
  4812. if (!ret) {
  4813. for (i = 0; i < len; i++) {
  4814. pull_data->on[i].no = base + i;
  4815. pull_data->on[i].val = tmp[i];
  4816. dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
  4817. i, pull_data->on[i].val);
  4818. }
  4819. } else {
  4820. goto err;
  4821. }
  4822. ret = msmsdcc_dt_get_array(dev, "qcom,pad-pull-off",
  4823. &tmp, &len, pull_data->size);
  4824. if (!ret) {
  4825. for (i = 0; i < len; i++) {
  4826. pull_data->off[i].no = base + i;
  4827. pull_data->off[i].val = tmp[i];
  4828. dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
  4829. i, pull_data->off[i].val);
  4830. }
  4831. } else {
  4832. goto err;
  4833. }
  4834. *pad_pull_data = pull_data;
  4835. err:
  4836. return ret;
  4837. }
  4838. static int msmsdcc_dt_get_pad_drv_info(struct device *dev, int id,
  4839. struct msm_mmc_pad_drv_data **pad_drv_data)
  4840. {
  4841. int ret = 0, base = 0, len, i;
  4842. u32 *tmp;
  4843. struct msm_mmc_pad_drv_data *drv_data;
  4844. struct msm_mmc_pad_drv *drv;
  4845. switch (id) {
  4846. case 1:
  4847. base = TLMM_HDRV_SDC1_CLK;
  4848. break;
  4849. case 2:
  4850. base = TLMM_HDRV_SDC2_CLK;
  4851. break;
  4852. case 3:
  4853. base = TLMM_HDRV_SDC3_CLK;
  4854. break;
  4855. case 4:
  4856. base = TLMM_HDRV_SDC4_CLK;
  4857. break;
  4858. default:
  4859. dev_err(dev, "%s: Invalid slot id\n", __func__);
  4860. ret = -EINVAL;
  4861. goto err;
  4862. }
  4863. drv_data = devm_kzalloc(dev, sizeof(struct msm_mmc_pad_drv_data),
  4864. GFP_KERNEL);
  4865. if (!drv_data) {
  4866. dev_err(dev, "No memory for msm_mmc_pad_drv_data\n");
  4867. ret = -ENOMEM;
  4868. goto err;
  4869. }
  4870. drv_data->size = 3; /* array size for clk, cmd, data */
  4871. /* Allocate on, off configs for clk, cmd, data */
  4872. drv = devm_kzalloc(dev, 2 * drv_data->size *\
  4873. sizeof(struct msm_mmc_pad_drv), GFP_KERNEL);
  4874. if (!drv) {
  4875. dev_err(dev, "No memory msm_mmc_pad_drv\n");
  4876. ret = -ENOMEM;
  4877. goto err;
  4878. }
  4879. drv_data->on = drv;
  4880. drv_data->off = drv + drv_data->size;
  4881. ret = msmsdcc_dt_get_array(dev, "qcom,pad-drv-on",
  4882. &tmp, &len, drv_data->size);
  4883. if (!ret) {
  4884. for (i = 0; i < len; i++) {
  4885. drv_data->on[i].no = base + i;
  4886. drv_data->on[i].val = tmp[i];
  4887. dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
  4888. i, drv_data->on[i].val);
  4889. }
  4890. } else {
  4891. goto err;
  4892. }
  4893. ret = msmsdcc_dt_get_array(dev, "qcom,pad-drv-off",
  4894. &tmp, &len, drv_data->size);
  4895. if (!ret) {
  4896. for (i = 0; i < len; i++) {
  4897. drv_data->off[i].no = base + i;
  4898. drv_data->off[i].val = tmp[i];
  4899. dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
  4900. i, drv_data->off[i].val);
  4901. }
  4902. } else {
  4903. goto err;
  4904. }
  4905. *pad_drv_data = drv_data;
  4906. err:
  4907. return ret;
  4908. }
  4909. static void msmsdcc_dt_get_cd_wp_gpio(struct device *dev,
  4910. struct mmc_platform_data *pdata)
  4911. {
  4912. enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
  4913. struct device_node *np = dev->of_node;
  4914. pdata->status_gpio = of_get_named_gpio_flags(np,
  4915. "cd-gpios", 0, &flags);
  4916. if (gpio_is_valid(pdata->status_gpio)) {
  4917. struct platform_device *pdev = container_of(dev,
  4918. struct platform_device, dev);
  4919. pdata->status_irq = platform_get_irq_byname(pdev, "status_irq");
  4920. pdata->is_status_gpio_active_low = flags & OF_GPIO_ACTIVE_LOW;
  4921. }
  4922. pdata->wpswitch_gpio = of_get_named_gpio_flags(np,
  4923. "wp-gpios", 0, &flags);
  4924. if (gpio_is_valid(pdata->wpswitch_gpio))
  4925. pdata->is_wpswitch_active_low = flags & OF_GPIO_ACTIVE_LOW;
  4926. }
  4927. static int msmsdcc_dt_parse_gpio_info(struct device *dev,
  4928. struct mmc_platform_data *pdata)
  4929. {
  4930. int ret = 0, id = 0, cnt, i;
  4931. struct msm_mmc_pin_data *pin_data;
  4932. struct device_node *np = dev->of_node;
  4933. msmsdcc_dt_get_cd_wp_gpio(dev, pdata);
  4934. pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
  4935. if (!pin_data) {
  4936. dev_err(dev, "No memory for pin_data\n");
  4937. ret = -ENOMEM;
  4938. goto err;
  4939. }
  4940. cnt = of_gpio_count(np);
  4941. if (cnt > 0) {
  4942. pin_data->is_gpio = true;
  4943. pin_data->gpio_data = devm_kzalloc(dev,
  4944. sizeof(struct msm_mmc_gpio_data), GFP_KERNEL);
  4945. if (!pin_data->gpio_data) {
  4946. dev_err(dev, "No memory for gpio_data\n");
  4947. ret = -ENOMEM;
  4948. goto err;
  4949. }
  4950. pin_data->gpio_data->size = cnt;
  4951. pin_data->gpio_data->gpio = devm_kzalloc(dev,
  4952. cnt * sizeof(struct msm_mmc_gpio), GFP_KERNEL);
  4953. if (!pin_data->gpio_data->gpio) {
  4954. dev_err(dev, "No memory for gpio\n");
  4955. ret = -ENOMEM;
  4956. goto err;
  4957. }
  4958. for (i = 0; i < cnt; i++) {
  4959. const char *name = NULL;
  4960. char result[32];
  4961. pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
  4962. of_property_read_string_index(np,
  4963. "qcom,gpio-names", i, &name);
  4964. snprintf(result, 32, "%s-%s",
  4965. dev_name(dev), name ? name : "?");
  4966. pin_data->gpio_data->gpio[i].name = result;
  4967. dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
  4968. pin_data->gpio_data->gpio[i].name,
  4969. pin_data->gpio_data->gpio[i].no);
  4970. }
  4971. } else {
  4972. pin_data->pad_data = devm_kzalloc(dev,
  4973. sizeof(struct msm_mmc_pad_data), GFP_KERNEL);
  4974. if (!pin_data->pad_data) {
  4975. dev_err(dev, "No memory for pin_data->pad_data\n");
  4976. ret = -ENOMEM;
  4977. goto err;
  4978. }
  4979. of_property_read_u32(np, "cell-index", &id);
  4980. ret = msmsdcc_dt_get_pad_pull_info(dev, id,
  4981. &pin_data->pad_data->pull);
  4982. if (ret)
  4983. goto err;
  4984. ret = msmsdcc_dt_get_pad_drv_info(dev, id,
  4985. &pin_data->pad_data->drv);
  4986. if (ret)
  4987. goto err;
  4988. }
  4989. pdata->pin_data = pin_data;
  4990. err:
  4991. if (ret)
  4992. dev_err(dev, "%s failed with err %d\n", __func__, ret);
  4993. return ret;
  4994. }
  4995. #define MAX_PROP_SIZE 32
  4996. static int msmsdcc_dt_parse_vreg_info(struct device *dev,
  4997. struct msm_mmc_reg_data **vreg_data, const char *vreg_name)
  4998. {
  4999. int len, ret = 0;
  5000. const __be32 *prop;
  5001. char prop_name[MAX_PROP_SIZE];
  5002. struct msm_mmc_reg_data *vreg;
  5003. struct device_node *np = dev->of_node;
  5004. snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
  5005. if (of_parse_phandle(np, prop_name, 0)) {
  5006. vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
  5007. if (!vreg) {
  5008. dev_err(dev, "No memory for vreg: %s\n", vreg_name);
  5009. ret = -ENOMEM;
  5010. goto err;
  5011. }
  5012. vreg->name = vreg_name;
  5013. snprintf(prop_name, MAX_PROP_SIZE,
  5014. "qcom,%s-always-on", vreg_name);
  5015. if (of_get_property(np, prop_name, NULL))
  5016. vreg->always_on = true;
  5017. snprintf(prop_name, MAX_PROP_SIZE,
  5018. "qcom,%s-lpm-sup", vreg_name);
  5019. if (of_get_property(np, prop_name, NULL))
  5020. vreg->lpm_sup = true;
  5021. snprintf(prop_name, MAX_PROP_SIZE,
  5022. "qcom,%s-voltage-level", vreg_name);
  5023. prop = of_get_property(np, prop_name, &len);
  5024. if (!prop || (len != (2 * sizeof(__be32)))) {
  5025. dev_warn(dev, "%s %s property\n",
  5026. prop ? "invalid format" : "no", prop_name);
  5027. } else {
  5028. vreg->low_vol_level = be32_to_cpup(&prop[0]);
  5029. vreg->high_vol_level = be32_to_cpup(&prop[1]);
  5030. }
  5031. snprintf(prop_name, MAX_PROP_SIZE,
  5032. "qcom,%s-current-level", vreg_name);
  5033. prop = of_get_property(np, prop_name, &len);
  5034. if (!prop || (len != (2 * sizeof(__be32)))) {
  5035. dev_warn(dev, "%s %s property\n",
  5036. prop ? "invalid format" : "no", prop_name);
  5037. } else {
  5038. vreg->lpm_uA = be32_to_cpup(&prop[0]);
  5039. vreg->hpm_uA = be32_to_cpup(&prop[1]);
  5040. }
  5041. *vreg_data = vreg;
  5042. dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
  5043. vreg->name, vreg->always_on ? "always_on," : "",
  5044. vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
  5045. vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
  5046. }
  5047. err:
  5048. return ret;
  5049. }
  5050. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || defined(CONFIG_BCM4354)
  5051. int brcm_wifi_status_register(
  5052. void (*callback)(int card_present, void *dev_id), void *dev_id, void *mmc_host);
  5053. unsigned int brcm_wifi_status(struct device *dev);
  5054. #endif /* defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || defined(CONFIG_BCM4354)*/
  5055. static struct mmc_platform_data *msmsdcc_populate_pdata(struct device *dev)
  5056. {
  5057. int i, ret;
  5058. struct mmc_platform_data *pdata;
  5059. struct device_node *np = dev->of_node;
  5060. u32 bus_width = 0, current_limit = 0;
  5061. u32 *clk_table = NULL, *sup_voltages = NULL;
  5062. int clk_table_len, sup_volt_len, len;
  5063. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || defined(CONFIG_BCM4354)
  5064. int vendor_type = 0;
  5065. #endif /* defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || defined(CONFIG_BCM4354)*/
  5066. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  5067. if (!pdata) {
  5068. dev_err(dev, "could not allocate memory for platform data\n");
  5069. goto err;
  5070. }
  5071. of_property_read_u32(np, "qcom,bus-width", &bus_width);
  5072. if (bus_width == 8) {
  5073. pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
  5074. } else if (bus_width == 4) {
  5075. pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
  5076. } else {
  5077. dev_notice(dev, "Invalid bus width, default to 1 bit mode\n");
  5078. pdata->mmc_bus_width = 0;
  5079. }
  5080. ret = msmsdcc_dt_get_array(dev, "qcom,sup-voltages",
  5081. &sup_voltages, &sup_volt_len, 0);
  5082. if (!ret) {
  5083. for (i = 0; i < sup_volt_len; i += 2) {
  5084. u32 mask;
  5085. mask = mmc_vddrange_to_ocrmask(sup_voltages[i],
  5086. sup_voltages[i + 1]);
  5087. if (!mask)
  5088. dev_err(dev, "Invalide voltage range %d\n", i);
  5089. pdata->ocr_mask |= mask;
  5090. }
  5091. dev_dbg(dev, "OCR mask=0x%x\n", pdata->ocr_mask);
  5092. }
  5093. ret = msmsdcc_dt_get_array(dev, "qcom,clk-rates",
  5094. &clk_table, &clk_table_len, 0);
  5095. if (!ret) {
  5096. pdata->sup_clk_table = clk_table;
  5097. pdata->sup_clk_cnt = clk_table_len;
  5098. }
  5099. pdata->vreg_data = devm_kzalloc(dev,
  5100. sizeof(struct msm_mmc_slot_reg_data), GFP_KERNEL);
  5101. if (!pdata->vreg_data) {
  5102. dev_err(dev, "could not allocate memory for vreg_data\n");
  5103. goto err;
  5104. }
  5105. if (msmsdcc_dt_parse_vreg_info(dev,
  5106. &pdata->vreg_data->vdd_data, "vdd"))
  5107. goto err;
  5108. if (msmsdcc_dt_parse_vreg_info(dev,
  5109. &pdata->vreg_data->vdd_io_data, "vdd-io"))
  5110. goto err;
  5111. if (msmsdcc_dt_parse_gpio_info(dev, pdata))
  5112. goto err;
  5113. len = of_property_count_strings(np, "qcom,bus-speed-mode");
  5114. for (i = 0; i < len; i++) {
  5115. const char *name = NULL;
  5116. of_property_read_string_index(np,
  5117. "qcom,bus-speed-mode", i, &name);
  5118. if (!name)
  5119. continue;
  5120. if (!strncmp(name, "SDR12", sizeof("SDR12")))
  5121. pdata->uhs_caps |= MMC_CAP_UHS_SDR12;
  5122. else if (!strncmp(name, "SDR25", sizeof("SDR25")))
  5123. pdata->uhs_caps |= MMC_CAP_UHS_SDR25;
  5124. else if (!strncmp(name, "SDR50", sizeof("SDR50")))
  5125. pdata->uhs_caps |= MMC_CAP_UHS_SDR50;
  5126. else if (!strncmp(name, "DDR50", sizeof("DDR50")))
  5127. pdata->uhs_caps |= MMC_CAP_UHS_DDR50;
  5128. else if (!strncmp(name, "SDR104", sizeof("SDR104")))
  5129. pdata->uhs_caps |= MMC_CAP_UHS_SDR104;
  5130. else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
  5131. pdata->uhs_caps2 |= MMC_CAP2_HS200_1_8V_SDR;
  5132. else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
  5133. pdata->uhs_caps2 |= MMC_CAP2_HS200_1_2V_SDR;
  5134. else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
  5135. pdata->uhs_caps |= MMC_CAP_1_8V_DDR
  5136. | MMC_CAP_UHS_DDR50;
  5137. else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
  5138. pdata->uhs_caps |= MMC_CAP_1_2V_DDR
  5139. | MMC_CAP_UHS_DDR50;
  5140. }
  5141. of_property_read_u32(np, "qcom,current-limit", &current_limit);
  5142. if (current_limit == 800)
  5143. pdata->uhs_caps |= MMC_CAP_MAX_CURRENT_800;
  5144. else if (current_limit == 600)
  5145. pdata->uhs_caps |= MMC_CAP_MAX_CURRENT_600;
  5146. else if (current_limit == 400)
  5147. pdata->uhs_caps |= MMC_CAP_MAX_CURRENT_400;
  5148. else if (current_limit == 200)
  5149. pdata->uhs_caps |= MMC_CAP_MAX_CURRENT_200;
  5150. if (of_get_property(np, "qcom,xpc", NULL))
  5151. pdata->xpc_cap = true;
  5152. if (of_get_property(np, "qcom,nonremovable", NULL))
  5153. pdata->nonremovable = true;
  5154. if (of_get_property(np, "qcom,disable-cmd23", NULL))
  5155. pdata->disable_cmd23 = true;
  5156. of_property_read_u32(np, "qcom,dat1-mpm-int",
  5157. &pdata->mpm_sdiowakeup_int);
  5158. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || defined(CONFIG_BCM4354)
  5159. printk(KERN_INFO"%s: before parsing vendor_type\n", __func__);
  5160. if (of_get_property(np, "status-cb", &vendor_type)) {
  5161. printk(KERN_INFO"%s: vendor_type=%d \n", __func__, vendor_type);
  5162. //if(1 == vendor_type) {
  5163. pdata->status = brcm_wifi_status;
  5164. pdata->register_status_notify = brcm_wifi_status_register;
  5165. pdata->built_in = 1;
  5166. //}
  5167. }
  5168. #endif /* defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || defined(CONFIG_BCM4354) */
  5169. return pdata;
  5170. err:
  5171. return NULL;
  5172. }
  5173. static int
  5174. msmsdcc_probe(struct platform_device *pdev)
  5175. {
  5176. struct mmc_platform_data *plat;
  5177. struct msmsdcc_host *host;
  5178. struct mmc_host *mmc;
  5179. unsigned long flags;
  5180. struct resource *core_irqres = NULL;
  5181. struct resource *bam_irqres = NULL;
  5182. struct resource *core_memres = NULL;
  5183. struct resource *dml_memres = NULL;
  5184. struct resource *bam_memres = NULL;
  5185. struct resource *dmares = NULL;
  5186. struct resource *dma_crci_res = NULL;
  5187. int ret = 0;
  5188. if (pdev->dev.of_node) {
  5189. plat = msmsdcc_populate_pdata(&pdev->dev);
  5190. of_property_read_u32((&pdev->dev)->of_node,
  5191. "cell-index", &pdev->id);
  5192. } else {
  5193. plat = pdev->dev.platform_data;
  5194. }
  5195. /* must have platform data */
  5196. if (!plat) {
  5197. pr_err("%s: Platform data not available\n", __func__);
  5198. ret = -EINVAL;
  5199. goto out;
  5200. }
  5201. if (disable_slots & (1 << (pdev->id - 1))) {
  5202. pr_info("%s: Slot %d disabled\n", __func__, pdev->id);
  5203. return -ENODEV;
  5204. }
  5205. if (pdev->id < 1 || pdev->id > 5)
  5206. return -EINVAL;
  5207. if (plat->is_sdio_al_client && !plat->sdiowakeup_irq) {
  5208. pr_err("%s: No wakeup IRQ for sdio_al client\n", __func__);
  5209. return -EINVAL;
  5210. }
  5211. if (pdev->resource == NULL || pdev->num_resources < 2) {
  5212. pr_err("%s: Invalid resource\n", __func__);
  5213. return -ENXIO;
  5214. }
  5215. core_memres = platform_get_resource_byname(pdev,
  5216. IORESOURCE_MEM, "core_mem");
  5217. bam_memres = platform_get_resource_byname(pdev,
  5218. IORESOURCE_MEM, "bam_mem");
  5219. dml_memres = platform_get_resource_byname(pdev,
  5220. IORESOURCE_MEM, "dml_mem");
  5221. core_irqres = platform_get_resource_byname(pdev,
  5222. IORESOURCE_IRQ, "core_irq");
  5223. bam_irqres = platform_get_resource_byname(pdev,
  5224. IORESOURCE_IRQ, "bam_irq");
  5225. dmares = platform_get_resource_byname(pdev,
  5226. IORESOURCE_DMA, "dma_chnl");
  5227. dma_crci_res = platform_get_resource_byname(pdev,
  5228. IORESOURCE_DMA, "dma_crci");
  5229. if (!core_irqres || !core_memres) {
  5230. pr_err("%s: Invalid sdcc core resource\n", __func__);
  5231. return -ENXIO;
  5232. }
  5233. /*
  5234. * Both BAM and DML memory resource should be preset.
  5235. * BAM IRQ resource should also be present.
  5236. */
  5237. if ((bam_memres && !dml_memres) ||
  5238. (!bam_memres && dml_memres) ||
  5239. ((bam_memres && dml_memres) && !bam_irqres)) {
  5240. pr_err("%s: Invalid sdcc BAM/DML resource\n", __func__);
  5241. return -ENXIO;
  5242. }
  5243. /*
  5244. * Setup our host structure
  5245. */
  5246. mmc = mmc_alloc_host(sizeof(struct msmsdcc_host), &pdev->dev);
  5247. if (!mmc) {
  5248. ret = -ENOMEM;
  5249. goto out;
  5250. }
  5251. host = mmc_priv(mmc);
  5252. host->pdev = pdev;
  5253. host->plat = plat;
  5254. host->mmc = mmc;
  5255. host->curr.cmd = NULL;
  5256. if (!plat->disable_bam && bam_memres && dml_memres && bam_irqres)
  5257. set_hw_caps(host, MSMSDCC_SPS_BAM_SUP);
  5258. else if (dmares)
  5259. set_hw_caps(host, MSMSDCC_DMA_SUP);
  5260. host->base = ioremap(core_memres->start,
  5261. resource_size(core_memres));
  5262. if (!host->base) {
  5263. ret = -ENOMEM;
  5264. goto host_free;
  5265. }
  5266. host->core_irqres = core_irqres;
  5267. host->bam_irqres = bam_irqres;
  5268. host->core_memres = core_memres;
  5269. host->dml_memres = dml_memres;
  5270. host->bam_memres = bam_memres;
  5271. host->dmares = dmares;
  5272. host->dma_crci_res = dma_crci_res;
  5273. spin_lock_init(&host->lock);
  5274. mutex_init(&host->clk_mutex);
  5275. #ifdef CONFIG_MMC_EMBEDDED_SDIO
  5276. if (plat->embedded_sdio)
  5277. mmc_set_embedded_sdio_data(mmc,
  5278. &plat->embedded_sdio->cis,
  5279. &plat->embedded_sdio->cccr,
  5280. plat->embedded_sdio->funcs,
  5281. plat->embedded_sdio->num_funcs);
  5282. #endif
  5283. tasklet_init(&host->dma_tlet, msmsdcc_dma_complete_tlet,
  5284. (unsigned long)host);
  5285. tasklet_init(&host->sps.tlet, msmsdcc_sps_complete_tlet,
  5286. (unsigned long)host);
  5287. if (is_dma_mode(host)) {
  5288. /* Setup DMA */
  5289. ret = msmsdcc_init_dma(host);
  5290. if (ret)
  5291. goto ioremap_free;
  5292. } else {
  5293. host->dma.channel = -1;
  5294. host->dma.crci = -1;
  5295. }
  5296. /*
  5297. * Setup SDCC bus voter clock.
  5298. */
  5299. host->bus_clk = clk_get(&pdev->dev, "bus_clk");
  5300. if (!IS_ERR_OR_NULL(host->bus_clk)) {
  5301. /* Vote for max. clk rate for max. performance */
  5302. ret = clk_set_rate(host->bus_clk, MSMSDCC_BUS_VOTE_MAX_RATE);
  5303. if (ret)
  5304. goto bus_clk_put;
  5305. ret = clk_prepare_enable(host->bus_clk);
  5306. if (ret)
  5307. goto bus_clk_put;
  5308. host->bus_clk_rate = MSMSDCC_BUS_VOTE_MAX_RATE;
  5309. }
  5310. /*
  5311. * Setup main peripheral bus clock
  5312. */
  5313. host->pclk = clk_get(&pdev->dev, "iface_clk");
  5314. if (!IS_ERR(host->pclk)) {
  5315. ret = clk_prepare_enable(host->pclk);
  5316. if (ret)
  5317. goto pclk_put;
  5318. host->pclk_rate = clk_get_rate(host->pclk);
  5319. }
  5320. /*
  5321. * Setup SDC MMC clock
  5322. */
  5323. host->clk = clk_get(&pdev->dev, "core_clk");
  5324. if (IS_ERR(host->clk)) {
  5325. ret = PTR_ERR(host->clk);
  5326. goto pclk_disable;
  5327. }
  5328. ret = clk_set_rate(host->clk, msmsdcc_get_min_sup_clk_rate(host));
  5329. if (ret) {
  5330. pr_err("%s: Clock rate set failed (%d)\n", __func__, ret);
  5331. goto clk_put;
  5332. }
  5333. ret = clk_prepare_enable(host->clk);
  5334. if (ret)
  5335. goto clk_put;
  5336. host->clk_rate = clk_get_rate(host->clk);
  5337. if (!host->clk_rate)
  5338. dev_err(&pdev->dev, "Failed to read MCLK\n");
  5339. set_default_hw_caps(host);
  5340. host->saved_tuning_phase = INVALID_TUNING_PHASE;
  5341. /*
  5342. * Set the register write delay according to min. clock frequency
  5343. * supported and update later when the host->clk_rate changes.
  5344. */
  5345. host->reg_write_delay =
  5346. (1 + ((3 * USEC_PER_SEC) /
  5347. msmsdcc_get_min_sup_clk_rate(host)));
  5348. atomic_set(&host->clks_on, 1);
  5349. ret = msmsdcc_msm_bus_register(host);
  5350. if (ret)
  5351. goto clk_disable;
  5352. if (host->msm_bus_vote.client_handle)
  5353. INIT_DELAYED_WORK(&host->msm_bus_vote.vote_work,
  5354. msmsdcc_msm_bus_work);
  5355. msmsdcc_msm_bus_cancel_work_and_set_vote(host, &mmc->ios);
  5356. /* Disable SDHCi mode if supported */
  5357. if (is_sdhci_supported(host))
  5358. writel_relaxed(0, (host->base + MCI_CORE_HC_MODE));
  5359. /* Apply Hard reset to SDCC to put it in power on default state */
  5360. msmsdcc_hard_reset(host);
  5361. #define MSM_MMC_DEFAULT_CPUDMA_LATENCY 200 /* usecs */
  5362. /* pm qos request to prevent apps idle power collapse */
  5363. if (host->plat->cpu_dma_latency)
  5364. host->cpu_dma_latency = host->plat->cpu_dma_latency;
  5365. else
  5366. host->cpu_dma_latency = MSM_MMC_DEFAULT_CPUDMA_LATENCY;
  5367. pm_qos_add_request(&host->pm_qos_req_dma,
  5368. PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
  5369. ret = msmsdcc_vreg_init(host, true);
  5370. if (ret) {
  5371. pr_err("%s: msmsdcc_vreg_init() failed (%d)\n", __func__, ret);
  5372. goto pm_qos_remove;
  5373. }
  5374. /* Clocks has to be running before accessing SPS/DML HW blocks */
  5375. if (is_sps_mode(host)) {
  5376. /* Initialize SPS */
  5377. ret = msmsdcc_sps_init(host);
  5378. if (ret)
  5379. goto vreg_deinit;
  5380. /* Initialize DML */
  5381. ret = msmsdcc_dml_init(host);
  5382. if (ret)
  5383. goto sps_exit;
  5384. }
  5385. mmc_dev(mmc)->dma_mask = &dma_mask;
  5386. /*
  5387. * Setup MMC host structure
  5388. */
  5389. mmc->ops = &msmsdcc_ops;
  5390. mmc->f_min = msmsdcc_get_min_sup_clk_rate(host);
  5391. mmc->f_max = msmsdcc_get_max_sup_clk_rate(host);
  5392. mmc->ocr_avail = plat->ocr_mask;
  5393. mmc->clkgate_delay = MSM_MMC_CLK_GATE_DELAY;
  5394. mmc->pm_caps |= MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
  5395. mmc->caps |= plat->mmc_bus_width;
  5396. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
  5397. mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
  5398. /*
  5399. * If we send the CMD23 before multi block write/read command
  5400. * then we need not to send CMD12 at the end of the transfer.
  5401. * If we don't send the CMD12 then only way to detect the PROG_DONE
  5402. * status is to use the AUTO_PROG_DONE status provided by SDCC4
  5403. * controller. So let's enable the CMD23 for SDCC4 only.
  5404. */
  5405. if (!plat->disable_cmd23 && is_auto_prog_done(host))
  5406. mmc->caps |= MMC_CAP_CMD23;
  5407. mmc->caps |= plat->uhs_caps;
  5408. mmc->caps2 |= plat->uhs_caps2;
  5409. /*
  5410. * XPC controls the maximum current in the default speed mode of SDXC
  5411. * card. XPC=0 means 100mA (max.) but speed class is not supported.
  5412. * XPC=1 means 150mA (max.) and speed class is supported.
  5413. */
  5414. if (plat->xpc_cap)
  5415. mmc->caps |= (MMC_CAP_SET_XPC_330 | MMC_CAP_SET_XPC_300 |
  5416. MMC_CAP_SET_XPC_180);
  5417. mmc->caps2 |= MMC_CAP2_PACKED_WR;
  5418. mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
  5419. mmc->caps2 |= (MMC_CAP2_BOOTPART_NOACC | MMC_CAP2_DETECT_ON_ERR);
  5420. mmc->caps2 |= MMC_CAP2_SANITIZE;
  5421. mmc->caps2 |= MMC_CAP2_CACHE_CTRL;
  5422. mmc->caps2 |= MMC_CAP2_POWEROFF_NOTIFY;
  5423. mmc->caps2 |= MMC_CAP2_STOP_REQUEST;
  5424. mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
  5425. if (plat->nonremovable)
  5426. mmc->caps |= MMC_CAP_NONREMOVABLE;
  5427. mmc->caps |= MMC_CAP_SDIO_IRQ;
  5428. if (plat->is_sdio_al_client)
  5429. mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY;
  5430. if (plat->built_in)
  5431. mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY | MMC_PM_KEEP_POWER;
  5432. mmc->max_segs = msmsdcc_get_nr_sg(host);
  5433. mmc->max_blk_size = MMC_MAX_BLK_SIZE;
  5434. mmc->max_blk_count = MMC_MAX_BLK_CNT;
  5435. mmc->max_req_size = MMC_MAX_REQ_SIZE;
  5436. mmc->max_seg_size = mmc->max_req_size;
  5437. writel_relaxed(0, host->base + MMCIMASK0);
  5438. writel_relaxed(MCI_CLEAR_STATIC_MASK, host->base + MMCICLEAR);
  5439. msmsdcc_sync_reg_wr(host);
  5440. writel_relaxed(MCI_IRQENABLE, host->base + MMCIMASK0);
  5441. mb();
  5442. host->mci_irqenable = MCI_IRQENABLE;
  5443. ret = request_irq(core_irqres->start, msmsdcc_irq, IRQF_SHARED,
  5444. DRIVER_NAME " (cmd)", host);
  5445. if (ret)
  5446. goto dml_exit;
  5447. ret = request_irq(core_irqres->start, msmsdcc_pio_irq, IRQF_SHARED,
  5448. DRIVER_NAME " (pio)", host);
  5449. if (ret)
  5450. goto irq_free;
  5451. /*
  5452. * Enable SDCC IRQ only when host is powered on. Otherwise, this
  5453. * IRQ is un-necessarily being monitored by MPM (Modem power
  5454. * management block) during idle-power collapse. The MPM will be
  5455. * configured to monitor the DATA1 GPIO line with level-low trigger
  5456. * and thus depending on the GPIO status, it prevents TCXO shutdown
  5457. * during idle-power collapse.
  5458. */
  5459. disable_irq(core_irqres->start);
  5460. host->sdcc_irq_disabled = 1;
  5461. if (!plat->sdiowakeup_irq) {
  5462. /* Check if registered as IORESOURCE_IRQ */
  5463. plat->sdiowakeup_irq =
  5464. platform_get_irq_byname(pdev, "sdiowakeup_irq");
  5465. if (plat->sdiowakeup_irq < 0)
  5466. plat->sdiowakeup_irq = 0;
  5467. }
  5468. if (plat->sdiowakeup_irq) {
  5469. ret = request_irq(plat->sdiowakeup_irq,
  5470. msmsdcc_platform_sdiowakeup_irq,
  5471. IRQF_SHARED | IRQF_TRIGGER_LOW,
  5472. DRIVER_NAME "sdiowakeup", host);
  5473. if (ret) {
  5474. pr_err("Unable to get sdio wakeup IRQ %d (%d)\n",
  5475. plat->sdiowakeup_irq, ret);
  5476. goto pio_irq_free;
  5477. } else {
  5478. spin_lock_irqsave(&host->lock, flags);
  5479. if (!host->sdio_wakeupirq_disabled) {
  5480. disable_irq_nosync(plat->sdiowakeup_irq);
  5481. host->sdio_wakeupirq_disabled = 1;
  5482. }
  5483. spin_unlock_irqrestore(&host->lock, flags);
  5484. }
  5485. }
  5486. if (plat->sdiowakeup_irq || plat->mpm_sdiowakeup_int) {
  5487. wake_lock_init(&host->sdio_wlock, WAKE_LOCK_SUSPEND,
  5488. mmc_hostname(mmc));
  5489. }
  5490. wake_lock_init(&host->sdio_suspend_wlock, WAKE_LOCK_SUSPEND,
  5491. mmc_hostname(mmc));
  5492. /*
  5493. * Setup card detect change
  5494. */
  5495. if (!plat->status_gpio)
  5496. plat->status_gpio = -ENOENT;
  5497. if (!plat->wpswitch_gpio)
  5498. plat->wpswitch_gpio = -ENOENT;
  5499. if (plat->status || gpio_is_valid(plat->status_gpio)) {
  5500. if (plat->status) {
  5501. host->oldstat = plat->status(mmc_dev(host->mmc));
  5502. } else {
  5503. msmsdcc_enable_status_gpio(host);
  5504. host->oldstat = msmsdcc_slot_status(host);
  5505. }
  5506. host->eject = !host->oldstat;
  5507. }
  5508. if (plat->status_irq) {
  5509. ret = request_threaded_irq(plat->status_irq, NULL,
  5510. msmsdcc_platform_status_irq,
  5511. plat->irq_flags,
  5512. DRIVER_NAME " (slot)",
  5513. host);
  5514. if (ret) {
  5515. pr_err("Unable to get slot IRQ %d (%d)\n",
  5516. plat->status_irq, ret);
  5517. goto sdiowakeup_irq_free;
  5518. }
  5519. } else if (plat->register_status_notify) {
  5520. plat->register_status_notify(msmsdcc_status_notify_cb, host, host->mmc);
  5521. } else if (!plat->status)
  5522. pr_err("%s: No card detect facilities available\n",
  5523. mmc_hostname(mmc));
  5524. mmc_set_drvdata(pdev, mmc);
  5525. ret = pm_runtime_set_active(&(pdev)->dev);
  5526. if (ret < 0)
  5527. pr_info("%s: %s: failed with error %d", mmc_hostname(mmc),
  5528. __func__, ret);
  5529. /*
  5530. * There is no notion of suspend/resume for SD/MMC/SDIO
  5531. * cards. So host can be suspended/resumed with out
  5532. * worrying about its children.
  5533. */
  5534. pm_suspend_ignore_children(&(pdev)->dev, true);
  5535. /*
  5536. * MMC/SD/SDIO bus suspend/resume operations are defined
  5537. * only for the slots that will be used for non-removable
  5538. * media or for all slots when CONFIG_MMC_UNSAFE_RESUME is
  5539. * defined. Otherwise, they simply become card removal and
  5540. * insertion events during suspend and resume respectively.
  5541. * Hence, enable run-time PM only for slots for which bus
  5542. * suspend/resume operations are defined.
  5543. */
  5544. #ifdef CONFIG_MMC_UNSAFE_RESUME
  5545. /*
  5546. * If this capability is set, MMC core will enable/disable host
  5547. * for every claim/release operation on a host. We use this
  5548. * notification to increment/decrement runtime pm usage count.
  5549. */
  5550. pm_runtime_enable(&(pdev)->dev);
  5551. #else
  5552. if (mmc->caps & MMC_CAP_NONREMOVABLE) {
  5553. pm_runtime_enable(&(pdev)->dev);
  5554. }
  5555. #endif
  5556. host->idle_tout = MSM_MMC_DEFAULT_IDLE_TIMEOUT;
  5557. setup_timer(&host->req_tout_timer, msmsdcc_req_tout_timer_hdlr,
  5558. (unsigned long)host);
  5559. mmc_add_host(mmc);
  5560. mmc->clk_scaling.up_threshold = 35;
  5561. mmc->clk_scaling.down_threshold = 5;
  5562. mmc->clk_scaling.polling_delay_ms = 100;
  5563. mmc->caps2 |= MMC_CAP2_CLK_SCALE;
  5564. pr_info("%s: Qualcomm MSM SDCC-core %pr %pr,%d dma %d dmacrcri %d\n",
  5565. mmc_hostname(mmc), core_memres, core_irqres,
  5566. (unsigned int) plat->status_irq, host->dma.channel,
  5567. host->dma.crci);
  5568. pr_info("%s: Controller capabilities: 0x%.8x\n",
  5569. mmc_hostname(mmc), host->hw_caps);
  5570. pr_info("%s: 8 bit data mode %s\n", mmc_hostname(mmc),
  5571. (mmc->caps & MMC_CAP_8_BIT_DATA ? "enabled" : "disabled"));
  5572. pr_info("%s: 4 bit data mode %s\n", mmc_hostname(mmc),
  5573. (mmc->caps & MMC_CAP_4_BIT_DATA ? "enabled" : "disabled"));
  5574. pr_info("%s: polling status mode %s\n", mmc_hostname(mmc),
  5575. (mmc->caps & MMC_CAP_NEEDS_POLL ? "enabled" : "disabled"));
  5576. pr_info("%s: MMC clock %u -> %u Hz, PCLK %u Hz\n",
  5577. mmc_hostname(mmc), msmsdcc_get_min_sup_clk_rate(host),
  5578. msmsdcc_get_max_sup_clk_rate(host), host->pclk_rate);
  5579. pr_info("%s: Slot eject status = %d\n", mmc_hostname(mmc),
  5580. host->eject);
  5581. pr_info("%s: Power save feature enable = %d\n",
  5582. mmc_hostname(mmc), msmsdcc_pwrsave);
  5583. if (is_dma_mode(host) && host->dma.channel != -1
  5584. && host->dma.crci != -1) {
  5585. pr_info("%s: DM non-cached buffer at %p, dma_addr: %pa\n",
  5586. mmc_hostname(mmc), host->dma.nc, &host->dma.nc_busaddr);
  5587. pr_info("%s: DM cmd busaddr: %pa, cmdptr busaddr: %pa\n",
  5588. mmc_hostname(mmc), &host->dma.cmd_busaddr,
  5589. &host->dma.cmdptr_busaddr);
  5590. } else if (is_sps_mode(host)) {
  5591. pr_info("%s: SPS-BAM data transfer mode available\n",
  5592. mmc_hostname(mmc));
  5593. } else
  5594. pr_info("%s: PIO transfer enabled\n", mmc_hostname(mmc));
  5595. #if defined(CONFIG_DEBUG_FS)
  5596. msmsdcc_dbg_createhost(host);
  5597. #endif
  5598. host->max_bus_bw.show = show_sdcc_to_mem_max_bus_bw;
  5599. host->max_bus_bw.store = store_sdcc_to_mem_max_bus_bw;
  5600. sysfs_attr_init(&host->max_bus_bw.attr);
  5601. host->max_bus_bw.attr.name = "max_bus_bw";
  5602. host->max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
  5603. ret = device_create_file(&pdev->dev, &host->max_bus_bw);
  5604. if (ret)
  5605. goto platform_irq_free;
  5606. if (!plat->status_irq) {
  5607. host->polling.show = show_polling;
  5608. host->polling.store = store_polling;
  5609. sysfs_attr_init(&host->polling.attr);
  5610. host->polling.attr.name = "polling";
  5611. host->polling.attr.mode = S_IRUGO | S_IWUSR;
  5612. ret = device_create_file(&pdev->dev, &host->polling);
  5613. if (ret)
  5614. goto remove_max_bus_bw_file;
  5615. }
  5616. host->idle_timeout.show = show_idle_timeout;
  5617. host->idle_timeout.store = store_idle_timeout;
  5618. sysfs_attr_init(&host->idle_timeout.attr);
  5619. host->idle_timeout.attr.name = "idle_timeout";
  5620. host->idle_timeout.attr.mode = S_IRUGO | S_IWUSR;
  5621. ret = device_create_file(&pdev->dev, &host->idle_timeout);
  5622. if (ret)
  5623. goto remove_polling_file;
  5624. if (!is_auto_cmd19(host))
  5625. goto add_auto_cmd21_atrr;
  5626. /* Sysfs entry for AUTO CMD19 control */
  5627. host->auto_cmd19_attr.show = show_enable_auto_cmd19;
  5628. host->auto_cmd19_attr.store = store_enable_auto_cmd19;
  5629. sysfs_attr_init(&host->auto_cmd19_attr.attr);
  5630. host->auto_cmd19_attr.attr.name = "enable_auto_cmd19";
  5631. host->auto_cmd19_attr.attr.mode = S_IRUGO | S_IWUSR;
  5632. ret = device_create_file(&pdev->dev, &host->auto_cmd19_attr);
  5633. if (ret)
  5634. goto remove_idle_timeout_file;
  5635. add_auto_cmd21_atrr:
  5636. if (!is_auto_cmd21(host))
  5637. goto exit;
  5638. /* Sysfs entry for AUTO CMD21 control */
  5639. host->auto_cmd21_attr.show = show_enable_auto_cmd21;
  5640. host->auto_cmd21_attr.store = store_enable_auto_cmd21;
  5641. sysfs_attr_init(&host->auto_cmd21_attr.attr);
  5642. host->auto_cmd21_attr.attr.name = "enable_auto_cmd21";
  5643. host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR;
  5644. ret = device_create_file(&pdev->dev, &host->auto_cmd21_attr);
  5645. if (ret)
  5646. goto remove_auto_cmd19_attr_file;
  5647. exit:
  5648. return 0;
  5649. remove_auto_cmd19_attr_file:
  5650. if (is_auto_cmd19(host))
  5651. device_remove_file(&pdev->dev, &host->auto_cmd19_attr);
  5652. remove_idle_timeout_file:
  5653. device_remove_file(&pdev->dev, &host->idle_timeout);
  5654. remove_polling_file:
  5655. if (!plat->status_irq)
  5656. device_remove_file(&pdev->dev, &host->polling);
  5657. remove_max_bus_bw_file:
  5658. device_remove_file(&pdev->dev, &host->max_bus_bw);
  5659. platform_irq_free:
  5660. del_timer_sync(&host->req_tout_timer);
  5661. pm_runtime_disable(&(pdev)->dev);
  5662. pm_runtime_set_suspended(&(pdev)->dev);
  5663. if (plat->status_irq)
  5664. free_irq(plat->status_irq, host);
  5665. msmsdcc_disable_status_gpio(host);
  5666. sdiowakeup_irq_free:
  5667. if (plat->sdiowakeup_irq || plat->mpm_sdiowakeup_int)
  5668. wake_lock_destroy(&host->sdio_wlock);
  5669. wake_lock_destroy(&host->sdio_suspend_wlock);
  5670. if (plat->sdiowakeup_irq)
  5671. free_irq(plat->sdiowakeup_irq, host);
  5672. pio_irq_free:
  5673. free_irq(core_irqres->start, host);
  5674. irq_free:
  5675. free_irq(core_irqres->start, host);
  5676. dml_exit:
  5677. if (is_sps_mode(host))
  5678. msmsdcc_dml_exit(host);
  5679. sps_exit:
  5680. if (is_sps_mode(host))
  5681. msmsdcc_sps_exit(host);
  5682. vreg_deinit:
  5683. msmsdcc_vreg_init(host, false);
  5684. pm_qos_remove:
  5685. if (host->cpu_dma_latency)
  5686. pm_qos_remove_request(&host->pm_qos_req_dma);
  5687. msmsdcc_msm_bus_cancel_work_and_set_vote(host, NULL);
  5688. msmsdcc_msm_bus_unregister(host);
  5689. clk_disable:
  5690. clk_disable_unprepare(host->clk);
  5691. clk_put:
  5692. clk_put(host->clk);
  5693. pclk_disable:
  5694. if (!IS_ERR(host->pclk))
  5695. clk_disable_unprepare(host->pclk);
  5696. pclk_put:
  5697. if (!IS_ERR(host->pclk))
  5698. clk_put(host->pclk);
  5699. if (!IS_ERR_OR_NULL(host->bus_clk))
  5700. clk_disable_unprepare(host->bus_clk);
  5701. bus_clk_put:
  5702. if (!IS_ERR_OR_NULL(host->bus_clk))
  5703. clk_put(host->bus_clk);
  5704. if (is_dma_mode(host)) {
  5705. if (host->dmares)
  5706. dma_free_coherent(NULL,
  5707. sizeof(struct msmsdcc_nc_dmadata),
  5708. host->dma.nc, host->dma.nc_busaddr);
  5709. }
  5710. ioremap_free:
  5711. iounmap(host->base);
  5712. host_free:
  5713. mmc_free_host(mmc);
  5714. out:
  5715. return ret;
  5716. }
  5717. #ifdef CONFIG_DEBUG_FS
  5718. static void msmsdcc_remove_debugfs(struct msmsdcc_host *host)
  5719. {
  5720. debugfs_remove_recursive(host->debugfs_host_dir);
  5721. host->debugfs_host_dir = NULL;
  5722. }
  5723. #else
  5724. static void msmsdcc_remove_debugfs(struct msmsdcc_host *host) {}
  5725. #endif
  5726. static int msmsdcc_remove(struct platform_device *pdev)
  5727. {
  5728. struct mmc_host *mmc = mmc_get_drvdata(pdev);
  5729. struct mmc_platform_data *plat;
  5730. struct msmsdcc_host *host;
  5731. if (!mmc)
  5732. return -ENXIO;
  5733. if (pm_runtime_suspended(&(pdev)->dev))
  5734. pm_runtime_resume(&(pdev)->dev);
  5735. host = mmc_priv(mmc);
  5736. DBG(host, "Removing SDCC device = %d\n", pdev->id);
  5737. plat = host->plat;
  5738. if (is_auto_cmd19(host))
  5739. device_remove_file(&pdev->dev, &host->auto_cmd19_attr);
  5740. if (is_auto_cmd21(host))
  5741. device_remove_file(&pdev->dev, &host->auto_cmd21_attr);
  5742. device_remove_file(&pdev->dev, &host->max_bus_bw);
  5743. if (!plat->status_irq)
  5744. device_remove_file(&pdev->dev, &host->polling);
  5745. device_remove_file(&pdev->dev, &host->idle_timeout);
  5746. msmsdcc_remove_debugfs(host);
  5747. del_timer_sync(&host->req_tout_timer);
  5748. tasklet_kill(&host->dma_tlet);
  5749. tasklet_kill(&host->sps.tlet);
  5750. mmc_remove_host(mmc);
  5751. if (plat->status_irq)
  5752. free_irq(plat->status_irq, host);
  5753. msmsdcc_disable_status_gpio(host);
  5754. wake_lock_destroy(&host->sdio_suspend_wlock);
  5755. if (plat->sdiowakeup_irq) {
  5756. irq_set_irq_wake(plat->sdiowakeup_irq, 0);
  5757. free_irq(plat->sdiowakeup_irq, host);
  5758. }
  5759. if (plat->sdiowakeup_irq || plat->mpm_sdiowakeup_int)
  5760. wake_lock_destroy(&host->sdio_wlock);
  5761. free_irq(host->core_irqres->start, host);
  5762. free_irq(host->core_irqres->start, host);
  5763. clk_put(host->clk);
  5764. if (!IS_ERR(host->pclk))
  5765. clk_put(host->pclk);
  5766. if (!IS_ERR_OR_NULL(host->bus_clk))
  5767. clk_put(host->bus_clk);
  5768. if (host->cpu_dma_latency)
  5769. pm_qos_remove_request(&host->pm_qos_req_dma);
  5770. if (host->msm_bus_vote.client_handle) {
  5771. msmsdcc_msm_bus_cancel_work_and_set_vote(host, NULL);
  5772. msmsdcc_msm_bus_unregister(host);
  5773. }
  5774. msmsdcc_vreg_init(host, false);
  5775. if (is_dma_mode(host)) {
  5776. if (host->dmares)
  5777. dma_free_coherent(NULL,
  5778. sizeof(struct msmsdcc_nc_dmadata),
  5779. host->dma.nc, host->dma.nc_busaddr);
  5780. }
  5781. if (is_sps_mode(host)) {
  5782. msmsdcc_dml_exit(host);
  5783. msmsdcc_sps_exit(host);
  5784. }
  5785. iounmap(host->base);
  5786. mmc_free_host(mmc);
  5787. pm_runtime_disable(&(pdev)->dev);
  5788. pm_runtime_set_suspended(&(pdev)->dev);
  5789. return 0;
  5790. }
  5791. #ifdef CONFIG_MSM_SDIO_AL
  5792. int msmsdcc_sdio_al_lpm(struct mmc_host *mmc, bool enable)
  5793. {
  5794. struct msmsdcc_host *host = mmc_priv(mmc);
  5795. unsigned long flags;
  5796. int rc = 0;
  5797. mutex_lock(&host->clk_mutex);
  5798. spin_lock_irqsave(&host->lock, flags);
  5799. pr_debug("%s: %sabling LPM\n", mmc_hostname(mmc),
  5800. enable ? "En" : "Dis");
  5801. if (enable) {
  5802. if (!host->sdcc_irq_disabled) {
  5803. writel_relaxed(0, host->base + MMCIMASK0);
  5804. disable_irq_nosync(host->core_irqres->start);
  5805. host->sdcc_irq_disabled = 1;
  5806. }
  5807. rc = msmsdcc_setup_clocks(host, false);
  5808. if (rc)
  5809. goto out;
  5810. if (host->plat->sdio_lpm_gpio_setup &&
  5811. !host->sdio_gpio_lpm) {
  5812. spin_unlock_irqrestore(&host->lock, flags);
  5813. host->plat->sdio_lpm_gpio_setup(mmc_dev(mmc), 0);
  5814. spin_lock_irqsave(&host->lock, flags);
  5815. host->sdio_gpio_lpm = 1;
  5816. }
  5817. if (host->sdio_wakeupirq_disabled) {
  5818. msmsdcc_enable_irq_wake(host);
  5819. enable_irq(host->plat->sdiowakeup_irq);
  5820. host->sdio_wakeupirq_disabled = 0;
  5821. }
  5822. } else {
  5823. rc = msmsdcc_setup_clocks(host, true);
  5824. if (rc)
  5825. goto out;
  5826. if (!host->sdio_wakeupirq_disabled) {
  5827. disable_irq_nosync(host->plat->sdiowakeup_irq);
  5828. host->sdio_wakeupirq_disabled = 1;
  5829. msmsdcc_disable_irq_wake(host);
  5830. }
  5831. if (host->plat->sdio_lpm_gpio_setup &&
  5832. host->sdio_gpio_lpm) {
  5833. spin_unlock_irqrestore(&host->lock, flags);
  5834. host->plat->sdio_lpm_gpio_setup(mmc_dev(mmc), 1);
  5835. spin_lock_irqsave(&host->lock, flags);
  5836. host->sdio_gpio_lpm = 0;
  5837. }
  5838. if (host->sdcc_irq_disabled && atomic_read(&host->clks_on)) {
  5839. writel_relaxed(host->mci_irqenable,
  5840. host->base + MMCIMASK0);
  5841. mb();
  5842. enable_irq(host->core_irqres->start);
  5843. host->sdcc_irq_disabled = 0;
  5844. }
  5845. }
  5846. out:
  5847. spin_unlock_irqrestore(&host->lock, flags);
  5848. mutex_unlock(&host->clk_mutex);
  5849. return rc;
  5850. }
  5851. #else
  5852. int msmsdcc_sdio_al_lpm(struct mmc_host *mmc, bool enable)
  5853. {
  5854. return 0;
  5855. }
  5856. #endif
  5857. #ifdef CONFIG_PM
  5858. #ifdef CONFIG_MMC_CLKGATE
  5859. static inline void msmsdcc_gate_clock(struct msmsdcc_host *host)
  5860. {
  5861. struct mmc_host *mmc = host->mmc;
  5862. unsigned long flags;
  5863. mmc_host_clk_hold(mmc);
  5864. spin_lock_irqsave(&mmc->clk_lock, flags);
  5865. mmc->clk_old = mmc->ios.clock;
  5866. mmc->ios.clock = 0;
  5867. mmc->clk_gated = true;
  5868. spin_unlock_irqrestore(&mmc->clk_lock, flags);
  5869. mmc_set_ios(mmc);
  5870. mmc_host_clk_release(mmc);
  5871. }
  5872. static inline void msmsdcc_ungate_clock(struct msmsdcc_host *host)
  5873. {
  5874. struct mmc_host *mmc = host->mmc;
  5875. mmc_host_clk_hold(mmc);
  5876. mmc->ios.clock = host->clk_rate;
  5877. mmc_set_ios(mmc);
  5878. mmc_host_clk_release(mmc);
  5879. }
  5880. #else
  5881. static inline void msmsdcc_gate_clock(struct msmsdcc_host *host)
  5882. {
  5883. struct mmc_host *mmc = host->mmc;
  5884. mmc->ios.clock = 0;
  5885. mmc_set_ios(mmc);
  5886. }
  5887. static inline void msmsdcc_ungate_clock(struct msmsdcc_host *host)
  5888. {
  5889. struct mmc_host *mmc = host->mmc;
  5890. mmc->ios.clock = host->clk_rate;
  5891. mmc_set_ios(mmc);
  5892. }
  5893. #endif
  5894. #ifdef CONFIG_DEBUG_FS
  5895. static void msmsdcc_print_pm_stats(struct msmsdcc_host *host, ktime_t start,
  5896. const char *func, int err)
  5897. {
  5898. ktime_t diff;
  5899. if (host->print_pm_stats && !err) {
  5900. diff = ktime_sub(ktime_get(), start);
  5901. pr_info("%s: %s: Completed in %llu usec\n",
  5902. mmc_hostname(host->mmc), func, (u64)ktime_to_us(diff));
  5903. }
  5904. }
  5905. #else
  5906. static void msmsdcc_print_pm_stats(struct msmsdcc_host *host, ktime_t start,
  5907. const char *func, int err) {}
  5908. #endif
  5909. static int
  5910. msmsdcc_runtime_suspend(struct device *dev)
  5911. {
  5912. struct mmc_host *mmc = dev_get_drvdata(dev);
  5913. struct msmsdcc_host *host = mmc_priv(mmc);
  5914. int rc = 0;
  5915. unsigned long flags;
  5916. ktime_t start = ktime_get();
  5917. if (host->plat->is_sdio_al_client) {
  5918. rc = 0;
  5919. goto out;
  5920. }
  5921. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || defined(CONFIG_BCM4354)
  5922. if (host->pdev->id == 2) {
  5923. host->mmc->pm_flags |= MMC_PM_KEEP_POWER;
  5924. printk(KERN_INFO "%s: Enter WIFI suspend\n", __func__);
  5925. }
  5926. #endif
  5927. pr_debug("%s: %s: start\n", mmc_hostname(mmc), __func__);
  5928. if (mmc) {
  5929. host->sdcc_suspending = 1;
  5930. mmc->suspend_task = current;
  5931. /*
  5932. * MMC core thinks that host is disabled by now since
  5933. * runtime suspend is scheduled after msmsdcc_disable()
  5934. * is called. Thus, MMC core will try to enable the host
  5935. * while suspending it. This results in a synchronous
  5936. * runtime resume request while in runtime suspending
  5937. * context and hence inorder to complete this resume
  5938. * requet, it will wait for suspend to be complete,
  5939. * but runtime suspend also can not proceed further
  5940. * until the host is resumed. Thus, it leads to a hang.
  5941. * Hence, increase the pm usage count before suspending
  5942. * the host so that any resume requests after this will
  5943. * simple become pm usage counter increment operations.
  5944. */
  5945. pm_runtime_get_noresume(dev);
  5946. /* If there is pending detect work abort runtime suspend */
  5947. if (unlikely(work_busy(&mmc->detect.work)))
  5948. rc = -EAGAIN;
  5949. else
  5950. rc = mmc_suspend_host(mmc);
  5951. pm_runtime_put_noidle(dev);
  5952. if (!rc) {
  5953. spin_lock_irqsave(&host->lock, flags);
  5954. host->sdcc_suspended = true;
  5955. spin_unlock_irqrestore(&host->lock, flags);
  5956. if (mmc->card && mmc_card_sdio(mmc->card) &&
  5957. mmc->ios.clock) {
  5958. /*
  5959. * If SDIO function driver doesn't want
  5960. * to power off the card, atleast turn off
  5961. * clocks to allow deep sleep (TCXO shutdown).
  5962. */
  5963. msmsdcc_gate_clock(host);
  5964. }
  5965. }
  5966. host->sdcc_suspending = 0;
  5967. mmc->suspend_task = NULL;
  5968. if (rc && wake_lock_active(&host->sdio_suspend_wlock))
  5969. wake_unlock(&host->sdio_suspend_wlock);
  5970. }
  5971. pr_debug("%s: %s: ends with err=%d\n", mmc_hostname(mmc), __func__, rc);
  5972. out:
  5973. /*
  5974. * Remove the vote immediately only if clocks are off in which
  5975. * case we might have queued work to remove vote but it may not
  5976. * be completed before runtime suspend or system suspend.
  5977. */
  5978. if (!atomic_read(&host->clks_on))
  5979. msmsdcc_msm_bus_cancel_work_and_set_vote(host, NULL);
  5980. msmsdcc_print_pm_stats(host, start, __func__, rc);
  5981. return rc;
  5982. }
  5983. static int
  5984. msmsdcc_runtime_resume(struct device *dev)
  5985. {
  5986. struct mmc_host *mmc = dev_get_drvdata(dev);
  5987. struct msmsdcc_host *host = mmc_priv(mmc);
  5988. unsigned long flags;
  5989. ktime_t start = ktime_get();
  5990. if (host->plat->is_sdio_al_client)
  5991. goto out;
  5992. pr_debug("%s: %s: start\n", mmc_hostname(mmc), __func__);
  5993. if (mmc) {
  5994. if (mmc->card && mmc_card_sdio(mmc->card) &&
  5995. mmc_card_keep_power(mmc)) {
  5996. msmsdcc_ungate_clock(host);
  5997. }
  5998. mmc_resume_host(mmc);
  5999. /*
  6000. * FIXME: Clearing of flags must be handled in clients
  6001. * resume handler.
  6002. */
  6003. spin_lock_irqsave(&host->lock, flags);
  6004. mmc->pm_flags = 0;
  6005. host->sdcc_suspended = false;
  6006. spin_unlock_irqrestore(&host->lock, flags);
  6007. /*
  6008. * After resuming the host wait for sometime so that
  6009. * the SDIO work will be processed.
  6010. */
  6011. if (mmc->card && mmc_card_sdio(mmc->card)) {
  6012. if ((host->plat->mpm_sdiowakeup_int ||
  6013. host->plat->sdiowakeup_irq) &&
  6014. wake_lock_active(&host->sdio_wlock))
  6015. wake_lock_timeout(&host->sdio_wlock, 1);
  6016. }
  6017. wake_unlock(&host->sdio_suspend_wlock);
  6018. }
  6019. host->pending_resume = false;
  6020. pr_debug("%s: %s: end\n", mmc_hostname(mmc), __func__);
  6021. out:
  6022. msmsdcc_print_pm_stats(host, start, __func__, 0);
  6023. return 0;
  6024. }
  6025. static int msmsdcc_runtime_idle(struct device *dev)
  6026. {
  6027. struct mmc_host *mmc = dev_get_drvdata(dev);
  6028. struct msmsdcc_host *host = mmc_priv(mmc);
  6029. if (host->plat->is_sdio_al_client)
  6030. return 0;
  6031. /* Idle timeout is not configurable for now */
  6032. pm_schedule_suspend(dev, host->idle_tout);
  6033. return -EAGAIN;
  6034. }
  6035. static int msmsdcc_pm_suspend(struct device *dev)
  6036. {
  6037. struct mmc_host *mmc = dev_get_drvdata(dev);
  6038. struct msmsdcc_host *host = mmc_priv(mmc);
  6039. int rc = 0;
  6040. ktime_t start = ktime_get();
  6041. if (host->plat->is_sdio_al_client) {
  6042. rc = 0;
  6043. goto out;
  6044. }
  6045. if (host->plat->status_irq) {
  6046. disable_irq(host->plat->status_irq);
  6047. msmsdcc_disable_status_gpio(host);
  6048. }
  6049. /*
  6050. * If system comes out of suspend, msmsdcc_pm_resume() sets the
  6051. * host->pending_resume flag if the SDCC wasn't runtime suspended.
  6052. * Now if the system again goes to suspend without any SDCC activity
  6053. * then host->pending_resume flag will remain set which may cause
  6054. * the SDCC resume to happen first and then suspend.
  6055. * To avoid this unnecessary resume/suspend, make sure that
  6056. * pending_resume flag is cleared before calling the
  6057. * msmsdcc_runtime_suspend().
  6058. */
  6059. if (!pm_runtime_suspended(dev) && !host->pending_resume)
  6060. rc = msmsdcc_runtime_suspend(dev);
  6061. out:
  6062. /* This flag must not be set if system is entering into suspend */
  6063. host->pending_resume = false;
  6064. msmsdcc_print_pm_stats(host, start, __func__, rc);
  6065. return rc;
  6066. }
  6067. static int msmsdcc_suspend_noirq(struct device *dev)
  6068. {
  6069. struct mmc_host *mmc = dev_get_drvdata(dev);
  6070. struct msmsdcc_host *host = mmc_priv(mmc);
  6071. int rc = 0;
  6072. /*
  6073. * After platform suspend there may be active request
  6074. * which might have enabled clocks. For example, in SDIO
  6075. * case, ksdioirq thread might have scheduled after sdcc
  6076. * suspend but before system freeze. In that case abort
  6077. * suspend and retry instead of keeping the clocks on
  6078. * during suspend and not allowing TCXO.
  6079. */
  6080. if (atomic_read(&host->clks_on) && !host->plat->is_sdio_al_client) {
  6081. pr_warn("%s: clocks are on after suspend, aborting system "
  6082. "suspend\n", mmc_hostname(mmc));
  6083. rc = -EAGAIN;
  6084. }
  6085. return rc;
  6086. }
  6087. static int msmsdcc_pm_resume(struct device *dev)
  6088. {
  6089. struct mmc_host *mmc = dev_get_drvdata(dev);
  6090. struct msmsdcc_host *host = mmc_priv(mmc);
  6091. int rc = 0;
  6092. ktime_t start = ktime_get();
  6093. if (host->plat->is_sdio_al_client) {
  6094. rc = 0;
  6095. goto out;
  6096. }
  6097. if (mmc->card && mmc_card_sdio(mmc->card))
  6098. rc = msmsdcc_runtime_resume(dev);
  6099. /*
  6100. * As runtime PM is enabled before calling the device's platform resume
  6101. * callback, we use the pm_runtime_suspended API to know if SDCC is
  6102. * really runtime suspended or not and set the pending_resume flag only
  6103. * if its not runtime suspended.
  6104. */
  6105. else if (!pm_runtime_suspended(dev))
  6106. host->pending_resume = true;
  6107. if (host->plat->status_irq) {
  6108. msmsdcc_enable_status_gpio(host);
  6109. msmsdcc_check_status((unsigned long)host);
  6110. enable_irq(host->plat->status_irq);
  6111. }
  6112. out:
  6113. msmsdcc_print_pm_stats(host, start, __func__, rc);
  6114. return rc;
  6115. }
  6116. #else
  6117. static int msmsdcc_runtime_suspend(struct device *dev)
  6118. {
  6119. return 0;
  6120. }
  6121. static int msmsdcc_runtime_idle(struct device *dev)
  6122. {
  6123. return 0;
  6124. }
  6125. static int msmsdcc_pm_suspend(struct device *dev)
  6126. {
  6127. return 0;
  6128. }
  6129. static int msmsdcc_pm_resume(struct device *dev)
  6130. {
  6131. return 0;
  6132. }
  6133. static int msmsdcc_suspend_noirq(struct device *dev)
  6134. {
  6135. return 0;
  6136. }
  6137. static int msmsdcc_runtime_resume(struct device *dev)
  6138. {
  6139. return 0;
  6140. }
  6141. #endif
  6142. static const struct dev_pm_ops msmsdcc_dev_pm_ops = {
  6143. .runtime_suspend = msmsdcc_runtime_suspend,
  6144. .runtime_resume = msmsdcc_runtime_resume,
  6145. .runtime_idle = msmsdcc_runtime_idle,
  6146. .suspend = msmsdcc_pm_suspend,
  6147. .resume = msmsdcc_pm_resume,
  6148. .suspend_noirq = msmsdcc_suspend_noirq,
  6149. };
  6150. static const struct of_device_id msmsdcc_dt_match[] = {
  6151. {.compatible = "qcom,msm-sdcc"},
  6152. { /* sentinel */ }
  6153. };
  6154. MODULE_DEVICE_TABLE(of, msmsdcc_dt_match);
  6155. static struct platform_driver msmsdcc_driver = {
  6156. .probe = msmsdcc_probe,
  6157. .remove = msmsdcc_remove,
  6158. .driver = {
  6159. .name = "msm_sdcc",
  6160. .pm = &msmsdcc_dev_pm_ops,
  6161. .of_match_table = msmsdcc_dt_match,
  6162. },
  6163. };
  6164. static int __init msmsdcc_init(void)
  6165. {
  6166. #if defined(CONFIG_DEBUG_FS)
  6167. int ret = 0;
  6168. ret = msmsdcc_dbg_init();
  6169. if (ret) {
  6170. pr_err("Failed to create debug fs dir \n");
  6171. return ret;
  6172. }
  6173. #endif
  6174. return platform_driver_register(&msmsdcc_driver);
  6175. }
  6176. static void __exit msmsdcc_exit(void)
  6177. {
  6178. platform_driver_unregister(&msmsdcc_driver);
  6179. #if defined(CONFIG_DEBUG_FS)
  6180. debugfs_remove(debugfs_dir);
  6181. #endif
  6182. }
  6183. module_init(msmsdcc_init);
  6184. module_exit(msmsdcc_exit);
  6185. MODULE_DESCRIPTION("Qualcomm Multimedia Card Interface driver");
  6186. MODULE_LICENSE("GPL");
  6187. #if defined(CONFIG_DEBUG_FS)
  6188. static int msmsdcc_dbg_idle_tout_get(void *data, u64 *val)
  6189. {
  6190. struct msmsdcc_host *host = data;
  6191. *val = host->idle_tout / 1000L;
  6192. return 0;
  6193. }
  6194. static int msmsdcc_dbg_idle_tout_set(void *data, u64 val)
  6195. {
  6196. struct msmsdcc_host *host = data;
  6197. unsigned long flags;
  6198. spin_lock_irqsave(&host->lock, flags);
  6199. host->idle_tout = (u32)val * 1000;
  6200. spin_unlock_irqrestore(&host->lock, flags);
  6201. return 0;
  6202. }
  6203. DEFINE_SIMPLE_ATTRIBUTE(msmsdcc_dbg_idle_tout_ops,
  6204. msmsdcc_dbg_idle_tout_get,
  6205. msmsdcc_dbg_idle_tout_set,
  6206. "%llu\n");
  6207. static int msmsdcc_dbg_pio_mode_get(void *data, u64 *val)
  6208. {
  6209. struct msmsdcc_host *host = data;
  6210. *val = (u64) host->enforce_pio_mode;
  6211. return 0;
  6212. }
  6213. static int msmsdcc_dbg_pio_mode_set(void *data, u64 val)
  6214. {
  6215. struct msmsdcc_host *host = data;
  6216. unsigned long flags;
  6217. spin_lock_irqsave(&host->lock, flags);
  6218. host->enforce_pio_mode = !!val;
  6219. spin_unlock_irqrestore(&host->lock, flags);
  6220. return 0;
  6221. }
  6222. DEFINE_SIMPLE_ATTRIBUTE(msmsdcc_dbg_pio_mode_ops,
  6223. msmsdcc_dbg_pio_mode_get,
  6224. msmsdcc_dbg_pio_mode_set,
  6225. "%llu\n");
  6226. static int msmsdcc_dbg_pm_stats_get(void *data, u64 *val)
  6227. {
  6228. struct msmsdcc_host *host = data;
  6229. *val = !!host->print_pm_stats;
  6230. return 0;
  6231. }
  6232. static int msmsdcc_dbg_pm_stats_set(void *data, u64 val)
  6233. {
  6234. struct msmsdcc_host *host = data;
  6235. unsigned long flags;
  6236. spin_lock_irqsave(&host->lock, flags);
  6237. host->print_pm_stats = !!val;
  6238. spin_unlock_irqrestore(&host->lock, flags);
  6239. return 0;
  6240. }
  6241. DEFINE_SIMPLE_ATTRIBUTE(msmsdcc_dbg_pm_stats_ops,
  6242. msmsdcc_dbg_pm_stats_get,
  6243. msmsdcc_dbg_pm_stats_set,
  6244. "%llu\n");
  6245. static void msmsdcc_dbg_createhost(struct msmsdcc_host *host)
  6246. {
  6247. int err = 0;
  6248. if (!debugfs_dir)
  6249. return;
  6250. host->debugfs_host_dir = debugfs_create_dir(
  6251. mmc_hostname(host->mmc), debugfs_dir);
  6252. if (IS_ERR(host->debugfs_host_dir)) {
  6253. err = PTR_ERR(host->debugfs_host_dir);
  6254. host->debugfs_host_dir = NULL;
  6255. pr_err("%s: Failed to create debugfs dir for host with err=%d\n",
  6256. mmc_hostname(host->mmc), err);
  6257. return;
  6258. }
  6259. host->debugfs_idle_tout = debugfs_create_file("idle_tout",
  6260. S_IRUSR | S_IWUSR, host->debugfs_host_dir, host,
  6261. &msmsdcc_dbg_idle_tout_ops);
  6262. if (IS_ERR(host->debugfs_idle_tout)) {
  6263. err = PTR_ERR(host->debugfs_idle_tout);
  6264. host->debugfs_idle_tout = NULL;
  6265. pr_err("%s: Failed to create idle_tout debugfs entry with err=%d\n",
  6266. mmc_hostname(host->mmc), err);
  6267. }
  6268. host->debugfs_pio_mode = debugfs_create_file("pio_mode",
  6269. S_IRUSR | S_IWUSR, host->debugfs_host_dir, host,
  6270. &msmsdcc_dbg_pio_mode_ops);
  6271. if (IS_ERR(host->debugfs_pio_mode)) {
  6272. err = PTR_ERR(host->debugfs_pio_mode);
  6273. host->debugfs_pio_mode = NULL;
  6274. pr_err("%s: Failed to create pio_mode debugfs entry with err=%d\n",
  6275. mmc_hostname(host->mmc), err);
  6276. }
  6277. host->debugfs_pm_stats = debugfs_create_file("pm_stats",
  6278. S_IRUSR | S_IWUSR, host->debugfs_host_dir, host,
  6279. &msmsdcc_dbg_pm_stats_ops);
  6280. if (IS_ERR(host->debugfs_pm_stats)) {
  6281. err = PTR_ERR(host->debugfs_pm_stats);
  6282. host->debugfs_pm_stats = NULL;
  6283. pr_err("%s: Failed to create pm_stats debugfs entry with err=%d\n",
  6284. mmc_hostname(host->mmc), err);
  6285. }
  6286. }
  6287. static int __init msmsdcc_dbg_init(void)
  6288. {
  6289. int err;
  6290. debugfs_dir = debugfs_create_dir("msm_sdcc", 0);
  6291. if (IS_ERR(debugfs_dir)) {
  6292. err = PTR_ERR(debugfs_dir);
  6293. debugfs_dir = NULL;
  6294. return err;
  6295. }
  6296. return 0;
  6297. }
  6298. #endif