gadget.c 127 KB

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