tegra-xudc.c 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * NVIDIA Tegra XUSB device mode controller
  4. *
  5. * Copyright (c) 2013-2022, NVIDIA CORPORATION. All rights reserved.
  6. * Copyright (c) 2015, Google Inc.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/completion.h>
  10. #include <linux/delay.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/dmapool.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/iopoll.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/phy/tegra/xusb.h>
  20. #include <linux/pm_domain.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/reset.h>
  25. #include <linux/usb/ch9.h>
  26. #include <linux/usb/gadget.h>
  27. #include <linux/usb/otg.h>
  28. #include <linux/usb/role.h>
  29. #include <linux/usb/phy.h>
  30. #include <linux/workqueue.h>
  31. /* XUSB_DEV registers */
  32. #define DB 0x004
  33. #define DB_TARGET_MASK GENMASK(15, 8)
  34. #define DB_TARGET(x) (((x) << 8) & DB_TARGET_MASK)
  35. #define DB_STREAMID_MASK GENMASK(31, 16)
  36. #define DB_STREAMID(x) (((x) << 16) & DB_STREAMID_MASK)
  37. #define ERSTSZ 0x008
  38. #define ERSTSZ_ERSTXSZ_SHIFT(x) ((x) * 16)
  39. #define ERSTSZ_ERSTXSZ_MASK GENMASK(15, 0)
  40. #define ERSTXBALO(x) (0x010 + 8 * (x))
  41. #define ERSTXBAHI(x) (0x014 + 8 * (x))
  42. #define ERDPLO 0x020
  43. #define ERDPLO_EHB BIT(3)
  44. #define ERDPHI 0x024
  45. #define EREPLO 0x028
  46. #define EREPLO_ECS BIT(0)
  47. #define EREPLO_SEGI BIT(1)
  48. #define EREPHI 0x02c
  49. #define CTRL 0x030
  50. #define CTRL_RUN BIT(0)
  51. #define CTRL_LSE BIT(1)
  52. #define CTRL_IE BIT(4)
  53. #define CTRL_SMI_EVT BIT(5)
  54. #define CTRL_SMI_DSE BIT(6)
  55. #define CTRL_EWE BIT(7)
  56. #define CTRL_DEVADDR_MASK GENMASK(30, 24)
  57. #define CTRL_DEVADDR(x) (((x) << 24) & CTRL_DEVADDR_MASK)
  58. #define CTRL_ENABLE BIT(31)
  59. #define ST 0x034
  60. #define ST_RC BIT(0)
  61. #define ST_IP BIT(4)
  62. #define RT_IMOD 0x038
  63. #define RT_IMOD_IMODI_MASK GENMASK(15, 0)
  64. #define RT_IMOD_IMODI(x) ((x) & RT_IMOD_IMODI_MASK)
  65. #define RT_IMOD_IMODC_MASK GENMASK(31, 16)
  66. #define RT_IMOD_IMODC(x) (((x) << 16) & RT_IMOD_IMODC_MASK)
  67. #define PORTSC 0x03c
  68. #define PORTSC_CCS BIT(0)
  69. #define PORTSC_PED BIT(1)
  70. #define PORTSC_PR BIT(4)
  71. #define PORTSC_PLS_SHIFT 5
  72. #define PORTSC_PLS_MASK GENMASK(8, 5)
  73. #define PORTSC_PLS_U0 0x0
  74. #define PORTSC_PLS_U2 0x2
  75. #define PORTSC_PLS_U3 0x3
  76. #define PORTSC_PLS_DISABLED 0x4
  77. #define PORTSC_PLS_RXDETECT 0x5
  78. #define PORTSC_PLS_INACTIVE 0x6
  79. #define PORTSC_PLS_RESUME 0xf
  80. #define PORTSC_PLS(x) (((x) << PORTSC_PLS_SHIFT) & PORTSC_PLS_MASK)
  81. #define PORTSC_PS_SHIFT 10
  82. #define PORTSC_PS_MASK GENMASK(13, 10)
  83. #define PORTSC_PS_UNDEFINED 0x0
  84. #define PORTSC_PS_FS 0x1
  85. #define PORTSC_PS_LS 0x2
  86. #define PORTSC_PS_HS 0x3
  87. #define PORTSC_PS_SS 0x4
  88. #define PORTSC_LWS BIT(16)
  89. #define PORTSC_CSC BIT(17)
  90. #define PORTSC_WRC BIT(19)
  91. #define PORTSC_PRC BIT(21)
  92. #define PORTSC_PLC BIT(22)
  93. #define PORTSC_CEC BIT(23)
  94. #define PORTSC_WPR BIT(30)
  95. #define PORTSC_CHANGE_MASK (PORTSC_CSC | PORTSC_WRC | PORTSC_PRC | \
  96. PORTSC_PLC | PORTSC_CEC)
  97. #define ECPLO 0x040
  98. #define ECPHI 0x044
  99. #define MFINDEX 0x048
  100. #define MFINDEX_FRAME_SHIFT 3
  101. #define MFINDEX_FRAME_MASK GENMASK(13, 3)
  102. #define PORTPM 0x04c
  103. #define PORTPM_L1S_MASK GENMASK(1, 0)
  104. #define PORTPM_L1S_DROP 0x0
  105. #define PORTPM_L1S_ACCEPT 0x1
  106. #define PORTPM_L1S_NYET 0x2
  107. #define PORTPM_L1S_STALL 0x3
  108. #define PORTPM_L1S(x) ((x) & PORTPM_L1S_MASK)
  109. #define PORTPM_RWE BIT(3)
  110. #define PORTPM_U2TIMEOUT_MASK GENMASK(15, 8)
  111. #define PORTPM_U1TIMEOUT_MASK GENMASK(23, 16)
  112. #define PORTPM_FLA BIT(24)
  113. #define PORTPM_VBA BIT(25)
  114. #define PORTPM_WOC BIT(26)
  115. #define PORTPM_WOD BIT(27)
  116. #define PORTPM_U1E BIT(28)
  117. #define PORTPM_U2E BIT(29)
  118. #define PORTPM_FRWE BIT(30)
  119. #define PORTPM_PNG_CYA BIT(31)
  120. #define EP_HALT 0x050
  121. #define EP_PAUSE 0x054
  122. #define EP_RELOAD 0x058
  123. #define EP_STCHG 0x05c
  124. #define DEVNOTIF_LO 0x064
  125. #define DEVNOTIF_LO_TRIG BIT(0)
  126. #define DEVNOTIF_LO_TYPE_MASK GENMASK(7, 4)
  127. #define DEVNOTIF_LO_TYPE(x) (((x) << 4) & DEVNOTIF_LO_TYPE_MASK)
  128. #define DEVNOTIF_LO_TYPE_FUNCTION_WAKE 0x1
  129. #define DEVNOTIF_HI 0x068
  130. #define PORTHALT 0x06c
  131. #define PORTHALT_HALT_LTSSM BIT(0)
  132. #define PORTHALT_HALT_REJECT BIT(1)
  133. #define PORTHALT_STCHG_REQ BIT(20)
  134. #define PORTHALT_STCHG_INTR_EN BIT(24)
  135. #define PORT_TM 0x070
  136. #define EP_THREAD_ACTIVE 0x074
  137. #define EP_STOPPED 0x078
  138. #define HSFSPI_COUNT0 0x100
  139. #define HSFSPI_COUNT13 0x134
  140. #define HSFSPI_COUNT13_U2_RESUME_K_DURATION_MASK GENMASK(29, 0)
  141. #define HSFSPI_COUNT13_U2_RESUME_K_DURATION(x) ((x) & \
  142. HSFSPI_COUNT13_U2_RESUME_K_DURATION_MASK)
  143. #define BLCG 0x840
  144. #define SSPX_CORE_CNT0 0x610
  145. #define SSPX_CORE_CNT0_PING_TBURST_MASK GENMASK(7, 0)
  146. #define SSPX_CORE_CNT0_PING_TBURST(x) ((x) & SSPX_CORE_CNT0_PING_TBURST_MASK)
  147. #define SSPX_CORE_CNT30 0x688
  148. #define SSPX_CORE_CNT30_LMPITP_TIMER_MASK GENMASK(19, 0)
  149. #define SSPX_CORE_CNT30_LMPITP_TIMER(x) ((x) & \
  150. SSPX_CORE_CNT30_LMPITP_TIMER_MASK)
  151. #define SSPX_CORE_CNT32 0x690
  152. #define SSPX_CORE_CNT32_POLL_TBURST_MAX_MASK GENMASK(7, 0)
  153. #define SSPX_CORE_CNT32_POLL_TBURST_MAX(x) ((x) & \
  154. SSPX_CORE_CNT32_POLL_TBURST_MAX_MASK)
  155. #define SSPX_CORE_CNT56 0x6fc
  156. #define SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX_MASK GENMASK(19, 0)
  157. #define SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX(x) ((x) & \
  158. SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX_MASK)
  159. #define SSPX_CORE_CNT57 0x700
  160. #define SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX_MASK GENMASK(19, 0)
  161. #define SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX(x) ((x) & \
  162. SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX_MASK)
  163. #define SSPX_CORE_CNT65 0x720
  164. #define SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID_MASK GENMASK(19, 0)
  165. #define SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID(x) ((x) & \
  166. SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID_MASK)
  167. #define SSPX_CORE_CNT66 0x724
  168. #define SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID_MASK GENMASK(19, 0)
  169. #define SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID(x) ((x) & \
  170. SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID_MASK)
  171. #define SSPX_CORE_CNT67 0x728
  172. #define SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID_MASK GENMASK(19, 0)
  173. #define SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID(x) ((x) & \
  174. SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID_MASK)
  175. #define SSPX_CORE_CNT72 0x73c
  176. #define SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT_MASK GENMASK(19, 0)
  177. #define SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT(x) ((x) & \
  178. SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT_MASK)
  179. #define SSPX_CORE_PADCTL4 0x750
  180. #define SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3_MASK GENMASK(19, 0)
  181. #define SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3(x) ((x) & \
  182. SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3_MASK)
  183. #define BLCG_DFPCI BIT(0)
  184. #define BLCG_UFPCI BIT(1)
  185. #define BLCG_FE BIT(2)
  186. #define BLCG_COREPLL_PWRDN BIT(8)
  187. #define BLCG_IOPLL_0_PWRDN BIT(9)
  188. #define BLCG_IOPLL_1_PWRDN BIT(10)
  189. #define BLCG_IOPLL_2_PWRDN BIT(11)
  190. #define BLCG_ALL 0x1ff
  191. #define CFG_DEV_SSPI_XFER 0x858
  192. #define CFG_DEV_SSPI_XFER_ACKTIMEOUT_MASK GENMASK(31, 0)
  193. #define CFG_DEV_SSPI_XFER_ACKTIMEOUT(x) ((x) & \
  194. CFG_DEV_SSPI_XFER_ACKTIMEOUT_MASK)
  195. #define CFG_DEV_FE 0x85c
  196. #define CFG_DEV_FE_PORTREGSEL_MASK GENMASK(1, 0)
  197. #define CFG_DEV_FE_PORTREGSEL_SS_PI 1
  198. #define CFG_DEV_FE_PORTREGSEL_HSFS_PI 2
  199. #define CFG_DEV_FE_PORTREGSEL(x) ((x) & CFG_DEV_FE_PORTREGSEL_MASK)
  200. #define CFG_DEV_FE_INFINITE_SS_RETRY BIT(29)
  201. /* FPCI registers */
  202. #define XUSB_DEV_CFG_1 0x004
  203. #define XUSB_DEV_CFG_1_IO_SPACE_EN BIT(0)
  204. #define XUSB_DEV_CFG_1_MEMORY_SPACE_EN BIT(1)
  205. #define XUSB_DEV_CFG_1_BUS_MASTER_EN BIT(2)
  206. #define XUSB_DEV_CFG_4 0x010
  207. #define XUSB_DEV_CFG_4_BASE_ADDR_MASK GENMASK(31, 15)
  208. #define XUSB_DEV_CFG_5 0x014
  209. /* IPFS registers */
  210. #define XUSB_DEV_CONFIGURATION_0 0x180
  211. #define XUSB_DEV_CONFIGURATION_0_EN_FPCI BIT(0)
  212. #define XUSB_DEV_INTR_MASK_0 0x188
  213. #define XUSB_DEV_INTR_MASK_0_IP_INT_MASK BIT(16)
  214. struct tegra_xudc_ep_context {
  215. __le32 info0;
  216. __le32 info1;
  217. __le32 deq_lo;
  218. __le32 deq_hi;
  219. __le32 tx_info;
  220. __le32 rsvd[11];
  221. };
  222. #define EP_STATE_DISABLED 0
  223. #define EP_STATE_RUNNING 1
  224. #define EP_STATE_HALTED 2
  225. #define EP_STATE_STOPPED 3
  226. #define EP_STATE_ERROR 4
  227. #define EP_TYPE_INVALID 0
  228. #define EP_TYPE_ISOCH_OUT 1
  229. #define EP_TYPE_BULK_OUT 2
  230. #define EP_TYPE_INTERRUPT_OUT 3
  231. #define EP_TYPE_CONTROL 4
  232. #define EP_TYPE_ISCOH_IN 5
  233. #define EP_TYPE_BULK_IN 6
  234. #define EP_TYPE_INTERRUPT_IN 7
  235. #define BUILD_EP_CONTEXT_RW(name, member, shift, mask) \
  236. static inline u32 ep_ctx_read_##name(struct tegra_xudc_ep_context *ctx) \
  237. { \
  238. return (le32_to_cpu(ctx->member) >> (shift)) & (mask); \
  239. } \
  240. static inline void \
  241. ep_ctx_write_##name(struct tegra_xudc_ep_context *ctx, u32 val) \
  242. { \
  243. u32 tmp; \
  244. \
  245. tmp = le32_to_cpu(ctx->member) & ~((mask) << (shift)); \
  246. tmp |= (val & (mask)) << (shift); \
  247. ctx->member = cpu_to_le32(tmp); \
  248. }
  249. BUILD_EP_CONTEXT_RW(state, info0, 0, 0x7)
  250. BUILD_EP_CONTEXT_RW(mult, info0, 8, 0x3)
  251. BUILD_EP_CONTEXT_RW(max_pstreams, info0, 10, 0x1f)
  252. BUILD_EP_CONTEXT_RW(lsa, info0, 15, 0x1)
  253. BUILD_EP_CONTEXT_RW(interval, info0, 16, 0xff)
  254. BUILD_EP_CONTEXT_RW(cerr, info1, 1, 0x3)
  255. BUILD_EP_CONTEXT_RW(type, info1, 3, 0x7)
  256. BUILD_EP_CONTEXT_RW(hid, info1, 7, 0x1)
  257. BUILD_EP_CONTEXT_RW(max_burst_size, info1, 8, 0xff)
  258. BUILD_EP_CONTEXT_RW(max_packet_size, info1, 16, 0xffff)
  259. BUILD_EP_CONTEXT_RW(dcs, deq_lo, 0, 0x1)
  260. BUILD_EP_CONTEXT_RW(deq_lo, deq_lo, 4, 0xfffffff)
  261. BUILD_EP_CONTEXT_RW(deq_hi, deq_hi, 0, 0xffffffff)
  262. BUILD_EP_CONTEXT_RW(avg_trb_len, tx_info, 0, 0xffff)
  263. BUILD_EP_CONTEXT_RW(max_esit_payload, tx_info, 16, 0xffff)
  264. BUILD_EP_CONTEXT_RW(edtla, rsvd[0], 0, 0xffffff)
  265. BUILD_EP_CONTEXT_RW(rsvd, rsvd[0], 24, 0x1)
  266. BUILD_EP_CONTEXT_RW(partial_td, rsvd[0], 25, 0x1)
  267. BUILD_EP_CONTEXT_RW(splitxstate, rsvd[0], 26, 0x1)
  268. BUILD_EP_CONTEXT_RW(seq_num, rsvd[0], 27, 0x1f)
  269. BUILD_EP_CONTEXT_RW(cerrcnt, rsvd[1], 18, 0x3)
  270. BUILD_EP_CONTEXT_RW(data_offset, rsvd[2], 0, 0x1ffff)
  271. BUILD_EP_CONTEXT_RW(numtrbs, rsvd[2], 22, 0x1f)
  272. BUILD_EP_CONTEXT_RW(devaddr, rsvd[6], 0, 0x7f)
  273. static inline u64 ep_ctx_read_deq_ptr(struct tegra_xudc_ep_context *ctx)
  274. {
  275. return ((u64)ep_ctx_read_deq_hi(ctx) << 32) |
  276. (ep_ctx_read_deq_lo(ctx) << 4);
  277. }
  278. static inline void
  279. ep_ctx_write_deq_ptr(struct tegra_xudc_ep_context *ctx, u64 addr)
  280. {
  281. ep_ctx_write_deq_lo(ctx, lower_32_bits(addr) >> 4);
  282. ep_ctx_write_deq_hi(ctx, upper_32_bits(addr));
  283. }
  284. struct tegra_xudc_trb {
  285. __le32 data_lo;
  286. __le32 data_hi;
  287. __le32 status;
  288. __le32 control;
  289. };
  290. #define TRB_TYPE_RSVD 0
  291. #define TRB_TYPE_NORMAL 1
  292. #define TRB_TYPE_SETUP_STAGE 2
  293. #define TRB_TYPE_DATA_STAGE 3
  294. #define TRB_TYPE_STATUS_STAGE 4
  295. #define TRB_TYPE_ISOCH 5
  296. #define TRB_TYPE_LINK 6
  297. #define TRB_TYPE_TRANSFER_EVENT 32
  298. #define TRB_TYPE_PORT_STATUS_CHANGE_EVENT 34
  299. #define TRB_TYPE_STREAM 48
  300. #define TRB_TYPE_SETUP_PACKET_EVENT 63
  301. #define TRB_CMPL_CODE_INVALID 0
  302. #define TRB_CMPL_CODE_SUCCESS 1
  303. #define TRB_CMPL_CODE_DATA_BUFFER_ERR 2
  304. #define TRB_CMPL_CODE_BABBLE_DETECTED_ERR 3
  305. #define TRB_CMPL_CODE_USB_TRANS_ERR 4
  306. #define TRB_CMPL_CODE_TRB_ERR 5
  307. #define TRB_CMPL_CODE_STALL 6
  308. #define TRB_CMPL_CODE_INVALID_STREAM_TYPE_ERR 10
  309. #define TRB_CMPL_CODE_SHORT_PACKET 13
  310. #define TRB_CMPL_CODE_RING_UNDERRUN 14
  311. #define TRB_CMPL_CODE_RING_OVERRUN 15
  312. #define TRB_CMPL_CODE_EVENT_RING_FULL_ERR 21
  313. #define TRB_CMPL_CODE_STOPPED 26
  314. #define TRB_CMPL_CODE_ISOCH_BUFFER_OVERRUN 31
  315. #define TRB_CMPL_CODE_STREAM_NUMP_ERROR 219
  316. #define TRB_CMPL_CODE_PRIME_PIPE_RECEIVED 220
  317. #define TRB_CMPL_CODE_HOST_REJECTED 221
  318. #define TRB_CMPL_CODE_CTRL_DIR_ERR 222
  319. #define TRB_CMPL_CODE_CTRL_SEQNUM_ERR 223
  320. #define BUILD_TRB_RW(name, member, shift, mask) \
  321. static inline u32 trb_read_##name(struct tegra_xudc_trb *trb) \
  322. { \
  323. return (le32_to_cpu(trb->member) >> (shift)) & (mask); \
  324. } \
  325. static inline void \
  326. trb_write_##name(struct tegra_xudc_trb *trb, u32 val) \
  327. { \
  328. u32 tmp; \
  329. \
  330. tmp = le32_to_cpu(trb->member) & ~((mask) << (shift)); \
  331. tmp |= (val & (mask)) << (shift); \
  332. trb->member = cpu_to_le32(tmp); \
  333. }
  334. BUILD_TRB_RW(data_lo, data_lo, 0, 0xffffffff)
  335. BUILD_TRB_RW(data_hi, data_hi, 0, 0xffffffff)
  336. BUILD_TRB_RW(seq_num, status, 0, 0xffff)
  337. BUILD_TRB_RW(transfer_len, status, 0, 0xffffff)
  338. BUILD_TRB_RW(td_size, status, 17, 0x1f)
  339. BUILD_TRB_RW(cmpl_code, status, 24, 0xff)
  340. BUILD_TRB_RW(cycle, control, 0, 0x1)
  341. BUILD_TRB_RW(toggle_cycle, control, 1, 0x1)
  342. BUILD_TRB_RW(isp, control, 2, 0x1)
  343. BUILD_TRB_RW(chain, control, 4, 0x1)
  344. BUILD_TRB_RW(ioc, control, 5, 0x1)
  345. BUILD_TRB_RW(type, control, 10, 0x3f)
  346. BUILD_TRB_RW(stream_id, control, 16, 0xffff)
  347. BUILD_TRB_RW(endpoint_id, control, 16, 0x1f)
  348. BUILD_TRB_RW(tlbpc, control, 16, 0xf)
  349. BUILD_TRB_RW(data_stage_dir, control, 16, 0x1)
  350. BUILD_TRB_RW(frame_id, control, 20, 0x7ff)
  351. BUILD_TRB_RW(sia, control, 31, 0x1)
  352. static inline u64 trb_read_data_ptr(struct tegra_xudc_trb *trb)
  353. {
  354. return ((u64)trb_read_data_hi(trb) << 32) |
  355. trb_read_data_lo(trb);
  356. }
  357. static inline void trb_write_data_ptr(struct tegra_xudc_trb *trb, u64 addr)
  358. {
  359. trb_write_data_lo(trb, lower_32_bits(addr));
  360. trb_write_data_hi(trb, upper_32_bits(addr));
  361. }
  362. struct tegra_xudc_request {
  363. struct usb_request usb_req;
  364. size_t buf_queued;
  365. unsigned int trbs_queued;
  366. unsigned int trbs_needed;
  367. bool need_zlp;
  368. struct tegra_xudc_trb *first_trb;
  369. struct tegra_xudc_trb *last_trb;
  370. struct list_head list;
  371. };
  372. struct tegra_xudc_ep {
  373. struct tegra_xudc *xudc;
  374. struct usb_ep usb_ep;
  375. unsigned int index;
  376. char name[8];
  377. struct tegra_xudc_ep_context *context;
  378. #define XUDC_TRANSFER_RING_SIZE 64
  379. struct tegra_xudc_trb *transfer_ring;
  380. dma_addr_t transfer_ring_phys;
  381. unsigned int enq_ptr;
  382. unsigned int deq_ptr;
  383. bool pcs;
  384. bool ring_full;
  385. bool stream_rejected;
  386. struct list_head queue;
  387. const struct usb_endpoint_descriptor *desc;
  388. const struct usb_ss_ep_comp_descriptor *comp_desc;
  389. };
  390. struct tegra_xudc_sel_timing {
  391. __u8 u1sel;
  392. __u8 u1pel;
  393. __le16 u2sel;
  394. __le16 u2pel;
  395. };
  396. enum tegra_xudc_setup_state {
  397. WAIT_FOR_SETUP,
  398. DATA_STAGE_XFER,
  399. DATA_STAGE_RECV,
  400. STATUS_STAGE_XFER,
  401. STATUS_STAGE_RECV,
  402. };
  403. struct tegra_xudc_setup_packet {
  404. struct usb_ctrlrequest ctrl_req;
  405. unsigned int seq_num;
  406. };
  407. struct tegra_xudc_save_regs {
  408. u32 ctrl;
  409. u32 portpm;
  410. };
  411. struct tegra_xudc {
  412. struct device *dev;
  413. const struct tegra_xudc_soc *soc;
  414. struct tegra_xusb_padctl *padctl;
  415. spinlock_t lock;
  416. struct usb_gadget gadget;
  417. struct usb_gadget_driver *driver;
  418. #define XUDC_NR_EVENT_RINGS 2
  419. #define XUDC_EVENT_RING_SIZE 4096
  420. struct tegra_xudc_trb *event_ring[XUDC_NR_EVENT_RINGS];
  421. dma_addr_t event_ring_phys[XUDC_NR_EVENT_RINGS];
  422. unsigned int event_ring_index;
  423. unsigned int event_ring_deq_ptr;
  424. bool ccs;
  425. #define XUDC_NR_EPS 32
  426. struct tegra_xudc_ep ep[XUDC_NR_EPS];
  427. struct tegra_xudc_ep_context *ep_context;
  428. dma_addr_t ep_context_phys;
  429. struct device *genpd_dev_device;
  430. struct device *genpd_dev_ss;
  431. struct device_link *genpd_dl_device;
  432. struct device_link *genpd_dl_ss;
  433. struct dma_pool *transfer_ring_pool;
  434. bool queued_setup_packet;
  435. struct tegra_xudc_setup_packet setup_packet;
  436. enum tegra_xudc_setup_state setup_state;
  437. u16 setup_seq_num;
  438. u16 dev_addr;
  439. u16 isoch_delay;
  440. struct tegra_xudc_sel_timing sel_timing;
  441. u8 test_mode_pattern;
  442. u16 status_buf;
  443. struct tegra_xudc_request *ep0_req;
  444. bool pullup;
  445. unsigned int nr_enabled_eps;
  446. unsigned int nr_isoch_eps;
  447. unsigned int device_state;
  448. unsigned int resume_state;
  449. int irq;
  450. void __iomem *base;
  451. resource_size_t phys_base;
  452. void __iomem *ipfs;
  453. void __iomem *fpci;
  454. struct regulator_bulk_data *supplies;
  455. struct clk_bulk_data *clks;
  456. bool device_mode;
  457. bool current_device_mode;
  458. struct work_struct usb_role_sw_work;
  459. struct phy **usb3_phy;
  460. struct phy *curr_usb3_phy;
  461. struct phy **utmi_phy;
  462. struct phy *curr_utmi_phy;
  463. struct tegra_xudc_save_regs saved_regs;
  464. bool suspended;
  465. bool powergated;
  466. struct usb_phy **usbphy;
  467. struct usb_phy *curr_usbphy;
  468. struct notifier_block vbus_nb;
  469. struct completion disconnect_complete;
  470. bool selfpowered;
  471. #define TOGGLE_VBUS_WAIT_MS 100
  472. struct delayed_work plc_reset_work;
  473. bool wait_csc;
  474. struct delayed_work port_reset_war_work;
  475. bool wait_for_sec_prc;
  476. };
  477. #define XUDC_TRB_MAX_BUFFER_SIZE 65536
  478. #define XUDC_MAX_ISOCH_EPS 4
  479. #define XUDC_INTERRUPT_MODERATION_US 0
  480. static struct usb_endpoint_descriptor tegra_xudc_ep0_desc = {
  481. .bLength = USB_DT_ENDPOINT_SIZE,
  482. .bDescriptorType = USB_DT_ENDPOINT,
  483. .bEndpointAddress = 0,
  484. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  485. .wMaxPacketSize = cpu_to_le16(64),
  486. };
  487. struct tegra_xudc_soc {
  488. const char * const *supply_names;
  489. unsigned int num_supplies;
  490. const char * const *clock_names;
  491. unsigned int num_clks;
  492. unsigned int num_phys;
  493. bool u1_enable;
  494. bool u2_enable;
  495. bool lpm_enable;
  496. bool invalid_seq_num;
  497. bool pls_quirk;
  498. bool port_reset_quirk;
  499. bool port_speed_quirk;
  500. bool has_ipfs;
  501. };
  502. static inline u32 fpci_readl(struct tegra_xudc *xudc, unsigned int offset)
  503. {
  504. return readl(xudc->fpci + offset);
  505. }
  506. static inline void fpci_writel(struct tegra_xudc *xudc, u32 val,
  507. unsigned int offset)
  508. {
  509. writel(val, xudc->fpci + offset);
  510. }
  511. static inline u32 ipfs_readl(struct tegra_xudc *xudc, unsigned int offset)
  512. {
  513. return readl(xudc->ipfs + offset);
  514. }
  515. static inline void ipfs_writel(struct tegra_xudc *xudc, u32 val,
  516. unsigned int offset)
  517. {
  518. writel(val, xudc->ipfs + offset);
  519. }
  520. static inline u32 xudc_readl(struct tegra_xudc *xudc, unsigned int offset)
  521. {
  522. return readl(xudc->base + offset);
  523. }
  524. static inline void xudc_writel(struct tegra_xudc *xudc, u32 val,
  525. unsigned int offset)
  526. {
  527. writel(val, xudc->base + offset);
  528. }
  529. static inline int xudc_readl_poll(struct tegra_xudc *xudc,
  530. unsigned int offset, u32 mask, u32 val)
  531. {
  532. u32 regval;
  533. return readl_poll_timeout_atomic(xudc->base + offset, regval,
  534. (regval & mask) == val, 1, 100);
  535. }
  536. static inline struct tegra_xudc *to_xudc(struct usb_gadget *gadget)
  537. {
  538. return container_of(gadget, struct tegra_xudc, gadget);
  539. }
  540. static inline struct tegra_xudc_ep *to_xudc_ep(struct usb_ep *ep)
  541. {
  542. return container_of(ep, struct tegra_xudc_ep, usb_ep);
  543. }
  544. static inline struct tegra_xudc_request *to_xudc_req(struct usb_request *req)
  545. {
  546. return container_of(req, struct tegra_xudc_request, usb_req);
  547. }
  548. static inline void dump_trb(struct tegra_xudc *xudc, const char *type,
  549. struct tegra_xudc_trb *trb)
  550. {
  551. dev_dbg(xudc->dev,
  552. "%s: %p, lo = %#x, hi = %#x, status = %#x, control = %#x\n",
  553. type, trb, trb->data_lo, trb->data_hi, trb->status,
  554. trb->control);
  555. }
  556. static void tegra_xudc_limit_port_speed(struct tegra_xudc *xudc)
  557. {
  558. u32 val;
  559. /* limit port speed to gen 1 */
  560. val = xudc_readl(xudc, SSPX_CORE_CNT56);
  561. val &= ~(SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX_MASK);
  562. val |= SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX(0x260);
  563. xudc_writel(xudc, val, SSPX_CORE_CNT56);
  564. val = xudc_readl(xudc, SSPX_CORE_CNT57);
  565. val &= ~(SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX_MASK);
  566. val |= SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX(0x6D6);
  567. xudc_writel(xudc, val, SSPX_CORE_CNT57);
  568. val = xudc_readl(xudc, SSPX_CORE_CNT65);
  569. val &= ~(SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID_MASK);
  570. val |= SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID(0x4B0);
  571. xudc_writel(xudc, val, SSPX_CORE_CNT66);
  572. val = xudc_readl(xudc, SSPX_CORE_CNT66);
  573. val &= ~(SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID_MASK);
  574. val |= SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID(0x4B0);
  575. xudc_writel(xudc, val, SSPX_CORE_CNT66);
  576. val = xudc_readl(xudc, SSPX_CORE_CNT67);
  577. val &= ~(SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID_MASK);
  578. val |= SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID(0x4B0);
  579. xudc_writel(xudc, val, SSPX_CORE_CNT67);
  580. val = xudc_readl(xudc, SSPX_CORE_CNT72);
  581. val &= ~(SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT_MASK);
  582. val |= SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT(0x10);
  583. xudc_writel(xudc, val, SSPX_CORE_CNT72);
  584. }
  585. static void tegra_xudc_restore_port_speed(struct tegra_xudc *xudc)
  586. {
  587. u32 val;
  588. /* restore port speed to gen2 */
  589. val = xudc_readl(xudc, SSPX_CORE_CNT56);
  590. val &= ~(SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX_MASK);
  591. val |= SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX(0x438);
  592. xudc_writel(xudc, val, SSPX_CORE_CNT56);
  593. val = xudc_readl(xudc, SSPX_CORE_CNT57);
  594. val &= ~(SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX_MASK);
  595. val |= SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX(0x528);
  596. xudc_writel(xudc, val, SSPX_CORE_CNT57);
  597. val = xudc_readl(xudc, SSPX_CORE_CNT65);
  598. val &= ~(SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID_MASK);
  599. val |= SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID(0xE10);
  600. xudc_writel(xudc, val, SSPX_CORE_CNT66);
  601. val = xudc_readl(xudc, SSPX_CORE_CNT66);
  602. val &= ~(SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID_MASK);
  603. val |= SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID(0x348);
  604. xudc_writel(xudc, val, SSPX_CORE_CNT66);
  605. val = xudc_readl(xudc, SSPX_CORE_CNT67);
  606. val &= ~(SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID_MASK);
  607. val |= SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID(0x5a0);
  608. xudc_writel(xudc, val, SSPX_CORE_CNT67);
  609. val = xudc_readl(xudc, SSPX_CORE_CNT72);
  610. val &= ~(SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT_MASK);
  611. val |= SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT(0x1c21);
  612. xudc_writel(xudc, val, SSPX_CORE_CNT72);
  613. }
  614. static void tegra_xudc_device_mode_on(struct tegra_xudc *xudc)
  615. {
  616. int err;
  617. pm_runtime_get_sync(xudc->dev);
  618. tegra_phy_xusb_utmi_pad_power_on(xudc->curr_utmi_phy);
  619. err = phy_power_on(xudc->curr_utmi_phy);
  620. if (err < 0)
  621. dev_err(xudc->dev, "UTMI power on failed: %d\n", err);
  622. err = phy_power_on(xudc->curr_usb3_phy);
  623. if (err < 0)
  624. dev_err(xudc->dev, "USB3 PHY power on failed: %d\n", err);
  625. dev_dbg(xudc->dev, "device mode on\n");
  626. phy_set_mode_ext(xudc->curr_utmi_phy, PHY_MODE_USB_OTG,
  627. USB_ROLE_DEVICE);
  628. xudc->current_device_mode = true;
  629. }
  630. static void tegra_xudc_device_mode_off(struct tegra_xudc *xudc)
  631. {
  632. bool connected = false;
  633. u32 pls, val;
  634. int err;
  635. dev_dbg(xudc->dev, "device mode off\n");
  636. xudc->current_device_mode = false;
  637. connected = !!(xudc_readl(xudc, PORTSC) & PORTSC_CCS);
  638. reinit_completion(&xudc->disconnect_complete);
  639. if (xudc->soc->port_speed_quirk)
  640. tegra_xudc_restore_port_speed(xudc);
  641. phy_set_mode_ext(xudc->curr_utmi_phy, PHY_MODE_USB_OTG, USB_ROLE_NONE);
  642. pls = (xudc_readl(xudc, PORTSC) & PORTSC_PLS_MASK) >>
  643. PORTSC_PLS_SHIFT;
  644. /* Direct link to U0 if disconnected in RESUME or U2. */
  645. if (xudc->soc->pls_quirk && xudc->gadget.speed == USB_SPEED_SUPER &&
  646. (pls == PORTSC_PLS_RESUME || pls == PORTSC_PLS_U2)) {
  647. val = xudc_readl(xudc, PORTPM);
  648. val |= PORTPM_FRWE;
  649. xudc_writel(xudc, val, PORTPM);
  650. val = xudc_readl(xudc, PORTSC);
  651. val &= ~(PORTSC_CHANGE_MASK | PORTSC_PLS_MASK);
  652. val |= PORTSC_LWS | PORTSC_PLS(PORTSC_PLS_U0);
  653. xudc_writel(xudc, val, PORTSC);
  654. }
  655. /* Wait for disconnect event. */
  656. if (connected)
  657. wait_for_completion(&xudc->disconnect_complete);
  658. /* Make sure interrupt handler has completed before powergating. */
  659. synchronize_irq(xudc->irq);
  660. tegra_phy_xusb_utmi_pad_power_down(xudc->curr_utmi_phy);
  661. err = phy_power_off(xudc->curr_utmi_phy);
  662. if (err < 0)
  663. dev_err(xudc->dev, "UTMI PHY power off failed: %d\n", err);
  664. err = phy_power_off(xudc->curr_usb3_phy);
  665. if (err < 0)
  666. dev_err(xudc->dev, "USB3 PHY power off failed: %d\n", err);
  667. pm_runtime_put(xudc->dev);
  668. }
  669. static void tegra_xudc_usb_role_sw_work(struct work_struct *work)
  670. {
  671. struct tegra_xudc *xudc = container_of(work, struct tegra_xudc,
  672. usb_role_sw_work);
  673. if (xudc->device_mode)
  674. tegra_xudc_device_mode_on(xudc);
  675. else
  676. tegra_xudc_device_mode_off(xudc);
  677. }
  678. static int tegra_xudc_get_phy_index(struct tegra_xudc *xudc,
  679. struct usb_phy *usbphy)
  680. {
  681. unsigned int i;
  682. for (i = 0; i < xudc->soc->num_phys; i++) {
  683. if (xudc->usbphy[i] && usbphy == xudc->usbphy[i])
  684. return i;
  685. }
  686. dev_info(xudc->dev, "phy index could not be found for shared USB PHY");
  687. return -1;
  688. }
  689. static void tegra_xudc_update_data_role(struct tegra_xudc *xudc,
  690. struct usb_phy *usbphy)
  691. {
  692. int phy_index;
  693. if ((xudc->device_mode && usbphy->last_event == USB_EVENT_VBUS) ||
  694. (!xudc->device_mode && usbphy->last_event != USB_EVENT_VBUS)) {
  695. dev_dbg(xudc->dev, "Same role(%d) received. Ignore",
  696. xudc->device_mode);
  697. return;
  698. }
  699. xudc->device_mode = usbphy->last_event == USB_EVENT_VBUS;
  700. phy_index = tegra_xudc_get_phy_index(xudc, usbphy);
  701. dev_dbg(xudc->dev, "%s(): current phy index is %d\n", __func__,
  702. phy_index);
  703. if (!xudc->suspended && phy_index != -1) {
  704. xudc->curr_utmi_phy = xudc->utmi_phy[phy_index];
  705. xudc->curr_usb3_phy = xudc->usb3_phy[phy_index];
  706. xudc->curr_usbphy = usbphy;
  707. schedule_work(&xudc->usb_role_sw_work);
  708. }
  709. }
  710. static int tegra_xudc_vbus_notify(struct notifier_block *nb,
  711. unsigned long action, void *data)
  712. {
  713. struct tegra_xudc *xudc = container_of(nb, struct tegra_xudc,
  714. vbus_nb);
  715. struct usb_phy *usbphy = (struct usb_phy *)data;
  716. dev_dbg(xudc->dev, "%s(): event is %d\n", __func__, usbphy->last_event);
  717. tegra_xudc_update_data_role(xudc, usbphy);
  718. return NOTIFY_OK;
  719. }
  720. static void tegra_xudc_plc_reset_work(struct work_struct *work)
  721. {
  722. struct delayed_work *dwork = to_delayed_work(work);
  723. struct tegra_xudc *xudc = container_of(dwork, struct tegra_xudc,
  724. plc_reset_work);
  725. unsigned long flags;
  726. spin_lock_irqsave(&xudc->lock, flags);
  727. if (xudc->wait_csc) {
  728. u32 pls = (xudc_readl(xudc, PORTSC) & PORTSC_PLS_MASK) >>
  729. PORTSC_PLS_SHIFT;
  730. if (pls == PORTSC_PLS_INACTIVE) {
  731. dev_info(xudc->dev, "PLS = Inactive. Toggle VBUS\n");
  732. phy_set_mode_ext(xudc->curr_utmi_phy, PHY_MODE_USB_OTG,
  733. USB_ROLE_NONE);
  734. phy_set_mode_ext(xudc->curr_utmi_phy, PHY_MODE_USB_OTG,
  735. USB_ROLE_DEVICE);
  736. xudc->wait_csc = false;
  737. }
  738. }
  739. spin_unlock_irqrestore(&xudc->lock, flags);
  740. }
  741. static void tegra_xudc_port_reset_war_work(struct work_struct *work)
  742. {
  743. struct delayed_work *dwork = to_delayed_work(work);
  744. struct tegra_xudc *xudc =
  745. container_of(dwork, struct tegra_xudc, port_reset_war_work);
  746. unsigned long flags;
  747. u32 pls;
  748. int ret;
  749. spin_lock_irqsave(&xudc->lock, flags);
  750. if (xudc->device_mode && xudc->wait_for_sec_prc) {
  751. pls = (xudc_readl(xudc, PORTSC) & PORTSC_PLS_MASK) >>
  752. PORTSC_PLS_SHIFT;
  753. dev_dbg(xudc->dev, "pls = %x\n", pls);
  754. if (pls == PORTSC_PLS_DISABLED) {
  755. dev_dbg(xudc->dev, "toggle vbus\n");
  756. /* PRC doesn't complete in 100ms, toggle the vbus */
  757. ret = tegra_phy_xusb_utmi_port_reset(
  758. xudc->curr_utmi_phy);
  759. if (ret == 1)
  760. xudc->wait_for_sec_prc = 0;
  761. }
  762. }
  763. spin_unlock_irqrestore(&xudc->lock, flags);
  764. }
  765. static dma_addr_t trb_virt_to_phys(struct tegra_xudc_ep *ep,
  766. struct tegra_xudc_trb *trb)
  767. {
  768. unsigned int index;
  769. index = trb - ep->transfer_ring;
  770. if (WARN_ON(index >= XUDC_TRANSFER_RING_SIZE))
  771. return 0;
  772. return (ep->transfer_ring_phys + index * sizeof(*trb));
  773. }
  774. static struct tegra_xudc_trb *trb_phys_to_virt(struct tegra_xudc_ep *ep,
  775. dma_addr_t addr)
  776. {
  777. struct tegra_xudc_trb *trb;
  778. unsigned int index;
  779. index = (addr - ep->transfer_ring_phys) / sizeof(*trb);
  780. if (WARN_ON(index >= XUDC_TRANSFER_RING_SIZE))
  781. return NULL;
  782. trb = &ep->transfer_ring[index];
  783. return trb;
  784. }
  785. static void ep_reload(struct tegra_xudc *xudc, unsigned int ep)
  786. {
  787. xudc_writel(xudc, BIT(ep), EP_RELOAD);
  788. xudc_readl_poll(xudc, EP_RELOAD, BIT(ep), 0);
  789. }
  790. static void ep_pause(struct tegra_xudc *xudc, unsigned int ep)
  791. {
  792. u32 val;
  793. val = xudc_readl(xudc, EP_PAUSE);
  794. if (val & BIT(ep))
  795. return;
  796. val |= BIT(ep);
  797. xudc_writel(xudc, val, EP_PAUSE);
  798. xudc_readl_poll(xudc, EP_STCHG, BIT(ep), BIT(ep));
  799. xudc_writel(xudc, BIT(ep), EP_STCHG);
  800. }
  801. static void ep_unpause(struct tegra_xudc *xudc, unsigned int ep)
  802. {
  803. u32 val;
  804. val = xudc_readl(xudc, EP_PAUSE);
  805. if (!(val & BIT(ep)))
  806. return;
  807. val &= ~BIT(ep);
  808. xudc_writel(xudc, val, EP_PAUSE);
  809. xudc_readl_poll(xudc, EP_STCHG, BIT(ep), BIT(ep));
  810. xudc_writel(xudc, BIT(ep), EP_STCHG);
  811. }
  812. static void ep_unpause_all(struct tegra_xudc *xudc)
  813. {
  814. u32 val;
  815. val = xudc_readl(xudc, EP_PAUSE);
  816. xudc_writel(xudc, 0, EP_PAUSE);
  817. xudc_readl_poll(xudc, EP_STCHG, val, val);
  818. xudc_writel(xudc, val, EP_STCHG);
  819. }
  820. static void ep_halt(struct tegra_xudc *xudc, unsigned int ep)
  821. {
  822. u32 val;
  823. val = xudc_readl(xudc, EP_HALT);
  824. if (val & BIT(ep))
  825. return;
  826. val |= BIT(ep);
  827. xudc_writel(xudc, val, EP_HALT);
  828. xudc_readl_poll(xudc, EP_STCHG, BIT(ep), BIT(ep));
  829. xudc_writel(xudc, BIT(ep), EP_STCHG);
  830. }
  831. static void ep_unhalt(struct tegra_xudc *xudc, unsigned int ep)
  832. {
  833. u32 val;
  834. val = xudc_readl(xudc, EP_HALT);
  835. if (!(val & BIT(ep)))
  836. return;
  837. val &= ~BIT(ep);
  838. xudc_writel(xudc, val, EP_HALT);
  839. xudc_readl_poll(xudc, EP_STCHG, BIT(ep), BIT(ep));
  840. xudc_writel(xudc, BIT(ep), EP_STCHG);
  841. }
  842. static void ep_unhalt_all(struct tegra_xudc *xudc)
  843. {
  844. u32 val;
  845. val = xudc_readl(xudc, EP_HALT);
  846. if (!val)
  847. return;
  848. xudc_writel(xudc, 0, EP_HALT);
  849. xudc_readl_poll(xudc, EP_STCHG, val, val);
  850. xudc_writel(xudc, val, EP_STCHG);
  851. }
  852. static void ep_wait_for_stopped(struct tegra_xudc *xudc, unsigned int ep)
  853. {
  854. xudc_readl_poll(xudc, EP_STOPPED, BIT(ep), BIT(ep));
  855. xudc_writel(xudc, BIT(ep), EP_STOPPED);
  856. }
  857. static void ep_wait_for_inactive(struct tegra_xudc *xudc, unsigned int ep)
  858. {
  859. xudc_readl_poll(xudc, EP_THREAD_ACTIVE, BIT(ep), 0);
  860. }
  861. static void tegra_xudc_req_done(struct tegra_xudc_ep *ep,
  862. struct tegra_xudc_request *req, int status)
  863. {
  864. struct tegra_xudc *xudc = ep->xudc;
  865. dev_dbg(xudc->dev, "completing request %p on EP %u with status %d\n",
  866. req, ep->index, status);
  867. if (likely(req->usb_req.status == -EINPROGRESS))
  868. req->usb_req.status = status;
  869. list_del_init(&req->list);
  870. if (usb_endpoint_xfer_control(ep->desc)) {
  871. usb_gadget_unmap_request(&xudc->gadget, &req->usb_req,
  872. (xudc->setup_state ==
  873. DATA_STAGE_XFER));
  874. } else {
  875. usb_gadget_unmap_request(&xudc->gadget, &req->usb_req,
  876. usb_endpoint_dir_in(ep->desc));
  877. }
  878. spin_unlock(&xudc->lock);
  879. usb_gadget_giveback_request(&ep->usb_ep, &req->usb_req);
  880. spin_lock(&xudc->lock);
  881. }
  882. static void tegra_xudc_ep_nuke(struct tegra_xudc_ep *ep, int status)
  883. {
  884. struct tegra_xudc_request *req;
  885. while (!list_empty(&ep->queue)) {
  886. req = list_first_entry(&ep->queue, struct tegra_xudc_request,
  887. list);
  888. tegra_xudc_req_done(ep, req, status);
  889. }
  890. }
  891. static unsigned int ep_available_trbs(struct tegra_xudc_ep *ep)
  892. {
  893. if (ep->ring_full)
  894. return 0;
  895. if (ep->deq_ptr > ep->enq_ptr)
  896. return ep->deq_ptr - ep->enq_ptr - 1;
  897. return XUDC_TRANSFER_RING_SIZE - (ep->enq_ptr - ep->deq_ptr) - 2;
  898. }
  899. static void tegra_xudc_queue_one_trb(struct tegra_xudc_ep *ep,
  900. struct tegra_xudc_request *req,
  901. struct tegra_xudc_trb *trb,
  902. bool ioc)
  903. {
  904. struct tegra_xudc *xudc = ep->xudc;
  905. dma_addr_t buf_addr;
  906. size_t len;
  907. len = min_t(size_t, XUDC_TRB_MAX_BUFFER_SIZE, req->usb_req.length -
  908. req->buf_queued);
  909. if (len > 0)
  910. buf_addr = req->usb_req.dma + req->buf_queued;
  911. else
  912. buf_addr = 0;
  913. trb_write_data_ptr(trb, buf_addr);
  914. trb_write_transfer_len(trb, len);
  915. trb_write_td_size(trb, req->trbs_needed - req->trbs_queued - 1);
  916. if (req->trbs_queued == req->trbs_needed - 1 ||
  917. (req->need_zlp && req->trbs_queued == req->trbs_needed - 2))
  918. trb_write_chain(trb, 0);
  919. else
  920. trb_write_chain(trb, 1);
  921. trb_write_ioc(trb, ioc);
  922. if (usb_endpoint_dir_out(ep->desc) ||
  923. (usb_endpoint_xfer_control(ep->desc) &&
  924. (xudc->setup_state == DATA_STAGE_RECV)))
  925. trb_write_isp(trb, 1);
  926. else
  927. trb_write_isp(trb, 0);
  928. if (usb_endpoint_xfer_control(ep->desc)) {
  929. if (xudc->setup_state == DATA_STAGE_XFER ||
  930. xudc->setup_state == DATA_STAGE_RECV)
  931. trb_write_type(trb, TRB_TYPE_DATA_STAGE);
  932. else
  933. trb_write_type(trb, TRB_TYPE_STATUS_STAGE);
  934. if (xudc->setup_state == DATA_STAGE_XFER ||
  935. xudc->setup_state == STATUS_STAGE_XFER)
  936. trb_write_data_stage_dir(trb, 1);
  937. else
  938. trb_write_data_stage_dir(trb, 0);
  939. } else if (usb_endpoint_xfer_isoc(ep->desc)) {
  940. trb_write_type(trb, TRB_TYPE_ISOCH);
  941. trb_write_sia(trb, 1);
  942. trb_write_frame_id(trb, 0);
  943. trb_write_tlbpc(trb, 0);
  944. } else if (usb_ss_max_streams(ep->comp_desc)) {
  945. trb_write_type(trb, TRB_TYPE_STREAM);
  946. trb_write_stream_id(trb, req->usb_req.stream_id);
  947. } else {
  948. trb_write_type(trb, TRB_TYPE_NORMAL);
  949. trb_write_stream_id(trb, 0);
  950. }
  951. trb_write_cycle(trb, ep->pcs);
  952. req->trbs_queued++;
  953. req->buf_queued += len;
  954. dump_trb(xudc, "TRANSFER", trb);
  955. }
  956. static unsigned int tegra_xudc_queue_trbs(struct tegra_xudc_ep *ep,
  957. struct tegra_xudc_request *req)
  958. {
  959. unsigned int i, count, available;
  960. bool wait_td = false;
  961. available = ep_available_trbs(ep);
  962. count = req->trbs_needed - req->trbs_queued;
  963. if (available < count) {
  964. count = available;
  965. ep->ring_full = true;
  966. }
  967. /*
  968. * To generate zero-length packet on USB bus, SW needs schedule a
  969. * standalone zero-length TD. According to HW's behavior, SW needs
  970. * to schedule TDs in different ways for different endpoint types.
  971. *
  972. * For control endpoint:
  973. * - Data stage TD (IOC = 1, CH = 0)
  974. * - Ring doorbell and wait transfer event
  975. * - Data stage TD for ZLP (IOC = 1, CH = 0)
  976. * - Ring doorbell
  977. *
  978. * For bulk and interrupt endpoints:
  979. * - Normal transfer TD (IOC = 0, CH = 0)
  980. * - Normal transfer TD for ZLP (IOC = 1, CH = 0)
  981. * - Ring doorbell
  982. */
  983. if (req->need_zlp && usb_endpoint_xfer_control(ep->desc) && count > 1)
  984. wait_td = true;
  985. if (!req->first_trb)
  986. req->first_trb = &ep->transfer_ring[ep->enq_ptr];
  987. for (i = 0; i < count; i++) {
  988. struct tegra_xudc_trb *trb = &ep->transfer_ring[ep->enq_ptr];
  989. bool ioc = false;
  990. if ((i == count - 1) || (wait_td && i == count - 2))
  991. ioc = true;
  992. tegra_xudc_queue_one_trb(ep, req, trb, ioc);
  993. req->last_trb = trb;
  994. ep->enq_ptr++;
  995. if (ep->enq_ptr == XUDC_TRANSFER_RING_SIZE - 1) {
  996. trb = &ep->transfer_ring[ep->enq_ptr];
  997. trb_write_cycle(trb, ep->pcs);
  998. ep->pcs = !ep->pcs;
  999. ep->enq_ptr = 0;
  1000. }
  1001. if (ioc)
  1002. break;
  1003. }
  1004. return count;
  1005. }
  1006. static void tegra_xudc_ep_ring_doorbell(struct tegra_xudc_ep *ep)
  1007. {
  1008. struct tegra_xudc *xudc = ep->xudc;
  1009. u32 val;
  1010. if (list_empty(&ep->queue))
  1011. return;
  1012. val = DB_TARGET(ep->index);
  1013. if (usb_endpoint_xfer_control(ep->desc)) {
  1014. val |= DB_STREAMID(xudc->setup_seq_num);
  1015. } else if (usb_ss_max_streams(ep->comp_desc) > 0) {
  1016. struct tegra_xudc_request *req;
  1017. /* Don't ring doorbell if the stream has been rejected. */
  1018. if (ep->stream_rejected)
  1019. return;
  1020. req = list_first_entry(&ep->queue, struct tegra_xudc_request,
  1021. list);
  1022. val |= DB_STREAMID(req->usb_req.stream_id);
  1023. }
  1024. dev_dbg(xudc->dev, "ring doorbell: %#x\n", val);
  1025. xudc_writel(xudc, val, DB);
  1026. }
  1027. static void tegra_xudc_ep_kick_queue(struct tegra_xudc_ep *ep)
  1028. {
  1029. struct tegra_xudc_request *req;
  1030. bool trbs_queued = false;
  1031. list_for_each_entry(req, &ep->queue, list) {
  1032. if (ep->ring_full)
  1033. break;
  1034. if (tegra_xudc_queue_trbs(ep, req) > 0)
  1035. trbs_queued = true;
  1036. }
  1037. if (trbs_queued)
  1038. tegra_xudc_ep_ring_doorbell(ep);
  1039. }
  1040. static int
  1041. __tegra_xudc_ep_queue(struct tegra_xudc_ep *ep, struct tegra_xudc_request *req)
  1042. {
  1043. struct tegra_xudc *xudc = ep->xudc;
  1044. int err;
  1045. if (usb_endpoint_xfer_control(ep->desc) && !list_empty(&ep->queue)) {
  1046. dev_err(xudc->dev, "control EP has pending transfers\n");
  1047. return -EINVAL;
  1048. }
  1049. if (usb_endpoint_xfer_control(ep->desc)) {
  1050. err = usb_gadget_map_request(&xudc->gadget, &req->usb_req,
  1051. (xudc->setup_state ==
  1052. DATA_STAGE_XFER));
  1053. } else {
  1054. err = usb_gadget_map_request(&xudc->gadget, &req->usb_req,
  1055. usb_endpoint_dir_in(ep->desc));
  1056. }
  1057. if (err < 0) {
  1058. dev_err(xudc->dev, "failed to map request: %d\n", err);
  1059. return err;
  1060. }
  1061. req->first_trb = NULL;
  1062. req->last_trb = NULL;
  1063. req->buf_queued = 0;
  1064. req->trbs_queued = 0;
  1065. req->need_zlp = false;
  1066. req->trbs_needed = DIV_ROUND_UP(req->usb_req.length,
  1067. XUDC_TRB_MAX_BUFFER_SIZE);
  1068. if (req->usb_req.length == 0)
  1069. req->trbs_needed++;
  1070. if (!usb_endpoint_xfer_isoc(ep->desc) &&
  1071. req->usb_req.zero && req->usb_req.length &&
  1072. ((req->usb_req.length % ep->usb_ep.maxpacket) == 0)) {
  1073. req->trbs_needed++;
  1074. req->need_zlp = true;
  1075. }
  1076. req->usb_req.status = -EINPROGRESS;
  1077. req->usb_req.actual = 0;
  1078. list_add_tail(&req->list, &ep->queue);
  1079. tegra_xudc_ep_kick_queue(ep);
  1080. return 0;
  1081. }
  1082. static int
  1083. tegra_xudc_ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
  1084. gfp_t gfp)
  1085. {
  1086. struct tegra_xudc_request *req;
  1087. struct tegra_xudc_ep *ep;
  1088. struct tegra_xudc *xudc;
  1089. unsigned long flags;
  1090. int ret;
  1091. if (!usb_ep || !usb_req)
  1092. return -EINVAL;
  1093. ep = to_xudc_ep(usb_ep);
  1094. req = to_xudc_req(usb_req);
  1095. xudc = ep->xudc;
  1096. spin_lock_irqsave(&xudc->lock, flags);
  1097. if (xudc->powergated || !ep->desc) {
  1098. ret = -ESHUTDOWN;
  1099. goto unlock;
  1100. }
  1101. ret = __tegra_xudc_ep_queue(ep, req);
  1102. unlock:
  1103. spin_unlock_irqrestore(&xudc->lock, flags);
  1104. return ret;
  1105. }
  1106. static void squeeze_transfer_ring(struct tegra_xudc_ep *ep,
  1107. struct tegra_xudc_request *req)
  1108. {
  1109. struct tegra_xudc_trb *trb = req->first_trb;
  1110. bool pcs_enq = trb_read_cycle(trb);
  1111. bool pcs;
  1112. /*
  1113. * Clear out all the TRBs part of or after the cancelled request,
  1114. * and must correct trb cycle bit to the last un-enqueued state.
  1115. */
  1116. while (trb != &ep->transfer_ring[ep->enq_ptr]) {
  1117. pcs = trb_read_cycle(trb);
  1118. memset(trb, 0, sizeof(*trb));
  1119. trb_write_cycle(trb, !pcs);
  1120. trb++;
  1121. if (trb_read_type(trb) == TRB_TYPE_LINK)
  1122. trb = ep->transfer_ring;
  1123. }
  1124. /* Requests will be re-queued at the start of the cancelled request. */
  1125. ep->enq_ptr = req->first_trb - ep->transfer_ring;
  1126. /*
  1127. * Retrieve the correct cycle bit state from the first trb of
  1128. * the cancelled request.
  1129. */
  1130. ep->pcs = pcs_enq;
  1131. ep->ring_full = false;
  1132. list_for_each_entry_continue(req, &ep->queue, list) {
  1133. req->usb_req.status = -EINPROGRESS;
  1134. req->usb_req.actual = 0;
  1135. req->first_trb = NULL;
  1136. req->last_trb = NULL;
  1137. req->buf_queued = 0;
  1138. req->trbs_queued = 0;
  1139. }
  1140. }
  1141. /*
  1142. * Determine if the given TRB is in the range [first trb, last trb] for the
  1143. * given request.
  1144. */
  1145. static bool trb_in_request(struct tegra_xudc_ep *ep,
  1146. struct tegra_xudc_request *req,
  1147. struct tegra_xudc_trb *trb)
  1148. {
  1149. dev_dbg(ep->xudc->dev, "%s: request %p -> %p; trb %p\n", __func__,
  1150. req->first_trb, req->last_trb, trb);
  1151. if (trb >= req->first_trb && (trb <= req->last_trb ||
  1152. req->last_trb < req->first_trb))
  1153. return true;
  1154. if (trb < req->first_trb && trb <= req->last_trb &&
  1155. req->last_trb < req->first_trb)
  1156. return true;
  1157. return false;
  1158. }
  1159. /*
  1160. * Determine if the given TRB is in the range [EP enqueue pointer, first TRB)
  1161. * for the given endpoint and request.
  1162. */
  1163. static bool trb_before_request(struct tegra_xudc_ep *ep,
  1164. struct tegra_xudc_request *req,
  1165. struct tegra_xudc_trb *trb)
  1166. {
  1167. struct tegra_xudc_trb *enq_trb = &ep->transfer_ring[ep->enq_ptr];
  1168. dev_dbg(ep->xudc->dev, "%s: request %p -> %p; enq ptr: %p; trb %p\n",
  1169. __func__, req->first_trb, req->last_trb, enq_trb, trb);
  1170. if (trb < req->first_trb && (enq_trb <= trb ||
  1171. req->first_trb < enq_trb))
  1172. return true;
  1173. if (trb > req->first_trb && req->first_trb < enq_trb && enq_trb <= trb)
  1174. return true;
  1175. return false;
  1176. }
  1177. static int
  1178. __tegra_xudc_ep_dequeue(struct tegra_xudc_ep *ep,
  1179. struct tegra_xudc_request *req)
  1180. {
  1181. struct tegra_xudc *xudc = ep->xudc;
  1182. struct tegra_xudc_request *r = NULL, *iter;
  1183. struct tegra_xudc_trb *deq_trb;
  1184. bool busy, kick_queue = false;
  1185. int ret = 0;
  1186. /* Make sure the request is actually queued to this endpoint. */
  1187. list_for_each_entry(iter, &ep->queue, list) {
  1188. if (iter != req)
  1189. continue;
  1190. r = iter;
  1191. break;
  1192. }
  1193. if (!r)
  1194. return -EINVAL;
  1195. /* Request hasn't been queued in the transfer ring yet. */
  1196. if (!req->trbs_queued) {
  1197. tegra_xudc_req_done(ep, req, -ECONNRESET);
  1198. return 0;
  1199. }
  1200. /* Halt DMA for this endpoint. */
  1201. if (ep_ctx_read_state(ep->context) == EP_STATE_RUNNING) {
  1202. ep_pause(xudc, ep->index);
  1203. ep_wait_for_inactive(xudc, ep->index);
  1204. }
  1205. deq_trb = trb_phys_to_virt(ep, ep_ctx_read_deq_ptr(ep->context));
  1206. /* Is the hardware processing the TRB at the dequeue pointer? */
  1207. busy = (trb_read_cycle(deq_trb) == ep_ctx_read_dcs(ep->context));
  1208. if (trb_in_request(ep, req, deq_trb) && busy) {
  1209. /*
  1210. * Request has been partially completed or it hasn't
  1211. * started processing yet.
  1212. */
  1213. dma_addr_t deq_ptr;
  1214. squeeze_transfer_ring(ep, req);
  1215. req->usb_req.actual = ep_ctx_read_edtla(ep->context);
  1216. tegra_xudc_req_done(ep, req, -ECONNRESET);
  1217. kick_queue = true;
  1218. /* EDTLA is > 0: request has been partially completed */
  1219. if (req->usb_req.actual > 0) {
  1220. /*
  1221. * Abort the pending transfer and update the dequeue
  1222. * pointer
  1223. */
  1224. ep_ctx_write_edtla(ep->context, 0);
  1225. ep_ctx_write_partial_td(ep->context, 0);
  1226. ep_ctx_write_data_offset(ep->context, 0);
  1227. deq_ptr = trb_virt_to_phys(ep,
  1228. &ep->transfer_ring[ep->enq_ptr]);
  1229. if (dma_mapping_error(xudc->dev, deq_ptr)) {
  1230. ret = -EINVAL;
  1231. } else {
  1232. ep_ctx_write_deq_ptr(ep->context, deq_ptr);
  1233. ep_ctx_write_dcs(ep->context, ep->pcs);
  1234. ep_reload(xudc, ep->index);
  1235. }
  1236. }
  1237. } else if (trb_before_request(ep, req, deq_trb) && busy) {
  1238. /* Request hasn't started processing yet. */
  1239. squeeze_transfer_ring(ep, req);
  1240. tegra_xudc_req_done(ep, req, -ECONNRESET);
  1241. kick_queue = true;
  1242. } else {
  1243. /*
  1244. * Request has completed, but we haven't processed the
  1245. * completion event yet.
  1246. */
  1247. tegra_xudc_req_done(ep, req, -ECONNRESET);
  1248. ret = -EINVAL;
  1249. }
  1250. /* Resume the endpoint. */
  1251. ep_unpause(xudc, ep->index);
  1252. if (kick_queue)
  1253. tegra_xudc_ep_kick_queue(ep);
  1254. return ret;
  1255. }
  1256. static int
  1257. tegra_xudc_ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
  1258. {
  1259. struct tegra_xudc_request *req;
  1260. struct tegra_xudc_ep *ep;
  1261. struct tegra_xudc *xudc;
  1262. unsigned long flags;
  1263. int ret;
  1264. if (!usb_ep || !usb_req)
  1265. return -EINVAL;
  1266. ep = to_xudc_ep(usb_ep);
  1267. req = to_xudc_req(usb_req);
  1268. xudc = ep->xudc;
  1269. spin_lock_irqsave(&xudc->lock, flags);
  1270. if (xudc->powergated || !ep->desc) {
  1271. ret = -ESHUTDOWN;
  1272. goto unlock;
  1273. }
  1274. ret = __tegra_xudc_ep_dequeue(ep, req);
  1275. unlock:
  1276. spin_unlock_irqrestore(&xudc->lock, flags);
  1277. return ret;
  1278. }
  1279. static int __tegra_xudc_ep_set_halt(struct tegra_xudc_ep *ep, bool halt)
  1280. {
  1281. struct tegra_xudc *xudc = ep->xudc;
  1282. if (!ep->desc)
  1283. return -EINVAL;
  1284. if (usb_endpoint_xfer_isoc(ep->desc)) {
  1285. dev_err(xudc->dev, "can't halt isochronous EP\n");
  1286. return -ENOTSUPP;
  1287. }
  1288. if (halt) {
  1289. ep_halt(xudc, ep->index);
  1290. } else {
  1291. ep_ctx_write_state(ep->context, EP_STATE_DISABLED);
  1292. ep_reload(xudc, ep->index);
  1293. ep_ctx_write_state(ep->context, EP_STATE_RUNNING);
  1294. ep_ctx_write_rsvd(ep->context, 0);
  1295. ep_ctx_write_partial_td(ep->context, 0);
  1296. ep_ctx_write_splitxstate(ep->context, 0);
  1297. ep_ctx_write_seq_num(ep->context, 0);
  1298. ep_reload(xudc, ep->index);
  1299. ep_unpause(xudc, ep->index);
  1300. ep_unhalt(xudc, ep->index);
  1301. tegra_xudc_ep_ring_doorbell(ep);
  1302. }
  1303. return 0;
  1304. }
  1305. static int tegra_xudc_ep_set_halt(struct usb_ep *usb_ep, int value)
  1306. {
  1307. struct tegra_xudc_ep *ep;
  1308. struct tegra_xudc *xudc;
  1309. unsigned long flags;
  1310. int ret;
  1311. if (!usb_ep)
  1312. return -EINVAL;
  1313. ep = to_xudc_ep(usb_ep);
  1314. xudc = ep->xudc;
  1315. spin_lock_irqsave(&xudc->lock, flags);
  1316. if (xudc->powergated) {
  1317. ret = -ESHUTDOWN;
  1318. goto unlock;
  1319. }
  1320. if (value && usb_endpoint_dir_in(ep->desc) &&
  1321. !list_empty(&ep->queue)) {
  1322. dev_err(xudc->dev, "can't halt EP with requests pending\n");
  1323. ret = -EAGAIN;
  1324. goto unlock;
  1325. }
  1326. ret = __tegra_xudc_ep_set_halt(ep, value);
  1327. unlock:
  1328. spin_unlock_irqrestore(&xudc->lock, flags);
  1329. return ret;
  1330. }
  1331. static void tegra_xudc_ep_context_setup(struct tegra_xudc_ep *ep)
  1332. {
  1333. const struct usb_endpoint_descriptor *desc = ep->desc;
  1334. const struct usb_ss_ep_comp_descriptor *comp_desc = ep->comp_desc;
  1335. struct tegra_xudc *xudc = ep->xudc;
  1336. u16 maxpacket, maxburst = 0, esit = 0;
  1337. u32 val;
  1338. maxpacket = usb_endpoint_maxp(desc);
  1339. if (xudc->gadget.speed == USB_SPEED_SUPER) {
  1340. if (!usb_endpoint_xfer_control(desc))
  1341. maxburst = comp_desc->bMaxBurst;
  1342. if (usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc))
  1343. esit = le16_to_cpu(comp_desc->wBytesPerInterval);
  1344. } else if ((xudc->gadget.speed < USB_SPEED_SUPER) &&
  1345. (usb_endpoint_xfer_int(desc) ||
  1346. usb_endpoint_xfer_isoc(desc))) {
  1347. if (xudc->gadget.speed == USB_SPEED_HIGH) {
  1348. maxburst = usb_endpoint_maxp_mult(desc) - 1;
  1349. if (maxburst == 0x3) {
  1350. dev_warn(xudc->dev,
  1351. "invalid endpoint maxburst\n");
  1352. maxburst = 0x2;
  1353. }
  1354. }
  1355. esit = maxpacket * (maxburst + 1);
  1356. }
  1357. memset(ep->context, 0, sizeof(*ep->context));
  1358. ep_ctx_write_state(ep->context, EP_STATE_RUNNING);
  1359. ep_ctx_write_interval(ep->context, desc->bInterval);
  1360. if (xudc->gadget.speed == USB_SPEED_SUPER) {
  1361. if (usb_endpoint_xfer_isoc(desc)) {
  1362. ep_ctx_write_mult(ep->context,
  1363. comp_desc->bmAttributes & 0x3);
  1364. }
  1365. if (usb_endpoint_xfer_bulk(desc)) {
  1366. ep_ctx_write_max_pstreams(ep->context,
  1367. comp_desc->bmAttributes &
  1368. 0x1f);
  1369. ep_ctx_write_lsa(ep->context, 1);
  1370. }
  1371. }
  1372. if (!usb_endpoint_xfer_control(desc) && usb_endpoint_dir_out(desc))
  1373. val = usb_endpoint_type(desc);
  1374. else
  1375. val = usb_endpoint_type(desc) + EP_TYPE_CONTROL;
  1376. ep_ctx_write_type(ep->context, val);
  1377. ep_ctx_write_cerr(ep->context, 0x3);
  1378. ep_ctx_write_max_packet_size(ep->context, maxpacket);
  1379. ep_ctx_write_max_burst_size(ep->context, maxburst);
  1380. ep_ctx_write_deq_ptr(ep->context, ep->transfer_ring_phys);
  1381. ep_ctx_write_dcs(ep->context, ep->pcs);
  1382. /* Select a reasonable average TRB length based on endpoint type. */
  1383. switch (usb_endpoint_type(desc)) {
  1384. case USB_ENDPOINT_XFER_CONTROL:
  1385. val = 8;
  1386. break;
  1387. case USB_ENDPOINT_XFER_INT:
  1388. val = 1024;
  1389. break;
  1390. case USB_ENDPOINT_XFER_BULK:
  1391. case USB_ENDPOINT_XFER_ISOC:
  1392. default:
  1393. val = 3072;
  1394. break;
  1395. }
  1396. ep_ctx_write_avg_trb_len(ep->context, val);
  1397. ep_ctx_write_max_esit_payload(ep->context, esit);
  1398. ep_ctx_write_cerrcnt(ep->context, 0x3);
  1399. }
  1400. static void setup_link_trb(struct tegra_xudc_ep *ep,
  1401. struct tegra_xudc_trb *trb)
  1402. {
  1403. trb_write_data_ptr(trb, ep->transfer_ring_phys);
  1404. trb_write_type(trb, TRB_TYPE_LINK);
  1405. trb_write_toggle_cycle(trb, 1);
  1406. }
  1407. static int __tegra_xudc_ep_disable(struct tegra_xudc_ep *ep)
  1408. {
  1409. struct tegra_xudc *xudc = ep->xudc;
  1410. if (ep_ctx_read_state(ep->context) == EP_STATE_DISABLED) {
  1411. dev_err(xudc->dev, "endpoint %u already disabled\n",
  1412. ep->index);
  1413. return -EINVAL;
  1414. }
  1415. ep_ctx_write_state(ep->context, EP_STATE_DISABLED);
  1416. ep_reload(xudc, ep->index);
  1417. tegra_xudc_ep_nuke(ep, -ESHUTDOWN);
  1418. xudc->nr_enabled_eps--;
  1419. if (usb_endpoint_xfer_isoc(ep->desc))
  1420. xudc->nr_isoch_eps--;
  1421. ep->desc = NULL;
  1422. ep->comp_desc = NULL;
  1423. memset(ep->context, 0, sizeof(*ep->context));
  1424. ep_unpause(xudc, ep->index);
  1425. ep_unhalt(xudc, ep->index);
  1426. if (xudc_readl(xudc, EP_STOPPED) & BIT(ep->index))
  1427. xudc_writel(xudc, BIT(ep->index), EP_STOPPED);
  1428. /*
  1429. * If this is the last endpoint disabled in a de-configure request,
  1430. * switch back to address state.
  1431. */
  1432. if ((xudc->device_state == USB_STATE_CONFIGURED) &&
  1433. (xudc->nr_enabled_eps == 1)) {
  1434. u32 val;
  1435. xudc->device_state = USB_STATE_ADDRESS;
  1436. usb_gadget_set_state(&xudc->gadget, xudc->device_state);
  1437. val = xudc_readl(xudc, CTRL);
  1438. val &= ~CTRL_RUN;
  1439. xudc_writel(xudc, val, CTRL);
  1440. val = xudc_readl(xudc, ST);
  1441. if (val & ST_RC)
  1442. xudc_writel(xudc, ST_RC, ST);
  1443. }
  1444. dev_info(xudc->dev, "ep %u disabled\n", ep->index);
  1445. return 0;
  1446. }
  1447. static int tegra_xudc_ep_disable(struct usb_ep *usb_ep)
  1448. {
  1449. struct tegra_xudc_ep *ep;
  1450. struct tegra_xudc *xudc;
  1451. unsigned long flags;
  1452. int ret;
  1453. if (!usb_ep)
  1454. return -EINVAL;
  1455. ep = to_xudc_ep(usb_ep);
  1456. xudc = ep->xudc;
  1457. spin_lock_irqsave(&xudc->lock, flags);
  1458. if (xudc->powergated) {
  1459. ret = -ESHUTDOWN;
  1460. goto unlock;
  1461. }
  1462. ret = __tegra_xudc_ep_disable(ep);
  1463. unlock:
  1464. spin_unlock_irqrestore(&xudc->lock, flags);
  1465. return ret;
  1466. }
  1467. static int __tegra_xudc_ep_enable(struct tegra_xudc_ep *ep,
  1468. const struct usb_endpoint_descriptor *desc)
  1469. {
  1470. struct tegra_xudc *xudc = ep->xudc;
  1471. unsigned int i;
  1472. u32 val;
  1473. if (xudc->gadget.speed == USB_SPEED_SUPER &&
  1474. !usb_endpoint_xfer_control(desc) && !ep->usb_ep.comp_desc)
  1475. return -EINVAL;
  1476. /* Disable the EP if it is not disabled */
  1477. if (ep_ctx_read_state(ep->context) != EP_STATE_DISABLED)
  1478. __tegra_xudc_ep_disable(ep);
  1479. ep->desc = desc;
  1480. ep->comp_desc = ep->usb_ep.comp_desc;
  1481. if (usb_endpoint_xfer_isoc(desc)) {
  1482. if (xudc->nr_isoch_eps > XUDC_MAX_ISOCH_EPS) {
  1483. dev_err(xudc->dev, "too many isochronous endpoints\n");
  1484. return -EBUSY;
  1485. }
  1486. xudc->nr_isoch_eps++;
  1487. }
  1488. memset(ep->transfer_ring, 0, XUDC_TRANSFER_RING_SIZE *
  1489. sizeof(*ep->transfer_ring));
  1490. setup_link_trb(ep, &ep->transfer_ring[XUDC_TRANSFER_RING_SIZE - 1]);
  1491. ep->enq_ptr = 0;
  1492. ep->deq_ptr = 0;
  1493. ep->pcs = true;
  1494. ep->ring_full = false;
  1495. xudc->nr_enabled_eps++;
  1496. tegra_xudc_ep_context_setup(ep);
  1497. /*
  1498. * No need to reload and un-halt EP0. This will be done automatically
  1499. * once a valid SETUP packet is received.
  1500. */
  1501. if (usb_endpoint_xfer_control(desc))
  1502. goto out;
  1503. /*
  1504. * Transition to configured state once the first non-control
  1505. * endpoint is enabled.
  1506. */
  1507. if (xudc->device_state == USB_STATE_ADDRESS) {
  1508. val = xudc_readl(xudc, CTRL);
  1509. val |= CTRL_RUN;
  1510. xudc_writel(xudc, val, CTRL);
  1511. xudc->device_state = USB_STATE_CONFIGURED;
  1512. usb_gadget_set_state(&xudc->gadget, xudc->device_state);
  1513. }
  1514. if (usb_endpoint_xfer_isoc(desc)) {
  1515. /*
  1516. * Pause all bulk endpoints when enabling an isoch endpoint
  1517. * to ensure the isoch endpoint is allocated enough bandwidth.
  1518. */
  1519. for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) {
  1520. if (xudc->ep[i].desc &&
  1521. usb_endpoint_xfer_bulk(xudc->ep[i].desc))
  1522. ep_pause(xudc, i);
  1523. }
  1524. }
  1525. ep_reload(xudc, ep->index);
  1526. ep_unpause(xudc, ep->index);
  1527. ep_unhalt(xudc, ep->index);
  1528. if (usb_endpoint_xfer_isoc(desc)) {
  1529. for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) {
  1530. if (xudc->ep[i].desc &&
  1531. usb_endpoint_xfer_bulk(xudc->ep[i].desc))
  1532. ep_unpause(xudc, i);
  1533. }
  1534. }
  1535. out:
  1536. dev_info(xudc->dev, "EP %u (type: %s, dir: %s) enabled\n", ep->index,
  1537. usb_ep_type_string(usb_endpoint_type(ep->desc)),
  1538. usb_endpoint_dir_in(ep->desc) ? "in" : "out");
  1539. return 0;
  1540. }
  1541. static int tegra_xudc_ep_enable(struct usb_ep *usb_ep,
  1542. const struct usb_endpoint_descriptor *desc)
  1543. {
  1544. struct tegra_xudc_ep *ep;
  1545. struct tegra_xudc *xudc;
  1546. unsigned long flags;
  1547. int ret;
  1548. if (!usb_ep || !desc || (desc->bDescriptorType != USB_DT_ENDPOINT))
  1549. return -EINVAL;
  1550. ep = to_xudc_ep(usb_ep);
  1551. xudc = ep->xudc;
  1552. spin_lock_irqsave(&xudc->lock, flags);
  1553. if (xudc->powergated) {
  1554. ret = -ESHUTDOWN;
  1555. goto unlock;
  1556. }
  1557. ret = __tegra_xudc_ep_enable(ep, desc);
  1558. unlock:
  1559. spin_unlock_irqrestore(&xudc->lock, flags);
  1560. return ret;
  1561. }
  1562. static struct usb_request *
  1563. tegra_xudc_ep_alloc_request(struct usb_ep *usb_ep, gfp_t gfp)
  1564. {
  1565. struct tegra_xudc_request *req;
  1566. req = kzalloc_obj(*req, gfp);
  1567. if (!req)
  1568. return NULL;
  1569. INIT_LIST_HEAD(&req->list);
  1570. return &req->usb_req;
  1571. }
  1572. static void tegra_xudc_ep_free_request(struct usb_ep *usb_ep,
  1573. struct usb_request *usb_req)
  1574. {
  1575. struct tegra_xudc_request *req = to_xudc_req(usb_req);
  1576. kfree(req);
  1577. }
  1578. static const struct usb_ep_ops tegra_xudc_ep_ops = {
  1579. .enable = tegra_xudc_ep_enable,
  1580. .disable = tegra_xudc_ep_disable,
  1581. .alloc_request = tegra_xudc_ep_alloc_request,
  1582. .free_request = tegra_xudc_ep_free_request,
  1583. .queue = tegra_xudc_ep_queue,
  1584. .dequeue = tegra_xudc_ep_dequeue,
  1585. .set_halt = tegra_xudc_ep_set_halt,
  1586. };
  1587. static int tegra_xudc_ep0_enable(struct usb_ep *usb_ep,
  1588. const struct usb_endpoint_descriptor *desc)
  1589. {
  1590. return -EBUSY;
  1591. }
  1592. static int tegra_xudc_ep0_disable(struct usb_ep *usb_ep)
  1593. {
  1594. return -EBUSY;
  1595. }
  1596. static const struct usb_ep_ops tegra_xudc_ep0_ops = {
  1597. .enable = tegra_xudc_ep0_enable,
  1598. .disable = tegra_xudc_ep0_disable,
  1599. .alloc_request = tegra_xudc_ep_alloc_request,
  1600. .free_request = tegra_xudc_ep_free_request,
  1601. .queue = tegra_xudc_ep_queue,
  1602. .dequeue = tegra_xudc_ep_dequeue,
  1603. .set_halt = tegra_xudc_ep_set_halt,
  1604. };
  1605. static int tegra_xudc_gadget_get_frame(struct usb_gadget *gadget)
  1606. {
  1607. struct tegra_xudc *xudc = to_xudc(gadget);
  1608. unsigned long flags;
  1609. int ret;
  1610. spin_lock_irqsave(&xudc->lock, flags);
  1611. if (xudc->powergated) {
  1612. ret = -ESHUTDOWN;
  1613. goto unlock;
  1614. }
  1615. ret = (xudc_readl(xudc, MFINDEX) & MFINDEX_FRAME_MASK) >>
  1616. MFINDEX_FRAME_SHIFT;
  1617. unlock:
  1618. spin_unlock_irqrestore(&xudc->lock, flags);
  1619. return ret;
  1620. }
  1621. static void tegra_xudc_resume_device_state(struct tegra_xudc *xudc)
  1622. {
  1623. unsigned int i;
  1624. u32 val;
  1625. ep_unpause_all(xudc);
  1626. /* Direct link to U0. */
  1627. val = xudc_readl(xudc, PORTSC);
  1628. if (((val & PORTSC_PLS_MASK) >> PORTSC_PLS_SHIFT) != PORTSC_PLS_U0) {
  1629. val &= ~(PORTSC_CHANGE_MASK | PORTSC_PLS_MASK);
  1630. val |= PORTSC_LWS | PORTSC_PLS(PORTSC_PLS_U0);
  1631. xudc_writel(xudc, val, PORTSC);
  1632. }
  1633. if (xudc->device_state == USB_STATE_SUSPENDED) {
  1634. xudc->device_state = xudc->resume_state;
  1635. usb_gadget_set_state(&xudc->gadget, xudc->device_state);
  1636. xudc->resume_state = 0;
  1637. }
  1638. /*
  1639. * Doorbells may be dropped if they are sent too soon (< ~200ns)
  1640. * after unpausing the endpoint. Wait for 500ns just to be safe.
  1641. */
  1642. ndelay(500);
  1643. for (i = 0; i < ARRAY_SIZE(xudc->ep); i++)
  1644. tegra_xudc_ep_ring_doorbell(&xudc->ep[i]);
  1645. }
  1646. static int tegra_xudc_gadget_wakeup(struct usb_gadget *gadget)
  1647. {
  1648. struct tegra_xudc *xudc = to_xudc(gadget);
  1649. unsigned long flags;
  1650. int ret = 0;
  1651. u32 val;
  1652. spin_lock_irqsave(&xudc->lock, flags);
  1653. if (xudc->powergated) {
  1654. ret = -ESHUTDOWN;
  1655. goto unlock;
  1656. }
  1657. val = xudc_readl(xudc, PORTPM);
  1658. dev_dbg(xudc->dev, "%s: PORTPM=%#x, speed=%x\n", __func__,
  1659. val, gadget->speed);
  1660. if (((xudc->gadget.speed <= USB_SPEED_HIGH) &&
  1661. (val & PORTPM_RWE)) ||
  1662. ((xudc->gadget.speed == USB_SPEED_SUPER) &&
  1663. (val & PORTPM_FRWE))) {
  1664. tegra_xudc_resume_device_state(xudc);
  1665. /* Send Device Notification packet. */
  1666. if (xudc->gadget.speed == USB_SPEED_SUPER) {
  1667. val = DEVNOTIF_LO_TYPE(DEVNOTIF_LO_TYPE_FUNCTION_WAKE)
  1668. | DEVNOTIF_LO_TRIG;
  1669. xudc_writel(xudc, 0, DEVNOTIF_HI);
  1670. xudc_writel(xudc, val, DEVNOTIF_LO);
  1671. }
  1672. }
  1673. unlock:
  1674. dev_dbg(xudc->dev, "%s: ret value is %d", __func__, ret);
  1675. spin_unlock_irqrestore(&xudc->lock, flags);
  1676. return ret;
  1677. }
  1678. static int tegra_xudc_gadget_pullup(struct usb_gadget *gadget, int is_on)
  1679. {
  1680. struct tegra_xudc *xudc = to_xudc(gadget);
  1681. unsigned long flags;
  1682. u32 val;
  1683. pm_runtime_get_sync(xudc->dev);
  1684. spin_lock_irqsave(&xudc->lock, flags);
  1685. if (is_on != xudc->pullup) {
  1686. val = xudc_readl(xudc, CTRL);
  1687. if (is_on)
  1688. val |= CTRL_ENABLE;
  1689. else
  1690. val &= ~CTRL_ENABLE;
  1691. xudc_writel(xudc, val, CTRL);
  1692. }
  1693. xudc->pullup = is_on;
  1694. dev_dbg(xudc->dev, "%s: pullup:%d", __func__, is_on);
  1695. spin_unlock_irqrestore(&xudc->lock, flags);
  1696. pm_runtime_put(xudc->dev);
  1697. return 0;
  1698. }
  1699. static int tegra_xudc_gadget_start(struct usb_gadget *gadget,
  1700. struct usb_gadget_driver *driver)
  1701. {
  1702. struct tegra_xudc *xudc = to_xudc(gadget);
  1703. unsigned long flags;
  1704. u32 val;
  1705. int ret;
  1706. unsigned int i;
  1707. if (!driver)
  1708. return -EINVAL;
  1709. pm_runtime_get_sync(xudc->dev);
  1710. spin_lock_irqsave(&xudc->lock, flags);
  1711. if (xudc->driver) {
  1712. ret = -EBUSY;
  1713. goto unlock;
  1714. }
  1715. xudc->setup_state = WAIT_FOR_SETUP;
  1716. xudc->device_state = USB_STATE_DEFAULT;
  1717. usb_gadget_set_state(&xudc->gadget, xudc->device_state);
  1718. ret = __tegra_xudc_ep_enable(&xudc->ep[0], &tegra_xudc_ep0_desc);
  1719. if (ret < 0)
  1720. goto unlock;
  1721. val = xudc_readl(xudc, CTRL);
  1722. val |= CTRL_IE | CTRL_LSE;
  1723. xudc_writel(xudc, val, CTRL);
  1724. val = xudc_readl(xudc, PORTHALT);
  1725. val |= PORTHALT_STCHG_INTR_EN;
  1726. xudc_writel(xudc, val, PORTHALT);
  1727. if (xudc->pullup) {
  1728. val = xudc_readl(xudc, CTRL);
  1729. val |= CTRL_ENABLE;
  1730. xudc_writel(xudc, val, CTRL);
  1731. }
  1732. for (i = 0; i < xudc->soc->num_phys; i++)
  1733. if (xudc->usbphy[i])
  1734. otg_set_peripheral(xudc->usbphy[i]->otg, gadget);
  1735. xudc->driver = driver;
  1736. unlock:
  1737. dev_dbg(xudc->dev, "%s: ret value is %d", __func__, ret);
  1738. spin_unlock_irqrestore(&xudc->lock, flags);
  1739. pm_runtime_put(xudc->dev);
  1740. return ret;
  1741. }
  1742. static int tegra_xudc_gadget_stop(struct usb_gadget *gadget)
  1743. {
  1744. struct tegra_xudc *xudc = to_xudc(gadget);
  1745. unsigned long flags;
  1746. u32 val;
  1747. unsigned int i;
  1748. pm_runtime_get_sync(xudc->dev);
  1749. spin_lock_irqsave(&xudc->lock, flags);
  1750. for (i = 0; i < xudc->soc->num_phys; i++)
  1751. if (xudc->usbphy[i])
  1752. otg_set_peripheral(xudc->usbphy[i]->otg, NULL);
  1753. val = xudc_readl(xudc, CTRL);
  1754. val &= ~(CTRL_IE | CTRL_ENABLE);
  1755. xudc_writel(xudc, val, CTRL);
  1756. __tegra_xudc_ep_disable(&xudc->ep[0]);
  1757. xudc->driver = NULL;
  1758. dev_dbg(xudc->dev, "Gadget stopped");
  1759. spin_unlock_irqrestore(&xudc->lock, flags);
  1760. pm_runtime_put(xudc->dev);
  1761. return 0;
  1762. }
  1763. static int tegra_xudc_gadget_vbus_draw(struct usb_gadget *gadget,
  1764. unsigned int m_a)
  1765. {
  1766. struct tegra_xudc *xudc = to_xudc(gadget);
  1767. dev_dbg(xudc->dev, "%s: %u mA\n", __func__, m_a);
  1768. if (xudc->curr_usbphy && xudc->curr_usbphy->chg_type == SDP_TYPE)
  1769. return usb_phy_set_power(xudc->curr_usbphy, m_a);
  1770. return 0;
  1771. }
  1772. static int tegra_xudc_set_selfpowered(struct usb_gadget *gadget, int is_on)
  1773. {
  1774. struct tegra_xudc *xudc = to_xudc(gadget);
  1775. dev_dbg(xudc->dev, "%s: %d\n", __func__, is_on);
  1776. xudc->selfpowered = !!is_on;
  1777. return 0;
  1778. }
  1779. static const struct usb_gadget_ops tegra_xudc_gadget_ops = {
  1780. .get_frame = tegra_xudc_gadget_get_frame,
  1781. .wakeup = tegra_xudc_gadget_wakeup,
  1782. .pullup = tegra_xudc_gadget_pullup,
  1783. .udc_start = tegra_xudc_gadget_start,
  1784. .udc_stop = tegra_xudc_gadget_stop,
  1785. .vbus_draw = tegra_xudc_gadget_vbus_draw,
  1786. .set_selfpowered = tegra_xudc_set_selfpowered,
  1787. };
  1788. static void no_op_complete(struct usb_ep *ep, struct usb_request *req)
  1789. {
  1790. }
  1791. static int
  1792. tegra_xudc_ep0_queue_status(struct tegra_xudc *xudc,
  1793. void (*cmpl)(struct usb_ep *, struct usb_request *))
  1794. {
  1795. xudc->ep0_req->usb_req.buf = NULL;
  1796. xudc->ep0_req->usb_req.dma = 0;
  1797. xudc->ep0_req->usb_req.length = 0;
  1798. xudc->ep0_req->usb_req.complete = cmpl;
  1799. xudc->ep0_req->usb_req.context = xudc;
  1800. return __tegra_xudc_ep_queue(&xudc->ep[0], xudc->ep0_req);
  1801. }
  1802. static int
  1803. tegra_xudc_ep0_queue_data(struct tegra_xudc *xudc, void *buf, size_t len,
  1804. void (*cmpl)(struct usb_ep *, struct usb_request *))
  1805. {
  1806. xudc->ep0_req->usb_req.buf = buf;
  1807. xudc->ep0_req->usb_req.length = len;
  1808. xudc->ep0_req->usb_req.complete = cmpl;
  1809. xudc->ep0_req->usb_req.context = xudc;
  1810. return __tegra_xudc_ep_queue(&xudc->ep[0], xudc->ep0_req);
  1811. }
  1812. static void tegra_xudc_ep0_req_done(struct tegra_xudc *xudc)
  1813. {
  1814. switch (xudc->setup_state) {
  1815. case DATA_STAGE_XFER:
  1816. xudc->setup_state = STATUS_STAGE_RECV;
  1817. tegra_xudc_ep0_queue_status(xudc, no_op_complete);
  1818. break;
  1819. case DATA_STAGE_RECV:
  1820. xudc->setup_state = STATUS_STAGE_XFER;
  1821. tegra_xudc_ep0_queue_status(xudc, no_op_complete);
  1822. break;
  1823. default:
  1824. xudc->setup_state = WAIT_FOR_SETUP;
  1825. break;
  1826. }
  1827. }
  1828. static int tegra_xudc_ep0_delegate_req(struct tegra_xudc *xudc,
  1829. struct usb_ctrlrequest *ctrl)
  1830. {
  1831. int ret;
  1832. spin_unlock(&xudc->lock);
  1833. ret = xudc->driver->setup(&xudc->gadget, ctrl);
  1834. spin_lock(&xudc->lock);
  1835. return ret;
  1836. }
  1837. static void set_feature_complete(struct usb_ep *ep, struct usb_request *req)
  1838. {
  1839. struct tegra_xudc *xudc = req->context;
  1840. if (xudc->test_mode_pattern) {
  1841. xudc_writel(xudc, xudc->test_mode_pattern, PORT_TM);
  1842. xudc->test_mode_pattern = 0;
  1843. }
  1844. }
  1845. static int tegra_xudc_ep0_set_feature(struct tegra_xudc *xudc,
  1846. struct usb_ctrlrequest *ctrl)
  1847. {
  1848. bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
  1849. u32 feature = le16_to_cpu(ctrl->wValue);
  1850. u32 index = le16_to_cpu(ctrl->wIndex);
  1851. u32 val, ep;
  1852. int ret;
  1853. if (le16_to_cpu(ctrl->wLength) != 0)
  1854. return -EINVAL;
  1855. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1856. case USB_RECIP_DEVICE:
  1857. switch (feature) {
  1858. case USB_DEVICE_REMOTE_WAKEUP:
  1859. if ((xudc->gadget.speed == USB_SPEED_SUPER) ||
  1860. (xudc->device_state == USB_STATE_DEFAULT))
  1861. return -EINVAL;
  1862. val = xudc_readl(xudc, PORTPM);
  1863. if (set)
  1864. val |= PORTPM_RWE;
  1865. else
  1866. val &= ~PORTPM_RWE;
  1867. xudc_writel(xudc, val, PORTPM);
  1868. break;
  1869. case USB_DEVICE_U1_ENABLE:
  1870. case USB_DEVICE_U2_ENABLE:
  1871. if ((xudc->device_state != USB_STATE_CONFIGURED) ||
  1872. (xudc->gadget.speed != USB_SPEED_SUPER))
  1873. return -EINVAL;
  1874. val = xudc_readl(xudc, PORTPM);
  1875. if ((feature == USB_DEVICE_U1_ENABLE) &&
  1876. xudc->soc->u1_enable) {
  1877. if (set)
  1878. val |= PORTPM_U1E;
  1879. else
  1880. val &= ~PORTPM_U1E;
  1881. }
  1882. if ((feature == USB_DEVICE_U2_ENABLE) &&
  1883. xudc->soc->u2_enable) {
  1884. if (set)
  1885. val |= PORTPM_U2E;
  1886. else
  1887. val &= ~PORTPM_U2E;
  1888. }
  1889. xudc_writel(xudc, val, PORTPM);
  1890. break;
  1891. case USB_DEVICE_TEST_MODE:
  1892. if (xudc->gadget.speed != USB_SPEED_HIGH)
  1893. return -EINVAL;
  1894. if (!set)
  1895. return -EINVAL;
  1896. xudc->test_mode_pattern = index >> 8;
  1897. break;
  1898. default:
  1899. return -EINVAL;
  1900. }
  1901. break;
  1902. case USB_RECIP_INTERFACE:
  1903. if (xudc->device_state != USB_STATE_CONFIGURED)
  1904. return -EINVAL;
  1905. switch (feature) {
  1906. case USB_INTRF_FUNC_SUSPEND:
  1907. if (set) {
  1908. val = xudc_readl(xudc, PORTPM);
  1909. if (index & USB_INTRF_FUNC_SUSPEND_RW)
  1910. val |= PORTPM_FRWE;
  1911. else
  1912. val &= ~PORTPM_FRWE;
  1913. xudc_writel(xudc, val, PORTPM);
  1914. }
  1915. return tegra_xudc_ep0_delegate_req(xudc, ctrl);
  1916. default:
  1917. return -EINVAL;
  1918. }
  1919. break;
  1920. case USB_RECIP_ENDPOINT:
  1921. ep = (index & USB_ENDPOINT_NUMBER_MASK) * 2 +
  1922. ((index & USB_DIR_IN) ? 1 : 0);
  1923. if ((xudc->device_state == USB_STATE_DEFAULT) ||
  1924. ((xudc->device_state == USB_STATE_ADDRESS) &&
  1925. (index != 0)))
  1926. return -EINVAL;
  1927. ret = __tegra_xudc_ep_set_halt(&xudc->ep[ep], set);
  1928. if (ret < 0)
  1929. return ret;
  1930. break;
  1931. default:
  1932. return -EINVAL;
  1933. }
  1934. return tegra_xudc_ep0_queue_status(xudc, set_feature_complete);
  1935. }
  1936. static int tegra_xudc_ep0_get_status(struct tegra_xudc *xudc,
  1937. struct usb_ctrlrequest *ctrl)
  1938. {
  1939. struct tegra_xudc_ep_context *ep_ctx;
  1940. u32 val, ep, index = le16_to_cpu(ctrl->wIndex);
  1941. u16 status = 0;
  1942. if (!(ctrl->bRequestType & USB_DIR_IN))
  1943. return -EINVAL;
  1944. if ((le16_to_cpu(ctrl->wValue) != 0) ||
  1945. (le16_to_cpu(ctrl->wLength) != 2))
  1946. return -EINVAL;
  1947. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1948. case USB_RECIP_DEVICE:
  1949. val = xudc_readl(xudc, PORTPM);
  1950. if (xudc->selfpowered)
  1951. status |= BIT(USB_DEVICE_SELF_POWERED);
  1952. if ((xudc->gadget.speed < USB_SPEED_SUPER) &&
  1953. (val & PORTPM_RWE))
  1954. status |= BIT(USB_DEVICE_REMOTE_WAKEUP);
  1955. if (xudc->gadget.speed == USB_SPEED_SUPER) {
  1956. if (val & PORTPM_U1E)
  1957. status |= BIT(USB_DEV_STAT_U1_ENABLED);
  1958. if (val & PORTPM_U2E)
  1959. status |= BIT(USB_DEV_STAT_U2_ENABLED);
  1960. }
  1961. break;
  1962. case USB_RECIP_INTERFACE:
  1963. if (xudc->gadget.speed == USB_SPEED_SUPER) {
  1964. status |= USB_INTRF_STAT_FUNC_RW_CAP;
  1965. val = xudc_readl(xudc, PORTPM);
  1966. if (val & PORTPM_FRWE)
  1967. status |= USB_INTRF_STAT_FUNC_RW;
  1968. }
  1969. break;
  1970. case USB_RECIP_ENDPOINT:
  1971. ep = (index & USB_ENDPOINT_NUMBER_MASK) * 2 +
  1972. ((index & USB_DIR_IN) ? 1 : 0);
  1973. ep_ctx = &xudc->ep_context[ep];
  1974. if ((xudc->device_state != USB_STATE_CONFIGURED) &&
  1975. ((xudc->device_state != USB_STATE_ADDRESS) || (ep != 0)))
  1976. return -EINVAL;
  1977. if (ep_ctx_read_state(ep_ctx) == EP_STATE_DISABLED)
  1978. return -EINVAL;
  1979. if (xudc_readl(xudc, EP_HALT) & BIT(ep))
  1980. status |= BIT(USB_ENDPOINT_HALT);
  1981. break;
  1982. default:
  1983. return -EINVAL;
  1984. }
  1985. xudc->status_buf = cpu_to_le16(status);
  1986. return tegra_xudc_ep0_queue_data(xudc, &xudc->status_buf,
  1987. sizeof(xudc->status_buf),
  1988. no_op_complete);
  1989. }
  1990. static void set_sel_complete(struct usb_ep *ep, struct usb_request *req)
  1991. {
  1992. /* Nothing to do with SEL values */
  1993. }
  1994. static int tegra_xudc_ep0_set_sel(struct tegra_xudc *xudc,
  1995. struct usb_ctrlrequest *ctrl)
  1996. {
  1997. if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE |
  1998. USB_TYPE_STANDARD))
  1999. return -EINVAL;
  2000. if (xudc->device_state == USB_STATE_DEFAULT)
  2001. return -EINVAL;
  2002. if ((le16_to_cpu(ctrl->wIndex) != 0) ||
  2003. (le16_to_cpu(ctrl->wValue) != 0) ||
  2004. (le16_to_cpu(ctrl->wLength) != 6))
  2005. return -EINVAL;
  2006. return tegra_xudc_ep0_queue_data(xudc, &xudc->sel_timing,
  2007. sizeof(xudc->sel_timing),
  2008. set_sel_complete);
  2009. }
  2010. static void set_isoch_delay_complete(struct usb_ep *ep, struct usb_request *req)
  2011. {
  2012. /* Nothing to do with isoch delay */
  2013. }
  2014. static int tegra_xudc_ep0_set_isoch_delay(struct tegra_xudc *xudc,
  2015. struct usb_ctrlrequest *ctrl)
  2016. {
  2017. u32 delay = le16_to_cpu(ctrl->wValue);
  2018. if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE |
  2019. USB_TYPE_STANDARD))
  2020. return -EINVAL;
  2021. if ((delay > 65535) || (le16_to_cpu(ctrl->wIndex) != 0) ||
  2022. (le16_to_cpu(ctrl->wLength) != 0))
  2023. return -EINVAL;
  2024. xudc->isoch_delay = delay;
  2025. return tegra_xudc_ep0_queue_status(xudc, set_isoch_delay_complete);
  2026. }
  2027. static void set_address_complete(struct usb_ep *ep, struct usb_request *req)
  2028. {
  2029. struct tegra_xudc *xudc = req->context;
  2030. if ((xudc->device_state == USB_STATE_DEFAULT) &&
  2031. (xudc->dev_addr != 0)) {
  2032. xudc->device_state = USB_STATE_ADDRESS;
  2033. usb_gadget_set_state(&xudc->gadget, xudc->device_state);
  2034. } else if ((xudc->device_state == USB_STATE_ADDRESS) &&
  2035. (xudc->dev_addr == 0)) {
  2036. xudc->device_state = USB_STATE_DEFAULT;
  2037. usb_gadget_set_state(&xudc->gadget, xudc->device_state);
  2038. }
  2039. }
  2040. static int tegra_xudc_ep0_set_address(struct tegra_xudc *xudc,
  2041. struct usb_ctrlrequest *ctrl)
  2042. {
  2043. struct tegra_xudc_ep *ep0 = &xudc->ep[0];
  2044. u32 val, addr = le16_to_cpu(ctrl->wValue);
  2045. if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE |
  2046. USB_TYPE_STANDARD))
  2047. return -EINVAL;
  2048. if ((addr > 127) || (le16_to_cpu(ctrl->wIndex) != 0) ||
  2049. (le16_to_cpu(ctrl->wLength) != 0))
  2050. return -EINVAL;
  2051. if (xudc->device_state == USB_STATE_CONFIGURED)
  2052. return -EINVAL;
  2053. dev_dbg(xudc->dev, "set address: %u\n", addr);
  2054. xudc->dev_addr = addr;
  2055. val = xudc_readl(xudc, CTRL);
  2056. val &= ~(CTRL_DEVADDR_MASK);
  2057. val |= CTRL_DEVADDR(addr);
  2058. xudc_writel(xudc, val, CTRL);
  2059. ep_ctx_write_devaddr(ep0->context, addr);
  2060. return tegra_xudc_ep0_queue_status(xudc, set_address_complete);
  2061. }
  2062. static int tegra_xudc_ep0_standard_req(struct tegra_xudc *xudc,
  2063. struct usb_ctrlrequest *ctrl)
  2064. {
  2065. int ret;
  2066. switch (ctrl->bRequest) {
  2067. case USB_REQ_GET_STATUS:
  2068. dev_dbg(xudc->dev, "USB_REQ_GET_STATUS\n");
  2069. ret = tegra_xudc_ep0_get_status(xudc, ctrl);
  2070. break;
  2071. case USB_REQ_SET_ADDRESS:
  2072. dev_dbg(xudc->dev, "USB_REQ_SET_ADDRESS\n");
  2073. ret = tegra_xudc_ep0_set_address(xudc, ctrl);
  2074. break;
  2075. case USB_REQ_SET_SEL:
  2076. dev_dbg(xudc->dev, "USB_REQ_SET_SEL\n");
  2077. ret = tegra_xudc_ep0_set_sel(xudc, ctrl);
  2078. break;
  2079. case USB_REQ_SET_ISOCH_DELAY:
  2080. dev_dbg(xudc->dev, "USB_REQ_SET_ISOCH_DELAY\n");
  2081. ret = tegra_xudc_ep0_set_isoch_delay(xudc, ctrl);
  2082. break;
  2083. case USB_REQ_CLEAR_FEATURE:
  2084. case USB_REQ_SET_FEATURE:
  2085. dev_dbg(xudc->dev, "USB_REQ_CLEAR/SET_FEATURE\n");
  2086. ret = tegra_xudc_ep0_set_feature(xudc, ctrl);
  2087. break;
  2088. case USB_REQ_SET_CONFIGURATION:
  2089. dev_dbg(xudc->dev, "USB_REQ_SET_CONFIGURATION\n");
  2090. /*
  2091. * In theory we need to clear RUN bit before status stage of
  2092. * deconfig request sent, but this seems to be causing problems.
  2093. * Clear RUN once all endpoints are disabled instead.
  2094. */
  2095. fallthrough;
  2096. default:
  2097. ret = tegra_xudc_ep0_delegate_req(xudc, ctrl);
  2098. break;
  2099. }
  2100. return ret;
  2101. }
  2102. static void tegra_xudc_handle_ep0_setup_packet(struct tegra_xudc *xudc,
  2103. struct usb_ctrlrequest *ctrl,
  2104. u16 seq_num)
  2105. {
  2106. int ret;
  2107. xudc->setup_seq_num = seq_num;
  2108. /* Ensure EP0 is unhalted. */
  2109. ep_unhalt(xudc, 0);
  2110. /*
  2111. * On Tegra210, setup packets with sequence numbers 0xfffe or 0xffff
  2112. * are invalid. Halt EP0 until we get a valid packet.
  2113. */
  2114. if (xudc->soc->invalid_seq_num &&
  2115. (seq_num == 0xfffe || seq_num == 0xffff)) {
  2116. dev_warn(xudc->dev, "invalid sequence number detected\n");
  2117. ep_halt(xudc, 0);
  2118. return;
  2119. }
  2120. if (ctrl->wLength)
  2121. xudc->setup_state = (ctrl->bRequestType & USB_DIR_IN) ?
  2122. DATA_STAGE_XFER : DATA_STAGE_RECV;
  2123. else
  2124. xudc->setup_state = STATUS_STAGE_XFER;
  2125. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
  2126. ret = tegra_xudc_ep0_standard_req(xudc, ctrl);
  2127. else
  2128. ret = tegra_xudc_ep0_delegate_req(xudc, ctrl);
  2129. if (ret < 0) {
  2130. dev_warn(xudc->dev, "setup request failed: %d\n", ret);
  2131. xudc->setup_state = WAIT_FOR_SETUP;
  2132. ep_halt(xudc, 0);
  2133. }
  2134. }
  2135. static void tegra_xudc_handle_ep0_event(struct tegra_xudc *xudc,
  2136. struct tegra_xudc_trb *event)
  2137. {
  2138. struct usb_ctrlrequest *ctrl = (struct usb_ctrlrequest *)event;
  2139. u16 seq_num = trb_read_seq_num(event);
  2140. if (xudc->setup_state != WAIT_FOR_SETUP) {
  2141. /*
  2142. * The controller is in the process of handling another
  2143. * setup request. Queue subsequent requests and handle
  2144. * the last one once the controller reports a sequence
  2145. * number error.
  2146. */
  2147. memcpy(&xudc->setup_packet.ctrl_req, ctrl, sizeof(*ctrl));
  2148. xudc->setup_packet.seq_num = seq_num;
  2149. xudc->queued_setup_packet = true;
  2150. } else {
  2151. tegra_xudc_handle_ep0_setup_packet(xudc, ctrl, seq_num);
  2152. }
  2153. }
  2154. static struct tegra_xudc_request *
  2155. trb_to_request(struct tegra_xudc_ep *ep, struct tegra_xudc_trb *trb)
  2156. {
  2157. struct tegra_xudc_request *req;
  2158. list_for_each_entry(req, &ep->queue, list) {
  2159. if (!req->trbs_queued)
  2160. break;
  2161. if (trb_in_request(ep, req, trb))
  2162. return req;
  2163. }
  2164. return NULL;
  2165. }
  2166. static void tegra_xudc_handle_transfer_completion(struct tegra_xudc *xudc,
  2167. struct tegra_xudc_ep *ep,
  2168. struct tegra_xudc_trb *event)
  2169. {
  2170. struct tegra_xudc_request *req;
  2171. struct tegra_xudc_trb *trb;
  2172. bool short_packet;
  2173. short_packet = (trb_read_cmpl_code(event) ==
  2174. TRB_CMPL_CODE_SHORT_PACKET);
  2175. trb = trb_phys_to_virt(ep, trb_read_data_ptr(event));
  2176. req = trb_to_request(ep, trb);
  2177. /*
  2178. * TDs are complete on short packet or when the completed TRB is the
  2179. * last TRB in the TD (the CHAIN bit is unset).
  2180. */
  2181. if (req && (short_packet || (!trb_read_chain(trb) &&
  2182. (req->trbs_needed == req->trbs_queued)))) {
  2183. struct tegra_xudc_trb *last = req->last_trb;
  2184. unsigned int residual;
  2185. residual = trb_read_transfer_len(event);
  2186. req->usb_req.actual = req->usb_req.length - residual;
  2187. dev_dbg(xudc->dev, "bytes transferred %u / %u\n",
  2188. req->usb_req.actual, req->usb_req.length);
  2189. tegra_xudc_req_done(ep, req, 0);
  2190. if (ep->desc && usb_endpoint_xfer_control(ep->desc))
  2191. tegra_xudc_ep0_req_done(xudc);
  2192. /*
  2193. * Advance the dequeue pointer past the end of the current TD
  2194. * on short packet completion.
  2195. */
  2196. if (short_packet) {
  2197. ep->deq_ptr = (last - ep->transfer_ring) + 1;
  2198. if (ep->deq_ptr == XUDC_TRANSFER_RING_SIZE - 1)
  2199. ep->deq_ptr = 0;
  2200. }
  2201. } else if (!req) {
  2202. dev_warn(xudc->dev, "transfer event on dequeued request\n");
  2203. }
  2204. if (ep->desc)
  2205. tegra_xudc_ep_kick_queue(ep);
  2206. }
  2207. static void tegra_xudc_handle_transfer_event(struct tegra_xudc *xudc,
  2208. struct tegra_xudc_trb *event)
  2209. {
  2210. unsigned int ep_index = trb_read_endpoint_id(event);
  2211. struct tegra_xudc_ep *ep = &xudc->ep[ep_index];
  2212. struct tegra_xudc_trb *trb;
  2213. u16 comp_code;
  2214. if (ep_ctx_read_state(ep->context) == EP_STATE_DISABLED) {
  2215. dev_warn(xudc->dev, "transfer event on disabled EP %u\n",
  2216. ep_index);
  2217. return;
  2218. }
  2219. /* Update transfer ring dequeue pointer. */
  2220. trb = trb_phys_to_virt(ep, trb_read_data_ptr(event));
  2221. comp_code = trb_read_cmpl_code(event);
  2222. if (comp_code != TRB_CMPL_CODE_BABBLE_DETECTED_ERR) {
  2223. ep->deq_ptr = (trb - ep->transfer_ring) + 1;
  2224. if (ep->deq_ptr == XUDC_TRANSFER_RING_SIZE - 1)
  2225. ep->deq_ptr = 0;
  2226. ep->ring_full = false;
  2227. }
  2228. switch (comp_code) {
  2229. case TRB_CMPL_CODE_SUCCESS:
  2230. case TRB_CMPL_CODE_SHORT_PACKET:
  2231. tegra_xudc_handle_transfer_completion(xudc, ep, event);
  2232. break;
  2233. case TRB_CMPL_CODE_HOST_REJECTED:
  2234. dev_info(xudc->dev, "stream rejected on EP %u\n", ep_index);
  2235. ep->stream_rejected = true;
  2236. break;
  2237. case TRB_CMPL_CODE_PRIME_PIPE_RECEIVED:
  2238. dev_info(xudc->dev, "prime pipe received on EP %u\n", ep_index);
  2239. if (ep->stream_rejected) {
  2240. ep->stream_rejected = false;
  2241. /*
  2242. * An EP is stopped when a stream is rejected. Wait
  2243. * for the EP to report that it is stopped and then
  2244. * un-stop it.
  2245. */
  2246. ep_wait_for_stopped(xudc, ep_index);
  2247. }
  2248. tegra_xudc_ep_ring_doorbell(ep);
  2249. break;
  2250. case TRB_CMPL_CODE_BABBLE_DETECTED_ERR:
  2251. /*
  2252. * Wait for the EP to be stopped so the controller stops
  2253. * processing doorbells.
  2254. */
  2255. ep_wait_for_stopped(xudc, ep_index);
  2256. ep->enq_ptr = ep->deq_ptr;
  2257. tegra_xudc_ep_nuke(ep, -EIO);
  2258. fallthrough;
  2259. case TRB_CMPL_CODE_STREAM_NUMP_ERROR:
  2260. case TRB_CMPL_CODE_CTRL_DIR_ERR:
  2261. case TRB_CMPL_CODE_INVALID_STREAM_TYPE_ERR:
  2262. case TRB_CMPL_CODE_RING_UNDERRUN:
  2263. case TRB_CMPL_CODE_RING_OVERRUN:
  2264. case TRB_CMPL_CODE_ISOCH_BUFFER_OVERRUN:
  2265. case TRB_CMPL_CODE_USB_TRANS_ERR:
  2266. case TRB_CMPL_CODE_TRB_ERR:
  2267. dev_err(xudc->dev, "completion error %#x on EP %u\n",
  2268. comp_code, ep_index);
  2269. ep_halt(xudc, ep_index);
  2270. break;
  2271. case TRB_CMPL_CODE_CTRL_SEQNUM_ERR:
  2272. dev_info(xudc->dev, "sequence number error\n");
  2273. /*
  2274. * Kill any queued control request and skip to the last
  2275. * setup packet we received.
  2276. */
  2277. tegra_xudc_ep_nuke(ep, -EINVAL);
  2278. xudc->setup_state = WAIT_FOR_SETUP;
  2279. if (!xudc->queued_setup_packet)
  2280. break;
  2281. tegra_xudc_handle_ep0_setup_packet(xudc,
  2282. &xudc->setup_packet.ctrl_req,
  2283. xudc->setup_packet.seq_num);
  2284. xudc->queued_setup_packet = false;
  2285. break;
  2286. case TRB_CMPL_CODE_STOPPED:
  2287. dev_dbg(xudc->dev, "stop completion code on EP %u\n",
  2288. ep_index);
  2289. /* Disconnected. */
  2290. tegra_xudc_ep_nuke(ep, -ECONNREFUSED);
  2291. break;
  2292. default:
  2293. dev_dbg(xudc->dev, "completion event %#x on EP %u\n",
  2294. comp_code, ep_index);
  2295. break;
  2296. }
  2297. }
  2298. static void tegra_xudc_reset(struct tegra_xudc *xudc)
  2299. {
  2300. struct tegra_xudc_ep *ep0 = &xudc->ep[0];
  2301. dma_addr_t deq_ptr;
  2302. unsigned int i;
  2303. xudc->setup_state = WAIT_FOR_SETUP;
  2304. xudc->device_state = USB_STATE_DEFAULT;
  2305. usb_gadget_set_state(&xudc->gadget, xudc->device_state);
  2306. ep_unpause_all(xudc);
  2307. for (i = 0; i < ARRAY_SIZE(xudc->ep); i++)
  2308. tegra_xudc_ep_nuke(&xudc->ep[i], -ESHUTDOWN);
  2309. /*
  2310. * Reset sequence number and dequeue pointer to flush the transfer
  2311. * ring.
  2312. */
  2313. ep0->deq_ptr = ep0->enq_ptr;
  2314. ep0->ring_full = false;
  2315. xudc->setup_seq_num = 0;
  2316. xudc->queued_setup_packet = false;
  2317. ep_ctx_write_rsvd(ep0->context, 0);
  2318. ep_ctx_write_partial_td(ep0->context, 0);
  2319. ep_ctx_write_splitxstate(ep0->context, 0);
  2320. ep_ctx_write_seq_num(ep0->context, 0);
  2321. deq_ptr = trb_virt_to_phys(ep0, &ep0->transfer_ring[ep0->deq_ptr]);
  2322. if (!dma_mapping_error(xudc->dev, deq_ptr)) {
  2323. ep_ctx_write_deq_ptr(ep0->context, deq_ptr);
  2324. ep_ctx_write_dcs(ep0->context, ep0->pcs);
  2325. }
  2326. ep_unhalt_all(xudc);
  2327. ep_reload(xudc, 0);
  2328. ep_unpause(xudc, 0);
  2329. }
  2330. static void tegra_xudc_port_connect(struct tegra_xudc *xudc)
  2331. {
  2332. struct tegra_xudc_ep *ep0 = &xudc->ep[0];
  2333. u16 maxpacket;
  2334. u32 val;
  2335. val = (xudc_readl(xudc, PORTSC) & PORTSC_PS_MASK) >> PORTSC_PS_SHIFT;
  2336. switch (val) {
  2337. case PORTSC_PS_LS:
  2338. xudc->gadget.speed = USB_SPEED_LOW;
  2339. break;
  2340. case PORTSC_PS_FS:
  2341. xudc->gadget.speed = USB_SPEED_FULL;
  2342. break;
  2343. case PORTSC_PS_HS:
  2344. xudc->gadget.speed = USB_SPEED_HIGH;
  2345. break;
  2346. case PORTSC_PS_SS:
  2347. xudc->gadget.speed = USB_SPEED_SUPER;
  2348. break;
  2349. default:
  2350. xudc->gadget.speed = USB_SPEED_UNKNOWN;
  2351. break;
  2352. }
  2353. xudc->device_state = USB_STATE_DEFAULT;
  2354. usb_gadget_set_state(&xudc->gadget, xudc->device_state);
  2355. xudc->setup_state = WAIT_FOR_SETUP;
  2356. if (xudc->gadget.speed == USB_SPEED_SUPER)
  2357. maxpacket = 512;
  2358. else
  2359. maxpacket = 64;
  2360. ep_ctx_write_max_packet_size(ep0->context, maxpacket);
  2361. tegra_xudc_ep0_desc.wMaxPacketSize = cpu_to_le16(maxpacket);
  2362. usb_ep_set_maxpacket_limit(&ep0->usb_ep, maxpacket);
  2363. if (!xudc->soc->u1_enable) {
  2364. val = xudc_readl(xudc, PORTPM);
  2365. val &= ~(PORTPM_U1TIMEOUT_MASK);
  2366. xudc_writel(xudc, val, PORTPM);
  2367. }
  2368. if (!xudc->soc->u2_enable) {
  2369. val = xudc_readl(xudc, PORTPM);
  2370. val &= ~(PORTPM_U2TIMEOUT_MASK);
  2371. xudc_writel(xudc, val, PORTPM);
  2372. }
  2373. if (xudc->gadget.speed <= USB_SPEED_HIGH) {
  2374. val = xudc_readl(xudc, PORTPM);
  2375. val &= ~(PORTPM_L1S_MASK);
  2376. if (xudc->soc->lpm_enable)
  2377. val |= PORTPM_L1S(PORTPM_L1S_ACCEPT);
  2378. else
  2379. val |= PORTPM_L1S(PORTPM_L1S_NYET);
  2380. xudc_writel(xudc, val, PORTPM);
  2381. }
  2382. val = xudc_readl(xudc, ST);
  2383. if (val & ST_RC)
  2384. xudc_writel(xudc, ST_RC, ST);
  2385. }
  2386. static void tegra_xudc_port_disconnect(struct tegra_xudc *xudc)
  2387. {
  2388. tegra_xudc_reset(xudc);
  2389. if (xudc->driver && xudc->driver->disconnect) {
  2390. spin_unlock(&xudc->lock);
  2391. xudc->driver->disconnect(&xudc->gadget);
  2392. spin_lock(&xudc->lock);
  2393. }
  2394. xudc->device_state = USB_STATE_NOTATTACHED;
  2395. usb_gadget_set_state(&xudc->gadget, xudc->device_state);
  2396. complete(&xudc->disconnect_complete);
  2397. }
  2398. static void tegra_xudc_port_reset(struct tegra_xudc *xudc)
  2399. {
  2400. tegra_xudc_reset(xudc);
  2401. if (xudc->driver) {
  2402. spin_unlock(&xudc->lock);
  2403. usb_gadget_udc_reset(&xudc->gadget, xudc->driver);
  2404. spin_lock(&xudc->lock);
  2405. }
  2406. tegra_xudc_port_connect(xudc);
  2407. }
  2408. static void tegra_xudc_port_suspend(struct tegra_xudc *xudc)
  2409. {
  2410. dev_dbg(xudc->dev, "port suspend\n");
  2411. xudc->resume_state = xudc->device_state;
  2412. xudc->device_state = USB_STATE_SUSPENDED;
  2413. usb_gadget_set_state(&xudc->gadget, xudc->device_state);
  2414. if (xudc->driver->suspend) {
  2415. spin_unlock(&xudc->lock);
  2416. xudc->driver->suspend(&xudc->gadget);
  2417. spin_lock(&xudc->lock);
  2418. }
  2419. }
  2420. static void tegra_xudc_port_resume(struct tegra_xudc *xudc)
  2421. {
  2422. dev_dbg(xudc->dev, "port resume\n");
  2423. tegra_xudc_resume_device_state(xudc);
  2424. if (xudc->driver->resume) {
  2425. spin_unlock(&xudc->lock);
  2426. xudc->driver->resume(&xudc->gadget);
  2427. spin_lock(&xudc->lock);
  2428. }
  2429. }
  2430. static inline void clear_port_change(struct tegra_xudc *xudc, u32 flag)
  2431. {
  2432. u32 val;
  2433. val = xudc_readl(xudc, PORTSC);
  2434. val &= ~PORTSC_CHANGE_MASK;
  2435. val |= flag;
  2436. xudc_writel(xudc, val, PORTSC);
  2437. }
  2438. static void __tegra_xudc_handle_port_status(struct tegra_xudc *xudc)
  2439. {
  2440. u32 portsc, porthalt;
  2441. porthalt = xudc_readl(xudc, PORTHALT);
  2442. if ((porthalt & PORTHALT_STCHG_REQ) &&
  2443. (porthalt & PORTHALT_HALT_LTSSM)) {
  2444. dev_dbg(xudc->dev, "STCHG_REQ, PORTHALT = %#x\n", porthalt);
  2445. porthalt &= ~PORTHALT_HALT_LTSSM;
  2446. xudc_writel(xudc, porthalt, PORTHALT);
  2447. }
  2448. portsc = xudc_readl(xudc, PORTSC);
  2449. if ((portsc & PORTSC_PRC) && (portsc & PORTSC_PR)) {
  2450. dev_dbg(xudc->dev, "PRC, PR, PORTSC = %#x\n", portsc);
  2451. clear_port_change(xudc, PORTSC_PRC | PORTSC_PED);
  2452. #define TOGGLE_VBUS_WAIT_MS 100
  2453. if (xudc->soc->port_reset_quirk) {
  2454. schedule_delayed_work(&xudc->port_reset_war_work,
  2455. msecs_to_jiffies(TOGGLE_VBUS_WAIT_MS));
  2456. xudc->wait_for_sec_prc = 1;
  2457. }
  2458. }
  2459. if ((portsc & PORTSC_PRC) && !(portsc & PORTSC_PR)) {
  2460. dev_dbg(xudc->dev, "PRC, Not PR, PORTSC = %#x\n", portsc);
  2461. clear_port_change(xudc, PORTSC_PRC | PORTSC_PED);
  2462. tegra_xudc_port_reset(xudc);
  2463. cancel_delayed_work(&xudc->port_reset_war_work);
  2464. xudc->wait_for_sec_prc = 0;
  2465. }
  2466. portsc = xudc_readl(xudc, PORTSC);
  2467. if (portsc & PORTSC_WRC) {
  2468. dev_dbg(xudc->dev, "WRC, PORTSC = %#x\n", portsc);
  2469. clear_port_change(xudc, PORTSC_WRC | PORTSC_PED);
  2470. if (!(xudc_readl(xudc, PORTSC) & PORTSC_WPR))
  2471. tegra_xudc_port_reset(xudc);
  2472. }
  2473. portsc = xudc_readl(xudc, PORTSC);
  2474. if (portsc & PORTSC_CSC) {
  2475. dev_dbg(xudc->dev, "CSC, PORTSC = %#x\n", portsc);
  2476. clear_port_change(xudc, PORTSC_CSC);
  2477. if (portsc & PORTSC_CCS)
  2478. tegra_xudc_port_connect(xudc);
  2479. else
  2480. tegra_xudc_port_disconnect(xudc);
  2481. if (xudc->wait_csc) {
  2482. cancel_delayed_work(&xudc->plc_reset_work);
  2483. xudc->wait_csc = false;
  2484. }
  2485. }
  2486. portsc = xudc_readl(xudc, PORTSC);
  2487. if (portsc & PORTSC_PLC) {
  2488. u32 pls = (portsc & PORTSC_PLS_MASK) >> PORTSC_PLS_SHIFT;
  2489. dev_dbg(xudc->dev, "PLC, PORTSC = %#x\n", portsc);
  2490. clear_port_change(xudc, PORTSC_PLC);
  2491. switch (pls) {
  2492. case PORTSC_PLS_U3:
  2493. tegra_xudc_port_suspend(xudc);
  2494. break;
  2495. case PORTSC_PLS_U0:
  2496. if (xudc->gadget.speed < USB_SPEED_SUPER)
  2497. tegra_xudc_port_resume(xudc);
  2498. break;
  2499. case PORTSC_PLS_RESUME:
  2500. if (xudc->gadget.speed == USB_SPEED_SUPER)
  2501. tegra_xudc_port_resume(xudc);
  2502. break;
  2503. case PORTSC_PLS_INACTIVE:
  2504. schedule_delayed_work(&xudc->plc_reset_work,
  2505. msecs_to_jiffies(TOGGLE_VBUS_WAIT_MS));
  2506. xudc->wait_csc = true;
  2507. break;
  2508. default:
  2509. break;
  2510. }
  2511. }
  2512. if (portsc & PORTSC_CEC) {
  2513. dev_warn(xudc->dev, "CEC, PORTSC = %#x\n", portsc);
  2514. clear_port_change(xudc, PORTSC_CEC);
  2515. }
  2516. dev_dbg(xudc->dev, "PORTSC = %#x\n", xudc_readl(xudc, PORTSC));
  2517. }
  2518. static void tegra_xudc_handle_port_status(struct tegra_xudc *xudc)
  2519. {
  2520. while ((xudc_readl(xudc, PORTSC) & PORTSC_CHANGE_MASK) ||
  2521. (xudc_readl(xudc, PORTHALT) & PORTHALT_STCHG_REQ))
  2522. __tegra_xudc_handle_port_status(xudc);
  2523. }
  2524. static void tegra_xudc_handle_event(struct tegra_xudc *xudc,
  2525. struct tegra_xudc_trb *event)
  2526. {
  2527. u32 type = trb_read_type(event);
  2528. dump_trb(xudc, "EVENT", event);
  2529. switch (type) {
  2530. case TRB_TYPE_PORT_STATUS_CHANGE_EVENT:
  2531. tegra_xudc_handle_port_status(xudc);
  2532. break;
  2533. case TRB_TYPE_TRANSFER_EVENT:
  2534. tegra_xudc_handle_transfer_event(xudc, event);
  2535. break;
  2536. case TRB_TYPE_SETUP_PACKET_EVENT:
  2537. tegra_xudc_handle_ep0_event(xudc, event);
  2538. break;
  2539. default:
  2540. dev_info(xudc->dev, "Unrecognized TRB type = %#x\n", type);
  2541. break;
  2542. }
  2543. }
  2544. static void tegra_xudc_process_event_ring(struct tegra_xudc *xudc)
  2545. {
  2546. struct tegra_xudc_trb *event;
  2547. dma_addr_t erdp;
  2548. while (true) {
  2549. event = xudc->event_ring[xudc->event_ring_index] +
  2550. xudc->event_ring_deq_ptr;
  2551. if (trb_read_cycle(event) != xudc->ccs)
  2552. break;
  2553. tegra_xudc_handle_event(xudc, event);
  2554. xudc->event_ring_deq_ptr++;
  2555. if (xudc->event_ring_deq_ptr == XUDC_EVENT_RING_SIZE) {
  2556. xudc->event_ring_deq_ptr = 0;
  2557. xudc->event_ring_index++;
  2558. }
  2559. if (xudc->event_ring_index == XUDC_NR_EVENT_RINGS) {
  2560. xudc->event_ring_index = 0;
  2561. xudc->ccs = !xudc->ccs;
  2562. }
  2563. }
  2564. erdp = xudc->event_ring_phys[xudc->event_ring_index] +
  2565. xudc->event_ring_deq_ptr * sizeof(*event);
  2566. xudc_writel(xudc, upper_32_bits(erdp), ERDPHI);
  2567. xudc_writel(xudc, lower_32_bits(erdp) | ERDPLO_EHB, ERDPLO);
  2568. }
  2569. static irqreturn_t tegra_xudc_irq(int irq, void *data)
  2570. {
  2571. struct tegra_xudc *xudc = data;
  2572. unsigned long flags;
  2573. u32 val;
  2574. val = xudc_readl(xudc, ST);
  2575. if (!(val & ST_IP))
  2576. return IRQ_NONE;
  2577. xudc_writel(xudc, ST_IP, ST);
  2578. spin_lock_irqsave(&xudc->lock, flags);
  2579. tegra_xudc_process_event_ring(xudc);
  2580. spin_unlock_irqrestore(&xudc->lock, flags);
  2581. return IRQ_HANDLED;
  2582. }
  2583. static int tegra_xudc_alloc_ep(struct tegra_xudc *xudc, unsigned int index)
  2584. {
  2585. struct tegra_xudc_ep *ep = &xudc->ep[index];
  2586. ep->xudc = xudc;
  2587. ep->index = index;
  2588. ep->context = &xudc->ep_context[index];
  2589. INIT_LIST_HEAD(&ep->queue);
  2590. /*
  2591. * EP1 would be the input endpoint corresponding to EP0, but since
  2592. * EP0 is bi-directional, EP1 is unused.
  2593. */
  2594. if (index == 1)
  2595. return 0;
  2596. ep->transfer_ring = dma_pool_alloc(xudc->transfer_ring_pool,
  2597. GFP_KERNEL,
  2598. &ep->transfer_ring_phys);
  2599. if (!ep->transfer_ring)
  2600. return -ENOMEM;
  2601. if (index) {
  2602. snprintf(ep->name, sizeof(ep->name), "ep%u%s", index / 2,
  2603. (index % 2 == 0) ? "out" : "in");
  2604. ep->usb_ep.name = ep->name;
  2605. usb_ep_set_maxpacket_limit(&ep->usb_ep, 1024);
  2606. ep->usb_ep.max_streams = 16;
  2607. ep->usb_ep.ops = &tegra_xudc_ep_ops;
  2608. ep->usb_ep.caps.type_bulk = true;
  2609. ep->usb_ep.caps.type_int = true;
  2610. if (index & 1)
  2611. ep->usb_ep.caps.dir_in = true;
  2612. else
  2613. ep->usb_ep.caps.dir_out = true;
  2614. list_add_tail(&ep->usb_ep.ep_list, &xudc->gadget.ep_list);
  2615. } else {
  2616. strscpy(ep->name, "ep0", 3);
  2617. ep->usb_ep.name = ep->name;
  2618. usb_ep_set_maxpacket_limit(&ep->usb_ep, 512);
  2619. ep->usb_ep.ops = &tegra_xudc_ep0_ops;
  2620. ep->usb_ep.caps.type_control = true;
  2621. ep->usb_ep.caps.dir_in = true;
  2622. ep->usb_ep.caps.dir_out = true;
  2623. }
  2624. return 0;
  2625. }
  2626. static void tegra_xudc_free_ep(struct tegra_xudc *xudc, unsigned int index)
  2627. {
  2628. struct tegra_xudc_ep *ep = &xudc->ep[index];
  2629. /*
  2630. * EP1 would be the input endpoint corresponding to EP0, but since
  2631. * EP0 is bi-directional, EP1 is unused.
  2632. */
  2633. if (index == 1)
  2634. return;
  2635. dma_pool_free(xudc->transfer_ring_pool, ep->transfer_ring,
  2636. ep->transfer_ring_phys);
  2637. }
  2638. static int tegra_xudc_alloc_eps(struct tegra_xudc *xudc)
  2639. {
  2640. struct usb_request *req;
  2641. unsigned int i;
  2642. int err;
  2643. xudc->ep_context =
  2644. dma_alloc_coherent(xudc->dev, XUDC_NR_EPS *
  2645. sizeof(*xudc->ep_context),
  2646. &xudc->ep_context_phys, GFP_KERNEL);
  2647. if (!xudc->ep_context)
  2648. return -ENOMEM;
  2649. xudc->transfer_ring_pool =
  2650. dmam_pool_create(dev_name(xudc->dev), xudc->dev,
  2651. XUDC_TRANSFER_RING_SIZE *
  2652. sizeof(struct tegra_xudc_trb),
  2653. sizeof(struct tegra_xudc_trb), 0);
  2654. if (!xudc->transfer_ring_pool) {
  2655. err = -ENOMEM;
  2656. goto free_ep_context;
  2657. }
  2658. INIT_LIST_HEAD(&xudc->gadget.ep_list);
  2659. for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) {
  2660. err = tegra_xudc_alloc_ep(xudc, i);
  2661. if (err < 0)
  2662. goto free_eps;
  2663. }
  2664. req = tegra_xudc_ep_alloc_request(&xudc->ep[0].usb_ep, GFP_KERNEL);
  2665. if (!req) {
  2666. err = -ENOMEM;
  2667. goto free_eps;
  2668. }
  2669. xudc->ep0_req = to_xudc_req(req);
  2670. return 0;
  2671. free_eps:
  2672. for (; i > 0; i--)
  2673. tegra_xudc_free_ep(xudc, i - 1);
  2674. free_ep_context:
  2675. dma_free_coherent(xudc->dev, XUDC_NR_EPS * sizeof(*xudc->ep_context),
  2676. xudc->ep_context, xudc->ep_context_phys);
  2677. return err;
  2678. }
  2679. static void tegra_xudc_init_eps(struct tegra_xudc *xudc)
  2680. {
  2681. xudc_writel(xudc, lower_32_bits(xudc->ep_context_phys), ECPLO);
  2682. xudc_writel(xudc, upper_32_bits(xudc->ep_context_phys), ECPHI);
  2683. }
  2684. static void tegra_xudc_free_eps(struct tegra_xudc *xudc)
  2685. {
  2686. unsigned int i;
  2687. tegra_xudc_ep_free_request(&xudc->ep[0].usb_ep,
  2688. &xudc->ep0_req->usb_req);
  2689. for (i = 0; i < ARRAY_SIZE(xudc->ep); i++)
  2690. tegra_xudc_free_ep(xudc, i);
  2691. dma_free_coherent(xudc->dev, XUDC_NR_EPS * sizeof(*xudc->ep_context),
  2692. xudc->ep_context, xudc->ep_context_phys);
  2693. }
  2694. static int tegra_xudc_alloc_event_ring(struct tegra_xudc *xudc)
  2695. {
  2696. unsigned int i;
  2697. for (i = 0; i < ARRAY_SIZE(xudc->event_ring); i++) {
  2698. xudc->event_ring[i] =
  2699. dma_alloc_coherent(xudc->dev, XUDC_EVENT_RING_SIZE *
  2700. sizeof(*xudc->event_ring[i]),
  2701. &xudc->event_ring_phys[i],
  2702. GFP_KERNEL);
  2703. if (!xudc->event_ring[i])
  2704. goto free_dma;
  2705. }
  2706. return 0;
  2707. free_dma:
  2708. for (; i > 0; i--) {
  2709. dma_free_coherent(xudc->dev, XUDC_EVENT_RING_SIZE *
  2710. sizeof(*xudc->event_ring[i - 1]),
  2711. xudc->event_ring[i - 1],
  2712. xudc->event_ring_phys[i - 1]);
  2713. }
  2714. return -ENOMEM;
  2715. }
  2716. static void tegra_xudc_init_event_ring(struct tegra_xudc *xudc)
  2717. {
  2718. unsigned int i;
  2719. u32 val;
  2720. for (i = 0; i < ARRAY_SIZE(xudc->event_ring); i++) {
  2721. memset(xudc->event_ring[i], 0, XUDC_EVENT_RING_SIZE *
  2722. sizeof(*xudc->event_ring[i]));
  2723. val = xudc_readl(xudc, ERSTSZ);
  2724. val &= ~(ERSTSZ_ERSTXSZ_MASK << ERSTSZ_ERSTXSZ_SHIFT(i));
  2725. val |= XUDC_EVENT_RING_SIZE << ERSTSZ_ERSTXSZ_SHIFT(i);
  2726. xudc_writel(xudc, val, ERSTSZ);
  2727. xudc_writel(xudc, lower_32_bits(xudc->event_ring_phys[i]),
  2728. ERSTXBALO(i));
  2729. xudc_writel(xudc, upper_32_bits(xudc->event_ring_phys[i]),
  2730. ERSTXBAHI(i));
  2731. }
  2732. val = lower_32_bits(xudc->event_ring_phys[0]);
  2733. xudc_writel(xudc, val, ERDPLO);
  2734. val |= EREPLO_ECS;
  2735. xudc_writel(xudc, val, EREPLO);
  2736. val = upper_32_bits(xudc->event_ring_phys[0]);
  2737. xudc_writel(xudc, val, ERDPHI);
  2738. xudc_writel(xudc, val, EREPHI);
  2739. xudc->ccs = true;
  2740. xudc->event_ring_index = 0;
  2741. xudc->event_ring_deq_ptr = 0;
  2742. }
  2743. static void tegra_xudc_free_event_ring(struct tegra_xudc *xudc)
  2744. {
  2745. unsigned int i;
  2746. for (i = 0; i < ARRAY_SIZE(xudc->event_ring); i++) {
  2747. dma_free_coherent(xudc->dev, XUDC_EVENT_RING_SIZE *
  2748. sizeof(*xudc->event_ring[i]),
  2749. xudc->event_ring[i],
  2750. xudc->event_ring_phys[i]);
  2751. }
  2752. }
  2753. static void tegra_xudc_fpci_ipfs_init(struct tegra_xudc *xudc)
  2754. {
  2755. u32 val;
  2756. if (xudc->soc->has_ipfs) {
  2757. val = ipfs_readl(xudc, XUSB_DEV_CONFIGURATION_0);
  2758. val |= XUSB_DEV_CONFIGURATION_0_EN_FPCI;
  2759. ipfs_writel(xudc, val, XUSB_DEV_CONFIGURATION_0);
  2760. usleep_range(10, 15);
  2761. }
  2762. /* Enable bus master */
  2763. val = XUSB_DEV_CFG_1_IO_SPACE_EN | XUSB_DEV_CFG_1_MEMORY_SPACE_EN |
  2764. XUSB_DEV_CFG_1_BUS_MASTER_EN;
  2765. fpci_writel(xudc, val, XUSB_DEV_CFG_1);
  2766. /* Program BAR0 space */
  2767. val = fpci_readl(xudc, XUSB_DEV_CFG_4);
  2768. val &= ~(XUSB_DEV_CFG_4_BASE_ADDR_MASK);
  2769. val |= xudc->phys_base & (XUSB_DEV_CFG_4_BASE_ADDR_MASK);
  2770. fpci_writel(xudc, val, XUSB_DEV_CFG_4);
  2771. fpci_writel(xudc, upper_32_bits(xudc->phys_base), XUSB_DEV_CFG_5);
  2772. usleep_range(100, 200);
  2773. if (xudc->soc->has_ipfs) {
  2774. /* Enable interrupt assertion */
  2775. val = ipfs_readl(xudc, XUSB_DEV_INTR_MASK_0);
  2776. val |= XUSB_DEV_INTR_MASK_0_IP_INT_MASK;
  2777. ipfs_writel(xudc, val, XUSB_DEV_INTR_MASK_0);
  2778. }
  2779. }
  2780. static void tegra_xudc_device_params_init(struct tegra_xudc *xudc)
  2781. {
  2782. u32 val, imod;
  2783. val = xudc_readl(xudc, BLCG);
  2784. if (xudc->soc->has_ipfs) {
  2785. val |= BLCG_ALL;
  2786. val &= ~(BLCG_DFPCI | BLCG_UFPCI | BLCG_FE |
  2787. BLCG_COREPLL_PWRDN);
  2788. val |= BLCG_IOPLL_0_PWRDN;
  2789. val |= BLCG_IOPLL_1_PWRDN;
  2790. val |= BLCG_IOPLL_2_PWRDN;
  2791. } else {
  2792. val &= ~BLCG_COREPLL_PWRDN;
  2793. }
  2794. xudc_writel(xudc, val, BLCG);
  2795. if (xudc->soc->port_speed_quirk)
  2796. tegra_xudc_limit_port_speed(xudc);
  2797. /* Set a reasonable U3 exit timer value. */
  2798. val = xudc_readl(xudc, SSPX_CORE_PADCTL4);
  2799. val &= ~(SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3_MASK);
  2800. val |= SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3(0x5dc0);
  2801. xudc_writel(xudc, val, SSPX_CORE_PADCTL4);
  2802. /* Default ping LFPS tBurst is too large. */
  2803. val = xudc_readl(xudc, SSPX_CORE_CNT0);
  2804. val &= ~(SSPX_CORE_CNT0_PING_TBURST_MASK);
  2805. val |= SSPX_CORE_CNT0_PING_TBURST(0xa);
  2806. xudc_writel(xudc, val, SSPX_CORE_CNT0);
  2807. /* Default tPortConfiguration timeout is too small. */
  2808. val = xudc_readl(xudc, SSPX_CORE_CNT30);
  2809. val &= ~(SSPX_CORE_CNT30_LMPITP_TIMER_MASK);
  2810. val |= SSPX_CORE_CNT30_LMPITP_TIMER(0x978);
  2811. xudc_writel(xudc, val, SSPX_CORE_CNT30);
  2812. if (xudc->soc->lpm_enable) {
  2813. /* Set L1 resume duration to 95 us. */
  2814. val = xudc_readl(xudc, HSFSPI_COUNT13);
  2815. val &= ~(HSFSPI_COUNT13_U2_RESUME_K_DURATION_MASK);
  2816. val |= HSFSPI_COUNT13_U2_RESUME_K_DURATION(0x2c88);
  2817. xudc_writel(xudc, val, HSFSPI_COUNT13);
  2818. }
  2819. /*
  2820. * Compliance suite appears to be violating polling LFPS tBurst max
  2821. * of 1.4us. Send 1.45us instead.
  2822. */
  2823. val = xudc_readl(xudc, SSPX_CORE_CNT32);
  2824. val &= ~(SSPX_CORE_CNT32_POLL_TBURST_MAX_MASK);
  2825. val |= SSPX_CORE_CNT32_POLL_TBURST_MAX(0xb0);
  2826. xudc_writel(xudc, val, SSPX_CORE_CNT32);
  2827. /* Direct HS/FS port instance to RxDetect. */
  2828. val = xudc_readl(xudc, CFG_DEV_FE);
  2829. val &= ~(CFG_DEV_FE_PORTREGSEL_MASK);
  2830. val |= CFG_DEV_FE_PORTREGSEL(CFG_DEV_FE_PORTREGSEL_HSFS_PI);
  2831. xudc_writel(xudc, val, CFG_DEV_FE);
  2832. val = xudc_readl(xudc, PORTSC);
  2833. val &= ~(PORTSC_CHANGE_MASK | PORTSC_PLS_MASK);
  2834. val |= PORTSC_LWS | PORTSC_PLS(PORTSC_PLS_RXDETECT);
  2835. xudc_writel(xudc, val, PORTSC);
  2836. /* Direct SS port instance to RxDetect. */
  2837. val = xudc_readl(xudc, CFG_DEV_FE);
  2838. val &= ~(CFG_DEV_FE_PORTREGSEL_MASK);
  2839. val |= CFG_DEV_FE_PORTREGSEL_SS_PI & CFG_DEV_FE_PORTREGSEL_MASK;
  2840. xudc_writel(xudc, val, CFG_DEV_FE);
  2841. val = xudc_readl(xudc, PORTSC);
  2842. val &= ~(PORTSC_CHANGE_MASK | PORTSC_PLS_MASK);
  2843. val |= PORTSC_LWS | PORTSC_PLS(PORTSC_PLS_RXDETECT);
  2844. xudc_writel(xudc, val, PORTSC);
  2845. /* Restore port instance. */
  2846. val = xudc_readl(xudc, CFG_DEV_FE);
  2847. val &= ~(CFG_DEV_FE_PORTREGSEL_MASK);
  2848. xudc_writel(xudc, val, CFG_DEV_FE);
  2849. /*
  2850. * Enable INFINITE_SS_RETRY to prevent device from entering
  2851. * Disabled.Error when attached to buggy SuperSpeed hubs.
  2852. */
  2853. val = xudc_readl(xudc, CFG_DEV_FE);
  2854. val |= CFG_DEV_FE_INFINITE_SS_RETRY;
  2855. xudc_writel(xudc, val, CFG_DEV_FE);
  2856. /* Set interrupt moderation. */
  2857. imod = XUDC_INTERRUPT_MODERATION_US * 4;
  2858. val = xudc_readl(xudc, RT_IMOD);
  2859. val &= ~((RT_IMOD_IMODI_MASK) | (RT_IMOD_IMODC_MASK));
  2860. val |= (RT_IMOD_IMODI(imod) | RT_IMOD_IMODC(imod));
  2861. xudc_writel(xudc, val, RT_IMOD);
  2862. /* increase SSPI transaction timeout from 32us to 512us */
  2863. val = xudc_readl(xudc, CFG_DEV_SSPI_XFER);
  2864. val &= ~(CFG_DEV_SSPI_XFER_ACKTIMEOUT_MASK);
  2865. val |= CFG_DEV_SSPI_XFER_ACKTIMEOUT(0xf000);
  2866. xudc_writel(xudc, val, CFG_DEV_SSPI_XFER);
  2867. }
  2868. static int tegra_xudc_phy_get(struct tegra_xudc *xudc)
  2869. {
  2870. int err = 0, usb3_companion_port;
  2871. unsigned int i, j;
  2872. xudc->utmi_phy = devm_kcalloc(xudc->dev, xudc->soc->num_phys,
  2873. sizeof(*xudc->utmi_phy), GFP_KERNEL);
  2874. if (!xudc->utmi_phy)
  2875. return -ENOMEM;
  2876. xudc->usb3_phy = devm_kcalloc(xudc->dev, xudc->soc->num_phys,
  2877. sizeof(*xudc->usb3_phy), GFP_KERNEL);
  2878. if (!xudc->usb3_phy)
  2879. return -ENOMEM;
  2880. xudc->usbphy = devm_kcalloc(xudc->dev, xudc->soc->num_phys,
  2881. sizeof(*xudc->usbphy), GFP_KERNEL);
  2882. if (!xudc->usbphy)
  2883. return -ENOMEM;
  2884. xudc->vbus_nb.notifier_call = tegra_xudc_vbus_notify;
  2885. for (i = 0; i < xudc->soc->num_phys; i++) {
  2886. char phy_name[] = "usb.-.";
  2887. /* Get USB2 phy */
  2888. snprintf(phy_name, sizeof(phy_name), "usb2-%d", i);
  2889. xudc->utmi_phy[i] = devm_phy_optional_get(xudc->dev, phy_name);
  2890. if (IS_ERR(xudc->utmi_phy[i])) {
  2891. err = PTR_ERR(xudc->utmi_phy[i]);
  2892. dev_err_probe(xudc->dev, err,
  2893. "failed to get PHY for phy-name usb2-%d\n", i);
  2894. goto clean_up;
  2895. } else if (xudc->utmi_phy[i]) {
  2896. /* Get usb-phy, if utmi phy is available */
  2897. xudc->usbphy[i] = devm_usb_get_phy_by_node(xudc->dev,
  2898. xudc->utmi_phy[i]->dev.of_node,
  2899. NULL);
  2900. if (IS_ERR(xudc->usbphy[i])) {
  2901. err = PTR_ERR(xudc->usbphy[i]);
  2902. dev_err_probe(xudc->dev, err,
  2903. "failed to get usbphy-%d\n", i);
  2904. goto clean_up;
  2905. }
  2906. } else if (!xudc->utmi_phy[i]) {
  2907. /* if utmi phy is not available, ignore USB3 phy get */
  2908. continue;
  2909. }
  2910. /* Get USB3 phy */
  2911. usb3_companion_port = tegra_xusb_padctl_get_usb3_companion(xudc->padctl, i);
  2912. if (usb3_companion_port < 0)
  2913. continue;
  2914. for (j = 0; j < xudc->soc->num_phys; j++) {
  2915. snprintf(phy_name, sizeof(phy_name), "usb3-%d", j);
  2916. xudc->usb3_phy[i] = devm_phy_optional_get(xudc->dev, phy_name);
  2917. if (IS_ERR(xudc->usb3_phy[i])) {
  2918. err = PTR_ERR(xudc->usb3_phy[i]);
  2919. dev_err_probe(xudc->dev, err,
  2920. "failed to get PHY for phy-name usb3-%d\n", j);
  2921. goto clean_up;
  2922. } else if (xudc->usb3_phy[i]) {
  2923. int usb2_port =
  2924. tegra_xusb_padctl_get_port_number(xudc->utmi_phy[i]);
  2925. int usb3_port =
  2926. tegra_xusb_padctl_get_port_number(xudc->usb3_phy[i]);
  2927. if (usb3_port == usb3_companion_port) {
  2928. dev_dbg(xudc->dev, "USB2 port %d is paired with USB3 port %d for device mode port %d\n",
  2929. usb2_port, usb3_port, i);
  2930. break;
  2931. }
  2932. }
  2933. }
  2934. }
  2935. return err;
  2936. clean_up:
  2937. for (i = 0; i < xudc->soc->num_phys; i++) {
  2938. xudc->usb3_phy[i] = NULL;
  2939. xudc->utmi_phy[i] = NULL;
  2940. xudc->usbphy[i] = NULL;
  2941. }
  2942. return err;
  2943. }
  2944. static void tegra_xudc_phy_exit(struct tegra_xudc *xudc)
  2945. {
  2946. unsigned int i;
  2947. for (i = 0; i < xudc->soc->num_phys; i++) {
  2948. phy_exit(xudc->usb3_phy[i]);
  2949. phy_exit(xudc->utmi_phy[i]);
  2950. }
  2951. }
  2952. static int tegra_xudc_phy_init(struct tegra_xudc *xudc)
  2953. {
  2954. int err;
  2955. unsigned int i;
  2956. for (i = 0; i < xudc->soc->num_phys; i++) {
  2957. err = phy_init(xudc->utmi_phy[i]);
  2958. if (err < 0) {
  2959. dev_err(xudc->dev, "UTMI PHY #%u initialization failed: %d\n", i, err);
  2960. goto exit_phy;
  2961. }
  2962. err = phy_init(xudc->usb3_phy[i]);
  2963. if (err < 0) {
  2964. dev_err(xudc->dev, "USB3 PHY #%u initialization failed: %d\n", i, err);
  2965. goto exit_phy;
  2966. }
  2967. }
  2968. return 0;
  2969. exit_phy:
  2970. tegra_xudc_phy_exit(xudc);
  2971. return err;
  2972. }
  2973. static const char * const tegra210_xudc_supply_names[] = {
  2974. "hvdd-usb",
  2975. "avddio-usb",
  2976. };
  2977. static const char * const tegra210_xudc_clock_names[] = {
  2978. "dev",
  2979. "ss",
  2980. "ss_src",
  2981. "hs_src",
  2982. "fs_src",
  2983. };
  2984. static const char * const tegra186_xudc_clock_names[] = {
  2985. "dev",
  2986. "ss",
  2987. "ss_src",
  2988. "fs_src",
  2989. };
  2990. static struct tegra_xudc_soc tegra210_xudc_soc_data = {
  2991. .supply_names = tegra210_xudc_supply_names,
  2992. .num_supplies = ARRAY_SIZE(tegra210_xudc_supply_names),
  2993. .clock_names = tegra210_xudc_clock_names,
  2994. .num_clks = ARRAY_SIZE(tegra210_xudc_clock_names),
  2995. .num_phys = 4,
  2996. .u1_enable = false,
  2997. .u2_enable = true,
  2998. .lpm_enable = false,
  2999. .invalid_seq_num = true,
  3000. .pls_quirk = true,
  3001. .port_reset_quirk = true,
  3002. .port_speed_quirk = false,
  3003. .has_ipfs = true,
  3004. };
  3005. static struct tegra_xudc_soc tegra186_xudc_soc_data = {
  3006. .clock_names = tegra186_xudc_clock_names,
  3007. .num_clks = ARRAY_SIZE(tegra186_xudc_clock_names),
  3008. .num_phys = 4,
  3009. .u1_enable = true,
  3010. .u2_enable = true,
  3011. .lpm_enable = false,
  3012. .invalid_seq_num = false,
  3013. .pls_quirk = false,
  3014. .port_reset_quirk = false,
  3015. .port_speed_quirk = false,
  3016. .has_ipfs = false,
  3017. };
  3018. static struct tegra_xudc_soc tegra194_xudc_soc_data = {
  3019. .clock_names = tegra186_xudc_clock_names,
  3020. .num_clks = ARRAY_SIZE(tegra186_xudc_clock_names),
  3021. .num_phys = 4,
  3022. .u1_enable = true,
  3023. .u2_enable = true,
  3024. .lpm_enable = true,
  3025. .invalid_seq_num = false,
  3026. .pls_quirk = false,
  3027. .port_reset_quirk = false,
  3028. .port_speed_quirk = true,
  3029. .has_ipfs = false,
  3030. };
  3031. static struct tegra_xudc_soc tegra234_xudc_soc_data = {
  3032. .clock_names = tegra186_xudc_clock_names,
  3033. .num_clks = ARRAY_SIZE(tegra186_xudc_clock_names),
  3034. .num_phys = 4,
  3035. .u1_enable = true,
  3036. .u2_enable = true,
  3037. .lpm_enable = true,
  3038. .invalid_seq_num = false,
  3039. .pls_quirk = false,
  3040. .port_reset_quirk = false,
  3041. .has_ipfs = false,
  3042. };
  3043. static const struct of_device_id tegra_xudc_of_match[] = {
  3044. {
  3045. .compatible = "nvidia,tegra210-xudc",
  3046. .data = &tegra210_xudc_soc_data
  3047. },
  3048. {
  3049. .compatible = "nvidia,tegra186-xudc",
  3050. .data = &tegra186_xudc_soc_data
  3051. },
  3052. {
  3053. .compatible = "nvidia,tegra194-xudc",
  3054. .data = &tegra194_xudc_soc_data
  3055. },
  3056. {
  3057. .compatible = "nvidia,tegra234-xudc",
  3058. .data = &tegra234_xudc_soc_data
  3059. },
  3060. { }
  3061. };
  3062. MODULE_DEVICE_TABLE(of, tegra_xudc_of_match);
  3063. static void tegra_xudc_powerdomain_remove(struct tegra_xudc *xudc)
  3064. {
  3065. if (xudc->genpd_dl_ss)
  3066. device_link_del(xudc->genpd_dl_ss);
  3067. if (xudc->genpd_dl_device)
  3068. device_link_del(xudc->genpd_dl_device);
  3069. if (xudc->genpd_dev_ss)
  3070. dev_pm_domain_detach(xudc->genpd_dev_ss, true);
  3071. if (xudc->genpd_dev_device)
  3072. dev_pm_domain_detach(xudc->genpd_dev_device, true);
  3073. }
  3074. static int tegra_xudc_powerdomain_init(struct tegra_xudc *xudc)
  3075. {
  3076. struct device *dev = xudc->dev;
  3077. int err;
  3078. xudc->genpd_dev_device = dev_pm_domain_attach_by_name(dev, "dev");
  3079. if (IS_ERR(xudc->genpd_dev_device)) {
  3080. err = PTR_ERR(xudc->genpd_dev_device);
  3081. dev_err(dev, "failed to get device power domain: %d\n", err);
  3082. return err;
  3083. }
  3084. xudc->genpd_dev_ss = dev_pm_domain_attach_by_name(dev, "ss");
  3085. if (IS_ERR(xudc->genpd_dev_ss)) {
  3086. err = PTR_ERR(xudc->genpd_dev_ss);
  3087. dev_err(dev, "failed to get SuperSpeed power domain: %d\n", err);
  3088. return err;
  3089. }
  3090. xudc->genpd_dl_device = device_link_add(dev, xudc->genpd_dev_device,
  3091. DL_FLAG_PM_RUNTIME |
  3092. DL_FLAG_STATELESS);
  3093. if (!xudc->genpd_dl_device) {
  3094. dev_err(dev, "failed to add USB device link\n");
  3095. return -ENODEV;
  3096. }
  3097. xudc->genpd_dl_ss = device_link_add(dev, xudc->genpd_dev_ss,
  3098. DL_FLAG_PM_RUNTIME |
  3099. DL_FLAG_STATELESS);
  3100. if (!xudc->genpd_dl_ss) {
  3101. dev_err(dev, "failed to add SuperSpeed device link\n");
  3102. return -ENODEV;
  3103. }
  3104. return 0;
  3105. }
  3106. static int tegra_xudc_probe(struct platform_device *pdev)
  3107. {
  3108. struct tegra_xudc *xudc;
  3109. struct resource *res;
  3110. unsigned int i;
  3111. int err;
  3112. xudc = devm_kzalloc(&pdev->dev, sizeof(*xudc), GFP_KERNEL);
  3113. if (!xudc)
  3114. return -ENOMEM;
  3115. xudc->dev = &pdev->dev;
  3116. platform_set_drvdata(pdev, xudc);
  3117. xudc->soc = of_device_get_match_data(&pdev->dev);
  3118. if (!xudc->soc)
  3119. return -ENODEV;
  3120. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
  3121. xudc->base = devm_ioremap_resource(&pdev->dev, res);
  3122. if (IS_ERR(xudc->base))
  3123. return PTR_ERR(xudc->base);
  3124. xudc->phys_base = res->start;
  3125. xudc->fpci = devm_platform_ioremap_resource_byname(pdev, "fpci");
  3126. if (IS_ERR(xudc->fpci))
  3127. return PTR_ERR(xudc->fpci);
  3128. if (xudc->soc->has_ipfs) {
  3129. xudc->ipfs = devm_platform_ioremap_resource_byname(pdev, "ipfs");
  3130. if (IS_ERR(xudc->ipfs))
  3131. return PTR_ERR(xudc->ipfs);
  3132. }
  3133. xudc->irq = platform_get_irq(pdev, 0);
  3134. if (xudc->irq < 0)
  3135. return xudc->irq;
  3136. err = devm_request_irq(&pdev->dev, xudc->irq, tegra_xudc_irq, 0,
  3137. dev_name(&pdev->dev), xudc);
  3138. if (err < 0) {
  3139. dev_err(xudc->dev, "failed to claim IRQ#%u: %d\n", xudc->irq,
  3140. err);
  3141. return err;
  3142. }
  3143. xudc->clks = devm_kcalloc(&pdev->dev, xudc->soc->num_clks, sizeof(*xudc->clks),
  3144. GFP_KERNEL);
  3145. if (!xudc->clks)
  3146. return -ENOMEM;
  3147. for (i = 0; i < xudc->soc->num_clks; i++)
  3148. xudc->clks[i].id = xudc->soc->clock_names[i];
  3149. err = devm_clk_bulk_get(&pdev->dev, xudc->soc->num_clks, xudc->clks);
  3150. if (err) {
  3151. dev_err_probe(xudc->dev, err, "failed to request clocks\n");
  3152. return err;
  3153. }
  3154. xudc->supplies = devm_kcalloc(&pdev->dev, xudc->soc->num_supplies,
  3155. sizeof(*xudc->supplies), GFP_KERNEL);
  3156. if (!xudc->supplies)
  3157. return -ENOMEM;
  3158. for (i = 0; i < xudc->soc->num_supplies; i++)
  3159. xudc->supplies[i].supply = xudc->soc->supply_names[i];
  3160. err = devm_regulator_bulk_get(&pdev->dev, xudc->soc->num_supplies,
  3161. xudc->supplies);
  3162. if (err) {
  3163. dev_err_probe(xudc->dev, err, "failed to request regulators\n");
  3164. return err;
  3165. }
  3166. xudc->padctl = tegra_xusb_padctl_get(&pdev->dev);
  3167. if (IS_ERR(xudc->padctl))
  3168. return PTR_ERR(xudc->padctl);
  3169. err = regulator_bulk_enable(xudc->soc->num_supplies, xudc->supplies);
  3170. if (err) {
  3171. dev_err(xudc->dev, "failed to enable regulators: %d\n", err);
  3172. goto put_padctl;
  3173. }
  3174. err = tegra_xudc_phy_get(xudc);
  3175. if (err)
  3176. goto disable_regulator;
  3177. err = tegra_xudc_powerdomain_init(xudc);
  3178. if (err)
  3179. goto put_powerdomains;
  3180. err = tegra_xudc_phy_init(xudc);
  3181. if (err)
  3182. goto put_powerdomains;
  3183. err = tegra_xudc_alloc_event_ring(xudc);
  3184. if (err)
  3185. goto disable_phy;
  3186. err = tegra_xudc_alloc_eps(xudc);
  3187. if (err)
  3188. goto free_event_ring;
  3189. spin_lock_init(&xudc->lock);
  3190. init_completion(&xudc->disconnect_complete);
  3191. INIT_WORK(&xudc->usb_role_sw_work, tegra_xudc_usb_role_sw_work);
  3192. INIT_DELAYED_WORK(&xudc->plc_reset_work, tegra_xudc_plc_reset_work);
  3193. INIT_DELAYED_WORK(&xudc->port_reset_war_work,
  3194. tegra_xudc_port_reset_war_work);
  3195. pm_runtime_enable(&pdev->dev);
  3196. xudc->gadget.ops = &tegra_xudc_gadget_ops;
  3197. xudc->gadget.ep0 = &xudc->ep[0].usb_ep;
  3198. xudc->gadget.name = "tegra-xudc";
  3199. xudc->gadget.max_speed = USB_SPEED_SUPER;
  3200. err = usb_add_gadget_udc(&pdev->dev, &xudc->gadget);
  3201. if (err) {
  3202. dev_err(&pdev->dev, "failed to add USB gadget: %d\n", err);
  3203. goto free_eps;
  3204. }
  3205. for (i = 0; i < xudc->soc->num_phys; i++) {
  3206. if (!xudc->usbphy[i])
  3207. continue;
  3208. usb_register_notifier(xudc->usbphy[i], &xudc->vbus_nb);
  3209. tegra_xudc_update_data_role(xudc, xudc->usbphy[i]);
  3210. }
  3211. return 0;
  3212. free_eps:
  3213. pm_runtime_disable(&pdev->dev);
  3214. tegra_xudc_free_eps(xudc);
  3215. free_event_ring:
  3216. tegra_xudc_free_event_ring(xudc);
  3217. disable_phy:
  3218. tegra_xudc_phy_exit(xudc);
  3219. put_powerdomains:
  3220. tegra_xudc_powerdomain_remove(xudc);
  3221. disable_regulator:
  3222. regulator_bulk_disable(xudc->soc->num_supplies, xudc->supplies);
  3223. put_padctl:
  3224. tegra_xusb_padctl_put(xudc->padctl);
  3225. return err;
  3226. }
  3227. static void tegra_xudc_remove(struct platform_device *pdev)
  3228. {
  3229. struct tegra_xudc *xudc = platform_get_drvdata(pdev);
  3230. unsigned int i;
  3231. pm_runtime_get_sync(xudc->dev);
  3232. cancel_delayed_work_sync(&xudc->plc_reset_work);
  3233. cancel_work_sync(&xudc->usb_role_sw_work);
  3234. usb_del_gadget_udc(&xudc->gadget);
  3235. tegra_xudc_free_eps(xudc);
  3236. tegra_xudc_free_event_ring(xudc);
  3237. tegra_xudc_powerdomain_remove(xudc);
  3238. regulator_bulk_disable(xudc->soc->num_supplies, xudc->supplies);
  3239. for (i = 0; i < xudc->soc->num_phys; i++) {
  3240. phy_power_off(xudc->utmi_phy[i]);
  3241. phy_power_off(xudc->usb3_phy[i]);
  3242. }
  3243. tegra_xudc_phy_exit(xudc);
  3244. pm_runtime_disable(xudc->dev);
  3245. pm_runtime_put(xudc->dev);
  3246. tegra_xusb_padctl_put(xudc->padctl);
  3247. }
  3248. static int __maybe_unused tegra_xudc_powergate(struct tegra_xudc *xudc)
  3249. {
  3250. unsigned long flags;
  3251. u32 val;
  3252. dev_dbg(xudc->dev, "entering ELPG\n");
  3253. spin_lock_irqsave(&xudc->lock, flags);
  3254. xudc->powergated = true;
  3255. xudc->saved_regs.ctrl = xudc_readl(xudc, CTRL);
  3256. xudc->saved_regs.portpm = xudc_readl(xudc, PORTPM);
  3257. xudc_writel(xudc, 0, CTRL);
  3258. spin_unlock_irqrestore(&xudc->lock, flags);
  3259. val = xudc_readl(xudc, BLCG);
  3260. val |= BLCG_COREPLL_PWRDN;
  3261. xudc_writel(xudc, val, BLCG);
  3262. clk_bulk_disable_unprepare(xudc->soc->num_clks, xudc->clks);
  3263. regulator_bulk_disable(xudc->soc->num_supplies, xudc->supplies);
  3264. dev_dbg(xudc->dev, "entering ELPG done\n");
  3265. return 0;
  3266. }
  3267. static int __maybe_unused tegra_xudc_unpowergate(struct tegra_xudc *xudc)
  3268. {
  3269. unsigned long flags;
  3270. int err;
  3271. dev_dbg(xudc->dev, "exiting ELPG\n");
  3272. err = regulator_bulk_enable(xudc->soc->num_supplies,
  3273. xudc->supplies);
  3274. if (err < 0)
  3275. return err;
  3276. err = clk_bulk_prepare_enable(xudc->soc->num_clks, xudc->clks);
  3277. if (err < 0)
  3278. return err;
  3279. tegra_xudc_fpci_ipfs_init(xudc);
  3280. tegra_xudc_device_params_init(xudc);
  3281. tegra_xudc_init_event_ring(xudc);
  3282. tegra_xudc_init_eps(xudc);
  3283. xudc_writel(xudc, xudc->saved_regs.portpm, PORTPM);
  3284. xudc_writel(xudc, xudc->saved_regs.ctrl, CTRL);
  3285. spin_lock_irqsave(&xudc->lock, flags);
  3286. xudc->powergated = false;
  3287. spin_unlock_irqrestore(&xudc->lock, flags);
  3288. dev_dbg(xudc->dev, "exiting ELPG done\n");
  3289. return 0;
  3290. }
  3291. static int __maybe_unused tegra_xudc_suspend(struct device *dev)
  3292. {
  3293. struct tegra_xudc *xudc = dev_get_drvdata(dev);
  3294. unsigned long flags;
  3295. spin_lock_irqsave(&xudc->lock, flags);
  3296. xudc->suspended = true;
  3297. spin_unlock_irqrestore(&xudc->lock, flags);
  3298. flush_work(&xudc->usb_role_sw_work);
  3299. if (!pm_runtime_status_suspended(dev)) {
  3300. /* Forcibly disconnect before powergating. */
  3301. tegra_xudc_device_mode_off(xudc);
  3302. tegra_xudc_powergate(xudc);
  3303. }
  3304. pm_runtime_disable(dev);
  3305. return 0;
  3306. }
  3307. static int __maybe_unused tegra_xudc_resume(struct device *dev)
  3308. {
  3309. struct tegra_xudc *xudc = dev_get_drvdata(dev);
  3310. unsigned long flags;
  3311. int err;
  3312. err = tegra_xudc_unpowergate(xudc);
  3313. if (err < 0)
  3314. return err;
  3315. spin_lock_irqsave(&xudc->lock, flags);
  3316. xudc->suspended = false;
  3317. if (xudc->device_mode != xudc->current_device_mode)
  3318. schedule_work(&xudc->usb_role_sw_work);
  3319. spin_unlock_irqrestore(&xudc->lock, flags);
  3320. pm_runtime_enable(dev);
  3321. return 0;
  3322. }
  3323. static int __maybe_unused tegra_xudc_runtime_suspend(struct device *dev)
  3324. {
  3325. struct tegra_xudc *xudc = dev_get_drvdata(dev);
  3326. return tegra_xudc_powergate(xudc);
  3327. }
  3328. static int __maybe_unused tegra_xudc_runtime_resume(struct device *dev)
  3329. {
  3330. struct tegra_xudc *xudc = dev_get_drvdata(dev);
  3331. return tegra_xudc_unpowergate(xudc);
  3332. }
  3333. static const struct dev_pm_ops tegra_xudc_pm_ops = {
  3334. SET_SYSTEM_SLEEP_PM_OPS(tegra_xudc_suspend, tegra_xudc_resume)
  3335. SET_RUNTIME_PM_OPS(tegra_xudc_runtime_suspend,
  3336. tegra_xudc_runtime_resume, NULL)
  3337. };
  3338. static struct platform_driver tegra_xudc_driver = {
  3339. .probe = tegra_xudc_probe,
  3340. .remove = tegra_xudc_remove,
  3341. .driver = {
  3342. .name = "tegra-xudc",
  3343. .pm = &tegra_xudc_pm_ops,
  3344. .of_match_table = tegra_xudc_of_match,
  3345. },
  3346. };
  3347. module_platform_driver(tegra_xudc_driver);
  3348. MODULE_DESCRIPTION("NVIDIA Tegra XUSB Device Controller");
  3349. MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
  3350. MODULE_AUTHOR("Hui Fu <hfu@nvidia.com>");
  3351. MODULE_AUTHOR("Nagarjuna Kristam <nkristam@nvidia.com>");
  3352. MODULE_LICENSE("GPL v2");