gadget.c 94 KB

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