hcd.c 163 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * hcd.c - DesignWare HS OTG Controller host-mode routines
  4. *
  5. * Copyright (C) 2004-2013 Synopsys, Inc.
  6. */
  7. /*
  8. * This file contains the core HCD code, and implements the Linux hc_driver
  9. * API
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/delay.h>
  18. #include <linux/io.h>
  19. #include <linux/slab.h>
  20. #include <linux/usb.h>
  21. #include <linux/usb/hcd.h>
  22. #include <linux/usb/ch11.h>
  23. #include <linux/usb/of.h>
  24. #include "core.h"
  25. #include "hcd.h"
  26. /*
  27. * =========================================================================
  28. * Host Core Layer Functions
  29. * =========================================================================
  30. */
  31. /**
  32. * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
  33. * used in both device and host modes
  34. *
  35. * @hsotg: Programming view of the DWC_otg controller
  36. */
  37. static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
  38. {
  39. u32 intmsk;
  40. /* Clear any pending OTG Interrupts */
  41. dwc2_writel(hsotg, 0xffffffff, GOTGINT);
  42. /* Clear any pending interrupts */
  43. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  44. /* Enable the interrupts in the GINTMSK */
  45. intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
  46. if (!hsotg->params.host_dma)
  47. intmsk |= GINTSTS_RXFLVL;
  48. if (!hsotg->params.external_id_pin_ctl)
  49. intmsk |= GINTSTS_CONIDSTSCHNG;
  50. intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
  51. GINTSTS_SESSREQINT;
  52. if (dwc2_is_device_mode(hsotg) && hsotg->params.lpm)
  53. intmsk |= GINTSTS_LPMTRANRCVD;
  54. dwc2_writel(hsotg, intmsk, GINTMSK);
  55. }
  56. static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
  57. {
  58. u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
  59. switch (hsotg->hw_params.arch) {
  60. case GHWCFG2_EXT_DMA_ARCH:
  61. dev_err(hsotg->dev, "External DMA Mode not supported\n");
  62. return -EINVAL;
  63. case GHWCFG2_INT_DMA_ARCH:
  64. dev_dbg(hsotg->dev, "Internal DMA Mode\n");
  65. if (hsotg->params.ahbcfg != -1) {
  66. ahbcfg &= GAHBCFG_CTRL_MASK;
  67. ahbcfg |= hsotg->params.ahbcfg &
  68. ~GAHBCFG_CTRL_MASK;
  69. }
  70. break;
  71. case GHWCFG2_SLAVE_ONLY_ARCH:
  72. default:
  73. dev_dbg(hsotg->dev, "Slave Only Mode\n");
  74. break;
  75. }
  76. if (hsotg->params.host_dma)
  77. ahbcfg |= GAHBCFG_DMA_EN;
  78. else
  79. hsotg->params.dma_desc_enable = false;
  80. dwc2_writel(hsotg, ahbcfg, GAHBCFG);
  81. return 0;
  82. }
  83. static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
  84. {
  85. u32 usbcfg;
  86. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  87. usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
  88. switch (hsotg->hw_params.op_mode) {
  89. case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
  90. if (hsotg->params.otg_caps.hnp_support &&
  91. hsotg->params.otg_caps.srp_support)
  92. usbcfg |= GUSBCFG_HNPCAP;
  93. fallthrough;
  94. case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
  95. case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
  96. case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
  97. if (hsotg->params.otg_caps.srp_support)
  98. usbcfg |= GUSBCFG_SRPCAP;
  99. break;
  100. case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
  101. case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
  102. case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
  103. default:
  104. break;
  105. }
  106. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  107. }
  108. static int dwc2_vbus_supply_init(struct dwc2_hsotg *hsotg)
  109. {
  110. if (hsotg->vbus_supply)
  111. return regulator_enable(hsotg->vbus_supply);
  112. return 0;
  113. }
  114. static int dwc2_vbus_supply_exit(struct dwc2_hsotg *hsotg)
  115. {
  116. if (hsotg->vbus_supply)
  117. return regulator_disable(hsotg->vbus_supply);
  118. return 0;
  119. }
  120. /**
  121. * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
  122. *
  123. * @hsotg: Programming view of DWC_otg controller
  124. */
  125. static void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
  126. {
  127. u32 intmsk;
  128. dev_dbg(hsotg->dev, "%s()\n", __func__);
  129. /* Disable all interrupts */
  130. dwc2_writel(hsotg, 0, GINTMSK);
  131. dwc2_writel(hsotg, 0, HAINTMSK);
  132. /* Enable the common interrupts */
  133. dwc2_enable_common_interrupts(hsotg);
  134. /* Enable host mode interrupts without disturbing common interrupts */
  135. intmsk = dwc2_readl(hsotg, GINTMSK);
  136. intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
  137. dwc2_writel(hsotg, intmsk, GINTMSK);
  138. }
  139. /**
  140. * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
  141. *
  142. * @hsotg: Programming view of DWC_otg controller
  143. */
  144. static void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
  145. {
  146. u32 intmsk = dwc2_readl(hsotg, GINTMSK);
  147. /* Disable host mode interrupts without disturbing common interrupts */
  148. intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
  149. GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
  150. dwc2_writel(hsotg, intmsk, GINTMSK);
  151. }
  152. /*
  153. * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
  154. * For system that have a total fifo depth that is smaller than the default
  155. * RX + TX fifo size.
  156. *
  157. * @hsotg: Programming view of DWC_otg controller
  158. */
  159. static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
  160. {
  161. struct dwc2_core_params *params = &hsotg->params;
  162. struct dwc2_hw_params *hw = &hsotg->hw_params;
  163. u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
  164. total_fifo_size = hw->total_fifo_size;
  165. rxfsiz = params->host_rx_fifo_size;
  166. nptxfsiz = params->host_nperio_tx_fifo_size;
  167. ptxfsiz = params->host_perio_tx_fifo_size;
  168. /*
  169. * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
  170. * allocation with support for high bandwidth endpoints. Synopsys
  171. * defines MPS(Max Packet size) for a periodic EP=1024, and for
  172. * non-periodic as 512.
  173. */
  174. if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
  175. /*
  176. * For Buffer DMA mode/Scatter Gather DMA mode
  177. * 2 * ((Largest Packet size / 4) + 1 + 1) + n
  178. * with n = number of host channel.
  179. * 2 * ((1024/4) + 2) = 516
  180. */
  181. rxfsiz = 516 + hw->host_channels;
  182. /*
  183. * min non-periodic tx fifo depth
  184. * 2 * (largest non-periodic USB packet used / 4)
  185. * 2 * (512/4) = 256
  186. */
  187. nptxfsiz = 256;
  188. /*
  189. * min periodic tx fifo depth
  190. * (largest packet size*MC)/4
  191. * (1024 * 3)/4 = 768
  192. */
  193. ptxfsiz = 768;
  194. params->host_rx_fifo_size = rxfsiz;
  195. params->host_nperio_tx_fifo_size = nptxfsiz;
  196. params->host_perio_tx_fifo_size = ptxfsiz;
  197. }
  198. /*
  199. * If the summation of RX, NPTX and PTX fifo sizes is still
  200. * bigger than the total_fifo_size, then we have a problem.
  201. *
  202. * We won't be able to allocate as many endpoints. Right now,
  203. * we're just printing an error message, but ideally this FIFO
  204. * allocation algorithm would be improved in the future.
  205. *
  206. * FIXME improve this FIFO allocation algorithm.
  207. */
  208. if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
  209. dev_err(hsotg->dev, "invalid fifo sizes\n");
  210. }
  211. static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
  212. {
  213. struct dwc2_core_params *params = &hsotg->params;
  214. u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
  215. if (!params->enable_dynamic_fifo)
  216. return;
  217. dwc2_calculate_dynamic_fifo(hsotg);
  218. /* Rx FIFO */
  219. grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
  220. dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
  221. grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
  222. grxfsiz |= params->host_rx_fifo_size <<
  223. GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
  224. dwc2_writel(hsotg, grxfsiz, GRXFSIZ);
  225. dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
  226. dwc2_readl(hsotg, GRXFSIZ));
  227. /* Non-periodic Tx FIFO */
  228. dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
  229. dwc2_readl(hsotg, GNPTXFSIZ));
  230. nptxfsiz = params->host_nperio_tx_fifo_size <<
  231. FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
  232. nptxfsiz |= params->host_rx_fifo_size <<
  233. FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
  234. dwc2_writel(hsotg, nptxfsiz, GNPTXFSIZ);
  235. dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
  236. dwc2_readl(hsotg, GNPTXFSIZ));
  237. /* Periodic Tx FIFO */
  238. dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
  239. dwc2_readl(hsotg, HPTXFSIZ));
  240. hptxfsiz = params->host_perio_tx_fifo_size <<
  241. FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
  242. hptxfsiz |= (params->host_rx_fifo_size +
  243. params->host_nperio_tx_fifo_size) <<
  244. FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
  245. dwc2_writel(hsotg, hptxfsiz, HPTXFSIZ);
  246. dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
  247. dwc2_readl(hsotg, HPTXFSIZ));
  248. if (hsotg->params.en_multiple_tx_fifo &&
  249. hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_91a) {
  250. /*
  251. * This feature was implemented in 2.91a version
  252. * Global DFIFOCFG calculation for Host mode -
  253. * include RxFIFO, NPTXFIFO and HPTXFIFO
  254. */
  255. dfifocfg = dwc2_readl(hsotg, GDFIFOCFG);
  256. dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
  257. dfifocfg |= (params->host_rx_fifo_size +
  258. params->host_nperio_tx_fifo_size +
  259. params->host_perio_tx_fifo_size) <<
  260. GDFIFOCFG_EPINFOBASE_SHIFT &
  261. GDFIFOCFG_EPINFOBASE_MASK;
  262. dwc2_writel(hsotg, dfifocfg, GDFIFOCFG);
  263. }
  264. }
  265. /**
  266. * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
  267. * the HFIR register according to PHY type and speed
  268. *
  269. * @hsotg: Programming view of DWC_otg controller
  270. *
  271. * NOTE: The caller can modify the value of the HFIR register only after the
  272. * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
  273. * has been set
  274. */
  275. u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
  276. {
  277. u32 usbcfg;
  278. u32 hprt0;
  279. int clock = 60; /* default value */
  280. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  281. hprt0 = dwc2_readl(hsotg, HPRT0);
  282. if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
  283. !(usbcfg & GUSBCFG_PHYIF16))
  284. clock = 60;
  285. if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
  286. GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
  287. clock = 48;
  288. if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  289. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
  290. clock = 30;
  291. if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  292. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
  293. clock = 60;
  294. if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  295. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
  296. clock = 48;
  297. if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
  298. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
  299. clock = 48;
  300. if ((usbcfg & GUSBCFG_PHYSEL) &&
  301. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
  302. clock = 48;
  303. if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
  304. /* High speed case */
  305. return 125 * clock - 1;
  306. /* FS/LS case */
  307. return 1000 * clock - 1;
  308. }
  309. /**
  310. * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
  311. * buffer
  312. *
  313. * @hsotg: Programming view of DWC_otg controller
  314. * @dest: Destination buffer for the packet
  315. * @bytes: Number of bytes to copy to the destination
  316. */
  317. void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
  318. {
  319. u32 *data_buf = (u32 *)dest;
  320. int word_count = (bytes + 3) / 4;
  321. int i;
  322. /*
  323. * Todo: Account for the case where dest is not dword aligned. This
  324. * requires reading data from the FIFO into a u32 temp buffer, then
  325. * moving it into the data buffer.
  326. */
  327. dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
  328. for (i = 0; i < word_count; i++, data_buf++)
  329. *data_buf = dwc2_readl(hsotg, HCFIFO(0));
  330. }
  331. /**
  332. * dwc2_dump_channel_info() - Prints the state of a host channel
  333. *
  334. * @hsotg: Programming view of DWC_otg controller
  335. * @chan: Pointer to the channel to dump
  336. *
  337. * Must be called with interrupt disabled and spinlock held
  338. *
  339. * NOTE: This function will be removed once the peripheral controller code
  340. * is integrated and the driver is stable
  341. */
  342. static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
  343. struct dwc2_host_chan *chan)
  344. {
  345. #ifdef VERBOSE_DEBUG
  346. int num_channels = hsotg->params.host_channels;
  347. struct dwc2_qh *qh;
  348. u32 hcchar;
  349. u32 hcsplt;
  350. u32 hctsiz;
  351. u32 hc_dma;
  352. int i;
  353. if (!chan)
  354. return;
  355. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  356. hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
  357. hctsiz = dwc2_readl(hsotg, HCTSIZ(chan->hc_num));
  358. hc_dma = dwc2_readl(hsotg, HCDMA(chan->hc_num));
  359. dev_dbg(hsotg->dev, " Assigned to channel %p:\n", chan);
  360. dev_dbg(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n",
  361. hcchar, hcsplt);
  362. dev_dbg(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n",
  363. hctsiz, hc_dma);
  364. dev_dbg(hsotg->dev, " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  365. chan->dev_addr, chan->ep_num, chan->ep_is_in);
  366. dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
  367. dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
  368. dev_dbg(hsotg->dev, " data_pid_start: %d\n", chan->data_pid_start);
  369. dev_dbg(hsotg->dev, " xfer_started: %d\n", chan->xfer_started);
  370. dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
  371. dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
  372. dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
  373. (unsigned long)chan->xfer_dma);
  374. dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
  375. dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
  376. dev_dbg(hsotg->dev, " NP inactive sched:\n");
  377. list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
  378. qh_list_entry)
  379. dev_dbg(hsotg->dev, " %p\n", qh);
  380. dev_dbg(hsotg->dev, " NP waiting sched:\n");
  381. list_for_each_entry(qh, &hsotg->non_periodic_sched_waiting,
  382. qh_list_entry)
  383. dev_dbg(hsotg->dev, " %p\n", qh);
  384. dev_dbg(hsotg->dev, " NP active sched:\n");
  385. list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
  386. qh_list_entry)
  387. dev_dbg(hsotg->dev, " %p\n", qh);
  388. dev_dbg(hsotg->dev, " Channels:\n");
  389. for (i = 0; i < num_channels; i++) {
  390. struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
  391. dev_dbg(hsotg->dev, " %2d: %p\n", i, chan);
  392. }
  393. #endif /* VERBOSE_DEBUG */
  394. }
  395. static int _dwc2_hcd_start(struct usb_hcd *hcd);
  396. static void dwc2_host_start(struct dwc2_hsotg *hsotg)
  397. {
  398. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  399. hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
  400. _dwc2_hcd_start(hcd);
  401. }
  402. static void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
  403. {
  404. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  405. hcd->self.is_b_host = 0;
  406. }
  407. static void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context,
  408. int *hub_addr, int *hub_port)
  409. {
  410. struct urb *urb = context;
  411. if (urb->dev->tt)
  412. *hub_addr = urb->dev->tt->hub->devnum;
  413. else
  414. *hub_addr = 0;
  415. *hub_port = urb->dev->ttport;
  416. }
  417. /*
  418. * =========================================================================
  419. * Low Level Host Channel Access Functions
  420. * =========================================================================
  421. */
  422. static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
  423. struct dwc2_host_chan *chan)
  424. {
  425. u32 hcintmsk = HCINTMSK_CHHLTD;
  426. switch (chan->ep_type) {
  427. case USB_ENDPOINT_XFER_CONTROL:
  428. case USB_ENDPOINT_XFER_BULK:
  429. dev_vdbg(hsotg->dev, "control/bulk\n");
  430. hcintmsk |= HCINTMSK_XFERCOMPL;
  431. hcintmsk |= HCINTMSK_STALL;
  432. hcintmsk |= HCINTMSK_XACTERR;
  433. hcintmsk |= HCINTMSK_DATATGLERR;
  434. if (chan->ep_is_in) {
  435. hcintmsk |= HCINTMSK_BBLERR;
  436. } else {
  437. hcintmsk |= HCINTMSK_NAK;
  438. hcintmsk |= HCINTMSK_NYET;
  439. if (chan->do_ping)
  440. hcintmsk |= HCINTMSK_ACK;
  441. }
  442. if (chan->do_split) {
  443. hcintmsk |= HCINTMSK_NAK;
  444. if (chan->complete_split)
  445. hcintmsk |= HCINTMSK_NYET;
  446. else
  447. hcintmsk |= HCINTMSK_ACK;
  448. }
  449. if (chan->error_state)
  450. hcintmsk |= HCINTMSK_ACK;
  451. break;
  452. case USB_ENDPOINT_XFER_INT:
  453. if (dbg_perio())
  454. dev_vdbg(hsotg->dev, "intr\n");
  455. hcintmsk |= HCINTMSK_XFERCOMPL;
  456. hcintmsk |= HCINTMSK_NAK;
  457. hcintmsk |= HCINTMSK_STALL;
  458. hcintmsk |= HCINTMSK_XACTERR;
  459. hcintmsk |= HCINTMSK_DATATGLERR;
  460. hcintmsk |= HCINTMSK_FRMOVRUN;
  461. if (chan->ep_is_in)
  462. hcintmsk |= HCINTMSK_BBLERR;
  463. if (chan->error_state)
  464. hcintmsk |= HCINTMSK_ACK;
  465. if (chan->do_split) {
  466. if (chan->complete_split)
  467. hcintmsk |= HCINTMSK_NYET;
  468. else
  469. hcintmsk |= HCINTMSK_ACK;
  470. }
  471. break;
  472. case USB_ENDPOINT_XFER_ISOC:
  473. if (dbg_perio())
  474. dev_vdbg(hsotg->dev, "isoc\n");
  475. hcintmsk |= HCINTMSK_XFERCOMPL;
  476. hcintmsk |= HCINTMSK_FRMOVRUN;
  477. hcintmsk |= HCINTMSK_ACK;
  478. if (chan->ep_is_in) {
  479. hcintmsk |= HCINTMSK_XACTERR;
  480. hcintmsk |= HCINTMSK_BBLERR;
  481. }
  482. break;
  483. default:
  484. dev_err(hsotg->dev, "## Unknown EP type ##\n");
  485. break;
  486. }
  487. dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
  488. if (dbg_hc(chan))
  489. dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
  490. }
  491. static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
  492. struct dwc2_host_chan *chan)
  493. {
  494. u32 hcintmsk = HCINTMSK_CHHLTD;
  495. /*
  496. * For Descriptor DMA mode core halts the channel on AHB error.
  497. * Interrupt is not required.
  498. */
  499. if (!hsotg->params.dma_desc_enable) {
  500. if (dbg_hc(chan))
  501. dev_vdbg(hsotg->dev, "desc DMA disabled\n");
  502. hcintmsk |= HCINTMSK_AHBERR;
  503. } else {
  504. if (dbg_hc(chan))
  505. dev_vdbg(hsotg->dev, "desc DMA enabled\n");
  506. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  507. hcintmsk |= HCINTMSK_XFERCOMPL;
  508. }
  509. if (chan->error_state && !chan->do_split &&
  510. chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
  511. if (dbg_hc(chan))
  512. dev_vdbg(hsotg->dev, "setting ACK\n");
  513. hcintmsk |= HCINTMSK_ACK;
  514. if (chan->ep_is_in) {
  515. hcintmsk |= HCINTMSK_DATATGLERR;
  516. if (chan->ep_type != USB_ENDPOINT_XFER_INT)
  517. hcintmsk |= HCINTMSK_NAK;
  518. }
  519. }
  520. dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
  521. if (dbg_hc(chan))
  522. dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
  523. }
  524. static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
  525. struct dwc2_host_chan *chan)
  526. {
  527. u32 intmsk;
  528. if (hsotg->params.host_dma) {
  529. if (dbg_hc(chan))
  530. dev_vdbg(hsotg->dev, "DMA enabled\n");
  531. dwc2_hc_enable_dma_ints(hsotg, chan);
  532. } else {
  533. if (dbg_hc(chan))
  534. dev_vdbg(hsotg->dev, "DMA disabled\n");
  535. dwc2_hc_enable_slave_ints(hsotg, chan);
  536. }
  537. /* Enable the top level host channel interrupt */
  538. intmsk = dwc2_readl(hsotg, HAINTMSK);
  539. intmsk |= 1 << chan->hc_num;
  540. dwc2_writel(hsotg, intmsk, HAINTMSK);
  541. if (dbg_hc(chan))
  542. dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
  543. /* Make sure host channel interrupts are enabled */
  544. intmsk = dwc2_readl(hsotg, GINTMSK);
  545. intmsk |= GINTSTS_HCHINT;
  546. dwc2_writel(hsotg, intmsk, GINTMSK);
  547. if (dbg_hc(chan))
  548. dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
  549. }
  550. /**
  551. * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
  552. * a specific endpoint
  553. *
  554. * @hsotg: Programming view of DWC_otg controller
  555. * @chan: Information needed to initialize the host channel
  556. *
  557. * The HCCHARn register is set up with the characteristics specified in chan.
  558. * Host channel interrupts that may need to be serviced while this transfer is
  559. * in progress are enabled.
  560. */
  561. static void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
  562. {
  563. u8 hc_num = chan->hc_num;
  564. u32 hcintmsk;
  565. u32 hcchar;
  566. u32 hcsplt = 0;
  567. if (dbg_hc(chan))
  568. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  569. /* Clear old interrupt conditions for this host channel */
  570. hcintmsk = 0xffffffff;
  571. hcintmsk &= ~HCINTMSK_RESERVED14_31;
  572. dwc2_writel(hsotg, hcintmsk, HCINT(hc_num));
  573. /* Enable channel interrupts required for this transfer */
  574. dwc2_hc_enable_ints(hsotg, chan);
  575. /*
  576. * Program the HCCHARn register with the endpoint characteristics for
  577. * the current transfer
  578. */
  579. hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
  580. hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
  581. if (chan->ep_is_in)
  582. hcchar |= HCCHAR_EPDIR;
  583. if (chan->speed == USB_SPEED_LOW)
  584. hcchar |= HCCHAR_LSPDDEV;
  585. hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
  586. hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
  587. dwc2_writel(hsotg, hcchar, HCCHAR(hc_num));
  588. if (dbg_hc(chan)) {
  589. dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
  590. hc_num, hcchar);
  591. dev_vdbg(hsotg->dev, "%s: Channel %d\n",
  592. __func__, hc_num);
  593. dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
  594. chan->dev_addr);
  595. dev_vdbg(hsotg->dev, " Ep Num: %d\n",
  596. chan->ep_num);
  597. dev_vdbg(hsotg->dev, " Is In: %d\n",
  598. chan->ep_is_in);
  599. dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
  600. chan->speed == USB_SPEED_LOW);
  601. dev_vdbg(hsotg->dev, " Ep Type: %d\n",
  602. chan->ep_type);
  603. dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
  604. chan->max_packet);
  605. }
  606. /* Program the HCSPLT register for SPLITs */
  607. if (chan->do_split) {
  608. if (dbg_hc(chan))
  609. dev_vdbg(hsotg->dev,
  610. "Programming HC %d with split --> %s\n",
  611. hc_num,
  612. chan->complete_split ? "CSPLIT" : "SSPLIT");
  613. if (chan->complete_split)
  614. hcsplt |= HCSPLT_COMPSPLT;
  615. hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
  616. HCSPLT_XACTPOS_MASK;
  617. hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
  618. HCSPLT_HUBADDR_MASK;
  619. hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
  620. HCSPLT_PRTADDR_MASK;
  621. if (dbg_hc(chan)) {
  622. dev_vdbg(hsotg->dev, " comp split %d\n",
  623. chan->complete_split);
  624. dev_vdbg(hsotg->dev, " xact pos %d\n",
  625. chan->xact_pos);
  626. dev_vdbg(hsotg->dev, " hub addr %d\n",
  627. chan->hub_addr);
  628. dev_vdbg(hsotg->dev, " hub port %d\n",
  629. chan->hub_port);
  630. dev_vdbg(hsotg->dev, " is_in %d\n",
  631. chan->ep_is_in);
  632. dev_vdbg(hsotg->dev, " Max Pkt %d\n",
  633. chan->max_packet);
  634. dev_vdbg(hsotg->dev, " xferlen %d\n",
  635. chan->xfer_len);
  636. }
  637. }
  638. dwc2_writel(hsotg, hcsplt, HCSPLT(hc_num));
  639. }
  640. /**
  641. * dwc2_hc_halt() - Attempts to halt a host channel
  642. *
  643. * @hsotg: Controller register interface
  644. * @chan: Host channel to halt
  645. * @halt_status: Reason for halting the channel
  646. *
  647. * This function should only be called in Slave mode or to abort a transfer in
  648. * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
  649. * controller halts the channel when the transfer is complete or a condition
  650. * occurs that requires application intervention.
  651. *
  652. * In slave mode, checks for a free request queue entry, then sets the Channel
  653. * Enable and Channel Disable bits of the Host Channel Characteristics
  654. * register of the specified channel to intiate the halt. If there is no free
  655. * request queue entry, sets only the Channel Disable bit of the HCCHARn
  656. * register to flush requests for this channel. In the latter case, sets a
  657. * flag to indicate that the host channel needs to be halted when a request
  658. * queue slot is open.
  659. *
  660. * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
  661. * HCCHARn register. The controller ensures there is space in the request
  662. * queue before submitting the halt request.
  663. *
  664. * Some time may elapse before the core flushes any posted requests for this
  665. * host channel and halts. The Channel Halted interrupt handler completes the
  666. * deactivation of the host channel.
  667. */
  668. void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
  669. enum dwc2_halt_status halt_status)
  670. {
  671. u32 nptxsts, hptxsts, hcchar;
  672. if (dbg_hc(chan))
  673. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  674. /*
  675. * In buffer DMA or external DMA mode channel can't be halted
  676. * for non-split periodic channels. At the end of the next
  677. * uframe/frame (in the worst case), the core generates a channel
  678. * halted and disables the channel automatically.
  679. */
  680. if ((hsotg->params.g_dma && !hsotg->params.g_dma_desc) ||
  681. hsotg->hw_params.arch == GHWCFG2_EXT_DMA_ARCH) {
  682. if (!chan->do_split &&
  683. (chan->ep_type == USB_ENDPOINT_XFER_ISOC ||
  684. chan->ep_type == USB_ENDPOINT_XFER_INT)) {
  685. dev_err(hsotg->dev, "%s() Channel can't be halted\n",
  686. __func__);
  687. return;
  688. }
  689. }
  690. if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
  691. dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
  692. if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
  693. halt_status == DWC2_HC_XFER_AHB_ERR) {
  694. /*
  695. * Disable all channel interrupts except Ch Halted. The QTD
  696. * and QH state associated with this transfer has been cleared
  697. * (in the case of URB_DEQUEUE), so the channel needs to be
  698. * shut down carefully to prevent crashes.
  699. */
  700. u32 hcintmsk = HCINTMSK_CHHLTD;
  701. dev_vdbg(hsotg->dev, "dequeue/error\n");
  702. dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
  703. /*
  704. * Make sure no other interrupts besides halt are currently
  705. * pending. Handling another interrupt could cause a crash due
  706. * to the QTD and QH state.
  707. */
  708. dwc2_writel(hsotg, ~hcintmsk, HCINT(chan->hc_num));
  709. /*
  710. * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
  711. * even if the channel was already halted for some other
  712. * reason
  713. */
  714. chan->halt_status = halt_status;
  715. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  716. if (!(hcchar & HCCHAR_CHENA)) {
  717. /*
  718. * The channel is either already halted or it hasn't
  719. * started yet. In DMA mode, the transfer may halt if
  720. * it finishes normally or a condition occurs that
  721. * requires driver intervention. Don't want to halt
  722. * the channel again. In either Slave or DMA mode,
  723. * it's possible that the transfer has been assigned
  724. * to a channel, but not started yet when an URB is
  725. * dequeued. Don't want to halt a channel that hasn't
  726. * started yet.
  727. */
  728. return;
  729. }
  730. }
  731. if (chan->halt_pending) {
  732. /*
  733. * A halt has already been issued for this channel. This might
  734. * happen when a transfer is aborted by a higher level in
  735. * the stack.
  736. */
  737. dev_vdbg(hsotg->dev,
  738. "*** %s: Channel %d, chan->halt_pending already set ***\n",
  739. __func__, chan->hc_num);
  740. return;
  741. }
  742. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  743. /* No need to set the bit in DDMA for disabling the channel */
  744. /* TODO check it everywhere channel is disabled */
  745. if (!hsotg->params.dma_desc_enable) {
  746. if (dbg_hc(chan))
  747. dev_vdbg(hsotg->dev, "desc DMA disabled\n");
  748. hcchar |= HCCHAR_CHENA;
  749. } else {
  750. if (dbg_hc(chan))
  751. dev_dbg(hsotg->dev, "desc DMA enabled\n");
  752. }
  753. hcchar |= HCCHAR_CHDIS;
  754. if (!hsotg->params.host_dma) {
  755. if (dbg_hc(chan))
  756. dev_vdbg(hsotg->dev, "DMA not enabled\n");
  757. hcchar |= HCCHAR_CHENA;
  758. /* Check for space in the request queue to issue the halt */
  759. if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
  760. chan->ep_type == USB_ENDPOINT_XFER_BULK) {
  761. dev_vdbg(hsotg->dev, "control/bulk\n");
  762. nptxsts = dwc2_readl(hsotg, GNPTXSTS);
  763. if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
  764. dev_vdbg(hsotg->dev, "Disabling channel\n");
  765. hcchar &= ~HCCHAR_CHENA;
  766. }
  767. } else {
  768. if (dbg_perio())
  769. dev_vdbg(hsotg->dev, "isoc/intr\n");
  770. hptxsts = dwc2_readl(hsotg, HPTXSTS);
  771. if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
  772. hsotg->queuing_high_bandwidth) {
  773. if (dbg_perio())
  774. dev_vdbg(hsotg->dev, "Disabling channel\n");
  775. hcchar &= ~HCCHAR_CHENA;
  776. }
  777. }
  778. } else {
  779. if (dbg_hc(chan))
  780. dev_vdbg(hsotg->dev, "DMA enabled\n");
  781. }
  782. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  783. chan->halt_status = halt_status;
  784. if (hcchar & HCCHAR_CHENA) {
  785. if (dbg_hc(chan))
  786. dev_vdbg(hsotg->dev, "Channel enabled\n");
  787. chan->halt_pending = 1;
  788. chan->halt_on_queue = 0;
  789. } else {
  790. if (dbg_hc(chan))
  791. dev_vdbg(hsotg->dev, "Channel disabled\n");
  792. chan->halt_on_queue = 1;
  793. }
  794. if (dbg_hc(chan)) {
  795. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  796. chan->hc_num);
  797. dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
  798. hcchar);
  799. dev_vdbg(hsotg->dev, " halt_pending: %d\n",
  800. chan->halt_pending);
  801. dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
  802. chan->halt_on_queue);
  803. dev_vdbg(hsotg->dev, " halt_status: %d\n",
  804. chan->halt_status);
  805. }
  806. }
  807. /**
  808. * dwc2_hc_cleanup() - Clears the transfer state for a host channel
  809. *
  810. * @hsotg: Programming view of DWC_otg controller
  811. * @chan: Identifies the host channel to clean up
  812. *
  813. * This function is normally called after a transfer is done and the host
  814. * channel is being released
  815. */
  816. void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
  817. {
  818. u32 hcintmsk;
  819. chan->xfer_started = 0;
  820. list_del_init(&chan->split_order_list_entry);
  821. /*
  822. * Clear channel interrupt enables and any unhandled channel interrupt
  823. * conditions
  824. */
  825. dwc2_writel(hsotg, 0, HCINTMSK(chan->hc_num));
  826. hcintmsk = 0xffffffff;
  827. hcintmsk &= ~HCINTMSK_RESERVED14_31;
  828. dwc2_writel(hsotg, hcintmsk, HCINT(chan->hc_num));
  829. }
  830. /**
  831. * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
  832. * which frame a periodic transfer should occur
  833. *
  834. * @hsotg: Programming view of DWC_otg controller
  835. * @chan: Identifies the host channel to set up and its properties
  836. * @hcchar: Current value of the HCCHAR register for the specified host channel
  837. *
  838. * This function has no effect on non-periodic transfers
  839. */
  840. static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
  841. struct dwc2_host_chan *chan, u32 *hcchar)
  842. {
  843. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  844. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  845. int host_speed;
  846. int xfer_ns;
  847. int xfer_us;
  848. int bytes_in_fifo;
  849. u16 fifo_space;
  850. u16 frame_number;
  851. u16 wire_frame;
  852. /*
  853. * Try to figure out if we're an even or odd frame. If we set
  854. * even and the current frame number is even the transfer
  855. * will happen immediately. Similar if both are odd. If one is
  856. * even and the other is odd then the transfer will happen when
  857. * the frame number ticks.
  858. *
  859. * There's a bit of a balancing act to get this right.
  860. * Sometimes we may want to send data in the current frame (AK
  861. * right away). We might want to do this if the frame number
  862. * _just_ ticked, but we might also want to do this in order
  863. * to continue a split transaction that happened late in a
  864. * microframe (so we didn't know to queue the next transfer
  865. * until the frame number had ticked). The problem is that we
  866. * need a lot of knowledge to know if there's actually still
  867. * time to send things or if it would be better to wait until
  868. * the next frame.
  869. *
  870. * We can look at how much time is left in the current frame
  871. * and make a guess about whether we'll have time to transfer.
  872. * We'll do that.
  873. */
  874. /* Get speed host is running at */
  875. host_speed = (chan->speed != USB_SPEED_HIGH &&
  876. !chan->do_split) ? chan->speed : USB_SPEED_HIGH;
  877. /* See how many bytes are in the periodic FIFO right now */
  878. fifo_space = (dwc2_readl(hsotg, HPTXSTS) &
  879. TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT;
  880. bytes_in_fifo = sizeof(u32) *
  881. (hsotg->params.host_perio_tx_fifo_size -
  882. fifo_space);
  883. /*
  884. * Roughly estimate bus time for everything in the periodic
  885. * queue + our new transfer. This is "rough" because we're
  886. * using a function that makes takes into account IN/OUT
  887. * and INT/ISO and we're just slamming in one value for all
  888. * transfers. This should be an over-estimate and that should
  889. * be OK, but we can probably tighten it.
  890. */
  891. xfer_ns = usb_calc_bus_time(host_speed, false, false,
  892. chan->xfer_len + bytes_in_fifo);
  893. xfer_us = NS_TO_US(xfer_ns);
  894. /* See what frame number we'll be at by the time we finish */
  895. frame_number = dwc2_hcd_get_future_frame_number(hsotg, xfer_us);
  896. /* This is when we were scheduled to be on the wire */
  897. wire_frame = dwc2_frame_num_inc(chan->qh->next_active_frame, 1);
  898. /*
  899. * If we'd finish _after_ the frame we're scheduled in then
  900. * it's hopeless. Just schedule right away and hope for the
  901. * best. Note that it _might_ be wise to call back into the
  902. * scheduler to pick a better frame, but this is better than
  903. * nothing.
  904. */
  905. if (dwc2_frame_num_gt(frame_number, wire_frame)) {
  906. dwc2_sch_vdbg(hsotg,
  907. "QH=%p EO MISS fr=%04x=>%04x (%+d)\n",
  908. chan->qh, wire_frame, frame_number,
  909. dwc2_frame_num_dec(frame_number,
  910. wire_frame));
  911. wire_frame = frame_number;
  912. /*
  913. * We picked a different frame number; communicate this
  914. * back to the scheduler so it doesn't try to schedule
  915. * another in the same frame.
  916. *
  917. * Remember that next_active_frame is 1 before the wire
  918. * frame.
  919. */
  920. chan->qh->next_active_frame =
  921. dwc2_frame_num_dec(frame_number, 1);
  922. }
  923. if (wire_frame & 1)
  924. *hcchar |= HCCHAR_ODDFRM;
  925. else
  926. *hcchar &= ~HCCHAR_ODDFRM;
  927. }
  928. }
  929. static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
  930. {
  931. /* Set up the initial PID for the transfer */
  932. if (chan->speed == USB_SPEED_HIGH) {
  933. if (chan->ep_is_in) {
  934. if (chan->multi_count == 1)
  935. chan->data_pid_start = DWC2_HC_PID_DATA0;
  936. else if (chan->multi_count == 2)
  937. chan->data_pid_start = DWC2_HC_PID_DATA1;
  938. else
  939. chan->data_pid_start = DWC2_HC_PID_DATA2;
  940. } else {
  941. if (chan->multi_count == 1)
  942. chan->data_pid_start = DWC2_HC_PID_DATA0;
  943. else
  944. chan->data_pid_start = DWC2_HC_PID_MDATA;
  945. }
  946. } else {
  947. chan->data_pid_start = DWC2_HC_PID_DATA0;
  948. }
  949. }
  950. /**
  951. * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
  952. * the Host Channel
  953. *
  954. * @hsotg: Programming view of DWC_otg controller
  955. * @chan: Information needed to initialize the host channel
  956. *
  957. * This function should only be called in Slave mode. For a channel associated
  958. * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
  959. * associated with a periodic EP, the periodic Tx FIFO is written.
  960. *
  961. * Upon return the xfer_buf and xfer_count fields in chan are incremented by
  962. * the number of bytes written to the Tx FIFO.
  963. */
  964. static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
  965. struct dwc2_host_chan *chan)
  966. {
  967. u32 i;
  968. u32 remaining_count;
  969. u32 byte_count;
  970. u32 dword_count;
  971. u32 *data_buf = (u32 *)chan->xfer_buf;
  972. if (dbg_hc(chan))
  973. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  974. remaining_count = chan->xfer_len - chan->xfer_count;
  975. if (remaining_count > chan->max_packet)
  976. byte_count = chan->max_packet;
  977. else
  978. byte_count = remaining_count;
  979. dword_count = (byte_count + 3) / 4;
  980. if (((unsigned long)data_buf & 0x3) == 0) {
  981. /* xfer_buf is DWORD aligned */
  982. for (i = 0; i < dword_count; i++, data_buf++)
  983. dwc2_writel(hsotg, *data_buf, HCFIFO(chan->hc_num));
  984. } else {
  985. /* xfer_buf is not DWORD aligned */
  986. for (i = 0; i < dword_count; i++, data_buf++) {
  987. u32 data = data_buf[0] | data_buf[1] << 8 |
  988. data_buf[2] << 16 | data_buf[3] << 24;
  989. dwc2_writel(hsotg, data, HCFIFO(chan->hc_num));
  990. }
  991. }
  992. chan->xfer_count += byte_count;
  993. chan->xfer_buf += byte_count;
  994. }
  995. /**
  996. * dwc2_hc_do_ping() - Starts a PING transfer
  997. *
  998. * @hsotg: Programming view of DWC_otg controller
  999. * @chan: Information needed to initialize the host channel
  1000. *
  1001. * This function should only be called in Slave mode. The Do Ping bit is set in
  1002. * the HCTSIZ register, then the channel is enabled.
  1003. */
  1004. static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
  1005. struct dwc2_host_chan *chan)
  1006. {
  1007. u32 hcchar;
  1008. u32 hctsiz;
  1009. if (dbg_hc(chan))
  1010. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1011. chan->hc_num);
  1012. hctsiz = TSIZ_DOPNG;
  1013. hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
  1014. dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
  1015. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  1016. hcchar |= HCCHAR_CHENA;
  1017. hcchar &= ~HCCHAR_CHDIS;
  1018. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  1019. }
  1020. /**
  1021. * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
  1022. * channel and starts the transfer
  1023. *
  1024. * @hsotg: Programming view of DWC_otg controller
  1025. * @chan: Information needed to initialize the host channel. The xfer_len value
  1026. * may be reduced to accommodate the max widths of the XferSize and
  1027. * PktCnt fields in the HCTSIZn register. The multi_count value may be
  1028. * changed to reflect the final xfer_len value.
  1029. *
  1030. * This function may be called in either Slave mode or DMA mode. In Slave mode,
  1031. * the caller must ensure that there is sufficient space in the request queue
  1032. * and Tx Data FIFO.
  1033. *
  1034. * For an OUT transfer in Slave mode, it loads a data packet into the
  1035. * appropriate FIFO. If necessary, additional data packets are loaded in the
  1036. * Host ISR.
  1037. *
  1038. * For an IN transfer in Slave mode, a data packet is requested. The data
  1039. * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  1040. * additional data packets are requested in the Host ISR.
  1041. *
  1042. * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  1043. * register along with a packet count of 1 and the channel is enabled. This
  1044. * causes a single PING transaction to occur. Other fields in HCTSIZ are
  1045. * simply set to 0 since no data transfer occurs in this case.
  1046. *
  1047. * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  1048. * all the information required to perform the subsequent data transfer. In
  1049. * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  1050. * controller performs the entire PING protocol, then starts the data
  1051. * transfer.
  1052. */
  1053. static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
  1054. struct dwc2_host_chan *chan)
  1055. {
  1056. u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
  1057. u16 max_hc_pkt_count = hsotg->params.max_packet_count;
  1058. u32 hcchar;
  1059. u32 hctsiz = 0;
  1060. u16 num_packets;
  1061. u32 ec_mc;
  1062. if (dbg_hc(chan))
  1063. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1064. if (chan->do_ping) {
  1065. if (!hsotg->params.host_dma) {
  1066. if (dbg_hc(chan))
  1067. dev_vdbg(hsotg->dev, "ping, no DMA\n");
  1068. dwc2_hc_do_ping(hsotg, chan);
  1069. chan->xfer_started = 1;
  1070. return;
  1071. }
  1072. if (dbg_hc(chan))
  1073. dev_vdbg(hsotg->dev, "ping, DMA\n");
  1074. hctsiz |= TSIZ_DOPNG;
  1075. }
  1076. if (chan->do_split) {
  1077. if (dbg_hc(chan))
  1078. dev_vdbg(hsotg->dev, "split\n");
  1079. num_packets = 1;
  1080. if (chan->complete_split && !chan->ep_is_in)
  1081. /*
  1082. * For CSPLIT OUT Transfer, set the size to 0 so the
  1083. * core doesn't expect any data written to the FIFO
  1084. */
  1085. chan->xfer_len = 0;
  1086. else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
  1087. chan->xfer_len = chan->max_packet;
  1088. else if (!chan->ep_is_in && chan->xfer_len > 188)
  1089. chan->xfer_len = 188;
  1090. hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
  1091. TSIZ_XFERSIZE_MASK;
  1092. /* For split set ec_mc for immediate retries */
  1093. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1094. chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1095. ec_mc = 3;
  1096. else
  1097. ec_mc = 1;
  1098. } else {
  1099. if (dbg_hc(chan))
  1100. dev_vdbg(hsotg->dev, "no split\n");
  1101. /*
  1102. * Ensure that the transfer length and packet count will fit
  1103. * in the widths allocated for them in the HCTSIZn register
  1104. */
  1105. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1106. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1107. /*
  1108. * Make sure the transfer size is no larger than one
  1109. * (micro)frame's worth of data. (A check was done
  1110. * when the periodic transfer was accepted to ensure
  1111. * that a (micro)frame's worth of data can be
  1112. * programmed into a channel.)
  1113. */
  1114. u32 max_periodic_len =
  1115. chan->multi_count * chan->max_packet;
  1116. if (chan->xfer_len > max_periodic_len)
  1117. chan->xfer_len = max_periodic_len;
  1118. } else if (chan->xfer_len > max_hc_xfer_size) {
  1119. /*
  1120. * Make sure that xfer_len is a multiple of max packet
  1121. * size
  1122. */
  1123. chan->xfer_len =
  1124. max_hc_xfer_size - chan->max_packet + 1;
  1125. }
  1126. if (chan->xfer_len > 0) {
  1127. num_packets = (chan->xfer_len + chan->max_packet - 1) /
  1128. chan->max_packet;
  1129. if (num_packets > max_hc_pkt_count) {
  1130. num_packets = max_hc_pkt_count;
  1131. chan->xfer_len = num_packets * chan->max_packet;
  1132. } else if (chan->ep_is_in) {
  1133. /*
  1134. * Always program an integral # of max packets
  1135. * for IN transfers.
  1136. * Note: This assumes that the input buffer is
  1137. * aligned and sized accordingly.
  1138. */
  1139. chan->xfer_len = num_packets * chan->max_packet;
  1140. }
  1141. } else {
  1142. /* Need 1 packet for transfer length of 0 */
  1143. num_packets = 1;
  1144. }
  1145. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1146. chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1147. /*
  1148. * Make sure that the multi_count field matches the
  1149. * actual transfer length
  1150. */
  1151. chan->multi_count = num_packets;
  1152. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1153. dwc2_set_pid_isoc(chan);
  1154. hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
  1155. TSIZ_XFERSIZE_MASK;
  1156. /* The ec_mc gets the multi_count for non-split */
  1157. ec_mc = chan->multi_count;
  1158. }
  1159. chan->start_pkt_count = num_packets;
  1160. hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
  1161. hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
  1162. TSIZ_SC_MC_PID_MASK;
  1163. dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
  1164. if (dbg_hc(chan)) {
  1165. dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
  1166. hctsiz, chan->hc_num);
  1167. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1168. chan->hc_num);
  1169. dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
  1170. (hctsiz & TSIZ_XFERSIZE_MASK) >>
  1171. TSIZ_XFERSIZE_SHIFT);
  1172. dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
  1173. (hctsiz & TSIZ_PKTCNT_MASK) >>
  1174. TSIZ_PKTCNT_SHIFT);
  1175. dev_vdbg(hsotg->dev, " Start PID: %d\n",
  1176. (hctsiz & TSIZ_SC_MC_PID_MASK) >>
  1177. TSIZ_SC_MC_PID_SHIFT);
  1178. }
  1179. if (hsotg->params.host_dma) {
  1180. dma_addr_t dma_addr;
  1181. if (chan->align_buf) {
  1182. if (dbg_hc(chan))
  1183. dev_vdbg(hsotg->dev, "align_buf\n");
  1184. dma_addr = chan->align_buf;
  1185. } else {
  1186. dma_addr = chan->xfer_dma;
  1187. }
  1188. dwc2_writel(hsotg, (u32)dma_addr, HCDMA(chan->hc_num));
  1189. if (dbg_hc(chan))
  1190. dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
  1191. (unsigned long)dma_addr, chan->hc_num);
  1192. }
  1193. /* Start the split */
  1194. if (chan->do_split) {
  1195. u32 hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
  1196. hcsplt |= HCSPLT_SPLTENA;
  1197. dwc2_writel(hsotg, hcsplt, HCSPLT(chan->hc_num));
  1198. }
  1199. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  1200. hcchar &= ~HCCHAR_MULTICNT_MASK;
  1201. hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
  1202. dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
  1203. if (hcchar & HCCHAR_CHDIS)
  1204. dev_warn(hsotg->dev,
  1205. "%s: chdis set, channel %d, hcchar 0x%08x\n",
  1206. __func__, chan->hc_num, hcchar);
  1207. /* Set host channel enable after all other setup is complete */
  1208. hcchar |= HCCHAR_CHENA;
  1209. hcchar &= ~HCCHAR_CHDIS;
  1210. if (dbg_hc(chan))
  1211. dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
  1212. (hcchar & HCCHAR_MULTICNT_MASK) >>
  1213. HCCHAR_MULTICNT_SHIFT);
  1214. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  1215. if (dbg_hc(chan))
  1216. dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
  1217. chan->hc_num);
  1218. chan->xfer_started = 1;
  1219. chan->requests++;
  1220. if (!hsotg->params.host_dma &&
  1221. !chan->ep_is_in && chan->xfer_len > 0)
  1222. /* Load OUT packet into the appropriate Tx FIFO */
  1223. dwc2_hc_write_packet(hsotg, chan);
  1224. }
  1225. /**
  1226. * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
  1227. * host channel and starts the transfer in Descriptor DMA mode
  1228. *
  1229. * @hsotg: Programming view of DWC_otg controller
  1230. * @chan: Information needed to initialize the host channel
  1231. *
  1232. * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
  1233. * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
  1234. * with micro-frame bitmap.
  1235. *
  1236. * Initializes HCDMA register with descriptor list address and CTD value then
  1237. * starts the transfer via enabling the channel.
  1238. */
  1239. void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
  1240. struct dwc2_host_chan *chan)
  1241. {
  1242. u32 hcchar;
  1243. u32 hctsiz = 0;
  1244. if (chan->do_ping)
  1245. hctsiz |= TSIZ_DOPNG;
  1246. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1247. dwc2_set_pid_isoc(chan);
  1248. /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
  1249. hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
  1250. TSIZ_SC_MC_PID_MASK;
  1251. /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
  1252. hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
  1253. /* Non-zero only for high-speed interrupt endpoints */
  1254. hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
  1255. if (dbg_hc(chan)) {
  1256. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1257. chan->hc_num);
  1258. dev_vdbg(hsotg->dev, " Start PID: %d\n",
  1259. chan->data_pid_start);
  1260. dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
  1261. }
  1262. dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
  1263. dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
  1264. chan->desc_list_sz, DMA_TO_DEVICE);
  1265. dwc2_writel(hsotg, chan->desc_list_addr, HCDMA(chan->hc_num));
  1266. if (dbg_hc(chan))
  1267. dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
  1268. &chan->desc_list_addr, chan->hc_num);
  1269. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  1270. hcchar &= ~HCCHAR_MULTICNT_MASK;
  1271. hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
  1272. HCCHAR_MULTICNT_MASK;
  1273. if (hcchar & HCCHAR_CHDIS)
  1274. dev_warn(hsotg->dev,
  1275. "%s: chdis set, channel %d, hcchar 0x%08x\n",
  1276. __func__, chan->hc_num, hcchar);
  1277. /* Set host channel enable after all other setup is complete */
  1278. hcchar |= HCCHAR_CHENA;
  1279. hcchar &= ~HCCHAR_CHDIS;
  1280. if (dbg_hc(chan))
  1281. dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
  1282. (hcchar & HCCHAR_MULTICNT_MASK) >>
  1283. HCCHAR_MULTICNT_SHIFT);
  1284. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  1285. if (dbg_hc(chan))
  1286. dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
  1287. chan->hc_num);
  1288. chan->xfer_started = 1;
  1289. chan->requests++;
  1290. }
  1291. /**
  1292. * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
  1293. * a previous call to dwc2_hc_start_transfer()
  1294. *
  1295. * @hsotg: Programming view of DWC_otg controller
  1296. * @chan: Information needed to initialize the host channel
  1297. *
  1298. * The caller must ensure there is sufficient space in the request queue and Tx
  1299. * Data FIFO. This function should only be called in Slave mode. In DMA mode,
  1300. * the controller acts autonomously to complete transfers programmed to a host
  1301. * channel.
  1302. *
  1303. * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  1304. * if there is any data remaining to be queued. For an IN transfer, another
  1305. * data packet is always requested. For the SETUP phase of a control transfer,
  1306. * this function does nothing.
  1307. *
  1308. * Return: 1 if a new request is queued, 0 if no more requests are required
  1309. * for this transfer
  1310. */
  1311. static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
  1312. struct dwc2_host_chan *chan)
  1313. {
  1314. if (dbg_hc(chan))
  1315. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1316. chan->hc_num);
  1317. if (chan->do_split)
  1318. /* SPLITs always queue just once per channel */
  1319. return 0;
  1320. if (chan->data_pid_start == DWC2_HC_PID_SETUP)
  1321. /* SETUPs are queued only once since they can't be NAK'd */
  1322. return 0;
  1323. if (chan->ep_is_in) {
  1324. /*
  1325. * Always queue another request for other IN transfers. If
  1326. * back-to-back INs are issued and NAKs are received for both,
  1327. * the driver may still be processing the first NAK when the
  1328. * second NAK is received. When the interrupt handler clears
  1329. * the NAK interrupt for the first NAK, the second NAK will
  1330. * not be seen. So we can't depend on the NAK interrupt
  1331. * handler to requeue a NAK'd request. Instead, IN requests
  1332. * are issued each time this function is called. When the
  1333. * transfer completes, the extra requests for the channel will
  1334. * be flushed.
  1335. */
  1336. u32 hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  1337. dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
  1338. hcchar |= HCCHAR_CHENA;
  1339. hcchar &= ~HCCHAR_CHDIS;
  1340. if (dbg_hc(chan))
  1341. dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
  1342. hcchar);
  1343. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  1344. chan->requests++;
  1345. return 1;
  1346. }
  1347. /* OUT transfers */
  1348. if (chan->xfer_count < chan->xfer_len) {
  1349. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1350. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1351. u32 hcchar = dwc2_readl(hsotg,
  1352. HCCHAR(chan->hc_num));
  1353. dwc2_hc_set_even_odd_frame(hsotg, chan,
  1354. &hcchar);
  1355. }
  1356. /* Load OUT packet into the appropriate Tx FIFO */
  1357. dwc2_hc_write_packet(hsotg, chan);
  1358. chan->requests++;
  1359. return 1;
  1360. }
  1361. return 0;
  1362. }
  1363. /*
  1364. * =========================================================================
  1365. * HCD
  1366. * =========================================================================
  1367. */
  1368. /*
  1369. * Processes all the URBs in a single list of QHs. Completes them with
  1370. * -ETIMEDOUT and frees the QTD.
  1371. *
  1372. * Must be called with interrupt disabled and spinlock held
  1373. */
  1374. static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
  1375. struct list_head *qh_list)
  1376. {
  1377. struct dwc2_qh *qh, *qh_tmp;
  1378. struct dwc2_qtd *qtd, *qtd_tmp;
  1379. list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
  1380. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
  1381. qtd_list_entry) {
  1382. dwc2_host_complete(hsotg, qtd, -ECONNRESET);
  1383. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  1384. }
  1385. }
  1386. }
  1387. static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
  1388. struct list_head *qh_list)
  1389. {
  1390. struct dwc2_qtd *qtd, *qtd_tmp;
  1391. struct dwc2_qh *qh, *qh_tmp;
  1392. unsigned long flags;
  1393. if (!qh_list->next)
  1394. /* The list hasn't been initialized yet */
  1395. return;
  1396. spin_lock_irqsave(&hsotg->lock, flags);
  1397. /* Ensure there are no QTDs or URBs left */
  1398. dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
  1399. list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
  1400. dwc2_hcd_qh_unlink(hsotg, qh);
  1401. /* Free each QTD in the QH's QTD list */
  1402. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
  1403. qtd_list_entry)
  1404. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  1405. if (qh->channel && qh->channel->qh == qh)
  1406. qh->channel->qh = NULL;
  1407. spin_unlock_irqrestore(&hsotg->lock, flags);
  1408. dwc2_hcd_qh_free(hsotg, qh);
  1409. spin_lock_irqsave(&hsotg->lock, flags);
  1410. }
  1411. spin_unlock_irqrestore(&hsotg->lock, flags);
  1412. }
  1413. /*
  1414. * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
  1415. * and periodic schedules. The QTD associated with each URB is removed from
  1416. * the schedule and freed. This function may be called when a disconnect is
  1417. * detected or when the HCD is being stopped.
  1418. *
  1419. * Must be called with interrupt disabled and spinlock held
  1420. */
  1421. static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
  1422. {
  1423. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
  1424. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_waiting);
  1425. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
  1426. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
  1427. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
  1428. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
  1429. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
  1430. }
  1431. /**
  1432. * dwc2_hcd_start() - Starts the HCD when switching to Host mode
  1433. *
  1434. * @hsotg: Pointer to struct dwc2_hsotg
  1435. */
  1436. void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
  1437. {
  1438. u32 hprt0;
  1439. if (hsotg->op_state == OTG_STATE_B_HOST) {
  1440. /*
  1441. * Reset the port. During a HNP mode switch the reset
  1442. * needs to occur within 1ms and have a duration of at
  1443. * least 50ms.
  1444. */
  1445. hprt0 = dwc2_read_hprt0(hsotg);
  1446. hprt0 |= HPRT0_RST;
  1447. dwc2_writel(hsotg, hprt0, HPRT0);
  1448. }
  1449. queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
  1450. msecs_to_jiffies(50));
  1451. }
  1452. /* Must be called with interrupt disabled and spinlock held */
  1453. static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
  1454. {
  1455. int num_channels = hsotg->params.host_channels;
  1456. struct dwc2_host_chan *channel;
  1457. u32 hcchar;
  1458. int i;
  1459. if (!hsotg->params.host_dma) {
  1460. /* Flush out any channel requests in slave mode */
  1461. for (i = 0; i < num_channels; i++) {
  1462. channel = hsotg->hc_ptr_array[i];
  1463. if (!list_empty(&channel->hc_list_entry))
  1464. continue;
  1465. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  1466. if (hcchar & HCCHAR_CHENA) {
  1467. hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
  1468. hcchar |= HCCHAR_CHDIS;
  1469. dwc2_writel(hsotg, hcchar, HCCHAR(i));
  1470. }
  1471. }
  1472. }
  1473. for (i = 0; i < num_channels; i++) {
  1474. channel = hsotg->hc_ptr_array[i];
  1475. if (!list_empty(&channel->hc_list_entry))
  1476. continue;
  1477. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  1478. if (hcchar & HCCHAR_CHENA) {
  1479. /* Halt the channel */
  1480. hcchar |= HCCHAR_CHDIS;
  1481. dwc2_writel(hsotg, hcchar, HCCHAR(i));
  1482. }
  1483. dwc2_hc_cleanup(hsotg, channel);
  1484. list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
  1485. /*
  1486. * Added for Descriptor DMA to prevent channel double cleanup in
  1487. * release_channel_ddma(), which is called from ep_disable when
  1488. * device disconnects
  1489. */
  1490. channel->qh = NULL;
  1491. }
  1492. /* All channels have been freed, mark them available */
  1493. if (hsotg->params.uframe_sched) {
  1494. hsotg->available_host_channels =
  1495. hsotg->params.host_channels;
  1496. } else {
  1497. hsotg->non_periodic_channels = 0;
  1498. hsotg->periodic_channels = 0;
  1499. }
  1500. }
  1501. /**
  1502. * dwc2_hcd_connect() - Handles connect of the HCD
  1503. *
  1504. * @hsotg: Pointer to struct dwc2_hsotg
  1505. *
  1506. * Must be called with interrupt disabled and spinlock held
  1507. */
  1508. void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
  1509. {
  1510. if (hsotg->lx_state != DWC2_L0)
  1511. usb_hcd_resume_root_hub(hsotg->priv);
  1512. hsotg->flags.b.port_connect_status_change = 1;
  1513. hsotg->flags.b.port_connect_status = 1;
  1514. }
  1515. /**
  1516. * dwc2_hcd_disconnect() - Handles disconnect of the HCD
  1517. *
  1518. * @hsotg: Pointer to struct dwc2_hsotg
  1519. * @force: If true, we won't try to reconnect even if we see device connected.
  1520. *
  1521. * Must be called with interrupt disabled and spinlock held
  1522. */
  1523. void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
  1524. {
  1525. u32 intr;
  1526. u32 hprt0;
  1527. /* Set status flags for the hub driver */
  1528. hsotg->flags.b.port_connect_status_change = 1;
  1529. hsotg->flags.b.port_connect_status = 0;
  1530. /*
  1531. * Shutdown any transfers in process by clearing the Tx FIFO Empty
  1532. * interrupt mask and status bits and disabling subsequent host
  1533. * channel interrupts.
  1534. */
  1535. intr = dwc2_readl(hsotg, GINTMSK);
  1536. intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
  1537. dwc2_writel(hsotg, intr, GINTMSK);
  1538. intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
  1539. dwc2_writel(hsotg, intr, GINTSTS);
  1540. /*
  1541. * Turn off the vbus power only if the core has transitioned to device
  1542. * mode. If still in host mode, need to keep power on to detect a
  1543. * reconnection.
  1544. */
  1545. if (dwc2_is_device_mode(hsotg)) {
  1546. if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
  1547. dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
  1548. dwc2_writel(hsotg, 0, HPRT0);
  1549. }
  1550. dwc2_disable_host_interrupts(hsotg);
  1551. }
  1552. /* Respond with an error status to all URBs in the schedule */
  1553. dwc2_kill_all_urbs(hsotg);
  1554. if (dwc2_is_host_mode(hsotg))
  1555. /* Clean up any host channels that were in use */
  1556. dwc2_hcd_cleanup_channels(hsotg);
  1557. dwc2_host_disconnect(hsotg);
  1558. /*
  1559. * Add an extra check here to see if we're actually connected but
  1560. * we don't have a detection interrupt pending. This can happen if:
  1561. * 1. hardware sees connect
  1562. * 2. hardware sees disconnect
  1563. * 3. hardware sees connect
  1564. * 4. dwc2_port_intr() - clears connect interrupt
  1565. * 5. dwc2_handle_common_intr() - calls here
  1566. *
  1567. * Without the extra check here we will end calling disconnect
  1568. * and won't get any future interrupts to handle the connect.
  1569. */
  1570. if (!force) {
  1571. hprt0 = dwc2_readl(hsotg, HPRT0);
  1572. if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
  1573. dwc2_hcd_connect(hsotg);
  1574. }
  1575. }
  1576. /**
  1577. * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
  1578. *
  1579. * @hsotg: Pointer to struct dwc2_hsotg
  1580. */
  1581. static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
  1582. {
  1583. if (hsotg->bus_suspended) {
  1584. hsotg->flags.b.port_suspend_change = 1;
  1585. usb_hcd_resume_root_hub(hsotg->priv);
  1586. }
  1587. if (hsotg->lx_state == DWC2_L1)
  1588. hsotg->flags.b.port_l1_change = 1;
  1589. }
  1590. /**
  1591. * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
  1592. *
  1593. * @hsotg: Pointer to struct dwc2_hsotg
  1594. *
  1595. * Must be called with interrupt disabled and spinlock held
  1596. */
  1597. void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
  1598. {
  1599. dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
  1600. /*
  1601. * The root hub should be disconnected before this function is called.
  1602. * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  1603. * and the QH lists (via ..._hcd_endpoint_disable).
  1604. */
  1605. /* Turn off all host-specific interrupts */
  1606. dwc2_disable_host_interrupts(hsotg);
  1607. /* Turn off the vbus power */
  1608. dev_dbg(hsotg->dev, "PortPower off\n");
  1609. dwc2_writel(hsotg, 0, HPRT0);
  1610. }
  1611. /* Caller must hold driver lock */
  1612. static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
  1613. struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
  1614. struct dwc2_qtd *qtd)
  1615. {
  1616. u32 intr_mask;
  1617. int retval;
  1618. int dev_speed;
  1619. if (!hsotg->flags.b.port_connect_status) {
  1620. /* No longer connected */
  1621. dev_err(hsotg->dev, "Not connected\n");
  1622. return -ENODEV;
  1623. }
  1624. dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
  1625. /* Some configurations cannot support LS traffic on a FS root port */
  1626. if ((dev_speed == USB_SPEED_LOW) &&
  1627. (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
  1628. (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
  1629. u32 hprt0 = dwc2_readl(hsotg, HPRT0);
  1630. u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  1631. if (prtspd == HPRT0_SPD_FULL_SPEED)
  1632. return -ENODEV;
  1633. }
  1634. if (!qtd)
  1635. return -EINVAL;
  1636. dwc2_hcd_qtd_init(qtd, urb);
  1637. retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
  1638. if (retval) {
  1639. dev_err(hsotg->dev,
  1640. "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
  1641. retval);
  1642. return retval;
  1643. }
  1644. intr_mask = dwc2_readl(hsotg, GINTMSK);
  1645. if (!(intr_mask & GINTSTS_SOF)) {
  1646. enum dwc2_transaction_type tr_type;
  1647. if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
  1648. !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  1649. /*
  1650. * Do not schedule SG transactions until qtd has
  1651. * URB_GIVEBACK_ASAP set
  1652. */
  1653. return 0;
  1654. tr_type = dwc2_hcd_select_transactions(hsotg);
  1655. if (tr_type != DWC2_TRANSACTION_NONE)
  1656. dwc2_hcd_queue_transactions(hsotg, tr_type);
  1657. }
  1658. return 0;
  1659. }
  1660. /* Must be called with interrupt disabled and spinlock held */
  1661. static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
  1662. struct dwc2_hcd_urb *urb)
  1663. {
  1664. struct dwc2_qh *qh;
  1665. struct dwc2_qtd *urb_qtd;
  1666. urb_qtd = urb->qtd;
  1667. if (!urb_qtd) {
  1668. dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
  1669. return -EINVAL;
  1670. }
  1671. qh = urb_qtd->qh;
  1672. if (!qh) {
  1673. dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
  1674. return -EINVAL;
  1675. }
  1676. urb->priv = NULL;
  1677. if (urb_qtd->in_process && qh->channel) {
  1678. dwc2_dump_channel_info(hsotg, qh->channel);
  1679. /* The QTD is in process (it has been assigned to a channel) */
  1680. if (hsotg->flags.b.port_connect_status)
  1681. /*
  1682. * If still connected (i.e. in host mode), halt the
  1683. * channel so it can be used for other transfers. If
  1684. * no longer connected, the host registers can't be
  1685. * written to halt the channel since the core is in
  1686. * device mode.
  1687. */
  1688. dwc2_hc_halt(hsotg, qh->channel,
  1689. DWC2_HC_XFER_URB_DEQUEUE);
  1690. }
  1691. /*
  1692. * Free the QTD and clean up the associated QH. Leave the QH in the
  1693. * schedule if it has any remaining QTDs.
  1694. */
  1695. if (!hsotg->params.dma_desc_enable) {
  1696. u8 in_process = urb_qtd->in_process;
  1697. dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
  1698. if (in_process) {
  1699. dwc2_hcd_qh_deactivate(hsotg, qh, 0);
  1700. qh->channel = NULL;
  1701. } else if (list_empty(&qh->qtd_list)) {
  1702. dwc2_hcd_qh_unlink(hsotg, qh);
  1703. }
  1704. } else {
  1705. dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
  1706. }
  1707. return 0;
  1708. }
  1709. /* Must NOT be called with interrupt disabled or spinlock held */
  1710. static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
  1711. struct usb_host_endpoint *ep, int retry)
  1712. {
  1713. struct dwc2_qtd *qtd, *qtd_tmp;
  1714. struct dwc2_qh *qh;
  1715. unsigned long flags;
  1716. int rc;
  1717. spin_lock_irqsave(&hsotg->lock, flags);
  1718. qh = ep->hcpriv;
  1719. if (!qh) {
  1720. rc = -EINVAL;
  1721. goto err;
  1722. }
  1723. while (!list_empty(&qh->qtd_list) && retry--) {
  1724. if (retry == 0) {
  1725. dev_err(hsotg->dev,
  1726. "## timeout in dwc2_hcd_endpoint_disable() ##\n");
  1727. rc = -EBUSY;
  1728. goto err;
  1729. }
  1730. spin_unlock_irqrestore(&hsotg->lock, flags);
  1731. msleep(20);
  1732. spin_lock_irqsave(&hsotg->lock, flags);
  1733. qh = ep->hcpriv;
  1734. if (!qh) {
  1735. rc = -EINVAL;
  1736. goto err;
  1737. }
  1738. }
  1739. dwc2_hcd_qh_unlink(hsotg, qh);
  1740. /* Free each QTD in the QH's QTD list */
  1741. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
  1742. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  1743. ep->hcpriv = NULL;
  1744. if (qh->channel && qh->channel->qh == qh)
  1745. qh->channel->qh = NULL;
  1746. spin_unlock_irqrestore(&hsotg->lock, flags);
  1747. dwc2_hcd_qh_free(hsotg, qh);
  1748. return 0;
  1749. err:
  1750. ep->hcpriv = NULL;
  1751. spin_unlock_irqrestore(&hsotg->lock, flags);
  1752. return rc;
  1753. }
  1754. /* Must be called with interrupt disabled and spinlock held */
  1755. static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
  1756. struct usb_host_endpoint *ep)
  1757. {
  1758. struct dwc2_qh *qh = ep->hcpriv;
  1759. if (!qh)
  1760. return -EINVAL;
  1761. qh->data_toggle = DWC2_HC_PID_DATA0;
  1762. return 0;
  1763. }
  1764. /**
  1765. * dwc2_core_init() - Initializes the DWC_otg controller registers and
  1766. * prepares the core for device mode or host mode operation
  1767. *
  1768. * @hsotg: Programming view of the DWC_otg controller
  1769. * @initial_setup: If true then this is the first init for this instance.
  1770. */
  1771. int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
  1772. {
  1773. u32 usbcfg, otgctl;
  1774. int retval;
  1775. dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
  1776. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  1777. /* Set ULPI External VBUS bit if needed */
  1778. usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
  1779. if (hsotg->params.phy_ulpi_ext_vbus)
  1780. usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
  1781. /* Set external TS Dline pulsing bit if needed */
  1782. usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
  1783. if (hsotg->params.ts_dline)
  1784. usbcfg |= GUSBCFG_TERMSELDLPULSE;
  1785. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  1786. /*
  1787. * Reset the Controller
  1788. *
  1789. * We only need to reset the controller if this is a re-init.
  1790. * For the first init we know for sure that earlier code reset us (it
  1791. * needed to in order to properly detect various parameters).
  1792. */
  1793. if (!initial_setup) {
  1794. retval = dwc2_core_reset(hsotg, false);
  1795. if (retval) {
  1796. dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
  1797. __func__);
  1798. return retval;
  1799. }
  1800. }
  1801. /*
  1802. * This needs to happen in FS mode before any other programming occurs
  1803. */
  1804. retval = dwc2_phy_init(hsotg, initial_setup);
  1805. if (retval)
  1806. return retval;
  1807. /* Program the GAHBCFG Register */
  1808. retval = dwc2_gahbcfg_init(hsotg);
  1809. if (retval)
  1810. return retval;
  1811. /* Program the GUSBCFG register */
  1812. dwc2_gusbcfg_init(hsotg);
  1813. /* Program the GOTGCTL register */
  1814. otgctl = dwc2_readl(hsotg, GOTGCTL);
  1815. otgctl &= ~GOTGCTL_OTGVER;
  1816. dwc2_writel(hsotg, otgctl, GOTGCTL);
  1817. /* Clear the SRP success bit for FS-I2c */
  1818. hsotg->srp_success = 0;
  1819. /* Enable common interrupts */
  1820. dwc2_enable_common_interrupts(hsotg);
  1821. /*
  1822. * Do device or host initialization based on mode during PCD and
  1823. * HCD initialization
  1824. */
  1825. if (dwc2_is_host_mode(hsotg)) {
  1826. dev_dbg(hsotg->dev, "Host Mode\n");
  1827. hsotg->op_state = OTG_STATE_A_HOST;
  1828. } else {
  1829. dev_dbg(hsotg->dev, "Device Mode\n");
  1830. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  1831. }
  1832. return 0;
  1833. }
  1834. /**
  1835. * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
  1836. * Host mode
  1837. *
  1838. * @hsotg: Programming view of DWC_otg controller
  1839. *
  1840. * This function flushes the Tx and Rx FIFOs and flushes any entries in the
  1841. * request queues. Host channels are reset to ensure that they are ready for
  1842. * performing transfers.
  1843. */
  1844. static void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
  1845. {
  1846. u32 hcfg, hfir, otgctl, usbcfg;
  1847. dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
  1848. /* Set HS/FS Timeout Calibration to 7 (max available value).
  1849. * The number of PHY clocks that the application programs in
  1850. * this field is added to the high/full speed interpacket timeout
  1851. * duration in the core to account for any additional delays
  1852. * introduced by the PHY. This can be required, because the delay
  1853. * introduced by the PHY in generating the linestate condition
  1854. * can vary from one PHY to another.
  1855. */
  1856. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  1857. usbcfg |= GUSBCFG_TOUTCAL(7);
  1858. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  1859. /* Restart the Phy Clock */
  1860. dwc2_writel(hsotg, 0, PCGCTL);
  1861. /* Initialize Host Configuration Register */
  1862. dwc2_init_fs_ls_pclk_sel(hsotg);
  1863. if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
  1864. hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
  1865. hcfg = dwc2_readl(hsotg, HCFG);
  1866. hcfg |= HCFG_FSLSSUPP;
  1867. dwc2_writel(hsotg, hcfg, HCFG);
  1868. }
  1869. /*
  1870. * This bit allows dynamic reloading of the HFIR register during
  1871. * runtime. This bit needs to be programmed during initial configuration
  1872. * and its value must not be changed during runtime.
  1873. */
  1874. if (hsotg->params.reload_ctl) {
  1875. hfir = dwc2_readl(hsotg, HFIR);
  1876. hfir |= HFIR_RLDCTRL;
  1877. dwc2_writel(hsotg, hfir, HFIR);
  1878. }
  1879. if (hsotg->params.dma_desc_enable) {
  1880. u32 op_mode = hsotg->hw_params.op_mode;
  1881. if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
  1882. !hsotg->hw_params.dma_desc_enable ||
  1883. op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
  1884. op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
  1885. op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
  1886. dev_err(hsotg->dev,
  1887. "Hardware does not support descriptor DMA mode -\n");
  1888. dev_err(hsotg->dev,
  1889. "falling back to buffer DMA mode.\n");
  1890. hsotg->params.dma_desc_enable = false;
  1891. } else {
  1892. hcfg = dwc2_readl(hsotg, HCFG);
  1893. hcfg |= HCFG_DESCDMA;
  1894. dwc2_writel(hsotg, hcfg, HCFG);
  1895. }
  1896. }
  1897. /* Configure data FIFO sizes */
  1898. dwc2_config_fifos(hsotg);
  1899. /* TODO - check this */
  1900. /* Clear Host Set HNP Enable in the OTG Control Register */
  1901. otgctl = dwc2_readl(hsotg, GOTGCTL);
  1902. otgctl &= ~GOTGCTL_HSTSETHNPEN;
  1903. dwc2_writel(hsotg, otgctl, GOTGCTL);
  1904. /* Make sure the FIFOs are flushed */
  1905. dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
  1906. dwc2_flush_rx_fifo(hsotg);
  1907. /* Clear Host Set HNP Enable in the OTG Control Register */
  1908. otgctl = dwc2_readl(hsotg, GOTGCTL);
  1909. otgctl &= ~GOTGCTL_HSTSETHNPEN;
  1910. dwc2_writel(hsotg, otgctl, GOTGCTL);
  1911. if (!hsotg->params.dma_desc_enable) {
  1912. int num_channels, i;
  1913. u32 hcchar;
  1914. /* Flush out any leftover queued requests */
  1915. num_channels = hsotg->params.host_channels;
  1916. for (i = 0; i < num_channels; i++) {
  1917. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  1918. if (hcchar & HCCHAR_CHENA) {
  1919. hcchar &= ~HCCHAR_CHENA;
  1920. hcchar |= HCCHAR_CHDIS;
  1921. hcchar &= ~HCCHAR_EPDIR;
  1922. dwc2_writel(hsotg, hcchar, HCCHAR(i));
  1923. }
  1924. }
  1925. /* Halt all channels to put them into a known state */
  1926. for (i = 0; i < num_channels; i++) {
  1927. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  1928. if (hcchar & HCCHAR_CHENA) {
  1929. hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
  1930. hcchar &= ~HCCHAR_EPDIR;
  1931. dwc2_writel(hsotg, hcchar, HCCHAR(i));
  1932. dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
  1933. __func__, i);
  1934. if (dwc2_hsotg_wait_bit_clear(hsotg, HCCHAR(i),
  1935. HCCHAR_CHENA,
  1936. 1000)) {
  1937. dev_warn(hsotg->dev,
  1938. "Unable to clear enable on channel %d\n",
  1939. i);
  1940. }
  1941. }
  1942. }
  1943. }
  1944. /* Enable ACG feature in host mode, if supported */
  1945. dwc2_enable_acg(hsotg);
  1946. /* Turn on the vbus power */
  1947. dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
  1948. if (hsotg->op_state == OTG_STATE_A_HOST) {
  1949. u32 hprt0 = dwc2_read_hprt0(hsotg);
  1950. dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
  1951. !!(hprt0 & HPRT0_PWR));
  1952. if (!(hprt0 & HPRT0_PWR)) {
  1953. hprt0 |= HPRT0_PWR;
  1954. dwc2_writel(hsotg, hprt0, HPRT0);
  1955. }
  1956. }
  1957. dwc2_enable_host_interrupts(hsotg);
  1958. }
  1959. /*
  1960. * Initializes dynamic portions of the DWC_otg HCD state
  1961. *
  1962. * Must be called with interrupt disabled and spinlock held
  1963. */
  1964. static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
  1965. {
  1966. struct dwc2_host_chan *chan, *chan_tmp;
  1967. int num_channels;
  1968. int i;
  1969. hsotg->flags.d32 = 0;
  1970. hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
  1971. if (hsotg->params.uframe_sched) {
  1972. hsotg->available_host_channels =
  1973. hsotg->params.host_channels;
  1974. } else {
  1975. hsotg->non_periodic_channels = 0;
  1976. hsotg->periodic_channels = 0;
  1977. }
  1978. /*
  1979. * Put all channels in the free channel list and clean up channel
  1980. * states
  1981. */
  1982. list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
  1983. hc_list_entry)
  1984. list_del_init(&chan->hc_list_entry);
  1985. num_channels = hsotg->params.host_channels;
  1986. for (i = 0; i < num_channels; i++) {
  1987. chan = hsotg->hc_ptr_array[i];
  1988. list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
  1989. dwc2_hc_cleanup(hsotg, chan);
  1990. }
  1991. /* Initialize the DWC core for host mode operation */
  1992. dwc2_core_host_init(hsotg);
  1993. }
  1994. static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
  1995. struct dwc2_host_chan *chan,
  1996. struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
  1997. {
  1998. int hub_addr, hub_port;
  1999. chan->do_split = 1;
  2000. chan->xact_pos = qtd->isoc_split_pos;
  2001. chan->complete_split = qtd->complete_split;
  2002. dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
  2003. chan->hub_addr = (u8)hub_addr;
  2004. chan->hub_port = (u8)hub_port;
  2005. }
  2006. static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
  2007. struct dwc2_host_chan *chan,
  2008. struct dwc2_qtd *qtd)
  2009. {
  2010. struct dwc2_hcd_urb *urb = qtd->urb;
  2011. struct dwc2_hcd_iso_packet_desc *frame_desc;
  2012. switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
  2013. case USB_ENDPOINT_XFER_CONTROL:
  2014. chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
  2015. switch (qtd->control_phase) {
  2016. case DWC2_CONTROL_SETUP:
  2017. dev_vdbg(hsotg->dev, " Control setup transaction\n");
  2018. chan->do_ping = 0;
  2019. chan->ep_is_in = 0;
  2020. chan->data_pid_start = DWC2_HC_PID_SETUP;
  2021. if (hsotg->params.host_dma)
  2022. chan->xfer_dma = urb->setup_dma;
  2023. else
  2024. chan->xfer_buf = urb->setup_packet;
  2025. chan->xfer_len = 8;
  2026. break;
  2027. case DWC2_CONTROL_DATA:
  2028. dev_vdbg(hsotg->dev, " Control data transaction\n");
  2029. chan->data_pid_start = qtd->data_toggle;
  2030. break;
  2031. case DWC2_CONTROL_STATUS:
  2032. /*
  2033. * Direction is opposite of data direction or IN if no
  2034. * data
  2035. */
  2036. dev_vdbg(hsotg->dev, " Control status transaction\n");
  2037. if (urb->length == 0)
  2038. chan->ep_is_in = 1;
  2039. else
  2040. chan->ep_is_in =
  2041. dwc2_hcd_is_pipe_out(&urb->pipe_info);
  2042. if (chan->ep_is_in)
  2043. chan->do_ping = 0;
  2044. chan->data_pid_start = DWC2_HC_PID_DATA1;
  2045. chan->xfer_len = 0;
  2046. if (hsotg->params.host_dma)
  2047. chan->xfer_dma = hsotg->status_buf_dma;
  2048. else
  2049. chan->xfer_buf = hsotg->status_buf;
  2050. break;
  2051. }
  2052. break;
  2053. case USB_ENDPOINT_XFER_BULK:
  2054. chan->ep_type = USB_ENDPOINT_XFER_BULK;
  2055. break;
  2056. case USB_ENDPOINT_XFER_INT:
  2057. chan->ep_type = USB_ENDPOINT_XFER_INT;
  2058. break;
  2059. case USB_ENDPOINT_XFER_ISOC:
  2060. chan->ep_type = USB_ENDPOINT_XFER_ISOC;
  2061. if (hsotg->params.dma_desc_enable)
  2062. break;
  2063. frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  2064. frame_desc->status = 0;
  2065. if (hsotg->params.host_dma) {
  2066. chan->xfer_dma = urb->dma;
  2067. chan->xfer_dma += frame_desc->offset +
  2068. qtd->isoc_split_offset;
  2069. } else {
  2070. chan->xfer_buf = urb->buf;
  2071. chan->xfer_buf += frame_desc->offset +
  2072. qtd->isoc_split_offset;
  2073. }
  2074. chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
  2075. if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
  2076. if (chan->xfer_len <= 188)
  2077. chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
  2078. else
  2079. chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
  2080. }
  2081. break;
  2082. }
  2083. }
  2084. static int dwc2_alloc_split_dma_aligned_buf(struct dwc2_hsotg *hsotg,
  2085. struct dwc2_qh *qh,
  2086. struct dwc2_host_chan *chan)
  2087. {
  2088. if (!hsotg->unaligned_cache ||
  2089. chan->max_packet > DWC2_KMEM_UNALIGNED_BUF_SIZE)
  2090. return -ENOMEM;
  2091. if (!qh->dw_align_buf) {
  2092. qh->dw_align_buf = kmem_cache_alloc(hsotg->unaligned_cache,
  2093. GFP_ATOMIC | GFP_DMA);
  2094. if (!qh->dw_align_buf)
  2095. return -ENOMEM;
  2096. }
  2097. qh->dw_align_buf_dma = dma_map_single(hsotg->dev, qh->dw_align_buf,
  2098. DWC2_KMEM_UNALIGNED_BUF_SIZE,
  2099. DMA_FROM_DEVICE);
  2100. if (dma_mapping_error(hsotg->dev, qh->dw_align_buf_dma)) {
  2101. dev_err(hsotg->dev, "can't map align_buf\n");
  2102. chan->align_buf = 0;
  2103. return -EINVAL;
  2104. }
  2105. chan->align_buf = qh->dw_align_buf_dma;
  2106. return 0;
  2107. }
  2108. #define DWC2_USB_DMA_ALIGN 4
  2109. static void dwc2_free_dma_aligned_buffer(struct urb *urb)
  2110. {
  2111. void *stored_xfer_buffer;
  2112. size_t length;
  2113. if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
  2114. return;
  2115. /* Restore urb->transfer_buffer from the end of the allocated area */
  2116. memcpy(&stored_xfer_buffer,
  2117. PTR_ALIGN(urb->transfer_buffer + urb->transfer_buffer_length,
  2118. dma_get_cache_alignment()),
  2119. sizeof(urb->transfer_buffer));
  2120. if (usb_urb_dir_in(urb)) {
  2121. if (usb_pipeisoc(urb->pipe))
  2122. length = urb->transfer_buffer_length;
  2123. else
  2124. length = urb->actual_length;
  2125. memcpy(stored_xfer_buffer, urb->transfer_buffer, length);
  2126. }
  2127. kfree(urb->transfer_buffer);
  2128. urb->transfer_buffer = stored_xfer_buffer;
  2129. urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
  2130. }
  2131. static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
  2132. {
  2133. void *kmalloc_ptr;
  2134. size_t kmalloc_size;
  2135. if (urb->num_sgs || urb->sg ||
  2136. urb->transfer_buffer_length == 0 ||
  2137. !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
  2138. return 0;
  2139. /*
  2140. * Allocate a buffer with enough padding for original transfer_buffer
  2141. * pointer. This allocation is guaranteed to be aligned properly for
  2142. * DMA
  2143. */
  2144. kmalloc_size = urb->transfer_buffer_length +
  2145. (dma_get_cache_alignment() - 1) +
  2146. sizeof(urb->transfer_buffer);
  2147. kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
  2148. if (!kmalloc_ptr)
  2149. return -ENOMEM;
  2150. /*
  2151. * Position value of original urb->transfer_buffer pointer to the end
  2152. * of allocation for later referencing
  2153. */
  2154. memcpy(PTR_ALIGN(kmalloc_ptr + urb->transfer_buffer_length,
  2155. dma_get_cache_alignment()),
  2156. &urb->transfer_buffer, sizeof(urb->transfer_buffer));
  2157. if (usb_urb_dir_out(urb))
  2158. memcpy(kmalloc_ptr, urb->transfer_buffer,
  2159. urb->transfer_buffer_length);
  2160. urb->transfer_buffer = kmalloc_ptr;
  2161. urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
  2162. return 0;
  2163. }
  2164. static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  2165. gfp_t mem_flags)
  2166. {
  2167. int ret;
  2168. /* We assume setup_dma is always aligned; warn if not */
  2169. WARN_ON_ONCE(urb->setup_dma &&
  2170. (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
  2171. ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
  2172. if (ret)
  2173. return ret;
  2174. ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  2175. if (ret)
  2176. dwc2_free_dma_aligned_buffer(urb);
  2177. return ret;
  2178. }
  2179. static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  2180. {
  2181. usb_hcd_unmap_urb_for_dma(hcd, urb);
  2182. dwc2_free_dma_aligned_buffer(urb);
  2183. }
  2184. /**
  2185. * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
  2186. * channel and initializes the host channel to perform the transactions. The
  2187. * host channel is removed from the free list.
  2188. *
  2189. * @hsotg: The HCD state structure
  2190. * @qh: Transactions from the first QTD for this QH are selected and assigned
  2191. * to a free host channel
  2192. */
  2193. static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  2194. {
  2195. struct dwc2_host_chan *chan;
  2196. struct dwc2_hcd_urb *urb;
  2197. struct dwc2_qtd *qtd;
  2198. if (dbg_qh(qh))
  2199. dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
  2200. if (list_empty(&qh->qtd_list)) {
  2201. dev_dbg(hsotg->dev, "No QTDs in QH list\n");
  2202. return -ENOMEM;
  2203. }
  2204. if (list_empty(&hsotg->free_hc_list)) {
  2205. dev_dbg(hsotg->dev, "No free channel to assign\n");
  2206. return -ENOMEM;
  2207. }
  2208. chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
  2209. hc_list_entry);
  2210. /* Remove host channel from free list */
  2211. list_del_init(&chan->hc_list_entry);
  2212. qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
  2213. urb = qtd->urb;
  2214. qh->channel = chan;
  2215. qtd->in_process = 1;
  2216. /*
  2217. * Use usb_pipedevice to determine device address. This address is
  2218. * 0 before the SET_ADDRESS command and the correct address afterward.
  2219. */
  2220. chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
  2221. chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
  2222. chan->speed = qh->dev_speed;
  2223. chan->max_packet = qh->maxp;
  2224. chan->xfer_started = 0;
  2225. chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
  2226. chan->error_state = (qtd->error_count > 0);
  2227. chan->halt_on_queue = 0;
  2228. chan->halt_pending = 0;
  2229. chan->requests = 0;
  2230. /*
  2231. * The following values may be modified in the transfer type section
  2232. * below. The xfer_len value may be reduced when the transfer is
  2233. * started to accommodate the max widths of the XferSize and PktCnt
  2234. * fields in the HCTSIZn register.
  2235. */
  2236. chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
  2237. if (chan->ep_is_in)
  2238. chan->do_ping = 0;
  2239. else
  2240. chan->do_ping = qh->ping_state;
  2241. chan->data_pid_start = qh->data_toggle;
  2242. chan->multi_count = 1;
  2243. if (urb->actual_length > urb->length &&
  2244. !dwc2_hcd_is_pipe_in(&urb->pipe_info))
  2245. urb->actual_length = urb->length;
  2246. if (hsotg->params.host_dma)
  2247. chan->xfer_dma = urb->dma + urb->actual_length;
  2248. else
  2249. chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
  2250. chan->xfer_len = urb->length - urb->actual_length;
  2251. chan->xfer_count = 0;
  2252. /* Set the split attributes if required */
  2253. if (qh->do_split)
  2254. dwc2_hc_init_split(hsotg, chan, qtd, urb);
  2255. else
  2256. chan->do_split = 0;
  2257. /* Set the transfer attributes */
  2258. dwc2_hc_init_xfer(hsotg, chan, qtd);
  2259. /* For non-dword aligned buffers */
  2260. if (hsotg->params.host_dma && qh->do_split &&
  2261. chan->ep_is_in && (chan->xfer_dma & 0x3)) {
  2262. dev_vdbg(hsotg->dev, "Non-aligned buffer\n");
  2263. if (dwc2_alloc_split_dma_aligned_buf(hsotg, qh, chan)) {
  2264. dev_err(hsotg->dev,
  2265. "Failed to allocate memory to handle non-aligned buffer\n");
  2266. /* Add channel back to free list */
  2267. chan->align_buf = 0;
  2268. chan->multi_count = 0;
  2269. list_add_tail(&chan->hc_list_entry,
  2270. &hsotg->free_hc_list);
  2271. qtd->in_process = 0;
  2272. qh->channel = NULL;
  2273. return -ENOMEM;
  2274. }
  2275. } else {
  2276. /*
  2277. * We assume that DMA is always aligned in non-split
  2278. * case or split out case. Warn if not.
  2279. */
  2280. WARN_ON_ONCE(hsotg->params.host_dma &&
  2281. (chan->xfer_dma & 0x3));
  2282. chan->align_buf = 0;
  2283. }
  2284. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  2285. chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  2286. /*
  2287. * This value may be modified when the transfer is started
  2288. * to reflect the actual transfer length
  2289. */
  2290. chan->multi_count = qh->maxp_mult;
  2291. if (hsotg->params.dma_desc_enable) {
  2292. chan->desc_list_addr = qh->desc_list_dma;
  2293. chan->desc_list_sz = qh->desc_list_sz;
  2294. }
  2295. dwc2_hc_init(hsotg, chan);
  2296. chan->qh = qh;
  2297. return 0;
  2298. }
  2299. /**
  2300. * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
  2301. * schedule and assigns them to available host channels. Called from the HCD
  2302. * interrupt handler functions.
  2303. *
  2304. * @hsotg: The HCD state structure
  2305. *
  2306. * Return: The types of new transactions that were assigned to host channels
  2307. */
  2308. enum dwc2_transaction_type dwc2_hcd_select_transactions(
  2309. struct dwc2_hsotg *hsotg)
  2310. {
  2311. enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
  2312. struct list_head *qh_ptr;
  2313. struct dwc2_qh *qh;
  2314. int num_channels;
  2315. #ifdef DWC2_DEBUG_SOF
  2316. dev_vdbg(hsotg->dev, " Select Transactions\n");
  2317. #endif
  2318. /* Process entries in the periodic ready list */
  2319. qh_ptr = hsotg->periodic_sched_ready.next;
  2320. while (qh_ptr != &hsotg->periodic_sched_ready) {
  2321. if (list_empty(&hsotg->free_hc_list))
  2322. break;
  2323. if (hsotg->params.uframe_sched) {
  2324. if (hsotg->available_host_channels <= 1)
  2325. break;
  2326. hsotg->available_host_channels--;
  2327. }
  2328. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  2329. if (dwc2_assign_and_init_hc(hsotg, qh))
  2330. break;
  2331. /*
  2332. * Move the QH from the periodic ready schedule to the
  2333. * periodic assigned schedule
  2334. */
  2335. qh_ptr = qh_ptr->next;
  2336. list_move_tail(&qh->qh_list_entry,
  2337. &hsotg->periodic_sched_assigned);
  2338. ret_val = DWC2_TRANSACTION_PERIODIC;
  2339. }
  2340. /*
  2341. * Process entries in the inactive portion of the non-periodic
  2342. * schedule. Some free host channels may not be used if they are
  2343. * reserved for periodic transfers.
  2344. */
  2345. num_channels = hsotg->params.host_channels;
  2346. qh_ptr = hsotg->non_periodic_sched_inactive.next;
  2347. while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
  2348. if (!hsotg->params.uframe_sched &&
  2349. hsotg->non_periodic_channels >= num_channels -
  2350. hsotg->periodic_channels)
  2351. break;
  2352. if (list_empty(&hsotg->free_hc_list))
  2353. break;
  2354. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  2355. if (hsotg->params.uframe_sched) {
  2356. if (hsotg->available_host_channels < 1)
  2357. break;
  2358. hsotg->available_host_channels--;
  2359. }
  2360. if (dwc2_assign_and_init_hc(hsotg, qh))
  2361. break;
  2362. /*
  2363. * Move the QH from the non-periodic inactive schedule to the
  2364. * non-periodic active schedule
  2365. */
  2366. qh_ptr = qh_ptr->next;
  2367. list_move_tail(&qh->qh_list_entry,
  2368. &hsotg->non_periodic_sched_active);
  2369. if (ret_val == DWC2_TRANSACTION_NONE)
  2370. ret_val = DWC2_TRANSACTION_NON_PERIODIC;
  2371. else
  2372. ret_val = DWC2_TRANSACTION_ALL;
  2373. if (!hsotg->params.uframe_sched)
  2374. hsotg->non_periodic_channels++;
  2375. }
  2376. return ret_val;
  2377. }
  2378. /**
  2379. * dwc2_queue_transaction() - Attempts to queue a single transaction request for
  2380. * a host channel associated with either a periodic or non-periodic transfer
  2381. *
  2382. * @hsotg: The HCD state structure
  2383. * @chan: Host channel descriptor associated with either a periodic or
  2384. * non-periodic transfer
  2385. * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
  2386. * for periodic transfers or the non-periodic Tx FIFO
  2387. * for non-periodic transfers
  2388. *
  2389. * Return: 1 if a request is queued and more requests may be needed to
  2390. * complete the transfer, 0 if no more requests are required for this
  2391. * transfer, -1 if there is insufficient space in the Tx FIFO
  2392. *
  2393. * This function assumes that there is space available in the appropriate
  2394. * request queue. For an OUT transfer or SETUP transaction in Slave mode,
  2395. * it checks whether space is available in the appropriate Tx FIFO.
  2396. *
  2397. * Must be called with interrupt disabled and spinlock held
  2398. */
  2399. static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
  2400. struct dwc2_host_chan *chan,
  2401. u16 fifo_dwords_avail)
  2402. {
  2403. int retval = 0;
  2404. if (chan->do_split)
  2405. /* Put ourselves on the list to keep order straight */
  2406. list_move_tail(&chan->split_order_list_entry,
  2407. &hsotg->split_order);
  2408. if (hsotg->params.host_dma && chan->qh) {
  2409. if (hsotg->params.dma_desc_enable) {
  2410. if (!chan->xfer_started ||
  2411. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  2412. dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
  2413. chan->qh->ping_state = 0;
  2414. }
  2415. } else if (!chan->xfer_started) {
  2416. dwc2_hc_start_transfer(hsotg, chan);
  2417. chan->qh->ping_state = 0;
  2418. }
  2419. } else if (chan->halt_pending) {
  2420. /* Don't queue a request if the channel has been halted */
  2421. } else if (chan->halt_on_queue) {
  2422. dwc2_hc_halt(hsotg, chan, chan->halt_status);
  2423. } else if (chan->do_ping) {
  2424. if (!chan->xfer_started)
  2425. dwc2_hc_start_transfer(hsotg, chan);
  2426. } else if (!chan->ep_is_in ||
  2427. chan->data_pid_start == DWC2_HC_PID_SETUP) {
  2428. if ((fifo_dwords_avail * 4) >= chan->max_packet) {
  2429. if (!chan->xfer_started) {
  2430. dwc2_hc_start_transfer(hsotg, chan);
  2431. retval = 1;
  2432. } else {
  2433. retval = dwc2_hc_continue_transfer(hsotg, chan);
  2434. }
  2435. } else {
  2436. retval = -1;
  2437. }
  2438. } else {
  2439. if (!chan->xfer_started) {
  2440. dwc2_hc_start_transfer(hsotg, chan);
  2441. retval = 1;
  2442. } else {
  2443. retval = dwc2_hc_continue_transfer(hsotg, chan);
  2444. }
  2445. }
  2446. return retval;
  2447. }
  2448. /*
  2449. * Processes periodic channels for the next frame and queues transactions for
  2450. * these channels to the DWC_otg controller. After queueing transactions, the
  2451. * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
  2452. * to queue as Periodic Tx FIFO or request queue space becomes available.
  2453. * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
  2454. *
  2455. * Must be called with interrupt disabled and spinlock held
  2456. */
  2457. static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
  2458. {
  2459. struct list_head *qh_ptr;
  2460. struct dwc2_qh *qh;
  2461. u32 tx_status;
  2462. u32 fspcavail;
  2463. u32 gintmsk;
  2464. int status;
  2465. bool no_queue_space = false;
  2466. bool no_fifo_space = false;
  2467. u32 qspcavail;
  2468. /* If empty list then just adjust interrupt enables */
  2469. if (list_empty(&hsotg->periodic_sched_assigned))
  2470. goto exit;
  2471. if (dbg_perio())
  2472. dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
  2473. tx_status = dwc2_readl(hsotg, HPTXSTS);
  2474. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2475. TXSTS_QSPCAVAIL_SHIFT;
  2476. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2477. TXSTS_FSPCAVAIL_SHIFT;
  2478. if (dbg_perio()) {
  2479. dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n",
  2480. qspcavail);
  2481. dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n",
  2482. fspcavail);
  2483. }
  2484. qh_ptr = hsotg->periodic_sched_assigned.next;
  2485. while (qh_ptr != &hsotg->periodic_sched_assigned) {
  2486. tx_status = dwc2_readl(hsotg, HPTXSTS);
  2487. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2488. TXSTS_QSPCAVAIL_SHIFT;
  2489. if (qspcavail == 0) {
  2490. no_queue_space = true;
  2491. break;
  2492. }
  2493. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  2494. if (!qh->channel) {
  2495. qh_ptr = qh_ptr->next;
  2496. continue;
  2497. }
  2498. /* Make sure EP's TT buffer is clean before queueing qtds */
  2499. if (qh->tt_buffer_dirty) {
  2500. qh_ptr = qh_ptr->next;
  2501. continue;
  2502. }
  2503. /*
  2504. * Set a flag if we're queuing high-bandwidth in slave mode.
  2505. * The flag prevents any halts to get into the request queue in
  2506. * the middle of multiple high-bandwidth packets getting queued.
  2507. */
  2508. if (!hsotg->params.host_dma &&
  2509. qh->channel->multi_count > 1)
  2510. hsotg->queuing_high_bandwidth = 1;
  2511. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2512. TXSTS_FSPCAVAIL_SHIFT;
  2513. status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
  2514. if (status < 0) {
  2515. no_fifo_space = true;
  2516. break;
  2517. }
  2518. /*
  2519. * In Slave mode, stay on the current transfer until there is
  2520. * nothing more to do or the high-bandwidth request count is
  2521. * reached. In DMA mode, only need to queue one request. The
  2522. * controller automatically handles multiple packets for
  2523. * high-bandwidth transfers.
  2524. */
  2525. if (hsotg->params.host_dma || status == 0 ||
  2526. qh->channel->requests == qh->channel->multi_count) {
  2527. qh_ptr = qh_ptr->next;
  2528. /*
  2529. * Move the QH from the periodic assigned schedule to
  2530. * the periodic queued schedule
  2531. */
  2532. list_move_tail(&qh->qh_list_entry,
  2533. &hsotg->periodic_sched_queued);
  2534. /* done queuing high bandwidth */
  2535. hsotg->queuing_high_bandwidth = 0;
  2536. }
  2537. }
  2538. exit:
  2539. if (no_queue_space || no_fifo_space ||
  2540. (!hsotg->params.host_dma &&
  2541. !list_empty(&hsotg->periodic_sched_assigned))) {
  2542. /*
  2543. * May need to queue more transactions as the request
  2544. * queue or Tx FIFO empties. Enable the periodic Tx
  2545. * FIFO empty interrupt. (Always use the half-empty
  2546. * level to ensure that new requests are loaded as
  2547. * soon as possible.)
  2548. */
  2549. gintmsk = dwc2_readl(hsotg, GINTMSK);
  2550. if (!(gintmsk & GINTSTS_PTXFEMP)) {
  2551. gintmsk |= GINTSTS_PTXFEMP;
  2552. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2553. }
  2554. } else {
  2555. /*
  2556. * Disable the Tx FIFO empty interrupt since there are
  2557. * no more transactions that need to be queued right
  2558. * now. This function is called from interrupt
  2559. * handlers to queue more transactions as transfer
  2560. * states change.
  2561. */
  2562. gintmsk = dwc2_readl(hsotg, GINTMSK);
  2563. if (gintmsk & GINTSTS_PTXFEMP) {
  2564. gintmsk &= ~GINTSTS_PTXFEMP;
  2565. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2566. }
  2567. }
  2568. }
  2569. /*
  2570. * Processes active non-periodic channels and queues transactions for these
  2571. * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  2572. * FIFO Empty interrupt is enabled if there are more transactions to queue as
  2573. * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  2574. * FIFO Empty interrupt is disabled.
  2575. *
  2576. * Must be called with interrupt disabled and spinlock held
  2577. */
  2578. static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
  2579. {
  2580. struct list_head *orig_qh_ptr;
  2581. struct dwc2_qh *qh;
  2582. u32 tx_status;
  2583. u32 qspcavail;
  2584. u32 fspcavail;
  2585. u32 gintmsk;
  2586. int status;
  2587. int no_queue_space = 0;
  2588. int no_fifo_space = 0;
  2589. int more_to_do = 0;
  2590. dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
  2591. tx_status = dwc2_readl(hsotg, GNPTXSTS);
  2592. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2593. TXSTS_QSPCAVAIL_SHIFT;
  2594. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2595. TXSTS_FSPCAVAIL_SHIFT;
  2596. dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
  2597. qspcavail);
  2598. dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
  2599. fspcavail);
  2600. /*
  2601. * Keep track of the starting point. Skip over the start-of-list
  2602. * entry.
  2603. */
  2604. if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
  2605. hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
  2606. orig_qh_ptr = hsotg->non_periodic_qh_ptr;
  2607. /*
  2608. * Process once through the active list or until no more space is
  2609. * available in the request queue or the Tx FIFO
  2610. */
  2611. do {
  2612. tx_status = dwc2_readl(hsotg, GNPTXSTS);
  2613. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2614. TXSTS_QSPCAVAIL_SHIFT;
  2615. if (!hsotg->params.host_dma && qspcavail == 0) {
  2616. no_queue_space = 1;
  2617. break;
  2618. }
  2619. qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
  2620. qh_list_entry);
  2621. if (!qh->channel)
  2622. goto next;
  2623. /* Make sure EP's TT buffer is clean before queueing qtds */
  2624. if (qh->tt_buffer_dirty)
  2625. goto next;
  2626. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2627. TXSTS_FSPCAVAIL_SHIFT;
  2628. status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
  2629. if (status > 0) {
  2630. more_to_do = 1;
  2631. } else if (status < 0) {
  2632. no_fifo_space = 1;
  2633. break;
  2634. }
  2635. next:
  2636. /* Advance to next QH, skipping start-of-list entry */
  2637. hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
  2638. if (hsotg->non_periodic_qh_ptr ==
  2639. &hsotg->non_periodic_sched_active)
  2640. hsotg->non_periodic_qh_ptr =
  2641. hsotg->non_periodic_qh_ptr->next;
  2642. } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
  2643. if (!hsotg->params.host_dma) {
  2644. tx_status = dwc2_readl(hsotg, GNPTXSTS);
  2645. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2646. TXSTS_QSPCAVAIL_SHIFT;
  2647. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2648. TXSTS_FSPCAVAIL_SHIFT;
  2649. dev_vdbg(hsotg->dev,
  2650. " NP Tx Req Queue Space Avail (after queue): %d\n",
  2651. qspcavail);
  2652. dev_vdbg(hsotg->dev,
  2653. " NP Tx FIFO Space Avail (after queue): %d\n",
  2654. fspcavail);
  2655. if (more_to_do || no_queue_space || no_fifo_space) {
  2656. /*
  2657. * May need to queue more transactions as the request
  2658. * queue or Tx FIFO empties. Enable the non-periodic
  2659. * Tx FIFO empty interrupt. (Always use the half-empty
  2660. * level to ensure that new requests are loaded as
  2661. * soon as possible.)
  2662. */
  2663. gintmsk = dwc2_readl(hsotg, GINTMSK);
  2664. gintmsk |= GINTSTS_NPTXFEMP;
  2665. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2666. } else {
  2667. /*
  2668. * Disable the Tx FIFO empty interrupt since there are
  2669. * no more transactions that need to be queued right
  2670. * now. This function is called from interrupt
  2671. * handlers to queue more transactions as transfer
  2672. * states change.
  2673. */
  2674. gintmsk = dwc2_readl(hsotg, GINTMSK);
  2675. gintmsk &= ~GINTSTS_NPTXFEMP;
  2676. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2677. }
  2678. }
  2679. }
  2680. /**
  2681. * dwc2_hcd_queue_transactions() - Processes the currently active host channels
  2682. * and queues transactions for these channels to the DWC_otg controller. Called
  2683. * from the HCD interrupt handler functions.
  2684. *
  2685. * @hsotg: The HCD state structure
  2686. * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
  2687. * or both)
  2688. *
  2689. * Must be called with interrupt disabled and spinlock held
  2690. */
  2691. void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
  2692. enum dwc2_transaction_type tr_type)
  2693. {
  2694. #ifdef DWC2_DEBUG_SOF
  2695. dev_vdbg(hsotg->dev, "Queue Transactions\n");
  2696. #endif
  2697. /* Process host channels associated with periodic transfers */
  2698. if (tr_type == DWC2_TRANSACTION_PERIODIC ||
  2699. tr_type == DWC2_TRANSACTION_ALL)
  2700. dwc2_process_periodic_channels(hsotg);
  2701. /* Process host channels associated with non-periodic transfers */
  2702. if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
  2703. tr_type == DWC2_TRANSACTION_ALL) {
  2704. if (!list_empty(&hsotg->non_periodic_sched_active)) {
  2705. dwc2_process_non_periodic_channels(hsotg);
  2706. } else {
  2707. /*
  2708. * Ensure NP Tx FIFO empty interrupt is disabled when
  2709. * there are no non-periodic transfers to process
  2710. */
  2711. u32 gintmsk = dwc2_readl(hsotg, GINTMSK);
  2712. gintmsk &= ~GINTSTS_NPTXFEMP;
  2713. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2714. }
  2715. }
  2716. }
  2717. static void dwc2_conn_id_status_change(struct work_struct *work)
  2718. {
  2719. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  2720. wf_otg);
  2721. u32 count = 0;
  2722. u32 gotgctl;
  2723. unsigned long flags;
  2724. dev_dbg(hsotg->dev, "%s()\n", __func__);
  2725. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  2726. dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
  2727. dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
  2728. !!(gotgctl & GOTGCTL_CONID_B));
  2729. /* B-Device connector (Device Mode) */
  2730. if (gotgctl & GOTGCTL_CONID_B) {
  2731. dwc2_vbus_supply_exit(hsotg);
  2732. /* Wait for switch to device mode */
  2733. dev_dbg(hsotg->dev, "connId B\n");
  2734. if (hsotg->bus_suspended) {
  2735. dev_info(hsotg->dev,
  2736. "Do port resume before switching to device mode\n");
  2737. dwc2_port_resume(hsotg);
  2738. }
  2739. while (!dwc2_is_device_mode(hsotg)) {
  2740. dev_info(hsotg->dev,
  2741. "Waiting for Peripheral Mode, Mode=%s\n",
  2742. dwc2_is_host_mode(hsotg) ? "Host" :
  2743. "Peripheral");
  2744. msleep(20);
  2745. /*
  2746. * Sometimes the initial GOTGCTRL read is wrong, so
  2747. * check it again and jump to host mode if that was
  2748. * the case.
  2749. */
  2750. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  2751. if (!(gotgctl & GOTGCTL_CONID_B))
  2752. goto host;
  2753. if (++count > 250)
  2754. break;
  2755. }
  2756. if (count > 250)
  2757. dev_err(hsotg->dev,
  2758. "Connection id status change timed out\n");
  2759. /*
  2760. * Exit Partial Power Down without restoring registers.
  2761. * No need to check the return value as registers
  2762. * are not being restored.
  2763. */
  2764. if (hsotg->in_ppd && hsotg->lx_state == DWC2_L2)
  2765. dwc2_exit_partial_power_down(hsotg, 0, false);
  2766. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  2767. dwc2_core_init(hsotg, false);
  2768. dwc2_enable_global_interrupts(hsotg);
  2769. spin_lock_irqsave(&hsotg->lock, flags);
  2770. dwc2_hsotg_core_init_disconnected(hsotg, false);
  2771. spin_unlock_irqrestore(&hsotg->lock, flags);
  2772. /* Enable ACG feature in device mode,if supported */
  2773. dwc2_enable_acg(hsotg);
  2774. dwc2_hsotg_core_connect(hsotg);
  2775. } else {
  2776. host:
  2777. /* A-Device connector (Host Mode) */
  2778. dev_dbg(hsotg->dev, "connId A\n");
  2779. while (!dwc2_is_host_mode(hsotg)) {
  2780. dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
  2781. dwc2_is_host_mode(hsotg) ?
  2782. "Host" : "Peripheral");
  2783. msleep(20);
  2784. if (++count > 250)
  2785. break;
  2786. }
  2787. if (count > 250)
  2788. dev_err(hsotg->dev,
  2789. "Connection id status change timed out\n");
  2790. spin_lock_irqsave(&hsotg->lock, flags);
  2791. dwc2_hsotg_disconnect(hsotg);
  2792. spin_unlock_irqrestore(&hsotg->lock, flags);
  2793. hsotg->op_state = OTG_STATE_A_HOST;
  2794. /* Initialize the Core for Host mode */
  2795. dwc2_core_init(hsotg, false);
  2796. dwc2_enable_global_interrupts(hsotg);
  2797. dwc2_hcd_start(hsotg);
  2798. }
  2799. }
  2800. static void dwc2_wakeup_detected(struct timer_list *t)
  2801. {
  2802. struct dwc2_hsotg *hsotg = from_timer(hsotg, t, wkp_timer);
  2803. u32 hprt0;
  2804. dev_dbg(hsotg->dev, "%s()\n", __func__);
  2805. /*
  2806. * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  2807. * so that OPT tests pass with all PHYs.)
  2808. */
  2809. hprt0 = dwc2_read_hprt0(hsotg);
  2810. dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
  2811. hprt0 &= ~HPRT0_RES;
  2812. dwc2_writel(hsotg, hprt0, HPRT0);
  2813. dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
  2814. dwc2_readl(hsotg, HPRT0));
  2815. dwc2_hcd_rem_wakeup(hsotg);
  2816. hsotg->bus_suspended = false;
  2817. /* Change to L0 state */
  2818. hsotg->lx_state = DWC2_L0;
  2819. }
  2820. static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
  2821. {
  2822. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  2823. return hcd->self.b_hnp_enable;
  2824. }
  2825. /**
  2826. * dwc2_port_suspend() - Put controller in suspend mode for host.
  2827. *
  2828. * @hsotg: Programming view of the DWC_otg controller
  2829. * @windex: The control request wIndex field
  2830. *
  2831. * Return: non-zero if failed to enter suspend mode for host.
  2832. *
  2833. * This function is for entering Host mode suspend.
  2834. * Must NOT be called with interrupt disabled or spinlock held.
  2835. */
  2836. int dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
  2837. {
  2838. unsigned long flags;
  2839. u32 pcgctl;
  2840. u32 gotgctl;
  2841. int ret = 0;
  2842. dev_dbg(hsotg->dev, "%s()\n", __func__);
  2843. spin_lock_irqsave(&hsotg->lock, flags);
  2844. if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
  2845. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  2846. gotgctl |= GOTGCTL_HSTSETHNPEN;
  2847. dwc2_writel(hsotg, gotgctl, GOTGCTL);
  2848. hsotg->op_state = OTG_STATE_A_SUSPEND;
  2849. }
  2850. switch (hsotg->params.power_down) {
  2851. case DWC2_POWER_DOWN_PARAM_PARTIAL:
  2852. ret = dwc2_enter_partial_power_down(hsotg);
  2853. if (ret)
  2854. dev_err(hsotg->dev,
  2855. "enter partial_power_down failed.\n");
  2856. break;
  2857. case DWC2_POWER_DOWN_PARAM_HIBERNATION:
  2858. /*
  2859. * Perform spin unlock and lock because in
  2860. * "dwc2_host_enter_hibernation()" function there is a spinlock
  2861. * logic which prevents servicing of any IRQ during entering
  2862. * hibernation.
  2863. */
  2864. spin_unlock_irqrestore(&hsotg->lock, flags);
  2865. ret = dwc2_enter_hibernation(hsotg, 1);
  2866. if (ret)
  2867. dev_err(hsotg->dev, "enter hibernation failed.\n");
  2868. spin_lock_irqsave(&hsotg->lock, flags);
  2869. break;
  2870. case DWC2_POWER_DOWN_PARAM_NONE:
  2871. /*
  2872. * If not hibernation nor partial power down are supported,
  2873. * clock gating is used to save power.
  2874. */
  2875. if (!hsotg->params.no_clock_gating)
  2876. dwc2_host_enter_clock_gating(hsotg);
  2877. break;
  2878. }
  2879. /* For HNP the bus must be suspended for at least 200ms */
  2880. if (dwc2_host_is_b_hnp_enabled(hsotg)) {
  2881. pcgctl = dwc2_readl(hsotg, PCGCTL);
  2882. pcgctl &= ~PCGCTL_STOPPCLK;
  2883. dwc2_writel(hsotg, pcgctl, PCGCTL);
  2884. spin_unlock_irqrestore(&hsotg->lock, flags);
  2885. msleep(200);
  2886. } else {
  2887. spin_unlock_irqrestore(&hsotg->lock, flags);
  2888. }
  2889. return ret;
  2890. }
  2891. /**
  2892. * dwc2_port_resume() - Exit controller from suspend mode for host.
  2893. *
  2894. * @hsotg: Programming view of the DWC_otg controller
  2895. *
  2896. * Return: non-zero if failed to exit suspend mode for host.
  2897. *
  2898. * This function is for exiting Host mode suspend.
  2899. * Must NOT be called with interrupt disabled or spinlock held.
  2900. */
  2901. int dwc2_port_resume(struct dwc2_hsotg *hsotg)
  2902. {
  2903. unsigned long flags;
  2904. int ret = 0;
  2905. spin_lock_irqsave(&hsotg->lock, flags);
  2906. switch (hsotg->params.power_down) {
  2907. case DWC2_POWER_DOWN_PARAM_PARTIAL:
  2908. ret = dwc2_exit_partial_power_down(hsotg, 0, true);
  2909. if (ret)
  2910. dev_err(hsotg->dev,
  2911. "exit partial_power_down failed.\n");
  2912. break;
  2913. case DWC2_POWER_DOWN_PARAM_HIBERNATION:
  2914. /* Exit host hibernation. */
  2915. ret = dwc2_exit_hibernation(hsotg, 0, 0, 1);
  2916. if (ret)
  2917. dev_err(hsotg->dev, "exit hibernation failed.\n");
  2918. break;
  2919. case DWC2_POWER_DOWN_PARAM_NONE:
  2920. /*
  2921. * If not hibernation nor partial power down are supported,
  2922. * port resume is done using the clock gating programming flow.
  2923. */
  2924. spin_unlock_irqrestore(&hsotg->lock, flags);
  2925. dwc2_host_exit_clock_gating(hsotg, 0);
  2926. spin_lock_irqsave(&hsotg->lock, flags);
  2927. break;
  2928. }
  2929. spin_unlock_irqrestore(&hsotg->lock, flags);
  2930. return ret;
  2931. }
  2932. /* Handles hub class-specific requests */
  2933. static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
  2934. u16 wvalue, u16 windex, char *buf, u16 wlength)
  2935. {
  2936. struct usb_hub_descriptor *hub_desc;
  2937. int retval = 0;
  2938. u32 hprt0;
  2939. u32 port_status;
  2940. u32 speed;
  2941. u32 pcgctl;
  2942. u32 pwr;
  2943. switch (typereq) {
  2944. case ClearHubFeature:
  2945. dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
  2946. switch (wvalue) {
  2947. case C_HUB_LOCAL_POWER:
  2948. case C_HUB_OVER_CURRENT:
  2949. /* Nothing required here */
  2950. break;
  2951. default:
  2952. retval = -EINVAL;
  2953. dev_err(hsotg->dev,
  2954. "ClearHubFeature request %1xh unknown\n",
  2955. wvalue);
  2956. }
  2957. break;
  2958. case ClearPortFeature:
  2959. if (wvalue != USB_PORT_FEAT_L1)
  2960. if (!windex || windex > 1)
  2961. goto error;
  2962. switch (wvalue) {
  2963. case USB_PORT_FEAT_ENABLE:
  2964. dev_dbg(hsotg->dev,
  2965. "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  2966. hprt0 = dwc2_read_hprt0(hsotg);
  2967. hprt0 |= HPRT0_ENA;
  2968. dwc2_writel(hsotg, hprt0, HPRT0);
  2969. break;
  2970. case USB_PORT_FEAT_SUSPEND:
  2971. dev_dbg(hsotg->dev,
  2972. "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  2973. if (hsotg->bus_suspended)
  2974. retval = dwc2_port_resume(hsotg);
  2975. break;
  2976. case USB_PORT_FEAT_POWER:
  2977. dev_dbg(hsotg->dev,
  2978. "ClearPortFeature USB_PORT_FEAT_POWER\n");
  2979. hprt0 = dwc2_read_hprt0(hsotg);
  2980. pwr = hprt0 & HPRT0_PWR;
  2981. hprt0 &= ~HPRT0_PWR;
  2982. dwc2_writel(hsotg, hprt0, HPRT0);
  2983. if (pwr)
  2984. dwc2_vbus_supply_exit(hsotg);
  2985. break;
  2986. case USB_PORT_FEAT_INDICATOR:
  2987. dev_dbg(hsotg->dev,
  2988. "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  2989. /* Port indicator not supported */
  2990. break;
  2991. case USB_PORT_FEAT_C_CONNECTION:
  2992. /*
  2993. * Clears driver's internal Connect Status Change flag
  2994. */
  2995. dev_dbg(hsotg->dev,
  2996. "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  2997. hsotg->flags.b.port_connect_status_change = 0;
  2998. break;
  2999. case USB_PORT_FEAT_C_RESET:
  3000. /* Clears driver's internal Port Reset Change flag */
  3001. dev_dbg(hsotg->dev,
  3002. "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  3003. hsotg->flags.b.port_reset_change = 0;
  3004. break;
  3005. case USB_PORT_FEAT_C_ENABLE:
  3006. /*
  3007. * Clears the driver's internal Port Enable/Disable
  3008. * Change flag
  3009. */
  3010. dev_dbg(hsotg->dev,
  3011. "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  3012. hsotg->flags.b.port_enable_change = 0;
  3013. break;
  3014. case USB_PORT_FEAT_C_SUSPEND:
  3015. /*
  3016. * Clears the driver's internal Port Suspend Change
  3017. * flag, which is set when resume signaling on the host
  3018. * port is complete
  3019. */
  3020. dev_dbg(hsotg->dev,
  3021. "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  3022. hsotg->flags.b.port_suspend_change = 0;
  3023. break;
  3024. case USB_PORT_FEAT_C_PORT_L1:
  3025. dev_dbg(hsotg->dev,
  3026. "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
  3027. hsotg->flags.b.port_l1_change = 0;
  3028. break;
  3029. case USB_PORT_FEAT_C_OVER_CURRENT:
  3030. dev_dbg(hsotg->dev,
  3031. "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  3032. hsotg->flags.b.port_over_current_change = 0;
  3033. break;
  3034. default:
  3035. retval = -EINVAL;
  3036. dev_err(hsotg->dev,
  3037. "ClearPortFeature request %1xh unknown or unsupported\n",
  3038. wvalue);
  3039. }
  3040. break;
  3041. case GetHubDescriptor:
  3042. dev_dbg(hsotg->dev, "GetHubDescriptor\n");
  3043. hub_desc = (struct usb_hub_descriptor *)buf;
  3044. hub_desc->bDescLength = 9;
  3045. hub_desc->bDescriptorType = USB_DT_HUB;
  3046. hub_desc->bNbrPorts = 1;
  3047. hub_desc->wHubCharacteristics =
  3048. cpu_to_le16(HUB_CHAR_COMMON_LPSM |
  3049. HUB_CHAR_INDV_PORT_OCPM);
  3050. hub_desc->bPwrOn2PwrGood = 1;
  3051. hub_desc->bHubContrCurrent = 0;
  3052. hub_desc->u.hs.DeviceRemovable[0] = 0;
  3053. hub_desc->u.hs.DeviceRemovable[1] = 0xff;
  3054. break;
  3055. case GetHubStatus:
  3056. dev_dbg(hsotg->dev, "GetHubStatus\n");
  3057. memset(buf, 0, 4);
  3058. break;
  3059. case GetPortStatus:
  3060. dev_vdbg(hsotg->dev,
  3061. "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
  3062. hsotg->flags.d32);
  3063. if (!windex || windex > 1)
  3064. goto error;
  3065. port_status = 0;
  3066. if (hsotg->flags.b.port_connect_status_change)
  3067. port_status |= USB_PORT_STAT_C_CONNECTION << 16;
  3068. if (hsotg->flags.b.port_enable_change)
  3069. port_status |= USB_PORT_STAT_C_ENABLE << 16;
  3070. if (hsotg->flags.b.port_suspend_change)
  3071. port_status |= USB_PORT_STAT_C_SUSPEND << 16;
  3072. if (hsotg->flags.b.port_l1_change)
  3073. port_status |= USB_PORT_STAT_C_L1 << 16;
  3074. if (hsotg->flags.b.port_reset_change)
  3075. port_status |= USB_PORT_STAT_C_RESET << 16;
  3076. if (hsotg->flags.b.port_over_current_change) {
  3077. dev_warn(hsotg->dev, "Overcurrent change detected\n");
  3078. port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  3079. }
  3080. if (!hsotg->flags.b.port_connect_status) {
  3081. /*
  3082. * The port is disconnected, which means the core is
  3083. * either in device mode or it soon will be. Just
  3084. * return 0's for the remainder of the port status
  3085. * since the port register can't be read if the core
  3086. * is in device mode.
  3087. */
  3088. *(__le32 *)buf = cpu_to_le32(port_status);
  3089. break;
  3090. }
  3091. hprt0 = dwc2_readl(hsotg, HPRT0);
  3092. dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
  3093. if (hprt0 & HPRT0_CONNSTS)
  3094. port_status |= USB_PORT_STAT_CONNECTION;
  3095. if (hprt0 & HPRT0_ENA)
  3096. port_status |= USB_PORT_STAT_ENABLE;
  3097. if (hprt0 & HPRT0_SUSP)
  3098. port_status |= USB_PORT_STAT_SUSPEND;
  3099. if (hprt0 & HPRT0_OVRCURRACT)
  3100. port_status |= USB_PORT_STAT_OVERCURRENT;
  3101. if (hprt0 & HPRT0_RST)
  3102. port_status |= USB_PORT_STAT_RESET;
  3103. if (hprt0 & HPRT0_PWR)
  3104. port_status |= USB_PORT_STAT_POWER;
  3105. speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  3106. if (speed == HPRT0_SPD_HIGH_SPEED)
  3107. port_status |= USB_PORT_STAT_HIGH_SPEED;
  3108. else if (speed == HPRT0_SPD_LOW_SPEED)
  3109. port_status |= USB_PORT_STAT_LOW_SPEED;
  3110. if (hprt0 & HPRT0_TSTCTL_MASK)
  3111. port_status |= USB_PORT_STAT_TEST;
  3112. /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  3113. if (hsotg->params.dma_desc_fs_enable) {
  3114. /*
  3115. * Enable descriptor DMA only if a full speed
  3116. * device is connected.
  3117. */
  3118. if (hsotg->new_connection &&
  3119. ((port_status &
  3120. (USB_PORT_STAT_CONNECTION |
  3121. USB_PORT_STAT_HIGH_SPEED |
  3122. USB_PORT_STAT_LOW_SPEED)) ==
  3123. USB_PORT_STAT_CONNECTION)) {
  3124. u32 hcfg;
  3125. dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
  3126. hsotg->params.dma_desc_enable = true;
  3127. hcfg = dwc2_readl(hsotg, HCFG);
  3128. hcfg |= HCFG_DESCDMA;
  3129. dwc2_writel(hsotg, hcfg, HCFG);
  3130. hsotg->new_connection = false;
  3131. }
  3132. }
  3133. dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
  3134. *(__le32 *)buf = cpu_to_le32(port_status);
  3135. break;
  3136. case SetHubFeature:
  3137. dev_dbg(hsotg->dev, "SetHubFeature\n");
  3138. /* No HUB features supported */
  3139. break;
  3140. case SetPortFeature:
  3141. dev_dbg(hsotg->dev, "SetPortFeature\n");
  3142. if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
  3143. goto error;
  3144. if (!hsotg->flags.b.port_connect_status) {
  3145. /*
  3146. * The port is disconnected, which means the core is
  3147. * either in device mode or it soon will be. Just
  3148. * return without doing anything since the port
  3149. * register can't be written if the core is in device
  3150. * mode.
  3151. */
  3152. break;
  3153. }
  3154. switch (wvalue) {
  3155. case USB_PORT_FEAT_SUSPEND:
  3156. dev_dbg(hsotg->dev,
  3157. "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  3158. if (windex != hsotg->otg_port)
  3159. goto error;
  3160. if (!hsotg->bus_suspended)
  3161. retval = dwc2_port_suspend(hsotg, windex);
  3162. break;
  3163. case USB_PORT_FEAT_POWER:
  3164. dev_dbg(hsotg->dev,
  3165. "SetPortFeature - USB_PORT_FEAT_POWER\n");
  3166. hprt0 = dwc2_read_hprt0(hsotg);
  3167. pwr = hprt0 & HPRT0_PWR;
  3168. hprt0 |= HPRT0_PWR;
  3169. dwc2_writel(hsotg, hprt0, HPRT0);
  3170. if (!pwr)
  3171. dwc2_vbus_supply_init(hsotg);
  3172. break;
  3173. case USB_PORT_FEAT_RESET:
  3174. dev_dbg(hsotg->dev,
  3175. "SetPortFeature - USB_PORT_FEAT_RESET\n");
  3176. hprt0 = dwc2_read_hprt0(hsotg);
  3177. if (hsotg->hibernated) {
  3178. retval = dwc2_exit_hibernation(hsotg, 0, 1, 1);
  3179. if (retval)
  3180. dev_err(hsotg->dev,
  3181. "exit hibernation failed\n");
  3182. }
  3183. if (hsotg->in_ppd) {
  3184. retval = dwc2_exit_partial_power_down(hsotg, 1,
  3185. true);
  3186. if (retval)
  3187. dev_err(hsotg->dev,
  3188. "exit partial_power_down failed\n");
  3189. }
  3190. if (hsotg->params.power_down ==
  3191. DWC2_POWER_DOWN_PARAM_NONE && hsotg->bus_suspended)
  3192. dwc2_host_exit_clock_gating(hsotg, 0);
  3193. pcgctl = dwc2_readl(hsotg, PCGCTL);
  3194. pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
  3195. dwc2_writel(hsotg, pcgctl, PCGCTL);
  3196. /* ??? Original driver does this */
  3197. dwc2_writel(hsotg, 0, PCGCTL);
  3198. hprt0 = dwc2_read_hprt0(hsotg);
  3199. pwr = hprt0 & HPRT0_PWR;
  3200. /* Clear suspend bit if resetting from suspend state */
  3201. hprt0 &= ~HPRT0_SUSP;
  3202. /*
  3203. * When B-Host the Port reset bit is set in the Start
  3204. * HCD Callback function, so that the reset is started
  3205. * within 1ms of the HNP success interrupt
  3206. */
  3207. if (!dwc2_hcd_is_b_host(hsotg)) {
  3208. hprt0 |= HPRT0_PWR | HPRT0_RST;
  3209. dev_dbg(hsotg->dev,
  3210. "In host mode, hprt0=%08x\n", hprt0);
  3211. dwc2_writel(hsotg, hprt0, HPRT0);
  3212. if (!pwr)
  3213. dwc2_vbus_supply_init(hsotg);
  3214. }
  3215. /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  3216. msleep(50);
  3217. hprt0 &= ~HPRT0_RST;
  3218. dwc2_writel(hsotg, hprt0, HPRT0);
  3219. hsotg->lx_state = DWC2_L0; /* Now back to On state */
  3220. break;
  3221. case USB_PORT_FEAT_INDICATOR:
  3222. dev_dbg(hsotg->dev,
  3223. "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  3224. /* Not supported */
  3225. break;
  3226. case USB_PORT_FEAT_TEST:
  3227. hprt0 = dwc2_read_hprt0(hsotg);
  3228. dev_dbg(hsotg->dev,
  3229. "SetPortFeature - USB_PORT_FEAT_TEST\n");
  3230. hprt0 &= ~HPRT0_TSTCTL_MASK;
  3231. hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
  3232. dwc2_writel(hsotg, hprt0, HPRT0);
  3233. break;
  3234. default:
  3235. retval = -EINVAL;
  3236. dev_err(hsotg->dev,
  3237. "SetPortFeature %1xh unknown or unsupported\n",
  3238. wvalue);
  3239. break;
  3240. }
  3241. break;
  3242. default:
  3243. error:
  3244. retval = -EINVAL;
  3245. dev_dbg(hsotg->dev,
  3246. "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
  3247. typereq, windex, wvalue);
  3248. break;
  3249. }
  3250. return retval;
  3251. }
  3252. static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
  3253. {
  3254. int retval;
  3255. if (port != 1)
  3256. return -EINVAL;
  3257. retval = (hsotg->flags.b.port_connect_status_change ||
  3258. hsotg->flags.b.port_reset_change ||
  3259. hsotg->flags.b.port_enable_change ||
  3260. hsotg->flags.b.port_suspend_change ||
  3261. hsotg->flags.b.port_over_current_change);
  3262. if (retval) {
  3263. dev_dbg(hsotg->dev,
  3264. "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
  3265. dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
  3266. hsotg->flags.b.port_connect_status_change);
  3267. dev_dbg(hsotg->dev, " port_reset_change: %d\n",
  3268. hsotg->flags.b.port_reset_change);
  3269. dev_dbg(hsotg->dev, " port_enable_change: %d\n",
  3270. hsotg->flags.b.port_enable_change);
  3271. dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
  3272. hsotg->flags.b.port_suspend_change);
  3273. dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
  3274. hsotg->flags.b.port_over_current_change);
  3275. }
  3276. return retval;
  3277. }
  3278. int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
  3279. {
  3280. u32 hfnum = dwc2_readl(hsotg, HFNUM);
  3281. #ifdef DWC2_DEBUG_SOF
  3282. dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
  3283. (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
  3284. #endif
  3285. return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
  3286. }
  3287. int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
  3288. {
  3289. u32 hprt = dwc2_readl(hsotg, HPRT0);
  3290. u32 hfir = dwc2_readl(hsotg, HFIR);
  3291. u32 hfnum = dwc2_readl(hsotg, HFNUM);
  3292. unsigned int us_per_frame;
  3293. unsigned int frame_number;
  3294. unsigned int remaining;
  3295. unsigned int interval;
  3296. unsigned int phy_clks;
  3297. /* High speed has 125 us per (micro) frame; others are 1 ms per */
  3298. us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
  3299. /* Extract fields */
  3300. frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
  3301. remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
  3302. interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
  3303. /*
  3304. * Number of phy clocks since the last tick of the frame number after
  3305. * "us" has passed.
  3306. */
  3307. phy_clks = (interval - remaining) +
  3308. DIV_ROUND_UP(interval * us, us_per_frame);
  3309. return dwc2_frame_num_inc(frame_number, phy_clks / interval);
  3310. }
  3311. int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
  3312. {
  3313. return hsotg->op_state == OTG_STATE_B_HOST;
  3314. }
  3315. static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
  3316. int iso_desc_count,
  3317. gfp_t mem_flags)
  3318. {
  3319. struct dwc2_hcd_urb *urb;
  3320. urb = kzalloc(struct_size(urb, iso_descs, iso_desc_count), mem_flags);
  3321. if (urb)
  3322. urb->packet_count = iso_desc_count;
  3323. return urb;
  3324. }
  3325. static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
  3326. struct dwc2_hcd_urb *urb, u8 dev_addr,
  3327. u8 ep_num, u8 ep_type, u8 ep_dir,
  3328. u16 maxp, u16 maxp_mult)
  3329. {
  3330. if (dbg_perio() ||
  3331. ep_type == USB_ENDPOINT_XFER_BULK ||
  3332. ep_type == USB_ENDPOINT_XFER_CONTROL)
  3333. dev_vdbg(hsotg->dev,
  3334. "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, maxp=%d (%d mult)\n",
  3335. dev_addr, ep_num, ep_dir, ep_type, maxp, maxp_mult);
  3336. urb->pipe_info.dev_addr = dev_addr;
  3337. urb->pipe_info.ep_num = ep_num;
  3338. urb->pipe_info.pipe_type = ep_type;
  3339. urb->pipe_info.pipe_dir = ep_dir;
  3340. urb->pipe_info.maxp = maxp;
  3341. urb->pipe_info.maxp_mult = maxp_mult;
  3342. }
  3343. /*
  3344. * NOTE: This function will be removed once the peripheral controller code
  3345. * is integrated and the driver is stable
  3346. */
  3347. void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
  3348. {
  3349. #ifdef DEBUG
  3350. struct dwc2_host_chan *chan;
  3351. struct dwc2_hcd_urb *urb;
  3352. struct dwc2_qtd *qtd;
  3353. int num_channels;
  3354. u32 np_tx_status;
  3355. u32 p_tx_status;
  3356. int i;
  3357. num_channels = hsotg->params.host_channels;
  3358. dev_dbg(hsotg->dev, "\n");
  3359. dev_dbg(hsotg->dev,
  3360. "************************************************************\n");
  3361. dev_dbg(hsotg->dev, "HCD State:\n");
  3362. dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels);
  3363. for (i = 0; i < num_channels; i++) {
  3364. chan = hsotg->hc_ptr_array[i];
  3365. dev_dbg(hsotg->dev, " Channel %d:\n", i);
  3366. dev_dbg(hsotg->dev,
  3367. " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  3368. chan->dev_addr, chan->ep_num, chan->ep_is_in);
  3369. dev_dbg(hsotg->dev, " speed: %d\n", chan->speed);
  3370. dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
  3371. dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
  3372. dev_dbg(hsotg->dev, " data_pid_start: %d\n",
  3373. chan->data_pid_start);
  3374. dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count);
  3375. dev_dbg(hsotg->dev, " xfer_started: %d\n",
  3376. chan->xfer_started);
  3377. dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
  3378. dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
  3379. (unsigned long)chan->xfer_dma);
  3380. dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
  3381. dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count);
  3382. dev_dbg(hsotg->dev, " halt_on_queue: %d\n",
  3383. chan->halt_on_queue);
  3384. dev_dbg(hsotg->dev, " halt_pending: %d\n",
  3385. chan->halt_pending);
  3386. dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
  3387. dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split);
  3388. dev_dbg(hsotg->dev, " complete_split: %d\n",
  3389. chan->complete_split);
  3390. dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr);
  3391. dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port);
  3392. dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos);
  3393. dev_dbg(hsotg->dev, " requests: %d\n", chan->requests);
  3394. dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
  3395. if (chan->xfer_started) {
  3396. u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
  3397. hfnum = dwc2_readl(hsotg, HFNUM);
  3398. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  3399. hctsiz = dwc2_readl(hsotg, HCTSIZ(i));
  3400. hcint = dwc2_readl(hsotg, HCINT(i));
  3401. hcintmsk = dwc2_readl(hsotg, HCINTMSK(i));
  3402. dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum);
  3403. dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar);
  3404. dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz);
  3405. dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint);
  3406. dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk);
  3407. }
  3408. if (!(chan->xfer_started && chan->qh))
  3409. continue;
  3410. list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
  3411. if (!qtd->in_process)
  3412. break;
  3413. urb = qtd->urb;
  3414. dev_dbg(hsotg->dev, " URB Info:\n");
  3415. dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n",
  3416. qtd, urb);
  3417. if (urb) {
  3418. dev_dbg(hsotg->dev,
  3419. " Dev: %d, EP: %d %s\n",
  3420. dwc2_hcd_get_dev_addr(&urb->pipe_info),
  3421. dwc2_hcd_get_ep_num(&urb->pipe_info),
  3422. dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
  3423. "IN" : "OUT");
  3424. dev_dbg(hsotg->dev,
  3425. " Max packet size: %d (%d mult)\n",
  3426. dwc2_hcd_get_maxp(&urb->pipe_info),
  3427. dwc2_hcd_get_maxp_mult(&urb->pipe_info));
  3428. dev_dbg(hsotg->dev,
  3429. " transfer_buffer: %p\n",
  3430. urb->buf);
  3431. dev_dbg(hsotg->dev,
  3432. " transfer_dma: %08lx\n",
  3433. (unsigned long)urb->dma);
  3434. dev_dbg(hsotg->dev,
  3435. " transfer_buffer_length: %d\n",
  3436. urb->length);
  3437. dev_dbg(hsotg->dev, " actual_length: %d\n",
  3438. urb->actual_length);
  3439. }
  3440. }
  3441. }
  3442. dev_dbg(hsotg->dev, " non_periodic_channels: %d\n",
  3443. hsotg->non_periodic_channels);
  3444. dev_dbg(hsotg->dev, " periodic_channels: %d\n",
  3445. hsotg->periodic_channels);
  3446. dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs);
  3447. np_tx_status = dwc2_readl(hsotg, GNPTXSTS);
  3448. dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n",
  3449. (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
  3450. dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n",
  3451. (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
  3452. p_tx_status = dwc2_readl(hsotg, HPTXSTS);
  3453. dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n",
  3454. (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
  3455. dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n",
  3456. (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
  3457. dwc2_dump_global_registers(hsotg);
  3458. dwc2_dump_host_registers(hsotg);
  3459. dev_dbg(hsotg->dev,
  3460. "************************************************************\n");
  3461. dev_dbg(hsotg->dev, "\n");
  3462. #endif
  3463. }
  3464. struct wrapper_priv_data {
  3465. struct dwc2_hsotg *hsotg;
  3466. };
  3467. /* Gets the dwc2_hsotg from a usb_hcd */
  3468. static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
  3469. {
  3470. struct wrapper_priv_data *p;
  3471. p = (struct wrapper_priv_data *)&hcd->hcd_priv;
  3472. return p->hsotg;
  3473. }
  3474. /**
  3475. * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
  3476. *
  3477. * This will get the dwc2_tt structure (and ttport) associated with the given
  3478. * context (which is really just a struct urb pointer).
  3479. *
  3480. * The first time this is called for a given TT we allocate memory for our
  3481. * structure. When everyone is done and has called dwc2_host_put_tt_info()
  3482. * then the refcount for the structure will go to 0 and we'll free it.
  3483. *
  3484. * @hsotg: The HCD state structure for the DWC OTG controller.
  3485. * @context: The priv pointer from a struct dwc2_hcd_urb.
  3486. * @mem_flags: Flags for allocating memory.
  3487. * @ttport: We'll return this device's port number here. That's used to
  3488. * reference into the bitmap if we're on a multi_tt hub.
  3489. *
  3490. * Return: a pointer to a struct dwc2_tt. Don't forget to call
  3491. * dwc2_host_put_tt_info()! Returns NULL upon memory alloc failure.
  3492. */
  3493. struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
  3494. gfp_t mem_flags, int *ttport)
  3495. {
  3496. struct urb *urb = context;
  3497. struct dwc2_tt *dwc_tt = NULL;
  3498. if (urb->dev->tt) {
  3499. *ttport = urb->dev->ttport;
  3500. dwc_tt = urb->dev->tt->hcpriv;
  3501. if (!dwc_tt) {
  3502. size_t bitmap_size;
  3503. /*
  3504. * For single_tt we need one schedule. For multi_tt
  3505. * we need one per port.
  3506. */
  3507. bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
  3508. sizeof(dwc_tt->periodic_bitmaps[0]);
  3509. if (urb->dev->tt->multi)
  3510. bitmap_size *= urb->dev->tt->hub->maxchild;
  3511. dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
  3512. mem_flags);
  3513. if (!dwc_tt)
  3514. return NULL;
  3515. dwc_tt->usb_tt = urb->dev->tt;
  3516. dwc_tt->usb_tt->hcpriv = dwc_tt;
  3517. }
  3518. dwc_tt->refcount++;
  3519. }
  3520. return dwc_tt;
  3521. }
  3522. /**
  3523. * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
  3524. *
  3525. * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
  3526. * of the structure are done.
  3527. *
  3528. * It's OK to call this with NULL.
  3529. *
  3530. * @hsotg: The HCD state structure for the DWC OTG controller.
  3531. * @dwc_tt: The pointer returned by dwc2_host_get_tt_info.
  3532. */
  3533. void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
  3534. {
  3535. /* Model kfree and make put of NULL a no-op */
  3536. if (!dwc_tt)
  3537. return;
  3538. WARN_ON(dwc_tt->refcount < 1);
  3539. dwc_tt->refcount--;
  3540. if (!dwc_tt->refcount) {
  3541. dwc_tt->usb_tt->hcpriv = NULL;
  3542. kfree(dwc_tt);
  3543. }
  3544. }
  3545. int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
  3546. {
  3547. struct urb *urb = context;
  3548. return urb->dev->speed;
  3549. }
  3550. static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
  3551. struct urb *urb)
  3552. {
  3553. struct usb_bus *bus = hcd_to_bus(hcd);
  3554. if (urb->interval)
  3555. bus->bandwidth_allocated += bw / urb->interval;
  3556. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  3557. bus->bandwidth_isoc_reqs++;
  3558. else
  3559. bus->bandwidth_int_reqs++;
  3560. }
  3561. static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
  3562. struct urb *urb)
  3563. {
  3564. struct usb_bus *bus = hcd_to_bus(hcd);
  3565. if (urb->interval)
  3566. bus->bandwidth_allocated -= bw / urb->interval;
  3567. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  3568. bus->bandwidth_isoc_reqs--;
  3569. else
  3570. bus->bandwidth_int_reqs--;
  3571. }
  3572. /*
  3573. * Sets the final status of an URB and returns it to the upper layer. Any
  3574. * required cleanup of the URB is performed.
  3575. *
  3576. * Must be called with interrupt disabled and spinlock held
  3577. */
  3578. void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
  3579. int status)
  3580. {
  3581. struct urb *urb;
  3582. int i;
  3583. if (!qtd) {
  3584. dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
  3585. return;
  3586. }
  3587. if (!qtd->urb) {
  3588. dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
  3589. return;
  3590. }
  3591. urb = qtd->urb->priv;
  3592. if (!urb) {
  3593. dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
  3594. return;
  3595. }
  3596. urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
  3597. if (dbg_urb(urb))
  3598. dev_vdbg(hsotg->dev,
  3599. "%s: urb %p device %d ep %d-%s status %d actual %d\n",
  3600. __func__, urb, usb_pipedevice(urb->pipe),
  3601. usb_pipeendpoint(urb->pipe),
  3602. usb_pipein(urb->pipe) ? "IN" : "OUT", status,
  3603. urb->actual_length);
  3604. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  3605. urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
  3606. for (i = 0; i < urb->number_of_packets; ++i) {
  3607. urb->iso_frame_desc[i].actual_length =
  3608. dwc2_hcd_urb_get_iso_desc_actual_length(
  3609. qtd->urb, i);
  3610. urb->iso_frame_desc[i].status =
  3611. dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
  3612. }
  3613. }
  3614. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
  3615. for (i = 0; i < urb->number_of_packets; i++)
  3616. dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
  3617. i, urb->iso_frame_desc[i].status);
  3618. }
  3619. urb->status = status;
  3620. if (!status) {
  3621. if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  3622. urb->actual_length < urb->transfer_buffer_length)
  3623. urb->status = -EREMOTEIO;
  3624. }
  3625. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
  3626. usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
  3627. struct usb_host_endpoint *ep = urb->ep;
  3628. if (ep)
  3629. dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
  3630. dwc2_hcd_get_ep_bandwidth(hsotg, ep),
  3631. urb);
  3632. }
  3633. usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
  3634. urb->hcpriv = NULL;
  3635. kfree(qtd->urb);
  3636. qtd->urb = NULL;
  3637. usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
  3638. }
  3639. /*
  3640. * Work queue function for starting the HCD when A-Cable is connected
  3641. */
  3642. static void dwc2_hcd_start_func(struct work_struct *work)
  3643. {
  3644. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  3645. start_work.work);
  3646. dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
  3647. dwc2_host_start(hsotg);
  3648. }
  3649. /*
  3650. * Reset work queue function
  3651. */
  3652. static void dwc2_hcd_reset_func(struct work_struct *work)
  3653. {
  3654. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  3655. reset_work.work);
  3656. unsigned long flags;
  3657. u32 hprt0;
  3658. dev_dbg(hsotg->dev, "USB RESET function called\n");
  3659. spin_lock_irqsave(&hsotg->lock, flags);
  3660. hprt0 = dwc2_read_hprt0(hsotg);
  3661. hprt0 &= ~HPRT0_RST;
  3662. dwc2_writel(hsotg, hprt0, HPRT0);
  3663. hsotg->flags.b.port_reset_change = 1;
  3664. spin_unlock_irqrestore(&hsotg->lock, flags);
  3665. }
  3666. static void dwc2_hcd_phy_reset_func(struct work_struct *work)
  3667. {
  3668. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  3669. phy_reset_work);
  3670. int ret;
  3671. ret = phy_reset(hsotg->phy);
  3672. if (ret)
  3673. dev_warn(hsotg->dev, "PHY reset failed\n");
  3674. }
  3675. /*
  3676. * =========================================================================
  3677. * Linux HC Driver Functions
  3678. * =========================================================================
  3679. */
  3680. /*
  3681. * Initializes the DWC_otg controller and its root hub and prepares it for host
  3682. * mode operation. Activates the root port. Returns 0 on success and a negative
  3683. * error code on failure.
  3684. */
  3685. static int _dwc2_hcd_start(struct usb_hcd *hcd)
  3686. {
  3687. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3688. struct usb_bus *bus = hcd_to_bus(hcd);
  3689. unsigned long flags;
  3690. u32 hprt0;
  3691. int ret;
  3692. dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
  3693. spin_lock_irqsave(&hsotg->lock, flags);
  3694. hsotg->lx_state = DWC2_L0;
  3695. hcd->state = HC_STATE_RUNNING;
  3696. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3697. if (dwc2_is_device_mode(hsotg)) {
  3698. spin_unlock_irqrestore(&hsotg->lock, flags);
  3699. return 0; /* why 0 ?? */
  3700. }
  3701. dwc2_hcd_reinit(hsotg);
  3702. hprt0 = dwc2_read_hprt0(hsotg);
  3703. /* Has vbus power been turned on in dwc2_core_host_init ? */
  3704. if (hprt0 & HPRT0_PWR) {
  3705. /* Enable external vbus supply before resuming root hub */
  3706. spin_unlock_irqrestore(&hsotg->lock, flags);
  3707. ret = dwc2_vbus_supply_init(hsotg);
  3708. if (ret)
  3709. return ret;
  3710. spin_lock_irqsave(&hsotg->lock, flags);
  3711. }
  3712. /* Initialize and connect root hub if one is not already attached */
  3713. if (bus->root_hub) {
  3714. dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
  3715. /* Inform the HUB driver to resume */
  3716. usb_hcd_resume_root_hub(hcd);
  3717. }
  3718. spin_unlock_irqrestore(&hsotg->lock, flags);
  3719. return 0;
  3720. }
  3721. /*
  3722. * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  3723. * stopped.
  3724. */
  3725. static void _dwc2_hcd_stop(struct usb_hcd *hcd)
  3726. {
  3727. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3728. unsigned long flags;
  3729. u32 hprt0;
  3730. /* Turn off all host-specific interrupts */
  3731. dwc2_disable_host_interrupts(hsotg);
  3732. /* Wait for interrupt processing to finish */
  3733. synchronize_irq(hcd->irq);
  3734. spin_lock_irqsave(&hsotg->lock, flags);
  3735. hprt0 = dwc2_read_hprt0(hsotg);
  3736. /* Ensure hcd is disconnected */
  3737. dwc2_hcd_disconnect(hsotg, true);
  3738. dwc2_hcd_stop(hsotg);
  3739. hsotg->lx_state = DWC2_L3;
  3740. hcd->state = HC_STATE_HALT;
  3741. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3742. spin_unlock_irqrestore(&hsotg->lock, flags);
  3743. /* keep balanced supply init/exit by checking HPRT0_PWR */
  3744. if (hprt0 & HPRT0_PWR)
  3745. dwc2_vbus_supply_exit(hsotg);
  3746. usleep_range(1000, 3000);
  3747. }
  3748. static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
  3749. {
  3750. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3751. unsigned long flags;
  3752. int ret = 0;
  3753. spin_lock_irqsave(&hsotg->lock, flags);
  3754. if (dwc2_is_device_mode(hsotg))
  3755. goto unlock;
  3756. if (hsotg->lx_state != DWC2_L0)
  3757. goto unlock;
  3758. if (!HCD_HW_ACCESSIBLE(hcd))
  3759. goto unlock;
  3760. if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
  3761. goto unlock;
  3762. if (hsotg->bus_suspended)
  3763. goto skip_power_saving;
  3764. if (hsotg->flags.b.port_connect_status == 0)
  3765. goto skip_power_saving;
  3766. switch (hsotg->params.power_down) {
  3767. case DWC2_POWER_DOWN_PARAM_PARTIAL:
  3768. /* Enter partial_power_down */
  3769. ret = dwc2_enter_partial_power_down(hsotg);
  3770. if (ret)
  3771. dev_err(hsotg->dev,
  3772. "enter partial_power_down failed\n");
  3773. /* After entering suspend, hardware is not accessible */
  3774. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3775. break;
  3776. case DWC2_POWER_DOWN_PARAM_HIBERNATION:
  3777. /* Enter hibernation */
  3778. spin_unlock_irqrestore(&hsotg->lock, flags);
  3779. ret = dwc2_enter_hibernation(hsotg, 1);
  3780. if (ret)
  3781. dev_err(hsotg->dev, "enter hibernation failed\n");
  3782. spin_lock_irqsave(&hsotg->lock, flags);
  3783. /* After entering suspend, hardware is not accessible */
  3784. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3785. break;
  3786. case DWC2_POWER_DOWN_PARAM_NONE:
  3787. /*
  3788. * If not hibernation nor partial power down are supported,
  3789. * clock gating is used to save power.
  3790. */
  3791. if (!hsotg->params.no_clock_gating) {
  3792. dwc2_host_enter_clock_gating(hsotg);
  3793. /* After entering suspend, hardware is not accessible */
  3794. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3795. }
  3796. break;
  3797. default:
  3798. goto skip_power_saving;
  3799. }
  3800. spin_unlock_irqrestore(&hsotg->lock, flags);
  3801. dwc2_vbus_supply_exit(hsotg);
  3802. spin_lock_irqsave(&hsotg->lock, flags);
  3803. /* Ask phy to be suspended */
  3804. if (!IS_ERR_OR_NULL(hsotg->uphy)) {
  3805. spin_unlock_irqrestore(&hsotg->lock, flags);
  3806. usb_phy_set_suspend(hsotg->uphy, true);
  3807. spin_lock_irqsave(&hsotg->lock, flags);
  3808. }
  3809. skip_power_saving:
  3810. hsotg->lx_state = DWC2_L2;
  3811. unlock:
  3812. spin_unlock_irqrestore(&hsotg->lock, flags);
  3813. return ret;
  3814. }
  3815. static int _dwc2_hcd_resume(struct usb_hcd *hcd)
  3816. {
  3817. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3818. unsigned long flags;
  3819. u32 hprt0;
  3820. int ret = 0;
  3821. spin_lock_irqsave(&hsotg->lock, flags);
  3822. if (dwc2_is_device_mode(hsotg))
  3823. goto unlock;
  3824. if (hsotg->lx_state != DWC2_L2)
  3825. goto unlock;
  3826. hprt0 = dwc2_read_hprt0(hsotg);
  3827. /*
  3828. * Added port connection status checking which prevents exiting from
  3829. * Partial Power Down mode from _dwc2_hcd_resume() if not in Partial
  3830. * Power Down mode.
  3831. */
  3832. if (hprt0 & HPRT0_CONNSTS) {
  3833. hsotg->lx_state = DWC2_L0;
  3834. goto unlock;
  3835. }
  3836. switch (hsotg->params.power_down) {
  3837. case DWC2_POWER_DOWN_PARAM_PARTIAL:
  3838. ret = dwc2_exit_partial_power_down(hsotg, 0, true);
  3839. if (ret)
  3840. dev_err(hsotg->dev,
  3841. "exit partial_power_down failed\n");
  3842. /*
  3843. * Set HW accessible bit before powering on the controller
  3844. * since an interrupt may rise.
  3845. */
  3846. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3847. break;
  3848. case DWC2_POWER_DOWN_PARAM_HIBERNATION:
  3849. ret = dwc2_exit_hibernation(hsotg, 0, 0, 1);
  3850. if (ret)
  3851. dev_err(hsotg->dev, "exit hibernation failed.\n");
  3852. /*
  3853. * Set HW accessible bit before powering on the controller
  3854. * since an interrupt may rise.
  3855. */
  3856. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3857. break;
  3858. case DWC2_POWER_DOWN_PARAM_NONE:
  3859. /*
  3860. * If not hibernation nor partial power down are supported,
  3861. * port resume is done using the clock gating programming flow.
  3862. */
  3863. spin_unlock_irqrestore(&hsotg->lock, flags);
  3864. dwc2_host_exit_clock_gating(hsotg, 0);
  3865. /*
  3866. * Initialize the Core for Host mode, as after system resume
  3867. * the global interrupts are disabled.
  3868. */
  3869. dwc2_core_init(hsotg, false);
  3870. dwc2_enable_global_interrupts(hsotg);
  3871. dwc2_hcd_reinit(hsotg);
  3872. spin_lock_irqsave(&hsotg->lock, flags);
  3873. /*
  3874. * Set HW accessible bit before powering on the controller
  3875. * since an interrupt may rise.
  3876. */
  3877. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3878. break;
  3879. default:
  3880. hsotg->lx_state = DWC2_L0;
  3881. goto unlock;
  3882. }
  3883. /* Change Root port status, as port status change occurred after resume.*/
  3884. hsotg->flags.b.port_suspend_change = 1;
  3885. /*
  3886. * Enable power if not already done.
  3887. * This must not be spinlocked since duration
  3888. * of this call is unknown.
  3889. */
  3890. if (!IS_ERR_OR_NULL(hsotg->uphy)) {
  3891. spin_unlock_irqrestore(&hsotg->lock, flags);
  3892. usb_phy_set_suspend(hsotg->uphy, false);
  3893. spin_lock_irqsave(&hsotg->lock, flags);
  3894. }
  3895. /* Enable external vbus supply after resuming the port. */
  3896. spin_unlock_irqrestore(&hsotg->lock, flags);
  3897. dwc2_vbus_supply_init(hsotg);
  3898. /* Wait for controller to correctly update D+/D- level */
  3899. usleep_range(3000, 5000);
  3900. spin_lock_irqsave(&hsotg->lock, flags);
  3901. /*
  3902. * Clear Port Enable and Port Status changes.
  3903. * Enable Port Power.
  3904. */
  3905. dwc2_writel(hsotg, HPRT0_PWR | HPRT0_CONNDET |
  3906. HPRT0_ENACHG, HPRT0);
  3907. /* Wait for controller to detect Port Connect */
  3908. spin_unlock_irqrestore(&hsotg->lock, flags);
  3909. usleep_range(5000, 7000);
  3910. spin_lock_irqsave(&hsotg->lock, flags);
  3911. unlock:
  3912. spin_unlock_irqrestore(&hsotg->lock, flags);
  3913. return ret;
  3914. }
  3915. /* Returns the current frame number */
  3916. static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
  3917. {
  3918. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3919. return dwc2_hcd_get_frame_number(hsotg);
  3920. }
  3921. static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
  3922. char *fn_name)
  3923. {
  3924. #ifdef VERBOSE_DEBUG
  3925. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3926. char *pipetype = NULL;
  3927. char *speed = NULL;
  3928. dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
  3929. dev_vdbg(hsotg->dev, " Device address: %d\n",
  3930. usb_pipedevice(urb->pipe));
  3931. dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n",
  3932. usb_pipeendpoint(urb->pipe),
  3933. usb_pipein(urb->pipe) ? "IN" : "OUT");
  3934. switch (usb_pipetype(urb->pipe)) {
  3935. case PIPE_CONTROL:
  3936. pipetype = "CONTROL";
  3937. break;
  3938. case PIPE_BULK:
  3939. pipetype = "BULK";
  3940. break;
  3941. case PIPE_INTERRUPT:
  3942. pipetype = "INTERRUPT";
  3943. break;
  3944. case PIPE_ISOCHRONOUS:
  3945. pipetype = "ISOCHRONOUS";
  3946. break;
  3947. }
  3948. dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype,
  3949. usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
  3950. "IN" : "OUT");
  3951. switch (urb->dev->speed) {
  3952. case USB_SPEED_HIGH:
  3953. speed = "HIGH";
  3954. break;
  3955. case USB_SPEED_FULL:
  3956. speed = "FULL";
  3957. break;
  3958. case USB_SPEED_LOW:
  3959. speed = "LOW";
  3960. break;
  3961. default:
  3962. speed = "UNKNOWN";
  3963. break;
  3964. }
  3965. dev_vdbg(hsotg->dev, " Speed: %s\n", speed);
  3966. dev_vdbg(hsotg->dev, " Max packet size: %d (%d mult)\n",
  3967. usb_endpoint_maxp(&urb->ep->desc),
  3968. usb_endpoint_maxp_mult(&urb->ep->desc));
  3969. dev_vdbg(hsotg->dev, " Data buffer length: %d\n",
  3970. urb->transfer_buffer_length);
  3971. dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
  3972. urb->transfer_buffer, (unsigned long)urb->transfer_dma);
  3973. dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
  3974. urb->setup_packet, (unsigned long)urb->setup_dma);
  3975. dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval);
  3976. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  3977. int i;
  3978. for (i = 0; i < urb->number_of_packets; i++) {
  3979. dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i);
  3980. dev_vdbg(hsotg->dev, " offset: %d, length %d\n",
  3981. urb->iso_frame_desc[i].offset,
  3982. urb->iso_frame_desc[i].length);
  3983. }
  3984. }
  3985. #endif
  3986. }
  3987. /*
  3988. * Starts processing a USB transfer request specified by a USB Request Block
  3989. * (URB). mem_flags indicates the type of memory allocation to use while
  3990. * processing this URB.
  3991. */
  3992. static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  3993. gfp_t mem_flags)
  3994. {
  3995. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3996. struct usb_host_endpoint *ep = urb->ep;
  3997. struct dwc2_hcd_urb *dwc2_urb;
  3998. int i;
  3999. int retval;
  4000. int alloc_bandwidth = 0;
  4001. u8 ep_type = 0;
  4002. u32 tflags = 0;
  4003. void *buf;
  4004. unsigned long flags;
  4005. struct dwc2_qh *qh;
  4006. bool qh_allocated = false;
  4007. struct dwc2_qtd *qtd;
  4008. struct dwc2_gregs_backup *gr;
  4009. gr = &hsotg->gr_backup;
  4010. if (dbg_urb(urb)) {
  4011. dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
  4012. dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
  4013. }
  4014. if (hsotg->hibernated) {
  4015. if (gr->gotgctl & GOTGCTL_CURMODE_HOST)
  4016. retval = dwc2_exit_hibernation(hsotg, 0, 0, 1);
  4017. else
  4018. retval = dwc2_exit_hibernation(hsotg, 0, 0, 0);
  4019. if (retval)
  4020. dev_err(hsotg->dev,
  4021. "exit hibernation failed.\n");
  4022. }
  4023. if (hsotg->in_ppd) {
  4024. retval = dwc2_exit_partial_power_down(hsotg, 0, true);
  4025. if (retval)
  4026. dev_err(hsotg->dev,
  4027. "exit partial_power_down failed\n");
  4028. }
  4029. if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_NONE &&
  4030. hsotg->bus_suspended) {
  4031. if (dwc2_is_device_mode(hsotg))
  4032. dwc2_gadget_exit_clock_gating(hsotg, 0);
  4033. else
  4034. dwc2_host_exit_clock_gating(hsotg, 0);
  4035. }
  4036. if (!ep)
  4037. return -EINVAL;
  4038. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
  4039. usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
  4040. spin_lock_irqsave(&hsotg->lock, flags);
  4041. if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
  4042. alloc_bandwidth = 1;
  4043. spin_unlock_irqrestore(&hsotg->lock, flags);
  4044. }
  4045. switch (usb_pipetype(urb->pipe)) {
  4046. case PIPE_CONTROL:
  4047. ep_type = USB_ENDPOINT_XFER_CONTROL;
  4048. break;
  4049. case PIPE_ISOCHRONOUS:
  4050. ep_type = USB_ENDPOINT_XFER_ISOC;
  4051. break;
  4052. case PIPE_BULK:
  4053. ep_type = USB_ENDPOINT_XFER_BULK;
  4054. break;
  4055. case PIPE_INTERRUPT:
  4056. ep_type = USB_ENDPOINT_XFER_INT;
  4057. break;
  4058. }
  4059. dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
  4060. mem_flags);
  4061. if (!dwc2_urb)
  4062. return -ENOMEM;
  4063. dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
  4064. usb_pipeendpoint(urb->pipe), ep_type,
  4065. usb_pipein(urb->pipe),
  4066. usb_endpoint_maxp(&ep->desc),
  4067. usb_endpoint_maxp_mult(&ep->desc));
  4068. buf = urb->transfer_buffer;
  4069. if (hcd_uses_dma(hcd)) {
  4070. if (!buf && (urb->transfer_dma & 3)) {
  4071. dev_err(hsotg->dev,
  4072. "%s: unaligned transfer with no transfer_buffer",
  4073. __func__);
  4074. retval = -EINVAL;
  4075. goto fail0;
  4076. }
  4077. }
  4078. if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  4079. tflags |= URB_GIVEBACK_ASAP;
  4080. if (urb->transfer_flags & URB_ZERO_PACKET)
  4081. tflags |= URB_SEND_ZERO_PACKET;
  4082. dwc2_urb->priv = urb;
  4083. dwc2_urb->buf = buf;
  4084. dwc2_urb->dma = urb->transfer_dma;
  4085. dwc2_urb->length = urb->transfer_buffer_length;
  4086. dwc2_urb->setup_packet = urb->setup_packet;
  4087. dwc2_urb->setup_dma = urb->setup_dma;
  4088. dwc2_urb->flags = tflags;
  4089. dwc2_urb->interval = urb->interval;
  4090. dwc2_urb->status = -EINPROGRESS;
  4091. for (i = 0; i < urb->number_of_packets; ++i)
  4092. dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
  4093. urb->iso_frame_desc[i].offset,
  4094. urb->iso_frame_desc[i].length);
  4095. urb->hcpriv = dwc2_urb;
  4096. qh = (struct dwc2_qh *)ep->hcpriv;
  4097. /* Create QH for the endpoint if it doesn't exist */
  4098. if (!qh) {
  4099. qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
  4100. if (!qh) {
  4101. retval = -ENOMEM;
  4102. goto fail0;
  4103. }
  4104. ep->hcpriv = qh;
  4105. qh_allocated = true;
  4106. }
  4107. qtd = kzalloc(sizeof(*qtd), mem_flags);
  4108. if (!qtd) {
  4109. retval = -ENOMEM;
  4110. goto fail1;
  4111. }
  4112. spin_lock_irqsave(&hsotg->lock, flags);
  4113. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  4114. if (retval)
  4115. goto fail2;
  4116. retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
  4117. if (retval)
  4118. goto fail3;
  4119. if (alloc_bandwidth) {
  4120. dwc2_allocate_bus_bandwidth(hcd,
  4121. dwc2_hcd_get_ep_bandwidth(hsotg, ep),
  4122. urb);
  4123. }
  4124. spin_unlock_irqrestore(&hsotg->lock, flags);
  4125. return 0;
  4126. fail3:
  4127. dwc2_urb->priv = NULL;
  4128. usb_hcd_unlink_urb_from_ep(hcd, urb);
  4129. if (qh_allocated && qh->channel && qh->channel->qh == qh)
  4130. qh->channel->qh = NULL;
  4131. fail2:
  4132. urb->hcpriv = NULL;
  4133. spin_unlock_irqrestore(&hsotg->lock, flags);
  4134. kfree(qtd);
  4135. fail1:
  4136. if (qh_allocated) {
  4137. struct dwc2_qtd *qtd2, *qtd2_tmp;
  4138. ep->hcpriv = NULL;
  4139. dwc2_hcd_qh_unlink(hsotg, qh);
  4140. /* Free each QTD in the QH's QTD list */
  4141. list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
  4142. qtd_list_entry)
  4143. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
  4144. dwc2_hcd_qh_free(hsotg, qh);
  4145. }
  4146. fail0:
  4147. kfree(dwc2_urb);
  4148. return retval;
  4149. }
  4150. /*
  4151. * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
  4152. */
  4153. static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
  4154. int status)
  4155. {
  4156. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4157. int rc;
  4158. unsigned long flags;
  4159. dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
  4160. dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
  4161. spin_lock_irqsave(&hsotg->lock, flags);
  4162. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  4163. if (rc)
  4164. goto out;
  4165. if (!urb->hcpriv) {
  4166. dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
  4167. goto out;
  4168. }
  4169. rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
  4170. usb_hcd_unlink_urb_from_ep(hcd, urb);
  4171. kfree(urb->hcpriv);
  4172. urb->hcpriv = NULL;
  4173. /* Higher layer software sets URB status */
  4174. spin_unlock(&hsotg->lock);
  4175. usb_hcd_giveback_urb(hcd, urb, status);
  4176. spin_lock(&hsotg->lock);
  4177. dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
  4178. dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
  4179. out:
  4180. spin_unlock_irqrestore(&hsotg->lock, flags);
  4181. return rc;
  4182. }
  4183. /*
  4184. * Frees resources in the DWC_otg controller related to a given endpoint. Also
  4185. * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  4186. * must already be dequeued.
  4187. */
  4188. static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
  4189. struct usb_host_endpoint *ep)
  4190. {
  4191. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4192. dev_dbg(hsotg->dev,
  4193. "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
  4194. ep->desc.bEndpointAddress, ep->hcpriv);
  4195. dwc2_hcd_endpoint_disable(hsotg, ep, 250);
  4196. }
  4197. /*
  4198. * Resets endpoint specific parameter values, in current version used to reset
  4199. * the data toggle (as a WA). This function can be called from usb_clear_halt
  4200. * routine.
  4201. */
  4202. static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
  4203. struct usb_host_endpoint *ep)
  4204. {
  4205. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4206. unsigned long flags;
  4207. dev_dbg(hsotg->dev,
  4208. "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
  4209. ep->desc.bEndpointAddress);
  4210. spin_lock_irqsave(&hsotg->lock, flags);
  4211. dwc2_hcd_endpoint_reset(hsotg, ep);
  4212. spin_unlock_irqrestore(&hsotg->lock, flags);
  4213. }
  4214. /*
  4215. * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  4216. * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  4217. * interrupt.
  4218. *
  4219. * This function is called by the USB core when an interrupt occurs
  4220. */
  4221. static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
  4222. {
  4223. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4224. return dwc2_handle_hcd_intr(hsotg);
  4225. }
  4226. /*
  4227. * Creates Status Change bitmap for the root hub and root port. The bitmap is
  4228. * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  4229. * is the status change indicator for the single root port. Returns 1 if either
  4230. * change indicator is 1, otherwise returns 0.
  4231. */
  4232. static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
  4233. {
  4234. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4235. buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
  4236. return buf[0] != 0;
  4237. }
  4238. /* Handles hub class-specific requests */
  4239. static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
  4240. u16 windex, char *buf, u16 wlength)
  4241. {
  4242. int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
  4243. wvalue, windex, buf, wlength);
  4244. return retval;
  4245. }
  4246. /* Handles hub TT buffer clear completions */
  4247. static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
  4248. struct usb_host_endpoint *ep)
  4249. {
  4250. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4251. struct dwc2_qh *qh;
  4252. unsigned long flags;
  4253. qh = ep->hcpriv;
  4254. if (!qh)
  4255. return;
  4256. spin_lock_irqsave(&hsotg->lock, flags);
  4257. qh->tt_buffer_dirty = 0;
  4258. if (hsotg->flags.b.port_connect_status)
  4259. dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
  4260. spin_unlock_irqrestore(&hsotg->lock, flags);
  4261. }
  4262. /*
  4263. * HPRT0_SPD_HIGH_SPEED: high speed
  4264. * HPRT0_SPD_FULL_SPEED: full speed
  4265. */
  4266. static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
  4267. {
  4268. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4269. if (hsotg->params.speed == speed)
  4270. return;
  4271. hsotg->params.speed = speed;
  4272. queue_work(hsotg->wq_otg, &hsotg->wf_otg);
  4273. }
  4274. static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
  4275. {
  4276. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4277. if (!hsotg->params.change_speed_quirk)
  4278. return;
  4279. /*
  4280. * On removal, set speed to default high-speed.
  4281. */
  4282. if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
  4283. udev->parent->speed < USB_SPEED_HIGH) {
  4284. dev_info(hsotg->dev, "Set speed to default high-speed\n");
  4285. dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
  4286. }
  4287. }
  4288. static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
  4289. {
  4290. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4291. if (!hsotg->params.change_speed_quirk)
  4292. return 0;
  4293. if (udev->speed == USB_SPEED_HIGH) {
  4294. dev_info(hsotg->dev, "Set speed to high-speed\n");
  4295. dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
  4296. } else if ((udev->speed == USB_SPEED_FULL ||
  4297. udev->speed == USB_SPEED_LOW)) {
  4298. /*
  4299. * Change speed setting to full-speed if there's
  4300. * a full-speed or low-speed device plugged in.
  4301. */
  4302. dev_info(hsotg->dev, "Set speed to full-speed\n");
  4303. dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
  4304. }
  4305. return 0;
  4306. }
  4307. static struct hc_driver dwc2_hc_driver = {
  4308. .description = "dwc2_hsotg",
  4309. .product_desc = "DWC OTG Controller",
  4310. .hcd_priv_size = sizeof(struct wrapper_priv_data),
  4311. .irq = _dwc2_hcd_irq,
  4312. .flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
  4313. .start = _dwc2_hcd_start,
  4314. .stop = _dwc2_hcd_stop,
  4315. .urb_enqueue = _dwc2_hcd_urb_enqueue,
  4316. .urb_dequeue = _dwc2_hcd_urb_dequeue,
  4317. .endpoint_disable = _dwc2_hcd_endpoint_disable,
  4318. .endpoint_reset = _dwc2_hcd_endpoint_reset,
  4319. .get_frame_number = _dwc2_hcd_get_frame_number,
  4320. .hub_status_data = _dwc2_hcd_hub_status_data,
  4321. .hub_control = _dwc2_hcd_hub_control,
  4322. .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
  4323. .bus_suspend = _dwc2_hcd_suspend,
  4324. .bus_resume = _dwc2_hcd_resume,
  4325. .map_urb_for_dma = dwc2_map_urb_for_dma,
  4326. .unmap_urb_for_dma = dwc2_unmap_urb_for_dma,
  4327. };
  4328. /*
  4329. * Frees secondary storage associated with the dwc2_hsotg structure contained
  4330. * in the struct usb_hcd field
  4331. */
  4332. static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
  4333. {
  4334. u32 ahbcfg;
  4335. u32 dctl;
  4336. int i;
  4337. dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
  4338. /* Free memory for QH/QTD lists */
  4339. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
  4340. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting);
  4341. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
  4342. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
  4343. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
  4344. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
  4345. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
  4346. /* Free memory for the host channels */
  4347. for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  4348. struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
  4349. if (chan) {
  4350. dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
  4351. i, chan);
  4352. hsotg->hc_ptr_array[i] = NULL;
  4353. kfree(chan);
  4354. }
  4355. }
  4356. if (hsotg->params.host_dma) {
  4357. if (hsotg->status_buf) {
  4358. dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
  4359. hsotg->status_buf,
  4360. hsotg->status_buf_dma);
  4361. hsotg->status_buf = NULL;
  4362. }
  4363. } else {
  4364. kfree(hsotg->status_buf);
  4365. hsotg->status_buf = NULL;
  4366. }
  4367. ahbcfg = dwc2_readl(hsotg, GAHBCFG);
  4368. /* Disable all interrupts */
  4369. ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
  4370. dwc2_writel(hsotg, ahbcfg, GAHBCFG);
  4371. dwc2_writel(hsotg, 0, GINTMSK);
  4372. if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
  4373. dctl = dwc2_readl(hsotg, DCTL);
  4374. dctl |= DCTL_SFTDISCON;
  4375. dwc2_writel(hsotg, dctl, DCTL);
  4376. }
  4377. if (hsotg->wq_otg) {
  4378. if (!cancel_work_sync(&hsotg->wf_otg))
  4379. flush_workqueue(hsotg->wq_otg);
  4380. destroy_workqueue(hsotg->wq_otg);
  4381. }
  4382. cancel_work_sync(&hsotg->phy_reset_work);
  4383. del_timer(&hsotg->wkp_timer);
  4384. }
  4385. static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
  4386. {
  4387. /* Turn off all host-specific interrupts */
  4388. dwc2_disable_host_interrupts(hsotg);
  4389. dwc2_hcd_free(hsotg);
  4390. }
  4391. /*
  4392. * Initializes the HCD. This function allocates memory for and initializes the
  4393. * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
  4394. * USB bus with the core and calls the hc_driver->start() function. It returns
  4395. * a negative error on failure.
  4396. */
  4397. int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
  4398. {
  4399. struct platform_device *pdev = to_platform_device(hsotg->dev);
  4400. struct resource *res;
  4401. struct usb_hcd *hcd;
  4402. struct dwc2_host_chan *channel;
  4403. u32 hcfg;
  4404. int i, num_channels;
  4405. int retval;
  4406. if (usb_disabled())
  4407. return -ENODEV;
  4408. dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
  4409. retval = -ENOMEM;
  4410. hcfg = dwc2_readl(hsotg, HCFG);
  4411. dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
  4412. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  4413. hsotg->frame_num_array = kcalloc(FRAME_NUM_ARRAY_SIZE,
  4414. sizeof(*hsotg->frame_num_array),
  4415. GFP_KERNEL);
  4416. if (!hsotg->frame_num_array)
  4417. goto error1;
  4418. hsotg->last_frame_num_array =
  4419. kcalloc(FRAME_NUM_ARRAY_SIZE,
  4420. sizeof(*hsotg->last_frame_num_array), GFP_KERNEL);
  4421. if (!hsotg->last_frame_num_array)
  4422. goto error1;
  4423. #endif
  4424. hsotg->last_frame_num = HFNUM_MAX_FRNUM;
  4425. /* Check if the bus driver or platform code has setup a dma_mask */
  4426. if (hsotg->params.host_dma &&
  4427. !hsotg->dev->dma_mask) {
  4428. dev_warn(hsotg->dev,
  4429. "dma_mask not set, disabling DMA\n");
  4430. hsotg->params.host_dma = false;
  4431. hsotg->params.dma_desc_enable = false;
  4432. }
  4433. /* Set device flags indicating whether the HCD supports DMA */
  4434. if (hsotg->params.host_dma) {
  4435. if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
  4436. dev_warn(hsotg->dev, "can't set DMA mask\n");
  4437. if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
  4438. dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
  4439. }
  4440. if (hsotg->params.change_speed_quirk) {
  4441. dwc2_hc_driver.free_dev = dwc2_free_dev;
  4442. dwc2_hc_driver.reset_device = dwc2_reset_device;
  4443. }
  4444. if (hsotg->params.host_dma)
  4445. dwc2_hc_driver.flags |= HCD_DMA;
  4446. hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
  4447. if (!hcd)
  4448. goto error1;
  4449. hcd->has_tt = 1;
  4450. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  4451. if (!res) {
  4452. retval = -EINVAL;
  4453. goto error2;
  4454. }
  4455. hcd->rsrc_start = res->start;
  4456. hcd->rsrc_len = resource_size(res);
  4457. ((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
  4458. hsotg->priv = hcd;
  4459. /*
  4460. * Disable the global interrupt until all the interrupt handlers are
  4461. * installed
  4462. */
  4463. dwc2_disable_global_interrupts(hsotg);
  4464. /* Initialize the DWC_otg core, and select the Phy type */
  4465. retval = dwc2_core_init(hsotg, true);
  4466. if (retval)
  4467. goto error2;
  4468. /* Create new workqueue and init work */
  4469. retval = -ENOMEM;
  4470. hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
  4471. if (!hsotg->wq_otg) {
  4472. dev_err(hsotg->dev, "Failed to create workqueue\n");
  4473. goto error2;
  4474. }
  4475. INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
  4476. timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0);
  4477. /* Initialize the non-periodic schedule */
  4478. INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
  4479. INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting);
  4480. INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
  4481. /* Initialize the periodic schedule */
  4482. INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
  4483. INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
  4484. INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
  4485. INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
  4486. INIT_LIST_HEAD(&hsotg->split_order);
  4487. /*
  4488. * Create a host channel descriptor for each host channel implemented
  4489. * in the controller. Initialize the channel descriptor array.
  4490. */
  4491. INIT_LIST_HEAD(&hsotg->free_hc_list);
  4492. num_channels = hsotg->params.host_channels;
  4493. memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
  4494. for (i = 0; i < num_channels; i++) {
  4495. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  4496. if (!channel)
  4497. goto error3;
  4498. channel->hc_num = i;
  4499. INIT_LIST_HEAD(&channel->split_order_list_entry);
  4500. hsotg->hc_ptr_array[i] = channel;
  4501. }
  4502. /* Initialize work */
  4503. INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
  4504. INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
  4505. INIT_WORK(&hsotg->phy_reset_work, dwc2_hcd_phy_reset_func);
  4506. /*
  4507. * Allocate space for storing data on status transactions. Normally no
  4508. * data is sent, but this space acts as a bit bucket. This must be
  4509. * done after usb_add_hcd since that function allocates the DMA buffer
  4510. * pool.
  4511. */
  4512. if (hsotg->params.host_dma)
  4513. hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
  4514. DWC2_HCD_STATUS_BUF_SIZE,
  4515. &hsotg->status_buf_dma, GFP_KERNEL);
  4516. else
  4517. hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
  4518. GFP_KERNEL);
  4519. if (!hsotg->status_buf)
  4520. goto error3;
  4521. /*
  4522. * Create kmem caches to handle descriptor buffers in descriptor
  4523. * DMA mode.
  4524. * Alignment must be set to 512 bytes.
  4525. */
  4526. if (hsotg->params.dma_desc_enable ||
  4527. hsotg->params.dma_desc_fs_enable) {
  4528. hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
  4529. sizeof(struct dwc2_dma_desc) *
  4530. MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
  4531. NULL);
  4532. if (!hsotg->desc_gen_cache) {
  4533. dev_err(hsotg->dev,
  4534. "unable to create dwc2 generic desc cache\n");
  4535. /*
  4536. * Disable descriptor dma mode since it will not be
  4537. * usable.
  4538. */
  4539. hsotg->params.dma_desc_enable = false;
  4540. hsotg->params.dma_desc_fs_enable = false;
  4541. }
  4542. hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
  4543. sizeof(struct dwc2_dma_desc) *
  4544. MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
  4545. if (!hsotg->desc_hsisoc_cache) {
  4546. dev_err(hsotg->dev,
  4547. "unable to create dwc2 hs isoc desc cache\n");
  4548. kmem_cache_destroy(hsotg->desc_gen_cache);
  4549. /*
  4550. * Disable descriptor dma mode since it will not be
  4551. * usable.
  4552. */
  4553. hsotg->params.dma_desc_enable = false;
  4554. hsotg->params.dma_desc_fs_enable = false;
  4555. }
  4556. }
  4557. if (hsotg->params.host_dma) {
  4558. /*
  4559. * Create kmem caches to handle non-aligned buffer
  4560. * in Buffer DMA mode.
  4561. */
  4562. hsotg->unaligned_cache = kmem_cache_create("dwc2-unaligned-dma",
  4563. DWC2_KMEM_UNALIGNED_BUF_SIZE, 4,
  4564. SLAB_CACHE_DMA, NULL);
  4565. if (!hsotg->unaligned_cache)
  4566. dev_err(hsotg->dev,
  4567. "unable to create dwc2 unaligned cache\n");
  4568. }
  4569. hsotg->otg_port = 1;
  4570. hsotg->frame_list = NULL;
  4571. hsotg->frame_list_dma = 0;
  4572. hsotg->periodic_qh_count = 0;
  4573. /* Initiate lx_state to L3 disconnected state */
  4574. hsotg->lx_state = DWC2_L3;
  4575. hcd->self.otg_port = hsotg->otg_port;
  4576. /* Don't support SG list at this point */
  4577. hcd->self.sg_tablesize = 0;
  4578. hcd->tpl_support = of_usb_host_tpl_support(hsotg->dev->of_node);
  4579. if (!IS_ERR_OR_NULL(hsotg->uphy))
  4580. otg_set_host(hsotg->uphy->otg, &hcd->self);
  4581. /*
  4582. * Finish generic HCD initialization and start the HCD. This function
  4583. * allocates the DMA buffer pool, registers the USB bus, requests the
  4584. * IRQ line, and calls hcd_start method.
  4585. */
  4586. retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
  4587. if (retval < 0)
  4588. goto error4;
  4589. device_wakeup_enable(hcd->self.controller);
  4590. dwc2_hcd_dump_state(hsotg);
  4591. dwc2_enable_global_interrupts(hsotg);
  4592. return 0;
  4593. error4:
  4594. kmem_cache_destroy(hsotg->unaligned_cache);
  4595. kmem_cache_destroy(hsotg->desc_hsisoc_cache);
  4596. kmem_cache_destroy(hsotg->desc_gen_cache);
  4597. error3:
  4598. dwc2_hcd_release(hsotg);
  4599. error2:
  4600. usb_put_hcd(hcd);
  4601. error1:
  4602. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  4603. kfree(hsotg->last_frame_num_array);
  4604. kfree(hsotg->frame_num_array);
  4605. #endif
  4606. dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
  4607. return retval;
  4608. }
  4609. /*
  4610. * Removes the HCD.
  4611. * Frees memory and resources associated with the HCD and deregisters the bus.
  4612. */
  4613. void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
  4614. {
  4615. struct usb_hcd *hcd;
  4616. dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
  4617. hcd = dwc2_hsotg_to_hcd(hsotg);
  4618. dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
  4619. if (!hcd) {
  4620. dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
  4621. __func__);
  4622. return;
  4623. }
  4624. if (!IS_ERR_OR_NULL(hsotg->uphy))
  4625. otg_set_host(hsotg->uphy->otg, NULL);
  4626. usb_remove_hcd(hcd);
  4627. hsotg->priv = NULL;
  4628. kmem_cache_destroy(hsotg->unaligned_cache);
  4629. kmem_cache_destroy(hsotg->desc_hsisoc_cache);
  4630. kmem_cache_destroy(hsotg->desc_gen_cache);
  4631. dwc2_hcd_release(hsotg);
  4632. usb_put_hcd(hcd);
  4633. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  4634. kfree(hsotg->last_frame_num_array);
  4635. kfree(hsotg->frame_num_array);
  4636. #endif
  4637. }
  4638. /**
  4639. * dwc2_backup_host_registers() - Backup controller host registers.
  4640. * When suspending usb bus, registers needs to be backuped
  4641. * if controller power is disabled once suspended.
  4642. *
  4643. * @hsotg: Programming view of the DWC_otg controller
  4644. */
  4645. int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
  4646. {
  4647. struct dwc2_hregs_backup *hr;
  4648. int i;
  4649. dev_dbg(hsotg->dev, "%s\n", __func__);
  4650. /* Backup Host regs */
  4651. hr = &hsotg->hr_backup;
  4652. hr->hcfg = dwc2_readl(hsotg, HCFG);
  4653. hr->haintmsk = dwc2_readl(hsotg, HAINTMSK);
  4654. for (i = 0; i < hsotg->params.host_channels; ++i)
  4655. hr->hcintmsk[i] = dwc2_readl(hsotg, HCINTMSK(i));
  4656. hr->hprt0 = dwc2_read_hprt0(hsotg);
  4657. hr->hfir = dwc2_readl(hsotg, HFIR);
  4658. hr->hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
  4659. hr->valid = true;
  4660. return 0;
  4661. }
  4662. /**
  4663. * dwc2_restore_host_registers() - Restore controller host registers.
  4664. * When resuming usb bus, device registers needs to be restored
  4665. * if controller power were disabled.
  4666. *
  4667. * @hsotg: Programming view of the DWC_otg controller
  4668. */
  4669. int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
  4670. {
  4671. struct dwc2_hregs_backup *hr;
  4672. int i;
  4673. dev_dbg(hsotg->dev, "%s\n", __func__);
  4674. /* Restore host regs */
  4675. hr = &hsotg->hr_backup;
  4676. if (!hr->valid) {
  4677. dev_err(hsotg->dev, "%s: no host registers to restore\n",
  4678. __func__);
  4679. return -EINVAL;
  4680. }
  4681. hr->valid = false;
  4682. dwc2_writel(hsotg, hr->hcfg, HCFG);
  4683. dwc2_writel(hsotg, hr->haintmsk, HAINTMSK);
  4684. for (i = 0; i < hsotg->params.host_channels; ++i)
  4685. dwc2_writel(hsotg, hr->hcintmsk[i], HCINTMSK(i));
  4686. dwc2_writel(hsotg, hr->hprt0, HPRT0);
  4687. dwc2_writel(hsotg, hr->hfir, HFIR);
  4688. dwc2_writel(hsotg, hr->hptxfsiz, HPTXFSIZ);
  4689. hsotg->frame_number = 0;
  4690. return 0;
  4691. }
  4692. /**
  4693. * dwc2_host_enter_hibernation() - Put controller in Hibernation.
  4694. *
  4695. * @hsotg: Programming view of the DWC_otg controller
  4696. */
  4697. int dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg)
  4698. {
  4699. unsigned long flags;
  4700. int ret = 0;
  4701. u32 hprt0;
  4702. u32 pcgcctl;
  4703. u32 gusbcfg;
  4704. u32 gpwrdn;
  4705. dev_dbg(hsotg->dev, "Preparing host for hibernation\n");
  4706. ret = dwc2_backup_global_registers(hsotg);
  4707. if (ret) {
  4708. dev_err(hsotg->dev, "%s: failed to backup global registers\n",
  4709. __func__);
  4710. return ret;
  4711. }
  4712. ret = dwc2_backup_host_registers(hsotg);
  4713. if (ret) {
  4714. dev_err(hsotg->dev, "%s: failed to backup host registers\n",
  4715. __func__);
  4716. return ret;
  4717. }
  4718. /* Enter USB Suspend Mode */
  4719. hprt0 = dwc2_readl(hsotg, HPRT0);
  4720. hprt0 |= HPRT0_SUSP;
  4721. hprt0 &= ~HPRT0_ENA;
  4722. dwc2_writel(hsotg, hprt0, HPRT0);
  4723. /* Wait for the HPRT0.PrtSusp register field to be set */
  4724. if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 5000))
  4725. dev_warn(hsotg->dev, "Suspend wasn't generated\n");
  4726. /*
  4727. * We need to disable interrupts to prevent servicing of any IRQ
  4728. * during going to hibernation
  4729. */
  4730. spin_lock_irqsave(&hsotg->lock, flags);
  4731. hsotg->lx_state = DWC2_L2;
  4732. gusbcfg = dwc2_readl(hsotg, GUSBCFG);
  4733. if (gusbcfg & GUSBCFG_ULPI_UTMI_SEL) {
  4734. /* ULPI interface */
  4735. /* Suspend the Phy Clock */
  4736. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4737. pcgcctl |= PCGCTL_STOPPCLK;
  4738. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4739. udelay(10);
  4740. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4741. gpwrdn |= GPWRDN_PMUACTV;
  4742. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4743. udelay(10);
  4744. } else {
  4745. /* UTMI+ Interface */
  4746. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4747. gpwrdn |= GPWRDN_PMUACTV;
  4748. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4749. udelay(10);
  4750. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4751. pcgcctl |= PCGCTL_STOPPCLK;
  4752. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4753. udelay(10);
  4754. }
  4755. /* Enable interrupts from wake up logic */
  4756. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4757. gpwrdn |= GPWRDN_PMUINTSEL;
  4758. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4759. udelay(10);
  4760. /* Unmask host mode interrupts in GPWRDN */
  4761. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4762. gpwrdn |= GPWRDN_DISCONN_DET_MSK;
  4763. gpwrdn |= GPWRDN_LNSTSCHG_MSK;
  4764. gpwrdn |= GPWRDN_STS_CHGINT_MSK;
  4765. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4766. udelay(10);
  4767. /* Enable Power Down Clamp */
  4768. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4769. gpwrdn |= GPWRDN_PWRDNCLMP;
  4770. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4771. udelay(10);
  4772. /* Switch off VDD */
  4773. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4774. gpwrdn |= GPWRDN_PWRDNSWTCH;
  4775. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4776. hsotg->hibernated = 1;
  4777. hsotg->bus_suspended = 1;
  4778. dev_dbg(hsotg->dev, "Host hibernation completed\n");
  4779. spin_unlock_irqrestore(&hsotg->lock, flags);
  4780. return ret;
  4781. }
  4782. /*
  4783. * dwc2_host_exit_hibernation()
  4784. *
  4785. * @hsotg: Programming view of the DWC_otg controller
  4786. * @rem_wakeup: indicates whether resume is initiated by Device or Host.
  4787. * @param reset: indicates whether resume is initiated by Reset.
  4788. *
  4789. * Return: non-zero if failed to enter to hibernation.
  4790. *
  4791. * This function is for exiting from Host mode hibernation by
  4792. * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  4793. */
  4794. int dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
  4795. int reset)
  4796. {
  4797. u32 gpwrdn;
  4798. u32 hprt0;
  4799. int ret = 0;
  4800. struct dwc2_gregs_backup *gr;
  4801. struct dwc2_hregs_backup *hr;
  4802. gr = &hsotg->gr_backup;
  4803. hr = &hsotg->hr_backup;
  4804. dev_dbg(hsotg->dev,
  4805. "%s: called with rem_wakeup = %d reset = %d\n",
  4806. __func__, rem_wakeup, reset);
  4807. dwc2_hib_restore_common(hsotg, rem_wakeup, 1);
  4808. hsotg->hibernated = 0;
  4809. /*
  4810. * This step is not described in functional spec but if not wait for
  4811. * this delay, mismatch interrupts occurred because just after restore
  4812. * core is in Device mode(gintsts.curmode == 0)
  4813. */
  4814. mdelay(100);
  4815. /* Clear all pending interupts */
  4816. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  4817. /* De-assert Restore */
  4818. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4819. gpwrdn &= ~GPWRDN_RESTORE;
  4820. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4821. udelay(10);
  4822. /* Restore GUSBCFG, HCFG */
  4823. dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
  4824. dwc2_writel(hsotg, hr->hcfg, HCFG);
  4825. /* De-assert Wakeup Logic */
  4826. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4827. gpwrdn &= ~GPWRDN_PMUACTV;
  4828. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4829. udelay(10);
  4830. hprt0 = hr->hprt0;
  4831. hprt0 |= HPRT0_PWR;
  4832. hprt0 &= ~HPRT0_ENA;
  4833. hprt0 &= ~HPRT0_SUSP;
  4834. dwc2_writel(hsotg, hprt0, HPRT0);
  4835. hprt0 = hr->hprt0;
  4836. hprt0 |= HPRT0_PWR;
  4837. hprt0 &= ~HPRT0_ENA;
  4838. hprt0 &= ~HPRT0_SUSP;
  4839. if (reset) {
  4840. hprt0 |= HPRT0_RST;
  4841. dwc2_writel(hsotg, hprt0, HPRT0);
  4842. /* Wait for Resume time and then program HPRT again */
  4843. mdelay(60);
  4844. hprt0 &= ~HPRT0_RST;
  4845. dwc2_writel(hsotg, hprt0, HPRT0);
  4846. } else {
  4847. hprt0 |= HPRT0_RES;
  4848. dwc2_writel(hsotg, hprt0, HPRT0);
  4849. /* Wait for Resume time and then program HPRT again */
  4850. mdelay(100);
  4851. hprt0 &= ~HPRT0_RES;
  4852. dwc2_writel(hsotg, hprt0, HPRT0);
  4853. }
  4854. /* Clear all interrupt status */
  4855. hprt0 = dwc2_readl(hsotg, HPRT0);
  4856. hprt0 |= HPRT0_CONNDET;
  4857. hprt0 |= HPRT0_ENACHG;
  4858. hprt0 &= ~HPRT0_ENA;
  4859. dwc2_writel(hsotg, hprt0, HPRT0);
  4860. hprt0 = dwc2_readl(hsotg, HPRT0);
  4861. /* Clear all pending interupts */
  4862. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  4863. /* Restore global registers */
  4864. ret = dwc2_restore_global_registers(hsotg);
  4865. if (ret) {
  4866. dev_err(hsotg->dev, "%s: failed to restore registers\n",
  4867. __func__);
  4868. return ret;
  4869. }
  4870. /* Restore host registers */
  4871. ret = dwc2_restore_host_registers(hsotg);
  4872. if (ret) {
  4873. dev_err(hsotg->dev, "%s: failed to restore host registers\n",
  4874. __func__);
  4875. return ret;
  4876. }
  4877. if (rem_wakeup) {
  4878. dwc2_hcd_rem_wakeup(hsotg);
  4879. /*
  4880. * Change "port_connect_status_change" flag to re-enumerate,
  4881. * because after exit from hibernation port connection status
  4882. * is not detected.
  4883. */
  4884. hsotg->flags.b.port_connect_status_change = 1;
  4885. }
  4886. hsotg->hibernated = 0;
  4887. hsotg->bus_suspended = 0;
  4888. hsotg->lx_state = DWC2_L0;
  4889. dev_dbg(hsotg->dev, "Host hibernation restore complete\n");
  4890. return ret;
  4891. }
  4892. bool dwc2_host_can_poweroff_phy(struct dwc2_hsotg *dwc2)
  4893. {
  4894. struct usb_device *root_hub = dwc2_hsotg_to_hcd(dwc2)->self.root_hub;
  4895. /* If the controller isn't allowed to wakeup then we can power off. */
  4896. if (!device_may_wakeup(dwc2->dev))
  4897. return true;
  4898. /*
  4899. * We don't want to power off the PHY if something under the
  4900. * root hub has wakeup enabled.
  4901. */
  4902. if (usb_wakeup_enabled_descendants(root_hub))
  4903. return false;
  4904. /* No reason to keep the PHY powered, so allow poweroff */
  4905. return true;
  4906. }
  4907. /**
  4908. * dwc2_host_enter_partial_power_down() - Put controller in partial
  4909. * power down.
  4910. *
  4911. * @hsotg: Programming view of the DWC_otg controller
  4912. *
  4913. * Return: non-zero if failed to enter host partial power down.
  4914. *
  4915. * This function is for entering Host mode partial power down.
  4916. */
  4917. int dwc2_host_enter_partial_power_down(struct dwc2_hsotg *hsotg)
  4918. {
  4919. u32 pcgcctl;
  4920. u32 hprt0;
  4921. int ret = 0;
  4922. dev_dbg(hsotg->dev, "Entering host partial power down started.\n");
  4923. /* Put this port in suspend mode. */
  4924. hprt0 = dwc2_read_hprt0(hsotg);
  4925. hprt0 |= HPRT0_SUSP;
  4926. dwc2_writel(hsotg, hprt0, HPRT0);
  4927. udelay(5);
  4928. /* Wait for the HPRT0.PrtSusp register field to be set */
  4929. if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 3000))
  4930. dev_warn(hsotg->dev, "Suspend wasn't generated\n");
  4931. /* Backup all registers */
  4932. ret = dwc2_backup_global_registers(hsotg);
  4933. if (ret) {
  4934. dev_err(hsotg->dev, "%s: failed to backup global registers\n",
  4935. __func__);
  4936. return ret;
  4937. }
  4938. ret = dwc2_backup_host_registers(hsotg);
  4939. if (ret) {
  4940. dev_err(hsotg->dev, "%s: failed to backup host registers\n",
  4941. __func__);
  4942. return ret;
  4943. }
  4944. /*
  4945. * Clear any pending interrupts since dwc2 will not be able to
  4946. * clear them after entering partial_power_down.
  4947. */
  4948. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  4949. /* Put the controller in low power state */
  4950. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4951. pcgcctl |= PCGCTL_PWRCLMP;
  4952. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4953. udelay(5);
  4954. pcgcctl |= PCGCTL_RSTPDWNMODULE;
  4955. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4956. udelay(5);
  4957. pcgcctl |= PCGCTL_STOPPCLK;
  4958. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4959. /* Set in_ppd flag to 1 as here core enters suspend. */
  4960. hsotg->in_ppd = 1;
  4961. hsotg->lx_state = DWC2_L2;
  4962. hsotg->bus_suspended = true;
  4963. dev_dbg(hsotg->dev, "Entering host partial power down completed.\n");
  4964. return ret;
  4965. }
  4966. /*
  4967. * dwc2_host_exit_partial_power_down() - Exit controller from host partial
  4968. * power down.
  4969. *
  4970. * @hsotg: Programming view of the DWC_otg controller
  4971. * @rem_wakeup: indicates whether resume is initiated by Reset.
  4972. * @restore: indicates whether need to restore the registers or not.
  4973. *
  4974. * Return: non-zero if failed to exit host partial power down.
  4975. *
  4976. * This function is for exiting from Host mode partial power down.
  4977. */
  4978. int dwc2_host_exit_partial_power_down(struct dwc2_hsotg *hsotg,
  4979. int rem_wakeup, bool restore)
  4980. {
  4981. u32 pcgcctl;
  4982. int ret = 0;
  4983. u32 hprt0;
  4984. dev_dbg(hsotg->dev, "Exiting host partial power down started.\n");
  4985. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4986. pcgcctl &= ~PCGCTL_STOPPCLK;
  4987. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4988. udelay(5);
  4989. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4990. pcgcctl &= ~PCGCTL_PWRCLMP;
  4991. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4992. udelay(5);
  4993. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4994. pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
  4995. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4996. udelay(100);
  4997. if (restore) {
  4998. ret = dwc2_restore_global_registers(hsotg);
  4999. if (ret) {
  5000. dev_err(hsotg->dev, "%s: failed to restore registers\n",
  5001. __func__);
  5002. return ret;
  5003. }
  5004. ret = dwc2_restore_host_registers(hsotg);
  5005. if (ret) {
  5006. dev_err(hsotg->dev, "%s: failed to restore host registers\n",
  5007. __func__);
  5008. return ret;
  5009. }
  5010. }
  5011. /* Drive resume signaling and exit suspend mode on the port. */
  5012. hprt0 = dwc2_read_hprt0(hsotg);
  5013. hprt0 |= HPRT0_RES;
  5014. hprt0 &= ~HPRT0_SUSP;
  5015. dwc2_writel(hsotg, hprt0, HPRT0);
  5016. udelay(5);
  5017. if (!rem_wakeup) {
  5018. /* Stop driveing resume signaling on the port. */
  5019. hprt0 = dwc2_read_hprt0(hsotg);
  5020. hprt0 &= ~HPRT0_RES;
  5021. dwc2_writel(hsotg, hprt0, HPRT0);
  5022. hsotg->bus_suspended = false;
  5023. } else {
  5024. /* Turn on the port power bit. */
  5025. hprt0 = dwc2_read_hprt0(hsotg);
  5026. hprt0 |= HPRT0_PWR;
  5027. dwc2_writel(hsotg, hprt0, HPRT0);
  5028. /* Connect hcd. */
  5029. dwc2_hcd_connect(hsotg);
  5030. mod_timer(&hsotg->wkp_timer,
  5031. jiffies + msecs_to_jiffies(71));
  5032. }
  5033. /* Set lx_state to and in_ppd to 0 as here core exits from suspend. */
  5034. hsotg->in_ppd = 0;
  5035. hsotg->lx_state = DWC2_L0;
  5036. dev_dbg(hsotg->dev, "Exiting host partial power down completed.\n");
  5037. return ret;
  5038. }
  5039. /**
  5040. * dwc2_host_enter_clock_gating() - Put controller in clock gating.
  5041. *
  5042. * @hsotg: Programming view of the DWC_otg controller
  5043. *
  5044. * This function is for entering Host mode clock gating.
  5045. */
  5046. void dwc2_host_enter_clock_gating(struct dwc2_hsotg *hsotg)
  5047. {
  5048. u32 hprt0;
  5049. u32 pcgctl;
  5050. dev_dbg(hsotg->dev, "Entering host clock gating.\n");
  5051. /* Put this port in suspend mode. */
  5052. hprt0 = dwc2_read_hprt0(hsotg);
  5053. hprt0 |= HPRT0_SUSP;
  5054. dwc2_writel(hsotg, hprt0, HPRT0);
  5055. /* Set the Phy Clock bit as suspend is received. */
  5056. pcgctl = dwc2_readl(hsotg, PCGCTL);
  5057. pcgctl |= PCGCTL_STOPPCLK;
  5058. dwc2_writel(hsotg, pcgctl, PCGCTL);
  5059. udelay(5);
  5060. /* Set the Gate hclk as suspend is received. */
  5061. pcgctl = dwc2_readl(hsotg, PCGCTL);
  5062. pcgctl |= PCGCTL_GATEHCLK;
  5063. dwc2_writel(hsotg, pcgctl, PCGCTL);
  5064. udelay(5);
  5065. hsotg->bus_suspended = true;
  5066. hsotg->lx_state = DWC2_L2;
  5067. }
  5068. /**
  5069. * dwc2_host_exit_clock_gating() - Exit controller from clock gating.
  5070. *
  5071. * @hsotg: Programming view of the DWC_otg controller
  5072. * @rem_wakeup: indicates whether resume is initiated by remote wakeup
  5073. *
  5074. * This function is for exiting Host mode clock gating.
  5075. */
  5076. void dwc2_host_exit_clock_gating(struct dwc2_hsotg *hsotg, int rem_wakeup)
  5077. {
  5078. u32 hprt0;
  5079. u32 pcgctl;
  5080. dev_dbg(hsotg->dev, "Exiting host clock gating.\n");
  5081. /* Clear the Gate hclk. */
  5082. pcgctl = dwc2_readl(hsotg, PCGCTL);
  5083. pcgctl &= ~PCGCTL_GATEHCLK;
  5084. dwc2_writel(hsotg, pcgctl, PCGCTL);
  5085. udelay(5);
  5086. /* Phy Clock bit. */
  5087. pcgctl = dwc2_readl(hsotg, PCGCTL);
  5088. pcgctl &= ~PCGCTL_STOPPCLK;
  5089. dwc2_writel(hsotg, pcgctl, PCGCTL);
  5090. udelay(5);
  5091. /* Drive resume signaling and exit suspend mode on the port. */
  5092. hprt0 = dwc2_read_hprt0(hsotg);
  5093. hprt0 |= HPRT0_RES;
  5094. hprt0 &= ~HPRT0_SUSP;
  5095. dwc2_writel(hsotg, hprt0, HPRT0);
  5096. udelay(5);
  5097. if (!rem_wakeup) {
  5098. /* In case of port resume need to wait for 40 ms */
  5099. msleep(USB_RESUME_TIMEOUT);
  5100. /* Stop driveing resume signaling on the port. */
  5101. hprt0 = dwc2_read_hprt0(hsotg);
  5102. hprt0 &= ~HPRT0_RES;
  5103. dwc2_writel(hsotg, hprt0, HPRT0);
  5104. hsotg->bus_suspended = false;
  5105. hsotg->lx_state = DWC2_L0;
  5106. } else {
  5107. mod_timer(&hsotg->wkp_timer,
  5108. jiffies + msecs_to_jiffies(71));
  5109. }
  5110. }