gadget.c 93 KB

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