gadget.c 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112
  1. // SPDX-License-Identifier: GPL-2.0
  2. /**
  3. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * Copyright 2008 Openmoko, Inc.
  7. * Copyright 2008 Simtec Electronics
  8. * Ben Dooks <ben@simtec.co.uk>
  9. * http://armlinux.simtec.co.uk/
  10. *
  11. * S3C USB2.0 High-speed / OtG driver
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/mutex.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/delay.h>
  22. #include <linux/io.h>
  23. #include <linux/slab.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/usb/ch9.h>
  26. #include <linux/usb/gadget.h>
  27. #include <linux/usb/phy.h>
  28. #include "core.h"
  29. #include "hw.h"
  30. /* conversion functions */
  31. static inline struct dwc2_hsotg_req *our_req(struct usb_request *req)
  32. {
  33. return container_of(req, struct dwc2_hsotg_req, req);
  34. }
  35. static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep)
  36. {
  37. return container_of(ep, struct dwc2_hsotg_ep, ep);
  38. }
  39. static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
  40. {
  41. return container_of(gadget, struct dwc2_hsotg, gadget);
  42. }
  43. static inline void dwc2_set_bit(void __iomem *ptr, u32 val)
  44. {
  45. dwc2_writel(dwc2_readl(ptr) | val, ptr);
  46. }
  47. static inline void dwc2_clear_bit(void __iomem *ptr, u32 val)
  48. {
  49. dwc2_writel(dwc2_readl(ptr) & ~val, ptr);
  50. }
  51. static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
  52. u32 ep_index, u32 dir_in)
  53. {
  54. if (dir_in)
  55. return hsotg->eps_in[ep_index];
  56. else
  57. return hsotg->eps_out[ep_index];
  58. }
  59. /* forward declaration of functions */
  60. static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg);
  61. /**
  62. * using_dma - return the DMA status of the driver.
  63. * @hsotg: The driver state.
  64. *
  65. * Return true if we're using DMA.
  66. *
  67. * Currently, we have the DMA support code worked into everywhere
  68. * that needs it, but the AMBA DMA implementation in the hardware can
  69. * only DMA from 32bit aligned addresses. This means that gadgets such
  70. * as the CDC Ethernet cannot work as they often pass packets which are
  71. * not 32bit aligned.
  72. *
  73. * Unfortunately the choice to use DMA or not is global to the controller
  74. * and seems to be only settable when the controller is being put through
  75. * a core reset. This means we either need to fix the gadgets to take
  76. * account of DMA alignment, or add bounce buffers (yuerk).
  77. *
  78. * g_using_dma is set depending on dts flag.
  79. */
  80. static inline bool using_dma(struct dwc2_hsotg *hsotg)
  81. {
  82. return hsotg->params.g_dma;
  83. }
  84. /*
  85. * using_desc_dma - return the descriptor DMA status of the driver.
  86. * @hsotg: The driver state.
  87. *
  88. * Return true if we're using descriptor DMA.
  89. */
  90. static inline bool using_desc_dma(struct dwc2_hsotg *hsotg)
  91. {
  92. return hsotg->params.g_dma_desc;
  93. }
  94. /**
  95. * dwc2_gadget_incr_frame_num - Increments the targeted frame number.
  96. * @hs_ep: The endpoint
  97. * @increment: The value to increment by
  98. *
  99. * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT.
  100. * If an overrun occurs it will wrap the value and set the frame_overrun flag.
  101. */
  102. static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep)
  103. {
  104. hs_ep->target_frame += hs_ep->interval;
  105. if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) {
  106. hs_ep->frame_overrun = true;
  107. hs_ep->target_frame &= DSTS_SOFFN_LIMIT;
  108. } else {
  109. hs_ep->frame_overrun = false;
  110. }
  111. }
  112. /**
  113. * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
  114. * @hsotg: The device state
  115. * @ints: A bitmask of the interrupts to enable
  116. */
  117. static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
  118. {
  119. u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  120. u32 new_gsintmsk;
  121. new_gsintmsk = gsintmsk | ints;
  122. if (new_gsintmsk != gsintmsk) {
  123. dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
  124. dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
  125. }
  126. }
  127. /**
  128. * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
  129. * @hsotg: The device state
  130. * @ints: A bitmask of the interrupts to enable
  131. */
  132. static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
  133. {
  134. u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  135. u32 new_gsintmsk;
  136. new_gsintmsk = gsintmsk & ~ints;
  137. if (new_gsintmsk != gsintmsk)
  138. dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
  139. }
  140. /**
  141. * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
  142. * @hsotg: The device state
  143. * @ep: The endpoint index
  144. * @dir_in: True if direction is in.
  145. * @en: The enable value, true to enable
  146. *
  147. * Set or clear the mask for an individual endpoint's interrupt
  148. * request.
  149. */
  150. static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
  151. unsigned int ep, unsigned int dir_in,
  152. unsigned int en)
  153. {
  154. unsigned long flags;
  155. u32 bit = 1 << ep;
  156. u32 daint;
  157. if (!dir_in)
  158. bit <<= 16;
  159. local_irq_save(flags);
  160. daint = dwc2_readl(hsotg->regs + DAINTMSK);
  161. if (en)
  162. daint |= bit;
  163. else
  164. daint &= ~bit;
  165. dwc2_writel(daint, hsotg->regs + DAINTMSK);
  166. local_irq_restore(flags);
  167. }
  168. /**
  169. * dwc2_hsotg_tx_fifo_count - return count of TX FIFOs in device mode
  170. */
  171. int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg)
  172. {
  173. if (hsotg->hw_params.en_multiple_tx_fifo)
  174. /* In dedicated FIFO mode we need count of IN EPs */
  175. return hsotg->hw_params.num_dev_in_eps;
  176. else
  177. /* In shared FIFO mode we need count of Periodic IN EPs */
  178. return hsotg->hw_params.num_dev_perio_in_ep;
  179. }
  180. /**
  181. * dwc2_hsotg_tx_fifo_total_depth - return total FIFO depth available for
  182. * device mode TX FIFOs
  183. */
  184. int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg)
  185. {
  186. int addr;
  187. int tx_addr_max;
  188. u32 np_tx_fifo_size;
  189. np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size,
  190. hsotg->params.g_np_tx_fifo_size);
  191. /* Get Endpoint Info Control block size in DWORDs. */
  192. tx_addr_max = hsotg->hw_params.total_fifo_size;
  193. addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size;
  194. if (tx_addr_max <= addr)
  195. return 0;
  196. return tx_addr_max - addr;
  197. }
  198. /**
  199. * dwc2_hsotg_tx_fifo_average_depth - returns average depth of device mode
  200. * TX FIFOs
  201. */
  202. int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg)
  203. {
  204. int tx_fifo_count;
  205. int tx_fifo_depth;
  206. tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg);
  207. tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
  208. if (!tx_fifo_count)
  209. return tx_fifo_depth;
  210. else
  211. return tx_fifo_depth / tx_fifo_count;
  212. }
  213. /**
  214. * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
  215. * @hsotg: The device instance.
  216. */
  217. static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
  218. {
  219. unsigned int ep;
  220. unsigned int addr;
  221. int timeout;
  222. u32 val;
  223. u32 *txfsz = hsotg->params.g_tx_fifo_size;
  224. /* Reset fifo map if not correctly cleared during previous session */
  225. WARN_ON(hsotg->fifo_map);
  226. hsotg->fifo_map = 0;
  227. /* set RX/NPTX FIFO sizes */
  228. dwc2_writel(hsotg->params.g_rx_fifo_size, hsotg->regs + GRXFSIZ);
  229. dwc2_writel((hsotg->params.g_rx_fifo_size << FIFOSIZE_STARTADDR_SHIFT) |
  230. (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT),
  231. hsotg->regs + GNPTXFSIZ);
  232. /*
  233. * arange all the rest of the TX FIFOs, as some versions of this
  234. * block have overlapping default addresses. This also ensures
  235. * that if the settings have been changed, then they are set to
  236. * known values.
  237. */
  238. /* start at the end of the GNPTXFSIZ, rounded up */
  239. addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size;
  240. /*
  241. * Configure fifos sizes from provided configuration and assign
  242. * them to endpoints dynamically according to maxpacket size value of
  243. * given endpoint.
  244. */
  245. for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
  246. if (!txfsz[ep])
  247. continue;
  248. val = addr;
  249. val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT;
  250. WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem,
  251. "insufficient fifo memory");
  252. addr += txfsz[ep];
  253. dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep));
  254. val = dwc2_readl(hsotg->regs + DPTXFSIZN(ep));
  255. }
  256. dwc2_writel(hsotg->hw_params.total_fifo_size |
  257. addr << GDFIFOCFG_EPINFOBASE_SHIFT,
  258. hsotg->regs + GDFIFOCFG);
  259. /*
  260. * according to p428 of the design guide, we need to ensure that
  261. * all fifos are flushed before continuing
  262. */
  263. dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
  264. GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
  265. /* wait until the fifos are both flushed */
  266. timeout = 100;
  267. while (1) {
  268. val = dwc2_readl(hsotg->regs + GRSTCTL);
  269. if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
  270. break;
  271. if (--timeout == 0) {
  272. dev_err(hsotg->dev,
  273. "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
  274. __func__, val);
  275. break;
  276. }
  277. udelay(1);
  278. }
  279. dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
  280. }
  281. /**
  282. * @ep: USB endpoint to allocate request for.
  283. * @flags: Allocation flags
  284. *
  285. * Allocate a new USB request structure appropriate for the specified endpoint
  286. */
  287. static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
  288. gfp_t flags)
  289. {
  290. struct dwc2_hsotg_req *req;
  291. req = kzalloc(sizeof(*req), flags);
  292. if (!req)
  293. return NULL;
  294. INIT_LIST_HEAD(&req->queue);
  295. return &req->req;
  296. }
  297. /**
  298. * is_ep_periodic - return true if the endpoint is in periodic mode.
  299. * @hs_ep: The endpoint to query.
  300. *
  301. * Returns true if the endpoint is in periodic mode, meaning it is being
  302. * used for an Interrupt or ISO transfer.
  303. */
  304. static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
  305. {
  306. return hs_ep->periodic;
  307. }
  308. /**
  309. * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
  310. * @hsotg: The device state.
  311. * @hs_ep: The endpoint for the request
  312. * @hs_req: The request being processed.
  313. *
  314. * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
  315. * of a request to ensure the buffer is ready for access by the caller.
  316. */
  317. static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
  318. struct dwc2_hsotg_ep *hs_ep,
  319. struct dwc2_hsotg_req *hs_req)
  320. {
  321. struct usb_request *req = &hs_req->req;
  322. usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
  323. }
  324. /*
  325. * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains
  326. * for Control endpoint
  327. * @hsotg: The device state.
  328. *
  329. * This function will allocate 4 descriptor chains for EP 0: 2 for
  330. * Setup stage, per one for IN and OUT data/status transactions.
  331. */
  332. static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg)
  333. {
  334. hsotg->setup_desc[0] =
  335. dmam_alloc_coherent(hsotg->dev,
  336. sizeof(struct dwc2_dma_desc),
  337. &hsotg->setup_desc_dma[0],
  338. GFP_KERNEL);
  339. if (!hsotg->setup_desc[0])
  340. goto fail;
  341. hsotg->setup_desc[1] =
  342. dmam_alloc_coherent(hsotg->dev,
  343. sizeof(struct dwc2_dma_desc),
  344. &hsotg->setup_desc_dma[1],
  345. GFP_KERNEL);
  346. if (!hsotg->setup_desc[1])
  347. goto fail;
  348. hsotg->ctrl_in_desc =
  349. dmam_alloc_coherent(hsotg->dev,
  350. sizeof(struct dwc2_dma_desc),
  351. &hsotg->ctrl_in_desc_dma,
  352. GFP_KERNEL);
  353. if (!hsotg->ctrl_in_desc)
  354. goto fail;
  355. hsotg->ctrl_out_desc =
  356. dmam_alloc_coherent(hsotg->dev,
  357. sizeof(struct dwc2_dma_desc),
  358. &hsotg->ctrl_out_desc_dma,
  359. GFP_KERNEL);
  360. if (!hsotg->ctrl_out_desc)
  361. goto fail;
  362. return 0;
  363. fail:
  364. return -ENOMEM;
  365. }
  366. /**
  367. * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
  368. * @hsotg: The controller state.
  369. * @hs_ep: The endpoint we're going to write for.
  370. * @hs_req: The request to write data for.
  371. *
  372. * This is called when the TxFIFO has some space in it to hold a new
  373. * transmission and we have something to give it. The actual setup of
  374. * the data size is done elsewhere, so all we have to do is to actually
  375. * write the data.
  376. *
  377. * The return value is zero if there is more space (or nothing was done)
  378. * otherwise -ENOSPC is returned if the FIFO space was used up.
  379. *
  380. * This routine is only needed for PIO
  381. */
  382. static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
  383. struct dwc2_hsotg_ep *hs_ep,
  384. struct dwc2_hsotg_req *hs_req)
  385. {
  386. bool periodic = is_ep_periodic(hs_ep);
  387. u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
  388. int buf_pos = hs_req->req.actual;
  389. int to_write = hs_ep->size_loaded;
  390. void *data;
  391. int can_write;
  392. int pkt_round;
  393. int max_transfer;
  394. to_write -= (buf_pos - hs_ep->last_load);
  395. /* if there's nothing to write, get out early */
  396. if (to_write == 0)
  397. return 0;
  398. if (periodic && !hsotg->dedicated_fifos) {
  399. u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
  400. int size_left;
  401. int size_done;
  402. /*
  403. * work out how much data was loaded so we can calculate
  404. * how much data is left in the fifo.
  405. */
  406. size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  407. /*
  408. * if shared fifo, we cannot write anything until the
  409. * previous data has been completely sent.
  410. */
  411. if (hs_ep->fifo_load != 0) {
  412. dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
  413. return -ENOSPC;
  414. }
  415. dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
  416. __func__, size_left,
  417. hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
  418. /* how much of the data has moved */
  419. size_done = hs_ep->size_loaded - size_left;
  420. /* how much data is left in the fifo */
  421. can_write = hs_ep->fifo_load - size_done;
  422. dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
  423. __func__, can_write);
  424. can_write = hs_ep->fifo_size - can_write;
  425. dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
  426. __func__, can_write);
  427. if (can_write <= 0) {
  428. dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
  429. return -ENOSPC;
  430. }
  431. } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
  432. can_write = dwc2_readl(hsotg->regs +
  433. DTXFSTS(hs_ep->fifo_index));
  434. can_write &= 0xffff;
  435. can_write *= 4;
  436. } else {
  437. if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
  438. dev_dbg(hsotg->dev,
  439. "%s: no queue slots available (0x%08x)\n",
  440. __func__, gnptxsts);
  441. dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
  442. return -ENOSPC;
  443. }
  444. can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
  445. can_write *= 4; /* fifo size is in 32bit quantities. */
  446. }
  447. max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
  448. dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
  449. __func__, gnptxsts, can_write, to_write, max_transfer);
  450. /*
  451. * limit to 512 bytes of data, it seems at least on the non-periodic
  452. * FIFO, requests of >512 cause the endpoint to get stuck with a
  453. * fragment of the end of the transfer in it.
  454. */
  455. if (can_write > 512 && !periodic)
  456. can_write = 512;
  457. /*
  458. * limit the write to one max-packet size worth of data, but allow
  459. * the transfer to return that it did not run out of fifo space
  460. * doing it.
  461. */
  462. if (to_write > max_transfer) {
  463. to_write = max_transfer;
  464. /* it's needed only when we do not use dedicated fifos */
  465. if (!hsotg->dedicated_fifos)
  466. dwc2_hsotg_en_gsint(hsotg,
  467. periodic ? GINTSTS_PTXFEMP :
  468. GINTSTS_NPTXFEMP);
  469. }
  470. /* see if we can write data */
  471. if (to_write > can_write) {
  472. to_write = can_write;
  473. pkt_round = to_write % max_transfer;
  474. /*
  475. * Round the write down to an
  476. * exact number of packets.
  477. *
  478. * Note, we do not currently check to see if we can ever
  479. * write a full packet or not to the FIFO.
  480. */
  481. if (pkt_round)
  482. to_write -= pkt_round;
  483. /*
  484. * enable correct FIFO interrupt to alert us when there
  485. * is more room left.
  486. */
  487. /* it's needed only when we do not use dedicated fifos */
  488. if (!hsotg->dedicated_fifos)
  489. dwc2_hsotg_en_gsint(hsotg,
  490. periodic ? GINTSTS_PTXFEMP :
  491. GINTSTS_NPTXFEMP);
  492. }
  493. dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
  494. to_write, hs_req->req.length, can_write, buf_pos);
  495. if (to_write <= 0)
  496. return -ENOSPC;
  497. hs_req->req.actual = buf_pos + to_write;
  498. hs_ep->total_data += to_write;
  499. if (periodic)
  500. hs_ep->fifo_load += to_write;
  501. to_write = DIV_ROUND_UP(to_write, 4);
  502. data = hs_req->req.buf + buf_pos;
  503. iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
  504. return (to_write >= can_write) ? -ENOSPC : 0;
  505. }
  506. /**
  507. * get_ep_limit - get the maximum data legnth for this endpoint
  508. * @hs_ep: The endpoint
  509. *
  510. * Return the maximum data that can be queued in one go on a given endpoint
  511. * so that transfers that are too long can be split.
  512. */
  513. static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
  514. {
  515. int index = hs_ep->index;
  516. unsigned int maxsize;
  517. unsigned int maxpkt;
  518. if (index != 0) {
  519. maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
  520. maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
  521. } else {
  522. maxsize = 64 + 64;
  523. if (hs_ep->dir_in)
  524. maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
  525. else
  526. maxpkt = 2;
  527. }
  528. /* we made the constant loading easier above by using +1 */
  529. maxpkt--;
  530. maxsize--;
  531. /*
  532. * constrain by packet count if maxpkts*pktsize is greater
  533. * than the length register size.
  534. */
  535. if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
  536. maxsize = maxpkt * hs_ep->ep.maxpacket;
  537. return maxsize;
  538. }
  539. /**
  540. * dwc2_hsotg_read_frameno - read current frame number
  541. * @hsotg: The device instance
  542. *
  543. * Return the current frame number
  544. */
  545. static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
  546. {
  547. u32 dsts;
  548. dsts = dwc2_readl(hsotg->regs + DSTS);
  549. dsts &= DSTS_SOFFN_MASK;
  550. dsts >>= DSTS_SOFFN_SHIFT;
  551. return dsts;
  552. }
  553. /**
  554. * dwc2_gadget_get_chain_limit - get the maximum data payload value of the
  555. * DMA descriptor chain prepared for specific endpoint
  556. * @hs_ep: The endpoint
  557. *
  558. * Return the maximum data that can be queued in one go on a given endpoint
  559. * depending on its descriptor chain capacity so that transfers that
  560. * are too long can be split.
  561. */
  562. static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep)
  563. {
  564. int is_isoc = hs_ep->isochronous;
  565. unsigned int maxsize;
  566. if (is_isoc)
  567. maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT :
  568. DEV_DMA_ISOC_RX_NBYTES_LIMIT;
  569. else
  570. maxsize = DEV_DMA_NBYTES_LIMIT;
  571. /* Above size of one descriptor was chosen, multiple it */
  572. maxsize *= MAX_DMA_DESC_NUM_GENERIC;
  573. return maxsize;
  574. }
  575. /*
  576. * dwc2_gadget_get_desc_params - get DMA descriptor parameters.
  577. * @hs_ep: The endpoint
  578. * @mask: RX/TX bytes mask to be defined
  579. *
  580. * Returns maximum data payload for one descriptor after analyzing endpoint
  581. * characteristics.
  582. * DMA descriptor transfer bytes limit depends on EP type:
  583. * Control out - MPS,
  584. * Isochronous - descriptor rx/tx bytes bitfield limit,
  585. * Control In/Bulk/Interrupt - multiple of mps. This will allow to not
  586. * have concatenations from various descriptors within one packet.
  587. *
  588. * Selects corresponding mask for RX/TX bytes as well.
  589. */
  590. static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask)
  591. {
  592. u32 mps = hs_ep->ep.maxpacket;
  593. int dir_in = hs_ep->dir_in;
  594. u32 desc_size = 0;
  595. if (!hs_ep->index && !dir_in) {
  596. desc_size = mps;
  597. *mask = DEV_DMA_NBYTES_MASK;
  598. } else if (hs_ep->isochronous) {
  599. if (dir_in) {
  600. desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT;
  601. *mask = DEV_DMA_ISOC_TX_NBYTES_MASK;
  602. } else {
  603. desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT;
  604. *mask = DEV_DMA_ISOC_RX_NBYTES_MASK;
  605. }
  606. } else {
  607. desc_size = DEV_DMA_NBYTES_LIMIT;
  608. *mask = DEV_DMA_NBYTES_MASK;
  609. /* Round down desc_size to be mps multiple */
  610. desc_size -= desc_size % mps;
  611. }
  612. return desc_size;
  613. }
  614. /*
  615. * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
  616. * @hs_ep: The endpoint
  617. * @dma_buff: DMA address to use
  618. * @len: Length of the transfer
  619. *
  620. * This function will iterate over descriptor chain and fill its entries
  621. * with corresponding information based on transfer data.
  622. */
  623. static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
  624. dma_addr_t dma_buff,
  625. unsigned int len)
  626. {
  627. struct dwc2_hsotg *hsotg = hs_ep->parent;
  628. int dir_in = hs_ep->dir_in;
  629. struct dwc2_dma_desc *desc = hs_ep->desc_list;
  630. u32 mps = hs_ep->ep.maxpacket;
  631. u32 maxsize = 0;
  632. u32 offset = 0;
  633. u32 mask = 0;
  634. int i;
  635. maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
  636. hs_ep->desc_count = (len / maxsize) +
  637. ((len % maxsize) ? 1 : 0);
  638. if (len == 0)
  639. hs_ep->desc_count = 1;
  640. for (i = 0; i < hs_ep->desc_count; ++i) {
  641. desc->status = 0;
  642. desc->status |= (DEV_DMA_BUFF_STS_HBUSY
  643. << DEV_DMA_BUFF_STS_SHIFT);
  644. if (len > maxsize) {
  645. if (!hs_ep->index && !dir_in)
  646. desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
  647. desc->status |= (maxsize <<
  648. DEV_DMA_NBYTES_SHIFT & mask);
  649. desc->buf = dma_buff + offset;
  650. len -= maxsize;
  651. offset += maxsize;
  652. } else {
  653. desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
  654. if (dir_in)
  655. desc->status |= (len % mps) ? DEV_DMA_SHORT :
  656. ((hs_ep->send_zlp) ? DEV_DMA_SHORT : 0);
  657. if (len > maxsize)
  658. dev_err(hsotg->dev, "wrong len %d\n", len);
  659. desc->status |=
  660. len << DEV_DMA_NBYTES_SHIFT & mask;
  661. desc->buf = dma_buff + offset;
  662. }
  663. desc->status &= ~DEV_DMA_BUFF_STS_MASK;
  664. desc->status |= (DEV_DMA_BUFF_STS_HREADY
  665. << DEV_DMA_BUFF_STS_SHIFT);
  666. desc++;
  667. }
  668. }
  669. /*
  670. * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain.
  671. * @hs_ep: The isochronous endpoint.
  672. * @dma_buff: usb requests dma buffer.
  673. * @len: usb request transfer length.
  674. *
  675. * Finds out index of first free entry either in the bottom or up half of
  676. * descriptor chain depend on which is under SW control and not processed
  677. * by HW. Then fills that descriptor with the data of the arrived usb request,
  678. * frame info, sets Last and IOC bits increments next_desc. If filled
  679. * descriptor is not the first one, removes L bit from the previous descriptor
  680. * status.
  681. */
  682. static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
  683. dma_addr_t dma_buff, unsigned int len)
  684. {
  685. struct dwc2_dma_desc *desc;
  686. struct dwc2_hsotg *hsotg = hs_ep->parent;
  687. u32 index;
  688. u32 maxsize = 0;
  689. u32 mask = 0;
  690. maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
  691. if (len > maxsize) {
  692. dev_err(hsotg->dev, "wrong len %d\n", len);
  693. return -EINVAL;
  694. }
  695. /*
  696. * If SW has already filled half of chain, then return and wait for
  697. * the other chain to be processed by HW.
  698. */
  699. if (hs_ep->next_desc == MAX_DMA_DESC_NUM_GENERIC / 2)
  700. return -EBUSY;
  701. /* Increment frame number by interval for IN */
  702. if (hs_ep->dir_in)
  703. dwc2_gadget_incr_frame_num(hs_ep);
  704. index = (MAX_DMA_DESC_NUM_GENERIC / 2) * hs_ep->isoc_chain_num +
  705. hs_ep->next_desc;
  706. /* Sanity check of calculated index */
  707. if ((hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC) ||
  708. (!hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC / 2)) {
  709. dev_err(hsotg->dev, "wrong index %d for iso chain\n", index);
  710. return -EINVAL;
  711. }
  712. desc = &hs_ep->desc_list[index];
  713. /* Clear L bit of previous desc if more than one entries in the chain */
  714. if (hs_ep->next_desc)
  715. hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L;
  716. dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
  717. __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);
  718. desc->status = 0;
  719. desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
  720. desc->buf = dma_buff;
  721. desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
  722. ((len << DEV_DMA_NBYTES_SHIFT) & mask));
  723. if (hs_ep->dir_in) {
  724. desc->status |= ((hs_ep->mc << DEV_DMA_ISOC_PID_SHIFT) &
  725. DEV_DMA_ISOC_PID_MASK) |
  726. ((len % hs_ep->ep.maxpacket) ?
  727. DEV_DMA_SHORT : 0) |
  728. ((hs_ep->target_frame <<
  729. DEV_DMA_ISOC_FRNUM_SHIFT) &
  730. DEV_DMA_ISOC_FRNUM_MASK);
  731. }
  732. desc->status &= ~DEV_DMA_BUFF_STS_MASK;
  733. desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
  734. /* Update index of last configured entry in the chain */
  735. hs_ep->next_desc++;
  736. return 0;
  737. }
  738. /*
  739. * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
  740. * @hs_ep: The isochronous endpoint.
  741. *
  742. * Prepare first descriptor chain for isochronous endpoints. Afterwards
  743. * write DMA address to HW and enable the endpoint.
  744. *
  745. * Switch between descriptor chains via isoc_chain_num to give SW opportunity
  746. * to prepare second descriptor chain while first one is being processed by HW.
  747. */
  748. static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
  749. {
  750. struct dwc2_hsotg *hsotg = hs_ep->parent;
  751. struct dwc2_hsotg_req *hs_req, *treq;
  752. int index = hs_ep->index;
  753. int ret;
  754. u32 dma_reg;
  755. u32 depctl;
  756. u32 ctrl;
  757. if (list_empty(&hs_ep->queue)) {
  758. dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
  759. return;
  760. }
  761. list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
  762. ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
  763. hs_req->req.length);
  764. if (ret) {
  765. dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
  766. break;
  767. }
  768. }
  769. depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
  770. dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
  771. /* write descriptor chain address to control register */
  772. dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
  773. ctrl = dwc2_readl(hsotg->regs + depctl);
  774. ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
  775. dwc2_writel(ctrl, hsotg->regs + depctl);
  776. /* Switch ISOC descriptor chain number being processed by SW*/
  777. hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1;
  778. hs_ep->next_desc = 0;
  779. }
  780. /**
  781. * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
  782. * @hsotg: The controller state.
  783. * @hs_ep: The endpoint to process a request for
  784. * @hs_req: The request to start.
  785. * @continuing: True if we are doing more for the current request.
  786. *
  787. * Start the given request running by setting the endpoint registers
  788. * appropriately, and writing any data to the FIFOs.
  789. */
  790. static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
  791. struct dwc2_hsotg_ep *hs_ep,
  792. struct dwc2_hsotg_req *hs_req,
  793. bool continuing)
  794. {
  795. struct usb_request *ureq = &hs_req->req;
  796. int index = hs_ep->index;
  797. int dir_in = hs_ep->dir_in;
  798. u32 epctrl_reg;
  799. u32 epsize_reg;
  800. u32 epsize;
  801. u32 ctrl;
  802. unsigned int length;
  803. unsigned int packets;
  804. unsigned int maxreq;
  805. unsigned int dma_reg;
  806. if (index != 0) {
  807. if (hs_ep->req && !continuing) {
  808. dev_err(hsotg->dev, "%s: active request\n", __func__);
  809. WARN_ON(1);
  810. return;
  811. } else if (hs_ep->req != hs_req && continuing) {
  812. dev_err(hsotg->dev,
  813. "%s: continue different req\n", __func__);
  814. WARN_ON(1);
  815. return;
  816. }
  817. }
  818. dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
  819. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  820. epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
  821. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
  822. __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
  823. hs_ep->dir_in ? "in" : "out");
  824. /* If endpoint is stalled, we will restart request later */
  825. ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
  826. if (index && ctrl & DXEPCTL_STALL) {
  827. dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
  828. return;
  829. }
  830. length = ureq->length - ureq->actual;
  831. dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
  832. ureq->length, ureq->actual);
  833. if (!using_desc_dma(hsotg))
  834. maxreq = get_ep_limit(hs_ep);
  835. else
  836. maxreq = dwc2_gadget_get_chain_limit(hs_ep);
  837. if (length > maxreq) {
  838. int round = maxreq % hs_ep->ep.maxpacket;
  839. dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
  840. __func__, length, maxreq, round);
  841. /* round down to multiple of packets */
  842. if (round)
  843. maxreq -= round;
  844. length = maxreq;
  845. }
  846. if (length)
  847. packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
  848. else
  849. packets = 1; /* send one packet if length is zero. */
  850. if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
  851. dev_err(hsotg->dev, "req length > maxpacket*mc\n");
  852. return;
  853. }
  854. if (dir_in && index != 0)
  855. if (hs_ep->isochronous)
  856. epsize = DXEPTSIZ_MC(packets);
  857. else
  858. epsize = DXEPTSIZ_MC(1);
  859. else
  860. epsize = 0;
  861. /*
  862. * zero length packet should be programmed on its own and should not
  863. * be counted in DIEPTSIZ.PktCnt with other packets.
  864. */
  865. if (dir_in && ureq->zero && !continuing) {
  866. /* Test if zlp is actually required. */
  867. if ((ureq->length >= hs_ep->ep.maxpacket) &&
  868. !(ureq->length % hs_ep->ep.maxpacket))
  869. hs_ep->send_zlp = 1;
  870. }
  871. epsize |= DXEPTSIZ_PKTCNT(packets);
  872. epsize |= DXEPTSIZ_XFERSIZE(length);
  873. dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
  874. __func__, packets, length, ureq->length, epsize, epsize_reg);
  875. /* store the request as the current one we're doing */
  876. hs_ep->req = hs_req;
  877. if (using_desc_dma(hsotg)) {
  878. u32 offset = 0;
  879. u32 mps = hs_ep->ep.maxpacket;
  880. /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */
  881. if (!dir_in) {
  882. if (!index)
  883. length = mps;
  884. else if (length % mps)
  885. length += (mps - (length % mps));
  886. }
  887. /*
  888. * If more data to send, adjust DMA for EP0 out data stage.
  889. * ureq->dma stays unchanged, hence increment it by already
  890. * passed passed data count before starting new transaction.
  891. */
  892. if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT &&
  893. continuing)
  894. offset = ureq->actual;
  895. /* Fill DDMA chain entries */
  896. dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset,
  897. length);
  898. /* write descriptor chain address to control register */
  899. dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
  900. dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
  901. __func__, (u32)hs_ep->desc_list_dma, dma_reg);
  902. } else {
  903. /* write size / packets */
  904. dwc2_writel(epsize, hsotg->regs + epsize_reg);
  905. if (using_dma(hsotg) && !continuing && (length != 0)) {
  906. /*
  907. * write DMA address to control register, buffer
  908. * already synced by dwc2_hsotg_ep_queue().
  909. */
  910. dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
  911. dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
  912. __func__, &ureq->dma, dma_reg);
  913. }
  914. }
  915. if (hs_ep->isochronous && hs_ep->interval == 1) {
  916. hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
  917. dwc2_gadget_incr_frame_num(hs_ep);
  918. if (hs_ep->target_frame & 0x1)
  919. ctrl |= DXEPCTL_SETODDFR;
  920. else
  921. ctrl |= DXEPCTL_SETEVENFR;
  922. }
  923. ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
  924. dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
  925. /* For Setup request do not clear NAK */
  926. if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
  927. ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
  928. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
  929. dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
  930. /*
  931. * set these, it seems that DMA support increments past the end
  932. * of the packet buffer so we need to calculate the length from
  933. * this information.
  934. */
  935. hs_ep->size_loaded = length;
  936. hs_ep->last_load = ureq->actual;
  937. if (dir_in && !using_dma(hsotg)) {
  938. /* set these anyway, we may need them for non-periodic in */
  939. hs_ep->fifo_load = 0;
  940. dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
  941. }
  942. /*
  943. * Note, trying to clear the NAK here causes problems with transmit
  944. * on the S3C6400 ending up with the TXFIFO becoming full.
  945. */
  946. /* check ep is enabled */
  947. if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
  948. dev_dbg(hsotg->dev,
  949. "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
  950. index, dwc2_readl(hsotg->regs + epctrl_reg));
  951. dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
  952. __func__, dwc2_readl(hsotg->regs + epctrl_reg));
  953. /* enable ep interrupts */
  954. dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
  955. }
  956. /**
  957. * dwc2_hsotg_map_dma - map the DMA memory being used for the request
  958. * @hsotg: The device state.
  959. * @hs_ep: The endpoint the request is on.
  960. * @req: The request being processed.
  961. *
  962. * We've been asked to queue a request, so ensure that the memory buffer
  963. * is correctly setup for DMA. If we've been passed an extant DMA address
  964. * then ensure the buffer has been synced to memory. If our buffer has no
  965. * DMA memory, then we map the memory and mark our request to allow us to
  966. * cleanup on completion.
  967. */
  968. static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
  969. struct dwc2_hsotg_ep *hs_ep,
  970. struct usb_request *req)
  971. {
  972. int ret;
  973. ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
  974. if (ret)
  975. goto dma_error;
  976. return 0;
  977. dma_error:
  978. dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
  979. __func__, req->buf, req->length);
  980. return -EIO;
  981. }
  982. static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
  983. struct dwc2_hsotg_ep *hs_ep,
  984. struct dwc2_hsotg_req *hs_req)
  985. {
  986. void *req_buf = hs_req->req.buf;
  987. /* If dma is not being used or buffer is aligned */
  988. if (!using_dma(hsotg) || !((long)req_buf & 3))
  989. return 0;
  990. WARN_ON(hs_req->saved_req_buf);
  991. dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
  992. hs_ep->ep.name, req_buf, hs_req->req.length);
  993. hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
  994. if (!hs_req->req.buf) {
  995. hs_req->req.buf = req_buf;
  996. dev_err(hsotg->dev,
  997. "%s: unable to allocate memory for bounce buffer\n",
  998. __func__);
  999. return -ENOMEM;
  1000. }
  1001. /* Save actual buffer */
  1002. hs_req->saved_req_buf = req_buf;
  1003. if (hs_ep->dir_in)
  1004. memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
  1005. return 0;
  1006. }
  1007. static void
  1008. dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
  1009. struct dwc2_hsotg_ep *hs_ep,
  1010. struct dwc2_hsotg_req *hs_req)
  1011. {
  1012. /* If dma is not being used or buffer was aligned */
  1013. if (!using_dma(hsotg) || !hs_req->saved_req_buf)
  1014. return;
  1015. dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
  1016. hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
  1017. /* Copy data from bounce buffer on successful out transfer */
  1018. if (!hs_ep->dir_in && !hs_req->req.status)
  1019. memcpy(hs_req->saved_req_buf, hs_req->req.buf,
  1020. hs_req->req.actual);
  1021. /* Free bounce buffer */
  1022. kfree(hs_req->req.buf);
  1023. hs_req->req.buf = hs_req->saved_req_buf;
  1024. hs_req->saved_req_buf = NULL;
  1025. }
  1026. /**
  1027. * dwc2_gadget_target_frame_elapsed - Checks target frame
  1028. * @hs_ep: The driver endpoint to check
  1029. *
  1030. * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
  1031. * corresponding transfer.
  1032. */
  1033. static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
  1034. {
  1035. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1036. u32 target_frame = hs_ep->target_frame;
  1037. u32 current_frame = dwc2_hsotg_read_frameno(hsotg);
  1038. bool frame_overrun = hs_ep->frame_overrun;
  1039. if (!frame_overrun && current_frame >= target_frame)
  1040. return true;
  1041. if (frame_overrun && current_frame >= target_frame &&
  1042. ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
  1043. return true;
  1044. return false;
  1045. }
  1046. /*
  1047. * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
  1048. * @hsotg: The driver state
  1049. * @hs_ep: the ep descriptor chain is for
  1050. *
  1051. * Called to update EP0 structure's pointers depend on stage of
  1052. * control transfer.
  1053. */
  1054. static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
  1055. struct dwc2_hsotg_ep *hs_ep)
  1056. {
  1057. switch (hsotg->ep0_state) {
  1058. case DWC2_EP0_SETUP:
  1059. case DWC2_EP0_STATUS_OUT:
  1060. hs_ep->desc_list = hsotg->setup_desc[0];
  1061. hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
  1062. break;
  1063. case DWC2_EP0_DATA_IN:
  1064. case DWC2_EP0_STATUS_IN:
  1065. hs_ep->desc_list = hsotg->ctrl_in_desc;
  1066. hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
  1067. break;
  1068. case DWC2_EP0_DATA_OUT:
  1069. hs_ep->desc_list = hsotg->ctrl_out_desc;
  1070. hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
  1071. break;
  1072. default:
  1073. dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
  1074. hsotg->ep0_state);
  1075. return -EINVAL;
  1076. }
  1077. return 0;
  1078. }
  1079. static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
  1080. gfp_t gfp_flags)
  1081. {
  1082. struct dwc2_hsotg_req *hs_req = our_req(req);
  1083. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1084. struct dwc2_hsotg *hs = hs_ep->parent;
  1085. bool first;
  1086. int ret;
  1087. dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
  1088. ep->name, req, req->length, req->buf, req->no_interrupt,
  1089. req->zero, req->short_not_ok);
  1090. /* Prevent new request submission when controller is suspended */
  1091. if (hs->lx_state != DWC2_L0) {
  1092. dev_dbg(hs->dev, "%s: submit request only in active state\n",
  1093. __func__);
  1094. return -EAGAIN;
  1095. }
  1096. /* initialise status of the request */
  1097. INIT_LIST_HEAD(&hs_req->queue);
  1098. req->actual = 0;
  1099. req->status = -EINPROGRESS;
  1100. ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
  1101. if (ret)
  1102. return ret;
  1103. /* if we're using DMA, sync the buffers as necessary */
  1104. if (using_dma(hs)) {
  1105. ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
  1106. if (ret)
  1107. return ret;
  1108. }
  1109. /* If using descriptor DMA configure EP0 descriptor chain pointers */
  1110. if (using_desc_dma(hs) && !hs_ep->index) {
  1111. ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
  1112. if (ret)
  1113. return ret;
  1114. }
  1115. first = list_empty(&hs_ep->queue);
  1116. list_add_tail(&hs_req->queue, &hs_ep->queue);
  1117. /*
  1118. * Handle DDMA isochronous transfers separately - just add new entry
  1119. * to the half of descriptor chain that is not processed by HW.
  1120. * Transfer will be started once SW gets either one of NAK or
  1121. * OutTknEpDis interrupts.
  1122. */
  1123. if (using_desc_dma(hs) && hs_ep->isochronous &&
  1124. hs_ep->target_frame != TARGET_FRAME_INITIAL) {
  1125. ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
  1126. hs_req->req.length);
  1127. if (ret)
  1128. dev_dbg(hs->dev, "%s: ISO desc chain full\n", __func__);
  1129. return 0;
  1130. }
  1131. if (first) {
  1132. if (!hs_ep->isochronous) {
  1133. dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
  1134. return 0;
  1135. }
  1136. while (dwc2_gadget_target_frame_elapsed(hs_ep))
  1137. dwc2_gadget_incr_frame_num(hs_ep);
  1138. if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
  1139. dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
  1140. }
  1141. return 0;
  1142. }
  1143. static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
  1144. gfp_t gfp_flags)
  1145. {
  1146. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1147. struct dwc2_hsotg *hs = hs_ep->parent;
  1148. unsigned long flags = 0;
  1149. int ret = 0;
  1150. spin_lock_irqsave(&hs->lock, flags);
  1151. ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
  1152. spin_unlock_irqrestore(&hs->lock, flags);
  1153. return ret;
  1154. }
  1155. static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
  1156. struct usb_request *req)
  1157. {
  1158. struct dwc2_hsotg_req *hs_req = our_req(req);
  1159. kfree(hs_req);
  1160. }
  1161. /**
  1162. * dwc2_hsotg_complete_oursetup - setup completion callback
  1163. * @ep: The endpoint the request was on.
  1164. * @req: The request completed.
  1165. *
  1166. * Called on completion of any requests the driver itself
  1167. * submitted that need cleaning up.
  1168. */
  1169. static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
  1170. struct usb_request *req)
  1171. {
  1172. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1173. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1174. dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
  1175. dwc2_hsotg_ep_free_request(ep, req);
  1176. }
  1177. /**
  1178. * ep_from_windex - convert control wIndex value to endpoint
  1179. * @hsotg: The driver state.
  1180. * @windex: The control request wIndex field (in host order).
  1181. *
  1182. * Convert the given wIndex into a pointer to an driver endpoint
  1183. * structure, or return NULL if it is not a valid endpoint.
  1184. */
  1185. static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
  1186. u32 windex)
  1187. {
  1188. struct dwc2_hsotg_ep *ep;
  1189. int dir = (windex & USB_DIR_IN) ? 1 : 0;
  1190. int idx = windex & 0x7F;
  1191. if (windex >= 0x100)
  1192. return NULL;
  1193. if (idx > hsotg->num_of_eps)
  1194. return NULL;
  1195. ep = index_to_ep(hsotg, idx, dir);
  1196. if (idx && ep->dir_in != dir)
  1197. return NULL;
  1198. return ep;
  1199. }
  1200. /**
  1201. * dwc2_hsotg_set_test_mode - Enable usb Test Modes
  1202. * @hsotg: The driver state.
  1203. * @testmode: requested usb test mode
  1204. * Enable usb Test Mode requested by the Host.
  1205. */
  1206. int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
  1207. {
  1208. int dctl = dwc2_readl(hsotg->regs + DCTL);
  1209. dctl &= ~DCTL_TSTCTL_MASK;
  1210. switch (testmode) {
  1211. case TEST_J:
  1212. case TEST_K:
  1213. case TEST_SE0_NAK:
  1214. case TEST_PACKET:
  1215. case TEST_FORCE_EN:
  1216. dctl |= testmode << DCTL_TSTCTL_SHIFT;
  1217. break;
  1218. default:
  1219. return -EINVAL;
  1220. }
  1221. dwc2_writel(dctl, hsotg->regs + DCTL);
  1222. return 0;
  1223. }
  1224. /**
  1225. * dwc2_hsotg_send_reply - send reply to control request
  1226. * @hsotg: The device state
  1227. * @ep: Endpoint 0
  1228. * @buff: Buffer for request
  1229. * @length: Length of reply.
  1230. *
  1231. * Create a request and queue it on the given endpoint. This is useful as
  1232. * an internal method of sending replies to certain control requests, etc.
  1233. */
  1234. static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
  1235. struct dwc2_hsotg_ep *ep,
  1236. void *buff,
  1237. int length)
  1238. {
  1239. struct usb_request *req;
  1240. int ret;
  1241. dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
  1242. req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
  1243. hsotg->ep0_reply = req;
  1244. if (!req) {
  1245. dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
  1246. return -ENOMEM;
  1247. }
  1248. req->buf = hsotg->ep0_buff;
  1249. req->length = length;
  1250. /*
  1251. * zero flag is for sending zlp in DATA IN stage. It has no impact on
  1252. * STATUS stage.
  1253. */
  1254. req->zero = 0;
  1255. req->complete = dwc2_hsotg_complete_oursetup;
  1256. if (length)
  1257. memcpy(req->buf, buff, length);
  1258. ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
  1259. if (ret) {
  1260. dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
  1261. return ret;
  1262. }
  1263. return 0;
  1264. }
  1265. /**
  1266. * dwc2_hsotg_process_req_status - process request GET_STATUS
  1267. * @hsotg: The device state
  1268. * @ctrl: USB control request
  1269. */
  1270. static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
  1271. struct usb_ctrlrequest *ctrl)
  1272. {
  1273. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1274. struct dwc2_hsotg_ep *ep;
  1275. __le16 reply;
  1276. int ret;
  1277. dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
  1278. if (!ep0->dir_in) {
  1279. dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
  1280. return -EINVAL;
  1281. }
  1282. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1283. case USB_RECIP_DEVICE:
  1284. /*
  1285. * bit 0 => self powered
  1286. * bit 1 => remote wakeup
  1287. */
  1288. reply = cpu_to_le16(0);
  1289. break;
  1290. case USB_RECIP_INTERFACE:
  1291. /* currently, the data result should be zero */
  1292. reply = cpu_to_le16(0);
  1293. break;
  1294. case USB_RECIP_ENDPOINT:
  1295. ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
  1296. if (!ep)
  1297. return -ENOENT;
  1298. reply = cpu_to_le16(ep->halted ? 1 : 0);
  1299. break;
  1300. default:
  1301. return 0;
  1302. }
  1303. if (le16_to_cpu(ctrl->wLength) != 2)
  1304. return -EINVAL;
  1305. ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
  1306. if (ret) {
  1307. dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
  1308. return ret;
  1309. }
  1310. return 1;
  1311. }
  1312. static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
  1313. /**
  1314. * get_ep_head - return the first request on the endpoint
  1315. * @hs_ep: The controller endpoint to get
  1316. *
  1317. * Get the first request on the endpoint.
  1318. */
  1319. static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
  1320. {
  1321. return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
  1322. queue);
  1323. }
  1324. /**
  1325. * dwc2_gadget_start_next_request - Starts next request from ep queue
  1326. * @hs_ep: Endpoint structure
  1327. *
  1328. * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
  1329. * in its handler. Hence we need to unmask it here to be able to do
  1330. * resynchronization.
  1331. */
  1332. static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
  1333. {
  1334. u32 mask;
  1335. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1336. int dir_in = hs_ep->dir_in;
  1337. struct dwc2_hsotg_req *hs_req;
  1338. u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
  1339. if (!list_empty(&hs_ep->queue)) {
  1340. hs_req = get_ep_head(hs_ep);
  1341. dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
  1342. return;
  1343. }
  1344. if (!hs_ep->isochronous)
  1345. return;
  1346. if (dir_in) {
  1347. dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
  1348. __func__);
  1349. } else {
  1350. dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
  1351. __func__);
  1352. mask = dwc2_readl(hsotg->regs + epmsk_reg);
  1353. mask |= DOEPMSK_OUTTKNEPDISMSK;
  1354. dwc2_writel(mask, hsotg->regs + epmsk_reg);
  1355. }
  1356. }
  1357. /**
  1358. * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
  1359. * @hsotg: The device state
  1360. * @ctrl: USB control request
  1361. */
  1362. static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
  1363. struct usb_ctrlrequest *ctrl)
  1364. {
  1365. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1366. struct dwc2_hsotg_req *hs_req;
  1367. bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
  1368. struct dwc2_hsotg_ep *ep;
  1369. int ret;
  1370. bool halted;
  1371. u32 recip;
  1372. u32 wValue;
  1373. u32 wIndex;
  1374. dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
  1375. __func__, set ? "SET" : "CLEAR");
  1376. wValue = le16_to_cpu(ctrl->wValue);
  1377. wIndex = le16_to_cpu(ctrl->wIndex);
  1378. recip = ctrl->bRequestType & USB_RECIP_MASK;
  1379. switch (recip) {
  1380. case USB_RECIP_DEVICE:
  1381. switch (wValue) {
  1382. case USB_DEVICE_REMOTE_WAKEUP:
  1383. hsotg->remote_wakeup_allowed = 1;
  1384. break;
  1385. case USB_DEVICE_TEST_MODE:
  1386. if ((wIndex & 0xff) != 0)
  1387. return -EINVAL;
  1388. if (!set)
  1389. return -EINVAL;
  1390. hsotg->test_mode = wIndex >> 8;
  1391. ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
  1392. if (ret) {
  1393. dev_err(hsotg->dev,
  1394. "%s: failed to send reply\n", __func__);
  1395. return ret;
  1396. }
  1397. break;
  1398. default:
  1399. return -ENOENT;
  1400. }
  1401. break;
  1402. case USB_RECIP_ENDPOINT:
  1403. ep = ep_from_windex(hsotg, wIndex);
  1404. if (!ep) {
  1405. dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
  1406. __func__, wIndex);
  1407. return -ENOENT;
  1408. }
  1409. switch (wValue) {
  1410. case USB_ENDPOINT_HALT:
  1411. halted = ep->halted;
  1412. dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
  1413. ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
  1414. if (ret) {
  1415. dev_err(hsotg->dev,
  1416. "%s: failed to send reply\n", __func__);
  1417. return ret;
  1418. }
  1419. /*
  1420. * we have to complete all requests for ep if it was
  1421. * halted, and the halt was cleared by CLEAR_FEATURE
  1422. */
  1423. if (!set && halted) {
  1424. /*
  1425. * If we have request in progress,
  1426. * then complete it
  1427. */
  1428. if (ep->req) {
  1429. hs_req = ep->req;
  1430. ep->req = NULL;
  1431. list_del_init(&hs_req->queue);
  1432. if (hs_req->req.complete) {
  1433. spin_unlock(&hsotg->lock);
  1434. usb_gadget_giveback_request(
  1435. &ep->ep, &hs_req->req);
  1436. spin_lock(&hsotg->lock);
  1437. }
  1438. }
  1439. /* If we have pending request, then start it */
  1440. if (!ep->req)
  1441. dwc2_gadget_start_next_request(ep);
  1442. }
  1443. break;
  1444. default:
  1445. return -ENOENT;
  1446. }
  1447. break;
  1448. default:
  1449. return -ENOENT;
  1450. }
  1451. return 1;
  1452. }
  1453. static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
  1454. /**
  1455. * dwc2_hsotg_stall_ep0 - stall ep0
  1456. * @hsotg: The device state
  1457. *
  1458. * Set stall for ep0 as response for setup request.
  1459. */
  1460. static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
  1461. {
  1462. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1463. u32 reg;
  1464. u32 ctrl;
  1465. dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
  1466. reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
  1467. /*
  1468. * DxEPCTL_Stall will be cleared by EP once it has
  1469. * taken effect, so no need to clear later.
  1470. */
  1471. ctrl = dwc2_readl(hsotg->regs + reg);
  1472. ctrl |= DXEPCTL_STALL;
  1473. ctrl |= DXEPCTL_CNAK;
  1474. dwc2_writel(ctrl, hsotg->regs + reg);
  1475. dev_dbg(hsotg->dev,
  1476. "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
  1477. ctrl, reg, dwc2_readl(hsotg->regs + reg));
  1478. /*
  1479. * complete won't be called, so we enqueue
  1480. * setup request here
  1481. */
  1482. dwc2_hsotg_enqueue_setup(hsotg);
  1483. }
  1484. /**
  1485. * dwc2_hsotg_process_control - process a control request
  1486. * @hsotg: The device state
  1487. * @ctrl: The control request received
  1488. *
  1489. * The controller has received the SETUP phase of a control request, and
  1490. * needs to work out what to do next (and whether to pass it on to the
  1491. * gadget driver).
  1492. */
  1493. static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
  1494. struct usb_ctrlrequest *ctrl)
  1495. {
  1496. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1497. int ret = 0;
  1498. u32 dcfg;
  1499. dev_dbg(hsotg->dev,
  1500. "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
  1501. ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
  1502. ctrl->wIndex, ctrl->wLength);
  1503. if (ctrl->wLength == 0) {
  1504. ep0->dir_in = 1;
  1505. hsotg->ep0_state = DWC2_EP0_STATUS_IN;
  1506. } else if (ctrl->bRequestType & USB_DIR_IN) {
  1507. ep0->dir_in = 1;
  1508. hsotg->ep0_state = DWC2_EP0_DATA_IN;
  1509. } else {
  1510. ep0->dir_in = 0;
  1511. hsotg->ep0_state = DWC2_EP0_DATA_OUT;
  1512. }
  1513. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1514. switch (ctrl->bRequest) {
  1515. case USB_REQ_SET_ADDRESS:
  1516. hsotg->connected = 1;
  1517. dcfg = dwc2_readl(hsotg->regs + DCFG);
  1518. dcfg &= ~DCFG_DEVADDR_MASK;
  1519. dcfg |= (le16_to_cpu(ctrl->wValue) <<
  1520. DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
  1521. dwc2_writel(dcfg, hsotg->regs + DCFG);
  1522. dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
  1523. ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
  1524. return;
  1525. case USB_REQ_GET_STATUS:
  1526. ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
  1527. break;
  1528. case USB_REQ_CLEAR_FEATURE:
  1529. case USB_REQ_SET_FEATURE:
  1530. ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
  1531. break;
  1532. }
  1533. }
  1534. /* as a fallback, try delivering it to the driver to deal with */
  1535. if (ret == 0 && hsotg->driver) {
  1536. spin_unlock(&hsotg->lock);
  1537. ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
  1538. spin_lock(&hsotg->lock);
  1539. if (ret < 0)
  1540. dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
  1541. }
  1542. /*
  1543. * the request is either unhandlable, or is not formatted correctly
  1544. * so respond with a STALL for the status stage to indicate failure.
  1545. */
  1546. if (ret < 0)
  1547. dwc2_hsotg_stall_ep0(hsotg);
  1548. }
  1549. /**
  1550. * dwc2_hsotg_complete_setup - completion of a setup transfer
  1551. * @ep: The endpoint the request was on.
  1552. * @req: The request completed.
  1553. *
  1554. * Called on completion of any requests the driver itself submitted for
  1555. * EP0 setup packets
  1556. */
  1557. static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
  1558. struct usb_request *req)
  1559. {
  1560. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1561. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1562. if (req->status < 0) {
  1563. dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
  1564. return;
  1565. }
  1566. spin_lock(&hsotg->lock);
  1567. if (req->actual == 0)
  1568. dwc2_hsotg_enqueue_setup(hsotg);
  1569. else
  1570. dwc2_hsotg_process_control(hsotg, req->buf);
  1571. spin_unlock(&hsotg->lock);
  1572. }
  1573. /**
  1574. * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
  1575. * @hsotg: The device state.
  1576. *
  1577. * Enqueue a request on EP0 if necessary to received any SETUP packets
  1578. * received from the host.
  1579. */
  1580. static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
  1581. {
  1582. struct usb_request *req = hsotg->ctrl_req;
  1583. struct dwc2_hsotg_req *hs_req = our_req(req);
  1584. int ret;
  1585. dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
  1586. req->zero = 0;
  1587. req->length = 8;
  1588. req->buf = hsotg->ctrl_buff;
  1589. req->complete = dwc2_hsotg_complete_setup;
  1590. if (!list_empty(&hs_req->queue)) {
  1591. dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
  1592. return;
  1593. }
  1594. hsotg->eps_out[0]->dir_in = 0;
  1595. hsotg->eps_out[0]->send_zlp = 0;
  1596. hsotg->ep0_state = DWC2_EP0_SETUP;
  1597. ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
  1598. if (ret < 0) {
  1599. dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
  1600. /*
  1601. * Don't think there's much we can do other than watch the
  1602. * driver fail.
  1603. */
  1604. }
  1605. }
  1606. static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
  1607. struct dwc2_hsotg_ep *hs_ep)
  1608. {
  1609. u32 ctrl;
  1610. u8 index = hs_ep->index;
  1611. u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
  1612. u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
  1613. if (hs_ep->dir_in)
  1614. dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
  1615. index);
  1616. else
  1617. dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
  1618. index);
  1619. if (using_desc_dma(hsotg)) {
  1620. /* Not specific buffer needed for ep0 ZLP */
  1621. dma_addr_t dma = hs_ep->desc_list_dma;
  1622. if (!index)
  1623. dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
  1624. dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0);
  1625. } else {
  1626. dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
  1627. DXEPTSIZ_XFERSIZE(0), hsotg->regs +
  1628. epsiz_reg);
  1629. }
  1630. ctrl = dwc2_readl(hsotg->regs + epctl_reg);
  1631. ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
  1632. ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
  1633. ctrl |= DXEPCTL_USBACTEP;
  1634. dwc2_writel(ctrl, hsotg->regs + epctl_reg);
  1635. }
  1636. /**
  1637. * dwc2_hsotg_complete_request - complete a request given to us
  1638. * @hsotg: The device state.
  1639. * @hs_ep: The endpoint the request was on.
  1640. * @hs_req: The request to complete.
  1641. * @result: The result code (0 => Ok, otherwise errno)
  1642. *
  1643. * The given request has finished, so call the necessary completion
  1644. * if it has one and then look to see if we can start a new request
  1645. * on the endpoint.
  1646. *
  1647. * Note, expects the ep to already be locked as appropriate.
  1648. */
  1649. static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
  1650. struct dwc2_hsotg_ep *hs_ep,
  1651. struct dwc2_hsotg_req *hs_req,
  1652. int result)
  1653. {
  1654. if (!hs_req) {
  1655. dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
  1656. return;
  1657. }
  1658. dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
  1659. hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
  1660. /*
  1661. * only replace the status if we've not already set an error
  1662. * from a previous transaction
  1663. */
  1664. if (hs_req->req.status == -EINPROGRESS)
  1665. hs_req->req.status = result;
  1666. if (using_dma(hsotg))
  1667. dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
  1668. dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
  1669. hs_ep->req = NULL;
  1670. list_del_init(&hs_req->queue);
  1671. /*
  1672. * call the complete request with the locks off, just in case the
  1673. * request tries to queue more work for this endpoint.
  1674. */
  1675. if (hs_req->req.complete) {
  1676. spin_unlock(&hsotg->lock);
  1677. usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
  1678. spin_lock(&hsotg->lock);
  1679. }
  1680. /* In DDMA don't need to proceed to starting of next ISOC request */
  1681. if (using_desc_dma(hsotg) && hs_ep->isochronous)
  1682. return;
  1683. /*
  1684. * Look to see if there is anything else to do. Note, the completion
  1685. * of the previous request may have caused a new request to be started
  1686. * so be careful when doing this.
  1687. */
  1688. if (!hs_ep->req && result >= 0)
  1689. dwc2_gadget_start_next_request(hs_ep);
  1690. }
  1691. /*
  1692. * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
  1693. * @hs_ep: The endpoint the request was on.
  1694. *
  1695. * Get first request from the ep queue, determine descriptor on which complete
  1696. * happened. SW based on isoc_chain_num discovers which half of the descriptor
  1697. * chain is currently in use by HW, adjusts dma_address and calculates index
  1698. * of completed descriptor based on the value of DEPDMA register. Update actual
  1699. * length of request, giveback to gadget.
  1700. */
  1701. static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
  1702. {
  1703. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1704. struct dwc2_hsotg_req *hs_req;
  1705. struct usb_request *ureq;
  1706. int index;
  1707. dma_addr_t dma_addr;
  1708. u32 dma_reg;
  1709. u32 depdma;
  1710. u32 desc_sts;
  1711. u32 mask;
  1712. hs_req = get_ep_head(hs_ep);
  1713. if (!hs_req) {
  1714. dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
  1715. return;
  1716. }
  1717. ureq = &hs_req->req;
  1718. dma_addr = hs_ep->desc_list_dma;
  1719. /*
  1720. * If lower half of descriptor chain is currently use by SW,
  1721. * that means higher half is being processed by HW, so shift
  1722. * DMA address to higher half of descriptor chain.
  1723. */
  1724. if (!hs_ep->isoc_chain_num)
  1725. dma_addr += sizeof(struct dwc2_dma_desc) *
  1726. (MAX_DMA_DESC_NUM_GENERIC / 2);
  1727. dma_reg = hs_ep->dir_in ? DIEPDMA(hs_ep->index) : DOEPDMA(hs_ep->index);
  1728. depdma = dwc2_readl(hsotg->regs + dma_reg);
  1729. index = (depdma - dma_addr) / sizeof(struct dwc2_dma_desc) - 1;
  1730. desc_sts = hs_ep->desc_list[index].status;
  1731. mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
  1732. DEV_DMA_ISOC_RX_NBYTES_MASK;
  1733. ureq->actual = ureq->length -
  1734. ((desc_sts & mask) >> DEV_DMA_ISOC_NBYTES_SHIFT);
  1735. /* Adjust actual length for ISOC Out if length is not align of 4 */
  1736. if (!hs_ep->dir_in && ureq->length & 0x3)
  1737. ureq->actual += 4 - (ureq->length & 0x3);
  1738. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  1739. }
  1740. /*
  1741. * dwc2_gadget_start_next_isoc_ddma - start next isoc request, if any.
  1742. * @hs_ep: The isochronous endpoint to be re-enabled.
  1743. *
  1744. * If ep has been disabled due to last descriptor servicing (IN endpoint) or
  1745. * BNA (OUT endpoint) check the status of other half of descriptor chain that
  1746. * was under SW control till HW was busy and restart the endpoint if needed.
  1747. */
  1748. static void dwc2_gadget_start_next_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
  1749. {
  1750. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1751. u32 depctl;
  1752. u32 dma_reg;
  1753. u32 ctrl;
  1754. u32 dma_addr = hs_ep->desc_list_dma;
  1755. unsigned char index = hs_ep->index;
  1756. dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
  1757. depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
  1758. ctrl = dwc2_readl(hsotg->regs + depctl);
  1759. /*
  1760. * EP was disabled if HW has processed last descriptor or BNA was set.
  1761. * So restart ep if SW has prepared new descriptor chain in ep_queue
  1762. * routine while HW was busy.
  1763. */
  1764. if (!(ctrl & DXEPCTL_EPENA)) {
  1765. if (!hs_ep->next_desc) {
  1766. dev_dbg(hsotg->dev, "%s: No more ISOC requests\n",
  1767. __func__);
  1768. return;
  1769. }
  1770. dma_addr += sizeof(struct dwc2_dma_desc) *
  1771. (MAX_DMA_DESC_NUM_GENERIC / 2) *
  1772. hs_ep->isoc_chain_num;
  1773. dwc2_writel(dma_addr, hsotg->regs + dma_reg);
  1774. ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
  1775. dwc2_writel(ctrl, hsotg->regs + depctl);
  1776. /* Switch ISOC descriptor chain number being processed by SW*/
  1777. hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1;
  1778. hs_ep->next_desc = 0;
  1779. dev_dbg(hsotg->dev, "%s: Restarted isochronous endpoint\n",
  1780. __func__);
  1781. }
  1782. }
  1783. /**
  1784. * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
  1785. * @hsotg: The device state.
  1786. * @ep_idx: The endpoint index for the data
  1787. * @size: The size of data in the fifo, in bytes
  1788. *
  1789. * The FIFO status shows there is data to read from the FIFO for a given
  1790. * endpoint, so sort out whether we need to read the data into a request
  1791. * that has been made for that endpoint.
  1792. */
  1793. static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
  1794. {
  1795. struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
  1796. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  1797. void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
  1798. int to_read;
  1799. int max_req;
  1800. int read_ptr;
  1801. if (!hs_req) {
  1802. u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
  1803. int ptr;
  1804. dev_dbg(hsotg->dev,
  1805. "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
  1806. __func__, size, ep_idx, epctl);
  1807. /* dump the data from the FIFO, we've nothing we can do */
  1808. for (ptr = 0; ptr < size; ptr += 4)
  1809. (void)dwc2_readl(fifo);
  1810. return;
  1811. }
  1812. to_read = size;
  1813. read_ptr = hs_req->req.actual;
  1814. max_req = hs_req->req.length - read_ptr;
  1815. dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
  1816. __func__, to_read, max_req, read_ptr, hs_req->req.length);
  1817. if (to_read > max_req) {
  1818. /*
  1819. * more data appeared than we where willing
  1820. * to deal with in this request.
  1821. */
  1822. /* currently we don't deal this */
  1823. WARN_ON_ONCE(1);
  1824. }
  1825. hs_ep->total_data += to_read;
  1826. hs_req->req.actual += to_read;
  1827. to_read = DIV_ROUND_UP(to_read, 4);
  1828. /*
  1829. * note, we might over-write the buffer end by 3 bytes depending on
  1830. * alignment of the data.
  1831. */
  1832. ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
  1833. }
  1834. /**
  1835. * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
  1836. * @hsotg: The device instance
  1837. * @dir_in: If IN zlp
  1838. *
  1839. * Generate a zero-length IN packet request for terminating a SETUP
  1840. * transaction.
  1841. *
  1842. * Note, since we don't write any data to the TxFIFO, then it is
  1843. * currently believed that we do not need to wait for any space in
  1844. * the TxFIFO.
  1845. */
  1846. static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
  1847. {
  1848. /* eps_out[0] is used in both directions */
  1849. hsotg->eps_out[0]->dir_in = dir_in;
  1850. hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
  1851. dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
  1852. }
  1853. static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
  1854. u32 epctl_reg)
  1855. {
  1856. u32 ctrl;
  1857. ctrl = dwc2_readl(hsotg->regs + epctl_reg);
  1858. if (ctrl & DXEPCTL_EOFRNUM)
  1859. ctrl |= DXEPCTL_SETEVENFR;
  1860. else
  1861. ctrl |= DXEPCTL_SETODDFR;
  1862. dwc2_writel(ctrl, hsotg->regs + epctl_reg);
  1863. }
  1864. /*
  1865. * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
  1866. * @hs_ep - The endpoint on which transfer went
  1867. *
  1868. * Iterate over endpoints descriptor chain and get info on bytes remained
  1869. * in DMA descriptors after transfer has completed. Used for non isoc EPs.
  1870. */
  1871. static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
  1872. {
  1873. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1874. unsigned int bytes_rem = 0;
  1875. struct dwc2_dma_desc *desc = hs_ep->desc_list;
  1876. int i;
  1877. u32 status;
  1878. if (!desc)
  1879. return -EINVAL;
  1880. for (i = 0; i < hs_ep->desc_count; ++i) {
  1881. status = desc->status;
  1882. bytes_rem += status & DEV_DMA_NBYTES_MASK;
  1883. if (status & DEV_DMA_STS_MASK)
  1884. dev_err(hsotg->dev, "descriptor %d closed with %x\n",
  1885. i, status & DEV_DMA_STS_MASK);
  1886. }
  1887. return bytes_rem;
  1888. }
  1889. /**
  1890. * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
  1891. * @hsotg: The device instance
  1892. * @epnum: The endpoint received from
  1893. *
  1894. * The RXFIFO has delivered an OutDone event, which means that the data
  1895. * transfer for an OUT endpoint has been completed, either by a short
  1896. * packet or by the finish of a transfer.
  1897. */
  1898. static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
  1899. {
  1900. u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
  1901. struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
  1902. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  1903. struct usb_request *req = &hs_req->req;
  1904. unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  1905. int result = 0;
  1906. if (!hs_req) {
  1907. dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
  1908. return;
  1909. }
  1910. if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
  1911. dev_dbg(hsotg->dev, "zlp packet received\n");
  1912. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  1913. dwc2_hsotg_enqueue_setup(hsotg);
  1914. return;
  1915. }
  1916. if (using_desc_dma(hsotg))
  1917. size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
  1918. if (using_dma(hsotg)) {
  1919. unsigned int size_done;
  1920. /*
  1921. * Calculate the size of the transfer by checking how much
  1922. * is left in the endpoint size register and then working it
  1923. * out from the amount we loaded for the transfer.
  1924. *
  1925. * We need to do this as DMA pointers are always 32bit aligned
  1926. * so may overshoot/undershoot the transfer.
  1927. */
  1928. size_done = hs_ep->size_loaded - size_left;
  1929. size_done += hs_ep->last_load;
  1930. req->actual = size_done;
  1931. }
  1932. /* if there is more request to do, schedule new transfer */
  1933. if (req->actual < req->length && size_left == 0) {
  1934. dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
  1935. return;
  1936. }
  1937. if (req->actual < req->length && req->short_not_ok) {
  1938. dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
  1939. __func__, req->actual, req->length);
  1940. /*
  1941. * todo - what should we return here? there's no one else
  1942. * even bothering to check the status.
  1943. */
  1944. }
  1945. /* DDMA IN status phase will start from StsPhseRcvd interrupt */
  1946. if (!using_desc_dma(hsotg) && epnum == 0 &&
  1947. hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
  1948. /* Move to STATUS IN */
  1949. dwc2_hsotg_ep0_zlp(hsotg, true);
  1950. return;
  1951. }
  1952. /*
  1953. * Slave mode OUT transfers do not go through XferComplete so
  1954. * adjust the ISOC parity here.
  1955. */
  1956. if (!using_dma(hsotg)) {
  1957. if (hs_ep->isochronous && hs_ep->interval == 1)
  1958. dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
  1959. else if (hs_ep->isochronous && hs_ep->interval > 1)
  1960. dwc2_gadget_incr_frame_num(hs_ep);
  1961. }
  1962. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
  1963. }
  1964. /**
  1965. * dwc2_hsotg_handle_rx - RX FIFO has data
  1966. * @hsotg: The device instance
  1967. *
  1968. * The IRQ handler has detected that the RX FIFO has some data in it
  1969. * that requires processing, so find out what is in there and do the
  1970. * appropriate read.
  1971. *
  1972. * The RXFIFO is a true FIFO, the packets coming out are still in packet
  1973. * chunks, so if you have x packets received on an endpoint you'll get x
  1974. * FIFO events delivered, each with a packet's worth of data in it.
  1975. *
  1976. * When using DMA, we should not be processing events from the RXFIFO
  1977. * as the actual data should be sent to the memory directly and we turn
  1978. * on the completion interrupts to get notifications of transfer completion.
  1979. */
  1980. static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
  1981. {
  1982. u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
  1983. u32 epnum, status, size;
  1984. WARN_ON(using_dma(hsotg));
  1985. epnum = grxstsr & GRXSTS_EPNUM_MASK;
  1986. status = grxstsr & GRXSTS_PKTSTS_MASK;
  1987. size = grxstsr & GRXSTS_BYTECNT_MASK;
  1988. size >>= GRXSTS_BYTECNT_SHIFT;
  1989. dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
  1990. __func__, grxstsr, size, epnum);
  1991. switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
  1992. case GRXSTS_PKTSTS_GLOBALOUTNAK:
  1993. dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
  1994. break;
  1995. case GRXSTS_PKTSTS_OUTDONE:
  1996. dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
  1997. dwc2_hsotg_read_frameno(hsotg));
  1998. if (!using_dma(hsotg))
  1999. dwc2_hsotg_handle_outdone(hsotg, epnum);
  2000. break;
  2001. case GRXSTS_PKTSTS_SETUPDONE:
  2002. dev_dbg(hsotg->dev,
  2003. "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
  2004. dwc2_hsotg_read_frameno(hsotg),
  2005. dwc2_readl(hsotg->regs + DOEPCTL(0)));
  2006. /*
  2007. * Call dwc2_hsotg_handle_outdone here if it was not called from
  2008. * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
  2009. * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
  2010. */
  2011. if (hsotg->ep0_state == DWC2_EP0_SETUP)
  2012. dwc2_hsotg_handle_outdone(hsotg, epnum);
  2013. break;
  2014. case GRXSTS_PKTSTS_OUTRX:
  2015. dwc2_hsotg_rx_data(hsotg, epnum, size);
  2016. break;
  2017. case GRXSTS_PKTSTS_SETUPRX:
  2018. dev_dbg(hsotg->dev,
  2019. "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
  2020. dwc2_hsotg_read_frameno(hsotg),
  2021. dwc2_readl(hsotg->regs + DOEPCTL(0)));
  2022. WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
  2023. dwc2_hsotg_rx_data(hsotg, epnum, size);
  2024. break;
  2025. default:
  2026. dev_warn(hsotg->dev, "%s: unknown status %08x\n",
  2027. __func__, grxstsr);
  2028. dwc2_hsotg_dump(hsotg);
  2029. break;
  2030. }
  2031. }
  2032. /**
  2033. * dwc2_hsotg_ep0_mps - turn max packet size into register setting
  2034. * @mps: The maximum packet size in bytes.
  2035. */
  2036. static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
  2037. {
  2038. switch (mps) {
  2039. case 64:
  2040. return D0EPCTL_MPS_64;
  2041. case 32:
  2042. return D0EPCTL_MPS_32;
  2043. case 16:
  2044. return D0EPCTL_MPS_16;
  2045. case 8:
  2046. return D0EPCTL_MPS_8;
  2047. }
  2048. /* bad max packet size, warn and return invalid result */
  2049. WARN_ON(1);
  2050. return (u32)-1;
  2051. }
  2052. /**
  2053. * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
  2054. * @hsotg: The driver state.
  2055. * @ep: The index number of the endpoint
  2056. * @mps: The maximum packet size in bytes
  2057. * @mc: The multicount value
  2058. *
  2059. * Configure the maximum packet size for the given endpoint, updating
  2060. * the hardware control registers to reflect this.
  2061. */
  2062. static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
  2063. unsigned int ep, unsigned int mps,
  2064. unsigned int mc, unsigned int dir_in)
  2065. {
  2066. struct dwc2_hsotg_ep *hs_ep;
  2067. void __iomem *regs = hsotg->regs;
  2068. u32 reg;
  2069. hs_ep = index_to_ep(hsotg, ep, dir_in);
  2070. if (!hs_ep)
  2071. return;
  2072. if (ep == 0) {
  2073. u32 mps_bytes = mps;
  2074. /* EP0 is a special case */
  2075. mps = dwc2_hsotg_ep0_mps(mps_bytes);
  2076. if (mps > 3)
  2077. goto bad_mps;
  2078. hs_ep->ep.maxpacket = mps_bytes;
  2079. hs_ep->mc = 1;
  2080. } else {
  2081. if (mps > 1024)
  2082. goto bad_mps;
  2083. hs_ep->mc = mc;
  2084. if (mc > 3)
  2085. goto bad_mps;
  2086. hs_ep->ep.maxpacket = mps;
  2087. }
  2088. if (dir_in) {
  2089. reg = dwc2_readl(regs + DIEPCTL(ep));
  2090. reg &= ~DXEPCTL_MPS_MASK;
  2091. reg |= mps;
  2092. dwc2_writel(reg, regs + DIEPCTL(ep));
  2093. } else {
  2094. reg = dwc2_readl(regs + DOEPCTL(ep));
  2095. reg &= ~DXEPCTL_MPS_MASK;
  2096. reg |= mps;
  2097. dwc2_writel(reg, regs + DOEPCTL(ep));
  2098. }
  2099. return;
  2100. bad_mps:
  2101. dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
  2102. }
  2103. /**
  2104. * dwc2_hsotg_txfifo_flush - flush Tx FIFO
  2105. * @hsotg: The driver state
  2106. * @idx: The index for the endpoint (0..15)
  2107. */
  2108. static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
  2109. {
  2110. dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
  2111. hsotg->regs + GRSTCTL);
  2112. /* wait until the fifo is flushed */
  2113. if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 100))
  2114. dev_warn(hsotg->dev, "%s: timeout flushing fifo GRSTCTL_TXFFLSH\n",
  2115. __func__);
  2116. }
  2117. /**
  2118. * dwc2_hsotg_trytx - check to see if anything needs transmitting
  2119. * @hsotg: The driver state
  2120. * @hs_ep: The driver endpoint to check.
  2121. *
  2122. * Check to see if there is a request that has data to send, and if so
  2123. * make an attempt to write data into the FIFO.
  2124. */
  2125. static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
  2126. struct dwc2_hsotg_ep *hs_ep)
  2127. {
  2128. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  2129. if (!hs_ep->dir_in || !hs_req) {
  2130. /**
  2131. * if request is not enqueued, we disable interrupts
  2132. * for endpoints, excepting ep0
  2133. */
  2134. if (hs_ep->index != 0)
  2135. dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
  2136. hs_ep->dir_in, 0);
  2137. return 0;
  2138. }
  2139. if (hs_req->req.actual < hs_req->req.length) {
  2140. dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
  2141. hs_ep->index);
  2142. return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
  2143. }
  2144. return 0;
  2145. }
  2146. /**
  2147. * dwc2_hsotg_complete_in - complete IN transfer
  2148. * @hsotg: The device state.
  2149. * @hs_ep: The endpoint that has just completed.
  2150. *
  2151. * An IN transfer has been completed, update the transfer's state and then
  2152. * call the relevant completion routines.
  2153. */
  2154. static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
  2155. struct dwc2_hsotg_ep *hs_ep)
  2156. {
  2157. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  2158. u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
  2159. int size_left, size_done;
  2160. if (!hs_req) {
  2161. dev_dbg(hsotg->dev, "XferCompl but no req\n");
  2162. return;
  2163. }
  2164. /* Finish ZLP handling for IN EP0 transactions */
  2165. if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
  2166. dev_dbg(hsotg->dev, "zlp packet sent\n");
  2167. /*
  2168. * While send zlp for DWC2_EP0_STATUS_IN EP direction was
  2169. * changed to IN. Change back to complete OUT transfer request
  2170. */
  2171. hs_ep->dir_in = 0;
  2172. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  2173. if (hsotg->test_mode) {
  2174. int ret;
  2175. ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
  2176. if (ret < 0) {
  2177. dev_dbg(hsotg->dev, "Invalid Test #%d\n",
  2178. hsotg->test_mode);
  2179. dwc2_hsotg_stall_ep0(hsotg);
  2180. return;
  2181. }
  2182. }
  2183. dwc2_hsotg_enqueue_setup(hsotg);
  2184. return;
  2185. }
  2186. /*
  2187. * Calculate the size of the transfer by checking how much is left
  2188. * in the endpoint size register and then working it out from
  2189. * the amount we loaded for the transfer.
  2190. *
  2191. * We do this even for DMA, as the transfer may have incremented
  2192. * past the end of the buffer (DMA transfers are always 32bit
  2193. * aligned).
  2194. */
  2195. if (using_desc_dma(hsotg)) {
  2196. size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
  2197. if (size_left < 0)
  2198. dev_err(hsotg->dev, "error parsing DDMA results %d\n",
  2199. size_left);
  2200. } else {
  2201. size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  2202. }
  2203. size_done = hs_ep->size_loaded - size_left;
  2204. size_done += hs_ep->last_load;
  2205. if (hs_req->req.actual != size_done)
  2206. dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
  2207. __func__, hs_req->req.actual, size_done);
  2208. hs_req->req.actual = size_done;
  2209. dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
  2210. hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
  2211. if (!size_left && hs_req->req.actual < hs_req->req.length) {
  2212. dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
  2213. dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
  2214. return;
  2215. }
  2216. /* Zlp for all endpoints, for ep0 only in DATA IN stage */
  2217. if (hs_ep->send_zlp) {
  2218. dwc2_hsotg_program_zlp(hsotg, hs_ep);
  2219. hs_ep->send_zlp = 0;
  2220. /* transfer will be completed on next complete interrupt */
  2221. return;
  2222. }
  2223. if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
  2224. /* Move to STATUS OUT */
  2225. dwc2_hsotg_ep0_zlp(hsotg, false);
  2226. return;
  2227. }
  2228. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  2229. }
  2230. /**
  2231. * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
  2232. * @hsotg: The device state.
  2233. * @idx: Index of ep.
  2234. * @dir_in: Endpoint direction 1-in 0-out.
  2235. *
  2236. * Reads for endpoint with given index and direction, by masking
  2237. * epint_reg with coresponding mask.
  2238. */
  2239. static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
  2240. unsigned int idx, int dir_in)
  2241. {
  2242. u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
  2243. u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
  2244. u32 ints;
  2245. u32 mask;
  2246. u32 diepempmsk;
  2247. mask = dwc2_readl(hsotg->regs + epmsk_reg);
  2248. diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK);
  2249. mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
  2250. mask |= DXEPINT_SETUP_RCVD;
  2251. ints = dwc2_readl(hsotg->regs + epint_reg);
  2252. ints &= mask;
  2253. return ints;
  2254. }
  2255. /**
  2256. * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
  2257. * @hs_ep: The endpoint on which interrupt is asserted.
  2258. *
  2259. * This interrupt indicates that the endpoint has been disabled per the
  2260. * application's request.
  2261. *
  2262. * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
  2263. * in case of ISOC completes current request.
  2264. *
  2265. * For ISOC-OUT endpoints completes expired requests. If there is remaining
  2266. * request starts it.
  2267. */
  2268. static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
  2269. {
  2270. struct dwc2_hsotg *hsotg = hs_ep->parent;
  2271. struct dwc2_hsotg_req *hs_req;
  2272. unsigned char idx = hs_ep->index;
  2273. int dir_in = hs_ep->dir_in;
  2274. u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
  2275. int dctl = dwc2_readl(hsotg->regs + DCTL);
  2276. dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
  2277. if (dir_in) {
  2278. int epctl = dwc2_readl(hsotg->regs + epctl_reg);
  2279. dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
  2280. if (hs_ep->isochronous) {
  2281. dwc2_hsotg_complete_in(hsotg, hs_ep);
  2282. return;
  2283. }
  2284. if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
  2285. int dctl = dwc2_readl(hsotg->regs + DCTL);
  2286. dctl |= DCTL_CGNPINNAK;
  2287. dwc2_writel(dctl, hsotg->regs + DCTL);
  2288. }
  2289. return;
  2290. }
  2291. if (dctl & DCTL_GOUTNAKSTS) {
  2292. dctl |= DCTL_CGOUTNAK;
  2293. dwc2_writel(dctl, hsotg->regs + DCTL);
  2294. }
  2295. if (!hs_ep->isochronous)
  2296. return;
  2297. if (list_empty(&hs_ep->queue)) {
  2298. dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
  2299. __func__, hs_ep);
  2300. return;
  2301. }
  2302. do {
  2303. hs_req = get_ep_head(hs_ep);
  2304. if (hs_req)
  2305. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
  2306. -ENODATA);
  2307. dwc2_gadget_incr_frame_num(hs_ep);
  2308. } while (dwc2_gadget_target_frame_elapsed(hs_ep));
  2309. dwc2_gadget_start_next_request(hs_ep);
  2310. }
  2311. /**
  2312. * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
  2313. * @hs_ep: The endpoint on which interrupt is asserted.
  2314. *
  2315. * This is starting point for ISOC-OUT transfer, synchronization done with
  2316. * first out token received from host while corresponding EP is disabled.
  2317. *
  2318. * Device does not know initial frame in which out token will come. For this
  2319. * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
  2320. * getting this interrupt SW starts calculation for next transfer frame.
  2321. */
  2322. static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
  2323. {
  2324. struct dwc2_hsotg *hsotg = ep->parent;
  2325. int dir_in = ep->dir_in;
  2326. u32 doepmsk;
  2327. u32 tmp;
  2328. if (dir_in || !ep->isochronous)
  2329. return;
  2330. /*
  2331. * Store frame in which irq was asserted here, as
  2332. * it can change while completing request below.
  2333. */
  2334. tmp = dwc2_hsotg_read_frameno(hsotg);
  2335. dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), -ENODATA);
  2336. if (using_desc_dma(hsotg)) {
  2337. if (ep->target_frame == TARGET_FRAME_INITIAL) {
  2338. /* Start first ISO Out */
  2339. ep->target_frame = tmp;
  2340. dwc2_gadget_start_isoc_ddma(ep);
  2341. }
  2342. return;
  2343. }
  2344. if (ep->interval > 1 &&
  2345. ep->target_frame == TARGET_FRAME_INITIAL) {
  2346. u32 dsts;
  2347. u32 ctrl;
  2348. dsts = dwc2_readl(hsotg->regs + DSTS);
  2349. ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
  2350. dwc2_gadget_incr_frame_num(ep);
  2351. ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index));
  2352. if (ep->target_frame & 0x1)
  2353. ctrl |= DXEPCTL_SETODDFR;
  2354. else
  2355. ctrl |= DXEPCTL_SETEVENFR;
  2356. dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index));
  2357. }
  2358. dwc2_gadget_start_next_request(ep);
  2359. doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
  2360. doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
  2361. dwc2_writel(doepmsk, hsotg->regs + DOEPMSK);
  2362. }
  2363. /**
  2364. * dwc2_gadget_handle_nak - handle NAK interrupt
  2365. * @hs_ep: The endpoint on which interrupt is asserted.
  2366. *
  2367. * This is starting point for ISOC-IN transfer, synchronization done with
  2368. * first IN token received from host while corresponding EP is disabled.
  2369. *
  2370. * Device does not know when first one token will arrive from host. On first
  2371. * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
  2372. * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
  2373. * sent in response to that as there was no data in FIFO. SW is basing on this
  2374. * interrupt to obtain frame in which token has come and then based on the
  2375. * interval calculates next frame for transfer.
  2376. */
  2377. static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
  2378. {
  2379. struct dwc2_hsotg *hsotg = hs_ep->parent;
  2380. int dir_in = hs_ep->dir_in;
  2381. if (!dir_in || !hs_ep->isochronous)
  2382. return;
  2383. if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
  2384. hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
  2385. if (using_desc_dma(hsotg)) {
  2386. dwc2_gadget_start_isoc_ddma(hs_ep);
  2387. return;
  2388. }
  2389. if (hs_ep->interval > 1) {
  2390. u32 ctrl = dwc2_readl(hsotg->regs +
  2391. DIEPCTL(hs_ep->index));
  2392. if (hs_ep->target_frame & 0x1)
  2393. ctrl |= DXEPCTL_SETODDFR;
  2394. else
  2395. ctrl |= DXEPCTL_SETEVENFR;
  2396. dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index));
  2397. }
  2398. dwc2_hsotg_complete_request(hsotg, hs_ep,
  2399. get_ep_head(hs_ep), 0);
  2400. }
  2401. dwc2_gadget_incr_frame_num(hs_ep);
  2402. }
  2403. /**
  2404. * dwc2_hsotg_epint - handle an in/out endpoint interrupt
  2405. * @hsotg: The driver state
  2406. * @idx: The index for the endpoint (0..15)
  2407. * @dir_in: Set if this is an IN endpoint
  2408. *
  2409. * Process and clear any interrupt pending for an individual endpoint
  2410. */
  2411. static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
  2412. int dir_in)
  2413. {
  2414. struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
  2415. u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
  2416. u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
  2417. u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
  2418. u32 ints;
  2419. u32 ctrl;
  2420. ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
  2421. ctrl = dwc2_readl(hsotg->regs + epctl_reg);
  2422. /* Clear endpoint interrupts */
  2423. dwc2_writel(ints, hsotg->regs + epint_reg);
  2424. if (!hs_ep) {
  2425. dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
  2426. __func__, idx, dir_in ? "in" : "out");
  2427. return;
  2428. }
  2429. dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
  2430. __func__, idx, dir_in ? "in" : "out", ints);
  2431. /* Don't process XferCompl interrupt if it is a setup packet */
  2432. if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
  2433. ints &= ~DXEPINT_XFERCOMPL;
  2434. /*
  2435. * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
  2436. * stage and xfercomplete was generated without SETUP phase done
  2437. * interrupt. SW should parse received setup packet only after host's
  2438. * exit from setup phase of control transfer.
  2439. */
  2440. if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
  2441. hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
  2442. ints &= ~DXEPINT_XFERCOMPL;
  2443. if (ints & DXEPINT_XFERCOMPL) {
  2444. dev_dbg(hsotg->dev,
  2445. "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
  2446. __func__, dwc2_readl(hsotg->regs + epctl_reg),
  2447. dwc2_readl(hsotg->regs + epsiz_reg));
  2448. /* In DDMA handle isochronous requests separately */
  2449. if (using_desc_dma(hsotg) && hs_ep->isochronous) {
  2450. dwc2_gadget_complete_isoc_request_ddma(hs_ep);
  2451. /* Try to start next isoc request */
  2452. dwc2_gadget_start_next_isoc_ddma(hs_ep);
  2453. } else if (dir_in) {
  2454. /*
  2455. * We get OutDone from the FIFO, so we only
  2456. * need to look at completing IN requests here
  2457. * if operating slave mode
  2458. */
  2459. if (hs_ep->isochronous && hs_ep->interval > 1)
  2460. dwc2_gadget_incr_frame_num(hs_ep);
  2461. dwc2_hsotg_complete_in(hsotg, hs_ep);
  2462. if (ints & DXEPINT_NAKINTRPT)
  2463. ints &= ~DXEPINT_NAKINTRPT;
  2464. if (idx == 0 && !hs_ep->req)
  2465. dwc2_hsotg_enqueue_setup(hsotg);
  2466. } else if (using_dma(hsotg)) {
  2467. /*
  2468. * We're using DMA, we need to fire an OutDone here
  2469. * as we ignore the RXFIFO.
  2470. */
  2471. if (hs_ep->isochronous && hs_ep->interval > 1)
  2472. dwc2_gadget_incr_frame_num(hs_ep);
  2473. dwc2_hsotg_handle_outdone(hsotg, idx);
  2474. }
  2475. }
  2476. if (ints & DXEPINT_EPDISBLD)
  2477. dwc2_gadget_handle_ep_disabled(hs_ep);
  2478. if (ints & DXEPINT_OUTTKNEPDIS)
  2479. dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
  2480. if (ints & DXEPINT_NAKINTRPT)
  2481. dwc2_gadget_handle_nak(hs_ep);
  2482. if (ints & DXEPINT_AHBERR)
  2483. dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
  2484. if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
  2485. dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
  2486. if (using_dma(hsotg) && idx == 0) {
  2487. /*
  2488. * this is the notification we've received a
  2489. * setup packet. In non-DMA mode we'd get this
  2490. * from the RXFIFO, instead we need to process
  2491. * the setup here.
  2492. */
  2493. if (dir_in)
  2494. WARN_ON_ONCE(1);
  2495. else
  2496. dwc2_hsotg_handle_outdone(hsotg, 0);
  2497. }
  2498. }
  2499. if (ints & DXEPINT_STSPHSERCVD) {
  2500. dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
  2501. /* Safety check EP0 state when STSPHSERCVD asserted */
  2502. if (hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
  2503. /* Move to STATUS IN for DDMA */
  2504. if (using_desc_dma(hsotg))
  2505. dwc2_hsotg_ep0_zlp(hsotg, true);
  2506. }
  2507. }
  2508. if (ints & DXEPINT_BACK2BACKSETUP)
  2509. dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
  2510. if (ints & DXEPINT_BNAINTR) {
  2511. dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
  2512. /*
  2513. * Try to start next isoc request, if any.
  2514. * Sometimes the endpoint remains enabled after BNA interrupt
  2515. * assertion, which is not expected, hence we can enter here
  2516. * couple of times.
  2517. */
  2518. if (hs_ep->isochronous)
  2519. dwc2_gadget_start_next_isoc_ddma(hs_ep);
  2520. }
  2521. if (dir_in && !hs_ep->isochronous) {
  2522. /* not sure if this is important, but we'll clear it anyway */
  2523. if (ints & DXEPINT_INTKNTXFEMP) {
  2524. dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
  2525. __func__, idx);
  2526. }
  2527. /* this probably means something bad is happening */
  2528. if (ints & DXEPINT_INTKNEPMIS) {
  2529. dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
  2530. __func__, idx);
  2531. }
  2532. /* FIFO has space or is empty (see GAHBCFG) */
  2533. if (hsotg->dedicated_fifos &&
  2534. ints & DXEPINT_TXFEMP) {
  2535. dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
  2536. __func__, idx);
  2537. if (!using_dma(hsotg))
  2538. dwc2_hsotg_trytx(hsotg, hs_ep);
  2539. }
  2540. }
  2541. }
  2542. /**
  2543. * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
  2544. * @hsotg: The device state.
  2545. *
  2546. * Handle updating the device settings after the enumeration phase has
  2547. * been completed.
  2548. */
  2549. static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
  2550. {
  2551. u32 dsts = dwc2_readl(hsotg->regs + DSTS);
  2552. int ep0_mps = 0, ep_mps = 8;
  2553. /*
  2554. * This should signal the finish of the enumeration phase
  2555. * of the USB handshaking, so we should now know what rate
  2556. * we connected at.
  2557. */
  2558. dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
  2559. /*
  2560. * note, since we're limited by the size of transfer on EP0, and
  2561. * it seems IN transfers must be a even number of packets we do
  2562. * not advertise a 64byte MPS on EP0.
  2563. */
  2564. /* catch both EnumSpd_FS and EnumSpd_FS48 */
  2565. switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
  2566. case DSTS_ENUMSPD_FS:
  2567. case DSTS_ENUMSPD_FS48:
  2568. hsotg->gadget.speed = USB_SPEED_FULL;
  2569. ep0_mps = EP0_MPS_LIMIT;
  2570. ep_mps = 1023;
  2571. break;
  2572. case DSTS_ENUMSPD_HS:
  2573. hsotg->gadget.speed = USB_SPEED_HIGH;
  2574. ep0_mps = EP0_MPS_LIMIT;
  2575. ep_mps = 1024;
  2576. break;
  2577. case DSTS_ENUMSPD_LS:
  2578. hsotg->gadget.speed = USB_SPEED_LOW;
  2579. ep0_mps = 8;
  2580. ep_mps = 8;
  2581. /*
  2582. * note, we don't actually support LS in this driver at the
  2583. * moment, and the documentation seems to imply that it isn't
  2584. * supported by the PHYs on some of the devices.
  2585. */
  2586. break;
  2587. }
  2588. dev_info(hsotg->dev, "new device is %s\n",
  2589. usb_speed_string(hsotg->gadget.speed));
  2590. /*
  2591. * we should now know the maximum packet size for an
  2592. * endpoint, so set the endpoints to a default value.
  2593. */
  2594. if (ep0_mps) {
  2595. int i;
  2596. /* Initialize ep0 for both in and out directions */
  2597. dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
  2598. dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
  2599. for (i = 1; i < hsotg->num_of_eps; i++) {
  2600. if (hsotg->eps_in[i])
  2601. dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
  2602. 0, 1);
  2603. if (hsotg->eps_out[i])
  2604. dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
  2605. 0, 0);
  2606. }
  2607. }
  2608. /* ensure after enumeration our EP0 is active */
  2609. dwc2_hsotg_enqueue_setup(hsotg);
  2610. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  2611. dwc2_readl(hsotg->regs + DIEPCTL0),
  2612. dwc2_readl(hsotg->regs + DOEPCTL0));
  2613. }
  2614. /**
  2615. * kill_all_requests - remove all requests from the endpoint's queue
  2616. * @hsotg: The device state.
  2617. * @ep: The endpoint the requests may be on.
  2618. * @result: The result code to use.
  2619. *
  2620. * Go through the requests on the given endpoint and mark them
  2621. * completed with the given result code.
  2622. */
  2623. static void kill_all_requests(struct dwc2_hsotg *hsotg,
  2624. struct dwc2_hsotg_ep *ep,
  2625. int result)
  2626. {
  2627. struct dwc2_hsotg_req *req, *treq;
  2628. unsigned int size;
  2629. ep->req = NULL;
  2630. list_for_each_entry_safe(req, treq, &ep->queue, queue)
  2631. dwc2_hsotg_complete_request(hsotg, ep, req,
  2632. result);
  2633. if (!hsotg->dedicated_fifos)
  2634. return;
  2635. size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
  2636. if (size < ep->fifo_size)
  2637. dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
  2638. }
  2639. /**
  2640. * dwc2_hsotg_disconnect - disconnect service
  2641. * @hsotg: The device state.
  2642. *
  2643. * The device has been disconnected. Remove all current
  2644. * transactions and signal the gadget driver that this
  2645. * has happened.
  2646. */
  2647. void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
  2648. {
  2649. unsigned int ep;
  2650. if (!hsotg->connected)
  2651. return;
  2652. hsotg->connected = 0;
  2653. hsotg->test_mode = 0;
  2654. for (ep = 0; ep < hsotg->num_of_eps; ep++) {
  2655. if (hsotg->eps_in[ep])
  2656. kill_all_requests(hsotg, hsotg->eps_in[ep],
  2657. -ESHUTDOWN);
  2658. if (hsotg->eps_out[ep])
  2659. kill_all_requests(hsotg, hsotg->eps_out[ep],
  2660. -ESHUTDOWN);
  2661. }
  2662. call_gadget(hsotg, disconnect);
  2663. hsotg->lx_state = DWC2_L3;
  2664. usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED);
  2665. }
  2666. /**
  2667. * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
  2668. * @hsotg: The device state:
  2669. * @periodic: True if this is a periodic FIFO interrupt
  2670. */
  2671. static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
  2672. {
  2673. struct dwc2_hsotg_ep *ep;
  2674. int epno, ret;
  2675. /* look through for any more data to transmit */
  2676. for (epno = 0; epno < hsotg->num_of_eps; epno++) {
  2677. ep = index_to_ep(hsotg, epno, 1);
  2678. if (!ep)
  2679. continue;
  2680. if (!ep->dir_in)
  2681. continue;
  2682. if ((periodic && !ep->periodic) ||
  2683. (!periodic && ep->periodic))
  2684. continue;
  2685. ret = dwc2_hsotg_trytx(hsotg, ep);
  2686. if (ret < 0)
  2687. break;
  2688. }
  2689. }
  2690. /* IRQ flags which will trigger a retry around the IRQ loop */
  2691. #define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
  2692. GINTSTS_PTXFEMP | \
  2693. GINTSTS_RXFLVL)
  2694. /**
  2695. * dwc2_hsotg_core_init - issue softreset to the core
  2696. * @hsotg: The device state
  2697. *
  2698. * Issue a soft reset to the core, and await the core finishing it.
  2699. */
  2700. void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
  2701. bool is_usb_reset)
  2702. {
  2703. u32 intmsk;
  2704. u32 val;
  2705. u32 usbcfg;
  2706. u32 dcfg = 0;
  2707. /* Kill any ep0 requests as controller will be reinitialized */
  2708. kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
  2709. if (!is_usb_reset)
  2710. if (dwc2_core_reset(hsotg, true))
  2711. return;
  2712. /*
  2713. * we must now enable ep0 ready for host detection and then
  2714. * set configuration.
  2715. */
  2716. /* keep other bits untouched (so e.g. forced modes are not lost) */
  2717. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  2718. usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
  2719. GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
  2720. if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS &&
  2721. (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
  2722. hsotg->params.speed == DWC2_SPEED_PARAM_LOW)) {
  2723. /* FS/LS Dedicated Transceiver Interface */
  2724. usbcfg |= GUSBCFG_PHYSEL;
  2725. } else {
  2726. /* set the PLL on, remove the HNP/SRP and set the PHY */
  2727. val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
  2728. usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
  2729. (val << GUSBCFG_USBTRDTIM_SHIFT);
  2730. }
  2731. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  2732. dwc2_hsotg_init_fifo(hsotg);
  2733. if (!is_usb_reset)
  2734. dwc2_set_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
  2735. dcfg |= DCFG_EPMISCNT(1);
  2736. switch (hsotg->params.speed) {
  2737. case DWC2_SPEED_PARAM_LOW:
  2738. dcfg |= DCFG_DEVSPD_LS;
  2739. break;
  2740. case DWC2_SPEED_PARAM_FULL:
  2741. if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
  2742. dcfg |= DCFG_DEVSPD_FS48;
  2743. else
  2744. dcfg |= DCFG_DEVSPD_FS;
  2745. break;
  2746. default:
  2747. dcfg |= DCFG_DEVSPD_HS;
  2748. }
  2749. dwc2_writel(dcfg, hsotg->regs + DCFG);
  2750. /* Clear any pending OTG interrupts */
  2751. dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
  2752. /* Clear any pending interrupts */
  2753. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  2754. intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
  2755. GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
  2756. GINTSTS_USBRST | GINTSTS_RESETDET |
  2757. GINTSTS_ENUMDONE | GINTSTS_OTGINT |
  2758. GINTSTS_USBSUSP | GINTSTS_WKUPINT |
  2759. GINTSTS_LPMTRANRCVD;
  2760. if (!using_desc_dma(hsotg))
  2761. intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
  2762. if (!hsotg->params.external_id_pin_ctl)
  2763. intmsk |= GINTSTS_CONIDSTSCHNG;
  2764. dwc2_writel(intmsk, hsotg->regs + GINTMSK);
  2765. if (using_dma(hsotg)) {
  2766. dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
  2767. hsotg->params.ahbcfg,
  2768. hsotg->regs + GAHBCFG);
  2769. /* Set DDMA mode support in the core if needed */
  2770. if (using_desc_dma(hsotg))
  2771. dwc2_set_bit(hsotg->regs + DCFG, DCFG_DESCDMA_EN);
  2772. } else {
  2773. dwc2_writel(((hsotg->dedicated_fifos) ?
  2774. (GAHBCFG_NP_TXF_EMP_LVL |
  2775. GAHBCFG_P_TXF_EMP_LVL) : 0) |
  2776. GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
  2777. }
  2778. /*
  2779. * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
  2780. * when we have no data to transfer. Otherwise we get being flooded by
  2781. * interrupts.
  2782. */
  2783. dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
  2784. DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
  2785. DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
  2786. DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
  2787. hsotg->regs + DIEPMSK);
  2788. /*
  2789. * don't need XferCompl, we get that from RXFIFO in slave mode. In
  2790. * DMA mode we may need this and StsPhseRcvd.
  2791. */
  2792. dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
  2793. DOEPMSK_STSPHSERCVDMSK) : 0) |
  2794. DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
  2795. DOEPMSK_SETUPMSK,
  2796. hsotg->regs + DOEPMSK);
  2797. /* Enable BNA interrupt for DDMA */
  2798. if (using_desc_dma(hsotg))
  2799. dwc2_set_bit(hsotg->regs + DOEPMSK, DOEPMSK_BNAMSK);
  2800. dwc2_writel(0, hsotg->regs + DAINTMSK);
  2801. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  2802. dwc2_readl(hsotg->regs + DIEPCTL0),
  2803. dwc2_readl(hsotg->regs + DOEPCTL0));
  2804. /* enable in and out endpoint interrupts */
  2805. dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
  2806. /*
  2807. * Enable the RXFIFO when in slave mode, as this is how we collect
  2808. * the data. In DMA mode, we get events from the FIFO but also
  2809. * things we cannot process, so do not use it.
  2810. */
  2811. if (!using_dma(hsotg))
  2812. dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
  2813. /* Enable interrupts for EP0 in and out */
  2814. dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
  2815. dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
  2816. if (!is_usb_reset) {
  2817. dwc2_set_bit(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
  2818. udelay(10); /* see openiboot */
  2819. dwc2_clear_bit(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
  2820. }
  2821. dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
  2822. /*
  2823. * DxEPCTL_USBActEp says RO in manual, but seems to be set by
  2824. * writing to the EPCTL register..
  2825. */
  2826. /* set to read 1 8byte packet */
  2827. dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
  2828. DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
  2829. dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
  2830. DXEPCTL_CNAK | DXEPCTL_EPENA |
  2831. DXEPCTL_USBACTEP,
  2832. hsotg->regs + DOEPCTL0);
  2833. /* enable, but don't activate EP0in */
  2834. dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
  2835. DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
  2836. /* clear global NAKs */
  2837. val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
  2838. if (!is_usb_reset)
  2839. val |= DCTL_SFTDISCON;
  2840. dwc2_set_bit(hsotg->regs + DCTL, val);
  2841. /* configure the core to support LPM */
  2842. dwc2_gadget_init_lpm(hsotg);
  2843. /* must be at-least 3ms to allow bus to see disconnect */
  2844. mdelay(3);
  2845. hsotg->lx_state = DWC2_L0;
  2846. dwc2_hsotg_enqueue_setup(hsotg);
  2847. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  2848. dwc2_readl(hsotg->regs + DIEPCTL0),
  2849. dwc2_readl(hsotg->regs + DOEPCTL0));
  2850. }
  2851. static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
  2852. {
  2853. /* set the soft-disconnect bit */
  2854. dwc2_set_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
  2855. }
  2856. void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
  2857. {
  2858. /* remove the soft-disconnect and let's go */
  2859. dwc2_clear_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
  2860. }
  2861. /**
  2862. * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
  2863. * @hsotg: The device state:
  2864. *
  2865. * This interrupt indicates one of the following conditions occurred while
  2866. * transmitting an ISOC transaction.
  2867. * - Corrupted IN Token for ISOC EP.
  2868. * - Packet not complete in FIFO.
  2869. *
  2870. * The following actions will be taken:
  2871. * - Determine the EP
  2872. * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
  2873. */
  2874. static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
  2875. {
  2876. struct dwc2_hsotg_ep *hs_ep;
  2877. u32 epctrl;
  2878. u32 daintmsk;
  2879. u32 idx;
  2880. dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
  2881. daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
  2882. for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
  2883. hs_ep = hsotg->eps_in[idx];
  2884. /* Proceed only unmasked ISOC EPs */
  2885. if (!hs_ep->isochronous || (BIT(idx) & ~daintmsk))
  2886. continue;
  2887. epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
  2888. if ((epctrl & DXEPCTL_EPENA) &&
  2889. dwc2_gadget_target_frame_elapsed(hs_ep)) {
  2890. epctrl |= DXEPCTL_SNAK;
  2891. epctrl |= DXEPCTL_EPDIS;
  2892. dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
  2893. }
  2894. }
  2895. /* Clear interrupt */
  2896. dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
  2897. }
  2898. /**
  2899. * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
  2900. * @hsotg: The device state:
  2901. *
  2902. * This interrupt indicates one of the following conditions occurred while
  2903. * transmitting an ISOC transaction.
  2904. * - Corrupted OUT Token for ISOC EP.
  2905. * - Packet not complete in FIFO.
  2906. *
  2907. * The following actions will be taken:
  2908. * - Determine the EP
  2909. * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
  2910. */
  2911. static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
  2912. {
  2913. u32 gintsts;
  2914. u32 gintmsk;
  2915. u32 daintmsk;
  2916. u32 epctrl;
  2917. struct dwc2_hsotg_ep *hs_ep;
  2918. int idx;
  2919. dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
  2920. daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
  2921. daintmsk >>= DAINT_OUTEP_SHIFT;
  2922. for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
  2923. hs_ep = hsotg->eps_out[idx];
  2924. /* Proceed only unmasked ISOC EPs */
  2925. if (!hs_ep->isochronous || (BIT(idx) & ~daintmsk))
  2926. continue;
  2927. epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
  2928. if ((epctrl & DXEPCTL_EPENA) &&
  2929. dwc2_gadget_target_frame_elapsed(hs_ep)) {
  2930. /* Unmask GOUTNAKEFF interrupt */
  2931. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  2932. gintmsk |= GINTSTS_GOUTNAKEFF;
  2933. dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
  2934. gintsts = dwc2_readl(hsotg->regs + GINTSTS);
  2935. if (!(gintsts & GINTSTS_GOUTNAKEFF)) {
  2936. dwc2_set_bit(hsotg->regs + DCTL, DCTL_SGOUTNAK);
  2937. break;
  2938. }
  2939. }
  2940. }
  2941. /* Clear interrupt */
  2942. dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
  2943. }
  2944. /**
  2945. * dwc2_hsotg_irq - handle device interrupt
  2946. * @irq: The IRQ number triggered
  2947. * @pw: The pw value when registered the handler.
  2948. */
  2949. static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
  2950. {
  2951. struct dwc2_hsotg *hsotg = pw;
  2952. int retry_count = 8;
  2953. u32 gintsts;
  2954. u32 gintmsk;
  2955. if (!dwc2_is_device_mode(hsotg))
  2956. return IRQ_NONE;
  2957. spin_lock(&hsotg->lock);
  2958. irq_retry:
  2959. gintsts = dwc2_readl(hsotg->regs + GINTSTS);
  2960. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  2961. dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
  2962. __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
  2963. gintsts &= gintmsk;
  2964. if (gintsts & GINTSTS_RESETDET) {
  2965. dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
  2966. dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
  2967. /* This event must be used only if controller is suspended */
  2968. if (hsotg->lx_state == DWC2_L2) {
  2969. dwc2_exit_partial_power_down(hsotg, true);
  2970. hsotg->lx_state = DWC2_L0;
  2971. }
  2972. }
  2973. if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
  2974. u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
  2975. u32 connected = hsotg->connected;
  2976. dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
  2977. dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
  2978. dwc2_readl(hsotg->regs + GNPTXSTS));
  2979. dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
  2980. /* Report disconnection if it is not already done. */
  2981. dwc2_hsotg_disconnect(hsotg);
  2982. /* Reset device address to zero */
  2983. dwc2_clear_bit(hsotg->regs + DCFG, DCFG_DEVADDR_MASK);
  2984. if (usb_status & GOTGCTL_BSESVLD && connected)
  2985. dwc2_hsotg_core_init_disconnected(hsotg, true);
  2986. }
  2987. if (gintsts & GINTSTS_ENUMDONE) {
  2988. dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
  2989. dwc2_hsotg_irq_enumdone(hsotg);
  2990. }
  2991. if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
  2992. u32 daint = dwc2_readl(hsotg->regs + DAINT);
  2993. u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
  2994. u32 daint_out, daint_in;
  2995. int ep;
  2996. daint &= daintmsk;
  2997. daint_out = daint >> DAINT_OUTEP_SHIFT;
  2998. daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
  2999. dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
  3000. for (ep = 0; ep < hsotg->num_of_eps && daint_out;
  3001. ep++, daint_out >>= 1) {
  3002. if (daint_out & 1)
  3003. dwc2_hsotg_epint(hsotg, ep, 0);
  3004. }
  3005. for (ep = 0; ep < hsotg->num_of_eps && daint_in;
  3006. ep++, daint_in >>= 1) {
  3007. if (daint_in & 1)
  3008. dwc2_hsotg_epint(hsotg, ep, 1);
  3009. }
  3010. }
  3011. /* check both FIFOs */
  3012. if (gintsts & GINTSTS_NPTXFEMP) {
  3013. dev_dbg(hsotg->dev, "NPTxFEmp\n");
  3014. /*
  3015. * Disable the interrupt to stop it happening again
  3016. * unless one of these endpoint routines decides that
  3017. * it needs re-enabling
  3018. */
  3019. dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
  3020. dwc2_hsotg_irq_fifoempty(hsotg, false);
  3021. }
  3022. if (gintsts & GINTSTS_PTXFEMP) {
  3023. dev_dbg(hsotg->dev, "PTxFEmp\n");
  3024. /* See note in GINTSTS_NPTxFEmp */
  3025. dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
  3026. dwc2_hsotg_irq_fifoempty(hsotg, true);
  3027. }
  3028. if (gintsts & GINTSTS_RXFLVL) {
  3029. /*
  3030. * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
  3031. * we need to retry dwc2_hsotg_handle_rx if this is still
  3032. * set.
  3033. */
  3034. dwc2_hsotg_handle_rx(hsotg);
  3035. }
  3036. if (gintsts & GINTSTS_ERLYSUSP) {
  3037. dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
  3038. dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
  3039. }
  3040. /*
  3041. * these next two seem to crop-up occasionally causing the core
  3042. * to shutdown the USB transfer, so try clearing them and logging
  3043. * the occurrence.
  3044. */
  3045. if (gintsts & GINTSTS_GOUTNAKEFF) {
  3046. u8 idx;
  3047. u32 epctrl;
  3048. u32 gintmsk;
  3049. u32 daintmsk;
  3050. struct dwc2_hsotg_ep *hs_ep;
  3051. daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
  3052. daintmsk >>= DAINT_OUTEP_SHIFT;
  3053. /* Mask this interrupt */
  3054. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  3055. gintmsk &= ~GINTSTS_GOUTNAKEFF;
  3056. dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
  3057. dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
  3058. for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
  3059. hs_ep = hsotg->eps_out[idx];
  3060. /* Proceed only unmasked ISOC EPs */
  3061. if (!hs_ep->isochronous || (BIT(idx) & ~daintmsk))
  3062. continue;
  3063. epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
  3064. if (epctrl & DXEPCTL_EPENA) {
  3065. epctrl |= DXEPCTL_SNAK;
  3066. epctrl |= DXEPCTL_EPDIS;
  3067. dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
  3068. }
  3069. }
  3070. /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
  3071. }
  3072. if (gintsts & GINTSTS_GINNAKEFF) {
  3073. dev_info(hsotg->dev, "GINNakEff triggered\n");
  3074. dwc2_set_bit(hsotg->regs + DCTL, DCTL_CGNPINNAK);
  3075. dwc2_hsotg_dump(hsotg);
  3076. }
  3077. if (gintsts & GINTSTS_INCOMPL_SOIN)
  3078. dwc2_gadget_handle_incomplete_isoc_in(hsotg);
  3079. if (gintsts & GINTSTS_INCOMPL_SOOUT)
  3080. dwc2_gadget_handle_incomplete_isoc_out(hsotg);
  3081. /*
  3082. * if we've had fifo events, we should try and go around the
  3083. * loop again to see if there's any point in returning yet.
  3084. */
  3085. if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
  3086. goto irq_retry;
  3087. spin_unlock(&hsotg->lock);
  3088. return IRQ_HANDLED;
  3089. }
  3090. static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
  3091. struct dwc2_hsotg_ep *hs_ep)
  3092. {
  3093. u32 epctrl_reg;
  3094. u32 epint_reg;
  3095. epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
  3096. DOEPCTL(hs_ep->index);
  3097. epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
  3098. DOEPINT(hs_ep->index);
  3099. dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
  3100. hs_ep->name);
  3101. if (hs_ep->dir_in) {
  3102. if (hsotg->dedicated_fifos || hs_ep->periodic) {
  3103. dwc2_set_bit(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
  3104. /* Wait for Nak effect */
  3105. if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
  3106. DXEPINT_INEPNAKEFF, 100))
  3107. dev_warn(hsotg->dev,
  3108. "%s: timeout DIEPINT.NAKEFF\n",
  3109. __func__);
  3110. } else {
  3111. dwc2_set_bit(hsotg->regs + DCTL, DCTL_SGNPINNAK);
  3112. /* Wait for Nak effect */
  3113. if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
  3114. GINTSTS_GINNAKEFF, 100))
  3115. dev_warn(hsotg->dev,
  3116. "%s: timeout GINTSTS.GINNAKEFF\n",
  3117. __func__);
  3118. }
  3119. } else {
  3120. if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
  3121. dwc2_set_bit(hsotg->regs + DCTL, DCTL_SGOUTNAK);
  3122. /* Wait for global nak to take effect */
  3123. if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
  3124. GINTSTS_GOUTNAKEFF, 100))
  3125. dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
  3126. __func__);
  3127. }
  3128. /* Disable ep */
  3129. dwc2_set_bit(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
  3130. /* Wait for ep to be disabled */
  3131. if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
  3132. dev_warn(hsotg->dev,
  3133. "%s: timeout DOEPCTL.EPDisable\n", __func__);
  3134. /* Clear EPDISBLD interrupt */
  3135. dwc2_set_bit(hsotg->regs + epint_reg, DXEPINT_EPDISBLD);
  3136. if (hs_ep->dir_in) {
  3137. unsigned short fifo_index;
  3138. if (hsotg->dedicated_fifos || hs_ep->periodic)
  3139. fifo_index = hs_ep->fifo_index;
  3140. else
  3141. fifo_index = 0;
  3142. /* Flush TX FIFO */
  3143. dwc2_flush_tx_fifo(hsotg, fifo_index);
  3144. /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
  3145. if (!hsotg->dedicated_fifos && !hs_ep->periodic)
  3146. dwc2_set_bit(hsotg->regs + DCTL, DCTL_CGNPINNAK);
  3147. } else {
  3148. /* Remove global NAKs */
  3149. dwc2_set_bit(hsotg->regs + DCTL, DCTL_CGOUTNAK);
  3150. }
  3151. }
  3152. /**
  3153. * dwc2_hsotg_ep_enable - enable the given endpoint
  3154. * @ep: The USB endpint to configure
  3155. * @desc: The USB endpoint descriptor to configure with.
  3156. *
  3157. * This is called from the USB gadget code's usb_ep_enable().
  3158. */
  3159. static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
  3160. const struct usb_endpoint_descriptor *desc)
  3161. {
  3162. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3163. struct dwc2_hsotg *hsotg = hs_ep->parent;
  3164. unsigned long flags;
  3165. unsigned int index = hs_ep->index;
  3166. u32 epctrl_reg;
  3167. u32 epctrl;
  3168. u32 mps;
  3169. u32 mc;
  3170. u32 mask;
  3171. unsigned int dir_in;
  3172. unsigned int i, val, size;
  3173. int ret = 0;
  3174. dev_dbg(hsotg->dev,
  3175. "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
  3176. __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
  3177. desc->wMaxPacketSize, desc->bInterval);
  3178. /* not to be called for EP0 */
  3179. if (index == 0) {
  3180. dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
  3181. return -EINVAL;
  3182. }
  3183. dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
  3184. if (dir_in != hs_ep->dir_in) {
  3185. dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
  3186. return -EINVAL;
  3187. }
  3188. mps = usb_endpoint_maxp(desc);
  3189. mc = usb_endpoint_maxp_mult(desc);
  3190. /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
  3191. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  3192. epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
  3193. dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
  3194. __func__, epctrl, epctrl_reg);
  3195. /* Allocate DMA descriptor chain for non-ctrl endpoints */
  3196. if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
  3197. hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
  3198. MAX_DMA_DESC_NUM_GENERIC *
  3199. sizeof(struct dwc2_dma_desc),
  3200. &hs_ep->desc_list_dma, GFP_ATOMIC);
  3201. if (!hs_ep->desc_list) {
  3202. ret = -ENOMEM;
  3203. goto error2;
  3204. }
  3205. }
  3206. spin_lock_irqsave(&hsotg->lock, flags);
  3207. epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
  3208. epctrl |= DXEPCTL_MPS(mps);
  3209. /*
  3210. * mark the endpoint as active, otherwise the core may ignore
  3211. * transactions entirely for this endpoint
  3212. */
  3213. epctrl |= DXEPCTL_USBACTEP;
  3214. /* update the endpoint state */
  3215. dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
  3216. /* default, set to non-periodic */
  3217. hs_ep->isochronous = 0;
  3218. hs_ep->periodic = 0;
  3219. hs_ep->halted = 0;
  3220. hs_ep->interval = desc->bInterval;
  3221. switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  3222. case USB_ENDPOINT_XFER_ISOC:
  3223. epctrl |= DXEPCTL_EPTYPE_ISO;
  3224. epctrl |= DXEPCTL_SETEVENFR;
  3225. hs_ep->isochronous = 1;
  3226. hs_ep->interval = 1 << (desc->bInterval - 1);
  3227. hs_ep->target_frame = TARGET_FRAME_INITIAL;
  3228. hs_ep->isoc_chain_num = 0;
  3229. hs_ep->next_desc = 0;
  3230. if (dir_in) {
  3231. hs_ep->periodic = 1;
  3232. mask = dwc2_readl(hsotg->regs + DIEPMSK);
  3233. mask |= DIEPMSK_NAKMSK;
  3234. dwc2_writel(mask, hsotg->regs + DIEPMSK);
  3235. } else {
  3236. mask = dwc2_readl(hsotg->regs + DOEPMSK);
  3237. mask |= DOEPMSK_OUTTKNEPDISMSK;
  3238. dwc2_writel(mask, hsotg->regs + DOEPMSK);
  3239. }
  3240. break;
  3241. case USB_ENDPOINT_XFER_BULK:
  3242. epctrl |= DXEPCTL_EPTYPE_BULK;
  3243. break;
  3244. case USB_ENDPOINT_XFER_INT:
  3245. if (dir_in)
  3246. hs_ep->periodic = 1;
  3247. if (hsotg->gadget.speed == USB_SPEED_HIGH)
  3248. hs_ep->interval = 1 << (desc->bInterval - 1);
  3249. epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
  3250. break;
  3251. case USB_ENDPOINT_XFER_CONTROL:
  3252. epctrl |= DXEPCTL_EPTYPE_CONTROL;
  3253. break;
  3254. }
  3255. /*
  3256. * if the hardware has dedicated fifos, we must give each IN EP
  3257. * a unique tx-fifo even if it is non-periodic.
  3258. */
  3259. if (dir_in && hsotg->dedicated_fifos) {
  3260. u32 fifo_index = 0;
  3261. u32 fifo_size = UINT_MAX;
  3262. size = hs_ep->ep.maxpacket * hs_ep->mc;
  3263. for (i = 1; i < hsotg->num_of_eps; ++i) {
  3264. if (hsotg->fifo_map & (1 << i))
  3265. continue;
  3266. val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
  3267. val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4;
  3268. if (val < size)
  3269. continue;
  3270. /* Search for smallest acceptable fifo */
  3271. if (val < fifo_size) {
  3272. fifo_size = val;
  3273. fifo_index = i;
  3274. }
  3275. }
  3276. if (!fifo_index) {
  3277. dev_err(hsotg->dev,
  3278. "%s: No suitable fifo found\n", __func__);
  3279. ret = -ENOMEM;
  3280. goto error1;
  3281. }
  3282. hsotg->fifo_map |= 1 << fifo_index;
  3283. epctrl |= DXEPCTL_TXFNUM(fifo_index);
  3284. hs_ep->fifo_index = fifo_index;
  3285. hs_ep->fifo_size = fifo_size;
  3286. }
  3287. /* for non control endpoints, set PID to D0 */
  3288. if (index && !hs_ep->isochronous)
  3289. epctrl |= DXEPCTL_SETD0PID;
  3290. /* WA for Full speed ISOC IN in DDMA mode.
  3291. * By Clear NAK status of EP, core will send ZLP
  3292. * to IN token and assert NAK interrupt relying
  3293. * on TxFIFO status only
  3294. */
  3295. if (hsotg->gadget.speed == USB_SPEED_FULL &&
  3296. hs_ep->isochronous && dir_in) {
  3297. /* The WA applies only to core versions from 2.72a
  3298. * to 4.00a (including both). Also for FS_IOT_1.00a
  3299. * and HS_IOT_1.00a.
  3300. */
  3301. u32 gsnpsid = dwc2_readl(hsotg->regs + GSNPSID);
  3302. if ((gsnpsid >= DWC2_CORE_REV_2_72a &&
  3303. gsnpsid <= DWC2_CORE_REV_4_00a) ||
  3304. gsnpsid == DWC2_FS_IOT_REV_1_00a ||
  3305. gsnpsid == DWC2_HS_IOT_REV_1_00a)
  3306. epctrl |= DXEPCTL_CNAK;
  3307. }
  3308. dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
  3309. __func__, epctrl);
  3310. dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
  3311. dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
  3312. __func__, dwc2_readl(hsotg->regs + epctrl_reg));
  3313. /* enable the endpoint interrupt */
  3314. dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
  3315. error1:
  3316. spin_unlock_irqrestore(&hsotg->lock, flags);
  3317. error2:
  3318. if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
  3319. dmam_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
  3320. sizeof(struct dwc2_dma_desc),
  3321. hs_ep->desc_list, hs_ep->desc_list_dma);
  3322. hs_ep->desc_list = NULL;
  3323. }
  3324. return ret;
  3325. }
  3326. /**
  3327. * dwc2_hsotg_ep_disable - disable given endpoint
  3328. * @ep: The endpoint to disable.
  3329. */
  3330. static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
  3331. {
  3332. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3333. struct dwc2_hsotg *hsotg = hs_ep->parent;
  3334. int dir_in = hs_ep->dir_in;
  3335. int index = hs_ep->index;
  3336. unsigned long flags;
  3337. u32 epctrl_reg;
  3338. u32 ctrl;
  3339. dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
  3340. if (ep == &hsotg->eps_out[0]->ep) {
  3341. dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
  3342. return -EINVAL;
  3343. }
  3344. if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
  3345. dev_err(hsotg->dev, "%s: called in host mode?\n", __func__);
  3346. return -EINVAL;
  3347. }
  3348. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  3349. spin_lock_irqsave(&hsotg->lock, flags);
  3350. ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
  3351. if (ctrl & DXEPCTL_EPENA)
  3352. dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
  3353. ctrl &= ~DXEPCTL_EPENA;
  3354. ctrl &= ~DXEPCTL_USBACTEP;
  3355. ctrl |= DXEPCTL_SNAK;
  3356. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
  3357. dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
  3358. /* disable endpoint interrupts */
  3359. dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
  3360. /* terminate all requests with shutdown */
  3361. kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
  3362. hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
  3363. hs_ep->fifo_index = 0;
  3364. hs_ep->fifo_size = 0;
  3365. spin_unlock_irqrestore(&hsotg->lock, flags);
  3366. return 0;
  3367. }
  3368. /**
  3369. * on_list - check request is on the given endpoint
  3370. * @ep: The endpoint to check.
  3371. * @test: The request to test if it is on the endpoint.
  3372. */
  3373. static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
  3374. {
  3375. struct dwc2_hsotg_req *req, *treq;
  3376. list_for_each_entry_safe(req, treq, &ep->queue, queue) {
  3377. if (req == test)
  3378. return true;
  3379. }
  3380. return false;
  3381. }
  3382. /**
  3383. * dwc2_hsotg_ep_dequeue - dequeue given endpoint
  3384. * @ep: The endpoint to dequeue.
  3385. * @req: The request to be removed from a queue.
  3386. */
  3387. static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  3388. {
  3389. struct dwc2_hsotg_req *hs_req = our_req(req);
  3390. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3391. struct dwc2_hsotg *hs = hs_ep->parent;
  3392. unsigned long flags;
  3393. dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
  3394. spin_lock_irqsave(&hs->lock, flags);
  3395. if (!on_list(hs_ep, hs_req)) {
  3396. spin_unlock_irqrestore(&hs->lock, flags);
  3397. return -EINVAL;
  3398. }
  3399. /* Dequeue already started request */
  3400. if (req == &hs_ep->req->req)
  3401. dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
  3402. dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
  3403. spin_unlock_irqrestore(&hs->lock, flags);
  3404. return 0;
  3405. }
  3406. /**
  3407. * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
  3408. * @ep: The endpoint to set halt.
  3409. * @value: Set or unset the halt.
  3410. * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
  3411. * the endpoint is busy processing requests.
  3412. *
  3413. * We need to stall the endpoint immediately if request comes from set_feature
  3414. * protocol command handler.
  3415. */
  3416. static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
  3417. {
  3418. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3419. struct dwc2_hsotg *hs = hs_ep->parent;
  3420. int index = hs_ep->index;
  3421. u32 epreg;
  3422. u32 epctl;
  3423. u32 xfertype;
  3424. dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
  3425. if (index == 0) {
  3426. if (value)
  3427. dwc2_hsotg_stall_ep0(hs);
  3428. else
  3429. dev_warn(hs->dev,
  3430. "%s: can't clear halt on ep0\n", __func__);
  3431. return 0;
  3432. }
  3433. if (hs_ep->isochronous) {
  3434. dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
  3435. return -EINVAL;
  3436. }
  3437. if (!now && value && !list_empty(&hs_ep->queue)) {
  3438. dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
  3439. ep->name);
  3440. return -EAGAIN;
  3441. }
  3442. if (hs_ep->dir_in) {
  3443. epreg = DIEPCTL(index);
  3444. epctl = dwc2_readl(hs->regs + epreg);
  3445. if (value) {
  3446. epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
  3447. if (epctl & DXEPCTL_EPENA)
  3448. epctl |= DXEPCTL_EPDIS;
  3449. } else {
  3450. epctl &= ~DXEPCTL_STALL;
  3451. xfertype = epctl & DXEPCTL_EPTYPE_MASK;
  3452. if (xfertype == DXEPCTL_EPTYPE_BULK ||
  3453. xfertype == DXEPCTL_EPTYPE_INTERRUPT)
  3454. epctl |= DXEPCTL_SETD0PID;
  3455. }
  3456. dwc2_writel(epctl, hs->regs + epreg);
  3457. } else {
  3458. epreg = DOEPCTL(index);
  3459. epctl = dwc2_readl(hs->regs + epreg);
  3460. if (value) {
  3461. epctl |= DXEPCTL_STALL;
  3462. } else {
  3463. epctl &= ~DXEPCTL_STALL;
  3464. xfertype = epctl & DXEPCTL_EPTYPE_MASK;
  3465. if (xfertype == DXEPCTL_EPTYPE_BULK ||
  3466. xfertype == DXEPCTL_EPTYPE_INTERRUPT)
  3467. epctl |= DXEPCTL_SETD0PID;
  3468. }
  3469. dwc2_writel(epctl, hs->regs + epreg);
  3470. }
  3471. hs_ep->halted = value;
  3472. return 0;
  3473. }
  3474. /**
  3475. * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
  3476. * @ep: The endpoint to set halt.
  3477. * @value: Set or unset the halt.
  3478. */
  3479. static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
  3480. {
  3481. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3482. struct dwc2_hsotg *hs = hs_ep->parent;
  3483. unsigned long flags = 0;
  3484. int ret = 0;
  3485. spin_lock_irqsave(&hs->lock, flags);
  3486. ret = dwc2_hsotg_ep_sethalt(ep, value, false);
  3487. spin_unlock_irqrestore(&hs->lock, flags);
  3488. return ret;
  3489. }
  3490. static const struct usb_ep_ops dwc2_hsotg_ep_ops = {
  3491. .enable = dwc2_hsotg_ep_enable,
  3492. .disable = dwc2_hsotg_ep_disable,
  3493. .alloc_request = dwc2_hsotg_ep_alloc_request,
  3494. .free_request = dwc2_hsotg_ep_free_request,
  3495. .queue = dwc2_hsotg_ep_queue_lock,
  3496. .dequeue = dwc2_hsotg_ep_dequeue,
  3497. .set_halt = dwc2_hsotg_ep_sethalt_lock,
  3498. /* note, don't believe we have any call for the fifo routines */
  3499. };
  3500. /**
  3501. * dwc2_hsotg_init - initialize the usb core
  3502. * @hsotg: The driver state
  3503. */
  3504. static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
  3505. {
  3506. u32 trdtim;
  3507. u32 usbcfg;
  3508. /* unmask subset of endpoint interrupts */
  3509. dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
  3510. DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
  3511. hsotg->regs + DIEPMSK);
  3512. dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
  3513. DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
  3514. hsotg->regs + DOEPMSK);
  3515. dwc2_writel(0, hsotg->regs + DAINTMSK);
  3516. /* Be in disconnected state until gadget is registered */
  3517. dwc2_set_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
  3518. /* setup fifos */
  3519. dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
  3520. dwc2_readl(hsotg->regs + GRXFSIZ),
  3521. dwc2_readl(hsotg->regs + GNPTXFSIZ));
  3522. dwc2_hsotg_init_fifo(hsotg);
  3523. /* keep other bits untouched (so e.g. forced modes are not lost) */
  3524. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  3525. usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
  3526. GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
  3527. /* set the PLL on, remove the HNP/SRP and set the PHY */
  3528. trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
  3529. usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
  3530. (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
  3531. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  3532. if (using_dma(hsotg))
  3533. dwc2_set_bit(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
  3534. }
  3535. /**
  3536. * dwc2_hsotg_udc_start - prepare the udc for work
  3537. * @gadget: The usb gadget state
  3538. * @driver: The usb gadget driver
  3539. *
  3540. * Perform initialization to prepare udc device and driver
  3541. * to work.
  3542. */
  3543. static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
  3544. struct usb_gadget_driver *driver)
  3545. {
  3546. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3547. unsigned long flags;
  3548. int ret;
  3549. if (!hsotg) {
  3550. pr_err("%s: called with no device\n", __func__);
  3551. return -ENODEV;
  3552. }
  3553. if (!driver) {
  3554. dev_err(hsotg->dev, "%s: no driver\n", __func__);
  3555. return -EINVAL;
  3556. }
  3557. if (driver->max_speed < USB_SPEED_FULL)
  3558. dev_err(hsotg->dev, "%s: bad speed\n", __func__);
  3559. if (!driver->setup) {
  3560. dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
  3561. return -EINVAL;
  3562. }
  3563. WARN_ON(hsotg->driver);
  3564. driver->driver.bus = NULL;
  3565. hsotg->driver = driver;
  3566. hsotg->gadget.dev.of_node = hsotg->dev->of_node;
  3567. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  3568. if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
  3569. ret = dwc2_lowlevel_hw_enable(hsotg);
  3570. if (ret)
  3571. goto err;
  3572. }
  3573. if (!IS_ERR_OR_NULL(hsotg->uphy))
  3574. otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
  3575. spin_lock_irqsave(&hsotg->lock, flags);
  3576. if (dwc2_hw_is_device(hsotg)) {
  3577. dwc2_hsotg_init(hsotg);
  3578. dwc2_hsotg_core_init_disconnected(hsotg, false);
  3579. }
  3580. hsotg->enabled = 0;
  3581. spin_unlock_irqrestore(&hsotg->lock, flags);
  3582. dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
  3583. return 0;
  3584. err:
  3585. hsotg->driver = NULL;
  3586. return ret;
  3587. }
  3588. /**
  3589. * dwc2_hsotg_udc_stop - stop the udc
  3590. * @gadget: The usb gadget state
  3591. * @driver: The usb gadget driver
  3592. *
  3593. * Stop udc hw block and stay tunned for future transmissions
  3594. */
  3595. static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
  3596. {
  3597. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3598. unsigned long flags = 0;
  3599. int ep;
  3600. if (!hsotg)
  3601. return -ENODEV;
  3602. /* all endpoints should be shutdown */
  3603. for (ep = 1; ep < hsotg->num_of_eps; ep++) {
  3604. if (hsotg->eps_in[ep])
  3605. dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
  3606. if (hsotg->eps_out[ep])
  3607. dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
  3608. }
  3609. spin_lock_irqsave(&hsotg->lock, flags);
  3610. hsotg->driver = NULL;
  3611. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  3612. hsotg->enabled = 0;
  3613. spin_unlock_irqrestore(&hsotg->lock, flags);
  3614. if (!IS_ERR_OR_NULL(hsotg->uphy))
  3615. otg_set_peripheral(hsotg->uphy->otg, NULL);
  3616. if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
  3617. dwc2_lowlevel_hw_disable(hsotg);
  3618. return 0;
  3619. }
  3620. /**
  3621. * dwc2_hsotg_gadget_getframe - read the frame number
  3622. * @gadget: The usb gadget state
  3623. *
  3624. * Read the {micro} frame number
  3625. */
  3626. static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
  3627. {
  3628. return dwc2_hsotg_read_frameno(to_hsotg(gadget));
  3629. }
  3630. /**
  3631. * dwc2_hsotg_pullup - connect/disconnect the USB PHY
  3632. * @gadget: The usb gadget state
  3633. * @is_on: Current state of the USB PHY
  3634. *
  3635. * Connect/Disconnect the USB PHY pullup
  3636. */
  3637. static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
  3638. {
  3639. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3640. unsigned long flags = 0;
  3641. dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
  3642. hsotg->op_state);
  3643. /* Don't modify pullup state while in host mode */
  3644. if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
  3645. hsotg->enabled = is_on;
  3646. return 0;
  3647. }
  3648. spin_lock_irqsave(&hsotg->lock, flags);
  3649. if (is_on) {
  3650. hsotg->enabled = 1;
  3651. dwc2_hsotg_core_init_disconnected(hsotg, false);
  3652. /* Enable ACG feature in device mode,if supported */
  3653. dwc2_enable_acg(hsotg);
  3654. dwc2_hsotg_core_connect(hsotg);
  3655. } else {
  3656. dwc2_hsotg_core_disconnect(hsotg);
  3657. dwc2_hsotg_disconnect(hsotg);
  3658. hsotg->enabled = 0;
  3659. }
  3660. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  3661. spin_unlock_irqrestore(&hsotg->lock, flags);
  3662. return 0;
  3663. }
  3664. static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
  3665. {
  3666. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3667. unsigned long flags;
  3668. dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
  3669. spin_lock_irqsave(&hsotg->lock, flags);
  3670. /*
  3671. * If controller is hibernated, it must exit from power_down
  3672. * before being initialized / de-initialized
  3673. */
  3674. if (hsotg->lx_state == DWC2_L2)
  3675. dwc2_exit_partial_power_down(hsotg, false);
  3676. if (is_active) {
  3677. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  3678. dwc2_hsotg_core_init_disconnected(hsotg, false);
  3679. if (hsotg->enabled) {
  3680. /* Enable ACG feature in device mode,if supported */
  3681. dwc2_enable_acg(hsotg);
  3682. dwc2_hsotg_core_connect(hsotg);
  3683. }
  3684. } else {
  3685. dwc2_hsotg_core_disconnect(hsotg);
  3686. dwc2_hsotg_disconnect(hsotg);
  3687. }
  3688. spin_unlock_irqrestore(&hsotg->lock, flags);
  3689. return 0;
  3690. }
  3691. /**
  3692. * dwc2_hsotg_vbus_draw - report bMaxPower field
  3693. * @gadget: The usb gadget state
  3694. * @mA: Amount of current
  3695. *
  3696. * Report how much power the device may consume to the phy.
  3697. */
  3698. static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
  3699. {
  3700. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3701. if (IS_ERR_OR_NULL(hsotg->uphy))
  3702. return -ENOTSUPP;
  3703. return usb_phy_set_power(hsotg->uphy, mA);
  3704. }
  3705. static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
  3706. .get_frame = dwc2_hsotg_gadget_getframe,
  3707. .udc_start = dwc2_hsotg_udc_start,
  3708. .udc_stop = dwc2_hsotg_udc_stop,
  3709. .pullup = dwc2_hsotg_pullup,
  3710. .vbus_session = dwc2_hsotg_vbus_session,
  3711. .vbus_draw = dwc2_hsotg_vbus_draw,
  3712. };
  3713. /**
  3714. * dwc2_hsotg_initep - initialise a single endpoint
  3715. * @hsotg: The device state.
  3716. * @hs_ep: The endpoint to be initialised.
  3717. * @epnum: The endpoint number
  3718. *
  3719. * Initialise the given endpoint (as part of the probe and device state
  3720. * creation) to give to the gadget driver. Setup the endpoint name, any
  3721. * direction information and other state that may be required.
  3722. */
  3723. static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
  3724. struct dwc2_hsotg_ep *hs_ep,
  3725. int epnum,
  3726. bool dir_in)
  3727. {
  3728. char *dir;
  3729. if (epnum == 0)
  3730. dir = "";
  3731. else if (dir_in)
  3732. dir = "in";
  3733. else
  3734. dir = "out";
  3735. hs_ep->dir_in = dir_in;
  3736. hs_ep->index = epnum;
  3737. snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
  3738. INIT_LIST_HEAD(&hs_ep->queue);
  3739. INIT_LIST_HEAD(&hs_ep->ep.ep_list);
  3740. /* add to the list of endpoints known by the gadget driver */
  3741. if (epnum)
  3742. list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
  3743. hs_ep->parent = hsotg;
  3744. hs_ep->ep.name = hs_ep->name;
  3745. if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
  3746. usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
  3747. else
  3748. usb_ep_set_maxpacket_limit(&hs_ep->ep,
  3749. epnum ? 1024 : EP0_MPS_LIMIT);
  3750. hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
  3751. if (epnum == 0) {
  3752. hs_ep->ep.caps.type_control = true;
  3753. } else {
  3754. if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
  3755. hs_ep->ep.caps.type_iso = true;
  3756. hs_ep->ep.caps.type_bulk = true;
  3757. }
  3758. hs_ep->ep.caps.type_int = true;
  3759. }
  3760. if (dir_in)
  3761. hs_ep->ep.caps.dir_in = true;
  3762. else
  3763. hs_ep->ep.caps.dir_out = true;
  3764. /*
  3765. * if we're using dma, we need to set the next-endpoint pointer
  3766. * to be something valid.
  3767. */
  3768. if (using_dma(hsotg)) {
  3769. u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
  3770. if (dir_in)
  3771. dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
  3772. else
  3773. dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
  3774. }
  3775. }
  3776. /**
  3777. * dwc2_hsotg_hw_cfg - read HW configuration registers
  3778. * @param: The device state
  3779. *
  3780. * Read the USB core HW configuration registers
  3781. */
  3782. static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
  3783. {
  3784. u32 cfg;
  3785. u32 ep_type;
  3786. u32 i;
  3787. /* check hardware configuration */
  3788. hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
  3789. /* Add ep0 */
  3790. hsotg->num_of_eps++;
  3791. hsotg->eps_in[0] = devm_kzalloc(hsotg->dev,
  3792. sizeof(struct dwc2_hsotg_ep),
  3793. GFP_KERNEL);
  3794. if (!hsotg->eps_in[0])
  3795. return -ENOMEM;
  3796. /* Same dwc2_hsotg_ep is used in both directions for ep0 */
  3797. hsotg->eps_out[0] = hsotg->eps_in[0];
  3798. cfg = hsotg->hw_params.dev_ep_dirs;
  3799. for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
  3800. ep_type = cfg & 3;
  3801. /* Direction in or both */
  3802. if (!(ep_type & 2)) {
  3803. hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
  3804. sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
  3805. if (!hsotg->eps_in[i])
  3806. return -ENOMEM;
  3807. }
  3808. /* Direction out or both */
  3809. if (!(ep_type & 1)) {
  3810. hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
  3811. sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
  3812. if (!hsotg->eps_out[i])
  3813. return -ENOMEM;
  3814. }
  3815. }
  3816. hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
  3817. hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
  3818. dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
  3819. hsotg->num_of_eps,
  3820. hsotg->dedicated_fifos ? "dedicated" : "shared",
  3821. hsotg->fifo_mem);
  3822. return 0;
  3823. }
  3824. /**
  3825. * dwc2_hsotg_dump - dump state of the udc
  3826. * @param: The device state
  3827. */
  3828. static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
  3829. {
  3830. #ifdef DEBUG
  3831. struct device *dev = hsotg->dev;
  3832. void __iomem *regs = hsotg->regs;
  3833. u32 val;
  3834. int idx;
  3835. dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
  3836. dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
  3837. dwc2_readl(regs + DIEPMSK));
  3838. dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
  3839. dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
  3840. dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
  3841. dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
  3842. /* show periodic fifo settings */
  3843. for (idx = 1; idx < hsotg->num_of_eps; idx++) {
  3844. val = dwc2_readl(regs + DPTXFSIZN(idx));
  3845. dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
  3846. val >> FIFOSIZE_DEPTH_SHIFT,
  3847. val & FIFOSIZE_STARTADDR_MASK);
  3848. }
  3849. for (idx = 0; idx < hsotg->num_of_eps; idx++) {
  3850. dev_info(dev,
  3851. "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
  3852. dwc2_readl(regs + DIEPCTL(idx)),
  3853. dwc2_readl(regs + DIEPTSIZ(idx)),
  3854. dwc2_readl(regs + DIEPDMA(idx)));
  3855. val = dwc2_readl(regs + DOEPCTL(idx));
  3856. dev_info(dev,
  3857. "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
  3858. idx, dwc2_readl(regs + DOEPCTL(idx)),
  3859. dwc2_readl(regs + DOEPTSIZ(idx)),
  3860. dwc2_readl(regs + DOEPDMA(idx)));
  3861. }
  3862. dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
  3863. dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
  3864. #endif
  3865. }
  3866. /**
  3867. * dwc2_gadget_init - init function for gadget
  3868. * @dwc2: The data structure for the DWC2 driver.
  3869. */
  3870. int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
  3871. {
  3872. struct device *dev = hsotg->dev;
  3873. int epnum;
  3874. int ret;
  3875. /* Dump fifo information */
  3876. dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
  3877. hsotg->params.g_np_tx_fifo_size);
  3878. dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
  3879. hsotg->gadget.max_speed = USB_SPEED_HIGH;
  3880. hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
  3881. hsotg->gadget.name = dev_name(dev);
  3882. hsotg->remote_wakeup_allowed = 0;
  3883. if (hsotg->params.lpm)
  3884. hsotg->gadget.lpm_capable = true;
  3885. if (hsotg->dr_mode == USB_DR_MODE_OTG)
  3886. hsotg->gadget.is_otg = 1;
  3887. else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
  3888. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  3889. ret = dwc2_hsotg_hw_cfg(hsotg);
  3890. if (ret) {
  3891. dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
  3892. return ret;
  3893. }
  3894. hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
  3895. DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
  3896. if (!hsotg->ctrl_buff)
  3897. return -ENOMEM;
  3898. hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
  3899. DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
  3900. if (!hsotg->ep0_buff)
  3901. return -ENOMEM;
  3902. if (using_desc_dma(hsotg)) {
  3903. ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
  3904. if (ret < 0)
  3905. return ret;
  3906. }
  3907. ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq,
  3908. IRQF_SHARED, dev_name(hsotg->dev), hsotg);
  3909. if (ret < 0) {
  3910. dev_err(dev, "cannot claim IRQ for gadget\n");
  3911. return ret;
  3912. }
  3913. /* hsotg->num_of_eps holds number of EPs other than ep0 */
  3914. if (hsotg->num_of_eps == 0) {
  3915. dev_err(dev, "wrong number of EPs (zero)\n");
  3916. return -EINVAL;
  3917. }
  3918. /* setup endpoint information */
  3919. INIT_LIST_HEAD(&hsotg->gadget.ep_list);
  3920. hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
  3921. /* allocate EP0 request */
  3922. hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
  3923. GFP_KERNEL);
  3924. if (!hsotg->ctrl_req) {
  3925. dev_err(dev, "failed to allocate ctrl req\n");
  3926. return -ENOMEM;
  3927. }
  3928. /* initialise the endpoints now the core has been initialised */
  3929. for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
  3930. if (hsotg->eps_in[epnum])
  3931. dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
  3932. epnum, 1);
  3933. if (hsotg->eps_out[epnum])
  3934. dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
  3935. epnum, 0);
  3936. }
  3937. ret = usb_add_gadget_udc(dev, &hsotg->gadget);
  3938. if (ret)
  3939. return ret;
  3940. dwc2_hsotg_dump(hsotg);
  3941. return 0;
  3942. }
  3943. /**
  3944. * dwc2_hsotg_remove - remove function for hsotg driver
  3945. * @pdev: The platform information for the driver
  3946. */
  3947. int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
  3948. {
  3949. usb_del_gadget_udc(&hsotg->gadget);
  3950. return 0;
  3951. }
  3952. int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
  3953. {
  3954. unsigned long flags;
  3955. if (hsotg->lx_state != DWC2_L0)
  3956. return 0;
  3957. if (hsotg->driver) {
  3958. int ep;
  3959. dev_info(hsotg->dev, "suspending usb gadget %s\n",
  3960. hsotg->driver->driver.name);
  3961. spin_lock_irqsave(&hsotg->lock, flags);
  3962. if (hsotg->enabled)
  3963. dwc2_hsotg_core_disconnect(hsotg);
  3964. dwc2_hsotg_disconnect(hsotg);
  3965. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  3966. spin_unlock_irqrestore(&hsotg->lock, flags);
  3967. for (ep = 0; ep < hsotg->num_of_eps; ep++) {
  3968. if (hsotg->eps_in[ep])
  3969. dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
  3970. if (hsotg->eps_out[ep])
  3971. dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
  3972. }
  3973. }
  3974. return 0;
  3975. }
  3976. int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
  3977. {
  3978. unsigned long flags;
  3979. if (hsotg->lx_state == DWC2_L2)
  3980. return 0;
  3981. if (hsotg->driver) {
  3982. dev_info(hsotg->dev, "resuming usb gadget %s\n",
  3983. hsotg->driver->driver.name);
  3984. spin_lock_irqsave(&hsotg->lock, flags);
  3985. dwc2_hsotg_core_init_disconnected(hsotg, false);
  3986. if (hsotg->enabled) {
  3987. /* Enable ACG feature in device mode,if supported */
  3988. dwc2_enable_acg(hsotg);
  3989. dwc2_hsotg_core_connect(hsotg);
  3990. }
  3991. spin_unlock_irqrestore(&hsotg->lock, flags);
  3992. }
  3993. return 0;
  3994. }
  3995. /**
  3996. * dwc2_backup_device_registers() - Backup controller device registers.
  3997. * When suspending usb bus, registers needs to be backuped
  3998. * if controller power is disabled once suspended.
  3999. *
  4000. * @hsotg: Programming view of the DWC_otg controller
  4001. */
  4002. int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
  4003. {
  4004. struct dwc2_dregs_backup *dr;
  4005. int i;
  4006. dev_dbg(hsotg->dev, "%s\n", __func__);
  4007. /* Backup dev regs */
  4008. dr = &hsotg->dr_backup;
  4009. dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
  4010. dr->dctl = dwc2_readl(hsotg->regs + DCTL);
  4011. dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
  4012. dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
  4013. dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
  4014. for (i = 0; i < hsotg->num_of_eps; i++) {
  4015. /* Backup IN EPs */
  4016. dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
  4017. /* Ensure DATA PID is correctly configured */
  4018. if (dr->diepctl[i] & DXEPCTL_DPID)
  4019. dr->diepctl[i] |= DXEPCTL_SETD1PID;
  4020. else
  4021. dr->diepctl[i] |= DXEPCTL_SETD0PID;
  4022. dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
  4023. dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
  4024. /* Backup OUT EPs */
  4025. dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
  4026. /* Ensure DATA PID is correctly configured */
  4027. if (dr->doepctl[i] & DXEPCTL_DPID)
  4028. dr->doepctl[i] |= DXEPCTL_SETD1PID;
  4029. else
  4030. dr->doepctl[i] |= DXEPCTL_SETD0PID;
  4031. dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
  4032. dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
  4033. dr->dtxfsiz[i] = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
  4034. }
  4035. dr->valid = true;
  4036. return 0;
  4037. }
  4038. /**
  4039. * dwc2_restore_device_registers() - Restore controller device registers.
  4040. * When resuming usb bus, device registers needs to be restored
  4041. * if controller power were disabled.
  4042. *
  4043. * @hsotg: Programming view of the DWC_otg controller
  4044. * @remote_wakeup: Indicates whether resume is initiated by Device or Host.
  4045. *
  4046. * Return: 0 if successful, negative error code otherwise
  4047. */
  4048. int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup)
  4049. {
  4050. struct dwc2_dregs_backup *dr;
  4051. int i;
  4052. dev_dbg(hsotg->dev, "%s\n", __func__);
  4053. /* Restore dev regs */
  4054. dr = &hsotg->dr_backup;
  4055. if (!dr->valid) {
  4056. dev_err(hsotg->dev, "%s: no device registers to restore\n",
  4057. __func__);
  4058. return -EINVAL;
  4059. }
  4060. dr->valid = false;
  4061. if (!remote_wakeup)
  4062. dwc2_writel(dr->dctl, hsotg->regs + DCTL);
  4063. dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
  4064. dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
  4065. dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
  4066. for (i = 0; i < hsotg->num_of_eps; i++) {
  4067. /* Restore IN EPs */
  4068. dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
  4069. dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
  4070. dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
  4071. /** WA for enabled EPx's IN in DDMA mode. On entering to
  4072. * hibernation wrong value read and saved from DIEPDMAx,
  4073. * as result BNA interrupt asserted on hibernation exit
  4074. * by restoring from saved area.
  4075. */
  4076. if (hsotg->params.g_dma_desc &&
  4077. (dr->diepctl[i] & DXEPCTL_EPENA))
  4078. dr->diepdma[i] = hsotg->eps_in[i]->desc_list_dma;
  4079. dwc2_writel(dr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
  4080. dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
  4081. /* Restore OUT EPs */
  4082. dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
  4083. /* WA for enabled EPx's OUT in DDMA mode. On entering to
  4084. * hibernation wrong value read and saved from DOEPDMAx,
  4085. * as result BNA interrupt asserted on hibernation exit
  4086. * by restoring from saved area.
  4087. */
  4088. if (hsotg->params.g_dma_desc &&
  4089. (dr->doepctl[i] & DXEPCTL_EPENA))
  4090. dr->doepdma[i] = hsotg->eps_out[i]->desc_list_dma;
  4091. dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
  4092. dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
  4093. }
  4094. return 0;
  4095. }
  4096. /**
  4097. * dwc2_gadget_init_lpm - Configure the core to support LPM in device mode
  4098. *
  4099. * @hsotg: Programming view of DWC_otg controller
  4100. *
  4101. */
  4102. void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg)
  4103. {
  4104. u32 val;
  4105. if (!hsotg->params.lpm)
  4106. return;
  4107. val = GLPMCFG_LPMCAP | GLPMCFG_APPL1RES;
  4108. val |= hsotg->params.hird_threshold_en ? GLPMCFG_HIRD_THRES_EN : 0;
  4109. val |= hsotg->params.lpm_clock_gating ? GLPMCFG_ENBLSLPM : 0;
  4110. val |= hsotg->params.hird_threshold << GLPMCFG_HIRD_THRES_SHIFT;
  4111. val |= hsotg->params.besl ? GLPMCFG_ENBESL : 0;
  4112. dwc2_writel(val, hsotg->regs + GLPMCFG);
  4113. dev_dbg(hsotg->dev, "GLPMCFG=0x%08x\n", dwc2_readl(hsotg->regs
  4114. + GLPMCFG));
  4115. }
  4116. /**
  4117. * dwc2_gadget_enter_hibernation() - Put controller in Hibernation.
  4118. *
  4119. * @hsotg: Programming view of the DWC_otg controller
  4120. *
  4121. * Return non-zero if failed to enter to hibernation.
  4122. */
  4123. int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg)
  4124. {
  4125. u32 gpwrdn;
  4126. int ret = 0;
  4127. /* Change to L2(suspend) state */
  4128. hsotg->lx_state = DWC2_L2;
  4129. dev_dbg(hsotg->dev, "Start of hibernation completed\n");
  4130. ret = dwc2_backup_global_registers(hsotg);
  4131. if (ret) {
  4132. dev_err(hsotg->dev, "%s: failed to backup global registers\n",
  4133. __func__);
  4134. return ret;
  4135. }
  4136. ret = dwc2_backup_device_registers(hsotg);
  4137. if (ret) {
  4138. dev_err(hsotg->dev, "%s: failed to backup device registers\n",
  4139. __func__);
  4140. return ret;
  4141. }
  4142. gpwrdn = GPWRDN_PWRDNRSTN;
  4143. gpwrdn |= GPWRDN_PMUACTV;
  4144. dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
  4145. udelay(10);
  4146. /* Set flag to indicate that we are in hibernation */
  4147. hsotg->hibernated = 1;
  4148. /* Enable interrupts from wake up logic */
  4149. gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
  4150. gpwrdn |= GPWRDN_PMUINTSEL;
  4151. dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
  4152. udelay(10);
  4153. /* Unmask device mode interrupts in GPWRDN */
  4154. gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
  4155. gpwrdn |= GPWRDN_RST_DET_MSK;
  4156. gpwrdn |= GPWRDN_LNSTSCHG_MSK;
  4157. gpwrdn |= GPWRDN_STS_CHGINT_MSK;
  4158. dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
  4159. udelay(10);
  4160. /* Enable Power Down Clamp */
  4161. gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
  4162. gpwrdn |= GPWRDN_PWRDNCLMP;
  4163. dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
  4164. udelay(10);
  4165. /* Switch off VDD */
  4166. gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
  4167. gpwrdn |= GPWRDN_PWRDNSWTCH;
  4168. dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
  4169. udelay(10);
  4170. /* Save gpwrdn register for further usage if stschng interrupt */
  4171. hsotg->gr_backup.gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
  4172. dev_dbg(hsotg->dev, "Hibernation completed\n");
  4173. return ret;
  4174. }
  4175. /**
  4176. * dwc2_gadget_exit_hibernation()
  4177. * This function is for exiting from Device mode hibernation by host initiated
  4178. * resume/reset and device initiated remote-wakeup.
  4179. *
  4180. * @hsotg: Programming view of the DWC_otg controller
  4181. * @rem_wakeup: indicates whether resume is initiated by Device or Host.
  4182. * @param reset: indicates whether resume is initiated by Reset.
  4183. *
  4184. * Return non-zero if failed to exit from hibernation.
  4185. */
  4186. int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
  4187. int rem_wakeup, int reset)
  4188. {
  4189. u32 pcgcctl;
  4190. u32 gpwrdn;
  4191. u32 dctl;
  4192. int ret = 0;
  4193. struct dwc2_gregs_backup *gr;
  4194. struct dwc2_dregs_backup *dr;
  4195. gr = &hsotg->gr_backup;
  4196. dr = &hsotg->dr_backup;
  4197. if (!hsotg->hibernated) {
  4198. dev_dbg(hsotg->dev, "Already exited from Hibernation\n");
  4199. return 1;
  4200. }
  4201. dev_dbg(hsotg->dev,
  4202. "%s: called with rem_wakeup = %d reset = %d\n",
  4203. __func__, rem_wakeup, reset);
  4204. dwc2_hib_restore_common(hsotg, rem_wakeup, 0);
  4205. if (!reset) {
  4206. /* Clear all pending interupts */
  4207. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  4208. }
  4209. /* De-assert Restore */
  4210. gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
  4211. gpwrdn &= ~GPWRDN_RESTORE;
  4212. dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
  4213. udelay(10);
  4214. if (!rem_wakeup) {
  4215. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  4216. pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
  4217. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  4218. }
  4219. /* Restore GUSBCFG, DCFG and DCTL */
  4220. dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
  4221. dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
  4222. dwc2_writel(dr->dctl, hsotg->regs + DCTL);
  4223. /* De-assert Wakeup Logic */
  4224. gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
  4225. gpwrdn &= ~GPWRDN_PMUACTV;
  4226. dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
  4227. if (rem_wakeup) {
  4228. udelay(10);
  4229. /* Start Remote Wakeup Signaling */
  4230. dwc2_writel(dr->dctl | DCTL_RMTWKUPSIG, hsotg->regs + DCTL);
  4231. } else {
  4232. udelay(50);
  4233. /* Set Device programming done bit */
  4234. dctl = dwc2_readl(hsotg->regs + DCTL);
  4235. dctl |= DCTL_PWRONPRGDONE;
  4236. dwc2_writel(dctl, hsotg->regs + DCTL);
  4237. }
  4238. /* Wait for interrupts which must be cleared */
  4239. mdelay(2);
  4240. /* Clear all pending interupts */
  4241. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  4242. /* Restore global registers */
  4243. ret = dwc2_restore_global_registers(hsotg);
  4244. if (ret) {
  4245. dev_err(hsotg->dev, "%s: failed to restore registers\n",
  4246. __func__);
  4247. return ret;
  4248. }
  4249. /* Restore device registers */
  4250. ret = dwc2_restore_device_registers(hsotg, rem_wakeup);
  4251. if (ret) {
  4252. dev_err(hsotg->dev, "%s: failed to restore device registers\n",
  4253. __func__);
  4254. return ret;
  4255. }
  4256. if (rem_wakeup) {
  4257. mdelay(10);
  4258. dctl = dwc2_readl(hsotg->regs + DCTL);
  4259. dctl &= ~DCTL_RMTWKUPSIG;
  4260. dwc2_writel(dctl, hsotg->regs + DCTL);
  4261. }
  4262. hsotg->hibernated = 0;
  4263. hsotg->lx_state = DWC2_L0;
  4264. dev_dbg(hsotg->dev, "Hibernation recovery completes here\n");
  4265. return ret;
  4266. }