gadget.c 106 KB

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