gadget.c 124 KB

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