gadget.c 96 KB

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