gadget.c 106 KB

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